Skip to contents

A model that declares a one-dimensional grid with grid() may write partial differential equations on it; before parsing, they are expanded into ordinary ODE states by the method of lines, so solving, sensitivities and estimation see a normal ODE model.

Usage

rxPde(model)

Arguments

model

model text

Value

the model text with every grid expanded into ODE states; text without a grid() declaration is returned unchanged

Details

The grid grid(x, from = 0, to = 1, n = 50) splits [from, to] into n cells of width h = (to - from)/n (finite volumes) with centers from + (i - 0.5)*h. from and to may be numbers or expressions of model parameters; n must be a positive integer constant. The grid name cannot be t or time. Supported statements and operators are:

  • d/dt(C[x]) = rhs declares the gridded state C, expanded into states C_1, ..., C_n. Inside rhs, C[x] (or the bare C) is the value in the current cell and x is the cell center.

  • C[x](0) = expr sets the initial condition of every cell (expr may use x).

  • r[x] <- expr defines a gridded intermediate variable r_1, ..., r_n.

  • d2x(C) is the second derivative (central difference); dx(C) is the first derivative as an upwind finite-volume difference for flow towards increasing x; dx(C, "forward") is upwind for flow towards decreasing x and dx(C, "central") is a central difference. For a grid named a these are d2a() and da().

  • value(C, x = 0) = g is a fixed-value (Dirichlet) boundary condition at the boundary x = 0 (which must be from or to), used by d2x(), central dx() and point values at that edge; an upwind dx() uses it only at its inflow edge. g cannot use gridded values.

  • flux(C, x = 0) = q adds an inward flux q (amount per unit length per time) through the boundary; the difference operators then use a zero gradient there. A boundary without a condition has zero gradient (no flux).

  • integrate(C, x) is the total over the grid (h times the cell sum); its first argument may be an expression of gridded values.

  • C[x = 0.5] is the value at a point, interpolated linearly between cell centers and the boundary values.

Doses go to individual cells by their state names (for example cmt = "C_1"); a cell holds a concentration, so a dose amt adds amt * h to integrate().

Author

Matthew L. Fidler

Examples

cat(rxPde("
  grid(x, from = 0, to = 1, n = 4)
  d/dt(C[x]) = D * d2x(C) - k * C[x]
  flux(C, x = 0) = kin
  tot = integrate(C, x)
"))
#> d/dt(C_1) = (D * ((C_2 - C_1)/0.0625) - k * C_1) + (kin)/0.25
#> d/dt(C_2) = D * ((C_1 - 2 * C_2 + C_3)/0.0625) - k * C_2
#> d/dt(C_3) = D * ((C_2 - 2 * C_3 + C_4)/0.0625) - k * C_3
#> d/dt(C_4) = D * ((-(C_4 - C_3))/0.0625) - k * C_4
#> tot = 0.25 * (C_1 + C_2 + C_3 + C_4)