Hello, I am currently trying to port a library developed for FEniCSx v0.9 to v0.10/v0.11. The compilation goes smoothly, but when I run the program, I encounter a problem that is reproduced by the MWE below:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.15...3.27)
project(test_nanobind)
if (CMAKE_VERSION VERSION_LESS 3.18)
set(DEV_MODULE Development)
else()
set(DEV_MODULE Development.Module)
endif()
find_package(Python 3.13 COMPONENTS Interpreter ${DEV_MODULE} REQUIRED)
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
# Detect the installed nanobind package and import it into CMake
execute_process(
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_ROOT)
find_package(nanobind CONFIG REQUIRED)
nanobind_add_module(test_mesh test_mesh.cc)
Depending on the micromanba environment, the Python version 3.13 specified above is changed to 3.14.
cmake cmd (3.30.8 cmake version):
#!/bin/bash
INSTALL_DIR=${CONDA_PREFIX}
COMPILER_C=gcc
COMPILER_CXX=g++
CXX_STD_="c++20"
CXX_STD_FLAG="-std=${CXX_STD_}"
EXTRA_FLAGS="-fPIC -I${INSTALL_DIR}/include"
CMAKE_BUILD_TYPE_I=Release
rm -rf build &> /dev/null
mkdir build &> /dev/null
cd build
echo "=============================== configure ====================================="
#-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON \
cmake \
-DCMAKE_PREFIX_PATH="${INSTALL_DIR}/lib/pkgconfig;${INSTALL_DIR}" \
-DCMAKE_C_COMPILER=${COMPILER_C} \
-DCMAKE_CXX_COMPILER=${COMPILER_CXX} \
-DCMAKE_CXX_FLAGS="${CXX_STD_FLAG} ${EXTRA_FLAGS}" \
-DBUILD_SHARED_LIBS=yes \
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE_I} \
..
echo "=============================== compile ======================================="
make VERBOSE=1
warper (test_mesh.cc):
#include <nanobind/nanobind.h>
#include <dolfinx/mesh/Mesh.h>
namespace nb = nanobind;
// In fact, this function is expected to be in my library
int real_test_arg_mesh(const dolfinx::mesh::Mesh<double> & mesh) { return 4; }
// This one is the warper
int test_arg_mesh(const dolfinx::mesh::Mesh<double> & mesh) { return real_test_arg_mesh(mesh); }
NB_MODULE(test_mesh, m) {
nb::module_::import_("dolfinx.cpp.mesh");
m.def("testArgMesh", &test_arg_mesh);
}
python script:
import test_mesh
from mpi4py import MPI
from dolfinx import mesh
domain= mesh.create_interval(MPI.COMM_WORLD,4,[0.0,5.],ghost_mode=mesh.GhostMode.none)
print("==========================")
print(test_mesh)
print("==========================")
print(dir(test_mesh))
print("==========================")
z=test_mesh.testArgMesh(domain._cpp_object)
print("value returned by testArgMesh: ",z)
As shown in the attached image:
the problem is that with the V0.1* versions, dolfinx.cpp.mesh.Mesh_float64 is no longer recognized as dolfinx::mesh::Mesh<double>, as if the Nanobind mapping had disappeared between V0.9 and these versions.
I’m surely missing something important, but I don’t know what. Any help would be appreciated.
Thanks in advance
