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 <array>
  17. #include <type_traits>
  18. #include <boost/numeric/odeint/util/bind.hpp>
  19. #include <boost/numeric/odeint/util/unwrap_reference.hpp>
  20. #include <boost/numeric/odeint/util/copy.hpp>
  21. #include <boost/numeric/odeint/util/is_pair.hpp>
  22. #include <boost/numeric/odeint/util/state_wrapper.hpp>
  23. #include <boost/numeric/odeint/util/resizer.hpp>
  24. #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
  25. #include <boost/numeric/odeint/stepper/base/algebra_stepper_base.hpp>
  26. namespace boost {
  27. namespace numeric {
  28. namespace odeint {
  29. template<
  30. size_t NumOfStages ,
  31. unsigned short Order ,
  32. class Coor ,
  33. class Momentum ,
  34. class Value ,
  35. class CoorDeriv ,
  36. class MomentumDeriv ,
  37. class Time ,
  38. class Algebra ,
  39. class Operations ,
  40. class Resizer
  41. >
  42. class symplectic_nystroem_stepper_base : public algebra_stepper_base< Algebra , Operations >
  43. {
  44. public:
  45. typedef algebra_stepper_base< Algebra , Operations > algebra_stepper_base_type;
  46. typedef typename algebra_stepper_base_type::algebra_type algebra_type;
  47. typedef typename algebra_stepper_base_type::operations_type operations_type;
  48. const static size_t num_of_stages = NumOfStages;
  49. typedef Coor coor_type;
  50. typedef Momentum momentum_type;
  51. typedef std::pair< coor_type , momentum_type > state_type;
  52. typedef CoorDeriv coor_deriv_type;
  53. typedef state_wrapper< coor_deriv_type> wrapped_coor_deriv_type;
  54. typedef MomentumDeriv momentum_deriv_type;
  55. typedef state_wrapper< momentum_deriv_type > wrapped_momentum_deriv_type;
  56. typedef std::pair< coor_deriv_type , momentum_deriv_type > deriv_type;
  57. typedef Value value_type;
  58. typedef Time time_type;
  59. typedef Resizer resizer_type;
  60. typedef stepper_tag stepper_category;
  61. #ifndef DOXYGEN_SKIP
  62. typedef symplectic_nystroem_stepper_base< NumOfStages , Order , Coor , Momentum , Value ,
  63. CoorDeriv , MomentumDeriv , Time , Algebra , Operations , Resizer > internal_stepper_base_type;
  64. #endif
  65. typedef unsigned short order_type;
  66. static const order_type order_value = Order;
  67. typedef std::array< value_type , num_of_stages > coef_type;
  68. symplectic_nystroem_stepper_base( const coef_type &coef_a , const coef_type &coef_b , const algebra_type &algebra = algebra_type() )
  69. : algebra_stepper_base_type( algebra ) , m_coef_a( coef_a ) , m_coef_b( coef_b ) ,
  70. m_dqdt_resizer() , m_dpdt_resizer() , m_dqdt() , m_dpdt()
  71. { }
  72. order_type order( void ) const
  73. {
  74. return order_value;
  75. }
  76. /*
  77. * Version 1 : do_step( system , x , t , dt )
  78. *
  79. * This version does not solve the forwarding problem, boost.range can not be used.
  80. */
  81. template< class System , class StateInOut >
  82. void do_step( System system , const StateInOut &state , time_type t , time_type dt )
  83. {
  84. typedef typename odeint::unwrap_reference< System >::type system_type;
  85. do_step_impl( system , state , t , state , dt , typename is_pair< system_type >::type() );
  86. }
  87. /**
  88. * \brief Same function as above. It differs only in a different const specifier in order
  89. * to solve the forwarding problem, can be used with Boost.Range.
  90. */
  91. template< class System , class StateInOut >
  92. void do_step( System system , StateInOut &state , time_type t , time_type dt )
  93. {
  94. typedef typename odeint::unwrap_reference< System >::type system_type;
  95. do_step_impl( system , state , t , state , dt , typename is_pair< system_type >::type() );
  96. }
  97. /*
  98. * Version 2 : do_step( system , q , p , t , dt );
  99. *
  100. * For Convenience
  101. *
  102. * The two overloads are needed in order to solve the forwarding problem.
  103. */
  104. template< class System , class CoorInOut , class MomentumInOut >
  105. void do_step( System system , CoorInOut &q , MomentumInOut &p , time_type t , time_type dt )
  106. {
  107. do_step( system , std::make_pair( std::ref( q ) , std::ref( p ) ) , t , dt );
  108. }
  109. /**
  110. * \brief Same function as do_step( system , q , p , t , dt ). It differs only in a different const specifier in order
  111. * to solve the forwarding problem, can be called with Boost.Range.
  112. */
  113. template< class System , class CoorInOut , class MomentumInOut >
  114. void do_step( System system , const CoorInOut &q , const MomentumInOut &p , time_type t , time_type dt )
  115. {
  116. do_step( system , std::make_pair( std::ref( q ) , std::ref( p ) ) , t , dt );
  117. }
  118. /*
  119. * Version 3 : do_step( system , in , t , out , dt )
  120. *
  121. * The forwarding problem is not solved in this version
  122. */
  123. template< class System , class StateIn , class StateOut >
  124. void do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
  125. {
  126. typedef typename odeint::unwrap_reference< System >::type system_type;
  127. do_step_impl( system , in , t , out , dt , typename is_pair< system_type >::type() );
  128. }
  129. template< class StateType >
  130. void adjust_size( const StateType &x )
  131. {
  132. resize_dqdt( x );
  133. resize_dpdt( x );
  134. }
  135. /** \brief Returns the coefficients a. */
  136. const coef_type& coef_a( void ) const { return m_coef_a; }
  137. /** \brief Returns the coefficients b. */
  138. const coef_type& coef_b( void ) const { return m_coef_b; }
  139. private:
  140. // stepper for systems with function for dq/dt = f(p) and dp/dt = -f(q)
  141. template< class System , class StateIn , class StateOut >
  142. void do_step_impl( System system , const StateIn &in , time_type /* t */ , StateOut &out , time_type dt , std::integral_constant<bool, true> )
  143. {
  144. typedef typename odeint::unwrap_reference< System >::type system_type;
  145. typedef typename odeint::unwrap_reference< typename system_type::first_type >::type coor_deriv_func_type;
  146. typedef typename odeint::unwrap_reference< typename system_type::second_type >::type momentum_deriv_func_type;
  147. system_type &sys = system;
  148. coor_deriv_func_type &coor_func = sys.first;
  149. momentum_deriv_func_type &momentum_func = sys.second;
  150. typedef typename odeint::unwrap_reference< StateIn >::type state_in_type;
  151. typedef typename odeint::unwrap_reference< typename state_in_type::first_type >::type coor_in_type;
  152. typedef typename odeint::unwrap_reference< typename state_in_type::second_type >::type momentum_in_type;
  153. const state_in_type &state_in = in;
  154. const coor_in_type &coor_in = state_in.first;
  155. const momentum_in_type &momentum_in = state_in.second;
  156. typedef typename odeint::unwrap_reference< StateOut >::type state_out_type;
  157. typedef typename odeint::unwrap_reference< typename state_out_type::first_type >::type coor_out_type;
  158. typedef typename odeint::unwrap_reference< typename state_out_type::second_type >::type momentum_out_type;
  159. state_out_type &state_out = out;
  160. coor_out_type &coor_out = state_out.first;
  161. momentum_out_type &momentum_out = state_out.second;
  162. m_dqdt_resizer.adjust_size(coor_in, [this](auto&& arg) { return this->resize_dqdt<coor_in_type>(std::forward<decltype(arg)>(arg)); });
  163. m_dpdt_resizer.adjust_size(momentum_in, [this](auto&& arg) { return this->resize_dpdt<momentum_in_type>(std::forward<decltype(arg)>(arg)); });
  164. // ToDo: check sizes?
  165. for( size_t l=0 ; l<num_of_stages ; ++l )
  166. {
  167. if( l == 0 )
  168. {
  169. coor_func( momentum_in , m_dqdt.m_v );
  170. this->m_algebra.for_each3( coor_out , coor_in , m_dqdt.m_v ,
  171. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_a[l] * dt ) );
  172. momentum_func( coor_out , m_dpdt.m_v );
  173. this->m_algebra.for_each3( momentum_out , momentum_in , m_dpdt.m_v ,
  174. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_b[l] * dt ) );
  175. }
  176. else
  177. {
  178. coor_func( momentum_out , m_dqdt.m_v );
  179. this->m_algebra.for_each3( coor_out , coor_out , m_dqdt.m_v ,
  180. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_a[l] * dt ) );
  181. momentum_func( coor_out , m_dpdt.m_v );
  182. this->m_algebra.for_each3( momentum_out , momentum_out , m_dpdt.m_v ,
  183. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , m_coef_b[l] * dt ) );
  184. }
  185. }
  186. }
  187. // stepper for systems with only function dp /dt = -f(q), dq/dt = p, time not required but still expected for compatibility reasons
  188. template< class System , class StateIn , class StateOut >
  189. void do_step_impl( System system , const StateIn &in , time_type /* t */ , StateOut &out , time_type dt , std::integral_constant<bool, false> )
  190. {
  191. typedef typename odeint::unwrap_reference< System >::type momentum_deriv_func_type;
  192. momentum_deriv_func_type &momentum_func = system;
  193. typedef typename odeint::unwrap_reference< StateIn >::type state_in_type;
  194. typedef typename odeint::unwrap_reference< typename state_in_type::first_type >::type coor_in_type;
  195. typedef typename odeint::unwrap_reference< typename state_in_type::second_type >::type momentum_in_type;
  196. const state_in_type &state_in = in;
  197. const coor_in_type &coor_in = state_in.first;
  198. const momentum_in_type &momentum_in = state_in.second;
  199. typedef typename odeint::unwrap_reference< StateOut >::type state_out_type;
  200. typedef typename odeint::unwrap_reference< typename state_out_type::first_type >::type coor_out_type;
  201. typedef typename odeint::unwrap_reference< typename state_out_type::second_type >::type momentum_out_type;
  202. state_out_type &state_out = out;
  203. coor_out_type &coor_out = state_out.first;
  204. momentum_out_type &momentum_out = state_out.second;
  205. // m_dqdt not required when called with momentum_func only - don't resize
  206. m_dpdt_resizer.adjust_size(momentum_in, [this](auto&& arg) { return this->resize_dpdt<momentum_in_type>(std::forward<decltype(arg)>(arg)); });
  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