OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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 
22 #include "Algorithms/Vektor.h"
26 
27 #include <boost/optional.hpp>
28 
29 #include <string>
30 #include <fstream>
31 #include <vector>
32 #include <functional>
33 #include <set>
34 
35 #include "H5hut.h"
36 
37 struct SetStatistics {
38  SetStatistics();
39 
40  std::string outputName_m;
41  double spos_m;
42  double refTime_m; // ns
43  double tmean_m; // ns
44  double trms_m; // ns
45  unsigned long nTotal_m;
62 };
63 
64 namespace std {
65  template<>
66  struct less<SetStatistics> {
67  bool operator() (const SetStatistics& x, const SetStatistics& y) const {
68  return x.spos_m < y.spos_m;
69  }
70  };
71 }
72 enum class CollectionType: unsigned short {
73  SPATIAL = 0,
74  TEMPORAL
75 };
76 
77 /*
78  - In the destructor we do ALL the file handling
79  - h5hut_mode_m defines h5hut or ASCII
80  */
81 class LossDataSink {
82  public:
83 
84  LossDataSink() = default;
85 
86  LossDataSink(std::string outfn, bool hdf5Save, CollectionType = CollectionType::TEMPORAL);
87 
88  LossDataSink(const LossDataSink &rsh);
89  ~LossDataSink() noexcept(false);
90 
91  bool inH5Mode() { return h5hut_mode_m;}
92 
93  void save(unsigned int numSets = 1, OpalData::OPENMODE openMode = OpalData::OPENMODE::UNDEFINED);
94 
95  void addReferenceParticle(const Vector_t &x,
96  const Vector_t &p,
97  double time,
98  double spos,
99  long long globalTrackStep);
100 
101  void addParticle(const OpalParticle &, const boost::optional<std::pair<int, short int>> &turnBunchNumPair = boost::none);
102 
103  size_t size() const;
104 
105  std::set<SetStatistics> computeStatistics(unsigned int numSets);
106 
107 private:
108  void openASCII() {
109  if(Ippl::myNode() == 0) {
110  os_m.open(fn_m.c_str(), std::ios::out);
111  }
112  }
113  void openH5(h5_int32_t mode = H5_O_WRONLY);
114 
115  void appendASCII() {
116  if (Ippl::myNode() == 0) {
117  os_m.open(fn_m.c_str(), std::ios::app);
118  }
119  }
120 
121  void writeHeaderASCII();
122  void writeHeaderH5();
123 
124  void saveASCII();
125  void saveH5(unsigned int setIdx);
126 
127  void closeASCII() {
128  if (Ippl::myNode() == 0) {
129  os_m.close();
130  }
131  }
132 
133  bool hasNoParticlesToDump() const;
134  bool hasTurnInformations() const;
135 
136  void reportOnError(h5_int64_t rc, const char* file, int line);
137 
138  void splitSets(unsigned int numSets);
139  SetStatistics computeSetStatistics(unsigned int setIdx);
140 
141  // filename without extension
142  std::string fn_m;
143 
144  // write either in ASCII or H5hut format
146 
147  // used to write out data in ASCII mode
148  std::ofstream os_m;
149 
151  h5_file_t H5file_m;
152 
153  std::string outputName_m;
154 
156  h5_int64_t H5call_m;
157 
158  std::vector<OpalParticle> particles_m;
159  std::vector<size_t> bunchNumber_m;
160  std::vector<size_t> turnNumber_m;
161 
162  std::vector<Vector_t> RefPartR_m;
163  std::vector<Vector_t> RefPartP_m;
164  std::vector<h5_int64_t> globalTrackStep_m;
165  std::vector<double> refTime_m;
166  std::vector<double> spos_m;
167 
168  std::vector<unsigned long> startSet_m;
169 
171 };
172 
173 inline
174 size_t LossDataSink::size() const {
175  return particles_m.size();
176 }
177 
178 inline
179 std::set<SetStatistics> LossDataSink::computeStatistics(unsigned int numStatistics) {
180  std::set<SetStatistics> stats;
181 
182  splitSets(numStatistics);
183 
184  for (unsigned int i = 0; i < numStatistics; ++ i) {
185  auto setStats = computeSetStatistics(i);
186  if (setStats.nTotal_m > 0) {
187  stats.insert(setStats);
188  }
189  }
190 
191  return stats;
192 }
193 
194 #endif
CollectionType
Definition: LossDataSink.h:72
OPENMODE
Enum for writing to files.
Definition: OpalData.h:64
Vector_t eps_norm_m
Definition: LossDataSink.h:60
Vector_t rmax_m
Definition: LossDataSink.h:49
Vector_t RefPartP_m
Definition: LossDataSink.h:47
std::string outputName_m
Definition: LossDataSink.h:40
Vector_t prms_m
Definition: LossDataSink.h:53
Vector_t pmean_m
Definition: LossDataSink.h:51
Vector_t psqsum_m
Definition: LossDataSink.h:57
double tmean_m
Definition: LossDataSink.h:43
Vector_t rmean_m
Definition: LossDataSink.h:50
unsigned long nTotal_m
Definition: LossDataSink.h:45
Vector_t rsqsum_m
Definition: LossDataSink.h:56
Vector_t eps2_m
Definition: LossDataSink.h:59
Vector_t normEmit_m
Definition: LossDataSink.h:55
Vector_t rrms_m
Definition: LossDataSink.h:52
Vector_t fac_m
Definition: LossDataSink.h:61
Vector_t RefPartR_m
Definition: LossDataSink.h:46
Vector_t rpsum_m
Definition: LossDataSink.h:58
Vector_t rmin_m
Definition: LossDataSink.h:48
double refTime_m
Definition: LossDataSink.h:42
Vector_t rprms_m
Definition: LossDataSink.h:54
std::vector< size_t > turnNumber_m
Definition: LossDataSink.h:160
std::vector< Vector_t > RefPartR_m
Definition: LossDataSink.h:162
void addReferenceParticle(const Vector_t &x, const Vector_t &p, double time, double spos, long long globalTrackStep)
void openASCII()
Definition: LossDataSink.h:108
h5_file_t H5file_m
used to write out data in H5hut mode
Definition: LossDataSink.h:151
void addParticle(const OpalParticle &, const boost::optional< std::pair< int, short int >> &turnBunchNumPair=boost::none)
~LossDataSink() noexcept(false)
size_t size() const
Definition: LossDataSink.h:174
void writeHeaderH5()
std::string fn_m
Definition: LossDataSink.h:142
std::vector< double > refTime_m
Definition: LossDataSink.h:165
std::set< SetStatistics > computeStatistics(unsigned int numSets)
Definition: LossDataSink.h:179
CollectionType collectionType_m
Definition: LossDataSink.h:170
std::string outputName_m
Definition: LossDataSink.h:153
void writeHeaderASCII()
std::ofstream os_m
Definition: LossDataSink.h:148
std::vector< Vector_t > RefPartP_m
Definition: LossDataSink.h:163
bool inH5Mode()
Definition: LossDataSink.h:91
h5_int64_t H5call_m
Current record, or time step, of H5 file.
Definition: LossDataSink.h:156
void reportOnError(h5_int64_t rc, const char *file, int line)
std::vector< size_t > bunchNumber_m
Definition: LossDataSink.h:159
std::vector< double > spos_m
Definition: LossDataSink.h:166
void openH5(h5_int32_t mode=H5_O_WRONLY)
SetStatistics computeSetStatistics(unsigned int setIdx)
void splitSets(unsigned int numSets)
std::vector< OpalParticle > particles_m
Definition: LossDataSink.h:158
void save(unsigned int numSets=1, OpalData::OPENMODE openMode=OpalData::OPENMODE::UNDEFINED)
bool hasTurnInformations() const
void appendASCII()
Definition: LossDataSink.h:115
void saveH5(unsigned int setIdx)
std::vector< h5_int64_t > globalTrackStep_m
Definition: LossDataSink.h:164
LossDataSink()=default
void closeASCII()
Definition: LossDataSink.h:127
bool hasNoParticlesToDump() const
std::vector< unsigned long > startSet_m
Definition: LossDataSink.h:168
static int myNode()
Definition: IpplInfo.cpp:691