comparison DEPENDENCIES/generic/include/boost/numeric/odeint/stepper/runge_kutta4_classic.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
1 /*
2 [auto_generated]
3 boost/numeric/odeint/stepper/runge_kutta4_classic.hpp
4
5 [begin_description]
6 Implementation for the classical Runge Kutta stepper.
7 [end_description]
8
9 Copyright 2009-2011 Karsten Ahnert
10 Copyright 2009-2011 Mario Mulansky
11
12 Distributed under the Boost Software License, Version 1.0.
13 (See accompanying file LICENSE_1_0.txt or
14 copy at http://www.boost.org/LICENSE_1_0.txt)
15 */
16
17
18 #ifndef BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED
19 #define BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED
20
21
22
23 #include <boost/numeric/odeint/stepper/base/explicit_stepper_base.hpp>
24 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
25 #include <boost/numeric/odeint/algebra/default_operations.hpp>
26
27 #include <boost/numeric/odeint/util/state_wrapper.hpp>
28 #include <boost/numeric/odeint/util/is_resizeable.hpp>
29 #include <boost/numeric/odeint/util/resizer.hpp>
30
31 namespace boost {
32 namespace numeric {
33 namespace odeint {
34
35 template<
36 class State ,
37 class Value = double ,
38 class Deriv = State ,
39 class Time = Value ,
40 class Algebra = range_algebra ,
41 class Operations = default_operations ,
42 class Resizer = initially_resizer
43 >
44 #ifndef DOXYGEN_SKIP
45 class runge_kutta4_classic
46 : public explicit_stepper_base<
47 runge_kutta4_classic< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
48 4 , State , Value , Deriv , Time , Algebra , Operations , Resizer >
49 #else
50 class runge_kutta4_classic : public explicit_stepper_base
51 #endif
52 {
53
54 public :
55
56 #ifndef DOXYGEN_SKIP
57 typedef explicit_stepper_base<
58 runge_kutta4_classic< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
59 4 , State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_base_type;
60 #else
61 typedef explicit_stepper_base< runge_kutta4_classic< ... > , ... > stepper_base_type;
62 #endif
63
64 typedef typename stepper_base_type::state_type state_type;
65 typedef typename stepper_base_type::value_type value_type;
66 typedef typename stepper_base_type::deriv_type deriv_type;
67 typedef typename stepper_base_type::time_type time_type;
68 typedef typename stepper_base_type::algebra_type algebra_type;
69 typedef typename stepper_base_type::operations_type operations_type;
70 typedef typename stepper_base_type::resizer_type resizer_type;
71
72 #ifndef DOXYGEN_SKIP
73 typedef typename stepper_base_type::stepper_type stepper_type;
74 typedef typename stepper_base_type::wrapped_state_type wrapped_state_type;
75 typedef typename stepper_base_type::wrapped_deriv_type wrapped_deriv_type;
76 #endif // DOXYGEN_SKIP
77
78
79
80 runge_kutta4_classic( const algebra_type &algebra = algebra_type() ) : stepper_base_type( algebra )
81 { }
82
83
84 template< class System , class StateIn , class DerivIn , class StateOut >
85 void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
86 {
87 // ToDo : check if size of in,dxdt,out are equal?
88
89 static const value_type val1 = static_cast< value_type >( 1 );
90
91 m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
92
93 typename odeint::unwrap_reference< System >::type &sys = system;
94
95 const time_type dh = dt / static_cast< value_type >( 2 );
96 const time_type th = t + dh;
97
98 // dt * dxdt = k1
99 // m_x_tmp = x + dh*dxdt
100 stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , dxdt ,
101 typename operations_type::template scale_sum2< value_type , time_type >( val1 , dh ) );
102
103
104 // dt * m_dxt = k2
105 sys( m_x_tmp.m_v , m_dxt.m_v , th );
106
107 // m_x_tmp = x + dh*m_dxt
108 stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , m_dxt.m_v ,
109 typename operations_type::template scale_sum2< value_type , time_type >( val1 , dh ) );
110
111
112 // dt * m_dxm = k3
113 sys( m_x_tmp.m_v , m_dxm.m_v , th );
114 //m_x_tmp = x + dt*m_dxm
115 stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , m_dxm.m_v ,
116 typename operations_type::template scale_sum2< value_type , time_type >( val1 , dt ) );
117
118
119 // dt * m_dxh = k4
120 sys( m_x_tmp.m_v , m_dxh.m_v , t + dt );
121 //x += dt/6 * ( m_dxdt + m_dxt + val2*m_dxm )
122 time_type dt6 = dt / static_cast< value_type >( 6 );
123 time_type dt3 = dt / static_cast< value_type >( 3 );
124 stepper_base_type::m_algebra.for_each6( out , in , dxdt , m_dxt.m_v , m_dxm.m_v , m_dxh.m_v ,
125 typename operations_type::template scale_sum5< value_type , time_type , time_type , time_type , time_type >( 1.0 , dt6 , dt3 , dt3 , dt6 ) );
126 }
127
128 template< class StateType >
129 void adjust_size( const StateType &x )
130 {
131 resize_impl( x );
132 stepper_base_type::adjust_size( x );
133 }
134
135 private:
136
137 template< class StateIn >
138 bool resize_impl( const StateIn &x )
139 {
140 bool resized = false;
141 resized |= adjust_size_by_resizeability( m_x_tmp , x , typename is_resizeable<state_type>::type() );
142 resized |= adjust_size_by_resizeability( m_dxm , x , typename is_resizeable<deriv_type>::type() );
143 resized |= adjust_size_by_resizeability( m_dxt , x , typename is_resizeable<deriv_type>::type() );
144 resized |= adjust_size_by_resizeability( m_dxh , x , typename is_resizeable<deriv_type>::type() );
145 return resized;
146 }
147
148
149 resizer_type m_resizer;
150
151 wrapped_deriv_type m_dxt;
152 wrapped_deriv_type m_dxm;
153 wrapped_deriv_type m_dxh;
154 wrapped_state_type m_x_tmp;
155
156 };
157
158
159 /********* DOXYGEN *********/
160
161 /**
162 * \class runge_kutta4_classic
163 * \brief The classical Runge-Kutta stepper of fourth order.
164 *
165 * The Runge-Kutta method of fourth order is one standard method for
166 * solving ordinary differential equations and is widely used, see also
167 * <a href="http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods">en.wikipedia.org/wiki/Runge-Kutta_methods</a>
168 * The method is explicit and fulfills the Stepper concept. Step size control
169 * or continuous output are not provided. This class implements the method directly, hence the
170 * generic Runge-Kutta algorithm is not used.
171 *
172 * This class derives from explicit_stepper_base and inherits its interface via
173 * CRTP (current recurring template pattern). For more details see
174 * explicit_stepper_base.
175 *
176 * \tparam State The state type.
177 * \tparam Value The value type.
178 * \tparam Deriv The type representing the time derivative of the state.
179 * \tparam Time The time representing the independent variable - the time.
180 * \tparam Algebra The algebra type.
181 * \tparam Operations The operations type.
182 * \tparam Resizer The resizer policy type.
183 */
184
185 /**
186 * \fn runge_kutta4_classic::runge_kutta4_classic( const algebra_type &algebra )
187 * \brief Constructs the runge_kutta4_classic class. This constructor can be used as a default
188 * constructor if the algebra has a default constructor.
189 * \param algebra A copy of algebra is made and stored inside explicit_stepper_base.
190 */
191
192
193 /**
194 * \fn runge_kutta4_classic::do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
195 * \brief This method performs one step. The derivative `dxdt` of `in` at the time `t` is passed to the method.
196 * The result is updated out of place, hence the input is in `in` and the output in `out`.
197 * Access to this step functionality is provided by explicit_stepper_base and
198 * `do_step_impl` should not be called directly.
199 *
200 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
201 * Simple System concept.
202 * \param in The state of the ODE which should be solved. in is not modified in this method
203 * \param dxdt The derivative of x at t.
204 * \param t The value of the time, at which the step should be performed.
205 * \param out The result of the step is written in out.
206 * \param dt The step size.
207 */
208
209 /**
210 * \fn runge_kutta4_classic::adjust_size( const StateType &x )
211 * \brief Adjust the size of all temporaries in the stepper manually.
212 * \param x A state from which the size of the temporaries to be resized is deduced.
213 */
214
215 } // odeint
216 } // numeric
217 } // boost
218
219
220 #endif // BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA4_CLASSIC_HPP_INCLUDED