Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
pyShamrockCtx.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
20#include "shamcomm/logs.hpp"
21#include "shammath/AABB.hpp"
25#include <pybind11/numpy.h>
26#include <pybind11/pybind11.h>
27#include <variant>
28
29template<class T>
30inline void register_patch_transform_class(py::module &m, std::string name) {
31
32 shamlog_debug_ln("[Py]", "registering", name);
33
35
36 py::class_<Tclass>(m, name.c_str())
37 .def(
38 "to_obj_coord",
39 [](Tclass &self, shamrock::patch::Patch p) -> shammath::AABB<T> {
40 auto tmp = self.to_obj_coord(p);
41 return shammath::AABB<T>(tmp.lower, tmp.upper);
42 })
43 .def("to_obj_coord", [](Tclass &self, shammath::AABB<u64_3> p) -> shammath::AABB<T> {
44 auto tmp = self.to_obj_coord(p);
45 return shammath::AABB<T>(tmp.lower, tmp.upper);
46 });
47}
48
49Register_pymod(pyshamrockctxinit) {
50
51 shamlog_debug_ln("[Py]", "registering shamrock.Patch");
52
53 py::class_<shamrock::patch::Patch>(m, "Patch")
54 .def(py::init<>())
55 .def_readwrite("id_patch", &shamrock::patch::Patch::id_patch)
56 .def_readwrite("pack_node_index", &shamrock::patch::Patch::pack_node_index)
57 .def_readwrite("load_value", &shamrock::patch::Patch::load_value)
58 .def_readwrite("coord_min", &shamrock::patch::Patch::coord_min)
59 .def_readwrite("coord_max", &shamrock::patch::Patch::coord_max)
60 .def_readwrite("node_owner_id", &shamrock::patch::Patch::node_owner_id);
61
62 register_patch_transform_class<f64_3>(m, "PatchCoordTransform_f64_3");
63 register_patch_transform_class<f32_3>(m, "PatchCoordTransform_f32_3");
64 register_patch_transform_class<i64_3>(m, "PatchCoordTransform_i64_3");
65
66 shamlog_debug_ln("[Py]", "registering shamrock.Context");
67
68 py::class_<ShamrockCtx>(m, "Context")
69 .def(py::init<>())
70 .def("pdata_layout_new", &ShamrockCtx::pdata_layout_new)
71 //.def("pdata_layout_do_double_prec_mode", &ShamrockCtx::pdata_layout_do_double_prec_mode)
72 //.def("pdata_layout_do_single_prec_mode", &ShamrockCtx::pdata_layout_do_single_prec_mode)
73 .def("pdata_layout_add_field", &ShamrockCtx::pdata_layout_add_field_t)
74 .def("pdata_layout_print", &ShamrockCtx::pdata_layout_print)
75 .def("init_sched", &ShamrockCtx::init_sched)
76 .def("close_sched", &ShamrockCtx::close_sched)
77 .def("pdata_layout_print", &ShamrockCtx::pdata_layout_print)
78 .def("pdata_layout_print", &ShamrockCtx::pdata_layout_print)
79 .def("pdata_layout_print", &ShamrockCtx::pdata_layout_print)
80 .def("pdata_layout_print", &ShamrockCtx::pdata_layout_print)
81 .def("dump_status", &ShamrockCtx::dump_status)
82 .def(
83 "scheduler_step",
84 [](ShamrockCtx &self, bool do_split_merge, bool do_load_balancing) {
85 self.scheduler_step(do_split_merge, do_load_balancing);
86 },
87 py::arg("do_split_merge") = true,
88 py::arg("do_load_balancing") = true)
89 .def(
90 "set_coord_domain_bound",
91 [](ShamrockCtx &ctx, std::array<f64, 3> min_vals, std::array<f64, 3> max_vals) {
92 ctx.set_coord_domain_bound(
93 {f64_3{min_vals[0], min_vals[1], min_vals[2]},
94 f64_3{max_vals[0], max_vals[1], max_vals[2]}});
95 })
96 .def(
97 "collect_data",
98 [](ShamrockCtx &ctx) {
99 auto data = ctx.allgather_data();
100
101 shamlog_info_ln("PatchScheduler", "collected :", data.size(), "patches");
102
103 py::dict dic_out;
104
105 using namespace shamrock;
106
107 for (auto fname : ctx.pdl->get_field_names()) {
108 append_to_map<f32>(fname, data, dic_out);
109 append_to_map<f32_2>(fname, data, dic_out);
110 append_to_map<f32_3>(fname, data, dic_out);
111 append_to_map<f32_4>(fname, data, dic_out);
112 append_to_map<f32_8>(fname, data, dic_out);
113 append_to_map<f32_16>(fname, data, dic_out);
114 append_to_map<f64>(fname, data, dic_out);
115 append_to_map<f64_2>(fname, data, dic_out);
116 append_to_map<f64_3>(fname, data, dic_out);
117 append_to_map<f64_4>(fname, data, dic_out);
118 append_to_map<f64_8>(fname, data, dic_out);
119 append_to_map<f64_16>(fname, data, dic_out);
120 append_to_map<u32>(fname, data, dic_out);
121 append_to_map<u64>(fname, data, dic_out);
122 append_to_map<u32_3>(fname, data, dic_out);
123 append_to_map<u64_3>(fname, data, dic_out);
124 append_to_map<i64_3>(fname, data, dic_out);
125 }
126
127 return dic_out;
128 })
129 .def("get_patch_list_global", &ShamrockCtx::get_patch_list_global);
130}
Header file for the patch struct and related function.
namespace for the main framework
Definition __init__.py:1
Pybind11 include and definitions.
#define Register_pymod(placeholdername)
Register a python module init function using static initialisation.
Axis-Aligned bounding box.
Definition AABB.hpp:99
Patch object that contain generic patch information.
Definition Patch.hpp:33
u64 pack_node_index
this value mean "to pack with index xxx in the global patch table" and not "to pack with id_pach == x...
Definition Patch.hpp:87
u32 node_owner_id
node rank owner of this patch
Definition Patch.hpp:93
u64 load_value
if synchronized contain the load value of the patch
Definition Patch.hpp:88
u64 id_patch
unique key that identify the patch
Definition Patch.hpp:86