{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Magnetostatic Fields\n", "=====================\n", "\n", "An example of using PlasmaPy's `Magnetostatic` class in `physics` subpackage.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "import astropy.units as u\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "from plasmapy.formulary import magnetostatics\n", "from plasmapy.plasma.sources import Plasma3D" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some common magnetostatic fields can be generated and added to a plasma object.\n", "A dipole\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "dipole = magnetostatics.MagneticDipole(\n", " np.array([0, 0, 1]) * u.A * u.m * u.m, np.array([0, 0, 0]) * u.m\n", ")\n", "print(dipole)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "initialize a a plasma, where the magnetic field will be calculated on\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "plasma = Plasma3D(\n", " domain_x=np.linspace(-2, 2, 30) * u.m,\n", " domain_y=np.linspace(0, 0, 1) * u.m,\n", " domain_z=np.linspace(-2, 2, 20) * u.m,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "add the dipole field to it\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "plasma.add_magnetostatic(dipole)\n", "\n", "X, Z = plasma.grid[0, :, 0, :], plasma.grid[2, :, 0, :]\n", "U = plasma.magnetic_field[0, :, 0, :].value.T # because grid uses 'ij' indexing\n", "W = plasma.magnetic_field[2, :, 0, :].value.T # because grid uses 'ij' indexing" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "plt.figure()\n", "plt.axis(\"square\")\n", "plt.xlim(-2, 2)\n", "plt.ylim(-2, 2)\n", "plt.title(\n", " \"Dipole field in x-z plane, generated by a dipole pointing in the z direction\"\n", ")\n", "plt.streamplot(plasma.x.value, plasma.z.value, U, W)" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "A circular current-carring wire (:class:`~plasmapy.formulary.magnetostatics.CircularWire`)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "cw = magnetostatics.CircularWire(\n", " np.array([0, 0, 1]), np.array([0, 0, 0]) * u.m, 1 * u.m, 1 * u.A\n", ")\n", "print(cw)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "initialize a a plasma, where the magnetic field will be calculated on\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "plasma = Plasma3D(\n", " domain_x=np.linspace(-2, 2, 30) * u.m,\n", " domain_y=np.linspace(0, 0, 1) * u.m,\n", " domain_z=np.linspace(-2, 2, 20) * u.m,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "add the circular coil field to it\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "plasma.add_magnetostatic(cw)\n", "\n", "X, Z = plasma.grid[0, :, 0, :], plasma.grid[2, :, 0, :]\n", "U = plasma.magnetic_field[0, :, 0, :].value.T # because grid uses 'ij' indexing\n", "W = plasma.magnetic_field[2, :, 0, :].value.T # because grid uses 'ij' indexing\n", "\n", "plt.figure()\n", "plt.axis(\"square\")\n", "plt.xlim(-2, 2)\n", "plt.ylim(-2, 2)\n", "plt.title(\n", " \"Circular coil field in x-z plane, generated by a circular coil in the x-y plane\"\n", ")\n", "plt.streamplot(plasma.x.value, plasma.z.value, U, W)" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "a circular wire can be described as parametric equation and converted to :class:`~plasmapy.formulary.magnetostatics.GeneralWire`\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "gw_cw = cw.to_GeneralWire()\n", "\n", "# the calculated magnetic field is close\n", "print(gw_cw.magnetic_field([0, 0, 0]) - cw.magnetic_field([0, 0, 0]))" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "A infinite straight wire (:class:`~plasmapy.formulary.magnetostatics.InfiniteStraightWire`)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "iw = magnetostatics.InfiniteStraightWire(\n", " np.array([0, 1, 0]), np.array([0, 0, 0]) * u.m, 1 * u.A\n", ")\n", "print(iw)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "initialize a a plasma, where the magnetic field will be calculated on\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "plasma = Plasma3D(\n", " domain_x=np.linspace(-2, 2, 30) * u.m,\n", " domain_y=np.linspace(0, 0, 1) * u.m,\n", " domain_z=np.linspace(-2, 2, 20) * u.m,\n", ")\n", "\n", "# add the infinite straight wire field to it\n", "plasma.add_magnetostatic(iw)\n", "\n", "X, Z = plasma.grid[0, :, 0, :], plasma.grid[2, :, 0, :]\n", "U = plasma.magnetic_field[0, :, 0, :].value.T # because grid uses 'ij' indexing\n", "W = plasma.magnetic_field[2, :, 0, :].value.T # because grid uses 'ij' indexing\n", "\n", "plt.figure()\n", "plt.title(\n", " \"Dipole field in x-z plane, generated by a infinite straight wire \"\n", " \"pointing in the y direction\"\n", ")\n", "plt.axis(\"square\")\n", "plt.xlim(-2, 2)\n", "plt.ylim(-2, 2)\n", "plt.streamplot(plasma.x.value, plasma.z.value, U, W)" ] } ], "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.8.2" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }