Chris@16: /* Chris@16: [auto_generated] Chris@16: boost/numeric/odeint/stepper/implicit_euler.hpp Chris@16: Chris@16: [begin_description] Chris@16: Impementation of the implicit Euler method. Works with ublas::vector as state type. Chris@16: [end_description] Chris@16: Chris@101: Copyright 2010-2012 Mario Mulansky Chris@101: Copyright 2010-2012 Karsten Ahnert Chris@101: Copyright 2012 Christoph Koke Chris@16: Chris@16: Distributed under the Boost Software License, Version 1.0. Chris@16: (See accompanying file LICENSE_1_0.txt or Chris@16: copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: Chris@16: Chris@16: #ifndef BOOST_NUMERIC_ODEINT_STEPPER_IMPLICIT_EULER_HPP_INCLUDED Chris@16: #define BOOST_NUMERIC_ODEINT_STEPPER_IMPLICIT_EULER_HPP_INCLUDED Chris@16: Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace numeric { Chris@16: namespace odeint { Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: template< class ValueType , class Resizer = initially_resizer > Chris@16: class implicit_euler Chris@16: { Chris@16: Chris@16: public: Chris@16: Chris@16: typedef ValueType value_type; Chris@16: typedef value_type time_type; Chris@16: typedef boost::numeric::ublas::vector< value_type > state_type; Chris@16: typedef state_wrapper< state_type > wrapped_state_type; Chris@16: typedef state_type deriv_type; Chris@16: typedef state_wrapper< deriv_type > wrapped_deriv_type; Chris@16: typedef boost::numeric::ublas::matrix< value_type > matrix_type; Chris@16: typedef state_wrapper< matrix_type > wrapped_matrix_type; Chris@16: typedef boost::numeric::ublas::permutation_matrix< size_t > pmatrix_type; Chris@16: typedef state_wrapper< pmatrix_type > wrapped_pmatrix_type; Chris@16: typedef Resizer resizer_type; Chris@16: typedef stepper_tag stepper_category; Chris@16: typedef implicit_euler< ValueType , Resizer > stepper_type; Chris@16: Chris@16: implicit_euler( value_type epsilon = 1E-6 ) Chris@16: : m_epsilon( epsilon ) Chris@16: { } Chris@16: Chris@16: Chris@16: template< class System > Chris@16: void do_step( System system , state_type &x , time_type t , time_type dt ) Chris@16: { Chris@16: typedef typename odeint::unwrap_reference< System >::type system_type; Chris@16: typedef typename odeint::unwrap_reference< typename system_type::first_type >::type deriv_func_type; Chris@16: typedef typename odeint::unwrap_reference< typename system_type::second_type >::type jacobi_func_type; Chris@16: system_type &sys = system; Chris@16: deriv_func_type &deriv_func = sys.first; Chris@16: jacobi_func_type &jacobi_func = sys.second; Chris@16: Chris@16: m_resizer.adjust_size( x , detail::bind( &stepper_type::template resize_impl , detail::ref( *this ) , detail::_1 ) ); Chris@16: Chris@16: for( size_t i=0 ; i( x.size() ); Chris@16: Chris@16: solve( m_b.m_v , m_jacobi.m_v ); Chris@16: Chris@16: m_x.m_v = x - m_b.m_v; Chris@16: Chris@16: // iterate Newton until some precision is reached Chris@16: // ToDo: maybe we should apply only one Newton step -> linear implicit one-step scheme Chris@16: while( boost::numeric::ublas::norm_2( m_b.m_v ) > m_epsilon ) Chris@16: { Chris@16: deriv_func( m_x.m_v , m_dxdt.m_v , t ); Chris@16: m_b.m_v = x - m_x.m_v + dt*m_dxdt.m_v; Chris@16: Chris@16: // simplified version, only the first Jacobian is used Chris@16: // jacobi( m_x , m_jacobi , t ); Chris@16: // m_jacobi *= dt; Chris@16: // m_jacobi -= boost::numeric::ublas::identity_matrix< value_type >( x.size() ); Chris@16: Chris@16: solve( m_b.m_v , m_jacobi.m_v ); Chris@16: Chris@16: m_x.m_v -= m_b.m_v; Chris@16: } Chris@16: x = m_x.m_v; Chris@16: } Chris@16: Chris@16: template< class StateType > Chris@16: void adjust_size( const StateType &x ) Chris@16: { Chris@16: resize_impl( x ); Chris@16: } Chris@16: Chris@16: Chris@16: private: Chris@16: Chris@16: template< class StateIn > Chris@16: bool resize_impl( const StateIn &x ) Chris@16: { Chris@16: bool resized = false; Chris@16: resized |= adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable::type() ); Chris@16: resized |= adjust_size_by_resizeability( m_x , x , typename is_resizeable::type() ); Chris@16: resized |= adjust_size_by_resizeability( m_b , x , typename is_resizeable::type() ); Chris@16: resized |= adjust_size_by_resizeability( m_jacobi , x , typename is_resizeable::type() ); Chris@16: resized |= adjust_size_by_resizeability( m_pm , x , typename is_resizeable::type() ); Chris@16: return resized; Chris@16: } Chris@16: Chris@16: Chris@16: void solve( state_type &x , matrix_type &m ) Chris@16: { Chris@16: int res = boost::numeric::ublas::lu_factorize( m , m_pm.m_v ); Chris@101: if( res != 0 ) std::exit(0); Chris@16: boost::numeric::ublas::lu_substitute( m , m_pm.m_v , x ); Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: value_type m_epsilon; Chris@16: resizer_type m_resizer; Chris@16: wrapped_deriv_type m_dxdt; Chris@16: wrapped_state_type m_x; Chris@16: wrapped_deriv_type m_b; Chris@16: wrapped_matrix_type m_jacobi; Chris@16: wrapped_pmatrix_type m_pm; Chris@16: Chris@16: Chris@16: }; Chris@16: Chris@16: Chris@16: } // odeint Chris@16: } // numeric Chris@16: } // boost Chris@16: Chris@16: Chris@16: #endif // BOOST_NUMERIC_ODEINT_STEPPER_IMPLICIT_EULER_HPP_INCLUDED