Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
RemDelaporteMathurin opened this issue Jul 31, 2024 · 2 comments
Open
Labels
enhancement New feature or request

Comments

@RemDelaporteMathurin
Copy link
Collaborator

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 RemDelaporteMathurin added the enhancement New feature or request label Jul 31, 2024
@RemDelaporteMathurin
Copy link
Collaborator Author

@jhdark what do you think?

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

@XinShen-CHN
Copy link
Contributor

XinShen-CHN commented Aug 1, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants