9 #include <boost/filesystem.hpp>
14 #define ADD_ATTACHMENT( fname ) { \
15 h5_int64_t h5err = H5AddAttachment (H5file_m, fname); \
16 if (h5err <= H5_ERR) { \
17 std::stringstream ss; \
18 ss << "failed to add attachment " << fname << " to file " << fn_m; \
19 throw GeneralClassicException(std::string(__func__), ss.str()); \
22 #define WRITE_FILEATTRIB_STRING( attribute, value ) { \
23 h5_int64_t h5err = H5WriteFileAttribString (H5file_m, attribute, value); \
24 if (h5err <= H5_ERR) { \
25 std::stringstream ss; \
26 ss << "failed to write string attribute " << attribute << " to file " << fn_m; \
27 throw GeneralClassicException(std::string(__func__), ss.str()); \
30 #define WRITE_STEPATTRIB_FLOAT64( attribute, value, size ) { \
31 h5_int64_t h5err = H5WriteStepAttribFloat64 (H5file_m, attribute, value, size); \
32 if (h5err <= H5_ERR) { \
33 std::stringstream ss; \
34 ss << "failed to write float64 attribute " << attribute << " to file " << fn_m; \
35 throw GeneralClassicException(std::string(__func__), ss.str()); \
38 #define WRITE_STEPATTRIB_INT64( attribute, value, size ) { \
39 h5_int64_t h5err = H5WriteStepAttribInt64 (H5file_m, attribute, value, size); \
40 if (h5err <= H5_ERR) { \
41 std::stringstream ss; \
42 ss << "failed to write int64 attribute " << attribute << " to file " << fn_m; \
43 throw GeneralClassicException(std::string(__func__), ss.str()); \
46 #define WRITE_DATA_FLOAT64( name, value ) { \
47 h5_int64_t h5err = H5PartWriteDataFloat64 (H5file_m, name, value); \
48 if (h5err <= H5_ERR) { \
49 std::stringstream ss; \
50 ss << "failed to write float64 data " << name << " to file " << fn_m; \
51 throw GeneralClassicException(std::string(__func__), ss.str()); \
54 #define WRITE_DATA_INT64( name, value ) { \
55 h5_int64_t h5err = H5PartWriteDataInt64 (H5file_m, name, value); \
56 if (h5err <= H5_ERR) { \
57 std::stringstream ss; \
58 ss << "failed to write int64 data " << name << " to file " << fn_m; \
59 throw GeneralClassicException(std::string(__func__), ss.str()); \
62 #define SET_STEP() { \
63 h5_int64_t h5err = H5SetStep (H5file_m, H5call_m); \
64 if (h5err <= H5_ERR) { \
65 std::stringstream ss; \
66 ss << "failed to set step " << H5call_m << " in file " << fn_m; \
67 throw GeneralClassicException(std::string(__func__), ss.str()); \
70 #define GET_NUM_STEPS() { \
71 H5call_m = H5GetNumSteps( H5file_m ); \
72 if (H5call_m <= H5_ERR) { \
73 std::stringstream ss; \
74 ss << "failed to get number of steps of file " << fn_m; \
75 throw GeneralClassicException(std::string(__func__), ss.str()); \
78 #define SET_NUM_PARTICLES( num ) { \
79 h5_int64_t h5err = H5PartSetNumParticles (H5file_m, num); \
80 if (h5err <= H5_ERR) { \
81 std::stringstream ss; \
82 ss << "failed to set number of particles to " << num << " in step " << H5call_m << " in file " << fn_m; \
83 throw GeneralClassicException(std::string(__func__), ss.str()); \
87 #define OPEN_FILE( fname, mode, props ) { \
88 H5file_m = H5OpenFile (fname, mode, props); \
89 if(H5file_m == (h5_file_t)H5_ERR) { \
90 std::stringstream ss; \
91 ss << "failed to open file " << fn_m; \
92 throw GeneralClassicException(std::string(__func__), ss.str()); \
95 #define CLOSE_FILE() { \
96 h5_int64_t h5err = H5CloseFile (H5file_m); \
97 if (h5err <= H5_ERR) { \
98 std::stringstream ss; \
99 ss << "failed to close file " << fn_m; \
100 throw GeneralClassicException(std::string(__func__), ss.str()); \
131 h5hut_mode_m(hdf5Save),
150 h5hut_mode_m(rsh.h5hut_mode_m),
151 H5file_m(rsh.H5file_m),
152 element_m(rsh.element_m),
153 H5call_m(rsh.H5call_m),
154 RefPartR_m(rsh.RefPartR_m),
155 RefPartP_m(rsh.RefPartP_m),
156 globalTrackStep_m(rsh.globalTrackStep_m),
157 refTime_m(rsh.refTime_m),
186 h5_prop_t props = H5CreateFileProp ();
188 H5SetPropFileMPIOCollective (props, &comm);
197 std::stringstream OPAL_version;
227 long long globalTrackStep
240 px_m.push_back(p(0));
241 py_m.push_back(p(1));
242 pz_m.push_back(p(2));
248 const double time,
const size_t turn,
249 const size_t& bunchNum) {
258 if (
element_m == std::string(
""))
return;
265 namespace fs = boost::filesystem;
279 for (
unsigned int i = 0; i < numSets; ++ i) {
324 size_t nLoc =
x_m.size();
333 size_t tLoc =
time_m.size();
342 size_t nLoc =
x_m.size();
352 globN[i] = locN[i] = 0;
362 if (setIdx <
spos_m.size()) {
378 std::vector<h5_int64_t> larray(
id_m.begin() + startIdx,
id_m.end() );
382 larray.assign (
turn_m.begin() + startIdx,
turn_m.end() );
399 const unsigned partCount =
x_m.size();
401 for(
unsigned i = 0; i < partCount; i++) {
419 while(notReceived > 0) {
420 unsigned dataBlocks = 0;
424 ERRORMSG(
"LossDataSink: Could not receive from client nodes output." <<
endl);
427 rmsg->
get(&dataBlocks);
428 for(
unsigned i = 0; i < dataBlocks; i++) {
430 size_t bunchNum, turn;
431 double rx, ry, rz, px, py, pz, time;
441 rmsg->
get(&bunchNum);
454 os_m << bunchNum <<
" ";
463 const unsigned msgsize =
x_m.size();
465 for(
unsigned i = 0; i < msgsize; i++) {
481 ERRORMSG(
"LossDataSink Ippl::Comm->send(smsg, 0, tag) failed " <<
endl;);
509 const size_t nLoc =
x_m.size();
510 size_t avgNumPerSet = nLoc / numSets;
511 std::vector<size_t> numPartsInSet(numSets, avgNumPerSet);
512 for (
unsigned int j = 0; j < (nLoc - numSets * avgNumPerSet); ++ j) {
517 std::vector<double> data(2 * numSets, 0.0);
518 double* meanT = &data[0];
519 double* numParticles = &data[numSets];
520 std::vector<double> timeRange(numSets, 0.0);
523 for (
unsigned int iteration = 0; iteration < 2; ++ iteration) {
525 for (
unsigned int j = 0; j < numSets; ++ j) {
527 const size_t &numThisSet = numPartsInSet[j];
528 for (
size_t k = 0; k < numThisSet; ++ k, ++ partIdx) {
529 meanT[j] +=
time_m[partIdx];
532 numParticles[j] = numThisSet;
535 allreduce(&(data[0]), 2 * numSets, std::plus<double>());
537 for (
unsigned int j = 0; j < numSets; ++ j) {
538 meanT[j] /= numParticles[j];
541 for (
unsigned int j = 0; j < numSets - 1; ++ j) {
542 timeRange[j] = 0.5 * (meanT[j] + meanT[j + 1]);
544 timeRange[numSets - 1] = maxT;
546 std::fill(numPartsInSet.begin(),
552 for (
size_t idx = 0; idx < nLoc; ++ idx) {
553 if (
time_m[idx] > timeRange[setNum]) {
554 numPartsInSet[setNum] = idx - idxPrior;
559 numPartsInSet[numSets - 1] = nLoc - idxPrior;
562 for (
unsigned int i = 0; i < numSets; ++ i) {
568 void cminmax(
double &
min,
double &
max,
double val) {
571 }
else if (val > max) {
581 const unsigned int totalSize = 45;
582 double plainData[totalSize];
591 size_t nLoc =
x_m.size();
599 unsigned int idx = startIdx;
600 for(
unsigned long k = 0; k < nLoc; ++ k, ++ idx) {
608 for(
int i = 0; i < 6; i++) {
609 localCentroid[i] += part[i];
610 for(
int j = 0; j <= i; j++) {
611 localMoments[i * 6 + j] += part[i] * part[j];
614 localOthers[0] +=
time_m[idx];
617 ::cminmax(rminmax[0], rminmax[1],
x_m[idx]);
618 ::cminmax(rminmax[2], rminmax[3],
y_m[idx]);
619 ::cminmax(rminmax[4], rminmax[5],
z_m[idx]);
622 for(
int i = 0; i < 6; i++) {
623 for(
int j = 0; j < i; j++) {
624 localMoments[j * 6 + i] = localMoments[i * 6 + j];
628 for (
unsigned int i = 0; i < totalSize; ++ i) {
629 plainData[i] = data[i].
sum;
632 new_reduce(plainData, totalSize, std::plus<double>());
633 new_reduce(rminmax, 6, std::greater<double>());
636 if (plainData[0] == 0.0)
return stat;
638 double *centroid = plainData + 1;
639 double *moments = plainData + 7;
640 double *others = plainData + 43;
649 for(
unsigned int i = 0 ; i < 3u; i++) {
652 stat.
rsqsum_m(i) = (moments[2 * i * 6 + 2 * i] -
655 moments[(2 * i + 1) * 6 + (2 * i) + 1] -
657 stat.
rpsum_m(i) = (moments[(2 * i) * 6 + (2 * i) + 1] -
668 for(
unsigned int i = 0 ; i < 3u; i++) {
673 stat.
fac_m(i) = (tmp == 0) ? 0.0 : 1.0 / tmp;
674 stat.
rmin_m(i) = -rminmax[2 * i];
675 stat.
rmax_m(i) = rminmax[2 * i + 1];
#define WRITE_STEPATTRIB_FLOAT64(attribute, value, size)
#define WRITE_FILEATTRIB_STRING(attribute, value)
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
std::vector< unsigned long > startSet_m
h5_file_t H5file_m
used to write out data in H5hut mode
#define OPEN_FILE(fname, mode, props)
std::vector< double > pz_m
int next_tag(int t, int s=1000)
std::vector< double > z_m
#define SET_NUM_PARTICLES(num)
Inform & level2(Inform &inf)
void save(unsigned int numSets=1, OpalData::OPENMODE openMode=OpalData::OPENMODE::UNDEFINED)
std::vector< size_t > bunchNum_m
std::vector< double > px_m
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
std::vector< Vector_t > RefPartP_m
static OpalData * getInstance()
#define OPAL_PROJECT_NAME
void saveH5(unsigned int setIdx)
#define WRITE_DATA_FLOAT64(name, value)
#define WRITE_STEPATTRIB_INT64(attribute, value, size)
OPENMODE
Enum for writing to files.
~LossDataSink() noexcept(false)
h5_int64_t H5call_m
Current record, or time step, of H5 file.
std::vector< double > py_m
void allreduce(const T *input, T *output, int count, Op op)
std::vector< double > spos_m
bool enableHDF5
If true HDF5 files are written.
OPENMODE getOpenMode() const
#define OPAL_PROJECT_VERSION
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
void addReferenceParticle(const Vector_t &x, const Vector_t &p, double time, double spos, long long globalTrackStep)
std::vector< Vector_t > RefPartR_m
static MPI_Comm getComm()
#define ADD_ATTACHMENT(fname)
Message & get(const T &cval)
Tps< T > sqrt(const Tps< T > &x)
Square root.
void splitSets(unsigned int numSets)
std::vector< size_t > turn_m
Message & put(const T &val)
std::vector< double > x_m
void openH5(h5_int32_t mode=H5_O_WRONLY)
std::string getGitRevision()
std::vector< double > time_m
std::vector< double > y_m
bool hasNoParticlesToDump()
Message * receive_block(int &node, int &tag)
#define WRITE_DATA_INT64(name, value)
SetStatistics computeSetStatistics(unsigned int setIdx)
void new_reduce(const T *input, T *output, int count, Op op, int root=0)
std::vector< double > refTime_m
static Communicate * Comm
bool send(Message *, int node, int tag, bool delmsg=true)
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
void addParticle(const Vector_t &x, const Vector_t &p, const size_t id)
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
std::vector< h5_int64_t > globalTrackStep_m