mirror of
				https://github.com/paboyle/Grid.git
				synced 2025-11-03 21:44:33 +00:00 
			
		
		
		
	Covariant laplacian and implicit integration
This commit is contained in:
		@@ -37,25 +37,19 @@ namespace Grid {
 | 
			
		||||
namespace QCD {
 | 
			
		||||
 | 
			
		||||
////////////////////////////////////////////////////////////////////
 | 
			
		||||
class RNGModuleParameters: Serializable {
 | 
			
		||||
 | 
			
		||||
struct RNGModuleParameters: Serializable {
 | 
			
		||||
  GRID_SERIALIZABLE_CLASS_MEMBERS(RNGModuleParameters,
 | 
			
		||||
  std::string, serial_seeds,
 | 
			
		||||
  std::string, parallel_seeds,);
 | 
			
		||||
public:
 | 
			
		||||
  std::vector<int> SerialSeed;
 | 
			
		||||
  std::vector<int> ParallelSeed;
 | 
			
		||||
 | 
			
		||||
  RNGModuleParameters(const std::vector<int> S = std::vector<int>(),
 | 
			
		||||
                      const std::vector<int> P = std::vector<int>())
 | 
			
		||||
      : SerialSeed(S), ParallelSeed(P) {}
 | 
			
		||||
  std::vector<int> getSerialSeeds(){return strToVec<int>(serial_seeds);}
 | 
			
		||||
  std::vector<int> getParallelSeeds(){return strToVec<int>(parallel_seeds);}
 | 
			
		||||
 | 
			
		||||
  RNGModuleParameters(): serial_seeds("1"), parallel_seeds("1"){}
 | 
			
		||||
 | 
			
		||||
  template <class ReaderClass >
 | 
			
		||||
  RNGModuleParameters(Reader<ReaderClass>& Reader){
 | 
			
		||||
    read(Reader, "RandomNumberGenerator", *this); 
 | 
			
		||||
    SerialSeed = strToVec<int>(serial_seeds);
 | 
			
		||||
    ParallelSeed = strToVec<int>(parallel_seeds);
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
};
 | 
			
		||||
@@ -82,12 +76,14 @@ public:
 | 
			
		||||
  GridParallelRNG& get_pRNG() { return *pRNG_.get(); }
 | 
			
		||||
 | 
			
		||||
  void seed() {
 | 
			
		||||
    if (Params_.SerialSeed.size() == 0 && Params_.ParallelSeed.size() == 0) {
 | 
			
		||||
      std::cout << "Seeds not initialized" << std::endl;
 | 
			
		||||
    auto SerialSeeds   = Params_.getSerialSeeds();
 | 
			
		||||
    auto ParallelSeeds = Params_.getParallelSeeds();
 | 
			
		||||
    if (SerialSeeds.size() == 0 && ParallelSeeds.size() == 0) {
 | 
			
		||||
      std::cout << GridLogError << "Seeds not initialized" << std::endl;
 | 
			
		||||
      exit(1);
 | 
			
		||||
    }
 | 
			
		||||
    sRNG_.SeedFixedIntegers(Params_.SerialSeed);
 | 
			
		||||
    pRNG_->SeedFixedIntegers(Params_.ParallelSeed);
 | 
			
		||||
    sRNG_.SeedFixedIntegers(SerialSeeds);
 | 
			
		||||
    pRNG_->SeedFixedIntegers(ParallelSeeds);
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -75,6 +75,8 @@ class HMCResourceManager {
 | 
			
		||||
  bool have_RNG;
 | 
			
		||||
  bool have_CheckPointer;
 | 
			
		||||
 | 
			
		||||
  // NOTE: operator << is not overloaded for std::vector<string> 
 | 
			
		||||
  // so thsi function is necessary
 | 
			
		||||
  void output_vector_string(const std::vector<std::string> &vs){
 | 
			
		||||
    for (auto &i: vs)
 | 
			
		||||
      std::cout << i << " ";
 | 
			
		||||
@@ -85,13 +87,13 @@ class HMCResourceManager {
 | 
			
		||||
 public:
 | 
			
		||||
  HMCResourceManager() : have_RNG(false), have_CheckPointer(false) {}
 | 
			
		||||
 | 
			
		||||
  template <class ReaderClass >
 | 
			
		||||
  template <class ReaderClass, class vector_type = vComplex >
 | 
			
		||||
  void initialize(ReaderClass &Read){
 | 
			
		||||
    // assumes we are starting from the main node
 | 
			
		||||
 | 
			
		||||
    // Geometry
 | 
			
		||||
    GridModuleParameters GridPar(Read);
 | 
			
		||||
    GridFourDimModule GridMod( GridPar) ;
 | 
			
		||||
    GridFourDimModule<vector_type> GridMod( GridPar) ;
 | 
			
		||||
    AddGrid("gauge", GridMod);
 | 
			
		||||
 | 
			
		||||
    // Checkpointer
 | 
			
		||||
@@ -100,9 +102,6 @@ class HMCResourceManager {
 | 
			
		||||
    std::string cp_type;
 | 
			
		||||
    read(Read,"name", cp_type);
 | 
			
		||||
    std::cout << "Registered types " << std::endl;
 | 
			
		||||
    // NOTE: operator << is not overloaded for std::vector<string> 
 | 
			
		||||
    // so it complains here
 | 
			
		||||
    //std::cout << CPfactory.getBuilderList() << std::endl;
 | 
			
		||||
    output_vector_string(CPfactory.getBuilderList());
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -178,7 +177,7 @@ class HMCResourceManager {
 | 
			
		||||
 | 
			
		||||
  // Add a named grid set, 4d shortcut
 | 
			
		||||
  void AddFourDimGrid(std::string s) {
 | 
			
		||||
    GridFourDimModule Mod;
 | 
			
		||||
    GridFourDimModule<vComplex> Mod;
 | 
			
		||||
    AddGrid(s, Mod);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -31,41 +31,48 @@ directory
 | 
			
		||||
#define HMC_GRID_MODULES
 | 
			
		||||
 | 
			
		||||
namespace Grid {
 | 
			
		||||
namespace QCD {
 | 
			
		||||
 | 
			
		||||
// Resources
 | 
			
		||||
// Modules for grids 
 | 
			
		||||
 | 
			
		||||
class GridModuleParameters: Serializable{
 | 
			
		||||
  
 | 
			
		||||
// Introduce another namespace HMCModules?
 | 
			
		||||
 | 
			
		||||
class GridModuleParameters: Serializable{   
 | 
			
		||||
public:
 | 
			
		||||
  GRID_SERIALIZABLE_CLASS_MEMBERS(GridModuleParameters,
 | 
			
		||||
  std::string, lattice,
 | 
			
		||||
  std::string,  mpi);
 | 
			
		||||
  std::string, mpi);
 | 
			
		||||
 | 
			
		||||
public: 
 | 
			
		||||
  // these namings are ugly
 | 
			
		||||
  // also ugly the distinction between the serializable members
 | 
			
		||||
  // and this
 | 
			
		||||
  std::vector<int> lattice_v;
 | 
			
		||||
  std::vector<int> mpi_v;
 | 
			
		||||
  std::vector<int> getLattice(){return strToVec<int>(lattice);}
 | 
			
		||||
  std::vector<int> getMpi()    {return strToVec<int>(mpi);}
 | 
			
		||||
 | 
			
		||||
  GridModuleParameters(const std::vector<int> l_ = std::vector<int>(),
 | 
			
		||||
    const std::vector<int> mpi_ = std::vector<int>()):lattice_v(l_), mpi_v(mpi_){}
 | 
			
		||||
 | 
			
		||||
  template <class ReaderClass>
 | 
			
		||||
  GridModuleParameters(Reader<ReaderClass>& Reader) {
 | 
			
		||||
    read(Reader, "LatticeGrid", *this);
 | 
			
		||||
    lattice_v = strToVec<int>(lattice);
 | 
			
		||||
    mpi_v = strToVec<int>(mpi);
 | 
			
		||||
    if (mpi_v.size() != lattice_v.size()) {
 | 
			
		||||
      std::cout << "Error in GridModuleParameters: lattice and mpi dimensions "
 | 
			
		||||
  void check(){
 | 
			
		||||
    if (getLattice().size() != getMpi().size()) {
 | 
			
		||||
      std::cout << GridLogError 
 | 
			
		||||
                << "Error in GridModuleParameters: lattice and mpi dimensions "
 | 
			
		||||
                   "do not match"
 | 
			
		||||
                << std::endl;
 | 
			
		||||
      exit(1);
 | 
			
		||||
    }
 | 
			
		||||
  }    
 | 
			
		||||
 | 
			
		||||
  template <class ReaderClass>
 | 
			
		||||
  GridModuleParameters(Reader<ReaderClass>& Reader) {
 | 
			
		||||
    read(Reader, name, *this);
 | 
			
		||||
    check();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Save on file
 | 
			
		||||
  template< class WriterClass>
 | 
			
		||||
  void save(Writer<WriterClass>& Writer){
 | 
			
		||||
    check();
 | 
			
		||||
    write(Writer, name, *this);
 | 
			
		||||
  }
 | 
			
		||||
private:
 | 
			
		||||
    std::string name = "LatticeGrid";
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Lower level class
 | 
			
		||||
class GridModule {
 | 
			
		||||
 public:
 | 
			
		||||
  GridCartesian* get_full() { 
 | 
			
		||||
@@ -84,27 +91,33 @@ class GridModule {
 | 
			
		||||
  
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// helpers
 | 
			
		||||
// FIXME define a class accepting also real vtypes
 | 
			
		||||
////////////////////////////////////
 | 
			
		||||
// Classes for the user
 | 
			
		||||
////////////////////////////////////
 | 
			
		||||
// Note: the space time grid must be out of the QCD namespace
 | 
			
		||||
template< class vector_type>
 | 
			
		||||
class GridFourDimModule : public GridModule {
 | 
			
		||||
 public:
 | 
			
		||||
  // add a function to create the module from a Reader
 | 
			
		||||
  GridFourDimModule() {
 | 
			
		||||
    using namespace QCD;
 | 
			
		||||
    set_full(SpaceTimeGrid::makeFourDimGrid(
 | 
			
		||||
        GridDefaultLatt(), GridDefaultSimd(4, vComplex::Nsimd()),
 | 
			
		||||
        GridDefaultLatt(), GridDefaultSimd(4, vector_type::Nsimd()),
 | 
			
		||||
        GridDefaultMpi()));
 | 
			
		||||
    set_rb(SpaceTimeGrid::makeFourDimRedBlackGrid(grid_.get()));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  template <class vector_type = vComplex>
 | 
			
		||||
  GridFourDimModule(GridModuleParameters Params) {
 | 
			
		||||
    if (Params.lattice_v.size() == 4) {
 | 
			
		||||
    using namespace QCD;
 | 
			
		||||
    Params.check();
 | 
			
		||||
    std::vector<int> lattice_v = Params.getLattice();
 | 
			
		||||
    std::vector<int> mpi_v = Params.getMpi();
 | 
			
		||||
    if (lattice_v.size() == 4) {
 | 
			
		||||
      set_full(SpaceTimeGrid::makeFourDimGrid(
 | 
			
		||||
          Params.lattice_v, GridDefaultSimd(4, vector_type::Nsimd()),
 | 
			
		||||
          Params.mpi_v));
 | 
			
		||||
          lattice_v, GridDefaultSimd(4, vector_type::Nsimd()),
 | 
			
		||||
          mpi_v));
 | 
			
		||||
      set_rb(SpaceTimeGrid::makeFourDimRedBlackGrid(grid_.get()));
 | 
			
		||||
    } else {
 | 
			
		||||
      std::cout
 | 
			
		||||
      std::cout << GridLogError 
 | 
			
		||||
          << "Error in GridFourDimModule: lattice dimension different from 4"
 | 
			
		||||
          << std::endl;
 | 
			
		||||
      exit(1);
 | 
			
		||||
@@ -112,10 +125,9 @@ class GridFourDimModule : public GridModule {
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef GridFourDimModule<vComplex> GridDefaultFourDimModule;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}  // namespace QCD
 | 
			
		||||
}  // namespace Grid
 | 
			
		||||
 | 
			
		||||
#endif  // HMC_GRID_MODULES
 | 
			
		||||
@@ -131,6 +131,49 @@ class Integrator {
 | 
			
		||||
    as[level].apply(update_P_hireps, Representations, Mom, U, ep);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void implicit_update_P(MomentaField& Mom, Field& U, int level, double ep) {
 | 
			
		||||
    // Fundamental updates, include smearing
 | 
			
		||||
    MomentaField Msum(Mom._grid);
 | 
			
		||||
    Msum = zero;
 | 
			
		||||
    for (int a = 0; a < as[level].actions.size(); ++a) {
 | 
			
		||||
      // Compute the force 
 | 
			
		||||
      // We need to compute the derivative of the actions
 | 
			
		||||
      // only once
 | 
			
		||||
      Field force(U._grid);
 | 
			
		||||
      conformable(U._grid, Mom._grid);
 | 
			
		||||
      Field& Us = Smearer.get_U(as[level].actions.at(a)->is_smeared);
 | 
			
		||||
      as[level].actions.at(a)->deriv(Us, force);  // deriv should NOT include Ta
 | 
			
		||||
 | 
			
		||||
      std::cout << GridLogIntegrator << "Smearing (on/off): " << as[level].actions.at(a)->is_smeared << std::endl;
 | 
			
		||||
      if (as[level].actions.at(a)->is_smeared) Smearer.smeared_force(force);
 | 
			
		||||
      force = FieldImplementation::projectForce(force); // Ta for gauge fields
 | 
			
		||||
      Real force_abs = std::sqrt(norm2(force)/U._grid->gSites());
 | 
			
		||||
      std::cout << GridLogIntegrator << "Force average: " << force_abs << std::endl;
 | 
			
		||||
      Msum += force;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    MomentaField NewMom = Mom;
 | 
			
		||||
    MomentaField OldMom = Mom;
 | 
			
		||||
    double threshold = 1e-6;
 | 
			
		||||
    // Here run recursively
 | 
			
		||||
    do{
 | 
			
		||||
      MomentaField MomDer(Mom._grid);
 | 
			
		||||
      OldMom = NewMom;
 | 
			
		||||
      // Compute the derivative of the kinetic term
 | 
			
		||||
      // with respect to the gauge field
 | 
			
		||||
 | 
			
		||||
      // Laplacian.Mder(NewMom, MomDer);
 | 
			
		||||
      // NewMom = Mom - ep*(MomDer + Msum);
 | 
			
		||||
 | 
			
		||||
    } while (norm2(NewMom - OldMom) > threshold);
 | 
			
		||||
 | 
			
		||||
    Mom = NewMom;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // update the auxiliary fields momenta
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  void update_U(Field& U, double ep) {
 | 
			
		||||
    update_U(P, U, ep);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -285,6 +285,74 @@ class ForceGradient : public Integrator<FieldImplementation, SmearingPolicy,
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
////////////////////////////////
 | 
			
		||||
// Riemannian Manifold HMC
 | 
			
		||||
// Girolami et al
 | 
			
		||||
////////////////////////////////
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
template <class FieldImplementation, class SmearingPolicy,
 | 
			
		||||
          class RepresentationPolicy =
 | 
			
		||||
              Representations<FundamentalRepresentation> >
 | 
			
		||||
class ImplicitLeapFrog : public Integrator<FieldImplementation, SmearingPolicy,
 | 
			
		||||
                                           RepresentationPolicy> {
 | 
			
		||||
 public:
 | 
			
		||||
  typedef ImplicitLeapFrog<FieldImplementation, SmearingPolicy, RepresentationPolicy>
 | 
			
		||||
      Algorithm;
 | 
			
		||||
  INHERIT_FIELD_TYPES(FieldImplementation);
 | 
			
		||||
 | 
			
		||||
  // Riemannian manifold metric operator
 | 
			
		||||
  // Hermitian operator Fisher
 | 
			
		||||
 | 
			
		||||
  std::string integrator_name(){return "ImplicitLeapFrog";}
 | 
			
		||||
 | 
			
		||||
  ImplicitLeapFrog(GridBase* grid, IntegratorParameters Par,
 | 
			
		||||
           ActionSet<Field, RepresentationPolicy>& Aset, SmearingPolicy& Sm)
 | 
			
		||||
      : Integrator<FieldImplementation, SmearingPolicy, RepresentationPolicy>(
 | 
			
		||||
            grid, Par, Aset, Sm){};
 | 
			
		||||
 | 
			
		||||
  void step(Field& U, int level, int _first, int _last) {
 | 
			
		||||
    int fl = this->as.size() - 1;
 | 
			
		||||
    // level  : current level
 | 
			
		||||
    // fl     : final level
 | 
			
		||||
    // eps    : current step size
 | 
			
		||||
 | 
			
		||||
    // Get current level step size
 | 
			
		||||
    RealD eps = this->Params.trajL/this->Params.MDsteps;
 | 
			
		||||
    for (int l = 0; l <= level; ++l) eps /= this->as[l].multiplier;
 | 
			
		||||
 | 
			
		||||
    int multiplier = this->as[level].multiplier;
 | 
			
		||||
    for (int e = 0; e < multiplier; ++e) {
 | 
			
		||||
      int first_step = _first && (e == 0);
 | 
			
		||||
      int last_step = _last && (e == multiplier - 1);
 | 
			
		||||
 | 
			
		||||
      if (first_step) {  // initial half step
 | 
			
		||||
       this->implicit_update_P(U, level, eps / 2.0);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (level == fl) {  // lowest level
 | 
			
		||||
        this->update_U(U, eps);
 | 
			
		||||
      } else {  // recursive function call
 | 
			
		||||
        this->step(U, level + 1, first_step, last_step);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      int mm = last_step ? 1 : 2;
 | 
			
		||||
      this->update_P(U, level, mm * eps / 2.0);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user