| 1 |
\name{daspk} |
| 2 |
\alias{daspk} |
| 3 |
\title{Solver for Differential Algebraic Equations (DAE)} |
| 4 |
\description{ |
| 5 |
Solves either: |
| 6 |
\itemize{ |
| 7 |
\item a system of ordinary differential equations (ODE) of the form |
| 8 |
\deqn{y' = f(t, y, ...)} or |
| 9 |
\item a system of differential algebraic equations (DAE) of the form |
| 10 |
\deqn{F(t,y,y') = 0} or |
| 11 |
\item a system of linearly implicit DAES in the |
| 12 |
form \deqn{M y' = f(t, y)} |
| 13 |
} |
| 14 |
using a combination of backward differentiation formula (BDF) and a |
| 15 |
direct linear system solution method (dense or banded). |
| 16 |
|
| 17 |
The \R function \code{daspk} provides an interface to the FORTRAN DAE |
| 18 |
solver of the same name, written by Linda R. Petzold, Peter N. Brown, |
| 19 |
Alan C. Hindmarsh and Clement W. Ulrich. |
| 20 |
|
| 21 |
The system of DE's is written as an \R function (which may, of course, |
| 22 |
use \code{\link{.C}}, \code{.Fortran}, \code{\link{.Call}}, etc., to |
| 23 |
call foreign code) or be defined in compiled code that has been |
| 24 |
dynamically loaded. |
| 25 |
} |
| 26 |
\usage{ |
| 27 |
daspk(y, times, func = NULL, parms, nind = c(length(y), 0, 0), |
| 28 |
dy = NULL, res = NULL, nalg = 0, |
| 29 |
rtol = 1e-6, atol = 1e-6, jacfunc = NULL, |
| 30 |
jacres = NULL, jactype = "fullint", mass = NULL, estini = NULL, |
| 31 |
verbose = FALSE, tcrit = NULL, hmin = 0, hmax = NULL, |
| 32 |
hini = 0, ynames = TRUE, maxord = 5, bandup = NULL, |
| 33 |
banddown = NULL, maxsteps = 5000, dllname = NULL, |
| 34 |
initfunc = dllname, initpar = parms, rpar = NULL, |
| 35 |
ipar = NULL, nout = 0, outnames = NULL, |
| 36 |
forcings=NULL, initforc = NULL, fcontrol=NULL, |
| 37 |
events = NULL, lags = NULL, ...) |
| 38 |
} |
| 39 |
|
| 40 |
\arguments{ |
| 41 |
\item{y }{the initial (state) values for the DE system. If \code{y} |
| 42 |
has a name attribute, the names will be used to label the output |
| 43 |
matrix. |
| 44 |
} |
| 45 |
\item{times }{time sequence for which output is wanted; the first |
| 46 |
value of \code{times} must be the initial time; if only one step is |
| 47 |
to be taken; set \code{times} = \code{NULL}. |
| 48 |
} |
| 49 |
\item{func }{to be used if the model is an ODE, or a DAE written in linearly |
| 50 |
implicit form (M y' = f(t, y)). |
| 51 |
\code{func} should be an \R-function that computes the |
| 52 |
values of the derivatives in the ODE system (the \emph{model |
| 53 |
definition}) at time t. |
| 54 |
|
| 55 |
\code{func} must be defined as: \code{func <- function(t, y, parms,...)}. |
| 56 |
\cr \code{t} is the current time point in the |
| 57 |
integration, \code{y} is the current estimate of the variables in |
| 58 |
the ODE system. If the initial values \code{y} has a \code{names} |
| 59 |
attribute, the names will be available inside \code{func}, unless |
| 60 |
\code{ynames} is FALSE. \code{parms} is a vector or list of |
| 61 |
parameters. \code{...} (optional) are any other arguments passed to |
| 62 |
the function. |
| 63 |
|
| 64 |
The return value of \code{func} should be a list, |
| 65 |
whose first element is a vector containing the derivatives of |
| 66 |
\code{y} with respect to \code{time}, and whose next elements are |
| 67 |
global values that are required at each point in \code{times}. |
| 68 |
The derivatives should be specified in the same order as the specification |
| 69 |
of the state variables \code{y}. |
| 70 |
|
| 71 |
Note that it is not possible to define \code{func} as a compiled |
| 72 |
function in a dynamically loaded shared library. Use \code{res} |
| 73 |
instead. |
| 74 |
} |
| 75 |
\item{parms }{vector or list of parameters used in \code{func}, |
| 76 |
\code{jacfunc}, or \code{res} |
| 77 |
} |
| 78 |
\item{nind }{if a DAE system: a three-valued vector with the number of |
| 79 |
variables of index 1, 2, 3 respectively. |
| 80 |
The equations must be defined such that the index 1 variables precede |
| 81 |
the index 2 variables which in turn precede the index 3 variables. |
| 82 |
The sum of the variables of different index should equal N, |
| 83 |
the total number of variables. |
| 84 |
Note that this has been added for consistency with \link{radau}. |
| 85 |
If used, then the variables are weighed differently than in the |
| 86 |
original daspk code, i.e. index 2 variables are scaled with 1/h, |
| 87 |
index 3 variables are scaled with 1/h^2. In some cases this allows |
| 88 |
daspk to solve index 2 or index 3 problems. |
| 89 |
} |
| 90 |
\item{dy }{the initial derivatives of the state variables of the DE |
| 91 |
system. Ignored if an ODE. |
| 92 |
} |
| 93 |
\item{res }{if a DAE system: either an \R-function that computes the |
| 94 |
residual function \eqn{F(t,y,y')} of the DAE system (the model |
| 95 |
defininition) at time \code{t}, or a character string giving the |
| 96 |
name of a compiled function in a dynamically loaded shared library. |
| 97 |
|
| 98 |
If \code{res} is a user-supplied \R-function, it must be defined as: |
| 99 |
\code{res <- function(t, y, dy, parms, ...)}. |
| 100 |
|
| 101 |
Here \code{t} is the current time point in the integration, \code{y} |
| 102 |
is the current estimate of the variables in the ODE system, |
| 103 |
\code{dy} are the corresponding derivatives. If the initial |
| 104 |
\code{y} or \code{dy} have a \code{names} attribute, the names will be |
| 105 |
available inside \code{res}, unless \code{ynames} is \code{FALSE}. |
| 106 |
\code{parms} is a vector of parameters. |
| 107 |
|
| 108 |
The return value of \code{res} should be a list, whose first element |
| 109 |
is a vector containing the residuals of the DAE system, |
| 110 |
i.e. \eqn{\delta = F(t,y,y')}{delta = F(t,y,y')}, and whose next elements contain output |
| 111 |
variables that are required at each point in \code{times}. |
| 112 |
|
| 113 |
If \code{res} is a string, then \code{dllname} must give the name of |
| 114 |
the shared library (without extension) which must be loaded before |
| 115 |
\code{daspk()} is called (see package vignette \code{"compiledCode"} |
| 116 |
for more information). |
| 117 |
} |
| 118 |
\item{nalg }{if a DAE system: the number of algebraic equations |
| 119 |
(equations not involving derivatives). Algebraic equations should |
| 120 |
always be the last, i.e. preceeded by the differential equations. |
| 121 |
|
| 122 |
Only used if \code{estini} = 1. |
| 123 |
} |
| 124 |
\item{rtol }{relative error tolerance, either a scalar or a vector, |
| 125 |
one value for each y, |
| 126 |
} |
| 127 |
\item{atol }{absolute error tolerance, either a scalar or a vector, |
| 128 |
one value for each y. |
| 129 |
} |
| 130 |
\item{jacfunc }{if not \code{NULL}, an \R function that computes the |
| 131 |
Jacobian of the system of differential equations. Only used in case |
| 132 |
the system is an ODE (\eqn{y' = f(t, y)}), specified by \code{func}. The \R |
| 133 |
calling sequence for \code{jacfunc} is identical to that of |
| 134 |
\code{func}. |
| 135 |
|
| 136 |
If the Jacobian is a full matrix, \code{jacfunc} should return a |
| 137 |
matrix \eqn{\partial\dot{y}/\partial y}{dydot/dy}, where the ith row contains the derivative of |
| 138 |
\eqn{dy_i/dt} with respect to \eqn{y_j}, or a vector containing the |
| 139 |
matrix elements by columns (the way \R and FORTRAN store matrices). |
| 140 |
|
| 141 |
If the Jacobian is banded, \code{jacfunc} should return a matrix |
| 142 |
containing only the nonzero bands of the Jacobian, rotated |
| 143 |
row-wise. See first example of lsode. |
| 144 |
} |
| 145 |
\item{jacres }{ \code{jacres} and not \code{jacfunc} should be used if |
| 146 |
the system is specified by the residual function \eqn{F(t, y, y')}, |
| 147 |
i.e. \code{jacres} is used in conjunction with \code{res}. |
| 148 |
|
| 149 |
If \code{jacres} is an \R-function, the calling sequence for |
| 150 |
\code{jacres} is identical to that of \code{res}, but with extra |
| 151 |
parameter \code{cj}. Thus it should be called as: \code{jacres = |
| 152 |
func(t, y, dy, parms, cj, ...)}. Here \code{t} is the current time |
| 153 |
point in the integration, \code{y} is the current estimate of the |
| 154 |
variables in the ODE system, \eqn{y'} are the corresponding derivatives |
| 155 |
and \code{cj} is a scalar, which is normally proportional to |
| 156 |
the inverse of the stepsize. If the initial \code{y} or \code{dy} |
| 157 |
have a \code{names} attribute, the names will be available inside |
| 158 |
\code{jacres}, unless |
| 159 |
\code{ynames} is \code{FALSE}. \code{parms} is a vector of |
| 160 |
parameters (which may have a names attribute). |
| 161 |
|
| 162 |
If the Jacobian is a full matrix, \code{jacres} should return the |
| 163 |
matrix \eqn{dG/dy + c_j\cdot dG/dy'}{dG/d y + cj*dG/d y'}, where the \eqn{i}th row is the sum of the |
| 164 |
derivatives of \eqn{G_i} with respect to \eqn{y_j} and the scaled |
| 165 |
derivatives of \eqn{G_i} with respect to \eqn{y'_j}. |
| 166 |
|
| 167 |
If the Jacobian is banded, \code{jacres} should return only the |
| 168 |
nonzero bands of the Jacobian, rotated rowwise. See details for the |
| 169 |
calling sequence when \code{jacres} is a string. |
| 170 |
} |
| 171 |
\item{jactype }{the structure of the Jacobian, one of |
| 172 |
\code{"fullint"}, \code{"fullusr"}, \code{"bandusr"} or |
| 173 |
\code{"bandint"} - either full or banded and estimated internally or |
| 174 |
by the user. |
| 175 |
} |
| 176 |
\item{mass }{the mass matrix. |
| 177 |
If not \code{NULL}, the problem is a linearly |
| 178 |
implicit DAE and defined as \eqn{M\, dy/dt = f(t,y)}{M dy/dt = f(t,y)}. The mass-matrix \eqn{M} |
| 179 |
should be of dimension \eqn{n*n} where \eqn{n} is the |
| 180 |
number of \eqn{y}-values. |
| 181 |
|
| 182 |
If \code{mass=NULL} then the model is either an ODE or a DAE, specified with |
| 183 |
\code{res} |
| 184 |
} |
| 185 |
\item{estini }{only if a DAE system, and if initial values of \code{y} |
| 186 |
and \code{dy} are not consistent (i.e. \eqn{F(t,y,dy) \neq 0}{F(t, |
| 187 |
y, dy) != 0}), setting |
| 188 |
\code{estini} = 1 or 2, will solve for them. If \code{estini} = 1: dy |
| 189 |
and the algebraic variables are estimated from \code{y}; in this |
| 190 |
case, the number of algebraic equations must be given (\code{nalg}). |
| 191 |
If \code{estini} = 2: \code{y} will be estimated from \code{dy}. |
| 192 |
} |
| 193 |
\item{verbose }{if TRUE: full output to the screen, e.g. will |
| 194 |
print the \code{diagnostiscs} of the integration - see details. |
| 195 |
} |
| 196 |
\item{tcrit }{the FORTRAN routine \code{daspk} overshoots its targets |
| 197 |
(times points in the vector \code{times}), and interpolates values |
| 198 |
for the desired time points. If there is a time beyond which |
| 199 |
integration should not proceed (perhaps because of a singularity), |
| 200 |
that should be provided in \code{tcrit}. |
| 201 |
} |
| 202 |
\item{hmin }{an optional minimum value of the integration stepsize. In |
| 203 |
special situations this parameter may speed up computations with the |
| 204 |
cost of precision. Don't use \code{hmin} if you don't know why! |
| 205 |
} |
| 206 |
\item{hmax }{an optional maximum value of the integration stepsize. If |
| 207 |
not specified, \code{hmax} is set to the largest difference in |
| 208 |
\code{times}, to avoid that the simulation possibly ignores |
| 209 |
short-term events. If 0, no maximal size is specified. |
| 210 |
} |
| 211 |
\item{hini }{initial step size to be attempted; if 0, the initial step |
| 212 |
size is determined by the solver |
| 213 |
} |
| 214 |
\item{ynames }{logical, if \code{FALSE}, names of state variables are not |
| 215 |
passed to function \code{func}; this may speed up the simulation especially |
| 216 |
for large models. |
| 217 |
} |
| 218 |
\item{maxord }{the maximum order to be allowed. Reduce \code{maxord} |
| 219 |
to save storage space ( <= 5) |
| 220 |
} |
| 221 |
\item{bandup }{number of non-zero bands above the diagonal, in case |
| 222 |
the Jacobian is banded (and \code{jactype} one of |
| 223 |
"bandint", "bandusr") |
| 224 |
} |
| 225 |
\item{banddown }{number of non-zero bands below the diagonal, in case |
| 226 |
the Jacobian is banded (and \code{jactype} one of |
| 227 |
"bandint", "bandusr") |
| 228 |
} |
| 229 |
\item{maxsteps }{maximal number of steps per output interval taken by the |
| 230 |
solver; will be recalculated to be at least 500 and a multiple of |
| 231 |
500; if \code{verbose} is \code{TRUE} |
| 232 |
the solver will give a warning if more than 500 steps are |
| 233 |
taken, but it will continue till \code{maxsteps} steps. |
| 234 |
(Note this warning was always given in deSolve versions < 1.10.3). |
| 235 |
} |
| 236 |
\item{dllname }{a string giving the name of the shared library |
| 237 |
(without extension) that contains all the compiled function or |
| 238 |
subroutine definitions referred to in \code{res} and |
| 239 |
\code{jacres}. See package vignette \code{"compiledCode"}. |
| 240 |
} |
| 241 |
\item{initfunc }{if not \code{NULL}, the name of the initialisation function |
| 242 |
(which initialises values of parameters), as provided in |
| 243 |
\file{dllname}. See package vignette \code{"compiledCode"}. |
| 244 |
} |
| 245 |
\item{initpar }{only when \file{dllname} is specified and an |
| 246 |
initialisation function \code{initfunc} is in the dll: the |
| 247 |
parameters passed to the initialiser, to initialise the common |
| 248 |
blocks (FORTRAN) or global variables (C, C++). |
| 249 |
} |
| 250 |
\item{rpar }{only when \file{dllname} is specified: a vector with |
| 251 |
double precision values passed to the dll-functions whose names are |
| 252 |
specified by \code{res} and \code{jacres}. |
| 253 |
} |
| 254 |
\item{ipar }{only when \file{dllname} is specified: a vector with |
| 255 |
integer values passed to the dll-functions whose names are specified |
| 256 |
by \code{res} and \code{jacres}. |
| 257 |
} |
| 258 |
\item{nout }{only used if \file{dllname} is specified and the model is |
| 259 |
defined in compiled code: the number of output variables calculated |
| 260 |
in the compiled function \code{res}, present in the shared |
| 261 |
library. Note: it is not automatically checked whether this is |
| 262 |
indeed the number of output variables calculated in the dll - you have |
| 263 |
to perform this check in the code - See package vignette |
| 264 |
\code{"compiledCode"}. |
| 265 |
} |
| 266 |
\item{outnames }{only used if \file{dllname} is specified and |
| 267 |
\code{nout} > 0: the names of output variables calculated in the |
| 268 |
compiled function \code{res}, present in the shared library. |
| 269 |
These names will be used to label the output matrix. |
| 270 |
} |
| 271 |
\item{forcings }{only used if \file{dllname} is specified: a list with |
| 272 |
the forcing function data sets, each present as a two-columned matrix, |
| 273 |
with (time,value); interpolation outside the interval |
| 274 |
[min(\code{times}), max(\code{times})] is done by taking the value at |
| 275 |
the closest data extreme. |
| 276 |
|
| 277 |
See \link{forcings} or package vignette \code{"compiledCode"}. |
| 278 |
} |
| 279 |
\item{initforc }{if not \code{NULL}, the name of the forcing function |
| 280 |
initialisation function, as provided in |
| 281 |
\file{dllname}. It MUST be present if \code{forcings} has been given a |
| 282 |
value. |
| 283 |
See \link{forcings} or package vignette \code{"compiledCode"}. |
| 284 |
} |
| 285 |
\item{fcontrol }{A list of control parameters for the forcing functions. |
| 286 |
See \link{forcings} or vignette \code{compiledCode}. |
| 287 |
} |
| 288 |
\item{events }{A matrix or data frame that specifies events, i.e. when the value of a |
| 289 |
state variable is suddenly changed. See \link{events} for more information. |
| 290 |
} |
| 291 |
\item{lags }{A list that specifies timelags, i.e. the number of steps |
| 292 |
that has to be kept. To be used for delay differential equations. |
| 293 |
See \link{timelags}, \link{dede} for more information. |
| 294 |
} |
| 295 |
\item{... }{additional arguments passed to \code{func}, |
| 296 |
\code{jacfunc}, \code{res} and \code{jacres}, allowing this to be a |
| 297 |
generic function. |
| 298 |
} |
| 299 |
} |
| 300 |
\value{ |
| 301 |
A matrix of class \code{deSolve} with up to as many rows as elements in |
| 302 |
\code{times} and as many |
| 303 |
columns as elements in \code{y} plus the number of "global" values |
| 304 |
returned in the next elements of the return from \code{func} or |
| 305 |
\code{res}, plus an additional column (the first) for the time value. |
| 306 |
There will be one row for each element in \code{times} unless the |
| 307 |
FORTRAN routine `daspk' returns with an unrecoverable error. If |
| 308 |
\code{y} has a names attribute, it will be used to label the columns |
| 309 |
of the output value. |
| 310 |
|
| 311 |
} |
| 312 |
\author{Karline Soetaert <[email protected]>} |
| 313 |
\examples{ |
| 314 |
## ======================================================================= |
| 315 |
## Coupled chemical reactions including an equilibrium |
| 316 |
## modeled as (1) an ODE and (2) as a DAE |
| 317 |
## |
| 318 |
## The model describes three chemical species A,B,D: |
| 319 |
## subjected to equilibrium reaction D <- > A + B |
| 320 |
## D is produced at a constant rate, prod |
| 321 |
## B is consumed at 1s-t order rate, r |
| 322 |
## Chemical problem formulation 1: ODE |
| 323 |
## ======================================================================= |
| 324 |
|
| 325 |
## Dissociation constant |
| 326 |
K <- 1 |
| 327 |
|
| 328 |
## parameters |
| 329 |
pars <- c( |
| 330 |
ka = 1e6, # forward rate |
| 331 |
r = 1, |
| 332 |
prod = 0.1) |
| 333 |
|
| 334 |
|
| 335 |
Fun_ODE <- function (t, y, pars) |
| 336 |
{ |
| 337 |
with (as.list(c(y, pars)), { |
| 338 |
ra <- ka*D # forward rate |
| 339 |
rb <- ka/K *A*B # backward rate |
| 340 |
|
| 341 |
## rates of changes |
| 342 |
dD <- -ra + rb + prod |
| 343 |
dA <- ra - rb |
| 344 |
dB <- ra - rb - r*B |
| 345 |
return(list(dy = c(dA, dB, dD), |
| 346 |
CONC = A+B+D)) |
| 347 |
}) |
| 348 |
} |
| 349 |
|
| 350 |
## ======================================================================= |
| 351 |
## Chemical problem formulation 2: DAE |
| 352 |
## 1. get rid of the fast reactions ra and rb by taking |
| 353 |
## linear combinations : dD+dA = prod (res1) and |
| 354 |
## dB-dA = -r*B (res2) |
| 355 |
## 2. In addition, the equilibrium condition (eq) reads: |
| 356 |
## as ra = rb : ka*D = ka/K*A*B = > K*D = A*B |
| 357 |
## ======================================================================= |
| 358 |
|
| 359 |
Res_DAE <- function (t, y, yprime, pars) |
| 360 |
{ |
| 361 |
with (as.list(c(y, yprime, pars)), { |
| 362 |
|
| 363 |
## residuals of lumped rates of changes |
| 364 |
res1 <- -dD - dA + prod |
| 365 |
res2 <- -dB + dA - r*B |
| 366 |
|
| 367 |
## and the equilibrium equation |
| 368 |
eq <- K*D - A*B |
| 369 |
|
| 370 |
return(list(c(res1, res2, eq), |
| 371 |
CONC = A+B+D)) |
| 372 |
}) |
| 373 |
} |
| 374 |
|
| 375 |
## ======================================================================= |
| 376 |
## Chemical problem formulation 3: Mass * Func |
| 377 |
## Based on the DAE formulation |
| 378 |
## ======================================================================= |
| 379 |
|
| 380 |
Mass_FUN <- function (t, y, pars) { |
| 381 |
with (as.list(c(y, pars)), { |
| 382 |
|
| 383 |
## as above, but without the |
| 384 |
f1 <- prod |
| 385 |
f2 <- - r*B |
| 386 |
|
| 387 |
## and the equilibrium equation |
| 388 |
f3 <- K*D - A*B |
| 389 |
|
| 390 |
return(list(c(f1, f2, f3), |
| 391 |
CONC = A+B+D)) |
| 392 |
}) |
| 393 |
} |
| 394 |
Mass <- matrix(nrow = 3, ncol = 3, byrow = TRUE, |
| 395 |
data=c(1, 0, 1, # dA + 0 + dB |
| 396 |
-1, 1, 0, # -dA + dB +0 |
| 397 |
0, 0, 0)) # algebraic |
| 398 |
|
| 399 |
times <- seq(0, 100, by = 2) |
| 400 |
|
| 401 |
## Initial conc; D is in equilibrium with A,B |
| 402 |
y <- c(A = 2, B = 3, D = 2*3/K) |
| 403 |
|
| 404 |
## ODE model solved with daspk |
| 405 |
ODE <- daspk(y = y, times = times, func = Fun_ODE, |
| 406 |
parms = pars, atol = 1e-10, rtol = 1e-10) |
| 407 |
|
| 408 |
## Initial rate of change |
| 409 |
dy <- c(dA = 0, dB = 0, dD = 0) |
| 410 |
|
| 411 |
## DAE model solved with daspk |
| 412 |
DAE <- daspk(y = y, dy = dy, times = times, |
| 413 |
res = Res_DAE, parms = pars, atol = 1e-10, rtol = 1e-10) |
| 414 |
|
| 415 |
MASS<- daspk(y=y, times=times, func = Mass_FUN, parms = pars, mass = Mass) |
| 416 |
|
| 417 |
## ================ |
| 418 |
## plotting output |
| 419 |
## ================ |
| 420 |
|
| 421 |
plot(ODE, DAE, xlab = "time", ylab = "conc", type = c("l", "p"), |
| 422 |
pch = c(NA, 1)) |
| 423 |
|
| 424 |
legend("bottomright", lty = c(1, NA), pch = c(NA, 1), |
| 425 |
col = c("black", "red"), legend = c("ODE", "DAE")) |
| 426 |
|
| 427 |
# difference between both implementations: |
| 428 |
max(abs(ODE-DAE)) |
| 429 |
|
| 430 |
## ======================================================================= |
| 431 |
## same DAE model, now with the Jacobian |
| 432 |
## ======================================================================= |
| 433 |
jacres_DAE <- function (t, y, yprime, pars, cj) |
| 434 |
{ |
| 435 |
with (as.list(c(y, yprime, pars)), { |
| 436 |
## res1 = -dD - dA + prod |
| 437 |
PD[1,1] <- -1*cj # d(res1)/d(A)-cj*d(res1)/d(dA) |
| 438 |
PD[1,2] <- 0 # d(res1)/d(B)-cj*d(res1)/d(dB) |
| 439 |
PD[1,3] <- -1*cj # d(res1)/d(D)-cj*d(res1)/d(dD) |
| 440 |
## res2 = -dB + dA - r*B |
| 441 |
PD[2,1] <- 1*cj |
| 442 |
PD[2,2] <- -r -1*cj |
| 443 |
PD[2,3] <- 0 |
| 444 |
## eq = K*D - A*B |
| 445 |
PD[3,1] <- -B |
| 446 |
PD[3,2] <- -A |
| 447 |
PD[3,3] <- K |
| 448 |
return(PD) |
| 449 |
}) |
| 450 |
} |
| 451 |
|
| 452 |
PD <- matrix(ncol = 3, nrow = 3, 0) |
| 453 |
|
| 454 |
DAE2 <- daspk(y = y, dy = dy, times = times, |
| 455 |
res = Res_DAE, jacres = jacres_DAE, jactype = "fullusr", |
| 456 |
parms = pars, atol = 1e-10, rtol = 1e-10) |
| 457 |
|
| 458 |
max(abs(DAE-DAE2)) |
| 459 |
|
| 460 |
## See \dynload subdirectory for a FORTRAN implementation of this model |
| 461 |
|
| 462 |
## ======================================================================= |
| 463 |
## The chemical model as a DLL, with production a forcing function |
| 464 |
## ======================================================================= |
| 465 |
times <- seq(0, 100, by = 2) |
| 466 |
|
| 467 |
pars <- c(K = 1, ka = 1e6, r = 1) |
| 468 |
|
| 469 |
## Initial conc; D is in equilibrium with A,B |
| 470 |
y <- c(A = 2, B = 3, D = as.double(2*3/pars["K"])) |
| 471 |
|
| 472 |
## Initial rate of change |
| 473 |
dy <- c(dA = 0, dB = 0, dD = 0) |
| 474 |
|
| 475 |
# production increases with time |
| 476 |
prod <- matrix(ncol = 2, |
| 477 |
data = c(seq(0, 100, by = 10), 0.1*(1+runif(11)*1))) |
| 478 |
|
| 479 |
ODE_dll <- daspk(y = y, dy = dy, times = times, res = "chemres", |
| 480 |
dllname = "deSolve", initfunc = "initparms", |
| 481 |
initforc = "initforcs", parms = pars, forcings = prod, |
| 482 |
atol = 1e-10, rtol = 1e-10, nout = 2, |
| 483 |
outnames = c("CONC","Prod")) |
| 484 |
|
| 485 |
plot(ODE_dll, which = c("Prod", "D"), xlab = "time", |
| 486 |
ylab = c("/day", "conc"), main = c("production rate","D")) |
| 487 |
|
| 488 |
} |
| 489 |
\references{ |
| 490 |
L. R. Petzold, A Description of DASSL: A Differential/Algebraic |
| 491 |
System Solver, in Scientific Computing, R. S. Stepleman et al. |
| 492 |
(Eds.), North-Holland, Amsterdam, 1983, pp. 65-68. |
| 493 |
|
| 494 |
K. E. Brenan, S. L. Campbell, and L. R. Petzold, Numerical |
| 495 |
Solution of Initial-Value Problems in Differential-Algebraic |
| 496 |
Equations, Elsevier, New York, 1989. |
| 497 |
|
| 498 |
P. N. Brown and A. C. Hindmarsh, Reduced Storage Matrix Methods |
| 499 |
in Stiff ODE Systems, J. Applied Mathematics and Computation, |
| 500 |
31 (1989), pp. 40-91. |
| 501 |
\doi{10.1016/0096-3003(89)90110-0} |
| 502 |
|
| 503 |
P. N. Brown, A. C. Hindmarsh, and L. R. Petzold, Using Krylov |
| 504 |
Methods in the Solution of Large-Scale Differential-Algebraic |
| 505 |
Systems, SIAM J. Sci. Comp., 15 (1994), pp. 1467-1488. |
| 506 |
\doi{10.1137/0915088} |
| 507 |
|
| 508 |
P. N. Brown, A. C. Hindmarsh, and L. R. Petzold, Consistent |
| 509 |
Initial Condition Calculation for Differential-Algebraic |
| 510 |
Systems, LLNL Report UCRL-JC-122175, August 1995; submitted to |
| 511 |
SIAM J. Sci. Comp. |
| 512 |
|
| 513 |
Netlib: \url{https://netlib.org} |
| 514 |
} |
| 515 |
\details{ |
| 516 |
The daspk solver uses the backward differentiation formulas of orders |
| 517 |
one through five (specified with \code{maxord}) to solve either: |
| 518 |
\itemize{ |
| 519 |
\item an ODE system of the form \deqn{y' = f(t,y,...)} |
| 520 |
or |
| 521 |
\item a DAE system of the form \deqn{y' = M f(t,y,...)} |
| 522 |
or |
| 523 |
\item a DAE system of the form \deqn{F(t,y,y') = 0}. |
| 524 |
The index of the DAE should be preferable <= 1. |
| 525 |
} |
| 526 |
|
| 527 |
ODEs are specified using argument \code{func}, |
| 528 |
DAEs are specified using argument \code{res}. |
| 529 |
|
| 530 |
If a DAE system, Values for y \emph{and} y' (argument \code{dy}) |
| 531 |
at the initial time must be given as input. Ideally, these values should be consistent, |
| 532 |
that is, if t, y, y' are the given initial values, they should |
| 533 |
satisfy F(t,y,y') = 0. \cr However, if consistent values are not |
| 534 |
known, in many cases daspk can solve for them: when \code{estini} = 1, |
| 535 |
y' and algebraic variables (their number specified with \code{nalg}) |
| 536 |
will be estimated, when \code{estini} = 2, y will be estimated. |
| 537 |
|
| 538 |
The form of the \bold{Jacobian} can be specified by |
| 539 |
\code{jactype}. This is one of: |
| 540 |
|
| 541 |
\describe{ |
| 542 |
\item{jactype = "fullint":}{a full Jacobian, calculated internally |
| 543 |
by \code{daspk}, the default, |
| 544 |
} |
| 545 |
\item{jactype = "fullusr":}{a full Jacobian, specified by user |
| 546 |
function \code{jacfunc} or \code{jacres}, |
| 547 |
} |
| 548 |
\item{jactype = "bandusr":}{a banded Jacobian, specified by user |
| 549 |
function \code{jacfunc} or \code{jacres}; the size of the bands |
| 550 |
specified by \code{bandup} and \code{banddown}, |
| 551 |
} |
| 552 |
\item{jactype = "bandint":}{a banded Jacobian, calculated by |
| 553 |
\code{daspk}; the size of the bands specified by \code{bandup} and |
| 554 |
\code{banddown}. |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
If \code{jactype} = "fullusr" or "bandusr" then the user must supply a |
| 559 |
subroutine \code{jacfunc}. |
| 560 |
|
| 561 |
If jactype = "fullusr" or "bandusr" then the user must supply a |
| 562 |
subroutine \code{jacfunc} or \code{jacres}. |
| 563 |
|
| 564 |
The input parameters \code{rtol}, and \code{atol} determine the |
| 565 |
\bold{error control} performed by the solver. If the request for |
| 566 |
precision exceeds the capabilities of the machine, \code{daspk} will return |
| 567 |
an error code. See \code{\link{lsoda}} for details. |
| 568 |
|
| 569 |
When the index of the variables is specified (argument \code{nind}), |
| 570 |
and higher index variables |
| 571 |
are present, then the equations are scaled such that equations corresponding |
| 572 |
to index 2 variables are multiplied with 1/h, for index 3 they are multiplied |
| 573 |
with 1/h^2, where h is the time step. This is not in the standard DASPK code, |
| 574 |
but has been added for consistency with solver \link{radau}. Because of this, |
| 575 |
daspk can solve certain index 2 or index 3 problems. |
| 576 |
|
| 577 |
\bold{res and jacres} may be defined in compiled C or FORTRAN code, as |
| 578 |
well as in an R-function. See package vignette \code{"compiledCode"} |
| 579 |
for details. Examples |
| 580 |
in FORTRAN are in the \file{dynload} subdirectory of the |
| 581 |
\code{deSolve} package directory. |
| 582 |
|
| 583 |
The diagnostics of the integration can be printed to screen |
| 584 |
by calling \code{\link{diagnostics}}. If \code{verbose} = \code{TRUE}, |
| 585 |
the diagnostics will written to the screen at the end of the integration. |
| 586 |
|
| 587 |
See vignette("deSolve") for an explanation of each element in the vectors |
| 588 |
containing the diagnostic properties and how to directly access them. |
| 589 |
|
| 590 |
\bold{Models} may be defined in compiled C or FORTRAN code, as well as |
| 591 |
in an R-function. See package vignette \code{"compiledCode"} for details. |
| 592 |
|
| 593 |
More information about models defined in compiled code is in the package |
| 594 |
vignette ("compiledCode"); information about linking forcing functions |
| 595 |
to compiled code is in \link{forcings}. |
| 596 |
|
| 597 |
Examples in both C and FORTRAN are in the \file{dynload} subdirectory |
| 598 |
of the \code{deSolve} package directory. |
| 599 |
} |
| 600 |
\seealso{ |
| 601 |
\itemize{ |
| 602 |
\item \code{\link{radau}} for integrating DAEs up to index 3, |
| 603 |
\item \code{\link{rk}}, \item \code{\link{rk4}} and \code{\link{euler}} for |
| 604 |
Runge-Kutta integrators. |
| 605 |
\item \code{\link{lsoda}}, \code{\link{lsode}}, |
| 606 |
\code{\link{lsodes}}, \code{\link{lsodar}}, \code{\link{vode}}, |
| 607 |
for other solvers of the Livermore family, |
| 608 |
\item \code{\link{ode}} for a general interface to most of the ODE solvers, |
| 609 |
\item \code{\link{ode.band}} for solving models with a banded |
| 610 |
Jacobian, |
| 611 |
\item \code{\link{ode.1D}} for integrating 1-D models, |
| 612 |
\item \code{\link{ode.2D}} for integrating 2-D models, |
| 613 |
\item \code{\link{ode.3D}} for integrating 3-D models, |
| 614 |
} |
| 615 |
|
| 616 |
\code{\link{diagnostics}} to print diagnostic messages. |
| 617 |
} |
| 618 |
\note{ |
| 619 |
In this version, the Krylov method is not (yet) supported. |
| 620 |
|
| 621 |
From \code{deSolve} version 1.10.4 and above, the following changes were made |
| 622 |
|
| 623 |
\enumerate{ |
| 624 |
\item the argument list to \code{daspk} |
| 625 |
now also includes \code{nind}, the index of each variable. |
| 626 |
This is used to scale the variables, such that \code{daspk} in R can also solve |
| 627 |
certain index 2 or index 3 problems, which the original Fortran version |
| 628 |
may not be able to solve. |
| 629 |
|
| 630 |
\item the default of \code{atol} was changed from 1e-8 to 1e-6, |
| 631 |
to be consistent with the other solvers. |
| 632 |
|
| 633 |
\item the multiple warnings from daspk when the number of steps exceed 500 |
| 634 |
were toggled off unless \code{verbose} is \code{TRUE} |
| 635 |
} |
| 636 |
} |
| 637 |
\keyword{math} |
| 638 |
|