23#include "OPALconfig.h"
47#define WRITE_FILEATTRIB_STRING(attribute, value) \
49 h5_int64_t h5err = H5WriteFileAttribString(H5file_m, attribute, value); \
50 if (h5err <= H5_ERR) { \
51 std::stringstream ss; \
52 ss << "failed to write string attribute " << attribute << " to file " << fileName_m; \
53 throw GeneralClassicException(std::string(__func__), ss.str()); \
56#define WRITE_STEPATTRIB_FLOAT64(attribute, value, size) \
58 h5_int64_t h5err = H5WriteStepAttribFloat64(H5file_m, attribute, value, size); \
59 if (h5err <= H5_ERR) { \
60 std::stringstream ss; \
61 ss << "failed to write float64 attribute " << attribute << " to file " << fileName_m; \
62 throw GeneralClassicException(std::string(__func__), ss.str()); \
65#define WRITE_STEPATTRIB_INT64(attribute, value, size) \
67 h5_int64_t h5err = H5WriteStepAttribInt64(H5file_m, attribute, value, size); \
68 if (h5err <= H5_ERR) { \
69 std::stringstream ss; \
70 ss << "failed to write int64 attribute " << attribute << " to file " << fileName_m; \
71 throw GeneralClassicException(std::string(__func__), ss.str()); \
74#define WRITE_DATA_FLOAT64(name, value) \
76 h5_int64_t h5err = H5PartWriteDataFloat64(H5file_m, name, value); \
77 if (h5err <= H5_ERR) { \
78 std::stringstream ss; \
79 ss << "failed to write float64 data " << name << " to file " << fileName_m; \
80 throw GeneralClassicException(std::string(__func__), ss.str()); \
83#define WRITE_DATA_INT64(name, value) \
85 h5_int64_t h5err = H5PartWriteDataInt64(H5file_m, name, value); \
86 if (h5err <= H5_ERR) { \
87 std::stringstream ss; \
88 ss << "failed to write int64 data " << name << " to file " << fileName_m; \
89 throw GeneralClassicException(std::string(__func__), ss.str()); \
94 h5_int64_t h5err = H5SetStep(H5file_m, H5call_m); \
95 if (h5err <= H5_ERR) { \
96 std::stringstream ss; \
97 ss << "failed to set step " << H5call_m << " in file " << fileName_m; \
98 throw GeneralClassicException(std::string(__func__), ss.str()); \
101#define GET_NUM_STEPS() \
103 H5call_m = H5GetNumSteps(H5file_m); \
104 if (H5call_m <= H5_ERR) { \
105 std::stringstream ss; \
106 ss << "failed to get number of steps of file " << fileName_m; \
107 throw GeneralClassicException(std::string(__func__), ss.str()); \
110#define SET_NUM_PARTICLES(num) \
112 h5_int64_t h5err = H5PartSetNumParticles(H5file_m, num); \
113 if (h5err <= H5_ERR) { \
114 std::stringstream ss; \
115 ss << "failed to set number of particles to " << num << " in step " << H5call_m \
116 << " in file " << fileName_m; \
117 throw GeneralClassicException(std::string(__func__), ss.str()); \
121#define OPEN_FILE(fname, mode, props) \
123 H5file_m = H5OpenFile(fname, mode, props); \
124 if (H5file_m == (h5_file_t)H5_ERR) { \
125 std::stringstream ss; \
126 ss << "failed to open file " << fileName_m; \
127 throw GeneralClassicException(std::string(__func__), ss.str()); \
130#define CLOSE_FILE() \
132 h5_int64_t h5err = H5CloseFile(H5file_m); \
133 if (h5err <= H5_ERR) { \
134 std::stringstream ss; \
135 ss << "failed to close file " << fileName_m; \
136 throw GeneralClassicException(std::string(__func__), ss.str()); \
142 const std::vector<OpalParticle>& particles,
unsigned int startIdx,
143 unsigned int numParticles, h5_float64_t* buffer,
144 std::function<h5_float64_t(
const OpalParticle&)> select) {
146 particles.begin() + startIdx, particles.begin() + startIdx + numParticles, buffer,
150 const std::vector<OpalParticle>& particles,
unsigned int startIdx,
151 unsigned int numParticles, h5_int64_t* buffer,
152 std::function<h5_int64_t(
const OpalParticle&)> select) {
154 particles.begin() + startIdx, particles.begin() + startIdx + numParticles, buffer,
158 void cminmax(
double&
min,
double&
max,
double val) {
161 }
else if (val >
max) {
193 : h5hut_mode_m(hdf5Save),
197 collectionType_m(collectionType) {
204 "LossDataSink::LossDataSink",
205 "You must select an OPTION to save Loss data files\n"
206 "Please, choose 'ENABLEHDF5=TRUE' or 'ASCIIDUMP=TRUE'");
217 : h5hut_mode_m(rhs.h5hut_mode_m),
218 H5file_m(rhs.H5file_m),
219 outputName_m(rhs.outputName_m),
220 H5call_m(rhs.H5call_m),
221 RefPartR_m(rhs.RefPartR_m),
222 RefPartP_m(rhs.RefPartP_m),
223 globalTrackStep_m(rhs.globalTrackStep_m),
224 refTime_m(rhs.refTime_m),
226 collectionType_m(rhs.collectionType_m) {
240 h5_prop_t props = H5CreateFileProp();
242 H5SetPropFileMPIOCollective(props, &comm);
249 std::stringstream OPAL_version;
250 OPAL_version << OPAL_PROJECT_NAME <<
" " << OPAL_PROJECT_VERSION <<
" # git rev. "
309 os_m <<
"# x (m), y (m), z (m), px ( ), py ( ), pz ( ), id";
311 os_m <<
", turn ( ), bunchNumber ( )";
313 os_m <<
", time (s)" << std::endl;
318 const Vector_t& x,
const Vector_t& p,
double time,
double spos,
long long globalTrackStep) {
327 const OpalParticle& particle,
const std::optional<std::pair<int, short>>& turnBunchNumPair) {
328 if (turnBunchNumPair) {
331 "LossDataSink::addParticle",
332 "Either no particle or all have turn number and bunch number");
351 namespace fs = std::filesystem;
362 for (
unsigned int i = 0; i < numSets; ++i) {
388 spos_m = std::vector<double>();
414 allreduce(hasTurnInformation, 1, std::logical_or<bool>());
416 return hasTurnInformation > 0;
421 std::size_t startIdx = 0, endIdx = nLoc;
425 nLoc = endIdx - startIdx;
428 std::unique_ptr<std::size_t[]> locN(
new std::size_t[
Ippl::getNodes()]);
429 std::unique_ptr<std::size_t[]> globN(
new std::size_t[
Ippl::getNodes()]);
432 globN[i] = locN[i] = 0;
445 if (setIdx <
spos_m.size()) {
473 "68-percentile", (tmpVector = engine.
get68Percentile(), &tmpVector[0]), 3);
475 "95-percentile", (tmpVector = engine.
get95Percentile(), &tmpVector[0]), 3);
477 "99-percentile", (tmpVector = engine.
get99Percentile(), &tmpVector[0]), 3);
482 "normalizedEps68Percentile",
485 "normalizedEps95Percentile",
488 "normalizedEps99Percentile",
491 "normalizedEps99_99Percentile",
498 std::vector<char> buffer(nLoc *
sizeof(h5_float64_t));
499 h5_float64_t* f64buffer = nLoc > 0 ?
reinterpret_cast<h5_float64_t*
>(&buffer[0]) :
nullptr;
500 h5_int64_t* i64buffer = nLoc > 0 ?
reinterpret_cast<h5_int64_t*
>(&buffer[0]) :
nullptr;
503 return particle.
getId();
507 return particle.
getX();
511 return particle.
getY();
515 return particle.
getZ();
519 return particle.
getPx();
523 return particle.
getPy();
527 return particle.
getPz();
566 for (
unsigned i = 0; i < partCount; i++) {
583 while (notReceived > 0) {
584 unsigned dataBlocks = 0;
588 ERRORMSG(
"LossDataSink: Could not receive from client nodes output." <<
endl);
591 rmsg->
get(&dataBlocks);
593 for (
unsigned i = 0; i < dataBlocks; i++) {
595 std::size_t bunchNum, turn;
596 double rx, ry, rz, px, py, pz, time;
598 os_m << (rmsg->
get(&rx), rx) <<
" ";
599 os_m << (rmsg->
get(&ry), ry) <<
" ";
600 os_m << (rmsg->
get(&rz), rz) <<
" ";
601 os_m << (rmsg->
get(&px), px) <<
" ";
602 os_m << (rmsg->
get(&py), py) <<
" ";
603 os_m << (rmsg->
get(&pz), pz) <<
" ";
604 os_m << (rmsg->
get(&
id), id) <<
" ";
606 os_m << (rmsg->
get(&turn), turn) <<
" ";
607 os_m << (rmsg->
get(&bunchNum), bunchNum) <<
" ";
609 os_m << (rmsg->
get(&time), time) << std::endl;
618 for (
unsigned i = 0; i < msgsize; i++) {
635 ERRORMSG(
"LossDataSink Ippl::Comm->send(smsg, 0, tag) failed " <<
endl);
664 std::size_t avgNumPerSet = nLoc / numSets;
665 std::vector<std::size_t> numPartsInSet(numSets, avgNumPerSet);
666 for (
unsigned int j = 0; j < (nLoc - numSets * avgNumPerSet); ++j) {
671 std::vector<double> data(2 * numSets, 0.0);
672 double* meanT = &data[0];
673 double* numParticles = &data[numSets];
674 std::vector<double> timeRange(numSets, 0.0);
677 for (
unsigned int iteration = 0; iteration < 2; ++iteration) {
678 std::size_t partIdx = 0;
679 for (
unsigned int j = 0; j < numSets; ++j) {
680 const std::size_t& numThisSet = numPartsInSet[j];
681 for (std::size_t k = 0; k < numThisSet; ++k, ++partIdx) {
683 maxT = std::max(maxT,
particles_m[partIdx].getTime());
685 numParticles[j] = numThisSet;
688 allreduce(&(data[0]), 2 * numSets, std::plus<double>());
690 for (
unsigned int j = 0; j < numSets; ++j) {
691 meanT[j] /= numParticles[j];
694 for (
unsigned int j = 0; j < numSets - 1; ++j) {
695 timeRange[j] = 0.5 * (meanT[j] + meanT[j + 1]);
697 timeRange[numSets - 1] = maxT;
699 std::fill(numPartsInSet.begin(), numPartsInSet.end(), 0);
700 std::size_t setNum = 0;
701 std::size_t idxPrior = 0;
702 for (std::size_t idx = 0; idx < nLoc; ++idx) {
703 if (
particles_m[idx].getTime() > timeRange[setNum]) {
704 numPartsInSet[setNum] = idx - idxPrior;
709 numPartsInSet[numSets - 1] = nLoc - idxPrior;
712 for (
unsigned int i = 0; i < numSets; ++i) {
721 const unsigned int totalSize = 45;
722 double plainData[totalSize];
723 double rminmax[6] = {};
730 std::size_t startIdx = 0;
739 unsigned int idx = startIdx;
740 for (
unsigned long k = 0; k < nLoc; ++k, ++idx) {
743 part[0] = particle.
getX();
744 part[1] = particle.
getPx();
745 part[2] = particle.
getY();
746 part[3] = particle.
getPy();
747 part[4] = particle.
getZ();
748 part[5] = particle.
getPz();
750 for (
int i = 0; i < 6; i++) {
751 localCentroid[i] += part[i];
752 for (
int j = 0; j <= i; j++) {
753 localMoments[i * 6 + j] += part[i] * part[j];
756 localOthers[0] += particle.
getTime();
757 localOthers[1] += std::pow(particle.
getTime(), 2);
759 ::cminmax(rminmax[0], rminmax[1], particle.
getX());
760 ::cminmax(rminmax[2], rminmax[3], particle.
getY());
761 ::cminmax(rminmax[4], rminmax[5], particle.
getZ());
764 for (
int i = 0; i < 6; i++) {
765 for (
int j = 0; j < i; j++) {
766 localMoments[j * 6 + i] = localMoments[i * 6 + j];
770 for (
unsigned int i = 0; i < totalSize; ++i) {
771 plainData[i] = data[i].
sum;
774 allreduce(plainData, totalSize, std::plus<double>());
775 allreduce(rminmax, 6, std::greater<double>());
777 if (plainData[0] == 0.0)
780 double* centroid = plainData + 1;
781 double* moments = plainData + 7;
782 double* others = plainData + 43;
789 stat.
nTotal_m = (
unsigned long)std::round(plainData[0]);
791 for (
unsigned int i = 0; i < 3u; i++) {
795 (moments[2 * i * 6 + 2 * i] - stat.
nTotal_m * std::pow(stat.
rmean_m(i), 2));
798 moments[(2 * i + 1) * 6 + (2 * i) + 1] - stat.
nTotal_m * std::pow(stat.
pmean_m(i), 2));
800 (moments[(2 * i) * 6 + (2 * i) + 1]
812 for (
unsigned int i = 0; i < 3u; i++) {
817 stat.
fac_m(i) = (tmp == 0) ? 0.0 : 1.0 / tmp;
818 stat.
rmin_m(i) = -rminmax[2 * i];
819 stat.
rmax_m(i) = rminmax[2 * i + 1];
#define OPEN_FILE(fname, mode, props)
#define WRITE_DATA_FLOAT64(name, value)
#define WRITE_STEPATTRIB_INT64(attribute, value, size)
#define WRITE_FILEATTRIB_STRING(attribute, value)
#define SET_NUM_PARTICLES(num)
#define WRITE_STEPATTRIB_FLOAT64(attribute, value, size)
#define WRITE_DATA_INT64(name, value)
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
void allreduce(const T *input, T *output, int count, Op op)
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
Inform & level2(Inform &inf)
Inform & endl(Inform &inf)
bool enableHDF5
If true HDF5 files are written.
std::string getGitRevision()
void checkAndAddOutputFileName(const std::string &outfn)
checks the output file names of all items to avoid duplicates
OpenMode getOpenMode() const
static OpalData * getInstance()
OpenMode
Enum for writing to files.
void setOpenMode(OpenMode openMode)
Vector_t getStandardDeviationMomentum() const
Vector_t get99Percentile() const
double getMeanKineticEnergy() const
Vector_t getStandardDeviationPosition() const
Vector_t getNormalizedEmittance95Percentile() const
Vector_t getNormalizedEmittance68Percentile() const
double getTotalCharge() const
Vector_t getNormalizedEmittance99_99Percentile() const
Vector_t getNormalizedEmittance99Percentile() const
Vector_t getMeanPosition() const
Vector_t get99_99Percentile() const
void compute(const std::vector< OpalParticle >::const_iterator &, const std::vector< OpalParticle >::const_iterator &)
double getStdKineticEnergy() const
Vector_t getNormalizedEmittance() const
double getStdTime() const
double getTotalMass() const
Vector_t getMeanMomentum() const
Vector_t get95Percentile() const
Vector_t get68Percentile() const
double getMeanTime() const
Vector_t getGeometricEmittance() const
double getPy() const
Get vertical momentum (no dimension).
double getPz() const
Get relative momentum error (no dimension).
double getY() const
Get vertical displacement in m.
double getCharge() const
Get charge in Coulomb.
double getZ() const
Get longitudinal displacement c*t in m.
double getMass() const
Get mass in GeV/c^2.
double getTime() const
Get time.
int64_t getId() const
Get the id of the particle.
double getPx() const
Get horizontal momentum (no dimension).
double getX() const
Get horizontal position in m.
bool save(unsigned int numSets=1, OpalData::OpenMode openMode=OpalData::OpenMode::UNDEFINED)
Returns whether the file was written; false (on all ranks) if there was nothing to save.
std::vector< Vector_t > RefPartR_m
void addReferenceParticle(const Vector_t &x, const Vector_t &p, double time, double spos, long long globalTrackStep)
std::vector< std::size_t > bunchNumber_m
h5_file_t H5file_m
used to write out data in H5hut mode
~LossDataSink() noexcept(false)
std::vector< std::size_t > turnNumber_m
std::vector< double > refTime_m
CollectionType collectionType_m
std::vector< Vector_t > RefPartP_m
h5_int64_t H5call_m
Current record, or time step, of H5 file.
void addParticle(const OpalParticle &, const std::optional< std::pair< int, short int > > &turnBunchNumPair=std::nullopt)
std::vector< double > spos_m
void openH5(h5_int32_t mode=H5_O_WRONLY)
SetStatistics computeSetStatistics(unsigned int setIdx)
void splitSets(unsigned int numSets)
std::vector< OpalParticle > particles_m
bool hasTurnInformations() const
void saveH5(unsigned int setIdx)
std::vector< h5_int64_t > globalTrackStep_m
bool hasNoParticlesToDump() const
std::vector< unsigned long > startSet_m
bool send(Message *, int node, int tag, bool delmsg=true)
Message * receive_block(int &node, int &tag)
Message & put(const T &val)
Message & get(const T &cval)
int next_tag(int t, int s=1000)
static MPI_Comm getComm()
static Communicate * Comm