SpinParser  1.0
SU2EffectiveAction.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 #include "lib/Exception.hpp"
11 #include "EffectiveAction.hpp"
12 #include "SU2FrgCore.hpp"
14 #include "SU2VertexTwoParticle.hpp"
15 
20 {
21 public:
26  {
29  }
30 
38  SU2EffectiveAction(const float cutoff, const SpinModel &spinModel, const SU2FrgCore *core)
39  {
42 
43  //set initial value
44  this->cutoff = cutoff;
45 
46  for (int linearIterator = 0; linearIterator < vertexTwoParticle->size; ++linearIterator)
47  {
48  float s, t, u;
49  LatticeIterator i1;
50  vertexTwoParticle->expandIterator(linearIterator, i1, s, t, u);
51 
52  for (auto i : spinModel.interactions)
53  {
54  if (i.first == i1)
55  {
56  vertexTwoParticle->getValueRef(linearIterator, SU2VertexTwoParticle::Symmetry::Spin) += i.second.interactionStrength[0][0] / core->normalization;
57  }
58  }
59  }
60  }
61 
66  {
67  delete vertexSingleParticle;
68  delete vertexTwoParticle;
69  }
70 
79  int writeCheckpoint(const std::string &dataFilePath, const bool append = false) const override
80  {
81  H5Eset_auto(H5E_DEFAULT, NULL, NULL);
82  hid_t file;
83 
84  //open or create file
85  if (append && H5Fis_hdf5(dataFilePath.c_str()) > 0) file = H5Fopen(dataFilePath.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
86  else file = H5Fcreate(dataFilePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
87  if (file < 0) throw Exception(Exception::Type::IOError, "Could not open data file for writing");
88 
89  //determine new checkpoint id
90  hsize_t numObjects;
91  H5Gget_num_objs(file, &numObjects);
92  int checkpointId = 0;
93  for (int i = 0; i < int(numObjects); ++i)
94  {
95  if (H5Gget_objtype_by_idx(file, i) == H5G_GROUP)
96  {
97  ++checkpointId;
98 
99  const int groupNameMaxLength = 32;
100  char groupName[groupNameMaxLength];
101  H5Gget_objname_by_idx(file, i, groupName, groupNameMaxLength);
102 
103  hid_t group = H5Gopen(file, groupName, H5P_DEFAULT);
104  hid_t attr = H5Aopen(group, "cutoff", H5P_DEFAULT);
105  float c;
106  H5Aread(attr, H5T_NATIVE_FLOAT, &c);
107  H5Aclose(attr);
108  H5Gclose(group);
109 
110  if (c == cutoff)
111  {
112  Log::log << Log::LogLevel::Warning << "Found existing checkpoint at cutoff " + std::to_string(cutoff) + ". Skipping checkpoint." << Log::endl;
113  H5Fclose(file);
114  return -1;
115  }
116  }
117  }
118  std::string checkpointName = "checkpoint_" + std::to_string(checkpointId);
119 
120  //create checkpoint group
121  hid_t group = H5Gcreate(file, checkpointName.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
122  hsize_t attrSpaceSize[1] = { 1 };
123  const int attrSpaceDim = 1;
124  hid_t attrSpace = H5Screate_simple(attrSpaceDim, attrSpaceSize, NULL);
125  hid_t attr = H5Acreate(group, "cutoff", H5T_NATIVE_FLOAT, attrSpace, H5P_DEFAULT, H5P_DEFAULT);
126  H5Awrite(attr, H5T_NATIVE_FLOAT, &cutoff);
127  H5Aclose(attr);
128  H5Sclose(attrSpace);
129 
130  //write vertex data
131  auto writeCheckpointDataset = [&group](const std::string &identifier, const int size, const float *data)
132  {
133  const int dataSpaceDim = 1;
134  const hsize_t dataSpaceSize[1] = { (hsize_t)size };
135  hid_t dataSpace = H5Screate_simple(dataSpaceDim, dataSpaceSize, NULL);
136  hid_t dataset = H5Dcreate(group, identifier.c_str(), H5T_NATIVE_FLOAT, dataSpace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
137  H5Dwrite(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
138  H5Dclose(dataset);
139  H5Sclose(dataSpace);
140  };
141  writeCheckpointDataset("cutoff", 1, &cutoff);
142  writeCheckpointDataset("v2", vertexSingleParticle->size, vertexSingleParticle->_data);
143  writeCheckpointDataset("v4dd", vertexTwoParticle->size, vertexTwoParticle->_dataDD);
144  writeCheckpointDataset("v4ss", vertexTwoParticle->size, vertexTwoParticle->_dataSS);
145 
146  //clean up and return
147  H5Gclose(group);
148  H5Fclose(file);
149  return checkpointId;
150  }
151 
159  bool readCheckpoint(const std::string &dataFilePath, const int checkpointId) override
160  {
161  H5Eset_auto(H5E_DEFAULT, NULL, NULL);
162 
163  //open file
164  hid_t file = H5Fopen(dataFilePath.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
165  if (file < 0) throw Exception(Exception::Type::IOError, "Could not open data file for reading");
166 
167  //find desired dataset name
168  std::string checkpointName;
169  if (checkpointId >= 0) checkpointName = "checkpoint_" + std::to_string(checkpointId);
170  else
171  {
172  hsize_t numObjects;
173  H5Gget_num_objs(file, &numObjects);
174  for (int i = int(numObjects) - 1; i >= 0; --i)
175  {
176  if (H5Gget_objtype_by_idx(file, i) == H5G_GROUP)
177  {
178  checkpointName = "checkpoint_" + std::to_string(i);
179  break;
180  }
181  }
182 
183  }
184  hid_t group = H5Gopen(file, checkpointName.c_str(), H5P_DEFAULT);
185  if (group < 0) return false;
186 
187  //read dataset
188  auto readDataset = [&group](const std::string &name, float *data)->bool
189  {
190  hid_t dataset = H5Dopen(group, name.c_str(), H5P_DEFAULT);
191  if (dataset < 0) return false;
192  H5Dread(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
193  H5Dclose(dataset);
194  return true;
195  };
196  if (!readDataset("cutoff", &cutoff)) return false;
197  if (!readDataset("v2", vertexSingleParticle->_data)) return false;
198  if (!readDataset("v4dd", vertexTwoParticle->_dataDD)) return false;
199  if (!readDataset("v4ss", vertexTwoParticle->_dataSS)) return false;
200 
201  //clean up and return
202  H5Gclose(group);
203  H5Fclose(file);
204  return true;
205  }
206 
212  bool isDiverged() const override
213  {
214  for (int i = 0; i < vertexSingleParticle->size; ++i)
215  {
216  if (std::isnan(vertexSingleParticle->getValueRef(i))) return true;
217  }
218 
219  for (int i = 0; i < vertexTwoParticle->size; ++i)
220  {
221  if (std::isnan(vertexTwoParticle->getValueRef(i, SU2VertexTwoParticle::Symmetry::Spin))) return true;
222  }
223 
224  for (int i = 0; i < vertexTwoParticle->size; ++i)
225  {
227  }
228 
229  return false;
230  }
231 
234 };
SU2VertexTwoParticle::Symmetry::Spin
@ Spin
Spin-type vertex.
SU2FrgCore::normalization
float normalization
Energy normalization factor.
Definition: SU2FrgCore.hpp:45
SU2VertexTwoParticle::getValueRef
float & getValueRef(const int iterator, const SU2VertexTwoParticle::Symmetry symmetry) const
Directly access a vertex value via a linear iterator in the range [0,size).
Definition: SU2VertexTwoParticle.hpp:167
SU2EffectiveAction::vertexSingleParticle
SU2VertexSingleParticle * vertexSingleParticle
Single-particle vertex data.
Definition: SU2EffectiveAction.hpp:232
SU2EffectiveAction::SU2EffectiveAction
SU2EffectiveAction(const float cutoff, const SpinModel &spinModel, const SU2FrgCore *core)
Construct a new effective action and initialize values at given cutoff for a given spin model.
Definition: SU2EffectiveAction.hpp:38
SU2FrgCore
FrgCore implementation for SU(2) models.
Definition: SU2FrgCore.hpp:15
Exception::Type::IOError
@ IOError
In/out error, raised when input or output routines fail.
SU2VertexTwoParticle.hpp
Two-particle vertex implementation for SU(2) models.
SU2VertexSingleParticle::_data
float * _data
Vertex data.
Definition: SU2VertexSingleParticle.hpp:103
Exception
Descriptor object for exceptions.
Definition: Exception.hpp:17
SU2VertexTwoParticle::size
int size
Size of the vertex per vertex channel (number of elements).
Definition: SU2VertexTwoParticle.hpp:634
SpinModel
Spin model representation.
Definition: SpinModel.hpp:19
EffectiveAction
Virtual implementation of a flowing effective action.
Definition: EffectiveAction.hpp:19
SU2VertexSingleParticle::size
int size
Total number of vertex elements.
Definition: SU2VertexSingleParticle.hpp:102
SU2EffectiveAction::readCheckpoint
bool readCheckpoint(const std::string &dataFilePath, const int checkpointId) override
Read checkpoint from file.
Definition: SU2EffectiveAction.hpp:159
SU2VertexSingleParticle::getValueRef
float & getValueRef(const int iterator) const
Directly access a vertex value by reference via a linear iterator in the range [0,...
Definition: SU2VertexSingleParticle.hpp:60
LatticeIterator
Lattice iterator object.
Definition: Lattice.hpp:166
SU2EffectiveAction::isDiverged
bool isDiverged() const override
Indicate whether the vertex has diverged to NaN.
Definition: SU2EffectiveAction.hpp:212
SU2VertexTwoParticle::_dataDD
float * _dataDD
Density channel of the vertex.
Definition: SU2VertexTwoParticle.hpp:638
SpinModel::interactions
std::vector< std::pair< LatticeIterator, SpinInteraction > > interactions
List of two-spin interactions in the model. All interactions are between the lattice site pair (s1,...
Definition: SpinModel.hpp:38
SU2VertexSingleParticle.hpp
Single-particle vertex implementation for SU(2) models.
EffectiveAction::cutoff
float cutoff
Value of the RG cutoff.
Definition: EffectiveAction.hpp:59
Exception.hpp
Descriptor object for exceptions.
SU2VertexTwoParticle::expandIterator
void expandIterator(int iterator, LatticeIterator &i1, float &s, float &t, float &u) const
Expand a linear iterator in the range [0,size) that iterates over all vertex entries.
Definition: SU2VertexTwoParticle.hpp:102
SU2VertexTwoParticle
Two-particle vertex implementation for SU(2) models.
Definition: SU2VertexTwoParticle.hpp:41
SU2EffectiveAction::SU2EffectiveAction
SU2EffectiveAction()
Construct a new SU2EffectiveAction object.
Definition: SU2EffectiveAction.hpp:25
SU2VertexSingleParticle
Single-particle vertex implementation for SU(2) models.
Definition: SU2VertexSingleParticle.hpp:17
EffectiveAction.hpp
Virtual implementation of a flowing effective action.
SU2EffectiveAction
Implementation of a flowing effective action for SU(2) models.
Definition: SU2EffectiveAction.hpp:19
SU2VertexTwoParticle::Symmetry::Density
@ Density
Density-type vertex.
SU2EffectiveAction::writeCheckpoint
int writeCheckpoint(const std::string &dataFilePath, const bool append=false) const override
Write checkpoint file.
Definition: SU2EffectiveAction.hpp:79
SU2EffectiveAction::~SU2EffectiveAction
~SU2EffectiveAction()
Destroy the SU2EffectiveAction object.
Definition: SU2EffectiveAction.hpp:65
SU2FrgCore.hpp
FrgCore implementation for SU(2) models.
SU2EffectiveAction::vertexTwoParticle
SU2VertexTwoParticle * vertexTwoParticle
Two-particle vertex data.
Definition: SU2EffectiveAction.hpp:233
SU2VertexTwoParticle::_dataSS
float * _dataSS
Spin channel of the vertex.
Definition: SU2VertexTwoParticle.hpp:637