GetFEM  5.4.2
gmm_iter.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-2020 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /**@file gmm_iter.h
33  @author Yves Renard <[email protected]>
34  @date February 10, 2003.
35  @brief Iteration object.
36 */
37 
38 #ifndef GMM_ITER_H__
39 #define GMM_ITER_H__
40 
41 #include "gmm_kernel.h"
42 #include <iomanip>
43 
44 namespace gmm {
45 
46  /** The Iteration object calculates whether the solution has reached the
47  desired accuracy, or whether the maximum number of iterations has
48  been reached.
49 
50  The method finished() checks the convergence. The first()
51  method is used to determine the first iteration of the loop.
52  */
53  class iteration {
54  protected :
55  double rhsn; /* Right hand side norm. */
56  size_type maxiter; /* Max. number of iterations. */
57  int noise; /* if noise > 0 iterations are printed. */
58  double resmax; /* maximum residu. */
59  double resminreach, resadd;
60  double diverged_res; /* Threshold beyond which the iterative */
61  /* is considered to diverge. */
62  size_type nit; /* iteration number. */
63  double res; /* last computed residu. */
64  std::string name; /* eventually, name of the method. */
65  bool written;
66  void (*callback)(const gmm::iteration&);
67  public :
68 
69  void init(void) {
70  nit = 0; res = 0.0; written = false;
71  resminreach = 1E200; resadd = 0.0;
72  callback = 0;
73  }
74 
75  iteration(double r = 1.0E-8, int noi = 0, size_type mit = size_type(-1),
76  double div_res = 1E200)
77  : rhsn(1.0), maxiter(mit), noise(noi), resmax(r), diverged_res(div_res)
78  { init(); }
79 
80  void operator ++(int) { nit++; written = false; resadd += res; }
81  void operator ++() { (*this)++; }
82 
83  bool first(void) { return nit == 0; }
84 
85  /* get/set the "noisyness" (verbosity) of the solvers */
86  int get_noisy(void) const { return noise; }
87  void set_noisy(int n) { noise = n; }
88  void reduce_noisy(void) { if (noise > 0) noise--; }
89 
90  double get_resmax(void) const { return resmax; }
91  void set_resmax(double r) { resmax = r; }
92 
93  double get_res() const { return res; }
94  void enforce_converged(bool c = true)
95  { if (c) res = double(0); else res = rhsn * resmax + double(1); }
96 
97  /* change the user-definable callback, called after each iteration */
98  void set_callback(void (*t)(const gmm::iteration&)) {
99  callback = t;
100  }
101 
102  double get_diverged_residual(void) const { return diverged_res; }
103  void set_diverged_residual(double r) { diverged_res = r; }
104 
105  size_type get_iteration(void) const { return nit; }
106  void set_iteration(size_type i) { nit = i; }
107 
108  size_type get_maxiter(void) const { return maxiter; }
109  void set_maxiter(size_type i) { maxiter = i; }
110 
111  double get_rhsnorm(void) const { return rhsn; }
112  void set_rhsnorm(double r) { rhsn = r; }
113 
114  bool converged(void) {
115  return !isnan(res) && res <= rhsn * resmax;
116  }
117  bool converged(double nr) {
118  res = gmm::abs(nr);
119  resminreach = std::min(resminreach, res);
120  return converged();
121  }
122  template <typename VECT> bool converged(const VECT &v)
123  { return converged(gmm::vect_norm2(v)); }
124  bool diverged(void) {
125  return isnan(res) || (nit>=maxiter)
126  || (res>=rhsn*diverged_res && nit > 4);
127  }
128  bool diverged(double nr) {
129  res = gmm::abs(nr);
130  resminreach = std::min(resminreach, res);
131  return diverged();
132  }
133 
134  bool finished(double nr) {
135  if (callback) callback(*this);
136  if (noise > 0 && !written) {
137  double a = (rhsn == 0) ? 1.0 : rhsn;
138  converged(nr);
139  cout << name << " iter " << std::setw(3) << nit << " residual "
140  << std::setw(12) << gmm::abs(nr) / a;
141 // if (nit % 100 == 0 && nit > 0) {
142 // cout << " (residual min " << resminreach / a << " mean val "
143 // << resadd / (100.0 * a) << " )";
144 // resadd = 0.0;
145 // }
146  cout << endl;
147  written = true;
148  }
149  return (converged(nr) || diverged(nr));
150  }
151  template <typename VECT> bool finished_vect(const VECT &v)
152  { return finished(double(gmm::vect_norm2(v))); }
153 
154 
155  void set_name(const std::string &n) { name = n; }
156  const std::string &get_name(void) const { return name; }
157 
158  };
159 
160 }
161 
162 #endif /* GMM_ITER_H__ */
bgeot::size_type
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49
gmm::iteration
The Iteration object calculates whether the solution has reached the desired accuracy,...
Definition: gmm_iter.h:53
gmm::vect_norm2
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norm2(const V &v)
Euclidean norm of a vector.
Definition: gmm_blas.h:557
gmm_kernel.h
Include the base gmm files.