Two Compartment Model Solution with Mathematica and Illustration of Central Volume Principle
Author: Michael Jerosch-Herold

Abstract
This didactic example starts with a two-compartment model as illustrated in Figure 1 of this Mathematica notebook. The two compartment model could be used to analyze the contrast enhancement in myocardial tissue.  The analytical solution of the two-compartment model is combined with appropriate initial conditions to obtain the impulse response. An impulse response is calculated for specific parameter values. Next the use of the impulse response is demonstrated by calculating the tissue response for a arterial input with the shape of a gamma-variate function.

First we specify the differential equations for a lumped compartment model, which comprises two compartments, with a permeable barrier between them, and flow through one compartment, representing the vascular space. c1(t) denotes the concentration of tracer in compartment 1, the vascular space, and c2(t) denotes the concentration in compartment 2, taken to represent the interstitial space in this example. F denotes the blood flow, V1,2 are the volumes of compartments 1,2, and PS is the permeability surface area product.

The two compartment model is described mathematically by a system of coupled linear differential equations:

c1'[t]==(-(F/v1))*c1[t]-(PS/v1)*c1[t]+(PS/v1)*c2[t],c2'[t]==(PS/v2)*c1[t]-(PS/v2)*c2[t]

eqns = {c1'[t] == (-(F/v1))*c1[t] - (PS/v1)*c1[t] + (PS/v1)*c2[t], c2'[t] == (PS/v2)*c1[t] - (PS/v2)*c2[t]};

Now we solve the system of coupled differential equations. This gives the general solution

DSolve[eqns, {c1[t], c2[t]}, t] // Simplify

The algebra looks rather tedious, if done by hand. C[1] and C[2] are undetermined coefficients for the general solution of the system of differential equations. We now specify a solution for initial conditions appropriate for the determination of the impulse response. After a unit impulse input the concentration of contrast immediately after the impulse (t=0+) is 1 in compartment #1. Compartment #2 initially has no tracer.

ImpulseResp=DSolve[Join[eqns, {c1[0]==1, c2[0]==0}], {c1,c2}, t] // Simplify

Amplitude =v1 c1[t]/(v1+v2) /. ImpulseResp /.  {t ->0}

Note that c2(t), the tracer concentration in compartment 2, is indeed 0 at t=0, in accordance with the specified initial conditions. To look at an example of the tracer concentration curves we specify the parameter values for the two compartment model.

parvals={PS->0.4, F->0.77, v1->0.08, v2->0.16};

Apply the parameter values to the solution with the above-specified initial conditions:

ImpulseRespSimulated=ImpulseResp/. parvals ;

Area under the external monitoring curve (AUC)

AUC=NIntegrate[(v1*c1[t]+v2 c2[t])/(v1+v2) /. ImpulseResp /. parvals, {t , 0, Infinity}]

The externally detected tracer after an impulse input, if normalized by its value at time t=0, can be interpreted as the probability that a tracer molecule remains in the two-compartment unit, i.e. in the tissue region. Alternatively one can normalize the same curve to unit area. In this latter case the resulting graph gives the probability for the residence times in the two-compartment region.

Plot the solution for the specified parameter values. Show the tracer concentration in the vascular and interstitial compartments, and the weighted sum of the two, which corresponds to the contrast-enhancement one would observe by external detection, using an imaging device.

TotalRes= (v1 c1[t]+ v2 c2[t])/v1/.ImpulseResp /. {v1->0.1, v2->0.22};

Bfunc := Function[{x, y, z}, TotalRes/. {t-> x, PS-> y, F->z}, Listable]

In the next graph you can interactively change the blood flow, and the permeability-surface area product through the sliders with the "PS" and "F" labels at the top. This allows to see how the changes of these parameters affect the probability for the tracer. Note that an increase of PS causes a prolonged washout of contrast, while at the smallest PS value the tracer is confined to the vascular compartment and the traversal of the tracer through the tissue unit is relatively quicker.

Manipulate[Plot[Bfunc[t, PS, F], { t, 0.0, 0.1, 2}, AxesLabel->{"Time [min]", "Tracer Residue"}, AxesOrigin->{0,0}, PlotStyle->{Red, Thickness[0.01]}, PlotRange->{{0,2},{0,1.0}}], {PS, 0.0, 1}, {F, 0.3, 2}]

Plot[{(v1 c1[t]+ v2 c2[t])/AUC,v2 c2[t]/AUC, v1 c1[t]/AUC} /.ImpulseResp /. parvals, { t, 0.0, 0.1, 2},  PlotStyle->{{Red, Thickness[0.005], Dashing[{Tiny}]}}, PlotRange->{{0,2},{0,1.0}}, AxesLabel->{"Time [min]", "Impulse Response"}]

Note how the amplitude of the impulse response equals the value for F, the flow.

Flow=((v1 c1[t]+ v2 c2[t])/AUC/.ImpulseResp /. parvals)/.{t->0}

As an aside: the solution of this constant coefficient system can also be obtained using the matrix exponential, and it yields the same results as above, as is readily verified.

solMat =MatrixExp[{{-(F+PS)/v1,PS/v1},{PS/v2,-PS/v2}} t].{1,0}//Simplify

f=solMat /. parvals

pp= solMat /. {F->0.1,  v1->0.1, v2->0.22};

Plot3D[v1 pp[[1]]+v2 pp[[2]] /. parvals, {t, 0,10}, {PS, 0.001, 0.05}]

cv[t_]:=f[[1]]; ci[t_]:=f[[2]]

Plot[(v1 cv[t]+v2 ci[t])/AUC/. parvals, {t, 0, 6}, PlotRange -> Full, AxesLabel->{"Time [min]", "Tracer Residue"}, Filling -> Axis]

Calculate convolution of impulse response with a gamma-variate input function. Compare this with the direct solution of the coupled differential equations using the same gamma-variate input function.

cgammavar = Compile[{A, u, tau, x}, If[x>0, A  x^u E^(-x/tau), 0]]

rr=(v1 cv[t]+v2 ci[t])/AUC /. parvals;

dt=0.02;

AIF=Table[{t, cgammavar[10^5/3.0, 2.0, 0.1, t] }, {t, 0, 2, dt}];

R represents the impulse response of the two-compartment system. It will be used here for convolution with the arterial input function AIF, which has the form of a gamma-variate. The convolution of AIF with R will give the tissue response to the AIF.

R = Table[{t, If[t>0, ((v1 cv[t]+v2 ci[t])/AUC/. parvals )[[1]], 0]}, {t, 0, 6, dt}];

Rvals = R[[#, 2]]& /@ Range[1,Length[R]];

AIFvals=AIF[[#,2]]& /@ Range[1, Length[AIF]];

Convolution of the two lists representing the AIF and impulse response R, with some noise added.

q=ListConvolve[Rvals v1 /.parvals, AIFvals, {1,-1}, 0];

qn=  q+ Table[ RandomReal[NormalDistribution[0,2]],{t,0,(Length[q]-1)*dt, dt}];

qn2 = {(#-1) dt, qn[[#]]} & /@ Range[1,Length[qn]];

ListPlot[{qn2, AIF}, PlotRange->{{0,2},{0,200}}, AxesLabel->{"Time [min]", "Tracer Residue"}]