NumProcs and ctypes


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’:

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:

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(“…/”)

np = NumProc(‘fluxerr’, exactq=qex, discreteq=qRT, extensionspace=RT,\
fespace=fs, hdivproduct=hdivipe, errorsquareq=qerrsqr)[/code]
gives “TypeError: ngsolve.comp.NumProc: No constructor defined!”

You would have to export the numproc to python. For some first introduction see Cmake & Python Export — NGS-Py 6.2.2302 documentation

The python export code would look something like this:

  // import ngsolve such that python base classes are defined

  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:

from libDPG import *
np = NumProcFluxError(constructor arguments...)