16#include <boost/filesystem.hpp>
21 const h5_int64_t H5TypesCHAR = H5_STRING_T;
22 const h5_int64_t H5TypesFLOAT = H5_FLOAT32_T;
23 const h5_int64_t H5TypesDOUBLE = H5_FLOAT64_T;
24 const h5_int64_t H5TypesINT32 = H5_INT32_T;
25 const h5_int64_t H5TypesINT64 = H5_INT64_T;
33 predecessorOPALFlavour_m(
"NOT SET"),
35 startedFromExistingFile_m(false) {
40 const std::string& fileName,
int restartStep, std::string sourceFile, h5_int32_t flags)
43 predecessorOPALFlavour_m(
"NOT SET"),
45 startedFromExistingFile_m(true) {
46 if (sourceFile.empty()) {
50 copyFile(sourceFile, restartStep, flags);
70 h5_prop_t props = H5CreateFileProp();
72 h5_err_t h5err = H5SetPropFileMPIOCollective(props, &comm);
85 h5_int64_t nFormerlySavedAutoPhaseCavities = 0;
86 bool fileWasClosed = (
file_m == 0);
88 if (nAutoPhaseCavities == 0)
91 open(H5_O_APPENDONLY);
92 if (!H5HasFileAttrib(
file_m,
"nAutoPhaseCavities")
93 || H5ReadFileAttribInt64(
file_m,
"nAutoPhaseCavities", &nFormerlySavedAutoPhaseCavities)
95 nFormerlySavedAutoPhaseCavities = 0;
97 if (nFormerlySavedAutoPhaseCavities == nAutoPhaseCavities) {
105 unsigned int elementNumber = 1;
108 for (; it <
end; ++it, ++elementNumber) {
109 if (elementNumber <= nFormerlySavedAutoPhaseCavities)
112 std::string nameAttributeName =
"Cav-" + std::to_string(elementNumber) +
"-name";
113 std::string valueAttributeName =
"Cav-" + std::to_string(elementNumber) +
"-value";
115 std::string elementName = (*it).first;
116 h5_float64_t elementPhase = (*it).second;
121 *
ippl::Info <<
"Saved phases in the h5 file: " << nameAttributeName <<
" -> " << elementName
122 <<
" --- " << valueAttributeName <<
" -> " << elementPhase <<
endl;
130 namespace fs = boost::filesystem;
131 if (!fs::exists(sourceFile)) {
133 "H5PartWrapper::copyFile",
"source file '" + sourceFile +
"' does not exist");
137 h5_prop_t props = H5CreateFileProp();
138 MPI_Comm comm =
ippl::Comm->getCommunicator();
139 h5_err_t h5err = H5SetPropFileMPIOCollective(props, &comm);
144 h5_file_t source = H5OpenFile(sourceFile.c_str(), H5_O_RDONLY, props);
145 PAssert(source != (h5_file_t)H5_ERR);
147 h5_ssize_t numStepsInSource = H5GetNumSteps(source);
149 if (lastStep == -1 || lastStep >= numStepsInSource) {
152 char opalFlavour[128];
174 props = H5CreateFileProp();
176 h5err = H5SetPropFileMPIOCollective(props, &comm);
178 source = H5OpenFile(sourceFileName.c_str(), H5_O_RDONLY, props);
179 PAssert(source != (h5_file_t)H5_ERR);
184 if (-lastStep > numStepsInSource) {
187 lastStep = numStepsInSource + lastStep;
198 fs::remove(sourceFileName);
205 h5_prop_t props = H5CreateFileProp();
206 MPI_Comm comm =
ippl::Comm->getCommunicator();
207 h5_err_t h5err = H5SetPropFileMPIOCollective(props, &comm);
212 h5_file_t source = H5OpenFile(sourceFile.c_str(), H5_O_RDONLY, props);
213 PAssert(source != (h5_file_t)H5_ERR);
215 h5_ssize_t numStepsInSource = H5GetNumSteps(source);
217 if (lastStep == -1 || lastStep >= numStepsInSource) {
220 char opalFlavour[128];
235 if (-lastStep > numStepsInSource) {
238 lastStep = numStepsInSource + lastStep;
268 std::ifstream source(sourceFile, std::ios::binary);
269 std::ofstream dest(
fileName_m, std::ios::binary);
271 std::istreambuf_iterator<char> begin_source(source);
272 std::istreambuf_iterator<char> end_source;
273 std::ostreambuf_iterator<char> begin_dest(dest);
274 std::copy(begin_source, end_source, begin_dest);
279 dest.bad(),
"H5PartWrapper::copyFile",
"could not copy file " + sourceFile);
283 sourceNode,
"H5PartWrapper::copyFile",
284 "received message to throw exception from node 0");
289 h5_int64_t numFileAttributes = H5GetNumFileAttribs(source);
291 const h5_size_t lengthAttributeName = 256;
292 char attributeName[lengthAttributeName];
293 h5_int64_t attributeType;
294 h5_size_t numAttributeElements;
295 std::vector<char> buffer(256);
296 h5_float32_t* f32buffer =
reinterpret_cast<h5_float32_t*
>(&buffer[0]);
297 h5_float64_t* f64buffer =
reinterpret_cast<h5_float64_t*
>(&buffer[0]);
298 h5_int32_t* i32buffer =
reinterpret_cast<h5_int32_t*
>(&buffer[0]);
299 h5_int64_t* i64buffer =
reinterpret_cast<h5_int64_t*
>(&buffer[0]);
301 for (h5_int64_t i = 0; i < numFileAttributes; ++i) {
303 source, i, attributeName, lengthAttributeName, &attributeType, &numAttributeElements));
305 if (attributeType == H5_STRING_T) {
306 if (buffer.size() < numAttributeElements) {
307 buffer.resize(numAttributeElements);
313 }
else if (attributeType == H5_FLOAT32_T) {
314 if (buffer.size() < numAttributeElements *
sizeof(h5_float32_t)) {
315 buffer.resize(numAttributeElements *
sizeof(h5_float32_t));
321 }
else if (attributeType == H5_FLOAT64_T) {
322 if (buffer.size() < numAttributeElements *
sizeof(h5_float64_t)) {
323 buffer.resize(numAttributeElements *
sizeof(h5_float64_t));
329 }
else if (attributeType == H5_INT32_T) {
330 if (buffer.size() < numAttributeElements *
sizeof(h5_int32_t)) {
331 buffer.resize(numAttributeElements *
sizeof(h5_int32_t));
337 }
else if (attributeType == H5_INT64_T) {
338 if (buffer.size() < numAttributeElements *
sizeof(h5_int64_t)) {
339 buffer.resize(numAttributeElements *
sizeof(h5_int64_t));
347 "H5PartWrapper::copyHeader",
"unknown data type: " + std::to_string(attributeType));
361 h5_int64_t numStepAttributes = H5GetNumStepAttribs(source);
363 h5_size_t lengthAttributeName = 256;
364 char attributeName[lengthAttributeName];
365 h5_int64_t attributeType;
366 h5_size_t numAttributeElements;
368 std::vector<char> buffer(256);
369 h5_float32_t* f32buffer =
reinterpret_cast<h5_float32_t*
>(&buffer[0]);
370 h5_float64_t* f64buffer =
reinterpret_cast<h5_float64_t*
>(&buffer[0]);
371 h5_int32_t* i32buffer =
reinterpret_cast<h5_int32_t*
>(&buffer[0]);
372 h5_int64_t* i64buffer =
reinterpret_cast<h5_int64_t*
>(&buffer[0]);
377 for (h5_int64_t i = 0; i < numStepAttributes; ++i) {
379 source, i, attributeName, lengthAttributeName, &attributeType, &numAttributeElements));
381 if (attributeType == H5TypesCHAR) {
382 if (buffer.size() < numAttributeElements) {
383 buffer.resize(numAttributeElements);
389 }
else if (attributeType == H5TypesFLOAT) {
390 if (buffer.size() < numAttributeElements *
sizeof(h5_float32_t)) {
391 buffer.resize(numAttributeElements *
sizeof(h5_float32_t));
397 }
else if (attributeType == H5TypesDOUBLE) {
398 if (buffer.size() < numAttributeElements *
sizeof(h5_float64_t)) {
399 buffer.resize(numAttributeElements *
sizeof(h5_float64_t));
405 }
else if (attributeType == H5TypesINT32) {
406 if (buffer.size() < numAttributeElements *
sizeof(h5_int32_t)) {
407 buffer.resize(numAttributeElements *
sizeof(h5_int32_t));
413 }
else if (attributeType == H5TypesINT64) {
414 if (buffer.size() < numAttributeElements *
sizeof(h5_int64_t)) {
415 buffer.resize(numAttributeElements *
sizeof(h5_int64_t));
423 "H5PartWrapper::copyStepHeader",
424 "unknown data type: " + std::to_string(attributeType));
430 h5_size_t lengthSetName = 256;
431 char setName[lengthSetName];
433 h5_size_t numSetElements;
435 h5_ssize_t numParticles = H5PartGetNumParticles(source);
436 h5_ssize_t numParticlesPerNode = numParticles /
ippl::Comm->size();
438 h5_ssize_t firstParticle = numParticlesPerNode *
ippl::Comm->rank();
439 h5_ssize_t lastParticle = firstParticle + numParticlesPerNode - 1;
441 lastParticle = numParticles - 1;
443 REPORTONERROR(H5PartSetView(source, firstParticle, lastParticle));
445 numParticles = lastParticle - firstParticle + 1;
448 std::vector<char> buffer(numParticles *
sizeof(h5_float64_t));
450 h5_float32_t* f32buffer =
reinterpret_cast<h5_float32_t*
>(buffer_ptr);
451 h5_float64_t* f64buffer =
reinterpret_cast<h5_float64_t*
>(buffer_ptr);
452 h5_int32_t* i32buffer =
reinterpret_cast<h5_int32_t*
>(buffer_ptr);
453 h5_int64_t* i64buffer =
reinterpret_cast<h5_int64_t*
>(buffer_ptr);
455 h5_ssize_t numDatasets = H5PartGetNumDatasets(source);
457 for (h5_ssize_t i = 0; i < numDatasets; ++i) {
459 H5PartGetDatasetInfo(source, i, setName, lengthSetName, &setType, &numSetElements));
461 if (setType == H5TypesFLOAT) {
462 READDATA(Float32, source, setName, f32buffer);
464 }
else if (setType == H5TypesDOUBLE) {
465 READDATA(Float64, source, setName, f64buffer);
467 }
else if (setType == H5TypesINT32) {
468 READDATA(Int32, source, setName, i32buffer);
470 }
else if (setType == H5TypesINT64) {
471 READDATA(Int64, source, setName, i64buffer);
475 "H5PartWrapper::copyStepData",
"unknown data type: " + std::to_string(setType));
479 numParticles = H5PartGetNumParticles(
file_m);
484 bool failed,
const std::string& where,
const std::string& what) {
496 int sourceNode,
const std::string& where,
const std::string& what) {
510 throw OpalException(
"H5PartWrapper::getNumParticles",
"no file opened");
514 h5_ssize_t numParticles = H5PartGetNumParticles(
file_m);
PartBunch< T, Dim >::ConstIterator end(PartBunch< T, Dim > const &bunch)
#define WRITESTRINGFILEATTRIB(file, name, value)
#define WRITESTEPATTRIB(type, file, name, value, length)
#define REPORTONERROR(rc)
#define READSTEPATTRIB(type, file, name, value)
#define WRITEDATA(type, file, name, value)
#define WRITEFILEATTRIB(type, file, name, value, length)
#define WRITESTRINGSTEPATTRIB(file, name, value)
#define READDATA(type, file, name, value)
#define READFILEATTRIB(type, file, name, value)
Inform & endl(Inform &inf)
std::string::iterator iterator
T * c_data(std::vector< T, A > &v)
std::unique_ptr< Inform > Info
std::unique_ptr< mpi::Communicator > Comm
int getNumberOfMaxPhases()
std::vector< MaxPhasesT >::iterator getLastMaxPhases()
std::vector< MaxPhasesT >::iterator getFirstMaxPhases()
static OpalData * getInstance()
size_t getNumParticles() const
void receiveFailureMessage(int sourceNode, const std::string &where, const std::string &what)
void copyStepData(h5_file_t source)
void copyFile(const std::string &sourceFile, int lastStep=-1, h5_int32_t flags=H5_O_WRONLY)
void open(h5_int32_t flags)
void copyFileSystem(const std::string &sourceFile)
H5PartWrapper(const std::string &fileName, h5_int32_t flags=H5_O_WRONLY)
void copyHeader(h5_file_t source)
std::string predecessorOPALFlavour_m
void storeCavityInformation()
void copyStepHeader(h5_file_t source)
void copyStep(h5_file_t source, int step)
static std::string copyFilePrefix_m
void sendFailureMessage(bool failed, const std::string &where, const std::string &what)
The base class for all OPAL exceptions.