Chris@16: /* Chris@16: [auto_generated] Chris@16: boost/numeric/odeint/stepper/runge_kutta4_classic.hpp Chris@16: Chris@16: [begin_description] Chris@16: Implementation for the classical Runge Kutta stepper. Chris@16: [end_description] Chris@16: Chris@101: Copyright 2010-2013 Karsten Ahnert Chris@101: Copyright 2010-2013 Mario Mulansky Chris@101: Copyright 2012 Christoph Koke Chris@16: Chris@16: Distributed under the Boost Software License, Version 1.0. Chris@16: (See accompanying file LICENSE_1_0.txt or Chris@16: copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: Chris@16: Chris@16: #ifndef BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED Chris@16: #define BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED Chris@16: Chris@16: Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace numeric { Chris@16: namespace odeint { Chris@16: Chris@16: template< Chris@16: class State , Chris@16: class Value = double , Chris@16: class Deriv = State , Chris@16: class Time = Value , Chris@101: class Algebra = typename algebra_dispatcher< State >::algebra_type , Chris@101: class Operations = typename operations_dispatcher< State >::operations_type , Chris@16: class Resizer = initially_resizer Chris@16: > Chris@16: #ifndef DOXYGEN_SKIP Chris@16: class runge_kutta4_classic Chris@16: : public explicit_stepper_base< Chris@16: runge_kutta4_classic< State , Value , Deriv , Time , Algebra , Operations , Resizer > , Chris@16: 4 , State , Value , Deriv , Time , Algebra , Operations , Resizer > Chris@16: #else Chris@16: class runge_kutta4_classic : public explicit_stepper_base Chris@16: #endif Chris@16: { Chris@16: Chris@16: public : Chris@16: Chris@16: #ifndef DOXYGEN_SKIP Chris@16: typedef explicit_stepper_base< Chris@16: runge_kutta4_classic< State , Value , Deriv , Time , Algebra , Operations , Resizer > , Chris@16: 4 , State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_base_type; Chris@16: #else Chris@16: typedef explicit_stepper_base< runge_kutta4_classic< ... > , ... > stepper_base_type; Chris@16: #endif Chris@16: Chris@16: typedef typename stepper_base_type::state_type state_type; Chris@16: typedef typename stepper_base_type::value_type value_type; Chris@16: typedef typename stepper_base_type::deriv_type deriv_type; Chris@16: typedef typename stepper_base_type::time_type time_type; Chris@16: typedef typename stepper_base_type::algebra_type algebra_type; Chris@16: typedef typename stepper_base_type::operations_type operations_type; Chris@16: typedef typename stepper_base_type::resizer_type resizer_type; Chris@16: Chris@16: #ifndef DOXYGEN_SKIP Chris@16: typedef typename stepper_base_type::stepper_type stepper_type; Chris@16: typedef typename stepper_base_type::wrapped_state_type wrapped_state_type; Chris@16: typedef typename stepper_base_type::wrapped_deriv_type wrapped_deriv_type; Chris@16: #endif // DOXYGEN_SKIP Chris@16: Chris@16: Chris@16: Chris@16: runge_kutta4_classic( const algebra_type &algebra = algebra_type() ) : stepper_base_type( algebra ) Chris@16: { } Chris@16: Chris@16: Chris@16: template< class System , class StateIn , class DerivIn , class StateOut > Chris@16: void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt ) Chris@16: { Chris@16: // ToDo : check if size of in,dxdt,out are equal? Chris@16: Chris@16: static const value_type val1 = static_cast< value_type >( 1 ); Chris@16: Chris@16: m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl< StateIn > , detail::ref( *this ) , detail::_1 ) ); Chris@16: Chris@16: typename odeint::unwrap_reference< System >::type &sys = system; Chris@16: Chris@16: const time_type dh = dt / static_cast< value_type >( 2 ); Chris@16: const time_type th = t + dh; Chris@16: Chris@16: // dt * dxdt = k1 Chris@16: // m_x_tmp = x + dh*dxdt Chris@16: stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , dxdt , Chris@16: typename operations_type::template scale_sum2< value_type , time_type >( val1 , dh ) ); Chris@16: Chris@16: Chris@16: // dt * m_dxt = k2 Chris@16: sys( m_x_tmp.m_v , m_dxt.m_v , th ); Chris@16: Chris@16: // m_x_tmp = x + dh*m_dxt Chris@16: stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , m_dxt.m_v , Chris@16: typename operations_type::template scale_sum2< value_type , time_type >( val1 , dh ) ); Chris@16: Chris@16: Chris@16: // dt * m_dxm = k3 Chris@16: sys( m_x_tmp.m_v , m_dxm.m_v , th ); Chris@16: //m_x_tmp = x + dt*m_dxm Chris@16: stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , m_dxm.m_v , Chris@16: typename operations_type::template scale_sum2< value_type , time_type >( val1 , dt ) ); Chris@16: Chris@16: Chris@16: // dt * m_dxh = k4 Chris@16: sys( m_x_tmp.m_v , m_dxh.m_v , t + dt ); Chris@101: Chris@16: //x += dt/6 * ( m_dxdt + m_dxt + val2*m_dxm ) Chris@16: time_type dt6 = dt / static_cast< value_type >( 6 ); Chris@16: time_type dt3 = dt / static_cast< value_type >( 3 ); Chris@16: stepper_base_type::m_algebra.for_each6( out , in , dxdt , m_dxt.m_v , m_dxm.m_v , m_dxh.m_v , Chris@101: typename operations_type::template scale_sum5< value_type , time_type , time_type , time_type , time_type >( 1.0 , dt6 , dt3 , dt3 , dt6 ) ); Chris@101: Chris@101: // x += dt/6 * m_dxdt + dt/3 * m_dxt ) Chris@101: // stepper_base_type::m_algebra.for_each4( out , in , dxdt , m_dxt.m_v , Chris@101: // typename operations_type::template scale_sum3< value_type , time_type , time_type >( 1.0 , dt6 , dt3 ) ); Chris@101: // // x += dt/3 * m_dxm + dt/6 * m_dxh ) Chris@101: // stepper_base_type::m_algebra.for_each4( out , out , m_dxm.m_v , m_dxh.m_v , Chris@101: // typename operations_type::template scale_sum3< value_type , time_type , time_type >( 1.0 , dt3 , dt6 ) ); Chris@101: Chris@16: } Chris@16: Chris@16: template< class StateType > Chris@16: void adjust_size( const StateType &x ) Chris@16: { Chris@16: resize_impl( x ); Chris@16: stepper_base_type::adjust_size( x ); Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: template< class StateIn > Chris@16: bool resize_impl( const StateIn &x ) Chris@16: { Chris@16: bool resized = false; Chris@16: resized |= adjust_size_by_resizeability( m_x_tmp , x , typename is_resizeable::type() ); Chris@16: resized |= adjust_size_by_resizeability( m_dxm , x , typename is_resizeable::type() ); Chris@16: resized |= adjust_size_by_resizeability( m_dxt , x , typename is_resizeable::type() ); Chris@16: resized |= adjust_size_by_resizeability( m_dxh , x , typename is_resizeable::type() ); Chris@16: return resized; Chris@16: } Chris@16: Chris@16: Chris@16: resizer_type m_resizer; Chris@16: Chris@16: wrapped_deriv_type m_dxt; Chris@16: wrapped_deriv_type m_dxm; Chris@16: wrapped_deriv_type m_dxh; Chris@16: wrapped_state_type m_x_tmp; Chris@16: Chris@16: }; Chris@16: Chris@16: Chris@16: /********* DOXYGEN *********/ Chris@16: Chris@16: /** Chris@16: * \class runge_kutta4_classic Chris@16: * \brief The classical Runge-Kutta stepper of fourth order. Chris@16: * Chris@16: * The Runge-Kutta method of fourth order is one standard method for Chris@16: * solving ordinary differential equations and is widely used, see also Chris@16: * en.wikipedia.org/wiki/Runge-Kutta_methods Chris@16: * The method is explicit and fulfills the Stepper concept. Step size control Chris@16: * or continuous output are not provided. This class implements the method directly, hence the Chris@16: * generic Runge-Kutta algorithm is not used. Chris@16: * Chris@16: * This class derives from explicit_stepper_base and inherits its interface via Chris@16: * CRTP (current recurring template pattern). For more details see Chris@16: * explicit_stepper_base. Chris@16: * Chris@16: * \tparam State The state type. Chris@16: * \tparam Value The value type. Chris@16: * \tparam Deriv The type representing the time derivative of the state. Chris@16: * \tparam Time The time representing the independent variable - the time. Chris@16: * \tparam Algebra The algebra type. Chris@16: * \tparam Operations The operations type. Chris@16: * \tparam Resizer The resizer policy type. Chris@16: */ Chris@16: Chris@16: /** Chris@16: * \fn runge_kutta4_classic::runge_kutta4_classic( const algebra_type &algebra ) Chris@16: * \brief Constructs the runge_kutta4_classic class. This constructor can be used as a default Chris@16: * constructor if the algebra has a default constructor. Chris@16: * \param algebra A copy of algebra is made and stored inside explicit_stepper_base. Chris@16: */ Chris@16: Chris@16: Chris@16: /** Chris@16: * \fn runge_kutta4_classic::do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt ) Chris@16: * \brief This method performs one step. The derivative `dxdt` of `in` at the time `t` is passed to the method. Chris@16: * The result is updated out of place, hence the input is in `in` and the output in `out`. Chris@16: * Access to this step functionality is provided by explicit_stepper_base and Chris@16: * `do_step_impl` should not be called directly. Chris@16: * Chris@16: * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the Chris@16: * Simple System concept. Chris@16: * \param in The state of the ODE which should be solved. in is not modified in this method Chris@16: * \param dxdt The derivative of x at t. Chris@16: * \param t The value of the time, at which the step should be performed. Chris@16: * \param out The result of the step is written in out. Chris@16: * \param dt The step size. Chris@16: */ Chris@16: Chris@16: /** Chris@16: * \fn runge_kutta4_classic::adjust_size( const StateType &x ) Chris@16: * \brief Adjust the size of all temporaries in the stepper manually. Chris@16: * \param x A state from which the size of the temporaries to be resized is deduced. Chris@16: */ Chris@16: Chris@16: } // odeint Chris@16: } // numeric Chris@16: } // boost Chris@16: Chris@16: Chris@16: #endif // BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED