Shamrock units library
This is the units library in use in the Shamrock code, this repository will be updated when change are made to this library in the Shamrock monorepo.
A unit, a prefix and a physical constant are all the same thing: a shamunits::Unit, i.e. an SI dimension and a factor. They live in three tables:
- shamunits::units (unit_table.hpp): units::metre, units::year, units::astronomical_unit, ...
- shamunits::prefix (prefix_table.hpp): prefix::kilo, prefix::mega, ...
- shamunits::constants (constant_table.hpp): constants::G, constants::c, constants::sol_mass, ...
and compose with *, / and upow. A UnitSystem<T> gives the value of any of them in that system with get<u>(), and to<u>() goes the other way. Each table also has a from_name lookup for names only known at run time (this is what the Python bindings use).
Everything is constexpr: with a compile-time unit, get<u>() is the product of the needed base-unit powers and the unit's factor, with no division, and it folds to a constant when the unit system is itself constexpr.
Here is an example of the usage of the units library (exemple.cpp):
#include <iostream>
int main(void) {
constexpr UnitSystem<double> si{};
constexpr UnitSystem<double> astro{
};
std::cout << astro.
get<constants::G>() << std::endl;
double t = 12;
std::cout << Constants<double>{astro}.get<constants::G>() << std::endl;
}
Physical constants in a unit system, plus a few dimensionless numbers.
A unit system: the values of the seven SI base units in code units.
constexpr Tret to() const noexcept
constexpr Tret get() const noexcept
Value of the compile-time unit u in this system.
The table of physical constants: each constant is a Unit (its SI value and dimension).
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.
The table of unit prefixes: a prefix is a dimensionless Unit.
The table of units: one constexpr Unit per unit, plus its name registry.
Unit from_name(std::string_view name)
Unit from its long or short name; throws std::invalid_argument if unknown.
constexpr Unit year
yr (Julian)
constexpr Unit astronomical_unit
au
src/shamunits/doc/godbolt.cpp is a single-file version of the library plus this example, ready to paste in Compiler Explorer (with -std=c++20). It is generated by tools/gen_shamunits_amalgamation.py.