Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
iteration.hpp
Go to the documentation of this file.
1 /* -*- mode: c++; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; show-trailing-whitespace: t -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
2 
3  This file is part of the Feel library
4 
5  Author(s): Christophe Prud'homme <christophe.prudhomme@feelpp.org>
6  Date: 2005-04-07
7 
8  Copyright (C) 2005,2006 EPFL
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 3.0 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
29 #ifndef __Iteration_H
30 #define __Iteration_H 1
31 
32 #include <boost/shared_ptr.hpp>
33 
34 namespace Feel
35 {
56 template<typename Real>
57 class Iteration
58 {
59 public:
60 
61 
65 
69  typedef Real NumericalType;
70  typedef typename ublas::type_traits<Real>::value_type value_type;
71  typedef typename ublas::type_traits<Real>::real_type real_type;
73 
77 
82  {
83  return new Iteration<NumericalType>;
84  }
85 
86  Iteration( Iteration const& iter )
87  :
88  __iterations( iter.__iterations ),
89  __max_iter( iter.__max_iter ),
90  __residual( iter.__residual ),
91  __precision( iter.__precision ),
92  __norm_init( iter.__norm_init )
93  {
94  // do nothing here
95  }
96 
97 
99  virtual ~Iteration()
100  {
101  // do nothing here
102  }
103 
105 
109 
112  {
113  if ( this == &iter )
114  {
115  return *this;
116  }
117 
118  __precision = iter.__precision;
119  __max_iter = iter.__max_iter;
120  __residual = iter.__residual;
121  __norm_init = iter.__norm_init;
122  return *this;
123  }
124 
126  void operator++() throw()
127  {
128  ++__iterations;
129  }
130 
132 
136 
137  int numberOfIterations() const
138  {
139  return __iterations;
140  }
141 
145  real_type residual() const throw()
146  {
147  return __residual;
148  }
149 
150  real_type relativePrecision() const
151  {
152  return __precision;
153  }
154 
155  int maximumNumberOfIterations() const
156  {
157  return __max_iter;
158  }
159 
160  NumericalType initialResidual() const
161  {
162  return __norm_init;
163  }
164 
165  real_type relaxation () const
166  {
167  return M_relaxation;
168  }
169  int iteration() const
170  {
171  return __iterations;
172  }
173 
177 
179 
182  void setMaximumNumberOfIterations( int m ) throw()
183  {
184  __max_iter = m;
185  }
186 
188 
192  {
193  __precision = p;
194  }
195 
197 
200  void setInitialResidual( NumericalType ninit ) throw()
201  {
202  __norm_init = ninit;
203  }
204 
205  void setRelaxation ( real_type __w )
206  {
207  M_relaxation = __w;
208  }
210 
214 
227  bool isFinished( NumericalType r, bool verbose = false ) //throw(SExceptionSolverHasNotConverged)
228  {
229  bool ret = false;
230 
231  if ( this->isConverged( r ) )
232  {
233  ret = true;
234  }
235 
236  else if ( __iterations >= __max_iter )
237  {
238  ret = true;
239  }
240 
241  else
242  {
243 #if 0
244  handleEvents( true );
245  std::string why = "Solver has not converged";
246  SExceptionSolverHasNotConverged __e( why.c_str(), LOCATION );
247  __e.setNumberOfIterations( __iterations );
248  __e.setResidual( __residual );
249  throw __e;
250 #endif
251  }
252 
253  handleEvents( ret, verbose );
254 
255  return ret;
256  }
257 
258 
259  template<typename VectorX>
260  bool isFinished( const VectorX& r, bool verbose = false ) //throw(SExceptionSolverHasNotConverged)
261  {
262  bool ret = false;
263 
264  if ( this->isConverged( r ) )
265  {
266  ret = true;
267  }
268 
269  else if ( __iterations >= __max_iter )
270  {
271  ret = true;
272  }
273 
274  else
275  {
276 #if 0
277  handleEvents( true );
278  std::string why = "Solver has not converged";
279  SExceptionSolverHasNotConverged __e( why.c_str(), LOCATION );
280  __e.setNumberOfIterations( __iterations );
281  __e.setResidual( __residual );
282  throw __e;
283 #endif
284  }
285 
286  handleEvents( ret, verbose );
287 
288  return ret;
289  }
290 
291  bool isConverged( NumericalType r ) throw()
292  {
293  __residual = r / __norm_init;
294  return ( __residual <= __precision );
295  }
296 
297  template<typename VectorX> bool isConverged( VectorX const& x ) throw()
298  {
299  __residual = ublas::norm_2( x ) / __norm_init;
300  return ( __residual <= __precision );
301  }
302 
303  bool isFirst() const
304  {
305  return ( __iterations == 0 );
306  }
307 
308  void reset()
309  {
310  __iterations = 0;
311  }
312 
314 
315 protected:
316 
322  :
323  __iterations( 0 ),
324  __max_iter( 0 ),
325  __residual( 0 ),
326  __precision( 0 ),
327  __norm_init( 1.0 ),
328  M_relaxation( 1.0 )
329  {
330  // do nothing here
331  }
332 
333  virtual void handleEvents( bool __is_finished, bool verbose )
334  {
335  if ( __iterations == 0 )
336  {
337  //SEvent startEv( IterationStarted, new IterationStarted<value_type>( __residual, __iterations) );
338  //SSubject::notifyObservers( &startEv );
339  if ( verbose )
340  std::cout << "iteration " << __iterations << " : " << residual() << "\n";
341  }
342 
343  if ( __is_finished == true )
344  {
345  //SEvent finishEv( IterationFinished, new IterationFinished<value_type>( __residual, __iterations) );
346  //SSubject::notifyObservers( &finishEv );
347  if ( verbose )
348  std::cout << "iteration " << __iterations << " : " << residual() << "\n";
349  }
350 
351  else
352  {
353  if ( verbose )
354  std::cout << "iteration " << __iterations << " : " << residual() << "\n";
355 
356  //SEvent aEvent( IterationUpdated, new IterationUpdated<value_type>( __residual, __iterations) );
357  //SSubject::notifyObservers( &aEvent );
358  }
359  }
360 private:
361 
362  int __iterations;
363  int __max_iter;
364 
365  real_type __residual;
366  real_type __precision;
367  real_type __norm_init;
368 
369  real_type M_relaxation;
370 };
371 
372 typedef Iteration<double> iteration_type;
373 typedef boost::shared_ptr<iteration_type> iteration_ptrtype;
374 }
375 
376 
377 #endif /* __Iteration_H */
Iteration & operator=(Iteration< NumericalType > const &iter)
copy operator
Definition: iteration.hpp:111
Iteration()
Definition: iteration.hpp:321
virtual ~Iteration()
destructor
Definition: iteration.hpp:99
brief description
Definition: iteration.hpp:57
void operator++()
prefix ++ operator
Definition: iteration.hpp:126
void setMaximumNumberOfIterations(int m)
set the Max number of iterations
Definition: iteration.hpp:182
real_type residual() const
get the Residual
Definition: iteration.hpp:145
Real NumericalType
Numerical Type.
Definition: iteration.hpp:69
static Iteration< NumericalType > * New()
create a new instance
Definition: iteration.hpp:81
bool isFinished(NumericalType r, bool verbose=false)
tells if the iteration finished
Definition: iteration.hpp:227
void setRelativePrecision(NumericalType p)
set the relative precision to reach
Definition: iteration.hpp:191
void setInitialResidual(NumericalType ninit)
initial norm for the residual
Definition: iteration.hpp:200

Generated on Sun Dec 22 2013 13:11:05 for Feel++ by doxygen 1.8.5