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