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.
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]) = rhsdeclares the gridded stateC, expanded into statesC_1, ..., C_n. Insiderhs,C[x](or the bareC) is the value in the current cell andxis the cell center.C[x](0) = exprsets the initial condition of every cell (exprmay usex).r[x] <- exprdefines a gridded intermediate variabler_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 increasingx;dx(C, "forward")is upwind for flow towards decreasingxanddx(C, "central")is a central difference. For a grid namedathese ared2a()andda().value(C, x = 0) = gis a fixed-value (Dirichlet) boundary condition at the boundaryx = 0(which must befromorto), used byd2x(), centraldx()and point values at that edge; an upwinddx()uses it only at its inflow edge.gcannot use gridded values.flux(C, x = 0) = qadds an inward fluxq(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 (htimes 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().
See also
Other Model definition:
rxClean(),
rxCompile(),
rxCreateCache(),
rxDelete(),
rxLastCompile(),
rxNorm(),
rxReservedKeywords,
rxSupportedFuns(),
rxSyntaxFunctions,
rxUnloadAll(),
rxode2(),
summary.rxode2()
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)
