{ "cells": [ { "cell_type": "markdown", "id": "e91b8379", "metadata": {}, "source": [ "# Example of converting user data into PLAID\n", "\n", "This code provides an example for converting user data into the PLAID (Physics Informed AI Datamodel) format." ] }, { "cell_type": "code", "execution_count": null, "id": "f687989f", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from Muscat.Bridges.CGNSBridge import MeshToCGNS\n", "from Muscat.MeshTools import MeshCreationTools as MCT\n", "\n", "from plaid import Dataset\n", "from plaid import Sample" ] }, { "cell_type": "markdown", "id": "855b2d50", "metadata": {}, "source": [ "## Construction stages" ] }, { "cell_type": "code", "execution_count": null, "id": "093f94b7", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "from IPython.display import Image\n", "try:\n", " filename = Path(__file__).parent.parent / \"docs\" / \"source\" / \"images\" / \"to_plaid.png\"\n", "except NameError:\n", " filename = Path(\"..\") / \"images\" / \"to_plaid.png\"\n", "Image(filename=filename)" ] }, { "cell_type": "markdown", "id": "00c79d59", "metadata": {}, "source": [ "## Define a 3D Mesh\n", "\n", "Define nodes and triangles to create a 3D mesh." ] }, { "cell_type": "code", "execution_count": null, "id": "93c646d0", "metadata": { "lines_to_next_cell": 1 }, "outputs": [], "source": [ "nodes_3D = np.array(\n", " [\n", " [0.0, 0.0, 0.0],\n", " [1.0, 0.0, 0.0],\n", " [1.0, 1.0, 0.0],\n", " [0.0, 1.0, 0.0],\n", " [0.5, 1.5, 1.0],\n", " ]\n", ")\n", "\n", "triangles = np.array(\n", " [\n", " [0, 1, 2],\n", " [0, 1, 4],\n", " [0, 2, 3],\n", " [0, 3, 4],\n", " [1, 2, 4],\n", " [2, 4, 3],\n", " ]\n", ")\n", "\n", "print(f\"nb nodes: {len(nodes_3D)}\")\n", "print(f\"nb triangles: {len(triangles)}\")" ] }, { "cell_type": "markdown", "id": "beed5ee1", "metadata": {}, "source": [ "### Visualize the Mesh\n", "\n", "Create a 3D plot to visualize the mesh." ] }, { "cell_type": "code", "execution_count": null, "id": "f22da95d", "metadata": {}, "outputs": [], "source": [ "def in_notebook():\n", " try:\n", " from IPython import get_ipython\n", " return 'IPKernelApp' in get_ipython().config\n", " except Exception:\n", " return False\n", "\n", "# Create a 3D plot\n", "fig = plt.figure()\n", "ax = fig.add_subplot(111, projection=\"3d\")\n", "\n", "ax.scatter(nodes_3D[:, 0], nodes_3D[:, 1], nodes_3D[:, 2], c=\"b\", marker=\"o\")\n", "\n", "for triangle in triangles:\n", " triangle_nodes = nodes_3D[triangle]\n", " triangle_nodes = np.concatenate((triangle_nodes, [triangle_nodes[0]]))\n", " ax.plot(triangle_nodes[:, 0], triangle_nodes[:, 1], triangle_nodes[:, 2], c=\"g\")\n", "\n", "ax.set_xlabel(\"X\")\n", "ax.set_ylabel(\"Y\")\n", "ax.set_zlabel(\"Z\")\n", "\n", "# Show the plot\n", "if in_notebook():\n", " plt.show()" ] }, { "cell_type": "markdown", "id": "cee12cd1", "metadata": {}, "source": [ "## Create Meshes Dataset from external data\n", "\n", "Generates a dataset (python list) of 3D meshes with random fields defined over nodes and elements" ] }, { "cell_type": "code", "execution_count": null, "id": "edeb10ce", "metadata": {}, "outputs": [], "source": [ "nb_meshes = 5000\n", "meshes = []\n", "\n", "print(\"Creating meshes dataset...\")\n", "for _ in range(nb_meshes):\n", " \"\"\"Create a Unstructured mesh using only points\n", " and the connectivity matrix for the triangles.\n", " Nodes id are given by there position in the list\n", " \"\"\"\n", " Mesh = MCT.CreateMeshOfTriangles(nodes_3D, triangles)\n", "\n", " \"\"\" Add field defined over the nodes (all the nodes).\n", " The keys are the names of the fields\n", " the values are the actual data of size (nb nodes, nb of components)\"\"\"\n", " Mesh.nodeFields[\"node_field\"] = np.random.randn(5)\n", "\n", " \"\"\" Add field defined over the elements (all the elements).\n", " The keys are the names of the fields\n", " the values are the actual data of size (nb elements, nb of components)\"\"\"\n", " Mesh.elemFields[\"elem_field\"] = np.random.randn(6)\n", "\n", " meshes.append(Mesh)\n", "\n", "print(f\"{len(meshes) = }\")" ] }, { "cell_type": "markdown", "id": "11df6e1e", "metadata": {}, "source": [ "## Convert to CGNS meshes" ] }, { "cell_type": "code", "execution_count": null, "id": "49f2974c", "metadata": {}, "outputs": [], "source": [ "CGNS_meshes = []\n", "for mesh in meshes:\n", " # Converts a Mesh (muscat mesh following vtk conventions) to a CGNS Mesh\n", " CGNS_tree = MeshToCGNS(mesh)\n", " CGNS_meshes.append(CGNS_tree)\n", "\n", "print(f\"{len(CGNS_meshes) = }\")" ] }, { "cell_type": "markdown", "id": "a3decda8", "metadata": {}, "source": [ "## Create PLAID Samples from CGNS meshes" ] }, { "cell_type": "code", "execution_count": null, "id": "f3c1a90f", "metadata": {}, "outputs": [], "source": [ "in_scalars_names = [\"P\", \"p1\", \"p2\", \"p3\", \"p4\", \"p5\"]\n", "out_scalars_names = [\"max_von_mises\", \"max_q\", \"max_U2_top\", \"max_sig22_top\"]\n", "out_fields_names = [\"U1\", \"U2\", \"q\", \"sig11\", \"sig22\", \"sig12\"]\n", "\n", "samples = []\n", "for cgns_tree in CGNS_meshes:\n", " # Add CGNS Meshe to samples with specific time steps\n", " sample = Sample()\n", "\n", " sample.features.add_tree(cgns_tree)\n", "\n", " # Add random scalar values to the sample\n", " for sname in in_scalars_names:\n", " sample.add_scalar(sname, np.random.randn())\n", "\n", " for sname in out_scalars_names:\n", " sample.add_scalar(sname, np.random.randn())\n", "\n", " # Add random field values to the sample\n", " for j, sname in enumerate(out_fields_names):\n", " sample.add_field(sname, np.random.rand(1, len(nodes_3D)))\n", "\n", " samples.append(sample)\n", "\n", "print(samples[0])" ] }, { "cell_type": "markdown", "id": "e2dcc3d6", "metadata": {}, "source": [ "## Create PLAID Dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "45167e1c", "metadata": {}, "outputs": [], "source": [ "infos: dict = {\n", " \"legal\": {\"owner\": \"Bob\", \"license\": \"my_license\"},\n", " \"data_production\": {\"type\": \"simulation\", \"physics\": \"3D example\"},\n", "}\n", "\n", "\n", "dataset = Dataset()\n", "\n", "# Set information for the PLAID dataset\n", "dataset.set_infos(infos)\n", "dataset.print_infos()" ] }, { "cell_type": "code", "execution_count": null, "id": "93ddbbc8", "metadata": {}, "outputs": [], "source": [ "# Add PLAID samples to the dataset\n", "sample_ids = dataset.add_samples(samples)\n", "print(sample_ids)\n", "print(dataset)" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:percent" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }