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
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(electron_volt, eV) \
66 X1(ergs, erg) \
67 X1(british_pint, pint)
68
70#define XMAC_UNIT_PREFIX \
71 X(tera, T, 12) \
72 X(giga, G, 9) \
73 X(mega, M, 6) \
74 X(kilo, k, 3) \
75 X(hecto, hect, 2) \
76 X(deca, dec, 1) \
77 X(None, _, 0) \
78 /*X(deci ,deci_, -1)*/ \
79 X(centi, c, -2) \
80 X(milli, m, -3) \
81 X(micro, mu, -6) \
82 X(nano, n, -9) \
83 X(pico, p, -12) \
84 X(femto, f, -15)
85
86namespace shamunits {
87
91 // clang-format off
92 #define X(longname, shortname, value) longname = value, shortname = value,
94 #undef X
95 // clang-format on
96 };
97
99 template<class T, UnitPrefix p>
100 inline constexpr T get_prefix_val() {
101 return details::pow_constexpr_fast_inv<p, T>(10, 1e-1);
102 }
103
106 static const std::unordered_map<std::string, UnitPrefix> map_name_to_unit_prefix{
107 // clang-format off
108 #define X(longname, shortname, value) {#longname, longname}, {#shortname, shortname},
110 #undef X
111 // clang-format on
112 };
113
116 static const std::unordered_map<UnitPrefix, std::string> map_u_to_name_prefix = {
117 // clang-format off
118 #define X(longname, shortname, value) {shortname, #shortname},
120 #undef X
121 // clang-format on
122 };
123
125 inline const std::string get_unit_prefix_name(UnitPrefix p) {
126
127 map_u_to_name_prefix.find(p);
128
129 if (auto search = map_u_to_name_prefix.find(p); search != map_u_to_name_prefix.end()) {
130 return search->second;
131 }
132
133 return "[Unknown Unit prefix name]";
134 }
135
137 inline const UnitPrefix unit_prefix_from_name(std::string p) {
138
139 map_name_to_unit_prefix.find(p);
140
141 if (auto search = map_name_to_unit_prefix.find(p);
142 search != map_name_to_unit_prefix.end()) {
143 return search->second;
144 }
145
146 throw std::invalid_argument("this unit prefix name is unknown");
147 return None; // to silence a warning
148 }
149
150 namespace units {
151
152 // clang-format off
154 enum UnitName {
156 #define X1(longname, shortname) longname, shortname = longname,
158 #undef X1
159 };
160
162 static const std::unordered_map<std::string, UnitName> map_name_to_unit{
164 #define X1(longname, shortname) {#longname, longname}, {#shortname, shortname},
166 #undef X1
167 };
168
170 static const std::unordered_map<UnitName, std::string> map_u_to_name = {
172 #define X1(longname, shortname) {shortname, #shortname},
174 #undef X1
175 };
176 // clang-format on
177
179 inline const std::string get_unit_name(UnitName p) {
180
181 map_u_to_name.find(p);
182
183 if (auto search = map_u_to_name.find(p); search != map_u_to_name.end()) {
184 return search->second;
185 }
186
187 return "[Unknown Unit name]";
188 }
189
191 inline const UnitName unit_from_name(std::string p) {
192
193 auto search = map_name_to_unit.find(p);
194
195 if (search != map_name_to_unit.end()) {
196 return search->second;
197 }
198
199 throw std::invalid_argument("this unit name is unknown : " + p);
200 return s; // to silence a warning
201 }
202
203 } // namespace units
204
205} // namespace shamunits
#define XMAC_UNITS
Definition of all units.
Definition Names.hpp:27
UnitName
List of all units name.
Definition Names.hpp:154
const UnitName unit_from_name(std::string p)
Get the UnitName enum value from a unit name as a string.
Definition Names.hpp:191
const std::string get_unit_name(UnitName p)
Get the unit name for a UnitName enum value.
Definition Names.hpp:179
#define XMAC_UNIT_PREFIX
Definition of all prefixes.
Definition Names.hpp:70
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:125
UnitPrefix
Enum of all prefixes.
Definition Names.hpp:89
constexpr T get_prefix_val()
Get the value of a prefix.
Definition Names.hpp:100
const UnitPrefix unit_prefix_from_name(std::string p)
Get the UnitPrefix enum value from a prefix name as a string.
Definition Names.hpp:137