modified_midpoint.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/stepper/modified_midpoint.hpp
  4. [begin_description]
  5. Modified midpoint method for the use in Burlish-Stoer stepper.
  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_MODIFIED_MIDPOINT_HPP_INCLUDED
  15. #define BOOST_NUMERIC_ODEINT_STEPPER_MODIFIED_MIDPOINT_HPP_INCLUDED
  16. #include <vector>
  17. #include <boost/numeric/odeint/stepper/base/explicit_stepper_base.hpp>
  18. #include <boost/numeric/odeint/util/resizer.hpp>
  19. #include <boost/numeric/odeint/util/is_resizeable.hpp>
  20. #include <boost/numeric/odeint/algebra/range_algebra.hpp>
  21. #include <boost/numeric/odeint/algebra/default_operations.hpp>
  22. #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
  23. #include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
  24. #include <boost/numeric/odeint/util/copy.hpp>
  25. namespace boost {
  26. namespace numeric {
  27. namespace odeint {
  28. template<
  29. class State ,
  30. class Value = double ,
  31. class Deriv = State ,
  32. class Time = Value ,
  33. class Algebra = typename algebra_dispatcher< State >::algebra_type ,
  34. class Operations = typename operations_dispatcher< State >::operations_type ,
  35. class Resizer = initially_resizer
  36. >
  37. #ifndef DOXYGEN_SKIP
  38. class modified_midpoint
  39. : public explicit_stepper_base<
  40. modified_midpoint< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
  41. 2 , State , Value , Deriv , Time , Algebra , Operations , Resizer >
  42. #else
  43. class modified_midpoint : public explicit_stepper_base
  44. #endif
  45. {
  46. public :
  47. typedef explicit_stepper_base<
  48. modified_midpoint< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
  49. 2 , State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_base_type;
  50. typedef typename stepper_base_type::state_type state_type;
  51. typedef typename stepper_base_type::wrapped_state_type wrapped_state_type;
  52. typedef typename stepper_base_type::value_type value_type;
  53. typedef typename stepper_base_type::deriv_type deriv_type;
  54. typedef typename stepper_base_type::wrapped_deriv_type wrapped_deriv_type;
  55. typedef typename stepper_base_type::time_type time_type;
  56. typedef typename stepper_base_type::algebra_type algebra_type;
  57. typedef typename stepper_base_type::operations_type operations_type;
  58. typedef typename stepper_base_type::resizer_type resizer_type;
  59. typedef typename stepper_base_type::stepper_type stepper_type;
  60. modified_midpoint( unsigned short steps = 2 , const algebra_type &algebra = algebra_type() )
  61. : stepper_base_type( algebra ) , m_steps( steps )
  62. { }
  63. template< class System , class StateIn , class DerivIn , class StateOut >
  64. void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
  65. {
  66. static const value_type val1 = static_cast< value_type >( 1 );
  67. static const value_type val05 = static_cast< value_type >( 1 ) / static_cast< value_type >( 2 );
  68. m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
  69. const time_type h = dt / static_cast<value_type>( m_steps );
  70. const time_type h2 = static_cast<value_type>(2) * h;
  71. typename odeint::unwrap_reference< System >::type &sys = system;
  72. time_type th = t + h;
  73. // m_x1 = x + h*dxdt
  74. stepper_base_type::m_algebra.for_each3( m_x1.m_v , in , dxdt ,
  75. typename operations_type::template scale_sum2< value_type , time_type >( val1 , h ) );
  76. sys( m_x1.m_v , m_dxdt.m_v , th );
  77. boost::numeric::odeint::copy( in , m_x0.m_v );
  78. unsigned short i = 1;
  79. while( i != m_steps )
  80. {
  81. // general step
  82. //tmp = m_x1; m_x1 = m_x0 + h2*m_dxdt; m_x0 = tmp
  83. stepper_base_type::m_algebra.for_each3( m_x1.m_v , m_x0.m_v , m_dxdt.m_v ,
  84. typename operations_type::template scale_sum_swap2< value_type , time_type >( val1 , h2 ) );
  85. th += h;
  86. sys( m_x1.m_v , m_dxdt.m_v , th);
  87. i++;
  88. }
  89. // last step
  90. // x = 0.5*( m_x0 + m_x1 + h*m_dxdt )
  91. stepper_base_type::m_algebra.for_each4( out , m_x0.m_v , m_x1.m_v , m_dxdt.m_v ,
  92. typename operations_type::template scale_sum3< value_type , value_type , time_type >( val05 , val05 , val05*h ) );
  93. }
  94. void set_steps( unsigned short steps )
  95. { m_steps = steps; }
  96. unsigned short steps( void ) const
  97. { return m_steps; }
  98. template< class StateIn >
  99. void adjust_size( const StateIn &x )
  100. {
  101. resize_impl( x );
  102. stepper_base_type::adjust_size( x );
  103. }
  104. private:
  105. template< class StateIn >
  106. bool resize_impl( const StateIn &x )
  107. {
  108. bool resized( false );
  109. resized |= adjust_size_by_resizeability( m_x0 , x , typename is_resizeable<state_type>::type() );
  110. resized |= adjust_size_by_resizeability( m_x1 , x , typename is_resizeable<state_type>::type() );
  111. resized |= adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable<deriv_type>::type() );
  112. return resized;
  113. }
  114. unsigned short m_steps;
  115. resizer_type m_resizer;
  116. wrapped_state_type m_x0;
  117. wrapped_state_type m_x1;
  118. wrapped_deriv_type m_dxdt;
  119. };
  120. /* Modified midpoint which stores derivatives and state at dt/2 in some external storage for later usage in dense output calculation
  121. * This Stepper is for use in Bulirsch Stoer only. It DOES NOT meet any stepper concept.
  122. */
  123. template<
  124. class State ,
  125. class Value = double ,
  126. class Deriv = State ,
  127. class Time = Value ,
  128. class Algebra = typename algebra_dispatcher< State >::algebra_type ,
  129. class Operations = typename operations_dispatcher< State >::operations_type ,
  130. class Resizer = initially_resizer
  131. >
  132. class modified_midpoint_dense_out
  133. {
  134. public :
  135. typedef State state_type;
  136. typedef Value value_type;
  137. typedef Deriv deriv_type;
  138. typedef Time time_type;
  139. typedef Algebra algebra_type;
  140. typedef Operations operations_type;
  141. typedef Resizer resizer_type;
  142. typedef state_wrapper< state_type > wrapped_state_type;
  143. typedef state_wrapper< deriv_type > wrapped_deriv_type;
  144. typedef modified_midpoint_dense_out< State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_type;
  145. typedef std::vector< wrapped_deriv_type > deriv_table_type;
  146. modified_midpoint_dense_out( unsigned short steps = 2 , const algebra_type &algebra = algebra_type() )
  147. : m_algebra( algebra ) , m_steps( steps )
  148. { }
  149. /*
  150. * performs a modified midpoint step with m_steps intermediate points
  151. * stores approximation for x(t+dt/2) in x_mp and all evaluated function results in derivs
  152. *
  153. */
  154. template< class System , class StateIn , class DerivIn , class StateOut >
  155. void do_step( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt ,
  156. state_type &x_mp , deriv_table_type &derivs )
  157. {
  158. static const value_type val1 = static_cast< value_type >( 1 );
  159. static const value_type val05 = static_cast< value_type >( 1 ) / static_cast< value_type >( 2 );
  160. m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize< StateIn > , detail::ref( *this ) , detail::_1 ) );
  161. const time_type h = dt / static_cast<value_type>( m_steps );
  162. const time_type h2 = static_cast<value_type>( 2 ) * h;
  163. typename odeint::unwrap_reference< System >::type &sys = system;
  164. time_type th = t + h;
  165. // m_x1 = x + h*dxdt
  166. m_algebra.for_each3( m_x1.m_v , in , dxdt ,
  167. typename operations_type::template scale_sum2< value_type , time_type >( val1 , h ) );
  168. if( m_steps == 2 )
  169. // result of first step already gives approximation at the center of the interval
  170. boost::numeric::odeint::copy( m_x1.m_v , x_mp );
  171. sys( m_x1.m_v , derivs[0].m_v , th );
  172. boost::numeric::odeint::copy( in , m_x0.m_v );
  173. unsigned short i = 1;
  174. while( i != m_steps )
  175. {
  176. // general step
  177. //tmp = m_x1; m_x1 = m_x0 + h2*m_dxdt; m_x0 = tmp
  178. m_algebra.for_each3( m_x1.m_v , m_x0.m_v , derivs[i-1].m_v ,
  179. typename operations_type::template scale_sum_swap2< value_type , time_type >( val1 , h2 ) );
  180. if( i == m_steps/2-1 )
  181. // save approximation at the center of the interval
  182. boost::numeric::odeint::copy( m_x1.m_v , x_mp );
  183. th += h;
  184. sys( m_x1.m_v , derivs[i].m_v , th);
  185. i++;
  186. }
  187. // last step
  188. // x = 0.5*( m_x0 + m_x1 + h*m_dxdt )
  189. m_algebra.for_each4( out , m_x0.m_v , m_x1.m_v , derivs[m_steps-1].m_v ,
  190. typename operations_type::template scale_sum3< value_type , value_type , time_type >( val05 , val05 , val05*h ) );
  191. }
  192. void set_steps( unsigned short steps )
  193. { m_steps = steps; }
  194. unsigned short steps( void ) const
  195. { return m_steps; }
  196. template< class StateIn >
  197. bool resize( const StateIn &x )
  198. {
  199. bool resized( false );
  200. resized |= adjust_size_by_resizeability( m_x0 , x , typename is_resizeable<state_type>::type() );
  201. resized |= adjust_size_by_resizeability( m_x1 , x , typename is_resizeable<state_type>::type() );
  202. return resized;
  203. }
  204. template< class StateIn >
  205. void adjust_size( const StateIn &x )
  206. {
  207. resize( x );
  208. }
  209. private:
  210. algebra_type m_algebra;
  211. unsigned short m_steps;
  212. resizer_type m_resizer;
  213. wrapped_state_type m_x0;
  214. wrapped_state_type m_x1;
  215. };
  216. /********** DOXYGEN ***********/
  217. /**
  218. * \class modified_midpoint
  219. *
  220. * Implementation of the modified midpoint method with a configurable
  221. * number of intermediate steps. This class is used by the Bulirsch-Stoer
  222. * algorithm and is not meant for direct usage.
  223. */
  224. /**
  225. * \class modified_midpoint_dense_out
  226. *
  227. * Implementation of the modified midpoint method with a configurable
  228. * number of intermediate steps. This class is used by the dense output
  229. * Bulirsch-Stoer algorithm and is not meant for direct usage.
  230. * \note This stepper is for internal use only and does not meet
  231. * any stepper concept.
  232. */
  233. }
  234. }
  235. }
  236. #endif // BOOST_NUMERIC_ODEINT_STEPPER_MODIFIED_MIDPOINT_HPP_INCLUDED