Version: SMASH-1.6
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 
128  const std::string &mode,
129  const std::string &name,
130  bool extended_format)
131  : OutputInterface(name), file_{path, mode}, extended_(extended_format) {
132  std::fwrite("SMSH", 4, 1, file_.get()); // magic number
133  write(format_version_); // file format version number
134  std::uint16_t format_variant = static_cast<uint16_t>(extended_);
135  write(format_variant);
136  write(VERSION_MAJOR); // SMASH version
137 }
138 
139 // write functions:
140 void BinaryOutputBase::write(const std::string &s) {
141  const auto size = boost::numeric_cast<uint32_t>(s.size());
142  std::fwrite(&size, sizeof(std::uint32_t), 1, file_.get());
143  std::fwrite(s.c_str(), s.size(), 1, file_.get());
144 }
145 
146 void BinaryOutputBase::write(const double x) {
147  std::fwrite(&x, sizeof(x), 1, file_.get());
148 }
149 
151  std::fwrite(v.begin(), sizeof(*v.begin()), 4, file_.get());
152 }
153 
154 void BinaryOutputBase::write(const Particles &particles) {
155  for (const auto &p : particles) {
157  }
158 }
159 
160 void BinaryOutputBase::write(const ParticleList &particles) {
161  for (const auto &p : particles) {
163  }
164 }
165 
167  write(p.position());
168  double mass = p.effective_mass();
169  std::fwrite(&mass, sizeof(mass), 1, file_.get());
170  write(p.momentum());
171  write(p.pdgcode().get_decimal());
172  write(p.id());
173  write(p.type().charge());
174  if (extended_) {
175  const auto history = p.get_history();
176  write(history.collisions_per_particle);
177  write(p.formation_time());
179  write(history.id_process);
180  write(static_cast<int32_t>(history.process_type));
181  write(history.time_last_collision);
182  write(history.p1.get_decimal());
183  write(history.p2.get_decimal());
184  }
185 }
186 
188  std::string name,
189  const OutputParameters &out_par)
191  path / ((name == "Collisions" ? "collisions_binary" : name) + ".bin"),
192  "wb", name, out_par.get_coll_extended(name)),
193  print_start_end_(out_par.coll_printstartend) {}
194 
196  const int) {
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 
206  const int32_t event_number,
207  double impact_parameter) {
208  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  // Event end line
216  char fchar = 'f';
217  std::fwrite(&fchar, sizeof(char), 1, file_.get());
218  write(event_number);
219  write(impact_parameter);
220 
221  // Flush to disk
222  std::fflush(file_.get());
223 }
224 
226  const double density) {
227  char ichar = 'i';
228  std::fwrite(&ichar, sizeof(char), 1, file_.get());
229  write(action.incoming_particles().size());
230  write(action.outgoing_particles().size());
231  std::fwrite(&density, sizeof(double), 1, file_.get());
232  const double weight = action.get_total_weight();
233  std::fwrite(&weight, sizeof(double), 1, file_.get());
234  const double partial_weight = action.get_partial_weight();
235  std::fwrite(&partial_weight, sizeof(double), 1, file_.get());
236  const auto type = static_cast<uint32_t>(action.get_type());
237  std::fwrite(&type, sizeof(uint32_t), 1, file_.get());
238  write(action.incoming_particles());
239  write(action.outgoing_particles());
240 }
241 
243  std::string name,
244  const OutputParameters &out_par)
245  : BinaryOutputBase(path / "particles_binary.bin", "wb", name,
246  out_par.part_extended),
247  only_final_(out_par.part_only_final) {}
248 
250  const int) {
251  char pchar = 'p';
252  if (!only_final_) {
253  std::fwrite(&pchar, sizeof(char), 1, file_.get());
254  write(particles.size());
255  write(particles);
256  }
257 }
258 
260  const int event_number,
261  double impact_parameter) {
262  char pchar = 'p';
263  std::fwrite(&pchar, sizeof(char), 1, file_.get());
264  write(particles.size());
265  write(particles);
266 
267  // Event end line
268  char fchar = 'f';
269  std::fwrite(&fchar, sizeof(char), 1, file_.get());
270  write(event_number);
271  write(impact_parameter);
272 
273  // Flush to disk
274  std::fflush(file_.get());
275 }
276 
278  const Clock &,
279  const DensityParameters &) {
280  char pchar = 'p';
281  if (!only_final_) {
282  std::fwrite(&pchar, sizeof(char), 1, file_.get());
283  write(particles.size());
284  write(particles);
285  }
286 }
287 
288 } // 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.
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_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...
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: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...
virtual ProcessType get_type() const
Get the process type.
Definition: action.h:130
uint16_t format_version_
Binary file format version number.
Definition: binaryoutput.h:112
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.
double effective_mass() const
Get the particle&#39;s effective mass.
Definition: particledata.cc:21
void at_eventend(const Particles &particles, const int32_t event_number, double impact_parameter) override
Writes the final particle information list of an event to the binary output.
HistoryData get_history() const
Get history information.
Definition: particledata.h:120
double xsec_scaling_factor(double delta_time=0.) const
Return the cross section scaling factor at a given time.
Definition: particledata.cc:71
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
const ParticleType & type() const
Get the type of the particle.
Definition: particledata.h:109
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.
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.
bool print_start_end_
Write initial and final particles additonally to collisions?
Definition: binaryoutput.h:170
const ParticleList & incoming_particles() const
Get the list of particles that go into the action.
Definition: action.cc:58
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
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:32
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.
Definition: action.h:24
const FourVector & momentum() const
Get the particle&#39;s 4-momentum.
Definition: particledata.h:139
iterator begin()
Definition: fourvector.h:268
bool extended_
Option for extended output.
Definition: binaryoutput.h:114