lsoda.py 8.1 KB

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