test_rk.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import pytest
  2. from numpy.testing import assert_allclose, assert_
  3. import numpy as np
  4. from scipy.integrate import RK23, RK45, DOP853
  5. from scipy.integrate._ivp import dop853_coefficients
  6. @pytest.mark.parametrize("solver", [RK23, RK45, DOP853])
  7. def test_coefficient_properties(solver):
  8. assert_allclose(np.sum(solver.B), 1, rtol=1e-15)
  9. assert_allclose(np.sum(solver.A, axis=1), solver.C, rtol=1e-14)
  10. def test_coefficient_properties_dop853():
  11. assert_allclose(np.sum(dop853_coefficients.B), 1, rtol=1e-15)
  12. assert_allclose(np.sum(dop853_coefficients.A, axis=1),
  13. dop853_coefficients.C,
  14. rtol=1e-14)
  15. @pytest.mark.parametrize("solver_class", [RK23, RK45, DOP853])
  16. def test_error_estimation(solver_class):
  17. step = 0.2
  18. solver = solver_class(lambda t, y: y, 0, [1], 1, first_step=step)
  19. solver.step()
  20. error_estimate = solver._estimate_error(solver.K, step)
  21. error = solver.y - np.exp([step])
  22. assert_(np.abs(error) < np.abs(error_estimate))
  23. @pytest.mark.parametrize("solver_class", [RK23, RK45, DOP853])
  24. def test_error_estimation_complex(solver_class):
  25. h = 0.2
  26. solver = solver_class(lambda t, y: 1j * y, 0, [1j], 1, first_step=h)
  27. solver.step()
  28. err_norm = solver._estimate_error_norm(solver.K, h, scale=[1])
  29. assert np.isrealobj(err_norm)