{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import micromagneticmodel as mm\n",
    "import oommfc as mc\n",
    "\n",
    "import discretisedfield as df  # df is here chosen to be an alias for discretisedfield\n",
    "\n",
    "p1 = (0, 0, 0)\n",
    "p2 = (100e-9, 50e-9, 20e-9)\n",
    "\n",
    "region = df.Region(p1=p1, p2=p2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyvista as pv\n",
    "\n",
    "pv.set_jupyter_backend(\"trame\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.41.1'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pv.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "lx, ly, lz = 100e-9, 50e-9, 20e-9\n",
    "\n",
    "subregions = {\n",
    "    \"bottom_subregion\": df.Region(p1=(20e-9, 0, 0), p2=(40e-9, 50e-9, 10e-9)),\n",
    "    \"top_subregion\": df.Region(p1=(80e-9, 40e-9, lz / 2), p2=(lx, ly, lz)),\n",
    "}\n",
    "\n",
    "cell = (5e-9, 5e-9, 5e-9)\n",
    "\n",
    "region = df.Region(p1=(0, 0, 0), p2=(lx, ly, lz))\n",
    "mesh = df.Mesh(region=region, cell=cell, subregions=subregions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6b500274c0644f2cb7df85ab278310e4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:40295/index.html?ui=P_0x7f1c53f13f40_0&reconnect=auto' style='widt…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plotter = pv.Plotter()\n",
    "mesh.pyvista(plotter=plotter, wireframe=True)\n",
    "plotter.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>Field</strong>\n",
       "<ul>\n",
       "  \n",
       "  <li><strong>Mesh</strong>\n",
       "<ul>\n",
       "  <li><strong>Region</strong>\n",
       "<ul>\n",
       "  <li>pmin = [-1e-09, -1e-09, -1e-09]</li>\n",
       "  <li>pmax = [1e-09, 1e-09, 1e-09]</li>\n",
       "  <li>dims = ['x', 'y', 'z']</li>\n",
       "  <li>units = ['m', 'm', 'm']</li>\n",
       "</ul></li>\n",
       "  <li>n = [5, 5, 5]</li>\n",
       "  </ul></li>\n",
       "  <li>nvdim = 3</li>\n",
       "  <li>vdims:\n",
       "      <ul><li>x</li>\n",
       "        <li>y</li>\n",
       "        <li>z</li>\n",
       "        </ul>\n",
       "    </li>\n",
       "  </ul>"
      ],
      "text/plain": [
       "Field(Mesh(Region(pmin=[-1e-09, -1e-09, -1e-09], pmax=[1e-09, 1e-09, 1e-09], dims=['x', 'y', 'z'], units=['m', 'm', 'm']), n=[5, 5, 5]), nvdim=3, vdims: (x, y, z))"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a, b, c = 1e-9, 1e-9, 1e-9\n",
    "cell = (0.5e-9, 0.5e-9, 0.5e-9)\n",
    "\n",
    "mesh = df.Mesh(p1=(-a, -b, -c), p2=(a, b, c), n=(5, 5, 5))  # cell=cell)\n",
    "\n",
    "\n",
    "def norm_fun(pos):\n",
    "    x, y, z = pos\n",
    "    if (x / a) ** 2 + (y / b) ** 2 + (z / c) ** 2 <= 1:\n",
    "        return 1e6\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "\n",
    "def value_fun(pos):\n",
    "    x, y, z = pos\n",
    "    c = 1e9\n",
    "    return (-c * y, c * x, c * z)\n",
    "\n",
    "\n",
    "field = df.Field(mesh, nvdim=3, value=value_fun, norm=norm_fun, valid=\"norm\")\n",
    "field"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def test_field():\n",
    "    mesh = df.Mesh(p1=(-5e-9, -5e-9, -5e-9), p2=(5e-9, 5e-9, 5e-9), n=(5, 5, 5))\n",
    "    c_array = mesh.coordinate_field().array\n",
    "\n",
    "    # The norm is defined via numpy for performance reasons;\n",
    "    # In the simple loop form it would be:\n",
    "    # x, y, _ = point\n",
    "    # if x**2 + y**2 <= 5e-9**2:\n",
    "    #     return 1e5\n",
    "    # else:\n",
    "    #     return 0\n",
    "    def norm(points):\n",
    "        return np.where(\n",
    "            (points[..., 0] ** 2 + points[..., 1] ** 2) <= 5e-9**2, 1e5, 0\n",
    "        )[..., np.newaxis]\n",
    "\n",
    "    # Values are defined in numpy for performance reasons\n",
    "    # We define vector fields with vx=0, vy=0, vz=+/-1 for x<0 / x>0\n",
    "    def value(points):\n",
    "        res = np.zeros((*mesh.n, 3))\n",
    "        res[..., 2] = np.where(points[..., 0] <= 0, 1, -1)\n",
    "        return res\n",
    "\n",
    "    return df.Field(\n",
    "        mesh,\n",
    "        nvdim=3,\n",
    "        value=value(c_array),\n",
    "        norm=norm(c_array),\n",
    "        vdims=[\"x\", \"y\", \"z\"],\n",
    "        valid=True,  #  \"norm\",\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>Field</strong>\n",
       "<ul>\n",
       "  \n",
       "  <li><strong>Mesh</strong>\n",
       "<ul>\n",
       "  <li><strong>Region</strong>\n",
       "<ul>\n",
       "  <li>pmin = [-5e-09, -5e-09, -5e-09]</li>\n",
       "  <li>pmax = [5e-09, 5e-09, 5e-09]</li>\n",
       "  <li>dims = ['x', 'y', 'z']</li>\n",
       "  <li>units = ['m', 'm', 'm']</li>\n",
       "</ul></li>\n",
       "  <li>n = [5, 5, 5]</li>\n",
       "  </ul></li>\n",
       "  <li>nvdim = 3</li>\n",
       "  <li>vdims:\n",
       "      <ul><li>x</li>\n",
       "        <li>y</li>\n",
       "        <li>z</li>\n",
       "        </ul>\n",
       "    </li>\n",
       "  </ul>"
      ],
      "text/plain": [
       "Field(Mesh(Region(pmin=[-5e-09, -5e-09, -5e-09], pmax=[5e-09, 5e-09, 5e-09], dims=['x', 'y', 'z'], units=['m', 'm', 'm']), n=[5, 5, 5]), nvdim=3, vdims: (x, y, z))"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "field = test_field()\n",
    "field"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>Field</strong>\n",
       "<ul>\n",
       "  \n",
       "  <li><strong>Mesh</strong>\n",
       "<ul>\n",
       "  <li><strong>Region</strong>\n",
       "<ul>\n",
       "  <li>pmin = [-5e-09, -5e-09, -5e-09]</li>\n",
       "  <li>pmax = [5e-09, 5e-09, 5e-09]</li>\n",
       "  <li>dims = ['x', 'y', 'z']</li>\n",
       "  <li>units = ['m', 'm', 'm']</li>\n",
       "</ul></li>\n",
       "  <li>n = [5, 5, 5]</li>\n",
       "  </ul></li>\n",
       "  <li>nvdim = 1</li>\n",
       "  </ul>"
      ],
      "text/plain": [
       "Field(Mesh(Region(pmin=[-5e-09, -5e-09, -5e-09], pmax=[5e-09, 5e-09, 5e-09], dims=['x', 'y', 'z'], units=['m', 'm', 'm']), n=[5, 5, 5]), nvdim=1)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "field.x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>Field</strong>\n",
       "<ul>\n",
       "  \n",
       "  <li><strong>Mesh</strong>\n",
       "<ul>\n",
       "  <li><strong>Region</strong>\n",
       "<ul>\n",
       "  <li>pmin = [-1e-09, -1e-09, -1e-09]</li>\n",
       "  <li>pmax = [1e-09, 1e-09, 1e-09]</li>\n",
       "  <li>dims = ['x', 'y', 'z']</li>\n",
       "  <li>units = ['m', 'm', 'm']</li>\n",
       "</ul></li>\n",
       "  <li>n = [5, 5, 5]</li>\n",
       "  </ul></li>\n",
       "  <li>nvdim = 1</li>\n",
       "  </ul>"
      ],
      "text/plain": [
       "Field(Mesh(Region(pmin=[-1e-09, -1e-09, -1e-09], pmax=[1e-09, 1e-09, 1e-09], dims=['x', 'y', 'z'], units=['m', 'm', 'm']), n=[5, 5, 5]), nvdim=1)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "field.x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a490fbe973f34748b482a9d9272f41a4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:40295/index.html?ui=P_0x7f1b501fc490_21&reconnect=auto' style='wid…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "field.pyvista.valid()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9b164a4245404e809290c91b94ee4a64",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:40295/index.html?ui=P_0x7f1b480f5610_20&reconnect=auto' style='wid…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "field.x.pyvista.contour(isosurfaces=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/holtsamu/repos/ubermag-devtools/repos/discretisedfield/discretisedfield/field.py:1242: RuntimeWarning: invalid value encountered in divide\n",
      "  res_array = function(self.array, other)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<strong>Field</strong>\n",
       "<ul>\n",
       "  \n",
       "  <li><strong>Mesh</strong>\n",
       "<ul>\n",
       "  <li><strong>Region</strong>\n",
       "<ul>\n",
       "  <li>pmin = [-1e-09, -1e-09, -1e-09]</li>\n",
       "  <li>pmax = [1e-09, 1e-09, 1e-09]</li>\n",
       "  <li>dims = ['x', 'y', 'z']</li>\n",
       "  <li>units = ['m', 'm', 'm']</li>\n",
       "</ul></li>\n",
       "  <li>n = [4, 4, 4]</li>\n",
       "  </ul></li>\n",
       "  <li>nvdim = 1</li>\n",
       "  <li>unit = rad</li>\n",
       "  </ul>"
      ],
      "text/plain": [
       "Field(Mesh(Region(pmin=[-1e-09, -1e-09, -1e-09], pmax=[1e-09, 1e-09, 1e-09], dims=['x', 'y', 'z'], units=['m', 'm', 'm']), n=[4, 4, 4]), nvdim=1, unit=rad)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "field.angle((1, 0, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Empty meshes cannot be plotted. Input mesh has zero points.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m/home/holtsamu/repos/ubermag-devtools/repos/discretisedfield/dev/pyvista.ipynb Cell 13\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> <a href='vscode-notebook-cell:/home/holtsamu/repos/ubermag-devtools/repos/discretisedfield/dev/pyvista.ipynb#X10sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m field\u001b[39m.\u001b[39;49mpyvista\u001b[39m.\u001b[39;49mcontour(contour_kwargs\u001b[39m=\u001b[39;49m{\u001b[39m\"\u001b[39;49m\u001b[39mscalars\u001b[39;49m\u001b[39m\"\u001b[39;49m: \u001b[39m'\u001b[39;49m\u001b[39mx\u001b[39;49m\u001b[39m'\u001b[39;49m})\n",
      "File \u001b[0;32m~/repos/ubermag-devtools/repos/discretisedfield/discretisedfield/plotting/pyvista_field.py:487\u001b[0m, in \u001b[0;36mPyVistaField.contour\u001b[0;34m(self, isosurfaces, contour_scalars, plotter, multiplier, scalars, color_field, filename, contour_kwargs, **kwargs)\u001b[0m\n\u001b[1;32m    482\u001b[0m     scalars \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mcolor_field\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m    483\u001b[0m field_pv \u001b[39m=\u001b[39m field_pv\u001b[39m.\u001b[39mextract_cells(\n\u001b[1;32m    484\u001b[0m     field_pv[\u001b[39m\"\u001b[39m\u001b[39mvalid\u001b[39m\u001b[39m\"\u001b[39m]\u001b[39m.\u001b[39mastype(\u001b[39mbool\u001b[39m)\n\u001b[1;32m    485\u001b[0m )\u001b[39m.\u001b[39mcell_data_to_point_data()\n\u001b[0;32m--> 487\u001b[0m plot\u001b[39m.\u001b[39;49madd_mesh(\n\u001b[1;32m    488\u001b[0m     field_pv\u001b[39m.\u001b[39;49mcontour(isosurfaces\u001b[39m=\u001b[39;49misosurfaces, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mcontour_kwargs),\n\u001b[1;32m    489\u001b[0m     smooth_shading\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m,\n\u001b[1;32m    490\u001b[0m     scalars\u001b[39m=\u001b[39;49mscalars,\n\u001b[1;32m    491\u001b[0m     \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs,\n\u001b[1;32m    492\u001b[0m )\n\u001b[1;32m    494\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_add_empty_region(plot, multiplier, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfield\u001b[39m.\u001b[39mmesh\u001b[39m.\u001b[39mregion)\n\u001b[1;32m    495\u001b[0m plot\u001b[39m.\u001b[39menable_eye_dome_lighting()\n",
      "File \u001b[0;32m~/anaconda3/envs/ubermagdev/lib/python3.8/site-packages/pyvista/plotting/plotter.py:3433\u001b[0m, in \u001b[0;36mBasePlotter.add_mesh\u001b[0;34m(self, mesh, color, style, scalars, clim, show_edges, edge_color, point_size, line_width, opacity, flip_scalars, lighting, n_colors, interpolate_before_map, cmap, label, reset_camera, scalar_bar_args, show_scalar_bar, multi_colors, name, texture, render_points_as_spheres, render_lines_as_tubes, smooth_shading, split_sharp_edges, ambient, diffuse, specular, specular_power, nan_color, nan_opacity, culling, rgb, categories, silhouette, use_transparency, below_color, above_color, annotations, pickable, preference, log_scale, pbr, metallic, roughness, render, component, emissive, copy_mesh, backface_params, show_vertices, **kwargs)\u001b[0m\n\u001b[1;32m   3429\u001b[0m         \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m'\u001b[39m\u001b[39mRGB array must be n_points/n_cells by 3/4 in shape.\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[1;32m   3431\u001b[0m \u001b[39mif\u001b[39;00m algo \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m \u001b[39mnot\u001b[39;00m mesh\u001b[39m.\u001b[39mn_points:\n\u001b[1;32m   3432\u001b[0m     \u001b[39m# Algorithms may initialize with an empty mesh\u001b[39;00m\n\u001b[0;32m-> 3433\u001b[0m     \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m'\u001b[39m\u001b[39mEmpty meshes cannot be plotted. Input mesh has zero points.\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[1;32m   3435\u001b[0m \u001b[39m# set main values\u001b[39;00m\n\u001b[1;32m   3436\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mmesh \u001b[39m=\u001b[39m mesh\n",
      "\u001b[0;31mValueError\u001b[0m: Empty meshes cannot be plotted. Input mesh has zero points."
     ]
    }
   ],
   "source": [
    "field.pyvista.contour(contour_kwargs={\"scalars\": \"x\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "be6b1af5b90c4a138f8262f7586b43ff",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:40295/index.html?ui=P_0x7f1c327eba30_5&reconnect=auto' style='widt…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "field.y.pyvista.contour()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table style='width: 100%;'><tr><th>Header</th><th>Data Arrays</th></tr><tr><td>\n",
       "<table style='width: 100%;'>\n",
       "<tr><th>RectilinearGrid</th><th>Information</th></tr>\n",
       "<tr><td>N Cells</td><td>1920</td></tr>\n",
       "<tr><td>N Points</td><td>2457</td></tr>\n",
       "<tr><td>X Bounds</td><td>-5.000e+00, 5.000e+00</td></tr>\n",
       "<tr><td>Y Bounds</td><td>-3.000e+00, 3.000e+00</td></tr>\n",
       "<tr><td>Z Bounds</td><td>-2.000e+00, 2.000e+00</td></tr>\n",
       "<tr><td>Dimensions</td><td>21, 13, 9</td></tr>\n",
       "<tr><td>N Arrays</td><td>6</td></tr>\n",
       "</table>\n",
       "\n",
       "</td><td>\n",
       "<table style='width: 100%;'>\n",
       "<tr><th>Name</th><th>Field</th><th>Type</th><th>N Comp</th><th>Min</th><th>Max</th></tr>\n",
       "<tr><td>norm</td><td>Cells</td><td>float64</td><td>1</td><td>0.000e+00</td><td>1.000e+06</td></tr>\n",
       "<tr><td>x</td><td>Cells</td><td>float64</td><td>1</td><td>-9.918e+05</td><td>9.918e+05</td></tr>\n",
       "<tr><td>y</td><td>Cells</td><td>float64</td><td>1</td><td>-9.972e+05</td><td>9.972e+05</td></tr>\n",
       "<tr><td>z</td><td>Cells</td><td>float64</td><td>1</td><td>-9.802e+05</td><td>9.802e+05</td></tr>\n",
       "<tr><td>field</td><td>Cells</td><td>float64</td><td>3</td><td>-9.972e+05</td><td>9.972e+05</td></tr>\n",
       "<tr><td>valid</td><td>Cells</td><td>int64</td><td>1</td><td>0.000e+00</td><td>1.000e+00</td></tr>\n",
       "</table>\n",
       "\n",
       "</td></tr> </table>"
      ],
      "text/plain": [
       "RectilinearGrid (0x7f9c6be3e640)\n",
       "  N Cells:      1920\n",
       "  N Points:     2457\n",
       "  X Bounds:     -5.000e+00, 5.000e+00\n",
       "  Y Bounds:     -3.000e+00, 3.000e+00\n",
       "  Z Bounds:     -2.000e+00, 2.000e+00\n",
       "  Dimensions:   21, 13, 9\n",
       "  N Arrays:     6"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "field_pv = pv.wrap(field.to_vtk())\n",
    "field_pv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table style='width: 100%;'><tr><th>Header</th><th>Data Arrays</th></tr><tr><td>\n",
       "<table style='width: 100%;'>\n",
       "<tr><th>RectilinearGrid</th><th>Information</th></tr>\n",
       "<tr><td>N Cells</td><td>1920</td></tr>\n",
       "<tr><td>N Points</td><td>2457</td></tr>\n",
       "<tr><td>X Bounds</td><td>-5.000e+00, 5.000e+00</td></tr>\n",
       "<tr><td>Y Bounds</td><td>-3.000e+00, 3.000e+00</td></tr>\n",
       "<tr><td>Z Bounds</td><td>-2.000e+00, 2.000e+00</td></tr>\n",
       "<tr><td>Dimensions</td><td>21, 13, 9</td></tr>\n",
       "<tr><td>N Arrays</td><td>3</td></tr>\n",
       "</table>\n",
       "\n",
       "</td><td>\n",
       "<table style='width: 100%;'>\n",
       "<tr><th>Name</th><th>Field</th><th>Type</th><th>N Comp</th><th>Min</th><th>Max</th></tr>\n",
       "<tr><td>norm</td><td>Cells</td><td>float64</td><td>1</td><td>0.000e+00</td><td>9.918e+05</td></tr>\n",
       "<tr><td><b>field</b></td><td>Cells</td><td>float64</td><td>1</td><td>-9.918e+05</td><td>9.918e+05</td></tr>\n",
       "<tr><td>valid</td><td>Cells</td><td>int64</td><td>1</td><td>0.000e+00</td><td>1.000e+00</td></tr>\n",
       "</table>\n",
       "\n",
       "</td></tr> </table>"
      ],
      "text/plain": [
       "RectilinearGrid (0x7f9c6be3e580)\n",
       "  N Cells:      1920\n",
       "  N Points:     2457\n",
       "  X Bounds:     -5.000e+00, 5.000e+00\n",
       "  Y Bounds:     -3.000e+00, 3.000e+00\n",
       "  Z Bounds:     -2.000e+00, 2.000e+00\n",
       "  Dimensions:   21, 13, 9\n",
       "  N Arrays:     3"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pv.wrap(field.x.to_vtk())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table style='width: 100%;'><tr><th>Header</th><th>Data Arrays</th></tr><tr><td>\n",
       "<table style='width: 100%;'>\n",
       "<tr><th>RectilinearGrid</th><th>Information</th></tr>\n",
       "<tr><td>N Cells</td><td>1920</td></tr>\n",
       "<tr><td>N Points</td><td>2457</td></tr>\n",
       "<tr><td>X Bounds</td><td>-5.000e+00, 5.000e+00</td></tr>\n",
       "<tr><td>Y Bounds</td><td>-3.000e+00, 3.000e+00</td></tr>\n",
       "<tr><td>Z Bounds</td><td>-2.000e+00, 2.000e+00</td></tr>\n",
       "<tr><td>Dimensions</td><td>21, 13, 9</td></tr>\n",
       "<tr><td>N Arrays</td><td>7</td></tr>\n",
       "</table>\n",
       "\n",
       "</td><td>\n",
       "<table style='width: 100%;'>\n",
       "<tr><th>Name</th><th>Field</th><th>Type</th><th>N Comp</th><th>Min</th><th>Max</th></tr>\n",
       "<tr><td>norm</td><td>Cells</td><td>float64</td><td>1</td><td>0.000e+00</td><td>1.000e+06</td></tr>\n",
       "<tr><td>x</td><td>Cells</td><td>float64</td><td>1</td><td>-9.918e+05</td><td>9.918e+05</td></tr>\n",
       "<tr><td>y</td><td>Cells</td><td>float64</td><td>1</td><td>-9.972e+05</td><td>9.972e+05</td></tr>\n",
       "<tr><td>z</td><td>Cells</td><td>float64</td><td>1</td><td>-9.802e+05</td><td>9.802e+05</td></tr>\n",
       "<tr><td>field</td><td>Cells</td><td>float64</td><td>3</td><td>-9.972e+05</td><td>9.972e+05</td></tr>\n",
       "<tr><td>valid</td><td>Cells</td><td>int64</td><td>1</td><td>0.000e+00</td><td>1.000e+00</td></tr>\n",
       "<tr><td><b>color_field</b></td><td>Cells</td><td>float64</td><td>1</td><td>-9.918e+05</td><td>9.918e+05</td></tr>\n",
       "</table>\n",
       "\n",
       "</td></tr> </table>"
      ],
      "text/plain": [
       "RectilinearGrid (0x7f9c6be3e640)\n",
       "  N Cells:      1920\n",
       "  N Points:     2457\n",
       "  X Bounds:     -5.000e+00, 5.000e+00\n",
       "  Y Bounds:     -3.000e+00, 3.000e+00\n",
       "  Z Bounds:     -2.000e+00, 2.000e+00\n",
       "  Dimensions:   21, 13, 9\n",
       "  N Arrays:     7"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "field_pv[\"color_field\"] = pv.wrap(field.x.to_vtk())[\"field\"].copy()\n",
    "field_pv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ffcbfd07f5df4f178dd580de4060b811",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:39181/index.html?ui=P_0x7f9c6be32df0_2&reconnect=auto' style='widt…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "field.pyvista.vector()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "50f6a833bff546889f61a800192e4698",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:39181/index.html?ui=P_0x7f9cd80d49a0_3&reconnect=auto' style='widt…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "field.pyvista.vector(cmap=\"viridis\", color_field=field.y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c125023c97d54fe3937c12bc86056f49",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:39181/index.html?ui=P_0x7f9c6bbd0040_4&reconnect=auto' style='widt…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "field.pyvista.vector(cmap=\"viridis\", scalars=\"x\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e80b7ad201134fa494f59d5d03a7e5ab",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:39181/index.html?ui=P_0x7f9cd80d4190_5&reconnect=auto' style='widt…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "field.x.pyvista.scalar(opacity=[1, 0, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6ea6f06fa8954ef8bcfec9100f9c4070",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:39181/index.html?ui=P_0x7f9c6e48baf0_6&reconnect=auto' style='widt…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "field.pyvista.contour(isosurfaces=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ab04e0a28a45456db3ee1f8f063a205a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:39181/index.html?ui=P_0x7f9c6bbd4c70_7&reconnect=auto' style='widt…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "field.pyvista.contour(isosurfaces=10, scalars=\"y\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "df3d39d896c34d208754265edfa424e3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:39181/index.html?ui=P_0x7f9c28060250_11&reconnect=auto' style='wid…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "field.pyvista.streamlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "40f8408234d64811be3475f485357e79",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:39181/index.html?ui=P_0x7f9c28066790_12&reconnect=auto' style='wid…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "field.pyvista.valid(opacity=1, culling=\"backface\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d80458db99634df1b1389a95d6dcd89a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value=\"<iframe src='http://localhost:39181/index.html?ui=P_0x7f9c280662e0_10&reconnect=auto' style='wid…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plotter = pv.Plotter()\n",
    "field.pyvista.vector(plotter=plotter, cmap=\"viridis\")\n",
    "field.pyvista.contour(plotter=plotter, isosurfaces=10, scalars=\"x\")\n",
    "plotter.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ubermagdev",
   "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.8.17"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}