Access Function underlying C++ object (in pybind11)

How can I access the underlying C++ object associated with a fenics Function in python? Is this possible?

I am trying to get my Python code using Fenics to work together with a C++ library for hierarchical matrices (hlibpro). The goal is to build a hierarchical matrix representation of a dense weighted convolution operator T defined as
Tf := convolution(phi, pointwise_multiply(w, f))
where the convolution kernel, phi, and weighting function, w, are fenics Functions that are computed in Python.

The hierarchical matrix C++ library must evaluate phi and w at many points. These evaulation points do not lie on gridpoints, and cannot be predicted in advance. Currently I use pybind11 to link the hierarchical matrix c++ code with my python code. When the hierarchical matrix library wants to evaluate phi at a point x, I use a callback in pybind11 to evaluate phi(x) in python. But this is very slow. As I understand things, the fenics Function is just a wrapped C++ object, so I am hoping that I could call the C++ object’s eval() method directly, and skip the intermediate trip through python.

You can simply use

u = Function(V)
cpp_u = u._cpp_object
cpp_u.eval(...)

Ok, I tried this, but I still get some errors. There seems to be a distinction between a
dolfin::Function
which I need, and a
dolfin.cpp.function.Function
which is the type of u._cpp_object. How do I get the dolfin::Function object? Or am I thinking about this wrong? I’m relatively inexperienced in C++, so I may have made a simple mistake.

Here’s a minimal non-working example:

fenics_Function_experiments.cpp:

#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <dolfin/function/Function.h>

void get_fenics_Function(dolfin::Function func) {} // Do nothing

PYBIND11_MODULE(fenics_Function_experiments, m)
{
m.def("get_fenics_Function", &get_fenics_Function);
}

fenics_Function_experiments.py:

from fenics import *
from fenics_Function_experiments import *

mesh = UnitSquareMesh(3,2)
V = FunctionSpace(mesh, 'CG', 1)
u = Function(V)
get_fenics_Function(u._cpp_object)

Compile command I used (compiled with no errors):
>>> g++ -O3 -Wall -shared -std=c++11 -fPIC python3 -m pybind11 --includes fenics_Function_experiments.cpp -o fenics_Function_experimentspython3-config --extension-suffix -I /home/nick/anaconda3/envs/fenics2/include/ -I /home/nick/anaconda3/envs/fenics2/include/eigen3 -L /home/nick/anaconda3/envs/fenics2/lib -shared /home/nick/anaconda3/envs/fenics2/lib/libdolfin.so

The error I get when running the python script:

>>> python fenics_Function_experiments.py
Traceback (most recent call last):
  File "fenics_Function_experiments.py", line 8, in <module>
    get_fenics_Function(u._cpp_object)
TypeError: get_fenics_Function(): incompatible function arguments. The following argument types are supported:
    1. (arg0: dolfin::Function) -> None

Invoked with: <dolfin.cpp.function.Function object at 0x7fcb416c10f0>