{ "cells": [ { "cell_type": "markdown", "metadata": { "nbpresent": { "id": "fb05e417-e23a-4ed5-b657-ca9ac0754eaf" } }, "source": [ "# Example 001: Trajectory in a Dipole\n", "\n", "In this example the electron trajectory in a dipole is calculated and plotted. Some attempt is made to explain each step along the way.\n", "\n", "We will first start by looking at the trajectory where we set the initial conditions for a particle *outside* of the dipole magnet (as is typical in the case of looking at undulators)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "nbpresent": { "id": "86836f5e-bfa4-47bf-8ec5-14ae98f85d27" } }, "outputs": [], "source": [ "# This has nothing to do with OSCARS, but it puts the matplotlib plots inline in the notebook\n", "%matplotlib inline\n", "\n", "# Import the OSCARS SR module\n", "import oscars.sr\n", "\n", "# Import basic plot utilities. You don't need these to run OSCARS, but it's used here for basic plots\n", "from oscars.plots_mpl import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "nbpresent": { "id": "cdfa2657-359e-426c-9535-e008cfc818a8" } }, "outputs": [], "source": [ "# Create a new OSCARS object. Default to 8 threads and always use the GPU if available\n", "osr = oscars.sr.sr(nthreads=8, gpu=1)" ] }, { "cell_type": "markdown", "metadata": { "nbpresent": { "id": "a2ea0adb-ed56-4be5-99cf-83de779e2a38" } }, "source": [ "## Create the dipole field\n", "\n", "Here we create the dipole field. Here *bfield* represents magnetic field [$B_x, B_y, B_z$]. The *width* is also in vector form which allows you to specify the spatial extent of the uniform field. If one component is zero then that spatial dimension is ignored (the field extends to $\\pm \\infty$ in that dimension).\n", "\n", "Typically clear_magnetic_fields() is called before adding a field in notebooks only to save time when making changes and rerunning sections of the code so it is not strictly necessary." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "nbpresent": { "id": "e51cad0c-a7a4-409c-8682-52850d6fcfb6" } }, "outputs": [], "source": [ "# Clear any existing fields (just good habit in notebook style) and add an undulator field\n", "osr.clear_bfields()\n", "osr.add_bfield_uniform(bfield=[0, -0.4, 0], width=[0, 0, 1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "nbpresent": { "id": "56a0f83d-a326-425d-8f18-6d7026088a26" } }, "outputs": [], "source": [ "# Just to check the field that we added seems visually correct\n", "plot_bfield(osr)" ] }, { "cell_type": "markdown", "metadata": { "nbpresent": { "id": "9111d2cd-8997-41b3-9923-3d46453926a3" } }, "source": [ "## Add a particle beam\n", "\n", "Now we add a simple particle beam. There are a few important things to understand about the setup of magnetic fields and particle beams, namely positions and times. You can define the initial position and time of a particle beam to be anywhere you like. The default initial time for a beam is t=0. The default initial direction is d0=[0, 0, 1]. The default initial position is x0=[0, 0, 0], but in some cases it makes more sense to specify this *before* the field of interest, in this case at z=-1 as in the first example([0, 0, -1]).\n", "\n", "One MUST specify ctstartstop. This is the start and stop time of the calculation. In this example we will start the calculation at t=0 and go to t=2 (given in units of ct) since the beam is relativistic. In this example you can specify the start time as less than 0 which is useful if you want to propogate the particle backwars in time. This is useful for instance if you have a bending magnet before the undulator that you wish to include.\n", "\n", "clear_particle_beams() is called, again for convenience, but it is not necessary.\n", "\n", "In set_particle_beam() the default particle type is 'electron', but other are possible (see documentation)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "nbpresent": { "id": "ff3a55cb-0874-47fe-b130-7410ffe0e169" } }, "outputs": [], "source": [ "# Setup beam similar to NSLSII\n", "osr.clear_particle_beams()\n", "osr.set_particle_beam(x0=[0, 0, -1], energy_GeV=3, current=0.500)\n", "\n", "# Set the start and stop times for the calculation\n", "osr.set_ctstartstop(0, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Verify input information - print all to screen\n", "osr.print_all()" ] }, { "cell_type": "markdown", "metadata": { "nbpresent": { "id": "f0cd7552-f10a-49fe-bfa0-92242a4b919e" } }, "source": [ "## Calculate Trajectory\n", "\n", "Now we calculate the trajectory and plot it. It is enough to call calculate_trajectory(). If you are doing other calculations (flux, spectra, power density) it is not necesary to call this since it is called internally." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "nbpresent": { "id": "25204804-4f9d-4eba-9a44-77276b7de233" } }, "outputs": [], "source": [ "# Run the particle trajectory calculation\n", "trajectory = osr.calculate_trajectory()\n", "\n", "# Plot the trajectory position and velocity\n", "plot_trajectory_position(trajectory)\n", "plot_trajectory_velocity(trajectory)" ] }, { "cell_type": "markdown", "metadata": { "nbpresent": { "id": "2a9ed80c-510d-461d-a7ad-3a2ffea1261e" } }, "source": [ "## Alternate Starting Position (and backward propogation)\n", "\n", "Here we set the beam *t0* in the middle of the dipole. This is convienent for some calculations.\n", "\n", "In order to propogate the particle backwards from the point given for its initial conditions one simply changes the definition of either the beam *t0* or the *ctstartstop*. Here we will change *ctstartstop* and *x0* parameters." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "nbpresent": { "id": "f921efab-9ffd-4d82-a101-20d63262d2f9" } }, "outputs": [], "source": [ "# Setup beam similar to NSLSII\n", "osr.clear_particle_beams()\n", "osr.set_particle_beam(energy_GeV=3, current=0.500)\n", "\n", "# Set the start and stop times for the calculation\n", "osr.set_ctstartstop(-1, 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "nbpresent": { "id": "517c7294-a5f5-4d07-8f68-451e2eb98d94" } }, "outputs": [], "source": [ "# Run the particle trajectory calculation\n", "trajectory = osr.calculate_trajectory()\n", "\n", "# Plot the trajectory position and velocity\n", "plot_trajectory_position(trajectory)\n", "plot_trajectory_velocity(trajectory)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "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.1" }, "nbpresent": { "slides": {}, "themes": { "default": "557276f8-b3b8-4063-9009-ab57c3135820", "theme": { "557276f8-b3b8-4063-9009-ab57c3135820": { "id": "557276f8-b3b8-4063-9009-ab57c3135820", "palette": { "19cc588f-0593-49c9-9f4b-e4d7cc113b1c": { "id": "19cc588f-0593-49c9-9f4b-e4d7cc113b1c", "rgb": [ 252, 252, 252 ] }, "31af15d2-7e15-44c5-ab5e-e04b16a89eff": { "id": "31af15d2-7e15-44c5-ab5e-e04b16a89eff", "rgb": [ 68, 68, 68 ] }, "50f92c45-a630-455b-aec3-788680ec7410": { "id": "50f92c45-a630-455b-aec3-788680ec7410", "rgb": [ 155, 177, 192 ] }, "c5cc3653-2ee1-402a-aba2-7caae1da4f6c": { "id": "c5cc3653-2ee1-402a-aba2-7caae1da4f6c", "rgb": [ 43, 126, 184 ] }, "efa7f048-9acb-414c-8b04-a26811511a21": { "id": "efa7f048-9acb-414c-8b04-a26811511a21", "rgb": [ 25.118061674008803, 73.60176211453744, 107.4819383259912 ] } }, "rules": { "blockquote": { "color": "50f92c45-a630-455b-aec3-788680ec7410" }, "code": { "font-family": "Anonymous Pro" }, "h1": { "color": "c5cc3653-2ee1-402a-aba2-7caae1da4f6c", "font-family": "Lato", "font-size": 8 }, "h2": { "color": "c5cc3653-2ee1-402a-aba2-7caae1da4f6c", "font-family": "Lato", "font-size": 6 }, "h3": { "color": "50f92c45-a630-455b-aec3-788680ec7410", "font-family": "Lato", "font-size": 5.5 }, "h4": { "color": "c5cc3653-2ee1-402a-aba2-7caae1da4f6c", "font-family": "Lato", "font-size": 5 }, "h5": { "font-family": "Lato" }, "h6": { "font-family": "Lato" }, "h7": { "font-family": "Lato" }, "pre": { "font-family": "Anonymous Pro", "font-size": 4 } }, "text-base": { "font-family": "Merriweather", "font-size": 4 } }, "adebfa95-1217-4c71-ab55-d245ea35589a": { "backgrounds": { "dc7afa04-bf90-40b1-82a5-726e3cff5267": { "background-color": "31af15d2-7e15-44c5-ab5e-e04b16a89eff", "id": "dc7afa04-bf90-40b1-82a5-726e3cff5267" } }, "id": "adebfa95-1217-4c71-ab55-d245ea35589a", "palette": { "19cc588f-0593-49c9-9f4b-e4d7cc113b1c": { "id": "19cc588f-0593-49c9-9f4b-e4d7cc113b1c", "rgb": [ 252, 252, 252 ] }, "31af15d2-7e15-44c5-ab5e-e04b16a89eff": { "id": "31af15d2-7e15-44c5-ab5e-e04b16a89eff", "rgb": [ 68, 68, 68 ] }, "50f92c45-a630-455b-aec3-788680ec7410": { "id": "50f92c45-a630-455b-aec3-788680ec7410", "rgb": [ 197, 226, 245 ] }, "c5cc3653-2ee1-402a-aba2-7caae1da4f6c": { "id": "c5cc3653-2ee1-402a-aba2-7caae1da4f6c", "rgb": [ 43, 126, 184 ] }, "efa7f048-9acb-414c-8b04-a26811511a21": { "id": "efa7f048-9acb-414c-8b04-a26811511a21", "rgb": [ 25.118061674008803, 73.60176211453744, 107.4819383259912 ] } }, "rules": { "a": { "color": "19cc588f-0593-49c9-9f4b-e4d7cc113b1c" }, "blockquote": { "color": "50f92c45-a630-455b-aec3-788680ec7410", "font-size": 3 }, "code": { "font-family": "Anonymous Pro" }, "h1": { "color": "19cc588f-0593-49c9-9f4b-e4d7cc113b1c", "font-family": "Merriweather", "font-size": 8 }, "h2": { "color": "19cc588f-0593-49c9-9f4b-e4d7cc113b1c", "font-family": "Merriweather", "font-size": 6 }, "h3": { "color": "50f92c45-a630-455b-aec3-788680ec7410", "font-family": "Lato", "font-size": 5.5 }, "h4": { "color": "c5cc3653-2ee1-402a-aba2-7caae1da4f6c", "font-family": "Lato", "font-size": 5 }, "h5": { "font-family": "Lato" }, "h6": { "font-family": "Lato" }, "h7": { "font-family": "Lato" }, "li": { "color": "50f92c45-a630-455b-aec3-788680ec7410", "font-size": 3.25 }, "pre": { "font-family": "Anonymous Pro", "font-size": 4 } }, "text-base": { "color": "19cc588f-0593-49c9-9f4b-e4d7cc113b1c", "font-family": "Lato", "font-size": 4 } } } } } }, "nbformat": 4, "nbformat_minor": 1 }