Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
pyUnits.cpp
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
16#include "shambackends/sycl.hpp"
19#include <pybind11/cast.h>
21#include <shamunits/Names.hpp>
23#include <memory>
24
25Register_pymod(pyunits_init) {
26
27 using UnitSystem = shamunits::UnitSystem<f64>;
28
29 py::class_<UnitSystem>(m, "UnitSystem")
30 .def(
31 py::init([](f64 unit_time,
32 f64 unit_length,
33 f64 unit_mass,
34 f64 unit_current,
35 f64 unit_temperature,
36 f64 unit_qte,
37 f64 unit_lumint) {
38 return std::make_unique<UnitSystem>(
39 unit_time,
40 unit_length,
41 unit_mass,
42 unit_current,
43 unit_temperature,
44 unit_qte,
45 unit_lumint);
46 }),
47 py::kw_only(),
48 py::arg("unit_time") = 1,
49 py::arg("unit_length") = 1,
50 py::arg("unit_mass") = 1,
51 py::arg("unit_current") = 1,
52 py::arg("unit_temperature") = 1,
53 py::arg("unit_qte") = 1,
54 py::arg("unit_lumint") = 1)
55 .def(
56 "get",
57 [](UnitSystem &self, std::string name, i32 power, std::string pref) {
59
60 return self.runtime_get(pref_, shamunits::units::unit_from_name(name), power);
61 },
62 // py::arg("self"),
63 py::arg("name"),
64 py::arg("power") = 1,
65 py::arg("pref") = "None")
66 .def(
67 "to",
68 [](UnitSystem &self, std::string name, i32 power, std::string pref) {
70
71 return self.runtime_to(pref_, shamunits::units::unit_from_name(name), power);
72 },
73 // py::arg("self"),
74 py::arg("name"),
75 py::arg("power") = 1,
76 py::arg("pref") = "None"
77
78 );
79
80 py::class_<shamunits::Constants<f64>>(m, "Constants")
81 .def(py::init([](UnitSystem s) {
82 return std::make_unique<shamunits::Constants<f64>>(s);
83 }))
86#define X(st, conv) \
87 .def( \
88 #st, \
89 [](shamunits::Constants<f64> &cte, i32 power) { \
90 return sycl::pown(cte.st(), power); \
91 }, \
92 py::arg("power") = 1)
96#undef X
98
99 ;
100}
#define UNITS_CONSTANTS
X macro to list all constants conversion & bindings.
Definition Constants.hpp:44
double f64
Alias for double.
std::int32_t i32
32 bit integer
Defines a unit system.
UnitPrefix
Enum of all prefixes.
Definition Names.hpp:89
const UnitPrefix unit_prefix_from_name(std::string p)
Get the UnitPrefix enum value from a prefix name as a string.
Definition Names.hpp:137
Pybind11 include and definitions.
#define Register_pymod(placeholdername)
Register a python module init function using static initialisation.