13. Comparing Lattices

Bmad’s program Tao keeps three copies of a lattice: the “design” lattice as read from the lattice file, which never changes, the “model” lattice, which the commands of Tao change, and a “base” lattice, a copy of the model made at some point to compare with. In SciBmad, a lattice is a set of Julia objects, which you change directly. To compare the lattice before and after a change, keep the result of twiss from before the change: it does not change when the lattice does.

Example

An open line with a drift, a bend and a quadrupole, for electrons with a total energy of 10 MeV, starting with beta functions of 10 m:

using SciBmad

@elements begin
  d = Drift(L=0.5)
  b = SBend(L=0.5, g_ref=1.0, Kn0=1.001, e1=0.1)
  q = Quadrupole(L=0.6, Kn1=0.23)
end
for ele in (d, b, q)
  ele.n_steps = 10 # For smooth plots
end
simple = Beamline([d, b, q], species_ref=Species("electron"), E_ref=10e6)
initial = (beta1=10.0, beta2=10.0)
tw_design = twiss(simple, initial=initial)
Twiss:
summ:
 q1     = 0.03131247815390913
 q2     = 0.027056500923310512
 etac   = 0.010153945554962213
 alphac = 0.012765144831025022

df:
31×26 DataFrame
 Row │ index  name  kind        s     beta1     phi1         dx        x             beta2     phi ⋯
     │                          [m]   [m]       [2π]         [m]       [m]           [m]       [2π ⋯
─────┼──────────────────────────────────────────────────────────────────────────────────────────────
   1 │     1  d     Drift       0.0   10.0      0.0          0.0        0.0          10.0      0.0 ⋯
   2 │     1  d     Drift       0.05  10.0003   0.000795768  0.0        0.0          10.0003   0.0
   3 │     1  d     Drift       0.1   10.001    0.0015915    0.0        0.0          10.001    0.0
   4 │     1  d     Drift       0.15  10.0023   0.00238715   0.0        0.0          10.0023   0.0
   5 │     1  d     Drift       0.2   10.004    0.00318267   0.0        0.0          10.004    0.0 ⋯
   6 │     1  d     Drift       0.25  10.0063   0.00397804   0.0        0.0          10.0063   0.0
   7 │     1  d     Drift       0.3   10.009    0.00477322   0.0        0.0          10.009    0.0
  ⋮  │   ⋮     ⋮        ⋮        ⋮       ⋮           ⋮          ⋮           ⋮           ⋮          ⋱
  26 │     3  q     Quadrupole  1.3    6.51008  0.0224865    0.264733  -0.000264477   8.79672  0.0
  27 │     3  q     Quadrupole  1.36   6.08805  0.0240032    0.292609  -0.000292327   8.77974  0.0 ⋯
  28 │     3  q     Quadrupole  1.42   5.67157  0.0256281    0.320244  -0.000319935   8.77812  0.0
  29 │     3  q     Quadrupole  1.48   5.26202  0.0273759    0.347613  -0.000347278   8.79187  0.0
  30 │     3  q     Quadrupole  1.54   4.86076  0.0292639    0.374694  -0.000374334   8.82102  0.0
  31 │    -1  END   ---         1.6    4.46911  0.0313125    0.401464  -0.000401079   8.86568  0.0 ⋯
                                                                      17 columns and 18 rows omitted

The field of the bend Kn0 is a little stronger than the curvature g_ref of the reference trajectory, so the orbit is not zero. Now the lattice is changed: a vertical kick of −0.5 mrad is added to the drift (a skew dipole field, Ks0L), and a horizontal kick of 1 mrad to the quadrupole (a normal dipole field, Kn0L: a positive normal dipole field kicks towards \(-x\)):

d.Ks0L = -0.0005
q.Kn0L = -0.001
tw = twiss(simple, initial=initial)
Twiss:
summ:
 q1     = 0.03131249665753308
 q2     = 0.027056514844851155
 etac   = 0.01023914891812972
 alphac = 0.012850348194192528

df:
31×26 DataFrame
 Row │ index  name  kind        s     beta1     phi1         dx        x             beta2     phi ⋯
     │                          [m]   [m]       [2π]         [m]       [m]           [m]       [2π ⋯
─────┼──────────────────────────────────────────────────────────────────────────────────────────────
   1 │     1  d     Drift       0.0   10.0      0.0          0.0        0.0          10.0      0.0 ⋯
   2 │     1  d     Drift       0.05  10.0003   0.000795768  0.0        0.0          10.0003   0.0
   3 │     1  d     Drift       0.1   10.001    0.0015915    0.0        0.0          10.001    0.0
   4 │     1  d     Drift       0.15  10.0023   0.00238715   0.0        0.0          10.0023   0.0
   5 │     1  d     Drift       0.2   10.004    0.00318267   0.0        0.0          10.004    0.0 ⋯
   6 │     1  d     Drift       0.25  10.0063   0.00397804   0.0        0.0          10.0063   0.0
   7 │     1  d     Drift       0.3   10.009    0.00477322   0.0        0.0          10.009    0.0
  ⋮  │   ⋮     ⋮        ⋮        ⋮       ⋮           ⋮          ⋮           ⋮           ⋮          ⋱
  26 │     3  q     Quadrupole  1.3    6.51008  0.0224865    0.264658  -0.000189699   8.79672  0.0
  27 │     3  q     Quadrupole  1.36   6.08805  0.0240032    0.292502  -0.000184691   8.77973  0.0 ⋯
  28 │     3  q     Quadrupole  1.42   5.67156  0.0256281    0.320098  -0.000173531   8.77812  0.0
  29 │     3  q     Quadrupole  1.48   5.26201  0.0273759    0.347423  -0.000156227   8.79186  0.0
  30 │     3  q     Quadrupole  1.54   4.86075  0.0292639    0.374454  -0.000132794   8.82101  0.0
  31 │    -1  END   ---         1.6    4.4691   0.0313125    0.401169  -0.000103252   8.86568  0.0 ⋯
                                                                      17 columns and 18 rows omitted

The orbit of the changed lattice, and its difference from the design:

using CairoMakie
fig = Figure(size=(700, 500))
ax1 = Axis(fig[1, 1], ylabel="orbit [mm]")
lines!(ax1, tw.s, 1e3 .* tw.x, label="x")
lines!(ax1, tw.s, 1e3 .* tw.y, label="y")
axislegend(ax1, position=:lt)
ax2 = Axis(fig[2, 1], xlabel="s [m]", ylabel="orbit - design [mm]")
lines!(ax2, tw.s, 1e3 .* (tw.x .- tw_design.x), label="x")
lines!(ax2, tw.s, 1e3 .* (tw.y .- tw_design.y), label="y")
fig

To compare with another state than the design, keep another twiss result, as Tao’s “base” lattice:

tw_base = tw
q.Ks0L = 0.0005
tw = twiss(simple, initial=initial)
maximum(abs, tw.y .- tw_base.y), maximum(abs, tw.x .- tw_base.x)
(0.00015103793942826144, 1.2708047658074265e-10)

To set the lattice back, set the parameters back (d.Ks0L = 0 etc.). A match can be undone with undo! of its result, e.g. res = optimize!(...), then undo!(res).

Exercises

  1. Orbit plots: Plot the horizontal orbit of the design lattice, and that of the changed lattice, in one plot.

  2. Functions: To save typing, define a function setit(k) that sets the vertical kick of q to k, so that setit(1e-6) is the same as q.Ks0L = 1e-6.

  3. Changing the tunes with the arcs: Start with the ring of chapter 9, and keep its twiss result as the design. Then change the tunes to 54.10 and 54.12 by varying the arc quadrupoles qf and qd instead of the tune cell. Plot the relative difference of the beta functions from the design, tw.beta1 ./ tw_design.beta1 .- 1, around the ring. How much “beta beating” is there? Why does the tune cell avoid it?