3 boost/numeric/odeint/stepper/adams_bashforth_moulton.hpp
6 Implementation of the Adams-Bashforth-Moulton method, a predictor-corrector multistep method.
9 Copyright 2011-2013 Karsten Ahnert
10 Copyright 2011-2013 Mario Mulansky
11 Copyright 2012 Christoph Koke
13 Distributed under the Boost Software License, Version 1.0.
14 (See accompanying file LICENSE_1_0.txt or
15 copy at http://www.boost.org/LICENSE_1_0.txt)
19 #ifndef BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
20 #define BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
23 #include <boost/numeric/odeint/util/bind.hpp>
25 #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
26 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
27 #include <boost/numeric/odeint/algebra/default_operations.hpp>
28 #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
29 #include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
31 #include <boost/numeric/odeint/util/state_wrapper.hpp>
32 #include <boost/numeric/odeint/util/resizer.hpp>
34 #include <boost/numeric/odeint/stepper/adams_bashforth.hpp>
35 #include <boost/numeric/odeint/stepper/adams_moulton.hpp>
47 class Value = double ,
50 class Algebra = typename algebra_dispatcher< State >::algebra_type ,
51 class Operations = typename operations_dispatcher< State >::operations_type ,
52 class Resizer = initially_resizer,
53 class InitializingStepper = runge_kutta4< State , Value , Deriv , Time , Algebra , Operations, Resizer >
55 class adams_bashforth_moulton
59 BOOST_STATIC_ASSERT(( Steps > 0 ));
60 BOOST_STATIC_ASSERT(( Steps < 9 ));
65 typedef State state_type;
66 typedef state_wrapper< state_type > wrapped_state_type;
67 typedef Value value_type;
68 typedef Deriv deriv_type;
69 typedef state_wrapper< deriv_type > wrapped_deriv_type;
70 typedef Time time_type;
71 typedef Algebra algebra_type;
72 typedef Operations operations_type;
73 typedef Resizer resizer_type;
74 typedef stepper_tag stepper_category;
75 typedef InitializingStepper initializing_stepper_type;
77 static const size_t steps = Steps;
79 typedef adams_bashforth< steps , state_type , value_type , deriv_type , time_type , algebra_type , operations_type , resizer_type, initializing_stepper_type > adams_bashforth_type;
80 typedef adams_moulton< steps , state_type , value_type , deriv_type , time_type , algebra_type , operations_type , resizer_type > adams_moulton_type;
81 typedef adams_bashforth_moulton< steps , state_type , value_type , deriv_type , time_type , algebra_type , operations_type , resizer_type , initializing_stepper_type> stepper_type;
83 typedef unsigned short order_type;
84 static const order_type order_value = steps;
86 /** \brief Constructs the adams_bashforth class. */
87 adams_bashforth_moulton( void )
88 : m_adams_bashforth() , m_adams_moulton( m_adams_bashforth.algebra() )
92 adams_bashforth_moulton( const algebra_type &algebra )
93 : m_adams_bashforth( algebra ) , m_adams_moulton( m_adams_bashforth.algebra() )
97 order_type order( void ) const { return order_value; }
99 template< class System , class StateInOut >
100 void do_step( System system , StateInOut &x , time_type t , time_type dt )
102 do_step_impl1( system , x , t , dt );
106 * \brief Second version to solve the forwarding problem, can be called with Boost.Range as StateInOut.
108 template< class System , class StateInOut >
109 void do_step( System system , const StateInOut &x , time_type t , time_type dt )
111 do_step_impl1( system , x , t , dt );
114 template< class System , class StateIn , class StateOut >
115 void do_step( System system , const StateIn &in , time_type t , const StateOut &out , time_type dt )
117 do_step_impl2( system , in , t , out , dt );
121 * \brief Second version to solve the forwarding problem, can be called with Boost.Range as StateOut.
123 template< class System , class StateIn , class StateOut >
124 void do_step( System system , const StateIn &in , time_type t , StateOut &out , time_type dt )
126 do_step_impl2( system , in ,t , out , dt );
130 template< class StateType >
131 void adjust_size( const StateType &x )
133 m_adams_bashforth.adjust_size( x );
134 m_adams_moulton.adjust_size( x );
139 template< class ExplicitStepper , class System , class StateIn >
140 void initialize( ExplicitStepper explicit_stepper , System system , StateIn &x , time_type &t , time_type dt )
142 m_adams_bashforth.initialize( explicit_stepper , system , x , t , dt );
146 template< class System , class StateIn >
147 void initialize( System system , StateIn &x , time_type &t , time_type dt )
149 m_adams_bashforth.initialize( system , x , t , dt );
156 template< typename System , typename StateInOut >
157 void do_step_impl1( System system , StateInOut &x , time_type t , time_type dt )
159 if( m_adams_bashforth.is_initialized() )
161 m_resizer.adjust_size( x , detail::bind( &stepper_type::template resize_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
162 m_adams_bashforth.do_step( system , x , t , m_x.m_v , dt );
163 m_adams_moulton.do_step( system , x , m_x.m_v , t+dt , x , dt , m_adams_bashforth.step_storage() );
167 m_adams_bashforth.do_step( system , x , t , dt );
171 template< typename System , typename StateIn , typename StateInOut >
172 void do_step_impl2( System system , StateIn const &in , time_type t , StateInOut & out , time_type dt )
174 if( m_adams_bashforth.is_initialized() )
176 m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
177 m_adams_bashforth.do_step( system , in , t , m_x.m_v , dt );
178 m_adams_moulton.do_step( system , in , m_x.m_v , t , out , dt , m_adams_bashforth.step_storage() );
182 m_adams_bashforth.do_step( system , in , t , out , dt );
187 template< class StateIn >
188 bool resize_impl( const StateIn &x )
190 return adjust_size_by_resizeability( m_x , x , typename is_resizeable< state_type >::type() );
193 adams_bashforth_type m_adams_bashforth;
194 adams_moulton_type m_adams_moulton;
195 wrapped_state_type m_x;
196 resizer_type m_resizer;
200 /********* DOXYGEN ********/
203 * \class adams_bashforth_moulton
204 * \brief The Adams-Bashforth-Moulton multistep algorithm.
206 * The Adams-Bashforth method is a multi-step predictor-corrector algorithm
207 * with configurable step number. The step number is specified as template
208 * parameter Steps and it then uses the result from the previous Steps steps.
210 * <a href="http://en.wikipedia.org/wiki/Linear_multistep_method">en.wikipedia.org/wiki/Linear_multistep_method</a>.
211 * Currently, a maximum of Steps=8 is supported.
212 * The method is explicit and fulfills the Stepper concept. Step size control
213 * or continuous output are not provided.
215 * This class derives from algebra_base and inherits its interface via
216 * CRTP (current recurring template pattern). For more details see
217 * algebra_stepper_base.
219 * \tparam Steps The number of steps (maximal 8).
220 * \tparam State The state type.
221 * \tparam Value The value type.
222 * \tparam Deriv The type representing the time derivative of the state.
223 * \tparam Time The time representing the independent variable - the time.
224 * \tparam Algebra The algebra type.
225 * \tparam Operations The operations type.
226 * \tparam Resizer The resizer policy type.
227 * \tparam InitializingStepper The stepper for the first two steps.
231 * \fn adams_bashforth_moulton::adams_bashforth_moulton( const algebra_type &algebra )
232 * \brief Constructs the adams_bashforth class. This constructor can be used as a default
233 * constructor if the algebra has a default constructor.
234 * \param algebra A copy of algebra is made and stored.
238 * \fn adams_bashforth_moulton::order( void ) const
239 * \brief Returns the order of the algorithm, which is equal to the number of steps+1.
240 * \return order of the method.
244 * \fn adams_bashforth_moulton::do_step( System system , StateInOut &x , time_type t , time_type dt )
245 * \brief This method performs one step. It transforms the result in-place.
247 * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
248 * Simple System concept.
249 * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
250 * \param t The value of the time, at which the step should be performed.
251 * \param dt The step size.
256 * \fn adams_bashforth_moulton::do_step( System system , const StateIn &in , time_type t , const StateOut &out , time_type dt )
257 * \brief The method performs one step with the stepper passed by Stepper. The state of the ODE is updated out-of-place.
259 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
260 * Simple System concept.
261 * \param in The state of the ODE which should be solved. in is not modified in this method
262 * \param t The value of the time, at which the step should be performed.
263 * \param out The result of the step is written in out.
264 * \param dt The step size.
268 * \fn adams_bashforth_moulton::adjust_size( const StateType &x )
269 * \brief Adjust the size of all temporaries in the stepper manually.
270 * \param x A state from which the size of the temporaries to be resized is deduced.
274 * \fn adams_bashforth_moulton::initialize( ExplicitStepper explicit_stepper , System system , StateIn &x , time_type &t , time_type dt )
275 * \brief Initialized the stepper. Does Steps-1 steps with the explicit_stepper to fill the buffer.
276 * \note The state x and time t are updated to the values after Steps-1 initial steps.
277 * \param explicit_stepper the stepper used to fill the buffer of previous step results
278 * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
279 * Simple System concept.
280 * \param x The initial state of the ODE which should be solved, updated after in this method.
281 * \param t The initial time, updated in this method.
282 * \param dt The step size.
286 * \fn adams_bashforth_moulton::initialize( System system , StateIn &x , time_type &t , time_type dt )
287 * \brief Initialized the stepper. Does Steps-1 steps using the standard initializing stepper
288 * of the underlying adams_bashforth stepper.
289 * \param system The system function to solve, hence the r.h.s. of the ordinary differential equation. It must fulfill the
290 * Simple System concept.
291 * \param x The state of the ODE which should be solved. After calling do_step the result is updated in x.
292 * \param t The value of the time, at which the step should be performed.
293 * \param dt The step size.
303 #endif // BOOST_NUMERIC_ODEINT_STEPPER_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED