Debian Codegraph @

python/src/mesh.cpp

raw
1 // Copyright (C) 2017 Chris N. Richardson and Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17
18 #include <memory>
19 #include <pybind11/pybind11.h>
20 #include <pybind11/eigen.h>
21 #include <pybind11/numpy.h>
22 #include <pybind11/stl.h>
23 #include <pybind11/eval.h>
24
25 #include <dolfin/common/Variable.h>
26 #include <dolfin/geometry/BoundingBoxTree.h>
27 #include <dolfin/mesh/BoundaryMesh.h>
28 #include <dolfin/mesh/Mesh.h>
29 #include <dolfin/mesh/MeshColoring.h>
30 #include <dolfin/mesh/MeshData.h>
31 #include <dolfin/mesh/MeshEditor.h>
32 #include <dolfin/mesh/CellType.h>
33 #include <dolfin/mesh/MeshTopology.h>
34 #include <dolfin/mesh/MeshGeometry.h>
35 #include <dolfin/mesh/MeshEntity.h>
36 #include <dolfin/mesh/MeshView.h>
37 #include <dolfin/mesh/Vertex.h>
38 #include <dolfin/mesh/Edge.h>
39 #include <dolfin/mesh/Face.h>
40 #include <dolfin/mesh/Facet.h>
41 #include <dolfin/mesh/Cell.h>
42 #include <dolfin/mesh/MeshEntityIterator.h>
43 #include <dolfin/mesh/MeshFunction.h>
44 #include <dolfin/mesh/MeshPartitioning.h>
45 #include <dolfin/mesh/MeshValueCollection.h>
46 #include <dolfin/mesh/MeshQuality.h>
47 #include <dolfin/mesh/SubDomain.h>
48 #include <dolfin/mesh/SubMesh.h>
49 #include <dolfin/mesh/SubsetIterator.h>
50 #include <dolfin/mesh/DomainBoundary.h>
51 #include <dolfin/mesh/PeriodicBoundaryComputation.h>
52 #include <dolfin/mesh/MeshTransformation.h>
53 #include <dolfin/mesh/MultiMesh.h>
54 #include <dolfin/function/Expression.h>
55
56 #include "casters.h"
57
58 namespace py = pybind11;
59
60 namespace dolfin_wrappers
61 {
62
63 void mesh(py::module& m)
64 {
65 // Make dolfin::SubDomain from pointer
66 m.def("make_dolfin_subdomain",
67 [](std::uintptr_t e)
68 {
69 dolfin::SubDomain *p = reinterpret_cast<dolfin::SubDomain *>(e);
70 return std::shared_ptr<const dolfin::SubDomain>(p);
71 });
72
73 // dolfin::CellType
74 py::class_<dolfin::CellType>
75 celltype(m, "CellType");
76 celltype.def("normal", (dolfin::Point (dolfin::CellType::*)(const dolfin::Cell&, std::size_t) const) &dolfin::CellType::normal);
77
78 // dolfin::CellType enums
79 py::enum_<dolfin::CellType::Type>(celltype, "Type")
80 .value("point", dolfin::CellType::Type::point)
81 .value("interval", dolfin::CellType::Type::interval)
82 .value("triangle", dolfin::CellType::Type::triangle)
83 .value("quadrilateral", dolfin::CellType::Type::quadrilateral)
84 .value("tetrahedron", dolfin::CellType::Type::tetrahedron)
85 .value("hexahedron", dolfin::CellType::Type::hexahedron);
86
87 celltype
88 .def("type2string", &dolfin::CellType::type2string)
89 .def("cell_type", &dolfin::CellType::cell_type)
90 .def("description", &dolfin::CellType::description);
91
92 // dolfin::MeshGeometry class
93 py::class_<dolfin::MeshGeometry, std::shared_ptr<dolfin::MeshGeometry>>
94 (m, "MeshGeometry", "DOLFIN MeshGeometry object")
95 .def("dim", &dolfin::MeshGeometry::dim, "Geometrical dimension")
96 .def("degree", &dolfin::MeshGeometry::degree, "Degree")
97 .def("get_entity_index", &dolfin::MeshGeometry::get_entity_index)
98 .def("num_entity_coordinates", &dolfin::MeshGeometry::num_entity_coordinates);
99
100 // dolfin::MeshView class
101 py::class_<dolfin::MeshView, std::shared_ptr<dolfin::MeshView>>
102 (m, "MeshView", "DOLFIN MeshView object")
103 .def("mesh", &dolfin::MeshView::mesh)
104 .def("create", &dolfin::MeshView::create)
105 .def("cell_map", &dolfin::MeshView::cell_map)
106 .def("vertex_map", &dolfin::MeshView::vertex_map);
107
108 // dolfin::MeshTopology class
109 py::class_<dolfin::MeshTopology, std::shared_ptr<dolfin::MeshTopology>, dolfin::Variable>
110 (m, "MeshTopology", "DOLFIN MeshTopology object")
111 .def("dim", &dolfin::MeshTopology::dim, "Topological dimension")
112 .def("init", (void (dolfin::MeshTopology::*)(std::size_t)) &dolfin::MeshTopology::init)
113 .def("init", (void (dolfin::MeshTopology::*)(std::size_t, std::size_t, std::size_t))
114 &dolfin::MeshTopology::init)
115 .def("clear", (void (dolfin::MeshTopology::*)()) &dolfin::MeshTopology::clear)
116 .def("clear", (void (dolfin::MeshTopology::*)(std::size_t, std::size_t))
117 &dolfin::MeshTopology::clear)
118 .def("__call__", (const dolfin::MeshConnectivity& (dolfin::MeshTopology::*)(std::size_t, std::size_t) const)
119 &dolfin::MeshTopology::operator(), py::return_value_policy::reference_internal)
120 .def("size", &dolfin::MeshTopology::size)
121 .def("hash", &dolfin::MeshTopology::hash)
122 .def("init_global_indices", &dolfin::MeshTopology::init_global_indices)
123 .def("have_global_indices", &dolfin::MeshTopology::have_global_indices)
124 .def("ghost_offset", &dolfin::MeshTopology::ghost_offset)
125 .def("cell_owner", (const std::vector<unsigned int>& (dolfin::MeshTopology::*)() const) &dolfin::MeshTopology::cell_owner)
126 .def("mapping", &dolfin::MeshTopology::mapping)
127 .def("set_global_index", &dolfin::MeshTopology::set_global_index)
128 .def("global_indices", [](const dolfin::MeshTopology& self, int dim)
129 { auto& indices = self.global_indices(dim); return py::array_t<std::int64_t>(indices.size(), indices.data()); })
130 .def("have_shared_entities", &dolfin::MeshTopology::have_shared_entities)
131 .def("shared_entities",
132 (std::map<std::int32_t, std::set<unsigned int> >&(dolfin::MeshTopology::*)(unsigned int))
133 &dolfin::MeshTopology::shared_entities)
134 .def("str", &dolfin::MeshTopology::str);
135
136 // dolfin::Mesh
137 py::class_<dolfin::Mesh, std::shared_ptr<dolfin::Mesh>, dolfin::Variable>
138 (m, "Mesh", py::dynamic_attr(), "DOLFIN Mesh object")
139 .def(py::init<>())
140 .def(py::init<std::string>())
141 .def(py::init<const dolfin::Mesh&>())
142 .def(py::init([](const MPICommWrapper comm)
143 { return std::unique_ptr<dolfin::Mesh>(new dolfin::Mesh(comm.get())); }))
144 .def(py::init([](const MPICommWrapper comm, const std::string filename)
145 { return std::unique_ptr<dolfin::Mesh>(new dolfin::Mesh(comm.get(), filename)); }))
146 .def("bounding_box_tree", &dolfin::Mesh::bounding_box_tree)
147 .def("cells", [](const dolfin::Mesh& self)
148 {
149 const unsigned int tdim = self.topology().dim();
150 return py::array({self.topology().size(tdim), self.type().num_vertices(tdim)},
151 self.topology()(tdim, 0)().data());
152 })
153 .def("cell_orientations", &dolfin::Mesh::cell_orientations)
154 .def("color", (const std::vector<std::size_t>& (dolfin::Mesh::*)(std::string) const)
155 &dolfin::Mesh::color)
156 .def("color", (const std::vector<std::size_t>& (dolfin::Mesh::*)(std::vector<std::size_t>) const)
157 &dolfin::Mesh::color)
158 .def("coordinates", [](dolfin::Mesh& self)
159 {
160 return Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
161 (self.geometry().x().data(),
162 self.geometry().num_points(),
163 self.geometry().dim());
164 },
165 py::return_value_policy::reference_internal)
166 .def("domains", (dolfin::MeshDomains& (dolfin::Mesh::*)())
167 &dolfin::Mesh::domains, py::return_value_policy::reference_internal)
168 .def("data", (dolfin::MeshData& (dolfin::Mesh::*)())
169 &dolfin::Mesh::data, "Data associated with a mesh")
170 .def("geometry", (dolfin::MeshGeometry& (dolfin::Mesh::*)()) &dolfin::Mesh::geometry,
171 py::return_value_policy::reference, "Mesh geometry")
172 .def("hash", &dolfin::Mesh::hash)
173 .def("hmax", &dolfin::Mesh::hmax)
174 .def("hmin", &dolfin::Mesh::hmin)
175 .def("id", &dolfin::Mesh::id)
176 .def("init_global", &dolfin::Mesh::init_global)
177 .def("init", (void (dolfin::Mesh::*)() const) &dolfin::Mesh::init)
178 .def("init", (std::size_t (dolfin::Mesh::*)(std::size_t) const) &dolfin::Mesh::init)
179 .def("init", (void (dolfin::Mesh::*)(std::size_t, std::size_t) const) &dolfin::Mesh::init)
180 .def("init_cell_orientations", &dolfin::Mesh::init_cell_orientations)
181 .def("init_cell_orientations", [](dolfin::Mesh& self, py::object o)
182 {
183 auto _o = o.attr("_cpp_object").cast<dolfin::Expression*>();
184 self.init_cell_orientations(*_o);
185 })
186 .def("mpi_comm", [](dolfin::Mesh& self)
187 { return MPICommWrapper(self.mpi_comm()); })
188 .def("num_entities", &dolfin::Mesh::num_entities,
189 "Number of mesh entities")
190 .def("num_vertices", &dolfin::Mesh::num_vertices, "Number of vertices")
191 .def("num_edges", &dolfin::Mesh::num_edges, "Number of edges")
192 .def("num_faces", &dolfin::Mesh::num_faces, "Number of faces")
193 .def("num_facets", &dolfin::Mesh::num_facets, "Number of facets")
194 .def("num_cells", &dolfin::Mesh::num_cells, "Number of cells")
195 .def("order", &dolfin::Mesh::order)
196 .def("ordered", &dolfin::Mesh::ordered)
197 .def("rmax", &dolfin::Mesh::rmax)
198 .def("rmin", &dolfin::Mesh::rmin)
199 .def("rotate", (void (dolfin::Mesh::*)(double, std::size_t, const dolfin::Point&))
200 &dolfin::Mesh::rotate)
201 .def("rotate", (void (dolfin::Mesh::*)(double, std::size_t)) &dolfin::Mesh::rotate,
202 py::arg("angle"), py::arg("axis")=2)
203 .def("scale", &dolfin::Mesh::scale)
204 .def("num_entities_global", &dolfin::Mesh::num_entities_global)
205 .def("smooth", &dolfin::Mesh::smooth, py::arg("num_iterations")=1)
206 .def("smooth_boundary", &dolfin::Mesh::smooth_boundary)
207 .def("snap_boundary", &dolfin::Mesh::snap_boundary, py::arg("subdomain"),
208 py::arg("harmonic_smoothing")=true)
209 .def("topology", (dolfin::MeshTopology& (dolfin::Mesh::*)())
210 &dolfin::Mesh::topology, "Mesh topology",
211 py::return_value_policy::reference_internal)
212 .def("translate", &dolfin::Mesh::translate)
213 .def("type", (const dolfin::CellType& (dolfin::Mesh::*)() const) &dolfin::Mesh::type,
214 py::return_value_policy::reference)
215 .def("ufl_id", [](const dolfin::Mesh& self){ return self.id(); })
216 .def("cell_name", [](const dolfin::Mesh& self)
217 { return dolfin::CellType::type2string(self.type().cell_type()); })
218 .def("build_mapping", &dolfin::Mesh::build_mapping);
219
220 // dolfin::MeshData
221 py::class_<dolfin::MeshData, std::shared_ptr<dolfin::MeshData>, dolfin::Variable>
222 (m, "MeshData", "Mesh data object")
223 .def("array", [](dolfin::MeshData& self, std::string key, std::size_t i)
224 {
225 const std::vector<std::size_t>& a = self.array(key, i);
226 return py::array_t<std::size_t>(a.size(), a.data());
227 })
228 .def("create_array", &dolfin::MeshData::create_array);
229
230 // dolfin::MeshDomain
231 py::class_<dolfin::MeshDomains, std::shared_ptr<dolfin::MeshDomains>>(m, "MeshDomains", "Mesh domains object")
232 .def("set_marker", &dolfin::MeshDomains::set_marker)
233 .def("get_marker", &dolfin::MeshDomains::get_marker)
234 .def("init", &dolfin::MeshDomains::init)
235 .def("markers", (std::map<std::size_t, std::size_t>& (dolfin::MeshDomains::*)(std::size_t))
236 &dolfin::MeshDomains::markers);
237
238 // dolfin::BoundaryMesh
239 py::class_<dolfin::BoundaryMesh, std::shared_ptr<dolfin::BoundaryMesh>, dolfin::Mesh>
240 (m, "BoundaryMesh", "DOLFIN BoundaryMesh object")
241 .def(py::init<const dolfin::Mesh&, std::string, bool>(),
242 py::arg("mesh"), py::arg("type"), py::arg("order")=true)
243 .def("entity_map", (dolfin::MeshFunction<std::size_t>& (dolfin::BoundaryMesh::*)(std::size_t))
244 &dolfin::BoundaryMesh::entity_map)
245 .def("entity_map", (const dolfin::MeshFunction<std::size_t>& (dolfin::BoundaryMesh::*)(std::size_t) const)
246 &dolfin::BoundaryMesh::entity_map);
247
248 // dolfin::MeshConnectivity class
249 py::class_<dolfin::MeshConnectivity, std::shared_ptr<dolfin::MeshConnectivity>>
250 (m, "MeshConnectivity", "DOLFIN MeshConnectivity object")
251 .def("__call__", [](const dolfin::MeshConnectivity& self, std::size_t i)
252 {
253 return Eigen::Map<const Eigen::Matrix<unsigned int, Eigen::Dynamic, 1>>(self(i), self.size(i));
254 }, py::return_value_policy::reference_internal)
255 .def("__call__", (const std::vector<unsigned int>& (dolfin::MeshConnectivity::*)() const)&dolfin::MeshConnectivity::operator(), py::return_value_policy::reference_internal)
256 .def("size", (std::size_t (dolfin::MeshConnectivity::*)() const)
257 &dolfin::MeshConnectivity::size)
258 .def("size", (std::size_t (dolfin::MeshConnectivity::*)(std::size_t) const)
259 &dolfin::MeshConnectivity::size);
260
261 // dolfin::MeshEntity class
262 py::class_<dolfin::MeshEntity, std::shared_ptr<dolfin::MeshEntity>>
263 (m, "MeshEntity", "DOLFIN MeshEntity object")
264 .def(py::init<const dolfin::Mesh&, std::size_t, std::size_t>())
265 .def("dim", &dolfin::MeshEntity::dim, "Topological dimension")
266 .def("mesh", &dolfin::MeshEntity::mesh, "Associated mesh")
267 .def("index", (std::size_t (dolfin::MeshEntity::*)() const)
268 &dolfin::MeshEntity::index, "Index")
269 .def("global_index", &dolfin::MeshEntity::global_index, "Global index")
270 .def("num_entities", &dolfin::MeshEntity::num_entities,
271 "Number of incident entities of given dimension")
272 .def("num_global_entities", &dolfin::MeshEntity::num_global_entities,
273 "Global number of incident entities of given dimension")
274 .def("entities", [](dolfin::MeshEntity& self, std::size_t dim)
275 {
276 return Eigen::Map<const Eigen::Matrix<unsigned int, Eigen::Dynamic, 1>>
277 (self.entities(dim), self.num_entities(dim));
278 })
279 .def("midpoint", &dolfin::MeshEntity::midpoint, "Midpoint of Entity")
280 .def("sharing_processes", &dolfin::MeshEntity::sharing_processes)
281 .def("is_shared", &dolfin::MeshEntity::is_shared)
282 .def("is_ghost", &dolfin::MeshEntity::is_ghost)
283 .def("__str__", [](dolfin::MeshEntity& self){return self.str(false);});
284
285 // dolfin::Vertex
286 py::class_<dolfin::Vertex, std::shared_ptr<dolfin::Vertex>, dolfin::MeshEntity>
287 (m, "Vertex", "DOLFIN Vertex object")
288 .def(py::init<const dolfin::Mesh&, std::size_t>())
289 .def("point", &dolfin::Vertex::point)
290 .def("x", (double (dolfin::Vertex::*)(std::size_t) const) &dolfin::Vertex::x );
291
292 // dolfin::Edge
293 py::class_<dolfin::Edge, std::shared_ptr<dolfin::Edge>, dolfin::MeshEntity>
294 (m, "Edge", "DOLFIN Edge object")
295 .def(py::init<const dolfin::Mesh&, std::size_t>())
296 .def("dot", &dolfin::Edge::dot)
297 .def("length", &dolfin::Edge::length);
298
299 // dolfin::Face
300 py::class_<dolfin::Face, std::shared_ptr<dolfin::Face>, dolfin::MeshEntity>
301 (m, "Face", "DOLFIN Face object")
302 .def(py::init<const dolfin::Mesh&, std::size_t>())
303 .def("normal", (dolfin::Point (dolfin::Face::*)() const) &dolfin::Face::normal)
304 .def("normal", (double (dolfin::Face::*)(std::size_t) const) &dolfin::Face::normal)
305 .def("area", &dolfin::Face::area);
306
307 // dolfin::Facet
308 py::class_<dolfin::Facet, std::shared_ptr<dolfin::Facet>, dolfin::MeshEntity>
309 (m, "Facet", "DOLFIN Facet object")
310 .def(py::init<const dolfin::Mesh&, std::size_t>())
311 .def("exterior", &dolfin::Facet::exterior)
312 .def("normal", (dolfin::Point (dolfin::Facet::*)() const) &dolfin::Facet::normal)
313 .def("normal", (double (dolfin::Facet::*)(std::size_t) const) &dolfin::Facet::normal);
314 // dolfin::Cell
315 py::class_<dolfin::Cell, std::shared_ptr<dolfin::Cell>, dolfin::MeshEntity>
316 (m, "Cell", "DOLFIN Cell object")
317 .def(py::init<const dolfin::Mesh&, std::size_t>())
318 .def("collides", (bool (dolfin::Cell::*)(const dolfin::Point&) const) &dolfin::Cell::collides)
319 .def("collides", (bool (dolfin::Cell::*)(const dolfin::MeshEntity&) const) &dolfin::Cell::collides)
320 .def("contains", &dolfin::Cell::contains)
321 .def("distance", &dolfin::Cell::distance)
322 .def("cell_normal", &dolfin::Cell::cell_normal)
323 .def("facet_area", &dolfin::Cell::facet_area)
324 .def("h", &dolfin::Cell::h)
325 .def("inradius", &dolfin::Cell::inradius)
326 .def("normal", (dolfin::Point (dolfin::Cell::*)(std::size_t) const) &dolfin::Cell::normal)
327 .def("circumradius", &dolfin::Cell::circumradius)
328 .def("radius_ratio", &dolfin::Cell::radius_ratio)
329 .def("volume", &dolfin::Cell::volume)
330 .def("get_vertex_coordinates", [](const dolfin::Cell& self){
331 std::vector<double> x;
332 self.get_vertex_coordinates(x);
333 return x; }, "Get cell vertex coordinates")
334 .def("get_coordinate_dofs", [](const dolfin::Cell& self){
335 std::vector<double> x;
336 self.get_coordinate_dofs(x);
337 return x; }, "Get the coordinates of the dofs in this cell")
338 .def("get_cell_data", &dolfin::Cell::get_cell_data)
339 .def("get_cell_topology", &dolfin::Cell::get_cell_topology)
340 .def("orientation", (std::size_t (dolfin::Cell::*)() const) &dolfin::Cell::orientation)
341 .def("orientation", (std::size_t (dolfin::Cell::*)(const dolfin::Point&) const) &dolfin::Cell::orientation);
342
343 // dolfin::MeshEntityIterator
344 py::class_<dolfin::MeshEntityIterator, std::shared_ptr<dolfin::MeshEntityIterator>>
345 (m, "MeshEntityIterator", "DOLFIN MeshEntityIterator object")
346 .def(py::init<const dolfin::Mesh&, std::size_t>())
347 .def("__iter__",[](dolfin::MeshEntityIterator& self) { self.operator--(); return self; }) // TODO: check return type and policy
348 .def("__next__",[](dolfin::MeshEntityIterator& self) // TODO: check return type and policy
349 {
350 self.operator++();
351 if (self.end())
352 throw py::stop_iteration("");
353 return *self;
354 });
355
356 // dolfin::SubsetIterator
357 py::class_<dolfin::SubsetIterator>(m, "SubsetIterator")
358 .def(py::init<const dolfin::MeshFunction<std::size_t>&, std::size_t>())
359 .def(py::init<const dolfin::SubsetIterator&>())
360 .def("__iter__",[](dolfin::SubsetIterator& self) { self.operator--(); return self; }) // TODO: check return type and policy
361 .def("__next__",[](dolfin::SubsetIterator& self)
362 {
363 self.operator++();
364 if (self.end())
365 throw py::stop_iteration("");
366 return *self;
367 });
368
369 m.def("entities", [](dolfin::Mesh& mesh, std::size_t dim)
370 { return dolfin::MeshEntityIterator(mesh, dim); });
371 m.def("entities", [](dolfin::MeshEntity& meshentity, std::size_t dim)
372 { return dolfin::MeshEntityIterator(meshentity, dim); });
373
374 #define MESHITERATOR_MACRO(TYPE, NAME) \
375 py::class_<dolfin::MeshEntityIteratorBase<dolfin::TYPE>, \
376 std::shared_ptr<dolfin::MeshEntityIteratorBase<dolfin::TYPE>>> \
377 (m, #TYPE"Iterator", "DOLFIN "#TYPE"Iterator object") \
378 .def(py::init<const dolfin::Mesh&>()) \
379 .def("__iter__",[](dolfin::MeshEntityIteratorBase<dolfin::TYPE>& self) { self.operator--(); return self; }) \
380 .def("__next__",[](dolfin::MeshEntityIteratorBase<dolfin::TYPE>& self) { \
381 self.operator++(); \
382 if (self.end()) \
383 throw py::stop_iteration(""); \
384 return *self; \
385 }); \
386 \
387 m.def(#NAME, [](dolfin::Mesh& mesh, std::string opt) \
388 { return dolfin::MeshEntityIteratorBase<dolfin::TYPE>(mesh, opt); }, \
389 py::arg("mesh"), py::arg("type")="regular"); \
390 m.def(#NAME, [](dolfin::MeshEntity& meshentity)\
391 { return dolfin::MeshEntityIteratorBase<dolfin::TYPE>(meshentity); })
392
393 MESHITERATOR_MACRO(Cell, cells);
394 MESHITERATOR_MACRO(Facet, facets);
395 MESHITERATOR_MACRO(Face, faces);
396 MESHITERATOR_MACRO(Edge, edges);
397 MESHITERATOR_MACRO(Vertex, vertices);
398 #undef MESHITERATOR_MACRO
399
400 // dolfin::MeshFunction
401 #define MESHFUNCTION_MACRO(SCALAR, SCALAR_NAME) \
402 py::class_<dolfin::MeshFunction<SCALAR>, \
403 std::shared_ptr<dolfin::MeshFunction<SCALAR>>, dolfin::Variable> \
404 (m, "MeshFunction"#SCALAR_NAME, "DOLFIN MeshFunction object") \
405 .def(py::init([](std::shared_ptr<const dolfin::Mesh> mesh, std::size_t dim) \
406 { return dolfin::MeshFunction<SCALAR>(mesh, dim, 0); })) \
407 .def(py::init<std::shared_ptr<const dolfin::Mesh>, std::size_t, SCALAR>()) \
408 .def(py::init<std::shared_ptr<const dolfin::Mesh>, std::string>()) \
409 .def(py::init<std::shared_ptr<const dolfin::Mesh>, std::size_t, const dolfin::MeshDomains&>()) \
410 .def(py::init<std::shared_ptr<const dolfin::Mesh>, const dolfin::MeshValueCollection<SCALAR>&>()) \
411 .def("__getitem__", (const SCALAR& (dolfin::MeshFunction<SCALAR>::*) \
412 (std::size_t) const) \
413 &dolfin::MeshFunction<SCALAR>::operator[]) \
414 .def("__setitem__", [](dolfin::MeshFunction<SCALAR>& self, \
415 std::size_t index, SCALAR value) \
416 { self.operator[](index) = value;}) \
417 .def("__getitem__", (const SCALAR& (dolfin::MeshFunction<SCALAR>::*) \
418 (const dolfin::MeshEntity&) const) \
419 &dolfin::MeshFunction<SCALAR>::operator[]) \
420 .def("__setitem__", [](dolfin::MeshFunction<SCALAR>& self, \
421 const dolfin::MeshEntity& index, SCALAR value) \
422 { self.operator[](index) = value;}) \
423 .def("__len__", &dolfin::MeshFunction<SCALAR>::size) \
424 .def("dim", &dolfin::MeshFunction<SCALAR>::dim) \
425 .def("size", &dolfin::MeshFunction<SCALAR>::size) \
426 .def("id", &dolfin::MeshFunction<SCALAR>::id) \
427 .def("ufl_id", &dolfin::MeshFunction<SCALAR>::id) \
428 .def("mesh", &dolfin::MeshFunction<SCALAR>::mesh) \
429 .def("set_values", &dolfin::MeshFunction<SCALAR>::set_values) \
430 .def("set_all", &dolfin::MeshFunction<SCALAR>::set_all) \
431 .def("set_value", (void (dolfin::MeshFunction<SCALAR>::*)(std::size_t, const SCALAR&)) \
432 &dolfin::MeshFunction<SCALAR>::set_value) \
433 .def("set_value", (void (dolfin::MeshFunction<SCALAR>::*)(std::size_t, const SCALAR&, const dolfin::Mesh&)) \
434 &dolfin::MeshFunction<SCALAR>::set_value) \
435 .def("where_equal", &dolfin::MeshFunction<SCALAR>::where_equal) \
436 .def("array", [](dolfin::MeshFunction<SCALAR>& self) \
437 { return Eigen::Map<Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>>(self.values(), self.size()); }, \
438 py::return_value_policy::reference_internal)
439
440 MESHFUNCTION_MACRO(bool, Bool);
441 MESHFUNCTION_MACRO(int, Int);
442 MESHFUNCTION_MACRO(double, Double);
443 MESHFUNCTION_MACRO(std::size_t, Sizet);
444 #undef MESHFUNCTION_MACRO
445
446 // dolfin::MeshValueCollection
447 #define MESHVALUECOLLECTION_MACRO(SCALAR, SCALAR_NAME) \
448 py::class_<dolfin::MeshValueCollection<SCALAR>, \
449 std::shared_ptr<dolfin::MeshValueCollection<SCALAR>>, dolfin::Variable> \
450 (m, "MeshValueCollection_"#SCALAR_NAME, "DOLFIN MeshValueCollection object") \
451 .def(py::init<std::shared_ptr<const dolfin::Mesh>>()) \
452 .def(py::init<std::shared_ptr<const dolfin::Mesh>, std::size_t>()) \
453 .def(py::init<std::shared_ptr<const dolfin::Mesh>, std::string>()) \
454 .def("dim", &dolfin::MeshValueCollection<SCALAR>::dim) \
455 .def("size", &dolfin::MeshValueCollection<SCALAR>::size) \
456 .def("get_value", &dolfin::MeshValueCollection<SCALAR>::get_value) \
457 .def("set_value", (bool (dolfin::MeshValueCollection<SCALAR>::*)(std::size_t, const SCALAR&)) \
458 &dolfin::MeshValueCollection<SCALAR>::set_value) \
459 .def("set_value", (bool (dolfin::MeshValueCollection<SCALAR>::*)(std::size_t, std::size_t, const SCALAR&)) \
460 &dolfin::MeshValueCollection<SCALAR>::set_value) \
461 .def("values", (std::map<std::pair<std::size_t, std::size_t>, SCALAR>& (dolfin::MeshValueCollection<SCALAR>::*)()) \
462 &dolfin::MeshValueCollection<SCALAR>::values, py::return_value_policy::reference) \
463 .def("assign", [](dolfin::MeshValueCollection<SCALAR>& self, const dolfin::MeshFunction<SCALAR>& mf) { self = mf; }) \
464 .def("assign", [](dolfin::MeshValueCollection<SCALAR>& self, const dolfin::MeshValueCollection<SCALAR>& other) \
465 { self = other; })
466
467 MESHVALUECOLLECTION_MACRO(bool, bool);
468 MESHVALUECOLLECTION_MACRO(int, int);
469 MESHVALUECOLLECTION_MACRO(double, double);
470 MESHVALUECOLLECTION_MACRO(std::size_t, sizet);
471 #undef MESHVALUECOLLECTION_MACRO
472
473 // dolfin::MeshEditor
474 py::class_<dolfin::MeshEditor, std::shared_ptr<dolfin::MeshEditor>>
475 (m, "MeshEditor", "DOLFIN MeshEditor object")
476 .def(py::init<>())
477 .def("open", (void (dolfin::MeshEditor::*)(dolfin::Mesh& , std::string, std::size_t, std::size_t, std::size_t))
478 &dolfin::MeshEditor::open,
479 py::arg("mesh"), py::arg("type"), py::arg("tdim"), py::arg("gdim"), py::arg("degree") = 1)
480 .def("init_vertices", &dolfin::MeshEditor::init_vertices)
481 .def("init_cells", &dolfin::MeshEditor::init_cells)
482 .def("init_vertices_global", &dolfin::MeshEditor::init_vertices_global)
483 .def("init_cells_global", &dolfin::MeshEditor::init_cells_global)
484 .def("add_vertex", (void (dolfin::MeshEditor::*)(std::size_t, const dolfin::Point&))
485 &dolfin::MeshEditor::add_vertex)
486 .def("add_vertex", (void (dolfin::MeshEditor::*)(std::size_t, const std::vector<double>&))
487 &dolfin::MeshEditor::add_vertex)
488 .def("add_vertex_global", (void (dolfin::MeshEditor::*)(std::size_t, std::size_t, const dolfin::Point&))
489 &dolfin::MeshEditor::add_vertex_global)
490 .def("add_vertex_global", (void (dolfin::MeshEditor::*)(std::size_t, std::size_t, const std::vector<double>&))
491 &dolfin::MeshEditor::add_vertex_global)
492 .def("add_cell", (void (dolfin::MeshEditor::*)(std::size_t, const std::vector<std::size_t>&))
493 &dolfin::MeshEditor::add_cell)
494 .def("close", &dolfin::MeshEditor::close, py::arg("order") = true);
495
496 // dolfin::MeshQuality
497 py::class_<dolfin::MeshQuality>
498 (m, "MeshQuality", "DOLFIN MeshQuality class")
499 .def_static("aspect_ratio_gamma", &dolfin::MeshQuality::aspect_ratio_gamma)
500 .def_static("radius_ratios", &dolfin::MeshQuality::radius_ratios)
501 .def_static("radius_ratio_histogram_data", &dolfin::MeshQuality::radius_ratio_histogram_data)
502 .def_static("radius_ratio_min_max", &dolfin::MeshQuality::radius_ratio_min_max)
503 .def_static("radius_ratio_matplotlib_histogram", &dolfin::MeshQuality::radius_ratio_matplotlib_histogram,
504 py::arg("mesh"), py::arg("num_bins")=50)
505 .def_static("dihedral_angles_min_max", &dolfin::MeshQuality::dihedral_angles_min_max)
506 .def_static("dihedral_angles_matplotlib_histogram", &dolfin::MeshQuality::dihedral_angles_matplotlib_histogram);
507
508 // dolfin::SubMesh
509 py::class_<dolfin::SubMesh, std::shared_ptr<dolfin::SubMesh>, dolfin::Mesh>
510 (m, "SubMesh", "DOLFIN SubMesh")
511 .def(py::init<const dolfin::Mesh&, std::size_t>())
512 .def(py::init<const dolfin::Mesh&, const dolfin::SubDomain&>())
513 .def(py::init<const dolfin::Mesh&, const dolfin::MeshFunction<std::size_t>&, std::size_t>());
514
515 // dolfin::SubDomain trampoline class for user overloading from
516 // Python
517 class PySubDomain : public dolfin::SubDomain
518 {
519 using dolfin::SubDomain::SubDomain;
520
521 bool inside(Eigen::Ref<const Eigen::VectorXd> x, bool on_boundary) const override
522 { PYBIND11_OVERLOAD(bool, dolfin::SubDomain, inside, x, on_boundary); }
523
524 void map(Eigen::Ref<const Eigen::VectorXd> x, Eigen::Ref<Eigen::VectorXd> y) const override
525 { PYBIND11_OVERLOAD(void, dolfin::SubDomain, map, x, y); }
526
527 void snap(Eigen::Ref<Eigen::VectorXd> x) const override
528 { PYBIND11_OVERLOAD(void, dolfin::SubDomain, snap, x); }
529 };
530
531 // dolfin::SubDomian
532 py::class_<dolfin::SubDomain, std::shared_ptr<dolfin::SubDomain>, PySubDomain>
533 (m, "SubDomain", "DOLFIN SubDomain object")
534 .def(py::init<double>(), py::arg("map_tol")=1.0e-10)
535 .def("inside", (bool (dolfin::SubDomain::*)(Eigen::Ref<const Eigen::VectorXd>, bool) const)
536 &dolfin::SubDomain::inside)
537 .def("map", (void (dolfin::SubDomain::*)(Eigen::Ref<const Eigen::VectorXd>, Eigen::Ref<Eigen::VectorXd>) const)
538 &dolfin::SubDomain::map)
539 .def("snap", (void (dolfin::SubDomain::*)(Eigen::Ref<Eigen::VectorXd>) const)
540 &dolfin::SubDomain::snap)
541 .def("set_property", &dolfin::SubDomain::set_property)
542 .def("get_property", &dolfin::SubDomain::get_property)
543 .def("mark_cells", (void (dolfin::SubDomain::*)(dolfin::Mesh&, std::size_t, bool) const)
544 &dolfin::SubDomain::mark_cells, py::arg("mesh"), py::arg("sub_domain"), py::arg("check_midpoint")=true)
545 .def("mark_facets", (void (dolfin::SubDomain::*)(dolfin::Mesh&, std::size_t, bool) const)
546 &dolfin::SubDomain::mark_facets, py::arg("mesh"), py::arg("sub_domain"), py::arg("check_midpoint")=true)
547 .def("mark", (void (dolfin::SubDomain::*)(dolfin::MeshFunction<std::size_t>&, std::size_t, bool) const)
548 &dolfin::SubDomain::mark, py::arg("meshfunction"), py::arg("marker"), py::arg("check_midpoint")=true)
549 .def("mark", (void (dolfin::SubDomain::*)(dolfin::MeshFunction<double>&, double, bool) const)
550 &dolfin::SubDomain::mark, py::arg("meshfunction"), py::arg("marker"), py::arg("check_midpoint")=true)
551 .def("mark", (void (dolfin::SubDomain::*)(dolfin::MeshFunction<bool>&, bool, bool) const)
552 &dolfin::SubDomain::mark, py::arg("meshfunction"), py::arg("marker"), py::arg("check_midpoint")=true);
553
554 // dolfin::DomainBoundary
555 py::class_<dolfin::DomainBoundary, std::shared_ptr<dolfin::DomainBoundary>, dolfin::SubDomain>
556 (m, "DomainBoundary")
557 .def(py::init<>());
558
559 // dolfin::PeriodicBoundaryComputation
560 py::class_<dolfin::PeriodicBoundaryComputation>
561 (m, "PeriodicBoundaryComputation")
562 .def(py::init<>())
563 .def_static("compute_periodic_pairs", &dolfin::PeriodicBoundaryComputation::compute_periodic_pairs)
564 .def_static("masters_slaves", &dolfin::PeriodicBoundaryComputation::masters_slaves);
565
566 // dolfin::MeshColoring
567 py::class_<dolfin::MeshColoring>(m, "MeshColoring")
568 .def_static("cell_colors", (dolfin::MeshFunction<std::size_t> (*)(std::shared_ptr<const dolfin::Mesh>, std::vector<std::size_t>))
569 &dolfin::MeshColoring::cell_colors)
570 .def_static("color_cells", &dolfin::MeshColoring::color_cells);
571
572 // dolfin::MeshPartitioning
573 py::class_<dolfin::MeshPartitioning>(m, "MeshPartitioning")
574 .def_static("build_distributed_mesh", (void (*)(dolfin::Mesh&)) &dolfin::MeshPartitioning::build_distributed_mesh);
575
576 // dolfin::MeshTransformation
577 py::class_<dolfin::MeshTransformation>(m, "MeshTransformation")
578 .def_static("translate", &dolfin::MeshTransformation::translate)
579 .def_static("rescale", &dolfin::MeshTransformation::rescale)
580 .def_static("scale", &dolfin::MeshTransformation::scale)
581 .def_static("rotate", (void (*)(dolfin::Mesh&, double, std::size_t)) &dolfin::MeshTransformation::rotate)
582 .def_static("rotate", (void (*)(dolfin::Mesh&, double, std::size_t, const dolfin::Point&))
583 &dolfin::MeshTransformation::rotate);
584
585 py::class_<dolfin::MultiMesh, std::shared_ptr<dolfin::MultiMesh>,
586 dolfin::Variable>(m, "MultiMesh", py::dynamic_attr())
587 .def(py::init<>())
588 .def("add", &dolfin::MultiMesh::add)
589 .def("build", &dolfin::MultiMesh::build, py::arg("quadrature_order") = 2)
590 .def("num_parts", &dolfin::MultiMesh::num_parts)
591 .def("compute_volume", &dolfin::MultiMesh::compute_volume)
592 .def("part", &dolfin::MultiMesh::part)
593 .def("cut_cells", &dolfin::MultiMesh::cut_cells)
594 .def("uncut_cells", &dolfin::MultiMesh::uncut_cells)
595 .def("covered_cells", &dolfin::MultiMesh::covered_cells)
596 .def("mark_covered", &dolfin::MultiMesh::mark_covered, "Function that marks a set of cells, given by indicies in a list, as covered.")
597 .def("compute_area", &dolfin::MultiMesh::compute_area)
598 .def("auto_cover", &dolfin::MultiMesh::auto_cover, " Marks all uncut and cut cells connected to the given point as covered.")
599 .def("quadrature_rules_interface",
600 (const std::map<unsigned int, std::vector<dolfin::MultiMesh::quadrature_rule> >&(dolfin::MultiMesh::*)(std::size_t) const)(&dolfin::MultiMesh::quadrature_rules_interface))
601 .def("quadrature_rules_interface",
602 static_cast<const std::vector<dolfin::MultiMesh::quadrature_rule>(dolfin::MultiMesh::*)(std::size_t, unsigned int) const>(&dolfin::MultiMesh::quadrature_rules_interface))
603 .def("quadrature_rules_cut_cells",
604 static_cast<const dolfin::MultiMesh::quadrature_rule(dolfin::MultiMesh::*)(std::size_t, unsigned int) const>(&dolfin::MultiMesh::quadrature_rules_cut_cells));
605 }
606
607
608 }