Version: SMASH-3.4
listmodus.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2025
3  * SMASH Team
4  *
5  * GNU General Public License (GPLv3 or later)
6  */
7 #ifndef SRC_INCLUDE_SMASH_LISTMODUS_H_
8 #define SRC_INCLUDE_SMASH_LISTMODUS_H_
9 
10 #include <cmath>
11 #include <cstdint>
12 #include <list>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include "forwarddeclarations.h"
18 #include "modusdefault.h"
19 
20 namespace smash {
21 
22 /**
23  * \ingroup modus
24  * ListModus: Provides a modus for running SMASH on an external particle list,
25  * for example as an afterburner calculation.
26  *
27  * To use this modus, choose
28  Modus: List
29  * \code
30  * General:
31  * Modus: List
32  * \endcode
33  * in the configuration file.
34  *
35  * Options for ListModus go in the "Modi"→"List" section of the
36  * configuration:
37  *
38  * \code
39  * Modi:
40  * List:
41  * # options here
42  * \endcode
43 
44  * For configuring see \ref doxypage_input_conf_modi_list.
45  *
46  *
47  * Since SMASH is searching for collisions in computational frame time 't',
48  * all particles need to be at the same time. If this is not the case in
49  * the list provided, the particles will be propagated backwards on
50  * straight lines ("anti-freestreaming"). To avoid unphysical interactions
51  * of these particles, the back-propagated particles receive a
52  * formation_time and zero cross_section_scaling_factor. The cross-sections
53  * are set to zero during the time, where the particle will just propagate
54  * on a straight line again to appear at the formation_time into the system.
55  *
56  */
57 class ListModus : public ModusDefault {
58  public:
59  /**
60  * Constructor
61  *
62  * Gathers all configuration variables for the List.
63  *
64  * \param[in] modus_config The configuration object that sets all
65  * initial conditions of the experiment
66  * \param[in] parameters Necessary because of templated usage in Experiment
67  *
68  * \throw InvalidEvents If more than 2 particles are at the same position in
69  * any of the events.
70  */
71  explicit ListModus(Configuration modus_config,
72  const ExperimentParameters &parameters);
73 
74  /**
75  * Construct an empty list. This is needed for children construction but it is
76  * offered as public instead of protected as it is also useful for JetScape.
77  */
78  ListModus() = default;
79 
80  /**
81  * Generates initial state of the particles in the system according to a list.
82  *
83  * \param[out] particles An empty list that gets filled up by this function
84  * \param[in] parameters Unused, but necessary because of templated use of
85  * this function
86  * \return The starting time of the simulation
87  *
88  * \see read_particles_from_next_event_ for possible exceptions thrown.
89  */
90  double initial_conditions(Particles *particles,
91  const ExperimentParameters &parameters);
92 
93  /**
94  * Tries to add a new particle to particles and performs consistency checks:
95  * (i) The PDG code is legal and exists in SMASH. If not, a warning is printed
96  * and the particle is ignored.
97  * (ii) The mass matches the pole mass of `pdgcode` in SMASH. If it does not,
98  * then a warning is printed, the pole mass of the particle is set equal
99  * to the corresponding mass from SMASH particle table and it's energy
100  * is recomputed as \f$ E^2 = p^2 + m^2 \f$.
101  * (iii) Any stable particle is on-shell, i.e. \f$ E^2 - p^2 = m^2 \f$. If it
102  * is not, then a warning is printed and the energy is set to
103  * \f$ E^2 = p^2 + m^2 \f$.
104  * (iv) If there are nan values in the position or momentum of the particle an
105  * exception is thrown.
106  *
107  * This very tolerant behaviour is justified by the practical
108  * usage of SMASH as afterburner. Usually particles unknown to SMASH are rare
109  * resonances, which do not play a large role. Mass mismatch is typically less
110  * than 1% and comes from rounding and from SMASH enforcing isospin symmetry
111  * (for example the mass of neutral pion is artificially forced to be the
112  * same as charged pion). On-shellness violation typically comes from the
113  * insufficient number of significant digits in the input file + rounding.
114  *
115  * \param[in] pdgcode PDG code of added particle
116  * \param[in] t Time of added particle
117  * \param[in] x x-coordinate of added particle
118  * \param[in] y y-coordinate of added particle
119  * \param[in] z z-coordinate of added particle
120  * \param[in] mass Mass of added particle
121  * \param[in] E Energy of added particle
122  * \param[in] px x-component of momentum of added particle
123  * \param[in] py y-component of momentum of added particle
124  * \param[in] pz z-component of momentum of added particle
125  * \param[in] optional_quantities Extra values present in the input list
126  * \param[out] particles Object to which the particle is added
127  */
128  void try_create_particle(
129  Particles &particles, PdgCode pdgcode, double t, double x, double y,
130  double z, double mass, double E, double px, double py, double pz,
131  const std::vector<std::string> &optional_quantities = {});
132 
133  /** \ingroup exception
134  * Used when external particle list cannot be found.
135  */
136  struct LoadFailure : public std::runtime_error {
137  using std::runtime_error::runtime_error;
138  };
139 
140  /** \ingroup exception
141  * Used when external particle list is invalid.
142  */
143  struct InvalidEvents : public std::invalid_argument {
144  using std::invalid_argument::invalid_argument;
145  };
146 
147  /// \return whether the modus is list modus (which is, yes, trivially true)
148  bool is_list() const { return true; }
149 
150  protected:
151  /// Starting time for the List; changed to the earliest formation time
152  double start_time_ = 0.;
153 
154  private:
155  /**
156  * Read the next event from file.
157  *
158  * @param particles The list of particles where the read information is stored
159  *
160  * \throw runtime_error If an input list file could not be found
161  * \throw LoadFailure If an input list file is not correctly formatted
162  * \throw invalid_argument If the listed charge of a particle does not
163  * correspond to its pdg charge
164  */
166 
167  /**
168  * Check if the given file has events left after the given position.
169  *
170  * \param[in] filepath Path to file to be checked
171  * \param[in] last_position Stream position in file after which check is
172  * performed
173  * \return \c true if there is at least one event left, \c false otherwise
174  *
175  * \throw runtime_error If file could not be read for whatever reason
176  */
177  bool file_has_events_(std::filesystem::path filepath,
178  std::streampos last_position);
179 
180  /**
181  * Return the absolute path of the data file. If an integer is passed, the
182  * filename is constructed using \c particle_list_filename_or_prefix_
183  * concatenated with the given number, otherwise the file prefix is understood
184  * to be the full filename. The file is expected to be in \c
185  * particle_list_file_directory_ folder.
186  *
187  * \param[in] file_id An \c std::optional integer
188  * \return The absolute file path to file
189  *
190  * \throw runtime_error If file does not exist.
191  */
192  std::filesystem::path file_path_(std::optional<int> file_id);
193 
194  /**
195  * Read the next event. Either from the current file if it has more events
196  * or from the next file (with \c file_id_ += 1)
197  *
198  * \return One event as string.
199  * \throw runtime_error If file could not be read for whatever reason.
200  */
201  std::string next_event_();
202 
203  /**
204  * Read and validate all events particles. At the moment this is done w.r.t.
205  * their positions and errors are reported if more than 2 particles have the
206  * same identical position.
207  *
208  * \throw InvalidEvents If more than 2 particles with the same identical
209  * position are found.
210  */
212 
213  /**
214  * Validate the optional fields. At the moment, it ensures that if at least
215  * one spin component is given, all four are given.
216  *
217  * \throw std::invalid_argument if not all spin components are given.
218  */
219  void validate_optional_fields_() const;
220 
221  /**
222  * Judge whether times are the same for all the particles; don't do
223  * anti-freestreaming if all particles start already at the same time.
224  *
225  * If particles are at different times, calculate earliest time as
226  * start_time_ and free-stream all particles back to this time.
227  *
228  * \param particles %Particles to be checked and possibly back-streamed.
229  */
231 
232  /**
233  * Sets the optional fields given in the input list into a particle.
234  *
235  * A warning is issued if the file is not read exactly as given, which
236  * happens if, for example, a float is present in an integer-related
237  * field.
238  *
239  * \param[in] p particle to be modified
240  * \param[in] optional_quantities list of values to be set
241  *
242  * \throw std::invalid_argument if the
243  * quantities in the input file do not obey the appropriate bounds.
244  */
246  ParticleData &p,
247  const std::vector<std::string> &optional_quantities) const;
248 
249  /// File directory of the particle list
251 
252  /**
253  * Prefix of the file(s) containing the particle list. If the user want to
254  * use a single file without numbering, this will contain the full filename.
255  */
257 
258  /// The id of the current file
259  std::optional<int> file_id_;
260 
261  /// Fields with optional quantities to be read
262  std::vector<std::string> optional_fields_{};
263  /// The unique id of the current event
265 
266  /// Last read position in current file
267  std::streampos last_read_position_ = 0;
268 
269  /// Auxiliary flag to warn about mass-discrepancies only once per instance
271  /// Auxiliary flag to warn about off-shell particles only once per instance
273 
274  /// Auxiliary flag to indicate the type of spin interaction used
276 
277  /**
278  * Flag to suppress some error messages. This is used during the validation
279  * of particles in all events, because there we do not know how many events
280  * exist and we simply try to read the next one till an error occurs. This
281  * triggers an error message which should not be printed to the user.
282  */
283  bool verbose_ = true;
284 
285  /**
286  * \ingroup logging
287  * Writes the initial state for the List to the output stream.
288  *
289  * \param[in] out The ostream into which to output
290  * \param[in] m The ListModus object to write into out
291  */
292  friend std::ostream &operator<<(std::ostream &out, const ListModus &m);
293 };
294 
295 /**
296  * \ingroup modus
297  * ListBox: Provides a modus for running the SMASH Box with an external particle
298  list,
299  *
300  * To use this modus, choose
301  Modus: ListBox
302  * \code
303  * General:
304  * Modus: ListBox
305  * \endcode
306  * in the configuration file.
307  *
308  * Options for ListBox go in the "Modi"→"ListBox" section of the
309  * configuration:
310  *
311  * \code
312  * Modi:
313  * ListBox:
314  * # options here
315  * \endcode
316 
317  * The ListBoxModus inherits all functionality from the ListModus.
318  * For more detailed configuring see \ref doxypage_input_conf_modi_list.
319  *
320  *
321  * Since SMASH is searching for collisions in computational frame time 't',
322  * all particles need to be at the same time. If this is not the case in
323  * the list provided, the particles will be propagated backwards on
324  * straight lines ("anti-freestreaming"). To avoid unphysical interactions
325  * of these particles, the back-propagated particles receive a
326  * formation_time and zero cross_section_scaling_factor. The cross-sections
327  * are set to zero during the time, where the particle will just propagate
328  * on a straight line again to appear at the formation_time into the system.
329  *
330  */
331 class ListBoxModus : public ListModus {
332  public:
333  /**
334  * Constructor (This is the same as for the ListModus)
335  *
336  * Gathers all configuration variables for the List.
337  *
338  * \param[in] modus_config The configuration object that sets all
339  * initial conditions of the experiment.
340  * \param[in] parameters Unused, but necessary because of templated
341  * initialization
342  */
343  explicit ListBoxModus(Configuration modus_config,
344  const ExperimentParameters &parameters);
345 
346  /// in the case of the ListBoxModus is_box has to be true
347  bool is_box() const { return true; }
348 
349  /// \copydoc smash::BoxModus::impose_boundary_conditions
350  int impose_boundary_conditions(Particles *particles,
351  const OutputsList &output_list = {});
352 
353  /// \copydoc smash::ModusDefault::create_grid
355  const Particles &particles, double min_cell_length,
356  double timestep_duration, CollisionCriterion crit,
357  const bool include_unformed_particles,
358  CellSizeStrategy strategy = CellSizeStrategy::Optimal) const {
360  if (crit == CollisionCriterion::Stochastic) {
362  }
363  return {{{0, 0, 0}, {length_, length_, length_}},
364  particles,
365  min_cell_length,
366  timestep_duration,
367  limit,
368  include_unformed_particles,
369  strategy};
370  }
371 
372  private:
373  /// Length of the cube's edge in fm
374  const double length_;
375 };
376 
377 } // namespace smash
378 
379 #endif // SRC_INCLUDE_SMASH_LISTMODUS_H_
Interface to the SMASH configuration files.
ListBox: Provides a modus for running the SMASH Box with an external particle list,...
Definition: listmodus.h:331
Grid< GridOptions::PeriodicBoundaries > create_grid(const Particles &particles, double min_cell_length, double timestep_duration, CollisionCriterion crit, const bool include_unformed_particles, CellSizeStrategy strategy=CellSizeStrategy::Optimal) const
Creates the Grid with normal boundary conditions.
Definition: listmodus.h:354
const double length_
Length of the cube's edge in fm.
Definition: listmodus.h:374
bool is_box() const
in the case of the ListBoxModus is_box has to be true
Definition: listmodus.h:347
int impose_boundary_conditions(Particles *particles, const OutputsList &output_list={})
Enforces that all particles are inside the box at the beginning of an event.
Definition: listmodus.cc:500
ListBoxModus(Configuration modus_config, const ExperimentParameters &parameters)
Constructor (This is the same as for the ListModus)
Definition: listmodus.cc:480
ListModus: Provides a modus for running SMASH on an external particle list, for example as an afterbu...
Definition: listmodus.h:57
bool file_has_events_(std::filesystem::path filepath, std::streampos last_position)
Check if the given file has events left after the given position.
Definition: listmodus.cc:393
std::optional< int > file_id_
The id of the current file.
Definition: listmodus.h:259
bool is_list() const
Definition: listmodus.h:148
std::string particle_list_filename_or_prefix_
Prefix of the file(s) containing the particle list.
Definition: listmodus.h:256
void backpropagate_to_same_time_if_needed_(Particles &particles)
Judge whether times are the same for all the particles; don't do anti-freestreaming if all particles ...
Definition: listmodus.cc:107
void validate_optional_fields_() const
Validate the optional fields.
Definition: listmodus.cc:459
std::filesystem::path file_path_(std::optional< int > file_id)
Return the absolute path of the data file.
Definition: listmodus.cc:320
SpinInteractionType spin_interaction_type_
Auxiliary flag to indicate the type of spin interaction used.
Definition: listmodus.h:275
std::string particle_list_file_directory_
File directory of the particle list.
Definition: listmodus.h:250
void validate_list_of_particles_of_all_events_() const
Read and validate all events particles.
Definition: listmodus.cc:434
void read_particles_from_next_event_(Particles &particles)
Read the next event from file.
Definition: listmodus.cc:282
double start_time_
Starting time for the List; changed to the earliest formation time.
Definition: listmodus.h:152
ListModus()=default
Construct an empty list.
bool verbose_
Flag to suppress some error messages.
Definition: listmodus.h:283
void try_create_particle(Particles &particles, PdgCode pdgcode, double t, double x, double y, double z, double mass, double E, double px, double py, double pz, const std::vector< std::string > &optional_quantities={})
Tries to add a new particle to particles and performs consistency checks: (i) The PDG code is legal a...
Definition: listmodus.cc:135
std::streampos last_read_position_
Last read position in current file.
Definition: listmodus.h:267
bool warn_about_mass_discrepancy_
Auxiliary flag to warn about mass-discrepancies only once per instance.
Definition: listmodus.h:270
bool warn_about_off_shell_particles_
Auxiliary flag to warn about off-shell particles only once per instance.
Definition: listmodus.h:272
std::string next_event_()
Read the next event.
Definition: listmodus.cc:347
std::vector< std::string > optional_fields_
Fields with optional quantities to be read.
Definition: listmodus.h:262
void insert_optional_quantities_to_(ParticleData &p, const std::vector< std::string > &optional_quantities) const
Sets the optional fields given in the input list into a particle.
Definition: listmodus.cc:152
double initial_conditions(Particles *particles, const ExperimentParameters &parameters)
Generates initial state of the particles in the system according to a list.
Definition: listmodus.cc:269
int event_id_
The unique id of the current event.
Definition: listmodus.h:264
Base class for Modus classes that provides default function implementations.
Definition: modusdefault.h:45
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
PdgCode stores a Particle Data Group Particle Numbering Scheme particle type number.
Definition: pdgcode.h:108
CollisionCriterion
Criteria used to check collisions.
@ Stochastic
Stochastic Criteiron.
SpinInteractionType
Possible spin interaction types.
@ Off
No spin interactions.
friend std::ostream & operator<<(std::ostream &out, const ListModus &m)
Writes the initial state for the List to the output stream.
Definition: listmodus.cc:101
constexpr int p
Proton.
Definition: action.h:24
CellNumberLimitation
Identifies whether the number of cells should be limited.
Definition: grid.h:55
@ ParticleNumber
Limit the number of cells to the number of particles.
@ None
No cell number limitation.
CellSizeStrategy
Indentifies the strategy of determining the cell size.
Definition: grid.h:33
@ Optimal
Look for optimal cell size.
Helper structure for Experiment.
Used when external particle list is invalid.
Definition: listmodus.h:143
Used when external particle list cannot be found.
Definition: listmodus.h:136