adams_bashforth.hpp 15 KB

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