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.

 1from BasicTools.Containers import UnstructuredMesh
 2from BasicTools.IO.XdmfWriter import WriteMeshToXdmf
 3from BasicTools.Containers import UnstructuredMeshCreationTools as UMCT
 4from scipy.spatial import Delaunay
 5import numpy as np
 6
 7def create_mesh_example() -> UnstructuredMesh:
 8    """This function creates a dummy mesh using Delaunay triangulation.
 9    """
10    # Create a grid of points
11    x, y = np.meshgrid(np.linspace(0,1,10), np.linspace(0,1,10))
12    points = np.stack([x.ravel(), y.ravel()], axis=1)
13    # Generate the triangles with Delaunay
14    tri = Delaunay(points)
15    triangles = tri.simplices
16    # Create a BasicTools UnstructuredMesh using the CreateMeshOfTriangles utility
17    mesh = UMCT.CreateMeshOfTriangles(points, triangles)
18    return mesh
19
20if __name__ == "__main__":
21
22    # Create a simple mesh
23    mesh = create_mesh_example()
24    
25    # Make six dummy nodal fields
26    fields = np.random.randn(mesh.GetNumberOfNodes(), 6)
27    
28    # Dump the mesh and nodal fields into a XDMF file
29    WriteMeshToXdmf(filename="WriteXdmf.xdmf",                          # path where the file will be stored
30                    baseMeshObject=mesh,                                # UnstructuredMesh object 
31                    PointFields=[fields[:,i] for i in range(6)],        # list of scalar fields
32                    PointFieldsNames=[f"field_{i}" for i in range(6)],  # list of names for each scalar field
33    )