% USEFUL LINKS:
% -------------
%
% - UiO LaTeX guides:          https://www.mn.uio.no/ifi/tjenester/it/hjelp/latex/
% - Mathematics:               https://en.wikibooks.org/wiki/LaTeX/Mathematics
% - Physics:                   https://ctan.uib.no/macros/latex/contrib/physics/physics.pdf
% - Basics of Tikz:            https://en.wikibooks.org/wiki/LaTeX/PGF/Tikz
% - All the colors!            https://en.wikibooks.org/wiki/LaTeX/Colors
% - How to make tables:        https://en.wikibooks.org/wiki/LaTeX/Tables
% - Code listing styles:       https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings
% - \includegraphics           https://en.wikibooks.org/wiki/LaTeX/Importing_Graphics
% - Learn more about figures:  https://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions
% - Automagic bibliography:    https://en.wikibooks.org/wiki/LaTeX/Bibliography_Management  (this one is kinda difficult the first time)
%
%                              (This document is of class "revtex4-1", the REVTeX Guide explains how the class works)
%   REVTeX Guide:              http://www.physics.csbsju.edu/370/papers/Journal_Style_Manuals/auguide4-1.pdf
%
%
% COMPILING THE .pdf FILE IN THE LINUX TERMINAL
% ---------------------------------------------
%
% [terminal]$ pdflatex report_example.tex
%
% Run the command twice, always.
%
% When using references, footnotes, etc. you should run the following chain of commands:
%
% [terminal]$ pdflatex report_example.tex
% [terminal]$ bibtex report_example
% [terminal]$ pdflatex report_example.tex
% [terminal]$ pdflatex report_example.tex
%
% This series of commands can of course be gathered into a single-line command:
% [terminal]$ pdflatex report_example.tex && bibtex report_example.aux && pdflatex report_example.tex && pdflatex report_example.tex
%
% ----------------------------------------------------



% \documentclass[english,notitlepage,reprint,nofootinbib]{revtex4-2}  % defines the basic parameters of the document
\documentclass[english,notitlepage,reprint,nofootinbib]{revtex4-2}  % defines the basic parameters of the document
% If you want a single-column, remove "reprint"
\usepackage{silence}
\WarningFilter{revtex4-2}{Repair the float}

% Allows special characters (including æøå)
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

% Note that you may need to download some of these packages manually, it depends on your setup.
% It may be usefult to download TeXMaker, because it includes a large library of the most common packages.

\usepackage{amssymb}  % mathematical symbols (physics imports amsmath)
\usepackage{amsmath}
\usepackage{graphicx}         % include graphics such as plots
\usepackage{xcolor}           % set colors
\usepackage{hyperref}         % automagic cross-referencing
\usepackage{listings}         % display code
\usepackage{subfigure}        % imports a lot of cool and useful figure commands
\usepackage{float}
%\usepackage[section]{placeins}
\usepackage{algorithm}
\usepackage{booktabs}
\usepackage[noend]{algpseudocode}
\usepackage{subfigure}
\usepackage{tikz}
\usepackage{cleveref}
\usepackage{siunitx}
% Define amu = Dalton
\DeclareSIUnit\amu{u}

\usepackage{todonotes}
\usetikzlibrary{quantikz}
% defines the color of hyperref objects
% Blending two colors:  blue!80!black  =  80% blue and 20% black
\hypersetup{ % this is just my personal choice, feel free to change things
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}}


% ===========================================
\newcommand{\vstat}{V_\mathrm{stat.}}
\newcommand{\vdyn}{V_\mathrm{dyn.}}
\newcommand{\estat}{E_\mathrm{stat.}}
\newcommand{\edyn}{E_\mathrm{dyn.}}
\newcommand{\estatc}[1]{E_{\mathrm{stat.},#1}}
\newcommand{\edync}[1]{E_{\mathrm{dyn.},#1}}

\begin{document}

\title{Accuracy and Stability of Numerical Integration Methods in Penning Trap Simulations}  % self-explanatory
\author{Lars Bogner} % self-explanatory
\date{\today}                             % self-explanatory
\noaffiliation                            % ignore this, but keep it.

%This is how we create an abstract section.
\begin{abstract}
    Using integration algorithms for ordinary differential equations, we predict the time evolution of charged particles in the electromagnetic fields of a Penning trap. Comparing the computational cost and numerical accuracy of the Forward Euler, Velocity-Verlet, Boris and Runge-Kutta 4th order methods, we find that the Boris algorithm is the most efficient choice for this problem. It is phase-space preserving, energy conserving and reduces the computational cost by up to \qty{700}{\percent} compared to general purpose approaches. To study ensemble properties of the trapped particles, we simulate the dynamics of 100 particles to find resonances and observe energy conservation of the different algorithms. Comparing to analytical solutions, at \num{32000} time steps of $h = \qty{1.56}{\ns}$, the Boris algorithm achieves a relative numerical error of the trajectory of less than \num{2e-3}.
\end{abstract}
\maketitle


% ===========================================
\section{Introduction}
\input{chapters/introduction}

% ===========================================
\section{Methods}\label{sec:methods}
%
\input{chapters/methods}


% ===========================================
\section{Results and discussion}\label{sec:results_and_discussion}
%
\input{chapters/results}

% ===========================================
\section{Conclusion}\label{sec:conclusion}
%
\input{chapters/conclusion}

% ===========================================
\appendix
\section{Electric Field Equations} \label{app:efield_equations}
Here we derive the electric field equations used in \cref{sec:methods}. The electric field following from $\vstat$ defined in \cref{eq:V_stat} is given by
\begin{align}
    \estatc{x} &= -\frac{\partial \vstat}{\partial x} = \frac{V_0}{d^2} x, \\
    \estatc{y} &= -\frac{\partial \vstat}{\partial y} = \frac{V_0}{d^2} y, \\
    \estatc{z} &= -\frac{\partial \vstat}{\partial z} = -\frac{2 V_0}{d^2} z,
\end{align}
for all $|\vec r| < d$ and zero otherwise. The electric field following from $\vdyn$ is simply
\begin{equation}
    \edyn = (1 + f \cos \omega_V t) \cdot \estat.
\end{equation}

\section{Special Case Analytical Solution} \label{app:special_case_analytical_solution}
In the special case of a single particle in the potential $\vstat$, with the inital conditions $\vec{r}(t_0) = (x_0, 0, z_0)$ and $\vec{v}(t_0) = (0, v_0, 0)$, we can derive an analytical solution for the trajectory. The solution for the $z$ component is straightforward
\begin{equation}
    z(t) = z_0 \cos(\omega_z t).
\end{equation}
From the equation system
\begin{align}
    f(0) = A_+ e^{-i \phi_+} + A_- e^{-i \phi_-} &\equiv x_0, \\
    \dot f(0) = -i \omega_+ A_+ e^{-i \phi_+} - i \omega_- A_- e^{-i \phi_-} &\equiv i v_0,
\end{align}
we derive $\phi_\pm = 0$ as $f(0) \in \mathbb{R}$ and therefore $A_+ + A_- = x_0$ and $\omega_+ A_+ + \omega_- A_- = -v_0$. Solving this system of equations for $A_\pm$ we find
\begin{equation}
    A_\pm =\pm \frac{v_0 + \omega_\mp x_0}{\omega_- - \omega_+}.
\end{equation}

\section{Further Results} \label{app:further_results}
\begin{figure}[H]
    \centering
    \includegraphics[width=\columnwidth]{../../src/project3/python/plots/xy_trajectory/two_particles_2_no_interactions_4000.000000_steps_velocity_verlet_positions_xy_trajectory.pdf}
    \caption{Trajectory of two particles in a Penning trap simulated using the Velocity-Verlet solver (4000 steps). The particles are initialized as 
    $\vec r_1 = (\qty{20}{\um}, \qty{0}{\um}, \qty{20}{\um})$, 
    $\vec v_1 = (\qty{0}{\um/\us}, \qty{25}{\um/\us}, \qty{0}{\um/\us})$, 
    $\vec r_2 = (\qty{25}{\um}, \qty{25}{\um}, \qty{0}{\um})$, 
    $\vec v_2 = (\qty{0}{\um/\us}, \qty{40}{\um/\us}, \qty{5}{\um/\us})$.}
    \label{fig:two_particle_trajectory_verlet}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\columnwidth]{../../src/project3/python/plots/xy_trajectory/two_particles_2_no_interactions_4000.000000_steps_euler_positions_xy_trajectory.pdf}
    \caption{Trajectory of two particles in a Penning trap simulated using the Euler solver (4000 steps). Same initial conditions as in Fig.~\ref{fig:two_particle_trajectory_verlet}.}
    \label{fig:two_particle_trajectory_euler}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\columnwidth]{../../src/project3/python/plots/xy_trajectory/two_particles_2_no_interactions_4000.000000_steps_boris_positions_xy_trajectory.pdf}
    \caption{Trajectory of two particles in a Penning trap simulated using the Boris solver (4000 steps). Same initial conditions as in Fig.~\ref{fig:two_particle_trajectory_verlet}.}
    \label{fig:two_particle_trajectory_boris}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\columnwidth]{../../src/project3/python/plots/Boris_many_particles_100_particles_enabled_interactions_total_energies.pdf}
    \caption{Total energy of 100 particles in a Penning trap simulated using the Boris solver with inter-particle interactions enabled for different time step sizes.}
    \label{fig:boris_many_particles_energy}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\columnwidth]{../../src/project3/python/plots/VelocityVerlet_many_particles_100_particles_enabled_interactions_total_energies.pdf}
    \caption{Total energy of 100 particles in a Penning trap simulated using the Velocity-Verlet solver with inter-particle interactions enabled for different time step sizes.}
    \label{fig:verlet_many_particles_energy}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\columnwidth]{../../src/project3/python/plots/Euler_many_particles_100_particles_enabled_interactions_total_energies.pdf}
    \caption{Total energy of 100 particles in a Penning trap simulated using the Euler solver with inter-particle interactions enabled for different time step sizes.}
    \label{fig:euler_many_particles_energy}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\columnwidth]{../../src/project3/python/plots/RK4_many_particles_100_particles_enabled_interactions_total_energies.pdf}
    \caption{Total energy of 100 particles in a Penning trap simulated using the RK4 solver with inter-particle interactions enabled for different time step sizes.}
    \label{fig:rk4_many_particles_energy}
\end{figure}

% ===========================================
\onecolumngrid
% \bibliographystyle{apalike}
\bibliographystyle{unsrt}
\bibliography{ref}


\end{document}