Version: SMASH-1.5
oscaroutput.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 #include "smash/oscaroutput.h"
10 
11 #include <string>
12 
13 #include <boost/filesystem.hpp>
14 
15 #include "smash/action.h"
16 #include "smash/clock.h"
17 #include "smash/config.h"
18 #include "smash/configuration.h"
19 #include "smash/cxx14compat.h"
21 #include "smash/particles.h"
22 
23 namespace smash {
24 
25 template <OscarOutputFormat Format, int Contents>
27  const std::string &name)
28  : OutputInterface(name),
29  file_{path /
30  (name + ".oscar" + ((Format == OscarFormat1999) ? "1999" : "")),
31  "w"} {
81  if (Format == OscarFormat2013) {
82  std::fprintf(file_.get(),
83  "#!OSCAR2013 %s t x y z mass "
84  "p0 px py pz pdg ID charge\n",
85  name.c_str());
86  std::fprintf(file_.get(),
87  "# Units: fm fm fm fm "
88  "GeV GeV GeV GeV GeV none none e\n");
89  std::fprintf(file_.get(), "# %s\n", VERSION_MAJOR);
90  } else if (Format == OscarFormat2013Extended) {
91  std::fprintf(file_.get(),
92  "#!OSCAR2013Extended %s t x y z mass p0 px py pz"
93  " pdg ID charge ncoll form_time xsecfac proc_id_origin"
94  " proc_type_origin time_last_coll pdg_mother1 pdg_mother2\n",
95  name.c_str());
96  std::fprintf(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\n");
99  std::fprintf(file_.get(), "# %s\n", VERSION_MAJOR);
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  VERSION_MAJOR);
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  current_event_ = event_number;
129  if (Contents & OscarAtEventstart) {
130  if (Format == OscarFormat2013 || Format == OscarFormat2013Extended) {
131  std::fprintf(file_.get(), "# event %i in %zu\n", event_number + 1,
132  particles.size());
133  } else {
134  /* OSCAR line prefix : initial particles; final particles; event id
135  * First block of an event: initial = 0, final = number of particles
136  */
137  const size_t zero = 0;
138  std::fprintf(file_.get(), "%zu %zu %i\n", zero, particles.size(),
139  event_number + 1);
140  }
141  write(particles);
142  }
143 }
144 
145 template <OscarOutputFormat Format, int Contents>
147  const int event_number,
148  double impact_parameter) {
149  if (Format == OscarFormat2013 || Format == OscarFormat2013Extended) {
150  if (Contents & OscarParticlesAtEventend) {
151  std::fprintf(file_.get(), "# event %i out %zu\n", event_number + 1,
152  particles.size());
153  write(particles);
154  }
155  // Comment end of an event
156  std::fprintf(file_.get(), "# event %i end 0 impact %7.3f\n",
157  event_number + 1, impact_parameter);
158  } else {
159  /* OSCAR line prefix : initial particles; final particles; event id
160  * Last block of an event: initial = number of particles, final = 0
161  * Block ends with null interaction. */
162  const size_t zero = 0;
163  if (Contents & OscarParticlesAtEventend) {
164  std::fprintf(file_.get(), "%zu %zu %i\n", particles.size(), zero,
165  event_number + 1);
166  write(particles);
167  }
168  // Null interaction marks the end of an event
169  std::fprintf(file_.get(), "%zu %zu %i %7.3f\n", zero, zero,
170  event_number + 1, impact_parameter);
171  }
172  // Flush to disk
173  std::fflush(file_.get());
174 }
175 
176 template <OscarOutputFormat Format, int Contents>
178  const double density) {
179  if (Contents & OscarInteractions) {
180  if (Format == OscarFormat2013 || Format == OscarFormat2013Extended) {
181  std::fprintf(file_.get(),
182  "# interaction in %zu out %zu rho %12.7f weight %12.7g"
183  " partial %12.7f type %5i\n",
184  action.incoming_particles().size(),
185  action.outgoing_particles().size(), density,
186  action.get_total_weight(), action.get_partial_weight(),
187  static_cast<int>(action.get_type()));
188  } else {
189  /* OSCAR line prefix : initial final
190  * particle creation: 0 1
191  * particle 2<->2 collision: 2 2
192  * resonance formation: 2 1
193  * resonance decay: 1 2
194  * etc.*/
195  std::fprintf(file_.get(), "%zu %zu %12.7f %12.7f %12.7f %5i\n",
196  action.incoming_particles().size(),
197  action.outgoing_particles().size(), density,
198  action.get_total_weight(), action.get_partial_weight(),
199  static_cast<int>(action.get_type()));
200  }
201  for (const auto &p : action.incoming_particles()) {
202  write_particledata(p);
203  }
204  for (const auto &p : action.outgoing_particles()) {
205  write_particledata(p);
206  }
207  }
208 }
209 
210 template <OscarOutputFormat Format, int Contents>
212  const Particles &particles, const Clock &, const DensityParameters &) {
213  if (Contents & OscarTimesteps) {
214  if (Format == OscarFormat2013 || Format == OscarFormat2013Extended) {
215  std::fprintf(file_.get(), "# event %i out %zu\n", current_event_ + 1,
216  particles.size());
217  } else {
218  const size_t zero = 0;
219  std::fprintf(file_.get(), "%zu %zu %i\n", particles.size(), zero,
220  current_event_ + 1);
221  }
222  write(particles);
223  }
224 }
225 
666 template <OscarOutputFormat Format, int Contents>
668  const ParticleData &data) {
669  const FourVector pos = data.position();
670  const FourVector mom = data.momentum();
671  if (Format == OscarFormat2013) {
672  std::fprintf(file_.get(), "%g %g %g %g %g %.9g %.9g %.9g %.9g %s %i %i\n",
673  pos.x0(), pos.x1(), pos.x2(), pos.x3(), data.effective_mass(),
674  mom.x0(), mom.x1(), mom.x2(), mom.x3(),
675  data.pdgcode().string().c_str(), data.id(),
676  data.type().charge());
677  } else if (Format == OscarFormat2013Extended) {
678  const auto h = data.get_history();
679  std::fprintf(
680  file_.get(),
681  "%g %g %g %g %g %.9g %.9g %.9g"
682  " %.9g %s %i %i %i %g %g %i %i %g %s %s\n",
683  pos.x0(), pos.x1(), pos.x2(), pos.x3(), data.effective_mass(), mom.x0(),
684  mom.x1(), mom.x2(), mom.x3(), data.pdgcode().string().c_str(),
685  data.id(), data.type().charge(), h.collisions_per_particle,
686  data.formation_time(), data.xsec_scaling_factor(), h.id_process,
687  static_cast<int>(h.process_type), h.time_last_collision,
688  h.p1.string().c_str(), h.p2.string().c_str());
689  } else {
690  std::fprintf(file_.get(), "%i %s %i %g %g %g %g %g %g %g %g %g\n",
691  data.id(), data.pdgcode().string().c_str(), 0, mom.x1(),
692  mom.x2(), mom.x3(), mom.x0(), data.effective_mass(), pos.x1(),
693  pos.x2(), pos.x3(), pos.x0());
694  }
695 }
696 
697 namespace {
709 template <int Contents>
710 std::unique_ptr<OutputInterface> create_select_format(
711  bool modern_format, const bf::path &path, const OutputParameters &out_par,
712  const std::string &name) {
713  const auto &log = logger<LogArea::Output>();
714  bool extended_format = (Contents & OscarInteractions) ? out_par.coll_extended
715  : out_par.part_extended;
716  if (modern_format && extended_format) {
717  return make_unique<OscarOutput<OscarFormat2013Extended, Contents>>(path,
718  name);
719  } else if (modern_format && !extended_format) {
720  return make_unique<OscarOutput<OscarFormat2013, Contents>>(path, name);
721  } else if (!modern_format && !extended_format) {
722  return make_unique<OscarOutput<OscarFormat1999, Contents>>(path, name);
723  } else {
724  // Only remaining possibility: (!modern_format && extended_format)
725  log.warn() << "Creating Oscar output: "
726  << "There is no extended Oscar1999 format.";
727  return make_unique<OscarOutput<OscarFormat1999, Contents>>(path, name);
728  }
729 }
730 } // unnamed namespace
731 
732 std::unique_ptr<OutputInterface> create_oscar_output(
733  const std::string &format, const std::string &content, const bf::path &path,
734  const OutputParameters &out_par) {
735  const auto &log = logger<LogArea::Output>();
736  if (format != "Oscar2013" && format != "Oscar1999") {
737  throw std::invalid_argument("Creating Oscar output: unknown format");
738  }
739  const bool modern_format = (format == "Oscar2013");
740  if (content == "Particles") {
741  if (out_par.part_only_final) {
742  return create_select_format<OscarParticlesAtEventend>(
743  modern_format, path, out_par, "particle_lists");
744  } else {
747  modern_format, path, out_par, "particle_lists");
748  }
749  } else if (content == "Collisions") {
750  if (out_par.coll_printstartend) {
753  modern_format, path, out_par, "full_event_history");
754  } else {
755  return create_select_format<OscarInteractions>(
756  modern_format, path, out_par, "full_event_history");
757  }
758  } else if (content == "Dileptons") {
759  if (modern_format && out_par.dil_extended) {
760  return make_unique<
762  "Dileptons");
763  } else if (modern_format && !out_par.dil_extended) {
764  return make_unique<OscarOutput<OscarFormat2013, OscarInteractions>>(
765  path, "Dileptons");
766  } else if (!modern_format && !out_par.dil_extended) {
767  return make_unique<OscarOutput<OscarFormat1999, OscarInteractions>>(
768  path, "Dileptons");
769  } else if (!modern_format && out_par.dil_extended) {
770  log.warn() << "Creating Oscar output: "
771  << "There is no extended Oscar1999 (dileptons) format.";
772  }
773  } else if (content == "Photons") {
774  if (modern_format && !out_par.photons_extended) {
775  return make_unique<OscarOutput<OscarFormat2013, OscarInteractions>>(
776  path, "Photons");
777  } else if (modern_format && out_par.photons_extended) {
778  return make_unique<
780  "Photons");
781  } else if (!modern_format && !out_par.photons_extended) {
782  return make_unique<OscarOutput<OscarFormat1999, OscarInteractions>>(
783  path, "Photons");
784  } else if (!modern_format && out_par.photons_extended) {
785  log.warn() << "Creating Oscar output: "
786  << "There is no extended Oscar1999 (photons) format.";
787  }
788  }
789 
790  throw std::invalid_argument("Create_oscar_output got unknown content.");
791 }
792 
793 } // namespace smash
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:310
store the state at the end of each event (at_eventend)
Definition: oscaroutput.h:50
A class to pre-calculate and store parameters relevant for density calculation.
Definition: density.h:103
double effective_mass() const
Get the particle&#39;s effective mass.
Definition: particledata.cc:21
bool part_extended
Extended format for particles output.
std::string string() const
Definition: pdgcode.h:252
virtual double get_partial_weight() const =0
Return the specific weight for the chosen outgoing channel, which is mainly used for the partial weig...
double x3() const
Definition: fourvector.h:302
const FourVector & momentum() const
Get the particle&#39;s 4-momentum.
Definition: particledata.h:139
double x0() const
Definition: fourvector.h:290
size_t size() const
Definition: particles.h:87
bool coll_printstartend
Print initial and final particles in event into collision output.
int charge() const
The charge of the particle.
Definition: particletype.h:178
double x1() const
Definition: fourvector.h:294
std::unique_ptr< OutputInterface > create_oscar_output(const std::string &format, const std::string &content, const bf::path &path, const OutputParameters &out_par)
Definition: oscaroutput.cc:732
const FourVector & position() const
Get the particle&#39;s position in Minkowski space.
Definition: particledata.h:185
store the state after N timesteps (after_Nth_timestep)
Definition: oscaroutput.h:46
double formation_time() const
Get the absolute formation time of the particle.
Definition: particledata.h:217
const ParticleList & outgoing_particles() const
Get the list of particles that resulted from the action.
Definition: action.h:244
bool photons_extended
Extended format for photon output.
bool part_only_final
Print only final particles in event.
const ParticleList & incoming_particles() const
Get the list of particles that go into the action.
Definition: action.cc:58
bool coll_extended
Extended format for collisions output.
OscarOutput(const bf::path &path, const std::string &name)
Create oscar output.
Helper structure for Experiment to hold output options and parameters.
virtual ProcessType get_type() const
Get the process type.
Definition: action.h:130
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.
double x2() const
Definition: fourvector.h:298
bool dil_extended
Extended format for dilepton output.
std::unique_ptr< T > make_unique(Args &&... args)
Definition for make_unique Is in C++14&#39;s standard library; necessary for older compilers.
Definition: cxx14compat.h:25
store the state at the start of each event (at_eventstart)
Definition: oscaroutput.h:48
constexpr int p
Proton.
const ParticleType & type() const
Get the type of the particle.
Definition: particledata.h:109
PdgCode pdgcode() const
Get the pdgcode of the particle.
Definition: particledata.h:81
std::unique_ptr< OutputInterface > create_select_format(bool modern_format, const bf::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:710
store interaction information (write_interaction)
Definition: oscaroutput.h:44
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
int 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
ParticleData contains the dynamic information of a certain particle.
Definition: particledata.h:52
HistoryData get_history() const
Get history information.
Definition: particledata.h:120
Definition: action.h:24
double xsec_scaling_factor(double delta_time=0.) const
Return the cross section scaling factor at a given time.
Definition: particledata.cc:69