Validates and converts a set of conditions once, so that repeated solves
with solveBatch(), an optimizer evaluating the same model at new
parameters, only pass the numbers that changed. solveODEBatch() repeats
that work on every call.
Usage
prepareBatch(
model,
conditions,
times = NULL,
parms = NULL,
tangent = NULL,
hessian = NULL,
fixed = NULL,
forcings = NULL,
abstol = 1e-06,
reltol = 1e-06,
maxattempts = 50L,
maxsteps = 1000000L,
hini = 0,
roottol = 1e-06,
maxroot = 1L,
cotangent = NULL,
curvature = NULL,
keepStore = FALSE,
store = NULL,
sensErrCon = TRUE,
adjoint = NULL,
maxOrder = NULL,
stald = FALSE
)Arguments
- model
- conditions
A named or unnamed list of per-condition argument lists. Recognized names are
times,parms,tangent,hessian,cotangent,curvature,fixed,forcings,abstol,reltol,maxattempts,maxsteps,hini,roottol,maxroot,keepStore,store,sensErrCon,adjoint,maxOrderandstald; each overrides the batch-wide argument of the same name.- times, parms
Batch-wide
timesandparmsas insolveODE(), for the conditions that do not give their own. DefaultNULL.- tangent
Optional numeric matrix, the tangent of the inputs; shapes in Details. Default
NULL: the identity on the active sensitivities.- hessian
Optional numeric array, the second derivative of the inputs; shapes in Details. Needs a model with
attr(model, "deriv2"). DefaultNULL: the inputs are affine in \(\theta\).- fixed
Optional character vector, sensitivity names held fixed in this call, a subset of
attr(model, "dimNames")$sens. UnlikefixedofcppODE()it changes without recompiling. Not with a full or partialtangent. DefaultNULL.- forcings
Named list of forcing data, one element per name in
attr(model, "forcings"): adata.framewith columnstimeandvalue, or a two-column matrix.cppODE()andcvode()take the forcing names, this argument the data. A forcing is the monotone cubic Hermite interpolant (PCHIP) of the data points and constant beyond the first and the last point, seeforcingValues(). DefaultNULL.- abstol
Absolute error tolerance. Default
1e-6.- reltol
Relative error tolerance. Default
1e-6.- maxattempts
Maximum number of consecutive step attempts without progress in time before the solve stops with
return_code = -4. Default50.- maxsteps
Maximum total number of integration steps. Default
1e6.- hini
Initial step size;
0(default) triggers automatic estimation.- roottol
Tolerance for locating root events. Default
1e-6. Underrootfunc = "equilibrate"the solve stops once every \(|\dot x_i| \le \mathrm{roottol}\, |x_i| + \mathrm{abstol}\); on acppODE()model the test covers the sensitivities as well.- maxroot
Maximum number of triggers per root event. Default
1.- cotangent
The cotangent of the outputs,
[n_t, n_x]or[n_t, n_x, n_seed]. Required by a model compiled withderivMode = "reverse"or"forward-reverse"unlesskeepStore = TRUE; an error on any other model.result$cotangentis then \(\sum_{t,i} w_{t,i}\, \partial x_i(t) / \partial \theta\), one column per seed. DefaultNULL.- curvature
Optional,
"forward-reverse"only: the derivative ofcotangentalong the tangent,[n_t, n_x, n_seed, n_s]. For a cotangent that is the gradient of a functional of the outputs, this is that functional's Hessian applied to the output tangent. DefaultNULL: the cotangent is constant in \(\theta\).- keepStore
Logical, default
FALSE. Return the checkpoints of a reverse solve as$store, for a later solve to reuse throughstore. Thecotangentmay then be omitted, and the solve returns the values alone. Only on acppODE()model compiled withderivMode = "reverse".- store
The
$storeof an earlier solve of the same model at the sametimesandparms. The solve then integrates nothing and runs the backward sweep alone. A store from a different point is an error. A store may be reused any number of times. DefaultNULL.- sensErrCon
Logical. Include the sensitivities, under
derivMode = "forward-reverse"the tangents, in the error test and the first step. DefaultTRUE.FALSEtakes the steps and corrector iterations of a value run, up to rounding: cheaper and less accurate, the sensitivities are the derivative of the computed solution, as the reverse mode of"adams"returns; that of"bdf"solves the corrector equations instead. Under"forward-reverse"the step grid does not depend on the directions of the tangent. Needs a model with sensitivities, has no effect underderivMode = "reverse"; acvode()model takesTRUEonly.- adjoint
Optional
adjointControl()object for a model compiled withderivMode = "reverse". DefaultNULL, the same asadjointControl().- maxOrder
Optional upper bound on the order of a multistep method, 1 to 5 for
"bdf"and 1 to 12 for"adams". DefaultNULL, the method's own maximum. An error on"rb4"and"tsit5".- stald
Logical, default
FALSE. Stability limit detection for method"bdf": the order drops when the step size leaves the stability region of the current order. See Stepping and error control.
Value
An object of class "cppDEbatch" for solveBatch().
Examples
# \donttest{
if (codegenAvailable()) {
## Marshal the conditions once, then re-solve with new numbers, as inside an
## optimizer.
f <- cppODE(c(x = "-k*x"), deriv = FALSE)
conditions <- list(a = list(parms = c(x = 1, k = 0.1)),
b = list(parms = c(x = 2, k = 0.1)))
h <- prepareBatch(f, conditions, times = 0:5)
res <- solveBatch(h, parms = list(c(x = 1, k = 0.5), c(x = 2, k = 0.5)))
res$b$variable
}
#> using C++ compiler: g++ [-O2 -DNDEBUG -w -fPIC -fopenmp]
#> x
#> [1,] 2.0000000
#> [2,] 1.2130658
#> [3,] 0.7357696
#> [4,] 0.4462715
#> [5,] 0.2706804
#> [6,] 0.1641778
# }