Tutorials › Quantitative Systems Pharmacology (QSP) › Choosing ODE Solvers for Mechanistic Models

Writing and Debugging a Model · Tutorial 75 of 799

Choosing ODE Solvers for Mechanistic Models

Use the model’s time scales, stability limits, and computational costs to choose an explicit or implicit solver for mechanistic ODE systems.

Intermediate 12 min read

What You'll Learn

  • Distinguish stiffness from merely rapid change in a model state.
  • Derive the explicit Euler stability limit for a decaying mode.
  • Compare explicit and implicit solver behavior on turnover and fast-slow systems.
  • Recognize why numerical stability does not guarantee accuracy.
  • Weigh step counts against the overhead of implicit solves.

Why Solver Choice Matters

A mechanistic model can be biologically well specified and still produce a poor simulation if its numerical solver is unsuitable for its time scales. An explicit solver advances the states using information evaluated at the current time. An implicit solver uses a relation that includes the unknown future state, requiring an equation solve at each step. The first approach is often efficient for smooth, nonstiff models; the second can be more efficient when fast dynamics impose restrictive stability limits on explicit steps.

The distinction is not simply “slow model” versus “fast model.” Stiffness arises when a system contains substantially different dynamic time scales and an explicit solver must take very small steps for numerical stability, even when the quantities of interest change slowly. A fast state alone does not prove that a model is stiff: the fast dynamics must also constrain the numerical step relative to the accuracy needed for the simulation.

Definition: A system is called stiff when stability requirements force an explicit numerical method to take steps much smaller than the steps needed to represent the solution’s features of interest. Stiffness depends on the equations, the time interval, the numerical method, and the requested accuracy; it is not a fixed label based only on a parameter value.

For this tutorial, assume the model is an ordinary differential equation \(d\mathbf{x}/dt=f(t,\mathbf{x})\), its right-hand side is sufficiently smooth over the interval being integrated, and the initial state is specified. These conditions support a well-defined numerical integration problem. The examples use continuous dynamics over each interval; a solver must not step blindly across a discontinuity in an input. Solver accuracy also depends on the error controls chosen, a topic considered separately from the solver-family choice here.

Stability Gives a First Selection Clue

A simple derivation shows why explicit steps can be restricted. Consider the scalar decay equation \(dy/dt=\lambda y\), where \(\lambda<0\). Its exact solution is \(y(t)=y(0)e^{\lambda t}\), which decays toward zero. The explicit Euler update with step size \(h\) is \(y_{n+1}=y_n+h\lambda y_n\). Therefore, each step multiplies the current value by an amplification factor:

$$ y_{n+1}=(1+h\lambda)y_n $$

For numerical decay rather than growth, the magnitude of this factor must be less than one. Thus \(|1+h\lambda|<1\), which for a negative real \(\lambda\) gives \(0<h<2/|\lambda|\). This is an absolute-stability condition for explicit Euler on this test equation. It does not guarantee that the numerical solution is accurate; accuracy may require a smaller step.

For a system of linear equations, the corresponding stability assessment concerns the eigenvalues of the system matrix. In a nonlinear model, the Jacobian \(J=\partial f/\partial\mathbf{x}\) describes local linearized dynamics, so its eigenvalues can help identify fast decaying modes. This is a diagnostic clue, not a complete solver decision rule: nonlinear behavior can change across the trajectory, and the quantities of interest and computational overhead also matter.

Key distinction: Stability means numerical errors do not grow uncontrollably under the method’s stability conditions. Accuracy means the computed trajectory is sufficiently close to the solution for the intended use. A stable step can still be too large to represent important changes.

Worked Example: A Smooth Turnover System

Consider a hypothetical turnover state \(R\), measured in mg/L, governed by \(dR/dt=k_{\mathrm{in}}-k_{\mathrm{out}}R\), with \(k_{\mathrm{in}}=12\) mg/L/h, \(k_{\mathrm{out}}=0.5\) h\(^{-1}\), and \(R(0)=0\) mg/L. Use explicit Euler with \(h=1\) h to approximate \(R\) at 2 h, then compare with the exact solution.

The model is smooth and has one decay rate, so there is no separation of time scales in this example. Its equilibrium is \(R_{\mathrm{eq}}=k_{\mathrm{in}}/k_{\mathrm{out}}=24\) mg/L. The exact solution from the stated initial condition is \(R(t)=24(1-e^{-0.5t})\) mg/L.

The Euler update is \(R_{n+1}=R_n+h(12-0.5R_n)\). Starting from \(R_0=0\), the first step gives \(R_1=0+1(12-0.5(0))=12\) mg/L. The second gives \(R_2=12+1(12-0.5(12))=18\) mg/L.

At 2 h, the exact value is \(24(1-e^{-1})\approx15.1709\) mg/L. The Euler estimate is high by \(18-24(1-e^{-1})\approx2.8291\) mg/L. The step is stable for this decay mode because \(1<2/0.5=4\) h, but its error is still substantial. A smaller step or an adaptive explicit method would generally improve the approximation for this smooth, nonstiff system without requiring an implicit solve.

Fast and Slow Modes in One Pathway

A coupled pathway can contain a slowly changing state and a rapidly relaxing state. Consider the hypothetical system below, where time is in hours and both states are dimensionless:

$$ \frac{dx}{dt}=-0.1x,\qquad \frac{dy}{dt}=100(x-y),\qquad x(0)=1,\quad y(0)=0 $$

Here \(x\) decays on a time scale of about \(1/0.1=10\) h. The state \(y\) rapidly approaches \(x\), with a relaxation time scale of about \(1/100=0.01\) h. The system matrix is \(\begin{pmatrix}-0.1&0\\100&-100\end{pmatrix}\), whose eigenvalues are \(-0.1\) and \(-100\). The ratio of the magnitudes is 1000, indicating strongly separated decay rates.

Worked Example: Explicit Euler on a Fast-Slow System

Apply explicit Euler to the system above with \(h=0.05\) h. The slow state after one step is \(x_1=1+0.05(-0.1)(1)=0.995\). For \(y\), the update is \(y_{n+1}=y_n+100h(x_n-y_n)\), so \(y_1=0+5(1-0)=5\). This is already far above the initial \(x\) value, despite the rapid dynamics representing relaxation toward \(x\).

A second step gives \(x_2=0.995+0.05(-0.1)(0.995)=0.990025\), while \(y_2=5+5(0.995-5)=-15.025\). The negative, rapidly growing oscillation is a numerical artifact for a state that should relax toward the positive, slowly varying \(x\).

The fast eigenvalue imposes the explicit Euler stability limit \(h<2/100=0.02\) h. The chosen step, 0.05 h, violates that condition. Its fast-mode amplification factor is \(1+0.05(-100)=-4\), whose magnitude exceeds one, explaining the alternating growth. Reducing the step below 0.02 h addresses this stability failure, although additional reduction may be needed for accuracy.

An implicit method can avoid this particular stability restriction for decaying modes. For example, backward Euler uses the future state in the update. Applied to a linear system \(d\mathbf{x}/dt=A\mathbf{x}\), it gives \(\mathbf{x}_{n+1}=\mathbf{x}_n+hA\mathbf{x}_{n+1}\), or \((I-hA)\mathbf{x}_{n+1}=\mathbf{x}_n\). For nonlinear equations, each step generally requires solving a nonlinear system, often with Newton-type iterations.

Worked Example: Backward Euler and Stability Versus Accuracy

Use one backward Euler step of \(h=0.05\) h on the same fast-slow system, starting at \(x_0=1\), \(y_0=0\). First solve the \(x\) update: \(x_1=x_0/(1+0.1h)=1/1.005\approx0.995025\). The \(y\) update is \(y_1=y_0+100h(x_1-y_1)\), so \(6y_1=5x_1\), giving \(y_1\approx0.829187\).

For comparison, the exact solution for \(y\) is \(y(t)=\frac{100}{99.9}(e^{-0.1t}-e^{-100t})\). At 0.05 h, this is approximately \(\frac{100}{99.9}(e^{-0.005}-e^{-5})=0.989264\). Thus the backward Euler value is about \(0.1601\) below the exact value. It remains bounded and positive, unlike the explicit result, but one large step does not accurately represent the rapid initial transition.

This example demonstrates the central trade-off: implicit stability can permit larger steps, but it does not make those steps accurate for every feature of a trajectory. If the early fast transient matters, the solver must still resolve it adequately.

Comparing Solver Families and Computational Cost

Model situationCommon choiceReason and caution
Smooth, nonstiff dynamicsExplicit Runge–Kutta methodOften efficient because steps are not forced to be very small for stability; check accuracy and stability over the full trajectory.
Strongly separated, stable time scalesImplicit method such as BDF or an implicit Runge–Kutta methodCan take larger stable steps on decaying fast modes; each step has additional equation-solving cost.
Unknown or changing stiffnessSolver with automatic stiff/nonstiff switching, when availableCan adapt the method to local behavior, but switching does not remove the need to inspect the simulated trajectory and solver diagnostics.

Explicit methods usually evaluate the right-hand side without solving a system of equations at each step. Implicit methods incur extra work for nonlinear iterations and, for large systems, linear algebra. Their advantage is that they may need far fewer steps when explicit stability limits are severe. For a small nonstiff model, implicit overhead may outweigh that benefit. For a large stiff model, Jacobian structure and the cost of linear solves can substantially affect the comparison.

Worked Example: Comparing Hypothetical Solver Costs

Suppose a hypothetical benchmark for a fast-slow model, using the same stated accuracy target, records 200 accepted explicit steps and 30 accepted implicit steps over an interval. For an illustrative cost calculation, assign one cost unit to each explicit step and six cost units to each implicit step, including its equation-solving overhead.

The estimated explicit cost is \(200\times1=200\) units. The estimated implicit cost is \(30\times6=180\) units. Under these assumptions, the implicit run is estimated to use 20 fewer cost units, or 10% less than the explicit estimate because \((200-180)/200=0.10\).

These assigned costs and step counts are hypothetical, not universal solver performance figures. The arithmetic illustrates why step count alone is insufficient: fewer implicit steps do not necessarily mean less computation. In practice, the comparison is meaningful only for the same model outputs and an acceptable accuracy target, and the outcome depends on model size, Jacobian calculations, implementation, and hardware.

Diagnostics, Failure Modes, and Practical Limits

Useful clues that an explicit solver is struggling with stiffness include repeated extremely small steps, excessive computational effort despite a slowly changing trajectory, or oscillatory and rapidly growing values in a model whose dynamics should decay. A warning or failed integration can also signal a problem, but it is not by itself proof of stiffness: incorrect equations, invalid state values, or other numerical difficulties can produce similar symptoms.

An implicit solver is not a universal remedy. It can be costly for nonstiff models, and its nonlinear solve may fail or require substantial effort. Neither solver family automatically preserves every biological constraint, such as nonnegativity or a conservation relationship. Inspect simulated states for plausible behavior and use model-specific checks rather than treating successful completion as proof of correctness.

A common mistake is to select a solver solely because one state changes rapidly, or to assume that a stable implicit result must be accurate. The correction is to consider the full system’s time scales and stability limits, identify which trajectory features matter, and assess both numerical behavior and computational cost. A rapid transient that is irrelevant to an endpoint may permit a different practical step pattern than a transient needed for interpretation or observation.

Practical takeaway: Start with an explicit solver for a smooth model without strong time-scale separation. Consider an implicit solver when explicit stability restrictions make the calculation inefficient. In either case, stability is not an accuracy guarantee, and solver choice cannot replace checking whether the simulated behavior is scientifically plausible.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions below.

  1. For explicit Euler applied to \(dy/dt=-4y\), what condition on \(h\) ensures absolute stability?
  2. In a fast-slow system with eigenvalues \(-0.2\) and \(-50\), which mode is more likely to constrain an explicit step for stability?
  3. Why can a backward Euler solution be stable but inaccurate for a rapidly relaxing state?
  4. A model has smooth dynamics and similar time scales across states. Which solver family is a reasonable initial choice, and why?
  5. Why is a comparison of accepted step counts alone inadequate for deciding which solver is faster?

Solutions. (1) The condition is \(0<h<2/4=0.5\), in the time units used by the model. (2) The mode with eigenvalue \(-50\) is faster and imposes the more restrictive explicit stability limit: \(h<2/50=0.04\). (3) Stability prevents uncontrolled growth of numerical errors, but a large step can still fail to resolve the state’s rapid change, as the backward Euler calculation demonstrated. (4) An explicit Runge–Kutta method is a reasonable initial choice because there is no strong time-scale separation forcing unusually small stable steps; accuracy and behavior still need to be checked. (5) Implicit steps can require nonlinear iterations and linear solves, so each may cost more than an explicit step; compare computational work for an acceptable accuracy target.

Clinical Biostats