Version: SMASH-2.1
smash.cc File Reference
#include <getopt.h>
#include <set>
#include <sstream>
#include <vector>
#include <boost/filesystem/fstream.hpp>
#include "smash/cxx14compat.h"
#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"

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}::print_disclaimer ()
 Print the disclaimer. More...
 
bf::path smash::anonymous_namespace{smash.cc}::default_output_path ()
 
void smash::anonymous_namespace{smash.cc}::ensure_path_is_valid (const bf::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_config_version_is_compatible (Configuration configuration)
 Checks if the SMASH version is compatible with the version of the configuration file. 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...
 
void smash::anonymous_namespace{smash.cc}::initialize_particles_and_decays (Configuration &configuration)
 Initialize the particles and decays from the configuration. More...
 
void smash::anonymous_namespace{smash.cc}::initialize_particles_and_decays (Configuration &configuration, sha256::Hash hash, bf::path tabulations_path)
 Initialize the particles and decays from the configuration, the hash and the path to the cashed resonance integrals. 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 403 of file smash.cc.

403  {
404  using namespace smash; // NOLINT(build/namespaces)
405 
406  constexpr option longopts[] = {
407  {"config", required_argument, 0, 'c'},
408  {"decaymodes", required_argument, 0, 'd'},
409  {"endtime", required_argument, 0, 'e'},
410  {"force", no_argument, 0, 'f'},
411  {"help", no_argument, 0, 'h'},
412  {"inputfile", required_argument, 0, 'i'},
413  {"modus", required_argument, 0, 'm'},
414  {"particles", required_argument, 0, 'p'},
415  {"output", required_argument, 0, 'o'},
416  {"list-2-to-n", no_argument, 0, 'l'},
417  {"resonance", required_argument, 0, 'r'},
418  {"cross-sections", required_argument, 0, 's'},
419  {"cross-sections-fs", required_argument, 0, 'S'},
420  {"dump-iSS", no_argument, 0, 'x'},
421  {"version", no_argument, 0, 'v'},
422  {"no-cache", no_argument, 0, 'n'},
423  {"quiet", no_argument, 0, 'q'},
424  {nullptr, 0, 0, 0}};
425 
426  // strip any path to progname
427  const std::string progname = bf::path(argv[0]).filename().native();
428 
429  try {
430  bool force_overwrite = false;
431  bf::path output_path = default_output_path(), input_path("./config.yaml");
432  std::vector<std::string> extra_config;
433  char *particles = nullptr, *decaymodes = nullptr, *modus = nullptr,
434  *end_time = nullptr, *pdg_string = nullptr, *cs_string = nullptr;
435  bool list2n_activated = false;
436  bool resonance_dump_activated = false;
437  bool cross_section_dump_activated = false;
438  bool final_state_cross_sections = false;
439  bool particles_dump_iSS_format = false;
440  bool cache_integrals = true;
441 
442  // parse command-line arguments
443  int opt;
444  bool suppress_disclaimer = false;
445  while ((opt = getopt_long(argc, argv, "c:d:e:fhi:m:p:o:lr:s:S:xvnq",
446  longopts, nullptr)) != -1) {
447  switch (opt) {
448  case 'c':
449  extra_config.emplace_back(optarg);
450  break;
451  case 'd':
452  decaymodes = optarg;
453  break;
454  case 'f':
455  force_overwrite = true;
456  break;
457  case 'i':
458  input_path = optarg;
459  break;
460  case 'h':
461  usage(EXIT_SUCCESS, progname);
462  break;
463  case 'm':
464  modus = optarg;
465  break;
466  case 'p':
467  particles = optarg;
468  break;
469  case 'e':
470  end_time = optarg;
471  break;
472  case 'o':
473  output_path = optarg;
474  break;
475  case 'l':
476  list2n_activated = true;
477  suppress_disclaimer = true;
478  break;
479  case 'r':
480  resonance_dump_activated = true;
481  pdg_string = optarg;
482  suppress_disclaimer = true;
483  break;
484  case 'S':
485  final_state_cross_sections = true;
486  // fallthrough
487  case 's':
488  cross_section_dump_activated = true;
489  cs_string = optarg;
490  suppress_disclaimer = true;
491  break;
492  case 'x':
493  particles_dump_iSS_format = true;
494  suppress_disclaimer = true;
495  break;
496  case 'v':
497  std::printf(
498  "%s\n"
499  "Branch : %s\nSystem : %s\nCompiler : %s %s\n"
500  "Build : %s\nDate : %s\n",
501  VERSION_MAJOR, GIT_BRANCH, CMAKE_SYSTEM, CMAKE_CXX_COMPILER_ID,
502  CMAKE_CXX_COMPILER_VERSION, CMAKE_BUILD_TYPE, BUILD_DATE);
503  std::exit(EXIT_SUCCESS);
504  case 'n':
505  cache_integrals = false;
506  break;
507  case 'q':
508  suppress_disclaimer = true;
509  break;
510  default:
511  usage(EXIT_FAILURE, progname);
512  }
513  }
514 
515  // Abort if there are unhandled arguments left.
516  if (optind < argc) {
517  std::cout << argv[0] << ": invalid argument -- '" << argv[optind]
518  << "'\n";
519  usage(EXIT_FAILURE, progname);
520  }
521 
522  if (!suppress_disclaimer) {
524  }
525 
526  // Read in config file
527  Configuration configuration(input_path.parent_path(),
528  input_path.filename());
529  // Merge config passed via command line
530  for (const auto &config : extra_config) {
531  configuration.merge_yaml(config);
532  }
533 
534  // Set up logging
536  configuration.take({"Logging", "default"}, einhard::ALL));
537  create_all_loggers(configuration["Logging"]);
538 
540 
541  // check if version matches before doing anything else
543 
545  " create ParticleType and DecayModes");
546 
547  auto particles_and_decays =
548  load_particles_and_decaymodes(particles, decaymodes);
549  /* For particles and decaymodes: external file is superior to config.
550  * Hovever, warn in case of conflict.
551  */
552  if (configuration.has_value({"particles"}) && particles) {
553  logg[LMain].warn(
554  "Ambiguity: particles from external file ", particles,
555  " requested, but there is also particle list in the config."
556  " Using particles from ",
557  particles);
558  }
559  if (!configuration.has_value({"particles"}) || particles) {
560  configuration["particles"] = particles_and_decays.first;
561  }
562 
563  if (configuration.has_value({"decaymodes"}) && decaymodes) {
564  logg[LMain].warn(
565  "Ambiguity: decaymodes from external file ", decaymodes,
566  " requested, but there is also decaymodes list in the config."
567  " Using decaymodes from",
568  decaymodes);
569  }
570  if (!configuration.has_value({"decaymodes"}) || decaymodes) {
571  configuration["decaymodes"] = particles_and_decays.second;
572  }
573 
574  // Calculate a hash of the SMASH version, the particles and decaymodes.
575  const std::string version(VERSION_MAJOR);
576  const std::string particle_string = configuration["particles"].to_string();
577  const std::string decay_string = configuration["decaymodes"].to_string();
578  sha256::Context hash_context;
579  hash_context.update(version);
580  hash_context.update(particle_string);
581  hash_context.update(decay_string);
582  const auto hash = hash_context.finalize();
583  logg[LMain].info() << "Config hash: " << sha256::hash_to_string(hash);
584 
585  bf::path tabulations_path;
586  if (cache_integrals) {
587  tabulations_path = output_path.parent_path() / "tabulations";
588  bf::create_directories(tabulations_path);
589  logg[LMain].info() << "Tabulations path: " << tabulations_path;
590  } else {
591  tabulations_path = "";
592  }
593  if (list2n_activated) {
594  /* Print only 2->n, n > 1. Do not dump decays, which can be found in
595  * decaymodes.txt anyway */
596  configuration.merge_yaml("{Collision_Term: {Two_to_One: False}}");
597  logg[LMain].info() << "Tabulations path: " << tabulations_path;
598  initialize_particles_and_decays(configuration, hash, tabulations_path);
599  auto scat_finder = actions_finder_for_dump(configuration);
600 
603 
604  scat_finder.dump_reactions();
605  std::exit(EXIT_SUCCESS);
606  }
607  if (particles_dump_iSS_format) {
609  ParticleTypePtrList list;
610  list.clear();
611  for (const auto &ptype : ParticleType::list_all()) {
612  list.push_back(&ptype);
613  }
614  std::sort(list.begin(), list.end(),
616  return a->mass() < b->mass();
617  });
618  for (const ParticleTypePtr &ptype : list) {
619  if (ptype->pdgcode().is_lepton() || ptype->baryon_number() < 0) {
620  continue;
621  }
622  const auto &decay_modes = ptype->decay_modes();
623  const auto &modelist = decay_modes.decay_mode_list();
624  int ndecays = ptype->is_stable() ? 1 : modelist.size();
625  std::printf("%13i %s %10.5f %10.5f %5i %5i %5i %5i %5i %5i %5i %5i\n",
626  ptype->pdgcode().get_decimal(),
627  smash::utf8::fill_left(ptype->name(), 12, ' ').c_str(),
628  ptype->mass(), ptype->width_at_pole(),
629  ptype->pdgcode().spin_degeneracy(), ptype->baryon_number(),
630  ptype->strangeness(), ptype->pdgcode().charmness(),
631  ptype->pdgcode().bottomness(), ptype->isospin() + 1,
632  ptype->charge(), ndecays);
633  if (!ptype->is_stable()) {
634  for (const auto &decay : modelist) {
635  auto ptypes = decay->particle_types();
636  std::printf("%13i %13i %20.5f %13i %13i %13i %13i %13i\n",
637  ptype->pdgcode().get_decimal(), 2, decay->weight(),
638  ptypes[0]->pdgcode().get_decimal(),
639  ptypes[1]->pdgcode().get_decimal(), 0, 0, 0);
640  }
641  } else {
642  std::printf("%13i %13i %20.5f %13i %13i %13i %13i %13i\n",
643  ptype->pdgcode().get_decimal(), 1, 1.0,
644  ptype->pdgcode().get_decimal(), 0, 0, 0, 0);
645  }
646  }
647  std::exit(EXIT_SUCCESS);
648  }
649  if (resonance_dump_activated) {
650  // Ignore config values that don't make sense.
651  initialize_particles_and_decays(configuration, hash, tabulations_path);
652  const auto _dummy = ExperimentBase::create(configuration, "");
655 
656  PdgCode pdg(pdg_string);
657  const ParticleType &res = ParticleType::find(pdg);
659  std::exit(EXIT_SUCCESS);
660  }
661  if (cross_section_dump_activated) {
662  initialize_particles_and_decays(configuration, hash, tabulations_path);
663  std::string arg_string(cs_string);
664  std::vector<std::string> args = split(arg_string, ',');
665  const unsigned int n_arg = args.size();
666  if (n_arg != 2 && n_arg != 4 && n_arg < 5) {
667  throw std::invalid_argument("-s usage: pdg1,pdg2[,m1,m2[,sqrts1,...]]");
668  }
669  PdgCode pdg_a(args[0]), pdg_b(args[1]);
670  const ParticleType &a = ParticleType::find(pdg_a);
671  const ParticleType &b = ParticleType::find(pdg_b);
672  if (n_arg < 4) {
673  for (unsigned int i = 0; i < 4 - n_arg; i++) {
674  args.push_back("");
675  }
676  }
677  double ma = (args[2] == "") ? a.mass() : std::stod(args[2]);
678  double mb = (args[3] == "") ? b.mass() : std::stod(args[3]);
679  if (a.is_stable() && args[2] != "" && std::stod(args[2]) != a.mass()) {
680  ma = a.mass();
681  std::cerr << "Warning: pole mass is used for stable particle "
682  << a.name() << " instead of " << args[2] << std::endl;
683  }
684  if (b.is_stable() && args[3] != "" && std::stod(args[3]) != b.mass()) {
685  mb = b.mass();
686  std::cerr << "Warning: pole mass is used for stable particle "
687  << b.name() << " instead of " << args[3] << std::endl;
688  }
689  const size_t plab_size = n_arg <= 4 ? 0 : n_arg - 4;
690  std::vector<double> plab;
691  plab.reserve(plab_size);
692  for (size_t i = 4; i < n_arg; i++) {
693  plab.push_back(std::stod(args.at(i)));
694  }
695  auto scat_finder = actions_finder_for_dump(configuration);
696 
699 
700  scat_finder.dump_cross_sections(a, b, ma, mb, final_state_cross_sections,
701  plab);
702  std::exit(EXIT_SUCCESS);
703  }
704  if (modus) {
705  configuration["General"]["Modus"] = std::string(modus);
706  }
707  if (end_time) {
708  configuration["General"]["End_Time"] = std::abs(std::atof(end_time));
709  }
710 
711  int64_t seed = configuration.read({"General", "Randomseed"});
712  if (seed < 0) {
713  configuration["General"]["Randomseed"] = random::generate_63bit_seed();
714  }
715 
716  // Check output path
717  ensure_path_is_valid(output_path);
718  const bf::path lock_path = output_path / "smash.lock";
719  FileLock lock(lock_path);
720  if (!lock.acquire()) {
721  throw std::runtime_error(
722  "Another instance of SMASH is already writing to the specified "
723  "output directory. If you are sure this is not the case, remove \"" +
724  lock_path.native() + "\".");
725  }
726  logg[LMain].debug("output path: ", output_path);
727  if (!force_overwrite && bf::exists(output_path / "config.yaml")) {
728  throw std::runtime_error(
729  "Output directory would get overwritten. Select a different output "
730  "directory, clean up, or tell SMASH to ignore existing files.");
731  }
732 
733  /* Keep a copy of the configuration that was used in the output directory
734  * also save information about SMASH build as a comment */
735  bf::ofstream(output_path / "config.yaml")
736  << "# " << VERSION_MAJOR << '\n'
737  << "# Branch : " << GIT_BRANCH << '\n'
738  << "# System : " << CMAKE_SYSTEM << '\n'
739  << "# Compiler : " << CMAKE_CXX_COMPILER_ID << ' '
740  << CMAKE_CXX_COMPILER_VERSION << '\n'
741  << "# Build : " << CMAKE_BUILD_TYPE << '\n'
742  << "# Date : " << BUILD_DATE << '\n'
743  << configuration.to_string() << '\n';
745  " create ParticleType and DecayModes");
746  initialize_particles_and_decays(configuration, hash, tabulations_path);
747 
748  // Create an experiment
749  logg[LMain].trace(SMASH_SOURCE_LOCATION, " create Experiment");
750  auto experiment = ExperimentBase::create(configuration, output_path);
751 
752  // Version value is not used in experiment. Get rid of it to prevent
753  // warning.
754  configuration.take({"Version"});
756 
757  // Run the experiment
758  logg[LMain].trace(SMASH_SOURCE_LOCATION, " run the Experiment");
759  experiment->run();
760  } catch (std::exception &e) {
761  logg[LMain].fatal() << "SMASH failed with the following error:\n"
762  << e.what();
763  return EXIT_FAILURE;
764  }
765 
766  logg[LMain].trace() << SMASH_SOURCE_LOCATION << " about to return from main";
767  return 0;
768 }
Interface to the SMASH configuration files.
bool has_value(std::initializer_list< const char * > keys) const
Returns whether there is a non-empty value behind the requested keys.
void merge_yaml(const std::string &yaml)
Merge the configuration in yaml into the existing tree.
std::string to_string() const
Returns a YAML string of the current tree.
Value take(std::initializer_list< const char * > keys)
The default interface for SMASH to read configuration values.
Value read(std::initializer_list< const char * > keys) const
Additional interface for SMASH to read configuration values without removing them.
Guard to create a file lock.
Definition: filelock.h:30
A pointer-like interface to global references to ParticleType objects.
Definition: particletype.h:665
Particle type contains the static properties of a particle species.
Definition: particletype.h:97
void dump_width_and_spectral_function() const
Prints out width and spectral function versus mass to the standard output.
const std::string & name() const
Definition: particletype.h:141
bool is_stable() const
Definition: particletype.h:239
double mass() const
Definition: particletype.h:144
PdgCode stores a Particle Data Group Particle Numbering Scheme particle type number.
Definition: pdgcode.h:108
A SHA256 context.
Definition: sha256.h:28
void update(uint8_t const *buffer, size_t buffer_size)
Add data to the SHA256 context.
Definition: sha256.cc:153
Hash finalize()
Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit h...
Definition: sha256.cc:185
#define SMASH_SOURCE_LOCATION
Hackery that is required to output the location in the source code where the log statement occurs.
Definition: logging.h:243
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > logg
An array that stores all pre-configured Logger objects.
Definition: logging.cc:39
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
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:118
Configuration configuration(std::string overrides={})
Return a configuration object filled with data from src/config.yaml.
Definition: setup.h:173
std::unique_ptr< ExperimentBase > experiment(const Configuration &c=configuration())
Create an experiment.
Definition: setup.h:187
@ ALL
Log all message.
Definition: einhard.hpp:106
void ignore_simulation_config_values(Configuration &configuration)
Remove all config values that are only needed for simulations.
Definition: smash.cc:361
ScatterActionsFinder actions_finder_for_dump(Configuration configuration)
Prepares ActionsFinder for cross-section and reaction dumps.
Definition: smash.cc:309
void check_for_unused_config_values(const Configuration &configuration)
Checks if there are unused config values.
Definition: smash.cc:346
void ensure_path_is_valid(const bf::path &path)
Ensures the output path is valid.
Definition: smash.cc:287
void print_disclaimer()
Print the disclaimer.
Definition: smash.cc:177
void initialize_particles_and_decays(Configuration &configuration, sha256::Hash hash, bf::path tabulations_path)
Initialize the particles and decays from the configuration, the hash and the path to the cashed reson...
Definition: smash.cc:381
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:322
int64_t generate_63bit_seed()
Generates a seed with a truly random 63-bit value, if possible.
Definition: random.cc:18
std::string hash_to_string(Hash hash)
Convert a SHA256 hash to a hexadecimal string.
Definition: sha256.cc:230
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.
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 setup_default_float_traps()
Setup the floating-point traps used throughout SMASH.
static constexpr int LMain
Definition: experiment.h:87
Here is the call graph for this function: