NeoPZ
bicgstab.h
Go to the documentation of this file.
1 
25 template < class Matrix, class Vector, class Preconditioner, class Real >
26 int
27 BiCGSTAB(const Matrix &A, Vector &x, const Vector &b,
28  /*const */Preconditioner &M, int64_t &max_iter, Real &tol, Vector *residual,const int FromCurrent)
29 {
30  Real resid;
31  Vector rho_1(1), rho_2(1), alpha(1), beta(1), omega(1);
32  Vector p, phat, s, shat, t, v;
33 
34  Vector resbackup;
35  Vector *res = residual;
36  if(!res) res = &resbackup;
37  Vector &r = *res;
38 
39  Real normb = TPZExtractVal::val(Norm(b));
40  if(FromCurrent) A.MultAdd(x,b,r,-1.,1.);
41  else {
42  x.Zero();
43  r = b;
44  }
45 
46  Vector rtilde = r;
47 
48  if (normb == 0.0)
49  normb = 1;
50 
51  if ((resid = TPZExtractVal::val(Norm(r))) / normb < tol) {
52  tol = resid;
53  max_iter = 0;
54  return 0;
55  }
56 
57  for (int64_t i = 1; i <= max_iter; i++) {
58  rho_1(0) = Dot(rtilde, r);
59  if (TPZExtractVal::val(rho_1(0)) == ((Real)0.)) {
60  tol = TPZExtractVal::val(Norm(r)) / normb;
61  return 2;
62  }
63  if (i == 1)
64  p = r;
65  else {
66  beta(0) = (rho_1(0)/rho_2(0)) * (alpha(0)/omega(0));
67  p = r + beta(0) * (p - omega(0) * v);
68  }
69 
70  M.Solve(p, phat);
71  A.Multiply(phat, v);
72  alpha(0) = rho_1(0) / Dot(rtilde, v);
73  s = r - alpha(0) * v;
74  if ((resid = TPZExtractVal::val(Norm(r))) / normb < tol) {
75  x += alpha(0) * phat;
76  tol = resid;
77  return 0;
78  }
79  M.Solve(s, shat);
80  A.Multiply(shat, t);
81  omega = Dot(t,s) / Dot(t,t);
82  x += alpha(0) * phat + omega(0) * shat;
83  r = s - omega(0) * t;
84 
85  rho_2(0) = rho_1(0);
86  if ((resid = TPZExtractVal::val(Norm(r))) / normb < tol) {
87  tol = resid;
88  max_iter = i;
89  return 0;
90  }
91  if (TPZExtractVal::val(omega(0)) == ((Real)0.)) {
92  tol = TPZExtractVal::val(Norm(r)) / normb;
93  return 3;
94  }
95  }
96 
97  tol = resid;
98  return 1;
99 }
TVar Dot(const TPZFMatrix< TVar > &A, const TPZFMatrix< TVar > &B)
Implement dot product for matrices.
Definition: pzfmatrix.cpp:2083
int BiCGSTAB(const Matrix &A, Vector &x, const Vector &b, Preconditioner &M, int64_t &max_iter, Real &tol, Vector *residual, const int FromCurrent)
BiCGSTAB solves the unsymmetric linear system using the Preconditioned BiConjugate Gradient Stabiliz...
Definition: bicgstab.h:27
static const double tol
Definition: pzgeoprism.cpp:23
TVar Norm(const TPZFMatrix< TVar > &A)
Returns the norm of the matrix A.
string res
Definition: test.py:151
static REAL val(const int number)
Definition: pzextractval.h:21
Definition: vectors.h:81