include/pops/runtime/dynamic/model_registry.hpp Source FileΒΆ

adc_cpp: include/pops/runtime/dynamic/model_registry.hpp Source File
adc_cpp 0.3.0
Model-free C++23 core for coupled hyperbolic-elliptic systems on adaptive (AMR) meshes, with MPI and GPU (Kokkos) backends
model_registry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <stdexcept>
5#include <string>
6
34
35namespace pops {
36
41 const char* name;
42 int n_vars;
44 const char* summary;
45};
46
50inline constexpr TransportTag kTransports[] = {
51 {"exb", 1, true, "scalar ExB drift advection, v = (-d_y phi, d_x phi) / B0"},
52 {"compressible", 4, false, "compressible Euler, 4 var (rho, rho u, rho v, E)"},
53 {"isothermal", 3, true, "isothermal Euler, 3 var (rho, rho u, rho v)"},
54};
55
63struct SourceTag {
64 const char* name;
66 const char* summary;
67};
68
70inline constexpr SourceTag kSources[] = {
71 {"none", 1, "neutral: no source term"},
72 {"potential", 3, "(q/m) rho E electrostatic force"},
73 {"gravity", 3, "rho g gravity force"},
74 {"magnetic", 3, "q v x B_z magnetized Lorentz force (explicit regime)"},
75 {"lorentz", 3, "alias of 'magnetic'"},
76 {"potential_magnetic", 3, "electrostatic + Lorentz, summed"},
77 {"potential_lorentz", 3, "alias of 'potential_magnetic'"},
78};
79
82 const char* name;
83 const char* summary;
84};
85
87inline constexpr EllipticTag kElliptics[] = {
88 {"charge", "rho - q : charge density (Poisson source)"},
89 {"background", "alpha (rho - n0) : neutralizing background"},
90 {"gravity", "sign * 4 pi G (rho - rho0) : gravitational coupling"},
91};
92
93namespace detail {
96template <class TagT, std::size_t N>
97std::string join_tag_names(const TagT (&tbl)[N], const char* sep, bool quote) {
98 std::string out;
99 for (std::size_t i = 0; i < N; ++i) {
100 if (i)
101 out += sep;
102 if (quote)
103 out += '\'';
104 out += tbl[i].name;
105 if (quote)
106 out += '\'';
107 }
108 return out;
109}
110} // namespace detail
111
114inline std::string transport_tags_csv(bool polar = false) {
115 std::string out;
116 for (const TransportTag& t : kTransports) {
117 if (polar && !t.polar_ok)
118 continue;
119 if (!out.empty())
120 out += '|';
121 out += t.name;
122 }
123 return out;
124}
125
127inline std::string source_tags_csv() {
128 return detail::join_tag_names(kSources, "|", false);
129}
130inline std::string elliptic_tags_csv() {
131 return detail::join_tag_names(kElliptics, "|", false);
132}
133
136inline std::string transport_choices() {
137 return detail::join_tag_names(kTransports, " | ", true);
138}
139inline std::string source_choices() {
140 return detail::join_tag_names(kSources, " | ", true);
141}
142inline std::string elliptic_choices() {
143 return detail::join_tag_names(kElliptics, " | ", true);
144}
145
147inline bool is_transport(const std::string& tag) {
148 for (const TransportTag& t : kTransports)
149 if (tag == t.name)
150 return true;
151 return false;
152}
153inline bool is_source(const std::string& tag) {
154 for (const SourceTag& t : kSources)
155 if (tag == t.name)
156 return true;
157 return false;
158}
159inline bool is_elliptic(const std::string& tag) {
160 for (const EllipticTag& t : kElliptics)
161 if (tag == t.name)
162 return true;
163 return false;
164}
165
170inline int transport_n_vars(const std::string& tag) {
171 for (const TransportTag& t : kTransports)
172 if (tag == t.name)
173 return t.n_vars;
174 return -1;
175}
176constexpr int transport_n_vars_ct(const char* name) {
177 for (const TransportTag& t : kTransports) {
178 const char* a = name;
179 const char* b = t.name;
180 while (*a != '\0' && *b != '\0' && *a == *b) {
181 ++a;
182 ++b;
183 }
184 if (*a == '\0' && *b == '\0')
185 return t.n_vars;
186 }
187 return -1;
188}
189
194inline std::string unknown_transport_msg(const std::string& tag) {
195 return "unknown transport '" + tag + "' (" + transport_tags_csv() + ")";
196}
197inline std::string unknown_elliptic_msg(const std::string& tag) {
198 return "unknown elliptic '" + tag + "' (" + elliptic_tags_csv() + ")";
199}
200
208inline void validate_transport(const std::string& tag) {
209 if (!is_transport(tag))
210 throw std::runtime_error(unknown_transport_msg(tag));
211}
212inline void validate_elliptic(const std::string& tag) {
213 if (!is_elliptic(tag))
214 throw std::runtime_error(unknown_elliptic_msg(tag));
215}
216
217} // namespace pops
std::string join_tag_names(const TagT(&tbl)[N], const char *sep, bool quote)
Joins the name field of a tag table into "a<sep>b<sep>..." (optionally each name single-quoted).
Definition model_registry.hpp:97
Definition amr_hierarchy.hpp:29
std::string elliptic_choices()
Definition model_registry.hpp:142
std::string source_choices()
Definition model_registry.hpp:139
void validate_elliptic(const std::string &tag)
Definition model_registry.hpp:212
constexpr int transport_n_vars_ct(const char *name)
Definition model_registry.hpp:176
bool is_source(const std::string &tag)
Definition model_registry.hpp:153
int transport_n_vars(const std::string &tag)
Conservative-variable count of a transport tag (source of truth for the static_assert below),...
Definition model_registry.hpp:170
std::string source_tags_csv()
Pipe list of source / elliptic tags (e.g. "charge|background|gravity").
Definition model_registry.hpp:127
std::string elliptic_tags_csv()
Definition model_registry.hpp:130
constexpr SourceTag kSources[]
SINGLE SOURCE of the builtin sources.
Definition model_registry.hpp:70
constexpr EllipticTag kElliptics[]
SINGLE SOURCE of the builtin elliptic right-hand sides.
Definition model_registry.hpp:87
std::string unknown_elliptic_msg(const std::string &tag)
Definition model_registry.hpp:197
bool is_transport(const std::string &tag)
Membership against the builtin tables.
Definition model_registry.hpp:147
constexpr TransportTag kTransports[]
SINGLE SOURCE of the builtin transports (order = historical display priority, used by the CSV / choic...
Definition model_registry.hpp:50
std::string transport_choices()
Quoted " | "-separated choices (e.g.
Definition model_registry.hpp:136
std::string transport_tags_csv(bool polar=false)
Pipe list of transport tags ("exb|compressible|isothermal"), as used in the dispatch rejection messag...
Definition model_registry.hpp:114
void validate_transport(const std::string &tag)
Validates a transport / elliptic tag against the builtin registry.
Definition model_registry.hpp:208
std::string unknown_transport_msg(const std::string &tag)
Rejection message for an unknown transport / elliptic tag, BYTE-IDENTICAL to the historical inline th...
Definition model_registry.hpp:194
bool is_elliptic(const std::string &tag)
Definition model_registry.hpp:159
Builtin ELLIPTIC right-hand-side brick tag.
Definition model_registry.hpp:81
const char * summary
Definition model_registry.hpp:83
const char * name
Definition model_registry.hpp:82
Builtin SOURCE brick tag.
Definition model_registry.hpp:63
const char * name
Definition model_registry.hpp:64
int min_vars
Definition model_registry.hpp:65
const char * summary
Definition model_registry.hpp:66
Builtin TRANSPORT brick tag.
Definition model_registry.hpp:40
const char * summary
Definition model_registry.hpp:44
int n_vars
Definition model_registry.hpp:42
bool polar_ok
Definition model_registry.hpp:43
const char * name
Definition model_registry.hpp:41