adams_bashforth_moulton.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/adams_bashforth_moulton.cpp
  4. [begin_description]
  5. This file tests the use of the Adams-Bashforth-Moulton.
  6. [end_description]
  7. Copyright 2011-2012 Karsten Ahnert
  8. Copyright 2011 Mario Mulansky
  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. #define BOOST_TEST_MODULE odeint_adams_bashforth_moulton
  14. #include <utility>
  15. #include <boost/test/unit_test.hpp>
  16. #include <boost/mpl/list.hpp>
  17. #include <boost/mpl/size_t.hpp>
  18. #include <boost/mpl/range_c.hpp>
  19. #include <boost/numeric/odeint/stepper/adams_bashforth_moulton.hpp>
  20. using namespace boost::unit_test;
  21. using namespace boost::numeric::odeint;
  22. typedef double value_type;
  23. struct lorenz
  24. {
  25. template< class State , class Deriv , class Value >
  26. void operator()( const State &_x , Deriv &_dxdt , const Value &dt ) const
  27. {
  28. const value_type sigma = 10.0;
  29. const value_type R = 28.0;
  30. const value_type b = 8.0 / 3.0;
  31. typename boost::range_iterator< const State >::type x = boost::begin( _x );
  32. typename boost::range_iterator< Deriv >::type dxdt = boost::begin( _dxdt );
  33. dxdt[0] = sigma * ( x[1] - x[0] );
  34. dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
  35. dxdt[2] = x[0]*x[1] - b * x[2];
  36. }
  37. };
  38. BOOST_AUTO_TEST_SUITE( adams_bashforth_moulton_test )
  39. typedef boost::mpl::range_c< size_t , 1 , 6 > vector_of_steps;
  40. BOOST_AUTO_TEST_CASE_TEMPLATE( test_init_and_steps , step_type , vector_of_steps )
  41. {
  42. const static size_t steps = step_type::value;
  43. typedef boost::array< value_type , 3 > state_type;
  44. adams_bashforth_moulton< steps , state_type > stepper;
  45. state_type x = {{ 10.0 , 10.0 , 10.0 }};
  46. const value_type dt = 0.01;
  47. value_type t = 0.0;
  48. stepper.initialize( lorenz() , x , t , dt );
  49. BOOST_CHECK_CLOSE( t , value_type( steps - 1 ) * dt , 1.0e-14 );
  50. stepper.do_step( lorenz() , x , t , dt );
  51. }
  52. BOOST_AUTO_TEST_CASE( test_copying )
  53. {
  54. typedef boost::array< double , 1 > state_type;
  55. typedef adams_bashforth_moulton< 2 , state_type > stepper_type;
  56. stepper_type s1;
  57. stepper_type s2( s1 );
  58. stepper_type s3;
  59. s3 = s1;
  60. }
  61. BOOST_AUTO_TEST_CASE( test_instantiation )
  62. {
  63. typedef boost::array< double , 3 > state_type;
  64. adams_bashforth_moulton< 1 , state_type > s1;
  65. adams_bashforth_moulton< 2 , state_type > s2;
  66. adams_bashforth_moulton< 3 , state_type > s3;
  67. adams_bashforth_moulton< 4 , state_type > s4;
  68. adams_bashforth_moulton< 5 , state_type > s5;
  69. adams_bashforth_moulton< 6 , state_type > s6;
  70. adams_bashforth_moulton< 7 , state_type > s7;
  71. adams_bashforth_moulton< 8 , state_type > s8;
  72. state_type x = {{ 10.0 , 10.0 , 10.0 }};
  73. value_type t = 0.0 , dt = 0.01;
  74. s1.do_step( lorenz() , x , t , dt );
  75. s2.do_step( lorenz() , x , t , dt );
  76. s3.do_step( lorenz() , x , t , dt );
  77. s4.do_step( lorenz() , x , t , dt );
  78. s5.do_step( lorenz() , x , t , dt );
  79. s6.do_step( lorenz() , x , t , dt );
  80. // s7.do_step( lorenz() , x , t , dt );
  81. // s8.do_step( lorenz() , x , t , dt );
  82. }
  83. BOOST_AUTO_TEST_SUITE_END()