Version: SMASH-1.6
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 
669 template <OscarOutputFormat Format, int Contents>
671  const ParticleData &data) {
672  const FourVector pos = data.position();
673  const FourVector mom = data.momentum();
674  if (Format == OscarFormat2013) {
675  std::fprintf(file_.get(), "%g %g %g %g %g %.9g %.9g %.9g %.9g %s %i %i\n",
676  pos.x0(), pos.x1(), pos.x2(), pos.x3(), data.effective_mass(),
677  mom.x0(), mom.x1(), mom.x2(), mom.x3(),
678  data.pdgcode().string().c_str(), data.id(),
679  data.type().charge());
680  } else if (Format == OscarFormat2013Extended) {
681  const auto h = data.get_history();
682  std::fprintf(
683  file_.get(),
684  "%g %g %g %g %g %.9g %.9g %.9g"
685  " %.9g %s %i %i %i %g %g %i %i %g %s %s\n",
686  pos.x0(), pos.x1(), pos.x2(), pos.x3(), data.effective_mass(), mom.x0(),
687  mom.x1(), mom.x2(), mom.x3(), data.pdgcode().string().c_str(),
688  data.id(), data.type().charge(), h.collisions_per_particle,
689  data.formation_time(), data.xsec_scaling_factor(), h.id_process,
690  static_cast<int>(h.process_type), h.time_last_collision,
691  h.p1.string().c_str(), h.p2.string().c_str());
692  } else {
693  std::fprintf(file_.get(), "%i %s %i %g %g %g %g %g %g %g %g %g\n",
694  data.id(), data.pdgcode().string().c_str(), 0, mom.x1(),
695  mom.x2(), mom.x3(), mom.x0(), data.effective_mass(), pos.x1(),
696  pos.x2(), pos.x3(), pos.x0());
697  }
698 }
699 
700 namespace {
712 template <int Contents>
713 std::unique_ptr<OutputInterface> create_select_format(
714  bool modern_format, const bf::path &path, const OutputParameters &out_par,
715  const std::string &name) {
716  const auto &log = logger<LogArea::Output>();
717  bool extended_format = (Contents & OscarInteractions) ? out_par.coll_extended
718  : out_par.part_extended;
719  if (modern_format && extended_format) {
720  return make_unique<OscarOutput<OscarFormat2013Extended, Contents>>(path,
721  name);
722  } else if (modern_format && !extended_format) {
723  return make_unique<OscarOutput<OscarFormat2013, Contents>>(path, name);
724  } else if (!modern_format && !extended_format) {
725  return make_unique<OscarOutput<OscarFormat1999, Contents>>(path, name);
726  } else {
727  // Only remaining possibility: (!modern_format && extended_format)
728  log.warn() << "Creating Oscar output: "
729  << "There is no extended Oscar1999 format.";
730  return make_unique<OscarOutput<OscarFormat1999, Contents>>(path, name);
731  }
732 }
733 } // unnamed namespace
734 
735 std::unique_ptr<OutputInterface> create_oscar_output(
736  const std::string &format, const std::string &content, const bf::path &path,
737  const OutputParameters &out_par) {
738  const auto &log = logger<LogArea::Output>();
739  if (format != "Oscar2013" && format != "Oscar1999") {
740  throw std::invalid_argument("Creating Oscar output: unknown format");
741  }
742  const bool modern_format = (format == "Oscar2013");
743  if (content == "Particles") {
744  if (out_par.part_only_final) {
745  return create_select_format<OscarParticlesAtEventend>(
746  modern_format, path, out_par, "particle_lists");
747  } else {
750  modern_format, path, out_par, "particle_lists");
751  }
752  } else if (content == "Collisions") {
753  if (out_par.coll_printstartend) {
756  modern_format, path, out_par, "full_event_history");
757  } else {
758  return create_select_format<OscarInteractions>(
759  modern_format, path, out_par, "full_event_history");
760  }
761  } else if (content == "Dileptons") {
762  if (modern_format && out_par.dil_extended) {
763  return make_unique<
765  "Dileptons");
766  } else if (modern_format && !out_par.dil_extended) {
767  return make_unique<OscarOutput<OscarFormat2013, OscarInteractions>>(
768  path, "Dileptons");
769  } else if (!modern_format && !out_par.dil_extended) {
770  return make_unique<OscarOutput<OscarFormat1999, OscarInteractions>>(
771  path, "Dileptons");
772  } else if (!modern_format && out_par.dil_extended) {
773  log.warn() << "Creating Oscar output: "
774  << "There is no extended Oscar1999 (dileptons) format.";
775  }
776  } else if (content == "Photons") {
777  if (modern_format && !out_par.photons_extended) {
778  return make_unique<OscarOutput<OscarFormat2013, OscarInteractions>>(
779  path, "Photons");
780  } else if (modern_format && out_par.photons_extended) {
781  return make_unique<
783  "Photons");
784  } else if (!modern_format && !out_par.photons_extended) {
785  return make_unique<OscarOutput<OscarFormat1999, OscarInteractions>>(
786  path, "Photons");
787  } else if (!modern_format && out_par.photons_extended) {
788  log.warn() << "Creating Oscar output: "
789  << "There is no extended Oscar1999 (photons) format.";
790  }
791  }
792 
793  throw std::invalid_argument("Create_oscar_output got unknown content.");
794 }
795 
796 } // 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
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 oscar output.
Definition: oscaroutput.cc:146
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
const FourVector & position() const
Get the particle&#39;s position in Minkowski space.
Definition: particledata.h:185
bool part_extended
Extended format for particles 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
void write_particledata(const ParticleData &data)
Write single particle information line to output.
Definition: oscaroutput.cc:670
double x0() const
Definition: fourvector.h:290
double effective_mass() const
Get the particle&#39;s effective mass.
Definition: particledata.cc:21
void at_intermediate_time(const Particles &particles, const Clock &clock, const DensityParameters &dens_param) override
Writes a prefix line then write out all current particles.
Definition: oscaroutput.cc:211
bool coll_printstartend
Print initial and final particles in event into collision output.
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:735
HistoryData get_history() const
Get history information.
Definition: particledata.h:120
store the state after N timesteps (after_Nth_timestep)
Definition: oscaroutput.h:46
double xsec_scaling_factor(double delta_time=0.) const
Return the cross section scaling factor at a given time.
Definition: particledata.cc:71
double x3() const
Definition: fourvector.h:302
bool photons_extended
Extended format for photon output.
bool part_only_final
Print only final particles in event.
size_t size() const
Definition: particles.h:87
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.
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
void at_eventstart(const Particles &particles, const int event_number) override
Writes the initial particle information of an event to the oscar output.
Definition: oscaroutput.cc:126
bool dil_extended
Extended format for dilepton output.
store the state at the start of each event (at_eventstart)
Definition: oscaroutput.h:48
constexpr int p
Proton.
void at_interaction(const Action &action, const double density) override
Writes a interaction prefix line and a line for every incoming and outgoing particle to the oscar out...
Definition: oscaroutput.cc:177
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:713
store interaction information (write_interaction)
Definition: oscaroutput.h:44
const ParticleList & incoming_particles() const
Get the list of particles that go into the action.
Definition: action.cc:58
double x2() const
Definition: fourvector.h:298
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
double x1() const
Definition: fourvector.h:294
void write(const Particles &particles)
Write the particle information of a list of particles to the output.
Definition: oscaroutput.cc:119
ParticleData contains the dynamic information of a certain particle.
Definition: particledata.h:52
Definition: action.h:24
const FourVector & momentum() const
Get the particle&#39;s 4-momentum.
Definition: particledata.h:139
std::string string() const
Definition: pdgcode.h:252