symplectic_rkn_stepper_base.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/stepper/base/symplectic_rkn_stepper_base.hpp
  4. [begin_description]
  5. Base class for symplectic Runge-Kutta-Nystrom steppers.
  6. [end_description]
  7. Copyright 2011-2013 Karsten Ahnert
  8. Copyright 2011-2013 Mario Mulansky
  9. Copyright 2012 Christoph Koke
  10. Distributed under the Boost Software License, Version 1.0.
  11. (See accompanying file LICENSE_1_0.txt or
  12. copy at http://www.boost.org/LICENSE_1_0.txt)
  13. */
  14. #ifndef BOOST_NUMERIC_ODEINT_STEPPER_BASE_SYMPLECTIC_RKN_STEPPER_BASE_HPP_INCLUDED
  15. #define BOOST_NUMERIC_ODEINT_STEPPER_BASE_SYMPLECTIC_RKN_STEPPER_BASE_HPP_INCLUDED
  16. #include <boost/array.hpp>
  17. #include <boost/numeric/odeint/util/bind.hpp>
  18. #include <boost/numeric/odeint/util/unwrap_reference.hpp>
  19. #include <boost/numeric/odeint/util/copy.hpp>
  20. #include <boost/numeric/odeint/util/is_pair.hpp>
  21. #include <boost/numeric/odeint/util/state_wrapper.hpp>
  22. #include <boost/numeric/odeint/util/resizer.hpp>
  23. #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
  24. #include <boost/numeric/odeint/stepper/base/algebra_stepper_base.hpp>
  25. namespace boost {
  26. namespace numeric {
  27. namespace odeint {
  28. template<
  29. size_t NumOfStages ,
  30. unsigned short Order ,
  31. class Coor ,
  32. class Momentum ,
  33. class Value ,
  34. class CoorDeriv ,
  35. class MomentumDeriv ,
  36. class Time ,
  37. class Algebra ,
  38. class Operations ,
  39. class Resizer
  40. >
  41. class symplectic_nystroem_stepper_base : public algebra_stepper_base< Algebra , Operations >
  42. {
  43. public:
  44. typedef algebra_stepper_base< Algebra , Operations > algebra_stepper_base_type;
  45. typedef typename algebra_stepper_base_type::algebra_type algebra_type;
  46. typedef typename algebra_stepper_base_type::operations_type operations_type;
  47. const static size_t num_of_stages = NumOfStages;
  48. typedef Coor coor_type;
  49. typedef Momentum momentum_type;
  50. typedef std::pair< coor_type , momentum_type > state_type;
  51. typedef CoorDeriv coor_deriv_type;
  52. typedef state_wrapper< coor_deriv_type> wrapped_coor_deriv_type;
  53. typedef MomentumDeriv momentum_deriv_type;
  54. typedef state_wrapper< momentum_deriv_type > wrapped_momentum_deriv_type;
  55. typedef std::pair< coor_deriv_type , momentum_deriv_type > deriv_type;
  56. typedef Value value_type;
  57. typedef Time time_type;
  58. typedef Resizer resizer_type;
  59. typedef stepper_tag stepper_category;
  60. #ifndef DOXYGEN_SKIP
  61. typedef symplectic_nystroem_stepper_base< NumOfStages , Order , Coor , Momentum , Value ,
  62. CoorDeriv , MomentumDeriv , Time , Algebra , Operations , Resizer > internal_stepper_base_type;
  63. #endif
  64. typedef unsigned short order_type;
  65. static const order_type order_value = Order;
  66. typedef boost::array< value_type , num_of_stages > coef_type;
  67. symplectic_nystroem_stepper_base( const coef_type &coef_a , const coef_type &coef_b , const algebra_type &algebra = algebra_type() )
  68. : algebra_stepper_base_type( algebra ) , m_coef_a( coef_a ) , m_coef_b( coef_b ) ,
  69. m_dqdt_resizer() , m_dpdt_resizer() , m_dqdt() , m_dpdt()
  70. { }
  71. order_type order( void ) const
  72. {
  73. return order_value;
  74. }
  75. /*
  76. * Version 1 : do_step( system , x , t , dt )
  77. *
  78. * This version does not solve the forwarding problem, boost.range can not be used.
  79. */
  80. template< class System , class StateInOut >
  81. void do_step( System system , const StateInOut &state , time_type t , time_type dt )
  82. {
  83. typedef typename odeint::unwrap_reference< System >::type system_type;
  84. do_step_impl( system , state , t , state , dt , typename is_pair< system_type >::type() );
  85. }
  86. /**
  87. * \brief Same function as above. It differs only in a different const specifier in order
  88. * to solve the forwarding problem, can be used with Boost.Range.
  89. */
  90. template< class System , class StateInOut >
  91. void do_step( System system , StateInOut &state , time_type t , time_type dt )
  92. {
  93. typedef typename odeint::unwrap_reference< System >::type system_type;
  94. do_step_impl( system , state , t , state , dt , typename is_pair< system_type >::type() );
  95. }
  96. /*
  97. * Version 2 : do_step( system , q , p , t , dt );
  98. *
  99. * For Convenience
  100. *
  101. * The two overloads are needed in order to solve the forwarding problem.
  102. */
  103. template< class System , class CoorInOut , class MomentumInOut >
  104. void do_step( System system , CoorInOut &q , MomentumInOut &p , time_type t , time_type dt )
  105. {
  106. do_step( system , std::make_pair( detail::ref( q ) , detail::ref( p ) ) , t , dt );
  107. }
  108. /**
  109. * \brief Same function as do_step( system , q , p , t , dt ). It differs only in a different const specifier in order
  110. * to solve the forwarding problem, can be called with Boost.Range.
  111. */
  112. template< class System , class CoorInOut , class MomentumInOut >
  113. void do_step( System system , const CoorInOut &q , const MomentumInOut &p , time_type t , time_type dt )
  114. {
  115. do_step( system , std::make_pair( detail::ref( q ) , detail::ref( p ) ) , t , dt );
  116. }
  117. /*
  118. * Version 3 : do_step( system , in , t , out , dt )
  119. *
  120. * The forwarding problem is not solved in this version
  121. */
  122. template< class System , class StateIn , class StateOut >
  123. void do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
  124. {
  125. typedef typename odeint::unwrap_reference< System >::type system_type;
  126. do_step_impl( system , in , t , out , dt , typename is_pair< system_type >::type() );
  127. }
  128. template< class StateType >
  129. void adjust_size( const StateType &x )
  130. {
  131. resize_dqdt( x );
  132. resize_dpdt( x );
  133. }
  134. /** \brief Returns the coefficients a. */
  135. const coef_type& coef_a( void ) const { return m_coef_a; }
  136. /** \brief Returns the coefficients b. */
  137. const coef_type& coef_b( void ) const { return m_coef_b; }
  138. private:
  139. // stepper for systems with function for dq/dt = f(p) and dp/dt = -f(q)
  140. template< class System , class StateIn , class StateOut >
  141. void do_step_impl( System system , const StateIn &in , time_type /* t */ , StateOut &out , time_type dt , boost::mpl::true_ )
  142. {
  143. typedef typename odeint::unwrap_reference< System >::type system_type;
  144. typedef typename odeint::unwrap_reference< typename system_type::first_type >::type coor_deriv_func_type;
  145. typedef typename odeint::unwrap_reference< typename system_type::second_type >::type momentum_deriv_func_type;
  146. system_type &sys = system;
  147. coor_deriv_func_type &coor_func = sys.first;
  148. momentum_deriv_func_type &momentum_func = sys.second;
  149. typedef typename odeint::unwrap_reference< StateIn >::type state_in_type;
  150. typedef typename odeint::unwrap_reference< typename state_in_type::first_type >::type coor_in_type;
  151. typedef typename odeint::unwrap_reference< typename state_in_type::second_type >::type momentum_in_type;
  152. const state_in_type &state_in = in;
  153. const coor_in_type &coor_in = state_in.first;
  154. const momentum_in_type &momentum_in = state_in.second;
  155. typedef typename odeint::unwrap_reference< StateOut >::type state_out_type;
  156. typedef typename odeint::unwrap_reference< typename state_out_type::first_type >::type coor_out_type;
  157. typedef typename odeint::unwrap_reference< typename state_out_type::second_type >::type momentum_out_type;
  158. state_out_type &state_out = out;
  159. coor_out_type &coor_out = state_out.first;
  160. momentum_out_type &momentum_out = state_out.second;
  161. m_dqdt_resizer.adjust_size( coor_in , detail::bind( &internal_stepper_base_type::template resize_dqdt< coor_in_type > , detail::ref( *this ) , detail::_1 ) );
  162. m_dpdt_resizer.adjust_size( momentum_in , detail::bind( &internal_stepper_base_type::template resize_dpdt< momentum_in_type > , detail::ref( *this ) , detail::_1 ) );
  163. // ToDo: check sizes?
  164. for( size_t l=0 ; l<num_of_stages ; ++l )
  165. {
  166. if( l == 0 )
  167. {
  168. coor_func( momentum_in , m_dqdt.m_v );
  169. this->m_algebra.for_each3( coor_out , coor_in , m_dqdt.m_v ,
  170. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_a[l] * dt ) );
  171. momentum_func( coor_out , m_dpdt.m_v );
  172. this->m_algebra.for_each3( momentum_out , momentum_in , m_dpdt.m_v ,
  173. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_b[l] * dt ) );
  174. }
  175. else
  176. {
  177. coor_func( momentum_out , m_dqdt.m_v );
  178. this->m_algebra.for_each3( coor_out , coor_out , m_dqdt.m_v ,
  179. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_a[l] * dt ) );
  180. momentum_func( coor_out , m_dpdt.m_v );
  181. this->m_algebra.for_each3( momentum_out , momentum_out , m_dpdt.m_v ,
  182. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_b[l] * dt ) );
  183. }
  184. }
  185. }
  186. // stepper for systems with only function dp /dt = -f(q), dq/dt = p, time not required but still expected for compatibility reasons
  187. template< class System , class StateIn , class StateOut >
  188. void do_step_impl( System system , const StateIn &in , time_type /* t */ , StateOut &out , time_type dt , boost::mpl::false_ )
  189. {
  190. typedef typename odeint::unwrap_reference< System >::type momentum_deriv_func_type;
  191. momentum_deriv_func_type &momentum_func = system;
  192. typedef typename odeint::unwrap_reference< StateIn >::type state_in_type;
  193. typedef typename odeint::unwrap_reference< typename state_in_type::first_type >::type coor_in_type;
  194. typedef typename odeint::unwrap_reference< typename state_in_type::second_type >::type momentum_in_type;
  195. const state_in_type &state_in = in;
  196. const coor_in_type &coor_in = state_in.first;
  197. const momentum_in_type &momentum_in = state_in.second;
  198. typedef typename odeint::unwrap_reference< StateOut >::type state_out_type;
  199. typedef typename odeint::unwrap_reference< typename state_out_type::first_type >::type coor_out_type;
  200. typedef typename odeint::unwrap_reference< typename state_out_type::second_type >::type momentum_out_type;
  201. state_out_type &state_out = out;
  202. coor_out_type &coor_out = state_out.first;
  203. momentum_out_type &momentum_out = state_out.second;
  204. // m_dqdt not required when called with momentum_func only - don't resize
  205. // m_dqdt_resizer.adjust_size( coor_in , detail::bind( &internal_stepper_base_type::template resize_dqdt< coor_in_type > , detail::ref( *this ) , detail::_1 ) );
  206. m_dpdt_resizer.adjust_size( momentum_in , detail::bind( &internal_stepper_base_type::template resize_dpdt< momentum_in_type > , detail::ref( *this ) , detail::_1 ) );
  207. // ToDo: check sizes?
  208. // step 0
  209. this->m_algebra.for_each3( coor_out , coor_in , momentum_in ,
  210. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_a[0] * dt ) );
  211. momentum_func( coor_out , m_dpdt.m_v );
  212. this->m_algebra.for_each3( momentum_out , momentum_in , m_dpdt.m_v ,
  213. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_b[0] * dt ) );
  214. for( size_t l=1 ; l<num_of_stages ; ++l )
  215. {
  216. this->m_algebra.for_each3( coor_out , coor_out , momentum_out ,
  217. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_a[l] * dt ) );
  218. momentum_func( coor_out , m_dpdt.m_v );
  219. this->m_algebra.for_each3( momentum_out , momentum_out , m_dpdt.m_v ,
  220. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_b[l] * dt ) );
  221. }
  222. }
  223. template< class StateIn >
  224. bool resize_dqdt( const StateIn &x )
  225. {
  226. return adjust_size_by_resizeability( m_dqdt , x , typename is_resizeable<coor_deriv_type>::type() );
  227. }
  228. template< class StateIn >
  229. bool resize_dpdt( const StateIn &x )
  230. {
  231. return adjust_size_by_resizeability( m_dpdt , x , typename is_resizeable<momentum_deriv_type>::type() );
  232. }
  233. const coef_type m_coef_a;
  234. const coef_type m_coef_b;
  235. resizer_type m_dqdt_resizer;
  236. resizer_type m_dpdt_resizer;
  237. wrapped_coor_deriv_type m_dqdt;
  238. wrapped_momentum_deriv_type m_dpdt;
  239. };
  240. /********* DOXYGEN *********/
  241. /**
  242. * \class symplectic_nystroem_stepper_base
  243. * \brief Base class for all symplectic steppers of Nystroem type.
  244. *
  245. * This class is the base class for the symplectic Runge-Kutta-Nystroem steppers. Symplectic steppers are usually
  246. * used to solve Hamiltonian systems and they conserve the phase space volume, see
  247. * <a href="http://en.wikipedia.org/wiki/Symplectic_integrator">en.wikipedia.org/wiki/Symplectic_integrator</a>.
  248. * Furthermore, the energy is conserved
  249. * in average. In detail this class of steppers can be used to solve separable Hamiltonian systems which can be written
  250. * in the form H(q,p) = H1(p) + H2(q). q is usually called the coordinate, while p is the momentum. The equations of motion
  251. * are dq/dt = dH1/dp, dp/dt = -dH2/dq.
  252. *
  253. * ToDo : add formula for solver and explanation of the coefficients
  254. *
  255. * symplectic_nystroem_stepper_base uses odeints algebra and operation system. Step size and error estimation are not
  256. * provided for this class of solvers. It derives from algebra_stepper_base. Several `do_step` variants are provided:
  257. *
  258. * - `do_step( sys , x , t , dt )` - The classical `do_step` method. The sys can be either a pair of function objects
  259. * for the coordinate or the momentum part or one function object for the momentum part. `x` is a pair of coordinate
  260. * and momentum. The state is updated in-place.
  261. * - `do_step( sys , q , p , t , dt )` - This method is similar to the method above with the difference that the coordinate
  262. * and the momentum are passed explicitly and not packed into a pair.
  263. * - `do_step( sys , x_in , t , x_out , dt )` - This method transforms the state out-of-place. `x_in` and `x_out` are here pairs
  264. * of coordinate and momentum.
  265. *
  266. * \tparam NumOfStages Number of stages.
  267. * \tparam Order The order of the stepper.
  268. * \tparam Coor The type representing the coordinates q.
  269. * \tparam Momentum The type representing the coordinates p.
  270. * \tparam Value The basic value type. Should be something like float, double or a high-precision type.
  271. * \tparam CoorDeriv The type representing the time derivative of the coordinate dq/dt.
  272. * \tparam MomemtnumDeriv The type representing the time derivative of the momentum dp/dt.
  273. * \tparam Time The type representing the time t.
  274. * \tparam Algebra The algebra.
  275. * \tparam Operations The operations.
  276. * \tparam Resizer The resizer policy.
  277. */
  278. /**
  279. * \fn symplectic_nystroem_stepper_base::symplectic_nystroem_stepper_base( const coef_type &coef_a , const coef_type &coef_b , const algebra_type &algebra )
  280. * \brief Constructs a symplectic_nystroem_stepper_base class. The parameters of the specific Nystroem method and the
  281. * algebra have to be passed.
  282. * \param coef_a The coefficients a.
  283. * \param coef_b The coefficients b.
  284. * \param algebra A copy of algebra is made and stored inside explicit_stepper_base.
  285. */
  286. /**
  287. * \fn symplectic_nystroem_stepper_base::order( void ) const
  288. * \return Returns the order of the stepper.
  289. */
  290. /**
  291. * \fn symplectic_nystroem_stepper_base::do_step( System system , const StateInOut &state , time_type t , time_type dt )
  292. * \brief This method performs one step. The system can be either a pair of two function object
  293. * describing the momentum part and the coordinate part or one function object describing only
  294. * the momentum part. In this case the coordinate is assumed to be trivial dq/dt = p. The state
  295. * is updated in-place.
  296. *
  297. * \note boost::ref or std::ref can be used for the system as well as for the state. So, it is correct
  298. * to write `stepper.do_step( make_pair( std::ref( fq ) , std::ref( fp ) ) , make_pair( std::ref( q ) , std::ref( p ) ) , t , dt )`.
  299. *
  300. * \note This method solves the forwarding problem.
  301. *
  302. * \param system The system, can be represented as a pair of two function object or one function object. See above.
  303. * \param state The state of the ODE. It is a pair of Coor and Momentum. The state is updated in-place, therefore, the
  304. * new value of the state will be written into this variable.
  305. * \param t The time of the ODE. It is not advanced by this method.
  306. * \param dt The time step.
  307. */
  308. /**
  309. * \fn symplectic_nystroem_stepper_base::do_step( System system , CoorInOut &q , MomentumInOut &p , time_type t , time_type dt )
  310. * \brief This method performs one step. The system can be either a pair of two function object
  311. * describing the momentum part and the coordinate part or one function object describing only
  312. * the momentum part. In this case the coordinate is assumed to be trivial dq/dt = p. The state
  313. * is updated in-place.
  314. *
  315. * \note boost::ref or std::ref can be used for the system. So, it is correct
  316. * to write `stepper.do_step( make_pair( std::ref( fq ) , std::ref( fp ) ) , q , p , t , dt )`.
  317. *
  318. * \note This method solves the forwarding problem.
  319. *
  320. * \param system The system, can be represented as a pair of two function object or one function object. See above.
  321. * \param q The coordinate of the ODE. It is updated in-place. Therefore, the new value of the coordinate will be written
  322. * into this variable.
  323. * \param p The momentum of the ODE. It is updated in-place. Therefore, the new value of the momentum will be written info
  324. * this variable.
  325. * \param t The time of the ODE. It is not advanced by this method.
  326. * \param dt The time step.
  327. */
  328. /**
  329. * \fn symplectic_nystroem_stepper_base::do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
  330. * \brief This method performs one step. The system can be either a pair of two function object
  331. * describing the momentum part and the coordinate part or one function object describing only
  332. * the momentum part. In this case the coordinate is assumed to be trivial dq/dt = p. The state
  333. * is updated out-of-place.
  334. *
  335. * \note boost::ref or std::ref can be used for the system. So, it is correct
  336. * to write `stepper.do_step( make_pair( std::ref( fq ) , std::ref( fp ) ) , x_in , t , x_out , dt )`.
  337. *
  338. * \note This method NOT solve the forwarding problem.
  339. *
  340. * \param system The system, can be represented as a pair of two function object or one function object. See above.
  341. * \param in The state of the ODE, which is a pair of coordinate and momentum. The state is updated out-of-place, therefore the
  342. * new value is written into out
  343. * \param t The time of the ODE. It is not advanced by this method.
  344. * \param out The new state of the ODE.
  345. * \param dt The time step.
  346. */
  347. /**
  348. * \fn symplectic_nystroem_stepper_base::adjust_size( const StateType &x )
  349. * \brief Adjust the size of all temporaries in the stepper manually.
  350. * \param x A state from which the size of the temporaries to be resized is deduced.
  351. */
  352. } // namespace odeint
  353. } // namespace numeric
  354. } // namespace boost
  355. #endif // BOOST_NUMERIC_ODEINT_STEPPER_BASE_SYMPLECTIC_RKN_STEPPER_BASE_HPP_INCLUDED