integrate._ode

First-order ODE integrators.

User-friendly interface to various numerical integrators for solving a system of first order ODEs with prescribed initial conditions:

d y(t)[i]
---------  = f(t,y(t))[i],
   d t

y(t=0)[i] = y0[i],

where:

i = 0, ..., len(y0) - 1

class ode

A generic interface class to numeric integrators. It has the following methods:

integrator = ode(f, jac=None)
integrator = integrator.set_integrator(name, **params)
integrator = integrator.set_initial_value(y0, t0=0.0)
integrator = integrator.set_f_params(*args)
integrator = integrator.set_jac_params(*args)
y1 = integrator.integrate(t1, step=False, relax=False)
flag = integrator.successful()

class complex_ode

This class has the same generic interface as ode, except it can handle complex f, y and Jacobians by transparently translating them into the equivalent real valued system. It supports the real valued solvers (i.e not zvode) and is an alternative to ode with the zvode solver, sometimes performing better.

Module Contents

Classes

ode(self,f,jac=None) A generic interface class to numeric integrators.
complex_ode(self,f,jac=None) A wrapper of ode for complex systems.
IntegratorConcurrencyError(self,name) Failure due to concurrent usage of an integrator that can be used
IntegratorBase()
vode(self,method=”adams”,with_jacobian=False,rtol=1e-06,atol=1e-12,lband=None,uband=None,order=12,nsteps=500,max_step=0.0,min_step=0.0,first_step=0.0)
zvode()
dopri5(self,rtol=1e-06,atol=1e-12,nsteps=500,max_step=0.0,first_step=0.0,safety=0.9,ifactor=10.0,dfactor=0.2,beta=0.0,method=None,verbosity=None)
dop853(self,rtol=1e-06,atol=1e-12,nsteps=500,max_step=0.0,first_step=0.0,safety=0.9,ifactor=6.0,dfactor=0.3,beta=0.0,method=None,verbosity=None)
lsoda(self,with_jacobian=False,rtol=1e-06,atol=1e-12,lband=None,uband=None,nsteps=500,max_step=0.0,min_step=0.0,first_step=0.0,ixpr=0,max_hnil=0,max_order_ns=12,max_order_s=5,method=None)

Functions

_transform_banded_jac(bjac) Convert a real matrix of the form (for example)
find_integrator(name)
_vode_banded_jac_wrapper(jacfunc,ml,jac_params) Wrap a banded Jacobian function with a function that pads
class ode(f, jac=None)

A generic interface class to numeric integrators.

Solve an equation system with (optional) jac = df/dy.

Note: The first two arguments of f(t, y, ...) are in the opposite order of the arguments in the system definition function used by scipy.integrate.odeint.

f : callable f(t, y, *f_args)
Right-hand side of the differential equation. t is a scalar, y.shape == (n,). f_args is set by calling set_f_params(*args). f should return a scalar, array or list (not a tuple).
jac : callable jac(t, y, *jac_args), optional
Jacobian of the right-hand side, jac[i,j] = d f[i] / d y[j]. jac_args is set by calling set_jac_params(*args).
t : float
Current time.
y : ndarray
Current variable values.

odeint : an integrator with a simpler interface based on lsoda from ODEPACK quad : for finding the area under a curve

Available integrators are listed below. They can be selected using the set_integrator method.

“vode”

Real-valued Variable-coefficient Ordinary Differential Equation solver, with fixed-leading-coefficient implementation. It provides implicit Adams method (for non-stiff problems) and a method based on backward differentiation formulas (BDF) (for stiff problems).

Source: http://www.netlib.org/ode/vode.f

Warning

This integrator is not re-entrant. You cannot have two ode instances using the “vode” integrator at the same time.

This integrator accepts the following parameters in set_integrator method of the ode class:

  • atol : float or sequence absolute tolerance for solution
  • rtol : float or sequence relative tolerance for solution
  • lband : None or int
  • uband : None or int Jacobian band width, jac[i,j] != 0 for i-lband <= j <= i+uband. Setting these requires your jac routine to return the jacobian in packed format, jac_packed[i-j+uband, j] = jac[i,j]. The dimension of the matrix must be (lband+uband+1, len(y)).
  • method: ‘adams’ or ‘bdf’ Which solver to use, Adams (non-stiff) or BDF (stiff)
  • with_jacobian : bool This option is only considered when the user has not supplied a Jacobian function and has not indicated (by setting either band) that the Jacobian is banded. In this case, with_jacobian specifies whether the iteration method of the ODE solver’s correction step is chord iteration with an internally generated full Jacobian or functional iteration with no Jacobian.
  • nsteps : int Maximum number of (internally defined) steps allowed during one call to the solver.
  • first_step : float
  • min_step : float
  • max_step : float Limits for the step sizes used by the integrator.
  • order : int Maximum order used by the integrator, order <= 12 for Adams, <= 5 for BDF.

“zvode”

Complex-valued Variable-coefficient Ordinary Differential Equation solver, with fixed-leading-coefficient implementation. It provides implicit Adams method (for non-stiff problems) and a method based on backward differentiation formulas (BDF) (for stiff problems).

Source: http://www.netlib.org/ode/zvode.f

Warning

This integrator is not re-entrant. You cannot have two ode instances using the “zvode” integrator at the same time.

This integrator accepts the same parameters in set_integrator as the “vode” solver.

Note

When using ZVODE for a stiff system, it should only be used for the case in which the function f is analytic, that is, when each f(i) is an analytic function of each y(j). Analyticity means that the partial derivative df(i)/dy(j) is a unique complex number, and this fact is critical in the way ZVODE solves the dense or banded linear systems that arise in the stiff case. For a complex stiff ODE system in which f is not analytic, ZVODE is likely to have convergence failures, and for this problem one should instead use DVODE on the equivalent real system (in the real and imaginary parts of y).

“lsoda”

Real-valued Variable-coefficient Ordinary Differential Equation solver, with fixed-leading-coefficient implementation. It provides automatic method switching between implicit Adams method (for non-stiff problems) and a method based on backward differentiation formulas (BDF) (for stiff problems).

Source: http://www.netlib.org/odepack

Warning

This integrator is not re-entrant. You cannot have two ode instances using the “lsoda” integrator at the same time.

This integrator accepts the following parameters in set_integrator method of the ode class:

  • atol : float or sequence absolute tolerance for solution
  • rtol : float or sequence relative tolerance for solution
  • lband : None or int
  • uband : None or int Jacobian band width, jac[i,j] != 0 for i-lband <= j <= i+uband. Setting these requires your jac routine to return the jacobian in packed format, jac_packed[i-j+uband, j] = jac[i,j].
  • with_jacobian : bool Not used.
  • nsteps : int Maximum number of (internally defined) steps allowed during one call to the solver.
  • first_step : float
  • min_step : float
  • max_step : float Limits for the step sizes used by the integrator.
  • max_order_ns : int Maximum order used in the nonstiff case (default 12).
  • max_order_s : int Maximum order used in the stiff case (default 5).
  • max_hnil : int Maximum number of messages reporting too small step size (t + h = t) (default 0)
  • ixpr : int Whether to generate extra printing at method switches (default False).

“dopri5”

This is an explicit runge-kutta method of order (4)5 due to Dormand & Prince (with stepsize control and dense output).

Authors:

E. Hairer and G. Wanner Universite de Geneve, Dept. de Mathematiques CH-1211 Geneve 24, Switzerland e-mail: ernst.hairer@math.unige.ch, gerhard.wanner@math.unige.ch

This code is described in [HNW93].

This integrator accepts the following parameters in set_integrator() method of the ode class:

  • atol : float or sequence absolute tolerance for solution
  • rtol : float or sequence relative tolerance for solution
  • nsteps : int Maximum number of (internally defined) steps allowed during one call to the solver.
  • first_step : float
  • max_step : float
  • safety : float Safety factor on new step selection (default 0.9)
  • ifactor : float
  • dfactor : float Maximum factor to increase/decrease step size by in one step
  • beta : float Beta parameter for stabilised step size control.
  • verbosity : int Switch for printing messages (< 0 for no messages).

“dop853”

This is an explicit runge-kutta method of order 8(5,3) due to Dormand & Prince (with stepsize control and dense output).

Options and references the same as “dopri5”.

A problem to integrate and the corresponding jacobian:

>>> from scipy.integrate import ode
>>>
>>> y0, t0 = [1.0j, 2.0], 0
>>>
>>> def f(t, y, arg1):
...     return [1j*arg1*y[0] + y[1], -arg1*y[1]**2]
>>> def jac(t, y, arg1):
...     return [[1j*arg1, 1], [0, -arg1*2*y[1]]]

The integration:

>>> r = ode(f, jac).set_integrator('zvode', method='bdf')
>>> r.set_initial_value(y0, t0).set_f_params(2.0).set_jac_params(2.0)
>>> t1 = 10
>>> dt = 1
>>> while r.successful() and r.t < t1:
...     print(r.t+dt, r.integrate(r.t+dt))
1 [-0.71038232+0.23749653j  0.40000271+0.j        ]
2.0 [ 0.19098503-0.52359246j  0.22222356+0.j        ]
3.0 [ 0.47153208+0.52701229j  0.15384681+0.j        ]
4.0 [-0.61905937+0.30726255j  0.11764744+0.j        ]
5.0 [ 0.02340997-0.61418799j  0.09523835+0.j        ]
6.0 [ 0.58643071+0.339819j  0.08000018+0.j      ]
7.0 [-0.52070105+0.44525141j  0.06896565+0.j        ]
8.0 [-0.15986733-0.61234476j  0.06060616+0.j        ]
9.0 [ 0.64850462+0.15048982j  0.05405414+0.j        ]
10.0 [-0.38404699+0.56382299j  0.04878055+0.j        ]
[HNW93]E. Hairer, S.P. Norsett and G. Wanner, Solving Ordinary Differential Equations i. Nonstiff Problems. 2nd edition. Springer Series in Computational Mathematics, Springer-Verlag (1993)
__init__(f, jac=None)
y()
set_initial_value(y, t=0.0)

Set initial conditions y(t) = y.

set_integrator(name, **integrator_params)

Set integrator by name.

name : str
Name of the integrator.
integrator_params
Additional parameters for the integrator.
integrate(t, step=False, relax=False)

Find y=y(t), set y as an initial condition, and return y.

t : float
The endpoint of the integration step.
step : bool
If True, and if the integrator supports the step method, then perform a single integration step and return. This parameter is provided in order to expose internals of the implementation, and should not be changed from its default value in most cases.
relax : bool
If True and if the integrator supports the run_relax method, then integrate until t_1 >= t and return. relax is not referenced if step=True. This parameter is provided in order to expose internals of the implementation, and should not be changed from its default value in most cases.
y : float
The integrated value at t
successful()

Check if integration was successful.

get_return_code()

Extracts the return code for the integration to enable better control if the integration fails.

set_f_params(*args)

Set extra parameters for user-supplied function f.

set_jac_params(*args)

Set extra parameters for user-supplied function jac.

set_solout(solout)

Set callable to be called at every successful integration step.

solout : callable
solout(t, y) is called at each internal integrator step, t is a scalar providing the current independent position y is the current soloution y.shape == (n,) solout should return -1 to stop integration otherwise it should return None or 0
_transform_banded_jac(bjac)

Convert a real matrix of the form (for example)

[0 0 A B] [0 0 0 B] [0 0 C D] [0 0 A D] [E F G H] to [0 F C H] [I J K L] [E J G L]

[I 0 K 0]

That is, every other column is shifted up one.

class complex_ode(f, jac=None)

A wrapper of ode for complex systems.

This functions similarly as ode, but re-maps a complex-valued equation system to a real-valued one before using the integrators.

f : callable f(t, y, *f_args)
Rhs of the equation. t is a scalar, y.shape == (n,). f_args is set by calling set_f_params(*args).
jac : callable jac(t, y, *jac_args)
Jacobian of the rhs, jac[i,j] = d f[i] / d y[j]. jac_args is set by calling set_f_params(*args).
t : float
Current time.
y : ndarray
Current variable values.

For usage examples, see ode.

__init__(f, jac=None)
_wrap(t, y, *f_args)
_wrap_jac(t, y, *jac_args)
y()
set_integrator(name, **integrator_params)

Set integrator by name.

name : str
Name of the integrator
integrator_params
Additional parameters for the integrator.
set_initial_value(y, t=0.0)

Set initial conditions y(t) = y.

integrate(t, step=False, relax=False)

Find y=y(t), set y as an initial condition, and return y.

t : float
The endpoint of the integration step.
step : bool
If True, and if the integrator supports the step method, then perform a single integration step and return. This parameter is provided in order to expose internals of the implementation, and should not be changed from its default value in most cases.
relax : bool
If True and if the integrator supports the run_relax method, then integrate until t_1 >= t and return. relax is not referenced if step=True. This parameter is provided in order to expose internals of the implementation, and should not be changed from its default value in most cases.
y : float
The integrated value at t
set_solout(solout)

Set callable to be called at every successful integration step.

solout : callable
solout(t, y) is called at each internal integrator step, t is a scalar providing the current independent position y is the current soloution y.shape == (n,) solout should return -1 to stop integration otherwise it should return None or 0
find_integrator(name)
class IntegratorConcurrencyError(name)

Failure due to concurrent usage of an integrator that can be used only for a single problem at a time.

__init__(name)
class IntegratorBase
acquire_new_handle()
check_handle()
reset(n, has_jac)

Prepare integrator for call: allocate memory, set flags, etc. n - number of equations. has_jac - if user has supplied function for evaluating Jacobian.

run(f, jac, y0, t0, t1, f_params, jac_params)

Integrate from t=t0 to t=t1 using y0 as an initial condition. Return 2-tuple (y1,t1) where y1 is the result and t=t1 defines the stoppage coordinate of the result.

step(f, jac, y0, t0, t1, f_params, jac_params)

Make one integration step and return (y1,t1).

run_relax(f, jac, y0, t0, t1, f_params, jac_params)

Integrate from t=t0 to t>=t1 and return (y1,t).

_vode_banded_jac_wrapper(jacfunc, ml, jac_params)

Wrap a banded Jacobian function with a function that pads the Jacobian with ml rows of zeros.

class vode(method="adams", with_jacobian=False, rtol=1e-06, atol=1e-12, lband=None, uband=None, order=12, nsteps=500, max_step=0.0, min_step=0.0, first_step=0.0)
__init__(method="adams", with_jacobian=False, rtol=1e-06, atol=1e-12, lband=None, uband=None, order=12, nsteps=500, max_step=0.0, min_step=0.0, first_step=0.0)
_determine_mf_and_set_bands(has_jac)

Determine the MF parameter (Method Flag) for the Fortran subroutine dvode.

In the Fortran code, the legal values of MF are:
10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25, -11, -12, -14, -15, -21, -22, -24, -25

but this python wrapper does not use negative values.

Returns

mf = 10*self.meth + miter
self.meth is the linear multistep method:
self.meth == 1: method=”adams” self.meth == 2: method=”bdf”
miter is the correction iteration method:
miter == 0: Functional iteraton; no Jacobian involved. miter == 1: Chord iteration with user-supplied full Jacobian miter == 2: Chord iteration with internally computed full Jacobian miter == 3: Chord iteration with internally computed diagonal Jacobian miter == 4: Chord iteration with user-supplied banded Jacobian miter == 5: Chord iteration with internally computed banded Jacobian

Side effects: If either self.mu or self.ml is not None and the other is None, then the one that is None is set to 0.

reset(n, has_jac)
run(f, jac, y0, t0, t1, f_params, jac_params)
step(*args)
run_relax(*args)
class zvode
reset(n, has_jac)
class dopri5(rtol=1e-06, atol=1e-12, nsteps=500, max_step=0.0, first_step=0.0, safety=0.9, ifactor=10.0, dfactor=0.2, beta=0.0, method=None, verbosity=None)
__init__(rtol=1e-06, atol=1e-12, nsteps=500, max_step=0.0, first_step=0.0, safety=0.9, ifactor=10.0, dfactor=0.2, beta=0.0, method=None, verbosity=None)
set_solout(solout, complex=False)
reset(n, has_jac)
run(f, jac, y0, t0, t1, f_params, jac_params)
_solout(nr, xold, x, y, nd, icomp, con)
class dop853(rtol=1e-06, atol=1e-12, nsteps=500, max_step=0.0, first_step=0.0, safety=0.9, ifactor=6.0, dfactor=0.3, beta=0.0, method=None, verbosity=None)
__init__(rtol=1e-06, atol=1e-12, nsteps=500, max_step=0.0, first_step=0.0, safety=0.9, ifactor=6.0, dfactor=0.3, beta=0.0, method=None, verbosity=None)
reset(n, has_jac)
class lsoda(with_jacobian=False, rtol=1e-06, atol=1e-12, lband=None, uband=None, nsteps=500, max_step=0.0, min_step=0.0, first_step=0.0, ixpr=0, max_hnil=0, max_order_ns=12, max_order_s=5, method=None)
__init__(with_jacobian=False, rtol=1e-06, atol=1e-12, lband=None, uband=None, nsteps=500, max_step=0.0, min_step=0.0, first_step=0.0, ixpr=0, max_hnil=0, max_order_ns=12, max_order_s=5, method=None)
reset(n, has_jac)
run(f, jac, y0, t0, t1, f_params, jac_params)
step(*args)
run_relax(*args)