OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
LossDataSink.h
Go to the documentation of this file.
1 //
2 // Class LossDataSink
3 // This class writes file attributes to describe phase space of loss files
4 //
5 // Copyright (c) 200x - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // All rights reserved
7 //
8 // This file is part of OPAL.
9 //
10 // OPAL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17 //
18 #ifndef LOSSDATASINK_H_
19 #define LOSSDATASINK_H_
20 
23 #include "Algorithms/Vektor.h"
25 
26 #include "H5hut.h"
27 
28 #include <boost/optional.hpp>
29 
30 #include <fstream>
31 #include <functional>
32 #include <set>
33 #include <string>
34 #include <vector>
35 
36 struct SetStatistics {
37  SetStatistics();
38 
39  std::string outputName_m;
40  double spos_m;
41  double refTime_m;
42  double tmean_m;
43  double trms_m;
44  unsigned long nTotal_m;
61 };
62 
63 namespace std {
64  template<>
65  struct less<SetStatistics> {
66  bool operator() (const SetStatistics& x, const SetStatistics& y) const {
67  return x.spos_m < y.spos_m;
68  }
69  };
70 }
71 enum class CollectionType: unsigned short {
72  SPATIAL = 0,
73  TEMPORAL
74 };
75 
76 /*
77  - In the destructor we do ALL the file handling
78  - h5hut_mode_m defines h5hut or ASCII
79  */
80 class LossDataSink {
81 
82 public:
83  LossDataSink() = default;
84 
85  LossDataSink(const std::string& outfn, bool hdf5Save, CollectionType = CollectionType::TEMPORAL);
86 
87  LossDataSink(const LossDataSink& rsh);
88  ~LossDataSink() noexcept(false);
89 
90  bool inH5Mode() { return h5hut_mode_m;}
91 
92  void save(unsigned int numSets = 1, OpalData::OpenMode openMode = OpalData::OpenMode::UNDEFINED);
93 
94  void addReferenceParticle(const Vector_t& x,
95  const Vector_t& p,
96  double time,
97  double spos,
98  long long globalTrackStep);
99 
100  void addParticle(const OpalParticle&, const boost::optional<std::pair<int, short int>>& turnBunchNumPair = boost::none);
101 
102  size_t size() const;
103 
104  std::set<SetStatistics> computeStatistics(unsigned int numSets);
105 
106 private:
107  void openASCII() {
108  if (Ippl::myNode() == 0) {
109  os_m.open(fileName_m.c_str(), std::ios::out);
110  }
111  }
112  void openH5(h5_int32_t mode = H5_O_WRONLY);
113 
114  void appendASCII() {
115  if (Ippl::myNode() == 0) {
116  os_m.open(fileName_m.c_str(), std::ios::app);
117  }
118  }
119 
120  void writeHeaderASCII();
121  void writeHeaderH5();
122 
123  void saveASCII();
124  void saveH5(unsigned int setIdx);
125 
126  void closeASCII() {
127  if (Ippl::myNode() == 0) {
128  os_m.close();
129  }
130  }
131 
132  bool hasNoParticlesToDump() const;
133  bool hasTurnInformations() const;
134 
135  void reportOnError(h5_int64_t rc, const char* file, int line);
136 
137  void splitSets(unsigned int numSets);
138  SetStatistics computeSetStatistics(unsigned int setIdx);
139 
140  std::string fileName_m;
141 
142  // write either in ASCII or H5hut format
144 
145  // used to write out data in ASCII mode
146  std::ofstream os_m;
147 
149  h5_file_t H5file_m;
150 
151  // filename without extension
152  std::string outputName_m;
153 
155  h5_int64_t H5call_m;
156 
157  std::vector<OpalParticle> particles_m;
158  std::vector<size_t> bunchNumber_m;
159  std::vector<size_t> turnNumber_m;
160 
161  std::vector<Vector_t> RefPartR_m;
162  std::vector<Vector_t> RefPartP_m;
163  std::vector<h5_int64_t> globalTrackStep_m;
164  std::vector<double> refTime_m;
165  std::vector<double> spos_m;
166 
167  std::vector<unsigned long> startSet_m;
168 
170 };
171 
172 inline
173 size_t LossDataSink::size() const {
174  return particles_m.size();
175 }
176 
177 inline
178 std::set<SetStatistics> LossDataSink::computeStatistics(unsigned int numStatistics) {
179  std::set<SetStatistics> stats;
180 
181  splitSets(numStatistics);
182 
183  for (unsigned int i = 0; i < numStatistics; ++ i) {
184  auto setStats = computeSetStatistics(i);
185  if (setStats.nTotal_m > 0) {
186  stats.insert(setStats);
187  }
188  }
189 
190  return stats;
191 }
192 
193 #endif
Vector_t fac_m
Definition: LossDataSink.h:60
void appendASCII()
Definition: LossDataSink.h:114
SetStatistics computeSetStatistics(unsigned int setIdx)
std::vector< size_t > bunchNumber_m
Definition: LossDataSink.h:158
bool hasNoParticlesToDump() const
std::string outputName_m
Definition: LossDataSink.h:152
Vector_t prms_m
Definition: LossDataSink.h:52
CollectionType
Definition: LossDataSink.h:71
Vector_t pmean_m
Definition: LossDataSink.h:50
void openASCII()
Definition: LossDataSink.h:107
void reportOnError(h5_int64_t rc, const char *file, int line)
static int myNode()
Definition: IpplInfo.cpp:691
Vector_t normEmit_m
Definition: LossDataSink.h:54
unsigned long nTotal_m
Definition: LossDataSink.h:44
void addReferenceParticle(const Vector_t &x, const Vector_t &p, double time, double spos, long long globalTrackStep)
double tmean_m
Definition: LossDataSink.h:42
Vector_t RefPartP_m
Definition: LossDataSink.h:46
void save(unsigned int numSets=1, OpalData::OpenMode openMode=OpalData::OpenMode::UNDEFINED)
h5_int64_t H5call_m
Current record, or time step, of H5 file.
Definition: LossDataSink.h:155
void addParticle(const OpalParticle &, const boost::optional< std::pair< int, short int >> &turnBunchNumPair=boost::none)
std::string fileName_m
Definition: LossDataSink.h:140
std::vector< h5_int64_t > globalTrackStep_m
Definition: LossDataSink.h:163
void closeASCII()
Definition: LossDataSink.h:126
std::vector< double > refTime_m
Definition: LossDataSink.h:164
double refTime_m
Definition: LossDataSink.h:41
h5_file_t H5file_m
used to write out data in H5hut mode
Definition: LossDataSink.h:149
bool inH5Mode()
Definition: LossDataSink.h:90
Vector_t rmin_m
Definition: LossDataSink.h:47
Vector_t eps2_m
Definition: LossDataSink.h:58
Vector_t rsqsum_m
Definition: LossDataSink.h:55
std::set< SetStatistics > computeStatistics(unsigned int numSets)
Definition: LossDataSink.h:178
std::vector< Vector_t > RefPartR_m
Definition: LossDataSink.h:161
if write to the Free Software Temple MA USA Also add information on how to contact you by electronic and paper mail If the program is make it output a short notice like this when it starts in an interactive mode
Definition: LICENSE:307
Vector_t RefPartR_m
Definition: LossDataSink.h:45
Vector_t eps_norm_m
Definition: LossDataSink.h:59
std::string outputName_m
Definition: LossDataSink.h:39
std::vector< OpalParticle > particles_m
Definition: LossDataSink.h:157
Vector_t rmax_m
Definition: LossDataSink.h:48
Vector_t psqsum_m
Definition: LossDataSink.h:56
void writeHeaderH5()
Vector_t rrms_m
Definition: LossDataSink.h:51
void saveH5(unsigned int setIdx)
void splitSets(unsigned int numSets)
OpenMode
Enum for writing to files.
Definition: OpalData.h:64
void writeHeaderASCII()
Vector_t rpsum_m
Definition: LossDataSink.h:57
bool hasTurnInformations() const
Vector_t rmean_m
Definition: LossDataSink.h:49
std::vector< double > spos_m
Definition: LossDataSink.h:165
std::ofstream os_m
Definition: LossDataSink.h:146
void openH5(h5_int32_t mode=H5_O_WRONLY)
Vector_t rprms_m
Definition: LossDataSink.h:53
LossDataSink()=default
std::vector< size_t > turnNumber_m
Definition: LossDataSink.h:159
std::vector< unsigned long > startSet_m
Definition: LossDataSink.h:167
std::vector< Vector_t > RefPartP_m
Definition: LossDataSink.h:162
CollectionType collectionType_m
Definition: LossDataSink.h:169
size_t size() const
Definition: LossDataSink.h:173
~LossDataSink() noexcept(false)