Chris@16
|
1 /* [auto_generated]
|
Chris@16
|
2 boost/numeric/odeint/stepper/controlled_runge_kutta.hpp
|
Chris@16
|
3
|
Chris@16
|
4 [begin_description]
|
Chris@16
|
5 The default controlled stepper which can be used with all explicit Runge-Kutta error steppers.
|
Chris@16
|
6 [end_description]
|
Chris@16
|
7
|
Chris@101
|
8 Copyright 2010-2013 Karsten Ahnert
|
Chris@101
|
9 Copyright 2010-2013 Mario Mulansky
|
Chris@101
|
10 Copyright 2012 Christoph Koke
|
Chris@16
|
11
|
Chris@16
|
12 Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
13 (See accompanying file LICENSE_1_0.txt or
|
Chris@16
|
14 copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
15 */
|
Chris@16
|
16
|
Chris@16
|
17
|
Chris@16
|
18 #ifndef BOOST_NUMERIC_ODEINT_STEPPER_CONTROLLED_RUNGE_KUTTA_HPP_INCLUDED
|
Chris@16
|
19 #define BOOST_NUMERIC_ODEINT_STEPPER_CONTROLLED_RUNGE_KUTTA_HPP_INCLUDED
|
Chris@16
|
20
|
Chris@16
|
21
|
Chris@16
|
22
|
Chris@16
|
23 #include <cmath>
|
Chris@16
|
24
|
Chris@16
|
25 #include <boost/config.hpp>
|
Chris@16
|
26 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
27 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 #include <boost/numeric/odeint/util/bind.hpp>
|
Chris@16
|
30 #include <boost/numeric/odeint/util/unwrap_reference.hpp>
|
Chris@16
|
31 #include <boost/numeric/odeint/util/copy.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 #include <boost/numeric/odeint/util/state_wrapper.hpp>
|
Chris@16
|
34 #include <boost/numeric/odeint/util/is_resizeable.hpp>
|
Chris@16
|
35 #include <boost/numeric/odeint/util/resizer.hpp>
|
Chris@16
|
36
|
Chris@16
|
37 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
|
Chris@16
|
38 #include <boost/numeric/odeint/algebra/default_operations.hpp>
|
Chris@101
|
39 #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
|
Chris@16
|
40
|
Chris@16
|
41 #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
|
Chris@16
|
42 #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
|
Chris@16
|
43
|
Chris@16
|
44 namespace boost {
|
Chris@16
|
45 namespace numeric {
|
Chris@16
|
46 namespace odeint {
|
Chris@16
|
47
|
Chris@16
|
48
|
Chris@16
|
49 template
|
Chris@16
|
50 <
|
Chris@16
|
51 class Value ,
|
Chris@101
|
52 class Algebra ,
|
Chris@101
|
53 class Operations
|
Chris@16
|
54 >
|
Chris@16
|
55 class default_error_checker
|
Chris@16
|
56 {
|
Chris@16
|
57 public:
|
Chris@16
|
58
|
Chris@16
|
59 typedef Value value_type;
|
Chris@16
|
60 typedef Algebra algebra_type;
|
Chris@16
|
61 typedef Operations operations_type;
|
Chris@16
|
62
|
Chris@16
|
63 default_error_checker(
|
Chris@16
|
64 value_type eps_abs = static_cast< value_type >( 1.0e-6 ) ,
|
Chris@16
|
65 value_type eps_rel = static_cast< value_type >( 1.0e-6 ) ,
|
Chris@16
|
66 value_type a_x = static_cast< value_type >( 1 ) ,
|
Chris@16
|
67 value_type a_dxdt = static_cast< value_type >( 1 ) )
|
Chris@16
|
68 : m_eps_abs( eps_abs ) , m_eps_rel( eps_rel ) , m_a_x( a_x ) , m_a_dxdt( a_dxdt )
|
Chris@16
|
69 { }
|
Chris@16
|
70
|
Chris@16
|
71
|
Chris@16
|
72 template< class State , class Deriv , class Err , class Time >
|
Chris@16
|
73 value_type error( const State &x_old , const Deriv &dxdt_old , Err &x_err , Time dt ) const
|
Chris@16
|
74 {
|
Chris@16
|
75 return error( algebra_type() , x_old , dxdt_old , x_err , dt );
|
Chris@16
|
76 }
|
Chris@16
|
77
|
Chris@16
|
78 template< class State , class Deriv , class Err , class Time >
|
Chris@16
|
79 value_type error( algebra_type &algebra , const State &x_old , const Deriv &dxdt_old , Err &x_err , Time dt ) const
|
Chris@16
|
80 {
|
Chris@16
|
81 // this overwrites x_err !
|
Chris@16
|
82 algebra.for_each3( x_err , x_old , dxdt_old ,
|
Chris@16
|
83 typename operations_type::template rel_error< value_type >( m_eps_abs , m_eps_rel , m_a_x , m_a_dxdt * get_unit_value( dt ) ) );
|
Chris@16
|
84
|
Chris@101
|
85 // value_type res = algebra.reduce( x_err ,
|
Chris@101
|
86 // typename operations_type::template maximum< value_type >() , static_cast< value_type >( 0 ) );
|
Chris@101
|
87 return algebra.norm_inf( x_err );
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@16
|
90 private:
|
Chris@16
|
91
|
Chris@16
|
92 value_type m_eps_abs;
|
Chris@16
|
93 value_type m_eps_rel;
|
Chris@16
|
94 value_type m_a_x;
|
Chris@16
|
95 value_type m_a_dxdt;
|
Chris@16
|
96
|
Chris@16
|
97 };
|
Chris@16
|
98
|
Chris@16
|
99
|
Chris@16
|
100
|
Chris@16
|
101
|
Chris@16
|
102
|
Chris@16
|
103
|
Chris@16
|
104
|
Chris@16
|
105
|
Chris@16
|
106 /*
|
Chris@16
|
107 * error stepper category dispatcher
|
Chris@16
|
108 */
|
Chris@16
|
109 template<
|
Chris@16
|
110 class ErrorStepper ,
|
Chris@16
|
111 class ErrorChecker = default_error_checker< typename ErrorStepper::value_type ,
|
Chris@16
|
112 typename ErrorStepper::algebra_type ,
|
Chris@16
|
113 typename ErrorStepper::operations_type > ,
|
Chris@16
|
114 class Resizer = typename ErrorStepper::resizer_type ,
|
Chris@16
|
115 class ErrorStepperCategory = typename ErrorStepper::stepper_category
|
Chris@16
|
116 >
|
Chris@16
|
117 class controlled_runge_kutta ;
|
Chris@16
|
118
|
Chris@16
|
119
|
Chris@16
|
120
|
Chris@16
|
121 /*
|
Chris@16
|
122 * explicit stepper version
|
Chris@16
|
123 *
|
Chris@16
|
124 * this class introduces the following try_step overloads
|
Chris@16
|
125 * try_step( sys , x , t , dt )
|
Chris@16
|
126 * try_step( sys , x , dxdt , t , dt )
|
Chris@16
|
127 * try_step( sys , in , t , out , dt )
|
Chris@16
|
128 * try_step( sys , in , dxdt , t , out , dt )
|
Chris@16
|
129 */
|
Chris@16
|
130 /**
|
Chris@16
|
131 * \brief Implements step size control for Runge-Kutta steppers with error
|
Chris@16
|
132 * estimation.
|
Chris@16
|
133 *
|
Chris@16
|
134 * This class implements the step size control for standard Runge-Kutta
|
Chris@16
|
135 * steppers with error estimation.
|
Chris@16
|
136 *
|
Chris@16
|
137 * \tparam ErrorStepper The stepper type with error estimation, has to fulfill the ErrorStepper concept.
|
Chris@16
|
138 * \tparam ErrorChecker The error checker
|
Chris@16
|
139 * \tparam Resizer The resizer policy type.
|
Chris@16
|
140 */
|
Chris@16
|
141 template<
|
Chris@16
|
142 class ErrorStepper ,
|
Chris@16
|
143 class ErrorChecker ,
|
Chris@16
|
144 class Resizer
|
Chris@16
|
145 >
|
Chris@16
|
146 class controlled_runge_kutta< ErrorStepper , ErrorChecker , Resizer , explicit_error_stepper_tag >
|
Chris@16
|
147 {
|
Chris@16
|
148
|
Chris@16
|
149 public:
|
Chris@16
|
150
|
Chris@16
|
151 typedef ErrorStepper stepper_type;
|
Chris@16
|
152 typedef typename stepper_type::state_type state_type;
|
Chris@16
|
153 typedef typename stepper_type::value_type value_type;
|
Chris@16
|
154 typedef typename stepper_type::deriv_type deriv_type;
|
Chris@16
|
155 typedef typename stepper_type::time_type time_type;
|
Chris@16
|
156 typedef typename stepper_type::algebra_type algebra_type;
|
Chris@16
|
157 typedef typename stepper_type::operations_type operations_type;
|
Chris@16
|
158 typedef Resizer resizer_type;
|
Chris@16
|
159 typedef ErrorChecker error_checker_type;
|
Chris@16
|
160 typedef explicit_controlled_stepper_tag stepper_category;
|
Chris@16
|
161
|
Chris@16
|
162 #ifndef DOXYGEN_SKIP
|
Chris@16
|
163 typedef typename stepper_type::wrapped_state_type wrapped_state_type;
|
Chris@16
|
164 typedef typename stepper_type::wrapped_deriv_type wrapped_deriv_type;
|
Chris@16
|
165
|
Chris@16
|
166 typedef controlled_runge_kutta< ErrorStepper , ErrorChecker , Resizer , explicit_error_stepper_tag > controlled_stepper_type;
|
Chris@16
|
167 #endif //DOXYGEN_SKIP
|
Chris@16
|
168
|
Chris@16
|
169
|
Chris@16
|
170 /**
|
Chris@16
|
171 * \brief Constructs the controlled Runge-Kutta stepper.
|
Chris@16
|
172 * \param error_checker An instance of the error checker.
|
Chris@16
|
173 * \param stepper An instance of the underlying stepper.
|
Chris@16
|
174 */
|
Chris@16
|
175 controlled_runge_kutta(
|
Chris@16
|
176 const error_checker_type &error_checker = error_checker_type( ) ,
|
Chris@16
|
177 const stepper_type &stepper = stepper_type( )
|
Chris@16
|
178 )
|
Chris@16
|
179 : m_stepper( stepper ) , m_error_checker( error_checker )
|
Chris@16
|
180 { }
|
Chris@16
|
181
|
Chris@16
|
182
|
Chris@16
|
183
|
Chris@16
|
184 /*
|
Chris@16
|
185 * Version 1 : try_step( sys , x , t , dt )
|
Chris@16
|
186 *
|
Chris@16
|
187 * The overloads are needed to solve the forwarding problem
|
Chris@16
|
188 */
|
Chris@16
|
189 /**
|
Chris@16
|
190 * \brief Tries to perform one step.
|
Chris@16
|
191 *
|
Chris@16
|
192 * This method tries to do one step with step size dt. If the error estimate
|
Chris@16
|
193 * is to large, the step is rejected and the method returns fail and the
|
Chris@16
|
194 * step size dt is reduced. If the error estimate is acceptably small, the
|
Chris@16
|
195 * step is performed, success is returned and dt might be increased to make
|
Chris@16
|
196 * the steps as large as possible. This method also updates t if a step is
|
Chris@16
|
197 * performed.
|
Chris@16
|
198 *
|
Chris@16
|
199 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
|
Chris@16
|
200 * Simple System concept.
|
Chris@16
|
201 * \param x The state of the ODE which should be solved. Overwritten if
|
Chris@16
|
202 * the step is successful.
|
Chris@16
|
203 * \param t The value of the time. Updated if the step is successful.
|
Chris@16
|
204 * \param dt The step size. Updated.
|
Chris@16
|
205 * \return success if the step was accepted, fail otherwise.
|
Chris@16
|
206 */
|
Chris@16
|
207 template< class System , class StateInOut >
|
Chris@16
|
208 controlled_step_result try_step( System system , StateInOut &x , time_type &t , time_type &dt )
|
Chris@16
|
209 {
|
Chris@16
|
210 return try_step_v1( system , x , t, dt );
|
Chris@16
|
211 }
|
Chris@16
|
212
|
Chris@16
|
213 /**
|
Chris@16
|
214 * \brief Tries to perform one step. Solves the forwarding problem and
|
Chris@16
|
215 * allows for using boost range as state_type.
|
Chris@16
|
216 *
|
Chris@16
|
217 * This method tries to do one step with step size dt. If the error estimate
|
Chris@16
|
218 * is to large, the step is rejected and the method returns fail and the
|
Chris@16
|
219 * step size dt is reduced. If the error estimate is acceptably small, the
|
Chris@16
|
220 * step is performed, success is returned and dt might be increased to make
|
Chris@16
|
221 * the steps as large as possible. This method also updates t if a step is
|
Chris@16
|
222 * performed.
|
Chris@16
|
223 *
|
Chris@16
|
224 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
|
Chris@16
|
225 * Simple System concept.
|
Chris@16
|
226 * \param x The state of the ODE which should be solved. Overwritten if
|
Chris@16
|
227 * the step is successful. Can be a boost range.
|
Chris@16
|
228 * \param t The value of the time. Updated if the step is successful.
|
Chris@16
|
229 * \param dt The step size. Updated.
|
Chris@16
|
230 * \return success if the step was accepted, fail otherwise.
|
Chris@16
|
231 */
|
Chris@16
|
232 template< class System , class StateInOut >
|
Chris@16
|
233 controlled_step_result try_step( System system , const StateInOut &x , time_type &t , time_type &dt )
|
Chris@16
|
234 {
|
Chris@16
|
235 return try_step_v1( system , x , t, dt );
|
Chris@16
|
236 }
|
Chris@16
|
237
|
Chris@16
|
238
|
Chris@16
|
239
|
Chris@16
|
240 /*
|
Chris@16
|
241 * Version 2 : try_step( sys , x , dxdt , t , dt )
|
Chris@16
|
242 *
|
Chris@16
|
243 * this version does not solve the forwarding problem, boost.range can not be used
|
Chris@16
|
244 */
|
Chris@16
|
245 /**
|
Chris@16
|
246 * \brief Tries to perform one step.
|
Chris@16
|
247 *
|
Chris@16
|
248 * This method tries to do one step with step size dt. If the error estimate
|
Chris@16
|
249 * is to large, the step is rejected and the method returns fail and the
|
Chris@16
|
250 * step size dt is reduced. If the error estimate is acceptably small, the
|
Chris@16
|
251 * step is performed, success is returned and dt might be increased to make
|
Chris@16
|
252 * the steps as large as possible. This method also updates t if a step is
|
Chris@16
|
253 * performed.
|
Chris@16
|
254 *
|
Chris@16
|
255 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
|
Chris@16
|
256 * Simple System concept.
|
Chris@16
|
257 * \param x The state of the ODE which should be solved. Overwritten if
|
Chris@16
|
258 * the step is successful.
|
Chris@16
|
259 * \param dxdt The derivative of state.
|
Chris@16
|
260 * \param t The value of the time. Updated if the step is successful.
|
Chris@16
|
261 * \param dt The step size. Updated.
|
Chris@16
|
262 * \return success if the step was accepted, fail otherwise.
|
Chris@16
|
263 */
|
Chris@16
|
264 template< class System , class StateInOut , class DerivIn >
|
Chris@16
|
265 controlled_step_result try_step( System system , StateInOut &x , const DerivIn &dxdt , time_type &t , time_type &dt )
|
Chris@16
|
266 {
|
Chris@16
|
267 m_xnew_resizer.adjust_size( x , detail::bind( &controlled_runge_kutta::template resize_m_xnew_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
|
Chris@16
|
268 controlled_step_result res = try_step( system , x , dxdt , t , m_xnew.m_v , dt );
|
Chris@16
|
269 if( res == success )
|
Chris@16
|
270 {
|
Chris@16
|
271 boost::numeric::odeint::copy( m_xnew.m_v , x );
|
Chris@16
|
272 }
|
Chris@16
|
273 return res;
|
Chris@16
|
274 }
|
Chris@16
|
275
|
Chris@16
|
276 /*
|
Chris@16
|
277 * Version 3 : try_step( sys , in , t , out , dt )
|
Chris@16
|
278 *
|
Chris@16
|
279 * this version does not solve the forwarding problem, boost.range can not be used
|
Chris@16
|
280 *
|
Chris@16
|
281 * the disable is needed to avoid ambiguous overloads if state_type = time_type
|
Chris@16
|
282 */
|
Chris@16
|
283 /**
|
Chris@16
|
284 * \brief Tries to perform one step.
|
Chris@16
|
285 *
|
Chris@16
|
286 * \note This method is disabled if state_type=time_type to avoid ambiguity.
|
Chris@16
|
287 *
|
Chris@16
|
288 * This method tries to do one step with step size dt. If the error estimate
|
Chris@16
|
289 * is to large, the step is rejected and the method returns fail and the
|
Chris@16
|
290 * step size dt is reduced. If the error estimate is acceptably small, the
|
Chris@16
|
291 * step is performed, success is returned and dt might be increased to make
|
Chris@16
|
292 * the steps as large as possible. This method also updates t if a step is
|
Chris@16
|
293 * performed.
|
Chris@16
|
294 *
|
Chris@16
|
295 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
|
Chris@16
|
296 * Simple System concept.
|
Chris@16
|
297 * \param in The state of the ODE which should be solved.
|
Chris@16
|
298 * \param t The value of the time. Updated if the step is successful.
|
Chris@16
|
299 * \param out Used to store the result of the step.
|
Chris@16
|
300 * \param dt The step size. Updated.
|
Chris@16
|
301 * \return success if the step was accepted, fail otherwise.
|
Chris@16
|
302 */
|
Chris@16
|
303 template< class System , class StateIn , class StateOut >
|
Chris@16
|
304 typename boost::disable_if< boost::is_same< StateIn , time_type > , controlled_step_result >::type
|
Chris@16
|
305 try_step( System system , const StateIn &in , time_type &t , StateOut &out , time_type &dt )
|
Chris@16
|
306 {
|
Chris@16
|
307 typename odeint::unwrap_reference< System >::type &sys = system;
|
Chris@16
|
308 m_dxdt_resizer.adjust_size( in , detail::bind( &controlled_runge_kutta::template resize_m_dxdt_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
|
Chris@16
|
309 sys( in , m_dxdt.m_v , t );
|
Chris@16
|
310 return try_step( system , in , m_dxdt.m_v , t , out , dt );
|
Chris@16
|
311 }
|
Chris@16
|
312
|
Chris@16
|
313
|
Chris@16
|
314 /*
|
Chris@16
|
315 * Version 4 : try_step( sys , in , dxdt , t , out , dt )
|
Chris@16
|
316 *
|
Chris@16
|
317 * this version does not solve the forwarding problem, boost.range can not be used
|
Chris@16
|
318 */
|
Chris@16
|
319 /**
|
Chris@16
|
320 * \brief Tries to perform one step.
|
Chris@16
|
321 *
|
Chris@16
|
322 * This method tries to do one step with step size dt. If the error estimate
|
Chris@16
|
323 * is to large, the step is rejected and the method returns fail and the
|
Chris@16
|
324 * step size dt is reduced. If the error estimate is acceptably small, the
|
Chris@16
|
325 * step is performed, success is returned and dt might be increased to make
|
Chris@16
|
326 * the steps as large as possible. This method also updates t if a step is
|
Chris@16
|
327 * performed.
|
Chris@16
|
328 *
|
Chris@16
|
329 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
|
Chris@16
|
330 * Simple System concept.
|
Chris@16
|
331 * \param in The state of the ODE which should be solved.
|
Chris@16
|
332 * \param dxdt The derivative of state.
|
Chris@16
|
333 * \param t The value of the time. Updated if the step is successful.
|
Chris@16
|
334 * \param out Used to store the result of the step.
|
Chris@16
|
335 * \param dt The step size. Updated.
|
Chris@16
|
336 * \return success if the step was accepted, fail otherwise.
|
Chris@16
|
337 */
|
Chris@16
|
338 template< class System , class StateIn , class DerivIn , class StateOut >
|
Chris@16
|
339 controlled_step_result try_step( System system , const StateIn &in , const DerivIn &dxdt , time_type &t , StateOut &out , time_type &dt )
|
Chris@16
|
340 {
|
Chris@16
|
341 BOOST_USING_STD_MIN();
|
Chris@16
|
342 BOOST_USING_STD_MAX();
|
Chris@16
|
343 using std::pow;
|
Chris@16
|
344
|
Chris@16
|
345 m_xerr_resizer.adjust_size( in , detail::bind( &controlled_runge_kutta::template resize_m_xerr_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
|
Chris@16
|
346
|
Chris@16
|
347 // do one step with error calculation
|
Chris@16
|
348 m_stepper.do_step( system , in , dxdt , t , out , dt , m_xerr.m_v );
|
Chris@16
|
349
|
Chris@16
|
350 m_max_rel_error = m_error_checker.error( m_stepper.algebra() , in , dxdt , m_xerr.m_v , dt );
|
Chris@16
|
351
|
Chris@16
|
352 if( m_max_rel_error > 1.0 )
|
Chris@16
|
353 {
|
Chris@16
|
354 // error too large - decrease dt ,limit scaling factor to 0.2 and reset state
|
Chris@101
|
355 dt *= max BOOST_PREVENT_MACRO_SUBSTITUTION ( static_cast<value_type>( static_cast<value_type>(9)/static_cast<value_type>(10) *
|
Chris@101
|
356 pow( m_max_rel_error , static_cast<value_type>(-1) / ( m_stepper.error_order() - 1 ) ) ) ,
|
Chris@101
|
357 static_cast<value_type>( static_cast<value_type>(1)/static_cast<value_type> (5) ) );
|
Chris@16
|
358 return fail;
|
Chris@16
|
359 }
|
Chris@16
|
360 else
|
Chris@16
|
361 {
|
Chris@16
|
362 if( m_max_rel_error < 0.5 )
|
Chris@16
|
363 {
|
Chris@16
|
364 // error should be > 0
|
Chris@101
|
365 m_max_rel_error = max BOOST_PREVENT_MACRO_SUBSTITUTION (
|
Chris@101
|
366 static_cast<value_type>( pow( static_cast<value_type>(5.0) , -static_cast<value_type>(m_stepper.stepper_order()) ) ) ,
|
Chris@101
|
367 m_max_rel_error );
|
Chris@16
|
368 //error too small - increase dt and keep the evolution and limit scaling factor to 5.0
|
Chris@16
|
369 t += dt;
|
Chris@16
|
370 dt *= static_cast<value_type>(9)/static_cast<value_type>(10) * pow( m_max_rel_error ,
|
Chris@16
|
371 static_cast<value_type>(-1) / m_stepper.stepper_order() );
|
Chris@16
|
372 return success;
|
Chris@16
|
373 }
|
Chris@16
|
374 else
|
Chris@16
|
375 {
|
Chris@16
|
376 t += dt;
|
Chris@16
|
377 return success;
|
Chris@16
|
378 }
|
Chris@16
|
379 }
|
Chris@16
|
380 }
|
Chris@16
|
381
|
Chris@16
|
382 /**
|
Chris@16
|
383 * \brief Returns the error of the last step.
|
Chris@16
|
384 *
|
Chris@16
|
385 * returns The last error of the step.
|
Chris@16
|
386 */
|
Chris@16
|
387 value_type last_error( void ) const
|
Chris@16
|
388 {
|
Chris@16
|
389 return m_max_rel_error;
|
Chris@16
|
390 }
|
Chris@16
|
391
|
Chris@16
|
392
|
Chris@16
|
393 /**
|
Chris@16
|
394 * \brief Adjust the size of all temporaries in the stepper manually.
|
Chris@16
|
395 * \param x A state from which the size of the temporaries to be resized is deduced.
|
Chris@16
|
396 */
|
Chris@16
|
397 template< class StateType >
|
Chris@16
|
398 void adjust_size( const StateType &x )
|
Chris@16
|
399 {
|
Chris@16
|
400 resize_m_xerr_impl( x );
|
Chris@16
|
401 resize_m_dxdt_impl( x );
|
Chris@16
|
402 resize_m_xnew_impl( x );
|
Chris@16
|
403 m_stepper.adjust_size( x );
|
Chris@16
|
404 }
|
Chris@16
|
405
|
Chris@16
|
406 /**
|
Chris@16
|
407 * \brief Returns the instance of the underlying stepper.
|
Chris@16
|
408 * \returns The instance of the underlying stepper.
|
Chris@16
|
409 */
|
Chris@16
|
410 stepper_type& stepper( void )
|
Chris@16
|
411 {
|
Chris@16
|
412 return m_stepper;
|
Chris@16
|
413 }
|
Chris@16
|
414
|
Chris@16
|
415 /**
|
Chris@16
|
416 * \brief Returns the instance of the underlying stepper.
|
Chris@16
|
417 * \returns The instance of the underlying stepper.
|
Chris@16
|
418 */
|
Chris@16
|
419 const stepper_type& stepper( void ) const
|
Chris@16
|
420 {
|
Chris@16
|
421 return m_stepper;
|
Chris@16
|
422 }
|
Chris@16
|
423
|
Chris@16
|
424 private:
|
Chris@16
|
425
|
Chris@16
|
426
|
Chris@16
|
427 template< class System , class StateInOut >
|
Chris@16
|
428 controlled_step_result try_step_v1( System system , StateInOut &x , time_type &t , time_type &dt )
|
Chris@16
|
429 {
|
Chris@16
|
430 typename odeint::unwrap_reference< System >::type &sys = system;
|
Chris@16
|
431 m_dxdt_resizer.adjust_size( x , detail::bind( &controlled_runge_kutta::template resize_m_dxdt_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
|
Chris@16
|
432 sys( x , m_dxdt.m_v ,t );
|
Chris@16
|
433 return try_step( system , x , m_dxdt.m_v , t , dt );
|
Chris@16
|
434 }
|
Chris@16
|
435
|
Chris@16
|
436 template< class StateIn >
|
Chris@16
|
437 bool resize_m_xerr_impl( const StateIn &x )
|
Chris@16
|
438 {
|
Chris@16
|
439 return adjust_size_by_resizeability( m_xerr , x , typename is_resizeable<state_type>::type() );
|
Chris@16
|
440 }
|
Chris@16
|
441
|
Chris@16
|
442 template< class StateIn >
|
Chris@16
|
443 bool resize_m_dxdt_impl( const StateIn &x )
|
Chris@16
|
444 {
|
Chris@16
|
445 return adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable<deriv_type>::type() );
|
Chris@16
|
446 }
|
Chris@16
|
447
|
Chris@16
|
448 template< class StateIn >
|
Chris@16
|
449 bool resize_m_xnew_impl( const StateIn &x )
|
Chris@16
|
450 {
|
Chris@16
|
451 return adjust_size_by_resizeability( m_xnew , x , typename is_resizeable<state_type>::type() );
|
Chris@16
|
452 }
|
Chris@16
|
453
|
Chris@16
|
454
|
Chris@16
|
455
|
Chris@16
|
456 stepper_type m_stepper;
|
Chris@16
|
457 error_checker_type m_error_checker;
|
Chris@16
|
458
|
Chris@16
|
459 resizer_type m_dxdt_resizer;
|
Chris@16
|
460 resizer_type m_xerr_resizer;
|
Chris@16
|
461 resizer_type m_xnew_resizer;
|
Chris@16
|
462
|
Chris@16
|
463 wrapped_deriv_type m_dxdt;
|
Chris@16
|
464 wrapped_state_type m_xerr;
|
Chris@16
|
465 wrapped_state_type m_xnew;
|
Chris@16
|
466 value_type m_max_rel_error;
|
Chris@16
|
467 };
|
Chris@16
|
468
|
Chris@16
|
469
|
Chris@16
|
470
|
Chris@16
|
471
|
Chris@16
|
472
|
Chris@16
|
473
|
Chris@16
|
474
|
Chris@16
|
475
|
Chris@16
|
476
|
Chris@16
|
477
|
Chris@16
|
478 /*
|
Chris@16
|
479 * explicit stepper fsal version
|
Chris@16
|
480 *
|
Chris@16
|
481 * the class introduces the following try_step overloads
|
Chris@16
|
482 * try_step( sys , x , t , dt )
|
Chris@16
|
483 * try_step( sys , in , t , out , dt )
|
Chris@16
|
484 * try_step( sys , x , dxdt , t , dt )
|
Chris@16
|
485 * try_step( sys , in , dxdt_in , t , out , dxdt_out , dt )
|
Chris@16
|
486 */
|
Chris@16
|
487 /**
|
Chris@16
|
488 * \brief Implements step size control for Runge-Kutta FSAL steppers with
|
Chris@16
|
489 * error estimation.
|
Chris@16
|
490 *
|
Chris@16
|
491 * This class implements the step size control for FSAL Runge-Kutta
|
Chris@16
|
492 * steppers with error estimation.
|
Chris@16
|
493 *
|
Chris@16
|
494 * \tparam ErrorStepper The stepper type with error estimation, has to fulfill the ErrorStepper concept.
|
Chris@16
|
495 * \tparam ErrorChecker The error checker
|
Chris@16
|
496 * \tparam Resizer The resizer policy type.
|
Chris@16
|
497 */
|
Chris@16
|
498 template<
|
Chris@16
|
499 class ErrorStepper ,
|
Chris@16
|
500 class ErrorChecker ,
|
Chris@16
|
501 class Resizer
|
Chris@16
|
502 >
|
Chris@16
|
503 class controlled_runge_kutta< ErrorStepper , ErrorChecker , Resizer , explicit_error_stepper_fsal_tag >
|
Chris@16
|
504 {
|
Chris@16
|
505
|
Chris@16
|
506 public:
|
Chris@16
|
507
|
Chris@16
|
508 typedef ErrorStepper stepper_type;
|
Chris@16
|
509 typedef typename stepper_type::state_type state_type;
|
Chris@16
|
510 typedef typename stepper_type::value_type value_type;
|
Chris@16
|
511 typedef typename stepper_type::deriv_type deriv_type;
|
Chris@16
|
512 typedef typename stepper_type::time_type time_type;
|
Chris@16
|
513 typedef typename stepper_type::algebra_type algebra_type;
|
Chris@16
|
514 typedef typename stepper_type::operations_type operations_type;
|
Chris@16
|
515 typedef Resizer resizer_type;
|
Chris@16
|
516 typedef ErrorChecker error_checker_type;
|
Chris@16
|
517 typedef explicit_controlled_stepper_fsal_tag stepper_category;
|
Chris@16
|
518
|
Chris@16
|
519 #ifndef DOXYGEN_SKIP
|
Chris@16
|
520 typedef typename stepper_type::wrapped_state_type wrapped_state_type;
|
Chris@16
|
521 typedef typename stepper_type::wrapped_deriv_type wrapped_deriv_type;
|
Chris@16
|
522
|
Chris@16
|
523 typedef controlled_runge_kutta< ErrorStepper , ErrorChecker , Resizer , explicit_error_stepper_tag > controlled_stepper_type;
|
Chris@16
|
524 #endif // DOXYGEN_SKIP
|
Chris@16
|
525
|
Chris@16
|
526 /**
|
Chris@16
|
527 * \brief Constructs the controlled Runge-Kutta stepper.
|
Chris@16
|
528 * \param error_checker An instance of the error checker.
|
Chris@16
|
529 * \param stepper An instance of the underlying stepper.
|
Chris@16
|
530 */
|
Chris@16
|
531 controlled_runge_kutta(
|
Chris@16
|
532 const error_checker_type &error_checker = error_checker_type() ,
|
Chris@16
|
533 const stepper_type &stepper = stepper_type()
|
Chris@16
|
534 )
|
Chris@16
|
535 : m_stepper( stepper ) , m_error_checker( error_checker ) ,
|
Chris@16
|
536 m_first_call( true )
|
Chris@16
|
537 { }
|
Chris@16
|
538
|
Chris@16
|
539 /*
|
Chris@16
|
540 * Version 1 : try_step( sys , x , t , dt )
|
Chris@16
|
541 *
|
Chris@16
|
542 * The two overloads are needed in order to solve the forwarding problem
|
Chris@16
|
543 */
|
Chris@16
|
544 /**
|
Chris@16
|
545 * \brief Tries to perform one step.
|
Chris@16
|
546 *
|
Chris@16
|
547 * This method tries to do one step with step size dt. If the error estimate
|
Chris@16
|
548 * is to large, the step is rejected and the method returns fail and the
|
Chris@16
|
549 * step size dt is reduced. If the error estimate is acceptably small, the
|
Chris@16
|
550 * step is performed, success is returned and dt might be increased to make
|
Chris@16
|
551 * the steps as large as possible. This method also updates t if a step is
|
Chris@16
|
552 * performed.
|
Chris@16
|
553 *
|
Chris@16
|
554 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
|
Chris@16
|
555 * Simple System concept.
|
Chris@16
|
556 * \param x The state of the ODE which should be solved. Overwritten if
|
Chris@16
|
557 * the step is successful.
|
Chris@16
|
558 * \param t The value of the time. Updated if the step is successful.
|
Chris@16
|
559 * \param dt The step size. Updated.
|
Chris@16
|
560 * \return success if the step was accepted, fail otherwise.
|
Chris@16
|
561 */
|
Chris@16
|
562 template< class System , class StateInOut >
|
Chris@16
|
563 controlled_step_result try_step( System system , StateInOut &x , time_type &t , time_type &dt )
|
Chris@16
|
564 {
|
Chris@16
|
565 return try_step_v1( system , x , t , dt );
|
Chris@16
|
566 }
|
Chris@16
|
567
|
Chris@16
|
568
|
Chris@16
|
569 /**
|
Chris@16
|
570 * \brief Tries to perform one step. Solves the forwarding problem and
|
Chris@16
|
571 * allows for using boost range as state_type.
|
Chris@16
|
572 *
|
Chris@16
|
573 * This method tries to do one step with step size dt. If the error estimate
|
Chris@16
|
574 * is to large, the step is rejected and the method returns fail and the
|
Chris@16
|
575 * step size dt is reduced. If the error estimate is acceptably small, the
|
Chris@16
|
576 * step is performed, success is returned and dt might be increased to make
|
Chris@16
|
577 * the steps as large as possible. This method also updates t if a step is
|
Chris@16
|
578 * performed.
|
Chris@16
|
579 *
|
Chris@16
|
580 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
|
Chris@16
|
581 * Simple System concept.
|
Chris@16
|
582 * \param x The state of the ODE which should be solved. Overwritten if
|
Chris@16
|
583 * the step is successful. Can be a boost range.
|
Chris@16
|
584 * \param t The value of the time. Updated if the step is successful.
|
Chris@16
|
585 * \param dt The step size. Updated.
|
Chris@16
|
586 * \return success if the step was accepted, fail otherwise.
|
Chris@16
|
587 */
|
Chris@16
|
588 template< class System , class StateInOut >
|
Chris@16
|
589 controlled_step_result try_step( System system , const StateInOut &x , time_type &t , time_type &dt )
|
Chris@16
|
590 {
|
Chris@16
|
591 return try_step_v1( system , x , t , dt );
|
Chris@16
|
592 }
|
Chris@16
|
593
|
Chris@16
|
594
|
Chris@16
|
595
|
Chris@16
|
596 /*
|
Chris@16
|
597 * Version 2 : try_step( sys , in , t , out , dt );
|
Chris@16
|
598 *
|
Chris@16
|
599 * This version does not solve the forwarding problem, boost::range can not be used.
|
Chris@16
|
600 *
|
Chris@16
|
601 * The disabler is needed to solve ambiguous overloads
|
Chris@16
|
602 */
|
Chris@16
|
603 /**
|
Chris@16
|
604 * \brief Tries to perform one step.
|
Chris@16
|
605 *
|
Chris@16
|
606 * \note This method is disabled if state_type=time_type to avoid ambiguity.
|
Chris@16
|
607 *
|
Chris@16
|
608 * This method tries to do one step with step size dt. If the error estimate
|
Chris@16
|
609 * is to large, the step is rejected and the method returns fail and the
|
Chris@16
|
610 * step size dt is reduced. If the error estimate is acceptably small, the
|
Chris@16
|
611 * step is performed, success is returned and dt might be increased to make
|
Chris@16
|
612 * the steps as large as possible. This method also updates t if a step is
|
Chris@16
|
613 * performed.
|
Chris@16
|
614 *
|
Chris@16
|
615 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
|
Chris@16
|
616 * Simple System concept.
|
Chris@16
|
617 * \param in The state of the ODE which should be solved.
|
Chris@16
|
618 * \param t The value of the time. Updated if the step is successful.
|
Chris@16
|
619 * \param out Used to store the result of the step.
|
Chris@16
|
620 * \param dt The step size. Updated.
|
Chris@16
|
621 * \return success if the step was accepted, fail otherwise.
|
Chris@16
|
622 */
|
Chris@16
|
623 template< class System , class StateIn , class StateOut >
|
Chris@16
|
624 typename boost::disable_if< boost::is_same< StateIn , time_type > , controlled_step_result >::type
|
Chris@16
|
625 try_step( System system , const StateIn &in , time_type &t , StateOut &out , time_type &dt )
|
Chris@16
|
626 {
|
Chris@16
|
627 if( m_dxdt_resizer.adjust_size( in , detail::bind( &controlled_runge_kutta::template resize_m_dxdt_impl< StateIn > , detail::ref( *this ) , detail::_1 ) ) || m_first_call )
|
Chris@16
|
628 {
|
Chris@16
|
629 initialize( system , in , t );
|
Chris@16
|
630 }
|
Chris@16
|
631 return try_step( system , in , m_dxdt.m_v , t , out , dt );
|
Chris@16
|
632 }
|
Chris@16
|
633
|
Chris@16
|
634
|
Chris@16
|
635 /*
|
Chris@16
|
636 * Version 3 : try_step( sys , x , dxdt , t , dt )
|
Chris@16
|
637 *
|
Chris@16
|
638 * This version does not solve the forwarding problem, boost::range can not be used.
|
Chris@16
|
639 */
|
Chris@16
|
640 /**
|
Chris@16
|
641 * \brief Tries to perform one step.
|
Chris@16
|
642 *
|
Chris@16
|
643 * This method tries to do one step with step size dt. If the error estimate
|
Chris@16
|
644 * is to large, the step is rejected and the method returns fail and the
|
Chris@16
|
645 * step size dt is reduced. If the error estimate is acceptably small, the
|
Chris@16
|
646 * step is performed, success is returned and dt might be increased to make
|
Chris@16
|
647 * the steps as large as possible. This method also updates t if a step is
|
Chris@16
|
648 * performed.
|
Chris@16
|
649 *
|
Chris@16
|
650 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
|
Chris@16
|
651 * Simple System concept.
|
Chris@16
|
652 * \param x The state of the ODE which should be solved. Overwritten if
|
Chris@16
|
653 * the step is successful.
|
Chris@16
|
654 * \param dxdt The derivative of state.
|
Chris@16
|
655 * \param t The value of the time. Updated if the step is successful.
|
Chris@16
|
656 * \param dt The step size. Updated.
|
Chris@16
|
657 * \return success if the step was accepted, fail otherwise.
|
Chris@16
|
658 */
|
Chris@16
|
659 template< class System , class StateInOut , class DerivInOut >
|
Chris@16
|
660 controlled_step_result try_step( System system , StateInOut &x , DerivInOut &dxdt , time_type &t , time_type &dt )
|
Chris@16
|
661 {
|
Chris@16
|
662 m_xnew_resizer.adjust_size( x , detail::bind( &controlled_runge_kutta::template resize_m_xnew_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
|
Chris@16
|
663 m_dxdt_new_resizer.adjust_size( x , detail::bind( &controlled_runge_kutta::template resize_m_dxdt_new_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) );
|
Chris@16
|
664 controlled_step_result res = try_step( system , x , dxdt , t , m_xnew.m_v , m_dxdtnew.m_v , dt );
|
Chris@16
|
665 if( res == success )
|
Chris@16
|
666 {
|
Chris@16
|
667 boost::numeric::odeint::copy( m_xnew.m_v , x );
|
Chris@16
|
668 boost::numeric::odeint::copy( m_dxdtnew.m_v , dxdt );
|
Chris@16
|
669 }
|
Chris@16
|
670 return res;
|
Chris@16
|
671 }
|
Chris@16
|
672
|
Chris@16
|
673
|
Chris@16
|
674 /*
|
Chris@16
|
675 * Version 4 : try_step( sys , in , dxdt_in , t , out , dxdt_out , dt )
|
Chris@16
|
676 *
|
Chris@16
|
677 * This version does not solve the forwarding problem, boost::range can not be used.
|
Chris@16
|
678 */
|
Chris@16
|
679 /**
|
Chris@16
|
680 * \brief Tries to perform one step.
|
Chris@16
|
681 *
|
Chris@16
|
682 * This method tries to do one step with step size dt. If the error estimate
|
Chris@16
|
683 * is to large, the step is rejected and the method returns fail and the
|
Chris@16
|
684 * step size dt is reduced. If the error estimate is acceptably small, the
|
Chris@16
|
685 * step is performed, success is returned and dt might be increased to make
|
Chris@16
|
686 * the steps as large as possible. This method also updates t if a step is
|
Chris@16
|
687 * performed.
|
Chris@16
|
688 *
|
Chris@16
|
689 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
|
Chris@16
|
690 * Simple System concept.
|
Chris@16
|
691 * \param in The state of the ODE which should be solved.
|
Chris@16
|
692 * \param dxdt The derivative of state.
|
Chris@16
|
693 * \param t The value of the time. Updated if the step is successful.
|
Chris@16
|
694 * \param out Used to store the result of the step.
|
Chris@16
|
695 * \param dt The step size. Updated.
|
Chris@16
|
696 * \return success if the step was accepted, fail otherwise.
|
Chris@16
|
697 */
|
Chris@16
|
698 template< class System , class StateIn , class DerivIn , class StateOut , class DerivOut >
|
Chris@16
|
699 controlled_step_result try_step( System system , const StateIn &in , const DerivIn &dxdt_in , time_type &t ,
|
Chris@16
|
700 StateOut &out , DerivOut &dxdt_out , time_type &dt )
|
Chris@16
|
701 {
|
Chris@16
|
702 BOOST_USING_STD_MIN();
|
Chris@16
|
703 BOOST_USING_STD_MAX();
|
Chris@16
|
704
|
Chris@16
|
705 using std::pow;
|
Chris@16
|
706
|
Chris@16
|
707 m_xerr_resizer.adjust_size( in , detail::bind( &controlled_runge_kutta::template resize_m_xerr_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
|
Chris@16
|
708
|
Chris@16
|
709 //fsal: m_stepper.get_dxdt( dxdt );
|
Chris@16
|
710 //fsal: m_stepper.do_step( sys , x , dxdt , t , dt , m_x_err );
|
Chris@16
|
711 m_stepper.do_step( system , in , dxdt_in , t , out , dxdt_out , dt , m_xerr.m_v );
|
Chris@16
|
712
|
Chris@16
|
713 // this potentially overwrites m_x_err! (standard_error_checker does, at least)
|
Chris@16
|
714 value_type max_rel_err = m_error_checker.error( m_stepper.algebra() , in , dxdt_in , m_xerr.m_v , dt );
|
Chris@16
|
715
|
Chris@16
|
716 if( max_rel_err > 1.0 )
|
Chris@16
|
717 {
|
Chris@16
|
718 // error too large - decrease dt ,limit scaling factor to 0.2 and reset state
|
Chris@101
|
719 dt *= max BOOST_PREVENT_MACRO_SUBSTITUTION ( static_cast<value_type>( static_cast<value_type>(9)/static_cast<value_type>(10) *
|
Chris@101
|
720 pow( max_rel_err , static_cast<value_type>(-1) / ( m_stepper.error_order() - 1 ) ) ) ,
|
Chris@101
|
721 static_cast<value_type>( static_cast<value_type>(1)/static_cast<value_type> (5)) );
|
Chris@16
|
722 return fail;
|
Chris@16
|
723 }
|
Chris@16
|
724 else
|
Chris@16
|
725 {
|
Chris@16
|
726 if( max_rel_err < 0.5 )
|
Chris@16
|
727 { //error too small - increase dt and keep the evolution and limit scaling factor to 5.0
|
Chris@16
|
728 // error should be > 0
|
Chris@101
|
729 max_rel_err = max BOOST_PREVENT_MACRO_SUBSTITUTION ( static_cast<value_type>( pow( static_cast<value_type>(5.0) , -static_cast<value_type>(m_stepper.stepper_order()) ) ) ,
|
Chris@101
|
730 max_rel_err );
|
Chris@16
|
731 t += dt;
|
Chris@16
|
732 dt *= static_cast<value_type>( static_cast<value_type>(9)/static_cast<value_type>(10) * pow( max_rel_err , static_cast<value_type>(-1) / m_stepper.stepper_order() ) );
|
Chris@16
|
733 return success;
|
Chris@16
|
734 }
|
Chris@16
|
735 else
|
Chris@16
|
736 {
|
Chris@16
|
737 t += dt;
|
Chris@16
|
738 return success;
|
Chris@16
|
739 }
|
Chris@16
|
740 }
|
Chris@16
|
741 }
|
Chris@16
|
742
|
Chris@16
|
743
|
Chris@16
|
744 /**
|
Chris@16
|
745 * \brief Resets the internal state of the underlying FSAL stepper.
|
Chris@16
|
746 */
|
Chris@16
|
747 void reset( void )
|
Chris@16
|
748 {
|
Chris@16
|
749 m_first_call = true;
|
Chris@16
|
750 }
|
Chris@16
|
751
|
Chris@16
|
752 /**
|
Chris@16
|
753 * \brief Initializes the internal state storing an internal copy of the derivative.
|
Chris@16
|
754 *
|
Chris@16
|
755 * \param deriv The initial derivative of the ODE.
|
Chris@16
|
756 */
|
Chris@16
|
757 template< class DerivIn >
|
Chris@16
|
758 void initialize( const DerivIn &deriv )
|
Chris@16
|
759 {
|
Chris@16
|
760 boost::numeric::odeint::copy( deriv , m_dxdt.m_v );
|
Chris@16
|
761 m_first_call = false;
|
Chris@16
|
762 }
|
Chris@16
|
763
|
Chris@16
|
764 /**
|
Chris@16
|
765 * \brief Initializes the internal state storing an internal copy of the derivative.
|
Chris@16
|
766 *
|
Chris@16
|
767 * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
|
Chris@16
|
768 * Simple System concept.
|
Chris@16
|
769 * \param x The initial state of the ODE which should be solved.
|
Chris@16
|
770 * \param t The initial time.
|
Chris@16
|
771 */
|
Chris@16
|
772 template< class System , class StateIn >
|
Chris@16
|
773 void initialize( System system , const StateIn &x , time_type t )
|
Chris@16
|
774 {
|
Chris@16
|
775 typename odeint::unwrap_reference< System >::type &sys = system;
|
Chris@16
|
776 sys( x , m_dxdt.m_v , t );
|
Chris@16
|
777 m_first_call = false;
|
Chris@16
|
778 }
|
Chris@16
|
779
|
Chris@16
|
780 /**
|
Chris@16
|
781 * \brief Returns true if the stepper has been initialized, false otherwise.
|
Chris@16
|
782 *
|
Chris@16
|
783 * \return true, if the stepper has been initialized, false otherwise.
|
Chris@16
|
784 */
|
Chris@16
|
785 bool is_initialized( void ) const
|
Chris@16
|
786 {
|
Chris@16
|
787 return ! m_first_call;
|
Chris@16
|
788 }
|
Chris@16
|
789
|
Chris@16
|
790
|
Chris@16
|
791 /**
|
Chris@16
|
792 * \brief Adjust the size of all temporaries in the stepper manually.
|
Chris@16
|
793 * \param x A state from which the size of the temporaries to be resized is deduced.
|
Chris@16
|
794 */
|
Chris@16
|
795 template< class StateType >
|
Chris@16
|
796 void adjust_size( const StateType &x )
|
Chris@16
|
797 {
|
Chris@16
|
798 resize_m_xerr_impl( x );
|
Chris@16
|
799 resize_m_dxdt_impl( x );
|
Chris@16
|
800 resize_m_dxdt_new_impl( x );
|
Chris@16
|
801 resize_m_xnew_impl( x );
|
Chris@16
|
802 }
|
Chris@16
|
803
|
Chris@16
|
804
|
Chris@16
|
805 /**
|
Chris@16
|
806 * \brief Returns the instance of the underlying stepper.
|
Chris@16
|
807 * \returns The instance of the underlying stepper.
|
Chris@16
|
808 */
|
Chris@16
|
809 stepper_type& stepper( void )
|
Chris@16
|
810 {
|
Chris@16
|
811 return m_stepper;
|
Chris@16
|
812 }
|
Chris@16
|
813
|
Chris@16
|
814 /**
|
Chris@16
|
815 * \brief Returns the instance of the underlying stepper.
|
Chris@16
|
816 * \returns The instance of the underlying stepper.
|
Chris@16
|
817 */
|
Chris@16
|
818 const stepper_type& stepper( void ) const
|
Chris@16
|
819 {
|
Chris@16
|
820 return m_stepper;
|
Chris@16
|
821 }
|
Chris@16
|
822
|
Chris@16
|
823
|
Chris@16
|
824
|
Chris@16
|
825 private:
|
Chris@16
|
826
|
Chris@16
|
827
|
Chris@16
|
828 template< class StateIn >
|
Chris@16
|
829 bool resize_m_xerr_impl( const StateIn &x )
|
Chris@16
|
830 {
|
Chris@16
|
831 return adjust_size_by_resizeability( m_xerr , x , typename is_resizeable<state_type>::type() );
|
Chris@16
|
832 }
|
Chris@16
|
833
|
Chris@16
|
834 template< class StateIn >
|
Chris@16
|
835 bool resize_m_dxdt_impl( const StateIn &x )
|
Chris@16
|
836 {
|
Chris@16
|
837 return adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable<deriv_type>::type() );
|
Chris@16
|
838 }
|
Chris@16
|
839
|
Chris@16
|
840 template< class StateIn >
|
Chris@16
|
841 bool resize_m_dxdt_new_impl( const StateIn &x )
|
Chris@16
|
842 {
|
Chris@16
|
843 return adjust_size_by_resizeability( m_dxdtnew , x , typename is_resizeable<deriv_type>::type() );
|
Chris@16
|
844 }
|
Chris@16
|
845
|
Chris@16
|
846 template< class StateIn >
|
Chris@16
|
847 bool resize_m_xnew_impl( const StateIn &x )
|
Chris@16
|
848 {
|
Chris@16
|
849 return adjust_size_by_resizeability( m_xnew , x , typename is_resizeable<state_type>::type() );
|
Chris@16
|
850 }
|
Chris@16
|
851
|
Chris@16
|
852
|
Chris@16
|
853 template< class System , class StateInOut >
|
Chris@16
|
854 controlled_step_result try_step_v1( System system , StateInOut &x , time_type &t , time_type &dt )
|
Chris@16
|
855 {
|
Chris@16
|
856 if( m_dxdt_resizer.adjust_size( x , detail::bind( &controlled_runge_kutta::template resize_m_dxdt_impl< StateInOut > , detail::ref( *this ) , detail::_1 ) ) || m_first_call )
|
Chris@16
|
857 {
|
Chris@16
|
858 initialize( system , x , t );
|
Chris@16
|
859 }
|
Chris@16
|
860 return try_step( system , x , m_dxdt.m_v , t , dt );
|
Chris@16
|
861 }
|
Chris@16
|
862
|
Chris@16
|
863
|
Chris@16
|
864 stepper_type m_stepper;
|
Chris@16
|
865 error_checker_type m_error_checker;
|
Chris@16
|
866
|
Chris@16
|
867 resizer_type m_dxdt_resizer;
|
Chris@16
|
868 resizer_type m_xerr_resizer;
|
Chris@16
|
869 resizer_type m_xnew_resizer;
|
Chris@16
|
870 resizer_type m_dxdt_new_resizer;
|
Chris@16
|
871
|
Chris@16
|
872 wrapped_deriv_type m_dxdt;
|
Chris@16
|
873 wrapped_state_type m_xerr;
|
Chris@16
|
874 wrapped_state_type m_xnew;
|
Chris@16
|
875 wrapped_deriv_type m_dxdtnew;
|
Chris@16
|
876 bool m_first_call;
|
Chris@16
|
877 };
|
Chris@16
|
878
|
Chris@16
|
879
|
Chris@16
|
880 /********** DOXYGEN **********/
|
Chris@16
|
881
|
Chris@16
|
882 /**** DEFAULT ERROR CHECKER ****/
|
Chris@16
|
883
|
Chris@16
|
884 /**
|
Chris@16
|
885 * \class default_error_checker
|
Chris@16
|
886 * \brief The default error checker to be used with Runge-Kutta error steppers
|
Chris@16
|
887 *
|
Chris@16
|
888 * This class provides the default mechanism to compare the error estimates
|
Chris@16
|
889 * reported by Runge-Kutta error steppers with user defined error bounds.
|
Chris@16
|
890 * It is used by the controlled_runge_kutta steppers.
|
Chris@16
|
891 *
|
Chris@16
|
892 * \tparam Value The value type.
|
Chris@16
|
893 * \tparam Algebra The algebra type.
|
Chris@16
|
894 * \tparam Operations The operations type.
|
Chris@16
|
895 */
|
Chris@16
|
896
|
Chris@16
|
897 /**
|
Chris@16
|
898 * \fn default_error_checker( value_type eps_abs , value_type eps_rel , value_type a_x , value_type a_dxdt )
|
Chris@16
|
899 * \brief Constructs the error checker.
|
Chris@16
|
900 *
|
Chris@16
|
901 * The error is calculated as follows: ????
|
Chris@16
|
902 *
|
Chris@16
|
903 * \param eps_abs Absolute tolerance level.
|
Chris@16
|
904 * \param eps_rel Relative tolerance level.
|
Chris@16
|
905 * \param a_x Factor for the weight of the state.
|
Chris@16
|
906 * \param a_dxdt Factor for the weight of the derivative.
|
Chris@16
|
907 */
|
Chris@16
|
908
|
Chris@16
|
909 /**
|
Chris@16
|
910 * \fn error( const State &x_old , const Deriv &dxdt_old , Err &x_err , Time dt ) const
|
Chris@16
|
911 * \brief Calculates the error level.
|
Chris@16
|
912 *
|
Chris@16
|
913 * If the returned error level is greater than 1, the estimated error was
|
Chris@16
|
914 * larger than the permitted error bounds and the step should be repeated
|
Chris@16
|
915 * with a smaller step size.
|
Chris@16
|
916 *
|
Chris@16
|
917 * \param x_old State at the beginning of the step.
|
Chris@16
|
918 * \param dxdt_old Derivative at the beginning of the step.
|
Chris@16
|
919 * \param x_err Error estimate.
|
Chris@16
|
920 * \param dt Time step.
|
Chris@16
|
921 * \return error
|
Chris@16
|
922 */
|
Chris@16
|
923
|
Chris@16
|
924 /**
|
Chris@16
|
925 * \fn error( algebra_type &algebra , const State &x_old , const Deriv &dxdt_old , Err &x_err , Time dt ) const
|
Chris@16
|
926 * \brief Calculates the error level using a given algebra.
|
Chris@16
|
927 *
|
Chris@16
|
928 * If the returned error level is greater than 1, the estimated error was
|
Chris@16
|
929 * larger than the permitted error bounds and the step should be repeated
|
Chris@16
|
930 * with a smaller step size.
|
Chris@16
|
931 *
|
Chris@16
|
932 * \param algebra The algebra used for calculation of the error.
|
Chris@16
|
933 * \param x_old State at the beginning of the step.
|
Chris@16
|
934 * \param dxdt_old Derivative at the beginning of the step.
|
Chris@16
|
935 * \param x_err Error estimate.
|
Chris@16
|
936 * \param dt Time step.
|
Chris@16
|
937 * \return error
|
Chris@16
|
938 */
|
Chris@16
|
939
|
Chris@16
|
940
|
Chris@16
|
941 } // odeint
|
Chris@16
|
942 } // numeric
|
Chris@16
|
943 } // boost
|
Chris@16
|
944
|
Chris@16
|
945
|
Chris@16
|
946 #endif // BOOST_NUMERIC_ODEINT_STEPPER_CONTROLLED_RUNGE_KUTTA_HPP_INCLUDED
|