Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:42:54

0001 /*
0002  [auto_generated]
0003  boost/numeric/odeint/stepper/base/explicit_stepper_base.hpp
0004 
0005  [begin_description]
0006  Base class for all explicit Runge Kutta steppers.
0007  [end_description]
0008 
0009  Copyright 2010-2013 Karsten Ahnert
0010  Copyright 2010-2012 Mario Mulansky
0011  Copyright 2012 Christoph Koke
0012 
0013  Distributed under the Boost Software License, Version 1.0.
0014  (See accompanying file LICENSE_1_0.txt or
0015  copy at http://www.boost.org/LICENSE_1_0.txt)
0016  */
0017 
0018 
0019 #ifndef BOOST_NUMERIC_ODEINT_STEPPER_BASE_EXPLICIT_STEPPER_BASE_HPP_INCLUDED
0020 #define BOOST_NUMERIC_ODEINT_STEPPER_BASE_EXPLICIT_STEPPER_BASE_HPP_INCLUDED
0021 
0022 
0023 #include <boost/utility/enable_if.hpp>
0024 #include <boost/type_traits/is_same.hpp>
0025 
0026 #include <boost/numeric/odeint/util/bind.hpp>
0027 #include <boost/numeric/odeint/util/unwrap_reference.hpp>
0028 
0029 #include <boost/numeric/odeint/util/state_wrapper.hpp>
0030 #include <boost/numeric/odeint/util/resizer.hpp>
0031 #include <boost/numeric/odeint/util/is_resizeable.hpp>
0032 
0033 #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
0034 
0035 #include <boost/numeric/odeint/stepper/base/algebra_stepper_base.hpp>
0036 
0037 namespace boost {
0038 namespace numeric {
0039 namespace odeint {
0040 
0041 /*
0042  * base class for explicit steppers
0043  * models the stepper concept
0044  *
0045  * this class provides the following overloads
0046     * do_step( sys , x , t , dt )
0047     * do_step( sys , in , t , out , dt )
0048     * do_step( sys , x , dxdt_in , t , dt )
0049     * do_step( sys , in , dxdt_in , t , out , dt )
0050  */
0051 
0052 template<
0053 class Stepper ,
0054 unsigned short Order ,
0055 class State ,
0056 class Value ,
0057 class Deriv ,
0058 class Time ,
0059 class Algebra ,
0060 class Operations ,
0061 class Resizer
0062 >
0063 class explicit_stepper_base : public algebra_stepper_base< Algebra , Operations >
0064 {
0065 public:
0066 
0067     #ifndef DOXYGEN_SKIP
0068     typedef explicit_stepper_base< Stepper , Order , State , Value , Deriv , Time , Algebra , Operations , Resizer > internal_stepper_base_type;
0069     #endif // DOXYGEN_SKIP
0070 
0071 
0072     typedef State state_type;
0073     typedef Value value_type;
0074     typedef Deriv deriv_type;
0075     typedef Time time_type;
0076     typedef Resizer resizer_type;
0077     typedef Stepper stepper_type;
0078     typedef stepper_tag stepper_category;
0079     typedef algebra_stepper_base< Algebra , Operations > algebra_stepper_base_type;
0080     typedef typename algebra_stepper_base_type::algebra_type algebra_type;
0081     typedef typename algebra_stepper_base_type::operations_type operations_type;
0082     typedef unsigned short order_type;
0083 
0084     #ifndef DOXYGEN_SKIP
0085     typedef state_wrapper< state_type > wrapped_state_type;
0086     typedef state_wrapper< deriv_type > wrapped_deriv_type;
0087     #endif // DOXYGEN_SKIP
0088 
0089 
0090     static const order_type order_value = Order;
0091 
0092 
0093     explicit_stepper_base( const algebra_type &algebra = algebra_type() )
0094     : algebra_stepper_base_type( algebra )
0095     { }
0096 
0097     /**
0098      * \return Returns the order of the stepper.
0099      */
0100     order_type order( void ) const
0101     {
0102         return order_value;
0103     }
0104 
0105 
0106     /*
0107      * Version 1 : do_step( sys , x , t , dt )
0108      *
0109      * the two overloads are needed in order to solve the forwarding problem
0110      */
0111     template< class System , class StateInOut >
0112     void do_step( System system , StateInOut &x , time_type t , time_type dt )
0113     {
0114         do_step_v1( system , x , t , dt );
0115     }
0116 
0117     /**
0118      * \brief Second version to solve the forwarding problem, can be called with Boost.Range as StateInOut.
0119      */
0120     template< class System , class StateInOut >
0121     void do_step( System system , const StateInOut &x , time_type t , time_type dt )
0122     {
0123         do_step_v1( system , x , t , dt );
0124     }
0125 
0126     /*
0127      * Version 2 : do_step( sys , x , dxdt , t , dt )
0128      *
0129       * this version does not solve the forwarding problem, boost.range can not be used
0130      *
0131      * the disable is needed to avoid ambiguous overloads if state_type = time_type
0132      */
0133     template< class System , class StateInOut , class DerivIn >
0134     typename boost::disable_if< boost::is_same< DerivIn , time_type > , void >::type
0135     do_step( System system , StateInOut &x , const DerivIn &dxdt , time_type t , time_type dt )
0136     {
0137         this->stepper().do_step_impl( system , x , dxdt , t , x , dt );
0138     }
0139 
0140 
0141     /*
0142      * named Version 2: do_step_dxdt_impl( sys , in , dxdt , t , dt )
0143      *
0144      * this version is needed when this stepper is used for initializing 
0145      * multistep stepper like adams-bashforth. Hence we provide an explicitely
0146      * named version that is not disabled. Meant for internal use only.
0147      */
0148     template < class System, class StateInOut, class DerivIn >
0149     void do_step_dxdt_impl( System system, StateInOut &x, const DerivIn &dxdt,
0150                             time_type t, time_type dt )
0151     {
0152         this->stepper().do_step_impl( system , x , dxdt , t , x , dt );
0153     }
0154 
0155 
0156     /*
0157      * Version 3 : do_step( sys , in , t , out , dt )
0158      *
0159      * this version does not solve the forwarding problem, boost.range can not be used
0160      */
0161     template< class System , class StateIn , class StateOut >
0162     void do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
0163     {
0164         typename odeint::unwrap_reference< System >::type &sys = system;
0165         m_resizer.adjust_size( in , detail::bind( &internal_stepper_base_type::template resize_impl<StateIn> , detail::ref( *this ) , detail::_1 ) );
0166         sys( in , m_dxdt.m_v ,t );
0167         this->stepper().do_step_impl( system , in , m_dxdt.m_v , t , out , dt );
0168     }
0169 
0170 
0171     /*
0172      * Version 4 : do_step( sys , in , dxdt , t , out , dt )
0173      *
0174      * this version does not solve the forwarding problem, boost.range can not be used
0175      */
0176     template< class System , class StateIn , class DerivIn , class StateOut >
0177     void do_step( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
0178     {
0179         this->stepper().do_step_impl( system , in , dxdt , t , out , dt );
0180     }
0181 
0182 
0183     /*
0184      * named Version 4: do_step_dxdt_impl( sys , in , dxdt , t , out, dt )
0185      *
0186      * this version is needed when this stepper is used for initializing 
0187      * multistep stepper like adams-bashforth. Hence we provide an explicitely
0188      * named version. Meant for internal use only.
0189      */
0190     template < class System, class StateIn, class DerivIn, class StateOut >
0191     void do_step_dxdt_impl( System system, const StateIn &in,
0192                             const DerivIn &dxdt, time_type t, StateOut &out,
0193                             time_type dt )
0194     {
0195         this->stepper().do_step_impl( system , in , dxdt , t , out , dt );
0196     }
0197 
0198     template< class StateIn >
0199     void adjust_size( const StateIn &x )
0200     {
0201         resize_impl( x );
0202     }
0203 
0204 private:
0205 
0206     stepper_type& stepper( void )
0207     {
0208         return *static_cast< stepper_type* >( this );
0209     }
0210 
0211     const stepper_type& stepper( void ) const
0212     {
0213         return *static_cast< const stepper_type* >( this );
0214     }
0215 
0216 
0217     template< class StateIn >
0218     bool resize_impl( const StateIn &x )
0219     {
0220         return adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable<deriv_type>::type() );
0221     }
0222 
0223 
0224     template< class System , class StateInOut >
0225     void do_step_v1( System system , StateInOut &x , time_type t , time_type dt )
0226     {
0227         typename odeint::unwrap_reference< System >::type &sys = system;
0228         m_resizer.adjust_size( x , detail::bind( &internal_stepper_base_type::template resize_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
0229         sys( x , m_dxdt.m_v ,t );
0230         this->stepper().do_step_impl( system , x , m_dxdt.m_v , t , x , dt );
0231     }
0232 
0233 
0234     resizer_type m_resizer;
0235 
0236 protected:
0237 
0238     wrapped_deriv_type m_dxdt;
0239 };
0240 
0241 
0242 /******* DOXYGEN *********/
0243 
0244 /**
0245  * \class explicit_stepper_base
0246  * \brief Base class for explicit steppers without step size control and without dense output.
0247  *
0248  * This class serves as the base class for all explicit steppers with algebra and operations.
0249  * Step size control and error estimation as well as dense output are not provided. explicit_stepper_base 
0250  * is used as the interface in a CRTP (currently recurring template pattern). In order to work 
0251  * correctly the parent class needs to have a method `do_step_impl( system , in , dxdt_in , t , out , dt )`. 
0252  * This is method is used by explicit_stepper_base. explicit_stepper_base derives from
0253  * algebra_stepper_base. An example how this class can be used is
0254  *
0255  * \code
0256  * template< class State , class Value , class Deriv , class Time , class Algebra , class Operations , class Resizer >
0257  * class custom_euler : public explicit_stepper_base< 1 , State , Value , Deriv , Time , Algebra , Operations , Resizer >
0258  * {
0259  *  public:
0260  *     
0261  *     typedef explicit_stepper_base< 1 , State , Value , Deriv , Time , Algebra , Operations , Resizer > base_type;
0262  *
0263  *     custom_euler( const Algebra &algebra = Algebra() ) { }
0264  * 
0265  *     template< class Sys , class StateIn , class DerivIn , class StateOut >
0266  *     void do_step_impl( Sys sys , const StateIn &in , const DerivIn &dxdt , Time t , StateOut &out , Time dt )
0267  *     {
0268  *         m_algebra.for_each3( out , in , dxdt , Operations::scale_sum2< Value , Time >( 1.0 , dt );
0269  *     }
0270  *
0271  *     template< class State >
0272  *     void adjust_size( const State &x )
0273  *     {
0274  *         base_type::adjust_size( x );
0275  *     }
0276  * };
0277  * \endcode
0278  *
0279  * For the Stepper concept only the `do_step( sys , x , t , dt )` needs to be implemented. But this class
0280  * provides additional `do_step` variants since the stepper is explicit. These methods can be used to increase
0281  * the performance in some situation, for example if one needs to analyze `dxdt` during each step. In this case 
0282  * one can use 
0283  *
0284  * \code
0285  * sys( x , dxdt , t );
0286  * stepper.do_step( sys , x , dxdt , t , dt );  // the value of dxdt is used here
0287  * t += dt;
0288  * \endcode
0289  *
0290  * In detail explicit_stepper_base provides the following `do_step` variants
0291  *   - `do_step( sys , x , t , dt )` - The classical `do_step` method needed to fulfill the Stepper concept. The state is updated in-place.
0292  *      A type modelling a Boost.Range can be used for x.
0293  *   - `do_step( sys , in , t , out , dt )` - This method updates the state out-of-place, hence the result of the step is stored in `out`.
0294  *   - `do_step( sys , x , dxdt , t , dt )` - This method updates the state in-place, but the derivative at the point `t` must be
0295  *      explicitly passed in `dxdt`. For an example see the code snippet above.
0296  *   - `do_step( sys , in , dxdt , t , out , dt )` - This method update the state out-of-place and expects that the derivative at the point 
0297  *     `t` is explicitly passed in `dxdt`. It is a combination of the two `do_step` methods above.
0298  *
0299  * \note The system is always passed as value, which might result in poor performance if it contains data. In this case it can be used with `boost::ref`
0300  * or `std::ref`, for example `stepper.do_step( boost::ref( sys ) , x , t , dt );`
0301  *
0302  * \note The time `t` is not advanced by the stepper. This has to done manually, or by the appropriate `integrate` routines or `iterator`s.
0303  *
0304  * \tparam Stepper The stepper on which this class should work. It is used via CRTP, hence explicit_stepper_base
0305  * provides the interface for the Stepper.
0306  * \tparam Order The order of the stepper.
0307  * \tparam State The state type for the stepper.
0308  * \tparam Value The value type for the stepper. This should be a floating point type, like float,
0309  * double, or a multiprecision type. It must not necessary be the value_type of the State. For example
0310  * the State can be a `vector< complex< double > >` in this case the Value must be double.
0311  * The default value is double.
0312  * \tparam Deriv The type representing time derivatives of the state type. It is usually the same type as the
0313  * state type, only if used with Boost.Units both types differ.
0314  * \tparam Time The type representing the time. Usually the same type as the value type. When Boost.Units is
0315  * used, this type has usually a unit.
0316  * \tparam Algebra The algebra type which must fulfill the Algebra Concept.
0317  * \tparam Operations The type for the operations which must fulfill the Operations Concept.
0318  * \tparam Resizer The resizer policy class.
0319  */
0320 
0321 
0322     /**
0323      * \fn explicit_stepper_base::explicit_stepper_base( const algebra_type &algebra )
0324      * \brief Constructs a explicit_stepper_base class. This constructor can be used as a default
0325      * constructor if the algebra has a default constructor.
0326      * \param algebra A copy of algebra is made and stored inside explicit_stepper_base.
0327      */
0328 
0329     /**
0330      * \fn explicit_stepper_base::order_type order( void ) const
0331      * \return Returns the order of the stepper.
0332      */
0333 
0334     /**
0335      * \fn explicit_stepper_base::do_step( System system , StateInOut &x , time_type t , time_type dt )
0336      * \brief This method performs one step. It transforms the result in-place.
0337      *
0338      * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
0339      *               Simple System concept.
0340      * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
0341      * \param t The value of the time, at which the step should be performed.
0342      * \param dt The step size.
0343      */
0344 
0345 
0346     /**
0347      * \fn explicit_stepper_base::do_step( System system , StateInOut &x , const DerivIn &dxdt , time_type t , time_type dt )
0348 
0349      * \brief The method performs one step. Additionally to the other method
0350      * the derivative of x is also passed to this method. It is supposed to be used in the following way:
0351      *
0352      * \code
0353      * sys( x , dxdt , t );
0354      * stepper.do_step( sys , x , dxdt , t , dt );
0355      * \endcode
0356      *
0357      * The result is updated in place in x. This method is disabled if Time and Deriv are of the same type. In this
0358      * case the method could not be distinguished from other `do_step` versions.
0359      * 
0360      * \note This method does not solve the forwarding problem.
0361      *
0362      * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
0363      *               Simple System concept.
0364      * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
0365      * \param dxdt The derivative of x at t.
0366      * \param t The value of the time, at which the step should be performed.
0367      * \param dt The step size.
0368      */
0369 
0370     /**
0371      * \fn void explicit_stepper_base::do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
0372      * \brief The method performs one step. The state of the ODE is updated out-of-place.
0373      * \note This method does not solve the forwarding problem.
0374      *
0375      * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
0376      *               Simple System concept.
0377      * \param in The state of the ODE which should be solved. in is not modified in this method
0378      * \param t The value of the time, at which the step should be performed.
0379      * \param out The result of the step is written in out.
0380      * \param dt The step size.
0381      */
0382 
0383     /**
0384      * \fn void explicit_stepper_base::do_step( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
0385      * \brief The method performs one step. The state of the ODE is updated out-of-place.
0386      * Furthermore, the derivative of x at t is passed to the stepper. 
0387      * It is supposed to be used in the following way:
0388      *
0389      * \code
0390      * sys( in , dxdt , t );
0391      * stepper.do_step( sys , in , dxdt , t , out , dt );
0392      * \endcode
0393      *
0394      * \note This method does not solve the forwarding problem.
0395      *
0396      * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
0397      *               Simple System concept.
0398      * \param in The state of the ODE which should be solved. in is not modified in this method
0399      * \param dxdt The derivative of x at t.
0400      * \param t The value of the time, at which the step should be performed.
0401      * \param out The result of the step is written in out.
0402      * \param dt The step size.
0403      */
0404 
0405     /**
0406      * \fn void explicit_stepper_base::adjust_size( const StateIn &x )
0407      * \brief Adjust the size of all temporaries in the stepper manually.
0408      * \param x A state from which the size of the temporaries to be resized is deduced.
0409      */
0410 
0411 } // odeint
0412 } // numeric
0413 } // boost
0414 
0415 #endif // BOOST_NUMERIC_ODEINT_STEPPER_BASE_EXPLICIT_STEPPER_BASE_HPP_INCLUDED