n_step_iterator_impl.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/iterator/detail/n_step_iterator_impl.hpp
  4. [begin_description]
  5. tba.
  6. [end_description]
  7. Copyright 2009-2013 Karsten Ahnert
  8. Copyright 2009-2013 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_ITERATOR_DETAIL_N_STEP_ITERATOR_IMPL_HPP_DEFINED
  14. #define BOOST_NUMERIC_ODEINT_ITERATOR_DETAIL_N_STEP_ITERATOR_IMPL_HPP_DEFINED
  15. #include <boost/numeric/odeint/iterator/detail/ode_iterator_base.hpp>
  16. #include <boost/numeric/odeint/util/unit_helper.hpp>
  17. namespace boost {
  18. namespace numeric {
  19. namespace odeint {
  20. template< class Iterator , class Stepper , class System , class State , typename Tag , class StepperTag >
  21. class n_step_iterator_impl;
  22. /*
  23. * Specilization for steppers and error steppers
  24. */
  25. /**
  26. * \brief ODE Iterator performing exactly n steps with constant step size. The value type of this iterator is the state type of the stepper.
  27. *
  28. * Implements an ODE iterator solving the ODE with constant step size. Uses steppers fulfilling the Stepper concept.
  29. * n_step_iterator is a model of single-pass iterator.
  30. *
  31. * The value type of this iterator is the state type of the stepper. Hence one can only access the state and not the current time.
  32. *
  33. * \tparam Stepper The stepper type which should be used during the iteration.
  34. * \tparam System The type of the system function (ODE) which should be solved.
  35. */
  36. template< class Iterator , class Stepper , class System , class State , typename Tag >
  37. class n_step_iterator_impl< Iterator , Stepper , System , State , Tag , stepper_tag >
  38. : public detail::ode_iterator_base< Iterator , Stepper , System , State , Tag >
  39. {
  40. private:
  41. typedef Stepper stepper_type;
  42. typedef System system_type;
  43. typedef typename boost::numeric::odeint::unwrap_reference< stepper_type >::type unwrapped_stepper_type;
  44. typedef State state_type;
  45. typedef typename traits::time_type< stepper_type >::type time_type;
  46. typedef typename traits::value_type< stepper_type >::type ode_value_type;
  47. #ifndef DOXYGEN_SKIP
  48. typedef detail::ode_iterator_base< Iterator , Stepper , System , State , Tag > base_type;
  49. #endif
  50. public:
  51. /**
  52. * \brief Constructs a n_step_iterator. This constructor should be used to construct the begin iterator.
  53. *
  54. * \param stepper The stepper to use during the iteration.
  55. * \param sys The system function (ODE) to solve.
  56. * \param s The initial state. const_step_iterator stores a reference of s and changes its value during the iteration.
  57. * \param t The initial time.
  58. * \param dt The initial time step.
  59. * \param num_of_steps the number of steps to be executed.
  60. */
  61. n_step_iterator_impl( stepper_type stepper , system_type sys , state_type &s ,
  62. time_type t , time_type dt , size_t num_of_steps )
  63. : base_type( stepper , sys , t , dt ) , m_t_start( t ) , m_state( &s ) ,
  64. m_steps(num_of_steps) , m_step( 0 )
  65. { }
  66. /**
  67. * \brief Constructs a const_step_iterator. This constructor should be used to construct the end iterator.
  68. *
  69. * \param stepper The stepper to use during the iteration.
  70. * \param sys The system function (ODE) to solve.
  71. * \param s The initial state. const_step_iterator stores a reference of s and changes its value during the iteration.
  72. */
  73. n_step_iterator_impl( stepper_type stepper , system_type sys , state_type &s )
  74. : base_type( stepper , sys ) , m_state( &s ) { }
  75. protected:
  76. friend class boost::iterator_core_access;
  77. void increment()
  78. {
  79. if( this->m_step < this->m_steps )
  80. {
  81. unwrapped_stepper_type &stepper = this->m_stepper;
  82. stepper.do_step( this->m_system , *this->m_state , this->m_t , this->m_dt );
  83. // use integer to compute current time to reduce roundoff errors
  84. this->m_step++;
  85. this->m_t = this->m_t_start + static_cast< typename unit_value_type<time_type>::type >(this->m_step)*this->m_dt;
  86. } else {
  87. this->m_at_end = true;
  88. }
  89. }
  90. public:
  91. const state_type& get_state() const
  92. {
  93. return *m_state;
  94. }
  95. private:
  96. time_type m_t_start;
  97. time_type m_t_end;
  98. state_type* m_state;
  99. size_t m_steps;
  100. size_t m_step;
  101. };
  102. /*
  103. * Specilization for dense output stepper
  104. */
  105. /**
  106. * \brief ODE Iterator with step-size control and dense output.
  107. *
  108. * Implements an ODE iterator solving the ODE with constant steps. Uses dense-output steppers.
  109. * n_step_iterator is a model of single-pass iterator.
  110. *
  111. * The value type of this iterator is the state type of the stepper. Hence one can only access the state and not the current time.
  112. *
  113. * \tparam Stepper The stepper type which should be used during the iteration.
  114. * \tparam System The type of the system function (ODE) which should be solved.
  115. */
  116. template< class Iterator , class Stepper , class System , class State , typename Tag >
  117. class n_step_iterator_impl< Iterator , Stepper , System , State , Tag , dense_output_stepper_tag >
  118. : public detail::ode_iterator_base< Iterator , Stepper , System , State , Tag >
  119. {
  120. private:
  121. typedef Stepper stepper_type;
  122. typedef System system_type;
  123. typedef typename boost::numeric::odeint::unwrap_reference< stepper_type >::type unwrapped_stepper_type;
  124. typedef State state_type;
  125. typedef typename traits::time_type< stepper_type >::type time_type;
  126. typedef typename traits::value_type< stepper_type >::type ode_value_type;
  127. #ifndef DOXYGEN_SKIP
  128. typedef detail::ode_iterator_base< Iterator , Stepper , System , State , Tag > base_type;
  129. #endif
  130. public:
  131. /**
  132. * \brief Constructs a const_step_iterator. This constructor should be used to construct the begin iterator.
  133. *
  134. * \param stepper The stepper to use during the iteration.
  135. * \param sys The system function (ODE) to solve.
  136. * \param s The initial state. const_step_iterator stores a reference of s and changes its value during the iteration.
  137. * \param t The initial time.
  138. * \param dt The initial time step.
  139. * \param num_of_steps the number of steps to be executed.
  140. */
  141. n_step_iterator_impl( stepper_type stepper , system_type sys , state_type &s ,
  142. time_type t , time_type dt , size_t num_of_steps )
  143. : base_type( stepper , sys , t , dt ) , m_t_start( t ) , m_state( &s ) ,
  144. m_steps( num_of_steps ) , m_step( 0 )
  145. {
  146. unwrapped_stepper_type &st = this->m_stepper;
  147. st.initialize( * ( this->m_state ) , this->m_t , this->m_dt );
  148. }
  149. /**
  150. * \brief Constructs a const_step_iterator. This constructor should be used to construct the end iterator.
  151. *
  152. * \param stepper The stepper to use during the iteration.
  153. * \param sys The system function (ODE) to solve.
  154. * \param s The initial state. const_step_iterator stores a reference of s and changes its value during the iteration.
  155. */
  156. n_step_iterator_impl( stepper_type stepper , system_type sys , state_type &s )
  157. : base_type( stepper , sys ) , m_state( &s )
  158. {
  159. }
  160. protected:
  161. friend class boost::iterator_core_access;
  162. void increment( void )
  163. {
  164. if( this->m_step < this->m_steps )
  165. {
  166. unwrapped_stepper_type &stepper = this->m_stepper;
  167. // use integer to compute current time to reduce roundoff errors
  168. this->m_step++;
  169. this->m_t = this->m_t_start + static_cast< typename unit_value_type<time_type>::type >(this->m_step)*this->m_dt;
  170. while( detail::less_with_sign( stepper.current_time() , this->m_t ,
  171. stepper.current_time_step() ) )
  172. {
  173. stepper.do_step( this->m_system );
  174. }
  175. stepper.calc_state( this->m_t , *( this->m_state ) );
  176. } else {
  177. this->m_at_end = true;
  178. }
  179. }
  180. public:
  181. const state_type& get_state() const
  182. {
  183. return *m_state;
  184. }
  185. private:
  186. time_type m_t_start;
  187. time_type m_t_end;
  188. state_type* m_state;
  189. size_t m_steps;
  190. size_t m_step;
  191. };
  192. } // namespace odeint
  193. } // namespace numeric
  194. } // namespace boost
  195. #endif // BOOST_NUMERIC_ODEINT_ITERATOR_DETAIL_N_STEP_ITERATOR_IMPL_HPP_DEFINED