Hi guys. The following is a short MWE of a python FEniCS ( I use dolphin 2019.1.0) script I use to interpolate between two different meshes. The idea is to speed up the interpolation especially if the size of function space is huge of the order of 10^6. But I am not able to successfully run it.
The following python code cast’s a dict to a <std::unordered map> in c++ using an argument to a void function (extract_dof_component_map_1). I require it to “pass the argument (dict) by reference” using the standard Pybind 11 module.
It shows a syntax error for this statement because the definition is incorrect in the PYBIND11 module. A successful run of the code should print “hi”.
test.py
from dolfin import *
import cppimport
compiled_cpp_module = cppimport.imp('delta_interpolation')
mesh = UnitCubeMesh(21, 21, 21)
V = VectorFunctionSpace(mesh, 'CG', 2)
dof_component_map = {}
compiled_cpp_module.extract_dof_component_map_1(dof_component_map, V)
cpp file
/*
<%
from dolfin.jit.jit import dolfin_pc
setup_pybind11(cfg)
cfg['include_dirs'] = dolfin_pc['include_dirs']
cfg['library_dirs'] = dolfin_pc['library_dirs']
cfg['compiler_args'] = ['-std=c++11', '-DHAS_MPI']
%>
*/
#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>
#include <pybind11/eigen.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <dolfin/la/GenericVector.h>
#include <dolfin/function/Function.h>
#include <dolfin/function/FunctionSpace.h>
#include <dolfin/fem/GenericDofMap.h>
#include <dolfin/fem/FiniteElement.h>
#include <dolfin/common/RangedIndexSet.h>
#include <dolfin/geometry/BoundingBoxTree.h>
#include <dolfin/mesh/Edge.h>
#include <dolfin/mesh/Mesh.h>
#include <dolfin/mesh/Cell.h>
#include <dolfin/mesh/CellType.h>
#include <dolfin/mesh/MeshEntityIterator.h>
#include <dolfin/mesh/Vertex.h>
#include <dolfin/geometry/Point.h>
#include <dolfin/mesh/MeshEntity.h>
PYBIND11_MAKE_OPAQUE(std::unordered_map<std::size_t, std::size_t>);
using namespace dolfin;
namespace py = pybind11;
void extract_dof_component_map_1(std::unordered_map<std::size_t,
std::size_t>& dof_component_map,
const FunctionSpace& V)
{
std::cout << "hi";
}
PYBIND11_MODULE(delta_interpolation, m)
{
py::bind_map<std::unordered_map<std::size_t, std::size_t>>(m, "unordered_mapping");
m.def("extract_dof_component_map_1", (void (*)(std::unordered_map<std::size_t,
std::size_t>& , const FunctionSpace&))
&extract_dof_component_map_1);
m.def("extract_dof_component_map_1", [](py::dict d, py::object U){
auto _d = d.cast<std::unordered_map<std::size_t, std::size_t>&>();
auto _U = U.attr("_cpp_object").cast<const FunctionSpace&>();
extract_dof_component_map_1(_d, _U);
});
}
Error
Line: compiled_cpp_module.extract_dof_component_map_1(dof_component_map, V)
RuntimeError: Uable to cast Python instance to C++ type (complie in debug mode for details)
Note: I have verified that the definition of FunctionSpace is correct in PYBIND11. Only the syntax for casting dict by reference is incorrect.
I appreciate any help to resolve this syntax error. Thanks in advance.