OPALX (Object Oriented Parallel Accelerator Library for Exascale) MINIorX
OPALX
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
25
26
27#include <boost/optional.hpp>
28
29#include <fstream>
30#include <functional>
31#include <set>
32#include <string>
33#include <vector>
34
35#include "H5hut.h"
36
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
64namespace std {
65 template <>
67 bool operator()(const SetStatistics& x, const SetStatistics& y) const {
68 return x.spos_m < y.spos_m;
69 }
70 };
71} // namespace std
72enum class CollectionType : unsigned short { SPATIAL = 0, TEMPORAL };
73
74/*
75 - In the destructor we do ALL the file handling
76 - h5hut_mode_m defines h5hut or ASCII
77 */
79public:
80 LossDataSink() = default;
81
82 LossDataSink(std::string outfn, bool hdf5Save, CollectionType = CollectionType::TEMPORAL);
83
84 LossDataSink(const LossDataSink& rsh);
85 ~LossDataSink() noexcept(false);
86
87 bool inH5Mode() {
88 return h5hut_mode_m;
89 }
90
91 void save(
92 unsigned int numSets = 1, OpalData::OpenMode openMode = OpalData::OpenMode::UNDEFINED);
93
95 const Vector_t<double, 3>& x, const Vector_t<double, 3>& p, double time, double spos,
96 long long globalTrackStep);
97
98 void addParticle(
99 const OpalParticle&,
100 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
106private:
107 void openASCII() {
108 if (ippl::Comm->rank() == 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::Comm->rank() == 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::Comm->rank() == 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 // filename without extension
141 std::string fileName_m;
142
143 // write either in ASCII or H5hut format
145
146 // used to write out data in ASCII mode
147 std::ofstream os_m;
148
150 h5_file_t H5file_m;
151
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<double, 3>> RefPartR_m;
162 std::vector<Vector_t<double, 3>> 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
172inline size_t LossDataSink::size() const {
173 return particles_m.size();
174}
175
176inline std::set<SetStatistics> LossDataSink::computeStatistics(unsigned int numStatistics) {
177 std::set<SetStatistics> stats;
178
179 splitSets(numStatistics);
180
181 for (unsigned int i = 0; i < numStatistics; ++i) {
182 auto setStats = computeSetStatistics(i);
183 if (setStats.nTotal_m > 0) {
184 stats.insert(setStats);
185 }
186 }
187
188 return stats;
189}
190
191#endif
CollectionType
Definition: LossDataSink.h:72
STL namespace.
static const TFunction2< bool, double > less
Definition: Expressions.cpp:90
std::unique_ptr< mpi::Communicator > Comm
Definition: Ippl.h:22
OpenMode
Enum for writing to files.
Definition: OpalData.h:58
Vector_t< double, 3 > rprms_m
Definition: LossDataSink.h:54
Vector_t< double, 3 > normEmit_m
Definition: LossDataSink.h:55
Vector_t< double, 3 > eps2_m
Definition: LossDataSink.h:59
Vector_t< double, 3 > rsqsum_m
Definition: LossDataSink.h:56
std::string outputName_m
Definition: LossDataSink.h:40
Vector_t< double, 3 > RefPartR_m
Definition: LossDataSink.h:46
Vector_t< double, 3 > RefPartP_m
Definition: LossDataSink.h:47
Vector_t< double, 3 > rrms_m
Definition: LossDataSink.h:52
Vector_t< double, 3 > eps_norm_m
Definition: LossDataSink.h:60
double tmean_m
Definition: LossDataSink.h:43
Vector_t< double, 3 > rmax_m
Definition: LossDataSink.h:49
Vector_t< double, 3 > prms_m
Definition: LossDataSink.h:53
unsigned long nTotal_m
Definition: LossDataSink.h:45
Vector_t< double, 3 > pmean_m
Definition: LossDataSink.h:51
Vector_t< double, 3 > fac_m
Definition: LossDataSink.h:61
Vector_t< double, 3 > psqsum_m
Definition: LossDataSink.h:57
Vector_t< double, 3 > rpsum_m
Definition: LossDataSink.h:58
Vector_t< double, 3 > rmin_m
Definition: LossDataSink.h:48
double refTime_m
Definition: LossDataSink.h:42
Vector_t< double, 3 > rmean_m
Definition: LossDataSink.h:50
bool operator()(const SetStatistics &x, const SetStatistics &y) const
Definition: LossDataSink.h:67
std::vector< Vector_t< double, 3 > > RefPartR_m
Definition: LossDataSink.h:161
std::vector< size_t > turnNumber_m
Definition: LossDataSink.h:159
void openASCII()
Definition: LossDataSink.h:107
h5_file_t H5file_m
used to write out data in H5hut mode
Definition: LossDataSink.h:150
~LossDataSink() noexcept(false)
size_t size() const
Definition: LossDataSink.h:172
void writeHeaderH5()
std::vector< double > refTime_m
Definition: LossDataSink.h:164
std::set< SetStatistics > computeStatistics(unsigned int numSets)
Definition: LossDataSink.h:176
CollectionType collectionType_m
Definition: LossDataSink.h:169
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
Definition: LossDataSink.h:152
void writeHeaderASCII()
void save(unsigned int numSets=1, OpalData::OpenMode openMode=OpalData::OpenMode::UNDEFINED)
std::ofstream os_m
Definition: LossDataSink.h:147
bool inH5Mode()
Definition: LossDataSink.h:87
h5_int64_t H5call_m
Current record, or time step, of H5 file.
Definition: LossDataSink.h:155
void reportOnError(h5_int64_t rc, const char *file, int line)
std::vector< size_t > bunchNumber_m
Definition: LossDataSink.h:158
std::string fileName_m
Definition: LossDataSink.h:141
std::vector< double > spos_m
Definition: LossDataSink.h:165
void addParticle(const OpalParticle &, const boost::optional< std::pair< int, short int > > &turnBunchNumPair=boost::none)
void openH5(h5_int32_t mode=H5_O_WRONLY)
SetStatistics computeSetStatistics(unsigned int setIdx)
void splitSets(unsigned int numSets)
In Opal-T monitors can be traversed several times.
std::vector< OpalParticle > particles_m
Definition: LossDataSink.h:157
bool hasTurnInformations() const
std::vector< Vector_t< double, 3 > > RefPartP_m
Definition: LossDataSink.h:162
void appendASCII()
Definition: LossDataSink.h:114
void saveH5(unsigned int setIdx)
std::vector< h5_int64_t > globalTrackStep_m
Definition: LossDataSink.h:163
LossDataSink()=default
void closeASCII()
Definition: LossDataSink.h:126
bool hasNoParticlesToDump() const
std::vector< unsigned long > startSet_m
Definition: LossDataSink.h:167