OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
LossDataSink.cpp
Go to the documentation of this file.
2 
3 #include "Ippl.h"
4 #include "Utilities/Options.h"
7 #include "Utilities/Util.h"
8 
9 #include <boost/filesystem.hpp>
10 #include "OPALconfig.h"
11 
12 #include <cassert>
13 
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()); \
20  }\
21 }
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()); \
28  }\
29 }
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()); \
36  }\
37 }
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()); \
44  }\
45 }
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()); \
52  }\
53 }
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()); \
60  }\
61 }
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()); \
68  }\
69 }
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()); \
76  }\
77 }
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()); \
84  }\
85 }
86 
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()); \
93  }\
94 }
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()); \
101  }\
102 }
103 
104 
106  element_m(""),
107  spos_m(0.0),
108  refTime_m(0.0),
109  tmean_m(0.0),
110  trms_m(0.0),
111  nTotal_m(0),
112  RefPartR_m(0.0),
113  RefPartP_m(0.0),
114  rmin_m(0.0),
115  rmax_m(0.0),
116  rmean_m(0.0),
117  pmean_m(0.0),
118  rrms_m(0.0),
119  prms_m(0.0),
120  rprms_m(0.0),
121  normEmit_m(0.0),
122  rsqsum_m(0.0),
123  psqsum_m(0.0),
124  rpsum_m(0.0),
125  eps2_m(0.0),
126  eps_norm_m(0.0),
127  fac_m(0.0)
128  { }
129 
130 LossDataSink::LossDataSink(std::string elem, bool hdf5Save, ElementBase::ElementType type):
131  h5hut_mode_m(hdf5Save),
132  H5file_m(0),
133  element_m(elem),
134  H5call_m(0),
135  type_m(type)
136 {
137  x_m.clear();
138  y_m.clear();
139  z_m.clear();
140  px_m.clear();
141  py_m.clear();
142  pz_m.clear();
143  id_m.clear();
144  turn_m.clear();
145  bunchNum_m.clear();
146  time_m.clear();
147 }
148 
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),
158  spos_m(rsh.spos_m),
159  type_m(rsh.type_m)
160 {
161  x_m.clear();
162  y_m.clear();
163  z_m.clear();
164  px_m.clear();
165  py_m.clear();
166  pz_m.clear();
167  id_m.clear();
168  turn_m.clear();
169  bunchNum_m.clear();
170  time_m.clear();
171 }
172 
174  LossDataSink(std::string("NULL"), false);
175 }
176 
177 LossDataSink::~LossDataSink() noexcept(false) {
178  if (H5file_m) {
179  CLOSE_FILE ();
180  H5file_m = 0;
181  }
182  Ippl::Comm->barrier();
183 }
184 
185 void LossDataSink::openH5(h5_int32_t mode) {
186  h5_prop_t props = H5CreateFileProp ();
187  MPI_Comm comm = Ippl::getComm();
188  H5SetPropFileMPIOCollective (props, &comm);
189  OPEN_FILE (fn_m.c_str(), mode, props);
190  H5CloseProp (props);
191 }
192 
193 
195 
196  // Write file attributes to describe phase space to H5 file.
197  std::stringstream OPAL_version;
198  OPAL_version << OPAL_PROJECT_NAME << " " << OPAL_PROJECT_VERSION << " # git rev. " << Util::getGitRevision();
199  WRITE_FILEATTRIB_STRING ("OPAL_version", OPAL_version.str().c_str());
200  WRITE_FILEATTRIB_STRING ("tUnit", "s");
201  WRITE_FILEATTRIB_STRING ("xUnit", "m");
202  WRITE_FILEATTRIB_STRING ("yUnit", "m");
203  WRITE_FILEATTRIB_STRING ("zUnit", "m");
204  WRITE_FILEATTRIB_STRING ("pxUnit", "#beta#gamma");
205  WRITE_FILEATTRIB_STRING ("pyUnit", "#beta#gamma");
206  WRITE_FILEATTRIB_STRING ("pzUnit", "#beta#gamma");
207  WRITE_FILEATTRIB_STRING ("idUnit", "1");
208 
210  if (hasTimeAttribute()) {
211  WRITE_FILEATTRIB_STRING ("turnUnit", "1");
212  WRITE_FILEATTRIB_STRING ("timeUnit", "s");
213  }
214  WRITE_FILEATTRIB_STRING ("SPOSUnit", "mm");
215  WRITE_FILEATTRIB_STRING ("TIMEUnit", "s");
216 
217  WRITE_FILEATTRIB_STRING ("mpart", "GeV");
218  WRITE_FILEATTRIB_STRING ("qi", "C");
219 
220  ADD_ATTACHMENT (OpalData::getInstance()->getInputFn().c_str());
221 }
222 
224  const Vector_t &p,
225  double time,
226  double spos,
227  long long globalTrackStep
228  ) {
229  RefPartR_m.push_back(x);
230  RefPartP_m.push_back(p);
231  globalTrackStep_m.push_back((h5_int64_t)globalTrackStep);
232  spos_m.push_back(spos);
233  refTime_m.push_back(time);
234 }
235 
236 void LossDataSink::addParticle(const Vector_t &x,const Vector_t &p, const size_t id) {
237  x_m.push_back(x(0));
238  y_m.push_back(x(1));
239  z_m.push_back(x(2));
240  px_m.push_back(p(0));
241  py_m.push_back(p(1));
242  pz_m.push_back(p(2));
243  id_m.push_back(id);
244 }
245 
246 // For ring type simulation, dump the time and turn number
247 void LossDataSink::addParticle(const Vector_t &x, const Vector_t &p, const size_t id,
248  const double time, const size_t turn,
249  const size_t& bunchNum) {
250  addParticle(x, p, id);
251  turn_m.push_back(turn);
252  time_m.push_back(time);
253  bunchNum_m.push_back(bunchNum);
254 }
255 
256 void LossDataSink::save(unsigned int numSets, OpalData::OPENMODE openMode) {
257 
258  if (element_m == std::string("")) return;
259  if (hasNoParticlesToDump()) return;
260 
261  if (openMode == OpalData::OPENMODE::UNDEFINED) {
262  openMode = OpalData::getInstance()->getOpenMode();
263  }
264 
265  namespace fs = boost::filesystem;
266  if (h5hut_mode_m) {
267  if (!Options::enableHDF5) return;
268 
269  fn_m = element_m + std::string(".h5");
270  INFOMSG(level2 << "Save " << fn_m << endl);
271  if (openMode == OpalData::OPENMODE::WRITE || !fs::exists(fn_m)) {
272  openH5();
273  writeHeaderH5();
274  } else {
275  openH5(H5_O_APPENDONLY);
276  GET_NUM_STEPS ();
277  }
278 
279  for (unsigned int i = 0; i < numSets; ++ i) {
280  saveH5(i);
281  }
282  CLOSE_FILE ();
283  H5file_m = 0;
284  }
285  else {
286  fn_m = element_m + std::string(".loss");
287  INFOMSG(level2 << "Save " << fn_m << endl);
288  if (openMode == OpalData::OPENMODE::WRITE || !fs::exists(fn_m)) {
289  openASCII();
291  } else {
292  appendASCII();
293  }
294  saveASCII();
295  closeASCII();
296  }
297  Ippl::Comm->barrier();
298 
299  x_m.clear();
300  y_m.clear();
301  z_m.clear();
302  px_m.clear();
303  py_m.clear();
304  pz_m.clear();
305  id_m.clear();
306  turn_m.clear();
307  bunchNum_m.clear();
308  time_m.clear();
309  spos_m.clear();
310  refTime_m.clear();
311  RefPartR_m.clear();
312  RefPartP_m.clear();
313  globalTrackStep_m.clear();
314 }
315 
316 // Note: This was changed to calculate the global number of dumped particles
317 // because there are two cases to be considered:
318 // 1. ALL nodes have 0 lost particles -> nothing to be done.
319 // 2. Some nodes have 0 lost particles, some not -> H5 can handle that but all
320 // nodes HAVE to participate, otherwise H5 waits endlessly for a response from
321 // the nodes that didn't enter the saveH5 function. -DW
323 
324  size_t nLoc = x_m.size();
325 
326  reduce(nLoc, nLoc, OpAddAssign());
327 
328  return nLoc == 0;
329 }
330 
332 
333  size_t tLoc = time_m.size();
334 
335  reduce(tLoc, tLoc, OpAddAssign());
336 
337  return tLoc > 0;
338 }
339 
340 void LossDataSink::saveH5(unsigned int setIdx) {
341  size_t startIdx = 0;
342  size_t nLoc = x_m.size();
343  if (setIdx + 1 < startSet_m.size()) {
344  startIdx = startSet_m[setIdx];
345  nLoc = startSet_m[setIdx + 1] - startSet_m[setIdx];
346  }
347 
348  std::unique_ptr<size_t[]> locN(new size_t[Ippl::getNodes()]);
349  std::unique_ptr<size_t[]> globN(new size_t[Ippl::getNodes()]);
350 
351  for(int i = 0; i < Ippl::getNodes(); i++) {
352  globN[i] = locN[i] = 0;
353  }
354 
355  locN[Ippl::myNode()] = nLoc;
356  reduce(locN.get(), locN.get() + Ippl::getNodes(), globN.get(), OpAddAssign());
357 
359  SET_STEP ();
360  SET_NUM_PARTICLES (nLoc);
361 
362  if (setIdx < spos_m.size()) {
363  WRITE_STEPATTRIB_FLOAT64 ("SPOS", &(spos_m[setIdx]), 1);
364  WRITE_STEPATTRIB_FLOAT64 ("TIME", &(refTime_m[setIdx]), 1);
365  WRITE_STEPATTRIB_FLOAT64 ("RefPartR", (h5_float64_t *)&(RefPartR_m[setIdx]), 3);
366  WRITE_STEPATTRIB_FLOAT64 ("RefPartP", (h5_float64_t *)&(RefPartP_m[setIdx]), 3);
367  WRITE_STEPATTRIB_INT64("GlobalTrackStep", &(globalTrackStep_m[setIdx]), 1);
368  }
369  // Write all data
370  WRITE_DATA_FLOAT64 ("x", &x_m[startIdx]);
371  WRITE_DATA_FLOAT64 ("y", &y_m[startIdx]);
372  WRITE_DATA_FLOAT64 ("z", &z_m[startIdx]);
373  WRITE_DATA_FLOAT64 ("px", &px_m[startIdx]);
374  WRITE_DATA_FLOAT64 ("py", &py_m[startIdx]);
375  WRITE_DATA_FLOAT64 ("pz", &pz_m[startIdx]);
376 
378  std::vector<h5_int64_t> larray(id_m.begin() + startIdx, id_m.end() );
379  WRITE_DATA_INT64 ("id", &larray[0]);
380  if (hasTimeAttribute()) {
381  WRITE_DATA_FLOAT64 ("time", &time_m[startIdx]);
382  larray.assign (turn_m.begin() + startIdx, turn_m.end() );
383  WRITE_DATA_INT64 ("turn", &larray[0]);
384  larray.assign (bunchNum_m.begin() + startIdx, bunchNum_m.end() );
385  WRITE_DATA_INT64 ("bunchNumber", &larray[0]);
386  }
387 
388  ++ H5call_m;
389 }
390 
392 
393  /*
394  ASCII output
395  */
397  bool hasTime = hasTimeAttribute(); // reduce needed for the case when node 0 has no particles
398  if(Ippl::Comm->myNode() == 0) {
399  const unsigned partCount = x_m.size();
400 
401  for(unsigned i = 0; i < partCount; i++) {
402  os_m << element_m << " ";
403  os_m << x_m[i] << " ";
404  os_m << y_m[i] << " ";
405  os_m << z_m[i] << " ";
406  os_m << px_m[i] << " ";
407  os_m << py_m[i] << " ";
408  os_m << pz_m[i] << " ";
409  os_m << id_m[i] << " ";
410  if (hasTime) {
411  os_m << turn_m[i] << " ";
412  os_m << bunchNum_m[i] << " ";
413  os_m << time_m[i];
414  }
415  os_m << std::endl;
416  }
417 
418  int notReceived = Ippl::getNodes() - 1;
419  while(notReceived > 0) {
420  unsigned dataBlocks = 0;
421  int node = COMM_ANY_NODE;
422  Message *rmsg = Ippl::Comm->receive_block(node, tag);
423  if(rmsg == 0) {
424  ERRORMSG("LossDataSink: Could not receive from client nodes output." << endl);
425  }
426  notReceived--;
427  rmsg->get(&dataBlocks);
428  for(unsigned i = 0; i < dataBlocks; i++) {
429  long id;
430  size_t bunchNum, turn;
431  double rx, ry, rz, px, py, pz, time;
432  rmsg->get(&id);
433  rmsg->get(&rx);
434  rmsg->get(&ry);
435  rmsg->get(&rz);
436  rmsg->get(&px);
437  rmsg->get(&py);
438  rmsg->get(&pz);
439  if (hasTime) {
440  rmsg->get(&turn);
441  rmsg->get(&bunchNum);
442  rmsg->get(&time);
443  }
444  os_m << element_m << " ";
445  os_m << rx << " ";
446  os_m << ry << " ";
447  os_m << rz << " ";
448  os_m << px << " ";
449  os_m << py << " ";
450  os_m << pz << " ";
451  os_m << id << " ";
452  if (hasTime) {
453  os_m << turn << " ";
454  os_m << bunchNum << " ";
455  os_m << time;
456  }
457  os_m << std::endl;
458  }
459  delete rmsg;
460  }
461  } else {
462  Message *smsg = new Message();
463  const unsigned msgsize = x_m.size();
464  smsg->put(msgsize);
465  for(unsigned i = 0; i < msgsize; i++) {
466  smsg->put(id_m[i]);
467  smsg->put(x_m[i]);
468  smsg->put(y_m[i]);
469  smsg->put(z_m[i]);
470  smsg->put(px_m[i]);
471  smsg->put(py_m[i]);
472  smsg->put(pz_m[i]);
473  if (hasTime) {
474  smsg->put(turn_m[i]);
475  smsg->put(bunchNum_m[i]);
476  smsg->put(time_m[i]);
477  }
478  }
479  bool res = Ippl::Comm->send(smsg, 0, tag);
480  if(! res)
481  ERRORMSG("LossDataSink Ippl::Comm->send(smsg, 0, tag) failed " << endl;);
482  }
483 }
484 
504 void LossDataSink::splitSets(unsigned int numSets) {
505  if (numSets <= 1 ||
506  x_m.size() == 0 ||
507  time_m.size() != x_m.size()) return;
508 
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) {
513  ++ numPartsInSet[j];
514  }
515  startSet_m.resize(numSets + 1, 0);
516 
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);
521  double maxT = time_m[0];
522 
523  for (unsigned int iteration = 0; iteration < 2; ++ iteration) {
524  size_t partIdx = 0;
525  for (unsigned int j = 0; j < numSets; ++ j) {
526 
527  const size_t &numThisSet = numPartsInSet[j];
528  for (size_t k = 0; k < numThisSet; ++ k, ++ partIdx) {
529  meanT[j] += time_m[partIdx];
530  maxT = std::max(maxT, time_m[partIdx]);
531  }
532  numParticles[j] = numThisSet;
533  }
534 
535  allreduce(&(data[0]), 2 * numSets, std::plus<double>());
536 
537  for (unsigned int j = 0; j < numSets; ++ j) {
538  meanT[j] /= numParticles[j];
539  }
540 
541  for (unsigned int j = 0; j < numSets - 1; ++ j) {
542  timeRange[j] = 0.5 * (meanT[j] + meanT[j + 1]);
543  }
544  timeRange[numSets - 1] = maxT;
545 
546  std::fill(numPartsInSet.begin(),
547  numPartsInSet.end(),
548  0);
549 
550  size_t setNum = 0;
551  size_t idxPrior = 0;
552  for (size_t idx = 0; idx < nLoc; ++ idx) {
553  if (time_m[idx] > timeRange[setNum]) {
554  numPartsInSet[setNum] = idx - idxPrior;
555  idxPrior = idx;
556  ++ setNum;
557  }
558  }
559  numPartsInSet[numSets - 1] = nLoc - idxPrior;
560  }
561 
562  for (unsigned int i = 0; i < numSets; ++ i) {
563  startSet_m[i + 1] = startSet_m[i] + numPartsInSet[i];
564  }
565 }
566 
567 namespace {
568  void cminmax(double &min, double &max, double val) {
569  if (-val > min) {
570  min = -val;
571  } else if (val > max) {
572  max = val;
573  }
574  }
575 }
576 
578  SetStatistics stat;
579  double part[6];
580 
581  const unsigned int totalSize = 45;
582  double plainData[totalSize];
583  double rminmax[6];
584 
585  Util::KahanAccumulation data[totalSize];
586  Util::KahanAccumulation *localCentroid = data + 1;
587  Util::KahanAccumulation *localMoments = data + 7;
588  Util::KahanAccumulation *localOthers = data + 43;
589 
590  size_t startIdx = 0;
591  size_t nLoc = x_m.size();
592  if (setIdx + 1 < startSet_m.size()) {
593  startIdx = startSet_m[setIdx];
594  nLoc = startSet_m[setIdx + 1] - startSet_m[setIdx];
595  }
596 
597  data[0].sum = nLoc;
598 
599  unsigned int idx = startIdx;
600  for(unsigned long k = 0; k < nLoc; ++ k, ++ idx) {
601  part[1] = px_m[idx];
602  part[3] = py_m[idx];
603  part[5] = pz_m[idx];
604  part[0] = x_m[idx];
605  part[2] = y_m[idx];
606  part[4] = z_m[idx];
607 
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];
612  }
613  }
614  localOthers[0] += time_m[idx];
615  localOthers[1] += std::pow(time_m[idx], 2);
616 
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]);
620  }
621 
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];
625  }
626  }
627 
628  for (unsigned int i = 0; i < totalSize; ++ i) {
629  plainData[i] = data[i].sum;
630  }
631 
632  new_reduce(plainData, totalSize, std::plus<double>());
633  new_reduce(rminmax, 6, std::greater<double>());
634 
635  if (Ippl::myNode() != 0) return stat;
636  if (plainData[0] == 0.0) return stat;
637 
638  double *centroid = plainData + 1;
639  double *moments = plainData + 7;
640  double *others = plainData + 43;
641 
642  stat.element_m = element_m;
643  stat.spos_m = spos_m[setIdx];
644  stat.refTime_m = refTime_m[setIdx];
645  stat.RefPartR_m = RefPartR_m[setIdx];
646  stat.RefPartP_m = RefPartP_m[setIdx];
647  stat.nTotal_m = (unsigned long)floor(plainData[0] + 0.5);
648 
649  for(unsigned int i = 0 ; i < 3u; i++) {
650  stat.rmean_m(i) = centroid[2 * i] / stat.nTotal_m;
651  stat.pmean_m(i) = centroid[(2 * i) + 1] / stat.nTotal_m;
652  stat.rsqsum_m(i) = (moments[2 * i * 6 + 2 * i] -
653  stat.nTotal_m * std::pow(stat.rmean_m(i), 2));
654  stat.psqsum_m(i) = std::max(0.0,
655  moments[(2 * i + 1) * 6 + (2 * i) + 1] -
656  stat.nTotal_m * std::pow(stat.pmean_m(i), 2));
657  stat.rpsum_m(i) = (moments[(2 * i) * 6 + (2 * i) + 1] -
658  stat.nTotal_m * stat.rmean_m(i) * stat.pmean_m(i));
659  }
660  stat.tmean_m = others[0] / stat.nTotal_m;
661  stat.trms_m = sqrt(std::max(0.0, (others[1] / stat.nTotal_m - std::pow(stat.tmean_m, 2))));
662 
663  stat.eps2_m = ((stat.rsqsum_m * stat.psqsum_m - stat.rpsum_m * stat.rpsum_m) /
664  (1.0 * stat.nTotal_m * stat.nTotal_m));
665 
666  stat.rpsum_m /= stat.nTotal_m;
667 
668  for(unsigned int i = 0 ; i < 3u; i++) {
669  stat.rrms_m(i) = sqrt(std::max(0.0, stat.rsqsum_m(i)) / stat.nTotal_m);
670  stat.prms_m(i) = sqrt(std::max(0.0, stat.psqsum_m(i)) / stat.nTotal_m);
671  stat.eps_norm_m(i) = std::sqrt(std::max(0.0, stat.eps2_m(i)));
672  double tmp = stat.rrms_m(i) * stat.prms_m(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];
676  }
677 
678  stat.rprms_m = stat.rpsum_m * stat.fac_m;
679 
680  return stat;
681 }
682 
683 
684 // vi: set et ts=4 sw=4 sts=4:
685 // Local Variables:
686 // mode:c++
687 // c-basic-offset: 4
688 // indent-tabs-mode:nil
689 // End:
#define CLOSE_FILE()
static int getNodes()
Definition: IpplInfo.cpp:773
void writeHeaderH5()
bool hasTimeAttribute()
#define WRITE_STEPATTRIB_FLOAT64(attribute, value, size)
Vector_t psqsum_m
Definition: LossDataSink.h:43
#define SET_STEP()
#define WRITE_FILEATTRIB_STRING(attribute, value)
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:123
std::vector< unsigned long > startSet_m
Definition: LossDataSink.h:168
#define ERRORMSG(msg)
Definition: IpplInfo.h:399
h5_file_t H5file_m
used to write out data in H5hut mode
Definition: LossDataSink.h:143
Vector_t rmin_m
Definition: LossDataSink.h:34
#define OPEN_FILE(fname, mode, props)
std::vector< double > pz_m
Definition: LossDataSink.h:156
std::vector< long > id_m
Definition: LossDataSink.h:150
void barrier(void)
const int COMM_ANY_NODE
Definition: Communicate.h:40
static int myNode()
Definition: IpplInfo.cpp:794
std::string fn_m
Definition: LossDataSink.h:134
#define IPPL_APP_CYCLE
Definition: Tags.h:113
FRONT * fs
Definition: hypervolume.cpp:59
int next_tag(int t, int s=1000)
Definition: TagMaker.h:43
std::vector< double > z_m
Definition: LossDataSink.h:153
#define SET_NUM_PARTICLES(num)
Inform & level2(Inform &inf)
Definition: Inform.cpp:46
void save(unsigned int numSets=1, OpalData::OPENMODE openMode=OpalData::OPENMODE::UNDEFINED)
std::vector< size_t > bunchNum_m
Definition: LossDataSink.h:157
std::vector< double > px_m
Definition: LossDataSink.h:154
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
Definition: GlobalComm.hpp:55
std::string element_m
Definition: LossDataSink.h:145
std::vector< Vector_t > RefPartP_m
Definition: LossDataSink.h:163
static OpalData * getInstance()
Definition: OpalData.cpp:209
#define OPAL_PROJECT_NAME
Definition: OPALconfig.h:2
Vector_t rpsum_m
Definition: LossDataSink.h:44
Vector_t rprms_m
Definition: LossDataSink.h:40
Vector_t fac_m
Definition: LossDataSink.h:47
Vector_t rrms_m
Definition: LossDataSink.h:38
void closeASCII()
Definition: LossDataSink.h:119
void saveH5(unsigned int setIdx)
double refTime_m
Definition: LossDataSink.h:28
#define WRITE_DATA_FLOAT64(name, value)
#define WRITE_STEPATTRIB_INT64(attribute, value, size)
OPENMODE
Enum for writing to files.
Definition: OpalData.h:69
~LossDataSink() noexcept(false)
h5_int64_t H5call_m
Current record, or time step, of H5 file.
Definition: LossDataSink.h:148
std::string element_m
Definition: LossDataSink.h:26
Vector_t prms_m
Definition: LossDataSink.h:39
Vector_t rmean_m
Definition: LossDataSink.h:36
Vector_t rsqsum_m
Definition: LossDataSink.h:42
Vector_t eps2_m
Definition: LossDataSink.h:45
std::vector< double > py_m
Definition: LossDataSink.h:155
void allreduce(const T *input, T *output, int count, Op op)
Definition: GlobalComm.hpp:510
Vector_t pmean_m
Definition: LossDataSink.h:37
std::vector< double > spos_m
Definition: LossDataSink.h:166
void writeHeaderASCII()
Definition: LossDataSink.h:104
#define INFOMSG(msg)
Definition: IpplInfo.h:397
bool enableHDF5
If true HDF5 files are written.
Definition: Options.cpp:35
OPENMODE getOpenMode() const
Definition: OpalData.cpp:381
#define OPAL_PROJECT_VERSION
Definition: OPALconfig.h:5
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
void addReferenceParticle(const Vector_t &x, const Vector_t &p, double time, double spos, long long globalTrackStep)
void appendASCII()
Definition: LossDataSink.h:98
std::vector< Vector_t > RefPartR_m
Definition: LossDataSink.h:162
static MPI_Comm getComm()
Definition: IpplInfo.h:178
#define ADD_ATTACHMENT(fname)
Message & get(const T &cval)
Definition: Message.h:484
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
void splitSets(unsigned int numSets)
std::vector< size_t > turn_m
Definition: LossDataSink.h:159
Message & put(const T &val)
Definition: Message.h:414
double tmean_m
Definition: LossDataSink.h:29
Vector_t eps_norm_m
Definition: LossDataSink.h:46
std::vector< double > x_m
Definition: LossDataSink.h:151
std::ofstream os_m
Definition: LossDataSink.h:140
Vector_t rmax_m
Definition: LossDataSink.h:35
void openH5(h5_int32_t mode=H5_O_WRONLY)
#define IPPL_APP_TAG3
Definition: Tags.h:106
long double sum
Definition: Util.h:171
Vector_t RefPartP_m
Definition: LossDataSink.h:33
std::string getGitRevision()
Definition: Util.cpp:15
Vector_t RefPartR_m
Definition: LossDataSink.h:32
std::vector< double > time_m
Definition: LossDataSink.h:160
unsigned long nTotal_m
Definition: LossDataSink.h:31
std::vector< double > y_m
Definition: LossDataSink.h:152
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)
Definition: GlobalComm.hpp:477
std::vector< double > refTime_m
Definition: LossDataSink.h:165
static Communicate * Comm
Definition: IpplInfo.h:93
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)
Definition: ReductionLoc.h:95
void openASCII()
Definition: LossDataSink.h:91
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)
Definition: PETE.h:816
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define GET_NUM_STEPS()
std::vector< h5_int64_t > globalTrackStep_m
Definition: LossDataSink.h:164