Version: SMASH-2.2
library.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2022-
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #include "smash/library.h"
11 
12 #include <boost/filesystem.hpp>
13 
14 #include "smash/decaymodes.h"
15 #include "smash/inputfunctions.h"
16 #include "smash/isoparticletype.h"
17 #include "smash/logging.h"
19 
20 namespace smash {
21 static constexpr int LMain = LogArea::Main::id;
22 
24  const std::string &config_file, const std::string &particles_file,
25  const std::string &decaymodes_file,
26  const std::vector<std::string> &extra_config) {
27  // Read in config file
28  bf::path config_path(config_file);
29  Configuration configuration(config_path.parent_path(),
30  config_path.filename());
31 
32  // Merge config passed via command line
33  for (const auto &config : extra_config) {
34  configuration.merge_yaml(config);
35  }
36 
37  // Set up logging
39  configuration.take({"Logging", "default"}, einhard::ALL));
41 
42  logg[LMain].trace(SMASH_SOURCE_LOCATION, " load ParticleType and DecayModes");
43 
44  bf::path particles_path(particles_file);
45  bf::path decaymodes_path(decaymodes_file);
46  auto particles_and_decays =
47  load_particles_and_decaymodes(particles_path, decaymodes_path);
48  /* For particles and decaymodes: external file is superior to config.
49  * However, warn in case of conflict.
50  */
51  if (configuration.has_value({"particles"}) && !particles_path.empty()) {
52  logg[LMain].warn(
53  "Ambiguity: particles from external file ", particles_path,
54  " requested, but there is also particle list in the config."
55  " Using particles from ",
56  particles_path);
57  }
58  if (!configuration.has_value({"particles"}) || !particles_path.empty()) {
59  configuration["particles"] = particles_and_decays.first;
60  }
61 
62  if (configuration.has_value({"decaymodes"}) && !decaymodes_path.empty()) {
63  logg[LMain].warn(
64  "Ambiguity: decaymodes from external file ", decaymodes_path,
65  " requested, but there is also decaymodes list in the config."
66  " Using decaymodes from",
67  decaymodes_path);
68  }
69  if (!configuration.has_value({"decaymodes"}) || !decaymodes_path.empty()) {
70  configuration["decaymodes"] = particles_and_decays.second;
71  }
72 
73  return configuration;
74 }
75 
77  Configuration &configuration, const std::string &version,
78  const std::string &tabulations_dir) {
80  " create ParticleType and DecayModes");
84 
85  // Calculate a hash of the SMASH version, the particles and decaymodes.
86  const std::string particle_string = configuration["particles"].to_string();
87  const std::string decay_string = configuration["decaymodes"].to_string();
88  sha256::Context hash_context;
89  hash_context.update(version);
90  hash_context.update(particle_string);
91  hash_context.update(decay_string);
92  const auto hash = hash_context.finalize();
93  logg[LMain].info() << "Config hash: " << sha256::hash_to_string(hash);
94 
95  logg[LMain].info("Tabulating cross section integrals...");
96  bf::path tabulations_path(tabulations_dir);
97  if (!tabulations_path.empty()) {
98  // Store tabulations on disk
99  bf::create_directories(tabulations_path);
100  logg[LMain].info() << "Tabulations path: " << tabulations_path;
101  }
102  IsoParticleType::tabulate_integrals(hash, tabulations_path);
103 }
104 
105 } // namespace smash
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.
static void load_decaymodes(const std::string &input)
Loads the DecayModes map as described in the input string.
Definition: decaymodes.cc:164
static void tabulate_integrals(sha256::Hash hash, const bf::path &tabulations_path)
Tabulate all relevant integrals.
static void check_consistency()
static void create_type_list(const std::string &particles)
Initialize the global ParticleType list (list_all) from the given input data.
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
@ ALL
Log all message.
Definition: einhard.hpp:106
std::string hash_to_string(Hash hash)
Convert a SHA256 hash to a hexadecimal string.
Definition: sha256.cc:230
Definition: action.h:24
void initialize_particles_decays_and_tabulations(Configuration &configuration, const std::string &version, const std::string &tabulations_dir={})
Initialize the particles and decays from the given configuration, plus tabulate the resonance integra...
Definition: library.cc:76
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:23
static constexpr int LMain
Definition: experiment.h:87
std::pair< std::string, std::string > load_particles_and_decaymodes(const boost::filesystem::path &particles_file, const boost::filesystem::path &decaymodes_file)
Loads particles and decaymodes from provided files particles_file and decaymodes_file.