Issue with MED to XDMF Conversion in FESTIM Workshop 8

Hello,

I’m learing how to use FESTIM and currently working on Workshop 8. I downloaded the mesh.med file from GitHub, but when I run the script, it doesn’t work.
Here’s the code I’m using:

Import meshio

def convert_med_to_xdmf(
    med_file,
    cell_file="mesh_domains.xdmf",
    facet_file="mesh_boundaries.xdmf",
    cell_type="tetra",
    facet_type="triangle",
):
    """Converts a MED mesh to XDMF
    Args:
        med_file (str): the name of the MED file
        cell_file (str, optional): the name of the file containing the
            volume markers. Defaults to "mesh_domains.xdmf".
        facet_file (str, optional): the name of the file containing the
            surface markers.. Defaults to "mesh_boundaries.xdmf".
        cell_type (str, optional): The topology of the cells. Defaults to "tetra".
        facet_type (str, optional): The topology of the facets. Defaults to "triangle".
    Returns:
        dict, dict: the correspondance dict, the cell types
    """
    msh = meshio.read(med_file)

    correspondance_dict = msh.cell_tags

    cell_data_types = msh.cell_data_dict["cell_tags"].keys()

    for mesh_block in msh.cells:
        if mesh_block.type == cell_type:

            meshio.write_points_cells(
                cell_file,
                msh.points,
                [mesh_block],
                cell_data={"f": [-1 * msh.cell_data_dict["cell_tags"][cell_type]]},
            )
        elif mesh_block.type == facet_type:
            meshio.write_points_cells(
                facet_file,
                msh.points,
                [mesh_block],
                cell_data={"f": [-1 * msh.cell_data_dict["cell_tags"][facet_type]]},
            )

    return correspondance_dict, cell_data_types
correspondance_dict, cell_data_types = convert_med_to_xdmf("mesh.med", cell_file="mesh_domains.xdmf", facet_file="mesh_boundaries.xdmf")

print(correspondance_dict)

However, I get the following error:

File "/home/ndrea_i_aio/festim/CAD integration.py", line 47, in <module>
  correspondance_dict, cell_data_types = convert_med_to_xdmf("mesh.med", cell_file="mesh_domains.xdmf", facet_file="mesh_boundaries.xdmf")

File "/home/ndrea_i_aio/festim/CAD integration.py", line 27, in convert_med_to_xdmf
  cell_data_types = msh.cell_data_dict["cell_tags"].keys()
KeyError: 'cell_tags'

How can I solve this issue?

Thank you in advance!

Hi @Andrea!

I could not reproduce your issue neither via pulling the Jupyter book from git, nor via making a standalone script.

Would you mind checking the downloaded med-file (or sharing it here) and the installed dependencies (e.g. environment the installation procedure of which is described on the Workshop main page)? The task also works fine in Binder

Thank you for your prompt reply!

I’m linking the downloaded .med file below.
I installed meshio using Conda, following the instruction provided on https://github.com/nschloe/meshio, and I verified that all dependencies are installed correctly.

I hope this answers all your questions—please let me know if you need anything else.
mesh.zip (1.8 MB)

@Andrea what cells do you have in your mesh? tetrahedra? triangles?

By default, cell_type defaults to "tetra" and facet_type defaults to "triangle".

For exampe, if you have a 2D mesh with triangles for cells then you need

convert_med_to_xdmf(
    med_file,
    cell_file="mesh_domains.xdmf",
    facet_file="mesh_boundaries.xdmf",
    cell_type="triangle",
    facet_type="line",
)

Also, what version of meshio are you using?

Thank you very much for your help.

I’m using version 5.3.5 of meshio. I replaced the mesh.med file from GitHub with the one used in the Binder environment, and now everything works correctly.

Thanks again for the support!

1 Like

@Andrea glad that things are now working!

The files in Github and Binder are the exact same files (because Binder is nothing more than a copy of the github repository with the installed dependencies).

The issue was somewhere else probably.

:smiley:

1 Like