Version: SMASH-1.6
smash.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2012-2019
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 #include <getopt.h>
10 
11 #include <set>
12 #include <sstream>
13 #include <vector>
14 
15 #include <boost/filesystem/fstream.hpp>
16 
17 #include "smash/cxx14compat.h"
18 #include "smash/decaymodes.h"
19 #include "smash/experiment.h"
20 #include "smash/filelock.h"
21 #include "smash/random.h"
24 #include "smash/stringfunctions.h"
25 /* build dependent variables */
26 #include "smash/config.h"
27 
28 namespace smash {
29 
30 namespace {
42 void usage(const int rc, const std::string &progname) {
123  std::printf("\nUsage: %s [option]\n\n", progname.c_str());
124  std::printf(
125  " -h, --help usage information\n"
126  "\n"
127  " -i, --inputfile <file> path to input configuration file\n"
128  " (default: ./config.yaml)\n"
129  " -d, --decaymodes <file> override default decay modes from file\n"
130  " -p, --particles <file> override default particles from file\n"
131  "\n"
132  " -c, --config <YAML> specify config value overrides\n"
133  " (multiple -c arguments are supported)\n"
134  " -m, --modus <modus> shortcut for -c 'General: { Modus: <modus> "
135  "}'\n"
136  " -e, --endtime <time> shortcut for -c 'General: { End_Time: <time> "
137  "}'"
138  "\n"
139  "\n"
140  " -o, --output <dir> output directory (default: ./data/<runid>)\n"
141  " -l, --list-2-to-n list all possible 2->n reactions (with n>1)\n"
142  " -r, --resonance <pdg> dump width(m) and m*spectral function(m^2)"
143  " for resonance pdg\n"
144  " -s, --cross-sections <pdg1>,<pdg2>[,mass1,mass2[,plab1,...]] \n"
145  " dump all partial cross-sections of "
146  "pdg1 + pdg2 reactions versus sqrt(s).\n"
147  " -S, --cross-sections-fs <pdg1>,<pdg2>[,mass1,mass2[,plab1,...]] \n"
148  " dump all partial final-state cross-sections "
149  "of pdg1 + pdg2 reactions versus sqrt(s). Non-deterministic if strings "
150  "are enabled.\n"
151  " Masses are optional, by default pole masses"
152  " are used.\n"
153  " Note the required comma and no spaces.\n"
154  " -f, --force force overwriting files in the output "
155  "directory"
156  "\n"
157  " -v, --version\n\n");
158  std::exit(rc);
159 }
160 
163  std::cout
164  << "###################################################################"
165  << "############"
166  << "\n"
167  << "\n"
168  << " :::s.\n"
169  << " .... ''ss:: "
170  "ah:\n"
171  << " a::''. ..:sss "
172  ".HH.\n"
173  << " mss'.. ...s'm. sSA##As mAhh##hsh##s. .hA##ASa sS###As "
174  "hMAh##h.\n"
175  << " :a':'. .'':as sM#' .HHm''HMS''AMs mMA' .AMs aMA. "
176  "'HHa..HM:\n"
177  << " .a:s'. ..''ss 'h#H#S. mMm 'M#' .HH. 'MH. :MA 'h#H#S. "
178  "hMm :M#\n"
179  << " .::ss'. .... 'SMm .HH. SMa hMm sM#..mHMs 'AMa "
180  "'MH. SMa\n"
181  << " .s::' #SMASHh aMS .MH: HM: #MMMmMM. #SMASHh "
182  "aMS .MM.\n"
183  << "\n"
184  << "###################################################################"
185  << "############"
186  << "\n"
187  << " This is SMASH version: " << VERSION_MAJOR << "\n"
188  << " Simulating Many Accelerated Strongly-interacting Hadrons"
189  << "\n"
190  << "\n"
191  << " Distributed under the GNU General Public License 3.0"
192  << " (GPLv3 or later)."
193  << "\n"
194  << " See LICENSE for details."
195  << "\n"
196  << " For the full list of contributors see AUTHORS."
197  << "\n"
198  << "\n"
199  << " When using SMASH, please cite"
200  << "\n"
201  << " J. Weil et al., Phys.Rev. C94 (2016) no.5, 054905"
202  << "\n"
203  << " and in addition, if Pythia is used please cite"
204  << "\n"
205  << " T. Sjöstrand, S. Mrenna and P. Skands, JHEP05 (2006) 026,"
206  << "\n"
207  << " Comput. Phys. Comm. 178 (2008) 852."
208  << "\n"
209  << "\n"
210  << " Webpage: https://smash-transport.github.io"
211  << "\n"
212  << "\n"
213  << " Report issues at https://github.com/smash-transport/smash"
214  << "\n"
215  << " or contact us by email at elfner@th.physik.uni-frankfurt.de"
216  << "\n"
217  << "\n"
218  << "###################################################################"
219  << "############"
220  << "\n"
221  << "\n";
222 }
223 
229 struct OutputDirectoryExists : public std::runtime_error {
230  using std::runtime_error::runtime_error;
231 };
238 struct OutputDirectoryOutOfIds : public std::runtime_error {
239  using std::runtime_error::runtime_error;
240 };
241 
244  const bf::path p = bf::absolute("data");
245  if (!bf::exists(p)) {
246  return p / "0";
247  }
248  bf::path p2;
249  for (int id = 0; id < std::numeric_limits<int>::max(); ++id) {
250  p2 = p / std::to_string(id);
251  if (!bf::exists(p2)) {
252  break;
253  }
254  }
255  if (p == p2) {
256  throw OutputDirectoryOutOfIds("no unique data subdir ID left");
257  }
258  return p2;
259 }
260 
267 void ensure_path_is_valid(const bf::path &path) {
268  if (bf::exists(path)) {
269  if (!bf::is_directory(path)) {
270  throw OutputDirectoryExists("The given path (" + path.native() +
271  ") exists, but it is not a directory.");
272  }
273  } else {
274  if (!bf::create_directories(path)) {
275  throw OutputDirectoryExists(
276  "Race condition detected: The directory " + path.native() +
277  " did not exist a few cycles ago, but was created in the meantime by "
278  "a different process.");
279  }
280  }
281 }
282 
290  // The following parameters are only relevant for nucleus-nucleus collisions.
291  // Setting them to the valuing values makes sure they don't have any effect.
292  const std::vector<bool> nucleon_has_interacted = {};
293  const int N_tot = 0;
294  const int N_proj = 0;
295 
296  ExperimentParameters params = create_experiment_parameters(configuration);
297  return ScatterActionsFinder(configuration, params, nucleon_has_interacted,
298  N_tot, N_proj);
299 }
300 
310  const std::string smash_version = "1.5";
311  const std::set<std::string> compatible_config_versions = {"1.5"};
312 
313  const std::string config_version = configuration.read({"Version"});
314 
315  if (compatible_config_versions.find(config_version) ==
316  compatible_config_versions.end()) {
317  std::stringstream err;
318  err << "The version of the configuration file (" << config_version
319  << ") is not compatible with the SMASH version (" << smash_version
320  << ").\nThe following config versions are supported:\n";
321  for (auto it : compatible_config_versions) {
322  err << it << " ";
323  }
324  err << "\nPlease consider updating your config or using a compatible SMASH"
325  " version.";
326  throw std::runtime_error(err.str());
327  }
328 }
329 
334  const std::string report = configuration.unused_values_report();
335 
336  if (report != "{}") {
337  throw std::runtime_error(
338  "The following configuration values were not used:\n" + report);
339  }
340 }
341 
349  configuration.take({"Version"});
350  configuration.take({"particles"});
351  configuration.take({"decaymodes"});
352  configuration.take({"Modi"});
353  configuration.take({"General"});
354  if (configuration.has_value({"Output"})) {
355  configuration.take({"Output"});
356  }
357  if (configuration.has_value({"Lattice"})) {
358  configuration.take({"Lattice"});
359  }
360  if (configuration.has_value({"Potentials"})) {
361  configuration.take({"Potentials"});
362  }
363  if (configuration.has_value({"Forced_Thermalization"})) {
364  configuration.take({"Forced_Thermalization"});
365  }
366 }
367 
368 } // unnamed namespace
369 
370 } // namespace smash
371 
382 int main(int argc, char *argv[]) {
383  using namespace smash; // NOLINT(build/namespaces)
384 
385  const auto &log = logger<LogArea::Main>();
386 
387  constexpr option longopts[] = {
388  {"config", required_argument, 0, 'c'},
389  {"decaymodes", required_argument, 0, 'd'},
390  {"endtime", required_argument, 0, 'e'},
391  {"force", no_argument, 0, 'f'},
392  {"help", no_argument, 0, 'h'},
393  {"inputfile", required_argument, 0, 'i'},
394  {"modus", required_argument, 0, 'm'},
395  {"particles", required_argument, 0, 'p'},
396  {"output", required_argument, 0, 'o'},
397  {"list-2-to-n", no_argument, 0, 'l'},
398  {"resonance", required_argument, 0, 'r'},
399  {"cross-sections", required_argument, 0, 's'},
400  {"cross-sections-fs", required_argument, 0, 'S'},
401  {"version", no_argument, 0, 'v'},
402  {nullptr, 0, 0, 0}};
403 
404  // strip any path to progname
405  const std::string progname = bf::path(argv[0]).filename().native();
406 
407  try {
408  bool force_overwrite = false;
409  bf::path output_path = default_output_path(), input_path("./config.yaml");
410  std::vector<std::string> extra_config;
411  char *particles = nullptr, *decaymodes = nullptr, *modus = nullptr,
412  *end_time = nullptr, *pdg_string = nullptr, *cs_string = nullptr;
413  bool list2n_activated = false;
414  bool resonance_dump_activated = false;
415  bool cross_section_dump_activated = false;
416  bool final_state_cross_sections = false;
417 
418  // parse command-line arguments
419  int opt;
420  bool suppress_disclaimer = false;
421  while ((opt = getopt_long(argc, argv, "c:d:e:fhi:m:p:o:lr:s:S:v", longopts,
422  nullptr)) != -1) {
423  switch (opt) {
424  case 'c':
425  extra_config.emplace_back(optarg);
426  break;
427  case 'd':
428  decaymodes = optarg;
429  break;
430  case 'f':
431  force_overwrite = true;
432  break;
433  case 'i':
434  input_path = optarg;
435  break;
436  case 'h':
437  usage(EXIT_SUCCESS, progname);
438  break;
439  case 'm':
440  modus = optarg;
441  break;
442  case 'p':
443  particles = optarg;
444  break;
445  case 'e':
446  end_time = optarg;
447  break;
448  case 'o':
449  output_path = optarg;
450  break;
451  case 'l':
452  list2n_activated = true;
453  suppress_disclaimer = true;
454  break;
455  case 'r':
456  resonance_dump_activated = true;
457  pdg_string = optarg;
458  suppress_disclaimer = true;
459  break;
460  case 'S':
461  final_state_cross_sections = true;
462  // fallthrough
463  case 's':
464  cross_section_dump_activated = true;
465  cs_string = optarg;
466  suppress_disclaimer = true;
467  break;
468  case 'v':
469  std::printf(
470  "%s\n"
471  "Branch : %s\nSystem : %s\nCompiler : %s %s\n"
472  "Build : %s\nDate : %s\n",
473  VERSION_MAJOR, GIT_BRANCH, CMAKE_SYSTEM, CMAKE_CXX_COMPILER_ID,
474  CMAKE_CXX_COMPILER_VERSION, CMAKE_BUILD_TYPE, BUILD_DATE);
475  std::exit(EXIT_SUCCESS);
476  default:
477  usage(EXIT_FAILURE, progname);
478  }
479  }
480 
481  // Abort if there are unhandled arguments left.
482  if (optind < argc) {
483  std::cout << argv[0] << ": invalid argument -- '" << argv[optind]
484  << "'\n";
485  usage(EXIT_FAILURE, progname);
486  }
487 
488  if (!suppress_disclaimer) {
490  }
491 
493 
494  // Read in config file
495  Configuration configuration(input_path.parent_path(),
496  input_path.filename());
497  // Merge config passed via command line
498  for (const auto &config : extra_config) {
499  configuration.merge_yaml(config);
500  }
501 
502  // Set up logging
504  configuration.take({"Logging", "default"}, einhard::ALL));
505  create_all_loggers(configuration["Logging"]);
506 
507  // check if version matches before doing anything else
509 
510  log.trace(source_location, " create ParticleType and DecayModes");
511 
512  auto particles_and_decays =
513  load_particles_and_decaymodes(particles, decaymodes);
514  /* For particles and decaymodes: external file is superior to config.
515  * Hovever, warn in case of conflict.
516  */
517  if (configuration.has_value({"particles"}) && particles) {
518  log.warn("Ambiguity: particles from external file ", particles,
519  " requested, but there is also particle list in the config."
520  " Using particles from ",
521  particles);
522  }
523  if (!configuration.has_value({"particles"}) || particles) {
524  configuration["particles"] = particles_and_decays.first;
525  }
526 
527  if (configuration.has_value({"decaymodes"}) && decaymodes) {
528  log.warn("Ambiguity: decaymodes from external file ", decaymodes,
529  " requested, but there is also decaymodes list in the config."
530  " Using decaymodes from",
531  decaymodes);
532  }
533  if (!configuration.has_value({"decaymodes"}) || decaymodes) {
534  configuration["decaymodes"] = particles_and_decays.second;
535  }
539 
540  if (list2n_activated) {
541  /* Print only 2->n, n > 1. Do not dump decays, which can be found in
542  * decaymodes.txt anyway */
543  configuration.merge_yaml("{Collision_Term: {Two_to_One: False}}");
544  auto scat_finder = actions_finder_for_dump(configuration);
545 
548 
549  scat_finder.dump_reactions();
550  std::exit(EXIT_SUCCESS);
551  }
552  if (resonance_dump_activated) {
553  // Ignore config values that don't make sense.
554  const auto _dummy = ExperimentBase::create(configuration, output_path);
557 
558  PdgCode pdg(pdg_string);
559  const ParticleType &res = ParticleType::find(pdg);
561  std::exit(EXIT_SUCCESS);
562  }
563  if (cross_section_dump_activated) {
564  std::string arg_string(cs_string);
565  std::vector<std::string> args = split(arg_string, ',');
566  const unsigned int n_arg = args.size();
567  if (n_arg != 2 && n_arg != 4 && n_arg < 5) {
568  throw std::invalid_argument("-s usage: pdg1,pdg2[,m1,m2[,sqrts1,...]]");
569  }
570  PdgCode pdg_a(args[0]), pdg_b(args[1]);
571  const ParticleType &a = ParticleType::find(pdg_a);
572  const ParticleType &b = ParticleType::find(pdg_b);
573  if (n_arg < 4) {
574  for (unsigned int i = 0; i < 4 - n_arg; i++) {
575  args.push_back("");
576  }
577  }
578  double ma = (args[2] == "") ? a.mass() : std::stod(args[2]);
579  double mb = (args[3] == "") ? b.mass() : std::stod(args[3]);
580  if (a.is_stable() && args[2] != "" && std::stod(args[2]) != a.mass()) {
581  ma = a.mass();
582  std::cerr << "Warning: pole mass is used for stable particle "
583  << a.name() << " instead of " << args[2] << std::endl;
584  }
585  if (b.is_stable() && args[3] != "" && std::stod(args[3]) != b.mass()) {
586  mb = b.mass();
587  std::cerr << "Warning: pole mass is used for stable particle "
588  << b.name() << " instead of " << args[3] << std::endl;
589  }
590  const size_t plab_size = n_arg <= 4 ? 0 : n_arg - 4;
591  std::vector<double> plab;
592  plab.reserve(plab_size);
593  for (size_t i = 4; i < n_arg; i++) {
594  plab.push_back(std::stod(args.at(i)));
595  }
596  auto scat_finder = actions_finder_for_dump(configuration);
597 
600 
601  scat_finder.dump_cross_sections(a, b, ma, mb, final_state_cross_sections,
602  plab);
603  std::exit(EXIT_SUCCESS);
604  }
605  if (modus) {
606  configuration["General"]["Modus"] = std::string(modus);
607  }
608  if (end_time) {
609  configuration["General"]["End_Time"] = std::abs(std::atof(end_time));
610  }
611 
612  int64_t seed = configuration.read({"General", "Randomseed"});
613  if (seed < 0) {
614  configuration["General"]["Randomseed"] = random::generate_63bit_seed();
615  }
616 
617  // Check output path
618  ensure_path_is_valid(output_path);
619  const bf::path lock_path = output_path / "smash.lock";
620  FileLock lock(lock_path);
621  if (!lock.acquire()) {
622  throw std::runtime_error(
623  "Another instance of SMASH is already writing to the specified "
624  "output directory. If you are sure this is not the case, remove \"" +
625  lock_path.native() + "\".");
626  }
627  log.debug("output path: ", output_path);
628  if (!force_overwrite && bf::exists(output_path / "config.yaml")) {
629  throw std::runtime_error(
630  "Output directory would get overwritten. Select a different output "
631  "directory, clean up, or tell SMASH to ignore existing files.");
632  }
633 
634  /* Keep a copy of the configuration that was used in the output directory
635  * also save information about SMASH build as a comment */
636  bf::ofstream(output_path / "config.yaml")
637  << "# " << VERSION_MAJOR << '\n'
638  << "# Branch : " << GIT_BRANCH << '\n'
639  << "# System : " << CMAKE_SYSTEM << '\n'
640  << "# Compiler : " << CMAKE_CXX_COMPILER_ID << ' '
641  << CMAKE_CXX_COMPILER_VERSION << '\n'
642  << "# Build : " << CMAKE_BUILD_TYPE << '\n'
643  << "# Date : " << BUILD_DATE << '\n'
644  << configuration.to_string() << '\n';
645  configuration.take({"particles"});
646  configuration.take({"decaymodes"});
647 
648  // Create an experiment
649  log.trace(source_location, " create Experiment");
650  auto experiment = ExperimentBase::create(configuration, output_path);
651 
652  // Version value is not used in experiment. Get rid of it to prevent
653  // warning.
654  configuration.take({"Version"});
656 
657  // Run the experiment
658  log.trace(source_location, " run the Experiment");
659  experiment->run();
660  } catch (std::exception &e) {
661  log.fatal() << "SMASH failed with the following error:\n" << e.what();
662  return EXIT_FAILURE;
663  }
664 
665  log.trace() << source_location << " about to return from main";
666  return 0;
667 }
int main(int argc, char *argv[])
Main program Smashes Many Accelerated Strongly-Interacting Hadrons :-)
Definition: smash.cc:382
std::string to_string() const
Returns a YAML string of the current tree.
Configuration configuration(std::string overrides={})
Return a configuration object filled with data from src/config.yaml.
Definition: setup.h:161
Exception class that is thrown, if the requested output directory already exists and -f was not speci...
Definition: smash.cc:229
bool acquire()
Try to acquire the file lock.
Definition: filelock.cc:20
static std::unique_ptr< ExperimentBase > create(Configuration config, const bf::path &output_path)
Factory method that creates and initializes a new Experiment<Modus>.
ScatterActionsFinder actions_finder_for_dump(Configuration configuration)
Prepares ActionsFinder for cross-section and reaction dumps.
Definition: smash.cc:289
std::string unused_values_report() const
Returns a string listing the key/value pairs that have not been taken yet.
bool is_stable() const
Definition: particletype.h:236
static void check_consistency()
void check_config_version_is_compatible(Configuration configuration)
Checks if the SMASH version is compatible with the version of the configuration file.
Definition: smash.cc:309
static void load_decaymodes(const std::string &input)
Loads the DecayModes map as described in the input string.
Definition: decaymodes.cc:163
std::pair< std::string, std::string > load_particles_and_decaymodes(const char *particles_file, const char *decaymodes_file)
Loads particles and decaymodes from provided files particles_file and decaymodes_file.
void ensure_path_is_valid(const bf::path &path)
Ensures the output path is valid.
Definition: smash.cc:267
Value read(std::initializer_list< const char * > keys) const
Additional interface for SMASH to read configuration values without removing them.
std::vector< std::string > split(const std::string &s, char delim)
Split string by delimiter.
Interface to the SMASH configuration files.
ExperimentParameters create_experiment_parameters(Configuration config)
Gathers all general Experiment parameters.
Definition: experiment.cc:281
bool has_value(std::initializer_list< const char * > keys) const
Returns whether there is a non-empty value behind the requested keys.
void setup_default_float_traps()
Setup the floating-point traps used throughout SMASH.
static const ParticleType & find(PdgCode pdgcode)
Returns the ParticleType object for the given pdgcode.
double mass() const
Definition: particletype.h:144
void merge_yaml(const std::string &yaml)
Merge the configuration in yaml into the existing tree.
void create_all_loggers(Configuration config)
Called from main() right after the Configuration object is fully set up to create all logger objects ...
Definition: logging.cc:116
const std::string & name() const
Definition: particletype.h:141
#define source_location
Hackery that is required to output the location in the source code where the log statement occurs...
Definition: logging.h:246
Value take(std::initializer_list< const char * > keys)
The default interface for SMASH to read configuration values.
Guard to create a file lock.
Definition: filelock.h:30
Particle type contains the static properties of a particle species.
Definition: particletype.h:97
A simple scatter finder: Just loops through all particles and checks each pair for a collision...
void print_disclaimer()
Print the disclaimer.
Definition: smash.cc:162
PdgCode stores a Particle Data Group Particle Numbering Scheme particle type number.
Definition: pdgcode.h:108
static void create_type_list(const std::string &particles)
Initialize the global ParticleType list (list_all) from the given input data.
constexpr int p
Proton.
void set_default_loglevel(einhard::LogLevel level)
Set the default log level (what will be returned from subsequent default_loglevel calls)...
Definition: logging.cc:24
Exception class that is thrown, if no new output path can be generated (there is a directory name for...
Definition: smash.cc:238
int64_t generate_63bit_seed()
Generates a seed with a truly random 63-bit value, if possible.
Definition: random.cc:17
void dump_width_and_spectral_function() const
Prints out width and spectral function versus mass to the standard output.
Log all message.
Definition: einhard.hpp:106
std::unique_ptr< ExperimentBase > experiment(const Configuration &c=configuration())
Create an experiment.
Definition: setup.h:175
void check_for_unused_config_values(const Configuration &configuration)
Checks if there are unused config values.
Definition: smash.cc:333
Helper structure for Experiment.
void ignore_simulation_config_values(Configuration &configuration)
Remove all config values that are only needed for simulations.
Definition: smash.cc:348
Definition: action.h:24