comparison DEPENDENCIES/generic/include/boost/numeric/odeint/stepper/runge_kutta_dopri5.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_kutta_dopri5.hpp
4
5 [begin_description]
6 Implementation of the Dormand-Prince 5(4) method. This stepper can also be used with the dense-output controlled 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_KUTTA_DOPRI5_HPP_INCLUDED
19 #define BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA_DOPRI5_HPP_INCLUDED
20
21
22 #include <boost/numeric/odeint/util/bind.hpp>
23
24 #include <boost/numeric/odeint/stepper/base/explicit_error_stepper_fsal_base.hpp>
25 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
26 #include <boost/numeric/odeint/algebra/default_operations.hpp>
27 #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
28
29 #include <boost/numeric/odeint/util/state_wrapper.hpp>
30 #include <boost/numeric/odeint/util/is_resizeable.hpp>
31 #include <boost/numeric/odeint/util/resizer.hpp>
32 #include <boost/numeric/odeint/util/same_instance.hpp>
33
34 namespace boost {
35 namespace numeric {
36 namespace odeint {
37
38
39
40 template<
41 class State ,
42 class Value = double ,
43 class Deriv = State ,
44 class Time = Value ,
45 class Algebra = range_algebra ,
46 class Operations = default_operations ,
47 class Resizer = initially_resizer
48 >
49 class runge_kutta_dopri5
50 #ifndef DOXYGEN_SKIP
51 : public explicit_error_stepper_fsal_base<
52 runge_kutta_dopri5< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
53 5 , 5 , 4 , State , Value , Deriv , Time , Algebra , Operations , Resizer >
54 #else
55 : public explicit_error_stepper_fsal_base
56 #endif
57 {
58
59 public :
60
61 #ifndef DOXYGEN_SKIP
62 typedef explicit_error_stepper_fsal_base<
63 runge_kutta_dopri5< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
64 5 , 5 , 4 , State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_base_type;
65 #else
66 typedef explicit_error_stepper_fsal_base< runge_kutta_dopri5< ... > , ... > stepper_base_type;
67 #endif
68
69 typedef typename stepper_base_type::state_type state_type;
70 typedef typename stepper_base_type::value_type value_type;
71 typedef typename stepper_base_type::deriv_type deriv_type;
72 typedef typename stepper_base_type::time_type time_type;
73 typedef typename stepper_base_type::algebra_type algebra_type;
74 typedef typename stepper_base_type::operations_type operations_type;
75 typedef typename stepper_base_type::resizer_type resizer_type;
76
77 #ifndef DOXYGEN_SKIP
78 typedef typename stepper_base_type::stepper_type stepper_type;
79 typedef typename stepper_base_type::wrapped_state_type wrapped_state_type;
80 typedef typename stepper_base_type::wrapped_deriv_type wrapped_deriv_type;
81 #endif // DOXYGEN_SKIP
82
83
84 runge_kutta_dopri5( const algebra_type &algebra = algebra_type() ) : stepper_base_type( algebra )
85 { }
86
87
88 template< class System , class StateIn , class DerivIn , class StateOut , class DerivOut >
89 void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt_in , time_type t ,
90 StateOut &out , DerivOut &dxdt_out , time_type dt )
91 {
92 const value_type a2 = static_cast<value_type> ( 1 ) / static_cast<value_type>( 5 );
93 const value_type a3 = static_cast<value_type> ( 3 ) / static_cast<value_type> ( 10 );
94 const value_type a4 = static_cast<value_type> ( 4 ) / static_cast<value_type> ( 5 );
95 const value_type a5 = static_cast<value_type> ( 8 )/static_cast<value_type> ( 9 );
96
97 const value_type b21 = static_cast<value_type> ( 1 ) / static_cast<value_type> ( 5 );
98
99 const value_type b31 = static_cast<value_type> ( 3 ) / static_cast<value_type>( 40 );
100 const value_type b32 = static_cast<value_type> ( 9 ) / static_cast<value_type>( 40 );
101
102 const value_type b41 = static_cast<value_type> ( 44 ) / static_cast<value_type> ( 45 );
103 const value_type b42 = static_cast<value_type> ( -56 ) / static_cast<value_type> ( 15 );
104 const value_type b43 = static_cast<value_type> ( 32 ) / static_cast<value_type> ( 9 );
105
106 const value_type b51 = static_cast<value_type> ( 19372 ) / static_cast<value_type>( 6561 );
107 const value_type b52 = static_cast<value_type> ( -25360 ) / static_cast<value_type> ( 2187 );
108 const value_type b53 = static_cast<value_type> ( 64448 ) / static_cast<value_type>( 6561 );
109 const value_type b54 = static_cast<value_type> ( -212 ) / static_cast<value_type>( 729 );
110
111 const value_type b61 = static_cast<value_type> ( 9017 ) / static_cast<value_type>( 3168 );
112 const value_type b62 = static_cast<value_type> ( -355 ) / static_cast<value_type>( 33 );
113 const value_type b63 = static_cast<value_type> ( 46732 ) / static_cast<value_type>( 5247 );
114 const value_type b64 = static_cast<value_type> ( 49 ) / static_cast<value_type>( 176 );
115 const value_type b65 = static_cast<value_type> ( -5103 ) / static_cast<value_type>( 18656 );
116
117 const value_type c1 = static_cast<value_type> ( 35 ) / static_cast<value_type>( 384 );
118 const value_type c3 = static_cast<value_type> ( 500 ) / static_cast<value_type>( 1113 );
119 const value_type c4 = static_cast<value_type> ( 125 ) / static_cast<value_type>( 192 );
120 const value_type c5 = static_cast<value_type> ( -2187 ) / static_cast<value_type>( 6784 );
121 const value_type c6 = static_cast<value_type> ( 11 ) / static_cast<value_type>( 84 );
122
123 typename odeint::unwrap_reference< System >::type &sys = system;
124
125 m_k_x_tmp_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_k_x_tmp_impl<StateIn> , detail::ref( *this ) , detail::_1 ) );
126
127 //m_x_tmp = x + dt*b21*dxdt
128 stepper_base_type::m_algebra.for_each3( m_x_tmp.m_v , in , dxdt_in ,
129 typename operations_type::template scale_sum2< value_type , time_type >( 1.0 , dt*b21 ) );
130
131 sys( m_x_tmp.m_v , m_k2.m_v , t + dt*a2 );
132 // m_x_tmp = x + dt*b31*dxdt + dt*b32*m_k2
133 stepper_base_type::m_algebra.for_each4( m_x_tmp.m_v , in , dxdt_in , m_k2.m_v ,
134 typename operations_type::template scale_sum3< value_type , time_type , time_type >( 1.0 , dt*b31 , dt*b32 ));
135
136 sys( m_x_tmp.m_v , m_k3.m_v , t + dt*a3 );
137 // m_x_tmp = x + dt * (b41*dxdt + b42*m_k2 + b43*m_k3)
138 stepper_base_type::m_algebra.for_each5( m_x_tmp.m_v , in , dxdt_in , m_k2.m_v , m_k3.m_v ,
139 typename operations_type::template scale_sum4< value_type , time_type , time_type , time_type >( 1.0 , dt*b41 , dt*b42 , dt*b43 ));
140
141 sys( m_x_tmp.m_v, m_k4.m_v , t + dt*a4 );
142 stepper_base_type::m_algebra.for_each6( m_x_tmp.m_v , in , dxdt_in , m_k2.m_v , m_k3.m_v , m_k4.m_v ,
143 typename operations_type::template scale_sum5< value_type , time_type , time_type , time_type , time_type >( 1.0 , dt*b51 , dt*b52 , dt*b53 , dt*b54 ));
144
145 sys( m_x_tmp.m_v , m_k5.m_v , t + dt*a5 );
146 stepper_base_type::m_algebra.for_each7( m_x_tmp.m_v , in , dxdt_in , m_k2.m_v , m_k3.m_v , m_k4.m_v , m_k5.m_v ,
147 typename operations_type::template scale_sum6< value_type , time_type , time_type , time_type , time_type , time_type >( 1.0 , dt*b61 , dt*b62 , dt*b63 , dt*b64 , dt*b65 ));
148
149 sys( m_x_tmp.m_v , m_k6.m_v , t + dt );
150 stepper_base_type::m_algebra.for_each7( out , in , dxdt_in , m_k3.m_v , m_k4.m_v , m_k5.m_v , m_k6.m_v ,
151 typename operations_type::template scale_sum6< value_type , time_type , time_type , time_type , time_type , time_type >( 1.0 , dt*c1 , dt*c3 , dt*c4 , dt*c5 , dt*c6 ));
152
153 // the new derivative
154 sys( out , dxdt_out , t + dt );
155 }
156
157
158
159 template< class System , class StateIn , class DerivIn , class StateOut , class DerivOut , class Err >
160 void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt_in , time_type t ,
161 StateOut &out , DerivOut &dxdt_out , time_type dt , Err &xerr )
162 {
163 const value_type c1 = static_cast<value_type> ( 35 ) / static_cast<value_type>( 384 );
164 const value_type c3 = static_cast<value_type> ( 500 ) / static_cast<value_type>( 1113 );
165 const value_type c4 = static_cast<value_type> ( 125 ) / static_cast<value_type>( 192 );
166 const value_type c5 = static_cast<value_type> ( -2187 ) / static_cast<value_type>( 6784 );
167 const value_type c6 = static_cast<value_type> ( 11 ) / static_cast<value_type>( 84 );
168
169 const value_type dc1 = c1 - static_cast<value_type> ( 5179 ) / static_cast<value_type>( 57600 );
170 const value_type dc3 = c3 - static_cast<value_type> ( 7571 ) / static_cast<value_type>( 16695 );
171 const value_type dc4 = c4 - static_cast<value_type> ( 393 ) / static_cast<value_type>( 640 );
172 const value_type dc5 = c5 - static_cast<value_type> ( -92097 ) / static_cast<value_type>( 339200 );
173 const value_type dc6 = c6 - static_cast<value_type> ( 187 ) / static_cast<value_type>( 2100 );
174 const value_type dc7 = static_cast<value_type>( -1 ) / static_cast<value_type> ( 40 );
175
176 /* ToDo: copy only if &dxdt_in == &dxdt_out ? */
177 if( same_instance( dxdt_in , dxdt_out ) )
178 {
179 m_dxdt_tmp_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_dxdt_tmp_impl<StateIn> , detail::ref( *this ) , detail::_1 ) );
180 boost::numeric::odeint::copy( dxdt_in , m_dxdt_tmp.m_v );
181 do_step_impl( system , in , dxdt_in , t , out , dxdt_out , dt );
182 //error estimate
183 stepper_base_type::m_algebra.for_each7( xerr , m_dxdt_tmp.m_v , m_k3.m_v , m_k4.m_v , m_k5.m_v , m_k6.m_v , dxdt_out ,
184 typename operations_type::template scale_sum6< time_type , time_type , time_type , time_type , time_type , time_type >( dt*dc1 , dt*dc3 , dt*dc4 , dt*dc5 , dt*dc6 , dt*dc7 ) );
185
186 }
187 else
188 {
189 do_step_impl( system , in , dxdt_in , t , out , dxdt_out , dt );
190 //error estimate
191 stepper_base_type::m_algebra.for_each7( xerr , dxdt_in , m_k3.m_v , m_k4.m_v , m_k5.m_v , m_k6.m_v , dxdt_out ,
192 typename operations_type::template scale_sum6< time_type , time_type , time_type , time_type , time_type , time_type >( dt*dc1 , dt*dc3 , dt*dc4 , dt*dc5 , dt*dc6 , dt*dc7 ) );
193
194 }
195
196 }
197
198
199 /*
200 * Calculates Dense-Output for Dopri5
201 *
202 * See Hairer, Norsett, Wanner: Solving Ordinary Differential Equations, Nonstiff Problems. I, p.191/192
203 *
204 * y(t+theta) = y(t) + h * sum_i^7 b_i(theta) * k_i
205 *
206 * A = theta^2 * ( 3 - 2 theta )
207 * B = theta^2 * ( theta - 1 )
208 * C = theta^2 * ( theta - 1 )^2
209 * D = theta * ( theta - 1 )^2
210 *
211 * b_1( theta ) = A * b_1 - C * X1( theta ) + D
212 * b_2( theta ) = 0
213 * b_3( theta ) = A * b_3 + C * X3( theta )
214 * b_4( theta ) = A * b_4 - C * X4( theta )
215 * b_5( theta ) = A * b_5 + C * X5( theta )
216 * b_6( theta ) = A * b_6 - C * X6( theta )
217 * b_7( theta ) = B + C * X7( theta )
218 *
219 * An alternative Method is described in:
220 *
221 * www-m2.ma.tum.de/homepages/simeon/numerik3/kap3.ps
222 */
223 template< class StateOut , class StateIn1 , class DerivIn1 , class StateIn2 , class DerivIn2 >
224 void calc_state( time_type t , StateOut &x ,
225 const StateIn1 &x_old , const DerivIn1 &deriv_old , time_type t_old ,
226 const StateIn2 & /* x_new */ , const DerivIn2 &deriv_new , time_type t_new ) const
227 {
228 const value_type b1 = static_cast<value_type> ( 35 ) / static_cast<value_type>( 384 );
229 const value_type b3 = static_cast<value_type> ( 500 ) / static_cast<value_type>( 1113 );
230 const value_type b4 = static_cast<value_type> ( 125 ) / static_cast<value_type>( 192 );
231 const value_type b5 = static_cast<value_type> ( -2187 ) / static_cast<value_type>( 6784 );
232 const value_type b6 = static_cast<value_type> ( 11 ) / static_cast<value_type>( 84 );
233
234 const time_type dt = ( t_new - t_old );
235 const value_type theta = ( t - t_old ) / dt;
236 const value_type X1 = static_cast< value_type >( 5 ) * ( static_cast< value_type >( 2558722523LL ) - static_cast< value_type >( 31403016 ) * theta ) / static_cast< value_type >( 11282082432LL );
237 const value_type X3 = static_cast< value_type >( 100 ) * ( static_cast< value_type >( 882725551 ) - static_cast< value_type >( 15701508 ) * theta ) / static_cast< value_type >( 32700410799LL );
238 const value_type X4 = static_cast< value_type >( 25 ) * ( static_cast< value_type >( 443332067 ) - static_cast< value_type >( 31403016 ) * theta ) / static_cast< value_type >( 1880347072LL ) ;
239 const value_type X5 = static_cast< value_type >( 32805 ) * ( static_cast< value_type >( 23143187 ) - static_cast< value_type >( 3489224 ) * theta ) / static_cast< value_type >( 199316789632LL );
240 const value_type X6 = static_cast< value_type >( 55 ) * ( static_cast< value_type >( 29972135 ) - static_cast< value_type >( 7076736 ) * theta ) / static_cast< value_type >( 822651844 );
241 const value_type X7 = static_cast< value_type >( 10 ) * ( static_cast< value_type >( 7414447 ) - static_cast< value_type >( 829305 ) * theta ) / static_cast< value_type >( 29380423 );
242
243 const value_type theta_m_1 = theta - static_cast< value_type >( 1 );
244 const value_type theta_sq = theta * theta;
245 const value_type A = theta_sq * ( static_cast< value_type >( 3 ) - static_cast< value_type >( 2 ) * theta );
246 const value_type B = theta_sq * theta_m_1;
247 const value_type C = theta_sq * theta_m_1 * theta_m_1;
248 const value_type D = theta * theta_m_1 * theta_m_1;
249
250 const value_type b1_theta = A * b1 - C * X1 + D;
251 const value_type b3_theta = A * b3 + C * X3;
252 const value_type b4_theta = A * b4 - C * X4;
253 const value_type b5_theta = A * b5 + C * X5;
254 const value_type b6_theta = A * b6 - C * X6;
255 const value_type b7_theta = B + C * X7;
256
257 // const state_type &k1 = *m_old_deriv;
258 // const state_type &k3 = dopri5().m_k3;
259 // const state_type &k4 = dopri5().m_k4;
260 // const state_type &k5 = dopri5().m_k5;
261 // const state_type &k6 = dopri5().m_k6;
262 // const state_type &k7 = *m_current_deriv;
263
264 stepper_base_type::m_algebra.for_each8( x , x_old , deriv_old , m_k3.m_v , m_k4.m_v , m_k5.m_v , m_k6.m_v , deriv_new ,
265 typename operations_type::template scale_sum7< value_type , time_type , time_type , time_type , time_type , time_type , time_type >( 1.0 , dt * b1_theta , dt * b3_theta , dt * b4_theta , dt * b5_theta , dt * b6_theta , dt * b7_theta ) );
266 }
267
268
269 template< class StateIn >
270 void adjust_size( const StateIn &x )
271 {
272 resize_k_x_tmp_impl( x );
273 resize_dxdt_tmp_impl( x );
274 stepper_base_type::adjust_size( x );
275 }
276
277
278 private:
279
280 template< class StateIn >
281 bool resize_k_x_tmp_impl( const StateIn &x )
282 {
283 bool resized = false;
284 resized |= adjust_size_by_resizeability( m_x_tmp , x , typename is_resizeable<state_type>::type() );
285 resized |= adjust_size_by_resizeability( m_k2 , x , typename is_resizeable<deriv_type>::type() );
286 resized |= adjust_size_by_resizeability( m_k3 , x , typename is_resizeable<deriv_type>::type() );
287 resized |= adjust_size_by_resizeability( m_k4 , x , typename is_resizeable<deriv_type>::type() );
288 resized |= adjust_size_by_resizeability( m_k5 , x , typename is_resizeable<deriv_type>::type() );
289 resized |= adjust_size_by_resizeability( m_k6 , x , typename is_resizeable<deriv_type>::type() );
290 return resized;
291 }
292
293 template< class StateIn >
294 bool resize_dxdt_tmp_impl( const StateIn &x )
295 {
296 return adjust_size_by_resizeability( m_dxdt_tmp , x , typename is_resizeable<deriv_type>::type() );
297 }
298
299
300
301 wrapped_state_type m_x_tmp;
302 wrapped_deriv_type m_k2 , m_k3 , m_k4 , m_k5 , m_k6 ;
303 wrapped_deriv_type m_dxdt_tmp;
304 resizer_type m_k_x_tmp_resizer;
305 resizer_type m_dxdt_tmp_resizer;
306 };
307
308
309
310 /************* DOXYGEN ************/
311 /**
312 * \class runge_kutta_dopri5
313 * \brief The Runge-Kutta Dormand-Prince 5 method.
314 *
315 * The Runge-Kutta Dormand-Prince 5 method is a very popular method for solving ODEs, see
316 * <a href=""></a>.
317 * The method is explicit and fulfills the Error Stepper concept. Step size control
318 * is provided but continuous output is available which make this method favourable for many applications.
319 *
320 * This class derives from explicit_error_stepper_fsal_base and inherits its interface via CRTP (current recurring
321 * template pattern). The method possesses the FSAL (first-same-as-last) property. See
322 * explicit_error_stepper_fsal_base for more details.
323 *
324 * \tparam State The state type.
325 * \tparam Value The value type.
326 * \tparam Deriv The type representing the time derivative of the state.
327 * \tparam Time The time representing the independent variable - the time.
328 * \tparam Algebra The algebra type.
329 * \tparam Operations The operations type.
330 * \tparam Resizer The resizer policy type.
331 */
332
333
334 /**
335 * \fn runge_kutta_dopri5::runge_kutta_dopri5( const algebra_type &algebra )
336 * \brief Constructs the runge_kutta_dopri5 class. This constructor can be used as a default
337 * constructor if the algebra has a default constructor.
338 * \param algebra A copy of algebra is made and stored inside explicit_stepper_base.
339 */
340
341 /**
342 * \fn runge_kutta_dopri5::do_step_impl( System system , const StateIn &in , const DerivIn &dxdt_in , time_type t , StateOut &out , DerivOut &dxdt_out , time_type dt )
343 * \brief This method performs one step. The derivative `dxdt_in` of `in` at the time `t` is passed to the
344 * method. The result is updated out-of-place, hence the input is in `in` and the output in `out`. Furthermore,
345 * the derivative is update out-of-place, hence the input is assumed to be in `dxdt_in` and the output in
346 * `dxdt_out`.
347 * Access to this step functionality is provided by explicit_error_stepper_fsal_base and
348 * `do_step_impl` should not be called directly.
349 *
350 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
351 * Simple System concept.
352 * \param in The state of the ODE which should be solved. in is not modified in this method
353 * \param dxdt_in The derivative of x at t. dxdt_in is not modified by this method
354 * \param t The value of the time, at which the step should be performed.
355 * \param out The result of the step is written in out.
356 * \param dxdt_out The result of the new derivative at time t+dt.
357 * \param dt The step size.
358 */
359
360 /**
361 * \fn runge_kutta_dopri5::do_step_impl( System system , const StateIn &in , const DerivIn &dxdt_in , time_type t , StateOut &out , DerivOut &dxdt_out , time_type dt , Err &xerr )
362 * \brief This method performs one step. The derivative `dxdt_in` of `in` at the time `t` is passed to the
363 * method. The result is updated out-of-place, hence the input is in `in` and the output in `out`. Furthermore,
364 * the derivative is update out-of-place, hence the input is assumed to be in `dxdt_in` and the output in
365 * `dxdt_out`.
366 * Access to this step functionality is provided by explicit_error_stepper_fsal_base and
367 * `do_step_impl` should not be called directly.
368 * An estimation of the error is calculated.
369 *
370 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
371 * Simple System concept.
372 * \param in The state of the ODE which should be solved. in is not modified in this method
373 * \param dxdt_in The derivative of x at t. dxdt_in is not modified by this method
374 * \param t The value of the time, at which the step should be performed.
375 * \param out The result of the step is written in out.
376 * \param dxdt_out The result of the new derivative at time t+dt.
377 * \param dt The step size.
378 * \param xerr An estimation of the error.
379 */
380
381 /**
382 * \fn runge_kutta_dopri5::calc_state( time_type t , StateOut &x , const StateIn1 &x_old , const DerivIn1 &deriv_old , time_type t_old , const StateIn2 & , const DerivIn2 &deriv_new , time_type t_new ) const
383 * \brief This method is used for continuous output and it calculates the state `x` at a time `t` from the
384 * knowledge of two states `old_state` and `current_state` at time points `t_old` and `t_new`. It also uses
385 * internal variables to calculate the result. Hence this method must be called after two successful `do_step`
386 * calls.
387 */
388
389 /**
390 * \fn runge_kutta_dopri5::adjust_size( const StateIn &x )
391 * \brief Adjust the size of all temporaries in the stepper manually.
392 * \param x A state from which the size of the temporaries to be resized is deduced.
393 */
394
395 } // odeint
396 } // numeric
397 } // boost
398
399
400 #endif // BOOST_NUMERIC_ODEINT_STEPPER_RUNGE_KUTTA_DOPRI5_HPP_INCLUDED