Importing 3D .msh file into Fenics as mesh

Hi everyone,

I have currently built a mesh in Gmsh, of a 3D cylinder as shown below:

R = 1.5;
lc  = .15;
Point(1) = {0,0,0,lc};
Point(2) = {R,0,0,lc};
Point(3) = {0,R,0,lc};
Point(4) = {-R,0,0,lc};
Point(5) = {0,-R,0,lc};

Circle(1) = {2,1,3};
Circle(2) = {3,1,4};
Circle(3) = {4,1,5};
Circle(4) = {5,1,2};

Line Loop(5) = {1,2,3,4};
Plane Surface(6) = {5};

Extrude {0,0,-8} {

Field[1] = Distance;
Field[1].NodesList = {1};

Field[2] = Threshold;
Field[2].IField = 1;
Field[2].LcMin = lc / 30;
Field[2].LcMax = lc;
Field[2].DistMin = 0.01;
Field[2].DistMax = 4;

Field[3] = Min;
Field[3].FieldsList = {2};
Background Field = 3;

Mesh.CharacteristicLengthExtendFromBoundary = 0;
Mesh.CharacteristicLengthFromPoints = 0;
Mesh.CharacteristicLengthFromCurvature = 0;

Now I wish to put this into my Fenics program, but I have seen various conflicting information about how to do this, especially given that it is a 3D mesh and I need to convert to multiple XDMF files for each dimension? If anyone could tell me how to do this, it would be much appreciated.

As you have not created any physical curves/surfaces/volumes, you only need to convert the mesh, as described here: Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio
In your case, that would be:

import meshio
msh ="mesh.msh")

tetra_cells = []
for cell in msh.cells:
    if  cell.type == "tetra":
        if len(tetra_cells) == 0:
            tetra_cells =
            tetra_cells = np.vstack([tetra_cells,])

tetra_mesh = meshio.Mesh(points=msh.points, cells={"tetra": tetra_cells})
meshio.write("mesh.xdmf", tetra_mesh)

This XDMF-file can be loaded in to dolfin as described in many other posts in this forum. Note that if you want to tag boundaries, you need to follow the instructions in the post Im linking to, saving each of the boundary-meshes to a separate file.

1 Like

Thanks for your reply! I have tried running the code you have provided which leads me to this error message:

 /home/fenics/.local/lib/python3.6/site-packages/h5py/ UserWarning: h5py is running against HDF5 1.10.0 when it was built against 1.10.4, this may cause problems
Traceback (most recent call last):
  File "", line 67, in <module>
    meshio.write("mesh.xdmf", tetra_mesh)
  File "/home/fenics/.local/lib/python3.6/site-packages/meshio/", line 144, in write
    return writer(filename, mesh, **kwargs)
  File "/home/fenics/.local/lib/python3.6/site-packages/meshio/xdmf/", line 528, in write
    XdmfWriter(*args, **kwargs)
  File "/home/fenics/.local/lib/python3.6/site-packages/meshio/xdmf/", line 336, in __init__
    self.h5_file = h5py.File(self.h5_filename, "w")
  File "/home/fenics/.local/lib/python3.6/site-packages/h5py/_hl/", 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/", 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)

I have tried fully uninstalling both meshio and h5py but this still gives me this error message. I have also tried reinstalling both of these modules as described within this thread Gmsh 4.4.1 in FEniCS? Meshio
but I then obtain:

ERROR: Command "/usr/bin/python3 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-19z847ve/h5py/'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);'"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-s2dwmcju/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /tmp/pip-install-19z847ve/h5py/   

For reference, I am running all of this within Docker version 19.03.5, build 633a0ea838f1

I have not had any issues when following Gmsh 4.4.1 in FEniCS? Meshio (in combination with a docker image)

If @dokken suggestion does not work (it did not for me), try the dolfinx docker image (as dokken suggested to me), that worked for me.

This is going to be a stupid question, but how exactly would I use a dolfinx docker image? I have meshio and h5py installed on my system but when I open a docker image, those modules are not carried over

You can pull the docker image dolfinx/real using docker pull dolfinx/real and run it as any other docker image. It has Gmsh, meshio and pygmsh preinstalled.

I have managed to pull the docker image successfully, but I am unsure of how I can run this docker image instead of my standard docker image. When I have dolfin and fenics set up on my computer, I had a default docker image (named fenicsproject) to make accessing fenics easier.

docker run -ti --rm -v "$(pwd):/home/fenics/shared" -w /home/fenics/shared dolfinx/real

When I run this within the directory in which all my Fenics files are stored, I cannot successfully run any of my scripts. Even attempting to start Fenics via fenicsproject run leads to:

root@72806c6319bf:/home/fenics/shared# python3
Traceback (most recent call last):
  File "", line 2, in <module>
    from fenics import *
ModuleNotFoundError: No module named 'fenics'
root@72806c6319bf:/home/fenics/shared# fenicsproject run
bash: fenicsproject: command not found

I think you are misunderstanding how docker works. Docker creates a container, with only the software specified in the image (dolfinx/real) installed. The dolfinx container has gmsh, meshio and pygmsh installed (but not dolfin). Therefore, you can use this container to convert your msh file to xdmf. This should be a separate program or you can do it through the command line with meshio-convert.
Then you can import this xdmf file into dolfin on your original system.

Oh I see. I have now been able to successfully convert my .msh file to a .xdmf file using the docker image. Thank you so much for your patience and help