modified_midpoint.hpp 10 KB

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