Parametric Normal Form

Everything that twiss computes can also be computed as a function of parameters of the lattice: for example how the tunes depend on magnet strengths, or how the beta functions depend on a length. These dependences are computed exactly, by automatic differentiation, and not by finite differences.

There are two ways to do this. The simplest is to write the quantities and the parameters as expressions, and let SciBmad compute their derivatives. The second is to make the parameters Taylor series yourself, with the GTPSA package, and get the complete dependence on them.

Derivatives with respect to lattice parameters

As an example, take a FODO cell with sextupoles:

@elements begin
  qf = Quadrupole(Kn1=0.36, L=0.5)
  sf = Sextupole(Kn2=1.2, L=0.2)
  d = Drift(L=0.1)
  b = SBend(L=1.2, angle=pi/132)
  qd = Quadrupole(Kn1=-0.36, L=0.5)
  sd = Sextupole(Kn2=-1.2, L=0.2)
end

fodo = Beamline([qf, sf, b, d, qd, sd, b, d],
        species_ref=Species("electron"), pc_ref=18e9)
Beamline:
 species_ref = electron
 pc_ref = 1.8e10

  Index   Name   Kind         L [m]   s [m]                s_downstream [m]   
  1       qf     Quadrupole   0.5     0.0f0                0.5
  2       sf     Sextupole    0.2     0.5                  0.7
  3       b      SBend        1.2     0.7                  1.9
  4       d      Drift        0.1     1.9                  2.0
  5       qd     Quadrupole   0.5     2.0                  2.5
  6       sd     Sextupole    0.2     2.5                  2.7
  7       b      SBend        1.2     2.7                  3.9000000000000004
  8       d      Drift        0.1     3.9000000000000004   4.0

We first construct a lazy twiss with lazy_twiss, which is basically a lazily-evaluated twiss call. It takes the same arguments as twiss, except at, cols, base_cols, as_taylor_series and GTPSA_descriptor, which are determined automatically given what you request. Its quantities are read identically as the summary values and columns of a twiss result are read(see Twiss), but nothing is computed yet. The parameters are given as knobs, e.g. @knob(qf.Kn1) for the strength of qf:

tw = lazy_twiss(fodo)
kf = @knob(qf.Kn1)
kd = @knob(qd.Kn1)
qd.Kn1

SciBmad.value_and_jacobian computes the values of the quantities and their derivatives with respect to the knobs. It is not exported, because DifferentiationInterface exports a function of the same name. The Jacobian has one row per quantity and one column per knob:

v, J = SciBmad.value_and_jacobian([tw.q1, tw.q2], [kf, kd])
J # [dq1/dkf dq1/dkd; dq2/dkf dq2/dkd]
2×2 Matrix{Float64}:
  0.553969   0.409162
 -0.416281  -0.563555

The same works for any column, at any location. For example, how the beta function and the dispersion at qd depend on the quadrupole strengths:

v, J = SciBmad.value_and_jacobian([tw.beta1[qd], tw.dx[qd]], [kf, kd])
J # [dbeta1/dkf dbeta1/dkd; ddx/dkf ddx/dkd]
2×2 Matrix{Float64}:
 -110.133   -78.0898
  -33.0176  -24.1711

And how the chromaticities depend on the sextupole strengths:

v, J = SciBmad.value_and_jacobian([tw.dq1, tw.dq2], [@knob(sf.Kn2), @knob(sd.Kn2)])
J # [ddq1/dksf ddq1/dksd; ddq2/dksf ddq2/dksd]
2×2 Matrix{Float64}:
  0.386295   0.271174
 -0.310178  -0.349452

No truncation order has to be chosen: each quantity knows the order it needs, here the second order in δ for the chromaticities.

Any parameter that can be set with object.parameter = value can be a knob: an element’s strength, length or misalignment, or a variable of context (see Knobs).

value computes only the values. deriv(quantity, knob) is the derivative as a new quantity, so that derivatives can be taken again. For example, the second derivative of the horizontal tune with respect to the strength of qf:

value(deriv(deriv(tw.q1, kf), kf)) # -5.5676
-5.567590565172149

Or how the first δ-derivative of the resonance driving term \(h_{2000}\) at the beginning depends on the strength of sf (the h terms are complex numbers):

value(deriv(tw.dh2000[1], @knob(sf.Kn2))) # 1.209 - 0.1048im
1.2090042121656435 - 0.10477967524643658im

These expressions are also what is matched in Matching and Optimization.

Taylor series in the parameters with GTPSA

The derivatives above are computed by making the knobs parameters of the truncated power series that twiss computes with, and restoring the lattice afterwards. This can also be done by hand, to get the complete Taylor series in the parameters. It uses the truncated power series package GTPSA, whose quickstart guide explains the ideas. The normal forms themselves are computed by the NonlinearNormalForm package.

First, a GTPSA Descriptor defines the variables, the parameters, and their truncation orders. Here, it has the six phase space variables to order 1, two parameters (the changes of the two quadrupole strengths) to order 1, and a maximum total order of 2:

dnf = Descriptor([1, 1, 1, 1, 1, 1], 2, [1, 1], 1)
Descriptor(NV=6, MO=2, NP=2, PO=1, NO=[ 1 1  1 1  1 1])

The tunes and the beta functions come from the first-order terms of the map in the phase space variables. Their first-order dependence on a parameter is therefore in terms of total order 2: one variable times one parameter. Higher orders work too, but take longer to compute.

The parameters are then added to the strengths:

dk = params(dnf)
qf.Kn1 = 0.36 + dk[1]
qd.Kn1 = -0.36 + dk[2]
TPS64{GTPSA.Dynamic}:
Descriptor(NV=6, MO=2, NP=2, PO=1, NO=[ 1 1  1 1  1 1])
 COEFFICIENT             ORDER   EXPONENTS
-3.5999999999999999E-01    0     0 0  0 0  0 0
 1.0000000000000000E+00    1     0 0  0 0  0 0  8^1

and twiss is given the Descriptor with GTPSA_descriptor:

twp = twiss(fodo, GTPSA_descriptor=dnf)
Twiss:
summ:
 q1     = 0.05344930466011301 + 0.08756175774882742 δ + 0.5539690604874662 Δk₁ + 0.4091622849161083 Δk₂
 q2     = 0.05253999423230936 - 0.004310546648378328 δ - 0.41628097933541114 Δk₁ - 0.5635546864898776 Δk₂
 etac   = 0.02021231039014999 - 0.4191932585716806 Δk₁ - 0.3103664171777643 Δk₂
 alphac = 0.020212311196075756 - 0.41919325857168055 Δk₁ - 0.31036641717776425 Δk₂

df:
9×26 DataFrame
 Row │ index  name  kind        s    beta1                              phi1                       ⋯
     │                          [m]  [m]                                [2π]                       ⋯
─────┼──────────────────────────────────────────────────────────────────────────────────────────────
   1 │     1  qf    Quadrupole  0.0  TPS64{Dynamic}:\nDescriptor(NV=6…  TPS64{Dynamic}:\nDescripto ⋯
   2 │     2  sf    Sextupole   0.5  TPS64{Dynamic}:\nDescriptor(NV=6…  TPS64{Dynamic}:\nDescripto
   3 │     3  b     SBend       0.7  TPS64{Dynamic}:\nDescriptor(NV=6…  TPS64{Dynamic}:\nDescripto
   4 │     4  d     Drift       1.9  TPS64{Dynamic}:\nDescriptor(NV=6…  TPS64{Dynamic}:\nDescripto
   5 │     5  qd    Quadrupole  2.0  TPS64{Dynamic}:\nDescriptor(NV=6…  TPS64{Dynamic}:\nDescripto ⋯
   6 │     6  sd    Sextupole   2.5  TPS64{Dynamic}:\nDescriptor(NV=6…  TPS64{Dynamic}:\nDescripto
   7 │     7  b     SBend       2.7  TPS64{Dynamic}:\nDescriptor(NV=6…  TPS64{Dynamic}:\nDescripto
   8 │     8  d     Drift       3.9  TPS64{Dynamic}:\nDescriptor(NV=6…  TPS64{Dynamic}:\nDescripto
   9 │    -1  END   ---         4.0  TPS64{Dynamic}:\nDescriptor(NV=6…  TPS64{Dynamic}:\nDescripto ⋯
                                                                                  21 columns omitted

With parameters in the Descriptor, twiss returns its results as Taylor series (as_taylor_series=true). The printed tunes now include their dependence on the strengths of qf and qd. grad gives the first derivatives with respect to the parameters as a vector:

grad(twp.q1) # [dq1/dkf, dq1/dkd], as above
2-element Vector{Float64}:
 0.5539690604874662
 0.4091622849161083
grad(twp.beta1[qd]) # [dbeta1/dkf, dbeta1/dkd]
2-element Vector{Float64}:
 -110.1325314963556
  -78.08981984499752

Afterwards, the strengths of qf and qd are still Taylor series, even if numbers are set back into them. Before using the beamline again, call scalarize!. It replaces every Taylor series in the beamline by its value (its 0th order part):

scalarize!(fodo)
Beamline:
 species_ref = electron
 pc_ref = 1.8e10

  Index   Name   Kind         L [m]   s [m]                s_downstream [m]   
  1       qf     Quadrupole   0.5     0.0f0                0.5
  2       sf     Sextupole    0.2     0.5                  0.7
  3       b      SBend        1.2     0.7                  1.9
  4       d      Drift        0.1     1.9                  2.0
  5       qd     Quadrupole   0.5     2.0                  2.5
  6       sd     Sextupole    0.2     2.5                  2.7
  7       b      SBend        1.2     2.7                  3.9000000000000004
  8       d      Drift        0.1     3.9000000000000004   4.0

Warning

Mixing Taylor series of two different Descriptors aborts the Julia process, with an error of the GTPSA C library. For example, twiss(fodo) above, before scalarize!, would abort Julia, because twiss uses its own Descriptor.