Fix `compile_cpp_code` in 2019.1.0

Issue #1104 new
Vladislav Lukoshkin created an issue

Problem: Function object can't be passed to cpp module wrapped with pybind11

The following code raises TypeError: simple(): incompatible function arguments. The following argument types are supported: 1. (u: dolfin::Function) -> float Invoked with: <dolfin.cpp.function.Function object at 0x7f79885703b0> in the latest version of docker image '2019.1.0' (I don’t how to correctly specify it, so I leave the hash of the tag here: ea64e15a739d).

# P1.ufl
# lies in ./cutils/tests/
element = FiniteElement("Lagrange", triangle, 1)

u = Coefficient(element)

M = u*dx
forms = [M]
from dolfin import *

cpp_code = """
#include <dolfin.h>
#include <pybind11/pybind11.h>
#include "P1.h"

using namespace dolfin;
namespace py = pybind11;

double simple(std::shared_ptr<Function> u) {
  auto V = u->function_space();
  P1::Form_M M(V->mesh(), u);
  return assemble(M);
}


PYBIND11_MODULE(SIGNATURE, m) {
    m.def("simple", &simple, py::arg("u"));
}
""" 

module = compile_cpp_code(cpp_code, include_dirs=['cutils/tests'])

mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, 'P', 1)
u = Function(V)

module.simple(u.cpp_object())

This code works fine in 2018.1.0 version.

Comments (1)

  1. Vladislav Lukoshkin reporter

    Forgot to mention, if passing just python Function (not calling .cpp_object method on it), then it raises

    TypeError: simple(): incompatible function arguments. The following argument types are supported:
        1. (u: dolfin::Function) -> float
    
    Invoked with: Coefficient(FunctionSpace(Mesh(VectorElement(FiniteElement('Lagrange', triangle, 1), dim=2), 16), FiniteElement('Lagrange', triangle, 1)), 21)
    

    That is, the changes made by Apr 19 possibly were aimed at getting rid of this extra call to .cpp_object, but maybe, the work wasn’t completed

  2. Log in to comment