implicit_euler.hpp 5.1 KB

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