10 #ifndef SRC_INCLUDE_SMASH_CONFIGURATION_H_
11 #define SRC_INCLUDE_SMASH_CONFIGURATION_H_
20 #include <yaml-cpp/yaml.h>
42 static Node
encode(
const T &x) {
return Node{
static_cast<std::string
>(x)}; }
52 static bool decode(
const Node &node, T &x) {
53 if (!node.IsScalar()) {
56 x =
static_cast<T
>(node.Scalar());
481 using std::runtime_error::runtime_error;
488 using std::runtime_error::runtime_error;
496 using std::runtime_error::runtime_error;
521 if (!(
n.IsScalar() ||
n.IsSequence() ||
n.IsMap())) {
522 std::stringstream err;
523 err <<
"Configuration value for \"" << key
524 <<
"\" is missing or invalid";
525 throw std::runtime_error(err.str());
561 template <
typename T>
574 template <
typename T>
577 return node_.as<T>();
578 }
catch (YAML::TypedBadConversion<T> &e) {
580 "The value for key \"" + std::string(
key_) +
581 "\" cannot be converted to the requested type.");
590 template <
typename T>
591 operator std::vector<T>()
const {
593 return node_.as<std::vector<T>>();
594 }
catch (YAML::TypedBadConversion<T> &e) {
596 "One of the values in the sequence for key \"" + std::string(
key_) +
597 "\" failed to convert to the requested type. E.g. [1 2] is a "
598 "sequence of one string \"1 2\" and [1, 2] is a sequence of two "
599 "integers. Often there is just a comma missing in the config "
601 }
catch (YAML::TypedBadConversion<std::vector<T>> &e) {
603 "The value for key \"" + std::string(
key_) +
604 "\" cannot be converted to the requested type. A sequence was "
605 "expected but apparently not found.");
616 template <
typename T,
size_t N>
617 operator std::array<T, N>()
const {
618 const std::vector<T> vec =
operator std::vector<T>();
619 const size_t n_read = vec.size();
623 std::string(
key_) +
"\". Expected " +
627 std::to_string(n_read) +
".");
629 std::array<T, N> arr;
630 std::copy_n(vec.begin(), N, arr.begin());
642 const std::vector<std::string> v =
operator std::vector<std::string>();
644 for (
const auto &x : v) {
648 }
else if (x ==
"Elastic") {
650 }
else if (x ==
"NN_to_NR") {
652 }
else if (x ==
"NN_to_DR") {
654 }
else if (x ==
"KN_to_KN") {
656 }
else if (x ==
"KN_to_KDelta") {
658 }
else if (x ==
"Strangeness_exchange") {
660 }
else if (x ==
"NNbar") {
662 }
else if (x ==
"PiDeuteron_to_NN") {
664 }
else if (x ==
"PiDeuteron_to_pidprime") {
666 }
else if (x ==
"NDeuteron_to_Ndprime") {
670 "The value for key \"" + std::string(
key_) +
671 "\" should be \"All\", \"Elastic\", \"NN_to_NR\", \"NN_to_DR\","
672 "\"KN_to_KN\", \"KN_to_KDelta\", \"PiDeuteron_to_NN\", "
673 "\"PiDeuteron_to_pidprime\", \"NDeuteron_to_Ndprime\", "
674 "\"Strangeness_exchange\" or "
675 "\"NNbar\", or any combination of these.");
689 const std::vector<std::string> v =
operator std::vector<std::string>();
691 for (
const auto &x : v) {
695 }
else if (x ==
"Meson_3to1") {
697 }
else if (x ==
"Deuteron_3to2") {
699 }
else if (x ==
"NNbar_5to2") {
701 }
else if (x ==
"A3_Nuclei_4to2") {
705 "The value for key \"" + std::string(
key_) +
706 "\" should be \"All\", \"Meson_3to1\", "
707 "\"Deuteron_3to2\" or \"NNbar_5to2\", "
708 "\"A3_Nuclei_4to2\", or any combination of "
722 operator std::set<ThermodynamicQuantity>()
const {
723 const std::vector<std::string> v =
operator std::vector<std::string>();
724 std::set<ThermodynamicQuantity> s;
725 for (
const auto &x : v) {
726 if (x ==
"rho_eckart") {
728 }
else if (x ==
"tmn") {
730 }
else if (x ==
"tmn_landau") {
732 }
else if (x ==
"landau_velocity") {
734 }
else if (x ==
"j_QBS") {
738 "The value for key \"" + std::string(
key_) +
739 "\" should be \"rho_eckart\", \"tmn\""
740 ", \"tmn_landau\", \"landau_velocity\" or \"j_QBS\".");
754 const std::string s =
operator std::string();
755 if (s ==
"center of velocity") {
758 if (s ==
"center of mass") {
761 if (s ==
"fixed target") {
765 "The value for key \"" + std::string(
key_) +
766 "\" should be \"center of velocity\" or \"center of mass\" "
767 "or \"fixed target\".");
778 const std::string s =
operator std::string();
789 "The value for key \"" + std::string(
key_) +
790 "\" should be \"off\" or \"on\" or \"frozen\".");
801 const std::string s =
operator std::string();
808 if (s ==
"baryonic isospin") {
814 if (s ==
"total isospin") {
822 "\" should be \"hadron\" or \"baryon\" "
823 "or \"baryonic isospin\" or \"pion\" "
835 const std::string s =
operator std::string();
836 if (s ==
"NoExpansion") {
839 if (s ==
"MasslessFRW") {
842 if (s ==
"MassiveFRW") {
845 if (s ==
"Exponential") {
849 "The value for key \"" + std::string(
key_) +
850 "\" should be \"NoExpansion\", \"MasslessFRW\"," +
851 "\"MassiveFRW\" or \"Exponential\".");
858 const std::string s =
operator std::string();
859 if (s ==
"Covariant Gaussian") {
862 if (s ==
"Finite difference") {
869 "The value for key \"" + std::string(
key_) +
870 "\" should be \"Covariant Gaussian\", \"Finite difference\"," +
878 const std::string s =
operator std::string();
887 "\" should be \"On\" or \"Off\".");
894 const std::string s =
operator std::string();
895 if (s ==
"Chain Rule") {
902 "The value for key \"" + std::string(
key_) +
903 "\" should be \"Chain Rule\" or \"Direct\".");
910 const std::string s =
operator std::string();
911 if (s ==
"Covariant Gaussian") {
914 if (s ==
"Discrete") {
917 if (s ==
"Triangular") {
921 "The value for key \"" + std::string(
key_) +
922 "\" should be \"Covariant Gaussian\", \"Discrete\"," +
923 " or \"Triangular\".");
934 const std::string s =
operator std::string();
943 "\" should be \"None\" or \"Fixed\".");
954 const std::string s =
operator std::string();
955 if (s ==
"thermal momenta") {
958 if (s ==
"thermal momenta quantum") {
961 if (s ==
"peaked momenta") {
965 "The value for key \"" + std::string(
key_) +
966 "\" should be \"thermal momenta\", \"thermal momenta quantum\", " +
967 "or \"peaked momenta\".");
978 const std::string s =
operator std::string();
979 if (s ==
"thermal momenta") {
982 if (s ==
"thermal momenta quantum") {
994 if (s ==
"IC_Massive") {
998 "The value for key \"" + std::string(
key_) +
999 "\" should be \"thermal momenta\", \"thermal momenta quantum\", " +
1000 "\"IC_ES\", \"IC_1M\", \"IC_2M\" or" +
"\"IC_Massive\".");
1011 const std::string s =
operator std::string();
1012 if (s ==
"no annihilation") {
1015 if (s ==
"resonances") {
1018 if (s ==
"two to five") {
1021 if (s ==
"strings") {
1025 "The value for key \"" + std::string(
key_) +
"\" should be " +
1026 "\"no annihilation\", \"resonances\", \"two to five\" or " +
1038 const std::string s =
operator std::string();
1039 if (s ==
"quadratic") {
1042 if (s ==
"custom") {
1045 if (s ==
"uniform") {
1049 "The value for key \"" + std::string(
key_) +
1050 "\" should be \"quadratic\", \"uniform\" or \"custom\".");
1061 const std::string s =
operator std::string();
1062 if (s ==
"mode sampling") {
1065 if (s ==
"biased BF") {
1068 if (s ==
"unbiased BF") {
1072 "The value for key \"" + std::string(
key_) +
1073 "\" should be \"mode sampling\", \"biased BF\" or \"unbiased BF\".");
1084 const std::string s =
operator std::string();
1085 if (s ==
"Geometric") {
1088 if (s ==
"Stochastic") {
1091 if (s ==
"Covariant") {
1095 "The value for key \"" + std::string(
key_) +
"\" should be " +
1096 "\"Geometric\", \"Stochastic\" " +
"or \"Covariant\".");
1107 const std::string s =
operator std::string();
1114 if (s ==
"IfNotEmpty") {
1118 std::string(
key_) +
"\" should be " +
1119 "\"Yes\", \"No\" or \"IfNotEmpty\".");
1142 explicit Configuration(
const bf::path &path,
const bf::path &filename);
1156 throw std::runtime_error(
1157 "Unknown control flag in Configuration constructor"
1158 " with a YAML formatted string. Please, use"
1159 " Configuration::InitializeFromYAMLString.");
1232 Value take(std::initializer_list<const char *> keys);
1235 template <
typename T>
1236 T
take(std::initializer_list<const char *> keys, T default_value) {
1240 return default_value;
1257 Value
read(std::initializer_list<const char *> keys)
const;
1260 template <
typename T>
1261 T
read(std::initializer_list<const char *> keys, T default_value) {
1265 return default_value;
1289 template <
typename T>
1301 template <
typename T>
1312 std::initializer_list<const char *> keys)
const;
1317 bool has_value(std::initializer_list<const char *> keys)
const;
Return type of Configuration::take that automatically determines the target type.
Value(const YAML::Node &n, const char *key)
Constructs the Value wrapper from a YAML::Node.
Value(const Value &)=delete
If you want to copy this you're doing it wrong.
T convert_for(const T &) const
Convert the value to the type of the supplied argument.
const char *const key_
The key to be interpreted.
const YAML::Node node_
a YAML leaf node
Value & operator=(const Value &)=delete
If you want to copy this you're doing it wrong.
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.
Configuration & operator=(const Configuration &)=default
If you want to copy this you're doing it wrong.
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.
bool has_value_including_empty(std::initializer_list< const char * > keys) const
Returns if there is a (maybe empty) value behind the requested keys.
std::string unused_values_report() const
Returns a string listing the key/value pairs that have not been taken yet.
Configuration(const Configuration &)=default
If you want to copy this you're doing it wrong.
void remove_all_but(const std::string &key)
Removes all entries in the map except for key.
Configuration & operator=(Configuration &&)=default
Moving is fine.
YAML::Node root_node_
the general_config.yaml contents - fully parsed
Configuration(const bf::path &path)
Reads config.yaml from the specified path.
Configuration(int invalue)
Trivial constructor for testing purposes.
Configuration(const YAML::Node &node)
Creates a subobject that has its root node at the given node.
Configuration & operator=(T &&value)
Assignment overwrites the value of the current YAML node.
Configuration(const char *yaml)
Value take(std::initializer_list< const char * > keys)
The default interface for SMASH to read configuration values.
Configuration(const char *yaml, const char sflag)
Initialize configuration with a YAML formatted string.
Configuration operator[](T &&key)
Access to the YAML::Node behind the requested keys.
Configuration(Configuration &&)=default
Moving is fine.
Value read(std::initializer_list< const char * > keys) const
Additional interface for SMASH to read configuration values without removing them.
T take(std::initializer_list< const char * > keys, T default_value)
static const char InitializeFromYAMLString
Flag to mark initialization with a YAML formatted string.
T read(std::initializer_list< const char * > keys, T default_value)
std::vector< std::string > list_upmost_nodes()
Lists all YAML::Nodes from the configuration setup.
SmearingMode
Modes of smearing.
FermiMotion
Option to use Fermi Motion.
@ On
Use fermi motion in combination with potentials.
@ Frozen
Use fermi motion without potentials.
@ Off
Don't use fermi motion.
std::bitset< 10 > ReactionsBitSet
Container for the 2 to 2 reactions in the code.
ThermalizationAlgorithm
Defines the algorithm used for the forced thermalization.
NNbarTreatment
Treatment of N Nbar Annihilation.
@ NoAnnihilation
No Annihilation.
@ TwoToFive
Directly create 5 pions, use with multi-particle reactions.
@ Resonances
Use intermediate Resonances.
@ Strings
Use string fragmentation.
RestFrameDensityDerivativesMode
Modes of calculating the gradients: whether to calculate the rest frame density derivatives.
TimeStepMode
The time step mode.
@ Fixed
Use fixed time step.
@ None
Don't use time steps; propagate from action to action.
Sampling
Possible methods of impact parameter sampling.
@ Quadratic
Sample from areal / quadratic distribution.
@ Custom
Sample from custom, user-defined distribution.
@ Uniform
Sample from uniform distribution.
std::bitset< 4 > MultiParticleReactionsBitSet
Container for the n to m reactions in the code.
CalculationFrame
The calculation frame.
DerivativesMode
Modes of calculating the gradients.
CollisionCriterion
Criteria used to check collisions.
@ Stochastic
Stochastic Criteiron.
@ Geometric
(Default) geometric criterion.
@ Covariant
Covariant Criterion.
FieldDerivativesMode
Modes of calculating the field gradients: chain rule or direct.
SphereInitialCondition
Initial condition for a particle in a sphere.
@ ThermalMomentaBoltzmann
OutputOnlyFinal
Whether and when only final state particles should be printed.
@ IfNotEmpty
Print only final-state particles, and those only if the event is not empty.
@ Yes
Print only final-state particles.
@ No
Print initial, intermediate and final-state particles.
BoxInitialCondition
Initial condition for a particle in a box.
@ ThermalMomentaBoltzmann
ExpansionMode
Defines properties of expansion for the metric (e.g.
DensityType
Allows to choose which kind of density to calculate.
Convert from YAML::Node to SMASH-readable (C++) format and vice versa.
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...
static Node encode(const T &x)
Serialization: Converts x (of any type) to a YAML::Node.
Thrown if the file does not exist.
Thrown when the types in the config file and C++ don't match.
Thrown for YAML parse errors.