.xdmf from 2D .msh

Happy New Year everyone!

I just tried to create .xdmf files from a 2D .msh file using Python script from

https://festim.readthedocs.io/en/latest/userguide/mesh.html#converting-meshes-using-meshio

And obtained:

Traceback (most recent call last):
File “/Users/mlavrent/meshio/msh2xdmf-festim.py”, line 27, in
tetra_cells = np.concatenate(tetra_cells_list)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “<array_function internals>”, line 200, in concatenate
ValueError: need at least one array to concatenate

Does this mean the script only works with 3D .xdmf?

Yes I think this script in the documentation will only work with 3D geometries, you may have to swap all the mentions of “triangle” for “line” and “tetra”’ for “triangle”

1 Like

Happy new year @miklavrent !

As @jhdark said this script can be applied for a 2D .msh file with a few changes:

Consider this:

import meshio
import numpy as np

msh = meshio.read("my_mesh.msh")

# Initialize lists to store cells and their corresponding data
triangle_cells_list = []
line_cells_list = []
triangle_data_list = []
line_data_list = []

# Extract cell data for all types
for cell in msh.cells:
    if cell.type == "triangle":
        triangle_cells_list.append(cell.data)
    elif cell.type == "line":
        line_cells_list.append(cell.data)

# Extract physical tags
for key, data in msh.cell_data_dict["gmsh:physical"].items():
    if key == "triangle":
        triangle_data_list.append(data)
    elif key == "line":
        line_data_list.append(data)

# Concatenate all linehedral cells and their data
line_cells = np.concatenate(line_cells_list)
line_data = np.concatenate(line_data_list)

# Concatenate all triangular cells and their data
triangle_cells = np.concatenate(triangle_cells_list)
triangle_data = np.concatenate(triangle_data_list)

# Create the linehedral mesh
line_mesh = meshio.Mesh(
    points=msh.points,
    cells=[("line", line_cells)],
    cell_data={"f": [line_data]},
)

# Create the triangular mesh for the surface
triangle_mesh = meshio.Mesh(
    points=msh.points,
    cells=[("triangle", triangle_cells)],
    cell_data={"f": [triangle_data]},
)

# Write the mesh files
meshio.write("volume_mesh.xdmf", triangle_mesh)
meshio.write("surface_mesh.xdmf", line_mesh)

Let me know if that works

1 Like

Thank you James and Remi, yes it works now!

1 Like