{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pyvista\npyvista.OFF_SCREEN = True\npyvista.set_jupyter_backend('static')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Plotting mode shapes\n\nA recipe for rendering the lowest few modes of a structure as a grid\nof sub-plots.  The key ingredients are\n:func:`femorph_solver.io.modal_result_to_grid` (which scatters every\nmode onto the mesh as a pyvista point-data array) and\n:meth:`pyvista.DataSet.warp_by_vector` (which deforms the mesh by the\nmode shape scaled to a fixed fraction of the bounding box).\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import annotations\n\nimport numpy as np\nimport pyvista as pv\n\nimport femorph_solver\nfrom femorph_solver import ELEMENTS"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Build a quick cantilever plate + solve\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "LX, LY, LZ = 1.0, 1.0, 0.01\ngrid = pv.StructuredGrid(\n    *np.meshgrid(\n        np.linspace(0.0, LX, 21),\n        np.linspace(0.0, LY, 21),\n        np.linspace(0.0, LZ, 3),\n        indexing=\"ij\",\n    )\n).cast_to_unstructured_grid()\n\nm = femorph_solver.Model.from_grid(grid)\nm.assign(ELEMENTS.HEX8, material={\"EX\": 2.0e11, \"PRXY\": 0.30, \"DENS\": 7850.0})\n\npts = np.asarray(grid.points)\nnode_nums = np.asarray(grid.point_data[\"ansys_node_num\"])\nm.fix(nodes=node_nums[pts[:, 0] < 1e-9].tolist(), dof=\"ALL\")\n\nres = m.solve_modal(n_modes=6)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Attach every mode to the grid\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "grid_plot = femorph_solver.io.modal_result_to_grid(m, res)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Render modes 1..6 in a 2 \u00d7 3 viewport grid\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plotter = pv.Plotter(shape=(2, 3), off_screen=True, window_size=(1200, 600))\nfor idx in range(6):\n    row, col = divmod(idx, 3)\n    plotter.subplot(row, col)\n    phi_k = grid_plot.point_data[f\"mode_{idx + 1}_disp\"]\n    factor = 0.1 / (np.max(np.abs(phi_k)) + 1e-30)\n    plotter.add_mesh(grid_plot, style=\"wireframe\", color=\"gray\", opacity=0.3)\n    plotter.add_mesh(\n        grid_plot.warp_by_vector(f\"mode_{idx + 1}_disp\", factor=factor),\n        scalars=f\"mode_{idx + 1}_magnitude\",\n        cmap=\"viridis\",\n        show_scalar_bar=False,\n    )\n    plotter.add_text(\n        f\"mode {idx + 1}: {res.frequency[idx]:.1f} Hz\",\n        position=\"upper_edge\",\n        font_size=10,\n    )\nplotter.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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.12.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}