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