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
15
16#include "shambackends/sycl.hpp"
20#include <nlohmann/json.hpp>
21#include <pybind11/cast.h>
23#include <shamunits/Names.hpp>
25#include <memory>
26
28 auto &m = root_module;
29
30 using UnitSystem = shamunits::UnitSystem<f64>;
31
32 py::class_<UnitSystem>(m, "UnitSystem")
33 .def(
34 py::init([](f64 unit_time,
35 f64 unit_length,
36 f64 unit_mass,
37 f64 unit_current,
38 f64 unit_temperature,
39 f64 unit_qte,
40 f64 unit_lumint) {
41 return std::make_unique<UnitSystem>(
42 unit_time,
43 unit_length,
44 unit_mass,
45 unit_current,
46 unit_temperature,
47 unit_qte,
48 unit_lumint);
49 }),
50 py::kw_only(),
51 py::arg("unit_time") = 1,
52 py::arg("unit_length") = 1,
53 py::arg("unit_mass") = 1,
54 py::arg("unit_current") = 1,
55 py::arg("unit_temperature") = 1,
56 py::arg("unit_qte") = 1,
57 py::arg("unit_lumint") = 1)
58 .def(
59 "get",
60 [](UnitSystem &self, std::string name, i32 power, std::string pref) {
62
63 return self.runtime_get(pref_, shamunits::units::unit_from_name(name), power);
64 },
65 // py::arg("self"),
66 py::arg("name"),
67 py::arg("power") = 1,
68 py::arg("pref") = "None")
69 .def(
70 "to",
71 [](UnitSystem &self, std::string name, i32 power, std::string pref) {
73
74 return self.runtime_to(pref_, shamunits::units::unit_from_name(name), power);
75 },
76 // py::arg("self"),
77 py::arg("name"),
78 py::arg("power") = 1,
79 py::arg("pref") = "None"
80
81 )
82 .def(
83 "to_json",
84 [](UnitSystem &self) {
85 auto json_loads = py::module_::import("json").attr("loads");
86 nlohmann::json j = self;
87 return json_loads(j.dump());
88 },
89 "Converts the unit system to a json like dictionary")
90 .def(
91 "from_json",
92 [](UnitSystem &self, const py::object &json_data) {
93 auto json_dumps = py::module_::import("json").attr("dumps");
94 std::string s = json_dumps(json_data).cast<std::string>();
95 self = nlohmann::json::parse(s).get<UnitSystem>();
96 },
97 "Sets the unit system from a json like dictionary");
98
99 py::class_<shamunits::Constants<f64>>(m, "Constants")
100 .def(py::init([](UnitSystem s) {
101 return std::make_unique<shamunits::Constants<f64>>(s);
102 }))
105#define X(st, conv) \
106 .def( \
107 #st, \
108 [](shamunits::Constants<f64> &cte, i32 power) { \
109 return sycl::pown(cte.st(), power); \
110 }, \
111 py::arg("power") = 1)
115#undef X
117
118 ;
119}
#define UNITS_CONSTANTS
X macro to list all constants conversion & bindings.
Definition Constants.hpp:44
const UnitName unit_from_name(std::string p)
Get the UnitName enum value from a unit name as a string.
Definition Names.hpp:192
double f64
Alias for double.
std::int32_t i32
32 bit integer
Defines a unit system.
UnitPrefix
Enum of all prefixes.
Definition Names.hpp:90
const UnitPrefix unit_prefix_from_name(std::string p)
Get the UnitPrefix enum value from a prefix name as a string.
Definition Names.hpp:138
Pybind11 include and definitions.
#define ON_PYTHON_INIT
Register a Python module init function using static initialization.
#define X
Temp definition for the X macro call to define the log levels.
Definition logs.hpp:131
Physical constants.