annotate DEPENDENCIES/generic/include/boost/numeric/odeint/stepper/bulirsch_stoer.hpp @ 46:d572322e2efe

Fix to .cat file check (was susceptible to DOS line-endings) and subrepo update
author Chris Cannam
date Thu, 07 Aug 2014 14:39:38 +0100
parents 2665513ce2d3
children c530137014c0
rev   line source
Chris@16 1 /*
Chris@16 2 [auto_generated]
Chris@16 3 boost/numeric/odeint/stepper/bulirsch_stoer.hpp
Chris@16 4
Chris@16 5 [begin_description]
Chris@16 6 Implementation of the Burlish-Stoer method. As described in
Chris@16 7 Ernst Hairer, Syvert Paul Norsett, Gerhard Wanner
Chris@16 8 Solving Ordinary Differential Equations I. Nonstiff Problems.
Chris@16 9 Springer Series in Comput. Mathematics, Vol. 8, Springer-Verlag 1987, Second revised edition 1993.
Chris@16 10 [end_description]
Chris@16 11
Chris@16 12 Copyright 2009-2011 Karsten Ahnert
Chris@16 13 Copyright 2009-2011 Mario Mulansky
Chris@16 14
Chris@16 15 Distributed under the Boost Software License, Version 1.0.
Chris@16 16 (See accompanying file LICENSE_1_0.txt or
Chris@16 17 copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 18 */
Chris@16 19
Chris@16 20
Chris@16 21 #ifndef BOOST_NUMERIC_ODEINT_STEPPER_BULIRSCH_STOER_HPP_INCLUDED
Chris@16 22 #define BOOST_NUMERIC_ODEINT_STEPPER_BULIRSCH_STOER_HPP_INCLUDED
Chris@16 23
Chris@16 24
Chris@16 25 #include <iostream>
Chris@16 26
Chris@16 27 #include <algorithm>
Chris@16 28
Chris@16 29 #include <boost/config.hpp> // for min/max guidelines
Chris@16 30
Chris@16 31 #include <boost/numeric/odeint/util/bind.hpp>
Chris@16 32 #include <boost/numeric/odeint/util/unwrap_reference.hpp>
Chris@16 33
Chris@16 34 #include <boost/numeric/odeint/stepper/controlled_runge_kutta.hpp>
Chris@16 35 #include <boost/numeric/odeint/stepper/modified_midpoint.hpp>
Chris@16 36 #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
Chris@16 37 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
Chris@16 38 #include <boost/numeric/odeint/algebra/default_operations.hpp>
Chris@16 39
Chris@16 40 #include <boost/numeric/odeint/util/state_wrapper.hpp>
Chris@16 41 #include <boost/numeric/odeint/util/is_resizeable.hpp>
Chris@16 42 #include <boost/numeric/odeint/util/resizer.hpp>
Chris@16 43 #include <boost/numeric/odeint/util/unit_helper.hpp>
Chris@16 44 #include <boost/numeric/odeint/util/detail/less_with_sign.hpp>
Chris@16 45
Chris@16 46 namespace boost {
Chris@16 47 namespace numeric {
Chris@16 48 namespace odeint {
Chris@16 49
Chris@16 50 template<
Chris@16 51 class State ,
Chris@16 52 class Value = double ,
Chris@16 53 class Deriv = State ,
Chris@16 54 class Time = Value ,
Chris@16 55 class Algebra = range_algebra ,
Chris@16 56 class Operations = default_operations ,
Chris@16 57 class Resizer = initially_resizer
Chris@16 58 >
Chris@16 59 class bulirsch_stoer {
Chris@16 60
Chris@16 61 public:
Chris@16 62
Chris@16 63 typedef State state_type;
Chris@16 64 typedef Value value_type;
Chris@16 65 typedef Deriv deriv_type;
Chris@16 66 typedef Time time_type;
Chris@16 67 typedef Algebra algebra_type;
Chris@16 68 typedef Operations operations_type;
Chris@16 69 typedef Resizer resizer_type;
Chris@16 70 #ifndef DOXYGEN_SKIP
Chris@16 71 typedef state_wrapper< state_type > wrapped_state_type;
Chris@16 72 typedef state_wrapper< deriv_type > wrapped_deriv_type;
Chris@16 73 typedef controlled_stepper_tag stepper_category;
Chris@16 74
Chris@16 75 typedef bulirsch_stoer< State , Value , Deriv , Time , Algebra , Operations , Resizer > controlled_error_bs_type;
Chris@16 76
Chris@16 77 typedef typename inverse_time< time_type >::type inv_time_type;
Chris@16 78
Chris@16 79 typedef std::vector< value_type > value_vector;
Chris@16 80 typedef std::vector< time_type > time_vector;
Chris@16 81 typedef std::vector< inv_time_type > inv_time_vector; //should be 1/time_type for boost.units
Chris@16 82 typedef std::vector< value_vector > value_matrix;
Chris@16 83 typedef std::vector< size_t > int_vector;
Chris@16 84 typedef std::vector< wrapped_state_type > state_table_type;
Chris@16 85 #endif //DOXYGEN_SKIP
Chris@16 86 const static size_t m_k_max = 8;
Chris@16 87
Chris@16 88 bulirsch_stoer(
Chris@16 89 value_type eps_abs = 1E-6 , value_type eps_rel = 1E-6 ,
Chris@16 90 value_type factor_x = 1.0 , value_type factor_dxdt = 1.0 )
Chris@16 91 : m_error_checker( eps_abs , eps_rel , factor_x, factor_dxdt ) , m_midpoint() ,
Chris@16 92 m_last_step_rejected( false ) , m_first( true ) ,
Chris@16 93 m_interval_sequence( m_k_max+1 ) ,
Chris@16 94 m_coeff( m_k_max+1 ) ,
Chris@16 95 m_cost( m_k_max+1 ) ,
Chris@16 96 m_table( m_k_max ) ,
Chris@16 97 STEPFAC1( 0.65 ) , STEPFAC2( 0.94 ) , STEPFAC3( 0.02 ) , STEPFAC4( 4.0 ) , KFAC1( 0.8 ) , KFAC2( 0.9 )
Chris@16 98 {
Chris@16 99 BOOST_USING_STD_MIN();
Chris@16 100 BOOST_USING_STD_MAX();
Chris@16 101 /* initialize sequence of stage numbers and work */
Chris@16 102 for( unsigned short i = 0; i < m_k_max+1; i++ )
Chris@16 103 {
Chris@16 104 m_interval_sequence[i] = 2 * (i+1);
Chris@16 105 if( i == 0 )
Chris@16 106 m_cost[i] = m_interval_sequence[i];
Chris@16 107 else
Chris@16 108 m_cost[i] = m_cost[i-1] + m_interval_sequence[i];
Chris@16 109 m_coeff[i].resize(i);
Chris@16 110 for( size_t k = 0 ; k < i ; ++k )
Chris@16 111 {
Chris@16 112 const value_type r = static_cast< value_type >( m_interval_sequence[i] ) / static_cast< value_type >( m_interval_sequence[k] );
Chris@16 113 m_coeff[i][k] = 1.0 / ( r*r - static_cast< value_type >( 1.0 ) ); // coefficients for extrapolation
Chris@16 114 }
Chris@16 115
Chris@16 116 // crude estimate of optimal order
Chris@16 117
Chris@16 118 m_current_k_opt = 4;
Chris@16 119 /* no calculation because log10 might not exist for value_type!
Chris@16 120 const value_type logfact( -log10( max BOOST_PREVENT_MACRO_SUBSTITUTION( eps_rel , static_cast< value_type >(1.0E-12) ) ) * 0.6 + 0.5 );
Chris@16 121 m_current_k_opt = max BOOST_PREVENT_MACRO_SUBSTITUTION( static_cast<value_type>( 1 ) , min BOOST_PREVENT_MACRO_SUBSTITUTION( static_cast<value_type>( m_k_max-1 ) , logfact ));
Chris@16 122 */
Chris@16 123 }
Chris@16 124
Chris@16 125 }
Chris@16 126
Chris@16 127
Chris@16 128 /*
Chris@16 129 * Version 1 : try_step( sys , x , t , dt )
Chris@16 130 *
Chris@16 131 * The overloads are needed to solve the forwarding problem
Chris@16 132 */
Chris@16 133 template< class System , class StateInOut >
Chris@16 134 controlled_step_result try_step( System system , StateInOut &x , time_type &t , time_type &dt )
Chris@16 135 {
Chris@16 136 return try_step_v1( system , x , t, dt );
Chris@16 137 }
Chris@16 138
Chris@16 139 /**
Chris@16 140 * \brief Second version to solve the forwarding problem, can be used with Boost.Range as StateInOut.
Chris@16 141 */
Chris@16 142 template< class System , class StateInOut >
Chris@16 143 controlled_step_result try_step( System system , const StateInOut &x , time_type &t , time_type &dt )
Chris@16 144 {
Chris@16 145 return try_step_v1( system , x , t, dt );
Chris@16 146 }
Chris@16 147
Chris@16 148 /*
Chris@16 149 * Version 2 : try_step( sys , x , dxdt , t , dt )
Chris@16 150 *
Chris@16 151 * this version does not solve the forwarding problem, boost.range can not be used
Chris@16 152 */
Chris@16 153 template< class System , class StateInOut , class DerivIn >
Chris@16 154 controlled_step_result try_step( System system , StateInOut &x , const DerivIn &dxdt , time_type &t , time_type &dt )
Chris@16 155 {
Chris@16 156 m_xnew_resizer.adjust_size( x , detail::bind( &controlled_error_bs_type::template resize_m_xnew< StateInOut > , detail::ref( *this ) , detail::_1 ) );
Chris@16 157 controlled_step_result res = try_step( system , x , dxdt , t , m_xnew.m_v , dt );
Chris@16 158 if( res == success )
Chris@16 159 {
Chris@16 160 boost::numeric::odeint::copy( m_xnew.m_v , x );
Chris@16 161 }
Chris@16 162 return res;
Chris@16 163 }
Chris@16 164
Chris@16 165 /*
Chris@16 166 * Version 3 : try_step( sys , in , t , out , dt )
Chris@16 167 *
Chris@16 168 * this version does not solve the forwarding problem, boost.range can not be used
Chris@16 169 */
Chris@16 170 template< class System , class StateIn , class StateOut >
Chris@16 171 typename boost::disable_if< boost::is_same< StateIn , time_type > , controlled_step_result >::type
Chris@16 172 try_step( System system , const StateIn &in , time_type &t , StateOut &out , time_type &dt )
Chris@16 173 {
Chris@16 174 typename odeint::unwrap_reference< System >::type &sys = system;
Chris@16 175 m_dxdt_resizer.adjust_size( in , detail::bind( &controlled_error_bs_type::template resize_m_dxdt< StateIn > , detail::ref( *this ) , detail::_1 ) );
Chris@16 176 sys( in , m_dxdt.m_v , t );
Chris@16 177 return try_step( system , in , m_dxdt.m_v , t , out , dt );
Chris@16 178 }
Chris@16 179
Chris@16 180
Chris@16 181 /*
Chris@16 182 * Full version : try_step( sys , in , dxdt_in , t , out , dt )
Chris@16 183 *
Chris@16 184 * contains the actual implementation
Chris@16 185 */
Chris@16 186 template< class System , class StateIn , class DerivIn , class StateOut >
Chris@16 187 controlled_step_result try_step( System system , const StateIn &in , const DerivIn &dxdt , time_type &t , StateOut &out , time_type &dt )
Chris@16 188 {
Chris@16 189 BOOST_USING_STD_MIN();
Chris@16 190 BOOST_USING_STD_MAX();
Chris@16 191
Chris@16 192 static const value_type val1( 1.0 );
Chris@16 193
Chris@16 194 typename odeint::unwrap_reference< System >::type &sys = system;
Chris@16 195 if( m_resizer.adjust_size( in , detail::bind( &controlled_error_bs_type::template resize_impl< StateIn > , detail::ref( *this ) , detail::_1 ) ) )
Chris@16 196 {
Chris@16 197 reset(); // system resized -> reset
Chris@16 198 }
Chris@16 199
Chris@16 200 if( dt != m_dt_last )
Chris@16 201 {
Chris@16 202 reset(); // step size changed from outside -> reset
Chris@16 203 }
Chris@16 204
Chris@16 205 bool reject( true );
Chris@16 206
Chris@16 207 time_vector h_opt( m_k_max+1 );
Chris@16 208 inv_time_vector work( m_k_max+1 );
Chris@16 209
Chris@16 210 time_type new_h = dt;
Chris@16 211
Chris@16 212 /* m_current_k_opt is the estimated current optimal stage number */
Chris@16 213 for( size_t k = 0 ; k <= m_current_k_opt+1 ; k++ )
Chris@16 214 {
Chris@16 215 /* the stage counts are stored in m_interval_sequence */
Chris@16 216 m_midpoint.set_steps( m_interval_sequence[k] );
Chris@16 217 if( k == 0 )
Chris@16 218 {
Chris@16 219 m_midpoint.do_step( sys , in , dxdt , t , out , dt );
Chris@16 220 /* the first step, nothing more to do */
Chris@16 221 }
Chris@16 222 else
Chris@16 223 {
Chris@16 224 m_midpoint.do_step( sys , in , dxdt , t , m_table[k-1].m_v , dt );
Chris@16 225 extrapolate( k , m_table , m_coeff , out );
Chris@16 226 // get error estimate
Chris@16 227 m_algebra.for_each3( m_err.m_v , out , m_table[0].m_v ,
Chris@16 228 typename operations_type::template scale_sum2< value_type , value_type >( val1 , -val1 ) );
Chris@16 229 const value_type error = m_error_checker.error( m_algebra , in , dxdt , m_err.m_v , dt );
Chris@16 230 h_opt[k] = calc_h_opt( dt , error , k );
Chris@16 231 work[k] = static_cast<value_type>( m_cost[k] ) / h_opt[k];
Chris@16 232
Chris@16 233 if( (k == m_current_k_opt-1) || m_first )
Chris@16 234 { // convergence before k_opt ?
Chris@16 235 if( error < 1.0 )
Chris@16 236 {
Chris@16 237 //convergence
Chris@16 238 reject = false;
Chris@16 239 if( (work[k] < KFAC2*work[k-1]) || (m_current_k_opt <= 2) )
Chris@16 240 {
Chris@16 241 // leave order as is (except we were in first round)
Chris@16 242 m_current_k_opt = min BOOST_PREVENT_MACRO_SUBSTITUTION( static_cast<int>(m_k_max)-1 , max BOOST_PREVENT_MACRO_SUBSTITUTION( 2 , static_cast<int>(k)+1 ) );
Chris@16 243 new_h = h_opt[k];
Chris@16 244 new_h *= static_cast<value_type>( m_cost[k+1] ) / static_cast<value_type>( m_cost[k] );
Chris@16 245 } else {
Chris@16 246 m_current_k_opt = min BOOST_PREVENT_MACRO_SUBSTITUTION( static_cast<int>(m_k_max)-1 , max BOOST_PREVENT_MACRO_SUBSTITUTION( 2 , static_cast<int>(k) ) );
Chris@16 247 new_h = h_opt[k];
Chris@16 248 }
Chris@16 249 break;
Chris@16 250 }
Chris@16 251 else if( should_reject( error , k ) && !m_first )
Chris@16 252 {
Chris@16 253 reject = true;
Chris@16 254 new_h = h_opt[k];
Chris@16 255 break;
Chris@16 256 }
Chris@16 257 }
Chris@16 258 if( k == m_current_k_opt )
Chris@16 259 { // convergence at k_opt ?
Chris@16 260 if( error < 1.0 )
Chris@16 261 {
Chris@16 262 //convergence
Chris@16 263 reject = false;
Chris@16 264 if( (work[k-1] < KFAC2*work[k]) )
Chris@16 265 {
Chris@16 266 m_current_k_opt = max BOOST_PREVENT_MACRO_SUBSTITUTION( 2 , static_cast<int>(m_current_k_opt)-1 );
Chris@16 267 new_h = h_opt[m_current_k_opt];
Chris@16 268 }
Chris@16 269 else if( (work[k] < KFAC2*work[k-1]) && !m_last_step_rejected )
Chris@16 270 {
Chris@16 271 m_current_k_opt = min BOOST_PREVENT_MACRO_SUBSTITUTION( static_cast<int>(m_k_max-1) , static_cast<int>(m_current_k_opt)+1 );
Chris@16 272 new_h = h_opt[k];
Chris@16 273 new_h *= m_cost[m_current_k_opt]/m_cost[k];
Chris@16 274 } else
Chris@16 275 new_h = h_opt[m_current_k_opt];
Chris@16 276 break;
Chris@16 277 }
Chris@16 278 else if( should_reject( error , k ) )
Chris@16 279 {
Chris@16 280 reject = true;
Chris@16 281 new_h = h_opt[m_current_k_opt];
Chris@16 282 break;
Chris@16 283 }
Chris@16 284 }
Chris@16 285 if( k == m_current_k_opt+1 )
Chris@16 286 { // convergence at k_opt+1 ?
Chris@16 287 if( error < 1.0 )
Chris@16 288 { //convergence
Chris@16 289 reject = false;
Chris@16 290 if( work[k-2] < KFAC2*work[k-1] )
Chris@16 291 m_current_k_opt = max BOOST_PREVENT_MACRO_SUBSTITUTION( 2 , static_cast<int>(m_current_k_opt)-1 );
Chris@16 292 if( (work[k] < KFAC2*work[m_current_k_opt]) && !m_last_step_rejected )
Chris@16 293 m_current_k_opt = min BOOST_PREVENT_MACRO_SUBSTITUTION( static_cast<int>(m_k_max)-1 , static_cast<int>(k) );
Chris@16 294 new_h = h_opt[m_current_k_opt];
Chris@16 295 } else
Chris@16 296 {
Chris@16 297 reject = true;
Chris@16 298 new_h = h_opt[m_current_k_opt];
Chris@16 299 }
Chris@16 300 break;
Chris@16 301 }
Chris@16 302 }
Chris@16 303 }
Chris@16 304
Chris@16 305 if( !reject )
Chris@16 306 {
Chris@16 307 t += dt;
Chris@16 308 }
Chris@16 309
Chris@16 310 if( !m_last_step_rejected || boost::numeric::odeint::detail::less_with_sign(new_h, dt, dt) )
Chris@16 311 {
Chris@16 312 m_dt_last = new_h;
Chris@16 313 dt = new_h;
Chris@16 314 }
Chris@16 315
Chris@16 316 m_last_step_rejected = reject;
Chris@16 317 m_first = false;
Chris@16 318
Chris@16 319 if( reject )
Chris@16 320 return fail;
Chris@16 321 else
Chris@16 322 return success;
Chris@16 323 }
Chris@16 324
Chris@16 325 /** \brief Resets the internal state of the stepper */
Chris@16 326 void reset()
Chris@16 327 {
Chris@16 328 m_first = true;
Chris@16 329 m_last_step_rejected = false;
Chris@16 330 }
Chris@16 331
Chris@16 332
Chris@16 333 /* Resizer methods */
Chris@16 334
Chris@16 335 template< class StateIn >
Chris@16 336 void adjust_size( const StateIn &x )
Chris@16 337 {
Chris@16 338 resize_m_dxdt( x );
Chris@16 339 resize_m_xnew( x );
Chris@16 340 resize_impl( x );
Chris@16 341 m_midpoint.adjust_size();
Chris@16 342 }
Chris@16 343
Chris@16 344
Chris@16 345 private:
Chris@16 346
Chris@16 347 template< class StateIn >
Chris@16 348 bool resize_m_dxdt( const StateIn &x )
Chris@16 349 {
Chris@16 350 return adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable<deriv_type>::type() );
Chris@16 351 }
Chris@16 352
Chris@16 353 template< class StateIn >
Chris@16 354 bool resize_m_xnew( const StateIn &x )
Chris@16 355 {
Chris@16 356 return adjust_size_by_resizeability( m_xnew , x , typename is_resizeable<state_type>::type() );
Chris@16 357 }
Chris@16 358
Chris@16 359 template< class StateIn >
Chris@16 360 bool resize_impl( const StateIn &x )
Chris@16 361 {
Chris@16 362 bool resized( false );
Chris@16 363 for( size_t i = 0 ; i < m_k_max ; ++i )
Chris@16 364 resized |= adjust_size_by_resizeability( m_table[i] , x , typename is_resizeable<state_type>::type() );
Chris@16 365 resized |= adjust_size_by_resizeability( m_err , x , typename is_resizeable<state_type>::type() );
Chris@16 366 return resized;
Chris@16 367 }
Chris@16 368
Chris@16 369
Chris@16 370 template< class System , class StateInOut >
Chris@16 371 controlled_step_result try_step_v1( System system , StateInOut &x , time_type &t , time_type &dt )
Chris@16 372 {
Chris@16 373 typename odeint::unwrap_reference< System >::type &sys = system;
Chris@16 374 m_dxdt_resizer.adjust_size( x , detail::bind( &controlled_error_bs_type::template resize_m_dxdt< StateInOut > , detail::ref( *this ) , detail::_1 ) );
Chris@16 375 sys( x , m_dxdt.m_v ,t );
Chris@16 376 return try_step( system , x , m_dxdt.m_v , t , dt );
Chris@16 377 }
Chris@16 378
Chris@16 379
Chris@16 380 template< class StateInOut >
Chris@16 381 void extrapolate( size_t k , state_table_type &table , const value_matrix &coeff , StateInOut &xest )
Chris@16 382 /* polynomial extrapolation, see http://www.nr.com/webnotes/nr3web21.pdf
Chris@16 383 uses the obtained intermediate results to extrapolate to dt->0
Chris@16 384 */
Chris@16 385 {
Chris@16 386 static const value_type val1 = static_cast< value_type >( 1.0 );
Chris@16 387 for( int j=k-1 ; j>0 ; --j )
Chris@16 388 {
Chris@16 389 m_algebra.for_each3( table[j-1].m_v , table[j].m_v , table[j-1].m_v ,
Chris@16 390 typename operations_type::template scale_sum2< value_type , value_type >( val1 + coeff[k][j] , -coeff[k][j] ) );
Chris@16 391 }
Chris@16 392 m_algebra.for_each3( xest , table[0].m_v , xest ,
Chris@16 393 typename operations_type::template scale_sum2< value_type , value_type >( val1 + coeff[k][0] , -coeff[k][0]) );
Chris@16 394 }
Chris@16 395
Chris@16 396 time_type calc_h_opt( time_type h , value_type error , size_t k ) const
Chris@16 397 /* calculates the optimal step size for a given error and stage number */
Chris@16 398 {
Chris@16 399 BOOST_USING_STD_MIN();
Chris@16 400 BOOST_USING_STD_MAX();
Chris@16 401 using std::pow;
Chris@16 402 value_type expo( 1.0/(2*k+1) );
Chris@16 403 value_type facmin = pow BOOST_PREVENT_MACRO_SUBSTITUTION( STEPFAC3 , expo );
Chris@16 404 value_type fac;
Chris@16 405 if (error == 0.0)
Chris@16 406 fac=1.0/facmin;
Chris@16 407 else
Chris@16 408 {
Chris@16 409 fac = STEPFAC2 / pow BOOST_PREVENT_MACRO_SUBSTITUTION( error / STEPFAC1 , expo );
Chris@16 410 fac = max BOOST_PREVENT_MACRO_SUBSTITUTION( facmin/STEPFAC4 , min BOOST_PREVENT_MACRO_SUBSTITUTION( 1.0/facmin , fac ) );
Chris@16 411 }
Chris@16 412 return h*fac;
Chris@16 413 }
Chris@16 414
Chris@16 415 controlled_step_result set_k_opt( size_t k , const inv_time_vector &work , const time_vector &h_opt , time_type &dt )
Chris@16 416 /* calculates the optimal stage number */
Chris@16 417 {
Chris@16 418 if( k == 1 )
Chris@16 419 {
Chris@16 420 m_current_k_opt = 2;
Chris@16 421 return success;
Chris@16 422 }
Chris@16 423 if( (work[k-1] < KFAC1*work[k]) || (k == m_k_max) )
Chris@16 424 { // order decrease
Chris@16 425 m_current_k_opt = k-1;
Chris@16 426 dt = h_opt[ m_current_k_opt ];
Chris@16 427 return success;
Chris@16 428 }
Chris@16 429 else if( (work[k] < KFAC2*work[k-1]) || m_last_step_rejected || (k == m_k_max-1) )
Chris@16 430 { // same order - also do this if last step got rejected
Chris@16 431 m_current_k_opt = k;
Chris@16 432 dt = h_opt[ m_current_k_opt ];
Chris@16 433 return success;
Chris@16 434 }
Chris@16 435 else
Chris@16 436 { // order increase - only if last step was not rejected
Chris@16 437 m_current_k_opt = k+1;
Chris@16 438 dt = h_opt[ m_current_k_opt-1 ] * m_cost[ m_current_k_opt ] / m_cost[ m_current_k_opt-1 ] ;
Chris@16 439 return success;
Chris@16 440 }
Chris@16 441 }
Chris@16 442
Chris@16 443 bool in_convergence_window( size_t k ) const
Chris@16 444 {
Chris@16 445 if( (k == m_current_k_opt-1) && !m_last_step_rejected )
Chris@16 446 return true; // decrease stepsize only if last step was not rejected
Chris@16 447 return ( (k == m_current_k_opt) || (k == m_current_k_opt+1) );
Chris@16 448 }
Chris@16 449
Chris@16 450 bool should_reject( value_type error , size_t k ) const
Chris@16 451 {
Chris@16 452 if( k == m_current_k_opt-1 )
Chris@16 453 {
Chris@16 454 const value_type d = m_interval_sequence[m_current_k_opt] * m_interval_sequence[m_current_k_opt+1] /
Chris@16 455 (m_interval_sequence[0]*m_interval_sequence[0]);
Chris@16 456 //step will fail, criterion 17.3.17 in NR
Chris@16 457 return ( error > d*d );
Chris@16 458 }
Chris@16 459 else if( k == m_current_k_opt )
Chris@16 460 {
Chris@16 461 const value_type d = m_interval_sequence[m_current_k_opt] / m_interval_sequence[0];
Chris@16 462 return ( error > d*d );
Chris@16 463 } else
Chris@16 464 return error > 1.0;
Chris@16 465 }
Chris@16 466
Chris@16 467 default_error_checker< value_type, algebra_type , operations_type > m_error_checker;
Chris@16 468 modified_midpoint< state_type , value_type , deriv_type , time_type , algebra_type , operations_type , resizer_type > m_midpoint;
Chris@16 469
Chris@16 470 bool m_last_step_rejected;
Chris@16 471 bool m_first;
Chris@16 472
Chris@16 473 time_type m_dt_last;
Chris@16 474 time_type m_t_last;
Chris@16 475
Chris@16 476 size_t m_current_k_opt;
Chris@16 477
Chris@16 478 algebra_type m_algebra;
Chris@16 479
Chris@16 480 resizer_type m_dxdt_resizer;
Chris@16 481 resizer_type m_xnew_resizer;
Chris@16 482 resizer_type m_resizer;
Chris@16 483
Chris@16 484 wrapped_state_type m_xnew;
Chris@16 485 wrapped_state_type m_err;
Chris@16 486 wrapped_deriv_type m_dxdt;
Chris@16 487
Chris@16 488 int_vector m_interval_sequence; // stores the successive interval counts
Chris@16 489 value_matrix m_coeff;
Chris@16 490 int_vector m_cost; // costs for interval count
Chris@16 491
Chris@16 492 state_table_type m_table; // sequence of states for extrapolation
Chris@16 493
Chris@16 494 const value_type STEPFAC1 , STEPFAC2 , STEPFAC3 , STEPFAC4 , KFAC1 , KFAC2;
Chris@16 495 };
Chris@16 496
Chris@16 497
Chris@16 498 /******** DOXYGEN ********/
Chris@16 499 /**
Chris@16 500 * \class bulirsch_stoer
Chris@16 501 * \brief The Bulirsch-Stoer algorithm.
Chris@16 502 *
Chris@16 503 * The Bulirsch-Stoer is a controlled stepper that adjusts both step size
Chris@16 504 * and order of the method. The algorithm uses the modified midpoint and
Chris@16 505 * a polynomial extrapolation compute the solution.
Chris@16 506 *
Chris@16 507 * \tparam State The state type.
Chris@16 508 * \tparam Value The value type.
Chris@16 509 * \tparam Deriv The type representing the time derivative of the state.
Chris@16 510 * \tparam Time The time representing the independent variable - the time.
Chris@16 511 * \tparam Algebra The algebra type.
Chris@16 512 * \tparam Operations The operations type.
Chris@16 513 * \tparam Resizer The resizer policy type.
Chris@16 514 */
Chris@16 515
Chris@16 516 /**
Chris@16 517 * \fn bulirsch_stoer::bulirsch_stoer( value_type eps_abs , value_type eps_rel , value_type factor_x , value_type factor_dxdt )
Chris@16 518 * \brief Constructs the bulirsch_stoer class, including initialization of
Chris@16 519 * the error bounds.
Chris@16 520 *
Chris@16 521 * \param eps_abs Absolute tolerance level.
Chris@16 522 * \param eps_rel Relative tolerance level.
Chris@16 523 * \param factor_x Factor for the weight of the state.
Chris@16 524 * \param factor_dxdt Factor for the weight of the derivative.
Chris@16 525 */
Chris@16 526
Chris@16 527 /**
Chris@16 528 * \fn bulirsch_stoer::try_step( System system , StateInOut &x , time_type &t , time_type &dt )
Chris@16 529 * \brief Tries to perform one step.
Chris@16 530 *
Chris@16 531 * This method tries to do one step with step size dt. If the error estimate
Chris@16 532 * is to large, the step is rejected and the method returns fail and the
Chris@16 533 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 534 * step is performed, success is returned and dt might be increased to make
Chris@16 535 * the steps as large as possible. This method also updates t if a step is
Chris@16 536 * performed. Also, the internal order of the stepper is adjusted if required.
Chris@16 537 *
Chris@16 538 * \param system The system function to solve, hence the r.h.s. of the ODE.
Chris@16 539 * It must fulfill the Simple System concept.
Chris@16 540 * \param x The state of the ODE which should be solved. Overwritten if
Chris@16 541 * the step is successful.
Chris@16 542 * \param t The value of the time. Updated if the step is successful.
Chris@16 543 * \param dt The step size. Updated.
Chris@16 544 * \return success if the step was accepted, fail otherwise.
Chris@16 545 */
Chris@16 546
Chris@16 547 /**
Chris@16 548 * \fn bulirsch_stoer::try_step( System system , StateInOut &x , const DerivIn &dxdt , time_type &t , time_type &dt )
Chris@16 549 * \brief Tries to perform one step.
Chris@16 550 *
Chris@16 551 * This method tries to do one step with step size dt. If the error estimate
Chris@16 552 * is to large, the step is rejected and the method returns fail and the
Chris@16 553 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 554 * step is performed, success is returned and dt might be increased to make
Chris@16 555 * the steps as large as possible. This method also updates t if a step is
Chris@16 556 * performed. Also, the internal order of the stepper is adjusted if required.
Chris@16 557 *
Chris@16 558 * \param system The system function to solve, hence the r.h.s. of the ODE.
Chris@16 559 * It must fulfill the Simple System concept.
Chris@16 560 * \param x The state of the ODE which should be solved. Overwritten if
Chris@16 561 * the step is successful.
Chris@16 562 * \param dxdt The derivative of state.
Chris@16 563 * \param t The value of the time. Updated if the step is successful.
Chris@16 564 * \param dt The step size. Updated.
Chris@16 565 * \return success if the step was accepted, fail otherwise.
Chris@16 566 */
Chris@16 567
Chris@16 568 /**
Chris@16 569 * \fn bulirsch_stoer::try_step( System system , const StateIn &in , time_type &t , StateOut &out , time_type &dt )
Chris@16 570 * \brief Tries to perform one step.
Chris@16 571 *
Chris@16 572 * \note This method is disabled if state_type=time_type to avoid ambiguity.
Chris@16 573 *
Chris@16 574 * This method tries to do one step with step size dt. If the error estimate
Chris@16 575 * is to large, the step is rejected and the method returns fail and the
Chris@16 576 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 577 * step is performed, success is returned and dt might be increased to make
Chris@16 578 * the steps as large as possible. This method also updates t if a step is
Chris@16 579 * performed. Also, the internal order of the stepper is adjusted if required.
Chris@16 580 *
Chris@16 581 * \param system The system function to solve, hence the r.h.s. of the ODE.
Chris@16 582 * It must fulfill the Simple System concept.
Chris@16 583 * \param in The state of the ODE which should be solved.
Chris@16 584 * \param t The value of the time. Updated if the step is successful.
Chris@16 585 * \param out Used to store the result of the step.
Chris@16 586 * \param dt The step size. Updated.
Chris@16 587 * \return success if the step was accepted, fail otherwise.
Chris@16 588 */
Chris@16 589
Chris@16 590
Chris@16 591 /**
Chris@16 592 * \fn bulirsch_stoer::try_step( System system , const StateIn &in , const DerivIn &dxdt , time_type &t , StateOut &out , time_type &dt )
Chris@16 593 * \brief Tries to perform one step.
Chris@16 594 *
Chris@16 595 * This method tries to do one step with step size dt. If the error estimate
Chris@16 596 * is to large, the step is rejected and the method returns fail and the
Chris@16 597 * step size dt is reduced. If the error estimate is acceptably small, the
Chris@16 598 * step is performed, success is returned and dt might be increased to make
Chris@16 599 * the steps as large as possible. This method also updates t if a step is
Chris@16 600 * performed. Also, the internal order of the stepper is adjusted if required.
Chris@16 601 *
Chris@16 602 * \param system The system function to solve, hence the r.h.s. of the ODE.
Chris@16 603 * It must fulfill the Simple System concept.
Chris@16 604 * \param in The state of the ODE which should be solved.
Chris@16 605 * \param dxdt The derivative of state.
Chris@16 606 * \param t The value of the time. Updated if the step is successful.
Chris@16 607 * \param out Used to store the result of the step.
Chris@16 608 * \param dt The step size. Updated.
Chris@16 609 * \return success if the step was accepted, fail otherwise.
Chris@16 610 */
Chris@16 611
Chris@16 612
Chris@16 613 /**
Chris@16 614 * \fn bulirsch_stoer::adjust_size( const StateIn &x )
Chris@16 615 * \brief Adjust the size of all temporaries in the stepper manually.
Chris@16 616 * \param x A state from which the size of the temporaries to be resized is deduced.
Chris@16 617 */
Chris@16 618
Chris@16 619 }
Chris@16 620 }
Chris@16 621 }
Chris@16 622
Chris@16 623 #endif // BOOST_NUMERIC_ODEINT_STEPPER_BULIRSCH_STOER_HPP_INCLUDED