BiCgcr.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_BICGCR_CXX
22 
23 namespace Seldon
24 {
25 
27 
45 #ifdef SELDON_WITH_VIRTUAL
46  template<class T, class Vector1>
47  int BiCgcr(const VirtualMatrix<T>& A, Vector1& x, const Vector1& b,
48  Preconditioner_Base<T>& M,
49  Iteration<typename ClassComplexType<T>::Treal>& iter)
50 #else
51  template <class Titer, class Matrix1, class Vector1, class Preconditioner>
52  int BiCgcr(const Matrix1& A, Vector1& x, const Vector1& b,
53  Preconditioner& M, Iteration<Titer> & iter)
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  Complexe rho, mu, alpha, beta, tau;
62  Vector1 v(b), w(b), s(b), z(b), p(b), a(b);
63  Complexe zero, one;
64  SetComplexZero(zero);
65  SetComplexOne(one);
66 
67  v.Fill(zero); w.Fill(zero); s.Fill(zero);
68  z.Fill(zero); p.Fill(zero); a.Fill(zero);
69 
70  // we initialize iter
71  int success_init = iter.Init(b);
72  if (success_init != 0)
73  return iter.ErrorCode();
74 
75  // we compute the residual v = b - Ax
76  Copy(b, v);
77  if (!iter.IsInitGuess_Null())
78  iter.MltAdd(-one, A, x, one, v);
79  else
80  x.Fill(zero);
81 
82  iter.SetNumberIteration(0);
83  // s = M*v p = s
84  M.Solve(A, v, s); Copy(s, p);
85  // a = A*p w = M*a
86  iter.Mlt(A, p, a); M.Solve(A, a, w);
87  // we made one product matrix vector
88  ++iter;
89 
90  while (! iter.Finished(v))
91  {
92  rho = DotProd(w, v);
93  mu = DotProd(w, a);
94 
95  // new iterate x = x + alpha*p0 where alpha = (r1,r0)/(p1,p1)
96  if (mu == zero)
97  {
98  iter.Fail(1, "Bicgcr breakdown #1");
99  break;
100  }
101  alpha = rho/mu;
102  Add(alpha, p, x);
103 
104  // new residual r0 = r0 - alpha * p1
105  // r1 = r1 - alpha*p2
106  Add(-alpha, a, v);
107  Add(-alpha, w, s);
108 
109  iter.Mlt(A, s, z);
110  tau = DotProd(w, z);
111 
112  if (tau == zero)
113  {
114  iter.Fail(2, "Bicgcr breakdown #2");
115  break;
116  }
117 
118  beta = tau/mu;
119 
120  Mlt(-beta, p);
121  Add(one, s, p);
122  Mlt(-beta, a);
123  Add(one, z, a);
124 
125  M.Solve(A, a, w);
126 
127  ++iter;
128  }
129 
130  return iter.ErrorCode();
131  }
132 
133 } // end namespace
134 
135 #define SELDON_FILE_ITERATIVE_BICGCR_CXX
136 #endif
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::BiCgcr
int BiCgcr(const Matrix1 &A, Vector1 &x, const Vector1 &b, Preconditioner &M, Iteration< Titer > &iter)
Solves a linear system by using BiCgCr.
Definition: BiCgcr.cxx:52
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::Iteration::Finished
bool Finished(const Vector1 &r) const
Returns true if the iterative solver has reached its end.
Definition: Iterative.cxx:264