Orbit Correction, LOCO and Open Lines¶
This page shows optimize! at work on orbit correction, on fitting a measured
orbit response matrix (as in LOCO), and on lines that are not periodic.
Orbit Correction¶
The closed orbit is a column of twiss like any other (tw.x[bpm], tw.y[bpm], …), so
optimize! can do orbit correction too. Here is a ring of ten cells, each with
a misaligned quadrupole, a
horizontal corrector and a BPM. The strengths of all correctors come from one vector variable of
the context, kicks (and gradient errors of the quadrupoles from another, dk, used below):
using SciBmad
ncell = 10
offsets = 1e-4 .* [0.8, -1.2, 0.3, 1.5, -0.6, 0.9, -1.1, 0.2, 1.3, -0.4] # in m
elements = LatElement[]
bpms = LatElement[]
for i in 1:ncell
bpm = Marker()
push!(bpms, bpm)
append!(elements, [Quadrupole(Kn1=DefExpr(c -> 0.36 + c.dk[i]), L=0.5, x_offset=offsets[i]),
Multipole(Kn0L=DefExpr(c -> c.kicks[i])), Drift(L=0.2), bpm,
SBend(L=6.5, angle=pi/ncell), Drift(L=0.2),
Quadrupole(Kn1=-0.36, L=0.5), Drift(L=0.2),
SBend(L=6.5, angle=pi/ncell), Drift(L=0.2)])
end
ring = Beamline(elements; species_ref=Species("electron"), pc_ref=18e9);
ring.context.kicks = zeros(ncell);
ring.context.dk = zeros(ncell);
twr = lazy_twiss(ring)
orbit = [twr.x[bpm] for bpm in bpms]
maximum(abs, value(orbit)) # 5.8e-4 m at the BPMs
0.0005790402437629866
minimize = sum(abs2, orbit) makes the orbit at the BPMs
as small as possible (in the least-squares sense), varying every corrector:
res = optimize!(vary = [@knob(ring.context.kicks)], minimize = sum(abs2, orbit))
maximum(abs, value(orbit)) # 4.5e-15 m
7.72743883572437e-15
Only the orbit is computed for this (none of the optics at the BPMs), which is faster. The closed orbit needs stable linear optics: if the errors make the ring unstable, correct the orbit first with the sextupoles turned off, then turn them on in steps.
Turning the sextupoles on in steps means correcting the orbit after every step. Here is a ring
like the one above, with sextupoles next to the quadrupoles, whose strengths scale with the
context variable ks, and with a second BPM in each cell:
selements = LatElement[]
sbpms = LatElement[]
for i in 1:ncell
b1, b2 = Marker(), Marker()
append!(sbpms, [b1, b2])
append!(selements, [Quadrupole(Kn1=0.36, L=0.5, x_offset=offsets[i]),
Sextupole(Kn2=DefExpr(c -> 4*c.ks), L=0.2),
Multipole(Kn0L=DefExpr(c -> c.kicks[i])), Drift(L=0.2), b1,
SBend(L=6.5, angle=pi/ncell), Drift(L=0.2),
Quadrupole(Kn1=-0.36, L=0.5), Sextupole(Kn2=DefExpr(c -> -6*c.ks), L=0.2),
b2, Drift(L=0.2), SBend(L=6.5, angle=pi/ncell), Drift(L=0.2)])
end
sring = Beamline(selements; species_ref=Species("electron"), pc_ref=18e9);
sring.context.kicks = zeros(ncell);
sring.context.ks = 0.0;
tws = lazy_twiss(sring)
sorbit = [tws.x[b] for b in sbpms]
20-element Vector{SciBmad._TwissLeaf}:
x[5]
x[10]
x[18]
x[23]
x[31]
x[36]
x[44]
x[49]
x[57]
x[62]
x[70]
x[75]
x[83]
x[88]
x[96]
x[101]
x[109]
x[114]
x[122]
x[127]
There are more BPMs than correctors, so the orbit cannot be zero at every BPM and is made as
small as possible. The orbit is corrected with the sextupoles off, and then again after every
step with optimize!(sres), which optimizes the same problem again:
sres = optimize!(vary = [@knob(sring.context.kicks)], minimize = sum(abs2, sorbit),
solver = GN(final_jacobian = false))
for ks in 0.2:0.2:1.0
sring.context.ks = ks
r = optimize!(sres)
println("ks = $ks: Jacobians $(r.jacobians), evaluations $(r.evaluations)")
end
ks = 0.2: Jacobians 1, evaluations 4
ks = 0.4: Jacobians 2, evaluations 20
ks = 0.6: Jacobians 0, evaluations 2
ks = 0.8: Jacobians 2, evaluations 19
ks = 1.0: Jacobians 2, evaluations 18
Each optimize! of the same problem starts from the last Jacobian computed for
it. The orbit changes little from one step to the next, and so does its Jacobian, so most steps need
no new one. A match normally ends with a newly computed Jacobian, so that its result is a minimum.
With GN(final_jacobian = false) the kept Jacobian may end it,
and the result is the minimum according to that Jacobian. Matching once more with a new Jacobian
shows how close that was:
x_kept = value(sorbit)
maximum(abs, x_kept) # 3.6e-6 m left at the BPMs
optimize!(sres; solver = GN()) # ends with a new Jacobian
maximum(abs, value(sorbit) - x_kept) # 0.0: it was the minimum already
0.0
The orbit response matrix, the change of the orbit at each BPM per kick of each corrector, is a
matrix of derivatives (deriv, see Values and
Derivatives), here with respect to the entries of kicks:
R = [deriv(twr.x[bpm], Knob(ring.context, :kicks, j)) for bpm in bpms, j in 1:ncell]
10×10 Matrix{SciBmad._Deriv}:
deriv(x[4], kicks[1]) deriv(x[4], kicks[2]) … deriv(x[4], kicks[10])
deriv(x[14], kicks[1]) deriv(x[14], kicks[2]) deriv(x[14], kicks[10])
deriv(x[24], kicks[1]) deriv(x[24], kicks[2]) deriv(x[24], kicks[10])
deriv(x[34], kicks[1]) deriv(x[34], kicks[2]) deriv(x[34], kicks[10])
deriv(x[44], kicks[1]) deriv(x[44], kicks[2]) deriv(x[44], kicks[10])
deriv(x[54], kicks[1]) deriv(x[54], kicks[2]) … deriv(x[54], kicks[10])
deriv(x[64], kicks[1]) deriv(x[64], kicks[2]) deriv(x[64], kicks[10])
deriv(x[74], kicks[1]) deriv(x[74], kicks[2]) deriv(x[74], kicks[10])
deriv(x[84], kicks[1]) deriv(x[84], kicks[2]) deriv(x[84], kicks[10])
deriv(x[94], kicks[1]) deriv(x[94], kicks[2]) deriv(x[94], kicks[10])
A measured response matrix can then be fitted, as in LOCO. It depends on the focusing of the ring, and hardly on the misalignments: they shift the orbit, but without sextupoles they barely change its response to a kick (here by 2e-4 of its size). So it is fitted with gradient errors.
Here the “measurement” is the response matrix of the ring with the gradient errors dk_true. The
model starts without them, and finds them again:
dk_true = 1e-3 .* [1.0, -2.0, 0.5, 1.5, -1.0, 0.8, -0.3, 2.0, -1.2, 0.4]
ring.context.dk = copy(dk_true)
R_measured = value(R)
ring.context.dk = zeros(ncell)
res = optimize!(vary = [@knob(ring.context.dk)], targets = vec(R .== R_measured))
maximum(abs, ring.context.dk - dk_true) # 4e-12: the errors are found again
4.059621996205243e-12
Further knobs fit the same way, for example quadrupole tilts for the coupling (with the vertical orbit in the matrix) and the calibrations of the BPMs and correctors.
Lines That Are Not Periodic¶
For a transfer line, a linac, or a section of a ring by itself, give the lattice functions at its
start with initial or a_initial (see Open lines). a_initial can be the a of
another lazy twiss, so that the line continues from another beamline. Here a line of four
quadrupoles follows the FODO cell of Matching and Optimization, with its tunes
matched to 0.25, and is matched to a waist with beta functions of 10 m at its end:
using SciBmad
@elements begin
qf = Quadrupole(Kn1=0.36, L=0.5)
sf = Sextupole(Kn2=1.2, L=0.2)
d = Drift(L=0.2)
b = SBend(L=6.5, angle=pi/132)
qd = Quadrupole(Kn1=-0.36, L=0.5)
sd = Sextupole(Kn2=-1.2, L=0.2)
end
fodo = Beamline([qf, sf, d, b, d, qd, sd, d, b, d],
species_ref=Species("electron"), pc_ref=18e9)
tw = lazy_twiss(fodo)
optimize!(vary = [@knob(qf.Kn1), @knob(qd.Kn1)], targets = [tw.q1 == 0.25, tw.q2 == 0.25]);
@elements begin
m1 = Quadrupole(Kn1=0.3, L=0.5)
m2 = Quadrupole(Kn1=-0.3, L=0.5)
m3 = Quadrupole(Kn1=0.3, L=0.5)
m4 = Quadrupole(Kn1=-0.3, L=0.5)
dm = Drift(L=3.0)
end
line = Beamline([m1, dm, m2, dm, m3, dm, m4, dm],
species_ref=Species("electron"), pc_ref=18e9)
tl = lazy_twiss(line; a_initial=tw.a[end]) # starts with the periodic lattice functions of the cell
res = optimize!(vary = [@knob(m1.Kn1), @knob(m2.Kn1), @knob(m3.Kn1), @knob(m4.Kn1)],
targets = [tl.beta1[end] == 10, tl.beta2[end] == 10,
tl.alpha1[end] == 0, tl.alpha2[end] == 0])
Converged (all targets met) with Gauss-Newton: 9 iterations, 12 evaluations (3 with derivatives), 10.4 s
Target value goal met
beta1[end] 10.0 == 10.0 yes
beta2[end] 10.0 == 10.0 yes
alpha1[end] 1.822930062e-11 == 0.0 yes
alpha2[end] -2.540210894e-11 == 0.0 yes
Knob before after lower upper
m1.Kn1 0.3 0.369631568 -Inf Inf
m2.Kn1 -0.3 -0.5741988167 -Inf Inf
m3.Kn1 0.3 0.4994244709 -Inf Inf
m4.Kn1 -0.3 -0.3747810411 -Inf Inf
Because the start of the line is taken from the cell each time, quadrupoles of the cell could be
varied in the same match. A whole ring can be built this way piece by piece: match the arc cell,
then a dispersion suppressor that follows it
(tl.dx[end] == 0, tl.dpx[end] == 0), then the
straight section that follows the suppressor, and finally the tunes and chromaticities of the
complete ring.
The Twiss parameters at the start can be knobs too: give initial as a
Context, and vary its
variables. Here, with the quadrupoles as matched above, they are matched so that the line ends in
a waist with beta functions of 5 m:
ini = Context(beta1=10.0, alpha1=0.0, beta2=10.0, alpha2=0.0)
ti = lazy_twiss(line; initial=ini)
res = optimize!(vary = [@knob(ini.beta1), @knob(ini.alpha1),
@knob(ini.beta2), @knob(ini.alpha2)],
targets = [ti.beta1[end] == 5, ti.beta2[end] == 5,
ti.alpha1[end] == 0, ti.alpha2[end] == 0])
Converged (all targets met) with Gauss-Newton: 13 iterations, 19 evaluations (5 with derivatives), 1.02 s
Target value goal met
beta1[end] 5.000000002 == 5.0 yes
beta2[end] 5.0 == 5.0 yes
alpha1[end] -1.612497787e-10 == 0.0 yes
alpha2[end] -1.861900615e-16 == 0.0 yes
Knob before after lower upper
beta1 10.0 33.98062459 -Inf Inf
alpha1 0.0 -2.501507761 -Inf Inf
beta2 10.0 7.16981089 -Inf Inf
alpha2 0.0 0.07771684947 -Inf Inf