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