Version: SMASH-3.4
binaryoutput.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2020,2022-2025
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_BINARYOUTPUT_H_
11 #define SRC_INCLUDE_SMASH_BINARYOUTPUT_H_
12 #include <cstdint>
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 #include "file.h"
18 #include "forwarddeclarations.h"
19 #include "numeric_cast.h"
20 #include "outputformatter.h"
21 #include "outputinterface.h"
22 #include "outputparameters.h"
23 
24 namespace smash {
25 
26 /**
27  * \ingroup output
28  * Base class for SMASH binary output.
29  */
31  protected:
32  /**
33  * Create binary output base.
34  *
35  * \param[in] path Output path.
36  * \param[in] mode Is used to determine the file access mode.
37  * \param[in] name Name of the output.
38  * \param[in] quantities The list of quantities printed to the output.
39  *
40  * \throw std::invalid_argument if the list of quantities is empty.
41  */
42  explicit BinaryOutputBase(const std::filesystem::path &path,
43  const std::string &mode, const std::string &name,
44  const std::vector<std::string> &quantities);
45 
46  /**
47  * Write several bytes to the binary output. Meant to be used by the
48  * OutputFormatter.
49  *
50  * \param[in] chunk vector of bytes to be written.
51  */
52  void write(const ToBinary::type &chunk);
53 
54  /**
55  * Write byte to binary output.
56  * \param[in] c Value to be written.
57  */
58  void write(const char c);
59 
60  /**
61  * Write string to binary output.
62  * \param[in] s String to be written.
63  */
64  void write(const std::string &s);
65 
66  /**
67  * Write double to binary output.
68  * \param[in] x Value to be written.
69  */
70  void write(const double x);
71 
72  /**
73  * Write four-vector to binary output.
74  * \param[in] v Four-vector to be written.
75  */
76  void write(const FourVector &v);
77 
78  /**
79  * Write integer (32 bit) to binary output.
80  * \param[in] x Value to be written.
81  */
82  void write(const std::int32_t x) {
83  std::fwrite(&x, sizeof(x), 1, file_.get());
84  }
85 
86  /**
87  * Write unsigned integer (32 bit) to binary output.
88  * \param[in] x Value to be written.
89  */
90  void write(const std::uint32_t x) {
91  std::fwrite(&x, sizeof(x), 1, file_.get());
92  }
93 
94  /**
95  * Write unsigned integer (16 bit) to binary output.
96  * \param[in] x Value to be written.
97  */
98  void write(const std::uint16_t x) {
99  std::fwrite(&x, sizeof(x), 1, file_.get());
100  }
101 
102  /**
103  * Write a std::size_t to binary output.
104  * \param[in] x Value to be written.
105  */
106  void write(const size_t x) { write(smash::numeric_cast<uint32_t>(x)); }
107 
108  /**
109  * Write particle data of each particle in particles to binary output.
110  * \param[in] particles List of particles, whose data is to be written.
111  */
112  void write(const Particles &particles);
113 
114  /**
115  * Write each particle data entry to binary output.
116  * \param[in] particles List of particles, whose data is to be written.
117  */
118  void write(const ParticleList &particles);
119 
120  /**
121  * Write particle data to binary output.
122  * \param[in] p Particle data to be written.
123  */
124  void write_particledata(const ParticleData &p);
125 
126  /// Binary particles output file path
128 
129  private:
130  /// Binary file format version number
131  const uint16_t format_version_ = 10;
132  /// Format variant number associated to the custom quantities case
133  const uint16_t format_custom_ = 2;
134  /// The output formatter
136 };
137 
138 /**
139  * \ingroup output
140  * \brief Saves SMASH collision history to binary file.
141  *
142  * This class writes each collision, decay and box wall crossing
143  * to the output file. Optionally, one can also write the
144  * initial and final particle lists to the same file.
145  * The output file is binary and has a block structure.
146  *
147  * Details of the output format can be found
148  * on the wiki in the User Guide section, look for binary output.
149  */
151  public:
152  /**
153  * Create binary particle output.
154  *
155  * \param[in] path Output path.
156  * \param[in] name Name of the output.
157  * \param[in] out_par A structure containing parameters of the output.
158  * \param[in] quantities The list of quantities printed to the output.
159  */
160  BinaryOutputCollisions(const std::filesystem::path &path, std::string name,
161  const OutputParameters &out_par,
162  const std::vector<std::string> &quantities);
163 
164  /**
165  * Writes the initial particle information list of an event to the binary
166  * output.
167  * \param[in] particles Current list of all particles.
168  * \param[in] event_label Numbers of event and ensemble.
169  * \param[in] event Event info, see \ref event_info
170  */
171  void at_eventstart(const Particles &particles, const EventLabel &event_label,
172  const EventInfo &event) override;
173 
174  /**
175  * Writes the final particle information list of an event to the binary
176  * output.
177  * \param[in] particles Current list of particles.
178  * \param[in] event_label Numbers of event and ensemble.
179  * \param[in] event Event info, see \ref event_info
180  */
181  void at_eventend(const Particles &particles, const EventLabel &event_label,
182  const EventInfo &event) override;
183 
184  /**
185  * Writes an interaction block, including information about the incoming and
186  * outgoing particles, to the binary output.
187  * \param[in] action Action that holds the information of the interaction.
188  * \param[in] density Density at the interaction point.
189  */
190  void at_interaction(const Action &action, const double density) override;
191 
192  private:
193  /// Write initial and final particles additonally to collisions?
195 };
196 
197 /**
198  * \ingroup output
199  *
200  * \brief Writes the particle list at specific times to the binary file
201  *
202  * This class writes the current particle list at a specific time t
203  * to the binary output file. This specific time can
204  * be: event start, event end or every next time interval \f$\Delta t \f$.
205  * Writing (or not writing) the output at these moments is controlled by
206  * different options. The time interval \f$\Delta t \f$ is also regulated by an
207  * option. The output file is binary and has a block structure.
208  *
209  * Details of the output format can be found
210  * on the wiki in the User Guide section, look for binary output.
211  */
213  public:
214  /**
215  * Create binary particle output.
216  *
217  * \param[in] path Output path.
218  * \param[in] name Name of the ouput.
219  * \param[in] out_par A structure containing the parameters of the output.
220  * \param[in] quantities The list of quantities printed to the output.
221  */
222  BinaryOutputParticles(const std::filesystem::path &path, std::string name,
223  const OutputParameters &out_par,
224  const std::vector<std::string> &quantities);
225 
226  /**
227  * Writes the initial particle information of an event to the binary output.
228  * \param[in] particles Current list of all particles.
229  * \param[in] event_label Numbers of event and ensemble.
230  * \param[in] event Event info, see \ref event_info
231  */
232  void at_eventstart(const Particles &particles, const EventLabel &event_label,
233  const EventInfo &event) override;
234 
235  /**
236  * Writes the final particle information of an event to the binary output.
237  * \param[in] particles Current list of particles.
238  * \param[in] event_label Numbers of event and ensemble.
239  * \param[in] event Event info, see \ref event_info
240  */
241  void at_eventend(const Particles &particles, const EventLabel &event_label,
242  const EventInfo &event) override;
243 
244  /**
245  * Writes particles at each time interval; fixed by option OUTPUT_INTERVAL.
246  * \param[in] particles Current list of particles.
247  * \param[in] clock Unused, needed since inherited.
248  * \param[in] dens_param Unused, needed since inherited.
249  * \param[in] event_label Numbers of event and ensemble.
250  * \param[in] event Event info, see \ref event_info.
251  */
252  void at_intermediate_time(const Particles &particles,
253  const std::unique_ptr<Clock> &clock,
254  const DensityParameters &dens_param,
255  const EventLabel &event_label,
256  const EventInfo &event) override;
257 
258  private:
259  /// Whether final- or initial-state particles should be written.
261 };
262 
263 /**
264  * \ingroup output
265  *
266  * \brief Writes the particles when crossing the hypersurface to the binary file
267  *
268  * This class writes each particle to the binary output at the time of crossing
269  * the hypersurface. This time corresponds to the proper time of the
270  * hypersruface, which is - if not specified differently in the configuration -
271  * the passing time of the two nuclei.
272  *
273  * Details of the output format can be found
274  * on the wiki in the User Guide section, look for Output: Initial Conditions.
275  */
277  public:
278  /**
279  * Create binary initial conditions particle output.
280  *
281  * \param[in] path Output path.
282  * \param[in] name Name of the ouput.
283  * \param[in] quantities The list of quantities printed to the output.
284  */
285  BinaryOutputInitialConditions(const std::filesystem::path &path,
286  std::string name,
287  const std::vector<std::string> &quantities);
288 
289  /**
290  * Writes the initial particle information of an event to the binary output.
291  * Function unused for IC output. Needed since inherited.
292  */
293  void at_eventstart(const Particles &, const EventLabel &,
294  const EventInfo &) override;
295 
296  /**
297  * Writes the final particle information of an event to the binary output.
298  * \param[in] particles Current list of particles.
299  * \param[in] event_label Number of event and ensemble.
300  * \param[in] event Event info, see \ref event_info
301  */
302  void at_eventend(const Particles &particles, const EventLabel &event_label,
303  const EventInfo &event) override;
304 
305  /**
306  * Writes particles that are removed when crossing the hypersurface to the
307  * output. Note that the particle information is written as a particle block,
308  * not as an interaction block.
309  * \param[in] action Action that holds the information of the interaction.
310  */
311  void at_interaction(const Action &action, const double) override;
312 };
313 
314 /**
315  * \ingroup output
316  *
317  * \brief Create a binary output object. This is a helper function for the \c
318  * Experiment class to facilitate the logic when creating the output objects.
319  *
320  * \param[in] format The output format as string, e.g. \c "Oscar2013"
321  * \param[in] content The output content as string, e.g. \c "Particles"
322  * \param[in] path The path to the output directory
323  * \param[in] out_par The output parameters object containing output metadata
324  *
325  * \return A \c std::unique_ptr<OutputInterface> polymorphically initialised to
326  * the correct binary output object.
327  */
328 std::unique_ptr<OutputInterface> create_binary_output(
329  const std::string &format, const std::string &content,
330  const std::filesystem::path &path, const OutputParameters &out_par);
331 
332 } // namespace smash
333 
334 #endif // SRC_INCLUDE_SMASH_BINARYOUTPUT_H_
Action is the base class for a generic process that takes a number of incoming particles and transfor...
Definition: action.h:35
Base class for SMASH binary output.
Definition: binaryoutput.h:30
void write(const std::uint16_t x)
Write unsigned integer (16 bit) to binary output.
Definition: binaryoutput.h:98
void write(const ToBinary::type &chunk)
Write several bytes to the 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:131
const uint16_t format_custom_
Format variant number associated to the custom quantities case.
Definition: binaryoutput.h:133
OutputFormatter< ToBinary > formatter_
The output formatter.
Definition: binaryoutput.h:135
RenamingFilePtr file_
Binary particles output file path.
Definition: binaryoutput.h:127
BinaryOutputBase(const std::filesystem::path &path, const std::string &mode, const std::string &name, const std::vector< std::string > &quantities)
Create binary output base.
void write(const std::int32_t x)
Write integer (32 bit) to binary output.
Definition: binaryoutput.h:82
void write(const size_t x)
Write a std::size_t to binary output.
Definition: binaryoutput.h:106
void write(const std::uint32_t x)
Write unsigned integer (32 bit) to binary output.
Definition: binaryoutput.h:90
Saves SMASH collision history to binary file.
Definition: binaryoutput.h:150
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 EventLabel &event_label, const EventInfo &event) override
Writes the initial particle information list of an event to the binary output.
void at_eventend(const Particles &particles, const EventLabel &event_label, 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, const std::vector< std::string > &quantities)
Create binary particle output.
bool print_start_end_
Write initial and final particles additonally to collisions?
Definition: binaryoutput.h:194
Writes the particles when crossing the hypersurface to the binary file.
Definition: binaryoutput.h:276
void at_eventend(const Particles &particles, const EventLabel &event_label, 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 std::vector< std::string > &quantities)
Create binary initial conditions particle output.
void at_interaction(const Action &action, const double) override
Writes particles that are removed when crossing the hypersurface to the output.
void at_eventstart(const Particles &, const EventLabel &, const EventInfo &) override
Writes the initial particle information of an event to the binary output.
Writes the particle list at specific times to the binary file.
Definition: binaryoutput.h:212
void at_eventstart(const Particles &particles, const EventLabel &event_label, const EventInfo &event) override
Writes the initial particle information of an event to the binary output.
void at_intermediate_time(const Particles &particles, const std::unique_ptr< Clock > &clock, const DensityParameters &dens_param, const EventLabel &event_label, const EventInfo &event) override
Writes particles at each time interval; fixed by option OUTPUT_INTERVAL.
OutputOnlyFinal only_final_
Whether final- or initial-state particles should be written.
Definition: binaryoutput.h:260
BinaryOutputParticles(const std::filesystem::path &path, std::string name, const OutputParameters &out_par, const std::vector< std::string > &quantities)
Create binary particle output.
void at_eventend(const Particles &particles, const EventLabel &event_label, 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:92
The FourVector class holds relevant values in Minkowski spacetime with (+, −, −, −) metric signature.
Definition: fourvector.h:33
A general-purpose formatter for output, supporting both ASCII and binary formats.
Abstraction of generic output.
ParticleData contains the dynamic information of a certain particle.
Definition: particledata.h:59
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
A RAII type to replace std::FILE *.
Definition: file.h:72
FILE * get()
Get the underlying FILE* pointer.
Definition: file.cc:27
std::vector< char > type
Return type of this converter.
OutputOnlyFinal
Whether and when only final state particles should be printed.
FormattingHelper< T > format(const T &value, const char *unit, int width=-1, int precision=-1)
Acts as a stream modifier for std::ostream to output an object with an optional suffix string and wit...
Definition: logging.h:217
std::unique_ptr< OutputInterface > create_binary_output(const std::string &format, const std::string &content, const std::filesystem::path &path, const OutputParameters &out_par)
Create a binary output object.
constexpr int p
Proton.
Definition: action.h:24
Structure to contain custom data for output.
Structure to contain information about the event and ensemble numbers.
Helper structure for Experiment to hold output options and parameters.