Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Unit.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
31
32namespace shamunits {
33
35 struct Dimension {
36 int second = 0;
37 int metre = 0;
38 int kilogram = 0;
39 int ampere = 0;
40 int kelvin = 0;
41 int mole = 0;
42 int candela = 0;
43
45 constexpr bool operator==(const Dimension &) const = default;
46 };
47
49 constexpr Dimension operator*(Dimension a, Dimension b) noexcept {
50 return {
51 .second = a.second + b.second,
52 .metre = a.metre + b.metre,
53 .kilogram = a.kilogram + b.kilogram,
54 .ampere = a.ampere + b.ampere,
55 .kelvin = a.kelvin + b.kelvin,
56 .mole = a.mole + b.mole,
57 .candela = a.candela + b.candela,
58 };
59 }
60
62 constexpr Dimension operator/(Dimension a, Dimension b) noexcept {
63 return {
64 .second = a.second - b.second,
65 .metre = a.metre - b.metre,
66 .kilogram = a.kilogram - b.kilogram,
67 .ampere = a.ampere - b.ampere,
68 .kelvin = a.kelvin - b.kelvin,
69 .mole = a.mole - b.mole,
70 .candela = a.candela - b.candela,
71 };
72 }
73
75 constexpr Dimension dim_pow(Dimension d, int n) noexcept {
76 return {
77 .second = d.second * n,
78 .metre = d.metre * n,
79 .kilogram = d.kilogram * n,
80 .ampere = d.ampere * n,
81 .kelvin = d.kelvin * n,
82 .mole = d.mole * n,
83 .candela = d.candela * n,
84 };
85 }
86
88 template<class Tscal = double>
89 struct Unit {
91 Tscal si_factor = 1;
92
94 constexpr bool operator==(const Unit &) const = default;
95 };
96
98 template<class Tdst, class Tsrc>
99 constexpr Unit<Tdst> unit_cast(Unit<Tsrc> u) noexcept {
100 return {.dim = u.dim, .si_factor = static_cast<Tdst>(u.si_factor)};
101 }
102
104 template<class T>
105 constexpr Unit<T> operator*(Unit<T> a, Unit<T> b) noexcept {
106 return {.dim = a.dim * b.dim, .si_factor = a.si_factor * b.si_factor};
107 }
108
110 template<class T>
111 constexpr Unit<T> operator/(Unit<T> a, Unit<T> b) noexcept {
112 return {.dim = a.dim / b.dim, .si_factor = a.si_factor / b.si_factor};
113 }
114
116 template<class T>
117 constexpr Unit<T> operator*(T s, Unit<T> u) noexcept {
118 return {.dim = u.dim, .si_factor = s * u.si_factor};
119 }
120
122 template<class T>
123 constexpr Unit<T> operator*(Unit<T> u, T s) noexcept {
124 return {.dim = u.dim, .si_factor = u.si_factor * s};
125 }
126
128 template<class T>
129 constexpr Unit<T> operator/(Unit<T> u, T s) noexcept {
130 return {.dim = u.dim, .si_factor = u.si_factor / s};
131 }
132
134 template<class T>
135 constexpr Unit<T> operator/(T s, Unit<T> u) noexcept {
136 return {.dim = Dimension{} / u.dim, .si_factor = s / u.si_factor};
137 }
138
146 template<class T>
147 constexpr Unit<T> upow(Unit<T> u, int n) noexcept {
148 T f = 1;
149 T b = u.si_factor;
150 int k = (n < 0) ? -n : n;
151 while (k != 0) {
152 if ((k & 1) != 0) {
153 f *= b;
154 }
155 k >>= 1;
156 if (k != 0) { // squaring past the last bit could overflow when the result fits
157 b *= b;
158 }
159 }
160 return {.dim = dim_pow(u.dim, n), .si_factor = (n < 0) ? T(1) / f : f};
161 }
162
164 template<class T = double>
165 constexpr Unit<T> base_unit(Dimension d) noexcept {
166 return {.dim = d, .si_factor = T(1)};
167 }
168
170 template<class T>
171 constexpr Unit<T> scale(T s) noexcept {
172 return {.dim = Dimension{}, .si_factor = s};
173 }
174
175} // namespace shamunits
namespace containing the units library
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 Dimension dim_pow(Dimension d, int n) noexcept
Dimension raised to an integer power: exponents scale.
Definition Unit.hpp:75
constexpr Dimension operator/(Dimension a, Dimension b) noexcept
Dimension of a quotient: exponents subtract.
Definition Unit.hpp:62
constexpr Dimension operator*(Dimension a, Dimension b) noexcept
Dimension of a product: exponents add.
Definition Unit.hpp:49
constexpr Unit< Tdst > unit_cast(Unit< Tsrc > u) noexcept
Explicit precision conversion, e.g. unit_cast<float>(units::metre).
Definition Unit.hpp:99
constexpr Unit< T > base_unit(Dimension d) noexcept
A base unit of the given dimension with factor 1, e.g. base_unit({.metre = 1}).
Definition Unit.hpp:165
constexpr Unit< T > upow(Unit< T > u, int n) noexcept
Integer power of a unit.
Definition Unit.hpp:147
Exponents of the seven SI base dimensions.
Definition Unit.hpp:35
int mole
amount of substance exponent
Definition Unit.hpp:41
int kilogram
mass exponent
Definition Unit.hpp:38
int kelvin
temperature exponent
Definition Unit.hpp:40
int metre
length exponent
Definition Unit.hpp:37
int ampere
electric current exponent
Definition Unit.hpp:39
constexpr bool operator==(const Dimension &) const =default
Exponent-wise equality.
int candela
luminous intensity exponent
Definition Unit.hpp:42
int second
time exponent
Definition Unit.hpp:36
A unit: its dimension and its magnitude expressed in SI base units, in precision Tscal.
Definition Unit.hpp:89
constexpr bool operator==(const Unit &) const =default
Exact equality of dimension and factor.
Dimension dim
SI dimension.
Definition Unit.hpp:90
Tscal si_factor
value of one of this unit in SI base units
Definition Unit.hpp:91