Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
pySolverGraph.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
21#include "shamcomm/logs.hpp"
26#include <pybind11/complex.h>
27#include <pybind11/pybind11.h>
28#include <vector>
29
30template<class T>
31void register_field(py::module &m, const char *class_name) {
32 using namespace shamrock::solvergraph;
33
34 py::class_<Field<T>, IEdge>(m, class_name)
35 .def(
36 "get_buf",
37 [](Field<T> &self, u64 id_patch) -> sham::DeviceBuffer<T> & {
38 return self.get_buf(id_patch);
39 },
40 py::return_value_policy::reference)
41 .def(
42 "__repr__",
43 [=](Field<T> &self) {
44 return shambase::format(
45 "{}(label={}, tex_symbol={}, nvar={})",
46 class_name,
47 self.get_label(),
48 self.get_tex_symbol(),
49 self.get_nvar());
50 })
51 .def("collect_data", [](Field<T> &self) -> std::vector<T> {
52 std::vector<T> base = {};
53 self.get_refs().for_each([&](u64 id, std::reference_wrapper<PatchDataField<T>> &pdf) {
54 auto copy = pdf.get().get_buf().copy_to_stdvec();
55 base.insert(base.end(), copy.begin(), copy.end());
56 });
57
58 std::vector<T> collected = {};
59 shamalgs::collective::vector_allgatherv(base, collected, MPI_COMM_WORLD);
60 return collected;
61 });
62
63 std::string map_fields_name = []() -> std::string {
64 if (std::is_same_v<T, f64>) {
65 return "map_fields_f64";
66 } else if (std::is_same_v<T, f64_3>) {
67 return "map_fields_f64_3";
68 } else {
70 }
71 }();
72
73 m.def(
74 map_fields_name.c_str(),
75 [](py::function func,
76 py::kwargs kwargs // only Field<T> are allowed
77 ) {
78 for (auto item : kwargs) {
79 if (!py::isinstance<Field<T>>(item.second)) {
80 throw py::type_error(
81 "all keyword arguments to map_fields must be Field objects");
82 }
83 }
84
86
87 for (auto item : kwargs) {
88 auto name = py::cast<std::string>(item.first);
89
90 auto &field = py::cast<Field<T> &>(item.second);
91
92 if (sizes.is_empty()) {
93 sizes = field.get_obj_cnts();
94 } else {
95 field.check_sizes(sizes);
96 }
97 }
98
99 Field<T> result = Field<T>(1, "ret", "ret");
100 result.ensure_sizes(sizes);
101
102 sizes.for_each([&](u64 id, u32 size) {
103 py::dict call_kwargs;
104
105 for (auto item : kwargs) {
106 auto name = py::cast<std::string>(item.first);
107
108 auto &field = py::cast<Field<T> &>(item.second);
109
110 auto vec_data = field.get(id).get_buf().copy_to_stdvec();
111 auto pyarray = shamrock::VecToNumpy<T>::convert(vec_data);
112
113 call_kwargs[name.c_str()] = pyarray;
114 }
115
116 py::tuple args(1);
117 args[0] = size;
118
119 py::object py_result = func(*args, **call_kwargs);
120
121 auto result_data = py_result.cast<std::vector<T>>();
122
123 result.get(id).get_buf().copy_from_stdvec(result_data);
124 });
125
126 return result;
127 });
128}
129
131
132 using namespace shamrock::solvergraph;
133
134 py::class_<IEdge>(root_module, "IEdge")
135 .def("get_label", &IEdge::get_label)
136 .def("get_tex_symbol", &IEdge::get_tex_symbol);
137
138 register_field<f64>(root_module, "Field_f64");
139 register_field<f64_3>(root_module, "Field_f64_3");
140}
constexpr const char * sizes
Temporary sizes for h-iteration.
Header file describing a Node Instance.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
Represents a collection of objects distributed across patches identified by a u64 id.
virtual void ensure_sizes(const shambase::DistributedData< u32 > &sizes)
Ensure that the sizes of the patches in the field match the given sizes (Can resize the underlying fi...
Definition Field.hpp:92
std::vector< int > vector_allgatherv(const std::vector< T > &send_vec, const MPI_Datatype &send_type, std::vector< T > &recv_vec, const MPI_Datatype &recv_type, const MPI_Comm comm)
allgatherv on vector with size query (size querying variant of vector_allgatherv_ks) //TODO add fault...
Definition exchanges.hpp:98
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
std::vector< std::string_view > args
Executable argument list (mapped from argv).
Definition cmdopt.cpp:63
Pybind11 include and definitions.
#define ON_PYTHON_INIT
Register a Python module init function using static initialization.