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.

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