QmrSym.cxx
1 // Copyright (C) 2003-2009 Marc DuruflĂ©
2 // Copyright (C) 2001-2009 Vivien Mallet
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_FILE_ITERATIVE_QMRSYM_CXX
22 
23 namespace Seldon
24 {
25 
27 
41 #ifdef SELDON_WITH_VIRTUAL
42  template<class T, class Vector1>
43  int QmrSym(const VirtualMatrix<T>& A, Vector1& x, const Vector1& b,
44  Preconditioner_Base<T>& M,
45  Iteration<typename ClassComplexType<T>::Treal>& iter)
46 #else
47  template <class Titer, class Matrix1, class Vector1, class Preconditioner>
48  int QmrSym(const Matrix1& A, Vector1& x, const Vector1& b,
49  Preconditioner& M, Iteration<Titer> & iter)
50 #endif
51  {
52  const int N = A.GetM();
53  if (N <= 0)
54  return 0;
55 
56  typedef typename Vector1::value_type Complexe;
57  Complexe delta, ep, beta;
58  typedef typename ClassComplexType<Complexe>::Treal Treal;
59  Treal rho, rho_1;
60  Complexe theta_1, gamma_1;
61  Complexe theta, gamma, eta;
62  Complexe zero, one;
63  SetComplexZero(zero);
64  SetComplexOne(one);
65  theta = zero; gamma = one; eta = -one; ep = zero;
66 
67  Vector1 r(b), y(b);
68  Vector1 v(b), p_tld(b);
69  Vector1 p(b), d(b), s(b);
70 
71  // we initialize iter
72  int success_init = iter.Init(b);
73  if (success_init != 0)
74  return iter.ErrorCode();
75 
76  // r = b - Ax
77  Copy(b, r);
78  if (!iter.IsInitGuess_Null())
79  iter.MltAdd(-one, A, x, one, r);
80  else
81  x.Fill(zero);
82 
83  Copy(r, v);
84 
85  M.Solve(A, v, y);
86  rho = Norm2(y);
87 
88  iter.SetNumberIteration(0);
89  // Loop until the stopping criteria are reached
90  while (! iter.Finished(r))
91  {
92 
93  if (rho == Treal(0))
94  {
95  iter.Fail(1, "Qmr breakdown #1");
96  break;
97  }
98 
99  // v = v / rho
100  // y = y / rho
101  Mlt(one/rho, v);
102  Mlt(one/rho, y);
103 
104  delta = DotProd(v, y);
105  if (delta == zero)
106  {
107  iter.Fail(3, "Qmr breakdown #2");
108  break;
109  }
110 
111  if (iter.First())
112  Copy(y, p);
113  else
114  {
115  // p = y - (rho delta / ep) p
116  Mlt(-rho * delta / ep, p);
117  Add(one, y, p);
118  }
119 
120  // product matrix vector p_tld = A*p
121  iter.Mlt(A, p, p_tld);
122 
123  ep = DotProd(p, p_tld);
124  if (ep == zero)
125  {
126  iter.Fail(4, "Qmr breakdown #3");
127  break;
128  }
129 
130  beta = ep / delta;
131  if (beta == zero)
132  {
133  iter.Fail(5, "Qmr breakdown #4");
134  break;
135  }
136 
137  // v = -beta v + p_tld
138  Mlt(-beta, v); Add(one, p_tld, v);
139  M.Solve(A, v, y);
140 
141  rho_1 = rho;
142  rho = Norm2(y);
143 
144  gamma_1 = gamma;
145  theta_1 = theta;
146 
147  theta = rho / (gamma_1 * beta);
148  gamma = one / sqrt(one + theta * theta);
149 
150  if (gamma == Treal(0))
151  {
152  iter.Fail(6, "Qmr breakdown #5");
153  break;
154  }
155 
156  eta = -eta * rho_1 * gamma * gamma / (beta * gamma_1 * gamma_1);
157 
158  if (iter.First())
159  {
160  Copy(p, d);
161  Mlt(eta, d);
162  Copy(p_tld, s);
163  Mlt(eta, s);
164  }
165  else
166  {
167  Complexe tmp = (theta_1 * theta_1 * gamma * gamma);
168  Mlt(tmp, d);
169  Add(eta, p, d);
170  Mlt(tmp, s);
171  Add(eta, p_tld, s);
172  }
173  Add(one, d, x);
174  Add(-one, s, r);
175 
176  ++iter;
177  }
178 
179  return iter.ErrorCode();
180  }
181 
182 } // end namespace
183 
184 #define SELDON_FILE_ITERATIVE_QMRSYM_CXX
185 #endif
Seldon::Iteration::First
bool First() const
Returns true if it is the first iteration.
Definition: Iterative.cxx:236
Seldon::Norm2
ClassComplexType< T >::Treal Norm2(const VectorExpression< T, E > &X)
returns 2-norm of an expression with vectors
Definition: Functions_BaseInline.cxx:153
Seldon::Iteration::Init
int Init(const Vector1 &r)
Initialization with the right hand side.
Definition: Iterative.cxx:218
Seldon::Iteration::Mlt
void Mlt(const Matrix1 &A, const Vector1 &x, Vector1 &y)
Computes y = A x.
Definition: IterativeInline.cxx:123
Seldon::Iteration::ErrorCode
int ErrorCode()
Returns the error code (if an error occured)
Definition: Iterative.cxx:351
Seldon::Iteration::Fail
void Fail(int i, const string &s)
Informs of a failure in the iterative solver.
Definition: Iterative.cxx:330
Seldon::Iteration::MltAdd
void MltAdd(const T1 &alpha, const Matrix1 &A, const Vector1 &x, const T1 &beta, Vector1 &y)
Computes y = beta y + alpha A x.
Definition: IterativeInline.cxx:109
Seldon::Iteration::SetNumberIteration
void SetNumberIteration(int nb)
Changes the number of iterations.
Definition: Iterative.cxx:177
Seldon::QmrSym
int QmrSym(const Matrix1 &A, Vector1 &x, const Vector1 &b, Preconditioner &M, Iteration< Titer > &iter)
Solves linear system using Symmetric Quasi-Minimal Residual (SQMR)
Definition: QmrSym.cxx:48
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Iteration
Class containing parameters for an iterative resolution.
Definition: Iterative.hxx:59
Seldon::Iteration::IsInitGuess_Null
bool IsInitGuess_Null() const
Returns true if the initial guess is null.
Definition: Iterative.cxx:247
Seldon::Iteration::Finished
bool Finished(const Vector1 &r) const
Returns true if the iterative solver has reached its end.
Definition: Iterative.cxx:264