Version: SMASH-3.1
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 <filesystem>
13 
14 #include "smash/configuration.h"
15 #include "smash/decaymodes.h"
16 #include "smash/inputfunctions.h"
17 #include "smash/isoparticletype.h"
18 #include "smash/logging.h"
20 #include "smash/stringfunctions.h"
21 
22 namespace smash {
23 static constexpr int LMain = LogArea::Main::id;
24 
25 static Configuration create_configuration(const std::string &,
26  const std::vector<std::string> &);
28 static void fully_validate_configuration(const Configuration &);
29 static void setup_logging(Configuration &);
31  const std::string &, const std::string &, Configuration &);
32 
34  const std::string &config_file, const std::string &particles_file,
35  const std::string &decaymodes_file,
36  const std::vector<std::string> &extra_config) {
37  Configuration configuration = create_configuration(config_file, extra_config);
38  fully_validate_configuration(configuration);
39  setup_logging(configuration);
41  particles_file, decaymodes_file, configuration);
42  return configuration;
43 }
44 
46  Configuration &configuration, const std::string &version,
47  const std::string &tabulations_dir) {
49  " create ParticleType and DecayModes");
50  const std::string particles_string = configuration.take({"particles"});
51  const std::string decaymodes_string = configuration.take({"decaymodes"});
52  ParticleType::create_type_list(particles_string);
53  DecayModes::load_decaymodes(decaymodes_string);
55 
56  // Calculate a hash of the SMASH version, the particles and decaymodes.
57  sha256::Context hash_context;
58  hash_context.update(version);
59  hash_context.update(particles_string);
60  hash_context.update(decaymodes_string);
61  const auto hash = hash_context.finalize();
62  logg[LMain].info() << "Config hash: " << sha256::hash_to_string(hash);
63 
64  logg[LMain].info("Tabulating cross section integrals...");
65  std::filesystem::path tabulations_path(tabulations_dir);
66  if (!tabulations_path.empty()) {
67  // Store tabulations on disk
68  std::filesystem::create_directories(tabulations_path);
69  logg[LMain].info() << "Tabulations path: " << tabulations_path;
70  }
71  IsoParticleType::tabulate_integrals(hash, tabulations_path);
72 }
73 
75  const std::string &config_file,
76  const std::vector<std::string> &extra_config) {
77  // Read in config file
78  std::filesystem::path config_path(config_file);
79  Configuration configuration(config_path.parent_path(),
80  config_path.filename());
81 
82  // Merge config passed via command line
83  for (const auto &config : extra_config) {
84  configuration.merge_yaml(config);
85  }
86  return configuration;
87 }
88 
90  const std::string conf_tag = LogArea::Configuration::textual();
91  const std::string main_tag = LogArea::Main::textual();
92  const auto size =
93  conf_tag.size() > main_tag.size() ? conf_tag.size() : main_tag.size();
94  logg[LogArea::Configuration::id].setAreaName(utf8::fill_both(conf_tag, size));
95  logg[LogArea::Main::id].setAreaName(utf8::fill_both(main_tag, size));
96 }
97 
98 static void fully_validate_configuration(const Configuration &configuration) {
100  if (configuration.validate() == Configuration::Is::Invalid) {
101  throw std::runtime_error("Validation of SMASH input failed.");
102  }
103 }
104 
105 static void setup_logging(Configuration &configuration) {
107  configuration.take({"Logging", "default"}, einhard::ALL));
109  {"Logging"}, Configuration::GetEmpty::Yes));
110 }
111 
113  const std::string &particles_file, const std::string &decaymodes_file,
114  Configuration &configuration) {
115  logg[LMain].trace(SMASH_SOURCE_LOCATION, " load ParticleType and DecayModes");
116  std::filesystem::path particles_path(particles_file);
117  std::filesystem::path decaymodes_path(decaymodes_file);
118  auto particles_and_decays =
119  load_particles_and_decaymodes(particles_path, decaymodes_path);
120  /* For particles and decaymodes: external file is superior to config.
121  * However, warn in case of conflict. */
122  if (configuration.has_value({"particles"}) && !particles_path.empty()) {
123  logg[LMain].warn(
124  "Ambiguity: particles from external file ", particles_path,
125  " requested, but there is also particle list in the config."
126  " Using particles from ",
127  particles_path);
128  }
129  if (!configuration.has_value({"particles"}) || !particles_path.empty()) {
130  configuration.set_value({"particles"}, particles_and_decays.first);
131  }
132 
133  if (configuration.has_value({"decaymodes"}) && !decaymodes_path.empty()) {
134  logg[LMain].warn(
135  "Ambiguity: decaymodes from external file ", decaymodes_path,
136  " requested, but there is also decaymodes list in the config."
137  " Using decaymodes from",
138  decaymodes_path);
139  }
140  if (!configuration.has_value({"decaymodes"}) || !decaymodes_path.empty()) {
141  configuration.set_value({"decaymodes"}, particles_and_decays.second);
142  }
143 }
144 
145 } // namespace smash
Interface to the SMASH configuration files.
bool has_value(std::initializer_list< const char * > keys) const
Return 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.
void set_value(std::initializer_list< const char * > keys, T &&value)
Overwrite the value of the specified YAML node.
Configuration extract_sub_configuration(std::initializer_list< const char * > keys, Configuration::GetEmpty empty_if_not_existing=Configuration::GetEmpty::No)
Create a new configuration from a then-removed section of the present object.
Value take(std::initializer_list< const char * > keys)
The default interface for SMASH to read configuration values.
Is validate(bool full_validation=true) const
Validate content of configuration in terms of YAML keys.
static void load_decaymodes(const std::string &input)
Loads the DecayModes map as described in the input string.
Definition: decaymodes.cc:163
static void tabulate_integrals(sha256::Hash hash, const std::filesystem::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:153
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:119
@ 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
std::string fill_both(const std::string &s, size_t width, char fill=' ')
Fill string with characters at both sides until the given width is reached.
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:45
static void do_minimal_loggers_setup_for_config_validation()
Definition: library.cc:89
static void read_particles_and_decaymodes_files_setting_keys_in_configuration(const std::string &, const std::string &, Configuration &)
Definition: library.cc:112
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:33
static Configuration create_configuration(const std::string &, const std::vector< std::string > &)
Definition: library.cc:74
static void setup_logging(Configuration &)
Definition: library.cc:105
std::pair< std::string, std::string > load_particles_and_decaymodes(const std::filesystem::path &particles_file, const std::filesystem::path &decaymodes_file)
Loads particles and decaymodes from provided files particles_file and decaymodes_file.
static constexpr int LMain
Definition: experiment.h:89
static void fully_validate_configuration(const Configuration &)
Definition: library.cc:98