annotate DEPENDENCIES/generic/include/boost/numeric/odeint/stepper/adams_bashforth.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 /*
Chris@16 2 [auto_generated]
Chris@16 3 boost/numeric/odeint/stepper/adams_bashforth.hpp
Chris@16 4
Chris@16 5 [begin_description]
Chris@16 6 Implementaton of the Adam-Bashforth method a multistep method used for the predictor step in the
Chris@16 7 Adams-Bashforth-Moulton method.
Chris@16 8 [end_description]
Chris@16 9
Chris@101 10 Copyright 2011-2013 Karsten Ahnert
Chris@101 11 Copyright 2011-2013 Mario Mulansky
Chris@101 12 Copyright 2012 Christoph Koke
Chris@101 13 Copyright 2013 Pascal Germroth
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_ADAMS_BASHFORTH_HPP_INCLUDED
Chris@16 22 #define BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_HPP_INCLUDED
Chris@16 23
Chris@16 24 #include <boost/static_assert.hpp>
Chris@16 25
Chris@16 26 #include <boost/numeric/odeint/util/bind.hpp>
Chris@16 27 #include <boost/numeric/odeint/util/unwrap_reference.hpp>
Chris@16 28
Chris@16 29 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
Chris@16 30 #include <boost/numeric/odeint/algebra/default_operations.hpp>
Chris@101 31 #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
Chris@101 32 #include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
Chris@16 33
Chris@16 34 #include <boost/numeric/odeint/util/state_wrapper.hpp>
Chris@16 35 #include <boost/numeric/odeint/util/is_resizeable.hpp>
Chris@16 36 #include <boost/numeric/odeint/util/resizer.hpp>
Chris@16 37
Chris@16 38 #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
Chris@16 39 #include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
Chris@101 40 #include <boost/numeric/odeint/stepper/extrapolation_stepper.hpp>
Chris@16 41
Chris@16 42 #include <boost/numeric/odeint/stepper/base/algebra_stepper_base.hpp>
Chris@16 43
Chris@16 44 #include <boost/numeric/odeint/stepper/detail/adams_bashforth_coefficients.hpp>
Chris@16 45 #include <boost/numeric/odeint/stepper/detail/adams_bashforth_call_algebra.hpp>
Chris@16 46 #include <boost/numeric/odeint/stepper/detail/rotating_buffer.hpp>
Chris@16 47
Chris@101 48 #include <boost/mpl/arithmetic.hpp>
Chris@101 49 #include <boost/mpl/min_max.hpp>
Chris@101 50 #include <boost/mpl/equal_to.hpp>
Chris@101 51
Chris@101 52 namespace mpl = boost::mpl;
Chris@16 53
Chris@16 54
Chris@16 55 namespace boost {
Chris@16 56 namespace numeric {
Chris@16 57 namespace odeint {
Chris@16 58
Chris@101 59 using mpl::int_;
Chris@101 60
Chris@101 61 /* if N >= 4, returns the smallest even number > N, otherwise returns 4 */
Chris@101 62 template < int N >
Chris@101 63 struct order_helper
Chris@101 64 : mpl::max< typename mpl::eval_if<
Chris@101 65 mpl::equal_to< mpl::modulus< int_< N >, int_< 2 > >,
Chris@101 66 int_< 0 > >,
Chris@101 67 int_< N >, int_< N + 1 > >::type,
Chris@101 68 int_< 4 > >::type
Chris@101 69 { };
Chris@16 70
Chris@16 71 template<
Chris@16 72 size_t Steps ,
Chris@16 73 class State ,
Chris@16 74 class Value = double ,
Chris@16 75 class Deriv = State ,
Chris@16 76 class Time = Value ,
Chris@101 77 class Algebra = typename algebra_dispatcher< State >::algebra_type ,
Chris@101 78 class Operations = typename operations_dispatcher< State >::operations_type ,
Chris@16 79 class Resizer = initially_resizer ,
Chris@101 80 class InitializingStepper = extrapolation_stepper< order_helper<Steps>::value,
Chris@101 81 State, Value, Deriv, Time,
Chris@101 82 Algebra, Operations, Resizer >
Chris@16 83 >
Chris@16 84 class adams_bashforth : public algebra_stepper_base< Algebra , Operations >
Chris@16 85 {
Chris@16 86
Chris@16 87 #ifndef DOXYGEN_SKIP
Chris@16 88 BOOST_STATIC_ASSERT(( Steps > 0 ));
Chris@16 89 BOOST_STATIC_ASSERT(( Steps < 9 ));
Chris@16 90 #endif
Chris@16 91
Chris@16 92 public :
Chris@16 93
Chris@16 94 typedef State state_type;
Chris@16 95 typedef state_wrapper< state_type > wrapped_state_type;
Chris@16 96 typedef Value value_type;
Chris@16 97 typedef Deriv deriv_type;
Chris@16 98 typedef state_wrapper< deriv_type > wrapped_deriv_type;
Chris@16 99 typedef Time time_type;
Chris@16 100 typedef Resizer resizer_type;
Chris@16 101 typedef stepper_tag stepper_category;
Chris@16 102
Chris@16 103 typedef InitializingStepper initializing_stepper_type;
Chris@16 104
Chris@101 105 typedef algebra_stepper_base< Algebra , Operations > algebra_stepper_base_type;
Chris@101 106 typedef typename algebra_stepper_base_type::algebra_type algebra_type;
Chris@101 107 typedef typename algebra_stepper_base_type::operations_type operations_type;
Chris@16 108 #ifndef DOXYGEN_SKIP
Chris@16 109 typedef adams_bashforth< Steps , State , Value , Deriv , Time , Algebra , Operations , Resizer , InitializingStepper > stepper_type;
Chris@16 110 #endif
Chris@16 111 static const size_t steps = Steps;
Chris@16 112
Chris@16 113
Chris@16 114
Chris@16 115 typedef unsigned short order_type;
Chris@16 116 static const order_type order_value = steps;
Chris@16 117
Chris@16 118 typedef detail::rotating_buffer< wrapped_deriv_type , steps > step_storage_type;
Chris@16 119
Chris@16 120
Chris@16 121
Chris@16 122 order_type order( void ) const { return order_value; }
Chris@16 123
Chris@16 124 adams_bashforth( const algebra_type &algebra = algebra_type() )
Chris@101 125 : algebra_stepper_base_type( algebra ) ,
Chris@101 126 m_step_storage() , m_resizer() , m_coefficients() ,
Chris@101 127 m_steps_initialized( 0 ) , m_initializing_stepper()
Chris@16 128 { }
Chris@16 129
Chris@16 130
Chris@16 131
Chris@16 132 /*
Chris@16 133 * Version 1 : do_step( system , x , t , dt );
Chris@16 134 *
Chris@16 135 * solves the forwarding problem
Chris@16 136 */
Chris@16 137 template< class System , class StateInOut >
Chris@16 138 void do_step( System system , StateInOut &x , time_type t , time_type dt )
Chris@16 139 {
Chris@16 140 do_step( system , x , t , x , dt );
Chris@16 141 }
Chris@16 142
Chris@16 143 /**
Chris@16 144 * \brief Second version to solve the forwarding problem, can be called with Boost.Range as StateInOut.
Chris@16 145 */
Chris@16 146 template< class System , class StateInOut >
Chris@16 147 void do_step( System system , const StateInOut &x , time_type t , time_type dt )
Chris@16 148 {
Chris@16 149 do_step( system , x , t , x , dt );
Chris@16 150 }
Chris@16 151
Chris@16 152
Chris@16 153
Chris@16 154 /*
Chris@16 155 * Version 2 : do_step( system , in , t , out , dt );
Chris@16 156 *
Chris@16 157 * solves the forwarding problem
Chris@16 158 */
Chris@16 159
Chris@16 160 template< class System , class StateIn , class StateOut >
Chris@16 161 void do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
Chris@16 162 {
Chris@16 163 do_step_impl( system , in , t , out , dt );
Chris@16 164 }
Chris@16 165
Chris@16 166 /**
Chris@16 167 * \brief Second version to solve the forwarding problem, can be called with Boost.Range as StateOut.
Chris@16 168 */
Chris@16 169 template< class System , class StateIn , class StateOut >
Chris@16 170 void do_step( System system , const StateIn &in , time_type t , const StateOut &out , time_type dt )
Chris@16 171 {
Chris@16 172 do_step_impl( system , in , t , out , dt );
Chris@16 173 }
Chris@16 174
Chris@16 175
Chris@16 176 template< class StateType >
Chris@16 177 void adjust_size( const StateType &x )
Chris@16 178 {
Chris@16 179 resize_impl( x );
Chris@16 180 }
Chris@16 181
Chris@16 182 const step_storage_type& step_storage( void ) const
Chris@16 183 {
Chris@16 184 return m_step_storage;
Chris@16 185 }
Chris@16 186
Chris@16 187 step_storage_type& step_storage( void )
Chris@16 188 {
Chris@16 189 return m_step_storage;
Chris@16 190 }
Chris@16 191
Chris@16 192 template< class ExplicitStepper , class System , class StateIn >
Chris@16 193 void initialize( ExplicitStepper explicit_stepper , System system , StateIn &x , time_type &t , time_type dt )
Chris@16 194 {
Chris@16 195 typename odeint::unwrap_reference< ExplicitStepper >::type &stepper = explicit_stepper;
Chris@16 196 typename odeint::unwrap_reference< System >::type &sys = system;
Chris@16 197
Chris@16 198 m_resizer.adjust_size( x , detail::bind( &stepper_type::template resize_impl<StateIn> , detail::ref( *this ) , detail::_1 ) );
Chris@16 199
Chris@101 200 for( size_t i=0 ; i+1<steps ; ++i )
Chris@16 201 {
Chris@16 202 if( i != 0 ) m_step_storage.rotate();
Chris@16 203 sys( x , m_step_storage[0].m_v , t );
Chris@101 204 stepper.do_step_dxdt_impl( system, x, m_step_storage[0].m_v, t,
Chris@101 205 dt );
Chris@16 206 t += dt;
Chris@16 207 }
Chris@16 208 m_steps_initialized = steps;
Chris@16 209 }
Chris@16 210
Chris@16 211 template< class System , class StateIn >
Chris@16 212 void initialize( System system , StateIn &x , time_type &t , time_type dt )
Chris@16 213 {
Chris@16 214 initialize( detail::ref( m_initializing_stepper ) , system , x , t , dt );
Chris@16 215 }
Chris@16 216
Chris@16 217 void reset( void )
Chris@16 218 {
Chris@16 219 m_steps_initialized = 0;
Chris@16 220 }
Chris@16 221
Chris@16 222 bool is_initialized( void ) const
Chris@16 223 {
Chris@101 224 return m_steps_initialized >= ( steps - 1 );
Chris@16 225 }
Chris@16 226
Chris@16 227 const initializing_stepper_type& initializing_stepper( void ) const { return m_initializing_stepper; }
Chris@16 228
Chris@16 229 initializing_stepper_type& initializing_stepper( void ) { return m_initializing_stepper; }
Chris@16 230
Chris@16 231 private:
Chris@16 232
Chris@16 233 template< class System , class StateIn , class StateOut >
Chris@16 234 void do_step_impl( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
Chris@16 235 {
Chris@16 236 typename odeint::unwrap_reference< System >::type &sys = system;
Chris@16 237 if( m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl<StateIn> , detail::ref( *this ) , detail::_1 ) ) )
Chris@16 238 {
Chris@16 239 m_steps_initialized = 0;
Chris@16 240 }
Chris@16 241
Chris@101 242 if( m_steps_initialized + 1 < steps )
Chris@16 243 {
Chris@16 244 if( m_steps_initialized != 0 ) m_step_storage.rotate();
Chris@16 245 sys( in , m_step_storage[0].m_v , t );
Chris@101 246 m_initializing_stepper.do_step_dxdt_impl(
Chris@101 247 system, in, m_step_storage[0].m_v, t, out, dt );
Chris@101 248 ++m_steps_initialized;
Chris@16 249 }
Chris@16 250 else
Chris@16 251 {
Chris@16 252 m_step_storage.rotate();
Chris@16 253 sys( in , m_step_storage[0].m_v , t );
Chris@101 254 detail::adams_bashforth_call_algebra< steps , algebra_type , operations_type >()( this->m_algebra , in , out , m_step_storage , m_coefficients , dt );
Chris@16 255 }
Chris@16 256 }
Chris@16 257
Chris@16 258
Chris@16 259 template< class StateIn >
Chris@16 260 bool resize_impl( const StateIn &x )
Chris@16 261 {
Chris@16 262 bool resized( false );
Chris@16 263 for( size_t i=0 ; i<steps ; ++i )
Chris@16 264 {
Chris@16 265 resized |= adjust_size_by_resizeability( m_step_storage[i] , x , typename is_resizeable<deriv_type>::type() );
Chris@16 266 }
Chris@16 267 return resized;
Chris@16 268 }
Chris@16 269
Chris@16 270 step_storage_type m_step_storage;
Chris@16 271 resizer_type m_resizer;
Chris@101 272 detail::adams_bashforth_coefficients< value_type , steps > m_coefficients;
Chris@16 273 size_t m_steps_initialized;
Chris@16 274 initializing_stepper_type m_initializing_stepper;
Chris@16 275
Chris@16 276 };
Chris@16 277
Chris@16 278
Chris@16 279 /***** DOXYGEN *****/
Chris@16 280
Chris@16 281 /**
Chris@16 282 * \class adams_bashforth
Chris@16 283 * \brief The Adams-Bashforth multistep algorithm.
Chris@16 284 *
Chris@16 285 * The Adams-Bashforth method is a multi-step algorithm with configurable step
Chris@16 286 * number. The step number is specified as template parameter Steps and it
Chris@16 287 * then uses the result from the previous Steps steps. See also
Chris@16 288 * <a href="http://en.wikipedia.org/wiki/Linear_multistep_method">en.wikipedia.org/wiki/Linear_multistep_method</a>.
Chris@16 289 * Currently, a maximum of Steps=8 is supported.
Chris@16 290 * The method is explicit and fulfills the Stepper concept. Step size control
Chris@16 291 * or continuous output are not provided.
Chris@16 292 *
Chris@16 293 * This class derives from algebra_base and inherits its interface via
Chris@16 294 * CRTP (current recurring template pattern). For more details see
Chris@16 295 * algebra_stepper_base.
Chris@16 296 *
Chris@16 297 * \tparam Steps The number of steps (maximal 8).
Chris@16 298 * \tparam State The state type.
Chris@16 299 * \tparam Value The value type.
Chris@16 300 * \tparam Deriv The type representing the time derivative of the state.
Chris@16 301 * \tparam Time The time representing the independent variable - the time.
Chris@16 302 * \tparam Algebra The algebra type.
Chris@16 303 * \tparam Operations The operations type.
Chris@16 304 * \tparam Resizer The resizer policy type.
Chris@16 305 * \tparam InitializingStepper The stepper for the first two steps.
Chris@16 306 */
Chris@16 307
Chris@16 308 /**
Chris@16 309 * \fn adams_bashforth::adams_bashforth( const algebra_type &algebra )
Chris@16 310 * \brief Constructs the adams_bashforth class. This constructor can be used as a default
Chris@16 311 * constructor if the algebra has a default constructor.
Chris@16 312 * \param algebra A copy of algebra is made and stored.
Chris@16 313 */
Chris@16 314
Chris@16 315 /**
Chris@16 316 * \fn order_type adams_bashforth::order( void ) const
Chris@16 317 * \brief Returns the order of the algorithm, which is equal to the number of steps.
Chris@16 318 * \return order of the method.
Chris@16 319 */
Chris@16 320
Chris@16 321 /**
Chris@16 322 * \fn void adams_bashforth::do_step( System system , StateInOut &x , time_type t , time_type dt )
Chris@16 323 * \brief This method performs one step. It transforms the result in-place.
Chris@16 324 *
Chris@16 325 * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
Chris@16 326 * Simple System concept.
Chris@16 327 * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
Chris@16 328 * \param t The value of the time, at which the step should be performed.
Chris@16 329 * \param dt The step size.
Chris@16 330 */
Chris@16 331
Chris@16 332 /**
Chris@16 333 * \fn void adams_bashforth::do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
Chris@16 334 * \brief The method performs one step with the stepper passed by Stepper. The state of the ODE is updated out-of-place.
Chris@16 335 *
Chris@16 336 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
Chris@16 337 * Simple System concept.
Chris@16 338 * \param in The state of the ODE which should be solved. in is not modified in this method
Chris@16 339 * \param t The value of the time, at which the step should be performed.
Chris@16 340 * \param out The result of the step is written in out.
Chris@16 341 * \param dt The step size.
Chris@16 342 */
Chris@16 343
Chris@16 344 /**
Chris@16 345 * \fn void adams_bashforth::adjust_size( const StateType &x )
Chris@16 346 * \brief Adjust the size of all temporaries in the stepper manually.
Chris@16 347 * \param x A state from which the size of the temporaries to be resized is deduced.
Chris@16 348 */
Chris@16 349
Chris@16 350
Chris@16 351 /**
Chris@16 352 * \fn const step_storage_type& adams_bashforth::step_storage( void ) const
Chris@16 353 * \brief Returns the storage of intermediate results.
Chris@16 354 * \return The storage of intermediate results.
Chris@16 355 */
Chris@16 356
Chris@16 357 /**
Chris@16 358 * \fn step_storage_type& adams_bashforth::step_storage( void )
Chris@16 359 * \brief Returns the storage of intermediate results.
Chris@16 360 * \return The storage of intermediate results.
Chris@16 361 */
Chris@16 362
Chris@16 363 /**
Chris@16 364 * \fn void adams_bashforth::initialize( ExplicitStepper explicit_stepper , System system , StateIn &x , time_type &t , time_type dt )
Chris@16 365 * \brief Initialized the stepper. Does Steps-1 steps with the explicit_stepper to fill the buffer.
Chris@16 366 * \param explicit_stepper the stepper used to fill the buffer of previous step results
Chris@16 367 * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
Chris@16 368 * Simple System concept.
Chris@16 369 * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
Chris@16 370 * \param t The value of the time, at which the step should be performed.
Chris@16 371 * \param dt The step size.
Chris@16 372 */
Chris@16 373
Chris@16 374 /**
Chris@16 375 * \fn void adams_bashforth::initialize( System system , StateIn &x , time_type &t , time_type dt )
Chris@16 376 * \brief Initialized the stepper. Does Steps-1 steps with an internal instance of InitializingStepper to fill the buffer.
Chris@16 377 * \note The state x and time t are updated to the values after Steps-1 initial steps.
Chris@16 378 * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
Chris@16 379 * Simple System concept.
Chris@16 380 * \param x The initial state of the ODE which should be solved, updated in this method.
Chris@16 381 * \param t The initial value of the time, updated in this method.
Chris@16 382 * \param dt The step size.
Chris@16 383 */
Chris@16 384
Chris@16 385 /**
Chris@16 386 * \fn void adams_bashforth::reset( void )
Chris@16 387 * \brief Resets the internal buffer of the stepper.
Chris@16 388 */
Chris@16 389
Chris@16 390 /**
Chris@16 391 * \fn bool adams_bashforth::is_initialized( void ) const
Chris@16 392 * \brief Returns true if the stepper has been initialized.
Chris@16 393 * \return bool true if stepper is initialized, false otherwise
Chris@16 394 */
Chris@16 395
Chris@16 396 /**
Chris@16 397 * \fn const initializing_stepper_type& adams_bashforth::initializing_stepper( void ) const
Chris@16 398 * \brief Returns the internal initializing stepper instance.
Chris@16 399 * \return initializing_stepper
Chris@16 400 */
Chris@16 401
Chris@16 402 /**
Chris@16 403 * \fn const initializing_stepper_type& adams_bashforth::initializing_stepper( void ) const
Chris@16 404 * \brief Returns the internal initializing stepper instance.
Chris@16 405 * \return initializing_stepper
Chris@16 406 */
Chris@16 407
Chris@16 408 /**
Chris@16 409 * \fn initializing_stepper_type& adams_bashforth::initializing_stepper( void )
Chris@16 410 * \brief Returns the internal initializing stepper instance.
Chris@16 411 * \return initializing_stepper
Chris@16 412 */
Chris@16 413
Chris@16 414 } // odeint
Chris@16 415 } // numeric
Chris@16 416 } // boost
Chris@16 417
Chris@16 418
Chris@16 419
Chris@16 420 #endif // BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_HPP_INCLUDED