I am trying to convert dolfin mesh in xdmf to .msh so that I could use the result in other applications.
But I have a problem writing the scripts.
Without an minimal effort/code showing what you have tried so far, it is not very likely that anyone will be able to help you. Have you considered using meshio? If so, what is the issue you are facing?
I tried the following code with meshio, but i still have an error reading the xdmf file at the beginning.
The file contains multiple grids under time series.
Here is the code:
import meshio
import h5py # For reading HDF5 data
Replace these file paths with your XDMF and Gmsh (MSH) file paths
xdmf_file = “input.xdmf”
msh_file = “output.msh”
Read the XDMF mesh
mesh = meshio.read(xdmf_file)
Create a Gmsh object
gmsh_mesh = meshio.Mesh()
Loop through each grid in the XDMF file
for grid in mesh.cells:
if “mesh” in grid.name:
topology_data_item = grid.data
topology_h5_path = topology_data_item[0] # Assuming it’s in the form “density.h5:…/topology”
topology_dataset = h5py.File(topology_h5_path.split(“:”)[0], “r”)[topology_h5_path.split(“:”)[1]]
gmsh_mesh.cells.append((grid.type, topology_dataset))
gmsh_mesh.cell_data["name"] = {"xHeavi": mesh.cell_data["xHeavi"]}
Write the mesh to Gmsh format
meshio.write(msh_file, gmsh_mesh)
print(f"Converted {xdmf_file} to {msh_file}")