Version: SMASH-1.5
binaryoutput.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2018
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 
117  const std::string &mode,
118  const std::string &name,
119  bool extended_format)
120  : OutputInterface(name), file_{path, mode}, extended_(extended_format) {
121  std::fwrite("SMSH", 4, 1, file_.get()); // magic number
122  write(format_version_); // file format version number
123  std::uint16_t format_variant = static_cast<uint16_t>(extended_);
124  write(format_variant);
125  write(VERSION_MAJOR); // SMASH version
126 }
127 
128 // write functions:
129 void BinaryOutputBase::write(const std::string &s) {
130  const auto size = boost::numeric_cast<uint32_t>(s.size());
131  std::fwrite(&size, sizeof(std::uint32_t), 1, file_.get());
132  std::fwrite(s.c_str(), s.size(), 1, file_.get());
133 }
134 
135 void BinaryOutputBase::write(const double x) {
136  std::fwrite(&x, sizeof(x), 1, file_.get());
137 }
138 
140  std::fwrite(v.begin(), sizeof(*v.begin()), 4, file_.get());
141 }
142 
143 void BinaryOutputBase::write(const Particles &particles) {
144  for (const auto &p : particles) {
146  }
147 }
148 
149 void BinaryOutputBase::write(const ParticleList &particles) {
150  for (const auto &p : particles) {
152  }
153 }
154 
156  write(p.position());
157  double mass = p.effective_mass();
158  std::fwrite(&mass, sizeof(mass), 1, file_.get());
159  write(p.momentum());
160  write(p.pdgcode().get_decimal());
161  write(p.id());
162  write(p.type().charge());
163  if (extended_) {
164  const auto history = p.get_history();
165  write(history.collisions_per_particle);
166  write(p.formation_time());
167  write(p.xsec_scaling_factor());
168  write(history.id_process);
169  write(static_cast<uint32_t>(history.process_type));
170  write(history.time_last_collision);
171  write(history.p1.get_decimal());
172  write(history.p2.get_decimal());
173  }
174 }
175 
177  std::string name,
178  const OutputParameters &out_par)
180  path / ((name == "Collisions" ? "collisions_binary" : name) + ".bin"),
181  "wb", name, out_par.get_coll_extended(name)),
182  print_start_end_(out_par.coll_printstartend) {}
183 
185  const int) {
186  char pchar = 'p';
187  if (print_start_end_) {
188  std::fwrite(&pchar, sizeof(char), 1, file_.get());
189  write(particles.size());
190  write(particles);
191  }
192 }
193 
195  const int event_number,
196  double impact_parameter) {
197  char pchar = 'p';
198  if (print_start_end_) {
199  std::fwrite(&pchar, sizeof(char), 1, file_.get());
200  write(particles.size());
201  write(particles);
202  }
203 
204  // Event end line
205  char fchar = 'f';
206  std::fwrite(&fchar, sizeof(char), 1, file_.get());
207  write(event_number);
208  write(impact_parameter);
209 
210  // Flush to disk
211  std::fflush(file_.get());
212 }
213 
215  const double density) {
216  char ichar = 'i';
217  std::fwrite(&ichar, sizeof(char), 1, file_.get());
218  write(action.incoming_particles().size());
219  write(action.outgoing_particles().size());
220  std::fwrite(&density, sizeof(double), 1, file_.get());
221  const double weight = action.get_total_weight();
222  std::fwrite(&weight, sizeof(double), 1, file_.get());
223  const double partial_weight = action.get_partial_weight();
224  std::fwrite(&partial_weight, sizeof(double), 1, file_.get());
225  const auto type = static_cast<uint32_t>(action.get_type());
226  std::fwrite(&type, sizeof(uint32_t), 1, file_.get());
227  write(action.incoming_particles());
228  write(action.outgoing_particles());
229 }
230 
232  std::string name,
233  const OutputParameters &out_par)
234  : BinaryOutputBase(path / "particles_binary.bin", "wb", name,
235  out_par.part_extended),
236  only_final_(out_par.part_only_final) {}
237 
239  const int) {
240  char pchar = 'p';
241  if (!only_final_) {
242  std::fwrite(&pchar, sizeof(char), 1, file_.get());
243  write(particles.size());
244  write(particles);
245  }
246 }
247 
249  const int event_number,
250  double impact_parameter) {
251  char pchar = 'p';
252  std::fwrite(&pchar, sizeof(char), 1, file_.get());
253  write(particles.size());
254  write(particles);
255 
256  // Event end line
257  char fchar = 'f';
258  std::fwrite(&fchar, sizeof(char), 1, file_.get());
259  write(event_number);
260  write(impact_parameter);
261 
262  // Flush to disk
263  std::fflush(file_.get());
264 }
265 
267  const Clock &,
268  const DensityParameters &) {
269  char pchar = 'p';
270  if (!only_final_) {
271  std::fwrite(&pchar, sizeof(char), 1, file_.get());
272  write(particles.size());
273  write(particles);
274  }
275 }
276 
277 } // namespace smash
void at_intermediate_time(const Particles &particles, const Clock &clock, const DensityParameters &dens_param) override
Writes particles at each time interval; fixed by option OUTPUT_INTERVAL.
A class to pre-calculate and store parameters relevant for density calculation.
Definition: density.h:103
void at_eventend(const Particles &particles, const int event_number, double impact_parameter) override
Writes the final particle information of an event to the binary output.
void at_interaction(const Action &action, const double density) override
Writes an interaction block, including information about the incoming and outgoing particles...
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:108
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...
void at_eventstart(const Particles &particles, const int event_number) override
Writes the initial particle information of an event to the binary output.
void write(const std::string &s)
Write string to binary output.
size_t size() const
Definition: particles.h:87
const ParticleList & outgoing_particles() const
Get the list of particles that resulted from the action.
Definition: action.h:244
const ParticleList & incoming_particles() const
Get the list of particles that go into the action.
Definition: action.cc:58
Helper structure for Experiment to hold output options and parameters.
virtual ProcessType get_type() const
Get the process type.
Definition: action.h:130
Base class for SMASH binary output.
Definition: binaryoutput.h:28
Clock tracks the time in the simulation.
Definition: clock.h:75
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.
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.
void at_eventend(const Particles &particles, const int event_number, double impact_parameter) override
Writes the final particle information list of an event to the binary output.
constexpr int p
Proton.
bool print_start_end_
Write initial and final particles additonally to collisions?
Definition: binaryoutput.h:170
bool only_final_
Write only final particles (True) or both, inital and final (False).
Definition: binaryoutput.h:228
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
The FourVector class holds relevant values in Minkowski spacetime with (+, −, −, −) metric signature.
Definition: fourvector.h:32
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.
Definition: action.h:24
iterator begin()
Definition: fourvector.h:268
bool extended_
Option for extended output.
Definition: binaryoutput.h:114