lsoda.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 time derivative of the state ``y``
  14. at time ``t``. The calling signature is ``fun(t, y)``, where ``t`` is a
  15. scalar and ``y`` is an ndarray with ``len(y) = len(y0)``. ``fun`` must
  16. return an array of the same shape as ``y``. See `vectorized` for more
  17. information.
  18. t0 : float
  19. Initial time.
  20. y0 : array_like, shape (n,)
  21. Initial state.
  22. t_bound : float
  23. Boundary time - the integration won't continue beyond it. It also
  24. determines the direction of the integration.
  25. first_step : float or None, optional
  26. Initial step size. Default is ``None`` which means that the algorithm
  27. should choose.
  28. min_step : float, optional
  29. Minimum allowed step size. Default is 0.0, i.e., the step size is not
  30. bounded and determined solely by the solver.
  31. max_step : float, optional
  32. Maximum allowed step size. Default is np.inf, i.e., the step size is not
  33. bounded and determined solely by the solver.
  34. rtol, atol : float and array_like, optional
  35. Relative and absolute tolerances. The solver keeps the local error
  36. estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a
  37. relative accuracy (number of correct digits), while `atol` controls
  38. absolute accuracy (number of correct decimal places). To achieve the
  39. desired `rtol`, set `atol` to be smaller than the smallest value that
  40. can be expected from ``rtol * abs(y)`` so that `rtol` dominates the
  41. allowable error. If `atol` is larger than ``rtol * abs(y)`` the
  42. number of correct digits is not guaranteed. Conversely, to achieve the
  43. desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller
  44. than `atol`. If components of y have different scales, it might be
  45. beneficial to set different `atol` values for different components by
  46. passing array_like with shape (n,) for `atol`. Default values are
  47. 1e-3 for `rtol` and 1e-6 for `atol`.
  48. jac : None or callable, optional
  49. Jacobian matrix of the right-hand side of the system with respect to
  50. ``y``. The Jacobian matrix has shape (n, n) and its element (i, j) is
  51. equal to ``d f_i / d y_j``. The function will be called as
  52. ``jac(t, y)``. If None (default), the Jacobian will be
  53. approximated by finite differences. It is generally recommended to
  54. provide the Jacobian rather than relying on a finite-difference
  55. approximation.
  56. lband, uband : int or None
  57. Parameters defining the bandwidth of the Jacobian,
  58. i.e., ``jac[i, j] != 0 only for i - lband <= j <= i + uband``. Setting
  59. these requires your jac routine to return the Jacobian in the packed format:
  60. the returned array must have ``n`` columns and ``uband + lband + 1``
  61. rows in which Jacobian diagonals are written. Specifically
  62. ``jac_packed[uband + i - j , j] = jac[i, j]``. The same format is used
  63. in `scipy.linalg.solve_banded` (check for an illustration).
  64. These parameters can be also used with ``jac=None`` to reduce the
  65. number of Jacobian elements estimated by finite differences.
  66. vectorized : bool, optional
  67. Whether `fun` may be called in a vectorized fashion. False (default)
  68. is recommended for this solver.
  69. If ``vectorized`` is False, `fun` will always be called with ``y`` of
  70. shape ``(n,)``, where ``n = len(y0)``.
  71. If ``vectorized`` is True, `fun` may be called with ``y`` of shape
  72. ``(n, k)``, where ``k`` is an integer. In this case, `fun` must behave
  73. such that ``fun(t, y)[:, i] == fun(t, y[:, i])`` (i.e. each column of
  74. the returned array is the time derivative of the state corresponding
  75. with a column of ``y``).
  76. Setting ``vectorized=True`` allows for faster finite difference
  77. approximation of the Jacobian by methods 'Radau' and 'BDF', but
  78. will result in slower execution for this solver.
  79. Attributes
  80. ----------
  81. n : int
  82. Number of equations.
  83. status : string
  84. Current status of the solver: 'running', 'finished' or 'failed'.
  85. t_bound : float
  86. Boundary time.
  87. direction : float
  88. Integration direction: +1 or -1.
  89. t : float
  90. Current time.
  91. y : ndarray
  92. Current state.
  93. t_old : float
  94. Previous time. None if no steps were made yet.
  95. nfev : int
  96. Number of evaluations of the right-hand side.
  97. njev : int
  98. 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)
  114. if first_step is None:
  115. first_step = 0 # LSODA value for automatic selection.
  116. else:
  117. first_step = validate_first_step(first_step, t0, t_bound)
  118. first_step *= self.direction
  119. if max_step == np.inf:
  120. max_step = 0 # LSODA value for infinity.
  121. elif max_step <= 0:
  122. raise ValueError("`max_step` must be positive.")
  123. if min_step < 0:
  124. raise ValueError("`min_step` must be nonnegative.")
  125. rtol, atol = validate_tol(rtol, atol, self.n)
  126. solver = ode(self.fun, jac)
  127. solver.set_integrator('lsoda', rtol=rtol, atol=atol, max_step=max_step,
  128. min_step=min_step, first_step=first_step,
  129. lband=lband, uband=uband)
  130. solver.set_initial_value(y0, t0)
  131. # Inject t_bound into rwork array as needed for itask=5.
  132. solver._integrator.rwork[0] = self.t_bound
  133. solver._integrator.call_args[4] = solver._integrator.rwork
  134. self._lsoda_solver = solver
  135. def _step_impl(self):
  136. solver = self._lsoda_solver
  137. integrator = solver._integrator
  138. # From lsoda.step and lsoda.integrate itask=5 means take a single
  139. # step and do not go past t_bound.
  140. itask = integrator.call_args[2]
  141. integrator.call_args[2] = 5
  142. solver._y, solver.t = integrator.run(
  143. solver.f, solver.jac or (lambda: None), solver._y, solver.t,
  144. self.t_bound, solver.f_params, solver.jac_params)
  145. integrator.call_args[2] = itask
  146. if solver.successful():
  147. self.t = solver.t
  148. # IMPORTANT: Must copy solver._y because the C code reuses the same
  149. # array object across calls (for in-place modification). Without copy,
  150. # solve_ivp would store references to the same array.
  151. self.y = solver._y.copy()
  152. # From LSODA Fortran source njev is equal to nlu.
  153. self.njev = integrator.iwork[12]
  154. self.nlu = integrator.iwork[12]
  155. return True, None
  156. else:
  157. return False, 'Unexpected istate in LSODA.'
  158. def _dense_output_impl(self):
  159. iwork = self._lsoda_solver._integrator.iwork
  160. rwork = self._lsoda_solver._integrator.rwork
  161. # We want to produce the Nordsieck history array, yh, up to the order
  162. # used in the last successful iteration. The step size is unimportant
  163. # because it will be scaled out in LsodaDenseOutput. Some additional
  164. # work may be required because ODEPACK's LSODA implementation produces
  165. # the Nordsieck history in the state needed for the next iteration.
  166. # iwork[13] contains order from last successful iteration, while
  167. # iwork[14] contains order to be attempted next.
  168. order = iwork[13]
  169. # rwork[11] contains the step size to be attempted next, while
  170. # rwork[10] contains step size from last successful iteration.
  171. h = rwork[11]
  172. # rwork[20:20 + (iwork[14] + 1) * self.n] contains entries of the
  173. # Nordsieck array in state needed for next iteration. We want
  174. # the entries up to order for the last successful step so use the
  175. # following.
  176. yh = np.reshape(rwork[20:20 + (order + 1) * self.n],
  177. (self.n, order + 1), order='F').copy()
  178. if iwork[14] < order:
  179. # If the order is set to decrease then the final column of yh
  180. # has not been updated within ODEPACK's LSODA
  181. # implementation because this column will not be used in the
  182. # next iteration. We must rescale this column to make the
  183. # associated step size consistent with the other columns.
  184. yh[:, -1] *= (h / rwork[10]) ** order
  185. return LsodaDenseOutput(self.t_old, self.t, h, order, yh)
  186. class LsodaDenseOutput(DenseOutput):
  187. def __init__(self, t_old, t, h, order, yh):
  188. super().__init__(t_old, t)
  189. self.h = h
  190. self.yh = yh
  191. self.p = np.arange(order + 1)
  192. def _call_impl(self, t):
  193. if t.ndim == 0:
  194. x = ((t - self.t) / self.h) ** self.p
  195. else:
  196. x = ((t - self.t) / self.h) ** self.p[:, None]
  197. return np.dot(self.yh, x)