3 boost/numeric/odeint/iterator/detail/times_iterator_impl.hpp
9 Copyright 2009-2013 Karsten Ahnert
10 Copyright 2009-2013 Mario Mulansky
12 Distributed under the Boost Software License, Version 1.0.
13 (See accompanying file LICENSE_1_0.txt or
14 copy at http://www.boost.org/LICENSE_1_0.txt)
18 #ifndef BOOST_NUMERIC_ODEINT_ITERATOR_DETAIL_TIMES_ITERATOR_IMPL_HPP_DEFINED
19 #define BOOST_NUMERIC_ODEINT_ITERATOR_DETAIL_TIMES_ITERATOR_IMPL_HPP_DEFINED
21 #include <boost/utility/enable_if.hpp>
22 #include <boost/type_traits/is_same.hpp>
23 #include <boost/throw_exception.hpp>
25 #include <boost/numeric/odeint/util/unit_helper.hpp>
26 #include <boost/numeric/odeint/util/copy.hpp>
27 #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
28 #include <boost/numeric/odeint/iterator/detail/ode_iterator_base.hpp>
36 template< class Iterator , class Stepper , class System , class State , class TimeIterator ,
37 typename Tag , typename StepperTag >
38 class times_iterator_impl;
41 * Specilization for basic steppers
44 * \brief ODE Iterator with constant step size.
46 * Implements an ODE iterator with observer calls at predefined times.
47 * Uses controlled steppers. times_iterator is a model of single-pass iterator.
49 * 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.
51 * \tparam Stepper The stepper type which should be used during the iteration.
52 * \tparam System The type of the system function (ODE) which should be solved.
54 template< class Iterator , class Stepper , class System , class State , class TimeIterator , typename Tag >
55 class times_iterator_impl< Iterator , Stepper , System , State , TimeIterator , Tag , stepper_tag >
56 : public detail::ode_iterator_base< Iterator , Stepper , System , State , Tag >
61 typedef Stepper stepper_type;
62 typedef System system_type;
63 typedef typename boost::numeric::odeint::unwrap_reference< stepper_type >::type unwrapped_stepper_type;
64 typedef State state_type;
65 typedef TimeIterator time_iterator_type;
66 typedef typename traits::time_type< stepper_type >::type time_type;
67 typedef typename traits::value_type< stepper_type >::type ode_value_type;
69 typedef detail::ode_iterator_base< Iterator , Stepper , System , State , Tag > base_type;
75 * \brief Constructs a times_iterator. This constructor should be used to construct the begin iterator.
77 * \param stepper The stepper to use during the iteration.
78 * \param sys The system function (ODE) to solve.
79 * \param s The initial state. adaptive_iterator stores a reference of s and changes its value during the iteration.
80 * \param t_start Iterator to the begin of a sequence of time values.
81 * \param t_end Iterator to the begin of a sequence of time values.
82 * \param dt The (initial) time step.
84 times_iterator_impl( stepper_type stepper , system_type sys , state_type &s ,
85 time_iterator_type t_start , time_iterator_type t_end , time_type dt )
86 : base_type( stepper , sys , *t_start , dt ) ,
87 m_t_start( t_start ) , m_t_end( t_end ) , m_state( &s )
89 if( t_start == t_end )
90 this->m_at_end = true;
94 * \brief Constructs an adaptive_iterator. This constructor should be used to construct the end iterator.
96 * \param stepper The stepper to use during the iteration.
97 * \param sys The system function (ODE) to solve.
98 * \param s The initial state. adaptive_iterator store a reference of s and changes its value during the iteration.
100 times_iterator_impl( stepper_type stepper , system_type sys , state_type &s )
101 : base_type( stepper , sys ) , m_state( &s ) { }
105 friend class boost::iterator_core_access;
109 unwrapped_stepper_type &stepper = this->m_stepper;
110 if( ++m_t_start != m_t_end )
112 while( detail::less_with_sign( this->m_t , static_cast<time_type>(*m_t_start) , this->m_dt ) )
114 const time_type current_dt = detail::min_abs( this->m_dt , static_cast<time_type>(*m_t_start) - this->m_t );
115 stepper.do_step( this->m_system , *( this->m_state ) , this->m_t , current_dt );
116 this->m_t += current_dt;
120 this->m_at_end = true;
125 const state_type& get_state() const
131 time_iterator_type m_t_start;
132 time_iterator_type m_t_end;
139 * Specilization for controlled steppers
142 * \brief ODE Iterator with adaptive step size control. The value type of this iterator is the state type of the stepper.
144 * Implements an ODE iterator with observer calls at predefined times.
145 * Uses controlled steppers. times_iterator is a model of single-pass iterator.
147 * 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.
149 * \tparam Stepper The stepper type which should be used during the iteration.
150 * \tparam System The type of the system function (ODE) which should be solved.
152 template< class Iterator , class Stepper , class System , class State , class TimeIterator , typename Tag >
153 class times_iterator_impl< Iterator , Stepper , System , State , TimeIterator , Tag , controlled_stepper_tag >
154 : public detail::ode_iterator_base< Iterator , Stepper , System , State , Tag >
159 typedef Stepper stepper_type;
160 typedef System system_type;
161 typedef typename boost::numeric::odeint::unwrap_reference< stepper_type >::type unwrapped_stepper_type;
162 typedef State state_type;
163 typedef TimeIterator time_iterator_type;
164 typedef typename traits::time_type< stepper_type >::type time_type;
165 typedef typename traits::value_type< stepper_type >::type ode_value_type;
167 typedef detail::ode_iterator_base< Iterator , Stepper , System , State , Tag > base_type;
173 * \brief Constructs a times_iterator. This constructor should be used to construct the begin iterator.
175 * \param stepper The stepper to use during the iteration.
176 * \param sys The system function (ODE) to solve.
177 * \param s The initial state. adaptive_iterator stores a reference of s and changes its value during the iteration.
178 * \param t_start Iterator to the begin of a sequence of time values.
179 * \param t_end Iterator to the begin of a sequence of time values.
180 * \param dt The (initial) time step.
182 times_iterator_impl( stepper_type stepper , system_type sys , state_type &s ,
183 time_iterator_type t_start , time_iterator_type t_end , time_type dt )
184 : base_type( stepper , sys , *t_start , dt ) ,
185 m_t_start( t_start ) , m_t_end( t_end ) , m_state( &s )
187 if( t_start == t_end )
188 this->m_at_end = true;
192 * \brief Constructs an adaptive_iterator. This constructor should be used to construct the end iterator.
194 * \param stepper The stepper to use during the iteration.
195 * \param sys The system function (ODE) to solve.
196 * \param s The initial state. adaptive_iterator store a reference of s and changes its value during the iteration.
198 times_iterator_impl( stepper_type stepper , system_type sys , state_type &s )
199 : base_type( stepper , sys ) , m_state( &s ) { }
203 friend class boost::iterator_core_access;
207 if( ++m_t_start != m_t_end )
209 while( detail::less_with_sign( this->m_t , static_cast<time_type>(*m_t_start) , this->m_dt ) )
211 if( detail::less_with_sign( static_cast<time_type>(*m_t_start) - this->m_t , this->m_dt , this->m_dt ) )
213 // we want to end exactly at the time point
214 time_type current_dt = static_cast<time_type>(*m_t_start) - this->m_t;
215 step_loop( current_dt );
217 step_loop( this->m_dt );
222 this->m_at_end = true;
227 void step_loop( time_type &dt )
229 unwrapped_stepper_type &stepper = this->m_stepper;
230 const size_t max_attempts = 1000;
232 controlled_step_result res = success;
235 res = stepper.try_step( this->m_system , *( this->m_state ) , this->m_t , dt );
238 while( ( res == fail ) && ( trials < max_attempts ) );
239 if( trials == max_attempts )
241 BOOST_THROW_EXCEPTION( std::overflow_error( "Adaptive iterator : Maximal number of iterations reached. A step size could not be found." ) );
246 const state_type& get_state() const
253 time_iterator_type m_t_start;
254 time_iterator_type m_t_end;
260 * Specilization for dense outputer steppers
263 * \brief ODE Iterator with step size control and dense output.
264 * Implements an ODE iterator with adaptive step size control. Uses dense-output steppers.
265 * times_iterator is a model of single-pass iterator.
267 * \tparam Stepper The stepper type which should be used during the iteration.
268 * \tparam System The type of the system function (ODE) which should be solved.
270 template< class Iterator , class Stepper , class System , class State , class TimeIterator , typename Tag >
271 class times_iterator_impl< Iterator , Stepper , System , State , TimeIterator , Tag , dense_output_stepper_tag >
272 : public detail::ode_iterator_base< Iterator , Stepper , System , State , Tag >
277 typedef Stepper stepper_type;
278 typedef System system_type;
279 typedef typename boost::numeric::odeint::unwrap_reference< stepper_type >::type unwrapped_stepper_type;
280 typedef State state_type;
281 typedef TimeIterator time_iterator_type;
282 typedef typename traits::time_type< stepper_type >::type time_type;
283 typedef typename traits::value_type< stepper_type >::type ode_value_type;
285 typedef detail::ode_iterator_base< Iterator , Stepper , System , State , Tag > base_type;
293 * \brief Constructs a times_iterator. This constructor should be used to construct the begin iterator.
295 * \param stepper The stepper to use during the iteration.
296 * \param sys The system function (ODE) to solve.
297 * \param s The initial state.
298 * \param t_start Iterator to the begin of a sequence of time values.
299 * \param t_end Iterator to the begin of a sequence of time values.
300 * \param dt The (initial) time step.
302 times_iterator_impl( stepper_type stepper , system_type sys , state_type &s ,
303 time_iterator_type t_start , time_iterator_type t_end , time_type dt )
304 : base_type( stepper , sys , *t_start , dt ) ,
305 m_t_start( t_start ) , m_t_end( t_end ) , m_final_time( *(t_end-1) ) ,
308 if( t_start != t_end )
310 unwrapped_stepper_type &st = this->m_stepper;
311 st.initialize( *( this->m_state ) , this->m_t , this->m_dt );
313 this->m_at_end = true;
318 * \brief Constructs a times_iterator. This constructor should be used to construct the end iterator.
320 * \param stepper The stepper to use during the iteration.
321 * \param sys The system function (ODE) to solve.
322 * \param s The initial state.
324 times_iterator_impl( stepper_type stepper , system_type sys , state_type &s )
325 : base_type( stepper , sys ) , m_state( &s ) { }
329 friend class boost::iterator_core_access;
333 unwrapped_stepper_type &st = this->m_stepper;
334 if( ++m_t_start != m_t_end )
336 this->m_t = static_cast<time_type>(*m_t_start);
337 while( detail::less_with_sign( st.current_time() , this->m_t , this->m_dt ) )
339 // make sure we don't go beyond the last point
340 if( detail::less_with_sign( m_final_time-st.current_time() , st.current_time_step() , st.current_time_step() ) )
342 st.initialize( st.current_state() , st.current_time() , m_final_time-st.current_time() );
344 st.do_step( this->m_system );
346 st.calc_state( this->m_t , *( this->m_state ) );
348 this->m_at_end = true;
353 const state_type& get_state() const
360 time_iterator_type m_t_start;
361 time_iterator_type m_t_end;
362 time_type m_final_time;
366 } // namespace odeint
367 } // namespace numeric
371 #endif // BOOST_NUMERIC_ODEINT_ITERATOR_DETAIL_TIMES_ITERATOR_IMPL_HPP_DEFINED