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());
479 using std::runtime_error::runtime_error;
486 using std::runtime_error::runtime_error;
494 using std::runtime_error::runtime_error;
519 if (!(
n.IsScalar() ||
n.IsSequence() ||
n.IsMap())) {
520 std::stringstream err;
521 err <<
"Configuration value for \"" << key
522 <<
"\" is missing or invalid";
523 throw std::runtime_error(err.str());
559 template <
typename T>
572 template <
typename T>
575 return node_.as<T>();
576 }
catch (YAML::TypedBadConversion<T> &e) {
578 "The value for key \"" + std::string(
key_) +
579 "\" cannot be converted to the requested type.");
588 template <
typename T>
589 operator std::vector<T>()
const {
591 return node_.as<std::vector<T>>();
592 }
catch (YAML::TypedBadConversion<T> &e) {
594 "One of the values in the sequence for key \"" + std::string(
key_) +
595 "\" failed to convert to the requested type. E.g. [1 2] is a "
596 "sequence of one string \"1 2\" and [1, 2] is a sequence of two "
597 "integers. Often there is just a comma missing in the config "
599 }
catch (YAML::TypedBadConversion<std::vector<T>> &e) {
601 "The value for key \"" + std::string(
key_) +
602 "\" cannot be converted to the requested type. A sequence was "
603 "expected but apparently not found.");
614 template <
typename T,
size_t N>
615 operator std::array<T, N>()
const {
616 const std::vector<T> vec =
operator std::vector<T>();
617 const size_t n_read = vec.size();
621 std::string(
key_) +
"\". Expected " +
625 std::to_string(n_read) +
".");
627 std::array<T, N> arr;
628 std::copy_n(vec.begin(), N, arr.begin());
640 const std::vector<std::string> v =
operator std::vector<std::string>();
642 for (
const auto &x : v) {
646 }
else if (x ==
"Elastic") {
648 }
else if (x ==
"NN_to_NR") {
650 }
else if (x ==
"NN_to_DR") {
652 }
else if (x ==
"KN_to_KN") {
654 }
else if (x ==
"KN_to_KDelta") {
656 }
else if (x ==
"Strangeness_exchange") {
658 }
else if (x ==
"NNbar") {
660 }
else if (x ==
"PiDeuteron_to_NN") {
662 }
else if (x ==
"PiDeuteron_to_pidprime") {
664 }
else if (x ==
"NDeuteron_to_Ndprime") {
668 "The value for key \"" + std::string(
key_) +
669 "\" should be \"All\", \"Elastic\", \"NN_to_NR\", \"NN_to_DR\","
670 "\"KN_to_KN\", \"KN_to_KDelta\", \"PiDeuteron_to_NN\", "
671 "\"PiDeuteron_to_pidprime\", \"NDeuteron_to_Ndprime\", "
672 "\"Strangeness_exchange\" or "
673 "\"NNbar\", or any combination of these.");
687 const std::vector<std::string> v =
operator std::vector<std::string>();
689 for (
const auto &x : v) {
693 }
else if (x ==
"Meson_3to1") {
695 }
else if (x ==
"Deuteron_3to2") {
697 }
else if (x ==
"NNbar_5to2") {
701 "The value for key \"" + std::string(
key_) +
702 "\" should be \"All\", \"Meson_3to1\", "
703 "\"Deuteron_3to2\" or \"NNbar_5to2\", or any combination of "
717 operator std::set<ThermodynamicQuantity>()
const {
718 const std::vector<std::string> v =
operator std::vector<std::string>();
719 std::set<ThermodynamicQuantity> s;
720 for (
const auto &x : v) {
721 if (x ==
"rho_eckart") {
723 }
else if (x ==
"tmn") {
725 }
else if (x ==
"tmn_landau") {
727 }
else if (x ==
"landau_velocity") {
729 }
else if (x ==
"j_QBS") {
733 "The value for key \"" + std::string(
key_) +
734 "\" should be \"rho_eckart\", \"tmn\""
735 ", \"tmn_landau\", \"landau_velocity\" or \"j_QBS\".");
749 const std::string s =
operator std::string();
750 if (s ==
"center of velocity") {
753 if (s ==
"center of mass") {
756 if (s ==
"fixed target") {
760 "The value for key \"" + std::string(
key_) +
761 "\" should be \"center of velocity\" or \"center of mass\" "
762 "or \"fixed target\".");
773 const std::string s =
operator std::string();
784 "The value for key \"" + std::string(
key_) +
785 "\" should be \"off\" or \"on\" or \"frozen\".");
796 const std::string s =
operator std::string();
803 if (s ==
"baryonic isospin") {
809 if (s ==
"total isospin") {
817 "\" should be \"hadron\" or \"baryon\" "
818 "or \"baryonic isospin\" or \"pion\" "
830 const std::string s =
operator std::string();
831 if (s ==
"NoExpansion") {
834 if (s ==
"MasslessFRW") {
837 if (s ==
"MassiveFRW") {
840 if (s ==
"Exponential") {
844 "The value for key \"" + std::string(
key_) +
845 "\" should be \"NoExpansion\", \"MasslessFRW\"," +
846 "\"MassiveFRW\" or \"Exponential\".");
853 const std::string s =
operator std::string();
854 if (s ==
"Covariant Gaussian") {
857 if (s ==
"Finite difference") {
864 "The value for key \"" + std::string(
key_) +
865 "\" should be \"Covariant Gaussian\", \"Finite difference\"," +
873 const std::string s =
operator std::string();
882 "\" should be \"On\" or \"Off\".");
889 const std::string s =
operator std::string();
890 if (s ==
"Chain Rule") {
897 "The value for key \"" + std::string(
key_) +
898 "\" should be \"Chain Rule\" or \"Direct\".");
905 const std::string s =
operator std::string();
906 if (s ==
"Covariant Gaussian") {
909 if (s ==
"Discrete") {
912 if (s ==
"Triangular") {
916 "The value for key \"" + std::string(
key_) +
917 "\" should be \"Covariant Gaussian\", \"Discrete\"," +
918 " or \"Triangular\".");
929 const std::string s =
operator std::string();
938 "\" should be \"None\" or \"Fixed\".");
949 const std::string s =
operator std::string();
950 if (s ==
"thermal momenta") {
953 if (s ==
"thermal momenta quantum") {
956 if (s ==
"peaked momenta") {
960 "The value for key \"" + std::string(
key_) +
961 "\" should be \"thermal momenta\", \"thermal momenta quantum\", " +
962 "or \"peaked momenta\".");
973 const std::string s =
operator std::string();
974 if (s ==
"thermal momenta") {
977 if (s ==
"thermal momenta quantum") {
989 if (s ==
"IC_Massive") {
993 "The value for key \"" + std::string(
key_) +
994 "\" should be \"thermal momenta\", \"thermal momenta quantum\", " +
995 "\"IC_ES\", \"IC_1M\", \"IC_2M\" or" +
"\"IC_Massive\".");
1006 const std::string s =
operator std::string();
1007 if (s ==
"no annihilation") {
1010 if (s ==
"resonances") {
1013 if (s ==
"two to five") {
1016 if (s ==
"strings") {
1020 "The value for key \"" + std::string(
key_) +
"\" should be " +
1021 "\"no annihilation\", \"resonances\", \"two to five\" or " +
1033 const std::string s =
operator std::string();
1034 if (s ==
"quadratic") {
1037 if (s ==
"custom") {
1040 if (s ==
"uniform") {
1044 "The value for key \"" + std::string(
key_) +
1045 "\" should be \"quadratic\", \"uniform\" or \"custom\".");
1056 const std::string s =
operator std::string();
1057 if (s ==
"mode sampling") {
1060 if (s ==
"biased BF") {
1063 if (s ==
"unbiased BF") {
1067 "The value for key \"" + std::string(
key_) +
1068 "\" should be \"mode sampling\", \"biased BF\" or \"unbiased BF\".");
1079 const std::string s =
operator std::string();
1080 if (s ==
"Geometric") {
1083 if (s ==
"Stochastic") {
1086 if (s ==
"Covariant") {
1090 "The value for key \"" + std::string(
key_) +
"\" should be " +
1091 "\"Geometric\", \"Stochastic\" " +
"or \"Covariant\".");
1102 const std::string s =
operator std::string();
1109 if (s ==
"IfNotEmpty") {
1113 std::string(
key_) +
"\" should be " +
1114 "\"Yes\", \"No\" or \"IfNotEmpty\".");
1137 explicit Configuration(
const bf::path &path,
const bf::path &filename);
1151 throw std::runtime_error(
1152 "Unknown control flag in Configuration constructor"
1153 " with a YAML formatted string. Please, use"
1154 " Configuration::InitializeFromYAMLString.");
1227 Value take(std::initializer_list<const char *> keys);
1230 template <
typename T>
1231 T
take(std::initializer_list<const char *> keys, T default_value) {
1235 return default_value;
1252 Value
read(std::initializer_list<const char *> keys)
const;
1255 template <
typename T>
1256 T
read(std::initializer_list<const char *> keys, T default_value) {
1260 return default_value;
1284 template <
typename T>
1296 template <
typename T>
1307 std::initializer_list<const char *> keys)
const;
1312 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.
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
std::bitset< 3 > MultiParticleReactionsBitSet
Container for the n to m reactions in the code.
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.