NeoPZ
bicg.h
Go to the documentation of this file.
1 
7 /******************************************************************
8  * @ingroup solver
9  * @brief BiCG solves the unsymmetric linear system \f$ Ax = b \f$
10  * using the Preconditioned BiConjugate Gradient method
11  * @return The return value indicates convergence within max_iter (input)
12  * iterations (0), or no convergence within max_iter iterations (1). \n
13  * Upon successful return, output arguments have the following values:
14  * @param A -- matrix of the system
15  * @param b -- vector of the system
16  * @param M -- preconditioner matrix
17  * @param x -- approximate solution to Ax = b
18  * @param max_iter -- the number of iterations performed before the tolerance was reached
19  * @param tol -- the residual after the final iteration
20  *****************************************************************/
25 template < class Matrix, class Vector, class Preconditioner, class Real >
26 int
27 BiCG( Matrix &A, Vector &x, const Vector &b,
28  Preconditioner &M, int64_t &max_iter, Real &tol)
29 {
30  Real resid;
31  Vector rho_1(1), rho_2(1), alpha(1), beta(1);
32  Vector z, ztilde, p, ptilde, q, qtilde;
33 
34  Real normb = TPZExtractVal::val(Norm(b));
35  Vector r = b - A * x;
36  Vector rtilde = r;
37 
38  if (normb == 0.0)
39  normb = 1;
40 
41  if ((resid = (TPZExtractVal::val(Norm(r)) / normb) <= tol)) {
42  tol = resid;
43  max_iter = 0;
44  return 0;
45  }
46 
47  for (int64_t i = 1; i <= max_iter; i++) {
48  M.Solve(r,z);
49  M.Solve(rtilde,ztilde);
50  rho_1(0) = Dot(z, rtilde);
51  if (TPZExtractVal::val(rho_1(0)) == ((Real)0.)) {
52  tol = (TPZExtractVal::val(Norm(r))) / normb;
53  max_iter = i;
54  return 2;
55  }
56  if (i == 1) {
57  p = z;
58  ptilde = ztilde;
59  } else {
60  beta(0) = rho_1(0) / rho_2(0);
61  p.TimesBetaPlusZ(beta(0),z);
62  ptilde.TimesBetaPlusZ(beta(0),ztilde);
63  }
64  q = A * p;
65  A.Multiply(ptilde,qtilde,1);
66  alpha(0) = rho_1(0) / Dot(ptilde, q);
67  x += alpha(0) * p;
68  r -= alpha(0) * q;
69  rtilde -= alpha(0) * qtilde;
70 
71  rho_2(0) = rho_1(0);
72  if ((resid = (TPZExtractVal::val(Norm(r))) / normb) < tol) {
73  tol = resid;
74  max_iter = i;
75  return 0;
76  }
77  }
78 
79  tol = resid;
80  return 1;
81 }
82 
TVar Dot(const TPZFMatrix< TVar > &A, const TPZFMatrix< TVar > &B)
Implement dot product for matrices.
Definition: pzfmatrix.cpp:2083
int BiCG(Matrix &A, Vector &x, const Vector &b, Preconditioner &M, int64_t &max_iter, Real &tol)
Definition: bicg.h:27
static const double tol
Definition: pzgeoprism.cpp:23
TVar Norm(const TPZFMatrix< TVar > &A)
Returns the norm of the matrix A.
static REAL val(const int number)
Definition: pzextractval.h:21
Definition: vectors.h:81