check_adapter.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/integrate/check_adapter.hpp
  4. [begin_description]
  5. Adapters to add checking facility to stepper and observer
  6. [end_description]
  7. Copyright 2015 Mario Mulansky
  8. Distributed under the Boost Software License, Version 1.0.
  9. (See accompanying file LICENSE_1_0.txt or
  10. copy at http://www.boost.org/LICENSE_1_0.txt)
  11. */
  12. #ifndef BOOST_NUMERIC_ODEINT_INTEGRATE_CHECK_ADAPTER_HPP_INCLUDED
  13. #define BOOST_NUMERIC_ODEINT_INTEGRATE_CHECK_ADAPTER_HPP_INCLUDED
  14. #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
  15. #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
  16. namespace boost {
  17. namespace numeric {
  18. namespace odeint {
  19. template<class Stepper, class Checker,
  20. class StepperCategory = typename base_tag<typename Stepper::stepper_category>::type>
  21. class checked_stepper;
  22. /**
  23. * \brief Adapter to combine basic stepper and checker.
  24. */
  25. template<class Stepper, class Checker>
  26. class checked_stepper<Stepper, Checker, stepper_tag>
  27. {
  28. public:
  29. typedef Stepper stepper_type;
  30. typedef Checker checker_type;
  31. // forward stepper typedefs
  32. typedef typename stepper_type::state_type state_type;
  33. typedef typename stepper_type::value_type value_type;
  34. typedef typename stepper_type::deriv_type deriv_type;
  35. typedef typename stepper_type::time_type time_type;
  36. private:
  37. stepper_type &m_stepper;
  38. checker_type &m_checker;
  39. public:
  40. /**
  41. * \brief Construct the checked_stepper.
  42. */
  43. checked_stepper(stepper_type &stepper, checker_type &checker)
  44. : m_stepper(stepper), m_checker(checker) { }
  45. /**
  46. * \brief forward of the do_step method
  47. */
  48. template<class System, class StateInOut>
  49. void do_step(System system, StateInOut &state, const time_type t, const time_type dt)
  50. {
  51. // do the step
  52. m_stepper.do_step(system, state, t, dt);
  53. // call the checker
  54. m_checker();
  55. }
  56. };
  57. /**
  58. * \brief Adapter to combine controlled stepper and checker.
  59. */
  60. template<class ControlledStepper, class Checker>
  61. class checked_stepper<ControlledStepper, Checker, controlled_stepper_tag>
  62. {
  63. public:
  64. typedef ControlledStepper stepper_type;
  65. typedef Checker checker_type;
  66. // forward stepper typedefs
  67. typedef typename stepper_type::state_type state_type;
  68. typedef typename stepper_type::value_type value_type;
  69. typedef typename stepper_type::deriv_type deriv_type;
  70. typedef typename stepper_type::time_type time_type;
  71. private:
  72. stepper_type &m_stepper;
  73. checker_type &m_checker;
  74. public:
  75. /**
  76. * \brief Construct the checked_stepper.
  77. */
  78. checked_stepper(stepper_type &stepper, checker_type &checker)
  79. : m_stepper(stepper), m_checker(checker) { }
  80. /**
  81. * \brief forward of the do_step method
  82. */
  83. template< class System , class StateInOut >
  84. controlled_step_result try_step( System system , StateInOut &state , time_type &t , time_type &dt )
  85. {
  86. // do the step
  87. if( m_stepper.try_step(system, state, t, dt) == success )
  88. {
  89. // call the checker if step was successful
  90. m_checker();
  91. return success;
  92. } else
  93. {
  94. // step failed -> return fail
  95. return fail;
  96. }
  97. }
  98. };
  99. /**
  100. * \brief Adapter to combine dense out stepper and checker.
  101. */
  102. template<class DenseOutStepper, class Checker>
  103. class checked_stepper<DenseOutStepper, Checker, dense_output_stepper_tag>
  104. {
  105. public:
  106. typedef DenseOutStepper stepper_type;
  107. typedef Checker checker_type;
  108. // forward stepper typedefs
  109. typedef typename stepper_type::state_type state_type;
  110. typedef typename stepper_type::value_type value_type;
  111. typedef typename stepper_type::deriv_type deriv_type;
  112. typedef typename stepper_type::time_type time_type;
  113. private:
  114. stepper_type &m_stepper;
  115. checker_type &m_checker;
  116. public:
  117. /**
  118. * \brief Construct the checked_stepper.
  119. */
  120. checked_stepper(stepper_type &stepper, checker_type &checker)
  121. : m_stepper(stepper), m_checker(checker) { }
  122. template< class System >
  123. std::pair< time_type , time_type > do_step( System system )
  124. {
  125. m_checker();
  126. return m_stepper.do_step(system);
  127. }
  128. /* provide the remaining dense out stepper interface */
  129. template< class StateType >
  130. void initialize( const StateType &x0 , time_type t0 , time_type dt0 )
  131. { m_stepper.initialize(x0, t0, dt0); }
  132. template< class StateOut >
  133. void calc_state( time_type t , StateOut &x ) const
  134. { m_stepper.calc_state(t, x); }
  135. template< class StateOut >
  136. void calc_state( time_type t , const StateOut &x ) const
  137. { m_stepper.calc_state(t, x); }
  138. const state_type& current_state( void ) const
  139. { return m_stepper.current_state(); }
  140. time_type current_time( void ) const
  141. { return m_stepper.current_time(); }
  142. const state_type& previous_state( void ) const
  143. { return m_stepper.previous_state(); }
  144. time_type previous_time( void ) const
  145. { return m_stepper.previous_time(); }
  146. time_type current_time_step( void ) const
  147. { return m_stepper.current_time_step(); }
  148. };
  149. /**
  150. * \brief Adapter to combine observer and checker.
  151. */
  152. template<class Observer, class Checker>
  153. class checked_observer
  154. {
  155. public:
  156. typedef Observer observer_type;
  157. typedef Checker checker_type;
  158. private:
  159. observer_type &m_observer;
  160. checker_type &m_checker;
  161. public:
  162. checked_observer(observer_type &observer, checker_type &checker)
  163. : m_observer(observer), m_checker(checker)
  164. {}
  165. template< class State , class Time >
  166. void operator()(const State& state, Time t) const
  167. {
  168. // call the observer
  169. m_observer(state, t);
  170. // reset the checker
  171. m_checker.reset();
  172. }
  173. };
  174. } // namespace odeint
  175. } // namespace numeric
  176. } // namespace boost
  177. #endif