(GPU-Batched) Dynamic Aperture¶
The dynamic aperture is the region of starting amplitudes in which particles survive a given number
of turns in a ring. The dynamic_aperture function finds its boundary in units of the beam sizes,
for several energy deviations δ, as described in the example below.
As with track, the parameters of the beamline, including
deferred expressions, are evaluated once when dynamic_aperture is called, and
kept for all turns of that call. To have parameters change during tracking, use
time-dependent parameters.
Reading the Result¶
dynamic_aperture returns a DynamicAperture. Here it is for a small ring with two sextupoles,
whose strengths are variables of the ring’s context (they are used again in the
next section):
@elements begin
qf = Quadrupole(Kn1=1.2, L=0.4)
sf = Sextupole(Kn2=DefExpr(c -> c.ksf), L=0.2)
d = Drift(L=0.3)
b = SBend(L=1.5, angle=pi/16)
qd = Quadrupole(Kn1=-1.2, L=0.4)
sd = Sextupole(Kn2=DefExpr(c -> c.ksd), L=0.2)
end
ring = Beamline(repeat([qf, sf, d, b, d, qd, sd, d, b, d], 16);
species_ref=Species("electron"), pc_ref=3e9)
ring.context.ksf = 1.66
ring.context.ksd = -2.80
da = dynamic_aperture(ring; n_turns=500, emit_1=1e-6, emit_2=1e-7, n_r=21, n_theta=9,
max_sig_x=80, max_sig_y=500, deltas=[0.0, 0.005], verbose=false)
DynamicAperture: 9 angles, 2 δ, 21 radial points (up to 80.0 sig_x, 500.0 sig_y)
δ 0.0 0.005
area 0.4453 0.4544
reach 0.005 (min_area 0.005556)
da.x[j, i] and da.y[j, i] are the boundary along the j-th angle of the grid for the i-th
δ: the starting position of the first particle lost along that angle, in units of the beam sizes.
It is Inf if no particle along the angle is lost:
da.x[:, 1] # δ = 0, from the positive x axis to the negative one
9-element Vector{Float64}:
40.0
40.650699430496616
33.941125496954285
21.430272212445033
3.429011037612589e-15
-22.961005941905388
-45.25483399593904
-51.737253820632056
-52.0
From these, the result has:
Quantity |
Meaning |
|---|---|
|
the boundary as a fraction of the radius of the grid (1 where none is lost) |
|
the area inside the boundary as a fraction of the area of the grid (the mean of |
|
the largest δ up to which |
|
the change of |
|
whether the |
min_area is a keyword argument of dynamic_aperture, by default da.resolution. The arrays
are indexed by position, e.g. da.area[end] is the area at the last δ.
The boundary is where the first particle along an angle is lost, but particles further out may survive. Plotting the result with Makie or Plots shows the whole grid, the particles that survived in green and those lost in red:
using CairoMakie
plot(da; delta=1)
Batched Dynamic Aperture Scans¶
Often one wants to do dynamic aperture scans over many different parameter values - for example,
over sextupole strengths scanned in brute-force genetic algorithm dynamic aperture optimizations.
While this can be done by spawning separate SciBmad processes for each sextupole strength setting,
one can also do parallel SciBmad dynamic aperture scans in a single process (or, on a single
GPU) using batch_dynamic_aperture. To do this, the parameter values to be scanned should be
variables set in the beamline’s context, which are then used as the elements’
parameters through deferred expressions, as ksf and ksd in the ring above.
Next, a matrix should be constructed where each row holds the values of one parameter setting in the
batch, and each column corresponds to the context variable, in an order provided to
batch_dynamic_aperture. In this case, these three parameter settings set the chromaticities to
about 0, 2 and 4 in both planes:
# ksf ksd
X = [1.66 -2.80;
2.64 -4.42;
3.63 -6.04]
das = batch_dynamic_aperture(ring, [:ksf, :ksd], X; n_turns=500,
emit_1=1e-6, emit_2=1e-7, n_r=21, n_theta=9, max_sig_x=80, max_sig_y=500,
deltas=[0.0, 0.005], verbose=false)
length(das)
3
All of the keyword arguments of dynamic_aperture, except for output_file, are accepted by
batch_dynamic_aperture. After the dynamic aperture scan is complete, the context variables are
returned to their previous values again.
Warning
The dynamic aperture is scanned around the closed orbits for each parameter setting in the batch
individually using SciBmad’s batched closed orbit finder. However, until batch_twiss is
implemented in SciBmad (planned), batch_dynamic_aperture currently uses the linear beam sizes of
the ring when it is first called prior to setting any of the batch parameter values. Therefore, if
the linear Twiss parameters change significantly across different parameter settings, then the beam
size initialized for the scan will be invalid.
The result is a vector with a DynamicAperture for each parameter setting, in the order of the
rows of X: das[c] is what dynamic_aperture returns for setting c. In this case, along the
positive x axis (the first angle), the boundary moves inward as the sextupoles get stronger:
[d.x[1, :] for d in das] # for each setting: δ = 0 and 0.5%
3-element Vector{Vector{Float64}}:
[40.0, 40.0]
[36.0, 36.0]
[32.0, 32.0]
Unlike dynamic_aperture, if the closed orbit for a set of parameters is not found, it will
continue and the dynamic aperture boundaries are zero for that set.
The dynamic aperture can also be optimized: see Dynamic Aperture Optimization.
dynamic_aperture — Function.
dynamic_aperture(ring::Beamline; kwargs...) -> DynamicAperture
Computes the acceptance of the ring by pushing a polar grid in x/sig_x and y/sig_y
space, for each of the provided deltas. The beam sizes sig_x and sig_y are those at the
beginning of the beamline for the given emittances. Returns a DynamicAperture: e.g.
da.x[j, i], da.y[j, i] is the starting position, in units of sig_x and sig_y, of the
first particle lost along the j-th angle of the grid for deltas[i]; Inf if no particle
along that angle is lost, and the column i is zero if the particle on the closed orbit itself
is lost. BatchParams in the lattice are not supported (an error: each particle of the grid
would see another entry).
Required Keyword arguments
n_r::Int: Number of radial points of the polar grid, from 0 to the maximum: each angle hasn_r - 1particles, and the closed orbit one for each δn_theta::Int: Number of angles to sample on the polar grid (seetheta_lims)deltas::AbstractArray: Array of each δ to compute the transverse acceptancemax_sig_x::Real: Maximum sigma-x of the polar grid, in units of sigma (e.g.max_sig_x=20to check up to 20 sigma)max_sig_y::Real: Maximum sigma-y of the polar grid, in units of sigma (e.g.max_sig_y=20to check up to 20 sigma)emit_1::Real: Horizontal-like emittance to assume [m]emit_2::Real: Vertical-like emittance to assume [m]n_turns::Int: Number of turns to track
Optional Keyword Arguments
backend: A CPU/GPU device backend to use, e.g.CPU(), orCUDA.CUDABackend()to use the GPU. Default isCPU()coordinates_number_type::Type: The type of the phase space coordinates to use in tracking. Default isFloat64delta_dependent_orbits::Bool: Iftrue, then for each δ indeltas, the computed acceptance line will be centered around that δ-dependent closed orbit (computed withrf_on=false). Else, all acceptance lines are centered around the 6D closed orbit. Default istrueif coasting beam is detected,falseif longitudinal motion is detected. The tracking itself includes RF cavities in either case, unlessrf_on=falseis given too.theta_lims: A tuple specifying the range of thetas to scan on the polar grid. Default is(0, pi), the upper half plane (y >= 0)sig_pz::Real: sigma-δ of the beam to assume, default is 0emit_3::Real: Longitudinal-like emittance to assume [m]min_area::Real: The area (a fraction of the area of the grid) below which the dynamic aperture counts as closed, forda.reach. Default isda.resolutionoutput_file: If provided, then a file containing the initial particle coordinates (w.r.t. the closed orbit(s)) as rows, with the last column specifying the particle state at the end of tracking, will be written to the file. Default isnothing.track_kwargs...: Any of the settings inTrackConfigexcept forn_turnsandsave_every_n_turnsmay be provided to configure the tracking.
DynamicAperture — Type.
DynamicAperture
The result of dynamic_aperture (and of each setting of batch_dynamic_aperture):
the dynamic aperture on a polar grid in x/sig_x and y/sig_y, for each δ of deltas. Its
arrays are indexed by position, by the angle j of the grid (thetas[j]) and i of deltas:
da.x[j, i],da.y[j, i]: the starting position, in units ofsig_xandsig_y, of the first particle lost along anglej;Infif none is lost, and zero for all angles if the particle on the closed orbit itself is lostda.r[j, i]: the same boundary as a fraction of the radius of the grid,min(sqrt((x/max_sig_x)^2 + (y/max_sig_y)^2), 1)(1 if no particle along the angle is lost)da.area[i]: the area inside the boundary as a fraction of the area of the grid (the mean ofr^2over the angles)da.reach: the largest δ up to which the area stays at leastmin_area: the first δ where it is smaller, interpolated linearly with the δ before, ordeltas[end]if it never is (deltasin increasing order of|δ|make this a reach in δ)da.resolution: the change ofareawhen the boundary along one angle moves by one radial step of the grid at half its radius,1/((n_r - 1)*n_theta), the smallest change the grid resolves; the defaultmin_areada.lost[k, j, i]: whether thek-th particle along anglej(k = 1nearest the closed orbit) was lost, andda.orbit_lost[i]whether the particle on the closed orbit was lostda.grid_x[k, j],da.grid_y[k, j]: the starting position of that particle relative to the closed orbit, in units ofsig_xandsig_yda.thetas,da.deltas,da.max_sig_x,da.max_sig_y,da.min_area
batch_dynamic_aperture — Function.
batch_dynamic_aperture(ring::Beamline, names, X; kwargs...) -> Vector{DynamicAperture}
Computes the dynamic aperture of the ring for many parameter settings at once, as
dynamic_aperture does for one, in a single process (or on a single GPU). The parameters are the
variables names of the ring’s context, which the elements use through deferred expressions.
Each row of X is one parameter setting, and column j holds the values of names[j]: in
setting c the variables names have the values X[c, :]. Each variable is set to a
BatchParam, so that the closed orbits of all settings are found by one batched
find_closed_orbit and the grids of all settings are tracked together as one bunch. Afterwards
the variables have their previous values again.
Returns a vector of DynamicApertures, one for each setting, in the order of the rows of
X: element c is what dynamic_aperture returns for setting c, with two differences:
The beam sizes
sig_xandsig_yare those of the ring as it is when called, before any of the values inXare set, the same for all settings. If the linear optics change much between settings, the beam sizes do not fit them (a batchedtwissis planned).Where the closed orbit of a setting (or, with
delta_dependent_orbits, of a setting and δ) is not found, its boundaries are zero and its particles count as lost, instead of an error.
Other BatchParams in the lattice are not supported (an error).
Required Keyword arguments
n_r,n_theta,deltas,max_sig_x,max_sig_y,emit_1,emit_2,n_turns: As indynamic_aperture
Optional Keyword Arguments
All keyword arguments of
dynamic_apertureexceptoutput_file:backend,coordinates_number_type,delta_dependent_orbits,theta_lims,sig_pz,emit_3,min_area,track_kwargs.... On the CPU the closed orbits are found inFloat64; on a GPUbackend(e.g.MetalBackend()) they are found there too, incoordinates_number_type, to the round-off of that type