% !TeX spellcheck = de_DE
%\documentclass[12pt,a4paper]{article}
%\usepackage{import}
%\subimport{../}{uebung.tex}
%
%\toggletrue{showSolution}
%
%
%\title{\thetitle{}\\Übung 5 - BoSSS}
%\date{\WeeksAfter{6}}
%
%
%\begin{document}
%
%\maketitle

\BoSSSopen{tutorial5/uebung5tutorial}
\graphicspath{{tutorial5/uebung5tutorial.texbatch/}}

\BoSSScmd{
///\section*{What's new}
///\begin{itemize}
/// \item implementing the Lax-Friedrichs flux
/// \item computation of "experimental order of convergence" (EOC)
///\end{itemize}
///
///\section*{Prerequisites}
///\begin{itemize}
/// \item definition of fluxes, chapter \ref{SpatialOperator}
/// \item grid generation, chapter \ref{GridInstantiation}
/// \item basics of convergence studies, chapter \ref{GridInstantiation}
///\end{itemize}
///Within this tutorial the Lax-Friedrichs flux will be implemented as an alternative to the Upwind flux (see chapter \ref{SpatialOperator}).
/// For both fluxes a convergence study will be performed and the "experimental order of convergence" (EOC) will be computed.
///For the implementation of the Lax-Friedrichs flux and the Upwind flux it is recommended to work through chapter \ref{SpatialOperator} first
/// as the definition of fluxes is explained there in more detail. For the second part of the tutorial,
/// chapter \ref{GridInstantiation} has already taught the basics of doing a convergence study.
///
///\section{Problem statement}
///\label{sec:NumFlux_problem}
///As an examplary problem, we consider the scalar transport equation
///\begin{equation}
///\frac{\partial c}{\partial t} + \nabla \cdot (\vec{u} c) = 0
///\end{equation}
///in the domain $\domain = [-1, 1] \times [-1, 1]$, where the concentration $c = c(x,y,t) \in \mathbb{R}$ is unknown and the velocity field is given by
///\begin{equation*}
///\vec{u} = \begin{pmatrix}
///y\\-x
///\end{pmatrix}
///\end{equation*}
///The analytical solution for the problem above is given by
///\begin{align*}
///c_\text{Exact}(x,y,t) &= \cos(\cos(t) x - \sin(t) y) \quad \text{ for } (x,y) \in \domain,
///\end{align*}
///which can be used to describe the initial and boundary conditions.
///
///\section{Solution within the BoSSS Framework}
///\label{sec:NumFlux_tutorial}
 }
\BoSSSexeSilent
\BoSSScmd{
restart;
 }
\BoSSSexeSilent
\BoSSScmd{
/// First, we define functions for the given velocity field and the exact solution
 }
\BoSSSexe
\BoSSScmd{
Func<double[], double> u = X => X[1];
 }
\BoSSSexe
\BoSSScmd{
Func<double[], double> v = X => -X[0];
 }
\BoSSSexe
\BoSSScmd{
Func<double[], double, double> cExact =      \newline
\btab  (X, t) => Math.Cos(Math.Cos(t)*X[0] - Math.Sin(t)*X[1]);
 }
\BoSSSexe
\BoSSScmd{
/// %=============================================
/// \subsection{Definition of the numerical fluxes} \label{sec:NumFlux_fluxes}
/// \subsection{Definition of the numerical fluxes} \label{sec:flux_and_convergence_fluxes}
/// %=============================================
 }
\BoSSSexe
\BoSSScmd{
using BoSSS.Platform.LinAlg;
 }
\BoSSSexe
\BoSSScmd{
/// Before implementing the \code{ScalarTransportFlux} as done in Tutorial 4, we define both fluxes
/// as functions in terms of the parameters \code{Uin}, \code{Uout}, the normal vector \code{n} and the
/// \code{velocityVector}. Recalling the Upwind flux, the corresponding flux function is defined as
 }
\BoSSSexe
\BoSSScmd{
Func<double, double, Vector, Vector, double> upwindFlux =     \newline
\btab delegate(double Uin, double Uout, Vector n, Vector velocityVector) \{     \newline
 \newline
\btab \btab if (velocityVector * n > 0) \{     \newline
\btab \btab \btab return (velocityVector * Uin) * n;     \newline
\btab \btab \} else \{     \newline
\btab \btab    return (velocityVector * Uout) * n;     \newline
\btab \btab \}     \newline
\btab \};
 }
\BoSSSexe
\BoSSScmd{
/// The second flux we are considering now, is the Lax-Friedrichs flux
/// \begin{equation*}
/// \hat{f}(c_h^-, c_h^+, \vec{u} \cdot \vec{n}) = \mean{\vec{u} \, c_h} \cdot \vec{n} + \frac{C}{2} \jump{c_h}.
/// \end{equation*}
/// The constant $C \in \mathbb{R}^+$ has to be sufficiently large in order to guarantee the stability of the
/// numerical scheme. We choose
/// \begin{equation*}
/// C = \max \abs{\vec{u} \cdot \vec{n}} = 1
/// \end{equation*}
/// for the given problem.
 }
\BoSSSexe
\BoSSScmd{
double C = 0.3;     \newline
 \newline
Func<double, double, Vector, Vector, double> laxFriedrichsFlux =     \newline
\btab delegate(double Uin, double Uout, Vector n, Vector velocityVector) \{     \newline
 \newline
\btab \btab return 0.5 * (Uin + Uout) * velocityVector * n - C * (Uout - Uin);     \newline
\btab \};
 }
\BoSSSexe
\BoSSScmd{
/// For the implementation of the \code{ScalarTransportFlux} we want to generate
/// instances by the definition of the \code{numericalFlux}. Therefore a new constructor is implemented
/// and the implementation of \code{InnerEdgeFlux} is rewritten in terms of the \code{numericalFlux}.
class ScalarTransportFlux : NonlinearFlux \{     \newline
 \newline
\btab private Func<double, double, Vector, Vector, double> numericalFlux;     \newline
 \newline
\btab // Provides instances of this class with a specific flux implementation     \newline
\btab public ScalarTransportFlux(     \newline
\btab \btab Func<double, double, Vector, Vector, double> numericalFlux) \{     \newline
 \newline
\btab \btab this.numericalFlux = numericalFlux;     \newline
\btab \}     \newline
 \newline
\btab public override IList<string> ArgumentOrdering \{     \newline
\btab \btab get \{ return new string[] \{ "ch" \}; \}     \newline
\btab \}     \newline
 \newline
\btab protected override void Flux(     \newline
\btab \btab double time, double[] x, double[] U, double[] output) \{     \newline
 \newline
\btab \btab output[0] = u(x) * U[0];     \newline
\btab \btab output[1] = v(x) * U[0];     \newline
\btab \}     \newline
 \newline
\btab // Makes use of the flux implementation supplied in the constructor     \newline
\btab protected override double InnerEdgeFlux(     \newline
\btab \btab double time, double[] x, double[] normal,      \newline
\btab \btab double[] Uin, double[] Uout, int jEdge) \{     \newline
 \newline
\btab \btab Vector n              = new Vector(normal);     \newline
\btab \btab Vector velocityVector = new Vector(u(x), v(x));     \newline
 \newline
\btab \btab return numericalFlux(Uin[0], Uout[0], n, velocityVector);     \newline
\btab \}     \newline
 \newline
\btab protected override double BorderEdgeFlux(     \newline
\btab \btab double time, double[] x, double[] normal, byte EdgeTag,     \newline
\btab \btab double[] Uin, int jEdge) \{     \newline
 \newline
\btab \btab double[] Uout = new double[] \{ cExact(x, time) \};     \newline
 \newline
\btab \btab return InnerEdgeFlux(time, x, normal, Uin, Uout, jEdge);     \newline
\btab \}     \newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// For both numerical fluxes we define a \code{SpatialOperator} that uses the corresponding flux to compute div(...)
 }
\BoSSSexe
\BoSSScmd{
var upwindOperator = new SpatialOperator(     \newline
\btab new string[] \{ "ch" \},     \newline
\btab new string[] \{ "div" \},     \newline
\btab QuadOrderFunc.NonLinear(2));
 }
\BoSSSexe
\BoSSScmd{
upwindOperator.EquationComponents["div"].Add(     \newline
\btab new ScalarTransportFlux(upwindFlux));
 }
\BoSSSexe
\BoSSScmd{
upwindOperator.Commit();
 }
\BoSSSexe
\BoSSScmd{
/// %
 }
\BoSSSexe
\BoSSScmd{
var laxFriedrichsOperator = new SpatialOperator(     \newline
\btab new string[] \{ "ch" \},     \newline
\btab new string[] \{ "div" \},     \newline
\btab QuadOrderFunc.NonLinear(2));
 }
\BoSSSexe
\BoSSScmd{
laxFriedrichsOperator.EquationComponents["div"].Add(     \newline
\btab new ScalarTransportFlux(laxFriedrichsFlux));
 }
\BoSSSexe
\BoSSScmd{
laxFriedrichsOperator.Commit();
 }
\BoSSSexe
\BoSSScmd{
/// %
 }
\BoSSSexe
\BoSSScmd{
/// %=============================================
/// \subsection{Convergence study}\label{sec:flux_and_convergence_convergence}
/// %=============================================
 }
\BoSSSexe
\BoSSScmd{
/// In the following a convergence study for the discretization error will be performed on grids
/// with $2\times2$, $4\times4$, $8\times8$ and $16\times16$ cells. The error at $t = \pi /4$
/// will be investigated for the polynomial degrees $\degree = 0, \ldots, 3$ of the ansatzfunctions.
/// The study will be done for both numerical fluxes.
 }
\BoSSSexe
\BoSSScmd{
/// We start by defining a series of nested grids for the convergence study
 }
\BoSSSexe
\BoSSScmd{
int[] numbersOfCells = new int[] \{ 2, 4, 8, 16 \};
 }
\BoSSSexe
\BoSSScmd{
GridData[] grids = new GridData[numbersOfCells.Length];
 }
\BoSSSexe
\BoSSScmd{
for (int i = 0; i < numbersOfCells.Length; i++) \{     \newline
 \newline
\btab double[] nodes = GenericBlas.Linspace(-1.0, 1.0, numbersOfCells[i] + 1);     \newline
 \newline
\btab GridCommons grid = Grid2D.Cartesian2DGrid(nodes, nodes);     \newline
 \newline
\btab grids[i] = new GridData(grid);     \newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// Then, a \code{SinglePhaseField} is defined for each polynomial degree on each grid.
/// The initial value is projected on each field.
 }
\BoSSSexe
\BoSSScmd{
int[] dgDegrees = new int[] \{ 0, 1, 2, 3 \};
 }
\BoSSSexe
\BoSSScmd{
SinglePhaseField[,] fields =      \newline
\btab new SinglePhaseField[dgDegrees.Length, numbersOfCells.Length];
 }
\BoSSSexe
\BoSSScmd{
for (int i = 0; i < dgDegrees.Length; i++) \{     \newline
\btab for (int j = 0; j < numbersOfCells.Length; j++) \{     \newline
 \newline
\btab \btab Basis basis = new Basis(grids[j], dgDegrees[i]);     \newline
\btab \btab fields[i, j] = new SinglePhaseField(     \newline
\btab \btab \btab \btab \btab \btab \btab   basis, "ch\_" + dgDegrees[i] + "\_" + grids[j]);     \newline
\btab \btab fields[i, j].ProjectField(X => cExact(X, 0.0));     \newline
\btab \}     \newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// Since the error at a fourth revolution is considered, the initial concentration has to be simulated until
/// $\code{endTime} = \pi / 4$. For the timestepping we are using the classical fourth order Runge-Kutta scheme.
/// The timestep size should be chosen small enough in order to reduce the \emph{temporal} error,
/// so that the \emph{spatial} error dominates for the convergence study.
 }
\BoSSSexe
\BoSSScmd{
using BoSSS.Solution.Timestepping;
 }
\BoSSSexe
\BoSSScmd{
double endTime        = Math.PI / 4.0;     \newline
int numberOfTimesteps = 100;     \newline
double dt             = endTime / numberOfTimesteps;
 }
\BoSSSexe
\BoSSScmd{
/// A \code{MultidimensionalArray} is a special double array that enables \emph{shallow} resizing and data
/// extraction. For example, sub-arrays can be extracted without needing to copy the entries of
/// the \code{MultidimensionalArray}. BoSSS makes extensive use of this class because this is crucial
/// for the performance.
/// Here, we create a three-dimensional array, where the first index corresponds to the flux
/// function, the second to the DG degree and the third to the number of cells.
MultidimensionalArray errors =      \newline
\btab MultidimensionalArray.Create(2, dgDegrees.Length, grids.Length);
 }
\BoSSSexe
\BoSSScmd{
for (int i = 0; i < dgDegrees.Length; i++) \{     \newline
\btab for (int j = 0; j < numbersOfCells.Length; j++) \{     \newline
 \newline
\btab \btab // Clones an object and casts it to the type of the original object  \newline
\btab \btab // at the same time.  \newline
\btab \btab SinglePhaseField c = fields[i, j].CloneAs();     \newline
 \newline
\btab \btab // Instantiate the timestepper (classical Runge-Kutta scheme)     \newline
\btab \btab // for the evolution of the concentration c with Upwind flux     \newline
\btab \btab RungeKutta timeStepper = new RungeKutta(     \newline
\btab \btab \btab RungeKutta.RungeKuttaSchemes.RungeKutta1901,     \newline
\btab \btab \btab upwindOperator, c);     \newline
 \newline
\btab \btab // Integrate in time for each timestep     \newline
\btab \btab for (int k = 0; k < numberOfTimesteps; k++) \{     \newline
\btab \btab \btab timeStepper.Perform(dt);     \newline
\btab \btab \}     \newline
 \newline
\btab \btab // Compute the error w.r.t. analytical solution     \newline
\btab \btab errors[0, i, j] = c.L2Error(X => cExact(X, endTime));     \newline
 \newline
\btab \btab // Simulate with the Lax-Friedrichs flux     \newline
\btab \btab c = fields[i, j].CloneAs();     \newline
\btab \btab timeStepper = new RungeKutta(     \newline
\btab \btab \btab \btab \btab \btab \btab  RungeKutta.RungeKuttaSchemes.RungeKutta1901,      \newline
\btab \btab \btab \btab \btab \btab \btab  laxFriedrichsOperator, c);     \newline
\btab \btab for (int k = 0; k < numberOfTimesteps; k++) \{     \newline
\btab \btab \btab timeStepper.Perform(dt);     \newline
\btab \btab \}     \newline
\btab \btab errors[1, i, j] = c.L2Error(X => cExact(X, endTime));     \newline
\btab \}     \newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// %=============================================
/// \subsection{Plotting results}\label{sec:NumFlux_plots}
/// \subsection{Plotting results}\label{sec:flux_and_convergence_plots}
/// %=============================================
 }
\BoSSSexe
\BoSSScmd{
 %
 }
\BoSSSexe
\BoSSScmd{
/// For the convergence plots the error is plotted over the mesh size,
/// where the coarsest grid size is defined as size = 1.
 }
\BoSSSexe
\BoSSScmd{
var sizes = numbersOfCells.Select(s => 2.0 / s).ToArray();
 }
\BoSSSexe
\BoSSScmd{
/// First, we are looking at the convergence plot of the Upwind flux.
/// Therefore, an instance of Gnuplot has to be generated.
var gpUpwind = new Gnuplot();     \newline
gpUpwind.SetYRange(Math.Pow(10,-7), Math.Pow(10,0));
 }
\BoSSSexe
\BoSSScmd{
for (int i = 0; i < dgDegrees.Length; i++) \{     \newline
 \newline
    /// Here, we use the shallow extraction features of
    /// \code{MultidimensionalArray} mentioned above.
    /// The command \code{ExtractSubArrayShallow(0, i, -1)}
    /// is roughly equivalent to writing "errors[0, i, :]" in Matlab
\btab var errorsForDegree = errors.ExtractSubArrayShallow(0, i, -1).To1DArray();     \newline
 \newline
\btab // some formatting of the plot data for the convergence study      \newline
\btab PlotFormat format = new PlotFormat(lineColor: (LineColors)(i+1),     \newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab    Style: Styles.LinesPoints,       \newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab    pointType: PointTypes.Diamond,       \newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab    pointSize: 2.0);     \newline
 \newline
\btab // Create log-log plot mesh size vs. error for dgDegrees[i]    \newline
\btab gpUpwind.PlotLogXLogY(sizes, errorsForDegree,      \newline
\btab \btab \btab \btab \btab \btab   "Upwind, order " + dgDegrees[i], format);     \newline
\}
 }
\BoSSSexe
\BoSSScmd{
gpUpwind.PlotNow(); // perform the plotting
 }
\BoSSSexe
\BoSSScmd{
/// The convergence plot is also done for the Lax-Friedrichs flux.
 }
\BoSSSexe
\BoSSScmd{
var gpLaxFriedrichs = new Gnuplot();     \newline
gpLaxFriedrichs.SetYRange(Math.Pow(10,-7), Math.Pow(10,0));
 }
\BoSSSexe
\BoSSScmd{
for (int i = 0; i < dgDegrees.Length; i++) \{     \newline
 \newline
\btab var errorsForDegree = errors.ExtractSubArrayShallow(1, i, -1).To1DArray();     \newline
 \newline
\btab PlotFormat format = new PlotFormat(lineColor: (LineColors)(i+1),     \newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab    Style: Styles.LinesPoints,       \newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab    pointType: PointTypes.Diamond,       \newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab    pointSize: 2.0);     \newline
 \newline
\btab gpLaxFriedrichs.PlotLogXLogY(sizes, errorsForDegree,      \newline
\btab \btab \btab \btab \btab \btab \btab \btab  "LaxFriedrichs, order " + dgDegrees[i],      \newline
\btab \btab \btab \btab \btab \btab \btab \btab  format);     \newline
\}
 }
\BoSSSexe
\BoSSScmd{
gpLaxFriedrichs.PlotNow();
 }
\BoSSSexe
\BoSSScmd{
/// %
 }
\BoSSSexe
\BoSSScmd{
/// %==========================================
/// \subsection{Linear regression by ordinary least squares}\label{sec:NumFlux_slopes}
/// \subsection{Linear regression by ordinary least squares}\label{sec:flux_and_convergence_slopes}
/// %===========================================
 }
\BoSSSexe
\BoSSScmd{
/// For the determination of the experimental order of convergence (EOC) the linear regression is used
/// to compute the slope of the line in the log-log plot. In the following, the ordinary least squares method
/// is implemented to estimate the regression coefficient of the slope for given sets of x- and y-values.
 }
\BoSSSexe
\BoSSScmd{
Func<double[], double[], double> slope =      \newline
\btab delegate(double[] xValues, double[] yValues) \{     \newline
 \newline
\btab if (xValues.Length != yValues.Length) \{     \newline
\btab \btab throw new ArgumentException();     \newline
\btab \}     \newline
 \newline
\btab xValues = xValues.Select(s => Math.Log10(s)).ToArray();     \newline
\btab yValues = yValues.Select(s => Math.Log10(s)).ToArray();     \newline
 \newline
\btab double xAverage = xValues.Sum() / xValues.Length;     \newline
\btab double yAverage = yValues.Sum() / yValues.Length;     \newline
 \newline
\btab double v1 = 0.0;     \newline
\btab double v2 = 0.0;     \newline
 \newline
\btab // Computation of the regression coefficient for the slope     \newline
\btab for (int i = 0; i < yValues.Length; i++) \{     \newline
\btab \btab v1 += (xValues[i] - xAverage) * (yValues[i] - yAverage);     \newline
\btab \btab v2 += Math.Pow(xValues[i] - xAverage, 2);     \newline
\btab \}     \newline
\btab return v1 / v2;     \newline
\};
 }
\BoSSSexe
\BoSSScmd{
// Verification that the slope is computed correctly     \newline
Math.Abs(slope(sizes, new double[] \{ 64.0, 16.0, 4.0, 1.0 \}) - 2.0) < 1e-14;
 }
\BoSSSexe
\BoSSScmd{
/// The experimental orders of convergence (EOC) are computed for both fluxes
/// for each polynomial degree of the ansatzfunctions
for (int i = 0; i < dgDegrees.Length; i++) \{     \newline
 \newline
\btab Console.WriteLine();     \newline
 \newline
\btab var errorsForDegree = errors.ExtractSubArrayShallow(0, i, -1).To1DArray();     \newline
 \newline
\btab Console.WriteLine(     \newline
\btab \btab "Upwind flux, order \{0\}:\textbackslash t\textbackslash t \{1:F2\}", i,      \newline
\btab \btab slope(sizes, errorsForDegree));     \newline
 \newline
\btab errorsForDegree = errors.ExtractSubArrayShallow(1, i, -1).To1DArray();     \newline
 \newline
\btab Console.WriteLine(     \newline
\btab \btab "Lax-Friedrichs flux, order \{0\}:\textbackslash t \{1:F2\}", i,     \newline
\btab \btab slope(sizes, errorsForDegree));     \newline
\}
 }
\BoSSSexe


\documentclass[../../../../main]{subfiles}
\graphicspath{{images/sv-basics/}}

\begin{document}

\paragraph{Stereovision basics}

Stereo vision is a field of computer vision and its purpose is to provide information about 3D structures on digital images. This is achieved by comparing some feature points of scene taken from two views at the same time. In traditional way it is done by a stereo camera, which is, basically, two cameras placed horizontally within specific distance, as human eyes. This technique allows to get relative distance from a camera to the object on the scene. This information is also known as relative depth information which is represented as disparity map \cite{Hartley2004}.

There are few steps that precede finding of depth information because images taken by camera could contain some distortions and etc. The first and the most important step is to calibrate cameras with their intrinsic parameters to make it function as a single system. This operation will provide extrinsic parameters of the camera, which is rotation and translation vectors of the camera. Calibration is achieved by showing calibration grid to the camera which looks like a chessboard. It is used because size of squares should be known and the grid pattern is simple for detection. Calibration grid is depicted in the figure \ref{fig:calibrationGrid}.

\begin{figure} [ht]
    \begin{center}
        \includegraphics[width=180pt]{calibration_grid}
        \caption{Calibration grid.}
        \label{fig:calibrationGrid}
    \end{center}
\end{figure}

\newpage

The results of calibration process are used to rectify images so that both images are changed in order to be projections to the same real world plane. Rectified images significantly reduce computations that are needed to get the 3D information from the scene. This refers to epipolar geometry (stereo vision) geometry. It describes relations between projections and 3D points that comes from the assumption that camera corresponds to the pinhole camera model. This model describes already mentioned relations from the ideal pinhole camera perspective. Pinhole is a light proof box with a tiny hole from one side thus pinhole camera does not have any lens, as a result, it produces inverted image. Figure \ref{fig:pinholeCamera} illustrates pinhole camera logic. From the nature of the camera, model does not take into consideration any kind of distortions caused by lens and etc. To correct the lens distortion pixel to real 3D coordinates transformation can be used.

\begin{figure} [ht]
    \begin{center}
        \includegraphics[width=200pt]{pinhole_camera}
        \caption{Pinhole camera model.}
        \label{fig:pinholeCamera}
    \end{center}
\end{figure}

Epipolar constraints between two cameras are described by essential and fundamental matrices. Essential matrix can be thought as a pre-step before fundamental matrix. One limitation of the essential matrix is that it can be used only with calibrated cameras however if the cameras are calibrated it can be used to get relative orientation in the cameras space and to find 3D position of pairs of points. It is possible to extract rotation and translation vectors with this method using \ac{SVD}. On the other hand, fundamental matrix deals with the uncalibrated cameras. In case of having fundamental matrix and camera calibration information it is possible to get essential matrix for further processing. Figure \ref{fig:essential_matrix} shows essential matrix equation described in the "Multiple View Geometry in Computer Vision" book \parencite[see][p257]{Hartley2004} where $K$ is the intrinsic camera parameters.

\begin{figure} [ht]
  \centering
      \begin{equation}
         E = K'^TFK
      \end{equation}
  \label{fig:essential_matrix}
  \caption{Equation of Essential matrix}
\end{figure}

In a simple stereo-vision system depth of a point can be easily found with an equation \ref{depthFormula} where $f$ is a focal length of the camera, $b$ is a distance between cameras, $d$ is a distance between corresponding points, also known as disparity.

\begin{figure} [ht]
  \centering
      \begin{equation}
         Depth = f * b/d
         \label{depthFormula}
      \end{equation}
  \caption{Formula of the point depth.}
\end{figure}

To determine the 3D point coordinates by two or more its 2D projection so called triangulation is used. It is much easier to do if the images are rectified but even if they are not they can be transformed to match the requirements.

\begin{figure} [ht!]
  \centering
      \begin{equation}
         Z = f\frac{B}{D}, X = u\frac{Z}{f}, Y = v\frac{Z}{f}
         \label{realCoordinatesFormula}
      \end{equation}
  \caption{Equations of 3D point's coordinates.}
\end{figure}

If the baseline is known (line $CC'$ from the figure \ref{fig:triangulation}), 3D coordinates could be found with the equation \ref{realCoordinatesFormula} where $B$ is a baseline, $D$ is a disparity, $f$ is a focal length, $u$ and $v$ are column and row with the origin in center of the image. Figure \ref{fig:triangulation} represents triangulation process.

\begin{figure} [ht]
    \begin{center}
        \includegraphics[width=200pt]{triangulation}
        \caption{Triangulation illustration.}
        \label{fig:triangulation}
    \end{center}
\end{figure}

\end{document}


\chapter{Eigen value problems/solutions}
\begin{abox}
	Practice set 1 solutions
	\end{abox}
\begin{enumerate}
	\begin{minipage}{\textwidth}
	\item The energy of the first excited quantum state of a particle in the two-dimensional potential $V(x, y)=\frac{1}{2} m \omega^{2}\left(x^{2}+4 y^{2}\right)$ is
	\exyear{NET DEC 2011}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $2 \hbar \omega$
	\task[\textbf{B.}]$3 \hbar \omega$
	\task[\textbf{C.}]$\frac{3}{2} \hbar \omega$
	\task[\textbf{D.}] $\frac{5}{2} \hbar \omega$
\end{tasks}
\begin{answer}
	$V(x, y)=\frac{1}{2} m \omega^{2}\left(x^{2}+4 y^{2}\right)=\frac{1}{2} m \omega^{2} x^{2}+\frac{1}{2} m 4 \omega^{2} y^{2}, \\
	E=\left(n_{x}+\frac{1}{2}\right) \hbar \omega+\left(n_{y}+\frac{1}{2}\right) 2 \hbar \omega$\\
	For ground state energy $n_{x}=0, n_{y}=0 \Rightarrow E=\frac{\hbar \omega}{2}+\frac{1}{2} 2 \hbar \omega=\frac{3 \hbar \omega}{2}$\\
	First exited state energy $n_{x}=1, n_{y}=0 \Rightarrow \frac{3 \hbar \omega}{2}+\hbar \omega=\frac{5 \hbar \omega}{2}$\\
	The correct option is \textbf{(d)}
\end{answer}
\begin{minipage}{\textwidth}
	\item Let $|0\rangle$ and $|1\rangle$ denote the normalized eigenstates corresponding to the ground and first excited states of a one dimensional harmonic oscillator. The uncertainty $\Delta p$ in the state $\frac{1}{\sqrt{2}}(|0\rangle+|1\rangle)$, is
	\exyear{NET DEC 2011}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\Delta p=\sqrt{\hbar m \omega} / 2$
	\task[\textbf{B.}]$\Delta p=\sqrt{\hbar m \omega / 2}$
	\task[\textbf{C.}]$\Delta p=\sqrt{\hbar m \omega}$
	\task[\textbf{D.}]$\Delta p=\sqrt{2 \hbar m \omega}$
\end{tasks}
\begin{answer}
	$\begin{aligned}
	&\text { Solution: }|\psi\rangle=\frac{1}{\sqrt{2}}(|0\rangle+|1\rangle), p=i \sqrt{\frac{m \omega \hbar}{2}}\left(a^{\dagger}-a\right) \\
	&\begin{aligned}
	a^{\dagger}|\psi\rangle &=\frac{1}{\sqrt{2}}(\sqrt{1}|1\rangle+\sqrt{2}|2\rangle) \text { and } a|\psi\rangle=\frac{1}{\sqrt{2}}(0+\sqrt{1}|0\rangle) \\
	\langle p\rangle &=i \sqrt{\frac{m \omega \hbar}{2}}\left(\left\langle\psi\left|a^{\dagger}-a\right| \psi\right\rangle\right)=0, p^{2}=-\frac{m \omega \hbar}{2}\left(a^{\dagger^{2}}+a^{2}-(2 N+1)\right) \\
	\left\langle p^{2}\right\rangle &=\frac{-m \omega \hbar}{2}\left[\left\langle a^{+^{2}}\right\rangle+\left\langle a^{2}\right\rangle-\langle 2 N+1\rangle\right]=\frac{m \omega \hbar}{2}\langle 2 N+1\rangle=\frac{m \omega \hbar}{2}\left(2 \cdot \frac{1}{2}+1\right)=m \omega \hbar \\
	\Delta p &=\sqrt{\left\langle p^{2}\right\rangle-\langle p\rangle^{2}}=\sqrt{m \omega \hbar}
	\end{aligned}
	\end{aligned}$\\
	The correct option is \textbf{(c)}
\end{answer}
\begin{minipage}{\textwidth}
	\item A particle of mass $m$ is in a cubic box of size $a$. The potential inside the box $(0 \leq x<a, 0 \leq y<a, 0 \leq z<a)$ is zero and infinite outside. If the particle is in an eigenstate of energy $E=\frac{14 \pi^{2} \hbar^{2}}{2 m a^{2}}$, its wavefunction is
	\exyear{NET JUNE 2012}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\psi=\left(\frac{2}{a}\right)^{3 / 2} \sin \frac{3 \pi x}{a} \sin \frac{5 \pi y}{a} \sin \frac{6 \pi z}{a}$
	\task[\textbf{B.}] $\psi=\left(\frac{2}{a}\right)^{3 / 2} \sin \frac{7 \pi x}{a} \sin \frac{4 \pi y}{a} \sin \frac{3 \pi z}{a}$
	\task[\textbf{C.}]$\psi=\left(\frac{2}{a}\right)^{3 / 2} \sin \frac{4 \pi x}{a} \sin \frac{8 \pi y}{a} \sin \frac{2 \pi z}{a}$
	\task[\textbf{D.}]$\psi=\left(\frac{2}{a}\right)^{3 / 2} \sin \frac{\pi x}{a} \sin \frac{2 \pi y}{a} \sin \frac{3 \pi z}{a}$
\end{tasks}
\begin{answer}
	$E_{n_{x}, n_{y}, n_{z}}=\left(n_{x}^{2}+n_{y}^{2}+n_{z}^{2}\right) \frac{\pi^{2} \hbar^{2}}{2 m a^{2}}=\frac{14 \pi^{2} \hbar^{2}}{2 m a^{2}}$ $\Rightarrow n_{x}^{2}+n_{y}^{2}+n_{z}^{2}=14 \Rightarrow n_{x}=1, n_{y}=2, n_{z}=3 .$\\
	The correct option is \textbf{(d)}
\end{answer}
\begin{minipage}{\textwidth}
	\item $\text { A particle in one-dimension is in the potential }$
	$V(x)=\left\{\begin{array}{llll}
	\infty & , & \text { if } & x<0 \\
	-V_{0} & , & \text { if } & 0 \leq x \leq l \\
	0 & & \text { if } & x>l
	\end{array}\right.$
	$\text { If there is at least one bound state, the minimum depth of potential is }$
	\exyear{NET JUNE 2012}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\frac{\hbar^{2} \pi^{2}}{8 m l^{2}}$
	\task[\textbf{B.}]$\frac{\hbar^{2} \pi^{2}}{2 m l^{2}}$
	\task[\textbf{C.}]$\frac{2 \hbar^{2} \pi^{2}}{m l^{2}}$
	\task[\textbf{D.}]$\frac{\hbar^{2} \pi^{2}}{m l^{2}}$
\end{tasks}
\begin{answer}
	\begin{figure}[H]
		\centering
		\includegraphics[height=3cm,width=5cm]{diagram-20210921(4)-crop}
		\caption{}
		\label{}
	\end{figure}
	For bound state, $-V_{0}<E<0$\\
	Wave function in region $\mathrm{I}, \psi_{I}=0, \psi_{I I}=A \sin k x+B \cos k x, \psi_{I I I}=c e^{-\gamma x}$ where $k=\frac{\sqrt{2 m\left(V_{0}+E\right)}}{\hbar^{2}}, \gamma=\frac{\sqrt{2 m(-E)}}{\hbar^{2}}$.\\
	Use Boundary condition at $x=0$ and $x=l$\\
	(wave function is continuous and differential at $x=0$ and $x=l$ ), one will get $k \cot k l=-\gamma \Rightarrow k l \cot k l=-\gamma l \Rightarrow \eta=-\xi \cot \xi$\\
	where $\gamma l=\eta, k l=\xi$.\\
	$\Rightarrow \eta^{2}+\xi^{2}=\frac{2 m V_{0} l^{2}}{\hbar^{2}}$\\
	$\text { For one bound state }\left(\frac{2 m V_{0} l^{2}}{\hbar^{2}}\right)^{1 / 2}=\frac{\pi}{2} \Rightarrow V_{0}=\frac{\pi^{2} \hbar^{2}}{8 m l^{2}} \text {. }$\\
	\begin{figure}[H]
		\centering
		\includegraphics[height=3cm,width=5cm]{diagram-20210921(3)-crop}
		\caption{}
		\label{}
	\end{figure}
	The correct option is \textbf{(a)}
\end{answer}
\begin{minipage}{\textwidth}
	\item $\text { The energy eigenvalues of a particle in the potential } V(x)=\frac{1}{2} m \omega^{2} x^{2}-a x \text { are }$
	\exyear{NET DEC 2012}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $E n=\left(n+\frac{1}{2}\right) \hbar \omega-\frac{a^{2}}{2 m \omega^{2}}$
	\task[\textbf{B.}]$E n=\left(n+\frac{1}{2}\right) \hbar \omega+\frac{a^{2}}{2 m \omega^{2}}$
	\task[\textbf{C.}]$E n=\left(n+\frac{1}{2}\right) \hbar \omega-\frac{a^{2}}{m \omega^{2}}$
	\task[\textbf{D.}]$E n=\left(n+\frac{1}{2}\right) \hbar \omega$
\end{tasks}
\begin{answer}
	Hamiltonian $(H)$ of Harmonic oscillator, $H=\frac{p_{x}^{2}}{2 m}+\frac{1}{2} m \omega^{2} x^{2}$\\
	Eigenvalue of this, $E_{n}=\left(n+\frac{1}{2}\right) \hbar \omega$\\
	But here, $H=\frac{p_{x}^{2}}{2 m}+\frac{1}{2} m \omega^{2} x^{2}-a x \Rightarrow H=\frac{p_{x}^{2}}{2 m}+\frac{1}{2} m \omega^{2}\left[x^{2}-\frac{2 a x}{m \omega^{2}}+\frac{a^{2}}{m^{2} \omega^{4}}\right]-\frac{a^{2}}{2 m \omega^{2}}$\\
	$H=\frac{p_{x}^{2}}{2 m}+\frac{1}{2} m \omega^{2}\left[x-\frac{a}{m \omega^{2}}\right]^{2}-\frac{a^{2}}{2 m \omega^{2}}$\\
	Energy eigenvalue, $E_{n}=\left(n+\frac{1}{2}\right) \hbar \omega-\frac{a^{2}}{2 m \omega^{2}}$\\
	The correct option is \textbf{(a)}
\end{answer}
\begin{minipage}{\textwidth}
	\item A particle is in the ground state of an infinite square well potential is given by,
	$$
	V(x)= \begin{cases}0 & \text { for }-a \leq x \leq a \\ \infty & \text { otherwise }\end{cases}
	$$
	The probability to find the particle in the interval between $-\frac{a}{2}$ and $\frac{a}{2}$ is
	\exyear{NET DEC 2013}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\frac{1}{2}$
	\task[\textbf{B.}]$\frac{1}{2}+\frac{1}{\pi}$
	\task[\textbf{C.}]$\frac{1}{2}-\frac{1}{\pi}$
	\task[\textbf{D.}]$\frac{1}{\pi}$
\end{tasks}
\begin{answer}
	The probability to find the particle in the interval between $-\frac{a}{2}$ and $\frac{a}{2}$ is
	$$
	\begin{aligned}
	&=\int_{-a / 2}^{a / 2} \sqrt{\frac{2}{2 a}} \cdot \sqrt{\frac{2}{2 a}} \cos \frac{\pi x}{2 a} \cdot \cos \frac{\pi x}{2 a} d x=\int_{-a / 2}^{a / 2} \frac{1}{a} \cos ^{2} \frac{\pi x}{2 a} d x=\frac{1}{a} \times \frac{1}{2}\left[\int_{-a / 2}^{a / 2}\left(1+\cos \frac{2 \pi x}{2 a}\right) d x\right] \\
	&=\frac{1}{2 a}\left[x+\frac{a}{\pi} \sin \frac{\pi x}{a}\right]_{-a / 2}^{a / 2}=\frac{1}{2 a}\left[\frac{a}{2}+\frac{a}{2}+\frac{a}{\pi}(1+1)\right]=\frac{1}{2 a}\left[a+\frac{2 a}{\pi}\right]=\left(\frac{1}{2}+\frac{1}{\pi}\right)
	\end{aligned}
	$$
	The correct option is \textbf{(b)}
\end{answer}
\begin{minipage}{\textwidth}
	\item A particle of mass $m$ in the potential $V(x, y)=\frac{1}{2} m \omega^{2}\left(4 x^{2}+y^{2}\right)$, is in an eigenstate of energy $E=\frac{5}{2} \hbar \omega$. The corresponding un-normalized eigen function is
	\exyear{NET JUNE 2014}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $y \exp \left[-\frac{m \omega}{2 \hbar}\left(2 x^{2}+y^{2}\right)\right]$
	\task[\textbf{B.}]$x \exp \left[-\frac{m \omega}{2 \hbar}\left(2 x^{2}+y^{2}\right)\right]$
	\task[\textbf{C.}]$y \exp \left[-\frac{m \omega}{2 \hbar}\left(x^{2}+y^{2}\right)\right]$
	\task[\textbf{D.}]$x y \exp \left[-\frac{m \omega}{2 \hbar}\left(x^{2}+y^{2}\right)\right]$
\end{tasks}
\begin{answer}
	$V(x, y)=\frac{1}{2} m \omega^{2}\left(4 x^{2}+y^{2}\right), E=\frac{5}{2} \hbar \omega$\\
	$\begin{aligned}
	&\Rightarrow V(x, y)=\frac{1}{2} m(2 \omega)^{2} x^{2}+\frac{1}{2} m \omega^{2} y^{2} \\
	&\text { Now, } E_{n}=\left(n_{x}+\frac{1}{2}\right) \hbar \omega_{x}+\left(n_{y}+\frac{1}{2}\right) \hbar \omega_{y}=\left(n_{x}+\frac{1}{2}\right) 2 \hbar \omega+\left(n_{y}+\frac{1}{2}\right) \hbar \omega \\
	&\Rightarrow E_{n}=\left(2 n_{x}+n_{y}+\frac{3}{2}\right) \hbar \omega \\
	&\because E_{n}=\frac{5}{2} \hbar \omega \text { when } n_{x}=0 \text { and } n_{y}=1 .
	\end{aligned}$\\
	The correct option is \textbf{(a)}
\end{answer}
\begin{minipage}{\textwidth}
	\item A particle of mass $m$ in three dimensions is in the potential
	$$
	V(r)= \begin{cases}0, & r<a \\ \infty, & r>a\end{cases}
	$$
	Its ground state energy is
	\exyear{NET JUNE 2014}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\frac{\pi^{2} \hbar^{2}}{2 m a^{2}}$
	\task[\textbf{B.}]$\frac{\pi^{2} \hbar^{2}}{m a^{2}}$
	\task[\textbf{C.}]$\frac{3 \pi^{2} \hbar^{2}}{2 m a^{2}}$
	\task[\textbf{D.}]$\frac{9 \pi^{2} \hbar^{2}}{2 m a^{2}}$
\end{tasks}
\begin{answer}
	$\left(-\frac{\hbar^{2}}{2 m}\right) \frac{d^{2} u(r)}{d r^{2}}+\frac{l(l+1)}{2 m r^{2}}+V(r) u(r)=E u(r)$ $\frac{d^{2} u(r)}{d r^{2}}=-K^{2} u(r) $\\
	$\because K=\sqrt{\frac{2 m E}{\hbar^{2}}}, l=0, V(r)=0$ $u(r)=A \sin K r+B \cos K r$\\
	Using boundary condition, $B=0$,\\
	$u(r)=A \sin K r, r=a, u(r)=0 \Rightarrow \sin K a=0 \Rightarrow K a=n \pi \Rightarrow E=\frac{\pi^{2} \hbar^{2}}{2 m a^{2}} \quad \because n=1$
\end{answer}
\begin{minipage}{\textwidth}
	\item A particle in the infinite square well potential
	$$
	V(x)= \begin{cases}0 & , 0<x<a \\ \infty & \text { otherwise }\end{cases}
	$$
	is prepared in a state with the wavefunction
	$$
	\psi(x)= \begin{cases}A \sin ^{3}\left(\frac{\pi x}{a}\right), & 0<x<a \\ 0 & \text { otherwise }\end{cases}
	$$
	The expectation value of the energy of the particle is
	\exyear{NET JUNE 2014}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\frac{5 \hbar^{2} \pi^{2}}{2 m a^{2}}$
	\task[\textbf{B.}]$\frac{9 \hbar^{2} \pi^{2}}{2 m a^{2}}$
	\task[\textbf{C.}]$\frac{9 \hbar^{2} \pi^{2}}{10 m a^{2}}$
	\task[\textbf{D.}]$\frac{\hbar^{2} \pi^{2}}{2 m a^{2}}$
\end{tasks}
\begin{answer}
	$ V(x)=\left\{\begin{array}{ll}
	0, & 0<x<a \\
	\infty, & \text { otherwise }
	\end{array} \quad \psi(x)=\left\{\begin{array}{ll}
	A \sin ^{3}\left(\frac{\pi x}{a}\right), & 0<x<a \\
	0 & \text { otherwise }
	\end{array}\right\}\right.$\\
	$\begin{aligned}
	\psi(x) &=A \sin ^{3}\left(\frac{\pi x}{a}\right)=A \frac{3}{4} \sin \frac{\pi x}{a}-A \frac{1}{4} \sin \frac{3 \pi x}{a} \quad\left(\because \sin 3 A=3 \sin A-4 \sin ^{3} A\right) \\
	&=\frac{A}{4}\left[\sqrt{\frac{a}{2}} \sqrt{\frac{2}{a}} \times 3 \sin \frac{\pi x}{a}-\sqrt{\frac{a}{2}} \sqrt{\frac{2}{a}} \sin \frac{3 \pi x}{a}\right] \Rightarrow \psi(x)=\frac{A}{4}\left[3 \sqrt{\frac{a}{2}} \phi_{1}(x)-\sqrt{\frac{a}{2}} \phi_{3}(x)\right] \\
	\langle\psi \mid \psi\rangle &=1 \Rightarrow 9 \frac{a}{32} A^{2}+\frac{a}{32} A^{2}=1 \Rightarrow \frac{10 a}{32} A^{2}=1 \Rightarrow A=\sqrt{\frac{32}{10 a}} \\
	\psi(x) &=\frac{1}{4}\left(3 \cdot \sqrt{\frac{a}{2}} \sqrt{\frac{32}{10 a}} \phi_{1}(x)-\sqrt{\frac{a}{2}} \sqrt{\frac{32}{10 a}} \phi_{3}(x)\right)=\frac{3}{\sqrt{10}} \phi_{1}(x)-\frac{1}{\sqrt{10}} \phi_{3}(x)
	\end{aligned}$\\
	Now, $\quad E_{1}=\frac{\pi^{2} \hbar^{2}}{2 m a^{2}}, \quad E_{3}=\frac{9 \pi^{2} \hbar^{2}}{2 m a^{2}} \Rightarrow\langle E\rangle=a_{n} P\left(a_{n}\right)$
	Probability $P\left(E_{1}\right)=\frac{\left\langle\left.\left\langle\varphi_{1} \mid \psi\right\rangle\right|^{2}\right.}{\langle\psi \mid \psi\rangle}=\frac{9}{10}, P\left(E_{3}\right)=\frac{\left|\left\langle\phi_{2} \mid \psi\right\rangle\right|^{2}}{\langle\psi \mid \psi\rangle}=\frac{1}{10}$
	$\langle E\rangle=\frac{9}{10} \times \frac{\pi^{2} \hbar^{2}}{2 m a^{2}}+\frac{1}{10} \times \frac{9 \pi^{2} \hbar^{2}}{2 m a^{2}} \Rightarrow\langle E\rangle=\frac{9 \pi^{2} \hbar^{2}}{10 m a^{2}}$\\
	The correct option is \textbf{(c)}
\end{answer}
\begin{minipage}{\textwidth}
	\item The ground state energy of the attractive delta function potential
	$$
	V(x)=-b \delta(x),
	$$
	where $b>0$, is calculated with the variational trial function
	$$
	\psi(x)=\left\{\begin{array}{ccc}
	A \cos \frac{\pi x}{2 a}, & \text { for } & -a<x<a, \\
	0, & & \text { otherwise, }
	\end{array}\right\} \text { is }
	$$
	\exyear{NET DEC 2014}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $-\frac{m b^{2}}{\pi^{2} \hbar^{2}}$
	\task[\textbf{B.}]$-\frac{2 m b^{2}}{\pi^{2} \hbar^{2}}$
	\task[\textbf{C.}]$-\frac{m b^{2}}{2 \pi^{2} \hbar^{2}}$
	\task[\textbf{D.}]$-\frac{m b^{2}}{4 \pi^{2} \hbar^{2}}$
\end{tasks}
\begin{answer}
	$V(x)=-b \delta(x) ; \quad b>0 \quad \text { and } \quad \psi(x)=\left\{A \cos \frac{\pi x}{2 a} ; \quad-a<x<a\right.$\\
	Normalized $\psi=\sqrt{\frac{2}{2 a}} \cos \frac{\pi x}{2 a}$\\
	$\langle T\rangle=\int_{-a}^{a} \psi^{*}\left(\frac{-\hbar^{2}}{2 m}\right) \frac{\partial^{2}}{\partial x^{2}} \psi d x=\frac{\pi^{2} \hbar^{2}}{8 m a^{2}}$\\
	$\langle V\rangle=\int_{-a}^{a} \psi^{*}-b \delta(x) \psi d x=\frac{2}{2 a}(-b)=-\frac{b}{a}$\\
	$\langle E\rangle=\frac{\pi^{2} \hbar^{2}}{8 m a^{2}}-\frac{b}{a} \Rightarrow \frac{\partial\langle E\rangle}{\partial a}=\frac{-2 \pi^{2} \hbar^{2}}{8 m a^{3}}+\frac{b}{a^{2}}=0 \Rightarrow \frac{-\pi^{2} \hbar^{2}}{4 m a}+b=0 \Rightarrow a=\frac{\pi^{2} \hbar^{2}}{4 m b}$\\
	Put the value of $a$ in equation: $\langle E\rangle=\frac{\pi^{2} \hbar^{2}}{8 m a^{2}}-\frac{b}{a}=\frac{\pi^{2} \hbar^{2}(4 m b)^{2}}{8 m\left(\pi^{2} \hbar^{2}\right)^{2}}-\frac{b(4 m b)}{\left(\pi^{2} \hbar^{2}\right)}=-\frac{2 m b^{2}}{\pi^{2} \hbar^{2}}$\\
	The correct option is \textbf{(b)}
\end{answer}
\begin{minipage}{\textwidth}
	\item Let $|\psi\rangle=c_{0}|0\rangle+c_{1}|1\rangle$ (where $c_{0}$ and $c_{1}$ are constants with $c_{0}^{2}+c_{1}^{2}=1$ ) be a linear combination of the wavefunctions of the ground and first excited states of the onedimensional harmonic oscillator. For what value of $c_{0}$ is the expectation value $\langle x\rangle$ a maximum?
	\exyear{NET DEC 2014}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\langle x\rangle=\sqrt{\frac{\hbar}{m \omega}}, \quad c_{0}=\frac{1}{\sqrt{2}}$
	\task[\textbf{B.}]$\langle x\rangle=\sqrt{\frac{\hbar}{2 m \omega}}, \quad c_{0}=\frac{1}{2}$
	\task[\textbf{C.}]$\langle x\rangle=\sqrt{\frac{\hbar}{2 m \omega}}, \quad c_{0}=\frac{1}{\sqrt{2}}$
	\task[\textbf{D.}]$\langle x\rangle=\sqrt{\frac{\hbar}{m \omega}}, \quad c_{0}=\frac{1}{2}$
\end{tasks}
\begin{answer}
	$\begin{aligned}
	&|\psi\rangle=c_{0}|0\rangle+c_{1}|1\rangle \\
	&\qquad \begin{array}{l}
	\langle X\rangle=\langle\psi|X| \psi\rangle \\
	\Rightarrow\langle X\rangle=2 c_{0} c_{1}\langle 0|X| 1\rangle=\left[\left(c_{0}^{2}+c_{1}^{2}\right)-\left(c_{0}-c_{1}\right)^{2}\right]\langle 0|X| 1\rangle=\left[1-\left(c_{0}-c_{1}\right)^{2}\right]\langle 0|X| 1\rangle \\
	\text { For max } \quad\langle X\rangle=c_{0}=c_{1} \quad \quad \because c_{0}^{2}+c_{1}^{2}=1 \Rightarrow c_{0}=\frac{1}{\sqrt{2}} \\
	\Rightarrow\langle X\rangle=2 \frac{1}{\sqrt{2}} \frac{1}{\sqrt{2}}\langle 0|X| 1\rangle=\langle 0|X| 1\rangle \\
	\sqrt{\frac{\hbar}{2 m \omega}}\left(\left\langle 0\left|a+a^{+}\right| 1\right\rangle\right) \Rightarrow\langle X\rangle=\sqrt{\frac{\hbar}{2 m \omega}}
	\end{array}
	\end{aligned}$\\
	The correct option is \textbf{(C)}
\end{answer}
\begin{minipage}{\textwidth}
	\item The ratio of the energy of the first excited state $E_{1}$, to that of the ground state $E_{0}$, to that of a particle in a three-dimensional rectangular box of side $L, L$ and $\frac{L}{2}$, is
	\exyear{NET JUNE 2015}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $3: 2$
	\task[\textbf{B.}]$2: 1$
	\task[\textbf{C.}]$4: 1$
	\task[\textbf{D.}]$4: 3$
\end{tasks}
\begin{answer}
	$E=\frac{\pi^{2} \hbar^{2}}{2 m L^{2}}\left[n_{x}^{2}+n_{y}^{2}+4 n_{z}^{2}\right]$, for ground state $n_{x}=1, n_{y}=1, n_{z}=1 \Rightarrow E_{0}=\frac{6 \pi^{2} \hbar^{2}}{2 m L^{2}}$\\
	For first excited state $n_{x}=1, n_{y}=2, n_{z}=1 \Rightarrow E=E_{1}=\frac{\pi^{2} \hbar^{2}}{2 m L^{2}}(1+4+4)=\frac{9 \pi^{2} \hbar^{2}}{2 m L^{2}}$
	$\therefore \frac{E_{1}}{E_{0}}=\frac{9}{6}=\frac{3}{2}$\\
	The correct option is \textbf{(a)}
\end{answer}
\begin{minipage}{\textwidth}
	\item The ground state energy of a particle in potential $V(x)=g|x|$, estimated using the trail wavefunction\\
	$\psi(x)= \begin{cases}\sqrt{\frac{c}{a^{5}}}\left(a^{2}-x^{2}\right), & x<|a| \\ 0, & x \geq|a|\end{cases}$\\
	$\text { (where } g \text { and } c \text { are constants) is }$
	\exyear{NET DEC 2015}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\frac{15}{16}\left(\frac{\hbar^{2} g^{2}}{m}\right)^{1 / 3}$
	\task[\textbf{B.}]$\frac{5}{6}\left(\frac{\hbar^{2} g^{2}}{m}\right)^{1 / 3}$
	\task[\textbf{C.}] $\frac{3}{4}\left(\frac{\hbar^{2} g^{2}}{m}\right)^{1 / 3}$
	\task[\textbf{D.}] $\frac{7}{8}\left(\frac{\hbar^{2} g^{2}}{m}\right)^{1 / 3}$
\end{tasks}
\begin{answer}
	$\begin{aligned}
	&\text { on: } \int_{-a}^{a} \psi^{*} \psi d x=1 \Rightarrow c=\frac{15}{16} \\
	&\langle T\rangle=\frac{-\hbar^{2}}{2 m}\left(\frac{15}{16 a^{2}}\right) \int_{-a}^{a}\left(a^{2}-x^{2}\right) \frac{\partial^{2}}{\partial x^{2}}\left(a^{2}-x^{2}\right) d x \Rightarrow\langle T\rangle=\frac{10 \hbar^{2}}{4 m a^{2}} \\
	&\langle V\rangle=\frac{15 \times 2 g}{16 a^{5}} \int_{0}^{a} x\left(a^{2}-x^{2}\right) d x \Rightarrow\langle V\rangle=\frac{5}{16} g a \\
	&E=\langle T\rangle+\langle V\rangle
	\end{aligned}$
	$$
	\begin{aligned}
	&E=\frac{10 \hbar^{2}}{4 m a^{2}}+\frac{5 g a}{16} \\
	&\frac{d E}{d a}=0 \Rightarrow a^{3}=\frac{8 \hbar}{m g} \Rightarrow a=2\left(\frac{\hbar^{2}}{m g}\right)^{\frac{1}{3}}
	\end{aligned}
	$$
	put the value of $a$ in equation (i)
	$$
	E=\frac{15}{16}\left(\frac{\hbar^{2} g^{2}}{m}\right)^{\frac{1}{3}}
	$$
	The correct option is \textbf{(a)}
\end{answer}
\begin{minipage}{\textwidth}
	\item The state of a particle of mass $m$ in a one dimensional rigid box in the interval 0 to $L$ is given by the normalized wavefunction $\psi(x)=\sqrt{\frac{2}{L}}\left(\frac{3}{5} \sin \left(\frac{2 \pi x}{L}\right)+\frac{4}{5} \sin \left(\frac{4 \pi x}{L}\right)\right)$. If its energy is measured the possible outcomes and the average value of energy are, respectively
	\exyear{NET JUNE 2016}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\frac{h^{2}}{2 m L^{2}}, \frac{2 h^{2}}{m L^{2}}$ and $\frac{73}{50} \frac{h^{2}}{m L^{2}}$
	\task[\textbf{B.}] $\frac{h^{2}}{8 m L^{2}}, \frac{h^{2}}{2 m L^{2}}$ and $\frac{19}{40} \frac{h^{2}}{m L^{2}}$
	\task[\textbf{C.}]$\frac{h^{2}}{2 m L^{2}}, \frac{2 h^{2}}{m L^{2}}$ and $\frac{19}{10} \frac{h^{2}}{m L^{2}}$
	\task[\textbf{D.}]$\frac{h^{2}}{8 m L^{2}}, \frac{2 h^{2}}{m L^{2}}$ and $\frac{73}{200} \frac{h^{2}}{m L^{2}}$
\end{tasks}
\begin{answer}
	$\psi(x)=\sqrt{\frac{2}{L}}\left(\frac{3}{5} \sin \left(\frac{2 \pi x}{L}\right)+\frac{4}{5} \sin \left(\frac{4 \pi x}{L}\right)\right)$\\
	Measurement $E=\frac{n^{2} \pi^{2} \hbar^{2}}{2 m L^{2}}$
	$\because n=2 \Rightarrow E_{2}=\frac{h^{2}}{2 m L^{2}}$ and $n=4 \Rightarrow E_{4}=\frac{2 h^{2}}{m L^{2}}$\\
	Probability $p\left(E_{2}\right)=\frac{9}{25}$ and $p\left(E_{4}\right)=\frac{16}{25}$\\
	Now, average value of energy is
	$\langle E\rangle=\sum a_{n} p\left(a_{n}\right)=\frac{9}{25} \times \frac{h^{2}}{2 m L^{2}}+\frac{16}{25} \times \frac{2 h^{2}}{m L^{2}}=\frac{73 h^{2}}{50 m L^{2}}$\\
	The correct option is \textbf{(a)}
\end{answer}
\begin{minipage}{\textwidth}
	\item A particle of charge $q$ in one dimension is in a simple harmonic potential with angular frequency $\omega .$ It is subjected to a time- dependent electric field $E(t)=A e^{-\left(\frac{t}{\tau}\right)^{2}}$, where $A$ and $\tau$ are positive constants and $\omega \tau \gg 1$. If in the distant past $t \rightarrow-\infty$ the particle was in its ground state, the probability that it will be in the first excited state as $t \rightarrow+\infty$ is proportional to
	\exyear{NET DEC 2016}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $e^{-\frac{1}{2}(\omega \tau)^{2}}$
	\task[\textbf{B.}] $e^{\frac{1}{2}(\omega \tau)^{2}}$
	\task[\textbf{C.}] 0
	\task[\textbf{D.}]$\frac{1}{(\omega \tau)^{2}}$
\end{tasks}
\begin{answer}
	$\text { Transition probability is proportional to } P_{i f} \propto\left|\int_{-\infty}^{\infty} e^{-\frac{t^{2}}{\tau^{2}}} e^{i \omega_{f} t}\right|^{2} \text { where }$\\
	$\begin{aligned}
	&\omega_{f i}=\frac{\frac{3}{2} \hbar \omega-\frac{1}{2} \hbar \omega}{\hbar}=\omega \\
	&P_{i f}=\left|\int_{-\infty}^{\infty} \exp -\frac{t^{2}}{\tau^{2}}+i \omega t d t\right|^{2}
	\end{aligned}$\\
	$\begin{aligned}
	&\text { Now calculate } \int_{-\infty}^{\infty} \exp \left(-\frac{t^{2}}{\tau^{2}}+i \omega t\right) d t=\int_{-\infty}^{\infty} \exp -\frac{1}{\tau^{2}}\left(t^{2}-i \omega t \tau^{2}+\left(\frac{i \omega \tau^{2}}{2}\right)^{2}-\left(\frac{i \omega \tau^{2}}{2}\right)^{2}\right) \\
	&=\exp \left(-\frac{\omega^{2} \tau^{2}}{4}\right) \int_{-\infty}^{\infty} \exp \frac{1}{\tau^{2}}\left(t-\frac{i \omega t}{2}\right)^{2} d t \\
	&P_{i f}=\left.\left|\int_{-\infty}^{\infty} \exp -\frac{t^{2}}{\tau^{2}}+i \omega t d t\right|^{2}\right|^{2} \\
	&P_{i f}=\left|\exp \left(-\frac{\omega^{2} \tau^{2}}{4}\right) \int_{-\infty}^{\infty} \exp \frac{1}{\tau^{2}}\left(t-\frac{i \omega t}{2}\right)^{2} d t\right|^{2} \\
	&P_{i f} \propto \exp -\frac{\omega^{2} \tau^{2}}{2}
	\end{aligned}$\\
	The correct option is \textbf{(a)}
\end{answer}
\begin{minipage}{\textwidth}
	\item Consider a potential barrier $A$ of height $V_{0}$ and width $b$, and another potential barrier $B$ of height $2 V_{0}$ and the same width $b$. The ratio $T_{A} / T_{B}$ of tunnelling probabilities $T_{A}$ and $T_{B}$, through barriers $A$ and $B$ respectively, for a particle of energy $V_{0} / 100$ is best approximated by
	\exyear{NET JUNE 2017}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}](a) $\exp \left[(\sqrt{1.99}-\sqrt{0.99}) \sqrt{8 m V_{0} b^{2} / \hbar^{2}}\right]$
	\task[\textbf{B.}]$\exp \left[(\sqrt{1.98}-\sqrt{0.98}) \sqrt{8 m V_{0} b^{2} / \hbar^{2}}\right]$
	\task[\textbf{C.}] $\exp \left[(\sqrt{2.99}-\sqrt{0.99}) \sqrt{8 m V_{0} b^{2} / \hbar^{2}}\right]$
	\task[\textbf{D.}]$\exp \left[(\sqrt{2.98}-\sqrt{0.98}) \sqrt{8 m V_{0} b^{2} / \hbar^{2}}\right]$
\end{tasks}
\begin{answer}
	$T \alpha e^{-\sqrt{2 m(V-E)}}, \quad$ where $E=\frac{V_{0}}{100}$
	For potential $A, \quad V=V_{0}$
	$$
	T_{A} \alpha e^{-\sqrt{\frac{2 m}{\hbar^{2}}}\left(V_{0}-\frac{V_{0}}{100}\right)} \Rightarrow T_{A} \alpha e^{-\sqrt{\frac{2 m}{\hbar^{2}}\left(\frac{99}{100} V_{0}\right)}} \alpha e^{-\sqrt{2 m\left(0.99 V_{0}\right)}}
	$$
	For Potential $B, \quad V=2 V_{0}$ and $E=\frac{V_{0}}{100}$
	$T_{B} \alpha e^{-\sqrt{\frac{2 m}{\hbar^{2}}\left(2 V_{0}-\frac{V_{0}}{100}\right)}} \Rightarrow T_{B} \alpha e^{-\sqrt{\frac{2 m}{\hbar^{2}}\left(\frac{199 V_{0}}{100}\right)}} \alpha e^{-\sqrt{2 m\left(1.99 V_{0}\right)}}$
	$$
	\begin{aligned}
	&\frac{T_{A}}{T_{B}}=\frac{e^{-\sqrt{0.99 V_{0}}}}{e^{-\sqrt{1.99 V_{0}}}} \\
	&\frac{T_{A}}{T_{B}}=\left(e^{\sqrt{1.99 V_{0}}}-e^{-\sqrt{0.99 V_{0}}}\right)
	\end{aligned}
	$$
	THe correct option is \textbf{(a)}
\end{answer}
\begin{minipage}{\textwidth}
	\item  Using the trial function
	$\psi(x)=\left\{\begin{array}{cc}
	A\left(a^{2}-x^{2}\right), & -a<x<a \\
	0 & \text { otherwise }
	\end{array}\right.$\\
	the ground state energy of a one-dimensional harmonic oscillator is
	\exyear{NET JUNE 2017}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\hbar \omega$
	\task[\textbf{B.}] $\sqrt{\frac{5}{14}} \hbar \omega$
	\task[\textbf{C.}]$\frac{1}{2} \hbar \omega$
	\task[\textbf{D.}] $\sqrt{\frac{5}{7}} \hbar \omega$
\end{tasks}
\begin{answer}
	$\psi(x)= \begin{cases}A\left(a^{2}-x^{2}\right), & -a<x<a \\ 0 & \text { otherwise }\end{cases}$ For normalization
	$$
	\begin{aligned}
	&\int \psi^{*} \psi d x=1 \\
	&A^{2}=\frac{15}{16 a^{5}} \Rightarrow A=\sqrt{\frac{15}{16 a^{5}}}
	\end{aligned}
	$$
	$\begin{aligned}
	&\langle T\rangle=\frac{-\hbar^{2}}{2 m} \int_{-a}^{a} \psi^{*} \frac{\partial^{2}}{\partial x^{2}} \psi d x=\frac{-\hbar^{2}}{2 m} \frac{15}{16 a^{5}} \cdot(-2)(2) \int_{0}^{a}\left(a^{2}-x^{2}\right) d x \\
	&\langle T\rangle=\frac{5 \hbar^{2}}{4 m a^{2}} \\
	&\langle V\rangle=\int_{-a}^{a} \psi^{*} V \psi d x, \text { where } V(x)=\frac{1}{2} m \omega^{2} x^{2}=\frac{1}{2} m \omega^{2} \frac{15}{16 a^{5}} 2 \int_{0}^{a} x^{2}\left(a^{2}-x^{2}\right)^{2} d x . \\
	&\langle V\rangle=\frac{m \omega^{2} a^{2}}{14}
	\end{aligned}$\\
	$\begin{aligned}
	&E=T+V=\frac{5 \hbar^{2}}{4 m a^{2}}+\frac{m \omega^{2} a^{2}}{14} \\
	&\frac{d E}{d a}=0 \Rightarrow \frac{5 \times(-2) \hbar^{2}}{4 m a^{3}}+\frac{m \omega^{2} a}{7}=0 \Rightarrow a^{4}=\frac{35}{2}\left(\frac{\hbar^{2}}{m^{2} \omega^{2}}\right) . \\
	&a^{2}=\left(\frac{35}{2}\right)^{1 / 2}\left(\frac{\hbar}{m \omega}\right) . \\
	&E=\frac{5}{4} \times \frac{\hbar^{2}}{m} \cdot \frac{m \omega}{\hbar} \sqrt{\frac{2}{35}}+\frac{m \omega^{2}}{14} \sqrt{\frac{35}{2}} \frac{\hbar}{m \omega} .
	\end{aligned}$\\
	$=\frac{\hbar \omega}{2}\left(\frac{5}{2} \sqrt{\frac{2}{35}}+\frac{1}{7} \sqrt{\frac{35}{2}}\right)=\frac{\hbar \omega}{2}\left(\sqrt{\frac{5}{14}}+\sqrt{\frac{5}{14}}\right)=\hbar \omega \sqrt{\frac{5}{14}}$\\
	The correct option is \textbf{(b)}
\end{answer}
\begin{minipage}{\textwidth}
	\item A particle of mass $m$ is confined in a three-dimensional box by the potential
	$$
	V(x, y, z)=\left\{\begin{array}{lc}
	0, & 0 \leq x, y, z \leq a \\
	\infty & \text { otherwise }
	\end{array}\right.
	$$
	The number of eigenstates of Hamiltonian with energy $\frac{9 \hbar^{2} \pi^{2}}{2 m a^{2}}$ is
	\exyear{NET JUNE 2018}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}]1
	\task[\textbf{B.}]6
	\task[\textbf{C.}]3
	\task[\textbf{D.}]4
\end{tasks}
\begin{answer}
	$\begin{aligned}
	&E_{n_{x}, n_{y}, n_{z}}=\frac{9 \pi^{2} \hbar^{2}}{2 m a^{2}} \\
	&\left.\qquad \begin{array}{rrr}
	n_{x} & n_{y} & n_{z} \\
	1 & 2 & 2 \\
	2 & 2 & 1 \\
	2 & 1 & 2
	\end{array}\right\} \\
	&\text { where } E_{x x, x y, x z}=\left(n_{x}^{2}+n_{y}^{2}+n_{z}^{2}\right) \frac{\pi^{2} \hbar^{2}}{2 m a^{2}}
	\end{aligned}$\\
	The correct option is \textbf{(c)}
\end{answer}
\begin{minipage}{\textwidth}
	\item At $t=0$, the wavefunction of an otherwise free particle confined between two infinite walls at $x=0$ and $x=L$ is $\psi(x, t=0)=\sqrt{\frac{2}{L}}\left(\sin \frac{\pi x}{L}-\sin \frac{3 \pi x}{L}\right)$. Its wave function at a later time $t=\frac{m L^{2}}{4 \pi h}$ is
	\exyear{NET JUNE 2018}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\sqrt{\frac{2}{L}}\left(\sin \frac{\pi x}{L}-\sin \frac{3 \pi x}{L}\right) e^{i \pi / 6}$
	\task[\textbf{B.}]$\sqrt{\frac{2}{L}}\left(\sin \frac{\pi x}{L}+\sin \frac{3 \pi x}{L}\right) e^{-i \pi / 6}$
	\task[\textbf{C.}]$\sqrt{\frac{2}{L}}\left(\sin \frac{\pi x}{L}-\sin \frac{3 \pi x}{L}\right) e^{-i \pi / 8}$
	\task[\textbf{D.}]$\sqrt{\frac{2}{L}}\left(\sin \frac{\pi x}{L}+\sin \frac{3 \pi x}{L}\right) e^{-i \pi / 6}$
\end{tasks}
\begin{answer}
	$\begin{aligned}
	&\text {solution } \psi(x, t=0)=\left(\sqrt{\frac{2}{L}} \sin \frac{\pi x}{L}-\sqrt{\frac{2}{L}} \sin \frac{3 \pi x}{L}\right) \\
	&\psi(x, t=0)=\left|\varphi_{1}\right\rangle-\left|\varphi_{3}\right\rangle \\
	&\psi(x, t)=\left|\varphi_{1}\right\rangle e^{\frac{-i E_{1} t}{\hbar}}-\left|\varphi_{3}\right\rangle e^{\frac{-i E_{3} t}{\hbar}} \\
	&E_{1}=\frac{\pi^{2} \hbar^{2}}{2 m L^{2}} E_{3}=\frac{9 \pi^{2} \hbar^{2}}{2 m L^{2}} t=\frac{m L^{2}}{4 \pi \hbar} \\
	&\psi(x, t)=\left|\varphi_{1}\right\rangle e^{\frac{-i \pi}{8}}-\left|\varphi_{3}\right\rangle e^{\frac{-9 i \pi}{8}}=e^{\frac{-i \pi}{8}}\left(\left|\varphi_{1}\right\rangle-\left|\varphi_{3}\right\rangle e^{-i \pi}\right) \\
	&=e^{\frac{-i \pi}{8}}\left(\left|\varphi_{1}\right\rangle+\left|\varphi_{3}\right\rangle\right)=e^{\frac{-i \pi}{8}}\left(\sqrt{\frac{2}{L}} \sin \frac{\pi x}{L}+\sqrt{\frac{2}{L}} \sin \frac{3 \pi x}{L}\right)
	\end{aligned}$\\
	The correct option is \textbf{(d)}
\end{answer}
\begin{minipage}{\textwidth}
	\item The ground state energy of an anisotropic harmonic oscillator described by the potential $V(x, y, z)=\frac{1}{2} m \omega^{2} x^{2}+2 m \omega^{2} y^{2}+8 m \omega^{2} z^{2}$ (in units of $\hbar \omega$ ) is
	\exyear{NET DEC 2018}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\frac{5}{2}$
	\task[\textbf{B.}]$\frac{7}{2}$
	\task[\textbf{C.}]$\frac{3}{2}$
	\task[\textbf{D.}]$\frac{1}{2}$
\end{tasks}
\begin{answer}
	$V(x, y, z)=\frac{1}{2} m \omega^{2} x^{2}+\frac{1}{2} m(2 \omega)^{2} y^{2}+\frac{1}{2} m(4 \omega)^{2} z^{2}$ $\omega_{x}=\omega \quad \omega_{y}=2 \omega \quad \omega_{z}=4 \omega$\\
	$E_{n_{x}, n_{y}, n_{z}}=\left(n_{x}+\frac{1}{2}\right) \hbar \omega_{x}+\left(n_{y}+\frac{1}{2}\right) \hbar \omega_{y}+\left(n_{z}+\frac{1}{2}\right) \hbar \omega_{z}$\\
	For ground state\\
	$n_{x}=0, n_{y}=0, n_{z}=0$\\
	$=\frac{1}{2} \hbar \omega+\frac{1}{2} \hbar 2 \omega+\frac{1}{2} \hbar 4 \omega=\frac{1}{2} \hbar \omega(1+2+4)=\frac{7}{2} \hbar \omega$\\
	The correct option is \textbf{(b)}
\end{answer}
\end{enumerate}

\newpage
\begin{abox}
	Practice set 2 solutions
\end{abox}
\begin{enumerate}
	\begin{minipage}{\textwidth}
		\item Which of the following is an allowed wavefunction for a particle in a bound state? $N$ is a constant and $\alpha, \beta>0$.
		\exyear{GATE 2010}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}] $\psi=N \frac{e^{-\alpha r}}{r^{3}}$
		\task[\textbf{B.}]$\psi=N\left(1-e^{-\alpha r}\right)$
		\task[\textbf{C.}]$\psi=N e^{-\alpha x} e^{-\beta\left(x^{2}+y^{2}+z^{2}\right)}$
		\task[\textbf{D.}]$\psi= \begin{cases}\text { non - zero constant } & \text { if } r<R \\ 0 & \text { if } r>R\end{cases}$
	\end{tasks}
	\begin{answer}
		The correct option is \textbf{(c)}
	\end{answer}
	\begin{minipage}{\textwidth}
		\item A particle of mass $m$ is confined in an infinite potential well:
		$$
		V(x)= \begin{cases}0, & \text { if } 0<x<L, \\ \infty, & \text { otherwise. }\end{cases}
		$$
		It is subjected to a perturbing potential $V_{p}(x)=V_{o} \sin \left(\frac{2 \pi x}{L}\right)$ within the well. Let $E^{(1)}$ and $E^{(2)}$ be corrections to the ground state energy in the first and second order in $V_{0}$, respectively. Which of the following are true?
		\exyear{GATE 2010}
		\begin{figure}[H]
			\centering
			\includegraphics[height=4cm,width=4cm]{diagram-20210824-crop}
		\end{figure}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}] $E^{(1)}=0 ; E^{(2)}<0$
		\task[\textbf{B.}]$E^{(1)}>0 ; E^{(2)}=0$
		\task[\textbf{C.}]$E^{(1)}=0 ; E^{(2)}$ depends on the sign of $V_{0}$
		\task[\textbf{D.}]$E^{(1)}<0 ; E^{(2)}<0$
	\end{tasks}
	\begin{answer}
		$E_{1}^{1}=\frac{2}{L} \int_{0}^{L} V_{0} \sin \frac{2 \pi x}{L} d x=0 ; E_{1}^{2}=\sum_{m \neq 1} \frac{\left|\left\langle\psi_{m}\left|V_{P}\right| \psi_{1}\right\rangle\right|^{2}}{E_{1}-E_{m}} \quad \because E_{1}<E_{m} \text { so } E_{1}^{2}=-v e$\\
		The correct option is \textbf{(a)}
	\end{answer}
	\begin{minipage}{\textwidth}
		\item An electron with energy $E$ is incident from left on a potential barrier, given by
		$$
		V(x)= \begin{cases}0, & \text { for } x<0 \\ V_{0}, & \text { for } x>0\end{cases}
		$$
		as shown in the figure. For $E<V_{0}$, the space part of the wavefunction for $x>0$ is of the form
		\exyear{GATE 2011}
		\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{diagram-20210824(6)-crop}
		\end{figure}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}] $e^{a x}$
		\task[\textbf{B.}] $e^{-a x}$
		\task[\textbf{C.}] $e^{i a x}$
		\task[\textbf{D.}]$e^{-i a x}$
	\end{tasks}
	\begin{answer}
		$E<V_{0} \text {, so there is decaying wave function. }	$\\
		The correct option is \textbf{(b)}
	\end{answer}
	\begin{minipage}{\textwidth}
		\item A particle of mass $m$ is confined in a two dimensional square well potential of dimension $a$. This potential $V(x, y)$ is given by
		$$
		\begin{aligned}
		V(x, y) &=0 \text { for }-a<x<a \text { and }-a<y<a \\
		&=\infty \text { elsewhere }
		\end{aligned}
		$$
		The energy of the first excited state for this particle is given by,
		\exyear{GATE 2012}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}]$\frac{\pi^{2} \hbar^{2}}{m a^{2}}$
		\task[\textbf{B.}] $\frac{2 \pi^{2} \hbar^{2}}{m a^{2}}$
		\task[\textbf{C.}]$\frac{5 \pi^{2} \hbar^{2}}{8 m a^{2}}$
		\task[\textbf{D.}] $\frac{4 \pi^{2} \hbar^{2}}{m a^{2}}$
	\end{tasks}
	\begin{answer}
		$$E=\left(n_{x}^{2}+n_{y}^{2}\right) \frac{\pi^{2} \hbar^{2}}{2 m(2 a)^{2}}=\left(n_{x}^{2}+n_{y}^{2}\right) \frac{\pi^{2} \hbar^{2}}{8 m a^{2}}=\frac{5 \pi^{2} \hbar^{2}}{8 m a^{2}} \quad \because n_{x}=1, n_{y}=2$$
		The correct option is \textbf{(c)}
	\end{answer}
	\begin{minipage}{\textwidth}
		\item A proton is confined to a cubic box, whose sides have length $10^{-12} \mathrm{~m}$. What is the minimum kinetic energy of the proton? The mass of proton is $1.67 \times 10^{-27} \mathrm{~kg}$ and Planck's constant is $6.63 \times 10^{-34} J_{S}$.
		\exyear{GATE 2013}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}] $1.1 \times 10^{-17} \mathrm{~J}$
		\task[\textbf{B.}] $3.3 \times 10^{-17} \mathrm{~J}$
		\task[\textbf{C.}]$9.9 \times 10^{-17} \mathrm{~J}$
		\task[\textbf{D.}]$6.6 \times 10^{-17} \mathrm{~J}$
	\end{tasks}
	\begin{answer}
		$\frac{3 \pi^{2} \hbar^{2}}{2 m a^{2}}=9.9 \times 10^{-17}$\\
		The correct option is \textbf{(c)}
	\end{answer}
	\begin{minipage}{\textwidth}
		\item Consider a system of eight non-interacting, identical quantum particles of spin $-\frac{3}{2}$ in a one dimensional box of length $L$. The minimum excitation energy of the system, in units of $\frac{\pi^{2} \hbar^{2}}{2 m L^{2}}$ is
		\exyear{GATE 2015}
	\end{minipage}
	\begin{answer}
		spin $\frac{3}{2} \Rightarrow$ degeneracy $=(2 S+1)=\left(2 \times \frac{3}{2}+1\right)=4$
		$$
		\begin{gathered}
		E_{\text {ground }}=4 \times \frac{\pi^{2} \hbar^{2}}{2 m L^{2}}+4 \times \frac{4 \pi^{2} \hbar^{2}}{2 m L^{2}}=\frac{20 \pi^{2} \hbar^{2}}{2 m L^{2}} \\
		E_{\text {excited }}^{I^{t}}=4 \times \frac{\pi^{2} \hbar^{2}}{2 m L^{2}}+3 \times 4 \frac{\pi^{2} \hbar^{2}}{2 m L^{2}}+1 \times 9 \frac{\pi^{2} \hbar^{2}}{2 m L^{2}}=25 \frac{\pi^{2} \hbar^{2}}{2 m L^{2}}
		\end{gathered}
		$$
		Now minimum excitation energy $\Delta E=E_{\text {excited }}^{I^{s^{\prime}}}-E_{\text {ground }}=25 \frac{\pi^{2} \hbar^{2}}{2 m L^{2}}-20 \frac{\pi^{2} \hbar^{2}}{2 m L^{2}}=5 \frac{\pi^{2} \hbar^{2}}{2 m L^{2}}$
	\end{answer}
	\begin{minipage}{\textwidth}
		\item A two-dimensional square rigid box of side $L$ contains six non-interacting electrons at $T=0 K .$ The mass of the electron is $m .$ The ground state energy of the system of electrons, in units of $\frac{\pi^{2} \hbar^{2}}{2 m L^{2}}$ is
		\exyear{GATE 2016}
	\end{minipage}
	\begin{answer}
		$ 2 \times \frac{\left(1^{2}+1^{2}\right) \pi^{2} \hbar^{2}}{2 m L^{2}}+4 \times \frac{\left(2^{2}+1^{2}\right) \pi^{2} \hbar^{2}}{2 m L^{2}}=\frac{24 \pi^{2} \hbar^{2}}{2 m L^{2}}$
	\end{answer}
	\begin{minipage}{\textwidth}
		\item A particle of mass $m$ and energy $E$, moving in the positive $x$ direction, is incident on a step potential at $x=0$, as indicated in the figure. The height of the potential is $V_{0}$, where $V_{0}>E$. At $x=x_{0}$, where $x_{0}>0$, the probability of finding the electron is $\frac{1}{e}$ times the probability of finding it at $x=0$. If $\alpha=\sqrt{\frac{2 m\left(V_{0}-E\right)}{\hbar^{2}}}$, the value of $x_{0}$ is
		\exyear{GATE 2016}
		\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{gate 5-crop}
		\end{figure}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}] $\frac{2}{\alpha}$
		\task[\textbf{B.}] $\frac{1}{\alpha}$
		\task[\textbf{C.}]$\frac{1}{2 \alpha}$
		\task[\textbf{D.}]$\frac{1}{4 \alpha}$
	\end{tasks}
	\begin{answer}
		$\frac{1}{e}=e^{-2 \alpha x_{0}}=e^{-1}=e^{-2 \alpha x_{0}} \Rightarrow x_{0}=\frac{1}{2 \alpha}$\\
		The correct option is \textbf{(c)}
	\end{answer}
	\begin{minipage}{\textwidth}
		\item A free electron of energy $1 \mathrm{eV}$ is incident upon a one-dimensional finite potential step of height $0.75 \mathrm{eV}$. The probability of its reflection from the barrier is........... (up to two decimal places).
		\exyear{GATE 2017}
	\end{minipage}
	\begin{answer}
		$R=\left(\frac{\sqrt{E}-\sqrt{E-V_{0}}}{\sqrt{E}+\sqrt{E-V_{0}}}\right)^{2}=\left(\frac{1-\sqrt{0.25}}{1+\sqrt{0.25}}\right)^{2}=\left(\frac{1-0.5}{1+0.5}\right)^{2}=0.11$
	\end{answer}
	\begin{minipage}{\textwidth}
		\item The ground state energy of a particle of mass $m$ in an infinite potential well is $E_{0} .$ It changes to $E_{0}\left(1+\alpha \times 10^{-3}\right)$, when there is a small potential pump of height $V_{0}=\frac{\pi^{2} \hbar^{2}}{50 m L^{2}}$ and width $a=L / 100$, as shown in the figure. The value of $\alpha$ is (up to two decimal places).
		\exyear{GATE 2018}
	\end{minipage}
	\begin{answer}
		$\begin{aligned}
		&\text { Solution: } \alpha_{1}=\left(\frac{L}{2}-\frac{a}{2}\right), \alpha_{2}=\left(\frac{L}{2}+\frac{a}{2}\right), \quad a=\frac{L}{100} \\
		&\qquad \begin{aligned}
		E_{1} &=V_{0} \int_{\alpha_{1}}^{\alpha_{2}}\left(\sqrt{\frac{2}{L}}\right)^{2} \sin ^{2}\left(\frac{\pi x}{L}\right) d x \\
		&=\frac{V_{0}}{L} \int_{\alpha_{1}}^{\alpha_{2}}\left[1-\cos \frac{2 \pi x}{L}\right] d x=\frac{V_{0}}{L}\left[x-\frac{L}{2 \pi} \sin \frac{2 \pi x}{L}\right]_{\alpha_{1}}^{\alpha_{2}}
		\end{aligned}
		\end{aligned}$
		$$
		\begin{aligned}
		&=\frac{V_{0}}{L}\left[a-\frac{L}{2 \pi}\left(\sin \frac{2 \pi(L+a)}{2 L}-\sin \frac{2 \pi(L-a)}{2 L}\right)\right] \\
		&=\frac{V_{0}}{L}\left[\frac{L}{100}-\frac{L}{2 \pi}\left(\sin \left(\pi+\frac{\pi a}{L}\right)-\sin \left(\pi-\frac{\pi a}{L}\right)\right)\right] \\
		&=V_{0}\left[\frac{1}{100}+\frac{1}{2 \pi}(0.0314+0.0314)\right] \\
		&=V_{0} \times 10^{-3}(10+10)=E_{0} \times 10^{-3} \times\left(\frac{20}{25}\right) \Rightarrow \alpha E_{0} \times 10^{-3}=0.81 \times E_{0} \times 10^{-3}
		\end{aligned}
		$$
		Hence, $\alpha=0.81$
	\end{answer}
	\begin{minipage}{\textwidth}
		\item $ \text { Consider a potential barrier } V(x) \text { of the form: }$
		\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{diagram-20210825(3)-crop}
		\end{figure}
		where $V_{0}$ is a constant. For particles of energy $E<V_{0}$ incident on this barrier from the left which of the following schematic diagrams best represents the probability density $|\psi(x)|^{2}$ as a function of $x$ ?
		\exyear{GATE 2019}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}]\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{diagram-20210825(4)-crop}
		\end{figure}
		\task[\textbf{B.}]\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{diagram-20210825(5)-crop}
		\end{figure}
		\task[\textbf{C.}]\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{diagram-20210825(6)-crop}
		\end{figure}
		\task[\textbf{D.}]\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{diagram-20210825(7)-crop}
		\end{figure}
	\end{tasks}
	\begin{answer}
		The correct option is \textbf{(a)}
	\end{answer}
\end{enumerate}

QM -CSIR/chapter/eigen value solutions.tex


\section{Proposed solution}
Before going into the details of the solution, let's describe the graphs
used for the analysis. The dataset used is a set of synthetic
graph generated using the Erdos-Renyi model.
Therefore, the graph generates $ER(n, p)$ is parametric in $p$ and $n$, whereas $n$ is the number of nodes and $p$ can be interpreted formally as the probability
of attaching a node regardless the previous realizations.  On the another hand it
can be seen as the density of the graph. In particular the set used by the experiments
is composed by four different graphs: $ER(10000, 0.2), ER(10000, 0.4), ER(10000, 0.8), ER(100000, 0.002)$.
The number of nodes was chosen as a tradeoff on a reasonable size, to evaluate the
goodness of the algorithm and the cost in terms of memory and space required, since
the space and time complexity are $O(n^2)$ which are not negligible\footnote{for low density a better algorithm were used provided in the networkx library, that were proposed by Vladimir Batagelj and Ulrik Brandes\cite{PhysRevE.71.036113}}.
\subsection{Problem Analysis}
\subsubsection{Completion time}
\label{sec:comp-time}
The sequential algorithm described in section .\ref{sub:seq-version} is an
iterative algorithm with an initialization phase where the $i$-th iteration visits level $i$. The
processing of the $i$-th level requires to go through each node
$v : d(v, s) = i$ and visit its neighborhood.
The time needed to process a node is independent of the
level at which it is visited as it only depends on the size
of its neighborhood. Hence, let the time taken by the node $v$ be denoted
with $T_v$. Then the time taken by the level $i$ is $T_{L_i} = \sum_{v : d(v, s) = i}T_v$.
Note that different levels require different times depending on the topology of the graph. In particular, the $i$-th level is influenced by the number of nodes at distance $i$ from the root
and by the size of the neighborhood of these nodes. The completion time of the $i$-th
iteration is given by $T_i = T_{L_i} + T_{swap} + T_{clear}$, i.e. the time taken by the level $i$ plus two additional factor: the former, $T_{swap}$, is the time taken to swap
$L$ and $\hat{L}$, which is the same for all the iterations since it is a swap of two pointers; the latter, $T_{clear}$, is
time taken by the cleaning of the new $\hat{L}$ so it depends on number of elements.
Let $G$ be the graph induced by the root node then the completion time for the sequential algorithm is:
$$
T_{seq}(G) = T_{init} + \sum^{\bar{d}(G)}_{i=1} T_i \leq  T_{init} + (\bar{d}(G) \cdot max_i \ T_i)
$$

Where $T_{init}$ is the time taken by the
initialization phase, therefore it takes into account: the initialization of the
vector of visits, the initialization of $F_i$ and $F_{i+1}$, the analysis
of the root node, the insertion of its neighbors in the frontier and
the creation of the vector of visits.

There mainly are three kind of possible parallelism:
\begin{enumerate}
    \item The frontier-level: which divides
    the work required by the computation of the single frontier among the workers;
    \item The neighborhood-level: which divides the work required by the single
    node computation, hence the visit of its neighbors among the workers;
    \item A combination of both 1 and 2.
\end{enumerate}
The analysis is focused on the first approach since the second
one is very sensitive to the local topology of a single node.
Moreover,
in real scenarios $\bar{k}$ is a very small value, which causes small units of work.
In addition, still considering real scenarios, $\bar{d}$ is a very small number
 and $n$ is a big value,
whereby the frontiers will contain a very large number of nodes.
\\
\\
Ideally, considering $nw$ workers one could think that the analysis of a single
frontier $F_i$ can be divided equally among the workers, obtaining a parallel
completion time for a single iteration $T^{par}_{T_i} \approx \frac{T_{L_i}}{nw} + T_{swap} + T_{clear}$.
However, this does not take into account that the analysis of
$L_{i+1}$ can start only when the analysis of $L_{i}$ is terminated,
which causes an additional time factor to synchronize all the workers.
The parallel completion time for a single iteration becomes then:
$T^{par}_{T_i} \approx \frac{T_{L_i}}{nw} + T_{sync} + T_{swap} + T_{clear}$.
Hence,
there are three tasks that can not be done concurrently,
as they are in critical section:
\begin{enumerate}
    \item Checking if a node has already been visited and, in the case it is
    necessary, updating the vector of visits;
    \item Adding the nodes found to $F_{i+1}$;
    \item Update the number of occurrences.
\end{enumerate}
Lets denote the additional factors listed above with:
 $T_{visited}$ due to 1, $T_{merge}$ due to 2 and $T_{update}$ due to 3.
 The first two are required at each iteration, the last one can be
 added directly once to $T_{par}$ since each workers
 can keep a counter of the found occurrences and at the end sum it.
 The completion time of single iteration in parallel can be better
 approximated by: $T^{par}_{T_i} \approx \frac{T_{L_i}}{nw}
 + T_{sync}
  + T_{visited} + T_{merge} + T_{swap} + T_{clear}$, resulting in the following
  parallel completion time:
  $$
  T_{par}(G, nw) \approx T_{init} + T_{update} + \sum^{\bar{d}(G)}_{i=1} T^{par}_{T_i}
  $$

Of course the above approximation considers the workload as perfectly balanced,
that in principle is not easy to achieve, since it is not only
a matter of the number of nodes (i.e. it is not enough to assign equal-size
subset of the frontier to each worker). On the other hand, the completion time $T_{S \subseteq L_i}$
is influenced by the completion time of each node $v \in S$, namely $T_v$. A necessary condition for the best scenario is that at iteration $i$, not necessary the best,
is the one where the number of nodes at level $L_{i+1}$ is equally
distributed among the neighbors of nodes contained in $L_i$.
\subsubsection{Sequential analysis}
In order to design a proper parallel solution,
the times of the various operations and phases that the sequential
 version requires were measured, to identify if there are any bottlenecks.
The expected ones are the memory accesses, more in detail:
\begin{itemize}
    \item The time to access an element of the graph contained in the frontier,
    $T_{read(v[i]\in G)}$, that is
    random and unpredictable, since it only depends on the topology of the graph. One could
    imagine that a possible improvement is to sort the frontier, this probably causes
    an overhead that exceeds the gain, however, more
    considerations require an additional
    analysis that this work does not take into account.
    \item The same reasoning on accessing graph elements
     also applies to the time to access the vector of visits, $T_{visited[v]}$, where, however,
    the access order is influenced by the organization of the neighborhood
    of the nodes in $L_i$.
\end{itemize}
The accesses to the current frontier $F_i$ and the neighborhood $\mathcal{N}(v)$ are efficient, since they are a scan from the
first to the last elements
of the vector, thus optimal in number of I/Os. Some of
 the measures to support this are presented in Table \ref{tab:seq-meas}\footnote{The results are obtained as an average over 10 runs}. Note the time taken by $T_{read(v \in \mathcal{N}(v))}$, $T_{read(visited[v])}$, $T_{write(visited[v])}$ were not measured since on average they took at most $\approx 5ns$.

\begin{table}[h]
    \begin{center}
        \begin{tabular}{|| l | c | c | c | c |}
            \hline
            & $ER(10000, 0.2)$ & $ER(10000, 0.4)$ & $ER(10000, 0.8)$ & $ER(100000, 0.002)$ \\ \hline \hline
             $T_{clear}$  & $\approx 164ns$& $\approx 176ns$& $\approx 167ns$ & $\approx 155ns$
             \\ \hline
             $T_{swap}$  & $\approx 154ns$& $\approx 168ns$& $\approx 163ns$ & $\approx 140ns$
             \\ \hline
             $T_{read(i \in F_i)}$  & $\approx 160ns$& $\approx 164ns$ & $\approx 166ns$ & $\approx 150ns$
             \\ \hline
             $T_{read(v[i] \in G)}$  & $\approx 1874ns$& $\approx 2837ns$& $\approx 4233ns$ & $\approx 771ns$
             \\ \hline
             \hline
        \end{tabular}
    \end{center}
    \caption{Sequential  measurements}
    \label{tab:seq-meas}
\end{table}
As can be observed, as the density increases the time taken by the I/O increases. This happens because the bigger is the vector to read the higher is the number of pages it takes in the upper-level cache, i.e. higher is the number of access to those pages for a scan. The sequential completion
times measured is shown in Table \ref{tab:seq-results} as average on 10 runs.
\begin{table}[!htb]
    \begin{center}
        \begin{tabular}{|| l | c | c | c | c ||}
            \hline
              & $ER(10000, 0.2)$ & $ER(10000, 0.4)$ & $ER(10000, 0.8)$ & $ER(100000, 0.002)$ \\ \hline \hline
             $\bar{T}_{seq}$ & $65ms$ & $126ms$ & $244ms$ & $127ms$ \\ \hline
             $\sigma(T_{seq})$ & $1ms$ & $422\mu s$ & $548\mu s$ & $399 \mu s$ \\ \hline
            \hline
        \end{tabular}
    \end{center}

    \caption{Sequential results}
    \label{tab:seq-results}
\end{table}

Considering the results obtained, different scenarios can be explored as interesting for the analysis of the parallel version: for instance a graph having a small number of nodes and fully connected generates a big frontier w.r.t. the number of nodes, although the time needed to process the frontier is too small compared to the overheads of thread creation. Similar scenario happens for processing graph with large number of nodes and really high value of $\bar{d}$ where the frontiers generated in average will be small, thus the overhead in synchronization is expected to exceeds the gain.

\subsection{Solution Description}
In order to prevent the load balancing issues
due to the variance of $k_{out}$, the proposed solution
uses a dynamic scheduling, which can be easily implemented
in a Master-Worker fashion. In the
present case, an auto-scheduling policy is implemented:  all the workers have access to the frontier
and obtain a new task of work using a shared
data structure. Retrieving a task has the cost of an
atomic fetch+add on an integer. Here a task correspond to a chunk, i.e.
 start and end indexes of $F_i$. Note that the size of the chunk $cw$
 is a parameter of the solution. Thanks to this shared data structure
 the master does not need to
 prepare the tasks for the workers. Instead it performs the same work as
 the worker with the difference that is also responsible for
 the swap and the cleaning of the new $F_{i+1}$.
\subsubsection{Local next frontier}
The trivial way to produce $F_{i+1}$ is to share an array among the workers and insert each new element in mutual exclusion, each new element found to the array. This solution is as easy as inefficient, since it produces different problems. First of all, false sharing occurs more frequently and the overhead due the atomicity is really high. In the proposed solution each worker has a local version of $F_i$, which contains only the nodes found by the worker at the end of the visit. The worker atomically adds all the elements of the local frontier to the global one. This reduces false sharing and decreases the overhead, since the processing is much faster.
\subsubsection{Expected performance}
\label{sec:how-it-perform}
The solution mentioned above mitigates, thanks to dynamic scheduling,
the problems due to the variance of $k_{out}$.
However, it does not eliminate the
problem in its entirely, because it remains
a parallelism only at the frontier level.
The worst case is when an huge hub occurs:
since the worker who has to process it,
will take much longer.
\\
\\
To discuss with more specificity the expected performance, it is necessary to evaluate it with respect to the topology and statistical properties of the input graph:
\begin{itemize}
    \item In general, the lower the value of $\bar{d}$ is, the higher the expected gain of the parallel version, because the overhead due to the level-synchronization will be the smaller and, the number of nodes in the frontiers will be higher, since $n$ should be divided among few frontiers.
    \item In addition, fixing $\bar{d}$ and enlarging $n$ increase the probably that some levels will contain a number of nodes high enough to achieve a good speedup increase.
    \item In addition to the previous property, if the $k_{out}$ is considered, it is possible to better estimate how the frontiers will be made. Namely, as the variance in $k_{out}$ is low, the confidence in the fact the nodes between the frontiers will be better distributed increases. This is not true in general, because actually it depends on which node are present in the different neighborhoods: the fewer are the duplicates among the nodes neighborhood the better distributed the nodes will be across the frontiers.
    \item Instead, the density itself does not say too much about about the distribution of the nodes among the levels, but in some specific case it can helps. For instance the synthetic dataset generated through the Erdos-Renyi increasing the value of $p$, i.e. the density, the algorithm tends to generate a small number of levels due to independency in drawing random edges: for small value of p the probability of finding node in higher level increases.

\end{itemize}

For example, in real graphs it is known that $n$ is a big number and $\bar{d}$ is really small number. Even though in those graphs hubs occurs often and this generates an high variance in $k_{out}$ and increments the load balancing issue.

\subsubsection{The influence of chunksize}
\label{sec:chunksize}
As mentioned before the proposed solution is parametric in $cw$, namely the chunksize, this measure refers to the number of nodes of $F_i$ that each workers will consider as a single task. The pop operation is as written in the previous sections with an efficient data structure that implements it at the cost of an atomic \texttt{fetch-add}. The choice of $cw$ value should be a trade-off between the advantage of having small tasking and the time taken by the pop operation. Small chunk size guarantees a fairer distribution among the workers, as cons it increments the number of pops, hence the overhead. Moreover if the expected variance in $k_{out}$ is high the advantage of having small chunk increase.
\subsection{Analysis and measurements}
All the experiments have been performed fixing $cw$ as 32, this number has been chosed according to what is written in section \ref{sec:chunksize} observing the overhead better described in section \ref{sec:overhead}.
\subsubsection{Build and execution}
The source code have been complied with \texttt{g++17} using the commands:
\begin{itemize}
    \item \texttt{g++ -std=c++17  ./src/bfs-sequential.cpp -o ./build/bfs-sequential -O3} \\ \texttt{-Wall -I include/ -ftree-vectorize }
    \item \texttt{g++ -std=c++17  ./src/bfs-pthread.cpp -o ./build/bfs-pthread -O3 -Wall} \\ \texttt{-pthread -I include/ -ftree-vectorize }
    \item \texttt{g++ -std=c++17  ./src/bfs-fastflow.cpp -o ./build/bfs-fastflow -O3 -Wall}\\ \texttt{-pthread -I include/ -ftree-vectorize }
\end{itemize}
The execution of the sequential code requires 3 positional arguments:
\begin{enumerate}
    \item \texttt{inputFile} : string, the path to the graph
    \item \texttt{startingNodeId} : integer, the id of the from which the bfs will start
    \item \texttt{labelTarget} : integer, label whose occurrences are to be counted
\end{enumerate}
The parallel versions requires 2 additional positional arguments:
\begin{enumerate}
    \item \texttt{nw} : integer, the number of workers to use
    \item \texttt{k}: integer, chunk size
\end{enumerate}
All the version print by default the completion time.
\subsubsection{pthread implementation}
In the \texttt{pthread} implementation, the main thread acts as master and so,
it executes the initialization phase, creates
the threads and then starts its work as described. As soon as the visit of the graph
is completed the master collects the result and prints it together with the completion time. The synchronization at the end of each level, among the workers and the master,
is implemented using an active wait barrier implemented through mutex and conditional variable.
\\
To handle the critical section in the check and update of the visited nodes a vector of atomic booleans was used as the vector of visits. The check and update was accomplished via a \texttt{compare and exchange} instruction, which was done at the node level in the frontier. Meanwhile at the neighborhood level (the one implemented in the sequential version) to avoid inserting duplicates a non-atomic vector of boolean was used, namely the \textit{vector of insertion}. Since the vector of insertion is composed by non-atomic boolean this does not guarantee that duplicates will not be added, but the checks at node level guarantee that the node will not be visited more than once. This was made because the vector of insertion generates few duplicates in the frontier the checks at node level will have often the same result, hence this helps to exploiting better the pipeline of the new processor through the branch prediction. The probability of adding duplicates grows as the $C(v)$ of the nodes $v$ in the previous frontier increase. A downside of the introduction of this vector is that it increases the probability that false sharing occurs.
\subsubsection{Overhead analysis}
\label{sec:overhead}
To better evaluate the performance as the number of threads increases, it is useful to observe the trend that the overheads show. With this type of analysis it is also possible to make a choice on the effective number of threads to use in case the nature (e.g. using the approximation of the completion time provided in section \ref{sec:comp-time}), intended as topology and statistical properties, of the graph is known. Two three classes of overhead can be identified:
\begin{enumerate}
    \item the first class grows linearly in the number of thread, which contains the creation of the threads $\bar{T}_{thd}$.
    \item the second class grows linearly in the $\bar{d}$ (the number of frontier): the synchronization time and the waiting for the lock, $T_{merge}$.
    \item the third class instead depends on the size of $F_i$, which contains: the overhead to checks and update atomically, $T_{visit}$, the vector of visits and the one needed to pop a new task, $T_{pop}$.
\end{enumerate}

\begin{table}[!htb]
    \begin{center}
        \begin{tabular}{|| l | c ||}
            \hline
             $T_{thd}$ & $130 \mu s$  \\ \hline
             $T_{visited}$ & $280ns$ \\ \hline
             $T_{pop}$ & $480ns$ \\ \hline
            \hline
        \end{tabular}
    \end{center}

    \label{tab:static-overhead-results}
    \caption{Static overhead measurements}
\end{table}

Even though the synchronization belongs to the second class, its value depends on the number of threads as shown in figure \ref{fig:level-sync}. The same applies to $T_{merge}$, the lock waiting, for which it also depends on the size of the local frontiers. Its trend is non-trivial as shown in figure \ref{fig:lock}: if the number of threads grows the overhead increases, in any case if the generated local frontiers are small as in the case of Erdos-Renyi with low density the growth decreases.

\begin{figure}[!htb]
    \centering
    \begin{minipage}{0.48\textwidth}
        \includegraphics[width=\textwidth]{plots/level-sync.pdf}
        \caption{Level-synchronization overheads}
        \label{fig:level-sync}
    \end{minipage}
    \begin{minipage}{0.48\textwidth}
        \includegraphics[width=\textwidth]{plots/lock-wait.pdf}
        \caption{Lock overheads}
        \label{fig:lock}
    \end{minipage}
\end{figure}

\subsubsection{Fastflow implementation}
The \texttt{FastFlow} version implements the same mechanisms as in the \texttt{pthread} version, except for the synchronization mechanisms, since it uses the Master-Worker with feedback queue. The master after having performed the initialization phase and indicated to the threads which is the pointer to the queue where to write the nodes they find, performs the same work of the workers and orchestrates the level-synchronization. The level-synchronization is implemented through the task queues and feedback queue, that were used to notify the master with the number of occurrences found in the level. Whenever the master receives all the feedbacks from all the workers, it swaps the pointers of $F_i$ and $F_{i+1}$ and informs the threads to work on the next level by indicating the new pointers pushing them on the task queues of each worker. Initially to start the worker the master pushes the initial pointers on the task queues.
\subsubsection{Results}
The following section reports all the results obtained by both the \texttt{pthread} and the \texttt{FastFlow} implementation. All the synthetic dataset have been exploited in order to show empirically what has been discussed until now. The results have been shown plotting the performances in terms of completion time and speedup reached. The results obtained by the
\texttt{FastFlow} version are worse in general than the \texttt{pthread} implementation. The gap shows an increasing trend as the number of threads grows. This gap is probably due to the fact that the \texttt{pthread} version can be more specific and implement the bare minimum in a more efficient way for the specific problem, especially in the synchronization mechanisms.
\\
As discussed earlier as the number of nodes in the frontiers increases, an increase in speedup is expected. In the Erdos-Renyi model fixed the number of nodes $n$, as $p$ increases we obtain graphs with a smaller number of frontiers, i.e. $\bar{d}$ smaller, and generating very large frontiers. This is well highlighted comparing the different plots in Figures
\ref{fig:perf_02}, \ref{fig:perf_04} and \ref{fig:perf_08}. Interestingly, as the number of nodes in the frontiers increases and, the number of frontiers decrease (because $n$ is fixed), the gap between the performances of the two versions shows a decreasing trend.

\begin{figure}[!htb]
    \centering
    \begin{minipage}{0.48\textwidth}
        \includegraphics[width=\textwidth]{plots/fastflow_performance_02_time.pdf}
    \end{minipage}
    \begin{minipage}{0.48\textwidth}
        \includegraphics[width=\textwidth]{plots/fastflow_speedup_02_time.pdf}
    \end{minipage}
    \caption{Performance and speedup using ER(10000, 0.2)}
    \label{fig:perf_02}
    \begin{minipage}{1\textwidth}
    \end{minipage}
    \begin{minipage}{0.48\textwidth}
        \includegraphics[width=\textwidth]{plots/fastflow_performance_04_time.pdf}
    \end{minipage}
    \begin{minipage}{0.48\textwidth}
        \includegraphics[width=\textwidth]{plots/fastflow_speedup_04_time.pdf}
    \end{minipage}
    \caption{Performance and speedup using ER(10000, 0.4)}
    \label{fig:perf_04}
    \begin{minipage}{1\textwidth}
    \end{minipage}
    \centering
    \begin{minipage}{0.48\textwidth}
        \includegraphics[width=\textwidth]{plots/fastflow_performance_08_time.pdf}
    \end{minipage}
    \begin{minipage}{0.48\textwidth}
        \includegraphics[width=\textwidth]{plots/fastflow_speedup_08_time.pdf}
    \end{minipage}
    \caption{Performance and speedup using ER(10000, 0.8)}
    \label{fig:perf_08}
\end{figure}

Moreover, as mentioned in section \ref{sec:how-it-perform} it is not only a matter of density, instead different factors must be taken into account, such as the number of nodes and $\bar{d}$. This is clear looking at the result obtained in Figure \ref{fig:perf_08} and \ref{fig:real-case}.

\begin{figure}[h]
    \centering
    \begin{minipage}{0.48\textwidth}
        \includegraphics[width=\textwidth]{plots/fastflow_performance_002_time.pdf}
    \end{minipage}
    \begin{minipage}{0.48\textwidth}
        \includegraphics[width=\textwidth]{plots/fastflow_speedup_002_time.pdf}
    \end{minipage}
    \begin{minipage}{1\textwidth}
    \caption{Performance and speedup using ER(100000, 0.002)}
    \label{fig:perf_002}
\end{minipage}
\end{figure}
\subsection{A real use case}
To further evaluate the solutions, a real case is shown, using as
 input a real network of $149279$ nodes and $\approx 10530774$ edges, hence $0.00095$ as density. The
 network was built as a network
  of interactions happened in the
  month of october
 2020 in the /r/politics subreddit. The nodes represent users who have
 commented or written posts, while the edges from user
 A to user B indicates that the former has posted a comment to the latter.
 The results obtained are reported in terms of performance and speedup in
 the Figure  \ref{fig:real-case}.

\begin{figure}[h]
\centering
\begin{minipage}{0.48\textwidth}
    \includegraphics[width=\textwidth]{plots/fastflow_performance_real_time.pdf}
\end{minipage}
\begin{minipage}{0.48\textwidth}
    \includegraphics[width=\textwidth]{plots/fastflow_speedup_real_time.pdf}
\end{minipage}
\caption{Real use case speedup}
\label{fig:real-case}
\end{figure}