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.

174 lines
7.1 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. // 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. #include "gmm_dense_Householder.h"
  70. namespace gmm {
  71. /** Generalized Minimum Residual
  72. This solve the unsymmetric linear system Ax = b using restarted GMRES.
  73. See: Y. Saad and M. Schulter. GMRES: A generalized minimum residual
  74. algorithm for solving nonsysmmetric linear systems, SIAM
  75. J. Sci. Statist. Comp. 7(1986), pp, 856-869
  76. */
  77. template <typename Mat, typename Vec, typename VecB, typename Precond,
  78. typename Basis >
  79. void gmres(const Mat &A, Vec &x, const VecB &b, const Precond &M,
  80. int restart, iteration &outer, Basis& KS) {
  81. typedef typename linalg_traits<Vec>::value_type T;
  82. typedef typename number_traits<T>::magnitude_type R;
  83. std::vector<T> w(vect_size(x)), r(vect_size(x)), u(vect_size(x));
  84. std::vector<T> c_rot(restart+1), s_rot(restart+1), s(restart+1);
  85. gmm::dense_matrix<T> H(restart+1, restart);
  86. #ifdef GMM_USES_MPI
  87. double t_ref, t_prec = MPI_Wtime(), t_tot = 0;
  88. static double tmult_tot = 0.0;
  89. t_ref = MPI_Wtime();
  90. cout << "GMRES " << endl;
  91. #endif
  92. mult(M,b,r);
  93. outer.set_rhsnorm(gmm::vect_norm2(r));
  94. if (outer.get_rhsnorm() == 0.0) { clear(x); return; }
  95. mult(A, scaled(x, T(-1)), b, w);
  96. mult(M, w, r);
  97. R beta = gmm::vect_norm2(r), beta_old = beta;
  98. int blocked = 0;
  99. iteration inner = outer;
  100. inner.reduce_noisy();
  101. inner.set_maxiter(restart);
  102. inner.set_name("GMRes inner");
  103. while (! outer.finished(beta)) {
  104. gmm::copy(gmm::scaled(r, R(1)/beta), KS[0]);
  105. gmm::clear(s);
  106. s[0] = beta;
  107. size_type i = 0; inner.init();
  108. do {
  109. mult(A, KS[i], u);
  110. mult(M, u, KS[i+1]);
  111. orthogonalize(KS, mat_col(H, i), i);
  112. R a = gmm::vect_norm2(KS[i+1]);
  113. H(i+1, i) = T(a);
  114. gmm::scale(KS[i+1], T(1) / a);
  115. for (size_type k = 0; k < i; ++k)
  116. Apply_Givens_rotation_left(H(k,i), H(k+1,i), c_rot[k], s_rot[k]);
  117. Givens_rotation(H(i,i), H(i+1,i), c_rot[i], s_rot[i]);
  118. Apply_Givens_rotation_left(H(i,i), H(i+1,i), c_rot[i], s_rot[i]);
  119. Apply_Givens_rotation_left(s[i], s[i+1], c_rot[i], s_rot[i]);
  120. ++inner, ++outer, ++i;
  121. } while (! inner.finished(gmm::abs(s[i])));
  122. upper_tri_solve(H, s, i, false);
  123. combine(KS, s, x, i);
  124. mult(A, gmm::scaled(x, T(-1)), b, w);
  125. mult(M, w, r);
  126. beta_old = std::min(beta, beta_old); beta = gmm::vect_norm2(r);
  127. if (int(inner.get_iteration()) < restart -1 || beta_old <= beta)
  128. ++blocked; else blocked = 0;
  129. if (blocked > 10) {
  130. if (outer.get_noisy()) cout << "Gmres is blocked, exiting\n";
  131. break;
  132. }
  133. #ifdef GMM_USES_MPI
  134. t_tot = MPI_Wtime() - t_ref;
  135. cout << "temps GMRES : " << t_tot << endl;
  136. #endif
  137. }
  138. }
  139. template <typename Mat, typename Vec, typename VecB, typename Precond >
  140. void gmres(const Mat &A, Vec &x, const VecB &b,
  141. const Precond &M, int restart, iteration& outer) {
  142. typedef typename linalg_traits<Vec>::value_type T;
  143. modified_gram_schmidt<T> orth(restart, vect_size(x));
  144. gmres(A, x, b, M, restart, outer, orth);
  145. }
  146. }
  147. #endif