\chapter{Space charge and beam-beam forces}

\section{Fields generated by a bunch of particles}



We assume that the bunch travels rigidly along $s$ with velocity $\beta_0 c$:
\begin{align}
&\rho\left(x, y, s, t\right) = \rho_0\left(x, y, s - \beta_0 ct\right) \label{rhorho0}\\
&\textbf{J}\left(x, y, s, t\right) = \beta_0c\, \rho_0\left(x, y, s - \beta_0 ct\right)  \hat{\textbf{i}}_s \label{JJ0}
\end{align}

We define an auxiliary variable $\zeta$ as the position along the bunch:
\begin{equation}
\zeta = s -\beta_0 c t \, .\label{zetadef}
\end{equation}
We call $K$ the lab reference frame in which we have defined all equations above, and we introduce a boosted frame $K'$ moving rigidly with the reference particle.
The coordinates in the two systems are related by a Lorentz transformation~\cite{jackson}:
\begin{align}
ct' &= \gamma_0 \left(ct -\beta_0 s \right)\label{lorA}\\
x' &= x\label{lorX}\\
y' &= y\label{lorY}\\
s' &= \gamma_0 \left(s -\beta_0 ct \right) = \gamma_0 \zeta\label{lorB}
\end{align}
The  corresponding inverse transformation is:
\begin{align}
ct &= \gamma_0 \left(ct' +\beta_0 s' \right)\label{lorC}\\
x &= x'\label{lorXinv}\\
y &= y'\label{lorYinv}\\
s &= \gamma_0 \left(s' +\beta_0 ct' \right)\label{lorD}
\end{align}



The quantities $\left(c \rho, J_x, J_y, J_s\right)$ form a Lorentz 4-vector and therefore they are transformed between $K$ and $K'$ by relationships similar to the Eqs.~\ref{lorA}-\ref{lorY}~\cite{jackson}:
\begin{align}
c\rho' \left(\textbf{r'}, t'\right)\ &= \gamma_0 \left[c \rho  \left(\textbf{r}\left(\textbf{r'}, t'\right), t\left(\textbf{r'}, t'\right)\right) -\beta_0 J_s \left(\textbf{r}\left(\textbf{r'}, t'\right), t\left(\textbf{r'}, t'\right)\right) \right]\label{lorrho}\\
J_s' \left(\textbf{r'}, t'\right)\ &= \gamma_0 \left[J_s  \left(\textbf{r}\left(\textbf{r'}, t'\right), t\left(\textbf{r'}, t'\right)\right) -\beta_0 c \rho \left(\textbf{r}\left(\textbf{r'}, t'\right), t\left(\textbf{r'}, t'\right)\right) \right]\label{lorjs}
\end{align}
where the transformations $\textbf{r}\left(\textbf{r'}, t'\right)$ and $t\left(\textbf{r'}, t'\right)$ are defined by Eqs.~\ref{lorC} and~\ref{lorD} respectively. The transverse components $J_x$ and $J_y$ of the current vector are invariant for our transformation, and are anyhow zero in our case.

Using Eq.\,\ref{JJ0} these become:
\begin{align}
\rho' \left(\textbf{r'}, t'\right)\ &= \frac{1}{\gamma_0}\rho\left(\textbf{r}\left(\textbf{r'}, t'\right), t\left(\textbf{r'}, t'\right)\right)
\\
J_s' \left(\textbf{r'}, t'\right)\ & = 0
\end{align}

Using Eqs.~\ref{rhorho0} and~\ref{lorC}-\ref{lorYinv}, we obtain:
\begin{equation}
\rho  \left(x', y', s(s', t'), t(s', t')\right) = \rho_0  \left(x', y', s(s', t') - \beta_0 c\,t(s', t')\right)
\end{equation}

From Eq.~\ref{lorB} we get:
\begin{equation}
s(s', t')- \beta_0 c\,t(s', t') = \frac{s'}{\gamma_0} 
\end{equation}
where the coordinate $t' $ has disappeared.

We can therefore write:
\begin{equation}
\rho' \left(x', y', s', t'\right) =   \frac{1}{\gamma_0} \rho_0  \left(x', y',  \frac{s'}{\gamma_0}\right)\label{rhoprimerho0}
\end{equation}

The electric potential in the bunch frame is the solution of Poisson's equation:

\begin{equation}
\frac{\partial^2 \phi'}{\partial x'^2} +  \frac{\partial^2 \phi'}{\partial y'^2}+  \frac{\partial^2 \phi'}{\partial s'^2}= -\frac{\rho' (x', y', s')}{\varepsilon_0}
\end{equation}

From Eq.~\ref{rhoprimerho0} we can write:
\begin{equation}
\frac{\partial^2 \phi'}{\partial x'^2} +  \frac{\partial^2 \phi'}{\partial y'^2}+  \frac{\partial^2 \phi'}{\partial s'^2}= -\frac{1}{\gamma_0\varepsilon_0}  \rho_0 \left(x', y', \frac{s'}{\gamma_0}\right)\label{poissrho0}
\end{equation}

We now make the substitution:
\begin{equation}
\zeta = \frac{s'}{\gamma_0} \label{subst}
\end{equation}
obtained from Eq.~\ref{lorB}, which allows to rewrite Eq.~\ref{poissrho0} as:
\begin{equation}
\frac{\partial^2 \phi'}{\partial x^2} +  \frac{\partial^2 \phi'}{\partial y^2}+  \frac{1}{\gamma_0^2}\frac{\partial^2 \phi'}{\partial \zeta^2}=  -\frac{1}{\gamma_0\varepsilon_0}{\rho}_0 \left(x, y,\zeta\right) \label{modifpoiss}
\end{equation}
Here we have dropped the ``$'$'' sign from $x$ and $y$ as these coordinates are unaffected by the Lorentz boost.




The quantities $\left( \frac{\phi}{c}, A_x, A_y, A_s\right)$ form a Lorentz 4-vector, so we can write:
\begin{align}
\phi &= \gamma_0 \left( {\phi'} +  \beta_0 c A'_s\right)\\
A_s &= A'_s +\beta_0 \frac{\phi'}{c}
\end{align}
In the bunch frame the charges are at rest therefore $A'_x=A'_y=A'_s=0$ therefore:
\begin{align}
\phi &= \gamma_0 \phi'\label{phiphip}\\
A_s &= \beta_0 \frac{\phi'}{c} =  \frac{\beta_0}{\gamma_0c}\phi
\end{align}

Combining Eq.\,\ref{phiphip} with Eq.\,\ref{modifpoiss} we obtain the equation in $\phi$:
\begin{equation}
\boxed{
\frac{\partial^2 \phi}{\partial x^2} +  \frac{\partial^2 \phi}{\partial y^2}+  \frac{1}{\gamma_0^2}\frac{\partial^2 \phi}{\partial \zeta^2}=  -\frac{1}{\varepsilon_0}{\rho}_0 \left(x, y,\zeta\right)} \label{modifpoiss_zeta}
\end{equation}

\subsection{2.5D approximation}
For large enough values of $\gamma_0$, Eq.~\ref{modifpoiss} can be approximated by:
\begin{equation}
\boxed{
\frac{\partial^2 \phi}{\partial x^2} +  \frac{\partial^2 \phi}{\partial y^2} = -\frac{1}{\varepsilon_0}{\rho}_0 \left(x, y,\zeta\right) }\label{2dpoiss}
\end{equation}
which means that we can solve a simple 2D problem for each beam slice (identified by its coordinate $\zeta$).


\subsection{Modulated 2D}
\label{sec:modulated2d}

Often the beam distribution can be factorized as:
\begin{equation}
\rho_0(x,y,\zeta) = q_0\lambda_0(\zeta)\rho_\perp(x,y) 
\end{equation}
where:
\begin{equation}
\int \rho_\perp(x,y) \,dx\,dy = 1
\end{equation}
and $\lambda_0(z)$ is therefore the bunch line density.

For a bunched beam:
\begin{equation}
\int \lambda_0(z) \,dz = N \label{eq:lamnorm}\\
\end{equation}
where $N$ is the bunch population.

In this case the potential can be factorized as:
\begin{equation}
\phi(x,y,\zeta) = q_0\lambda(\zeta)\phi_\perp(x,y) 
\label{eq:factorized2d}
\end{equation}

where $\phi_\perp(x,y)$ is the solution of the following 2D Poisson equation:
\begin{equation}
\frac{\partial^2 \phi_\perp}{\partial x^2} +  \frac{\partial^2 \phi_\perp}{\partial y^2} = -\frac{1}{\varepsilon_0}{\rho}_\perp \left(x, y\right) \label{2dpoisspeerp}
\end{equation}

%\section{Interaction time}
%In the lab frame the particle moves with speed $\beta$:
%\begin{equation}
%s(t) = \zeta_p +\beta c t
%\end{equation}
%
%In the frame $K'$, the kinematic equation of the particle can be obtained by replacing Eqs.~\ref{lorC} and~\ref{lorD} into Eq.~\ref{st_tau}:
%\begin{equation}
%\gamma_0 \left(s' +\beta_0 ct' \right) = \zeta_p +\beta \gamma_0 \left(ct' +\beta_0 s' \right)
%\end{equation}
%
%Solving for $s'$ we obtain:
%\begin{equation}
%s' = -\beta \gamma c \tau = \gamma \zeta\label{sprimezeta}
%\end{equation}
%Of course for the reference particle we obtain $s' = 0$.
%We observe that \textbf{beam particles are at rest in the reference frame $K'$ and that the distance between them is increased by a factor $\gamma$ with respect to the lab frame $K$}.

%\section{Transverse kick on the beam particle}
%
%We now evaluate the change on the transverse momentum for a beam particle defined in the lab frame by its transverse coordinates $x$ and $y$ and by its delay $\tau$ with respect to the reference particle (or equivalently by its $\zeta$ coordinate, defined by Eq.~\ref{zetadef}).
%
%We have seen that in the frame $K'$ the particle is at rest and has longitudinal coordinate $s' = \gamma \zeta$ (see Eq.~\ref{sprimezeta}). 
%The x' component of the electric field $\textbf{E}'$ acting on P is given by (see Eqs.~\ref{potential} and~\ref{phiphiprime}):
%\begin{equation}
%E'_x = -\frac{\partial \phi'}{\partial x} = -\frac{1}{\gamma_0}\frac{\partial \phi}{\partial x} \label{Exprime}
%\end{equation}
%Again, we have dropped the ``$'$'' sign from $x$ and $y$ as these coordinates are unaffected by the Lorentz boost.
%
%
%The change in the x component of the momentum, which is an invariant for our Lorentz transformation, is given by :
%\begin{equation}
%\Delta P_x = \Delta P'_x = qE'_x T'
%\end{equation}
%
%Using Eqs.~\ref{Exprime} and~\ref{Tprime} we can write:
%\begin{equation}
%\Delta P_x = -\frac{qL}{\beta c} \frac{\partial \phi}{\partial x}\left(x, y,\zeta\right)
%\end{equation}
%
%Normalizing to the momentum of the reference particle:
%
%\begin{equation}
%\Delta p_x = \frac{\Delta P_x} {P}= -\frac{qL}{ m\gamma\beta^2 c^2} \frac{\partial \phi}{\partial x}\left(x, y,\zeta\right)\label{dpx}
%\end{equation}
%
%Similarly, for the $y$-direction we can write: 
%\begin{equation}
%\Delta p_y = \frac{\Delta P_y} {P}= -\frac{qL}{ m\gamma\beta^2 c^2} \frac{\partial \phi}{\partial y}\left(x, y,\zeta\right)\label{dpy}
%\end{equation}

\section{Lorentz force}
We now compute the Lorentz force on the particles moving in the longitudinal directions, including particles of the bunch itself (space charge forces) and particles of a colliding bunch moving in the opposite directions (beam-beam forces).
The angles of such test particles are neglected as done in the usual thin-lens approximation. Therefore the velocity of a test particle can be written as:
\begin{equation}
\textbf{v} = \beta c\, \hat{\textbf{i}}_s
\end{equation}

The Lorentz force can be written as:
\begin{equation}
\begin{split}
\textbf{F} &=q \left( -\nabla \phi -\frac{\partial \textbf{A}}{\partial t}
 + \beta c \ \hat{\textbf{i}}_s \times {\left(\nabla \times \textbf{A} \right)} \right)\\
 &=q \left( -\nabla \phi -\frac{\beta_0}{\gamma_0 c}\frac{\partial \phi}{\partial t}\hat{\textbf{i}}_s
 + \beta c \ \hat{\textbf{i}}_s \times {\left(\nabla \times \textbf{A} \right)} \right)
 \end{split}
\end{equation}

We compute the vector product:
\begin{align}
\begin{split}
\hat{\textbf{i}}_s \times \left(\nabla \times \textbf{A}\right) &= \left(\frac{\partial A_s}{\partial x} - \frac{\partial A_x}{\partial s} \right) \hat{\textbf{i}}_x + \left(\frac{\partial A_s}{\partial y} - \frac{\partial A_y}{\partial s} \right) \hat{\textbf{i}}_y\\
 &= \left(\frac{\partial A_s}{\partial x} - \frac{\partial A_x}{\partial s} \right) \hat{\textbf{i}}_x + \left(\frac{\partial A_s}{\partial y} - \frac{\partial A_y}{\partial s} \right) \hat{\textbf{i}}_y + \underbrace{\left(\frac{\partial A_s}{\partial s} - \frac{\partial A_s}{\partial s} \right)}_{=0} \hat{\textbf{i}}_s\\
 &= \nabla A_s - \frac{\partial \textbf{A}}{\partial s} 
\end{split} 
\end{align}

We replace:
\begin{equation}
\textbf{F} 
=q \left( -\nabla \phi -\frac{\beta_0}{\gamma_0 c}\frac{\partial \phi}{\partial t}\hat{\textbf{i}}_s
 + \beta  \beta_0\nabla \phi - \frac{\beta \beta_0}{\gamma_0} \frac{\partial \phi}{\partial s} \hat{\textbf{i}}_s
  \right)
\end{equation}

The potentials will have the same form as the sources (this can be shown explicitly using the Lorentz transformations):
\begin{equation}
\phi(x, y, s, t) = \phi\left(x, y, t - \frac{s}{\beta_0 c}\right)
\end{equation}
For a function in this form we can write:
\begin{equation}
 \frac{\partial \phi}{\partial s} = 
\frac{\partial}{\partial\zeta} 
 = -\frac{1}{\beta_0 c}\frac{\partial \phi}{\partial t} \label{derder}
\end{equation}


obtaining:
\begin{equation}
\textbf{F} 
=q \left( -\nabla \phi +\frac{\beta_0^2}{\gamma_0}\frac{\partial \phi}{\partial \zeta}\hat{\textbf{i}}_s
 + \beta  \beta_0\nabla \phi - \frac{\beta \beta_0}{\gamma_0} \frac{\partial \phi}{\partial \zeta} \hat{\textbf{i}}_s
  \right)
\end{equation}


Reorganizing:
\begin{equation}
\textbf{F} 
=  -q(1-\beta  \beta_0)\nabla \phi -\frac{\beta_0(\beta-\beta_0)}{\gamma_0}\frac{\partial \phi}{\partial \zeta}\hat{\textbf{i}}_s
\end{equation}

Writing the dependencies explicitly:
\begin{align}
F_x(x, y, \zeta(t)) &=  -q(1-\beta  \beta_0) \frac{\partial \phi}{\partial x}(x, y, \zeta(t))\label{eq:forcex}\\
F_y(x, y, \zeta(t)) &=  -q(1-\beta  \beta_0) \frac{\partial \phi}{\partial y}(x, y, \zeta(t))\label{eq:forcey}\\
F_z(x, y, \zeta(t)) &=  -q\left(1-\beta  \beta_0 -\frac{\beta_0(\beta-\beta_0)}{\gamma_0}\right) \frac{\partial \phi}{\partial \zeta}(x, y, \zeta(t))\label{eq:forcez}
\end{align}
where $\zeta(t)$ is the position of the particle within the bunch.

\section{Space charge}

Over the single interaction we neglect the particle slippage\footnote{In any case one would need to take into account also the dispersion in order to have the right slippage.}:
\begin{align}
&\beta = \beta_0\\
&\zeta(t) = \zeta
\end{align}

This gives the following simplification of Eqs.\,\eqref{eq:forcex}\,-\,\eqref{eq:forcez}:
\begin{align}
F_x(x, y, \zeta) &=  -q(1-\beta_0^2) \frac{\partial \phi}{\partial x}(x, y, \zeta)\\
F_y(x, y, \zeta) &=  -q(1-\beta_0^2) \frac{\partial \phi}{\partial y}(x, y, \zeta)\\
F_z(x, y, \zeta) &=  -q (1-\beta_0^2) \frac{\partial \phi}{\partial \zeta}(x, y, \zeta)
\end{align}

In this way the force over the single interaction becomes independent of time and therefore we can compute the kicks simply as:
\begin{equation}
\Delta \textbf{P} = \frac{L}{\beta_0 c}\textbf{F} 
\end{equation}
where $L$ is the portion of the machine on which we want to compute the e-cloud interaction.

The kicks on the normalized momenta can be expressed as (recalling that $P_0=m_0\beta_0\gamma_0c$):

\begin{align}
&\boxed{
\Delta p_x = \frac{m_0}{m}\frac{\Delta P_x} {P_0}= -\frac{qL (1-\beta_0^2)}{ m\gamma_0\beta_0^2 c^2} \frac{\partial \phi}{\partial x}\left(x, y,\zeta\right)}\label{dpx}\\
&\boxed{
\Delta p_y = \frac{m_0}{m}\frac{\Delta P_y} {P_0}= -\frac{qL (1-\beta_0^2)}{ m\gamma_0\beta_0^2 c^2} \frac{\partial \phi}{\partial y}\left(x, y,\zeta\right)}\label{dpy}\\
&\boxed{
\Delta \delta \simeq \Delta p_z = \frac{m_0}{m}\frac{\Delta P_z} {P_0}= -\frac{qL (1-\beta_0^2)}{ m\gamma_0\beta_0^2 c^2} \frac{\partial \phi}{\partial \zeta}\left(x, y,\zeta\right)}
\label{dpz}
\end{align}

If the beam includes particles of different species (tracking of fragments), note that here $q$ and $m$ refer to the individual particle while $m_0$ is the mass of the reference particle.



In the modulated 2D case (see Sec.\,\ref{sec:modulated2d} and in particular Eq.\,\ref{eq:factorized2d}), the kick can be expressed as:
\begin{align}
&\boxed{
\Delta p_x = \frac{m_0}{m}\frac{\Delta P_x} {P_0}= -\frac{qq_0L (1-\beta_0^2)}{ m\gamma_0\beta_0^2 c^2} \,\lambda_0(\zeta)\,\frac{\partial {\phi_\perp}}{\partial x}\left(x, y\right)}\label{dpx_mod}\\
&\boxed{
\Delta p_y = \frac{m_0}{m}\frac{\Delta P_y} {P_0}= -\frac{qq_0L (1-\beta_0^2)}{ m\gamma_0\beta_0^2 c^2} \,\lambda_0(\zeta)\, \frac{\partial{\phi_\perp}}{\partial y}\left(x, y\right)}\label{dpy_mod}\\
&\boxed{
\Delta \delta \simeq \Delta p_z = \frac{ m_0} {m}\frac{\Delta P_z} {P_0}= -\frac{qq_0L (1-\beta_0^2)}{ m\gamma_0\beta_0^2 c^2} \,\frac{d\lambda_0}{d\zeta}(\zeta)\,{\phi_\perp}\left(x, y\right)}\label{dpz_mod}
\end{align}

In some cases, for example in the case of transversely Gaussian beams, an analytic closed form exists for the quantities $\frac{\partial \phi_\perp}{\partial x}$ and $\frac{\partial \phi_\perp}{\partial y}$ but not for the potential $\phi_\perp$ itself. In those cases, in order to compute the longitudinal kick, it is possible to obtain a function $\phi_\perp (x,y)$ generating the same transverse kicks by computing numerically the integral:

\begin{equation}
\phi_\perp (x,y) = \int_{(0, 0)}^{(x, y)} 
\left(
\frac{\partial \phi_\perp}{\partial x} \mathbf{\hat{i}}_x
+
\frac{\partial \phi_\perp}{\partial y} \mathbf{\hat{i}}_y
\right)
\cdot
d\mathbf{r'}
\end{equation}



\section{Beam-beam interaction (4D model)}

We consider a test particle moving in the opposite direction with velocity:
\begin{align}
\textbf{v}_W = -\beta_{0W} c\, \hat{\textbf{i}}_s\\
s_W(t) = -\beta_{0W} ct
\end{align}
Equations\,\eqref{eq:forcex}\,-\,\eqref{eq:forcez} become:
\begin{align}
F_x(x, y, \zeta_W(t)) &=  -q(1+\beta_{0W}  \beta_{0S}) \frac{\partial \phi}{\partial x}(x, y, \zeta_W(t)) \label{eq:bbfgenx}\\
F_y(x, y, \zeta_W(t)) &=  -q(1+\beta_{0W}  \beta_{0S}) \frac{\partial \phi}{\partial y}(x, y, \zeta_W(t))\label{eq:bbfgeny}\\
F_z(x, y, \zeta_W(t)) &=  -q\left(1+\beta_{0W}  \beta_{0S} -\frac{\beta_{0S}(\beta_{0W}+\beta_{0S})}{\gamma_0}\right) \frac{\partial \phi}{\partial \zeta}(x, y, \zeta_W(t))\label{eq:bbfgenz}
\end{align}
where we have used the subscript $S$ (strong) for the bunch generating the fields, and the subscript $W$ (weak) for the test particle. 

$\zeta_W(t)$ is the position of the test particle within the bunch generating the fields: 
\begin{equation}
\zeta_W(t)= s_W(t) -\beta_{0S} c t  = -(\beta_{0W}+\beta_{0S})ct
\label{eq:zetaw}
\end{equation}

In modulated-2D case (Eq.\,\ref{eq:factorized2d}), Eqs.\,\eqref{eq:bbfgenx}\,-\,\eqref{eq:bbfgeny} become:
\begin{align}
F_x(x, y, \zeta_W(t)) &=  -q q_{0S} (1+\beta_{0W}  \beta_{0S})
\lambda_{0S}(\zeta_W(t))
 \frac{\partial \phi_\perp}{\partial x}(x, y ) \\
F_y(x, y, \zeta_W(t)) &=  -qq_{0S}  (1+\beta_{0W}  \beta_{0S})
\lambda_{0S}(\zeta_W(t))
 \frac{\partial \phi_\perp}{\partial y}(x, y ) \\
F_z(x, y, \zeta_W(t)) &=  -qq_{0S}\left(1+\beta_{0W}  \beta_{0S} -\frac{\beta_{0S}(\beta_{0W}+\beta_{0S})}{\gamma_0}\right) \frac{d \lambda_{0S}}{d \zeta}(\zeta_W(t)) \phi_\perp(x, y)
\end{align}

The change in momentum for the test particle is given by:
\begin{equation}
\Delta \textbf{P} = \int_{-\infty}^{+\infty} \textbf{F}(t) \,dt
\end{equation}
Therefore:
\begin{align}
\Delta P_x(x, y, \zeta_W(t)) &=  -qq_{0S} N_S (1+\beta_{0W}  \beta_{0s})
\frac{\partial \phi_\perp}{\partial x}(x, y ) \int_{-\infty}^{+\infty}\lambda_{0S}(\zeta_W(t)) \,dt\\
\Delta P_y(x, y, \zeta_W(t)) &=  -qq_{0S} N_S (1+\beta_{0W}  \beta_{0s})
 \frac{\partial \phi_\perp}{\partial y}(x, y ) \int_{-\infty}^{+\infty}\lambda_{0S}(\zeta_W(t)) \,dt\\
\Delta P_z(x, y, \zeta_W(t)) &=  -qq_{0S}\left(1+\beta_{0W}  \beta_{0S} -\frac{\beta_{0S}(\beta_{0W}+\beta_{0S})}{\gamma_0}\right) \phi_\perp(x, y) \int_{-\infty}^{+\infty}\frac{d \lambda_{0S}}{d \zeta}(\zeta_W(t)) \, dt
\end{align}

Using Eq.\,\eqref{eq:zetaw} and Eq.\,\eqref{eq:lamnorm} we can write:
\begin{equation}
\int_{-\infty}^{+\infty}\lambda_{0S}(\zeta_W(t)) \,dt 
=\frac{1}{(\beta_{0W}+\beta_{0S})c}\int_{-\infty}^{+\infty}\lambda_{0S}(\zeta) \,d\zeta = \frac{N_S}{(\beta_{0W}+\beta_{0S})c}
\end{equation}

Similarly, for a bunched beam:
\begin{equation}
\int_{-\infty}^{+\infty}
\frac{d \lambda_{0S}}{d \zeta}(\zeta_W(t)) \,dt 
=\frac{1}{(\beta_{0W}+\beta_{0S})c}\int_{-\infty}^{+\infty}\frac{d \lambda_{0S}}{d \zeta} \,d\zeta = \frac{ \lambda_{0S}(+\infty)-\lambda_{0S}(-\infty)}{(\beta_{0W}+\beta_{0S})c} = 0
\end{equation}

From which we can write:
\begin{align}
&\boxed{
\Delta p_x = \frac{m_0}{m}\frac{\Delta P_x} {P_0}= -\frac{qq_{0S} N_S 
}{m\beta_{0W}\gamma_{0W}c^2}
\frac{(1+\beta_{0W}  \beta_{0s})}{(\beta_{0W}+\beta_{0S})}
\frac{\partial \phi_\perp}{\partial x}(x, y )}\\
&\boxed{
\Delta p_y = \frac{m_0}{m}\frac{\Delta P_y} {P_0}= -\frac{qq_{0S} N_S 
}{m\beta_{0W}\gamma_{0W}c^2}
\frac{(1+\beta_{0W}  \beta_{0s})}{(\beta_{0W}+\beta_{0S})}
\frac{\partial \phi_\perp}{\partial y}(x, y )}\\
&\boxed{
\Delta p_z = \frac{m_0}{m}\frac{\Delta P_z} {P_0}=0}
\end{align}

\section{Longitudinal profiles}

\subsection{Gaussian profile}

The profile is in the form:
\begin{equation}
\lambda_{0}(z)=\frac{N}{\sqrt{2 \pi} \sigma} e^{-\frac{(z-z_0)^{2}}{2 \sigma^{2}}}
\end{equation}


\subsection{q-Gaussian}

The profile is in the form:
\begin{equation}
\lambda_0(z)=\frac{N\sqrt{\beta}}{C_{q}} e_{q}\left(-\beta (z-z_0)^{2}\right)
\end{equation}
where $e_q$ is the q-exponential function:
\begin{equation}
e_{q}(x)=[1+(1-q) x]_{+}^{\frac{1}{1-q}}
\end{equation}
$C_q$ is a normalization factor dependent on $q$ alone:
\begin{equation}
C_{q}=\frac{\sqrt{\pi} \Gamma\left(\frac{3-q}{2(q-1)}\right)}{\sqrt{q-1} \Gamma\left(\frac{1}{q-1}\right)}
\end{equation}

The parameter beta defines the standard deviation of the distribution:

\begin{equation}
\sigma = \sqrt{\frac{1}{\beta(5-3 q)}} \iff \beta ={\frac{1}{\sigma^2(5-3 q)}}
\end{equation}



These expressions are valid for values of the parameter $q$ in the range of interest:
\begin{equation}
1<q<\frac{5}{3}
\end{equation}

In general the q-Gaussian is defined outside this range, but for smaller values it has limited support (not of interest) and for larger values it has an undefined standard deviation.

\section{Beam-beam interaction (6D model, Hirata method)}

This chapter describes in detail the numerical method used for the simulation of beam-beam interactions in the weak-strong framework using the ``Synchro Beam Mapping'' 
approach \cite{hirata, hirata_crossing_angle}.  This allows correctly modeling the coupling introduced by beam-beam between the longitudinal and transverse planes. The goal of this document is  in particular to provide in a compact, complete and self-consistent manner, the set of equations that are needed for the implementation in a numerical code. 
Complementary information can be found in \cite{bb6dslides}, including graphical representations of the procedure presented in this note and several validation tests.

The effect of a ``crossing angle'' in an arbitrary ``crossing plane'' with respect to the assigned reference frame is taken into account with a suitable coordinate transformation following the approach described in \cite{hirata, beam_beam}. The employed description of the strong beam allows the correct inclusion of the hour-glass effect as well as the linear coupling at the interaction point, following the treatment presented in \cite{beam_beam}. 


If not differently stated in an explicit way in the following, all coordinates are given in the reference system defined by the closed orbit of the weak beam, which is traveling with positive speed along the $s$ direction. The Interaction Point (IP) is located at $s$=0 and the crossing plane is defined by as the angle that the strong beam forms with the $s$-axis. In the presence of an offset between the beams (separation), the orientation of the reference system is defined by the closed orbit of the weak beam and the system is centered at the IP location as defined for the strong beam. Therefore the strong beam passes always through the origin of the reference frame.


\subsection{Direct Lorentz boost (for the weak beam)}
\label{sec:directboost}

We want to transform the coordinates by moving to a Lorentz boosted frame in which the collision is head-on (i.e. $p_x=p_y=0$ for the strong beam and for the reference particle of the weak beam). We call $\phi$ the half crossing angle and $\alpha$ the angle that the crossing plane makes with respect to the $x-z$ plane. For this purpose, we perform a transformation which actually includes four operations (more details can be found in Appendix~\ref{app:boost} and in~\cite{bb6dslides, beam_beam}):
\begin{itemize}
\item Transform the accelerator positions and momenta into Cartesian coordinates (which can then be Lorentz boosted);
\item Rotate particle coordinates to the ``barycentric'' reference frame;
\item Perform the Lorentz boost;
\item Drift all the particles back to $s=0$ (as not all particles with $s=0$ are fixed points of the transformation, and we are tracking with respect to $s$ and not with respect to time).
\end{itemize}

We name the original accelerator coordinates (as defined in the SixTrack Physics Manual \cite{sixtracksite}):
\begin{equation}
\left(x, p_x, y, p_y, \sigma, \delta\right)
\end{equation}
and the transformed coordinates:
\begin{equation}
\left(x^*, p_x^*, y^*, p_y^*, \sigma^*, \delta^*\right)
\end{equation}

We start by computing the drift Hamiltonian in the original coordinates (we are doing a Lorentz transformation, therefore constants matter as we are assuming that $h$ is the total energy of the particle):
\begin{equation}
h = \delta + 1 -\sqrt{\left(1+\delta\right)^2-p_x^2-p_y^2}
\end{equation}

We transform the momenta:
\begin{align}
p_x^* &= \frac{p_x}{\cos \phi} - h \cos \alpha \frac{\tan \phi}{\cos \phi} \label{eq:px}\\ 
p_y^* & = \frac{p_y}{\cos \phi} - h \sin \alpha \frac{\tan \phi}{\cos \phi}\\ 
\delta^* & = \delta -p_x \cos \alpha \tan \phi - p_y \sin \alpha \tan \phi +h \tan^2 \phi \label{eq:delta}
\end{align}

In order to calculate the angles in the transformed frame, we evaluate:
\begin{equation}
p^*_z  =  \sqrt{\left(1+\delta^*\right)^2-{p_x^*}^2-{p_y^*}^2}
\end{equation}

We can now evaluate the following derivatives of the transformed Hamiltonian (from Hamilton's equations it can be easily seen that these are the angles in the boosted frame):

\begin{align}
h^*_x &= \frac{\partial h^*}{\partial p^*_x} = \frac{p^*_x}{p^*_z}\\
h^*_y &= \frac{\partial h^*}{\partial p^*_y} = \frac{p^*_y}{p^*_z}\\
h^*_\sigma &= \frac{\partial h^*}{\partial \delta} = 1-\frac{\delta^*+1}{p^*_z}
\end{align}

These can be used to build the following matrix:

\begin{equation}
L  =\left( \begin{matrix}

\left( 1 + h^*_x \cos \alpha \sin \phi\right) & h^*_x \sin \alpha \sin \phi & \cos \alpha \tan \phi \\
h_y^* \cos \alpha \sin \phi & \left( 1 + h^*_y \sin \alpha \sin \phi\right) & \sin \alpha \tan \phi\\
h_\sigma^* \cos \alpha \sin \phi & h_\sigma^* \sin \alpha \sin \phi  & \frac{1}{\cos \phi}\\
\end{matrix} \right)
\label{eq:Lmat}
\end{equation}

which can then be used to transform the test-particle positions:

\begin{equation}
\left( \begin{matrix}
x^*\\
y^*\\
\sigma^*
\end{matrix}\right) = 
L 
\left( \begin{matrix}
x\\
y\\
\sigma
\end{matrix}\right)
\end{equation}


\subsection{Synchro-beam mapping}

Following the approach introduced in~\cite{hirata}, the strong beam is sliced along z. A common approach is to use constant-charge slices (see Appendix~\ref{app:slicing}). For each particle in the weak beam and for each slice in the strong beam we perform the following.
~\\

We identify the position of the Collision Point (CP):

\begin{equation}
S = \frac{\sigma^*-\sigma^*_\textrm{sl}}{2}
\label{eq:sdef}
\end{equation}

Here $\sigma^*$ is defined in the reference system of the weak beam ( $\sigma^*>0$ for particles at the head of the weak bunch) while $\sigma^*_\textrm{sl}$ is defined in the reference system of the strong beam ( $\sigma^*_\textrm{sl}>0$ for particles at the head of the strong bunch). $S$ is the coordinate of the collision point \underline{in the reference system of the weak beam} (from Eq.~\ref{eq:sdef}, we can see that particles at the head of the weak bunch, collide with particles at the tail of the strong bunch at $S>0$). 
~\\

\textit{N.B. Here we are making an approximation since we are assuming that particles are moving at the speed of light along z independently on their angles. This means that the presented approach works only for small particle angles. It is for this reason that we need to Lorentz boost to get rid of the crossing angle and we cannot just move to the reference of the strong beam using a rotation (in this case the weak beam would have large angles).}

We now evaluate the transverse position of the particle at the CP, with respect to the centroid of the slice, taking into account the particle angles :
\begin{align}
\overline{x}^* &= x^* + p^*_x S - (x^*_\textrm{sl} - p^*_{x, \textrm{sl}} S)\\
\overline{y}^* &= y^* + p^*_y S - (y^*_\textrm{sl} - p^*_{y, \textrm{sl}} S)
\end{align}
Here $x^*_\textrm{sl}$, $y^*_\textrm{sl}$, $p^*_{x, \textrm{sl}}$ and $p^*_{y, \textrm{sl}}$  are defined \underline{in the coordinate system of the weak beam}. The momenta of the strong slice appear with a negative sign since in the weak frame the strong slice is travelling "backwards".

\subsection{Propagation of the strong beam to the collision point}

The distribution of the strong beam in the transverse phase-space can be written using the $\Sigma$-matrix~\cite{wolski2014beam}:
\begin{equation}
f(\eta) = f_0 e^{{-\eta}^{\mathrm{T}} \Sigma^{-1}\eta}
\end{equation}

where:
\begin{equation}
\eta = \left(
\begin{matrix}
x\\
p_x\\
y\\
p_y
\end{matrix}
\right)
\end{equation}

Points having same phase space density lie on hyper-elliptic manifolds defined by the equation:
\begin{equation}
\eta^\mathrm{T}\Sigma^{-1}\eta = \mathrm{const.}
\label{eqellipse}
\end{equation}

Further considerations on the $\Sigma$-matrix can be found in Appendix~\ref{app:sigma}.

We transform the $\Sigma$-matrix at the Interaction Point to take into account the Lorentz Boost:
\begin{align}
\Sigma^{*0}_{11} &= \Sigma_{11} ^0\\
\Sigma^{*0}_{12} &= \Sigma_{12} ^0/\cos \phi\\
\Sigma^{*0}_{13} &= \Sigma_{13} ^0\\
\Sigma^{*0}_{14} &= \Sigma_{14} ^0/\cos \phi\\
\Sigma^{*0}_{22} &= \Sigma_{22} ^0/\cos^2 \phi\\
\Sigma^{*0}_{23} &= \Sigma_{23} ^0/\cos \phi\\
\Sigma^{*0}_{24} &= \Sigma_{24} ^0/\cos^2 \phi\\
\Sigma^{*0}_{33} &= \Sigma_{33} ^0\\
\Sigma^{*0}_{34} &= \Sigma_{34} ^0/\cos \phi\\
\Sigma^{*0}_{44} &= \Sigma_{44} ^0/\cos^2 \phi\\
\end{align}

We transport the position part of the boosted $\Sigma$-matrix to the CP (here we are taking into account hourglass effect, assuming that we are in a drift space):
\begin{align}
\Sigma^*_{11} &= \Sigma_{11} ^{*0}+2 \Sigma_{12} ^{*0} S+\Sigma_{22} ^{*0} S^2 \label{eq:TranspSig11}\\
\Sigma^*_{33} &= \Sigma_{33} ^{*0}+2 \Sigma_{34} ^{*0} S +\Sigma_{44} ^{*0} S^2\label{eq:TranspSig33}\\
\Sigma^*_{13} &= \Sigma_{13} ^{*0}+\left(\Sigma_{14} ^{*0} +\Sigma_{23}^{*0}\right) S +\Sigma_{24} ^{*0} S^2\label{eq:TranspSig13}
\end{align}

The $\Sigma$-matrix is given \underline{in the reference system of the weak beam}.
~\\
For singular cases we will also need to transport the other terms:
\begin{align}
\Sigma^*_{12} &= \Sigma_{12} ^{*0}+\Sigma_{22} ^{*0}S\\
\Sigma^*_{14} &= \Sigma_{14} ^{*0}+\Sigma_{24} ^{*0}S\\
\Sigma^*_{22} &= \Sigma_{22} ^{*0}\\
\Sigma^*_{23} &= \Sigma_{23} ^{*0}+\Sigma_{24} ^{*0}S\\
\Sigma^*_{24} &= \Sigma_{24} ^{*0}\\
\Sigma^*_{34} &= \Sigma_{34} ^{*0}+\Sigma_{44} ^{*0}S\\
\Sigma^*_{44} &= \Sigma_{44} ^{*0}
\end{align}



We introduce the following three auxiliary quantities:
\begin{align}
R\left(S \right)  &= \Sigma^*_{11} - \Sigma^*_{33}\\
W\left(S\right) &= \Sigma^*_{11} + \Sigma^*_{33}\\
T\left( S \right) &= {R^2 + 4 {\Sigma^*_{13}}^2 } \label{eq:Tfun}
\end{align}

The following derivatives will be needed in the following:
\begin{align}
\frac{\partial R}{\partial S}  &= 2\left(\Sigma_{12} ^0 - \Sigma_{34}^0\right) + 2S\left(\Sigma_{22} ^0 - \Sigma_{44}^0\right) \\ 
\frac{\partial W}{\partial S} &= 2\left(\Sigma_{12} ^0 + \Sigma_{34}^0\right) + 2S\left(\Sigma_{22} ^0 + \Sigma_{44}^0\right) \\
\frac{\partial \Sigma^*_{13}}{\partial S}  &= \Sigma_{14} ^0 + \Sigma_{23} ^0 + 2\Sigma_{24} ^0 S\\
\frac{\partial T}{\partial S}  &= 2R \frac{\partial R}{\partial  S} +8  \Sigma^*_{13} \frac{\partial \Sigma^*_{13}}{\partial  S}
\end{align}

We will now compute, at the location of the CP, the coupling angle $\theta$, defining a reference frame in which the beam is decoupled.
We will call $\hat{x}$ and $\hat{y}$ the coordinates in the decoupled frame and  $\hat{\Sigma}^*_{11}$,  $\hat{\Sigma}^*_{33}$ the corresponding squared beam sizes. The angle $\theta$ is defined as the angle between the $\hat{x}$-axis and the $x$-axis.

These quantities can be found by diagonalizing the $x-y$ block of the $\Sigma$-matrix.
We will make determination choices (Eqs.~\ref{eq:cos2theta}, \ref{eq:sincostheta} and \ref{eq:sigmashat}) so that the set $(\theta, \hat{\Sigma}^*_{11}, \hat{\Sigma}^*_{33})$ is uniquely defined and the coupling angle $\theta$ lies in the interval:
\begin{align}
-\frac{\pi}{4}<\theta<\frac{\pi}{4}
\end{align}

Different cases need to be treated separately:
~\\

\textbf{\underline{Case T>0, $\left|\Sigma^*_{13}\right|$>0}}
~\\

We evaluate the coupling angle at the position of the CP in the boosted frame:
\begin{equation}
\cos 2\theta = \textrm{sgn} \left(\Sigma^*_{11} - \Sigma^*_{33}\right)\frac{\Sigma^*_{11} - \Sigma^*_{33}}{\sqrt{\left(\Sigma^*_{11} - \Sigma^*_{33}\right)^2 + 4 {\Sigma^*_{13}}^2 }}
\end{equation}

Or more synthetically:
\begin{equation}
\cos 2\theta = \textrm{sgn}(R)\frac{R}{\sqrt{T}}
\label{eq:cos2theta}
\end{equation}

In the following we will need also the derivative of this quantity:
\begin{equation}
\frac{\partial }{\partial S} \left[ \cos 2\theta \right] =  \textrm{sgn}(R)
\left(\frac{\partial R}{\partial S} \frac{1}{\sqrt{T}}-\frac{R}{2\left( \sqrt{T}\right)^3}\frac{\partial T}{\partial S}\right)
\end{equation}

It can be proved that \cite{beam_beam}:
\begin{align}
\cos \theta &= \sqrt{\frac{1}{2}\left(1 + \cos 2\theta \right)}\\
\sin \theta &= \textrm{sgn}(R)\textrm{sgn}(\Sigma^*_{13}) \sqrt{\frac{1}{2}\left(1 - \cos 2\theta \right)}
\label{eq:sincostheta}
\end{align}
~\\

The corresponding derivatives are given by:
\begin{align}
\frac{\partial }{\partial S} \cos \theta =& \frac{1}{4 \cos \theta} \frac{\partial }{\partial S} \cos 2\theta \\
\frac{\partial }{\partial S} \sin \theta =& -\frac{1}{4 \sin \theta} \frac{\partial }{\partial S} \cos 2\theta \label{eq:dS_sintheta}
\end{align}

The squared beam sizes in the rotated (un-coupled) boosted frame are given by:

\begin{align}
\hat{\Sigma}^*_{11} &= \frac{1}{2}\left[ \left(\Sigma^*_{11} + \Sigma^*_{33}\right) +\textrm{sgn}\left(\Sigma^*_{11} - \Sigma^*_{33}\right)\sqrt{\left(\Sigma^*_{11} - \Sigma^*_{33}\right)^2 + 4 {\Sigma^*_{13}}^2 }\right] \\
\hat{\Sigma}^*_{33} &= \frac{1}{2}\left[ \left(\Sigma^*_{11} + \Sigma^*_{33}\right) -\textrm{sgn}\left(\Sigma^*_{11} - \Sigma^*_{33}\right)\sqrt{\left(\Sigma^*_{11} - \Sigma^*_{33}\right)^2 + 4 {\Sigma^*_{13}}^2 }\right] 
\label{eq:sigmashat}
\end{align}


Equation~\ref{eq:sigmashat} can be written in a compact form as:

\begin{align}
\hat{\Sigma}^*_{11} &= \frac{1}{2}\left(W + \textrm{sgn}(R)\sqrt{T} \right)\\
\hat{\Sigma}^*_{33} &= \frac{1}{2}\left(W - \textrm{sgn}(R)\sqrt{T} \right) \label{eq:Sigmashat}
\end{align}

The corresponding derivatives, which will be needed in the following, are given by:
\begin{align}
\frac{\partial }{\partial S} \left[\hat{\Sigma}^*_{11} \right] = \frac{1}{2}\left(\frac{\partial W}{\partial S} + \textrm{sgn}(R)\frac{1}{2\sqrt{T}}\frac{\partial T}{\partial S}  \right)\\
\frac{\partial }{\partial S} \left[\hat{\Sigma}^*_{33} \right] = \frac{1}{2}\left(\frac{\partial W}{\partial S} - \textrm{sgn}(R)\frac{1}{2\sqrt{T}}\frac{\partial T}{\partial S}  \right)\label{eq:dS_Sigmas}
\end{align}

\textbf{\underline{Case T>0, $\left|\Sigma^*_{13}\right|$=0:}}
~\\

The treatment of the previous case is still applicable with the exception of Eq.~\ref{eq:dS_sintheta} in which the denominator becomes zero.
This happens when $\Sigma^*_{13}$=0, which implies $\sqrt{T}=\left| R \right|$ and therefore $\cos 2 \theta = 1$. The case $T=0$ will be treated separately later, therefore here we can assume $\left| R \right|>0$. We can expand with respect to ${\Sigma^*_{13}}/{R}$ obtaining:

\begin{equation}
\cos 2 \theta = \frac{\left| R \right|}{\sqrt{R^2+4 {\Sigma^*_{13}}^2}} = \frac{1}{\sqrt{1+4 \frac{{\Sigma^*_{13}}^2}{R^2}}} \simeq \frac{1}{1+2 \frac{{\Sigma^*_{13}}^2}{R^2}} \simeq 1-2 \frac{{\Sigma^*_{13}}^2}{R^2}
\end{equation}

Replacing these result in Eq.~\ref{eq:sincostheta} we obtain:

\begin{equation}
\sin \theta = \mathrm{sgn}(R)\mathrm{sgn}(\Sigma^*_{13})\frac{\left| \Sigma^*_{13} \right|}{\left| R \right|} = \frac{\Sigma^*_{13} }{ R } \label{eq:spacialsintheta}
\end{equation}





We call $S_0$ the location at which ${\Sigma}^*_{13}=0$. At this location we define the auxiliary quantities:
\begin{align}
c =& \Sigma^*_{14} + \Sigma^*_{23}\\
d =& \Sigma^*_{24}
\end{align}

We introduce $\Delta S = S - S_0$ and we can write using Eqs.~\ref{eq:TranspSig11}--\ref{eq:TranspSig13}:
\begin{equation}
\Sigma^*_{13} = c \Delta S +d \Delta S^2 \label{eq:sig13spec}
\end{equation}

By taking the derivative of Eq.~\ref{eq:spacialsintheta} and using Eq.~\ref{eq:sig13spec} we obtain:
\begin{equation}
\frac{\partial }{\partial S} \sin \theta =\frac{1}{R^2}\left[\left(c + 2d \Delta S \right) R \ - \frac{\partial R }{\partial S}\left(c\Delta S + d \Delta S^2 \right)\right]
\end{equation}

In the implementation we need only the value for $\Delta S$=0, which is simply given by:
\begin{equation}
\frac{\partial }{\partial S} \sin \theta =\frac{c}{R}
\end{equation}
 
 
 
\textbf{\underline{Case T=0, $\left| c \right|$>0}}
~\\

Special care has to be taken at sections $S_0$ at which ${\Sigma}^*_{11}={\Sigma}^*_{33}$ and ${\Sigma}^*_{13}=0$ as Eqs.~\ref{eq:cos2theta}  and \ref{eq:dS_Sigmas} cannot be evaluated directly. Also in this case we define:
\begin{equation}
\Delta S = S - S_0
\end{equation}

At the location of the apparent singularity ($\Delta S$=0) we define the auxiliary quantities:
\begin{align}
a =& \Sigma^*_{12} - \Sigma^*_{34}\\
b =& \Sigma^*_{22} - \Sigma^*_{44}\\
c =& \Sigma^*_{14} + \Sigma^*_{23}\\
d =& \Sigma^*_{24} 
\end{align}

and therefore, using Eqs.~\ref{eq:TranspSig11}--\ref{eq:TranspSig13}, we can write:
\begin{align}
&R = 2a \Delta S + b \Delta S^2 \label{eq:specR}\\
&\Sigma^*_{13} =c \Delta S +d \Delta S^2\label{eq:specS13}
\end{align}

With these definitions the function $T$ (defined by Eq.~\ref{eq:Tfun}) can be expanded around $\Delta S =0$ (using the Eqs.~\ref{eq:TranspSig11}, \ref{eq:TranspSig33}, \ref{eq:TranspSig13}):
\begin{equation}
T = \Delta S^2\left[\left(2a +b \Delta S \right)^2 +4\left( c +d \Delta S\right)^2\right]
\label{eq:Tapprox}
\end{equation}

Replacing Eq.~\ref{eq:Tapprox} in Eq.~\ref{eq:cos2theta} allows  removing the apparent singularity:
\begin{equation}
\cos 2 \theta = \frac{\left| 2a+b \Delta S\right|}{\sqrt{\left(2a +b \Delta S \right)^2 +4\left( c +d \Delta S\right)^2}}
\label{eq:cost_spc}
\end{equation}

This can be derived obtaining:
\begin{align}
\frac{\partial }{\partial S} \left[ \cos 2\theta \right] &= \mathrm{sgn}(2a+b \Delta S)\left[
\frac{b}{\sqrt{\left(2a +b \Delta S \right)^2 +4\left( c +d \Delta S\right)^2}}
\right. \nonumber \\  &\left. \qquad \qquad \qquad \qquad \qquad
-\frac{(2a+b\Delta S)(2ab+b^2 \Delta s +4 c d+ 4 d^2 \Delta S)}{\left(\sqrt{\left(2a +b \Delta S \right)^2 +4\left( c +d \Delta S\right)^2}\right)^3}
\right]
\end{align}

Similarly, replacing Eq.~\ref{eq:Tapprox} in Eq.~\ref{eq:Sigmashat} we obtain:
\begin{align}
\hat{\Sigma}^*_{11} &= \frac{W}{2} + \frac{1}{2} \mathrm{sgn}\left(2a\Delta S + b \Delta S^2\right)\left| \Delta S\right| \sqrt{\left(2a +b \Delta S \right)^2 +4\left( c +d \Delta S\right)^2}\\
\hat{\Sigma}^*_{33} &= \frac{W}{2} - \frac{1}{2} \mathrm{sgn}\left(2a\Delta S + b \Delta S^2\right)\left| \Delta S\right| \sqrt{\left(2a +b \Delta S \right)^2 +4\left( c +d \Delta S\right)^2}\label{eq:SigmasSing}
\end{align}

This can be derived obtaining:
\begin{align}
\frac{\partial }{\partial S} \left[\hat{\Sigma}^*_{11} \right] =& \frac{1}{2}\frac{\partial W }{\partial S}
+\frac{1}{2}\mathrm{sgn}\left(2a\Delta S +b \Delta S^2 \right)\mathrm{sgn}(\Delta S)\left[
\sqrt{\left(2a +b \Delta S \right)^2 +4\left( c +d \Delta S\right)^2}
\right. \nonumber \\ & \qquad  \qquad \qquad \qquad \qquad  \qquad \qquad\left. 
+\frac{\Delta S \left(2ab+b^2 \Delta s +4 c d+ 4 d^2 \Delta S \right)}{\sqrt{\left(2a +b \Delta S \right)^2 +4\left( c +d \Delta S\right)^2}}
\right] \\
\frac{\partial }{\partial S} \left[\hat{\Sigma}^*_{33} \right] =& \frac{1}{2}\frac{\partial W }{\partial S}
-\frac{1}{2}\mathrm{sgn}\left(2a\Delta S +b \Delta S^2 \right)\mathrm{sgn}(\Delta S)\left[
\sqrt{\left(2a +b \Delta S \right)^2 +4\left( c +d \Delta S\right)^2}
\right. \nonumber \\ & \qquad  \qquad \qquad \qquad \qquad  \qquad \qquad\left. 
+\frac{\Delta S \left(2ab+b^2 \Delta s +4 c d+ 4 d^2 \Delta S \right)}{\sqrt{\left(2a +b \Delta S \right)^2 +4\left( c +d \Delta S\right)^2}}
\right] 
\end{align}

In the implementation only the values at $\Delta S$=0 are needed. For this case the obtained results above can be simplified as:
\begin{equation}
\cos 2 \theta = \frac{\left| 2a\right|}{2\sqrt{a^2 + c^2}}
\end{equation}

\begin{equation}
\frac{\partial }{\partial S} \left[ \cos 2\theta \right] = \mathrm{sgn}(2a)\left[
\frac{b}{2\sqrt{a^2 + c^2 }}-\frac{a(ab+2cd)}{2\left(\sqrt{a^2 + c^2}\right)^3}
\right]
\end{equation}

\begin{align}
\hat{\Sigma}^*_{11} &= \frac{W}{2}\\
\hat{\Sigma}^*_{33} &= \frac{W}{2}
\end{align}

\begin{align}
\frac{\partial }{\partial S} \left[\hat{\Sigma}^*_{11} \right] =& \frac{1}{2}\frac{\partial W }{\partial S}
+ \mathrm{sgn}(2a)\sqrt{a^2 + c^2 }
\\
\frac{\partial }{\partial S} \left[\hat{\Sigma}^*_{33} \right] =&\frac{1}{2}\frac{\partial W }{\partial S}
- \mathrm{sgn}(2a)\sqrt{a^2 + c^2 }
\end{align}

Eqs.~\ref{eq:sincostheta} and~\ref{eq:dS_sintheta} can still be used to evaluate $\sin \theta$ and $\cos \theta$ and the corresponding derivatives, once we assume that $\mathrm{sgn}(0) = 1$ and noticing from Eqs.~\ref{eq:specR} and \ref{eq:specS13} that for small $\Delta S$:
\begin{equation}
\mathrm{sgn}(R)\mathrm{sgn}(\Sigma^*_{13}) = \mathrm{sgn}(a)\mathrm{sgn}(c)
\end{equation}


\textbf{\underline{Case T=0, c=0, $\left| a \right|$>0}}
~\\

The treatment of the previous case is still applicable with the exception of Eq.~\ref{eq:dS_sintheta} in which the denominator becomes zero.

For this case we can write (from Eq.~\ref{eq:cost_spc}) around the point where this condition is verified:
\begin{equation}
\cos 2\theta =  \frac{1}{\sqrt{1+\frac{4d^2 \Delta S^2}{\left(2a +b \Delta S\right)^2}}} \simeq  
1-\frac{2d^2 \Delta S^2}{\left(2a +b \Delta S\right)^2}
\label{eq:cos2theta_manip}
\end{equation}

We notice from Eqs.~\ref{eq:specR} and \ref{eq:specS13} that for small $\Delta S$:
\begin{equation}
\mathrm{sgn}(R)\mathrm{sgn}(\Sigma^*_{13}) = \mathrm{sgn}(a)\mathrm{sgn}(d)\mathrm{sgn}(\Delta S)\label{eq:signsin_sing}
\end{equation}

Replacing Eq.~\ref{eq:cos2theta_manip} and \ref{eq:signsin_sing} into in Eq.~\ref{eq:sincostheta}
we obtain:
\begin{equation}
\sin\theta =  \frac{d\Delta S}{2a} \left| 1 - \frac{b \Delta S}{2a}\right|
\end{equation}

which can be derived in $\Delta S = 0$ obtaining:
\begin{equation}
\frac{\partial }{\partial S} \sin \theta =\frac{d}{2a} 
\end{equation}

The case in which also $d=0$ is (or is equivalent to) the uncoupled case as $\Sigma^*_{13}$ is zero for all $S$.
~\\


\textbf{\underline{Case T=0, c=0, a=0}}
~\\

Around the apparently singular point we can write:
\begin{align}
&R = b \Delta S^2\\
&\Sigma^*_{13} =d \Delta S^2
\end{align}

Therefore:
\begin{equation}
T = S^4\left( b^2 + 4 d^2\right)
\end{equation}

and:
\begin{equation}
\cos 2 \theta = \frac{\left|b\right|}{\sqrt{b^2+4d^2}}
\end{equation}

which is a constant. Eqs. \ref{eq:sincostheta} and \ref{eq:dS_sintheta} can still be used to evaluate $\sin \theta$ and $\cos \theta$ while the corresponding derivatives vanish:

This can be derived obtaining:
\begin{align}
\frac{\partial }{\partial S} \cos \theta &= 0\\
\frac{\partial }{\partial S} \sin \theta &= 0
\end{align}

Replacing $a=c=0$ into Eq \ref{eq:SigmasSing} we obtain:
\begin{align}
\hat{\Sigma}^*_{11}  =& \frac{W}{2}
+\frac{1}{2}\mathrm{sgn}(b)\Delta S^2\sqrt{b^2+4d^2} \\
\hat{\Sigma}^*_{33} =& \frac{W}{2}
-\frac{1}{2}\mathrm{sgn}(b)\Delta S^2\sqrt{b^2+4d^2}
\end{align}

and:

\begin{align}
\frac{\partial }{\partial S} \left[\hat{\Sigma}^*_{11} \right] =& \frac{1}{2}\frac{\partial W }{\partial S}\\
\frac{\partial }{\partial S} \left[\hat{\Sigma}^*_{33} \right] =& \frac{1}{2}\frac{\partial W }{\partial S}
\end{align}

The case in which also $d=0$ is (or is equivalent to the uncoupled case) as $\Sigma^*_{13}$ is zero for all $S$.




\subsection{Forces and kicks on weak beam particles}

The positions of the weak beam particle in the un-coupled boosted frame are given by:
\begin{align}
\hat{\overline{x}}^* &= \overline{x}^*\cos \theta  + \overline{y}^*\sin \theta \\
\hat{\overline{y}}^* &= -\overline{x}^*\sin \theta  + \overline{y}^* \cos \theta
\end{align}

In the following we will also need to evaluate:
\begin{align}
\frac{\partial }{\partial S} \left[\hat{\overline{x}}^*\left(\theta (S)\right) \right] &= 
 \frac{\partial \overline{x}^* }{\partial S}  \cos \theta
+ \overline{x}^*  \frac{\partial }{\partial S} \left[ \cos \theta \right] 
+ \frac{\partial \overline{y}^* }{\partial S}  \sin \theta
+ \overline{y}^*\frac{\partial }{\partial S} \left[ \sin \theta \right]  \\
\frac{\partial }{\partial S} \left[\hat{\overline{y}}^*\left(\theta (S)\right) \right] &= 
- \frac{\partial \overline{x}^* }{\partial S}  \sin \theta
-\overline{x}^*  \frac{\partial }{\partial S} \left[ \sin \theta \right] 
+  \frac{\partial \overline{y}^* }{\partial S}  \cos \theta
+ \overline{y}^*\frac{\partial }{\partial S} \left[ \cos \theta \right]  
\end{align}



In this boosted, rotated and re-centered frame, closed formulas exist to evaluate the following quantities:

\begin{align}
\hat{F}^*_x &= -K_{sl} \frac{\partial \hat{U}^*}{\partial \hat{\overline{x}}^*}\left(\hat{\overline{x}}^*, \hat{\overline{y}}^* , \hat{\Sigma}^*_{11}, \hat{\Sigma}^*_{33}\right) \label{eq:firstf}\\
%
\hat{F}^*_y &= -K_{sl}\frac{\partial \hat{U}^*}{\partial \hat{\overline{y}}^*}\left(\hat{\overline{x}}^*, \hat{\overline{y}}^* , \hat{\Sigma}^*_{11}, \hat{\Sigma}^*_{33}\right)\\
%
\hat{G}^*_x &= -K_{sl}\frac{\partial \hat{U}^*}{\partial \hat{\Sigma}^*_{11}}\left(\hat{\overline{x}}^*, \hat{\overline{y}}^* , \hat{\Sigma}^*_{11}, \hat{\Sigma}^*_{33}\right)\\
%
\hat{G}^*_y &= -K_{sl}\frac{\partial \hat{U}^*}{\partial \hat{\Sigma}^*_{33}}\left(\hat{\overline{x}}^*, \hat{\overline{y}}^* , \hat{\Sigma}^*_{33}, \hat{\Sigma}^*_{33} \right) \label{eq:lastf}
\end{align}
where $\hat{U}^*$ is the electric potential associated to the normalized transverse distribution and:
\begin{equation}
K_{sl} = \frac{N_{sl} q_{sl} q_0}{P_0 c}
\label{eq:factor}
\end{equation}
where $N_{sl}$ is the number of particles in the strong-beam slice, $q_{sl}$ and $q_0$ are the particle charges for the strong and weak beam respectively, $P_0$ is the reference momentum of the weak beam.

The minus sign in the Eqs.~\ref{eq:firstf}-\ref{eq:lastf} comes from the definition of electric potential, i.e. $E=-\nabla U$.


For a bi-Gaussian beam (elliptic) \cite{hirata}:
\begin{align}
\hat{f}^*_x = -\frac{\partial \hat{U}^*}{\partial \hat{\overline{x}}^*} &= \frac{1}{2 \epsilon_0 \sqrt{2 \pi \left(\hat{\Sigma}^*_{11}-\hat{\Sigma}^*_{33}\right)}} \textrm{Im} \left[w\left(\frac{\hat{\overline{x}}^*+i \hat{\overline{y}}^*}{\sqrt{2 \left(\hat{\Sigma}^*_{11}-\hat{\Sigma}^*_{33}\right)}}\right) \right.
\nonumber \\
&\qquad\qquad\left.
- \exp \left(-\frac{(\hat{\overline{x}}^*)^2}{2\hat{\Sigma}^*_{11}}-\frac{(\hat{\overline{y}}^*)^2}{2\hat{\Sigma}^*_{33}}\right) w\left(\frac{\hat{\overline{x}}^*\sqrt{\frac{\hat{\Sigma}^*_{33}}{\hat{\Sigma}^*_{11}}}+i \hat{\overline{y}}^*\sqrt{\frac{\hat{\Sigma}^*_{11}}{\hat{\Sigma}^*_{33}}}}{\sqrt{2 \left(\hat{\Sigma}^*_{11}-\hat{\Sigma}^*_{33}\right)}}\right)
\right]\\
\hat{f}^*_y  = -\frac{\partial \hat{U}^*}{\partial \hat{\overline{x}}^*} &= \frac{1}{2 \epsilon_0 \sqrt{2 \pi \left(\hat{\Sigma}^*_{11}-\hat{\Sigma}^*_{33}\right)}} \textrm{Re} \left[w\left(\frac{\hat{\overline{x}}^*+i \hat{\overline{y}}^*}{\sqrt{2 \left(\hat{\Sigma}^*_{11}-\hat{\Sigma}^*_{33}\right)}}\right) \right.
\nonumber \\
&\qquad\qquad\left.
- \exp \left(-\frac{(\hat{\overline{x}}^*)^2}{2\hat{\Sigma}^*_{11}}-\frac{(\hat{\overline{y}}^*)^2}{2\hat{\Sigma}^*_{33}}\right) w\left(\frac{\hat{\overline{x}}^*\sqrt{\frac{\hat{\Sigma}^*_{33}}{\hat{\Sigma}^*_{11}}}+i \hat{\overline{y}}^*\sqrt{\frac{\hat{\Sigma}^*_{11}}{\hat{\Sigma}^*_{33}}}}{\sqrt{2 \left(\hat{\Sigma}^*_{11}-\hat{\Sigma}^*_{33}\right)}}\right)
\right]\\
\hat{g}^*_x = -\frac{\partial \hat{U}^*}{\partial \hat{\Sigma}^*_{11}} &= -\frac{1}{2 \left(\hat{\Sigma}^*_{11}-\hat{\Sigma}^*_{33}\right)}
 \left\{\hat{\overline{x}}^* \hat{E}^*_x  + \hat{\overline{y}}^* \hat{E}^*_y +\
 \frac{1}{2 \pi \epsilon_0}\left[\sqrt{\frac{\hat{\Sigma}^*_{33}}{\hat{\Sigma}^*_{11}}}  \exp \left(-\frac{(\hat{\overline{x}}^*)^2}{2\hat{\Sigma}^*_{11}}-\frac{(\hat{\overline{y}}^*)^2}{2\hat{\Sigma}^*_{33}}\right)   -1 \right]\right\}\\
\hat{g}^*_y =  -\frac{\partial \hat{U}^*}{\partial \hat{\Sigma}^*_{33}} &= \frac{1}{2 \left(\hat{\Sigma}^*_{11}-\hat{\Sigma}^*_{33}\right)}
 \left\{\hat{\overline{x}}^* \hat{E}^*_x  + \hat{\overline{y}}^* \hat{E}^*_y +\
 \frac{1}{2 \pi \epsilon_0}\left[\sqrt{\frac{\hat{\Sigma}^*_{11}}{\hat{\Sigma}^*_{33}}}  \exp \left(-\frac{(\hat{\overline{x}}^*)^2}{2\hat{\Sigma}^*_{11}}-\frac{(\hat{\overline{y}}^*)^2}{2\hat{\Sigma}^*_{33}}\right)   -1 \right]\right\}
\end{align}
where $w$ is the Faddeeva function.
 
 
For a round beam, i.e. $\hat{\Sigma}^*_{11}=\hat{\Sigma}^*_{33} = \hat{\Sigma}^*$:
\begin{align}
\hat{f}^*_x = -\frac{\partial \hat{U}^*}{\partial \hat{\overline{x}}^*} &= \frac{1}{2 \pi \epsilon_0} \left[1-  \exp \left(-\frac{(\hat{\overline{x}}^*)^2+(\hat{\overline{y}}^*)^2}{2{\hat{\Sigma}}^*}\right) \right]\frac{x}{(\hat{\overline{x}}^*)^2+(\hat{\overline{y}}^*)^2}\\
\hat{f}^*_y = -\frac{\partial \hat{U}^*}{\partial \hat{\overline{x}}^*} &= \frac{1}{2 \pi \epsilon_0} \left[1-  \exp \left(-\frac{(\hat{\overline{x}}^*)^2+(\hat{\overline{y}}^*)^2}{2{\hat{\Sigma}}^*}\right) \right]\frac{y}{(\hat{\overline{x}}^*)^2+(\hat{\overline{y}}^*)^2}\\
\hat{g}^*_x =-\frac{\partial \hat{U}^*}{\partial \hat{\Sigma}^*_{11}} &= \frac{1}{2\left[(\hat{\overline{x}}^*)^2+(\hat{\overline{y}}^*)^2\right]} 
\left[
\hat{\overline{y}}^* \hat{E}^*_y  - \hat{\overline{x}}^* \hat{E}^*_x 
+\frac{1}{ 2 \pi \epsilon_0} \frac{\left(\hat{\overline{x}}^*\right)^2}{\hat{\Sigma}^*} \exp \left(-\frac{(\hat{\overline{x}}^*)^2+(\hat{\overline{y}}^*)^2}{2{\hat{\Sigma}}^*}\right)\right]\\
%
\hat{g}^*_y =-\frac{\partial \hat{U}^*}{\partial \hat{\Sigma}^*_{33}} &= \frac{1}{2\left[(\hat{\overline{x}}^*)^2+(\hat{\overline{y}}^*)^2\right]} 
\left[
\hat{\overline{x}}^* \hat{E}^*_x  - \hat{\overline{y}}^* \hat{E}^*_y 
+\frac{1}{ 2 \pi \epsilon_0} \frac{\left(\hat{\overline{y}}^*\right)^2}{\hat{\Sigma}^*} \exp \left(-\frac{(\hat{\overline{x}}^*)^2+(\hat{\overline{y}}^*)^2}{2{\hat{\Sigma}}^*}\right)\right]
\end{align}

We have used lower-case symbols to indicate that the factor given by Eq.~\ref{eq:factor} is not yet applied.
~\\

The transverse kicks in the coupled (but still boosted) reference frame are given by:
\begin{align}
F^*_x &= \hat{F}^*_x  \cos \theta  -  \hat{F}^*_y \sin \theta \\
F^*_y &= \hat{F}^*_x \sin \theta   + \hat{F}^*_y \cos \theta  
\end{align}

To compute the longitudinal kick we notice from Eq.~\ref{eq:sdef} that:
\begin{equation}
 \frac{\partial }{\partial z} =  \frac{1}{2} \frac{\partial }{\partial S} 
\end{equation}

Therefore:
\begin{equation}
F^*_z  =  \frac{1}{2}\frac{\partial }{\partial S} \left[
\hat{U}^*\left(\hat{\overline{x}}^*\left(\theta (S)\right), \hat{\overline{y}}^*\left(\theta (S)\right) , \hat{\Sigma}^*_{11}(S), \hat{\Sigma}^*_{33}(S)\right) 
\right]
\end{equation}

This can be rewritten as:
\begin{align}
F^*_z  =  &\frac{1}{2}\left(
\hat{F}^*_x \frac{\partial }{\partial S} \left[\hat{\overline{x}}^*\left(\theta (S)\right) \right]+
\hat{F}^*_y \frac{\partial }{\partial S} \left[\hat{\overline{y}}^*\left(\theta (S)\right) \right]+
\hat{G}^*_x\frac{\partial }{\partial S} \left[\hat{\Sigma}^*_{11}(S)\right]+
\hat{G}^*_y\frac{\partial }{\partial S} \left[\hat{\Sigma}^*_{33}(S)\right]\right)
\end{align}
where all the terms have been evaluated before.

The quantities evaluated so far can be used to compute the effect of the beam-beam interaction on the particles coordinates and momenta~\cite{hirata}:
\begin{align}
x^*_{new} &= x^* - S {F}^*_x\\
p^*_{x, new} &= p^*_{x} + {F}^*_x\\
y^*_{new} &= y^* - S {F}^*_y\\
p^*_{y, new} &= p^*_{y} + {F}^*_y\\
z^*_{new} &= z^*\\
\delta^*_{new} &= \delta^* + {F}^*_z
+\frac{1}{2}\left[ 
{F}^*_x\left(p^*_{x} + \frac{1}{2}{F}^*_x + p^*_{x,\textrm{sl}}\right)+
{F}^*_y\left(p^*_{y} + \frac{1}{2}{F}^*_y + p^*_{y,\textrm{sl}}\right)
\right]
\end{align}

The physical meaning of the different terms in these equations is illustrated in~\cite{bb6dslides}.

\subsection{Inverse Lorentz boost (for the weak beam)}

Now we need to go back to the accelerator coordinates by undoing the transformation described in Sec.~\ref{sec:directboost}.

As before we evaluate:
\begin{equation}
p^*_z  =  \sqrt{\left(1+\delta^*\right)^2-{p_x^*}^2-{p_y^*}^2}
\end{equation}
and then:
\begin{align}
h^*_x &= \frac{p^*_x}{p^*_z}\\
h^*_y &= \frac{p^*_y}{p^*_z}\\
h^*_\sigma &= 1-\frac{\delta^*+1}{p^*_z}
\end{align}

We invert the matrix (\ref{eq:Lmat}) using Cramer's rule:

\begin{equation}
\mathrm{Det}\left( L \right) =  \frac{1}{\cos \phi} + \left( h_x^* \cos \alpha + h_y^* \sin \alpha - h_\sigma^* \sin \phi \right)\tan \phi
\end{equation}
\tiny
\begin{multline}
L^{\mathrm{inv}} = \frac{1}{\mathrm{Det}\left( L\right)} \times\\
\left( \begin{matrix}
\left(\frac{1}{\cos \phi} + \sin \alpha  \tan \phi \left( h^*_y - h^*_\sigma\sin \alpha \sin \phi\right)\right) & 
\sin \alpha \tan \phi \left( h^*_\sigma\cos \alpha \sin \phi - h_x^*\right)& 
-\tan \phi \left( \cos \alpha - h^*_x \sin^2 \alpha \sin \phi + h_y^* \cos \alpha \sin \alpha \sin  \phi \right)\\
\cos \alpha \tan \phi \left( -h^*_y +h^*_\sigma \sin \alpha  \sin \phi\right)& 
\left(\frac{1}{\cos \phi} + \cos \alpha  \tan \phi \left( h^*_x - h^*_\sigma\cos \alpha \sin \phi\right)\right) &
-\tan \phi \left( \sin \alpha - h^*_y \cos^2 \alpha \sin \phi + h_x^* \cos \alpha \sin \alpha \sin  \phi \right)\\
%
-h_\sigma^* \cos \alpha \sin \phi  &
 -h_\sigma^* \sin \alpha \sin \phi & 
 \left( 1 + h_x^* \cos \alpha \sin \phi +h^*_y \sin \alpha \sin  \phi \right)\\
\end{matrix} \right)
\end{multline}
\normalsize

This can be used to transform the positions:

\begin{equation}
\left( \begin{matrix}
x\\
y\\
\sigma
\end{matrix}\right) 
 = 
L^{\mathrm{inv}}
\left( \begin{matrix}
x^*\\
y^*\\
\sigma^*
\end{matrix}\right)
\end{equation}

The Hamiltonian can be transformed with a re-scaling:
\begin{equation}
h = h^* \cos^2 \phi = \left(\delta^* + 1 -\sqrt{\left(1+\delta^*\right)^2-{p_x^*}^2-{p_y^*}^2}\right)\cos^2 \phi
\end{equation}

This can be used to transform the transverse momenta (inverting Eqs. \ref{eq:px} and following):

\begin{align}
p_x &= p_x^* \cos \phi + h \cos \alpha \tan \phi\\
p_y &= p_y^* \cos \phi + h \sin \alpha \tan \phi
\end{align}

The longitudinal momentum can be calculated using directly Eq.~\ref{eq:delta}:

\begin{equation}
\delta = \delta^* + p_x \cos \alpha \tan \phi + p_y \sin \alpha \tan \phi - h \tan^2 \phi
\end{equation}

\subsection{Additional material}



\subsubsection{Detailed explanation of "the boost" transformation}
\label{app:boost}
The reference frame transformation used in Sec.~\ref{sec:directboost} can be written as \cite{hirata, beam_beam}:
\begin{equation}
\left( \begin{matrix}
\sigma^*\\
x^*\\
s^*\\
y^*
\end{matrix} \right) = A^{-1}{R_{\mathrm{CP}}}^{-1}L_{\mathrm{boost}}R_{\mathrm{CA}}R_{\mathrm{CP}}A
\left( \begin{matrix}
\sigma\\
x\\
s\\
y
\end{matrix} \right) 
\end{equation}

Here $A$ is the matrix transforming the accelerator coordinates (Courant-Snyder) to Cartesian coordinates:

\begin{equation}
\left( \begin{matrix}
ct\\
X\\
Z\\
Y
\end{matrix} \right) = A
\left( \begin{matrix}
\sigma\\
x\\
s\\
y
\end{matrix} \right) =
\left( \begin{matrix}
-1 &0 &1&0\\
0 &1 &0&0\\
0 &0 &1&0\\
0 &0 &0&1
\end{matrix} \right) 
\left( \begin{matrix}
\sigma\\
x\\
s\\
y
\end{matrix} \right)
\end{equation}

$R_{\mathrm{CP}}$ is a rotation matrix bringing the crossing plane to the $X-Z$ plane:
\begin{equation}
R_{\mathrm{CP}}=
\left( \begin{matrix}
1 &0 &1&0\\
0 &\cos\alpha &0&\sin\alpha\\
0 &0 &1&0\\
0 &-\sin\alpha &0&\cos\alpha
\end{matrix} \right) 
\end{equation}

$R_{\mathrm{CA}}$ is a rotation matrix moving to the barycentric reference frame (in which the two beams are symmetric with respect to the s-axis):
\begin{equation}
R_{\mathrm{CA}}=
\left( \begin{matrix}
1 &0 &0&0\\
0 &\cos\phi&\sin\phi &0\\
0 &-\sin\phi&\cos\phi &0\\
0 &0 &0&1
\end{matrix} \right) 
\end{equation}

$L_{\mathrm{boost}}$ is the matrix defining a Lorentz boost in the direction of the rotated X-axis:
\begin{equation}
L_{\mathrm{boost}}=
\left( \begin{matrix}
1/\cos\phi &-\tan\phi &0&0\\
-\tan\phi &1/\cos\phi&0 &0\\
0 &0&1 &0\\
0 &0 &0&1
\end{matrix} \right) 
\end{equation}

The momenta are transformed similarly \cite{beam_beam}:

\begin{equation}
\left( \begin{matrix}
\delta^*\\
p_x^*\\
h^*\\
p_y^*
\end{matrix} \right) = 
B^{-1}{R_{\mathrm{CP}}}^{-1}L_{\mathrm{boost}}R_{\mathrm{CA}}R_{\mathrm{CP}}B
\left( \begin{matrix}
\delta\\
p_x\\
h\\
p_y
\end{matrix} \right) 
\end{equation}

where the transformation from accelerator to Cartesian coordinates given by:

\begin{equation}
\left( \begin{matrix}
E/c-p_0\\
P_x\\
P_z-p_0\\
P_y
\end{matrix} \right) = p_0
\left( \begin{matrix}
1 &0 &0&0\\
0 &1&0 &0\\
0 &0&-1 &0\\
0 &0 &0&1
\end{matrix} \right)
\left( \begin{matrix}
\delta\\
p_x\\
h\\
p_y
\end{matrix} \right) 
\end{equation}

As explaned in Sec.\ref{sec:directboost} not all particles with $s=0$ are fixed points of the transformation, therefore a drift  back to s=0 needs to be performed as we are tracking w.r.t. $s$ and not w.r.t. time. The net effect of the transformation is to move from the reference frame of the weak beam to the boosted barycentric frame.

%\section{Some equations for slides}

%\begin{equation}
%\sin \theta = 0
%\end{equation}

\subsubsection{Constant charge slicing}
\label{app:slicing}
We consider a Gaussian longitudinal bunch distribution:

\begin{equation}
\lambda(z) = \frac{1}{\sigma_z \sqrt{2\pi}}e^{-\frac{z^2}{2\sigma_z^2}} 
\end{equation}

We introduce the cumulative distribution function:

\begin{equation}
Q(z) = \int_{-\infty}^{z} \lambda(z')dz' = \frac{1}{2}+\frac{1}{2}\mathrm{erf}\left(\frac{z}{\sqrt{2}\sigma_z}\right)
\label{cumul}
\end{equation}

We define longitudinal cuts $z^{\mathrm{cut}}_n$ such that the bunch is sliced in $N$ sections having the same charge:

\begin{equation}
Q(z^{\mathrm{cut}}_n) = \frac{n}{N}
\label{constsli}
\end{equation}

Replacing \ref{constsli} in \ref{cumul} we obtain:

\begin{equation}
z^{\mathrm{cut}}_n = \sqrt{2} \sigma_z \mathrm{erf}^{-1}\left( \frac{2n}{N}-1\right)
\end{equation}

For each slice we need to find the longitudinal centroid position. For generic slice having edges $z_1$ and $z_2$ the centroid position can be written as:

\begin{equation}
z^{centroid}= \frac{1}{Q(z_2) - Q(z_1)}\int_{z_1}^{z_2} z \lambda(z)dz = \frac{\sigma_z}{\sqrt{2\pi}\left(Q(z_2) - Q(z_1)\right)}\left(e^{-\frac{z_1^2}{2\sigma_z^2}}-e^{-\frac{z_2^2}{2\sigma_z^2}}\right)
\end{equation}

\subsubsection{Considerations on the $\Sigma$-matrix description}
\label{app:sigma}

Given the reduced $\Sigma$-matrix of the beam (including only position terms, no momenta):
\begin{equation}
\Sigma = \left(
\begin{matrix}
{\Sigma}_{11} & {\Sigma}_{13}\\
{\Sigma}_{13} & {\Sigma}_{33}
\end{matrix}
\right)
\end{equation}



 the distribution for a Gaussian beam can be written as:
\begin{equation}
\rho(\textbf{x}) = \rho_0 e^{-\textbf{x}^{\mathrm{T}} \Sigma^{-1}\textbf{x}}
\end{equation}

Points having same density lie con ellipses defined by the equation:
\begin{equation}
\textbf{x}^\mathrm{T}\Sigma^{-1}\textbf{x} = \mathrm{const.}
\label{eqellipse2}
\end{equation}

As $\Sigma$ is symmetric, it can be diagonalized:
\begin{equation}
\Sigma = \textbf{V} \textbf{W} \textbf{V}^{T}
\label{diagsigma}
\end{equation}

where the matrix $\textbf{V}$ has in its columns the eigenvectors of $\Sigma$ and $\textbf{W}$ is a diagonal matrix with the corresponding eigenvalues:
\begin{equation}
W = \left(
\begin{matrix}
\hat{\Sigma}_{11} & 0\\
0 & \hat{\Sigma}_{33}
\end{matrix}
\right)
\end{equation}

 $\textbf{V}$ is a unitary matrix (eigenvectors are ortho-normal):
\begin{equation}
\textbf{V} \textbf{V}^{T} = \textbf{I} \Rightarrow \textbf{V}^{-1} = \textbf{V}^{T}
\label{unitarity}
\end{equation}

\textbf{V} can be used to transform coordinates from the initial frame to the de-coupled frame:

where $\hat{\textbf{x}}$ are the coordinates in the decoupled frame, i.e. the projections of of $\textbf{x}$ on the eigenvectors:
\begin{equation}
\hat{\textbf{x}}= \textbf{V}^{T}\textbf{x}
\label{vartransform}
\end{equation}

Combining Eqs.~\ref{diagsigma} and~\ref{unitarity} we can write:

\begin{equation}
\Sigma^{-1} = \textbf{V} \textbf{W}^{-1} \textbf{V}^{T}
\end{equation}

This can be replaced in Eq.~\ref{eqellipse2}, re-writing the equation of the ellipse as:
\begin{equation}
\textbf{x}^\mathrm{T}\textbf{V} \textbf{W}^{-1} \textbf{V}^{T}\textbf{x} = \mathrm{const.}
\end{equation}

Using Eq.~\ref{vartransform} we obtain the equation of the ellipse in the reference system of the eigenvectors:
\begin{equation}
\hat{\textbf{x}}^\mathrm{T}\textbf{W}^{-1} \hat{\textbf{x}} = \mathrm{const.}
\end{equation}
which can be rewritten in the familiar form:
\begin{equation}
\frac{\hat{x}^2}{\hat{\Sigma}_{11}}+\frac{\hat{y}^2}{\hat{\Sigma}_{33}}=\mathrm{const.}
\end{equation}

Once the $\Sigma$-matrix is assigned, the one-sigma ellipse can be drawn by the following procedure:
\begin{itemize}
\item We diagonalize $\Sigma$ and we generate an auxiliary matrix defined as:
\begin{equation}
\textbf{A} = \textbf{V} \sqrt{\textbf{W}} \textbf{V}^{T}
\end{equation}
\item We generate a set of points in the unitary circle
\begin{equation}
\textbf{z} = \left[
\begin{matrix}
\cos t\\
\sin t
\end{matrix}
\right]
\end{equation}
\item We apply $ \textbf{A} $ to t to generate points on the one-sigma ellipse:
\begin{equation}
\textbf{x}_{1\sigma} = \textbf{A} \textbf{z}
\end{equation}
\end{itemize}

This can be verified as follows:
\begin{align}
{\textbf{x}_{1\sigma}}^\mathrm{T}\Sigma^{-1}\textbf{x}_{1\sigma} 
&= \textbf{z}^\mathrm{T}\textbf{A}^\mathrm{T}\Sigma^{-1}\textbf{A}\textbf{z} = \textbf{z}^\mathrm{T}(\textbf{V} \sqrt{\textbf{W}} \textbf{V})(\textbf{V}^{T} \textbf{W}^{-1} \textbf{V}^{T})(\textbf{V}\sqrt{\textbf{W}} \textbf{V}^{T}) \textbf{z} \nonumber
\\&=\textbf{z}^\mathrm{T}\textbf{V} \sqrt{\textbf{W}}  \textbf{W}^{-1} \sqrt{\textbf{W}} \textbf{V}^{T}\textbf{z}
= \textbf{z}^\mathrm{T}\textbf{V}  \textbf{V}^{T}\textbf{z} 
= \textbf{z}^\mathrm{T}\textbf{z} = 1
\end{align}

\section{Beam-beam interaction (6d model, Particle In Cell)}

To simulate self-consistently the interaction between two bunches of particles, it is possible to use the Particle In Cell method. The computation is done in a boosted reference frame in which the bunches move mainly along $s$ as illustrated in the previous section.

For this purpose, as for space charge simulations, we define a uniform 3D grid with grid sizes $\Delta x$, $\Delta y$,  $\Delta \zeta$. We note that each value of $\zeta$ corresponds to a different time of arrival at the Interaction Point (IP):
\begin{equation}
\zeta = s_\text{IP} - \beta_0 c t 
\ \Leftrightarrow \ 
t = \frac{s_\text{IP} - \zeta}{\beta_0 c}
\end{equation}

We simulate the interaction in discrete time intervals corresponding to the passage of the different slices. The duration of each interval is
\begin{equation}
\Delta t = \frac{\Delta \zeta}{\beta_0 c}
\label{eq:dtbbpic}
\end{equation}

We call $t_i$ the time at which the $i$-th slice is passing at the IP. The is is related to the $\zeta_i$ coordinate of the slice by the relation
\begin{equation}
t_i = \frac{s_\text{IP} - \zeta_i}{\beta_0 c}
\end{equation}

\subsection{Propagation of particles during the interaction}
\label{sec:prop_bb_pic}

From the conventional tracking using $s$ as independent variable we get for all particles the coordinates at the IP, which we call $x_\text{IP}$, $p_\text{x\,IP}$, $y_\text{IP}$, $p_\text{y\,IP}$, $zeta_\text{IP} = s_\text{y\,IP} - \beta_0 c t_\text{IP} $, where $t_\text{IP} $ is the time of arrival of the particle at the IP. Assuming the motion is in a drift space, for each time step, we propagate the particles from the IP to their positions at the time $t_i$:
\begin{equation}
x \left( t_i \right) = x_\text{IP} + \beta_x c\left( t_i -t_\text{IP}\right)
=
x_\text{IP} + \beta_x c\left( \frac{s_\text{IP} - \zeta_i}{\beta_0 c} -\frac{s_\text{IP} - \zeta_\text{IP}}{\beta_0 c}\right)
\end{equation}

Using the fact that:
\begin{equation}
p_x=\frac{P_x}{P_0}=\frac{m_0 \gamma \beta_x c}{m_0 \gamma_0 \beta_0 c} =\frac{ \gamma \beta_x}{ \gamma_0 \beta_0} 
\end{equation}
we can write:
\begin{equation}
x \left( t_i \right)
= x_\text{IP} + p_x\frac{\gamma_0}{\gamma}\left(\zeta_\text{IP} -  \zeta_i \right)
\end{equation}

With the coordinates of the propagated particles we can solve a Poisson problem having as source the 3D particle distribution at time $t_i$. Using the fact that the bunches are elongated and relativistic, we can solve the 2D Poisson equation instead of the full 3D problem. This procedure needs to be performed for the two colliding bunches.

\subsection{Time relation between the two beams}
For a particle of beam 1 having longitudinal coordinate $\zeta_\text{IP}^\text{B1}$ we want to know the longitudinal coordinate $\zeta_\text{IP}^\text{B2}$ corresponding to the section of beam 2 crossing the particle at time $t_i$.

We assume that the reference systems of the two beams are antiparallel and coincident in transverse:
\begin{align}
&s^\text{B2} - s_\text{IP}^\text{B2} = 
- \left( s^\text{B1} - s_\text{IP}^\text{B1}\right)
\label{eq:sb1b2}\\
&x^\text{B2} = - x^\text{B1}\\
&y^\text{B2} = + x^\text{B1}
\end{align}

Similarly as done in Sec.\,\ref{sec:prop_bb_pic}, we can write the $s$ position at the time $t_i$ for the particles of beam 1 and beam 2:
\begin{align}
&s^\text{B1} \left( t_i \right)
= s^\text{B1}_\text{IP} + \frac{\beta^\text{B1}_s}{\beta_0^\text{B1}}\left(\zeta^\text{B1}_\text{IP} -  \zeta^\text{B1}_i \right)
\label{eq:sprop_b1}\\
&s^\text{B2} \left( t_i \right)
= s^\text{B2}_\text{IP} + \frac{\beta^\text{B2}_s}{\beta_0^\text{B2}}\left(\zeta^\text{B2}_\text{IP} -  \zeta^\text{B2}_i \right)
\label{eq:sprop_b2}
\end{align}

To find particles that are at the same $s$ at time $t_i$ we replace Eqs.\,\ref{eq:sprop_b1}-\ref{eq:sprop_b2} in \ref{eq:sb1b2} we obtain:

\begin{equation}
\zeta^\text{B2}_\text{IP}
=
\zeta^\text{B2}_i
-\frac{\beta^\text{B1}_s}{\beta_0^\text{B1}}
\frac{\beta_0^\text{B2}}{\beta^\text{B2}_s}
\left(\zeta^\text{B1}_\text{IP} -  \zeta^\text{B1}_i \right)
\end{equation}

This relation can be used to probe the field map generated by the other bunch at the position of each particle.

\subsection{Computation of the kick}

The transverse kick at each time step can be written as:

\begin{equation}
\Delta p^\text{B1}_x = \frac{\Delta P^\text{B1}_x}{P^\text{B1}_0} = \frac{F_x \Delta t}{P^\text{B1}_0}
\end{equation}

Using Eqs.\,\ref{eq:forcex} and~\ref{eq:dtbbpic}, taking onto account that the beams move in opposite directions, we obtain:
\begin{equation}
\Delta p^\text{B1}_x = 
-\frac{q\Delta \zeta^\text{B1}}
{m_0^\text{B1}\gamma^\text{B1}_0(\beta^\text{B1}_0)^2 c^2}
\left(1+\beta^\text{B1}  \beta^\text{B2}\right) 
\frac{\partial \phi^\text{B2}}{\partial x}(x, y, \zeta^\text{B2})
\end{equation}

\section{Configuration of beam-beam lenses for tracking simulations (weak-strong)}

The effects of the non-linear forces introduced by beam-beam interactions in the Large Hadron Collider~(LHC) are studied with tracking simulations using, for example, the SixTrack and sixtracklib codes\,\cite{sixtracksite, sixtracklibrepo}. In these simulations the beam-beam interactions are modeled by a set of ``thin'' non-linear lenses around the collision points.
 ``6D beam-beam lenses'' based on Hirata's synchro-beam method\,\cite{hirata, bb6dnote, bb6dslides} are used to model the Head-On (HO) interactions at the for interaction points (IPs) while simpler ``4D lenses'' are used to model parasitic Long-Range encounters\,\cite{casbeambeam}.
 
This document describes a method to configure the beam-beam lenses in tracking simulations based on the model of the accelerator, which has been recently developed as an evolution of existing tools in MAD-X scripting language\,\cite{bbmacrorepo}\footnote{
The authors would like to acknowledge all the colleagues who  have contributed to the development of the MAD-X tools for the configuration of tracking simulations, on which the present work is largely based, and have provided important input and support, in particular G.~Arduini, J.~Barranco~Garcia, R.~De~Maria, S.~Fartoukh, M.~Giovannozzi, S.~Kostoglou, E.~M\'etral,  Y.~Papaphippou, D.~Pellegrini, T.~Pieloni and F.~Van Der Veken.}

In particular, in Sec.\,\ref{sec:posdir}, we discuss how to reconstruct the absolute position of the two beams with respect to the lab frame using the twiss and survey tables; in Sec.\,\ref{sec:separ} we discuss how to compute the separation between the two beams; in Sec.\,\ref{sec:xing} we describe how to identify the crossing plane and crossing angle; in Sec.\,\ref{sec:b4} we describe how to configure the anticlockwise beam (conventionally called beam~4)  from the MAD-X model based on two clockwise-oriented sequences; in Sec.\,\ref{sec:crab} we introduce the effect of crab cavities on the beam-beam configuration. 

\subsection{Identification of the beam position and direction}
\label{sec:posdir}

The position and orientation of the beams at a certain machine element can be obtained from MAD-X combining the information from the survey and twiss tables.

We assume that:
\begin{itemize}
    \item The sequences start from an element at which the reference trajectories of the two beams are known to be parallel;
    \item Both beams (B1 and B2) have the same orientation (clockwise);
    \item Markers or beam-beam lenses are installed at the $s$-locations of the beam-beam interactions.
\end{itemize}

The survey provides the coordinates in the lab frame of the two beams:
\begin{equation}
    \textbf{P}^\text{su} = \left(
    \begin{array}{c}
         x^\text{su}\\
         y^\text{su}\\
         s^\text{su}
    \end{array}\right)
\end{equation}
and the corresponding set of angles $(\theta^\text{su}, \phi^\text{su}, \psi^\text{su})$ defining the orientation of the local reference system used by the twiss\,\cite{madsite}. The origin and the orientation of the lab frame are defined by the first element in the sequence.

The components of the unit vectors defining the local reference frame with respect to the lab frame can be obtained from the following relationship:
\begin{multline}
\left(\hat{\textbf{e}}_x, \hat{\textbf{e}}_y, \hat{\textbf{e}}_s\right)=\\
\left(\begin{array}{ccc}
\cos \theta^\text{su} & 0 & \sin \theta^\text{su} \\
0 & 1 & 0 \\
-\sin \theta^\text{su} & 0 & \cos \theta^\text{su}
\end{array}\right)
\times
\left(\begin{array}{ccc}
1 & 0 & 0 \\
0 & \cos \phi^\text{su} & \sin \phi^\text{su} \\
0 & -\sin \phi^\text{su} & \cos \phi^\text{su}
\end{array}\right)
\times
\left(\begin{array}{ccc}
\cos \psi^\text{su} & -\sin \psi^\text{su} & 0 \\
\sin \psi^\text{su} & \cos \psi^\text{su} & 0 \\
0 & 0 & 1
\end{array}\right)   \, .
\end{multline}

The MAD-X twiss provides the transverse position of the beam in the local reference frame $(x^\text{tw}, y^\text{tw})$, so that the absolute position of the beam in the lab frame can written as
\begin{equation}
    \textbf{P} = \textbf{P}^\text{su} 
    + x^\text{tw} \hat{\textbf{e}}_x
    + y^\text{tw} \hat{\textbf{e}}_y
    \, .
\end{equation}

At the beam-beam locations the local reference frames for the two beams are assumed to be aligned. This is not strictly the case in the regions between the separation-recombination magnets (D1 and D2), but also in that case that case the existing small divergence can be considered negligible.
The beam-beam module of pymask checks the conditions:
\begin{align}
    ||\hat{\textbf{e}}_x^\text{b1} -  \hat{\textbf{e}}_x^\text{b2}|| \ll 1 \, ,\\
    ||\hat{\textbf{e}}_y^\text{b1} -  \hat{\textbf{e}}_y^\text{b2}|| \ll 1 \, .
\end{align}
Therefore we will simply define:
\begin{align}
    \hat{\textbf{e}}_x = \hat{\textbf{e}}_x^\text{b1} = \hat{\textbf{e}}_x^\text{b2} \, ,\\
    \hat{\textbf{e}}_y = \hat{\textbf{e}}_y^\text{b1} = \hat{\textbf{e}}_y^\text{b2} \, .
\end{align}

\subsection{Computation of beam-beam separations}
\label{sec:separ}

The beam-beam separations are defined as the transverse coordinates of the strong beam with respect to the weak beam. They can be computed as:
\begin{align}
    \Delta x &= \hat{\textbf{e}}_x \cdot \left( \textbf{P}^S -  \textbf{P}^W \right) \, ,\\
    \Delta y &= \hat{\textbf{e}}_y \cdot \left( \textbf{P}^S -  \textbf{P}^W \right) \, ,
\end{align}
where the superscripts identify the weak (W) and the strong (S) beam.

Typically the accuracy of the survey table is insufficient to computed the separations correctly, especially for elements that are too far from the first element in the sequence, due to accumulation of errors along the sequence. A correction can be computed looking as the apparent displacement of the closest Interaction Point (IP) between the two surveys, as the IPs are supposed to coincide.

\subsection{Crossing plane and crossing angle}
\label{sec:xing}

At the beam-beam encounters the local reference frames for the two beams share the same orientation. Therefore the elevation angle $\alpha$ of the crossing plane  and the crossing angle $\theta$ can be computed in the local reference frame, as will be illustrated in the following.

\begin{figure}[t]
\centering
\includegraphics[width=.8\textwidth, trim=0cm 1cm 0cm 1cm, clip]{figures/xang_xplane.png}
\caption{\small Schematic illustration of the crossing plane. \label{fig:xing}}
\end{figure}

\subsection{The crossing plane}

The directions defined by the local trajectories of the two beams are identified by the unit vectors
\begin{align}
\hat{\textbf{p}}^W &= \left(p^W_x, p^W_y, p^W_s\right) \, ,\\
\hat{\textbf{p}}^S &= \left(p^S_x, p^S_y, p^S_s\right) \, ,\
\end{align}
containing the angles of the closed orbit obtained from the twiss of the two beams.

The plane defined by these two directions is called Crossing Plane (XP), as illustrated in Fig.\,\ref{fig:xing}, and its equation is given by:
\begin{equation}
    \textbf{v}_\text{XP}(w_1, w_2) = w_1 \hat{\textbf{p}}^W +
    w_2 \hat{\textbf{p}}^S \, .
\label{eq:xpeq}
\end{equation}

The line defined by the intersection of the crossing plane and the transverse plane identified by the unit vectors $\hat{\textbf{e}}_x$  and $\hat{\textbf{e}}_y$ is given by the condition:
\begin{equation}
\textbf{v}_\text{XP}(w_1, w_2) \cdot \hat{\textbf{e}}_s=0 \, .
\label{eq:inters1}
\end{equation}

Replacing Eq.\,\eqref{eq:xpeq} into Eq.\,\eqref{eq:inters1} we obtain:
\begin{equation}
 w_1 p^W_s + w_2 p^S_s = 0 \, ,
\end{equation}

and replacing this condition in Eq.\,\eqref{eq:xpeq} we obtain the equation of the intersection line
\begin{equation}
    \textbf{v}_\text{T}(w_1) = w_1 \left(\hat{\textbf{p}}^W -
    \frac{p^W_{s}}{p^S_{s}} \hat{\textbf{p}}^S\right)
    \, .
\label{eq:vxy}
\end{equation}



The elevation angle $\alpha$ of the intersection line with respect to the local $x$-direction ($\hat{\textbf{e}}_x$) can be written as:
\begin{equation}
    \alpha=\arctan \frac
    {\textbf{v}_\text{T}\cdot\hat{\textbf{e}}_y}
    {\textbf{v}_\text{T}\cdot\hat{\textbf{e}}_x}
    \, .
\end{equation}

Using Eq.\,\eqref{eq:vxy} we obtain:
\begin{equation}
    \alpha=\arctan \cfrac
    {\left(p_y^W -
    \cfrac{p^W_{s}}{p^S_{s}} p_y^S\right)}
    {\left(p_x^W -
    \cfrac{p^W_{s}}{p^S_{s}} p_x^S\right)}
    \,. 
\end{equation}

In the paraxial approximation ($p^S_{s} \simeq p^W_{s}\simeq 1$) this simply becomes:
\begin{equation}
    \alpha=\arctan \cfrac
    {\Delta p_y}
    {\Delta p_x}
    \, ,
    \label{eq:alpha1}
\end{equation}
where we have defined:
\begin{align}
    \Delta p_x &= p_x^W - p_x^S \, ,\\
    \Delta p_y &= p_y^W - p_y^S \, .
\end{align}
In the legacy beam-beam macros as well as in the configuration pymask tool, the following logic is implemented
\begin{equation}
    \alpha = \begin{cases} 
    \arctan \left(\cfrac
    {\Delta p_y}
    {\Delta p_x}\right) &\mbox{if } \left|\Delta p_x\right| \geq \left|\Delta p_y\right| \\ 
    \cfrac{\pi}{2} - \arctan \left(\cfrac
    {\Delta p_x}
    {\Delta p_y}
    \right)&\mbox{if } \left|\Delta p_x\right| < \left|\Delta p_y\right| \end{cases}
    \, ,
\end{equation}
for which $\alpha$ is limited to the range:
\begin{equation}
     -\cfrac{\pi}{4}  \leq \alpha \leq \cfrac{3}{4}\pi
     \, .
\end{equation}

In particular, for a purely horizontal crossing we have $\alpha=0$ and for a purely vertical crossing we have $\alpha=\frac{\pi}{2}$.

\subsection{The crossing angle}
The crossing angle $\theta$ between the two beams can be found from the relation:
\begin{equation}
\cos\theta=\hat{\textbf{p}}^W\cdot
\hat{\textbf{p}}^S
\, .
\label{eq:dotprod}
\end{equation}

The half crossing angle 
\begin{equation}
    \phi = \frac{\theta}{2}
\end{equation}
is often used instead of $\theta$.

In the paraxial approximation
\begin{align}
    p_x \ll 1 \, ,\\
    p_y \ll 1 \, ,\\
\end{align}
the scalar product in Eq.\,\eqref{eq:dotprod} can be rewritten as
\begin{align}
    \hat{\textbf{p}}^W\cdot
\hat{\textbf{p}}^S &=
p^W_{x}p^S_{x}+p^W_{y}p^S_{y}+p^W_{s}p^S_{s}
\nonumber\\
&= p^W_{x}p^S_{x}+p^W_{y}p^S_{y} +\sqrt{1 - \left(p^W_{x}\right)^2 - \left(p^W_{y}\right)^2}
\sqrt{1 - \left(p^S_{x}\right)^2 - \left(p^S_{y}\right)^2}
\nonumber\\
&\simeq  p^W_{x}p^S_{x}+p^W_{y}p^S_{y}
+\left(1 - \cfrac{\left(p^W_{x}\right)^2}{2} - \cfrac{\left(p^W_{y}\right)^2}{2}\right)
\left(1 - \cfrac{\left(p^S_{x}\right)^2}{2} - \cfrac{\left(p^S_{y}\right)^2}{2}\right)
\nonumber\\
&\simeq p^W_{x}p^S_{x}+p^W_{y}p^S_{y}
+ 1 - \cfrac{\left(p^W_{x}\right)^2}{2} - \cfrac{\left(p^W_{y}\right)^2}{2} - \cfrac{\left(p^S_{x}\right)^2}{2} - \cfrac{\left(p^S_{y}\right)^2}{2}
\, ,
\end{align}
which can be written in compact form as:
\begin{equation}
\hat{\textbf{p}}^W\cdot
\hat{\textbf{p}}^S
\simeq
1 - \cfrac{
\left(p^W_{x} - p^S_{x}\right)^2 + 
\left(p^W_{y} - p^S_{y}\right)^2}{2}
\, .
\label{eq:dotappr}
\end{equation}
For small crossing angle we can write:
\begin{equation}
    \cos \theta \simeq 1-\cfrac{\theta^2}{2} \, .
    \label{eq:cosappr}
\end{equation}
Replacing Eqs.\,\eqref{eq:dotappr} and\,\eqref{eq:cosappr} into Eq.\,\eqref{eq:dotprod} we obtain
\begin{equation}
    \left| \theta \right| = 
    \sqrt{\Delta p_x^2 + \Delta p_y^2} \, .
\end{equation}

The sign of $\theta$ is defined positive when the weak beam needs to rotate in the clockwise sense in the crossing plane in order to be brought on the strong beam. This corresponds to the following sign choices:
\begin{center}
  \begin{tabular}{c|cc}
     &  $\left|\Delta p_x\right| > \left|\Delta p_y\right|$ & $\left|\Delta p_x\right| < \left|\Delta p_y\right|$\\
     \hline
 $\Delta p_x\geq0,\,   \Delta p_y \geq 0$  &$\theta>0$ &$\theta>0$\\
 $\Delta p_x < 0,\,   \Delta p_y \geq 0$  &$\theta<0$ &$\theta>0$\\
 $\Delta p_x < 0,\,   \Delta p_y < 0$  &$\theta<0$ &$\theta<0$\\
 $\Delta p_x\geq0,\,   \Delta p_y < 0$  &$\theta>0$ &$\theta<0$\\
\end{tabular}  
\end{center}
which are consistent with the sign convention used in LHC operation.

\subsection{Transformations for the counterclockwise beam (B4)}
\label{sec:b4}

The typically used MAD-X model of the LHC consists of two sequences both having clockwise (CW) orientation, conventionally called Beam 1 and Beam 2. To perform tracking simulations of the anticlockwise (ACW) beam, an anticlockwise sequence needs to be generated, which is conventionally called Beam~4. The beam-beam lenses in the Beam~4 sequence can be configured based on the beam-beam lenses defined in Beam~2, taking into account that the two are related by the following change of coordinates:
\begin{align}
    x^\text{ACW} &= -x^\text{CW} \, , \label{eq:b4x}\\
    y^\text{ACW} &= +y^\text{CW} \, , \\
    s^\text{ACW} &= -s^\text{CW} \, .
\end{align}

The corresponding transformation for the transverse momenta is:
\begin{align}
    p_x^\text{ACW} &= +p_x^\text{CW} \, ,\\
    p_y^\text{ACW} &= -p_y^\text{CW} \, .\label{eq:b4py}
\end{align}
This can be easily seen from the fact that:
\begin{align}
    p_x &\simeq \cfrac{dx}{ds} \, ,\\
    p_y &\simeq \cfrac{dy}{ds} \, .
\end{align}

Additionally, from Eqs.\,\eqref{eq:b4x} - \eqref{eq:b4py} it is possible to derive the following relations to transform the $\Sigma$-matrix\,\cite{bb6dnote} of the strong beam:
\begin{align}
    \Sigma_{11}^\text{ACW} &= +\Sigma_{11}^\text{CW} \, ,\\
    \Sigma_{12}^\text{ACW} &= -\Sigma_{12}^\text{CW} \, ,\\
    \Sigma_{13}^\text{ACW} &= -\Sigma_{13}^\text{CW} \, ,\\
    \Sigma_{14}^\text{ACW} &= +\Sigma_{14}^\text{CW} \, ,\\
    \Sigma_{22}^\text{ACW} &= +\Sigma_{22}^\text{CW} \, ,\\
    \Sigma_{23}^\text{ACW} &= +\Sigma_{23}^\text{CW} \, ,\\
    \Sigma_{24}^\text{ACW} &= -\Sigma_{24}^\text{CW} \, ,\\
    \Sigma_{33}^\text{ACW} &= +\Sigma_{33}^\text{CW} \, ,\\
    \Sigma_{34}^\text{ACW} &= -\Sigma_{34}^\text{CW} \, ,\\
    \Sigma_{44}^\text{ACW} &= +\Sigma_{44}^\text{CW} \, .\\
\end{align}

\subsection{Crab crossing}
\label{sec:crab}

To discuss the effect of crab cavities, we define along the bunches of Beam~1 and Beam~2 (sharing the same $s$ coordinate as in the MAD-X model), the longitudinal coordinates $z_1$ and $z_2$, oriented like $s$.

Assuming that the slices with $z_1 = z_2 = 0$ collide at s=0, the collision point (CP) for two generic slices $z_1$ and $z_2$ is at the location:
\begin{equation}
    s_\text{CP} = \frac{z_1 + z_2}{2} \, .
    \label{eq:cp}
\end{equation}

In the absence of crab crossing, the transverse position of the two beams is independent from $z$:
\begin{align}
    x_1 &= +\phi s \, ,\\
    x_2 &= -\phi s \, .
\end{align}

Ideal crab cavities, in the linear approximation, introduce a z-dependent orbit correction such that:
\begin{align}
    x_1(s) &= +\phi s + \phi_c z_1 \label{eq:x1} \, ,\\
    x_2(s) &= -\phi s - \phi_c z_2 \label{eq:x2} \, ,
\end{align}
where $\phi_c$ is the crabbing angle and we assume, without loss of generality, horizontal crabbing plane.

The separation of the two slices at their collision point is obtained replacing \eqref{eq:cp} into \eqref{eq:x1} and \eqref{eq:x2}:
\begin{equation}
    \Delta x(s_\text{CP}) = x_2(s_\text{CP}) - x_1(s_\text{CP}) = 
    - (\phi + \phi_c) (z_1 + z_2)  \, .
\end{equation}
If $\phi_c = -\phi$, the separation is zero independently of $z_1$ and $z_2$ (perfect crabbing).

The crab crossing in the IPs of the HL-LHC for the clockwise and anticlockwise beams is illustrated with the relevant sign conventions in Figs.\,\ref{fig:crab_ip1b1}\,-\,\ref{fig:crab_ip5b4}.

\begin{figure}[p]
\centering
\includegraphics[width=.87\textwidth, trim=0cm 0cm 0cm 0cm, clip]{figures/b1ip1.png}
\caption{\small Crab crossing in the IP1 of the HL-LHC modeled for the tracking of the clockwise beam (beam~1).  \label{fig:crab_ip1b1}}
\includegraphics[width=.87\textwidth, trim=0cm 0cm 0cm 0cm, clip]{figures/b4ip1.png}
\caption{\small Crab crossing in the IP1 of the HL-LHC modeled for the tracking of the anticlockwise beam (beam~4). \label{fig:crab_ip1b4}}
\end{figure}

\begin{figure}[p]
\centering
\includegraphics[width=.87\textwidth, trim=0cm 0cm 0cm 0cm, clip]{figures/b1ip5.png}
\caption{\small Crab crossing in the IP5 of the HL-LHC modeled for the tracking of the clockwise beam (beam~1).  \label{fig:crab_ip5b1}}
\includegraphics[width=.87\textwidth, trim=0cm 0cm 0cm 0cm, clip]{figures/b4ip5.png}
\caption{\small Crab crossing in the IP5 of the HL-LHC modeled for the tracking of the anticlockwise beam (beam~4). \label{fig:crab_ip5b4}}
\end{figure}

\subsubsection{Configuration of beam-beam lenses for beam 1}

In order to model the HO interaction for a crab crossing, the ``strong bunch'' is sliced longitudinally using the constant charge method, and one beam-beam lens for each slice is installed in the sequence.

In particular, in the sequence of beam 1, the lens corresponding to a slice of the strong beam (beam 2) having longitudinal coordinate $z_2=Z_2$ is installed at the location where the slice encounters the synchronous particle of the weak beam (see Eq.\,\eqref{eq:cp} with $z_1 = 0$):
\begin{equation}
    s_\text{lens} = +\frac{Z_2}{2}
    \, .
    \label{eq:slens}
\end{equation}


The position of the strong beam at the lens can be found replacing Eq.\,\eqref{eq:slens} into Eq.\,\eqref{eq:x2}:
\begin{equation}
   X_2 = - s_\text{lens} (\phi + 2 \phi_c)
   \, .
\end{equation}

The effect of the crab bump alone is given by:
\begin{equation}
   X^\text{crab}_2 = - 2 \phi_c s_\text{lens} = -\phi_c Z_2 \, .
\end{equation}

Taking into account the RF curvature coming from the crab cavity frequency, the position of the slice at the beam-beam lens can be written as:
\begin{equation}
   X^\text{crab}_2 = -\phi_c \frac{L_\text{ring}}{2 \pi h_\text{CC}} \sin\left(  \frac{2 \pi h_\text{CC}}{L_\text{ring}} Z_2\right) = -\phi_c \frac{L_\text{ring}}{2 \pi h_\text{CC}} \sin\left(  \frac{2 \pi h_\text{CC}}{L_\text{ring}} 2 s_\text{lens}\right)
   \, ,
   \label{eq:x2crab}
\end{equation}
where $h_\text{CC}$ is the harmonic number of the crab cavity and $L_\text{ring}$ is the circumference of the ring.

\subsection{Configuration of beam-beam lenses for beam 2}
In the sequence of beam 2, we install the beam-beam lens for a slice of the strong beam (beam 1) having longitudinal coordinate $z_1=Z_1$ at the location where the slice encounters the synchronous particle of the weak beam, (see Eq.\,\eqref{eq:cp} with $z_2 = 0$):
\begin{equation}
    s_\text{lens} = \frac{Z_1}{2}
    \, .
    \label{eq:slensb2}
\end{equation}

The position of the strong beam at the lens can be found replacing Eq.\,\eqref{eq:slensb2} into\,Eq.\,\eqref{eq:x1}:
\begin{equation}
   X_1 = s_\text{lens} (\phi + 2 \phi_c)
   \, .
\end{equation}

The effect of the crab bump alone is given by:
\begin{equation}
   X^\text{crab}_1 = 2 \phi_c s_\text{lens} = \phi_c Z_1
   \, .
\end{equation}

Taking into account the RF curvature coming from the crab cavity frequency, the position of the slice at the beam-beam lens can be written as:
\begin{equation}
   X^\text{crab}_1 = \phi_c \frac{L_\text{ring}}{2 \pi h_\text{CC}} \sin\left(  \frac{2 \pi h_\text{CC}}{L_\text{ring}} Z_1\right) = \phi_c \frac{L_\text{ring}}{2 \pi h_\text{CC}} \sin\left(  \frac{2 \pi h_\text{CC}}{L_\text{ring}} 2 s_\text{lens}\right)
   \, .
   \label{eq:x1crab}
\end{equation}

From Eqs.\,\eqref{eq:x2crab} and \eqref{eq:x1crab}, we find that for lenses at the same longitudinal position $s_\text{lens}$ the corresponding slices of the two beams $Z_1 = Z_2 = 2 s_\text{lens}$ have opposite transverse coordinates:
\begin{equation}
X^\text{crab}_1= -X^\text{crab}_2
\, .
\end{equation}

\subsubsection{Crab bump from twiss table}

For a non-ideal crabbing, for example in the presence of a non-closure of the crab-bump, the realistic $z$-dependent  orbit distortion introduced by the crab cavities can be characterized using the twiss, by installing orbit correctors at the position of the crab cavities that introduce the crab cavity deflection as seen at a certain reference position along the bunch $z_\text{ref}$. 
To obtain the effect on particles at different positions along bunch it is possible to apply the following scaling:
\begin{equation}
x\left(z\right) = x\left(z_\text{ref}\right)
 \cfrac
 {\sin\left(  \frac{2 \pi h_\text{CC}}{L_\text{ring}} z\right)}
 {\sin\left(  \frac{2 \pi h_\text{CC}}{L_\text{ring}} z_\text{ref}\right)} 
 \, .
\end{equation}

\subsection{Step-by-step configuration procedure}

Based on the method introduced in the previous sections, the following procedure has been implemented in pymask to configure the beam-beam lenses in the sixtrack and sixtracklib tracking model:

\begin{enumerate}
\item Inactive beam-beam lenses (not configured) are installed in both clockwise sequences (Beam~1 and Beam~2) at the locations of the HO and LR beam-beam encounters. As discussed in Sec.\,\ref{sec:crab}, at each IP a set of lenses is installed to model the HO, one corresponding to each bunch slice.
\item The MAD-X twiss and survey tables are computed 
for both clockwise sequences.
\item The transverse beam shapes ($\Sigma$-matrix) are extracted from the twiss table for all beam-beam lenses.
\item The positions of the beams at the beam-beam lenses in the lab frame are computed combining the information from the survey and twiss tables, as discussed in Sec.\,\ref{sec:posdir}.
\item The beam-beam separations are computed, as discussed in Sec.\,\ref{sec:separ}.
\item For all HO interactions, the crossing plane and the crossing angle are identified, as discussed in Sec.\,\ref{sec:xing}.
\item The relevant quantities for the beam-beam lenses in the anticlockwise sequences (Beam~3 and Beam~4) are obtained from the data computed for the lenses in the clockwise sequences (Beam~1 and Beam~2), using the transformations described in Sec.\,\ref{sec:b4}.
\item The effect of the crab cavities is introduced by using the shape of the crab bumps obtained from twiss tables computed with orbit correctors at the locations of the cavities, as discussed in Sec.\,\ref{sec:crab}.
\item The information computed before is used to configure the beam-beam lenses in the MAD-X model of the sequence for which the tracking simulation will be performed, typically either Beam 1 or Beam 4.
\item The SixTrack input and the pysixtrack/sixtracklib input files are generated  using the MAD-X model and the additional information computed as described above. 
\item The closed orbit as computed from the MAD-X sequences is saved on file, for the generation of matched beam distributions and for the computation of the beam-beam dipolar kicks on the closed orbit, which are usually subtracted in weak-strong tracking simulations.
\end{enumerate}


\chapter{Bhabha scattering and beamstrahlung}

\section{Bhabha scattering}
In quantum electrodynamics (QED), the Coulomb attraction of two opposite charges (e.g. an electron and a positron) is called Bhabha scattering~\cite{Griffiths:111880}. The mathematical treatment of Bhabha scattering can be done using the method of equivalent photons (Weizsäcker-Williams approach)~\cite{Weizsacker1934, Williams:1935dka}. The essence of this method lies in the fact that the electromagnetic field of a relativistic charged particle, say the positron, is almost transversal and can therefore accurately be substituted by an appropriately chosen equivalent radiation field of photons. Thus, the cross section for the scattering of an electron with this positron (Bhabha scattering) can be approximated by that of the electron and an "equivalent" photon (Compton scattering). In this case, the equivalent photon corresponds to the exchanged virtual photon between the scattering primaries. The whole process, including the subsequent emission of bremsstrahlung photons can be treated in a numerical simulation as an inverse Compton scattering process~\cite{Schulte:331845}. In this, the virtual photons emitted by the positron will collide with the electron. Due to the relativistic dynamics of the participating leptons, the virtual photons have an energy which is often negligible compared to that of the leptons, thus we can treat them as real. The process is called inverse since here the electron will lose energy while the photons will gain energy, contrary to standard Compton scattering. The scattered photons are real and typically end up with an energy $E'_{\gamma}$ comparable to the initial lepton energy $E_e$~\cite{ARUTYUNIAN1963176}.

The generation of photons from radiative Bhabha scattering in Xsuite can be divided into 3 steps. First, the charge density of the opposite bunch slice at the location of the macroparticle in the soft-Gaussian approximation is computed~\cite{Bassetti:122227}. From this one computes the integrated luminosity of the collision of the macroparticle with the virtual photons represented by the slice, integrated over the time of passing through the slice. Second, a set of virtual photons is generated corresponding to the total energy of the opposite slice. Third, the code iterates over these virtual photons and simulates the bremsstrahlung process as a series of inverse Compton scattering events between the macroparticle and each virtual photon. 

\subsection{Luminosity Computation} 

\begin{figure}[h]
	\centering
	\includegraphics[width=.6\textwidth]{figures/bhabha_1.png}
	\caption{\small Schematic illustration of a single macroparticle from bunch 1 (blue) colliding with a single longitudinal slice of the opposing bunch 2 (red).  \label{fig:bhabha_1}}
\end{figure}

Figure~\ref{fig:bhabha_1} illustratres how \texttt{Xfields} computes the integrated luminosity in a collision of a single macroparticle from one beam with a single slice of the opposing beam\footnote{Note that this luminosity can be recorded in a table with the \textbf{flag\_luminosity} flag of the \texttt{BeamBeamGaussian3D} element and \textbf{lumitable} keyword in the \texttt{Xline} internal log. The recorded entries must be summed up to get the total integrated luminosity of the collision. This method has an uncertainty of $\pm10\%$ compared to the analytical formula.}. On the figure $x,y$ denote the transverse coordinates of a macroparticle in the boosted and uncoupled frame, at the collision point with a slice of the opposing bunch, corresponding to the notation $\hat{\overline{x}}^*, \hat{\overline{y}}^* $ in the previous sections. The centroid (mean) coordinate of the opposing slice, with a bunch intensity of $N_{b,s}$, is denoted by $x_c,y_c$, in the boosted, uncoupled, transported reference frame of its own bunch. \texttt{Xfields} models the charge density of a longitudinal slice as a 2D Gaussian distribution $\rho(x,y)$. Considering an infinitesimal area $\delta x \delta y$ around the transverse position $x,y$ of a given macroparticle at the collision point with the slice, one can write the number of charges with which this macroparticle will interact:

\begin{equation}
	\begin{split}
		N_e(x,y) & = N_{b,s} \rho(x,y) \delta x \delta y,
	\end{split}
	\label{eq:bhabha_1}
\end{equation}

and the integrated luminosity of the macroparticle-slice collision:

\begin{equation}
	L = \frac{N_{b,m}\cdot N_e(x,y)}{\delta x \delta y} = N_{b,m} N_{b,s} \rho(x,y),
	\label{eq:bhabha_2}
\end{equation}


where $N_{b,m}$ denotes the number of elementary charges per macroparticle.

\subsection{Virtual Photon Generation}

Equation~\eqref{eq:bhabha_2} describes the integrated luminosity of primary-primary collisions. In order to simulate the collision of the primaries with virtual photons instead, \texttt{Xfields} uses the assumption that the virtual photon distribution $N_{\gamma}(x,y)$ is proportional to that of the primary charges:

\begin{equation}
	N_{\gamma}(x,y) = nN_e(x,y),
	\label{eq:bhabha_3}
\end{equation}

where $n$ is a proportionality factor denoting the number of virtual photons corresponding to one elementary charge. The number density spectrum of virtual photons is given by:

\begin{equation}
	\frac{dn}{dxdQ^2} = \frac{\alpha}{2\pi}\frac{1 + (1 - x)^2}{x}\frac{1}{Q^2},
	\label{eq:bhabha_4}
\end{equation}

where $\displaystyle x=\frac{\hbar\omega}{E_e}=\frac{E_\gamma}{E_e}$ is the total energy of the virtual photon normalized to the primary energy and $Q^2$ is the squared virtuality of the virtual photon~\cite{Halzen:1984mc}. 

The virtual photon energies and virtualities can be drawn using the method of inverse CDF (Cumulative Distribution Function) sampling. The sampling algorithm in \texttt{Xsuite} has been adapted from \texttt{GUINEA-PIG}~\cite{guineapig}, a Particle In Cell (PIC) based single beam-beam collision simulation software. For each macroparticle in the beam, we first compute the total amount of equivalent photons using the energy of the opposite bunch slice. Subsequently, the energy and virtuality of each photon will be sampled. In the current implementation all virtual photons inherit the dynamical variables of the strong bunch slice centroid. Note that the virtual photons sampled this way will also be "macroparticles" in the sense that they represent the dynamics of all virtual photons generated by all charges in a primary macroparticle.

\subsection{Inverse Compton Scattering of Virtual Photons}

We account for the proportionality of the primary charge and virtual photon distributions described by Eq.~\eqref{eq:bhabha_3} by resampling the virtual photons for each macroparticle. With each photon, we simulate the bremsstrahlung process in the form of a set of inverse Compton scattering events. The number of Compton events can be described as:

\begin{equation}
	R = \sigma_{C,tot}(s)L = \sigma_{C,tot}(s) N_{b,m} N_{b,s} \rho(x,y),
	\label{eq:bhabha_5}
\end{equation}
where $\displaystyle s\approx\frac{4E_\gamma E_e}{m_e^2c^4}$ is the center of mass energy squared of the photon-primary Compton interaction, normalized to the rest mass of the primary~\cite{GINZBURG198347}, and $\sigma_{C,tot}(s)$ denotes the total Compton scattering cross section, given by:
\begin{equation}
	\begin{split}
		\sigma_{C,tot}(s) & = \frac{2\pi r_e^2}{s}\left[\mathrm{ln}(s+1)\left(1 - \frac{4}{s} - \frac{8}{s^2}\right).+ \frac{1}{2} + \frac{8}{s} - \frac{1}{2(s+1)^2}\right],
	\end{split}
	\label{eq:bhabha_6}
\end{equation}
with $\displaystyle r_e$ being the classical electron radius. For each event, we sample the scattered photon energy from the differential cross section:
\begin{equation}
	\begin{split}
		\frac{d\sigma_C}{dy} = \frac{2\pi r_e^2}{s}\left[ \frac{1}{1-y} + 1 - y - \frac{4y}{s(1-y)} + \frac{4y^2}{s^2(1-y)^2} \right],
	\end{split}
	\label{eq:bhabha_7}
\end{equation}
which describes the scattering of a beam of unpolarized photons on the primary charge~\cite{Schulte:331845}. Here $\displaystyle y=\frac{\hbar\omega'}{E_e}=\frac{E_\gamma'}{E_e}$ is the energy of the scattered photon in units of the total energy of the colliding primary. Given the energy $E_\gamma'$, we can compute the scattering angle of the primary and the photon as well as their momenta, using the constraints given by energy and momentum conservation. While the emitted photon spectrum corresponds to the sum of all charges represented by a macroparticle, a given macroparticle should represent the dynamics of a single primary charge. Thus, the dynamical variables of the macroparticles are updated according to energy and momentum conservation accounting for the emission of only a fraction of the photons. The latter are picked randomly based on a probability corresponding to the inverse of the number of charges per macroparticle. 

\section{Beamstrahlung}
The implementation of beamstrahlung in \texttt{Xfields} is based on \texttt{GUINEA-PIG}~\cite{guineapig}. In this section a high level summary of the modeling is presented. Further details can be found in~\cite{Yokoya:1985ab}. 

\texttt{Xfields} samples the quantum theoretical synchrotron radiation spectrum $G(v,\xi)$:

\begin{equation}
G(v,\xi) = \frac{v^2}{(1 - (1 - \xi)v^3)^2}\left( G_1(y) + \frac{\xi^2 y^2}{1 + \xi y}G_2(y) \right),
 	\label{eq:bs_1}
\end{equation}

which is normalized such that $G(v=0,\xi)=1$ and $G(v,\xi)\leq1$ for all $v$ and $\xi$. The variable $\xi$ is defined as:

\begin{equation}
	\xi=\frac{E_{crit}}{E} 
	\label{eq:bs_2}
\end{equation}

and denotes the magnitude of the quantum correction, i.e. the critical energy normalized to the energy $E$ of the primary particle in GeV undergoing the beamstrahlung process. The critical beamstrahlung energy is defined in the classical way as:

\begin{equation}
 E_{crit}=\frac{3\hbar c\gamma^3}{2\rho}
	\label{eq:bs_3}
\end{equation}

The unitless variable $y$ is related to the energy of the emitted beamstrahlung photon $E_{\gamma}$:

\begin{equation}
	y = \frac{E_{\gamma}}{E_{crit}} \frac{1}{1 - \frac{E_{\gamma}}{E}}.
	\label{eq:bs_4}
\end{equation}

Equation~\ref{eq:bs_4} can be expressed with the help of a uniform random variable $v$ as follows:

\begin{equation}
	y = \frac{v^3}{1 - v^3};~v\in U[0,1].
	\label{eq:bs_5}
\end{equation}

With these the number of beamstrahlung photons emitted in the interval [$v$, $v+\Delta v$] during a time interval $\delta_t$ can be given as:

\begin{equation}
    \Delta N_{\gamma} = p_0G(v, \xi)\Delta v,
	\label{eq:bs_6}
\end{equation}

where

\begin{equation}
p_0 = \frac{2^{\frac{2}{3}}}{\Gamma(\frac{4}{3})} \frac{\alpha\gamma\delta_t}{\rho}\approx 25.4\cdot \frac{E \delta_t}{\rho}
	\label{eq:bs_7}
\end{equation}

is a scaling factor dependent on the relativistic $\gamma$ of the primary, the instantaneous bending radius $\rho$ and the fine structure constant $\alpha$. The bending radius of each macroparticle in the electromagnetic field of a given longitudinal slice of the opposite bunch is obtained from the radial kick:

\begin{equation}
	F_r^*=r_{pp}\sqrt{F_x^{*2} + F_y^{*2}},
	\label{eq:bs_8}
\end{equation}
\begin{equation}
	\rho=\frac{1}{F_r^*},
	\label{eq:bs_9}
\end{equation}

with $r_{pp}=\frac{1}{1 + \delta}=\frac{p}{p_0}$. In the \texttt{Xfields} \texttt{BeamBeamGaussian3D} element the time interval $\delta_t$ is expressed as a longitudinal distance $\Delta z$, which is the distance the macroparticle travels between two consecutive longitudinal slices and it corresponds to the bin width of the longitudinal slicing.

Figure~\ref{fig:bs_1} shows the beamstrahlung photon number density $p_0G(v,\xi)$ for a fixed value of $p_0$ and $\xi$. The area in the region C is the mean number of beamstrahlung photons emitted during an interval $\delta_t$, i.e. a passage through one longitudinal slice of width $\Delta z$.

\begin{figure}[h]
	\centering
	\includegraphics[width=.5\textwidth]{figures/bs_1.png}
	\caption{\small Schematic illustration of the number density function of beamstrahlung photons $p_0G(v,\xi)$ (red curve) for a given $p_0$ (blue dashed line) and $\xi$, as a function of $v$.  \label{fig:bs_1}}
\end{figure}

The functions $G_1(y)$ and $G_2(y)$ are defined as follows:

\begin{equation}
	\begin{split}
		G_1(y) & = \frac{\sqrt{3}\Gamma(\frac{1}{3})}{2^{\frac{5}{3}}\pi} \int\limits_{y}^{\infty} K_{\frac{5}{3}}(x) dx, \\
		G_2(y) & = \frac{\sqrt{3}\Gamma(\frac{1}{3})}{2^{\frac{5}{3}}\pi}  K_{\frac{2}{3}}(y).
	\end{split}
 	\label{eq:bs_10}
\end{equation}

Equations~\ref{eq:bs_10} are evaluated numerically with the below approximate formulas:

\begin{equation}
	\begin{split}
	0 \leq y \leq 1.54 & \\
	G_1(y) & = y^{-\frac{2}{3}} (1 - 0.8432885317\cdot y^{\frac{2}{3}} + 0.1835132767\cdot y^2 \\
	& - 0.0527949659\cdot y^{\frac{10}{3}} + 0.0156489316\cdot y^4) \\
	G_2(y) & = y^{-\frac{2}{3}} (0.4999456517 - 0.5853467515\cdot y^{\frac{4}{3}} \\
	& + 0.3657833336\cdot y^2 - 0.0695055284\cdot y^{\frac{10}{3}} + 0.0191803860\cdot y^4) 	\end{split}
\label{eq:bs_11}
\end{equation}
\begin{equation}
	\begin{split}
	1.54 < y \leq 4.48 & \\	
	G_1(y) & = \frac{2.066603927 - 0.5718025331\cdot y + 0.04243170587\cdot y^2}{-0.9691386396 + 5.651947051\cdot y - 0.6903991322\cdot y^2 + y^3} \\
	G_2(y) & = \frac{1.8852203645 - 0.5176616313\cdot y + 0.03812218492\cdot y^2}{-0.4915880600 + 6.1800441958\cdot y - 0.6524469236\cdot y^2 + y^3}
	\end{split}
\label{eq:bs_12}
\end{equation}

\begin{equation}
\begin{split}
	4.48 < y \leq 165.0 & \\	
	G_1(y) & = 	\frac{e^{-y}}{\sqrt{y}}\cdot \frac{1.0174394594 + 0.5831679349\cdot y}{0.9949036186 + y} \\
	G_2(y) & = \frac{e^{-y}}{\sqrt{y}}\cdot \frac{0.2847316689 + 0.5830684600\cdot y}{0.3915531539 + y}.
	\end{split}
 	\label{eq:bs_13}
\end{equation}

For $y>165$ the model assumes no radiation. With these one can simulate beamstrahlung emission by first drawing a random uniform number $p$. The condition $p>p_0$ corresponds to region A on Fig.~\ref{fig:bs_1}, therefore no photons are emitted. In the other case a second random uniform number $v$ is drawn, and Eq.~\ref{eq:bs_1} is computed. If $p<p_0G(v,\xi)$ is satisfied (region C) a photon is emitted with an energy

\begin{equation}
\frac{E_\gamma}{E} = \frac{\xi v^3}{1 - (1 - \xi)v^3},
	\label{eq:bs_14}
\end{equation}

otherwise no photon is emitted (region B). The generated beamstrahlung photons are themselves macroparticles in the sense that they represent the dynamics of all photons generated by all charges in a primary macroparticle.
\newpage


\chapter{Wakefields and impedances}

\section{Transverse wakefields}

Transverse wakefields are defined such that the corresponding transverse kicks can be written as:
\begin{align}
    \Delta p_x = 
     & \frac{q^2 e^2}{m_0 \gamma \beta_0^2 c^2}
     \sum_{i,j,k,l \geq 0} 
        x^k y^l\int_{-\infty}^\infty
        \bar{x}^i(z')\bar{y}^j(z')\lambda(z')W^{i,j,k,l}_{x}(z - z') \, dz'
        \label{eq:wake_kick_x}\\
    %%%%%%
    \Delta p_y =
     & \frac{q^2 e^2}{m_0 \gamma \beta_0^2 c^2}
     \sum_{i,j,k,l \geq 0} 
        x^k y^l\int_{-\infty}^\infty
        \bar{x}^i(z')\bar{y}^j(z')\lambda(z')W^{i,j, k, l}_{y}(z - z') \, dz'
        \label{eq:wake_kick_y}
\end{align}
where $\bar{x}(z)$ and $\bar{y}(z)$ are the transverse centroid positions along the beam.

The convolution can be obtained numerically using the method of Section \ref{sec:fftconv1d}.



The lower order terms of the summation are often called as:
\begin{itemize}
    \item $W_x^{0,0,0,0}$ constant x
    \item $W_y^{0,0,0,0}$ constant y
    \item $W_x^{1,0,0,0}$ dipolar $x$, or driving $x$
    \item $W_y^{0,1,0,0}$ dipolar $y$, or driving $y$ 
    \item $W_x^{0,1,0,0}$ dipolar $xy$, or driving $xy$
    \item $W_y^{1,0,0,0}$ dipolar $yx$, or driving $yx$
    \item $W_x^{0,0,1,0}$ quadrupolar $x$, or detuning $x$
    \item $W_y^{0,0,0,1}$ quadrupolar $y$, or detuning $y$
    \item $W_x^{0,0,0,1}$ quadrupolar $xy$, or detuning $xy$
    \item $W_y^{0,0,1,0}$ quadrupolar $yx$, or detuning $yx$
\end{itemize}

The $z$ variable can be written as a function of time in the lab frame as:
\begin{equation}\label{eq:z}
    z = -\beta_0 c t,
\end{equation}

We call $\widehat{W}_{x,y}$ the wakefield defined as a function of time $t$:
\begin{align}
    &\widehat{W}_{x,y}(t) = {W}_{x,y}\left(-\beta_0 c t\right) \label{eq:wake_vs_t}\\
    &W_{x,y}(z) = \widehat{W}_{x,y}\left(-\frac{z}{\beta_0 c}\right) \label{eq:wake_vs_z}
\end{align}

For ultrarelativistic beams we have:

\begin{align}
    &\widehat{W}_{x,y}(t) = 0 \quad &\text{for } t < 0\\
    &W_{x,y}(z) = 0  \quad &\text{for } z > 0
\end{align}

The coefficient $1/\gamma\beta_0^2$ in Eqs.\,\ref{eq:wake_kick_x} and\,\ref{eq:wake_kick_y} comes from the fact that, if we have a force $F_x$ acting on a length $\Delta s$, we can derive the corresponding kick as follows:
\begin{equation}
    \Delta P_{x} = F_{x} \Delta t,
\end{equation}
and substituting the $P_{x}$ with the normalized one $p_{x} = \frac{P_{x}}{P_0}$ we find
\begin{equation}
    \Delta p_{x} = \frac{F_{x}}{P_0} \Delta t = \frac{F_{x}}{P_0}\frac{\Delta s }{\beta_0 c},
\end{equation}
hence substituting $P_0 = m_0 \gamma \beta_0 c$ we find
\begin{equation}
    \Delta p_{x} = \frac{F_{x}}{m_0 \gamma \beta_0^2 c^2} \Delta s.
\end{equation}

\section{Transverse impedances}
The transverse beam coupling impedances $Z_{x,y}(\omega)$ are related to the wakefields through a Fourier transform (see \cite[Eq. 1.216]{Mounet:Thesis}):
\begin{align}
    &\widehat{W}_{x,y}(t) = -\frac{j}{2\pi}\int_{-\infty}^{+\infty} d\omega \ e^{j\omega t} Z_{x,y}(\omega)~,\label{eq:wake_from_imp}\\
    &Z_{x,y}(\omega) = j\int_{-\infty}^{+\infty} dt \ e^{-j\omega t} \widehat{W}_{x,y}(t)~.\label{eq:imp_from_wake}
\end{align}
The equivalence between the two equations can be seen multiplying both sides of Eq.\,\ref{eq:wake_from_imp} by $e^{-j\omega' t}$ and integrating over $t$
\begin{align}
    \int_{-\infty}^{\infty}dt \ e^{-j\omega' t}\widehat{W}_{x,y}(t) &= -\frac{j}{2\pi}\int_{-\infty}^{\infty}dt \ \int_{-\infty}^{+\infty} d\omega Z_{x,y}(\omega)e^{j(\omega-\omega') t}\\
    &= -\frac{j}{2\pi} \int_{-\infty}^{+\infty} d\omega \ Z_{x,y}(\omega) \int_{-\infty}^{\infty}dt \ e^{j(\omega-\omega') t}.
\end{align}
We now apply the following property of the Dirac $\delta$ function
\begin{equation}
    \delta(\omega-\omega') =  \frac{j}{2\pi} \int_{-\infty}^{\infty} dt \ e^{j(\omega-\omega') t},
\end{equation}
and we find
\begin{equation}
    \int_{-\infty}^{\infty}dt \ e^{-j\omega' t}\widehat{W}_{x,y}(t) = -j \int_{-\infty}^{+\infty} d\omega \ Z_{x,y}(\omega) \delta(\omega-\omega') = -j Z_{x, y}(\omega').
\end{equation}

We can write the impedances also in terms of the wakefield expressed as a function of $z= -\beta_0 c t$ (using Eqs.\,\ref{eq:wake_vs_t} and\,\ref{eq:wake_vs_z}):
\begin{align}
    &W_{x,y}(z) = -\frac{j}{2\pi}\int_{-\infty}^{+\infty} d\omega \ e^{-j\omega \frac{z}{\beta_0 c}} Z_{x,y}(\omega)\, ,\\
    &Z_{x,y}(\omega) = \frac{j}{\beta_0 c} \int_{-\infty}^{+\infty} \ dz e^{j \omega \frac{z}{\beta_0 c}} W_{x,y}(z)\, .
\end{align}

As the transverse wakes are real functions, the impedances satisfy the following symmetry properties:
\begin{align}
&\operatorname{Re}\left\{ Z_{x,y}( - \omega) \right\}
=
-\operatorname{Re}\left\{ Z_{x,y}( \omega) \right\}\\
&\operatorname{Im}\left\{ Z_{x,y}( -\omega) \right\}
=
\operatorname{Im}\left\{ Z_{x,y}( \omega) \right\}
\end{align}

\section{Longitudinal wakefield}

Longitudinal wakefields are defined such that the corresponding kicks can be written as:
\begin{equation}
    \Delta \delta = -\frac{q^2 e^2}{m_0 \gamma \beta_0^2 c^2} \int_{-\infty}^{+\infty} dz' \lambda(z') W_s(z-z') ~ .
\end{equation}
The minus sign is introduced such that a positive wake causes the particles to lose energy. The convolution can be obtained numerically using the method of Section \ref{sec:fftconv1d}.

Also in this case, we call $\widehat{W}_{s}$ the wakefield defined as a function of time $t$:
\begin{align}
    &\widehat{W}_{s}(t) = {W}_{s}\left(\beta_0 c t\right) \label{eq:wakel_vs_t}\\
    &W_s(z) = \widehat{W}_{s}\left(-\frac{z}{\beta_0 c}\right) \label{eq:wakel_vs_z}
\end{align}

\section{Longitudinal impedance}

The longitudinal beam coupling impedances $Z_{s}(\omega)$ are related to the wakefields through a Fourier transform (see \cite[Eq. 1.216]{Mounet:Thesis}):
\begin{align}
    &\widehat{W}_s(t) = \frac{1}{2\pi}\int_{-\infty}^{+\infty} d\omega \ e^{j\omega t} Z_{s}(\omega)~,\label{eq:wake_from_imp_long}\\
    &Z_s(\omega) = \int_{-\infty}^{+\infty} dt \ e^{-j\omega t} \widehat{W}_{s}(t)~.\label{eq:imp_from_wake_long}
\end{align}

We can write the impedances also in terms of the wakefield expressed as a function of $z= -\beta_0 c t$ (using Eqs.\,\ref{eq:wakel_vs_t} and\,\ref{eq:wakel_vs_z}):
\begin{align}
    &W_{s}(z) = \frac{1}{2\pi}\int_{-\infty}^{+\infty} d\omega \ e^{-j\omega \frac{z}{\beta_0 c}} Z_{s}(\omega)\, ,\\
    &Z_{s}(\omega) = \frac{1}{\beta_0 c} \int_{-\infty}^{+\infty} \ dz e^{j \omega \frac{z}{\beta_0 c}} W_{s}(z)\, .
\end{align}

As the longitudinal wake is a real function, the conrresponding impedance satisfies the following symmetry properties:
\begin{align}
&\operatorname{Re}\left\{ Z_{s}( - \omega) \right\}
=
\operatorname{Re}\left\{ Z_s( \omega) \right\}\\
&\operatorname{Im}\left\{ Z_{s}( -\omega) \right\}
=
-\operatorname{Im}\left\{ Z_{s}( \omega) \right\}
\end{align}

\section{Analytical wakes}
\subsection*{Resonator}
Ultra-relativistic resonator with shunt impedance $R$, quality factor $Q>1$ and resonant frequency $f_r$
\begin{itemize}
    \item Longitudinal:
    \begin{align}
        W_{s}(t) &= \frac{\omega_r R}{Q} e^{-\alpha t} \left(
                   \cos(\widehat{\omega}_r t) -
                   \frac{\alpha}{\widehat{\omega}_r} \sin(\widehat{\omega}_r t)\right)~,\\
        Z_{s}(\omega) &= \dfrac{R}{1 - jQ\left(\frac{\omega_r}{\omega} - \frac{\omega}{\omega_r}\right)}~.
    \end{align}
    \item Transverse:
    \begin{align}
        W_{x,y}(t) &= \frac{\omega_r^2 R }{Q \widehat{\omega}_r} e^{-\alpha t}\sin(\widehat{\omega}_r t)~,\\
        Z_{x,y}(\omega) &= \frac{\omega_r}{\omega} \dfrac{R}{1 - jQ\left(\frac{\omega_r}{\omega} - \frac{\omega}{\omega_r}\right)}~.
    \end{align}
\end{itemize}
where $\omega_r = 2\pi f_r$, $\widehat{\omega}_r = \omega_r \sqrt{1-\frac{1}{4Q^2}}$, $\alpha=\frac{\omega_r}{2Q}$. Ref: \cite[Section 2.2]{Mounet:Thesis}

\subsection*{Cylindrical thick wall}
Cylindrical resistive wall wake, based on the "classic thick wall formula" (see e.g. \cite[Chapter 2]{Chao:CollectiveInst}) with resistivity $\rho$, permeability $\mu$, radius r and length L:
\begin{itemize}
    \item Longitudinal:
    \begin{align}
        W_{s}(t) &= -L \frac{1}{4\pi r}
                    \sqrt{\frac{Z_0 \rho}{\pi c}}
                    t^{-\frac{3}{2}}~,\\
        Z_{s}(\omega) &= L \left(1 + j~\text{sign}(\omega) \right) \frac{\rho}{2\pi r} \frac{1}{\delta_s(\omega)}~,
    \end{align}
    where $\delta_s(\omega) = \sqrt{\frac{2 \rho}{|\omega|}}$ is the frequency-dependent skin depth.\\
    Note: the longitudinal resistive wall wake is negative for any value of $t$, while, with the adopted sign convention, longitudinal wakes should be positive for $t \rightarrow 0+$. This happens because the thick wall approximation is not valid for small $t$. Computing the wake numerically with IW2D shows that the resistive wall wake is actually positive close to zero and performs a few oscillations after which it agrees very well with the formula above. For applications where short range effects are relevant this model should not be used.
    \item Transverse:
    \begin{align}
        W_{x,y}(t) &= L \frac{1}{\pi r^3} \sqrt{\frac{c Z_0 \rho}{\pi}} t^{-\frac{1}{2}} ~,\\
        Z_{x,y}(\omega) &= L (1+j~\text{sign}(\omega))
                    \frac{\rho}{\pi r^3} \frac{1}{\omega \sqrt{\varepsilon_0 \mu_0}}
                    \frac{1}{\delta_s(\omega)}~.
    \end{align}    
\end{itemize}


\chapter{Intra-Beam Scattering}

Intra-beam scattering (IBS) is the process of small angle, multiple Coulomb scattering of charged particles within the beam.
It leads to a redistribution of the particle momenta in six-dimentional phase space.

\section{Analytical Growth Rates}
\label{section:ibs_growth_rates}

Theoretical models commonly characterize the effect of intra-beam scattering through growth rates, or growth times.
The former is expressed in \(\left[s^{-1}\right]\)) and the latter in \(\left[s\right]\).
These govern the evolution of the rms beam sizes or rms emittances of the beam, depending on the convention used.
\newline

Growth rates (and growth times) can be expressed in either amplitude or emittance convention.
The former governs the evolution of rms beam sizes while the latter governs that of rms emittances.
The two conventions are equivalent by a factor \(2\) and conversion can be done as:

\begin{figure}[!htb]
    \centering
    \begin{tikzpicture}
        \matrix (m) [matrix of math nodes,row sep=8em,column sep=6em,minimum width=2em]{
            \textrm{Emit. Times}  &  \textrm{Amp. Times}  \\
            \textrm{Emit. Rates}  &  \textrm{Amp. Rates}  \\
        };
        \path[-stealth]
        % These two go between emit. times and amp. times (top nodes)
        (m-1-2) edge[bend right=10] node [above] {\( \div 2 \)} (m-1-1)  % amp. times to emit. times
        (m-1-1) edge[bend right=10] node [below] {\( \times 2 \)} (m-1-2)  % emit. times to amp. times
        % These two go between emit. rates and amp. rates (bottom nodes)
        (m-2-2) edge [bend right=10] node [above] {\( \times 2 \)} (m-2-1)  % amp. rates to emit. rates
        (m-2-1) edge [bend right=10] node [below] {\( \div 2 \)} (m-2-2)  % emit. rates to amp. rates
        % These two go between emit. times and emit. rates (left nodes)
        (m-1-1) edge[bend left=15] node [left, rotate=-90, anchor=center, yshift=8pt] {\( inverse \)} (m-2-1)  % emit. times to emit. rates
        (m-2-1) edge[bend left=15] node [right, rotate=90, anchor=center, yshift=8pt] {\( inverse \)} (m-1-1)  % emit. rates to emit. times
        % These two go between amp. times and amp. rates (right nodes)
        (m-1-2) edge[bend left=15] node [left, rotate=-90, anchor=center, yshift=8pt] {\( inverse \)} (m-2-2)  % amp. times to amp. rates
        (m-2-2) edge[bend left=15] node [right, rotate=90, anchor=center, yshift=8pt] {\( inverse \)} (m-1-2);  % amp. rates to amp. times
    \end{tikzpicture}
    \caption{Illustration of the conversions between IBS growth rates and times, across emittance and amplitude conventions.}
    \label{figure:ibs_convention_conversion}
\end{figure}

The growth rates themselves are expressed from the lattice optics as well as the beam properties.

\textbf{In Xsuite}, for consistency with synchrotron radiation damping times (see section ~\ref{section:sr_damping_times}), \textbf{the amplitude growth rates are computed}.
The horizontal (\(K_x\)), vertical (\(K_y\)) and longitudinal (\(K_z\)) amplitude growth rates are defined as:

\begin{equation}
    \begin{aligned}
        K_x = \frac{1}{\tau_x} &= \frac{1}{\varepsilon^{1/2}_x} \frac{d \varepsilon^{1/2}_x}{dt} \text{,  } \\
        K_y = \frac{1}{\tau_y} &= \frac{1}{\varepsilon^{1/2}_y} \frac{d \varepsilon^{1/2}_y}{dt} \text{,  } \\
        K_z = \frac{1}{\tau_z} &= \frac{1}{\varepsilon^{1/2}_z} \frac{d \varepsilon^{1/2}_z}{dt} \text{, }
    \end{aligned}
    \label{equation:ibs_rms_emittances_evolutions}
\end{equation}
where (\(\tau_x\)), (\(\tau_y\)) and (\(\tau_z\)) are the amplitude growth times.
\newline

Currently two different formalism are available to compute these growth rates.
Both assume transverse and longitudinal Gaussian bunch profiles.
Both rely on the computation of the Coulomb logarithm \(L_C\), which in \textit{xfields} is computed according to the expression in the Physics Vade Mecum~\cite{AIP:Anderson:Physics_Vade_Mecum}:

\begin{equation}
    L_C = \ln \left( \frac{r_{max}}{r_{min}} \right) \text{ .}
    \label{equation:coulomb_logarithm}
\end{equation}

In Eq~\eqref{equation:coulomb_logarithm} \(r_{max}\) is taken as the smaller of \(\sigma_x\) and the Debye length, while \(r_{min}\) is taken as the larger of the classical distance of closest approach and the quantum diffraction limit from the nuclear radius.

\subsection{Nagaitsev Formalism}
\label{subsection:nagaitsev_formalism}

One available formalism follows the approach introduced by S.~Nagaitsev in~\cite{PRAB:Nagaitsev:IBS_formulas_fast_numerical_evaluation}.
It provides a fast computation method through symmetric elliptic integrals of the second kind, \(R_D(x,y,z)\), defined as:

\begin{equation}
    R_D(x, y, z) = \frac{3}{2} \int_{0}^{\infty} \frac{dt}{\sqrt{(t + x)(t + y)(t + z)^3}} \text{ .}
    \label{equation:elliptic_integrals}
\end{equation}
\newline

Interestingly, this elliptic integral has the following special properties:

\begin{equation}
    R_D(x, x, x) = x^{-3/2} \text{,  }
    \label{equation:RD_property1}
\end{equation}

\begin{equation}
    R_D(x, y, z) + R_D(y, z, x) + R_D(z, x, y) = \frac{3}{\sqrt{xyz}} \text{ .}
    \label{equation:RD_property2}
\end{equation}

Thanks to Eq~\eqref{equation:RD_property2} only two evaluations of this integral are needed to obtain various simple terms from which one can compute the growth rates.
Importantly, the computation time of this integral in Nagaitsev's approach does not scale with the size of the lattice.
\newline

First the \(a_x, a_y, a_s, a_1\) and \(a_2\) terms are computed:

\begin{equation}
    \begin{aligned}
        a_x &= \frac{\beta_x}{\varepsilon_x} \text{,  }
        a_y = \frac{\beta_y}{\varepsilon_y} \text{,  }
        a_s = a_x \left( \frac{D_x^{2}}{\beta_x^{2}} + \Phi_x^2 \right) + \frac{1}{\sigma_p^{2}} \text{,  } \\
        a_1 &= \frac{1}{2} (a_x + \gamma^2 a_s) \text{,  }
        a_2 = \frac{1}{2} (a_x - \gamma^2 a_s) \text{ ,}
    \end{aligned}
    \label{equation:nagaitsev_step1}
\end{equation}
where the \(\Phi_{x,y}\) term is defined as:

\begin{equation}
    \Phi_{x,y} = D_{x,y}^{\prime} - \frac{\beta_{x,y}^{\prime} D_{x,y}}{2 \beta_{x,y}} \text{ .}
\end{equation}

Then the \(\lambda_1, \lambda_2\) and \(\lambda_3\) terms are computed:

\begin{equation}
    \lambda_1 = a_y \text{ , }
    \lambda_2 = a_1 + \sqrt{a_2^{2} + \gamma^2 a_x^{2} \Phi_x^2} \text{ , }
    \lambda_3 = a_1 - \sqrt{a_2^{2} + \gamma^2 a_x^{2} \Phi_x^2} \text{ .}
    \label{equation:nagaitsev_lambdas}
\end{equation}
and used to compute three integrals \(R_1, R_2\) and \(R_3\) (though with Eq~\eqref{equation:RD_property2} only two need to be computed):

\begin{equation}
    \begin{aligned}
        R1 = \frac{1}{\lambda_1} R_D(\frac{1}{\lambda_2}, \frac{1}{\lambda_3}, \frac{1}{\lambda_1}) \text{ ,} \\
        R2 = \frac{1}{\lambda_2} R_D(\frac{1}{\lambda_3}, \frac{1}{\lambda_1}, \frac{1}{\lambda_2}) \text{ ,} \\
        R3 = \frac{1}{\lambda_3} R_D(\frac{1}{\lambda_1}, \frac{1}{\lambda_2}, \frac{1}{\lambda_3}) \text{ .}
    \end{aligned}
    \label{equation:nagaitsev_r1r2r3}
\end{equation}

Using all the above the \(S_p, S_x\) and \(S_{xp}\) terms are computed according to Eq~\eqref{equation:nagaitsev_spsxsxp}:

\begin{equation}
    \begin{aligned}
        S_p &= \frac{\gamma^2}{2} \left[ 2 R_1 - R_2 \left(1 - \frac{3 a_2}{\sqrt{a_2^{2} + \gamma^2 a_x^{2} \Phi_x^2}}\right) - R_3 \left(1 + \frac{3 a_2}{\sqrt{a_2^{2} + \gamma^2 a_x^{2} \Phi_x^2}}\right) \right] \text{ ,} \\
        S_x &= \frac{1}{2} \left[ 2 R_1 - R_2 \left(1 + \frac{3 a_2}{\sqrt{a_2^{2} + \gamma^2 a_x^{2} \Phi_x^2}}\right) - R_3 \left(1 - \frac{3 a_2}{\sqrt{a_2^{2} + \gamma^2 a_x^{2} \Phi_x^2}}\right) \right] \text{ ,} \\
        S_{xp} &= \frac{3 \gamma^2 \Phi_x^2 a_x}{\sqrt{a_2^{2} + \gamma^2 a_x^{2} \Phi_x^2}} \left( R_3 - R_2 \right) \text{ .}
    \end{aligned}
    \label{equation:nagaitsev_spsxsxp}
\end{equation}

From these, one computes the integrals - called the \textit{Nagaitsev integrals} in the \textit{xfields} code base - \(I_x, I_y\) and \(I_z\):

\begin{equation}
    \begin{aligned}
        I_x &= \int_0^C \frac{\beta_x ds}{L \sigma_x \sigma_y} \left[S_x + \left( \frac{D_x^{2}}{\beta_x^{2}} + \Phi_x^{2} \right) S_p + S_{xp} \right] \text{ ,} \\
        I_y &= \int_0^C \frac{\beta_y ds}{L \sigma_x \sigma_y} \left(R_2 + R_3 - 2 R_1\right) \text{ ,} \\
        I_z &= \int_0^C \frac{ds}{L \sigma_x \sigma_y} S_p \text{ .}
    \end{aligned}
    \label{equation:nagaitsev_integrals}
\end{equation}
with \(C\) the circumference (or length) of the machine.
Finally, the \textit{emittance growth rates} in the horizontal, vertical and longitudinal planes are computed as:

\begin{equation}
    \boxed{\frac{1}{\varepsilon_x} \frac{d \varepsilon_x}{dt} = \frac{1}{\varepsilon_x} \frac{N r_0^{2} c L_C}{12 \pi \beta^3 \gamma^5 \sigma_z} I_x}
    \label{equation:nagaitsev_tx}
\end{equation}

\begin{equation}
    \boxed{\frac{1}{\varepsilon_y} \frac{d \varepsilon_y}{dt} = \frac{1}{\varepsilon_y} \frac{N r_0^{2} c L_C}{12 \pi \beta^3 \gamma^5 \sigma_z} I_y}
    \label{equation:nagaitsev_ty}
\end{equation}

\begin{equation}
    \boxed{\frac{1}{\varepsilon_z} \frac{d \varepsilon_z}{dt} = \frac{1}{\sigma_p^{2}} \frac{N r_0^{2} c L_C}{12 \pi \beta^3 \gamma^5 \sigma_z} I_z}
    \label{equation:nagaitsev_tz}
\end{equation}

In the above \(N\) is the total beam intensity, \(r_0\) the classical particle radius, \(c\) the speed of light in vacuum, \(L_C\) the Coulomb logarithm from Eq~\eqref{equation:coulomb_logarithm}, \(\beta\) and \(\gamma\) the relativistic parameters of the beam and \(\sigma_z\) the bunch length.
\newline

\textbf{Please note: Nagaitsev's computations yield emittance growth rates}.
In Xsuite these are computed as exposed above, and converted to amplitude growth rates before being returned to the user, as shown in Fig.~\ref{figure:ibs_convention_conversion}.
\newline

Importantly, this formalism does not take into account vertical dispersion, and in the presence of \(D_y\) will yield an erroneous vertical growth rate.
For machines with vertical dispersion, the Bjorken-Mtingwa formalism presented below is recommended.

\subsection{Bjorken-Mtingwa Formalism}
\label{subsection:bjorken_mtingwa_formalism}

The IBS growth rates can also be computed according to the theory by Bjorken and Mtingwa~\cite{CERN:Bjorken_Mtingwa:Intrabeam_Scattering}.
The specific implementation follows that of the MAD-X code, for which modifications to the terms of B\&M's theory have been made by Antoniou and Zimmermann to account for vertical dispersion non-ultrarelativistic beams~\cite{CERN:Antoniou:Revision_IBS_MADX}.
\newline

In this formalism, growth rates are computed at every element in the lattice and averaged over the machine.
For a given plane \(u\) (horizontal, vertical or longitudinal), the \textit{emittance growth rate} is computed as:

\begin{equation}
    T_u = \frac{N r_0^{2} c m^3 L_C \pi^{2}}{\gamma \Gamma} \left\langle \int_0^{\infty} \frac{d \lambda \lambda^{1 / 2}}{\left[\operatorname{det}(L + \lambda I)\right]^{1/2}} \left\{\operatorname{Tr} L^{(u)} \operatorname{Tr}\left(\frac{1}{L + \lambda I}\right) - 3 \operatorname{Tr} L^{(u)} \left(\frac{1}{L + \lambda I}\right)\right\}\right\rangle
    \label{equation:bjorken_mtingwa_original}
\end{equation}
in which \(N\) is the total beam intensity, \(r_0\) the classical particle radius, \(c\) the speed of light in vacuum, \(m\) the mass of the considered particle, \(L_C\) the Coulomb logarithm from Eq~\eqref{equation:coulomb_logarithm}, \(\gamma\) the relativistic parameter of the beam, and \(\Gamma\) the six-dimensional phase space volume of the beam, defined as:

\begin{equation}
    \Gamma = \left( 2 \pi \right)^3 \left( \beta \gamma \right)^3 m^3 \varepsilon_x \varepsilon_y \sigma_{\delta} \sigma_z
\end{equation}
with \(\sigma_{\delta}\) the relative momentum spread and \(\sigma_z\) the bunch length.
One should note the expression for \(\Gamma\) is corrected by a factor \(\sqrt{2}\) for coasting beams.

In Eq~\eqref{equation:bjorken_mtingwa_original} \(\lambda\) is simply the integration variable, \(I\) is the 3x3 identity matrix, and \(L\) is the 3x3 matrix and the matrix \(L\) is defined as:

\begin{equation}
    L = L^{(x)} + L^{(y)} + L^{(z)} \text{ ,}
\end{equation}
where the plane-dependent matrices \(L^{(x)}, L^{(y)}\) and \(L^{(z)}\) are defined as:

\begin{equation}
    L^{(x)} = \frac{\beta_x}{\epsilon_x} \left(
        \begin{array}{ccc}
            1              & -\gamma \phi_x         & 0 \\
            -\gamma \phi_x & \gamma^2 H_x / \beta_x & 0 \\
            0              & 0                      & 0
        \end{array} \right) \text{ ,}
\end{equation}

\begin{equation}
    L^{(y)} = \frac{\beta_y}{\epsilon_y} \left(
        \begin{array}{ccc}
            0 & 0                      & 0 \\
            0 & \gamma^2 H_y / \beta_y & -\gamma \phi_y \\
            0 & -\gamma \phi_y         & 1
        \end{array} \right) \text{ ,}
\end{equation}

\begin{equation}
    L^{(z)} = \frac{\gamma^2}{\sigma_\delta^2} \left(
        \begin{array}{lll}
            0 & 0 & 0 \\
            0 & 1 & 0 \\
            0 & 0 & 0
        \end{array} \right) \text{ .}
\end{equation}

The \(\Phi_{x,y}\) and \(H_{x,y}\) terms are defined as:

\begin{equation}
    \phi_{x,y} = D_{x,y}^{\prime} - \frac{\beta_{x,y}^{\prime} D_{x,y}}{2 \beta_{x,y}} \text{ ,}
    \label{equation:bm_phi}
\end{equation}
and

\begin{equation}
    H_{x,y} = \frac{D_{x,y}^2 + \beta_{x,y}^2 \phi_{x,y}^2}{\beta_{x,y}} \text{ .}
    \label{equation:bm_h}
\end{equation}

In~\cite{CERN:Antoniou:Revision_IBS_MADX} a new expression was derived for each growth rates, which is the implemented approach.
In \textit{xfields}, the computation of the growth rates takes the following steps.
First the \(a, b, c, a_x, b_x, a_y, b_y, a_z\) and \(b_z\) terms are computed as defined below:

\begin{equation}
    a = \gamma^2 \left(\frac{H_x}{\varepsilon_x} + \frac{H_y}{\varepsilon_y}\right) + \frac{\gamma^2}{\sigma_{\delta}^{2}} + \left(\frac{\beta_x}{\varepsilon_x} + \frac{\beta_y}{\varepsilon_y} \right) \text{ ,}
    \label{equation:bm_a}
\end{equation}

\begin{equation}
    b = \left(\frac{\beta_x}{\varepsilon_x} + \frac{\beta_y}{\varepsilon_y}\right) \left(\frac{\gamma^2 D_x^{2}}{\varepsilon_x \beta_x} + \frac{\gamma^2 D_y^{2}}{\varepsilon_y \beta_y} + \frac{\gamma^2}{\sigma_{\delta}^{2}}\right) + \frac{\beta_x \beta_y}{\varepsilon_x \varepsilon_y} \gamma^2 \left( \Phi_x^{2} + \Phi_y^{2}\right) + \frac{\beta_x \beta_y}{\varepsilon_x \varepsilon_y} \text{ ,}
    \label{equation:bm_b}
\end{equation}

\begin{equation}
    c = \frac{\beta_x \beta_y}{\varepsilon_x \varepsilon_y} \left( \frac{\gamma^2 D_x^{2}}{\varepsilon_x \beta_x} + \frac{\gamma^2 D_y^{2}}{\varepsilon_y \beta_y} + \frac{\gamma^2}{\sigma_{\delta}^{2}} \right) \text{ ,}
    \label{equation:bm_c}
\end{equation}

\begin{equation}
    \begin{aligned}
    a_x = & 2 \gamma^2 \left(\frac{H_x}{\varepsilon_x} + \frac{H_y}{\varepsilon_y} + \frac{1}{\sigma_{\delta}^{2}}\right) - \frac{\beta_x H_y}{H_x \varepsilon_y} + \frac{\beta_x}{H_x \gamma^2} \left( \frac{2 \beta_x}{\varepsilon_y} - \frac{\beta_y}{\varepsilon_y} - \frac{\gamma^2}{\sigma_{\delta}^{2}} \right) \\
          & - 2 \frac{\beta_x}{\varepsilon_x} \frac{\beta_y}{\varepsilon_y} + \frac{\beta_x}{\gamma^2 H_x} \left( \frac{6 \beta_x}{\varepsilon_x} \gamma^2 \Phi_x^{2} \right) \text{ ,}
    \end{aligned}
    \label{equation:bm_ax}
\end{equation}

\begin{equation}
    \begin{aligned}
        b_x = & \left(\frac{\beta_x}{\varepsilon_x} + \frac{\beta_y}{\varepsilon_y}\right) \left(\frac{\gamma^2 H_x}{\varepsilon_x} + \frac{\gamma^2 H_y}{\varepsilon_y} + \frac{\gamma^2}{\sigma_{\delta}^{2}}\right) - \gamma^2 \left(\frac{\beta_x^{2}}{\varepsilon_x^{2}} \Phi_x^{2} + \frac{\beta_y^{2}}{\varepsilon_y^{2}} \Phi_y^{2}\right) + \left(\frac{\beta_x}{\varepsilon_x} - \frac{4 \beta_y}{\varepsilon_y}\right) \frac{\beta_x}{\varepsilon_x} \\
              & + \frac{\beta_x}{\gamma^2 H_x} \left(\frac{\gamma^2}{\sigma_{\delta}^{2}} \left(\frac{\beta_x}{\varepsilon_x} - \frac{2 \beta_y}{\varepsilon_y} \right) + \frac{\beta_x \beta_y}{\varepsilon_x \varepsilon_y} + \frac{6 \beta_x \beta_y}{\varepsilon_x \varepsilon_y} \gamma^2 \Phi_x^{2} + \gamma^2 \left(\frac{2 \beta_y^{2} \Phi_y^{2}}{\varepsilon_y^{2}} - \frac{\beta_x^{2} \Phi_x^{2}}{\varepsilon_x^{2}}\right)\right) \\
              & + \frac{\beta_x H_y}{\varepsilon_y H_x} \left(\frac{\beta_x}{\varepsilon_x} - \frac{2 \beta_y}{\varepsilon_y}\right)
    \end{aligned}
    \label{equation:bm_bx}
\end{equation}

\begin{equation}
    \begin{aligned}
    a_y = & - \gamma^2 \left(\frac{H_x}{\varepsilon_x} + \frac{2 H_y}{\varepsilon_y} + \frac{\beta_x}{\beta_y} \frac{H_y}{\varepsilon_x} + \frac{1}{\sigma_{\delta}^{2}}\right) + 2 \gamma^4 \frac{H_y}{\beta_y} \left(\frac{H_y}{\varepsilon_y} + \frac{H_x}{\varepsilon_x}\right) \\
          & + \frac{2 \gamma^4 H_y}{\beta_y \sigma_{\delta}^{2}} - \left(\frac{\beta_x}{\varepsilon_x} - \frac{2 \beta_y}{\varepsilon_y}\right) + \left(\frac{6 \beta_y}{\varepsilon_y} \gamma^2 \Phi_y^{2}\right)
    \end{aligned}
    \label{equation:bm_ay}
\end{equation}

\begin{equation}
    \begin{aligned}
    b_y = & \gamma^2 \left(\frac{\beta_y}{\varepsilon_y} - \frac{2 \beta_x}{\varepsilon_x}\right) \left(\frac{H_x}{\varepsilon_x} + \frac{1}{\sigma_{\delta}^{2}}\right) + \left(\frac{\beta_y}{\varepsilon_y} - \frac{4 \beta_x}{\varepsilon_x}\right) \frac{\gamma^2 H_y}{\varepsilon_y} + \frac{\beta_x \beta_y}{\varepsilon_x \varepsilon_y} \\
          & + \gamma^2 \left(\frac{2 \beta_x^{2} \Phi_x^{2}}{\varepsilon_x^{2}} - \frac{\beta_y^{2} \Phi_y^{2}}{\varepsilon_y^{2}}\right) + \frac{\gamma^4 H_y}{\beta_y} \left(\frac{\beta_x}{\varepsilon_x} + \frac{\beta_y}{\varepsilon_y}\right) \left(\frac{H_y}{\varepsilon_y} + \frac{1}{\sigma_{\delta}^{2}}\right)\\
          & + \left(\frac{\beta_x}{\varepsilon_x} + \frac{\beta_y}{\varepsilon_y}\right) \gamma^4 \frac{H_x H_y}{\beta_y \varepsilon_x} - \gamma^4 \frac{H_y}{\beta_y} \left(\frac{\beta_x^{2}}{\varepsilon_x^{2}} \Phi_x^{2} + \frac{\beta_y^{2}}{\varepsilon_y^{2}} \Phi_y^{2}\right) + \frac{6 \beta_x \beta_y}{\varepsilon_x \varepsilon_y} \gamma^2 \Phi_y^{2}
    \end{aligned}
    \label{equation:bm_by}
\end{equation}

\begin{equation}
    a_z = 2 \gamma^2 \left(\frac{H_x}{\varepsilon_x} + \frac{H_y}{\varepsilon_y} + \frac{1}{\sigma_{\delta}^{2}}\right) - \frac{\beta_x}{\varepsilon_x} - \frac{\beta_y}{\varepsilon_y}
    \label{equation:bm_az}
\end{equation}

\begin{equation}
    b_z = \left(\frac{\beta_x}{\varepsilon_x} + \frac{\beta_y}{\varepsilon_y}\right) \gamma^2 \left(\frac{H_x}{\varepsilon_x} + \frac{H_y}{\varepsilon_y} + \frac{1}{\sigma_{\delta}^{2}}\right) - 2 \frac{\beta_x \beta_y}{\varepsilon_x \varepsilon_y} - \gamma^2 \left(\frac{\beta_x^{2} \Phi_x^{2}}{\varepsilon_x^{2}} + \frac{\beta_y^{2} \Phi_y^{2}}{\varepsilon_y^{2}}\right)
    \label{equation:bm_bz}
\end{equation}

Finally, the \textit{emittance growth rates} in the horizontal, vertical and longitudinal planes are computed as:

\begin{equation}
    \boxed{\frac{1}{\varepsilon_x} \frac{d \varepsilon_x}{dt} = \frac{N r_0^{2} c m^3 L_C \pi^2}{\gamma \Gamma} \left< \left[\frac{\gamma^2 H_x}{\varepsilon_x}\right] \int_0^{\infty} \frac{\lambda^{1/2} \left[a_x \lambda + b_x\right]}{\left(\lambda^3 + a \lambda^2 + b \lambda + c\right)} d \lambda \right>}
    \label{equation:bm_tx}
\end{equation}

\begin{equation}
    \boxed{\frac{1}{\varepsilon_y} \frac{d \varepsilon_y}{dt} = \frac{N r_0^{2} c m^3 L_C \pi^2}{\gamma \Gamma} \left< \left[\frac{\beta_y}{\varepsilon_y}\right] \int_0^{\infty} \frac{\lambda^{1/2} \left[a_y \lambda + b_y\right]}{\left(\lambda^3 + a \lambda^2 + b \lambda + c\right)} d \lambda \right>}
    \label{equation:bm_ty}
\end{equation}

\begin{equation}
    \boxed{\frac{1}{\varepsilon_z} \frac{d \varepsilon_z}{dt} = \frac{N r_0^{2} c m^3 L_C \pi^2}{\gamma \Gamma} \left< \left[\frac{\gamma^2}{\sigma_{\delta}^{2}}\right] \int_0^{\infty} \frac{\lambda^{1/2} \left[a_z \lambda + b_z\right]}{\left(\lambda^3 + a \lambda^2 + b \lambda + c\right)} d \lambda \right>}
    \label{equation:bm_tz}
\end{equation}
where the constants in the common fraction term are the same as for Eq~\eqref{equation:bjorken_mtingwa_original}, \(\lambda\) is an integration variable and the angled bracket signify the averaging over the lattice, given the terms contained inside are arrays with one value per element.
\newline

\textbf{Please note: Bjorken and Mtingwa's computations yield emittance growth rates}.
In Xsuite these are computed as exposed above, and converted to amplitude growth rates before being returned to the user, as shown in Fig.~\ref{figure:ibs_convention_conversion}.
where the constants in the common fraction term are the same as for Eq~\eqref{equation:bjorken_mtingwa_original}, \(\lambda\) is an integration variable and the angled bracket signifies the averaging over the lattice, given the terms contained inside are arrays with one value per element.

\section{Steady-state emittances}
\label{subsection:steady-state_emittances}

The steady-state emittances in the presence of Synchrotron Radiation (SR), Quantum Excitation (QE), and Intra-Beam Scattering (IBS) emerge from a dynamic equilibrium, where the combined effect of these three phenomena balances each other out. More specifically, the QE and SR are accounted for through the natural emittances and SR damping constants, the IBS is described by its growth rates.
Each of these effects depends on the optical functions along the storage ring.
All the following equations remain valid independently of which formalism (Nagaitsev or Bjorken-Mtingwa) is used when computing the IBS growth rates. In addition, the SR damping constants and IBS growth rates follow the amplitude convention as defined in Eq.~\eqref{eq:grate_rad_damp}.

\subsection{Steady-state emittances with QE, SR, and IBS}

The steady-state emittances are solutions to a system of three ordinary differential equations describing the dynamic interplay of the QE, SR, and IBS. The system of differential equations can be solved numerically by computing the emittance evolution for a succession of infinitesimal time steps. The equations are as follows:

%Final equilibrium emittance depends on the quantum excitation, synchrotron damping constants ($\alpha^{SR}_x, \alpha^{SR}_y, \alpha^{SR}_z$), and IBS growth rates ($\alpha^{IBS}_x(\varepsilon_{x}, \varepsilon_{y}, \varepsilon_{z}), \alpha^{IBS}_y(\varepsilon_{x}, \varepsilon_{y}, \varepsilon_{z}), \alpha^{IBS}_z(\varepsilon_{x}, \varepsilon_{y}, \varepsilon_{z})$)

\begin{equation}
    \frac{d\varepsilon_u}{dt} = -2 \alpha^{SR}_u \left(\varepsilon_u - \varepsilon_{u,0} \right) + 2\alpha^{IBS}_u \varepsilon_{u}, \quad u = x,y,z
\label{eq:emit_evol}
\end{equation}

$\varepsilon_{u,0}$ is the natural emittance, $\varepsilon_u$ the emittance, $\alpha^{SR}_u$ the SR damping constant and $\alpha^{IBS}_u$ the IBS growth rate.
It can be noted that the IBS growth rates are a function of all three emittances ($\alpha^{IBS}_u(\varepsilon_{x}, \varepsilon_{y}, \varepsilon_{z})$).

The steady-state emittance is reached when the emittance evolution reaches zero and leads to:
\begin{equation}
    \varepsilon_u = \frac{\varepsilon_{u,0}}{1 - \alpha^{IBS}_u / \alpha^{SR}_u}, \quad u = x,y,z
\label{eq:emit_final}
\end{equation}

Although mathematically correct, the equation assumes a finite vertical emittance. In the case of lepton storage rings, the vertical emittance is often negligible in the absence of vertical dispersion or betatron coupling. As a result, the IBS growth rates would reach extremely large values when a near zero vertical emittance is considered. A finite vertical emittance is introduced through betatron coupling or an external excitation to overcome this limitation.

\subsection{Steady-state emittances due to betatron coupling}

In the presence of betatron coupling, emittance sharing occurs between the two transverse planes. The treatment of betatron coupling can be simplified by expressing it through the emittance coupling factor $\kappa = \Tilde{\varepsilon}_y/\Tilde{\varepsilon}_x$, the ratio of the perturbed vertical emittance to the horizontal one.

Based on the natural emittance, defined in Eq.~\eqref{eq:equilemi1} but written in a form~\cite{Lee:2018fhc} so that the horizontal damping partition number $j_x$ appears:
\begin{equation*}
    \varepsilon_0 = C_q \frac{\gamma^2}{j_x} \frac{I_5}{I_2},
\end{equation*}
where $C_q = \frac{55}{32 \sqrt{3}} \frac{\hbar}{m_e c} \approx 3.84 \times 10^{-13}$ m for electrons, $\gamma$ the relativistic gamma and $I_2,I_5$ respectively the second and fifth radiation integrals.

\begin{equation}
    \Tilde{\varepsilon_0} = C_q \frac{\gamma^2}{\Tilde{j_x}} \frac{\langle K^3 \mathcal{H} \rangle}{K^2}, \qquad \mathrm{where} \quad \Tilde{j_x} = \frac{j_x}{1 + \kappa} + \frac{\kappa j_y}{1 + \kappa}
\label{eq:j_kappa}
\end{equation}

Here, $j_y$ represents the vertical damping partition number.
After some simplifications and remembering $\Tilde{\varepsilon}_0 = \Tilde{\varepsilon}_x + \Tilde{\varepsilon}_y$ we finally obtain:
\begin{equation}
    \Tilde{\varepsilon}_{x,0} = \frac{\Tilde{\varepsilon}_0}{1 + \kappa} = \frac{\Tilde{\varepsilon}_0}{1 + \kappa \frac{j_y}{j_x}}, \qquad \Tilde{\varepsilon}_{y,0} = \frac{\kappa \Tilde{\varepsilon}_0}{1 + \kappa} = \frac{\kappa \Tilde{\varepsilon}_0}{1 + \kappa \frac{j_y}{j_x}}
\label{eq:eps_kappa}
\end{equation}

%The betatron coupling leaves the longitudinal plane unaffected, the Eqs.~\eqref{eq:emit_evol},\eqref{eq:emit_final} remain valid in the following.
Furthermore, the perturbed horizontal and vertical emittances can be expressed as a function of the ones without betatron coupling:
\begin{equation*}
    \Tilde{\varepsilon}_x = \frac{\varepsilon}{1 + \kappa \frac{j_y}{j_x}}, \quad \Tilde{\varepsilon}_y = \frac{\kappa \varepsilon}{1 + \kappa \frac{j_y}{j_x}}
\end{equation*}

It can be noted that total transverse emittance conservation occurs solely if $j_x = j_y$.

As a result, the transverse emittance evolution can be rewritten as:
\begin{equation}
\frac{d\Tilde{\varepsilon}_u}{dt} = -2 \alpha^{SR}_u \left(\Tilde{\varepsilon}_u - \Tilde{\varepsilon}_{u,0}\right) + 2 \alpha^{IBS}_u \Tilde{\varepsilon}_u, \quad u = x,y
\label{eq:emit_evol_coupling}
\end{equation}

And the steady-state transverse emittances become:
\begin{equation}
    \Tilde{\varepsilon}_u = \frac{\Tilde{\varepsilon}_{u,0}}{1 - \frac{\alpha^{IBS}_u}{\alpha^{SR}_u \left(1 + \kappa j_y / j_x \right)}}, \quad u = x,y
\label{eq:emit_final_coupling}
\end{equation}

%The main drawback 

\subsection{Steady-state emittances due to an external excitation}

Alternatively, a finite vertical emittance can also be obtained by exciting the beam using the Pulse-Picking by Resonant Excitation (PPRE) method~\cite{Hwang2019AnalyticalAN} in the vertical plane.
In this situation, the horizontal and vertical planes are left uncoupled while the vertical emittance can be controlled. Nonetheless, the emittance coupling factor can still be employed. 
In this scenario, the emittance evolution follows Eq.~\eqref{eq:emit_evol_coupling} and the final emittance Eq.~\eqref{eq:emit_final}. Compared to the previous case, the total transverse emittance is not conserved even if $j_x = j_y$.

\section{IBS Kicks}
\label{section:ibs_kicks}

The approach of using analytical growth rates does not provide a way to study the interplay of IBS with arbitrary effects, such as space charge, electron clouds, beam-beam, etc.
To do so, it is necessary to include IBS effects in tracking simulations together with other desired effects.
In \textit{xfields} two elements are available to model IBS effects in tracking simulations, both providing momenta kicks to tracked particles according to a specific formalism.

\subsection{Analytical Kicks}
\label{subsection:analytical_kicks}

A first element is available to provide momenta kicks based on analytical growth rates, according to the approach introduced by R. Bruce~\cite{PRAB:Bruce:IBSAnalyticalKick}.
In \textit{xfields} the computation of the kick is done as follows.
\newline

First, the beam intensity \(N\), bunch length \(\sigma_z\), momentum deviation \(\sigma_{\delta}\), and geometric emittances \(\varepsilon_{x,y}\) are inferred from the tracked particles object.
These are used to compute the analytical IBS growth rates.
From these, each particle is given a momentum kick in each dimension according to:

\begin{equation}
    \boxed{\Delta p_u = R \sigma_{p_u} \sqrt{ 2 T_{IBS,u} T_{rev} \sigma_z \sqrt{\pi} \rho(z)} \text{ ;   } u=x,y,z}
    \label{equation:momentum_kick_analytical}
\end{equation}

Here \(R\) is a random number from the standard normal distribution; \(\sigma_{p_u}\) is the standard deviation of the momentum in plane \(u\); \(T_{IBS,u}\) is the \textit{emittance growth rate} for plane \(u\); \(T_{rev}\) is the revolution frequency and \(\rho(z)\) is the longitudinal line density.
\newline

Note from the form of Eq~\eqref{equation:momentum_kick_analytical} that only zero or strictly positive growth rates are valid, and as such this formalism is traditionally available above transition energy.
\newline

The longitudinal line density \(\rho(z)\) is used as a weighting factor to provide a stronger kick to particles in the denser regions of the bunch.
It is obtained by binning the longitudinal plane of the particle distribution and normalizing the values.

\subsection{Kinetic Kicks}
\label{subsection:kinetic_kicks}

A second element is available to provide momenta kicks based on diffusion and friction terms from the kinetic theory of gases, as introduced by P. Zenkevich~\cite{NuclInstr:Zenkevich:IBSKineticKick}.
The momentum kick has a form similar to the Langevin equation:

\begin{equation}
    \Delta p_u = - K_u p_u \sigma_z \sqrt{\pi} \rho(z) \Delta t + R \sigma_{p_u} \sqrt{2 C_u \sigma_z \sqrt{\pi} \rho(z) \Delta t} \text{ ;   } u=x,y,z
    \label{equation:original_kinetic_kick}
\end{equation}
where \(K_u\) and \(C_u\) are functions of the friction and diffusion terms, respectively.
\newline

In \textit{xfields} the exact implementation makes use of terms from the Nagaitsev formalism (see \ref{subsection:nagaitsev_formalism}) as derived by M. Zampetakis~\cite{arXiv:Zampetakis:Interplay_SC_IBS_LHC_Chain}.
First the \(a_x, a_y, a_s, a_1\) and \(a_2\) terms are computed according to Eq~\eqref{equation:nagaitsev_step1}.
Then the \(\lambda_1, \lambda_2\) and \(\lambda_3\) terms are computed according to Eq~\eqref{equation:nagaitsev_lambdas}, to obtain the \(R_1, R_2\) and \(R_3\) elliptic integrals according to Eq~\eqref{equation:nagaitsev_r1r2r3}.
\newline

New forms equivalent to the original diffusion and friction terms of the Approximate Model can be expressed from these.
By first defining

\begin{equation}
    q = \sqrt{a_2^{2} + \gamma^2 a_x^{2} \Phi_x^2} \text{ ,}
\end{equation}
one can first compute:

\begin{equation}
    \begin{aligned}
        D_{x,x} &= \frac{1}{2} \left[ 2 R_1 + R2 \left(1 - \frac{a_2}{q}\right) + R_3 \left(1 + \frac{a_2}{q}\right) \right] \text{,  } \\
        D_{x,z} &= \frac{3 \gamma^2 \Phi_x^2 a_x}{q} \left(R_3 - R_2\right) \text{,  }  \\
        D_{y,y} &= R_2 + R_3 \text{,  }  \\
        D_{z,z} &= \frac{\gamma^2}{2} \left[ 2 R_1 +  R_2 \left(1 + \frac{a_2}{q}\right) + R_3 \left(1 - \frac{a_2}{q}\right) \right] \text{, }
    \end{aligned}
    \label{equation:kinetic_AM_diffusion_terms}
\end{equation}

and then:
\begin{equation}
    \begin{aligned}
        K_x &= R_2 \left(1 + \frac{a_2}{q}\right) + R_3 \left(1 - \frac{a_2}{q}\right) \text{,  } \\
        K_y &= 2 R_1 \text{,  }  \\
        K_z &= \gamma^2 \left[ R_2 \left(1 - \frac{a_2}{q}\right) + R_3 \left(1 + \frac{a_2}{q}\right) \right] \text{ .}
    \end{aligned}
    \label{equation:kinetic_AM_friction_terms}
\end{equation}

The following integrals are computed from the above:

\begin{equation}
    \begin{aligned}
        D_{xi} &= \int_0^C \frac{\beta_x ds}{C \sigma_x \sigma_y} \left[D_{x,x} + \left(\frac{D_x^2}{\beta_x^2} + \Phi_x^2\right) D_{z,z} + D_{x,z} \right] \text{,  } \\
        D_{yi} &= \int_0^C \frac{\beta_y ds}{C \sigma_x \sigma_y} D_{y,y} \text{,  }  \\
        D_{zi} &= \int_0^C \frac{ds}{C \sigma_x \sigma_y} D_{z,z} \text{ .}
    \end{aligned}
    \label{equation:kinetic_diffusion_integrals}
\end{equation}

\begin{equation}
    \begin{aligned}
        F_{xi} &= \int_0^C \frac{\beta_x ds}{C \sigma_x \sigma_y} \left[K_x + \left(\frac{D_x^2}{\beta_x^2} + \Phi_x^2\right) K_z \right] \text{,  } \\
        F_{yi} &= \int_0^C \frac{\beta_y ds}{C \sigma_x \sigma_y} K_x \text{,  }  \\
        F_{zi} &= \int_0^C \frac{ds}{C \sigma_x \sigma_y} K_z \text{ .}
    \end{aligned}
    \label{equation:kinetic_friction_integrals}
\end{equation}

with \(C\) the circumference of the machine.
Finally, the new diffusion coefficients are computed according to:

\begin{equation}
    \boxed{G_x = \frac{1}{\varepsilon_x} \frac{N r_0^2 c L_C}{12 \pi \beta^3 \gamma^5 \sigma_z} D_{xi}}
    \label{equation:kinetic_horizontal_diffusion_coefficient}
\end{equation}

\begin{equation}
    \boxed{G_y = \frac{1}{\varepsilon_y} \frac{N r_0^2 c L_C}{12 \pi \beta^3 \gamma^5 \sigma_z} D_{yi}}
    \label{equation:kinetic_vertical_diffusion_coefficient}
\end{equation}

\begin{equation}
    \boxed{G_z = \frac{1}{\sigma_{\delta}^2} \frac{N r_0^2 c L_C}{12 \pi \beta^3 \gamma^5 \sigma_z} D_{zi}}
    \label{equation:kinetic_longitudinal_diffusion_coefficient}
\end{equation}
and the friction coefficients according to:

\begin{equation}
    \boxed{F_x = \frac{1}{\varepsilon_x} \frac{N r_0^2 c L_C}{12 \pi \beta^3 \gamma^5 \sigma_z} F_{xi}}
    \label{equation:kinetic_horizontal_friction_coefficient}
\end{equation}

\begin{equation}
    \boxed{F_y = \frac{1}{\varepsilon_y} \frac{N r_0^2 c L_C}{12 \pi \beta^3 \gamma^5 \sigma_z} F_{yi}}
    \label{equation:kinetic_vertical_friction_coefficient}
\end{equation}

\begin{equation}
    \boxed{F_z = \frac{1}{\sigma_{\delta}^2} \frac{N r_0^2 c L_C}{12 \pi \beta^3 \gamma^5 \sigma_z} F_{zi}}
    \label{equation:kinetic_longitudinal_friction_coefficient}
\end{equation}

In the above \(N\) is the total beam intensity, \(r_0\) the classical particle radius, \(c\) the speed of light in vacuum, \(L_C\) the Coulomb logarithm from Eq~\eqref{equation:coulomb_logarithm}.
\(\beta\) and \(\gamma\) are the relativistic parameters of the beam and \(\sigma_z\) the bunch length.
\newline

From these coefficients, each particle is given a momentum kick in each dimension according to:

\begin{equation}
    \boxed{\Delta p_u = - F_u p_u T_{rev} 2 \sqrt{\pi} \sigma_z \rho(z) + R \sigma_{p_u} \sqrt{T_{rev} G_u 2 \sqrt{\pi} \sigma_z \rho(z)} \text{ ;   } u=x,y,z}
    \label{equation:momentum_kick_kinetic}
\end{equation}

Here \(R\) is a random number from the standard normal distribution; \(p_u\) and \(\sigma_{p_u}\) are the momentum and its standard deviation in plane \(u\); \(T_{rev}\) is the revolution frequency and \(\rho(z)\) is the longitudinal line density as defined previously (see \ref{subsection:analytical_kicks}).


\chapter{FFT solvers and convolutions}

\section{Notation for Discrete Fourier Transform}
We will use the following notation for the Discrete Fourier Transform of a sequence of length $M$:
\begin{equation}
\hat{a}_k = \text{DFT}_M(a_m) =  \sum_{m=0}^{M-1} a_m\, e^{-j2\pi  \frac{km}{M}}  \quad \text{for } k \in 0, ..., M
\end{equation}
The corresponding inverse transform is defined as:
\begin{equation}
{a}_m = \text{DFT}^{-1}_M(\hat{a}_k) =  \frac{1}{M}\sum_{k=0}^{M-1} \hat{a}_k\, e^{j2\pi  \frac{km}{M}}  \quad \text{for } m \in 0, ..., M
\end{equation}

Multidimensional Discrete Fourier Transforms are obtained by applying sequentially 1D DFTs.. For example, in two dimensions:

\begin{equation}
\begin{split}
\hat{a}_{k_xk_y} &= \text{DFT}_{M_xM_y}\left\{a_{m_xm_y}\right\}  
= \text{DFT}_{M_y} \left\{\text{DFT}_{M_x}\left\{a_{m_xm_y}\right\}\right\}\\  
&=\sum_{m_x=0}^{M_x-1} e^{-j 2\pi  \frac{k_x m_x}{M_x}} 
\sum_{m_y=0}^{M_y-1} e^{-j 2\pi  \frac{k_y m_y}{M_y}} a_{m_xm_y}
\end{split}
\end{equation}
\begin{equation}
\begin{split}
{a}_{m_xm_y} &= \text{DFT}^{-1}_{M_xM_y}\left\{a_{k_x k_y}\right\}  
= \text{DFT}^{-1}_{M_y} \left\{\text{DFT}^{-1}_{M_x}\left\{\hat{a}_{k_x k_y}\right\}\right\}\\  
&=\frac{1}{M_x M_y}\sum_{k_x=0}^{M_x-1} e^{j 2\pi  \frac{k_x m_x}{M_x}} 
\sum_{k_y=0}^{M_y-1} e^{j 2\pi  \frac{k_y m_y}{M_y}} \hat{a}_{k_xk_y}
\end{split}
\end{equation}

\section{FFT convolution - 1D case}
\label{sec:fftconv1d}
The potential can be written as the convolution of a Green function with the charge distribution:
\begin{equation}
\phi(x) = \int_{-\infty}^{+\infty} \rho(x')\,G(x-x') dx'
\label{eq:conv}
\end{equation}

We assume that the source is limited to the region  $[0, L]$:
\begin{equation}
\rho(x) = \rho(x)\,\Pi_{[0,L]}\left(x\right)
\label{eq:rholim}
\end{equation}
where $\Pi_{[a,b]}(x)$ is a rectangular window function defined as:
\begin{equation}
\Pi_{[a,b]}(x) = 
\begin{cases}
1\quad\text{for } x \in [a, b]\\
0\quad\text{elsewhere}
\end{cases}
\end{equation}

We are interested in the electric potential only the region occupied by the sources, so we can compute:
\begin{equation}
\phi_L(x) = \phi(x) \Pi_{[0, L]}\left(x\right)
\label{eq:philim}
\end{equation}

We replace Eq.\,\eqref{eq:rholim} and Eq.\,\eqref{eq:philim} into Eq.\eqref{eq:conv}, obtaining:
\begin{equation}
\phi_L(x) = \Pi_{[0,L]}\left( x\right)
\int_{-\infty}^{+\infty} 
\Pi_{[0,L]}\left(x'\right)
\rho(x')\,G(x-x') dx'
\end{equation}
We apply the change of variable $x'' = x - x'$:
\begin{equation}
\phi_L(x) = 
\int_{-\infty}^{+\infty} 
\Pi_{[0,L]}\left({x}\right)
\Pi_{[0,L]}\left({x-x''}\right)
\rho(x-x'')\,G(x'') \,dx''
\label{eq:conv1}
\end{equation}
The integrand vanishes outside the set of the $(x, x'')$ defined by:
\begin{equation}
\begin{cases}
0 < x <{L}\\
0 < (x-x'') <{L}
\end{cases}
\end{equation}

We flip the signs in the second equation, obtaining:
\begin{equation}
\begin{cases}
0 < x <{L}\\
-L < (x''-x) <0
\end{cases}
\end{equation}

Combining the two equations we obtain:
\begin{equation}
-L<-L + x < x'' <x<L
\end{equation}
i.e. the integrand is zero for $-L<x''<L$.
Therefore in Eq.\,\eqref{eq:conv1} we can replace $G(x'')$ with its truncated version:
\begin{equation}
G_{2L}(x'') = G(x'')\,\Pi_{[-L,L]}
\left(
{x''}
\right)
\end{equation}

obtaining:
\begin{equation}
\phi_L(x) = 
\int_{-\infty}^{+\infty} 
\Pi_{[0,L]}\left({x}\right)
\Pi_{[0,L]}\left({x-x''}\right)
\rho(x-x'')\,G_{2L}(x'') dx''
\label{eq:conv2}
\end{equation}

Since the two window functions force the integrand to zero outside the region $|x''|<L$ we can replace $G_{2L}(x'')$ with its replicated version:
\begin{equation}
G_{2LR}(x'') = \sum_{n=-\infty}^{+\infty}G_{2L}(x''-2nL) = \sum_{n=-\infty}^{+\infty}G(x'' -2nL)\,\Pi_{[-L,L]}
\left(
{x''-2nL}
\right)
\label{eq:GLR}
\end{equation}
obtaining:
\begin{equation}
\phi_L(x) = 
\int_{-\infty}^{+\infty} 
\Pi_{[0,L]}\left({x}\right)
\Pi_{[0,L]}\left({x-x''}\right)
\rho(x-x'')\,G_{2LR}(x'') dx''
\end{equation}

We can go back to the initial coordinate by substituting $x'' = x-x'$:
\begin{equation}
\phi_L(x) = 
\Pi_{[0,L]}\left(x\right)
\int_{-\infty}^{+\infty} 
\rho(x')\,G_{2LR}(x-x') dx'
\end{equation}

This is a cyclic convolution, so we can proceed as follows. We split the integral:
\begin{equation}
\phi_L(x) = 
\Pi_{[0,L]}\left({x}\right)
\sum_{n=-\infty}^{+\infty}
\int_{2nL}^{2(n+1)L} 
\rho(x')\,G_{2LR}(x-x') \,dx'
\label{eq:conv3}
\end{equation}
In each term we replace $x''' = x'+2nL$:
\begin{equation}
\phi_L(x) = 
\Pi_{[0,L]}\left({x}\right)
\sum_{n=-\infty}^{+\infty}
\int_{0 }^{2L} 
\rho(x'''-2nL)\,G_{2LR}(x-x'''-2nL) \,dx'''
\label{eq:conv4}
\end{equation}
We use the fact that $G_{2LR}(x)$ is periodic:
\begin{equation}
\begin{split}
\phi_L(x) &= 
\Pi_{[0,L]}\left({x}\right)
\sum_{n=-\infty}^{+\infty}
\int_{0 }^{2L} 
\rho(x'''-2nL)\,G_{2LR}(x-x''') dx'''\\
\\&=
\Pi_{[0,L]}\left({x}\right)
\int_{0 }^{2L}  
\sum_{n=-\infty}^{+\infty}
\rho(x'''-2nL)\,G_{2LR}(x-x''') dx'''
\end{split}
\label{eq:conv5}
\end{equation}

We can define a replicated version of $\rho(x)$:
\begin{equation}
\rho_{2LR}(x)= \sum_{n=-\infty}^{+\infty}
\rho(x-2nL)
\end{equation}
noting that this implies:
\begin{equation}
\rho_{2LR}(x)= 0 \quad \text{for } x \in [L, 2L]
\label{eq:zeros}
\end{equation}

We obtain:
\begin{equation}
\phi_L(x) = 
\Pi_{[0,L]}\left({x}\right)
\int_{0 }^{2L} 
\rho_{2LR}(x')\,G_{2LR}(x-x') dx'
\label{eq:conv6}
\end{equation}

The function:

\begin{equation}
\phi_{2LR}(x) = 
\int_{0 }^{2L} 
\rho_{2LR}(x')\,G_{2LR}(x-x') dx'
\label{eq:confin}
\end{equation}
is periodic of period $2L$. From it the potential of interest can be simply calculated by selecting the first half period $[0, L]$:
\begin{equation}
\phi_L(x) = 
\Pi_{[0,L]}\left({x}\right)
\phi_{2LR}(x)
\label{eq:sel}
\end{equation}

To compute the convolution in Eq.\,\ref{eq:confin} we expand $\phi_{2LR}(x)$ in Fourier series:
\begin{equation}
\phi_{2LR}(x) = \sum_{k=-\infty}^{+\infty} \tilde{\phi}_k\, e^{j2\pi k \frac{x}{2L}}
\label{eq:phifour}
\end{equation}
where the Fourier coefficients are given by:
\begin{equation}
\tilde{\phi}_k = \frac{1}{2L}\int_0^{2L} \phi_{2LR}(x)\, e^{-j2\pi k \frac{x}{2L}} \, dx
\label{eq:phik}
\end{equation}

We replace Eq.\,\eqref{eq:confin} into Eq.\,\eqref{eq:phik} obtaining:
\begin{equation}
\hat{\phi}_k = \frac{1}{2L}\int_0^{2L} \int_{0 }^{2L} 
\rho_{2LR}(x')\,G_{2LR}(x-x') \, e^{-j2\pi k \frac{x}{2L}} \,  dx'\, dx
\end{equation}

With the change of variable $x'' = x-x'$ we obtain:
\begin{equation}
\tilde{\phi}_k = 
\frac{1}{2L}
\int_0^{2L} 
\rho_{2LR}(x') e^{-j2\pi k \frac{x'}{2L}}dx'\,
\int_{0 }^{2L} 
\,G_{2LR}(x'') e^{-j2\pi k \frac{x''}{2L}}\,  \,  dx''
\end{equation}

where we recognize the Fourier coefficients of $\rho_{2LR}(x)$ and $\,G_{2LR}(x)$:
\begin{align}
\tilde{\rho}_k = \frac{1}{2L}\int_0^{2L} \rho_{2LR}(x)\, e^{-j2\pi k \frac{x}{2L}} \, dx \label{eq:rhok}\\
\tilde{G}_k = \frac{1}{2L}\int_0^{2L} G_{2LR}(x)\, e^{-j2\pi k \frac{x}{2L}} \, dx \label{eq:Gk}
\end{align}
obtaining simply:
\begin{equation}
\hat{\phi}_k = 2L \, \hat{G}_k \, \hat{\rho}_k
\label{eq:freqconv}
\end{equation}

I assume to have the functions $\rho_{2LR}(x)$ and  $G_{2LR}(x)$ sampled (or averaged) with step:
\begin{equation}
h_x = \frac{2L}{M} = \frac{L}{N}
\end{equation}

I can approximate the integrals in Eqs.\,\eqref{eq:rhok} and\,\eqref{eq:Gk} as:
\begin{align}
\tilde{\rho}_k = \frac{1}{M}\sum_{n=0}^{M-1} \rho_{2LR}(x_n)\, e^{-j2\pi  \frac{kn}{M}}  
= \frac{1}{M} \hat{\rho}_k
\label{eq:rhokfft}\\
\tilde{G}_k = \frac{1}{M}\sum_{n=0}^{M-1} G_{2LR}(x_n)\, e^{-j2\pi  \frac{kn}{M}} 
= \frac{1}{M} \hat{G}_k\label{eq:Gkfft}
\end{align}

where we recognize the Discrete Fourier Transforms:
\begin{align}
\hat{\rho}_k = \text{DFT}_M\left\{ \rho_{2LR}(x_n)\right\}\\
\hat{G}_k = \text{DFT}_M\left\{ G_{2LR}(x_n)\right\}
\end{align}



Using Eq.\,\eqref{eq:phifour} we can obtain a sampled version of $\phi(x)$:
\begin{equation}
\phi_{2LR}(x_n) = 
\sum_{n=0}^{M-1}  
\tilde{\phi}_k\, e^{j2\pi \frac{kn}{M}}
\label{eq:phifft}
\end{equation}
where we have assumed that $\phi(x)$ is sufficiently smooth to allow truncating the sum.


Using Eqs.\,\eqref{eq:freqconv}, \eqref{eq:rhokfft} and\,\eqref{eq:Gkfft}  we obtain:
\begin{equation}
\phi_{2LR}(x_n) = 
2L \sum_{n=0}^{M-1}  
\tilde{G}_k \, \tilde{\rho}_k\, e^{j2\pi \frac{kn}{M}} 
= 
\frac{2L}{M^2}
\sum_{n=0}^{M-1}  
\hat{G}_k \, \hat{\rho}_k\, e^{j2\pi \frac{kn}{M}} 
\label{eq:phifftsimpl}
\end{equation}

This can be rewritten as:
\begin{equation}
\phi_{2LR}(x_n) = 
\frac{1}{M}
\sum_{n=0}^{M-1}  
(h_x\hat{G}_k) \, \hat{\rho}_k\, e^{j2\pi \frac{kn}{M}} 
=\text{DFT}_M^{-1}\left\{\phi_k
\right\}
\label{eq:invfft}
\end{equation}
where 
\begin{equation}
\hat{\phi}_k =h_x\hat{G}_k \, \hat{\rho}_k
\label{eq:phiknint}
\end{equation}
We call ``Integrated Green Function'' the quantity:
\begin{equation}
G_{2LR}(x_n) = h_x G_{2LR}(x_n)
\end{equation}
we introduce the corresponding Fourier transform:
\begin{equation}
\hat{G}_k^\text{int} = \text{DFT}_M\left\{ G_{2LR}^\text{int}(x_n)\right\}
\end{equation}
Eq.\,\eqref{eq:phiknint} can be rewritten as:
\begin{equation}
\boxed{
\hat{\phi}_k =\hat{G}_k^\text{int} \, \hat{\rho}_k}
\end{equation}

In summary the potential at the grid nodes can be computed as follows:
\begin{enumerate}
\item We compute the Integrated Green function at the grid points in the range $[0, L]$:
\begin{equation}
G_{2LR}^\text{int}(x_n) = \int_{x_n-\frac{h_x}{2}}^{x_n+\frac{h_x}{2}} G(x) dx
\end{equation}
\item We extend to the interval $[L, 2L]$ using the fact that in this interval:
\begin{equation}
G^\text{int}_{2LR}(x_n) = G^\text{int}_{2LR}(x_n-2L) =  G^\text{int}_{2LR}(2L-x_n)
\end{equation}
where the first equality comes from the periodicity of $G^\text{int}_{2LR}(x)$ and the second from the fact that $G(x)$ is an even function (i.e. $G(x) = G(-x)$).
Note that for $x_n \in [L, 2L]$ we have that $2L-x_n \in [0, L]$ so we can reuse the values computed at the previous step.
\item We transform it:
\begin{equation}
\hat{G}^\text{int}_k = \text{DFT}_{2N}\left\{ G_{2LR}(x_n)\right\}
\end{equation}
\item We assume that we are given $\rho(x_n)$ in the interval $[0, L]$. From this we can obtain $\rho_{2LR}(x_n)$ over the interval $[0, 2L]$ simply extending the sequence with zeros (see Eq.\,\eqref{eq:zeros}).
\item We transform it:
\begin{equation}
\hat{\rho}_k = \text{DFT}_{2N}\left\{ \rho_{2LR}(x_n)\right\}
\end{equation}
\item We compute the potential in the transformed domain:
\begin{equation}
\hat{\phi}_k = \hat{G}^\text{int}_k \hat{\rho}_k \quad \text{for } k\in [0, 2N]
\end{equation}
\item We inverse-transform:
\begin{equation}
\phi_{2LR}(x_n)  = \text{DFT}_{2N}^{-1}\left\{\hat{\phi}_k\right\}
\end{equation}
which provides the physical potential in the range $[0, L]$:
\begin{equation}
\phi(x_n)  = \phi_{2LR}(x_n)  \quad \text{for } x_n\in [0, L]
\end{equation}
\end{enumerate}

\section{Extension to multiple dimensions}

The procedure described above can be extended to multiple dimensions by applying the same reasoning for all coordinates. 
This gives the following procedure:
\begin{enumerate}
\item We compute the Integrated Green function at the grid points in the volume $[0, L_x]\times[0, L_y]\times[0, L_z]$:
\begin{equation}
G_{2LR}^\text{int}(x_{n_x}, y_{n_y}, z_{n_z}) = 
\int_{x_{n_x}-\frac{h_x}{2}}^{x_{n_x}+\frac{h_x}{2}} dx
\int_{y_{n_y}-\frac{h_y}{2}}^{y_{n_y}+\frac{h_y}{2}} dy
\int_{z_{n_z}-\frac{h_z}{2}}^{z_{n_z}+\frac{h_z}{2}} dz\,
G(x,y,z)
\end{equation}
\item We extend to the region $[0, 2L_x]\times[0, 2L_y]\times[0, 2L_z]$ using the fact that:
\begin{multline}
G^\text{int}_{2LR}(x_n, y_n, z_n) = G^\text{int}_{2LR}(x_n-2L_x, y_n, z_n) =  G^\text{int}_{2LR}(2L_x-x_n, y_n, z_n)\\
\text{for } x_n \in [L_x, 2L_x], y_n \in [0, 2L_y], z_n \in [0, 2L_z]
\end{multline}
\begin{multline}
G^\text{int}_{2LR}(x_n, y_n, z_n) = G^\text{int}_{2LR}(x_n, y_n-2L_y, z_n) =  G^\text{int}_{2LR}(x_n, 2L_y-y_n,  z_n)\\
\text{for } y_n \in [L_y, 2L_y], x_n \in [0, 2L_x], z_n \in [0, 2L_z]
\end{multline}
\begin{multline}
G^\text{int}_{2LR}(x_n, y_n, z_n) = G^\text{int}_{2LR}(x_n, y_n, z_n-2L_z) =  G^\text{int}_{2LR}(x_n, y_n,  2L_z-z_n)\\
\text{for } z_n \in [L_z, 2L_z], x_n \in [0, 2L_x], y_n \in [0, 2L_y]
\end{multline}
This allows reusing the values computed at the previous step.
\item We transform it:
\begin{equation}
\hat{G}^\text{int}_{k_x k_y k_z} = 
\text{DFT}_{2N_x 2N_y 2N_z}\left\{ G_{2LR}(x_n, y_n, z_n)\right\}
\end{equation}
\item We assume that we are given $\rho(x_n, y_n, z_n)$ in the region $[0, L_x]\times[0, L_y]\times[0, L_z]$. From this we can obtain $\rho_{2LR}(x_n)$ over the region $[0, 2L_x]\times[0, 2L_y]\times[0, 2L_z]$ simply extending the matrix with zeros (see Eq.\,\eqref{eq:zeros}).
\item We transform it:
\begin{equation}
\hat{\rho}^\text{int}_{k_x k_y k_z} = 
\text{DFT}_{2N_x 2N_y 2N_z}\left\{ \rho_{2LR}(x_n, y_n, z_n)\right\}
\end{equation}
\item We compute the potential in the transformed domain:
\begin{equation}
\hat{\phi}_{k_x k_y k_z} = \hat{G}^\text{int}_{k_x k_y k_z} \, \hat{\rho}_{k_x k_y k_z} \quad \text{for } k_{x/y/z}\in [0, 2N_{x/y/z}]
\end{equation}
\item We inverse-transform:
\begin{equation}
\phi_{2LR}(x_n, y_n, z_n)  = \text{DFT}_{2N_x 2N_y 2N_z}^{-1}
\left\{\hat{\phi}_{k_x k_y k_z}\right\}
\end{equation}
which provides the physical potential in the region $[0, L_x]\times[0, L_y]\times[0, L_z]$:
\begin{multline}
\phi(x_n, y_n, z_n) = \phi_{2LR}(x_n, y_n, z_n)  
\text{ for } (x_n, y_n, z_n) \in [0, L_x]\times[0, L_y]\times[0, L_z]
\end{multline}
\end{enumerate}

\section{Green functions for 2D and 3D Poisson problems}

\subsection*{3D Poisson problem, free space boundary conditions}

For the equation:
\begin{equation}
\nabla^2 \phi(x,y,z) = -\frac{1}{\varepsilon_0} \rho(x,y,z)
\end{equation}
where:
\begin{equation}
\nabla = \left(\frac{\partial}{\partial x}, 
                      \frac{\partial}{\partial y},
                      \frac{\partial}{\partial z} \right)
\end{equation}
the solution can be written as 
\begin{equation}
\phi(x, y, z) = \iiint_{-\infty}^{+\infty} \rho(x', y', z')
   \,G(x-x', y-y', z-z')\,dx'\,dy'\,dz'
\end{equation}
where:
\begin{equation}
G(x, y, z) = \frac{1}{4\pi\varepsilon_0}\frac{1}{
\sqrt{x^2 +y^2 +z^2}
}
\end{equation}

The corresponding integrated Green function~\cite{QIANG2004278}. can be written as:
\begin{align}
G_{2LR}^\text{int}(x_{n_x}, y_{n_y}, z_{n_z}) =& 
\int_{x_{n_x}-\frac{h_x}{2}}^{x_{n_x}+\frac{h_x}{2}} dx
\int_{y_{n_y}-\frac{h_y}{2}}^{y_{n_y}+\frac{h_y}{2}} dy
\int_{z_{n_z}-\frac{h_z}{2}}^{x_{n_z}+\frac{h_z}{2}} dz\,
G(x,y,z)\\
= &+F\left(x_{n_x}+\frac{h_x}{2}, y_{n_x}+\frac{h_y}{2}, z_{n_x}+\frac{h_z}{2}\right) \\
    &- F\left(x_{n_x}+\frac{h_x}{2}, y_{n_x}+\frac{h_y}{2}, z_{n_x}-\frac{h_z}{2}\right)\\
    &- F\left(x_{n_x}+\frac{h_x}{2}, y_{n_x}-\frac{h_y}{2}, z_{n_x}+\frac{h_z}{2}\right)\\ 
    &+ F\left(x_{n_x}+\frac{h_x}{2}, y_{n_x}-\frac{h_y}{2}, z_{n_x}-\frac{h_z}{2}\right)\\
    &-F\left(x_{n_x}-\frac{h_x}{2}, y_{n_x}+\frac{h_y}{2}, z_{n_x}+\frac{h_z}{2}\right) \\
    &+ F\left(x_{n_x}-\frac{h_x}{2}, y_{n_x}+\frac{h_y}{2}, z_{n_x}-\frac{h_z}{2}\right)\\
    &+ F\left(x_{n_x}-\frac{h_x}{2}, y_{n_x}-\frac{h_y}{2}, z_{n_x}+\frac{h_z}{2}\right)\\ 
    & - F\left(x_{n_x}-\frac{h_x}{2}, y_{n_x}-\frac{h_y}{2}, z_{n_x}-\frac{h_z}{2}\right)
\end{align}
where $F(x,y,z)$ is a primitive of $G(x,y,z)$, which can be obtained as:
\begin{equation}
F(x,y,z) = 
\int_{x_0}^{x} dx
\int_{y_0}^{y} dy
\int_{z_0}^{x} dz\,
G(x,y,z)
\end{equation}
with $(x_0, y_0, z_0)$ being an arbitrary starting point.

An expression for $F(x,y,z)$ is the following
\begin{align}
F(x,y,z) =&\frac{1}{4\pi\varepsilon_0}\iiint \frac{1}{\sqrt{x^{2}+y^{2}+z^{2}}} d x d y d z\\ 
= \frac{1}{4\pi \varepsilon_0}&\left[-\frac{z^{2}}{2} \arctan \left(\frac{x y}{z \sqrt{x^{2}+y^{2}+z^{2}}}\right)\right.
-\frac{y^{2}}{2} \arctan \left(\frac{x z}{y \sqrt{x^{2}+y^{2}+z^{2}}}\right)\\
&-\frac{x^{2}}{2} \arctan \left(\frac{y z}{x \sqrt{x^{2}+y^{2}+z^{2}}}\right) 
+y z \ln \left(x+\sqrt{x^{2}+y^{2}+z^{2}}\right)\\
&\left. +x z \ln \left(y+\sqrt{x^{2}+y^{2}+z^{2}}\right)
+x y \ln \left(z+\sqrt{x^{2}+y^{2}+z^{2}}\right)\right]
\end{align}

Note that we need to choose the first cell center to be in (0,0,0) for evaluation of the integrated Green function. Therefore the cell edges have non zero coordinates and the denominators in the formula will always be non-vanishing.

\subsection*{2D Poisson problem, free space boundary conditions}

For the equation:
\begin{equation}
\nabla_\perp^2 \phi(x,y) = -\frac{1}{\varepsilon_0} \rho(x,y)
\end{equation}
where:
\begin{equation}
\nabla = \left(\frac{\partial}{\partial x}, 
                      \frac{\partial}{\partial y} \right)
\end{equation}
the solution can be written as 
\begin{equation}
\phi(x, y) = \iiint_{-\infty}^{+\infty} \rho(x', y')
   \,G(x-x', y-y')\,dx'\,dy'
\end{equation}
where:
\begin{equation}
G(x, y) = -\frac{1}{4\pi\varepsilon_0} \log\left( \frac{x^2 + y^2}{r_0^2}\right)
\end{equation}
where $r_0$ is arbitrary constant which has no effect on the evaluated fields (changes the potential by an additive constant). 

The corresponding integrated Green function can be written as:
\begin{align}
G_{2LR}^\text{int}(x_{n_x}, y_{n_y}) =& 
\int_{x_{n_x}-\frac{h_x}{2}}^{x_{n_x}+\frac{h_x}{2}} dx
\int_{y_{n_y}-\frac{h_y}{2}}^{y_{n_y}+\frac{h_y}{2}} dy\,
G(x,y,z)\\
= &+F\left(x_{n_x}+\frac{h_x}{2}, y_{n_x}+\frac{h_y}{2}\right)\\
    &-F\left(x_{n_x}+\frac{h_x}{2}, y_{n_x}-\frac{h_y}{2}\right)\\
    &-F\left(x_{n_x}-\frac{h_x}{2}, y_{n_x}+\frac{h_y}{2}\right)\\ 
    &+F\left(x_{n_x}-\frac{h_x}{2}, y_{n_x}-\frac{h_y}{2}\right)
\end{align}
where $F(x,y)$ is a primitive of $G(x,y)$, which can be obtained as:
\begin{equation}
F(x,y) = 
\int_{x_0}^{x} dx
\int_{y_0}^{y} dy\,
G(x,y)
\end{equation}
where $(x_0, y_0)$ is an arbitrary starting point.

An expression for $F(x,y)$ is the following (where we have chosen $r_0=1$):
\begin{align}
F(x,y) &=-\frac{1}{4\pi\varepsilon_0}\iint \ln \left(x^{2}+y^{2}\right) dx/,dy\\
&=\frac{1}{4\pi\varepsilon_0}\left[3 x y-x^{2} \arctan (y / x)-y^{2} \arctan (x / y)-x y \ln \left(x^{2}+y^{2}\right)\right]
\end{align}

Note that we need to choose the first cell center to be in (0,0) for evaluation of the integrated Green function. Therefore the cell edges have non zero coordinates and the denominators in the formula will always be non-vanishing.

\section{Generalization to observation interval different from source interval}
The potential generated by a source $\rho(x)$ can be written as the convolution of a Green function with the charge distribution:
\begin{equation}
\phi(x) = \int_{-\infty}^{+\infty} \rho(x')\,G(x-x') dx'
\label{eq:conv_gen}
\end{equation}

We assume that the source is limited to the region  $[a, b]$:
\begin{equation}
\rho(x) = \rho(x)\,\Pi_{[a,b]}\left(x\right)
\label{eq:rholim_gen}
\end{equation}
where $\Pi_{[a,b]}(x)$ is a rectangular window function defined as:
\begin{equation}
\Pi_{[a,b]}(x) = 
\begin{cases}
1\quad\text{for } x \in [a, b]\\
0\quad\text{elsewhere}
\end{cases}
\end{equation}

We are interested in the electric potential in a given region $[c, d]$, so we can compute:
\begin{equation}
\phi_{cd}(x) = \phi(x) \Pi_{[c, d]}\left(x\right)
\label{eq:philim_gen}
\end{equation}

We combine Eqs.\,\eqref{eq:rholim_gen}, \eqref{eq:philim_gen} and \eqref{eq:conv_gen}, obtaining:
\begin{equation}
\phi_{cd}(x) = \Pi_{[c,d]}\left( x\right)
\int_{-\infty}^{+\infty} 
\Pi_{[a,b]}\left(x'\right)
\rho(x')\,G(x-x') dx'
\end{equation}
We apply the change of variable $x'' = x - x'$:
\begin{equation}
\phi_{cd}(x) = 
\int_{-\infty}^{+\infty} 
\Pi_{[c,d]}\left({x}\right)
\Pi_{[a,b]}\left({x-x''}\right)
\rho(x-x'')\,G(x'') \,dx''
\label{eq:conv1_gen}
\end{equation}
The integrand vanishes outside the set of the $(x, x'')$ defined by the two window functions:
\begin{equation}
\begin{cases}
c < x <d\\
a < (x-x'') <b
\end{cases}
\end{equation}

We flip the signs in the second equation, obtaining:
\begin{equation}
\begin{cases}
c < x < d\\
-b < (x''-x) <-a
\end{cases}
\end{equation}

Combining the two equations we obtain:
\begin{equation}
c-b<-b + x < x'' <-a+x<d-a
\end{equation}
i.e. the integrand is not zero for $c-b< x'' <d-a$.
Therefore in Eq.\,\eqref{eq:conv1_gen} we can replace $G(x'')$ with its truncated version:
\begin{equation}
G_\text{tr}(x'') = G(x'')\,\Pi_{[c-b,~d-a]}
\left(
{x''}
\right)
\end{equation}

obtaining:
\begin{equation}
\phi_{cd}(x) = 
\int_{-\infty}^{+\infty} 
\Pi_{[c,d]}\left({x}\right)
\Pi_{[a,b]}\left({x-x''}\right)
\rho(x-x'')\,G_\text{tr}(x'') dx''
\label{eq:conv2_gen}
\end{equation}


We can go back to the initial coordinate by substituting $x'' = x-x'$:
\begin{equation}
\phi_{cd}(x) = 
\Pi_{[c,d]}\left({x}\right)
\int_{-\infty}^{+\infty} 
\rho(x')\,G_\text{tr}(x-x') dx'
\end{equation}


We call:
\begin{align}
L_1 = b - a\\
L_2 = d - c 
\end{align}

The measure of the set on which $G_\text{tr}(x'')$ is non zero is 
%
\begin{equation}
(d-a) - (c - b) = L_1+ L_2
\end{equation}

We define $L$ such that:
\begin{equation}
L_1+ L_2 = 2L
\end{equation}

Since the two window functions in Eq.\,\ref{eq:conv2_gen} force the integrand to zero outside the region $c-b< x'' <d-a$ of measure $2L$, we can replace $G_\text{tr}(x'')$ with its replicated version:
\begin{equation}
G_{R}(x'') = \sum_{n=-\infty}^{+\infty}G_\text{tr}(x''-2nL) = \sum_{n=-\infty}^{+\infty}G(x'' -2nL)\,\Pi_{[c-b,~d-a]}
\left(
{x''-2nL}
\right)
\label{eq:GLR_gen}
\end{equation}
obtaining:
\begin{equation}
\phi_{cd}(x) = 
\int_{-\infty}^{+\infty} 
\Pi_{[c,d]}\left({x}\right)
\Pi_{[a,b]}\left({x-x''}\right)
\rho(x-x'')\,G_{R}(x'') dx''
\end{equation}

We can go back to the initial coordinate by substituting $x'' = x-x'$:
\begin{equation}
\phi_{cd}(x) = 
\Pi_{[c,d]}\left({x}\right)
\int_{-\infty}^{+\infty} 
\rho(x')\,G_{R}(x-x') dx'
\end{equation}

This is a cyclic convolution, so we can proceed as follows. We split the integral:
\begin{equation}
\phi_{cd}(x) = 
\Pi_{[c,d]}\left({x}\right)
\sum_{n=-\infty}^{+\infty}
\int_{2nL}^{2(n+1)L} 
\rho(x')\,G_{R}(x-x') \,dx'
\label{eq:conv3_gen}
\end{equation}
In each term we replace $x''' = x'+2nL$:
\begin{equation}
\phi_{cd}(x) = 
\Pi_{[c,d]}\left({x}\right)
\sum_{n=-\infty}^{+\infty}
\int_{0 }^{2L} 
\rho(x'''-2nL)\,G_{R}(x-x'''-2nL) \,dx'''
\label{eq:conv4_gen}
\end{equation}
We use the fact that $G_{R}(x)$ is periodic:
\begin{equation}
\begin{split}
\phi_{cd}(x) &= 
\Pi_{[c,d]}\left({x}\right)
\sum_{n=-\infty}^{+\infty}
\int_{0 }^{2L} 
\rho(x'''-2nL)\,G_{R}(x-x''') dx'''\\
\\&=
\Pi_{[c,d]}\left({x}\right)
\int_{0 }^{2L}  
G_{R}(x-x''')
\sum_{n=-\infty}^{+\infty}
\rho(x'''-2nL) 
\, dx'''
\end{split}
\label{eq:conv5_gen}
\end{equation}

We can define a replicated version of $\rho(x)$:
\begin{equation}
\rho_{R}(x)= \sum_{n=-\infty}^{+\infty}
\rho(x-2nL)
\end{equation}

%noting that this implies:
%\begin{equation}
%\rho_{2LR}(x)= 0 \quad \text{for } x \in [L, 2L]
%\label{eq:zeros}
%\end{equation}

We obtain:
\begin{equation}
\phi_{cd}(x) = 
\Pi_{[c,d]}\left({x}\right)
\int_{0 }^{2L} 
\rho_{R}(x')\,G_{R}(x-x') dx'
\label{eq:conv6_gen}
\end{equation}

The function:
\begin{equation}
\phi_{R}(x) = 
\int_{0 }^{2L} 
\rho_{R}(x')\,G_{R}(x-x') dx'
\label{eq:confin_gen}
\end{equation}
is periodic of period $2L$. Replacing in Eq.~\ref{eq:conv6_gen} we see that the potential of interest can be simply calculated by selecting the right interval $[c, d]$:
\begin{equation}
\phi_{cd}(x) = 
\Pi_{[c,d]}\left({x}\right)
\phi_{R}(x)
\label{eq:sel_gen}
\end{equation}

To compute the convolution in Eq.\,\ref{eq:confin_gen} we expand $\phi_{R}(x)$ in a Fourier series starting from $x=c$:
\begin{equation}
\phi_{R}(x) = \sum_{k=-\infty}^{+\infty} \tilde{\phi}_k\, e^{j2\pi k \frac{x}{2L}}
\label{eq:phifour_gen}
\end{equation}
where the Fourier coefficients are given by:
\begin{equation}
\tilde{\phi}_k = \frac{1}{2L}\int_0^{2L} \phi_{R}(x)\, e^{-j2\pi k \frac{x}{2L}} \, dx
\label{eq:phik_gen}
\end{equation}

We replace Eq.\,\eqref{eq:confin_gen} into Eq.\,\eqref{eq:phik_gen} obtaining:
\begin{equation}
\tilde{\phi}_k = \frac{1}{2L}\int_0^{2L} \int_{0 }^{2L} 
\rho_{R}(x')\,G_{R}(x-x') \, e^{-j2\pi k \frac{x}{2L}} \,  dx'\, dx
\end{equation}

With the change of variable $x'' = x-x'$ we obtain:
\begin{equation}
\tilde{\phi}_k = 
\frac{1}{2L}
\int_0^{2L} 
\rho_{R}(x') e^{-j2\pi k \frac{x'}{2L}}dx'\,
\int_{0 }^{2L} 
\,G_{R}(x'') e^{-j2\pi k \frac{x''}{2L}}\,  \,  dx''
\end{equation}

where we recognize the Fourier coefficients of $\rho_{R}(x)$ and $\,G_{R}(x)$:
\begin{align}
\tilde{\rho}_k = \frac{1}{2L}\int_0^{2L} \rho_{R}(x)\, e^{-j2\pi k \frac{x}{2L}} \, dx \label{eq:rhok_gen}\\
\tilde{G}_k = \frac{1}{2L}\int_0^{2L} G_{R}(x)\, e^{-j2\pi k \frac{x}{2L}} \, dx \label{eq:Gk_gen}
\end{align}
obtaining simply:
\begin{equation}
\tilde{\phi}_k = 2L \, \tilde{G}_k \, \tilde{\rho}_k
\label{eq:freqconv_gen}
\end{equation}

We assume to have the functions $\rho_{R}(x)$ and  $G_{R}(x)$ sampled (or averaged) with step:
\begin{equation}
h_x = \frac{2L}{M}
\end{equation}

We assume that all intervals have size multiple of $h_x$. So we can define:
\begin{align}
N_1 &= L_1 / h_x\\
N_2 &= L_2 / h_x
\end{align}

We call:
\begin{align}
 \rho_{Rn} &= \rho_{R}\left(a + nh_x\right)\\
 \phi_{Rn} &= \phi_{R}\left(c + nh_x\right)\\
 G_{Rn} &= G_{R}\left(c - b + nh_x\right)
\end{align}

By construction in the range $0 \leq n <M$:
\begin{equation}
 \rho_{Rn} \equiv \rho_n = 
 \begin{cases}
\rho\left(a + nh_x\right)&\text{for}~0 \leq n <N_1 \\
 0 & \text{for}~N_1 \leq n < M
 \end{cases}
\end{equation}

\begin{equation}
G_{Rn} \equiv  G_n = G\left(c - b + nh_x\right)
\text{ for}~0 \leq n <M
\end{equation}

We can approximate the integral as follows:
\begin{align}
\tilde{\rho}_k &= \frac{1}{2L}\int_0^{2L} \rho_{R}(x)\, e^{-j2\pi k \frac{x}{2L}} \, dx = \frac{1}{2L}\int_a^{a+2L} \rho_{R}(x)\, e^{-j2\pi k \frac{x}{2L}} \, dx\\
&\simeq 
\frac{h_x}{2L}\sum_{n=0}^{M-1} \rho_{R}(a+nh_x)\, e^{-j2\pi k \frac{a+nh_x}{2L}} 
=
e^{-j2\pi k \frac{a}{2L}} \frac{1}{M}
\sum_{n=0}^{M-1}
 \rho_{Rn} e^{-j2\pi \frac{kn}{M}} 
\end{align}

We recognize the Discrete Fourier Transform:
\begin{equation}
\tilde{\rho}_k 
=
e^{-j2\pi k \frac{a}{2L}} \frac{1}{M}
\text{DFT}_M\left\{ \rho_{Rn}\right\}
=
e^{-j2\pi k \frac{a}{2L}} \frac{1}{M}
\hat{\rho}_k 
\end{equation}
and similarly we can obtain
\begin{equation}
\tilde{\phi}_k 
=
e^{-j2\pi k \frac{c}{2L}} \frac{1}{M}
\text{DFT}_M\left\{ \phi_{Rn}\right\}
=
e^{-j2\pi k \frac{c}{2L}} \frac{1}{M}
\hat{\phi}_k 
\end{equation}

\begin{equation}
\tilde{G}_k 
=
e^{-j2\pi k \frac{c-b}{2L}} \frac{1}{M}
\text{DFT}_M\left\{ G_{Rn}\right\}
=
e^{-j2\pi k \frac{c-b}{2L}} \frac{1}{M}
\hat{G}_k 
\end{equation}


Replacing in Eq. \ref{eq:freqconv_gen} we obtain

\begin{equation}
\hat{\phi}_k  = 
h_x e^{j2\pi k \frac{b-a}{2L}} 
\hat{\rho}_k \hat{G}_k
=
h_x e^{j2\pi k \frac{N_1}{M}} 
\hat{\rho}_k \hat{G}_k
\end{equation}

\section{Compressed FFT convolution}

We assume that the source has the form
\begin{equation}
\rho(x) =  \sum_{j=A}^{B-1} \rho^\text{loc}_j(x - jP)
\label{eq:rholim_period}
\end{equation}
where $\rho^\text{loc}_j(x)$ is limited to the interval $[a, b]$.

We are interested in the potential in a set of intervals given by:
\begin{equation}
[c+iP,~d+iP]~~\text{ for } i=C, ... , D-1
\end{equation}
%so we define:
%\begin{equation}
%\phi_i(x) =  \phi(x+iP)\Pi_{[c,d]}(x)
%\label{eq:rholim_period}
%\end{equation}

The contribution of the j-th term of $\rho$ to  $\phi$ int the i-th interval:

\begin{equation}
\phi_{ij}(x) = 
\int_{-\infty}^{+\infty} 
\rho^\text{loc}_j(x' - jP)\,G^\text{tr}_{i-j}(x-x') dx'
\end{equation}

where:
\begin{equation}
G^\text{tr}_l(x'') = G(x'')\,
\Pi_{[c-b+lP,~d-a+lP]}
\left(
{x''}
\right)
\end{equation}

We define a local version of $G^\text{tr}$ as

\begin{equation}
G^\text{tr, loc}_l(x) = G^\text{tr}_l(x + lP) 
=
G(x +lP)\,
\Pi_{[c-b,~d-a]}
\left(x\right)
\end{equation}

obtaining:
\begin{equation}
\phi_{ij}(x) = 
\int_{-\infty}^{+\infty} 
\rho^\text{loc}_j(x' - jP)\,G^\text{tr, loc}_{i-j}(x-x'-(i-j)P) dx'
\end{equation}

We replace $x' = x' - jP$:
\begin{equation}
\phi_{ij}(x) = 
\int_{-\infty}^{+\infty} 
\rho^\text{loc}_j(x')\,G^\text{tr, loc}_{i-j}(x-x' - iP) dx'
\end{equation}

We define a local version of $\phi$:
\begin{equation}
\phi_{ij}^\text{loc}(x) = \phi_{ij}(x+iP)
\end{equation}

obtaining:
\begin{equation}
\phi_{ij}^\text{loc}(x) = \int_{-\infty}^{+\infty} 
\rho^\text{loc}_j(x')\,G^\text{tr, loc}_{i-j}(x-x') dx'
\end{equation}

I explicit all the pies:
\begin{equation}
\phi_{ij}^\text{loc}(x) = \int_{-\infty}^{+\infty}
\Pi_{[a,b]}(x')
\Pi_{[c-b,~d-a]}(x-x')
\rho^\text{loc}_j(x')\,G^\text{tr, loc}_{i-j}(x-x') dx'
\end{equation}

Again, we want to find the region in $x$ where this is non-zero:

\begin{align}
&a<x'<b\\
&c-b< x-x'<d-a
\end{align}

from which:
\begin{align}
&c-b+x'< x<d-a+x'\\
&c-b+a< x<d-a+b
\end{align}

So we find that $\phi_{ij}^\text{loc}(x)$ is non-zero in the region:
\begin{align}
&c-L_1< x<d+L_1
\label{eq:support}
\end{align}


The total potential in the i-th interval of interest:
\begin{equation}
\phi_{i}^\text{loc}(x)
=\sum_{j=A}^{B-1} \phi_{ij}^\text{loc}(x) = \sum_{j=A}^{B-1}\int_{-\infty}^{+\infty}
\rho^\text{loc}_j(x')\,G^\text{tr, loc}_{i-j}(x-x') dx'
\label{eq:phi_i_loc}
\end{equation}

Since all terms in the sum are zero outside the region defined by Eq.\,\ref{eq:support} also $\phi_{i}^\text{loc}(x)$ is zero outside the same interval, which is larger by $2L_1$ compared to the set of interest $[c, d]$.

We build:

\begin{equation}
G^\text{aux} (x)= \sum_{l=C-B+1}^{D-A-1}
G^\text{tr, loc}_{l}(x-l L_\text{aux})
\label{eq:def_gaux}
\end{equation}
where:
\begin{equation}
L_\text{aux} = L_1 + L_2
\end{equation}

and 
\begin{equation}
\rho^\text{aux}(x)= 
\sum_{j=A}^{B-1} \rho_{j}^\text{loc}(x - jL_\text{aux})
\label{eq:def_rhoaux}
\end{equation}

and we define 
\begin{equation}
\phi^\text{aux}(x)= 
\int_{-\infty}^{+\infty}
\rho^\text{aux}(x')\,G^\text{aux}(x-x') dx'
\label{eq:def_phiaux}
\end{equation}

We extract a segment of it:


\begin{equation}
\phi^\text{aux, loc}_i(x)= 
\phi^\text{aux}(x + iL_\text{aux}) 
\Pi_{[c, d]}(x)
\label{eq:def_phiauxloc}
\end{equation}

We replace Eq.~\ref{eq:def_gaux}:

\begin{equation}
\phi^\text{aux, loc}_i(x)= 
\Pi_{[c, d]}(x)
\int_{-\infty}^{+\infty}
\rho^\text{aux}(x')\,G^\text{aux}(x-x'+iL_\text{aux}) dx'
\end{equation}

We replace Eq.~\ref{eq:def_rhoaux} and Eq.~\ref{eq:def_gaux}:
\begin{align}
\phi^\text{aux, loc}_i(x)&= 
\Pi_{[c, d]}(x)
\int_{-\infty}^{+\infty}
\sum_{j=A}^{B-1} \rho_{j}^\text{loc}(x' - jL_\text{aux})\,
\sum_{l=C-B+1}^{D-A-1}
G^\text{tr, loc}_{l}(x-x'+(i-l)L_\text{aux})
dx'\\
&=
\Pi_{[c, d]}(x)
\sum_{l=C-B+1}^{D-A-1} \sum_{j=A}^{B-1} \int_{-\infty}^{+\infty}\rho_{j}^\text{loc}(x' - jL_\text{aux})\,
G^\text{tr, loc}_{l}(x-x'+(i-l)L_\text{aux})
dx'
\end{align}

We change variable $x''= x' - (i-l)L_\text{aux}$
\begin{align}
\phi^\text{aux, loc}_i(x)
&=
\sum_{l=C-B+1}^{D-A-1} \sum_{j=A}^{B-1} \int_{-\infty}^{+\infty}
\Pi_{[c, d]}(x)
\rho_{j}^\text{loc}(x'' + (i-l -j)L_\text{aux})\,
G^\text{tr, loc}_{l}(x-x'')
dx''
\end{align}

The integrand is nonzero for:
\begin{align}
& c < x <d\\
& a < x'' +(i-l-j)L_\text{aux}) < b\\
& c-b < x-x''< d-a
\end{align}

I subtract the first and the last:
\begin{align}
& a < x'' +(i-l-j)L_\text{aux}) < b\\
& -b < -x''< -a
\end{align}

I flip the last
\begin{align}
& a < x'' +(i-l-j)L_\text{aux}) < b\\
& a < x''< b
\end{align}

The two are compatible only if
\begin{align}
l=i-j
\label{eq:nonzeroterms}
\end{align}

This means that in the double sum only the terms satisfying Eq.\,\ref{eq:nonzeroterms} are nonzero, hence:
\begin{align}
\phi^\text{aux, loc}_i(x)
&=
\Pi_{[c, d]}(x)
\sum_{j=A}^{B-1} \int_{-\infty}^{+\infty}
\rho_{j}^\text{loc}(x'' + )\,
G^\text{tr, loc}_{i-j}(x-x'')
dx''
\end{align}

Comparing against Eq.\,\ref{eq:phi_i_loc}
we find:
\begin{align}
\Pi_{[c, d]}(x)\phi^\text{aux, loc}_i(x)
&=
\Pi_{[c, d]}(x)
\phi^\text{loc}_i(x)
\end{align}

Using Eq.\,\ref{eq:def_phiauxloc} we obtain:
\begin{align}
\Pi_{[c, d]}(x)
\phi^\text{loc}_i(x)
=
\Pi_{[c, d]}(x)\phi^\text{aux}(x + iL_\text{aux}) 
\end{align}

To compute the convolution in Eq.\,\ref{eq:def_phiaux} we can use the results from the previous section.

We call:
\begin{align}
&N_S = B-A\\
&N_T = D-C\\
\end{align}

The support of $\rho^\text{aux}(x)$ is:
\begin{equation}
[a +AL_\text{aux}, a + B L_\text{aux}]
\text{  having size  } N_S  L_\text{aux}
\end{equation}
The support of $G^\text{aux}(x)$ is:
\begin{equation}
[c-b +(C-B+1)L_\text{aux}, c-b + (D-A) L_\text{aux}]
\text{  having size  } \left(N_S + N_T -1\right) L_\text{aux}
\end{equation}

Using a sampling step $h_x$, we can define:
\begin{align}
N_1 &= L_1 / h_x\\
N_2 &= L_2 / h_x\\
N_\text{aux} &= L_\text{aux} / h_x = N_1+N_2
\end{align}
The number of samples in the support of $G^\text{aux}(x)$ is
\begin{align}
M_\text{aux} &= (N_S+N_T-1)N_\text{aux} 
\end{align}



We define
\begin{equation}
G^\text{aux}_m = G^\text{aux}\left(c-b +(C-B+1)L_\text{aux} + mh_x\right) 
\text{ for}~0 \leq m <M_\text{aux}
\end{equation}

Replacing Eq.\,\ref{eq:def_gaux}: 
\begin{eqnarray}
G^\text{aux}_m 
&=&
\sum_{l=C-B+1}^{D-A-1}
G^\text{tr, loc}_{l}(c-b +(C-B+1)L_\text{aux} + mh_x -l L_\text{aux})\\
&=&
\sum_{l=C-B+1}^{D-A-1}
G(c-b  +(C-B+1)L_\text{aux} +lP + h_x (m  -l N_\text{aux}) ) \times\\
&&
\Pi_{[c-b,~d-a]}(c-b +(C-B+1)L_\text{aux} + h_x(m -l N_\text{aux}))
\end{eqnarray}

We define:
\begin{multline}
G^\text{segm}_{l,n} =  G(c-b  +(C-B+1)L_\text{aux} +lP + n h_x ) 
\Pi_{[c-b,~d-a]}(c-b +(C-B+1)L_\text{aux} + n h_x)\\
\text{ for}~0 \leq n <N_\text{aux}
~\text{ and}~(C-B+1) \leq l < (D-A)
\end{multline}

So we can write:
\begin{equation}
G^\text{aux}_m = \sum_{l=C-B+1}^{D-A-1}
G^\text{segm}_{l,m-lN_\text{aux}}
\end{equation}
We define:
\begin{equation}
\rho^\text{aux}_m = 
 \begin{cases}
\rho^\text{aux}\left(a +AL_\text{aux} + mh_x\right) 
&\text{ for}~0 \leq m <N_S N_\text{aux}\\
 0 & \text{for}~N_S N_\text{aux} \leq m < M_\text{aux}
\end{cases}
\end{equation}

We can use the result from before linking the DFTs of these sequences:
\begin{equation}
\hat{\phi}^\text{aux}_k  = 
h_x 
e^{j2\pi k \frac{(B-A-1)L_\text{aux} + (b-a)}{(N_S + N_T-1)L_\text{aux}}}
=
h_x 
e^{j2\pi k \frac{(N_S-1)N_\text{aux} + N_1}{(N_S + N_T -1)N_\text{aux}}}
\hat{\rho}^\text{aux}_k \hat{G}^\text{aux}_k
\end{equation}

The inverse DFT of $\hat{\phi}^\text{aux}_k$ provides:
\begin{equation}
\phi^\text{aux}_m = 
\phi^\text{aux}\left(c +CL_\text{aux} + mh_x\right) 
\text{~~ for}~0 \leq m <N_T N_\text{aux}\\
\end{equation}
