26#include <pybind11/complex.h>
27#include <pybind11/pybind11.h>
31void register_field(py::module &m,
const char *class_name) {
32 using namespace shamrock::solvergraph;
34 py::class_<Field<T>,
IEdge>(m, class_name)
38 return self.get_buf(id_patch);
40 py::return_value_policy::reference)
44 return shambase::format(
45 "{}(label={}, tex_symbol={}, nvar={})",
48 self.get_tex_symbol(),
51 .def(
"collect_data", [](
Field<T> &self) -> std::vector<T> {
52 std::vector<T> base = {};
54 auto copy = pdf.get().get_buf().copy_to_stdvec();
55 base.insert(base.end(), copy.begin(), copy.end());
58 std::vector<T> collected = {};
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";
74 map_fields_name.c_str(),
78 for (auto item : kwargs) {
79 if (!py::isinstance<Field<T>>(item.second)) {
81 "all keyword arguments to map_fields must be Field objects");
87 for (
auto item : kwargs) {
88 auto name = py::cast<std::string>(item.first);
90 auto &field = py::cast<Field<T> &>(item.second);
92 if (
sizes.is_empty()) {
93 sizes = field.get_obj_cnts();
95 field.check_sizes(sizes);
103 py::dict call_kwargs;
105 for (
auto item : kwargs) {
106 auto name = py::cast<std::string>(item.first);
108 auto &field = py::cast<Field<T> &>(item.second);
110 auto vec_data = field.get(
id).get_buf().copy_to_stdvec();
111 auto pyarray = shamrock::VecToNumpy<T>::convert(vec_data);
113 call_kwargs[name.c_str()] = pyarray;
119 py::object py_result = func(*args, **call_kwargs);
121 auto result_data = py_result.cast<std::vector<T>>();
123 result.get(
id).get_buf().copy_from_stdvec(result_data);
132 using namespace shamrock::solvergraph;
134 py::class_<IEdge>(root_module,
"IEdge")
135 .def(
"get_label", &IEdge::get_label)
136 .def(
"get_tex_symbol", &IEdge::get_tex_symbol);
138 register_field<f64>(root_module,
"Field_f64");
139 register_field<f64_3>(root_module,
"Field_f64_3");
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...
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...
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).
Pybind11 include and definitions.
#define ON_PYTHON_INIT
Register a Python module init function using static initialization.