Chris@16: /* Chris@16: [auto_generated] Chris@16: boost/numeric/odeint/stepper/modified_midpoint.hpp Chris@16: Chris@16: [begin_description] Chris@16: Modified midpoint method for the use in Burlish-Stoer stepper. Chris@16: [end_description] Chris@16: Chris@101: Copyright 2011-2013 Mario Mulansky Chris@101: Copyright 2011-2013 Karsten Ahnert 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_MODIFIED_MIDPOINT_HPP_INCLUDED Chris@16: #define BOOST_NUMERIC_ODEINT_STEPPER_MODIFIED_MIDPOINT_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #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 modified_midpoint Chris@16: : public explicit_stepper_base< Chris@16: modified_midpoint< State , Value , Deriv , Time , Algebra , Operations , Resizer > , Chris@16: 2 , State , Value , Deriv , Time , Algebra , Operations , Resizer > Chris@16: #else Chris@16: class modified_midpoint : public explicit_stepper_base Chris@16: #endif Chris@16: { Chris@16: Chris@16: public : Chris@16: Chris@16: typedef explicit_stepper_base< Chris@16: modified_midpoint< State , Value , Deriv , Time , Algebra , Operations , Resizer > , Chris@16: 2 , State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_base_type; Chris@16: Chris@16: typedef typename stepper_base_type::state_type state_type; Chris@16: typedef typename stepper_base_type::wrapped_state_type wrapped_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::wrapped_deriv_type wrapped_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: typedef typename stepper_base_type::stepper_type stepper_type; Chris@16: Chris@16: Chris@16: modified_midpoint( unsigned short steps = 2 , const algebra_type &algebra = algebra_type() ) Chris@16: : stepper_base_type( algebra ) , m_steps( steps ) 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: static const value_type val1 = static_cast< value_type >( 1 ); Chris@16: static const value_type val05 = static_cast< value_type >( 1 ) / static_cast< value_type >( 2 ); 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: const time_type h = dt / static_cast( m_steps ); Chris@16: const time_type h2 = static_cast(2) * h; Chris@16: Chris@16: typename odeint::unwrap_reference< System >::type &sys = system; Chris@16: Chris@16: time_type th = t + h; Chris@16: Chris@16: // m_x1 = x + h*dxdt Chris@16: stepper_base_type::m_algebra.for_each3( m_x1.m_v , in , dxdt , Chris@16: typename operations_type::template scale_sum2< value_type , time_type >( val1 , h ) ); Chris@16: Chris@16: sys( m_x1.m_v , m_dxdt.m_v , th ); Chris@16: Chris@16: boost::numeric::odeint::copy( in , m_x0.m_v ); Chris@16: Chris@16: unsigned short i = 1; Chris@16: while( i != m_steps ) Chris@16: { Chris@16: // general step Chris@16: //tmp = m_x1; m_x1 = m_x0 + h2*m_dxdt; m_x0 = tmp Chris@16: stepper_base_type::m_algebra.for_each3( m_x1.m_v , m_x0.m_v , m_dxdt.m_v , Chris@16: typename operations_type::template scale_sum_swap2< value_type , time_type >( val1 , h2 ) ); Chris@16: th += h; Chris@16: sys( m_x1.m_v , m_dxdt.m_v , th); Chris@16: i++; Chris@16: } Chris@16: Chris@16: // last step Chris@16: // x = 0.5*( m_x0 + m_x1 + h*m_dxdt ) Chris@16: stepper_base_type::m_algebra.for_each4( out , m_x0.m_v , m_x1.m_v , m_dxdt.m_v , Chris@16: typename operations_type::template scale_sum3< value_type , value_type , time_type >( val05 , val05 , val05*h ) ); Chris@16: } Chris@16: Chris@16: Chris@16: void set_steps( unsigned short steps ) Chris@16: { m_steps = steps; } Chris@16: Chris@16: Chris@16: unsigned short steps( void ) const Chris@16: { return m_steps; } Chris@16: Chris@16: Chris@16: template< class StateIn > Chris@16: void adjust_size( const StateIn &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_x0 , x , typename is_resizeable::type() ); Chris@16: resized |= adjust_size_by_resizeability( m_x1 , x , typename is_resizeable::type() ); Chris@16: resized |= adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable::type() ); Chris@16: return resized; Chris@16: } Chris@16: Chris@16: Chris@16: unsigned short m_steps; Chris@16: Chris@16: resizer_type m_resizer; Chris@16: Chris@16: wrapped_state_type m_x0; Chris@16: wrapped_state_type m_x1; Chris@16: wrapped_deriv_type m_dxdt; Chris@16: Chris@16: }; Chris@16: Chris@16: Chris@16: /* Modified midpoint which stores derivatives and state at dt/2 in some external storage for later usage in dense output calculation Chris@16: * This Stepper is for use in Bulirsch Stoer only. It DOES NOT meet any stepper concept. 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: class modified_midpoint_dense_out Chris@16: { Chris@16: Chris@16: public : Chris@16: Chris@16: typedef State state_type; Chris@16: typedef Value value_type; Chris@16: typedef Deriv deriv_type; Chris@16: typedef Time time_type; Chris@16: typedef Algebra algebra_type; Chris@16: typedef Operations operations_type; Chris@16: typedef Resizer resizer_type; Chris@16: typedef state_wrapper< state_type > wrapped_state_type; Chris@16: typedef state_wrapper< deriv_type > wrapped_deriv_type; Chris@16: Chris@16: typedef modified_midpoint_dense_out< State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_type; Chris@16: typedef std::vector< wrapped_deriv_type > deriv_table_type; Chris@16: Chris@16: modified_midpoint_dense_out( unsigned short steps = 2 , const algebra_type &algebra = algebra_type() ) Chris@16: : m_algebra( algebra ) , m_steps( steps ) Chris@16: { } Chris@16: Chris@16: /* Chris@16: * performs a modified midpoint step with m_steps intermediate points Chris@16: * stores approximation for x(t+dt/2) in x_mp and all evaluated function results in derivs Chris@16: * Chris@16: */ Chris@16: Chris@16: template< class System , class StateIn , class DerivIn , class StateOut > Chris@16: void do_step( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt , Chris@16: state_type &x_mp , deriv_table_type &derivs ) Chris@16: { Chris@16: Chris@16: static const value_type val1 = static_cast< value_type >( 1 ); Chris@16: static const value_type val05 = static_cast< value_type >( 1 ) / static_cast< value_type >( 2 ); Chris@16: Chris@16: m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize< StateIn > , detail::ref( *this ) , detail::_1 ) ); Chris@16: Chris@16: const time_type h = dt / static_cast( m_steps ); Chris@16: const time_type h2 = static_cast( 2 ) * h; Chris@16: Chris@16: typename odeint::unwrap_reference< System >::type &sys = system; Chris@16: Chris@16: time_type th = t + h; Chris@16: Chris@16: // m_x1 = x + h*dxdt Chris@16: m_algebra.for_each3( m_x1.m_v , in , dxdt , Chris@16: typename operations_type::template scale_sum2< value_type , time_type >( val1 , h ) ); Chris@16: Chris@16: if( m_steps == 2 ) Chris@16: // result of first step already gives approximation at the center of the interval Chris@16: boost::numeric::odeint::copy( m_x1.m_v , x_mp ); Chris@16: Chris@16: sys( m_x1.m_v , derivs[0].m_v , th ); Chris@16: Chris@16: boost::numeric::odeint::copy( in , m_x0.m_v ); Chris@16: Chris@16: unsigned short i = 1; Chris@16: while( i != m_steps ) Chris@16: { Chris@16: // general step Chris@16: //tmp = m_x1; m_x1 = m_x0 + h2*m_dxdt; m_x0 = tmp Chris@16: m_algebra.for_each3( m_x1.m_v , m_x0.m_v , derivs[i-1].m_v , Chris@16: typename operations_type::template scale_sum_swap2< value_type , time_type >( val1 , h2 ) ); Chris@16: if( i == m_steps/2-1 ) Chris@16: // save approximation at the center of the interval Chris@16: boost::numeric::odeint::copy( m_x1.m_v , x_mp ); Chris@16: Chris@16: th += h; Chris@16: sys( m_x1.m_v , derivs[i].m_v , th); Chris@16: i++; Chris@16: } Chris@16: Chris@16: // last step Chris@16: // x = 0.5*( m_x0 + m_x1 + h*m_dxdt ) Chris@16: m_algebra.for_each4( out , m_x0.m_v , m_x1.m_v , derivs[m_steps-1].m_v , Chris@16: typename operations_type::template scale_sum3< value_type , value_type , time_type >( val05 , val05 , val05*h ) ); Chris@16: } Chris@16: Chris@16: Chris@16: void set_steps( unsigned short steps ) Chris@16: { m_steps = steps; } Chris@16: Chris@16: Chris@16: unsigned short steps( void ) const Chris@16: { return m_steps; } Chris@16: Chris@16: Chris@16: template< class StateIn > Chris@16: bool resize( const StateIn &x ) Chris@16: { Chris@16: bool resized( false ); Chris@16: resized |= adjust_size_by_resizeability( m_x0 , x , typename is_resizeable::type() ); Chris@16: resized |= adjust_size_by_resizeability( m_x1 , x , typename is_resizeable::type() ); Chris@16: return resized; Chris@16: } Chris@16: Chris@16: template< class StateIn > Chris@16: void adjust_size( const StateIn &x ) Chris@16: { Chris@16: resize( x ); Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: algebra_type m_algebra; Chris@16: Chris@16: unsigned short m_steps; Chris@16: Chris@16: resizer_type m_resizer; Chris@16: Chris@16: wrapped_state_type m_x0; Chris@16: wrapped_state_type m_x1; Chris@16: Chris@16: }; Chris@16: Chris@16: Chris@16: Chris@16: /********** DOXYGEN ***********/ Chris@16: Chris@16: /** Chris@16: * \class modified_midpoint Chris@16: * Chris@16: * Implementation of the modified midpoint method with a configurable Chris@16: * number of intermediate steps. This class is used by the Bulirsch-Stoer Chris@16: * algorithm and is not meant for direct usage. Chris@16: */ Chris@16: Chris@16: Chris@16: /** Chris@16: * \class modified_midpoint_dense_out Chris@16: * Chris@16: * Implementation of the modified midpoint method with a configurable Chris@16: * number of intermediate steps. This class is used by the dense output Chris@16: * Bulirsch-Stoer algorithm and is not meant for direct usage. Chris@16: * \note This stepper is for internal use only and does not meet Chris@16: * any stepper concept. Chris@16: */ Chris@16: Chris@16: Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: #endif // BOOST_NUMERIC_ODEINT_STEPPER_MODIFIED_MIDPOINT_HPP_INCLUDED