Chris@16
|
1 /*
|
Chris@16
|
2 [auto_generated]
|
Chris@16
|
3 boost/numeric/odeint/stepper/modified_midpoint.hpp
|
Chris@16
|
4
|
Chris@16
|
5 [begin_description]
|
Chris@16
|
6 Modified midpoint method for the use in Burlish-Stoer stepper.
|
Chris@16
|
7 [end_description]
|
Chris@16
|
8
|
Chris@16
|
9 Copyright 2009-2011 Karsten Ahnert
|
Chris@16
|
10 Copyright 2009-2011 Mario Mulansky
|
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_MODIFIED_MIDPOINT_HPP_INCLUDED
|
Chris@16
|
19 #define BOOST_NUMERIC_ODEINT_STEPPER_MODIFIED_MIDPOINT_HPP_INCLUDED
|
Chris@16
|
20
|
Chris@16
|
21 #include <vector>
|
Chris@16
|
22
|
Chris@16
|
23 #include <boost/numeric/odeint/stepper/base/explicit_stepper_base.hpp>
|
Chris@16
|
24 #include <boost/numeric/odeint/util/resizer.hpp>
|
Chris@16
|
25 #include <boost/numeric/odeint/util/is_resizeable.hpp>
|
Chris@16
|
26 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
|
Chris@16
|
27 #include <boost/numeric/odeint/algebra/default_operations.hpp>
|
Chris@16
|
28 #include <boost/numeric/odeint/util/copy.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost {
|
Chris@16
|
31 namespace numeric {
|
Chris@16
|
32 namespace odeint {
|
Chris@16
|
33
|
Chris@16
|
34 template<
|
Chris@16
|
35 class State ,
|
Chris@16
|
36 class Value = double ,
|
Chris@16
|
37 class Deriv = State ,
|
Chris@16
|
38 class Time = Value ,
|
Chris@16
|
39 class Algebra = range_algebra ,
|
Chris@16
|
40 class Operations = default_operations ,
|
Chris@16
|
41 class Resizer = initially_resizer
|
Chris@16
|
42 >
|
Chris@16
|
43 #ifndef DOXYGEN_SKIP
|
Chris@16
|
44 class modified_midpoint
|
Chris@16
|
45 : public explicit_stepper_base<
|
Chris@16
|
46 modified_midpoint< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
|
Chris@16
|
47 2 , State , Value , Deriv , Time , Algebra , Operations , Resizer >
|
Chris@16
|
48 #else
|
Chris@16
|
49 class modified_midpoint : public explicit_stepper_base
|
Chris@16
|
50 #endif
|
Chris@16
|
51 {
|
Chris@16
|
52
|
Chris@16
|
53 public :
|
Chris@16
|
54
|
Chris@16
|
55 typedef explicit_stepper_base<
|
Chris@16
|
56 modified_midpoint< State , Value , Deriv , Time , Algebra , Operations , Resizer > ,
|
Chris@16
|
57 2 , State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_base_type;
|
Chris@16
|
58
|
Chris@16
|
59 typedef typename stepper_base_type::state_type state_type;
|
Chris@16
|
60 typedef typename stepper_base_type::wrapped_state_type wrapped_state_type;
|
Chris@16
|
61 typedef typename stepper_base_type::value_type value_type;
|
Chris@16
|
62 typedef typename stepper_base_type::deriv_type deriv_type;
|
Chris@16
|
63 typedef typename stepper_base_type::wrapped_deriv_type wrapped_deriv_type;
|
Chris@16
|
64 typedef typename stepper_base_type::time_type time_type;
|
Chris@16
|
65 typedef typename stepper_base_type::algebra_type algebra_type;
|
Chris@16
|
66 typedef typename stepper_base_type::operations_type operations_type;
|
Chris@16
|
67 typedef typename stepper_base_type::resizer_type resizer_type;
|
Chris@16
|
68 typedef typename stepper_base_type::stepper_type stepper_type;
|
Chris@16
|
69
|
Chris@16
|
70
|
Chris@16
|
71 modified_midpoint( unsigned short steps = 2 , const algebra_type &algebra = algebra_type() )
|
Chris@16
|
72 : stepper_base_type( algebra ) , m_steps( steps )
|
Chris@16
|
73 { }
|
Chris@16
|
74
|
Chris@16
|
75 template< class System , class StateIn , class DerivIn , class StateOut >
|
Chris@16
|
76 void do_step_impl( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt )
|
Chris@16
|
77 {
|
Chris@16
|
78 static const value_type val1 = static_cast< value_type >( 1 );
|
Chris@16
|
79 static const value_type val05 = static_cast< value_type >( 1 ) / static_cast< value_type >( 2 );
|
Chris@16
|
80
|
Chris@16
|
81 m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_impl< StateIn > , detail::ref( *this ) , detail::_1 ) );
|
Chris@16
|
82
|
Chris@16
|
83 const time_type h = dt / static_cast<value_type>( m_steps );
|
Chris@16
|
84 const time_type h2 = static_cast<value_type>(2) * h;
|
Chris@16
|
85
|
Chris@16
|
86 typename odeint::unwrap_reference< System >::type &sys = system;
|
Chris@16
|
87
|
Chris@16
|
88 time_type th = t + h;
|
Chris@16
|
89
|
Chris@16
|
90 // m_x1 = x + h*dxdt
|
Chris@16
|
91 stepper_base_type::m_algebra.for_each3( m_x1.m_v , in , dxdt ,
|
Chris@16
|
92 typename operations_type::template scale_sum2< value_type , time_type >( val1 , h ) );
|
Chris@16
|
93
|
Chris@16
|
94 sys( m_x1.m_v , m_dxdt.m_v , th );
|
Chris@16
|
95
|
Chris@16
|
96 boost::numeric::odeint::copy( in , m_x0.m_v );
|
Chris@16
|
97
|
Chris@16
|
98 unsigned short i = 1;
|
Chris@16
|
99 while( i != m_steps )
|
Chris@16
|
100 {
|
Chris@16
|
101 // general step
|
Chris@16
|
102 //tmp = m_x1; m_x1 = m_x0 + h2*m_dxdt; m_x0 = tmp
|
Chris@16
|
103 stepper_base_type::m_algebra.for_each3( m_x1.m_v , m_x0.m_v , m_dxdt.m_v ,
|
Chris@16
|
104 typename operations_type::template scale_sum_swap2< value_type , time_type >( val1 , h2 ) );
|
Chris@16
|
105 th += h;
|
Chris@16
|
106 sys( m_x1.m_v , m_dxdt.m_v , th);
|
Chris@16
|
107 i++;
|
Chris@16
|
108 }
|
Chris@16
|
109
|
Chris@16
|
110 // last step
|
Chris@16
|
111 // x = 0.5*( m_x0 + m_x1 + h*m_dxdt )
|
Chris@16
|
112 stepper_base_type::m_algebra.for_each4( out , m_x0.m_v , m_x1.m_v , m_dxdt.m_v ,
|
Chris@16
|
113 typename operations_type::template scale_sum3< value_type , value_type , time_type >( val05 , val05 , val05*h ) );
|
Chris@16
|
114 }
|
Chris@16
|
115
|
Chris@16
|
116
|
Chris@16
|
117 void set_steps( unsigned short steps )
|
Chris@16
|
118 { m_steps = steps; }
|
Chris@16
|
119
|
Chris@16
|
120
|
Chris@16
|
121 unsigned short steps( void ) const
|
Chris@16
|
122 { return m_steps; }
|
Chris@16
|
123
|
Chris@16
|
124
|
Chris@16
|
125 template< class StateIn >
|
Chris@16
|
126 void adjust_size( const StateIn &x )
|
Chris@16
|
127 {
|
Chris@16
|
128 resize_impl( x );
|
Chris@16
|
129 stepper_base_type::adjust_size( x );
|
Chris@16
|
130 }
|
Chris@16
|
131
|
Chris@16
|
132 private:
|
Chris@16
|
133
|
Chris@16
|
134 template< class StateIn >
|
Chris@16
|
135 bool resize_impl( const StateIn &x )
|
Chris@16
|
136 {
|
Chris@16
|
137 bool resized( false );
|
Chris@16
|
138 resized |= adjust_size_by_resizeability( m_x0 , x , typename is_resizeable<state_type>::type() );
|
Chris@16
|
139 resized |= adjust_size_by_resizeability( m_x1 , x , typename is_resizeable<state_type>::type() );
|
Chris@16
|
140 resized |= adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable<deriv_type>::type() );
|
Chris@16
|
141 return resized;
|
Chris@16
|
142 }
|
Chris@16
|
143
|
Chris@16
|
144
|
Chris@16
|
145 unsigned short m_steps;
|
Chris@16
|
146
|
Chris@16
|
147 resizer_type m_resizer;
|
Chris@16
|
148
|
Chris@16
|
149 wrapped_state_type m_x0;
|
Chris@16
|
150 wrapped_state_type m_x1;
|
Chris@16
|
151 wrapped_deriv_type m_dxdt;
|
Chris@16
|
152
|
Chris@16
|
153 };
|
Chris@16
|
154
|
Chris@16
|
155
|
Chris@16
|
156 /* Modified midpoint which stores derivatives and state at dt/2 in some external storage for later usage in dense output calculation
|
Chris@16
|
157 * This Stepper is for use in Bulirsch Stoer only. It DOES NOT meet any stepper concept.
|
Chris@16
|
158 */
|
Chris@16
|
159 template<
|
Chris@16
|
160 class State ,
|
Chris@16
|
161 class Value = double ,
|
Chris@16
|
162 class Deriv = State ,
|
Chris@16
|
163 class Time = Value ,
|
Chris@16
|
164 class Algebra = range_algebra ,
|
Chris@16
|
165 class Operations = default_operations ,
|
Chris@16
|
166 class Resizer = initially_resizer
|
Chris@16
|
167 >
|
Chris@16
|
168 class modified_midpoint_dense_out
|
Chris@16
|
169 {
|
Chris@16
|
170
|
Chris@16
|
171 public :
|
Chris@16
|
172
|
Chris@16
|
173 typedef State state_type;
|
Chris@16
|
174 typedef Value value_type;
|
Chris@16
|
175 typedef Deriv deriv_type;
|
Chris@16
|
176 typedef Time time_type;
|
Chris@16
|
177 typedef Algebra algebra_type;
|
Chris@16
|
178 typedef Operations operations_type;
|
Chris@16
|
179 typedef Resizer resizer_type;
|
Chris@16
|
180 typedef state_wrapper< state_type > wrapped_state_type;
|
Chris@16
|
181 typedef state_wrapper< deriv_type > wrapped_deriv_type;
|
Chris@16
|
182
|
Chris@16
|
183 typedef modified_midpoint_dense_out< State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_type;
|
Chris@16
|
184 typedef std::vector< wrapped_deriv_type > deriv_table_type;
|
Chris@16
|
185
|
Chris@16
|
186 modified_midpoint_dense_out( unsigned short steps = 2 , const algebra_type &algebra = algebra_type() )
|
Chris@16
|
187 : m_algebra( algebra ) , m_steps( steps )
|
Chris@16
|
188 { }
|
Chris@16
|
189
|
Chris@16
|
190 /*
|
Chris@16
|
191 * performs a modified midpoint step with m_steps intermediate points
|
Chris@16
|
192 * stores approximation for x(t+dt/2) in x_mp and all evaluated function results in derivs
|
Chris@16
|
193 *
|
Chris@16
|
194 */
|
Chris@16
|
195
|
Chris@16
|
196 template< class System , class StateIn , class DerivIn , class StateOut >
|
Chris@16
|
197 void do_step( System system , const StateIn &in , const DerivIn &dxdt , time_type t , StateOut &out , time_type dt ,
|
Chris@16
|
198 state_type &x_mp , deriv_table_type &derivs )
|
Chris@16
|
199 {
|
Chris@16
|
200
|
Chris@16
|
201 static const value_type val1 = static_cast< value_type >( 1 );
|
Chris@16
|
202 static const value_type val05 = static_cast< value_type >( 1 ) / static_cast< value_type >( 2 );
|
Chris@16
|
203
|
Chris@16
|
204 m_resizer.adjust_size( in , detail::bind( &stepper_type::template resize< StateIn > , detail::ref( *this ) , detail::_1 ) );
|
Chris@16
|
205
|
Chris@16
|
206 const time_type h = dt / static_cast<value_type>( m_steps );
|
Chris@16
|
207 const time_type h2 = static_cast<value_type>( 2 ) * h;
|
Chris@16
|
208
|
Chris@16
|
209 typename odeint::unwrap_reference< System >::type &sys = system;
|
Chris@16
|
210
|
Chris@16
|
211 time_type th = t + h;
|
Chris@16
|
212
|
Chris@16
|
213 // m_x1 = x + h*dxdt
|
Chris@16
|
214 m_algebra.for_each3( m_x1.m_v , in , dxdt ,
|
Chris@16
|
215 typename operations_type::template scale_sum2< value_type , time_type >( val1 , h ) );
|
Chris@16
|
216
|
Chris@16
|
217 if( m_steps == 2 )
|
Chris@16
|
218 // result of first step already gives approximation at the center of the interval
|
Chris@16
|
219 boost::numeric::odeint::copy( m_x1.m_v , x_mp );
|
Chris@16
|
220
|
Chris@16
|
221 sys( m_x1.m_v , derivs[0].m_v , th );
|
Chris@16
|
222
|
Chris@16
|
223 boost::numeric::odeint::copy( in , m_x0.m_v );
|
Chris@16
|
224
|
Chris@16
|
225 unsigned short i = 1;
|
Chris@16
|
226 while( i != m_steps )
|
Chris@16
|
227 {
|
Chris@16
|
228 // general step
|
Chris@16
|
229 //tmp = m_x1; m_x1 = m_x0 + h2*m_dxdt; m_x0 = tmp
|
Chris@16
|
230 m_algebra.for_each3( m_x1.m_v , m_x0.m_v , derivs[i-1].m_v ,
|
Chris@16
|
231 typename operations_type::template scale_sum_swap2< value_type , time_type >( val1 , h2 ) );
|
Chris@16
|
232 if( i == m_steps/2-1 )
|
Chris@16
|
233 // save approximation at the center of the interval
|
Chris@16
|
234 boost::numeric::odeint::copy( m_x1.m_v , x_mp );
|
Chris@16
|
235
|
Chris@16
|
236 th += h;
|
Chris@16
|
237 sys( m_x1.m_v , derivs[i].m_v , th);
|
Chris@16
|
238 i++;
|
Chris@16
|
239 }
|
Chris@16
|
240
|
Chris@16
|
241 // last step
|
Chris@16
|
242 // x = 0.5*( m_x0 + m_x1 + h*m_dxdt )
|
Chris@16
|
243 m_algebra.for_each4( out , m_x0.m_v , m_x1.m_v , derivs[m_steps-1].m_v ,
|
Chris@16
|
244 typename operations_type::template scale_sum3< value_type , value_type , time_type >( val05 , val05 , val05*h ) );
|
Chris@16
|
245 }
|
Chris@16
|
246
|
Chris@16
|
247
|
Chris@16
|
248 void set_steps( unsigned short steps )
|
Chris@16
|
249 { m_steps = steps; }
|
Chris@16
|
250
|
Chris@16
|
251
|
Chris@16
|
252 unsigned short steps( void ) const
|
Chris@16
|
253 { return m_steps; }
|
Chris@16
|
254
|
Chris@16
|
255
|
Chris@16
|
256 template< class StateIn >
|
Chris@16
|
257 bool resize( const StateIn &x )
|
Chris@16
|
258 {
|
Chris@16
|
259 bool resized( false );
|
Chris@16
|
260 resized |= adjust_size_by_resizeability( m_x0 , x , typename is_resizeable<state_type>::type() );
|
Chris@16
|
261 resized |= adjust_size_by_resizeability( m_x1 , x , typename is_resizeable<state_type>::type() );
|
Chris@16
|
262 return resized;
|
Chris@16
|
263 }
|
Chris@16
|
264
|
Chris@16
|
265 template< class StateIn >
|
Chris@16
|
266 void adjust_size( const StateIn &x )
|
Chris@16
|
267 {
|
Chris@16
|
268 resize( x );
|
Chris@16
|
269 }
|
Chris@16
|
270
|
Chris@16
|
271 private:
|
Chris@16
|
272
|
Chris@16
|
273 algebra_type m_algebra;
|
Chris@16
|
274
|
Chris@16
|
275 unsigned short m_steps;
|
Chris@16
|
276
|
Chris@16
|
277 resizer_type m_resizer;
|
Chris@16
|
278
|
Chris@16
|
279 wrapped_state_type m_x0;
|
Chris@16
|
280 wrapped_state_type m_x1;
|
Chris@16
|
281
|
Chris@16
|
282 };
|
Chris@16
|
283
|
Chris@16
|
284
|
Chris@16
|
285
|
Chris@16
|
286 /********** DOXYGEN ***********/
|
Chris@16
|
287
|
Chris@16
|
288 /**
|
Chris@16
|
289 * \class modified_midpoint
|
Chris@16
|
290 *
|
Chris@16
|
291 * Implementation of the modified midpoint method with a configurable
|
Chris@16
|
292 * number of intermediate steps. This class is used by the Bulirsch-Stoer
|
Chris@16
|
293 * algorithm and is not meant for direct usage.
|
Chris@16
|
294 */
|
Chris@16
|
295
|
Chris@16
|
296
|
Chris@16
|
297 /**
|
Chris@16
|
298 * \class modified_midpoint_dense_out
|
Chris@16
|
299 *
|
Chris@16
|
300 * Implementation of the modified midpoint method with a configurable
|
Chris@16
|
301 * number of intermediate steps. This class is used by the dense output
|
Chris@16
|
302 * Bulirsch-Stoer algorithm and is not meant for direct usage.
|
Chris@16
|
303 * \note This stepper is for internal use only and does not meet
|
Chris@16
|
304 * any stepper concept.
|
Chris@16
|
305 */
|
Chris@16
|
306
|
Chris@16
|
307
|
Chris@16
|
308 }
|
Chris@16
|
309 }
|
Chris@16
|
310 }
|
Chris@16
|
311
|
Chris@16
|
312 #endif // BOOST_NUMERIC_ODEINT_STEPPER_MODIFIED_MIDPOINT_HPP_INCLUDED
|