2015-06-21 10:58:46 +01:00
|
|
|
#ifndef GRID_PREC_GCR_H
|
|
|
|
#define GRID_PREC_GCR_H
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//VPGCR Abe and Zhang, 2005.
|
|
|
|
//INTERNATIONAL JOURNAL OF NUMERICAL ANALYSIS AND MODELING
|
|
|
|
//Computing and Information Volume 2, Number 2, Pages 147-161
|
2015-06-30 15:02:27 +01:00
|
|
|
//NB. Likely not original reference since they are focussing on a preconditioner variant.
|
|
|
|
// but VPGCR was nicely written up in their paper
|
2015-06-21 10:58:46 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-06-20 22:24:21 +01:00
|
|
|
namespace Grid {
|
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
template<class Field>
|
|
|
|
class PrecGeneralisedConjugateResidual : public OperatorFunction<Field> {
|
2015-06-20 22:24:21 +01:00
|
|
|
public:
|
|
|
|
RealD Tolerance;
|
|
|
|
Integer MaxIterations;
|
|
|
|
int verbose;
|
2015-06-21 10:58:46 +01:00
|
|
|
int mmax;
|
|
|
|
int nstep;
|
|
|
|
int steps;
|
|
|
|
LinearFunction<Field> &Preconditioner;
|
|
|
|
|
2015-06-30 15:02:27 +01:00
|
|
|
PrecGeneralisedConjugateResidual(RealD tol,Integer maxit,LinearFunction<Field> &Prec,int _mmax,int _nstep) :
|
2015-06-21 10:58:46 +01:00
|
|
|
Tolerance(tol),
|
|
|
|
MaxIterations(maxit),
|
|
|
|
Preconditioner(Prec),
|
|
|
|
mmax(_mmax),
|
|
|
|
nstep(_nstep)
|
|
|
|
{
|
|
|
|
verbose=1;
|
2015-06-20 22:24:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void operator() (LinearOperatorBase<Field> &Linop,const Field &src, Field &psi){
|
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
psi=zero;
|
2015-06-20 22:24:21 +01:00
|
|
|
RealD cp, ssq,rsq;
|
2015-06-21 10:58:46 +01:00
|
|
|
ssq=norm2(src);
|
|
|
|
rsq=Tolerance*Tolerance*ssq;
|
2015-06-20 22:24:21 +01:00
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
Field r(src._grid);
|
|
|
|
|
|
|
|
steps=0;
|
|
|
|
for(int k=0;k<MaxIterations;k++){
|
|
|
|
|
|
|
|
cp=GCRnStep(Linop,src,psi,rsq);
|
|
|
|
|
2015-07-23 17:31:13 +01:00
|
|
|
if ( verbose ) std::cout<<GridLogMessage<<"VPGCR("<<mmax<<","<<nstep<<") "<< steps <<" steps cp = "<<cp<<std::endl;
|
2015-06-21 10:58:46 +01:00
|
|
|
|
|
|
|
if(cp<rsq) {
|
|
|
|
Linop.HermOp(psi,r);
|
|
|
|
axpy(r,-1.0,src,r);
|
2015-06-22 12:49:44 +01:00
|
|
|
RealD tr = norm2(r);
|
2015-07-23 17:31:13 +01:00
|
|
|
std::cout<<GridLogMessage<<"PrecGeneralisedConjugateResidual: Converged on iteration " <<steps
|
2015-06-21 10:58:46 +01:00
|
|
|
<< " computed residual "<<sqrt(cp/ssq)
|
2015-06-22 12:49:44 +01:00
|
|
|
<< " true residual " <<sqrt(tr/ssq)
|
|
|
|
<< " target " <<Tolerance <<std::endl;
|
2015-06-21 10:58:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-07-23 17:31:13 +01:00
|
|
|
std::cout<<GridLogMessage<<"Variable Preconditioned GCR did not converge"<<std::endl;
|
2015-06-21 10:58:46 +01:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
RealD GCRnStep(LinearOperatorBase<Field> &Linop,const Field &src, Field &psi,RealD rsq){
|
|
|
|
|
|
|
|
RealD cp;
|
|
|
|
RealD a, b, c, d;
|
|
|
|
RealD zAz, zAAz;
|
|
|
|
RealD rAq, rq;
|
2015-06-20 22:24:21 +01:00
|
|
|
|
|
|
|
GridBase *grid = src._grid;
|
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
Field r(grid);
|
|
|
|
Field z(grid);
|
2015-06-30 15:02:27 +01:00
|
|
|
Field tmp(grid);
|
|
|
|
Field ttmp(grid);
|
2015-06-21 10:58:46 +01:00
|
|
|
Field Az(grid);
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
// history for flexible orthog
|
|
|
|
////////////////////////////////
|
|
|
|
std::vector<Field> q(mmax,grid);
|
|
|
|
std::vector<Field> p(mmax,grid);
|
|
|
|
std::vector<RealD> qq(mmax);
|
|
|
|
|
|
|
|
//////////////////////////////////
|
|
|
|
// initial guess x0 is taken as nonzero.
|
|
|
|
// r0=src-A x0 = src
|
|
|
|
//////////////////////////////////
|
|
|
|
Linop.HermOpAndNorm(psi,Az,zAz,zAAz);
|
|
|
|
r=src-Az;
|
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
// p = Prec(r)
|
|
|
|
/////////////////////
|
|
|
|
Preconditioner(r,z);
|
2015-06-30 15:02:27 +01:00
|
|
|
|
2015-07-23 17:31:13 +01:00
|
|
|
std::cout<<GridLogMessage<< " Preconditioner in " << norm2(r)<<std::endl;
|
|
|
|
std::cout<<GridLogMessage<< " Preconditioner out " << norm2(z)<<std::endl;
|
2015-06-30 15:02:27 +01:00
|
|
|
|
|
|
|
Linop.HermOp(z,tmp);
|
|
|
|
|
2015-07-23 17:31:13 +01:00
|
|
|
std::cout<<GridLogMessage<< " Preconditioner Aout " << norm2(tmp)<<std::endl;
|
2015-06-30 15:02:27 +01:00
|
|
|
ttmp=tmp;
|
|
|
|
tmp=tmp-r;
|
|
|
|
|
2015-07-23 17:31:13 +01:00
|
|
|
std::cout<<GridLogMessage<< " Preconditioner resid " << std::sqrt(norm2(tmp)/norm2(r))<<std::endl;
|
2015-06-30 15:02:27 +01:00
|
|
|
/*
|
2015-07-23 17:31:13 +01:00
|
|
|
std::cout<<GridLogMessage<<r<<std::endl;
|
|
|
|
std::cout<<GridLogMessage<<z<<std::endl;
|
|
|
|
std::cout<<GridLogMessage<<ttmp<<std::endl;
|
|
|
|
std::cout<<GridLogMessage<<tmp<<std::endl;
|
2015-06-30 15:02:27 +01:00
|
|
|
*/
|
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
Linop.HermOpAndNorm(z,Az,zAz,zAAz);
|
2015-06-20 22:24:21 +01:00
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
//p[0],q[0],qq[0]
|
|
|
|
p[0]= z;
|
|
|
|
q[0]= Az;
|
|
|
|
qq[0]= zAAz;
|
2015-06-20 22:24:21 +01:00
|
|
|
|
|
|
|
cp =norm2(r);
|
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
for(int k=0;k<nstep;k++){
|
2015-06-20 22:24:21 +01:00
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
steps++;
|
2015-06-20 22:24:21 +01:00
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
int kp = k+1;
|
|
|
|
int peri_k = k %mmax;
|
|
|
|
int peri_kp= kp%mmax;
|
2015-06-20 22:24:21 +01:00
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
rq= real(innerProduct(r,q[peri_k])); // what if rAr not real?
|
|
|
|
a = rq/qq[peri_k];
|
2015-06-20 22:24:21 +01:00
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
axpy(psi,a,p[peri_k],psi);
|
2015-06-20 22:24:21 +01:00
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
cp = axpy_norm(r,-a,q[peri_k],r);
|
2015-06-20 22:24:21 +01:00
|
|
|
|
2015-07-23 17:31:13 +01:00
|
|
|
std::cout<<GridLogMessage<< " VPGCR_step resid" <<sqrt(cp/rsq)<<std::endl;
|
2015-06-21 10:58:46 +01:00
|
|
|
if((k==nstep-1)||(cp<rsq)){
|
|
|
|
return cp;
|
|
|
|
}
|
2015-06-20 22:24:21 +01:00
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
Preconditioner(r,z);// solve Az = r
|
|
|
|
Linop.HermOpAndNorm(z,Az,zAz,zAAz);
|
|
|
|
|
2015-06-30 15:02:27 +01:00
|
|
|
|
|
|
|
Linop.HermOp(z,tmp);
|
|
|
|
tmp=tmp-r;
|
2015-07-23 17:31:13 +01:00
|
|
|
std::cout<<GridLogMessage<< " Preconditioner resid" <<sqrt(norm2(tmp)/norm2(r))<<std::endl;
|
2015-06-30 15:02:27 +01:00
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
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];
|
2015-06-20 22:24:21 +01:00
|
|
|
|
|
|
|
}
|
2015-06-21 10:58:46 +01:00
|
|
|
qq[peri_kp]=norm2(q[peri_kp]); // could use axpy_norm
|
2015-06-20 22:24:21 +01:00
|
|
|
|
|
|
|
|
2015-06-21 10:58:46 +01:00
|
|
|
}
|
|
|
|
assert(0); // never reached
|
|
|
|
return cp;
|
2015-06-20 22:24:21 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|