1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-14 09:45:36 +00:00
Grid/lib/algorithms/iterative/ConjugateResidual.h

112 lines
3.2 KiB
C
Raw Normal View History

2018-01-15 00:12:20 +00:00
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/algorithms/iterative/ConjugateResidual.h
Copyright (C) 2015
Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
See the full license in the file "LICENSE" in the top level distribution directory
2018-01-15 00:12:20 +00:00
*************************************************************************************/
/* END LEGAL */
2015-06-05 18:16:25 +01:00
#ifndef GRID_CONJUGATE_RESIDUAL_H
#define GRID_CONJUGATE_RESIDUAL_H
2018-01-15 00:12:20 +00:00
NAMESPACE_BEGIN(Grid);
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
/////////////////////////////////////////////////////////////
// Base classes for iterative processes based on operators
// single input vec, single output vec.
/////////////////////////////////////////////////////////////
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
template<class Field>
class ConjugateResidual : public OperatorFunction<Field> {
public:
RealD Tolerance;
Integer MaxIterations;
int verbose;
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
ConjugateResidual(RealD tol,Integer maxit) : Tolerance(tol), MaxIterations(maxit) {
verbose=0;
};
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
void operator() (LinearOperatorBase<Field> &Linop,const Field &src, Field &psi){
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
RealD a, b, c, d;
RealD cp, ssq,rsq;
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
RealD rAr, rAAr, rArp;
RealD pAp, pAAp;
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
GridBase *grid = src._grid;
psi=zero;
Field r(grid), p(grid), Ap(grid), Ar(grid);
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
r=src;
p=src;
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
Linop.HermOpAndNorm(p,Ap,pAp,pAAp);
Linop.HermOpAndNorm(r,Ar,rAr,rAAr);
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
cp =norm2(r);
ssq=norm2(src);
rsq=Tolerance*Tolerance*ssq;
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
if (verbose) std::cout<<GridLogMessage<<"ConjugateResidual: iteration " <<0<<" residual "<<cp<< " target"<< rsq<<std::endl;
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
for(int k=1;k<MaxIterations;k++){
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
a = rAr/pAAp;
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
axpy(psi,a,p,psi);
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
cp = axpy_norm(r,-a,Ap,r);
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
rArp=rAr;
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
Linop.HermOpAndNorm(r,Ar,rAr,rAAr);
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
b =rAr/rArp;
2015-06-05 18:16:25 +01:00
2018-01-15 00:12:20 +00:00
axpy(p,b,p,r);
pAAp=axpy_norm(Ap,b,Ap,Ar);
2018-01-15 00:12:20 +00:00
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;
2015-06-05 18:16:25 +01:00
}
}
2018-01-15 00:12:20 +00:00
std::cout<<GridLogMessage<<"ConjugateResidual did NOT converge"<<std::endl;
assert(0);
}
};
NAMESPACE_END(Grid);
2015-06-05 18:16:25 +01:00
#endif