Tutorials › Quantitative Systems Pharmacology (QSP) › Diagnosing Numerical Artifacts at Spatial Boundaries

Spatial and Agent-Based Approaches · Tutorial 227 of 799

Diagnosing Numerical Artifacts at Spatial Boundaries

Learn to diagnose spurious boundary gradients and mass changes by checking boundary fluxes, discrete conservation, and whether local features persist under mesh refinement.

Advanced 11 min read

What You'll Learn

  • Relate continuous boundary conditions to finite-volume boundary fluxes.
  • Check whether simulated mass changes match boundary fluxes and reaction terms.
  • Identify how an incorrect no-flux implementation can create a false gradient.
  • Use local mesh refinement to distinguish an unresolved boundary layer from a physical one.
  • Compare boundary-cell values and fluxes across numerical formulations.
  • Separate expected mass loss through a permeable boundary from numerical conservation error.

Why Spatial Boundaries Need Special Checks

A spatial simulation can look smooth and plausible while representing the wrong boundary condition. A boundary stencil may inadvertently hold concentration at zero, omit a boundary flux, or apply a flux over the wrong distance. The resulting error is often largest near the edge of the domain, where it can appear as a real biological gradient or a thin boundary layer. A coarse mesh can also fail to resolve a real layer, changing its apparent thickness and the flux leaving the tissue.

The key diagnostic is to ask what the model says should cross each boundary, then compare that with what the numerical solution does. For a concentration \(c(x,t)\) diffusing in one dimension, define the diffusive flux in the positive \(x\)-direction as \(J=-D\,\partial c/\partial x\), where \(D\) is the diffusion coefficient. A no-flux boundary requires zero outward flux. A permeable or fixed-concentration boundary may instead allow mass to leave or enter. Those cases should not be expected to conserve mass inside the domain.

Definition: A numerical boundary artifact is a feature of the computed solution—such as an edge gradient, mass change, or thin layer—that is caused by discretization or boundary-condition implementation rather than by the specified physical model. A conservation residual is the difference between the simulated mass change and the mass change predicted by boundary fluxes and reaction terms.

For a one-dimensional tissue of length \(L\) and constant cross-sectional area \(A\), the total amount is \(M(t)=A\int_0^L c(x,t)\,dx\). If the model includes production \(q\) and first-order degradation \(k c\), its continuous mass balance is

$$ \frac{dM}{dt} = A\left[J(0,t)-J(L,t)\right] + A\int_0^L \left(q-kc\right)\,dx. $$

The signs follow from defining \(J\) in the positive \(x\)-direction: outward flux is \(-J(0,t)\) at the left boundary and \(J(L,t)\) at the right. Thus positive outward flux reduces the amount inside. With no reactions and no-flux boundaries, \(dM/dt=0\). This balance is a direct diagnostic, not merely a theoretical property: the numerical change in mass should agree with the fluxes and reactions the code actually applies.

Discrete Fluxes and Conservation Checks

Consider a finite-volume discretization with \(N\) equal cells of width \(h\), cell concentrations \(c_i\), and unit cross-sectional area. The discrete mass is \(M_h=h\sum_{i=1}^N c_i\). At an internal face between cells \(i\) and \(i+1\), approximate the flux by

$$ J_{i+1/2}=-D\frac{c_{i+1}-c_i}{h}. $$

The rate of change in a cell is the flux entering its left face minus the flux leaving its right face, divided by \(h\), plus any local reaction rate. When these equations are summed across cells, every internal face flux appears once with each sign and cancels. Only boundary fluxes and reactions remain. This cancellation is why a conservative finite-volume scheme is useful for diagnosing boundary errors: an unexplained change in total mass points to a boundary term, a reaction term, or a numerical implementation problem.

For a no-flux boundary, set the outward boundary flux to zero. Do not replace this condition with a zero concentration: \(c=0\) is a Dirichlet condition, which generally permits material to diffuse out. For a Robin boundary, which relates flux to the difference between tissue concentration and an external concentration, nonzero loss or gain may be physically intended. The appropriate check is then not “mass must remain constant,” but “mass change must match the specified boundary flux.”

Worked Example: A No-Flux Tissue That Loses Mass

A hypothetical diffusion-only model has four cell-centered concentrations \(c=[1,1,1,1]\) in amount per unit volume, cell width \(h=0.25\) mm, and \(D=1\) mm\(^2\)/min. Both boundaries are specified to be no-flux. The initial concentration is uniform, so the physical solution should remain uniform and the total amount per unit area should remain \(M_h=0.25(1+1+1+1)=1\) in amount per unit area.

Suppose the left and right boundary cells are mistakenly coupled to an exterior concentration of zero, using a half-cell distance \(h/2\). This is an absorbing, fixed-concentration boundary, not a no-flux boundary. The outward flux at either edge is initially \(D(1-0)/(h/2)=2D/h=8\) amount per unit area per minute. The total outward flux is therefore 16, so the initial mass rate should be \(-16\) under this mistaken implementation.

The corresponding finite-volume boundary-cell rate is \(-2D c_1/h^2\) at the left edge, and likewise at the right. Here \(-2(1)(1)/(0.25)^2=-32\) concentration units per minute for each boundary cell. With an explicit time step \(\Delta t=0.01\) min, each boundary cell changes by \(-32(0.01)=-0.32\); the two interior cells do not change on this first step because all internal concentration differences were initially zero. The updated values are \([0.68,1,1,0.68]\), giving \(M_h=0.25(3.36)=0.84\). The mass decrease of \(0.16\) equals the initial total outward flux \(16\) multiplied by \(0.01\) min.

The arithmetic is internally consistent, but the simulation violates the stated no-flux condition. The strong edge gradient and mass loss are artifacts of imposing the wrong boundary condition. Refining this mesh would not repair the modeling error: the boundary implementation must first be corrected.

Localizing a Conservation Failure

A global mass check can reveal a problem without revealing its location. Compare each cell’s concentration change with its net incoming flux and reaction rate, then inspect the two boundary faces separately. In a correct diffusion-only no-flux calculation, internal fluxes redistribute mass but cancel in the sum, and both boundary fluxes are zero. If the global balance fails, check whether a boundary term was omitted, assigned the wrong sign, evaluated at the wrong distance, or included twice.

Worked Example: Verifying a Conservative Update

Take three cell-centered values \([1.0,0.8,0.5]\) amount per unit volume, \(h=0.1\) mm, \(D=0.2\) mm\(^2\)/min, and no-flux boundaries. There are no reactions. The finite-volume diffusion rates are \(D(c_2-c_1)/h^2\) for the left cell, \(D(c_1-2c_2+c_3)/h^2\) for the middle cell, and \(D(c_2-c_3)/h^2\) for the right cell.

Substitution gives the rates \((0.2)(0.8-1.0)/0.01=-4\), \((0.2)(1.0-1.6+0.5)/0.01=-2\), and \((0.2)(0.8-0.5)/0.01=6\) amount per unit volume per minute. Their sum is \(-4-2+6=0\), so the discrete total mass rate \(h\sum_i dc_i/dt\) is zero, as required by the no-flux balance.

With an explicit step of \(0.001\) min, the updated values are \([0.996,0.798,0.506]\). The initial mass is \(0.1(1.0+0.8+0.5)=0.23\), and the updated mass is \(0.1(0.996+0.798+0.506)=0.23\). The first cell loses \(0.0004\) amount per unit area and the last gains \(0.0006\), while the middle loses \(0.0002\); together, these changes sum to zero. If a computed update using these same fluxes changes total mass, the error is in the update or boundary bookkeeping, not in the specified diffusion process.

Distinguishing a Real Boundary Layer from an Unresolved One

A boundary layer is a rapid spatial change over a short distance. It may be physically real—for example, diffusion balanced by rapid local degradation—or it may be produced by a faulty boundary condition. Even with correct conditions, a coarse mesh can distort a real layer: it may make the layer appear too thick or too thin, misestimate the boundary gradient, or leave too few cells to describe its shape. Refinement is therefore most informative after the boundary condition and mass balance have been checked.

For a simple benchmark, consider steady diffusion and first-order degradation on a semi-infinite domain, with \(D\,d^2c/dx^2-kc=0\), \(c(0)=1\), and concentration tending to zero far from the boundary. Let \(\ell=\sqrt{D/k}\) be the characteristic decay length. The continuous profile is \(c(x)=\exp(-x/\ell)\). A centered spatial difference on a uniform mesh gives a discrete decaying solution \(c_i=r^i\), where \(q=kh^2/D\) and the decaying root is

$$ r=\frac{2+q-\sqrt{q^2+4q}}{2}. $$

This relation gives a simple quantitative check on how the mesh represents the layer. For small \(h/\ell\), the discrete ratio \(r\) approaches the continuous ratio \(\exp(-h/\ell)\). If they differ substantially, the numerical profile is not resolving the continuous decay accurately.

Worked Example: Detecting a Coarse-Mesh Boundary Layer

Use hypothetical parameters \(D=1\) mm\(^2\)/min and \(k=100\) min\(^{-1}\). The continuous decay length is \(\ell=\sqrt{1/100}=0.1\) mm. Compare a coarse mesh with \(h=0.2\) mm to a refined mesh with \(h=0.05\) mm. For the coarse mesh, \(q=kh^2/D=100(0.2)^2=4\), so \(r=(6-\sqrt{32})/2=0.1716\). The continuous ratio over the same spacing is \(\exp(-0.2/0.1)=\exp(-2)=0.1353\).

For the refined mesh, \(q=100(0.05)^2=0.25\), giving \(r=(2.25-\sqrt{1.0625})/2=0.6096\). The continuous ratio is \(\exp(-0.05/0.1)=\exp(-0.5)=0.6065\). The refined discrete ratio is close to the continuous value, whereas the coarse-grid ratio is appreciably larger. The coarse solution therefore decays too slowly from the boundary and represents a layer that is too broad. This is a resolution error, not evidence that the underlying model has a longer physical decay length.

A useful refinement test compares more than a whole-domain average. Track the first few cell concentrations, the estimated boundary gradient or flux, and the width of the layer as \(h\) decreases. A feature that changes substantially with local refinement is not yet numerically reliable. A feature that stabilizes across refinements and agrees with an independent flux or analytic check is more credible, though numerical agreement alone does not establish that the biological model is correct.

Diagnostics, Alternatives, and Limitations

Use checks that answer different questions. A mass balance tests whether the implemented fluxes and reactions account for the simulated amount change. A local boundary inspection tests whether the imposed condition is actually being applied. Mesh refinement tests whether spatial resolution is sufficient. Comparing a finite-volume flux implementation with a separately coded finite-difference boundary stencil can help expose a shared sign or indexing error, provided the two methods represent the same boundary condition.

These approaches are complementary, not interchangeable. Refinement cannot correct a wrong boundary condition; conservation cannot reveal every error if the incorrect implementation is itself conservative; and visual smoothness is not a validation test. A deliberately imposed boundary source or sink can conserve neither interior mass nor a no-flux balance, but its change should still match the prescribed flux. Likewise, a correct no-flux diffusion scheme can conserve mass while producing a wrong profile if its interior discretization is inaccurate.

A practical diagnostic sequence is:

1
Write down the intended boundary condition.
State whether each edge has zero flux, fixed concentration, or a flux related to the external environment. Specify the outward-flux sign convention.
2
Calculate the expected amount balance.
Combine boundary fluxes with integrated production and degradation. For diffusion-only no-flux conditions, the expected total amount change is zero.
3
Inspect the boundary-cell equations.
Check the flux at each edge, its sign, the distance used in the gradient, and whether the boundary cell has the correct volume.
4
Refine where the feature occurs.
Compare boundary concentrations and fluxes on progressively finer local meshes. Do this after correcting any mismatch between the intended and implemented condition.

One common mistake is to see a stable edge gradient after refinement and conclude that it must be physical. The correction is to verify the boundary condition and mass balance first: a consistently imposed but incorrect absorbing boundary can produce a stable, reproducible, and entirely unintended gradient. Another pitfall is to demand constant mass in a model with a permeable boundary. Instead, calculate the expected loss or gain from the boundary flux and compare it with the simulated amount change.

These checks have limits. A global balance may hide offsetting local errors; a boundary-value check may miss an inaccurate interior profile; and a refinement study can converge to the solution of the wrong equations. For strong gradients, irregular geometry, or spatially varying transport, the flux approximation and the geometric representation also need scrutiny. The goal is not to choose one diagnostic, but to make boundary conditions, fluxes, mass changes, and local mesh behavior mutually consistent.

Check Your Understanding

Answer each question, then compare with the brief solutions.

  1. A diffusion-only model uses no-flux boundaries. Its total simulated amount decreases, and the boundary cells have the lowest concentrations. What should be checked first?
  2. A model has a fixed zero concentration at one edge. Is constant total amount the appropriate diagnostic expectation?
  3. For a no-flux finite-volume diffusion model, internal face fluxes are included in the two cells sharing each face. Why should they not change the summed mass rate?
  4. A boundary layer changes width substantially when the boundary mesh is refined, but the total amount balance is accurate. What does this indicate?

Solutions. (1) Inspect the implemented edge flux and boundary-cell equation, including signs, distances, and whether zero concentration has accidentally replaced zero flux. Then compare the mass change with the boundary-flux balance. (2) No. A fixed zero concentration can allow outward diffusion; simulated mass change should be compared with the resulting boundary flux and any reaction terms. (3) The flux leaving one cell enters its neighbor with equal magnitude and opposite sign, so those internal contributions cancel in the sum. Only boundary fluxes and reactions change total mass. (4) The layer is not yet spatially resolved, even though the global balance is consistent. Further local refinement is needed before its width or boundary gradient can be interpreted reliably.

Clinical Biostats