Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Constants.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
21#include "Names.hpp"
22#include "UnitSystem.hpp"
23#include "details/utils.hpp"
24#include <stdexcept>
25
32#define addconstant(name) \
33 template<int power = 1> \
34 inline constexpr T name()
35
37#define Uget(unitname, mult_pow) units.template get<None, units::unitname, (mult_pow) * power>()
38
40#define Cget(constant_name, mult_pow) \
41 details::pow_constexpr_fast_inv<(mult_pow) * power>(constant_name, 1 / constant_name)
42
44#define UNITS_CONSTANTS \
45 /* si base ctes */ \
46 X(delta_nu_cs , Uget(Hertz, 1)) \
47 X(c /************/, Uget(m, 1) * Uget(s, -1)) \
48 X(h /************/, Uget(Joule, 1) * Uget(s, -1)) \
49 X(e /************/, Uget(Coulomb, 1)) \
50 X(k /************/, Uget(Joule, 1) * Uget(Kelvin, -1)) \
51 X(Na /***********/, Uget(mole, -1)) \
52 X(Kcd /**********/, Uget(lm, 1) * Uget(Watt, -1)) \
53 /* times */ \
54 X(second , Uget(s, 1)) \
55 X(minute , Uget(s, 1)) \
56 X(hour , Uget(s, 1)) \
57 X(day /***/, Uget(s, 1)) \
58 X(year , Uget(s, 1)) \
59 /* sizes */ \
60 X(au /*****************/, Uget(m, 1)) \
61 X(astronomical_unit , Uget(m, 1)) \
62 X(light_year /*********/, Uget(m, 1)) \
63 X(parsec /*************/, Uget(m, 1)) \
64 X(solar_radius /*******/, Uget(m, 1)) \
65 X(earth_radius /*******/, Uget(m, 1)) \
66 X(planck_length /******/, Uget(m, 1)) \
67 /* masses */ \
68 X(proton_mass /****/, Uget(kg, 1)) \
69 X(electron_mass , Uget(kg, 1)) \
70 X(earth_mass /*****/, Uget(kg, 1)) \
71 X(jupiter_mass /***/, Uget(kg, 1)) \
72 X(sol_mass /*******/, Uget(kg, 1)) \
73 X(planck_mass /****/, Uget(kg, 1)) \
74 X(dalton /*********/, Uget(kg, 1)) \
75 /* densities */ \
76 X(guiness_density, Uget(kg, 1) * Uget(m, -1)) \
77 /* derived ctes */ \
78 X(G /**********/, Uget(N, 1) * Uget(m, 2) * Uget(kg, -2)) \
79 X(hbar /*******/, Uget(Joule, 1) * Uget(s, -1)) \
80 X(mu_0 /*******/, Uget(N, 1) * Uget(A, -2)) \
81 X(Z_0 /********/, Uget(Ohm, 1)) \
82 X(epsilon_0 , Uget(F, 1) * Uget(m, -1)) \
83 X(ke /*********/, Uget(N, 1) * Uget(m, 2) * Uget(Coulomb, -2)) \
84 X(kb /*********/, Uget(J, 1) * Uget(K, -1)) \
85 X(sigma /******/, Uget(W, 1) * Uget(m, -2) * Uget(K, -4))
86
87namespace shamunits {
88
93 template<class T>
94 constexpr T pi = 3.141592653589793116;
95
97 template<class T>
98 constexpr T fine_structure = 0.0072973525693;
99
101 template<class T>
102 constexpr T proton_electron_ratio = 1836.1526734311;
103
105 template<class T>
106 constexpr T electron_proton_ratio = 1 / proton_electron_ratio<T>;
107
111 template<class T>
112 struct Constants {
113
116
118 struct Si {
119
120 // si system base constants
121 static constexpr T delta_nu_cs = 9192631770; // (s-1)
122 static constexpr T c = 299792458; // (m.s-1)
123 static constexpr T h = 6.62607015e-34; // (J.s-1)
124 static constexpr T e = 1.602176634e-19; // (C)
125 static constexpr T k = 1.380649e-23; // (J.K-1 )
126 static constexpr T Na = 6.02214076e23; // (mol-1 )
127 static constexpr T Kcd = 683; // (lm.W-1)
128
129 // other constants in si units
130 static constexpr T G = 6.6743015e-11; // (N.m2.kg-2)
131 static constexpr T hbar = h / (2 * pi<T>); // (J.s-1)
132 static constexpr T mu_0 = 1.2566370621219e-6; // (N.A-2)
133 static constexpr T Z_0 = mu_0 * c; // (Ohm)
134 static constexpr T epsilon_0 = 1 / (Z_0 * c); // (F.m-1)
135 static constexpr T ke = 1 / (4 * pi<T> * epsilon_0); // (N.m2.C-2)
136 static constexpr T kb = 1.380649e-23; // (J.K-1)
137
139 static constexpr T sigma = 5.670374419e-8;
140
141 static constexpr T second = 1; //(s)
142 static constexpr T minute = Conv::mn_to_s; //(s)
143 static constexpr T hour = Conv::hr_to_s; //(s)
144 static constexpr T day = Conv::dy_to_s; //(s)
145 static constexpr T year = Conv::yr_to_s; //(s)
146
147 static constexpr T astronomical_unit = Conv::au_to_m; //(m)
148 static constexpr T au = astronomical_unit; //(m)
149 static constexpr T light_year = Conv::ly_to_m; //(m)
150 static constexpr T parsec = Conv::pc_to_m; //(m)
151 static constexpr T solar_radius = Conv::rsol_to_m; //(m)
152 static constexpr T earth_radius = Conv::rearth_to_m; //(m)
153 static constexpr T planck_length = 1.61625518e-35; //(m)
154
155 static constexpr T proton_mass = 1.67262192e-27; //(kg)
156 static constexpr T electron_mass = proton_mass * electron_proton_ratio<T>; //(kg)
157 static constexpr T earth_mass = 5.9722e24; //(kg)
158 static constexpr T jupiter_mass = 1.898e27; //(kg)
159 static constexpr T sol_mass = 1.98847e30; //(kg)
160 static constexpr T planck_mass = 2.17643424e-8; //(kg)
161 static constexpr T dalton = 1.66053906892e-27; //(kg)
162
163 static constexpr T guiness_density = Conv::gcm3_to_guiness_density * 1000; //(kg.m-3)
164 };
165
168
171
173#define X(name, conv) \
174 addconstant(name) { return Cget(Si::name, 1) * conv; }
176#undef X
177 };
178
179} // namespace shamunits
180
393#undef Uget
394#undef addconstant
#define UNITS_CONSTANTS
X macro to list all constants conversion & bindings.
Definition Constants.hpp:44
Defines a unit system.
namespace containing the units library
constexpr T electron_proton_ratio
Mass ration between the electron and proton.
constexpr T pi
Value of pi Usage : auto pi = shamunits::pi<T>;
Definition Constants.hpp:94
constexpr T fine_structure
Fine structure constant.
Definition Constants.hpp:98
constexpr T proton_electron_ratio
Mass ration between the proton and electron.
Physical constant in SI units.
static constexpr T Na
get Na in the si unit system (mol-1 )
static constexpr T planck_length
get the value of a planck_length in the distance unit of the si unit system (m)
static constexpr T k
get k in the si unit system (J.K-1 )
static constexpr T hour
get the value of a hour in the time unit of the si unit system (s)
static constexpr T electron_mass
get the value of a electron_mass in the mass unit of the si unit system (kg)
static constexpr T parsec
get the value of a parsec in the distance unit of the si unit system (m)
static constexpr T c
get c in the si unit system (m.s-1)
static constexpr T Z_0
get the value of Z_0 in the si unit system (Ohm)
static constexpr T kb
get the value of kb in the si unit system (J.K-1)
static constexpr T mu_0
get the value of mu_0 in the si unit system (N.A-2)
static constexpr T ke
get the value of ke in the si unit system (N.m2.C-2)
static constexpr T earth_mass
get the value of a earth_mass in the mass unit of the si unit system (kg)
static constexpr T light_year
get the value of a light_year in the distance unit of the si unit system (m)
static constexpr T solar_radius
get the value of a solar radius in the distance unit of the si unit system (m)
static constexpr T delta_nu_cs
get delta_nu_cs in the si unit system (s-1)
static constexpr T au
get the value of an au in the distance unit of the si unit system (m)
static constexpr T planck_mass
get the value of a planck_mass in the mass unit of the si unit system (kg)
static constexpr T sigma
Stephan Boltzmann constant (W.m-2.K-4)
static constexpr T epsilon_0
get the value of epsilon_0 in the si unit system (F.m-1)
static constexpr T hbar
get the value of hbar in the si unit system (J.s-1)
static constexpr T Kcd
get Kcd in the si unit system (lm.W-1)
static constexpr T proton_mass
get the value of a proton_mass in the mass unit of the si unit system (kg)
static constexpr T G
get the value of G in the si unit system (N.m2.kg-2)
static constexpr T h
get h in the si unit system (J.s-1)
static constexpr T year
get the value of a year in the time unit of the si unit system (s)
static constexpr T sol_mass
get the value of a sol_mass in the mass unit of the si unit system (kg)
static constexpr T jupiter_mass
get the value of a jupiter_mass in the mass unit of the si unit system (kg)
static constexpr T guiness_density
get the value of a guiness_density in the density of the si unit system (kg.m-3)
static constexpr T astronomical_unit
get the value of an astronomical_unit in the distance unit of the si unit system (m)
static constexpr T earth_radius
get the value of one earth equatorial radius in the distance unit of the si unit system (m)
static constexpr T day
get the value of a day in the time unit of the si unit system (s)
static constexpr T e
get e in the si unit system (C)
Physical constants.
Constants(const UnitSystem< T > units)
Construct the shamunits::Constants class with a unit system.
const UnitSystem< T > units
Current unit system of the constants.
static constexpr T pc_to_m
conversion factor from parsecs to meters
static constexpr T dy_to_s
conversion factor from days to seconds
static constexpr T hr_to_s
conversion factor from hours to seconds
static constexpr T ly_to_m
conversion factor from light years to meters
static constexpr T yr_to_s
conversion factor from years to seconds
static constexpr T rsol_to_m
conversion factor from solar radii to meters
static constexpr T rearth_to_m
conversion factor from earth equatorial radii to meters
static constexpr T au_to_m
conversion factor from au to meters
static constexpr T mn_to_s
conversion factor from minutes to seconds