stepper_with_ranges.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/stepper_with_ranges.cpp
  4. [begin_description]
  5. This file tests if the steppers play well with Boost.Range.
  6. [end_description]
  7. Copyright 2011-2012 Karsten Ahnert
  8. Copyright 2011-2013 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. // 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_stepper_with_ranges
  19. #include <boost/test/unit_test.hpp>
  20. #include <vector>
  21. #include <utility>
  22. #include <iostream>
  23. #include <boost/array.hpp>
  24. #include <boost/range.hpp>
  25. #include <boost/ref.hpp>
  26. #include <boost/numeric/odeint/stepper/euler.hpp>
  27. #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54_classic.hpp>
  28. #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
  29. #include <boost/numeric/odeint/stepper/controlled_runge_kutta.hpp>
  30. #include <boost/numeric/odeint/stepper/symplectic_euler.hpp>
  31. #include <boost/numeric/odeint/stepper/dense_output_runge_kutta.hpp>
  32. typedef std::vector< double > state_type;
  33. typedef boost::array< double , 3 > state_type2;
  34. /* explicitly force range algebra for this array! */
  35. namespace boost { namespace numeric { namespace odeint {
  36. template<>
  37. struct algebra_dispatcher< state_type2 >
  38. { typedef range_algebra algebra_type; };
  39. } } }
  40. /*
  41. * The two systems are needed, since for steppers with more than
  42. * one internal step it is difficult to calculate the exact result
  43. *
  44. * system1 is suited for euler
  45. */
  46. struct system1
  47. {
  48. template< class State , class Deriv >
  49. void operator()( const State &x_ , Deriv &dxdt_ , double t )
  50. {
  51. typename boost::range_iterator< const State >::type x = boost::begin( x_ );
  52. typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );
  53. dxdt[0] = x[0];
  54. dxdt[1] = 2.0;
  55. dxdt[2] = 3.0;
  56. }
  57. template< class State , class Deriv >
  58. void operator()( const State &x_ , const Deriv &dxdt_ , double t )
  59. {
  60. typename boost::range_iterator< const State >::type x = boost::begin( x_ );
  61. typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );
  62. dxdt[0] = x[0];
  63. dxdt[1] = 2.0;
  64. dxdt[2] = 3.0;
  65. }
  66. };
  67. /*
  68. * system2 is suited for all steppers, it allows you to calculate the result analytically.
  69. */
  70. struct system2
  71. {
  72. template< class State , class Deriv >
  73. void operator()( const State &x_ , Deriv &dxdt_ , double t )
  74. {
  75. typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );
  76. dxdt[0] = 1.0;
  77. dxdt[1] = 2.0;
  78. dxdt[2] = 3.0;
  79. }
  80. template< class State , class Deriv >
  81. void operator()( const State &x_ , const Deriv &dxdt_ , double t )
  82. {
  83. typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );
  84. dxdt[0] = 1.0;
  85. dxdt[1] = 2.0;
  86. dxdt[2] = 3.0;
  87. }
  88. };
  89. /*
  90. * Useful for Hamiltonian systems
  91. */
  92. struct ham_sys
  93. {
  94. template< class State , class Deriv >
  95. void operator()( const State &x_ , Deriv &dxdt_ )
  96. {
  97. typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );
  98. dxdt[0] = 1.0;
  99. dxdt[1] = 2.0;
  100. dxdt[2] = 3.0;
  101. }
  102. template< class State , class Deriv >
  103. void operator()( const State &x_ , const Deriv &dxdt_ )
  104. {
  105. typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ );
  106. dxdt[0] = 1.0;
  107. dxdt[1] = 2.0;
  108. dxdt[2] = 3.0;
  109. }
  110. };
  111. struct vector_fixture
  112. {
  113. const static size_t dim = 6;
  114. boost::array< double , dim > in;
  115. boost::array< double , dim > q;
  116. boost::array< double , dim > p;
  117. state_type err;
  118. vector_fixture( void )
  119. : in() , err( 3 )
  120. {
  121. for( size_t i=0 ; i<dim ; ++i )
  122. {
  123. in[ i ] = q[i] = p[i] = double( i );
  124. }
  125. for( size_t i=0 ; i<3 ; ++i )
  126. {
  127. err[i] = double( i ) * 10.0;
  128. }
  129. }
  130. ~vector_fixture( void )
  131. {
  132. }
  133. };
  134. #define CHECK_VALUES( x , x0 , x1 , x2 , x3 , x4 , x5 ) \
  135. BOOST_CHECK_CLOSE( x[0] , x0 , 1.0e-8 ); \
  136. BOOST_CHECK_CLOSE( x[1] , x1 , 1.0e-8 ); \
  137. BOOST_CHECK_CLOSE( x[2] , x2 , 1.0e-8 ); \
  138. BOOST_CHECK_CLOSE( x[3] , x3 , 1.0e-8 ); \
  139. BOOST_CHECK_CLOSE( x[4] , x4 , 1.0e-8 ); \
  140. BOOST_CHECK_CLOSE( x[5] , x5 , 1.0e-8 )
  141. BOOST_AUTO_TEST_SUITE( stepper_with_ranges )
  142. BOOST_AUTO_TEST_CASE( explicit_euler_with_range_v1 )
  143. {
  144. vector_fixture f;
  145. boost::numeric::odeint::euler< state_type > euler;
  146. euler.do_step( system1() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , 0.1 , 0.1 );
  147. CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
  148. }
  149. BOOST_AUTO_TEST_CASE( explicit_error_k54_with_range_v1 )
  150. {
  151. vector_fixture f;
  152. boost::numeric::odeint::runge_kutta_cash_karp54_classic< state_type > rk54;
  153. rk54.do_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , 0.1 , 0.1 );
  154. CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
  155. }
  156. BOOST_AUTO_TEST_CASE( explicit_error_k54_with_range_v5 )
  157. {
  158. vector_fixture f;
  159. boost::numeric::odeint::runge_kutta_cash_karp54_classic< state_type > rk54;
  160. rk54.do_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , 0.1 , 0.1 , f.err );
  161. CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
  162. }
  163. BOOST_AUTO_TEST_CASE( runge_kutta_dopri5_with_range_v1 )
  164. {
  165. vector_fixture f;
  166. boost::numeric::odeint::runge_kutta_dopri5< state_type > dopri5;
  167. dopri5.do_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , 0.1 , 0.1 );
  168. CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
  169. }
  170. BOOST_AUTO_TEST_CASE( runge_kutta_dopri5_with_range_v5 )
  171. {
  172. vector_fixture f;
  173. boost::numeric::odeint::runge_kutta_dopri5< state_type > dopri5;
  174. dopri5.do_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , 0.1 , 0.1 , f.err );
  175. CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
  176. }
  177. BOOST_AUTO_TEST_CASE( controlled_error_stepper_rk54 )
  178. {
  179. double t = 0.0 , dt = 0.1;
  180. vector_fixture f;
  181. boost::numeric::odeint::controlled_runge_kutta< boost::numeric::odeint::runge_kutta_cash_karp54_classic< state_type > > stepper;
  182. stepper.try_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , t , dt );
  183. CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
  184. }
  185. BOOST_AUTO_TEST_CASE( controlled_error_stepper_dopri5 )
  186. {
  187. double t = 0.0 , dt = 0.1;
  188. vector_fixture f;
  189. boost::numeric::odeint::controlled_runge_kutta< boost::numeric::odeint::runge_kutta_dopri5< state_type > > stepper;
  190. stepper.try_step( system2() , std::make_pair( f.in.begin() + 1 , f.in.begin() + 4 ) , t , dt );
  191. CHECK_VALUES( f.in , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
  192. }
  193. BOOST_AUTO_TEST_CASE( symplectic_euler_coor_func )
  194. {
  195. vector_fixture f;
  196. boost::numeric::odeint::symplectic_euler< state_type > euler;
  197. euler.do_step( ham_sys() ,
  198. std::make_pair( f.q.begin() + 1 , f.q.begin() + 4 ) ,
  199. std::make_pair( f.p.begin() + 3 , f.p.begin() + 6 ) ,
  200. 0.0 , 0.1 );
  201. CHECK_VALUES( f.q , 0.0 , 1.3 , 2.4 , 3.5 , 4.0 , 5.0 );
  202. CHECK_VALUES( f.p , 0.0 , 1.0 , 2.0 , 3.1 , 4.2 , 5.3 );
  203. }
  204. BOOST_AUTO_TEST_CASE( symplectic_euler_coor_and_mom_func )
  205. {
  206. vector_fixture f;
  207. boost::numeric::odeint::symplectic_euler< state_type > euler;
  208. euler.do_step( std::make_pair( ham_sys() , ham_sys() ) ,
  209. std::make_pair( f.q.begin() + 1 , f.q.begin() + 4 ) ,
  210. std::make_pair( f.p.begin() + 3 , f.p.begin() + 6 ) ,
  211. 0.0 , 0.1 );
  212. CHECK_VALUES( f.q , 0.0 , 1.1 , 2.2 , 3.3 , 4.0 , 5.0 );
  213. CHECK_VALUES( f.p , 0.0 , 1.0 , 2.0 , 3.1 , 4.2 , 5.3 );
  214. }
  215. BOOST_AUTO_TEST_CASE( dense_output_euler_with_ranges )
  216. {
  217. using namespace boost::numeric::odeint;
  218. vector_fixture f;
  219. dense_output_runge_kutta< euler< state_type > > stepper;
  220. stepper.initialize( std::make_pair( f.in.begin() + 1, f.in.begin() + 4 ) , 0.0 , 0.1 );
  221. stepper.do_step( system1() );
  222. stepper.calc_state( 0.05 , std::make_pair( f.in.begin() + 1 ,f.in.begin() +4 ) );
  223. CHECK_VALUES( f.in , 0.0 , 1.05 , 2.1 , 3.15 , 4.0 , 5.0 );
  224. }
  225. BOOST_AUTO_TEST_CASE( dense_output_dopri5_with_ranges )
  226. {
  227. using namespace boost::numeric::odeint;
  228. vector_fixture f;
  229. dense_output_runge_kutta<
  230. controlled_runge_kutta<
  231. runge_kutta_dopri5< state_type >
  232. > > stepper;
  233. stepper.initialize( std::make_pair( f.in.begin() + 1, f.in.begin() + 4 ) , 0.0 , 0.1 );
  234. stepper.do_step( system2() );
  235. stepper.calc_state( 0.05 , std::make_pair( f.in.begin() + 1 ,f.in.begin() +4 ) );
  236. CHECK_VALUES( f.in , 0.0 , 1.05 , 2.1 , 3.15 , 4.0 , 5.0 );
  237. }
  238. BOOST_AUTO_TEST_SUITE_END()