3 boost/numeric/odeint/integrate/check_adapter.hpp
6 Adapters to add checking facility to stepper and observer
9 Copyright 2015 Mario Mulansky
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)
16 #ifndef BOOST_NUMERIC_ODEINT_INTEGRATE_CHECK_ADAPTER_HPP_INCLUDED
17 #define BOOST_NUMERIC_ODEINT_INTEGRATE_CHECK_ADAPTER_HPP_INCLUDED
19 #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
20 #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
27 template<class Stepper, class Checker,
28 class StepperCategory = typename base_tag<typename Stepper::stepper_category>::type>
29 class checked_stepper;
33 * \brief Adapter to combine basic stepper and checker.
35 template<class Stepper, class Checker>
36 class checked_stepper<Stepper, Checker, stepper_tag>
40 typedef Stepper stepper_type;
41 typedef Checker checker_type;
42 // forward stepper typedefs
43 typedef typename stepper_type::state_type state_type;
44 typedef typename stepper_type::value_type value_type;
45 typedef typename stepper_type::deriv_type deriv_type;
46 typedef typename stepper_type::time_type time_type;
49 stepper_type &m_stepper;
50 checker_type &m_checker;
54 * \brief Construct the checked_stepper.
56 checked_stepper(stepper_type &stepper, checker_type &checker)
57 : m_stepper(stepper), m_checker(checker) { }
60 * \brief forward of the do_step method
62 template<class System, class StateInOut>
63 void do_step(System system, StateInOut &state, const time_type t, const time_type dt)
66 m_stepper.do_step(system, state, t, dt);
74 * \brief Adapter to combine controlled stepper and checker.
76 template<class ControlledStepper, class Checker>
77 class checked_stepper<ControlledStepper, Checker, controlled_stepper_tag>
81 typedef ControlledStepper stepper_type;
82 typedef Checker checker_type;
83 // forward stepper typedefs
84 typedef typename stepper_type::state_type state_type;
85 typedef typename stepper_type::value_type value_type;
86 typedef typename stepper_type::deriv_type deriv_type;
87 typedef typename stepper_type::time_type time_type;
90 stepper_type &m_stepper;
91 checker_type &m_checker;
95 * \brief Construct the checked_stepper.
97 checked_stepper(stepper_type &stepper, checker_type &checker)
98 : m_stepper(stepper), m_checker(checker) { }
101 * \brief forward of the do_step method
103 template< class System , class StateInOut >
104 controlled_step_result try_step( System system , StateInOut &state , time_type &t , time_type &dt )
107 if( m_stepper.try_step(system, state, t, dt) == success )
109 // call the checker if step was successful
114 // step failed -> return fail
122 * \brief Adapter to combine dense out stepper and checker.
124 template<class DenseOutStepper, class Checker>
125 class checked_stepper<DenseOutStepper, Checker, dense_output_stepper_tag>
129 typedef DenseOutStepper stepper_type;
130 typedef Checker checker_type;
131 // forward stepper typedefs
132 typedef typename stepper_type::state_type state_type;
133 typedef typename stepper_type::value_type value_type;
134 typedef typename stepper_type::deriv_type deriv_type;
135 typedef typename stepper_type::time_type time_type;
138 stepper_type &m_stepper;
139 checker_type &m_checker;
143 * \brief Construct the checked_stepper.
145 checked_stepper(stepper_type &stepper, checker_type &checker)
146 : m_stepper(stepper), m_checker(checker) { }
149 template< class System >
150 std::pair< time_type , time_type > do_step( System system )
153 return m_stepper.do_step(system);
156 /* provide the remaining dense out stepper interface */
157 template< class StateType >
158 void initialize( const StateType &x0 , time_type t0 , time_type dt0 )
159 { m_stepper.initialize(x0, t0, dt0); }
162 template< class StateOut >
163 void calc_state( time_type t , StateOut &x ) const
164 { m_stepper.calc_state(t, x); }
166 template< class StateOut >
167 void calc_state( time_type t , const StateOut &x ) const
168 { m_stepper.calc_state(t, x); }
170 const state_type& current_state( void ) const
171 { return m_stepper.current_state(); }
173 time_type current_time( void ) const
174 { return m_stepper.current_time(); }
176 const state_type& previous_state( void ) const
177 { return m_stepper.previous_state(); }
179 time_type previous_time( void ) const
180 { return m_stepper.previous_time(); }
182 time_type current_time_step( void ) const
183 { return m_stepper.current_time_step(); }
189 * \brief Adapter to combine observer and checker.
191 template<class Observer, class Checker>
192 class checked_observer
195 typedef Observer observer_type;
196 typedef Checker checker_type;
199 observer_type &m_observer;
200 checker_type &m_checker;
203 checked_observer(observer_type &observer, checker_type &checker)
204 : m_observer(observer), m_checker(checker)
207 template< class State , class Time >
208 void operator()(const State& state, Time t) const
211 m_observer(state, t);
218 } // namespace odeint
219 } // namespace numeric