adams_bashforth_moulton.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/stepper/adams_bashforth_moulton.hpp
  4. [begin_description]
  5. Implementation of the Adams-Bashforth-Moulton method, a predictor-corrector multistep method.
  6. [end_description]
  7. Copyright 2011-2013 Karsten Ahnert
  8. Copyright 2011-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_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
  15. #define BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
  16. #include <boost/numeric/odeint/util/bind.hpp>
  17. #include <boost/numeric/odeint/stepper/stepper_categories.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/util/state_wrapper.hpp>
  23. #include <boost/numeric/odeint/util/resizer.hpp>
  24. #include <boost/numeric/odeint/stepper/adams_bashforth.hpp>
  25. #include <boost/numeric/odeint/stepper/adams_moulton.hpp>
  26. namespace boost {
  27. namespace numeric {
  28. namespace odeint {
  29. template<
  30. size_t Steps ,
  31. class State ,
  32. class Value = double ,
  33. class Deriv = State ,
  34. class Time = Value ,
  35. class Algebra = typename algebra_dispatcher< State >::algebra_type ,
  36. class Operations = typename operations_dispatcher< State >::operations_type ,
  37. class Resizer = initially_resizer,
  38. class InitializingStepper = runge_kutta4< State , Value , Deriv , Time , Algebra , Operations, Resizer >
  39. >
  40. class adams_bashforth_moulton
  41. {
  42. #ifndef DOXYGEN_SKIP
  43. BOOST_STATIC_ASSERT(( Steps > 0 ));
  44. BOOST_STATIC_ASSERT(( Steps < 9 ));
  45. #endif
  46. public :
  47. typedef State state_type;
  48. typedef state_wrapper< state_type > wrapped_state_type;
  49. typedef Value value_type;
  50. typedef Deriv deriv_type;
  51. typedef state_wrapper< deriv_type > wrapped_deriv_type;
  52. typedef Time time_type;
  53. typedef Algebra algebra_type;
  54. typedef Operations operations_type;
  55. typedef Resizer resizer_type;
  56. typedef stepper_tag stepper_category;
  57. typedef InitializingStepper initializing_stepper_type;
  58. static const size_t steps = Steps;
  59. #ifndef DOXYGEN_SKIP
  60. typedef adams_bashforth< steps , state_type , value_type , deriv_type , time_type , algebra_type , operations_type , resizer_type, initializing_stepper_type > adams_bashforth_type;
  61. typedef adams_moulton< steps , state_type , value_type , deriv_type , time_type , algebra_type , operations_type , resizer_type > adams_moulton_type;
  62. typedef adams_bashforth_moulton< steps , state_type , value_type , deriv_type , time_type , algebra_type , operations_type , resizer_type , initializing_stepper_type> stepper_type;
  63. #endif //DOXYGEN_SKIP
  64. typedef unsigned short order_type;
  65. static const order_type order_value = steps;
  66. /** \brief Constructs the adams_bashforth class. */
  67. adams_bashforth_moulton( void )
  68. : m_adams_bashforth() , m_adams_moulton( m_adams_bashforth.algebra() )
  69. , m_x() , m_resizer()
  70. { }
  71. adams_bashforth_moulton( const algebra_type &algebra )
  72. : m_adams_bashforth( algebra ) , m_adams_moulton( m_adams_bashforth.algebra() )
  73. , m_x() , m_resizer()
  74. { }
  75. order_type order( void ) const { return order_value; }
  76. template< class System , class StateInOut >
  77. void do_step( System system , StateInOut &x , time_type t , time_type dt )
  78. {
  79. do_step_impl1( system , x , t , dt );
  80. }
  81. /**
  82. * \brief Second version to solve the forwarding problem, can be called with Boost.Range as StateInOut.
  83. */
  84. template< class System , class StateInOut >
  85. void do_step( System system , const StateInOut &x , time_type t , time_type dt )
  86. {
  87. do_step_impl1( system , x , t , dt );
  88. }
  89. template< class System , class StateIn , class StateOut >
  90. void do_step( System system , const StateIn &in , time_type t , const StateOut &out , time_type dt )
  91. {
  92. do_step_impl2( system , in , t , out , dt );
  93. }
  94. /**
  95. * \brief Second version to solve the forwarding problem, can be called with Boost.Range as StateOut.
  96. */
  97. template< class System , class StateIn , class StateOut >
  98. void do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
  99. {
  100. do_step_impl2( system , in ,t , out , dt );
  101. }
  102. template< class StateType >
  103. void adjust_size( const StateType &x )
  104. {
  105. m_adams_bashforth.adjust_size( x );
  106. m_adams_moulton.adjust_size( x );
  107. resize_impl( x );
  108. }
  109. template< class ExplicitStepper , class System , class StateIn >
  110. void initialize( ExplicitStepper explicit_stepper , System system , StateIn &x , time_type &t , time_type dt )
  111. {
  112. m_adams_bashforth.initialize( explicit_stepper , system , x , t , dt );
  113. }
  114. template< class System , class StateIn >
  115. void initialize( System system , StateIn &x , time_type &t , time_type dt )
  116. {
  117. m_adams_bashforth.initialize( system , x , t , dt );
  118. }
  119. void reset(void)
  120. {
  121. m_adams_bashforth.reset();
  122. }
  123. private:
  124. template< typename System , typename StateInOut >
  125. void do_step_impl1( System system , StateInOut &x , time_type t , time_type dt )
  126. {
  127. if( m_adams_bashforth.is_initialized() )
  128. {
  129. m_resizer.adjust_size( x , detail::bind( &stepper_type::template resize_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
  130. m_adams_bashforth.do_step( system , x , t , m_x.m_v , dt );
  131. m_adams_moulton.do_step( system , x , m_x.m_v , t+dt , x , dt , m_adams_bashforth.step_storage() );
  132. }
  133. else
  134. {
  135. m_adams_bashforth.do_step( system , x , t , dt );
  136. }
  137. }
  138. template< typename System , typename StateIn , typename StateInOut >
  139. void do_step_impl2( System system , StateIn const &in , time_type t , StateInOut & out , time_type dt )
  140. {
  141. if( m_adams_bashforth.is_initialized() )
  142. {
  143. m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
  144. m_adams_bashforth.do_step( system , in , t , m_x.m_v , dt );
  145. m_adams_moulton.do_step( system , in , m_x.m_v , t+dt , out , dt , m_adams_bashforth.step_storage() );
  146. }
  147. else
  148. {
  149. m_adams_bashforth.do_step( system , in , t , out , dt );
  150. }
  151. }
  152. template< class StateIn >
  153. bool resize_impl( const StateIn &x )
  154. {
  155. return adjust_size_by_resizeability( m_x , x , typename is_resizeable< state_type >::type() );
  156. }
  157. adams_bashforth_type m_adams_bashforth;
  158. adams_moulton_type m_adams_moulton;
  159. wrapped_state_type m_x;
  160. resizer_type m_resizer;
  161. };
  162. /********* DOXYGEN ********/
  163. /**
  164. * \class adams_bashforth_moulton
  165. * \brief The Adams-Bashforth-Moulton multistep algorithm.
  166. *
  167. * The Adams-Bashforth method is a multi-step predictor-corrector algorithm
  168. * with configurable step number. The step number is specified as template
  169. * parameter Steps and it then uses the result from the previous Steps steps.
  170. * See also
  171. * <a href="http://en.wikipedia.org/wiki/Linear_multistep_method">en.wikipedia.org/wiki/Linear_multistep_method</a>.
  172. * Currently, a maximum of Steps=8 is supported.
  173. * The method is explicit and fulfills the Stepper concept. Step size control
  174. * or continuous output are not provided.
  175. *
  176. * This class derives from algebra_base and inherits its interface via
  177. * CRTP (current recurring template pattern). For more details see
  178. * algebra_stepper_base.
  179. *
  180. * \tparam Steps The number of steps (maximal 8).
  181. * \tparam State The state type.
  182. * \tparam Value The value type.
  183. * \tparam Deriv The type representing the time derivative of the state.
  184. * \tparam Time The time representing the independent variable - the time.
  185. * \tparam Algebra The algebra type.
  186. * \tparam Operations The operations type.
  187. * \tparam Resizer The resizer policy type.
  188. * \tparam InitializingStepper The stepper for the first two steps.
  189. */
  190. /**
  191. * \fn adams_bashforth_moulton::adams_bashforth_moulton( const algebra_type &algebra )
  192. * \brief Constructs the adams_bashforth class. This constructor can be used as a default
  193. * constructor if the algebra has a default constructor.
  194. * \param algebra A copy of algebra is made and stored.
  195. */
  196. /**
  197. * \fn adams_bashforth_moulton::order( void ) const
  198. * \brief Returns the order of the algorithm, which is equal to the number of steps+1.
  199. * \return order of the method.
  200. */
  201. /**
  202. * \fn adams_bashforth_moulton::do_step( System system , StateInOut &x , time_type t , time_type dt )
  203. * \brief This method performs one step. It transforms the result in-place.
  204. *
  205. * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
  206. * Simple System concept.
  207. * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
  208. * \param t The value of the time, at which the step should be performed.
  209. * \param dt The step size.
  210. */
  211. /**
  212. * \fn adams_bashforth_moulton::do_step( System system , const StateIn &in , time_type t , const StateOut &out , time_type dt )
  213. * \brief The method performs one step with the stepper passed by Stepper. The state of the ODE is updated out-of-place.
  214. *
  215. * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
  216. * Simple System concept.
  217. * \param in The state of the ODE which should be solved. in is not modified in this method
  218. * \param t The value of the time, at which the step should be performed.
  219. * \param out The result of the step is written in out.
  220. * \param dt The step size.
  221. */
  222. /**
  223. * \fn adams_bashforth_moulton::adjust_size( const StateType &x )
  224. * \brief Adjust the size of all temporaries in the stepper manually.
  225. * \param x A state from which the size of the temporaries to be resized is deduced.
  226. */
  227. /**
  228. * \fn adams_bashforth_moulton::initialize( ExplicitStepper explicit_stepper , System system , StateIn &x , time_type &t , time_type dt )
  229. * \brief Initialized the stepper. Does Steps-1 steps with the explicit_stepper to fill the buffer.
  230. * \note The state x and time t are updated to the values after Steps-1 initial steps.
  231. * \param explicit_stepper the stepper used to fill the buffer of previous step results
  232. * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
  233. * Simple System concept.
  234. * \param x The initial state of the ODE which should be solved, updated after in this method.
  235. * \param t The initial time, updated in this method.
  236. * \param dt The step size.
  237. */
  238. /**
  239. * \fn adams_bashforth_moulton::initialize( System system , StateIn &x , time_type &t , time_type dt )
  240. * \brief Initialized the stepper. Does Steps-1 steps using the standard initializing stepper
  241. * of the underlying adams_bashforth stepper.
  242. * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
  243. * Simple System concept.
  244. * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
  245. * \param t The value of the time, at which the step should be performed.
  246. * \param dt The step size.
  247. */
  248. /**
  249. * \fn adams_bashforth_moulton::reset( void )
  250. * \brief Resets the internal buffers of the stepper.
  251. */
  252. } // odeint
  253. } // numeric
  254. } // boost
  255. #endif // BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED