TBCI Numerical high perf. C++ Library  2.8.0
cg.h
Go to the documentation of this file.
1 
5 /* $Id: cg.h,v 1.10.2.3 2019/05/28 11:13:02 garloff Exp $ */
6 
7 #ifndef TBCI_SOLVER_CG_H
8 #define TBCI_SOLVER_CG_H
9 
10 #include "tbci/basics.h"
11 #include "tbci/solver/precond.h"
12 
14 
15 
16 INST3(template <typename T, Matrix<T>, Vector<T> > class NN friend \
17 int CG (const Matrix<T>&, Vector<T>&, const Vector<T>&,\
18  const Preconditioner_Sig<T, Matrix<T> >&, unsigned int&, double&);)
19 INST3(template <typename T, BdMatrix<T>, Vector<T> > class NN friend \
20 int CG (const BdMatrix<T>&, Vector<T>&, const Vector<T>&,\
21  const Preconditioner_Sig<T, BdMatrix<T> >&, unsigned int&, double&);)
22 
23 
44 template < typename T, typename SysMatrix, typename SysVector >
45 int CG (const SysMatrix &A, SysVector &x, const SysVector &b,
46  const Preconditioner_Sig<T, SysMatrix> &M, unsigned int &max_iter, double &tol)
47 {
48  double resid;
49  unsigned int dim = A.rows();
50  SysVector p(dim), z(dim), q(dim);
51  typename SysVector::value_type alpha, beta, rho, rho_1(0);
52 
53  double normb = b.fabs();
54  if (normb == 0.0) normb = 1.0; else normb = 1.0 / normb;
55 
56  SysVector r(b - A*x);
57 
58 
59  if ((resid = r.fabs() * normb) <= tol)
60  {
61  tol = resid;
62  max_iter = 0;
63  return 0;
64  }
65 
66 //Aenderung Marco:
67  for (unsigned int i = 1; i <= max_iter; i++)
68  {
69  z = M.solve(r);
70  rho = dot(r,z);
71 
72  if (i == 1)
73  p = z;
74  else {
75  beta = rho/ rho_1;
76  p = z + beta * p;
77  }
78 
79  q = A*p;
80  alpha = rho/ dot(p,q);
81 
82  x += alpha * p;
83  r -= alpha * q;
84 
85  if ((resid = r.fabs() * normb) <= tol)
86  {
87  tol = resid;
88  max_iter = i;
89  return 0;
90  }
91 
92  rho_1 = rho;
93  }
94 
95  tol = resid;
96  return 1;
97 }
98 
99 
100 INST3(template <typename T, Matrix<T>, Vector<T> > class NN friend \
101 int CG2 (const Matrix<T>&, Vector<T>&, const Vector<T>&,\
102  const Preconditioner_Sig<T, Matrix<T> >&, unsigned int&, double&);)
103 INST3(template <typename T, BdMatrix<T>, Vector<T> > class NN friend \
104 int CG2 (const BdMatrix<T>&, Vector<T>&, const Vector<T>&,\
105  const Preconditioner_Sig<T, BdMatrix<T> >&, unsigned int&, double&);)
106 
111 template < typename T, typename SysMatrix, typename SysVector>
112 int CG2(const SysMatrix &A, SysVector &x, const SysVector &b,
113  const Preconditioner_Sig<T, SysMatrix> &M, unsigned int &max_iter, double &tol)
114 {
115  double resid;
116  unsigned int dim = A.rows();
117  SysVector p(dim), z(dim), q(dim);
118  typename SysVector::value_type alpha, beta, rho, rho_1(0);
119 
120  double normb = b.fabs();
121  if (normb == 0.0) normb = 1; else normb = 1 / normb;
122 
123  SysVector r(b - A*x);
124 
125 
126  if ((resid = r.fabs() * normb) <= tol)
127  {
128  tol = resid;
129  max_iter = 0;
130  return 0;
131  }
132 
133 //Aenderung Marco:
134  for (unsigned int i = 1; i <= max_iter; i++)
135  {
136  z = M.solve(r);
137  rho = (r*z);
138 
139  if (i == 1)
140  p = z;
141  else {
142  beta = rho/ rho_1;
143  p = z + beta * p;
144  }
145 
146  q = A*p;
147  alpha = rho/ (p*q);
148 
149  x += alpha * p;
150  r -= alpha * q;
151 
152  if ((resid = r.fabs() * normb) <= tol)
153  {
154  tol = resid;
155  max_iter = i;
156  return 0;
157  }
158 
159  rho_1 = rho;
160  }
161 
162  tol = resid;
163  return 1;
164 }
165 
167 
168 #endif /* TBCI_SOLVER_CG_H */
const Vector< T > const Vector< T > & x
Definition: LM_fit.h:97
Abstract base class for all Preconditioners.
Definition: precond.h:40
const Vector< T > const Vector< T > const Vector< T > & p
Definition: LM_fit.h:97
The class BdMatrix is an implementation to store and do operations on sparse Matrices with a band str...
Definition: band_matrix.h:103
#define NAMESPACE_TBCI
Definition: basics.h:317
unsigned long dim
Definition: bvector.h:74
NAMESPACE_TBCI Vector< T > class NN friend int CG(const BdMatrix< T > &, Vector< T > &, const Vector< T > &, const Preconditioner_Sig< T, BdMatrix< T > > &, unsigned int &, double &)
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > & z
Definition: LM_fit.h:172
F_TMatrix< T > b
Definition: f_matrix.h:736
#define INST3(x, y, z)
Definition: basics.h:240
Definition: bvector.h:49
T dot(const T &a1, const T &a2)
Definition: basics.h:1183
int i
Definition: LM_fit.h:71
SysMatrix
Definition: bicgstab.h:90
virtual TVector< T > solve(const Vector< T > &) const =0
Vector< T > class NN friend int CG2(const BdMatrix< T > &, Vector< T > &, const Vector< T > &, const Preconditioner_Sig< T, BdMatrix< T > > &, unsigned int &, double &)
#define NAMESPACE_END
Definition: basics.h:323
Definition: bvector.h:54
#define T
Definition: bdmatlib.cc:20