stepper_with_units.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/stepper_with_units.cpp
  4. [begin_description]
  5. This file tests if the steppers play well with Boost.Units.
  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. #define BOOST_TEST_MODULE odeint_stepper_with_units
  14. // the runge-kutta 78 stepper invoked with boost units requires increased fusion macro variables!
  15. // note that by default the rk78 + units test case is disabled as it requires enormous memory when compiling (5 GB)
  16. #define BOOST_FUSION_INVOKE_MAX_ARITY 15
  17. #define BOOST_RESULT_OF_NUM_ARGS 15
  18. #include <boost/numeric/odeint/config.hpp>
  19. #include <boost/test/unit_test.hpp>
  20. #include <boost/units/systems/si/length.hpp>
  21. #include <boost/units/systems/si/time.hpp>
  22. #include <boost/units/systems/si/velocity.hpp>
  23. #include <boost/units/systems/si/acceleration.hpp>
  24. #include <boost/units/systems/si/io.hpp>
  25. #include <boost/fusion/container.hpp>
  26. #include <boost/mpl/vector.hpp>
  27. #include <boost/numeric/odeint/stepper/euler.hpp>
  28. #include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
  29. #include <boost/numeric/odeint/stepper/runge_kutta4_classic.hpp>
  30. #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54.hpp>
  31. #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54_classic.hpp>
  32. #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
  33. #include <boost/numeric/odeint/stepper/runge_kutta_fehlberg78.hpp>
  34. #include <boost/numeric/odeint/stepper/controlled_runge_kutta.hpp>
  35. #include <boost/numeric/odeint/stepper/dense_output_runge_kutta.hpp>
  36. #include <boost/numeric/odeint/stepper/bulirsch_stoer.hpp>
  37. #include <boost/numeric/odeint/stepper/bulirsch_stoer_dense_out.hpp>
  38. #include <boost/numeric/odeint/algebra/fusion_algebra.hpp>
  39. #include <boost/numeric/odeint/algebra/fusion_algebra_dispatcher.hpp>
  40. using namespace boost::numeric::odeint;
  41. using namespace boost::unit_test;
  42. namespace mpl = boost::mpl;
  43. namespace fusion = boost::fusion;
  44. namespace units = boost::units;
  45. namespace si = boost::units::si;
  46. typedef double value_type;
  47. typedef units::quantity< si::time , value_type > time_type;
  48. typedef units::quantity< si::length , value_type > length_type;
  49. typedef units::quantity< si::velocity , value_type > velocity_type;
  50. typedef units::quantity< si::acceleration , value_type > acceleration_type;
  51. typedef fusion::vector< length_type , velocity_type > state_type;
  52. typedef fusion::vector< velocity_type , acceleration_type > deriv_type;
  53. void oscillator( const state_type &x , deriv_type &dxdt , time_type t )
  54. {
  55. const units::quantity< si::frequency , value_type > omega = 1.0 * si::hertz;
  56. fusion::at_c< 0 >( dxdt ) = fusion::at_c< 1 >( x );
  57. fusion::at_c< 1 >( dxdt ) = - omega * omega * fusion::at_c< 0 >( x );
  58. }
  59. template< class Stepper >
  60. void check_stepper( Stepper &stepper )
  61. {
  62. typedef Stepper stepper_type;
  63. typedef typename stepper_type::state_type state_type;
  64. typedef typename stepper_type::value_type value_type;
  65. typedef typename stepper_type::deriv_type deriv_type;
  66. typedef typename stepper_type::time_type time_type;
  67. typedef typename stepper_type::order_type order_type;
  68. typedef typename stepper_type::algebra_type algebra_type;
  69. typedef typename stepper_type::operations_type operations_type;
  70. const time_type t( 0.0 * si::second );
  71. time_type dt( 0.1 * si::second );
  72. state_type x( 1.0 * si::meter , 0.0 * si::meter_per_second );
  73. // test call method one
  74. stepper.do_step( oscillator , x , t , dt );
  75. // test call method two
  76. stepper.do_step( oscillator , x , t , x , dt );
  77. // test call method three
  78. deriv_type dxdt;
  79. oscillator( x , dxdt , t );
  80. stepper.do_step( oscillator , x , dxdt , t , dt );
  81. // test call method four
  82. oscillator( x , dxdt , t );
  83. stepper.do_step( oscillator , x , dxdt , t , x , dt );
  84. }
  85. template< class Stepper >
  86. void check_fsal_stepper( Stepper &stepper )
  87. {
  88. typedef Stepper stepper_type;
  89. typedef typename stepper_type::state_type state_type;
  90. typedef typename stepper_type::value_type value_type;
  91. typedef typename stepper_type::deriv_type deriv_type;
  92. typedef typename stepper_type::time_type time_type;
  93. typedef typename stepper_type::order_type order_type;
  94. typedef typename stepper_type::algebra_type algebra_type;
  95. typedef typename stepper_type::operations_type operations_type;
  96. const time_type t( 0.0 * si::second );
  97. time_type dt( 0.1 * si::second );
  98. state_type x( 1.0 * si::meter , 0.0 * si::meter_per_second );
  99. // test call method one
  100. stepper.do_step( oscillator , x , t , dt );
  101. // test call method two
  102. stepper.do_step( oscillator , x , t , x , dt );
  103. // test call method three
  104. deriv_type dxdt;
  105. oscillator( x , dxdt , t );
  106. stepper.do_step( oscillator , x , dxdt , t , dt );
  107. // test call method four
  108. stepper.do_step( oscillator , x , dxdt , t , x , dxdt , dt );
  109. }
  110. template< class Stepper >
  111. void check_error_stepper( Stepper &stepper )
  112. {
  113. typedef Stepper stepper_type;
  114. typedef typename stepper_type::state_type state_type;
  115. typedef typename stepper_type::value_type value_type;
  116. typedef typename stepper_type::deriv_type deriv_type;
  117. typedef typename stepper_type::time_type time_type;
  118. typedef typename stepper_type::order_type order_type;
  119. typedef typename stepper_type::algebra_type algebra_type;
  120. typedef typename stepper_type::operations_type operations_type;
  121. const time_type t( 0.0 * si::second );
  122. time_type dt( 0.1 * si::second );
  123. state_type x( 1.0 * si::meter , 0.0 * si::meter_per_second ) , xerr;
  124. // test call method one
  125. stepper.do_step( oscillator , x , t , dt , xerr );
  126. // test call method two
  127. stepper.do_step( oscillator , x , t , x , dt , xerr );
  128. // test call method three
  129. deriv_type dxdt;
  130. oscillator( x , dxdt , t );
  131. stepper.do_step( oscillator , x , dxdt , t , dt , xerr );
  132. // test call method four
  133. stepper.do_step( oscillator , x , dxdt , t , x , dt , xerr );
  134. }
  135. template< class Stepper >
  136. void check_fsal_error_stepper( Stepper &stepper )
  137. {
  138. typedef Stepper stepper_type;
  139. typedef typename stepper_type::state_type state_type;
  140. typedef typename stepper_type::value_type value_type;
  141. typedef typename stepper_type::deriv_type deriv_type;
  142. typedef typename stepper_type::time_type time_type;
  143. typedef typename stepper_type::order_type order_type;
  144. typedef typename stepper_type::algebra_type algebra_type;
  145. typedef typename stepper_type::operations_type operations_type;
  146. const time_type t( 0.0 * si::second );
  147. time_type dt( 0.1 * si::second );
  148. state_type x( 1.0 * si::meter , 0.0 * si::meter_per_second ) , xerr;
  149. // test call method one
  150. stepper.do_step( oscillator , x , t , dt , xerr );
  151. // test call method two
  152. stepper.do_step( oscillator , x , t , x , dt , xerr );
  153. // test call method three
  154. deriv_type dxdt;
  155. oscillator( x , dxdt , t );
  156. stepper.do_step( oscillator , x , dxdt , t , dt , xerr );
  157. // test call method four
  158. stepper.do_step( oscillator , x , dxdt , t , x , dxdt , dt , xerr );
  159. }
  160. template< class Stepper >
  161. void check_controlled_stepper( Stepper &stepper )
  162. {
  163. typedef Stepper stepper_type;
  164. typedef typename stepper_type::state_type state_type;
  165. typedef typename stepper_type::value_type value_type;
  166. typedef typename stepper_type::deriv_type deriv_type;
  167. typedef typename stepper_type::time_type time_type;
  168. time_type t( 0.0 * si::second );
  169. time_type dt( 0.1 * si::second );
  170. state_type x( 1.0 * si::meter , 0.0 * si::meter_per_second );
  171. // test call method one
  172. stepper.try_step( oscillator , x , t , dt );
  173. }
  174. template< class Stepper >
  175. void check_dense_output_stepper( Stepper &stepper )
  176. {
  177. typedef Stepper stepper_type;
  178. typedef typename stepper_type::state_type state_type;
  179. typedef typename stepper_type::value_type value_type;
  180. typedef typename stepper_type::deriv_type deriv_type;
  181. typedef typename stepper_type::time_type time_type;
  182. // typedef typename stepper_type::order_type order_type;
  183. time_type t( 0.0 * si::second );
  184. time_type dt( 0.1 * si::second );
  185. state_type x( 1.0 * si::meter , 0.0 * si::meter_per_second ) , x2;
  186. stepper.initialize( x , t , dt );
  187. stepper.do_step( oscillator );
  188. stepper.calc_state( dt / 2.0 , x2 );
  189. }
  190. class stepper_types : public mpl::vector
  191. <
  192. euler< state_type , value_type , deriv_type , time_type >,
  193. runge_kutta4< state_type , value_type , deriv_type , time_type > ,
  194. runge_kutta4_classic< state_type , value_type , deriv_type , time_type > ,
  195. runge_kutta_cash_karp54< state_type , value_type , deriv_type , time_type >,
  196. runge_kutta_cash_karp54_classic< state_type , value_type , deriv_type , time_type >
  197. // don't run rk78 test - gcc requires > 5GB RAM to compile this
  198. //, runge_kutta_fehlberg78< state_type , value_type , deriv_type , time_type >
  199. > { };
  200. class fsal_stepper_types : public mpl::vector
  201. <
  202. runge_kutta_dopri5< state_type , value_type , deriv_type , time_type >
  203. > { };
  204. class error_stepper_types : public mpl::vector
  205. <
  206. runge_kutta_cash_karp54_classic< state_type , value_type , deriv_type , time_type >
  207. //, runge_kutta_fehlberg78< state_type , value_type , deriv_type , time_type >
  208. > { };
  209. class fsal_error_stepper_types : public mpl::vector
  210. <
  211. runge_kutta_dopri5< state_type , value_type , deriv_type , time_type >
  212. > { };
  213. class controlled_stepper_types : public mpl::vector
  214. <
  215. controlled_runge_kutta< runge_kutta_cash_karp54_classic< state_type , value_type , deriv_type , time_type > > ,
  216. controlled_runge_kutta< runge_kutta_dopri5< state_type , value_type , deriv_type , time_type > >
  217. , bulirsch_stoer< state_type , value_type , deriv_type , time_type >
  218. // rk78 with units needs up to 3GB memory to compile - disable testing...
  219. //, controlled_runge_kutta< runge_kutta_fehlberg78< state_type , value_type , deriv_type , time_type > >
  220. > { };
  221. class dense_output_stepper_types : public mpl::vector
  222. <
  223. dense_output_runge_kutta< euler< state_type , value_type , deriv_type , time_type > > ,
  224. dense_output_runge_kutta<
  225. controlled_runge_kutta< runge_kutta_dopri5< state_type , value_type , deriv_type , time_type > > >
  226. //, bulirsch_stoer_dense_out< state_type , value_type , deriv_type , time_type >
  227. > { };
  228. BOOST_AUTO_TEST_SUITE( stepper_with_units )
  229. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_test , Stepper , stepper_types )
  230. {
  231. Stepper stepper;
  232. check_stepper( stepper );
  233. }
  234. BOOST_AUTO_TEST_CASE_TEMPLATE( fsl_stepper_test , Stepper , fsal_stepper_types )
  235. {
  236. Stepper stepper;
  237. check_fsal_stepper( stepper );
  238. }
  239. BOOST_AUTO_TEST_CASE_TEMPLATE( error_stepper_test , Stepper , error_stepper_types )
  240. {
  241. Stepper stepper;
  242. check_error_stepper( stepper );
  243. }
  244. BOOST_AUTO_TEST_CASE_TEMPLATE( fsal_error_stepper_test , Stepper , fsal_error_stepper_types )
  245. {
  246. Stepper stepper;
  247. check_fsal_error_stepper( stepper );
  248. }
  249. BOOST_AUTO_TEST_CASE_TEMPLATE( controlled_stepper_test , Stepper , controlled_stepper_types )
  250. {
  251. Stepper stepper;
  252. check_controlled_stepper( stepper );
  253. }
  254. BOOST_AUTO_TEST_CASE_TEMPLATE( dense_ouput_test , Stepper , dense_output_stepper_types )
  255. {
  256. Stepper stepper;
  257. check_dense_output_stepper( stepper );
  258. }
  259. BOOST_AUTO_TEST_SUITE_END()