ABLATE Source Documentation  0.12.33
parameters.hpp
1 #ifndef ABLATELIBRARY_PARAMETERS_HPP
2 #define ABLATELIBRARY_PARAMETERS_HPP
3 #include <petsc.h>
4 #include <array>
5 #include <map>
6 #include <optional>
7 #include <sstream>
8 #include <string>
9 #include <unordered_set>
10 #include <vector>
11 #include "parameterException.hpp"
12 
13 namespace ablate::parameters {
14 
15 class Parameters {
16  private:
17  template <typename T>
18  static void toValue(const std::string& inputString, T& outputValue) {
19  std::istringstream ss(inputString);
20  ss >> outputValue;
21  }
22 
23  template <typename T>
24  static void toValue(const std::string& inputString, std::vector<T>& outputValue) {
25  std::istringstream ss(inputString);
26  T tempValue;
27  while (ss >> tempValue) {
28  outputValue.push_back(tempValue);
29  }
30  }
31 
32  template <typename T, std::size_t N>
33  static void toValue(const std::string& inputString, std::array<T, N>& outputValue) {
34  std::istringstream ss(inputString);
35  T tempValue;
36  std::size_t index = 0;
37  // set to default value of T
38  T defaultValue = {};
39  outputValue.fill(defaultValue);
40 
41  while (ss >> tempValue && index < N) {
42  outputValue[index] = tempValue;
43  index++;
44  }
45  }
46 
47  // value specific cases
48  static void toValue(const std::string& inputString, bool& outputValue);
49 
50  public:
51  virtual ~Parameters() = default;
52 
53  virtual std::optional<std::string> GetString(std::string paramName) const = 0;
54  virtual std::unordered_set<std::string> GetKeys() const = 0;
55 
56  template <typename T>
57  std::optional<T> Get(std::string paramName) const {
58  auto value = GetString(paramName);
59  if (value.has_value()) {
60  T num;
61  toValue(value.value(), num);
62  return num;
63  } else {
64  return {};
65  }
66  }
67 
68  template <typename T>
69  T Get(std::string paramName, T defaultValue) const {
70  auto value = GetString(paramName);
71  if (value.has_value()) {
72  T num;
73  toValue(value.value(), num);
74  return num;
75  } else {
76  return defaultValue;
77  }
78  }
79 
80  template <typename T>
81  T GetExpect(std::string paramName) const {
82  auto value = GetString(paramName);
83  if (value.has_value()) {
84  T num;
85  toValue(value.value(), num);
86  return num;
87  } else {
88  throw ParameterException(paramName);
89  }
90  }
91 
98  template <typename T>
99  std::map<std::string, T> ToMap() const {
100  std::map<std::string, T> map;
101  for (const auto& key : GetKeys()) {
102  map[key] = GetExpect<T>(key);
103  }
104  return map;
105  }
106 
107  void Fill(PetscOptions options) const;
108 
109  template <typename T>
110  void Fill(int numberValues, const char* const* valueNames, T* constantArray) const {
111  // March over each parameter
112  for (int n = 0; n < numberValues; n++) {
113  // make a temp string
114  auto stringName = std::string(valueNames[n]);
115 
116  // Set the value
117  constantArray[n] = GetExpect<T>(stringName);
118  }
119  }
120 
121  template <typename T>
122  void Fill(int numberValues, const char* const* valueNames, T* constantArray, std::map<std::string, T> defaultValues) const {
123  // March over each parameter
124  for (int n = 0; n < numberValues; n++) {
125  // make a temp string
126  auto stringName = std::string(valueNames[n]);
127 
128  // Set the value
129  if (defaultValues.count(stringName)) {
130  constantArray[n] = Get<T>(stringName, defaultValues[stringName]);
131  } else {
132  // no default value
133  constantArray[n] = GetExpect<T>(stringName);
134  }
135  }
136  }
137 };
138 } // namespace ablate::parameters
139 
140 #endif // ABLATELIBRARY_PARAMETERS_HPP
Definition: parameters.hpp:15
std::map< std::string, T > ToMap() const
Definition: parameters.hpp:99
Definition: parameterException.hpp:6