{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nParticle stepper\n================\n\nAn example of PlasmaPy's particle stepper class, currently in need of a rewrite\nfor speed. Currently disabled from running.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom astropy import units as u\nfrom plasmapy.classes import Plasma, Species"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initialize a plasma. This will be a source of electric and magnetic\nfields for our particles to move in.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plasma = Plasma(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)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initialize the fields. We'll take $\\vec{B}$ in the $\\hat{x}$ direction\nand $E$ in the $\\hat{y}$ direction, which gets us an $E \\times B$ drift\nin $\\hat{z}$.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "B0 = 4 * u.T\nplasma.magnetic_field[0, :, :, :] = np.ones((10, 10, 10)) * B0\n\nE0 = 2 * u.V / u.m\nplasma.electric_field[1, :, :, :] = np.ones((10, 10, 10)) * E0"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initialize the particle. We'll take one proton `p` with a timestep of\n$10^{-10}s$ and run it for 10000 iterations.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "species = Species(plasma, 'p', 1, 1, 1e-10 * u.s, 10000)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initialize the particle's velocity. We'll limit ourselves to one in the\n$\\hat{x}$ direction, parallel to the magnetic field $\\vec{B}$ - that\nway, it won't turn in the $\\hat{z}$ direction.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "V0 = 1 * (u.m / u.s)\nspecies.v[0][0] = V0"
      ]
    },
    {
      "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
      },
      "outputs": [],
      "source": [
        "species.run()\nspecies.plot_time_trajectories()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the shape of the trajectory in 3D.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "species.plot_trajectories()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "As a test, we calculate the mean velocity in the z direction from the\nvelocity and position\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "vmean = species.velocity_history[:, :, 2].mean()\nprint(f\"The calculated drift velocity is {vmean:.4f} to compare with the\"\n      f\"theoretical E0/B0 = {E0/B0:.4f}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "and from position:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "Vdrift = species.position_history[-1, 0, 2] / (species.NT * species.dt)\nnormdrift = Vdrift\nprint(f\"The calculated drift velocity from position is {normdrift:.4f}\")"
      ]
    }
  ],
  "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.6.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}