explicit_generic_rk.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/stepper/explicit_generic_rk.hpp
  4. [begin_description]
  5. Implementation of the generic Runge-Kutta steppers. This is the base class for many Runge-Kutta steppers.
  6. [end_description]
  7. Copyright 2009-2011 Karsten Ahnert
  8. Copyright 2009-2011 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. #ifndef BOOST_NUMERIC_ODEINT_STEPPER_EXPLICIT_GENERIC_RK_HPP_INCLUDED
  14. #define BOOST_NUMERIC_ODEINT_STEPPER_EXPLICIT_GENERIC_RK_HPP_INCLUDED
  15. #include <boost/array.hpp>
  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/stepper/detail/generic_rk_algorithm.hpp>
  20. #include <boost/numeric/odeint/util/state_wrapper.hpp>
  21. #include <boost/numeric/odeint/util/is_resizeable.hpp>
  22. #include <boost/numeric/odeint/util/resizer.hpp>
  23. namespace boost {
  24. namespace numeric {
  25. namespace odeint {
  26. //forward declarations
  27. #ifndef DOXYGEN_SKIP
  28. template<
  29. size_t StageCount,
  30. size_t Order,
  31. class State ,
  32. class Value = double ,
  33. class Deriv = State ,
  34. class Time = Value ,
  35. class Algebra = range_algebra ,
  36. class Operations = default_operations ,
  37. class Resizer = initially_resizer
  38. >
  39. class explicit_generic_rk;
  40. struct stage_vector;
  41. template< class T , class Constant >
  42. struct array_wrapper
  43. {
  44. typedef const typename boost::array< T , Constant::value > type;
  45. };
  46. template< class T , size_t i >
  47. struct stage
  48. {
  49. T c;
  50. boost::array< T , i > a;
  51. };
  52. template< class T , class Constant >
  53. struct stage_wrapper
  54. {
  55. typedef stage< T , Constant::value > type;
  56. };
  57. #endif
  58. template<
  59. size_t StageCount,
  60. size_t Order,
  61. class State ,
  62. class Value ,
  63. class Deriv ,
  64. class Time ,
  65. class Algebra ,
  66. class Operations ,
  67. class Resizer
  68. >
  69. #ifndef DOXYGEN_SKIP
  70. class explicit_generic_rk : public explicit_stepper_base<
  71. explicit_generic_rk< StageCount , Order , State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
  72. Order , State , Value , Deriv , Time , Algebra , Operations , Resizer >
  73. #else
  74. class explicit_generic_rk : public explicit_stepper_base
  75. #endif
  76. {
  77. public:
  78. #ifndef DOXYGEN_SKIP
  79. typedef explicit_stepper_base<
  80. explicit_generic_rk< StageCount , Order , State , Value , Deriv ,Time , Algebra , Operations , Resizer > ,
  81. Order , State , Value , Deriv , Time , Algebra ,
  82. Operations , Resizer > stepper_base_type;
  83. #else
  84. typedef explicit_stepper_base< ... > stepper_base_type;
  85. #endif
  86. typedef typename stepper_base_type::state_type state_type;
  87. typedef typename stepper_base_type::wrapped_state_type wrapped_state_type;
  88. typedef typename stepper_base_type::value_type value_type;
  89. typedef typename stepper_base_type::deriv_type deriv_type;
  90. typedef typename stepper_base_type::wrapped_deriv_type wrapped_deriv_type;
  91. typedef typename stepper_base_type::time_type time_type;
  92. typedef typename stepper_base_type::algebra_type algebra_type;
  93. typedef typename stepper_base_type::operations_type operations_type;
  94. typedef typename stepper_base_type::resizer_type resizer_type;
  95. #ifndef DOXYGEN_SKIP
  96. typedef explicit_generic_rk< StageCount , Order , State , Value , Deriv ,Time , Algebra , Operations , Resizer > stepper_type;
  97. #endif
  98. typedef detail::generic_rk_algorithm< StageCount , Value , Algebra , Operations > rk_algorithm_type;
  99. typedef typename rk_algorithm_type::coef_a_type coef_a_type;
  100. typedef typename rk_algorithm_type::coef_b_type coef_b_type;
  101. typedef typename rk_algorithm_type::coef_c_type coef_c_type;
  102. #ifndef DOXYGEN_SKIP
  103. static const size_t stage_count = StageCount;
  104. #endif
  105. public:
  106. explicit_generic_rk( const coef_a_type &a , const coef_b_type &b , const coef_c_type &c ,
  107. const algebra_type &algebra = algebra_type() )
  108. : stepper_base_type( algebra ) , m_rk_algorithm( a , b , c )
  109. { }
  110. template< class System , class StateIn , class DerivIn , class StateOut >
  111. void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt ,
  112. time_type t , StateOut &out , time_type dt )
  113. {
  114. m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
  115. // actual calculation done in generic_rk.hpp
  116. m_rk_algorithm.do_step( stepper_base_type::m_algebra , system , in , dxdt , t , out , dt , m_x_tmp.m_v , m_F );
  117. }
  118. template< class StateIn >
  119. void adjust_size( const StateIn &x )
  120. {
  121. resize_impl( x );
  122. stepper_base_type::adjust_size( x );
  123. }
  124. private:
  125. template< class StateIn >
  126. bool resize_impl( const StateIn &x )
  127. {
  128. bool resized( false );
  129. resized |= adjust_size_by_resizeability( m_x_tmp , x , typename is_resizeable<state_type>::type() );
  130. for( size_t i = 0 ; i < StageCount-1 ; ++i )
  131. {
  132. resized |= adjust_size_by_resizeability( m_F[i] , x , typename is_resizeable<deriv_type>::type() );
  133. }
  134. return resized;
  135. }
  136. rk_algorithm_type m_rk_algorithm;
  137. resizer_type m_resizer;
  138. wrapped_state_type m_x_tmp;
  139. wrapped_deriv_type m_F[StageCount-1];
  140. };
  141. /*********** DOXYGEN *************/
  142. /**
  143. * \class explicit_generic_rk
  144. * \brief A generic implementation of explicit Runge-Kutta algorithms. This class is as a base class
  145. * for all explicit Runge-Kutta steppers.
  146. *
  147. * This class implements the explicit Runge-Kutta algorithms without error estimation in a generic way.
  148. * The Butcher tableau is passed to the stepper which constructs the stepper scheme with the help of a
  149. * template-metaprogramming algorithm. ToDo : Add example!
  150. *
  151. * This class derives explicit_stepper_base which provides the stepper interface.
  152. *
  153. * \tparam StageCount The number of stages of the Runge-Kutta algorithm.
  154. * \tparam Order The order of the stepper.
  155. * \tparam State The type representing the state of the ODE.
  156. * \tparam Value The floating point type which is used in the computations.
  157. * \tparam Time The type representing the independent variable - the time - of the ODE.
  158. * \tparam Algebra The algebra type.
  159. * \tparam Operations The operations type.
  160. * \tparam Resizer The resizer policy type.
  161. */
  162. /**
  163. * \fn explicit_generic_rk::explicit_generic_rk( const coef_a_type &a , const coef_b_type &b , const coef_c_type &c , const algebra_type &algebra )
  164. * \brief Constructs the explicit_generic_rk class. See examples section for details on the coefficients.
  165. * \param a Triangular matrix of parameters b in the Butcher tableau.
  166. * \param b Last row of the butcher tableau.
  167. * \param c Parameters to calculate the time points in the Butcher tableau.
  168. * \param algebra A copy of algebra is made and stored inside explicit_stepper_base.
  169. */
  170. /**
  171. * \fn explicit_generic_rk::do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
  172. * \brief This method performs one step. The derivative `dxdt` of `in` at the time `t` is passed to the method.
  173. * The result is updated out of place, hence the input is in `in` and the output in `out`.
  174. * Access to this step functionality is provided by explicit_stepper_base and
  175. * `do_step_impl` should not be called directly.
  176. *
  177. * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
  178. * Simple System concept.
  179. * \param in The state of the ODE which should be solved. in is not modified in this method
  180. * \param dxdt The derivative of x at t.
  181. * \param t The value of the time, at which the step should be performed.
  182. * \param out The result of the step is written in out.
  183. * \param dt The step size.
  184. */
  185. /**
  186. * \fn explicit_generic_rk::adjust_size( const StateIn &x )
  187. * \brief Adjust the size of all temporaries in the stepper manually.
  188. * \param x A state from which the size of the temporaries to be resized is deduced.
  189. */
  190. }
  191. }
  192. }
  193. #endif // BOOST_NUMERIC_ODEINT_STEPPER_EXPLICIT_GENERIC_RK_HPP_INCLUDED