Debian Codegraph @

man/radau.Rd

raw
1 \name{radau}
2 \alias{radau}
3
4 \title{Implicit Runge-Kutta RADAU IIA}
5
6 \description{
7 Solves the initial value problem for stiff or nonstiff systems of
8 ordinary differential equations (ODE) in the form: \deqn{dy/dt =
9 f(t,y)} or linearly implicit differential algebraic equations in the
10 form: \deqn{M dy/dt = f(t,y)}.
11
12 The \R function \code{radau} provides an interface to the Fortran solver
13 RADAU5, written by Ernst Hairer and G. Wanner, which implements the 3-stage
14 RADAU IIA method.
15 It implements the implicit Runge-Kutta method of order 5 with step size
16 control and continuous output.
17 The system of ODEs or DAEs is written as an \R function or can be defined in
18 compiled code that has been dynamically loaded.
19 }
20 \usage{
21 radau(y, times, func, parms, nind = c(length(y), 0, 0),
22 rtol = 1e-6, atol = 1e-6, jacfunc = NULL, jactype = "fullint",
23 mass = NULL, massup = NULL, massdown = NULL, rootfunc = NULL,
24 verbose = FALSE, nroot = 0, hmax = NULL, hini = 0, ynames = TRUE,
25 bandup = NULL, banddown = NULL, maxsteps = 5000,
26 dllname = NULL, initfunc = dllname, initpar = parms,
27 rpar = NULL, ipar = NULL, nout = 0, outnames = NULL,
28 forcings = NULL, initforc = NULL, fcontrol = NULL,
29 events=NULL, lags = NULL, ...)
30 }
31
32 \arguments{
33 \item{y }{the initial (state) values for the ODE system. If \code{y}
34 has a name attribute, the names will be used to label the output
35 matrix.
36 }
37 \item{times }{time sequence for which output is wanted; the first
38 value of \code{times} must be the initial time; if only one step is
39 to be taken; set \code{times} = \code{NULL}.
40 }
41 \item{func }{either an \R-function that computes the values of the
42 derivatives in the ODE system (the \emph{model definition}) at time
43 t, or the right-hand side of the equation \deqn{M dy/dt = f(t,y)}
44 if a DAE. (if \code{mass} is supplied then the problem is assumed a DAE).
45
46 \code{func} can also be a character string giving the name of a compiled
47 function in a dynamically loaded shared library.
48
49 If \code{func} is an \R-function, it must be defined as:
50
51 \code{func <- function(t, y, parms,...)}.
52
53 \code{t} is the current time
54 point in the integration, \code{y} is the current estimate of the
55 variables in the ODE system. If the initial values \code{y} has a
56 \code{names} attribute, the names will be available inside \code{func}.
57 \code{parms} is a vector or list of parameters; ... (optional) are
58 any other arguments passed to the function.
59
60 The return value of \code{func} should be a list, whose first
61 element is a vector containing the derivatives of \code{y} with
62 respect to \code{time}, and whose next elements are global values
63 that are required at each point in \code{times}. The derivatives
64 must be specified in the \bold{same order} as the state variables \code{y}.
65
66 If \code{func} is
67 a string, then \code{dllname} must give the name of the shared
68 library (without extension) which must be loaded before
69 \code{radau()} is called. See deSolve package vignette \code{"compiledCode"}
70 for more details.
71 }
72 \item{parms }{vector or list of parameters used in \code{func} or
73 \code{jacfunc}.
74 }
75 \item{nind }{if a DAE system: a three-valued vector with the number of
76 variables of index 1, 2, 3 respectively.
77 The equations must be defined such that the index 1 variables precede
78 the index 2 variables which in turn precede the index 3 variables.
79 The sum of the variables of different index should equal N,
80 the total number of variables.
81 This has implications on the scaling of the variables,
82 i.e. index 2 variables are scaled by 1/h,
83 index 3 variables are scaled by 1/h^2.
84 }
85 \item{rtol }{relative error tolerance, either a
86 scalar or an array as long as \code{y}. See details.
87 }
88 \item{atol }{absolute error tolerance, either a scalar or an array as
89 long as \code{y}. See details.
90 }
91 \item{jacfunc }{if not \code{NULL}, an \R function that computes the
92 Jacobian of the system of differential equations
93 \eqn{\partial\dot{y}_i/\partial y_j}{dydot(i)/dy(j)}, or
94 a string giving the name of a function or subroutine in
95 \file{dllname} that computes the Jacobian (see vignette
96 \code{"compiledCode"} from package deSolve, for more about this option).
97
98 In some circumstances, supplying
99 \code{jacfunc} can speed up the computations, if the system is
100 stiff. The \R calling sequence for \code{jacfunc} is identical to
101 that of \code{func}.
102
103 If the Jacobian is a full matrix,
104 \code{jacfunc} should return a matrix
105 \eqn{\partial\dot{y}/\partial y}{dydot/dy}, where the ith row
106 contains the derivative of \eqn{dy_i/dt} with respect to \eqn{y_j},
107 or a vector containing the matrix elements by columns (the way \R
108 and FORTRAN store matrices). \cr If the Jacobian is banded,
109 \code{jacfunc} should return a matrix containing only the nonzero
110 bands of the Jacobian, rotated row-wise. See example.
111 }
112 \item{jactype }{the structure of the Jacobian, one of
113 \code{"fullint"}, \code{"fullusr"}, \code{"bandusr"} or
114 \code{"bandint"} - either full or banded and estimated internally or
115 by user.
116 }
117 \item{mass }{the mass matrix.
118 If not \code{NULL}, the problem is a linearly
119 implicit DAE and defined as \eqn{M\, dy/dt = f(t,y)}{M dy/dt = f(t,y)}.
120 If the mass-matrix \eqn{M} is full, it should be of dimension
121 \eqn{n^2}{n*n} where \eqn{n} is the number of \eqn{y}-values;
122 if banded the number of rows should be less than \eqn{n},
123 and the mass-matrix is stored diagonal-wise with element \eqn{(i, j)}
124 stored in \code{mass(i - j + mumas + 1, j)}.
125
126 If \code{mass = NULL} then the model is an ODE (default)
127 }
128 \item{massup }{number of non-zero bands above the diagonal of the \code{mass}
129 matrix, in case it is banded.
130 }
131 \item{massdown }{number of non-zero bands below the diagonal of the \code{mass}
132 matrix, in case it is banded.
133 }
134 \item{rootfunc }{if not \code{NULL}, an \R function that computes the
135 function whose root has to be estimated or a string giving the name
136 of a function or subroutine in \file{dllname} that computes the root
137 function. The \R calling sequence for \code{rootfunc} is identical
138 to that of \code{func}. \code{rootfunc} should return a vector with
139 the function values whose root is sought.
140 }
141 \item{verbose }{if \code{TRUE}: full output to the screen, e.g. will
142 print the \code{diagnostiscs} of the integration - see details.
143 }
144 \item{nroot }{only used if \file{dllname} is specified: the number of
145 constraint functions whose roots are desired during the integration;
146 if \code{rootfunc} is an R-function, the solver estimates the number
147 of roots.
148 }
149 \item{hmax }{an optional maximum value of the integration stepsize. If
150 not specified, \code{hmax} is set to the largest difference in
151 \code{times}, to avoid that the simulation possibly ignores
152 short-term events. If 0, no maximal size is specified.
153 }
154 \item{hini }{initial step size to be attempted; if 0, the initial step
155 size is set equal to 1e-6. Usually 1e-3 to 1e-5 is good for stiff equations
156 }
157 \item{ynames }{logical, if \code{FALSE} names of state variables are not
158 passed to function \code{func}; this may speed up the simulation especially
159 for multi-D models.
160 }
161 \item{bandup }{number of non-zero bands above the diagonal, in case
162 the Jacobian is banded.
163 }
164 \item{banddown }{number of non-zero bands below the diagonal, in case
165 the Jacobian is banded.
166 }
167 \item{maxsteps }{average maximal number of steps per output interval
168 taken by the solver. This argument is defined such as to ensure
169 compatibility with the Livermore-solvers. RADAU only accepts the maximal
170 number of steps for the entire integration, and this is calculated
171 as \code{length(times) * maxsteps}.
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 vignette \code{"compiledCode"}
177 from package \code{deSolve}.
178 }
179 \item{initfunc }{if not \code{NULL}, the name of the initialisation function
180 (which initialises values of parameters), as provided in
181 \file{dllname}. See vignette \code{"compiledCode"}
182 from package \code{deSolve}.
183 }
184 \item{initpar }{only when \file{dllname} is specified and an
185 initialisation function \code{initfunc} is in the dll: the
186 parameters passed to the initialiser, to initialise the common
187 blocks (FORTRAN) or global variables (C, C++).
188 }
189 \item{rpar }{only when \file{dllname} is specified: a vector with
190 double precision values passed to the dll-functions whose names are
191 specified by \code{func} and \code{jacfunc}.
192 }
193 \item{ipar }{only when \file{dllname} is specified: a vector with
194 integer values passed to the dll-functions whose names are specified
195 by \code{func} and \code{jacfunc}.
196 }
197 \item{nout }{only used if \code{dllname} is specified and the model is
198 defined in compiled code: the number of output variables calculated
199 in the compiled function \code{func}, present in the shared
200 library. Note: it is not automatically checked whether this is
201 indeed the number of output variables calculed in the DLL - you have
202 to perform this check in the code - See vignette \code{"compiledCode"}
203 from package \code{deSolve}.
204 }
205 \item{outnames }{only used if \file{dllname} is specified and
206 \code{nout} > 0: the names of output variables calculated in the
207 compiled function \code{func}, present in the shared library.
208 These names will be used to label the output matrix.
209 }
210 \item{forcings }{only used if \file{dllname} is specified: a list with
211 the forcing function data sets, each present as a two-columned matrix,
212 with (time, value); interpolation outside the interval
213 [min(\code{times}), max(\code{times})] is done by taking the value at
214 the closest data extreme.
215
216 See \link{forcings} or package vignette \code{"compiledCode"}.
217 }
218 \item{initforc }{if not \code{NULL}, the name of the forcing function
219 initialisation function, as provided in
220 \file{dllname}. It MUST be present if \code{forcings} has been given a
221 value.
222 See \link{forcings} or package vignette \code{"compiledCode"}.
223 }
224 \item{fcontrol }{A list of control parameters for the forcing functions.
225 See \link{forcings} or vignette \code{compiledCode}.
226 }
227 \item{events }{A matrix or data frame that specifies events, i.e. when the value of a
228 state variable is suddenly changed. See \link{events} for more information.
229 }
230 \item{lags }{A list that specifies timelags, i.e. the number of steps
231 that has to be kept. To be used for delay differential equations.
232 See \link{timelags}, \link{dede} for more information.
233 }
234 \item{... }{additional arguments passed to \code{func} and
235 \code{jacfunc} allowing this to be a generic function.
236 }
237 }
238 \value{
239 A matrix of class \code{deSolve} with up to as many rows as elements
240 in \code{times} and as many columns as elements in \code{y} plus the number of "global"
241 values returned in the next elements of the return from \code{func},
242 plus and additional column for the time value. There will be a row
243 for each element in \code{times} unless the FORTRAN routine
244 returns with an unrecoverable error. If \code{y} has a names
245 attribute, it will be used to label the columns of the output value.
246
247 }
248 \author{Karline Soetaert
249
250 }
251 \examples{
252 ## =======================================================================
253 ## Example 1: ODE
254 ## Various ways to solve the same model.
255 ## =======================================================================
256
257 ## the model, 5 state variables
258 f1 <- function (t, y, parms) {
259 ydot <- vector(len = 5)
260
261 ydot[1] <- 0.1*y[1] -0.2*y[2]
262 ydot[2] <- -0.3*y[1] +0.1*y[2] -0.2*y[3]
263 ydot[3] <- -0.3*y[2] +0.1*y[3] -0.2*y[4]
264 ydot[4] <- -0.3*y[3] +0.1*y[4] -0.2*y[5]
265 ydot[5] <- -0.3*y[4] +0.1*y[5]
266
267 return(list(ydot))
268 }
269
270 ## the Jacobian, written as a full matrix
271 fulljac <- function (t, y, parms) {
272 jac <- matrix(nrow = 5, ncol = 5, byrow = TRUE,
273 data = c(0.1, -0.2, 0 , 0 , 0 ,
274 -0.3, 0.1, -0.2, 0 , 0 ,
275 0 , -0.3, 0.1, -0.2, 0 ,
276 0 , 0 , -0.3, 0.1, -0.2,
277 0 , 0 , 0 , -0.3, 0.1))
278 return(jac)
279 }
280
281 ## the Jacobian, written in banded form
282 bandjac <- function (t, y, parms) {
283 jac <- matrix(nrow = 3, ncol = 5, byrow = TRUE,
284 data = c( 0 , -0.2, -0.2, -0.2, -0.2,
285 0.1, 0.1, 0.1, 0.1, 0.1,
286 -0.3, -0.3, -0.3, -0.3, 0))
287 return(jac)
288 }
289
290 ## initial conditions and output times
291 yini <- 1:5
292 times <- 1:20
293
294 ## default: stiff method, internally generated, full Jacobian
295 out <- radau(yini, times, f1, parms = 0)
296 plot(out)
297
298 ## stiff method, user-generated full Jacobian
299 out2 <- radau(yini, times, f1, parms = 0, jactype = "fullusr",
300 jacfunc = fulljac)
301
302 ## stiff method, internally-generated banded Jacobian
303 ## one nonzero band above (up) and below(down) the diagonal
304 out3 <- radau(yini, times, f1, parms = 0, jactype = "bandint",
305 bandup = 1, banddown = 1)
306
307 ## stiff method, user-generated banded Jacobian
308 out4 <- radau(yini, times, f1, parms = 0, jactype = "bandusr",
309 jacfunc = bandjac, bandup = 1, banddown = 1)
310
311
312 ## =======================================================================
313 ## Example 2: ODE
314 ## stiff problem from chemical kinetics
315 ## =======================================================================
316 Chemistry <- function (t, y, p) {
317 dy1 <- -.04*y[1] + 1.e4*y[2]*y[3]
318 dy2 <- .04*y[1] - 1.e4*y[2]*y[3] - 3.e7*y[2]^2
319 dy3 <- 3.e7*y[2]^2
320 list(c(dy1, dy2, dy3))
321 }
322
323 times <- 10^(seq(0, 10, by = 0.1))
324 yini <- c(y1 = 1.0, y2 = 0, y3 = 0)
325
326 out <- radau(func = Chemistry, times = times, y = yini, parms = NULL)
327 plot(out, log = "x", type = "l", lwd = 2)
328
329 ## =============================================================================
330 ## Example 3: DAE
331 ## Car axis problem, index 3 DAE, 8 differential, 2 algebraic equations
332 ## from
333 ## F. Mazzia and C. Magherini. Test Set for Initial Value Problem Solvers,
334 ## release 2.4. Department
335 ## of Mathematics, University of Bari and INdAM, Research Unit of Bari,
336 ## February 2008.
337 ## Available from https://archimede.uniba.it/~testset/
338 ## =============================================================================
339
340 ## Problem is written as M*y' = f(t,y,p).
341 ## caraxisfun implements the right-hand side:
342
343 caraxisfun <- function(t, y, parms) {
344 with(as.list(y), {
345
346 yb <- r * sin(w * t)
347 xb <- sqrt(L * L - yb * yb)
348 Ll <- sqrt(xl^2 + yl^2)
349 Lr <- sqrt((xr - xb)^2 + (yr - yb)^2)
350
351 dxl <- ul; dyl <- vl; dxr <- ur; dyr <- vr
352
353 dul <- (L0-Ll) * xl/Ll + 2 * lam2 * (xl-xr) + lam1*xb
354 dvl <- (L0-Ll) * yl/Ll + 2 * lam2 * (yl-yr) + lam1*yb - k * g
355
356 dur <- (L0-Lr) * (xr-xb)/Lr - 2 * lam2 * (xl-xr)
357 dvr <- (L0-Lr) * (yr-yb)/Lr - 2 * lam2 * (yl-yr) - k * g
358
359 c1 <- xb * xl + yb * yl
360 c2 <- (xl - xr)^2 + (yl - yr)^2 - L * L
361
362 list(c(dxl, dyl, dxr, dyr, dul, dvl, dur, dvr, c1, c2))
363 })
364 }
365
366 eps <- 0.01; M <- 10; k <- M * eps^2/2;
367 L <- 1; L0 <- 0.5; r <- 0.1; w <- 10; g <- 1
368
369 yini <- c(xl = 0, yl = L0, xr = L, yr = L0,
370 ul = -L0/L, vl = 0,
371 ur = -L0/L, vr = 0,
372 lam1 = 0, lam2 = 0)
373
374 # the mass matrix
375 Mass <- diag(nrow = 10, 1)
376 Mass[5,5] <- Mass[6,6] <- Mass[7,7] <- Mass[8,8] <- M * eps * eps/2
377 Mass[9,9] <- Mass[10,10] <- 0
378 Mass
379
380 # index of the variables: 4 of index 1, 4 of index 2, 2 of index 3
381 index <- c(4, 4, 2)
382
383 times <- seq(0, 3, by = 0.01)
384 out <- radau(y = yini, mass = Mass, times = times, func = caraxisfun,
385 parms = NULL, nind = index)
386
387 plot(out, which = 1:4, type = "l", lwd = 2)
388 }
389 \references{
390 E. Hairer and G. Wanner, 1996. Solving Ordinary Differential Equations II.
391 Stiff and Differential-algebraic problems. Springer series in computational
392 mathematics 14, Springer-Verlag, second edition.
393 }
394 \details{
395 The work is done by the FORTRAN subroutine \code{RADAU5}, whose
396 documentation should be consulted for details. The implementation
397 is based on the Fortran 77 version from January 18, 2002.
398
399 There are four standard choices for the Jacobian which can be specified with
400 \code{jactype}.
401
402 The options for \bold{jactype} are
403
404 \describe{
405 \item{jactype = "fullint"}{a full Jacobian, calculated internally by
406 the solver.
407 }
408 \item{jactype = "fullusr"}{a full Jacobian, specified by user
409 function \code{jacfunc}.
410 }
411 \item{jactype = "bandusr"}{a banded Jacobian, specified by user
412 function \code{jacfunc}; the size of the bands specified by
413 \code{bandup} and \code{banddown}.
414 }
415 \item{jactype = "bandint"}{a banded Jacobian, calculated by radau;
416 the size of the bands specified by \code{bandup} and
417 \code{banddown}.
418 }
419 }
420
421 Inspection of the example below shows how to specify both a banded and
422 full Jacobian.
423
424 The input parameters \code{rtol}, and \code{atol} determine the
425 \bold{error control} performed by the solver, which roughly keeps the
426 local error of \eqn{y(i)} below \eqn{rtol(i)*abs(y(i))+atol(i)}.
427
428 The diagnostics of the integration can be printed to screen
429 by calling \code{\link{diagnostics}}. If \code{verbose} = \code{TRUE},
430 the diagnostics will be written to the screen at the end of the integration.
431
432 See vignette("deSolve") from the \code{deSolve} package for an
433 explanation of each element in the vectors
434 containing the diagnostic properties and how to directly access them.
435
436 \bold{Models} may be defined in compiled C or FORTRAN code, as well as
437 in an R-function. See package vignette \code{"compiledCode"} from package
438 \code{deSolve} for details.
439
440 Information about linking forcing functions to compiled code is in
441 \link{forcings} (from package \code{deSolve}).
442
443 \code{radau} can find the root of at least one of a set of constraint functions
444 \code{rootfunc} of the independent and dependent variables. It then returns the
445 solution at the root if that occurs sooner than the specified stop
446 condition, and otherwise returns the solution according the specified
447 stop condition.
448
449 Caution: Because of numerical errors in the function
450 \code{rootfun} due to roundoff and integration error, \code{radau} may
451 return false roots, or return the same root at two or more
452 nearly equal values of \code{time}.
453 }
454 \seealso{
455 \itemize{
456 \item \code{\link{ode}} for a general interface to most of the ODE solvers ,
457 \item \code{\link{ode.1D}} for integrating 1-D models,
458 \item \code{\link{ode.2D}} for integrating 2-D models,
459 \item \code{\link{ode.3D}} for integrating 3-D models,
460 \item \code{\link{daspk}} for integrating DAE models up to index 1
461 }
462
463 \code{\link{diagnostics}} to print diagnostic messages.
464 }
465 \keyword{math}
466