{ "cells": [ { "cell_type": "markdown", "id": "843a4434-19f8-45c6-aa7d-04cf00ba7594", "metadata": {}, "source": [ "# Dump mesh into a XDMF file" ] }, { "cell_type": "markdown", "id": "76f376da", "metadata": {}, "source": [ "This small example shows how to dump a mesh and fields into a XDMF file that can be read by ParaView." ] }, { "cell_type": "markdown", "id": "3c225025-1a1b-4b36-a559-7e2387ed727a", "metadata": {}, "source": [ "## Includes" ] }, { "cell_type": "code", "execution_count": null, "id": "53dcaa6c-8144-4c3e-8abe-f20cddc638fa", "metadata": {}, "outputs": [], "source": [ "from Muscat.MeshContainers.Mesh import Mesh\n", "from Muscat.IO.XdmfWriter import WriteMeshToXdmf\n", "from Muscat.MeshTools.MeshCreationTools import CreateMeshOfTriangles\n", "from Muscat.Helpers.IO.TemporaryDirectory import TemporaryDirectory\n", "\n", "from scipy.spatial import Delaunay\n", "import pyvista\n", "pyvista.global_theme._jupyter_backend = 'panel' # remove this line to get interactive 3D plots\n", "import numpy as np\n" ] }, { "cell_type": "markdown", "id": "c10a4dde-1643-42e2-b0e4-2680dde554b0", "metadata": {}, "source": [ "## Function to create a dummy mesh using Delaunay triangulation." ] }, { "cell_type": "code", "execution_count": null, "id": "ca175e15-8956-4a90-9c83-af5f141bdaea", "metadata": {}, "outputs": [], "source": [ "def create_mesh_example() -> Mesh:\n", "\n", " # Create a grid of points\n", " x, y = np.meshgrid(np.linspace(0,1,10), np.linspace(0,1,10))\n", " points = np.stack([x.ravel(), y.ravel()], axis=1)\n", " # Generate the triangles with Delaunay\n", " tri = Delaunay(points)\n", " triangles = tri.simplices\n", " # Create a Muscat Mesh using the CreateMeshOfTriangles utility\n", " mesh = CreateMeshOfTriangles(points, triangles)\n", " return mesh\n" ] }, { "cell_type": "markdown", "id": "75e46467-3b24-4a9d-9f9d-e689fe0560c2", "metadata": {}, "source": [ "## Create a simple mesh" ] }, { "cell_type": "code", "execution_count": null, "id": "d78c21ac-bcd4-4aed-a8da-ff8d40c63ca4", "metadata": {}, "outputs": [], "source": [ "mesh = create_mesh_example()\n" ] }, { "cell_type": "markdown", "id": "9c4d9f57-96cb-49e9-8cae-a05141b6c527", "metadata": {}, "source": [ "## Plot Mesh" ] }, { "cell_type": "code", "execution_count": null, "id": "484ee49d-8b12-4598-bbda-9d1925ae6cd3", "metadata": {}, "outputs": [], "source": [ "from Muscat.Bridges.PyVistaBridge import PlotMesh\n", "PlotMesh(mesh,show_edges=True )\n" ] }, { "cell_type": "markdown", "id": "46179072-7ecb-4ca6-85e6-fda7cba9ba30", "metadata": {}, "source": [ "## Make six dummy nodal fields" ] }, { "cell_type": "code", "execution_count": null, "id": "08ff95c0-c570-4d31-ba5a-6f6aeb1fd147", "metadata": {}, "outputs": [], "source": [ "fields = np.random.randn(mesh.GetNumberOfNodes(), 6)\n" ] }, { "cell_type": "markdown", "id": "b2ae92f8-6fac-4585-bb41-2a144da9944b", "metadata": {}, "source": [ "## Dump the mesh and nodal fields into a XDMF file" ] }, { "cell_type": "code", "execution_count": null, "id": "e48326c3-692b-40b2-bd58-dab3970d233f", "metadata": {}, "outputs": [], "source": [ "WriteMeshToXdmf(filename=TemporaryDirectory.GetTempPath() + \"WriteXdmf.xdmf\", # path where the file will be stored\n", " baseMeshObject=mesh, # Mesh object\n", " PointFields=[fields[:,i] for i in range(6)], # list of scalar fields\n", " PointFieldsNames=[f\"field_{i}\" for i in range(6)], # list of names for each scalar field\n", ")\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.10" } }, "nbformat": 4, "nbformat_minor": 5 }