Version: SMASH-3.1
binaryoutput.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2020,2022-2024
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #include "smash/binaryoutput.h"
11 
12 #include <cstdint>
13 #include <filesystem>
14 #include <string>
15 
16 #include "smash/action.h"
17 #include "smash/clock.h"
18 #include "smash/config.h"
19 
20 namespace smash {
21 
22 static constexpr int LHyperSurfaceCrossing = LogArea::HyperSurfaceCrossing::id;
23 
136 BinaryOutputBase::BinaryOutputBase(const std::filesystem::path &path,
137  const std::string &mode,
138  const std::string &name,
139  bool extended_format)
140  : OutputInterface(name), file_{path, mode}, extended_(extended_format) {
141  std::fwrite("SMSH", 4, 1, file_.get()); // magic number
142  write(format_version_); // file format version number
143  std::uint16_t format_variant = static_cast<uint16_t>(extended_);
144  write(format_variant);
145  write(SMASH_VERSION);
146 }
147 
148 // write functions:
149 void BinaryOutputBase::write(const char c) {
150  std::fwrite(&c, sizeof(char), 1, file_.get());
151 }
152 
153 void BinaryOutputBase::write(const std::string &s) {
154  const auto size = smash::numeric_cast<uint32_t>(s.size());
155  std::fwrite(&size, sizeof(std::uint32_t), 1, file_.get());
156  std::fwrite(s.c_str(), s.size(), 1, file_.get());
157 }
158 
159 void BinaryOutputBase::write(const double x) {
160  std::fwrite(&x, sizeof(x), 1, file_.get());
161 }
162 
164  std::fwrite(v.begin(), sizeof(*v.begin()), 4, file_.get());
165 }
166 
167 void BinaryOutputBase::write(const Particles &particles) {
168  for (const auto &p : particles) {
170  }
171 }
172 
173 void BinaryOutputBase::write(const ParticleList &particles) {
174  for (const auto &p : particles) {
176  }
177 }
178 
180  write(p.position());
181  double mass = p.effective_mass();
182  std::fwrite(&mass, sizeof(mass), 1, file_.get());
183  write(p.momentum());
184  write(p.pdgcode().get_decimal());
185  write(p.id());
186  write(p.type().charge());
187  if (extended_) {
188  const auto history = p.get_history();
189  write(history.collisions_per_particle);
190  write(p.formation_time());
191  write(p.xsec_scaling_factor());
192  write(history.id_process);
193  write(static_cast<int32_t>(history.process_type));
194  write(history.time_last_collision);
195  write(history.p1.get_decimal());
196  write(history.p2.get_decimal());
197  write(p.type().baryon_number());
198  write(p.type().strangeness());
199  }
200 }
201 
203  const std::filesystem::path &path, std::string name,
204  const OutputParameters &out_par)
206  path / ((name == "Collisions" ? "collisions_binary" : name) + ".bin"),
207  "wb", name, out_par.get_coll_extended(name)),
208  print_start_end_(out_par.coll_printstartend) {}
209 
211  const int, const EventInfo &) {
212  const char pchar = 'p';
213  if (print_start_end_) {
214  std::fwrite(&pchar, sizeof(char), 1, file_.get());
215  write(particles.size());
216  write(particles);
217  }
218 }
219 
221  const int32_t event_number,
222  const EventInfo &event) {
223  const char pchar = 'p';
224  if (print_start_end_) {
225  std::fwrite(&pchar, sizeof(char), 1, file_.get());
226  write(particles.size());
227  write(particles);
228  }
229 
230  // Event end line
231  const char fchar = 'f';
232  std::fwrite(&fchar, sizeof(char), 1, file_.get());
233  write(event_number);
234  write(event.impact_parameter);
235  const char empty = event.empty_event;
236  write(empty);
237 
238  // Flush to disk
239  std::fflush(file_.get());
240 }
241 
243  const double density) {
244  const char ichar = 'i';
245  std::fwrite(&ichar, sizeof(char), 1, file_.get());
246  write(action.incoming_particles().size());
247  write(action.outgoing_particles().size());
248  std::fwrite(&density, sizeof(double), 1, file_.get());
249  const double weight = action.get_total_weight();
250  std::fwrite(&weight, sizeof(double), 1, file_.get());
251  const double partial_weight = action.get_partial_weight();
252  std::fwrite(&partial_weight, sizeof(double), 1, file_.get());
253  const auto type = static_cast<uint32_t>(action.get_type());
254  std::fwrite(&type, sizeof(uint32_t), 1, file_.get());
255  write(action.incoming_particles());
256  write(action.outgoing_particles());
257 }
258 
259 BinaryOutputParticles::BinaryOutputParticles(const std::filesystem::path &path,
260  std::string name,
261  const OutputParameters &out_par)
262  : BinaryOutputBase(path / "particles_binary.bin", "wb", name,
263  out_par.part_extended),
264  only_final_(out_par.part_only_final) {}
265 
266 void BinaryOutputParticles::at_eventstart(const Particles &particles, const int,
267  const EventInfo &) {
268  const char pchar = 'p';
270  std::fwrite(&pchar, sizeof(char), 1, file_.get());
271  write(particles.size());
272  write(particles);
273  }
274 }
275 
277  const int event_number,
278  const EventInfo &event) {
279  const char pchar = 'p';
281  std::fwrite(&pchar, sizeof(char), 1, file_.get());
282  write(particles.size());
283  write(particles);
284  }
285 
286  // Event end line
287  const char fchar = 'f';
288  std::fwrite(&fchar, sizeof(char), 1, file_.get());
289  write(event_number);
290  write(event.impact_parameter);
291  const char empty = event.empty_event;
292  write(empty);
293 
294  // Flush to disk
295  std::fflush(file_.get());
296 }
297 
299  const std::unique_ptr<Clock> &,
300  const DensityParameters &,
301  const EventInfo &) {
302  const char pchar = 'p';
304  std::fwrite(&pchar, sizeof(char), 1, file_.get());
305  write(particles.size());
306  write(particles);
307  }
308 }
309 
311  const std::filesystem::path &path, std::string name,
312  const OutputParameters &out_par)
313  : BinaryOutputBase(path / "SMASH_IC.bin", "wb", name, out_par.ic_extended) {
314 }
315 
317  const EventInfo &) {}
318 
320  const int event_number,
321  const EventInfo &event) {
322  // Event end line
323  const char fchar = 'f';
324  std::fwrite(&fchar, sizeof(char), 1, file_.get());
325  write(event_number);
326  write(event.impact_parameter);
327  const char empty = event.empty_event;
328  write(empty);
329 
330  // Flush to disk
331  std::fflush(file_.get());
332 
333  // If the runtime is too short some particles might not yet have
334  // reached the hypersurface. Warning is printed.
335  if (particles.size() != 0 && !event.impose_kinematic_cut_for_SMASH_IC) {
337  "End time might be too small for initial conditions output. "
338  "Hypersurface has not yet been crossed by ",
339  particles.size(), " particle(s).");
340  }
341 }
342 
344  const double) {
346  const char pchar = 'p';
347  std::fwrite(&pchar, sizeof(char), 1, file_.get());
348  write(action.incoming_particles().size());
349  write(action.incoming_particles());
350  }
351 }
352 } // namespace smash
Action is the base class for a generic process that takes a number of incoming particles and transfor...
Definition: action.h:35
virtual ProcessType get_type() const
Get the process type.
Definition: action.h:131
virtual double get_total_weight() const =0
Return the total weight value, which is mainly used for the weight output entry.
const ParticleList & incoming_particles() const
Get the list of particles that go into the action.
Definition: action.cc:58
virtual double get_partial_weight() const =0
Return the specific weight for the chosen outgoing channel, which is mainly used for the partial weig...
const ParticleList & outgoing_particles() const
Get the list of particles that resulted from the action.
Definition: action.h:247
Base class for SMASH binary output.
Definition: binaryoutput.h:29
BinaryOutputBase(const std::filesystem::path &path, const std::string &mode, const std::string &name, bool extended_format)
Create binary output base.
bool extended_
Option for extended output.
Definition: binaryoutput.h:122
void write(const char c)
Write byte to binary output.
void write_particledata(const ParticleData &p)
Write particle data to binary output.
const uint16_t format_version_
Binary file format version number.
Definition: binaryoutput.h:120
RenamingFilePtr file_
Binary particles output file path.
Definition: binaryoutput.h:116
void at_interaction(const Action &action, const double density) override
Writes an interaction block, including information about the incoming and outgoing particles,...
void at_eventstart(const Particles &particles, const int event_number, const EventInfo &event) override
Writes the initial particle information list of an event to the binary output.
bool print_start_end_
Write initial and final particles additonally to collisions?
Definition: binaryoutput.h:179
void at_eventend(const Particles &particles, const int32_t event_number, const EventInfo &event) override
Writes the final particle information list of an event to the binary output.
BinaryOutputCollisions(const std::filesystem::path &path, std::string name, const OutputParameters &out_par)
Create binary particle output.
void at_eventstart(const Particles &, const int, const EventInfo &) override
Writes the initial particle information of an event to the binary output.
void at_interaction(const Action &action, const double) override
Writes particles that are removed when crossing the hypersurface to the output.
void at_eventend(const Particles &particles, const int event_number, const EventInfo &event) override
Writes the final particle information of an event to the binary output.
BinaryOutputInitialConditions(const std::filesystem::path &path, std::string name, const OutputParameters &out_par)
Create binary initial conditions particle output.
void at_eventstart(const Particles &particles, const int event_number, const EventInfo &event) override
Writes the initial particle information of an event to the binary output.
OutputOnlyFinal only_final_
Whether final- or initial-state particles should be written.
Definition: binaryoutput.h:241
void at_intermediate_time(const Particles &particles, const std::unique_ptr< Clock > &clock, const DensityParameters &dens_param, const EventInfo &event) override
Writes particles at each time interval; fixed by option OUTPUT_INTERVAL.
BinaryOutputParticles(const std::filesystem::path &path, std::string name, const OutputParameters &out_par)
Create binary particle output.
void at_eventend(const Particles &particles, const int event_number, const EventInfo &event) override
Writes the final particle information of an event to the binary output.
A class to pre-calculate and store parameters relevant for density calculation.
Definition: density.h:108
The FourVector class holds relevant values in Minkowski spacetime with (+, −, −, −) metric signature.
Definition: fourvector.h:33
iterator begin()
Definition: fourvector.h:291
Abstraction of generic output.
ParticleData contains the dynamic information of a certain particle.
Definition: particledata.h:58
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
size_t size() const
Definition: particles.h:87
FILE * get()
Get the underlying FILE* pointer.
Definition: file.cc:27
@ IfNotEmpty
Print only final-state particles, and those only if the event is not empty.
@ No
Print initial, intermediate and final-state particles.
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > logg
An array that stores all pre-configured Logger objects.
Definition: logging.cc:39
constexpr int p
Proton.
Definition: action.h:24
static constexpr int LHyperSurfaceCrossing
Definition: binaryoutput.cc:22
@ HyperSurfaceCrossing
See here for a short description.
Structure to contain custom data for output.
bool empty_event
True if no collisions happened.
bool impose_kinematic_cut_for_SMASH_IC
Whether or not kinematic cuts are employed for SMASH IC.
double impact_parameter
Impact parameter for collider modus, otherwise dummy.
Helper structure for Experiment to hold output options and parameters.