AMDiS 2.11-git
The Adaptive Multi-Dimensional Simulation Toolbox
 
Loading...
Searching...
No Matches
Initfile.hpp
1#pragma once
2
3#include <cassert>
4#include <cmath>
5#include <optional>
6#include <string>
7#include <type_traits>
8#include <vector>
9
10#include <dune/common/parametertree.hh>
11#include <dune/functions/functionspacebases/concepts.hh>
12#include <muParser.h>
13
14namespace AMDiS
15{
17 {
18 public:
20 static void init(std::string const& in);
21
23
29 template <class T,
30 std::enable_if_t<!std::is_arithmetic_v<T>, int> = 0>
31 static std::optional<T> get(std::string const& key)
32 {
33 if (pt().hasKey(key)) {
35 return getVector<T>(key);
36 else
37 return pt().get<T>(key);
38 }
39 else
40 return {};
41 }
42
43 template <class T,
44 std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
45 static std::optional<T> get(std::string const& key) {
46 if (pt().hasKey(key)) {
47 std::string aux;
48 std::string valStr = pt().get(key, aux);
49 return parseExpression<T>(valStr,key);
50 } else {
51 return {};
52 }
53 }
54
56
63 template <class T>
64 static void get(std::string const& key, T& value) {
65 value = get<T>(key).value_or(value);
66 }
67
68 template <class T>
69 static void set(std::string const& key, T const& value)
70 {
71 pt()[key] = value;
72 }
73
75 static int getMsgInfo()
76 {
77 return singlett().msgInfo_;
78 }
79
81 static void printParameters();
82
83 static void clearData() {}
84
85 protected:
86 Initfile() = default;
87
89 static Dune::ParameterTree& pt()
90 {
91 return singlett().pt_;
92 }
93
95 void read(std::string const& fn, bool force = false);
96 void write(std::string const& fn);
97
100
102 template <class T>
103 static std::optional<T> getVector(const std::string& key) {
104 if constexpr (std::is_arithmetic_v<typename T::value_type>) {
105 T result{};
106 std::vector<std::string> aux;
107 auto valStr = pt().get(key, aux);
108 if constexpr(Dune::models<Dune::Functions::Concept::HasResize, T>())
109 result.resize(valStr.size());
110
111 assert(result.size() == valStr.size());
112 for (std::size_t i = 0; i < valStr.size(); ++i) {
113 result[i] = parseExpression<typename T::value_type,T>(valStr[i],key,i);
114 }
115 return result;
116 } else {
117 return pt().get<T>(key);
118 }
119 }
120
122 template<typename T>
123 static bool parse(const std::string& input, T& result) {
124 std::string inputSwap = input;
125 std::string allowedChars = "+-*/()0123456789";
126
127 // do not parse strings
128 std::string notAllowed = ",abcdfghijklmnopqrstuvwxyzABCDFGHIJKLMNOPQRSTUVWXYZ";
129 std::size_t found = inputSwap.find_first_of(allowedChars);
130 std::size_t found2 = inputSwap.find_first_of(notAllowed);
131 mu::Parser parser;
132
133 if (found != std::string::npos && found2 == std::string::npos) {
134 parser.SetExpr(input);
135 result = parser.Eval();
136 return true;
137 }
138 return false;
139 }
140
141 template<typename T>
142 static T parseExpression(const std::string& input,
143 const std::string& key) {
144 T result;
145 if (parse(input, result)) return result;
146
147 return pt().get<T>(key);
148 }
149
150 template<typename T, typename Container>
151 static T parseExpression(const std::string& input,
152 const std::string& key,
153 size_t i) {
154 T result;
155 if (parse(input, result)) return result;
156
157 auto container = pt().get<Container>(key);
158 return container[i];
159 }
160
162 template <typename Container, typename = void>
163 struct is_arithmetic_container : std::false_type {};
164
165 template <typename Container>
166 struct is_arithmetic_container<Container,
167 std::enable_if_t<std::is_arithmetic<typename Container::value_type>::value &&
168 !std::is_same<Container, std::string>::value>>
169 : std::true_type {};
170
171
174 {
175 static Initfile initfile;
176 return initfile;
177 }
178
179 int msgInfo_ = 0;
180 int paramInfo_ = 1;
181 bool breakOnMissingTag_ = false;
182
184 Dune::ParameterTree pt_;
185 };
186
187 using Parameters = Initfile;
188
189} // end namespace AMDiS
Definition Initfile.hpp:17
void read(std::string const &fn, bool force=false)
Fill an parametr-tree from a file with filename fn.
Definition Initfile.cpp:32
void getInternalParameters()
read standard values for output and information of parameter-values
Definition Initfile.cpp:38
static void get(std::string const &key, T &value)
Get parameter-values from parameter-tree with default value.
Definition Initfile.hpp:64
static int getMsgInfo()
Returns specified info level.
Definition Initfile.hpp:75
static std::optional< T > getVector(const std::string &key)
get Parameter values for vector types
Definition Initfile.hpp:103
static void printParameters()
print all data in singleton to std::cout
Definition Initfile.cpp:49
static std::optional< T > get(std::string const &key)
Get parameter-values from parameter-tree.
Definition Initfile.hpp:31
Dune::ParameterTree pt_
ParameterTree to read/store parameter values.
Definition Initfile.hpp:184
static Initfile & singlett()
Return the singleton that contains the data.
Definition Initfile.hpp:173
static Dune::ParameterTree & pt()
Return the parameter-tree.
Definition Initfile.hpp:89
static bool parse(const std::string &input, T &result)
replace expression with the respective result
Definition Initfile.hpp:123
static void init(std::string const &in)
initialize singleton object and global parameters
Definition Initfile.cpp:25
Template for container types with [] operator and existing value_type.
Definition Initfile.hpp:163