mirror of
https://github.com/paboyle/Grid.git
synced 2025-04-09 21:50:45 +01:00
Namespace, indent
This commit is contained in:
parent
40232dcefe
commit
2cceebbf12
@ -1,4 +1,4 @@
|
|||||||
/*************************************************************************************
|
/*************************************************************************************
|
||||||
|
|
||||||
Grid physics library, www.github.com/paboyle/Grid
|
Grid physics library, www.github.com/paboyle/Grid
|
||||||
|
|
||||||
@ -24,88 +24,88 @@ 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_CONJUGATE_RESIDUAL_H
|
#ifndef GRID_CONJUGATE_RESIDUAL_H
|
||||||
#define GRID_CONJUGATE_RESIDUAL_H
|
#define GRID_CONJUGATE_RESIDUAL_H
|
||||||
|
|
||||||
namespace Grid {
|
NAMESPACE_BEGIN(Grid);
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////
|
||||||
// Base classes for iterative processes based on operators
|
// Base classes for iterative processes based on operators
|
||||||
// single input vec, single output vec.
|
// single input vec, single output vec.
|
||||||
/////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
template<class Field>
|
template<class Field>
|
||||||
class ConjugateResidual : public OperatorFunction<Field> {
|
class ConjugateResidual : public OperatorFunction<Field> {
|
||||||
public:
|
public:
|
||||||
RealD Tolerance;
|
RealD Tolerance;
|
||||||
Integer MaxIterations;
|
Integer MaxIterations;
|
||||||
int verbose;
|
int verbose;
|
||||||
|
|
||||||
ConjugateResidual(RealD tol,Integer maxit) : Tolerance(tol), MaxIterations(maxit) {
|
ConjugateResidual(RealD tol,Integer maxit) : Tolerance(tol), MaxIterations(maxit) {
|
||||||
verbose=0;
|
verbose=0;
|
||||||
};
|
};
|
||||||
|
|
||||||
void operator() (LinearOperatorBase<Field> &Linop,const Field &src, Field &psi){
|
void operator() (LinearOperatorBase<Field> &Linop,const Field &src, Field &psi){
|
||||||
|
|
||||||
RealD a, b, c, d;
|
RealD a, b, c, d;
|
||||||
RealD cp, ssq,rsq;
|
RealD cp, ssq,rsq;
|
||||||
|
|
||||||
RealD rAr, rAAr, rArp;
|
RealD rAr, rAAr, rArp;
|
||||||
RealD pAp, pAAp;
|
RealD pAp, pAAp;
|
||||||
|
|
||||||
GridBase *grid = src._grid;
|
GridBase *grid = src._grid;
|
||||||
psi=zero;
|
psi=zero;
|
||||||
Field r(grid), p(grid), Ap(grid), Ar(grid);
|
Field r(grid), p(grid), Ap(grid), Ar(grid);
|
||||||
|
|
||||||
r=src;
|
r=src;
|
||||||
p=src;
|
p=src;
|
||||||
|
|
||||||
|
Linop.HermOpAndNorm(p,Ap,pAp,pAAp);
|
||||||
|
Linop.HermOpAndNorm(r,Ar,rAr,rAAr);
|
||||||
|
|
||||||
|
cp =norm2(r);
|
||||||
|
ssq=norm2(src);
|
||||||
|
rsq=Tolerance*Tolerance*ssq;
|
||||||
|
|
||||||
|
if (verbose) std::cout<<GridLogMessage<<"ConjugateResidual: iteration " <<0<<" residual "<<cp<< " target"<< rsq<<std::endl;
|
||||||
|
|
||||||
|
for(int k=1;k<MaxIterations;k++){
|
||||||
|
|
||||||
|
a = rAr/pAAp;
|
||||||
|
|
||||||
|
axpy(psi,a,p,psi);
|
||||||
|
|
||||||
|
cp = axpy_norm(r,-a,Ap,r);
|
||||||
|
|
||||||
|
rArp=rAr;
|
||||||
|
|
||||||
Linop.HermOpAndNorm(p,Ap,pAp,pAAp);
|
|
||||||
Linop.HermOpAndNorm(r,Ar,rAr,rAAr);
|
Linop.HermOpAndNorm(r,Ar,rAr,rAAr);
|
||||||
|
|
||||||
cp =norm2(r);
|
b =rAr/rArp;
|
||||||
ssq=norm2(src);
|
|
||||||
rsq=Tolerance*Tolerance*ssq;
|
|
||||||
|
|
||||||
if (verbose) std::cout<<GridLogMessage<<"ConjugateResidual: iteration " <<0<<" residual "<<cp<< " target"<< rsq<<std::endl;
|
|
||||||
|
|
||||||
for(int k=1;k<MaxIterations;k++){
|
|
||||||
|
|
||||||
a = rAr/pAAp;
|
|
||||||
|
|
||||||
axpy(psi,a,p,psi);
|
|
||||||
|
|
||||||
cp = axpy_norm(r,-a,Ap,r);
|
|
||||||
|
|
||||||
rArp=rAr;
|
|
||||||
|
|
||||||
Linop.HermOpAndNorm(r,Ar,rAr,rAAr);
|
|
||||||
|
|
||||||
b =rAr/rArp;
|
|
||||||
|
|
||||||
axpy(p,b,p,r);
|
axpy(p,b,p,r);
|
||||||
pAAp=axpy_norm(Ap,b,Ap,Ar);
|
pAAp=axpy_norm(Ap,b,Ap,Ar);
|
||||||
|
|
||||||
if(verbose) std::cout<<GridLogMessage<<"ConjugateResidual: iteration " <<k<<" residual "<<cp<< " target"<< rsq<<std::endl;
|
if(verbose) std::cout<<GridLogMessage<<"ConjugateResidual: iteration " <<k<<" residual "<<cp<< " target"<< rsq<<std::endl;
|
||||||
|
|
||||||
if(cp<rsq) {
|
|
||||||
Linop.HermOp(psi,Ap);
|
|
||||||
axpy(r,-1.0,src,Ap);
|
|
||||||
RealD true_resid = norm2(r)/ssq;
|
|
||||||
std::cout<<GridLogMessage<<"ConjugateResidual: Converged on iteration " <<k
|
|
||||||
<< " computed residual "<<sqrt(cp/ssq)
|
|
||||||
<< " true residual "<<sqrt(true_resid)
|
|
||||||
<< " target " <<Tolerance <<std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if(cp<rsq) {
|
||||||
|
Linop.HermOp(psi,Ap);
|
||||||
|
axpy(r,-1.0,src,Ap);
|
||||||
|
RealD true_resid = norm2(r)/ssq;
|
||||||
|
std::cout<<GridLogMessage<<"ConjugateResidual: Converged on iteration " <<k
|
||||||
|
<< " computed residual "<<sqrt(cp/ssq)
|
||||||
|
<< " true residual "<<sqrt(true_resid)
|
||||||
|
<< " target " <<Tolerance <<std::endl;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout<<GridLogMessage<<"ConjugateResidual did NOT converge"<<std::endl;
|
|
||||||
assert(0);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}
|
std::cout<<GridLogMessage<<"ConjugateResidual did NOT converge"<<std::endl;
|
||||||
|
assert(0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
NAMESPACE_END(Grid);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user