\section{Joint inversion}\label{sec:joint}
The term joint inversion denotes the simultaneous inversion of a number of different data types.
We can classify different types according to the relation of the associated parameters:
\begin{enumerate}
	\item Identical parameters is historical the classical joint inversion, e.g. both DC and EM aim at $\rho$.
	\item Parameters are indirectly connected by petrophysical relations, e.g. ERT and GPR both aiming at water content.
	\item Independent parameters. In this case only the structures can be linked to each other. For simple models this can involve the inversion of the geometry. For fixed models structural information is exchanged\footnote{Note that this type is formally a structurally coupled cooperative inversion.}.
\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Classical joint inversion of DC and EM soundings}\label{sec:jointdcem}
File \file{doc/tutorial/code/joint/dcem1dinv.cpp}\\
First, let us consider to jointly invert different electromagnetic methods, e.g. direct current (DC) and Frequency Domain Electromagnetic (FDEM).
For the latter we assume a two-coil system in horizontal coplanar model with 10 frequencies between 110\,Hz and 56\,kHz.
Whereas DC resistivity yields apparent resistivities, the FDEM data are expressed as ratio between secondary and primary field in per cent.

The two ready methods \lstinline|DC1dModelling| and \lstinline|FDEM1dModelling| are very easily combined since they use the same block model.
In the response function the two vectors are combined.
We create a new modelling class that derives from the base modelling class\footnote{In order to use the classes, \file{dc1dmodelling.h} and \file{em1dmodelling.h} have to be included.} and has two members of the individual classes, which must be initialized in the constructor.
Alternatively we could derive from one of the two classes and use only a member of the other.

\begin{lstlisting}[language=C++,morekeywords={RVector,RMatrix}]
class DCEM1dModelling : public ModellingBase {
public:
    DCEM1dModelling( size_t nlay, RVector & ab2, RVector & mn2,
                     RVector & freq, double coilspacing, bool verbose ) :
    	ModellingBase( verbose ), // base constructor
    	fDC_( nlay, ab2, mn2, verbose ), // FDEM constructor
    	fEM_( nlay, freq, coilspacing, verbose ) { // DC constructor
          setMesh( createMesh1DBlock( nlay ) ); // create mesh
    }
    RVector response( const RVector & model ){ // paste together responses
        return cat( fDC_.response( model ), fEM_.response( model ) );
    }
protected:
    DC1dModelling fDC_;
    FDEM1dModelling fEM_;
};
\end{lstlisting}

In the response function both response functions are called and combined using the cat command.
We set the usual transformation (log for apparent resistivity and logLU for the resistivity) and inversion (Marquardt scheme) options as above.
In case of identical responses (e.g. apparent resistivities) this would be the whole thing.
Here we have to care about the different data types (cf. section \ref{sec:mt1d}), i.e. always positive, log-distributed $\rho_a$ from DC and possibly negative, linearly distributed, relative magnetic fields.
The transformations are again combined using \lstinline|CumulativeTrans|
\begin{lstlisting}[language=C++,morekeywords={RVector,RMatrix}]
    RTransLog transRhoa;
    RTrans transEM;
    CumulativeTrans< RVector > transData;
    transData.push_back( transRhoa, ab2.size() );
    transData.push_back( transEM, freq.size() * 2 );
\end{lstlisting}

In the code we create a synthetic model \lstinline|synthModel|, calculate the forward response and noisify it by given noise levels.

\begin{lstlisting}[language=C++,morekeywords={RVector,RMatrix}]
    /*! compute synthetic model (created before) by calling f */
    RVector synthData( f( synthModel ) );
    /*! error models: relative percentage for DC, absolute for EM */
    RVector errorDC = synthData( 0, ab2.size() ) * errDC / 100.0;
    RVector errorEM( freq.size() * 2, errEM );
    RVector errorAbs( cat( errorDC, errorEM ) );
    /*! noisify synthetic data using the determined error model */
    RVector rand( synthData.size() );
    randn( rand );
    synthData = synthData + rand * errorAbs;
\end{lstlisting}

The inversion is converging to a $\chi^2$ value of about 1, i.e. we fit the data within error bounds.
Finally a resolution analysis is done to determine how well the individual parameters (2 thickness and 3 resistivitiy values) are determined.
We can compare it with single inversions by drastically increasing the error level for one of the methods by a factor of 10.
Table \ref{tab:dcemresolution} shows the resulting diagonal values of the resolution matrix for a three-layer model.
The first layer is well resolved in all variants except the first layer resistivity for EM.
Considering the values for the other resistivities we can clearly see that EM is detecting the good conductor and DC describes the resistor as expected from the theory.

\begin{table}[h]%
\centering
\begin{tabular}{r|ccccc}
Method & $d_1$=20\,m & $d_2$=20\,m & $\rho_1$=200\,$\Omega$m & $\rho_2$=10\,$\Omega$m & $\rho_3$=50\,$\Omega$m \\ \hline
Joint inversion: & 0.98 & 0.46 & 0.98 & 0.67 & 0.57 \\
EM dominated:    & 0.97 & 0.36 & 0.71 & 0.66 & 0.20 \\
DC dominated:    & 0.96 & 0.21 & 0.97 & 0.32 & 0.62
\end{tabular}
\caption{Resolution measures for Joint inversion and quasi-single inversions using an error model increased by a factor of 10.}\label{tab:dcemresolution}
\end{table}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Block joint inversion of DC/MRS data}\label{sec:blockjoint}
%File \file{doc/tutorial/code/joint/dc\_mt\_block1d.cpp}\\
If the underlying parameters of the jointed inversion are independent, a combination can only be achieved via the geometry.
For the case of a block 1d discretization both methods are affected by the layer thicknesses.
%We use the same methods as before \sperre

Similar to the above example, we create a combined modelling class that is derived from one method, in this case \lstinline|MRS1DBlockModelling|.
This one is a block model (water content and thickness) variant of the magnetic resonance sounding (MRS) modelling \lstinline|MRSModelling|.
The class has a DC resistivity forward modelling and holds the number of layers \lstinline|nlay|.

The model is created in the constructor using \lstinline|createMesh1DBlock( nlay, 2 )| that is able to hold, additionally to the thickness (region 0), multiple properties, here water content (region 1) and resistivity (region 2).
From the model vector the thickness, water content (or their combination) and resistivity has to be extracted and the result of the two forward calls are combined using the cat command.
The Jacobian is by default brute force, which is quite cheap for block models.

\begin{lstlisting}[language=C++,morekeywords={RVector,RMatrix}]
class DC_MRS_BlockModelling : public MRS1dBlockModelling{
public:
    DC_MRS_BlockModelling( size_t nlay, DataContainer & data, RMatrix & KR,
                           RMatrix & KI, RVector & zvec, bool verbose ) :
        MRS1dBlockModelling( nlay, KR, KI, zvec, verbose ), nl_( nlay ) {
            setMesh( createMesh1DBlock( nlay, 2 ) ); //two-properties
            Mesh mymesh = createMesh1DBlock( nlay ); //single block mesh
            fDC_ = new DC1dModelling( mymesh, data, nlay, verbose );
        }
    virtual ~DC_MRS_BlockModelling( ){ delete fDC_; }

    RVector response( const RVector & model ){
        //! extract resistivity, watercontent & thickness from model vec
        RVector thk( model, 0 , nl_ - 1 );
        RVector wcthk( model, 0 , nl_ * 2 - 1 );
        RVector res( model, nl_ * 2 - 1 , nl_ * 3 - 1 );
        return cat( MRS1dBlockModelling::response( wcthk ),
                    fDC_->rhoa( res, thk ) );
    }
protected:
    DC1dModelling *fDC_;
    int nl_;
};
\end{lstlisting}

In order to use the class, we have to build a cumulative data transform as in subsection \ref{sec:mt1d}.
Model transformations are logarithmic to ensure positive values, additionally an upper bound of 0.4 is defined for the water content.
\begin{lstlisting}[language=C++,morekeywords={RVector,RMatrix}]
    RTrans transVolt;    // linear voltages
    RTransLog transRhoa; // logarithmic apparent resistivities
    CumulativeTrans< RVector > transData;
    transData.push_back( transVolt, errorMRS.size() );
    transData.push_back( transRhoa, dataDC.size() );
    RTransLog transRes;
    RTransLogLU transWC(0.0, 0.4);
    RTransLog transThk;
\end{lstlisting}

In order to achieve a Marquardt inversion scheme, the constraint type is set to zero for all regions:
\begin{lstlisting}
    f.regionManager().setConstraintType( 0 );
\end{lstlisting}
Appropriately, the transformations and starting values are set.
\begin{lstlisting}
    f.region( 0 )->setTransModel( transThk );
    f.region( 0 )->setStartValue( 5.1 );
    f.region( 1 )->setTransModel( transWC );
    f.region( 1 )->setStartValue( 0.1 );
    f.region( 2 )->setTransModel( transRes );
    f.region( 2 )->setStartValue( median( dataDC.rhoa() ) );
\end{lstlisting}

We use a synthetic model of three layers representing a vadoze zone ($\rho=500\Omega$m, 0.1\% water, 4m thick), a saturated zone ($\rho=100\Omega$m, 40\% water content, 10m thick) and a bedrock ($\rho=2000\Omega$m, no water).
3\,\% and 20\,nV Gaussian noise are added to the DC and MRS data, respectively.
Figure~\ref{fig:blockjoint} shows the result of the combined inversion, which is very close to the synthetic model due to the improved information content from two models.

\begin{figure}[htb]
\centering\includegraphics[width=0.9\textwidth]{dc_mrs_blockjoint}
\caption{Joint inversion result of block-coupled DC resistivity (left) and MRS (right) sounding.}\label{fig:blockjoint}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Structurally coupled cooperative inversion of DC and MRS soundings}\label{sec:structjoint}
File \file{doc/tutorial/code/joint/dc\_mrs\_joint1d.cpp}\\
In many cases it is not clear whether the model boundaries observed by different methods are identical or how many of them are existing.
Nevertheless we expect a similarity of the structure, i.e. the gradients.
On smooth model discretizations of any dimension the exchange of geometrical information can be achieved using the constraint control function \citep{guerue06nearsurface}.
Main idea is to decrease the weight for the roughness operator of one method depending on the partial derivative of the other.
A large roughness as a possible interface should enable the interface on the other side by a low weight.
There are different possible functions for doing so.
Originally, a iteratively re-weighted least squares scheme was proposed that incorporates the whole distribution.
Here we use a simple function
\begin{equation}
    w_c(r) = \frac{a}{|r|+a}+a
\end{equation}
where $w_c$ is the weight, $r$ is the roughness value and $a$ is a small quantity.
For $r\rightarrow 0$ the weight $w_c=1+a$ lies slightly above 1, for $r\rightarrow\infty$ it becomes $a$.

In this case we apply it to DC resistivity and MRS sounding for a smooth 1d model.
The latter operator is linear and thus realized by a simple matrix vector multiplication of the kernel function and the water content vector.
We initialise the two independent inversions and run one iteration step each.
In the iteration loop we calculate the function of one roughness and set it as constraint weight for the other before running another inversion step.
\begin{lstlisting}
    invMRS.setMaxIter( 1 );
    invDC.setMaxIter( 1 );
    invMRS.run(); //! init and run 1 step
    invDC.run(); //! init and run 1 step
    double a = 0.1;
    RVector cWeight( nlay - 1 );
    for ( int iter = 1; iter < maxIter; iter++ ) {
        cWeight = a / ( abs( invDC.roughness() ) + a ) + a;
        invMRS.setCWeight( cWeight );
        cWeight = a / ( abs( invMRS.roughness() ) + a ) + a;
        invDC.setCWeight( cWeight );
        invDC.oneStep();
        invMRS.oneStep();
    }
\end{lstlisting}

Figure \ref{fig:dcmrsstruct} shows the inversion result for the above mentioned three-layer case.
Without coupling (a) the transitions between the layers are quite smooth.
Much more significant jumps in both parameters occur when structural coupling is applied (b) and make the interpretation of both layer thickness and representative parameters less ambiguous.

\begin{figure}[htb]
\includegraphics[width=0.5\textwidth]{DC-MRS-uncoupled}
\hfill
\includegraphics[width=0.5\textwidth]{DC-MRS-coupled}\\
\vskip -2ex
a\hfill b%\\[1ex]
\caption{Synthetic model (red) and inversion results (blue) for DC (left) and MRS (right) 1D inversion without (a) and with (b) structural coupling}\label{fig:dcmrsstruct}
\end{figure}

Of course the coupling does not have to affect the whole model.
The constraint weight vector can as well be set for an individual region such as the aquifer.
See inversion templates on how to do structural coupled inversion more easily and flexibly.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Petrophysical joint inversion}\label{sec:petrojoint}
Target: water content in a soil column using GPR (CRIM equation) and DC (Archie equation)

\sperre{TO BE IMPLEMENTED}

\begin{figure}[htb]
\includegraphics[width=0.5\textwidth]{petro1}
\hfill
\includegraphics[width=0.5\textwidth]{petroji}\\
\vskip -8ex
a\hfill b%\\[1ex]
\caption{Scheme for separate inversion (a) and petrophysical joint inversion (b) of GPR and ERT data to obtain an image of porosity or water content}\label{fig:petroji}
\end{figure}


\chapter{Proof of \texorpdfstring{\cref*{thm:aggregate-queries:sec}}{Theorem~\ref{thm:aggregate-queries:sec}}}%
\label{app:aggregate-queries}

We start by proving the security of the privacy-preserving authentication protocols on multiset operations. Note that the proofs of $sub(\cdot, \cdot)$ and $empty(\cdot)$ are similar to those on set operations in~\cite{10.1007/978-3-642-22792-9_6} and are hence omitted.

\section{Security of \texorpdfstring{$sum(\cdot)$}{sum(\textcdot)} Operation}

\begin{lemma}[Security of $sum(\cdot)$ operation]\label{lem:aggregate-queries:sum}
  Under the bilinear $q$-strong Diffie-Hellman assumption, if the accumulative value returned by $sum(\{X_1,\dots,X_n\})$ passes the client's verification, then the probability of $sum(\{X_1,\dots,X_n\}) \neq acc(\uplus_{i=1}^n X_i)$ is negligible for any PPT adversary.
\end{lemma}

\begin{proof}
  First, we prove that this lemma holds for $n=2$ by contradiction. Support there is a PPT algorithm that computs such a multiset $S = \{y_1, \dots, y_{\ell}\}$, whereas $y_j \notin X_1 \uplus X_2$ for some $1 \le j \le \ell$. This means that
  \begin{align*}
    e(acc(X_1), acc(X_2)) &= e(g^{\prod_{y \in S} (y+s)}, g) \Rightarrow \\
    {e(g, g)}^{\prod_{x \in X_1 \uplus X_2} (x+s) } &= {e(g, g)}^{\prod_{y \in S} (y+s)}
  \end{align*}
  Note that $(y_j + s)  \nmid \prod_{x \in X_1 \uplus X_2} (x+s)$. Therefore, there exists a polynomial $Q(s)$ (computable in polynomial time) of degree $n-1$ and constant $\lambda \neq 0$, such that $\prod_{x \in X_1 \uplus X_2} (x+s) = Q(s)(y_j + s) + \lambda$. Thus, we have
  \begin{align*}
& {e(g, g)}^{(y_j + s)\prod_{1 \le i \neq j \le \ell} (y_i+s)} = {e(g,g)}^{Q(s)(y_j + s) + \lambda} \Rightarrow \\
& {e(g, g)}^{1/(y_j + s)} = {\left[ {e(g,g)}^{\prod_{1 \le i \neq j \le \ell} (y_i+s)} {e(g,g)}^{-Q(s)} \right]}^{\lambda^{-1}}.
  \end{align*}
  Thus, this algorithm can break the bilinear $q$-strong Diffie-Hellman assumption. This proves that our lemma holds for $n=2$.

  Supposing our lemma is true for any $n=k$, where $k\ge 2$, in the following we prove it is also true for $n=k+1$. Let $\varepsilon_k$ denote the event that \textsf{Adv} returns incorrect accumulative value $sum(\uplus_{i=1}^{k} X_i)$ and passes the client's verification. By law of probability, we have:
  \begin{align*}
    \Pr[\varepsilon_{k+1}] &=  \Pr[\varepsilon_{k}]\Pr[\varepsilon_{k+1} | \varepsilon_{k}] +
    \Pr[\varepsilon_{k}']\Pr[\varepsilon_{k+1} | \varepsilon_{k}']\\
                           &\leq \Pr[\varepsilon_{k}] + \Pr[\varepsilon_{k+1}|\varepsilon_{k}'] \\
                           &= \Pr[\varepsilon_{k}] + \Pr[\varepsilon_{2}],
  \end{align*}
  where $\varepsilon'$ denotes the complement of an event. Finally, we have:
  \begin{align*}
    \Pr[\varepsilon_n] \leq (n-1) \Pr[\varepsilon_2]
  \end{align*}
  Since $\Pr[\varepsilon_2]$ is negligible according to our proof for $n=2$, we can conclude that $\Pr[\varepsilon_{n}]$ is negligible as $p\gg n$ in a cyclic multiplicative group $\mathbb{G}$. Hence, this lemma is proved.
\end{proof}

\section{Security of \texorpdfstring{$union(\cdot)$}{union(\textcdot)} Operation}

\begin{lemma}[Security of $union(\cdot)$ operation]\label{lem:aggregate-queries:union}
  Under the bilinear $q$-strong Diffie-Hellman assumption, if the accumulative value returned by $union(\{X_1,\dots,X_n\})$ passes the client's verification, then the probability of $union(\{X_1,\dots,X_n\}) \neq acc(\cup_{i=1}^n X_i)$ is negligible for any PPT adversary.
\end{lemma}
\begin{proof}
  Let $U = \cup_{i=1}^n X_i$. The $union(\cdot)$ protocol consists of three modules
  \begin{inlineenum}
  \item $sub(\widehat{X}_1, U)$, $sub(\widehat{X}_2, U)$, $\dots$, $sub(\widehat{X}_n, U)$;%
    \label{enum:aggregate-queries:union-proof:mod1}
  \item $empty(\{U-\widehat{X}_1, U-\widehat{X}_2, \dots, U -\widehat{X}_n\})$; and%
    \label{enum:aggregate-queries:union-proof:mod2}
  \item SP sends the ECRH hash value ${h_e(U)}^*$ for $U$.%
    \label{enum:aggregate-queries:union-proof:mod3}
  \end{inlineenum}
  Due to the composition property and the security of~\ref{enum:aggregate-queries:union-proof:mod1} and~\ref{enum:aggregate-queries:union-proof:mod2}, we only need to prove the security of~\ref{enum:aggregate-queries:union-proof:mod3}, that is, ${acc(U)}^*$ cannot be forged if ${h_e(U)}^*$ passes the client's verification. Denote $acc^{-1}({acc(U)}^*)$ as the set who has accumulator value ${acc(U)}^*$.

  First, we prove $acc^{-1}({acc(U)}^*)\subseteq U$, i.e., the returned value ${acc(U)}^*=g^{{P(U)}^*}$=$g^{P(U) \cdot Q_U(s)}$, where $Q_U(s)$ is some polynomial. We prove this by contradiction.
  We assume there exists an element $x_j \in \widehat{X}_i$ such that $(x_j + s) \nmid {P(U)}^*$, i.e., ${P(U)}^* = (x_i + s) \cdot Q_i(s) + \lambda_i$, where $Q_i(s)$ is some polynomial and $\lambda_i$ is a constant value. During the execution of protocol $sub(\widehat{X}_i, U)$, by expanding $e(acc(\widehat{X}_i), W_i^*) = e({acc(U)}^*, g)$, the adversary can get:
  \begin{align*}
    e(g^{\prod_{k=1}^{|\widehat{X}_i|}(x_k+s) }, W_i^*)  = {e(g, g)}^{(x_j+s) \cdot Q_i(s) + \lambda_i}.
  \end{align*}
  By dividing $(x_j+s)$ on the exponents of both sides, the adversary can get:
  \begin{align*}
    {e(g, W_i^*)}^{\prod_{k=1}^{j-1}{(x_k+s)} \cdot \prod_{k=j+1}^{|\widehat{X}_i|}{(x_k+s)}} = {e (g, g)}^{Q_i(s) + \lambda_i/(x_j+s)}.
  \end{align*}
  Finally, the adversary can get:
  \begin{align}
&{e(g, g)}^{1/(x_j + s)}  \nonumber \\
    = &{({e(g, W_i^*)}^{\prod_{k=1}^{j-1}(x_k+s) \cdot \prod_{k=j+1}^{|\widehat{X}_i|}{(x_k+s)}} \cdot {e(g, g)}^{-Q_i(s)})}^{1/\lambda_i},\nonumber
  \end{align}
  which violates the bilinear $q$-strong Diffie-Hellman assumption mention in \cref{sec:aggregate-queries:prelim}. Hence, by contradiction, ${P(U)}^*$ must be in the form of $P(U) \cdot Q_U(s)$.

  Next, we prove $U \subseteq acc^{-1}({acc(U)}^*)$, i.e., $Q_U(s)$ can only be a constant value. We prove this by contradiction. For each set $\widehat{X}_i$, ${acc(U - \widehat{X}_i)}^* = g^{P(U-\widehat{X}_i) \cdot Q_U(s)}$. During the execution of $empty(\cdot)$, by expanding $\prod_{i=1}^n e({acc(U-\widehat{X}_i)}^*, F_i^*)=e(g, g)$, the adversary can get:
  \begin{align*}
    \prod_{i=1}^n e(g^{P(U-\widehat{X}_i)}, g^{Q_i(s)}) = e(g, g),
  \end{align*}
  and then
  \begin{align*}
    {e(g,g)}^{Q_U(s)(\prod_{i=1}^n P(U-\widehat{X}_i)\cdot Q_i(s))}=e(g,g).
  \end{align*}
  Finally, the adversary can get:
  \begin{align*}
    {e(g, g)}^{1/Q_U(s)} = {e(g, g)}^{P(U-\widehat{X}_i \cdot Q_i(s))}.
  \end{align*}
  Since $Q_U(s)$ is a polynomial whose order is larger than $1$, the adversary violates the bilinear $q$-strong Diffie-Hellman assumption. Thus, by contradiction, $Q_U(s)$ must be a constant value.

  By the above two arguments, we prove that the accumulative value ${acc(U)}^*$ cannot be forged if it passes the client's verification.
\end{proof}

\section{Security of \texorpdfstring{$times(\cdot, \cdot)$}{times(\textcdot, \textcdot)} Operation}

\begin{lemma}[Security of $times(\cdot,\cdot)$ operation]\label{lem:aggregate-queries:times}
  Under the bilinear $q$-strong Diffie-Hellman assumption, if the accumulative value returned by $times(X,t)$ passes the client's verification, then the probability of $times(X,t) \neq acc(\uplus_{i=1}^t X)$ is negligible for any PPT adversary.
\end{lemma}
\begin{proof}
  The $times(\cdot, \cdot)$ operation relies only on the $sum(\cdot)$ operation, and therefore the proof is similar to that of \cref{lem:aggregate-queries:sum}.
\end{proof}

\section{Security of PA\texorpdfstring{$^2$}{\^{}2} Algorithms}

Following the above lemmas, we now show that the query results returned by the PA$^2$ algorithms cannot be forged.
\aggregatesecuritytheorem*

\begin{proof}
  According to the lemmas on the security of operations $sub(\cdot,\cdot)$, $empty(\cdot)$, $sum(\cdot)$, $union(\cdot)$, $times(\cdot,\cdot)$, the theorem is trivially true due to the composition property.
\end{proof}


\documentclass[10pt,letterpaper]{article}

\usepackage{outlines}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage{enumitem}
\usepackage{caption}
\usepackage{subcaption}
\DeclareCaptionOptionNoValue{centering}{\centering} % Make sure everything is centered in subs
\captionsetup[sub]{centering}

\usepackage{multirow}
\usepackage{cancel}
\usepackage{float}

\usepackage{parskip}

\usepackage{slantsc,lmodern}

\usepackage{pgfplotstable,booktabs}
\usepackage{textcomp}
\usepackage{gensymb}

\usepackage{paralist}

\usepackage{amsmath}
\usepackage{tikz}
\usepackage{hyperref}

\usepackage{pst-node}
\usepackage{auto-pst-pdf}

\usepackage[paper=a4paper,margin=1.25in]{geometry}

\makeatletter
\g@addto@macro\@floatboxreset\centering
\makeatother

\newcommand{\volume}{{\ooalign{\hfil$V$\hfil\cr\kern0.08em--\hfil\cr}}}

\author{Thaddeus Hughes \\ hughes.thad@gmail.com \\ thaddeus-maximus.github.io}
\date{\today}
\title{Documentation and Validation of EveryCalc's Beam Tool}

\begin{document}
	\maketitle

	\begin{abstract}
		Beams are common structures for structural analysis. As such many analytical formulas for specific use cases exist. Complex cases can also be computed by use of static superposition, or the utilization of a finite-element (FE) model. I created a web interface for a rudimentary beam FE model.
	\end{abstract}

	\section{How does FE work?}

	A finite-element model works generally by solving a matrix equation of the form

	\begin{align}
		\{F\} = [K]\{q\} .
	\end{align}

	Where ${F}$ is the \textit{load vector}, $[K]$ is the \textit{stiffness matrix}, and ${q}$ is the \textit{displacement vector}. This may be recognized as simply a matrix form of a spring equation $F = k \delta$, which it is! The FE model works by splitting a large component into several small springs (\textit{elements}) with endpoints (\textit{nodes}).

	There are a few methods to solve this equation. One simple one is to multiply both sides by the matrix inverse (since there is no direct equivalent of division with matrices). This method does not scale well- but it works with the few elements we will need for this calculator.

	\begin{align}
		[K]^{-1} \{F\} = [K]^{-1} [K]\{q\} = \{q\}
	\end{align}

	There are many different forms of stiffness, depending on the exact element used.

	We will use a 2D beam element derived from \textit{Euler-Bernoulli beam theory}. This element has four degrees of freedom: vertical deflection $v$ and rotation $\psi$ at each end node.

	\begin{figure}[H]
	\begin{tikzpicture}[x=0.8in,y=0.8in]
		\draw[darkgray, ultra thick] (-2,-.5) -- (-2,.5) -- (2,.5) -- (2,-.5) -- cycle;
		\draw[black, ultra thick, ->] (-2.6,-0.6) -- (-2.6,0.6) node[pos=0.5, left]{$v_1$};
		\draw[black, ultra thick, ->] (+2.6,-0.6) -- (+2.6,0.6) node[pos=0.5, right]{$v_2$};
		\draw[black, ultra thick, ->] (-1.7,0) arc(0:230:0.3) node[pos=0.2, right]{$\psi_1$};
		\draw[black, ultra thick, ->] (2.3,0) arc(0:230:0.3) node[pos=0.2, right]{$\psi_2$};
		\fill[black] (2,0) circle (0.05);
		\fill[black] (-2,0) circle (0.05);
	\end{tikzpicture}
	\caption{A beam with a fixed support, a pinned support, and a force load.}
	\end{figure}

	It can be found that the equation derived from euler-bernoulli beam theory is

	\begin{align}
		\begin{Bmatrix}
			F_1 \\
			M_1 \\
			F_2 \\
			M_2
		\end{Bmatrix} =
		\begin{bmatrix}
			12  & 6L   & -12 & 6L   \\
			6L  & 4L^2 & -6L & 2L^2 \\
			-12 & -6L  & 12  & -6L  \\
			6L  & 2L^2 & -6L & 4L^2
		\end{bmatrix}
		\begin{Bmatrix}
			v_1 \\
			\psi_1 \\
			v_2 \\
			\psi_2
		\end{Bmatrix}
	\end{align}

	This is just one element though! How do we link the elements together?

	\section{Direct Assembly}

	One simple way is by \textit{direct assembly}. It can be noticed that (roughly) the matrix looks something like

	\begin{align}
		\begin{Bmatrix}
			F_1 \\
			F_2
		\end{Bmatrix} =
		\begin{bmatrix}
			K & -K \\
			-K & K
		\end{bmatrix}
		\begin{Bmatrix}
			u_1 \\
			u_2
		\end{Bmatrix}
	\end{align}

	If we had two elements linked together like so:

	\begin{figure}[H]
	\begin{tikzpicture}[x=0.4in,y=0.4in]
		\draw[darkgray, ultra thick] (-2,-.5) -- (-2,.5) -- (2,.5) -- (2,-.5) -- cycle;
		\draw[darkgray, ultra thick] (2,-.5) -- (2,.5) -- (6,.5) -- (6,-.5) -- cycle;
		\node[black] at (0,0) {A} ;
		\node[black] at (4,0) {B} ;
		\fill[black] (-2,0) circle (0.1) node[left]{1};
		\fill[black] (2,0)  circle (0.1) node[left]{2};
		\fill[black] (6,0)  circle (0.1) node[left]{3};
	\end{tikzpicture}
	\caption{Two connected beams.}
	\end{figure}

	These two beams share node 2. That is to say, they both see the forces from node 2, and the deflection at node 2. This leads us to combine the equations for each element as so:

	\begin{align}
		\begin{Bmatrix}
			F_1 \\
			F_2 \\
			F_3
		\end{Bmatrix} =
		\begin{bmatrix}
			K_A  & -K_A    & 0    \\
			-K_A & K_A+K_B & -K_B \\
			0    & -K_B    & K_B
		\end{bmatrix}
		\begin{Bmatrix}
			u_1 \\
			u_2 \\
			u_3
		\end{Bmatrix}
	\end{align}

	We've linked the nodes together, but we now need to constrain them. These beams are currently floating in space- we need to anchor them otherwise our displacements are meaningless (furthermore, the matrix $[K]$ would be singular and unsolvable). For example, let's apply the constraint that node 1 is fixed; that is, $u_1$ = 0. As a result, the associated column in $[K]$ (the first column) does not matter. Any loads applied to the node also do not matter since they would be absorbed by the fixed constraint. This allows us to remove columns and rows 1 of the stiffness matrix, degrees of freedom 1, and load 1, changing our equation to:

	\[
	\begin{pspicture}
		\begin{Bmatrix}
			\rnode{A}{F_1} \\
			F_2 \\
			F_3
		\end{Bmatrix} =
		\begin{bmatrix}
			\rnode{C}{K_A}  & -K_A    & 0    \\
			-K_A & K_A+K_B & -K_B \\
			\rnode{D}{0}    & -K_B    & K_B
		\end{bmatrix}
		\begin{Bmatrix}
			\rnode{B}{u_1} \\
			u_2 \\
			u_3
		\end{Bmatrix}
		\ncline{A}{B}
		\ncline{C}{D}
	\end{pspicture}
	\]

	\begin{align}
		\begin{Bmatrix}
			F_2 \\
			F_3
		\end{Bmatrix} =
		\begin{bmatrix}
			K_A+K_B & -K_B \\
			-K_B    & K_B
		\end{bmatrix}
		\begin{Bmatrix}
			u_2 \\
			u_3
		\end{Bmatrix}
	\end{align}

	At this point we could plug in values $F_2$ and $F_3$, and solve with the matrix inverse method.

	\section{Application to Beams}

	Let's show how this would work with a beam. Consider the following example:

	\begin{figure}[H]
	\begin{tikzpicture}[x=1.2in,y=1.2in]
		\draw[gray] (0, 0.05) -- (0, 0.3) node[pos=1, above]{$0$};
		\draw[gray] (1, 0.05) -- (1, 0.3) node[pos=1, above]{$L$};
		\draw[gray] (2, 0.05) -- (2, 0.3) node[pos=1, above]{$2L$};
		\draw[gray] (3, 0.05) -- (3, 0.3) node[pos=1, above]{$3L$};
		\draw[gray] (4, 0.05) -- (4, 0.3) node[pos=1, above]{$4L$};
		\draw[darkgray, ultra thick] (0,0) -- (4,0);
		\draw[blue, ultra thick, ->] (3,-0.05) -- (3,-0.3) node[pos=0.5, right]{W};
		\fill[black, ultra thick] (0.9,0) -- (1.1,0) -- (1.1,-0.2) -- (0.9,-0.2) -- cycle;
		\fill[black, ultra thick] (2,0) -- (2.1,-0.2) -- (1.9,-0.2) -- cycle;
	\end{tikzpicture}
	\caption{A beam with a fixed support, a pinned support, and a force load.}
	\end{figure}

	We can start by creating a general stiffness matrix with the general assembly method:

	\begin{align}
		[K] =
		\begin{bmatrix}
12 & 6L & -12 & 6L & & & & & & \\
6L & 4L^2 & -6L & 2L^2 & & & & & & \\
-12 & -6L & 24 & 12L & -12 & 6L & & & & \\
6L & 2L^2 & 12L & 8L^2 & -6L & 2L^2 & & & & \\
 & & -12 & -6L & 24 & 12L & -12 & 6L & & \\
 & & 6L & 2L^2 & 12L & 8L^2 & -6L & 2L^2 & & \\
 & & & & -12 & -6L & 24 & 12L & -12 & 6L \\
 & & & & 6L & 2L^2 & 12L & 8L^2 & -6L & 2L^2 \\
 & & & & & & -12 & -6L & 12 & -6L \\
 & & & & & & 6L & 2L^2 & -6L & 4L^2
		\end{bmatrix}
	\end{align}

	Zeroes have been omitted from matrix to aid in readability.

	The load vector is simply

	\begin{align}
		{F} =
		\begin{Bmatrix}
			F_1 \\
			M_1 \\
			F_2 \\
			M_2 \\
			F_3 \\
			M_3 \\
			F_4 \\
			M_4 \\
			F_5 \\
			M_5
		\end{Bmatrix} =
		\begin{Bmatrix}
			0 \\
			0 \\
			0 \\
			0 \\
			-W \\
			0 \\
			0 \\
			0 \\
			0 \\
			0
		\end{Bmatrix} .
	\end{align}

	Combining this yields the full, unconstrained equation

	\begin{align}
		\begin{Bmatrix}
			0 \\
			0 \\
			0 \\
			0 \\
			-W \\
			0 \\
			0 \\
			0 \\
			0 \\
			0
		\end{Bmatrix} = \begin{bmatrix}
12 & 6L & -12 & 6L & & & & & & \\
6L & 4L^2 & -6L & 2L^2 & & & & & & \\
-12 & -6L & 24 & 0 & -12 & 6L & & & & \\
6L & 2L^2 & 0 & 8L^2 & -6L & 2L^2 & & & & \\
 & & -12 & -6L & 24 & 0 & -12 & 6L & & \\
 & & 6L & 2L^2 & 0 & 8L^2 & -6L & 2L^2 & & \\
 & & & & -12 & -6L & 24 & 0 & -12 & 6L \\
 & & & & 6L & 2L^2 & 0 & 8L^2 & -6L & 2L^2 \\
 & & & & & & -12 & -6L & 12 & -6L \\
 & & & & & & 6L & 2L^2 & -6L & 4L^2
		\end{bmatrix} \begin{Bmatrix}
			v_1 \\
			\psi_1 \\
			v_2 \\
			\psi_2 \\
			v_3 \\
			\psi_3 \\
			v_4 \\
			\psi_4 \\
			v_5 \\
			\psi_5
		\end{Bmatrix} .
	\end{align}

	The fixed support at node 2 removes degrees of freedom $v_2$, $\psi_2$. The pinned support at node 3 removes only $v_3$ (pin still permits rotation).

	\[
	\begin{pspicture}
		\begin{Bmatrix}
			0 \\
			0 \\
			\rnode{A}{0} \\
			\rnode{C}{0} \\
			\rnode{E}{0} \\
			0 \\
			-W \\
			0 \\
			0 \\
			0
		\end{Bmatrix} = \begin{bmatrix}
12 & 6L & \rnode{G}{-12} & \rnode{I}{6L} & \rnode{K}{} & & & & & \\
6L & 4L^2 & -6L & 2L^2 & & & & & & \\
-12 & -6L & 24 & 0 & -12 & 6L & & & & \\
6L & 2L^2 & 0 & 8L^2 & -6L & 2L^2 & & & & \\
 & & -12 & -6L & 24 & 0 & -12 & 6L & & \\
 & & 6L & 2L^2 & 0 & 8L^2 & -6L & 2L^2 & & \\
 & & & & -12 & -6L & 24 & 0 & -12 & 6L \\
 & & & & 6L & 2L^2 & 0 & 8L^2 & -6L & 2L^2 \\
 & & & & & & -12 & -6L & 12 & -6L \\
 & & \rnode{H}{} & \rnode{J}{} & \rnode{L}{} & & 6L & 2L^2 & -6L & 4L^2
		\end{bmatrix} \begin{Bmatrix}
			v_1 \\
			\psi_1 \\
			\rnode{B}{v_2} \\
			\rnode{D}{\psi_2} \\
			\rnode{F}{v_3} \\
			\psi_3 \\
			v_4 \\
			\psi_4 \\
			v_5 \\
			\psi_5
		\end{Bmatrix}
		\psset{nodesep=-1.5ex, linewidth=0.4pt}
		\ncline{A}{B}
		\ncline{C}{D}
		\ncline{E}{F}
		\ncline{G}{H}
		\ncline{I}{J}
		\ncline{K}{L}
	\end{pspicture}
	\]

	\begin{align}
		\begin{Bmatrix}
			0 \\
			0 \\
			0 \\
			-W \\
			0 \\
			0 \\
			0
		\end{Bmatrix} = \begin{bmatrix}
12 & 6L & & & & & \\
6L & 4L^2 & & & & & \\
 & & 8L^2 & -6L & 2L^2 & & \\
 & & -6L & 24 & 0 & -12 & 6L \\
 & & 2L^2 & 0 & 8L^2 & -6L & 2L^2 \\
 & & & -12 & -6L & 12 & -6L \\
 & & & 6L & 2L^2 & -6L & 4L^2
 		\end{bmatrix} \begin{Bmatrix}
			v_1 \\
			\psi_1 \\
			\psi_3 \\
			v_4 \\
			\psi_4 \\
			v_5 \\
			\psi_5
		\end{Bmatrix}
	\end{align}

	At this point, the matrix equation could be solved to achieve the nodal displacement vector. Afterwards, the resulting displacement matrix could be used with the unconstrained stiffness matrix to determine the loads at the constraints.

	\section{Prescribing Displacements}

	But, what if instead, we wanted to prescribe a displacement rather than a force? This throws a wrench in this whole process... or at least would mean we'd have to go back to the drawing board, and try \textit{matrix partitioning}. Let's consider a different setup, of the form

	\begin{align}
		\begin{Bmatrix}
			\{F_u\} \\
			\{F_p\}
		\end{Bmatrix} = \begin{bmatrix}
			[K_{u,u}] & [K_{u,p}] \\
			[K_{p,u}] & [K_{p,p}]
 		\end{bmatrix} \begin{Bmatrix}
			\{q_{u}\} \\
			\{q_{p}\}
		\end{Bmatrix},
	\end{align}

	where $\{F_u\}$ and $\{F_p\}$ are the portions of the load vector corresponding to the unprescribed and prescribed-displacement portions, respectively. The same holds for $\{q_u\}$ and $\{q_p\}$. There's no \textit{true} sub-vectoring going on here, the brackets are just to denote that these are bits of matrices inside the larger matrices, not just scalar values.

	We can expand out first row of the matrix equation to find that

	\begin{align}
		\{F_u\} = [K_{u,u}] \{q_{u}\} + [K_{u,p}] \{q_{p}\}
	\end{align}

	Solving this yields

	\begin{align}
		[K_{u,u}] \{q_{u}\} = \{F_u\} - [K_{u,p}] \{q_{p}\}
	\end{align}

	which is of the form $[M] \{x\} = \{b\}$ (since $\{q_{p}\}$ is the prescribed displacements), so can be solved.

	After finding the non-prescribed displacements, the second row of the matrix equation can be expanded to
	\begin{align}
		\{F_p\} = [K_{p,u}] \{q_{u}\} + [K_{p,p}] \{q_{p}\}
	\end{align}

	which can be used to compute the forces required to produce the prescribed displacements.

	We would then simply need to re-build the force and displacement vectors, and then we can proceed to post-processing.

	\section{Post Processing and Shape Functions}

	Usually, a displacement \textit{field} is desired rather than merely the nodal values. Getting deflection at arbitrary points requires the use of \textit{shape functions}. In the derivation of the beam stiffness matrix (which this paper does not cover), particular \textit{shape functions} were used: the Hermite shape functions. These are nondimensional equations that represent the contribution of one \textit{nodal} degree of freedom to the displacement \textit{field} in an element. By combining these displacement fields, we can determine the total displacement field.

	\begin{figure}[H]
		\includegraphics[width=0.7\textwidth]{hermite.png}
		\caption{Hermite shape functions}
	\end{figure}

	These hermite shape functions can be represented as:

	\begin{align}
		N = \begin{Bmatrix}
				1/4 (1-\zeta^2 (2+\zeta) \\
				L/8 (1-\zeta)^2 (\zeta+1) \\
				1/4 (1+\zeta)^2 (2-\zeta) \\
				L/8 (1+\zeta)^2 (\zeta-1)
		\end{Bmatrix}^T
	\end{align}

	Where $L$ is the element length and $\zeta$ is a nondimensional parameter that is -1 at the left of the element, +1 at the right, and 0 in the center.

	\begin{align}
		\zeta = 2 \frac{x}{L} - 1
	\end{align}

	Where $x$ is the distance from the left of the element.

	The functions can be used to find the displacement field $v$:

	\begin{align}
		v = \{N\} \{q\} =
		\begin{Bmatrix}
				1/4 (1-\zeta^2 (2+\zeta) \\
				L/8 (1-\zeta)^2 (\zeta+1) \\
				1/4 (1+\zeta)^2 (2-\zeta) \\
				L/8 (1+\zeta)^2 (\zeta-1)
		\end{Bmatrix}^T
		\begin{Bmatrix}
			v_1 \\
			\psi_1 \\
			v_2 \\
			\psi_2
		\end{Bmatrix}
	\end{align}

	This helps us plot the displacement field, but what about the slope, bending moment, and shear force?

	The slope $\theta$ is simply the derivative of the displacement field $v$ with respect to position $x$. The bending moment $M$ and shear force $V$ can also be found from Euler-Bernoulli beam theory.

	\begin{align}
		\theta &= \frac{d v}{d x} \\
		M      &= E I \frac{d^2 v}{d x^2} \\
		V      &= \frac{d}{d x} (E I \frac{d^2 v}{d x^2}) = E I \frac{d^3 v}{d x^3}
	\end{align}

	Assuming elastic modulus $E$ and second moment of area $I$ are constant over the element.

	To find these derivatives of $v$, we can use the shape functions, ignoring the nodal displacements since they do not vary with $x$.

	\begin{align}
		\frac{dv}{dx} = \frac{d}{dx} [\{N\} \{q\}] = \frac{d \{N\}}{dx} \{q\}
	\end{align}

	The chain rule can be used to help find these derivatives.

	\begin{align}
		\frac{d \{N\}}{d x} &= \frac{d \{N\}}{d \zeta} \frac{d \zeta}{d x} \\
		\frac{d^2 \{N\}}{d x^2} &= \frac{d^2 \{N\}}{d \zeta^2} (\frac{d \zeta}{d x})^2 + \frac{d \{N\}}{d \zeta} \frac{d^2 \zeta}{d x^2} \\
		\frac{d^3 \{N\}}{d x^3} &= \frac{d^3 \{N\}}{d \zeta^3} (\frac{d \zeta}{d x})^3 + 3 \frac{d^2 \{N\}}{d \zeta^2} \frac{d \zeta}{d x}\frac{d^2 \zeta}{d x^2} + \frac{d \{N\}}{d \zeta}\frac{d^3 \zeta}{d x^3}
	\end{align}

	Looks ugly! But luckily, the higher order derivatives of $\zeta$ are zero.

	\begin{align}
		\frac{d \zeta}{d x} &= \frac{2}{L} \\
		\frac{d^2 \zeta}{d x^2} = \frac{d^3 \zeta}{d x^3} &= 0
	\end{align}

	\begin{align}
		\frac{d \{N\}}{d x} &= \frac{d \{N\}}{d \zeta} \frac{2}{L} \\
		\frac{d^2 \{N\}}{d x^2} &= \frac{d^2 \{N\}}{d \zeta^2} (\frac{2}{L})^2 \\
		\frac{d^3 \{N\}}{d x^3} &= \frac{d^3 \{N\}}{d \zeta^3} (\frac{2}{L})^3
	\end{align}

	The shape function derivatives are

	\begin{align}
		\frac{d \{N\}}{d x}     =
		\begin{Bmatrix}
				3/4 (\zeta^2-1) 2/L \\
				L/8 (3 \zeta^2-2 \zeta - 1) 2/L \\
			    3/4 (\zeta^2-1) 2/L \\
				L/8 (3 \zeta^2+2 \zeta -1) 2/L
		\end{Bmatrix}^T &
		\frac{d^2 \{N\}}{d x^2} =
		\begin{Bmatrix}
				(6 \zeta)/L^2 \\
				(3 \zeta-1)/L \\
			   -(6 \zeta)/L^2 \\
				(3 \zeta+1)/L
		\end{Bmatrix}^T &
		\frac{d^3 \{N\}}{d x^3} =
		\begin{Bmatrix}
				12/L^3 \\
				6/L^2 \\
			   -12/L^3 \\
				6/L^2
		\end{Bmatrix}^T
	\end{align}

	\section{Generalization}
	How does this get generalized to different, arbitrary, and variable scenarios?

	\begin{asparaitem}
		\item All of the loads and constraints are gathered and binned by type. Each load is given a node id.
		\item The position along the beam for each load or constraint is stored by node id.
		\item The node ids are sorted by the order of positions.
		\item Close nodes (those that are within 1/100 of the beam length) are merged so that the corresponding load/constraints share the same node id. The old node id and position is deleted.
		\item Beam elements are produced between the first and second node, second and third, and so forth.
		\item The force matrix is populated with forces
		\item Direct assembly is performed on the beam elements
		\item Degrees of freedom corresponding to the node ids where pinned/fixed nodes exist are struck and removed from the stiffness and force matrices.
		\item The matrix system of equations is solved
		\item The resulting nodal displacements are used in conjunction with the shape functions to interpolate a displacement field.
	\end{asparaitem}

	\section{Sanity Check: are the Shape Functions Appropriate?}

	Usually finite element methods are used as approximations by employing the use of many, many elements to approximate the true displacement field. Beam elements, though, are somewhat unique in that \textit{under certain circumstances} they will model the underlying diplacement field exactly. Let's return to Euler-Bernoulli beam theory. This theory states that the displacement field $q$ can be represented as

	\begin{align}
		\frac{d^2}{d x^2} E I \frac{d^2 v}{d x^2} = q ,
	\end{align}

	where $q$ is a distributed load (i.e. with dimensions of force per unit length). Euler-Bernoulli beam theory has several assumptions wrapped up in it, such as the requirement that plane sections remain plane, and no large deflections.

	My calculator does not model distributed loads, so $q$ is always zero. Additionally, the cross-section and elastic modulus remain constant, so $E$ and $I$ can be moved outside of the derivative, leaving

	\begin{align}
		E I \frac{d^4 v}{d x^4} &= 0 \\
		\frac{d^4 v}{d x^4} &= 0
	\end{align}

	If we integrate both sides repeatedly, we can get the form of $v$.

	\begin{align}
		\int \frac{d^4 v}{d x^4} dx &= \int 0 dx \\
		\int \frac{d^3 v}{d x^3} dx &= \int c_1 dx \\
		\int \frac{d^2 v}{d x^2} dx &= \int c_1 x + c_2 dx \\
		\int \frac{d v}{d x} dx     &= \int c_1 x^2 + c_2 x + c_3 dx \\
		v &= c_1 x^3 + c_2 x^2 + c_3 x + c_4
	\end{align}

	The shape of $v$ under the assumptions of constant $E$ and $I$ with only point loads is a cubic polynomial, a function with four degrees of freedom. What else has four degrees of freedom? Our elements! Using beam elements provides a sufficient quantity of degrees of freedom to provide exact analytical solutions to problems where Euler-Bernoulli beam theory is appropriate, and require point loads and constant cross sections. This may sound restrictive, but encompasses nearly all applications of beams, which is why they are such a powerful modeling tool even in full-blown FE applications.

	\section{Validation Examples}

	Comparison to analytical solutions is a necessary component when validating any FE model. \href{https://mechanicalc.com/reference/beam-deflection-tables}{\underline{MechaniCalc}} has some good analytical solutions I will compare to.

	\newpage
	\subsection{Cantilevered, Intermediate Load}

	\begin{figure}[H]
		\includegraphics[width=0.5\textwidth]{beam_case1_schematic.png}
	\end{figure}

	Let $E = 69 GPa$ (aluminum), $L = 100 mm$, round bar with $d = 5 mm$, $F = 200 N$, $a = 60mm$.

	\begin{align}
		I = \frac{\pi}{64} d^4 = \frac{\pi}{64} (0.100 m)^4 = 3.06796 \times 10^{-11} m^4
	\end{align}
	\begin{align}
		\delta_{a} &= - \frac{F a^2}{6 E I} (3 L - a) = - \frac{200 N \times (0.06 m)^2}{6 \times 69 GPa \times 30.6796 mm^4} (3 \times 0.06 m - 0.06 m) = 6.802 mm\\
		\delta_{end} &= - \frac{F a^2}{6 E I} (3 L - a) = - \frac{200 N \times (0.06 m)^2}{6 \times 69 GPa \times 30.6796 mm^4} (3 \times 0.1 m - 0.06 m) = 13.6048 mm\\
		\theta_{a/2} &= - \frac{F (a/2)^2}{2 E I} (2 L - a/2) = \frac{200 N \times .03 m}{2 \times 69 GPa \times 30.6796 mm^4} (2 \times 0.1m - .03m) = 7.305 \ deg\\
		V_{0 \rightarrow a} &= F = 200 \ N \\
		M_{x=0} &= -F \times a = - 200 N \times .06 m = - 12\ Nm
	\end{align}

	You can plug in the values to the calculator and verify this load case for yourself. I found all of the above numbers to be accurate.

	\newpage
	\subsection{Simply Supported, Center Moment}

	\begin{figure}[H]
		\includegraphics[width=0.5\textwidth]{beam_case2_schematic.png}
	\end{figure}

	Let $E = 27557 ksi$ (steel), $L = 6 in$, box bar with $w = 0.5 in$, $h = 2in$, $M = 1200 ft-lbf$.

	\begin{align}
		I = \frac{1}{12} b h^3 = 1/3 \ in^4
	\end{align}
	\begin{align}
		\delta_{x = 1.25 in} &= \frac{ - M x}{24 L E I}(L^2 - 4*x^2) = \frac{-12\ ft\ lbf\ 1.25 \ in}{24 \times \ 6 \ in \ 27557 \ ksi \ 1/3 \ in^4} ((6 in)^2 - 4 (1.25 in)^2) = -.4048 \ thou \\
		\theta_{1} &= \frac{- M L}{24 E I} = \frac{-1200\ ft\ lbf\ 6 \ in}{24 \times \ 27557 \ ksi \ 1/3 \ in^4} = -.02246 \ deg \\
		V &= M / L = 1200 ft-lbf / 6 in = 2400 lbf \\
		M_{x=L/2} &= M / 2 = 1200 ft-lbf / 2 = 600 ft-lbf
	\end{align}

	You can plug in the values to the calculator and verify this load case for yourself. I found all of the above numbers to be accurate.

\end{document}


\section{Characterization of CUDA Kernels}\label{sec:characterization}
The current architectures of GPUs have a hierarchical memory management, where cores make data requests in their registers, these requests pass through caches that in some cases may be shared caches, and so on until to arrive to its global memory. When a request data of a thread increases of level in the memory hierarchy, it increases increases the communication latency. On the contrary, while higher memory level, higher  capacity of storage, see Figure~\ref{fig:PyrLatency}.

\begin{figure}[htpb]
\centering
\includegraphics[scale=.75]{./images/memhierarchy.png}
\caption{Variance of the latency and capacity of storage in the memory systems}
\label{fig:PyrLatency}
\end{figure}

The performance of a function executed over a GPU depends greatly on the optimizations made in the accesses to data in the memory hierarchy. The bandwidth of the memory is optimized by grouping a set of threads, so, the threads in the group are benefited in the communication. This effect is called coalesced accesses~\citep{Wu:2013:Coalesced, Che:2011}. In Tesla architectures, the first GPGPU architecture, these coalesced accesses were by threads of a half warp, i.e. 16 threads can be coalesced to one transaction for word of size 8-bit, 16-bit, 32 bit, 64-bit or 128-bit. On Fermi, Kepler and newer architectures coalesced accesses can be done by all threads of a warp.

Since the early generations of GPUs for general purpose, GPUs have had different types of memories, which are differentiated by the type and visibility in the data. In Table~\ref{tab:memories} are presented the different types of memories existing in current GPUs. This table shows the type of operation that each memory can do.
The constant memory is an off-chip and it can be read by all threads in a kernel, however the CPU is the only which can write on it.
The On Chip memories are those that are inside each multiprocessors and consequently the communication latency is lower. Global memory is the main memory of the GPU, local and constant memory are just different addressing modes of the global memory. Global Memory is DRAM, on the contrary, all on-chip memory (shared memory, registers, and caches) are SRAM.

\begin{table}[htpb]
\centering
\begin{tabular}{| l | c | c | c  |  c |}
\hline \hline%inserts double horizontal lines
\textbf{Type} & \textit{\textbf{On Chip}}&\textbf{Cacheable}&\textbf{Operations}&\textbf{Visibility} \\ \hline
Registers&Yes&No&Read/Write&\textit{Thread}\\ \hline
Local&Not&Yes&Read/Write&\textit{Thread}\\ \hline
Shared&Yes&No&Read/Write&Block\\ \hline
Global&No&Yes&Read/Write&\textit{Kernels}\\ \hline
Constant&No&Yes&Read&\textit{Kernels}\\ \hline
Texture&No&Yes&Read/Write&\textit{Kernels}\\ \hline
\hline
\end{tabular}
\caption{Memory types of the GPUs manufactured by Nvidia}
\label{tab:memories} % is used to refer this table in the text
\end{table}

We analyzed different kernels, aiming to show which are the different optimizations that impact the performance of a GPU application. Figure~\ref{fig:BlockTunning} shows the running times of the kernel of matrix multiplication using only global memory without coalesced accesses, i.e. the kernel $MMGU$. The experiments are done varying the number of threads per block, with dimensions of $8\times{}8$, $16\times{}16$, or $32\times{}32$. Figure~\ref{fig:BlockTunning}A shows the running times, Figure~\ref{fig:BlockTunning}B the number of load transactions per request in the global memory and Figure~\ref{fig:BlockTunning}C the number of store transactions per request in the global memory. This kernel has a bad access in the global memory, for this reason it reach higher performance when the dimension of the blocks is lower.

\begin{figure}[htpb]
	\centering
    \includegraphics[scale=.5]{images/plotBlock.pdf}
    \caption{Tuning of threads per Block in MMGU on the GPU GTX-970}
    \label{fig:BlockTunning}
\end{figure}

Figure~\ref{fig:Coalesced} show the performance of two application in two different versions each one, the applications are matrix multiplication and matrix addiction, the versions  of these applications are $MMGC$ and $MMGU$; and, $MAC$ and $MAU$; respectively. The selected dimensions of threads per block in each kernel was $8\times{}8$. Each kernel change communication pattern in the global memory. Figure~\ref{fig:Coalesced}A shows the running time of the kernels $MMGC$ and $MMGU$ and Figure~\ref{fig:Coalesced}B the number of load transactions per request in the global memory. Figure~\ref{fig:Coalesced}C shows the running time of the kernels $MAC$ and $MAU$ and Figure~\ref{fig:Coalesced}D the number of load transactions per request in the global memory of two different version of the matrix addition. It is easy to perceive that when the number of transaction per request in the global memory is smaller the running time of the application improve and consequently the arithmetic throughput of the application.

\begin{figure}[htpb]
	\centering
    \includegraphics[scale=.5]{images/plotCoalesced.pdf}
    \caption{Coalesced accesses impact in 2 different kernels of Matrix Multiplication and Matrix Addition on the GPU GTX-970}
    \label{fig:Coalesced}
\end{figure}

Figure~\ref{fig:Shared} shows the impact of the shared memory and coalesced accesses in the 4 versions of matrix multiplication. Figure~\ref{fig:Shared}A shows the running time of the kernels $MMGU$, $MMGC$, $MMSU$ and $MMSC$; Figure~\ref{fig:Shared}B shows the throughput in the global memory and Figure~\ref{fig:Shared}C shows the number of load transactions per request in the global memory of the 4 kernels. The worst throughput in the load memory is done for the kernel $MMSU$, it means that using shared memory does not improve the throughput  in the load memory, coalesced accesses are necessary for this goal.

\begin{figure}[htpb]
	\centering
    \includegraphics[scale=.5]{images/plotCoalescedShared.pdf}
    \caption{Shared memory optimizations in kernels of Matrix Multiplication on the GPU GTX-970}
    \label{fig:Shared}
\end{figure}

In ont of the work indirectly relate with this thesis, we implemented an autotuner for the CUDA compiler using the use the OpenTuner
framework~\cite{ansel2014opentuner} and used it to search for the compilation parameters  that optimize the performance of different GPU applications.

Figure~\ref{fig:autotuning} shows the results of an implemented autotuner for the CUDA compiler using the use the OpenTuner framework~\citep{ansel2014opentuner} and used it to search for the compilation parameters that optimize the performance of GPU applications. The main result is this research was to show that it is possible to optimize code written for GPUs by automatically tuning just the parameters of the CUDA compiler.

\paragraph{The Search Space}\label{sec:parameters}

% \newcommand{\specialcell}[1]{\begin{minipage}[m]{0.52\columnwidth}\centering#1\end{minipage}}

\begin{table}[htpb]
    \centering
    \footnotesize
        \begin{tabular}{cc}
        \toprule
        \textbf{Flag}&\textbf{Description} \\\midrule
        \texttt{no-align-double} & \specialcell{Specifies that \texttt{malign-double} should not be passed as a compiler argument on 32-bit platforms. \textbf{Step}: NVCC} \\ \midrule
        \texttt{use\_fast\_math} & \specialcell{Uses the fast math library, implies \texttt{ftz=true}, \texttt{prec-div=false}, \texttt{prec-sqrt=false} and \texttt{fmad=true}. \textbf{Step}: NVCC} \\\midrule
        \texttt{gpu-architecture} & \specialcell{Specifies the NVIDIA virtual GPU architecture for which the CUDA input files must be compiled. \textbf{Step}: NVCC \textbf{Values}: \texttt{sm\_20}, \texttt{sm\_21}, \texttt{sm\_30}, \texttt{sm\_32}, \texttt{sm\_35}, \texttt{sm\_50}, \texttt{sm\_52}} \\\midrule
        \texttt{relocatable-device-code} & \specialcell{Enables the generation of relocatable device code. If disabled, executable device code is generated. Relocatable device code must be linked before it can be executed. \textbf{Step}: NVCC} \\\midrule
        \texttt{ftz} & \specialcell{Controls single-precision denormals support. \texttt{ftz=true} flushes denormal values to zero and \texttt{ftz=false} preserves denormal values. \textbf{Step}: NVCC} \\\midrule
        \texttt{prec-div} & \specialcell{Controls single-precision floating-point division and reciprocals. \texttt{prec-div=true} enables the IEEE round-to-nearest mode and \texttt{prec-div=false} enables the fast approximation mode. \textbf{Step}: NVCC} \\\midrule
        \texttt{prec-sqrt} & \specialcell{Controls single-precision floating-point squre root. \texttt{prec-sqrt=true} enables the IEEE round-to-nearest mode and \texttt{prec-sqrt=false} enables the fast approximation mode. \textbf{Step}: NVCC} \\\midrule
        \texttt{def-load-cache} & \specialcell{Default cache modifier on global/generic load. \textbf{Step}: PTX \textbf{Values}: \texttt{ca}, \texttt{cg}, \texttt{cv}, \texttt{cs}} \\\midrule
        \texttt{opt-level} & \specialcell{Specifies high-level optimizations. \textbf{Step}: PTX \textbf{Values}: \texttt{0 - 3}} \\\midrule
        \texttt{fmad} & \specialcell{Enables the contraction of floating-point multiplies and adds/subtracts into floating-point multiply-add operations (FMAD, FFMA, or DFMA). \textbf{Step}: PTX} \\\midrule
        \texttt{allow-expensive-optimizations} & \specialcell{Enables the compiler to perform expensive optimizations using maximum available resources (memory and compile-time). If unspecified, default behavior is to enable this feature for optimization level $\geqslant$O2. \textbf{Step}: PTX} \\\midrule
        \texttt{maxrregcount} & \specialcell{Specifies the maximum number of registers that GPU functions can use. \textbf{Step}: PTX \textbf{Values}: \texttt{16 - 64}} \\\midrule
        \texttt{preserve-relocs} & \specialcell{Makes the \texttt{PTX} assembler generate relocatable references for variables and preserve relocations generated for them in the linked executable. \textbf{Step}: NVLINK} \\\midrule
        \end{tabular}
    \caption{Description of flags in the search space}
    \label{tab:flags}
\end{table}

Table~\ref{tab:flags} details the subset of the CUDA configuration
parameters used in the experiments~\footnote{Adapted from: http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc [Accessed on 20 February 2018]}.
The parameters target different compilation steps: the \emph{PTX} optimizing assembler;
the \emph{NVLINK} linker; and the \emph{NVCC} compiler.  We compared the
performance of programs generated by tuned parameters with the
standard compiler optimizations, namely \texttt{--opt-level=0,1,2,3}.
Different \texttt{--opt-level}s could also be selected during tuning.
We did not use compiler options that target the host linker or the library manager
since they do not affect performance.
The size of the search space defined by all possible combinations
of the flags in Table~\ref{tab:flags} is in the order of $10^{6}$ making
hand-optimization or exhaustive searches very time consuming.

Figure~\ref{fig:autotuning} show the results of the application matrix multiplication with the 4 different version used in this work. We can see that optimizations can be done by compiler parameters, and these compiler parameters can impact the speedup of GPU application in up $4x$, this work has been done by~\cite{bruel:2017:CCPE}.

\begin{figure}[htpb]
	\centering
    \includegraphics[scale=.2]{images/MatrixSummary.eps}
    \caption{Summary of the speedups achieved versus \emph{-O2} in matrix multiplication versions}
    \label{fig:autotuning}
\end{figure}


% To build, install TeX-Live, then in this directory,
% pdflatex spark-inequality-impact.tex
% bibtex spark-inequality-impact.aux
% pdflatex spark-inequality-impact.tex
% pdflatex spark-inequality-impact.tex

\documentclass[11pt, oneside]{article}  % use "amsart" instead of "article" for AMSLaTeX format
\usepackage{geometry}
\geometry{letterpaper}                  % ... or a4paper or a5paper or ...
%\geometry{landscape}                   % Activate for for rotated page geometry
\usepackage[parfill]{parskip}           % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}                   % Use pdf, png, jpg, or eps with pdflatex; use eps in DVI mode
                                        % TeX will automatically convert eps --> pdf in pdflatex
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{breakurl}
\usepackage{mathtools}
\usepackage{listings}

\lstset{basicstyle=\small}

\DeclareMathOperator{\Atkinson}{A}
\DeclareMathOperator{\round}{round}

\title{spark-inequality-impact Background}
\author{Stuart Ambler, Guillaume Saint Jacques, Amir Sepehri}
\date{\today}

\begin{document}
\maketitle
\tableofcontents

The spark-inequality-impact software provides a way for economists and others to compute the Atkinson index, a measure of economic inequality, from any Apache Spark DataFrame with a non-negative ``value'' column of double type (not necessarily monetary amounts), using a scala UDAF (user-defined aggregation function) that also gives a ``theoretical'' (asymptotic) variance for the index.  This will scale to large amounts of data.  The software also allows inference on the Atkinson index, including functions to calculate approximate confidence intervals for the index, confidence intervals for the difference of indices from two populations, p-values for the hypothesis that two samples come from populations with the same Atkinson index, and given an Atkinson index value and its $\epsilon$ parameter, an Atkinson-equivalent population of ``haves'' and ``have-nots'', usable as a representative population for that value.

Please see also \cite{SaintJacques} for broader background, more connections and detail.  In particular it gives the asymptotic distribution of the index, which this software uses to calculate an approximate variance.  For background in spark, \cite{Spark} is the documentation for the version of Spark the software has been tested with.  There are many online tutorials.  There are many online references on statistical inference; one that emphasizes the normal approximations we use, which are suitable for large samples, is \cite{Matloff} (see chapter 15 for confidence intervals and section 16.4 for p-values).

\section{The Atkinson inequality index and its $\epsilon$ parameter}

The British economist Anthony B. Atkinson wrote a paper ``On the Measurement of Inequality'' \cite{Atkinson} in 1970 that discusses measures of inequality including the one now named after him, that can be derived from mathematical assumptions about social utility functions and inequality measures.  The Atkinson index summarizes in one number between $0$ and $1$, the inequality of incomes of a group of people.  The value $0$ is attained when everyone has the same income.  Higher values indicate more inequality, and $1$ is an upper bound.

There is an adjustable parameter $\epsilon$, used to indicate how averse to inequality the index will be.  In the spark-inequality-impact software, we assume $0 \le \epsilon < 1$.  It is a choice, with $0$ indicating no aversion to inequality and $1$ indicating considerable aversion; see \cite{SaintJacques} for more on this choice.  \cite{AtkinsonWiki} gives summary information on the Atkinson index and an interpretation of $\epsilon$.

The Atkinson index of inequality with inequality aversion parameter \(\epsilon \) on a sample \(x_1, \ldots, x_n\) is defined by the formula
\[ \Atkinson_\epsilon (x_1, \ldots, x_n) = 1 - \frac{(\frac{1}{n}\sum_{i=1}^n x_i^{1-\epsilon})^\frac{1}{1-\epsilon}}{\frac{1}{n}\sum_{i=1}^n x_i}.\]

An important feature of the Atkinson index method is the fact that it is agnostic to the underlying factors driving inequality, and only measures the inequality in the distribution.

\section{A simple interpretation of the Atkinson index}

Here is a simple way to get a sense of meaning of the actual values of Atkinson indices. The basic idea is to pick, from the infinity of populations with the same Atkinson index (given $\epsilon$), an especially easy to understand representative.

\subsection{Haves and have-nots}

Consider taking the total income for a group of people and dividing it equally among a fraction of them, the ``haves'', leaving the others with no income, the ``have-nots''.  If the fraction is $0$ (no ``haves'') or $1$ (no ``have-nots''), the Atkinson index is $0$ for perfect equality.  Otherwise, if there are relatively few haves, the Atkinson index is large, indicating a lot of inequality, or if relatively many, the index is smaller.  This allows a conversion from the Atkinson index of a group of people (and $\epsilon$), to a representative group with only ``haves'' and ``have-nots'' but with the same Atkinson index.  Below are graphs of the conversion for three values of $\epsilon$.

More technically, let the population size be $N$.  Since the Atkinson index is invariant when all incomes are multiplied by the same positive number, we may as well say that all the ``haves'', whose number is $m$, get $1$ unit of income, and the ``have-nots'', $0$.  Thus we can take the list $x$ of incomes with $m$ equal to $1$ and $N - m$ equal to $0$.  Then the Atkinson index is
\begin{align}
  \Atkinson_{\epsilon)}(x) &= 1 - \frac{(\frac{m}{N})^{\frac{1}{1 - \epsilon}}}{\frac{m}{N}} = 1 - \left(\frac{m}{N}\right)^{\frac{\epsilon}{1 - \epsilon}} \notag
\end{align}
Conversely, if we know the Atkinson index $a$, we can get the fraction $f$ of ``haves'', and obtain their number $m$ to a pretty good approximation since $N$ is fairly large for us, by $m = \round(f N)$.  If $a$ is $1$ we set $f$ to something tiny and $m$ to $1$.  Otherwise,
\begin{align}
  f &= (1 - a) ^ \frac{1 - \epsilon}{\epsilon} \notag
\end{align}

\includegraphics[scale=0.5]{../R/atkinson-to-haves.pdf}

\subsection{Comparison of pairs of Atkinson indices using haves}
It may be important to compare Atkinson indices for pairs of groups, such as from A/B tests.  Each of the Atkinson indices can be converted to a fraction, and then the fractions subtracted.  The interpretation, for example of ``the Atkinson-equivalent fraction of haves increased by 0.1'' would be that splitting up the income equally among a group of haves of the size that corresponds to the Atkinson index, both before and after an intervention, the fraction of haves increased by $0.1$.

\subsection{Variance of number and fraction of haves in a random sample}
Suppose that we sample $r$ elements at random from a population of size $N$ that actually has $h$ haves. Then following \cite{Feller} pages 232-233, the number of haves in the sample has a hypergeometric distribution with expected value $r h / N$ and variance $r h (N - h) (N - r) / (N^2 (N - 1))$.  Letting $f = h / N$ denote the fraction of haves in the population,
\begin{align}
  \text{Expected fraction of haves in sample} &= f \notag \\
  \text{Variance of fraction of haves in sample} &= f (1 - f) \frac{N - r}{N - 1} \notag
\end{align}
(The factor $(N - r) / (N - 1)$ would be omitted if the sampling were with replacement.)  To compute confidence intervals for the haves fraction, one can use R package hypersamplan, or e.g. the online calculator at \url{https://epitools.ausvet.com.au/ciproportion}.

In a simulation drawing samples of size $2$K from a population of size $20$K with $20$\% $1$s and the rest $0$s, the actual and calculated variances agreed within $1.4$\%.  For a gamma distribution with shape parameter 0.114 (sharply peaked) the agreement was within $13$\%.  For lognormal data, the ratio of actual to calculated was $15$ in one run; the numbers varied from run to run.

\section{A way to think  of  Atkinson index differences in terms of CDFs}

Here is theoretical background giving a way to think of the difference of two Atkinson indices, in terms of the distributions of the populations.  The population value of the Atkinson index is

\[ \Atkinson_\epsilon(F) = 1 - \frac{\left(\int x^{1-\epsilon} dF(x) \right)^{\frac{1}{1-\epsilon}}}{\int x dF(x)} \]

Since the Atkinson index is scale invariant, we can assume \(\int x dF(x) = 1\). Then, we can compare the Atkinson index for two populations with CDFs \(F\) and \(G\) using the following heuristic:

\[\Atkinson_\epsilon(F)  - \Atkinson_\epsilon(G)  \approx \left(\int x^{1-\epsilon} dG(x) \right)^{\frac{\epsilon}{1-\epsilon}}  \left(\int x^{-\epsilon} (G(x)- F(x)) d x \right)\].

What this means is that the difference between the distribution at \(x\) is weighted by \(x^{-\epsilon}\). This weights the difference at the lower part of the population more heavily than the upper part. The difference in the weighting depends on \(\epsilon\). The following derivation justifies the approximation.

For two populations with CDFs \(F\) and \(G\) we have

\[  \Atkinson_\epsilon(F)  - \Atkinson_\epsilon(G) = \left(\int x^{1-\epsilon} dG(x) \right)^{\frac{1}{1-\epsilon}} - \left(\int x^{1-\epsilon} dF(x) \right)^{\frac{1}{1-\epsilon}} \]

A Taylor expansion of the function \(f(x) = x^{\frac{1}{1-\epsilon}} \) yields

\[\Atkinson_\epsilon(F)  - \Atkinson_\epsilon(G)  \approx \frac{1}{1-\epsilon} \left(\int x^{1-\epsilon} dG(x) \right)^{\frac{\epsilon}{1-\epsilon}}  \left(\int x^{1-\epsilon} dG(x) - \int x^{1-\epsilon} dF(x) \right) \]

Using integration by parts we get the result.

\medskip

\addcontentsline{toc}{section}{References}
\bibliography{spark-inequality-impact}{}
\bibliographystyle{alpha}

\end{document}


\chapter{Magnetic Field in Matter}
\section{Magnetization}
\begin{wrapfigure}{r}{0.3\textwidth}
	\begin{center}
		\includegraphics[width=0.28\textwidth]{magnetic moment}
	\end{center}
	\caption{magnetic dipole moment}
\end{wrapfigure}

In  electrostatics, we had seen that the electrical polarization of the medium  influences the electrical behavior of substances. In the same way we have internal currents within a material because there are moving charges in atoms and molecules which can create their own magnetic field.Any current loop has a magnetic field and thus has a magnetic dipole moment.There are two types effects here, the first is due to the orbital motion of the electrons and the second is due to the intrinsic spin magnetic moment of the electrons. The second effect is purely quantum mechanical in origin .Like in the electrostatic case, the magnetic moment could be intrinsic to an atom or molecule or it could be induced by an externally applied magnetic field. \par
Ordinarily, the current loops inside an object cancel each other out because of the random orientation of the atoms. But when a magnetic field is applied, a net alignment of these magnetic dipoles occurs, and the medium becomes magnetically polarized, or magnetized.
\\
Let ${m_{i}}$ be the magnetic moment of the $\mathrm{i}$ -th atom inside a matter. We define magnetization as the net magnetic moment per unit volume
$$
\vec{M}=\lim _{\Delta V \rightarrow 0} \frac{\sum_{i} {m_{i}}}{\Delta V}
$$
The magnetization  plays a role analogous to the polarization ${P}$ in electrostatics.  The magnetization  could be paramagnetism, diamagnetism, or even ferromagnetism. We'll discuss it later.
\subsection{Surface and Volume current}
Suppose we have a piece of magnetized material the magnetic dipole moment per unit volume, ${M}$, is given.The vector potential of a single dipole ${m}$ is given by, (We don't do derivation here).
\begin{align*}
\vec{\mathrm{A}}(\mathrm{r})&=\frac{\mu_{0}}{4 \pi} \int \frac{1}{r}\left[\nabla^{\prime} \times \mathrm{M}\left(\mathrm{r}^{\prime}\right)\right] \mathrm{d} \tau^{\prime}+\frac{\mu_{0}}{4 \pi} \oint \frac{1}{\mathrm{r}}\left[\vec{\mathrm{M}}\left(\mathrm{r}^{\prime}\right) \times \vec{\mathrm{d}} \mathrm{a}^{\prime}\right]
\intertext{The first term looks like the potential of a volume current density,}
{J}_{M}&=\nabla \times {M},
\intertext{While the second term looks like the potential of a surface current density,}
{K}_{M}&={M} \times \hat{n}
\intertext{Where $\hat{n}$ is the normal unit vector. With these definitions,}
\vec{A}({r})&=\frac{\mu_{0}}{4 \pi}\left\{\int \frac{{J}_{M}\left({r}^{\prime}\right)}{r} d\tau^{\prime}+\oint \frac{{K}_{M}\left({r}^{\prime}\right)}{r} \vec{d a^{\prime}}\right\}
\end{align*}
\subsection{Auxiliary Field}
There are both free and bound current in a magnetic material. So the total current ${J}$ could be written as
\begin{align*}
\vec{J}&=\mathrm{J}_{\mathrm{b}}+\mathrm{J}_{\mathrm{f}}
\intertext{Where ${J}_{b}$ is bound and ${J}_{f}$ is the free current.}
\intertext{According to Ampere's law,}
\oint \vec{\mathrm{B}} \cdot \mathrm{d} \vec{l}&=\mu_{0} \mathrm{I}_{\mathrm{enc}} \\
\vec{\nabla} \times \vec{\mathrm{B}}&=\mu_{0} \vec{\mathrm{J}}
\intertext{ Let us substitute the value of $J_b$}
\vec{\mathrm{J}}_{\mathrm{b}}&=\vec{\nabla} \times \vec{\mathrm{M}} \\
\frac{1}{\mu_{0}}(\vec{\nabla} \times \vec{\mathrm{B}})&=\vec{\mathrm{J}}\\&=\vec{\mathrm{J}}_{\mathrm{f}}+\vec{\mathrm{J}}_{\mathrm{b}}\\&=\vec{\mathrm{J}}_{\mathrm{f}}+(\vec{\nabla} \times \vec{\mathrm{M}}) \\
\vec{\nabla} \times\left(\frac{1}{\mu_{0}} \vec{\mathrm{B}}-\vec{\mathrm{M}}\right)&=\vec{\mathrm{J}_{\mathrm{f}}}
\intertext{Now, define the quantity inside the curl as ${H}$}
\vec{\mathrm{H}} &\equiv \frac{1}{\mu_{0}} \vec{\mathrm{B}}-\vec{\mathrm{M}}
\intertext{So now the equation becomes}
\vec{\nabla} \times \overrightarrow{\mathrm{H}}&=\overrightarrow{\mathrm{J}}_{\mathrm{f}}
\intertext{Or in integral terms,}
\phi \overrightarrow{\mathrm{H}} \cdot \mathrm{d} \vec{l}&=\mathrm{I}_{\mathrm{fenc}}
\intertext{Where $I_{\text {fenc }}$ is the total free current passing through the Amperian loop. These are the forms of Ampere's law inside matter. $  H $ plays a role in magnetostatics analogous to $D$ in electrostatics:}
\end{align*}
\section{Linear and Non linear medium}
To complete the description of macroscopic magnetostatics, there must be a constitutive relation between ${H}$ and ${B}$.
\begin{align}
\notag {M}&\propto{H}\\
{M}&=\chi_{m} {H}\label{magnetic susceptibility}
\end{align}
 The constant $\chi_{m}$ is called the magnetic susceptibility. It is dimensionless quantity it's typical values are around $10^{-5}$.
 Materials that obey equation.\ref{magnetic susceptibility} are called linear media. For linear media
 \begin{align*}
 {B}&=\mu_{0}({H}+{M})=\mu_{0}\left(1+\chi_{m}\right) {H}
 \intertext{ Thus ${B}$ is also proportional to ${H}$ :}
 {B}&=\mu {H}
  \intertext{Where $\mu=\mu_{0}\left(1+\chi_{m}\right)$ is the permeability of the material.}
\end{align*}
 \section{Diamagnetism, Paramagnetism, Ferromagnetism}
Based on the magnetic response, materials can be classified into the following three major groups:  Diamagnetism,  Paramagnetism,  and Ferromagnetism.
\subsection{Diamagnetism}
Diamagnetism, a very weak fundamental property of all matter, arises due to the non-cooperative behavior of orbiting electrons when exposed to an externally applied magnetic field.
\begin{itemize}
	\item  Very weak; exists ONLY in presence of an external field, non-permanent.
	\item  Applied external field acts on atoms of a material, slightly unbalancing their orbiting electrons, and creates small magnetic dipoles within atoms which oppose the applied field. This action produces a negative magnetic effect known as diamagnetism.
	\item  The induced magnetic moment is small, and the magnetization $(M)$ direction is opposite to the direction of applied field $(H)$.
	\item  Thus the relative permeability is less than unity i.e. magnetic susceptibility $(\chi_{m})$ is negative, and is in order of $-10^{-5}$.
	\item  Materials such as $\mathrm{Cu}, \mathrm{Ag}, \mathrm{Si}$, Ag and alumina are diamagnetic at room temperature.
\end{itemize}
\subsection{Paramagnetism}
Materials which exhibit a small positive magnetic susceptibility in the presence of a magnetic field are called para-magnetic, and the effect is termed as para-magnetism.
This class of the materials has a net magnetic moment due to the existence of unpaired electrons in partially filled orbitals. However, the individual magnetic moments do not interact magnetically and hence the magnetization is zero when there is no external applied field or when the externally applied field is removed.
\begin{itemize}
	\item  Slightly stronger than Diamagnetism. When an external field is applied dipoles line-up with the field, resulting in a positive magnetization. However, the dipoles do not interact.

	\item In the absence of an external field, the orientations of atomic magnetic moments are random leading to no net magnetization.
	\item  When an external field is applied dipoles line-up with the field, resulting in a positive magnetization.
	- However, because the dipoles do not interact, extremely large magnetic fields are required to align all of the dipoles.
	\item  In addition, the effect is lost as soon as the magnetic field is removed.
	\item  Since thermal agitation randomizes the directions of the magnetic dipoles, an increase in temperature decreases the paramagnetic effect.
	\item Magnetic susceptibility $(\chi_{m})$  of these materials is slightly positive, and lies in the range $+10^{-5}$ to $+10^{-2}$
	\item  Para-magnetism is produced in many materials like aluminium, calcium, titanium, alloys of copper.
\end{itemize}

\subsection{Ferromagnetism}
Certain materials possess permanent magnetic moments even in the absence of an external field, These are called Ferromagnetic materials.
The atomic moments in these materials exhibit very strong interactions. These interactions are produced by electronic exchange forces and result in either a parallel or an antiparallel alignment of atomic moments
\begin{itemize}
	\item  Both dia- and para- magnetic materials are considered as non-magnetic because they exhibit magnetization only in presence of an external field.

\item  The permennat dipoles can easily line-up with the imposed magnetic field due to the exchange interaction or mutual reinforcement of the dipoles. These are chrematistics of ferromagnetism.
\item  Materials with ferro-magnetism (Examples: Fe, Co, Ni, Gd) possess magnetic susceptibilities approaching $10^{6}$.
	\item  Above a specific temperature called Curie temperature, ferro-magnetic materials behave as para-magnetic materials and their susceptibility is given by the Curie-Weiss law, defined as
	$$
	\chi_{m}=\frac{C}{T-T_{c}}
	$$
	 Where $C$ - material constant, $T-$ temperature, $T_{c}-$ Curie temperature.
	\item  Ferro Magnets are very strong; dipoles line-up permanently upon application of external field. Has two sub-classes:-
	\begin{itemize}
		\item Anti-ferro-magnetism.
		\item  Ferri-magnetism
	\end{itemize}

\end{itemize}
 \section{Magnetostatic Boundary Conditions}
Just as the electric field suffers a discontinuity at a surface charge, so the magnetic field is discontinuous at a surface current. Only this time it is the tangential component that changes.
\begin{figure}[H]
	\centering
	\includegraphics[height=4cm,width=6.5cm]{3-crop}
	\caption{Wafer thin pillbox}
	\label{Wafer thin pillbox}
\end{figure}
 Indeed, if we apply $\nabla \cdot {B}=0$ in the integral form
$\oint_{S} {B} \cdot  d a=0$\
to a thin gaussian pillbox straddling the surface Figure. \ref{Wafer thin pillbox} , we obtain,
\begin{equation}\label{boundary condition}
B_{above}^{\perp}-B_{below}^{\perp}=0.
\end{equation}
Where $B^{\perp}$ is the component of the magnetic field ${B}$ perpendicular to the surface. Equation.\ref*{boundary condition} tells us that
$B^{\perp}$ is continuous at the interface.
\begin{figure}[H]
	\centering
	\includegraphics[height=4cm,width=6.5cm]{3-crop}
	\caption{Amperian loop}
	\label{Amperian loop}
\end{figure}
\begin{align}
\intertext{As for the tangential components, from Ampere's law $\nabla \times {B}=\mu_{0}{I_{enc}}$ an amperian loop running perpendicular to the current figure.\ref{Amperian loop} yields}
\notag \oint {B} \cdot d {l}&=\left(B_{above}^{\|}-B_{below}^{\|}\right) l\\\oint {B} \cdot d {l}&=\mu_{0}K l .\\
\text{or} \quad B_{above}^{\|}-B_{below}^{\|}&=\mu_{0}K
\intertext{Thus the component of ${B}$ that is parallel to the surface but perpendicular to the current is discontinuous in the amount $\mu_{0}K$.These results can be summarized in a single formula: }
\vec{\mathrm{B}}_{\text {above }}-\vec{\mathrm{B}}_{\text {below }}&=\mu_{0}(\mathrm{~K} \times \hat{\mathrm{n}})
\intertext{Where $\hat{n}$ is a unit vector perpendicular to the surface, pointing "upward". Like the scalar potential of electrostatics the magnetic vector potential is also continuous}
A_{\text {above }}&=A_{\text {below }}
\intertext{But the derivative of vector potential has a discontinuity}
\frac{\partial \mathrm{A}_{\mathrm{above}}}{\partial \mathrm{n}}-\frac{\partial \mathrm{A}_{\text {below }}}{\partial \mathrm{n}}&=-\mu_{0} \mathrm{~K}
\end{align}
\begin{center}
	\framebox{
		\parbox[t][6cm]{7cm}{

			\addvspace{0.2cm} \centering
			\begin{align*}
			B_{above}^{\perp}-B_{below}^{\perp}&=0\\\\
			B_{above}^{\|}-B_{below}^{\|}&=\mu_{0}K\\\\
			A_{\text {above }}&=A_{\text {below }}\\\\ \frac{\partial \mathrm{A}_{\mathrm{above}}}{\partial \mathrm{n}}-\frac{\partial \mathrm{A}_{\text {below }}}{\partial \mathrm{n}}&=-\mu_{0} \mathrm{~K}
			\end{align*}
			} }
\end{center}
\newpage
\begin{abox}
	Previous year question
	\end{abox}
\begin{enumerate}
	\begin{minipage}{\textwidth}
		\item At a surface current, which one of the magnetostatic boundary condition is $\underline{\text { NOT }}$ CORRECT?
		\exyear{GATE 2013}
	\end{minipage}
	\begin{tasks}(1)
		\task[\textbf{A.}]Normal component of the magnetic field is continuous.
		\task[\textbf{B.}]Normal component of the magnetic vector potential is continuous.
		\task[\textbf{C.}]Tangential component of the magnetic vector potential is continuous.
		\task[\textbf{D.}]Tangential component of the magnetic vector potential is not continuous.
	\end{tasks}
	\begin{answer}
		The correct option is \textbf{(d)}
	\end{answer}

\end{enumerate}

Electrodynamics- CSIR/chapter/magnetic field in matter.tex


\documentclass{book}

\usepackage{latexsym, graphics}
\usepackage{graphicx}
\usepackage{amsmath}

\DeclareMathOperator{\Div}{div}
\DeclareMathOperator{\Rot}{rot}
\newcommand{\parder}[2]{\frac{\partial {#1}}{\partial {#2}}}

\setlength{\oddsidemargin}{0.0in}
\setlength{\evensidemargin}{0.0in}
\setlength{\topmargin}{-0.30in}

\setlength{\textheight}{9in}
\setlength{\textwidth}{6in}

\renewcommand{\baselinestretch}{2.0}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newtheorem{lemma}{Lemma}[section]
\newtheorem{theorem}{Theorem}[section]
\newtheorem{definition}{Definition}[section]
\newtheorem{observation}{Observation}[section]
\newtheorem{proposition}{Proposition}[section]
\newtheorem{property}{Property}[section]
\newtheorem{problem}{Problem}[section]
\newtheorem{formulation}{Formulation}[chapter]

\newenvironment{proof}{{\bf Proof:}\ \ }{\begin{flushright} \ \\
                       {$\Box$} \end{flushright}}
\title{The Power Distribution Network Atlas:\\
  An Overview of PDN Design Fundaments }
\author{Ryan Coutts, Eric Zhang, C.K. Cheng}
\date{ }

\begin{document}
\maketitle
\tableofcontents

\chapter{Preface}

This book is not targeted at the engineer who works with modern power distribution networks (PDN) every day.  This book is targeted at the engineer who handles some of the adjacent systems and should know some of the basic considerations that go into PDN design.

\subsection*{Organization}
This book is organized into ... sections.

PDN Atlas Outline (add low level)
1.	Introduction

 a.	Introduction to the PDN

2.	Mathematics Fundamentals

a.	RLC Models (Eric)

b.	Scattering Parameters (Ryan)

c.	Extraction Fundamentals (Ryan)

d.	Simulation Backbones (Eric)

3.	Architecture

a.	Voltage Budgeting and the relationship to timing (Ryan)

b.	Shared vs. Split rail Designs (Eric)

c.	Serial, Differential and Logic Rails and their important parameters (Ryan)

4.	Components

a.	Voltage Regulators (Eric)

b.	Printed Circuit Boards (Eric)

c.	Decoupling Capacitors (Eric)

d.	Substrate and Semiconductor Packaging (Eric)

e.	Die Level Design

i.	Wire resistance at the nm level (Ryan)

ii.	Layout dependent effects (Ryan)

iii.	Statistical analysis (Ryan)

iv.	Power Gating (Ryan)

v.	LDO (Ryan)

5.	Advanced

a.	3D IC and Advanced Packaging Effects (Eric)

b.	Advanced Capacitors (Eric)

c.	Capacitor Optimization (Eric)

d.	Temperature dependent budget effects (Ryan)

e.	Integrated Voltage Regulators (Ryan)

\chapter{Mathematics Fundamentals}
\section{The RLC Model}
\section{Scattering Parameters}

\textbf{Introduction}

Although most PDN discussion start with a parametric evaluation of resistance, inductance and capacitance these three factors alone are not sufficient to describe the physics at work in a PDN system.  In reality, these systems are too large to be described by lumped elements.

For example, resistance is the real component of loss through the network.  An ideal resistor has the same amount of loss at all frequencies, however because of the specific field distribution in wire there are eddy currents which prevent the flow of current down the middle of the wire at high frequencies.  As a result, the current must them flow exclusively on the outer layer of metal forcing the same amount of current through less metal making the loss component of a wire increase with frequency.  The depth at which the current flows is called the skin depth and denoted as $\delta$.

Due to the inability of the RLC model to describe and predict these effects, engineers have started to rely on full field solvers and scattering parameters.  A field solver is a software program which takes geometry, materials and boundary conditions as inputs and solves the electric and magnetic fields within a structure.

There are a variety of field solvers available with different algorithms which usually trade-off accuracy and runtime. The most notable algorithms are the Finite Element Method (FEM) and Finite Difference Time Domain (FDTD).

( More on Algorithms )

Whatever algorithm is chosen to solve the structure the fundamental formulas they are solving are always Maxwell's Equations\cite{maxwells}.  Maxwell's equations are four equations together that describe how electric and magnetics fields behave.  Although these equations are solved the by fields solver, understanding them is an important aspect of using the field solver.

\textbf{Divergence}

( Add Divergence )

\textbf{Curl}

( Add Curl )

\textbf{Electric and Magnetic Fields}

( Add description of magnetic and electric fields)

\textbf{Gauss's Law}

Gauss's law deals with the connection between the electric field and the electric charge stored within the volume.  The law says that the divergence of the electric field through any closed volume is equal to the amount of charge stored within the volume divided by the electric permeability of that volume.  This is written as equation \ref{eq:Gauss}.

\begin{equation} \label{eq:Gauss}
\nabla \cdot \vec{E}=\frac{\rho}{\epsilon_{0}}
\end{equation}

What this means is that electric charge is created by a spatial difference of electric field.  We can relate the electric field to voltage and say that a voltage difference then must contain charge.  This equation then directly relates to the capacitor equation $Q=CV$ which relates the voltage difference to charge.

\textbf{Gauss's Law for Magnetism}

Gauss's law relates the magnetic field to the magnetic charge stored in a volume.  The law says that the divergence of a magnetic field through any closed volume is equal to zero.  This is written as equation \ref{eq:GaussMagnetism}.

\begin{equation} \label{eq:GaussMagnetism}
\nabla \cdot \vec{B}=0
\end{equation}

Fundamentally, this means that magnetic charge does not exist in nature.  This implies that if you have a closed surface with a magnetic field, and charge does not exist, then any magnetic field lines entering the surface must also leave the surface with equal magnitude.  This in effect means that magnetic field lines are always drawn as ellipses that begin and end at the same location.

\textbf{Faraday's Law of Induction}

Faraday's Law of Induction relates to how a changing magnetic field can create an electric field.  The law states that the curl of the electric field is equal to  the negative time derivative of the changing magnetic field. This is written as equation \ref{eq:FaradayLaw}.

\begin{equation} \label{eq:FaradayLaw}
\nabla \times \vec{E} = - \frac{\delta \vec{B}}{\delta t}
\end{equation}

This means that when theres an enclosing magnetic flux which is changing a voltage potential will be generated.

\textbf{Ampere's Circuital Law}

Ampere's Circuital Law relates the time varying electric field and current to the magnetic flux.  It says that current or a changing electric field will create a magnetic field .  This is written as equation \ref{eq:AmperesLaw}

\begin{equation} \label{eq:AmperesLaw}
\nabla \times \vec{B} = \mu_{0} \left(J + \epsilon_{0} \frac{\delta  \vec{E}}{\delta t} \right)
\end{equation}

( Say something about this...)

\textbf{Impulse Response Abstraction}

The geometry and meshing that the field solvers do to obtain great accuracy can be abstracted to better leverage the technology.  The way that we leverage this is that we recognize that the system can be described by a linear time invariant (LTI) formulation.  This means that the system can be characterized at specific locations completely by an impulse response and re-used in future simulations without the field solver at almost no-accuracy loss and a huge speed increase.  This is done typically by leveraging scattering parameters.

\textbf{Scattering Parameters}

Scattering parameters were developed to describe the frequency profile of a linear time-invariant system.  The concept is to take a voltage wave and inject it into a system at a port. Then measure the voltage wave at that port and another port.  This means that S-parameters are essentially formulated with only two ports.  In fact, S-paremeters are always characterized with two ports at a time, but can be extended to any number of ports.

\begin{figure}
\centering
\includegraphics[width=0.7\linewidth]{./Images/SParamaterBlackBox}
\caption{}
\label{fig:SParamaterBlackBox}
\end{figure}

The picture \ref{fig:SParamaterBlackBox} shows a typical 2-Port network.  In order to characterize port 1, $V_{1}^{+}$ is excited with a specific frequency with a port impedance of $R_{p}$ which is usually $50 \Omega $.  The return waves, $V_{x}^{-}$, are then measured in both phase and magnitude and recorded and the s-parameters are then generated for $S_{1x}$. This process is then repeated for the second port. Once complete the system can then be described by the s-parmaeter matrix equation \ref{eq:spar_mtx}.

\begin{equation} \label{eq:spar_mtx}
\begin{bmatrix}
V_{1}^{-} \\  V_{2}^{-}
\end{bmatrix}
=
\begin{bmatrix}
S_{11} & S_{12} \\
S_{21} & S_{22}
\end{bmatrix}
\cdot
\begin{bmatrix}
V_{1}^{+} \\  V_{2}^{+}
\end{bmatrix}
\end{equation}

\begin{equation}
S_{11} = \frac{V_{1}^{-}}{V_{1}^{+}},
S_{12} = \frac{V_{1}^{-}}{V_{1}^{+}},
S_{21} = \frac{V_{1}^{-}}{V_{2}^{+}},
S_{22} = \frac{V_{2}^{-}}{V_{2}^{+}},
\end{equation}

In this formulation, all of the parameters contain a phase and magnitude and are on the imaginary plane.

\textbf{Frequency to Time Domain Conversion}

Once the frequency domain is characterized we can convert to the time domain.  Because the time domain contains all frequency points and the frequency points in the s-parameter are explicitly characterized, we must design the frequency points in the characterization to contain the important frequency content we care about.  If we take too little frequency points to characterize the time domain waveform and ask another tool to use the limited s-parameter information then this can introduce error.  The tools typically have some way of trying to handle the missing information, but none of them are as good as the real data.

When converting from frequency to time domain there are two rules that we must enforce: causality and passivity.

Causality is the concept that the system will not respond until acted on.  This is to say that when computing the impulse response we expect all outputs to have no response before time=0s.  When there is frequency content missing in the s-parameter, the frequency error shows up at all frequencies including before the impulse.  This can cause stability issues in the simulation and as such simulators typically truncate the impulse response to prevent this phenomenon from effecting the results at the expense of accuracy.

Passivity is

\textbf{Impedance Parameters}

In PDN designs we typically have a current load and we want to evaluate the voltage response.  This

\section{Extraction Fundamentals}
\section{Simulation Backbones}

\chapter{Architecture}
\section{Voltage Budget and Timing}
\section{Shared vs. Split Rail Designs}
\section{Serial, Differential, and Logic Rails and their Important Parameters}

\chapter{Components}
\section{Voltage Regulators}
\section{Printed Circuit Boards (PCB)}
\section{Decoupling Capacitors}
\section{Substrate and Semiconductor Packaging}
\section{Die Level Design}
\subsection{Wire Resistance at the nm Level}
\subsection{Layout Dependent Effects}
\subsection{Statistical Analysis}
\subsection{Power Gating}
\subsection{LDO}

\chapter{Advanced}
\section{3D IC and Advanced Packaging Effects}
\section{Advanced Capacitors}
\section{Capacitor Optimization}
\section{Temperature Dependent Budgeting}
\section{Integrated Voltage Regulators}

\begin{thebibliography}{99}
\bibitem{maxwells}
Maxwell's equations - Cite
\end{thebibliography}

\end{document}