{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Particle stepper\n", "================\n", "\n", "An example of PlasmaPy's particle stepper class, currently in need of a rewrite\n", "for speed.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "from astropy import units as u\n", "\n", "from plasmapy.formulary import ExB_drift, gyrofrequency\n", "from plasmapy.plasma import Plasma\n", "from plasmapy.simulation import ParticleTracker" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Take a look at the docs to :func:`~plasmapy.formulary.parameters.gyrofrequency` and :class:`~plasmapy.simulation.particletracker.ParticleTracker` for more information" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initialize a plasma. This will be a source of electric and magnetic\n", "fields for our particles to move in.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "plasma = Plasma(\n", " domain_x=np.linspace(-1, 1, 10) * u.m,\n", " domain_y=np.linspace(-1, 1, 10) * u.m,\n", " domain_z=np.linspace(-1, 1, 10) * u.m,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initialize the fields. We'll take B in the x direction\n", "and E in the y direction, which gets us an E cross B drift\n", "in the negative z direction.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "B0 = 4 * u.T\n", "plasma.magnetic_field[0, ...] = B0\n", "\n", "E0 = 2 * u.V / u.m\n", "plasma.electric_field[1, ...] = E0\n", "\n", "ExB_drift(plasma.electric_field[:, 0, 0, 0], plasma.magnetic_field[:, 0, 0, 0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the timestep. We'll take one proton `p`, take its gyrofrequency, invert that\n", "to get to the gyroperiod, and resolve that into 10 steps for higher accuracy." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "freq = gyrofrequency(B0, \"p\").to(u.Hz, equivalencies=u.dimensionless_angles())\n", "gyroperiod = (1 / freq).to(u.s)\n", "steps_to_gyroperiod = 50\n", "timestep = 20 * gyroperiod / steps_to_gyroperiod" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initialize the trajectory calculation.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "number_steps = steps_to_gyroperiod * int(2 * np.pi)\n", "trajectory = ParticleTracker(plasma, \"p\", 1, 1, timestep, number_steps)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We still have to initialize the particle's velocity. We'll limit ourselves to\n", "one in the x direction, parallel to the magnetic field B -\n", "that way, it won't turn in the z direction.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "trajectory.v[0][0] = 1 * (u.m / u.s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the pusher and plot the trajectory versus time.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "trajectory.run()\n", "trajectory.plot_time_trajectories()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also take a look at the trajectory in the z direction:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "trajectory.plot_time_trajectories(\"z\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or plot the shape of the trajectory in 3D:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "trajectory.plot_trajectories()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As a test, we calculate the mean velocity in the z direction from the\n", "velocity:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "vmean = trajectory.velocity_history[:, :, 2].mean()\n", "print(\n", " f\"The calculated drift velocity is {vmean:.4f} to compare with the \"\n", " f\"expected E0/B0 = {-E0/B0:.4f}\"\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.9.1" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }