annotate DEPENDENCIES/generic/include/boost/numeric/odeint/stepper/bulirsch_stoer.hpp @ 133:4acb5d8d80b6 tip

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