Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
shamunits Directory Reference
Directory dependency graph for shamunits:

Files

 
exemple.cpp

Detailed Description

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.

Almost everything is marked constexpr in the library, so most of the conversion if possible will be opmitized away by the compiler, allowing for zero cost abstraction here :)

Here is an example of the usage of the units library :

#include <iostream>
int main(void){
using namespace shamunits;
//create si units
UnitSystem<double> si {};
// get the value of au^2 in the unit system
// but it is quite big :)
std::cout << si.get<units::astronomical_unit,2>() << std::endl;
double sol_mass = Constants<double>(si).sol_mass();
/*
* create a unit system with time in Myr, length in au, mass in solar masses
*/
UnitSystem<double> astro_units {
si.get<mega, units::years>(),
si.get<units::astronomical_unit>(),
si.get<units::kilogram>()*sol_mass,
};
//this time it returns 1 because the base length is the astronomical unit
std::cout << astro_units.get<units::astronomical_unit,2>() << std::endl;
Constants<double> astro_cte {astro_units};
// in those units G is 3.94781e+25
std::cout << astro_cte.G() << std::endl;
//now if the code return a value in astro_units
//we can convert it to any units like so
double value = 12; //here 12 Myr
// print : value = 3.15576e+19 s
std::cout << "value = "<< astro_units.to<units::second>() << " s"<< std::endl;
}
constexpr T get() const noexcept
get the value of a units in the current system
constexpr T to()
To the si units (convert a system unit to si ones). This function is the inverse of get.
namespace containing the units library
Physical constants.
constexpr T sol_mass()
get the value of a sol_mass in the mass unit of the current unit system units
constexpr T G()
get the value of G in the current unit system units

If you want to try here is a godbolt link : https://godbolt.org/z/5zjGMea57