OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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) 2026, 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
24
25#include "H5hut.h"
26
27#include <cstddef>
28#include <fstream>
29#include <functional>
30#include <optional>
31#include <set>
32#include <string>
33#include <utility>
34#include <vector>
35
79
80namespace std {
81 template <>
82 struct less<SetStatistics> {
83 bool operator()(const SetStatistics& x, const SetStatistics& y) const {
84 return x.spos_m < y.spos_m;
85 }
86 };
87} // namespace std
88enum class CollectionType : unsigned short { SPATIAL = 0, TEMPORAL };
89
90/*
91 - In the destructor we do ALL the file handling
92 - h5hut_mode_m defines h5hut or ASCII
93 */
95public:
96 LossDataSink() = default;
97
98 LossDataSink(std::string outfn, bool hdf5Save, CollectionType = CollectionType::TEMPORAL);
99
100 LossDataSink(const LossDataSink& rsh);
101 ~LossDataSink() noexcept(false);
102
103 bool inH5Mode() const { return h5hut_mode_m; }
104
105 void save(
106 unsigned int numSets = 1, OpalData::OpenMode openMode = OpalData::OpenMode::UNDEFINED);
107
109 const Vector_t<double, 3>& x, const Vector_t<double, 3>& p, double time, double spos,
110 long long globalTrackStep);
111
112 void addParticle(
113 const OpalParticle&,
114 const std::optional<std::pair<int, short int>>& turnBunchNumPair = std::nullopt);
115
116 size_t size() const;
117
118 std::set<SetStatistics> computeStatistics(unsigned int numSets);
119
120private:
121 void writeFileAttribString(const char* attribute, const char* value);
122 void writeStepAttribFloat64(const char* attribute, const h5_float64_t* value, h5_int64_t size);
123 void writeStepAttribInt64(const char* attribute, const h5_int64_t* value, h5_int64_t size);
124 void writeDataFloat64(const char* name, const h5_float64_t* value);
125 void writeDataInt64(const char* name, const h5_int64_t* value);
126
127 void setStep();
128 void getNumSteps();
129 void setNumParticles(h5_int64_t num);
130
131 void openFile(const char* fname, h5_int32_t mode, h5_prop_t props);
132 void closeFile();
133
134 void openASCII() {
135 if (ippl::Comm->rank() == 0) {
136 os_m.open(fileName_m.c_str(), std::ios::out);
137 }
138 }
139 void openH5(h5_int32_t mode = H5_O_WRONLY);
140
141 void appendASCII() {
142 if (ippl::Comm->rank() == 0) {
143 os_m.open(fileName_m.c_str(), std::ios::app);
144 }
145 }
146
147 void writeHeaderASCII();
148 void writeHeaderH5();
149
150 void saveASCII();
151 void saveH5(unsigned int setIdx);
152
153 void closeASCII() {
154 if (ippl::Comm->rank() == 0) {
155 os_m.close();
156 }
157 }
158
159 bool hasNoParticlesToDump() const;
160 bool hasTurnInformations() const;
161
162 void splitSets(unsigned int numSets);
163 SetStatistics computeSetStatistics(unsigned int setIdx);
164
165 // filename without extension
166 std::string fileName_m;
167
168 // write either in ASCII or H5hut format
169 bool h5hut_mode_m = false;
170
171 // used to write out data in ASCII mode
172 std::ofstream os_m;
173
175 h5_file_t H5file_m = 0;
176
177 std::string outputName_m;
178
180 h5_int64_t H5call_m = 0;
181
182 std::vector<OpalParticle> particles_m;
183 std::vector<size_t> bunchNumber_m;
184 std::vector<size_t> turnNumber_m;
185
186 std::vector<Vector_t<double, 3>> RefPartR_m;
187 std::vector<Vector_t<double, 3>> RefPartP_m;
188 std::vector<h5_int64_t> globalTrackStep_m;
189 std::vector<double> refTime_m;
190 std::vector<double> spos_m;
191
192 std::vector<unsigned long> startSet_m;
193
195};
196
197inline size_t LossDataSink::size() const { return particles_m.size(); }
198
199inline std::set<SetStatistics> LossDataSink::computeStatistics(unsigned int numStatistics) {
200 std::set<SetStatistics> stats;
201
202 splitSets(numStatistics);
203
204 for (unsigned int i = 0; i < numStatistics; ++i) {
205 auto setStats = computeSetStatistics(i);
206 if (setStats.nTotal_m > 0) {
207 stats.insert(setStats);
208 }
209 }
210
211 return stats;
212}
213
214#endif
ippl::Vector< T, Dim > Vector_t
CollectionType
std::vector< Vector_t< double, 3 > > RefPartR_m
std::vector< size_t > turnNumber_m
h5_file_t H5file_m
used to write out data in H5hut mode
~LossDataSink() noexcept(false)
size_t size() const
void setNumParticles(h5_int64_t num)
std::vector< double > refTime_m
void writeStepAttribFloat64(const char *attribute, const h5_float64_t *value, h5_int64_t size)
std::set< SetStatistics > computeStatistics(unsigned int numSets)
CollectionType collectionType_m
void addReferenceParticle(const Vector_t< double, 3 > &x, const Vector_t< double, 3 > &p, double time, double spos, long long globalTrackStep)
std::string outputName_m
void writeHeaderASCII()
void save(unsigned int numSets=1, OpalData::OpenMode openMode=OpalData::OpenMode::UNDEFINED)
std::ofstream os_m
h5_int64_t H5call_m
Current record, or time step, of H5 file.
void openFile(const char *fname, h5_int32_t mode, h5_prop_t props)
void addParticle(const OpalParticle &, const std::optional< std::pair< int, short int > > &turnBunchNumPair=std::nullopt)
std::vector< size_t > bunchNumber_m
bool inH5Mode() const
std::string fileName_m
void writeStepAttribInt64(const char *attribute, const h5_int64_t *value, h5_int64_t size)
void writeFileAttribString(const char *attribute, const char *value)
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
std::vector< Vector_t< double, 3 > > RefPartP_m
void appendASCII()
void writeDataFloat64(const char *name, const h5_float64_t *value)
void saveH5(unsigned int setIdx)
void writeDataInt64(const char *name, const h5_int64_t *value)
std::vector< h5_int64_t > globalTrackStep_m
LossDataSink()=default
void closeASCII()
bool hasNoParticlesToDump() const
std::vector< unsigned long > startSet_m
OpenMode
Enum for writing to files.
Definition OpalData.h:58
STL namespace.
Vector_t< double, 3 > rprms_m
Vector_t< double, 3 > normalizedEps68Percentile_m
double stdKineticEnergy_m
Vector_t< double, 3 > normEmit_m
Vector_t< double, 3 > eps2_m
Vector_t< double, 3 > rsqsum_m
Vector_t< double, 3 > maxR_m
std::string outputName_m
Vector_t< double, 3 > RefPartR_m
Vector_t< double, 3 > RefPartP_m
Vector_t< double, 3 > rrms_m
Vector_t< double, 3 > ninetyNinePercentile_m
Vector_t< double, 3 > ninetyFivePercentile_m
Vector_t< double, 3 > eps_norm_m
Vector_t< double, 3 > ninetyNine_NinetyNinePercentile_m
Vector_t< double, 3 > geomEmit_m
Vector_t< double, 3 > rmax_m
Vector_t< double, 3 > prms_m
double meanKineticEnergy_m
double totalMass_m
double totalCharge_m
double meanGamma_m
unsigned long nTotal_m
Vector_t< double, 3 > sixtyEightPercentile_m
Vector_t< double, 3 > pmean_m
Vector_t< double, 3 > fac_m
Vector_t< double, 3 > normalizedEps95Percentile_m
Vector_t< double, 3 > psqsum_m
Vector_t< double, 3 > rpsum_m
Vector_t< double, 3 > normalizedEps99Percentile_m
Vector_t< double, 3 > normalizedEps99_99Percentile_m
Vector_t< double, 3 > rmin_m
Vector_t< double, 3 > rmean_m
bool operator()(const SetStatistics &x, const SetStatistics &y) const