Version: SMASH-2.0
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 
20 namespace smash {
21 
22 static constexpr int HyperSurfaceCrossing = LogArea::HyperSurfaceCrossing::id;
23 
135  const std::string &mode,
136  const std::string &name,
137  bool extended_format)
138  : OutputInterface(name), file_{path, mode}, extended_(extended_format) {
139  std::fwrite("SMSH", 4, 1, file_.get()); // magic number
140  write(format_version_); // file format version number
141  std::uint16_t format_variant = static_cast<uint16_t>(extended_);
142  write(format_variant);
143  write(VERSION_MAJOR); // SMASH version
144 }
145 
146 // write functions:
147 void BinaryOutputBase::write(const char c) {
148  std::fwrite(&c, sizeof(char), 1, file_.get());
149 }
150 
151 void BinaryOutputBase::write(const std::string &s) {
152  const auto size = boost::numeric_cast<uint32_t>(s.size());
153  std::fwrite(&size, sizeof(std::uint32_t), 1, file_.get());
154  std::fwrite(s.c_str(), s.size(), 1, file_.get());
155 }
156 
157 void BinaryOutputBase::write(const double x) {
158  std::fwrite(&x, sizeof(x), 1, file_.get());
159 }
160 
162  std::fwrite(v.begin(), sizeof(*v.begin()), 4, file_.get());
163 }
164 
165 void BinaryOutputBase::write(const Particles &particles) {
166  for (const auto &p : particles) {
168  }
169 }
170 
171 void BinaryOutputBase::write(const ParticleList &particles) {
172  for (const auto &p : particles) {
174  }
175 }
176 
178  write(p.position());
179  double mass = p.effective_mass();
180  std::fwrite(&mass, sizeof(mass), 1, file_.get());
181  write(p.momentum());
182  write(p.pdgcode().get_decimal());
183  write(p.id());
184  write(p.type().charge());
185  if (extended_) {
186  const auto history = p.get_history();
187  write(history.collisions_per_particle);
188  write(p.formation_time());
189  write(p.xsec_scaling_factor());
190  write(history.id_process);
191  write(static_cast<int32_t>(history.process_type));
192  write(history.time_last_collision);
193  write(history.p1.get_decimal());
194  write(history.p2.get_decimal());
195  }
196 }
197 
199  std::string name,
200  const OutputParameters &out_par)
202  path / ((name == "Collisions" ? "collisions_binary" : name) + ".bin"),
203  "wb", name, out_par.get_coll_extended(name)),
204  print_start_end_(out_par.coll_printstartend) {}
205 
207  const int, const EventInfo &) {
208  const char pchar = 'p';
209  if (print_start_end_) {
210  std::fwrite(&pchar, sizeof(char), 1, file_.get());
211  write(particles.size());
212  write(particles);
213  }
214 }
215 
217  const int32_t event_number,
218  const EventInfo &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(event.impact_parameter);
231  const char empty = event.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 
262 void BinaryOutputParticles::at_eventstart(const Particles &particles, const int,
263  const EventInfo &) {
264  const char pchar = 'p';
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  const EventInfo &event) {
275  const char pchar = 'p';
277  std::fwrite(&pchar, sizeof(char), 1, file_.get());
278  write(particles.size());
279  write(particles);
280  }
281 
282  // Event end line
283  const char fchar = 'f';
284  std::fwrite(&fchar, sizeof(char), 1, file_.get());
285  write(event_number);
286  write(event.impact_parameter);
287  const char empty = event.empty_event;
288  write(empty);
289 
290  // Flush to disk
291  std::fflush(file_.get());
292 }
293 
295  const std::unique_ptr<Clock> &,
296  const DensityParameters &,
297  const EventInfo &) {
298  const char pchar = 'p';
300  std::fwrite(&pchar, sizeof(char), 1, file_.get());
301  write(particles.size());
302  write(particles);
303  }
304 }
305 
307  const bf::path &path, std::string name, const OutputParameters &out_par)
308  : BinaryOutputBase(path / "SMASH_IC.bin", "wb", name, out_par.ic_extended) {
309 }
310 
312  const EventInfo &) {}
313 
315  const int event_number,
316  const EventInfo &event) {
317  // Event end line
318  const char fchar = 'f';
319  std::fwrite(&fchar, sizeof(char), 1, file_.get());
320  write(event_number);
321  write(event.impact_parameter);
322  const char empty = event.empty_event;
323  write(empty);
324 
325  // Flush to disk
326  std::fflush(file_.get());
327 
328  // If the runtime is too short some particles might not yet have
329  // reached the hypersurface. Warning is printed.
330  if (particles.size() != 0) {
332  "End time might be too small for initial conditions output. "
333  "Hypersurface has not yet been crossed by ",
334  particles.size(), " particle(s).");
335  }
336 }
337 
339  const double) {
341  const char pchar = 'p';
342  std::fwrite(&pchar, sizeof(char), 1, file_.get());
343  write(action.incoming_particles().size());
344  write(action.incoming_particles());
345  }
346 }
347 } // 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:121
smash::DensityParameters
A class to pre-calculate and store parameters relevant for density calculation.
Definition: density.h:108
smash::BinaryOutputParticles::only_final_
OutputOnlyFinal only_final_
Whether final- or initial-state particles should be written.
Definition: binaryoutput.h:240
smash::Particles::size
size_t size() const
Definition: particles.h:87
smash::ParticleData
Definition: particledata.h:52
smash::BinaryOutputParticles::at_eventstart
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.
Definition: binaryoutput.cc:262
binaryoutput.h
smash::BinaryOutputParticles::at_eventend
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.
Definition: binaryoutput.cc:272
smash::BinaryOutputParticles::at_intermediate_time
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.
Definition: binaryoutput.cc:294
smash::BinaryOutputInitialConditions::at_eventstart
void at_eventstart(const Particles &, const int, const EventInfo &) override
Writes the initial particle information of an event to the binary output.
Definition: binaryoutput.cc:311
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::BinaryOutputCollisions::at_eventend
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.
Definition: binaryoutput.cc:216
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:134
smash::EventInfo
Structure to contain custom data for output.
Definition: outputinterface.h:35
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::BinaryOutputCollisions::at_eventstart
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.
Definition: binaryoutput.cc:206
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:115
smash::BinaryOutputBase::write_particledata
void write_particledata(const ParticleData &p)
Write particle data to binary output.
Definition: binaryoutput.cc:177
smash::OutputInterface
Abstraction of generic output.
Definition: outputinterface.h:65
smash::BinaryOutputBase::write
void write(const char c)
Write byte to binary output.
Definition: binaryoutput.cc:147
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:338
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:238
smash::BinaryOutputInitialConditions::at_eventend
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.
Definition: binaryoutput.cc:314
smash::HyperSurfaceCrossing
static constexpr int HyperSurfaceCrossing
Definition: binaryoutput.cc:22
smash::BinaryOutputCollisions::print_start_end_
bool print_start_end_
Write initial and final particles additonally to collisions?
Definition: binaryoutput.h:178
smash::Particles
Definition: particles.h:33
smash::EventInfo::empty_event
bool empty_event
True if no collisions happened.
Definition: outputinterface.h:51
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::EventInfo::impact_parameter
double impact_parameter
Impact parameter for collider modus, otherwise dummy.
Definition: outputinterface.h:37
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:57
smash::BinaryOutputInitialConditions::BinaryOutputInitialConditions
BinaryOutputInitialConditions(const bf::path &path, std::string name, const OutputParameters &out_par)
Create binary initial conditions particle output.
Definition: binaryoutput.cc:306
smash::BinaryOutputParticles::BinaryOutputParticles
BinaryOutputParticles(const bf::path &path, std::string name, const OutputParameters &out_par)
Create binary particle output.
Definition: binaryoutput.cc:255
smash::NeedsToWrap::No
smash::BinaryOutputCollisions::BinaryOutputCollisions
BinaryOutputCollisions(const bf::path &path, std::string name, const OutputParameters &out_par)
Create binary particle output.
Definition: binaryoutput.cc:198
smash::BinaryOutputBase
Definition: binaryoutput.h:29