Version: SMASH-1.8
binaryoutput.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2020
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 
23 static constexpr int HyperSurfaceCrossing = LogArea::HyperSurfaceCrossing::id;
24 
136  const std::string &mode,
137  const std::string &name,
138  bool extended_format)
139  : OutputInterface(name), file_{path, mode}, extended_(extended_format) {
140  std::fwrite("SMSH", 4, 1, file_.get()); // magic number
141  write(format_version_); // file format version number
142  std::uint16_t format_variant = static_cast<uint16_t>(extended_);
143  write(format_variant);
144  write(VERSION_MAJOR); // SMASH version
145 }
146 
147 // write functions:
148 void BinaryOutputBase::write(const char c) {
149  std::fwrite(&c, sizeof(char), 1, file_.get());
150 }
151 
152 void BinaryOutputBase::write(const std::string &s) {
153  const auto size = boost::numeric_cast<uint32_t>(s.size());
154  std::fwrite(&size, sizeof(std::uint32_t), 1, file_.get());
155  std::fwrite(s.c_str(), s.size(), 1, file_.get());
156 }
157 
158 void BinaryOutputBase::write(const double x) {
159  std::fwrite(&x, sizeof(x), 1, file_.get());
160 }
161 
163  std::fwrite(v.begin(), sizeof(*v.begin()), 4, file_.get());
164 }
165 
166 void BinaryOutputBase::write(const Particles &particles) {
167  for (const auto &p : particles) {
169  }
170 }
171 
172 void BinaryOutputBase::write(const ParticleList &particles) {
173  for (const auto &p : particles) {
175  }
176 }
177 
179  write(p.position());
180  double mass = p.effective_mass();
181  std::fwrite(&mass, sizeof(mass), 1, file_.get());
182  write(p.momentum());
183  write(p.pdgcode().get_decimal());
184  write(p.id());
185  write(p.type().charge());
186  if (extended_) {
187  const auto history = p.get_history();
188  write(history.collisions_per_particle);
189  write(p.formation_time());
190  write(p.xsec_scaling_factor());
191  write(history.id_process);
192  write(static_cast<int32_t>(history.process_type));
193  write(history.time_last_collision);
194  write(history.p1.get_decimal());
195  write(history.p2.get_decimal());
196  }
197 }
198 
200  std::string name,
201  const OutputParameters &out_par)
203  path / ((name == "Collisions" ? "collisions_binary" : name) + ".bin"),
204  "wb", name, out_par.get_coll_extended(name)),
205  print_start_end_(out_par.coll_printstartend) {}
206 
208  const int) {
209  const char pchar = 'p';
210  if (print_start_end_) {
211  std::fwrite(&pchar, sizeof(char), 1, file_.get());
212  write(particles.size());
213  write(particles);
214  }
215 }
216 
218  const int32_t event_number,
219  double impact_parameter,
220  bool empty_event) {
221  const char pchar = 'p';
222  if (print_start_end_) {
223  std::fwrite(&pchar, sizeof(char), 1, file_.get());
224  write(particles.size());
225  write(particles);
226  }
227 
228  // Event end line
229  const char fchar = 'f';
230  std::fwrite(&fchar, sizeof(char), 1, file_.get());
231  write(event_number);
232  write(impact_parameter);
233  const char empty = empty_event;
234  write(empty);
235 
236  // Flush to disk
237  std::fflush(file_.get());
238 }
239 
241  const double density) {
242  const char ichar = 'i';
243  std::fwrite(&ichar, sizeof(char), 1, file_.get());
244  write(action.incoming_particles().size());
245  write(action.outgoing_particles().size());
246  std::fwrite(&density, sizeof(double), 1, file_.get());
247  const double weight = action.get_total_weight();
248  std::fwrite(&weight, sizeof(double), 1, file_.get());
249  const double partial_weight = action.get_partial_weight();
250  std::fwrite(&partial_weight, sizeof(double), 1, file_.get());
251  const auto type = static_cast<uint32_t>(action.get_type());
252  std::fwrite(&type, sizeof(uint32_t), 1, file_.get());
253  write(action.incoming_particles());
254  write(action.outgoing_particles());
255 }
256 
258  std::string name,
259  const OutputParameters &out_par)
260  : BinaryOutputBase(path / "particles_binary.bin", "wb", name,
261  out_par.part_extended),
262  only_final_(out_par.part_only_final) {}
263 
265  const int) {
266  const char pchar = 'p';
268  std::fwrite(&pchar, sizeof(char), 1, file_.get());
269  write(particles.size());
270  write(particles);
271  }
272 }
273 
275  const int event_number,
276  double impact_parameter,
277  bool empty_event) {
278  const char pchar = 'p';
279  if (!(empty_event && only_final_ == OutputOnlyFinal::IfNotEmpty)) {
280  std::fwrite(&pchar, sizeof(char), 1, file_.get());
281  write(particles.size());
282  write(particles);
283  }
284 
285  // Event end line
286  const char fchar = 'f';
287  std::fwrite(&fchar, sizeof(char), 1, file_.get());
288  write(event_number);
289  write(impact_parameter);
290  const char empty = empty_event;
291  write(empty);
292 
293  // Flush to disk
294  std::fflush(file_.get());
295 }
296 
298  const std::unique_ptr<Clock> &,
299  const DensityParameters &) {
300  const char pchar = 'p';
302  std::fwrite(&pchar, sizeof(char), 1, file_.get());
303  write(particles.size());
304  write(particles);
305  }
306 }
307 
309  const bf::path &path, std::string name, const OutputParameters &out_par)
310  : BinaryOutputBase(path / "SMASH_IC.bin", "wb", name, out_par.ic_extended) {
311 }
312 
314  const int) {}
315 
317  const int event_number,
318  double impact_parameter,
319  bool empty_event) {
320  // Event end line
321  const char fchar = 'f';
322  std::fwrite(&fchar, sizeof(char), 1, file_.get());
323  write(event_number);
324  write(impact_parameter);
325  const char empty = empty_event;
326  write(empty);
327 
328  // Flush to disk
329  std::fflush(file_.get());
330 
331  // If the runtime is too short some particles might not yet have
332  // reached the hypersurface. Warning is printed.
333  if (particles.size() != 0) {
335  "End time might be too small for initial conditions output. "
336  "Hypersurface has not yet been crossed by ",
337  particles.size(), " particle(s).");
338  }
339 }
340 
342  const double) {
344  const char pchar = 'p';
345  std::fwrite(&pchar, sizeof(char), 1, file_.get());
346  write(action.incoming_particles().size());
347  write(action.incoming_particles());
348  }
349 }
350 } // namespace smash
smash::Action::get_type
virtual ProcessType get_type() const
Get the process type.
Definition: action.h:131
smash
Definition: action.h:24
smash::BinaryOutputBase::extended_
bool extended_
Option for extended output.
Definition: binaryoutput.h:120
smash::BinaryOutputCollisions::at_eventstart
void at_eventstart(const Particles &particles, const int event_number) override
Writes the initial particle information list of an event to the binary output.
Definition: binaryoutput.cc:207
smash::DensityParameters
A class to pre-calculate and store parameters relevant for density calculation.
Definition: density.h:107
smash::BinaryOutputParticles::only_final_
OutputOnlyFinal only_final_
Whether final- or initial-state particles should be written.
Definition: binaryoutput.h:239
smash::Particles::size
size_t size() const
Definition: particles.h:87
smash::ParticleData
Definition: particledata.h:52
smash::BinaryOutputParticles::at_eventend
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: binaryoutput.cc:274
binaryoutput.h
smash::Action::get_partial_weight
virtual double get_partial_weight() const =0
Return the specific weight for the chosen outgoing channel, which is mainly used for the partial weig...
smash::FourVector::begin
iterator begin()
Definition: fourvector.h:281
smash::BinaryOutputInitialConditions::at_eventend
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: binaryoutput.cc:316
action.h
smash::BinaryOutputBase::BinaryOutputBase
BinaryOutputBase(const bf::path &path, const std::string &mode, const std::string &name, bool extended_format)
Create binary output base.
Definition: binaryoutput.cc:135
smash::logg
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > logg
An array that stores all pre-configured Logger objects.
Definition: logging.cc:39
clock.h
smash::Action::outgoing_particles
const ParticleList & outgoing_particles() const
Get the list of particles that resulted from the action.
Definition: action.h:245
smash::RenamingFilePtr::get
FILE * get()
Get the underlying FILE* pointer.
Definition: file.cc:27
OutputOnlyFinal::IfNotEmpty
Print only final-state particles, and those only if the event is not empty.
smash::OutputParameters
Helper structure for Experiment to hold output options and parameters.
Definition: outputparameters.h:25
smash::BinaryOutputBase::file_
RenamingFilePtr file_
Binary particles output file path.
Definition: binaryoutput.h:114
smash::BinaryOutputBase::write_particledata
void write_particledata(const ParticleData &p)
Write particle data to binary output.
Definition: binaryoutput.cc:178
smash::OutputInterface
Abstraction of generic output.
Definition: outputinterface.h:35
smash::BinaryOutputBase::write
void write(const char c)
Write byte to binary output.
Definition: binaryoutput.cc:148
smash::Action::get_total_weight
virtual double get_total_weight() const =0
Return the total weight value, which is mainly used for the weight output entry.
smash::BinaryOutputInitialConditions::at_interaction
void at_interaction(const Action &action, const double) override
Writes particles that are removed when crossing the hypersurface to the output.
Definition: binaryoutput.cc:341
smash::BinaryOutputCollisions::at_interaction
void at_interaction(const Action &action, const double density) override
Writes an interaction block, including information about the incoming and outgoing particles,...
Definition: binaryoutput.cc:240
smash::HyperSurfaceCrossing
static constexpr int HyperSurfaceCrossing
Definition: binaryoutput.cc:23
smash::BinaryOutputCollisions::print_start_end_
bool print_start_end_
Write initial and final particles additonally to collisions?
Definition: binaryoutput.h:178
particles.h
smash::Particles
Definition: particles.h:33
smash::Action
Definition: action.h:35
smash::ProcessType::HyperSurfaceCrossing
Hypersurface crossing Particles are removed from the evolution and printed to a separate output to se...
smash::FourVector
Definition: fourvector.h:33
smash::pdg::p
constexpr int p
Proton.
Definition: pdgcode_constants.h:28
smash::Action::incoming_particles
const ParticleList & incoming_particles() const
Get the list of particles that go into the action.
Definition: action.cc:59
smash::BinaryOutputInitialConditions::BinaryOutputInitialConditions
BinaryOutputInitialConditions(const bf::path &path, std::string name, const OutputParameters &out_par)
Create binary initial conditions particle output.
Definition: binaryoutput.cc:308
smash::BinaryOutputParticles::BinaryOutputParticles
BinaryOutputParticles(const bf::path &path, std::string name, const OutputParameters &out_par)
Create binary particle output.
Definition: binaryoutput.cc:257
smash::NeedsToWrap::No
smash::BinaryOutputCollisions::at_eventend
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.
Definition: binaryoutput.cc:217
smash::BinaryOutputParticles::at_eventstart
void at_eventstart(const Particles &particles, const int event_number) override
Writes the initial particle information of an event to the binary output.
Definition: binaryoutput.cc:264
smash::BinaryOutputInitialConditions::at_eventstart
void at_eventstart(const Particles &, const int) override
Writes the initial particle information of an event to the binary output.
Definition: binaryoutput.cc:313
smash::BinaryOutputCollisions::BinaryOutputCollisions
BinaryOutputCollisions(const bf::path &path, std::string name, const OutputParameters &out_par)
Create binary particle output.
Definition: binaryoutput.cc:199
smash::BinaryOutputBase
Definition: binaryoutput.h:28
smash::BinaryOutputParticles::at_intermediate_time
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.
Definition: binaryoutput.cc:297