Gcr.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_GCR_CXX
22 
23 namespace Seldon
24 {
25 
27 
45 #ifdef SELDON_WITH_VIRTUAL
46  template<class T, class Vector1>
47  int Gcr(const VirtualMatrix<T>& A, Vector1& x, const Vector1& b,
48  Preconditioner_Base<T>& M,
49  Iteration<typename ClassComplexType<T>::Treal>& outer)
50 #else
51  template <class Titer, class Matrix1, class Vector1, class Preconditioner>
52  int Gcr(const Matrix1& A, Vector1& x, const Vector1& b,
53  Preconditioner& M, Iteration<Titer> & outer)
54 #endif
55  {
56  const int N = A.GetM();
57  if (N <= 0)
58  return 0;
59 
60  typedef typename Vector1::value_type Complexe;
61  int m = outer.GetRestart();
62  // we initialize outer
63  int success_init = outer.Init(b);
64  if (success_init != 0)
65  return outer.ErrorCode();
66 
67  std::vector<Vector1> p(m+1, b), w(m+1, b);
68 
69  Vector<Complexe> beta(m+1);
70 
71  Vector1 r(b), q(b), u(b);
72  Complexe zero, one;
73  SetComplexZero(zero);
74  SetComplexOne(one);
75 
76  for (int i = 0; i < (m+1); i++)
77  {
78  p[i].Fill(zero);
79  w[i].Fill(zero);
80  }
81 
82  // we compute initial residual
83  Copy(b,u);
84  if (!outer.IsInitGuess_Null())
85  outer.MltAdd(-one, A, x, one, u);
86  else
87  x.Fill(zero);
88 
89  M.Solve(A, u, r);
90 
91  Complexe alpha, delta;
92 
93  typedef typename ClassComplexType<Complexe>::Treal Treal;
94  Treal normr = Norm2(r);
95  outer.SetNumberIteration(0);
96  // Loop until the stopping criteria are satisfied
97  while (! outer.Finished(r))
98  {
99  // m is the maximum number of inner iterations
100  Iteration<Treal> inner(outer);
101  inner.SetNumberIteration(outer.GetNumberIteration());
102  inner.SetMaxNumberIteration(outer.GetNumberIteration()+m);
103  Copy(r, p[0]);
104  Mlt(Treal(1)/normr, p[0]);
105 
106  int j = 0;
107 
108  while (! inner.Finished(r) )
109  {
110  // product matrix vector u=A*p(j)
111  outer.Mlt(A, p[j], u);
112 
113  // preconditioning
114  M.Solve(A, u, w[j]);
115 
116  beta(j) = DotProdConj(w[j], w[j]);
117  if (beta(j) == zero)
118  {
119  outer.Fail(1, "Gcr breakdown #1");
120  break;
121  }
122 
123  // new iterate x = x + alpha*p(j) new residual r = r - alpha*w(j)
124  // where alpha = (conj(r_j),A*p_j)/(A*p_j,A*p_j)
125  alpha = DotProdConj(w[j], r) / beta(j);
126  Add(alpha, p[j], x);
127  Add(-alpha, w[j], r);
128 
129  ++inner;
130  ++outer;
131  // product Matrix vector u = A*r
132  outer.Mlt(A, r, u);
133  M.Solve(A, u, q);
134 
135  Copy(r, p[j+1]);
136  // we compute direction p(j+1) = r(j+1) +
137  // \sum_{i=0..j} ( -(A*r_j+1,A*p_i)/(A*p_i,A*p_i) p(i))
138  for (int i = 0; i <= j; i++)
139  {
140  delta = -DotProdConj(w[i], q)/beta(i);
141  Add(delta, p[i], p[j+1]);
142  }
143 
144  ++inner;
145  ++outer;
146  ++j;
147  }
148  normr = Norm2(r);
149  }
150 
151  return outer.ErrorCode();
152  }
153 
154 } // end namespace
155 
156 #define SELDON_FILE_ITERATIVE_GCR_CXX
157 #endif
Seldon::Iteration::GetNumberIteration
int GetNumberIteration() const
Returns the number of iterations.
Definition: Iterative.cxx:134
Seldon::Iteration::GetRestart
int GetRestart() const
Returns the restart parameter.
Definition: Iterative.cxx:110
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::Vector
Definition: SeldonHeader.hxx:207
Seldon::Iteration::SetMaxNumberIteration
void SetMaxNumberIteration(int max_iteration)
Changes the maximum number of iterations.
Definition: Iterative.cxx:169
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
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::Gcr
int Gcr(const Matrix1 &A, Vector1 &x, const Vector1 &b, Preconditioner &M, Iteration< Titer > &outer)
Solves a linear system by using Generalized Conjugate Residual (GCR)
Definition: Gcr.cxx:52
Seldon::Iteration::Finished
bool Finished(const Vector1 &r) const
Returns true if the iterative solver has reached its end.
Definition: Iterative.cxx:264