3 boost/numeric/odeint/stepper/rosenbrock4_controller.hpp
6 Controller for the Rosenbrock4 method.
9 Copyright 2011-2012 Karsten Ahnert
10 Copyright 2011-2012 Mario Mulansky
11 Copyright 2012 Christoph Koke
13 Distributed under the Boost Software License, Version 1.0.
14 (See accompanying file LICENSE_1_0.txt or
15 copy at http://www.boost.org/LICENSE_1_0.txt)
19 #ifndef BOOST_NUMERIC_ODEINT_STEPPER_ROSENBROCK4_CONTROLLER_HPP_INCLUDED
20 #define BOOST_NUMERIC_ODEINT_STEPPER_ROSENBROCK4_CONTROLLER_HPP_INCLUDED
22 #include <boost/config.hpp>
23 #include <boost/numeric/odeint/util/bind.hpp>
25 #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
26 #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
28 #include <boost/numeric/odeint/util/copy.hpp>
29 #include <boost/numeric/odeint/util/is_resizeable.hpp>
30 #include <boost/numeric/odeint/util/detail/less_with_sign.hpp>
32 #include <boost/numeric/odeint/stepper/rosenbrock4.hpp>
38 template< class Stepper >
39 class rosenbrock4_controller
46 typedef Stepper stepper_type;
47 typedef typename stepper_type::value_type value_type;
48 typedef typename stepper_type::state_type state_type;
49 typedef typename stepper_type::wrapped_state_type wrapped_state_type;
50 typedef typename stepper_type::time_type time_type;
51 typedef typename stepper_type::deriv_type deriv_type;
52 typedef typename stepper_type::wrapped_deriv_type wrapped_deriv_type;
53 typedef typename stepper_type::resizer_type resizer_type;
54 typedef controlled_stepper_tag stepper_category;
56 typedef rosenbrock4_controller< Stepper > controller_type;
59 rosenbrock4_controller( value_type atol = 1.0e-6 , value_type rtol = 1.0e-6 ,
60 const stepper_type &stepper = stepper_type() )
61 : m_stepper( stepper ) , m_atol( atol ) , m_rtol( rtol ) ,
62 m_max_dt( static_cast<time_type>(0) ) ,
63 m_first_step( true ) , m_err_old( 0.0 ) , m_dt_old( 0.0 ) ,
64 m_last_rejected( false )
67 rosenbrock4_controller( value_type atol, value_type rtol, time_type max_dt,
68 const stepper_type &stepper = stepper_type() )
69 : m_stepper( stepper ) , m_atol( atol ) , m_rtol( rtol ) , m_max_dt( max_dt ) ,
70 m_first_step( true ) , m_err_old( 0.0 ) , m_dt_old( 0.0 ) ,
71 m_last_rejected( false )
74 value_type error( const state_type &x , const state_type &xold , const state_type &xerr )
76 BOOST_USING_STD_MAX();
80 const size_t n = x.size();
81 value_type err = 0.0 , sk = 0.0;
82 for( size_t i=0 ; i<n ; ++i )
84 sk = m_atol + m_rtol * max BOOST_PREVENT_MACRO_SUBSTITUTION ( abs( xold[i] ) , abs( x[i] ) );
85 err += xerr[i] * xerr[i] / sk / sk;
87 return sqrt( err / value_type( n ) );
90 value_type last_error( void ) const
98 template< class System >
99 boost::numeric::odeint::controlled_step_result
100 try_step( System sys , state_type &x , time_type &t , time_type &dt )
102 m_xnew_resizer.adjust_size( x , detail::bind( &controller_type::template resize_m_xnew< state_type > , detail::ref( *this ) , detail::_1 ) );
103 boost::numeric::odeint::controlled_step_result res = try_step( sys , x , t , m_xnew.m_v , dt );
106 boost::numeric::odeint::copy( m_xnew.m_v , x );
112 template< class System >
113 boost::numeric::odeint::controlled_step_result
114 try_step( System sys , const state_type &x , time_type &t , state_type &xout , time_type &dt )
116 if( m_max_dt != static_cast<time_type>(0) && detail::less_with_sign(m_max_dt, dt, dt) )
118 // given step size is bigger then max_dt
119 // set limit and return fail
124 BOOST_USING_STD_MIN();
125 BOOST_USING_STD_MAX();
128 static const value_type safe = 0.9 , fac1 = 5.0 , fac2 = 1.0 / 6.0;
130 m_xerr_resizer.adjust_size( x , detail::bind( &controller_type::template resize_m_xerr< state_type > , detail::ref( *this ) , detail::_1 ) );
132 m_stepper.do_step( sys , x , t , xout , dt , m_xerr.m_v );
133 value_type err = error( xout , x , m_xerr.m_v );
135 value_type fac = max BOOST_PREVENT_MACRO_SUBSTITUTION (
136 fac2 , min BOOST_PREVENT_MACRO_SUBSTITUTION (
138 static_cast< value_type >( pow( err , 0.25 ) / safe ) ) );
139 value_type dt_new = dt / fac;
144 m_first_step = false;
148 value_type fac_pred = ( m_dt_old / dt ) * pow( err * err / m_err_old , 0.25 ) / safe;
149 fac_pred = max BOOST_PREVENT_MACRO_SUBSTITUTION (
150 fac2 , min BOOST_PREVENT_MACRO_SUBSTITUTION ( fac1 , fac_pred ) );
151 fac = max BOOST_PREVENT_MACRO_SUBSTITUTION ( fac , fac_pred );
156 m_err_old = max BOOST_PREVENT_MACRO_SUBSTITUTION ( static_cast< value_type >( 0.01 ) , err );
157 if( m_last_rejected )
158 dt_new = ( dt >= 0.0 ?
159 min BOOST_PREVENT_MACRO_SUBSTITUTION ( dt_new , dt ) :
160 max BOOST_PREVENT_MACRO_SUBSTITUTION ( dt_new , dt ) );
162 // limit step size to max_dt
163 if( m_max_dt != static_cast<time_type>(0) )
165 dt = detail::min_abs(m_max_dt, dt_new);
167 m_last_rejected = false;
173 m_last_rejected = true;
179 template< class StateType >
180 void adjust_size( const StateType &x )
188 stepper_type& stepper( void )
193 const stepper_type& stepper( void ) const
203 template< class StateIn >
204 bool resize_m_xerr( const StateIn &x )
206 return adjust_size_by_resizeability( m_xerr , x , typename is_resizeable<state_type>::type() );
209 template< class StateIn >
210 bool resize_m_xnew( const StateIn &x )
212 return adjust_size_by_resizeability( m_xnew , x , typename is_resizeable<state_type>::type() );
216 stepper_type m_stepper;
217 resizer_type m_xerr_resizer;
218 resizer_type m_xnew_resizer;
219 wrapped_state_type m_xerr;
220 wrapped_state_type m_xnew;
221 value_type m_atol , m_rtol;
224 value_type m_err_old , m_dt_old;
225 bool m_last_rejected;
233 } // namespace odeint
234 } // namespace numeric
238 #endif // BOOST_NUMERIC_ODEINT_STEPPER_ROSENBROCK4_CONTROLLER_HPP_INCLUDED