1 /* 2 [auto_generated] 3 boost/numeric/odeint/integrate/check_adapter.hpp 4 5 [begin_description] 6 Adapters to add checking facility to stepper and observer 7 [end_description] 8 9 Copyright 2015 Mario Mulansky 10 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 16 #ifndef BOOST_NUMERIC_ODEINT_INTEGRATE_CHECK_ADAPTER_HPP_INCLUDED 17 #define BOOST_NUMERIC_ODEINT_INTEGRATE_CHECK_ADAPTER_HPP_INCLUDED 18 19 #include <boost/numeric/odeint/stepper/stepper_categories.hpp> 20 #include <boost/numeric/odeint/stepper/controlled_step_result.hpp> 21 22 23 namespace boost { 24 namespace numeric { 25 namespace odeint { 26 27 template<class Stepper, class Checker, 28 class StepperCategory = typename base_tag<typename Stepper::stepper_category>::type> 29 class checked_stepper; 30 31 32 /** 33 * \brief Adapter to combine basic stepper and checker. 34 */ 35 template<class Stepper, class Checker> 36 class checked_stepper<Stepper, Checker, stepper_tag> 37 { 38 39 public: 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; 47 48 private: 49 stepper_type &m_stepper; 50 checker_type &m_checker; 51 52 public: 53 /** 54 * \brief Construct the checked_stepper. 55 */ checked_stepper(stepper_type & stepper,checker_type & checker)56 checked_stepper(stepper_type &stepper, checker_type &checker) 57 : m_stepper(stepper), m_checker(checker) { } 58 59 /** 60 * \brief forward of the do_step method 61 */ 62 template<class System, class StateInOut> do_step(System system,StateInOut & state,const time_type t,const time_type dt)63 void do_step(System system, StateInOut &state, const time_type t, const time_type dt) 64 { 65 // do the step 66 m_stepper.do_step(system, state, t, dt); 67 // call the checker 68 m_checker(); 69 } 70 }; 71 72 73 /** 74 * \brief Adapter to combine controlled stepper and checker. 75 */ 76 template<class ControlledStepper, class Checker> 77 class checked_stepper<ControlledStepper, Checker, controlled_stepper_tag> 78 { 79 80 public: 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; 88 89 private: 90 stepper_type &m_stepper; 91 checker_type &m_checker; 92 93 public: 94 /** 95 * \brief Construct the checked_stepper. 96 */ checked_stepper(stepper_type & stepper,checker_type & checker)97 checked_stepper(stepper_type &stepper, checker_type &checker) 98 : m_stepper(stepper), m_checker(checker) { } 99 100 /** 101 * \brief forward of the do_step method 102 */ 103 template< class System , class StateInOut > try_step(System system,StateInOut & state,time_type & t,time_type & dt)104 controlled_step_result try_step( System system , StateInOut &state , time_type &t , time_type &dt ) 105 { 106 // do the step 107 if( m_stepper.try_step(system, state, t, dt) == success ) 108 { 109 // call the checker if step was successful 110 m_checker(); 111 return success; 112 } else 113 { 114 // step failed -> return fail 115 return fail; 116 } 117 } 118 }; 119 120 121 /** 122 * \brief Adapter to combine dense out stepper and checker. 123 */ 124 template<class DenseOutStepper, class Checker> 125 class checked_stepper<DenseOutStepper, Checker, dense_output_stepper_tag> 126 { 127 128 public: 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; 136 137 private: 138 stepper_type &m_stepper; 139 checker_type &m_checker; 140 141 public: 142 /** 143 * \brief Construct the checked_stepper. 144 */ checked_stepper(stepper_type & stepper,checker_type & checker)145 checked_stepper(stepper_type &stepper, checker_type &checker) 146 : m_stepper(stepper), m_checker(checker) { } 147 148 149 template< class System > do_step(System system)150 std::pair< time_type , time_type > do_step( System system ) 151 { 152 m_checker(); 153 return m_stepper.do_step(system); 154 } 155 156 /* provide the remaining dense out stepper interface */ 157 template< class StateType > initialize(const StateType & x0,time_type t0,time_type dt0)158 void initialize( const StateType &x0 , time_type t0 , time_type dt0 ) 159 { m_stepper.initialize(x0, t0, dt0); } 160 161 162 template< class StateOut > calc_state(time_type t,StateOut & x) const163 void calc_state( time_type t , StateOut &x ) const 164 { m_stepper.calc_state(t, x); } 165 166 template< class StateOut > calc_state(time_type t,const StateOut & x) const167 void calc_state( time_type t , const StateOut &x ) const 168 { m_stepper.calc_state(t, x); } 169 current_state(void) const170 const state_type& current_state( void ) const 171 { return m_stepper.current_state(); } 172 current_time(void) const173 time_type current_time( void ) const 174 { return m_stepper.current_time(); } 175 previous_state(void) const176 const state_type& previous_state( void ) const 177 { return m_stepper.previous_state(); } 178 previous_time(void) const179 time_type previous_time( void ) const 180 { return m_stepper.previous_time(); } 181 current_time_step(void) const182 time_type current_time_step( void ) const 183 { return m_stepper.current_time_step(); } 184 185 }; 186 187 188 /** 189 * \brief Adapter to combine observer and checker. 190 */ 191 template<class Observer, class Checker> 192 class checked_observer 193 { 194 public: 195 typedef Observer observer_type; 196 typedef Checker checker_type; 197 198 private: 199 observer_type &m_observer; 200 checker_type &m_checker; 201 202 public: checked_observer(observer_type & observer,checker_type & checker)203 checked_observer(observer_type &observer, checker_type &checker) 204 : m_observer(observer), m_checker(checker) 205 {} 206 207 template< class State , class Time > operator ()(const State & state,Time t) const208 void operator()(const State& state, Time t) const 209 { 210 // call the observer 211 m_observer(state, t); 212 // reset the checker 213 m_checker.reset(); 214 } 215 }; 216 217 218 } // namespace odeint 219 } // namespace numeric 220 } // namespace boost 221 222 #endif