extrapolation_stepper.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/stepper/extrapolation_stepper.hpp
  4. [begin_description]
  5. extrapolation stepper
  6. [end_description]
  7. Copyright 2009-2015 Mario Mulansky
  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_EXTRAPOLATION_STEPPER_HPP_INCLUDED
  13. #define BOOST_NUMERIC_ODEINT_STEPPER_EXTRAPOLATION_STEPPER_HPP_INCLUDED
  14. #include <iostream>
  15. #include <algorithm>
  16. #include <boost/config.hpp> // for min/max guidelines
  17. #include <boost/static_assert.hpp>
  18. #include <boost/numeric/odeint/util/bind.hpp>
  19. #include <boost/numeric/odeint/util/unwrap_reference.hpp>
  20. #include <boost/numeric/odeint/stepper/base/explicit_error_stepper_base.hpp>
  21. #include <boost/numeric/odeint/stepper/modified_midpoint.hpp>
  22. #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
  23. #include <boost/numeric/odeint/algebra/range_algebra.hpp>
  24. #include <boost/numeric/odeint/algebra/default_operations.hpp>
  25. #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
  26. #include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
  27. #include <boost/numeric/odeint/util/state_wrapper.hpp>
  28. #include <boost/numeric/odeint/util/is_resizeable.hpp>
  29. #include <boost/numeric/odeint/util/resizer.hpp>
  30. #include <boost/numeric/odeint/util/unit_helper.hpp>
  31. #include <boost/numeric/odeint/util/detail/less_with_sign.hpp>
  32. namespace boost
  33. {
  34. namespace numeric
  35. {
  36. namespace odeint
  37. {
  38. template < unsigned short Order, class State, class Value = double,
  39. class Deriv = State, class Time = Value,
  40. class Algebra = typename algebra_dispatcher< State >::algebra_type,
  41. class Operations =
  42. typename operations_dispatcher< State >::operations_type,
  43. class Resizer = initially_resizer >
  44. #ifndef DOXYGEN_SKIP
  45. class extrapolation_stepper
  46. : public explicit_error_stepper_base<
  47. extrapolation_stepper< Order, State, Value, Deriv, Time, Algebra,
  48. Operations, Resizer >,
  49. Order, Order, Order - 2, State, Value, Deriv, Time, Algebra,
  50. Operations, Resizer >
  51. #else
  52. class extrapolation_stepper : public explicit_error_stepper_base
  53. #endif
  54. {
  55. private:
  56. // check for Order being odd
  57. BOOST_STATIC_ASSERT_MSG(
  58. ( ( Order % 2 ) == 0 ) && ( Order > 2 ),
  59. "extrapolation_stepper requires even Order larger than 2" );
  60. public:
  61. #ifndef DOXYGEN_SKIP
  62. typedef explicit_error_stepper_base<
  63. extrapolation_stepper< Order, State, Value, Deriv, Time, Algebra,
  64. Operations, Resizer >,
  65. Order, Order, Order - 2, State, Value, Deriv, Time, Algebra, Operations,
  66. Resizer > stepper_base_type;
  67. #else
  68. typedef explicit_error_stepper_base< extrapolation_stepper< ... >, ... >
  69. stepper_base_type;
  70. #endif
  71. typedef typename stepper_base_type::state_type state_type;
  72. typedef typename stepper_base_type::value_type value_type;
  73. typedef typename stepper_base_type::deriv_type deriv_type;
  74. typedef typename stepper_base_type::time_type time_type;
  75. typedef typename stepper_base_type::algebra_type algebra_type;
  76. typedef typename stepper_base_type::operations_type operations_type;
  77. typedef typename stepper_base_type::resizer_type resizer_type;
  78. #ifndef DOXYGEN_SKIP
  79. typedef typename stepper_base_type::stepper_type stepper_type;
  80. typedef typename stepper_base_type::wrapped_state_type wrapped_state_type;
  81. typedef typename stepper_base_type::wrapped_deriv_type wrapped_deriv_type;
  82. typedef std::vector< value_type > value_vector;
  83. typedef std::vector< value_vector > value_matrix;
  84. typedef std::vector< size_t > int_vector;
  85. typedef std::vector< wrapped_state_type > state_table_type;
  86. typedef modified_midpoint< state_type, value_type, deriv_type, time_type,
  87. algebra_type, operations_type,
  88. resizer_type > midpoint_stepper_type;
  89. #endif // DOXYGEN_SKIP
  90. typedef unsigned short order_type;
  91. static const order_type order_value = stepper_base_type::order_value;
  92. static const order_type stepper_order_value =
  93. stepper_base_type::stepper_order_value;
  94. static const order_type error_order_value =
  95. stepper_base_type::error_order_value;
  96. const static size_t m_k_max = ( order_value - 2 ) / 2;
  97. extrapolation_stepper( const algebra_type &algebra = algebra_type() )
  98. : stepper_base_type( algebra ), m_interval_sequence( m_k_max + 1 ),
  99. m_coeff( m_k_max + 1 ), m_table( m_k_max )
  100. {
  101. for ( unsigned short i = 0; i < m_k_max + 1; i++ )
  102. {
  103. m_interval_sequence[i] = 2 * ( i + 1 );
  104. m_coeff[i].resize( i );
  105. for ( size_t k = 0; k < i; ++k )
  106. {
  107. const value_type r =
  108. static_cast< value_type >( m_interval_sequence[i] ) /
  109. static_cast< value_type >( m_interval_sequence[k] );
  110. m_coeff[i][k] =
  111. static_cast< value_type >( 1 ) /
  112. ( r * r - static_cast< value_type >(
  113. 1 ) ); // coefficients for extrapolation
  114. }
  115. }
  116. }
  117. template < class System, class StateIn, class DerivIn, class StateOut,
  118. class Err >
  119. void do_step_impl( System system, const StateIn &in, const DerivIn &dxdt,
  120. time_type t, StateOut &out, time_type dt, Err &xerr )
  121. {
  122. // std::cout << "dt: " << dt << std::endl;
  123. // normal step
  124. do_step_impl( system, in, dxdt, t, out, dt );
  125. static const value_type val1( 1.0 );
  126. // additionally, perform the error calculation
  127. stepper_base_type::m_algebra.for_each3(
  128. xerr, out, m_table[0].m_v,
  129. typename operations_type::template scale_sum2<
  130. value_type, value_type >( val1, -val1 ) );
  131. }
  132. template < class System, class StateInOut, class DerivIn, class Err >
  133. void do_step_impl_io( System system, StateInOut &inout, const DerivIn &dxdt,
  134. time_type t, time_type dt, Err &xerr )
  135. {
  136. // normal step
  137. do_step_impl_io( system, inout, dxdt, t, dt );
  138. static const value_type val1( 1.0 );
  139. // additionally, perform the error calculation
  140. stepper_base_type::m_algebra.for_each3(
  141. xerr, inout, m_table[0].m_v,
  142. typename operations_type::template scale_sum2<
  143. value_type, value_type >( val1, -val1 ) );
  144. }
  145. template < class System, class StateIn, class DerivIn, class StateOut >
  146. void do_step_impl( System system, const StateIn &in, const DerivIn &dxdt,
  147. time_type t, StateOut &out, time_type dt )
  148. {
  149. m_resizer.adjust_size(
  150. in, detail::bind( &stepper_type::template resize_impl< StateIn >,
  151. detail::ref( *this ), detail::_1 ) );
  152. size_t k = 0;
  153. m_midpoint.set_steps( m_interval_sequence[k] );
  154. m_midpoint.do_step( system, in, dxdt, t, out, dt );
  155. for ( k = 1; k <= m_k_max; ++k )
  156. {
  157. m_midpoint.set_steps( m_interval_sequence[k] );
  158. m_midpoint.do_step( system, in, dxdt, t, m_table[k - 1].m_v, dt );
  159. extrapolate( k, m_table, m_coeff, out );
  160. }
  161. }
  162. template < class System, class StateInOut, class DerivIn >
  163. void do_step_impl_io( System system, StateInOut &inout, const DerivIn &dxdt,
  164. time_type t, time_type dt )
  165. {
  166. // special care for inout
  167. m_xout_resizer.adjust_size(
  168. inout,
  169. detail::bind( &stepper_type::template resize_m_xout< StateInOut >,
  170. detail::ref( *this ), detail::_1 ) );
  171. do_step_impl( system, inout, dxdt, t, m_xout.m_v, dt );
  172. boost::numeric::odeint::copy( m_xout.m_v, inout );
  173. }
  174. template < class System, class StateInOut, class DerivIn >
  175. void do_step_dxdt_impl( System system, StateInOut &x, const DerivIn &dxdt,
  176. time_type t, time_type dt )
  177. {
  178. do_step_impl_io( system , x , dxdt , t , dt );
  179. }
  180. template < class System, class StateIn, class DerivIn, class StateOut >
  181. void do_step_dxdt_impl( System system, const StateIn &in,
  182. const DerivIn &dxdt, time_type t, StateOut &out,
  183. time_type dt )
  184. {
  185. do_step_impl( system , in , dxdt , t , out , dt );
  186. }
  187. template < class StateIn > void adjust_size( const StateIn &x )
  188. {
  189. resize_impl( x );
  190. m_midpoint.adjust_size( x );
  191. }
  192. private:
  193. template < class StateIn > bool resize_impl( const StateIn &x )
  194. {
  195. bool resized( false );
  196. for ( size_t i = 0; i < m_k_max; ++i )
  197. resized |= adjust_size_by_resizeability(
  198. m_table[i], x, typename is_resizeable< state_type >::type() );
  199. return resized;
  200. }
  201. template < class StateIn > bool resize_m_xout( const StateIn &x )
  202. {
  203. return adjust_size_by_resizeability(
  204. m_xout, x, typename is_resizeable< state_type >::type() );
  205. }
  206. template < class StateInOut >
  207. void extrapolate( size_t k, state_table_type &table,
  208. const value_matrix &coeff, StateInOut &xest )
  209. /* polynomial extrapolation, see http://www.nr.com/webnotes/nr3web21.pdf
  210. uses the obtained intermediate results to extrapolate to dt->0
  211. */
  212. {
  213. static const value_type val1 = static_cast< value_type >( 1.0 );
  214. for ( int j = k - 1; j > 0; --j )
  215. {
  216. stepper_base_type::m_algebra.for_each3(
  217. table[j - 1].m_v, table[j].m_v, table[j - 1].m_v,
  218. typename operations_type::template scale_sum2<
  219. value_type, value_type >( val1 + coeff[k][j],
  220. -coeff[k][j] ) );
  221. }
  222. stepper_base_type::m_algebra.for_each3(
  223. xest, table[0].m_v, xest,
  224. typename operations_type::template scale_sum2<
  225. value_type, value_type >( val1 + coeff[k][0], -coeff[k][0] ) );
  226. }
  227. private:
  228. midpoint_stepper_type m_midpoint;
  229. resizer_type m_resizer;
  230. resizer_type m_xout_resizer;
  231. int_vector m_interval_sequence; // stores the successive interval counts
  232. value_matrix m_coeff;
  233. wrapped_state_type m_xout;
  234. state_table_type m_table; // sequence of states for extrapolation
  235. };
  236. /******** DOXYGEN *******/
  237. /**
  238. * \class extrapolation_stepper
  239. * \brief Extrapolation stepper with configurable order, and error estimation.
  240. *
  241. * The extrapolation stepper is a stepper with error estimation and configurable
  242. * order. The order is given as template parameter and needs to be an _odd_
  243. * number. The stepper is based on several executions of the modified midpoint
  244. * method and a Richardson extrapolation. This is essentially the same technique
  245. * as for bulirsch_stoer, but without the variable order.
  246. *
  247. * \note The Order parameter has to be an even number greater 2.
  248. */
  249. }
  250. }
  251. }
  252. #endif