rosenbrock4.hpp 14 KB

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