Orbit response¶
Parametric twiss example
This notebook requires knowledge of GTPSA.jl because it uses twiss with as_taylor_series=true.
using SciBmad
using CairoMakie
set_theme!(theme_latexfonts()) # Set plotting package theme to LaTeX
include("../../lattices/RCSV5S0.jl");
tw = twiss(ring)
Twiss:
summ:
q1 = 43.13000170559638
q2 = 56.21000112673147
q3 = -0.02074470822460294
etac = 0.0006425524685148873
alphac = 0.0006430166817195061
df:
5388×30 DataFrame
Row │ index name kind s beta1 phi1 dx ⋯
│ [m] [m] [2π] [m] ⋯
──────┼─────────────────────────────────────────────────────────────────────────
1 │ 1 qfs_6 Quadrupole 0.0 14.264 0.0 -7.33637e ⋯
2 │ 2 qlo_1 Drift 0.47 14.264 0.00516781 -7.33639e
3 │ 3 bpm_1 BPM 0.525 14.117 0.00578468 -7.29799e
4 │ 4 clo_1 Drift 0.545 14.0639 0.00601058 -7.28403e
5 │ 5 cor_1 Kicker 0.583 13.9633 0.00644216 -7.2575e- ⋯
6 │ 6 clo_2 Drift 0.84 13.2982 0.009444 -7.07806e
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱
5383 │ 5383 cor_352 Kicker 1500.8 5.17061 43.0012 -2.99503e
5384 │ 5384 clo_704 Drift 1501.05 5.24592 43.009 -3.17449e
5385 │ 5385 mqa5_14 Drift 1501.09 5.25925 43.0102 -3.20103e ⋯
5386 │ 5386 qlo_704 Drift 1506.96 14.117 43.1294 -7.29796e
5387 │ 5387 end1 Marker 1507.01 14.264 43.13 -7.33637e
5388 │ -1 END --- 1507.01 14.264 43.13 -7.33637e
24 columns and 5376 rows omitted
# Plot the betas
f = Figure(fontsize=22, size=(800,500));
ax = Axis(f[1, 1], xlabel="s [m]", ylabel="Beta Functions");
lines!(ax, tw.s, tw.beta1, label=L"\beta_1");
lines!(ax, tw.s, tw.beta2, label=L"\beta_2");
axislegend(position=:lt);
display(f);
# Let's see the linear closed orbit response as a function of
# e.g. qf_1 y_alignment and b0_1 tilt (2 PARAMETERS)
const DPARAMS1 = Descriptor(6, 1, 2, 1) # 6 dynamic variables to 1st order, 2 parameters to 1st order
Δk = params(DPARAMS1)
qf_1.y_offset = Δk[1] # make y_offset a parameter
b0_1.tilt = Δk[2] # make tilt a parameter
tw = twiss(ring; GTPSA_descriptor=DPARAMS1, as_taylor_series=true)
Twiss:
summ:
q1 = 43.13000170559638
q2 = 56.21000112673147
q3 = -0.020744708224603666
etac = 0.0006425524685149318
alphac = 0.0006430166817195505
df:
5388×30 DataFrame
Row │ index name kind s beta1 phi1 dx ⋯
│ [m] [m] [2π] [m] ⋯
──────┼─────────────────────────────────────────────────────────────────────────
1 │ 1 qfs_6 Quadrupole 0.0 14.264 0.0 -7.33637e ⋯
2 │ 2 qlo_1 Drift 0.47 14.264 0.00516781 -7.33639e
3 │ 3 bpm_1 BPM 0.525 14.117 0.00578468 -7.29799e
4 │ 4 clo_1 Drift 0.545 14.0639 0.00601058 -7.28403e
5 │ 5 cor_1 Kicker 0.583 13.9633 0.00644216 -7.2575e- ⋯
6 │ 6 clo_2 Drift 0.84 13.2982 0.009444 -7.07806e
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱
5383 │ 5383 cor_352 Kicker 1500.8 5.17061 43.0012 -2.99503e
5384 │ 5384 clo_704 Drift 1501.05 5.24592 43.009 -3.17449e
5385 │ 5385 mqa5_14 Drift 1501.09 5.25925 43.0102 -3.20103e ⋯
5386 │ 5386 qlo_704 Drift 1506.96 14.117 43.1294 -7.29796e
5387 │ 5387 end1 Marker 1507.01 14.264 43.13 -7.33637e
5388 │ -1 END --- 1507.01 14.264 43.13 -7.33637e
24 columns and 5376 rows omitted
# Closed orbit is now a Taylor series in the parameters:
# E.g. at the first element:
tw.y[1]
TPS64{GTPSA.Dynamic}:
Descriptor(NV=6, MO=1, NP=2, PO=1)
COEFFICIENT ORDER EXPONENTS
-3.4019914795086181E-01 1 0 0 0 0 0 0 7^1
-3.4327120607787705E-02 1 0 0 0 0 0 0 8^1
This says that \(\frac{\partial y_{co}}{\partial(\textrm{y\_offset})} = -3.4019914795086181\times 10^{-1}\) and \(\frac{\partial y_{co}}{\partial(\textrm{tilt})} = -3.4327120607787705\times 10^{-2}\). See the orbit response around the ring:
dy_dy_offset = map(x->x[[0,0,0,0,0,0,1]], tw.y)
dy_dtilt = map(x->x[[0,0,0,0,0,0,0,1]], tw.y)
# Plot the response around the ring:
f = Figure(fontsize=22, size=(800,500));
ax = Axis(f[1, 1], xlabel="s [m]");
lines!(ax, tw.s, dy_dy_offset, label=L"\frac{\partial y_{co}}{\partial(\textrm{y\_offset})}\ \textrm{(Exact)}"); # L".." specifies a LaTeX string
lines!(ax, tw.s, dy_dtilt, label=L"\frac{\partial y_{co}}{\partial(\textrm{tilt})}\ \textrm{(Exact)}");
axislegend(position=:lt);
display(f);
Now let’s compare to finite differencing:
# First get rid of TPSA parameters in ring:
scalarize!(ring)
# Now set up finite differences:
step = 1e-6
# First dy/dy_offset
qf_1.y_offset = step
b0_1.tilt = 0
t = twiss(ring)
# scalar gets zeroth order part
dy_dy_offset_FD = scalar.(t.y)/step
# Now dy/dtilt:
qf_1.y_offset = 0
b0_1.tilt = step
t = twiss(ring)
dy_dtilt_FD = scalar.(t.y)/step
# And plot:
# Plot the response around the ring:
f = Figure(fontsize=22, size=(800,500));
ax = Axis(f[1, 1], xlabel="s [m]");
lines!(ax, t.s, dy_dy_offset_FD, label=L"\frac{\partial y_{co}}{\partial(\textrm{y\_offset})}\ \textrm{(Finite differences)}"); # L".." specifies a LaTeX string
lines!(ax, t.s, dy_dtilt_FD, label=L"\frac{\partial y_{co}}{\partial(\textrm{tilt})}\ \textrm{(Finite differences)}");
axislegend(position=:lt);
display(f);
Finite differences is usually ok for first order derivatives, but suffers significant error beyond that. Thanks to SciBmad’s automatic differentiability, we can exactly compute the nonlinear derivatives. Simply specify a higher order for the GTPSA Descriptor and follow the same procedure above.