Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 08:40:09

0001 /*
0002  [auto_generated]
0003  boost/numeric/odeint/stepper/adams_bashforth.hpp
0004 
0005  [begin_description]
0006  Implementaton of the Adam-Bashforth method a multistep method used for the predictor step in the
0007  Adams-Bashforth-Moulton method.
0008  [end_description]
0009 
0010  Copyright 2011-2013 Karsten Ahnert
0011  Copyright 2011-2013 Mario Mulansky
0012  Copyright 2012 Christoph Koke
0013  Copyright 2013 Pascal Germroth
0014 
0015  Distributed under the Boost Software License, Version 1.0.
0016  (See accompanying file LICENSE_1_0.txt or
0017  copy at http://www.boost.org/LICENSE_1_0.txt)
0018  */
0019 
0020 
0021 #ifndef BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_HPP_INCLUDED
0022 #define BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_HPP_INCLUDED
0023 
0024 #include <boost/static_assert.hpp>
0025 
0026 #include <boost/numeric/odeint/util/unwrap_reference.hpp>
0027 
0028 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
0029 #include <boost/numeric/odeint/algebra/default_operations.hpp>
0030 #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
0031 #include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
0032 
0033 #include <boost/numeric/odeint/util/state_wrapper.hpp>
0034 #include <boost/numeric/odeint/util/is_resizeable.hpp>
0035 #include <boost/numeric/odeint/util/resizer.hpp>
0036 
0037 #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
0038 #include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
0039 #include <boost/numeric/odeint/stepper/extrapolation_stepper.hpp>
0040 
0041 #include <boost/numeric/odeint/stepper/base/algebra_stepper_base.hpp>
0042 
0043 #include <boost/numeric/odeint/stepper/detail/adams_bashforth_coefficients.hpp>
0044 #include <boost/numeric/odeint/stepper/detail/adams_bashforth_call_algebra.hpp>
0045 #include <boost/numeric/odeint/stepper/detail/rotating_buffer.hpp>
0046 
0047 #include <boost/mpl/arithmetic.hpp>
0048 #include <boost/mpl/min_max.hpp>
0049 #include <boost/mpl/equal_to.hpp>
0050 
0051 namespace mpl = boost::mpl;
0052 
0053 
0054 namespace boost {
0055 namespace numeric {
0056 namespace odeint {
0057 
0058     using mpl::int_;
0059 
0060     /* if N >= 4, returns the smallest even number > N, otherwise returns 4 */
0061     template < int N >
0062     struct order_helper
0063         : mpl::max< typename mpl::eval_if<
0064                         mpl::equal_to< mpl::modulus< int_< N >, int_< 2 > >,
0065                                        int_< 0 > >,
0066                         int_< N >, int_< N + 1 > >::type,
0067                     int_< 4 > >::type
0068     { };
0069 
0070 template<
0071 size_t Steps ,
0072 class State ,
0073 class Value = double ,
0074 class Deriv = State ,
0075 class Time = Value ,
0076 class Algebra = typename algebra_dispatcher< State >::algebra_type ,
0077 class Operations = typename operations_dispatcher< State >::operations_type ,
0078 class Resizer = initially_resizer ,
0079 class InitializingStepper = extrapolation_stepper< order_helper<Steps>::value, 
0080                                                    State, Value, Deriv, Time,
0081                                                    Algebra, Operations, Resizer >
0082 >
0083 class adams_bashforth : public algebra_stepper_base< Algebra , Operations >
0084 {
0085 
0086 #ifndef DOXYGEN_SKIP
0087     static_assert(( Steps > 0 && Steps < 9 ), "Must have between 1 and 8 steps inclusive");
0088 #endif
0089 
0090 public :
0091 
0092     typedef State state_type;
0093     typedef state_wrapper< state_type > wrapped_state_type;
0094     typedef Value value_type;
0095     typedef Deriv deriv_type;
0096     typedef state_wrapper< deriv_type > wrapped_deriv_type;
0097     typedef Time time_type;
0098     typedef Resizer resizer_type;
0099     typedef stepper_tag stepper_category;
0100 
0101     typedef InitializingStepper initializing_stepper_type;
0102 
0103     typedef algebra_stepper_base< Algebra , Operations > algebra_stepper_base_type;
0104     typedef typename algebra_stepper_base_type::algebra_type algebra_type;
0105     typedef typename algebra_stepper_base_type::operations_type operations_type;
0106 #ifndef DOXYGEN_SKIP
0107     typedef adams_bashforth< Steps , State , Value , Deriv , Time , Algebra , Operations , Resizer , InitializingStepper > stepper_type;
0108 #endif
0109     static const size_t steps = Steps;
0110 
0111 
0112 
0113     typedef unsigned short order_type;
0114     static const order_type order_value = steps;
0115 
0116     typedef detail::rotating_buffer< wrapped_deriv_type , steps > step_storage_type;
0117 
0118 
0119     
0120     order_type order( void ) const { return order_value; }
0121 
0122     adams_bashforth( const algebra_type &algebra = algebra_type() )
0123     : algebra_stepper_base_type( algebra ) ,
0124       m_step_storage() , m_resizer() , m_coefficients() ,
0125       m_steps_initialized( 0 ) , m_initializing_stepper()
0126     { }
0127 
0128 
0129 
0130     /*
0131      * Version 1 : do_step( system , x , t , dt );
0132      *
0133      * solves the forwarding problem
0134      */
0135     template< class System , class StateInOut >
0136     void do_step( System system , StateInOut &x , time_type t , time_type dt )
0137     {
0138         do_step( system , x , t , x , dt );
0139     }
0140 
0141     /**
0142      * \brief Second version to solve the forwarding problem, can be called with Boost.Range as StateInOut.
0143      */
0144     template< class System , class StateInOut >
0145     void do_step( System system , const StateInOut &x , time_type t , time_type dt )
0146     {
0147         do_step( system , x , t , x , dt );
0148     }
0149 
0150 
0151 
0152     /*
0153      * Version 2 : do_step( system , in , t , out , dt );
0154      *
0155      * solves the forwarding problem
0156      */
0157 
0158     template< class System , class StateIn , class StateOut >
0159     void do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
0160     {
0161         do_step_impl( system , in , t , out , dt );
0162     }
0163 
0164     /**
0165      * \brief Second version to solve the forwarding problem, can be called with Boost.Range as StateOut.
0166      */
0167     template< class System , class StateIn , class StateOut >
0168     void do_step( System system , const StateIn &in , time_type t , const StateOut &out , time_type dt )
0169     {
0170         do_step_impl( system , in , t , out , dt );
0171     }
0172 
0173 
0174     template< class StateType >
0175     void adjust_size( const StateType &x )
0176     {
0177         resize_impl( x );
0178     }
0179 
0180     const step_storage_type& step_storage( void ) const
0181     {
0182         return m_step_storage;
0183     }
0184 
0185     step_storage_type& step_storage( void )
0186     {
0187         return m_step_storage;
0188     }
0189 
0190     template< class ExplicitStepper , class System , class StateIn >
0191     void initialize( ExplicitStepper explicit_stepper , System system , StateIn &x , time_type &t , time_type dt )
0192     {
0193         typename odeint::unwrap_reference< ExplicitStepper >::type &stepper = explicit_stepper;
0194         typename odeint::unwrap_reference< System >::type &sys = system;
0195 
0196         m_resizer.adjust_size(x, [this](auto&& arg) { return this->resize_impl<StateIn>(std::forward<decltype(arg)>(arg)); });
0197 
0198         for( size_t i=0 ; i+1<steps ; ++i )
0199         {
0200             if( i != 0 ) m_step_storage.rotate();
0201             sys( x , m_step_storage[0].m_v , t );
0202             stepper.do_step_dxdt_impl( system, x, m_step_storage[0].m_v, t,
0203                                        dt );
0204             t += dt;
0205         }
0206         m_steps_initialized = steps;
0207     }
0208 
0209     template< class System , class StateIn >
0210     void initialize( System system , StateIn &x , time_type &t , time_type dt )
0211     {
0212         initialize( std::ref( m_initializing_stepper ) , system , x , t , dt );
0213     }
0214 
0215     void reset( void )
0216     {
0217         m_steps_initialized = 0;
0218     }
0219 
0220     bool is_initialized( void ) const
0221     {
0222         return m_steps_initialized >= ( steps - 1 );
0223     }
0224 
0225     const initializing_stepper_type& initializing_stepper( void ) const { return m_initializing_stepper; }
0226 
0227     initializing_stepper_type& initializing_stepper( void ) { return m_initializing_stepper; }
0228 
0229 private:
0230 
0231     template< class System , class StateIn , class StateOut >
0232     void do_step_impl( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
0233     {
0234         typename odeint::unwrap_reference< System >::type &sys = system;
0235         if( m_resizer.adjust_size(in, [this](auto&& arg) { return this->resize_impl<StateIn>(std::forward<decltype(arg)>(arg)); } ) )
0236         {
0237             m_steps_initialized = 0;
0238         }
0239 
0240         if( m_steps_initialized + 1 < steps )
0241         {
0242             if( m_steps_initialized != 0 ) m_step_storage.rotate();
0243             sys( in , m_step_storage[0].m_v , t );
0244             m_initializing_stepper.do_step_dxdt_impl(
0245                 system, in, m_step_storage[0].m_v, t, out, dt );
0246             ++m_steps_initialized;
0247         }
0248         else
0249         {
0250             m_step_storage.rotate();
0251             sys( in , m_step_storage[0].m_v , t );
0252             detail::adams_bashforth_call_algebra< steps , algebra_type , operations_type >()( this->m_algebra , in , out , m_step_storage , m_coefficients , dt );
0253         }
0254     }
0255 
0256 
0257     template< class StateIn >
0258     bool resize_impl( const StateIn &x )
0259     {
0260         bool resized( false );
0261         for( size_t i=0 ; i<steps ; ++i )
0262         {
0263             resized |= adjust_size_by_resizeability( m_step_storage[i] , x , typename is_resizeable<deriv_type>::type() );
0264         }
0265         return resized;
0266     }
0267 
0268     step_storage_type m_step_storage;
0269     resizer_type m_resizer;
0270     detail::adams_bashforth_coefficients< value_type , steps > m_coefficients;
0271     size_t m_steps_initialized;
0272     initializing_stepper_type m_initializing_stepper;
0273 
0274 };
0275 
0276 
0277 /***** DOXYGEN *****/
0278 
0279 /**
0280  * \class adams_bashforth
0281  * \brief The Adams-Bashforth multistep algorithm.
0282  *
0283  * The Adams-Bashforth method is a multi-step algorithm with configurable step
0284  * number. The step number is specified as template parameter Steps and it 
0285  * then uses the result from the previous Steps steps. See also
0286  * <a href="http://en.wikipedia.org/wiki/Linear_multistep_method">en.wikipedia.org/wiki/Linear_multistep_method</a>.
0287  * Currently, a maximum of Steps=8 is supported.
0288  * The method is explicit and fulfills the Stepper concept. Step size control
0289  * or continuous output are not provided.
0290  * 
0291  * This class derives from algebra_base and inherits its interface via
0292  * CRTP (current recurring template pattern). For more details see
0293  * algebra_stepper_base.
0294  *
0295  * \tparam Steps The number of steps (maximal 8).
0296  * \tparam State The state type.
0297  * \tparam Value The value type.
0298  * \tparam Deriv The type representing the time derivative of the state.
0299  * \tparam Time The time representing the independent variable - the time.
0300  * \tparam Algebra The algebra type.
0301  * \tparam Operations The operations type.
0302  * \tparam Resizer The resizer policy type.
0303  * \tparam InitializingStepper The stepper for the first two steps.
0304  */
0305 
0306     /**
0307      * \fn adams_bashforth::adams_bashforth( const algebra_type &algebra )
0308      * \brief Constructs the adams_bashforth class. This constructor can be used as a default
0309      * constructor if the algebra has a default constructor. 
0310      * \param algebra A copy of algebra is made and stored.
0311      */
0312 
0313     /**
0314      * \fn order_type adams_bashforth::order( void ) const
0315      * \brief Returns the order of the algorithm, which is equal to the number of steps.
0316      * \return order of the method.
0317      */
0318 
0319     /**
0320      * \fn void adams_bashforth::do_step( System system , StateInOut &x , time_type t , time_type dt )
0321      * \brief This method performs one step. It transforms the result in-place.
0322      *
0323      * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
0324      *               Simple System concept.
0325      * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
0326      * \param t The value of the time, at which the step should be performed.
0327      * \param dt The step size.
0328      */
0329 
0330     /**
0331      * \fn void adams_bashforth::do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
0332      * \brief The method performs one step with the stepper passed by Stepper. The state of the ODE is updated out-of-place.
0333      *
0334      * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
0335      *               Simple System concept.
0336      * \param in The state of the ODE which should be solved. in is not modified in this method
0337      * \param t The value of the time, at which the step should be performed.
0338      * \param out The result of the step is written in out.
0339      * \param dt The step size.
0340      */
0341 
0342     /**
0343      * \fn void adams_bashforth::adjust_size( const StateType &x )
0344      * \brief Adjust the size of all temporaries in the stepper manually.
0345      * \param x A state from which the size of the temporaries to be resized is deduced.
0346      */
0347 
0348 
0349     /**
0350      * \fn const step_storage_type& adams_bashforth::step_storage( void ) const
0351      * \brief Returns the storage of intermediate results.
0352      * \return The storage of intermediate results.
0353      */
0354 
0355     /**
0356      * \fn step_storage_type& adams_bashforth::step_storage( void )
0357      * \brief Returns the storage of intermediate results.
0358      * \return The storage of intermediate results.
0359      */
0360 
0361     /**
0362      * \fn void adams_bashforth::initialize( ExplicitStepper explicit_stepper , System system , StateIn &x , time_type &t , time_type dt )
0363      * \brief Initialized the stepper. Does Steps-1 steps with the explicit_stepper to fill the buffer.
0364      * \param explicit_stepper the stepper used to fill the buffer of previous step results
0365      * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
0366      *               Simple System concept.
0367      * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
0368      * \param t The value of the time, at which the step should be performed.
0369      * \param dt The step size.
0370      */
0371 
0372     /**
0373      * \fn void adams_bashforth::initialize( System system , StateIn &x , time_type &t , time_type dt )
0374      * \brief Initialized the stepper. Does Steps-1 steps with an internal instance of InitializingStepper to fill the buffer.
0375      * \note The state x and time t are updated to the values after Steps-1 initial steps.
0376      * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
0377      *               Simple System concept.
0378      * \param x The initial state of the ODE which should be solved, updated in this method.
0379      * \param t The initial value of the time, updated in this method.
0380      * \param dt The step size.
0381      */
0382 
0383     /**
0384      * \fn void adams_bashforth::reset( void )
0385      * \brief Resets the internal buffer of the stepper.
0386      */
0387 
0388     /**
0389      * \fn bool adams_bashforth::is_initialized( void ) const
0390      * \brief Returns true if the stepper has been initialized.
0391      * \return bool true if stepper is initialized, false otherwise
0392      */
0393 
0394     /**
0395      * \fn const initializing_stepper_type& adams_bashforth::initializing_stepper( void ) const
0396      * \brief Returns the internal initializing stepper instance.
0397      * \return initializing_stepper
0398      */
0399 
0400     /**
0401      * \fn const initializing_stepper_type& adams_bashforth::initializing_stepper( void ) const
0402      * \brief Returns the internal initializing stepper instance.
0403      * \return initializing_stepper
0404      */
0405 
0406     /**
0407      * \fn initializing_stepper_type& adams_bashforth::initializing_stepper( void )
0408      * \brief Returns the internal initializing stepper instance.
0409      * \return initializing_stepper
0410      */
0411 
0412 } // odeint
0413 } // numeric
0414 } // boost
0415 
0416 
0417 
0418 #endif // BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_HPP_INCLUDED