% NB: use pdflatex to compile NOT pdftex.  Also make sure youngtab is
% there...

% converting eps graphics to pdf with ps2pdf generates way too much
% whitespace in the resulting pdf, so crop with pdfcrop
% cf. http://www.cora.nwra.com/~stockwel/rgspages/pdftips/pdftips.shtml

\documentclass[10pt,aspectratio=169,dvipsnames]{beamer}

\usetheme[color/block=transparent]{metropolis}

\usepackage[absolute,overlay]{textpos}
\usepackage{booktabs}
\usepackage[utf8]{inputenc}

\usepackage[scale=2]{ccicons}

\usepackage[official]{eurosym}

%use this to add space between rows
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}

\setbeamerfont{alerted text}{series=\bfseries}
\setbeamercolor{alerted text}{fg=Mahogany}
\setbeamercolor{background canvas}{bg=white}

\newcommand{\R}{\mathbb{R}}

\def\l{\lambda}
\def\m{\mu}
\def\d{\partial}
\def\cL{\mathcal{L}}
\def\co2{CO${}_2$}

\def\bra#1{\left\langle #1\right|}
\def\ket#1{\left| #1\right\rangle}
\newcommand{\braket}[2]{\langle #1 | #2 \rangle}
\newcommand{\norm}[1]{\left\| #1 \right\|}
\def\corr#1{\Big\langle #1 \Big\rangle}
\def\corrs#1{\langle #1 \rangle}

% for sources http://tex.stackexchange.com/questions/48473/best-way-to-give-sources-of-images-used-in-a-beamer-presentation

\setbeamercolor{framesource}{fg=gray}
\setbeamerfont{framesource}{size=\tiny}

\newcommand{\source}[1]{\begin{textblock*}{5cm}(10.5cm,8.35cm)
    \begin{beamercolorbox}[ht=0.5cm,right]{framesource}
        \usebeamerfont{framesource}\usebeamercolor[fg]{framesource} Source: {#1}
    \end{beamercolorbox}
\end{textblock*}}

\usepackage{hyperref}

\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\usepackage[europeanresistors,americaninductors]{circuitikz}

%\usepackage[pdftex]{graphicx}

\graphicspath{{graphics/}}

\DeclareGraphicsExtensions{.pdf,.jpeg,.png,.jpg,.gif}

\def\goat#1{{\scriptsize\color{green}{[#1]}}}

\let\olditem\item
\renewcommand{\item}{%
\olditem\vspace{5pt}}

\title{Energy System Modelling\\ Summer Semester 2020, Lecture 4}
%\subtitle{---}
\author{
  {\bf Dr. Tom Brown}, \href{mailto:tom.brown@kit.edu}{tom.brown@kit.edu}, \url{https://nworbmot.org/}\\
  \emph{Karlsruhe Institute of Technology (KIT), Institute for Automation and Applied Informatics (IAI)}
}

\date{}

\titlegraphic{
  \vspace{0cm}
  \hspace{10cm}
    \includegraphics[trim=0 0cm 0 0cm,height=1.8cm,clip=true]{kit.png}

\vspace{5.1cm}

  {\footnotesize

  Unless otherwise stated, graphics and text are Copyright \copyright Tom Brown, 2020.
  Graphics and text for which no other attribution are given are licensed under a
  \href{https://creativecommons.org/licenses/by/4.0/}{Creative Commons
  Attribution 4.0 International Licence}. \ccby}
}

\begin{document}

\maketitle

\begin{frame}

  \frametitle{Table of Contents}
  \setbeamertemplate{section in toc}[sections numbered]
  \tableofcontents[hideallsubsections]
\end{frame}

\section{3-node example from last time}

\begin{frame}
  \frametitle{Solving 3-node example}

  Last time we looked at an example where energy conservation at each
  vertex (Kirchhoff's Current Law, KCL) was not enough information to
  solve the power flow, since there are multiple paths in the network.
  Assume equal reactances $x_\ell = x$ on each edge.

  \vspace{.3cm}

  \begin{columns}
    \column[c]{.5\textwidth}

    \centering

  %https://tex.stackexchange.com/questions/270543/draw-a-graph-in-latex-with-tikz
  \begin{tikzpicture}
    \begin{scope}[every node/.style={circle,thick,draw,fill=yellow}]
      \node (1) at (0,3) {$+6$};
      \node (2) at (3,3) {$0$};
      \node (3) at (1.5,0) {$-6$};
    \end{scope}

    \begin{scope}[>={Stealth[black]},
        every node/.style={fill=white,circle},
        every edge/.style={draw=red,very thick}]
      \path [->] (1) edge node {$?$} (2);
      \path [->] (2) edge node {$?$} (3);
      \path [->] (3) edge node {$?$} (1);
    \end{scope}
  \end{tikzpicture}

      \column[c]{.5\textwidth}

      Formalise by labelling the nodes and edges:

      \vspace{.5cm}

\begin{tikzpicture}
    \begin{scope}[every node/.style={circle,thick,draw,fill=cyan}]%text=white}]
      \node (1) at (0,2.5) {1};
      \node (2) at (2.5,2.5) {2};
      \node (3) at (1.25,.3) {3};
    \end{scope}

    \begin{scope}[>={Stealth[black]},
        every node/.style={fill=white,circle},
        every edge/.style={draw=black,very thick}]
      \path [->] (1) edge node {1} (2);
      \path [->] (2) edge node {2} (3);
      \path [->] (3) edge node {3} (1);
    \end{scope}
  \end{tikzpicture}

      \vspace{.5cm}

We have $p_i = (6,0,-6)$. (Check $\sum_i p_i = 0$.)

Goal is to find $f_\ell$ for $\ell = 1,2,3$.

  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Solving 3-node example: Kirchhoff's Current Law (KCL)}
  \begin{columns}

      \column[c]{.5\textwidth}

      \vspace{.2cm}

\begin{tikzpicture}
    \begin{scope}[every node/.style={circle,thick,draw,fill=cyan}]%text=white}]
      \node (1) at (0,2.5) {1};
      \node (2) at (2.5,2.5) {2};
      \node (3) at (1.25,.3) {3};
    \end{scope}

    \begin{scope}[>={Stealth[black]},
        every node/.style={fill=white,circle},
        every edge/.style={draw=black,very thick}]
      \path [->] (1) edge node {1} (2);
      \path [->] (2) edge node {2} (3);
      \path [->] (3) edge node {3} (1);
    \end{scope}
  \end{tikzpicture}

      \vspace{.3cm}

      Kirchhoff's Current Law gives us:
  \begin{equation*}
    p_i = \sum_\ell K_{i\ell} f_\ell \hspace{2cm} \forall i
  \end{equation*}

  The incidence matrix $K$ is given by:
        \begin{equation*}
\mathbf{K}_{i \ell}=\left(\begin{matrix}
 1 & 0 & -1\\
 -1 & 1 & 0\\
 0 & -1 & 1
\end{matrix}\right)
\end{equation*}
      \column[c]{.5\textwidth}
        So we get:
        \begin{align*}
          p_1 & = 6 = f_1 - f_3 \\
          p_2 & = 0 = f_2 - f_1 \\
          p_3 & = -6 = f_3 - f_2
        \end{align*}
        Sum of KCL equations is always zero, so
        reduce to $N-1 = 2$ independent equations:
        \begin{align*}
          6 & = f_1 - f_3 \\
          0 & = f_2 - f_1
        \end{align*}
        Not enough information to solve!

        Need more information from KVL and reactances.

  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Solving 3-node example: Kirchhoff's Voltage Law (KVL)}
  \begin{columns}

      \column[c]{.5\textwidth}

      \vspace{.2cm}

\begin{tikzpicture}
    \begin{scope}[every node/.style={circle,thick,draw,fill=cyan}]%text=white}]
      \node (1) at (0,2.5) {1};
      \node (2) at (2.5,2.5) {2};
      \node (3) at (1.25,.3) {3};
    \end{scope}

    \begin{scope}[>={Stealth[black]},
        every node/.style={fill=white,circle},
        every edge/.style={draw=black,very thick}]
      \path [->] (1) edge node {1} (2);
      \path [->] (2) edge node {2} (3);
      \path [->] (3) edge node {3} (1);
    \end{scope}
  \end{tikzpicture}

      \vspace{.1cm}

      One formulation of Kirchhoff's Voltage Law gives us $L-N+1$ equations for cycles:
  \begin{equation*}
    \sum_\ell C_{\ell c} x_\ell f_\ell = 0 \hspace{2cm} \forall c
  \end{equation*}
  The cycle matrix $C$ is given by:
        \begin{equation*}
\mathbf{C}_{\ell c}=\left(\begin{matrix}
 1 \\
 1\\
 1
\end{matrix}\right)
\end{equation*}
      \column[c]{.5\textwidth}
        For equal reactances $x_\ell = x$ we get:
        \begin{align*}
          \sum_\ell C_{\ell 1} x_\ell f_\ell = x(f_1 + f_2 + f_3) = 0
        \end{align*}
        Together with KCL equations we now have 3 independent equations for 3 unknowns. Solve:
        \begin{align*}
          f_1 & = 2 \\
          f_2 & = 2 \\
          f_3 & = -4
        \end{align*}

  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Solving 3-node example: Solution}

  \vspace{.3cm}

  \begin{columns}

      \column[c]{.5\textwidth}

  Solution:
  \vspace{.3cm}

    \centering
  %https://tex.stackexchange.com/questions/270543/draw-a-graph-in-latex-with-tikz
  \begin{tikzpicture}
    \begin{scope}[every node/.style={circle,thick,draw,fill=yellow}]
      \node (1) at (0,3) {$+6$};
      \node (2) at (3,3) {$0$};
      \node (3) at (1.5,0) {$-6$};
    \end{scope}

    \begin{scope}[>={Stealth[black]},
        every node/.style={fill=white,circle},
        every edge/.style={draw=red,very thick}]
      \path [->] (1) edge node {$2$} (2);
      \path [->] (2) edge node {$2$} (3);
      \path [->] (3) edge node {$-4$} (1);
    \end{scope}
  \end{tikzpicture}

  \raggedright

      \column[c]{.5\textwidth}

      Along 2-edge path reactance is double the 1-edge path, so half as much power flows along the 2-edge path as the 1-edge path.

      \vspace{.5cm}
  NB: For directed graph, sign determines direction of flow.

  \end{columns}
\end{frame}

\section{Full power flow equations}

\begin{frame}
  \frametitle{Goal: Understand the physical origin of these equations}

  Last time we said we can (in the linear approximation) express the
  flow $f_\ell$ on each line in terms of the voltage angles $\theta_i$
  at the nodes for a line $\ell$ with
  reactance $x_\ell$ as
  \begin{equation*}
    f_\ell = \frac{\theta_i - \theta_j}{x_\ell} = \frac{1}{x_\ell}\sum_{i} K_{i\ell} \theta_i
  \end{equation*}
   This is a relative of Ohm's Law in DC circuits, $I = \frac{V_1 - V_2}{R}$.

  Now we explain the physics of where this comes from, and the linear approximation
  that leads to it.

  This is also useful when we consider the synchronisation of
  oscillators later.
\end{frame}

\begin{frame}
  \frametitle{Alternating Current}

  The majority of electrical power, including what you get out of a
  wall plug, is transmitted as \alert{Alternating Current (AC)},
  i.e. both the voltage and current are sinusoidal waves.

  \begin{center}
  \includegraphics[width=8cm]{738px-Types_of_current}
  \end{center}

  [Some power is transmitted as \alert{Direct Current (DC)} under bodies of water and
    indeed many electronic devices require DC (must convert AC to DC).]
  \source{Wikipedia}
\end{frame}

\begin{frame}
  \frametitle{Why alternating current?}

  Battle of currents! Edison versus Westinghouse/Tesla in late 1880s, early 1890s, etc.

  \url{https://en.wikipedia.org/wiki/War_of_Currents}

  AC won, because it's easy to transform AC to a higher voltage, so
  you can transmit a given power $P = VI$ with a lower current and thus avoid
  the $I^2R$ resistive losses in power lines.

  Reason: $\frac{d}{dt}$ in $\mathcal{E} = \frac{d\Phi}{dt}$; use a
  solenoid to induce a \alert{fluctuating} magnetic field in another
  solenoid with a different number of turns, giving different
  potential difference.

  Frequency of 50~Hz is uniform across Europe (except for
  train-electricity, e.g. in Germany 16.7~Hz). 60~Hz in USA, western half of
  Japan, etc.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Frankfurt: Home of Long-Distance AC Transmission}

  First long-distance high-voltage alternating-current transmission in 1891 from hydroelectric
  plant in Lauffen to Frankfurt for the Elektrotechnische Ausstellung (176~km, 15~kV).

  \begin{columns}[T]
\begin{column}{3.5cm}
  \includegraphics[trim=0 0cm 0 0cm,width=3.7cm,clip=true]{IEAFrankfurt1891c.jpg}
\end{column}
\begin{column}{2.5cm}
  \includegraphics[trim=0 0cm 0 0cm,width=2.8cm,clip=true]{Drehstromuebertragung_Lauffen-Frankfurt.png}
\end{column}
\begin{column}{5cm}
  \vspace{1cm}
        \includegraphics[trim=0 0cm 0 0cm,width=5cm,clip=true]{Lauffen-Frankfurt1891-1991.jpg}
\end{column}
  \end{columns}

  \source{Wikipedia}

\end{frame}

\begin{frame}
  \frametitle{Sinuisoidal waves}

  The voltage is usually written in terms of the \alert{angular frequency} $\omega =
  2\pi f$ (radians per second) rather than frequency $f$ (Hertz) and the \alert{Root-Mean-Squared (RMS)} voltage magnitude $V_{\textrm{rms}}$
  \begin{equation*}
    V(t) = V_{\textrm{peak}} \sin(\omega t) = \sqrt{2} V_{\textrm{rms}} \sin(\omega t)
  \end{equation*}
  Similarly for the current we have
  \begin{equation*}
    I(t) = I_{\textrm{peak}} \sin(\omega t - \varphi) = \sqrt{2} I_{\textrm{rms}} \sin(\omega t - \varphi)
  \end{equation*}
  Note that they are not necessarily in phase, $\varphi \neq 0$.

  The RMS values are useful because then for the \alert{average power} with $\varphi = 0$ we can forget factors of 2
  \begin{equation*}
    \langle P(t) \rangle = \langle V(t)I(t) \rangle = 2 V_{\textrm{rms}} I_{\textrm{rms}} \langle\sin^2(\omega t)\rangle = V_{\textrm{rms}} I_{\textrm{rms}}
  \end{equation*}

\end{frame}

\begin{frame}
  \frametitle{Resistive loads}

  For purely \alert{resistive loads}, e.g. a kettle or an electric heater, we have
  \begin{equation*}
    V(t) = R I(t)
  \end{equation*}
  and thus for a voltage of $V(t) = \sqrt{2} V_{\textrm{rms}}
  e^{j\omega t}$ (NB: for engineers $j = \sqrt{-1}$ to avoid confusion
  with the current $i$) we have
  \begin{equation*}
    I(t) = \sqrt{2} \frac{V_{\textrm{rms}}}{R} e^{j\omega t} = \frac{1}{R} V(t)
  \end{equation*}
  or in terms of the RMS value and phase shift
  \begin{align*}
    I_{\textrm{rms}} & = \frac{1}{R} V_{\textrm{rms}} \\
    \varphi & = 0
  \end{align*}

\end{frame}

\begin{frame}
  \frametitle{Resistive loads}

  In terms of the waveforms, the current has no phase shift from the voltage.

  \centering
  \includegraphics[width=11cm]{vi-r.pdf}

\end{frame}

\begin{frame}
  \frametitle{Capacitive loads}

  For purely \alert{capacitive loads} we have
  \begin{equation*}
       I(t) = C  \frac{dV(t)}{dt}
  \end{equation*}
  and thus for a voltage of $V(t) = \sqrt{2} V_{\textrm{rms}}
  e^{j\omega t}$ we get
  \begin{equation*}
    I(t) = \sqrt{2} j\omega C V_{\textrm{rms}} e^{j\omega t} = j\omega C V(t)
  \end{equation*}
  or in terms of the RMS value and phase shift
  \begin{align*}
    I_{\textrm{rms}} & = \omega C V_{\textrm{rms}} \\
    \varphi & = -\frac{\pi}{2}
  \end{align*}
  We write $X_{C} = \frac{1}{\omega C}$ for the \alert{capacitive reactance}.

\end{frame}

\begin{frame}
  \frametitle{Capacitive loads}

  Current peaks before the voltage (it \alert{leads} the voltage),
  since first charge must accumulate on the plates; once the charge is
  on the plates, the current drops to zero and the voltage peaks.

  \begin{columns}
    \column[c]{.7\textwidth}
  \includegraphics[width=10cm]{vi-c.pdf}
    \column[c]{.3\textwidth}
  \includegraphics[width=4cm]{capacitor.png}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Inductive loads}

  For purely \alert{inductive loads}, e.g. a motor during start-up
  \begin{equation*}
    V(t) = L \frac{d I(t)}{dt}
  \end{equation*}
  and thus for a voltage of $V(t) = \sqrt{2} V_{\textrm{rms}}
  e^{j\omega t}$ we get
  \begin{equation*}
    I(t) = \sqrt{2} \frac{V_{\textrm{rms}}}{j\omega L} e^{j\omega t} = \frac{1}{j\omega L}V(t)
  \end{equation*}
  or in terms of the RMS value and phase shift
  \begin{align*}
    I_{\textrm{rms}} & = \frac{1}{\omega L} V_{\textrm{rms}} \\
    \varphi & = \frac{\pi}{2}
  \end{align*}
  We write $X_{L} = \omega L$ for the \alert{inductive reactance}, in analogy to the resistance.

\end{frame}

\begin{frame}
  \frametitle{Inductive loads}

  Now current peaks after the voltage (it \alert{lags} the voltage),
  since the flow of current in the solenoid resists the changing voltage.

  \begin{columns}
    \column[c]{.7\textwidth}
  \includegraphics[width=10cm]{vi-l.pdf}
    \column[c]{.3\textwidth}
  \includegraphics[width=4cm]{600px-Solenoid_and_Ampere_Law_-_2.png}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{General loads}

  General loads will have a combination of resistive, capacitive and
  inductive parts. For an RLC circuit in series the voltage across the
  components is additive
  \begin{equation*}
    V(t) = R I(t) + L\frac{dI(t)}{dt} + \frac{1}{C} \int_{-infty}^t I(\tau) d\tau
  \end{equation*}
  and therefore for a sinuisoidal voltage with angular frequency $\omega$ we get
  \begin{equation*}
    V(t) = \left[ R + j\omega L + \frac{1}{j\omega C} \right] I(t)
  \end{equation*}
  which leads us to define a general complex notion of resistance called \alert{impedance}
  \begin{equation*}
    Z =  R + j\omega L + \frac{1}{j\omega C} = R + j(X_L - X_C) = R + jX
  \end{equation*}
  where $X$ is the reactance $X = X_L - X_C$.
\end{frame}

\begin{frame}
  \frametitle{Impedances and admittances}

  Thus for a regular sinuisodal setup we have
  \begin{equation*}
    V(t) = ZI(t)
  \end{equation*}
  where the complex \alert{impedance} takes care both of the relation
  of the RMS values of the current and the voltage, and their phase
  difference. We can decompose $Z$ into real resistance $R$ and real reactance $X$
  \begin{equation*}
    Z = R + jX
  \end{equation*}

  The inverse impedance, called the \alert{admittance} is given by
  \begin{equation*}
    Y = \frac{1}{Z}
  \end{equation*}
  so that
  \begin{equation*}
    I(t) = Y V(t)
  \end{equation*}
  We can also decompose this into real conductance $G$ and real susceptance $B$
  \begin{equation*}
    Y = G + jB
  \end{equation*}

\end{frame}

\begin{frame}
  \frametitle{Simple transmission line}

  A simple model for a transmission line $\ell$ between nodes $i$ and
  $j$ is a resistance $R$ in series with an (inductive) reactance $X$.

  [Typical values are for a 380~kV overhead transmission line e.g. $R = 0.03$~Ohm/km and $X = 0.3$~Ohm/km.]

  The voltage at each node (compared to ground) is given by $V_i(t) = \sqrt{2}
  V_ie^{j(\omega t + \theta_i)}$ where $\theta_i$ is the phase offset
  for each node and $V_i$ is the RMS voltage magnitude.

  Now the current in the transmission line is given by
  \begin{equation*}
    I(t) = \frac{1}{R + jX} \left[ V_j(t) - V_i(t) \right] =  \frac{1}{R + jX}\sqrt{2} V_i e^{j(\omega t + \theta_i)} \left[\frac{V_j}{V_i} e^{j(\theta_j - \theta_i)} - 1\right]
  \end{equation*}

\end{frame}

\begin{frame}
  \frametitle{Active versus reactive power}

  Now let's consider the power injection at the first node. This is
  simply the voltage there multiplied by the current in the
  transmission line.

  It's convenient to eliminate the time-dependent part $e^{j\omega t}$
  by multiplying the voltage with the complex conjugate of the current
  \begin{equation*}
    S = P + jQ = \frac{1}{2} V(t)I^*(t)
  \end{equation*}

  For a resistive load with $V(t) = R I(t)$ this reproduces the
  \alert{active power} $P$.

  For loads where the $I(t)$ is not in phase with the voltage, we get
  a flow of \alert{reactive power} $Q$.

  $S = P + j Q$ is called the \alert{apparent power}.

\end{frame}

\begin{frame}
  \frametitle{Linearisation: Assumption 1/3}

  Now if we consider the power injected at the first node we get
  \begin{equation*}
    P_i + jQ_i =  \frac{1}{R + jX} V_i^2\left[\frac{V_j}{V_i} e^{j(\theta_i - \theta_j)} - 1\right]
  \end{equation*}

  This is the full non-linear equation for the power flow. Now let's
  linearise by making some simplifying assumptions.

  1. Assume the voltage magnitudes are the same everywhere in the network $V_i = V_j$
  \begin{equation*}
    P_i + jQ_i =  \frac{1}{R + jX} V_i^2\left[e^{j(\theta_i - \theta_j)} - 1\right]
  \end{equation*}
  This means \alert{power flows primarily according to angle differences} in this approximation.

\end{frame}

\begin{frame}
  \frametitle{Linearisation: Assumption 2/3}

  2. Now assume that the voltage angle differences across the transmission line are small enough that $\sin(\theta_i - \theta_j) \sim (\theta_i - \theta_j)$
  \begin{align*}
    P_i + jQ_i & =  \frac{1}{R + jX} V_i^2\left[e^{j(\theta_i - \theta_j)} - 1\right] \\
    & \sim  \frac{1}{R + jX} V_i^2\left[j(\theta_i - \theta_j)\right]
  \end{align*}

  This assumption is usually valid, since for stability reasons, we usually have in the transmission network
  $(\theta_i - \theta_j) \leq \frac{\pi}{6}$ (30 degrees).
\end{frame}

\begin{frame}
  \frametitle{Linearisation: Assumption 3/3}

  3. Finally we assume $R << X$ so that we can ignore the resistance $R$
  \begin{align*}
    P_i + jQ_i
    & =  \frac{1}{R + jX} V_i^2\left[j(\theta_i - \theta_j)\right] \\
    & \sim  \frac{1}{jX} V_i^2\left[j(\theta_i - \theta_j)\right]\\
    & =  \frac{V_i^2}{X}(\theta_i - \theta_j)
  \end{align*}

  Note that ignoring $R$ means that we ignore resistive losses in the transmission lines and also since $Q_i \sim 0$, we ignore the flow of reactive power. Finally we absorb the voltage into the definition of the \alert{per unit} reactance $x_\ell = \frac{X}{V_i^2}$ to get
  \begin{equation*}
    f_\ell = P_i = -P_j = \frac{\theta_i - \theta_j}{x_\ell}
  \end{equation*}

\end{frame}

\begin{frame}
  \frametitle{Three-phase power}

  Electricity is generally generated simultaneously in
    3 separate circuits separate by 120 degrees or $\frac{2\pi}{3}$
  \begin{center}
  \includegraphics[width=7cm]{3_phase_AC_waveform}
  \end{center}

  In your plug, you only see one phase, but your oven may use all
  three phases.

  \source{Wikipedia}
\end{frame}

\begin{frame}
  \frametitle{Three-phase power}

  Why three phases? This was settled in the late 1880s.

  1. The total power delivery is constant
  \begin{equation*}
    \frac{d}{dt} P(t) =     \frac{d}{dt} \left[P_a(t) + P_b(t) + P_c(t) \right] = 0
  \end{equation*}
  This reduces mechanical stress on generators and motors.

  2. The sum of voltages and currents is zero, so no return path
  required! Saving on materials.

  Both facts follow from
  \begin{equation*}
    \sum_{k=0}^{N-1} e^{j\frac{2\pi k}{N}} = 0
  \end{equation*}
  for $N > 1$.

  3. Why $N=3$ rather than $N=2$? Allows directional rotating fields for induction motors (thanks Tesla!).
\end{frame}

\begin{frame}
  \frametitle{Roots of unity for $N=3$}

  For $N=3$, check they add up to zero:

  \centering
  \includegraphics[width=8cm]{f8UGvjRAqV-3rd-roots-of-unity.png}

  \source{\href{https://brilliant.org/wiki/roots-of-unity/}{brilliant.org}}
\end{frame}

\begin{frame}
  \frametitle{Rotating field in a three-phase induction motor}

  A brilliant insight (credited to Tesla, but the history is
  \href{https://en.wikipedia.org/wiki/Induction\_motor\#History}{complicated})
  was that with three-phase power, you can place your wires spaced at
  $2\pi/3$ to create a \alert{rotating} magnetic field

  \url{https://www.youtube.com/watch?v=LtJoJBUSe28}

  which can then induce a current in a rotor cage, which then
  experiences a torque thanks to the magnetic field: this is the
  principle of the \alert{induction motor}.

  It would not be possible to create such a rotating field with a
  single-phase or two-phase system.

\end{frame}

\begin{frame}
  \frametitle{Three-phase power}
  \begin{center}
  \includegraphics[width=9cm]{circuit}
  \end{center}

  \source{\href{https://en.wikipedia.org/wiki/Three-phase_electric_power}{Wikipedia}}

\end{frame}

\section{Computing the Linear Power Flow}

\begin{frame}
  \frametitle{The goal of power flow analysis}

  \begin{columns}
    \column[c]{.55\textwidth}

  The goal of a power/load flow analysis is to find the flows in the
  lines of a network given a power injection pattern at the nodes.

    I.e. given power injection at the nodes
\begin{equation*}
\mathbf{P}_{i}=\left(\begin{matrix}
50 \\
50 \\
0 \\
-100
\end{matrix}\right)
\end{equation*}
what are the flows in lines 1-4?

\vspace{.1cm}

To find the flows, it is sufficient to know the \alert{reactances} of
the lines $x_\ell$ and the \alert{voltages angles} $\theta_i$ at each node.

\column[c]{.45\textwidth}

\begin{tikzpicture}
    \begin{scope}[every node/.style={circle,thick,draw,fill=cyan}]%text=white}]
      \node (4) at (0,2.5) {4};
      \node (2) at (2.5,2.5) {2};
      \node (3) at (1.25,.3) {3};
      \node (1) at (4.2,4.2) {1};
    \end{scope}

    \begin{scope}[>={Stealth[black]},
        every node/.style={fill=white,circle},
        every edge/.style={draw=black,very thick}]
      \path [->] (1) edge node {1} (2);
      \path [->] (2) edge node {2} (3);
      \path [->] (3) edge node {4} (4);
      \path [->] (2) edge node {3} (4);
    \end{scope}
  \end{tikzpicture}
\end{columns}

\end{frame}

\begin{frame}
  \frametitle{Framing the load flow problem}

  Suppose we have $N$ nodes labelled by $i$, and $L$ edges labelled by
  $\ell$ forming a directed graph $G$.

  Suppose at each node we have a \alert{power imbalance} $p_i$ ($p_i >
  0$ means its generating more than it consumes and $p_i < 0$ means it
  is consuming more than it).

  Since we cannot create or destroy energy (and we're ignoring losses):
  \begin{equation*}
    \sum_i p_i = 0
  \end{equation*}

  \alert{Question}: How do the flows $f_\ell$ in the network relate to the nodal power
  imbalances?

  \alert{Answer}: According to the reactances (generalisation of
  resistance for oscillating voltage/current) and the corresponding
  voltages.

\end{frame}

\begin{frame}
  \frametitle{Kirchhoff's Current Law (KCL)}

  KCL says (in this linear setting) that the nodal power imbalance at
  node $i$ is equal to the sum of direct flows arriving at the
  node. This can be expressed compactly with the incidence matrix

  \begin{equation*}
    p_i = \sum_\ell K_{i\ell} f_\ell \hspace{2cm} \forall i
  \end{equation*}

  Only $N-1$ of these equations are independent for a connected network, since $\sum_i K_{i\ell} = 0$.
\end{frame}

\begin{frame}
  \frametitle{Kirchhoff's Voltage Law (KVL)}

  KVL says that the sum of voltage differences across edges for any
  closed cycle must add up to zero.

  If the voltage angle at any node is given by $\theta_i$ then the voltage difference across edge $\ell$ is
  \begin{equation*}
    \sum_i K_{i\ell} \theta_i
  \end{equation*}

  And Kirchhoff's law can be expressed using the cycle matrix encoding of independent cycles
  \begin{equation*}
    \sum_\ell C_{\ell c} \sum_i K_{i\ell} \theta_i = 0 \hspace{2cm} \forall c
  \end{equation*}

  [Automatic, since we already said KC = 0.]

\end{frame}

\begin{frame}
  \frametitle{Kirchhoff's Voltage Law (KVL)}

  Physics gives us the expression of the flow $f_\ell$ on each line $\ell$ with reactance $x_\ell$ in terms of the voltage angles at the nodes $\theta_i$  (a
  relative of $V = IR$)
  \begin{equation}
    f_\ell = \frac{\theta_i - \theta_j}{x_\ell} = \frac{1}{x_\ell}\sum_{i} K_{i\ell} \theta_i \label{eq:1}
  \end{equation}
      [NB: This restricts the $L$ variables $f_\ell$ to depend only on the $N$ voltage angles $\theta_i$. Since the flow doesn't change under a constant shift $\theta_i \to \theta_i + c$, we can choose   a \alert{slack} or \alert{reference node} such that  $\theta_1 = 0$, so there are only $N-1$ independent variables.]

      \vspace{.5cm}

  KVL now becomes $L-N+1$ binding constraints on the line flows $f_\ell$
  \begin{equation}
    \sum_\ell C_{\ell c} x_\ell f_\ell = 0 \hspace{2cm} \forall c  \label{eq:2}
  \end{equation}
  [NB: Equations \eqref{eq:1} and  \eqref{eq:2} are equivalent and both restrict our $L$ variables $f_\ell$ to an $N-1$ dimensional subspace.]

\end{frame}

\begin{frame}
  \frametitle{Solving the equations via the line flows}

  Now we have $N-1$ equations for the flows $f_\ell$ from KCL:
  \begin{equation*}
    p_i = \sum_\ell K_{i\ell} f_\ell \hspace{2cm}\forall i \in \{ 1, \dots N-1 \}
  \end{equation*}
  and $L-N+1$ equations from KVL:
  \begin{equation*}
    \sum_\ell C_{\ell c} x_\ell f_\ell = 0 \hspace{2cm}\forall c \in \{ 1, \dots L-N+1 \}
  \end{equation*}

  So $L$ independent linear equations for $L$ variables $f_\ell$.

  Can solve with e.g. LU decomposition using specialised sparse solvers, with polynomial complexity in $L$. (For dense matrices complexity $O(L^a)$ where $2 < a < 3$.)

  This formulation is useful for the optimisation later, but we can solve a smaller dimensional linear system with $N-1$ variables using the voltage angles.
\end{frame}

\begin{frame}
  \frametitle{Solving the equations via the voltage angles}

  If we combine
    \begin{equation}
    f_\ell  = \frac{1}{x_\ell}\sum_{i} K_{i\ell} \theta_i \label{eq:3}
  \end{equation}
    with Kirchhoff's Current Law we get
    \begin{equation*}
    p_i = \sum_{\ell} K_{i\ell}f_\ell =   \sum_{\ell} K_{i\ell} \frac{1}{x_\ell}\sum_{j} K_{j\ell} \theta_j
    \end{equation*}
    This is a \alert{weighted Laplacian}. If we write $B_{k\ell}$ for the diagonal matrix with $B_{\ell\ell} = \frac{1}{x_\ell}$ then
    \begin{equation*}
      L = KBK^t
    \end{equation*}
    and we get a \alert{discrete Poisson equation} for the $\theta_i$ sourced by the $p_i$
    \begin{equation*}
      p_i = \sum_{j} L_{ij} \theta_j
    \end{equation*}
    This is a set of $N-1$ sparse linear equations for the $\theta_j$ ($N-1$ since $\sum_i L_{ij} = 0$).
    We can solve this for the $\theta_i$ and then find the flows using equation \eqref{eq:3}. Polynomial complexity in $N$.

\end{frame}

\begin{frame}
  \frametitle{Solving the equations via the PTDF}

  If we are repeating the calculation for a fixed network multiple times with different power injections, it can make sense to do the full matrix inversion.

  Given $p_i$ at every node, we want to find the flows $f_\ell$. We
  have the equations
    \begin{align*}
      p_i & = \sum_{j} L_{ij} \theta_j \\
     f_\ell  & = \frac{1}{x_\ell}\sum_{i} K_{i\ell} \theta_i
    \end{align*}

    Basic idea: invert $L$ to get $\theta_i$ in terms of $p_i$
    \begin{equation*}
      \theta_i  = \sum_{k} (L^{-1})_{ik} p_k
    \end{equation*}
    then insert to get the flows as a linear function of the power injections $p_i$
    \begin{equation*}
    f_\ell   = \frac{1}{x_\ell}\sum_{i,k} K_{i\ell}  (L^{-1})_{ik} p_k = \sum_k \textrm{PTDF}_{\ell k} p_k
    \end{equation*}
    called the \alert{Power Transfer Distribution Factors} (PTDF).

\end{frame}

\begin{frame}
  \frametitle{Inverting Laplacian $L$}

  There is one small catch: $L$ is \alert{not invertible} since we saw last
  time it has (for a connected network) one zero eigenvalue, with
  eigenvector $(1,1, \dots 1)$, since by construction $\sum_j L_{ij} =
  0$.

  This is related to a gauge freedom to add a constant to all voltage angles
  \begin{equation*}
    \theta_i \to \theta_i + c
  \end{equation*}
  which does not affect physical quantities:
    \begin{align*}
      p_i & = \sum_{j} L_{ij} (\theta_j+ c) = \sum_{j} L_{ij} (\theta_j)  \\
     f_\ell  & = \frac{1}{x_\ell}\sum_{i} K_{i\ell}( \theta_i  + c) = \frac{1}{x_\ell}\sum_{i} K_{i\ell}( \theta_i )
    \end{align*}

    Typically choose a \alert{slack} or \alert{reference node} such that  $\theta_1 = 0$.

\end{frame}

\begin{frame}
  \frametitle{Inverting Laplacian $L$}

  Two solutions:

  1. Since $\theta_1 = 0$ and $p_1$ is not independent of the other
  power injections ($\sum_{i=1}^N p_i = 0$ implies $p_1 = -
  \sum_{i=2}^N p_i$), we can ignore these elements and invert
  the lower-right $(N-1) \times (N-1)$ part of $L$ (which doesn't have zero eigenvalues) to find the
  remaining $\{\theta_i\}_{i=2,\dots N}$ in terms of the
  $\{p_i\}_{i=2,\dots N}$.

  2. Use the Moore-Penrose pseudo-inverse.

  Write $L$ in terms of its basis of orthonormal eigenvectors $e^n_i$ ($\sum_j L_{ij} e^n_j = \l_n e^n_i$, $\sum_i e^n_i e^n_i = 1$ and  $\sum_i e^n_i e^m_i = 0$ if $n \neq m$):
  \begin{equation*}
    L_{ij} = \sum_n \l_n e^n_i e^n_j
  \end{equation*}
  then the Moore-Penrose pseudo-inverse is:
  \begin{equation*}
    L^\dagger_{ij} = \sum_{n | \l_n \neq 0} \frac{1}{\l_n} e^n_i e^n_j
  \end{equation*}

\end{frame}

\begin{frame}
  \frametitle{Check the Moore-Penrose pseudo-inverse}

  Let's check the Moore-Penrose pseudo-inverse really gives us an inverse:
  \begin{align*}
    \sum_{j} L_{ij}L^\dagger_{jk} &= \sum_j \sum_n \l_n e^n_i e^n_j  \sum_{m | \l_m \neq 0} \frac{1}{\l_m} e^m_j e^m_k \\
&= \sum_n \l_n e^n_i  \sum_{m | \l_m \neq 0} \frac{1}{\l_m} e^m_k \sum_j   e^n_j e^m_j \\
    & =   \sum_{m | \l_m \neq 0} \frac{\l_m}{\l_m} e^m_i e^m_k\\
    & =   \sum_{m | \l_m \neq 0}  e^m_i e^m_k
  \end{align*}
  From line 2 to 3 we use the orthogonality of the eigenvectors.

  This is almost the identity. It has eigenvalues 1 for each eigenvector $e^n_k$ except for zero eigenvectors of $L$ with $\l_n = 0$, which it annihilates.

\end{frame}

\begin{frame}
  \frametitle{4-node example}

  \begin{columns}
\column[c]{.5\textwidth}
\begin{equation*}
\mathbf{K}_{i \ell}=\left(\begin{matrix}
1 & 0 & 0 & 0\\
-1 & 1 & 1 & 0\\
0 & -1 & 0 & 1\\
0 & 0 & -1 & -1
\end{matrix}\right)
\end{equation*}
\begin{equation*}
\mathbf{L}_{ij}=\left(\begin{matrix}
1 & -1 & 0 & 0\\
-1 & 3 & -1 & -1\\
0 & -1 & 2 & -1\\
0 & -1 & -1 & 2
\end{matrix}\right)
\end{equation*}
\begin{equation*}
\mathbf{PTDF}_{\ell i}=\left(\begin{matrix}
0 & -1 & -1 & -1\\
0 & 0 & -2/3 & -1/3\\
0 & 0 & -1/3 & -2/3\\
0 & 0 & 1/3 & -1/3
\end{matrix}\right)
\end{equation*}

\column[c]{.5\textwidth}

\begin{tikzpicture}
    \begin{scope}[every node/.style={circle,thick,draw,fill=cyan}]%text=white}]
      \node (4) at (0,2.5) {4};
      \node (2) at (2.5,2.5) {2};
      \node (3) at (1.25,.3) {3};
      \node (1) at (4.2,4.2) {1};
    \end{scope}

    \begin{scope}[>={Stealth[black]},
        every node/.style={fill=white,circle},
        every edge/.style={draw=black,very thick}]
      \path [->] (1) edge node {1} (2);
      \path [->] (2) edge node {2} (3);
      \path [->] (3) edge node {4} (4);
      \path [->] (2) edge node {3} (4);
    \end{scope}
  \end{tikzpicture}
\end{columns}

\end{frame}

\begin{frame}
  \frametitle{4-node example}

  \begin{columns}
\column[c]{.65\textwidth}
\begin{align*}
\sum_i \mathbf{PTDF}_{\ell i}p_i & =\left(\begin{matrix}
0 & -1 & -1 & -1\\
0 & 0 & -2/3 & -1/3\\
0 & 0 & -1/3 & -2/3\\
0 & 0 & 1/3 & -1/3
\end{matrix}\right)\left(\begin{matrix}
50\\
50 \\
0\\
-100
\end{matrix}\right) \\
& =\left(\begin{matrix}
50\\
33.3 \\
66.7 \\
33.3
\end{matrix}\right)
\end{align*}

\column[c]{.35\textwidth}

\begin{tikzpicture}
    \begin{scope}[every node/.style={circle,thick,draw,fill=yellow}]
      \node (4) at (0,2.5) {-100};
      \node (2) at (2.5,2.5) {+50};
      \node (3) at (1.25,.3) {0};
      \node (1) at (4.2,4.2) {+50};
    \end{scope}

    \begin{scope}[>={Stealth[black]},
                every node/.style={fill=white,circle},
        every edge/.style={draw=red,very thick}]
      \path [->] (1) edge node {50} (2);
      \path [->] (2) edge node {33.3} (3);
      \path [->] (3) edge node {33.3} (4);
      \path [->] (2) edge node {66.7} (4);
    \end{scope}
  \end{tikzpicture}
\end{columns}

\end{frame}

\begin{frame}
  \frametitle{PTDF as sensitivity}

  Can also `experimentally' determine the Power Transfer Distribution
  Factors (PTDF) by choosing a slack node (in this case node 1).

  Each column (labelled by $i$) is then the resulting line flows if we have
  a simple power transfer from node $i$ to the slack $p_i = 1$ and $p_1
  = -1$.

  \begin{columns}
\column[c]{.5\textwidth}
\begin{equation*}
\mathbf{PTDF}_{\ell i}=\left(\begin{matrix}
0 & -1 & -1 & -1\\
0 & 0 & -2/3 & -1/3\\
0 & 0 & -1/3 & -2/3\\
0 & 0 & 1/3 & -1/3
\end{matrix}\right)
\end{equation*}

\column[c]{.5\textwidth}
\begin{tikzpicture}
    \begin{scope}[every node/.style={circle,thick,draw,fill=cyan}]%text=white}]
      \node (4) at (0,2.5) {4};
      \node (2) at (2.5,2.5) {2};
      \node (3) at (1.25,.3) {3};
      \node (1) at (4.2,4.2) {1};
    \end{scope}

    \begin{scope}[>={Stealth[black]},
        every node/.style={fill=white,circle},
        every edge/.style={draw=black,very thick}]
      \path [->] (1) edge node {1} (2);
      \path [->] (2) edge node {2} (3);
      \path [->] (3) edge node {4} (4);
      \path [->] (2) edge node {3} (4);
    \end{scope}
  \end{tikzpicture}

\end{columns}

\end{frame}

\begin{frame}
  \frametitle{PTDF as sensitivity: example of 3rd column for node 3}
  \begin{columns}
    \column[c]{.5\textwidth}
    Focus on 3rd column of PTDF and look at power flow with $p_3 = +1$ and slack $p_1 = -1$. Coefficients determined by resulting flow:
\begin{equation*}
\mathbf{PTDF}_{\ell 3}=\left(\begin{matrix}
 &  & -1 &\\
 &  & -2/3 & \\
 &  & -1/3 & \\
 &  & 1/3 &
\end{matrix}\right)
\end{equation*}

\column[c]{.5\textwidth}

\begin{tikzpicture}
    \begin{scope}[every node/.style={circle,thick,draw,fill=yellow}]
      \node (4) at (0,2.5) {0};
      \node (2) at (2.5,2.5) {0};
      \node (3) at (1.25,.3) {+1};
      \node (1) at (4.2,4.2) {-1};
    \end{scope}

    \begin{scope}[>={Stealth[black]},
                every node/.style={fill=white,circle},
        every edge/.style={draw=red,very thick}]
      \path [->] (1) edge node {-1} (2);
      \path [->] (2) edge node {-2/3} (3);
      \path [->] (3) edge node {1/3} (4);
      \path [->] (2) edge node {-1/3} (4);
    \end{scope}
  \end{tikzpicture}
\end{columns}

\end{frame}

\section{Consequences of limiting power transfers}

\begin{frame}
  \frametitle{Line loading limits}

  You cannot pass infinite current through a transmission line.

  As it warms, it sags, then it will become damaged and/or hit a
  building/tree and cause a short-circuit. For this reasons there are
  always \alert{thermal limits} on current transfer. There may also be
  limits on the amount of power or current based on concerns about
  \alert{voltage stability} or \alert{general stability}.

  Typically each line has a well-defined \alert{line loading limit} on the
  amount of current or power that can flow through it:
  \begin{equation*}
    | f_{\ell } | \leq F_\ell
  \end{equation*}
  where here $F_\ell$ is the maximum power capacity of the transmission line.

  These limits prevent the transfer of renewable energy or other power sources.

\end{frame}

\begin{frame}
  \frametitle{Adjusting generator dispatch to avoid overloading}

  To avoid overloading the power lines, we must adjust our generator
  output (or the demand) so that the power imbalances do not overload
  the network.

  We will now generalise and adjust our notation.

  From lecture 3 we had for a single node:
  \begin{equation*}
    - p_t = m_t -b_t + c_t = d_t - Ww_t - Ss_t -b_t + c_t = 0
  \end{equation*}
  where $p_t$ was the nodal power balance, $m_t$ was the mismatch
  (load $d_t$ minus wind $Ww_t$ and solar $Ss_t$), $b_t$ was the
  backup power and $c_t$ was curtailment.

  We generalised this to multiple nodes labelled by $i$
  \begin{equation*}
    - p_{i,t} = m_{i,t} -b_{i,t} + c_{i,t} = d_{i,t} - W_iw_{i,t} - S_is_{i,t} -b_{i,t} + c_{i,t}
  \end{equation*}
  where now we don't enforce $p_{i,t} = 0$ but $\sum_{i} p_{i,t} = 0$ for
  all $t$.

\end{frame}

\begin{frame}
  \frametitle{Adjusting generator dispatch to avoid overloading} Now
  we write the dispatch of all generators at node $i$ (wind, solar,
  backup) labelled by technology $s$ as $g_{i,s,t}$ ($i$ labels node, $s$ technology and $t$ time) so that we have a relation between load $d_{i,t}$, generation $g_{i,s,t}$ and network flows $f_{\ell,t}$
  \begin{equation*}
    p_{i,t} = \sum_{s} g_{i,s,t} - d_{i,t} = \sum_{\ell} K_{i\ell} f_{\ell,t}
  \end{equation*}
  Where $s$ runs over the wind, solar and backup capacity generators
  (e.g. hydro or natural gas) at the node.

  A dispatchable generator's $g_{i,s,t}$ output can be controlled
  within the limits of its power capacity $G_{i,s}$
  \begin{equation*}
      0 \leq g_{i,s,t} \leq  G_{i,s}
  \end{equation*}
\end{frame}

\begin{frame}
  \frametitle{Variable generation constraints}

  For a renewable generator we have time series of availability $0\leq G_{i,s,t}\leq 1$ (the $s_t$ and $w_t$ before; $W$ and $S$ are the capacity $G_{i,s}$):
    \begin{equation*}
      0 \leq g_{i,s,t} \leq G_{i,s,t} G_{i,s} \leq  G_{i,s}
    \end{equation*}
    Curtailment corresponds to the case where $g_{i,s,t} < G_{i,s,t} G_{i,s}$:

     \centering
  \begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=10cm]{scigrid-curtailment}};
\draw (3,2.75) node{$g_{i,s,t}$};
\draw (3,3.4) node{$G_{i,s,t}G_{i,s}$};
\draw (3,4.7) node{$G_{i,s}$};
  \end{tikzpicture}

\end{frame}

\begin{frame}
  \frametitle{Germany curtailment example}

  See \url{https://pypsa.org/examples/scigrid-lopf-then-pf.html}.

\end{frame}

\begin{frame}
  \frametitle{European transmission versus backup energy}

  Consider backup energy in a simplified European grid:

  \centering
  \includegraphics[trim=0 4cm 0 4cm,width=9cm,clip=true]{europe_map}

\end{frame}

\begin{frame}
  \frametitle{DE versus EU backup energy from last time}

  Germany needed backup generation for 31\% of total load:

  \centering
  \includegraphics[width=6.3cm]{mismatch-duration-DE}

  \raggedright
  Europe needed Backup generation for only 24\% of the total load:

  \centering
  \includegraphics[width=6.3cm]{mismatch-duration-EU}

\end{frame}

\begin{frame}
  \frametitle{European transmission versus backup energy}

  \href{http://www.sciencedirect.com/science/article/pii/S0960148113005351}{Transmission needs across a fully renewable European power system} by Rodriguez, Becker, Andresen, Heide, Greiner, Renewable Energy, 2014

  \centering
  \includegraphics[width=6cm]{sarah_balancing.png}

\end{frame}

\end{document}


\chapter{Simulator description}\label{chapt:simdescr}
\section{Introduction}
This chapter discusses in detail the different components of \gls{dfts}.

\section{Intermediate feature tensor packetization} \label{sec:simdescr:pkt}
A feature tensor with height $h$, width $w$, and the number of channels $c$ will be denoted $\gls{xtensor} \in \mathbb{R}^{\gls{h} \times \gls{w} \times \gls{c}}$. An illustration of a feature tensor extracted from layer \texttt{add\_1} of \gls{resnet18} \cite{ResNet} is shown in Fig. \ref{fig:simdescr:pkt:tensorviz}.
Layer \texttt{add\_1} corresponds to the output of the shortcut connection and element-wise addition applied to the final output of the \texttt{conv2\_x} layers, as denoted in \cite[Table 1]{ResNet}. Each channel in a DFTS2 tensor is partitioned into groups of $\gls{rpp}$ rows, as indicated by the dashed green lines in Fig. \ref{fig:simdescr:pkt:tensorviz}. Zero padding is done on the last packet of a channel if the number of channel rows ($\gls{h}$) is not an integer multiple of the number of packet rows ($\gls{rpp}$). Packetized tensors can be min-max quantized \cite{choi2018deep} to a user-specified number of bits/element. While it is possible to further compress the data packets by entropy coding, this was not implemented in the current version of DFTS  because the focus is on error resilience, not on compression.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1]{Figures/tensorlostviz3icip.pdf}
	\caption[ResNet18 tensor visualization]{A tensor $\mathcal{X} \in \mathbb{R}^{h \times w \times c}$ from layer \texttt{add\_1} of ResNet-18. Several consecutive rows in each channel form a feature data packet.}
	\label{fig:simdescr:pkt:tensorviz}
\end{figure}

In \verb|yaml| files, the packetization parameter may be specified as shown below
\verbatiminput{Files/pktexample.yml}
Packetization is implemented in \verb|models.packetModel.py|. The user can create a packetization object with the following code snippet. The \verb|rowsPerPacket| is the desired number of rows per packet \gls{rpp} for each feature tensor channel. \verb|deviceOut| is a deep feature tensor \gls{xtensor}, usually output by the device sub-model.

\begin{lstlisting}[language=Python, caption=Creating a packetization object]
	from models.packetModel import PacketModel as PacketModel
	pkt_obj_model = PacketModel(rows_per_packet=rowsPerPacket,
	data_tensor=deviceOut)
\end{lstlisting}

The packetized tensor data may be accessed as

\begin{lstlisting}[language=Python, caption=Manipulating packetized tensor data]
	packetized_data = pkt_obj_model.packet_seq
	unpacketized_data = pkt_obj_model.data_tensor
\end{lstlisting}

In \verb|add_1| \gls{resnet18} tensors, there are 64 channels, each of which is $56 \times 56$. With $\gls{rpp}=8$, we have 7 packets of $8 \times 56$ elements each. So \verb|packetized_data| would be a 32-bit floating point \verb|numpy| array of dimensions $7 \times 8 \times 56 \times 64$. When dealing with tensor data for a batch of $k$ images, then \verb|packetized_data| would be $k \times 7 \times 8 \times 56 \times 64$. \verb|unpacketized_data| would be $k \times 56 \times 56 \times 64$.

\section{Channel models} \label{sec:simdescr:channel}
DFTS offers three models for the communication channel model: random loss, i.e., independent and identically distributed (iid), Gilbert-Elliott and external packet traces. Technically, the perfect channel model case, that is, no packet loss at all, is also a channel model.

In \verb|yaml| files, the communication channel parameters may be specified as shown below.
\verbatiminput{Files/channelexample.yml}
The user can switch between channel models by editing the \verb|include| flag. If no channel model is selected (all three \verb|include| flags are \verb|False|), the \gls{dfts} experiment will be run in the perfect channel model mode.

Gilbert and random loss channel models are implemented in \verb|gbChannel.py| and \verb|trivialChannel.py| in the \verb|channel| directory. An object may be initialized for these two models using the following code snippet. \verb|channel| is a dictionary created by processing the \verb|yaml| file.

\begin{lstlisting}[language=Python,caption=Creating a lossy channel model object]
	from runExpt.calloc import loadChannel
	channel = loadChannel(channel)
\end{lstlisting}
The \verb|channel| object is then passed to the function \verb|transmit| which unsurprisingly transmits the packetized bitstream over a lossy channel.

\begin{lstlisting}[language=Python,caption=Lossy channel model]
	from .simmods import *
	dO, lM, rI, lI = transmit(deviceOut, channel, rowsPerPacket)
\end{lstlisting}

\verb|deviceOut| is the device sub-model output tensor (may or may not be quantized). \verb|d0| is the packetized (according to \verb|rowsPerPacket|-\gls{rpp}) tensor transmitted over an unreliable \verb|channel| realization. \verb|lM| is the loss matrix, \verb|rI| is a list of received indices while \verb|lI| is a list of lost indices.

We will later on discuss how the user should go about loading external packet traces.

\subsection{Gilbert-Elliott channels}

\subsection{Packet traces}

\section{Packet loss concealment} \label{sec:simdescr:ec}
By recovering missing packets in deep feature tensors, we are able to provide the cloud sub-model with more ``complete" tensor data to exploit in order to complete the inference task. \gls{dfts} allows the user to switch on or off error concealment. The following packet loss concealment methods include
\begin{itemize}
	\item general tensor completion methods: \gls{silrtc} \cite{liu2012tensor} and \gls{halrtc} \cite{liu2012tensor}.\\
	These methods were developed to recover missing values in visual data. They make no assumption as to the nature of the loss (in other words, they are agnostic to the chosen packetization scheme). \gls{silrtc} and \gls{halrtc} adopt an iterative approach: they have to be run a number of times on the corrupted tensor to recover the missing packets. The number of iterations to be run is a parameter for the engineer to tune.
	\item methods which are specific to deep feature tensors in \gls{ci}: \gls{altec} \cite{Bragile2020} and \gls{caltec} \cite{CALTeC_ICIP_2021}. \\ \gls{altec} was originally developed for a single row of feature data per packet scheme. \gls{dfts} provides an \gls{altec} modified to handle a multiple rows of feature tensor data per packet scheme.
	\item image inpainting-based methods, such as Navier-Stokes \cite{navierstokes} which has been used for error concealment in \gls{ci} in \cite{Bajic2021objdet}.
\end{itemize}

\section{Simulation mode} \label{sec:simdescr:simmode}


\chapter{Introduction to Time Series Analysis}
A {\sl time series} is a collection of observations made
sequentially in time. Examples are daily mortality counts,
particulate air pollution measurements, and temperature data.
Figure 1 shows these for the city of Chicago from 1987 to 1994.
The public health question is whether daily mortality is
associated with particle levels, controlling for temperature.

\centerline{\epsfig{figure=Plots/plot-10-01.ps,angle=270,width=\textwidth}}

We represent time series measurements with $Y_1, \dots, Y_T$
where $T$ is the total number of measurements. In order to analyze
a time series, it is useful to set down a statistical model in the
form of a {\sl stochastic process}. A stochastic process can be
described as a statistical phenomenon
that evolves in time. While most statistical problems are
concerned with estimating properties of a population from a
sample, in time series analysis there is a different situation.
Although it might be possible to vary the length of the observed
sample, it is usually impossible to make multiple observations at
any single time (for example, one can't observe today's mortality
count more than once). This makes the conventional statistical
procedures, based on large sample estimates, inappropriate.
Stationarity is a convenient assumption that permits us to
describe the statistical properties of a time series.

\input{section-10-01}
\input{section-10-02}


\section{Results and Conclusion}
\label{sec:conclusion}

\subsection{Result}
\label{sec:results}

We trained and evaluated several different classifiers using 10-fold cross-validation and independent test on our data set. The classifiers were based on existing works and complemented by commonly used classification approaches. It included: \textit{Random forest decision trees}, \textit{logistic regression}, \textit{support vector machines with sequential minimal optimization}, \textit{naive bayes classifier}, and \textit{bayesian networks}. We are using WEKA library and tool for the technical platform. The classifiers were evaluated according to several established performance metric (accuracy, mean absolute error (MAE) \cite{willmott2005advantages}, and area under ROC (AUC) \cite{huang2005using}.

\renewcommand{\arraystretch}{1.5}
\setlength{\tabcolsep}{8pt}
\begin{table}[]
\centering
\caption{Summary of 10-fold cross -validation and independent test accuracies with feature normalization data set.}
\label{table:table2}
\resizebox{0.5\textwidth}{!}{%
\begin{tabular}{|l|l|l|l|l|}
\hline
\multicolumn{1}{|c|}{\textbf{Full Classes}}    & \multicolumn{2}{c|}{\textbf{10-Folds Cross Validation}} & \multicolumn{2}{c|}{\textbf{Independent Test}} \\ \hline
Original                                       & Random forest                 & 89.37\%                 & Random forest                & 88.54\%         \\ \hline
Linear Scale                                   & Random forest                 & 89.77\%                 & Logistic Regression          & 81.65\%         \\ \hline
Min-Max Normalization                          & Random forest                 & 89.53\%                 & Logistic Regression          & 81.65\%         \\ \hline
Softmax Scaling                                & Random forest                 & 80.13\%                 & Random forest                & 79.81\%         \\ \hline
Z-Score Normalization                          & Random forest                 & 89.68\%                 & Random forest                & 86.52\%         \\ \hline
\multicolumn{1}{|c|}{\textbf{Reduced Classes}} & \multicolumn{2}{c|}{\textbf{10-Folds Cross Validation}} & \multicolumn{2}{c|}{\textbf{Independent Test}} \\ \hline
Original                                       & Random forest                 & 94.52\%                 & Random forest                & 93.53\%         \\ \hline
Linear Scale                                   & Random forest                 & 94.33\%                 & Random forest                & 88.48\%         \\ \hline
Min-Max Normalization                          & Random forest                 & 94.39\%                 & Random forest                & 88.78\%         \\ \hline
Softmax Scaling                                & Random forest                 & 92.59\%                 & Random forest                & 92.34\%         \\ \hline
Z-Score Normalization                          & Random forest                 & 89.40\%                 & Random forest                & 92.10\%         \\ \hline
\end{tabular}%
}
\end{table}

Table \ref{table:table2} list the highest accuracy from 10-fold cross-validation and independent test of our works. Random forest is the most stable with good performance from our works. The accuracy result also reasonable. it means the good accuracy is not overfitting after checking the cross-validation with the independent test results.

\renewcommand{\arraystretch}{1.5}
\setlength{\tabcolsep}{8pt}
\begin{table}[]
\centering
\caption{Comparison with previous works. Value with (*) is the previous work results, and marked by (**) is our work results.}
\label{table:table3}
\resizebox{0.5\textwidth}{!}{%
\begin{tabular}{|c|c|c|c|c|c|c|}
\hline
\textbf{Classifier}             & \multicolumn{2}{c|}{\textbf{Accuracy}} & \multicolumn{2}{c|}{\textbf{Mean Absolute Error}} & \multicolumn{2}{c|}{\textbf{Wgt. Avg. AUC}} \\ \hline
\textbf{Full set of classes}    & *                  & **                & *                       & **                      & *                    & **                   \\ \hline
Random Forest                   & 76.27\%            & 89.37\%           & 0.0905                  & 0.0691                  & 0.943                & 0.984                \\ \hline
Logistic Regression             & 75.85\%            & 82.64\%           & 0.0826                  & 0.1013                  & 0.947                & 0.962                \\ \hline
SMO                             & 75.28\%            & 81.65\%           & 0.1753                  & 0.2503                  & 0.926                & 0.91                 \\ \hline
Bayesian Networks               & 72.03\%            & 70.54\%           & 0.0801                  & 0.122                   & 0.933                & 0.927                \\ \hline
Naive Bayes                     & 70.76\%            & 74.43\%           & 0.0769                  & 0.1055                  & 0.933                & 0.95                 \\ \hline
\textbf{Reduced set of classes} & *                  & **                & *                       & **                      & *                    & **                   \\ \hline
Bayesian Networks               & 96.58\%            & 92.61\%           & 0.0322                  & 0.0919                  & 0.995                & 0.973                \\ \hline
SMO                             & 96.15\%            & 88.15\%           & 0.2308                  & 0.1185                  & 0.975                & 0.875                \\ \hline
Logistic Regression             & 96.15\%            & 88.15\%           & 0.0381                  & 0.1662                  & 0.993                & 0.953                \\ \hline
Naive Bayes                     & 95.58\%            & 89.84\%           & 0.0383                  & 0.2319                  & 0.994                & 0.89                 \\ \hline
Random Forest                   & 91.17\%            & 94.52\%           & 0.1182                  & 0.0916                  & 0.985                & 0.987                \\ \hline
\end{tabular}%
}
\end{table}

Table \ref{table:table3}, the comparison result of our works with previous works. Which is our works is quite better in full set classes. Random forest still dominating for the accuracy result.

Classify the player roles is not easy. We asking \textit{Dota 2} experts to manually classify the player roles. The responses were highly divergent among them. This illustrates that the classification tasks is difficult, even for human experts. Our data set from the professional tournament (Kiev Major 2017 and \textit{The International 2016}) with 8419 data set, We separated into 70\% for training data and 30\% the rest for testing data. We also classified our data set with the same attributes but with a reduced set of classes (\textit{carry}, \textit{support}) inspired by Gao et al. \cite{gao2013classifying}, classification with our attributes achieved a higher accuracy for our data set. A direct comparison is not possible since the data sets differ. Still, the result indicate that classification with a reduced set of classes works well and could already be employed for many applications. Summarizing, we can state that the full set of classes shows promising results but also highlights that, although these classes are accepted by \textit{Dota 2} experts, some are ambiguous even to humans manually labeling the data.

\subsection{Conclusion and Future Work}
\label{sec:conclusion}

In this paper, we presented and discussed an approach to apply machine learning techniques for the classification of player roles in \textit{Dota 2}. We believe that our approach should be applicable to another MOBA games, which is share many similar key game mechanics. With 89.37\% in Random forest for 10 fold cross validation and 88.54\% for our independent test result shows that it is still promising and reasonable result. Applying Feature normalization on our classification has proved that Linear scaling with 89.77\%, but sadly, this accuracy result is not really enhanced significantly. Reducing our full classes was very successful with an accuracy of 94.52\% on Random forest, which is already suitable for many applications. Again, Random forest proved to be very stable and well suited to this domain. In the future we plan to look more closely at the mentioned problem of identifying the game phase and transitions more reliably. It might also be beneficial to detect roles not for a whole match but rather for phases or sections to account for role changes during the game.


\documentclass{memoir}
\usepackage{notestemplate}

%\logo{~/School-Work/Auxiliary-Files/resources/png/logo.png}
%\institute{Rice University}
%\faculty{Faculty of Whatever Sciences}
%\department{Department of Mathematics}
%\title{Class Notes}
%\subtitle{Based on MATH xxx}
%\author{\textit{Author}\\Gabriel \textsc{Gress}}
%\supervisor{Linus \textsc{Torvalds}}
%\context{Well, I was bored...}
%\date{\today}

\begin{document}

% \maketitle

% Notes taken on 05-10-21

\section{Techniques in Characteristic p > 0}
\label{sec:techniques_in_characteristic_p_0}

\begin{prop}
	Let \(F\) be a field of characteristic \(p>0\). Then for all \(a,b \in F\), we get that
	\begin{align*}
		(a+b)^{p} = a^{p} + b^{p}\\
		(ab)^{p} = a^{p}b^{p}
	\end{align*}
\end{prop}
This is the "Freshman's Dream".

\begin{defn}[Frobenius Endomorphism]
	For a field \(F\) of characteristic \(p>0\), the function
	\begin{align*}
		\varphi :F\to F\\
		a\mapsto a^{p}
	\end{align*}
	is the \textbf{Frobenius endomorphism} of \(F\).
\end{defn}
\begin{cor}
	The Frobenius endomorphism of \(F\) is an injective field homomorphism. When \(F\) is finite, it is also surjective.
\end{cor}

Now we will go back to some propositions about finite fields using these ideas.
\begin{prop}
	Every irreducible polynomial over a finite field \(F\) is separable. Moreover, \(f(x) \in F[x]\) is separable if and only if it is the product of distinct irreducible polynomials in \(F[x]\).
\end{prop}
This follows by contradiction. One can express the irreducible polynomial as a polynomial of the form \(g(x^{p})\), but this polynomial can be shown to be reducible, and so cannot occur.

\begin{defn}[Perfect]
	A field \(K\) of characteristic \(p>0\) is called \textbf{perfect} if every element of \(K\) is a \(p\)-th power in \(K\)-- that is, \(K = K^{p}\).
\end{defn}
By convention any field of characteristic zero is also called perfect.\\

We have just shown that every irreducible polynomial over a perfect field is separable, and hence finite extensions of perfect fields are separable.

\begin{hw}
	Prove that there exists a non-perfect infinite field \(F\), i.e. find \(f(x) \in F[x]\) so that \(f\) is irreducible and not separable.
\end{hw}

These concepts can be used to prove that the \(n\)-th cyclotomic polynomial \(\Phi_n(x) \in \Z[x]\) is irreducible.

\end{document}

Abstract Algebra - Introductory/Algebra II/Notes/source/Lecture23 - Polynomial_Fields_Over_Fp.tex


\documentclass[12pt]{cdblatex}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

This code verifies that successive $\ny{n}^{a}$ introduce a new leading power of $\eps$ while leaving the lower
order terms unchanged. That is
\begin{align}
   \ny{n+1}^a - \ny{n}^a = \BigO{\eps^{n+1}}
\end{align}

\clearpage

\begin{cadabra}
   {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w#}::Indices(position=independent).

   \nabla{#}::Derivative.

   g_{a b}::Metric.
   g^{a b}::InverseMetric.
   g_{a}^{b}::KroneckerDelta.
   g^{a}_{b}::KroneckerDelta.
   \delta^{a}_{b}::KroneckerDelta.
   \delta_{a}^{b}::KroneckerDelta.

   R_{a b c d}::RiemannTensor.
   R^{a}_{b c d}::RiemannTensor.

   R_{a b c d}::Depends(\nabla{#}).
   R^{a}_{b c d}::Depends(\nabla{#}).

   # Dx{#}::LaTeXForm{"{\Dx}"}.  # LCB: currently causes a bug, it kills ::KeepWeight for Dx

\end{cadabra}

\clearpage

\begin{cadabra}
   # note: keeping numbering as is (out of order) to ensure R appears before \nabla R etc.
   def product_sort (obj):
       substitute (obj,$ A^{a}                            -> A001^{a}               $)
       substitute (obj,$ x^{a}                            -> A002^{a}               $)
       substitute (obj,$ g^{a b}                          -> A003^{a b}             $)
       substitute (obj,$ \nabla_{e f g h}{R_{a b c d}}    -> A008_{a b c d e f g h} $)
       substitute (obj,$ \nabla_{e f g}{R_{a b c d}}      -> A007_{a b c d e f g}   $)
       substitute (obj,$ \nabla_{e f}{R_{a b c d}}        -> A006_{a b c d e f}     $)
       substitute (obj,$ \nabla_{e}{R_{a b c d}}          -> A005_{a b c d e}       $)
       substitute (obj,$ R_{a b c d}                      -> A004_{a b c d}         $)
       sort_product   (obj)
       rename_dummies (obj)
       substitute (obj,$ A001^{a}                  -> A^{a}                         $)
       substitute (obj,$ A002^{a}                  -> x^{a}                         $)
       substitute (obj,$ A003^{a b}                -> g^{a b}                       $)
       substitute (obj,$ A004_{a b c d}            -> R_{a b c d}                   $)
       substitute (obj,$ A005_{a b c d e}          -> \nabla_{e}{R_{a b c d}}       $)
       substitute (obj,$ A006_{a b c d e f}        -> \nabla_{e f}{R_{a b c d}}     $)
       substitute (obj,$ A007_{a b c d e f g}      -> \nabla_{e f g}{R_{a b c d}}   $)
       substitute (obj,$ A008_{a b c d e f g h}    -> \nabla_{e f g h}{R_{a b c d}} $)

       return obj

   # now check that y(n+1) - y(n) = Order \eps^{n+1}

   import cdblib

   y2 = cdblib.get ('y2','../geodesic-bvp.json')
   y3 = cdblib.get ('y3','../geodesic-bvp.json')
   y4 = cdblib.get ('y4','../geodesic-bvp.json')
   y5 = cdblib.get ('y5','../geodesic-bvp.json')

   diff32 := @(y3) - @(y2).
   diff43 := @(y4) - @(y3).
   diff54 := @(y5) - @(y4).

   diff32 = product_sort (diff32)
   rename_dummies        (diff32)
   canonicalise          (diff32)         # cdb (diff32.001,diff32)

   diff43 = product_sort (diff43)
   rename_dummies        (diff43)
   canonicalise          (diff43)         # cdb (diff43.001,diff43)

   diff54 = product_sort (diff54)
   rename_dummies        (diff54)
   canonicalise          (diff54)         # cdb (diff54.001,diff54)

   def truncateR (obj,n):

   # I would like to assign different weights to \nabla_{a}, \nabla_{a b}, \nabla_{a b c} etc. but no matter
   # what I do it appears that Cadabra assigns the same weight to all of these regardless of the number of subscripts.
   # It seems that the weight is assigned to the symbol \nabla alone. So I'm forced to use the following substitution trick.

       Q_{a b c d}::Weight(label=numR,value=2).
       Q_{a b c d e}::Weight(label=numR,value=3).
       Q_{a b c d e f}::Weight(label=numR,value=4).
       Q_{a b c d e f g}::Weight(label=numR,value=5).

       tmp := @(obj).

       substitute (tmp, $\nabla_{e f g}{R_{a b c d}} -> Q_{a b c d e f g}$)
       substitute (tmp, $\nabla_{e f}{R_{a b c d}} -> Q_{a b c d e f}$)
       substitute (tmp, $\nabla_{e}{R_{a b c d}} -> Q_{a b c d e}$)
       substitute (tmp, $R_{a b c d} -> Q_{a b c d}$)

       ans = Ex(0)

       for i in range (0,n+1):
          foo := @(tmp).
          bah = Ex("numR = " + str(i))
          keep_weight (foo, bah)
          ans = ans + foo

       substitute (ans, $Q_{a b c d e f g} -> \nabla_{e f g}{R_{a b c d}}$)
       substitute (ans, $Q_{a b c d e f} -> \nabla_{e f}{R_{a b c d}}$)
       substitute (ans, $Q_{a b c d e} -> \nabla_{e}{R_{a b c d}}$)
       substitute (ans, $Q_{a b c d} -> R_{a b c d}$)

       return ans

   diff32 = truncateR (diff32,2)        # cdb (diff32.002,diff32)
   diff43 = truncateR (diff43,3)        # cdb (diff43.002,diff43)
   diff54 = truncateR (diff54,4)        # cdb (diff54.002,diff54)

\end{cadabra}

\clearpage

% =================================================================================================
\section*{Verify order of $\ny{n}^{a}$}

If things have gone to plan then we should see that $\ny{n+1}^a - \ny{n}^a = \BigO{\eps^{n+1}}$. And we do. Good show.

\begin{dgroup*}
   \begin{dmath*} \ny{3}^{a} - \ny{2}^{a} = \cdb{diff32.001} \end{dmath*}
   \begin{dmath*} \ny{4}^{a} - \ny{3}^{a} = \cdb{diff43.001} \end{dmath*}
   % \begin{dmath*} \ny{5}^{a} - \ny{4}^{a} = \cdb{diff54.001} \end{dmath*}  % too long
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} \nT{2}\left(\ny{3}^{a} - \ny{2}^{a}\right) = \cdb{diff32.002} \end{dmath*}
   \begin{dmath*} \nT{3}\left(\ny{4}^{a} - \ny{3}^{a}\right) = \cdb{diff43.002} \end{dmath*}
   \begin{dmath*} \nT{4}\left(\ny{5}^{a} - \ny{4}^{a}\right) = \cdb{diff54.002} \end{dmath*}
\end{dgroup*}

\end{document}


\section{BL test case I}

In this section test case 1 is solved (1D incompressible
BL equations with gas injection along a bore hole).
The gas is injected at a velocity of $u_g \phi =1$ and
a saturation of one $S_g=1$. On the outlet boundary the
pressure level is set to zero. All boundary conditions are
applied naturally.

A converged time step of $1\times 10^{-3}$ is used for all simulations
and applications with the continuous and discontinuous between
element formulations are presented. All simulations
use the overlapping mixed finite element method with a
piecewise linear variation of the velocity within each
element and quadatric pressure. Saturation is collocated at
the pressure nodes. Although saturation is cacluated using
a control volume formulation a FEM interpolation is
used to form the high order fluxes and these are also
used in this section for most of the graphs.

\subsubsection{Continuous formulation}

{\it Upwind solution}

In figure \ref{bl-exact-meth-upwind}  the
control volume (CV) saturation of gas (with horizontal and vertical lines) and
the quadratic finite element interpolation of these are
presented.
As can be seen the solution converges with increased resolution.
Notice that the corners of the control volumes pass
very closely
to the converged solution. This is because upwinding of
the velocities is used (or equivalently the relative permeabilities)
and it is at these corners that the equations reach the
balance necessary to match closely the analytical solution.

{\it Optimal upwinding}

If one uses 80$\%$ upwinding and 20$\%$ downwinding then
one gets a closer match with the converged solution,
see figure \ref{bl-exact-meth-cv-0-8-ele50} where we see the
control volume solution and figure \ref{bl-optimal-upwind}
and where these solutions with upwinding as well as
the optimal quantity of upwinding (as described in section \ref{opt-up})
are compared.
One should note that 80$\%$ upwinding matches quite closely with the
optimal upwind parameter results and these results converge rapidly
with increase resolution. When applying optimal upwinding
the quantity of upwinding (upwind fraction) varies from
1 (full upwinding) which is typically applied near shocks
and $\frac{1}{2}$ which is central differencing. See
the upwind fractions in figure \ref{bl-upwind-frac} for the
different grid resolutions and each of the two phases.
The upwind fraction is chosen so as to increase the accuracy
of the formulation. However, even though downwinding of the
velocity may increase this accuracy we limit ourselves to
central difference. If however, one chooses to use
upwinding and downwinding then one obtains the solution
shown in figure \ref{bl-upwind-v-up-and-down}. Including
the downwind components may not be consistent with the characteristics
of the differential equation and thus may detract from
its accurate solution. The optimal solution for a number
of different resolutions ranging from 3 elements to 50 elements
is shown in figure \ref{bl-3-10-20-50}. Notice that
the solution converges rapidly. There are no oscillations in
the corresponding control volume solutions, but here we are
looking at the finite element interpolation (using a Galerkin
project) of the control volume solution which does oscillate
near the shock.

% cty:

%fig1: bl-exact-meth-upwind.xmgr

%fig2: bl-exact-meth-cv-0-8-ele50.xmgr

%fig3: bl-optimal-upwind.xmgr

%fig4: bl-upwind-frac.xmgr

%fig5: bl-upwind-v-up-and-down.xmgr

%fig6: bl-3-10-20-50.xmgr

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-exact-meth-upwind}
\end{center}
\vspace{0.cm}}
\caption{The continuous upwind method applied to the BL test
case 1 for different mesh resolutions.}
\label{bl-exact-meth-upwind}
\end{figure}

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-exact-meth-cv-0-8-ele50}
\end{center}
\vspace{0.cm}}
\caption{Comparison of the control volume solutions using 80$\%$ upwinding
and with optimal upwinding and using 50 continuous quadatric elements. }
\label{bl-exact-meth-cv-0-8-ele50}
\end{figure}

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-optimal-upwind}
\end{center}
\vspace{0.cm}}
\caption{Comparison of upwind, 80$\%$ upwind and optimal upwind solutions
at various resolutions.  }
\label{bl-optimal-upwind}
\end{figure}

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-upwind-v-up-and-down}
\end{center}
\vspace{0.cm}}
\caption{A comparison of the optimal upwind formulation when using
downwind as well as upwinding and upwinding only. The finite element interpolation
of the gas saturation is shown at different mesh resolutions.
Downwinding seems to detract from the accuracy of the solution. }
\label{bl-upwind-v-up-and-down}
\end{figure}

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-upwind-frac}
\end{center}
\vspace{0.cm}}
\caption{The fraction of upwinding used in the optimal upwinding approach at the control volume boundaries at the final time step and for three different mesh resolutions. Notice that the upwind fraction is at its largest near the shocks and is relatively small
where the solution is smooth. The magnitude of the upwind fraction also reduces
with increased mesh resolution. Also shown is the finite element interpolation
of the gas saturation at the 3 mesh resolutions.  }
\label{bl-upwind-frac}
\end{figure}

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-3-10-20-50}
\end{center}
\vspace{0.cm}}
\caption{The finite element interpolation of the gas volume fraction, for the optimal upwind scheme and for different mesh resolutions. }
\label{bl-3-10-20-50}
\end{figure}

\subsubsection{Discontinuous formulation}

Here results for the discontinuous between elements
of saturation and pressure are presented.
Having the discontinuity between elements potentially allows one
to use a course mesh to represent abruptly changing fields.
For example, using two discontinuous elements only one
obtains a result, for the BL test case, which is qualatively
similar to the converged solution, see \ref{bl-dg-2eles}.
This solution was obtained with upwinding within and
between the elements, as described in section \ref{opt-up}.
This upwind scheme issued in all results presented here
with the acception of solutions obtained using a central scheme
as shown in figure \ref{bl-dg-cent-4-10-20} for different
resolutions. The corresponding upwind solutions at the
same mesh resolutions are shown in figure \ref{bl-dg-4-10-20}.
In order to get a feel for how the discontinuous solutions
compare, in terms of accuracy, with the continuous
solutions of the previous subsection they are compared
in figure \ref{bl-dg-4-10-vers-cty}. At the courser resolution
they do seem more accurate despite the outstanding accuracy
of the continuous formulation. We also show
the accuracy of the formulation for linear fem-saturation/pressure and various mesh resolutions in figure \ref{bl-dg-p1-2-4-5-10-20-40}.
Near the shock front there is little benefit in using high order elements
so linear elements perform well here compared to quadratic elements.

% dg:

%fig7: bl-dg-2eles.xmgr

%fig8: bl-dg-cent-4-10-20.xmgr

%fig9: bl-dg-4-10-20.xmgr

%fig10: bl-dg-4-10-vers-cty.xmgr

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-dg-2eles}
\end{center}
\vspace{0.cm}}
\caption{Two element solution using the discontinuous formulation. Both the
control volume gas saturation and the finite element interpolation
of this saturation are shown.  }
\label{bl-dg-2eles}
\end{figure}

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-dg-cent-4-10-20}
\end{center}
\vspace{0.cm}}
\caption{Gas saturations from the discontinuous formulation with no upwinding and for
different mesh resolutions. Notice that there are substantial oscillations.  }
\label{bl-dg-cent-4-10-20}
\end{figure}

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-dg-4-10-20}
\end{center}
\vspace{0.cm}}
\caption{Gas saturations from the discontinuous formulation with upwinding and for
different mesh resolutions.
Notice that oscillations are suppressed compared to the central scheme.  }
\label{bl-dg-4-10-20}
\end{figure}

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-dg-4-10-vers-cty}
\end{center}
\vspace{0.cm}}
\caption{Gas saturations shown comparing the accuracy of the discontinuous between elements and continuous
formulation. The 50 element continuous solution may be viewed as a converged
result.     }
\label{bl-dg-4-10-vers-cty}
\end{figure}

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-dg-p1-2-4-5-10-20-40}
\end{center}
\vspace{0.cm}}
\caption{Gas saturations for p1(linear) pressure/(fem-interpolation of saturation)
for changing mesh resolution.   }
\label{bl-dg-p1-2-4-5-10-20-40}
\end{figure}

%\pagebreak

\subsubsection{BL test case with positive and negative gravity}

In this section we apply the the formulations to the test
case of the previous section but with gravity defined
in both the negative (figure \ref{bl-sou-neg}) and
positive (figure \ref{bl-sou-pos}) x-directions.
These solutions converge to the analytical solutions
shown in the appendix.
Notice that for the negative gravity source the less dense gas region
is compressed (compared to the case without gravity shown in
the previous section) by the weight of the more dense liquid and
the opposite happens for the positive gravity result.

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-sou-pos}
\end{center}
\vspace{0.cm}}
\caption{Gas saturations for p2(quadratic) pressure/(fem-interpolation of saturation)
for changing mesh resolution and for the test case with gravity acting in
the +ve x-direction. }
\label{bl-sou-pos}
\end{figure}

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/bl-sou-neg}
\end{center}
\vspace{0.cm}}
\caption{Gas saturations for p2(quadratic) pressure/(fem-interpolation of saturation)
for changing mesh resolution and for the test case with gravity acting in
the -ve x-direction. }
\label{bl-sou-neg}
\end{figure}

\subsubsection{Pressure/density wave propogation}

In this section we solve a single phase flow problem
with an compressible with a simple EoS given by
$\rho=1+0.01 p_{CV}$ and the fluid initially as rest.
The inlet velocity is accelerated within
one time step (the time step sizes are $\Delta t=1\times 10^{-4}$)
to a unit velocity and is sustained for 0.005 seconds.
The inlet velocity is then decreased to zero.
The rest is a density/pressure wave which
propagates across the domain with a characteristic
velocity of $c=10$. The results at $t=0.25$ seconds
into the simulation are shown. The outlet pressure is zero
and initial density unity.

An intermediate density using a quadratic DG pressure
and cubic DG velocity are shown in figure \ref{compres-imp-nonlin}.
The final solution is shown for this scheme as well
as a quadratic continuous pressure and quadratic DG velocity
scheme. In addition, we use the same two scheme's but
switching off the non-linear terms in the momentum
equations (retaining the time term only) to
produce the results shown in figure \ref{compres-imp-no-nonlin}. We have
also shown the results using the overlapping finite
element method. For the continuous scheme this results
in an exact representation of the momentum equation.
For the DG pressure formulation this is no longer
true but is close in some sense.

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/compres-imp-nonlin}
\end{center}
\vspace{0.cm}}
\caption{Density wave propogation from left to right across the domain
with DG and continuous pressure formulations. The fem-interpolation of
density is shown. }
\label{compres-imp-nonlin}
\end{figure}

\begin{figure}[H]
\vbox{
\begin{center}
\includegraphics[width=17.5cm,height=12.5cm]{./doc_figures/compres-imp-no-nonlin}
\end{center}
\vspace{0.cm}}
\caption{Density wave propogation from left to right across the domain
with DG, continuous pressure and overlapping velocity formulations.
The non-linear terms in the momentum equations are switched off
in these simulations. The fem-interpolation of
density is shown.  }
\label{compres-imp-no-nonlin}
\end{figure}


\chapter{CONCLUSIONS AND FUTURE WORK} \label{ch:conclusion}% Must have a blank line after every section label

This work analyzes a method of detecting hierarchical and communities in complex networks. This method is capable of detecting the absence of significant community structure as an early stopping criterion, and it performs competitively against other community detection methods. It has been shown to be stable to perturbations in the graph structure, unlike competing methods. Additionally, it can detect ties.

We have shown that the leximin method (and the related MCF cut algorithm) can identify ties, while eight popular methods cannot. These ties are a superposition of two dendrogram behaviors, and perturbing edge capacities gives a smooth transition away from the tie behavior.

Gridlock is a fragmentation of the network structure that occurs when the network lacks any clear mesoscopic, community structure. All edges are saturated with flow at the same time, without any bottlenecks. For a given expected density of edges, larger graphs produce gridlock more frequently. Every gridlock graph with rational capacities is related to a multigraph with unit capacities. On these multigraphs, flow is passed between all pairs along edge-disjoint paths.

The leximin method is competitive with other methods for low mixing parameter. It tends to break off singletons and small structures between or at fringes of communities. Breaking off singletons suggests worthiness for evaluations involving overlapping communities. Additionally, in graphs with real-world properties but lacking strong community structure, gridlock is extremely likely to occur.

Normalized mutual information (NMI) is deficient for evaluating community detection as a ground truth because it exaggerates accuracy of a random assignment to clusters. Argued in favor of adjusted mutual information (AMI), which addresses this deficiency. Most algorithms' scores do not change significantly under AMI; the trends are the same. It does, however, assess the leximin method in a way that agrees with intuition.

Due to the complexity of the leximin method, other methods for identifying community structure may be more desirable when the ability to recognize ties is not important or concerns about stability are not paramount. Additionally, in networks with weak community structure, other methods demonstrably outperform the AMI of the leximin method, so they should be preferred.

\section{Future Work}

Often, the disagreement between LFR-given clusters and those identified by the leximin method is \emph{not} because nodes are assigned to the \emph{wrong} communities. Instead, nodes are splintered off into \emph{their own} communities. This especially happens when equally strong connections tie the node to two different communities. This suggests an overlapping structure with some nodes serving as hubs or middlemen. Consequently, it would be beneficial to test the method on Lancichinetti and Fortunato's benchmark for overlapping communities~\cite{lancichinetti2009benchmarks}.

Additionally, the leading eigenvector method is another that displays early stopping behavior. Work to contrast their criteria for lack of mesoscopic structure in random graphs is warranted.

Further, because the leximin method operates in such a high-order polynomial time, it would be beneficial to compare the accuracy of community detection performed by approximation methods that exist~\cite{shahrokhi1989approximation}~\cite{madry2010faster}.

Finally, work to tighten the bound of the conjecture from \autoref{ch:random} would be beneficial to the traffic theory problems which surround the MCFP.