1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 15:55:37 +00:00
Grid/Hadrons/Modules/MDistil/LapEvec.hpp

355 lines
13 KiB
C++
Raw Normal View History

2019-01-18 13:14:28 +00:00
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: Hadrons/Modules/MDistil/LapEvec.hpp
Copyright (C) 2019
2019-01-18 13:14:28 +00:00
Author: Felix Erben <ferben@ed.ac.uk>
Author: Michael Marshall <Michael.Marshall@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
*************************************************************************************/
/* END LEGAL */
#ifndef Hadrons_MDistil_LapEvec_hpp_
#define Hadrons_MDistil_LapEvec_hpp_
#include <Hadrons/Global.hpp>
#include <Hadrons/Module.hpp>
#include <Hadrons/ModuleFactory.hpp>
#include <Hadrons/EigenPack.hpp>
2019-01-18 13:14:28 +00:00
// These are members of Distillation
#include <Hadrons/Distil.hpp>
2019-01-18 13:14:28 +00:00
BEGIN_HADRONS_NAMESPACE
2019-01-22 13:19:39 +00:00
BEGIN_MODULE_NAMESPACE(MDistil)
2019-01-18 13:14:28 +00:00
/******************************************************************************
2019-01-22 13:19:39 +00:00
2019-01-23 12:59:55 +00:00
Laplacian eigenvectors - parameters
2019-01-18 13:14:28 +00:00
2019-01-23 12:59:55 +00:00
******************************************************************************/
struct StoutParameters: Serializable {
GRID_SERIALIZABLE_CLASS_MEMBERS(StoutParameters,
2019-01-22 13:19:39 +00:00
int, steps,
double, parm) // TODO: change name of this to rho
2019-01-23 12:59:55 +00:00
StoutParameters() = default;
template <class ReaderClass> StoutParameters(Reader<ReaderClass>& Reader){read(Reader,"StoutSmearing",*this);}
};
struct ChebyshevParameters: Serializable {
GRID_SERIALIZABLE_CLASS_MEMBERS(ChebyshevParameters,
2019-01-22 13:19:39 +00:00
int, PolyOrder,
double, alpha,
2019-01-23 12:59:55 +00:00
double, beta)
ChebyshevParameters() = default;
template <class ReaderClass> ChebyshevParameters(Reader<ReaderClass>& Reader){read(Reader,"Chebyshev",*this);}
};
struct LanczosParameters: Serializable {
GRID_SERIALIZABLE_CLASS_MEMBERS(LanczosParameters,
2019-01-24 13:26:05 +00:00
//int, Nstart,
2019-01-22 13:19:39 +00:00
int, Nvec,
int, Nk,
2019-01-24 13:26:05 +00:00
//int, Nm, // Not currently used
2019-01-22 13:19:39 +00:00
int, Np,
int, MaxIt,
2019-01-24 13:26:05 +00:00
//int, MinRes,
2019-01-23 12:59:55 +00:00
double, resid)
LanczosParameters() = default;
template <class ReaderClass> LanczosParameters(Reader<ReaderClass>& Reader){read(Reader,"Lanczos",*this);}
};
// These are the actual parameters passed to the module during construction
class LapEvecPar: Serializable
{
public:
GRID_SERIALIZABLE_CLASS_MEMBERS(LapEvecPar,
std::string, gauge,
// std::string, ConfigFileDir,
// std::string, ConfigFileName,
2019-01-24 18:50:18 +00:00
//,std::string, EigenPackName
StoutParameters, Stout
2019-01-24 18:50:18 +00:00
,ChebyshevParameters, Cheby
,LanczosParameters, Lanczos
//,DistilParameters, Distil
)//,SolverParameters, Solver)
2019-01-23 12:59:55 +00:00
};
/******************************************************************************
Laplacian eigenvectors - Module (class) definition
******************************************************************************/
2019-01-28 12:28:35 +00:00
template <typename GImpl>
2019-01-18 13:14:28 +00:00
class TLapEvec: public Module<LapEvecPar>
{
public:
2019-01-28 12:28:35 +00:00
GAUGE_TYPE_ALIASES(GImpl,);
// constructor
TLapEvec(const std::string name);
// destructor
virtual ~TLapEvec(void);
// dependency relation
virtual std::vector<std::string> getInput(void);
virtual std::vector<std::string> getOutput(void);
// setup
virtual void setup(void);
// execution
virtual void execute(void);
protected:
// These variables are created in setup() and freed in Cleanup()
GridCartesian * gridLD; // Owned by me, so I must delete it
GridCartesian * gridHD; // Owned by environment (so I won't delete it)
int Nx, Ny, Nz, Nt;
protected:
2019-01-30 21:16:09 +00:00
virtual void Cleanup(void);
2019-01-18 13:14:28 +00:00
};
2019-01-28 12:28:35 +00:00
MODULE_REGISTER_TMP(LapEvec, TLapEvec<GIMPL>, MDistil);
2019-01-18 13:14:28 +00:00
/******************************************************************************
TLapEvec implementation
2019-01-18 13:14:28 +00:00
******************************************************************************/
//constexpr char szEigenPackSuffix[] = "_eigenPack";
2019-01-18 13:14:28 +00:00
// constructor /////////////////////////////////////////////////////////////////
2019-01-28 12:28:35 +00:00
template <typename GImpl>
TLapEvec<GImpl>::TLapEvec(const std::string name) : gridLD{nullptr}, Module<LapEvecPar>(name)
{
//LOG(Message) << "TLapEvec constructor : TLapEvec<GImpl>::TLapEvec(const std::string name)" << std::endl;
//LOG(Message) << "this=" << this << ", gridLD=" << gridLD << std::endl;
}
// destructor /////////////////////////////////////////////////////////////////
2019-01-28 12:28:35 +00:00
template <typename GImpl>
TLapEvec<GImpl>::~TLapEvec()
{
Cleanup();
}
2019-01-18 13:14:28 +00:00
// dependencies/products ///////////////////////////////////////////////////////
2019-01-28 12:28:35 +00:00
template <typename GImpl>
std::vector<std::string> TLapEvec<GImpl>::getInput(void)
2019-01-18 13:14:28 +00:00
{
std::vector<std::string> in = {par().gauge};
2019-01-18 13:14:28 +00:00
return in;
}
2019-01-28 12:28:35 +00:00
template <typename GImpl>
std::vector<std::string> TLapEvec<GImpl>::getOutput(void)
2019-01-18 13:14:28 +00:00
{
std::vector<std::string> out = {getName()}; // This is the higher dimensional eigenpack
2019-01-18 13:14:28 +00:00
return out;
}
// setup ///////////////////////////////////////////////////////////////////////
2019-01-28 12:28:35 +00:00
template <typename GImpl>
void TLapEvec<GImpl>::setup(void)
2019-01-18 13:14:28 +00:00
{
Cleanup();
Environment & e{env()};
gridHD = e.getGrid();
gridLD = MakeLowerDimGrid( gridHD );
Nx = gridHD->_fdimensions[Xdir];
Ny = gridHD->_fdimensions[Ydir];
Nz = gridHD->_fdimensions[Zdir];
Nt = gridHD->_fdimensions[Tdir];
// Temporaries
2019-01-24 13:26:05 +00:00
//envTmpLat(GaugeField, "Umu");
envTmpLat(GaugeField, "Umu_stout");
envTmpLat(GaugeField, "Umu_smear");
envTmp(LatticeGaugeField, "UmuNoTime",1,LatticeGaugeField(gridLD));
envTmp(LatticeColourVector, "src",1,LatticeColourVector(gridLD));
envTmp(std::vector<DistilEP>, "eig",1,std::vector<DistilEP>(Nt));
// Output objects
envCreate(DistilEP, getName(), 1, par().Lanczos.Nvec, gridHD );
}
// clean up any temporaries created by setup (that aren't stored in the environment)
2019-01-28 12:28:35 +00:00
template <typename GImpl>
void TLapEvec<GImpl>::Cleanup(void)
{
if( gridLD != nullptr ) {
delete gridLD;
gridLD = nullptr;
}
gridHD = nullptr;
2019-01-18 13:14:28 +00:00
}
/******************************************************************************
Calculate low-mode eigenvalues of the Laplacian
******************************************************************************/
2019-01-18 13:14:28 +00:00
// execution ///////////////////////////////////////////////////////////////////
2019-01-28 12:28:35 +00:00
template <typename GImpl>
void TLapEvec<GImpl>::execute(void)
2019-01-18 13:14:28 +00:00
{
2019-01-24 18:50:18 +00:00
LOG(Message) << "execute() : start for " << getName() << std::endl;
const ChebyshevParameters &ChebPar{par().Cheby};
2019-01-24 18:50:18 +00:00
const LanczosParameters &LPar{par().Lanczos};
const int &nvec{LPar.Nvec};
//const bool exact_distillation{TI==Nt && LI==nvec};
//const bool full_tdil{TI==Nt};
//const int &Nt_inv{full_tdil ? 1 : TI};
// Assertions on the parameters we read
2019-01-24 18:50:18 +00:00
//assert(TI>1);
//assert(LI>1);
//if(exact_distillation)
//assert(nnoise==1);
//else
//assert(nnoise>1);
2019-01-24 13:26:05 +00:00
auto &Umu = envGet(GaugeField, par().gauge);
2019-01-24 18:50:18 +00:00
envGetTmp(GaugeField, Umu_smear);
2019-01-24 13:26:05 +00:00
// Stout smearing
2019-01-24 13:26:05 +00:00
Umu_smear = Umu;
LOG(Message) << "Initial plaquette: " << WilsonLoops<PeriodicGimplR>::avgPlaquette(Umu) << std::endl;
{
2019-01-24 18:50:18 +00:00
const StoutParameters &Stout{par().Stout};
envGetTmp(GaugeField, Umu_stout);
2019-04-26 08:54:30 +01:00
Smear_Stout<PeriodicGimplR> LS(Stout.parm, Tdir); // spatial smearing only
2019-01-24 18:50:18 +00:00
for (int i = 0; i < Stout.steps; i++) {
LS.smear(Umu_stout, Umu_smear);
Umu_smear = Umu_stout;
}
}
LOG(Message) << "Smeared plaquette: " << WilsonLoops<PeriodicGimplR>::avgPlaquette(Umu_smear) << std::endl;
// For debugging only, write logging output to a local file
std::ofstream * ll = nullptr;
const int rank{gridHD->ThisRank()};
if((0)) { // debug to a local log file
std::string filename{"Local_"};
filename.append(std::to_string(rank));
filename.append(".log");
ll = new std::ofstream(filename);
}
////////////////////////////////////////////////////////////////////////
// Invert Peardon Nabla operator separately on each time-slice
////////////////////////////////////////////////////////////////////////
bool bReturnValue = true;
auto & eig4d = envGet(DistilEP, getName() );
envGetTmp(std::vector<DistilEP>, eig); // Eigenpack for each timeslice
envGetTmp(LatticeGaugeField, UmuNoTime); // Gauge field without time dimension
envGetTmp(LatticeColourVector, src);
const int Ntlocal{gridHD->LocalDimensions()[Tdir]};
const int Ntfirst{gridHD->LocalStarts()[Tdir]};
2019-01-24 16:30:13 +00:00
const char DefaultOperatorXml[] = "<OPERATOR>Michael</OPERATOR>";
const char DefaultsolverXml[] = "<SOLVER>Felix</SOLVER>";
for(int t=Ntfirst;bReturnValue && t<Ntfirst+Ntlocal;t++){
std::cout << GridLogMessage << "------------------------------------------------------------" << std::endl;
std::cout << GridLogMessage << " Compute eigenpack, Timeslice = " << t << std::endl;
std::cout << GridLogMessage << "------------------------------------------------------------" << std::endl;
std::cout << "T: " << t << " / " << Ntfirst + Ntlocal << std::endl;
eig[t].resize(LPar.Nk+LPar.Np,gridLD);
// Construct smearing operator
ExtractSliceLocal(UmuNoTime,Umu_smear,0,t-Ntfirst,Grid::QCD::Tdir); // switch to 3d/4d objects
LinOpPeardonNabla<LatticeColourVector> PeardonNabla(UmuNoTime);
std::cout << "Chebyshev preconditioning to order " << ChebPar.PolyOrder
<< " with parameters (alpha,beta) = (" << ChebPar.alpha << "," << ChebPar.beta << ")" << std::endl;
Chebyshev<LatticeColourVector> Cheb(ChebPar.alpha,ChebPar.beta,ChebPar.PolyOrder);
//from Test_Cheby.cc
if ( ((0)) && Ntfirst == 0 && t==0) {
std::ofstream of("cheby_" + std::to_string(ChebPar.alpha) + "_" + std::to_string(ChebPar.beta) + "_" + std::to_string(ChebPar.PolyOrder));
Cheb.csv(of);
}
// Construct source vector according to Test_dwf_compressed_lanczos.cc
src=11.0;
RealD nn = norm2(src);
nn = Grid::sqrt(nn);
src = src * (1.0/nn);
GridLogIRL.Active(1);
LinOpPeardonNablaHerm<LatticeColourVector> PeardonNablaCheby(Cheb,PeardonNabla);
ImplicitlyRestartedLanczos<LatticeColourVector> IRL(PeardonNablaCheby,PeardonNabla,LPar.Nvec,LPar.Nk,LPar.Nk+LPar.Np,LPar.resid,LPar.MaxIt);
int Nconv = 0;
if(ll) *ll << t << " : Before IRL.calc()" << std::endl;
IRL.calc(eig[t].eval,eig[t].evec,src,Nconv);
if(ll) *ll << t << " : After IRL.calc()" << std::endl;
if( Nconv < LPar.Nvec ) {
bReturnValue = false;
if(ll) *ll << t << " : Convergence error : Only " << Nconv << " converged!" << std::endl;
} else {
if( Nconv > LPar.Nvec )
eig[t].resize( LPar.Nvec, gridLD );
std::cout << GridLogMessage << "Timeslice " << t << " has " << eig[t].eval.size() << " eigenvalues and " << eig[t].evec.size() << " eigenvectors." << std::endl;
// Now rotate the eigenvectors into our phase convention
RotateEigen( eig[t].evec );
if((0)) { // Debugging only
2019-01-25 13:26:48 +00:00
// Write the eigenvectors and eigenvalues to disk
//std::cout << GridLogMessage << "Writing eigenvalues/vectors to " << pszEigenPack << std::endl;
eig[t].record.operatorXml = DefaultOperatorXml;
eig[t].record.solverXml = DefaultsolverXml;
2019-01-28 12:28:35 +00:00
eig[t].write("DistilEigen",false,t);
2019-01-25 13:26:48 +00:00
//std::cout << GridLogMessage << "Written eigenvectors" << std::endl;
}
}
std::cout << "T: " << t << " / " << Ntfirst + Ntlocal << std::endl;
for (int i=0;i<LPar.Nvec;i++){
std::cout << "Inserting Timeslice " << t << " into vector " << i << std::endl;
InsertSliceLocal(eig[t].evec[i],eig4d.evec[i],0,t,3);
// TODO: Discuss: is this needed? Is there a better way?
if(t==0)
eig4d.eval[i] = eig[t].eval[i];
}
}
2019-01-24 16:30:13 +00:00
// Now write out the 4d eigenvectors
eig4d.record.operatorXml = DefaultOperatorXml;
eig4d.record.solverXml = DefaultsolverXml;
2019-04-19 14:09:36 +01:00
std::string sEigenPackName(getName());
sEigenPackName.append(".");
sEigenPackName.append(std::to_string(vm().getTrajectory()));
eig4d.write(sEigenPackName,false);
2019-01-24 16:30:13 +00:00
// Close the local debugging log file
if( ll ) {
*ll << " Returning " << bReturnValue << std::endl;
delete ll;
}
2019-01-23 12:59:55 +00:00
LOG(Message) << "execute() : end" << std::endl;
2019-01-18 13:14:28 +00:00
}
END_MODULE_NAMESPACE
END_HADRONS_NAMESPACE
#endif // Hadrons_MDistil_LapEvec_hpp_