rosenbrock4_controller.hpp 5.9 KB

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