Files
Dean Huang e9bd61b56f start
2025-12-08 10:11:12 +08:00

161 lines
4.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visualizing DEVSIM results in a Jupyter Notebook\n",
"\n",
"Excellent standalone software exist to visualize meshes on results such as [VisIt](https://visit-dav.github.io/visit-website/index.html) and [Paraview](https://www.paraview.org/). \n",
"\n",
"However, it is sometimes useful to interweave code and visualization, for instance when debugging.\n",
"\n",
"There are many ways to do this. Here, we use [PyVista](https://docs.pyvista.org/) with the [panel](https://panel.holoviz.org/) backend.\n",
"\n",
"### Installing PyVista\n",
"\n",
"You will need to install the requisite packages:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"install_pyvista = True # True to install\n",
"package_manager = 'conda' # or 'pip'\n",
"\n",
"if install_pyvista:\n",
" if package_manager=='conda':\n",
" %conda install --yes -c conda-forge pyvista\n",
" %conda install --yes -c conda-forge panel\n",
" %conda install -c conda-forge wurlitzer \n",
" elif package_manager=='pip':\n",
" %pip install pyvista\n",
" %pip install panel\n",
" %pip install wurlitzer"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generating mesh data\n",
"\n",
"A DEVSIM simulation's mesh data can be saved to a `.dat` file with the [devsim.write_devices](https://devsim.net/CommandReference.html#devsim.write_devices) function with the `type='tecplot'` argument. \n",
"\n",
"In this examples directory, the `gmsh_diode2d.py` and `gmsh_diode3d.py` scripts can perform this and generate, respectively, `gmsh_diode2d.dat` and `gmsh_diode3d_dd.dat`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"run = True # True to run the scripts\n",
"\n",
"if run:\n",
" !python ../gmsh_diode2d.py\n",
" !python ../gmsh_diode3d.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2D Visualization\n",
"\n",
"We can load the `.dat` data and visualize it in the notebook. Specifically, we will load the `xy` plane, and display the \"Electrons\" field:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pyvista as pv\n",
"from wurlitzer import pipes # To suppress some vtk output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with pipes() as (out, err):\n",
" reader = pv.get_reader('../gmsh_diode2d.dat')\n",
" mesh = reader.read()\n",
" plotter = pv.Plotter(notebook=True)\n",
" _ = plotter.add_mesh(mesh, scalars='Electrons', log_scale=True, cmap='RdBu')\n",
" _ = plotter.show_grid()\n",
" _ = plotter.camera_position = \"xy\"\n",
" _ = plotter.show(jupyter_backend='panel')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3D Visualization\n",
"\n",
"In 3D, we choose the `panel` backend for interactive visualization:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with pipes() as (out, err):\n",
" reader = pv.get_reader('../gmsh_diode3d_dd.dat')\n",
" mesh = reader.read()\n",
" plotter = pv.Plotter(notebook=True)\n",
" _ = plotter.add_mesh(mesh, scalars='Electrons', log_scale=True, cmap='RdBu')\n",
" _ = plotter.show_grid()\n",
" _ = plotter.show(jupyter_backend='panel')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Going further\n",
"\n",
"There are many more plotting options and examples in the [pyvista examples](https://docs.pyvista.org/examples/index.html)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.5 ('devsimscratch': conda)",
"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.10.5"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "64c133f3a1967b296905f90b4858707aa8b6457960082c15c2ca47266618a04c"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}