dense_output_runge_kutta.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/stepper/dense_output_runge_kutta.hpp
  4. [begin_description]
  5. Implementation of the Dense-output stepper for all steppers. Note, that this class does
  6. not computes the result but serves as an interface.
  7. [end_description]
  8. Copyright 2011-2013 Karsten Ahnert
  9. Copyright 2011-2015 Mario Mulansky
  10. Copyright 2012 Christoph Koke
  11. Distributed under the Boost Software License, Version 1.0.
  12. (See accompanying file LICENSE_1_0.txt or
  13. copy at http://www.boost.org/LICENSE_1_0.txt)
  14. */
  15. #ifndef BOOST_NUMERIC_ODEINT_STEPPER_DENSE_OUTPUT_RUNGE_KUTTA_HPP_INCLUDED
  16. #define BOOST_NUMERIC_ODEINT_STEPPER_DENSE_OUTPUT_RUNGE_KUTTA_HPP_INCLUDED
  17. #include <utility>
  18. #include <stdexcept>
  19. #include <boost/throw_exception.hpp>
  20. #include <boost/numeric/odeint/util/bind.hpp>
  21. #include <boost/numeric/odeint/util/copy.hpp>
  22. #include <boost/numeric/odeint/util/state_wrapper.hpp>
  23. #include <boost/numeric/odeint/util/is_resizeable.hpp>
  24. #include <boost/numeric/odeint/util/resizer.hpp>
  25. #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
  26. #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
  27. #include <boost/numeric/odeint/integrate/max_step_checker.hpp>
  28. namespace boost {
  29. namespace numeric {
  30. namespace odeint {
  31. template< class Stepper , class StepperCategory = typename Stepper::stepper_category >
  32. class dense_output_runge_kutta;
  33. /**
  34. * \brief The class representing dense-output Runge-Kutta steppers.
  35. * \note In this stepper, the initialize method has to be called before using
  36. * the do_step method.
  37. *
  38. * The dense-output functionality allows to interpolate the solution between
  39. * subsequent integration points using intermediate results obtained during the
  40. * computation. This version works based on a normal stepper without step-size
  41. * control.
  42. *
  43. *
  44. * \tparam Stepper The stepper type of the underlying algorithm.
  45. */
  46. template< class Stepper >
  47. class dense_output_runge_kutta< Stepper , stepper_tag >
  48. {
  49. public:
  50. /*
  51. * We do not need all typedefs.
  52. */
  53. typedef Stepper stepper_type;
  54. typedef typename stepper_type::state_type state_type;
  55. typedef typename stepper_type::wrapped_state_type wrapped_state_type;
  56. typedef typename stepper_type::value_type value_type;
  57. typedef typename stepper_type::deriv_type deriv_type;
  58. typedef typename stepper_type::wrapped_deriv_type wrapped_deriv_type;
  59. typedef typename stepper_type::time_type time_type;
  60. typedef typename stepper_type::algebra_type algebra_type;
  61. typedef typename stepper_type::operations_type operations_type;
  62. typedef typename stepper_type::resizer_type resizer_type;
  63. typedef dense_output_stepper_tag stepper_category;
  64. typedef dense_output_runge_kutta< Stepper > dense_output_stepper_type;
  65. /**
  66. * \brief Constructs the dense_output_runge_kutta class. An instance of the
  67. * underlying stepper can be provided.
  68. * \param stepper An instance of the underlying stepper.
  69. */
  70. dense_output_runge_kutta( const stepper_type &stepper = stepper_type() )
  71. : m_stepper( stepper ) , m_resizer() ,
  72. m_x1() , m_x2() , m_current_state_x1( true ) ,
  73. m_t() , m_t_old() , m_dt()
  74. { }
  75. /**
  76. * \brief Initializes the stepper. Has to be called before do_step can be
  77. * used to set the initial conditions and the step size.
  78. * \param x0 The initial state of the ODE which should be solved.
  79. * \param t0 The initial time, at which the step should be performed.
  80. * \param dt0 The step size.
  81. */
  82. template< class StateType >
  83. void initialize( const StateType &x0 , time_type t0 , time_type dt0 )
  84. {
  85. m_resizer.adjust_size( x0 , detail::bind( &dense_output_stepper_type::template resize_impl< StateType > , detail::ref( *this ) , detail::_1 ) );
  86. boost::numeric::odeint::copy( x0 , get_current_state() );
  87. m_t = t0;
  88. m_dt = dt0;
  89. }
  90. /**
  91. * \brief Does one time step.
  92. * \note initialize has to be called before using this method to set the
  93. * initial conditions x,t and the stepsize.
  94. * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
  95. * Simple System concept.
  96. * \return Pair with start and end time of the integration step.
  97. */
  98. template< class System >
  99. std::pair< time_type , time_type > do_step( System system )
  100. {
  101. m_stepper.do_step( system , get_current_state() , m_t , get_old_state() , m_dt );
  102. m_t_old = m_t;
  103. m_t += m_dt;
  104. toggle_current_state();
  105. return std::make_pair( m_t_old , m_t );
  106. }
  107. /*
  108. * The next two overloads are needed to solve the forwarding problem
  109. */
  110. /**
  111. * \brief Calculates the solution at an intermediate point.
  112. * \param t The time at which the solution should be calculated, has to be
  113. * in the current time interval.
  114. * \param x The output variable where the result is written into.
  115. */
  116. template< class StateOut >
  117. void calc_state( time_type t , StateOut &x ) const
  118. {
  119. if( t == current_time() )
  120. {
  121. boost::numeric::odeint::copy( get_current_state() , x );
  122. }
  123. m_stepper.calc_state( x , t , get_old_state() , m_t_old , get_current_state() , m_t );
  124. }
  125. /**
  126. * \brief Calculates the solution at an intermediate point. Solves the forwarding problem
  127. * \param t The time at which the solution should be calculated, has to be
  128. * in the current time interval.
  129. * \param x The output variable where the result is written into, can be a boost range.
  130. */
  131. template< class StateOut >
  132. void calc_state( time_type t , const StateOut &x ) const
  133. {
  134. m_stepper.calc_state( x , t , get_old_state() , m_t_old , get_current_state() , m_t );
  135. }
  136. /**
  137. * \brief Adjust the size of all temporaries in the stepper manually.
  138. * \param x A state from which the size of the temporaries to be resized is deduced.
  139. */
  140. template< class StateType >
  141. void adjust_size( const StateType &x )
  142. {
  143. resize_impl( x );
  144. m_stepper.stepper().resize( x );
  145. }
  146. /**
  147. * \brief Returns the current state of the solution.
  148. * \return The current state of the solution x(t).
  149. */
  150. const state_type& current_state( void ) const
  151. {
  152. return get_current_state();
  153. }
  154. /**
  155. * \brief Returns the current time of the solution.
  156. * \return The current time of the solution t.
  157. */
  158. time_type current_time( void ) const
  159. {
  160. return m_t;
  161. }
  162. /**
  163. * \brief Returns the last state of the solution.
  164. * \return The last state of the solution x(t-dt).
  165. */
  166. const state_type& previous_state( void ) const
  167. {
  168. return get_old_state();
  169. }
  170. /**
  171. * \brief Returns the last time of the solution.
  172. * \return The last time of the solution t-dt.
  173. */
  174. time_type previous_time( void ) const
  175. {
  176. return m_t_old;
  177. }
  178. /**
  179. * \brief Returns the current time step.
  180. * \return dt.
  181. */
  182. time_type current_time_step( void ) const
  183. {
  184. return m_dt;
  185. }
  186. private:
  187. state_type& get_current_state( void )
  188. {
  189. return m_current_state_x1 ? m_x1.m_v : m_x2.m_v ;
  190. }
  191. const state_type& get_current_state( void ) const
  192. {
  193. return m_current_state_x1 ? m_x1.m_v : m_x2.m_v ;
  194. }
  195. state_type& get_old_state( void )
  196. {
  197. return m_current_state_x1 ? m_x2.m_v : m_x1.m_v ;
  198. }
  199. const state_type& get_old_state( void ) const
  200. {
  201. return m_current_state_x1 ? m_x2.m_v : m_x1.m_v ;
  202. }
  203. void toggle_current_state( void )
  204. {
  205. m_current_state_x1 = ! m_current_state_x1;
  206. }
  207. template< class StateIn >
  208. bool resize_impl( const StateIn &x )
  209. {
  210. bool resized = false;
  211. resized |= adjust_size_by_resizeability( m_x1 , x , typename is_resizeable<state_type>::type() );
  212. resized |= adjust_size_by_resizeability( m_x2 , x , typename is_resizeable<state_type>::type() );
  213. return resized;
  214. }
  215. stepper_type m_stepper;
  216. resizer_type m_resizer;
  217. wrapped_state_type m_x1 , m_x2;
  218. bool m_current_state_x1; // if true, the current state is m_x1
  219. time_type m_t , m_t_old , m_dt;
  220. };
  221. /**
  222. * \brief The class representing dense-output Runge-Kutta steppers with FSAL property.
  223. *
  224. * The interface is the same as for dense_output_runge_kutta< Stepper , stepper_tag >.
  225. * This class provides dense output functionality based on methods with step size controlled
  226. *
  227. *
  228. * \tparam Stepper The stepper type of the underlying algorithm.
  229. */
  230. template< class Stepper >
  231. class dense_output_runge_kutta< Stepper , explicit_controlled_stepper_fsal_tag >
  232. {
  233. public:
  234. /*
  235. * We do not need all typedefs.
  236. */
  237. typedef Stepper controlled_stepper_type;
  238. typedef typename controlled_stepper_type::stepper_type stepper_type;
  239. typedef typename stepper_type::state_type state_type;
  240. typedef typename stepper_type::wrapped_state_type wrapped_state_type;
  241. typedef typename stepper_type::value_type value_type;
  242. typedef typename stepper_type::deriv_type deriv_type;
  243. typedef typename stepper_type::wrapped_deriv_type wrapped_deriv_type;
  244. typedef typename stepper_type::time_type time_type;
  245. typedef typename stepper_type::algebra_type algebra_type;
  246. typedef typename stepper_type::operations_type operations_type;
  247. typedef typename stepper_type::resizer_type resizer_type;
  248. typedef dense_output_stepper_tag stepper_category;
  249. typedef dense_output_runge_kutta< Stepper > dense_output_stepper_type;
  250. dense_output_runge_kutta( const controlled_stepper_type &stepper = controlled_stepper_type() )
  251. : m_stepper( stepper ) , m_resizer() ,
  252. m_current_state_x1( true ) ,
  253. m_x1() , m_x2() , m_dxdt1() , m_dxdt2() ,
  254. m_t() , m_t_old() , m_dt() ,
  255. m_is_deriv_initialized( false )
  256. { }
  257. template< class StateType >
  258. void initialize( const StateType &x0 , time_type t0 , time_type dt0 )
  259. {
  260. m_resizer.adjust_size( x0 , detail::bind( &dense_output_stepper_type::template resize< StateType > , detail::ref( *this ) , detail::_1 ) );
  261. boost::numeric::odeint::copy( x0 , get_current_state() );
  262. m_t = t0;
  263. m_dt = dt0;
  264. m_is_deriv_initialized = false;
  265. }
  266. template< class System >
  267. std::pair< time_type , time_type > do_step( System system )
  268. {
  269. if( !m_is_deriv_initialized )
  270. {
  271. typename odeint::unwrap_reference< System >::type &sys = system;
  272. sys( get_current_state() , get_current_deriv() , m_t );
  273. m_is_deriv_initialized = true;
  274. }
  275. failed_step_checker fail_checker; // to throw a runtime_error if step size adjustment fails
  276. controlled_step_result res = fail;
  277. m_t_old = m_t;
  278. do
  279. {
  280. res = m_stepper.try_step( system , get_current_state() , get_current_deriv() , m_t ,
  281. get_old_state() , get_old_deriv() , m_dt );
  282. fail_checker(); // check for overflow of failed steps
  283. }
  284. while( res == fail );
  285. toggle_current_state();
  286. return std::make_pair( m_t_old , m_t );
  287. }
  288. /*
  289. * The two overloads are needed in order to solve the forwarding problem.
  290. */
  291. template< class StateOut >
  292. void calc_state( time_type t , StateOut &x ) const
  293. {
  294. m_stepper.stepper().calc_state( t , x , get_old_state() , get_old_deriv() , m_t_old ,
  295. get_current_state() , get_current_deriv() , m_t );
  296. }
  297. template< class StateOut >
  298. void calc_state( time_type t , const StateOut &x ) const
  299. {
  300. m_stepper.stepper().calc_state( t , x , get_old_state() , get_old_deriv() , m_t_old ,
  301. get_current_state() , get_current_deriv() , m_t );
  302. }
  303. template< class StateIn >
  304. bool resize( const StateIn &x )
  305. {
  306. bool resized = false;
  307. resized |= adjust_size_by_resizeability( m_x1 , x , typename is_resizeable<state_type>::type() );
  308. resized |= adjust_size_by_resizeability( m_x2 , x , typename is_resizeable<state_type>::type() );
  309. resized |= adjust_size_by_resizeability( m_dxdt1 , x , typename is_resizeable<deriv_type>::type() );
  310. resized |= adjust_size_by_resizeability( m_dxdt2 , x , typename is_resizeable<deriv_type>::type() );
  311. return resized;
  312. }
  313. template< class StateType >
  314. void adjust_size( const StateType &x )
  315. {
  316. resize( x );
  317. m_stepper.stepper().resize( x );
  318. }
  319. const state_type& current_state( void ) const
  320. {
  321. return get_current_state();
  322. }
  323. time_type current_time( void ) const
  324. {
  325. return m_t;
  326. }
  327. const state_type& previous_state( void ) const
  328. {
  329. return get_old_state();
  330. }
  331. time_type previous_time( void ) const
  332. {
  333. return m_t_old;
  334. }
  335. time_type current_time_step( void ) const
  336. {
  337. return m_dt;
  338. }
  339. private:
  340. state_type& get_current_state( void )
  341. {
  342. return m_current_state_x1 ? m_x1.m_v : m_x2.m_v ;
  343. }
  344. const state_type& get_current_state( void ) const
  345. {
  346. return m_current_state_x1 ? m_x1.m_v : m_x2.m_v ;
  347. }
  348. state_type& get_old_state( void )
  349. {
  350. return m_current_state_x1 ? m_x2.m_v : m_x1.m_v ;
  351. }
  352. const state_type& get_old_state( void ) const
  353. {
  354. return m_current_state_x1 ? m_x2.m_v : m_x1.m_v ;
  355. }
  356. deriv_type& get_current_deriv( void )
  357. {
  358. return m_current_state_x1 ? m_dxdt1.m_v : m_dxdt2.m_v ;
  359. }
  360. const deriv_type& get_current_deriv( void ) const
  361. {
  362. return m_current_state_x1 ? m_dxdt1.m_v : m_dxdt2.m_v ;
  363. }
  364. deriv_type& get_old_deriv( void )
  365. {
  366. return m_current_state_x1 ? m_dxdt2.m_v : m_dxdt1.m_v ;
  367. }
  368. const deriv_type& get_old_deriv( void ) const
  369. {
  370. return m_current_state_x1 ? m_dxdt2.m_v : m_dxdt1.m_v ;
  371. }
  372. void toggle_current_state( void )
  373. {
  374. m_current_state_x1 = ! m_current_state_x1;
  375. }
  376. controlled_stepper_type m_stepper;
  377. resizer_type m_resizer;
  378. bool m_current_state_x1;
  379. wrapped_state_type m_x1 , m_x2;
  380. wrapped_deriv_type m_dxdt1 , m_dxdt2;
  381. time_type m_t , m_t_old , m_dt;
  382. bool m_is_deriv_initialized;
  383. };
  384. } // namespace odeint
  385. } // namespace numeric
  386. } // namespace boost
  387. #endif // BOOST_NUMERIC_ODEINT_STEPPER_DENSE_OUTPUT_RUNGE_KUTTA_HPP_INCLUDED