{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Triangle Mesh" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import plotly.io as pio\n", "\n", "from lapy import Solver, TetMesh, TriaMesh, io, plot\n", "\n", "pio.renderers.default = \"sphinx_gallery\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This tutorial will show you some of our visualization functionality. For that we load a larger mesh of the cube and compute the first three eigenvalues and eigenvectors. We also show how to save the eigenfunctions to disk." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tria = TriaMesh.read_vtk(\"../data/cubeTria.vtk\")\n", "fem = Solver(tria)\n", "evals, evecs = fem.eigs(k=3)\n", "evDict = dict()\n", "evDict[\"Refine\"] = 0\n", "evDict[\"Degree\"] = 1\n", "evDict[\"Dimension\"] = 2\n", "evDict[\"Elements\"] = len(tria.t)\n", "evDict[\"DoF\"] = len(tria.v)\n", "evDict[\"NumEW\"] = 3\n", "evDict[\"Eigenvalues\"] = evals\n", "evDict[\"Eigenvectors\"] = evecs\n", "io.write_ev(\"../data/cubeTria.ev\", evDict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look at the result by visualizing the first non-constant eigenfunction on top of the cube mesh. You can see that the extrema localize in two diametrically opposed corners." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot.plot_tria_mesh(\n", " tria,\n", " vfunc=evecs[:, 1],\n", " xrange=None,\n", " yrange=None,\n", " zrange=None,\n", " showcaxis=False,\n", " caxis=None,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also adjust the axes and add a color scale." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot.plot_tria_mesh(\n", " tria,\n", " vfunc=evecs[:, 1],\n", " xrange=[-2, 2],\n", " yrange=[-2, 2],\n", " zrange=[-2, 2],\n", " showcaxis=True,\n", " caxis=[-0.3, 0.5],\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tetrahedral Mesh" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we load a tetrahedral mesh and again compute the first 3 eigenvectors." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tetra = TetMesh.read_vtk(\"../data/cubeTetra.vtk\")\n", "fem = Solver(tetra)\n", "evals, evecs = fem.eigs(k=3)\n", "evDict = dict()\n", "evDict[\"Refine\"] = 0\n", "evDict[\"Degree\"] = 1\n", "evDict[\"Dimension\"] = 2\n", "evDict[\"Elements\"] = len(tetra.t)\n", "evDict[\"DoF\"] = len(tetra.v)\n", "evDict[\"NumEW\"] = 3\n", "evDict[\"Eigenvalues\"] = evals\n", "evDict[\"Eigenvectors\"] = evecs\n", "io.write_ev(\"../data/cubeTetra.ev\", evDict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The eigenvector defines a function on all vertices, also inside the cube. Here we can see it as a color overlay on the boundary." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot.plot_tet_mesh(\n", " tetra,\n", " vfunc=evecs[:, 1],\n", " xrange=None,\n", " yrange=None,\n", " zrange=None,\n", " showcaxis=False,\n", " caxis=None,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The plot function allows cutting the solid object open (here we keep every vertex where the function is larger than 0)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot.plot_tet_mesh(\n", " tetra,\n", " cutting=(\"f>0\"),\n", " vfunc=evecs[:, 1],\n", " xrange=[-2, 2],\n", " yrange=[-2, 2],\n", " zrange=[-2, 2],\n", " showcaxis=True,\n", " caxis=[-0.3, 0.5],\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "Python3", "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" } }, "nbformat": 4, "nbformat_minor": 4 }