const_step_iterator_impl.hpp 8.7 KB

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