18 #include <boost/filesystem.hpp>
23 const h5_int64_t H5TypesCHAR = H5_STRING_T;
24 const h5_int64_t H5TypesFLOAT = H5_FLOAT32_T;
25 const h5_int64_t H5TypesDOUBLE = H5_FLOAT64_T;
26 const h5_int64_t H5TypesINT32 = H5_INT32_T;
27 const h5_int64_t H5TypesINT64 = H5_INT64_T;
35 predecessorOPALFlavour_m(
"NOT SET"),
37 startedFromExistingFile_m(false)
45 predecessorOPALFlavour_m(
"NOT SET"),
47 startedFromExistingFile_m(true)
51 copyFile(sourceFile, restartStep, flags);
73 h5_prop_t props = H5CreateFileProp ();
75 h5_err_t h5err = H5SetPropFileMPIOCollective (props, &comm);
88 h5_int64_t nFormerlySavedAutoPhaseCavities = 0;
89 bool fileWasClosed = (
file_m == 0);
91 if (nAutoPhaseCavities == 0)
return;
92 if (fileWasClosed)
open(H5_O_APPENDONLY);
93 if (!H5HasFileAttrib(
file_m,
"nAutoPhaseCavities") ||
94 H5ReadFileAttribInt64(
file_m,
"nAutoPhaseCavities", &nFormerlySavedAutoPhaseCavities) != H5_SUCCESS) {
95 nFormerlySavedAutoPhaseCavities = 0;
97 if (nFormerlySavedAutoPhaseCavities == nAutoPhaseCavities) {
98 if (fileWasClosed)
close();
104 unsigned int elementNumber = 1;
107 for(; it <
end; ++ it, ++ elementNumber) {
108 if (elementNumber <= nFormerlySavedAutoPhaseCavities)
continue;
110 std::string nameAttributeName =
"Cav-" + std::to_string(elementNumber) +
"-name";
111 std::string valueAttributeName =
"Cav-" + std::to_string(elementNumber) +
"-value";
113 std::string elementName = (*it).first;
114 h5_float64_t elementPhase = (*it).second;
119 INFOMSG(
"Saved phases in the h5 file: "
120 << nameAttributeName <<
" -> " << elementName <<
" --- "
121 << valueAttributeName <<
" -> " << elementPhase <<
endl);
124 if (fileWasClosed)
close();
129 namespace fs = boost::filesystem;
130 if (!fs::exists(sourceFile)) {
132 "source file '" + sourceFile +
"' does not exist");
136 h5_prop_t props = H5CreateFileProp ();
138 h5_err_t h5err = H5SetPropFileMPIOCollective (props, &comm);
143 h5_file_t source = H5OpenFile (sourceFile.c_str(), H5_O_RDONLY, props);
144 PAssert (source != (h5_file_t)H5_ERR);
146 h5_ssize_t numStepsInSource = H5GetNumSteps(source);
148 if (lastStep == -1 || lastStep >= numStepsInSource) {
151 char opalFlavour[128];
173 props = H5CreateFileProp ();
175 h5err = H5SetPropFileMPIOCollective (props, &comm);
177 source = H5OpenFile (sourceFileName.c_str(), H5_O_RDONLY, props);
178 PAssert (source != (h5_file_t)H5_ERR);
183 if (-lastStep > numStepsInSource) {
186 lastStep = numStepsInSource + lastStep;
197 fs::remove(sourceFileName);
205 h5_prop_t props = H5CreateFileProp ();
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 "H5PartWrapper::copyFile",
280 "could not copy file " + sourceFile);
284 "H5PartWrapper::copyFile",
285 "received message to throw exception from node 0");
292 h5_int64_t numFileAttributes = H5GetNumFileAttribs(source);
294 const h5_size_t lengthAttributeName = 256;
295 char attributeName[lengthAttributeName];
296 h5_int64_t attributeType;
297 h5_size_t numAttributeElements;
298 std::vector<char> buffer(256);
299 h5_float32_t *f32buffer =
reinterpret_cast<h5_float32_t*
>(&buffer[0]);
300 h5_float64_t *f64buffer =
reinterpret_cast<h5_float64_t*
>(&buffer[0]);
301 h5_int32_t *i32buffer =
reinterpret_cast<h5_int32_t*
>(&buffer[0]);
302 h5_int64_t *i64buffer =
reinterpret_cast<h5_int64_t*
>(&buffer[0]);
304 for (h5_int64_t i = 0; i < numFileAttributes; ++ i) {
310 &numAttributeElements));
312 if (attributeType == H5_STRING_T) {
313 if (buffer.size() < numAttributeElements) {
314 buffer.resize(numAttributeElements);
320 }
else if (attributeType == H5_FLOAT32_T) {
321 if (buffer.size() < numAttributeElements *
sizeof(h5_float32_t)) {
322 buffer.resize(numAttributeElements *
sizeof(h5_float32_t));
328 }
else if (attributeType == H5_FLOAT64_T) {
329 if (buffer.size() < numAttributeElements *
sizeof(h5_float64_t)) {
330 buffer.resize(numAttributeElements *
sizeof(h5_float64_t));
336 }
else if (attributeType == H5_INT32_T) {
337 if (buffer.size() < numAttributeElements *
sizeof(h5_int32_t)) {
338 buffer.resize(numAttributeElements *
sizeof(h5_int32_t));
344 }
else if (attributeType == H5_INT64_T) {
345 if (buffer.size() < numAttributeElements *
sizeof(h5_int64_t)) {
346 buffer.resize(numAttributeElements *
sizeof(h5_int64_t));
354 "unknown data type: " + std::to_string(attributeType));
373 h5_int64_t numStepAttributes = H5GetNumStepAttribs(source);
375 h5_size_t lengthAttributeName = 256;
376 char attributeName[lengthAttributeName];
377 h5_int64_t attributeType;
378 h5_size_t numAttributeElements;
380 std::vector<char> buffer(256);
381 h5_float32_t *f32buffer =
reinterpret_cast<h5_float32_t*
>(&buffer[0]);
382 h5_float64_t *f64buffer =
reinterpret_cast<h5_float64_t*
>(&buffer[0]);
383 h5_int32_t *i32buffer =
reinterpret_cast<h5_int32_t*
>(&buffer[0]);
384 h5_int64_t *i64buffer =
reinterpret_cast<h5_int64_t*
>(&buffer[0]);
389 for (h5_int64_t i = 0; i < numStepAttributes; ++ i) {
395 &numAttributeElements));
397 if (attributeType == H5TypesCHAR) {
398 if (buffer.size() < numAttributeElements) {
399 buffer.resize(numAttributeElements);
405 }
else if (attributeType == H5TypesFLOAT) {
406 if (buffer.size() < numAttributeElements *
sizeof(h5_float32_t)) {
407 buffer.resize(numAttributeElements *
sizeof(h5_float32_t));
413 }
else if (attributeType == H5TypesDOUBLE) {
414 if (buffer.size() < numAttributeElements *
sizeof(h5_float64_t)) {
415 buffer.resize(numAttributeElements *
sizeof(h5_float64_t));
421 }
else if (attributeType == H5TypesINT32) {
422 if (buffer.size() < numAttributeElements *
sizeof(h5_int32_t)) {
423 buffer.resize(numAttributeElements *
sizeof(h5_int32_t));
429 }
else if (attributeType == H5TypesINT64) {
430 if (buffer.size() < numAttributeElements *
sizeof(h5_int64_t)) {
431 buffer.resize(numAttributeElements *
sizeof(h5_int64_t));
439 "unknown data type: " + std::to_string(attributeType));
447 h5_size_t lengthSetName = 256;
448 char setName[lengthSetName];
450 h5_size_t numSetElements;
452 h5_ssize_t numParticles = H5PartGetNumParticles(source);
455 h5_ssize_t firstParticle = numParticlesPerNode *
Ippl::myNode();
456 h5_ssize_t lastParticle = firstParticle + numParticlesPerNode - 1;
458 lastParticle = numParticles - 1;
460 REPORTONERROR(H5PartSetView(source, firstParticle, lastParticle));
462 numParticles = lastParticle - firstParticle + 1;
465 std::vector<char> buffer(numParticles *
sizeof(h5_float64_t));
467 h5_float32_t *f32buffer =
reinterpret_cast<h5_float32_t*
>(buffer_ptr);
468 h5_float64_t *f64buffer =
reinterpret_cast<h5_float64_t*
>(buffer_ptr);
469 h5_int32_t *i32buffer =
reinterpret_cast<h5_int32_t*
>(buffer_ptr);
470 h5_int64_t *i64buffer =
reinterpret_cast<h5_int64_t*
>(buffer_ptr);
472 h5_ssize_t numDatasets = H5PartGetNumDatasets(source);
474 for (h5_ssize_t i = 0; i < numDatasets; ++ i) {
475 REPORTONERROR(H5PartGetDatasetInfo(source, i, setName, lengthSetName, &setType, &numSetElements));
477 if (setType == H5TypesFLOAT) {
478 READDATA(Float32, source, setName, f32buffer);
480 }
else if (setType == H5TypesDOUBLE) {
481 READDATA(Float64, source, setName, f64buffer);
483 }
else if (setType == H5TypesINT32) {
484 READDATA(Int32, source, setName, i32buffer);
486 }
else if (setType == H5TypesINT64) {
487 READDATA(Int64, source, setName, i64buffer);
491 "unknown data type: " + std::to_string(setType));
495 numParticles = H5PartGetNumParticles(
file_m);
500 const std::string &
where,
501 const std::string &what) {
512 const std::string &
where,
513 const std::string &what) {
530 h5_ssize_t numParticles = H5PartGetNumParticles(
file_m);
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< 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)
void putMessage(Message &m, const T &t)
void getMessage(Message &m, T &t)
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)
Inform & endl(Inform &inf)
std::string::iterator iterator
T * c_data(std::vector< T, A > &v)
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.
Message * receive_block(int &node, int &tag)
virtual int broadcast_all(Message *, int)
static MPI_Comm getComm()
static Communicate * Comm