NeoPZ
cg.h
Go to the documentation of this file.
1 
25 #ifdef TEST
26 #include <list>
27 #endif
28 
29 template < class Matrix, class Vector, class Preconditioner, class Real >
30 int
31 CG( Matrix &A, Vector &x, const Vector &b,
32  Preconditioner &M, Vector *residual, int64_t &max_iter, Real &tol,const int FromCurrent)
33 {
34  Real resid;
35  Vector p, z, q;
36  REAL alpha, beta, rho, rho_1 = 0;
37 
38  REAL normb = TPZExtractVal::val(Norm(b));
39  Vector resbackup;
40  Vector *res = residual;
41 
42 #ifdef TEST
43  std::list< TPZFMatrix<REAL> > plist,qlist;
44  std::list< TPZFMatrix<REAL> >::iterator jt;
45  std::list< TPZFMatrix<REAL> >::iterator kt;
46  Vector Au;
47 #endif
48 
49  if(!res) res = &resbackup;
50  Vector &r = *res;
51  // Vector r = b - A*x;
52  if(FromCurrent)
53  {
54  A.MultAdd(x,b,r,-1.,1.);
55  }
56  else {
57  x.Zero();
58  r = b;
59  }
60 
61  if (normb == 0.0)
62  normb = 1.0;
63 
64  if ((resid = (TPZExtractVal::val( Norm(r) ) ) / normb) <= tol) {
65  tol = resid;
66  max_iter = 0;
67  return 0;
68  }
69  int64_t i;
70  for (i = 1; i <= max_iter; i++) {
71  M.Solve(r,z);
72  rho = TPZExtractVal::val(Dot(r, z));
73 
74  if (i == 1)
75  p = z;
76  else {
77  beta = rho / rho_1;
78  p.TimesBetaPlusZ(beta,z);
79  }
80 #ifdef TEST
81 
82  plist.push_back(p);
83 #endif
84 
85  A.Multiply(p,q);
86  alpha = rho / (TPZExtractVal::val(Dot(p, q)));
87 
88 #ifdef TEST
89  qlist.push_back(q);
90 #endif
91 
92  x.ZAXPY(alpha,p);
93  r.ZAXPY(-alpha,q);
94 
95 #ifdef TEST
96  A.Multiply(x,Au);
97  REAL energy = Dot(x,Au)/2.-Dot(x,b);
98 #endif
99 
100  if ((resid = (TPZExtractVal::val(Norm(r))) / normb) <= tol) {
101  tol = resid;
102  max_iter = i;
103 #ifdef PZDEBUG
104  std::cout << "cg iter = " << i << " res = " << resid << std::endl;
105 #endif
106  return 0;
107  }
108 #ifdef PZDEBUG
109  std::cout << "cg iter = " << i << " res = " << resid /*<< " energy " << energy */ << std::endl;
110 #endif
111 #ifdef TEST
112  std::cout << " energy " << energy << std::endl;
113  TPZFMatrix<REAL> inner(plist.size(),plist.size(),0.);
114  {
115  int64_t j,k;
116  for(j=0, jt = plist.begin(); jt != plist.end(); jt++,j++)
117  {
118  for(k=0, kt = qlist.begin(); kt != qlist.end(); kt++,k++)
119  {
120  inner(j,k) = Dot((*jt),(*kt));
121  }
122  }
123  }
124  inner.Print("Inner product of search directions");
125 #endif
126  rho_1 = rho;
127  }
128 
129  tol = resid;
130  std::cout << "cg iter = " << i << " res = " << resid << std::endl;
131  return 1;
132 }
TVar Dot(const TPZFMatrix< TVar > &A, const TPZFMatrix< TVar > &B)
Implement dot product for matrices.
Definition: pzfmatrix.cpp:2083
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
int CG(Matrix &A, Vector &x, const Vector &b, Preconditioner &M, Vector *residual, int64_t &max_iter, Real &tol, const int FromCurrent)
CG solves the symmetric positive definite linear system using the Conjugate Gradient method...
Definition: cg.h:31
Definition: vectors.h:81
virtual void Print(std::ostream &out) const
Definition: pzmatrix.h:253