Version: SMASH-1.8
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 406 of file smash.cc.

406  {
407  using namespace smash; // NOLINT(build/namespaces)
408 
409  constexpr option longopts[] = {
410  {"config", required_argument, 0, 'c'},
411  {"decaymodes", required_argument, 0, 'd'},
412  {"endtime", required_argument, 0, 'e'},
413  {"force", no_argument, 0, 'f'},
414  {"help", no_argument, 0, 'h'},
415  {"inputfile", required_argument, 0, 'i'},
416  {"modus", required_argument, 0, 'm'},
417  {"particles", required_argument, 0, 'p'},
418  {"output", required_argument, 0, 'o'},
419  {"list-2-to-n", no_argument, 0, 'l'},
420  {"resonance", required_argument, 0, 'r'},
421  {"cross-sections", required_argument, 0, 's'},
422  {"cross-sections-fs", required_argument, 0, 'S'},
423  {"dump-iSS", no_argument, 0, 'x'},
424  {"version", no_argument, 0, 'v'},
425  {"no-cache", no_argument, 0, 'n'},
426  {nullptr, 0, 0, 0}};
427 
428  // strip any path to progname
429  const std::string progname = bf::path(argv[0]).filename().native();
430 
431  try {
432  bool force_overwrite = false;
433  bf::path output_path = default_output_path(), input_path("./config.yaml");
434  std::vector<std::string> extra_config;
435  char *particles = nullptr, *decaymodes = nullptr, *modus = nullptr,
436  *end_time = nullptr, *pdg_string = nullptr, *cs_string = nullptr;
437  bool list2n_activated = false;
438  bool resonance_dump_activated = false;
439  bool cross_section_dump_activated = false;
440  bool final_state_cross_sections = false;
441  bool particles_dump_iSS_format = false;
442  bool cache_integrals = true;
443 
444  // parse command-line arguments
445  int opt;
446  bool suppress_disclaimer = false;
447  while ((opt = getopt_long(argc, argv, "c:d:e:fhi:m:p:o:lr:s:S:xvn",
448  longopts, nullptr)) != -1) {
449  switch (opt) {
450  case 'c':
451  extra_config.emplace_back(optarg);
452  break;
453  case 'd':
454  decaymodes = optarg;
455  break;
456  case 'f':
457  force_overwrite = true;
458  break;
459  case 'i':
460  input_path = optarg;
461  break;
462  case 'h':
463  usage(EXIT_SUCCESS, progname);
464  break;
465  case 'm':
466  modus = optarg;
467  break;
468  case 'p':
469  particles = optarg;
470  break;
471  case 'e':
472  end_time = optarg;
473  break;
474  case 'o':
475  output_path = optarg;
476  break;
477  case 'l':
478  list2n_activated = true;
479  suppress_disclaimer = true;
480  break;
481  case 'r':
482  resonance_dump_activated = true;
483  pdg_string = optarg;
484  suppress_disclaimer = true;
485  break;
486  case 'S':
487  final_state_cross_sections = true;
488  // fallthrough
489  case 's':
490  cross_section_dump_activated = true;
491  cs_string = optarg;
492  suppress_disclaimer = true;
493  break;
494  case 'x':
495  particles_dump_iSS_format = true;
496  suppress_disclaimer = true;
497  break;
498  case 'v':
499  std::printf(
500  "%s\n"
501  "Branch : %s\nSystem : %s\nCompiler : %s %s\n"
502  "Build : %s\nDate : %s\n",
503  VERSION_MAJOR, GIT_BRANCH, CMAKE_SYSTEM, CMAKE_CXX_COMPILER_ID,
504  CMAKE_CXX_COMPILER_VERSION, CMAKE_BUILD_TYPE, BUILD_DATE);
505  std::exit(EXIT_SUCCESS);
506  case 'n':
507  cache_integrals = false;
508  break;
509  default:
510  usage(EXIT_FAILURE, progname);
511  }
512  }
513 
514  // Abort if there are unhandled arguments left.
515  if (optind < argc) {
516  std::cout << argv[0] << ": invalid argument -- '" << argv[optind]
517  << "'\n";
518  usage(EXIT_FAILURE, progname);
519  }
520 
521  if (!suppress_disclaimer) {
523  }
524 
526 
527  // Read in config file
528  Configuration configuration(input_path.parent_path(),
529  input_path.filename());
530  // Merge config passed via command line
531  for (const auto &config : extra_config) {
532  configuration.merge_yaml(config);
533  }
534 
535  // Set up logging
537  configuration.take({"Logging", "default"}, einhard::ALL));
538  create_all_loggers(configuration["Logging"]);
539 
540  // check if version matches before doing anything else
542 
543  logg[LMain].trace(source_location, " create ParticleType and DecayModes");
544 
545  auto particles_and_decays =
546  load_particles_and_decaymodes(particles, decaymodes);
547  /* For particles and decaymodes: external file is superior to config.
548  * Hovever, warn in case of conflict.
549  */
550  if (configuration.has_value({"particles"}) && particles) {
551  logg[LMain].warn(
552  "Ambiguity: particles from external file ", particles,
553  " requested, but there is also particle list in the config."
554  " Using particles from ",
555  particles);
556  }
557  if (!configuration.has_value({"particles"}) || particles) {
558  configuration["particles"] = particles_and_decays.first;
559  }
560 
561  if (configuration.has_value({"decaymodes"}) && decaymodes) {
562  logg[LMain].warn(
563  "Ambiguity: decaymodes from external file ", decaymodes,
564  " requested, but there is also decaymodes list in the config."
565  " Using decaymodes from",
566  decaymodes);
567  }
568  if (!configuration.has_value({"decaymodes"}) || decaymodes) {
569  configuration["decaymodes"] = particles_and_decays.second;
570  }
571 
572  // Calculate a hash of the SMASH version, the particles and decaymodes.
573  const std::string version(VERSION_MAJOR);
574  const std::string particle_string = configuration["particles"].to_string();
575  const std::string decay_string = configuration["decaymodes"].to_string();
576  sha256::Context hash_context;
577  hash_context.update(version);
578  hash_context.update(particle_string);
579  hash_context.update(decay_string);
580  const auto hash = hash_context.finalize();
581  logg[LMain].info() << "Config hash: " << sha256::hash_to_string(hash);
582 
583  bf::path tabulations_path;
584  if (cache_integrals) {
585  tabulations_path = output_path.parent_path() / "tabulations";
586  bf::create_directories(tabulations_path);
587  logg[LMain].info() << "Tabulations path: " << tabulations_path;
588  } else {
589  tabulations_path = "";
590  }
591  if (list2n_activated) {
592  /* Print only 2->n, n > 1. Do not dump decays, which can be found in
593  * decaymodes.txt anyway */
594  configuration.merge_yaml("{Collision_Term: {Two_to_One: False}}");
595  logg[LMain].info() << "Tabulations path: " << tabulations_path;
596  initialize_particles_and_decays(configuration, hash, tabulations_path);
597  auto scat_finder = actions_finder_for_dump(configuration);
598 
601 
602  scat_finder.dump_reactions();
603  std::exit(EXIT_SUCCESS);
604  }
605  if (particles_dump_iSS_format) {
607  ParticleTypePtrList list;
608  list.clear();
609  for (const auto &ptype : ParticleType::list_all()) {
610  list.push_back(&ptype);
611  }
612  std::sort(list.begin(), list.end(),
614  return a->mass() < b->mass();
615  });
616  for (const ParticleTypePtr ptype : list) {
617  if (ptype->pdgcode().is_lepton() || ptype->baryon_number() < 0) {
618  continue;
619  }
620  const auto &decay_modes = ptype->decay_modes();
621  const auto &modelist = decay_modes.decay_mode_list();
622  int ndecays = ptype->is_stable() ? 1 : modelist.size();
623  printf("%13i %s %10.5f %10.5f %5i %5i %5i %5i %5i %5i %5i %5i\n",
624  ptype->pdgcode().get_decimal(),
625  smash::utf8::fill_left(ptype->name(), 12, ' ').c_str(),
626  ptype->mass(), ptype->width_at_pole(),
627  ptype->pdgcode().spin_degeneracy(), ptype->baryon_number(),
628  ptype->strangeness(), ptype->pdgcode().charmness(),
629  ptype->pdgcode().bottomness(), ptype->isospin() + 1,
630  ptype->charge(), ndecays);
631  if (!ptype->is_stable()) {
632  for (const auto &decay : modelist) {
633  auto ptypes = decay->particle_types();
634  printf("%13i %13i %20.5f %13i %13i %13i %13i %13i\n",
635  ptype->pdgcode().get_decimal(), 2, decay->weight(),
636  ptypes[0]->pdgcode().get_decimal(),
637  ptypes[1]->pdgcode().get_decimal(), 0, 0, 0);
638  }
639  } else {
640  printf("%13i %13i %20.5f %13i %13i %13i %13i %13i\n",
641  ptype->pdgcode().get_decimal(), 1, 1.0,
642  ptype->pdgcode().get_decimal(), 0, 0, 0, 0);
643  }
644  }
645  std::exit(EXIT_SUCCESS);
646  }
647  if (resonance_dump_activated) {
648  // Ignore config values that don't make sense.
649  initialize_particles_and_decays(configuration, hash, tabulations_path);
650  const auto _dummy = ExperimentBase::create(configuration, "");
653 
654  PdgCode pdg(pdg_string);
655  const ParticleType &res = ParticleType::find(pdg);
657  std::exit(EXIT_SUCCESS);
658  }
659  if (cross_section_dump_activated) {
660  initialize_particles_and_decays(configuration, hash, tabulations_path);
661  std::string arg_string(cs_string);
662  std::vector<std::string> args = split(arg_string, ',');
663  const unsigned int n_arg = args.size();
664  if (n_arg != 2 && n_arg != 4 && n_arg < 5) {
665  throw std::invalid_argument("-s usage: pdg1,pdg2[,m1,m2[,sqrts1,...]]");
666  }
667  PdgCode pdg_a(args[0]), pdg_b(args[1]);
668  const ParticleType &a = ParticleType::find(pdg_a);
669  const ParticleType &b = ParticleType::find(pdg_b);
670  if (n_arg < 4) {
671  for (unsigned int i = 0; i < 4 - n_arg; i++) {
672  args.push_back("");
673  }
674  }
675  double ma = (args[2] == "") ? a.mass() : std::stod(args[2]);
676  double mb = (args[3] == "") ? b.mass() : std::stod(args[3]);
677  if (a.is_stable() && args[2] != "" && std::stod(args[2]) != a.mass()) {
678  ma = a.mass();
679  std::cerr << "Warning: pole mass is used for stable particle "
680  << a.name() << " instead of " << args[2] << std::endl;
681  }
682  if (b.is_stable() && args[3] != "" && std::stod(args[3]) != b.mass()) {
683  mb = b.mass();
684  std::cerr << "Warning: pole mass is used for stable particle "
685  << b.name() << " instead of " << args[3] << std::endl;
686  }
687  const size_t plab_size = n_arg <= 4 ? 0 : n_arg - 4;
688  std::vector<double> plab;
689  plab.reserve(plab_size);
690  for (size_t i = 4; i < n_arg; i++) {
691  plab.push_back(std::stod(args.at(i)));
692  }
693  auto scat_finder = actions_finder_for_dump(configuration);
694 
697 
698  scat_finder.dump_cross_sections(a, b, ma, mb, final_state_cross_sections,
699  plab);
700  std::exit(EXIT_SUCCESS);
701  }
702  if (modus) {
703  configuration["General"]["Modus"] = std::string(modus);
704  }
705  if (end_time) {
706  configuration["General"]["End_Time"] = std::abs(std::atof(end_time));
707  }
708 
709  int64_t seed = configuration.read({"General", "Randomseed"});
710  if (seed < 0) {
711  configuration["General"]["Randomseed"] = random::generate_63bit_seed();
712  }
713 
714  // Check output path
715  ensure_path_is_valid(output_path);
716  const bf::path lock_path = output_path / "smash.lock";
717  FileLock lock(lock_path);
718  if (!lock.acquire()) {
719  throw std::runtime_error(
720  "Another instance of SMASH is already writing to the specified "
721  "output directory. If you are sure this is not the case, remove \"" +
722  lock_path.native() + "\".");
723  }
724  logg[LMain].debug("output path: ", output_path);
725  if (!force_overwrite && bf::exists(output_path / "config.yaml")) {
726  throw std::runtime_error(
727  "Output directory would get overwritten. Select a different output "
728  "directory, clean up, or tell SMASH to ignore existing files.");
729  }
730 
731  /* Keep a copy of the configuration that was used in the output directory
732  * also save information about SMASH build as a comment */
733  bf::ofstream(output_path / "config.yaml")
734  << "# " << VERSION_MAJOR << '\n'
735  << "# Branch : " << GIT_BRANCH << '\n'
736  << "# System : " << CMAKE_SYSTEM << '\n'
737  << "# Compiler : " << CMAKE_CXX_COMPILER_ID << ' '
738  << CMAKE_CXX_COMPILER_VERSION << '\n'
739  << "# Build : " << CMAKE_BUILD_TYPE << '\n'
740  << "# Date : " << BUILD_DATE << '\n'
741  << configuration.to_string() << '\n';
742  logg[LMain].trace(source_location, " create ParticleType and DecayModes");
743  initialize_particles_and_decays(configuration, hash, tabulations_path);
744 
745  // Create an experiment
746  logg[LMain].trace(source_location, " create Experiment");
747  auto experiment = ExperimentBase::create(configuration, output_path);
748 
749  // Version value is not used in experiment. Get rid of it to prevent
750  // warning.
751  configuration.take({"Version"});
753 
754  // Run the experiment
755  logg[LMain].trace(source_location, " run the Experiment");
756  experiment->run();
757  } catch (std::exception &e) {
758  logg[LMain].fatal() << "SMASH failed with the following error:\n"
759  << e.what();
760  return EXIT_FAILURE;
761  }
762 
763  logg[LMain].trace() << source_location << " about to return from main";
764  return 0;
765 }
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:730
smash::Test::configuration
Configuration configuration(std::string overrides={})
Return a configuration object filled with data from src/config.yaml.
Definition: setup.h:161
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:349
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:19
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:77
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:105
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:325
smash::Configuration
Interface to the SMASH configuration files.
Definition: configuration.h:464
smash::ParticleTypePtr
Definition: particletype.h:663
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:240
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:236
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:364
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:384
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:283
smash::Test::experiment
std::unique_ptr< ExperimentBase > experiment(const Configuration &c=configuration())
Create an experiment.
Definition: setup.h:175
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:259
smash::ParticleType::list_all
static const ParticleTypeList & list_all()
Definition: particletype.cc:57
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:305
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:173