OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
DataSink.cpp
Go to the documentation of this file.
1//
2// Class DataSink
3// This class acts as an observer during the calculation. It generates diagnostic
4// output of the accelerated beam such as statistical beam descriptors of particle
5// positions, momenta, beam phase space (emittance) etc. These are written to file
6// at periodic time steps during the calculation.
7//
8// This class also writes the full beam phase space to an H5 file at periodic time
9// steps in the calculation (this period is different from that of the statistical
10// numbers).
11
12// Class also writes processor load balancing data to file to track parallel
13// calculation efficiency.
14//
15// Copyright (c) 2008 - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
16// All rights reserved
17//
18// This file is part of OPAL.
19//
20// OPAL is free software: you can redistribute it and/or modify
21// it under the terms of the GNU General Public License as published by
22// the Free Software Foundation, either version 3 of the License, or
23// (at your option) any later version.
24//
25// You should have received a copy of the GNU General Public License
26// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
27//
28#include "Structure/DataSink.h"
29
30#include "OPALconfig.h"
31
33#include "Fields/Fieldmap.h"
34#include "Physics/Units.h"
38#include "Utilities/Options.h"
39#include "Utilities/Timer.h"
40#include "Utilities/Util.h"
41
42#include <sstream>
43
45 this->init();
46}
47
48DataSink::DataSink(H5PartWrapper* h5wrapper, bool restart) {
49 if (restart && !Options::enableHDF5) {
50 throw OpalException("DataSink::DataSink()", "Can not restart when HDF5 is disabled");
51 }
52
53 this->init(restart, h5wrapper);
54
55 if (restart)
57}
58
59DataSink::DataSink(H5PartWrapper* h5wrapper) : DataSink(h5wrapper, false) {
60}
61
64 return;
65
66 h5Writer_m->writePhaseSpace(beam, FDext);
67}
68
70 PartBunch_t* beam, Vector_t<double, 3> FDext[], double meanEnergy, double refPr, double refPt,
71 double refPz, double refR, double refTheta, double refZ, double azimuth, double elevation,
72 bool local) const {
74 return -1;
75
76 return h5Writer_m->writePhaseSpace(
77 beam, FDext, meanEnergy, refPr, refPt, refPz, refR, refTheta, refZ, azimuth, elevation,
78 local);
79}
80
82 PartBunch_t* beam, Vector_t<double, 3> FDext[], const double& azimuth) const {
83 this->dumpSDDS(beam, FDext, losses_t(), azimuth);
84}
85
87 PartBunch_t* beam, Vector_t<double, 3> FDext[], const losses_t& losses,
88 const double& azimuth) const {
89 beam->calcBeamParameters();
90
91 size_t npOutside = 0;
93 npOutside = beam->calcNumPartsOutside(Options::beamHaloBoundary * beam->get_rrms());
94
95 IpplTimings::startTimer(StatMarkerTimer_m);
96
97 statWriter_m->write(beam, FDext, losses, azimuth, npOutside);
98
100
101 //for (size_t i = 0; i < sddsWriter_m.size(); ++i)
102 // sddsWriter_m[i]->write(beam);
103
104 IpplTimings::stopTimer(StatMarkerTimer_m);
105}
106
109 return;
110
111 h5Writer_m->storeCavityInformation();
112}
113
116 return;
117
118 h5Writer_m->changeH5Wrapper(h5wrapper);
119}
120
122 if (ippl::Comm->rank() == 0 && Options::enableVTK) {
123 bg.writeGeomToVtk(fn);
124 }
125}
126
128 const PartBunch_t* beam, long long& step, size_t& impact, double& sey_num,
129 size_t numberOfFieldEmittedParticles, bool nEmissionMode, std::string fn) {
130 double charge = 0.0;
131 size_t Npart = 0;
132 double Npart_d = 0.0;
133 if (!nEmissionMode) {
134 charge = -1.0 * beam->getCharge();
135 // reduce(charge, charge, OpAddAssign());
136 Npart_d = -1.0 * charge / beam->getChargePerParticle();
137 } else {
138 Npart = beam->getTotalNum();
139 }
140 if (ippl::Comm->rank() == 0) {
141 std::string ffn = fn + std::string(".dat");
142
143 std::unique_ptr<Inform> ofp(new Inform(nullptr, ffn.c_str(), Inform::APPEND, 0));
144 Inform& fid = *ofp;
145 fid.precision(6);
146 fid << std::setiosflags(std::ios::scientific);
147 double t = beam->getT() * Units::s2ns;
148 if (!nEmissionMode) {
149 if (step == 0) {
150 fid << "#Time/ns" << std::setw(18) << "#Geometry impacts" << std::setw(18)
151 << "tot_sey" << std::setw(18) << "TotalCharge" << std::setw(18) << "PartNum"
152 << " numberOfFieldEmittedParticles " << endl;
153 }
154 fid << t << std::setw(18) << impact << std::setw(18) << sey_num << std::setw(18)
155 << charge << std::setw(18) << Npart_d << std::setw(18)
156 << numberOfFieldEmittedParticles << endl;
157 } else {
158 if (step == 0) {
159 fid << "#Time/ns" << std::setw(18) << "#Geometry impacts" << std::setw(18)
160 << "tot_sey" << std::setw(18) << "ParticleNumber"
161 << " numberOfFieldEmittedParticles " << endl;
162 }
163 fid << t << std::setw(18) << impact << std::setw(18) << sey_num << std::setw(18)
164 << double(Npart) << std::setw(18) << numberOfFieldEmittedParticles << endl;
165 }
166 }
167}
168
170 unsigned int linesToRewind = 0;
171
172 double spos = h5Writer_m->getLastPosition();
173 if (statWriter_m->exists()) {
174 // use stat file to get position
175 linesToRewind = statWriter_m->rewindToSpos(spos);
176 statWriter_m->replaceVersionString();
177 }
178 h5Writer_m->close();
179
180 // rewind all others
181 if (linesToRewind > 0) {
182 for (size_t i = 0; i < sddsWriter_m.size(); ++i) {
183 sddsWriter_m[i]->rewindLines(linesToRewind);
184 sddsWriter_m[i]->replaceVersionString();
185 }
186 }
187}
188
189void DataSink::init(bool restart, H5PartWrapper* h5wrapper) {
190 std::string fn = OpalData::getInstance()->getInputBasename();
191
192 lossWrCounter_m = 0;
193 StatMarkerTimer_m = IpplTimings::getTimer("Write Stat");
194
195 statWriter_m = statWriter_t(new StatWriter(fn + std::string(".stat"), restart));
196
197 sddsWriter_m.push_back(sddsWriter_t(new LBalWriter(fn + std::string(".lbal"), restart)));
198
200 h5Writer_m = h5Writer_t(new H5Writer(h5wrapper, restart));
201 }
202}
PartBunch< PLayout_t< double, 3 >, double, 3 > PartBunch_t
ippl::Vector< T, Dim > Vector_t
constexpr double s2ns
Definition Units.h:44
bool enableVTK
If true VTK files are written.
Definition Options.cpp:83
bool enableHDF5
If true HDF5 files are written.
Definition Options.cpp:81
double beamHaloBoundary
Definition Options.cpp:89
std::string getInputBasename()
get input file name without extension
Definition OpalData.cpp:685
static OpalData * getInstance()
Definition OpalData.cpp:195
void gatherLoadBalanceStatistics()
size_t calcNumPartsOutside(Vector_t< T, Dim > x)
returns the number of particles outside of a box defined by x
void calcBeamParameters()
double getT() const
double getCharge() const
get the total charge per simulation particle
Vector_t< T, Dim > get_rrms() const
double getChargePerParticle() const
get the macro particle charge
size_t getTotalNum() const
void writeGeomToVtk(std::string fn)
void rewindLines()
Definition DataSink.cpp:169
std::vector< sddsWriter_t > sddsWriter_m
Definition DataSink.h:109
void init(bool restart=false, H5PartWrapper *h5wrapper=nullptr)
Definition DataSink.cpp:189
void changeH5Wrapper(H5PartWrapper *h5wrapper)
Definition DataSink.cpp:114
StatWriter::losses_t losses_t
Definition DataSink.h:46
void writeImpactStatistics(const PartBunch_t *beam, long long int &step, size_t &impact, double &sey_num, size_t numberOfFieldEmittedParticles, bool nEmissionMode, std::string fn)
Definition DataSink.cpp:127
void writeGeomToVtk(BoundaryGeometry &bg, std::string fn)
Definition DataSink.cpp:121
std::unique_ptr< SDDSWriter > sddsWriter_t
Definition DataSink.h:48
void dumpH5(PartBunch_t *beam, Vector_t< double, 3 > FDext[]) const
Definition DataSink.cpp:62
void storeCavityInformation()
Write cavity information from H5 file.
Definition DataSink.cpp:107
void dumpSDDS(PartBunch_t *beam, Vector_t< double, 3 > FDext[], const double &azimuth=-1) const
Definition DataSink.cpp:81
IpplTimings::TimerRef StatMarkerTimer_m
Timer to track statistics write time.
Definition DataSink.h:117
std::unique_ptr< H5Writer > h5Writer_t
Definition DataSink.h:49
unsigned int lossWrCounter_m
needed to create index for vtk file
Definition DataSink.h:114
statWriter_t statWriter_m
Definition DataSink.h:108
DataSink()
Default constructor.
Definition DataSink.cpp:44
h5Writer_t h5Writer_m
Definition DataSink.h:107
std::unique_ptr< StatWriter > statWriter_t
Definition DataSink.h:47
The base class for all OPAL exceptions.