11 #ifndef SFCSORTBENCH_CG_H 12 #define SFCSORTBENCH_CG_H 14 #include "mathUtils.h" 16 #include "operators.h" 45 template <
typename Real >
46 int CG(
ot::Mesh* mesh, Real *x,Real *b,
int max_iter, Real& tol)
49 Real resid,alpha,beta,rho,rho_1;
62 const unsigned int local_dof=nodeLocalEnd-nodeLocalBegin;
66 Real * p=
new Real[dof];
67 Real * z=
new Real[dof];
68 Real * q=
new Real[dof];
70 Real * Ax=
new Real[dof];
71 Real * Ap=
new Real[dof];
72 Real * r0=
new Real[dof];
73 Real * r1=
new Real[dof];
76 Real normb = normLInfty(b+nodeLocalBegin,local_dof,activeComm);
78 fem::operators::poisson::matvec(mesh,x,Ax);
81 sprintf(fPrefix,
"%s_%d",
"cg",0);
82 const char * varNames[]={
"U"};
83 const double * var[]={Ax};
84 io::vtk::mesh2vtuFine(mesh,fPrefix,0,NULL,NULL,1,varNames,var);
86 for(
unsigned int i=nodeLocalBegin;i<nodeLocalEnd;i++)
96 Real normr=normLInfty(r0+nodeLocalBegin,local_dof,activeComm);
98 if(!activeRank) std::cout<<
"initial residual : "<<(normr/normb)<<std::endl;
100 if ((resid = normr / normb) <= tol) {
118 for(
unsigned int i=1;i<=max_iter;i++)
120 fem::operators::poisson::matvec(mesh,p,Ap,
false);
122 alpha=(dot(r0+nodeLocalBegin,r0+nodeLocalBegin,local_dof,activeComm)/dot(p+nodeLocalBegin,Ap+nodeLocalBegin,local_dof,activeComm));
134 for(
unsigned int e=nodeLocalBegin;e<nodeLocalEnd;e++)
137 r1[e]=r0[e]-alpha*Ap[e];
140 normr=normLInfty(r1+nodeLocalBegin,local_dof,activeComm);
143 if((!activeRank) && (i%10==0)) std::cout<<
" iteration : "<<i<<
" residual : "<<resid<<std::endl;
145 if ((resid = normr / normb) <= tol) {
147 if((!activeRank)) std::cout<<
" iteration : "<<i<<
" residual : "<<resid<<std::endl;
163 beta=(dot(r1+nodeLocalBegin,r1+nodeLocalBegin,local_dof,activeComm)/dot(r0+nodeLocalBegin,r0+nodeLocalBegin,local_dof,activeComm));
170 for(
unsigned int e=nodeLocalBegin;e<nodeLocalEnd;e++)
172 p[e]=r1[e]+beta*p[e];
176 fem::operators::poisson::applyDirichletBoundary(mesh,p,0.0);
177 fem::operators::poisson::applyDirichletBoundary(mesh,r0,0.0);
181 sprintf(fPrefix,
"%s_%d",
"cg",i);
182 const char * varNames[]={
"U"};
183 const double * var[]={x};
184 io::vtk::mesh2vtuFine(mesh,fPrefix,0,NULL,NULL,1,varNames,var);
220 #endif //SFCSORTBENCH_CG_H MPI_Comm getMPIGlobalCommunicator() const
returns the global communicator
Definition: mesh.h:1099
unsigned int getNodeLocalBegin() const
return the location of local node begin
Definition: mesh.h:1043
int Mpi_Bcast(T *buffer, int count, int root, MPI_Comm comm)
This is modified to perform matrix-free iterative methods for solving linear systems that generated f...
Definition: cg.h:42
unsigned int getDegOfFreedom() const
returns the dof for a partition
Definition: mesh.h:1052
unsigned int getMPIRank() const
returns the rank
Definition: mesh.h:1108
bool isActive() const
returns true if mesh is active
Definition: mesh.h:1173
unsigned int getNodeLocalEnd() const
return the location of local node end
Definition: mesh.h:1045
MPI_Comm getMPICommunicator() const
returns the communicator (acitve)
Definition: mesh.h:1096
unsigned int getMPICommSize() const
returns the comm size:
Definition: mesh.h:1111