Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /*
0002  [auto_generated]
0003  boost/numeric/odeint/stepper/runge_kutta4_classic.hpp
0004 
0005  [begin_description]
0006  Implementation for the classical Runge Kutta stepper.
0007  [end_description]
0008 
0009  Copyright 2010-2013 Karsten Ahnert
0010  Copyright 2010-2013 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_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED
0020 #define BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED
0021 
0022 
0023 
0024 #include <boost/numeric/odeint/stepper/base/explicit_stepper_base.hpp>
0025 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
0026 #include <boost/numeric/odeint/algebra/default_operations.hpp>
0027 #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
0028 #include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
0029 
0030 #include <boost/numeric/odeint/util/state_wrapper.hpp>
0031 #include <boost/numeric/odeint/util/is_resizeable.hpp>
0032 #include <boost/numeric/odeint/util/resizer.hpp>
0033 
0034 namespace boost {
0035 namespace numeric {
0036 namespace odeint {
0037 
0038 template<
0039 class State ,
0040 class Value = double ,
0041 class Deriv = State ,
0042 class Time = Value ,
0043 class Algebra = typename algebra_dispatcher< State >::algebra_type ,
0044 class Operations = typename operations_dispatcher< State >::operations_type ,
0045 class Resizer = initially_resizer
0046 >
0047 #ifndef DOXYGEN_SKIP
0048 class runge_kutta4_classic
0049 : public explicit_stepper_base<
0050   runge_kutta4_classic< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
0051   4 , State , Value , Deriv , Time , Algebra , Operations , Resizer >
0052 #else
0053 class runge_kutta4_classic : public explicit_stepper_base
0054 #endif
0055 {
0056 
0057 public :
0058 
0059     #ifndef DOXYGEN_SKIP
0060     typedef explicit_stepper_base<
0061     runge_kutta4_classic< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
0062     4 , State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_base_type;
0063     #else
0064     typedef explicit_stepper_base< runge_kutta4_classic< ... > , ... > stepper_base_type;
0065     #endif
0066 
0067     typedef typename stepper_base_type::state_type state_type;
0068     typedef typename stepper_base_type::value_type value_type;
0069     typedef typename stepper_base_type::deriv_type deriv_type;
0070     typedef typename stepper_base_type::time_type time_type;
0071     typedef typename stepper_base_type::algebra_type algebra_type;
0072     typedef typename stepper_base_type::operations_type operations_type;
0073     typedef typename stepper_base_type::resizer_type resizer_type;
0074 
0075     #ifndef DOXYGEN_SKIP
0076     typedef typename stepper_base_type::stepper_type stepper_type;
0077     typedef typename stepper_base_type::wrapped_state_type wrapped_state_type;
0078     typedef typename stepper_base_type::wrapped_deriv_type wrapped_deriv_type;
0079     #endif // DOXYGEN_SKIP
0080 
0081 
0082 
0083     runge_kutta4_classic( const algebra_type &algebra = algebra_type() ) : stepper_base_type( algebra )
0084     { }
0085 
0086 
0087     template< class System , class StateIn , class DerivIn , class StateOut >
0088     void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
0089     {
0090         // ToDo : check if size of in,dxdt,out are equal?
0091 
0092         static const value_type val1 = static_cast< value_type >( 1 );
0093 
0094         m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
0095 
0096         typename odeint::unwrap_reference< System >::type &sys = system;
0097 
0098         const time_type dh = dt / static_cast< value_type >( 2 );
0099         const time_type th = t + dh;
0100 
0101         // dt * dxdt = k1
0102         // m_x_tmp = x + dh*dxdt
0103         stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , dxdt ,
0104                 typename operations_type::template scale_sum2< value_type , time_type >( val1 , dh ) );
0105 
0106 
0107         // dt * m_dxt = k2
0108         sys( m_x_tmp.m_v , m_dxt.m_v , th );
0109 
0110         // m_x_tmp = x + dh*m_dxt
0111         stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , m_dxt.m_v ,
0112                 typename operations_type::template scale_sum2< value_type , time_type >( val1 , dh ) );
0113 
0114 
0115         // dt * m_dxm = k3
0116         sys( m_x_tmp.m_v , m_dxm.m_v , th );
0117         //m_x_tmp = x + dt*m_dxm
0118         stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , m_dxm.m_v ,
0119                 typename operations_type::template scale_sum2< value_type , time_type >( val1 , dt ) );
0120 
0121 
0122         // dt * m_dxh = k4
0123         sys( m_x_tmp.m_v , m_dxh.m_v , t + dt );
0124 
0125         //x += dt/6 * ( m_dxdt + m_dxt + val2*m_dxm )
0126         time_type dt6 = dt / static_cast< value_type >( 6 );
0127         time_type dt3 = dt / static_cast< value_type >( 3 );
0128         stepper_base_type::m_algebra.for_each6( out , in , dxdt , m_dxt.m_v , m_dxm.m_v , m_dxh.m_v ,
0129                                              typename operations_type::template scale_sum5< value_type , time_type , time_type , time_type , time_type >( 1.0 , dt6 , dt3 , dt3 , dt6 ) );
0130         
0131         // x += dt/6 * m_dxdt + dt/3 * m_dxt )
0132         // stepper_base_type::m_algebra.for_each4( out , in , dxdt , m_dxt.m_v , 
0133         //                                         typename operations_type::template scale_sum3< value_type , time_type , time_type >( 1.0 , dt6 , dt3 ) ); 
0134         // // x += dt/3 * m_dxm + dt/6 * m_dxh )
0135         // stepper_base_type::m_algebra.for_each4( out , out , m_dxm.m_v , m_dxh.m_v , 
0136         //                                         typename operations_type::template scale_sum3< value_type , time_type , time_type >( 1.0 , dt3 , dt6 ) ); 
0137 
0138     }
0139 
0140     template< class StateType >
0141     void adjust_size( const StateType &x )
0142     {
0143         resize_impl( x );
0144         stepper_base_type::adjust_size( x );
0145     }
0146 
0147 private:
0148 
0149     template< class StateIn >
0150     bool resize_impl( const StateIn &x )
0151     {
0152         bool resized = false;
0153         resized |= adjust_size_by_resizeability( m_x_tmp , x , typename is_resizeable<state_type>::type() );
0154         resized |= adjust_size_by_resizeability( m_dxm , x , typename is_resizeable<deriv_type>::type() );
0155         resized |= adjust_size_by_resizeability( m_dxt , x , typename is_resizeable<deriv_type>::type() );
0156         resized |= adjust_size_by_resizeability( m_dxh , x , typename is_resizeable<deriv_type>::type() );
0157         return resized;
0158     }
0159 
0160 
0161     resizer_type m_resizer;
0162 
0163     wrapped_deriv_type m_dxt;
0164     wrapped_deriv_type m_dxm;
0165     wrapped_deriv_type m_dxh;
0166     wrapped_state_type m_x_tmp;
0167 
0168 };
0169 
0170 
0171 /********* DOXYGEN *********/
0172 
0173 /**
0174  * \class runge_kutta4_classic
0175  * \brief The classical Runge-Kutta stepper of fourth order.
0176  *
0177  * The Runge-Kutta method of fourth order is one standard method for
0178  * solving ordinary differential equations and is widely used, see also
0179  * <a href="http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods">en.wikipedia.org/wiki/Runge-Kutta_methods</a>
0180  * The method is explicit and fulfills the Stepper concept. Step size control
0181  * or continuous output are not provided.  This class implements the method directly, hence the 
0182  * generic Runge-Kutta algorithm is not used.
0183  * 
0184  * This class derives from explicit_stepper_base and inherits its interface via
0185  * CRTP (current recurring template pattern). For more details see
0186  * explicit_stepper_base.
0187  *
0188  * \tparam State The state type.
0189  * \tparam Value The value type.
0190  * \tparam Deriv The type representing the time derivative of the state.
0191  * \tparam Time The time representing the independent variable - the time.
0192  * \tparam Algebra The algebra type.
0193  * \tparam Operations The operations type.
0194  * \tparam Resizer The resizer policy type.
0195  */
0196 
0197     /**
0198      * \fn runge_kutta4_classic::runge_kutta4_classic( const algebra_type &algebra )
0199      * \brief Constructs the runge_kutta4_classic class. This constructor can be used as a default
0200      * constructor if the algebra has a default constructor. 
0201      * \param algebra A copy of algebra is made and stored inside explicit_stepper_base.
0202      */
0203 
0204 
0205     /**
0206      * \fn runge_kutta4_classic::do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
0207      * \brief This method performs one step. The derivative `dxdt` of `in` at the time `t` is passed to the method.
0208      * The result is updated out of place, hence the input is in `in` and the output in `out`.
0209      * Access to this step functionality is provided by explicit_stepper_base and 
0210      * `do_step_impl` should not be called directly.
0211      *
0212      * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
0213      *               Simple System concept.
0214      * \param in The state of the ODE which should be solved. in is not modified in this method
0215      * \param dxdt The derivative of x at t.
0216      * \param t The value of the time, at which the step should be performed.
0217      * \param out The result of the step is written in out.
0218      * \param dt The step size.
0219      */
0220 
0221     /**
0222      * \fn runge_kutta4_classic::adjust_size( const StateType &x )
0223      * \brief Adjust the size of all temporaries in the stepper manually.
0224      * \param x A state from which the size of the temporaries to be resized is deduced.
0225      */
0226 
0227 } // odeint
0228 } // numeric
0229 } // boost
0230 
0231 
0232 #endif // BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED