Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
prefix_table.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
20
21#include "shamunits/Unit.hpp"
23#include <string_view>
24#include <array>
25
26namespace shamunits::prefix {
27
28 inline constexpr Unit<> tera = scale(1e12);
29 inline constexpr Unit<> giga = scale(1e9);
30 inline constexpr Unit<> mega = scale(1e6);
31 inline constexpr Unit<> kilo = scale(1e3);
32 inline constexpr Unit<> hecto = scale(1e2);
33 inline constexpr Unit<> deca = scale(1e1);
34 inline constexpr Unit<> none = scale(1.);
35 inline constexpr Unit<> centi = scale(1e-2);
36 inline constexpr Unit<> milli = scale(1e-3);
37 inline constexpr Unit<> micro = scale(1e-6);
38 inline constexpr Unit<> nano = scale(1e-9);
39 inline constexpr Unit<> pico = scale(1e-12);
40 inline constexpr Unit<> femto = scale(1e-15);
41
43 inline constexpr auto registry = std::to_array<NamedUnit>({
44 {"tera", "T", tera},
45 {"giga", "G", giga},
46 {"mega", "M", mega},
47 {"kilo", "k", kilo},
48 {"hecto", "hect", hecto},
49 {"deca", "dec", deca},
50 {"None", "_", none},
51 {"centi", "c", centi},
52 {"milli", "m", milli},
53 {"micro", "mu", micro},
54 {"nano", "n", nano},
55 {"pico", "p", pico},
56 {"femto", "f", femto},
57 });
58
59 static_assert(details::names_are_valid(registry), "empty or duplicate prefix name");
60
62 inline Unit<> from_name(std::string_view name) {
63 return details::unit_from_name(registry, name, "prefix");
64 }
65
66} // namespace shamunits::prefix
A physical unit as a value: its SI dimension and its magnitude in SI base units.
constexpr Unit< T > scale(T s) noexcept
A dimensionless scale factor, e.g. a prefix: scale(1e6) (precision of the argument).
Definition Unit.hpp:171
constexpr auto registry
Name registry: Python passes pref="None" by default, so "None" must stay.
constexpr Unit tera
T.
constexpr Unit kilo
k
constexpr Unit pico
p
constexpr Unit deca
dec
constexpr Unit none
no prefix ("None", "_")
constexpr Unit milli
m
constexpr Unit mega
M.
constexpr Unit giga
G.
constexpr Unit centi
c
constexpr Unit micro
mu
constexpr Unit nano
n
constexpr Unit hecto
hect
Unit from_name(std::string_view name)
Prefix from its long or short name; throws std::invalid_argument if unknown.
constexpr Unit femto
f
Name tables mapping long and short names to units, with constexpr lookup.
Unit unit_from_name(std::span< const NamedUnit > registry, std::string_view name, std::string_view kind="unit")
Unit named name in registry; throws std::invalid_argument when unknown.
Definition registry.hpp:58
constexpr bool names_are_valid(std::span< const NamedUnit > registry) noexcept
True when no name is empty and no name appears twice, long and short names together.
Definition registry.hpp:70
A unit: its dimension and its magnitude expressed in SI base units, in precision Tscal.
Definition Unit.hpp:89