Dump mesh into a XDMF file

This small example shows how to dump a mesh and fields into a XDMF file that can be read by ParaView.

Includes

[1]:
from Muscat.Containers.Mesh import Mesh
from Muscat.IO.XdmfWriter import WriteMeshToXdmf
from Muscat.Containers.MeshCreationTools import CreateMeshOfTriangles
from Muscat.Helpers.IO.TemporaryDirectory import TemporaryDirectory

from scipy.spatial import Delaunay
import pyvista
pyvista.global_theme._jupyter_backend = 'panel' # remove this line to get interactive 3D plots
import numpy as np

Function to creates a dummy mesh using Delaunay triangulation.

[2]:
def create_mesh_example() -> Mesh:

    # Create a grid of points
    x, y = np.meshgrid(np.linspace(0,1,10), np.linspace(0,1,10))
    points = np.stack([x.ravel(), y.ravel()], axis=1)
    # Generate the triangles with Delaunay
    tri = Delaunay(points)
    triangles = tri.simplices
    # Create a Muscat Mesh using the CreateMeshOfTriangles utility
    mesh = CreateMeshOfTriangles(points, triangles)
    return mesh

Create a simple mesh

[3]:
mesh = create_mesh_example()

Plot Mesh

[4]:
from Muscat.Bridges.PyVistaBridge import PlotMesh
PlotMesh(mesh,show_edges=True )
../_images/notebooks_WriteXdmf_9_0.png

Make six dummy nodal fields

[5]:
fields = np.random.randn(mesh.GetNumberOfNodes(), 6)

Dump the mesh and nodal fields into a XDMF file

[6]:
WriteMeshToXdmf(filename=TemporaryDirectory.GetTempPath() + "WriteXdmf.xdmf",  # path where the file will be stored
                    baseMeshObject=mesh,                                       # Mesh object
                    PointFields=[fields[:,i] for i in range(6)],               # list of scalar fields
                    PointFieldsNames=[f"field_{i}" for i in range(6)],         # list of names for each scalar field
)