Error when calling function gmshio.model_to_mesh in version 0.8.0

Hello, I am trying to run old codes in the new 0.8.0 version and I have an issue with the function gmshio.model_to_mesh. Please find my code bellow.

import gmsh
from mpi4py import MPI
from dolfinx.io import gmshio, XDMFFile
Lx = 1.
Ly = .5
Lcrack = 0.3
lc = 0.1
dist_min = .1
dist_max = .3
refinement_ratio = 10
gdim = 2
mesh_comm = MPI.COMM_WORLD
model_rank = 0
gmsh.initialize()

facet_tags = {"left": 1, "right": 2, "top": 3, "crack": 4, "bottom_no_crack": 5}
cell_tags = {"all": 20}

if mesh_comm.rank == model_rank:
    model = gmsh.model()
    model.add("Rectangle")
    model.setCurrent("Rectangle")
    # Create the points
    p1 = model.geo.addPoint(0.0, 0.0, 0, lc)
    p2 = model.geo.addPoint(Lcrack, 0.0, 0, lc)
    p3 = model.geo.addPoint(Lx, 0, 0, lc)
    p4 = model.geo.addPoint(Lx, Ly, 0, lc)
    p5 = model.geo.addPoint(0, Ly, 0, lc)
    # Create the lines
    l1 = model.geo.addLine(p1, p2, tag=facet_tags["crack"])
    l2 = model.geo.addLine(p2, p3, tag=facet_tags["bottom_no_crack"])
    l3 = model.geo.addLine(p3, p4, tag=facet_tags["right"])
    l4 = model.geo.addLine(p4, p5, tag=facet_tags["top"])
    l5 = model.geo.addLine(p5, p1, tag=facet_tags["left"])
    # Create the surface
    cloop1 = model.geo.addCurveLoop([l1, l2, l3, l4, l5])
    surface_1 = model.geo.addPlaneSurface([cloop1])
    
    # Define the mesh size and fields for the mesh refinement
    model.mesh.field.add("Distance", 1)
    model.mesh.field.setNumbers(1, "NodesList", [p2])
    model.mesh.field.add("Threshold", 2)
    model.mesh.field.setNumber(2, "IField", 1)
    model.mesh.field.setNumber(2, "LcMin", lc / refinement_ratio)
    model.mesh.field.setNumber(2, "LcMax", lc)
    model.mesh.field.setNumber(2, "DistMin", dist_min)
    model.mesh.field.setNumber(2, "DistMax", dist_max)
    model.mesh.field.setAsBackgroundMesh(2)
    model.geo.synchronize()
    
    # Assign mesh and facet tags
    surface_entities = [entity[1] for entity in model.getEntities(2)]
    model.addPhysicalGroup(2, surface_entities, tag=cell_tags["all"])
    model.setPhysicalName(2, 2, "Rectangle surface")
    model.mesh.generate(gdim)
    
    for (key,value) in facet_tags.items():
        model.addPhysicalGroup(1, [value], tag=value)
        model.setPhysicalName(1, value, key)
msh, cell_tags, facet_tags = gmshio.model_to_mesh(model, mesh_comm, model_rank, gdim=gdim)

I obtain the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~/anaconda3/envs/fenicsx-cism-2024/lib/python3.12/site-packages/dolfinx/fem/element.py:91, in _(e)
     90 try:
---> 91     return CoordinateElement(_cpp.fem.CoordinateElement_float32(e._e))
     92 except TypeError:

TypeError: __init__(): incompatible function arguments. The following argument types are supported:
    1. __init__(self, celltype: dolfinx.cpp.mesh.CellType, degree: int) -> None
    2. __init__(self, element: basix::FiniteElement<float>) -> None
    3. __init__(self, celltype: dolfinx.cpp.mesh.CellType, degree: int, variant: int) -> None

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

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 msh, cell_tags, facet_tags = gmshio.model_to_mesh(model, mesh_comm, model_rank, gdim=gdim)

File ~/anaconda3/envs/fenicsx-cism-2024/lib/python3.12/site-packages/dolfinx/io/gmshio.py:293, in model_to_mesh(model, comm, rank, gdim, partitioner, dtype)
    291 gmsh_cell_perm = cell_perm_array(_cpp.mesh.to_type(str(ufl_domain.ufl_cell())), num_nodes)
    292 cells = cells[:, gmsh_cell_perm].copy()
--> 293 mesh = create_mesh(comm, cells, x[:, :gdim].astype(dtype, copy=False), ufl_domain, partitioner)
    295 # Create MeshTags for cells
    296 local_entities, local_values = distribute_entity_data(
    297     mesh._cpp_object, mesh.topology.dim, cells, cell_values
    298 )

File ~/anaconda3/envs/fenicsx-cism-2024/lib/python3.12/site-packages/dolfinx/mesh.py:407, in create_mesh(comm, cells, x, e, partitioner)
    404 try:
    405     # e is a UFL domain
    406     e_ufl = e.ufl_coordinate_element()  # type: ignore
--> 407     cmap = _coordinate_element(e_ufl.basix_element)  # type: ignore
    408     domain = e
    409     dtype = cmap.dtype

File ~/anaconda3/envs/fenicsx-cism-2024/lib/python3.12/functools.py:907, in singledispatch.<locals>.wrapper(*args, **kw)
    903 if not args:
    904     raise TypeError(f'{funcname} requires at least '
    905                     '1 positional argument')
--> 907 return dispatch(args[0].__class__)(*args, **kw)

File ~/anaconda3/envs/fenicsx-cism-2024/lib/python3.12/site-packages/dolfinx/fem/element.py:93, in _(e)
     91     return CoordinateElement(_cpp.fem.CoordinateElement_float32(e._e))
     92 except TypeError:
---> 93     return CoordinateElement(_cpp.fem.CoordinateElement_float64(e._e))

TypeError: __init__(): incompatible function arguments. The following argument types are supported:
    1. __init__(self, celltype: dolfinx.cpp.mesh.CellType, degree: int) -> None
    2. __init__(self, element: basix::FiniteElement<double>) -> None
    3. __init__(self, celltype: dolfinx.cpp.mesh.CellType, degree: int, variant: int) -> None

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

Many thanks in advance for your help!
best,

Claire

Hi Claire,

I cannot reproduce the error with the docker image: ghcr.io/fenics/dolfinx/dolfinx:v0.8.0 .

It seems lie you are using conda for your your installation. Do you mind sharing how you set up your environment?

(This error looks similar to: Problem reading mesh checkpoints (N-to-M) with adios4dolfinx - #7 by Exon)

Hi! I followed these instructions: FEniCSx examples at CISM 2024 — CISM Variational Fracture with FEniCSx

Those sort of errors are typically related to a mismatch in nanobind version, most probably the version you have is not the same as the one used when the conda package was built, or you have nanobind coming from pypi instead of the one coming from conda.

I am not a conda user so I can’t give more specific suggestions, but hopefully that is a starting point.

I can reproduce the issue when using those instructions (only exception is using conda from conda-forge rather than anaconda due to Licensing).

Can reproduce with slightly smaller env file on a Ubuntu 22.04.4 LTS laptop.

name: fenicsx-cism-2024
channels:
  - conda-forge
dependencies:
  - fenics-dolfinx
  - python-gmsh

and the following code:

from basix.ufl import element

import dolfinx.cpp.fem

c_el = element("Lagrange", "triangle", 1, shape=(2,))
dolfinx.cpp.fem.CoordinateElement_float64(c_el.basix_element._e)

returning

Traceback (most recent call last):
  File "/home/dokken/Documents/debug/mwe_debug.py", line 6, in <module>
    dolfinx.cpp.fem.CoordinateElement_float64(c_el.basix_element._e)
TypeError: __init__(): incompatible function arguments. The following argument types are supported:
    1. __init__(self, celltype: dolfinx.cpp.mesh.CellType, degree: int) -> None
    2. __init__(self, element: basix::FiniteElement<double>) -> None
    3. __init__(self, celltype: dolfinx.cpp.mesh.CellType, degree: int, variant: int) -> None

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

which runs fine in docker

Resulting conda env export:

conda env export
name: fenicsx-cism-2024
channels:
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=conda_forge
  - _openmp_mutex=4.5=2_gnu
  - _sysroot_linux-64_curr_repodata_hack=3=h69a702a_16
  - binutils_impl_linux-64=2.40=ha1999f0_7
  - binutils_linux-64=2.40=hb3c18ed_1
  - bzip2=1.0.8=h4bc722e_7
  - c-ares=1.33.1=heb4867d_0
  - c-blosc2=2.15.1=hc57e6cf_0
  - ca-certificates=2024.8.30=hbcca054_0
  - cairo=1.18.0=hebfffa5_3
  - cffi=1.17.1=py312h06ac9bb_0
  - expat=2.6.3=h5888daf_0
  - fenics-basix=0.8.0=py312h68727a3_2
  - fenics-dolfinx=0.8.0=py312h66e9945_105
  - fenics-ffcx=0.8.0=pyh4af843d_0
  - fenics-libbasix=0.8.0=h7cb7ce6_2
  - fenics-libdolfinx=0.8.0=h17dcdb5_105
  - fenics-ufcx=0.8.0=h22f594c_0
  - fenics-ufl=2024.1.0=pyhd8ed1ab_0
  - fftw=3.3.10=mpi_mpich_hbcf76dd_10
  - fltk=1.3.9=h9305793_1
  - font-ttf-dejavu-sans-mono=2.37=hab24e00_0
  - font-ttf-inconsolata=3.000=h77eed37_0
  - font-ttf-source-code-pro=2.038=h77eed37_0
  - font-ttf-ubuntu=0.83=h77eed37_2
  - fontconfig=2.14.2=h14ed4e7_0
  - fonts-conda-ecosystem=1=0
  - fonts-conda-forge=1=0
  - freeimage=3.18.0=h4b96d29_20
  - freetype=2.12.1=h267a509_2
  - gcc_impl_linux-64=12.4.0=hb2e57f8_1
  - gcc_linux-64=12.4.0=h6b7512a_1
  - gmp=6.3.0=hac33072_2
  - gmsh=4.12.2=h6b98cf8_0
  - gxx_impl_linux-64=12.4.0=h613a52c_1
  - gxx_linux-64=12.4.0=h8489865_1
  - hdf5=1.14.3=mpi_mpich_h0f54ddc_5
  - hypre=2.31.0=mpi_mpich_hd1da18f_1
  - icu=75.1=he02047a_0
  - imath=3.1.11=hfc55251_0
  - jxrlib=1.1=hd590300_3
  - kahip=3.16=h2fbc463_4
  - kahip-python=3.16=py312ha7be871_4
  - kernel-headers_linux-64=3.10.0=h4a8ded7_16
  - keyutils=1.6.1=h166bdaf_0
  - krb5=1.21.3=h659f571_0
  - lcms2=2.16=hb7c19ff_0
  - ld_impl_linux-64=2.40=hf3520f5_7
  - lerc=4.0.0=h27087fc_0
  - libadios2=2.10.1=mpi_mpich_hb885cfe_3
  - libaec=1.1.3=h59595ed_0
  - libblas=3.9.0=23_linux64_openblas
  - libboost=1.86.0=hb8260a3_2
  - libboost-devel=1.86.0=h1a2810e_2
  - libboost-headers=1.86.0=ha770c72_2
  - libcblas=3.9.0=23_linux64_openblas
  - libcurl=8.9.1=hdb1bdb2_0
  - libdeflate=1.21=h4bc722e_0
  - libedit=3.1.20191231=he28a2e2_2
  - libev=4.33=hd590300_2
  - libexpat=2.6.3=h5888daf_0
  - libffi=3.4.2=h7f98852_5
  - libgcc=14.1.0=h77fa898_1
  - libgcc-devel_linux-64=12.4.0=ha4f9413_101
  - libgcc-ng=14.1.0=h69a702a_1
  - libgfortran=14.1.0=h69a702a_1
  - libgfortran-ng=14.1.0=h69a702a_1
  - libgfortran5=14.1.0=hc5f4f2c_1
  - libglib=2.80.3=h315aac3_2
  - libglu=9.0.0=ha6d2627_1004
  - libgomp=14.1.0=h77fa898_1
  - libhwloc=2.11.1=default_hecaa2ac_1000
  - libiconv=1.17=hd590300_2
  - libjpeg-turbo=3.0.0=hd590300_1
  - liblapack=3.9.0=23_linux64_openblas
  - libnghttp2=1.58.0=h47da74e_1
  - libnsl=2.0.1=hd590300_0
  - libopenblas=0.3.27=pthreads_hac2b453_1
  - libpng=1.6.43=h2797004_0
  - libptscotch=7.0.4=h2376d02_5
  - libraw=0.21.1=h2a13503_2
  - libsanitizer=12.4.0=h46f95d5_1
  - libscotch=7.0.4=h3055ed5_5
  - libsodium=1.0.20=h4ab18f5_0
  - libsqlite=3.46.1=hadc24fc_0
  - libssh2=1.11.0=h0841786_0
  - libstdcxx=14.1.0=hc0a3c3a_1
  - libstdcxx-devel_linux-64=12.4.0=ha4f9413_101
  - libstdcxx-ng=14.1.0=h4852527_1
  - libtiff=4.6.0=h46a8edc_4
  - libuuid=2.38.1=h0b41bf4_0
  - libwebp-base=1.4.0=hd590300_0
  - libxcb=1.16=hb9d3cd8_1
  - libxcrypt=4.4.36=hd590300_1
  - libxml2=2.12.7=he7c6b58_4
  - libzlib=1.3.1=h4ab18f5_1
  - lz4-c=1.9.4=hcb278e6_0
  - metis=5.1.0=h59595ed_1007
  - mpfr=4.2.1=h38ae2d0_2
  - mpi=1.0=mpich
  - mpi4py=4.0.0=py312h0068001_3
  - mpich=4.2.2=h4a7f18d_101
  - mumps-include=5.7.2=ha770c72_0
  - mumps-mpi=5.7.2=h09c71e5_0
  - ncurses=6.5=he02047a_1
  - numpy=2.1.1=py312h58c1407_0
  - occt=7.7.2=novtk_h130ccc2_101
  - openexr=3.2.2=haf962dd_1
  - openjpeg=2.5.2=h488ebb8_0
  - openssl=3.3.2=hb9d3cd8_0
  - parmetis=4.0.3=h583469f_1006
  - pcre2=10.44=hba22ea6_2
  - petsc=3.21.5=real_h8dc6568_100
  - petsc4py=3.21.5=py312h23142ab_1
  - pip=24.2=pyh8b19718_1
  - pixman=0.43.2=h59595ed_0
  - pkg-config=0.29.2=h4bc722e_1009
  - pthread-stubs=0.4=h36c2ea0_1001
  - pugixml=1.14=h59595ed_0
  - pycparser=2.22=pyhd8ed1ab_0
  - python=3.12.5=h2ad013b_0_cpython
  - python-gmsh=4.12.2=h57928b3_0
  - python_abi=3.12=5_cp312
  - rapidjson=1.1.0.post20240409=hac33072_1
  - readline=8.2=h8228510_1
  - scalapack=2.2.0=h417d24c_2
  - setuptools=73.0.1=pyhd8ed1ab_0
  - slepc=3.21.1=real_h97ad6bc_302
  - slepc4py=3.21.1=py312hf817a4c_104
  - suitesparse=7.8.2=hb42a789_0
  - superlu=5.2.2=h00795ac_0
  - superlu_dist=9.0.0=h3feb4ed_1
  - sysroot_linux-64=2.17=h4a8ded7_16
  - tbb=2021.13.0=h84d6215_0
  - tk=8.6.13=noxft_h4845f30_101
  - tzdata=2024a=h8827d51_1
  - wheel=0.44.0=pyhd8ed1ab_0
  - xorg-fixesproto=5.0=h7f98852_1002
  - xorg-kbproto=1.0.7=h7f98852_1002
  - xorg-libice=1.1.1=hd590300_0
  - xorg-libsm=1.2.4=h7391055_0
  - xorg-libx11=1.8.9=hb711507_1
  - xorg-libxau=1.0.11=hd590300_0
  - xorg-libxdmcp=1.1.3=h7f98852_0
  - xorg-libxext=1.3.4=h0b41bf4_2
  - xorg-libxfixes=5.0.3=h7f98852_1004
  - xorg-libxmu=1.1.3=h4ab18f5_1
  - xorg-libxrender=0.9.11=hd590300_0
  - xorg-libxt=1.3.0=hd590300_1
  - xorg-renderproto=0.11.1=h7f98852_1002
  - xorg-xextproto=7.3.0=h0b41bf4_1003
  - xorg-xproto=7.0.31=h7f98852_1007
  - xz=5.2.6=h166bdaf_0
  - yaml=0.2.5=h7f98852_2
  - zeromq=4.3.5=ha4adb4c_5
  - zfp=0.5.5=h9c3ff4c_8
  - zlib=1.3.1=h4ab18f5_1
  - zlib-ng=2.2.1=he02047a_0
  - zstd=1.5.6=ha6fb4c9_0
  - pip:
      - attrs==24.2.0
      - fancycompleter==0.9.1
      - pdbpp==0.10.3
      - pygments==2.18.0
      - pyrepl==0.9.0
      - wmctrl==0.5

Thanks to help from the everhelpful @minrk, a temporary workaround for this issue is using the environment file:
environment.yml

name: fenicsx-cism-2024
channels:
  - conda-forge
dependencies:
  - fenics-dolfinx
  - python-gmsh
  - fenics-basix=0.8.0=*_1

as this seems to be related to fenics-basix being built with gcc-13 while fenics-dolfinx is built with gcc-12 (paraphrasing Min here). He will try to fix this, but in the meantime this is a workaround.

Sorry about this!

Indeed, it is because fenics-basix has been updated to the latest compilers and fenics-dolfinx had not yet, but nanobind requires that the two use matching compiler versions and this wasn’t expressed in the runtime requirements of fenics-basix. This has been fixed in:

1 Like

I confirm the code runs now with my conda environment. Thanks!