generic_error_stepper.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/generic_error_stepper.cpp
  4. [begin_description]
  5. This file tests the generic error stepper.
  6. [end_description]
  7. Copyright 2011 Mario Mulansky
  8. Copyright 2012 Karsten Ahnert
  9. Distributed under the Boost Software License, Version 1.0.
  10. (See accompanying file LICENSE_1_0.txt or
  11. copy at http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. // disable checked iterator warning for msvc
  14. #include <boost/config.hpp>
  15. #ifdef BOOST_MSVC
  16. #pragma warning(disable:4996)
  17. #endif
  18. #define BOOST_TEST_MODULE odeint_generic_error_stepper
  19. #include <iostream>
  20. #include <utility>
  21. #include <boost/test/unit_test.hpp>
  22. #include <boost/numeric/odeint/stepper/explicit_error_generic_rk.hpp>
  23. #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54.hpp>
  24. #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54_classic.hpp>
  25. #include <boost/array.hpp>
  26. using namespace boost::unit_test;
  27. using namespace boost::numeric::odeint;
  28. namespace fusion = boost::fusion;
  29. typedef double value_type;
  30. typedef boost::array< value_type , 2 > state_type;
  31. void sys( const state_type &x , state_type &dxdt , const value_type &t )
  32. {
  33. dxdt[ 0 ] = x[ 0 ] + 2 * x[ 1 ];
  34. dxdt[ 1 ] = x[ 1 ];
  35. }
  36. typedef explicit_error_generic_rk< 6 , 5 , 4 , 5 , state_type> error_rk_generic_type;
  37. const boost::array< double , 1 > a1 = {{ 0.2 }};
  38. const boost::array< double , 2 > a2 = {{ 3.0/40.0 , 9.0/40 }};
  39. const boost::array< double , 3 > a3 = {{ 0.3 , -0.9 , 1.2 }};
  40. const boost::array< double , 4 > a4 = {{ -11.0/54.0 , 2.5 , -70.0/27.0 , 35.0/27.0 }};
  41. const boost::array< double , 5 > a5 = {{ 1631.0/55296.0 , 175.0/512.0 , 575.0/13824.0 , 44275.0/110592.0 , 253.0/4096.0 }};
  42. const error_rk_generic_type::coef_a_type a = fusion::make_vector( a1 , a2 , a3 , a4 , a5 );
  43. const error_rk_generic_type::coef_b_type b = {{ 37.0/378.0 , 0.0 , 250.0/621.0 , 125.0/594.0 , 0.0 , 512.0/1771.0 }};
  44. const error_rk_generic_type::coef_b_type b2 = {{ b[0]-2825.0/27648.0 , b[1]-0.0 , b[2]-18575.0/48384.0 , b[3]-13525.0/55296.0 , b[4]-277.0/14336.0 , b[5]-0.25 }};
  45. const error_rk_generic_type::coef_c_type c = {{ 0.0 , 0.2 , 0.3 , 0.6 , 1.0 , 7.0/8 }};
  46. typedef runge_kutta_cash_karp54< state_type > error_rk54_ck_generic_type;
  47. typedef runge_kutta_cash_karp54_classic< state_type > rk54_ck_type;
  48. BOOST_AUTO_TEST_SUITE( generic_error_stepper_test )
  49. BOOST_AUTO_TEST_CASE( test_generic_error_stepper )
  50. {
  51. //simultaneously test copying
  52. error_rk_generic_type rk_generic_( a , b , b2 , c );
  53. error_rk_generic_type rk_generic = rk_generic_;
  54. error_rk54_ck_generic_type rk54_ck_generic_;
  55. error_rk54_ck_generic_type rk54_ck_generic = rk54_ck_generic_;
  56. //std::cout << stepper;
  57. rk54_ck_type rk54_ck_;
  58. rk54_ck_type rk54_ck = rk54_ck_;
  59. typedef error_rk_generic_type::state_type state_type;
  60. typedef error_rk_generic_type::value_type stepper_value_type;
  61. typedef error_rk_generic_type::deriv_type deriv_type;
  62. typedef error_rk_generic_type::time_type time_type;
  63. state_type x = {{ 0.0 , 1.0 }};
  64. state_type y = x;
  65. state_type z = x;
  66. state_type x_err , y_err , z_err;
  67. rk_generic.do_step( sys , x , 0.0 , 0.1 , x_err );
  68. rk54_ck_generic.do_step( sys , y , 0.0 , 0.1 , y_err);
  69. rk54_ck.do_step( sys , z , 0.0 , 0.1 , z_err );
  70. BOOST_CHECK_NE( 0.0 , x[0] );
  71. BOOST_CHECK_NE( 1.0 , x[1] );
  72. BOOST_CHECK_NE( 0.0 , x_err[0] );
  73. BOOST_CHECK_NE( 0.0 , x_err[1] );
  74. // compare with analytic solution of above system
  75. BOOST_CHECK_EQUAL( x[0] , y[0] );
  76. BOOST_CHECK_EQUAL( x[1] , y[1] );
  77. BOOST_CHECK_EQUAL( x[0] , z[0] );
  78. BOOST_CHECK_EQUAL( x[1] , z[1] );
  79. }
  80. BOOST_AUTO_TEST_SUITE_END()