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