Version: SMASH-2.0
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
 
struct  smash::anonymous_namespace{smash.cc}::OutputDirectoryOutOfIds
 

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 410 of file smash.cc.

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