Version: SMASH-1.5
configuration.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2018
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_CONFIGURATION_H_
11 #define SRC_INCLUDE_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 {
314  public:
319  struct IncorrectTypeInAssignment : public std::runtime_error {
320  using std::runtime_error::runtime_error;
321  };
326  struct ParseError : public std::runtime_error {
327  using std::runtime_error::runtime_error;
328  };
329 
334  struct FileDoesNotExist : public std::runtime_error {
335  using std::runtime_error::runtime_error;
336  };
337 
345  class Value {
346  friend class Configuration;
347 
349  const YAML::Node node_;
351  const char *const key_;
352 
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());
365  }
366  }
367 
368  public:
370  Value(const Value &) = delete;
372  Value &operator=(const Value &) = delete;
373 
400  template <typename T>
401  T convert_for(const T &) const {
402  return *this;
403  }
404 
413  template <typename T>
414  operator T() const {
415  try {
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.");
421  }
422  }
423 
429  template <typename T>
430  operator std::vector<T>() const {
431  try {
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 "
439  "file.");
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.");
445  }
446  }
447 
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();
459  // Alert if size does not match
460  if (n_read != N) {
461  throw IncorrectTypeInAssignment("Wrong number of values in array \"" +
462  std::string(key_) + "\". Expected " +
463  std::to_string(N) +
464  " values,"
465  " found " +
466  std::to_string(n_read) + ".");
467  }
468  std::array<T, N> arr;
469  std::copy_n(vec.begin(), N, arr.begin());
470  return arr;
471  }
472 
480  operator ReactionsBitSet() const {
481  const std::vector<std::string> v = operator std::vector<std::string>();
482  ReactionsBitSet s;
483  for (const auto &x : v) {
484  if (x == "All") {
485  s.set();
486  break;
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") {
499  } else {
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.");
505  }
506  }
507  return s;
508  }
509 
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") {
524  s.insert(ThermodynamicQuantity::Tmn);
525  } else if (x == "tmn_landau") {
527  } else if (x == "landau_velocity") {
529  } else {
531  "The value for key \"" + std::string(key_) +
532  "\" should be \"rho_eckart\", \"tmn\""
533  ", \"tmn_landau\" or \"landau_velocity\".");
534  }
535  }
536  return s;
537  }
538 
546  operator CalculationFrame() const {
547  const std::string s = operator std::string();
548  if (s == "center of velocity") {
550  }
551  if (s == "center of mass") {
553  }
554  if (s == "fixed target") {
556  }
558  "The value for key \"" + std::string(key_) +
559  "\" should be \"center of velocity\" or \"center of mass\" "
560  "or \"fixed target\".");
561  }
562 
570  operator FermiMotion() const {
571  const std::string s = operator std::string();
572  if (s == "off") {
573  return FermiMotion::Off;
574  }
575  if (s == "on") {
576  return FermiMotion::On;
577  }
578  if (s == "frozen") {
579  return FermiMotion::Frozen;
580  }
582  "The value for key \"" + std::string(key_) +
583  "\" should be \"off\" or \"on\" or \"frozen\".");
584  }
585 
593  operator DensityType() const {
594  const std::string s = operator std::string();
595  if (s == "hadron") {
596  return DensityType::Hadron;
597  }
598  if (s == "baryon") {
599  return DensityType::Baryon;
600  }
601  if (s == "baryonic isospin") {
603  }
604  if (s == "pion") {
605  return DensityType::Pion;
606  }
607  if (s == "none") {
608  return DensityType::None;
609  }
610  throw IncorrectTypeInAssignment("The value for key \"" +
611  std::string(key_) +
612  "\" should be \"hadron\" or \"baryon\" "
613  "or \"baryonic isospin\" or \"pion\" "
614  "or \"none\".");
615  }
616 
624  operator ExpansionMode() const {
625  const std::string s = operator std::string();
626  if (s == "NoExpansion") {
628  }
629  if (s == "MasslessFRW") {
631  }
632  if (s == "MassiveFRW") {
634  }
635  if (s == "Exponential") {
637  }
639  "The value for key \"" + std::string(key_) +
640  "\" should be \"NoExpansion\", \"MasslessFRW\"," +
641  "\"MassiveFRW\" or \"Exponential\".");
642  }
643 
651  operator TimeStepMode() const {
652  const std::string s = operator std::string();
653  if (s == "None") {
654  return TimeStepMode::None;
655  }
656  if (s == "Fixed") {
657  return TimeStepMode::Fixed;
658  }
659  throw IncorrectTypeInAssignment("The value for key \"" +
660  std::string(key_) +
661  "\" should be \"None\" or \"Fixed\".");
662  }
663 
671  operator BoxInitialCondition() const {
672  const std::string s = operator std::string();
673  if (s == "thermal momenta") {
675  }
676  if (s == "peaked momenta") {
678  }
680  "The value for key \"" + std::string(key_) +
681  "\" should be \"thermal momenta\" or \"peaked momenta\".");
682  }
683 
691  operator SphereInitialCondition() const {
692  const std::string s = operator std::string();
693  if (s == "thermal momenta") {
695  }
696  if (s == "IC_ES") {
698  }
699  if (s == "IC_1M") {
701  }
702  if (s == "IC_2M") {
704  }
705  if (s == "IC_Massive") {
707  }
709  "The value for key \"" + std::string(key_) +
710  "\" should be \"thermal momenta\", \"IC_ES\", " +
711  "\"IC_1M\", \"IC_2M\" or" + "\"IC_Massive\".");
712  }
713 
721  operator NNbarTreatment() const {
722  const std::string s = operator std::string();
723  if (s == "no annihilation") {
725  }
726  if (s == "resonances") {
728  }
729  if (s == "strings") {
731  }
733  "The value for key \"" + std::string(key_) + "\" should be " +
734  "\"no annihilation\", \"detailed balance\", or \"strings\".");
735  }
736 
744  operator Sampling() const {
745  const std::string s = operator std::string();
746  if (s == "quadratic") {
747  return Sampling::Quadratic;
748  }
749  if (s == "custom") {
750  return Sampling::Custom;
751  }
752  if (s == "uniform") {
753  return Sampling::Uniform;
754  }
756  "The value for key \"" + std::string(key_) +
757  "\" should be \"quadratic\", \"uniform\" or \"custom\".");
758  }
759 
767  operator ThermalizationAlgorithm() const {
768  const std::string s = operator std::string();
769  if (s == "mode sampling") {
771  }
772  if (s == "biased BF") {
774  }
775  if (s == "unbiased BF") {
777  }
779  "The value for key \"" + std::string(key_) +
780  "\" should be \"mode sampling\", \"biased BF\" or \"unbiased BF\".");
781  }
782  };
783 
789  explicit Configuration(const bf::path &path);
790 
798  explicit Configuration(const bf::path &path, const bf::path &filename);
799 
800 #ifdef BUILD_TESTS
801 
809  explicit Configuration(const char *yaml) : root_node_(YAML::Load(yaml)) {}
810 #endif
811 
813  Configuration(const Configuration &) = default;
815  Configuration &operator=(const Configuration &) = default;
816 
818  Configuration(Configuration &&) = default;
820  Configuration &operator=(Configuration &&) = default;
821 
832  void merge_yaml(const std::string &yaml);
833 
835  std::vector<std::string> list_upmost_nodes();
836 
858  Value take(std::initializer_list<const char *> keys);
859 
861  template <typename T>
862  T take(std::initializer_list<const char *> keys, T default_value) {
863  if (has_value(keys)) {
864  return take(keys);
865  }
866  return default_value;
867  }
868 
883  Value read(std::initializer_list<const char *> keys) const;
884 
886  template <typename T>
887  T read(std::initializer_list<const char *> keys, T default_value) {
888  if (has_value(keys)) {
889  return read(keys);
890  }
891  return default_value;
892  }
893 
899  void remove_all_but(const std::string &key);
900 
915  template <typename T>
917  return root_node_[std::forward<T>(key)];
918  }
919 
927  template <typename T>
928  Configuration &operator=(T &&value) {
929  root_node_ = std::forward<T>(value);
930  return *this;
931  }
932 
938  std::initializer_list<const char *> keys) const;
943  bool has_value(std::initializer_list<const char *> keys) const;
944 
948  std::string unused_values_report() const;
949 
955  std::string to_string() const;
956 
957  private:
964  Configuration(const YAML::Node &node) // NOLINT(runtime/explicit) : see above
965  : root_node_(node) {}
966 
968  YAML::Node root_node_;
969 };
970 
971 } // namespace smash
972 
973 #endif // SRC_INCLUDE_CONFIGURATION_H_
Configuration(const YAML::Node &node)
Creates a subobject that has its root node at the given node.
Value read(std::initializer_list< const char *> keys) const
Additional interface for SMASH to read configuration values without removing them.
std::string unused_values_report() const
Returns a string listing the key/value pairs that have not been taken yet.
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. ...
const char *const key_
The key to be interpreted.
Configuration(const bf::path &path)
Reads config.yaml from the specified path.
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)
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.
Definition: configuration.h:34
std::vector< std::string > list_upmost_nodes()
Lists all YAML::Nodes from the configuration setup.
bool has_value(std::initializer_list< const char *> keys) const
Returns whether there is a non-empty value behind the requested keys.
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
Sample from uniform distribution.
SphereInitialCondition
Initial condition for a particle in a sphere.
elastic scattering: particles remain the same, only momenta change
void merge_yaml(const std::string &yaml)
Merge the configuration in yaml into the existing tree.
Configuration & operator=(const Configuration &)=default
If you want to copy this you&#39;re doing it wrong.
CalculationFrame
The calculation frame.
TimeStepMode
The time step mode.
Sample from custom, user-defined distribution.
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&#39;t use fermi motion.
void remove_all_but(const std::string &key)
Removes all entries in the map except for key.
std::bitset< 6 > ReactionsBitSet
Container for the 2 to 2 reactions in the code.
T read(std::initializer_list< const char *> keys, T default_value)
Value & operator=(const Value &)=delete
If you want to copy this you&#39;re doing it wrong.
Use fermi motion without potentials.
Use string fragmentation.
Use fixed time step.
Use intermediate Resonances.
ExpansionMode
Defines properties of expansion for the metric (e.g.
bool has_value_including_empty(std::initializer_list< const char *> keys) const
Returns if there is a (maybe empty) value behind the requested keys.
T take(std::initializer_list< const char *> keys, T default_value)
Sampling
Possible methods of impact parameter sampling.
Thrown when the types in the config file and C++ don&#39;t match.
Value take(std::initializer_list< const char *> keys)
The default interface for SMASH to read configuration values.
T convert_for(const T &) const
Convert the value to the type of the supplied argument.
YAML::Node root_node_
the general_config.yaml contents - fully parsed
constexpr int n
Neutron.
DensityType
Allows to choose which kind of density to calculate.
Definition: density.h:34
static Node encode(const T &x)
Serialization: Converts x (of any type) to a YAML::Node.
Definition: configuration.h:42
std::string to_string() const
Returns a YAML string of the current tree.
Use fermi motion in combination with potentials.
Definition: action.h:24