Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio

Thanks for the link.

but it seems to me that this post and the link that you shared mostly covers geometry that are generated from scratch with gmsh, whereas I deal with a CAD in a .step format. Is anyone has already dealt with such a thing ?

Thanks,

Quentin

The whole point of the reference is that it contains the updated syntax for reading in a msh file and converting it to xdmf, replacing

with

mesh_from_file = meshio.read("mesh.msh")
def create_mesh(mesh, cell_type, prune_z=False):
    cells = mesh.get_cells_type(cell_type)
    cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
    points = mesh.points[:, :2] if prune_z else mesh.points
    out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={
                           "name_to_read": [cell_data]})
    return out_mesh
line_mesh = create_mesh(mesh_from_file, "line", prune_z=True)
meshio.write("facet_mesh.xdmf", line_mesh)

triangle_mesh = create_mesh(mesh_from_file, "triangle", prune_z=True)
meshio.write("mesh.xdmf", triangle_mesh)

or similar

Dear dokken,

thank you for you reply.

Even though the facet mesh is generated sucessfully whit the comand above, for some reason, If I use

tetrahedron_mesh = create_mesh(mesh, "tetrahedron", prune_z=False)
meshio.write("SLICE.xdmf", tetrahedron_mesh)

I get the following error

Traceback (most recent call last):
  File "/mnt/c/Users/quent/Documents/Codes/Python/Mesh_GMSH2MeshIO/XDMF_OUTPUT_BOUNDARIES_TAGS/Conversion_GMSH2MeshIO.py", line 151, in <module>
    tetrahedron_mesh = create_mesh(MAILLAGE, "tetrahedron", prune_z=False)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/c/Users/quent/Documents/Codes/Python/Mesh_GMSH2MeshIO/XDMF_OUTPUT_BOUNDARIES_TAGS/Conversion_GMSH2MeshIO.py", line 141, in create_mesh
    cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/quentin_digimind/anaconda3/envs/MESHING/lib/python3.12/site-packages/meshio/_mesh.py", line 227, in get_cell_data
    return np.concatenate(
           ^^^^^^^^^^^^^^^
ValueError: need at least one array to concatenate

even though I added physical groups for volumes as

volumes = gmsh.model.getEntities(dim=3)
for volume in volumes:
    gmsh.model.addPhysicalGroup(3, [volume[1]] ,tag=volume[1] )
    gmsh.model.setPhysicalName(3, volume[1], str(volume[1]))

whereas

meshio.write("SLICE.xdmf", meshio.Mesh(points=mesh.points,cells=mesh.cells))

provides a mesh that seems OK.

Just to let you know .

best regards,

Quentin

As I do not have your step file, I cannot reproduce your error.

Note that it is not adviced to call
gmsh.model.occ.synchronize()
after adding physical groups, as it tends to remove them,

I finally fixe this issue ( was related to an issue with volume physical groups), so I am able to write the mesh as described in the post, but when I try to import the mesh into FeniCS I get the error

mesh = Mesh()
with XDMFFile("SLICE.xdmf") as infile:
    infile.read(mesh)
    
mvc = MeshValueCollection("size_t", mesh, 2) 

with XDMFFile("facet_SLICE.xdmf") as infile:
    infile.read(mvc, "name_to_read")
    
mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)

I get the error

Traceback (most recent call last):
  File "/mnt/c/Users/quent/Documents/Codes/FENICS/CaseSetup/HeatTransfer/SliceCalculation_FirstGo/OneSliceCalculus.py", line 25, in <module>
    mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
         ^^^^^^^^
AttributeError: module 'mshr.cpp' has no attribute 'mesh'

inspired from other scripts, I tried to declare something like

mf = MeshFunction("size_t",mesh, 2)

but I cannot find a way to get the mvc values into mf …

Please include the full code, with imports, as this is likely due to the use of multiple wildcard imports within the same script.

here it is

import numpy as np
from fenics import *
from dolfin import *
from mshr import *


tol=1E-8

# read a mesh properly written
mesh = Mesh()
with XDMFFile("SLICE.xdmf") as infile:
    infile.read(mesh)
    
mvc = MeshValueCollection("size_t", mesh, 1) 

with XDMFFile("facet_SLICE.xdmf") as infile:
    infile.read(mvc, "name_to_read")
    
mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
```

indeed only importing fenics solve the problem thanks

This should be reduced to
from dolfin import *, as fenics and dolfin is the same package.

1 Like

Dear Dokken. When I try to open this tutorial it shows that “File not found”. Could you send the link again?

Not sure what link you are referring to

https://jsdokken.com/converted_files/tutorial_pygmsh.html This link.

Try Mesh generation and conversion with GMSH and PYGMSH | Jørgen S. Dokken

1 Like

Dear @dokken how we can merge two meshes together for getting one XMl file?

Hi dokken,

I tried this but unfortunately it didn’t work for my case. My python script and the error message can be found below. What mistakes did I make?

from future import print_function
from fenics import *
from dolfin import *
from ufl import nabla_div
from mpi4py import MPI
import numpy as np
import meshio

Scaled variables

E = 2.5e11
nv = 0.27

lambda_ = E * nv / ((1 + nv) * (1 - 2 * nv))
mu = 0.5 * E / (1 + nv)

Create mesh and define function space

#mesh = BoxMesh(Point(-1, -1, -1), Point(1, 1, 1), 10, 10, 10)

msh = meshio.read(“S1_V8.msh”)
meshio.write(“mesh.xdmf”, meshio.Mesh(points=msh.points, cells={“tetra”: msh.cells[“tetra”]}))
meshio.write(“mf.xdmf”, meshio.Mesh(points=msh.points, cells={“triangle”: msh.cells[“triangle”]},
cell_data={“triangle”: {“name_to_read”: msh.cell_data[“triangle”][“gmsh:physical”]}}))

meshio.write(“cf.xdmf”, meshio.Mesh(
points=msh.points, cells={“tetra”: msh.cells[“tetra”]},
cell_data={“tetra”: {“name_to_read”: msh.cell_data[“tetra”][“gmsh:physical”]}}))

mesh = Mesh()
with XDMFFile(“mesh.xdmf”) as infile:
infile.read(mesh)
mvc = MeshValueCollection(“size_t”, mesh, 2)
with XDMFFile(“mf.xdmf”) as infile:
infile.read(mvc, “name_to_read”)
mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)

#meshio.write(“S1_V8.xml”,msh)
#mesh = Mesh(“mesh.xdmf”)

V = VectorFunctionSpace(mesh, ‘P’, 1)

As referred to in the previous post:

The syntax has updated:

def create_mesh(mesh, cell_type, prune_z=False):
    cells = mesh.get_cells_type(cell_type)
    cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
    points = mesh.points[:, :2] if prune_z else mesh.points
    out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={
                           "name_to_read": [cell_data]})
    return out_mesh
1 Like

I updated my codes and now I got another issue saying "KeyError:‘gmsh:physical’ ". My msh file is generated from gmsh based on a stl file. My python scrip for gmsh is pasted below.

import gmsh
import sys

gmsh.initialize()
gmsh.merge(‘S1_V8.stl’)

s = gmsh.model.getEntities(2)
l = gmsh.model.geo.addSurfaceLoop([e[1] for e in s])
V0 = gmsh.model.geo.addVolume([l])
gmsh.model.geo.synchronize()

gmsh.model.addPhysicalGroup(3, [V0], 1)

gmsh.model.mesh.generate(3)

#obtain the model dimension
xmin, ymin, zmin, xmax, ymax, zmax = gmsh.model.getBoundingBox(-1, -1)

gmsh.write(“S1_V8_tests.msh”)
gmsh.finalize()

What read command did you now use?
Could you print mesh_from_file and mesh_from_file.cell_data

1 Like

I modified something and also tried to print those as you suggested. The read codes and the outputs are:


The good news is that there is cell data in your grid, marked with “gmsh:physical”, so the error you had in:

does not make sense, and as you can see from your new code, you get way further, as you get to the write stage. Please remove the “tetra_mesh.xdmf” and “tetra_mesh.h5” file and try to run the script again.

1 Like