\chapter{Single-particle tracking}

XTrack is a 6D single particle symplectic tracking code used to compute the
trajectories of individual relativistic charged particles in circular
accelerators. It has been developed based on SixTrack.

The physical models are collected from the main references
\cite{ripken85,barber87,ripken95,heinemann95,barber96,beam_beam,rf_multipoles},
which contain more details of the derivation of the maps.


\section{Notation and reference frame}

The speed, momentum, energy, rest mass, charge of a particle are indicated
by $v$, $P$, $E$, $m$ and $q$, respectively.  These quantities are
related by the following equations:
\begin{align}
  v&=\beta c &
  E^2-P^2c^2&=m^2c^4 &
  E & = \gamma mc^2 &
  Pc & =\beta E
\end{align}
where $\beta$ and $\gamma$ are the relativistic factors.

In a curvilinear reference frame defined by a constant curvature $h_x$ in the
$\hat X, \hat Z$ plane and parameterized by $s$, the
position of the particle at a time $t$ can be written as:
\begin{align}
  \vec Q(t)= \vec r(s) + x \,\hat x(s) + y\, \hat y(s),
\end{align}
and therefore identified by the coordinates $s, x, y, t$ in the reference frame
defined by $\hat x(s)$ and $\hat y(s)$. In particle tracking, $s$ is normally
used as independent parameter and $t$ as a coordinate.

The electromagnetic fields {\bf E} and {\bf B} can be derived in a curvilinear
reference frame from the potentials $V(x,y,s,t)$ and $\mathbf{A}(x,y,s,t)$, where
\begin{align}
\mathbf{A}(x,y,s,t)=A_x(x,y,s,t) \hat x(s) + A_y(x,y,s,t) \hat y(s) + A_s(x,y,s,t) \hat z(s)
\end{align}
and for which:
\begin{align}
  \mathbf{E}  &= -\nabla V - \frac{\partial \mathbf{A}}{\partial t} 
               = -\partial_x V \hat x - \partial_y V \hat y -
  \frac{1}{1+h x}  \partial_s V \hat z - \partial_t \mathbf{A}\\
  \mathbf{B} &= \nabla\times\mathbf{A}  =
  \left(\partial_y A_s - \frac{\partial_s  A_y}{1+h x} \right) \hat x 
  +\left(\frac{\partial_s A_x-\partial_x (1+h x) A_s }{1+h x} \right)\hat y \\
  &+\quad \left(\partial_x A_y - \partial_y A_x \right) \hat z.
\end{align}
In this reference frame the canonical momenta are:
\begin{align}
  P_x&=m \gamma \dot x + q A_x, &
  P_y&=m \gamma \dot y + q A_y, &
  P_s&=m \gamma \dot s (1 + h x)^2 + q (1 + h x) A_s.
\end{align}
and the energy of a particle and the field is
\begin{align}
E=qV + c \sqrt{(mc)^2
              +\frac{(P_s- q A_s(1+hx))^2}{(1+hx)^2}
              +(P_x-q A_x)^2 + (P_x-q A_x)^2}.
\end{align}







\section{Hamiltonian and particle coordinates}

If $s(t)$ is monotonically increasing, it is possible to derive the equations
of motion using $s$ as the independent parameter, $(-t, E)$ as conjugate coordinates and $-P_s$ as Hamiltonian.

\begin{align}
  P_s&= (1+h x) \left( 
       \sqrt{ \frac{(E-q\phi)^2}{c^2} - (mc)^2
           - (P_x - q A_x)^2
           - (P_y - q A_y)^2}
       +q A_s
    \right)
\end{align}

Since in accelerators the orbits of the 
particles are often a perturbation of the reference trajectory followed by a
particle with rest mass $m_0$, charge $q_0$, speed $\beta_0 c$ and momentum
$P_0$, one could use the following derived quantities that usually assume small
values:
\begin{align}
p(x,y) &=
  \frac{m_0}{m}\frac{P(x,y)}{P_0}   &
\chi &=
  \frac{q}{q_0}\frac{m_0}{m} &
a(x,y,s) &=
  \frac{q_0}{P_0}  A(x,y,s)
\end{align}
Note that here $m$ is used to indicate the rest mass of particles of species
different from the reference particle (which has mass $m_0$) and not the relativistic mass.
Further rescaling the energy and charge density as
\begin{align}
  e(x,y,s) &=
  \frac{m_0}{m}\frac{E(x,y,s)}{P_0}   &
  \varphi(x,y,s) &=
  \frac{q_0}{P_0c}\phi(x,y,s) \,,
\end{align}
% This allows us to rewrite the Hamiltonian as:
% \begin{equation}\notag
%   - (1+h x) \, P_0\frac{m}{m_0}\left(
%       \sqrt{
%           \frac{\left(e - q\varphi\right)^2}{c^2} 
%             - \frac{1}{\beta_0^2\gamma_0^2}
%        - (p_x - \chi a_x)^2
%        - (p_y - \chi a_y)^2}
%        + \chi a_s
%     \right)
% \end{equation}
and as all canonical momenta scale with the same factor, we can define a new Hamiltonian
$\tilde{H}$ that still satisfies the same equations of motion:
\begin{align}\notag
  &\tilde{H}(x,y,-t, p_x, p_y, e) =
      \frac{m_0}{m}\frac{1}{P_0}H(x,y,-t, P_x, P_y, E) \\
  &\tilde{H}=
    - (1+h x) \left(
      \sqrt{ 
          \left(\frac{e}{c} - \chi\varphi\right)^2
            - \frac{1}{\beta_0^2\gamma_0^2}
           - (p_x - \chi a_x)^2
           - (p_y - \chi a_y)^2}
       + \chi a_s
    \right)
\end{align}

\subsection{Longitudinal coordinates}

Different sets of longitudinal coordinates can be used:
\begin{align}
\xi &= s \frac{\beta}{\beta_0} - \beta c t &
\tau &= \frac{s}{\beta_0} - ct &
\zeta &= s - \beta_0 ct \label{eq:coord1}\\
\delta &=
  \frac{P \frac{m_0}{m} -P_0}{P_0} &
p_\tau &=
  \frac{1}{\beta_0} \frac{E \frac{m_0}{m} -E_0}{E_0} &
p_\zeta &=
  \frac{1}{\beta_0^2}\frac{E \frac{m_0}{m} -E_0}{E_0}
  \label{eq:coord2}
\end{align}
where variables in the same columns are canonically conjugate.

%As the position of the reference particle is given by
%$s=\beta_0ct_0$ we see that $\tau$ is the time advance of the tracked %particle w.r.t.\ 
%the reference particle:
%\begin{equation}
%  \tau = c\left(t_0-t\right) = c\Delta t
%\end{equation}
%This is the coordinate used by MAD-X. In Xsuite the pair $(\zeta, \delta)$ is used; note that
%these are not canonical as the canonical conjugates are the pairs $(\xi, \delta)$, $(\tau, p_\tau)$,
%and  $(\zeta, p_\zeta)$.

The different longitudinal variables can be easily related to each other:
\begin{align}
  &\xi=
      \beta\tau =
      \frac{\beta}{\beta_0}\zeta \\
  &p_\tau = \beta_0 p_\zeta\\
  &\delta= \sqrt{p_\tau^2 + 2 \frac{p_\tau}{\beta_0} +1} - 1
       =\beta p_\tau + \frac{\beta-\beta_0}{\beta_0}\\
  &\delta= \sqrt{\beta_0^2p_\zeta^2 + 2 p_\zeta +1} - 1
      =\beta\beta_0 p_\zeta + \frac{\beta-\beta_0}{\beta_0}\\
  &\gamma = \gamma_0 (1 + \beta_0 p_\tau)\\
  &\beta = \sqrt{1 - \frac{1 - \beta_0}{\left(1 + \beta_0 p_\tau\right)^2}}
\end{align}

For small energy deviations ($\delta \ll 1$, $p_\tau \ll 1$, $p_\zeta \ll 1$),
we can neglect the terms of order $\delta^2$, $p_\tau^2$, $p_\zeta^2$ and higher, hence
the following approximations hold:
\begin{align}
  &\delta \simeq \frac{p_\tau}{\beta_0} \\
  &\delta \simeq p_\zeta \\
  &\beta \simeq \beta_0 + (1 - \beta_0^2) p_\tau
\end{align}

\subsection{Hamiltonian expressions}

The conjugate pairs can be generated by the following generating functions \footnote{
$F_2(-t , p_{\rm new}, s)$,\,
$e = \frac{\partial F_2}{\partial (-t)}$, \,
$q_{\rm new} = \frac{\partial F_2}{\partial p_{\rm new}}$,\,
$H_{\rm new} = H + \frac{\partial F_2}{\partial s}$
}

\begin{align}
F_2& = x p_x + y p_y + \left(\frac{s}{\beta_0}-ct\right)
                            \frac{1+\delta}{\beta} \\
F_2& = x p_x + y p_y + \left(\frac{s}{\beta_0}-ct\right)
                            \left(p_\tau + \frac{1}{\beta_0}\right) \\
F_2& = x p_x + y p_y + \left(\frac{s}{\beta_0}-ct\right)
                            \left(\beta_0 p_\zeta + \frac{1}{\beta_0}\right)
\end{align}

The Hamiltonians are then:

% \begin{align}
%  H_\delta   &= \frac{1+\delta}{\beta\beta_0} - \frac{m_0}{m}\frac{P_s}{P_0} &
%  H_\tau   &= \frac{p_\tau}{\beta_0} - \frac{m_0}{m}\frac{P_s}{P_0} &
%  H_\zeta &= p_\zeta - \frac{m_0}{m}\frac{P_s}{P_0}
% \end{align}

\begin{align*}
 H_\delta   &= \frac{1+\delta}{\beta\beta_0} - (1+h x) \left(
      \sqrt{ \left(\frac{1+\delta}{\beta}-\chi\varphi\right)^2
           - \frac{1}{\beta_0^2\gamma_0^2} 
           - (p_x - \chi a_x)^2
           - (p_y - \chi a_y)^2}
       + \chi a_s
    \right) \\
 H_\tau   &= \frac{p_\tau}{\beta_0} - (1+h x) \left(
      \sqrt{ \left(p_\tau+\frac{1}{\beta_0}-\chi\varphi\right)^2
           - \frac{1}{\beta_0^2\gamma_0^2}
           - (p_x - \chi a_x)^2
           - (p_y - \chi a_y)^2}
       + \chi a_s
    \right) \\
  H_\zeta &= p_\zeta - (1+h x) \left(
      \sqrt{ \left(\beta_0 p_\zeta+\frac{1}{\beta_0}-\chi\varphi\right)^2
           - \frac{1}{\beta_0^2\gamma_0^2}
           - (p_x - \chi a_x)^2
           - (p_y - \chi a_y)^2}
       + \chi a_s
    \right)
\end{align*}
% where 
% \begin{align}
% \delta &= \frac{P \frac{m_0}{m} -P_0}{P_0} &
% \chi &= \frac{q}{q_0}\frac{m_0}{m}.
% \end{align}
Note that things get complicated when using the pair $(\xi, \delta)$, as then the
Hamiltonian contains terms in $\beta$, which in turn depends on the energy.  In particular:
\begin{equation}
\frac{\partial \beta}{\partial \delta} =
    \beta\, \frac{1-\beta^2}{1+\delta}
\end{equation}

For this reason we prefer using $H_\tau$ when deriving the equations of motion. Note
that when $\varphi=0$, the Hamiltonian simplifies into:
\begin{equation}
H_\tau  =
\frac{p_\tau}{\beta_0} - (1+h x) \left(
      \sqrt{ \left(1+\delta\right)^2
           - (p_x - \chi a_x)^2
           - (p_y - \chi a_y)^2}
       + \chi a_s
    \right)
\end{equation}

The following identities are useful to derive the equations of motion:

\begin{align}
&\frac{\partial \delta}{\partial p_\tau} =
    \frac{p_\tau+1/\beta_0}{1+\delta} = \frac{1}{\beta} \\
&\frac{\partial}{\partial\delta}\left( 
    \frac{1+\delta}{\beta\beta_0}
  \right)=
    \frac{\beta}{\beta_0}
% \delta&=\sqrt{\beta_0^2 p_\zeta^2 + 2 p_\zeta +1} -1 &
% \frac{d \delta}{d p_\zeta}= \frac{\beta_0^2 p_\zeta+1}{1+\delta} = \frac{\beta_0}{\beta}
\end{align}

\section{Cavity time, energy errors and acceleration}

A cavity kick depends on:

\begin{equation}
\sin(2 \pi f T + \phi)
\end{equation}

where T is laboratory time.


For the most general case:

\begin{equation}
\sin(2 \pi f T + \phi) = \sin\left(2 \pi f \frac{s-\zeta}{\beta_0 c}  + \phi \right)
\end{equation}

Most codes drop the term $2 \pi f s / (\beta_0 c)$ that is

\begin{equation}
\sin(2 \pi f T + \phi) \to  \sin\left(- 2 \pi f \frac{\zeta}{\beta_0 c} + \phi\right)
\end{equation}

to make sure that a particle that is syncrhonous to the reference trajectory is in phase with the cavity.


\subsection{Implementing energy errors} 

One can define

\begin{equation}
\begin{aligned}
s &= s_0 + n (L_0-L) + n L\\
f_{\rm rev} &= \beta_0 c / L\\ 
f &= h f_{\rm rev}
\end{aligned}
\end{equation}

where $s_0$ is the path length at the cavity turn at 0, $L_0$ is the design circumference, $n$ is the turn number, $h$ is the harmonic number, L is the new path length with an energy error. Indeed one could write $L=L_0(1 +\eta \delta_s$) where $\eta$ is a constant property of the lattice.

Multiple cavities can have their own defined $L$.

Using these definitions, then


\begin{align}
\sin(2 \pi f T + \phi) =
&\sin\left(2 \pi h f_{\rm rev} \frac{s_0 + n(L_0-L) -\zeta}{\beta_0 c}  + \phi\right)\\
=&\sin\left(2 \pi h f_{\rm rev} \frac{n(L_0-L) -\zeta}{\beta_0 c}  + \phi'\right)
\end{align}


where $\phi'=\frac{2\pi h s_0}{L} + \phi$.


In MAD-X twiss and MAD8, indeed the longitudinal coordinates is directly $\zeta'=n(L_0-L) -\zeta$ and the term $n(L_0-L)$ is added smoothly in each thick element. This forces all the cavities to share the same $L$ or $f_{\rm rev}$.

In SixTrack or MAD-X track, one could simply define a turn dependent phase
\begin{equation}
\phi=\phi_0 + 2 \pi h f_{\rm rev} n(L_0-L)
\end{equation}
which is very general or in alternative add a special element that perform at each turn the following transformation:

\begin{equation}
\zeta_{\rm new}=(L_0-L) -\zeta_{\rm old} 
\end{equation}

\subsection{Acceleration}

Accelaration can be achieved by renormalized the relative variables using a new momentum reference. This has the side effect that the fields of the magnets (expressed in normalized strength) follow the energy ramp and that the cavity frequency (if expressed in terms of the harmonic number (NB we should perhaps change this in the Xtrack interface) is updated.

The re-normalization if done once at each turn is:

\begin{align}
p_{x,\rm new} &= p_{x,\rm old} \frac{P_{0,\rm old}}{P_{0,\rm new}} &
p_{y,\rm new} &= p_{y,\rm old} \frac{P_{0,\rm old}}{P_{0,\rm new}} \\
\delta_{\rm new}&= (\delta_{\rm old}+1) \frac{P_{0,\rm old}}{P_{0,\rm new}} -1 &
p_{\tau,\rm new} &= \frac{p_{\tau,\rm old}\, P_{0,\rm old}\,c + E_{0,\rm old} - E_{0,\rm new}}{P_{0,\rm new}c} \\
\zeta_{\rm new} &= s\beta_0 \left(\frac{1}{\beta_{0,\rm new}} -
\frac{1}{\beta_{0,\rm old}}\right) - \zeta_{\rm old} &
\tau_{\rm new} &= s\left(\frac{1}{\beta_{0,\rm new}} -  \frac{1}{\beta_{0,\rm old}}\right) - \tau_{\rm old} \\
\end{align}





\section{Beam elements}

\subsection{Drift}
A drift is a straight, field-free region ($h(x,y)=0$, $V=0$ and
$\mathbf{A}=0$).  The exact and expanded Hamiltonian for a drift space are
\begin{align}
  H_\tau =
    \frac{p_\tau}{\beta_0}  - \sqrt{(1+\delta)^2 - p_x^2 - p_y^2}
  &\approx
    \frac{p_\tau}{\beta_0} - \delta + \frac{1}{2}\frac{p_x^2+p_y^2}{1+\delta}.
\end{align}
% \begin{align}
%   H_\sigma = p_\sigma - \sqrt{(1+\delta)^2 - p_x^2 - p_y^2} &\approx
%   p_\sigma - \delta + \frac{1}{2}\frac{p_x^2+p_y^2}{1+\delta}.
% \end{align}

The map is given by solving the equations of motion:
\begin{align}
  \frac{\text{d} p_i}{\text{d}s} &= -\frac{\partial H}{\partial q_i} &
  \frac{\text{d} q_i}{\text{d}s} &=  \frac{\partial H}{\partial p_i} 
\end{align}
As there is no explicit dependency on the position coordinates in the Hamiltonian, the
momenta remain unchanged in a drift.

For the position coordinates, we get:
\begin{align}
  \left(x\right)' &=
        \frac{p_x}{p_z}
        \approx \frac{p_x}{1+\delta} \\
  \left(y\right)' &=
        \frac{p_y}{p_z}
        \approx \frac{p_y}{1+\delta} \\
  \left(\tau\right)' &=
        \frac{1}{\beta_0} - \frac{1}{\beta}\frac{1+\delta}{p_z}
        \approx \frac{1}{\beta_0} - \frac{1}{\beta} - \frac{1}{\beta}
                \frac{p_x^2 + p_y^2}{2} \\
  p_z &=
        \sqrt{(1+\delta)^2 - p_x^2 - p_y^2}
\end{align}



\subsubsection{Expanded Drift}

The map relative to the expanded Hamiltonian is then
\begin{align}
  x_p &= \frac{p_x}{1+\delta} & 
  y_p &= \frac{p_y}{1+\delta}  \\
  x & \leftarrow x + x_p l &
  y & \leftarrow y + y_p l
\end{align}
% \begin{align}
%   \tau & \leftarrow \tau +
%    \frac{l}{\beta_0} - \frac{l}{\beta} -
%     \frac{l}{\beta} \frac{x_p^2+y_p^2}{2}=
%     \tau+
%     l\left(\frac{\delta}{\beta_0}-\frac{p_t}{1+\delta} - \frac{x_p^2+y_p^2}{2\beta}\right)
% \end{align}
\begin{align}
  \zeta &
    \leftarrow \zeta+
    l\left(1- \frac{\beta_0}{\beta}\left(1 + \frac{x_p^2+y_p^2}{2}\right)\right)
\end{align}

\subsubsection{Exact Drift}

The map relative to the exact Hamiltonian is then
% \begin{align}
%   p_z&=\sqrt{(1+\delta)^2 - p_x^2 - p_y^2} \\
%   \frac{d p_z}{d p_t}&= \frac{p_t+1/\beta_0}{p_z} = \frac{1}{\beta_z}  \\
%   \frac{d p_z}{d p_\sigma}&= \frac{\beta_0^2 p_\sigma+1}{p_z} = \frac{\beta_0}{\beta_z}  
% \end{align}
\begin{align}
  x & \leftarrow x + \frac{p_x}{p_z} l  &
  y & \leftarrow y + \frac{p_x}{p_z} l
\end{align}
% \begin{align}
%   \tau & \leftarrow \tau + \frac{l}{\beta_0}
%   -\frac{l}{\beta_z}=
%   l\left(\frac{1}{\beta_0}-\frac{p_t+1/\beta_0}{p_z}\right)
% \end{align}
\begin{align}
  \zeta & \leftarrow \zeta + l\left(
          1-\frac{\beta_0}{\beta}\frac{1+\delta}{p_z}
          \right)
\end{align}




\subsubsection{Polar Drift}
It is possible to define a ``polar'' drift that has the effect of rotating the reference frame
\cite{forest99} for instance in the $x$-$z$ plane

\begin{align}
p_x & \leftarrow   p_x \cos \theta + p_z \sin\theta &
p_z & \leftarrow - p_x \sin \theta + p_z \cos\theta \\
z   &= -x \sin \theta & x' &= p_x/p_z &  y' &= p_y/p_z \\
x   & \leftarrow x \cos\theta - x' z  &
y   & \leftarrow y - x' z  & \tau & \leftarrow \tau + z\frac{1}{\beta}\frac{1+\delta}{p_z} .
\end{align}
where $\theta$ is the angle bringing the new $\hat x$ towards the old $\hat z$.
The map can be also generated by combining a rotation with a $-x
\sin(\theta)$-length drift. In case of an $\hat x$ rotation the role of $x$ and $y$ are interchanged. 


\subsection{Dipole}

In a curvilinear reference system with a constant curvature $h$ in the
horizontal plane a uniform magnetic field can be derived by the vector potential:
\begin{align}
  A_x & = 0, & A_y & = 0, & A_s & = 
  - B_y \left(x-\frac{h x^2}{2 (1+h x)}\right).
\end{align}

With the following normalization $k_0=\frac{q_0}{p} B_y$ is the inverse of the bending 
radius of the reference particle.

The exact and expanded Hamiltonian for a horizontal bending magnet is (eq. 2.12 in
\cite{barber87})
\begin{align}
  H &= \frac{p_\tau}{\beta_0} 
       - (1+h x)\sqrt{(1+\delta)^2 -p_x^2 - p_y^2}
       + \chi k_0 \left( x + \frac{h x^2}{2} \right)  \\
    &\simeq   \frac{p_\tau}{\beta_0}
    + \frac{1}{2}\frac{p_x^2+p_y^2}{1+\delta}
  - (1+h x) (1+\delta) + \chi k_0 \left( x + \frac{h x^2}{2} \right)
\end{align}


\subsubsection{Thin dipole}
The map for a thin dipole kick (horizontal or vertical) from the expanded Hamiltonian is 
(eq. 4.12 in \cite{heinemann95}):
\begin{align}
  p_x &\leftarrow p_x + (h_x l - \chi k_0 l)  + h_x l \delta - \chi k_0 l h_x x \\
  p_y &\leftarrow p_y - (h_y l - \chi \hat k_0 l) - h_y l \delta - \chi \hat k_0 l h_y y\\
  \tau &\leftarrow \tau - \frac{h_xx - h_yy}{\beta}  l.
\end{align}


%\subsubsection{mad thin dipole}
% see ttt = sqrt( one + two*track(6,jtrk)*beti + track(6,jtrk)*track(6,jtrk) ) 

%
%to be checked
%??? dipr 
%\begin{align}
%  p_x &\leftarrow p_x + (h_x l - \chi k_0 l)  + h_x l \delta - \chi k_0 l h_x x \\
%  p_y &\leftarrow p_y - (h_y l - \chi \hat k_0 l) - h_y l \delta  + \chi k_0 l h_y y\\
%  \tau &\leftarrow \tau - \frac{h_xx - h_yy}{\beta}  l.
%\end{align}

 %   ttt = sqrt( one + two*track(6,jtrk)*beti + track(6,jtrk)*track(6,jtrk) ) 
 %   track(2,jtrk) = track(2,jtrk) - (dbr + dxt(jtrk) - dipr * (ttt - one))
 %    track(4,jtrk) = track(4,jtrk) + (dbi + dyt(jtrk) - dipi * (ttt - one))
 %    track(5,jtrk) = track(5,jtrk) - &
 %         (dipr*track(1,jtrk) - dipi*track(3,jtrk)) *   &
 %         ((one + bet0*track(6,jtrk))/ttt) * bet0i



\subsubsection{Thick dipole}
Defining the following quantities,
\begin{align}
  G_x&= \chi \frac{k_0 h_x}{1+\delta}, & G_y&= \chi \frac{ \hat k_0 h_y}{1+\delta} \\
  C_{x,y}&=\cos(\sqrt{G_{x,y}}L), & S_{x,y}&=\sin(\sqrt{G_{x,y}}L)
\end{align}
the map relative to the expanded Hamiltonian is (eq. 4.11 in \cite{barber87})
\begin{align}
  x   &\leftarrow C_x \cdot x + \frac{S_x}{\sqrt{G_x}}\frac{1}{1+\delta} \cdot p_x + \frac{\delta}{h_x} (1 - C_x) \\
  p_x &\leftarrow -\sqrt{G_x} (1+\delta) \cdot S_x \cdot x + C_x \cdot p_x + \delta \sqrt{1+\delta} \cdot S_x \\
  y   &\leftarrow C_y \cdot y + \frac{S_y}{\sqrt{G_y}}\frac{1}{1+\delta} \cdot p_y + \frac{\delta}{h_y} (1 - C_y) \\
  p_y &\leftarrow -\sqrt{G_y} (1+\delta) \cdot S_y \cdot y + C_y \cdot p_y + \delta \sqrt{1+\delta} \cdot S_y \\
  \zeta &\leftarrow \zeta + L\left(1 - \frac{\beta_0}{\beta}\right) \\
  & \qquad\, -\frac{\beta_0}{\beta} \Bigg[ \frac{h_x S_x}{\sqrt{G_x}} \cdot x + \frac{1-C_x}{h_x} \cdot p_x
  + \frac{h_y S_y}{\sqrt{G_y}} \cdot y + \frac{1-C_y}{h_y} \cdot p_y
  + \delta \left(2L - \frac{S_x}{\sqrt{G_x}} - \frac{S_y}{\sqrt{G_y}} \right) \Bigg] \\
  & \qquad\, - \frac{1}{4}\frac{\beta_0}{\beta} \Bigg[ G_x \left(L-\frac{C_xS_x}{\sqrt{G_x}} \right)
  \left(x - \frac{\delta}{h_x}\right)^2
  + \left(L+\frac{C_xS_x}{\sqrt{G_x}} \right) \frac{p_x^2}{(1+\delta)^2}
  -\left(x-\frac{\delta}{h_x}\right) \frac{2S_x^2}{1+\delta} \cdot p_x \\
  & \qquad\, + G_y \left(L-\frac{C_yS_y}{\sqrt{G_y}} \right)
  \left(y - \frac{\delta}{h_y}\right)^2 + \left(L+\frac{C_yS_y}{\sqrt{G_y}}\right) 
  \frac{p_y^2}{(1+\delta)^2}
  -\left(y-\frac{\delta}{h_y}\right)\frac{2S_y^2}{1+\delta} \cdot p_y \Bigg].
\end{align}

\subsubsection{Dipole Edge effects}
Considering the dipole edge effects from a dipole of length $L$ and bending angle $\theta$, 
the map is
\begin{align*}
    p_x &\to p_x + \frac{1+\delta}{\rho} \tan(\alpha) \cdot x \\
    p_y &\to p_y - \frac{1+\delta}{\rho} \tan(\alpha) \cdot y,
\end{align*}
where the bending radius $\rho$ and $\alpha$ are defined as
\begin{align*}
    \rho^{-1}   &= \frac{h_x}{\sqrt{1+\delta}} &
    \alpha &= \frac{1}{2} \frac{L}{\rho} = \frac{\theta}{2}.
\end{align*}

\subsubsection{Fringe field}

Based on: \url{https://cds.cern.ch/record/2857004}

The map of the fringe field of a bending magnet can be written as:
\begin{align} 
y^f & =\frac{2 y}{1+\sqrt{1-2 \frac{\partial \Phi}{\partial p_y} y}} \\ 
x^f & =x+\frac{1}{2} \frac{\partial \Phi}{\partial p_x} y^{f^2} \\
p_y^f & =p_y-\Phi y^f \\ 
\zeta^f & =\zeta+\frac{\beta_0}{\beta}\frac{1}{2} \frac{\partial \Phi}{\partial \delta} y^{f^2}
\end{align}


where:
\begin{equation}
\Phi\left(p_x, p_y, \delta\right)=\frac{k_0 x^{\prime}}{1+y^{\prime 2}}
-g k_0^2 f_\text{int}
\left(\frac{(1+\delta)^2-p_y^2}{p_z^3}
+x^{\prime 2}
\frac{(1+\delta)^2-p_x^2}{p_z^3}\right)
\end{equation}

We define:
\begin{align}
\phi_0(x', y') &= \frac{x^{\prime}}{1+y^{\prime 2}}\\
\phi_x(p_x, p_y, \delta) &= \frac{(1+\delta)^2-p_x^2}{p_z^3}\\
\phi_y(p_x, p_y, \delta) &= \frac{(1+\delta)^2-p_y^2}{p_z^3}
\end{align}

So we can rewrite:
\begin{equation}
\Phi\left(p_x, p_y, \delta\right)=\frac{k_0 x^{\prime}}{1+y^{\prime 2}}
-g k_0^2 f_\text{int}
\left(\frac{(1+\delta)^2-p_y^2}{p_z^3}
+x^{\prime 2}
\frac{(1+\delta)^2-p_x^2}{p_z^3}\right)
\end{equation}

so we can rewrite:
\begin{equation}
\Phi\left(p_x, p_y, \delta\right)=
k_0 \phi_0(x', y')
-g k_0^2 f_\text{int} \left(
\phi_y(p_x, p_y, \delta) +x^{\prime 2} \phi_x(p_x, p_y, \delta)
\right)
\end{equation}

The derivatives can be written as:

\begin{align}
\frac{\partial \Phi}{\partial p_x} &= 
k_0 \left( \frac{\partial \phi_0}{\partial x'}\frac{\partial x'}{\partial p_x} + \frac{\partial \phi_0}{\partial y'}\frac{\partial y'}{\partial p_x} \right)
-g k_0^2 f_\text{int} \left(
\frac{\partial \phi_y}{\partial p_x}
+ 2x'\frac{\partial x'}{\partial p_x}\phi_x
+x'^2 
\frac{\partial \phi_x}{\partial p_x}
\right)\\
%
\frac{\partial \Phi}{\partial p_y} &= 
k_0 \left( \frac{\partial \phi_0}{\partial x'}\frac{\partial x'}{\partial p_y} + \frac{\partial \phi_0}{\partial y'}\frac{\partial y'}{\partial p_y} \right)
-g k_0^2 f_\text{int} \left(
\frac{\partial \phi_y}{\partial p_y}
+ 2x'\frac{\partial x'}{\partial p_y}\phi_x
+x'^2 
\frac{\partial \phi_x}{\partial p_y}
\right)\\
%
\frac{\partial \Phi}{\partial \delta} &= 
k_0 \left( \frac{\partial \phi_0}{\partial x'}\frac{\partial x'}{\partial \delta} + \frac{\partial \phi_0}{\partial y'}\frac{\partial y'}{\partial \delta} \right)
-g k_0^2 f_\text{int} \left(
\frac{\partial \phi_y}{\partial \delta} 
+ 2x'\frac{\partial x'}{\partial \delta}\phi_x
+x'^2 
\frac{\partial \phi_x}{\partial \delta} 
\right)
\end{align}

Expression of all other quantities needed for the calculation:


\begin{align}
x' &= \frac{p_x}{p_z}\\
\frac{\partial x'}{\partial p_x} &= - \frac{p_x}{p_z^2}\frac{\partial p_z}{\partial p_x} + \frac{1}{p_z} \\
\frac{\partial x'}{\partial p_y} &= - \frac{p_x}{p_z^2}\frac{\partial p_z}{\partial p_x}\\
\frac{\partial x'}{\partial \delta} &= - \frac{p_x}{p_z^2}\frac{\partial p_z}{\partial \delta}
\end{align}

\begin{align}
y' &= \frac{p_y}{p_z}\\
\frac{\partial y'}{\partial p_y} &= - \frac{p_y}{p_z^2}\frac{\partial p_z}{\partial p_y}  \\
\frac{\partial y'}{\partial p_y} &= - \frac{p_y}{p_z^2}\frac{\partial p_z}{\partial p_y} + \frac{1}{p_z}\\
\frac{\partial y'}{\partial \delta} &= - \frac{p_y}{p_z^2}\frac{\partial p_z}{\partial \delta}
\end{align}


\begin{align}
p_z &= \sqrt{(1+\delta)^2 - p_x^2 - p_y^2}\\
\frac{\partial p_z}{\partial p_x} &= -\frac{p_x}{p_z} = -x'\\
\frac{\partial p_z}{\partial p_y} &= -\frac{p_y}{p_z} = -y'\\
\frac{\partial p_z}{\partial \delta} &= \frac{1+\delta}{p_z}
\end{align}


\begin{align}
\phi_x &= \frac{(1+\delta)^2-p_x^2}{p_z^3}\\
\frac{\partial \phi_x}{\partial p_x} &=  -\frac{3}{p_z^4}\frac{\partial p_z}{\partial p_x} \left((1+\delta)^2-p_x^2 \right)
- 2 \frac{p_x}{p_z^3}\\
\frac{\partial \phi_x}{\partial p_y} &= -\frac{3}{p_z^4}\frac{\partial p_z}{\partial p_y} \left((1+\delta)^2-p_x^2 \right)\\
\frac{\partial \phi_x}{\partial \delta} &= -\frac{3}{p_z^4}\frac{\partial p_z}{\partial \delta} \left((1+\delta)^2-p_x^2 \right)
+ 2 \frac{(1+\delta)}{p_z^3}
\end{align}

\begin{align}
\phi_y &= \frac{(1+\delta)^2-p_y^2}{p_z^3}\\
\frac{\partial \phi_y}{\partial p_x} &=  -\frac{3}{p_z^4}\frac{\partial p_z}{\partial p_x} \left((1+\delta)^2-p_y^2 \right)\\
\frac{\partial \phi_y}{\partial p_y} &= -\frac{3}{p_z^4}\frac{\partial p_z}{\partial p_y} \left((1+\delta)^2-p_y^2 \right)
- 2 \frac{p_y}{p_z^3}\\
\frac{\partial \phi_x}{\partial \delta} &= -\frac{3}{p_z^4}\frac{\partial p_z}{\partial \delta} \left((1+\delta)^2-p_x^2 \right)
+ 2 \frac{(1+\delta)}{p_z^3}
\end{align}

\begin{align}
\phi_0 &= \frac{x^{\prime}}{1+y^{\prime 2}}\\
\frac{\partial \phi_0}{\partial x'} &= \frac{1}{1+y^{\prime 2}}\\
\frac{\partial \phi_0}{\partial y'} &= - \frac{2x'y'}{(1+y^{\prime 2})^2}
\end{align}

\subsubsection{MAD8 fringe}

Again nased on: \url{https://cds.cern.ch/record/2857004} (eq. 37)

The map of the fringe field of a bending magnet can be written as:
\begin{align} 
y^f & =\frac{2 y}{1+\sqrt{1-2 \frac{\partial \Psi}{\partial p_y} y}} \\ 
x^f & =x+\frac{1}{2} \frac{\partial \Psi}{\partial p_x} y^{f^2} \\
p_y^f & =p_y-\Psi y^f \\ 
\zeta^f & =\zeta+\frac{\beta_0}{\beta}\frac{1}{2} \frac{\partial \Psi}{\partial \delta} y^{f^2}
\end{align}
where:
\begin{equation}
\Psi\left(p_x, p_y, \delta\right)=k_0\tan ^{-1}\left(\frac{x^{\prime}}{1+y^{\prime 2}}\right)-g k_0^2 f_\text{int} p_z \left(1+x^{\prime 2}\left(2+y^{\prime 2}\right)\right) 
\end{equation}

We define:
\begin{align}
\phi_0(x', y') &= \frac{x^{\prime}}{1+y^{\prime 2}}\\
\phi_1(x', y') &= 1+2x^{\prime 2}+x^{\prime 2}y^{\prime 2} 
\end{align}

so we can write
\begin{equation}
\Psi=k_0\tan^{-1}\left(\phi_0 \right)
-g k_0^2 f_\text{int} 
p_z \phi_1
\end{equation}

from which:
\begin{align}
\frac{\partial\Psi}{\partial p_x} &= 
k_0 \frac{1}{1 + \phi_0^2}\frac{\partial\phi_0}{\partial p_x}
-gk_0^2 f_\text{int}
\left(\phi_1 \frac{\partial p_z}{\partial p_x}
 + p_z \frac{\phi_1}{\partial p_x}
\right)\\
\frac{\partial\Psi}{\partial p_y} &= 
k_0 \frac{1}{1 + \phi_0^2}\frac{\partial\phi_0}{\partial p_y}
-gk_0^2 f_\text{int}
\left(\phi_1 \frac{\partial p_z}{\partial p_y}
 + p_z \frac{\phi_1}{\partial p_y}
\right)\\
\frac{\partial\Psi}{\partial \delta} &= 
k_0 \frac{1}{1 + \phi_0^2}\frac{\partial\phi_0}{\partial \delta }
-gk_0^2 f_\text{int}
\left(\phi_1 \frac{\partial p_z}{\partial p_y}
 + p_z \frac{\phi_1}{\partial \delta}
\right)\\
\end{align}

\begin{align}
\frac{\partial \phi_{0,1}}{\partial p_x} &= 
\frac{\partial \phi_{0,1}}{\partial x'} \frac{\partial x'}{\partial p_x}
+\frac{\partial \phi_{0,1}}{\partial y'} \frac{\partial y'}{\partial p_x}
\\
\frac{\partial \phi_{0,1}}{\partial p_y} &= 
\frac{\partial \phi_{0,1}}{\partial x'} \frac{\partial x'}{\partial p_y}
+\frac{\partial\phi_{0,1}}{\partial y'} \frac{\partial y'}{\partial p_y}
\\
\frac{\partial \phi_{0,1}}{\partial \delta} &= 
\frac{\partial \phi_{0,1}}{\partial x'} \frac{\partial x'}{\partial \delta}
+\frac{\partial\phi_{0,1}}{\partial y'} \frac{\partial y'}{\partial \delta}
\end{align}


\begin{align}
\phi_0 &= \frac{x^{\prime}}{1+y^{\prime 2}}\\
\frac{\partial \phi_0}{\partial x'} &= \frac{1}{1+y^{\prime 2}}\\
\frac{\partial \phi_0}{\partial y'} &= - \frac{2x'y'}{(1+y^{\prime 2})^2}
\end{align}

\begin{align}
\phi_1 &= 1+2x^{\prime 2}+x^{\prime 2}y^{\prime 2} \\
\frac{\partial \phi_1}{\partial x'} &= 4x' + 2x'y'^2\\
\frac{\partial \phi_1}{\partial y'} &= 2x'^2y'
\end{align}


\subsection{Combined dipole quadrupole}

The following vector potential in curvilinear coordinates
\begin{align}
A_s= -\frac{g}{1+h x}
    \left(\frac{x^2}{2} - \frac{y^2}{2} +
          \frac{h x^3}{3} \right)
\end{align}
produce a field 
\begin{align}
B_x&= g \left(y + \frac{h x y}{1+h x} \right) &
B_y&= g x 
\end{align}

The following vector potential in curvilinear coordinates
\begin{align}
A_s= -\frac{g}{1+h x}
    \left(\frac{x^2}{2} - \frac{y^2}{2} +
          \frac{h x^3}{3} -   \frac{h x y^2}{2} \right)
\end{align}
produce a field 
\begin{align}
B_x&= g y &
B_y&= g \left(x + \frac{h y^2}{2+2 h x} \right) 
\end{align}


\subsection{Thin Multipole}

The effect of a thin multipole can be approximated by the following Hamiltonian

A longitudinally uniform static magnetic field can be described by the following equations
\begin{align}
    B_y+iB_x&=\sum_{n=1}     \frac{B_n+iA_n}{r_0^{n-1}} (x+iy)^{n-1} \\
            &=B_N \sum_{n=N} \frac{b_n+ia_n}{r_0^{n-1}} (x+iy)^{n-1}  .
\end{align}

%Usually multipole are expressed as relative to 
The kick $\Delta \vec P  = q_0 v_z \hat z \times (B_x \hat x + B_y \hat y)$ translates into
\begin{align}
   \Delta p_x - i \Delta p_y = -\frac{q_0}{P_0}\chi(B_y + i B_x) 
\end{align}


A thin multiple idealizes the effect of the field by taking the limit of the integration 
length going to zero while keeping constant the integrated strength. The Hamiltonian is:
\begin{align}
  H= \delta(s) \chi L \Re\left[\sum_{n=0} (k_n + i\hat k_n) (x+iy)^{n+1} \right].
\end{align}
where
\begin{align}
  k_n     &=  n!\frac{q_0}{p_0}  \frac{B_{n+1}}{r_0^n}  &
  \hat k_n&=  n!\frac{q_0}{p_0}  \frac{A_{n+1}}{r_0^n} .
\end{align}


The corresponding map is:
\begin{align}
  p_x &\leftarrow p_x - \chi L\cdot\Re\left[\sum_{n=0} \frac{1}{n!} (k_n + i\hat k_n) (x+iy)^n \right], \\
  p_y &\leftarrow p_y + \chi L\cdot\Im\left[\sum_{n=0} \frac{1}{n!} (k_n + i\hat k_n) (x+iy)^n \right],
\end{align}

In case a curvature $h$, the vector potential become:

\begin{align}
f(x,y)&=\int B_x(x,y) dy  \\
g(x,y)&=\int \partial x  B_x(x,y) dy \\
a_s(x,y)&=\frac{c_1}{1 + h x} + f(x,y) -
   \frac{\int_1^x (1 + h \tilde{x}) (g(\tilde{x},y)+\tilde{x}) +h f(x,y)  \, d\tilde{x}}{1+ h x}
\end{align}

\begin{align}
\frac{\int_1^x \left(-h \tilde{x} \left(g(x,y)\right)-\int \text{bx}^{(1,0)}(\tilde{x},y) \, dy-h \int \text{bx}(\tilde{x},y) \,
   dy-h \tilde{x} \text{by}(\tilde{x},y)-\text{by}(\tilde{x},y)\right) \, d\tilde{x}}{h x+1}
\end{align}



\subsection{Accelerating Cavity}

The approximated energy gain of a particle passing through an electric field of frequency $f=\frac{k}{2\pi c}$ for which:
\begin{align}
V \sin(\phi - k \tau) = \int_{-l/2}^{l/2} E_s(0,0,t,s)  {\rm \,d}s.
\end{align}

An equivalent vector potential can be derived and normalized as
\begin{align}
A_s& = - \frac{V}{\omega} \cos(\phi - k \tau ) & 
V_n&=  \frac{q_0}{P_0 c} V  & 
\end{align}
from which one can derive the following map
\begin{align}
p_\tau & \leftarrow p_\tau + \chi V_n \sin(\phi - k \tau + k \frac{s-s_0}{\beta_0}  ),
\end{align}
where the additional terms in the phase is added in case harmonic number is not exactly
integer and the phase is unlocked phase ). The new $\delta$ can be updated from the new $p_\tau$.



\subsection{RF-Multipole}

The RF-multipole generalizes the interaction of a particle with an electromagnetic field by assuming that

\begin{align}
\Delta E(x,y,\tau) &= q \int_{-L/2}^{L/2} E_z(x,y,t)  {\rm \,d}s \\
\Delta P_x(x,y,\tau) &= q \int_{-L/2}^{L/2} E_x(x,y,t) + \beta c B_y(x,y,t) {\rm \,d}s\\
\Delta P_y(x,y,\tau) &= q \int_{-L/2}^{L/2} E_y(x,y,t) - \beta c B_x(x,y,t) {\rm \,d}s.
\end{align}
are harmonic in $x,y$ and periodic in $\tau$ of frequency $f=\frac{k}{2\pi c}$ such that:

\begin{align}
a_s(x,y,\tau) 
&= \Re \left[ \sum_{n=1}^N
      \left(       k_n \cos(\phi_n -k \tau ) +
            i \hat k_n \cos(\hat \phi_n -k \tau)
      \right)    
      (x+i y )^n
     \right],
\end{align}

The map then follows:
\begin{align}
    \Delta p_x &= -\sum_{n=1}^N \frac{\chi}{n!} \Re\left[ (k_n C_n + i \hat k_n \hat C_n)(x+iy)^{(n-1)}\right], \\
    \Delta p_y &=  \sum_{n=1}^N \frac{\chi}{n!} \Im\left[ (k_n C_n + i \hat k_n \hat C_n)(x+iy)^{(n-1)}\right], \\
    \Delta p_\tau &= -\chi k \sum_{n=1}^N \Re\left[( k_n S_n + i k_n \hat S_n ) (x+iy)^n\right],
\end{align}
where
\begin{align}
     C_n&=\cos(\phi_n-\omega \Delta t) &
\hat C_n&=\cos(\hat \phi_n-\omega \Delta t) \\
     S_n&=\sin(\phi_n-\omega \Delta t) &
\hat S_n&=\sin(\hat \phi_n-\omega \Delta t) .
\end{align}


\subsection{Solenoid}

The derivation largely follows one by Forest~\cite{forest99}, while the final map can be verified to be the same as the one by Wolski~\cite{wolski2014beam}.

We can write the Hamiltonian for the solenoid as follows:
\begin{equation}
  H = p_\zeta -\sqrt{\left(1 + \delta \right)^2 - \left(p_x + \frac{b_z}{2}y\right)^2 - \left(p_y - \frac{b_z}{2}x\right)^2}
\end{equation}
where we have defined the normalized quantities $b_z = B_z\frac{q_0}{P_0}$, $a_x = A_x\frac{q_0}{P_0}$, $a_y = A_y\frac{q_0}{P_0}$.
This can be obtained knowing the general Hamiltonian
\begin{equation}
  H = p_\zeta -\sqrt{(1 + \delta)^2 - (p_x - a_x)^2 - (p_y - a_y)^2} - a_z,
\end{equation}
we can extract the magnetic field potential and convince ourselves that $H$ describes a magnetic field with only the longitudinal component equal to $B_z$, as expected of a solenoid:
\begin{equation}
  \mathbf{A} =
  \begin{bmatrix}
    A_x \\ A_y \\ A_z
  \end{bmatrix}
  = \begin{bmatrix}
    -\frac{B_z}{2}y\\
    \frac{B_z}{2}x\\
    0
  \end{bmatrix}
  %
  \implies
  %
  \mathbf{B} = \nabla \times \mathbf{A}
  = \begin{bmatrix}
    \frac{\partial A_z}{\partial y} - \frac{\partial A_y}{\partial z}\\
    \frac{\partial A_z}{\partial x} - \frac{\partial A_x}{\partial z}\\
    \frac{\partial A_y}{\partial x} - \frac{\partial A_x}{\partial y}\\
  \end{bmatrix}
  = \begin{bmatrix}
    0\\
    0\\
    B_z
  \end{bmatrix}.
\end{equation}

The Hamiltonian $H$ can be simplified, by applying the following transformation, which should be understood as the change of reference from the general coordinate system $\mathbf{X}$ to a new $\mathbf{X}_\text{new}$:
\[
  T := \begin{bmatrix}
  -\frac{1}{2} & 0 & 0 & \frac{1}{b_{z}} \\
  0 & 1 & \frac{1}{2} \, b_{z} & 0 \\
  -\frac{1}{2} & 0 & 0 & -\frac{1}{b_{z}} \\
  0 & 1 & -\frac{1}{2} \, b_{z} & 0
  \end{bmatrix}.
\]
In particular, note that if
\[
  \mathbf{X} =
  \begin{bmatrix}
    x \\ p_x \\ y \\ p_y
  \end{bmatrix}
  = T^{-1} \mathbf{X}_\text{new}
  = \begin{bmatrix}
    -x_\text{new} - y_\text{new} \\
    \frac{1}{2}(p_{x,\text{new}} + p_{y,\text{new}}) \\
    \frac{1}{b_z}(p_{x,\text{new}} - p_{y,\text{new}}) \\
    \frac{b_z}{2} (x_\text{new} - y_\text{new})
  \end{bmatrix},
\]
then we can rewrite $H$ in terms of $\mathbf{X}_\text{new}$ (dropping the `new' suffix, while keeping it in mind) as
\[
  K := -\sqrt{(1 + \delta)^2 - p_x^2 - b_z^2 x^2}.
\]
We can simplify $H$ even further, rewriting it in terms of the following action-angle variables:
\begin{equation}\label{eq:solenoid-x-px}
  x := \sqrt{\frac{2 J}{|b_z|}} \cos(\phi)
  \;\;\text{ and }\;\;
  p_x := \sqrt{2 |b_z| J} \sin(\phi).
\end{equation}
The new Hamiltonian with respect to $J$ is the following:
\begin{multline*}
  K = -\sqrt{(1 + \delta)^2 - p_x^2 - b_z^2 x^2} = \\
  %
  -\sqrt{(1 + \delta)^2 - \left(\sqrt{2 |b_z| J} \sin(\phi)\right)^2 - b_z^2 \sqrt{\frac{2 J}{|b_z|}} \cos(\phi)^2} = \\
  %
  -\sqrt{(1 + \delta)^2 - \left(\sqrt{2 |b_z| J} \sin(\phi)\right)^2 - \left(\sqrt{2 |b_z| J} \cos(\phi)\right)^2} = \\
  %
  -\sqrt{(1 + \delta)^2 - 2 |b_z| J}.
\end{multline*}
Then, using Hamilton's equations, we can solve for $\phi$:
\[
  \frac{d \phi}{d z} = \frac{\partial K}{\partial J} \implies \phi(z) = \phi(0) + z \frac{\partial K}{\partial J} = \phi(0) - z\frac{|b_z|}{K}.
\]
Let $\omega := -b_z / K$. Keeping in mind that we are still in the realm of $\mathbf{X}_\text{new}$, we can compute $x_\text{new}$ and $y_\text{new}$ substituting the above into (\ref{eq:solenoid-x-px}).
Note that we can drop the modulus on $b_z$ in both $\omega$ and the equations below, as $\cos$ is an even function, and while $\sin$ is an odd function and the signs of $\sin(\omega z)$ and $b_z$ will cancel out anyway.
\begin{multline*}
  x = \sqrt{\frac{2J}{|b_z|}} \cos\left(\phi(0) + \left(- z\frac{|b_z|}{K}\right)\right) = \\
    \sqrt{\frac{2J}{|b_z|}} \cos\phi(0) \cos(\omega z) - \frac{\sqrt{2J|b_z|}}{|b_z|} \sin\phi(0) \sin\left(- z\frac{|b_z|}{K}\right) = \\
    x_0 \cos(\omega z) - \frac{p_{x,0}}{b_z} \sin(\omega z)
\end{multline*}
\begin{multline*}
  p_x = \sqrt{2 |b_z| J} \sin\left(\phi(0) + \left(- z\frac{|b_z|}{K}\right)\right) = \\
    \sqrt{2 |b_z| J} \sin\phi(0) \cos(\omega z) + |b_z| \sqrt{\frac{2J}{|b_z|}} \cos\phi(0) \sin\left(- z\frac{|b_z|}{K}\right) = \\
    p_{x,0} \cos(\omega z) + b_z x_0 \sin(\omega z)
\end{multline*}
These equations give us the map for the solenoid in $\mathbf{X}_\text{new}$.
We can write this transformation in the form of a matrix
\[
R := \begin{bmatrix}
  \cos(\omega z) & -\frac{\sin(\omega z)}{b_z} & 0 & 0 \\
  b_z \sin(\omega z) & \cos(\omega z) & 0 & 0 \\
  0 & 0 & 1 & 0 \\
  0 & 0 & 0 & 1
\end{bmatrix},
\]
and therefore the whole solenoid map in $\mathbf{X}$ as follows (let $S := \sin(\omega z)$ and $C := \cos(\omega z)$):
\[
  M := T^{-1} R T = \begin{bmatrix}
    % 1st row
    \frac{C + 1}{2} &
    \frac{S}{b_z} &
    \frac{S}{2} &
    \frac{1 - C}{b_z} \\[6pt]
    % 2nd row
    -\frac{b_z S}{4} &
    \frac{C + 1}{2} &
    \frac{b_z (C - 1)}{4} &
    \frac{S}{2} \\[6pt]
    % 3rd row
    -\frac{S}{2} &
    \frac{C - 1}{b_z} &
    \frac{C + 1}{2} &
    \frac{S}{b_z} \\[6pt]
    % 4th row
    \frac{b_z (1 - C)}{4} &
    -\frac{S}{2} &
    -\frac{b_z S}{4} &
    \frac{C + 1}{2}
  \end{bmatrix}
\]

In the tracking procedure of Xtrack (and MAD-X) the map is implemented with respect to a different quantity \verb|sk|, which we will denote with $k$, and which represents half of magnetic field strength $b_z$: $k = \frac{b_z}{2}$.
Let $s := \sin(\frac{z k}{H}) = \sin(\frac{\omega z}{2})$ and $c := \cos(\frac{\omega z}{2})$; then we can rewrite $M$ using the trigonometric identities:
\begin{align*}
  \cos(2 \theta) = 2 \cos^2\theta - 1 = 1 - 2 \sin^2\theta &\implies c^2 = \frac{C + 1}{2} \text{ and } s^2 = \frac{1 - C}{2}, \\
  \sin(2 \theta) = 2 \cos\theta \sin\theta &\implies sc = \frac{S}{2},
\end{align*}
as the following transfer matrix
\[
  M = \begin{bmatrix}
    c^2 & \frac{cs}{k} & cs & \frac{s^2}{k} \\
    -kcs & c^2 & -ks^2 & cs \\
    -cs & -\frac{s^2}{k} & c^2 & \frac{cs}{k} \\
    ks^2 & -cs & -kcs & c^2
  \end{bmatrix},
\]
which, with relatively little effort, can be verified to correspond to the implementation of the tracking procedure.
We have the following map (note the change in $\zeta$ is analogous to the drift):

\begin{align*}
  x &\leftarrow {\left(x \cos\left(\theta\right) + y \sin\left(\theta\right)\right)} \cos\left(\theta\right) + \frac{2}{b_{z}} {\left(p_{x} \cos\left(\theta\right) + p_{y} \sin\left(\theta\right)\right)} \sin\left(\theta\right) \\
  p_x &\leftarrow -\frac{1}{2} \, {\left(x \cos\left(\theta\right) + y \sin\left(\theta\right)\right)} b_{z} \sin\left(\theta\right) + {\left(p_{x} \cos\left(\theta\right) + p_{y} \sin\left(\theta\right)\right)} \cos\left(\theta\right) \\
  y &\leftarrow {\left(y \cos\left(\theta\right) - x \sin\left(\theta\right)\right)} \cos\left(\theta\right) + \frac{2}{b_{z}} {\left(p_{y} \cos\left(\theta\right) - p_{x} \sin\left(\theta\right)\right)} \sin\left(\theta\right) \\
  p_y &\leftarrow -\frac{1}{2} \, {\left(y \cos\left(\theta\right) - x \sin\left(\theta\right)\right)} b_{z} \sin\left(\theta\right) + {\left(p_{y} \cos\left(\theta\right) - p_{x} \sin\left(\theta\right)\right)} \cos\left(\theta\right) \\
  \zeta &\leftarrow \zeta + L \left(1 - \frac{\beta_0}{\beta} \frac{1 + \delta}{p_z}\right),
\end{align*}
where $p_z :=  \sqrt{{\left(\delta + 1\right)}^2 - {\left(\frac{b_z}{2} x - p_{y}\right)}^{2} - {\left(\frac{b_{z}}{2} y + p_{x}\right)}^{2}}$, $\theta := \frac{b_{z} L}{2 p_z} $, and $L$ is the length of the thick solenoid.


%\section{Beam-Beam}
%
%The closed expression of an integrated kick experienced by a test particle of charge  $q=Z e$ due to a 2D uncoupled Gaussian charge distribution of total charge $q_b=N_b e$ defined by $\sigma_x, \sigma_y$ and located at $\Delta x, \Delta y$ from the test particle and moving at $v_z=\beta_b c$ from the test particle can be obtained through the use of complex the function \cite{bassetti-erskine}:
%
%\begin{align}
%w(z)=\exp({-z^2})\left(1+\frac{2i}{\sqrt{\pi}}\int_0^z \exp({\xi^2}) d\xi \right)=
%     \exp({-z^2}){\rm erfc}(-iz).
%\end{align}
%
%The kick can then be expressed as
%\begin{equation}
%\Delta p_y+i \Delta p_x= K_b \frac{\sqrt{\pi}}{r}
%\left( {\rm w}\left(\xi_1 \right)
%       -\exp\left( \xi_2^2-\xi_1^2 \right)
%         {\rm w}\left( \xi_2 \right)
%\right)
%\end{equation}
%or, when $\sigma_x=\sigma_y=\sigma$, with
%\begin{equation}
%\Delta p_y+i \Delta p_x= K_b \frac{i \Delta x+ \Delta y}{\Delta_x^2+\Delta y^2}
%\left( 1 - \exp\left(-\frac{\Delta x^2+\Delta y^2}{2\sigma^2}\right) \right)
%\end{equation}
%since $w(z) \leftarrow i / (\sqrt{\pi} z) $ for $z\leftarrow \infty$ with and where
%\begin{align}
%r&=\sqrt{2(\sigma_x^2 - \sigma_y^2)} &
%\xi_1&=\frac {\Delta x}r +i \frac {\Delta y}r &
%\xi_2= \frac{\Delta x\sigma_y}{r\sigma_x}+i \frac{\Delta y \sigma_x}{r\sigma_y}.
%\end{align}
%and where
%\begin{align}
%K_b 
%=\chi \frac{q_0 q_b  (1 -\beta \beta_b)}{2\pi \epsilon_0  P_0 c^2}
%=\frac{2 N_b Z r_0 (1 -\beta \beta_b)}{ \beta_0 \gamma_0} && 
%r_0=\frac{e^2}{4\pi\epsilon_0 m c^2},
%\end{align}
%
%In case the 2D distribution approximates the integrated effect of a longitudinal distribution (e.g. beam beam effect) the kick has to be scaled by a time of flight factor
%\begin{align}
%K_b^{BB}&=\frac{K_b}{\beta - \beta_b}, 
%\end{align}
%while in case the distribution is stationary of length L and charge density $\lambda$ such that $N_b=\lambda L$.
%\begin{align}
%K_b^{SC}&=\frac{K_b}{\beta}. 
%\end{align}
%
%In case the distribution is not longitudinally uniform and the potential of a slice of charge can be written as
%$q_b U(X,Y,Z,t)/(P_0 c)$ where $S=\frac{Z-\beta_0\tau}{2}$ is the location of the interaction point, 
%one can apply the so-called Synchro-Betatron mapping in the ultra-relativistic limit:
%\begin{align}
%x &\leftarrow x- S f_X & 
%y &\leftarrow y- S f_Y   \\
%p_x &\leftarrow p_x+  f_X=x'   &
%p_y &\leftarrow p_y+  f_Y=y' 
%\end{align} 
%\begin{align}
%p_t & \leftarrow p_t  + \frac{1}{2}
%   \left( f_Z 
%        + f_X \frac{p_x +x'}{2} 
%        + f_Y \frac{p_y +y'}{2} \right)
%\end{align} with $f_{X,Y,Z}=- n \partial_{X,Y,Z} U(X=x+S p_x,Y=y+S p_y,Z=Z,t=\tau)$.
%
%In the non-relativistic limit:
%\begin{align}
%f_{X,Y} &= - q_b \frac{1 -\beta \beta_b}{\beta - \beta_b} \partial_{X,Y} U&
%f_Z &= - q_b \frac{1}{\beta - \beta_b} \partial_Z U \\
%S&=\beta_0 \frac{Z/\beta-\tau}{\beta - \beta_b}&
%p_t & \leftarrow p_t  + \frac{\beta_0}{\beta-\beta_b}\left(\ldots\right)
%\end{align}.

\subsection{AC-dipole}
The excitation amplitude of the AC-dipole is denoted by $A$ [Tm], the excitation frequency by $q_d$ [$2\pi$] and the phase of the excitation by $\phi$. The map
presented here is for a purely horizontal dipole, the map for a vertical dipole is obtained by replacing $p_x\to p_y$.

The effect of the AC-dipole is split into four stages. The turn number is denoted by $n$.
\begin{enumerate}
  \item A number of free turns $n_{\text{free}}$, in which the AC-dipole has no effect on the motion.
  \item Ramp-up of the voltage from $0$ to the excitation amplitude $A$ for $n_{\text{ramp-up}}$ turns.
        \begin{align*}
            n' &= \frac{n-n_{\text{free}}}{n_{\text{ramp-up}}} \\
            p_x &\to p_x + n' \cdot \frac{A}{pc} \cdot(1+\delta) \sin\left(2\pi q_d\cdot(n-n_{\text{free}})+\phi\right)
        \end{align*}
  \item Constant excitation amplitude for $n_{\text{flat}}$ turns.
        \begin{align*}
            p_x &\to p_x + \frac{A}{pc}\cdot(1+\delta)\sin\left(2\pi q_d\cdot(n-n_{\text{free}})+\phi\right)
        \end{align*}
  \item Ramp-down of the voltage from the excitation amplitude $A$ to $0$ for $n_{\text{ramp-down}}$ turns.
        \begin{align*}
            n' &= \frac{n-n_{\text{free}}-n_{\text{ramp-up}}-n_{\text{flat}}-n_{\text{ramp-down}}}{n_{\text{ramp-down}}} \\
            p_x &\to p_x + n' \cdot \frac{A}{p} \cdot(1+\delta) \sin\left(2\pi q_d\cdot(n-n_{\text{free}})+\phi\right)
        \end{align*}
\end{enumerate}

\subsection{Wire}
For each part we define $p_z=\sqrt{(1+\delta)^2-x'^2-y'^2}$, using the current values for $x'$ and $y'$.

Step 1. Initial backwards drift of length $L=\frac{embl}{2}$.
\begin{align*}
	x &\to x - L\cdot\frac{x'}{p_z} \\
    y &\to y - L\cdot\frac{y'}{p_z}
\end{align*}

Step 2.
\begin{align*}
	y &\to y - \frac{x\cdot\sin(t_x)}{
    \cos\left(\arctan\left(\frac{x'}{p_z}\right)-t_x\right)}\cdot
    \frac{y'}{\sqrt{(1+\delta)^2-y'^2}} \\
    x &\to x\cdot\left[\cos(t_x)-\sin(t_x)\cdot\tan\left(
    \arctan\left(\frac{x'}{p_z}\right)-t_x\right)\right] \\
    x' &\to \sqrt{(1+\delta)^2-y'^2}\cdot\sin\left(
    \arctan\left(\frac{x'}{p_z}\right)-t_x\right) \\
    x &\to x- \frac{y\cdot\sin(t_y)}{\cos\left(\arctan\left(\frac{y'}{p_z}\right)
    -t_y\right)} \cdot\frac{x'}{\sqrt{(1+\delta)^2-x'^2}} \\
    y &\to y \left[ \cos(t_y) - \sin(t_y)\cdot\tan\left(
    \arctan\left(\frac{y'}{p_z}\right)-t_y\right) \right] \\
    y' &\to \sqrt{(1+\delta)^2-x'^2}\sin\left(\arctan\left(
    \frac{y'}{p_z}\right)-t_y\right)
\end{align*}

Step 3. Drift part of length $L=lin$.
\begin{align*}
    x &\to x + L \cdot \frac{x'}{p_z} \\
    y &\to y + L \cdot \frac{y'}{p_z}
\end{align*}

Step 4. Here $x_i=x-r_x$ and $y=y-r_y$.
\begin{align*}
    x' &\to x' - \frac{\frac{cur\cdot10^{-7}}{chi}\cdot x_i}{x_i^2+y_i^2}
    \left[\sqrt{(lin+l)^2+x_i^2+y_i^2}-\sqrt{(lin-l)^2+x_i^2+y_i^2} \right] \\
    y' &\to y' - \frac{\frac{cur\cdot10^{-7}}{chi}\cdot y_i}{x_i^2+y_i^2}
    \left[\sqrt{(lin+l)^2+x_i^2+y_i^2}-\sqrt{(lin-l)^2+x_i^2+y_i^2} \right]
\end{align*}

Step 5. Drift of length $L=leff-lin$.
\begin{align*}
    x &\to x + L \frac{x'}{p_z} \\
    y &\to y + L \frac{y'}{p_z}
\end{align*}

Step 6.
\begin{align*}
	x &\to x - \frac{y\cdot\sin(-t_y)}{
    \cos\left(\arctan\left(\frac{y'}{p_z}\right)+t_y\right)}\cdot
    \frac{x'}{\sqrt{(1+\delta)^2-x'^2}} \\
    y &\to y\cdot\left[\cos(-t_y)-\sin(-t_y)\cdot\tan\left(
    \arctan\left(\frac{y'}{p_z}\right)+t_y\right)\right] \\
    y' &\to \sqrt{(1+\delta)^2-x'^2}\cdot\sin\left(
    \arctan\left(\frac{y'}{p_z}\right)+t_y\right) \\
    y &\to y- \frac{x\cdot\sin(-t_x)}{\cos\left(\arctan\left(\frac{x'}{p_z}\right)
    +t_x\right)} \cdot\frac{y'}{\sqrt{(1+\delta)^2-y'^2}} \\
    x &\to x \left[ \cos(-t_x) - \sin(-t_x)\cdot\tan\left(
    \arctan\left(\frac{x'}{p_z}\right)+t_x\right) \right] \\
    x' &\to \sqrt{(1+\delta)^2-y'^2}\cdot\sin\left(\arctan\left(
    \frac{x'}{p_z}\right)+t_x\right)
\end{align*}

Step 7. Shift.
\begin{align*}
    x &\to x + embl\cdot \tan(t_x) \\
    y &\to y + embl\cdot\frac{\tan(t_y)}{\cos(t_x)}
\end{align*}

Step 8. Negative drift of length $L=\frac{embl}{2}$.
\begin{align*}
    x &\to x - L\cdot \frac{x'}{p_z} \\
    y &\to y - L\cdot \frac{y'}{p_z}
\end{align*}


\subsection{Misalignment}

Misalignments of elements affects the coordinates at the entrance of an
element as follows
\begin{align*}
    x &\to (x-x_s)\cdot t_c + (y-y_s)\cdot t_s \\
    y &\to -(x-x_s)\cdot t_s + (y-y_s)\cdot t_c,
\end{align*}
where $x_s$ and $y_s$ are the displacements in the horizontal and vertical
directions, respectively. $t_c$ and $t_s$ are the cosine and sine of the tilt
angle for the element.

\subsection{Electron Lens}
\label{elense}
\subsubsection{Hollow electron lens - uniform annular profile}
For a uniform distribution of the electron beam between $R_1$ and $R_2$, the radial kick can be described by a shape function $f(r)$ and a maximum kick strength $\theta_{\rm max}$:
\begin{equation}
\theta(r)=\frac{f(r)}{(r/R_2)}\cdot \theta_{\rm max}
\end{equation}
with $r=\sqrt{x^2+y^2}$ and $\theta_{\rm max}$ independent of $r$. The shape function $f(r)$ is defined as
\begin{equation}
f(r) = \frac{I(r)}{I_T}=\frac{2\pi}{I_T}\int_{0}^r r\rho(r) dr
\end{equation}
where $I_T$ is the total electron beam current, $I(r)$ is the current enclosed in a radius $r$ and $\rho(r)$ is the electron beam density distribution.

For a uniform profile one then obtains:
\begin{equation}
\begin{cases} 0 &,\quad r< R_1\\
\frac{r^2-R_1^2}{R_2^2-R_1^2} &,\quad R_1 \leq r < R_2\\
1 &,\quad R_2 \leq r
\end{cases}
\end{equation}
and
\begin{equation}
\theta_{\rm max} = \theta(R_2) = \frac{2LI_T(1\pm\beta_e\beta_p)}{4\pi\epsilon_0  \left(B\rho\right)_p\beta_e\beta_p c^2}\cdot\frac{1}{R_2}
\end{equation}
where $L$ is the length of the e-lens, $I_T$ the total electron beam current, $\beta_{e/p}$ the relativistic $\beta$ of electron/proton beam, $B\rho$ the magnetic rigidity, $c$ the speed of light and $\epsilon_0$ the vacuum permittivity. The $\pm$-sign represents the two cases of the electron beam traveling in the direction of the proton beam ($+$) or in the opposite direction ($-$). For hollow electron beam collimation, electron and proton beam travel in the same direction.

The kick in $(x',y')$ can then be expressed as (note $\frac{x}{r}=\cos(\phi),\frac{y}{r}=\sin(\phi)$):
\begin{align}
x'&=x'-\theta_{\rm max}\cdot\frac{r_2}{r^2}\cdot f(r)\cdot x\\
y'&=y'-\theta_{\rm max}\cdot\frac{r_2}{r^2}\cdot f(r)\cdot y
\end{align}
If the electron lens is offset by $(x_{\rm offset},y_{\rm offset})$, the coordinates $(x,y)$ are simply transfered to:
\begin{align}
\tilde x&=x+x_{\rm offset}\\
\tilde y&=y+y_{\rm offset}\\
\tilde r&=\sqrt{\tilde x^2+\tilde y^2}
\end{align}
and the kick is then given by:
\begin{align}
x'&=x'-\theta_{\rm max}\cdot\frac{r_2}{\tilde r^2}\cdot f(\tilde r)\cdot \tilde x\\
y'&=y'-\theta_{\rm max}\cdot\frac{r_2}{\tilde r^2}\cdot f(\tilde r)\cdot \tilde y
\end{align}

\subsection{Electron Cooler}

Electron cooling is a process used to reduce the momentum spread of charged particles such as ions or protons. This process involves passing the particles through a cloud of electrons that are cooler than the particles themselves. The particles lose energy as they collide with the electrons, causing them to slow down and reduce their temperature. The Parkhomchuk model is a mathematical model that describes the force acting on a particle as it passes through the electron cooler. This force is given by the equation \cite{parkhomchuk2000electron}:
\begin{equation}
\vec{F}= -\frac{4 e^4 n_{\mathrm{e}}}{m_e} \frac{\vec{V}}{\left(\sqrt{\vec{V}^2+V_{\mathrm{eff}}^2}\right)^3} \ln \left(\frac{\rho_{\min }+\rho_{\max }+\rho_{\mathrm{L}}}{\rho_{\min }+\rho_{\mathrm{L}}}\right)
\label{Eq:Parkhomchuk}
\end{equation}

Where $\vec{F}$ is the force acting on the ion when the ion is within the radius of the electron beam, $e$ is the elementary charge, $n_\mathrm{e}$ is the density of the electrons, $m_e$ is the electron mass, and $\vec{V}$ is the velocity difference between the ion and the electron. $V_\mathrm{eff}$ is the effective root-mean-square velocity of motion of Larmor circles, which is influenced by both the longitudinal root-mean-square electron velocity, $\mathrm{V}_\mathrm{l}$, and transverse drift motions, which arise due to imperfections in the magnetic field. In particular, the transverse motion is described by $\mathrm{V}_\mathrm{magnet}$, which shows how the root-mean-square electron velocity is affected by the quality of the magnetic field. The magnetic field quality is given by the ratio of the perpendicular and longitudinal components of the magnetic field, where 0 indicates an ideal magnetic field quality. The expression for $\mathrm{V}_\mathrm{magnet}$ depends on the magnetic field quality in the following way: $\mathrm{V}_\mathrm{magnet} = \beta_0 \gamma_0  c B_{ratio}$, where $B_{ratio}$ is the magnetic field quality. Where $c$ is the speed of light, $\beta_0$ is the velocity of the electrons, $\gamma_0$ is the relativistic factor, and $B_\mathrm{ratio}$ is the magnetic field quality. These two parameters combine in the following way to produce the effective electron velocity:

$$\mathrm{V}_{\mathrm{eff}} = \sqrt{\mathrm{Ve}_\mathrm{l}^2 + \mathrm{Ve}_\mathrm{magnet}^2}$$
The variables $\rho_{\min}$ and $\rho_{\max}$ are the minimum and maximum impact parameters and are defined as:
$$\rho_{min} = \frac{Z r_e}{(V/c)^2}$$
where $Z$ is the charge of the particle, $r_e$ is the classical electron radius.
$$\rho_{max} = \frac{V}{\omega_{plasma}+\frac{1}{\tau}}$$
Where $\omega_{\mathrm{plasma}}$ is the plasma frequency, which is given by $c \sqrt{4  \pi  n_e  r_e}$ and $\tau$ is the time that the ion spends in the cooler. Finally, $\rho_{\mathrm{L}}$ is the Larmor radius of the electrons, which is given by:
$$\rho_{\mathrm{L}}=\frac{m_e \cdot Ve_{\perp}}{e \cdot B}$$
Where $m_e$ is the mass of the electron, $B$ is the magnetic field strength, and $Ve_{\perp}$ is the perpendicular component of the electron velocity.

\subsubsection{Electron beam space charge}

An additional effect of electron cooling that needs to be taken into consideration is the space charge of the electron beam. Moreover, the electron beam will assume a parabolic profile with respect to the radius, which is given by \cite{poth1990electron}:
\begin{equation}
\frac{\Delta E(r)}{E_0}
=\frac{I r_{\mathrm{e}}}{e c} \frac{\gamma+1}{\beta_0^3 \gamma^2}\left(\frac{r}{r_0}\right)^2
\approx 1.2 \times 10^{-4} \frac{I}{\beta_0^3}\left(\frac{r}{r_{e-beam}}\right)^2
\label{Eq:space_charge_electron_beam}
\end{equation}
Equation \ref{Eq:space_charge_electron_beam} says that the electrons at the edge electron beam have a larger momentum than the electrons at the center. This means that the ions at the edge of the beam pipe will reach a larger equilibrium momentum than the ions at the core because the ions will assume the momentum of the electrons. 

The Xsuite electron cooler allows for the inclusion of an optional effect called "space charge neutralization," which is determined by the parameter "Neutralization space charge." A value of 0 for this parameter indicates that there is no space charge in the electron beam, while a value of 1 indicates that the electron beam will follow a parabolic profile as described in Equation \ref{Eq:space_charge_electron_beam}.

\subsubsection{Electron beam rotation}

An additional effect is the rotation of the electron beam around the beam axis due to the magnetic field of the electron cooler. The angular velocity of the rotation is given by \cite{poth1990electron}:
\begin{equation}
\omega
=\frac{\vec{F} \times \vec{B}}{e r|B|^2}
=\frac{I}{2 \pi \epsilon_0 c r_{\mathrm{e}-\text { beam }}^2 \beta \gamma^2 B_{\|}}
\approx 60 \frac{I}{r_{\mathrm{e} \text {-beam }}^2 \beta \gamma^2 B_{\|}}
\label{Eq:rotation_electron_beam}
\end{equation}
The inclusion of this effect in the Xsuite electron cooler is optional and determined by the parameter "Neutralization rotation." A value of 0 indicates that there is no rotation of the electron beam, while a value of 1 indicates that the electron beam will rotate with the angular frequency described in Equation \ref{Eq:rotation_electron_beam}.


\chapter{Linear optics calculations}
\label{opt}
Optics calculation are needed to study the motion around the closed orbit. By defining $z$ as the vector of $2 k$ coordinates,  
\begin{align}\label{opt:eqn:1}
z&=(z_1,\ldots,z_{2k})^T=(x-x_0,p_x-p_{x0},y-y_0,p_y-p_{y0},\tau-\tau_0,p_\tau-p_{\tau0})^T
\end{align}
one can define linear transfer maps (e.g. $M_{1\to 2}$ that propagates coordinates between two points $s_1$, $s_2$) and the one-turn map (e.g. $M_1$ that combines the effects for one turn starting from $s_1$):
\begin{align}\label{opt:eqn:2}
z(s_2)&= M_{1\to 2} z(s_1) & z(C+s_1) &= M_1 z(s_1).
\end{align}
In the following we will describe the optics calculation based on the Ripken formalism described in \cite{willeke88}. A good summary is also given in the MAD8 physics manual \cite{mad8phys}.

\section{Diagonalisation of one-turn matrix}
\label{opt:sec:1}
Since the matrices derive from symplectic maps, the eigenvalue spectrum of the one-turn map $M$ consists of 2$k$ distinct eigenvalues and linearly independent eigenvectors. In addition, for the motion to be stable the eigenvalues $\lambda_k^{\pm}$ with eigenvectors $v_k^{\pm}$ have to be complex \cite{willeke88}:
\begin{align}
M v_k^\pm  =  \lambda_k^\pm v_k^\pm, \ k=1,\ldots, 3 \\
v_k^+=(v_k^-)^*, \quad \lambda_k^+=(\lambda_k^-)^*, \quad |\lambda_k^{\pm}|=1
\end{align}
As the eigenvectors are linearly independent $M$ can be diagonalized with
\begin{align}
M &= V \Lambda V^{-1},
\end{align}
where $V$ consists of the eigenvectors and $\Lambda$ of the eigenvalues:
\begin{align}
V=&\left(
\begin{array}{cccc}
v^+_{1,1} & v^-_{1,1} & \cdots & v^-_{3,1}\\
v^+_{1,2} & v^-_{1,2} & \cdots & v^-_{3,2}\\
\vdots    & \vdots    & \vdots & \vdots \\
\end{array}
\right)  &
\Lambda=&\left(
\begin{array}{cccc}
\lambda^+_1 &    & &\\
& \lambda^-_1 & &\\
& & \ddots & \\
& & & \lambda^-_3
\end{array}
\right)
\end{align}
for which $v^{\pm}_{i,j}$ is the component $j$ of eigenvector $v_i^{\pm}$.

The same calculation can be carried out with real numbers by the following definitions:
\begin{align}\
v_k^\pm &= a_k\pm ib_k, & \lambda_k^\pm &= \cos \mu_k \pm i \sin \mu_k, &
\mu_k, a_k, b_k \in \mathbb{R}
\end{align}
such that:
\begin{align}\label{opt:eqn:1:1}
M=&W R W^{-1}
\end{align}
with
\begin{align}
R=R(\mu_k)=&\left(
\begin{array}{ccccc}
\cos \mu_1 & \sin \mu_1  &  & &\\
-\sin \mu_1 & \cos \mu_1 &  & &\\
&    & \ddots & &\\
& & & \cos \mu_3 & \sin \mu_3 \\
& & & -\sin \mu_3 & \cos \mu_3 \\
\end{array}
\right), \\
W=&\left(
\begin{array}{ccccc}
a_{1,1} & b_{1,1} & \cdots & a_{3,1} & b_{3,1} \\
a_{1,2} & b_{1,2} & \cdots & a_{3,2} & b_{3,2} \\
\vdots    & \vdots    & \vdots & \vdots\\
a_{1,6} & b_{1,6} & \cdots & a_{3,6} & b_{3,6} \\
\end{array}
\right)
\end{align}
Usually $\mu_k$ is written as $\mu_k=2\pi Q_k$, where $Q_k$ is then the tune of the mode $k$.
\section{Normalisation of eigenvectors}
\label{opt:sec:2}
By convention, the eigenvectors and values are normalized, sorted and rotated so that the following three conditions are fulfilled:
\begin{enumerate}
\item Plane 1 is associated with the horizontal, plane 2 with the vertical and plane 3 with the longitudinal plane. This is achieved by first normalizing the eigenvectors $v_k^{\pm}$ and then sorting them so that:
\begin{align}\label{opt:eqn:2:1}
|v_{j,2j-1}^{+}| =|v_{j,2j-1}^{-}| = \max_{k=1,2,3} v_{k,j}, \quad j=1,\ldots, 3
\end{align}
\item The eigenvectors are then rotated with a phase term $\psi_k$
\begin{align}
v_k& \to v_k \exp(i \psi_k) 
\end{align}
such that
\begin{align}\label{opt:eqn:2:2}
\mathrm{angle}(v_{k,2k-1}^{+})=0 \leftrightarrow \psi_k=-\mathrm{angle}(v_{k,2k-1}^{+})
\end{align}
In real space, Eqn.~\ref{opt:eqn:2:1} and \ref{opt:eqn:2:2} then become equivalent to:
\begin{align}
|a_{j,2j-1}| &=\max_{k=1,2,3} |a_{k,j}|,& b_{j,2 j-1}&=0, & j=1,\ldots, 3
\end{align}
This has the effect that a particle with $x=0$ is transformed to $\tilde x$ in the normalized phase space.
\item The sign of $b_{k,j}$ is fixed by the symplectic condition on $W$
\begin{align}
W^T S W = S
\end{align}
with $S$ defined as
\begin{align}
S&=\left(
\begin{array}{ccc}
0 & 1  &  \\
-1 & 0  &  \\
&    & \ddots \\
\end{array} 
\right)
\end{align}
which is equivalent to:
\begin{align}\label{opt:eqn:2:3}
a_k^T \cdot S \cdot b_k &=1, \quad b_k^T \cdot S \cdot a_k =-1, & \text{ for } k=l\nonumber\\
a_k^T \cdot S \cdot b_l &=0, & \text{ for }  k\not=l\\
a_k^T \cdot S \cdot a_l &=0, \quad b_k^T \cdot S \cdot b_l =0,  & k,l=1,\ldots,3 \nonumber
\end{align}
Eqn.~\ref{opt:eqn:2:3} yields that in phase space $a_k$ is thus obtained by an anticlockwise rotation of $b_k$ by $\pi/2$ and a scaling of its length with $|a_k|=\frac{1}{|b_k|}$.
\end{enumerate}
\section{Conversion to normalized coordinates}
\label{opt:sec:3}
We will show in the following that in the normalized phase space the propagation of particle coordinates $z(s)$ from $s_1$ to $s_2$ is just a rotation by an angle $\phi_k$ in the $k=1,\ldots,3$ planes, while the amplitude $I_k$ and initial phase $\phi_{k,0}$ stay constant, explicitly $z(s)$ is then given by:
\begin{align}\label{opt:eqn:3:3}
z(s)=\sum_{k=1}^3 \sqrt{2I_k}\left(
a_k(s) \cos \left(\phi_{k,0} + \phi_k(s)\right) -
b_k(s) \sin \left(\phi_{k,0} + \phi_k(s)\right)
\right) 
\end{align}
and
\begin{align}
z(s_2)&=W(s_2)R(\phi_k)W(s_1)^{-1}z(s_1), \\
&\hspace{30pt} \text{ with } \phi_k=\phi_k(s_2)-\phi_k(s_1)\nonumber
\end{align}
This implies that one turn is simply a rotation by $\phi_k=2\pi Q_k$ where $Q_k$ is the tune of the mode $k$. In the transverse plane the tune ($Q_{I,II}$) is usually positive and the particles rotate clockwise, while in the longitudinal plane the tune ($Q_{III}$) is negative above $\gamma_T$ leading to an anticlockwise rotation.

For the derivation the following steps are needed:
\begin{enumerate}
\item The effect of one turn on the normalized variable $\tilde z(s)=W^{-1}(s) z(s)$ is a rotation:
\begin{align}\label{opt:eqn:3:1}
\tilde z(C+s) = W^{-1}z(s+C)\overset{(\rm Eqn.\ref{opt:eqn:1:1})}{=}W^{-1}WRW^{-1}z(s)= R\tilde z(s),
\end{align}
As $M$ and $R$ are symplectic also $W$ is symplectic, and its inverse is thus given by $S^{-1}W^{T}S$, explicitly:
\begin{align}
W^{-1}&=
\left(
\begin{array}{cccccc}
b_{12} & - b_{11} &   b_{14} & - b_{13} &   b_{16} & - b_{15}\\
- a_{12} &   a_{11} & - a_{14} &   a_{13} & - a_{16} &   a_{15}\\
b_{22} & - b_{21} &   b_{24} & - b_{23} &   b_{26} & - b_{25}\\
- a_{22} &   a_{21} & - a_{24} &   a_{23} & - a_{26} &   a_{25}\\
b_{32} & - b_{31} &   b_{34} & - b_{33} &   b_{36} & - b_{35}\\
- a_{32} &   a_{31} & - a_{34} &   a_{33} & - a_{36} &   a_{35}\\
\end{array}
\right)
\label{eq:winv}
\end{align}
\item The one-turn map and $W$-matrix can be propagated from $s_1$ to $s_2$ by
\begin{align}
M_2&=M_{1 \to 2} M_1 M^{-1}_{1 \to 2}  &
W_2&=M_{1 \to 2} W_1
\end{align}
As Eqn.~\ref{opt:eqn:3:1} represents a similarity transformation, the eigenvalues are thus independent of the position $s$ and as the rotation matrix $R$ consists of the eigenvalues of $M$, the angle of the rotation $\mu_k=2\pi Q_k$ is thus also independent of $s$.

\item As Eqn.~\ref{opt:eqn:1:1} represents a basis transformation from the standard $\mathbb{R}^2$ basis to the eigenvector basis, the vectors $a_k$ and $b_k$ are projected onto (Eqn.~\ref{opt:eqn:2:3}):
\begin{align}\label{opt:eqn:3:2}
\tilde a_1=W^{-1}a_1&=-SW^TSa_1\nonumber\\
&=-S(a_1Sa_1,b_1Sa_1,\ldots,b_3Sa_1)^T=(1,0,\ldots,0)\nonumber\\
\tilde b_1=W^{-1}b_1&=-SW^TSb_1\nonumber\\
&=-S(a_1Sb_1,b_1Sb_1,\ldots,b_3Sb_1)^T=(0,1,\ldots,0)\\
\cdots & \nonumber\\
\tilde b_3=W^{-1}b_3&=-SW^TSb_3\nonumber\\
&=-S(a_1Sb_3,b_1Sb_3,\ldots,b_3Sb_3)^T=(0,0,\ldots,1)\nonumber
\end{align}
in the normalized phase space.
\item From Eqn.~\ref{opt:eqn:3:1} it follows that the amplitude $I_k$ and initial phase $\phi_{k0}$ of $\tilde z=W^{-1}z=(\tilde z_{a_1},\tilde z_{b_1},\ldots,\tilde z_{b_3})$ 
\begin{align}
I_k&=\frac{(\tilde z_{a_k})^2 +(\tilde z_{b_k})^2}{2}, \quad k=1,\ldots,3\label{opt:eqn:20a}\\
\tan\phi_{k0}&=-\frac{\tilde z_{b_k}}{\tilde z_{a_k}} \label{opt:eqn:20b}
\end{align}
are constants of the motion.
%, which is illustrated in Fig.~\ref{opt:fig:1}.
%\begin{figure}[h]
%	\centering
%	\includegraphics{normalized_phase_space_cropped.pdf}
%	\caption{Normalized phase space.\label{opt:fig:1}}
%\end{figure}
The initial phase is defined with a minus sign in view of the definition of the Twiss parameters, where the initial phase is then added (and not subtracted) to the phase advance. The components of $\tilde z$ are then explicitly given by:
\begin{align}
\tilde z_{a_k}&= \sum_{j=1}^3  b_{k,2j} z_{2j-1}- b_{k,2j-1} z_{2j}, \quad k=1,\ldots,3\\
\tilde z_{b_k}&= \sum_{j=1}^3 a_{k,2j-1} z_{2j}- a_{k,2j} z_{2j-1}, \quad k=1,\ldots,3.
\end{align}
An arbitrary vector $z(s)$ can thus be written in the following form:
\begin{align}
 z(s)&=W(s)\tilde z(s)\nonumber\\
 &=W(s)\left(\sum_{k=1}^{3}\tilde z_{a_k}\tilde a_k + \tilde z_{b_k}\tilde b_k\right)\nonumber\\
 &=\sum_{k=1}^{3}\tilde z_{a_k}W(s)\tilde a_k + \tilde z_{b_k}W(s)\tilde b_k\overset{\rm Eqn.~\ref{opt:eqn:3:2}}{=}\sum_{k=1}^{3}\tilde z_{a_k}a_k + \tilde z_{b_k}b_k\nonumber\\
 &\overset{\rm Eqns.~\ref
 	{opt:eqn:20a},\ref{opt:eqn:20b}}=\sum_{k=1}^{3}\sqrt{2I_k}\left(a_k\cos{\phi_{k0}}-b_k\sin{\phi_{k0}}\right)
\end{align}
\end{enumerate}

\section{Twiss parameters}
\label{opt:sec:4}
In the following the parameter $k$ will always be used for the mode $k$ and the parameter
$j=1,2,3$ for the horizontal ($x,p_x$), vertical ($y,p_y$) and longitudinal plane
$(\zeta,\delta)$ in the phase space. $z_{2j-1}$ then stands for the coordinates $(x,y,\zeta$)
and $z_{2j}$ for $(p_x,_y,\delta$).

The Twiss parameters can be introduced by writing the components of the eigenvector basis
$(a_k(s),b_k(s))$ as the product of two envelope functions $\sqrt{\beta_{k,j}(s)}$,
$\sqrt{\gamma_{k,j}(s)}$ and phase functions $\phi_{k,j}(s)$, $\bar\phi_{k,j}(s) = \phi_{k,j}(s) - \arctan(1/\alpha_{k,j})$,
also called Twiss parameters or lattice functions, with
\begin{align}
a_{k,2j-1}(s)&=\sqrt{\beta_{k,j}(s)}\cos{\phi_{k,j}(s)},\nonumber\\ b_{k,2j-1}(s)&=\sqrt{\beta_{k,j}(s)}\sin{\phi_{k,j}(s)}, \ k,j=1,\ldots,3, \label{opt:eqn:4:1}\\
a_{k,2j}(s)&=\sqrt{\gamma_{k,j}(s)}\cos{\bar\phi_{k,j}(s)}, \nonumber\\
b_{k,2j}(s)&=\sqrt{\gamma_{k,j}(s)}\sin{\bar\phi_{k,j}(s)}, \ k,j=1,\ldots,3 \label{opt:eqn:4:2}
\end{align}
where $\beta_{k,j}(s), \alpha_{k,j}(s), \gamma_{k,j}(s)$ represent the projection of the ellipse of mode $k$ on the plane of coordinates $z_{2k-1}-z_{2k}$. 
%(see Fig.~\ref{opt:fig:2})
%\begin{figure}[!ht]
%	\centering
%	\includegraphics[width=1.0\linewidth]{ripken_phase_space_ellipse.png}
%	\caption{Projection of lattice function in the $z-z'$ plane.\label{opt:fig:2}}
%\end{figure}

Using Eqns.~\ref{opt:eqn:3:3}, \ref{opt:eqn:4:1}, \ref{opt:eqn:4:2} and $\cos(x+y)=\cos x\cos y-\sin x\sin y$, the coordinates $z(s)$ can be expressed by:
\begin{align}
z_{2j-1}(s)&=\sum_{k=1}^3 \sqrt{2I_k
\beta_{k,j}(s)}\cos{(\phi_{k,j}(s)+\phi_{k,0})}\\
z_{2j}(s)&=\sum_{k=1}^3 \sqrt{2I_k
\gamma_{k,j}(s)}\cos{(\bar\phi_{k,j}(s)+\phi_{k,0})}, \ j=1,\ldots,3
\end{align}
Conversely the lattice functions can also be expressed by $a_k$ and $b_k$ with
\begin{align}
\beta_{k,j}(s)&=a_{k,2j-1}(s)^2 +b_{k,2j-1}(s)^2 \\
\alpha_{k,j}(s)&=- a_{k,2j-1}(s)a_{k,2j}(s) -b_{k,2j-1}(s)b_{k,2j}(s) \\
\gamma_{k,j}(s)&=a_{k,2j}(s)^2 +b_{k,2j}(s)^2,
\end{align}
The well known relations between the lattice functions
\begin{align}
\sum_{j=1}^3\beta_{k,j}\phi_{k,j}'&=1 \\
\gamma_{k,j}&=\frac{\beta_{k,j}^2\phi_{k,j}'^2+\alpha_{k,j}^2}{\beta_{k,j}}, \text{ with  }\\
\alpha_{k,j}&:=-\frac{1}{2}\beta_{k,j}'
\end{align}
can then be derived with the help of the normalization condition (Eqn.~\ref{opt:eqn:2:3})
\begin{align}
a_k^TSb_k=1
\end{align}
by the following steps:
\begin{enumerate}
\item As $x'=\frac{dx}{ds},\ y'=\frac{dy}{ds}$ and $\delta=\frac{d\zeta}{ds}$ the following relations hold also for $a_k$ and $b_k$:
\begin{align}
a_{k,2j}=a_{k,2j-1}'&=\frac{d}{ds}(a_{k,2j-1}), \\
b_{k,2j}=b_{k,2j-1}'&=\frac{d}{ds}(b_{k,2j-1}),\ k,j=1,\ldots,3 
\end{align}
\item The normalization condition Eqn.~\ref{opt:eqn:2:3} can then be written as
\begin{align}
a_k^TSb_k&=\sum_{j=1}^3\sqrt{\beta_{k,j}}\cos{\phi_{k,j}}\left(\sqrt{\beta_{k,j}}\sin{\phi_{k,j}}\right)'\nonumber\\
& \qquad -\left(\sqrt{\beta_{k,j}}\cos{\phi_{k,j}}\right)'\sqrt{\beta_{k,j}}\sin{\phi_{k,j}}\nonumber\\
&=\sum_{j=1}^3\beta_{k,j}\phi_{k,j}'\nonumber\\
&=1 \label{opt:eqn:4:3}
\end{align}
Note that Eqn.~\ref{opt:eqn:4:3} yields the the following relation between the phase advance $\phi$ and $\beta$ in 2D:
\begin{align}
\phi(s)=\phi(0)+\int_{s_0}^s\frac{1}{\beta(\bar s)}d\bar s
\end{align}
\item Using the abbreviation $\alpha_{k,j}:=-\frac{1}{2}\beta_{k,j}$, one finds for each mode $k$ and plane $j$
\begin{align}
\sqrt{\gamma_{k,j}}\cos{\phi_{k,j}}&=a_{k,2j}=a_{k,2j-1}'=(\sqrt{\beta_{k,j}}\cos{\phi_{k,j}})' &\quad (1)\nonumber\\
\sqrt{\gamma_{k,j}}\sin{\phi_{k,j}}&=b_{k,2j}=b_{k,2j-1}'=(\sqrt{\beta_{k,j}}\sin{\phi_{k,j}})' &\quad (2)\nonumber\\
\overset{(1)^2+(2)^2}{\Rightarrow} \gamma_{k,j}&=\frac{\beta_{k,j}^2\phi_{k,j}'^2+\alpha_{k,j}^2}{\beta_{k,j}}, \quad k,j=1,\ldots,3 &
\end{align}
which simplifies in the 2D case to:
\begin{align}
\gamma\overset{\rm Eqn.~\ref{opt:eqn:4:3}}{=}\frac{1+\alpha^2}{\beta}
\end{align}
\end{enumerate}

\section{Transformation to normalized coordinates}

The $W$ matrix can be used to transform normalized coordinate into physical coordinates and viceversa:

\begin{equation}
\left(\begin{array}{l}
x \\
p_x \\
y \\
p_y \\
\zeta \\
p_\zeta
\end{array}\right)
=
W
\left(\begin{array}{l}
\hat{x} \\
\hat{p}_x \\
\hat{y} \\
\hat{p}_y \\
\hat{\zeta} \\
\hat{p}_\zeta
\end{array}\right)
=
W
\left(\begin{array}{l}
\sqrt{\varepsilon_x} \tilde{x} \\
\sqrt{\varepsilon_x} \tilde{p}_x \\
\sqrt{\varepsilon_y} \tilde{y} \\
\sqrt{\varepsilon_y} \tilde{p}_y \\
\sqrt{\varepsilon_\zeta} \tilde{\zeta} \\
\sqrt{\varepsilon_\zeta} \tilde{p}_\zeta
\end{array}\right)
\label{eq:norm_coord}
\end{equation}
where 
\begin{equation}
\left(\begin{array}{llllll}\tilde{x} & \tilde{p}_x & \tilde{y} & \tilde{p}_y & \tilde{\zeta} & \tilde{p}_\zeta\end{array}\right)
\end{equation}
are normalized coordinates in sigmas and $\varepsilon_x$, $\varepsilon_y$ and $\varepsilon_\zeta$ are the geometric emittances.

\section{Action, amplitude and emittance}

We define the action associated to the three modes:
\begin{align}
J_x = \frac{\hat{x}^2 + \hat{p}^2_x}{2},
\quad
J_y = \frac{\hat{y}^2 + \hat{p}^2_y}{2},
\quad
J_\zeta = \frac{\hat{\zeta}^2 + \hat{p}^2_\zeta}{2}
\label{eq:action}
\end{align}

The corresponding amplitudes are defined such that:
\begin{align}
A_x &= \sqrt{\hat{x}^2 + \hat{p}^2_x} = \sqrt{2J_x}, \label{eq:ampl_x}\\
A_y &= \sqrt{\hat{y}^2 + \hat{p}^2_y} = \sqrt{2J_y}\\
A_\zeta &= \sqrt{\hat{\zeta}^2 + \hat{p}^2_\zeta} = \sqrt{2J_\zeta}
\end{align}

A Gaussian distribution is defined such that the density with respect to each action can be written as:
\begin{equation}
f(J_x) = K e^{J_x/\varepsilon_x}
\end{equation}
where the emittance $\varepsilon_x$ can be written as:
\begin{equation}
\varepsilon_x = <J_x> = \int J_x f(J_x) \,dJ_x
\end{equation}


\section{Dispersion and crab dispersion}

For a particle having no betatron amplitude ($\hat{x} = \hat{p}_x =\hat{y} =\hat{p}_y=0$) we can write:

\begin{align}
x &= W_{15}\hat{\zeta} + W_{16}\hat{p}_\zeta \label{eq:crab_x}\\
\zeta &= W_{55}\hat{\zeta} + W_{56}\hat{p}_\zeta \label{eq:crab_zeta}\\
p_\zeta &= W_{65}\hat{\zeta} + W_{66}\hat{p}_\zeta \label{eq:crab_pzeta}
\end{align}


\subsection{Dispersion}
The dispersion is:
\begin{equation}
D_{x}^{p_\zeta} = \frac{dx}{d \delta} \quad \text{for } \zeta=0
\end{equation}

By imposing $\zeta = 0$ in Eq.\,\ref{eq:crab_zeta} we obtain:

\begin{equation}
\hat{\zeta} = - \frac{W_{56}}{W_{55}}\hat{p_\zeta}
\label{eq:crab_zeta_pzeta}
\end{equation}

We replace in Eq.\,\ref{eq:crab_pzeta}:

\begin{equation}
  \hat{p}_\zeta = \left( W_{66}\ -  \frac{W_{65}W_{56}}{W_{55}} \right)^{-1}p_\zeta
\end{equation}

From Eq.\,\ref{eq:crab_zeta_pzeta} we obtain:
\begin{equation}
\hat{\zeta} = - \frac{W_{56}}{W_{55}}\left( W_{66}\ -  \frac{W_{65}W_{56}}{W_{55}} \right)^{-1} p_\zeta
\end{equation}

Replacing the last two into Eq.\,\ref{eq:crab_x} we obtain:

\begin{equation}
x = \left(W_{16} -\frac{W_{15}W_{56}}{W_{55}}\right)\left( W_{66}\ -  \frac{W_{65}W_{56}}{W_{55}} \right)^{-1} p_\zeta
\end{equation}

which gives the dispersion:
\begin{equation}
D_{x}^{p_\zeta} = \left(W_{16} -\frac{W_{15}W_{56}}{W_{55}}\right)\left( W_{66}\ -  \frac{W_{65}W_{56}}{W_{55}} \right)^{-1}
\end{equation}

A similar type of calculation can be done for the other planes, and for the transverse momentum dispersion, obtaining $D_{px}^{p_\zeta}$:
which gives the dispersion:
\begin{equation}
D_{px}^{p_\zeta} = \left(W_{26} -\frac{W_{25}W_{56}}{W_{55}}\right)\left( W_{66}\ -  \frac{W_{65}W_{56}}{W_{55}} \right)^{-1}
\end{equation}

\begin{equation}
D_{y}^{p_\zeta} = \left(W_{36} -\frac{W_{35}W_{56}}{W_{55}}\right)\left( W_{66}\ -  \frac{W_{65}W_{56}}{W_{55}} \right)^{-1}
\end{equation}

\begin{equation}
D_{py}^{p_\zeta} = \left(W_{46} -\frac{W_{45}W_{56}}{W_{55}}\right)\left( W_{66}\ -  \frac{W_{65}W_{56}}{W_{55}} \right)^{-1}
\end{equation}

\subsection{Crab dispersion}

The crab dispersion is:
\begin{equation}
D_x^\zeta = \frac{dx}{dz} \quad \text{for } p_\zeta=0
\end{equation}

By imposing $p_\zeta = 0$ in Eq.\,\ref{eq:crab_pzeta} we obtain:

\begin{equation}
\hat{p}_\zeta = - \frac{W_{65}}{W_{66}}\hat{\zeta}
\label{eq:crab_pzeta_zeta}
\end{equation}

We replace in Eq.\,\ref{eq:crab_zeta}:

\begin{equation}
\hat{\zeta} = \left( W_{55}\ -  \frac{W_{56}W_{65}}{W_{66}} \right)^{-1}\zeta
\end{equation}

From Eq.\,\ref{eq:crab_pzeta_zeta} we obtain:
\begin{equation}
\hat{p}_\zeta = - \frac{W_{65}}{W_{66}}\left( W_{55}\ -  \frac{W_{56}W_{65}}{W_{66}} \right)^{-1} \zeta
\end{equation}

Replacing the last two into Eq.\,\ref{eq:crab_x} we obtain:

\begin{equation}
x = \left(W_{15} -\frac{W_{16}W_{65}}{W_{66}}\right)\left( W_{55}\ -  \frac{W_{56}W_{65}}{W_{66}} \right)^{-1} \zeta
\end{equation}

which gives the crab dispersion:
\begin{equation}
D_x^\zeta = \left(W_{15} -\frac{W_{16}W_{65}}{W_{66}}\right)\left( W_{55}\ -  \frac{W_{56}W_{65}}{W_{66}} \right)^{-1}
\end{equation}

A similar type of calculation can be done for the other planes, and for transverse momentum crab dispersion obtaining:
\begin{equation}
D_{p_x}^\zeta = \left(W_{25} -\frac{W_{26}W_{65}}{W_{66}}\right)\left( W_{55}\ -  \frac{W_{56}W_{65}}{W_{66}} \right)^{-1}
\end{equation}
\begin{equation}
D_{y}^\zeta = \left(W_{35} -\frac{W_{36}W_{65}}{W_{66}}\right)\left( W_{55}\ -  \frac{W_{56}W_{65}}{W_{66}} \right)^{-1}
\end{equation}
\begin{equation}
D_{p_y}^\zeta = \left(W_{45} -\frac{W_{46}W_{65}}{W_{66}}\right)\left( W_{55}\ -  \frac{W_{56}W_{65}}{W_{66}} \right)^{-1}
\end{equation}

\section{Linear betatron coupling}

The following is based on \cite{luo2004phase} and \cite{Jones:2020hhl}.
~\\~\\
In the presence of betatron coupling the transverse on-momentum motion can be written as:
\begin{equation}
\left\{\begin{array}{l}
x_n=A_{1, x} \cos \left[2 \pi Q_1(n-1)+\phi_{1, x}\right]+A_{2, x} \cos \left[2 \pi Q_2(n-1)+\phi_{2, x}\right] \\
y_n=A_{1, y} \cos \left[2 \pi Q_1(n-1)+\phi_{1, y}\right]+A_{2, y} \cos \left[2 \pi Q_2(n-1)+\phi_{2, y}\right]
\end{array}\right.
\end{equation}

We can define:
\begin{equation}
\left\{\begin{array}{l}
r_1=\left|A_{1, y}\right| /\left|A_{1, x}\right| \\
r_2=\left|A_{2, x}\right| /\left|A_{2, y}\right|
\end{array}\right.
\end{equation}

\begin{equation}
\left\{\begin{aligned}
\Delta \phi_1 & =\phi_{1, y}-\phi_{1, x} \\
\Delta \phi_2 & =\phi_{2, x}-\phi_{2, y}
\end{aligned}\right.
\end{equation}

These quantities can be obtained from the normalized W matrix as:
\begin{equation}
\left\{\begin{array}{l}
r_1=\sqrt{W_{31}^2+W_{32}^2} / W_{11} \\
r_2=\sqrt{W_{13}^2+W_{14}^2} / W_{33}
\end{array}\right.
\end{equation}

\begin{equation}
\left\{\begin{array}{l}
\Delta \phi_{1,0}=\arctan \left(W_{32} / W_{31}\right) \\
\Delta \phi_{2,0}=\arctan \left(W_{14} / W_{13}\right)
\end{array}\right.
\end{equation}

From these we can compute the following quantities as\,\cite{Jones:2020hhl}:
\begin{equation}
\left|C^{-}\right|=\frac{2 \sqrt{r_1 r_2}\left|Q_1-Q_2\right|}{\left(1+r_1 r_2\right)}
\end{equation}
\begin{equation}
\chi(s) = \Delta \phi_{1,0}(s)
\end{equation}
\begin{equation}
C^-(s) = \left|C^{-}\right|e^{i\chi(s)}
\end{equation}
where $Q_1$ and $Q_2$ are the tunes of the betatron eigenmodes. Note that only the phase of $C^-(s)$ is $s$ dependent.

It is possible to prove that $C^-$ is related to the skew quadrupole strengths along the ring by the following relation:
\begin{equation}
C^{-}=\left|C^{-}\right| e^{i \chi}=\frac{1}{2 \pi} \int_0^L \sqrt{\beta_x \beta_y} k_s e^{i\left[\Phi_x-\Phi_y-2 \pi \Delta \cdot s / L\right]} d l .
\end{equation}
where $\Delta$ is the difference of the unperturbed fractional tunes.





\chapter{Synchrotron radiation}

We collect here some relevant properties of synchrotron radiation~\cite{hofmann_2004}:

We assume $B = |B_\perp|$.

Classical particle radius:
\begin{equation}
r_0=Q^2 /\left(4 \pi \epsilon_0 m_0 c^2\right)
\end{equation}

Curvature, rigidity, field:
\begin{equation}
\frac{1}{\rho}=\frac{Q B}{p}=\frac{Q B}{m_0 c \beta \gamma}
\end{equation}

Emitted power:
\begin{equation}
P_{\mathrm{s}}=\frac{2 r_0 c^3 Q^2 \beta^2 \gamma^2 B^2}{3 m_0 c^2}
\end{equation}



Critical frequency:
\begin{equation}
\omega_{\mathrm{c}}=\frac{3 Q \beta^2 \gamma^2 B }{2 m_0}
\end{equation}

Critical energy:
\begin{equation}
E_{\gamma \mathrm{c}}=\hbar \omega_{\mathrm{c}}
=\frac{3 Q \hbar\beta^2 \gamma^2 B }{2 m_0}
\end{equation}

Number of photons per unit time:
\begin{equation}
\dot{n}_{\mathrm{s}}=\frac{15 \sqrt{3}}{8}\frac{P_{\mathrm{s}}}{E_{\gamma \mathrm{c}}} 
=\frac{60 \sqrt{3}}{72}
\frac{ r_0 c Q B}{\hbar}
\end{equation}

Average photon energy:
\begin{equation}
\left\langle E_\gamma\right\rangle=\frac{8 \sqrt{3}}{45} E_{\gamma \mathrm{c}}
=\frac{8 \sqrt{3}}{15} 
\frac{ Q \hbar\beta^2 \gamma^2 B }{2 m_0}
\end{equation}

Photon energy variance:
\begin{equation}
\left\langle E_\gamma^2\right\rangle=\frac{11}{27} E_{\gamma \mathrm{c}}^2
= 
\frac{11}{12}\frac{ Q^2\hbar^2\beta^4\gamma^4 B^2 }{ m_0^2}
\end{equation}

\begin{equation}
\left\langle \dot{n}_{\mathrm{s}} \Delta \delta ^2\right\rangle
=
\frac{\left\langle \dot{n}_{\mathrm{s}} {E_\gamma^2}
 \right\rangle} {E_0^2}
= 
\frac{11}{12}
\frac{1}{m_0^2 c^4 \gamma_0^2 }
\frac{ Q^2\hbar^2\beta^4\gamma^4 B^2 }{ m_0^2}
\frac{60 \sqrt{3}}{72}
\frac{ c Q B}{\hbar}
\frac{Q^2}{4 \pi \epsilon_0 m_0 c^2}
\end{equation}

The algorithm to generate photon energies with the appropriate distribution in described in~\cite{helmut_synrad_90}.



\section{Damping from synchrotron radiation}

The damping constants from synchrotron radiation can be easily obtained from magnitude of the eigenvalues of the one-turn matrix:
\begin{align}
\alpha_x &= -\log( \lvert \lambda_x \rvert)\\
\alpha_y &= -\log( \lvert \lambda_y \rvert)\\
\alpha_\zeta &= -\log( \lvert \lambda_\zeta \rvert)
\end{align}

The damping acts such that:
\begin{equation}
\frac{1}{A_x} \frac{dA_x}{dt} = - \frac{\alpha_x}{T_0} 
\end{equation}
where $T_0$ is the revolution period.
From Eq.\,\ref{eq:ampl_x} we obtain:
\begin{equation}
\frac{dJ_x}{dt} = - \frac{2\alpha_x}{T_0}  J_x
\end{equation}
By averaging over the beam distribution we obtain:
\begin{equation}
\frac{d\varepsilon_x}{dt} = - \frac{2\alpha_x}{T_0}  \varepsilon_x
\label{eq:grate_rad_damp}
\end{equation}

\section{Equilibrium emittance}

This section is based on the approach described in \cite{chao_eq_emit}.

To account for the kicks experienced by the particles due to quantum excitation we note that the transverse momentum change due to an energy kick in the direction of the particle motion can be written as:
\begin{align}
P_{x,y}^\text{new} = P_{x,y}^\text{old}  \frac{P^\text{new}}{P^\text{old}}
\end{align}

From this:
\begin{align}
P_{x,y}^\text{new} - P_{x,y}^\text{old} = P_{x,y}^\text{old}  \left(\frac{P^\text{new} - P^\text{old}}{P^\text{old}} \right) 
\end{align}

Dividing by $P_0$:
\begin{align}
\frac{P_{x,y}^\text{new} - P_{x,y}^\text{old}}{P_0} = 
\frac{P_{x,y}^\text{old}}{P_0}
\left(\frac{P^\text{new} - P^\text{old}}{P_0} \right) 
\frac{P_0}{P^\text{old}}
\end{align}

Using the accelerator coordinates definitions (Eqs.\,\ref{eq:coord1} and \ref{eq:coord2}), we obtain:
\begin{align}
\Delta p_{x,y} = \frac{p_{x,y}}{1 + \delta} \Delta \delta
\end{align}

The corresponding change in normalized coordinates can be computed from Eq.\,\ref{eq:norm_coord}:

\begin{equation}
\left(\begin{array}{l}
\Delta \tilde{x} \\
\Delta \tilde{p}_x\\
\Delta \tilde{y} \\
\Delta \tilde{p}_y \\
\Delta \tilde{\zeta} \\
\Delta \tilde{p}_\zeta
\end{array}\right)
=
W^{-1}
\left(\begin{array}{c}
0 \\
\cfrac{p_{x}}{1 + \delta} \Delta \delta \\
0 \\
\cfrac{p_{y}}{1 + \delta} \Delta \delta \\
0 \\
\Delta \delta
\end{array}\right)
\label{eq:norm_coord}
\end{equation}

Using the Eq.\,\ref{eq:winv} we obtain:
\begin{align}
&\Delta \hat{x} = \mathcal{K}_x \Delta \delta \label{eq:sr_dxhat}\\
&\Delta \hat{p}_x = \mathcal{K}_{p_x} \Delta \delta \label{eq:sr_dxphat}\\
&\Delta \hat{y} = \mathcal{K}_{y} \Delta \delta \\
&\Delta \hat{p}_y = \mathcal{K}_{p_y} \Delta \delta\\
&\Delta \hat{\zeta} = \mathcal{K}_{\zeta} \Delta \delta \\
&\Delta \hat{p}_\zeta = \mathcal{K}_{p_{\zeta}}\Delta \delta
\end{align}
where:
\begin{align}
\mathcal{K}_x &= 
\left( \cfrac{a_{11}p_{x} + a_{13}p_{y} }{1 + \delta} + a_{15}\right)\\
\mathcal{K}_{p_x} &= 
\left( \cfrac{b_{11}p_{x} + b_{13}p_{y} }{1 + \delta} + b_{15}\right)\\
\mathcal{K}_y &= 
\left( \cfrac{a_{21}p_{x} + a_{23}p_{y} }{1 + \delta} + a_{25}\right)\\
\mathcal{K}_{{p}_y} &= 
\left( \cfrac{b_{21}p_{x} + b_{23}p_{y} }{1 + \delta} + b_{25}\right)\\
\mathcal{K}_{\zeta} &= 
\left( \cfrac{a_{31}p_{x} + a_{33}p_{y} }{1 + \delta} + a_{35}\right)\\
\mathcal{K}_{p_\zeta} &= 
\left( \cfrac{b_{31}p_{x} + b_{33}p_{y} }{1 + \delta} + b_{35}\right)
\end{align}

The change in action (see Eq.\,\ref{eq:action}) associated to the first mode, due to the emission of a photon can be written as:
\begin{align}
\Delta J_x &= \frac{1}{2}\left[
\left(\hat{x} +\Delta \hat{x}\right)^2 
\left(\hat{p}_x +\Delta \hat{p}_x \right)^2 
-\hat{x}^2 - \hat{p}_x^2
\right]\\
&= \frac{1}{2}\left[
\Delta \hat{x}^2 + \Delta \hat{p}_{x}^2
+ 2 \hat{x} \Delta \hat{x} + 2 \hat{p}_{x} \Delta \hat{p}_{x} 
\right]
\end{align}

Averaging over all particles in the beam we obtain:
\begin{align}
\Delta \varepsilon_x = <\Delta J_x> = 
\frac{1}{2}\left( <\Delta \hat{x}^2> + <\Delta \hat{p}_{x}^2> \right)
\end{align}

Using Eqs.\,\ref{eq:sr_dxhat} and \ref{eq:sr_dxphat} we obtain:
\begin{align}
\Delta \varepsilon_x = <\Delta J_x> = 
\frac{1}{2}\left(\mathcal{K}^2_x + \mathcal{K}^2_{p_x}\right)<\Delta \delta^2>
\end{align}

Assuming that the kicks are uncorrelated we can obtain the emittance growth rate from quantum excitation integrating over a full turn:
\begin{align}
\left(\frac {d\varepsilon_x}{dt} \right)_\text{quant}= 
\frac{1}{2T_0c}\int_0^C\left(\mathcal{K}^2_x + \mathcal{K}^2_{p_x}\right)<\dot{N} \Delta \delta^2> \,ds
\label{eq:grate_quantum}
\end{align}
where $\dot{N}$ is the photon emission rate (number of photons per unit time), $T_0$ is the revolution period, C is the circumference.

By summing Eqs.\,\ref{eq:grate_rad_damp} and \ref{eq:grate_quantum} we obtain the total instantaneous growth rate:
\begin{align}
\frac {d\varepsilon_x}{dt} = 
\left(\frac {d\varepsilon_x}{dt}\right)_\text{damp}
+
\left(\frac {d\varepsilon_x}{dt}\right)_\text{quant}
=
- \frac{2\alpha_x}{T_0}  \varepsilon_x
+
\frac{1}{2T_0c}\int_0^C\left(\mathcal{K}^2_x + \mathcal{K}^2_{p_x}\right)<\dot{N} \Delta \delta^2> \,ds
\end{align}

By imposing the derivative to be zero we obtain the value of the equilibrium emittance:
\begin{align}
\varepsilon_x= 
\frac{1}{4\alpha_xc}\int_0^C\left(\mathcal{K}^2_x + \mathcal{K}^2_{p_x}\right)<\dot{N} \Delta \delta^2> \,ds
\label{eq:equilemi1}
\end{align}

In the ultra-relativistic approximation:
\begin{align}
<\dot{N} \Delta \delta^2> = 
\frac{<\dot{N} (\Delta E)^2>}{E_0^2}
\label{eq:equilemi2}
\end{align}

\chapter{Synchrotron motion}
\label{ch:synchmon}

We collect here some relevant properties and quantities of the longitudinal particle motion.

Definition of momentum compaction factor:
\begin{equation}
\alpha_c = 
\frac{\Delta C/C}\delta
\end{equation}

Slip factor:
\begin{equation}
\eta = -\frac{\Delta f/f_0}{\delta} = \alpha_c - \frac{1}{\gamma_0^2}
= \frac{1}{\gamma_t^2}- \frac{1}{\gamma_0^2}
\end{equation}

(positive above transition)

Slippage over a single turn:
\begin{align}
\Delta \zeta 
& = -\beta_0 c \Delta T 
= -\beta_0 c (T - T_0) 
= -\beta_0 c \left(\frac{1}{f} - \frac{1}{f_0}\right) \\
&= -\frac{\beta_0 c}{f_0} \left(\frac{1}{1 + {\Delta f}/{f_0}} - 1\right)
\simeq \frac{\beta_0 c}{f_0}\frac{\Delta f}{f_0}
= -\eta \frac{\beta_0 c}{f_0}\delta
= -\eta C \delta
\end{align}

RF kick
\begin{align}
\Delta E
& = q V_{RF} \sin\left( 2 \pi h_{RF} f_0 t + \phi_{RF} \right)\\
& = q V_{RF} \sin\left(-2 \pi h_{RF} \frac{\zeta}{C} + \phi_{RF} \right)\\
& = q V_{RF} \sin\left(-2 \pi f_{RF} \frac{\zeta}{\beta_0 c} + \phi_{RF} \right)
\end{align}
from which:
\begin{align}
\Delta p_\zeta = \frac{\Delta E}{\beta_0^2E_0} 
= \frac{q V_{RF}}{\beta_0^2E_0}\sin\left(-2 \pi f_{RF} \frac{\zeta}{\beta_0 c} + \phi_{RF}\right)
\end{align}

\section{Linearized motion}

We expand around the fixed point $\zeta_0$:
\begin{align}
\Delta p_\zeta \approx \frac{q V_{RF}}{\beta_0^2E_0}\sin\left(-2 \pi f_{RF} \frac{\zeta_0}{\beta_0 c} + \phi_{RF}\right) -\frac{2\pi q f_{RF}V_{RF}}{\beta_0^3 E_0 c}(\zeta-\zeta_0)\cos\left(-2\pi f_{RF} \frac{\zeta_0}{\beta_0 c} + \phi_{RF}\right)
\end{align}

We call synchronous phase:
\begin{align}
\phi_s = -2 \pi f_{RF} \frac{\zeta_0}{\beta_0 c} + \phi_{RF}
\end{align}
And we call 
\begin{align}
\hat{\zeta}= \zeta - \zeta_0
\end{align}
obtaining:
\begin{align}
\Delta p_\zeta \approx \frac{q V_{RF}}{\beta_0^2E_0}\sin{\phi_s} -\frac{2\pi q f_{RF} V_{RF}}{\beta_0^3 E_0 c}\hat{\zeta}\cos{\phi_s}
\end{align}

We assume that that the energy deviation of the stable fixed point is zero:
\begin{align}
\Delta {p}_\zeta \approx -\frac{2\pi q f_{RF} V_{RF}}{\beta_0^3 E_0 c}\cos{\phi_s}\hat{\zeta}
\label{eq:deltapzeta}
\end{align}





\section{Smooth approximation}

Assuming that the slippage and the energy kicks are uniformly distributed along the ring we have:
\begin{align}
\frac{dp_\zeta}{d s} &= \frac{\Delta {p}_\zeta}{C}
=-\frac{2\pi q V_{RF}}{\beta_0^3 C E_0 c}\cos{\phi_s}\hat{\zeta}
\\
\frac{d\hat{\zeta}}{d s} &= \frac{\Delta \zeta}{C} = -\eta p_\zeta
\label{eg:dzeta_ds}
\end{align}

where we have used the approximation:
\begin{align}
\delta \ll 1  \Rightarrow \delta \simeq p_\zeta
\end{align}

We derive the second equation and replace the first:

\begin{align}
\frac{d^2\hat{\zeta}}{d s^2}  -  \frac{2\pi q \eta f_{RF} V_{RF}}{\beta_0^3 C E_0 c}\cos{\phi_s}\hat{\zeta} = 0
\end{align}

The motion is stable if
\begin{align}
\eta \cos{\phi_s} < 0
\end{align}

In that case the solution is in the form:
\begin{align}
\hat{\zeta}(s) = A \sin\left( \sqrt{-  \frac{2\pi q \eta f_{RF} V_{RF}}{\beta_0^3 C E_0 c}\cos{\phi_s}} s + B\right) = A \sin\left( 2 \pi Q_s s/C + B\right)
\end{align}

where the synchrotron tune is given by:


\begin{align}
Q_s = \sqrt{-  \frac{2\pi q \eta f_{RF} V_{RF}}{\beta_0^3 C E_0 c}\cos{\phi_s}} \frac{C}{2\pi}
= \sqrt{-  \frac{ q \eta f_{RF} C V_{RF}}{2\pi\beta_0^3 E_0 c}\cos{\phi_s}}
\end{align}
We replace
\begin{align}
f_{RF} = 
\frac{h_{RF} {\beta_0 c}}{C}
\end{align}
obtaining:

\begin{align}
Q_s 
= \sqrt{-  \frac{ q \eta h_{RF} V_{RF}}{2\pi\beta_0^2 E_0}\cos{\phi_s}}
\end{align}

The solution can be written as:
\begin{align}
\hat{\zeta}(s) = \hat{\zeta}_A \cos\left( 2 \pi Q_s s/C\right) + B \sin\left( 2 \pi Q_s s/C \right)
\end{align}

Replacing in Eq.~\ref{eg:dzeta_ds}:
\begin{align}
p_\zeta = - \frac{2\pi Q_s} {\eta C} \left( -\zeta_A \sin\left( 2 \pi Q_s s/C\right) + B \cos\left( 2 \pi Q_s s/C \right)\right)
\end{align}
Replacing $s=0$:

\begin{align}
p_{\zeta_A} = - \frac{2\pi Q_s} {\eta C} B
\end{align}

from which:
\begin{align}
B = -\frac{\eta C}{2\pi Q_s}p_{\zeta_A} = -\beta_\zeta p_{\zeta_A}
\end{align}

where we have defined:
\begin{align}
\beta_\zeta = \frac{\eta C}{2\pi Q_s}
\end{align}

Replacing
\begin{align}
\hat{\zeta}(s) &= \hat{\zeta}_A \cos\left( 2 \pi Q_s \frac{s}{C}\right) -p_{\zeta_A} \beta_\zeta \sin\left( 2 \pi Q_s \frac{s}{C} \right)\\
p_\zeta(s) &= \frac{\hat{\zeta}_A}{\beta_\zeta} \sin\left( 2 \pi Q_s \frac{s}{C}\right)
           + p_{\zeta_A} \cos\left( 2 \pi Q_s \frac{s}{C}\right)
\end{align}

For the kick-drift mode we want to rewrite the Eq.~\ref{eq:deltapzeta}:
\begin{align}
\Delta {p}_\zeta =-\frac{2\pi q f_{RF} V_{RF}}{\beta_0^3 E_0 c}\cos{\phi_s}\hat{\zeta}
\end{align}

\section{Hamiltonian of the synchrotron motion}

In this section we use the time in the laboratory frame as independent variable as done in the PyHEADTAIL longitudinal treatment. In this section we also include the effect of a reference momentum change of $\Delta P_0$ per turn.

We assume small energy deviations, hence we can consider the coordinates $(\zeta, \delta)$ to be canonically conjugate ($\delta \ll 1 \Rightarrow \delta \simeq p_\zeta$).

The longitudinal motion can be described by the following Hamiltonian:
\begin{equation}
H(\zeta, \delta) = -\frac{1}{2} \eta \beta_0 c \delta^2
                   + \frac{\beta_0 c}{C}\frac{\Delta P_0}{P_0} \zeta
                   - \frac{q_0}{P_0}\sum_i \frac{1}{2\pi h_i}V_i \cos\left(
                   		-2 \pi h_i \frac{\zeta}{C} + \phi_i \right)
\end{equation}

This can be proven using Hamilton's equations:
\begin{align}
&\frac{d\zeta}{dt} = \frac{\partial H}{\partial \delta} = -\eta \beta_0 c \delta\\
&\frac{d\delta}{dt} = -\frac{\partial H}{\partial \zeta} = 
	- \frac{\beta_0 c}{C}\frac{\Delta P_0}{P_0}
                   + \frac{q_0}{P_0 C} \sum_i 
                   V_i \sin\left(
                   	-2 \pi h_i \frac{\zeta}{C} + \phi_i \right)
\end{align}

The coordinate change over one revolution is:
\begin{align}
&\Delta \zeta = \frac{d\zeta}{dt}\frac{C}{\beta_0 c} 
	= -\eta C \delta\\
&\Delta \delta =\frac{d\delta}{dt} \frac{C}{\beta_0 c}= 
	- \frac{\Delta P_0}{P_0}
                   + \frac{q_0}{\beta_0 c P_0} \sum_i 
                   V_i \sin\left(
                   	-2 \pi h_i \frac{\zeta}{C} + \phi_i \right)
\end{align}
which are consistent with those found in Sec.\,\ref{ch:synchmon}.

\subsection{Fixed points}

The fixed points can be found by imposing $\Delta\zeta = 0$ and $\Delta\delta = 0$.
If only a single harmonic is present a closed solution can be found:

\begin{equation}
- \frac{\Delta P_0}{P_0}
+ \frac{q_0}{\beta_0 c P_0} 
V_\text{RF} \sin\left(
-2 \pi h_\text{RF} \frac{\zeta}{C} + \phi_i \right) = 0
\end{equation}

We want to get an explicit expression for $\zeta$:
\begin{equation}
 \frac{\Delta P_0\beta_0 c }{q_0 V_\text{RF}} 
 = 
\sin\left(
-2 \pi h_\text{RF} \frac{\zeta}{C} + \phi_i \right)
\end{equation}
There are two families of solutions:
\begin{align}
 &\arcsin\left(\frac{\Delta P_0\beta_0 c }{q_0 V_\text{RF}} \right)
 + 2 n \pi
 = 
-2 \pi h_\text{RF} \frac{\zeta}{C} + \phi_\text{RF} \\
 &\pi - \arcsin\left(\frac{\Delta P_0\beta_0 c }{q_0 V_\text{RF}} \right)
 + 2 n \pi
 = 
-2 \pi h_\text{RF} \frac{\zeta}{C} + \phi_\text{RF}
\end{align}
where $n$ is an integer number. Depending on the sign of $\eta$ Only one family of fixed points is stable.

We solve for $\zeta$:
\begin{align}
&\zeta =
 \frac{C}{2 \pi h_\text{RF}} \left(
 \phi_\text{RF}
 -\arcsin\left(\frac{\Delta P_0\beta_0 c }{q_0 V_\text{RF}} \right)
 +2 n \pi
 \right)\\
 &\zeta =
 \frac{C}{2 \pi h_\text{RF}} \left(
 \phi_\text{RF} + \pi
 +\arcsin\left(\frac{\Delta P_0\beta_0 c }{q_0 V_\text{RF}} \right)
 +2 n \pi
 \right)
\end{align}
It is possible to set $\phi_\text{RF}$ to place a fixed point of either family in $\zeta = 0$:
\begin{align}
&\phi_\text{RF} = 
 \arcsin\left(\frac{\Delta P_0\beta_0 c }{q_0 V_\text{RF}} \right)
 \label{eq:phibelowtr}
 \\
 & \phi_\text{RF} = 
 \pi
 - \arcsin\left(\frac{\Delta P_0\beta_0 c }{q_0 V_\text{RF}} \right)
  \label{eq:phiabovetr}
\end{align}
It can be shown that, to have a stable fixed point in $\zeta=0$ one needs to use Eq.\,\ref{eq:phibelowtr} when $\eta<0$ (below transition) and Eq.\,\ref{eq:phiabovetr} when $\eta>0$ (below transition).


\chapter{Coasting beams}

In coasting beams different particles have different revolution frequencies, depending on their momentum, particles perform a different number of turns in a given time. 

If in the line we have collective elements which need to measure the beam distribution at a certain location $s$ and at a given time $t$, we need to ensure that all particles present a $s$ at the instant $t$ are present at the element.

We define for each particle and for any couple of positions $s_1 < s_2$:
\begin{equation}
\hat{\beta}(s_1, s_2) = \frac{1}{c}\frac{s_2-s_1}{t(s_2) - t(s_1)}
\end{equation}

We choose an auxiliary value $\beta_\text{sim}$ such that at all times:
\begin{equation}
\hat{\beta}(s_1, s_2) < \beta_\text{sim} \quad \text{for all particles and any } s_1, s_2
\label{eq:betasim1}
\end{equation}
\begin{equation}
\hat{\beta}(s_1, s_2) > \frac{\beta_\text{sim}}{2}\quad \text{for all particles and any } s_1, s_2
\label{eq:betasim2}
\end{equation}

From $\beta_{sim}$ we define an auxiliary time interval $\Delta T$ as:
\begin{equation}
\Delta T = \frac{L}{\beta_\text{sim}c}
\label{eq:deltatdef}
\end{equation}


At each ``turn'' $n$ we want to simulate at any  collective element the time frame $F_n(s) $ given by:
\begin{equation}
F_n(s) = \left[ T_n(s) - \frac{\Delta T}{2}, T_n(s) + \frac{\Delta T}{2} \right]
\label{eq:framedef}
\end{equation}
where:
\begin{equation}
T_n(s) = n \Delta T + \frac{s}{\beta_\text{sim} c}
\label{eq:tndef}
\end{equation}
We can see that intervals are contiguous, hence at any locations over $N$ turns we are simulating a time interval of length $N \Delta T$.

From Eq\,\ref{eq:tndef} we can simply derive the following  relations, which will be useful in the following:
\begin{equation}
T_{n+1}(s) - T_{n}(s)  = \Delta T
\label{eq:tniter}
\end{equation}
\begin{equation}
T_{n}(s_2) - T_{n}(s_1)  = \frac{s_2 - s_1}{\beta_\text{sim} c}
\label{eq:frametransport}
\end{equation}

We also note that the condition $t(s) \in F_n(s)$ can be rewritten as:
\begin{equation}
- \frac{\Delta T}{2} < t - n \Delta T - \frac{s}{\beta_\text{sim} c} < \frac{\Delta T}{2}
\label{eq:frameineq}
\end{equation}


We can prove the following propositions (see Sec.\,\ref{seq:coasting_proofs}):

~\\
\textbf{Proposition 1:} If the time $t_k(s_1)$ defining the $k$-th arrival of a particle at location $s_1$ falls in the frame $F_n(s_1)$, then the particle arrives at location $s_2 > s_1$ either in the frame $F_n(s_2)$ or in the following frame $F_{n+1}(s_2)$. In symbols:
%\begin{equation}
%T_{n}(s_2) - \frac{\Delta T}{2}
%< t_k(s_1) <
%T_{n+1}(s_2) + \frac{\Delta T}{2}
%\end{equation}
\begin{equation}
t_k(s_1) \in F_n(s_1) \Rightarrow t_k(s_2) \in F_n(s_2) \cup  F_{n+1}(s_2) \quad \text{for any } s_1 < s_2 
\end{equation}
~\\
\textbf{Proposition 2:} If the time $t_k(s_2)$ defining the $k$-th arrival of a particle at location $s_2$ falls in the frame $F_n(s_2)$, then the time of $(k+1)$-th arrival at any location $s_1 < s_2$ falls in the frame $F_{n+1}(s_1)$ or in the following frame $F_{n+1}(s_1)$. In symbols:
\begin{equation}
t_k(s_2) \in F_n(s_2) \Rightarrow t_{k+1}(s_1) \in F_{n+1}(s_1) \cup  F_{n+2}(s_1) \quad \text{for any } s_1 < s_2 
\end{equation}
~\\
\textbf{Proposition 3:} If the time $t_k(L)$ defining the $k$-th arrival of a particle at the end of the line falls in the frame $F_n(L)$, than the time $t_{k+1}(0) = t_k(L)$ of the $(k+1)$-th arrival of the particle at $s=0$ falls in the interval 
$F_{n+1}(0)$. In symbols:
\begin{equation}
t_k(L) \in F_n(L) \Rightarrow t_{k+1}(0) \in F_{n+1}(0)
\end{equation}

\section{$\zeta$ definition and update}

For the tracking of coasting beams we define the longitudinal coordinate $\zeta$ as:
\begin{equation}
\zeta = s  - \beta_0 c (t - n\Delta T)
\end{equation}
where $s$ is the distance from the start of the line for the present turn, $t$ is the absolute time since the start of the simulation, $n$ is the index of the present simulated time frame.
% $k$ is the number of times the particle has passed at the end of the line.

We can write t in terms of $\zeta$ as:
\begin{equation}
 t = \frac{s}{\beta_0 c}  - \frac{\zeta}{\beta_0 c} + n\Delta T
 \label{eq:tfromzeta}
\end{equation}

With this definition the $\zeta$ coordinate needs to be updated each time the particle passes at the start of the line since across the $s=0$ we can write:
\begin{align}
\zeta^- &= L - \beta_0 c (t - n\Delta T)\\
\zeta^+ &= 0 - \beta_0 c (t - (n+1)\Delta T)
\end{align}
where $t$ is the time at which the particle passes zero (which is the same in both equations).

Combining the two equations we can write:
\begin{equation}
\zeta^+ = \zeta^- - (L - \beta_0 c \Delta T)
\end{equation}

In standard simulations for bunched beams the simulated frame is $\Delta T = L / (\beta_0 c)$ , hence the $\zeta$ coordinate is continuous. 

For coasting beams $\Delta T$ is given by Eq.\,\ref{eq:deltatdef}, hence the $\zeta$ needs to be updated at each turn using:
\begin{equation}
\boxed{
\zeta^+ = \zeta^- - L\left(1 - \frac{\beta_0}{\beta_\text{sim}} \right)
}
\end{equation}

We want to translate the condition Eq.\,\ref{eq:frameineq} into a condition on $\zeta$. Replacing Eq.\,\ref{eq:tfromzeta} into Eq.\,\ref{eq:frameineq} we obtain:
\begin{equation}
- \frac{\Delta T}{2} < \left(\frac{s}{\beta_0 c}  - \frac{\zeta}{\beta_0 c} + n \Delta T\right) - n \Delta T - \frac{s}{\beta_\text{sim} c} < \frac{\Delta T}{2}
\end{equation}
We change signs:
\begin{equation}
\frac{\Delta T}{2} > -\frac{s}{\beta_0 c}  + \frac{\zeta}{\beta_0 c} + \frac{s}{\beta_\text{sim} c} > -\frac{\Delta T}{2}
\end{equation}

We rearrange:
\begin{equation}
-\frac{\Delta T}{2} < -\frac{s}{\beta_0 c}  + \frac{\zeta}{\beta_0 c} + \frac{s}{\beta_\text{sim} c} < \frac{\Delta T}{2}
\end{equation}

\begin{equation}
-\beta_0 c \frac{\Delta T}{2} < \zeta - s \left(1 -  \frac{\beta_0}{\beta_\text{sim} }\right) < \beta_0 c\frac{\Delta T}{2}
\end{equation}
from which we can write:
\begin{equation}
\boxed{
t \in F_n(s) \quad \Leftrightarrow \quad
-\frac{\Delta \zeta}{2} < \zeta - s \left(1 -  \frac{\beta_0}{\beta_\text{sim} }\right) < \frac{\Delta \zeta}{2}
}
\label{eq:framezeta}
\end{equation}
where we have defined:
\begin{equation}
\boxed{
 \Delta \zeta = \beta_0 c \Delta T = \frac{\beta_0}{\beta_\text{sim}} L
}
\end{equation}

\section{Handling particles jumping to the following frame}

From Proposition 1 we know that during tracking particles will either stay in the present frame or ``jump'' to the following one.

From Eqs.\,\ref{eq:framedef} and \ref{eq:framezeta}, we know that the jump occurs when:
\begin{equation}
t > T_n(s) + \frac{\Delta T}{2}
~
\Leftrightarrow
~
\zeta < -\frac{\Delta \zeta}{2} + s\left(1-\frac{\beta_0}{\beta_{\text {sim }}}\right) 
 \end{equation}
 
When this condition is met, the particle tracking needs to be paused for the remainder of the frame $n$ (based on Proposition~1 it cannot go back to frame $n$) and, based on Proposition~2, its tracking needs to be resumed from the same location where the tracking was paused when tracking the following frame.
As the frame index is increased, $\zeta$ needs to be updated to preserve the time of arrival:
\begin{align}
\zeta_\text{before jump} &= s  - \beta_0 c \left( t - n\Delta T \right) \\
\zeta_\text{after jump} &= s  - \beta_0 c \left( t - (n+1) \Delta T \right)
\end{align}
from which we obtain:
\begin{align}
\boxed{
\zeta_\text{after jump} = \zeta_\text{before jump} + \beta_0 c \Delta T = \zeta_\text{before jump} + \Delta \zeta
}
\end{align}




\section{Proofs}
\label{seq:coasting_proofs}


We notice that from Eqs.\,\ref{eq:betasim1} and~\ref{eq:betasim2} we can write
\begin{equation}
\hat{\beta} < \beta_\text{sim} 
\quad \Rightarrow \quad
\frac{1}{\hat{\beta}} > \frac{1}{\beta_\text{sim}}
\quad \Rightarrow \quad
\left(
\frac{1}{\hat{\beta}} - \frac{1}{\beta_\text{sim}}
\right) > 0
\end{equation}
\begin{equation}
\hat{\beta} > \frac{\beta_\text{sim}}{2} 
\quad \Rightarrow \quad
\frac{1}{\hat{\beta}} < \frac{2}{\beta_\text{sim}}
\quad \Rightarrow \quad
\left(
\frac{1}{\hat{\beta}} - \frac{1}{\beta_\text{sim}}
\right) < \frac{1}{\beta_\text{sim}}
\end{equation}

Combining the two we obtain

\begin{equation}
0<\left(
\frac{1}{\hat{\beta}} - \frac{1}{\beta_\text{sim}}
\right) < \frac{1}{\beta_\text{sim}}
\label{eq:betaineq}
\end{equation}

\subsubsection*{Proof of proposition 1}
By definition of $\hat{\beta}$ we can write:
\begin{equation}
t_k(s_2) = t_k(s_1) + \frac{s_2 - s_1}{\hat{\beta} c} 
\label{eq:tk2fromtk1}
\end{equation}

By the hypothesis:
\begin{equation}
t_k(s_1) > T_n(s_1) - \frac{\Delta T}{2}
\end{equation}
hence:
\begin{equation}
t_k(s_2) > T_n(s_1) - \frac{\Delta T}{2} + \frac{s_2 - s_1}{\hat{\beta} c} 
\end{equation}
Using Eq.\,\ref{eq:frametransport} we can write:
\begin{equation}
t_k(s_2) > T_n(s_2) - \frac{s_2 - s_1}{{\beta}_\text{sim} c}  - \frac{\Delta T}{2} + \frac{s_2 - s_1}{\hat{\beta} c} 
\end{equation}
Rearranging:
\begin{equation}
t_k(s_2) > T_n(s_2) - \frac{\Delta T}{2} + \frac{s_2 - s_1}{c} \left(
\frac{1}{\hat{\beta}(s)} - \frac{1}{\beta_\text{sim}}
\right)
\end{equation}

Using Eq.\,\ref{eq:betaineq} and the fact that by hypotheses that $s_2 > s_1$, we know that the last term is positive. Therefore we can write:
\begin{equation}
t_k(s_2) > T_n(s_2) - \frac{\Delta T}{2}
\label{tk_s2_right}
\end{equation}

Similarly from the hypothesis we know:
\begin{equation}
t_k(s_1) < T_n(s_1) + \frac{\Delta T}{2}
\end{equation}

From Eq.\,\ref{eq:tk2fromtk1} we can write:
\begin{equation}
t_k(s_2) < T_n(s_1) + \frac{\Delta T}{2} + \frac{s_2 - s_1}{\hat{\beta} c} 
\end{equation}
Again, using Eq.\,\ref{eq:frametransport} we obtain:
\begin{equation}
t_k(s_2) < T_n(s_2) - \frac{s_2 - s_1}{{\beta}_\text{sim} c}  - \frac{\Delta T}{2} + \frac{s_2 - s_1}{\hat{\beta} c} 
\end{equation}
Rearranging:
\begin{equation}
t_k(s_2) < T_n(s_2) + \frac{\Delta T}{2} + \frac{s_2 - s_1}{c} \left(
\frac{1}{\hat{\beta}(s)} - \frac{1}{\beta_\text{sim}}
\right)
\end{equation}
Using Eq.\,\ref{eq:betaineq} we obtain:
\begin{equation}
t_k(s_2) < T_n(s_2) + \frac{\Delta T}{2} + \frac{s_2 - s_1}{\beta_\text{sim}c}
\label{eq:tk2smallerinterm}
\end{equation}
Using the fact that $s_2 - s_1 < L$ and Eq.\,\ref{eq:deltatdef} we can write:
\begin{equation}
\frac{s_2 - s_1}{\beta_\text{sim}c} < \frac{L}{\beta_\text{sim}c} = \Delta T
\end{equation}
Replacing in Eq.\,\ref{eq:tk2smallerinterm} we obtain:
\begin{equation}
t_k(s_2) < T_n(s_2) + \frac{\Delta T}{2} + \Delta T
\end{equation}
Using Eq.\,\ref{eq:tniter} we obtain:
\begin{equation}
t_k(s_2) < T_{n+1}(s_2) + \frac{\Delta T}{2}
\label{tk_s2_left}
\end{equation}
Combining Eq.\,\ref{tk_s2_right} and~\ref{tk_s2_right} we obtain:
\begin{equation}
T_{n}(s_2) - \frac{\Delta T}{2} < t_k(s_2) < T_{n+1}(s_2) + \frac{\Delta T}{2}
\label{tk_s2_left}
\end{equation}
which is what we wanted to prove.

\subsubsection*{Proof of proposition 2}

From the hypothesis:
\begin{equation}
t_{k}(s_2) > T_{n}(s_2) - \frac{\Delta T}{2}
\end{equation}
As $L>s_2$, using Proposition 1 we can write:
\begin{equation}
t_{k}(L) > T_{n}(L) - \frac{\Delta T}{2}
\end{equation}
Using Proposition 3 we can write:
\begin{equation}
t_{k+1}(0) > T_{n+1}(0) - \frac{\Delta T}{2}
\end{equation}
Using the fact that $s1>0$, we can apply again Proposition 1, obtaining:
\begin{equation}
t_{k+1}(s1) > T_{n+1}(1) - \frac{\Delta T}{2}
\end{equation}
which is what we wanted to prove.


\subsubsection*{Proof of proposition 3}

By definition:
\begin{equation}
t_{k+1}(0) = t_{k}(L)
\end{equation}

We know that $t_{k}(L) \in F_n(L)$ hence, from Eq.\,\ref{eq:framedef} we can write:
\begin{equation}
t_{k+1}(0) > T_n(L) - \frac{\Delta T}{2}
\end{equation}
From Eq.\,\ref{eq:tndef} we obtain:
\begin{equation}
t_{k+1}(0) > n \Delta T - \frac{\Delta T}{2} + \frac{L}{\beta_\text{sim}c}
\end{equation}
from Eq.\,\ref{eq:deltatdef} we get:
\begin{equation}
t_{k+1}(0) > (n+1) \Delta T - \frac{\Delta T}{2}
\end{equation}
and from Eq.\,\ref{eq:tndef} (with $s=0$) we obtain:\begin{equation}
t_{k+1}(0) > T_{n+1}(0) - \frac{\Delta T}{2}
\end{equation}
which is what we wanted to prove.


