TPCCLIB
Loading...
Searching...
No Matches
fit_av.c File Reference

Fit A-V difference using compartmental model. More...

Go to the source code of this file.

Functions

int simC3vb (double *t, double *cab, const int nr, double f, double k1, double k2, double k3, double k4, double k5, double k6, double *cvb, double *ct)

Detailed Description

Fit A-V difference using compartmental model.

Author
Vesa Oikonen

Definition in file fit_av.c.

Function Documentation

◆ simC3vb()

int simC3vb ( double * t,
double * cab,
const int nr,
double f,
double k1,
double k2,
double k3,
double k4,
double k5,
double k6,
double * cvb,
double * ct )

Simulate venous blood TAC using 1-3 tissue compartment model (compartments in series)

Memory for cvb must be allocated in the calling program. To retrieve the tissue TAC, pointer to allocated memory for ct can be given.

The units of rate constants must be related to the time unit; 1/min and min, or 1/sec and sec.

Returns
Function returns 0 when successful, else a value >= 1.
Author
Vesa Oikonen
See also
simC3vs, simC1, simC3p, simC3vp
Parameters
tArray of time values; must be in increasing order.
cabArray of arterial blood activities.
nrNumber of values (samples) in TACs.
fPerfusion; must be f>=K1.
k1Rate constant of the model; must be K1<=f.
k2Rate constant of the model.
k3Rate constant of the model.
k4Rate constant of the model.
k5Rate constant of the model.
k6Rate constant of the model.
cvbPointer for venous blood TAC array to be simulated; must be allocated
ctPointer for tissue TAC to be simulated, or NULL

Definition at line 109 of file fit_av.c.

134 {
135 int i;
136 double b, c, d, w, z, dt2;
137 double cai, ca_last, t_last;
138 double ct1, ct1_last, ct2, ct2_last, ct3, ct3_last;
139 double ct1i, ct1i_last, ct2i, ct2i_last, ct3i, ct3i_last;
140
141
142 /* Check for data */
143 if(nr<2) return 1;
144 if(cvb==NULL) return 2;
145
146 /* Check actual parameter number */
147 if(!(f>=0.0) || !(k1>=0.0) || k1>f) return 3;
148 if(k3<=0.0) {k3=0.0; if(k2<=0.0) k2=0.0;}
149 else if(k5<=0.0) {k5=0.0; if(k4<=0.0) k4=0.0;}
150 else {if(k6<=0.0) k6=0.0;}
151
152 /* Calculate curves */
153 t_last=0.0; if(t[0]<t_last) t_last=t[0];
154 cai=ca_last=0.0;
155 ct1_last=ct2_last=ct3_last=ct1i_last=ct2i_last=ct3i_last=0.0;
156 ct1=ct2=ct3=ct1i=ct2i=ct3i=0.0;
157 for(i=0; i<nr; i++) {
158 /* delta time / 2 */
159 dt2=0.5*(t[i]-t_last);
160 /* calculate values */
161 if(dt2<0.0) {
162 return 5;
163 } else if(dt2>0.0) {
164 /* arterial integral */
165 cai+=(cab[i]+ca_last)*dt2;
166 /* partial results */
167 b=ct1i_last+dt2*ct1_last;
168 c=ct2i_last+dt2*ct2_last;
169 d=ct3i_last+dt2*ct3_last;
170 w=k4 + k5 - (k5*k6*dt2)/(1.0+k6*dt2);
171 z=1.0+w*dt2;
172 /* 1st tissue compartment and its integral */
173 ct1 = (
174 + k1*z*cai + (k3*k4*dt2 - (k2+k3)*z)*b
175 + k4*c + k4*k6*dt2*d/(1.0+k6*dt2)
176 ) / ( z*(1.0 + dt2*(k2+k3)) - k3*k4*dt2*dt2 );
177 ct1i = ct1i_last + dt2*(ct1_last+ct1);
178 /* 2nd tissue compartment and its integral */
179 ct2 = (k3*ct1i - w*c + k6*d/(1.0+k6*dt2)) / z;
180 ct2i = ct2i_last + dt2*(ct2_last+ct2);
181 /* 3rd tissue compartment and its integral */
182 ct3 = (k5*ct2i - k6*d) / (1.0 + k6*dt2);
183 ct3i = ct3i_last + dt2*(ct3_last+ct3);
184 }
185 if(f>0.0) {
186 double dct = k1*cab[i] - k2*ct1;
187 cvb[i] = cab[i] - dct/f;
188 } else
189 cvb[i]=cab[i];
190
191 /* copy values to argument arrays; set very small values to zero */
192 if(ct!=NULL) {ct[i]=ct1+ct2+ct3; if(fabs(ct[i])<1.0e-12) ct[i]=0.0;}
193
194 /* prepare to the next loop */
195 t_last=t[i]; ca_last=cab[i];
196 ct1_last=ct1; ct1i_last=ct1i;
197 ct2_last=ct2; ct2i_last=ct2i;
198 ct3_last=ct3; ct3i_last=ct3i;
199 }
200
201 return 0;
202}