annotate DEPENDENCIES/generic/include/boost/numeric/odeint/stepper/implicit_euler.hpp @ 46:d572322e2efe

Fix to .cat file check (was susceptible to DOS line-endings) and subrepo update
author Chris Cannam
date Thu, 07 Aug 2014 14:39:38 +0100
parents 2665513ce2d3
children c530137014c0
rev   line source
Chris@16 1 /*
Chris@16 2 [auto_generated]
Chris@16 3 boost/numeric/odeint/stepper/implicit_euler.hpp
Chris@16 4
Chris@16 5 [begin_description]
Chris@16 6 Impementation of the implicit Euler method. Works with ublas::vector as state type.
Chris@16 7 [end_description]
Chris@16 8
Chris@16 9 Copyright 2009-2011 Karsten Ahnert
Chris@16 10 Copyright 2009-2011 Mario Mulansky
Chris@16 11
Chris@16 12 Distributed under the Boost Software License, Version 1.0.
Chris@16 13 (See accompanying file LICENSE_1_0.txt or
Chris@16 14 copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 15 */
Chris@16 16
Chris@16 17
Chris@16 18 #ifndef BOOST_NUMERIC_ODEINT_STEPPER_IMPLICIT_EULER_HPP_INCLUDED
Chris@16 19 #define BOOST_NUMERIC_ODEINT_STEPPER_IMPLICIT_EULER_HPP_INCLUDED
Chris@16 20
Chris@16 21
Chris@16 22 #include <utility>
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/odeint/stepper/stepper_categories.hpp>
Chris@16 27
Chris@16 28 #include <boost/numeric/odeint/util/ublas_wrapper.hpp>
Chris@16 29 #include <boost/numeric/odeint/util/is_resizeable.hpp>
Chris@16 30 #include <boost/numeric/odeint/util/resizer.hpp>
Chris@16 31
Chris@16 32 #include <boost/numeric/ublas/vector.hpp>
Chris@16 33 #include <boost/numeric/ublas/matrix.hpp>
Chris@16 34 #include <boost/numeric/ublas/lu.hpp>
Chris@16 35
Chris@16 36 namespace boost {
Chris@16 37 namespace numeric {
Chris@16 38 namespace odeint {
Chris@16 39
Chris@16 40
Chris@16 41
Chris@16 42
Chris@16 43
Chris@16 44
Chris@16 45
Chris@16 46
Chris@16 47 template< class ValueType , class Resizer = initially_resizer >
Chris@16 48 class implicit_euler
Chris@16 49 {
Chris@16 50
Chris@16 51 public:
Chris@16 52
Chris@16 53 typedef ValueType value_type;
Chris@16 54 typedef value_type time_type;
Chris@16 55 typedef boost::numeric::ublas::vector< value_type > state_type;
Chris@16 56 typedef state_wrapper< state_type > wrapped_state_type;
Chris@16 57 typedef state_type deriv_type;
Chris@16 58 typedef state_wrapper< deriv_type > wrapped_deriv_type;
Chris@16 59 typedef boost::numeric::ublas::matrix< value_type > matrix_type;
Chris@16 60 typedef state_wrapper< matrix_type > wrapped_matrix_type;
Chris@16 61 typedef boost::numeric::ublas::permutation_matrix< size_t > pmatrix_type;
Chris@16 62 typedef state_wrapper< pmatrix_type > wrapped_pmatrix_type;
Chris@16 63 typedef Resizer resizer_type;
Chris@16 64 typedef stepper_tag stepper_category;
Chris@16 65 typedef implicit_euler< ValueType , Resizer > stepper_type;
Chris@16 66
Chris@16 67 implicit_euler( value_type epsilon = 1E-6 )
Chris@16 68 : m_epsilon( epsilon )
Chris@16 69 { }
Chris@16 70
Chris@16 71
Chris@16 72 template< class System >
Chris@16 73 void do_step( System system , state_type &x , time_type t , time_type dt )
Chris@16 74 {
Chris@16 75 typedef typename odeint::unwrap_reference< System >::type system_type;
Chris@16 76 typedef typename odeint::unwrap_reference< typename system_type::first_type >::type deriv_func_type;
Chris@16 77 typedef typename odeint::unwrap_reference< typename system_type::second_type >::type jacobi_func_type;
Chris@16 78 system_type &sys = system;
Chris@16 79 deriv_func_type &deriv_func = sys.first;
Chris@16 80 jacobi_func_type &jacobi_func = sys.second;
Chris@16 81
Chris@16 82 m_resizer.adjust_size( x , detail::bind( &stepper_type::template resize_impl<state_type> , detail::ref( *this ) , detail::_1 ) );
Chris@16 83
Chris@16 84 for( size_t i=0 ; i<x.size() ; ++i )
Chris@16 85 m_pm.m_v[i] = i;
Chris@16 86
Chris@16 87 t += dt;
Chris@16 88
Chris@16 89 // apply first Newton step
Chris@16 90 deriv_func( x , m_dxdt.m_v , t );
Chris@16 91
Chris@16 92 m_b.m_v = dt * m_dxdt.m_v;
Chris@16 93
Chris@16 94 jacobi_func( x , m_jacobi.m_v , t );
Chris@16 95 m_jacobi.m_v *= dt;
Chris@16 96 m_jacobi.m_v -= boost::numeric::ublas::identity_matrix< value_type >( x.size() );
Chris@16 97
Chris@16 98 solve( m_b.m_v , m_jacobi.m_v );
Chris@16 99
Chris@16 100 m_x.m_v = x - m_b.m_v;
Chris@16 101
Chris@16 102 // iterate Newton until some precision is reached
Chris@16 103 // ToDo: maybe we should apply only one Newton step -> linear implicit one-step scheme
Chris@16 104 while( boost::numeric::ublas::norm_2( m_b.m_v ) > m_epsilon )
Chris@16 105 {
Chris@16 106 deriv_func( m_x.m_v , m_dxdt.m_v , t );
Chris@16 107 m_b.m_v = x - m_x.m_v + dt*m_dxdt.m_v;
Chris@16 108
Chris@16 109 // simplified version, only the first Jacobian is used
Chris@16 110 // jacobi( m_x , m_jacobi , t );
Chris@16 111 // m_jacobi *= dt;
Chris@16 112 // m_jacobi -= boost::numeric::ublas::identity_matrix< value_type >( x.size() );
Chris@16 113
Chris@16 114 solve( m_b.m_v , m_jacobi.m_v );
Chris@16 115
Chris@16 116 m_x.m_v -= m_b.m_v;
Chris@16 117 }
Chris@16 118 x = m_x.m_v;
Chris@16 119 }
Chris@16 120
Chris@16 121 template< class StateType >
Chris@16 122 void adjust_size( const StateType &x )
Chris@16 123 {
Chris@16 124 resize_impl( x );
Chris@16 125 }
Chris@16 126
Chris@16 127
Chris@16 128 private:
Chris@16 129
Chris@16 130 template< class StateIn >
Chris@16 131 bool resize_impl( const StateIn &x )
Chris@16 132 {
Chris@16 133 bool resized = false;
Chris@16 134 resized |= adjust_size_by_resizeability( m_dxdt , x , typename is_resizeable<deriv_type>::type() );
Chris@16 135 resized |= adjust_size_by_resizeability( m_x , x , typename is_resizeable<state_type>::type() );
Chris@16 136 resized |= adjust_size_by_resizeability( m_b , x , typename is_resizeable<deriv_type>::type() );
Chris@16 137 resized |= adjust_size_by_resizeability( m_jacobi , x , typename is_resizeable<matrix_type>::type() );
Chris@16 138 resized |= adjust_size_by_resizeability( m_pm , x , typename is_resizeable<pmatrix_type>::type() );
Chris@16 139 return resized;
Chris@16 140 }
Chris@16 141
Chris@16 142
Chris@16 143 void solve( state_type &x , matrix_type &m )
Chris@16 144 {
Chris@16 145 int res = boost::numeric::ublas::lu_factorize( m , m_pm.m_v );
Chris@16 146 if( res != 0 ) exit(0);
Chris@16 147 boost::numeric::ublas::lu_substitute( m , m_pm.m_v , x );
Chris@16 148 }
Chris@16 149
Chris@16 150 private:
Chris@16 151
Chris@16 152 value_type m_epsilon;
Chris@16 153 resizer_type m_resizer;
Chris@16 154 wrapped_deriv_type m_dxdt;
Chris@16 155 wrapped_state_type m_x;
Chris@16 156 wrapped_deriv_type m_b;
Chris@16 157 wrapped_matrix_type m_jacobi;
Chris@16 158 wrapped_pmatrix_type m_pm;
Chris@16 159
Chris@16 160
Chris@16 161 };
Chris@16 162
Chris@16 163
Chris@16 164 } // odeint
Chris@16 165 } // numeric
Chris@16 166 } // boost
Chris@16 167
Chris@16 168
Chris@16 169 #endif // BOOST_NUMERIC_ODEINT_STEPPER_IMPLICIT_EULER_HPP_INCLUDED