(GPU-)Batched Closed Orbit Finder

The find_closed_orbit function can be used to find the fixed point of a periodic beamline. It uses the Newton method implemented in the BatchSolve package. By default, automatic differentiation via ForwardDiff is used to compute the Jacobian(s) each iteration.

@elements begin
  qf = Quadrupole(Kn1=0.36, L=0.5)
  d = Drift(L=1.2)
  qd = Quadrupole(Kn1=-0.36, L=0.5)
  kick = HKicker(Kn0L=1e-5)
end

fodo = Beamline([qf, d, qd, d, kick],
        species_ref=Species("electron"), E_ref=18e9) # some Beamline
co_sol = find_closed_orbit(fodo);

A NamedTuple is returned containing the properties:

  • v0: The closed orbit

  • coasting_beam: true if no longitudinal oscillations (4D closed orbit), false otherwise

  • sol: Another NamedTuple with details of the solution from BatchSolve:

    • u: The coordinates the Newton method solved for: all six, or for a coasting beam only the four transverse ones (δ is fixed), one row per particle.

    • f: The final residual: the change of these coordinates over one turn, which is zero on the closed orbit.

    • jac: The Jacobian of the residual at the last iteration (of all particles together).

    • retcode: 0x0 if the solution converged, 0x1 if the solver failed, 0x2 if it reached the maximum number of iterations (one per particle with several particles).

    • iters: The number of iterations taken (one per particle with several particles).

An initial “guess” for the closed orbit can be provided by specifying a 1 x 6 matrix of particle phase space coordinates to the keyword argument v0. By default, it is zeros(1, 6).

co_sol = find_closed_orbit(fodo; v0=[1e-5 2e-6 -1e-5 -2e-6 0.0 1e-5])
co_sol.v0
1×6 Matrix{Float64}:
 -0.000498981  -4.96046e-5  -9.31588e-25  8.90719e-26  0.0  1.0e-5

This beamline has no RF, so the beam is coasting (co_sol.coasting_beam is true), and the last coordinate δ of the guess did not change: the result is the closed orbit for δ = 1e-5 (see δ-Dependent Closed Orbits below).

Note

When v0 is provided, it will be mutated in place with the result!

δ-Dependent Closed Orbits

For beamlines with coasting beam, δ-dependent closed orbits can be computed. To do this, we set v0 equal to a matrix of n_particles x 6:

# To get delta-dependent closed orbits:
v0 = [0. 0. 0. 0. 0. 0.1e-2; # δ = 0.1e-2
      0. 0. 0. 0. 0. 0.2e-2] # δ = 0.2e-2

co_sol = find_closed_orbit(fodo, v0=v0)
co_sol.v0
2×6 Matrix{Float64}:
 -0.00049943   -4.96448e-5  0.0  0.0  0.0  0.001
 -0.000499883  -4.96855e-5  0.0  0.0  0.0  0.002

If v0 is a CUDA array type CuArray, then the Newton method used to find the closed orbits will be GPU-parallelized, with bindings directly to cuBLAS’s batched linear system solvers.

using CUDA
n_particles = 100000
v0 = CUDA.zeros(Float64, n_particles, 6)
v0[:,6] .= CuArray(range(start=0, stop=1e-2, length=n_particles))

co_sol = find_closed_orbit(fodo, v0=v0)

Closed Orbits with Batch Parameters

If any element parameters are defined with BatchParams, then as with track, each particle in the initial guess matrix will see that corresponding parameter (with a guess of one row, the default v0, the first value). Element lengths and the reference energy can be batched too.

Below, we first find two closed orbits, one for kick.Kn0L=1e-5 and one for kick.Kn0L=2e-5, and then the same with δ = 0.1e-2 for the first and δ = 0.2e-2 for the second:

# With BatchParams
kick.Kn0L = BatchParam([1e-5, 2e-5])
v0 = zeros(2, 6)
co_sol = find_closed_orbit(fodo, v0=v0)

# BatchParams + delta-dependent closed orbits:
v0 = [0. 0. 0. 0. 0. 0.1e-2; # δ = 0.1e-2
      0. 0. 0. 0. 0. 0.2e-2] # δ = 0.2e-2
co_sol = find_closed_orbit(fodo, v0=v0)
(v0 = [-0.000499429580692261 -4.964484747922549e-5 … 0.0 0.001; -0.0009997640972285228 -9.937089270145935e-5 … 0.0 0.002], coasting_beam = true, sol = (u = [-0.000499429580692261 -4.964484747922549e-5 0.0 0.0; -0.0009997640972285228 -9.937089270145935e-5 0.0 0.0], f = [-1.0842021724855044e-19 0.0 0.0 0.0; 0.0 8.131516293641283e-20 0.0 0.0], jac = sparse([1, 3, 5, 7, 2, 4, 6, 8, 1, 3  …  6, 8, 1, 3, 5, 7, 2, 4, 6, 8], [1, 1, 1, 1, 2, 2, 2, 2, 3, 3  …  6, 6, 7, 7, 7, 7, 8, 8, 8, 8], [0.3729345122731341, 0.04962649592106685, 0.0, 0.0, 0.37249557502755526, 0.04957706733496511, 0.0, 0.0, -3.7517388268279843, -0.2978139673475231  …  -0.2381508356712967, 0.04957706045998415, 0.0, 0.0, -3.0189641981835194, 0.31344220382690424, 0.0, 0.0, -3.016338705414775, 0.31312181914188], 8, 8), retcode = UInt8[0x00; 0x00;;], iters = [2; 2;;]))

Again, by making these CuArrays, the closed orbit finding will be GPU-parallelized.

using CUDA

n_kick_strengths = 100
n_deltas = 1000
n_particles = n_kick_strengths * n_deltas
v0 = CUDA.zeros(Float64, n_particles, 6)
deltas = repeat(range(start=0, stop=1e-2, length=n_deltas), outer=n_kick_strengths)
v0[:,6] .= CuArray(deltas)

kick_strengths = repeat(range(start=1e-5, stop=2e-5, length=n_kick_strengths), inner=n_deltas)
kick.Kn0L = BatchParam(CuArray(kick_strengths))
co_sol = find_closed_orbit(fodo, v0=v0)

find_closed_orbit — Function.

find_closed_orbit(bl::Beamline; v0=zeros(1, 6), kwargs...)

Finds the closed orbit of the beamline bl using the initial guess v0. Supports batched- closed orbit finding including BatchParams and/or different δ-dependent closed orbits in rings with coasting beam, simply by including more particles in the guess v0.

A named tuple is returned containing the following fields:

  • v0: The initial matrix v0, mutated to contain the result

  • coasting_beam: true if coasting beam, false otherwise

  • sol: Another named tuple containing Newton solver convergence information (see SciBmad.BatchSolve.newton), including the Jacobian(s) for the particle(s) at the last iteration.

This function is GPU-parallelizable by specifying v0 as a GPUArray (e.g. CuArray), and will use CUBLAS’s batched linear system solvers for the Newton solve.

Keyword arguments

  • v0: Matrix of size (n_particles, 6) as the initial guess. If n_particles > 1, batched closed orbit finding will be used. NOTE: v0 will be mutated in place with the result! Default is zeros(1, 6).

  • reltol: Relative convergence tolerance of the Newton solver, default is 1e-13

  • abstol: Absolute convergence tolerance of the residual norm, default is 1e-13

  • maxiter: Maximum iterations for the Newton root finder before failure, default is 100

  • rf_on::Bool: If false, then any RFParams in any LatElements are ignored for the entirety of the tracking. Default is true.

  • batch_start::Int: Specifies the starting index for BatchParams that particles in a bunch will see. E.g., if batch_start=3, then particle 1 sees index 3 in all BatchParams, particle 2 sees index 4, etc. Indexing of BatchParams will loop around to the start if the particle index exceeds the length of the BatchParams. Default is 1. WARNING: setting this argument will caused unaligned memory access on the GPU, which may reduce performance. In this case, consider circshift-ing all BatchParams instead.

  • autodiff: Automatic-differentiation backend to use (e.g. AutoForwardDiff(), AutoEnzyme(), AutoGTPSA(), etc.). Default is AutoForwardDiff. See ADTypes.jl for all supported backends.

  • warn: If true, warnings about the result will be printed. Default is true

  • coast_tol: Tolerance for the first-order delta dependences on each of the phase space variables below which coasting beam is assumed. Default is 1e-14.

  • coasting_beam: Bool that can be optionally specified as true or false to bypass the coasting- beam check and save some computation time. (With the check and the default zero guess for one particle, its evaluation of the one-turn map is also the first step of the Newton solver.)

  • batch: Optionally specify if batched-solution is used as Val{true}() or Val{false}() to improve type-stability of this function.

  • prep: A DifferentiationInterface.JacobianPrep object to use for the automatic-differentation backend. Default is nothing meaning to construct one automatically using the autodiff backend

Examples

qf = Quadrupole(Kn1=0.36, L=0.5)
d = Drift(L=1.2)
qd = Quadrupole(Kn1=-0.36, L=0.5)
kick = HKicker(Kn0L=1e-5)

fodo = Beamline([qf, d, qd, d, kick], 
        species_ref=Species("electron"), E_ref=18e9) # some Beamline
co_sol = find_closed_orbit(fodo)

# To get delta-dependent closed orbits:
v0 = [0. 0. 0. 0. 0. 0.1e-2; # δ = 0.1e-2
      0. 0. 0. 0. 0. 0.2e-2] # δ = 0.2e-2

co_sol = find_closed_orbit(fodo, v0=v0)

# With BatchParams
kick.Kn0L = BatchParam([1e-5, 2e-5])
v0 = zeros(2, 6)
co_sol = find_closed_orbit(fodo, v0=v0)

# BatchParams + delta-dependent closed orbits:
v0 = [0. 0. 0. 0. 0. 0.1e-2; # δ = 0.1e-2
      0. 0. 0. 0. 0. 0.2e-2] # δ = 0.2e-2
co_sol = find_closed_orbit(fodo, v0=v0)