MolSim
Loading...
Searching...
No Matches
CellUtils.h
Go to the documentation of this file.
1
9#pragma once
10#include "CLIUtils.h"
11#include <bitset>
12#include <sstream>
13#include <string>
14#include <unordered_map>
15#include <vector>
16
20enum class CellType { INNER, BORDER, HALO };
24enum class BorderLocation { NORTH = 0, SOUTH = 1, WEST = 2, EAST = 3, ABOVE = 4, BELOW = 5 };
25
27namespace CellUtils {
29static inline const std::unordered_map<std::string, BoundaryCondition> conditionTable = {
30 {"outflow", BoundaryCondition::OUTFLOW},
31 {"reflective", BoundaryCondition::REFLECTIVE},
32 {"periodic", BoundaryCondition::PERIODIC}};
33
35static inline const std::unordered_map<std::string, CellType> typeTable = {
36 {"inner", CellType::INNER}, {"border", CellType::BORDER}, {"halo", CellType::HALO}};
37
39static inline const std::unordered_map<std::string, HaloLocation> haloTable = {
40 {"north", HaloLocation::NORTH}, {"south", HaloLocation::SOUTH}, {"east", HaloLocation::EAST},
41 {"west", HaloLocation::WEST}, {"above", HaloLocation::ABOVE}, {"below", HaloLocation::BELOW},
42};
43
45static inline const std::unordered_map<BoundaryCondition, std::string> conditionStringTable = []() {
46 std::unordered_map<BoundaryCondition, std::string> reverseMap;
47 for (const auto &pair : conditionTable)
48 reverseMap[pair.second] = pair.first;
49 return reverseMap;
50}();
51
53static inline const std::unordered_map<CellType, std::string> typeStringTable = []() {
54 std::unordered_map<CellType, std::string> reverseMap;
55 for (const auto &pair : typeTable)
56 reverseMap[pair.second] = pair.first;
57 return reverseMap;
58}();
59
61static inline const std::unordered_map<HaloLocation, std::string> haloStringTable = []() {
62 std::unordered_map<HaloLocation, std::string> reverseMap;
63 for (const auto &pair : haloTable)
64 reverseMap[pair.second] = pair.first;
65 return reverseMap;
66}();
67
74static inline std::string fromBoundaryCondition(BoundaryCondition type) { return conditionStringTable.at(type); }
75
82static inline BoundaryCondition toBoundaryCondition(const std::string &type) {
83 auto it = conditionTable.find(type);
84 if (it != conditionTable.end())
85 return it->second;
86 else
87 CLIUtils::error("Invalid output type", type);
88 return BoundaryCondition::OUTFLOW; // shouldn't reach this; included to silence warning
89}
90
97static inline std::string fromType(CellType type) { return typeStringTable.at(type); }
98
105static inline CellType toType(const std::string &type) {
106 auto it = typeTable.find(type);
107 if (it != typeTable.end())
108 return it->second;
109 else
110 CLIUtils::error("Invalid output type", type);
111 return CellType::INNER; // shouldn't reach this; included to silence warning
112}
113
120static inline std::string fromHalo(HaloLocation type) { return haloStringTable.at(type); }
121
130static inline std::string fromHaloVec(const std::vector<HaloLocation> &vec) {
131 std::stringstream ss;
132 for (auto v : vec) {
133 switch (v) {
135 ss << "N";
136 break;
138 ss << "S";
139 break;
141 ss << "E";
142 break;
144 ss << "W";
145 break;
147 ss << "A";
148 break;
150 ss << "B";
151 break;
152 default:
153 SPDLOG_WARN("This shouldn't happen! (haloVec)");
154 break;
155 }
156 }
157 return ss.str();
158}
159
168static inline std::string fromBoundaryConditionArray(const std::array<BoundaryCondition, 6> &arr) {
169 std::stringstream ss;
170 for (auto c : arr) {
171 switch (c) {
173 ss << "o";
174 break;
176 ss << "r";
177 break;
179 ss << "p";
180 break;
181 default:
182 SPDLOG_WARN("This shouldn't happen! (fromBoundaryConditionArray)");
183 break;
184 }
185 }
186 return ss.str();
187}
188
195static inline HaloLocation toHalo(const std::string &type) {
196 auto it = haloTable.find(type);
197 if (it != haloTable.end())
198 return it->second;
199 else
200 CLIUtils::error("Invalid output type", type);
201 return HaloLocation::NORTH; // shouldn't reach this; included to silence warning
202}
203
213static inline std::array<BoundaryCondition, 6> stringToBoundaryConditions(const std::string &str) {
214 if (str.size() != 6)
215 CLIUtils::error("Invalid boundary condition string length!");
216 std::array<BoundaryCondition, 6> arr;
217 size_t index = 0;
218 for (auto &c : str) {
219 switch (c) {
220 case 'o':
221 arr[index++] = BoundaryCondition::OUTFLOW;
222 break;
223 case 'r':
224 arr[index++] = BoundaryCondition::REFLECTIVE;
225 break;
226 case 'p':
227 arr[index++] = BoundaryCondition::PERIODIC;
228 break;
229 default:
230 CLIUtils::error("Invalid boundary condition character (must be one of: o, r)!");
231 }
232 }
233 return arr;
234}
235
236} // namespace CellUtils
Utility functions for console input / output.
CellType
Enum containing the possible types of cells.
Definition CellUtils.h:20
HaloLocation
Enum containing the possible corner and edge directions of halo cells.
Definition CellUtils.h:22
BoundaryCondition
Enum containing the possible types of boundary conditions.
Definition CellUtils.h:18
BorderLocation
Enum containing the possible corner and edge directions of border cells.
Definition CellUtils.h:24
Enumeration class corresponding to the type schema type.
Definition vtk-unstructured.h:2125
static void error(const char *msg, const std::string &opt="", bool usage=true, bool close=true)
Prints an error message to stderr with the prefix "ERROR: ", optionally prints the usage string and e...
Definition CLIUtils.h:149
Namespace containing utility functions for Cell and CellContainer objects.
Definition CellUtils.h:27
static const std::unordered_map< CellType, std::string > typeStringTable
Reverse map containing conversion information for converting a CellType enum to a string.
Definition CellUtils.h:53
static const std::unordered_map< HaloLocation, std::string > haloStringTable
Reverse map containing conversion information for converting a HaloLocation enum to a string.
Definition CellUtils.h:61
static std::string fromType(CellType type)
Converts a CellType to a string.
Definition CellUtils.h:97
static HaloLocation toHalo(const std::string &type)
Converts a string to a HaloLocation enum using a dedicated map.
Definition CellUtils.h:195
static std::string fromBoundaryCondition(BoundaryCondition type)
Converts a BoundaryCondition to a string.
Definition CellUtils.h:74
static const std::unordered_map< BoundaryCondition, std::string > conditionStringTable
Reverse map containing conversion information for converting a BoundaryCondition enum to a string.
Definition CellUtils.h:45
static const std::unordered_map< std::string, BoundaryCondition > conditionTable
Map containing conversion information for converting a string to a BoundaryCondition enum.
Definition CellUtils.h:29
static std::string fromHaloVec(const std::vector< HaloLocation > &vec)
Converts a vector of HaloLocation enums to a truncated, concatenated string.
Definition CellUtils.h:130
static const std::unordered_map< std::string, HaloLocation > haloTable
Map containing conversion information for converting a string to a HaloLocation enum.
Definition CellUtils.h:39
static std::array< BoundaryCondition, 6 > stringToBoundaryConditions(const std::string &str)
Converts a string of length 6 to an array of boundary conditions for each boundary.
Definition CellUtils.h:213
static CellType toType(const std::string &type)
Converts a string to a CellType enum using a dedicated map.
Definition CellUtils.h:105
static std::string fromBoundaryConditionArray(const std::array< BoundaryCondition, 6 > &arr)
Converts an array of BoundaryCondition enums to a truncated, concatenated string.
Definition CellUtils.h:168
static std::string fromHalo(HaloLocation type)
Converts a HaloLocation to a string.
Definition CellUtils.h:120
static const std::unordered_map< std::string, CellType > typeTable
Map containing conversion information for converting a string to a CellType enum.
Definition CellUtils.h:35
static BoundaryCondition toBoundaryCondition(const std::string &type)
Converts a string to a BoundaryCondition enum using a dedicated map.
Definition CellUtils.h:82