NumProcs and ctypes

More
7 years 1 month ago #617 by ddrake
NumProcs and ctypes was created by ddrake
Hi,

The DPG library has some useful NumProcs written in C++ and used by pde files. I was wondering if it would be possible to bring those NumProcs into Python in the same way we can bring in spaces and elements using ctypes.CDLL.

For example, we have in 'fluxerr.cpp':
Code:
template<typename SCAL> class NumProcFluxError : public NumProc { ... NumProcFluxError ( shared_ptr<PDE> apde, const Flags & flags) : NumProc(apde) { fes = GetPDE()->GetFESpace(flags.GetStringFlag("fespace",NULL)); ext = GetPDE()->GetFESpace(flags.GetStringFlag("extensionspace",NULL)); hdivip = GetPDE()->GetBilinearForm(flags.GetStringFlag("hdivproduct",NULL)); q = GetPDE()->GetGridFunction(flags.GetStringFlag("discreteq",NULL)); Q = GetPDE()->GetGridFunction(flags.GetStringFlag("exactq",NULL)); err = GetPDE()->GetGridFunction(flags.GetStringFlag("errorsquareq",NULL)); } ... } static RegisterNumProc<NumProcFluxError<double>> npinitfluxerr("fluxerr");

Then in a pde file:
Code:
shared = "../libDPG" ... numproc fluxerr calc_fluxerror_fracnorm # Calculate ||q - Q||. -exactq=qex -discreteq=qRT -extensionspace=RT -fespace=fs -hdivproduct=hdivipe -errorsquareq=qerrsqr

This works, but in Python,
Code:
from ctypes import CDLL from ngsolve import * libDPG = CDLL("../libDPG.so") ... np = NumProc('fluxerr', exactq=qex, discreteq=qRT, extensionspace=RT,\ fespace=fs, hdivproduct=hdivipe, errorsquareq=qerrsqr)
gives "TypeError: ngsolve.comp.NumProc: No constructor defined!"
More
7 years 3 weeks ago #669 by christopher
Replied by christopher on topic NumProcs and ctypes
You would have to export the numproc to python. For some first introduction see ngsolve.org/docu/latest/mylittlengs/1_Basic/pythonExport.html

The python export code would look something like this:
Code:
PYBIND11_MODULE(libDPG,m) { // import ngsolve such that python base classes are defined py::module::import("ngsolve"); py::class_<NumProcFluxError, shared_ptr<NumProcFluxError>, NumProc> (m, "NumProcFluxError") .def(py::init<types of constructor arguments>()) ; }
And you would have to provide a C++ constructor for the numproc with the arguments you want to call it from Python. Then you should be able to import your library and create the numproc like this:
Code:
from libDPG import * np = NumProcFluxError(constructor arguments...)

Best
Christopher
The following user(s) said Thank You: ddrake
Time to create page: 0.101 seconds