netgen/libsrc/meshing/python_mesh.cpp

745 lines
28 KiB
C++
Raw Normal View History

2014-08-31 19:05:24 +06:00
#ifdef NG_PYTHON
2014-10-08 21:48:48 +06:00
#include <../general/ngpython.hpp>
2014-08-30 06:15:59 +06:00
#include <mystdlib.h>
#include "meshing.hpp"
// #include <csg.hpp>
// #include <geometry2d.hpp>
2016-07-10 21:07:36 +05:00
#include <../interface/writeuser.hpp>
2014-08-30 06:15:59 +06:00
using namespace netgen;
2014-12-19 19:03:36 +05:00
namespace netgen
{
extern bool netgen_executable_started;
2014-12-19 19:03:36 +05:00
extern shared_ptr<NetgenGeometry> ng_geometry;
}
2014-08-30 06:15:59 +06:00
2014-08-31 15:14:18 +06:00
template <typename T, int BASE = 0, typename TIND = int>
2016-11-04 16:14:52 +05:00
void ExportArray (py::module &m)
2014-08-30 06:15:59 +06:00
{
2016-11-15 22:12:51 +05:00
using TA = Array<T,BASE,TIND>;
2014-08-30 06:15:59 +06:00
string name = string("Array_") + typeid(T).name();
2016-11-04 16:14:52 +05:00
py::class_<Array<T,BASE,TIND>>(m, name.c_str())
.def ("__len__", [] ( Array<T,BASE,TIND> &self ) { return self.Size(); } )
2014-08-30 06:15:59 +06:00
.def ("__getitem__",
2014-08-31 15:14:18 +06:00
FunctionPointer ([](Array<T,BASE,TIND> & self, TIND i) -> T&
2014-08-30 06:15:59 +06:00
{
2014-08-31 15:14:18 +06:00
if (i < BASE || i >= BASE+self.Size())
2016-11-04 16:14:52 +05:00
throw py::index_error();
2014-08-30 06:15:59 +06:00
return self[i];
}),
2016-11-04 16:14:52 +05:00
py::return_value_policy::reference)
2016-11-15 22:12:51 +05:00
.def("__iter__", [] ( TA & self) {
return py::make_iterator (self.begin(),self.end());
}, py::keep_alive<0,1>()) // keep array alive while iterator is used
2014-08-31 15:14:18 +06:00
2014-08-30 06:15:59 +06:00
;
}
2015-10-22 20:26:43 +05:00
void TranslateException (const NgException & ex)
{
string err = string("Netgen exception: ")+ex.What();
PyErr_SetString(PyExc_RuntimeError, err.c_str());
}
2014-08-30 06:15:59 +06:00
static Transformation<3> global_trafo(Vec<3> (0,0,0));
2014-08-30 06:15:59 +06:00
2016-11-04 16:14:52 +05:00
DLL_HEADER void ExportNetgenMeshing(py::module &m)
2014-08-30 06:15:59 +06:00
{
m.attr("_netgen_executable_started") = py::cast(netgen::netgen_executable_started);
py::class_<NGDummyArgument>(m, "NGDummyArgument")
.def("__bool__", []( NGDummyArgument &self ) { return false; } )
;
2014-08-30 06:15:59 +06:00
py::class_<Point<2>> (m, "Point2d")
.def(py::init<double,double>())
.def ("__str__", &ToString<Point<2>>)
.def(py::self-py::self)
.def(py::self+Vec<2>())
.def(py::self-Vec<2>())
;
py::class_<Point<3>> (m, "Point3d")
.def(py::init<double,double,double>())
.def ("__str__", &ToString<Point<3>>)
.def(py::self-py::self)
.def(py::self+Vec<3>())
.def(py::self-Vec<3>())
;
m.def ("Pnt", FunctionPointer
([](double x, double y, double z) { return global_trafo(Point<3>(x,y,z)); }));
m.def ("Pnt", FunctionPointer
([](double x, double y) { return Point<2>(x,y); }));
m.def ("Pnt", FunctionPointer
([](double x, double y, double z) { return Point<3>(x,y,z); }));
m.def ("Pnt", FunctionPointer
([](double x, double y) { return Point<2>(x,y); }));
py::class_<Vec<2>> (m, "Vec2d")
.def(py::init<double,double>())
.def ("__str__", &ToString<Vec<3>>)
.def(py::self+py::self)
.def(py::self-py::self)
.def(-py::self)
.def(double()*py::self)
.def("Norm", &Vec<2>::Length)
;
py::class_<Vec<3>> (m, "Vec3d")
.def(py::init<double,double,double>())
.def ("__str__", &ToString<Vec<3>>)
.def(py::self+py::self)
.def(py::self-py::self)
.def(-py::self)
.def(double()*py::self)
.def("Norm", &Vec<3>::Length)
;
m.def ("Vec", FunctionPointer
([] (double x, double y, double z) { return global_trafo(Vec<3>(x,y,z)); }));
m.def ("Vec", FunctionPointer
([] (double x, double y) { return Vec<2>(x,y); }));
m.def ("SetTransformation", FunctionPointer
([](int dir, double angle)
{
if (dir > 0)
global_trafo.SetAxisRotation (dir, angle*M_PI/180);
else
global_trafo = Transformation<3> (Vec<3>(0,0,0));
}),
py::arg("dir")=int(0), py::arg("angle")=int(0));
2015-10-22 20:26:43 +05:00
2016-11-04 16:14:52 +05:00
py::class_<PointIndex>(m, "PointId")
.def(py::init<int>())
2014-08-30 06:15:59 +06:00
.def("__repr__", &ToString<PointIndex>)
.def("__str__", &ToString<PointIndex>)
2016-11-04 16:14:52 +05:00
.def_property_readonly("nr", &PointIndex::operator int)
2015-05-20 13:39:55 +05:00
.def("__eq__" , FunctionPointer( [](PointIndex &self, PointIndex &other)
{ return static_cast<int>(self)==static_cast<int>(other); }) )
.def("__hash__" , FunctionPointer( [](PointIndex &self ) { return static_cast<int>(self); }) )
2014-08-30 06:15:59 +06:00
;
2014-09-26 02:23:31 +06:00
2016-11-04 16:14:52 +05:00
py::class_<ElementIndex>(m, "ElementId3D")
.def(py::init<int>())
2015-05-18 19:19:38 +05:00
.def("__repr__", &ToString<ElementIndex>)
.def("__str__", &ToString<ElementIndex>)
2016-11-04 16:14:52 +05:00
.def_property_readonly("nr", &ElementIndex::operator int)
2015-05-20 13:39:55 +05:00
.def("__eq__" , FunctionPointer( [](ElementIndex &self, ElementIndex &other)
{ return static_cast<int>(self)==static_cast<int>(other); }) )
.def("__hash__" , FunctionPointer( [](ElementIndex &self ) { return static_cast<int>(self); }) )
2015-05-18 19:19:38 +05:00
;
2016-11-04 16:14:52 +05:00
py::class_<SurfaceElementIndex>(m, "ElementId2D")
.def(py::init<int>())
2015-05-18 19:19:38 +05:00
.def("__repr__", &ToString<SurfaceElementIndex>)
.def("__str__", &ToString<SurfaceElementIndex>)
2016-11-04 16:14:52 +05:00
.def_property_readonly("nr", &SurfaceElementIndex::operator int)
2015-05-20 13:39:55 +05:00
.def("__eq__" , FunctionPointer( [](SurfaceElementIndex &self, SurfaceElementIndex &other)
{ return static_cast<int>(self)==static_cast<int>(other); }) )
.def("__hash__" , FunctionPointer( [](SurfaceElementIndex &self ) { return static_cast<int>(self); }) )
2015-05-18 19:19:38 +05:00
;
2016-11-04 16:14:52 +05:00
py::class_<SegmentIndex>(m, "ElementId1D")
.def(py::init<int>())
2015-05-18 19:19:38 +05:00
.def("__repr__", &ToString<SegmentIndex>)
.def("__str__", &ToString<SegmentIndex>)
2016-11-04 16:14:52 +05:00
.def_property_readonly("nr", &SegmentIndex::operator int)
2015-05-20 13:39:55 +05:00
.def("__eq__" , FunctionPointer( [](SegmentIndex &self, SegmentIndex &other)
{ return static_cast<int>(self)==static_cast<int>(other); }) )
.def("__hash__" , FunctionPointer( [](SegmentIndex &self ) { return static_cast<int>(self); }) )
2015-05-18 19:19:38 +05:00
;
2014-09-26 02:23:31 +06:00
/*
2016-11-04 16:14:52 +05:00
py::class_<Point<3>> ("Point")
.def(py::init<double,double,double>())
2014-08-31 15:14:18 +06:00
;
2014-09-26 02:23:31 +06:00
*/
2016-11-04 16:14:52 +05:00
py::class_<MeshPoint /* ,py::bases<Point<3>> */ >(m, "MeshPoint")
.def(py::init<Point<3>>())
2015-05-18 19:19:38 +05:00
.def("__str__", &ToString<MeshPoint>)
.def("__repr__", &ToString<MeshPoint>)
2016-11-04 16:14:52 +05:00
.def_property_readonly("p", FunctionPointer([](const MeshPoint & self)
{
py::list l;
l.append ( py::cast(self[0]) );
l.append ( py::cast(self[1]) );
l.append ( py::cast(self[2]) );
return py::tuple(l);
2014-08-31 15:14:18 +06:00
}))
2016-08-06 15:55:59 +05:00
.def("__getitem__", FunctionPointer([](const MeshPoint & self, int index) {
if(index<0 || index>2)
2016-11-04 16:14:52 +05:00
throw py::index_error();
2016-08-06 15:55:59 +05:00
return self[index];
}))
2014-08-31 15:14:18 +06:00
;
2015-01-20 22:41:16 +05:00
2016-11-04 16:14:52 +05:00
py::class_<Element>(m, "Element3D")
.def("__init__", [](Element *instance, int index, py::list vertices)
2015-05-18 19:19:38 +05:00
{
2016-11-04 16:14:52 +05:00
if (py::len(vertices) == 4)
2015-09-24 03:35:18 +05:00
{
2016-11-04 16:14:52 +05:00
new (instance) Element(TET);
2015-09-24 03:35:18 +05:00
for (int i = 0; i < 4; i++)
2016-11-04 16:14:52 +05:00
(*instance)[i] = py::extract<PointIndex>(vertices[i])();
instance->SetIndex(index);
2016-08-11 20:28:55 +05:00
}
2016-11-24 00:44:52 +05:00
else if (py::len(vertices) == 6)
2016-08-11 20:28:55 +05:00
{
2016-11-04 16:14:52 +05:00
new (instance) Element(PRISM);
2016-08-11 20:28:55 +05:00
for (int i = 0; i < 6; i++)
2016-11-04 16:14:52 +05:00
(*instance)[i] = py::extract<PointIndex>(vertices[i])();
instance->SetIndex(index);
2015-09-24 03:35:18 +05:00
}
2016-11-24 00:44:52 +05:00
else if (py::len(vertices) == 8)
2015-09-24 03:35:18 +05:00
{
2016-11-04 16:14:52 +05:00
new (instance) Element(HEX);
2015-09-24 03:35:18 +05:00
for (int i = 0; i < 8; i++)
2016-11-04 16:14:52 +05:00
(*instance)[i] = py::extract<PointIndex>(vertices[i])();
instance->SetIndex(index);
2015-09-24 03:35:18 +05:00
}
2016-11-24 00:44:52 +05:00
else
throw NgException ("cannot create element");
2016-11-04 16:14:52 +05:00
},
py::arg("index")=1,py::arg("vertices"),
2015-05-18 19:19:38 +05:00
"create volume element"
)
.def("__repr__", &ToString<Element>)
2016-11-04 16:14:52 +05:00
.def_property("index", &Element::GetIndex, &Element::SetIndex)
.def_property_readonly("vertices",
FunctionPointer ([](const Element & self) -> py::list
2014-08-30 06:15:59 +06:00
{
2016-11-04 16:14:52 +05:00
py::list li;
2014-08-30 06:15:59 +06:00
for (int i = 0; i < self.GetNV(); i++)
2016-11-04 16:14:52 +05:00
li.append (py::cast(self[i]));
2014-08-30 06:15:59 +06:00
return li;
}))
;
2014-10-01 19:16:34 +06:00
2016-11-04 16:14:52 +05:00
py::class_<Element2d>(m, "Element2D")
.def("__init__",
[](Element2d *instance, int index, py::list vertices)
2015-05-18 19:19:38 +05:00
{
2016-11-04 16:14:52 +05:00
if (py::len(vertices) == 3)
2015-09-24 03:35:18 +05:00
{
2016-11-04 16:14:52 +05:00
new (instance) Element2d(TRIG);
2015-09-24 03:35:18 +05:00
for (int i = 0; i < 3; i++)
2016-11-04 16:14:52 +05:00
(*instance)[i] = py::extract<PointIndex>(vertices[i])();
instance->SetIndex(index);
2015-09-24 03:35:18 +05:00
}
else
{
2016-11-04 16:14:52 +05:00
new (instance) Element2d(QUAD);
2015-09-24 03:35:18 +05:00
for (int i = 0; i < 4; i++)
2016-11-04 16:14:52 +05:00
(*instance)[i] = py::extract<PointIndex>(vertices[i])();
instance->SetIndex(index);
2015-09-24 03:35:18 +05:00
}
2016-11-04 16:14:52 +05:00
},
py::arg("index")=1,py::arg("vertices"),
2015-05-18 19:19:38 +05:00
"create surface element"
)
2016-11-04 16:14:52 +05:00
.def_property("index", &Element2d::GetIndex, &Element2d::SetIndex)
.def_property_readonly("vertices",
FunctionPointer([](const Element2d & self) -> py::list
2015-01-20 22:41:16 +05:00
{
2016-11-04 16:14:52 +05:00
py::list li;
2015-01-20 22:41:16 +05:00
for (int i = 0; i < self.GetNV(); i++)
2016-11-04 16:14:52 +05:00
li.append(py::cast(self[i]));
2015-01-20 22:41:16 +05:00
return li;
}))
;
2015-05-18 19:19:38 +05:00
2016-11-04 16:14:52 +05:00
py::class_<Segment>(m, "Element1D")
.def("__init__",
2016-11-21 20:35:46 +05:00
[](Segment *instance, py::list vertices, py::list surfaces, int index)
2015-05-18 19:19:38 +05:00
{
2016-11-04 16:14:52 +05:00
new (instance) Segment();
2015-05-18 19:19:38 +05:00
for (int i = 0; i < 2; i++)
2016-11-04 16:14:52 +05:00
(*instance)[i] = py::extract<PointIndex>(vertices[i])();
instance -> si = index;
2016-11-21 20:35:46 +05:00
// needed for codim2 in 3d
instance -> edgenr = index;
2015-08-31 20:41:26 +05:00
if (len(surfaces))
{
2016-11-04 16:14:52 +05:00
instance->surfnr1 = py::extract<int>(surfaces[0])();
instance->surfnr2 = py::extract<int>(surfaces[1])();
2015-08-31 20:41:26 +05:00
}
2016-11-04 16:14:52 +05:00
},
py::arg("vertices"),
py::arg("surfaces")=py::list(),
py::arg("index")=1,
2015-05-18 19:19:38 +05:00
"create segment element"
)
2015-11-11 22:46:18 +05:00
.def("__repr__", &ToString<Segment>)
2016-11-04 16:14:52 +05:00
.def_property_readonly("vertices",
FunctionPointer ([](const Segment & self) -> py::list
2015-05-18 19:19:38 +05:00
{
2016-11-04 16:14:52 +05:00
py::list li;
2015-05-18 19:19:38 +05:00
for (int i = 0; i < 2; i++)
2016-11-04 16:14:52 +05:00
li.append (py::cast(self[i]));
2015-05-18 19:19:38 +05:00
return li;
}))
2016-11-04 16:14:52 +05:00
.def_property_readonly("surfaces",
FunctionPointer ([](const Segment & self) -> py::list
2015-05-18 19:19:38 +05:00
{
2016-11-04 16:14:52 +05:00
py::list li;
li.append (py::cast(self.surfnr1));
li.append (py::cast(self.surfnr2));
2015-05-18 19:19:38 +05:00
return li;
}))
2016-11-05 21:15:16 +05:00
.def_property_readonly("index", FunctionPointer([](const Segment &self) -> size_t
2016-10-28 19:49:50 +05:00
{
2016-11-21 18:54:11 +05:00
return self.si;
2016-10-28 19:49:50 +05:00
}))
2016-11-21 18:54:11 +05:00
.def_property_readonly("edgenr", FunctionPointer([](const Segment & self) -> size_t
{
return self.edgenr;
}))
2015-05-18 19:19:38 +05:00
;
2016-11-04 16:14:52 +05:00
py::class_<Element0d>(m, "Element0D")
.def("__init__",
[](Element0d *instance, PointIndex vertex, int index)
2015-11-11 22:46:18 +05:00
{
2016-11-04 16:14:52 +05:00
new (instance) Element0d;
instance->pnum = vertex;
instance->index = index;
},
py::arg("vertex"),
py::arg("index")=1,
2015-11-11 22:46:18 +05:00
"create point element"
)
.def("__repr__", &ToString<Element0d>)
2016-11-04 16:14:52 +05:00
.def_property_readonly("vertices",
FunctionPointer ([](const Element0d & self) -> py::list
2015-11-11 22:46:18 +05:00
{
2016-11-04 16:14:52 +05:00
py::list li;
li.append (py::cast(self.pnum));
2015-11-11 22:46:18 +05:00
return li;
}))
;
2015-05-18 19:19:38 +05:00
2016-11-04 16:14:52 +05:00
py::class_<FaceDescriptor>(m, "FaceDescriptor")
.def(py::init<const FaceDescriptor&>())
.def("__init__",
[](FaceDescriptor *instance, int surfnr, int domin, int domout, int bc)
2015-08-29 16:54:00 +05:00
{
2016-11-04 16:14:52 +05:00
new (instance) FaceDescriptor();
instance->SetSurfNr(surfnr);
instance->SetDomainIn(domin);
instance->SetDomainOut(domout);
instance->SetBCProperty(bc);
},
py::arg("surfnr")=1,
py::arg("domin")=1,
2016-11-04 18:55:15 +05:00
py::arg("domout")=py::int_(0),
py::arg("bc")=py::int_(0),
2015-08-29 16:54:00 +05:00
"create facedescriptor")
2015-05-18 19:19:38 +05:00
.def("__str__", &ToString<FaceDescriptor>)
.def("__repr__", &ToString<FaceDescriptor>)
2016-11-04 16:14:52 +05:00
.def_property("surfnr", &FaceDescriptor::SurfNr, &FaceDescriptor::SetSurfNr)
.def_property("domin", &FaceDescriptor::DomainIn, &FaceDescriptor::SetDomainIn)
.def_property("domout", &FaceDescriptor::DomainOut, &FaceDescriptor::SetDomainOut)
.def_property("bc", &FaceDescriptor::BCProperty, &FaceDescriptor::SetBCProperty)
2017-02-09 14:06:34 +05:00
.def_property("bcname",
[](FaceDescriptor & self) -> string { return self.GetBCName(); },
[](FaceDescriptor & self, string name) { self.SetBCName(new string(name)); } // memleak
)
2016-11-04 16:14:52 +05:00
.def("SetSurfaceColor", [](FaceDescriptor & self, py::list color )
2016-08-25 20:18:16 +05:00
{
Vec3d c;
2016-11-04 16:14:52 +05:00
c.X() = py::extract<double>(color[0])();
c.Y() = py::extract<double>(color[1])();
c.Z() = py::extract<double>(color[2])();
2016-08-25 20:18:16 +05:00
self.SetSurfColour(c);
2016-11-04 16:14:52 +05:00
})
2015-05-18 19:19:38 +05:00
;
ExportArray<Element,0,size_t>(m);
ExportArray<Element2d,0,size_t>(m);
2016-11-04 16:14:52 +05:00
ExportArray<Segment>(m);
ExportArray<Element0d>(m);
ExportArray<MeshPoint,PointIndex::BASE,PointIndex>(m);
ExportArray<FaceDescriptor>(m);
py::implicitly_convertible< int, PointIndex>();
py::class_<NetgenGeometry, shared_ptr<NetgenGeometry>> (m, "NetgenGeometry")
;
2014-08-30 06:15:59 +06:00
2016-11-04 16:14:52 +05:00
py::class_<Mesh,shared_ptr<Mesh>>(m, "Mesh")
// .def(py::init<>("create empty mesh"))
2015-10-22 19:32:58 +05:00
2016-11-04 16:14:52 +05:00
.def("__init__",
[](Mesh *instance, int dim)
2015-10-22 19:32:58 +05:00
{
2016-11-04 16:14:52 +05:00
new (instance) Mesh();
instance->SetDimension(dim);
},
py::arg("dim")=3
)
2015-10-22 19:32:58 +05:00
2014-08-30 06:15:59 +06:00
.def("__str__", &ToString<Mesh>)
2014-12-19 19:03:36 +05:00
.def("Load", FunctionPointer
([](Mesh & self, const string & filename)
{
2015-08-31 20:41:26 +05:00
istream * infile;
if (filename.find(".vol.gz") != string::npos)
infile = new igzstream (filename.c_str());
else
infile = new ifstream (filename.c_str());
// ifstream input(filename);
2016-02-27 00:35:09 +05:00
#ifdef PARALLEL
// int id;
MPI_Comm_rank(MPI_COMM_WORLD, &id);
MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
if (id == 0)
{
self.Load(*infile);
self.Distribute();
}
else
{
self.SendRecvMesh();
}
#else
2015-08-31 20:41:26 +05:00
self.Load(*infile);
2016-02-27 00:35:09 +05:00
#endif
2014-12-19 19:03:36 +05:00
for (int i = 0; i < geometryregister.Size(); i++)
{
2015-08-31 20:41:26 +05:00
NetgenGeometry * hgeom = geometryregister[i]->LoadFromMeshFile (*infile);
2014-12-19 19:03:36 +05:00
if (hgeom)
{
ng_geometry.reset (hgeom);
break;
}
}
}))
// static_cast<void(Mesh::*)(const string & name)>(&Mesh::Load))
2014-08-30 06:15:59 +06:00
.def("Save", static_cast<void(Mesh::*)(const string & name)const>(&Mesh::Save))
2016-11-04 16:14:52 +05:00
.def("Export",
[] (Mesh & self, string filename, string format)
2016-07-10 21:07:36 +05:00
{
if (WriteUserFormat (format, self, *self.GetGeometry(), filename))
{
string err = string ("nothing known about format")+format;
Array<const char*> names, extensions;
RegisterUserFormats (names, extensions);
err += "\navailable formats are:\n";
for (auto name : names)
err += string("'") + name + "'\n";
throw NgException (err);
}
2016-11-04 16:14:52 +05:00
},
py::arg("filename"), py::arg("format"))
2016-07-10 21:07:36 +05:00
2016-11-04 16:14:52 +05:00
.def_property("dim", &Mesh::GetDimension, &Mesh::SetDimension)
2015-08-31 20:41:26 +05:00
2014-08-30 06:15:59 +06:00
.def("Elements3D",
static_cast<Array<Element,0,size_t>&(Mesh::*)()> (&Mesh::VolumeElements),
2016-11-04 16:14:52 +05:00
py::return_value_policy::reference)
2014-08-31 15:14:18 +06:00
.def("Elements2D",
static_cast<Array<Element2d,0,size_t>&(Mesh::*)()> (&Mesh::SurfaceElements),
2016-11-04 16:14:52 +05:00
py::return_value_policy::reference)
2014-08-31 15:14:18 +06:00
2015-05-18 19:19:38 +05:00
.def("Elements1D",
static_cast<Array<Segment>&(Mesh::*)()> (&Mesh::LineSegments),
2016-11-04 16:14:52 +05:00
py::return_value_policy::reference)
2015-05-18 19:19:38 +05:00
2015-11-11 22:46:18 +05:00
.def("Elements0D", FunctionPointer([] (Mesh & self) -> Array<Element0d>&
{
return self.pointelements;
} ),
2016-11-04 16:14:52 +05:00
py::return_value_policy::reference)
2015-05-18 19:19:38 +05:00
2014-08-31 15:14:18 +06:00
.def("Points",
2014-09-03 15:07:10 +06:00
static_cast<Mesh::T_POINTS&(Mesh::*)()> (&Mesh::Points),
2016-11-04 16:14:52 +05:00
py::return_value_policy::reference)
2014-08-30 06:15:59 +06:00
2015-05-18 19:19:38 +05:00
.def("FaceDescriptor", static_cast<FaceDescriptor&(Mesh::*)(int)> (&Mesh::GetFaceDescriptor),
2016-11-04 16:14:52 +05:00
py::return_value_policy::reference)
2015-12-11 18:31:28 +05:00
.def("GetNFaceDescriptors", &Mesh::GetNFD)
2016-11-18 20:57:42 +05:00
.def("GetNCD2Names", &Mesh::GetNCD2Names)
2015-12-11 18:31:28 +05:00
2014-08-30 06:15:59 +06:00
2014-08-31 15:14:18 +06:00
.def("__getitem__", FunctionPointer ([](const Mesh & self, PointIndex pi)
{
return self[pi];
}))
.def ("Add", FunctionPointer ([](Mesh & self, MeshPoint p)
{
return self.AddPoint (Point3d(p));
}))
2015-05-18 19:19:38 +05:00
.def ("Add", FunctionPointer ([](Mesh & self, const Element & el)
{
return self.AddVolumeElement (el);
}))
.def ("Add", FunctionPointer ([](Mesh & self, const Element2d & el)
{
return self.AddSurfaceElement (el);
}))
.def ("Add", FunctionPointer ([](Mesh & self, const Segment & el)
{
return self.AddSegment (el);
}))
2015-11-11 22:46:18 +05:00
.def ("Add", FunctionPointer ([](Mesh & self, const Element0d & el)
{
return self.pointelements.Append (el);
}))
2015-05-18 19:19:38 +05:00
.def ("Add", FunctionPointer ([](Mesh & self, const FaceDescriptor & fd)
{
return self.AddFaceDescriptor (fd);
}))
2016-12-15 17:05:34 +05:00
.def ("DeleteSurfaceElement",
FunctionPointer ([](Mesh & self, SurfaceElementIndex i)
{
return self.DeleteSurfaceElement (i);
}))
2016-12-14 17:27:01 +05:00
.def ("Compress", FunctionPointer ([](Mesh & self)
2016-12-15 17:05:34 +05:00
{
return self.Compress ();
}))
2015-09-01 13:50:15 +05:00
.def ("SetBCName", &Mesh::SetBCName)
2015-09-01 22:21:52 +05:00
.def ("GetBCName", FunctionPointer([](Mesh & self, int bc)->string
{ return self.GetBCName(bc); }))
.def ("SetMaterial", &Mesh::SetMaterial)
.def ("GetMaterial", FunctionPointer([](Mesh & self, int domnr)
{ return string(self.GetMaterial(domnr)); }))
2016-10-20 16:19:24 +05:00
2016-11-18 20:57:42 +05:00
.def ("GetCD2Name", &Mesh::GetCD2Name)
.def("SetCD2Name", &Mesh::SetCD2Name)
2016-11-16 20:13:17 +05:00
.def ("AddPointIdentification", [](Mesh & self, py::object pindex1, py::object pindex2, int identnr, int type)
{
2016-11-16 20:13:17 +05:00
if(py::extract<PointIndex>(pindex1).check() && py::extract<PointIndex>(pindex2).check())
{
2016-11-16 20:13:17 +05:00
self.GetIdentifications().Add (py::extract<PointIndex>(pindex1)(), py::extract<PointIndex>(pindex2)(), identnr);
self.GetIdentifications().SetType(identnr, Identifications::ID_TYPE(type)); // type = 2 ... periodic
}
2016-11-16 20:13:17 +05:00
},
//py::default_call_policies(),
py::arg("pid1"),
py::arg("pid2"),
py::arg("identnr"),
py::arg("type"))
.def ("GenerateVolumeMesh",
2016-11-04 16:14:52 +05:00
[](Mesh & self, py::object pymp)
2015-08-29 16:54:00 +05:00
{
cout << "generate vol mesh" << endl;
2016-10-20 16:19:24 +05:00
2015-08-29 16:54:00 +05:00
MeshingParameters mp;
2016-11-04 16:14:52 +05:00
if (py::extract<MeshingParameters>(pymp).check())
mp = py::extract<MeshingParameters>(pymp)();
2016-10-20 16:19:24 +05:00
else
{
mp.optsteps3d = 5;
}
2015-08-29 16:54:00 +05:00
MeshVolume (mp, self);
2015-09-02 22:01:49 +05:00
OptimizeVolume (mp, self);
2016-11-04 16:14:52 +05:00
},
py::arg("mp")=NGDummyArgument())
2015-08-29 16:54:00 +05:00
2015-12-11 18:31:28 +05:00
.def ("OptimizeVolumeMesh", FunctionPointer
([](Mesh & self)
{
MeshingParameters mp;
mp.optsteps3d = 5;
OptimizeVolume (mp, self);
}))
2015-08-29 16:54:00 +05:00
.def ("Refine", FunctionPointer
([](Mesh & self)
{
2016-02-26 16:19:10 +05:00
if (self.GetGeometry())
self.GetGeometry()->GetRefinement().Refine(self);
else
Refinement().Refine(self);
2015-08-29 16:54:00 +05:00
}))
2016-05-09 12:48:33 +05:00
.def ("SetGeometry", FunctionPointer
([](Mesh & self, shared_ptr<NetgenGeometry> geo)
2016-05-09 12:48:33 +05:00
{
self.SetGeometry(geo);
}))
/*
.def ("SetGeometry", FunctionPointer
([](Mesh & self, shared_ptr<CSGeometry> geo)
{
self.SetGeometry(geo);
}))
*/
2016-05-06 00:26:33 +05:00
.def ("BuildSearchTree", &Mesh::BuildElementSearchTree)
2014-12-18 19:00:58 +05:00
.def ("BoundaryLayer", FunctionPointer
2016-11-04 16:14:52 +05:00
([](Mesh & self, int bc, py::list thicknesses, int volnr, py::list materials)
{
2016-11-04 16:14:52 +05:00
int n = py::len(thicknesses);
BoundaryLayerParameters blp;
for (int i = 1; i <= self.GetNFD(); i++)
if (self.GetFaceDescriptor(i).BCProperty() == bc)
blp.surfid.Append (i);
cout << "add layer at surfaces: " << blp.surfid << endl;
blp.prismlayers = n;
blp.growthfactor = 1.0;
// find max domain nr
int maxind = 0;
for (ElementIndex ei = 0; ei < self.GetNE(); ei++)
maxind = max (maxind, self[ei].GetIndex());
cout << "maxind = " << maxind << endl;
for ( int i=0; i<n; i++ )
{
2016-11-04 16:14:52 +05:00
blp.heights.Append( py::extract<double>(thicknesses[i])()) ;
blp.new_matnrs.Append( maxind+1+i );
2016-11-04 16:14:52 +05:00
self.SetMaterial (maxind+1+i, py::extract<string>(materials[i])().c_str());
}
blp.bulk_matnr = volnr;
GenerateBoundaryLayer (self, blp);
}
))
.def ("BoundaryLayer", FunctionPointer
2015-01-09 02:18:33 +05:00
([](Mesh & self, int bc, double thickness, int volnr, string material)
2014-12-18 19:00:58 +05:00
{
BoundaryLayerParameters blp;
2014-12-18 21:46:54 +05:00
for (int i = 1; i <= self.GetNFD(); i++)
if (self.GetFaceDescriptor(i).BCProperty() == bc)
blp.surfid.Append (i);
cout << "add layer at surfaces: " << blp.surfid << endl;
2014-12-18 19:00:58 +05:00
blp.prismlayers = 1;
blp.hfirst = thickness;
blp.growthfactor = 1.0;
2014-12-18 21:46:54 +05:00
// find max domain nr
int maxind = 0;
for (ElementIndex ei = 0; ei < self.GetNE(); ei++)
maxind = max (maxind, self[ei].GetIndex());
cout << "maxind = " << maxind << endl;
self.SetMaterial (maxind+1, material.c_str());
2015-01-09 02:18:33 +05:00
blp.new_matnr = maxind+1;
blp.bulk_matnr = volnr;
2014-12-18 19:00:58 +05:00
GenerateBoundaryLayer (self, blp);
}
))
2016-10-28 19:49:50 +05:00
.def ("Scale", FunctionPointer([](Mesh & self, double factor)
{
for(auto i = 0; i<self.GetNP();i++)
self.Point(i).Scale(factor);
}))
2014-12-18 19:00:58 +05:00
2014-08-31 15:14:18 +06:00
;
2014-08-30 06:15:59 +06:00
2016-12-05 18:39:09 +05:00
py::enum_<MESHING_STEP>(m,"MeshingStep")
.value("MESHEDGES",MESHCONST_MESHEDGES)
.value("MESHSURFACE",MESHCONST_OPTSURFACE)
.value("MESHVOLUME",MESHCONST_OPTVOLUME)
;
2014-08-31 15:14:18 +06:00
typedef MeshingParameters MP;
2016-11-04 16:14:52 +05:00
py::class_<MP> (m, "MeshingParameters")
.def(py::init<>())
.def("__init__",
[](MP *instance, double maxh, bool quad_dominated, int optsteps2d, int optsteps3d,
2016-12-05 19:08:25 +05:00
MESHING_STEP perfstepsend, int only3D_domain)
2014-10-05 22:54:16 +06:00
{
2016-11-04 16:14:52 +05:00
new (instance) MeshingParameters;
instance->maxh = maxh;
instance->quad = int(quad_dominated);
instance->optsteps2d = optsteps2d;
instance->optsteps3d = optsteps3d;
instance->only3D_domain_nr = only3D_domain;
2016-12-05 19:08:25 +05:00
instance->perfstepsend = perfstepsend;
2016-11-04 16:14:52 +05:00
},
py::arg("maxh")=1000,
py::arg("quad_dominated")=false,
py::arg("optsteps2d") = 3,
py::arg("optsteps3d") = 3,
2016-12-05 18:39:09 +05:00
py::arg("perfstepsend") = MESHCONST_OPTVOLUME,
2016-12-05 19:37:05 +05:00
py::arg("only3D_domain") = 0,
2014-10-05 22:54:16 +06:00
"create meshing parameters"
2015-11-25 15:25:03 +05:00
)
2014-08-31 15:14:18 +06:00
.def("__str__", &ToString<MP>)
2016-11-04 16:14:52 +05:00
.def_property("maxh",
2014-08-31 15:14:18 +06:00
FunctionPointer ([](const MP & mp ) { return mp.maxh; }),
FunctionPointer ([](MP & mp, double maxh) { return mp.maxh = maxh; }))
.def("RestrictH", FunctionPointer
([](MP & mp, double x, double y, double z, double h)
{
mp.meshsize_points.Append ( MeshingParameters::MeshSizePoint (Point<3> (x,y,z), h));
}),
2016-11-04 16:14:52 +05:00
py::arg("x"), py::arg("y"), py::arg("z"), py::arg("h")
)
2014-08-30 06:15:59 +06:00
;
2014-12-05 21:00:01 +05:00
2016-11-04 16:14:52 +05:00
m.def("SetTestoutFile", FunctionPointer ([] (const string & filename)
2014-12-05 21:00:01 +05:00
{
delete testout;
testout = new ofstream (filename);
}));
2014-12-09 19:58:26 +05:00
2016-11-04 16:14:52 +05:00
m.def("SetMessageImportance", FunctionPointer ([] (int importance)
2014-12-09 19:58:26 +05:00
{
int old = printmessage_importance;
printmessage_importance = importance;
return old;
}));
2014-08-30 06:15:59 +06:00
}
2016-11-04 16:14:52 +05:00
PYBIND11_PLUGIN(libmesh) {
py::module m("mesh", "pybind mesh");
ExportNetgenMeshing(m);
return m.ptr();
2014-08-30 06:15:59 +06:00
}
2014-08-31 19:05:24 +06:00
#endif
2014-08-30 06:15:59 +06:00