festim-dev / FESTIM

Coupled hydrogen/tritium transport and heat transfer modelling using FEniCS
https://festim.readthedocs.io/en/stable/
Apache License 2.0
88 stars 22 forks source link

Provide native support for MED to XDMF conversion with meshio? #844

Open RemDelaporteMathurin opened 1 month ago

RemDelaporteMathurin commented 1 month ago

This snippet is used in a lot of places:

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

Should we consider adding it to the festim codebase (maybe in helpers)?

We would make meshio an optional dependency only required if you want to run this function:

def convert_med_to_xdmf(...):
    import meshio
RemDelaporteMathurin commented 1 month ago

@jhdark what do you think?

@XinShen-CHN do you think that would be a nice addition for newcomers?

XinShen-CHN commented 1 month ago

@XinShen-CHN do you think that would be a nice addition for newcomers?

Yes, it would be nice to newcomers. It might be clearer and more convenient to use for the new, but I think the explanation of this snippet is being well described in User's guide https://festim.readthedocs.io/en/latest/userguide/mesh.html and FESTIM workshop task 08 https://github.com/festim-dev/FESTIM-workshop/blob/main/tasks/task08.ipynb. For me, as a new beginner, this snippet is easy to use when I try to convert the .MED file.