adaptive_adams_coefficients.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <boost/config.hpp>
  2. #ifdef BOOST_MSVC
  3. #pragma warning(disable:4996)
  4. #endif
  5. #define BOOST_TEST_MODULE odeint_adaptive_adams_coefficients
  6. #include <boost/test/unit_test.hpp>
  7. #include <boost/numeric/odeint/stepper/detail/adaptive_adams_coefficients.hpp>
  8. #include <vector>
  9. #include <boost/mpl/list.hpp>
  10. #include <boost/mpl/size_t.hpp>
  11. #include <boost/mpl/range_c.hpp>
  12. using namespace boost::unit_test;
  13. using namespace boost::numeric::odeint;
  14. typedef double value_type;
  15. BOOST_AUTO_TEST_SUITE( adaptive_adams_coefficients_test )
  16. typedef boost::mpl::range_c< size_t , 2 , 10 > vector_of_steps;
  17. BOOST_AUTO_TEST_CASE_TEMPLATE( test_step, step_type, vector_of_steps )
  18. {
  19. const static size_t steps = step_type::value;
  20. typedef std::vector<double> deriv_type;
  21. typedef double time_type;
  22. typedef detail::adaptive_adams_coefficients<steps, deriv_type, time_type> aac_type;
  23. std::vector<double> deriv;
  24. deriv.push_back(-1);
  25. time_type t = 0.0;
  26. time_type dt = 0.1;
  27. aac_type coeff;
  28. for(size_t i=0; i<steps; ++i)
  29. {
  30. coeff.predict(t, dt);
  31. coeff.do_step(deriv);
  32. coeff.confirm();
  33. t+= dt;
  34. if(coeff.m_eo < steps)
  35. coeff.m_eo ++;
  36. }
  37. std::vector<value_type> v(10);
  38. v[0] = 1.0/1.0;
  39. v[1] = 1.0/2.0;
  40. v[2] = 5.0/12.0;
  41. v[3] = 9.0/24.0;
  42. v[4] = 251.0/720.0;
  43. v[5] = 95.0/288.0;
  44. v[6] = 19087.0/60480.0;
  45. v[7] = 5257.0/17280.0;
  46. v[8] = 5311869667636789.0/18014398509481984.0;
  47. for(size_t i=0; i<steps; ++i)
  48. {
  49. BOOST_CHECK_SMALL(coeff.beta[1][i] - 1.0, 1e-15);
  50. if(i == 0)
  51. BOOST_CHECK_SMALL(coeff.phi[2][i].m_v[0] + 1, 1e-15);
  52. else if (i == steps-1 && steps%2 == 1)
  53. BOOST_CHECK_SMALL(coeff.phi[2][i].m_v[0] - 1, 1e-14);
  54. else if (i == steps-1 && steps%2 == 0)
  55. BOOST_CHECK_SMALL(coeff.phi[2][i].m_v[0] + 1, 1e-14);
  56. else
  57. BOOST_CHECK_SMALL(coeff.phi[2][i].m_v[0], 1e-15);
  58. BOOST_CHECK_SMALL(coeff.g[i] - v[i], 1e-15);
  59. }
  60. }
  61. BOOST_AUTO_TEST_CASE( test_copy )
  62. {
  63. typedef std::vector<double> deriv_type;
  64. typedef double time_type;
  65. typedef detail::adaptive_adams_coefficients<3, deriv_type, time_type> aac_type;
  66. aac_type c1;
  67. deriv_type deriv(1);
  68. deriv[0] = 1.0;
  69. time_type t = 0.0;
  70. time_type dt = 0.01;
  71. for(size_t i=0; i<3; ++i)
  72. {
  73. c1.predict(t, dt);
  74. c1.do_step(deriv);
  75. c1.confirm();
  76. t+= dt;
  77. if(c1.m_eo < 3)
  78. c1.m_eo ++;
  79. }
  80. aac_type c2(c1);
  81. BOOST_CHECK_EQUAL(c1.phi[0][0].m_v[0], c2.phi[0][0].m_v[0]);
  82. BOOST_CHECK(&(c1.phi[0][0].m_v) != &(c2.phi[0][0].m_v));
  83. aac_type c3;
  84. deriv_type *p1 = &(c3.phi[0][0].m_v);
  85. c3 = c1;
  86. BOOST_CHECK(p1 == (&(c3.phi[0][0].m_v)));
  87. BOOST_CHECK_EQUAL(c1.phi[0][0].m_v[0], c3.phi[0][0].m_v[0]);
  88. }
  89. BOOST_AUTO_TEST_SUITE_END()