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

Simulation of 2-tissue compartmental models. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpccm.h"

Go to the source code of this file.

Functions

int simC2 (double *t, double *ca, const int nr, const double k1, const double k2, const double k3, const double k4, double *ct, double *cta, double *ctb)
int simC2_i (double *t, double *cai, const int nr, const double k1, const double k2, const double k3, const double k4, double *ct, double *cta, double *ctb)

Detailed Description

Simulation of 2-tissue compartmental models.

Definition in file sim2cm.c.

Function Documentation

◆ simC2()

int simC2 ( double * t,
double * ca,
const int nr,
const double k1,
const double k2,
const double k3,
const double k4,
double * ct,
double * cta,
double * ctb )

Simulate tissue TAC using two-tissue compartment model and plasma TAC, at plasma TAC times.

Memory for ct must be allocated in the calling program. To retrieve the separate tissue compartment TACs, pointer to allocated memory for cta and/or ctb can be given; if compartmental TACs are not required, NULL pointer can be given instead.

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
simC2_i, simC1, simC1DI, simC2l, simC3s, simC3p, simSRTM, simSamples, simDispersion, parExampleTTACs, parExamplePerfectBolus
Parameters
tArray of time values.
caArray of arterial activities.
nrNumber of values in TACs.
k1Rate constant of the model.
k2Rate constant of the model.
k3Rate constant of the model.
k4Rate constant of the model.
ctPointer for TAC array to be simulated; must be allocated.
ctaPointer for 1st compartment TAC to be simulated, or NULL.
ctbPointer for 2nd compartment TAC to be simulated, or NULL.

Definition at line 31 of file sim2cm.c.

52 {
53 int i;
54 double dt2, r, u, v;
55 double cai, ca_last, t_last;
56 double ct1, ct1_last, ct2, ct2_last;
57 double ct1i, ct1i_last, ct2i, ct2i_last;
58
59
60 /* Check for data */
61 if(nr<2) return 1;
62 if(t==NULL || ca==NULL || ct==NULL) return 2;
63
64 /* Check parameters */
65 if(k1<0.0) return 3;
66
67 /* Calculate curves */
68 t_last=0.0; if(t[0]<t_last) t_last=t[0];
69 cai=ca_last=0.0;
70 ct1_last=ct2_last=ct1i_last=ct2i_last=0.0;
71 ct1=ct2=ct1i=ct2i=0.0;
72 for(i=0; i<nr; i++) {
73 /* delta time / 2 */
74 dt2=0.5*(t[i]-t_last);
75 /* calculate values */
76 if(dt2<0.0) {
77 return 5;
78 } else if(dt2>0.0) {
79 /* arterial integral */
80 cai+=(ca[i]+ca_last)*dt2;
81 /* Calculate partial results */
82 r=1.0+k4*dt2;
83 u=ct1i_last+dt2*ct1_last;
84 v=ct2i_last+dt2*ct2_last;
85 /* 1st tissue compartment and its integral */
86 ct1 = ( k1*cai - (k2 + (k3/r))*u + (k4/r)*v ) / ( 1.0 + dt2*(k2 + (k3/r)) );
87 ct1i = ct1i_last + dt2*(ct1_last+ct1);
88 /* 2nd tissue compartment and its integral */
89 ct2 = (k3*ct1i - k4*v) / r;
90 ct2i = ct2i_last + dt2*(ct2_last+ct2);
91 }
92 /* copy values to argument arrays */
93 ct[i]=ct1+ct2;
94 if(cta!=NULL) cta[i]=ct1;
95 if(ctb!=NULL) ctb[i]=ct2;
96 /* prepare to the next loop */
97 t_last=t[i]; ca_last=ca[i];
98 ct1_last=ct1; ct1i_last=ct1i;
99 ct2_last=ct2; ct2i_last=ct2i;
100 }
101
102 return 0;
103}

◆ simC2_i()

int simC2_i ( double * t,
double * cai,
const int nr,
const double k1,
const double k2,
const double k3,
const double k4,
double * ct,
double * cta,
double * ctb )

Simulate tissue TAC using two-tissue compartment model and plasma TAC, at plasma TAC times.

Memory for ct must be allocated in the calling program. To retrieve the separate tissue compartment TACs, pointer to allocated memory for cta and/or ctb can be given; if compartmental TACs are not required, NULL pointer can be given instead.

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

This version uses integral of arterial TAC as input function. Only advantage over simC2() is that the calculation of integral can be fully controlled and possibly more precise in some situations.

Returns
Function returns 0 when successful, else a value >= 1.
Author
Vesa Oikonen
See also
simC2, simC1, simC1_i, simC3s, simC3p, simSamples, simDelay
Parameters
tArray of time values.
caiArray of AUC 0-t of arterial activities.
nrNumber of values in TACs.
k1Rate constant of the model.
k2Rate constant of the model.
k3Rate constant of the model.
k4Rate constant of the model.
ctPointer for TAC array to be simulated; must be allocated.
ctaPointer for 1st compartment TAC to be simulated, or NULL.
ctbPointer for 2nd compartment TAC to be simulated, or NULL.

Definition at line 124 of file sim2cm.c.

145 {
146 int i;
147 double dt2, r, u, v;
148 double t_last;
149 double ct1, ct1_last, ct2, ct2_last;
150 double ct1i, ct1i_last, ct2i, ct2i_last;
151
152
153 /* Check for data */
154 if(nr<2) return 1;
155 if(t==NULL || cai==NULL || ct==NULL) return 2;
156
157 /* Check parameters */
158 if(k1<0.0) return 3;
159
160 /* Calculate curves */
161 t_last=0.0; if(t[0]<t_last) t_last=t[0];
162 ct1_last=ct2_last=ct1i_last=ct2i_last=0.0;
163 ct1=ct2=ct1i=ct2i=0.0;
164 for(i=0; i<nr; i++) {
165 /* delta time / 2 */
166 dt2=0.5*(t[i]-t_last);
167 /* calculate values */
168 if(dt2<0.0) {
169 return 5;
170 } else if(dt2>0.0) {
171 /* Calculate partial results */
172 r=1.0+k4*dt2;
173 u=ct1i_last+dt2*ct1_last;
174 v=ct2i_last+dt2*ct2_last;
175 /* 1st tissue compartment and its integral */
176 ct1 = ( k1*cai[i] - (k2 + (k3/r))*u + (k4/r)*v ) / ( 1.0 + dt2*(k2 + (k3/r)) );
177 ct1i = ct1i_last + dt2*(ct1_last+ct1);
178 /* 2nd tissue compartment and its integral */
179 ct2 = (k3*ct1i - k4*v) / r;
180 ct2i = ct2i_last + dt2*(ct2_last+ct2);
181 }
182 /* copy values to argument arrays */
183 ct[i]=ct1+ct2;
184 if(cta!=NULL) cta[i]=ct1;
185 if(ctb!=NULL) ctb[i]=ct2;
186 /* prepare to the next loop */
187 t_last=t[i];
188 ct1_last=ct1; ct1i_last=ct1i;
189 ct2_last=ct2; ct2i_last=ct2i;
190 }
191
192 return 0;
193}