1 /*
2  [auto_generated]
3  boost/numeric/odeint/stepper/explicit_error_generic_rk.hpp
4 
5  [begin_description]
6  Implementation of the generic Runge Kutta error stepper. Base class for many RK error steppers.
7  [end_description]
8 
9  Copyright 2011-2013 Mario Mulansky
10  Copyright 2011-2013 Karsten Ahnert
11  Copyright 2012 Christoph Koke
12 
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)
16  */
17 
18 
19 #ifndef BOOST_NUMERIC_ODEINT_STEPPER_EXPLICIT_ERROR_GENERIC_RK_HPP_INCLUDED
20 #define BOOST_NUMERIC_ODEINT_STEPPER_EXPLICIT_ERROR_GENERIC_RK_HPP_INCLUDED
21 
22 #include <boost/numeric/odeint/stepper/base/explicit_error_stepper_base.hpp>
23 
24 #include <boost/numeric/odeint/algebra/default_operations.hpp>
25 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
26 #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
27 #include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
28 #include <boost/numeric/odeint/stepper/detail/generic_rk_algorithm.hpp>
29 #include <boost/numeric/odeint/stepper/detail/generic_rk_call_algebra.hpp>
30 #include <boost/numeric/odeint/stepper/detail/generic_rk_operations.hpp>
31 
32 #include <boost/numeric/odeint/util/state_wrapper.hpp>
33 #include <boost/numeric/odeint/util/is_resizeable.hpp>
34 #include <boost/numeric/odeint/util/resizer.hpp>
35 
36 
37 
38 namespace boost {
39 namespace numeric {
40 namespace odeint {
41 
42 
43 template<
44 size_t StageCount,
45 size_t Order,
46 size_t StepperOrder ,
47 size_t ErrorOrder ,
48 class State ,
49 class Value = double ,
50 class Deriv = State ,
51 class Time = Value ,
52 class Algebra = typename algebra_dispatcher< State >::algebra_type ,
53 class Operations = typename operations_dispatcher< State >::operations_type ,
54 class Resizer = initially_resizer
55 >
56 #ifndef DOXYGEN_SKIP
57 class explicit_error_generic_rk
58 : public explicit_error_stepper_base<
59   explicit_error_generic_rk< StageCount , Order , StepperOrder , ErrorOrder , State ,
60   Value , Deriv , Time , Algebra , Operations , Resizer > ,
61   Order , StepperOrder , ErrorOrder , State , Value , Deriv , Time , Algebra ,
62   Operations , Resizer >
63 #else
64 class explicit_error_generic_rk : public explicit_error_stepper_base
65 #endif
66 {
67 
68 public:
69 #ifndef DOXYGEN_SKIP
70     typedef explicit_error_stepper_base<
71             explicit_error_generic_rk< StageCount , Order , StepperOrder , ErrorOrder , State ,
72             Value , Deriv , Time , Algebra , Operations , Resizer > ,
73             Order , StepperOrder , ErrorOrder , State , Value , Deriv , Time , Algebra ,
74             Operations , Resizer > stepper_base_type;
75 #else
76     typedef explicit_stepper_base< ... > stepper_base_type;
77 #endif
78     typedef typename stepper_base_type::state_type state_type;
79     typedef typename stepper_base_type::wrapped_state_type wrapped_state_type;
80     typedef typename stepper_base_type::value_type value_type;
81     typedef typename stepper_base_type::deriv_type deriv_type;
82     typedef typename stepper_base_type::wrapped_deriv_type wrapped_deriv_type;
83     typedef typename stepper_base_type::time_type time_type;
84     typedef typename stepper_base_type::algebra_type algebra_type;
85     typedef typename stepper_base_type::operations_type operations_type;
86     typedef typename stepper_base_type::resizer_type resizer_type;
87 #ifndef DOXYGEN_SKIP
88     typedef explicit_error_generic_rk< StageCount , Order , StepperOrder , ErrorOrder , State ,
89             Value , Deriv , Time , Algebra , Operations , Resizer > stepper_type;
90 #endif
91     typedef detail::generic_rk_algorithm< StageCount , Value , Algebra , Operations > rk_algorithm_type;
92 
93     typedef typename rk_algorithm_type::coef_a_type coef_a_type;
94     typedef typename rk_algorithm_type::coef_b_type coef_b_type;
95     typedef typename rk_algorithm_type::coef_c_type coef_c_type;
96 
97     static const size_t stage_count = StageCount;
98 
99 private:
100 
101 
102 public:
103 
104     // we use an explicit_generic_rk to do the normal rk step
105     // and add a separate calculation of the error estimate afterwards
explicit_error_generic_rk(const coef_a_type & a,const coef_b_type & b,const coef_b_type & b2,const coef_c_type & c,const algebra_type & algebra=algebra_type ())106     explicit_error_generic_rk( const coef_a_type &a ,
107             const coef_b_type &b ,
108             const coef_b_type &b2 ,
109             const coef_c_type &c ,
110             const algebra_type &algebra = algebra_type() )
111     : stepper_base_type( algebra ) , m_rk_algorithm( a , b , c ) , m_b2( b2 )
112     { }
113 
114 
115     template< class System , class StateIn , class DerivIn , class StateOut , class Err >
do_step_impl(System system,const StateIn & in,const DerivIn & dxdt,time_type t,StateOut & out,time_type dt,Err & xerr)116     void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt ,
117             time_type t , StateOut &out , time_type dt , Err &xerr )
118     {
119         // normal step
120         do_step_impl( system , in , dxdt , t , out , dt );
121 
122         // additionally, perform the error calculation
123         detail::template generic_rk_call_algebra< StageCount , algebra_type >()( stepper_base_type::m_algebra ,
124                 xerr , dxdt , m_F , detail::generic_rk_scale_sum_err< StageCount , operations_type , value_type , time_type >( m_b2 , dt) );
125     }
126 
127 
128     template< class System , class StateIn , class DerivIn , class StateOut >
do_step_impl(System system,const StateIn & in,const DerivIn & dxdt,time_type t,StateOut & out,time_type dt)129     void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt ,
130             time_type t , StateOut &out , time_type dt )
131     {
132         m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
133 
134         // actual calculation done in generic_rk.hpp
135         m_rk_algorithm.do_step( stepper_base_type::m_algebra , system , in , dxdt , t , out , dt , m_x_tmp.m_v , m_F );
136     }
137 
138 
139     template< class StateIn >
adjust_size(const StateIn & x)140     void adjust_size( const StateIn &x )
141     {
142         resize_impl( x );
143         stepper_base_type::adjust_size( x );
144     }
145 
146 
147 private:
148 
149     template< class StateIn >
resize_impl(const StateIn & x)150     bool resize_impl( const StateIn &x )
151     {
152         bool resized( false );
153         resized |= adjust_size_by_resizeability( m_x_tmp , x , typename is_resizeable<state_type>::type() );
154         for( size_t i = 0 ; i < StageCount-1 ; ++i )
155         {
156             resized |= adjust_size_by_resizeability( m_F[i] , x , typename is_resizeable<deriv_type>::type() );
157         }
158         return resized;
159     }
160 
161 
162     rk_algorithm_type m_rk_algorithm;
163     coef_b_type m_b2;
164 
165     resizer_type m_resizer;
166 
167     wrapped_state_type m_x_tmp;
168     wrapped_deriv_type m_F[StageCount-1];
169 
170 };
171 
172 
173 /********* DOXYGEN *********/
174 
175 /**
176  * \class explicit_error_generic_rk
177  * \brief A generic implementation of explicit Runge-Kutta algorithms with error estimation. This class is as a
178  * base class for all explicit Runge-Kutta steppers with error estimation.
179  *
180  * This class implements the explicit Runge-Kutta algorithms with error estimation in a generic way.
181  * The Butcher tableau is passed to the stepper which constructs the stepper scheme with the help of a
182  * template-metaprogramming algorithm. ToDo : Add example!
183  *
184  * This class derives explicit_error_stepper_base which provides the stepper interface.
185  *
186  * \tparam StageCount The number of stages of the Runge-Kutta algorithm.
187  * \tparam Order The order of a stepper if the stepper is used without error estimation.
188  * \tparam StepperOrder The order of a step if the stepper is used with error estimation. Usually Order and StepperOrder have
189  * the same value.
190  * \tparam ErrorOrder The order of the error step if the stepper is used with error estimation.
191  * \tparam State The type representing the state of the ODE.
192  * \tparam Value The floating point type which is used in the computations.
193  * \tparam Time The type representing the independent variable - the time - of the ODE.
194  * \tparam Algebra The algebra type.
195  * \tparam Operations The operations type.
196  * \tparam Resizer The resizer policy type.
197  */
198 
199 
200     /**
201      * \fn explicit_error_generic_rk::explicit_error_generic_rk( const coef_a_type &a , const coef_b_type &b , const coef_b_type &b2 , const coef_c_type &c , const algebra_type &algebra )
202      * \brief Constructs the explicit_error_generik_rk class with the given parameters a, b, b2 and c. See examples section for details on the coefficients.
203      *
204      * \param a Triangular matrix of parameters b in the Butcher tableau.
205      * \param b Last row of the butcher tableau.
206      * \param b2 Parameters for lower-order evaluation to estimate the error.
207      * \param c Parameters to calculate the time points in the Butcher tableau.
208      * \param algebra A copy of algebra is made and stored inside explicit_stepper_base.
209      */
210 
211 
212     /**
213      * \fn explicit_error_generic_rk::do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt , Err &xerr )
214      * \brief This method performs one step. The derivative `dxdt` of `in` at the time `t` is passed to the method.
215      * The result is updated out-of-place, hence the input is in `in` and the output in `out`. Futhermore, an
216      * estimation of the error is stored in `xerr`. `do_step_impl` is used by explicit_error_stepper_base.
217      *
218      * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
219      *               Simple System concept.
220      * \param in The state of the ODE which should be solved. in is not modified in this method
221      * \param dxdt The derivative of x at t.
222      * \param t The value of the time, at which the step should be performed.
223      * \param out The result of the step is written in out.
224      * \param dt The step size.
225      * \param xerr The result of the error estimation is written in xerr.
226      */
227 
228     /**
229      * \fn explicit_error_generic_rk::do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
230      * \brief This method performs one step. The derivative `dxdt` of `in` at the time `t` is passed to the method.
231      * The result is updated out-of-place, hence the input is in `in` and the output in `out`.
232      * Access to this step functionality is provided by explicit_stepper_base and
233      * `do_step_impl` should not be called directly.
234      *
235      * \param system The system function to solve, hence the r.h.s. of the ODE. It must fulfill the
236      *               Simple System concept.
237      * \param in The state of the ODE which should be solved. in is not modified in this method
238      * \param dxdt The derivative of x at t.
239      * \param t The value of the time, at which the step should be performed.
240      * \param out The result of the step is written in out.
241      * \param dt The step size.
242      */
243 
244     /**
245      * \fn explicit_error_generic_rk::adjust_size( const StateIn &x )
246      * \brief Adjust the size of all temporaries in the stepper manually.
247      * \param x A state from which the size of the temporaries to be resized is deduced.
248      */
249 
250 }
251 }
252 }
253 
254 
255 #endif // BOOST_NUMERIC_ODEINT_STEPPER_EXPLICIT_ERROR_GENERIC_RK_HPP_INCLUDED
256