Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
exemple.cpp
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
15#include <iostream>
16
17// g++ -std=c++20 -Iinclude exemple.cpp
18
19int main(void) {
20
21 using namespace shamunits;
22
23 // create si units
24 constexpr UnitSystem<double> si{};
25
26 // get the value of au^2 in the unit system
27 // but it is quite big :)
28 std::cout << si.get<upow(units::astronomical_unit, 2)>() << std::endl;
29
30 /*
31 * create a unit system with time in Myr, length in au, mass in solar masses
32 */
33 constexpr UnitSystem<double> astro{
37 };
38
39 // this time it returns 1 because the base length is the astronomical unit
40 std::cout << astro.get<upow(units::astronomical_unit, 2)>() << std::endl;
41
42 // in those units G is 3.94781e+13
43 std::cout << astro.get<constants::G>() << std::endl;
44
45 // units compose: one km/s is 210950 au/Myr
46 std::cout << astro.get<prefix::kilo * units::metre / units::second>() << std::endl;
47
48 // a value returned by the code in astro units (here 12 Myr) converted to seconds:
49 // prints 3.78691e+14 s
50 double t = 12;
51 std::cout << t * astro.to<units::second>() << " s" << std::endl;
52
53 // units, prefixes and constants can also be looked up by name at run time:
54 // one parsec is 206266 au
55 std::cout << astro.get(units::from_name("pc")) << std::endl;
56
57 // Constants<T> is kept for backward compatibility: this is astro.get<constants::G>()
58 std::cout << Constants<double>{astro}.get<constants::G>() << std::endl;
59}
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.
Definition Unit.hpp:147
The table of unit prefixes: a prefix is a dimensionless Unit.
constexpr Unit kilo
k
constexpr Unit mega
M.
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 second
s
constexpr Unit metre
m
constexpr Unit year
yr (Julian)
constexpr Unit astronomical_unit
au