OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
StatWriter.cpp
Go to the documentation of this file.
1 //
2 // Class StatWriter
3 // This class writes bunch statistics (*.stat).
4 //
5 // Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // Christof Metzger-Kraus, Open Sourcerer
7 // All rights reserved
8 //
9 // This file is part of OPAL.
10 //
11 // OPAL is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
18 //
19 #include "StatWriter.h"
20 
23 #include "Utilities/Timer.h"
24 
25 #include <sstream>
26 
27 StatWriter::StatWriter(const std::string& fname, bool restart)
28  : StatBaseWriter(fname, restart)
29 { }
30 
31 
32 void StatWriter::fillHeader(const losses_t &losses) {
33 
34  if (this->hasColumns()) {
35  return;
36  }
37 
38  columns_m.addColumn("t", "double", "ns", "Time");
39  columns_m.addColumn("s", "double", "m", "Path length");
40  columns_m.addColumn("numParticles", "long", "1", "Number of Macro Particles");
41  columns_m.addColumn("charge", "double", "1", "Bunch Charge");
42  columns_m.addColumn("energy", "double", "MeV", "Mean Bunch Energy");
43 
44  columns_m.addColumn("rms_x", "double", "m", "RMS Beamsize in x");
45  columns_m.addColumn("rms_y", "double", "m", "RMS Beamsize in y");
46  columns_m.addColumn("rms_s", "double", "m", "RMS Beamsize in s");
47 
48  columns_m.addColumn("rms_px", "double", "1", "RMS Normalized Momenta in x");
49  columns_m.addColumn("rms_py", "double", "1", "RMS Normalized Momenta in y");
50  columns_m.addColumn("rms_ps", "double", "1", "RMS Normalized Momenta in s");
51 
52  columns_m.addColumn("emit_x", "double", "m", "Normalized Emittance x");
53  columns_m.addColumn("emit_y", "double", "m", "Normalized Emittance y");
54  columns_m.addColumn("emit_s", "double", "m", "Normalized Emittance s");
55 
56  columns_m.addColumn("mean_x", "double", "m", "Mean Beam Position in x");
57  columns_m.addColumn("mean_y", "double", "m", "Mean Beam Position in y");
58  columns_m.addColumn("mean_s", "double", "m", "Mean Beam Position in s");
59 
60  columns_m.addColumn("ref_x", "double", "m", "x coordinate of reference particle in lab cs");
61  columns_m.addColumn("ref_y", "double", "m", "y coordinate of reference particle in lab cs");
62  columns_m.addColumn("ref_z", "double", "m", "z coordinate of reference particle in lab cs");
63 
64  columns_m.addColumn("ref_px", "double", "1", "x momentum of reference particle in lab cs");
65  columns_m.addColumn("ref_py", "double", "1", "y momentum of reference particle in lab cs");
66  columns_m.addColumn("ref_pz", "double", "1", "z momentum of reference particle in lab cs");
67 
68  columns_m.addColumn("max_x", "double", "m", "Max Beamsize in x");
69  columns_m.addColumn("max_y", "double", "m", "Max Beamsize in y");
70  columns_m.addColumn("max_s", "double", "m", "Max Beamsize in s");
71 
72  columns_m.addColumn("xpx", "double", "1", "Correlation xpx");
73  columns_m.addColumn("ypy", "double", "1", "Correlation ypy");
74  columns_m.addColumn("zpz", "double", "1", "Correlation zpz");
75 
76  columns_m.addColumn("Dx", "double", "m", "Dispersion in x");
77  columns_m.addColumn("DDx", "double", "1", "Derivative of dispersion in x");
78  columns_m.addColumn("Dy", "double", "m", "Dispersion in y");
79  columns_m.addColumn("DDy", "double", "1", "Derivative of dispersion in y");
80 
81  columns_m.addColumn("Bx_ref", "double", "T", "Bx-Field component of ref particle");
82  columns_m.addColumn("By_ref", "double", "T", "By-Field component of ref particle");
83  columns_m.addColumn("Bz_ref", "double", "T", "Bz-Field component of ref particle");
84 
85  columns_m.addColumn("Ex_ref", "double", "MV/m", "Ex-Field component of ref particle");
86  columns_m.addColumn("Ey_ref", "double", "MV/m", "Ey-Field component of ref particle");
87  columns_m.addColumn("Ez_ref", "double", "MV/m", "Ez-Field component of ref particle");
88 
89  columns_m.addColumn("dE", "double", "MeV", "energy spread of the beam");
90  columns_m.addColumn("dt", "double", "ns", "time step size");
91  columns_m.addColumn("partsOutside", "double", "1", "outside n*sigma of the beam");
92 
93  if (OpalData::getInstance()->isInOPALCyclMode() &&
94  Ippl::getNodes() == 1) {
95  columns_m.addColumn("R0_x", "double", "m", "R0 Particle position in x");
96  columns_m.addColumn("R0_y", "double", "m", "R0 Particle position in y");
97  columns_m.addColumn("R0_s", "double", "m", "R0 Particle position in z");
98 
99  columns_m.addColumn("P0_x", "double", "1", "R0 Particle momentum in x");
100  columns_m.addColumn("P0_y", "double", "1", "R0 Particle momentum in y");
101  columns_m.addColumn("P0_s", "double", "1", "R0 Particle momentum in z");
102  }
103 
104  if (OpalData::getInstance()->isInOPALCyclMode()) {
105  columns_m.addColumn("halo_x", "double", "1", "Halo in x");
106  columns_m.addColumn("halo_y", "double", "1", "Halo in y");
107  columns_m.addColumn("halo_z", "double", "1", "Halo in z");
108 
109  columns_m.addColumn("azimuth", "double", "deg",
110  "Azimuth in global coordinates");
111  }
112 
113  for (size_t i = 0; i < losses.size(); ++ i) {
114  columns_m.addColumn(losses[i].first, "long", "1",
115  "Number of lost particles in element");
116  }
117 
118  if ( mode_m == std::ios::app )
119  return;
120 
121  OPALTimer::Timer simtimer;
122  std::string dateStr(simtimer.date());
123  std::string timeStr(simtimer.time());
124 
125  std::stringstream ss;
126  ss << "Statistics data '"
128  << "' " << dateStr << " " << timeStr;
129 
130  this->addDescription(ss.str(), "stat parameters");
131 
132  this->addDefaultParameters();
133 
134 
135  this->addInfo("ascii", 1);
136 }
137 
138 
140  const losses_t &losses, const double& azimuth,
141  const size_t npOutside)
142 {
143  double Ekin = beam->get_meanKineticEnergy();
144 
145  double pathLength = beam->get_sPos();
146 
149 
150  double Q = beam->getCharge();
151 
152  if (Ippl::myNode() != 0) {
153  return;
154  }
155 
156  fillHeader(losses);
157 
158  this->open();
159 
160  this->writeHeader();
161 
162  columns_m.addColumnValue("t", beam->getT() * 1e9); // 1
163  columns_m.addColumnValue("s", pathLength); // 2
164  columns_m.addColumnValue("numParticles", beam->getTotalNum()); // 3
165  columns_m.addColumnValue("charge", Q); // 4
166  columns_m.addColumnValue("energy", Ekin); // 5
167 
168  columns_m.addColumnValue("rms_x", beam->get_rrms()(0)); // 6
169  columns_m.addColumnValue("rms_y", beam->get_rrms()(1)); // 7
170  columns_m.addColumnValue("rms_s", beam->get_rrms()(2)); // 8
171 
172  columns_m.addColumnValue("rms_px", beam->get_prms()(0)); // 9
173  columns_m.addColumnValue("rms_py", beam->get_prms()(1)); // 10
174  columns_m.addColumnValue("rms_ps", beam->get_prms()(2)); // 11
175 
176  columns_m.addColumnValue("emit_x", beam->get_norm_emit()(0)); // 12
177  columns_m.addColumnValue("emit_y", beam->get_norm_emit()(1)); // 13
178  columns_m.addColumnValue("emit_s", beam->get_norm_emit()(2)); // 14
179 
180  columns_m.addColumnValue("mean_x", beam->get_rmean()(0) ); // 15
181  columns_m.addColumnValue("mean_y", beam->get_rmean()(1) ); // 16
182  columns_m.addColumnValue("mean_s", beam->get_rmean()(2) ); // 17
183 
184  columns_m.addColumnValue("ref_x", beam->RefPartR_m(0)); // 18
185  columns_m.addColumnValue("ref_y", beam->RefPartR_m(1)); // 19
186  columns_m.addColumnValue("ref_z", beam->RefPartR_m(2)); // 20
187 
188  columns_m.addColumnValue("ref_px", beam->RefPartP_m(0)); // 21
189  columns_m.addColumnValue("ref_py", beam->RefPartP_m(1)); // 22
190  columns_m.addColumnValue("ref_pz", beam->RefPartP_m(2)); // 23
191 
192  columns_m.addColumnValue("max_x", beam->get_maxExtent()(0)); // 24
193  columns_m.addColumnValue("max_y", beam->get_maxExtent()(1)); // 25
194  columns_m.addColumnValue("max_s", beam->get_maxExtent()(2)); // 26
195 
196  // Write out Courant Snyder parameters.
197  columns_m.addColumnValue("xpx", beam->get_rprms()(0)); // 27
198  columns_m.addColumnValue("ypy", beam->get_rprms()(1)); // 28
199  columns_m.addColumnValue("zpz", beam->get_rprms()(2)); // 29
200 
201  // Write out dispersion.
202  columns_m.addColumnValue("Dx", beam->get_Dx()); // 30
203  columns_m.addColumnValue("DDx", beam->get_DDx()); // 31
204  columns_m.addColumnValue("Dy", beam->get_Dy()); // 32
205  columns_m.addColumnValue("DDy", beam->get_DDy()); // 33
206 
207  // Write head/reference particle/tail field information.
208  columns_m.addColumnValue("Bx_ref", FDext[0](0)); // 34 B-ref x
209  columns_m.addColumnValue("By_ref", FDext[0](1)); // 35 B-ref y
210  columns_m.addColumnValue("Bz_ref", FDext[0](2)); // 36 B-ref z
211 
212  columns_m.addColumnValue("Ex_ref", FDext[1](0)); // 37 E-ref x
213  columns_m.addColumnValue("Ey_ref", FDext[1](1)); // 38 E-ref y
214  columns_m.addColumnValue("Ez_ref", FDext[1](2)); // 39 E-ref z
215 
216  columns_m.addColumnValue("dE", beam->getdE()); // 40 dE energy spread
217  columns_m.addColumnValue("dt", beam->getdT() * 1e9); // 41 dt time step size
218  columns_m.addColumnValue("partsOutside", npOutside); // 42 number of particles outside n*sigma
219 
220  if (OpalData::getInstance()->isInOPALCyclMode()) {
221  if (Ippl::getNodes() == 1) {
222  if (beam->getLocalNum() > 0) {
223  columns_m.addColumnValue("R0_x", beam->R[0](0));
224  columns_m.addColumnValue("R0_y", beam->R[0](1));
225  columns_m.addColumnValue("R0_s", beam->R[0](2));
226  columns_m.addColumnValue("P0_x", beam->P[0](0));
227  columns_m.addColumnValue("P0_y", beam->P[0](1));
228  columns_m.addColumnValue("P0_s", beam->P[0](2));
229  } else {
230  columns_m.addColumnValue("R0_x", 0.0);
231  columns_m.addColumnValue("R0_y", 0.0);
232  columns_m.addColumnValue("R0_s", 0.0);
233  columns_m.addColumnValue("P0_x", 0.0);
234  columns_m.addColumnValue("P0_y", 0.0);
235  columns_m.addColumnValue("P0_s", 0.0);
236  }
237  }
238  Vector_t halo = beam->get_halo();
239  columns_m.addColumnValue("halo_x", halo(0));
240  columns_m.addColumnValue("halo_y", halo(1));
241  columns_m.addColumnValue("halo_z", halo(2));
242 
243  columns_m.addColumnValue("azimuth", azimuth);
244  }
245 
246  for(size_t i = 0; i < losses.size(); ++ i) {
247  long unsigned int loss = losses[i].second;
248  columns_m.addColumnValue(losses[i].first, loss);
249  }
250 
251  this->writeRow();
252 
253  this->close();
254 }
double get_Dx() const
double get_meanKineticEnergy() const
ParticlePos_t & R
Vector_t RefPartP_m
size_t getLocalNum() const
size_t getTotalNum() const
double get_DDy() const
ParticleAttrib< Vector_t > P
double getdT() const
Vector_t get_rrms() const
Vector_t RefPartR_m
Vector_t get_prms() const
double getdE() const
double getCharge() const
get the total charge per simulation particle
double get_DDx() const
Vector_t get_norm_emit() const
double get_Dy() const
Vector_t get_rprms() const
Vector_t get_maxExtent() const
Vector_t get_halo() const
double get_sPos() const
double getT() const
Vector_t get_rmean() const
std::string getInputFn()
get opals input filename
Definition: OpalData.cpp:654
static OpalData * getInstance()
Definition: OpalData.cpp:195
void addColumn(const std::string &name, const std::string &type, const std::string &unit, const std::string &desc, std::ios_base::fmtflags flags=std::ios_base::scientific, unsigned short precision=15)
void addColumnValue(const std::string &name, const T &val)
Definition: SDDSColumnSet.h:63
SDDSColumnSet columns_m
Definition: SDDSWriter.h:126
bool hasColumns() const
Definition: SDDSWriter.h:207
void addDefaultParameters()
Definition: SDDSWriter.cpp:211
void addDescription(const std::string &text, const std::string &content)
Definition: SDDSWriter.h:164
void open()
Definition: SDDSWriter.cpp:134
void writeHeader()
Write SDDS header.
Definition: SDDSWriter.cpp:151
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition: SDDSWriter.h:124
void close()
Definition: SDDSWriter.cpp:144
void writeRow()
Definition: SDDSWriter.h:192
void addInfo(const std::string &mode, const size_t &no_row_counts)
Definition: SDDSWriter.h:185
std::vector< std::pair< std::string, unsigned int > > losses_t
Definition: StatWriter.h:27
virtual void write(const PartBunchBase< double, 3 > *)
Definition: SDDSWriter.h:66
void fillHeader(const losses_t &losses=losses_t())
Definition: StatWriter.cpp:32
StatWriter(const std::string &fname, bool restart)
Definition: StatWriter.cpp:27
std::string date() const
Return date.
Definition: Timer.cpp:35
std::string time() const
Return time.
Definition: Timer.cpp:42
static int getNodes()
Definition: IpplInfo.cpp:670
static int myNode()
Definition: IpplInfo.cpp:691