Version: SMASH-2.0
configuration.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2020
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_CONFIGURATION_H_
11 #define SRC_INCLUDE_SMASH_CONFIGURATION_H_
12 
13 #include <array>
14 #include <set>
15 #include <stdexcept>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #include <yaml-cpp/yaml.h> // NOLINT(build/include_order)
21 
22 #include "density.h"
23 #include "forwarddeclarations.h"
24 
25 namespace YAML {
26 
33 template <typename T>
34 struct convert {
42  static Node encode(const T &x) { return Node{static_cast<std::string>(x)}; }
43 
52  static bool decode(const Node &node, T &x) {
53  if (!node.IsScalar()) {
54  return false;
55  } else {
56  x = static_cast<T>(node.Scalar());
57  return true;
58  }
59  }
60 };
61 } // namespace YAML
62 
63 namespace smash {
465  public:
470  struct IncorrectTypeInAssignment : public std::runtime_error {
471  using std::runtime_error::runtime_error;
472  };
477  struct ParseError : public std::runtime_error {
478  using std::runtime_error::runtime_error;
479  };
480 
485  struct FileDoesNotExist : public std::runtime_error {
486  using std::runtime_error::runtime_error;
487  };
488 
496  class Value {
497  friend class Configuration;
498 
500  const YAML::Node node_;
502  const char *const key_;
503 
510  Value(const YAML::Node &n, const char *key) : node_(n), key_(key) {
511  if (!(n.IsScalar() || n.IsSequence() || n.IsMap())) {
512  std::stringstream err;
513  err << "Configuration value for \"" << key
514  << "\" is missing or invalid";
515  throw std::runtime_error(err.str());
516  }
517  }
518 
519  public:
521  Value(const Value &) = delete;
523  Value &operator=(const Value &) = delete;
524 
551  template <typename T>
552  T convert_for(const T &) const {
553  return *this;
554  }
555 
564  template <typename T>
565  operator T() const {
566  try {
567  return node_.as<T>();
568  } catch (YAML::TypedBadConversion<T> &e) {
570  "The value for key \"" + std::string(key_) +
571  "\" cannot be converted to the requested type.");
572  }
573  }
574 
580  template <typename T>
581  operator std::vector<T>() const {
582  try {
583  return node_.as<std::vector<T>>();
584  } catch (YAML::TypedBadConversion<T> &e) {
586  "One of the values in the sequence for key \"" + std::string(key_) +
587  "\" failed to convert to the requested type. E.g. [1 2] is a "
588  "sequence of one string \"1 2\" and [1, 2] is a sequence of two "
589  "integers. Often there is just a comma missing in the config "
590  "file.");
591  } catch (YAML::TypedBadConversion<std::vector<T>> &e) {
593  "The value for key \"" + std::string(key_) +
594  "\" cannot be converted to the requested type. A sequence was "
595  "expected but apparently not found.");
596  }
597  }
598 
606  template <typename T, size_t N>
607  operator std::array<T, N>() const {
608  const std::vector<T> vec = operator std::vector<T>();
609  const size_t n_read = vec.size();
610  // Alert if size does not match
611  if (n_read != N) {
612  throw IncorrectTypeInAssignment("Wrong number of values in array \"" +
613  std::string(key_) + "\". Expected " +
614  std::to_string(N) +
615  " values,"
616  " found " +
617  std::to_string(n_read) + ".");
618  }
619  std::array<T, N> arr;
620  std::copy_n(vec.begin(), N, arr.begin());
621  return arr;
622  }
623 
631  operator ReactionsBitSet() const {
632  const std::vector<std::string> v = operator std::vector<std::string>();
633  ReactionsBitSet s;
634  for (const auto &x : v) {
635  if (x == "All") {
636  s.set();
637  break;
638  } else if (x == "Elastic") {
640  } else if (x == "NN_to_NR") {
642  } else if (x == "NN_to_DR") {
644  } else if (x == "KN_to_KN") {
646  } else if (x == "KN_to_KDelta") {
648  } else if (x == "Strangeness_exchange") {
650  } else if (x == "NNbar") {
652  } else if (x == "PiDeuteron_to_NN") {
654  } else if (x == "PiDeuteron_to_pidprime") {
656  } else if (x == "NDeuteron_to_Ndprime") {
658  } else {
660  "The value for key \"" + std::string(key_) +
661  "\" should be \"All\", \"Elastic\", \"NN_to_NR\", \"NN_to_DR\","
662  "\"KN_to_KN\", \"KN_to_KDelta\", \"PiDeuteron_to_NN\", "
663  "\"PiDeuteron_to_pidprime\", \"NDeuteron_to_Ndprime\", "
664  "\"Strangeness_exchange\" or "
665  "\"NNbar\", or any combination of these.");
666  }
667  }
668  return s;
669  }
670 
678  operator MultiParticleReactionsBitSet() const {
679  const std::vector<std::string> v = operator std::vector<std::string>();
681  for (const auto &x : v) {
682  if (x == "All") {
683  s.set();
684  break;
685  } else if (x == "Meson_3to1") {
687  } else if (x == "Deuteron_3to2") {
689  } else {
691  "The value for key \"" + std::string(key_) +
692  "\" should be \"All\", \"Meson_3to1\" or "
693  "\"Deuteron_3to2\", or any combination of these.");
694  }
695  }
696  return s;
697  }
698 
706  operator std::set<ThermodynamicQuantity>() const {
707  const std::vector<std::string> v = operator std::vector<std::string>();
708  std::set<ThermodynamicQuantity> s;
709  for (const auto &x : v) {
710  if (x == "rho_eckart") {
712  } else if (x == "tmn") {
713  s.insert(ThermodynamicQuantity::Tmn);
714  } else if (x == "tmn_landau") {
716  } else if (x == "landau_velocity") {
718  } else if (x == "j_QBS") {
720  } else {
722  "The value for key \"" + std::string(key_) +
723  "\" should be \"rho_eckart\", \"tmn\""
724  ", \"tmn_landau\", \"landau_velocity\" or \"j_QBS\".");
725  }
726  }
727  return s;
728  }
729 
737  operator CalculationFrame() const {
738  const std::string s = operator std::string();
739  if (s == "center of velocity") {
741  }
742  if (s == "center of mass") {
744  }
745  if (s == "fixed target") {
747  }
749  "The value for key \"" + std::string(key_) +
750  "\" should be \"center of velocity\" or \"center of mass\" "
751  "or \"fixed target\".");
752  }
753 
761  operator FermiMotion() const {
762  const std::string s = operator std::string();
763  if (s == "off") {
764  return FermiMotion::Off;
765  }
766  if (s == "on") {
767  return FermiMotion::On;
768  }
769  if (s == "frozen") {
770  return FermiMotion::Frozen;
771  }
773  "The value for key \"" + std::string(key_) +
774  "\" should be \"off\" or \"on\" or \"frozen\".");
775  }
776 
784  operator DensityType() const {
785  const std::string s = operator std::string();
786  if (s == "hadron") {
787  return DensityType::Hadron;
788  }
789  if (s == "baryon") {
790  return DensityType::Baryon;
791  }
792  if (s == "baryonic isospin") {
794  }
795  if (s == "pion") {
796  return DensityType::Pion;
797  }
798  if (s == "total isospin") {
800  }
801  if (s == "none") {
802  return DensityType::None;
803  }
804  throw IncorrectTypeInAssignment("The value for key \"" +
805  std::string(key_) +
806  "\" should be \"hadron\" or \"baryon\" "
807  "or \"baryonic isospin\" or \"pion\" "
808  "or \"none\".");
809  }
810 
818  operator ExpansionMode() const {
819  const std::string s = operator std::string();
820  if (s == "NoExpansion") {
822  }
823  if (s == "MasslessFRW") {
825  }
826  if (s == "MassiveFRW") {
828  }
829  if (s == "Exponential") {
831  }
833  "The value for key \"" + std::string(key_) +
834  "\" should be \"NoExpansion\", \"MasslessFRW\"," +
835  "\"MassiveFRW\" or \"Exponential\".");
836  }
837 
845  operator TimeStepMode() const {
846  const std::string s = operator std::string();
847  if (s == "None") {
848  return TimeStepMode::None;
849  }
850  if (s == "Fixed") {
851  return TimeStepMode::Fixed;
852  }
853  throw IncorrectTypeInAssignment("The value for key \"" +
854  std::string(key_) +
855  "\" should be \"None\" or \"Fixed\".");
856  }
857 
865  operator BoxInitialCondition() const {
866  const std::string s = operator std::string();
867  if (s == "thermal momenta") {
869  }
870  if (s == "thermal momenta quantum") {
872  }
873  if (s == "peaked momenta") {
875  }
877  "The value for key \"" + std::string(key_) +
878  "\" should be \"thermal momenta\", \"thermal momenta quantum\", " +
879  "or \"peaked momenta\".");
880  }
881 
889  operator SphereInitialCondition() const {
890  const std::string s = operator std::string();
891  if (s == "thermal momenta") {
893  }
894  if (s == "thermal momenta quantum") {
896  }
897  if (s == "IC_ES") {
899  }
900  if (s == "IC_1M") {
902  }
903  if (s == "IC_2M") {
905  }
906  if (s == "IC_Massive") {
908  }
910  "The value for key \"" + std::string(key_) +
911  "\" should be \"thermal momenta\", \"thermal momenta quantum\", " +
912  "\"IC_ES\", \"IC_1M\", \"IC_2M\" or" + "\"IC_Massive\".");
913  }
914 
922  operator NNbarTreatment() const {
923  const std::string s = operator std::string();
924  if (s == "no annihilation") {
926  }
927  if (s == "resonances") {
929  }
930  if (s == "strings") {
932  }
934  "The value for key \"" + std::string(key_) + "\" should be " +
935  "\"no annihilation\", \"detailed balance\", or \"strings\".");
936  }
937 
945  operator Sampling() const {
946  const std::string s = operator std::string();
947  if (s == "quadratic") {
948  return Sampling::Quadratic;
949  }
950  if (s == "custom") {
951  return Sampling::Custom;
952  }
953  if (s == "uniform") {
954  return Sampling::Uniform;
955  }
957  "The value for key \"" + std::string(key_) +
958  "\" should be \"quadratic\", \"uniform\" or \"custom\".");
959  }
960 
968  operator ThermalizationAlgorithm() const {
969  const std::string s = operator std::string();
970  if (s == "mode sampling") {
972  }
973  if (s == "biased BF") {
975  }
976  if (s == "unbiased BF") {
978  }
980  "The value for key \"" + std::string(key_) +
981  "\" should be \"mode sampling\", \"biased BF\" or \"unbiased BF\".");
982  }
983 
991  operator CollisionCriterion() const {
992  const std::string s = operator std::string();
993  if (s == "Geometric") {
995  }
996  if (s == "Stochastic") {
998  }
999  if (s == "Covariant") {
1001  }
1003  "The value for key \"" + std::string(key_) + "\" should be " +
1004  "\"Geometric\", \"Stochastic\" " + "or \"Covariant\".");
1005  }
1006 
1014  operator OutputOnlyFinal() const {
1015  const std::string s = operator std::string();
1016  if (s == "Yes") {
1017  return OutputOnlyFinal::Yes;
1018  }
1019  if (s == "No") {
1020  return OutputOnlyFinal::No;
1021  }
1022  if (s == "IfNotEmpty") {
1024  }
1025  throw IncorrectTypeInAssignment("The value for key \"" +
1026  std::string(key_) + "\" should be " +
1027  "\"Yes\", \"No\" or \"IfNotEmpty\".");
1028  }
1029  };
1030 
1036  explicit Configuration(const bf::path &path);
1037 
1045  explicit Configuration(const bf::path &path, const bf::path &filename);
1046 
1047 #ifdef BUILD_TESTS
1048 
1056  explicit Configuration(const char *yaml) : root_node_(YAML::Load(yaml)) {}
1057 #endif
1058 
1060  Configuration(const Configuration &) = default;
1062  Configuration &operator=(const Configuration &) = default;
1063 
1065  Configuration(Configuration &&) = default;
1067  Configuration &operator=(Configuration &&) = default;
1068 
1079  void merge_yaml(const std::string &yaml);
1080 
1082  std::vector<std::string> list_upmost_nodes();
1083 
1105  Value take(std::initializer_list<const char *> keys);
1106 
1108  template <typename T>
1109  T take(std::initializer_list<const char *> keys, T default_value) {
1110  if (has_value(keys)) {
1111  return take(keys);
1112  }
1113  return default_value;
1114  }
1115 
1130  Value read(std::initializer_list<const char *> keys) const;
1131 
1133  template <typename T>
1134  T read(std::initializer_list<const char *> keys, T default_value) {
1135  if (has_value(keys)) {
1136  return read(keys);
1137  }
1138  return default_value;
1139  }
1140 
1146  void remove_all_but(const std::string &key);
1147 
1162  template <typename T>
1164  return root_node_[std::forward<T>(key)];
1165  }
1166 
1174  template <typename T>
1175  Configuration &operator=(T &&value) {
1176  root_node_ = std::forward<T>(value);
1177  return *this;
1178  }
1179 
1185  std::initializer_list<const char *> keys) const;
1190  bool has_value(std::initializer_list<const char *> keys) const;
1191 
1195  std::string unused_values_report() const;
1196 
1202  std::string to_string() const;
1203 
1204  private:
1211  Configuration(const YAML::Node &node) // NOLINT(runtime/explicit) : see above
1212  : root_node_(node) {}
1213 
1215  YAML::Node root_node_;
1216 };
1217 
1218 } // namespace smash
1219 
1220 #endif // SRC_INCLUDE_SMASH_CONFIGURATION_H_
SphereInitialCondition::IC_Massive
smash
Definition: action.h:24
NN_to_DR
Definition: forwarddeclarations.h:220
ThermodynamicQuantity::TmnLandau
YAML::convert::encode
static Node encode(const T &x)
Serialization: Converts x (of any type) to a YAML::Node.
Definition: configuration.h:42
Sampling::Quadratic
Sample from areal / quadratic distribution.
CalculationFrame::CenterOfVelocity
ThermodynamicQuantity::LandauVelocity
smash::Configuration::list_upmost_nodes
std::vector< std::string > list_upmost_nodes()
Lists all YAML::Nodes from the configuration setup.
Definition: configuration.cc:132
TimeStepMode
TimeStepMode
The time step mode.
Definition: forwarddeclarations.h:114
smash::Configuration::Value::node_
const YAML::Node node_
a YAML leaf node
Definition: configuration.h:500
SphereInitialCondition::IC_2M
OutputOnlyFinal
OutputOnlyFinal
Whether and when only final state particles should be printed.
Definition: forwarddeclarations.h:205
YAML::convert
Convert from YAML::Node to SMASH-readable (C++) format and vice versa.
Definition: configuration.h:34
smash::Configuration::root_node_
YAML::Node root_node_
the general_config.yaml contents - fully parsed
Definition: configuration.h:1215
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
NNbar
Definition: forwarddeclarations.h:224
smash::DensityType::BaryonicIsospin
SphereInitialCondition::ThermalMomentaBoltzmann
ExpansionMode::NoExpansion
PiDeuteron_to_NN
Definition: forwarddeclarations.h:225
ExpansionMode::Exponential
NNbarTreatment::Strings
Use string fragmentation.
smash::Configuration::to_string
std::string to_string() const
Returns a YAML string of the current tree.
Definition: configuration.cc:192
smash::Configuration::Value::Value
Value(const YAML::Node &n, const char *key)
Constructs the Value wrapper from a YAML::Node.
Definition: configuration.h:510
FermiMotion::Off
Don't use fermi motion.
smash::Configuration::unused_values_report
std::string unused_values_report() const
Returns a string listing the key/value pairs that have not been taken yet.
Definition: configuration.cc:186
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
BoxInitialCondition
BoxInitialCondition
Initial condition for a particle in a box.
Definition: forwarddeclarations.h:132
Strangeness_exchange
Definition: forwarddeclarations.h:223
smash::Configuration::read
T read(std::initializer_list< const char * > keys, T default_value)
Definition: configuration.h:1134
YAML
Definition: configuration.h:25
FermiMotion::Frozen
Use fermi motion without potentials.
ReactionsBitSet
std::bitset< 10 > ReactionsBitSet
Container for the 2 to 2 reactions in the code.
Definition: forwarddeclarations.h:231
smash::Configuration::ParseError
Definition: configuration.h:477
smash::Configuration::Configuration
Configuration(const YAML::Node &node)
Creates a subobject that has its root node at the given node.
Definition: configuration.h:1211
Sampling::Custom
Sample from custom, user-defined distribution.
smash::Configuration
Interface to the SMASH configuration files.
Definition: configuration.h:464
MultiParticleReactionsBitSet
std::bitset< 2 > MultiParticleReactionsBitSet
Container for the 2 to 2 reactions in the code.
Definition: forwarddeclarations.h:240
ThermalizationAlgorithm::UnbiasedBF
Deuteron_3to2
Definition: forwarddeclarations.h:236
forwarddeclarations.h
ThermalizationAlgorithm
ThermalizationAlgorithm
Defines the algorithm used for the forced thermalization.
Definition: forwarddeclarations.h:251
OutputOnlyFinal::IfNotEmpty
Print only final-state particles, and those only if the event is not empty.
CollisionCriterion::Stochastic
Stochastic Criteiron.
CollisionCriterion::Covariant
Covariant Criterion.
SphereInitialCondition::ThermalMomentaQuantum
smash::Configuration::Configuration
Configuration(const bf::path &path)
Reads config.yaml from the specified path.
Definition: configuration.cc:89
Sampling
Sampling
Possible methods of impact parameter sampling.
Definition: forwarddeclarations.h:104
ThermodynamicQuantity::Tmn
SphereInitialCondition::IC_1M
smash::Configuration::Configuration
Configuration(const char *yaml)
Definition: configuration.h:1056
ExpansionMode::MassiveFRW
BoxInitialCondition::ThermalMomentaQuantum
CollisionCriterion
CollisionCriterion
Criteria used to check collisions.
Definition: forwarddeclarations.h:195
smash::Configuration::Value
Return type of Configuration::take that automatically determines the target type.
Definition: configuration.h:496
density.h
ExpansionMode::MasslessFRW
smash::DensityType::Pion
NDeuteron_to_Ndprime
Definition: forwarddeclarations.h:227
OutputOnlyFinal::Yes
Print only final-state particles.
CalculationFrame::FixedTarget
ThermodynamicQuantity::EckartDensity
ThermalizationAlgorithm::ModeSampling
SphereInitialCondition::IC_ES
SphereInitialCondition
SphereInitialCondition
Initial condition for a particle in a sphere.
Definition: forwarddeclarations.h:151
BoxInitialCondition::ThermalMomentaBoltzmann
KN_to_KN
Definition: forwarddeclarations.h:221
KN_to_KDelta
Definition: forwarddeclarations.h:222
ThermalizationAlgorithm::BiasedBF
NNbarTreatment::Resonances
Use intermediate Resonances.
CalculationFrame
CalculationFrame
The calculation frame.
Definition: forwarddeclarations.h:87
smash::DensityType
DensityType
Allows to choose which kind of density to calculate.
Definition: density.h:36
Sampling::Uniform
Sample from uniform distribution.
smash::Configuration::Value::key_
const char *const key_
The key to be interpreted.
Definition: configuration.h:502
smash::Configuration::merge_yaml
void merge_yaml(const std::string &yaml)
Merge the configuration in yaml into the existing tree.
Definition: configuration.cc:118
FermiMotion::On
Use fermi motion in combination with potentials.
smash::Configuration::Value::convert_for
T convert_for(const T &) const
Convert the value to the type of the supplied argument.
Definition: configuration.h:552
smash::ProcessType::Elastic
elastic scattering: particles remain the same, only momenta change
smash::Configuration::IncorrectTypeInAssignment
Definition: configuration.h:470
BoxInitialCondition::PeakedMomenta
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::DensityType::None
smash::Configuration::FileDoesNotExist
Definition: configuration.h:485
NN_to_NR
Definition: forwarddeclarations.h:219
smash::DensityType::Isospin3_tot
ThermodynamicQuantity::j_QBS
smash::Configuration::has_value_including_empty
bool has_value_including_empty(std::initializer_list< const char * > keys) const
Returns if there is a (maybe empty) value behind the requested keys.
Definition: configuration.cc:175
smash::Configuration::operator=
Configuration & operator=(T &&value)
Assignment overwrites the value of the current YAML node.
Definition: configuration.h:1175
NNbarTreatment
NNbarTreatment
Treatment of N Nbar Annihilation.
Definition: forwarddeclarations.h:176
smash::Configuration::Value::operator=
Value & operator=(const Value &)=delete
If you want to copy this you're doing it wrong.
smash::pdg::n
constexpr int n
Neutron.
Definition: pdgcode_constants.h:30
smash::Configuration::operator=
Configuration & operator=(const Configuration &)=default
If you want to copy this you're doing it wrong.
NNbarTreatment::NoAnnihilation
No Annihilation.
CalculationFrame::CenterOfMass
PiDeuteron_to_pidprime
Definition: forwarddeclarations.h:226
smash::DensityType::Hadron
CollisionCriterion::Geometric
(Default) geometric criterion.
ExpansionMode
ExpansionMode
Defines properties of expansion for the metric (e.g.
Definition: forwarddeclarations.h:168
smash::DensityType::Baryon
smash::Configuration::remove_all_but
void remove_all_but(const std::string &key)
Removes all entries in the map except for key.
Definition: configuration.cc:163
smash::Configuration::operator[]
Configuration operator[](T &&key)
Access to the YAML::Node behind the requested keys.
Definition: configuration.h:1163
smash::NeedsToWrap::No
TimeStepMode::Fixed
Use fixed time step.
YAML::convert::decode
static bool decode(const Node &node, T &x)
Deserialization: Converts a YAML::Node to any SMASH-readable data type and returns whether or not thi...
Definition: configuration.h:52
FermiMotion
FermiMotion
Option to use Fermi Motion.
Definition: forwarddeclarations.h:94
smash::Configuration::take
T take(std::initializer_list< const char * > keys, T default_value)
Definition: configuration.h:1109
Meson_3to1
Definition: forwarddeclarations.h:235