Tutorials › Quantitative Systems Pharmacology (QSP) › The Method of Lines for Spatial Models

Spatial and Agent-Based Approaches · Tutorial 225 of 799

The Method of Lines for Spatial Models

Learn to construct and interpret method-of-lines ODE systems for spatial pharmacology models, including the effects of mesh spacing on computational size and stiffness.

Advanced 12 min read

What You'll Learn

  • Translate a spatial diffusion-reaction equation into a system of ODEs.
  • Incorporate initial and boundary conditions into the semi-discrete model.
  • Extend the construction to multiple species and spatial dimensions.
  • Estimate how mesh spacing changes the number of states and fastest diffusion time scale.
  • Recognize stiffness and common implementation errors in method-of-lines simulations.

From a Spatial PDE to an ODE System

A spatial pharmacology model often describes how a concentration changes both over time and across tissue. A partial differential equation (PDE) expresses this continuous evolution, but many ODE solvers are designed to advance a finite vector of state variables in time. The method of lines connects these representations: discretize space, retain continuous time, and solve the resulting ODE system.

The earlier material on finite-difference discretization of diffusion introduced spatial difference formulas. Here the focus is on how those formulas become a complete time-dependent system, how boundary and initial conditions enter that system, and what the mesh implies for computation. The result is semi-discrete: space is represented at finitely many points, while time remains continuous until an ODE solver advances the solution.

Definition: The method of lines is a numerical approach in which spatial derivatives in a PDE are replaced by discrete approximations at mesh points, producing a system of ODEs in time. The vector of all mesh-point states is the semi-discrete state; its initial value is obtained by evaluating the PDE’s initial condition on the mesh.

For a concentration \(u(x,t)\) in a one-dimensional tissue, consider a constant-diffusion reaction-diffusion equation,

$$ \frac{\partial u}{\partial t} = D\frac{\partial^2 u}{\partial x^2}+R(u,x,t), \qquad 0<x<L,\quad t>0. $$

Here \(D\) is the diffusion coefficient, with units of length squared per time, and \(R\) is the local reaction rate, with units of concentration per time. The PDE also requires an initial condition \(u(x,0)=u_0(x)\) and boundary conditions at \(x=0\) and \(x=L\). For the basic construction below, assume a uniform spatial mesh, sufficient spatial smoothness for the centered second-difference approximation, and known boundary conditions. Reactions may be nonlinear or coupled to other species.

Constructing the Semi-Discrete Equations

Let \(x_i=ih\), where \(h=L/(N+1)\), and let \(u_i(t)\) approximate \(u(x_i,t)\) at the \(N\) interior mesh points, \(i=1,\ldots,N\). With fixed, zero-concentration boundary conditions, \(u_0(t)=u_{N+1}(t)=0\). Replace the second spatial derivative at each interior point by its centered difference:

$$ \frac{\partial^2 u}{\partial x^2}(x_i,t) \ \approx\ \frac{u_{i-1}(t)-2u_i(t)+u_{i+1}(t)}{h^2}. $$

Substitution into the PDE gives one ODE for every interior point:

$$ \frac{du_i}{dt} = D\frac{u_{i-1}-2u_i+u_{i+1}}{h^2} + R(u_i,x_i,t), \qquad i=1,\ldots,N. $$

The boundary values are known inputs to the equations; they are not additional unknowns in this particular setup. Collecting the interior values into \(\mathbf{u}(t)=(u_1(t),\ldots,u_N(t))^\mathsf{T}\) gives \(\mathbf{u}'=F(t,\mathbf{u})\). Its initial vector is \(\mathbf{u}(0)=(u_0(x_1),\ldots,u_0(x_N))^\mathsf{T}\), where \(u_0(x)\) here denotes the initial spatial profile, not the boundary-node value.

Why the Discretization Has This Form

The centered difference follows from Taylor expansion about \(x_i\). For a sufficiently smooth function,

$$ u(x_i+h,t)=u_i+h u_x+\frac{h^2}{2}u_{xx} +\frac{h^3}{6}u_{xxx}+\frac{h^4}{24}u_{xxxx}+\cdots, $$

and the corresponding expansion at \(x_i-h\) has the odd-order terms with opposite signs. Adding the two expansions cancels those terms:

$$ u(x_i-h,t)-2u_i+u(x_i+h,t) = h^2u_{xx}(x_i,t)+\frac{h^4}{12}u_{xxxx}(x_i,t)+\cdots. $$

Dividing by \(h^2\) shows that the approximation equals \(u_{xx}(x_i,t)\) plus a leading error proportional to \(h^2\), provided the needed derivatives are bounded. The derivation explains both the formula and its conditions: it uses equally spaced points and a sufficiently smooth solution. It does not guarantee that the full simulation is accurate, since the time solver and the boundary treatment also contribute errors.

Worked Example: Three Interior Nodes With Decay

Consider a hypothetical tissue segment with zero concentration at its two ends. Use three interior nodes with spacing \(h=0.5\) mm, diffusion coefficient \(D=0.20\) mm\(^2\)/h, and first-order local loss \(R(u)=-ku\), with \(k=0.10\) h\(^{-1}\). Suppose the interior concentrations at a particular time are \(u_1=1.0\), \(u_2=2.0\), and \(u_3=1.0\) μM. The boundary values are \(u_0=u_4=0\) μM.

The diffusion coefficient in the semi-discrete equation is \(D/h^2=0.20/(0.5)^2=0.80\) h\(^{-1}\). Thus each equation is \(du_i/dt=0.80(u_{i-1}-2u_i+u_{i+1})-0.10u_i\). At node 1,

$$ \frac{du_1}{dt} =0.80(0-2(1.0)+2.0)-0.10(1.0) =-0.10\ \text{μM/h}. $$

At node 2, \(du_2/dt=0.80(1.0-2(2.0)+1.0)-0.10(2.0)=-1.8\) μM/h. By symmetry, \(du_3/dt=-0.10\) μM/h. The diffusion term redistributes concentration between nodes, while the reaction term removes it locally. These are instantaneous rates, not concentrations at a later time; an ODE solver uses them to advance the full state vector.

Coupled Species and Boundary Conditions

For \(m\) interacting species on the same mesh, each species has a state at each interior point. The semi-discrete system then has \(mN\) unknowns. At each node, the reaction term can depend on all local species, while diffusion couples each species to neighboring nodes. For instance, binding, degradation, or signaling can be represented in the local reaction terms without changing the basic spatial construction.

Boundary conditions determine how the first and last interior equations are completed. With fixed concentration boundaries, their known values appear as neighboring values in the difference formula. With zero-flux boundaries, the normal spatial derivative is specified instead; an implementation must use that condition to construct the boundary-adjacent difference equations. Time-varying prescribed boundary concentrations similarly enter as known time-dependent terms. Boundary values should not be silently treated as zero unless that is the specified physical condition.

Worked Example: Two Reacting Species on One Mesh

Consider hypothetical concentrations \(A\) and \(B\), with \(A\) converting to \(B\) at rate \(k_{\mathrm{on}}A\), \(B\) converting back at rate \(k_{\mathrm{off}}B\), and \(B\) degrading at rate \(k_{\mathrm{deg}}B\). At one interior node, let \(h=0.20\) mm, \(D_A=0.040\) mm\(^2\)/h, \(D_B=0.010\) mm\(^2\)/h, \(k_{\mathrm{on}}=0.30\) h\(^{-1}\), \(k_{\mathrm{off}}=0.10\) h\(^{-1}\), and \(k_{\mathrm{deg}}=0.20\) h\(^{-1}\). Let the node values be \(A_i=2.0\) μM and \(B_i=1.0\) μM, with neighboring values \(A_{i-1}=1.5\), \(A_{i+1}=1.0\), \(B_{i-1}=0.8\), and \(B_{i+1}=1.2\) μM.

The diffusion multipliers are \(D_A/h^2=0.040/0.040=1.0\) h\(^{-1}\) and \(D_B/h^2=0.010/0.040=0.25\) h\(^{-1}\). The local second-difference numerators are \(1.5-2(2.0)+1.0=-1.5\) μM for \(A\), and \(0.8-2(1.0)+1.2=0\) μM for \(B\). Therefore,

$$ \frac{dA_i}{dt} =1.0(-1.5)-0.30(2.0)+0.10(1.0) =-2.0\ \text{μM/h}, $$

and

$$ \frac{dB_i}{dt} =0.25(0)+0.30(2.0)-0.10(1.0)-0.20(1.0) =0.30\ \text{μM/h}. $$

These rates illustrate how the method combines spatial flux effects and local reaction kinetics in each ODE. On a mesh with \(N\) interior nodes, this two-species model has \(2N\) state variables, not merely two states.

Mesh Spacing, System Size, and Stiffness

For a one-dimensional domain of fixed length \(L\), reducing \(h\) increases the number of mesh points approximately in proportion to \(1/h\). In \(d\) spatial dimensions, a uniformly refined grid can require a number of unknowns proportional to \(h^{-d}\) per species. Thus, a modest refinement in each spatial direction can greatly increase the ODE dimension and the work required to evaluate and store its state.

Finer spacing also creates faster diffusion modes. For the one-dimensional discrete diffusion operator with fixed zero-concentration boundaries, the eigenvalues are

$$ \lambda_k = -\frac{4D}{h^2} \sin^2\!\left(\frac{k\pi}{2(N+1)}\right), \qquad k=1,\ldots,N. $$

The most negative eigenvalue has magnitude close to \(4D/h^2\) on a fine mesh. Meanwhile, the slowest mode over a fixed domain has a rate on the scale of \(D/L^2\). Their separation grows roughly as \(1/h^2\). When a system contains both fast and slow time scales, it can be stiff: an explicit time-stepping method may need very small steps for numerical stability even when the quantities of interest change slowly. Stiffness is a property of the system’s time scales and numerical representation, not evidence that the biological process itself is changing rapidly everywhere.

Worked Example: Estimating Diffusion Stiffness

For a hypothetical one-dimensional tissue of length \(L=1.0\) mm, take \(D=0.010\) mm\(^2\)/min and \(h=0.10\) mm. There are \(N=L/h-1=9\) interior nodes with fixed zero-concentration boundaries. The largest eigenvalue magnitude is

$$ |\lambda_9| = \frac{4(0.010)}{(0.10)^2} \sin^2\!\left(\frac{9\pi}{20}\right) \approx 0.390\ \text{min}^{-1}. $$

The slowest diffusion rate is \(|\lambda_1|=0.40\sin^2(\pi/20)\approx0.00979\) min\(^{-1}\), giving a rate ratio of approximately \(0.390/0.00979=39.8\). For this linear diffusion system, forward Euler requires \(\Delta t\leq 2/|\lambda_9|\), or about \(5.13\) min, for stability. If the spacing is halved while \(L\) remains fixed, the fastest diffusion rate is approximately four times larger, so the corresponding explicit stability limit is approximately four times smaller. This estimate concerns stability, not a guarantee of adequate temporal accuracy.

Implementation Checks and Alternatives

A useful implementation check is to verify the units and structure of the semi-discrete equations. The ratio \(D/h^2\) must have units of inverse time, so multiplying it by a concentration gives a concentration rate. Diffusion should couple a node to its spatial neighbors, and reaction terms should be evaluated with the intended species and parameters at that node. For zero-flux boundaries, a constant concentration profile should have zero diffusion contribution; if it does not, the boundary formula or indexing may be wrong.

Common warning signs include boundary values being omitted or counted as unknowns inconsistently, a missing factor of \(h^2\), and a mesh index shifted by one. A negative concentration or oscillatory profile can indicate unsuitable time steps, a problematic boundary treatment, or a discretization that does not preserve the expected properties. Such behavior is a diagnostic to investigate, not a reason to clip values automatically: clipping changes the numerical model and can conceal the source of the error. When the system has fast diffusion modes alongside slow reactions, compare a stiff ODE solver with an explicit method and inspect solver step sizes and error messages.

The method of lines separates spatial approximation from time integration. A fully discrete method chooses both a spatial grid and time steps in one scheme; it can be efficient for structured problems, but its stability and accuracy depend on the combined discretization. A PDE solver may provide specialized spatial and temporal algorithms without exposing the resulting ODE system directly. Finite-volume methods instead formulate flux balances over spatial cells, which can be advantageous when conservation and irregular cell geometry are central. These approaches are alternatives, not interchangeable guarantees: each still requires appropriate boundary conditions, resolution, and numerical checks.

The method of lines does not eliminate modeling or numerical limitations. The centered spatial difference relies on adequate smoothness and a suitable mesh, and the semi-discrete equations inherit any incorrect PDE assumptions or boundary conditions. A large ODE system may be expensive, and stiffness may require an implicit solver or other stiff-system method. Spatial resolution and time-integration error must be assessed separately; a plausible-looking trajectory alone does not establish that either is small.

Key takeaway: The method of lines converts each spatial mesh value into a time-dependent state and each spatial derivative into coupling among those states. Refining the mesh increases both the ODE dimension and the fastest diffusion rate, so spatial representation and solver choice must be considered together.

Check Your Understanding

Answer each question, then compare with the solutions below.

  1. For \(u_t=Du_{xx}-ku\), what ODE does the centered method of lines give at an interior node?
  2. A model has 12 interior nodes and three species, each represented at every node. How many semi-discrete state variables does it have?
  3. If \(h\) is halved with the domain and \(D\) fixed, approximately how does the fastest diffusion rate change?
  4. Why is a zero-flux boundary condition not generally implemented by setting the boundary concentration to zero?
  5. What unit should \(D/h^2\) have, and why?

Solutions. (1) \(du_i/dt=D(u_{i-1}-2u_i+u_{i+1})/h^2-ku_i\), with boundary-adjacent values treated according to the specified boundary condition. (2) \(12\times3=36\) states. (3) It increases by approximately a factor of four because the diffusion rate scales as \(D/h^2\). (4) Zero flux specifies a zero normal gradient, not a zero concentration; imposing zero concentration would describe a different boundary condition. (5) Inverse time, since \(D\) has units length squared per time and \(h^2\) has units length squared; multiplying \(D/h^2\) by concentration therefore gives a concentration rate.

Clinical Biostats