explicit_generic_rk.hpp 8.3 KB

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