In this example the electron trajectory in a dipole is calculated and plotted. Some attempt is made to explain each step along the way.
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).
# This has nothing to do with OSCARS, but it puts the matplotlib plots inline in the notebook
%matplotlib inline
# Import the OSCARS SR module
import oscars.sr
# Import basic plot utilities. You don't need these to run OSCARS, but it's used here for basic plots
from oscars.plots_mpl import *
# Create a new OSCARS object. Default to 8 threads and always use the GPU if available
osr = oscars.sr.sr(nthreads=8, gpu=1)
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).
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.
# Clear any existing fields (just good habit in notebook style) and add an undulator field
osr.clear_bfields()
osr.add_bfield_uniform(bfield=[0, -0.4, 0], width=[0, 0, 1])
# Just to check the field that we added seems visually correct
plot_bfield(osr)
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]).
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.
clear_particle_beams() is called, again for convenience, but it is not necessary.
In set_particle_beam() the default particle type is 'electron', but other are possible (see documentation)
# Setup beam similar to NSLSII
osr.clear_particle_beams()
osr.set_particle_beam(x0=[0, 0, -1], energy_GeV=3, current=0.500)
# Set the start and stop times for the calculation
osr.set_ctstartstop(0, 2)
# Verify input information - print all to screen
osr.print_all()
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.
# Run the particle trajectory calculation
trajectory = osr.calculate_trajectory()
# Plot the trajectory position and velocity
plot_trajectory_position(trajectory)
plot_trajectory_velocity(trajectory)
Here we set the beam t0 in the middle of the dipole. This is convienent for some calculations.
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.
# Setup beam similar to NSLSII
osr.clear_particle_beams()
osr.set_particle_beam(energy_GeV=3, current=0.500)
# Set the start and stop times for the calculation
osr.set_ctstartstop(-1, 1)
# Run the particle trajectory calculation
trajectory = osr.calculate_trajectory()
# Plot the trajectory position and velocity
plot_trajectory_position(trajectory)
plot_trajectory_velocity(trajectory)