Why Check Total Amount?
A PBPK simulation can produce smooth, plausible concentration profiles while still containing an error in the way amounts, doses, or losses are accounted for. A useful independent check is to compare the total amount present in all modeled compartments with the amount that should be present according to the initial condition and cumulative external inputs and losses. This comparison tests a consequence of the amount balances without requiring each compartment to follow a particular concentration profile.
The check is an accounting identity, not a test of whether the model’s physiology is appropriate. Its value is diagnostic: a residual that starts away from zero or grows systematically can point to a bookkeeping problem, while a small residual that shrinks when numerical settings are tightened is consistent with solver error. Neither pattern alone proves the cause, so the residual should be interpreted alongside the equations and simulation output.
Why the Residual Should Be Zero
For a system of amount states, drug can move between compartments through internal flows, enter through external dosing, or leave through external loss processes. An internal transfer decreases the amount in its source compartment and increases the amount in its destination compartment by the same amount per unit time. When the compartment equations are added, these internal terms cancel. The remaining total rate of change is the external input rate minus the external loss rate.
Write the total external input rate as \(u(t)\) and the total external loss rate as \(\ell(t)\), both in amount per unit time. The cumulative quantities are the time integrals of those rates. For continuous inputs and losses, the total balance is:
Integrating the total rate equation from zero to \(t\) gives \(A_{\mathrm{tot}}(t)-A_0=D(t)-L(t)\). Rearranging yields the residual definition above and shows why its exact value is zero. This derivation assumes that the sum includes every modeled amount state, and that every external input and loss is included once in the corresponding cumulative quantity. Internal transfers must not be counted as external inputs or losses.
A bolus dose is a discrete input rather than a continuous rate. Include its amount as a jump in \(D(t)\) at the dosing time, or use an equivalent accounting convention that includes the bolus in the amount balance. The residual should be evaluated consistently on each side of the jump. A nonzero residual immediately after a bolus may indicate that the dose was added to the amount states but not to cumulative dose accounting, or vice versa.
Worked Example: Calculating a Residual From Compartment Amounts
Consider hypothetical output at \(t=4\) h from a model with three amount states. At time zero their total amount was \(A_0=100\) mg. By 4 h, cumulative dosing was \(D=20\) mg and cumulative loss was \(L=5\) mg. The simulated amounts are 30.20 mg, 64.50 mg, and 20.27 mg.
First add the amounts in all modeled compartments:
The residual is:
The balance predicts \(100+20-5=115\) mg, so the simulated total is 0.03 mg below that prediction. Relative to 115 mg, the discrepancy is \(0.03/115=0.0002609\), or about 0.0261%. This small residual is evidence of close total-amount accounting at this time, but whether it is acceptable depends on the required accuracy, solver settings, and scale of the quantities. It does not establish that each compartment amount is accurate.
Reading the Residual Over Time
A single endpoint can miss important behavior. Calculate \(R(t)\) at several output times, including time zero and times near dosing events or rapid changes in the solution. A plot of residual against time can help distinguish a constant offset, a step change, and gradual drift. Always check that the amount sum, cumulative dose, and cumulative loss are evaluated at matching times and with consistent units.
For comparing runs or models with different amount scales, a scaled residual can supplement the residual in mg. One possible scale is the largest of the total amount, initial amount, cumulative input, and cumulative loss, with a small positive reference amount \(A_{\mathrm{ref}}\) to avoid division by zero:
This dimensionless quantity aids comparison, but it does not set a universal pass-or-fail threshold. For example, a residual of 0.1 mg may be negligible in a simulation involving grams and consequential in one involving a few tenths of a milligram. Solver tolerances, model purpose, and the desired precision all matter.
Worked Example: A Residual That Grows With Time
Suppose a hypothetical model begins with 50 mg, receives a cumulative 5 mg by \(t=1\) h and 10 mg by \(t=2\) h, and has cumulative losses of 1 mg and 2 mg at those times. Its simulated total amounts are 55 mg at 1 h and 60 mg at 2 h.
At 1 h, the amount implied by the external balance is \(50+5-1=54\) mg. Therefore:
At 2 h, the externally balanced amount is \(50+10-2=58\) mg, giving:
The residual increases by 1 mg between these times rather than remaining near a fixed small discrepancy. If the increase coincides with an internal flow being mistakenly added to the system as an external source, that implementation error is a plausible explanation: the apparent extra amount accumulates over time. Check the amount equations and dose and loss bookkeeping before concluding that the solver is responsible. This pattern is a clue, not a diagnosis by itself; other errors can also produce a growing residual.
Solver Error and Implementation Error
Numerical solvers approximate the continuous-time solution at finite precision. Their errors can leave a nonzero residual even when the equations and accounting are correct. A practical investigation is to rerun the same model with tighter tolerances, or with a smaller maximum step where relevant, and compare residuals at the same times. If the residual decreases substantially and the amount profiles converge, that supports numerical error as an important contributor. It is not proof that the implementation is correct: a coding error can also produce a convergent, nonzero residual.
Implementation problems often leave patterns that do not disappear with solver refinement. Examples include omitting a compartment from the total, using the wrong sign for an external loss, counting a dose twice, or treating an internal transfer as an external source. A residual already nonzero at time zero suggests checking the initial total and the initial accounting convention. A sudden change at an event time suggests checking event handling and whether the dose or loss was recorded consistently. A smooth increase may reflect an omitted or duplicated rate, although solver error can also accumulate gradually.
A useful numerical strategy is to integrate cumulative input and loss as additional states, rather than reconstructing both solely from sparse saved output. Their rates are the same external input and loss rates used in the amount balance. This avoids relying on coarse post hoc quadrature for those quantities, although the additional states are themselves numerically integrated. If cumulative quantities are instead computed from saved output, refine the output grid or use an appropriate integration method and check that this calculation is not the main source of the residual.
Worked Example: Checking Whether Refinement Reduces the Residual
Consider a hypothetical single-compartment infusion model with initial amount 4 mg, constant input 2 mg/h for 3 h, and no loss. The exact total at 3 h is \(4+(2)(3)=10\) mg. Suppose a coarse solver run reports 9.96 mg, while a tighter run reports 9.9998 mg. The cumulative dose is 6 mg in both runs.
For the coarse run:
For the tighter run:
The absolute residual decreases from 0.04 mg to 0.0002 mg, a factor of \(0.04/0.0002=200\). The corresponding relative discrepancies against 10 mg are 0.4% and 0.002%. This marked reduction is consistent with solver error in the coarse run. It does not establish that other parts of a more complex model are implemented correctly; those still require checks of equations, states, and event accounting.
Diagnostics, Limitations, and Common Pitfalls
Treat the residual as one diagnostic among several. A small total residual can coexist with incorrect individual compartment amounts: an excess in one compartment may cancel a deficit in another. Further, an internal transfer can be assigned incorrectly between two states while leaving the total unchanged. Inspect state-specific trajectories and verify that each transfer appears with equal and opposite signs in its source and destination equations. These checks complement, rather than replace, the total balance.
When investigating a concerning residual, first confirm that all amount states are included and expressed in the same units. Then verify the initial total, the signs and units of cumulative inputs and losses, and the treatment of discrete events. Compare residuals across time and solver settings. Finally, examine whether the residual is near the scale expected from numerical integration and rounding. If only rounded output is available, small discrepancies can arise because the displayed amounts do not preserve the precision of the underlying simulation.
- Pitfall: treating every nonzero residual as a coding error. A finite-step numerical solution and rounded output can both produce nonzero values. Compare runs with tighter numerical settings before attributing a small discrepancy to implementation.
- Pitfall: treating a small residual as proof the model is correct. Total amounts can balance while drug is assigned to the wrong compartments or an internal flow is misrepresented. Check the component equations and state profiles as well.
- Pitfall: counting internal transfers as system input or loss. Transfers between modeled compartments cancel in the total balance. Only inputs from outside the modeled system enter \(D(t)\), and only losses outside it enter \(L(t)\).
- Pitfall: comparing quantities at inconsistent times. Evaluate amounts, cumulative inputs, and cumulative losses at the same time, with bolus jumps handled consistently.
Compared with checking each compartment equation separately, the total residual is a compact system-wide check and is often easy to calculate from simulation output. Its limitation is that aggregation hides redistribution errors. Solver-refinement comparisons help assess numerical sensitivity, but they are not a substitute for checking the model’s bookkeeping. Using these approaches together gives a more informative diagnosis than any one check alone.
Check Your Understanding
Answer each question, then compare your reasoning with the solutions.
- A model has \(A_0=80\) mg, cumulative dose 12 mg, cumulative loss 7 mg, and total simulated amount 84.8 mg. Calculate the residual.
- Why does an internal transfer between two included compartments not appear in the system-wide dose or loss totals?
- A residual is zero at time zero, jumps immediately after a bolus, and stays approximately constant afterward. Name one accounting issue to check.
- Why can a residual close to zero fail to reveal an incorrect distribution of drug among compartments?
- A residual becomes much smaller when tolerances are tightened. What does this support, and what does it not prove?
Solutions. 1. \(R=84.8-80-12+7=-0.2\) mg, so the simulated amount is 0.2 mg below the amount implied by the external balance. 2. The transfer removes amount from one included state and adds the same amount to another; the changes cancel when all states are summed. 3. Check whether the bolus was recorded consistently in both the amount state and cumulative dose accounting, including the timing convention used for the residual. 4. Errors in different compartments can offset in the sum, leaving the total unchanged even though the individual amounts are wrong. 5. The reduction supports solver error as a contributor to the coarse-run residual. It does not prove the implementation is correct, because other implementation errors may persist or be hidden by total aggregation.