Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/scipy/integrate/_ivp/lsoda.py: 19%
57 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-12 06:31 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-12 06:31 +0000
1import numpy as np
2from scipy.integrate import ode
3from .common import validate_tol, validate_first_step, warn_extraneous
4from .base import OdeSolver, DenseOutput
7class LSODA(OdeSolver):
8 """Adams/BDF method with automatic stiffness detection and switching.
10 This is a wrapper to the Fortran solver from ODEPACK [1]_. It switches
11 automatically between the nonstiff Adams method and the stiff BDF method.
12 The method was originally detailed in [2]_.
14 Parameters
15 ----------
16 fun : callable
17 Right-hand side of the system. The calling signature is ``fun(t, y)``.
18 Here ``t`` is a scalar, and there are two options for the ndarray ``y``:
19 It can either have shape (n,); then ``fun`` must return array_like with
20 shape (n,). Alternatively it can have shape (n, k); then ``fun``
21 must return an array_like with shape (n, k), i.e. each column
22 corresponds to a single column in ``y``. The choice between the two
23 options is determined by `vectorized` argument (see below). The
24 vectorized implementation allows a faster approximation of the Jacobian
25 by finite differences (required for this solver).
26 t0 : float
27 Initial time.
28 y0 : array_like, shape (n,)
29 Initial state.
30 t_bound : float
31 Boundary time - the integration won't continue beyond it. It also
32 determines the direction of the integration.
33 first_step : float or None, optional
34 Initial step size. Default is ``None`` which means that the algorithm
35 should choose.
36 min_step : float, optional
37 Minimum allowed step size. Default is 0.0, i.e., the step size is not
38 bounded and determined solely by the solver.
39 max_step : float, optional
40 Maximum allowed step size. Default is np.inf, i.e., the step size is not
41 bounded and determined solely by the solver.
42 rtol, atol : float and array_like, optional
43 Relative and absolute tolerances. The solver keeps the local error
44 estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a
45 relative accuracy (number of correct digits), while `atol` controls
46 absolute accuracy (number of correct decimal places). To achieve the
47 desired `rtol`, set `atol` to be smaller than the smallest value that
48 can be expected from ``rtol * abs(y)`` so that `rtol` dominates the
49 allowable error. If `atol` is larger than ``rtol * abs(y)`` the
50 number of correct digits is not guaranteed. Conversely, to achieve the
51 desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller
52 than `atol`. If components of y have different scales, it might be
53 beneficial to set different `atol` values for different components by
54 passing array_like with shape (n,) for `atol`. Default values are
55 1e-3 for `rtol` and 1e-6 for `atol`.
56 jac : None or callable, optional
57 Jacobian matrix of the right-hand side of the system with respect to
58 ``y``. The Jacobian matrix has shape (n, n) and its element (i, j) is
59 equal to ``d f_i / d y_j``. The function will be called as
60 ``jac(t, y)``. If None (default), the Jacobian will be
61 approximated by finite differences. It is generally recommended to
62 provide the Jacobian rather than relying on a finite-difference
63 approximation.
64 lband, uband : int or None
65 Parameters defining the bandwidth of the Jacobian,
66 i.e., ``jac[i, j] != 0 only for i - lband <= j <= i + uband``. Setting
67 these requires your jac routine to return the Jacobian in the packed format:
68 the returned array must have ``n`` columns and ``uband + lband + 1``
69 rows in which Jacobian diagonals are written. Specifically
70 ``jac_packed[uband + i - j , j] = jac[i, j]``. The same format is used
71 in `scipy.linalg.solve_banded` (check for an illustration).
72 These parameters can be also used with ``jac=None`` to reduce the
73 number of Jacobian elements estimated by finite differences.
74 vectorized : bool, optional
75 Whether `fun` is implemented in a vectorized fashion. A vectorized
76 implementation offers no advantages for this solver. Default is False.
78 Attributes
79 ----------
80 n : int
81 Number of equations.
82 status : string
83 Current status of the solver: 'running', 'finished' or 'failed'.
84 t_bound : float
85 Boundary time.
86 direction : float
87 Integration direction: +1 or -1.
88 t : float
89 Current time.
90 y : ndarray
91 Current state.
92 t_old : float
93 Previous time. None if no steps were made yet.
94 nfev : int
95 Number of evaluations of the right-hand side.
96 njev : int
97 Number of evaluations of the Jacobian.
99 References
100 ----------
101 .. [1] A. C. Hindmarsh, "ODEPACK, A Systematized Collection of ODE
102 Solvers," IMACS Transactions on Scientific Computation, Vol 1.,
103 pp. 55-64, 1983.
104 .. [2] L. Petzold, "Automatic selection of methods for solving stiff and
105 nonstiff systems of ordinary differential equations", SIAM Journal
106 on Scientific and Statistical Computing, Vol. 4, No. 1, pp. 136-148,
107 1983.
108 """
109 def __init__(self, fun, t0, y0, t_bound, first_step=None, min_step=0.0,
110 max_step=np.inf, rtol=1e-3, atol=1e-6, jac=None, lband=None,
111 uband=None, vectorized=False, **extraneous):
112 warn_extraneous(extraneous)
113 super().__init__(fun, t0, y0, t_bound, vectorized)
115 if first_step is None:
116 first_step = 0 # LSODA value for automatic selection.
117 else:
118 first_step = validate_first_step(first_step, t0, t_bound)
120 first_step *= self.direction
122 if max_step == np.inf:
123 max_step = 0 # LSODA value for infinity.
124 elif max_step <= 0:
125 raise ValueError("`max_step` must be positive.")
127 if min_step < 0:
128 raise ValueError("`min_step` must be nonnegative.")
130 rtol, atol = validate_tol(rtol, atol, self.n)
132 solver = ode(self.fun, jac)
133 solver.set_integrator('lsoda', rtol=rtol, atol=atol, max_step=max_step,
134 min_step=min_step, first_step=first_step,
135 lband=lband, uband=uband)
136 solver.set_initial_value(y0, t0)
138 # Inject t_bound into rwork array as needed for itask=5.
139 solver._integrator.rwork[0] = self.t_bound
140 solver._integrator.call_args[4] = solver._integrator.rwork
142 self._lsoda_solver = solver
144 def _step_impl(self):
145 solver = self._lsoda_solver
146 integrator = solver._integrator
148 # From lsoda.step and lsoda.integrate itask=5 means take a single
149 # step and do not go past t_bound.
150 itask = integrator.call_args[2]
151 integrator.call_args[2] = 5
152 solver._y, solver.t = integrator.run(
153 solver.f, solver.jac or (lambda: None), solver._y, solver.t,
154 self.t_bound, solver.f_params, solver.jac_params)
155 integrator.call_args[2] = itask
157 if solver.successful():
158 self.t = solver.t
159 self.y = solver._y
160 # From LSODA Fortran source njev is equal to nlu.
161 self.njev = integrator.iwork[12]
162 self.nlu = integrator.iwork[12]
163 return True, None
164 else:
165 return False, 'Unexpected istate in LSODA.'
167 def _dense_output_impl(self):
168 iwork = self._lsoda_solver._integrator.iwork
169 rwork = self._lsoda_solver._integrator.rwork
171 order = iwork[14]
172 h = rwork[11]
173 yh = np.reshape(rwork[20:20 + (order + 1) * self.n],
174 (self.n, order + 1), order='F').copy()
176 return LsodaDenseOutput(self.t_old, self.t, h, order, yh)
179class LsodaDenseOutput(DenseOutput):
180 def __init__(self, t_old, t, h, order, yh):
181 super().__init__(t_old, t)
182 self.h = h
183 self.yh = yh
184 self.p = np.arange(order + 1)
186 def _call_impl(self, t):
187 if t.ndim == 0:
188 x = ((t - self.t) / self.h) ** self.p
189 else:
190 x = ((t - self.t) / self.h) ** self.p[:, None]
192 return np.dot(self.yh, x)