Tutorials › Physiologically-Based Pharmacokinetics (PBPK) › Diagnosing Mass-Balance Errors in Implemented Models

Units, Dimensional Consistency and Debugging · Tutorial 93 of 799

Diagnosing Mass-Balance Errors in Implemented Models

Use residual magnitude, sign, and time pattern—together with compartment-level checks—to locate mass-balance errors without over-interpreting what the residual can identify.

Intermediate 12 min read

What You'll Learn

  • Define instantaneous and cumulative whole-body mass-balance residuals.
  • Derive why correctly implemented internal transfers cancel when organ balances are summed.
  • Recognize the residual signatures of one-sided omitted and duplicated tissue fluxes.
  • Calculate how a reversed transfer sign and unrecorded elimination affect the residual.
  • Use residual time patterns alongside compartment equations to narrow down likely faults.
  • Distinguish implementation checks from numerical solver error and limits on diagnosis.

Why the Residual Is a Diagnostic Signal

A whole-body mass-balance residual is more than a pass-or-fail check. Its sign, magnitude, and time course can help locate a defect in a multi-organ PBPK implementation. A residual that follows a tissue-exchange flux suggests a different class of problem from one that tracks a clearance rate or grows as an unexplained offset.

The residual is evidence, not a unique identifier. Different coding errors can produce the same net imbalance, and some important errors preserve whole-body mass balance entirely. A reliable diagnosis combines the residual with the individual organ equations and the model’s flux definitions.

Definition: Let \(A_i(t)\) be the amount of drug in organ or compartment \(i\), and let \(M(t)=\sum_i A_i(t)\) be the total amount represented by the model. Let \(u(t)\) be the external input rate and \(e(t)\) the rate of all external elimination processes that are included in the intended balance. The instantaneous whole-body residual is $$ r(t)=\sum_i \frac{dA_i(t)}{dt}-u(t)+e(t). $$ A correctly implemented balance has \(r(t)=0\), apart from numerical approximation. Over an interval beginning at \(t_0\), the cumulative residual is $$ R(t)=M(t)-M(t_0)-\int_{t_0}^{t}u(s)\,ds+\int_{t_0}^{t}e(s)\,ds. $$ It too should be zero apart from numerical approximation when the starting amount, inputs, eliminations, and state changes are consistently represented.

Amounts might be in mg, rates in mg/h, and time in h. The instantaneous residual then has units mg/h, while the cumulative residual has units mg. Keep these two quantities distinct when plotting or reporting a diagnostic. A persistent small rate error can accumulate into a substantial cumulative discrepancy.

Why Internal Fluxes Cancel

Consider an internal transfer \(J_{ij}(t)\) from compartment \(i\) to compartment \(j\), defined as positive in that direction. In a correct pair of equations, it contributes \(-J_{ij}\) to \(dA_i/dt\) and \(+J_{ij}\) to \(dA_j/dt\). When the equations are summed, the contributions cancel:

$$ -J_{ij}(t)+J_{ij}(t)=0. $$

The same cancellation applies to every transfer that remains inside the modelled system. The summed derivative therefore contains external input and elimination, but not internal redistribution. This gives a short derivation of the residual check: if the organ equations represent all internal transfers with equal and opposite contributions, then \(\sum_i dA_i/dt=u-e\), so \(r(t)=(u-e)-u+e=0\).

The cancellation rule also explains an important limitation. If an internal transfer is omitted from both organ equations, the model’s total mass can still balance: neither equation contains that transfer, so the pair still sums to zero for it. The predicted organ amounts may be wrong, but the whole-body residual will not reveal the missing exchange. The residual diagnoses failures of net accounting; it does not prove that every intended physiological process is present.

Reading a Time-Varying Residual

When \(r(t)\) is not near zero, inspect its temporal pattern and compare it with candidate fluxes. For a proposed transfer \(J(t)\), plot or tabulate \(r(t)\) alongside \(J(t)\). A residual close to \(+J(t)\), \(-J(t)\), or \(-2J(t)\) suggests a particular imbalance in the paired equations, but the sign depends on which compartment is wrong and on the convention used for the flux direction.

1
Check the residual calculation.
Confirm that every modelled amount is included in \(M\), that input and elimination use the intended signs, and that rates and amounts use consistent units.
2
Locate the time pattern.
Identify when the residual starts, whether it changes sign, and whether it is approximately constant, proportional to a candidate flux, or related to total amount.
3
Audit the corresponding equations.
For a suspected internal transfer, verify that the source loses exactly the amount the destination gains. For a suspected external loss, check whether it appears in both the state equations and the balance ledger.
4
Check numerical stability.
Compare solver tolerances or step sizes and, where possible, evaluate the residual from the model’s right-hand-side rates as well as from simulated amounts.

A derivative-based residual uses the rates computed by the model at a given time. A cumulative residual computed from simulated amounts also depends on numerical integration and on how input and elimination integrals are evaluated. A discrepancy that shrinks substantially with tighter tolerances or smaller steps may be numerical; a stable, structured discrepancy warrants a model or implementation audit. There is no universal residual threshold: interpret its size relative to the model’s amount and rate scales, units, numerical accuracy, and intended use.

Worked Example: A Tissue Outflow Is Missing From One Equation

In a hypothetical two-organ subsystem, a tissue exchange flux \(J_{AB}\) is positive from organ A to organ B. At one time point, \(J_{AB}=3.20\) mg/h. The implemented equation for A includes \(-J_{AB}\), but the equation for B omits \(+J_{AB}\). Assume all other terms in the summed equations correctly match external input and elimination. What residual does this defect produce?

With the defect, the pair contributes \(-3.20\) mg/h to the sum of the organ rates, rather than zero. Thus $$ r(t)=\bigl(-J_{AB}+0\bigr)-\bigl(-J_{AB}+J_{AB}\bigr)=-J_{AB}. $$ Substituting the hypothetical rate gives \(r(t)=-3.20\) mg/h.

The negative residual means the represented total amount is decreasing \(3.20\) mg/h faster than can be accounted for by the recorded input and elimination. If the flux later falls to zero, this component of the residual also disappears; if the flux reverses direction under the model’s definition, the residual changes sign with it. A close match between \(r(t)\) and \(-J_{AB}(t)\) would support checking the paired A-to-B terms, but would not by itself prove which line of code is wrong.

If both \(+J_{AB}\) and \(-J_{AB}\) were absent, their net contribution to \(r(t)\) would still be zero. The tissue profiles could then be incorrect without a whole-body residual signal.

Distinguishing Duplicate Transfers and Sign Errors

A duplicated transfer and a sign error can both create residuals that track an internal flux. Their idealized signatures differ when only one member of the pair is affected. For a correct transfer from A to B, the pair sums to \(-J+J=0\). If the destination receives the same flux twice, it sums to \(-J+2J=+J\). If the destination instead has the wrong sign, the pair sums to \(-J-J=-2J\). These patterns are useful clues, not universal fingerprints: other simultaneous errors can change their size or sign.

Worked Example: A Duplicated Tissue Inflow

In a hypothetical model, a transfer from A to B is \(J_{AB}=2.00\) mg/h. The source equation contains the correct \(-J_{AB}\), but the destination equation contains \(+2J_{AB}\), because the inflow was added twice. Other terms balance correctly. Calculate the residual contribution.

The transfer’s net contribution to the summed organ rates is $$ -J_{AB}+2J_{AB}=J_{AB}. $$ Therefore, \(r(t)=+2.00\) mg/h. The positive sign means the modelled total amount is increasing \(2.00\) mg/h more than external input and elimination justify. If the residual follows \(+J_{AB}(t)\) over time, inspect the destination equation for duplicate additions and check whether the same physical transfer has been assigned more than once.

A duplication that is applied equally to the source loss and destination gain can still cancel in the whole-body sum. For example, using \(-2J_{AB}\) in A and \(+2J_{AB}\) in B preserves net mass balance, even though the exchange rate in the implemented model is wrong. The residual is not a general detector of incorrect flux magnitudes.

Worked Example: A Reversed Sign in the Destination Equation

Use a hypothetical transfer \(J_{AB}=3.20\) mg/h, positive from A to B. The source equation correctly contains \(-J_{AB}\), but the destination equation incorrectly contains \(-J_{AB}\) instead of \(+J_{AB}\). What residual contribution follows?

The pair now contributes \(-J_{AB}-J_{AB}=-2J_{AB}\). Substitution gives $$ r(t)=-2(3.20\ \text{mg/h})=-6.40\ \text{mg/h}. $$ The represented total amount falls \(6.40\) mg/h faster than recorded input and elimination explain. A residual near \(-2J_{AB}(t)\) makes a sign reversal in one member of the transfer pair a plausible hypothesis. Inspect the stated flux direction and the source and destination signs in the equations rather than changing the sign solely to force the total residual to zero.

Recognizing Unrecorded Elimination

An unrecorded elimination term differs conceptually from an internal transfer: it is an external loss, so it does not have a matching gain in another modelled compartment. Suppose the state equations remove drug at rate \(L(t)\), but the balance calculation includes only the recorded elimination \(e(t)\). Then the summed state rates contain \(u-e-L\), and the calculated residual is \(-L(t)\). A negative residual that resembles a clearance-related loss should prompt an audit of elimination terms and the balance ledger.

Worked Example: An Elimination Term Is Absent From the Ledger

In a hypothetical simulation, external input is \(u=0.20\) mg/h and recorded elimination is \(e=0.40\) mg/h. The state equations also remove drug through an additional unrecorded process at \(L=0.0350\) mg/h, while the residual calculation does not include \(L\). Find the residual.

The summed state rate is $$ \sum_i \frac{dA_i}{dt}=u-e-L=0.20-0.40-0.0350=-0.235\ \text{mg/h}. $$ Using the stated residual definition, $$ r(t)=-0.235-0.20+0.40=-0.0350\ \text{mg/h}=-L. $$ The negative residual matches the unrecorded loss. If \(L\) varies over time, compare its predicted time course with \(-r(t)\). Also check whether the process is genuinely external: a transfer into a modelled metabolite or other state must be accounted for as an internal transfer, not simply labelled elimination.

Diagnostics, Alternatives, and Limits

A whole-body residual compresses many equations into one number at each time. It is efficient for checking net conservation, but it loses information about where a discrepancy occurred. Compare it with the per-compartment balances: for each compartment, list incoming fluxes, outgoing fluxes, external input or loss, and the resulting derivative. This local ledger is often the most direct way to test a suspected pair of terms.

Other checks answer different questions. Dimensional analysis can reveal a rate with incompatible units but cannot establish that equal-and-opposite signs were coded correctly. Comparing concentration profiles may reveal implausible tissue behaviour, but a plausible-looking profile does not guarantee conservation. Code inspection can locate an error, though it is easier to interpret when guided by a residual pattern. A solver-refinement check helps assess numerical error, but reducing numerical error cannot repair an incorrect balance equation.

Several limitations matter in practice:

  • Residuals do not uniquely identify a fault. A one-sided missing transfer, an unintended loss, or an input-accounting error can all produce a negative residual. Compare candidate terms over time and inspect the equations.
  • Mass-balanced models can still be wrong. A flux omitted from both paired equations, duplicated as an equal source loss and destination gain, or assigned the wrong rate in both compartments may preserve the whole-body balance.
  • Numerical calculations can obscure the pattern. Finite differences of noisy simulated amounts and inconsistent quadrature can create apparent residuals. Use consistent time grids, units, and integration methods, and assess whether the pattern persists under tighter numerical settings.
  • The ledger must match the system boundary. A process is external only if its drug amount leaves the set of modelled states. If a product or compartment remains represented, its gain belongs in the system balance.
Key takeaway: Use \(r(t)\) as a time-resolved conservation check. A residual that tracks a candidate flux can direct attention to a missing, duplicated, or reversed term; a residual resembling an external loss can direct attention to elimination bookkeeping. Confirm the diagnosis in the compartment equations, because residual patterns are clues rather than proofs.

Check Your Understanding

For each question, state the expected residual contribution using the convention \(r=\sum_i dA_i/dt-u+e\).

  1. A transfer \(J\) from A to B is included as \(-J\) in A and omitted in B. What is its residual contribution?
  2. A transfer \(J=1.50\) mg/h is included as \(-J\) in A and \(+2J\) in B. Calculate the residual contribution and interpret its sign.
  3. A model removes drug at an additional rate \(L=0.080\) mg/h, but this loss is absent from the elimination ledger. What residual contribution results?
  4. Why might a model have an incorrect tissue-exchange process but a zero whole-body residual?
  5. A residual closely tracks \(-J(t)\). Does this prove that the destination equation omitted \(+J(t)\)? Explain.

Solutions. (1) The pair contributes \(-J+0\), so \(r=-J\). (2) The pair contributes \(-J+2J=+J\), giving \(r=+1.50\) mg/h; the represented total increases faster than the recorded external balance predicts. (3) The unrecorded external loss contributes \(r=-L=-0.080\) mg/h. (4) If the same internal transfer is omitted from both paired equations, it contributes zero to the sum; similarly, equal source and destination errors can cancel. (5) No. The pattern is consistent with that omission, but another imbalance with the same time course could produce it. Inspect the paired equations and other candidate terms before assigning a cause.