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