Version: SMASH-1.7
binaryoutput.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2019
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #include "smash/binaryoutput.h"
11 
12 #include <string>
13 
14 #include <boost/filesystem.hpp>
15 
16 #include "smash/action.h"
17 #include "smash/clock.h"
18 #include "smash/config.h"
19 #include "smash/particles.h"
20 
21 namespace smash {
22 
134  const std::string &mode,
135  const std::string &name,
136  bool extended_format)
137  : OutputInterface(name), file_{path, mode}, extended_(extended_format) {
138  std::fwrite("SMSH", 4, 1, file_.get()); // magic number
139  write(format_version_); // file format version number
140  std::uint16_t format_variant = static_cast<uint16_t>(extended_);
141  write(format_variant);
142  write(VERSION_MAJOR); // SMASH version
143 }
144 
145 // write functions:
146 void BinaryOutputBase::write(const char c) {
147  std::fwrite(&c, sizeof(char), 1, file_.get());
148 }
149 
150 void BinaryOutputBase::write(const std::string &s) {
151  const auto size = boost::numeric_cast<uint32_t>(s.size());
152  std::fwrite(&size, sizeof(std::uint32_t), 1, file_.get());
153  std::fwrite(s.c_str(), s.size(), 1, file_.get());
154 }
155 
156 void BinaryOutputBase::write(const double x) {
157  std::fwrite(&x, sizeof(x), 1, file_.get());
158 }
159 
161  std::fwrite(v.begin(), sizeof(*v.begin()), 4, file_.get());
162 }
163 
164 void BinaryOutputBase::write(const Particles &particles) {
165  for (const auto &p : particles) {
167  }
168 }
169 
170 void BinaryOutputBase::write(const ParticleList &particles) {
171  for (const auto &p : particles) {
173  }
174 }
175 
177  write(p.position());
178  double mass = p.effective_mass();
179  std::fwrite(&mass, sizeof(mass), 1, file_.get());
180  write(p.momentum());
181  write(p.pdgcode().get_decimal());
182  write(p.id());
183  write(p.type().charge());
184  if (extended_) {
185  const auto history = p.get_history();
186  write(history.collisions_per_particle);
187  write(p.formation_time());
189  write(history.id_process);
190  write(static_cast<int32_t>(history.process_type));
191  write(history.time_last_collision);
192  write(history.p1.get_decimal());
193  write(history.p2.get_decimal());
194  }
195 }
196 
198  std::string name,
199  const OutputParameters &out_par)
201  path / ((name == "Collisions" ? "collisions_binary" : name) + ".bin"),
202  "wb", name, out_par.get_coll_extended(name)),
203  print_start_end_(out_par.coll_printstartend) {}
204 
206  const int) {
207  const char pchar = 'p';
208  if (print_start_end_) {
209  std::fwrite(&pchar, sizeof(char), 1, file_.get());
210  write(particles.size());
211  write(particles);
212  }
213 }
214 
216  const int32_t event_number,
217  double impact_parameter,
218  bool empty_event) {
219  const char pchar = 'p';
220  if (print_start_end_) {
221  std::fwrite(&pchar, sizeof(char), 1, file_.get());
222  write(particles.size());
223  write(particles);
224  }
225 
226  // Event end line
227  const char fchar = 'f';
228  std::fwrite(&fchar, sizeof(char), 1, file_.get());
229  write(event_number);
230  write(impact_parameter);
231  const char empty = empty_event;
232  write(empty);
233 
234  // Flush to disk
235  std::fflush(file_.get());
236 }
237 
239  const double density) {
240  const char ichar = 'i';
241  std::fwrite(&ichar, sizeof(char), 1, file_.get());
242  write(action.incoming_particles().size());
243  write(action.outgoing_particles().size());
244  std::fwrite(&density, sizeof(double), 1, file_.get());
245  const double weight = action.get_total_weight();
246  std::fwrite(&weight, sizeof(double), 1, file_.get());
247  const double partial_weight = action.get_partial_weight();
248  std::fwrite(&partial_weight, sizeof(double), 1, file_.get());
249  const auto type = static_cast<uint32_t>(action.get_type());
250  std::fwrite(&type, sizeof(uint32_t), 1, file_.get());
251  write(action.incoming_particles());
252  write(action.outgoing_particles());
253 }
254 
256  std::string name,
257  const OutputParameters &out_par)
258  : BinaryOutputBase(path / "particles_binary.bin", "wb", name,
259  out_par.part_extended),
260  only_final_(out_par.part_only_final) {}
261 
263  const int) {
264  const char pchar = 'p';
265  if (!only_final_) {
266  std::fwrite(&pchar, sizeof(char), 1, file_.get());
267  write(particles.size());
268  write(particles);
269  }
270 }
271 
273  const int event_number,
274  double impact_parameter,
275  bool empty_event) {
276  const char pchar = 'p';
277  std::fwrite(&pchar, sizeof(char), 1, file_.get());
278  write(particles.size());
279  write(particles);
280 
281  // Event end line
282  const char fchar = 'f';
283  std::fwrite(&fchar, sizeof(char), 1, file_.get());
284  write(event_number);
285  write(impact_parameter);
286  const char empty = empty_event;
287  write(empty);
288 
289  // Flush to disk
290  std::fflush(file_.get());
291 }
292 
294  const std::unique_ptr<Clock> &,
295  const DensityParameters &) {
296  const char pchar = 'p';
297  if (!only_final_) {
298  std::fwrite(&pchar, sizeof(char), 1, file_.get());
299  write(particles.size());
300  write(particles);
301  }
302 }
303 
305  const bf::path &path, std::string name, const OutputParameters &out_par)
306  : BinaryOutputBase(path / "SMASH_IC.bin", "wb", name, out_par.ic_extended) {
307 }
308 
310  const int) {}
311 
313  const int event_number,
314  double impact_parameter,
315  bool empty_event) {
316  const auto &log = logger<LogArea::HyperSurfaceCrossing>();
317 
318  // Event end line
319  const char fchar = 'f';
320  std::fwrite(&fchar, sizeof(char), 1, file_.get());
321  write(event_number);
322  write(impact_parameter);
323  const char empty = empty_event;
324  write(empty);
325 
326  // Flush to disk
327  std::fflush(file_.get());
328 
329  // If the runtime is too short some particles might not yet have
330  // reached the hypersurface. Warning is printed.
331  if (particles.size() != 0) {
332  log.warn(
333  "End time might be too small for initial conditions output. "
334  "Hypersurface has not yet been crossed by ",
335  particles.size(), " particle(s).");
336  }
337 }
338 
340  const double) {
342  const char pchar = 'p';
343  std::fwrite(&pchar, sizeof(char), 1, file_.get());
344  write(action.incoming_particles().size());
345  write(action.incoming_particles());
346  }
347 }
348 } // namespace smash
double formation_time() const
Get the absolute formation time of the particle.
Definition: particledata.h:217
A class to pre-calculate and store parameters relevant for density calculation.
Definition: density.h:106
PdgCode pdgcode() const
Get the pdgcode of the particle.
Definition: particledata.h:81
void at_interaction(const Action &action, const double density) override
Writes an interaction block, including information about the incoming and outgoing particles...
const FourVector & position() const
Get the particle&#39;s position in Minkowski space.
Definition: particledata.h:185
BinaryOutputCollisions(const bf::path &path, std::string name, const OutputParameters &out_par)
Create binary particle output.
FILE * get()
Get the underlying FILE* pointer.
Definition: file.cc:27
RenamingFilePtr file_
Binary particles output file path.
Definition: binaryoutput.h:114
void write_particledata(const ParticleData &p)
Write particle data to binary output.
virtual double get_partial_weight() const =0
Return the specific weight for the chosen outgoing channel, which is mainly used for the partial weig...
virtual ProcessType get_type() const
Get the process type.
Definition: action.h:130
Hypersurface crossing Particles are removed from the evolution and printed to a separate output to se...
void at_eventstart(const Particles &particles, const int event_number) override
Writes the initial particle information of an event to the binary output.
double effective_mass() const
Get the particle&#39;s effective mass.
Definition: particledata.cc:21
void at_eventend(const Particles &particles, const int event_number, double impact_parameter, bool empty_event) override
Writes the final particle information of an event to the binary output.
HistoryData get_history() const
Get history information.
Definition: particledata.h:120
void at_eventend(const Particles &particles, const int32_t event_number, double impact_parameter, bool empty_event) override
Writes the final particle information list of an event to the binary output.
double xsec_scaling_factor(double delta_time=0.) const
Return the cross section scaling factor at a given time.
Definition: particledata.cc:73
size_t size() const
Definition: particles.h:87
Helper structure for Experiment to hold output options and parameters.
Base class for SMASH binary output.
Definition: binaryoutput.h:28
void at_interaction(const Action &action, const double) override
Writes particles that are removed when crossing the hypersurface to the output.
const ParticleType & type() const
Get the type of the particle.
Definition: particledata.h:109
Action is the base class for a generic process that takes a number of incoming particles and transfor...
Definition: action.h:34
virtual double get_total_weight() const =0
Return the total weight value, which is mainly used for the weight output entry.
int32_t charge() const
The charge of the particle.
Definition: particletype.h:188
BinaryOutputBase(const bf::path &path, const std::string &mode, const std::string &name, bool extended_format)
Create binary output base.
void at_eventstart(const Particles &particles, const int event_number) override
Writes the initial particle information list of an event to the binary output.
constexpr int p
Proton.
BinaryOutputInitialConditions(const bf::path &path, std::string name, const OutputParameters &out_par)
Create binary initial conditions particle output.
bool print_start_end_
Write initial and final particles additonally to collisions?
Definition: binaryoutput.h:178
const ParticleList & incoming_particles() const
Get the list of particles that go into the action.
Definition: action.cc:58
void at_eventstart(const Particles &, const int) override
Writes the initial particle information of an event to the binary output.
void write(const char c)
Write byte to binary output.
bool only_final_
Write only final particles (True) or both, inital and final (False).
Definition: binaryoutput.h:239
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
const uint16_t format_version_
Binary file format version number.
Definition: binaryoutput.h:118
const ParticleList & outgoing_particles() const
Get the list of particles that resulted from the action.
Definition: action.h:244
int32_t id() const
Get the id of the particle.
Definition: particledata.h:70
The FourVector class holds relevant values in Minkowski spacetime with (+, −, −, −) metric signature.
Definition: fourvector.h:33
int32_t get_decimal() const
Definition: pdgcode.h:655
BinaryOutputParticles(const bf::path &path, std::string name, const OutputParameters &out_par)
Create binary particle output.
ParticleData contains the dynamic information of a certain particle.
Definition: particledata.h:52
Abstraction of generic output.
void at_eventend(const Particles &particles, const int event_number, double impact_parameter, bool empty_event) override
Writes the final particle information of an event to the binary output.
Definition: action.h:24
const FourVector & momentum() const
Get the particle&#39;s 4-momentum.
Definition: particledata.h:139
iterator begin()
Definition: fourvector.h:281
void at_intermediate_time(const Particles &particles, const std::unique_ptr< Clock > &clock, const DensityParameters &dens_param) override
Writes particles at each time interval; fixed by option OUTPUT_INTERVAL.
bool extended_
Option for extended output.
Definition: binaryoutput.h:120