runge_kutta_cash_karp54_classic.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/stepper/runge_kutta_cash_karp54_classic.hpp
  4. [begin_description]
  5. Classical implementation of the Runge-Kutta Cash-Karp 5(4) method.
  6. [end_description]
  7. Copyright 2010-2013 Mario Mulansky
  8. Copyright 2010-2013 Karsten Ahnert
  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_RUNGE_KUTTA_CASH_KARP54_CLASSIC_HPP_INCLUDED
  15. #define BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA_CASH_KARP54_CLASSIC_HPP_INCLUDED
  16. #include <boost/numeric/odeint/util/bind.hpp>
  17. #include <boost/numeric/odeint/stepper/base/explicit_error_stepper_base.hpp>
  18. #include <boost/numeric/odeint/algebra/range_algebra.hpp>
  19. #include <boost/numeric/odeint/algebra/default_operations.hpp>
  20. #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
  21. #include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
  22. #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
  23. #include <boost/numeric/odeint/util/state_wrapper.hpp>
  24. #include <boost/numeric/odeint/util/is_resizeable.hpp>
  25. #include <boost/numeric/odeint/util/resizer.hpp>
  26. namespace boost {
  27. namespace numeric {
  28. namespace odeint {
  29. template<
  30. class State ,
  31. class Value = double ,
  32. class Deriv = State ,
  33. class Time = Value ,
  34. class Algebra = typename algebra_dispatcher< State >::algebra_type ,
  35. class Operations = typename operations_dispatcher< State >::operations_type ,
  36. class Resizer = initially_resizer
  37. >
  38. #ifndef DOXYGEN_SKIP
  39. class runge_kutta_cash_karp54_classic
  40. : public explicit_error_stepper_base<
  41. runge_kutta_cash_karp54_classic< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
  42. 5 , 5 , 4 , State , Value , Deriv , Time , Algebra , Operations , Resizer >
  43. #else
  44. class runge_kutta_cash_karp54_classic : public explicit_error_stepper_base
  45. #endif
  46. {
  47. public :
  48. #ifndef DOXYGEN_SKIP
  49. typedef explicit_error_stepper_base<
  50. runge_kutta_cash_karp54_classic< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
  51. 5 , 5 , 4 , State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_base_type;
  52. #else
  53. typedef explicit_error_stepper_base< runge_kutta_cash_karp54_classic< ... > , ... > stepper_base_type;
  54. #endif
  55. typedef typename stepper_base_type::state_type state_type;
  56. typedef typename stepper_base_type::value_type value_type;
  57. typedef typename stepper_base_type::deriv_type deriv_type;
  58. typedef typename stepper_base_type::time_type time_type;
  59. typedef typename stepper_base_type::algebra_type algebra_type;
  60. typedef typename stepper_base_type::operations_type operations_type;
  61. typedef typename stepper_base_type::resizer_type resizer_type;
  62. #ifndef DOXYGEN_SKIP
  63. typedef typename stepper_base_type::wrapped_state_type wrapped_state_type;
  64. typedef typename stepper_base_type::wrapped_deriv_type wrapped_deriv_type;
  65. typedef typename stepper_base_type::stepper_type stepper_type;
  66. #endif
  67. runge_kutta_cash_karp54_classic( const algebra_type &algebra = algebra_type() ) : stepper_base_type( algebra )
  68. { }
  69. template< class System , class StateIn , class DerivIn , class StateOut , class Err >
  70. void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt , Err &xerr )
  71. {
  72. const value_type c1 = static_cast<value_type> ( 37 ) / static_cast<value_type>( 378 );
  73. const value_type c3 = static_cast<value_type> ( 250 ) / static_cast<value_type>( 621 );
  74. const value_type c4 = static_cast<value_type> ( 125 ) / static_cast<value_type>( 594 );
  75. const value_type c6 = static_cast<value_type> ( 512 ) / static_cast<value_type>( 1771 );
  76. const value_type dc1 = c1 - static_cast<value_type> ( 2825 ) / static_cast<value_type>( 27648 );
  77. const value_type dc3 = c3 - static_cast<value_type> ( 18575 ) / static_cast<value_type>( 48384 );
  78. const value_type dc4 = c4 - static_cast<value_type> ( 13525 ) / static_cast<value_type>( 55296 );
  79. const value_type dc5 = static_cast<value_type> ( -277 ) / static_cast<value_type>( 14336 );
  80. const value_type dc6 = c6 - static_cast<value_type> ( 1 ) / static_cast<value_type> ( 4 );
  81. do_step_impl( system , in , dxdt , t , out , dt );
  82. //error estimate
  83. stepper_base_type::m_algebra.for_each6( xerr , dxdt , m_k3.m_v , m_k4.m_v , m_k5.m_v , m_k6.m_v ,
  84. typename operations_type::template scale_sum5< time_type , time_type , time_type , time_type , time_type >( dt*dc1 , dt*dc3 , dt*dc4 , dt*dc5 , dt*dc6 ));
  85. }
  86. template< class System , class StateIn , class DerivIn , class StateOut >
  87. void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
  88. {
  89. const value_type a2 = static_cast<value_type> ( 1 ) / static_cast<value_type> ( 5 );
  90. const value_type a3 = static_cast<value_type> ( 3 ) / static_cast<value_type> ( 10 );
  91. const value_type a4 = static_cast<value_type> ( 3 ) / static_cast<value_type> ( 5 );
  92. const value_type a5 = static_cast<value_type> ( 1 );
  93. const value_type a6 = static_cast<value_type> ( 7 ) / static_cast<value_type> ( 8 );
  94. const value_type b21 = static_cast<value_type> ( 1 ) / static_cast<value_type> ( 5 );
  95. const value_type b31 = static_cast<value_type> ( 3 ) / static_cast<value_type>( 40 );
  96. const value_type b32 = static_cast<value_type> ( 9 ) / static_cast<value_type>( 40 );
  97. const value_type b41 = static_cast<value_type> ( 3 ) / static_cast<value_type> ( 10 );
  98. const value_type b42 = static_cast<value_type> ( -9 ) / static_cast<value_type> ( 10 );
  99. const value_type b43 = static_cast<value_type> ( 6 ) / static_cast<value_type> ( 5 );
  100. const value_type b51 = static_cast<value_type> ( -11 ) / static_cast<value_type>( 54 );
  101. const value_type b52 = static_cast<value_type> ( 5 ) / static_cast<value_type> ( 2 );
  102. const value_type b53 = static_cast<value_type> ( -70 ) / static_cast<value_type>( 27 );
  103. const value_type b54 = static_cast<value_type> ( 35 ) / static_cast<value_type>( 27 );
  104. const value_type b61 = static_cast<value_type> ( 1631 ) / static_cast<value_type>( 55296 );
  105. const value_type b62 = static_cast<value_type> ( 175 ) / static_cast<value_type>( 512 );
  106. const value_type b63 = static_cast<value_type> ( 575 ) / static_cast<value_type>( 13824 );
  107. const value_type b64 = static_cast<value_type> ( 44275 ) / static_cast<value_type>( 110592 );
  108. const value_type b65 = static_cast<value_type> ( 253 ) / static_cast<value_type>( 4096 );
  109. const value_type c1 = static_cast<value_type> ( 37 ) / static_cast<value_type>( 378 );
  110. const value_type c3 = static_cast<value_type> ( 250 ) / static_cast<value_type>( 621 );
  111. const value_type c4 = static_cast<value_type> ( 125 ) / static_cast<value_type>( 594 );
  112. const value_type c6 = static_cast<value_type> ( 512 ) / static_cast<value_type>( 1771 );
  113. typename odeint::unwrap_reference< System >::type &sys = system;
  114. m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl<StateIn> , detail::ref( *this ) , detail::_1 ) );
  115. //m_x1 = x + dt*b21*dxdt
  116. stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , dxdt ,
  117. typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , dt*b21 ) );
  118. sys( m_x_tmp.m_v , m_k2.m_v , t + dt*a2 );
  119. // m_x_tmp = x + dt*b31*dxdt + dt*b32*m_x2
  120. stepper_base_type::m_algebra.for_each4( m_x_tmp.m_v , in , dxdt , m_k2.m_v ,
  121. typename operations_type::template scale_sum3< value_type , time_type , time_type >( 1.0 , dt*b31 , dt*b32 ));
  122. sys( m_x_tmp.m_v , m_k3.m_v , t + dt*a3 );
  123. // m_x_tmp = x + dt * (b41*dxdt + b42*m_x2 + b43*m_x3)
  124. stepper_base_type::m_algebra.for_each5( m_x_tmp.m_v , in , dxdt , m_k2.m_v , m_k3.m_v ,
  125. typename operations_type::template scale_sum4< value_type , time_type , time_type , time_type >( 1.0 , dt*b41 , dt*b42 , dt*b43 ));
  126. sys( m_x_tmp.m_v, m_k4.m_v , t + dt*a4 );
  127. stepper_base_type::m_algebra.for_each6( m_x_tmp.m_v , in , dxdt , m_k2.m_v , m_k3.m_v , m_k4.m_v ,
  128. typename operations_type::template scale_sum5< value_type , time_type , time_type , time_type , time_type >( 1.0 , dt*b51 , dt*b52 , dt*b53 , dt*b54 ));
  129. sys( m_x_tmp.m_v , m_k5.m_v , t + dt*a5 );
  130. stepper_base_type::m_algebra.for_each7( m_x_tmp.m_v , in , dxdt , m_k2.m_v , m_k3.m_v , m_k4.m_v , m_k5.m_v ,
  131. typename operations_type::template scale_sum6< value_type , time_type , time_type , time_type , time_type , time_type >( 1.0 , dt*b61 , dt*b62 , dt*b63 , dt*b64 , dt*b65 ));
  132. sys( m_x_tmp.m_v , m_k6.m_v , t + dt*a6 );
  133. stepper_base_type::m_algebra.for_each6( out , in , dxdt , m_k3.m_v , m_k4.m_v , m_k6.m_v ,
  134. typename operations_type::template scale_sum5< value_type , time_type , time_type , time_type , time_type >( 1.0 , dt*c1 , dt*c3 , dt*c4 , dt*c6 ));
  135. }
  136. /**
  137. * \brief Adjust the size of all temporaries in the stepper manually.
  138. * \param x A state from which the size of the temporaries to be resized is deduced.
  139. */
  140. template< class StateIn >
  141. void adjust_size( const StateIn &x )
  142. {
  143. resize_impl( x );
  144. stepper_base_type::adjust_size( x );
  145. }
  146. private:
  147. template< class StateIn >
  148. bool resize_impl( const StateIn &x )
  149. {
  150. bool resized = false;
  151. resized |= adjust_size_by_resizeability( m_x_tmp , x , typename is_resizeable<state_type>::type() );
  152. resized |= adjust_size_by_resizeability( m_k2 , x , typename is_resizeable<deriv_type>::type() );
  153. resized |= adjust_size_by_resizeability( m_k3 , x , typename is_resizeable<deriv_type>::type() );
  154. resized |= adjust_size_by_resizeability( m_k4 , x , typename is_resizeable<deriv_type>::type() );
  155. resized |= adjust_size_by_resizeability( m_k5 , x , typename is_resizeable<deriv_type>::type() );
  156. resized |= adjust_size_by_resizeability( m_k6 , x , typename is_resizeable<deriv_type>::type() );
  157. return resized;
  158. }
  159. wrapped_state_type m_x_tmp;
  160. wrapped_deriv_type m_k2, m_k3, m_k4, m_k5, m_k6;
  161. resizer_type m_resizer;
  162. };
  163. /************ DOXYGEN *************/
  164. /**
  165. * \class runge_kutta_cash_karp54_classic
  166. * \brief The Runge-Kutta Cash-Karp method implemented without the generic Runge-Kutta algorithm.
  167. *
  168. * The Runge-Kutta Cash-Karp method is one of the standard methods for
  169. * solving ordinary differential equations, see
  170. * <a href="http://en.wikipedia.org/wiki/Cash%E2%80%93Karp_method">en.wikipedia.org/wiki/Cash-Karp_method</a>.
  171. * The method is explicit and fulfills the Error Stepper concept. Step size control
  172. * is provided but continuous output is not available for this method.
  173. *
  174. * This class derives from explicit_error_stepper_base and inherits its interface via CRTP (current recurring
  175. * template pattern). This class implements the method directly, hence the generic Runge-Kutta algorithm is not used.
  176. *
  177. * \tparam State The state type.
  178. * \tparam Value The value type.
  179. * \tparam Deriv The type representing the time derivative of the state.
  180. * \tparam Time The time representing the independent variable - the time.
  181. * \tparam Algebra The algebra type.
  182. * \tparam Operations The operations type.
  183. * \tparam Resizer The resizer policy type.
  184. */
  185. /**
  186. * \fn runge_kutta_cash_karp54_classic::runge_kutta_cash_karp54_classic( const algebra_type &algebra )
  187. * \brief Constructs the runge_kutta_cash_karp54_classic class. This constructor can be used as a default
  188. * constructor if the algebra has a default constructor.
  189. * \param algebra A copy of algebra is made and stored inside explicit_stepper_base.
  190. */
  191. /**
  192. * \fn runge_kutta_cash_karp54_classic::do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt , Err &xerr )
  193. * \brief This method performs one step. The derivative `dxdt` of `in` at the time `t` is passed to the method.
  194. *
  195. * The result is updated out-of-place, hence the input is in `in` and the output in `out`. Futhermore, an
  196. * estimation of the error is stored in `xerr`.
  197. * Access to this step functionality is provided by explicit_error_stepper_base and
  198. * `do_step_impl` should not be called directly.
  199. *
  200. * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
  201. * Simple System concept.
  202. * \param in The state of the ODE which should be solved. in is not modified in this method
  203. * \param dxdt The derivative of x at t.
  204. * \param t The value of the time, at which the step should be performed.
  205. * \param out The result of the step is written in out.
  206. * \param dt The step size.
  207. * \param xerr The result of the error estimation is written in xerr.
  208. */
  209. /**
  210. * \fn runge_kutta_cash_karp54_classic::do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
  211. * \brief This method performs one step. The derivative `dxdt` of `in` at the time `t` is passed to the method.
  212. * The result is updated out-of-place, hence the input is in `in` and the output in `out`.
  213. * Access to this step functionality is provided by explicit_error_stepper_base and
  214. * `do_step_impl` should not be called directly.
  215. *
  216. * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
  217. * Simple System concept.
  218. * \param in The state of the ODE which should be solved. in is not modified in this method
  219. * \param dxdt The derivative of x at t.
  220. * \param t The value of the time, at which the step should be performed.
  221. * \param out The result of the step is written in out.
  222. * \param dt The step size.
  223. */
  224. } // odeint
  225. } // numeric
  226. } // boost
  227. #endif // BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA_CASH_KARP54_CLASSIC_HPP_INCLUDED