extrapolation_stepper.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. static_assert(
  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(in, [this](auto&& arg) { return this->resize_impl<StateIn>(std::forward<decltype(arg)>(arg)); });
  150. size_t k = 0;
  151. m_midpoint.set_steps( m_interval_sequence[k] );
  152. m_midpoint.do_step( system, in, dxdt, t, out, dt );
  153. for ( k = 1; k <= m_k_max; ++k )
  154. {
  155. m_midpoint.set_steps( m_interval_sequence[k] );
  156. m_midpoint.do_step( system, in, dxdt, t, m_table[k - 1].m_v, dt );
  157. extrapolate( k, m_table, m_coeff, out );
  158. }
  159. }
  160. template < class System, class StateInOut, class DerivIn >
  161. void do_step_impl_io( System system, StateInOut &inout, const DerivIn &dxdt,
  162. time_type t, time_type dt )
  163. {
  164. // special care for inout
  165. m_xout_resizer.adjust_size(inout, [this](auto&& arg) { return this->resize_m_xout<StateInOut>(std::forward<decltype(arg)>(arg)); });
  166. do_step_impl( system, inout, dxdt, t, m_xout.m_v, dt );
  167. boost::numeric::odeint::copy( m_xout.m_v, inout );
  168. }
  169. template < class System, class StateInOut, class DerivIn >
  170. void do_step_dxdt_impl( System system, StateInOut &x, const DerivIn &dxdt,
  171. time_type t, time_type dt )
  172. {
  173. do_step_impl_io( system , x , dxdt , t , dt );
  174. }
  175. template < class System, class StateIn, class DerivIn, class StateOut >
  176. void do_step_dxdt_impl( System system, const StateIn &in,
  177. const DerivIn &dxdt, time_type t, StateOut &out,
  178. time_type dt )
  179. {
  180. do_step_impl( system , in , dxdt , t , out , dt );
  181. }
  182. template < class StateIn > void adjust_size( const StateIn &x )
  183. {
  184. resize_impl( x );
  185. m_midpoint.adjust_size( x );
  186. }
  187. private:
  188. template < class StateIn > bool resize_impl( const StateIn &x )
  189. {
  190. bool resized( false );
  191. for ( size_t i = 0; i < m_k_max; ++i )
  192. resized |= adjust_size_by_resizeability(
  193. m_table[i], x, typename is_resizeable< state_type >::type() );
  194. return resized;
  195. }
  196. template < class StateIn > bool resize_m_xout( const StateIn &x )
  197. {
  198. return adjust_size_by_resizeability(
  199. m_xout, x, typename is_resizeable< state_type >::type() );
  200. }
  201. template < class StateInOut >
  202. void extrapolate( size_t k, state_table_type &table,
  203. const value_matrix &coeff, StateInOut &xest )
  204. /* polynomial extrapolation, see http://www.nr.com/webnotes/nr3web21.pdf
  205. uses the obtained intermediate results to extrapolate to dt->0
  206. */
  207. {
  208. static const value_type val1 = static_cast< value_type >( 1.0 );
  209. for ( int j = k - 1; j > 0; --j )
  210. {
  211. stepper_base_type::m_algebra.for_each3(
  212. table[j - 1].m_v, table[j].m_v, table[j - 1].m_v,
  213. typename operations_type::template scale_sum2<
  214. value_type, value_type >( val1 + coeff[k][j],
  215. -coeff[k][j] ) );
  216. }
  217. stepper_base_type::m_algebra.for_each3(
  218. xest, table[0].m_v, xest,
  219. typename operations_type::template scale_sum2<
  220. value_type, value_type >( val1 + coeff[k][0], -coeff[k][0] ) );
  221. }
  222. private:
  223. midpoint_stepper_type m_midpoint;
  224. resizer_type m_resizer;
  225. resizer_type m_xout_resizer;
  226. int_vector m_interval_sequence; // stores the successive interval counts
  227. value_matrix m_coeff;
  228. wrapped_state_type m_xout;
  229. state_table_type m_table; // sequence of states for extrapolation
  230. };
  231. /******** DOXYGEN *******/
  232. /**
  233. * \class extrapolation_stepper
  234. * \brief Extrapolation stepper with configurable order, and error estimation.
  235. *
  236. * The extrapolation stepper is a stepper with error estimation and configurable
  237. * order. The order is given as template parameter and needs to be an _odd_
  238. * number. The stepper is based on several executions of the modified midpoint
  239. * method and a Richardson extrapolation. This is essentially the same technique
  240. * as for bulirsch_stoer, but without the variable order.
  241. *
  242. * \note The Order parameter has to be an even number greater 2.
  243. */
  244. }
  245. }
  246. }
  247. #endif