Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/scipy/integrate/_ivp/rk.py: 32%

191 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-12 06:31 +0000

1import numpy as np 

2from .base import OdeSolver, DenseOutput 

3from .common import (validate_max_step, validate_tol, select_initial_step, 

4 norm, warn_extraneous, validate_first_step) 

5from . import dop853_coefficients 

6 

7# Multiply steps computed from asymptotic behaviour of errors by this. 

8SAFETY = 0.9 

9 

10MIN_FACTOR = 0.2 # Minimum allowed decrease in a step size. 

11MAX_FACTOR = 10 # Maximum allowed increase in a step size. 

12 

13 

14def rk_step(fun, t, y, f, h, A, B, C, K): 

15 """Perform a single Runge-Kutta step. 

16 

17 This function computes a prediction of an explicit Runge-Kutta method and 

18 also estimates the error of a less accurate method. 

19 

20 Notation for Butcher tableau is as in [1]_. 

21 

22 Parameters 

23 ---------- 

24 fun : callable 

25 Right-hand side of the system. 

26 t : float 

27 Current time. 

28 y : ndarray, shape (n,) 

29 Current state. 

30 f : ndarray, shape (n,) 

31 Current value of the derivative, i.e., ``fun(x, y)``. 

32 h : float 

33 Step to use. 

34 A : ndarray, shape (n_stages, n_stages) 

35 Coefficients for combining previous RK stages to compute the next 

36 stage. For explicit methods the coefficients at and above the main 

37 diagonal are zeros. 

38 B : ndarray, shape (n_stages,) 

39 Coefficients for combining RK stages for computing the final 

40 prediction. 

41 C : ndarray, shape (n_stages,) 

42 Coefficients for incrementing time for consecutive RK stages. 

43 The value for the first stage is always zero. 

44 K : ndarray, shape (n_stages + 1, n) 

45 Storage array for putting RK stages here. Stages are stored in rows. 

46 The last row is a linear combination of the previous rows with 

47 coefficients 

48 

49 Returns 

50 ------- 

51 y_new : ndarray, shape (n,) 

52 Solution at t + h computed with a higher accuracy. 

53 f_new : ndarray, shape (n,) 

54 Derivative ``fun(t + h, y_new)``. 

55 

56 References 

57 ---------- 

58 .. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential 

59 Equations I: Nonstiff Problems", Sec. II.4. 

60 """ 

61 K[0] = f 

62 for s, (a, c) in enumerate(zip(A[1:], C[1:]), start=1): 

63 dy = np.dot(K[:s].T, a[:s]) * h 

64 K[s] = fun(t + c * h, y + dy) 

65 

66 y_new = y + h * np.dot(K[:-1].T, B) 

67 f_new = fun(t + h, y_new) 

68 

69 K[-1] = f_new 

70 

71 return y_new, f_new 

72 

73 

74class RungeKutta(OdeSolver): 

75 """Base class for explicit Runge-Kutta methods.""" 

76 C: np.ndarray = NotImplemented 

77 A: np.ndarray = NotImplemented 

78 B: np.ndarray = NotImplemented 

79 E: np.ndarray = NotImplemented 

80 P: np.ndarray = NotImplemented 

81 order: int = NotImplemented 

82 error_estimator_order: int = NotImplemented 

83 n_stages: int = NotImplemented 

84 

85 def __init__(self, fun, t0, y0, t_bound, max_step=np.inf, 

86 rtol=1e-3, atol=1e-6, vectorized=False, 

87 first_step=None, **extraneous): 

88 warn_extraneous(extraneous) 

89 super().__init__(fun, t0, y0, t_bound, vectorized, 

90 support_complex=True) 

91 self.y_old = None 

92 self.max_step = validate_max_step(max_step) 

93 self.rtol, self.atol = validate_tol(rtol, atol, self.n) 

94 self.f = self.fun(self.t, self.y) 

95 if first_step is None: 

96 self.h_abs = select_initial_step( 

97 self.fun, self.t, self.y, self.f, self.direction, 

98 self.error_estimator_order, self.rtol, self.atol) 

99 else: 

100 self.h_abs = validate_first_step(first_step, t0, t_bound) 

101 self.K = np.empty((self.n_stages + 1, self.n), dtype=self.y.dtype) 

102 self.error_exponent = -1 / (self.error_estimator_order + 1) 

103 self.h_previous = None 

104 

105 def _estimate_error(self, K, h): 

106 return np.dot(K.T, self.E) * h 

107 

108 def _estimate_error_norm(self, K, h, scale): 

109 return norm(self._estimate_error(K, h) / scale) 

110 

111 def _step_impl(self): 

112 t = self.t 

113 y = self.y 

114 

115 max_step = self.max_step 

116 rtol = self.rtol 

117 atol = self.atol 

118 

119 min_step = 10 * np.abs(np.nextafter(t, self.direction * np.inf) - t) 

120 

121 if self.h_abs > max_step: 

122 h_abs = max_step 

123 elif self.h_abs < min_step: 

124 h_abs = min_step 

125 else: 

126 h_abs = self.h_abs 

127 

128 step_accepted = False 

129 step_rejected = False 

130 

131 while not step_accepted: 

132 if h_abs < min_step: 

133 return False, self.TOO_SMALL_STEP 

134 

135 h = h_abs * self.direction 

136 t_new = t + h 

137 

138 if self.direction * (t_new - self.t_bound) > 0: 

139 t_new = self.t_bound 

140 

141 h = t_new - t 

142 h_abs = np.abs(h) 

143 

144 y_new, f_new = rk_step(self.fun, t, y, self.f, h, self.A, 

145 self.B, self.C, self.K) 

146 scale = atol + np.maximum(np.abs(y), np.abs(y_new)) * rtol 

147 error_norm = self._estimate_error_norm(self.K, h, scale) 

148 

149 if error_norm < 1: 

150 if error_norm == 0: 

151 factor = MAX_FACTOR 

152 else: 

153 factor = min(MAX_FACTOR, 

154 SAFETY * error_norm ** self.error_exponent) 

155 

156 if step_rejected: 

157 factor = min(1, factor) 

158 

159 h_abs *= factor 

160 

161 step_accepted = True 

162 else: 

163 h_abs *= max(MIN_FACTOR, 

164 SAFETY * error_norm ** self.error_exponent) 

165 step_rejected = True 

166 

167 self.h_previous = h 

168 self.y_old = y 

169 

170 self.t = t_new 

171 self.y = y_new 

172 

173 self.h_abs = h_abs 

174 self.f = f_new 

175 

176 return True, None 

177 

178 def _dense_output_impl(self): 

179 Q = self.K.T.dot(self.P) 

180 return RkDenseOutput(self.t_old, self.t, self.y_old, Q) 

181 

182 

183class RK23(RungeKutta): 

184 """Explicit Runge-Kutta method of order 3(2). 

185 

186 This uses the Bogacki-Shampine pair of formulas [1]_. The error is controlled 

187 assuming accuracy of the second-order method, but steps are taken using the 

188 third-order accurate formula (local extrapolation is done). A cubic Hermite 

189 polynomial is used for the dense output. 

190 

191 Can be applied in the complex domain. 

192 

193 Parameters 

194 ---------- 

195 fun : callable 

196 Right-hand side of the system. The calling signature is ``fun(t, y)``. 

197 Here ``t`` is a scalar and there are two options for ndarray ``y``. 

198 It can either have shape (n,), then ``fun`` must return array_like with 

199 shape (n,). Or alternatively it can have shape (n, k), then ``fun`` 

200 must return array_like with shape (n, k), i.e. each column 

201 corresponds to a single column in ``y``. The choice between the two 

202 options is determined by `vectorized` argument (see below). 

203 t0 : float 

204 Initial time. 

205 y0 : array_like, shape (n,) 

206 Initial state. 

207 t_bound : float 

208 Boundary time - the integration won't continue beyond it. It also 

209 determines the direction of the integration. 

210 first_step : float or None, optional 

211 Initial step size. Default is ``None`` which means that the algorithm 

212 should choose. 

213 max_step : float, optional 

214 Maximum allowed step size. Default is np.inf, i.e., the step size is not 

215 bounded and determined solely by the solver. 

216 rtol, atol : float and array_like, optional 

217 Relative and absolute tolerances. The solver keeps the local error 

218 estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a 

219 relative accuracy (number of correct digits), while `atol` controls 

220 absolute accuracy (number of correct decimal places). To achieve the 

221 desired `rtol`, set `atol` to be smaller than the smallest value that 

222 can be expected from ``rtol * abs(y)`` so that `rtol` dominates the 

223 allowable error. If `atol` is larger than ``rtol * abs(y)`` the 

224 number of correct digits is not guaranteed. Conversely, to achieve the 

225 desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller 

226 than `atol`. If components of y have different scales, it might be 

227 beneficial to set different `atol` values for different components by 

228 passing array_like with shape (n,) for `atol`. Default values are 

229 1e-3 for `rtol` and 1e-6 for `atol`. 

230 vectorized : bool, optional 

231 Whether `fun` is implemented in a vectorized fashion. Default is False. 

232 

233 Attributes 

234 ---------- 

235 n : int 

236 Number of equations. 

237 status : string 

238 Current status of the solver: 'running', 'finished' or 'failed'. 

239 t_bound : float 

240 Boundary time. 

241 direction : float 

242 Integration direction: +1 or -1. 

243 t : float 

244 Current time. 

245 y : ndarray 

246 Current state. 

247 t_old : float 

248 Previous time. None if no steps were made yet. 

249 step_size : float 

250 Size of the last successful step. None if no steps were made yet. 

251 nfev : int 

252 Number evaluations of the system's right-hand side. 

253 njev : int 

254 Number of evaluations of the Jacobian. Is always 0 for this solver as it does not use the Jacobian. 

255 nlu : int 

256 Number of LU decompositions. Is always 0 for this solver. 

257 

258 References 

259 ---------- 

260 .. [1] P. Bogacki, L.F. Shampine, "A 3(2) Pair of Runge-Kutta Formulas", 

261 Appl. Math. Lett. Vol. 2, No. 4. pp. 321-325, 1989. 

262 """ 

263 order = 3 

264 error_estimator_order = 2 

265 n_stages = 3 

266 C = np.array([0, 1/2, 3/4]) 

267 A = np.array([ 

268 [0, 0, 0], 

269 [1/2, 0, 0], 

270 [0, 3/4, 0] 

271 ]) 

272 B = np.array([2/9, 1/3, 4/9]) 

273 E = np.array([5/72, -1/12, -1/9, 1/8]) 

274 P = np.array([[1, -4 / 3, 5 / 9], 

275 [0, 1, -2/3], 

276 [0, 4/3, -8/9], 

277 [0, -1, 1]]) 

278 

279 

280class RK45(RungeKutta): 

281 """Explicit Runge-Kutta method of order 5(4). 

282 

283 This uses the Dormand-Prince pair of formulas [1]_. The error is controlled 

284 assuming accuracy of the fourth-order method accuracy, but steps are taken 

285 using the fifth-order accurate formula (local extrapolation is done). 

286 A quartic interpolation polynomial is used for the dense output [2]_. 

287 

288 Can be applied in the complex domain. 

289 

290 Parameters 

291 ---------- 

292 fun : callable 

293 Right-hand side of the system. The calling signature is ``fun(t, y)``. 

294 Here ``t`` is a scalar, and there are two options for the ndarray ``y``: 

295 It can either have shape (n,); then ``fun`` must return array_like with 

296 shape (n,). Alternatively it can have shape (n, k); then ``fun`` 

297 must return an array_like with shape (n, k), i.e., each column 

298 corresponds to a single column in ``y``. The choice between the two 

299 options is determined by `vectorized` argument (see below). 

300 t0 : float 

301 Initial time. 

302 y0 : array_like, shape (n,) 

303 Initial state. 

304 t_bound : float 

305 Boundary time - the integration won't continue beyond it. It also 

306 determines the direction of the integration. 

307 first_step : float or None, optional 

308 Initial step size. Default is ``None`` which means that the algorithm 

309 should choose. 

310 max_step : float, optional 

311 Maximum allowed step size. Default is np.inf, i.e., the step size is not 

312 bounded and determined solely by the solver. 

313 rtol, atol : float and array_like, optional 

314 Relative and absolute tolerances. The solver keeps the local error 

315 estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a 

316 relative accuracy (number of correct digits), while `atol` controls 

317 absolute accuracy (number of correct decimal places). To achieve the 

318 desired `rtol`, set `atol` to be smaller than the smallest value that 

319 can be expected from ``rtol * abs(y)`` so that `rtol` dominates the 

320 allowable error. If `atol` is larger than ``rtol * abs(y)`` the 

321 number of correct digits is not guaranteed. Conversely, to achieve the 

322 desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller 

323 than `atol`. If components of y have different scales, it might be 

324 beneficial to set different `atol` values for different components by 

325 passing array_like with shape (n,) for `atol`. Default values are 

326 1e-3 for `rtol` and 1e-6 for `atol`. 

327 vectorized : bool, optional 

328 Whether `fun` is implemented in a vectorized fashion. Default is False. 

329 

330 Attributes 

331 ---------- 

332 n : int 

333 Number of equations. 

334 status : string 

335 Current status of the solver: 'running', 'finished' or 'failed'. 

336 t_bound : float 

337 Boundary time. 

338 direction : float 

339 Integration direction: +1 or -1. 

340 t : float 

341 Current time. 

342 y : ndarray 

343 Current state. 

344 t_old : float 

345 Previous time. None if no steps were made yet. 

346 step_size : float 

347 Size of the last successful step. None if no steps were made yet. 

348 nfev : int 

349 Number evaluations of the system's right-hand side. 

350 njev : int 

351 Number of evaluations of the Jacobian. Is always 0 for this solver as it does not use the Jacobian. 

352 nlu : int 

353 Number of LU decompositions. Is always 0 for this solver. 

354 

355 References 

356 ---------- 

357 .. [1] J. R. Dormand, P. J. Prince, "A family of embedded Runge-Kutta 

358 formulae", Journal of Computational and Applied Mathematics, Vol. 6, 

359 No. 1, pp. 19-26, 1980. 

360 .. [2] L. W. Shampine, "Some Practical Runge-Kutta Formulas", Mathematics 

361 of Computation,, Vol. 46, No. 173, pp. 135-150, 1986. 

362 """ 

363 order = 5 

364 error_estimator_order = 4 

365 n_stages = 6 

366 C = np.array([0, 1/5, 3/10, 4/5, 8/9, 1]) 

367 A = np.array([ 

368 [0, 0, 0, 0, 0], 

369 [1/5, 0, 0, 0, 0], 

370 [3/40, 9/40, 0, 0, 0], 

371 [44/45, -56/15, 32/9, 0, 0], 

372 [19372/6561, -25360/2187, 64448/6561, -212/729, 0], 

373 [9017/3168, -355/33, 46732/5247, 49/176, -5103/18656] 

374 ]) 

375 B = np.array([35/384, 0, 500/1113, 125/192, -2187/6784, 11/84]) 

376 E = np.array([-71/57600, 0, 71/16695, -71/1920, 17253/339200, -22/525, 

377 1/40]) 

378 # Corresponds to the optimum value of c_6 from [2]_. 

379 P = np.array([ 

380 [1, -8048581381/2820520608, 8663915743/2820520608, 

381 -12715105075/11282082432], 

382 [0, 0, 0, 0], 

383 [0, 131558114200/32700410799, -68118460800/10900136933, 

384 87487479700/32700410799], 

385 [0, -1754552775/470086768, 14199869525/1410260304, 

386 -10690763975/1880347072], 

387 [0, 127303824393/49829197408, -318862633887/49829197408, 

388 701980252875 / 199316789632], 

389 [0, -282668133/205662961, 2019193451/616988883, -1453857185/822651844], 

390 [0, 40617522/29380423, -110615467/29380423, 69997945/29380423]]) 

391 

392 

393class DOP853(RungeKutta): 

394 """Explicit Runge-Kutta method of order 8. 

395 

396 This is a Python implementation of "DOP853" algorithm originally written 

397 in Fortran [1]_, [2]_. Note that this is not a literate translation, but 

398 the algorithmic core and coefficients are the same. 

399 

400 Can be applied in the complex domain. 

401 

402 Parameters 

403 ---------- 

404 fun : callable 

405 Right-hand side of the system. The calling signature is ``fun(t, y)``. 

406 Here, ``t`` is a scalar, and there are two options for the ndarray ``y``: 

407 It can either have shape (n,); then ``fun`` must return array_like with 

408 shape (n,). Alternatively it can have shape (n, k); then ``fun`` 

409 must return an array_like with shape (n, k), i.e. each column 

410 corresponds to a single column in ``y``. The choice between the two 

411 options is determined by `vectorized` argument (see below). 

412 t0 : float 

413 Initial time. 

414 y0 : array_like, shape (n,) 

415 Initial state. 

416 t_bound : float 

417 Boundary time - the integration won't continue beyond it. It also 

418 determines the direction of the integration. 

419 first_step : float or None, optional 

420 Initial step size. Default is ``None`` which means that the algorithm 

421 should choose. 

422 max_step : float, optional 

423 Maximum allowed step size. Default is np.inf, i.e. the step size is not 

424 bounded and determined solely by the solver. 

425 rtol, atol : float and array_like, optional 

426 Relative and absolute tolerances. The solver keeps the local error 

427 estimates less than ``atol + rtol * abs(y)``. Here `rtol` controls a 

428 relative accuracy (number of correct digits), while `atol` controls 

429 absolute accuracy (number of correct decimal places). To achieve the 

430 desired `rtol`, set `atol` to be smaller than the smallest value that 

431 can be expected from ``rtol * abs(y)`` so that `rtol` dominates the 

432 allowable error. If `atol` is larger than ``rtol * abs(y)`` the 

433 number of correct digits is not guaranteed. Conversely, to achieve the 

434 desired `atol` set `rtol` such that ``rtol * abs(y)`` is always smaller 

435 than `atol`. If components of y have different scales, it might be 

436 beneficial to set different `atol` values for different components by 

437 passing array_like with shape (n,) for `atol`. Default values are 

438 1e-3 for `rtol` and 1e-6 for `atol`. 

439 vectorized : bool, optional 

440 Whether `fun` is implemented in a vectorized fashion. Default is False. 

441 

442 Attributes 

443 ---------- 

444 n : int 

445 Number of equations. 

446 status : string 

447 Current status of the solver: 'running', 'finished' or 'failed'. 

448 t_bound : float 

449 Boundary time. 

450 direction : float 

451 Integration direction: +1 or -1. 

452 t : float 

453 Current time. 

454 y : ndarray 

455 Current state. 

456 t_old : float 

457 Previous time. None if no steps were made yet. 

458 step_size : float 

459 Size of the last successful step. None if no steps were made yet. 

460 nfev : int 

461 Number evaluations of the system's right-hand side. 

462 njev : int 

463 Number of evaluations of the Jacobian. Is always 0 for this solver 

464 as it does not use the Jacobian. 

465 nlu : int 

466 Number of LU decompositions. Is always 0 for this solver. 

467 

468 References 

469 ---------- 

470 .. [1] E. Hairer, S. P. Norsett G. Wanner, "Solving Ordinary Differential 

471 Equations I: Nonstiff Problems", Sec. II. 

472 .. [2] `Page with original Fortran code of DOP853 

473 <http://www.unige.ch/~hairer/software.html>`_. 

474 """ 

475 n_stages = dop853_coefficients.N_STAGES 

476 order = 8 

477 error_estimator_order = 7 

478 A = dop853_coefficients.A[:n_stages, :n_stages] 

479 B = dop853_coefficients.B 

480 C = dop853_coefficients.C[:n_stages] 

481 E3 = dop853_coefficients.E3 

482 E5 = dop853_coefficients.E5 

483 D = dop853_coefficients.D 

484 

485 A_EXTRA = dop853_coefficients.A[n_stages + 1:] 

486 C_EXTRA = dop853_coefficients.C[n_stages + 1:] 

487 

488 def __init__(self, fun, t0, y0, t_bound, max_step=np.inf, 

489 rtol=1e-3, atol=1e-6, vectorized=False, 

490 first_step=None, **extraneous): 

491 super().__init__(fun, t0, y0, t_bound, max_step, rtol, atol, 

492 vectorized, first_step, **extraneous) 

493 self.K_extended = np.empty((dop853_coefficients.N_STAGES_EXTENDED, 

494 self.n), dtype=self.y.dtype) 

495 self.K = self.K_extended[:self.n_stages + 1] 

496 

497 def _estimate_error(self, K, h): # Left for testing purposes. 

498 err5 = np.dot(K.T, self.E5) 

499 err3 = np.dot(K.T, self.E3) 

500 denom = np.hypot(np.abs(err5), 0.1 * np.abs(err3)) 

501 correction_factor = np.ones_like(err5) 

502 mask = denom > 0 

503 correction_factor[mask] = np.abs(err5[mask]) / denom[mask] 

504 return h * err5 * correction_factor 

505 

506 def _estimate_error_norm(self, K, h, scale): 

507 err5 = np.dot(K.T, self.E5) / scale 

508 err3 = np.dot(K.T, self.E3) / scale 

509 err5_norm_2 = np.linalg.norm(err5)**2 

510 err3_norm_2 = np.linalg.norm(err3)**2 

511 if err5_norm_2 == 0 and err3_norm_2 == 0: 

512 return 0.0 

513 denom = err5_norm_2 + 0.01 * err3_norm_2 

514 return np.abs(h) * err5_norm_2 / np.sqrt(denom * len(scale)) 

515 

516 def _dense_output_impl(self): 

517 K = self.K_extended 

518 h = self.h_previous 

519 for s, (a, c) in enumerate(zip(self.A_EXTRA, self.C_EXTRA), 

520 start=self.n_stages + 1): 

521 dy = np.dot(K[:s].T, a[:s]) * h 

522 K[s] = self.fun(self.t_old + c * h, self.y_old + dy) 

523 

524 F = np.empty((dop853_coefficients.INTERPOLATOR_POWER, self.n), 

525 dtype=self.y_old.dtype) 

526 

527 f_old = K[0] 

528 delta_y = self.y - self.y_old 

529 

530 F[0] = delta_y 

531 F[1] = h * f_old - delta_y 

532 F[2] = 2 * delta_y - h * (self.f + f_old) 

533 F[3:] = h * np.dot(self.D, K) 

534 

535 return Dop853DenseOutput(self.t_old, self.t, self.y_old, F) 

536 

537 

538class RkDenseOutput(DenseOutput): 

539 def __init__(self, t_old, t, y_old, Q): 

540 super().__init__(t_old, t) 

541 self.h = t - t_old 

542 self.Q = Q 

543 self.order = Q.shape[1] - 1 

544 self.y_old = y_old 

545 

546 def _call_impl(self, t): 

547 x = (t - self.t_old) / self.h 

548 if t.ndim == 0: 

549 p = np.tile(x, self.order + 1) 

550 p = np.cumprod(p) 

551 else: 

552 p = np.tile(x, (self.order + 1, 1)) 

553 p = np.cumprod(p, axis=0) 

554 y = self.h * np.dot(self.Q, p) 

555 if y.ndim == 2: 

556 y += self.y_old[:, None] 

557 else: 

558 y += self.y_old 

559 

560 return y 

561 

562 

563class Dop853DenseOutput(DenseOutput): 

564 def __init__(self, t_old, t, y_old, F): 

565 super().__init__(t_old, t) 

566 self.h = t - t_old 

567 self.F = F 

568 self.y_old = y_old 

569 

570 def _call_impl(self, t): 

571 x = (t - self.t_old) / self.h 

572 

573 if t.ndim == 0: 

574 y = np.zeros_like(self.y_old) 

575 else: 

576 x = x[:, None] 

577 y = np.zeros((len(x), len(self.y_old)), dtype=self.y_old.dtype) 

578 

579 for i, f in enumerate(reversed(self.F)): 

580 y += f 

581 if i % 2 == 0: 

582 y *= x 

583 else: 

584 y *= 1 - x 

585 y += self.y_old 

586 

587 return y.T