Module Odepack
Binding to ODEPACK. This is a collection of solvers for the initial value problem for ordinary differential equation systems. See the ODEPACK page and Netlib.
You can jump to the interface of the Odepack library.
Example of use
To solve the equation ∂ₜ²u = f(t,u) with initial conditions u(t₀) = u₀ and ∂ₜu(t₀) = u'₀, you must first reduce it to a first order ODE: ∂ₜ(y₁,y₂) = (y₂, f(t,y₁)) with the initial condition y(t₀) = (u₀, u'₀). Then write an OCaml function to evaluate the right hand side of this ODE:
let ode t (y: vec) (dy: vec) =
dy.{1} <- y.{2};
dy.{2} <- f t y.{1} and get an approximate value of the vector y(t) with
let init = Array1.of_array float64 fortran_layout [|u₀; u'₀|] in
Odepack.vec(Odepack.lsoda ode init t₀ t)You must explicitly project the return value of Odepack.lsoda with Odepack.vec to get the state of the system because there are several other operations that you can perform on this value (see above). The value of u(t) is the given by the first component of y, so you can define (an approximation of) u with
let u ~u0 ~u'0 t =
let init = Array1.of_array float64 fortran_layout [|u0; u'0|] in
Odepack.vec(Odepack.lsoda ode init t₀ t).{1}- version
- 0.7.1
- author
- Christophe Troestler (Christophe.Troestler@umons.ac.be)
Odepack library
type vec= (float, Stdlib.Bigarray.float64_elt, Stdlib.Bigarray.fortran_layout) Stdlib.Bigarray.Array1.tRepresentation of vectors.
type mat= (float, Stdlib.Bigarray.float64_elt, Stdlib.Bigarray.fortran_layout) Stdlib.Bigarray.Array2.tRepresentation of matrices.
type jacobian=|Auto_fullInternally generated (difference quotient) full Jacobian
|Auto_band of int * intInternally generated (difference quotient) band Jacobian. It takes
(l,u)wherel(resp.u) is the number of lines below (resp. above) the diagonal (excluded).|Full of float -> vec -> mat -> unitFull dfmeans that a functiondfis provided to compute the full Jacobian matrix (∂fᵢ/∂yⱼ) of the vector field f(t,y).df t y jacmust store ∂fᵢ/∂yⱼ(t,y) intojac.{i,j}.|Band of int * int * float -> vec -> int -> mat -> unitBand(l, u, df)means that a functiondfis provided to compute the banded Jacobian matrix withl(resp.u) diagonals below (resp. above) the main one (not counted).df t y d jacmust store ∂fᵢ/∂yⱼ(t,y) intojac.{i-j+d, j}.dis the row ofjaccorresponding to the main diagonal of the Jacobian matrix.Types of Jacobian matrices.
val lsoda : ?rtol:float -> ?rtol_vec:vec -> ?atol:float -> ?atol_vec:vec -> ?jac:jacobian -> ?mxstep:int -> ?copy_y0:bool -> ?debug:bool -> ?debug_switches:bool -> (float -> vec -> vec -> unit) -> vec -> float -> float -> tlsoda f y0 t0 tsolves the ODE dy/dt = F(t,y) with initial condition y(t0) =y0. The execution off t y y'must compute the value of the F(t,y) and store it iny'. It uses a dense or banded Jacobian when the problem is stiff, but it automatically selects between nonstiff (Adams) and stiff (BDF) methods. It uses the nonstiff method initially, and dynamically monitors data in order to decide which method to use.- parameter rtol
relative error tolerance parameter. Default
1e-6.
- parameter rtol_vec
relative error tolerance vector.
- parameter atol
absolute error tolerance parameter. Default
1e-6.
- parameter atol_vec
absolute error tolerance vector.
If
rtol_vec(resp.atol_vec) is specified, it is used in place ofrtol(resp.atol). Specifying onlyrtol(resp.atol) is equivalent to pass a constantrtol_vec(resp.atol_vec). The solver will control the vector E = (E(i)) of estimated local errors iny, according to an inequality of the form max-norm(E(i)/EWT(i)) <= 1, where EWT(i) =rtol_vec.{i} * abs_float(y.{i}) + atol_vec.{i}.
- parameter jac
is an optional Jabobian matrix. If the problem is expected to be stiff much of the time, you are encouraged to supply
jac, for the sake of efficiency. Default:Auto_full.
- parameter mxstep
maximum number of (internally defined) steps allowed during one call to the solver. The default value is 500.
- parameter copy_y0
if
false, the vectory0is MODIFIED to contain the value of the solution at timet. Otherwisey0is unchanged (the current solution vector is then obtained byOdepack.vec). Default:true.
- parameter debug
allows
lsodato print messages. Defaulttrue. The messages contain valuable information, it is not recommended to turn them off.
- parameter debug_switches
prints a message to stdout on each (automatic) method switch (between nonstiff and stiff). Default:
false.
val lsodar : ?rtol:float -> ?rtol_vec:vec -> ?atol:float -> ?atol_vec:vec -> ?jac:jacobian -> ?mxstep:int -> ?copy_y0:bool -> ?debug:bool -> ?debug_switches:bool -> g:(float -> vec -> vec -> unit) -> ng:int -> (float -> vec -> vec -> unit) -> vec -> float -> float -> tlsodar f y0 t0 t ~g ~ngis likelsodabut has root searching capabilities. The algorithm will stop before reaching timetif a root of one of thengconstraints is found. You can determine whether thelsodarstopped at a root usinghas_root. It only finds those roots for which some component ofg, as a function of t, changes sign in the interval of integration. The functiongis evaluated likef, that is:g t y goutmust write togout.{1},..., gout.{ng}the values of thengconstraints.
val time : t -> floatt odereturns the current time at which the solution vector was computed.
val advance : ?time:float -> t -> unitadvance ode ~time:tmodifiesodeso that an approximation of the value of the solution at timestis computed. Note that, if the solver has root searching capabilities and a time is provided, the solver may stop before that time if a root is found. The time is recorded for future calls toadvance ode. If the solver has no root finding capabilities and no time is provided, this function does nothing.
val has_root : t -> boolhas_root odesays wheter the solver stopped (i.e. the current state ofodeis) because a root was found. If the solver has no root searching capabilities, this returnsfalse.
val root : t -> int -> boolroot t ireturns true iff theith constraint inlsodarhas a root. It raisesInvalid_argumentifiis not between 1 andng, the number of constraints (included). This only makes sense ifhas_root tholds.
val roots : t -> bool arrayroots treturns an arrayrsuch thatr.(i)holds if and only if theith constraint has a root.