Permalink
Browse files

Merge branch '51-update-pybind11' into 'master'

Resolve "Update Pybind11"

Closes #51

See merge request !237
  • Loading branch information...
JSchoeberl committed Sep 21, 2017
2 parents 54021a5 + e05c7df commit 12721c717e01ea9058060c6d989f107e498099a8
@@ -605,10 +605,9 @@ void NGS_DLL_HEADER ExportNgbla(py::module & m) {
});
}
PYBIND11_PLUGIN(libngbla) {
py::module m("bla", "pybind bla");
PYBIND11_MODULE(libngbla, m) {
m.attr("__name__") = "bla";
ExportNgbla(m);
return m.ptr();
}
#endif // NGS_PYTHON
@@ -31,7 +31,7 @@ namespace ngcomp
FESpace::FinalizeUpdate(lh);
}
shared_ptr<Array<int>> GetUsedIdnrs() { return used_idnrs; }
shared_ptr<Array<int>> GetUsedIdnrs() const { return used_idnrs; }
virtual string GetClassName() const override { return "Periodic" + space->GetClassName(); }
shared_ptr<FESpace> GetBaseSpace() const { return space; }
@@ -87,7 +87,7 @@ namespace ngcomp
virtual void Update (LocalHeap & lh) override;
shared_ptr<Array<Complex>> GetFactors() { return factors; }
shared_ptr<Array<Complex>> GetFactors() const { return factors; }
virtual void VTransformMR (ElementId ei, SliceMatrix<double> mat, TRANSFORM_TYPE tt) const override;
virtual void VTransformMC (ElementId ei, SliceMatrix<Complex> mat, TRANSFORM_TYPE tt) const override;

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -470,26 +470,6 @@ struct GenericPow {
void ExportCoefficientFunction(py::module &m)
{
m.def("CreateCoefficientFunction", [] (py::object self, py::object val, py::object dims)
{
auto coef = MakeCoefficient(val);
if(dims)
{
try {
Array<int> cdims = makeCArray<int> (dims);
coef->SetDimensions(cdims);
}
catch (py::type_error){ }
}
return coef;
},
py::arg("self"), py::arg("coef"),py::arg("dims")=DummyArgument(),
"Construct a CoefficientFunction from either one of\n"
" a scalar (float or complex)\n"
" a tuple of scalars and or CFs to define a vector-valued CF\n"
" use dims=(h,w) to define matrix-valued CF\n"
" a list of scalars and or CFs to define a domain-wise CF"
);
py::class_<CoefficientFunction, shared_ptr<CoefficientFunction>>
(m, "CoefficientFunction",
@@ -510,7 +490,26 @@ val : can be one of the following:
Creates a domain-wise CF, use with generator expressions and mesh.GetMaterials()
and mesh.GetBoundaries()
)raw")
.def(py::init([] (py::object val, py::object dims)
{
auto coef = MakeCoefficient(val);
if(dims)
{
try {
Array<int> cdims = makeCArray<int> (dims);
coef->SetDimensions(cdims);
}
catch (py::type_error){ }
}
return coef;
}),
py::arg("coef"),py::arg("dims")=DummyArgument(),
"Construct a CoefficientFunction from either one of\n"
" a scalar (float or complex)\n"
" a tuple of scalars and or CFs to define a vector-valued CF\n"
" use dims=(h,w) to define matrix-valued CF\n"
" a list of scalars and or CFs to define a domain-wise CF"
)
.def("__str__", [](CF& self) { return ToString<>(self);})
.def("__call__", [] (CF& self, BaseMappedIntegrationPoint & mip) -> py::object
@@ -1173,7 +1172,8 @@ void NGS_DLL_HEADER ExportNgfem(py::module &m) {
})
;
m.def("CreateElementTransformation", [] (py::object self_class, ELEMENT_TYPE et, py::list vertices)
py::class_<ElementTransformation, shared_ptr<ElementTransformation>>(m, "ElementTransformation")
.def(py::init([] (ELEMENT_TYPE et, py::list vertices)
-> shared_ptr<ElementTransformation>
{
int nv = ElementTopology::GetNVertices(et);
@@ -1193,10 +1193,8 @@ void NGS_DLL_HEADER ExportNgfem(py::module &m) {
default:
throw Exception ("cannot create ElementTransformation");
}
},
py::arg("self_class"), py::arg("et")=ET_TRIG,py::arg("vertices"));
py::class_<ElementTransformation, shared_ptr<ElementTransformation>>(m, "ElementTransformation")
}),
py::arg("et")=ET_TRIG,py::arg("vertices"))
.def_property_readonly("VB", &ElementTransformation::VB)
.def_property_readonly("spacedim", &ElementTransformation::SpaceDim)
.def_property_readonly("elementid", &ElementTransformation::GetElementId)
@@ -1220,12 +1218,14 @@ void NGS_DLL_HEADER ExportNgfem(py::module &m) {
(m, "DifferentialOperator")
;
m.def("CreateBilinearFormIntegrator", [] (py::object self_class, const string name,
py::object py_coef, int dim, bool imag,
string filename, py::kwargs kwargs)
typedef BilinearFormIntegrator BFI;
auto bfi_class = py::class_<BFI, shared_ptr<BFI>> (m, "BFI");
bfi_class
.def(py::init([bfi_class] (const string name, py::object py_coef, int dim, bool imag,
string filename, py::kwargs kwargs)
-> shared_ptr<BilinearFormIntegrator>
{
auto flags = CreateFlagsFromKwArgs(self_class,kwargs);
auto flags = CreateFlagsFromKwArgs(bfi_class,kwargs);
Array<shared_ptr<CoefficientFunction>> coef = MakeCoefficients(py_coef);
auto bfi = GetIntegrators().CreateBFI (name, dim, coef);
@@ -1240,17 +1240,13 @@ void NGS_DLL_HEADER ExportNgfem(py::module &m) {
bfi -> SetFlags (flags);
if (imag)
bfi = make_shared<ComplexBilinearFormIntegrator> (bfi, Complex(0,1));
self_class.attr("__initialize__")(bfi,**kwargs);
bfi_class.attr("__initialize__")(bfi,**kwargs);
return bfi;
},
py::arg("self_class"), py::arg("name")="",
}),
py::arg("name")="",
py::arg("coef"),py::arg("dim")=-1,
py::arg("imag")=false, py::arg("filename")=""
);
typedef BilinearFormIntegrator BFI;
py::class_<BFI, shared_ptr<BFI>>
(m, "BFI")
)
.def_static("__flags_doc__", [] ()
{
return py::dict
@@ -1344,47 +1340,46 @@ void NGS_DLL_HEADER ExportNgfem(py::module &m) {
py::arg("bfi")=NULL, py::arg("dim")=2, py::arg("comp")=0
);
m.def("CreateLinearFormIntegrator", [] (py::object self_class, string name, int dim,
py::object py_coef,
py::object definedon, bool imag, const Flags & flags,
py::object definedonelem)
{
Array<shared_ptr<CoefficientFunction>> coef = MakeCoefficients(py_coef);
auto lfi = GetIntegrators().CreateLFI (name, dim, coef);
if (!lfi) throw Exception(string("undefined integrator '")+name+
"' in "+ToString(dim)+ " dimension having 1 coefficient");
if(hasattr(definedon,"Mask"))
{
auto vb = py::cast<VorB>(definedon.attr("VB")());
if(vb != lfi->VB())
throw Exception(string("LinearFormIntegrator ") + name + " not defined for " +
(vb==VOL ? "VOL" : (vb==BND ? "BND" : "BBND")));
lfi->SetDefinedOn(py::cast<BitArray>(definedon.attr("Mask")()));
}
if (py::extract<py::list> (definedon).check())
{
Array<int> defon = makeCArray<int> (definedon);
for (int & d : defon) d--;
lfi -> SetDefinedOn (defon);
}
if (! py::extract<DummyArgument> (definedonelem).check())
lfi -> SetDefinedOnElements (py::extract<shared_ptr<BitArray>>(definedonelem)());
if (imag)
lfi = make_shared<ComplexLinearFormIntegrator> (lfi, Complex(0,1));
return lfi;
},
py::arg("self_class"),
py::arg("name")=NULL,py::arg("dim")=-1,
py::arg("coef"),py::arg("definedon")=DummyArgument(),
py::arg("imag")=false, py::arg("flags")=py::dict(),
py::arg("definedonelements")=DummyArgument());
typedef LinearFormIntegrator LFI;
py::class_<LFI, shared_ptr<LFI>>
(m, "LFI")
.def(py::init([] (string name, int dim,
py::object py_coef,
py::object definedon, bool imag, const Flags & flags,
py::object definedonelem)
{
Array<shared_ptr<CoefficientFunction>> coef = MakeCoefficients(py_coef);
auto lfi = GetIntegrators().CreateLFI (name, dim, coef);
if (!lfi) throw Exception(string("undefined integrator '")+name+
"' in "+ToString(dim)+ " dimension having 1 coefficient");
if(hasattr(definedon,"Mask"))
{
auto vb = py::cast<VorB>(definedon.attr("VB")());
if(vb != lfi->VB())
throw Exception(string("LinearFormIntegrator ") + name + " not defined for " +
(vb==VOL ? "VOL" : (vb==BND ? "BND" : "BBND")));
lfi->SetDefinedOn(py::cast<BitArray>(definedon.attr("Mask")()));
}
if (py::extract<py::list> (definedon).check())
{
Array<int> defon = makeCArray<int> (definedon);
for (int & d : defon) d--;
lfi -> SetDefinedOn (defon);
}
if (! py::extract<DummyArgument> (definedonelem).check())
lfi -> SetDefinedOnElements (py::extract<shared_ptr<BitArray>>(definedonelem)());
if (imag)
lfi = make_shared<ComplexLinearFormIntegrator> (lfi, Complex(0,1));
return lfi;
}),
py::arg("name")=NULL,py::arg("dim")=-1,
py::arg("coef"),py::arg("definedon")=DummyArgument(),
py::arg("imag")=false, py::arg("flags")=py::dict(),
py::arg("definedonelements")=DummyArgument())
.def("__str__", [](shared_ptr<LFI> self) { return ToString<LinearFormIntegrator>(*self); } )
// .def("GetDefinedOn", &Integrator::GetDefinedOn)
@@ -1453,10 +1448,9 @@ void NGS_DLL_HEADER ExportNgfem(py::module &m) {
}
PYBIND11_PLUGIN(libngfem) {
py::module m("fem", "pybind fem");
PYBIND11_MODULE(libngfem, m) {
m.attr("__name__") = "fem";
ExportNgfem(m);
return m.ptr();
}
@@ -22,20 +22,24 @@ void NGS_DLL_HEADER ExportNgla(py::module &m) {
py::class_<BaseVector, shared_ptr<BaseVector>>(m, "BaseVector",
py::dynamic_attr() // add dynamic attributes
)
.def("__reduce__", [] (py::object self_object)
{
auto constructor = py::module::import("ngsolve").attr("CreateBaseVector");
auto self = py::cast<shared_ptr<BaseVector>>(self_object);
auto entries = py::list();
if(self->IsComplex())
for(auto entry : self->FV<Complex>())
entries.append(py::cast(entry));
else
for(auto entry : self->FV<double>())
entries.append(py::cast(entry));
auto constructor_args = py::make_tuple(self->Size(),self->IsComplex(),self->EntrySize(),entries,self_object.attr("__dict__"));
return py::make_tuple(constructor, constructor_args);
})
.def(py::pickle([] (const BaseVector& bv)
{
py::list lst;
for(auto val : bv.FVDouble())
lst.append(val);
return py::make_tuple(bv.Size(),bv.IsComplex(),bv.EntrySize(),lst);
},
[] (py::tuple state) -> shared_ptr<BaseVector>
{
auto bv = CreateBaseVector(state[0].cast<size_t>(),
state[1].cast<bool>(),
state[2].cast<size_t>());
auto lst = state[3].cast<py::list>();
for(auto i : Range(py::len(lst)))
bv.FVDouble()[i] = lst[i].cast<double>();
return bv;
}
))
.def("__str__", [](BaseVector &self) { return ToString<BaseVector>(self); } )
.def("__repr__", [](BaseVector &self) { return "basevector"; } )
.def_property_readonly("size", py::cpp_function( [] (BaseVector &self) { return self.Size(); } ) )
@@ -685,10 +689,9 @@ void NGS_DLL_HEADER ExportNgla(py::module &m) {
PYBIND11_PLUGIN(libngla) {
py::module m("la", "pybind la");
PYBIND11_MODULE(libngla, m) {
m.attr("__name__") = "la";
ExportNgla(m);
return m.ptr();
}
@@ -73,7 +73,7 @@ void SetFlag(Flags &flags, string s, py::object value)
}
}
Flags NGS_DLL_HEADER CreateFlagsFromKwArgs(const py::object& pyclass, const py::kwargs& kwargs, py::list info)
Flags NGS_DLL_HEADER CreateFlagsFromKwArgs(py::object pyclass, const py::kwargs& kwargs, py::list info)
{
auto flags_doc = pyclass.attr("__flags_doc__")();
py::dict flags_dict;
@@ -488,10 +488,9 @@ void NGS_DLL_HEADER ExportNgstd(py::module & m) {
}
PYBIND11_PLUGIN(libngstd) {
py::module m("ngstd", "pybind ngstd");
PYBIND11_MODULE(libngstd, m) {
m.attr("__name__") = "ngstd";
ExportNgstd(m);
return m.ptr();
}
@@ -367,7 +367,7 @@ template <> inline void PyExportSymbolTable<shared_ptr<double>, shared_ptr<doubl
}
// Parse python kwargs to flags
Flags NGS_DLL_HEADER CreateFlagsFromKwArgs(const py::object& pyclass, const py::kwargs& kwargs, py::list info = py::list());
Flags NGS_DLL_HEADER CreateFlagsFromKwArgs(py::object pyclass, const py::kwargs& kwargs, py::list info = py::list());
// replace docu links with plain text for help function
const char* docu_string(const char* str);
Oops, something went wrong.

0 comments on commit 12721c7

Please sign in to comment.