annotate DEPENDENCIES/generic/include/boost/numeric/odeint/stepper/rosenbrock4.hpp @ 46:d572322e2efe

Fix to .cat file check (was susceptible to DOS line-endings) and subrepo update
author Chris Cannam
date Thu, 07 Aug 2014 14:39:38 +0100
parents 2665513ce2d3
children c530137014c0
rev   line source
Chris@16 1 /*
Chris@16 2 [auto_generated]
Chris@16 3 boost/numeric/odeint/stepper/rosenbrock4.hpp
Chris@16 4
Chris@16 5 [begin_description]
Chris@16 6 Implementation of the Rosenbrock 4 method for solving stiff ODEs. Note, that a
Chris@16 7 controller and a dense-output stepper exist for this method,
Chris@16 8 [end_description]
Chris@16 9
Chris@16 10 Copyright 2009-2011 Karsten Ahnert
Chris@16 11 Copyright 2009-2011 Mario Mulansky
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_ROSENBROCK4_HPP_INCLUDED
Chris@16 20 #define BOOST_NUMERIC_ODEINT_STEPPER_ROSENBROCK4_HPP_INCLUDED
Chris@16 21
Chris@16 22
Chris@16 23 #include <boost/numeric/odeint/util/bind.hpp>
Chris@16 24 #include <boost/numeric/odeint/util/unwrap_reference.hpp>
Chris@16 25 #include <boost/numeric/ublas/vector.hpp>
Chris@16 26 #include <boost/numeric/ublas/matrix.hpp>
Chris@16 27 #include <boost/numeric/ublas/lu.hpp>
Chris@16 28
Chris@16 29 #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
Chris@16 30
Chris@16 31 #include <boost/numeric/odeint/util/ublas_wrapper.hpp>
Chris@16 32 #include <boost/numeric/odeint/util/is_resizeable.hpp>
Chris@16 33 #include <boost/numeric/odeint/util/resizer.hpp>
Chris@16 34
Chris@16 35 #include <boost/numeric/ublas/vector.hpp>
Chris@16 36 #include <boost/numeric/ublas/matrix.hpp>
Chris@16 37 #include <boost/numeric/ublas/lu.hpp>
Chris@16 38
Chris@16 39
Chris@16 40 namespace boost {
Chris@16 41 namespace numeric {
Chris@16 42 namespace odeint {
Chris@16 43
Chris@16 44
Chris@16 45 /*
Chris@16 46 * ToDo:
Chris@16 47 *
Chris@16 48 * 2. Interfacing for odeint, check if controlled_error_stepper can be used
Chris@16 49 * 3. dense output
Chris@16 50 */
Chris@16 51
Chris@16 52
Chris@16 53
Chris@16 54 template< class Value >
Chris@16 55 struct default_rosenbrock_coefficients
Chris@16 56 {
Chris@16 57 typedef Value value_type;
Chris@16 58 typedef unsigned short order_type;
Chris@16 59
Chris@16 60 default_rosenbrock_coefficients( void )
Chris@16 61 : gamma ( static_cast< value_type >( 0.25 ) ) ,
Chris@16 62 d1 ( static_cast< value_type >( 0.25 ) ) ,
Chris@16 63 d2 ( static_cast< value_type >( -0.1043 ) ) ,
Chris@16 64 d3 ( static_cast< value_type >( 0.1035 ) ) ,
Chris@16 65 d4 ( static_cast< value_type >( 0.3620000000000023e-01 ) ) ,
Chris@16 66 c2 ( static_cast< value_type >( 0.386 ) ) ,
Chris@16 67 c3 ( static_cast< value_type >( 0.21 ) ) ,
Chris@16 68 c4 ( static_cast< value_type >( 0.63 ) ) ,
Chris@16 69 c21 ( static_cast< value_type >( -0.5668800000000000e+01 ) ) ,
Chris@16 70 a21 ( static_cast< value_type >( 0.1544000000000000e+01 ) ) ,
Chris@16 71 c31 ( static_cast< value_type >( -0.2430093356833875e+01 ) ) ,
Chris@16 72 c32 ( static_cast< value_type >( -0.2063599157091915e+00 ) ) ,
Chris@16 73 a31 ( static_cast< value_type >( 0.9466785280815826e+00 ) ) ,
Chris@16 74 a32 ( static_cast< value_type >( 0.2557011698983284e+00 ) ) ,
Chris@16 75 c41 ( static_cast< value_type >( -0.1073529058151375e+00 ) ) ,
Chris@16 76 c42 ( static_cast< value_type >( -0.9594562251023355e+01 ) ) ,
Chris@16 77 c43 ( static_cast< value_type >( -0.2047028614809616e+02 ) ) ,
Chris@16 78 a41 ( static_cast< value_type >( 0.3314825187068521e+01 ) ) ,
Chris@16 79 a42 ( static_cast< value_type >( 0.2896124015972201e+01 ) ) ,
Chris@16 80 a43 ( static_cast< value_type >( 0.9986419139977817e+00 ) ) ,
Chris@16 81 c51 ( static_cast< value_type >( 0.7496443313967647e+01 ) ) ,
Chris@16 82 c52 ( static_cast< value_type >( -0.1024680431464352e+02 ) ) ,
Chris@16 83 c53 ( static_cast< value_type >( -0.3399990352819905e+02 ) ) ,
Chris@16 84 c54 ( static_cast< value_type >( 0.1170890893206160e+02 ) ) ,
Chris@16 85 a51 ( static_cast< value_type >( 0.1221224509226641e+01 ) ) ,
Chris@16 86 a52 ( static_cast< value_type >( 0.6019134481288629e+01 ) ) ,
Chris@16 87 a53 ( static_cast< value_type >( 0.1253708332932087e+02 ) ) ,
Chris@16 88 a54 ( static_cast< value_type >( -0.6878860361058950e+00 ) ) ,
Chris@16 89 c61 ( static_cast< value_type >( 0.8083246795921522e+01 ) ) ,
Chris@16 90 c62 ( static_cast< value_type >( -0.7981132988064893e+01 ) ) ,
Chris@16 91 c63 ( static_cast< value_type >( -0.3152159432874371e+02 ) ) ,
Chris@16 92 c64 ( static_cast< value_type >( 0.1631930543123136e+02 ) ) ,
Chris@16 93 c65 ( static_cast< value_type >( -0.6058818238834054e+01 ) ) ,
Chris@16 94 d21 ( static_cast< value_type >( 0.1012623508344586e+02 ) ) ,
Chris@16 95 d22 ( static_cast< value_type >( -0.7487995877610167e+01 ) ) ,
Chris@16 96 d23 ( static_cast< value_type >( -0.3480091861555747e+02 ) ) ,
Chris@16 97 d24 ( static_cast< value_type >( -0.7992771707568823e+01 ) ) ,
Chris@16 98 d25 ( static_cast< value_type >( 0.1025137723295662e+01 ) ) ,
Chris@16 99 d31 ( static_cast< value_type >( -0.6762803392801253e+00 ) ) ,
Chris@16 100 d32 ( static_cast< value_type >( 0.6087714651680015e+01 ) ) ,
Chris@16 101 d33 ( static_cast< value_type >( 0.1643084320892478e+02 ) ) ,
Chris@16 102 d34 ( static_cast< value_type >( 0.2476722511418386e+02 ) ) ,
Chris@16 103 d35 ( static_cast< value_type >( -0.6594389125716872e+01 ) )
Chris@16 104 {}
Chris@16 105
Chris@16 106 const value_type gamma;
Chris@16 107 const value_type d1 , d2 , d3 , d4;
Chris@16 108 const value_type c2 , c3 , c4;
Chris@16 109 const value_type c21 ;
Chris@16 110 const value_type a21;
Chris@16 111 const value_type c31 , c32;
Chris@16 112 const value_type a31 , a32;
Chris@16 113 const value_type c41 , c42 , c43;
Chris@16 114 const value_type a41 , a42 , a43;
Chris@16 115 const value_type c51 , c52 , c53 , c54;
Chris@16 116 const value_type a51 , a52 , a53 , a54;
Chris@16 117 const value_type c61 , c62 , c63 , c64 , c65;
Chris@16 118 const value_type d21 , d22 , d23 , d24 , d25;
Chris@16 119 const value_type d31 , d32 , d33 , d34 , d35;
Chris@16 120
Chris@16 121 static const order_type stepper_order = 4;
Chris@16 122 static const order_type error_order = 3;
Chris@16 123 };
Chris@16 124
Chris@16 125
Chris@16 126
Chris@16 127 template< class Value , class Coefficients = default_rosenbrock_coefficients< Value > , class Resizer = initially_resizer >
Chris@16 128 class rosenbrock4
Chris@16 129 {
Chris@16 130 private:
Chris@16 131
Chris@16 132 public:
Chris@16 133
Chris@16 134 typedef Value value_type;
Chris@16 135 typedef boost::numeric::ublas::vector< value_type > state_type;
Chris@16 136 typedef state_type deriv_type;
Chris@16 137 typedef value_type time_type;
Chris@16 138 typedef boost::numeric::ublas::matrix< value_type > matrix_type;
Chris@16 139 typedef boost::numeric::ublas::permutation_matrix< size_t > pmatrix_type;
Chris@16 140 typedef Resizer resizer_type;
Chris@16 141 typedef Coefficients rosenbrock_coefficients;
Chris@16 142 typedef stepper_tag stepper_category;
Chris@16 143 typedef unsigned short order_type;
Chris@16 144
Chris@16 145 typedef state_wrapper< state_type > wrapped_state_type;
Chris@16 146 typedef state_wrapper< deriv_type > wrapped_deriv_type;
Chris@16 147 typedef state_wrapper< matrix_type > wrapped_matrix_type;
Chris@16 148 typedef state_wrapper< pmatrix_type > wrapped_pmatrix_type;
Chris@16 149
Chris@16 150 typedef rosenbrock4< Value , Coefficients , Resizer > stepper_type;
Chris@16 151
Chris@16 152 const static order_type stepper_order = rosenbrock_coefficients::stepper_order;
Chris@16 153 const static order_type error_order = rosenbrock_coefficients::error_order;
Chris@16 154
Chris@16 155 rosenbrock4( void )
Chris@16 156 : m_resizer() , m_x_err_resizer() ,
Chris@16 157 m_jac() , m_pm() ,
Chris@16 158 m_dfdt() , m_dxdt() , m_dxdtnew() ,
Chris@16 159 m_g1() , m_g2() , m_g3() , m_g4() , m_g5() ,
Chris@16 160 m_cont3() , m_cont4() , m_xtmp() , m_x_err() ,
Chris@16 161 m_coef()
Chris@16 162 { }
Chris@16 163
Chris@16 164
Chris@16 165 order_type order() const { return stepper_order; }
Chris@16 166
Chris@16 167 template< class System >
Chris@16 168 void do_step( System system , const state_type &x , time_type t , state_type &xout , time_type dt , state_type &xerr )
Chris@16 169 {
Chris@16 170 // get the system and jacobi function
Chris@16 171 typedef typename odeint::unwrap_reference< System >::type system_type;
Chris@16 172 typedef typename odeint::unwrap_reference< typename system_type::first_type >::type deriv_func_type;
Chris@16 173 typedef typename odeint::unwrap_reference< typename system_type::second_type >::type jacobi_func_type;
Chris@16 174 system_type &sys = system;
Chris@16 175 deriv_func_type &deriv_func = sys.first;
Chris@16 176 jacobi_func_type &jacobi_func = sys.second;
Chris@16 177
Chris@16 178 const size_t n = x.size();
Chris@16 179
Chris@16 180 m_resizer.adjust_size( x , detail::bind( &stepper_type::template resize_impl<state_type> , detail::ref( *this ) , detail::_1 ) );
Chris@16 181
Chris@16 182 for( size_t i=0 ; i<n ; ++i )
Chris@16 183 m_pm.m_v( i ) = i;
Chris@16 184
Chris@16 185 deriv_func( x , m_dxdt.m_v , t );
Chris@16 186 jacobi_func( x , m_jac.m_v , t , m_dfdt.m_v );
Chris@16 187
Chris@16 188 m_jac.m_v *= -1.0;
Chris@16 189 m_jac.m_v += 1.0 / m_coef.gamma / dt * boost::numeric::ublas::identity_matrix< value_type >( n );
Chris@16 190 boost::numeric::ublas::lu_factorize( m_jac.m_v , m_pm.m_v );
Chris@16 191
Chris@16 192 for( size_t i=0 ; i<n ; ++i )
Chris@16 193 m_g1.m_v[i] = m_dxdt.m_v[i] + dt * m_coef.d1 * m_dfdt.m_v[i];
Chris@16 194 boost::numeric::ublas::lu_substitute( m_jac.m_v , m_pm.m_v , m_g1.m_v );
Chris@16 195
Chris@16 196
Chris@16 197 for( size_t i=0 ; i<n ; ++i )
Chris@16 198 m_xtmp.m_v[i] = x[i] + m_coef.a21 * m_g1.m_v[i];
Chris@16 199 deriv_func( m_xtmp.m_v , m_dxdtnew.m_v , t + m_coef.c2 * dt );
Chris@16 200 for( size_t i=0 ; i<n ; ++i )
Chris@16 201 m_g2.m_v[i] = m_dxdtnew.m_v[i] + dt * m_coef.d2 * m_dfdt.m_v[i] + m_coef.c21 * m_g1.m_v[i] / dt;
Chris@16 202 boost::numeric::ublas::lu_substitute( m_jac.m_v , m_pm.m_v , m_g2.m_v );
Chris@16 203
Chris@16 204
Chris@16 205 for( size_t i=0 ; i<n ; ++i )
Chris@16 206 m_xtmp.m_v[i] = x[i] + m_coef.a31 * m_g1.m_v[i] + m_coef.a32 * m_g2.m_v[i];
Chris@16 207 deriv_func( m_xtmp.m_v , m_dxdtnew.m_v , t + m_coef.c3 * dt );
Chris@16 208 for( size_t i=0 ; i<n ; ++i )
Chris@16 209 m_g3.m_v[i] = m_dxdtnew.m_v[i] + dt * m_coef.d3 * m_dfdt.m_v[i] + ( m_coef.c31 * m_g1.m_v[i] + m_coef.c32 * m_g2.m_v[i] ) / dt;
Chris@16 210 boost::numeric::ublas::lu_substitute( m_jac.m_v , m_pm.m_v , m_g3.m_v );
Chris@16 211
Chris@16 212
Chris@16 213 for( size_t i=0 ; i<n ; ++i )
Chris@16 214 m_xtmp.m_v[i] = x[i] + m_coef.a41 * m_g1.m_v[i] + m_coef.a42 * m_g2.m_v[i] + m_coef.a43 * m_g3.m_v[i];
Chris@16 215 deriv_func( m_xtmp.m_v , m_dxdtnew.m_v , t + m_coef.c4 * dt );
Chris@16 216 for( size_t i=0 ; i<n ; ++i )
Chris@16 217 m_g4.m_v[i] = m_dxdtnew.m_v[i] + dt * m_coef.d4 * m_dfdt.m_v[i] + ( m_coef.c41 * m_g1.m_v[i] + m_coef.c42 * m_g2.m_v[i] + m_coef.c43 * m_g3.m_v[i] ) / dt;
Chris@16 218 boost::numeric::ublas::lu_substitute( m_jac.m_v , m_pm.m_v , m_g4.m_v );
Chris@16 219
Chris@16 220
Chris@16 221 for( size_t i=0 ; i<n ; ++i )
Chris@16 222 m_xtmp.m_v[i] = x[i] + m_coef.a51 * m_g1.m_v[i] + m_coef.a52 * m_g2.m_v[i] + m_coef.a53 * m_g3.m_v[i] + m_coef.a54 * m_g4.m_v[i];
Chris@16 223 deriv_func( m_xtmp.m_v , m_dxdtnew.m_v , t + dt );
Chris@16 224 for( size_t i=0 ; i<n ; ++i )
Chris@16 225 m_g5.m_v[i] = m_dxdtnew.m_v[i] + ( m_coef.c51 * m_g1.m_v[i] + m_coef.c52 * m_g2.m_v[i] + m_coef.c53 * m_g3.m_v[i] + m_coef.c54 * m_g4.m_v[i] ) / dt;
Chris@16 226 boost::numeric::ublas::lu_substitute( m_jac.m_v , m_pm.m_v , m_g5.m_v );
Chris@16 227
Chris@16 228 for( size_t i=0 ; i<n ; ++i )
Chris@16 229 m_xtmp.m_v[i] += m_g5.m_v[i];
Chris@16 230 deriv_func( m_xtmp.m_v , m_dxdtnew.m_v , t + dt );
Chris@16 231 for( size_t i=0 ; i<n ; ++i )
Chris@16 232 xerr[i] = m_dxdtnew.m_v[i] + ( m_coef.c61 * m_g1.m_v[i] + m_coef.c62 * m_g2.m_v[i] + m_coef.c63 * m_g3.m_v[i] + m_coef.c64 * m_g4.m_v[i] + m_coef.c65 * m_g5.m_v[i] ) / dt;
Chris@16 233 boost::numeric::ublas::lu_substitute( m_jac.m_v , m_pm.m_v , xerr );
Chris@16 234
Chris@16 235 for( size_t i=0 ; i<n ; ++i )
Chris@16 236 xout[i] = m_xtmp.m_v[i] + xerr[i];
Chris@16 237 }
Chris@16 238
Chris@16 239 template< class System >
Chris@16 240 void do_step( System system , state_type &x , time_type t , time_type dt , state_type &xerr )
Chris@16 241 {
Chris@16 242 do_step( system , x , t , x , dt , xerr );
Chris@16 243 }
Chris@16 244
Chris@16 245 /*
Chris@16 246 * do_step without error output - just calls above functions with and neglects the error estimate
Chris@16 247 */
Chris@16 248 template< class System >
Chris@16 249 void do_step( System system , const state_type &x , time_type t , state_type &xout , time_type dt )
Chris@16 250 {
Chris@16 251 m_x_err_resizer.adjust_size( x , detail::bind( &stepper_type::template resize_x_err<state_type> , detail::ref( *this ) , detail::_1 ) );
Chris@16 252 do_step( system , x , t , xout , dt , m_x_err.m_v );
Chris@16 253 }
Chris@16 254
Chris@16 255 template< class System >
Chris@16 256 void do_step( System system , state_type &x , time_type t , time_type dt )
Chris@16 257 {
Chris@16 258 m_x_err_resizer.adjust_size( x , detail::bind( &stepper_type::template resize_x_err<state_type> , detail::ref( *this ) , detail::_1 ) );
Chris@16 259 do_step( system , x , t , dt , m_x_err.m_v );
Chris@16 260 }
Chris@16 261
Chris@16 262 void prepare_dense_output()
Chris@16 263 {
Chris@16 264 const size_t n = m_g1.m_v.size();
Chris@16 265 for( size_t i=0 ; i<n ; ++i )
Chris@16 266 {
Chris@16 267 m_cont3.m_v[i] = m_coef.d21 * m_g1.m_v[i] + m_coef.d22 * m_g2.m_v[i] + m_coef.d23 * m_g3.m_v[i] + m_coef.d24 * m_g4.m_v[i] + m_coef.d25 * m_g5.m_v[i];
Chris@16 268 m_cont4.m_v[i] = m_coef.d31 * m_g1.m_v[i] + m_coef.d32 * m_g2.m_v[i] + m_coef.d33 * m_g3.m_v[i] + m_coef.d34 * m_g4.m_v[i] + m_coef.d35 * m_g5.m_v[i];
Chris@16 269 }
Chris@16 270 }
Chris@16 271
Chris@16 272
Chris@16 273 void calc_state( time_type t , state_type &x ,
Chris@16 274 const state_type &x_old , time_type t_old ,
Chris@16 275 const state_type &x_new , time_type t_new )
Chris@16 276 {
Chris@16 277 const size_t n = m_g1.m_v.size();
Chris@16 278 time_type dt = t_new - t_old;
Chris@16 279 time_type s = ( t - t_old ) / dt;
Chris@16 280 time_type s1 = 1.0 - s;
Chris@16 281 for( size_t i=0 ; i<n ; ++i )
Chris@16 282 x[i] = x_old[i] * s1 + s * ( x_new[i] + s1 * ( m_cont3.m_v[i] + s * m_cont4.m_v[i] ) );
Chris@16 283 }
Chris@16 284
Chris@16 285
Chris@16 286
Chris@16 287 template< class StateType >
Chris@16 288 void adjust_size( const StateType &x )
Chris@16 289 {
Chris@16 290 resize_impl( x );
Chris@16 291 resize_x_err( x );
Chris@16 292 }
Chris@16 293
Chris@16 294
Chris@16 295 protected:
Chris@16 296
Chris@16 297 template< class StateIn >
Chris@16 298 bool resize_impl( const StateIn &x )
Chris@16 299 {
Chris@16 300 bool resized = false;
Chris@16 301 resized |= adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable<deriv_type>::type() );
Chris@16 302 resized |= adjust_size_by_resizeability( m_dfdt , x , typename is_resizeable<deriv_type>::type() );
Chris@16 303 resized |= adjust_size_by_resizeability( m_dxdtnew , x , typename is_resizeable<deriv_type>::type() );
Chris@16 304 resized |= adjust_size_by_resizeability( m_xtmp , x , typename is_resizeable<state_type>::type() );
Chris@16 305 resized |= adjust_size_by_resizeability( m_g1 , x , typename is_resizeable<state_type>::type() );
Chris@16 306 resized |= adjust_size_by_resizeability( m_g2 , x , typename is_resizeable<state_type>::type() );
Chris@16 307 resized |= adjust_size_by_resizeability( m_g3 , x , typename is_resizeable<state_type>::type() );
Chris@16 308 resized |= adjust_size_by_resizeability( m_g4 , x , typename is_resizeable<state_type>::type() );
Chris@16 309 resized |= adjust_size_by_resizeability( m_g5 , x , typename is_resizeable<state_type>::type() );
Chris@16 310 resized |= adjust_size_by_resizeability( m_cont3 , x , typename is_resizeable<state_type>::type() );
Chris@16 311 resized |= adjust_size_by_resizeability( m_cont4 , x , typename is_resizeable<state_type>::type() );
Chris@16 312 resized |= adjust_size_by_resizeability( m_jac , x , typename is_resizeable<matrix_type>::type() );
Chris@16 313 resized |= adjust_size_by_resizeability( m_pm , x , typename is_resizeable<pmatrix_type>::type() );
Chris@16 314 return resized;
Chris@16 315 }
Chris@16 316
Chris@16 317 template< class StateIn >
Chris@16 318 bool resize_x_err( const StateIn &x )
Chris@16 319 {
Chris@16 320 return adjust_size_by_resizeability( m_x_err , x , typename is_resizeable<state_type>::type() );
Chris@16 321 }
Chris@16 322
Chris@16 323 private:
Chris@16 324
Chris@16 325
Chris@16 326 resizer_type m_resizer;
Chris@16 327 resizer_type m_x_err_resizer;
Chris@16 328
Chris@16 329 wrapped_matrix_type m_jac;
Chris@16 330 wrapped_pmatrix_type m_pm;
Chris@16 331 wrapped_deriv_type m_dfdt , m_dxdt , m_dxdtnew;
Chris@16 332 wrapped_state_type m_g1 , m_g2 , m_g3 , m_g4 , m_g5;
Chris@16 333 wrapped_state_type m_cont3 , m_cont4;
Chris@16 334 wrapped_state_type m_xtmp;
Chris@16 335 wrapped_state_type m_x_err;
Chris@16 336
Chris@16 337 const rosenbrock_coefficients m_coef;
Chris@16 338 };
Chris@16 339
Chris@16 340
Chris@16 341 } // namespace odeint
Chris@16 342 } // namespace numeric
Chris@16 343 } // namespace boost
Chris@16 344
Chris@16 345 #endif // BOOST_NUMERIC_ODEINT_STEPPER_ROSENBROCK4_HPP_INCLUDED