Files
katrin-plasma/docs/masters/creiling/LaTeX/sections/montecarlo.tex
T
2019-12-09 10:29:31 +01:00

394 lines
21 KiB
TeX

% !TEX root = ../thesis.tex
% !TEX useTabs
%% LaTeX2e class for student theses
%% sections/montecarlo.tex
%%
%% Karlsruhe Institute of Technology
%% Institute for Program Structures and Data Organization
%% Chair for Software Design and Quality (SDQ)
%%
%% Dr.-Ing. Erik Burger
%% burger@kit.edu
%%
%% Version 1.3.3, 2018-04-17
\chapter{Monte Carlo and computation techniques}
\label{ch:mon}
In Physics, Monte Carlo simulations are used to emulate complex physical problems on computers by dismantling them into their underlying basic processes.
These basic processes follow probability distributions, which can in general be gained from experiment or theory.
In Monte Carlo simulations these probability distributions are imitated using random numbers.
The creation of uniform pseudo random numbers on computers is covered in \autoref{sec:mon:rng}.
The sampling of arbitrary statistical distributions from these uniform random numbers is treated in \autoref{sec:mon:sample}.
In physics Monte Carlo simulations are often used to determine the properties of a complex system by repetitive simulation of single particles in this system.
A common approach is a semi-classical Monte Carlo simulation, where particle propagation and interactions between particles is separated.
Particle interactions are treated quantum mechanically and are therefore described by probability distributions.
The statistical description of particle interactions is covered in \autoref{sec:mon:interactions}.
Following an interaction the particle will be deflected by a certain scattering angle.
The calculation of the final direction of motion after an interaction is treated in \autoref{sec:mon:dir}.
Propagation of particles is treated classically in semi classical Monte Carlo simulations and is therefore definite.
Free propagation without external forces is trivial and determined by conservation of momentum.
The trajectory of charged particles in electric and magnetic fields is complex and usually not analytically solvable.
Two algorithms for numerical trajectory calculation of charged particles in electric and magnetic field are presented in \autoref{sec:mon:boris} and \autoref{sec:mon:drift}.
Analogous to real physical experiments, Monte Carlo simulations need high statistics for good results.
This requires high wall times.
A good way to reduce the computation time is parallel computation.
In section \ref{sec:mon:mpi} the \ac{mpi} for parallel computing is introduced.
\section{Random number generation}
\label{sec:mon:rng}
Random numbers are essential for the quality of a Monte Carlo simulation.
Real random numbers can only be obtained from real physical experiments.
Thus using these is possible but often not practicable.
Typically, Monte Carlo simulations use \acp{prng}, which create numbers using special algorithms.
The numbers are random in good approximation.
The randomness depends on the algorithm implemented and often scales with computation effort.
The first \ac{prng} was the congruential generator proposed in 1951 \cite{Lehmer:1951aa}.
New elements are calculated by
\begin{equation}
\label{eq:mon:con}
x_{k+1} = (a \cdot x_k + b)\bmod M \,
\end{equation}
where $x_0$, $a$, $b$ and $M$ are initial integer values for the \ac{prng} and $b<M$.
Hence values for $x$ are created on $[0, (M-1)]$.
Commonly uniform random numbers $\eta$ on $[0, 1)$ are required.
These can be obtained by
\begin{equation}
\eta = \frac{x}{M} \, .
\end{equation}
Congruential generators are very memory and computation efficient.
However, they are outdated as the maximum length of sequence is strongly limited by the maximum integer number on the system, since the sequence repeats itself, if $x_0$ is generated again (\autoref{eq:mon:con}).
The proper choice of $a$, $b$ and $M$ determines important properties of the \ac{prng} such as randomness and length of sequence. For more information about requirements of \ac{prng} see \cite{Haghighat:2015aa}.
The seed $x_0$ of the \ac{prng} initializes the sequence explicitly and makes the simulation reproducible by using the same seed.
The Mersenne-Twister 19937 is a modern \ac{prng} which is commonly used.
The algorithm satisfies for randomness, has a long sequence of $2^{19937}-1$ and the computational effort is very efficient compared to other \ac{prng} algorithms.
For details see
\\ \cite{Haghighat:2015aa, Kolonko:2008aa}
\section{Sampling of Random variables}
\label{sec:mon:sample}
In Monte Carlo simulations \ac{prng} grant access to uniform random numbers $\eta$ on $[0,1]$ (\autoref{sec:mon:rng}).
Commonly physics follow arbitrary statical distributions.
Using sampling techniques a random quantity $x$ that follows an arbitrary \ac{pdf} $p(x)$ can be created from uniform random numbers $\eta_i$.
\ac{pdf} describes the statistical properties of a quantity $x$.
The relative probability of observing the quantity within $[a,b]$ is
\begin{equation}
w([a,b]) = \int_a^b p(x) \upd x \, ,
\end{equation}
where $p(x)$ is the \ac{pdf} of $x$.
The \ac{cdf} of $p(x)$ is defined by
\begin{equation}
P(x) \equiv \int_{-\infty}^x p(x') \upd x'
\end{equation}
and indicates the probability of obtaining a value smaller or equal to $x$.
\subsection*{Inverse transform sampling theorem}
\begin{figure}[tb]
\centering
\input{pics/monte/inversion.pgf}
\caption{Sketch of the numerical inversion technique. The black solid line represents the \ac{pdf} and the grey bars represent the bins of equal sizes.}
\label{fig:mon:inv}
\end{figure}
Following \cite{Kolonko:2008aa} the inverse transform sampling theorem states that a random quantity $x$ that follows the \ac{pdf} $p(x)$ can be sampled from a uniform random number $\eta$ by
\begin{equation}
\label{eq:mon:inversion}
x = P^{-1}(\eta) \, .
\end{equation}
If the \ac{cdf} $P(x)$ is analytically invertible this approach is the best choice and unmatched in terms of precision and computational effort.
Else $P^{-1}(x)$ can be obtained from numerical inversion of $P(x)$.
The \ac{pdf} is split into $N$ bins of equal size $A = \frac{1}{N}$ (\autoref{fig:mon:inv}).
As each bin is equally probable a bin $i_x$ can be sampled by
\begin{equation}
i_x = \mathrm{int}(N \cdot \eta_1) + 1 \, .
\end{equation}
From the bin $i_x$ the value of $x$ can be sampled.
Assuming each value within a bin being equal probable the final value for $x$ is sampled from
\begin{equation}
x = x_{i_x, \text{low}} + \eta_2 (x_{i_x, \text{up}} - x_{i_x, \text{low}}) \, ,
\end{equation}
where $x_{i_x, \text{low}}$, $x_{i_x, \text{up}}$ are lower and upper bin border of bin $i_x$.
The assumption of values within each bin being constant will lead to divergences from the original $p(x)$.
This error can be reduced by increasing the number of bins $N$ but will also lead to higher memory requirement and computational effort.
\subsection*{Rejection method}
\begin{figure}[tb]
\centering
\input{pics/monte/rejection.pgf}
\caption{Sketch of rejection method. The black solid line represents the \ac{pdf}. Values are sampled within $a<x<b$ and $0<y<p_{max}$ represented by dotted surface. Values sampled in grey area are accepted and values in white area are recjected.}
\label{fig:mon:rejection}
\end{figure}
The Rejection method samples $x$ directly from the surface of the \ac{pdf}.
Therefore the \ac{pdf} is enclosed by a rectangular frame with borders $[a,b]$ and $[0,p_{max}]$ (\autoref{fig:mon:rejection}).
A random test point $(x_\mathrm{t}, y_\mathrm{t})$ in the frame is created by
\begin{align}
x_\mathrm{t} &= a + \eta_1 \cdot (b-a) \, , \\
y_\mathrm{t} &= \eta_2 \cdot p_\mathrm{max} \, .
\end{align}
The point is accepted if it is within the \ac{pdf}
\begin{equation}
y_\mathrm{t} \leq p(x_\mathrm{t}) \, .
\end{equation}
Else the test point is rejected and a new test point is created. The algorithm is repeated until the condition is met and a point is accepted.
Thus a random variable following $p(x)$ is created.
The rejection method models the \ac{pdf} exactly, but can be very computation intensive.
The efficiency $e$ of the method is determined by the ratio of acceptance area to total sampling area:
\begin{equation}
e = \frac{\int_a^b p(x) \upd x}{p_\text{max} \cdot (b-a) } \, .
\end{equation}
\\ \cite{Haghighat:2015aa, Kolonko:2008aa}
\subsection*{Numerical evaluation}
\begin{figure}[tb]
\centering
\input{pics/monte/numeva.pgf}
\caption{\ac{cdf} of a discrete histogram with boarders $x_i$.}
\label{fig:mon:numeva}
\end{figure}
Numerical evaluation is the best method for sampling discrete \acp{pdf} (\autoref{fig:mon:numeva}).
Discrete \acp{pdf} represent either categoric data or measurement results in histogram form with equidistant $x$-resolution.
Let the $i$-th bin have the \ac{cdf}-value $P_i$.
A bin $i_x$ can be sampled by searching for
\begin{equation}
P_{i_x-1} < \eta < P_{i_x} \, .
\end{equation}
Thus for categorical data the corresponding outcome is sampled.
When sampling from histograms the value of $x$ can be returned as value in the bin mid or for more precise resolution linear interpolation can be applied.
The efficiency depends on the number of bins and the search algorithm.
\\ \cite{Haghighat:2015aa, Kolonko:2008aa}
\section{Particle Interactions}
\label{sec:mon:interactions}
In particle physics interactions are described as point like events.
The particles' states before and after the interaction are regarded as free waves.
The interaction itself is a complicated quantum physical process determined by any acting force.
With the concept of cross sections, this complicated interaction process can be simplified and expressed by the probability of observing a certain final state from a known initial state.
Interaction cross sections are obtained from experiment and theory.
\subsection*{Two particle scattering}
The \acf{tcs} is a measure for the probability of an interaction happening between two particles.
It is energy dependent and generally measured and modeled in the laboratory system, where the impact particle with energy $E$ hits a fixed target particle.
For $N_\mathrm{T}$ number of target particles the probability for an interaction is defined by
\begin{equation}
w = \sigma(E) \cdot \frac{N_\mathrm{T}}{A} \, ,
\end{equation}
where $\sigma(E)$ is the \ac{tcs} and $A$ is the surface of the target.
Thus the \ac{tcs} can be interpreted as the effective target surface of a single target particle.
For a constant density of target particles $n_\mathrm{T}$ the probability for an interaction after passing a distance $x$ results in
\begin{equation}
\label{eq:mon:prob}
w = \sigma(E) \cdot n_\mathrm{T} \cdot x\, .
\end{equation}
From \autoref{eq:mon:prob} the average distance between two interactions, the \acf{mfp}, calculates to
\begin{equation}
\lambda(E) = \frac{1}{\sigma(E) \cdot n_\mathrm{T}} \, .
\end{equation}
For more than one \ac{ic} the total \ac{mfp} results in
\begin{equation}
\label{eq:mon:lambda}
\lambda_\mathrm{tot}(E) = \frac{1}{\sum_i \Sigma_i(E)} \, ,
\end{equation}
where the $\Sigma_i(E) = \sigma_\mathrm{i}(E) \cdot n_\mathrm{T,i}$ are the \acp{mcs} of the \acp{ic} and $n_\mathrm{T,i}$ the corresponding target particle densities.
\subsection*{Differential cross sections}
Following an interaction the particles can result in various final states.
These states can be expressed by measurable physical quantities such as a scattering angle or transfered energy.
The statistical distribution for observing a specific outcome $x$ after an interaction can be expressed by the \acf{dcs} $\frac{\upd \sigma}{\upd x}$.
The probability for observing $x$ within a specific range $[a,b]$ is calculated by
\begin{equation}
w = \frac{1}{\sigma} \cdot \int_a^b \frac{\upd \sigma}{\upd x} \upd x \, ,
\end{equation}
where $\sigma$ is the \ac{tcs} for the specific \ac{ic} and is linked to the \ac{dcs} by
\begin{equation}
\sigma = \int_{x_\text{min}}^{x_\text{max}} \frac{\upd \sigma}{\upd x} \upd x \, .
\end{equation}
${x_\text{min}}$ and ${x_\text{max}}$ are lower and upper limit for the observable $x$.
\subsection*{Reaction rates}
For low energetic and rare interactions \acp{tcs} are difficult to measure.
Often only reaction rates are accessible.
These indicate the number of interactions per unit of time in dependence to number particles involved in the interaction.
For a two species system with densities $n_\mathrm{I}$ and $n_\mathrm{T}$ the number of interactions within a unit of volume per unit of time is defined by
\begin{equation}
\label{eq:mon:two_k}
\frac{\upd n_\mathrm{R}}{\upd t} \equiv k_2 \cdot n_\mathrm{I} \cdot n_\mathrm{T} \, ,
\end{equation}
where $k_2$ is the reaction rate for the two particle interaction.
For one particle of species $I$ with velocity $v = \frac{\upd x}{\upd t}$ the probability for an interaction after moving by $x$ calculates to
\begin{equation}
w = k_2 \cdot n_\mathrm{T} \frac{x}{v} \, ,
\end{equation}
With \autoref{eq:mon:prob} this yields to
\begin{equation}
\sigma = \frac{k_2}{v} \, .
\end{equation}
For ternary interactions, where one particle of species I interacts with two particles of species T the reaction rate $k_3$ is defined by
\begin{equation}
\frac{\upd n_\mathrm{R}}{\upd t} \equiv k_3 \cdot n_\mathrm{I} \cdot n_\mathrm{T} \cdot n_\mathrm{T} \, .
\end{equation}
Thus the \ac{tcs} and the \ac{mcs} results in
\begin{align}
\sigma = \frac{k_3}{v} n_\mathrm{T}\, , \\
\Sigma = \frac{k_3}{v} n_\mathrm{T}^2\, .
\end{align}
\section{Change of particle track by interactions}
\label{sec:mon:dir}
The scattering angle indicates the deflection of the original trajectory caused by an interaction.
It is commonly stated in spherical coordinates with the polar angle $\theta_{0}$ and the azimuthal angle $\varphi_{0}$ in respect to the initial direction of motion $\hat{n}$.
This section presents a derivation for the formula for calculating the direction of motion after interaction $\hat{n}^{\prime}$ following \cite{Haghighat:2015aa}.
A full derivation is shown because the source is faulty.
Let the initial direction of the particle be
\begin{equation}
\hat{n} = u\hat{x} + v \hat{y} + w \hat{z} \, ,
\end{equation}
where $\hat{x} $, $\hat{y}$ and $\hat{z}$ are unit vectors of the simulation code's coordinate system.
Transformation to a new coordinate system with unit vectors $\hat{x}^{\prime}$, $\hat{y}^{\prime}$, $\hat{z}^{\prime}$, which satisfy for
\begin{align}
\hat{z}^{\prime} &= \hat{n} \, , \\
\hat{y}^{\prime} &= \frac{\hat{n} \times \hat{z}} {\mid \hat{n} \times \hat{z} \mid} = \frac{v}{s} \hat{x} - \frac{u}{s} \hat{y} \, , \\
\hat{x}^{\prime} &= \hat{y}^{\prime} \times \hat{z}^{\prime} = \frac{-uw}{s} \hat{x} + \frac{-vw}{s} \hat{y} + s \hat{z} \, , \\
s &= \sqrt{u^{2} + v^{2}} = \sqrt{1-w^{2}}
\end{align}
yields
\begin{equation}
\hat{n}^{\prime} = \sin\theta_{0} \cos\varphi_{0} \hat{x}^{\prime} + \sin\theta_{0} \sin\varphi_{0} \hat{y}^{\prime} + \cos\theta_{0} \hat{z}^{\prime} \, .
\end{equation}
Using the nine identities $\hat{i} \cdot \hat{j}^{\prime}$ for $i,j = x,y,z$ and parameterizing $\hat{n}^{\prime}$ in the initial coordinate system as:
\begin{equation}
\hat{n}^{\prime} = u^{\prime}\hat{x} + v^{\prime} \hat{y} + w^{\prime} \hat{z}
\end{equation}
results in:
\begin{equation}
\label{eq:mon:dir}
\begin{split}
u^{\prime} = \hat{n}^{\prime} \cdot \hat{x} &= \Bigr(\frac{-uw}{s}\cos \varphi_{0} + \frac{-v}{s} \sin \varphi_{0} \Bigl)\sin\theta_{0} + u \cos\theta_{0} \\
v^{\prime} = \hat{n}^{\prime} \cdot \hat{y} &= \Bigr(\frac{-vw}{s}\cos \varphi_{0} + \frac{u}{s} \sin \varphi_{0} \Bigl)\sin\theta_{0} + v \cos\theta_{0} \\
w^{\prime} = \hat{n}^{\prime} \cdot \hat{z} &= s \cos \varphi_{0} \sin \theta_{0} + w \cos \theta_{0} \, ,
\end{split}
\end{equation}
which allows for easy computation of final direction of motion in the initial coordinate system.
\section{Boris Push}
\label{sec:mon:boris}
The Boris Push is the standard algorithm for trajectory calculation of charged particles with relativistic gamma factor $\gamma < 100$ in arbitrary electric and magnetic fields in full kinetic plasma simulations .
A particle with charge $q$ and mass $m$ in an electric field $\vec{E}$ and magnetic field $\vec{B}$ will be affected by Coulomb and Lorentz force resulting in a total force of
\begin{equation}
\vec{F} = q \Big(\vec{E}(\vec{x}) + \frac{\vec{v} \times \vec{B}(\vec{x})}{c} \Big) \, ,
\end{equation}
where $(\vec{x}, \vec{v})$ denote the particle's phase space coordinates and $c$ the speed of light.
The Boris Push algorithm solves the particle's equation of motion
\begin{equation}
\frac{\upd^2 \vec{x}}{\upd t^2} = \frac{\upd \vec{v}}{\upd t} = \frac{\vec{F}_L}{m}
\end{equation}
by approximating the time differential $\upd t$ using a finite time element $\Delta t$.
Hence the motion follows
\begin{align}
\frac{\vec{x}_{k+1} - \vec{x}_k}{\Delta t} &= \vec{v}_{k+1} \, ,
\label{eq:mon:xcond} \\
\frac{\vec{v}_{k+1} - \vec{v}_k}{\Delta t} & = \frac{q}{m} \Big(\vec{E}_k + \frac{(\vec{v}_{k+1} - \vec{v}_k) \times \vec{B}_k}{2c} \Big) \, .
\label{eq:mon:vcond}
\end{align}
Typically $\vec{x}_k$ and $\vec{v}_k$ are shifted by half a time step. Hence $\vec{x}_k$, $\vec{E}_k$, $\vec{B}_k$ correspond to $t = t_k \equiv k \cdot \Delta t$ and $\vec{v}_k$ corresponds to $t = t_k - \frac{\Delta t}{2}$.
Equation \ref{eq:mon:xcond} can be solved straight forward.
Equation \ref{eq:mon:vcond} is solved by
\begin{equation}
\vec{v}_{k+1} = \vec{v}^{+} + \frac{q}{m} \vec{E}_k \frac{\Delta t}{2} \, ,
\label{eq:mon:boost1}
\end{equation}
where
\begin{equation}
\vec{v}^{+} = \vec{v}^{-} + (\vec{v}^{-} + \vec{v}^{-} \times \vec{h} ) \times s)
\label{eq:mon:rot}
\end{equation}
and
\begin{align}
\vec{v}^{-} =& \vec{v}_k + \frac{q}{m} \vec{E}_k \frac{\Delta t}{2} \, ,
\label{eq:mon:boost2}\\
\vec{h} = & \frac{q}{m} \vec{B}_k \frac{\Delta t}{2} \, , \\
\vec{s} =& \frac{2 \vec{h} }{1+h^2} \, .
\end{align}
The change of velocity is calculated by two linear boosts caused by the electric field (\autoref{eq:mon:boost1} and \autoref{eq:mon:boost2}) and a rotation between the two boost caused by $\vec{B}$ (\autoref{eq:mon:rot}).
The Boris Push provides a very exact solution for arbitrary electric and magnetic fields if $\Delta t$ is chosen small enough.
The required $\Delta t$ depends on $\vec{E}(\vec{x})$ and $\vec{B}(\vec{x})$ and determines the computational effort needed for the algorithm.
Especially resolving gyro motions requires time steps of $\Delta t \approx \frac{1}{20}\frac{1}{\omega_g}$, where $\omega_g$ is the gyro frequency of the particle \cite{Lange:2012aa}.
This can limit efficiency strongly in high magnetic fields.
\\ \cite{Boris:1970aa, Qin:2013aa, Zenitani:2018aa}
\section{Drift Approximation}
\label{sec:mon:drift}
A method to overcome the high computational effort needed for the Boris Push algorithm (\autoref{sec:mon:boris}) is the Drift Approximation.
It can applied for propagation of charged particles in spatial and time constant magnetic fields.
In absence of electric fields the particle is solely affected by the Lorentz force.
The motion can be described as superposition of a linear motion parallel to the magnetic field $\vec{B}$ with velocity $v_\parallel$ and a gyration around the magnetic field with velocity $v_\perp$.
The gyro frequency calculates to
\begin{equation}
\omega_g = |q| \frac{|B|}{m}
\end{equation}
and the motion of a particle with charge $q$ follows
\begin{align}
x(t) = & x_c + \frac{v_\perp}{\omega_g} \cos(\omega_g t) \, , \\
y(t) = & y_c + \mathrm{sign}(q) \cdot \frac{v_\perp}{\omega_g} \sin(\omega_g t) \, , \\
z(t) = & z_0 + v_\parallel \cdot t \, ,
\end{align}
where the magnetic field is aligned along the $z$-axis. $z_0$ is the initial particle's $z$-position at $t=0$ and $(x_c, y_c)$ are the center of the gyro motion.
An electric field $E_\parallel$ parallel to the magnetic field leads to a boost of $v_\parallel$ following
\begin{equation}
v_\parallel (t) = v_\parallel (0) + \frac{q}{m} E_\parallel t \, .
\end{equation}
A perpendicular electric field $\vec{E}_\perp$ leads to a constant drift of the center of the gyro motion with
\begin{equation}
\vec{v_E} = \frac{\vec{B} \times \vec{E_\perp}}{B^2} \, .
\end{equation}
\\ \cite{Northrop:1961aa, Blank:2015aa}
\section{Parallel computing strategies and the MPI standard}
\label{sec:mon:mpi}
Parallel computing is inevitable for Monte Carlo simulations since good statistics require a high number of iterations.
For Monte Carlo simulations, where the physical environment in the simulation is constant in time, no communication between individual CPU cores is needed.
Each core initializes and performs the simulation individually.
In the post processing the total results is composed from the individual outputs.
Note that different initial \ac{prng} seeds for each core are mandatory. Otherwise the result from each core is identical (\autoref{sec:mon:rng}).
If the physical environment in the simulation is calculated within the simulation, communication between cores is inevitable.
The \acf{mpi} allows communication across cores in a distributed memory system.
All participating cores are initialized in the \ac{mpi}-world and mapped to individual addresses.
Communication between cores is performed using \ac{mpi}-commands.
The command must be met on all participating cores.
Hence all participating cores will only continue calculating when all cores have called the corresponding \ac{mpi}-command.
Thus bad implementation of the \ac{mpi}-commands can drastically reduce simulation performance, as cores might wait for others to call the \ac{mpi}-command.
\\ \cite{Nielsen:2016aa}