bulirsch_stoer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/bulirsch_stoer.cpp
  4. [begin_description]
  5. This file tests the Bulirsch-Stoer 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_bulirsch_stoer
  19. #include <utility>
  20. #include <iostream>
  21. #include <boost/array.hpp>
  22. #include <boost/test/unit_test.hpp>
  23. #include <boost/numeric/odeint/stepper/bulirsch_stoer.hpp>
  24. #include <boost/numeric/odeint/stepper/bulirsch_stoer_dense_out.hpp>
  25. #include <boost/numeric/odeint/integrate/integrate_adaptive.hpp>
  26. using namespace boost::unit_test;
  27. using namespace boost::numeric::odeint;
  28. typedef double value_type;
  29. typedef boost::array< value_type , 3 > state_type;
  30. const double sigma = 10.0;
  31. const double R = 28.0;
  32. const double b = 8.0 / 3.0;
  33. struct lorenz
  34. {
  35. template< class State , class Deriv >
  36. void operator()( const State &x , Deriv &dxdt , double t ) const
  37. {
  38. dxdt[0] = sigma * ( x[1] - x[0] );
  39. dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
  40. dxdt[2] = -b * x[2] + x[0] * x[1];
  41. }
  42. };
  43. struct const_system
  44. {
  45. template< class State , class Deriv >
  46. void operator()( const State &x , Deriv &dxdt , double t ) const
  47. {
  48. dxdt[0] = 1.0;
  49. dxdt[1] = 1.0;
  50. dxdt[2] = 1.0;
  51. }
  52. };
  53. struct sin_system
  54. {
  55. template< class State , class Deriv >
  56. void operator()( const State &x , Deriv &dxdt , double t ) const
  57. {
  58. dxdt[0] = sin( x[0] );
  59. dxdt[1] = cos( x[1] );
  60. dxdt[2] = sin( x[2] ) + cos( x[2] );
  61. }
  62. };
  63. BOOST_AUTO_TEST_SUITE( bulirsch_stoer_test )
  64. BOOST_AUTO_TEST_CASE( test_bulirsch_stoer )
  65. {
  66. typedef bulirsch_stoer< state_type > stepper_type;
  67. stepper_type stepper( 1E-9 , 1E-9 , 1.0 , 0.0 );
  68. state_type x;
  69. x[0] = 10.0 ; x[1] = 10.0 ; x[2] = 5.0;
  70. double dt = 0.1;
  71. //stepper.try_step( lorenz() , x , t , dt );
  72. std::cout << "starting integration..." << std::endl;
  73. size_t steps = integrate_adaptive( stepper , lorenz() , x , 0.0 , 10.0 , dt );
  74. std::cout << "required steps: " << steps << std::endl;
  75. bulirsch_stoer_dense_out< state_type > bs_do( 1E-9 , 1E-9 , 1.0 , 0.0 );
  76. x[0] = 10.0 ; x[1] = 10.0 ; x[2] = 5.0;
  77. double t = 0.0;
  78. dt = 1E-1;
  79. bs_do.initialize( x , t , dt );
  80. bs_do.do_step( sin_system() );
  81. std::cout << "one step successful, new time: " << bs_do.current_time() << " (" << t << ")" << std::endl;
  82. x = bs_do.current_state();
  83. std::cout << "x( " << bs_do.current_time() << " ) = [ " << x[0] << " , " << x[1] << " , " << x[2] << " ]" << std::endl;
  84. bs_do.calc_state( bs_do.current_time()/3 , x );
  85. std::cout << "x( " << bs_do.current_time()/3 << " ) = [ " << x[0] << " , " << x[1] << " , " << x[2] << " ]" << std::endl;
  86. std::cout << std::endl << "=======================================================================" << std::endl << std::endl;
  87. x[0] = 10.0 ; x[1] = 10.0 ; x[2] = 5.0;
  88. t = 0.0; dt /= 3;
  89. bs_do.initialize( x , t , dt );
  90. bs_do.do_step( sin_system() );
  91. x = bs_do.current_state();
  92. std::cout << "x( " << bs_do.current_time() << " ) = [ " << x[0] << " , " << x[1] << " , " << x[2] << " ]" << std::endl;
  93. t = dt;
  94. bs_do.initialize( x , t , dt );
  95. bs_do.do_step( sin_system() );
  96. x = bs_do.current_state();
  97. t = 2*dt;
  98. bs_do.initialize( x , t , dt );
  99. bs_do.do_step( sin_system() );
  100. x = bs_do.current_state();
  101. std::cout << "x( " << bs_do.current_time() << " ) = [ " << x[0] << " , " << x[1] << " , " << x[2] << " ]" << std::endl << std::endl << std::endl;
  102. }
  103. BOOST_AUTO_TEST_CASE( test_bulirsch_stoer_adjust_size )
  104. {
  105. typedef bulirsch_stoer< state_type > stepper_type;
  106. stepper_type stepper( 1E-9 , 1E-9 , 1.0 , 0.0 );
  107. state_type x; x[0] = 10.0 ; x[1] = 10.0 ; x[2] = 5.0;
  108. stepper.adjust_size( x );
  109. double dt = 0.1;
  110. size_t steps = integrate_adaptive( stepper , lorenz() , x , 0.0 , 10.0 , dt );
  111. std::cout << "required steps: " << steps << std::endl;
  112. }
  113. BOOST_AUTO_TEST_SUITE_END()