Nonlinear Twiss

This Jupyter notebook introduces SciBmad’s parametric nonlinear normal form routines, with the goal of familiarizing users with Truncated Power Series Algebra (TPSA) methods in accelerator physics. Various “nonlinear Twiss” calculations are made using the Electron Storage Ring of the Electron-Ion Collider. E.g., we will compute the beta function as a Taylor series in the momentum deviation \(\delta\), and can also do so as a Taylor series in variations of some parameters, e.g. sextupole strengths.

Map-based perturbation theory is an approach to perturbation theory introduced by Dragt and Finn, and developed in great detail for accelerator physics applications by Forest back in the early 90s. It is the only fully general and self consistent way to compute all such important quantities of the map, without any approximations up to the truncation order of the Taylor series. All quantities computed by SciBmad use this most powerful and general formalism.

using SciBmad
using CairoMakie # Plotting package
set_theme!(theme_latexfonts()) # Set plotting package theme to LaTeX
include("../../lattices/esr-v6.3.1.jl") # ESR lattice in lattices directory of SciBmad repo
Beamline:
 species_ref = electron
 pc_ref = 1.784626263654482e10

  Index   Name      Kind         L [m]                 s [m]                s_ ⋯
  1       IP6       Marker       0.0f0                 0.0f0                0. ⋯
  2       DRIFT0    Drift        5.3                   0.0f0                5. ⋯
  3       Q1ER_6    Quadrupole   1.8                   5.3                  7. ⋯
  4       DRIFT1    Drift        0.5                   7.1                  7. ⋯
  5       Q2ER_6    Quadrupole   1.4                   7.6                  9. ⋯
  6       DRIFT2    Drift        7.74999999999994      9.0                  16 ⋯
  7       D2ER_6    SBend        5.500091667736123     16.74999999999994    22 ⋯
  8       DRIFT3    Drift        19.81459999999994     22.25009166773606    42 ⋯
  9       SQ3ER_6   Quadrupole   0.25                  42.064691667736      42 ⋯
  10      DRIFT4    Drift        0.15000000000000568   42.314691667736      42 ⋯
  11      Q3ER_6    Quadrupole   0.6                   42.464691667736005   43 ⋯
  12      DRIFT5    Drift        3.5                   43.06469166773601    46 ⋯
  13      SQ4ER_6   Quadrupole   0.25                  46.56469166773601    46 ⋯
  14      DRIFT6    Drift        0.15000000000000568   46.81469166773601    46 ⋯
  15      Q4ER_6    Quadrupole   0.6                   46.96469166773601    47 ⋯
  16      DRIFT7    Drift        3.5                   47.564691667736014   51 ⋯
  17      SQ5ER_6   Quadrupole   0.25                  51.064691667736014   51 ⋯
  18      DRIFT8    Drift        0.15000000000000568   51.314691667736014   51 ⋯
  19      Q5ER_6    Quadrupole   0.6                   51.46469166773602    52 ⋯
  ⋮       ⋮         ⋮            ⋮                     ⋮                    ⋮  ⋱

                                                  1 column and 6251 rows omitted

Let’s start by just calling twiss:

tw = twiss(ring)
Twiss:
summ:
 q1     = 48.07999999808012
 q2     = 44.140000003805
 etac   = 0.0006088732861226077
 alphac = 0.0006088741059934764

df:
6271×26 DataFrame
  Row │ index  name       kind        s        beta1       phi1       dx       ⋯
      │                               [m]      [m]         [2π]       [m]      ⋯
──────┼─────────────────────────────────────────────────────────────────────────
    1 │     1  IP6        Marker         0.0     0.589999   0.0        4.74366 ⋯
    2 │     2  DRIFT0     Drift          0.0     0.589999   0.0        4.74366
    3 │     3  Q1ER_6     Quadrupole     5.3    48.2002     0.232355   6.01291
    4 │     4  DRIFT1     Drift          7.1   151.952      0.236135   1.05195
    5 │     5  Q2ER_6     Quadrupole     7.6   220.652      0.236569   1.26542 ⋯
    6 │     6  DRIFT2     Drift          9.0   333.073      0.237332   1.5499e
    7 │     7  D2ER_6     SBend         16.75  233.455      0.241756   1.27366
  ⋮   │   ⋮        ⋮          ⋮          ⋮         ⋮           ⋮           ⋮   ⋱
 6266 │  6266  Q1EF_6     Quadrupole  3821.63  701.78      47.8408    -2.01029
 6267 │  6267  DRIFT2661  Drift       3823.24  575.384     47.8412    -1.81637 ⋯
 6268 │  6268  Q0EF_6     Quadrupole  3827.0   109.246     47.8436    -7.80829
 6269 │  6269  DRIFT2662  Drift       3828.2    57.607     47.8461    -5.58669
 6270 │  6270  IP6        Marker      3834.0     0.589999  48.08       4.74366
 6271 │    -1  END        ---         3834.0     0.589999  48.08       4.74366 ⋯
                                                20 columns and 6258 rows omitted

To see what properties tw has, use propertynames:

print(propertynames(tw))
[:q1, :q2, :etac, :alphac, :index, :name, :kind, :s, :beta1, :phi1, :dx, :x, :beta2, :phi2, :dy, :y, :slip, :alpha1, :alpha2, :px, :py, :z, :pz, :dpx, :dpy, :gammac, :c11, :c12, :c21, :c22]

The output columns of twiss can be customized using the cols keyword argument. For all possible columns, see ??twiss.

We can plot the betas:

f = Figure(fontsize=22, size=(800,500));
ax = Axis(f[1, 1], xlabel="s [m]", ylabel="Beta Function [m]");
lines!(ax, tw.s, tw.beta1, label=L"\beta_1"); # L".." specifies a LaTeX string
lines!(ax, tw.s, tw.beta2, label=L"\beta_2");
axislegend(position=:rt);
display(f);

Note that the plot shows \(\beta_1\) and \(\beta_2\) using a Teng-Edwards coupling formalism.

The closed orbit can be accessed from fields x, px, y, py, z, and pz, and the periodic dispersion can be obtained from dx, dpx, dy, and dpy. The syntax d<column>_<order> can be used for any scalar-valued to take a chromatic derivative, and by omitting _<order> the order is assumed to be 1.

f = Figure(fontsize=22, size=(800,500));
ax = Axis(f[1, 1], xlabel="s [m]", ylabel="Dispersion [m]");
lines!(ax, tw.s, tw.dx, label=L"\eta_x"); # L".." specifies a LaTeX string
lines!(ax, tw.s, tw.dy, label=L"\eta_y");
axislegend(position=:rt);
display(f);

Higher order chromatic quantities can be computed by setting the chromatic order chrom appropriately, and requesting the corresponding column in the twiss result using cols. E.g., to get the second order dispersions \(\frac{\partial^2x}{\partial\delta^2}\), \(\frac{\partial^2y}{\partial\delta^2}\) as well as the chromatic beta beat \(\frac{\partial\beta_1}{\partial\delta}\), \(\frac{\partial\beta_2}{\partial\delta}\):

tw2 = twiss(ring; cols=["dx_2", "dy_2", "beta1", "dbeta1", "beta2", "dbeta2"], chrom=2)
Twiss:
summ:
 q1     = 48.07999999808012 + 1.0002276718873708 δ
 q2     = 44.140000003805 + 1.0008473253740475 δ
 etac   = 0.0006088732861225831 + 0.0011325507772318268 δ
 alphac = 0.00060887410599342 + 0.0011325483186177044 δ

df:
6271×10 DataFrame
  Row │ index  name       kind        s        dx_2       dy_2         beta1   ⋯
      │                               [m]                              [m]     ⋯
──────┼─────────────────────────────────────────────────────────────────────────
    1 │     1  IP6        Marker         0.0    -13.9699   4.89151e-5    0.589 ⋯
    2 │     2  DRIFT0     Drift          0.0    -13.9699   4.89151e-5    0.589
    3 │     3  Q1ER_6     Quadrupole     5.3     32.5881   0.00436896   48.200
    4 │     4  DRIFT1     Drift          7.1     63.3539   0.00413133  151.952
    5 │     5  Q2ER_6     Quadrupole     7.6     77.1023   0.00360006  220.652 ⋯
    6 │     6  DRIFT2     Drift          9.0     96.3624   0.00282745  333.073
    7 │     7  D2ER_6     SBend         16.75    88.5706   0.00219472  233.455
  ⋮   │   ⋮        ⋮          ⋮          ⋮         ⋮           ⋮           ⋮   ⋱
 6266 │  6266  Q1EF_6     Quadrupole  3821.63  -211.083   -0.0027384   701.78
 6267 │  6267  DRIFT2661  Drift       3823.24  -192.16    -0.00311756  575.384 ⋯
 6268 │  6268  Q0EF_6     Quadrupole  3827.0    -86.4911  -0.00489041  109.246
 6269 │  6269  DRIFT2662  Drift       3828.2    -64.9202  -0.00467868   57.607
 6270 │  6270  IP6        Marker      3834.0    -13.9699   4.89151e-5    0.589
 6271 │    -1  END        ---         3834.0    -13.9699   4.89151e-5    0.589 ⋯
                                                 4 columns and 6258 rows omitted

Note the chromaticities have also been computed

f = Figure(fontsize=22, size=(800,500));
ax = Axis(f[1, 1], xlabel="s [m]", ylabel="Second Order Dispersion [m]");
lines!(ax, tw2.s, tw2.dx_2, label=L"\partial_\delta\eta_x"); # L".." specifies a LaTeX string
lines!(ax, tw2.s, tw2.dy_2, label=L"\partial_\delta\eta_y");
axislegend(position=:rt);
display(f);
f = Figure(fontsize=22, size=(800,500));
ax = Axis(f[1, 1], xlabel="s [m]", ylabel="Chromatic Beta-Beat");
lines!(ax, tw2.s, tw2.dbeta1 ./ tw2.beta1, label=L"\partial_\delta\beta_1"); # L".." specifies a LaTeX string
lines!(ax, tw2.s, tw2.dbeta2 ./ tw2.beta2, label=L"\partial_\delta\beta_2");
axislegend(position=:rt);
display(f);

We can also write the Montague functions, which are phase invariants in achromatic regions, as

\[W = \sqrt{A^2+B^2} \ , \ A=\frac{\partial\alpha}{\partial\delta}-\frac{\alpha}{\beta}\frac{\partial\beta}{\partial\delta} \ , \ B=\frac{1}{\beta}\frac{\partial\beta}{\partial\delta} \ .\]
tww = twiss(ring; cols=["w1", "w2"], chrom=2)
Twiss:
summ:
 q1     = 48.07999999808012 + 1.0002276718873708 δ
 q2     = 44.140000003805 + 1.0008473253740475 δ
 etac   = 0.0006088732861225831 + 0.0011325507772318268 δ
 alphac = 0.00060887410599342 + 0.0011325483186177044 δ

df:
6271×6 DataFrame
  Row │ index  name       kind        s        w1        w2       
      │                               [m]      [1]       [1]      
──────┼───────────────────────────────────────────────────────────
    1 │     1  IP6        Marker         0.0    9.45204   13.0064
    2 │     2  DRIFT0     Drift          0.0    9.45204   13.0064
    3 │     3  Q1ER_6     Quadrupole     5.3   16.8513    90.6522
    4 │     4  DRIFT1     Drift          7.1   34.6455   105.245
    5 │     5  Q2ER_6     Quadrupole     7.6   47.3488   119.518
    6 │     6  DRIFT2     Drift          9.0   59.043    132.939
    7 │     7  D2ER_6     SBend         16.75  60.1801   133.801
  ⋮   │   ⋮        ⋮          ⋮          ⋮        ⋮         ⋮
 6266 │  6266  Q1EF_6     Quadrupole  3821.63  66.9226   137.415
 6267 │  6267  DRIFT2661  Drift       3823.24  60.7257   134.087
 6268 │  6268  Q0EF_6     Quadrupole  3827.0   13.2809   111.098
 6269 │  6269  DRIFT2662  Drift       3828.2    7.82958  105.719
 6270 │  6270  IP6        Marker      3834.0    9.45204   13.0064
 6271 │    -1  END        ---         3834.0    9.45204   13.0064
                                                 6258 rows omitted
f = Figure(fontsize=22, size=(800,500));
ax = Axis(f[1, 1], xlabel="s [m]", ylabel="Montague Function");
lines!(ax, tww.s, tww.w1, label=L"W_1"); # L".." specifies a LaTeX string
lines!(ax, tww.s, tww.w2, label=L"W_2");
axislegend(position=:rt);
display(f);

Using the operator notation, the nonlinear effective Hamiltonian is defined as the polynomial \(h\) in

\[\mathcal{M} = \mathcal{A}_{cs}^{-1}\exp{(: h : )} \mathcal{R} \mathcal{A}_{cs}\]

where \(\mathcal{M}\) is the compositional operator representing the one turn map and \(\mathcal{A}_{cs}\) is the compositional operator representing only a linear (Courant Snyder) normalizing transformation. Monomials of \(h\) are sometimes referred to as “resonance driving terms” and “detune coefficients”.

We can extract any monomial of the nonlinear effective Hamiltonian by simply setting the order of twiss appropriately (must be at least one less than the total order of the monomial). And, if the beam is coasting, we can take chromatic derivatives.

For example to compute \(\frac{\partial h_{2000}}{\partial\delta}\):

twh = twiss(ring; cols=["dh2000"], order=2)
Twiss:
summ:
 q1     = 48.07999999808012 + 1.0002276718873144 δ
 q2     = 44.140000003805 + 1.0008473253738335 δ
 etac   = 0.0006088732861226077 + 0.0011325507772322252 δ
 alphac = 0.0006088741059934445 + 0.0011325483186181028 δ

df:
6271×5 DataFrame
  Row │ index  name       kind        s        dh2000              
      │                               [m]                          
──────┼────────────────────────────────────────────────────────────
    1 │     1  IP6        Marker         0.0   0.743645-2.15193im
    2 │     2  DRIFT0     Drift          0.0   0.743645-2.15193im
    3 │     3  Q1ER_6     Quadrupole     5.3     1.1817+3.88328im
    4 │     4  DRIFT1     Drift          7.1    3.30673+7.66223im
    5 │     5  Q2ER_6     Quadrupole     7.6    4.81905+10.3371im
    6 │     6  DRIFT2     Drift          9.0    6.15191+12.8227im
    7 │     7  D2ER_6     SBend         16.75   5.47572+13.422im
  ⋮   │   ⋮        ⋮          ⋮          ⋮              ⋮
 6266 │  6266  Q1EF_6     Quadrupole  3821.63  -8.81775-13.4946im
 6267 │  6267  DRIFT2661  Drift       3823.24  -8.07258-12.1981im
 6268 │  6268  Q0EF_6     Quadrupole  3827.0   -2.60198-1.86111im
 6269 │  6269  DRIFT2662  Drift       3828.2   -1.86129-0.304146im
 6270 │  6270  IP6        Marker      3834.0   0.743645-2.15193im
 6271 │    -1  END        ---         3834.0   0.743645-2.15193im
                                                  6258 rows omitted
f = Figure(fontsize=22, size=(800,500));
ax = Axis(f[1, 1], xlabel="s [m]");
lines!(ax, twh.s, abs.(twh.dh2000), label=L"|\partial h_{2000}/\partial\delta|");
axislegend(position=:rt);
display(f);

Notice that this looks quite a lot like the Montague function - this is correct, and can be intuitively understood because \(h_{2000}\) quantifies the beta-beating oscillating at \(2\times\) the betatron frequency, and \(\partial h_{2000}/\partial\delta\) describes how that depends on \(\delta\).

Of course we can also look at geometric terms:

twh2 = twiss(ring, cols=["h3000", "h2100", "h1020", "h1101", "h1002"], order=2)
f = Figure(fontsize=22, size=(800,500));
ax = Axis(f[1, 1], xlabel="s [m]");
lines!(ax, twh2.s, abs.(twh2.h3000), label=L"|\partial h_{h3000}/\partial\delta|");
lines!(ax, twh2.s, abs.(twh2.h2100), label=L"|\partial h_{h2100}/\partial\delta|");
lines!(ax, twh2.s, abs.(twh2.h1020), label=L"|\partial h_{h1020}/\partial\delta|");
lines!(ax, twh2.s, abs.(twh2.h1101), label=L"|\partial h_{h1101}/\partial\delta|");
lines!(ax, twh2.s, abs.(twh2.h1002), label=L"|\partial h_{h1002}/\partial\delta|");
axislegend(position=:rt);
display(f);

Because the sextupoles in the arcs have been arranged in \(-I\) pairs, the geometric terms cancel themselves locally.

Spin analysis is also provided in twiss, by setting spin=true.

tws = twiss(ring; spin=true)
f = Figure(fontsize=22, size=(800,500));
ax = Axis(f[1, 1], xlabel="s [m]");
lines!(ax, tws.s, tws.dnx, label=L"\partial n_x/\partial\delta");
lines!(ax, tws.s, tws.dny, label=L"\partial n_y/\partial\delta");
lines!(ax, tws.s, tws.dnz, label=L"\partial n_z/\partial\delta");
dn_amp = @. sqrt(tws.dnx^2 + tws.dny^2 + tws.dnz^2)
lines!(ax, tws.s, dn_amp, label=L"|\partial \hat{n}/\partial\delta|");
axislegend(position=:rt);
display(f);

Also note the (amplitude-dependent) spin tune will be calculated:

tws.qspin
AmplitudeDependentValue:
 -0.4999997778639269 - 39.9999997367878 δ

For higher order tunes, set the order higher:

twtune = twiss(ring; at=[], spin=true, order=3); # at=[] means only compute tunes
twtune.qspin
AmplitudeDependentValue:
 -0.4999997778639269 - 39.9999997367878 δ - 0.05106264795980088 J₁ + 2377.9662316135586 J₂ - 1.5845345679169045 δ² + 262.58859924109555 δ J₁ + 245142.30931396713 δ J₂ + 468.7335796509802 δ³

To get a particular term in the Taylor series, use the function getterm:

getterm(twtune.qspin, delta=1, J1=1)
262.58859924109555

Finally, ,more advanced users who have become familiar with GTPSA.jl can get the output as Taylor series by setting the flag as_taylor_series=true. A custom GTPSA descriptor can also be provided with GTPSA_descriptor for doing parametric normal form analysis (see the orbit-response.ipynb example notebook).

twisf = twiss(ring; at=[1], cols=["nx","ny","nz"], spin=true, as_taylor_series=true)
n = [twisf.nx[1], twisf.ny[1], twisf.nz[1]]
3-element Vector{TPS64{GTPSA.Dynamic}}:
Descriptor(NV=6, MO=1)
INDEX  COEFFICIENT             ORDER   EXPONENTS
----------------------------------------------------
  1:  -1.7014762942609857E-15    0     0 0  0 0  0 0
  1:   2.9437795671891087E-04    1     1 0  0 0  0 0
  1:  -4.1507516882558846E+01    1     0 1  0 0  0 0
  1:  -3.3567033718986136E+02    1     0 0  1 0  0 0
  1:  -3.1223550771310529E+00    1     0 0  0 1  0 0
  1:  -4.3422439755247755E-06    1     0 0  0 0  0 1
----------------------------------------------------
  2:   1.9500601133870532E-10    0     0 0  0 0  0 0
  2:  -1.3657465543940380E-04    1     1 0  0 0  0 0
  2:   1.2501396756456440E-04    1     0 1  0 0  0 0
  2:  -5.6419852071752892E+01    1     0 0  1 0  0 0
  2:  -2.4600820432436976E+01    1     0 0  0 1  0 0
  2:  -1.5707985192657588E+00    1     0 0  0 0  0 1
----------------------------------------------------
  3:  -1.0000000000000000E+00    0     0 0  0 0  0 0
  3:  -2.6633109359471688E-14    1     1 0  0 0  0 0
  3:   9.5002310367242166E-14    1     0 1  0 0  0 0
  3:  -1.1001636479876143E-08    1     0 0  1 0  0 0
  3:  -4.7973006456281294E-09    1     0 0  0 1  0 0
  3:  -3.0631502889363634E-10    1     0 0  0 0  0 1