TBCI Numerical high perf. C++ Library 2.8.0
cg.h
Go to the documentation of this file.
1
4
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
16INST3(template <typename T, Matrix<T>, Vector<T> > class NN friend \
17int CG (const Matrix<T>&, Vector<T>&, const Vector<T>&,\
18 const Preconditioner_Sig<T, Matrix<T> >&, unsigned int&, double&);)
19INST3(template <typename T, BdMatrix<T>, Vector<T> > class NN friend \
20int CG (const BdMatrix<T>&, Vector<T>&, const Vector<T>&,\
21 const Preconditioner_Sig<T, BdMatrix<T> >&, unsigned int&, double&);)
22
23
44template < typename T, typename SysMatrix, typename SysVector >
45int 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
100INST3(template <typename T, Matrix<T>, Vector<T> > class NN friend \
101int CG2 (const Matrix<T>&, Vector<T>&, const Vector<T>&,\
102 const Preconditioner_Sig<T, Matrix<T> >&, unsigned int&, double&);)
103INST3(template <typename T, BdMatrix<T>, Vector<T> > class NN friend \
104int CG2 (const BdMatrix<T>&, Vector<T>&, const Vector<T>&,\
105 const Preconditioner_Sig<T, BdMatrix<T> >&, unsigned int&, double&);)
106
111template < typename T, typename SysMatrix, typename SysVector>
112int 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 > const Vector< T > & p
Definition LM_fit.h:98
const Vector< T > const Vector< T > & x
Definition LM_fit.h:97
int i
Definition LM_fit.h:71
#define INST3(x, y, z)
Definition basics.h:240
#define NAMESPACE_END
Definition basics.h:323
#define NAMESPACE_TBCI
Definition basics.h:317
#define T
Definition bdmatlib.cc:20
SysMatrix
Definition bicgstab.h:90
Vector< T > class NN friend int CG2(const BdMatrix< T > &, Vector< T > &, const Vector< T > &, const Preconditioner_Sig< T, BdMatrix< T > > &, unsigned int &, double &)
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 &)
The class BdMatrix is an implementation to store and do operations on sparse Matrices with a band str...
Abstract base class for all Preconditioners.
Definition precond.h:41
virtual TVector< T > solve(const Vector< T > &) const =0
cplx< T > dot(const cplx< T > &a, const cplx< T > &b)
Definition cplx.h:300
F_TMatrix< T > b
Definition f_matrix.h:736