You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.8 KiB

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