Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
UnitSystem.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 "Unit.hpp"
20#include "details/utils.hpp"
21
22namespace shamunits {
23
48 template<class T>
49 class UnitSystem {
50 public:
51 T s;
52 T m;
53 T kg;
54 T A;
55 T K;
56 T mol;
57 T cd;
65
77 constexpr explicit UnitSystem(
78 T unit_time = 1,
79 T unit_length = 1,
80 T unit_mass = 1,
81 T unit_current = 1,
82 T unit_temperature = 1,
83 T unit_qte = 1,
84 T unit_lumint = 1)
85 : s(1 / unit_time), m(1 / unit_length), kg(1 / unit_mass), A(1 / unit_current),
86 K(1 / unit_temperature), mol(1 / unit_qte), cd(1 / unit_lumint), s_inv(unit_time),
87 m_inv(unit_length), kg_inv(unit_mass), A_inv(unit_current), K_inv(unit_temperature),
88 mol_inv(unit_qte), cd_inv(unit_lumint) {}
89
90 // ---------------------------------------------------------------------------------------
91 // Getters. Units come from shamunits::units, prefixes from shamunits::prefix and constants
92 // from shamunits::constants, and compose freely: get<prefix::mega * units::year>(),
93 // get<upow(units::metre, 2)>(). get<u>(), to<u>() and get(u) never divide.
94 //
95 // Precision: the product is accumulated in Tacc, the widest of T, Tret and double, and
96 // converted to Tret once at the end. Units and their factors are double by design, so
97 // this costs nothing for UnitSystem<f64>; for f32 it keeps intermediate powers (e.g.
98 // s^-3 = 3e40 in a Myr system) from overflowing, so the result is the correctly rounded
99 // value whenever it fits in Tret. The factor is applied last.
101 template<class Tret>
102 using acc_t = decltype(T{} * Tret{} * double{});
103
104 // ---------------------------------------------------------------------------------------
105
120 template<Unit u, class Tret = T>
121 constexpr Tret get() const noexcept {
123 using Tacc = acc_t<Tret>;
124 return Tret(
125 pow_constexpr_fast_inv<u.dim.second>(Tacc(s), Tacc(s_inv))
126 * pow_constexpr_fast_inv<u.dim.metre>(Tacc(m), Tacc(m_inv))
127 * pow_constexpr_fast_inv<u.dim.kilogram>(Tacc(kg), Tacc(kg_inv))
128 * pow_constexpr_fast_inv<u.dim.ampere>(Tacc(A), Tacc(A_inv))
129 * pow_constexpr_fast_inv<u.dim.kelvin>(Tacc(K), Tacc(K_inv))
130 * pow_constexpr_fast_inv<u.dim.mole>(Tacc(mol), Tacc(mol_inv))
131 * pow_constexpr_fast_inv<u.dim.candela>(Tacc(cd), Tacc(cd_inv))
132 * Tacc(u.si_factor));
133 }
134
138 template<Unit u, class Tret = T>
139 constexpr Tret to() const noexcept {
140 return get<upow(u, -1), Tret>();
141 }
142
145 template<class Tret = T, class Tu>
146 constexpr Tret get(Unit<Tu> u) const noexcept {
147 using details::ipow;
148 using Tacc = acc_t<Tret>;
149 return Tret(
150 ipow(Tacc(s), u.dim.second, Tacc(s_inv)) * ipow(Tacc(m), u.dim.metre, Tacc(m_inv))
151 * ipow(Tacc(kg), u.dim.kilogram, Tacc(kg_inv))
152 * ipow(Tacc(A), u.dim.ampere, Tacc(A_inv))
153 * ipow(Tacc(K), u.dim.kelvin, Tacc(K_inv))
154 * ipow(Tacc(mol), u.dim.mole, Tacc(mol_inv))
155 * ipow(Tacc(cd), u.dim.candela, Tacc(cd_inv)) * Tacc(u.si_factor));
156 }
157
160 template<class Tret = T, class Tu>
161 constexpr Tret get(Unit<Tu> u, int power) const noexcept {
162 return get<Tret>(upow(u, power));
163 }
164
166 template<class Tret = T, class Tu>
167 constexpr Tret to(Unit<Tu> u) const noexcept {
168 return get<Tret>(upow(u, -1));
169 }
170
172 template<class Tret = T, class Tu>
173 constexpr Tret to(Unit<Tu> u, int power) const noexcept {
174 return get<Tret>(upow(u, -power));
175 }
176 };
177
178} // namespace shamunits
A physical unit as a value: its SI dimension and its magnitude in SI base units.
constexpr Tret get(Unit< Tu > u, int power) const noexcept
constexpr Tret to(Unit< Tu > u) const noexcept
Inverse of get(u).
constexpr UnitSystem(T unit_time=1, T unit_length=1, T unit_mass=1, T unit_current=1, T unit_temperature=1, T unit_qte=1, T unit_lumint=1)
Construct a new UnitSystem from either supplied base units or SI by default.
decltype(f64{} *Tret{} *double{}) acc_t
constexpr Tret to() const noexcept
constexpr Tret get() const noexcept
Value of the compile-time unit u in this system.
constexpr Tret get(Unit< Tu > u) const noexcept
constexpr Tret to(Unit< Tu > u, int power) const noexcept
Inverse of get(u, power).
namespace containing the units library
constexpr Unit< T > upow(Unit< T > u, int n) noexcept
Integer power of a unit.
Definition Unit.hpp:147
A unit: its dimension and its magnitude expressed in SI base units, in precision Tscal.
Definition Unit.hpp:89
Integer powers with a supplied reciprocal, so a negative exponent never divides.
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
constexpr T ipow(T a, int n, T a_inv) noexcept
Runtime twin of pow_constexpr_fast_inv, for an exponent only known at run time.
Definition utils.hpp:45