Version: SMASH-3.4
smash.cc File Reference
#include <getopt.h>
#include <filesystem>
#include <set>
#include <sstream>
#include <vector>
#include "smash/decaymodes.h"
#include "smash/experiment.h"
#include "smash/filelock.h"
#include "smash/random.h"
#include "smash/scatteractionsfinder.h"
#include "smash/setup_particles_decaymodes.h"
#include "smash/sha256.h"
#include "smash/stringfunctions.h"
#include "smash/config.h"
#include "smash/library.h"

Go to the source code of this file.

Classes

struct  smash::anonymous_namespace{smash.cc}::OutputDirectoryExists
 Exception class that is thrown, if the requested output directory already exists and -f was not specified on the command line. More...
 
struct  smash::anonymous_namespace{smash.cc}::OutputDirectoryOutOfIds
 Exception class that is thrown, if no new output path can be generated (there is a directory name for each positive integer value) More...
 

Namespaces

 smash
 
 smash::anonymous_namespace{smash.cc}
 

Functions

void smash::anonymous_namespace{smash.cc}::usage (const int rc, const std::string &progname)
 Prints usage information and exits the program. More...
 
void smash::anonymous_namespace{smash.cc}::print_disclaimer ()
 Print the disclaimer. More...
 
std::filesystem::path smash::anonymous_namespace{smash.cc}::default_output_path ()
 
void smash::anonymous_namespace{smash.cc}::ensure_path_is_valid (const std::filesystem::path &path)
 Ensures the output path is valid. More...
 
ScatterActionsFinder smash::anonymous_namespace{smash.cc}::actions_finder_for_dump (Configuration &configuration)
 Prepares ActionsFinder for cross-section and reaction dumps. More...
 
void smash::anonymous_namespace{smash.cc}::check_for_unused_config_values (const Configuration &configuration)
 Checks if there are unused config values. More...
 
void smash::anonymous_namespace{smash.cc}::ignore_simulation_config_values (Configuration &configuration)
 Remove all config values that are only needed for simulations. More...
 
int main (int argc, char *argv[])
 Main program Smashes Many Accelerated Strongly-Interacting Hadrons :-) More...
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Main program Smashes Many Accelerated Strongly-Interacting Hadrons :-)

Do command line parsing and hence decides modus

Parameters
[in]argcNumber of arguments on command-line
[in]argvList of arguments on command-line
Returns
Either 0 or EXIT_FAILURE.

Definition at line 438 of file smash.cc.

438  {
439  using namespace smash; // NOLINT(build/namespaces)
440 
441  constexpr option longopts[] = {
442  {"config", required_argument, 0, 'c'},
443  {"decaymodes", required_argument, 0, 'd'},
444  {"endtime", required_argument, 0, 'e'},
445  {"force", no_argument, 0, 'f'},
446  {"help", no_argument, 0, 'h'},
447  {"inputfile", required_argument, 0, 'i'},
448  {"modus", required_argument, 0, 'm'},
449  {"particles", required_argument, 0, 'p'},
450  {"output", required_argument, 0, 'o'},
451  {"tabulations", required_argument, 0, 't'},
452  {"list-2-to-n", no_argument, 0, 'l'},
453  {"resonance", required_argument, 0, 'r'},
454  {"cross-sections", required_argument, 0, 's'},
455  {"cross-sections-fs", required_argument, 0, 'S'},
456  {"dump-iSS", no_argument, 0, 'x'},
457  {"version", no_argument, 0, 'v'},
458  {"no-cache", no_argument, 0, 'n'},
459  {"quiet", no_argument, 0, 'q'},
460  {nullptr, 0, 0, 0}};
461 
462  // strip any path to progname
463  const std::string progname =
464  std::filesystem::path(argv[0]).filename().native();
465 
466  try {
467  bool force_overwrite = false;
468  std::filesystem::path output_path = default_output_path();
469  std::string input_path("./config.yaml"), particles, decaymodes;
470  std::vector<std::string> extra_config;
471  char *modus = nullptr, *end_time = nullptr, *pdg_string = nullptr,
472  *cs_string = nullptr;
473  std::string custom_tabulations_path;
474  bool list2n_activated = false;
475  bool resonance_dump_activated = false;
476  bool cross_section_dump_activated = false;
477  bool final_state_cross_sections = false;
478  bool particles_dump_iSS_format = false;
479  bool cache_integrals = true;
480  bool suppress_disclaimer = false;
481 
482  // parse command-line arguments
483  int opt;
484  while ((opt = getopt_long(argc, argv, "c:d:e:fhi:m:p:o:t:lr:s:S:xvnq",
485  longopts, nullptr)) != -1) {
486  switch (opt) {
487  case 'c':
488  extra_config.emplace_back(optarg);
489  break;
490  case 'd':
491  decaymodes = optarg;
492  break;
493  case 'f':
494  force_overwrite = true;
495  break;
496  case 'i':
497  input_path = optarg;
498  break;
499  case 'h':
500  usage(EXIT_SUCCESS, progname);
501  break;
502  case 'm':
503  modus = optarg;
504  break;
505  case 'p':
506  particles = optarg;
507  break;
508  case 'e':
509  end_time = optarg;
510  break;
511  case 'o':
512  output_path = optarg;
513  break;
514  case 't':
515  custom_tabulations_path = optarg;
516  break;
517  case 'l':
518  list2n_activated = true;
519  suppress_disclaimer = true;
520  break;
521  case 'r':
522  resonance_dump_activated = true;
523  pdg_string = optarg;
524  suppress_disclaimer = true;
525  break;
526  case 'S':
527  final_state_cross_sections = true;
528  // fallthrough
529  case 's':
530  cross_section_dump_activated = true;
531  cs_string = optarg;
532  suppress_disclaimer = true;
533  break;
534  case 'x':
535  particles_dump_iSS_format = true;
536  suppress_disclaimer = true;
537  break;
538  case 'v':
539  std::printf(
540  "%s\n"
541 #ifdef GIT_BRANCH
542  "Branch : %s\n"
543 #endif
544  "System : %s\nCompiler : %s %s\n"
545  "Build : %s\nDate : %s\n",
546  SMASH_VERSION,
547 #ifdef GIT_BRANCH
548  GIT_BRANCH,
549 #endif
550  CMAKE_SYSTEM, CMAKE_CXX_COMPILER_ID, CMAKE_CXX_COMPILER_VERSION,
551  CMAKE_BUILD_TYPE, BUILD_DATE);
552  std::exit(EXIT_SUCCESS);
553  case 'n':
554  cache_integrals = false;
555  break;
556  case 'q':
557  suppress_disclaimer = true;
558  break;
559  default:
560  usage(EXIT_FAILURE, progname);
561  }
562  }
563 
564  // Abort if there are unhandled arguments left.
565  if (optind < argc) {
566  std::cout << argv[0] << ": invalid argument -- '" << argv[optind]
567  << "'\n";
568  usage(EXIT_FAILURE, progname);
569  }
570 
571  if (!suppress_disclaimer) {
573  }
574 
575  auto configuration = setup_config_and_logging(input_path, particles,
576  decaymodes, extra_config);
577 
579 
580  // Check output path
581  ensure_path_is_valid(output_path);
582  if (!cache_integrals && !custom_tabulations_path.empty()) {
583  throw std::invalid_argument(
584  "--tabulations cannot be used together with --no-cache.");
585  }
586  std::string tabulations_path;
587  if (cache_integrals) {
588  if (!custom_tabulations_path.empty()) {
589  tabulations_path = custom_tabulations_path;
590  } else {
591  tabulations_path = output_path.has_parent_path()
592  ? output_path.parent_path().string()
593  : ".";
594  tabulations_path += "/tabulations";
595  }
596  } else {
597  tabulations_path = "";
598  }
599  const std::string version(SMASH_VERSION);
600 
601  if (list2n_activated) {
602  /* Print only 2->n, n > 1. Do not dump decays, which can be found in
603  * decaymodes.txt anyway */
604  configuration.merge_yaml("{Collision_Term: {Two_to_One: False}}");
605  initialize_particles_decays_and_tabulations(configuration, version,
606  tabulations_path);
607  auto scat_finder = actions_finder_for_dump(configuration);
608 
609  ignore_simulation_config_values(configuration);
610  check_for_unused_config_values(configuration);
611 
612  scat_finder.dump_reactions();
613  std::exit(EXIT_SUCCESS);
614  }
615  if (particles_dump_iSS_format) {
616  initialize_particles_decays_and_tabulations(configuration, version,
617  tabulations_path);
618  ParticleTypePtrList list;
619  list.clear();
620  for (const auto &ptype : ParticleType::list_all()) {
621  list.push_back(&ptype);
622  }
623  std::sort(list.begin(), list.end(),
625  return a->mass() < b->mass();
626  });
627  for (const ParticleTypePtr &ptype : list) {
628  if (ptype->pdgcode().is_lepton() || ptype->baryon_number() < 0) {
629  continue;
630  }
631  const auto &decay_modes = ptype->decay_modes();
632  const auto &modelist = decay_modes.decay_mode_list();
633  int ndecays = ptype->is_stable() ? 1 : modelist.size();
634  std::printf("%13i %s %10.5f %10.5f %5i %5i %5i %5i %5i %5i %5i %5i\n",
635  ptype->pdgcode().get_decimal(),
636  smash::utf8::fill_left(ptype->name(), 12, ' ').c_str(),
637  ptype->mass(), ptype->width_at_pole(),
638  ptype->pdgcode().spin_degeneracy(), ptype->baryon_number(),
639  ptype->strangeness(), ptype->pdgcode().charmness(),
640  ptype->pdgcode().bottomness(), ptype->isospin() + 1,
641  ptype->charge(), ndecays);
642  if (!ptype->is_stable()) {
643  for (const auto &decay : modelist) {
644  auto ptypes = decay->particle_types();
645  std::printf("%13i %13i %20.5f %13i %13i %13i %13i %13i\n",
646  ptype->pdgcode().get_decimal(), 2, decay->weight(),
647  ptypes[0]->pdgcode().get_decimal(),
648  ptypes[1]->pdgcode().get_decimal(), 0, 0, 0);
649  }
650  } else {
651  std::printf("%13i %13i %20.5f %13i %13i %13i %13i %13i\n",
652  ptype->pdgcode().get_decimal(), 1, 1.0,
653  ptype->pdgcode().get_decimal(), 0, 0, 0, 0);
654  }
655  }
656  std::exit(EXIT_SUCCESS);
657  }
658  if (resonance_dump_activated) {
659  // Ignore config values that don't make sense.
660  initialize_particles_decays_and_tabulations(configuration, version,
661  tabulations_path);
662  const auto _dummy = ExperimentBase::create(configuration, output_path);
663  ignore_simulation_config_values(configuration);
664  check_for_unused_config_values(configuration);
665 
666  PdgCode pdg(pdg_string);
667  const ParticleType &res = ParticleType::find(pdg);
669  std::exit(EXIT_SUCCESS);
670  }
671  if (cross_section_dump_activated) {
672  initialize_particles_decays_and_tabulations(configuration, version,
673  tabulations_path);
674  std::string arg_string(cs_string);
675  std::vector<std::string> args = split(arg_string, ',');
676  const unsigned int n_arg = args.size();
677  if (n_arg != 2 && n_arg != 4 && n_arg < 5) {
678  throw std::invalid_argument("-s usage: pdg1,pdg2[,m1,m2[,plab1,...]]");
679  }
680  PdgCode pdg_a(args[0]), pdg_b(args[1]);
681  const ParticleType &a = ParticleType::find(pdg_a);
682  const ParticleType &b = ParticleType::find(pdg_b);
683  if (n_arg < 4) {
684  for (unsigned int i = 0; i < 4 - n_arg; i++) {
685  args.push_back("");
686  }
687  }
688  double ma = (args[2] == "") ? a.mass() : std::stod(args[2]);
689  double mb = (args[3] == "") ? b.mass() : std::stod(args[3]);
690  if (a.is_stable() && args[2] != "" && std::stod(args[2]) != a.mass()) {
691  ma = a.mass();
692  std::ostringstream warn_msg{"Pole mass ", std::ios::ate};
693  warn_msg << a.mass() << " GeV is used for stable particle " << a.name()
694  << " instead of the provided " << args[2] << " GeV.\n";
695  logg[LMain].warn(warn_msg.str());
696  }
697  if (b.is_stable() && args[3] != "" && std::stod(args[3]) != b.mass()) {
698  mb = b.mass();
699  std::ostringstream warn_msg{"Pole mass ", std::ios::ate};
700  warn_msg << b.mass() << " GeV is used for stable particle " << b.name()
701  << " instead of the provided " << args[3] << " GeV.\n";
702  logg[LMain].warn(warn_msg.str());
703  }
704  const size_t plab_size = n_arg <= 4 ? 0 : n_arg - 4;
705  std::vector<double> plab;
706  plab.reserve(plab_size);
707  for (size_t i = 4; i < n_arg; i++) {
708  plab.push_back(std::stod(args.at(i)));
709  }
710  auto scat_finder = actions_finder_for_dump(configuration);
711 
712  ignore_simulation_config_values(configuration);
713  check_for_unused_config_values(configuration);
714 
715  scat_finder.dump_cross_sections(a, b, ma, mb, final_state_cross_sections,
716  plab);
717  std::exit(EXIT_SUCCESS);
718  }
719  if (modus) {
720  configuration.set_value(InputKeys::gen_modus, std::string(modus));
721  }
722  if (end_time) {
723  configuration.set_value(InputKeys::gen_endTime,
724  std::abs(std::atof(end_time)));
725  }
726 
727  int64_t seed = configuration.read(InputKeys::gen_randomseed);
728  if (seed < 0) {
729  configuration.set_value(InputKeys::gen_randomseed,
731  }
732 
733  // Avoid overwriting SMASH output
734  const std::filesystem::path lock_path = output_path / "smash.lock";
735  FileLock lock(lock_path);
736  if (!lock.acquire()) {
737  throw std::runtime_error(
738  "Another instance of SMASH is already writing to the specified "
739  "output directory. If you are sure this is not the case, remove \"" +
740  lock_path.native() + "\".");
741  }
742  logg[LMain].debug("output path: ", output_path);
743  if (!force_overwrite &&
744  std::filesystem::exists(output_path / "config.yaml")) {
745  throw std::runtime_error(
746  "Output directory would get overwritten. Select a different output "
747  "directory, clean up, or tell SMASH to ignore existing files.");
748  }
749 
750  /* Keep a copy of the configuration that was used in the output directory
751  * also save information about SMASH build as a comment */
752  std::ofstream(output_path / "config.yaml")
753  << "# " << SMASH_VERSION << '\n'
754 #ifdef GIT_BRANCH
755  << "# Branch : " << GIT_BRANCH << '\n'
756 #endif
757  << "# System : " << CMAKE_SYSTEM << '\n'
758  << "# Compiler : " << CMAKE_CXX_COMPILER_ID << ' '
759  << CMAKE_CXX_COMPILER_VERSION << '\n'
760  << "# Build : " << CMAKE_BUILD_TYPE << '\n'
761  << "# Date : " << BUILD_DATE << '\n'
762  << configuration.to_string() << '\n';
763 
764  const auto hash =
765  initialize_particles_decays_and_return_hash(configuration, version);
766 
767  // Create an experiment
768  logg[LMain].trace(SMASH_SOURCE_LOCATION, " create Experiment");
769  auto experiment = ExperimentBase::create(configuration, output_path);
770  check_for_unused_config_values(configuration);
771  tabulate_resonance_integrals(hash, tabulations_path);
772 
773  // Run the experiment
774  logg[LMain].trace(SMASH_SOURCE_LOCATION, " run the Experiment");
775  experiment->run();
776  } catch (std::exception &e) {
777  logg[LMain].fatal() << "SMASH failed with the following error:\n"
778  << e.what();
779  return EXIT_FAILURE;
780  }
781 
782  logg[LMain].trace() << SMASH_SOURCE_LOCATION << " about to return from main";
783  return 0;
784 }
static std::unique_ptr< ExperimentBase > create(Configuration &config, const std::filesystem::path &output_path)
Factory method that creates and initializes a new Experiment<Modus>.
Definition: experiment.cc:22
Guard to create a file lock.
Definition: filelock.h:30
A pointer-like interface to global references to ParticleType objects.
Definition: particletype.h:731
Particle type contains the static properties of a particle species.
Definition: particletype.h:100
void dump_width_and_spectral_function() const
Prints out width and spectral function versus mass to the standard output.
static const ParticleType & find(PdgCode pdgcode)
Returns the ParticleType object for the given pdgcode.
Definition: particletype.cc:99
const std::string & name() const
Definition: particletype.h:144
static const ParticleTypeList & list_all()
Definition: particletype.cc:51
bool is_stable() const
Definition: particletype.h:251
double mass() const
Definition: particletype.h:147
PdgCode stores a Particle Data Group Particle Numbering Scheme particle type number.
Definition: pdgcode.h:108
#define SMASH_SOURCE_LOCATION
Hackery that is required to output the location in the source code where the log statement occurs.
Definition: logging.h:153
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > & logg
An array that stores all pre-configured Logger objects.
Definition: logging.h:245
std::unique_ptr< ExperimentBase > experiment(Configuration c)
Create an experiment given an input configuration.
Definition: setup.h:169
void ignore_simulation_config_values(Configuration &configuration)
Remove all config values that are only needed for simulations.
Definition: smash.cc:408
ScatterActionsFinder actions_finder_for_dump(Configuration &configuration)
Prepares ActionsFinder for cross-section and reaction dumps.
Definition: smash.cc:386
void usage(const int rc, const std::string &progname)
Prints usage information and exits the program.
Definition: smash.cc:153
void ensure_path_is_valid(const std::filesystem::path &path)
Ensures the output path is valid.
Definition: smash.cc:364
void check_for_unused_config_values(const Configuration &configuration)
Checks if there are unused config values.
Definition: smash.cc:394
void print_disclaimer()
Print the disclaimer.
Definition: smash.cc:252
std::filesystem::path default_output_path()
Definition: smash.cc:340
int64_t generate_63bit_seed()
Generates a seed with a truly random 63-bit value, if possible.
Definition: random.cc:21
std::string fill_left(const std::string &s, size_t width, char fill=' ')
Fill string with characters to the left until the given width is reached.
Definition: action.h:24
std::vector< std::string > split(const std::string &s, char delim)
Split string by delimiter.
void initialize_particles_decays_and_tabulations(Configuration &configuration, const std::string &version, const std::string &tabulations_dir={})
Wrapper over a function that initializes the particles and decays from the given configuration,...
Definition: library.cc:46
void tabulate_resonance_integrals(const sha256::Hash &hash, const std::string &tabulations_dir)
Tabulate the resonance integrals.
Definition: library.cc:75
Configuration setup_config_and_logging(const std::string &config_file, const std::string &particles_file={}, const std::string &decaymodes_file={}, const std::vector< std::string > &extra_config={})
Set up configuration and logging from input files and extra config.
Definition: library.cc:34
sha256::Hash initialize_particles_decays_and_return_hash(Configuration &configuration, const std::string &version)
Initialize the particles and decays from the given configuration.
Definition: library.cc:54
void setup_default_float_traps()
Setup the floating-point traps used throughout SMASH.
static constexpr int LMain
Definition: experiment.h:94
static const Key< int64_t > gen_randomseed
See user guide description for more information.
Definition: input_keys.h:1398
static const Key< std::string > gen_modus
See user guide description for more information.
Definition: input_keys.h:1361
static const Key< double > gen_endTime
See user guide description for more information.
Definition: input_keys.h:1333
Here is the call graph for this function: