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.

173 lines
7.0 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2017 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. // This file is a modified version of gmres.h from ITL.
  27. // See http://osl.iu.edu/research/itl/
  28. // Following the corresponding Copyright notice.
  29. //===========================================================================
  30. //
  31. // Copyright (c) 1998-2001, University of Notre Dame. All rights reserved.
  32. // Redistribution and use in source and binary forms, with or without
  33. // modification, are permitted provided that the following conditions are met:
  34. //
  35. // * Redistributions of source code must retain the above copyright
  36. // notice, this list of conditions and the following disclaimer.
  37. // * Redistributions in binary form must reproduce the above copyright
  38. // notice, this list of conditions and the following disclaimer in the
  39. // documentation and/or other materials provided with the distribution.
  40. // * Neither the name of the University of Notre Dame nor the
  41. // names of its contributors may be used to endorse or promote products
  42. // derived from this software without specific prior written permission.
  43. //
  44. // THIS SOFTWARE IS PROVIDED BY THE TRUSTEES OF INDIANA UNIVERSITY AND
  45. // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  46. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  47. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES
  48. // OF INDIANA UNIVERSITY AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  49. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  50. // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  51. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  52. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  53. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  54. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  55. //
  56. //===========================================================================
  57. /**@file gmm_solver_gmres.h
  58. @author Andrew Lumsdaine <lums@osl.iu.edu>
  59. @author Lie-Quan Lee <llee@osl.iu.edu>
  60. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  61. @date October 13, 2002.
  62. @brief GMRES (Generalized Minimum Residual) iterative solver.
  63. */
  64. #ifndef GMM_KRYLOV_GMRES_H
  65. #define GMM_KRYLOV_GMRES_H
  66. #include "gmm_kernel.h"
  67. #include "gmm_iter.h"
  68. #include "gmm_modified_gram_schmidt.h"
  69. namespace gmm {
  70. /** Generalized Minimum Residual
  71. This solve the unsymmetric linear system Ax = b using restarted GMRES.
  72. See: Y. Saad and M. Schulter. GMRES: A generalized minimum residual
  73. algorithm for solving nonsysmmetric linear systems, SIAM
  74. J. Sci. Statist. Comp. 7(1986), pp, 856-869
  75. */
  76. template <typename Mat, typename Vec, typename VecB, typename Precond,
  77. typename Basis >
  78. void gmres(const Mat &A, Vec &x, const VecB &b, const Precond &M,
  79. int restart, iteration &outer, Basis& KS) {
  80. typedef typename linalg_traits<Vec>::value_type T;
  81. typedef typename number_traits<T>::magnitude_type R;
  82. std::vector<T> w(vect_size(x)), r(vect_size(x)), u(vect_size(x));
  83. std::vector<T> c_rot(restart+1), s_rot(restart+1), s(restart+1);
  84. gmm::dense_matrix<T> H(restart+1, restart);
  85. #ifdef GMM_USES_MPI
  86. double t_ref, t_prec = MPI_Wtime(), t_tot = 0;
  87. static double tmult_tot = 0.0;
  88. t_ref = MPI_Wtime();
  89. cout << "GMRES " << endl;
  90. #endif
  91. mult(M,b,r);
  92. outer.set_rhsnorm(gmm::vect_norm2(r));
  93. if (outer.get_rhsnorm() == 0.0) { clear(x); return; }
  94. mult(A, scaled(x, T(-1)), b, w);
  95. mult(M, w, r);
  96. R beta = gmm::vect_norm2(r), beta_old = beta;
  97. int blocked = 0;
  98. iteration inner = outer;
  99. inner.reduce_noisy();
  100. inner.set_maxiter(restart);
  101. inner.set_name("GMRes inner");
  102. while (! outer.finished(beta)) {
  103. gmm::copy(gmm::scaled(r, R(1)/beta), KS[0]);
  104. gmm::clear(s);
  105. s[0] = beta;
  106. size_type i = 0; inner.init();
  107. do {
  108. mult(A, KS[i], u);
  109. mult(M, u, KS[i+1]);
  110. orthogonalize(KS, mat_col(H, i), i);
  111. R a = gmm::vect_norm2(KS[i+1]);
  112. H(i+1, i) = T(a);
  113. gmm::scale(KS[i+1], T(1) / a);
  114. for (size_type k = 0; k < i; ++k)
  115. Apply_Givens_rotation_left(H(k,i), H(k+1,i), c_rot[k], s_rot[k]);
  116. Givens_rotation(H(i,i), H(i+1,i), c_rot[i], s_rot[i]);
  117. Apply_Givens_rotation_left(H(i,i), H(i+1,i), c_rot[i], s_rot[i]);
  118. Apply_Givens_rotation_left(s[i], s[i+1], c_rot[i], s_rot[i]);
  119. ++inner, ++outer, ++i;
  120. } while (! inner.finished(gmm::abs(s[i])));
  121. upper_tri_solve(H, s, i, false);
  122. combine(KS, s, x, i);
  123. mult(A, gmm::scaled(x, T(-1)), b, w);
  124. mult(M, w, r);
  125. beta_old = std::min(beta, beta_old); beta = gmm::vect_norm2(r);
  126. if (int(inner.get_iteration()) < restart -1 || beta_old <= beta)
  127. ++blocked; else blocked = 0;
  128. if (blocked > 10) {
  129. if (outer.get_noisy()) cout << "Gmres is blocked, exiting\n";
  130. break;
  131. }
  132. #ifdef GMM_USES_MPI
  133. t_tot = MPI_Wtime() - t_ref;
  134. cout << "temps GMRES : " << t_tot << endl;
  135. #endif
  136. }
  137. }
  138. template <typename Mat, typename Vec, typename VecB, typename Precond >
  139. void gmres(const Mat &A, Vec &x, const VecB &b,
  140. const Precond &M, int restart, iteration& outer) {
  141. typedef typename linalg_traits<Vec>::value_type T;
  142. modified_gram_schmidt<T> orth(restart, vect_size(x));
  143. gmres(A, x, b, M, restart, outer, orth);
  144. }
  145. }
  146. #endif