{
  "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# Exporting results to VTK / ParaView\n\nOnce you've got a ``ModalResult`` / ``StaticResult``, sending it\ndownstream to ParaView / VisIt / any VTK-aware tool is a one-liner.\n:mod:`femorph_solver.io` wraps the pyvista writers so you don't have to\nhand-roll the point-data scatter.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import annotations\n\nimport tempfile\nfrom pathlib import Path\n\nimport numpy as np\nimport pyvista as pv\n\nimport femorph_solver\nfrom femorph_solver import ELEMENTS"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Build + solve a tiny plate\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "grid = pv.StructuredGrid(\n    *np.meshgrid(\n        np.linspace(0.0, 1.0, 11),\n        np.linspace(0.0, 0.3, 5),\n        np.linspace(0.0, 0.01, 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=4)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Write every mode as a ``.vtu`` snapshot\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "out_dir = Path(tempfile.mkdtemp(prefix=\"femorph_solver_docs_\"))\nvtu_path = out_dir / \"plate_modes.vtu\"\nfemorph_solver.io.write_modal_vtk(vtu_path, m, res)\nprint(f\"wrote modal snapshot \u2192 {vtu_path}  ({vtu_path.stat().st_size:,} bytes)\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Inspect the file\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "back = pv.read(str(vtu_path))\nvector_arrays = [\n    name for name in back.point_data if name.startswith(\"mode_\") and name.endswith(\"_disp\")\n]\nprint(f\"file carries {len(vector_arrays)} mode-shape vector fields:\")\nfor name in vector_arrays:\n    print(f\"  {name}: shape = {back.point_data[name].shape}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## For a single static field, the canonical pattern is the same\n:func:`femorph_solver.io.static_result_to_grid` is the static analogue\nof :func:`modal_result_to_grid`.  Example:\n\n```python\ngrid_static = femorph_solver.io.static_result_to_grid(m, static_res)\ngrid_static.save(\"static.vtu\")\n```\n"
      ]
    }
  ],
  "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
}