generic_stepper.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/generic_stepper.cpp
  4. [begin_description]
  5. This file tests the generic 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_stepper
  19. #include <iostream>
  20. #include <utility>
  21. #include <boost/test/unit_test.hpp>
  22. #include <boost/numeric/odeint/stepper/explicit_generic_rk.hpp>
  23. #include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
  24. #include <boost/numeric/odeint/stepper/runge_kutta4_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_generic_rk< 4 , 4 , state_type> rk_generic_type;
  37. typedef runge_kutta4< state_type > rk4_generic_type;
  38. const boost::array< double , 1 > a1 = {{ 0.5 }};
  39. const boost::array< double , 2 > a2 = {{ 0.0 , 0.5 }};
  40. const boost::array< double , 3 > a3 = {{ 0.0 , 0.0 , 1.0 }};
  41. const rk_generic_type::coef_a_type a = fusion::make_vector( a1 , a2 , a3 );
  42. const rk_generic_type::coef_b_type b = {{ 1.0/6 , 1.0/3 , 1.0/3 , 1.0/6 }};
  43. const rk_generic_type::coef_c_type c = {{ 0.0 , 0.5 , 0.5 , 1.0 }};
  44. typedef runge_kutta4_classic< state_type > rk4_type;
  45. BOOST_AUTO_TEST_SUITE( generic_stepper_test )
  46. BOOST_AUTO_TEST_CASE( test_generic_stepper )
  47. {
  48. //simultaneously test copying
  49. rk_generic_type rk_generic_( a , b , c );
  50. rk_generic_type rk_generic = rk_generic_;
  51. rk4_generic_type rk4_generic_;
  52. rk4_generic_type rk4_generic = rk4_generic_;
  53. //std::cout << stepper;
  54. rk4_type rk4_;
  55. rk4_type rk4 = rk4_;
  56. typedef rk_generic_type::state_type state_type;
  57. typedef rk_generic_type::value_type stepper_value_type;
  58. typedef rk_generic_type::deriv_type deriv_type;
  59. typedef rk_generic_type::time_type time_type;
  60. state_type x = {{ 0.0 , 1.0 }};
  61. state_type y = x;
  62. state_type z = x;
  63. rk_generic.do_step( sys , x , 0.0 , 0.1 );
  64. rk4_generic.do_step( sys , y , 0.0 , 0.1 );
  65. rk4.do_step( sys , z , 0.0 , 0.1 );
  66. BOOST_CHECK_NE( 0.0 , x[0] );
  67. BOOST_CHECK_NE( 1.0 , x[1] );
  68. // compare with analytic solution of above system
  69. BOOST_CHECK_EQUAL( x[0] , y[0] );
  70. BOOST_CHECK_EQUAL( x[1] , y[1] );
  71. BOOST_CHECK_EQUAL( x[0] , z[0] );
  72. BOOST_CHECK_EQUAL( x[1] , z[1] );
  73. }
  74. BOOST_AUTO_TEST_SUITE_END()