implicit_euler.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/stepper/implicit_euler.hpp
  4. [begin_description]
  5. Impementation of the implicit Euler method. Works with ublas::vector as state type.
  6. [end_description]
  7. Copyright 2009-2011 Karsten Ahnert
  8. Copyright 2009-2011 Mario Mulansky
  9. Distributed under the Boost Software License, Version 1.0.
  10. (See accompanying file LICENSE_1_0.txt or
  11. copy at http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. #ifndef BOOST_NUMERIC_ODEINT_STEPPER_IMPLICIT_EULER_HPP_INCLUDED
  14. #define BOOST_NUMERIC_ODEINT_STEPPER_IMPLICIT_EULER_HPP_INCLUDED
  15. #include <utility>
  16. #include <boost/numeric/odeint/util/bind.hpp>
  17. #include <boost/numeric/odeint/util/unwrap_reference.hpp>
  18. #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
  19. #include <boost/numeric/odeint/util/ublas_wrapper.hpp>
  20. #include <boost/numeric/odeint/util/is_resizeable.hpp>
  21. #include <boost/numeric/odeint/util/resizer.hpp>
  22. #include <boost/numeric/ublas/vector.hpp>
  23. #include <boost/numeric/ublas/matrix.hpp>
  24. #include <boost/numeric/ublas/lu.hpp>
  25. namespace boost {
  26. namespace numeric {
  27. namespace odeint {
  28. template< class ValueType , class Resizer = initially_resizer >
  29. class implicit_euler
  30. {
  31. public:
  32. typedef ValueType value_type;
  33. typedef value_type time_type;
  34. typedef boost::numeric::ublas::vector< value_type > state_type;
  35. typedef state_wrapper< state_type > wrapped_state_type;
  36. typedef state_type deriv_type;
  37. typedef state_wrapper< deriv_type > wrapped_deriv_type;
  38. typedef boost::numeric::ublas::matrix< value_type > matrix_type;
  39. typedef state_wrapper< matrix_type > wrapped_matrix_type;
  40. typedef boost::numeric::ublas::permutation_matrix< size_t > pmatrix_type;
  41. typedef state_wrapper< pmatrix_type > wrapped_pmatrix_type;
  42. typedef Resizer resizer_type;
  43. typedef stepper_tag stepper_category;
  44. typedef implicit_euler< ValueType , Resizer > stepper_type;
  45. implicit_euler( value_type epsilon = 1E-6 )
  46. : m_epsilon( epsilon )
  47. { }
  48. template< class System >
  49. void do_step( System system , state_type &x , time_type t , time_type dt )
  50. {
  51. typedef typename odeint::unwrap_reference< System >::type system_type;
  52. typedef typename odeint::unwrap_reference< typename system_type::first_type >::type deriv_func_type;
  53. typedef typename odeint::unwrap_reference< typename system_type::second_type >::type jacobi_func_type;
  54. system_type &sys = system;
  55. deriv_func_type &deriv_func = sys.first;
  56. jacobi_func_type &jacobi_func = sys.second;
  57. m_resizer.adjust_size( x , detail::bind( &stepper_type::template resize_impl<state_type> , detail::ref( *this ) , detail::_1 ) );
  58. for( size_t i=0 ; i<x.size() ; ++i )
  59. m_pm.m_v[i] = i;
  60. t += dt;
  61. // apply first Newton step
  62. deriv_func( x , m_dxdt.m_v , t );
  63. m_b.m_v = dt * m_dxdt.m_v;
  64. jacobi_func( x , m_jacobi.m_v , t );
  65. m_jacobi.m_v *= dt;
  66. m_jacobi.m_v -= boost::numeric::ublas::identity_matrix< value_type >( x.size() );
  67. solve( m_b.m_v , m_jacobi.m_v );
  68. m_x.m_v = x - m_b.m_v;
  69. // iterate Newton until some precision is reached
  70. // ToDo: maybe we should apply only one Newton step -> linear implicit one-step scheme
  71. while( boost::numeric::ublas::norm_2( m_b.m_v ) > m_epsilon )
  72. {
  73. deriv_func( m_x.m_v , m_dxdt.m_v , t );
  74. m_b.m_v = x - m_x.m_v + dt*m_dxdt.m_v;
  75. // simplified version, only the first Jacobian is used
  76. // jacobi( m_x , m_jacobi , t );
  77. // m_jacobi *= dt;
  78. // m_jacobi -= boost::numeric::ublas::identity_matrix< value_type >( x.size() );
  79. solve( m_b.m_v , m_jacobi.m_v );
  80. m_x.m_v -= m_b.m_v;
  81. }
  82. x = m_x.m_v;
  83. }
  84. template< class StateType >
  85. void adjust_size( const StateType &x )
  86. {
  87. resize_impl( x );
  88. }
  89. private:
  90. template< class StateIn >
  91. bool resize_impl( const StateIn &x )
  92. {
  93. bool resized = false;
  94. resized |= adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable<deriv_type>::type() );
  95. resized |= adjust_size_by_resizeability( m_x , x , typename is_resizeable<state_type>::type() );
  96. resized |= adjust_size_by_resizeability( m_b , x , typename is_resizeable<deriv_type>::type() );
  97. resized |= adjust_size_by_resizeability( m_jacobi , x , typename is_resizeable<matrix_type>::type() );
  98. resized |= adjust_size_by_resizeability( m_pm , x , typename is_resizeable<pmatrix_type>::type() );
  99. return resized;
  100. }
  101. void solve( state_type &x , matrix_type &m )
  102. {
  103. int res = boost::numeric::ublas::lu_factorize( m , m_pm.m_v );
  104. if( res != 0 ) exit(0);
  105. boost::numeric::ublas::lu_substitute( m , m_pm.m_v , x );
  106. }
  107. private:
  108. value_type m_epsilon;
  109. resizer_type m_resizer;
  110. wrapped_deriv_type m_dxdt;
  111. wrapped_state_type m_x;
  112. wrapped_deriv_type m_b;
  113. wrapped_matrix_type m_jacobi;
  114. wrapped_pmatrix_type m_pm;
  115. };
  116. } // odeint
  117. } // numeric
  118. } // boost
  119. #endif // BOOST_NUMERIC_ODEINT_STEPPER_IMPLICIT_EULER_HPP_INCLUDED