10 #ifndef SRC_INCLUDE_CONFIGURATION_H_ 11 #define SRC_INCLUDE_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());
320 using std::runtime_error::runtime_error;
327 using std::runtime_error::runtime_error;
335 using std::runtime_error::runtime_error;
359 Value(
const YAML::Node &
n,
const char *key) : node_(n), key_(key) {
360 if (!(n.IsScalar() || n.IsSequence() || n.IsMap())) {
361 std::stringstream err;
362 err <<
"Configuration value for \"" << key
363 <<
"\" is missing or invalid";
364 throw std::runtime_error(err.str());
400 template <
typename T>
413 template <
typename T>
416 return node_.as<T>();
417 }
catch (YAML::TypedBadConversion<T> &e) {
419 "The value for key \"" + std::string(key_) +
420 "\" cannot be converted to the requested type.");
429 template <
typename T>
430 operator std::vector<T>()
const {
432 return node_.as<std::vector<T>>();
433 }
catch (YAML::TypedBadConversion<T> &e) {
435 "One of the values in the sequence for key \"" + std::string(key_) +
436 "\" failed to convert to the requested type. E.g. [1 2] is a " 437 "sequence of one string \"1 2\" and [1, 2] is a sequence of two " 438 "integers. Often there is just a comma missing in the config " 440 }
catch (YAML::TypedBadConversion<std::vector<T>> &e) {
442 "The value for key \"" + std::string(key_) +
443 "\" cannot be converted to the requested type. A sequence was " 444 "expected but apparently not found.");
455 template <
typename T,
size_t N>
456 operator std::array<T, N>()
const {
457 const std::vector<T> vec =
operator std::vector<T>();
458 const size_t n_read = vec.size();
462 std::string(key_) +
"\". Expected " +
466 std::to_string(n_read) +
".");
468 std::array<T, N> arr;
469 std::copy_n(vec.begin(), N, arr.begin());
481 const std::vector<std::string> v =
operator std::vector<std::string>();
483 for (
const auto &x : v) {
487 }
else if (x ==
"Elastic") {
489 }
else if (x ==
"NN_to_NR") {
491 }
else if (x ==
"NN_to_DR") {
493 }
else if (x ==
"KN_to_KN") {
495 }
else if (x ==
"KN_to_KDelta") {
497 }
else if (x ==
"Strangeness_exchange") {
501 "The value for key \"" + std::string(key_) +
502 "\" should be \"All\", \"Elastic\", \"NN_to_NR\", \"NN_to_DR\"," 503 "\"KN_to_KN\", \"KN_to_KDelta\" or \"strangeness_exchange\"," 504 " or any combination of these.");
517 operator std::set<ThermodynamicQuantity>()
const {
518 const std::vector<std::string> v =
operator std::vector<std::string>();
519 std::set<ThermodynamicQuantity> s;
520 for (
const auto &x : v) {
521 if (x ==
"rho_eckart") {
523 }
else if (x ==
"tmn") {
525 }
else if (x ==
"tmn_landau") {
527 }
else if (x ==
"landau_velocity") {
529 }
else if (x ==
"j_QBS") {
533 "The value for key \"" + std::string(key_) +
534 "\" should be \"rho_eckart\", \"tmn\"" 535 ", \"tmn_landau\", \"landau_velocity\" or \"j_QBS\".");
549 const std::string s =
operator std::string();
550 if (s ==
"center of velocity") {
553 if (s ==
"center of mass") {
556 if (s ==
"fixed target") {
560 "The value for key \"" + std::string(key_) +
561 "\" should be \"center of velocity\" or \"center of mass\" " 562 "or \"fixed target\".");
573 const std::string s =
operator std::string();
584 "The value for key \"" + std::string(key_) +
585 "\" should be \"off\" or \"on\" or \"frozen\".");
596 const std::string s =
operator std::string();
598 return DensityType::Hadron;
601 return DensityType::Baryon;
603 if (s ==
"baryonic isospin") {
604 return DensityType::BaryonicIsospin;
607 return DensityType::Pion;
609 if (s ==
"total isospin") {
610 return DensityType::Isospin3_tot;
613 return DensityType::None;
617 "\" should be \"hadron\" or \"baryon\" " 618 "or \"baryonic isospin\" or \"pion\" " 630 const std::string s =
operator std::string();
631 if (s ==
"NoExpansion") {
634 if (s ==
"MasslessFRW") {
637 if (s ==
"MassiveFRW") {
640 if (s ==
"Exponential") {
644 "The value for key \"" + std::string(key_) +
645 "\" should be \"NoExpansion\", \"MasslessFRW\"," +
646 "\"MassiveFRW\" or \"Exponential\".");
657 const std::string s =
operator std::string();
666 "\" should be \"None\" or \"Fixed\".");
677 const std::string s =
operator std::string();
678 if (s ==
"thermal momenta") {
681 if (s ==
"peaked momenta") {
685 "The value for key \"" + std::string(key_) +
686 "\" should be \"thermal momenta\" or \"peaked momenta\".");
697 const std::string s =
operator std::string();
698 if (s ==
"thermal momenta") {
710 if (s ==
"IC_Massive") {
714 "The value for key \"" + std::string(key_) +
715 "\" should be \"thermal momenta\", \"IC_ES\", " +
716 "\"IC_1M\", \"IC_2M\" or" +
"\"IC_Massive\".");
727 const std::string s =
operator std::string();
728 if (s ==
"no annihilation") {
731 if (s ==
"resonances") {
734 if (s ==
"strings") {
738 "The value for key \"" + std::string(key_) +
"\" should be " +
739 "\"no annihilation\", \"detailed balance\", or \"strings\".");
750 const std::string s =
operator std::string();
751 if (s ==
"quadratic") {
757 if (s ==
"uniform") {
761 "The value for key \"" + std::string(key_) +
762 "\" should be \"quadratic\", \"uniform\" or \"custom\".");
773 const std::string s =
operator std::string();
774 if (s ==
"mode sampling") {
777 if (s ==
"biased BF") {
780 if (s ==
"unbiased BF") {
784 "The value for key \"" + std::string(key_) +
785 "\" should be \"mode sampling\", \"biased BF\" or \"unbiased BF\".");
803 explicit Configuration(
const bf::path &path,
const bf::path &filename);
837 void merge_yaml(
const std::string &yaml);
840 std::vector<std::string> list_upmost_nodes();
863 Value take(std::initializer_list<const char *> keys);
866 template <
typename T>
867 T
take(std::initializer_list<const char *> keys, T default_value) {
868 if (has_value(keys)) {
871 return default_value;
888 Value read(std::initializer_list<const char *> keys)
const;
891 template <
typename T>
892 T
read(std::initializer_list<const char *> keys, T default_value) {
893 if (has_value(keys)) {
896 return default_value;
904 void remove_all_but(
const std::string &key);
920 template <
typename T>
922 return root_node_[std::forward<T>(key)];
932 template <
typename T>
934 root_node_ = std::forward<T>(value);
942 bool has_value_including_empty(
943 std::initializer_list<const char *> keys)
const;
948 bool has_value(std::initializer_list<const char *> keys)
const;
953 std::string unused_values_report()
const;
960 std::string to_string()
const;
970 : root_node_(node) {}
978 #endif // SRC_INCLUDE_CONFIGURATION_H_ Configuration(const YAML::Node &node)
Creates a subobject that has its root node at the given node.
Thrown if the file does not exist.
ThermalizationAlgorithm
Defines the algorithm used for the forced thermalization.
FermiMotion
Option to use Fermi Motion.
Return type of Configuration::take that automatically determines the target type. ...
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.
BoxInitialCondition
Initial condition for a particle in a box.
Value(const YAML::Node &n, const char *key)
Constructs the Value wrapper from a YAML::Node.
const YAML::Node node_
a YAML leaf node
Configuration(const char *yaml)
T take(std::initializer_list< const char * > keys, T default_value)
Interface to the SMASH configuration files.
Thrown for YAML parse errors.
NNbarTreatment
Treatment of N Nbar Annihilation.
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...
Sample from uniform distribution.
SphereInitialCondition
Initial condition for a particle in a sphere.
CalculationFrame
The calculation frame.
TimeStepMode
The time step mode.
Sample from custom, user-defined distribution.
Don't use time steps; propagate from action to action.
Configuration operator[](T &&key)
Access to the YAML::Node behind the requested keys.
Sample from areal / quadratic distribution.
Configuration & operator=(T &&value)
Assignment overwrites the value of the current YAML node.
Don't use fermi motion.
std::bitset< 6 > ReactionsBitSet
Container for the 2 to 2 reactions in the code.
Use fermi motion without potentials.
Use string fragmentation.
Use intermediate Resonances.
ExpansionMode
Defines properties of expansion for the metric (e.g.
Sampling
Possible methods of impact parameter sampling.
Thrown when the types in the config file and C++ don't match.
YAML::Node root_node_
the general_config.yaml contents - fully parsed
DensityType
Allows to choose which kind of density to calculate.
static Node encode(const T &x)
Serialization: Converts x (of any type) to a YAML::Node.
T read(std::initializer_list< const char * > keys, T default_value)
Use fermi motion in combination with potentials.