# Introduction to Compartmental Modeling

A variety of biological systems maybe be modeled a well-mixed tanks, or as composite systems of interconnected well-mixed tanks (or compartments). Examples include chemical reactions inside cells or compartments within cells when spatial gradients of intracellular reactants are not important. Other examples (treated below) arise in pharmacokinetic modeling where distinct body compartments (e.g., blood plasma) are treated as well-mixed tanks. The key assumption in compartmental modeling is that within a fluid system the solutes of interest are homogeneously distributed with no spatial gradients existing over the time-scales of interest.

### Example 1: First-order washout from a well-mixed tank Example 1: Simple compartmental system.

As a first example, consider the system illustrated to the right with water flowing in and out at rates $F_{in}$ and $F_{out}$. A compartment model has a volume and a concentration of a substance. The product of the volume and the concentration is a quantity of material. The change in the quantity is described by mass balance equations.

The volume is usually designated as $V$, the concentration as $C$, and the amount of material as $Q$. The change in concentration, $dQ/dt$ is governed by sources (which add material to $Q$) and sinks which subtract material from $Q$. A source will appear as contributing a positive rate of change. A sink will contribute a negative rate of change. The change in $Q$ can be expressed as

$$\frac{dQ}{dt} = (\textrm{rate of mass flow in}) – (\textrm{rate of mass flow out})$$

$$= C_{in} F_{in} – C F_{out},$$

where $C_{in}$ is the concentration of solute in the inflow. This expression invokes the well-mixed assumption that the concentration in the outflow is equal to the overall concentration in the container.

If the inflow and outflow are equal, $F_{in} = F_{out} = F$, and the inflow concentration $C_{in}$ is 0, then the rate of change of concentration of a solute in the fluid in the container is given by:

$$\frac{dQ}{dt} = - F C$$ .

Since the volume is constant, $V dC/dt = dQ/dt$, and

$$\frac{dC}{dt} = -\frac{F}{V} C.$$

The time-dependent behavior of the potassium conductance can be simulated using a program that integrates the equation for $dC/dt$. Simulation in MATLAB requires a function that returns the computed time derivative $dC/dt$ as a function of $C(t)$. The MATLAB function CdT_exponential.m has the following syntax:

function f = dCdT_exponential(~,c,F,V)
% FUNCTION DCDT(C)
% Inputs: ~ - placeholder
% c - concentration (M)
% V - volume (constant, L)
% F - flow (constant, L/sec)
% Outputs: f - concentration time derivative
% {dc/dt}
f = -(F/V)*c;

This function can be integrated using MATLAB to simulate washout of solute from the container with the following script: Simple exponential washout.

% Set initial condition, flow and volume
c0 = 1; % M
V = 1; % L
F = 1; % L/sec
[t,c] = ode23s(@dCdT_exponential,[0 5],c0,[],F,V);
plot(t,c)
xlabel('t (s)');
ylabel('c (M)');
set(gca,'fontsize',16)

which gives the output illustrated to the right.

To run this example on the web using JSim, see Comp1Flow.

### Example 2: First order reversible reaction in a single compartment

A net change in concentration ($C_{substance}$) can be expressed as:

$$dC/dt = -(\textrm{rate of consumption of substance}) + (\textrm{rate of production of substance}) \, .$$

In this example we consider concentrations of two substances, $A$ and $B$, dissolved in fluid contained in single compartment of constant volume.  Assume concentration of substance $A$ converts to an amount of substance $B$ at a rate specified by $G_{a2b}$ and $B$ converts back to $A$ at the rate $G_{b2a}$. For species A and B, the change in concentration for two substances over time period t is given by:

$$dA/dt=−G_{a2b} A + G_{b2a} B$$
and
$$dB/dt=G_{a2b} A−G_{b2a} B \, ,$$

where $G_{a2b}$ and $G_{b2a}$ are first order rate constants, with units of per unit time.

Just like in Example 1, MATLAB requires a function that returns the computed time derivatives of the concentration variables. In this case we have two state variables $A(t)$ and $B(t)$. The MATLAB function DXDT_AtoB_reaction returns the time derivatives of these variables as a two-component vector:

function f = DXDT_AtoB_reaction(~,x,Ga2b,Gb2a)
% FUNCTION DXDT_AtoB_reaction(C)
% Inputs: ~ - placeholder
% x - vector of state variables
% x(1) = concentration of A (mM)
% x(2) = concentration of B (mM)
% Ga2b - rate constant for A -> B reaction
% Gb2a - rate constant for B -> A reaction
% Outputs: f - concentration time derivative
% {dA/dt, dB/dt}
A = x(1); % concentration of A
B = x(2); % concentration of B
dBdT = +Ga2b*A - Gb2a*B; Simple reaction A converts to B

This function can be integrated with the following script:

% Set initial condition and rate constants
A0 = 0.5; % mM
B0 = 1.0; % mM
Ga2b = 0.1; % sec^-1
Gb2a = 0.3; % sec^-1
[t,x] = ode23s(@DXDT_AtoB_reaction,[0 5],[A0 B0],[],Ga2b,Gb2a);
plot(t,x(:,1),t,x(:,2))
xlabel('t (s)');
ylabel('c_A, c_B (mM)');
set(gca,'fontsize',16)

which gives the output illustrated to the right.

To run this example on the web using JSim, see Comp1Reaction.

### Example 3: First-order reaction with fluid flow in a single compartment

The equations in the previous two examples can be combined to model a single compartment system
with both flow and two reactants:

$$dC/dt = (\textrm{rate of mass flow in}) – (\textrm{rate of mass flow out}) -$$

$$(\textrm{rate of consumption of substance}) + (\textrm{rate of production of substance}) \, .$$

As in example 2, a chamber of constant volume $V$ has two substances, $A$ and $B$, dissolved in fluid. The chamber also has a constant fluid inflow of $F$ with a constant fluid outflow. The inflow carries some concentration of substance $A$, designated $A_{in}$. The concentrations of $A$ and $B$ over time are given by the equations:

$$dA/dt=(F*(A_{in}-A))/V−G_{a2b} A + G_{b2a} B$$

and

$$dB/dt=-(F*B)/V + G_{a2b} A−G_{b2a} B \, .$$

Just like the previous examples, MATLAB requires a function that returns the computed time derivatives of the concentration variables. In this case we have two state variables $A(t)$ and $B(t)$. The MATLAB function DXDT_AtoB_flowreaction returns the time derivatives of these variables as a two-component vector:

function f = DXDT_AtoB_flowreaction(~,x,A_in,V,F,Ga2b,Gb2a)
% FUNCTION DXDT_AtoB_reaction(C)
% Inputs: ~ - placeholder
% x - vector of state variables
% x(1) = concentration of A (mM)
% x(2) = concentration of B (mM)
% A_in - concentration A inflow
% V - Chamber volume
% F - Fluid inflow into chamber
% Ga2b - rate constant for A -> B reaction
% Gb2a - rate constant for B -> A reaction
% Outputs: f - concentration time derivative
% {dA/dt, dB/dt}
A = x(1); % concentration of A
B = x(2); % concentration of B
dAdT = F*(A_in - A)/V - Ga2b*A + Gb2a*B;
dBdT = -F*B/V + Ga2b*A - Gb2a*B; Simple reaction of A and B with inflow of A

This function can be integrated with the following script:

%Set the initial concentrations of A and B, flows, rate constants and volume.
Ain = 0.1; % mM/sec
V = 1.0; % ml
F = 1.0; % ml/sec
Ga2b = 0.1; % mM/sec
Gb2a = 0.3; % mM/sec
A0 = 0.5; % mM
B0 = 1.0; % mM

[t, x] = ode23s(@DXDT_AtoB_flowreaction,[0 5],[A0 B0], [],Ain,V,F,Ga2b,Gb2a);
plot(t,x(:,1),t,x(:,2))
xlabel('t (s)');
ylabel('c_A, c_B (mM)');
set(gca,'fontsize',16)

which gives the output illustrated to the right.

To run this example on the web using JSim, see Comp1FlowReaction.

### Example 4: First Order Exchange Between Two Compartments

The exchange between two compartments with different concentrations of material can be modeled with:

$$d(\textrm{Concentration in Compartment})/dt =$$

$$((\textrm{Concentration in Compartment}) - (\textrm{Concentration in Other Compartment})$$

$$/ (\textrm{Volume of Compartment})$$.

This example considers two compartments with different concentrations of a substance. Compartment 1 has volume $V_1$ and concentration $A$, while compartment 2 has volume $V_2$ and concentration $B$. The two compartments are adjacent and a permeable membrane allows material to pass between the two at a rate $PS$. The rate is the same in either direction. The concentration in compartment 1 can be expressed as:

$$dA/dt = PS*(B-A)/V_1$$

Compartment 2 can be modeled by:

$$dB/dt = PS*(A-B)/V_2$$

Just like the previous examples, MATLAB requires a function that returns the computed time derivatives of the concentration variables. In this case we have two state variables $A(t)$ and $B(t)$. The MATLAB function DXDT_2Compartment_Exchange returns the time derivatives of these variables as a two-component vector:

function f = DXDT_2Compartment_Exchange(~,x,V1,V2,PS)
% FUNCTION DXDT_AtoB_reaction(C)
% Inputs: ~ - placeholder
% x - vector of state variables
% x(1) = concentration in compartment A (mM)
% x(2) = concentration in compartment B (mM)
% V1 - Chamber 1 volume
% V2 - Chamber 2 volume
% PS - Exchange rate between two compartments
% Outputs: f - concentration time derivative
% {dA/dt, dB/dt}
A = x(1); % concentration of A
B = x(2); % concentration of B
dBdT = PS*(A-B)/V2; This function can be integrated with the following script:

%Set the initial concentrations of A and B, flows, rate constants and volume.
V1 = 0.05; % ml
V2 = 0.15; % ml
PS = 5/60; % mM/sec
A0 = 1.5; % mM
B0 = 0.5; % mM

[t, x] = ode23s(@DXDT_2Compartment_Exchange,[0 5],[A0 B0], [],V1,V2,PS);
plot(t,x(:,1),t,x(:,2))
xlabel('t (s)');
ylabel('c_A, c_B (mM)');
set(gca,'fontsize',16)

which gives the output illustrated to the right.

To run this example on the web using JSim, see Comp2Exchange. dCdT_exponential.m DXDT_AtoB_reaction.m DXDT_AtoB_flowreaction.m DXDT_2Compartment_Exchange.m