test_integrator.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import unittest
  2. import numpy as np
  3. from swiftt import integrators
  4. from swiftt.taylor import taylor_map, real_multivar_taylor, factory_taylor
  5. from swiftt.math_algebra import cos
  6. tol_coeff = 1.e-12
  7. null_expansion_2var_order2 = factory_taylor.zero_expansion(2, 2)
  8. null_expansion_2var_order3 = factory_taylor.zero_expansion(2, 3)
  9. null_expansion_3var_order2 = factory_taylor.zero_expansion(3, 2)
  10. null_expansion_4var_order5 = factory_taylor.zero_expansion(4, 5)
  11. def test_integrator_history(integ):
  12. coeff = np.zeros(null_expansion_2var_order3.dim_alg)
  13. coeff[1] = 1.
  14. x0 = [null_expansion_2var_order3.create_expansion_with_coeff(coeff)]
  15. coeff[1], coeff[2] = 0., 1.
  16. x0.append(null_expansion_2var_order3.create_expansion_with_coeff(coeff))
  17. xf1 = integ.integrate(0., 1., x0, 10, keep_history=True)[0][-1]
  18. xf2 = integ.integrate(0., 1., x0, 10, keep_history=False)[0][-1]
  19. for el1, el2 in zip(xf1, xf2):
  20. if el1 != el2:
  21. return False
  22. return True
  23. class TestIntegrator(unittest.TestCase):
  24. def test_propag_dim2(self):
  25. order = 4
  26. expansions = factory_taylor.create_unknown_map(order, [1., 0.])
  27. tf = 1.
  28. n_steps = 20
  29. def func(tau, x):
  30. return taylor_map.RealTaylorMap([x[1], 2. * (tau * x[1] + x[0])])
  31. integ = integrators.ABM8(func) # RKF45(func, max_stepsize=None, step_multiplier=None)
  32. states, times = integ.integrate(0., tf, expansions, n_steps, keep_history=False)
  33. if np.fabs(states[-1][0].const - np.exp(tf * tf)) > 1e-4:
  34. self.fail()
  35. def test_integ_euler(self):
  36. if not test_integrator_history(integrators.Euler(lambda t, x: np.array([cos(x[1]), -x[0]],
  37. dtype=real_multivar_taylor.RealMultivarTaylor))):
  38. self.fail()
  39. def test_integ_heun(self):
  40. if not test_integrator_history(integrators.Heun(lambda t, x: np.array([cos(x[1]), -x[0]],
  41. dtype=real_multivar_taylor.RealMultivarTaylor))):
  42. self.fail()
  43. def test_integ_rk4(self):
  44. if not test_integrator_history(integrators.RK4(lambda t, x: np.array([cos(x[1]), -x[0]],
  45. dtype=real_multivar_taylor.RealMultivarTaylor))):
  46. self.fail()
  47. def test_integ_bs(self):
  48. if not test_integrator_history(integrators.BS(lambda t, x: np.array([cos(x[1]), -x[0]],
  49. dtype=real_multivar_taylor.RealMultivarTaylor), 4)):
  50. self.fail()
  51. def test_integ_ab8(self):
  52. if not test_integrator_history(integrators.AB8(lambda t, x: np.array([cos(x[1]), -x[0]],
  53. dtype=real_multivar_taylor.RealMultivarTaylor))):
  54. self.fail()
  55. def test_integ_abm8(self):
  56. if not test_integrator_history(integrators.ABM8(lambda t, x: np.array([cos(x[1]), -x[0]],
  57. dtype=real_multivar_taylor.RealMultivarTaylor))):
  58. self.fail()
  59. def test_integ_rkf45(self):
  60. if not test_integrator_history(integrators.RKF45(lambda t, x: np.array([cos(x[1]), -x[0]],
  61. dtype=real_multivar_taylor.RealMultivarTaylor), 2)):
  62. self.fail()
  63. def test_integ_rkf45_event(self):
  64. if not test_integrator_history(integrators.RKF45(lambda t, x: np.array([cos(x[1]), -x[0]],
  65. dtype=real_multivar_taylor.RealMultivarTaylor), 2,
  66. event_func=lambda t, x: t > 1000., tol_event=0.1)):
  67. self.fail()
  68. #
  69. # def test_event_detec(self):
  70. #
  71. # t0 = 0.
  72. # tf = 10.
  73. # x0 = np.array([0.5, 1.])
  74. # n_steps = int((tf - t0) / 0.01)
  75. # tol_event = 0.01
  76. #
  77. # def fun(t, x):
  78. # return np.array([x[1], cos(x[0])])
  79. #
  80. # def event(t, x):
  81. # return x[0] < 0.
  82. #
  83. # inteRKF45 = integrators.RKF45(fun, 2, event_func=event, tol_event=tol_event)
  84. # statesRKF45, timesRKF45 = inteRKF45.integrate(t0, tf, x0, n_steps, keep_history=False)
  85. #
  86. # inteTaylor = integrators_algebra.TaylorVarsize(fun, order=4, dim_state=2, event_func=event,
  87. # tol_event=tol_event)
  88. # statesTaylor, timesTaylor = inteTaylor.integrate(t0, tf, x0, n_steps, keep_history=False)
  89. #
  90. # if not np.allclose(timesRKF45[-1], timesTaylor[-1], atol=1.e-2, rtol=1.e-3):
  91. # self.fail()
  92. if __name__ == '__main__':
  93. unittest.main()