Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Names.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
18
19#include "details/utils.hpp"
20#include <unordered_map>
21#include <stdexcept>
22#include <string>
23
27#define XMAC_UNITS \
28 X1(second, s) /*base units*/ \
29 X1(metre, m) \
30 X1(kilogram, kg) \
31 X1(Ampere, A) \
32 X1(Kelvin, K) \
33 X1(mole, mol) \
34 X1(candela, cd) \
35 /*derived units*/ \
36 X1(Hertz, Hz) /* hertz : frequency (s-1) */ \
37 X1(Newton, N) /* (kg.m.s-2)*/ \
38 X1(Pascal, Pa) /* (kg.m-1.s-2) = (N/m2)*/ \
39 X1(Joule, J) /* (kg.m2.s-2) = (N.m = Pa.m3)*/ \
40 X1(Watt, W) /* (kg.m2.s-3) = (J/s)*/ \
41 X1(Coulomb, C) /* (s.A)*/ \
42 X1(Volt, V) /* (kg.m2.s-3.A-1) = (W/A) = (J/C)*/ \
43 X1(Farad, F) /* (kg-1.m-2.s4.A2) = (C/V) = (C2/J)*/ \
44 X1(Ohm, ohm) /* (kg.m2.s-3.A-2) = (V/A) = (J.s/C2)*/ \
45 X1(Siemens, S) /* (kg-1.m-2.s3.A2) = (ohm-1)*/ \
46 X1(Weber, Wb) /* (kg.m2.s-2.A-1) = (V.s)*/ \
47 X1(Tesla, T) /* (kg.s-2.A-1) = (Wb/m2)*/ \
48 X1(Henry, H) /* (kg.m2.s-2.A-2) = (Wb/A)*/ \
49 X1(lumens, lm) /* (cd.sr) = (cd.sr)*/ \
50 X1(lux, lx) /* (cd.sr.m-2) = (lm/m2)*/ \
51 X1(Bequerel, Bq) /* (s-1)*/ \
52 X1(Gray, Gy) /* (m2.s-2) = (J/kg)*/ \
53 X1(Sievert, Sv) /* (m2.s-2) = (J/kg)*/ \
54 X1(katal, kat) /* (mol.s-1) */ \
55 /*relative units*/ \
56 X1(minutes, mn) \
57 X1(hours, hr) \
58 X1(days, dy) \
59 X1(years, yr) \
60 X1(astronomical_unit, au) \
61 X1(light_year, ly) \
62 X1(parsec, pc) \
63 X1(solar_radius, rsol) \
64 X1(earth_radius, rearth) \
65 X1(solar_mass, sol_mass) \
66 X1(electron_volt, eV) \
67 X1(ergs, erg) \
68 X1(british_pint, pint)
69
71#define XMAC_UNIT_PREFIX \
72 X(tera, T, 12) \
73 X(giga, G, 9) \
74 X(mega, M, 6) \
75 X(kilo, k, 3) \
76 X(hecto, hect, 2) \
77 X(deca, dec, 1) \
78 X(None, _, 0) \
79 /*X(deci ,deci_, -1)*/ \
80 X(centi, c, -2) \
81 X(milli, m, -3) \
82 X(micro, mu, -6) \
83 X(nano, n, -9) \
84 X(pico, p, -12) \
85 X(femto, f, -15)
86
87namespace shamunits {
88
92 // clang-format off
93 #define X(longname, shortname, value) longname = value, shortname = value,
95 #undef X
96 // clang-format on
97 };
98
100 template<class T, UnitPrefix p>
101 inline constexpr T get_prefix_val() {
103 }
104
107 static const std::unordered_map<std::string, UnitPrefix> map_name_to_unit_prefix{
108 // clang-format off
109 #define X(longname, shortname, value) {#longname, longname}, {#shortname, shortname},
111 #undef X
112 // clang-format on
113 };
114
117 static const std::unordered_map<UnitPrefix, std::string> map_u_to_name_prefix = {
118 // clang-format off
119 #define X(longname, shortname, value) {shortname, #shortname},
121 #undef X
122 // clang-format on
123 };
124
126 inline const std::string get_unit_prefix_name(UnitPrefix p) {
127
128 map_u_to_name_prefix.find(p);
129
130 if (auto search = map_u_to_name_prefix.find(p); search != map_u_to_name_prefix.end()) {
131 return search->second;
132 }
133
134 return "[Unknown Unit prefix name]";
135 }
136
138 inline const UnitPrefix unit_prefix_from_name(std::string p) {
139
140 map_name_to_unit_prefix.find(p);
141
142 if (auto search = map_name_to_unit_prefix.find(p);
143 search != map_name_to_unit_prefix.end()) {
144 return search->second;
145 }
146
147 throw std::invalid_argument("this unit prefix name is unknown");
148 return None; // to silence a warning
149 }
150
151 namespace units {
152
153 // clang-format off
155 enum UnitName {
157 #define X1(longname, shortname) longname, shortname = longname,
159 #undef X1
160 };
161
163 static const std::unordered_map<std::string, UnitName> map_name_to_unit{
165 #define X1(longname, shortname) {#longname, longname}, {#shortname, shortname},
167 #undef X1
168 };
169
171 static const std::unordered_map<UnitName, std::string> map_u_to_name = {
173 #define X1(longname, shortname) {shortname, #shortname},
175 #undef X1
176 };
177 // clang-format on
178
180 inline const std::string get_unit_name(UnitName p) {
181
182 map_u_to_name.find(p);
183
184 if (auto search = map_u_to_name.find(p); search != map_u_to_name.end()) {
185 return search->second;
186 }
187
188 return "[Unknown Unit name]";
189 }
190
192 inline const UnitName unit_from_name(std::string p) {
193
194 auto search = map_name_to_unit.find(p);
195
196 if (search != map_name_to_unit.end()) {
197 return search->second;
198 }
199
200 throw std::invalid_argument("this unit name is unknown : " + p);
201 return s; // to silence a warning
202 }
203
204 } // namespace units
205
206} // namespace shamunits
#define XMAC_UNITS
Definition of all units.
Definition Names.hpp:27
UnitName
List of all units name.
Definition Names.hpp:155
const UnitName unit_from_name(std::string p)
Get the UnitName enum value from a unit name as a string.
Definition Names.hpp:192
const std::string get_unit_name(UnitName p)
Get the unit name for a UnitName enum value.
Definition Names.hpp:180
#define XMAC_UNIT_PREFIX
Definition of all prefixes.
Definition Names.hpp:71
namespace containing the units library
const std::string get_unit_prefix_name(UnitPrefix p)
Get the prefix name for a UnitPrefix enum value.
Definition Names.hpp:126
UnitPrefix
Enum of all prefixes.
Definition Names.hpp:90
constexpr T get_prefix_val()
Get the value of a prefix.
Definition Names.hpp:101
const UnitPrefix unit_prefix_from_name(std::string p)
Get the UnitPrefix enum value from a prefix name as a string.
Definition Names.hpp:138
constexpr T pow_constexpr_fast_inv(T a, T a_inv) noexcept
Constexpr power expansion function with supplied inverse for negative values.
Definition utils.hpp:22