(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.

Thanks to SciBmad’s support for both explicit Single Instruction, Multiple Data (SIMD) on the CPU and Single Instruction, Multiple Thread (SIMT) on the GPU, highly-parallelized and performant tracking simulations can be run on all types of architectures. Here we will do a dynamic aperture scan of the v6.3.1 Electron Storage Ring (ESR) of the Electron-Ion Collider (EIC) using SciBmad’s dynamic_aperture function on an Apple Metal GPU. Also supported are NVIDIA CUDA, AMD ROCm, and Intel oneAPI GPUs.

Firstly, please be aware that SciBmad’s dynamic_aperture function works differently than that in classical Bmad; in Bmad, the dynamic aperture is computed by tracking a particle initialized with some angle and radius in the x-y acceptance plane, and if it lives for the specified number of turns, then a larger radius is used. This is repeated until the particle is lost, and then that radius is specified as the dynamic aperture.

In SciBmad, a polar grid of particles spanning the entire acceptance plane, with density and maximums specifiable by the user, is pushed in parallel, all at once. At the end of tracking, particles will either be alive or lost - a binary heatmap is produced in the x-y acceptance plane. With this, the first lost radius along each angle is chosen as the dynamic aperture. We have found this approach to be significantly faster thanks to the (GPU-)parallelizability of it.

Apple Metal GPUs only support 32-bit floats, so we must specify coordinates_number_type=Float32 as a keyword argument.

using Metal, SciBmad, CairoMakie
# Lattice in the SciBmad Github lattices directory
include("../../lattices/esr-v6.3.1-tapered.jl"); 
# Turn on radiation
for ele in ring.line
  ele.rad_damping = true
end
da = dynamic_aperture(
    ring;
    n_turns = 2000,
    emit_1  = 30e-9,
    emit_2  = 15e-9,
    emit_3  = 7.30474E-06,
    n_r     = 30,
    n_theta = 65,
    max_sig_x = 15,
    max_sig_y = 22,
    deltas = (0:0.1:1).*10^-2,
    backend = Metal.MetalBackend(), # Specify a Metal backend
    coordinates_number_type = Float32, # Metal only compatible with Float32
);
Initializing dynamic_aperture with 20746 particles
Initializing bunch on GPU
Finished turn 2000 out of 2000
Tracking complete
f2 = Figure(fontsize=22, size=(700,500));
ax = Axis(f2[1, 1], xlabel=L"x/\sigma_x", ylabel=L"y/\sigma_y", limits=(-15,15,0,22))
deltas = (0:0.1:1).*10^-2
for i in 1:length(deltas)
    lines!(da.x[:,i], da.y[:,i], linewidth=4, color=i, colorrange=(1,11), label=L"\delta=%$(round(deltas[i]*10^2; sigdigits=1))\%")
end
Legend(f2[1,2], ax)
display(f2);
_images/examples-julia-dynamic-aperture-gpu-5-fab71c7c.png

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

da.r[j, i]

the boundary as a fraction of the radius of the grid (1 where none is lost)

da.area[i]

the area inside the boundary as a fraction of the area of the grid (the mean of r^2 over the angles)

da.reach

the largest δ up to which area stays at least min_area, interpolated between the δ of the grid

da.resolution

the change of area when the boundary along one angle moves by one radial step at half the radius: the smallest change the grid resolves

da.lost[k, j, i]

whether the k-th particle along angle j was lost

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 has n_r - 1 particles, and the closed orbit one for each δ

  • n_theta::Int: Number of angles to sample on the polar grid (see theta_lims)

  • deltas::AbstractArray: Array of each δ to compute the transverse acceptance

  • max_sig_x::Real: Maximum sigma-x of the polar grid, in units of sigma (e.g. max_sig_x=20 to 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=20 to 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(), or CUDA.CUDABackend() to use the GPU. Default is CPU()

  • coordinates_number_type::Type: The type of the phase space coordinates to use in tracking. Default is Float64

  • delta_dependent_orbits::Bool: If true, then for each δ in deltas, the computed acceptance line will be centered around that δ-dependent closed orbit (computed with rf_on=false). Else, all acceptance lines are centered around the 6D closed orbit. Default is true if coasting beam is detected, false if longitudinal motion is detected. The tracking itself includes RF cavities in either case, unless rf_on=false is 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 0

  • emit_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, for da.reach. Default is da.resolution

  • output_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 is nothing.

  • track_kwargs...: Any of the settings in TrackConfig except for n_turns and save_every_n_turns may 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 of sig_x and sig_y, of the first particle lost along angle j; Inf if none is lost, and zero for all angles if the particle on the closed orbit itself is lost

  • da.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 of r^2 over the angles)

  • da.reach: the largest δ up to which the area stays at least min_area: the first δ where it is smaller, interpolated linearly with the δ before, or deltas[end] if it never is (deltas in increasing order of |δ| make this a reach in δ)

  • da.resolution: the change of area when 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 default min_area

  • da.lost[k, j, i]: whether the k-th particle along angle j (k = 1 nearest the closed orbit) was lost, and da.orbit_lost[i] whether the particle on the closed orbit was lost

  • da.grid_x[k, j], da.grid_y[k, j]: the starting position of that particle relative to the closed orbit, in units of sig_x and sig_y

  • da.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_x and sig_y are those of the ring as it is when called, before any of the values in X are set, the same for all settings. If the linear optics change much between settings, the beam sizes do not fit them (a batched twiss is 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 in dynamic_aperture

Optional Keyword Arguments

  • All keyword arguments of dynamic_aperture except output_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 in Float64; on a GPU backend (e.g. MetalBackend()) they are found there too, in coordinates_number_type, to the round-off of that type