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