annotate DEPENDENCIES/generic/include/boost/numeric/odeint/stepper/controlled_runge_kutta.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 /* [auto_generated]
Chris@16 2 boost/numeric/odeint/stepper/controlled_runge_kutta.hpp
Chris@16 3
Chris@16 4 [begin_description]
Chris@16 5 The default controlled stepper which can be used with all explicit Runge-Kutta error steppers.
Chris@16 6 [end_description]
Chris@16 7
Chris@16 8 Copyright 2009-2011 Karsten Ahnert
Chris@16 9 Copyright 2009-2011 Mario Mulansky
Chris@16 10
Chris@16 11 Distributed under the Boost Software License, Version 1.0.
Chris@16 12 (See accompanying file LICENSE_1_0.txt or
Chris@16 13 copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 14 */
Chris@16 15
Chris@16 16
Chris@16 17 #ifndef BOOST_NUMERIC_ODEINT_STEPPER_CONTROLLED_RUNGE_KUTTA_HPP_INCLUDED
Chris@16 18 #define BOOST_NUMERIC_ODEINT_STEPPER_CONTROLLED_RUNGE_KUTTA_HPP_INCLUDED
Chris@16 19
Chris@16 20
Chris@16 21
Chris@16 22 #include <cmath>
Chris@16 23
Chris@16 24 #include <boost/config.hpp>
Chris@16 25 #include <boost/utility/enable_if.hpp>
Chris@16 26 #include <boost/type_traits/is_same.hpp>
Chris@16 27
Chris@16 28 #include <boost/numeric/odeint/util/bind.hpp>
Chris@16 29 #include <boost/numeric/odeint/util/unwrap_reference.hpp>
Chris@16 30 #include <boost/numeric/odeint/util/copy.hpp>
Chris@16 31
Chris@16 32 #include <boost/numeric/odeint/util/state_wrapper.hpp>
Chris@16 33 #include <boost/numeric/odeint/util/is_resizeable.hpp>
Chris@16 34 #include <boost/numeric/odeint/util/resizer.hpp>
Chris@16 35
Chris@16 36 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
Chris@16 37 #include <boost/numeric/odeint/algebra/default_operations.hpp>
Chris@16 38
Chris@16 39 #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
Chris@16 40 #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
Chris@16 41
Chris@16 42 namespace boost {
Chris@16 43 namespace numeric {
Chris@16 44 namespace odeint {
Chris@16 45
Chris@16 46
Chris@16 47 template
Chris@16 48 <
Chris@16 49 class Value ,
Chris@16 50 class Algebra = range_algebra ,
Chris@16 51 class Operations = default_operations
Chris@16 52 >
Chris@16 53 class default_error_checker
Chris@16 54 {
Chris@16 55 public:
Chris@16 56
Chris@16 57 typedef Value value_type;
Chris@16 58 typedef Algebra algebra_type;
Chris@16 59 typedef Operations operations_type;
Chris@16 60
Chris@16 61 default_error_checker(
Chris@16 62 value_type eps_abs = static_cast< value_type >( 1.0e-6 ) ,
Chris@16 63 value_type eps_rel = static_cast< value_type >( 1.0e-6 ) ,
Chris@16 64 value_type a_x = static_cast< value_type >( 1 ) ,
Chris@16 65 value_type a_dxdt = static_cast< value_type >( 1 ) )
Chris@16 66 : m_eps_abs( eps_abs ) , m_eps_rel( eps_rel ) , m_a_x( a_x ) , m_a_dxdt( a_dxdt )
Chris@16 67 { }
Chris@16 68
Chris@16 69
Chris@16 70 template< class State , class Deriv , class Err , class Time >
Chris@16 71 value_type error( const State &x_old , const Deriv &dxdt_old , Err &x_err , Time dt ) const
Chris@16 72 {
Chris@16 73 return error( algebra_type() , x_old , dxdt_old , x_err , dt );
Chris@16 74 }
Chris@16 75
Chris@16 76 template< class State , class Deriv , class Err , class Time >
Chris@16 77 value_type error( algebra_type &algebra , const State &x_old , const Deriv &dxdt_old , Err &x_err , Time dt ) const
Chris@16 78 {
Chris@16 79 // this overwrites x_err !
Chris@16 80 algebra.for_each3( x_err , x_old , dxdt_old ,
Chris@16 81 typename operations_type::template rel_error< value_type >( m_eps_abs , m_eps_rel , m_a_x , m_a_dxdt * get_unit_value( dt ) ) );
Chris@16 82
Chris@16 83 value_type res = algebra.reduce( x_err ,
Chris@16 84 typename operations_type::template maximum< value_type >() , static_cast< value_type >( 0 ) );
Chris@16 85 return res;
Chris@16 86 }
Chris@16 87
Chris@16 88 private:
Chris@16 89
Chris@16 90 value_type m_eps_abs;
Chris@16 91 value_type m_eps_rel;
Chris@16 92 value_type m_a_x;
Chris@16 93 value_type m_a_dxdt;
Chris@16 94
Chris@16 95 };
Chris@16 96
Chris@16 97
Chris@16 98
Chris@16 99
Chris@16 100
Chris@16 101
Chris@16 102
Chris@16 103
Chris@16 104 /*
Chris@16 105 * error stepper category dispatcher
Chris@16 106 */
Chris@16 107 template<
Chris@16 108 class ErrorStepper ,
Chris@16 109 class ErrorChecker = default_error_checker< typename ErrorStepper::value_type ,
Chris@16 110 typename ErrorStepper::algebra_type ,
Chris@16 111 typename ErrorStepper::operations_type > ,
Chris@16 112 class Resizer = typename ErrorStepper::resizer_type ,
Chris@16 113 class ErrorStepperCategory = typename ErrorStepper::stepper_category
Chris@16 114 >
Chris@16 115 class controlled_runge_kutta ;
Chris@16 116
Chris@16 117
Chris@16 118
Chris@16 119 /*
Chris@16 120 * explicit stepper version
Chris@16 121 *
Chris@16 122 * this class introduces the following try_step overloads
Chris@16 123 * try_step( sys , x , t , dt )
Chris@16 124 * try_step( sys , x , dxdt , t , dt )
Chris@16 125 * try_step( sys , in , t , out , dt )
Chris@16 126 * try_step( sys , in , dxdt , t , out , dt )
Chris@16 127 */
Chris@16 128 /**
Chris@16 129 * \brief Implements step size control for Runge-Kutta steppers with error
Chris@16 130 * estimation.
Chris@16 131 *
Chris@16 132 * This class implements the step size control for standard Runge-Kutta
Chris@16 133 * steppers with error estimation.
Chris@16 134 *
Chris@16 135 * \tparam ErrorStepper The stepper type with error estimation, has to fulfill the ErrorStepper concept.
Chris@16 136 * \tparam ErrorChecker The error checker
Chris@16 137 * \tparam Resizer The resizer policy type.
Chris@16 138 */
Chris@16 139 template<
Chris@16 140 class ErrorStepper ,
Chris@16 141 class ErrorChecker ,
Chris@16 142 class Resizer
Chris@16 143 >
Chris@16 144 class controlled_runge_kutta< ErrorStepper , ErrorChecker , Resizer , explicit_error_stepper_tag >
Chris@16 145 {
Chris@16 146
Chris@16 147 public:
Chris@16 148
Chris@16 149 typedef ErrorStepper stepper_type;
Chris@16 150 typedef typename stepper_type::state_type state_type;
Chris@16 151 typedef typename stepper_type::value_type value_type;
Chris@16 152 typedef typename stepper_type::deriv_type deriv_type;
Chris@16 153 typedef typename stepper_type::time_type time_type;
Chris@16 154 typedef typename stepper_type::algebra_type algebra_type;
Chris@16 155 typedef typename stepper_type::operations_type operations_type;
Chris@16 156 typedef Resizer resizer_type;
Chris@16 157 typedef ErrorChecker error_checker_type;
Chris@16 158 typedef explicit_controlled_stepper_tag stepper_category;
Chris@16 159
Chris@16 160 #ifndef DOXYGEN_SKIP
Chris@16 161 typedef typename stepper_type::wrapped_state_type wrapped_state_type;
Chris@16 162 typedef typename stepper_type::wrapped_deriv_type wrapped_deriv_type;
Chris@16 163
Chris@16 164 typedef controlled_runge_kutta< ErrorStepper , ErrorChecker , Resizer , explicit_error_stepper_tag > controlled_stepper_type;
Chris@16 165 #endif //DOXYGEN_SKIP
Chris@16 166
Chris@16 167
Chris@16 168 /**
Chris@16 169 * \brief Constructs the controlled Runge-Kutta stepper.
Chris@16 170 * \param error_checker An instance of the error checker.
Chris@16 171 * \param stepper An instance of the underlying stepper.
Chris@16 172 */
Chris@16 173 controlled_runge_kutta(
Chris@16 174 const error_checker_type &error_checker = error_checker_type( ) ,
Chris@16 175 const stepper_type &stepper = stepper_type( )
Chris@16 176 )
Chris@16 177 : m_stepper( stepper ) , m_error_checker( error_checker )
Chris@16 178 { }
Chris@16 179
Chris@16 180
Chris@16 181
Chris@16 182 /*
Chris@16 183 * Version 1 : try_step( sys , x , t , dt )
Chris@16 184 *
Chris@16 185 * The overloads are needed to solve the forwarding problem
Chris@16 186 */
Chris@16 187 /**
Chris@16 188 * \brief Tries to perform one step.
Chris@16 189 *
Chris@16 190 * This method tries to do one step with step size dt. If the error estimate
Chris@16 191 * is to large, the step is rejected and the method returns fail and the
Chris@16 192 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 193 * step is performed, success is returned and dt might be increased to make
Chris@16 194 * the steps as large as possible. This method also updates t if a step is
Chris@16 195 * performed.
Chris@16 196 *
Chris@16 197 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 198 * Simple System concept.
Chris@16 199 * \param x The state of the ODE which should be solved. Overwritten if
Chris@16 200 * the step is successful.
Chris@16 201 * \param t The value of the time. Updated if the step is successful.
Chris@16 202 * \param dt The step size. Updated.
Chris@16 203 * \return success if the step was accepted, fail otherwise.
Chris@16 204 */
Chris@16 205 template< class System , class StateInOut >
Chris@16 206 controlled_step_result try_step( System system , StateInOut &x , time_type &t , time_type &dt )
Chris@16 207 {
Chris@16 208 return try_step_v1( system , x , t, dt );
Chris@16 209 }
Chris@16 210
Chris@16 211 /**
Chris@16 212 * \brief Tries to perform one step. Solves the forwarding problem and
Chris@16 213 * allows for using boost range as state_type.
Chris@16 214 *
Chris@16 215 * This method tries to do one step with step size dt. If the error estimate
Chris@16 216 * is to large, the step is rejected and the method returns fail and the
Chris@16 217 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 218 * step is performed, success is returned and dt might be increased to make
Chris@16 219 * the steps as large as possible. This method also updates t if a step is
Chris@16 220 * performed.
Chris@16 221 *
Chris@16 222 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 223 * Simple System concept.
Chris@16 224 * \param x The state of the ODE which should be solved. Overwritten if
Chris@16 225 * the step is successful. Can be a boost range.
Chris@16 226 * \param t The value of the time. Updated if the step is successful.
Chris@16 227 * \param dt The step size. Updated.
Chris@16 228 * \return success if the step was accepted, fail otherwise.
Chris@16 229 */
Chris@16 230 template< class System , class StateInOut >
Chris@16 231 controlled_step_result try_step( System system , const StateInOut &x , time_type &t , time_type &dt )
Chris@16 232 {
Chris@16 233 return try_step_v1( system , x , t, dt );
Chris@16 234 }
Chris@16 235
Chris@16 236
Chris@16 237
Chris@16 238 /*
Chris@16 239 * Version 2 : try_step( sys , x , dxdt , t , dt )
Chris@16 240 *
Chris@16 241 * this version does not solve the forwarding problem, boost.range can not be used
Chris@16 242 */
Chris@16 243 /**
Chris@16 244 * \brief Tries to perform one step.
Chris@16 245 *
Chris@16 246 * This method tries to do one step with step size dt. If the error estimate
Chris@16 247 * is to large, the step is rejected and the method returns fail and the
Chris@16 248 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 249 * step is performed, success is returned and dt might be increased to make
Chris@16 250 * the steps as large as possible. This method also updates t if a step is
Chris@16 251 * performed.
Chris@16 252 *
Chris@16 253 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 254 * Simple System concept.
Chris@16 255 * \param x The state of the ODE which should be solved. Overwritten if
Chris@16 256 * the step is successful.
Chris@16 257 * \param dxdt The derivative of state.
Chris@16 258 * \param t The value of the time. Updated if the step is successful.
Chris@16 259 * \param dt The step size. Updated.
Chris@16 260 * \return success if the step was accepted, fail otherwise.
Chris@16 261 */
Chris@16 262 template< class System , class StateInOut , class DerivIn >
Chris@16 263 controlled_step_result try_step( System system , StateInOut &x , const DerivIn &dxdt , time_type &t , time_type &dt )
Chris@16 264 {
Chris@16 265 m_xnew_resizer.adjust_size( x , detail::bind( &controlled_runge_kutta::template resize_m_xnew_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
Chris@16 266 controlled_step_result res = try_step( system , x , dxdt , t , m_xnew.m_v , dt );
Chris@16 267 if( res == success )
Chris@16 268 {
Chris@16 269 boost::numeric::odeint::copy( m_xnew.m_v , x );
Chris@16 270 }
Chris@16 271 return res;
Chris@16 272 }
Chris@16 273
Chris@16 274 /*
Chris@16 275 * Version 3 : try_step( sys , in , t , out , dt )
Chris@16 276 *
Chris@16 277 * this version does not solve the forwarding problem, boost.range can not be used
Chris@16 278 *
Chris@16 279 * the disable is needed to avoid ambiguous overloads if state_type = time_type
Chris@16 280 */
Chris@16 281 /**
Chris@16 282 * \brief Tries to perform one step.
Chris@16 283 *
Chris@16 284 * \note This method is disabled if state_type=time_type to avoid ambiguity.
Chris@16 285 *
Chris@16 286 * This method tries to do one step with step size dt. If the error estimate
Chris@16 287 * is to large, the step is rejected and the method returns fail and the
Chris@16 288 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 289 * step is performed, success is returned and dt might be increased to make
Chris@16 290 * the steps as large as possible. This method also updates t if a step is
Chris@16 291 * performed.
Chris@16 292 *
Chris@16 293 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 294 * Simple System concept.
Chris@16 295 * \param in The state of the ODE which should be solved.
Chris@16 296 * \param t The value of the time. Updated if the step is successful.
Chris@16 297 * \param out Used to store the result of the step.
Chris@16 298 * \param dt The step size. Updated.
Chris@16 299 * \return success if the step was accepted, fail otherwise.
Chris@16 300 */
Chris@16 301 template< class System , class StateIn , class StateOut >
Chris@16 302 typename boost::disable_if< boost::is_same< StateIn , time_type > , controlled_step_result >::type
Chris@16 303 try_step( System system , const StateIn &in , time_type &t , StateOut &out , time_type &dt )
Chris@16 304 {
Chris@16 305 typename odeint::unwrap_reference< System >::type &sys = system;
Chris@16 306 m_dxdt_resizer.adjust_size( in , detail::bind( &controlled_runge_kutta::template resize_m_dxdt_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
Chris@16 307 sys( in , m_dxdt.m_v , t );
Chris@16 308 return try_step( system , in , m_dxdt.m_v , t , out , dt );
Chris@16 309 }
Chris@16 310
Chris@16 311
Chris@16 312 /*
Chris@16 313 * Version 4 : try_step( sys , in , dxdt , t , out , dt )
Chris@16 314 *
Chris@16 315 * this version does not solve the forwarding problem, boost.range can not be used
Chris@16 316 */
Chris@16 317 /**
Chris@16 318 * \brief Tries to perform one step.
Chris@16 319 *
Chris@16 320 * This method tries to do one step with step size dt. If the error estimate
Chris@16 321 * is to large, the step is rejected and the method returns fail and the
Chris@16 322 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 323 * step is performed, success is returned and dt might be increased to make
Chris@16 324 * the steps as large as possible. This method also updates t if a step is
Chris@16 325 * performed.
Chris@16 326 *
Chris@16 327 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 328 * Simple System concept.
Chris@16 329 * \param in The state of the ODE which should be solved.
Chris@16 330 * \param dxdt The derivative of state.
Chris@16 331 * \param t The value of the time. Updated if the step is successful.
Chris@16 332 * \param out Used to store the result of the step.
Chris@16 333 * \param dt The step size. Updated.
Chris@16 334 * \return success if the step was accepted, fail otherwise.
Chris@16 335 */
Chris@16 336 template< class System , class StateIn , class DerivIn , class StateOut >
Chris@16 337 controlled_step_result try_step( System system , const StateIn &in , const DerivIn &dxdt , time_type &t , StateOut &out , time_type &dt )
Chris@16 338 {
Chris@16 339 BOOST_USING_STD_MIN();
Chris@16 340 BOOST_USING_STD_MAX();
Chris@16 341 using std::pow;
Chris@16 342
Chris@16 343 m_xerr_resizer.adjust_size( in , detail::bind( &controlled_runge_kutta::template resize_m_xerr_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
Chris@16 344
Chris@16 345 // do one step with error calculation
Chris@16 346 m_stepper.do_step( system , in , dxdt , t , out , dt , m_xerr.m_v );
Chris@16 347
Chris@16 348 m_max_rel_error = m_error_checker.error( m_stepper.algebra() , in , dxdt , m_xerr.m_v , dt );
Chris@16 349
Chris@16 350 if( m_max_rel_error > 1.0 )
Chris@16 351 {
Chris@16 352 // error too large - decrease dt ,limit scaling factor to 0.2 and reset state
Chris@16 353 dt *= max BOOST_PREVENT_MACRO_SUBSTITUTION ( static_cast<value_type>(9)/static_cast<value_type>(10) * pow( m_max_rel_error ,
Chris@16 354 static_cast<value_type>(-1) / ( m_stepper.error_order() - 1 ) ) ,
Chris@16 355 static_cast<value_type>(1)/static_cast<value_type> (5) );
Chris@16 356 return fail;
Chris@16 357 }
Chris@16 358 else
Chris@16 359 {
Chris@16 360 if( m_max_rel_error < 0.5 )
Chris@16 361 {
Chris@16 362 // error should be > 0
Chris@16 363 m_max_rel_error = max BOOST_PREVENT_MACRO_SUBSTITUTION ( pow( 5.0 , -m_stepper.stepper_order() ) , m_max_rel_error );
Chris@16 364 //error too small - increase dt and keep the evolution and limit scaling factor to 5.0
Chris@16 365 t += dt;
Chris@16 366 dt *= static_cast<value_type>(9)/static_cast<value_type>(10) * pow( m_max_rel_error ,
Chris@16 367 static_cast<value_type>(-1) / m_stepper.stepper_order() );
Chris@16 368 return success;
Chris@16 369 }
Chris@16 370 else
Chris@16 371 {
Chris@16 372 t += dt;
Chris@16 373 return success;
Chris@16 374 }
Chris@16 375 }
Chris@16 376 }
Chris@16 377
Chris@16 378 /**
Chris@16 379 * \brief Returns the error of the last step.
Chris@16 380 *
Chris@16 381 * returns The last error of the step.
Chris@16 382 */
Chris@16 383 value_type last_error( void ) const
Chris@16 384 {
Chris@16 385 return m_max_rel_error;
Chris@16 386 }
Chris@16 387
Chris@16 388
Chris@16 389 /**
Chris@16 390 * \brief Adjust the size of all temporaries in the stepper manually.
Chris@16 391 * \param x A state from which the size of the temporaries to be resized is deduced.
Chris@16 392 */
Chris@16 393 template< class StateType >
Chris@16 394 void adjust_size( const StateType &x )
Chris@16 395 {
Chris@16 396 resize_m_xerr_impl( x );
Chris@16 397 resize_m_dxdt_impl( x );
Chris@16 398 resize_m_xnew_impl( x );
Chris@16 399 m_stepper.adjust_size( x );
Chris@16 400 }
Chris@16 401
Chris@16 402 /**
Chris@16 403 * \brief Returns the instance of the underlying stepper.
Chris@16 404 * \returns The instance of the underlying stepper.
Chris@16 405 */
Chris@16 406 stepper_type& stepper( void )
Chris@16 407 {
Chris@16 408 return m_stepper;
Chris@16 409 }
Chris@16 410
Chris@16 411 /**
Chris@16 412 * \brief Returns the instance of the underlying stepper.
Chris@16 413 * \returns The instance of the underlying stepper.
Chris@16 414 */
Chris@16 415 const stepper_type& stepper( void ) const
Chris@16 416 {
Chris@16 417 return m_stepper;
Chris@16 418 }
Chris@16 419
Chris@16 420 private:
Chris@16 421
Chris@16 422
Chris@16 423 template< class System , class StateInOut >
Chris@16 424 controlled_step_result try_step_v1( System system , StateInOut &x , time_type &t , time_type &dt )
Chris@16 425 {
Chris@16 426 typename odeint::unwrap_reference< System >::type &sys = system;
Chris@16 427 m_dxdt_resizer.adjust_size( x , detail::bind( &controlled_runge_kutta::template resize_m_dxdt_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
Chris@16 428 sys( x , m_dxdt.m_v ,t );
Chris@16 429 return try_step( system , x , m_dxdt.m_v , t , dt );
Chris@16 430 }
Chris@16 431
Chris@16 432 template< class StateIn >
Chris@16 433 bool resize_m_xerr_impl( const StateIn &x )
Chris@16 434 {
Chris@16 435 return adjust_size_by_resizeability( m_xerr , x , typename is_resizeable<state_type>::type() );
Chris@16 436 }
Chris@16 437
Chris@16 438 template< class StateIn >
Chris@16 439 bool resize_m_dxdt_impl( const StateIn &x )
Chris@16 440 {
Chris@16 441 return adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable<deriv_type>::type() );
Chris@16 442 }
Chris@16 443
Chris@16 444 template< class StateIn >
Chris@16 445 bool resize_m_xnew_impl( const StateIn &x )
Chris@16 446 {
Chris@16 447 return adjust_size_by_resizeability( m_xnew , x , typename is_resizeable<state_type>::type() );
Chris@16 448 }
Chris@16 449
Chris@16 450
Chris@16 451
Chris@16 452 stepper_type m_stepper;
Chris@16 453 error_checker_type m_error_checker;
Chris@16 454
Chris@16 455 resizer_type m_dxdt_resizer;
Chris@16 456 resizer_type m_xerr_resizer;
Chris@16 457 resizer_type m_xnew_resizer;
Chris@16 458
Chris@16 459 wrapped_deriv_type m_dxdt;
Chris@16 460 wrapped_state_type m_xerr;
Chris@16 461 wrapped_state_type m_xnew;
Chris@16 462 value_type m_max_rel_error;
Chris@16 463 };
Chris@16 464
Chris@16 465
Chris@16 466
Chris@16 467
Chris@16 468
Chris@16 469
Chris@16 470
Chris@16 471
Chris@16 472
Chris@16 473
Chris@16 474 /*
Chris@16 475 * explicit stepper fsal version
Chris@16 476 *
Chris@16 477 * the class introduces the following try_step overloads
Chris@16 478 * try_step( sys , x , t , dt )
Chris@16 479 * try_step( sys , in , t , out , dt )
Chris@16 480 * try_step( sys , x , dxdt , t , dt )
Chris@16 481 * try_step( sys , in , dxdt_in , t , out , dxdt_out , dt )
Chris@16 482 */
Chris@16 483 /**
Chris@16 484 * \brief Implements step size control for Runge-Kutta FSAL steppers with
Chris@16 485 * error estimation.
Chris@16 486 *
Chris@16 487 * This class implements the step size control for FSAL Runge-Kutta
Chris@16 488 * steppers with error estimation.
Chris@16 489 *
Chris@16 490 * \tparam ErrorStepper The stepper type with error estimation, has to fulfill the ErrorStepper concept.
Chris@16 491 * \tparam ErrorChecker The error checker
Chris@16 492 * \tparam Resizer The resizer policy type.
Chris@16 493 */
Chris@16 494 template<
Chris@16 495 class ErrorStepper ,
Chris@16 496 class ErrorChecker ,
Chris@16 497 class Resizer
Chris@16 498 >
Chris@16 499 class controlled_runge_kutta< ErrorStepper , ErrorChecker , Resizer , explicit_error_stepper_fsal_tag >
Chris@16 500 {
Chris@16 501
Chris@16 502 public:
Chris@16 503
Chris@16 504 typedef ErrorStepper stepper_type;
Chris@16 505 typedef typename stepper_type::state_type state_type;
Chris@16 506 typedef typename stepper_type::value_type value_type;
Chris@16 507 typedef typename stepper_type::deriv_type deriv_type;
Chris@16 508 typedef typename stepper_type::time_type time_type;
Chris@16 509 typedef typename stepper_type::algebra_type algebra_type;
Chris@16 510 typedef typename stepper_type::operations_type operations_type;
Chris@16 511 typedef Resizer resizer_type;
Chris@16 512 typedef ErrorChecker error_checker_type;
Chris@16 513 typedef explicit_controlled_stepper_fsal_tag stepper_category;
Chris@16 514
Chris@16 515 #ifndef DOXYGEN_SKIP
Chris@16 516 typedef typename stepper_type::wrapped_state_type wrapped_state_type;
Chris@16 517 typedef typename stepper_type::wrapped_deriv_type wrapped_deriv_type;
Chris@16 518
Chris@16 519 typedef controlled_runge_kutta< ErrorStepper , ErrorChecker , Resizer , explicit_error_stepper_tag > controlled_stepper_type;
Chris@16 520 #endif // DOXYGEN_SKIP
Chris@16 521
Chris@16 522 /**
Chris@16 523 * \brief Constructs the controlled Runge-Kutta stepper.
Chris@16 524 * \param error_checker An instance of the error checker.
Chris@16 525 * \param stepper An instance of the underlying stepper.
Chris@16 526 */
Chris@16 527 controlled_runge_kutta(
Chris@16 528 const error_checker_type &error_checker = error_checker_type() ,
Chris@16 529 const stepper_type &stepper = stepper_type()
Chris@16 530 )
Chris@16 531 : m_stepper( stepper ) , m_error_checker( error_checker ) ,
Chris@16 532 m_first_call( true )
Chris@16 533 { }
Chris@16 534
Chris@16 535 /*
Chris@16 536 * Version 1 : try_step( sys , x , t , dt )
Chris@16 537 *
Chris@16 538 * The two overloads are needed in order to solve the forwarding problem
Chris@16 539 */
Chris@16 540 /**
Chris@16 541 * \brief Tries to perform one step.
Chris@16 542 *
Chris@16 543 * This method tries to do one step with step size dt. If the error estimate
Chris@16 544 * is to large, the step is rejected and the method returns fail and the
Chris@16 545 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 546 * step is performed, success is returned and dt might be increased to make
Chris@16 547 * the steps as large as possible. This method also updates t if a step is
Chris@16 548 * performed.
Chris@16 549 *
Chris@16 550 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 551 * Simple System concept.
Chris@16 552 * \param x The state of the ODE which should be solved. Overwritten if
Chris@16 553 * the step is successful.
Chris@16 554 * \param t The value of the time. Updated if the step is successful.
Chris@16 555 * \param dt The step size. Updated.
Chris@16 556 * \return success if the step was accepted, fail otherwise.
Chris@16 557 */
Chris@16 558 template< class System , class StateInOut >
Chris@16 559 controlled_step_result try_step( System system , StateInOut &x , time_type &t , time_type &dt )
Chris@16 560 {
Chris@16 561 return try_step_v1( system , x , t , dt );
Chris@16 562 }
Chris@16 563
Chris@16 564
Chris@16 565 /**
Chris@16 566 * \brief Tries to perform one step. Solves the forwarding problem and
Chris@16 567 * allows for using boost range as state_type.
Chris@16 568 *
Chris@16 569 * This method tries to do one step with step size dt. If the error estimate
Chris@16 570 * is to large, the step is rejected and the method returns fail and the
Chris@16 571 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 572 * step is performed, success is returned and dt might be increased to make
Chris@16 573 * the steps as large as possible. This method also updates t if a step is
Chris@16 574 * performed.
Chris@16 575 *
Chris@16 576 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 577 * Simple System concept.
Chris@16 578 * \param x The state of the ODE which should be solved. Overwritten if
Chris@16 579 * the step is successful. Can be a boost range.
Chris@16 580 * \param t The value of the time. Updated if the step is successful.
Chris@16 581 * \param dt The step size. Updated.
Chris@16 582 * \return success if the step was accepted, fail otherwise.
Chris@16 583 */
Chris@16 584 template< class System , class StateInOut >
Chris@16 585 controlled_step_result try_step( System system , const StateInOut &x , time_type &t , time_type &dt )
Chris@16 586 {
Chris@16 587 return try_step_v1( system , x , t , dt );
Chris@16 588 }
Chris@16 589
Chris@16 590
Chris@16 591
Chris@16 592 /*
Chris@16 593 * Version 2 : try_step( sys , in , t , out , dt );
Chris@16 594 *
Chris@16 595 * This version does not solve the forwarding problem, boost::range can not be used.
Chris@16 596 *
Chris@16 597 * The disabler is needed to solve ambiguous overloads
Chris@16 598 */
Chris@16 599 /**
Chris@16 600 * \brief Tries to perform one step.
Chris@16 601 *
Chris@16 602 * \note This method is disabled if state_type=time_type to avoid ambiguity.
Chris@16 603 *
Chris@16 604 * This method tries to do one step with step size dt. If the error estimate
Chris@16 605 * is to large, the step is rejected and the method returns fail and the
Chris@16 606 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 607 * step is performed, success is returned and dt might be increased to make
Chris@16 608 * the steps as large as possible. This method also updates t if a step is
Chris@16 609 * performed.
Chris@16 610 *
Chris@16 611 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 612 * Simple System concept.
Chris@16 613 * \param in The state of the ODE which should be solved.
Chris@16 614 * \param t The value of the time. Updated if the step is successful.
Chris@16 615 * \param out Used to store the result of the step.
Chris@16 616 * \param dt The step size. Updated.
Chris@16 617 * \return success if the step was accepted, fail otherwise.
Chris@16 618 */
Chris@16 619 template< class System , class StateIn , class StateOut >
Chris@16 620 typename boost::disable_if< boost::is_same< StateIn , time_type > , controlled_step_result >::type
Chris@16 621 try_step( System system , const StateIn &in , time_type &t , StateOut &out , time_type &dt )
Chris@16 622 {
Chris@16 623 if( m_dxdt_resizer.adjust_size( in , detail::bind( &controlled_runge_kutta::template resize_m_dxdt_impl< StateIn > , detail::ref( *this ) , detail::_1 ) ) || m_first_call )
Chris@16 624 {
Chris@16 625 initialize( system , in , t );
Chris@16 626 }
Chris@16 627 return try_step( system , in , m_dxdt.m_v , t , out , dt );
Chris@16 628 }
Chris@16 629
Chris@16 630
Chris@16 631 /*
Chris@16 632 * Version 3 : try_step( sys , x , dxdt , t , dt )
Chris@16 633 *
Chris@16 634 * This version does not solve the forwarding problem, boost::range can not be used.
Chris@16 635 */
Chris@16 636 /**
Chris@16 637 * \brief Tries to perform one step.
Chris@16 638 *
Chris@16 639 * This method tries to do one step with step size dt. If the error estimate
Chris@16 640 * is to large, the step is rejected and the method returns fail and the
Chris@16 641 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 642 * step is performed, success is returned and dt might be increased to make
Chris@16 643 * the steps as large as possible. This method also updates t if a step is
Chris@16 644 * performed.
Chris@16 645 *
Chris@16 646 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 647 * Simple System concept.
Chris@16 648 * \param x The state of the ODE which should be solved. Overwritten if
Chris@16 649 * the step is successful.
Chris@16 650 * \param dxdt The derivative of state.
Chris@16 651 * \param t The value of the time. Updated if the step is successful.
Chris@16 652 * \param dt The step size. Updated.
Chris@16 653 * \return success if the step was accepted, fail otherwise.
Chris@16 654 */
Chris@16 655 template< class System , class StateInOut , class DerivInOut >
Chris@16 656 controlled_step_result try_step( System system , StateInOut &x , DerivInOut &dxdt , time_type &t , time_type &dt )
Chris@16 657 {
Chris@16 658 m_xnew_resizer.adjust_size( x , detail::bind( &controlled_runge_kutta::template resize_m_xnew_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
Chris@16 659 m_dxdt_new_resizer.adjust_size( x , detail::bind( &controlled_runge_kutta::template resize_m_dxdt_new_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
Chris@16 660 controlled_step_result res = try_step( system , x , dxdt , t , m_xnew.m_v , m_dxdtnew.m_v , dt );
Chris@16 661 if( res == success )
Chris@16 662 {
Chris@16 663 boost::numeric::odeint::copy( m_xnew.m_v , x );
Chris@16 664 boost::numeric::odeint::copy( m_dxdtnew.m_v , dxdt );
Chris@16 665 }
Chris@16 666 return res;
Chris@16 667 }
Chris@16 668
Chris@16 669
Chris@16 670 /*
Chris@16 671 * Version 4 : try_step( sys , in , dxdt_in , t , out , dxdt_out , dt )
Chris@16 672 *
Chris@16 673 * This version does not solve the forwarding problem, boost::range can not be used.
Chris@16 674 */
Chris@16 675 /**
Chris@16 676 * \brief Tries to perform one step.
Chris@16 677 *
Chris@16 678 * This method tries to do one step with step size dt. If the error estimate
Chris@16 679 * is to large, the step is rejected and the method returns fail and the
Chris@16 680 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 681 * step is performed, success is returned and dt might be increased to make
Chris@16 682 * the steps as large as possible. This method also updates t if a step is
Chris@16 683 * performed.
Chris@16 684 *
Chris@16 685 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 686 * Simple System concept.
Chris@16 687 * \param in The state of the ODE which should be solved.
Chris@16 688 * \param dxdt The derivative of state.
Chris@16 689 * \param t The value of the time. Updated if the step is successful.
Chris@16 690 * \param out Used to store the result of the step.
Chris@16 691 * \param dt The step size. Updated.
Chris@16 692 * \return success if the step was accepted, fail otherwise.
Chris@16 693 */
Chris@16 694 template< class System , class StateIn , class DerivIn , class StateOut , class DerivOut >
Chris@16 695 controlled_step_result try_step( System system , const StateIn &in , const DerivIn &dxdt_in , time_type &t ,
Chris@16 696 StateOut &out , DerivOut &dxdt_out , time_type &dt )
Chris@16 697 {
Chris@16 698 BOOST_USING_STD_MIN();
Chris@16 699 BOOST_USING_STD_MAX();
Chris@16 700
Chris@16 701 using std::pow;
Chris@16 702
Chris@16 703 m_xerr_resizer.adjust_size( in , detail::bind( &controlled_runge_kutta::template resize_m_xerr_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
Chris@16 704
Chris@16 705 //fsal: m_stepper.get_dxdt( dxdt );
Chris@16 706 //fsal: m_stepper.do_step( sys , x , dxdt , t , dt , m_x_err );
Chris@16 707 m_stepper.do_step( system , in , dxdt_in , t , out , dxdt_out , dt , m_xerr.m_v );
Chris@16 708
Chris@16 709 // this potentially overwrites m_x_err! (standard_error_checker does, at least)
Chris@16 710 value_type max_rel_err = m_error_checker.error( m_stepper.algebra() , in , dxdt_in , m_xerr.m_v , dt );
Chris@16 711
Chris@16 712 if( max_rel_err > 1.0 )
Chris@16 713 {
Chris@16 714 // error too large - decrease dt ,limit scaling factor to 0.2 and reset state
Chris@16 715 dt *= max BOOST_PREVENT_MACRO_SUBSTITUTION ( static_cast<value_type>( static_cast<value_type>(9)/static_cast<value_type>(10) * pow( max_rel_err , static_cast<value_type>(-1) / ( m_stepper.error_order() - 1 ) ) ) , static_cast<value_type>( static_cast<value_type>(1)/static_cast<value_type> (5)) );
Chris@16 716 return fail;
Chris@16 717 }
Chris@16 718 else
Chris@16 719 {
Chris@16 720 if( max_rel_err < 0.5 )
Chris@16 721 { //error too small - increase dt and keep the evolution and limit scaling factor to 5.0
Chris@16 722 // error should be > 0
Chris@16 723 max_rel_err = max BOOST_PREVENT_MACRO_SUBSTITUTION ( pow( 5.0 , -m_stepper.stepper_order() ) , max_rel_err );
Chris@16 724 t += dt;
Chris@16 725 dt *= static_cast<value_type>( static_cast<value_type>(9)/static_cast<value_type>(10) * pow( max_rel_err , static_cast<value_type>(-1) / m_stepper.stepper_order() ) );
Chris@16 726 return success;
Chris@16 727 }
Chris@16 728 else
Chris@16 729 {
Chris@16 730 t += dt;
Chris@16 731 return success;
Chris@16 732 }
Chris@16 733 }
Chris@16 734 }
Chris@16 735
Chris@16 736
Chris@16 737 /**
Chris@16 738 * \brief Resets the internal state of the underlying FSAL stepper.
Chris@16 739 */
Chris@16 740 void reset( void )
Chris@16 741 {
Chris@16 742 m_first_call = true;
Chris@16 743 }
Chris@16 744
Chris@16 745 /**
Chris@16 746 * \brief Initializes the internal state storing an internal copy of the derivative.
Chris@16 747 *
Chris@16 748 * \param deriv The initial derivative of the ODE.
Chris@16 749 */
Chris@16 750 template< class DerivIn >
Chris@16 751 void initialize( const DerivIn &deriv )
Chris@16 752 {
Chris@16 753 boost::numeric::odeint::copy( deriv , m_dxdt.m_v );
Chris@16 754 m_first_call = false;
Chris@16 755 }
Chris@16 756
Chris@16 757 /**
Chris@16 758 * \brief Initializes the internal state storing an internal copy of the derivative.
Chris@16 759 *
Chris@16 760 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 761 * Simple System concept.
Chris@16 762 * \param x The initial state of the ODE which should be solved.
Chris@16 763 * \param t The initial time.
Chris@16 764 */
Chris@16 765 template< class System , class StateIn >
Chris@16 766 void initialize( System system , const StateIn &x , time_type t )
Chris@16 767 {
Chris@16 768 typename odeint::unwrap_reference< System >::type &sys = system;
Chris@16 769 sys( x , m_dxdt.m_v , t );
Chris@16 770 m_first_call = false;
Chris@16 771 }
Chris@16 772
Chris@16 773 /**
Chris@16 774 * \brief Returns true if the stepper has been initialized, false otherwise.
Chris@16 775 *
Chris@16 776 * \return true, if the stepper has been initialized, false otherwise.
Chris@16 777 */
Chris@16 778 bool is_initialized( void ) const
Chris@16 779 {
Chris@16 780 return ! m_first_call;
Chris@16 781 }
Chris@16 782
Chris@16 783
Chris@16 784 /**
Chris@16 785 * \brief Adjust the size of all temporaries in the stepper manually.
Chris@16 786 * \param x A state from which the size of the temporaries to be resized is deduced.
Chris@16 787 */
Chris@16 788 template< class StateType >
Chris@16 789 void adjust_size( const StateType &x )
Chris@16 790 {
Chris@16 791 resize_m_xerr_impl( x );
Chris@16 792 resize_m_dxdt_impl( x );
Chris@16 793 resize_m_dxdt_new_impl( x );
Chris@16 794 resize_m_xnew_impl( x );
Chris@16 795 }
Chris@16 796
Chris@16 797
Chris@16 798 /**
Chris@16 799 * \brief Returns the instance of the underlying stepper.
Chris@16 800 * \returns The instance of the underlying stepper.
Chris@16 801 */
Chris@16 802 stepper_type& stepper( void )
Chris@16 803 {
Chris@16 804 return m_stepper;
Chris@16 805 }
Chris@16 806
Chris@16 807 /**
Chris@16 808 * \brief Returns the instance of the underlying stepper.
Chris@16 809 * \returns The instance of the underlying stepper.
Chris@16 810 */
Chris@16 811 const stepper_type& stepper( void ) const
Chris@16 812 {
Chris@16 813 return m_stepper;
Chris@16 814 }
Chris@16 815
Chris@16 816
Chris@16 817
Chris@16 818 private:
Chris@16 819
Chris@16 820
Chris@16 821 template< class StateIn >
Chris@16 822 bool resize_m_xerr_impl( const StateIn &x )
Chris@16 823 {
Chris@16 824 return adjust_size_by_resizeability( m_xerr , x , typename is_resizeable<state_type>::type() );
Chris@16 825 }
Chris@16 826
Chris@16 827 template< class StateIn >
Chris@16 828 bool resize_m_dxdt_impl( const StateIn &x )
Chris@16 829 {
Chris@16 830 return adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable<deriv_type>::type() );
Chris@16 831 }
Chris@16 832
Chris@16 833 template< class StateIn >
Chris@16 834 bool resize_m_dxdt_new_impl( const StateIn &x )
Chris@16 835 {
Chris@16 836 return adjust_size_by_resizeability( m_dxdtnew , x , typename is_resizeable<deriv_type>::type() );
Chris@16 837 }
Chris@16 838
Chris@16 839 template< class StateIn >
Chris@16 840 bool resize_m_xnew_impl( const StateIn &x )
Chris@16 841 {
Chris@16 842 return adjust_size_by_resizeability( m_xnew , x , typename is_resizeable<state_type>::type() );
Chris@16 843 }
Chris@16 844
Chris@16 845
Chris@16 846 template< class System , class StateInOut >
Chris@16 847 controlled_step_result try_step_v1( System system , StateInOut &x , time_type &t , time_type &dt )
Chris@16 848 {
Chris@16 849 if( m_dxdt_resizer.adjust_size( x , detail::bind( &controlled_runge_kutta::template resize_m_dxdt_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) ) || m_first_call )
Chris@16 850 {
Chris@16 851 initialize( system , x , t );
Chris@16 852 }
Chris@16 853 return try_step( system , x , m_dxdt.m_v , t , dt );
Chris@16 854 }
Chris@16 855
Chris@16 856
Chris@16 857 stepper_type m_stepper;
Chris@16 858 error_checker_type m_error_checker;
Chris@16 859
Chris@16 860 resizer_type m_dxdt_resizer;
Chris@16 861 resizer_type m_xerr_resizer;
Chris@16 862 resizer_type m_xnew_resizer;
Chris@16 863 resizer_type m_dxdt_new_resizer;
Chris@16 864
Chris@16 865 wrapped_deriv_type m_dxdt;
Chris@16 866 wrapped_state_type m_xerr;
Chris@16 867 wrapped_state_type m_xnew;
Chris@16 868 wrapped_deriv_type m_dxdtnew;
Chris@16 869 bool m_first_call;
Chris@16 870 };
Chris@16 871
Chris@16 872
Chris@16 873 /********** DOXYGEN **********/
Chris@16 874
Chris@16 875 /**** DEFAULT ERROR CHECKER ****/
Chris@16 876
Chris@16 877 /**
Chris@16 878 * \class default_error_checker
Chris@16 879 * \brief The default error checker to be used with Runge-Kutta error steppers
Chris@16 880 *
Chris@16 881 * This class provides the default mechanism to compare the error estimates
Chris@16 882 * reported by Runge-Kutta error steppers with user defined error bounds.
Chris@16 883 * It is used by the controlled_runge_kutta steppers.
Chris@16 884 *
Chris@16 885 * \tparam Value The value type.
Chris@16 886 * \tparam Algebra The algebra type.
Chris@16 887 * \tparam Operations The operations type.
Chris@16 888 */
Chris@16 889
Chris@16 890 /**
Chris@16 891 * \fn default_error_checker( value_type eps_abs , value_type eps_rel , value_type a_x , value_type a_dxdt )
Chris@16 892 * \brief Constructs the error checker.
Chris@16 893 *
Chris@16 894 * The error is calculated as follows: ????
Chris@16 895 *
Chris@16 896 * \param eps_abs Absolute tolerance level.
Chris@16 897 * \param eps_rel Relative tolerance level.
Chris@16 898 * \param a_x Factor for the weight of the state.
Chris@16 899 * \param a_dxdt Factor for the weight of the derivative.
Chris@16 900 */
Chris@16 901
Chris@16 902 /**
Chris@16 903 * \fn error( const State &x_old , const Deriv &dxdt_old , Err &x_err , Time dt ) const
Chris@16 904 * \brief Calculates the error level.
Chris@16 905 *
Chris@16 906 * If the returned error level is greater than 1, the estimated error was
Chris@16 907 * larger than the permitted error bounds and the step should be repeated
Chris@16 908 * with a smaller step size.
Chris@16 909 *
Chris@16 910 * \param x_old State at the beginning of the step.
Chris@16 911 * \param dxdt_old Derivative at the beginning of the step.
Chris@16 912 * \param x_err Error estimate.
Chris@16 913 * \param dt Time step.
Chris@16 914 * \return error
Chris@16 915 */
Chris@16 916
Chris@16 917 /**
Chris@16 918 * \fn error( algebra_type &algebra , const State &x_old , const Deriv &dxdt_old , Err &x_err , Time dt ) const
Chris@16 919 * \brief Calculates the error level using a given algebra.
Chris@16 920 *
Chris@16 921 * If the returned error level is greater than 1, the estimated error was
Chris@16 922 * larger than the permitted error bounds and the step should be repeated
Chris@16 923 * with a smaller step size.
Chris@16 924 *
Chris@16 925 * \param algebra The algebra used for calculation of the error.
Chris@16 926 * \param x_old State at the beginning of the step.
Chris@16 927 * \param dxdt_old Derivative at the beginning of the step.
Chris@16 928 * \param x_err Error estimate.
Chris@16 929 * \param dt Time step.
Chris@16 930 * \return error
Chris@16 931 */
Chris@16 932
Chris@16 933
Chris@16 934 } // odeint
Chris@16 935 } // numeric
Chris@16 936 } // boost
Chris@16 937
Chris@16 938
Chris@16 939 #endif // BOOST_NUMERIC_ODEINT_STEPPER_CONTROLLED_RUNGE_KUTTA_HPP_INCLUDED