rosenbrock4_controller.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/stepper/rosenbrock4_controller.hpp
  4. [begin_description]
  5. Controller for the Rosenbrock4 method.
  6. [end_description]
  7. Copyright 2011-2012 Karsten Ahnert
  8. Copyright 2011-2012 Mario Mulansky
  9. Copyright 2012 Christoph Koke
  10. Distributed under the Boost Software License, Version 1.0.
  11. (See accompanying file LICENSE_1_0.txt or
  12. copy at http://www.boost.org/LICENSE_1_0.txt)
  13. */
  14. #ifndef BOOST_NUMERIC_ODEINT_STEPPER_ROSENBROCK4_CONTROLLER_HPP_INCLUDED
  15. #define BOOST_NUMERIC_ODEINT_STEPPER_ROSENBROCK4_CONTROLLER_HPP_INCLUDED
  16. #include <boost/config.hpp>
  17. #include <boost/numeric/odeint/util/bind.hpp>
  18. #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
  19. #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
  20. #include <boost/numeric/odeint/util/copy.hpp>
  21. #include <boost/numeric/odeint/util/is_resizeable.hpp>
  22. #include <boost/numeric/odeint/util/detail/less_with_sign.hpp>
  23. #include <boost/numeric/odeint/stepper/rosenbrock4.hpp>
  24. namespace boost {
  25. namespace numeric {
  26. namespace odeint {
  27. template< class Stepper >
  28. class rosenbrock4_controller
  29. {
  30. private:
  31. public:
  32. typedef Stepper stepper_type;
  33. typedef typename stepper_type::value_type value_type;
  34. typedef typename stepper_type::state_type state_type;
  35. typedef typename stepper_type::wrapped_state_type wrapped_state_type;
  36. typedef typename stepper_type::time_type time_type;
  37. typedef typename stepper_type::deriv_type deriv_type;
  38. typedef typename stepper_type::wrapped_deriv_type wrapped_deriv_type;
  39. typedef typename stepper_type::resizer_type resizer_type;
  40. typedef controlled_stepper_tag stepper_category;
  41. typedef rosenbrock4_controller< Stepper > controller_type;
  42. rosenbrock4_controller( value_type atol = 1.0e-6 , value_type rtol = 1.0e-6 ,
  43. const stepper_type &stepper = stepper_type() )
  44. : m_stepper( stepper ) , m_atol( atol ) , m_rtol( rtol ) ,
  45. m_max_dt( static_cast<time_type>(0) ) ,
  46. m_first_step( true ) , m_err_old( 0.0 ) , m_dt_old( 0.0 ) ,
  47. m_last_rejected( false )
  48. { }
  49. rosenbrock4_controller( value_type atol, value_type rtol, time_type max_dt,
  50. const stepper_type &stepper = stepper_type() )
  51. : m_stepper( stepper ) , m_atol( atol ) , m_rtol( rtol ) , m_max_dt( max_dt ) ,
  52. m_first_step( true ) , m_err_old( 0.0 ) , m_dt_old( 0.0 ) ,
  53. m_last_rejected( false )
  54. { }
  55. value_type error( const state_type &x , const state_type &xold , const state_type &xerr )
  56. {
  57. BOOST_USING_STD_MAX();
  58. using std::abs;
  59. using std::sqrt;
  60. const size_t n = x.size();
  61. value_type err = 0.0 , sk = 0.0;
  62. for( size_t i=0 ; i<n ; ++i )
  63. {
  64. sk = m_atol + m_rtol * max BOOST_PREVENT_MACRO_SUBSTITUTION ( abs( xold[i] ) , abs( x[i] ) );
  65. err += xerr[i] * xerr[i] / sk / sk;
  66. }
  67. return sqrt( err / value_type( n ) );
  68. }
  69. value_type last_error( void ) const
  70. {
  71. return m_err_old;
  72. }
  73. template< class System >
  74. boost::numeric::odeint::controlled_step_result
  75. try_step( System sys , state_type &x , time_type &t , time_type &dt )
  76. {
  77. m_xnew_resizer.adjust_size( x , detail::bind( &controller_type::template resize_m_xnew< state_type > , detail::ref( *this ) , detail::_1 ) );
  78. boost::numeric::odeint::controlled_step_result res = try_step( sys , x , t , m_xnew.m_v , dt );
  79. if( res == success )
  80. {
  81. boost::numeric::odeint::copy( m_xnew.m_v , x );
  82. }
  83. return res;
  84. }
  85. template< class System >
  86. boost::numeric::odeint::controlled_step_result
  87. try_step( System sys , const state_type &x , time_type &t , state_type &xout , time_type &dt )
  88. {
  89. if( m_max_dt != static_cast<time_type>(0) && detail::less_with_sign(m_max_dt, dt, dt) )
  90. {
  91. // given step size is bigger then max_dt
  92. // set limit and return fail
  93. dt = m_max_dt;
  94. return fail;
  95. }
  96. BOOST_USING_STD_MIN();
  97. BOOST_USING_STD_MAX();
  98. using std::pow;
  99. static const value_type safe = 0.9 , fac1 = 5.0 , fac2 = 1.0 / 6.0;
  100. m_xerr_resizer.adjust_size( x , detail::bind( &controller_type::template resize_m_xerr< state_type > , detail::ref( *this ) , detail::_1 ) );
  101. m_stepper.do_step( sys , x , t , xout , dt , m_xerr.m_v );
  102. value_type err = error( xout , x , m_xerr.m_v );
  103. value_type fac = max BOOST_PREVENT_MACRO_SUBSTITUTION (
  104. fac2 , min BOOST_PREVENT_MACRO_SUBSTITUTION (
  105. fac1 ,
  106. static_cast< value_type >( pow( err , 0.25 ) / safe ) ) );
  107. value_type dt_new = dt / fac;
  108. if ( err <= 1.0 )
  109. {
  110. if( m_first_step )
  111. {
  112. m_first_step = false;
  113. }
  114. else
  115. {
  116. value_type fac_pred = ( m_dt_old / dt ) * pow( err * err / m_err_old , 0.25 ) / safe;
  117. fac_pred = max BOOST_PREVENT_MACRO_SUBSTITUTION (
  118. fac2 , min BOOST_PREVENT_MACRO_SUBSTITUTION ( fac1 , fac_pred ) );
  119. fac = max BOOST_PREVENT_MACRO_SUBSTITUTION ( fac , fac_pred );
  120. dt_new = dt / fac;
  121. }
  122. m_dt_old = dt;
  123. m_err_old = max BOOST_PREVENT_MACRO_SUBSTITUTION ( static_cast< value_type >( 0.01 ) , err );
  124. if( m_last_rejected )
  125. dt_new = ( dt >= 0.0 ?
  126. min BOOST_PREVENT_MACRO_SUBSTITUTION ( dt_new , dt ) :
  127. max BOOST_PREVENT_MACRO_SUBSTITUTION ( dt_new , dt ) );
  128. t += dt;
  129. // limit step size to max_dt
  130. if( m_max_dt != static_cast<time_type>(0) )
  131. {
  132. dt = detail::min_abs(m_max_dt, dt_new);
  133. } else {
  134. dt = dt_new;
  135. }
  136. m_last_rejected = false;
  137. return success;
  138. }
  139. else
  140. {
  141. dt = dt_new;
  142. m_last_rejected = true;
  143. return fail;
  144. }
  145. }
  146. template< class StateType >
  147. void adjust_size( const StateType &x )
  148. {
  149. resize_m_xerr( x );
  150. resize_m_xnew( x );
  151. }
  152. stepper_type& stepper( void )
  153. {
  154. return m_stepper;
  155. }
  156. const stepper_type& stepper( void ) const
  157. {
  158. return m_stepper;
  159. }
  160. protected:
  161. template< class StateIn >
  162. bool resize_m_xerr( const StateIn &x )
  163. {
  164. return adjust_size_by_resizeability( m_xerr , x , typename is_resizeable<state_type>::type() );
  165. }
  166. template< class StateIn >
  167. bool resize_m_xnew( const StateIn &x )
  168. {
  169. return adjust_size_by_resizeability( m_xnew , x , typename is_resizeable<state_type>::type() );
  170. }
  171. stepper_type m_stepper;
  172. resizer_type m_xerr_resizer;
  173. resizer_type m_xnew_resizer;
  174. wrapped_state_type m_xerr;
  175. wrapped_state_type m_xnew;
  176. value_type m_atol , m_rtol;
  177. time_type m_max_dt;
  178. bool m_first_step;
  179. value_type m_err_old , m_dt_old;
  180. bool m_last_rejected;
  181. };
  182. } // namespace odeint
  183. } // namespace numeric
  184. } // namespace boost
  185. #endif // BOOST_NUMERIC_ODEINT_STEPPER_ROSENBROCK4_CONTROLLER_HPP_INCLUDED