DNDSR 0.2.1
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
Mesh_bind.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "DNDS/Array_bind.hpp"
5#include "Mesh.hpp"
6#include <pybind11/pybind11.h>
7#include <pybind11_json/pybind11_json.hpp>
8#include <pybind11/eigen.h>
9#include <pybind11/functional.h>
10
11namespace DNDS::Geom
12{
14
15 inline void pybind11_ElemInfo_define(py::module_ &m)
16 {
17 auto ElemInfo_ = tPy_ElemInfo(m, "ElemInfo");
18 ElemInfo_
19 .def("getElemType", &ElemInfo::getElemType)
20 .def("setElemType", &ElemInfo::setElemType)
21 .def_readwrite("zone", &ElemInfo::zone);
22 }
23
24 inline void pybind11_ArrayElemInfo_define(py::module_ &m)
25 {
26 pybind11_Array_define<ElemInfo>(m);
27 pybind11_ParArray_define<ElemInfo>(m);
28 pybind11_ArrayTransformer_define<ParArray<ElemInfo>>(m);
29 pybind11_ParArrayPair_define<ElemInfo>(m);
30 }
31
32 inline void pybind11_MeshLocDefine(py::module_ &m)
33 {
34#define DNDS_PY_ENUM_CLASS_MeshLoc_ADD(v) value(#v, MeshLoc::v)
35 py::enum_<MeshLoc>(m, "MeshLoc")
36 .DNDS_PY_ENUM_CLASS_MeshLoc_ADD(Unknown)
37 .DNDS_PY_ENUM_CLASS_MeshLoc_ADD(Node)
38 .DNDS_PY_ENUM_CLASS_MeshLoc_ADD(Face)
39 .DNDS_PY_ENUM_CLASS_MeshLoc_ADD(Cell);
40#undef DNDS_PY_ENUM_CLASS_MeshLoc_ADD
41 }
42
44
45 inline void pybind11_UnstructuredMesh_define(py::module_ &m)
46 {
47#define DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(foo) \
48 def(#foo, &UnstructuredMesh::foo)
49
50#define DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(m_name) \
51 def_readonly(#m_name, &UnstructuredMesh::m_name, py::return_value_policy::reference_internal)
52
53 auto UnstructuredMesh_ = tPy_UnstructuredMesh(m, "UnstructuredMesh");
54 UnstructuredMesh_
55 .def(py::init<>([](const MPIInfo &mpi, int n_dim)
56 { return std::make_shared<UnstructuredMesh>(mpi, n_dim); }))
57 // basic
58 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(coords)
59 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(cell2node)
60 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(bnd2node)
61 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(bnd2cell)
62 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(cell2cell)
63 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(cellElemInfo)
64 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(bndElemInfo)
65 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(cell2cellOrig)
66 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(node2nodeOrig)
67 // interpolated
68 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(cell2face)
69 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(face2cell)
70 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(face2node)
71 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER(faceElemInfo);
72
73 UnstructuredMesh_
74 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(RecoverNode2CellAndNode2Bnd)
75 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(RecoverCell2CellAndBnd2Cell)
76 .def("BuildGhostPrimary", &UnstructuredMesh::BuildGhostPrimary, py::arg("nGhostLayers") = 1)
78 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(AdjLocal2GlobalPrimary)
79 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(AdjGlobal2LocalPrimaryForBnd)
80 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(AdjLocal2GlobalPrimaryForBnd)
81 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(AdjGlobal2LocalFacial)
82 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(AdjLocal2GlobalFacial)
83 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(AdjGlobal2LocalC2F)
84 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(AdjLocal2GlobalC2F)
85 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(BuildGhostN2CB)
86 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(AdjGlobal2LocalN2CB)
87 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(AdjLocal2GlobalN2CB)
88 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(AssertOnN2CB)
89 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(InterpolateFace)
90 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(AssertOnFaces)
91 .def("ConstructBndMesh", [](UnstructuredMesh &self, ssp<UnstructuredMesh> &pbMesh)
92 { self.ConstructBndMesh(*pbMesh); }, py::arg("bndMesh"))
94 // ObtainLocalFactFillOrdering
95 // ObtainSymmetricSymbolicFactorization
96 // .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(ReorderLocalCells) //! it has argument now
97 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(getMPI)
98 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(getDim)
99 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumNode)
100 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumCell)
101 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumFace)
102 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumBnd)
103 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumNodeGhost)
104 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumCellGhost)
105 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumFaceGhost)
106 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumBndGhost)
107 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumNodeProc)
108 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumCellProc)
109 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumFaceProc)
110 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumBndProc)
111 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumNodeGlobal)
112 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumCellGlobal)
113 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumFaceGlobal)
114 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(NumBndGlobal);
115 //!!
116 UnstructuredMesh_
117 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(BuildO2FromO1Elevation)
118 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(BuildBisectO1FormO2)
119 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(IsO1)
120 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(IsO2)
121 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(RecreatePeriodicNodes)
122 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(BuildVTKConnectivity);
123
124 UnstructuredMesh_
125 .DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(getArrayBytes);
126#undef DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_READONLY_MEMBER
127#undef DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC
128
129 UnstructuredMesh_
130 .def("ReorderLocalCells", &UnstructuredMesh::ReorderLocalCells, py::arg("nParts") = 1, py::arg("nPartsInner") = 1)
131 .def("ReadSerialize", &UnstructuredMesh::ReadSerialize, py::arg("serializer"), py::arg("name"))
132 .def("WriteSerialize", &UnstructuredMesh::WriteSerialize, py::arg("serializer"), py::arg("name"))
133 .def(
134 "ReadSerializeAndDistribute",
136 const std::string &name, py::object options_in)
137 {
138 auto options_full = PartitionOptions();
139 auto json_options_full = nlohmann::ordered_json(options_full);
140 if (!options_in.is_none())
141 {
142 auto json_options_in = nlohmann::json(options_in);
143 json_options_full.merge_patch(json_options_in);
144 }
146 serializer, name,
147 json_options_full.template get<PartitionOptions>());
148 },
149 py::arg("serializer"), py::arg("name"), py::arg("partitionOptions") = py::none());
150
151 UnstructuredMesh_.def("SetPeriodicGeometry", &UnstructuredMesh::SetPeriodicGeometry,
152 py::arg("translation1") = Geom::tPoint{1, 0, 0},
153 py::arg("rotationCenter1") = Geom::tPoint{0, 0, 0},
154 py::arg("eulerAngles1") = Geom::tPoint{0, 0, 0},
155 py::arg("translation2") = Geom::tPoint{0, 1, 0},
156 py::arg("rotationCenter2") = Geom::tPoint{0, 0, 0},
157 py::arg("eulerAngles2") = Geom::tPoint{0, 0, 0},
158 py::arg("translation3") = Geom::tPoint{0, 0, 1},
159 py::arg("rotationCenter3") = Geom::tPoint{0, 0, 0},
160 py::arg("eulerAngles3") = Geom::tPoint{0, 0, 0});
161
162 UnstructuredMesh_
163 .def("CellFaceOther", &UnstructuredMesh::CellFaceOther,
164 py::arg("iCell"), py::arg("iFace"))
165 .def("CellIsFaceBack", &UnstructuredMesh::CellIsFaceBack,
166 py::arg("iCell"), py::arg("iFace"));
167
168 auto WallDistOptions_ = py_class_ssp<UnstructuredMesh::WallDistOptions>(UnstructuredMesh_, "WallDistOptions");
169 WallDistOptions_.def(py::init());
170 WallDistOptions_
171 .def_readwrite("method", &UnstructuredMesh::WallDistOptions::method)
172 .def_readwrite("verbose", &UnstructuredMesh::WallDistOptions::verbose)
173 .def_readwrite("wallDistExecution", &UnstructuredMesh::WallDistOptions::wallDistExecution)
174 .def_readwrite("minWallDist", &UnstructuredMesh::WallDistOptions::minWallDist)
175 .def_readwrite("subdivide_quad", &UnstructuredMesh::WallDistOptions::subdivide_quad);
176
177 UnstructuredMesh_
178 .def("BuildNodeWallDist", &UnstructuredMesh::BuildNodeWallDist, py::arg("fBndIsWall"), py::arg("options") = UnstructuredMesh::WallDistOptions{});
179
180 UnstructuredMesh_
181 .def(
182 "TransformCoords",
183 [](py::object self, py::function f)
184 {
185 auto &mesh = self.cast<UnstructuredMesh &>();
186 auto make_view = [&self](ssp<DNDS::ArrayEigenVector<3>> &arr) -> py::array_t<real>
187 {
188 index N = arr->Size();
189 std::vector<py::ssize_t> shape = {py::ssize_t(N), py::ssize_t(3)};
190 std::vector<py::ssize_t> strides = {py::ssize_t(sizeof(real) * 3),
191 py::ssize_t(sizeof(real))};
192 return {shape, strides, arr->data(), self};
193 };
194 if (mesh.coords.father->Size() > 0)
195 f(make_view(mesh.coords.father));
196 if (mesh.coords.son->Size() > 0)
197 f(make_view(mesh.coords.son));
198 },
199 py::arg("func"))
200 .def("to_device", [](UnstructuredMesh &self, const std::string &backend)
201 { self.to_device(device_backend_name_to_enum(backend)); }, py::arg("backend"))
202 .def("to_host", &UnstructuredMesh::to_host);
203 }
204
206
208 {
209 auto UnstructuredMeshSerialRW_ = tPy_UnstructuredMeshSerialRW(m, "UnstructuredMeshSerialRW");
210 UnstructuredMeshSerialRW_
211 .def(py::init(
213 { return std::make_shared<UnstructuredMeshSerialRW>(mesh, mRank); }),
214 py::arg("mesh"), py::arg("mRank") = 0)
215 .def_readwrite("mesh", &UnstructuredMeshSerialRW::mesh)
216 .def_readonly("dataIsSerialOut", &UnstructuredMeshSerialRW::dataIsSerialOut)
217 .def_readonly("dataIsSerialIn", &UnstructuredMeshSerialRW::dataIsSerialIn)
218 .def("ReadFromCGNSSerial", py::overload_cast<const std::string &>(&UnstructuredMeshSerialRW::ReadFromCGNSSerial))
219 .def("Deduplicate1to1Periodic", &UnstructuredMeshSerialRW::Deduplicate1to1Periodic)
220 .def("BuildCell2Cell", &UnstructuredMeshSerialRW::BuildCell2Cell)
221 .def("BuildSerialOut", &UnstructuredMeshSerialRW::BuildSerialOut)
222 .def(
223 "MeshPartitionCell2Cell",
224 [](UnstructuredMeshSerialRW &self, py::object options_in) // use default
225 {
226 auto options_full = UnstructuredMeshSerialRW::PartitionOptions();
227 auto json_options_full = nlohmann::ordered_json(options_full); // warning: using {} here makes it a list
228 if (!options_in.is_none())
229 {
230 auto json_options_in = nlohmann::json(options_in);
231 json_options_full.merge_patch(json_options_in);
232 }
233 // std::cout << json_options_full << std::endl;
234 self.MeshPartitionCell2Cell(json_options_full.template get<UnstructuredMeshSerialRW::PartitionOptions>());
235 },
236 py::arg("options") = py::none())
237 .def("PartitionReorderToMeshCell2Cell", &UnstructuredMeshSerialRW::PartitionReorderToMeshCell2Cell);
238 }
239}
pybind11 bindings for Array / ParArray / ArrayPair.
Shared pybind11 plumbing used by every *_bind.hpp in DNDS (buffer-protocol type check,...
Eigen::Matrix< real, 3, 3 > m
#define DNDS_GEOM_UNSTRUCTURED_MESH_PY_DEF_SIMP_FUNC(foo)
void to_host()
Pull every registered pair back to host memory.
void to_device(DeviceBackend B)
Mirror every registered ArrayPair to the given device backend.
void pybind11_ArrayElemInfo_define(py::module_ &m)
Definition Mesh_bind.hpp:24
Eigen::Vector3d tPoint
Definition Geometric.hpp:9
void pybind11_ElemInfo_define(py::module_ &m)
Definition Mesh_bind.hpp:15
py_class_ssp< ElemInfo > tPy_ElemInfo
Definition Mesh_bind.hpp:13
py_class_ssp< UnstructuredMesh > tPy_UnstructuredMesh
Definition Mesh_bind.hpp:43
void pybind11_UnstructuredMeshSerialRW_define(py::module_ &m)
void pybind11_UnstructuredMesh_define(py::module_ &m)
Definition Mesh_bind.hpp:45
void pybind11_MeshLocDefine(py::module_ &m)
Definition Mesh_bind.hpp:32
py_class_ssp< UnstructuredMeshSerialRW > tPy_UnstructuredMeshSerialRW
ssp< SerializerBase > SerializerBaseSSP
py::classh< T > py_class_ssp
DeviceBackend device_backend_name_to_enum(std::string_view s)
Inverse of device_backend_name. Returns Unknown for unrecognised names.
int64_t index
Global row / DOF index type (signed 64-bit; handles multi-billion-cell meshes).
Definition Defines.hpp:112
std::shared_ptr< T > ssp
Shortened alias for std::shared_ptr used pervasively in DNDSR.
Definition Defines.hpp:143
double real
Canonical floating-point scalar used throughout DNDSR (double precision).
Definition Defines.hpp:110
int MPI_int
MPI counterpart type for MPI_int (= C int). Used for counts and ranks in MPI calls.
Definition MPI.hpp:54
DNDS_DEVICE_CALLABLE Elem::ElemType getElemType() const
t_index zone
positive for BVnum, 0 for internal Elems, Negative for ?
DNDS_DEVICE_CALLABLE void setElemType(Elem::ElemType t)
void MeshPartitionCell2Cell(const PartitionOptions &options)
void ReadFromCGNSSerial(const std::string &fName, const t_FBCName_2_ID &FBCName_2_ID)
reads a cgns file as serial input
void BuildSerialOut()
should be called to build data for serial out
Definition Mesh.cpp:193
DNDS::ssp< UnstructuredMesh > mesh
Definition Mesh.hpp:1117
void BuildCell2Cell()
build cell2cell topology, with node-neighbors included
Geom::PartitionOptions PartitionOptions
Definition Mesh.hpp:1110
void ReorderLocalCells(int nParts=1, int nPartsInner=1)
void BuildGhostPrimary(int nGhostLayers=1)
building ghost (son) from primary (currently only cell2cell)
Definition Mesh.cpp:667
void ReadSerialize(Serializer::SerializerBaseSSP serializerP, const std::string &name)
Definition Mesh.cpp:1467
bool CellIsFaceBack(index iCell, index iFace) const
Definition Mesh.hpp:839
index CellFaceOther(index iCell, index iFace) const
Definition Mesh.hpp:845
void SetPeriodicGeometry(const tPoint &translation1, const tPoint &rotationCenter1, const tPoint &eulerAngles1, const tPoint &translation2, const tPoint &rotationCenter2, const tPoint &eulerAngles2, const tPoint &translation3, const tPoint &rotationCenter3, const tPoint &eulerAngles3)
Definition Mesh.cpp:347
void WriteSerialize(Serializer::SerializerBaseSSP serializerP, const std::string &name)
Definition Mesh.cpp:1429
void ReadSerializeAndDistribute(Serializer::SerializerBaseSSP serializerP, const std::string &name, const PartitionOptions &partitionOptions)
Reads mesh from an H5 serializer using even-split distribution, then repartitions via ParMetis for lo...
void ConstructBndMesh(UnstructuredMesh &bMesh)
Definition Mesh.cpp:1560
void BuildNodeWallDist(const std::function< bool(Geom::t_index)> &fBndIsWall, WallDistOptions options=WallDistOptions{})
Lightweight bundle of an MPI communicator and the calling rank's coordinates.
Definition MPI.hpp:231
constexpr DNDS::index N
tElemInfoArrayPair cellElemInfo
DistributedHex3D mesh
mesh AdjLocal2GlobalPrimary()