mirror of
https://github.com/paboyle/Grid.git
synced 2025-04-10 06:00:45 +01:00
Namespace and indentation
This commit is contained in:
parent
ae9175735a
commit
23ef0e3e19
@ -1,4 +1,4 @@
|
|||||||
/*************************************************************************************
|
/*************************************************************************************
|
||||||
|
|
||||||
Grid physics library, www.github.com/paboyle/Grid
|
Grid physics library, www.github.com/paboyle/Grid
|
||||||
|
|
||||||
@ -24,8 +24,8 @@ Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|||||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
See the full license in the file "LICENSE" in the top level distribution directory
|
See the full license in the file "LICENSE" in the top level distribution directory
|
||||||
*************************************************************************************/
|
*************************************************************************************/
|
||||||
/* END LEGAL */
|
/* END LEGAL */
|
||||||
#ifndef GRID_PREC_GCR_H
|
#ifndef GRID_PREC_GCR_H
|
||||||
#define GRID_PREC_GCR_H
|
#define GRID_PREC_GCR_H
|
||||||
|
|
||||||
@ -36,195 +36,195 @@ Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|||||||
//NB. Likely not original reference since they are focussing on a preconditioner variant.
|
//NB. Likely not original reference since they are focussing on a preconditioner variant.
|
||||||
// but VPGCR was nicely written up in their paper
|
// but VPGCR was nicely written up in their paper
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
namespace Grid {
|
NAMESPACE_BEGIN(Grid);
|
||||||
|
|
||||||
template<class Field>
|
template<class Field>
|
||||||
class PrecGeneralisedConjugateResidual : public OperatorFunction<Field> {
|
class PrecGeneralisedConjugateResidual : public OperatorFunction<Field> {
|
||||||
public:
|
public:
|
||||||
RealD Tolerance;
|
RealD Tolerance;
|
||||||
Integer MaxIterations;
|
Integer MaxIterations;
|
||||||
int verbose;
|
int verbose;
|
||||||
int mmax;
|
int mmax;
|
||||||
int nstep;
|
int nstep;
|
||||||
int steps;
|
int steps;
|
||||||
GridStopWatch PrecTimer;
|
GridStopWatch PrecTimer;
|
||||||
GridStopWatch MatTimer;
|
GridStopWatch MatTimer;
|
||||||
GridStopWatch LinalgTimer;
|
GridStopWatch LinalgTimer;
|
||||||
|
|
||||||
LinearFunction<Field> &Preconditioner;
|
LinearFunction<Field> &Preconditioner;
|
||||||
|
|
||||||
PrecGeneralisedConjugateResidual(RealD tol,Integer maxit,LinearFunction<Field> &Prec,int _mmax,int _nstep) :
|
PrecGeneralisedConjugateResidual(RealD tol,Integer maxit,LinearFunction<Field> &Prec,int _mmax,int _nstep) :
|
||||||
Tolerance(tol),
|
Tolerance(tol),
|
||||||
MaxIterations(maxit),
|
MaxIterations(maxit),
|
||||||
Preconditioner(Prec),
|
Preconditioner(Prec),
|
||||||
mmax(_mmax),
|
mmax(_mmax),
|
||||||
nstep(_nstep)
|
nstep(_nstep)
|
||||||
{
|
{
|
||||||
verbose=1;
|
verbose=1;
|
||||||
};
|
};
|
||||||
|
|
||||||
void operator() (LinearOperatorBase<Field> &Linop,const Field &src, Field &psi){
|
void operator() (LinearOperatorBase<Field> &Linop,const Field &src, Field &psi){
|
||||||
|
|
||||||
psi=zero;
|
psi=zero;
|
||||||
RealD cp, ssq,rsq;
|
RealD cp, ssq,rsq;
|
||||||
ssq=norm2(src);
|
ssq=norm2(src);
|
||||||
rsq=Tolerance*Tolerance*ssq;
|
rsq=Tolerance*Tolerance*ssq;
|
||||||
|
|
||||||
Field r(src._grid);
|
Field r(src._grid);
|
||||||
|
|
||||||
PrecTimer.Reset();
|
PrecTimer.Reset();
|
||||||
MatTimer.Reset();
|
MatTimer.Reset();
|
||||||
LinalgTimer.Reset();
|
LinalgTimer.Reset();
|
||||||
|
|
||||||
GridStopWatch SolverTimer;
|
GridStopWatch SolverTimer;
|
||||||
SolverTimer.Start();
|
SolverTimer.Start();
|
||||||
|
|
||||||
steps=0;
|
steps=0;
|
||||||
for(int k=0;k<MaxIterations;k++){
|
for(int k=0;k<MaxIterations;k++){
|
||||||
|
|
||||||
cp=GCRnStep(Linop,src,psi,rsq);
|
cp=GCRnStep(Linop,src,psi,rsq);
|
||||||
|
|
||||||
std::cout<<GridLogMessage<<"VPGCR("<<mmax<<","<<nstep<<") "<< steps <<" steps cp = "<<cp<<std::endl;
|
std::cout<<GridLogMessage<<"VPGCR("<<mmax<<","<<nstep<<") "<< steps <<" steps cp = "<<cp<<std::endl;
|
||||||
|
|
||||||
if(cp<rsq) {
|
if(cp<rsq) {
|
||||||
|
|
||||||
SolverTimer.Stop();
|
SolverTimer.Stop();
|
||||||
|
|
||||||
Linop.HermOp(psi,r);
|
Linop.HermOp(psi,r);
|
||||||
axpy(r,-1.0,src,r);
|
axpy(r,-1.0,src,r);
|
||||||
RealD tr = norm2(r);
|
RealD tr = norm2(r);
|
||||||
std::cout<<GridLogMessage<<"PrecGeneralisedConjugateResidual: Converged on iteration " <<steps
|
std::cout<<GridLogMessage<<"PrecGeneralisedConjugateResidual: Converged on iteration " <<steps
|
||||||
<< " computed residual "<<sqrt(cp/ssq)
|
<< " computed residual "<<sqrt(cp/ssq)
|
||||||
<< " true residual " <<sqrt(tr/ssq)
|
<< " true residual " <<sqrt(tr/ssq)
|
||||||
<< " target " <<Tolerance <<std::endl;
|
<< " target " <<Tolerance <<std::endl;
|
||||||
|
|
||||||
std::cout<<GridLogMessage<<"VPGCR Time elapsed: Total "<< SolverTimer.Elapsed() <<std::endl;
|
|
||||||
std::cout<<GridLogMessage<<"VPGCR Time elapsed: Precon "<< PrecTimer.Elapsed() <<std::endl;
|
|
||||||
std::cout<<GridLogMessage<<"VPGCR Time elapsed: Matrix "<< MatTimer.Elapsed() <<std::endl;
|
|
||||||
std::cout<<GridLogMessage<<"VPGCR Time elapsed: Linalg "<< LinalgTimer.Elapsed() <<std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
std::cout<<GridLogMessage<<"VPGCR Time elapsed: Total "<< SolverTimer.Elapsed() <<std::endl;
|
||||||
|
std::cout<<GridLogMessage<<"VPGCR Time elapsed: Precon "<< PrecTimer.Elapsed() <<std::endl;
|
||||||
|
std::cout<<GridLogMessage<<"VPGCR Time elapsed: Matrix "<< MatTimer.Elapsed() <<std::endl;
|
||||||
|
std::cout<<GridLogMessage<<"VPGCR Time elapsed: Linalg "<< LinalgTimer.Elapsed() <<std::endl;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
std::cout<<GridLogMessage<<"Variable Preconditioned GCR did not converge"<<std::endl;
|
|
||||||
assert(0);
|
|
||||||
}
|
}
|
||||||
|
std::cout<<GridLogMessage<<"Variable Preconditioned GCR did not converge"<<std::endl;
|
||||||
|
assert(0);
|
||||||
|
}
|
||||||
|
|
||||||
RealD GCRnStep(LinearOperatorBase<Field> &Linop,const Field &src, Field &psi,RealD rsq){
|
RealD GCRnStep(LinearOperatorBase<Field> &Linop,const Field &src, Field &psi,RealD rsq){
|
||||||
|
|
||||||
RealD cp;
|
RealD cp;
|
||||||
RealD a, b, c, d;
|
RealD a, b, c, d;
|
||||||
RealD zAz, zAAz;
|
RealD zAz, zAAz;
|
||||||
RealD rAq, rq;
|
RealD rAq, rq;
|
||||||
|
|
||||||
GridBase *grid = src._grid;
|
GridBase *grid = src._grid;
|
||||||
|
|
||||||
Field r(grid);
|
Field r(grid);
|
||||||
Field z(grid);
|
Field z(grid);
|
||||||
Field tmp(grid);
|
Field tmp(grid);
|
||||||
Field ttmp(grid);
|
Field ttmp(grid);
|
||||||
Field Az(grid);
|
Field Az(grid);
|
||||||
|
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
// history for flexible orthog
|
// history for flexible orthog
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
std::vector<Field> q(mmax,grid);
|
std::vector<Field> q(mmax,grid);
|
||||||
std::vector<Field> p(mmax,grid);
|
std::vector<Field> p(mmax,grid);
|
||||||
std::vector<RealD> qq(mmax);
|
std::vector<RealD> qq(mmax);
|
||||||
|
|
||||||
//////////////////////////////////
|
//////////////////////////////////
|
||||||
// initial guess x0 is taken as nonzero.
|
// initial guess x0 is taken as nonzero.
|
||||||
// r0=src-A x0 = src
|
// r0=src-A x0 = src
|
||||||
//////////////////////////////////
|
//////////////////////////////////
|
||||||
MatTimer.Start();
|
MatTimer.Start();
|
||||||
Linop.HermOpAndNorm(psi,Az,zAz,zAAz);
|
Linop.HermOpAndNorm(psi,Az,zAz,zAAz);
|
||||||
MatTimer.Stop();
|
MatTimer.Stop();
|
||||||
r=src-Az;
|
r=src-Az;
|
||||||
|
|
||||||
/////////////////////
|
/////////////////////
|
||||||
// p = Prec(r)
|
// p = Prec(r)
|
||||||
/////////////////////
|
/////////////////////
|
||||||
PrecTimer.Start();
|
PrecTimer.Start();
|
||||||
Preconditioner(r,z);
|
Preconditioner(r,z);
|
||||||
PrecTimer.Stop();
|
PrecTimer.Stop();
|
||||||
|
|
||||||
MatTimer.Start();
|
MatTimer.Start();
|
||||||
Linop.HermOp(z,tmp);
|
Linop.HermOp(z,tmp);
|
||||||
MatTimer.Stop();
|
MatTimer.Stop();
|
||||||
|
|
||||||
ttmp=tmp;
|
ttmp=tmp;
|
||||||
tmp=tmp-r;
|
tmp=tmp-r;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
std::cout<<GridLogMessage<<r<<std::endl;
|
std::cout<<GridLogMessage<<r<<std::endl;
|
||||||
std::cout<<GridLogMessage<<z<<std::endl;
|
std::cout<<GridLogMessage<<z<<std::endl;
|
||||||
std::cout<<GridLogMessage<<ttmp<<std::endl;
|
std::cout<<GridLogMessage<<ttmp<<std::endl;
|
||||||
std::cout<<GridLogMessage<<tmp<<std::endl;
|
std::cout<<GridLogMessage<<tmp<<std::endl;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
MatTimer.Start();
|
||||||
|
Linop.HermOpAndNorm(z,Az,zAz,zAAz);
|
||||||
|
MatTimer.Stop();
|
||||||
|
|
||||||
|
//p[0],q[0],qq[0]
|
||||||
|
p[0]= z;
|
||||||
|
q[0]= Az;
|
||||||
|
qq[0]= zAAz;
|
||||||
|
|
||||||
|
cp =norm2(r);
|
||||||
|
|
||||||
|
for(int k=0;k<nstep;k++){
|
||||||
|
|
||||||
|
steps++;
|
||||||
|
|
||||||
|
int kp = k+1;
|
||||||
|
int peri_k = k %mmax;
|
||||||
|
int peri_kp= kp%mmax;
|
||||||
|
|
||||||
|
rq= real(innerProduct(r,q[peri_k])); // what if rAr not real?
|
||||||
|
a = rq/qq[peri_k];
|
||||||
|
|
||||||
|
axpy(psi,a,p[peri_k],psi);
|
||||||
|
|
||||||
|
cp = axpy_norm(r,-a,q[peri_k],r);
|
||||||
|
|
||||||
|
if((k==nstep-1)||(cp<rsq)){
|
||||||
|
return cp;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout<<GridLogMessage<< " VPGCR_step["<<steps<<"] resid " <<sqrt(cp/rsq)<<std::endl;
|
||||||
|
|
||||||
|
PrecTimer.Start();
|
||||||
|
Preconditioner(r,z);// solve Az = r
|
||||||
|
PrecTimer.Stop();
|
||||||
|
|
||||||
MatTimer.Start();
|
MatTimer.Start();
|
||||||
Linop.HermOpAndNorm(z,Az,zAz,zAAz);
|
Linop.HermOpAndNorm(z,Az,zAz,zAAz);
|
||||||
|
Linop.HermOp(z,tmp);
|
||||||
MatTimer.Stop();
|
MatTimer.Stop();
|
||||||
|
tmp=tmp-r;
|
||||||
|
std::cout<<GridLogMessage<< " Preconditioner resid " <<sqrt(norm2(tmp)/norm2(r))<<std::endl;
|
||||||
|
|
||||||
//p[0],q[0],qq[0]
|
q[peri_kp]=Az;
|
||||||
p[0]= z;
|
p[peri_kp]=z;
|
||||||
q[0]= Az;
|
|
||||||
qq[0]= zAAz;
|
|
||||||
|
|
||||||
cp =norm2(r);
|
int northog = ((kp)>(mmax-1))?(mmax-1):(kp); // if more than mmax done, we orthog all mmax history.
|
||||||
|
for(int back=0;back<northog;back++){
|
||||||
|
|
||||||
for(int k=0;k<nstep;k++){
|
int peri_back=(k-back)%mmax; assert((k-back)>=0);
|
||||||
|
|
||||||
steps++;
|
|
||||||
|
|
||||||
int kp = k+1;
|
|
||||||
int peri_k = k %mmax;
|
|
||||||
int peri_kp= kp%mmax;
|
|
||||||
|
|
||||||
rq= real(innerProduct(r,q[peri_k])); // what if rAr not real?
|
|
||||||
a = rq/qq[peri_k];
|
|
||||||
|
|
||||||
axpy(psi,a,p[peri_k],psi);
|
|
||||||
|
|
||||||
cp = axpy_norm(r,-a,q[peri_k],r);
|
|
||||||
|
|
||||||
if((k==nstep-1)||(cp<rsq)){
|
|
||||||
return cp;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout<<GridLogMessage<< " VPGCR_step["<<steps<<"] resid " <<sqrt(cp/rsq)<<std::endl;
|
|
||||||
|
|
||||||
PrecTimer.Start();
|
|
||||||
Preconditioner(r,z);// solve Az = r
|
|
||||||
PrecTimer.Stop();
|
|
||||||
|
|
||||||
MatTimer.Start();
|
|
||||||
Linop.HermOpAndNorm(z,Az,zAz,zAAz);
|
|
||||||
Linop.HermOp(z,tmp);
|
|
||||||
MatTimer.Stop();
|
|
||||||
tmp=tmp-r;
|
|
||||||
std::cout<<GridLogMessage<< " Preconditioner resid " <<sqrt(norm2(tmp)/norm2(r))<<std::endl;
|
|
||||||
|
|
||||||
q[peri_kp]=Az;
|
|
||||||
p[peri_kp]=z;
|
|
||||||
|
|
||||||
int northog = ((kp)>(mmax-1))?(mmax-1):(kp); // if more than mmax done, we orthog all mmax history.
|
|
||||||
for(int back=0;back<northog;back++){
|
|
||||||
|
|
||||||
int peri_back=(k-back)%mmax; assert((k-back)>=0);
|
|
||||||
|
|
||||||
b=-real(innerProduct(q[peri_back],Az))/qq[peri_back];
|
|
||||||
p[peri_kp]=p[peri_kp]+b*p[peri_back];
|
|
||||||
q[peri_kp]=q[peri_kp]+b*q[peri_back];
|
|
||||||
|
|
||||||
}
|
|
||||||
qq[peri_kp]=norm2(q[peri_kp]); // could use axpy_norm
|
|
||||||
|
|
||||||
|
b=-real(innerProduct(q[peri_back],Az))/qq[peri_back];
|
||||||
|
p[peri_kp]=p[peri_kp]+b*p[peri_back];
|
||||||
|
q[peri_kp]=q[peri_kp]+b*q[peri_back];
|
||||||
|
|
||||||
}
|
}
|
||||||
assert(0); // never reached
|
qq[peri_kp]=norm2(q[peri_kp]); // could use axpy_norm
|
||||||
return cp;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
assert(0); // never reached
|
||||||
}
|
return cp;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
NAMESPACE_END(Grid);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user