
Partial differential equations
2026-12-03
Source:vignettes/articles/rxode2-partial-differential-equations.Rmd
rxode2-partial-differential-equations.RmdPartial differential equations by the method of lines
Some pharmacometric processes change over a spatial or structural dimension as well as over time: diffusion through skin or tissue, age-structured cell populations, or drug release from particles of different sizes. These are partial differential equations (PDEs).
rxode2 solves one-dimensional PDEs by the method of
lines: the dimension is split into n cells, and
the PDE becomes one ODE per cell. You write the PDE once on a grid and
rxode2 writes the ODEs, so solving, sensitivities and nlmixr2 estimation
see an ordinary ODE model.
Declaring a grid and a gridded state
grid(x, from = 0, to = 1, n = 50) splits
[0, 1] into 50 cells of width h = 1/50 with
centers x = h/2, 3h/2, .... d/dt(C[x]) then
declares a state on that grid, which becomes the states
C_1, ..., C_50. Inside the equation:
-
C[x](orC) is the value in the current cell andxis its center; -
d2x(C)is the second derivative (diffusion); -
dx(C)is the first derivative (advection), upwind for flow towards increasingx;dx(C, "forward")is upwind for flow the other way anddx(C, "central")is a central difference.
C[x](0) = expr sets the initial value of every cell, and
expr may use x.
Diffusion with fixed boundary values
Diffusion on [0, 1] with both ends held at zero and a
sine-shaped start has the exact solution
exp(-D*pi^2*t)*sin(pi*x):
diffusion <- function() {
ini({
tD <- log(0.1)
})
model({
D <- exp(tD)
grid(x, from = 0, to = 1, n = 40)
d/dt(C[x]) <- D * d2x(C)
C[x](0) <- sin(pi * x)
value(C, x = 0) <- 0
value(C, x = 1) <- 0
mid <- C[x = 0.5]
})
}
s <- rxSolve(diffusion, et(seq(0, 5, by = 0.25)))
exact <- exp(-0.1 * pi^2 * s$time)
max(abs(s$mid - exact))
#> [1] 0.0007709638C[x = 0.5] is the value at a point, interpolated between
cell centers. The error shrinks about four-fold each time n
doubles.
Boundary conditions
Each edge of the grid takes at most one condition:
-
value(C, x = 0) <- gholds the value at the edge atg(Dirichlet); -
flux(C, x = 0) <- qadds an inward fluxqthrough the edge (Neumann), in amount per unit length per time; - an edge without a condition has no flux.
The boundary is named by its position, which must be the
from or to of the grid. Both g
and q may be any expression of the model, including
parameters and time-varying quantities.
Example: absorption through a membrane
A drug crosses a membrane from a donor surface held at a constant concentration into a sink on the far side. The amount in the membrane rises to a steady state:
membrane <- function() {
ini({
tD <- log(0.05)
})
model({
D <- exp(tD)
grid(x, from = 0, to = 1, n = 30)
d/dt(C[x]) <- D * d2x(C)
value(C, x = 0) <- 1
value(C, x = 1) <- 0
inMembrane <- integrate(C, x)
surface <- C[x = 0.5]
})
}
s <- rxSolve(membrane, et(seq(0, 20, by = 0.5)))
plot(s, inMembrane, surface)
integrate(C, x) is the total amount on the grid; its
first argument may also be an expression such as
integrate(x * C[x], x).
Mass balance and dosing
With no-flux edges and no loss, the total amount is conserved. Doses
go to individual cells by their state names. A cell holds a
concentration, so a dose into a cell of width h adds
amt * h to the total; to add an amount of 1 to
the first of 20 cells on [0, 1], dose 20:
Refining the grid
The grid is a single model line, so piping a new grid()
changes the resolution without touching the equations:
The size can also be a fixed ini() value, so
ini() refines the grid:
Gridded intermediate variables
r[x] <- expr defines a variable in every cell
(r_1, ..., r_n), which other gridded equations and
integrate() can use:
reaction <- function() {
ini({
tD <- log(0.05)
tk <- log(0.5)
})
model({
D <- exp(tD)
k <- exp(tk)
grid(x, from = 0, to = 1, n = 20)
rate[x] <- k * C[x] / (1 + C[x])
d/dt(C[x]) <- D * d2x(C) - rate[x]
C[x](0) <- 1
eliminated <- integrate(rate, x)
})
}
s <- rxSolve(reaction, et(seq(0, 10, by = 0.5)))
plot(s, eliminated)
Inspecting the expanded model
rxPde() shows the ODEs that rxode2 writes for model
text:
cat(rxPde("
grid(x, from = 0, to = 1, n = 3)
d/dt(C[x]) = D * d2x(C) - k * C[x]
flux(C, x = 0) = kin
"))
#> d/dt(C_1) = (D * ((C_2 - C_1)/0.111111111111111) - k * C_1) + (kin)/0.333333333333333
#> d/dt(C_2) = D * ((C_1 - 2 * C_2 + C_3)/0.111111111111111) - k * C_2
#> d/dt(C_3) = D * ((-(C_3 - C_2))/0.111111111111111) - k * C_3Sensitivities and estimation
The expanded model is an ordinary ODE system, so its forward
sensitivities are the usual variational equations, one per cell and
parameter, and nlmixr2’s FOCEi uses them for its gradients. A model with
ini() estimates, such as diffusion above, is
passed to nlmixr2() like any other model. Each eta adds
n sensitivity states, so keep n modest during
estimation and refine it afterwards.
Notes
- Only one-dimensional grids are supported; a model may have several grids and several states per grid.
-
nmust be a number or a fixedini()value, set when the model is built (change it withini(), not inrxSolve());fromandtomay be parameters (point valuesC[x = v]need numericfromandto). - The expanded state names (
C_1, …) must not already be used in the model. - PDEs on fine grids are stiff, so keep the default
"liblsoda"or use"lsoda"rather than a non-stiff method. For large grids,method = "cvode", cvodeLinSolver = "band"factors only the band of the Jacobian that the grid stencil fills.