Mallard 0.8.0
Unstructured, GPU accelerated, high-order finite volume CFD solver
Mallard website
Loading...
Searching...
No Matches
input.h
Go to the documentation of this file.
1
12#ifndef INPUT_H
13#define INPUT_H
14
15#include <algorithm>
16#include <stdexcept>
17#include <string>
18#include <unordered_map>
19#include <vector>
20
21#include <toml.hpp>
22
23#include "common_typedef.h"
24
29struct InputError : std::runtime_error {
30 using std::runtime_error::runtime_error;
31};
32
36template <typename T>
37InputError unknown_option(const std::unordered_map<std::string, T> & options, const std::string & key,
38 const std::string & value) {
39 std::vector<std::string> names;
40 for (const auto & entry : options) names.push_back(entry.first);
41 std::sort(names.begin(), names.end());
42 std::string list;
43 for (const auto & name : names) list += (list.empty() ? "" : ", ") + name;
44 return InputError(key + " = \"" + value + "\" is not one of: " + list + ".");
45}
46
53inline rtype as_real(const toml::value & v, const std::string & key) {
54 if (v.is_floating()) return static_cast<rtype>(v.as_floating());
55 if (v.is_integer()) return static_cast<rtype>(v.as_integer());
56 throw InputError(toml::format_error(key + " must be a number", v, "here"));
57}
58
60inline double as_double(const toml::value & v, const std::string & key) {
61 if (v.is_floating()) return v.as_floating();
62 if (v.is_integer()) return static_cast<double>(v.as_integer());
63 throw InputError(toml::format_error(key + " must be a number", v, "here"));
64}
65
66inline double find_double(const toml::value & v, const std::string & key) {
67 if (!v.contains(key)) throw InputError("missing " + key + ".");
68 return as_double(v.at(key), key);
69}
70
71inline double find_double_or(const toml::value & v, const std::string & key, const double fallback) {
72 return v.contains(key) ? as_double(v.at(key), key) : fallback;
73}
74
75inline rtype find_real(const toml::value & v, const std::string & key) {
76 if (!v.contains(key)) throw InputError("missing " + key + ".");
77 return as_real(v.at(key), key);
78}
79
80inline rtype find_real(const toml::value & v, const std::string & table, const std::string & key) {
81 if (!v.contains(table)) throw InputError("missing [" + table + "].");
82 if (!v.at(table).contains(key)) throw InputError("missing " + table + "." + key + ".");
83 return as_real(v.at(table).at(key), table + "." + key);
84}
85
86inline rtype find_real_or(const toml::value & v, const std::string & key, const rtype fallback) {
87 return v.contains(key) ? as_real(v.at(key), key) : fallback;
88}
89
90inline rtype find_real_or(const toml::value & v, const std::string & table, const std::string & key,
91 const rtype fallback) {
92 if (!v.contains(table) || !v.at(table).contains(key)) return fallback;
93 return as_real(v.at(table).at(key), table + "." + key);
94}
95
96inline std::vector<rtype> find_real_vector(const toml::value & v, const std::string & key) {
97 if (!v.contains(key)) throw InputError("missing " + key + ".");
98 std::vector<rtype> out;
99 for (const auto & item : v.at(key).as_array()) out.push_back(as_real(item, key));
100 return out;
101}
102
103inline std::vector<rtype> find_real_vector(const toml::value & v, const std::string & table, const std::string & key) {
104 if (!v.contains(table)) throw InputError("missing [" + table + "].");
105 if (!v.at(table).contains(key)) throw InputError("missing " + table + "." + key + ".");
106 std::vector<rtype> out;
107 for (const auto & item : v.at(table).at(key).as_array()) out.push_back(as_real(item, table + "." + key));
108 return out;
109}
110
111#endif // INPUT_H
KOKKOS_INLINE_FUNCTION rtype dot(const rtype *a, const rtype *b)
Compute the dot product of two arrays.
Definition common_math.h:32
Typedefs for common types.
float rtype
Definition common_typedef.h:39
rtype find_real(const toml::value &v, const std::string &key)
Definition input.h:75
std::vector< rtype > find_real_vector(const toml::value &v, const std::string &key)
Definition input.h:96
double find_double_or(const toml::value &v, const std::string &key, const double fallback)
Definition input.h:71
rtype find_real_or(const toml::value &v, const std::string &key, const rtype fallback)
Definition input.h:86
rtype as_real(const toml::value &v, const std::string &key)
Definition input.h:53
double find_double(const toml::value &v, const std::string &key)
Definition input.h:66
InputError unknown_option(const std::unordered_map< std::string, T > &options, const std::string &key, const std::string &value)
Error for a value not among the accepted options, listing them.
Definition input.h:37
double as_double(const toml::value &v, const std::string &key)
Double-precision lookups, for inputs of the chemistry (double in every build).
Definition input.h:60
An invalid or missing input value. The message names the key and the offending value; the caller adds...
Definition input.h:29
TableBatchT< Space > out
Definition teno_setup.cpp:681