{ "cells": [ { "cell_type": "markdown", "id": "a69faa87-ef81-4a54-8179-205bb045bb7c", "metadata": {}, "source": [ "# Distance computation from a surface mesh" ] }, { "cell_type": "markdown", "id": "c0afb4ea-5520-4f29-91a8-c3ddc4d057c0", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": null, "id": "1de44921-e995-4884-ac88-c18405825100", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pyvista as pv\n", "pv.global_theme._jupyter_backend = 'panel' # remove this line to get interactive 3D plots\n", "from Muscat.Simple import *\n", "from Muscat.Containers.MeshInspectionTools import ExtractElementsByElementFilter\n", "from Muscat.Bridges.PyVistaBridge import MeshToPyVista\n", "from Muscat.Containers.MeshCreationTools import CreateDisk\n", "from Muscat.Containers.MeshFieldOperations import GetFieldTransferOp\n", "from Muscat.Containers.MeshTools import GetElementsCenters" ] }, { "cell_type": "markdown", "id": "b92e0301-6648-4dda-b093-f1c81e4cdf7a", "metadata": {}, "source": [ "## Mesh Creation and Target Points" ] }, { "cell_type": "code", "execution_count": null, "id": "06e3b9d7-33cd-471f-a474-4c89edbf9940", "metadata": {}, "outputs": [], "source": [ "# create a mesh and be add and extra zeros to obtain a mesh in 3D\n", "diskMesh = CreateDisk(nr = 10, nTheta= 10, r0=0.5, r1= 1., theta0= 0, theta1=np.pi/2, ofTriangles=True)\n", "diskMesh.nodes = np.hstack((diskMesh.nodes,np.zeros((diskMesh.GetNumberOfNodes(),1), dtype=diskMesh.nodes.dtype ) ) )" ] }, { "cell_type": "code", "execution_count": null, "id": "d7bb415e-943b-4672-954e-0711ce9a562a", "metadata": {}, "outputs": [], "source": [ "# define some target points\n", "TargetPoints = np.array(\n", " [[1.0,1.0,0],\n", " [0.2,0.6,0.6],\n", " [-.2,0.8,0],\n", " [0.6,0.2,0.0],\n", " ])" ] }, { "cell_type": "markdown", "id": "0fb1b677-520f-4d1c-89cf-1bf231ea97d4", "metadata": {}, "source": [ "## Transfer Operator" ] }, { "cell_type": "markdown", "id": "25033b93-7af2-4f55-8983-58502e1e2f3f", "metadata": {}, "source": [ "Compute the transfer operator from an isoparametric field mesh to the target points.\n", "\n", "- By default the FEField is isoparametric (number of nodes == number of dofs)\n", "- By default the method is \"Interp/Clamp\"" ] }, { "cell_type": "code", "execution_count": null, "id": "40d22fec-ad0c-429f-936b-8e4467ccaae6", "metadata": {}, "outputs": [], "source": [ "P, status, entities = GetFieldTransferOp(FEField('input',diskMesh),TargetPoints)" ] }, { "cell_type": "markdown", "id": "e7b811db-8f66-42c8-b58a-296e5497d9f9", "metadata": {}, "source": [ "## Extract Used Elements and Compute Elements Centers " ] }, { "cell_type": "code", "execution_count": null, "id": "63abb2ef-b790-4b48-bada-e5071cd5880a", "metadata": {}, "outputs": [], "source": [ "# compute the position of the information extraction\n", "dataExtractionPoints = P.dot(diskMesh.nodes)\n", "# generate a mask to plot elements used for the ext\n", "mask =np.zeros(diskMesh.GetNumberOfElements(), dtype=bool)\n", "# be sure to put true only in the case of 1: \"Interp\"=1 or \"Clamp\"=3\n", "mask[entities] = np.logical_or(status ==1,status == 3)\n", "# extract elements using the mask\n", "usedElements = ExtractElementsByElementFilter(diskMesh, ElementFilter(eMask=mask))\n", "#compute the center of the used elements\n", "elementCenters = GetElementsCenters(usedElements)\n" ] }, { "cell_type": "markdown", "id": "889f7ac4-dda7-431f-b99c-de4e97e6373f", "metadata": {}, "source": [ "## Console Output" ] }, { "cell_type": "code", "execution_count": null, "id": "615d2d0f-dc86-4ca6-8986-5511ba5fd495", "metadata": {}, "outputs": [], "source": [ "np.set_printoptions(precision=3,suppress=True)\n", "print(f\"Number of target points: {TargetPoints.shape[0]}\")\n", "print(\"Position if the location to extract the information \")\n", "print(dataExtractionPoints)\n", "print(f\"Status : {status.flatten()}\")\n", "print(f\"Entities : {entities.flatten()}\")" ] }, { "cell_type": "markdown", "id": "5ece9460-ed2d-4dcc-809a-a15a60a69f4e", "metadata": {}, "source": [ "## Graphical Output " ] }, { "cell_type": "code", "execution_count": null, "id": "577d7fe1-85bb-4040-99a6-e357977805b4", "metadata": {}, "outputs": [], "source": [ "p = pv.Plotter()\n", "p.add_mesh(MeshToPyVista(diskMesh),style=\"surface\",show_edges=True)\n", "p.add_mesh(MeshToPyVista(usedElements), line_width=5,color=\"RED\")\n", "for tp in range(TargetPoints.shape[0]):\n", " p.add_ruler(dataExtractionPoints[tp,:], TargetPoints[tp,:], number_labels=2, show_labels=False, title=\"\")\n", " #p.add_mesh(pv.Sphere(radius=0.001, center= TargetPoints[tp,:]))\n", " p.add_points(TargetPoints[tp,:],style=\"points\")\n", " p.add_points(TargetPoints, render_points_as_spheres=True, point_size=1)\n", " p.add_point_labels(TargetPoints, labels= list(map(lambda x: \"Target Point:\"+str(x),range(TargetPoints.shape[0]))),shape_opacity=0.5, bold=False)\n", " p.add_point_labels(elementCenters, labels= list(map(lambda x : \"Source Element:\" +str(x) ,entities.flatten())),point_size=5, shape_opacity=0.5,justification_vertical=\"top\",justification_horizontal=\"right\",fill_shape=False,shape=None, bold=False)\n", "p.show()" ] } ], "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" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "0ad6b6964bc94e04b178fa6bdff4306e": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "0ca0741127ca4f00b281d6209585f3c5": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLStyleModel", "state": { "description_width": "", "font_size": null, "text_color": null } }, "115ff364d54f48459a3e5cdd933221bc": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLModel", "state": { "layout": "IPY_MODEL_402619bb773a4638bea12afc9ed31ddd", "style": "IPY_MODEL_2e7f521310b64f9c84e8b61472f9de29", "value": "" } }, "1274e3d6d52f4f5da650b7e12c48cddf": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "29ad252eecf94b5b8cd96b2d98d10f1c": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLStyleModel", "state": { "description_width": "", "font_size": null, "text_color": null } }, "2c2cf398cf9a4e5cad4ddb41ab052a8d": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "2e7f521310b64f9c84e8b61472f9de29": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLStyleModel", "state": { "description_width": "", "font_size": null, "text_color": null } }, "3081904fc8af4fd588dfc219e0e79ff3": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLStyleModel", "state": { "description_width": "", "font_size": null, "text_color": null } }, "402619bb773a4638bea12afc9ed31ddd": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "41e8004cc50547f2ad6b232850da0248": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "4784afb52eea4934a781147b02853520": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "4847f791c69e42979bae07ea1f897c87": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLStyleModel", "state": { "description_width": "", "font_size": null, "text_color": null } }, "4c71aa0209124e4a9aac93fa1b11064b": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLModel", "state": { "layout": "IPY_MODEL_9133b0a085f34c35824ed61ebcf02d9a", "style": "IPY_MODEL_a0f0220c23ca41529695926531aa1326", "value": "" } }, "664f24b62c0a4603a10079dd6af11035": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLStyleModel", "state": { "description_width": "", "font_size": null, "text_color": null } }, "6fa299cad8784b438ecbaca9cdf6a9ab": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLModel", "state": { "layout": "IPY_MODEL_4784afb52eea4934a781147b02853520", "style": "IPY_MODEL_3081904fc8af4fd588dfc219e0e79ff3", "value": "" } }, "8ea3971d0131494b9e908dae0a4743d8": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLModel", "state": { "layout": "IPY_MODEL_1274e3d6d52f4f5da650b7e12c48cddf", "style": "IPY_MODEL_0ca0741127ca4f00b281d6209585f3c5", "value": "" } }, "911a3cf0298d41c3945a8654545ae98f": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLModel", "state": { "layout": "IPY_MODEL_a5f7b0acc4464e9fad7666f38f22d505", "style": "IPY_MODEL_edbcaba1de234543b4eae485fa286675", "value": "" } }, "9133b0a085f34c35824ed61ebcf02d9a": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "97deadeca22b4908afd91708a8cf2749": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLModel", "state": { "layout": "IPY_MODEL_2c2cf398cf9a4e5cad4ddb41ab052a8d", "style": "IPY_MODEL_664f24b62c0a4603a10079dd6af11035", "value": "" } }, "a0f0220c23ca41529695926531aa1326": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLStyleModel", "state": { "description_width": "", "font_size": null, "text_color": null } }, "a5f7b0acc4464e9fad7666f38f22d505": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "bc2dbb06483b4622844935307612d90b": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": {} }, "d596fab2890444cbbfb875dafe62123e": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLModel", "state": { "layout": "IPY_MODEL_0ad6b6964bc94e04b178fa6bdff4306e", "style": "IPY_MODEL_4847f791c69e42979bae07ea1f897c87", "value": "" } }, "eb4be0ff2bce47a3b144bc641af7dabf": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLStyleModel", "state": { "description_width": "", "font_size": null, "text_color": null } }, "edbcaba1de234543b4eae485fa286675": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLStyleModel", "state": { "description_width": "", "font_size": null, "text_color": null } }, "f828e2467c7147a68839ff8a8840e724": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLModel", "state": { "layout": "IPY_MODEL_41e8004cc50547f2ad6b232850da0248", "style": "IPY_MODEL_29ad252eecf94b5b8cd96b2d98d10f1c", "value": "" } }, "fe427b2912844e01b761ed5be3eff1e4": { "model_module": "@jupyter-widgets/controls", "model_module_version": "2.0.0", "model_name": "HTMLModel", "state": { "layout": "IPY_MODEL_bc2dbb06483b4622844935307612d90b", "style": "IPY_MODEL_eb4be0ff2bce47a3b144bc641af7dabf", "value": "" } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }