Converter from GMSH to XDMF (with physical groups)

Hello,

I recently had to import GMSH mesh in Fenics, so I wrote a small script to ease the import.
This script uses meshio to convert a GMSH mesh file (msh2, msh40, msh41) into two XDMF files: one for the domain and another to store the physical groups defined in GMSH. There is also an utility function returning the Mesh and the MeshFunction associated to those files. The script and more details about its usage are available at: https://github.com/floiseau/msh2xdmf.

Feel free to recommend any bugs, improvements or modifications.

9 Likes

Thanks @floiseau. I’ll add your script to the dolfin-bin meshio-tools package provided by Debian and Ubuntu.

@floiseau thank for the tool. Could you please tell me how to install this tool? I downloaded the repository and ran the test command in the tool’s folder

I got the error β€œNo module named pytest”

Thanks

In order to use the script, you need to install fenics and meshio. To run the test, you need to install pytest.
Once those python package are installed, you just need to put the script msh2xdmf.pyin the same directory as your mesh and follow the instructions in the Github readme.
I hope those explanations fixes your issue.

1 Like

@floiseau Thank you very much for your reply. Unfortunately, I got the following error during running the test. Could you please help me figure this out? I am running FEniCS on Anaconda and have Meshio version 4.0.13 install on FEniCS environment.

test/conversion/test_conversion.py Fatal Python error: Aborted

Current thread 0x00007fa7dc39a740 (most recent call first):
File β€œβ€, line 219 in _call_with_frames_removed
File β€œβ€, line 1109 in exec_module
File β€œβ€, line 671 in _load_unlocked
File β€œβ€, line 975 in _find_and_load_unlocked
File β€œβ€, line 991 in _find_and_load
File β€œβ€, line 219 in _call_with_frames_removed
File β€œβ€, line 1042 in _handle_fromlist
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/h5py/init.py”, line 55 in
File β€œβ€, line 219 in _call_with_frames_removed
File β€œβ€, line 783 in exec_module
File β€œβ€, line 671 in _load_unlocked
File β€œβ€, line 975 in _find_and_load_unlocked
File β€œβ€, line 991 in _find_and_load
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/meshio/xdmf/main.py”, line 320 in init
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/meshio/xdmf/main.py”, line 528 in write
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/meshio/_helpers.py”, line 144 in write
File β€œ/home/phuris/Meshing Tools/msh2xdmf-master/msh2xdmf.py”, line 76 in export_domain
File β€œ/home/phuris/Meshing Tools/msh2xdmf-master/msh2xdmf.py”, line 26 in msh2xdmf
File β€œ/home/phuris/Meshing Tools/msh2xdmf-master/test/conversion/test_conversion.py”, line 14 in test_conversion
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/python.py”, line 182 in pytest_pyfunc_call
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/callers.py”, line 187 in _multicall
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/manager.py”, line 84 in
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/manager.py”, line 93 in _hookexec
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/hooks.py”, line 286 in call
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/python.py”, line 1477 in runtest
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/runner.py”, line 135 in pytest_runtest_call
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/callers.py”, line 187 in _multicall
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/manager.py”, line 84 in
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/manager.py”, line 93 in _hookexec
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/hooks.py”, line 286 in call
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/runner.py”, line 217 in
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/runner.py”, line 244 in from_call
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/runner.py”, line 216 in call_runtest_hook
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/runner.py”, line 186 in call_and_report
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/runner.py”, line 100 in runtestprotocol
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/runner.py”, line 85 in pytest_runtest_protocol
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/callers.py”, line 187 in _multicall
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/manager.py”, line 84 in
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/manager.py”, line 93 in _hookexec
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/hooks.py”, line 286 in call
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/main.py”, line 272 in pytest_runtestloop
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/callers.py”, line 187 in _multicall
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/manager.py”, line 84 in
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/manager.py”, line 93 in _hookexec
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/hooks.py”, line 286 in call
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/main.py”, line 247 in _main
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/main.py”, line 191 in wrap_session
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/main.py”, line 240 in pytest_cmdline_main
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/callers.py”, line 187 in _multicall
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/manager.py”, line 84 in
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/manager.py”, line 93 in _hookexec
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pluggy/hooks.py”, line 286 in call
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/_pytest/config/init.py”, line 124 in main
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/pytest/main.py”, line 7 in
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/runpy.py”, line 86 in _run_code
File β€œ/home/phuris/anaconda3/envs/fenicsproject/lib/python3.8/runpy.py”, line 193 in _run_module_as_main
Aborted (core dumped)

I am not sure and the error message is not explicit. There might be a problem with your h5py installation (it might be running against an older version of HDF5). You can try to just import h5py in a Python script and run it to see if there is any error message.

Thank you very much, I have one more question regarding to the conversion but I am not sure if this is a problem of FeniCS or the Meshio. Is this tool capable of converting structured meshes to FEniCS or is there any other tools that can also convert structured meshes with physical subdomains and boundaries?

If you mean that you want to use quadrilateral elements, they are not supported by the current version of FEniCS.

The support in the current version of dolfin for quadrilateral meshes is quite limited.
This is currently being improved in the development version of dolfin, dolfinx. The interface for converting meshes from gmsh to dolfin is there in development, see the following pull request (not merged yet)

Thank you very much for the information.

Thank you for the script! I have a question regarding setting the boundary conditions.
I have defined 1 Physical Volume as the domain and 4 physical surfaces as boundaries in my .geo file. However, when I use

python3 msh2xdmf.py -d 3 mesh.msh

to convert my .msh file to .xdmf, the domain is not listed in the table. But the mesh_domain.xdmf file is created. How can I fix this?

To import my 3D mesh and to setup the boundary conditions (e.g. β€œinlet”) I used:

mesh, mesh_function, association_table = import_mesh_from_xdmf(prefix="mesh", dim=3)
...
bcu_inflow  = DirichletBC(V, Constant((0, 0, 10)), "inlet")

However, I get the error as below:
RuntimeError: Unable to compile C++ code with dijitso

I should be very grateful if you could guide me on how to successfully do this.

You should tag physical boundaries with integers, not strings such as Β«inletΒ», as this is not a supported input format to dolfin. The reason for this is simply that a string takes up way more space than an integer, and the length is undefined.

You need to provide a minimal example, illustrating the issue you are having for me to give any further guidance.

This is simply because the conversion script is made for simplices (triangles and tetrahedrons).
You could modify the msh2xdmf.py script, but I cannot guarantee that the resulting mesh works out well in dolfin, as the quadrilateral and hexahedral support in dolfin is very limited.

I would recommend switching to dolfinx if you really require these element shapes.

1 Like

Hi @floiseau,

I do have a problem regarding the conflicts of h5py and HDF5. I have tried the possible solutions listed here but nothing worked for me. The error message still shows that there is a conflicting between these two modules. Any other possibilities that I can solve it? Thanks in advance.

Kind regards,
Alice

Hi @Alice_Peng,

I suppose that the issue is that h5py is built against a wrong version of HDF5.
In this case, you will need to find the version of HDF5. It can be found by running one of the h5 commands with the --version argument (for instance h5dump --version).
Using pip, you can install the version of h5py matching the version of your HDF5 install by running:

$ HDF5_VERSION=X.Y.Z pip install --no-binary=h5py h5py

If you use conda as a package manager, you should also be able to choose the version of both packages.

Best regards,
Flavien

Hi @floiseau,

Thank you for this script. Do you know how to make this work inside a fenics container ?

Cheers
Rem

Hi @RemDelaporteMathurin,
You can use this script inside a Fenics container by installing the dependencies (meshio with the command pip install meshio[all] and optionally pytest with the command pip install -U pytest) inside this container.

I get the following error :

fenics@c0079f837be3:~/shared$ python3 convert_msh_xdfm.py step_file.msh
/home/fenics/.local/lib/python3.6/site-packages/h5py/__init__.py:40: UserWarning: h5py is running against HDF5 1.10.0 when it was built against 1.10.4, this may cause problems
  '{0}.{1}.{2}'.format(*version.hdf5_built_version_tuple)
Traceback (most recent call last):
  File "convert_msh_xdfm.py", line 255, in <module>
    msh2xdmf(args.msh_file, args.dimension, directory=current_directory)
  File "convert_msh_xdfm.py", line 28, in msh2xdmf
    export_domain(msh, dim, directory, prefix)
  File "convert_msh_xdfm.py", line 90, in export_domain
    file_format="xdmf"
  File "/home/fenics/.local/lib/python3.6/site-packages/meshio/_helpers.py", line 144, in write
    return writer(filename, mesh, **kwargs)
  File "/home/fenics/.local/lib/python3.6/site-packages/meshio/xdmf/main.py", line 528, in write
    XdmfWriter(*args, **kwargs)
  File "/home/fenics/.local/lib/python3.6/site-packages/meshio/xdmf/main.py", line 336, in __init__
    self.h5_file = h5py.File(self.h5_filename, "w")
  File "/home/fenics/.local/lib/python3.6/site-packages/h5py/_hl/files.py", line 405, in __init__
    fapl = make_fapl(driver, libver, rdcc_nslots, rdcc_nbytes, rdcc_w0, **kwds)
  File "/home/fenics/.local/lib/python3.6/site-packages/h5py/_hl/files.py", line 111, in make_fapl
    plist.set_libver_bounds(low, high)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5p.pyx", line 1142, in h5py.h5p.PropFAID.set_libver_bounds
ValueError: Invalid high library version bound (invalid high library version bound)