{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Plot a Magnetic Field {#magnetic_fields_example}\n\nThe following example demonstrates how PyVista can be used to plot a\nmagnetic field.\n\nThis example relies on `streamlines_from_source()\n<pyvista.PolyDataFilters.streamlines_from_source>`{.interpreted-text\nrole=\"func\"} to generate streamlines and\n`add_volume() <pyvista.Plotter.add_volume>`{.interpreted-text\nrole=\"func\"} to plot the strength of the magnetic field.\n\nThis dataset was created from the [Coil Field\nLines](https://magpylib.readthedocs.io/en/latest/examples/examples_30_coil_field_lines.html)\nexample from the awesome\n[magpylib](https://github.com/magpylib/magpylib) library.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nimport pyvista as pv\nfrom pyvista import examples"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Download the DataSet\n\nLet\\'s first download the example dataset and show that it\\'s a\n`pyvista.ImageData`{.interpreted-text role=\"class\"} with the magnetic\nfield stored as the `'B'` array in `point_data`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "grid = examples.download_coil_magnetic_field()\ngrid.point_data"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Create Coils\n\nCreate several hoops to represent the coil. This matches the geometry in\nthe original example.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "coils = [pv.Polygon((0, 0, z), radius=5, n_sides=100, fill=False) for z in np.linspace(-8, 8, 16)]\ncoil_block = pv.MultiBlock(coils)\ncoil_block.plot(render_lines_as_tubes=True, line_width=10)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Compute and Plot Field Lines\n\nNext, let\\'s compute streamlines from the center of the coil to\nrepresent the direction of the magnetic force. For this, we can create a\nsimple `pyvista.Disc`{.interpreted-text role=\"func\"} and use that as the\nsource of the streamlines.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "seed = pv.Disc(inner=1, outer=5.4, r_res=2, c_res=12)\nstrl = grid.streamlines_from_source(\n    seed,\n    vectors='B',\n    max_time=180,\n    initial_step_length=0.1,\n    integration_direction='both',\n)\n\npl = pv.Plotter()\npl.add_mesh(\n    strl.tube(radius=0.1),\n    cmap='bwr',\n    ambient=0.2,\n)\npl.add_mesh(coil_block, render_lines_as_tubes=True, line_width=5, color='w')\npl.camera.zoom(3)\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Plot the Magnet Field Strength\n\nFinally, let\\'s bring this all together by plotting the magnetic field\nstrength while also plotting the streamlines and the coil.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Take the norm of the magnetic field\nscalars = np.linalg.norm(grid['B'], axis=1)\n\n# Customize the opacity to make it easier to visualize the strength of the\n# field nearby the coil\nopacity = 1 - np.geomspace(1.0, 0.05, 10)\n\n\n\n# Add this all to the plotter\npl = pv.Plotter()\npl.add_mesh(\n    strl.tube(radius=0.1),\n    color='black',\n)\npl.add_mesh(coil_block, render_lines_as_tubes=True, line_width=5, color='w')\nvol = pl.add_volume(\n    grid,\n    scalars=scalars,\n    opacity=opacity,\n    cmap='hot',\n    show_scalar_bar=False,\n)\nvol.prop.interpolation_type = 'linear'\npl.camera.zoom(5)\npl.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.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}