annotate DEPENDENCIES/generic/include/boost/numeric/odeint/stepper/rosenbrock4.hpp @ 133:4acb5d8d80b6 tip

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