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