DOLFINx 0.9 tutorial example: functionspace(..., ("Lagrange", 1)) raises TypeError

Context

I recently installed the 0.9 stack from source and started going through tutorial examples. Despite matching versions, I kept hitting the same crash at fem.functionspace(...).

My environment:

  • dolfinx==0.9.0

  • basix==0.9.0

  • ffcx==0.9.0

  • ufl==2024.2.0

  • petsc4py built for real scalars

  • venv path looks correct: .../venv/lib/python3.12/site-packages/...

Example that fails

from mpi4py import MPI
import numpy as np

import ufl
from dolfinx import fem, io, mesh, plot
from dolfinx.fem.petsc import LinearProblem
from ufl import ds, dx, grad, inner

msh = mesh.create_rectangle(
    comm=MPI.COMM_WORLD,
    points=((0.0, 0.0), (2.0, 1.0)),
    n=(32, 16),
    cell_type=mesh.CellType.triangle,
)
V = fem.functionspace(msh, ("Lagrange", 1))

Error output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[13], line 15
      7 from ufl import ds, dx, grad, inner
      9 msh = mesh.create_rectangle(
     10     comm=MPI.COMM_WORLD,
     11     points=((0.0, 0.0), (2.0, 1.0)),
     12     n=(32, 16),
     13     cell_type=mesh.CellType.triangle,
     14 )
---> 15 V = fem.functionspace(msh, ("Lagrange", 1))

File ~/opt/install/fenicsx/venv/lib/python3.12/site-packages/dolfinx/fem/function.py:628, in functionspace(mesh, element, form_compiler_options, jit_options)
    625     form_compiler_options = dict()
    626 form_compiler_options["scalar_type"] = dtype
--> 628 cpp_element = _create_dolfinx_element(mesh.comm, mesh.topology.cell_type, ufl_e, dtype)
    630 cpp_dofmap = _cpp.fem.create_dofmap(mesh.comm, mesh.topology._cpp_object, cpp_element)
    632 assert np.issubdtype(
    633     mesh.geometry.x.dtype, cpp_element.dtype
    634 ), "Mesh and element dtype are not compatible."

File ~/opt/install/fenicsx/venv/lib/python3.12/site-packages/dolfinx/fem/function.py:586, in _create_dolfinx_element(comm, cell_type, ufl_e, dtype)
    584 else:
    585     basix_e = ufl_e.basix_element._e
--> 586     return CppElement(basix_e, ufl_e.block_size, ufl_e.is_symmetric)

TypeError: __init__(): incompatible function arguments. The following argument types are supported:
    1. __init__(self, element: basix::FiniteElement<double>, block_size: int, symmetric: bool) -> None
    2. __init__(self, elements: collections.abc.Sequence[dolfinx.cpp.fem.FiniteElement_float64]) -> None
    3. __init__(self, cell_type: dolfinx.cpp.mesh.CellType, points: numpy.ndarray[dtype=float64, shape=(*, *)], block_size: int, symmetry: bool) -> None

Invoked with types: dolfinx.cpp.fem.FiniteElement_float64, basix._basixcpp.FiniteElement_float64, int, bool

Thank you.

Did you build then at the same time, and ensured that the nanobind version for building basix and dolfinx were the same?

Usually, this indicates that basix have been built with one version of nanobind, while dolfinx with another.

Could you attach the commands you used to install from source?

1 Like

Thank you! The problem was indeed with nanobind visibility. I had asked ChatGPT to help me rebuild with the correct arguments, and that resolved it. It pinned nanobind 2.9.2 in my venv and rebuilt Basix and DOLFINx with --no-build-isolation, explicitly setting nanobind_DIR. I also exported PETSC_DIR, PETSC_ARCH, and SLEPC_DIR so pkg-config could find them. After that, the build succeeded and FunctionSpace works now.