Error after gmshio.model_to_mesh

I am gutted as a file I have been most happily working with all morning suddenly stopped to work at noon.
I create a mesh with gmsh and all works great, as it has always before. But after issuing


domain, cell_markers, facet_markers = gmshio.model_to_mesh(gmsh.model, mesh_comm, gmsh_model_rank, gdim=gdim)

I get the error

TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. dolfinx.cpp.fem.CoordinateElement(celltype: dolfinx.cpp.mesh.CellType, degree: int)
    2. dolfinx.cpp.fem.CoordinateElement(celltype: dolfinx.cpp.mesh.CellType, degree: int, variant: basix::element::lagrange_variant)
Invoked with: , 1, 

I tried to see if it worked for the tutorial Tutorial: membrane and I get the same error, it has always worked.

The MWE is hence

import gmsh
gmsh.initialize()
membrane = gmsh.model.occ.addDisk(0, 0, 0, 1, 1)
gmsh.model.occ.synchronize()
gdim = 2
gmsh.model.addPhysicalGroup(gdim, [membrane], 1)
gmsh.option.setNumber("Mesh.CharacteristicLengthMin",0.05)
gmsh.option.setNumber("Mesh.CharacteristicLengthMax",0.05)
gmsh.model.mesh.generate(gdim)
from dolfinx.io import gmshio
from mpi4py import MPI

gmsh_model_rank = 0
mesh_comm = MPI.COMM_WORLD
domain, cell_markers, facet_markers = gmshio.model_to_mesh(gmsh.model, mesh_comm, gmsh_model_rank, gdim=gdim)

and I get the error mentioned above.
Any hints please? Thanks a lot, this one left me very puzzled.

Issuing

import dolfinx
print(dolfinx.__version__, dolfinx.git_commit_hash)

I get

0.6.0 7ac1d50b477b430858a9d3f53289bb950900528d

Hi, I’m not able to reproduce the error with -

import dolfinx
print(dolfinx.__version__, dolfinx.git_commit_hash)

gives -

0.5.2 unknown

And MWE yields -

Info    : Meshing 1D...
Info    : Meshing curve 1 (Ellipse)
Info    : Done meshing 1D (Wall 0.00121124s, CPU 0.000417s)
Info    : Meshing 2D...
Info    : Meshing surface 1 (Plane, Frontal-Delaunay)
Info    : Done meshing 2D (Wall 0.0519911s, CPU 0.049835s)
Info    : 1550 nodes 3099 elements

I think your question has been addressed here Error converting mesh from GMSH to DOLFINx

1 Like

Hello,
many thanks for this.
The post you linked was interesting, but did not help me too much.
I am on MacOS, so Ubuntu packages are not an option.
The suggested workaround of installing first gmsh
and python-gmsh and then mamba install -n fenicsx-env -c conda-forge fenics-dolfinx=0.6.0=*_101 as that fenics-dolfinx version seem not to be available any more. If try to install, after gmsh and its Python Api, fenics-dolfinx I get all sort of conflicts, and conda Solve Environment runs forever, to no avail.
I am totally stuck spent the whole day tampering with this any remaining suggestions maybe? Am i the only with this issue, must be something on my machine then? Running Jupyter Lab via Docker is hardly an option for me too regretfully, was loving me local installation. Thanks a lot

Did you install dolfinx through conda? I recently got the same error after installing new packages into a previously-working conda environment (which triggered all the fenics-* packages to update). It seems that the workaround you found now results in incompatible versions of dolfinx and basix being installed.

If conda installation is available to you, I can recommend the following, which is based on the workaround you found but explicitly specifies the version and build for all FEniCSx packages to ensure a compatible environment (I prefer to use mamba in place of conda because it solves the environment much faster).

First, install mamba:

conda activate
conda install -c conda-forge mamba

Then copy these commands to a shell script and run it with bash -i <script-name> (the “-i” is needed for the conda activate command to work):

#!/bin/bash

# Select PETSc scalar type and generate environment name
SCALAR_TYPE=real
ENV_NAME="dolfinx-${SCALAR_TYPE}"


# Create list of packages to install, depending on PETSc scalar type.
# The selected packages depend on Python 3.10 and MPICH for MPI communication.
if [[ "$SCALAR_TYPE" == "real" ]]; then
  FENICSX_PKGS="fenics-basix=0.6.0=py310hdf3cbec_0 fenics-dolfinx=0.6.0=py310hf97048e_101 fenics-ffcx=0.6.0=pyh56297ac_0 fenics-libbasix=0.6.0=h1284905_0 fenics-libdolfinx=0.6.0=h4cb9d57_101 fenics-ufcx=0.6.0=h56297ac_0 fenics-ufl=2023.1.1=pyhd8ed1ab_1"
else
  FENICSX_PKGS="fenics-basix=0.6.0=py310hdf3cbec_0 fenics-dolfinx=0.6.0=py310he6dc2dd_1 fenics-ffcx=0.6.0=pyh56297ac_0 fenics-libbasix=0.6.0=h1284905_0 fenics-libdolfinx=0.6.0=hf51c956_1 fenics-ufcx=0.6.0=h56297ac_0 fenics-ufl=2023.1.1=pyhd8ed1ab_1"
fi


# Install packages and activate the new environment
mamba create -y -c conda-forge -n "${ENV_NAME}" $FENICSX_PKGS
conda activate "${ENV_NAME}"
if [[ "$?" == "0" ]]; then
  conda config --env --add channels conda-forge

  # Pin FEniCSx packages
  for PKG in $FENICSX_PKGS; do
    conda config --env --append pinned_packages $PKG
  done

  mamba install -y python-gmsh
else
  echo "Failed to create ${ENV_NAME} environment"
fi

Unfortunately, I can’t test this on MacOS, but hopefully you can make any translations, if needed.

1 Like

Hello conpierce8,

I think it did work, that was outstanding help thank you.

Your bash script results in an error, about mamba activate requiring an init command, and it even says the environment could not be created.

Yet, the environment is there, could be activated by conda activate.

Just one final question. Your script does not install gmsh, is that intended? Is python-gmsh all it is needed? I got an error when running import gmsh, module not found.
I tried mamba install gmsh and still same error, module not found. This Stack Overflow post suggests gmsh is to be installed via pip, as the Conda package does not contain the Python API. But is not python-gmsh the API one needs? A tad confusing for me.

I did pip install and it all works now, which is great, thank you very much, much appreciated.

1 Like

Hi Mark,

I have also encountered that error, but after some fiddling with the script I thought I had eliminated the error. Can you post the exact output from the script? (If not, no problem.)

The naming conventions for Gmsh can be a little confusing. The conda package called gmsh installs only the Gmsh binaries. It doesn’t add anything to the Python packages installed in your environment (at <anaconda_dir>/envs/dolfinx-real/lib/python3.10/site-packages). Hence, when you install only gmsh, you get an ImportError. Installing python-gmsh adds the Python bindings for Gmsh, i.e. it installs a Python package named gmsh in site-packages. Since the conda package python-gmsh depends on the conda package gmsh, installing python-gmsh will also install gmsh.

I prefer to install the conda package python-gmsh rather than use pip, because it’s recommended to avoid mixing pip and conda wherever possible.

-Connor

1 Like

On further thought, you may need to install the osx-64 packages (rather than the linux-64 packages which are specified by the build numbers above). Here’s a script with osx-64 build strings and the option to specify the MPI implementation. You can try running it with bash -i <script_name>, or source ./<script_name> if you still get the error from conda activate.

#!/bin/bash
#
# Creates a compatible FEniCSx installation through conda for OSX-64.


# Select PETSc scalar type and generate environment name
SCALAR_TYPE=real
MPI_IMPL=mpich
ENV_NAME="dolfinx-${MPI_IMPL}-${SCALAR_TYPE}"


# Create list of packages to install, depending on PETSc scalar type and MPI implementation.
# The selected packages depend on Python 3.10.

# Dependencies of fenicx-dolfinx from the osx-64 subdir
DOLFINX_DEPS="fenics-basix=0.6.0=py310ha23aa8a_0 fenics-ufcx=0.6.0=h56297ac_0 fenics-ufl=2023.1.1=pyhd8ed1ab_1 fenics-ffcx=0.6.0=pyh56297ac_0"

# List of required packages
if [[ "$SCALAR_TYPE" == "real" ]]; then
  if [[ "$MPI_IMPL" == "mpich" ]]; then
    FENICSX_PKGS="fenics-dolfinx=0.6.0=py310h9047b3e_101 ${DOLFINX_DEPS}"
  else
    FENICSX_PKGS="fenics-dolfinx=0.6.0=py310h7d02ea0_101 ${DOLFINX_DEPS}"
  fi
else
  if [[ "$MPI_IMPL" == "mpich" ]]; then
    FENICSX_PKGS="fenics-dolfinx=0.6.0=py310h3de82d3_1 ${DOLFINX_DEPS}"
  else
    FENICSX_PKGS="fenics-dolfinx=0.6.0=py310h66e7674_1 ${DOLFINX_DEPS}"
  fi
fi


# Install packages and activate the new environment
mamba create -y -c conda-forge -n "${ENV_NAME}" $FENICSX_PKGS
conda activate "${ENV_NAME}"
if [[ "$?" == "0" ]]; then
  conda config --env --add channels conda-forge

  # Pin FEniCSx packages
  for PKG in $FENICSX_PKGS; do
    conda config --env --append pinned_packages $PKG
  done

  mamba install -y python-gmsh
else
  echo "'conda activate ${ENV_NAME}' failed."
  echo "You may need to manually pin the FEniCSx packages and then install python-gmsh."
fi

Hi there,

Can you post the exact output from the script? (If not, no problem.)

sure, I re-ran it as I did yesterday and this is the output:

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.


Failed to create dolfinx_test-real environment

If at this point I issued conda activate it would work though.
But two notes are in order:

  1. this is on Linux, Ubunti 20.04, as I wanted to try/understand it before trying on me other laptop
  2. I ran the script via ./ , I missed your suggestion about bash -i or source you clearly were aware of the problem. Indeed I tried once more source ./<script_name>, and there are no errors at all as you mentioned, my bad and rushed to it sorry for the confusion.

And indeed, python-gmsh installs gmsh too.
Thanks again extremely useful.

1 Like

Just chiming in to say I ran into a similar issue present with gmshio.model_to_mesh()

Traceback (most recent call last):
  File "PATH/FILENAME.py", line 39, in <module>
    msh, cell_markers, facet_markers = gmshio.model_to_mesh(gmsh.model, MPI.COMM_SELF, 0)
  File "home/usr_name/anaconda3/envs/env_name/lib/python3.10/site-packages/dolfinx/io/gmshio.py", line 250, in model_to_mesh
    mesh = create_mesh(comm, cells, x[:, :gdim], ufl_domain, partitioner)
  File "/home/usr_name/anaconda3/envs/env_name/lib/python3.10/site-packages/dolfinx/mesh.py", line 177, in create_mesh
    cmap = _cpp.fem.CoordinateElement(_uflcell_to_dolfinxcell[cell_shape], cell_degree, variant)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. dolfinx.cpp.fem.CoordinateElement(celltype: dolfinx.cpp.mesh.CellType, degree: int)
    2. dolfinx.cpp.fem.CoordinateElement(celltype: dolfinx.cpp.mesh.CellType, degree: int, variant: basix::element::lagrange_variant)

Invoked with: <CellType.interval: 2>, 1, <LagrangeVariant.equispaced: 0>

I was able to eliminate this error by conda installing the following specific packages (without having to run the above script):

conda install fenics-basix=0.6.0=py310hdf3cbec_0 fenics-dolfinx=0.6.0=py310hf97048e_101 fenics-ffcx=0.6.0=pyh56297ac_0 fenics-libbasix=0.6.0=h1284905_0 fenics-libdolfinx=0.6.0=h4cb9d57_101 fenics-ufcx=0.6.0=h56297ac_0 fenics-ufl=2023.1.1=pyhd8ed1ab_1