Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
shamunits::UnitSystem< T > Class Template Reference

A unit system, defined by the SI value of its base units. More...

#include <shamunits/include/shamunits/UnitSystem.hpp>

Collaboration diagram for shamunits::UnitSystem< T >:

Public Types

template<class Tret>
using acc_t = decltype(T{} * Tret{} * double{})
 Accumulation type of the Unit-value getters: the widest of T, Tret and double.

Public Member Functions

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.
template<Unit u, class Tret = T>
constexpr Tret get () const noexcept
 Value of the compile-time unit u in this system.
template<Unit u, class Tret = T>
constexpr Tret to () const noexcept
template<class Tret = T, class Tu>
constexpr Tret get (Unit< Tu > u) const noexcept
template<class Tret = T, class Tu>
constexpr Tret get (Unit< Tu > u, int power) const noexcept
template<class Tret = T, class Tu>
constexpr Tret to (Unit< Tu > u) const noexcept
 Inverse of get(u).
template<class Tret = T, class Tu>
constexpr Tret to (Unit< Tu > u, int power) const noexcept
 Inverse of get(u, power).

Public Attributes

T s
 value of a second in this system
T m
 value of a metre in this system
T kg
 value of a kilogram in this system
T A
 value of an ampere in this system
T K
 value of a kelvin in this system
T mol
 value of a mole in this system
T cd
 value of a candela in this system
T s_inv
 inverse value of a second in this system (the time unit in s)
T m_inv
 inverse value of a metre in this system (the length unit in m)
T kg_inv
 inverse value of a kilogram in this system (the mass unit in kg)
T A_inv
 inverse value of an ampere in this system
T K_inv
 inverse value of a kelvin in this system
T mol_inv
 inverse value of a mole in this system
T cd_inv
 inverse value of a candela in this system

Detailed Description

template<class T>
class shamunits::UnitSystem< T >

A unit system, defined by the SI value of its base units.

Units, prefixes and constants come from shamunits/unit_table.hpp, shamunits/prefix_table.hpp and shamunits/constant_table.hpp, and compose freely:

using namespace shamunits;
constexpr UnitSystem<double> si{};
// time in Myr, length in au, mass in solar masses
constexpr UnitSystem<double> astro{
};
astro.get<upow(units::astronomical_unit, 2)>(); // 1
astro.get<constants::G>(); // 3.947813e13
astro.to<units::second>(); // one code time unit in s
astro.get<constants::G, float>(); // result in another precision
astro.get(units::from_name("pc"), -1); // unit known at run time
A unit system, defined by the SI value of its base units.
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.
constexpr Tret to() const noexcept
constexpr Tret get() const noexcept
Value of the compile-time unit u in this system.
constexpr Unit sol_mass
solar mass
namespace containing the units library
constexpr Unit< T > upow(Unit< T > u, int n) noexcept
Integer power of a unit.
Definition Unit.hpp:147
constexpr Unit mega
M.
Unit from_name(std::string_view name)
Unit from its long or short name; throws std::invalid_argument if unknown.
constexpr Unit second
s
constexpr Unit year
yr (Julian)
constexpr Unit astronomical_unit
au

Definition at line 49 of file UnitSystem.hpp.

Member Typedef Documentation

◆ acc_t

template<class T>
template<class Tret>
using shamunits::UnitSystem< T >::acc_t = decltype(T{} * Tret{} * double{})

Accumulation type of the Unit-value getters: the widest of T, Tret and double.

Definition at line 102 of file UnitSystem.hpp.

Constructor & Destructor Documentation

◆ UnitSystem()

template<class T>
shamunits::UnitSystem< T >::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 )
inlineexplicitconstexpr

Construct a new UnitSystem from either supplied base units or SI by default.

Parameters
unit_time
unit_length
unit_mass
unit_current
unit_temperature
unit_qte
unit_lumint

Definition at line 77 of file UnitSystem.hpp.

Member Function Documentation

◆ get() [1/3]

template<class T>
template<Unit u, class Tret = T>
Tret shamunits::UnitSystem< T >::get ( ) const
inlineconstexprnoexcept

Value of the compile-time unit u in this system.

u is a by-value template parameter, so every exponent is a constant expression: dimensions with exponent 0 expand to nothing. The parameter is Unit u (deduced) on purpose; it compiles on clang 15. Never replace it with a raw double template parameter, which clang < 18 rejects.

astro.get<units::astronomical_unit>(); // in this system's lengths
si.get<prefix::mega * units::year>(); // 3.15576e13
usys.get<constants::G, f32>(); // result as f32
float f32
Alias for float.

Definition at line 121 of file UnitSystem.hpp.

◆ get() [2/3]

template<class T>
template<class Tret = T, class Tu>
Tret shamunits::UnitSystem< T >::get ( Unit< Tu > u) const
inlineconstexprnoexcept

Value of a unit only known at run time (e.g. looked up by name). Exponents are runtime values; the stored *_inv members serve negative exponents, so there is no division.

Definition at line 146 of file UnitSystem.hpp.

◆ get() [3/3]

template<class T>
template<class Tret = T, class Tu>
Tret shamunits::UnitSystem< T >::get ( Unit< Tu > u,
int power ) const
inlineconstexprnoexcept

get(u) for u raised to a runtime power: exactly get(upow(u, power)). The only entry point that can divide (in upow, for power < 0); this is the Python path.

Definition at line 161 of file UnitSystem.hpp.

◆ to() [1/3]

template<class T>
template<Unit u, class Tret = T>
Tret shamunits::UnitSystem< T >::to ( ) const
inlineconstexprnoexcept

Inverse of get<u>(): one unit of this system expressed in u, e.g. to<units::kilogram>() is the code mass unit in kg. The reciprocal of the factor is a constant expression, so this does not divide either.

Definition at line 139 of file UnitSystem.hpp.

◆ to() [2/3]

template<class T>
template<class Tret = T, class Tu>
Tret shamunits::UnitSystem< T >::to ( Unit< Tu > u) const
inlineconstexprnoexcept

Inverse of get(u).

Definition at line 167 of file UnitSystem.hpp.

◆ to() [3/3]

template<class T>
template<class Tret = T, class Tu>
Tret shamunits::UnitSystem< T >::to ( Unit< Tu > u,
int power ) const
inlineconstexprnoexcept

Inverse of get(u, power).

Definition at line 173 of file UnitSystem.hpp.

Member Data Documentation

◆ A

template<class T>
T shamunits::UnitSystem< T >::A

value of an ampere in this system

Definition at line 54 of file UnitSystem.hpp.

◆ A_inv

template<class T>
T shamunits::UnitSystem< T >::A_inv

inverse value of an ampere in this system

Definition at line 61 of file UnitSystem.hpp.

◆ cd

template<class T>
T shamunits::UnitSystem< T >::cd

value of a candela in this system

Definition at line 57 of file UnitSystem.hpp.

◆ cd_inv

template<class T>
T shamunits::UnitSystem< T >::cd_inv

inverse value of a candela in this system

Definition at line 64 of file UnitSystem.hpp.

◆ K

template<class T>
T shamunits::UnitSystem< T >::K

value of a kelvin in this system

Definition at line 55 of file UnitSystem.hpp.

◆ K_inv

template<class T>
T shamunits::UnitSystem< T >::K_inv

inverse value of a kelvin in this system

Definition at line 62 of file UnitSystem.hpp.

◆ kg

template<class T>
T shamunits::UnitSystem< T >::kg

value of a kilogram in this system

Definition at line 53 of file UnitSystem.hpp.

◆ kg_inv

template<class T>
T shamunits::UnitSystem< T >::kg_inv

inverse value of a kilogram in this system (the mass unit in kg)

Definition at line 60 of file UnitSystem.hpp.

◆ m

template<class T>
T shamunits::UnitSystem< T >::m

value of a metre in this system

Definition at line 52 of file UnitSystem.hpp.

◆ m_inv

template<class T>
T shamunits::UnitSystem< T >::m_inv

inverse value of a metre in this system (the length unit in m)

Definition at line 59 of file UnitSystem.hpp.

◆ mol

template<class T>
T shamunits::UnitSystem< T >::mol

value of a mole in this system

Definition at line 56 of file UnitSystem.hpp.

◆ mol_inv

template<class T>
T shamunits::UnitSystem< T >::mol_inv

inverse value of a mole in this system

Definition at line 63 of file UnitSystem.hpp.

◆ s

template<class T>
T shamunits::UnitSystem< T >::s

value of a second in this system

Definition at line 51 of file UnitSystem.hpp.

◆ s_inv

template<class T>
T shamunits::UnitSystem< T >::s_inv

inverse value of a second in this system (the time unit in s)

Definition at line 58 of file UnitSystem.hpp.


The documentation for this class was generated from the following file: