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.

210 lines
7.9 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2015 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 qmr.h from ITL.
  27. // See http://osl.iu.edu/research/itl/
  28. // Following the corresponding Copyright notice.
  29. //===========================================================================
  30. //
  31. // Copyright (c) 1997-2001, The Trustees of Indiana University.
  32. // All rights reserved.
  33. // Redistribution and use in source and binary forms, with or without
  34. // modification, are permitted provided that the following conditions are met:
  35. //
  36. // * Redistributions of source code must retain the above copyright
  37. // notice, this list of conditions and the following disclaimer.
  38. // * Redistributions in binary form must reproduce the above copyright
  39. // notice, this list of conditions and the following disclaimer in the
  40. // documentation and/or other materials provided with the distribution.
  41. // * Neither the name of the University of Notre Dame nor the
  42. // names of its contributors may be used to endorse or promote products
  43. // derived from this software without specific prior written permission.
  44. //
  45. // THIS SOFTWARE IS PROVIDED BY THE TRUSTEES OF INDIANA UNIVERSITY AND
  46. // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  47. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  48. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES
  49. // OF INDIANA UNIVERSITY AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  50. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  51. // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  52. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  53. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  54. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  55. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56. //
  57. //===========================================================================
  58. /**@file gmm_solver_qmr.h
  59. @author Andrew Lumsdaine <lums@osl.iu.edu>
  60. @author Lie-Quan Lee <llee@osl.iu.edu>
  61. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  62. @date October 13, 2002.
  63. @brief Quasi-Minimal Residual iterative solver.
  64. */
  65. #ifndef GMM_QMR_H
  66. #define GMM_QMR_H
  67. #include "gmm_kernel.h"
  68. #include "gmm_iter.h"
  69. namespace gmm {
  70. /** Quasi-Minimal Residual.
  71. This routine solves the unsymmetric linear system Ax = b using
  72. the Quasi-Minimal Residual method.
  73. See: R. W. Freund and N. M. Nachtigal, A quasi-minimal residual
  74. method for non-Hermitian linear systems, Numerical Math.,
  75. 60(1991), pp. 315-339
  76. Preconditioner - Incomplete LU, Incomplete LU with threshold,
  77. SSOR or identity_preconditioner.
  78. */
  79. template <typename Matrix, typename Vector, typename VectorB,
  80. typename Precond1>
  81. void qmr(const Matrix &A, Vector &x, const VectorB &b, const Precond1 &M1,
  82. iteration& iter) {
  83. typedef typename linalg_traits<Vector>::value_type T;
  84. typedef typename number_traits<T>::magnitude_type R;
  85. T delta(0), ep(0), beta(0), theta_1(0), gamma_1(0);
  86. T theta(0), gamma(1), eta(-1);
  87. R rho_1(0), rho, xi;
  88. typedef typename temporary_vector<Vector>::vector_type TmpVec;
  89. size_type nn = vect_size(x);
  90. TmpVec r(nn), v_tld(nn), y(nn), w_tld(nn), z(nn), v(nn), w(nn);
  91. TmpVec y_tld(nn), z_tld(nn), p(nn), q(nn), p_tld(nn), d(nn), s(nn);
  92. iter.set_rhsnorm(double(gmm::vect_norm2(b)));
  93. if (iter.get_rhsnorm() == 0.0) { clear(x); return; }
  94. gmm::mult(A, gmm::scaled(x, T(-1)), b, r);
  95. gmm::copy(r, v_tld);
  96. gmm::left_mult(M1, v_tld, y);
  97. rho = gmm::vect_norm2(y);
  98. gmm::copy(r, w_tld);
  99. gmm::transposed_right_mult(M1, w_tld, z);
  100. xi = gmm::vect_norm2(z);
  101. while (! iter.finished_vect(r)) {
  102. if (rho == R(0) || xi == R(0)) {
  103. if (iter.get_maxiter() == size_type(-1))
  104. { GMM_ASSERT1(false, "QMR failed to converge"); }
  105. else { GMM_WARNING1("QMR failed to converge"); return; }
  106. }
  107. gmm::copy(gmm::scaled(v_tld, T(R(1)/rho)), v);
  108. gmm::scale(y, T(R(1)/rho));
  109. gmm::copy(gmm::scaled(w_tld, T(R(1)/xi)), w);
  110. gmm::scale(z, T(R(1)/xi));
  111. delta = gmm::vect_sp(z, y);
  112. if (delta == T(0)) {
  113. if (iter.get_maxiter() == size_type(-1))
  114. { GMM_ASSERT1(false, "QMR failed to converge"); }
  115. else { GMM_WARNING1("QMR failed to converge"); return; }
  116. }
  117. gmm::right_mult(M1, y, y_tld);
  118. gmm::transposed_left_mult(M1, z, z_tld);
  119. if (iter.first()) {
  120. gmm::copy(y_tld, p);
  121. gmm::copy(z_tld, q);
  122. } else {
  123. gmm::add(y_tld, gmm::scaled(p, -(T(xi * delta) / ep)), p);
  124. gmm::add(z_tld, gmm::scaled(q, -(T(rho * delta) / ep)), q);
  125. }
  126. gmm::mult(A, p, p_tld);
  127. ep = gmm::vect_sp(q, p_tld);
  128. if (ep == T(0)) {
  129. if (iter.get_maxiter() == size_type(-1))
  130. { GMM_ASSERT1(false, "QMR failed to converge"); }
  131. else { GMM_WARNING1("QMR failed to converge"); return; }
  132. }
  133. beta = ep / delta;
  134. if (beta == T(0)) {
  135. if (iter.get_maxiter() == size_type(-1))
  136. { GMM_ASSERT1(false, "QMR failed to converge"); }
  137. else { GMM_WARNING1("QMR failed to converge"); return; }
  138. }
  139. gmm::add(p_tld, gmm::scaled(v, -beta), v_tld);
  140. gmm::left_mult(M1, v_tld, y);
  141. rho_1 = rho;
  142. rho = gmm::vect_norm2(y);
  143. gmm::mult(gmm::transposed(A), q, w_tld);
  144. gmm::add(w_tld, gmm::scaled(w, -beta), w_tld);
  145. gmm::transposed_right_mult(M1, w_tld, z);
  146. xi = gmm::vect_norm2(z);
  147. gamma_1 = gamma;
  148. theta_1 = theta;
  149. theta = rho / (gamma_1 * beta);
  150. gamma = T(1) / gmm::sqrt(T(1) + gmm::sqr(theta));
  151. if (gamma == T(0)) {
  152. if (iter.get_maxiter() == size_type(-1))
  153. { GMM_ASSERT1(false, "QMR failed to converge"); }
  154. else { GMM_WARNING1("QMR failed to converge"); return; }
  155. }
  156. eta = -eta * T(rho_1) * gmm::sqr(gamma) / (beta * gmm::sqr(gamma_1));
  157. if (iter.first()) {
  158. gmm::copy(gmm::scaled(p, eta), d);
  159. gmm::copy(gmm::scaled(p_tld, eta), s);
  160. } else {
  161. T tmp = gmm::sqr(theta_1 * gamma);
  162. gmm::add(gmm::scaled(p, eta), gmm::scaled(d, tmp), d);
  163. gmm::add(gmm::scaled(p_tld, eta), gmm::scaled(s, tmp), s);
  164. }
  165. gmm::add(d, x);
  166. gmm::add(gmm::scaled(s, T(-1)), r);
  167. ++iter;
  168. }
  169. }
  170. }
  171. #endif