Version: SMASH-3.1
oscaroutput.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2023
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #include "smash/oscaroutput.h"
11 
12 #include <filesystem>
13 #include <string>
14 
15 #include "smash/action.h"
16 #include "smash/clock.h"
17 #include "smash/config.h"
19 
20 namespace smash {
21 static constexpr int LHyperSurfaceCrossing = LogArea::HyperSurfaceCrossing::id;
22 
23 template <OscarOutputFormat Format, int Contents>
24 OscarOutput<Format, Contents>::OscarOutput(const std::filesystem::path &path,
25  const std::string &name)
26  : OutputInterface(name),
27  file_{path /
28  (name + ".oscar" + ((Format == OscarFormat1999) ? "1999" : "")),
29  "w"} {
79  if (Format == OscarFormat2013) {
80  std::fprintf(file_.get(),
81  "#!OSCAR2013 %s t x y z mass "
82  "p0 px py pz pdg ID charge\n",
83  name.c_str());
84  std::fprintf(file_.get(),
85  "# Units: fm fm fm fm "
86  "GeV GeV GeV GeV GeV none none e\n");
87  std::fprintf(file_.get(), "# %s\n", SMASH_VERSION);
88  } else if (Format == OscarFormat2013Extended) {
89  std::fprintf(file_.get(),
90  "#!OSCAR2013Extended %s t x y z mass p0 px py pz"
91  " pdg ID charge ncoll form_time xsecfac proc_id_origin"
92  " proc_type_origin time_last_coll pdg_mother1 pdg_mother2"
93  " baryon_number strangeness\n",
94  name.c_str());
95  std::fprintf(
96  file_.get(),
97  "# Units: fm fm fm fm GeV GeV GeV GeV GeV"
98  " none none e none fm none none none fm none none none none\n");
99  std::fprintf(file_.get(), "# %s\n", SMASH_VERSION);
100  } else {
101  const std::string &oscar_name =
102  name == "particle_lists" ? "final_id_p_x" : name;
103  // This is necessary because OSCAR199A requires
104  // this particular string for particle output.
105 
106  std::fprintf(file_.get(), "# OSC1999A\n# %s\n# %s\n", oscar_name.c_str(),
107  SMASH_VERSION);
108  std::fprintf(file_.get(), "# Block format:\n");
109  std::fprintf(file_.get(), "# nin nout event_number\n");
110  std::fprintf(file_.get(), "# id pdg 0 px py pz p0 mass x y z t\n");
111  std::fprintf(file_.get(),
112  "# End of event: 0 0 event_number"
113  " impact_parameter\n");
114  std::fprintf(file_.get(), "#\n");
115  }
116 }
117 
118 template <OscarOutputFormat Format, int Contents>
119 inline void OscarOutput<Format, Contents>::write(const Particles &particles) {
120  for (const ParticleData &data : particles) {
121  write_particledata(data);
122  }
123 }
124 
125 template <OscarOutputFormat Format, int Contents>
127  const int event_number,
128  const EventInfo &) {
129  current_event_ = event_number;
130  if (Contents & OscarAtEventstart) {
131  if (Format == OscarFormat2013 || Format == OscarFormat2013Extended) {
132  std::fprintf(file_.get(), "# event %i in %zu\n", event_number,
133  particles.size());
134  } else {
135  /* OSCAR line prefix : initial particles; final particles; event id
136  * First block of an event: initial = 0, final = number of particles
137  */
138  const size_t zero = 0;
139  std::fprintf(file_.get(), "%zu %zu %i\n", zero, particles.size(),
140  event_number);
141  }
142  if (!(Contents & OscarParticlesIC)) {
143  // We do not want the inital particle list to be printed in case of IC
144  // output
145  write(particles);
146  }
147  }
148 }
149 
150 template <OscarOutputFormat Format, int Contents>
152  const int event_number,
153  const EventInfo &event) {
154  if (Format == OscarFormat2013 || Format == OscarFormat2013Extended) {
155  if (Contents & OscarParticlesAtEventend ||
156  (Contents & OscarParticlesAtEventendIfNotEmpty && !event.empty_event)) {
157  std::fprintf(file_.get(), "# event %i out %zu\n", event_number,
158  particles.size());
159  write(particles);
160  }
161  // Comment end of an event
162  const char *empty_event_str = event.empty_event ? "no" : "yes";
163  std::fprintf(
164  file_.get(),
165  "# event %i end 0 impact %7.3f scattering_projectile_target %s\n",
166  event_number, event.impact_parameter, empty_event_str);
167  } else {
168  /* OSCAR line prefix : initial particles; final particles; event id
169  * Last block of an event: initial = number of particles, final = 0
170  * Block ends with null interaction. */
171  const size_t zero = 0;
172  if (Contents & OscarParticlesAtEventend ||
173  (Contents & OscarParticlesAtEventendIfNotEmpty && !event.empty_event)) {
174  std::fprintf(file_.get(), "%zu %zu %i\n", particles.size(), zero,
175  event_number);
176  write(particles);
177  }
178  // Null interaction marks the end of an event
179  std::fprintf(file_.get(), "%zu %zu %i %7.3f\n", zero, zero, event_number,
180  event.impact_parameter);
181  }
182  // Flush to disk
183  std::fflush(file_.get());
184 
185  if (Contents & OscarParticlesIC) {
186  // If the runtime is too short some particles might not yet have
187  // reached the hypersurface. Warning is printed.
188  if (particles.size() != 0 && !event.impose_kinematic_cut_for_SMASH_IC) {
190  "End time might be too small for initial conditions output. "
191  "Hypersurface has not yet been crossed by ",
192  particles.size(), " particle(s).");
193  }
194  }
195 }
196 
197 template <OscarOutputFormat Format, int Contents>
199  const double density) {
200  if (Contents & OscarInteractions) {
201  if (Format == OscarFormat2013 || Format == OscarFormat2013Extended) {
202  std::fprintf(file_.get(),
203  "# interaction in %zu out %zu rho %12.7f weight %12.7g"
204  " partial %12.7f type %5i\n",
205  action.incoming_particles().size(),
206  action.outgoing_particles().size(), density,
207  action.get_total_weight(), action.get_partial_weight(),
208  static_cast<int>(action.get_type()));
209  } else {
210  /* OSCAR line prefix : initial final
211  * particle creation: 0 1
212  * particle 2<->2 collision: 2 2
213  * resonance formation: 2 1
214  * resonance decay: 1 2
215  * etc.*/
216  std::fprintf(file_.get(), "%zu %zu %12.7f %12.7f %12.7f %5i\n",
217  action.incoming_particles().size(),
218  action.outgoing_particles().size(), density,
219  action.get_total_weight(), action.get_partial_weight(),
220  static_cast<int>(action.get_type()));
221  }
222  for (const auto &p : action.incoming_particles()) {
223  write_particledata(p);
224  }
225  for (const auto &p : action.outgoing_particles()) {
226  write_particledata(p);
227  }
228  } else if (Contents & OscarParticlesIC) {
229  for (const auto &p : action.incoming_particles()) {
230  write_particledata(p);
231  }
232  }
233 }
234 
235 template <OscarOutputFormat Format, int Contents>
237  const Particles &particles, const std::unique_ptr<Clock> &,
238  const DensityParameters &, const EventInfo &) {
239  if (Contents & OscarTimesteps) {
240  if (Format == OscarFormat2013 || Format == OscarFormat2013Extended) {
241  std::fprintf(file_.get(), "# event %i out %zu\n", current_event_,
242  particles.size());
243  } else {
244  const size_t zero = 0;
245  std::fprintf(file_.get(), "%zu %zu %i\n", particles.size(), zero,
246  current_event_);
247  }
248  write(particles);
249  }
250 }
251 
745 template <OscarOutputFormat Format, int Contents>
747  const ParticleData &data) {
748  const FourVector pos = data.position();
749  const FourVector mom = data.momentum();
750  if (Format == OscarFormat2013) {
751  std::fprintf(file_.get(), "%g %g %g %g %g %.9g %.9g %.9g %.9g %s %i %i\n",
752  pos.x0(), pos.x1(), pos.x2(), pos.x3(), data.effective_mass(),
753  mom.x0(), mom.x1(), mom.x2(), mom.x3(),
754  data.pdgcode().string().c_str(), data.id(),
755  data.type().charge());
756  } else if (Format == OscarFormat2013Extended) {
757  const auto h = data.get_history();
758  std::fprintf(
759  file_.get(),
760  "%g %g %g %g %g %.9g %.9g %.9g"
761  " %.9g %s %i %i %i %g %g %i %i %g %s %s %i %i\n",
762  pos.x0(), pos.x1(), pos.x2(), pos.x3(), data.effective_mass(), mom.x0(),
763  mom.x1(), mom.x2(), mom.x3(), data.pdgcode().string().c_str(),
764  data.id(), data.type().charge(), h.collisions_per_particle,
765  data.formation_time(), data.xsec_scaling_factor(), h.id_process,
766  static_cast<int>(h.process_type), h.time_last_collision,
767  h.p1.string().c_str(), h.p2.string().c_str(),
768  data.type().baryon_number(), data.type().strangeness());
769  } else {
770  std::fprintf(file_.get(), "%i %s %i %g %g %g %g %g %g %g %g %g\n",
771  data.id(), data.pdgcode().string().c_str(), 0, mom.x1(),
772  mom.x2(), mom.x3(), mom.x0(), data.effective_mass(), pos.x1(),
773  pos.x2(), pos.x3(), pos.x0());
774  }
775 }
776 
777 namespace {
789 template <int Contents>
790 std::unique_ptr<OutputInterface> create_select_format(
791  bool modern_format, const std::filesystem::path &path,
792  const OutputParameters &out_par, const std::string &name) {
793  bool extended_format = (Contents & OscarInteractions) ? out_par.coll_extended
794  : out_par.part_extended;
795  if (modern_format && extended_format) {
796  return std::make_unique<OscarOutput<OscarFormat2013Extended, Contents>>(
797  path, name);
798  } else if (modern_format && !extended_format) {
799  return std::make_unique<OscarOutput<OscarFormat2013, Contents>>(path, name);
800  } else if (!modern_format && !extended_format) {
801  return std::make_unique<OscarOutput<OscarFormat1999, Contents>>(path, name);
802  } else {
803  // Only remaining possibility: (!modern_format && extended_format)
804  logg[LOutput].warn() << "Creating Oscar output: "
805  << "There is no extended Oscar1999 format.";
806  return std::make_unique<OscarOutput<OscarFormat1999, Contents>>(path, name);
807  }
808 }
809 } // unnamed namespace
810 
811 std::unique_ptr<OutputInterface> create_oscar_output(
812  const std::string &format, const std::string &content,
813  const std::filesystem::path &path, const OutputParameters &out_par) {
814  if (format != "Oscar2013" && format != "Oscar1999") {
815  throw std::invalid_argument("Creating Oscar output: unknown format");
816  }
817  const bool modern_format = (format == "Oscar2013");
818  if (content == "Particles") {
819  if (out_par.part_only_final == OutputOnlyFinal::Yes) {
820  return create_select_format<OscarParticlesAtEventend>(
821  modern_format, path, out_par, "particle_lists");
822  } else if (out_par.part_only_final == OutputOnlyFinal::IfNotEmpty) {
823  return create_select_format<OscarParticlesAtEventendIfNotEmpty>(
824  modern_format, path, out_par, "particle_lists");
825 
826  } else { // out_par.part_only_final == OutputOnlyFinal::No
829  modern_format, path, out_par, "particle_lists");
830  }
831  } else if (content == "Collisions") {
832  if (out_par.coll_printstartend) {
835  modern_format, path, out_par, "full_event_history");
836  } else {
837  return create_select_format<OscarInteractions>(
838  modern_format, path, out_par, "full_event_history");
839  }
840  } else if (content == "Dileptons") {
841  if (modern_format && out_par.dil_extended) {
842  return std::make_unique<
844  "Dileptons");
845  } else if (modern_format && !out_par.dil_extended) {
846  return std::make_unique<OscarOutput<OscarFormat2013, OscarInteractions>>(
847  path, "Dileptons");
848  } else if (!modern_format && !out_par.dil_extended) {
849  return std::make_unique<OscarOutput<OscarFormat1999, OscarInteractions>>(
850  path, "Dileptons");
851  } else if (!modern_format && out_par.dil_extended) {
852  logg[LOutput].warn()
853  << "Creating Oscar output: "
854  << "There is no extended Oscar1999 (dileptons) format.";
855  }
856  } else if (content == "Photons") {
857  if (modern_format && !out_par.photons_extended) {
858  return std::make_unique<OscarOutput<OscarFormat2013, OscarInteractions>>(
859  path, "Photons");
860  } else if (modern_format && out_par.photons_extended) {
861  return std::make_unique<
863  "Photons");
864  } else if (!modern_format && !out_par.photons_extended) {
865  return std::make_unique<OscarOutput<OscarFormat1999, OscarInteractions>>(
866  path, "Photons");
867  } else if (!modern_format && out_par.photons_extended) {
868  logg[LOutput].warn()
869  << "Creating Oscar output: "
870  << "There is no extended Oscar1999 (photons) format.";
871  }
872  } else if (content == "Initial_Conditions") {
873  if (modern_format && !out_par.ic_extended) {
874  return std::make_unique<
876  path, "SMASH_IC");
877  } else if (modern_format && out_par.ic_extended) {
878  return std::make_unique<OscarOutput<
880  path, "SMASH_IC");
881  } else if (!modern_format && !out_par.ic_extended) {
882  return std::make_unique<
884  path, "SMASH_IC");
885  } else if (!modern_format && out_par.ic_extended) {
886  logg[LOutput].warn()
887  << "Creating Oscar output: "
888  << "There is no extended Oscar1999 (initial conditions) format.";
889  }
890  }
891 
892  throw std::invalid_argument("Create_oscar_output got unknown content.");
893 }
894 
895 } // namespace smash
Action is the base class for a generic process that takes a number of incoming particles and transfor...
Definition: action.h:35
virtual ProcessType get_type() const
Get the process type.
Definition: action.h:131
virtual double get_total_weight() const =0
Return the total weight value, which is mainly used for the weight output entry.
const ParticleList & incoming_particles() const
Get the list of particles that go into the action.
Definition: action.cc:58
virtual double get_partial_weight() const =0
Return the specific weight for the chosen outgoing channel, which is mainly used for the partial weig...
const ParticleList & outgoing_particles() const
Get the list of particles that resulted from the action.
Definition: action.h:247
A class to pre-calculate and store parameters relevant for density calculation.
Definition: density.h:108
The FourVector class holds relevant values in Minkowski spacetime with (+, −, −, −) metric signature.
Definition: fourvector.h:33
double x3() const
Definition: fourvector.h:325
double x2() const
Definition: fourvector.h:321
double x0() const
Definition: fourvector.h:313
double x1() const
Definition: fourvector.h:317
Abstraction of generic output.
ParticleData contains the dynamic information of a certain particle.
Definition: particledata.h:58
PdgCode pdgcode() const
Get the pdgcode of the particle.
Definition: particledata.h:87
const ParticleType & type() const
Get the type of the particle.
Definition: particledata.h:128
double xsec_scaling_factor(double delta_time=0.) const
Return the cross section scaling factor at a given time.
Definition: particledata.cc:86
const FourVector & momentum() const
Get the particle's 4-momentum.
Definition: particledata.h:158
double formation_time() const
Get the absolute formation time of the particle.
Definition: particledata.h:236
double effective_mass() const
Get the particle's effective mass.
Definition: particledata.cc:24
int32_t id() const
Get the id of the particle.
Definition: particledata.h:76
HistoryData get_history() const
Get history information.
Definition: particledata.h:139
const FourVector & position() const
Get the particle's position in Minkowski space.
Definition: particledata.h:204
int strangeness() const
Definition: particletype.h:213
int32_t charge() const
The charge of the particle.
Definition: particletype.h:189
int baryon_number() const
Definition: particletype.h:210
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
size_t size() const
Definition: particles.h:87
std::string string() const
Definition: pdgcode.h:325
@ IfNotEmpty
Print only final-state particles, and those only if the event is not empty.
@ Yes
Print only final-state particles.
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > logg
An array that stores all pre-configured Logger objects.
Definition: logging.cc:39
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_oscar_output(const std::string &format, const std::string &content, const std::filesystem::path &path, const OutputParameters &out_par)
Definition: oscaroutput.cc:811
OscarOutput(const std::filesystem::path &path, const std::string &name)
Create oscar output.
@ OscarParticlesAtEventend
store the state at the end of each event (at_eventend)
Definition: oscaroutput.h:50
@ OscarParticlesAtEventendIfNotEmpty
store the state at the end of each event if it is not empty (at_eventend)
Definition: oscaroutput.h:52
@ OscarAtEventstart
store the state at the start of each event (at_eventstart)
Definition: oscaroutput.h:48
@ OscarInteractions
store interaction information (write_interaction)
Definition: oscaroutput.h:44
@ OscarParticlesIC
store the particles that are removed on the hypersurface
Definition: oscaroutput.h:54
@ OscarTimesteps
store the state after N timesteps (after_Nth_timestep)
Definition: oscaroutput.h:46
@ OscarFormat1999
Definition: oscaroutput.h:32
@ OscarFormat2013Extended
Definition: oscaroutput.h:31
@ OscarFormat2013
Definition: oscaroutput.h:30
std::unique_ptr< OutputInterface > create_select_format(bool modern_format, const std::filesystem::path &path, const OutputParameters &out_par, const std::string &name)
Helper function that creates the oscar output with the format selected by create_oscar_output (except...
Definition: oscaroutput.cc:790
constexpr int p
Proton.
Definition: action.h:24
static constexpr int LHyperSurfaceCrossing
Definition: binaryoutput.cc:22
static constexpr int LOutput
Structure to contain custom data for output.
bool empty_event
True if no collisions happened.
bool impose_kinematic_cut_for_SMASH_IC
Whether or not kinematic cuts are employed for SMASH IC.
double impact_parameter
Impact parameter for collider modus, otherwise dummy.
Helper structure for Experiment to hold output options and parameters.
bool dil_extended
Extended format for dilepton output.
bool coll_extended
Extended format for collisions output.
bool part_extended
Extended format for particles output.
bool photons_extended
Extended format for photon output.
OutputOnlyFinal part_only_final
Print only final particles in event.
bool ic_extended
Extended initial conditions output.
bool coll_printstartend
Print initial and final particles in event into collision output.