{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Proton Radiography: Creating Synthetic Proton Radiographs by Particle Tracing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Proton radiography is a diagnostic technique often used to interrogate the electric and magnetic fields inside high energy density plasmas. The area of interest is positioned between a bright source of protons and a detector plane. Electric and magnetic fields in the plasma deflect the protons, producing patterns on the detector. Since this represents a non-linear and line-integrated measurement of the fields, the interpretation of these \"proton radiographs\" is complicated.\n", "\n", "The `proton_radiography.SyntheticProtonRadiograph` class creates a synthetic proton radiographs given a grid of electric and magnetic field (produced either by simulations or analytical models). After the geometry of the problem has been set up, a particle tracing algorithm is run, pushing the protons through the field region. After all of the protons have reached the detector plane, a synthetic proton radiograph is created by making a 2D histogram in that plane." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import astropy.constants as const\n", "import astropy.units as u\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "from mpl_toolkits.mplot3d import Axes3D\n", "\n", "from plasmapy.diagnostics import proton_radiography as prad\n", "from plasmapy.plasma.grids import CartesianGrid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To illustrate the use of this package, we'll first create an example `plasmapy.plasma.grid.CartesianGrid` object and fill it with the analytical electric field produced by a sphere of Gaussian potential." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a Cartesian grid\n", "L = 1 * u.mm\n", "grid = CartesianGrid(-L, L, num=100)\n", "\n", "# Create a spherical potential with a Gaussian radial distribution\n", "radius = np.linalg.norm(grid.grid, axis=3)\n", "arg = (radius / (L / 3)).to(u.dimensionless_unscaled)\n", "potential = 2e5 * np.exp(-(arg ** 2)) * u.V\n", "\n", "# Calculate E from the potential\n", "Ex, Ey, Ez = np.gradient(potential, grid.dax0, grid.dax1, grid.dax2)\n", "Ex = -np.where(radius < L / 2, Ex, 0)\n", "Ey = -np.where(radius < L / 2, Ey, 0)\n", "Ez = -np.where(radius < L / 2, Ez, 0)\n", "\n", "# Add those quantities to the grid\n", "grid.add_quantities(E_x=Ex, E_y=Ey, E_z=Ez, phi=potential)\n", "\n", "\n", "# Plot the E-field\n", "fig = plt.figure(figsize=(8, 8))\n", "ax = fig.add_subplot(111, projection=\"3d\")\n", "ax.view_init(30, 30)\n", "\n", "# skip some points to make the vector plot intelligable\n", "s = tuple([slice(None, None, 6)] * 3)\n", "\n", "ax.quiver(\n", " grid.pts0[s].to(u.mm).value,\n", " grid.pts1[s].to(u.mm).value,\n", " grid.pts2[s].to(u.mm).value,\n", " grid[\"E_x\"][s],\n", " grid[\"E_y\"][s],\n", " grid[\"E_z\"][s],\n", " length=1e-6,\n", ")\n", "\n", "ax.set_xlabel(\"X (mm)\")\n", "ax.set_ylabel(\"Y (mm)\")\n", "ax.set_zlabel(\"Z (mm)\")\n", "ax.set_xlim(-1, 1)\n", "ax.set_ylim(-1, 1)\n", "ax.set_zlim(-1, 1)\n", "ax.set_title(\"Gaussian Potential Electric Field\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Prior to running the particle tracing algorithm, the simulation instance must be instantiated by providing some information about the setup, including the locations of the source and detector relative to the origin of the grid.\n", "\n", "\n", "\n", "The source and detector coordinates are entered as a 3-tuple in one of three coordinate systems: Cartesian ($x$, $y$, $z$), spherical ($r$, $\\theta$, $\\phi$) or cylindrical ($r$, $\\theta$, $z$). All values should be `astropy.units.Quantity` instances with units of either length or angle. The vector from the source to the detector should pass through the origin to maximize the number of particles that pass through the simulated fields." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "source = (0 * u.mm, -10 * u.mm, 0 * u.mm)\n", "detector = (0 * u.mm, 100 * u.mm, 0 * u.mm)\n", "\n", "sim = prad.SyntheticProtonRadiograph(grid, source, detector, verbose=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that, since the example grid did not include a B-field, the B-field is assumed to be zero and a warning is printed.\n", "\n", "Next, a distribution of `nparticles` simulated particles of energy `proton_energy` is created. Setting the `max_theta` parameter eliminates particles with large angles (relative to the source-detector axis) which otherwise would likely not hit the detector. Particles with angles less than $\\theta_{max}$ but greater than $\\theta_{track}$ in the setup figure above will not cross the grid. These particles are retained, but are coasted directly to the detector plane instead of being pushed through the grid.\n", "\n", "The `particle` keyword sets the type of the particles being traced. The default particle is protons, which is set here explicitly to demonstrate the use of the keyword.\n", "\n", "By default, the particle velocities are initialized with random angles (a Monte-Carlo approach) with a uniform flux per unit solid angle. However, particles can also be initialized in other ways by setting the `distribution` keyword." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.create_particles(1e5, 3 * u.MeV, max_theta=np.pi / 15 * u.rad, particle=\"p\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The simulation is now ready to run. In brief, the steps of the simulation cycle are as follows:\n", "\n", "1. Particles that will never hit the field grid are ignored (until a later step, when they will be automatically advanced to the detector plane).\n", "\n", "\n", "2. Particles are advanced to the time when the first particle enters the simulation volume. This is done in one step to save computation time.\n", "\n", "\n", "3. While particles are on the grid, the particle pusher advances them each timestep by executing the following steps:\n", "\n", " A. The fields at each particle's location are interpolated using the interpolators defined in the `plasmapy.plasma.grids.AbstractGrid` subclasses.\n", " \n", " B. The simulation timestep is automatically (and adaptively) calculated based on the proton energy, grid resolution, and field amplitudes. This timestep can be clamped or overridden by setting the `dt` keyword in the `run()` function.\n", " \n", " C. An implementation of the Boris particle push algorithm is used to advance the velocities and positions of the particles in the interpolated fields.\n", " \n", " \n", "4. After all of the particles have left the grid, all particles are advanced to the detector plane (again saving time). Particles that are headed away from the detector plane at this point are deleted, as those particles will never\n", "be detected.\n", "\n", "When the simulation runs, a progress meter will show the number of particles currently on the grid. This bar will start at zero, increase as particles enter the grid, then decrease as they leave it. When almost all particles have left the grid, the simulation ends." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "sim.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following plot illustrates that, after the simulation has ended, all particles have been advanced to the detector plane." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(figsize=(8, 8))\n", "ax = fig.add_subplot(111, projection=\"3d\")\n", "ax.view_init(30, 150)\n", "ax.set_xlabel(\"X (cm)\")\n", "ax.set_ylabel(\"Y (cm)\")\n", "ax.set_zlabel(\"Z (cm)\")\n", "\n", "# Plot the source-to-detector axis\n", "ax.quiver(\n", " sim.source[0] * 100,\n", " sim.source[1] * 100,\n", " sim.source[2] * 100,\n", " sim.detector[0] * 100,\n", " sim.detector[1] * 100,\n", " sim.detector[2] * 100,\n", " color=\"black\",\n", ")\n", "\n", "# Plot the simulation field grid volume\n", "ax.scatter(0, 0, 0, color=\"green\", marker=\"s\", linewidth=5, label=\"Simulated Fields\")\n", "\n", "# Plot the the proton source and detector plane locations\n", "ax.scatter(\n", " sim.source[0] * 100,\n", " sim.source[1] * 100,\n", " sim.source[2] * 100,\n", " color=\"red\",\n", " marker=\"*\",\n", " linewidth=5,\n", " label=\"Source\",\n", ")\n", "\n", "ax.scatter(\n", " sim.detector[0] * 100,\n", " sim.detector[1] * 100,\n", " sim.detector[2] * 100,\n", " color=\"blue\",\n", " marker=\"*\",\n", " linewidth=10,\n", " label=\"Detector\",\n", ")\n", "\n", "\n", "# Plot the final proton positions of some (not all) of the protons\n", "ind = slice(None, None, 200)\n", "ax.scatter(\n", " sim.x[ind, 0] * 100, sim.x[ind, 1] * 100, sim.x[ind, 2] * 100, label=\"Protons\",\n", ")\n", "\n", "ax.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A 'synthetic proton radiograph' can now be constructed by creating a 2D histogram of proton positions in the image plane. The synthetic radiograph function takes two keywords:\n", "\n", "- 'size' gives the locations of the lower left and upper right corners of the detector grid in image plane coordinates.\n", "\n", "- 'bins' is the number of histogram bins to be used in the horizontal and vertical directions. Using more bins creates a higher resolution image, but at the cost of more noise." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbsphinx-thumbnail": { "tooltip": "Proton Radiography" } }, "outputs": [], "source": [ "# A function to reduce repetative plotting\n", "def plot_radiograph(hax, vax, intensity):\n", " fig, ax = plt.subplots(figsize=(8, 8))\n", " plot = ax.pcolormesh(\n", " hax.to(u.cm).value, vax.to(u.cm).value, intensity.T, cmap=\"Blues_r\",\n", " )\n", " cb = fig.colorbar(plot)\n", " cb.ax.set_ylabel(\"Intensity\")\n", " ax.set_aspect(\"equal\")\n", " ax.set_xlabel(\"X (cm), Image plane\")\n", " ax.set_ylabel(\"Z (cm), Image plane\")\n", " ax.set_title(\"Synthetic Proton Radiograph\")\n", "\n", "\n", "size = np.array([[-1, 1], [-1, 1]]) * 1.5 * u.cm\n", "bins = [200, 200]\n", "hax, vax, intensity = sim.synthetic_radiograph(size=size, bins=bins)\n", "plot_radiograph(hax, vax, intensity)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As expected, the outward-pointing electric field in the sphere has deflected the protons out of the central region, leaving a dark shadow.\n", "\n", "Kugland et al. 2012 and Bott et al. 2017 define the dimensionless \"contrast parameter\" that separates different regimes of proton radiography:\n", "\n", "\\begin{equation}\n", "\\mu = \\frac{l \\alpha}{a}\n", "\\end{equation}\n", "Where $l$ is the distance from the source to the grid, $a$ is the spatial scale of the scattering electromagnetic fields, and $\\alpha$ is the particle deflection angle. The value of $\\mu$ can fall in one of three regimes:\n", "\n", "\\begin{align}\n", "\\mu &\\ll 1 \\rightarrow \\text{ linear}\\\\\n", "\\mu &< \\mu_c \\rightarrow \\text{ nonlinear injective}\\\\\n", "\\mu &> \\mu_c \\rightarrow \\text{ caustic}\\\\\n", "\\end{align}\n", "\n", "where $\\mu_c \\sim 1$ is a characteristic value at which particle paths cross, leading to the formation of bright caustics. Correctly placing a radiograph in the correct regime is necessary to determine which analysis techniques can be applied to it.\n", "\n", "The maximum deflection angle can be calculated after the simulation has run by comparing the initial and final velocity vectors of each particle" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "max_deflection = sim.max_deflection\n", "print(f\"Maximum deflection α = {np.rad2deg(max_deflection):.2f}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The spatial scale of the field constructed in this example is $\\sim$ 1 mm, and $l$ is approximately the distance from the source to the grid origin. Therefore, we can calculate the value of $\\mu$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 1 * u.mm\n", "l = np.linalg.norm(sim.source * u.m).to(u.mm)\n", "mu = l * max_deflection.value / a\n", "print(f\"a = {a}\")\n", "print(f\"l = {l:.1f}\")\n", "print(f\"μ = {mu:.2f}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which places this example in the non-linear injective regime." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Options" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For sake of comparison, here is the result achieved by setting `distribution = 'uniform'` in the `create_particles()` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.create_particles(\n", " 1e5, 3 * u.MeV, max_theta=np.pi / 15 * u.rad, distribution=\"uniform\"\n", ")\n", "sim.run()\n", "size = np.array([[-1, 1], [-1, 1]]) * 1.5 * u.cm\n", "bins = [200, 200]\n", "hax, vax, intensity = sim.synthetic_radiograph(size=size, bins=bins)\n", "plot_radiograph(hax, vax, intensity)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }