explicit_error_generic_rk.hpp 10 KB

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