adams_bashforth.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/stepper/adams_bashforth.hpp
  4. [begin_description]
  5. Implementaton of the Adam-Bashforth method a multistep method used for the predictor step in the
  6. Adams-Bashforth-Moulton method.
  7. [end_description]
  8. Copyright 2009-2011 Karsten Ahnert
  9. Copyright 2009-2011 Mario Mulansky
  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_HPP_INCLUDED
  15. #define BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_HPP_INCLUDED
  16. #include <boost/static_assert.hpp>
  17. #include <boost/numeric/odeint/util/bind.hpp>
  18. #include <boost/numeric/odeint/util/unwrap_reference.hpp>
  19. #include <boost/numeric/odeint/algebra/range_algebra.hpp>
  20. #include <boost/numeric/odeint/algebra/default_operations.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. #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
  25. #include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
  26. #include <boost/numeric/odeint/stepper/base/algebra_stepper_base.hpp>
  27. #include <boost/numeric/odeint/stepper/detail/adams_bashforth_coefficients.hpp>
  28. #include <boost/numeric/odeint/stepper/detail/adams_bashforth_call_algebra.hpp>
  29. #include <boost/numeric/odeint/stepper/detail/rotating_buffer.hpp>
  30. namespace boost {
  31. namespace numeric {
  32. namespace odeint {
  33. template<
  34. size_t Steps ,
  35. class State ,
  36. class Value = double ,
  37. class Deriv = State ,
  38. class Time = Value ,
  39. class Algebra = range_algebra ,
  40. class Operations = default_operations ,
  41. class Resizer = initially_resizer ,
  42. class InitializingStepper = runge_kutta4< State , Value , Deriv , Time , Algebra , Operations, Resizer >
  43. >
  44. class adams_bashforth : public algebra_stepper_base< Algebra , Operations >
  45. {
  46. #ifndef DOXYGEN_SKIP
  47. BOOST_STATIC_ASSERT(( Steps > 0 ));
  48. BOOST_STATIC_ASSERT(( Steps < 9 ));
  49. #endif
  50. public :
  51. typedef State state_type;
  52. typedef state_wrapper< state_type > wrapped_state_type;
  53. typedef Value value_type;
  54. typedef Deriv deriv_type;
  55. typedef state_wrapper< deriv_type > wrapped_deriv_type;
  56. typedef Time time_type;
  57. typedef Resizer resizer_type;
  58. typedef stepper_tag stepper_category;
  59. typedef InitializingStepper initializing_stepper_type;
  60. typedef typename algebra_stepper_base< Algebra , Operations >::algebra_type algebra_type;
  61. typedef typename algebra_stepper_base< Algebra , Operations >::operations_type operations_type;
  62. #ifndef DOXYGEN_SKIP
  63. typedef adams_bashforth< Steps , State , Value , Deriv , Time , Algebra , Operations , Resizer , InitializingStepper > stepper_type;
  64. #endif
  65. static const size_t steps = Steps;
  66. typedef unsigned short order_type;
  67. static const order_type order_value = steps;
  68. typedef detail::rotating_buffer< wrapped_deriv_type , steps > step_storage_type;
  69. order_type order( void ) const { return order_value; }
  70. adams_bashforth( const algebra_type &algebra = algebra_type() )
  71. : m_step_storage() , m_resizer() , m_coefficients() ,
  72. m_steps_initialized( 0 ) , m_initializing_stepper() ,
  73. m_algebra( algebra )
  74. { }
  75. adams_bashforth( const adams_bashforth &stepper )
  76. : m_step_storage( stepper.m_step_storage ) , m_resizer( stepper.m_resizer ) , m_coefficients() ,
  77. m_steps_initialized( stepper.m_steps_initialized ) , m_initializing_stepper( stepper.m_initializing_stepper ) ,
  78. m_algebra( stepper.m_algebra )
  79. { }
  80. adams_bashforth& operator=( const adams_bashforth &stepper )
  81. {
  82. m_resizer = stepper.m_resizer;
  83. m_step_storage = stepper.m_step_storage;
  84. m_algebra = stepper.m_algebra;
  85. return *this;
  86. }
  87. /*
  88. * Version 1 : do_step( system , x , t , dt );
  89. *
  90. * solves the forwarding problem
  91. */
  92. template< class System , class StateInOut >
  93. void do_step( System system , StateInOut &x , time_type t , time_type dt )
  94. {
  95. do_step( system , x , t , x , dt );
  96. }
  97. /**
  98. * \brief Second version to solve the forwarding problem, can be called with Boost.Range as StateInOut.
  99. */
  100. template< class System , class StateInOut >
  101. void do_step( System system , const StateInOut &x , time_type t , time_type dt )
  102. {
  103. do_step( system , x , t , x , dt );
  104. }
  105. /*
  106. * Version 2 : do_step( system , in , t , out , dt );
  107. *
  108. * solves the forwarding problem
  109. */
  110. template< class System , class StateIn , class StateOut >
  111. void do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
  112. {
  113. do_step_impl( system , in , t , out , dt );
  114. }
  115. /**
  116. * \brief Second version to solve the forwarding problem, can be called with Boost.Range as StateOut.
  117. */
  118. template< class System , class StateIn , class StateOut >
  119. void do_step( System system , const StateIn &in , time_type t , const StateOut &out , time_type dt )
  120. {
  121. do_step_impl( system , in , t , out , dt );
  122. }
  123. template< class StateType >
  124. void adjust_size( const StateType &x )
  125. {
  126. resize_impl( x );
  127. }
  128. const step_storage_type& step_storage( void ) const
  129. {
  130. return m_step_storage;
  131. }
  132. step_storage_type& step_storage( void )
  133. {
  134. return m_step_storage;
  135. }
  136. template< class ExplicitStepper , class System , class StateIn >
  137. void initialize( ExplicitStepper explicit_stepper , System system , StateIn &x , time_type &t , time_type dt )
  138. {
  139. typename odeint::unwrap_reference< ExplicitStepper >::type &stepper = explicit_stepper;
  140. typename odeint::unwrap_reference< System >::type &sys = system;
  141. m_resizer.adjust_size( x , detail::bind( &stepper_type::template resize_impl<StateIn> , detail::ref( *this ) , detail::_1 ) );
  142. for( size_t i=0 ; i<steps-1 ; ++i )
  143. {
  144. if( i != 0 ) m_step_storage.rotate();
  145. sys( x , m_step_storage[0].m_v , t );
  146. stepper.do_step( system , x , m_step_storage[0].m_v , t , dt );
  147. t += dt;
  148. }
  149. m_steps_initialized = steps;
  150. }
  151. template< class System , class StateIn >
  152. void initialize( System system , StateIn &x , time_type &t , time_type dt )
  153. {
  154. initialize( detail::ref( m_initializing_stepper ) , system , x , t , dt );
  155. }
  156. void reset( void )
  157. {
  158. m_steps_initialized = 0;
  159. }
  160. bool is_initialized( void ) const
  161. {
  162. return m_steps_initialized >= steps;
  163. }
  164. const initializing_stepper_type& initializing_stepper( void ) const { return m_initializing_stepper; }
  165. initializing_stepper_type& initializing_stepper( void ) { return m_initializing_stepper; }
  166. private:
  167. template< class System , class StateIn , class StateOut >
  168. void do_step_impl( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
  169. {
  170. typename odeint::unwrap_reference< System >::type &sys = system;
  171. if( m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl<StateIn> , detail::ref( *this ) , detail::_1 ) ) )
  172. {
  173. m_steps_initialized = 0;
  174. }
  175. if( m_steps_initialized < steps - 1 )
  176. {
  177. if( m_steps_initialized != 0 ) m_step_storage.rotate();
  178. sys( in , m_step_storage[0].m_v , t );
  179. m_initializing_stepper.do_step( system , in , m_step_storage[0].m_v , t , out , dt );
  180. m_steps_initialized++;
  181. }
  182. else
  183. {
  184. m_step_storage.rotate();
  185. sys( in , m_step_storage[0].m_v , t );
  186. detail::adams_bashforth_call_algebra< steps , algebra_type , operations_type >()( m_algebra , in , out , m_step_storage , m_coefficients , dt );
  187. }
  188. }
  189. template< class StateIn >
  190. bool resize_impl( const StateIn &x )
  191. {
  192. bool resized( false );
  193. for( size_t i=0 ; i<steps ; ++i )
  194. {
  195. resized |= adjust_size_by_resizeability( m_step_storage[i] , x , typename is_resizeable<deriv_type>::type() );
  196. }
  197. return resized;
  198. }
  199. step_storage_type m_step_storage;
  200. resizer_type m_resizer;
  201. const detail::adams_bashforth_coefficients< value_type , steps > m_coefficients;
  202. size_t m_steps_initialized;
  203. initializing_stepper_type m_initializing_stepper;
  204. protected:
  205. algebra_type m_algebra;
  206. };
  207. /***** DOXYGEN *****/
  208. /**
  209. * \class adams_bashforth
  210. * \brief The Adams-Bashforth multistep algorithm.
  211. *
  212. * The Adams-Bashforth method is a multi-step algorithm with configurable step
  213. * number. The step number is specified as template parameter Steps and it
  214. * then uses the result from the previous Steps steps. See also
  215. * <a href="http://en.wikipedia.org/wiki/Linear_multistep_method">en.wikipedia.org/wiki/Linear_multistep_method</a>.
  216. * Currently, a maximum of Steps=8 is supported.
  217. * The method is explicit and fulfills the Stepper concept. Step size control
  218. * or continuous output are not provided.
  219. *
  220. * This class derives from algebra_base and inherits its interface via
  221. * CRTP (current recurring template pattern). For more details see
  222. * algebra_stepper_base.
  223. *
  224. * \tparam Steps The number of steps (maximal 8).
  225. * \tparam State The state type.
  226. * \tparam Value The value type.
  227. * \tparam Deriv The type representing the time derivative of the state.
  228. * \tparam Time The time representing the independent variable - the time.
  229. * \tparam Algebra The algebra type.
  230. * \tparam Operations The operations type.
  231. * \tparam Resizer The resizer policy type.
  232. * \tparam InitializingStepper The stepper for the first two steps.
  233. */
  234. /**
  235. * \fn adams_bashforth::adams_bashforth( const algebra_type &algebra )
  236. * \brief Constructs the adams_bashforth class. This constructor can be used as a default
  237. * constructor if the algebra has a default constructor.
  238. * \param algebra A copy of algebra is made and stored.
  239. */
  240. /**
  241. * \fn order_type adams_bashforth::order( void ) const
  242. * \brief Returns the order of the algorithm, which is equal to the number of steps.
  243. * \return order of the method.
  244. */
  245. /**
  246. * \fn void adams_bashforth::do_step( System system , StateInOut &x , time_type t , time_type dt )
  247. * \brief This method performs one step. It transforms the result in-place.
  248. *
  249. * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
  250. * Simple System concept.
  251. * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
  252. * \param t The value of the time, at which the step should be performed.
  253. * \param dt The step size.
  254. */
  255. /**
  256. * \fn void adams_bashforth::do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
  257. * \brief The method performs one step with the stepper passed by Stepper. The state of the ODE is updated out-of-place.
  258. *
  259. * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
  260. * Simple System concept.
  261. * \param in The state of the ODE which should be solved. in is not modified in this method
  262. * \param t The value of the time, at which the step should be performed.
  263. * \param out The result of the step is written in out.
  264. * \param dt The step size.
  265. */
  266. /**
  267. * \fn void adams_bashforth::adjust_size( const StateType &x )
  268. * \brief Adjust the size of all temporaries in the stepper manually.
  269. * \param x A state from which the size of the temporaries to be resized is deduced.
  270. */
  271. /**
  272. * \fn const step_storage_type& adams_bashforth::step_storage( void ) const
  273. * \brief Returns the storage of intermediate results.
  274. * \return The storage of intermediate results.
  275. */
  276. /**
  277. * \fn step_storage_type& adams_bashforth::step_storage( void )
  278. * \brief Returns the storage of intermediate results.
  279. * \return The storage of intermediate results.
  280. */
  281. /**
  282. * \fn void adams_bashforth::initialize( ExplicitStepper explicit_stepper , System system , StateIn &x , time_type &t , time_type dt )
  283. * \brief Initialized the stepper. Does Steps-1 steps with the explicit_stepper to fill the buffer.
  284. * \param explicit_stepper the stepper used to fill the buffer of previous step results
  285. * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
  286. * Simple System concept.
  287. * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
  288. * \param t The value of the time, at which the step should be performed.
  289. * \param dt The step size.
  290. */
  291. /**
  292. * \fn void adams_bashforth::initialize( System system , StateIn &x , time_type &t , time_type dt )
  293. * \brief Initialized the stepper. Does Steps-1 steps with an internal instance of InitializingStepper to fill the buffer.
  294. * \note The state x and time t are updated to the values after Steps-1 initial steps.
  295. * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
  296. * Simple System concept.
  297. * \param x The initial state of the ODE which should be solved, updated in this method.
  298. * \param t The initial value of the time, updated in this method.
  299. * \param dt The step size.
  300. */
  301. /**
  302. * \fn void adams_bashforth::reset( void )
  303. * \brief Resets the internal buffer of the stepper.
  304. */
  305. /**
  306. * \fn bool adams_bashforth::is_initialized( void ) const
  307. * \brief Returns true if the stepper has been initialized.
  308. * \return bool true if stepper is initialized, false otherwise
  309. */
  310. /**
  311. * \fn const initializing_stepper_type& adams_bashforth::initializing_stepper( void ) const
  312. * \brief Returns the internal initializing stepper instance.
  313. * \return initializing_stepper
  314. */
  315. /**
  316. * \fn const initializing_stepper_type& adams_bashforth::initializing_stepper( void ) const
  317. * \brief Returns the internal initializing stepper instance.
  318. * \return initializing_stepper
  319. */
  320. /**
  321. * \fn initializing_stepper_type& adams_bashforth::initializing_stepper( void )
  322. * \brief Returns the internal initializing stepper instance.
  323. * \return initializing_stepper
  324. */
  325. } // odeint
  326. } // numeric
  327. } // boost
  328. #endif // BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_HPP_INCLUDED