runge_kutta4_classic.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/stepper/runge_kutta4_classic.hpp
  4. [begin_description]
  5. Implementation for the classical Runge Kutta stepper.
  6. [end_description]
  7. Copyright 2010-2013 Karsten Ahnert
  8. Copyright 2010-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_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED
  15. #define BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED
  16. #include <boost/numeric/odeint/stepper/base/explicit_stepper_base.hpp>
  17. #include <boost/numeric/odeint/algebra/range_algebra.hpp>
  18. #include <boost/numeric/odeint/algebra/default_operations.hpp>
  19. #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
  20. #include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
  21. #include <boost/numeric/odeint/util/state_wrapper.hpp>
  22. #include <boost/numeric/odeint/util/is_resizeable.hpp>
  23. #include <boost/numeric/odeint/util/resizer.hpp>
  24. namespace boost {
  25. namespace numeric {
  26. namespace odeint {
  27. template<
  28. class State ,
  29. class Value = double ,
  30. class Deriv = State ,
  31. class Time = Value ,
  32. class Algebra = typename algebra_dispatcher< State >::algebra_type ,
  33. class Operations = typename operations_dispatcher< State >::operations_type ,
  34. class Resizer = initially_resizer
  35. >
  36. #ifndef DOXYGEN_SKIP
  37. class runge_kutta4_classic
  38. : public explicit_stepper_base<
  39. runge_kutta4_classic< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
  40. 4 , State , Value , Deriv , Time , Algebra , Operations , Resizer >
  41. #else
  42. class runge_kutta4_classic : public explicit_stepper_base
  43. #endif
  44. {
  45. public :
  46. #ifndef DOXYGEN_SKIP
  47. typedef explicit_stepper_base<
  48. runge_kutta4_classic< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
  49. 4 , State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_base_type;
  50. #else
  51. typedef explicit_stepper_base< runge_kutta4_classic< ... > , ... > stepper_base_type;
  52. #endif
  53. typedef typename stepper_base_type::state_type state_type;
  54. typedef typename stepper_base_type::value_type value_type;
  55. typedef typename stepper_base_type::deriv_type deriv_type;
  56. typedef typename stepper_base_type::time_type time_type;
  57. typedef typename stepper_base_type::algebra_type algebra_type;
  58. typedef typename stepper_base_type::operations_type operations_type;
  59. typedef typename stepper_base_type::resizer_type resizer_type;
  60. #ifndef DOXYGEN_SKIP
  61. typedef typename stepper_base_type::stepper_type stepper_type;
  62. typedef typename stepper_base_type::wrapped_state_type wrapped_state_type;
  63. typedef typename stepper_base_type::wrapped_deriv_type wrapped_deriv_type;
  64. #endif // DOXYGEN_SKIP
  65. runge_kutta4_classic( const algebra_type &algebra = algebra_type() ) : stepper_base_type( algebra )
  66. { }
  67. template< class System , class StateIn , class DerivIn , class StateOut >
  68. void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
  69. {
  70. // ToDo : check if size of in,dxdt,out are equal?
  71. static const value_type val1 = static_cast< value_type >( 1 );
  72. m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
  73. typename odeint::unwrap_reference< System >::type &sys = system;
  74. const time_type dh = dt / static_cast< value_type >( 2 );
  75. const time_type th = t + dh;
  76. // dt * dxdt = k1
  77. // m_x_tmp = x + dh*dxdt
  78. stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , dxdt ,
  79. typename operations_type::template scale_sum2< value_type , time_type >( val1 , dh ) );
  80. // dt * m_dxt = k2
  81. sys( m_x_tmp.m_v , m_dxt.m_v , th );
  82. // m_x_tmp = x + dh*m_dxt
  83. stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , m_dxt.m_v ,
  84. typename operations_type::template scale_sum2< value_type , time_type >( val1 , dh ) );
  85. // dt * m_dxm = k3
  86. sys( m_x_tmp.m_v , m_dxm.m_v , th );
  87. //m_x_tmp = x + dt*m_dxm
  88. stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , m_dxm.m_v ,
  89. typename operations_type::template scale_sum2< value_type , time_type >( val1 , dt ) );
  90. // dt * m_dxh = k4
  91. sys( m_x_tmp.m_v , m_dxh.m_v , t + dt );
  92. //x += dt/6 * ( m_dxdt + m_dxt + val2*m_dxm )
  93. time_type dt6 = dt / static_cast< value_type >( 6 );
  94. time_type dt3 = dt / static_cast< value_type >( 3 );
  95. stepper_base_type::m_algebra.for_each6( out , in , dxdt , m_dxt.m_v , m_dxm.m_v , m_dxh.m_v ,
  96. typename operations_type::template scale_sum5< value_type , time_type , time_type , time_type , time_type >( 1.0 , dt6 , dt3 , dt3 , dt6 ) );
  97. // x += dt/6 * m_dxdt + dt/3 * m_dxt )
  98. // stepper_base_type::m_algebra.for_each4( out , in , dxdt , m_dxt.m_v ,
  99. // typename operations_type::template scale_sum3< value_type , time_type , time_type >( 1.0 , dt6 , dt3 ) );
  100. // // x += dt/3 * m_dxm + dt/6 * m_dxh )
  101. // stepper_base_type::m_algebra.for_each4( out , out , m_dxm.m_v , m_dxh.m_v ,
  102. // typename operations_type::template scale_sum3< value_type , time_type , time_type >( 1.0 , dt3 , dt6 ) );
  103. }
  104. template< class StateType >
  105. void adjust_size( const StateType &x )
  106. {
  107. resize_impl( x );
  108. stepper_base_type::adjust_size( x );
  109. }
  110. private:
  111. template< class StateIn >
  112. bool resize_impl( const StateIn &x )
  113. {
  114. bool resized = false;
  115. resized |= adjust_size_by_resizeability( m_x_tmp , x , typename is_resizeable<state_type>::type() );
  116. resized |= adjust_size_by_resizeability( m_dxm , x , typename is_resizeable<deriv_type>::type() );
  117. resized |= adjust_size_by_resizeability( m_dxt , x , typename is_resizeable<deriv_type>::type() );
  118. resized |= adjust_size_by_resizeability( m_dxh , x , typename is_resizeable<deriv_type>::type() );
  119. return resized;
  120. }
  121. resizer_type m_resizer;
  122. wrapped_deriv_type m_dxt;
  123. wrapped_deriv_type m_dxm;
  124. wrapped_deriv_type m_dxh;
  125. wrapped_state_type m_x_tmp;
  126. };
  127. /********* DOXYGEN *********/
  128. /**
  129. * \class runge_kutta4_classic
  130. * \brief The classical Runge-Kutta stepper of fourth order.
  131. *
  132. * The Runge-Kutta method of fourth order is one standard method for
  133. * solving ordinary differential equations and is widely used, see also
  134. * <a href="http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods">en.wikipedia.org/wiki/Runge-Kutta_methods</a>
  135. * The method is explicit and fulfills the Stepper concept. Step size control
  136. * or continuous output are not provided. This class implements the method directly, hence the
  137. * generic Runge-Kutta algorithm is not used.
  138. *
  139. * This class derives from explicit_stepper_base and inherits its interface via
  140. * CRTP (current recurring template pattern). For more details see
  141. * explicit_stepper_base.
  142. *
  143. * \tparam State The state type.
  144. * \tparam Value The value type.
  145. * \tparam Deriv The type representing the time derivative of the state.
  146. * \tparam Time The time representing the independent variable - the time.
  147. * \tparam Algebra The algebra type.
  148. * \tparam Operations The operations type.
  149. * \tparam Resizer The resizer policy type.
  150. */
  151. /**
  152. * \fn runge_kutta4_classic::runge_kutta4_classic( const algebra_type &algebra )
  153. * \brief Constructs the runge_kutta4_classic class. This constructor can be used as a default
  154. * constructor if the algebra has a default constructor.
  155. * \param algebra A copy of algebra is made and stored inside explicit_stepper_base.
  156. */
  157. /**
  158. * \fn runge_kutta4_classic::do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
  159. * \brief This method performs one step. The derivative `dxdt` of `in` at the time `t` is passed to the method.
  160. * The result is updated out of place, hence the input is in `in` and the output in `out`.
  161. * Access to this step functionality is provided by explicit_stepper_base and
  162. * `do_step_impl` should not be called directly.
  163. *
  164. * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
  165. * Simple System concept.
  166. * \param in The state of the ODE which should be solved. in is not modified in this method
  167. * \param dxdt The derivative of x at t.
  168. * \param t The value of the time, at which the step should be performed.
  169. * \param out The result of the step is written in out.
  170. * \param dt The step size.
  171. */
  172. /**
  173. * \fn runge_kutta4_classic::adjust_size( const StateType &x )
  174. * \brief Adjust the size of all temporaries in the stepper manually.
  175. * \param x A state from which the size of the temporaries to be resized is deduced.
  176. */
  177. } // odeint
  178. } // numeric
  179. } // boost
  180. #endif // BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED