12 #include <boost/filesystem.hpp>
17 const h5_int64_t H5TypesCHAR = H5_STRING_T;
18 const h5_int64_t H5TypesFLOAT = H5_FLOAT32_T;
19 const h5_int64_t H5TypesDOUBLE = H5_FLOAT64_T;
20 const h5_int64_t H5TypesINT32 = H5_INT32_T;
21 const h5_int64_t H5TypesINT64 = H5_INT64_T;
29 predecessorOPALFlavour_m(
"NOT SET"),
31 startedFromExistingFile_m(false)
39 predecessorOPALFlavour_m(
"NOT SET"),
41 startedFromExistingFile_m(true)
43 namespace fs = boost::filesystem;
47 copyFile(sourceFile, restartStep, flags);
69 h5_prop_t props = H5CreateFileProp ();
71 h5_err_t h5err = H5SetPropFileMPIOCollective (props, &comm);
75 assert (h5err != H5_ERR);
77 assert (
file_m != (h5_file_t)H5_ERR);
84 h5_int64_t nFormerlySavedAutoPhaseCavities = 0;
85 bool fileWasClosed = (
file_m == 0);
87 if (nAutoPhaseCavities == 0)
return;
88 if (fileWasClosed)
open(H5_O_APPENDONLY);
89 if (!H5HasFileAttrib(
file_m,
"nAutoPhaseCavities") ||
90 H5ReadFileAttribInt64(
file_m,
"nAutoPhaseCavities", &nFormerlySavedAutoPhaseCavities) != H5_SUCCESS) {
91 nFormerlySavedAutoPhaseCavities = 0;
93 if (nFormerlySavedAutoPhaseCavities == nAutoPhaseCavities) {
94 if (fileWasClosed)
close();
100 unsigned int elementNumber = 1;
103 for(; it < end; ++ it, ++ elementNumber) {
104 if (elementNumber <= nFormerlySavedAutoPhaseCavities)
continue;
106 std::string nameAttributeName =
"Cav-" + std::to_string(elementNumber) +
"-name";
107 std::string valueAttributeName =
"Cav-" + std::to_string(elementNumber) +
"-value";
109 std::string elementName = (*it).first;
110 h5_float64_t elementPhase = (*it).second;
115 INFOMSG(
"Saved phases in the h5 file: "
116 << nameAttributeName <<
" -> " << elementName <<
" --- "
117 << valueAttributeName <<
" -> " << elementPhase <<
endl);
120 if (fileWasClosed)
close();
125 namespace fs = boost::filesystem;
126 if (!fs::exists(sourceFile)) {
128 "source file '" + sourceFile +
"' does not exist");
132 h5_prop_t props = H5CreateFileProp ();
134 h5_err_t h5err = H5SetPropFileMPIOCollective (props, &comm);
138 assert (h5err != H5_ERR);
139 h5_file_t source = H5OpenFile (sourceFile.c_str(), H5_O_RDONLY, props);
140 assert (source != (h5_file_t)H5_ERR);
142 h5_ssize_t numStepsInSource = H5GetNumSteps(source);
144 if (lastStep == -1 || lastStep >= numStepsInSource) {
147 char opalFlavour[128];
163 fs::rename(fileName_m, sourceFileName);
169 props = H5CreateFileProp ();
171 h5err = H5SetPropFileMPIOCollective (props, &comm);
172 assert (h5err != H5_ERR);
173 source = H5OpenFile (sourceFileName.c_str(), H5_O_RDONLY, props);
174 assert (source != (h5_file_t)H5_ERR);
179 if (-lastStep > numStepsInSource) {
182 lastStep = numStepsInSource + lastStep;
193 fs::remove(sourceFileName);
201 h5_prop_t props = H5CreateFileProp ();
203 h5_err_t h5err = H5SetPropFileMPIOCollective (props, &comm);
207 assert (h5err != H5_ERR);
208 h5_file_t source = H5OpenFile (sourceFile.c_str(), H5_O_RDONLY, props);
209 assert (source != (h5_file_t)H5_ERR);
211 h5_ssize_t numStepsInSource = H5GetNumSteps(source);
213 if (lastStep == -1 || lastStep >= numStepsInSource) {
216 char opalFlavour[128];
231 if (-lastStep > numStepsInSource) {
234 lastStep = numStepsInSource + lastStep;
264 std::ifstream source(sourceFile, std::ios::binary);
265 std::ofstream dest(
fileName_m, std::ios::binary);
267 std::istreambuf_iterator<char> begin_source(source);
268 std::istreambuf_iterator<char> end_source;
269 std::ostreambuf_iterator<char> begin_dest(dest);
270 std::copy(begin_source, end_source, begin_dest);
275 "H5PartWrapper::copyFile",
276 "could not copy file " + sourceFile);
280 "H5PartWrapper::copyFile",
281 "received message to throw exception from node 0");
288 h5_int64_t numFileAttributes = H5GetNumFileAttribs(source);
290 const h5_size_t lengthAttributeName = 256;
291 char attributeName[lengthAttributeName];
292 h5_int64_t attributeType;
293 h5_size_t numAttributeElements;
294 std::vector<char> buffer(256);
295 h5_float32_t *f32buffer =
reinterpret_cast<h5_float32_t*
>(&buffer[0]);
296 h5_float64_t *f64buffer =
reinterpret_cast<h5_float64_t*
>(&buffer[0]);
297 h5_int32_t *i32buffer =
reinterpret_cast<h5_int32_t*
>(&buffer[0]);
298 h5_int64_t *i64buffer =
reinterpret_cast<h5_int64_t*
>(&buffer[0]);
300 for (h5_int64_t i = 0; i < numFileAttributes; ++ i) {
306 &numAttributeElements));
308 if (attributeType == H5_STRING_T) {
309 if (buffer.size() < numAttributeElements) {
310 buffer.resize(numAttributeElements);
316 }
else if (attributeType == H5_FLOAT32_T) {
317 if (buffer.size() < numAttributeElements *
sizeof(h5_float32_t)) {
318 buffer.resize(numAttributeElements *
sizeof(h5_float32_t));
324 }
else if (attributeType == H5_FLOAT64_T) {
325 if (buffer.size() < numAttributeElements *
sizeof(h5_float64_t)) {
326 buffer.resize(numAttributeElements *
sizeof(h5_float64_t));
332 }
else if (attributeType == H5_INT32_T) {
333 if (buffer.size() < numAttributeElements *
sizeof(h5_int32_t)) {
334 buffer.resize(numAttributeElements *
sizeof(h5_int32_t));
340 }
else if (attributeType == H5_INT64_T) {
341 if (buffer.size() < numAttributeElements *
sizeof(h5_int64_t)) {
342 buffer.resize(numAttributeElements *
sizeof(h5_int64_t));
350 "unknown data type: " + std::to_string(attributeType));
369 h5_int64_t numStepAttributes = H5GetNumStepAttribs(source);
371 h5_size_t lengthAttributeName = 256;
372 char attributeName[lengthAttributeName];
373 h5_int64_t attributeType;
374 h5_size_t numAttributeElements;
376 std::vector<char> buffer(256);
377 h5_float32_t *f32buffer =
reinterpret_cast<h5_float32_t*
>(&buffer[0]);
378 h5_float64_t *f64buffer =
reinterpret_cast<h5_float64_t*
>(&buffer[0]);
379 h5_int32_t *i32buffer =
reinterpret_cast<h5_int32_t*
>(&buffer[0]);
380 h5_int64_t *i64buffer =
reinterpret_cast<h5_int64_t*
>(&buffer[0]);
385 for (h5_int64_t i = 0; i < numStepAttributes; ++ i) {
391 &numAttributeElements));
393 if (attributeType == H5TypesCHAR) {
394 if (buffer.size() < numAttributeElements) {
395 buffer.resize(numAttributeElements);
401 }
else if (attributeType == H5TypesFLOAT) {
402 if (buffer.size() < numAttributeElements *
sizeof(h5_float32_t)) {
403 buffer.resize(numAttributeElements *
sizeof(h5_float32_t));
409 }
else if (attributeType == H5TypesDOUBLE) {
410 if (buffer.size() < numAttributeElements *
sizeof(h5_float64_t)) {
411 buffer.resize(numAttributeElements *
sizeof(h5_float64_t));
417 }
else if (attributeType == H5TypesINT32) {
418 if (buffer.size() < numAttributeElements *
sizeof(h5_int32_t)) {
419 buffer.resize(numAttributeElements *
sizeof(h5_int32_t));
425 }
else if (attributeType == H5TypesINT64) {
426 if (buffer.size() < numAttributeElements *
sizeof(h5_int64_t)) {
427 buffer.resize(numAttributeElements *
sizeof(h5_int64_t));
435 "unknown data type: " + std::to_string(attributeType));
443 h5_size_t lengthSetName = 256;
444 char setName[lengthSetName];
446 h5_size_t numSetElements;
448 h5_ssize_t numParticles = H5PartGetNumParticles(source);
451 h5_ssize_t firstParticle = numParticlesPerNode *
Ippl::myNode();
452 h5_ssize_t lastParticle = firstParticle + numParticlesPerNode - 1;
454 lastParticle = numParticles - 1;
456 REPORTONERROR(H5PartSetView(source, firstParticle, lastParticle));
458 numParticles = lastParticle - firstParticle + 1;
461 std::vector<char> buffer(numParticles *
sizeof(h5_float64_t));
462 h5_float32_t *f32buffer =
reinterpret_cast<h5_float32_t*
>(&buffer[0]);
463 h5_float64_t *f64buffer =
reinterpret_cast<h5_float64_t*
>(&buffer[0]);
464 h5_int32_t *i32buffer =
reinterpret_cast<h5_int32_t*
>(&buffer[0]);
465 h5_int64_t *i64buffer =
reinterpret_cast<h5_int64_t*
>(&buffer[0]);
467 h5_ssize_t numDatasets = H5PartGetNumDatasets(source);
469 for (h5_ssize_t i = 0; i < numDatasets; ++ i) {
470 REPORTONERROR(H5PartGetDatasetInfo(source, i, setName, lengthSetName, &setType, &numSetElements));
472 if (setType == H5TypesFLOAT) {
473 READDATA(Float32, source, setName, f32buffer);
475 }
else if (setType == H5TypesDOUBLE) {
476 READDATA(Float64, source, setName, f64buffer);
478 }
else if (setType == H5TypesINT32) {
479 READDATA(Int32, source, setName, i32buffer);
481 }
else if (setType == H5TypesINT64) {
482 READDATA(Int64, source, setName, i64buffer);
486 "unknown data type: " + std::to_string(setType));
490 numParticles = H5PartGetNumParticles(
file_m);
495 const std::string &
where,
496 const std::string &what) {
507 const std::string &
where,
508 const std::string &what) {
525 h5_ssize_t numParticles = H5PartGetNumParticles(
file_m);
std::vector< MaxPhasesT >::iterator getLastMaxPhases()
void open(h5_int32_t flags)
void sendFailureMessage(bool failed, const std::string &where, const std::string &what)
The base class for all OPAL exceptions.
void copyHeader(h5_file_t source)
void copyStep(h5_file_t source, int step)
#define WRITEDATA(type, file, name, value)
std::string predecessorOPALFlavour_m
#define REPORTONERROR(rc)
#define WRITESTRINGSTEPATTRIB(file, name, value)
void copyFileSystem(const std::string &sourceFile)
static OpalData * getInstance()
static std::string copyFilePrefix_m
PETE_TTTree< OpWhere, typename Cond_t::PETE_Expr_t, typename True_t::PETE_Expr_t, PETE_Scalar< Vektor< T, Dim > > > where(const PETE_Expr< Cond_t > &c, const PETE_Expr< True_t > &t, const Vektor< T, Dim > &f)
void copyStepHeader(h5_file_t source)
static MPI_Comm getComm()
#define WRITESTEPATTRIB(type, file, name, value, length)
void storeCavityInformation()
std::vector< MaxPhasesT >::iterator getFirstMaxPhases()
int getNumberOfMaxPhases()
#define READSTEPATTRIB(type, file, name, value)
void getMessage(Message &m, T &t)
#define WRITEFILEATTRIB(type, file, name, value, length)
#define READDATA(type, file, name, value)
std::string::iterator iterator
void putMessage(Message &m, const T &t)
void copyFile(const std::string &sourceFile, int lastStep=-1, h5_int32_t flags=H5_O_WRONLY)
void copyStepData(h5_file_t source)
H5PartWrapper(const std::string &fileName, h5_int32_t flags=H5_O_WRONLY)
Message * receive_block(int &node, int &tag)
void receiveFailureMessage(int sourceNode, const std::string &where, const std::string &what)
size_t getNumParticles() const
static Communicate * Comm
virtual int broadcast_all(Message *, int)
#define READFILEATTRIB(type, file, name, value)
Inform & endl(Inform &inf)
#define WRITESTRINGFILEATTRIB(file, name, value)