netgen/libsrc/meshing/python_mesh.cpp

232 lines
7.4 KiB
C++
Raw Normal View History

2014-08-31 19:05:24 +06:00
#ifdef NG_PYTHON
2014-08-30 06:15:59 +06:00
#include <boost/python.hpp>
2014-08-31 15:14:18 +06:00
#include <boost/python/slice.hpp>
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"
using namespace netgen;
namespace bp = boost::python;
2014-12-19 19:03:36 +05:00
namespace netgen
{
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>
2014-08-30 06:15:59 +06:00
void ExportArray ()
{
string name = string("Array_") + typeid(T).name();
2014-08-31 15:14:18 +06:00
bp::class_<Array<T,BASE,TIND>,boost::noncopyable>(name.c_str())
.def ("__len__", &Array<T,BASE,TIND>::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())
2014-08-30 06:15:59 +06:00
bp::exec("raise IndexError()\n");
return self[i];
}),
bp::return_value_policy<bp::reference_existing_object>())
2014-08-31 15:14:18 +06:00
.def ("__iter__",
bp::range (FunctionPointer([](Array<T,BASE,TIND> & self) { return &self[BASE]; }),
FunctionPointer([](Array<T,BASE,TIND> & self) { return &self[BASE+self.Size()]; })))
2014-08-30 06:15:59 +06:00
;
}
void ExportNetgenMeshing()
{
2014-10-08 21:48:48 +06:00
ModuleScope module("meshing");
2014-08-30 06:15:59 +06:00
2014-08-31 15:14:18 +06:00
bp::class_<PointIndex>("PointId", bp::init<int>())
2014-08-30 06:15:59 +06:00
.def("__repr__", &ToString<PointIndex>)
.def("__str__", &ToString<PointIndex>)
.add_property("nr", &PointIndex::operator int)
;
2014-09-26 02:23:31 +06:00
/*
2014-08-31 15:14:18 +06:00
bp::class_<Point<3>> ("Point")
.def(bp::init<double,double,double>())
;
2014-09-26 02:23:31 +06:00
*/
bp::class_<MeshPoint /* ,bp::bases<Point<3>> */ >("MeshPoint")
// .def(bp::init<Point<3>>())
2014-08-31 15:14:18 +06:00
.add_property("p", FunctionPointer([](const MeshPoint & self)
{
bp::list l;
2014-11-24 21:23:34 +05:00
l.append ( self[0] );
l.append ( self[1] );
l.append ( self[2] );
return bp::tuple(l);
2014-08-31 15:14:18 +06:00
}))
;
2014-08-30 06:15:59 +06:00
bp::class_<Element>("Element3D")
2014-08-31 18:12:31 +06:00
.add_property("index", &Element::GetIndex, &Element::SetIndex)
2014-08-30 06:15:59 +06:00
.add_property("vertices",
FunctionPointer ([](const Element & self) -> bp::list
{
bp::list li;
for (int i = 0; i < self.GetNV(); i++)
li.append (self[i]);
return li;
}))
;
2014-10-01 19:16:34 +06:00
bp::class_<Element2d>("Element2D")
.add_property("index", &Element2d::GetIndex, &Element2d::SetIndex)
.add_property("vertices",
FunctionPointer([](const Element2d & self) -> bp::list
{
bp::list li;
for (int i = 0; i < self.GetNV(); i++)
li.append(self[i]);
return li;
}))
;
2014-08-30 06:15:59 +06:00
ExportArray<Element>();
2014-08-31 15:14:18 +06:00
ExportArray<Element2d>();
ExportArray<MeshPoint,PointIndex::BASE,PointIndex>();
2014-08-30 06:15:59 +06:00
;
2014-12-11 21:06:48 +05:00
bp::class_<Mesh,shared_ptr<Mesh>,boost::noncopyable>("Mesh", bp::no_init)
.def(bp::init<>("create empty mesh"))
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)
{
ifstream input(filename);
self.Load(input);
for (int i = 0; i < geometryregister.Size(); i++)
{
NetgenGeometry * hgeom = geometryregister[i]->LoadFromMeshFile (input);
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))
.def("Elements3D",
2014-09-03 15:07:10 +06:00
static_cast<Array<Element>&(Mesh::*)()> (&Mesh::VolumeElements),
2014-08-30 06:15:59 +06:00
bp::return_value_policy<bp::reference_existing_object>())
2014-08-31 15:14:18 +06:00
.def("Elements2D",
2014-09-03 15:07:10 +06:00
static_cast<Array<Element2d>&(Mesh::*)()> (&Mesh::SurfaceElements),
2014-08-31 15:14:18 +06:00
bp::return_value_policy<bp::reference_existing_object>())
.def("Points",
2014-09-03 15:07:10 +06:00
static_cast<Mesh::T_POINTS&(Mesh::*)()> (&Mesh::Points),
2014-08-30 06:15:59 +06:00
bp::return_value_policy<bp::reference_existing_object>())
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));
}))
2014-12-11 21:06:48 +05:00
/*
2014-12-11 19:53:39 +05:00
.def("__init__", bp::make_constructor
(FunctionPointer ([]()
{
2014-12-11 21:06:48 +05:00
cout << "create new mesh" << endl;
auto tmp = make_shared<Mesh>();
2014-12-11 19:53:39 +05:00
return tmp;
})),
"create empty mesh"
)
2014-12-11 21:06:48 +05:00
*/
2014-12-18 19:00:58 +05:00
.def ("BoundaryLayer", FunctionPointer
([](Mesh & self, int bc, double thickness, string material)
{
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());
blp.matnr = maxind+1;
2014-12-18 19:00:58 +05:00
GenerateBoundaryLayer (self, blp);
}
))
2014-08-31 15:14:18 +06:00
;
2014-08-30 06:15:59 +06:00
2014-08-31 15:14:18 +06:00
typedef MeshingParameters MP;
2014-10-05 22:54:16 +06:00
bp::class_<MP> ("MeshingParameters", bp::init<>())
.def("__init__", bp::make_constructor
(FunctionPointer ([](double maxh)
{
auto tmp = new MeshingParameters;
tmp->maxh = maxh;
return tmp;
}),
bp::default_call_policies(), // need it to use arguments
(bp::arg("maxh")=1000)),
"create meshing parameters"
)
2014-08-31 15:14:18 +06:00
.def("__str__", &ToString<MP>)
.add_property("maxh",
FunctionPointer ([](const MP & mp ) { return mp.maxh; }),
FunctionPointer ([](MP & mp, double maxh) { return mp.maxh = maxh; }))
2014-08-30 06:15:59 +06:00
;
2014-12-05 21:00:01 +05:00
bp::def("SetTestoutFile", FunctionPointer ([] (const string & filename)
{
delete testout;
testout = new ofstream (filename);
}));
2014-12-09 19:58:26 +05:00
bp::def("SetMessageImportance", FunctionPointer ([] (int importance)
{
int old = printmessage_importance;
printmessage_importance = importance;
return old;
}));
2014-08-30 06:15:59 +06:00
}
BOOST_PYTHON_MODULE(libmesh) {
ExportNetgenMeshing();
}
2014-08-31 19:05:24 +06:00
#endif
2014-08-30 06:15:59 +06:00