adaptive_adams_bashforth_moulton.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. boost/numeric/odeint/stepper/detail/adaptive_adams_bashforth_moulton.hpp
  3. [begin_description]
  4. Implemetation of an adaptive adams bashforth moulton stepper.
  5. Used as the stepper for the controlled adams bashforth moulton stepper.
  6. [end_description]
  7. Copyright 2017 Valentin Noah Hartmann
  8. Distributed under the Boost Software License, Version 1.0.
  9. (See accompanying file LICENSE_1_0.txt or
  10. copy at http://www.boost.org/LICENSE_1_0.txt)
  11. */
  12. #ifndef BOOST_NUMERIC_ODEINT_STEPPER_ADAPTIVE_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
  13. #define BOOST_NUMERIC_ODEINT_STEPPER_ADAPTIVE_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
  14. #include <boost/numeric/odeint/stepper/detail/adaptive_adams_coefficients.hpp>
  15. #include <boost/numeric/odeint/util/unwrap_reference.hpp>
  16. #include <boost/numeric/odeint/util/bind.hpp>
  17. #include <boost/numeric/odeint/util/copy.hpp>
  18. #include <boost/numeric/odeint/algebra/default_operations.hpp>
  19. #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
  20. #include <boost/numeric/odeint/algebra/operations_dispatcher.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/base/algebra_stepper_base.hpp>
  26. #include <boost/numeric/odeint/stepper/detail/rotating_buffer.hpp>
  27. namespace boost {
  28. namespace numeric {
  29. namespace odeint {
  30. template<
  31. size_t Steps,
  32. class State,
  33. class Value = double,
  34. class Deriv = State,
  35. class Time = Value,
  36. class Algebra = typename algebra_dispatcher< State >::algebra_type ,
  37. class Operations = typename operations_dispatcher< State >::operations_type,
  38. class Resizer = initially_resizer
  39. >
  40. class adaptive_adams_bashforth_moulton: public algebra_stepper_base< Algebra , Operations >
  41. {
  42. public:
  43. static const size_t steps = Steps;
  44. typedef unsigned short order_type;
  45. static const order_type order_value = steps;
  46. typedef State state_type;
  47. typedef Value value_type;
  48. typedef Deriv deriv_type;
  49. typedef Time time_type;
  50. typedef state_wrapper< state_type > wrapped_state_type;
  51. typedef state_wrapper< deriv_type > wrapped_deriv_type;
  52. typedef algebra_stepper_base< Algebra , Operations > algebra_stepper_base_type;
  53. typedef typename algebra_stepper_base_type::algebra_type algebra_type;
  54. typedef typename algebra_stepper_base_type::operations_type operations_type;
  55. typedef Resizer resizer_type;
  56. typedef error_stepper_tag stepper_category;
  57. typedef detail::adaptive_adams_coefficients< Steps , Deriv , Value , Time , Algebra , Operations , Resizer > coeff_type;
  58. typedef adaptive_adams_bashforth_moulton< Steps , State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_type;
  59. order_type order() const { return order_value; };
  60. order_type stepper_order() const { return order_value + 1; };
  61. order_type error_order() const { return order_value; };
  62. adaptive_adams_bashforth_moulton( const algebra_type &algebra = algebra_type() )
  63. :algebra_stepper_base_type( algebra ), m_coeff(),
  64. m_dxdt_resizer(), m_xnew_resizer(), m_xerr_resizer()
  65. {};
  66. template< class System >
  67. void do_step(System system, state_type &inOut, time_type t, time_type dt )
  68. {
  69. m_xnew_resizer.adjust_size( inOut , detail::bind( &stepper_type::template resize_xnew_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
  70. do_step(system, inOut, t, m_xnew.m_v, dt, m_xerr.m_v);
  71. boost::numeric::odeint::copy( m_xnew.m_v , inOut);
  72. };
  73. template< class System >
  74. void do_step(System system, const state_type &in, time_type t, state_type &out, time_type dt )
  75. {
  76. do_step(system, in, t, out, dt, m_xerr.m_v);
  77. };
  78. template< class System >
  79. void do_step(System system, state_type &inOut, time_type t, time_type dt, state_type &xerr)
  80. {
  81. m_xnew_resizer.adjust_size( inOut , detail::bind( &stepper_type::template resize_xnew_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
  82. do_step(system, inOut, t, m_xnew.m_v, dt, xerr);
  83. boost::numeric::odeint::copy( m_xnew.m_v , inOut);
  84. };
  85. template< class System >
  86. void do_step(System system, const state_type &in, time_type t, state_type &out, time_type dt , state_type &xerr)
  87. {
  88. do_step_impl(system, in, t, out, dt, xerr);
  89. system(out, m_dxdt.m_v, t+dt);
  90. m_coeff.do_step(m_dxdt.m_v);
  91. m_coeff.confirm();
  92. if(m_coeff.m_eo < order_value)
  93. {
  94. m_coeff.m_eo ++;
  95. }
  96. };
  97. template< class ExplicitStepper, class System >
  98. void initialize(ExplicitStepper stepper, System system, state_type &inOut, time_type &t, time_type dt)
  99. {
  100. reset();
  101. dt = dt/static_cast< time_type >(order_value);
  102. m_dxdt_resizer.adjust_size( inOut , detail::bind( &stepper_type::template resize_dxdt_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
  103. system( inOut , m_dxdt.m_v , t );
  104. for( size_t i=0 ; i<order_value; ++i )
  105. {
  106. stepper.do_step_dxdt_impl( system, inOut, m_dxdt.m_v, t, dt );
  107. system( inOut , m_dxdt.m_v , t + dt);
  108. m_coeff.predict(t, dt);
  109. m_coeff.do_step(m_dxdt.m_v);
  110. m_coeff.confirm();
  111. t += dt;
  112. if(m_coeff.m_eo < order_value)
  113. {
  114. ++m_coeff.m_eo;
  115. }
  116. }
  117. };
  118. template< class System >
  119. void initialize(System system, state_type &inOut, time_type &t, time_type dt)
  120. {
  121. reset();
  122. dt = dt/static_cast< time_type >(order_value);
  123. for(size_t i=0; i<order_value; ++i)
  124. {
  125. this->do_step(system, inOut, t, dt);
  126. t += dt;
  127. };
  128. };
  129. template< class System >
  130. void do_step_impl(System system, const state_type & in, time_type t, state_type & out, time_type &dt, state_type &xerr)
  131. {
  132. size_t eO = m_coeff.m_eo;
  133. m_xerr_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_xerr_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
  134. m_dxdt_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_dxdt_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
  135. m_coeff.predict(t, dt);
  136. if (m_coeff.m_steps_init == 1)
  137. {
  138. system(in, m_dxdt.m_v, t);
  139. m_coeff.do_step(m_dxdt.m_v, 1);
  140. }
  141. boost::numeric::odeint::copy( in , out );
  142. for(size_t i=0; i<eO; ++i)
  143. {
  144. this->m_algebra.for_each3(out, out, m_coeff.phi[1][i].m_v,
  145. typename Operations::template scale_sum2<double, double>(1.0, dt*m_coeff.g[i]*m_coeff.beta[0][i]));
  146. }
  147. system(out, m_dxdt.m_v, t+dt);
  148. m_coeff.do_step(m_dxdt.m_v);
  149. this->m_algebra.for_each3(out, out, m_coeff.phi[0][eO].m_v,
  150. typename Operations::template scale_sum2<double, double>(1.0, dt*m_coeff.g[eO]));
  151. // error for current order
  152. this->m_algebra.for_each2(xerr, m_coeff.phi[0][eO].m_v,
  153. typename Operations::template scale_sum1<double>(dt*(m_coeff.g[eO])));
  154. };
  155. const coeff_type& coeff() const { return m_coeff; };
  156. coeff_type & coeff() { return m_coeff; };
  157. void reset() { m_coeff.reset(); };
  158. const deriv_type & dxdt() const { return m_dxdt.m_v; };
  159. private:
  160. template< class StateType >
  161. bool resize_dxdt_impl( const StateType &x )
  162. {
  163. return adjust_size_by_resizeability( m_dxdt, x, typename is_resizeable<deriv_type>::type() );
  164. };
  165. template< class StateType >
  166. bool resize_xnew_impl( const StateType &x )
  167. {
  168. return adjust_size_by_resizeability( m_xnew, x, typename is_resizeable<state_type>::type() );
  169. };
  170. template< class StateType >
  171. bool resize_xerr_impl( const StateType &x )
  172. {
  173. return adjust_size_by_resizeability( m_xerr, x, typename is_resizeable<state_type>::type() );
  174. };
  175. coeff_type m_coeff;
  176. resizer_type m_dxdt_resizer;
  177. resizer_type m_xnew_resizer;
  178. resizer_type m_xerr_resizer;
  179. wrapped_deriv_type m_dxdt;
  180. wrapped_state_type m_xnew;
  181. wrapped_state_type m_xerr;
  182. };
  183. } // odeint
  184. } // numeric
  185. } // boost
  186. #endif