TPCCLIB
Loading...
Searching...
No Matches
sim1cm.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include <stdio.h>
8#include <stdlib.h>
9#include <math.h>
10#include <time.h>
11#include <string.h>
12/*****************************************************************************/
13#include "tpccm.h"
14/*****************************************************************************/
15
16/*****************************************************************************/
30 double *t,
32 double *ci,
34 const int nr,
36 const double k1,
38 const double k2,
40 const double Vfit,
42 double *ct
43) {
44 int i;
45 double dt2;
46 double cii, ci_last, t_last;
47 double ct_last, cti, cti_last;
48
49
50 /* Check for data */
51 if(nr<2) return(1);
52 if(t==NULL || ci==NULL || ct==NULL) return(2);
53
54 /* Calculate curves */
55 t_last=0.0; cii=ci_last=0.0;
56 cti=ct_last=cti_last=0.0;
57 for(i=0; i<nr; i++) {
58 /* delta time / 2 */
59 dt2=0.5*(t[i]-t_last);
60 /* calculate values */
61 if(dt2<0.0) {
62 return(5);
63 } else if(dt2>0.0) {
64 /* input integral */
65 cii+=(ci[i]+ci_last)*dt2;
66 /* Tissue compartment and its integral */
67 ct[i] = (Vfit*ci[i] + k1*cii - k2*(cti_last+dt2*ct_last)) / (1.0 + dt2*k2);
68 cti = cti_last + dt2*(ct_last+ct[i]);
69 }
70 /* set very small values to zero
71 if(fabs(ct[i])<1.0e-12) ct[i]=0.0; */
72 /* prepare to the next loop */
73 t_last=t[i]; ci_last=ci[i];
74 ct_last=ct[i]; cti_last=cti;
75 }
76 return(0);
77}
78/*****************************************************************************/
79
80/*****************************************************************************/
95 double *t,
97 double *ca,
99 const int nr,
101 const double k1,
103 const double k2,
105 double *ct
106) {
107 int i;
108 double dt2;
109 double cai, ca_last, t_last;
110 double ct1, ct1_last;
111 double ct1i, ct1i_last;
112
113
114 /* Check for data */
115 if(nr<2) return 1;
116 if(t==NULL || ca==NULL || ct==NULL) return 2;
117
118 /* Check actual parameter number */
119 if(!(k1>=0.0)) return 3;
120
121 /* Calculate curves */
122 t_last=0.0; if(t[0]<t_last) t_last=t[0];
123 cai=ca_last=0.0;
124 ct1_last=ct1i_last=0.0;
125 ct1=ct1i=0.0;
126 for(i=0; i<nr; i++) {
127 /* delta time / 2 */
128 dt2=0.5*(t[i]-t_last);
129 /* calculate values */
130 if(dt2<0.0) {
131 return 5;
132 } else if(dt2>0.0) {
133 /* arterial integral */
134 cai+=(ca[i]+ca_last)*dt2;
135 /* tissue compartment and its integral */
136 ct1 = (k1*cai - k2*(ct1i_last+dt2*ct1_last)) / (1.0 + dt2*k2);
137 ct1i = ct1i_last + dt2*(ct1_last+ct1);
138 }
139 /* copy values to argument arrays */
140 ct[i]=ct1;
141 /* prepare to the next loop */
142 t_last=t[i]; ca_last=ca[i];
143 ct1_last=ct1; ct1i_last=ct1i;
144 }
145
146 return 0;
147}
148/*****************************************************************************/
149
150/*****************************************************************************/
166 double *t,
168 double *cai,
170 const int nr,
172 const double k1,
174 const double k2,
176 double *ct
177) {
178 int i;
179 double dt2;
180 double t_last;
181 double ct1, ct1_last;
182 double ct1i, ct1i_last;
183
184
185 /* Check for data */
186 if(nr<2) return 1;
187 if(t==NULL || cai==NULL || ct==NULL) return 2;
188
189 /* Check actual parameter number */
190 if(!(k1>=0.0)) return 3;
191
192 /* Calculate curves */
193 t_last=0.0; if(t[0]<t_last) t_last=t[0];
194 ct1_last=ct1i_last=0.0;
195 ct1=ct1i=0.0;
196 for(i=0; i<nr; i++) {
197 /* delta time / 2 */
198 dt2=0.5*(t[i]-t_last);
199 /* calculate values */
200 if(dt2<0.0) {
201 return 5;
202 } else if(dt2>0.0) {
203 /* tissue compartment and its integral */
204 ct1 = (k1*cai[i] - k2*(ct1i_last+dt2*ct1_last)) / (1.0 + dt2*k2);
205 ct1i = ct1i_last + dt2*(ct1_last+ct1);
206 }
207 /* copy values to argument arrays */
208 ct[i]=ct1;
209 /* prepare to the next loop */
210 t_last=t[i]; ct1_last=ct1; ct1i_last=ct1i;
211 }
212
213 return 0;
214}
215/*****************************************************************************/
216
217/*****************************************************************************/
231 double *t,
233 double *ca,
235 const int nr,
237 const double k1,
239 const double k2,
241 double *ct
242) {
243 int i;
244 double dt2;
245 double cap, ctp, tp; // 'p' marks previous
246
247
248 /* Check for data */
249 if(nr<2) return 1;
250 if(t==NULL || ca==NULL || ct==NULL) return 2;
251
252 /* Check actual parameter number */
253 if(!(k1>=0.0)) return 3;
254
255 /* Set 'previous' sample */
256 tp=0.0; if(t[0]<tp) tp=t[0];
257 cap=ctp=0.0;
258 /* Calculate curves */
259 for(i=0; i<nr; i++) {
260 /* delta time / 2 */
261 dt2=0.5*(t[i]-tp);
262 /* calculate values */
263 if(dt2<0.0) {
264 return 5;
265 } else if(dt2>=0.0) { // must be >= because ct[0] is not set elsewhere
266 ct[i]=(k1*dt2*(cap+ca[i]) + (1.0 - k2*dt2)*ctp) / (1.0 + k2*dt2);
267 }
268 /* prepare to the next sample */
269 tp=t[i]; cap=ca[i];
270 ctp=ct[i];
271 }
272
273 return 0;
274}
275/*****************************************************************************/
276
277/*****************************************************************************/
int simMBF(double *t, double *ci, const int nr, const double k1, const double k2, const double Vfit, double *ct)
Definition sim1cm.c:28
int simC1(double *t, double *ca, const int nr, const double k1, const double k2, double *ct)
Definition sim1cm.c:93
int simC1_d(double *t, double *ca, const int nr, const double k1, const double k2, double *ct)
Definition sim1cm.c:229
int simC1_i(double *t, double *cai, const int nr, const double k1, const double k2, double *ct)
Definition sim1cm.c:164
Header file for libtpccm.