| 1 |
\name{lsodes} |
| 2 |
\alias{lsodes} |
| 3 |
\title{Solver for Ordinary Differential Equations (ODE) With |
| 4 |
Sparse Jacobian |
| 5 |
} |
| 6 |
\description{ |
| 7 |
Solves the initial value problem for stiff systems of ordinary |
| 8 |
differential equations (ODE) in the form: \deqn{dy/dt = f(t,y)} and |
| 9 |
where the Jacobian matrix df/dy has an arbitrary sparse structure. |
| 10 |
|
| 11 |
The \R function \code{lsodes} provides an interface to the FORTRAN ODE |
| 12 |
solver of the same name, written by Alan C. Hindmarsh and Andrew |
| 13 |
H. Sherman. |
| 14 |
|
| 15 |
The system of ODE's is written as an \R function or be defined in |
| 16 |
compiled code that has been dynamically loaded. |
| 17 |
} |
| 18 |
|
| 19 |
\usage{ |
| 20 |
lsodes(y, times, func, parms, rtol = 1e-6, atol = 1e-6, |
| 21 |
jacvec = NULL, sparsetype = "sparseint", nnz = NULL, |
| 22 |
inz = NULL, rootfunc = NULL, |
| 23 |
verbose = FALSE, nroot = 0, tcrit = NULL, hmin = 0, |
| 24 |
hmax = NULL, hini = 0, ynames = TRUE, maxord = NULL, |
| 25 |
maxsteps = 5000, lrw = NULL, liw = NULL, dllname = NULL, |
| 26 |
initfunc = dllname, initpar = parms, rpar = NULL, |
| 27 |
ipar = NULL, nout = 0, outnames = NULL, forcings=NULL, |
| 28 |
initforc = NULL, fcontrol=NULL, events=NULL, lags = NULL, |
| 29 |
...) |
| 30 |
} |
| 31 |
\arguments{ |
| 32 |
\item{y }{the initial (state) values for the ODE system. If \code{y} |
| 33 |
has a name attribute, the names will be used to label the output |
| 34 |
matrix. |
| 35 |
} |
| 36 |
\item{times }{time sequence for which output is wanted; the first |
| 37 |
value of \code{times} must be the initial time; if only one step is |
| 38 |
to be taken; set \code{times} = \code{NULL}. |
| 39 |
} |
| 40 |
\item{func }{either an \R-function that computes the values of the |
| 41 |
derivatives in the ODE system (the \emph{model definition}) at time |
| 42 |
\code{t}, or a character string giving the name of a compiled |
| 43 |
function in a dynamically loaded shared library. |
| 44 |
|
| 45 |
If \code{func} is an \R-function, it must be defined as: |
| 46 |
\code{func <- function(t, y, parms,...)}. \code{t} is the current |
| 47 |
time point in the integration, \code{y} is the current estimate of |
| 48 |
the variables in the ODE system. If the initial values \code{y} has |
| 49 |
a \code{names} attribute, the names will be available inside \code{func}. |
| 50 |
\code{parms} is a vector or list of parameters; ... (optional) are |
| 51 |
any other arguments passed to the function. |
| 52 |
|
| 53 |
The return value of \code{func} should be a list, whose first |
| 54 |
element is a vector containing the derivatives of \code{y} with |
| 55 |
respect to \code{time}, and whose next elements are global values |
| 56 |
that are required at each point in \code{times}. The derivatives |
| 57 |
must be specified in the \bold{same order} as the state variables \code{y}. |
| 58 |
|
| 59 |
If \code{func} is |
| 60 |
a string, then \code{dllname} must give the name of the shared |
| 61 |
library (without extension) which must be loaded before |
| 62 |
\code{lsodes()} is called. See package vignette \code{"compiledCode"} |
| 63 |
for more details. |
| 64 |
} |
| 65 |
\item{parms }{vector or list of parameters used in \code{func} or |
| 66 |
\code{jacfunc}. |
| 67 |
} |
| 68 |
\item{rtol }{relative error tolerance, either a scalar or an array as |
| 69 |
long as \code{y}. See details. |
| 70 |
} |
| 71 |
\item{atol }{absolute error tolerance, either a scalar or an array as |
| 72 |
long as \code{y}. See details. |
| 73 |
} |
| 74 |
\item{jacvec }{if not \code{NULL}, an \R function that computes a |
| 75 |
column of the Jacobian of the system of differential equations |
| 76 |
\eqn{\partial\dot{y}_i/\partial y_j}{dydot(i)/dy(j)}, |
| 77 |
or a string giving the name of a function or |
| 78 |
subroutine in \file{dllname} that computes the column of the |
| 79 |
Jacobian (see vignette \code{"compiledCode"} for more about this option). |
| 80 |
|
| 81 |
The \R |
| 82 |
calling sequence for \code{jacvec} is identical to that of |
| 83 |
\code{func}, but with extra parameter \code{j}, denoting the column |
| 84 |
number. Thus, \code{jacvec} should be called as: \code{jacvec = |
| 85 |
func(t, y, j, parms)} and \code{jacvec} should return a vector |
| 86 |
containing column \code{j} of the Jacobian, i.e. its i-th value is |
| 87 |
\eqn{\partial\dot{y}_i/\partial y_j}{dydot(i)/dy(j)}. |
| 88 |
If this function is absent, \code{lsodes} will |
| 89 |
generate the Jacobian by differences. |
| 90 |
} |
| 91 |
\item{sparsetype }{the sparsity structure of the Jacobian, one of |
| 92 |
"sparseint" or "sparseusr", "sparsejan", ..., |
| 93 |
The sparsity can be estimated internally by lsodes (first option) |
| 94 |
or given by the user (last two). See details. |
| 95 |
} |
| 96 |
\item{nnz }{the number of nonzero elements in the sparse Jacobian (if |
| 97 |
this is unknown, use an estimate). |
| 98 |
} |
| 99 |
\item{inz }{if \code{sparsetype} equal to "sparseusr", a two-columned matrix |
| 100 |
with the (row, column) indices to the nonzero elements in the sparse |
| 101 |
Jacobian. If \code{sparsetype} = "sparsejan", a vector with the elements |
| 102 |
ian followed by he elements jan as used in the lsodes code. See details. |
| 103 |
In all other cases, ignored. |
| 104 |
} |
| 105 |
\item{rootfunc }{if not \code{NULL}, an \R function that computes the |
| 106 |
function whose root has to be estimated or a string giving the name |
| 107 |
of a function or subroutine in \file{dllname} that computes the root |
| 108 |
function. The \R calling sequence for \code{rootfunc} is identical |
| 109 |
to that of \code{func}. \code{rootfunc} should return a vector with |
| 110 |
the function values whose root is sought. |
| 111 |
} |
| 112 |
\item{verbose }{if \code{TRUE}: full output to the screen, e.g. will |
| 113 |
print the \code{diagnostiscs} of the integration - see details. |
| 114 |
} |
| 115 |
\item{nroot }{only used if \file{dllname} is specified: the number of |
| 116 |
constraint functions whose roots are desired during the integration; |
| 117 |
if \code{rootfunc} is an R-function, the solver estimates the number |
| 118 |
of roots. |
| 119 |
} |
| 120 |
\item{tcrit }{if not \code{NULL}, then \code{lsodes} cannot integrate |
| 121 |
past \code{tcrit}. The FORTRAN routine \code{lsodes} overshoots its |
| 122 |
targets (times points in the vector \code{times}), and interpolates |
| 123 |
values for the desired time points. If there is a time beyond which |
| 124 |
integration should not proceed (perhaps because of a singularity), |
| 125 |
that should be provided in \code{tcrit}. |
| 126 |
} |
| 127 |
\item{hmin }{an optional minimum value of the integration stepsize. In |
| 128 |
special situations this parameter may speed up computations with the |
| 129 |
cost of precision. Don't use \code{hmin} if you don't know why! |
| 130 |
} |
| 131 |
\item{hmax }{an optional maximum value of the integration stepsize. If |
| 132 |
not specified, \code{hmax} is set to the largest difference in |
| 133 |
\code{times}, to avoid that the simulation possibly ignores |
| 134 |
short-term events. If 0, no maximal size is specified. |
| 135 |
} |
| 136 |
\item{hini }{initial step size to be attempted; if 0, the initial step |
| 137 |
size is determined by the solver. |
| 138 |
} |
| 139 |
\item{ynames }{logical, if \code{FALSE} names of state variables are |
| 140 |
not passed to function \code{func}; this may speed up the simulation |
| 141 |
especially for multi-D models. |
| 142 |
} |
| 143 |
\item{maxord }{the maximum order to be allowed. \code{NULL} uses the |
| 144 |
default, i.e. order 12 if implicit Adams method (meth = 1), order 5 |
| 145 |
if BDF method (meth = 2). Reduce maxord to save storage space. |
| 146 |
} |
| 147 |
\item{maxsteps }{maximal number of steps per output interval taken by the |
| 148 |
solver. |
| 149 |
} |
| 150 |
\item{lrw }{the length of the real work array rwork; due to the |
| 151 |
sparsicity, this cannot be readily predicted. If \code{NULL}, a |
| 152 |
guess will be made, and if not sufficient, \code{lsodes} will return |
| 153 |
with a message indicating the size of rwork actually required. |
| 154 |
Therefore, some experimentation may be necessary to estimate the |
| 155 |
value of \code{lrw}. |
| 156 |
|
| 157 |
For instance, if you get the error: |
| 158 |
\preformatted{ |
| 159 |
DLSODES- RWORK length is insufficient to proceed. |
| 160 |
Length needed is .ge. LENRW (=I1), exceeds LRW (=I2) |
| 161 |
In above message, I1 = 27627 I2 = 25932 |
| 162 |
} |
| 163 |
set \code{lrw} equal to 27627 or a higher value |
| 164 |
|
| 165 |
} |
| 166 |
\item{liw }{the length of the integer work array iwork; due to the |
| 167 |
sparsicity, this cannot be readily predicted. If \code{NULL}, a guess will |
| 168 |
be made, and if not sufficient, \code{lsodes} will return with a |
| 169 |
message indicating the size of iwork actually required. Therefore, |
| 170 |
some experimentation may be necessary to estimate the value of |
| 171 |
\code{liw}. |
| 172 |
} |
| 173 |
\item{dllname }{a string giving the name of the shared library |
| 174 |
(without extension) that contains all the compiled function or |
| 175 |
subroutine definitions refered to in \code{func} and |
| 176 |
\code{jacfunc}. See package vignette \code{"compiledCode"}. |
| 177 |
} |
| 178 |
\item{initfunc }{if not \code{NULL}, the name of the initialisation function |
| 179 |
(which initialises values of parameters), as provided in |
| 180 |
\file{dllname}. See package vignette \code{"compiledCode"}. |
| 181 |
} |
| 182 |
\item{initpar }{only when \file{dllname} is specified and an |
| 183 |
initialisation function \code{initfunc} is in the dll: the |
| 184 |
parameters passed to the initialiser, to initialise the common |
| 185 |
blocks (FORTRAN) or global variables (C, C++). |
| 186 |
} |
| 187 |
\item{rpar }{only when \file{dllname} is specified: a vector with |
| 188 |
double precision values passed to the dll-functions whose names are |
| 189 |
specified by \code{func} and \code{jacfunc}. |
| 190 |
} |
| 191 |
\item{ipar }{only when \file{dllname} is specified: a vector with |
| 192 |
integer values passed to the dll-functions whose names are specified |
| 193 |
by \code{func} and \code{jacfunc}. |
| 194 |
} |
| 195 |
\item{nout }{only used if \code{dllname} is specified and the model is |
| 196 |
defined in compiled code: the number of output variables calculated |
| 197 |
in the compiled function \code{func}, present in the shared |
| 198 |
library. Note: it is not automatically checked whether this is |
| 199 |
indeed the number of output variables calculated in the dll - you have |
| 200 |
to perform this check in the code. See package vignette |
| 201 |
\code{"compiledCode"}. |
| 202 |
} |
| 203 |
\item{outnames }{only used if \file{dllname} is specified and |
| 204 |
\code{nout} > 0: the names of output variables calculated in the |
| 205 |
compiled function \code{func}, present in the shared library. |
| 206 |
These names will be used to label the output matrix. |
| 207 |
} |
| 208 |
\item{forcings }{only used if \file{dllname} is specified: a list with |
| 209 |
the forcing function data sets, each present as a two-columned matrix, |
| 210 |
with (time,value); interpolation outside the interval |
| 211 |
[min(\code{times}), max(\code{times})] is done by taking the value at |
| 212 |
the closest data extreme. |
| 213 |
|
| 214 |
See \link{forcings} or package vignette \code{"compiledCode"}. |
| 215 |
} |
| 216 |
\item{initforc }{if not \code{NULL}, the name of the forcing function |
| 217 |
initialisation function, as provided in |
| 218 |
\file{dllname}. It MUST be present if \code{forcings} has been given a |
| 219 |
value. |
| 220 |
See \link{forcings} or package vignette \code{"compiledCode"}. |
| 221 |
} |
| 222 |
\item{fcontrol }{A list of control parameters for the forcing functions. |
| 223 |
See \link{forcings} or vignette \code{compiledCode}. |
| 224 |
} |
| 225 |
\item{events }{A matrix or data frame that specifies events, i.e. when the value of a |
| 226 |
state variable is suddenly changed. See \link{events} for more information. |
| 227 |
} |
| 228 |
\item{lags }{A list that specifies timelags, i.e. the number of steps |
| 229 |
that has to be kept. To be used for delay differential equations. |
| 230 |
See \link{timelags}, \link{dede} for more information. |
| 231 |
} |
| 232 |
\item{... }{additional arguments passed to \code{func} and |
| 233 |
\code{jacfunc} allowing this to be a generic function. |
| 234 |
} |
| 235 |
} |
| 236 |
\value{ |
| 237 |
A matrix of class \code{deSolve} with up to as many rows as elements |
| 238 |
in \code{times} and as many columns as elements in \code{y} plus the number of "global" |
| 239 |
values returned in the next elements of the return from \code{func}, |
| 240 |
plus and additional column for the time value. There will be a row |
| 241 |
for each element in \code{times} unless the FORTRAN routine `lsodes' |
| 242 |
returns with an unrecoverable error. If \code{y} has a names |
| 243 |
attribute, it will be used to label the columns of the output value. |
| 244 |
} |
| 245 |
\author{Karline Soetaert <[email protected]>} |
| 246 |
\examples{ |
| 247 |
## Various ways to solve the same model. |
| 248 |
|
| 249 |
## ======================================================================= |
| 250 |
## The example from lsodes source code |
| 251 |
## A chemical model |
| 252 |
## ======================================================================= |
| 253 |
|
| 254 |
n <- 12 |
| 255 |
y <- rep(1, n) |
| 256 |
dy <- rep(0, n) |
| 257 |
|
| 258 |
times <- c(0, 0.1*(10^(0:4))) |
| 259 |
|
| 260 |
rtol <- 1.0e-4 |
| 261 |
atol <- 1.0e-6 |
| 262 |
|
| 263 |
parms <- c(rk1 = 0.1, rk2 = 10.0, rk3 = 50.0, rk4 = 2.5, rk5 = 0.1, |
| 264 |
rk6 = 10.0, rk7 = 50.0, rk8 = 2.5, rk9 = 50.0, rk10 = 5.0, |
| 265 |
rk11 = 50.0, rk12 = 50.0,rk13 = 50.0, rk14 = 30.0, |
| 266 |
rk15 = 100.0,rk16 = 2.5, rk17 = 100.0,rk18 = 2.5, |
| 267 |
rk19 = 50.0, rk20 = 50.0) |
| 268 |
|
| 269 |
# |
| 270 |
chemistry <- function (time, Y, pars) { |
| 271 |
with (as.list(pars), { |
| 272 |
dy[1] <- -rk1 *Y[1] |
| 273 |
dy[2] <- rk1 *Y[1] + rk11*rk14*Y[4] + rk19*rk14*Y[5] - |
| 274 |
rk3 *Y[2]*Y[3] - rk15*Y[2]*Y[12] - rk2*Y[2] |
| 275 |
dy[3] <- rk2 *Y[2] - rk5 *Y[3] - rk3*Y[2]*Y[3] - |
| 276 |
rk7*Y[10]*Y[3] + rk11*rk14*Y[4] + rk12*rk14*Y[6] |
| 277 |
dy[4] <- rk3 *Y[2]*Y[3] - rk11*rk14*Y[4] - rk4*Y[4] |
| 278 |
dy[5] <- rk15*Y[2]*Y[12] - rk19*rk14*Y[5] - rk16*Y[5] |
| 279 |
dy[6] <- rk7 *Y[10]*Y[3] - rk12*rk14*Y[6] - rk8*Y[6] |
| 280 |
dy[7] <- rk17*Y[10]*Y[12] - rk20*rk14*Y[7] - rk18*Y[7] |
| 281 |
dy[8] <- rk9 *Y[10] - rk13*rk14*Y[8] - rk10*Y[8] |
| 282 |
dy[9] <- rk4 *Y[4] + rk16*Y[5] + rk8*Y[6] + |
| 283 |
rk18*Y[7] |
| 284 |
dy[10] <- rk5 *Y[3] + rk12*rk14*Y[6] + rk20*rk14*Y[7] + |
| 285 |
rk13*rk14*Y[8] - rk7 *Y[10]*Y[3] - rk17*Y[10]*Y[12] - |
| 286 |
rk6 *Y[10] - rk9*Y[10] |
| 287 |
dy[11] <- rk10*Y[8] |
| 288 |
dy[12] <- rk6 *Y[10] + rk19*rk14*Y[5] + rk20*rk14*Y[7] - |
| 289 |
rk15*Y[2]*Y[12] - rk17*Y[10]*Y[12] |
| 290 |
return(list(dy)) |
| 291 |
}) |
| 292 |
} |
| 293 |
|
| 294 |
## ======================================================================= |
| 295 |
## application 1. lsodes estimates the structure of the Jacobian |
| 296 |
## and calculates the Jacobian by differences |
| 297 |
## ======================================================================= |
| 298 |
out <- lsodes(func = chemistry, y = y, parms = parms, times = times, |
| 299 |
atol = atol, rtol = rtol, verbose = TRUE) |
| 300 |
|
| 301 |
## ======================================================================= |
| 302 |
## application 2. the structure of the Jacobian is input |
| 303 |
## lsodes calculates the Jacobian by differences |
| 304 |
## this is not so efficient... |
| 305 |
## ======================================================================= |
| 306 |
|
| 307 |
## elements of Jacobian that are not zero |
| 308 |
nonzero <- matrix(nc = 2, byrow = TRUE, data = c( |
| 309 |
1, 1, 2, 1, # influence of sp1 on rate of change of others |
| 310 |
2, 2, 3, 2, 4, 2, 5, 2, 12, 2, |
| 311 |
2, 3, 3, 3, 4, 3, 6, 3, 10, 3, |
| 312 |
2, 4, 3, 4, 4, 4, 9, 4, # d (dyi)/dy4 |
| 313 |
2, 5, 5, 5, 9, 5, 12, 5, |
| 314 |
3, 6, 6, 6, 9, 6, 10, 6, |
| 315 |
7, 7, 9, 7, 10, 7, 12, 7, |
| 316 |
8, 8, 10, 8, 11, 8, |
| 317 |
3,10, 6,10, 7,10, 8,10, 10,10, 12,10, |
| 318 |
2,12, 5,12, 7,12, 10,12, 12,12) |
| 319 |
) |
| 320 |
|
| 321 |
## when run, the default length of rwork is too small |
| 322 |
## lsodes will tell the length actually needed |
| 323 |
# out2 <- lsodes(func = chemistry, y = y, parms = parms, times = times, |
| 324 |
# inz = nonzero, atol = atol,rtol = rtol) #gives warning |
| 325 |
out2 <- lsodes(func = chemistry, y = y, parms = parms, times = times, |
| 326 |
sparsetype = "sparseusr", inz = nonzero, |
| 327 |
atol = atol, rtol = rtol, verbose = TRUE, lrw = 353) |
| 328 |
|
| 329 |
## ======================================================================= |
| 330 |
## application 3. lsodes estimates the structure of the Jacobian |
| 331 |
## the Jacobian (vector) function is input |
| 332 |
## ======================================================================= |
| 333 |
chemjac <- function (time, Y, j, pars) { |
| 334 |
with (as.list(pars), { |
| 335 |
PDJ <- rep(0,n) |
| 336 |
|
| 337 |
if (j == 1){ |
| 338 |
PDJ[1] <- -rk1 |
| 339 |
PDJ[2] <- rk1 |
| 340 |
} else if (j == 2) { |
| 341 |
PDJ[2] <- -rk3*Y[3] - rk15*Y[12] - rk2 |
| 342 |
PDJ[3] <- rk2 - rk3*Y[3] |
| 343 |
PDJ[4] <- rk3*Y[3] |
| 344 |
PDJ[5] <- rk15*Y[12] |
| 345 |
PDJ[12] <- -rk15*Y[12] |
| 346 |
} else if (j == 3) { |
| 347 |
PDJ[2] <- -rk3*Y[2] |
| 348 |
PDJ[3] <- -rk5 - rk3*Y[2] - rk7*Y[10] |
| 349 |
PDJ[4] <- rk3*Y[2] |
| 350 |
PDJ[6] <- rk7*Y[10] |
| 351 |
PDJ[10] <- rk5 - rk7*Y[10] |
| 352 |
} else if (j == 4) { |
| 353 |
PDJ[2] <- rk11*rk14 |
| 354 |
PDJ[3] <- rk11*rk14 |
| 355 |
PDJ[4] <- -rk11*rk14 - rk4 |
| 356 |
PDJ[9] <- rk4 |
| 357 |
} else if (j == 5) { |
| 358 |
PDJ[2] <- rk19*rk14 |
| 359 |
PDJ[5] <- -rk19*rk14 - rk16 |
| 360 |
PDJ[9] <- rk16 |
| 361 |
PDJ[12] <- rk19*rk14 |
| 362 |
} else if (j == 6) { |
| 363 |
PDJ[3] <- rk12*rk14 |
| 364 |
PDJ[6] <- -rk12*rk14 - rk8 |
| 365 |
PDJ[9] <- rk8 |
| 366 |
PDJ[10] <- rk12*rk14 |
| 367 |
} else if (j == 7) { |
| 368 |
PDJ[7] <- -rk20*rk14 - rk18 |
| 369 |
PDJ[9] <- rk18 |
| 370 |
PDJ[10] <- rk20*rk14 |
| 371 |
PDJ[12] <- rk20*rk14 |
| 372 |
} else if (j == 8) { |
| 373 |
PDJ[8] <- -rk13*rk14 - rk10 |
| 374 |
PDJ[10] <- rk13*rk14 |
| 375 |
PDJ[11] <- rk10 |
| 376 |
} else if (j == 10) { |
| 377 |
PDJ[3] <- -rk7*Y[3] |
| 378 |
PDJ[6] <- rk7*Y[3] |
| 379 |
PDJ[7] <- rk17*Y[12] |
| 380 |
PDJ[8] <- rk9 |
| 381 |
PDJ[10] <- -rk7*Y[3] - rk17*Y[12] - rk6 - rk9 |
| 382 |
PDJ[12] <- rk6 - rk17*Y[12] |
| 383 |
} else if (j == 12) { |
| 384 |
PDJ[2] <- -rk15*Y[2] |
| 385 |
PDJ[5] <- rk15*Y[2] |
| 386 |
PDJ[7] <- rk17*Y[10] |
| 387 |
PDJ[10] <- -rk17*Y[10] |
| 388 |
PDJ[12] <- -rk15*Y[2] - rk17*Y[10] |
| 389 |
} |
| 390 |
return(PDJ) |
| 391 |
}) |
| 392 |
} |
| 393 |
|
| 394 |
out3 <- lsodes(func = chemistry, y = y, parms = parms, times = times, |
| 395 |
jacvec = chemjac, atol = atol, rtol = rtol) |
| 396 |
|
| 397 |
## ======================================================================= |
| 398 |
## application 4. The structure of the Jacobian (nonzero elements) AND |
| 399 |
## the Jacobian (vector) function is input |
| 400 |
## ======================================================================= |
| 401 |
out4 <- lsodes(func = chemistry, y = y, parms = parms, times = times, |
| 402 |
lrw = 351, sparsetype = "sparseusr", inz = nonzero, |
| 403 |
jacvec = chemjac, atol = atol, rtol = rtol, |
| 404 |
verbose = TRUE) |
| 405 |
|
| 406 |
# The sparsejan variant |
| 407 |
# note: errors in inz may cause R to break, so this is not without danger... |
| 408 |
# out5 <- lsodes(func = chemistry, y = y, parms = parms, times = times, |
| 409 |
# jacvec = chemjac, atol = atol, rtol = rtol, sparsetype = "sparsejan", |
| 410 |
# inz = c(1,3,8,13,17,21,25,29,32,32,38,38,43, # ian |
| 411 |
# 1,2, 2,3,4,5,12, 2,3,4,6,10, 2,3,4,9, 2,5,9,12, 3,6,9,10, # jan |
| 412 |
# 7,9,10,12, 8,10,11, 3,6,7,8,10,12, 2,5,7,10,12), lrw = 343) |
| 413 |
|
| 414 |
} |
| 415 |
\references{ |
| 416 |
Alan C. Hindmarsh, ODEPACK, A Systematized Collection of ODE Solvers, |
| 417 |
in Scientific Computing, R. S. Stepleman et al. (Eds.), North-Holland, |
| 418 |
Amsterdam, 1983, pp. 55-64. |
| 419 |
|
| 420 |
S. C. Eisenstat, M. C. Gursky, M. H. Schultz, and A. H. Sherman, Yale |
| 421 |
Sparse Matrix Package: I. The Symmetric Codes, |
| 422 |
Int. J. Num. Meth. Eng., 18 (1982), pp. 1145-1151. |
| 423 |
|
| 424 |
S. C. Eisenstat, M. C. Gursky, M. H. Schultz, and A. H. Sherman, Yale |
| 425 |
Sparse Matrix Package: II. The Nonsymmetric Codes, Research Report |
| 426 |
No. 114, Dept. of Computer Sciences, Yale University, 1977. |
| 427 |
} |
| 428 |
\details{ |
| 429 |
|
| 430 |
The work is done by the FORTRAN subroutine \code{lsodes}, whose |
| 431 |
documentation should be consulted for details (it is included as |
| 432 |
comments in the source file \file{src/opkdmain.f}). The implementation |
| 433 |
is based on the November, 2003 version of lsodes, from Netlib. |
| 434 |
|
| 435 |
\code{lsodes} is applied for stiff problems, where the Jacobian has a |
| 436 |
sparse structure. |
| 437 |
|
| 438 |
There are several choices depending on whether \code{jacvec} |
| 439 |
is specified and depending on the setting of \code{sparsetype}. |
| 440 |
|
| 441 |
If function \code{jacvec} is present, then it should return the j-th |
| 442 |
column of the Jacobian matrix. |
| 443 |
|
| 444 |
There are also several choices for the sparsity specification, selected by |
| 445 |
argument \code{sparsetype}. |
| 446 |
\itemize{ |
| 447 |
\item \code{sparsetype} = \code{"sparseint"}. The sparsity is estimated |
| 448 |
by the solver, based on numerical differences. |
| 449 |
In this case, it is advisable to provide an estimate of the number |
| 450 |
of non-zero elements in the Jacobian (\code{nnz}). |
| 451 |
This value can be approximate; upon return the number of nonzero |
| 452 |
elements actually required will be known (1st element of attribute |
| 453 |
\code{dims}). |
| 454 |
In this case, \code{inz} need not be specified. |
| 455 |
\item \code{sparsetype} = \code{"sparseusr"}. The sparsity is determined by |
| 456 |
the user. In this case, \code{inz} should be a \code{matrix}, containing indices |
| 457 |
(row, column) to the nonzero elements in the Jacobian matrix. |
| 458 |
The number of nonzeros \code{nnz} will be set equal to the number of rows |
| 459 |
in \code{inz}. |
| 460 |
\item \code{sparsetype} = \code{"sparsejan"}. The sparsity is also determined by |
| 461 |
the user. |
| 462 |
In this case, \code{inz} should be a \code{vector}, containting the \code{ian} and |
| 463 |
\code{jan} elements of the sparse storage format, as used in the sparse solver. |
| 464 |
Elements of \code{ian} should be the first \code{n+1} elements of this vector, and |
| 465 |
contain the starting locations in \code{jan} of columns 1.. n. |
| 466 |
\code{jan} contains the row indices of the nonzero locations of |
| 467 |
the Jacobian, reading in columnwise order. |
| 468 |
The number of nonzeros \code{nnz} will be set equal to the length of \code{inz} - (n+1). |
| 469 |
\item \code{sparsetype} = \code{"1D"}, \code{"2D"}, \code{"3D"}. |
| 470 |
The sparsity is estimated by the solver, based on numerical differences. |
| 471 |
Assumes finite differences in a 1D, 2D or 3D regular grid - used by |
| 472 |
functions \code{ode.1D}, \code{ode.2D}, \code{ode.3D}. |
| 473 |
Similar are \code{"2Dmap"}, and \code{"3Dmap"}, which also include a |
| 474 |
mapping variable (passed in nnz). |
| 475 |
} |
| 476 |
|
| 477 |
The input parameters \code{rtol}, and \code{atol} determine the |
| 478 |
\bold{error control} performed by the solver. See \code{\link{lsoda}} |
| 479 |
for details. |
| 480 |
|
| 481 |
The diagnostics of the integration can be printed to screen |
| 482 |
by calling \code{\link{diagnostics}}. If \code{verbose} = \code{TRUE}, |
| 483 |
the diagnostics will written to the screen at the end of the integration. |
| 484 |
|
| 485 |
See vignette("deSolve") for an explanation of each element in the vectors |
| 486 |
containing the diagnostic properties and how to directly access them. |
| 487 |
|
| 488 |
\bold{Models} may be defined in compiled C or FORTRAN code, as well as |
| 489 |
in an R-function. See package vignette \code{"compiledCode"} for details. |
| 490 |
|
| 491 |
More information about models defined in compiled code is in the package |
| 492 |
vignette ("compiledCode"); information about linking forcing functions |
| 493 |
to compiled code is in \link{forcings}. |
| 494 |
|
| 495 |
Examples in both C and FORTRAN are in the \file{doc/examples/dynload} subdirectory |
| 496 |
of the \code{deSolve} package directory. |
| 497 |
|
| 498 |
\code{lsodes} can find the root of at least one of a set of constraint functions |
| 499 |
\code{rootfunc} of the independent and dependent variables. It then returns the |
| 500 |
solution at the root if that occurs sooner than the specified stop |
| 501 |
condition, and otherwise returns the solution according the specified |
| 502 |
stop condition. |
| 503 |
|
| 504 |
Caution: Because of numerical errors in the function |
| 505 |
\code{rootfun} due to roundoff and integration error, \code{lsodes} may |
| 506 |
return false roots, or return the same root at two or more |
| 507 |
nearly equal values of \code{time}. |
| 508 |
|
| 509 |
} |
| 510 |
\seealso{ |
| 511 |
\itemize{ |
| 512 |
\item \code{\link{rk}}, \item \code{\link{rk4}} and \code{\link{euler}} for |
| 513 |
Runge-Kutta integrators. |
| 514 |
\item \code{\link{lsoda}}, \code{\link{lsode}}, |
| 515 |
\code{\link{lsodar}}, \code{\link{vode}}, |
| 516 |
\code{\link{daspk}} for other solvers of the Livermore family, |
| 517 |
\item \code{\link{ode}} for a general interface to most of the ODE solvers, |
| 518 |
\item \code{\link{ode.band}} for solving models with a banded |
| 519 |
Jacobian, |
| 520 |
\item \code{\link{ode.1D}} for integrating 1-D models, |
| 521 |
\item \code{\link{ode.2D}} for integrating 2-D models, |
| 522 |
\item \code{\link{ode.3D}} for integrating 3-D models, |
| 523 |
} |
| 524 |
|
| 525 |
\code{\link{diagnostics}} to print diagnostic messages. |
| 526 |
} |
| 527 |
\keyword{math} |
| 528 |
|