82 h5_int64_t nFormerlySavedAutoPhaseCavities = 0;
83 bool fileWasClosed = (
file_m == 0);
85 if (nAutoPhaseCavities == 0)
return;
86 if (fileWasClosed)
open(H5_O_APPENDONLY);
87 if (!H5HasFileAttrib(
file_m,
"nAutoPhaseCavities")
88 || H5ReadFileAttribInt64(
file_m,
"nAutoPhaseCavities", &nFormerlySavedAutoPhaseCavities)
90 nFormerlySavedAutoPhaseCavities = 0;
92 if (nFormerlySavedAutoPhaseCavities == nAutoPhaseCavities) {
93 if (fileWasClosed)
close();
99 unsigned int elementNumber = 1;
102 for (; it < end; ++it, ++elementNumber) {
103 if (elementNumber <= nFormerlySavedAutoPhaseCavities)
continue;
105 std::string nameAttributeName =
"Cav-" + std::to_string(elementNumber) +
"-name";
106 std::string valueAttributeName =
"Cav-" + std::to_string(elementNumber) +
"-value";
108 std::string elementName = (*it).first;
109 h5_float64_t elementPhase = (*it).second;
114 *ippl::Info <<
"Saved phases in the h5 file: " << nameAttributeName <<
" -> " << elementName
115 <<
" --- " << valueAttributeName <<
" -> " << elementPhase << endl;
118 if (fileWasClosed)
close();
122 namespace fs = std::filesystem;
123 if (!fs::exists(sourceFile)) {
125 "H5PartWrapper::copyFile",
"source file '" + sourceFile +
"' does not exist");
129 h5_prop_t props = H5CreateFileProp();
130 MPI_Comm comm = ippl::Comm->getCommunicator();
131 h5_err_t h5err = H5SetPropFileMPIOCollective(props, &comm);
135 PAssert(h5err != H5_ERR);
136 h5_file_t source = H5OpenFile(sourceFile.c_str(), H5_O_RDONLY, props);
137 PAssert(source != (h5_file_t)H5_ERR);
139 h5_ssize_t numStepsInSource = H5GetNumSteps(source);
141 if (lastStep == -1 || lastStep >= numStepsInSource) {
144 char opalFlavour[128];
156 ippl::Comm->barrier();
159 if (ippl::Comm->rank() == 0) {
163 ippl::Comm->barrier();
166 props = H5CreateFileProp();
167 comm = ippl::Comm->getCommunicator();
168 h5err = H5SetPropFileMPIOCollective(props, &comm);
169 PAssert(h5err != H5_ERR);
170 source = H5OpenFile(sourceFileName.c_str(), H5_O_RDONLY, props);
171 PAssert(source != (h5_file_t)H5_ERR);
176 if (-lastStep > numStepsInSource) {
179 lastStep = numStepsInSource + lastStep;
189 if (ippl::Comm->rank() == 0) {
190 fs::remove(sourceFileName);
197 h5_prop_t props = H5CreateFileProp();
198 MPI_Comm comm = ippl::Comm->getCommunicator();
199 h5_err_t h5err = H5SetPropFileMPIOCollective(props, &comm);
203 PAssert(h5err != H5_ERR);
204 h5_file_t source = H5OpenFile(sourceFile.c_str(), H5_O_RDONLY, props);
205 PAssert(source != (h5_file_t)H5_ERR);
207 h5_ssize_t numStepsInSource = H5GetNumSteps(source);
209 if (lastStep == -1 || lastStep >= numStepsInSource) {
212 char opalFlavour[128];
227 if (-lastStep > numStepsInSource) {
230 lastStep = numStepsInSource + lastStep;
250 if (ippl::Comm->rank() == sourceNode) {
259 std::ifstream source(sourceFile, std::ios::binary);
260 std::ofstream dest(
fileName_m, std::ios::binary);
262 std::istreambuf_iterator<char> begin_source(source);
263 std::istreambuf_iterator<char> end_source;
264 std::ostreambuf_iterator<char> begin_dest(dest);
265 std::copy(begin_source, end_source, begin_dest);
270 dest.bad(),
"H5PartWrapper::copyFile",
"could not copy file " + sourceFile);
274 sourceNode,
"H5PartWrapper::copyFile",
275 "received message to throw exception from node 0");
280 h5_int64_t numFileAttributes = H5GetNumFileAttribs(source);
282 const h5_size_t lengthAttributeName = 256;
283 char attributeName[lengthAttributeName];
284 h5_int64_t attributeType;
285 h5_size_t numAttributeElements;
286 std::vector<char> buffer(256);
287 h5_float32_t* f32buffer =
reinterpret_cast<h5_float32_t*
>(&buffer[0]);
288 h5_float64_t* f64buffer =
reinterpret_cast<h5_float64_t*
>(&buffer[0]);
289 h5_int32_t* i32buffer =
reinterpret_cast<h5_int32_t*
>(&buffer[0]);
290 h5_int64_t* i64buffer =
reinterpret_cast<h5_int64_t*
>(&buffer[0]);
292 for (h5_int64_t i = 0; i < numFileAttributes; ++i) {
294 source, i, attributeName, lengthAttributeName, &attributeType,
295 &numAttributeElements));
297 if (attributeType == H5_STRING_T) {
298 if (buffer.size() < numAttributeElements) {
299 buffer.resize(numAttributeElements);
305 }
else if (attributeType == H5_FLOAT32_T) {
306 if (buffer.size() < numAttributeElements *
sizeof(h5_float32_t)) {
307 buffer.resize(numAttributeElements *
sizeof(h5_float32_t));
313 }
else if (attributeType == H5_FLOAT64_T) {
314 if (buffer.size() < numAttributeElements *
sizeof(h5_float64_t)) {
315 buffer.resize(numAttributeElements *
sizeof(h5_float64_t));
321 }
else if (attributeType == H5_INT32_T) {
322 if (buffer.size() < numAttributeElements *
sizeof(h5_int32_t)) {
323 buffer.resize(numAttributeElements *
sizeof(h5_int32_t));
329 }
else if (attributeType == H5_INT64_T) {
330 if (buffer.size() < numAttributeElements *
sizeof(h5_int64_t)) {
331 buffer.resize(numAttributeElements *
sizeof(h5_int64_t));
339 "H5PartWrapper::copyHeader",
340 "unknown data type: " + std::to_string(attributeType));
354 h5_int64_t numStepAttributes = H5GetNumStepAttribs(source);
356 const h5_size_t lengthAttributeName = 256;
357 std::vector<char> attributeName(lengthAttributeName);
358 h5_int64_t attributeType;
359 h5_size_t numAttributeElements;
361 std::vector<char> buffer(256);
366 for (h5_int64_t i = 0; i < numStepAttributes; ++i) {
368 source, i, attributeName.data(), lengthAttributeName, &attributeType,
369 &numAttributeElements));
371 if (attributeType == H5TypesCHAR) {
372 if (buffer.size() < numAttributeElements) {
373 buffer.resize(numAttributeElements);
379 }
else if (attributeType == H5TypesFLOAT) {
380 if (buffer.size() < numAttributeElements *
sizeof(h5_float32_t)) {
381 buffer.resize(numAttributeElements *
sizeof(h5_float32_t));
385 Float32, source, attributeName.data(),
386 reinterpret_cast<h5_float32_t*
>(buffer.data()));
388 Float32,
file_m, attributeName.data(),
389 reinterpret_cast<h5_float32_t*
>(buffer.data()), numAttributeElements);
391 }
else if (attributeType == H5TypesDOUBLE) {
392 if (buffer.size() < numAttributeElements *
sizeof(h5_float64_t)) {
393 buffer.resize(numAttributeElements *
sizeof(h5_float64_t));
397 Float64, source, attributeName.data(),
398 reinterpret_cast<h5_float64_t*
>(buffer.data()));
400 Float64,
file_m, attributeName.data(),
401 reinterpret_cast<h5_float64_t*
>(buffer.data()), numAttributeElements);
403 }
else if (attributeType == H5TypesINT32) {
404 if (buffer.size() < numAttributeElements *
sizeof(h5_int32_t)) {
405 buffer.resize(numAttributeElements *
sizeof(h5_int32_t));
409 Int32, source, attributeName.data(),
410 reinterpret_cast<h5_int32_t*
>(buffer.data()));
412 Int32,
file_m, attributeName.data(),
413 reinterpret_cast<h5_int32_t*
>(buffer.data()), numAttributeElements);
415 }
else if (attributeType == H5TypesINT64) {
416 if (buffer.size() < numAttributeElements *
sizeof(h5_int64_t)) {
417 buffer.resize(numAttributeElements *
sizeof(h5_int64_t));
421 Int64, source, attributeName.data(),
422 reinterpret_cast<h5_int64_t*
>(buffer.data()));
424 Int64,
file_m, attributeName.data(),
425 reinterpret_cast<h5_int64_t*
>(buffer.data()), numAttributeElements);
429 "H5PartWrapper::copyStepHeader",
430 "unknown data type: " + std::to_string(attributeType));
436 const h5_size_t lengthSetName = 256;
437 std::vector<char> setName(lengthSetName);
439 h5_size_t numSetElements;
441 h5_ssize_t numParticles = H5PartGetNumParticles(source);
442 h5_ssize_t numParticlesPerNode = numParticles / ippl::Comm->size();
444 h5_ssize_t firstParticle = numParticlesPerNode * ippl::Comm->rank();
445 h5_ssize_t lastParticle = firstParticle + numParticlesPerNode - 1;
446 if (ippl::Comm->rank() == ippl::Comm->size() - 1) lastParticle = numParticles - 1;
448 REPORTONERROR(H5PartSetView(source, firstParticle, lastParticle));
450 numParticles = lastParticle - firstParticle + 1;
453 std::vector<char> buffer(numParticles *
sizeof(h5_float64_t));
455 h5_float32_t* f32buffer =
reinterpret_cast<h5_float32_t*
>(buffer_ptr);
456 h5_float64_t* f64buffer =
reinterpret_cast<h5_float64_t*
>(buffer_ptr);
457 h5_int32_t* i32buffer =
reinterpret_cast<h5_int32_t*
>(buffer_ptr);
458 h5_int64_t* i64buffer =
reinterpret_cast<h5_int64_t*
>(buffer_ptr);
460 h5_ssize_t numDatasets = H5PartGetNumDatasets(source);
462 for (h5_ssize_t i = 0; i < numDatasets; ++i) {
464 source, i, setName.data(), lengthSetName, &setType, &numSetElements));
466 if (setType == H5TypesFLOAT) {
467 READDATA(Float32, source, setName.data(), f32buffer);
469 }
else if (setType == H5TypesDOUBLE) {
470 READDATA(Float64, source, setName.data(), f64buffer);
472 }
else if (setType == H5TypesINT32) {
473 READDATA(Int32, source, setName.data(), i32buffer);
475 }
else if (setType == H5TypesINT64) {
476 READDATA(Int64, source, setName.data(), i64buffer);
480 "H5PartWrapper::copyStepData",
"unknown data type: " + std::to_string(setType));
484 numParticles = H5PartGetNumParticles(
file_m);