% ===> this file was generated automatically by noweave --- better not edit it
\section{Introduction}

This model adds equations for a rigid body and constraints
to attach rigid bodies to other things.  The treatment is taken
from Chapter 12 of Zienkiewicz and Taylor~\cite{ZienTayl00b}.

The nodal variables are displacements in the $x$ and $y$ directions
and rotation about the $z$ axis.

\subsection{Interface}

\nwfilename{model-rigid.nw}\nwbegincode{1}\sublabel{NW1ipNXo-2o4OLZ-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-2o4OLZ-1}}}\moddef{model-rigid.h~{\nwtagstyle{}\subpageref{NW1ipNXo-2o4OLZ-1}}}\endmoddef\nwstartdeflinemarkup\nwenddeflinemarkup
#ifndef MODEL_RIGID_H
#define MODEL_RIGID_H

#include <modelmgr.h>

void model_rigid_register(model_mgr_t model_mgr);

#endif /* MODEL_RIGID_H */
\nwnotused{model-rigid.h}\nwendcode{}\nwbegindocs{2}\nwdocspar

\subsection{Rigid body equations}

% Equations of motion for the rigid body

The equations of motion for a rigid body with center of mass $R$
are the balance of linear and angular momentum:
\begin{eqnarray*}
  (m      r_{,t})_{,t} =   p_{,t} & = & \sum f_a \\
  (J \omega_{,t})_{,t} = \pi_{,t} & = & \sum (x_a - r) \times f_a
\end{eqnarray*}
Since we are working in two dimensions, $J$ is a scalar value
and not a 3-by-3 tensor, which makes life a little simpler.

\subsection{Rigid-flexible coupling}

% Lagrange multipliers

In general, our rigid bodies will be coupled to flexural elements
(beams).  We add the coupling by writing a set of constraint
equations that relate the rigid body displacements to the
displacements of attached nodes, and enforcing those constraints
via Lagrange multipliers.  For the moment, let $x$ be the vector
of unknowns for the flexible part, and $y$ be the vector of
unknowns for the rigid part.  Then we have two energy functionals
$I_1(x)$ and $I_2(y)$, and a coupling equation $C(x,y) = 0$.
According to Lagrange multipliers, to minimize $I_1 + I_2$
subject to the constraints, we should minimize
\[
  I(x, y, \lambda) = I_1(x) + I_2(y) + \lambda^T C(x,y)
\]

After we take variations of everything in sight, we end up with
the augmented residual equations
\[
  \begin{pmatrix}
    R_1(x) + \frac{\partial C}{\partial x}^T \lambda \\
    R_2(y) + \frac{\partial C}{\partial y}^T \lambda \\
    C(x, y)
  \end{pmatrix} = 0
\]
The tangent matrix is
\[
  \begin{pmatrix}
       K_1 &      0 & C_{,x}^T \\
         0 &    K_2 & C_{,y}^T \\
    C_{,x} & C_{,y} &        0
  \end{pmatrix}
\]

Note that the Lagrange multiplier variables $\lambda$ are purely
local, and could be eliminated on an element-by-element basis.
Right now, we leave them explicit in the system.

Let $u_1, u_2$ and $\theta$ be the displacement and rotation
nodal degrees of freedom for a beam node which is to be connected
to a rigid body.  The rigid body is described by the current position
of its center of mass ($r_1, r_2$) and the rotation matrix $Q(\omega)$
(rotation of $\omega$ in the plane).
If $R$ and $X$ are the reference positions for the rigid body
center of mass and the attached node, then
\[
  C =
  \begin{pmatrix}
    (r + Q(\omega) (X-R)) - x \\
    \omega - \theta
  \end{pmatrix} = 0
\]
There is one problem with these constraints -- the ``rotational''
parameter $\theta$ for the beam theory actually represents a
linearization, while $Q(\omega)$ is an honest rotation.  For
the moment, I cross my fingers and hope that the angles remain small
enough that replacing $\theta - \omega = O(\omega^2)$ with
$\theta - \omega = 0$ will not be too terrible.

\section{Implementation}

\nwenddocs{}\nwbegincode{3}\sublabel{NW1ipNXo-9ZqTB-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-9ZqTB-1}}}\moddef{model-rigid.c~{\nwtagstyle{}\subpageref{NW1ipNXo-9ZqTB-1}}}\endmoddef\nwstartdeflinemarkup\nwenddeflinemarkup
#include <sugar.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>

#include <model_rigid.h>
#include <affine.h>
#include <mesh.h>
#include <vars.h>
#include <assemble.h>
#include <netdraw.h>
#include <netout.h>

\LA{}types~{\nwtagstyle{}\subpageref{NW1ipNXo-4H4VOG-1}}\RA{}
\LA{}model functions~{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-1}}\RA{}
\LA{}registration function~{\nwtagstyle{}\subpageref{NW1ipNXo-CkEIP-1}}\RA{}
\nwnotused{model-rigid.c}\nwendcode{}\nwbegindocs{4}\nwdocspar

\nwenddocs{}\nwbegincode{5}\sublabel{NW1ipNXo-CkEIP-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-CkEIP-1}}}\moddef{registration function~{\nwtagstyle{}\subpageref{NW1ipNXo-CkEIP-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwenddeflinemarkup
void model_rigid_register(model_mgr_t model_mgr)
\{
    model_element_t model;

    \LA{}register models~{\nwtagstyle{}\subpageref{NW1ipNXo-uej2X-1}}\RA{}
\}
\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{6}\nwdocspar

\subsection{Rigid body model}

The user will eventually have to supply a mass and an inertia tensor,
but at the moment we just leave placeholders.  Note that the ``user''
here could well be a subnet or other higher-level abstraction.

\nwenddocs{}\nwbegincode{7}\sublabel{NW1ipNXo-4H4VOG-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-4H4VOG-1}}}\moddef{types~{\nwtagstyle{}\subpageref{NW1ipNXo-4H4VOG-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwprevnextdefs{\relax}{NW1ipNXo-4H4VOG-2}\nwenddeflinemarkup
typedef struct rigid_t \{
    element_t element;
    int node;
    int vars[3];
    double m;
    double J;
\} rigid_t;

\nwalsodefined{\\{NW1ipNXo-4H4VOG-2}}\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{8}\nwdocspar

\nwenddocs{}\nwbegincode{9}\sublabel{NW1ipNXo-3N4EZB-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-1}}}\moddef{model functions~{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwprevnextdefs{\relax}{NW1ipNXo-3N4EZB-2}\nwenddeflinemarkup
static element_t* rigid_init(mesh_t mesh, const char* model,
                                     model_element_t* modelfunc)
\{
    rigid_t* self = (rigid_t*)
        mempool_cget(mesh_pool(mesh), sizeof(*self));

    self->element.data = self;
    self->element.model = modelfunc;

    mesh_param_get_nodes(mesh, &(self->node), 1);

    return &(self->element);
\}

\nwalsodefined{\\{NW1ipNXo-3N4EZB-2}\\{NW1ipNXo-3N4EZB-3}\\{NW1ipNXo-3N4EZB-4}\\{NW1ipNXo-3N4EZB-5}\\{NW1ipNXo-3N4EZB-6}\\{NW1ipNXo-3N4EZB-7}\\{NW1ipNXo-3N4EZB-8}\\{NW1ipNXo-3N4EZB-9}}\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{10}\nwdocspar

I am not sure whether giving the rigid body variables the same names
as the corresponding beam nodal variables is such a good idea.  But
I will do it anyway, at least for the moment.

\nwenddocs{}\nwbegincode{11}\sublabel{NW1ipNXo-3N4EZB-2}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-2}}}\moddef{model functions~{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwprevnextdefs{NW1ipNXo-3N4EZB-1}{NW1ipNXo-3N4EZB-3}\nwenddeflinemarkup
static void rigid_vars(void* pself, vars_mgr_t vars)
\{
    rigid_t* self = (rigid_t*) pself;

    self->vars[0] = vars_node(vars, self->node, "x");
    self->vars[1] = vars_node(vars, self->node, "y");
    self->vars[2] = vars_node(vars, self->node, "rz");
\}

\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{12}\nwdocspar

Once dynamics are ready, we'll have to make appropriate additions. %'
Until then, we just need an output function.

\nwenddocs{}\nwbegincode{13}\sublabel{NW1ipNXo-3N4EZB-3}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-3}}}\moddef{model functions~{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwprevnextdefs{NW1ipNXo-3N4EZB-2}{NW1ipNXo-3N4EZB-4}\nwenddeflinemarkup
static void rigid_output(void* pself, netout_t* netout)
\{
    rigid_t* self = (rigid_t*) pself;

    netout_string        (netout, "model",     "rigid");
    netout_int           (netout, "node",      self->node);
    netout_int_matrix    (netout, "vars",      self->vars, 1, 3);
\}

\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{14}\nwdocspar

\nwenddocs{}\nwbegincode{15}\sublabel{NW1ipNXo-uej2X-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-uej2X-1}}}\moddef{register models~{\nwtagstyle{}\subpageref{NW1ipNXo-uej2X-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-CkEIP-1}}\nwprevnextdefs{\relax}{NW1ipNXo-uej2X-2}\nwenddeflinemarkup
memset(&model, 0, sizeof(model));
model.init    = rigid_init;
model.vars    = rigid_vars;
model.output  = rigid_output;
model_mgr_add_element(model_mgr, "rigid", &model);

\nwalsodefined{\\{NW1ipNXo-uej2X-2}}\nwused{\\{NW1ipNXo-CkEIP-1}}\nwendcode{}\nwbegindocs{16}\nwdocspar

\subsection{Coupling model}

\nwenddocs{}\nwbegincode{17}\sublabel{NW1ipNXo-4H4VOG-2}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-4H4VOG-2}}}\moddef{types~{\nwtagstyle{}\subpageref{NW1ipNXo-4H4VOG-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwprevnextdefs{NW1ipNXo-4H4VOG-1}{\relax}\nwenddeflinemarkup
typedef struct constraint2d_t \{
    element_t element;
    int node[2];
    int vars[9];
    double relpos[3];
\} constraint2d_t;

\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{18}\nwdocspar

\nwenddocs{}\nwbegincode{19}\sublabel{NW1ipNXo-3N4EZB-4}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-4}}}\moddef{model functions~{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwprevnextdefs{NW1ipNXo-3N4EZB-3}{NW1ipNXo-3N4EZB-5}\nwenddeflinemarkup
static element_t* constraint2d_init(mesh_t mesh, const char* model,
                                          model_element_t* modelfunc)
\{
    constraint2d_t* self = (constraint2d_t*)
        mempool_cget(mesh_pool(mesh), sizeof(*self));

    self->element.data = self;
    self->element.model = modelfunc;

    mesh_param_get_nodes(mesh, self->node, 2);

    return &(self->element);
\}

\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{20}\nwdocspar

\nwenddocs{}\nwbegincode{21}\sublabel{NW1ipNXo-3N4EZB-5}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-5}}}\moddef{model functions~{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwprevnextdefs{NW1ipNXo-3N4EZB-4}{NW1ipNXo-3N4EZB-6}\nwenddeflinemarkup
static void constraint2d_set_position(void* pself, mesh_t mesh)
\{
    constraint2d_t* self = (constraint2d_t*) pself;
    double* x1 = mesh_node(mesh, self->node[0])->x;
    double* x2 = mesh_node(mesh, self->node[1])->x;

    self->relpos[0] = x2[0] - x1[0];
    self->relpos[1] = x2[1] - x1[1];
    self->relpos[2] = x2[2] - x1[2];
\}

\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{22}\nwdocspar

\nwenddocs{}\nwbegincode{23}\sublabel{NW1ipNXo-3N4EZB-6}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-6}}}\moddef{model functions~{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwprevnextdefs{NW1ipNXo-3N4EZB-5}{NW1ipNXo-3N4EZB-7}\nwenddeflinemarkup
static void constraint2d_vars(void* pself, vars_mgr_t vars)
\{
    constraint2d_t* self = (constraint2d_t*) pself;

    self->vars[0] = vars_node(vars, self->node[0], "x");
    self->vars[1] = vars_node(vars, self->node[0], "y");
    self->vars[2] = vars_node(vars, self->node[0], "rz");

    self->vars[3] = vars_node(vars, self->node[1], "x");
    self->vars[4] = vars_node(vars, self->node[1], "y");
    self->vars[5] = vars_node(vars, self->node[1], "rz");

    /* Multiplier variables */
    self->vars[6] = vars_branch(vars, "lx" );
    self->vars[7] = vars_branch(vars, "ly" );
    self->vars[8] = vars_branch(vars, "lrz");
\}

\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{24}\nwdocspar

Recall that the constraint equations are
\[
  C =
  \begin{pmatrix}
    (r + Q(\omega) (X-R)) - x \\
    \omega - \theta
  \end{pmatrix} = 0
\]
and the corresponding tangent matrix (in block form) is
\[
  \frac{\partial C}{\partial (r, \omega, x, \theta)} =
    \begin{pmatrix}
      -I & \frac{\partial Q}{\partial \omega} (X-R) & -I &  0 \\
       0 & 1                                        &  0 & -1
    \end{pmatrix}
\]

\nwenddocs{}\nwbegincode{25}\sublabel{NW1ipNXo-VTJdx-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-VTJdx-1}}}\moddef{contribute $\partial C / \partial (x, \theta)$~{\nwtagstyle{}\subpageref{NW1ipNXo-VTJdx-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-3N4EZB-8}}\nwenddeflinemarkup
Kij(7,4) = Kij(4,7) = -1;
Kij(8,5) = Kij(5,8) = -1;
Kij(9,6) = Kij(6,9) = -1;
\nwused{\\{NW1ipNXo-3N4EZB-8}}\nwendcode{}\nwbegindocs{26}\nwdocspar

\nwenddocs{}\nwbegincode{27}\sublabel{NW1ipNXo-2wGS1U-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-2wGS1U-1}}}\moddef{contribute $\partial C^T \lambda / \partial (x, \theta)$~{\nwtagstyle{}\subpageref{NW1ipNXo-2wGS1U-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-3N4EZB-7}}\nwenddeflinemarkup
Ri(4) = -xi(7);
Ri(5) = -xi(8);
Ri(6) = -xi(9);
\nwused{\\{NW1ipNXo-3N4EZB-7}}\nwendcode{}\nwbegindocs{28}\nwdocspar

\nwenddocs{}\nwbegincode{29}\sublabel{NW1ipNXo-tMd7t-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-tMd7t-1}}}\moddef{contribute $\partial C / \partial (r, \omega)$~{\nwtagstyle{}\subpageref{NW1ipNXo-tMd7t-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-3N4EZB-8}}\nwenddeflinemarkup
Kij(7,1) = Kij(1,7) =  1;
Kij(8,2) = Kij(2,8) =  1;

Kij(7,3) = Kij(3,7) = \LA{}$(\partial Q(X-R) / \partial \omega)_1$~{\nwtagstyle{}\subpageref{NW1ipNXo-JnnYx-1}}\RA{};
Kij(8,3) = Kij(3,8) = \LA{}$(\partial Q(X-R) / \partial \omega)_2$~{\nwtagstyle{}\subpageref{NW1ipNXo-KQhz1-1}}\RA{};
Kij(9,3) = Kij(3,9) =  1;

\nwused{\\{NW1ipNXo-3N4EZB-8}}\nwendcode{}\nwbegindocs{30}\nwdocspar

\nwenddocs{}\nwbegincode{31}\sublabel{NW1ipNXo-2YwHlo-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-2YwHlo-1}}}\moddef{contribute $\partial C^T \lambda / \partial (r, \omega)$~{\nwtagstyle{}\subpageref{NW1ipNXo-2YwHlo-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-3N4EZB-7}}\nwenddeflinemarkup
Ri(1) = xi(7);
Ri(2) = xi(8);
Ri(3) = \LA{}$(\partial Q(X-R) / \partial \omega)_1$~{\nwtagstyle{}\subpageref{NW1ipNXo-JnnYx-1}}\RA{} * xi(7) +
        \LA{}$(\partial Q(X-R) / \partial \omega)_1$~{\nwtagstyle{}\subpageref{NW1ipNXo-JnnYx-1}}\RA{} * xi(8) +
        xi(9);
\nwused{\\{NW1ipNXo-3N4EZB-7}}\nwendcode{}\nwbegindocs{32}\nwdocspar

\nwenddocs{}\nwbegincode{33}\sublabel{NW1ipNXo-3Haqk6-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-3Haqk6-1}}}\moddef{contribute $C(x, y)$~{\nwtagstyle{}\subpageref{NW1ipNXo-3Haqk6-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-3N4EZB-7}}\nwenddeflinemarkup
Ri(7) = xi(1) - xi(4) + (\LA{}$(Q(X-R))_1$~{\nwtagstyle{}\subpageref{NW1ipNXo-4AcJSx-1}}\RA{} - relpos[0]);
Ri(8) = xi(2) - xi(5) + (\LA{}$(Q(X-R))_2$~{\nwtagstyle{}\subpageref{NW1ipNXo-4A44sR-1}}\RA{} - relpos[1]);
Ri(9) = xi(3) - xi(6);
\nwused{\\{NW1ipNXo-3N4EZB-7}}\nwendcode{}\nwbegindocs{34}\nwdocspar

\nwenddocs{}\nwbegincode{35}\sublabel{NW1ipNXo-4AcJSx-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-4AcJSx-1}}}\moddef{$(Q(X-R))_1$~{\nwtagstyle{}\subpageref{NW1ipNXo-4AcJSx-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-3Haqk6-1}}\nwenddeflinemarkup
( c * relpos[0]  +  -s * relpos[1])
\nwused{\\{NW1ipNXo-3Haqk6-1}}\nwendcode{}\nwbegindocs{36}\nwdocspar

\nwenddocs{}\nwbegincode{37}\sublabel{NW1ipNXo-4A44sR-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-4A44sR-1}}}\moddef{$(Q(X-R))_2$~{\nwtagstyle{}\subpageref{NW1ipNXo-4A44sR-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-3Haqk6-1}}\nwenddeflinemarkup
( s * relpos[0]  +   c * relpos[1])
\nwused{\\{NW1ipNXo-3Haqk6-1}}\nwendcode{}\nwbegindocs{38}\nwdocspar

\nwenddocs{}\nwbegincode{39}\sublabel{NW1ipNXo-JnnYx-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-JnnYx-1}}}\moddef{$(\partial Q(X-R) / \partial \omega)_1$~{\nwtagstyle{}\subpageref{NW1ipNXo-JnnYx-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-tMd7t-1}\\{NW1ipNXo-2YwHlo-1}}\nwenddeflinemarkup
(-s * relpos[0]  +  -c * relpos[1])
\nwused{\\{NW1ipNXo-tMd7t-1}\\{NW1ipNXo-2YwHlo-1}}\nwendcode{}\nwbegindocs{40}\nwdocspar

\nwenddocs{}\nwbegincode{41}\sublabel{NW1ipNXo-KQhz1-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-KQhz1-1}}}\moddef{$(\partial Q(X-R) / \partial \omega)_2$~{\nwtagstyle{}\subpageref{NW1ipNXo-KQhz1-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-tMd7t-1}}\nwenddeflinemarkup
( c * relpos[0]  +  -s * relpos[1])
\nwused{\\{NW1ipNXo-tMd7t-1}}\nwendcode{}\nwbegindocs{42}\nwdocspar

\nwenddocs{}\nwbegincode{43}\sublabel{NW1ipNXo-41Jggm-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-41Jggm-1}}}\moddef{define locals for residual computation~{\nwtagstyle{}\subpageref{NW1ipNXo-41Jggm-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-3N4EZB-7}\\{NW1ipNXo-3N4EZB-8}}\nwenddeflinemarkup
#define xi(i) xlocal[(i)-1]
double xlocal[9];

double* relpos = self->relpos;
double  c, s;

assemble_matrix_add(x, self->vars, 9, xlocal);
c = cos(xi(6));
s = sin(xi(6));
\nwused{\\{NW1ipNXo-3N4EZB-7}\\{NW1ipNXo-3N4EZB-8}}\nwendcode{}\nwbegindocs{44}\nwdocspar

\nwenddocs{}\nwbegincode{45}\sublabel{NW1ipNXo-3N4EZB-7}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-7}}}\moddef{model functions~{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwprevnextdefs{NW1ipNXo-3N4EZB-6}{NW1ipNXo-3N4EZB-8}\nwenddeflinemarkup
static void constraint2d_R(void* pself, assemble_matrix_t *R,
                                 assemble_matrix_t* x,
                                 assemble_matrix_t* v,
                                 assemble_matrix_t* a)
\{
    constraint2d_t* self = (constraint2d_t*) pself;

    #define Ri(i) Rlocal[(i)-1]
    double Rlocal[9];

    \LA{}define locals for residual computation~{\nwtagstyle{}\subpageref{NW1ipNXo-41Jggm-1}}\RA{}
    memset(Rlocal, 0, sizeof(Rlocal));

    \LA{}contribute $\partial C^T \lambda / \partial (x, \theta)$~{\nwtagstyle{}\subpageref{NW1ipNXo-2wGS1U-1}}\RA{}
    \LA{}contribute $\partial C^T \lambda / \partial (r, \omega)$~{\nwtagstyle{}\subpageref{NW1ipNXo-2YwHlo-1}}\RA{}
    \LA{}contribute $C(x, y)$~{\nwtagstyle{}\subpageref{NW1ipNXo-3Haqk6-1}}\RA{}

    #undef xi
    #undef Ri

    assemble_matrix_add(R, self->vars, 9, Rlocal);
\}

\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{46}\nwdocspar

\nwenddocs{}\nwbegincode{47}\sublabel{NW1ipNXo-3N4EZB-8}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-8}}}\moddef{model functions~{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwprevnextdefs{NW1ipNXo-3N4EZB-7}{NW1ipNXo-3N4EZB-9}\nwenddeflinemarkup
static void constraint2d_dR(void* pself, assemble_matrix_t* dR,
                                  double cx, assemble_matrix_t* x,
                                  double cv, assemble_matrix_t* v,
                                  double ca, assemble_matrix_t* a)
\{
    constraint2d_t* self = (constraint2d_t*) pself;

    #define Kij(i,j) Klocal[(i) + (j)*9 - 10]
    double Klocal[81];

    \LA{}define locals for residual computation~{\nwtagstyle{}\subpageref{NW1ipNXo-41Jggm-1}}\RA{}
    memset(Klocal, 0, sizeof(Klocal));

    \LA{}contribute $\partial C / \partial (x, \theta)$~{\nwtagstyle{}\subpageref{NW1ipNXo-VTJdx-1}}\RA{}
    \LA{}contribute $\partial C / \partial (r, \omega)$~{\nwtagstyle{}\subpageref{NW1ipNXo-tMd7t-1}}\RA{}

    #undef xi
    #undef Kij

    assemble_matrix_add(dR, self->vars, 9, Klocal);
\}

\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{48}\nwdocspar

\nwenddocs{}\nwbegincode{49}\sublabel{NW1ipNXo-3N4EZB-9}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-9}}}\moddef{model functions~{\nwtagstyle{}\subpageref{NW1ipNXo-3N4EZB-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-9ZqTB-1}}\nwprevnextdefs{NW1ipNXo-3N4EZB-8}{\relax}\nwenddeflinemarkup
static void constraint2d_output(void* pself, netout_t* netout)
\{
    constraint2d_t* self = (constraint2d_t*) pself;

    netout_string        (netout, "model",     "constraint2d");
    netout_int_matrix    (netout, "node",      self->node, 1, 2);
    netout_int_matrix    (netout, "vars",      self->vars, 1, 9);
\}

\nwused{\\{NW1ipNXo-9ZqTB-1}}\nwendcode{}\nwbegindocs{50}\nwdocspar

\nwenddocs{}\nwbegincode{51}\sublabel{NW1ipNXo-uej2X-2}\nwmargintag{{\nwtagstyle{}\subpageref{NW1ipNXo-uej2X-2}}}\moddef{register models~{\nwtagstyle{}\subpageref{NW1ipNXo-uej2X-1}}}\plusendmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW1ipNXo-CkEIP-1}}\nwprevnextdefs{NW1ipNXo-uej2X-1}{\relax}\nwenddeflinemarkup
memset(&model, 0, sizeof(model));
model.init         = constraint2d_init;
model.set_position = constraint2d_set_position;
model.vars         = constraint2d_vars;
model.R            = constraint2d_R;
model.dR           = constraint2d_dR;
model.output       = constraint2d_output;
model_mgr_add_element(model_mgr, "constraint",   &model);
model_mgr_add_element(model_mgr, "constraint2d", &model);

\nwused{\\{NW1ipNXo-CkEIP-1}}\nwendcode{}

\nwixlogsorted{c}{{$(\partial Q(X-R) / \partial \omega)_1$}{NW1ipNXo-JnnYx-1}{\nwixu{NW1ipNXo-tMd7t-1}\nwixu{NW1ipNXo-2YwHlo-1}\nwixd{NW1ipNXo-JnnYx-1}}}%
\nwixlogsorted{c}{{$(\partial Q(X-R) / \partial \omega)_2$}{NW1ipNXo-KQhz1-1}{\nwixu{NW1ipNXo-tMd7t-1}\nwixd{NW1ipNXo-KQhz1-1}}}%
\nwixlogsorted{c}{{$(Q(X-R))_1$}{NW1ipNXo-4AcJSx-1}{\nwixu{NW1ipNXo-3Haqk6-1}\nwixd{NW1ipNXo-4AcJSx-1}}}%
\nwixlogsorted{c}{{$(Q(X-R))_2$}{NW1ipNXo-4A44sR-1}{\nwixu{NW1ipNXo-3Haqk6-1}\nwixd{NW1ipNXo-4A44sR-1}}}%
\nwixlogsorted{c}{{contribute $\partial C / \partial (r, \omega)$}{NW1ipNXo-tMd7t-1}{\nwixd{NW1ipNXo-tMd7t-1}\nwixu{NW1ipNXo-3N4EZB-8}}}%
\nwixlogsorted{c}{{contribute $\partial C / \partial (x, \theta)$}{NW1ipNXo-VTJdx-1}{\nwixd{NW1ipNXo-VTJdx-1}\nwixu{NW1ipNXo-3N4EZB-8}}}%
\nwixlogsorted{c}{{contribute $\partial C^T \lambda / \partial (r, \omega)$}{NW1ipNXo-2YwHlo-1}{\nwixd{NW1ipNXo-2YwHlo-1}\nwixu{NW1ipNXo-3N4EZB-7}}}%
\nwixlogsorted{c}{{contribute $\partial C^T \lambda / \partial (x, \theta)$}{NW1ipNXo-2wGS1U-1}{\nwixd{NW1ipNXo-2wGS1U-1}\nwixu{NW1ipNXo-3N4EZB-7}}}%
\nwixlogsorted{c}{{contribute $C(x, y)$}{NW1ipNXo-3Haqk6-1}{\nwixd{NW1ipNXo-3Haqk6-1}\nwixu{NW1ipNXo-3N4EZB-7}}}%
\nwixlogsorted{c}{{define locals for residual computation}{NW1ipNXo-41Jggm-1}{\nwixd{NW1ipNXo-41Jggm-1}\nwixu{NW1ipNXo-3N4EZB-7}\nwixu{NW1ipNXo-3N4EZB-8}}}%
\nwixlogsorted{c}{{model functions}{NW1ipNXo-3N4EZB-1}{\nwixu{NW1ipNXo-9ZqTB-1}\nwixd{NW1ipNXo-3N4EZB-1}\nwixd{NW1ipNXo-3N4EZB-2}\nwixd{NW1ipNXo-3N4EZB-3}\nwixd{NW1ipNXo-3N4EZB-4}\nwixd{NW1ipNXo-3N4EZB-5}\nwixd{NW1ipNXo-3N4EZB-6}\nwixd{NW1ipNXo-3N4EZB-7}\nwixd{NW1ipNXo-3N4EZB-8}\nwixd{NW1ipNXo-3N4EZB-9}}}%
\nwixlogsorted{c}{{model-rigid.c}{NW1ipNXo-9ZqTB-1}{\nwixd{NW1ipNXo-9ZqTB-1}}}%
\nwixlogsorted{c}{{model-rigid.h}{NW1ipNXo-2o4OLZ-1}{\nwixd{NW1ipNXo-2o4OLZ-1}}}%
\nwixlogsorted{c}{{register models}{NW1ipNXo-uej2X-1}{\nwixu{NW1ipNXo-CkEIP-1}\nwixd{NW1ipNXo-uej2X-1}\nwixd{NW1ipNXo-uej2X-2}}}%
\nwixlogsorted{c}{{registration function}{NW1ipNXo-CkEIP-1}{\nwixu{NW1ipNXo-9ZqTB-1}\nwixd{NW1ipNXo-CkEIP-1}}}%
\nwixlogsorted{c}{{types}{NW1ipNXo-4H4VOG-1}{\nwixu{NW1ipNXo-9ZqTB-1}\nwixd{NW1ipNXo-4H4VOG-1}\nwixd{NW1ipNXo-4H4VOG-2}}}%
\nwbegindocs{52}\nwdocspar
\nwenddocs{}


\documentclass{article}
\author{Oliver Kisielius}
\title{Circuit design calculations}
\def\iff{\leftrightarrow}
\def\Z{\mathbb{Z}}
\def\R{\mathbb{R}}
\def\TF{\therefore}
\def\arraystretch{1.3}% Add space between rows of tables.
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{pdfpages}
\setlength{\parindent}{0pt}
\begin{document}
\maketitle

\section*{Note}

I'm not an electrical engineer. Of course the warning in the MIT license applies to
everything in this repository, but I'll say a few redundant words here.

I've done everything that occurred to me to make the design safe for
people and hardware.  ``Safe for hardware'' means that, if it works
the way I intended, my circuit shouldn't have some failure mode where
the 12 volts from the gaggle of AA batteries ends up ruining the
Raspberry Pi, the load cell, or any other component. But I could be
wrong.

I don't know how I could possibly mess ``safe for people''. But what do I know? I'm not
an electrical engineer.

%% Thanks to this answer:
%% https://stackoverflow.com/questions/2739159/inserting-a-pdf-file-in-latex
\includepdf[pages={1}]{schematic.pdf}

In broad strokes, here's why I did things the way I did:
\begin{itemize}
\item The AD8237 units are instrumentation amplifiers. You can find their data sheets
  online.
  \begin{itemize}
    \item One AD8237 monitors the battery

Look at $R_1$, $R_3$, and $R_5$.

\end{document}


\documentclass[a4paper,12pt]{report}

\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{placeins}

\usepackage[english]{babel}
\usepackage{booktabs}
\usepackage{stfloats}
\usepackage[T1]{fontenc}

\usepackage[titletoc,title]{appendix}

\usepackage{float}
\restylefloat{table}

\usepackage{longtable}

\usepackage{xcolor,colortbl}

\usepackage{mathpazo} % Palatino
\usepackage{avant}    % Avant Garde

\usepackage[margin=2.6cm, bottom=3.cm, top=3.cm, portrait]{geometry}\usepackage{caption}\usepackage[a4paper]{hyperref}\usepackage{adjustbox}\usepackage{enumitem}

 \usepackage[stable]{footmisc}

\renewcommand{\vec}[1]{\mathbf{#1}}

\setlength\parindent{0pt}

\newcommand{\HRule}[1]{\hfill \rule{0.2\linewidth}{#1}}

\makeatletter
\newcommand\primitiveinput[1]
{\@@input #1 }
\makeatother

\definecolor{Gray}{gray}{0.90}

\renewcommand\thesection{\arabic{section}}

%\renewcommand\appendixname{Appendix}
%\renewcommand\appendixpagename{Appendix}

\begin{document}

\thispagestyle{empty} % Remove page numbering on this page

%----------------------------------------------------------------------------------------
%	TITLE SECTION
%----------------------------------------------------------------------------------------

\colorbox{Gray}{
	\parbox[t]{1.0\linewidth}{
		\centering \fontsize{28pt}{40pt}\selectfont % The first argument for fontsize is the font size of the text and the second is the line spacing - you may need to play with these for your particular title
		\vspace*{0.7cm} % Space between the start of the title and the top of the grey box

		\hfill Xfields \\
		\hfill physics manual

%		\hfill Beam Screens\\

		\vspace*{0.7cm} % Space between the end of the title and the bottom of the grey box
	}
}

%----------------------------------------------------------------------------------------

%\vfill
%                \hfill {\huge Bunch length 1.0 ns}
                \vfill

%----------------------------------------------------------------------------------------
%	AUTHOR NAME AND INFORMATION SECTION
%----------------------------------------------------------------------------------------

{\centering \large
\hfill Giovanni Iadarola \\
%\hfill \\
\hfill CERN - Geneva, Switzerland

\HRule{1pt}} % Horizontal line, thickness changed here

%----------------------------------------------------------------------------------------

\clearpage % Whitespace to the end of the page

\newpage

\tableofcontents

\newpage

\renewcommand*{\arraystretch}{1.4}

\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 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 Lorenz 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 on 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}

\section{Beam-beam interaction}

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 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$ is   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 a limited support (not of interest) and for larger values has a not defined standard deviation.

\section{FFT Poisson solver}

\subsection{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}_n = \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}_{n_xn_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}

\subsection{FFT convolution - 1D case}
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(\frac{x}{L}\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(\frac{x}{L}\right)
\Pi_{[0,L]}\left(\frac{x-x''}{L}\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(
\frac{x''-2nL}{2L}
\right)
\label{eq:GLR}
\end{equation}
obtaining:
\begin{equation}
\phi_L(x) =
\int_{-\infty}^{+\infty}
\Pi_{[0,L]}\left(\frac{x}{L}\right)
\Pi_{[0,L]}\left(\frac{x-x''}{L}\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(\frac{x}{L}\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(\frac{x}{L}\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(\frac{x}{L}\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(\frac{x}{L}\right)
\sum_{n=-\infty}^{+\infty}
\int_{0 }^{2L}
\rho(x'''-2nL)\,G_{2LR}(x-x''') dx'''\\
\\&=
\Pi_{[0,L]}\left(\frac{x}{L}\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(\frac{x}{L}\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(\frac{x}{L}\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}

\subsection{Extension to multiple dimensionss}

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}

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

\subsubsection{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 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.

\subsubsection{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,z)$ is the following (where we have chosen $r_0=1$:
\begin{align}
F(x,y,z) &=-\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.

%\begin{thebibliography}{99}
%
%
%
%
%\end{thebibliography}

\end{document}


% Abstract ====================================================================
\pdfbookmark[1]{Abstract}{Abstract}
\chapter*{Abstract}

Several real-world binary classification problems are example-dependent cost-sensitive in nature, where the
costs due to misclassification vary between examples and not only within classes. However, standard binary
classification methods do not take these costs into account, and assume a constant cost of
misclassification errors. This approach is not realistic in many real-world applications. For
example in credit card fraud detection, failing to detect a fraudulent transaction may have an
economical impact from a few to thousands of Euros, depending on the particular transaction and card
holder. In churn modeling, a model is used for predicting which customers are more likely to
abandon a service provider. In this context, failing to identify a   profitable or unprofitable
churner has a significant different economic   result. Similarly, in direct marketing, wrongly
predicting that a customer   will not accept an offer when in fact he will, may have different
financial impact, as not all   customers generate the same profit. Lastly, in credit scoring,
accepting   loans from bad customers does not have the same economical loss, since customers have
different   credit lines, therefore, different profit.

Accordingly, the goal of this thesis is to provide an in-depth analysis of example-dependent
cost-sensitive classification. We analyze four real-world classification problems, namely,
credit card fraud detection, credit scoring, churn modeling and direct marketing. For each problem,
we propose an example-dependent cost-sensitive evaluation measure.

We propose four example-dependent cost-sensitive methods; the first method is a cost-sensitive
Bayes minimum risk classifier which consists in quantifying tradeoffs between various decisions
using probabilities and the costs that accompany such decisions. Second, we propose a
cost-sensitive logistic regression technique. This algorithm is based on a new logistic regression
cost function; one that takes into account the real costs due to misclassification and correct
classification. Subsequently, we propose a cost-sensitive decision trees algorithm which is based
on incorporating the different example-dependent costs into a new cost-based impurity measure and a
new cost-based pruning criteria. Lastly, we define an example-dependent cost-sensitive framework
for ensembles of decision-trees. It is based on training example-dependent cost-sensitive
decision trees using four different random inducer methods and then blending them using three
different combination approaches. Moreover, we present the library \mbox{\textit{CostCla}} developed
as part of the thesis. This library is an open-source implementation of all the algorithms covered
in this manuscript.

Finally, the experimental results show the importance of using the real example-dependent financial
costs associated with real-world applications. We found that there are significant differences
in the results when evaluating a model using a traditional cost-insensitive measure such as
accuracy or F1Score, than when using the financial savings. Moreover, the results show that the
proposed algorithms have better results for all databases, in the sense of higher savings.