%Deep neural networks (DNNs) have been demonstrated effective for approximating complex and high dimensional functions. In the data-driven inverse modeling, we use DNNs to substitute unknown physical relations, such as constitutive relations, in a physical system described by partial differential equations (PDEs). The coupled system of DNNs and PDEs enables describing complex physical relations while satisfying the physics to the largest extent. However, training the DNNs embedded in PDEs is challenging because input-output pairs of DNNs may not be available, and the physical system may be highly nonlinear, leading to an implicit numerical scheme. We propose an approach, physics constrained learning, to train the DNNs from sparse observations data that are not necessarily input-output pairs of DNNs while enforcing the PDE constraints numerically. Particularly, we present an efficient automatic differentiation based technique that differentiates through implicit PDE solvers. We demonstrate the effectiveness of our method on various problems in solid mechanics and fluid dynamics. Our PCL method enables learning a neural-network-based physical relation from any observations that are interlinked with DNNs through PDEs.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Beamer Presentation
% LaTeX Template
% Version 1.0 (10/11/12)
%
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
%
% License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%    PACKAGES AND THEMES
%----------------------------------------------------------------------------------------

\documentclass[usenames,dvipsnames]{beamer}
\usepackage{animate}
\usepackage{float}
\usepackage{bm}
\usepackage{mathtools}
\usepackage{extarrows}

\newcommand{\ChoL}{\mathsf{L}}
\newcommand{\bx}{\mathbf{x}}
\newcommand{\ii}{\mathrm{i}}
\newcommand{\bxi}{\bm{\xi}}
\newcommand{\bmu}{\bm{\mu}}
\newcommand{\bb}{\mathbf{b}}
\newcommand{\bA}{\mathbf{A}}
\newcommand{\bJ}{\mathbf{J}}
\newcommand{\bB}{\mathbf{B}}
\newcommand{\bM}{\mathbf{M}}

\newcommand{\by}{\mathbf{y}}
\newcommand{\bw}{\mathbf{w}}

\newcommand{\bX}{\mathbf{X}}
\newcommand{\bY}{\mathbf{Y}}
\newcommand{\bs}{\mathbf{s}}
\newcommand{\sign}{\mathrm{sign}}
\newcommand{\bt}[0]{\bm{\theta}}
\newcommand{\bc}{\mathbf{c}}
\newcommand{\bzero}{\mathbf{0}}
\renewcommand{\bf}{\mathbf{f}}
\newcommand{\bu}{\mathbf{u}}
\newcommand{\bv}[0]{\mathbf{v}}

\mode<presentation> {

% The Beamer class comes with a number of default slide themes
% which change the colors and layouts of slides. Below this is a list
% of all the themes, uncomment each in turn to see what they look like.

%\usetheme{default}
%\usetheme{AnnArbor}
%\usetheme{Antibes}
%\usetheme{Bergen}
%\usetheme{Berkeley}
%\usetheme{Berlin}
%\usetheme{Boadilla}
%\usetheme{CambridgeUS}
%\usetheme{Copenhagen}
%\usetheme{Darmstadt}
%\usetheme{Dresden}
%\usetheme{Frankfurt}
%\usetheme{Goettingen}
%\usetheme{Hannover}
%\usetheme{Ilmenau}
%\usetheme{JuanLesPins}
%\usetheme{Luebeck}
\usetheme{Madrid}
%\usetheme{Malmoe}
%\usetheme{Marburg}
%\usetheme{Montpellier}
%\usetheme{PaloAlto}
%\usetheme{Pittsburgh}
%\usetheme{Rochester}
%\usetheme{Singapore}
%\usetheme{Szeged}
%\usetheme{Warsaw}

% As well as themes, the Beamer class has a number of color themes
% for any slide theme. Uncomment each of these in turn to see how it
% changes the colors of your current slide theme.

%\usecolortheme{albatross}
\usecolortheme{beaver}
%\usecolortheme{beetle}
%\usecolortheme{crane}
%\usecolortheme{dolphin}
%\usecolortheme{dove}
%\usecolortheme{fly}
%\usecolortheme{lily}
%\usecolortheme{orchid}
%\usecolortheme{rose}
%\usecolortheme{seagull}
%\usecolortheme{seahorse}
%\usecolortheme{whale}
%\usecolortheme{wolverine}

%\setbeamertemplate{footline} % To remove the footer line in all slides uncomment this line
%\setbeamertemplate{footline}[page number] % To replace the footer line in all slides with a simple slide count uncomment this line

%\setbeamertemplate{navigation symbols}{} % To remove the navigation symbols from the bottom of all slides uncomment this line
}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{soul}
\newcommand{\red}[1]{\textcolor{red}{#1}}
%
%\usepackage{graphicx} % Allows including images
%\usepackage{booktabs} % Allows the use of \toprule, \midrule and \bottomrule in tables
%
%
%\usepackage{amsthm}
%
%\usepackage{todonotes}
%\usepackage{floatrow}
%
%\usepackage{pgfplots,algorithmic,algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
%\usepackage[toc,page]{appendix}
%\usepackage{float}
%\usepackage{booktabs}
%\usepackage{bm}
%
%\theoremstyle{definition}
%
\newcommand{\RR}[0]{\mathbb{R}}
%
%\newcommand{\bx}{\mathbf{x}}
%\newcommand{\ii}{\mathrm{i}}
%\newcommand{\bxi}{\bm{\xi}}
%\newcommand{\bmu}{\bm{\mu}}
%\newcommand{\bb}{\mathbf{b}}
%\newcommand{\bA}{\mathbf{A}}
%\newcommand{\bJ}{\mathbf{J}}
%\newcommand{\bB}{\mathbf{B}}
%\newcommand{\bM}{\mathbf{M}}
%\newcommand{\bF}{\mathbf{F}}
%
%\newcommand{\by}{\mathbf{y}}
%\newcommand{\bw}{\mathbf{w}}
%\newcommand{\bn}{\mathbf{n}}
%
%\newcommand{\bX}{\mathbf{X}}
%\newcommand{\bY}{\mathbf{Y}}
%\newcommand{\bs}{\mathbf{s}}
%\newcommand{\sign}{\mathrm{sign}}
%\newcommand{\bt}[0]{\bm{\theta}}
%\newcommand{\bc}{\mathbf{c}}
%\newcommand{\bzero}{\mathbf{0}}
%\renewcommand{\bf}{\mathbf{f}}
%\newcommand{\bu}{\mathbf{u}}
%\newcommand{\bv}[0]{\mathbf{v}}

\AtBeginSection[]
{
   \begin{frame}
       \frametitle{Outline}
       \tableofcontents[currentsection]
   \end{frame}
}

%----------------------------------------------------------------------------------------
%    TITLE PAGE
%----------------------------------------------------------------------------------------
\usepackage{bm}
\newcommand*{\TakeFourierOrnament}[1]{{%
\fontencoding{U}\fontfamily{futs}\selectfont\char#1}}
\newcommand*{\danger}{\TakeFourierOrnament{66}}

\title[Physics Constrained Learning]{Subsurface Inverse Modeling with Physics Based Machine Learning} % The short title appears at the bottom of every slide, the full title is only on the title page

\author[Kailai Xu (\texttt{kailaix@stanford.edu}), et al.]{Kailai Xu and Dongzhuo Li\\ Jerry M. Harris,  Eric Darve}% Your name
%\institute[] % Your institution as it will appear on the bottom of every slide, may be shorthand to save space
%{
%%ICME, Stanford University \\ % Your institution for the title page
%%\medskip
%%\textit{kailaix@stanford.edu}\quad \textit{darve@stanford.edu} % Your email address
%}
\date{}% Date, can be changed to a custom date
% Mathematics of PDEs

\newcommand\blfootnote[1]{%
  \begingroup
  \renewcommand\thefootnote{}\footnote{#1}%
  \addtocounter{footnote}{-1}%
  \endgroup
}
\begin{document}

\usebackgroundtemplate{%
\begin{picture}(0,250)
\centering
	{{\includegraphics[width=1.0\paperwidth]{../background}}}
\end{picture}
  }
%\usebackgroundtemplate{%
%  \includegraphics[width=\paperwidth,height=\paperheight]{figures/back}}
\begin{frame}

\titlepage % Print the title page as the first slide

%dfa
\end{frame}
\usebackgroundtemplate{}

\section{Inverse Modeling}

\begin{frame}
	\frametitle{Inverse Modeling}
	\begin{itemize}
		\item \textbf{Inverse modeling} identifies a certain set of parameters or functions with which the outputs of the forward analysis matches the desired result or measurement.
		\item Many real life engineering problems can be formulated as inverse modeling problems: shape optimization for improving the performance of structures, optimal control of fluid dynamic systems, etc.t
	\end{itemize}
	\begin{figure}[hbt]
	\centering
  \includegraphics[width=0.8\textwidth]{../inverse2}
\end{figure}
\end{frame}

\begin{frame}
	\frametitle{Inverse Modeling}
	\begin{figure}
	\centering
  \includegraphics[width=1.0\textwidth]{../inverse3}
\end{figure}
\end{frame}

\begin{frame}
	\frametitle{Inverse Modeling for Subsurface Properties}

	 There are many forms of subsurface inverse modeling problems.

	 	\begin{figure}
	 	\centering
	 	\includegraphics[width=1.0\textwidth]{../inverse_types}
	 \end{figure}

\begin{center}
	\textbf{\underline{The Central Challenge}}
\end{center}
	\begin{center}
\textcolor{red}{\textbf{Can we have a general approach for solving these inverse problems?}}
\end{center}

\end{frame}

\begin{frame}
	\frametitle{Parameter Inverse Problem}
	We can formulate inverse modeling as a PDE-constrained optimization problem
	\begin{equation*}
		\min_{\theta} L_h(u_h) \quad \mathrm{s.t.}\; F_h(\theta, u_h) = 0
	\end{equation*}
	\begin{itemize}
		\item The \textcolor{red}{loss function} $L_h$ measures the discrepancy between the prediction $u_h$ and the observation $u_{\mathrm{obs}}$, e.g., $L_h(u_h) = \|u_h - u_{\mathrm{obs}}\|_2^2$.
		\item $\theta$ is the \textcolor{red}{model parameter} to be calibrated.
		\item The \textcolor{red}{physics constraints} $F_h(\theta, u_h)=0$ are described by a system of partial differential equations. Solving for $u_h$ may require solving linear systems or applying an iterative algorithm such as the Newton-Raphson method.
	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{Function Inverse Problem}

	\begin{equation*}
		\min_{\textcolor{red}{f}} L_h(u_h) \quad \mathrm{s.t.}\; F_h(\textcolor{red}{f}, u_h) = 0
	\end{equation*}

	What if the unknown is a \textcolor{red}{function} instead of a set of parameters?
\begin{itemize}
	\item Koopman operator in dynamical systems.
	\item Constitutive relations in solid mechanics.
	\item Turbulent closure relations in fluid mechanics.
	\item Neural-network-based physical properties.
	\item ...
\end{itemize}

The candidate solution space is \textcolor{red}{infinite dimensional}.

	\begin{figure}[hbt]
	\includegraphics[width=0.75\textwidth]{../nnfwi.png}
\end{figure}
\end{frame}

\begin{frame}
	\frametitle{Physics Based Machine Learning}
	$$\min_{\theta} L_h(u_h) \quad \mathrm{s.t.}\;F_h(\textcolor{red}{NN_\theta}, u_h) = 0$$
	\vspace{-0.5cm}
	\begin{itemize}
		\item Deep neural networks exhibit capability of approximating high dimensional and complicated functions.
		\item \textbf{Physics based machine learning}: \textcolor{red}{the unknown function is approximated by a deep neural network, and the physical constraints are enforced by numerical schemes}.
		\item \textcolor{red}{Satisfy the physics to the largest extent}.
	\end{itemize}
	\begin{figure}[hbt]
  \includegraphics[width=0.75\textwidth]{../physics_based_machine_learning.png}
\end{figure}
\end{frame}

\begin{frame}
	\frametitle{Gradient Based Optimization}
	\begin{equation}\label{equ:opt}
		\min_{\theta} L_h(u_h) \quad \mathrm{s.t.}\; F_h(\theta, u_h) = 0
		\end{equation}

	\begin{itemize}
		\item We can now apply a gradient-based optimization method to (\ref{equ:opt}).
		\item The key is to \textcolor{red}{calculate the gradient descent direction} $g^k$
		$$\theta^{k+1} \gets \theta^k - \alpha g^k$$
	\end{itemize}

	\begin{figure}[hbt]
	\centering
  \includegraphics[width=0.6\textwidth]{../im.pdf}
\end{figure}

\end{frame}

\section{Automatic Differentiation}

\begin{frame}
	\frametitle{Automatic Differentiation}
The fact that bridges the \textcolor{red}{technical} gap between machine learning and inverse modeling:
	\begin{itemize}
		\item Deep learning (and many other machine learning techniques) and numerical schemes share the same computational model: composition of individual operators.
	\end{itemize}

\begin{minipage}[b]{0.4\textwidth}

\begin{center}
\textcolor{red}{Mathematical Fact}

\

	Back-propagation

$||$

Reverse-mode

 Automatic Differentiation

$||$

 Discrete

 Adjoint-State Method
\end{center}
\end{minipage}~
\begin{minipage}[b]{0.6\textwidth}
\begin{figure}[hbt]
\centering
  \includegraphics[width=0.8\textwidth]{../compare-NN-PDE.png}
\end{figure}
\end{minipage}

\end{frame}

\begin{frame}
	\frametitle{Forward Mode vs. Reverse Mode}

	\begin{itemize}
		\item Reverse mode automatic differentiation evaluates gradients in the \textcolor{red}{reverse order} of forward computation.
		\item Reverse mode automatic differentiation is a more efficient way to compute gradients of a many-to-one mapping $J(\alpha_1, \alpha_2, \alpha_3,\alpha_4)$ $\Rightarrow$ suitable for minimizing a loss (misfit) function.
	\end{itemize}

	\begin{figure}[hbt]
		\includegraphics[width=0.8\textwidth]{../fdrd}
	\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Computational Graph for Numerical Schemes}

	\begin{itemize}
		\item To leverage automatic differentiation for inverse modeling, we need to express the numerical schemes in the ``AD language'': computational graph.
		\item No matter how complicated a numerical scheme is, it can be decomposed into a collection of operators that are interlinked via state variable dependencies.
	\end{itemize}

	\begin{figure}[hbt]
  \includegraphics[width=1.0\textwidth]{../cgnum}
\end{figure}

\end{frame}

%\begin{frame}
%	\frametitle{Code Example}
%	\begin{itemize}
%		\item  Find $b$ such that $u(0.5)=1.0$ and
%		$$-bu''(x)+u(x) = 8 + 4x - 4x^2, x\in[0,1], u(0)=u(1)=0$$
%	\end{itemize}
%	\begin{figure}[hbt]
%  \includegraphics[width=0.8\textwidth]{../code.png}
%\end{figure}
%\end{frame}

\section{Physics Constrained Learning}
\begin{frame}

	\frametitle{Challenges in AD}

	\begin{minipage}[t]{0.49\textwidth}
	\vspace{-3cm}
\begin{itemize}
	\item Most AD frameworks only deal with \textcolor{red}{explicit operators}, i.e., the functions with analytical derivatives that are easy to implement.
	\item Many scientific computing algorithms are \textcolor{red}{iterative} or \textcolor{red}{implicit} in nature.
\end{itemize}
\end{minipage}~
\begin{minipage}[t]{0.49\textwidth}
  \includegraphics[width=1.0\textwidth]{../sim.png}
\end{minipage}

	% Please add the following required packages to your document preamble:
% \usepackage{booktabs}
\begin{table}[]
\begin{tabular}{@{}lll@{}}
\toprule
Linear/Nonlinear & Explicit/Implicit & Expression   \\ \midrule
Linear           & Explicit          & $y=Ax$       \\
Nonlinear        & Explicit          & $y = F(x)$   \\
\textbf{Linear}           & \textbf{Implicit}          & $Ay = x$     \\
\textbf{Nonlinear}        & \textbf{Implicit}          & $F(x,y) = 0$ \\ \bottomrule
\end{tabular}
\end{table}
\end{frame}

\begin{frame}
	\frametitle{Implicit Operators in Subsurface Modeling}

		\begin{itemize}
			\item For reasons such as nonlinearity and stability, implicit operators (schemes) are almost everywhere in subsurface modeling...
				\begin{figure}
				\centering
				\includegraphics[width=0.9\textwidth]{../nonlinear.PNG}
			\end{figure}
			\item The ultimate solution: \textcolor{red}{design ``differentiable'' implicit operators}.
		\end{itemize}

\end{frame}

\begin{frame}
	\frametitle{Example}

\begin{itemize}
	\item Consider a function $f:x\rightarrow y$, which is implicitly defined by
	$$F(x,y) = x^3 - (y^3+y) = 0$$
If not using the cubic formula for finding the roots, the forward computation consists of iterative algorithms, such as the Newton's method and bisection method
\end{itemize}

\begin{minipage}[t]{0.48\textwidth}
\centering
\begin{algorithmic}
\State $y^0 \gets 0$
\State $k \gets 0$
\While {$|F(x, y^k)|>\epsilon$}
\State $\delta^k \gets F(x, y^k)/F'_y(x,y^k)$
\State $y^{k+1}\gets y^k - \delta^k$
\State $k \gets k+1$
\EndWhile
\State \textbf{Return} $y^k$
\end{algorithmic}
\end{minipage}~
\begin{minipage}[t]{0.48\textwidth}
\centering
\begin{algorithmic}
\State $l \gets -M$, $r\gets M$, $m\gets 0$
\While {$|F(x, m)|>\epsilon$}
\State $c \gets \frac{a+b}{2}$
\If{$F(x, m)>0$}
\State $a\gets m$
\Else
\State $b\gets m$
\EndIf
\EndWhile
\State \textbf{Return} $c$
\end{algorithmic}

\end{minipage}

\end{frame}

\begin{frame}
	\frametitle{Example}

	\begin{itemize}
%		\item A simple approach is to save part or all intermediate steps, and ``back-propagate''. This approach is expensive in both computation and memory\footnote{Ablin, Pierre, Gabriel Peyré, and Thomas Moreau. ``Super-efficiency of automatic differentiation for functions defined as a minimum.''}.
%		\item Nevertheless, the simple approach works in some scenarios where accuracy or cost is not an issue, e.g., automatic differetiation of soft-DTW and Sinkhorn distance.
		\item An efficient way is to apply the \textcolor{red}{implicit function theorem}. For our example, $F(x,y)=x^3-(y^3+y)=0$, treat $y$ as a function of $x$ and take the derivative on both sides
		$$3x^2 - 3y(x)^2y'(x)-1=0\Rightarrow y'(x) = \frac{3x^2-1}{3y(x)^2}$$
	The above gradient is \textcolor{red}{exact}.
	\end{itemize}
	\begin{center}
			\textbf{Can we apply the same idea to inverse modeling?}
	\end{center}

\end{frame}

\begin{frame}
	\frametitle{Physics Constrained Learning}
	$${\small    \min_{\theta}\; L_h(u_h) \quad \mathrm{s.t.}\;\; F_h(\theta, u_h) = 0}$$
	\begin{itemize}
		\item Assume that we solve for $u_h=G_h(\theta)$ with $F_h(\theta, u_h)=0$, and then
		      $${\small\tilde L_h(\theta)  = L_h(G_h(\theta))}$$
		\item Applying the \textcolor{red}{implicit function theorem}
		      {  \scriptsize
			      \begin{equation*}
				      \frac{{\partial {F_h(\theta, u_h)}}}{{\partial \theta }} + {\frac{{\partial {F_h(\theta, u_h)}}}{{\partial {u_h}}}}
				      \textcolor{red}{\frac{\partial G_h(\theta)}{\partial \theta}}
				      = 0 \Rightarrow
				      \textcolor{red}{\frac{\partial G_h(\theta)}{\partial \theta}} =  -\Big( \frac{{\partial {F_h(\theta, u_h)}}}{{\partial {u_h}}} \Big)^{ - 1} \frac{{\partial {F_h(\theta, u_h)}}}{{\partial \theta }}
			      \end{equation*}
		      }
		\item Finally we have
			      {\scriptsize
				      \begin{equation*}
					      \boxed{\frac{{\partial {{\tilde L}_h}(\theta )}}{{\partial \theta }}
					      = \frac{\partial {{ L}_h}(u_h )}{\partial u_h}\frac{\partial G_h(\theta)}{\partial \theta}=
					      - \textcolor{red}{ \frac{{\partial {L_h}({u_h})}}{{\partial {u_h}}} } \;
					      \textcolor{blue}{ \Big( {\frac{{\partial {F_h(\theta, u_h)}}}{{\partial {u_h}}}\Big|_{u_h = {G_h}(\theta )}} \Big)^{ - 1} } \;
					      \textcolor{ForestGreen}{ \frac{{\partial {F_h(\theta, u_h)}}}{{\partial \theta }}\Big|_{u_h = {G_h}(\theta )} }
					      }
				      \end{equation*}
			      }

	\end{itemize}

{\tiny Kailai Xu and Eric Darve, \textit{Physics Constrained Learning for Data-driven Inverse Modeling from Sparse Observations}}

\end{frame}

\section{Applications}

\begin{frame}
	\frametitle{Parameter Inverse Problem: Elastic Full Waveform Inversion for Subsurface Flow Problems}
	\begin{figure}[hbt]
  \includegraphics[width=0.8\textwidth]{../geo.png}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{Fully Nonlinear Implicit Schemes}
\begin{itemize}
	\item The governing equation is a nonlinear PDE
\begin{minipage}[b]{0.48\textwidth}
{\scriptsize
	\begin{align*}
	&\frac{\partial }{{\partial t}}(\phi {{S_i}}{\rho _i}) + \nabla  \cdot ({\rho _i}{\mathbf{v}_i}) = {\rho _i}{q_i},\quad
      i = 1,2	\\
     & S_{1} + S_{2} = 1\\
      &{\mathbf{v}_i} = - \frac{{\textcolor{blue}{K}{\textcolor{red}{k_{ri}}}}}{{{\tilde{\mu}_i}}}(\nabla {P_i} - g{\rho _i}\nabla Z), \quad
      i=1, 2\\
	&k_{r1}(S_1) = \frac{k_{r1}^o S_1^{L_1}}{S_1^{L_1} + E_1 S_2^{T_1}}\\
	&k_{r2}(S_1) = \frac{ S_2^{L_2}}{S_2^{L_2} + E_2 S_1^{T_2}}
	\end{align*}
	}
\end{minipage}~\vline
\begin{minipage}[b]{0.48\textwidth}
\flushleft
	{\scriptsize \begin{eqnarray*}
 && \rho \frac{\partial v_z}{\partial t} = \frac{\partial \sigma_{zz}}{\partial z} + \frac{\partial \sigma_{xz}}{\partial x} \nonumber \\
 && \rho \frac{\partial v_x}{\partial t} = \frac{\partial \sigma_{xx}}{\partial x} + \frac{\partial \sigma_{xz}}{\partial z} \nonumber \\
 && \frac{\partial \sigma_{zz}}{\partial t} = (\lambda + 2\mu)\frac{\partial v_z}{\partial z} + \lambda\frac{\partial v_x}{\partial x} \nonumber \\
 && \frac{\partial \sigma_{xx}}{\partial t} = (\lambda + 2\mu)\frac{\partial v_x}{\partial x} + \lambda\frac{\partial v_z}{\partial z} \nonumber \\
 && \frac{\partial \sigma_{xz}}{\partial t} = \mu (\frac{\partial v_z}{\partial x} + \frac{\partial v_x}{\partial z}),
\end{eqnarray*}}
\end{minipage}

	\item For stability and efficiency, implicit methods are the industrial standards.
{\scriptsize	$$\phi (S_2^{n + 1} - S_2^n) - \nabla \cdot \left( {{m_{2}}(S_2^{n + 1})K\nabla \Psi _2^n} \right) \Delta t =
\left(q_2^n + q_1^n \frac{m_2(S^{n+1}_2)}{m_1(S^{n+1}_2)}\right)
\Delta t\quad m_i(s) = \frac{k_{ri}(s)}{\tilde \mu_i}
$$}
\end{itemize}

\end{frame}

\begin{frame}
	\frametitle{Inverse Modeling Workflow}
	Traditionally, the inversion is typically solved by separately inverting the wave equation (FWI) and the flow transport equations.
	\begin{figure}
		\centering
			\includegraphics[width=0.8\textwidth]{../coupled3}
		\includegraphics[width=0.7\textwidth]{../coupled1}
	\end{figure}
\end{frame}

\begin{frame}
	\frametitle{Coupled Inversion vs. Decoupled Inversion}
	We found that \textcolor{red}{coupled inversion reduces the artifacts from FWI significantly and yields a substantially better results}.
	\begin{figure}
		\centering
		\includegraphics[width=0.6\textwidth]{../coupled2}
	\end{figure}
\end{frame}

\begin{frame}
	\frametitle{Travel Time vs. Full Waveforms}
	We also compared using only travel time (left, Eikonal equation) versus using full waveforms (right, FWI) for inversion. We found that \textcolor{red}{full waveforms do contain more information for making a better estimation of the permeability property}.
		\begin{figure}
		\centering
		\includegraphics[width=0.8\textwidth]{../coupled4}
	\end{figure}

{\small The Eikonal equation solver was also implemented with physics constrained learning!}
\end{frame}

\begin{frame}

	Check out our package FwiFlow.jl for wave and flow inversion and our recently published paper for this work.

	\begin{figure}
	\centering
	\includegraphics[width=0.45\textwidth]{../fwiflow}~
	\includegraphics[width=0.45\textwidth]{../fwiflow2}
\end{figure}

\begin{columns}
	\centering
	\begin{column}{0.33\textwidth}
		\begin{center}
			\textcolor{red}{\textbf{High Performance}}
		\end{center}
	Solves inverse modeling problems faster with our GPU-accelerated FWI module.
	\end{column}
		\begin{column}{0.33\textwidth}
		\begin{center}
			\textcolor{red}{\textbf{Designed for Subsurface Modeling}}
		\end{center}
	Provides many operators that can be reused for different subsurface modeling problems.
	\end{column}
	\begin{column}{0.33\textwidth}
			\begin{center}
		\textcolor{red}{\textbf{Easy to Extend}}
		\end{center}
		Allows users to implement and insert their own custom operators and solve new problems.
	\end{column}

\end{columns}

\end{frame}

\newcommand{\bsigma}[0]{\bm{\sigma}}
\newcommand{\bepsilon}[0]{\bm{\epsilon}}

\begin{frame}
	\frametitle{Function Inverse Problem: Modeling Viscoelasticity}
%
	\begin{itemize}
		\item Multi-physics Interaction of Coupled Geomechanics and Multi-Phase Flow Equations
{\small
\begin{align*}
\mathrm{div}\bsigma(\bu) - b \nabla p &= 0\\
    \frac{1}{M} \frac{\partial p}{\partial t} + b\frac{\partial \epsilon_v(\bu)}{\partial t} - \nabla\cdot\left(\frac{k}{B_f\mu}\nabla p\right) &= f(x,t)	\\
    	\bsigma &= \bsigma(\bepsilon, \dot\bepsilon)
\end{align*}
}
\item Approximate the constitutive relation by a neural network
{\small
$$\bsigma^{n+1} = \mathcal{NN}_{\bt} (\bsigma^n, \bepsilon^n) + H\bepsilon^{n+1}$$}
	\end{itemize}
	\begin{figure}[hbt]
	\centering
  \includegraphics[width=0.5\textwidth]{../ip}~
  \includegraphics[width=0.3\textwidth]{../cell}
\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Neural Networks: Inverse Modeling of Viscoelasticity}

	\begin{itemize}
		\item We propose the following form for modeling viscosity (assume the time step size is fixed):
		%   $$\bsigma^{n+1} = \mathcal{NN}_{\bt} (\bsigma^n, \bepsilon^n) + H\bepsilon^{n+1}$$
		$$\bsigma^{n+1} - \bsigma^{n} = \mathcal{NN}_{\bt} (\bsigma^n, \bepsilon^n) + H (\bepsilon^{n+1} - \bepsilon^n)$$
	\end{itemize}
	\begin{itemize}
		\item $H$ is a free optimizable \textcolor{red}{symmetric positive definite matrix} (SPD). Hence the numerical stiffness matrix is SPD.
		\item Implicit linear equation
		%   $$\bsigma^{n+1} = H(\bepsilon^{n+1} - \bepsilon^n) +  \left(\mathcal{NN}_{\bt} (\bsigma^n, \bepsilon^n)+H\bepsilon^n \right)$$
		$$\bsigma^{n+1} - H \bepsilon^{n+1} = - H \bepsilon^n
		+ \mathcal{NN}_{\bt} (\bsigma^n, \bepsilon^n) + \bsigma^{n}:= \mathcal{NN}_{\bt}^* (\bsigma^n, \bepsilon^n)$$
		\item Linear system to solve in each time step $\Rightarrow$ good balance between \textcolor{red}{numerical stability} and \textcolor{red}{computational cost}.
		\item Good performance in our numerical examples.
	\end{itemize}
\end{frame}

% \begin{frame}
% 	\frametitle{Neural Networks: Inverse Modeling of Viscoelasticity}
% 	\begin{figure}
% 		\centering
% 		\includegraphics[width=0.7\textwidth]{figures/strainstreess}
% 	\end{figure}
% \end{frame}

\begin{frame}
	\frametitle{Training Strategy and Numerical Stability}

	\begin{itemize}
		\item Physics constrained learning = improved numerical stability in predictive modeling.
		\item For simplicity, consider two strategies to train an NN-based constitutive relation using direct data $\{(\epsilon_o^n, \sigma_o^n)\}_n$
		$$\Delta \sigma^n = H \Delta \epsilon^n + \mathcal{NN}_{\bt} (\sigma^n, \epsilon^n),\quad H \succ 0$$
		\item Training with input-output pairs
		$$\min_{\bt} \sum_n \Big(\sigma_o^{n+1} - \big(H\epsilon_o^{n+1} +  \mathcal{NN}_{\bt}^* (\sigma_o^n, \epsilon_o^n)\big) \Big)^2$$
		\item Better stability using training on trajectory = \textcolor{red}{physics constrained learning}
		\begin{gather*}
		\min_{\bt} \ \sum_n (\sigma^n(\bt) - \sigma_o^n)^2 \\
		\text{s.t. }\text{I.C.} \ \sigma^1 = \sigma^1_o \text{ and time integrator\ }
		{\small \Delta \sigma^n = H \Delta \epsilon^n + \mathcal{NN}_{\bt} (\sigma^n, \epsilon^n)}
		\end{gather*}
	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{Experimental Data}

	\includegraphics[width=1.0\textwidth]{../experiment}

	{\scriptsize Experimental data from: Javidan, Mohammad Mahdi, and Jinkoo Kim. ``Experimental and numerical Sensitivity Assessment of Viscoelasticity for polymer composite Materials.'' Scientific Reports 10.1 (2020): 1--9.}

\end{frame}

\begin{frame}
	\frametitle{Inverse Modeling of Viscoelasticity}

	\begin{itemize}
		\item Comparison with space varying linear elasticity approximation
		\begin{equation*}
			\bsigma = H(x, y) \bepsilon
		\end{equation*}
	\end{itemize}
	\begin{figure}[hbt]
  \includegraphics[width=1.0\textwidth]{../visco1}
\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Inverse Modeling of Viscoelasticity}
	\begin{figure}[hbt]
  \includegraphics[width=0.7\textwidth]{../visco2}
\end{figure}

\end{frame}

\section{ADCME: Scientific Machine Learning for Inverse Modeling}

\begin{frame}
	\frametitle{Physical Simulation as a Computational Graph}
	\begin{figure}[hbt]
		\includegraphics[width=1.0\textwidth]{../custom.png}
	\end{figure}
\end{frame}

\begin{frame}
	\frametitle{A General Approach to Inverse Modeling}
	\begin{figure}[hbt]
  \includegraphics[width=1.0\textwidth]{../summary.png}
\end{figure}
%
\end{frame}

%}
%\usebackgroundtemplate{}
%----------------------------------------------------------------------------------------
%    PRESENTATION SLIDES
%----------------------------------------------------------------------------------------

%------------------------------------------------

\end{document}


\section{Introduction}
Any biological system is characterised by interactions between components. The study of these interactions is essential to understanding the mechanisms that regulate complex diseases and to unravel the functional aspects of genetic compounds.
In several fields of research, from social to telecommunication and biology, system interactions are increasingly represented by graphical models (\citealp{Vidal2011Complex, BAR03a, wisdomcrowds}). Generally speaking, those are defined by a set of nodes and a set of edges. Each node usually represents a specific biological component that interacts with others to perform specific functions. Edges may have several meanings, depending on the type of interactions they represent, such as similarity, causality, distance, etc.
In the field of network theory and genetics, the nodes of a graph usually represent genes and the edges represent the interactions among nodes. Consequently, a network graph of genetic interactions is a suitable way to visualise clusters, detect modules or pathways, according to the purpose of the analysis.
Network modelling has proven to be an effective approach in computational biology due to the straightforward representation of conditional dependency between variables (\citealp{netmedicine1, netmedicine2}).
It is known that genes act in clusters and their individual effects tend to be characterised by a smaller magnitude within the system as a whole (\citealp{Michalak2008243, YiST07}). Graphical models facilitate the detection of  main genetic effects. Moreover, pathways of genes become more visible to the researcher who investigates the data, giving a more complete explanation of the biological function that the pathway itself performs.
One viable way to represent the interactions of the nodes of a graph - and consequently the  topology of the resulting network - is usually represented by the adjacency matrix $\beta = \beta_{ij}$. The values of each entry $(i,j)$ in the adjacency matrix represent the magnitude of the interaction between two nodes, whereas zeros are equivalent to absence of interaction between node $i$ and node $j$.
Specifically to the field of computational biology, one possible way to learn the structure of genetic interactions is to analyse the expression profile of a number of genes. The task becomes challenging due to the presence of noise in the measurements, the high dimensionality of data and multicollinearity of variables.
Despite active research in the field of high-density oligonucleotide arrays, noise still represents a consistent source of error. Any analysis subsequent to the measurement of a subset of genes should take into consideration the artifacts that are usually introduced by noise or by the computational methods performed to mitigate it (\citealp{microarray_noise, microarray_high_noise}).
In addition to the presence of noise, high dimensionality is a very common aspect of genetic data. The number of genes $p$, usually much larger than the number of individuals $n$, makes the task of discovering interactions extremely difficult.
Without loss of generality, the problem of inferring the conditional independence between variables is equivalent to the problem of computing the sample covariance matrix of the interactions among variables. In the case of high dimensional data, as well as in a more relaxed case in which the number of individuals has a similar order of magnitude as the number of genes, the inverse of the sample covariance matrix does not exist (\citealp{Buhl93mle}). This makes the solution of the interaction problem numerically unstable and the discovered interactions unreliable.

Finally, gene expression profiles are affected by the presence of multicollinearity (\citealp{est_multicoll, ml_multicoll}), namely two or more genes or genetic compounds can be highly correlated. Highly correlated predictor variables can give rise to non-sensical results or, specifically to regression methods, can lead to parameter estimates of incorrect magnitude and sign (\emph{harmful multicollinearity}). Moreover, the greater the number of covariates, the higher the risk of such critical scenarios (\citealp{multicollinearity_kvs}).
A number of techniques to mitigate the problem of multicollinearity have been indicated in the literature. Regressing each covariate on the others and investigating the stability of regression models to predicting the response variable are two methods that have been denoted in (\citealp{multicollinearity_kvs}). The same line of conclusion is depicted in (\citealp{farrar1964multicollinearity}), which states that successful forecast with multicollinear variables requires both a stable dependency relationship between the response and the independent variables and stable interdependency relationships within the predictors. Collecting additional data as a solution of the multicollinearity problem is suggested in (\citealp{multicollinearity_kvs, farrar1964multicollinearity}).
The presence of multicollinearity can influence the performance of methods that rely on regression. The regression coefficient of a predictor variable's importance on the target variable has the tendency to lose precision with respect to the case in which the same genes were uncorrelated.
From a biological perspective, it is broadly recognised that strong genetic correlations are frequent in microarray data and that, in contrast, complete independence between any two gene expression measurements is rare (\citealp{genesets}). Therefore, it is expected that functionally related genes are correlated to each other and might be co-expressed. This biological phenomenon can be explained by assuming the presence of high correlation for a subset of genes in the dataset under study. Moreover, as the gene sets to be tested are usually chosen on the basis of functional annotation, it should be expected that many of the tested genes might be, in fact, correlated (\citealp{genesets}).
Some regression-based methods like the one described in this paper are even more sensitive to the presence of multicollinearity as they tend to select only one or few highly correlated variables.

We propose a penalised linear regression approach that can deal with the aforementioned issues affecting genetic data. We analyse the gene expression profiles of individuals with a common trait to infer the network structure of interactions among genes. The core idea consists in reducing the number of meaningful interactions with each gene, in order to build a sparse network. Penalised linear regression (Lasso) has been investigated in seminal work reported in (\citealp{Tibshirani94regressionshrinkage, Meinshausen06highdimensional, finegold, Meinshausen_stabilityselection}), in which each variable is considered response and the remaining ones are independent covariates. In the aforementioned work, bootstrapping has been extensively used to improve the stability of the predicted interactions. Unfortunately, the nature of genetic data and the presence of highly correlated variables can play a detrimental role that affects the overall reliability of discovered interactions. Specifically, Lasso-based regression procedures are known to deal poorly with highly correlated variables since only one in a group of multi correlated covariates is selected. Bootstrapping does not seem to mitigate such a troublesome condition.

In this paper, we consider the use of Lasso penalised regression as a starting point. We subsequently rely on a permutation-based approach in order to increase the significance of predicted interactions.

In Section \ref{approach}, we describe the method in detail. In Section \ref{results}, we measure the performance of our approach on simulated genetic networks of different size. Conclusion and future developments are drawn in Section \ref{conclusion}.

%However, it remains unclear how the three processes of differentiation, proliferation, and apoptosis in regulating stem cells collectively manage these challenging tasks.


% Chapter 4
%
\chapter{Implementation of SendIt} % Main chapter title
%
\label{Chapter4} % For referencing the chapter elsewhere, use \ref{Chapter1}
%
In this chapter the implementation specifics of SendIt will be discussed. It will go through how the application looks and functions, how the ACS server is implemented, and finally, how the application can be extended and improved.

\section{Application}
%
  In this section, everything regarding the implementation of the application will be discussed, except the usage modes. It will go through the implementation and usage of SendIt and clearly explain how the different concepts and technology works.
  %
  \subsection{Cryptography}
  \label{sec:crypto_imp}
    In SendIt's implementation, the Web Crypto API (SubtleCrypto module) is used for generating and handling keys \cite{ar_webcrypto}. This allows for an easy and reliable way to standardize the handling of keys, encryption and decryption, in every system. The Web Crypto API is available in most internet browsers. It has been available through the Chrome browser, since the release of version 37 \cite{url_webcr_supp}. Using a pre-developed, and tested API for SendIt's cryptographic functions allows for a more reliable system. Since the implementation of the cryptographic functions and debugging has already been done, it allows more time to be spent on developing other functionality. This is the reasoning for using the Web Crypto API.

    The system begins with creating a unique key pair, if no such key pair already exists. This check is done at the time of choosing to send or receive a file so that, if desired, the user can change to the correct identity for this connection. The key pairs used consists of two RSA-OAEP 2048 bit keys with SHA-1 hashing, but this is easy to change if the need arises. The choice of key-type was based on the recommendation of the WebCrypto API specification \cite{ar_webcrypto}.

    The key-exchange is done over the secure DataChannel created by WebRTCs PeerConnection \cite{ar_webrtc}. Once a key exchange (successful connection) has taken place, the created key pair and the other identity's public key will be written to the disk.

    SendIt imports the file into memory once a key is needed. After which it extracts the keys stored. Once the keys have been used and the communication has finished, SendIt will overwrite the existing file with the new, updated information. The file containing the keys has information exceeding just the known keys. Both the identity associated with a key, and the key itself, has to be stored.

    As discussed in \Cref{sec:crypto_des}, SendIt uses symmetric and asymmetric keys, since asymmetric keys can only encrypt small amounts of data. The maximum amount of data the asymmetric key pair mentioned previously (\emph{RSA-OAEP 2048 bit with SHA-1 hashing}) can encrypt is 214 bytes \cite{PKCSV2RSA2012}. SendIt uses an AES-GCM symmetric key, with a length of 256 bits, which allows for encryption of up to \emph{2\textsuperscript{39}-256} bytes of data \cite{ar_webcrypto}. This is well within the limits of what is necessary in SendIt.

%
  \subsection{Connection setup}
  \label{sec:conn_set_imp}
  %Connection setup - Offer&Answer generation & Processing
    This section will explain the functionality of generating the connection information and how each endpoint processes that information in order to create the connection. SendIt implements two different ways to share the connection information, in order to create a P2P connection between two endpoints. These two modes will be explained in \Cref{Chapter5}.

    %Offer / Answer generation
    The Offer and Answer generation and processing is done as described in \Cref{sec:webrtc}. The difference from the normal usage is that they can also be encrypted before being transferred, which means they have to be decrypted before being used. The original form of the data is a JavaScript object.

    To encrypt the data, it is first turned into a string, then to an ArrayBuffer, after which it is encrypted. To decrypt the data, it is converted from a string to an array of integers. It is then converted to an Uint8Array before being decrypted. The decrypted data is also an Uint8Array, which is turned into a string, and then back into a JavaScript object. All the conversions just mentioned, stem from the different data formats required by the different libraries and frameworks. For more information on how the data is exchanged, see \Cref{Chapter5}.

    %ICE and SDP? How and waht differs?
    The actual setup of the connection and the creation of the DataChannel is done according to the examples and descriptions in \Cref{sec:webrtc}. The only difference in connection setup between the two modes, is if ICE trickling is used or not.

    When the Offer and Answer is successfully exchanged, a direct connection is created. There are scenarios, when this is not case. Known causes of issues with completing the connection are:
    %
    \begin{itemize}
      \item Setup not completed within a certain time frame (see \Cref{Chapter6})
      \item One or both endpoints are behind symmetrical NAT
      \item One or both endpoints change network location (For example, connect to a different network.)
    \end{itemize}
    %

    When implementing SendIt, the choice was made to not support symmetrical NAT, as it requires a TURN server. This means the connection would no longer be a direct end-to-end connection.
    Widespread use of IPv6 would solve this issue, as it would eliminate the need for NAT traversal and TURN servers. As for changes in network conditions, there is nothing to be done on the application side, except including a signaling server. As such, the system assumes that users will stay in the same network conditions for the duration the connection is active. For the ACS mode, automatic reconnection is an option which can be added as an extra feature.

  \subsection{File transfer functionality}
    %Chunking & Size limit
    The communication will consist mostly of file data, and as such, it is interesting to know how much data can be handled. In theory, splitting files into chunks during reading, and then transferring these chunks, allows for infinitely large transfers. The default max size for Node.js is approximately 1 GB for 32-bit machines, and 2 GB for 64-bit machines. This indicates the maximum amount of data that can be kept in memory. The reason for this limit is because this is the max amount of data the V8 JavaScript engine used by Node.js can have in memory at the time \cite{url_node,url_v8}.

    The current functionality separates each file into chunks of 1200 bytes, as is the limit imposed by the Chromium implementation of WebRTC \cite{SctptransportCcCode}. The max file size is set to 160 megabytes during development, since it was the max size used by PubShare \cite{url_pubshare}. It is likely that this can be increased without any issues, since Node.js has support for keeping larger files in memory, but it would require some testing before being ready to deploy. See \Cref{fig:file_off} for an illustration of the previous explanation.

  \subsection{Programming languages, libraries and frameworks}
    SendIt is developed in JavaScript utilizing the Electron framework. These technologies were chosen because they allow for easy implementation, while supporting multiple operating systems. Easy connection setup and direct communication via P2P is also available, through pre-developed and tested frameworks. In addition, it also allows for the use of existing libraries and standardizations developed for web browsers, in desktop applications.

    To clarify, this means that the user does not need to open their web browser to utilize SendIt, but can install it and run it like they would run any desktop program with a GUI. It also allows for easy creation of an installer file, which means the end user only needs to download and run the installer, for the program to be usable.

    %
    \begin{figure}
    \centering
    \includegraphics[width=60mm]{Dev_Stack_New}
    \caption[Application stack]{The stack for the prototype application. It is built using HTML, Electron, and JavaScript.}
    \label{fig:dev_stack}
    \end{figure}

    The electron framework uses Node.js for the back end and Chromium for the front end \cite{url_electron}. In practice, this means that the system is built on: HTML, Electron and JavaScript, where Electron utilizes Chromium and Node.js. (see \Cref{fig:dev_stack}) This allows us to use modules and frameworks from any of the previously mentioned entities independently. Our implementation uses these libraries and frameworks:
    %
    \begin{itemize}
    \item Node.js API - Used for reading and writing to the disk and finding correct files and folders \cite{url_node}.
    \item Chrome Web Cryptography API - Used to handle the creation and exportation of keys, encryption, and decryption \cite{ar_webcrypto,url_webcr_supp}.
    \item Node.js Clipboardy - Utilized to automatically copy the generated Offer/Answer to the clipboard \cite{url_clipboardy}.
    \item Node.js Electron-prompt - Used to create pop-ups for requesting user input \cite{url_ele-prompt}.
    \item Chrome Native WebRTC - Used for creating and managing WebRTC connections \cite{url_webrtc_chrome}.
    \item jQuery v3.2.1 - Utilized to manage front end actions and dynamic updates \cite{url_jQuery}.
    \item Bootstrap v3.3.7 - Used to manage front end modules and dynamic updates \cite{url_bootstrap}.
    \end{itemize}
    %

  %
  \subsection{Program flow}
  \label{sec:progflow}
  %Program Flow
  The general appearance of the program will be explained in the following section. The functionality which is unique to each mode will be discussed in \Cref{Chapter5}. That means that most of the actual functionality is not explained here, but rather the installation, setup, and settings available. In addition, it will also explain the screens not related to the connection setup. These include the waiting screen, the transfer status screen, and the transfer complete screen.
  %
    \subsubsection*{Installation and launching application}
    %Installation and launch
      %Screenshots and explanation
    The application comes in the form of installer files for Linux, Mac, and Windows. These files are in the format of \emph{.deb} for Linux, \emph{.dmg} for Mac, and \emph{.exe} for Windows. The installers are very basic and require no interaction except executing them. After that is done, the image shown in \Cref{fig:inst} will appear and display a small animation. Afterwards, SendIt is installed and will be available. In most cases, the icon will be available on the desktop. Once the application is launched for the first time, a pop-up window will appear, as indicated in \Cref{fig:popup}. Afterwards, the user is taken to the Home screen. This pop-up window will only be displayed the first time the program is opened.
    \begin{figure}[H]
      \centering
      \includegraphics[width=60mm]{Figures/Base/installer}
      \decoRule
      \caption[SendIt: Install animation]{While the installation of SendIt is ongoing, this small box, with basic animations, will appear. Once it disappears, the program is installed.}
      \label{fig:inst}
    \end{figure}

    \begin{figure}[H]
      \centering
      \includegraphics[width=80mm]{Figures/Base/start_up}
      \decoRule
      \caption[SendIt: First launch pop-up]{This pop-up appears the first time the program is started. It informs the user of the default location of the upload folder. The files the user wants to send should be placed in this folder.}
      \label{fig:popup}
    \end{figure}
    %
  \subsubsection*{Home screen}
  %Home screen
    %Screenshot & explanation. Include popup!
  The Home screen is the first screen the user sees. On this screen, there is not much detail or information. The logo and acronym for SendIt is displayed, as well as the navigation bar. From here on, it is all about choosing the desired functionality or tweaking the settings to fit the users desire. The only difference between the Home screen for the two modes, is the formatting of the word 'Serverless' at the bottom of the screen, as well as the ACS mode not having a receive button on the navigation bar.
    \begin{figure}[H]
      \centering
      \includegraphics[width=\textwidth]{Figures/Base/Home_Screen}
      \decoRule
      \caption[SendIt ACS mode: Home screen]{The Home screen displayed when the application is in ACS mode. The navigation bar has no button for receiving files. The 'Serverless'-part of SendIt's acronym is also crossed out, at the bottom of the page.}
      \label{fig:hs_acs}
    \end{figure}

    \begin{figure}[H]
      \centering
      \includegraphics[width=\textwidth]{Figures/Base/Home_Screen_SL}
      \decoRule
      \caption[SendIt Serverless mode: Home screen]{The Home screen displayed when the application is in Serverless mode. The navigation bar has a button for both sending and receiving files.}
      \label{fig:hs_sl}
    \end{figure}

  \begin{figure}[H]
      \centering
      \includegraphics[width=\textwidth]{Figures/Base/navbar_sl}
      \decoRule
      \caption[SendIt: Navigation bar]{The navigation bar displayed in Serverless mode. The receive-button is not present in the ACS mode, since a separate page will be displayed if someone offers to send file(s) to you. This bar is always displayed at the top of the window.}
      \label{fig:hs_nb}
    \end{figure}
  %
  \subsubsection*{Settings}
  %Settings screen
    %Screenshot and explanation.
    The base view of the settings page is represented in \Cref{fig:sett}. At the top one can input which identity (or e-mail address if you will) to use. Further down, one can remove the configuration file, the file which stores all the data about which identity and which settings to use. Following are radio buttons, where one can choose which mode to use and if one wants to use custom locations, for example, where to store downloaded files. At the bottom, information about the current settings are displayed. Finally, there is a 'save changes' button, to store the changes made.

    In \Cref{fig:set_det}, more detailed options are displayed. These appear when clicking the non-default option of the radio buttons. If the ACS option is selected for \emph{Mode selection}, the field for indicating the address of the server is displayed, as well as a 'save' button and a 'reset' button. Afterwards, there is an option for manually selecting a file from which to load keys. One can also remove the file currently used, by pressing the 'remove ALL current keys!' button or remove individual keys by clicking on the corresponding e-mail address. Finally, one can customize the download and upload folder location.
    %Base screen
    \begin{figure}[H]
      \centering
      \includegraphics[width=\textwidth]{Figures/Base/Settings}
      \decoRule
      \caption[SendIt: Settings screen]{The screen used for indicating user preferences, upload and download locations, identity management, and mode selection.}
      \label{fig:sett}
    \end{figure}

    %Details screen
    \begin{figure}[H]
      \centering
      \includegraphics[width=70mm]{Figures/Base/settings_expanded}
      \decoRule
      \caption[SendIt: Detailed settings screen]{The expanded version of the settings screen with the selections and menus displayed.}
      \label{fig:set_det}
    \end{figure}

  \subsubsection*{After successful connection setup}
  \label{sec:file_recv}
  %
    All the following examples are taken from the Serverless mode, but they look identical in the ACS mode, with the exception of the navigation bar. These are the different screens shown once the endpoint has completed their part of the connection setup exchange.\\

    \noindent
    \underline{Waiting screen}\\
    The waiting screen is displayed while the endpoints are waiting for WebRTC to establish the P2P connection.
    \begin{figure}[H]
      \centering
      \includegraphics[width=\textwidth]{Figures/Base/waiting}
      \decoRule
      \caption[SendIt: Waiting for connection screen]{This screen is displayed while waiting for the endpoints to connect via P2P (WebRTC).}
      \label{fig:SL_wait}
    \end{figure}

    %
    \noindent
    \underline{Transfer screen}\\
      It displays details about the current file being transferred; it's name and type, as well as the total number of files to transfer. It also shows the percentage of data transferred for the current file. Finally, there is a cancel button in case one end wants to stop the transfer.
    \begin{figure}[H]
      \centering
      \includegraphics[width=\textwidth]{Figures/Base/transfer}
      \decoRule
      \caption[SendIt: Transfer screen]{This screen displays details about the status of the current transfer.}
      \label{fig:SL_trans}
    \end{figure}

    %
    \noindent
    \underline{Connection completed screen (\emph{Sender})}
    \begin{figure}[H]
      \centering
      \includegraphics[width=\textwidth]{Figures/Base/sender_complete}
      \decoRule
      \caption[SendIt: Final screen (Sender)]{This screen displays details about which files were transferred.}
      \label{fig:SL_rec1}
    \end{figure}

    %
    \noindent
    \underline{Connection completed (\emph{Receiver})}
    \begin{figure}[H]
      \centering
      \includegraphics[width=\textwidth]{Figures/Base/receiver_complete}
      \decoRule
      \caption[SendIt: Final screen (Receiver)]{This screen displays details about which files were received. It also has an 'open containing folder'-button for easy access to the received files.}
      \label{fig:SL_rec2}
    \end{figure}
%
\section{ACS server implementation}
\label{sec:acs_serv_imp}

  The ACS server is implemented in JavaScript, using the Node.js environment. It uses a library implementing the Web Crypto API for Node.js \cite{WebcryptoW3CWeb2018}, which allows for the use of the same keys, the same encryption scheme and generally the same cryptographic solutions as in the application. This makes it easy to use and means there is no need for developing support for, or using other cryptographic methods, to authenticate with the ACS server.

  \subsection{WebSockets}
  \label{sec:acsws}
  %WS
    %How is it used, secure, advantage
    %IMPLEMENTATION
    All communication between endpoints and the ACS server is done over WebSockets using HTTPS. This enables bi-directional communication at any time and creates an easy interface to use for communicating with different endpoints. The communication is exclusively done using the protocol described in the next section.

    The WebSocket interface and API also allows for easy handling and management of clients. Clients can connect and disconnect randomly without affecting the service as a whole. All clients are treated equally and it is easy to address each client individually. Because of this, it is very easy to receive information from one client and immediately forward it to the intended recipient.

  \subsection{Protocol}
  \label{sec:prot_imp}
  This section reviews the implementation of the protocol discussed in \Cref{sec:wsprot}. For an overview of the packet format, see \Cref{tab:basic}. The format indicated for each of these packets, goes in the data field of the general packet format. Following is the practical implementation of the protocol.
  %
  \subsubsection*{Lookup}
 %
  The \emph{Lookup} packet from client to server does not contain any data. The packet sent from the server to the client can contain the data indicated in \Cref{tab:lookup}. If \emph{res} is \emph{true}, the data will consist of the fields \emph{res}, \emph{wrap} and \emph{key}. The \emph{wrap} field consist of the symmetric key, encrypted with the other endpoint's public key. If \emph{false}, it will consist of \emph{res} and \emph{key} only.
%
  \begin{table}
    \caption[ACS protocol: Lookup packet]{Lookup packet}
    \label{tab:lookup}
    \centering
    \begin{tabular}{l l l l}
      \tabhead{Name} & \tabhead{Type} & \tabhead{Argument details} & \tabhead{Required} \\
      \midrule
      res & Boolean & Indicates if authentication setup is needed or not & Yes\\
      key & JWK & Server's public key in JWK format & Yes\\
      wrap & Array & Encrypted symmetric key& No\\
      \bottomrule\\
    \end{tabular}
  \end{table}
%
  \subsubsection*{Authentication setup}
  %
  The possible arguments used by the Authentication setup and Authentication Setup Reply functionality are shown in respectively \Cref{tab:auth_set} and \Cref{tab:auth_s_r}. The public key of the client is sent to the server. The server tries to set up authentication for subsequent connections. The authentication setup is considered a success if no previous data is stored for this e-mail or public key. If this is the case, the \emph{Authentication Setup Reply} packet will contain \emph{res} and \emph{wrap}, where \emph{res} is set to \emph{true}. The \emph{wrap} field consists of the symmetric key, encrypted with the other endpoint's public key. If the setup fails, the \emph{wrap} field is not included.
%
  \begin{table}
    \caption[ACS protocol: Authentication Setup packet]{Authentication Setup packet}
    \label{tab:auth_set}
    \centering
    \begin{tabular}{l l l l}
      \tabhead{Name} & \tabhead{Type} & \tabhead{Argument details} & \tabhead{Required} \\
      \midrule
      key & JWK & Client's public key in JWK format & Yes\\
      \bottomrule\\
    \end{tabular}
  \end{table}
%
  \begin{table}
    \caption[ACS protocol: Authentication Setup Reply packet]{Authentication Setup Reply packet}
    \label{tab:auth_s_r}
    \centering
    \begin{tabular}{l l l l}
      \tabhead{Name} & \tabhead{Type} & \tabhead{Argument details} & \tabhead{Required} \\
      \midrule
      res & Boolean & Authentication result & Yes\\
      wrap & Array & Encrypted symmetric key& No\\
      \bottomrule\\
    \end{tabular}
  \end{table}
%
  \subsubsection*{Authentication}
  %
  \Cref{tab:prot_auth} and \Cref{tab:prot_auth_rep} shows the possible arguments for the authentication packets. The \emph{ciph} field contains the client's email address, encrypted with the symmetric key. The \emph{Authentication Result} packet returns a boolean value directly in the data field, indicating the result of the authentication process. Successful authentication returns \emph{true}.
%
  \begin{table}
    \caption[ACS protocol: Authentication packet]{Authentication packet}
    \label{tab:prot_auth}
    \centering
    \begin{tabular}{l l l l}
      \tabhead{Name} & \tabhead{Type} & \tabhead{Argument details} & \tabhead{Required} \\
      \midrule
      ciph & Array & Client's encrypted e-mail address & Yes\\
      \bottomrule\\
    \end{tabular}
  \end{table}
%
  \begin{table}
    \caption[ACS protocol: Authentication Result packet]{Authentication Result packet}
    \label{tab:prot_auth_rep}
    \centering
    \begin{tabular}{l l l l}
      \tabhead{Name} & \tabhead{Type} & \tabhead{Argument details} & \tabhead{Required} \\
      \midrule
      - & Boolean & Authentication result & Yes\\
      \bottomrule\\
    \end{tabular}
  \end{table}
 %
  \subsubsection*{Init}
  %
  The \emph{Init} packet can consist of the data shown in \Cref{tab:init}. It contains information about the files being offered. Each object in the \emph{files} array has information about the file name, file type and the size of the file.

   \begin{table}
    \caption[ACS protocol: Initiate Connection packet]{Initiate Connection packet}
    \label{tab:init}
    \centering
    \begin{tabular}{l l l l}
      \tabhead{Name} & \tabhead{Type} & \tabhead{Argument details} & \tabhead{Required} \\
      \midrule
      files & Array & Array of objects with file data & Yes\\
      \bottomrule\\
    \end{tabular}
  \end{table}
%
  \subsubsection*{Accept}
  %
  The \emph{Accept} packet contains the WebRTC Offer generated and indicates that the endpoint wants to receive the data previously offered. The WebRTC Offer can either be encrypted or in cleartext. See \Cref{tab:acc} for more information about the data transferred. If the Offer is sent in cleartext, the data transferred is just the WebRTC Offer object. If not, the data consists of the other three fields (\emph{wrap}, \emph{iv}, and \emph{ciph}). The \emph{wrap} field consists of the symmetric key, encrypted with the other endpoint's public key. The \emph{ciph} field consists of the WebRTC Offer, encrypted with the symmetric key.

  \begin{table}
    \caption[ACS protocol: Accept packet]{Accept packet}
    \label{tab:acc}
    \centering
    \begin{tabular}{l l l l}
      \tabhead{Name} & \tabhead{Type} & \tabhead{Argument details} & \tabhead{Required} \\
      \midrule
      - & Object & The WebRTC Offer generated by the endpoint & No\\
      wrap & Array & Encrypted symmetric key & No\\
      iv & Array & Initialization vector for the symmetric key & No\\
      ciph & Array & Encrypted WebRTC Offer & No\\
      \bottomrule\\
    \end{tabular}
  \end{table}
  %
  \subsubsection*{Refuse}
  The refuse packet contains no data. If this packet is received the connection setup is stopped.
%
  \subsubsection*{Answer}
%
 The \emph{Answer} packet contains the WebRTC Answer generated by the endpoint. The WebRTC Answer can either be encrypted or in cleartext. See \Cref{tab:ans} for more information about the data transferred. If the Answer is sent in cleartext, the data transferred is just the WebRTC Answer object. If not, the data consists of the other two fields (\emph{iv} and \emph{ciph}). The \emph{iv} field consists of the initialization vector, encrypted with the other endpoint's public key. The \emph{ciph} field consists of the WebRTC Answer, encrypted with the symmetric key.
 %
\begin{table}
    \caption[ACS protocol: Answer packet]{Answer packet}
    \label{tab:ans}
    \centering
    \begin{tabular}{l l l l}
      \tabhead{Name} & \tabhead{Type} & \tabhead{Argument details} & \tabhead{Required} \\
      \midrule
      - & Object & The WebRTC Answer generated by the endpoint & No\\
      iv & Array & Encrypted initialization vector for the symmetric key & No\\
      ciph & Array & Encrypted WebRTC Answer & No\\
      \bottomrule\\
    \end{tabular}
  \end{table}
%
  \subsubsection*{ICE}
%
  This packet contains the data showed in \Cref{tab:ice}. The data can either be encrypted or sent in cleartext. If it is not encrypted, it will be sent as an ICE candidate object. If it is encrypted, it will consist of the fields \emph{ciph} and \emph{iv}. The \emph{ciph} field consists of the ICE candidate, encrypted with the symmetric key.
%
  \begin{table}
    \caption[ACS protocol: ICE packet]{ICE packet}
    \label{tab:ice}
    \centering
    \begin{tabular}{l l l l}
      \tabhead{Name} & \tabhead{Type} & \tabhead{Argument details} & \tabhead{Required} \\
      \midrule
      - & Object & The ICE candidate generated by the endpoint & No\\
      ciph & Array & Encrypted ICE candidate & No\\
      iv & Array & Initialization vector for the symmetric key & No\\
      \bottomrule\\
    \end{tabular}
  \end{table}
%
  \subsubsection*{Done}
%
  The \emph{done} packet contains no data, and indicates that the connection is terminated and that the endpoints are ready for a new connection.
%
  \subsubsection*{Error}
%
  The \emph{error} packet can contain data about which error occurred. If the server receives an \emph{error} packet with an indicated destination, it will forward it to the correct endpoint. The data will have the format indicated in \Cref{tab:err}.
%
  \begin{table}
    \caption[ACS protocol: Error packet]{Error packet}
    \label{tab:err}
    \centering
    \begin{tabular}{l l l l}
      \tabhead{Name} & \tabhead{Type} & \tabhead{Argument details} & \tabhead{Required} \\
      \midrule
      - & String & Error details & No\\
      \bottomrule\\
    \end{tabular}
  \end{table}
%
  \subsubsection*{Wait}
%
  The \emph{wait} packet does not contain data. It indicates that an endpoint is busy, and, as such, cannot partake in a connection at this time. If a destination is specified, the server forwards the packet to the correct endpoint.
%
\section{Extendability and improvements}
%
In this section the extendability of SendIt, in general, will be assessed. SendIt can be used as a platform to build extended functionality, and as such, it should be noted in what way this can be done, and how one stands to benefit from doing it. It will also discuss possibilities for improving the current implementation.

  \subsection{Key storage encryption}
  %
    The file where keys are stored should be encrypted and password protected, or otherwise access restricted. The key file should only be usable if the correct password is provided. If the wrong password is provided, the keys should not be accessible. The key file should be updated every time it is used by the system. This will allow users to change the password used to access the keys between each use and also make it easy to update information regarding each key. In addition, it gives no guarantee that the same encryption is used each time, which makes attacks over time harder to execute, since there is no reliable way to analyze changes or patterns in the way the file is stored. This functionality should be implemented and would improve the solution. It is not currently implemented in SendIt, due to time constraints.

  \subsection{E-mail verification}
  %E-mail verification
    One way to extend the current functionality is to add e-mail verification to the process of registering an identity. This would increase the trustworthiness of each identity since proving ownership of the registered e-mail address would be a necessity. It would however, also include all the issues stemming from how the e-mail system is implemented. It would also make the registration process harder and require more from the users before being able to use the system. Because of these issues, it is not included in the system by default, but can easily be added. It is left up to the end users to develop and extend the proposed system, if such functionality is desired.

  \subsection{Support for bigger files}
  \label{sec:bigfile}
  %
    Supporting bigger files can be achieved by reading in chunks of the file. Then, once a chunk is completely transferred, the next chunk is read into memory. This will allow both endpoints to handle smaller amounts of data at a time, while still having transmitted the whole file after the transfer of all chunks have been completed. It is not recommended to implement this until after the 'resume transfer' feature is implemented, as transferring large amounts of data, without any way of resuming it in case of failure, is less than optimal.

  \subsection{Resume transfer}
  \label{sec:res_trans}
    %
    \begin{table}
      \caption[Record of communication]{Fields included in the record of communication.}
      \label{tab:comm_rec}
      \centering
      \begin{tabular}{cccc}
            \textbf{Sender} & \textbf{File(s)} & \textbf{Date} & Completed \\
            \midrule
            test@email.com & picture.jpg & 2018-03-20 & 0\\
            another@email.com & document.doc & 2018-05-13 & 4\\
            \bottomrule
      \end{tabular}\\
    \end{table}
    %
    This functionality can easily be implemented based on a communication record. Since every identity will have a list of previously transferred files, it will have the file name included. If the transfer is not completed, this record can store information about which chunk of the file was the last to be received, and the endpoint can request the transfer to be continued from there.

    If the sender is not willing to resume the previous transfer, it can either start over, or the sender can offer to transfer another file. This decision is up to the sender's settings and/or preference. If the sender choses to not resume the transfer, the data previously stored on the receiver's local system should be removed, and the record updated as a failed transfer. The system should only allow for the requested file to be shared on the subsequent connection.

    To clarify: Alice tries to send Bob \emph{File A}, but the connection is broken. If Alice tries to send \emph{File A} again, it will resume from the last chunk received. If it fails again, it will also allow for the transmission of \emph{File A} to be resumed. However, if Alice contacts Bob again, but tries to send \emph{File B} this time, the previously transmitted information (\emph{File A}) stored by Bob should be removed.

    The record of communications can be implemented by creating a log that contains the fields indicated in \Cref{tab:comm_rec}. The last field can either be \emph{-1} (meaning failed), \emph{0} (meaning success) or the number of the last received chunk. This is useful for being in compliance with the GDPR, allowing the user to keep track of their interactions for reviewing their activity, and implementing a 'resume transfer' functionality, as mentioned.

  \subsection{WebRTC IDP inclusion}
  %WebRTC IDP
    WebRTC comes with a suggested standard for implementing Identity Provider services. An Identity Provider is a trusted third party that corroborates an identity. An example would be connecting ones Facebook account to an identity, as a means for other endpoints to verify the authenticity of that identity. This is possible for many different services and can be a means of increasing trust in identities. Currently, there are some arguments and disagreements on how this should be implemented in WebRTC, and as such, there are very few existing frameworks that can be used. This is expected to change, and at that point, using these services will allow for an easier way to increase trust in endpoints.

    The trust is of course reliant on the end user already trusting the service that is used as the Identity Provider, and that the identity is as expected. For example:\\
    If the Identity Provider used is a known service (such as Facebook), one can reasonably trust the data received. If it is from a service unknown to the user, then the identity provision does not increase the trust at all, since the data may be created for malicious purposes. In the same way, if the endpoint is expecting to be communicating with Alice, but Bob's identity is asserted by the provider, the end user should be sceptical.

    In summary, this functionality would allow one to link accounts from other, independent services with their WebRTC connection, in order to corroborate the endpoint's identity and increase trust.

  \subsection{SendIt as a platform}
  %
    This is an interesting idea since the proposed system allows for connection setup and identity assertion. One can use SendIt for this functionality and build any kind of additional functionality on top, if so desired. Especially combining with VOIP, which WebRTC is often used for, can be useful. It allows the developers to focus on their services and additional functionality, while allowing the easy to use and secure setup offered by SendIt to take care of identity management and authentication. The design and implementation of SendIt is modular, which means one can easily pick and choose which functionality one wants to utilize, and discard the rest. This makes it easy to take advantage of the wanted functionality, while not complicating the solution by including the functionality that is not useful to the specific scenario at hand.


\documentclass{article}
\usepackage[minionint,mathlf,textlf]{MinionPro} % To gussy up a bit
\usepackage[margin=1in]{geometry}
\usepackage{graphicx} % For .eps inclusion
%\usepackage{indentfirst} % Controls indentation
\usepackage[compact]{titlesec} % For regulating spacing before section titles
\usepackage{adjustbox} % For vertically-aligned side-by-side minipages
\usepackage{array, mathrsfs, mathrsfs, mhchem, amsmath} % For centering of tabulars with text-wrapping columns
\usepackage{hyperref, chemfig}
\usepackage{subfigure}
\usepackage[autolinebreaks,framed,numbered]{mcode}
\newcommand{\Lapl}{\mathscr{L}}

\pagenumbering{gobble}
\setlength\parindent{0 cm}
\begin{document}
\large

\section*{Recap of Turing patterns with note on stationary waves in two dimensions}

In the last question on this week's problem set, you'll be asked to determine which modes will be unstable for a given field size. (The goal of the problem is to show that more stripes will appear as an organism grows.) That question is framed so that it can be solved using the simplifying assumption that the relevant length scale $L$ is the distance from one corner of the field to the other. This works well for explaining the number of stripes in a long, thin domain, but does not explain the transition from a striped pattern to a spotted one as the organism grows fatter.\\

To explain the spots, we should consider perturbations of the form:
\[ \begin{pmatrix} \alpha\\ \beta \end{pmatrix} = \begin{pmatrix} \alpha_0\\ \beta_0 \end{pmatrix} e^{\lambda t} e^{iqx} e^{iry} \]

We will plug this solution into our reaction-diffusion equation on the plane:
\[ \frac{\partial}{\partial t} \begin{pmatrix} \alpha\\ \beta \end{pmatrix} = \mathbf{C} \begin{pmatrix} \alpha\\ \beta \end{pmatrix} + \mathbf{D} \nabla^2 \begin{pmatrix} \alpha\\ \beta \end{pmatrix} \]
$\nabla^2$ is the Laplace operator. In one dimension, it is simply the second spatial derivative. On the Cartestian plane, it is:
\[ \nabla^2 = \frac{\partial^2}{\partial x^2} + \frac{\partial^2}{\partial y^2} \]
Plugging in, calculating the derivatives, and combining terms, we have:
\begin{eqnarray}
\left[ \mathbf{C} - \left( q^2 + r^2 \right) \mathbf{D} - \lambda \mathbf{I} \right] \begin{pmatrix} \alpha_0\\ \beta_0 \end{pmatrix} = 0 \implies \left|\mathbf{C} - \left( q^2 + r^2 \right) \mathbf{D}  - \lambda \mathbf{I} \right| = 0 \label{eqn:twodeigenvalue}
\end{eqnarray}
Expanding out the determinant gives a quadratic expression for $\lambda$. We need at least one of the $\lambda_i$ to be positive for the system to be unstable, however, the $\lambda_i$ are centered around a negative number. Therefore we need a large discriminant term to push the large solution to a positive value. Specifically, we need\footnote{See Lecture 29 notes or the spatial modeling chapter by Iglesias for the full version of an analogous derivation.}:
\begin{eqnarray}
 \textrm{det} \left( \mathbf{C} \right) - \left(q^2 + r^2 \right) \left(c_{11}D_B + c_{22} D_A \right) + \left(q^2 + r^2 \right)^2 D_A D_B < 0 \label{eqn:twod}
 \end{eqnarray}
As in the one-dimensional case, the wavenumbers $q$ and $r$ cannot assume just any value. Consider a patterning field shaped like the round surface of a cylinder.

\begin{center}
\includegraphics[width=0.5\textwidth]{geometry.pdf}
\end{center}

We could cut this surface and lay it flat: it would have a length $\ell_x$ equal to the length of the cylinder and a height $\ell_y$ equal to the circumference of the cylinder. The ``boundaries" at $x=0$ and $x=\ell_x$ are reflective: we therefore require that $\partial_{xx}$ be zero there. The allowable modes in this dimension therefore include half-waves. However, the ``boundaries" at $y=0$ and $y=\ell_y$ must have equal values, so only full waves are allowed in the $y$ direction. Thus:
\[ q_n = \frac{\pi \, n}{\ell_x} \hspace{1 cm} \textrm{and} \hspace{1 cm} r_m = \frac{2 \, \pi \, m}{\ell_y} , \hspace{1 cm} n, m \in \mathbb{N^0} \]

\begin{center}
\includegraphics[width=0.5\textwidth]{first_modes_excited.pdf}
\end{center}

To find which modes can be excited for given $\ell_x$ and $\ell_y$, we can plug trial combinations of $q_n$ and $r_m$ into inequality \ref{eqn:twod} and check whether it is satisfied. Multiple combinations may satisfy the expression: in this case, the one which will ``win" at long times is the one with the largest eigenvalue, which we could determine using equation \ref{eqn:twodeigenvalue}.

\subsection*{Aside: non-stationary spatial waves}

One of the two lectures cut from this year's course was on symmetry and symmetry breaking. We would have covered an example where spatial oscillations are used to identify the center of the \textit{E. coli} cell, where the septum will form. The basic principle is that oscillations in MinD from one pole to the opposite leave a region in the middle where the average concentration of MinD is relatively low: it is in this region that the contractile FtsZ ring will form at cell division.\\

The Min system is one of the rare few that can be reconstituted \textit{in vitro} (Loose et al., 2008 and Zieske and Schwille, 2014). Zieske and Schwille induce these oscillations in compartments of varying size, demonstrating that if the dimensions are changed the number of regions where average MinD concentration is low will change. This strikingly demonstrates the dependency of the system on the dimensions of the cell. [Videos from Zieske and Schwille, 2014.]

\section*{Introduction to modularity and evolvability}

Critical genes are not at liberty to explore sequence space freely: no mutation in them will persist if it impairs their function. This means that these genes can become trapped in ``local maxima" of performance or fail to gain new functions that interfere with their original one. This limitation can be bypassed, however, via random gene duplication.\\

After gene duplication, as long as one copy of the gene continues to perform its essential function, the other paralog is freed of selective pressure. A typical outcome is that the unnecessary paralog becomes a non-functional pseudogene and over time is lost entirely. However, with luck, this paralog might instead acquire a new and useful function through mutation. This strategy for acquiring new functionality through gene duplication and subsequent divergence was first proposed by Susumu Ohno (1970) and nowadays has strong support from sequence analysis.\\

The notion that strong selection to maintain a current function is likely to interfere with evolution of a new function is not simply a narrative: in several specific systems it has been demonstrated directly. Consider for example TEM-1 $\beta$-lactamase, the ampicillin resistance-conferring \textit{bla} gene found on virtually every bacterial plasmid map. Stiffler et al. (2015) recently explored all amino acid substitution in TEM-1 \textit{exhaustively} (19 alternatives at each of nearly 300 residues) and found that 106 of them allow TEM-1 to better neutralize cefotaxime, another antibiotic. Unfortunately, most of these mutations also interfere with the enzyme's ability to neutralize ampicillin as evidenced by a fitness defect when grown on ampicillin. The implication is that it would be relatively difficult for the gene to evolve cefotaxime neutralization while undergoing selection for ampicillin resistance. This is a case where gene duplication and subsequent relief of selection on one paralog would facilitate acquisition of a new function.

\begin{center}
\includegraphics[width=0.5\textwidth]{stiffler_graphic.pdf}
\end{center}

\section*{Two-component systems}

One gene family where this mode of evolution is especially apparent are the two-component signaling systems. (The signaling lecture was unfortunately canceled this year due to weather, so we will instead discuss that system here.) Unlike the MAP kinase systems which have appeared often in the course, two-component systems consist of only a receptor (called a histidine kinase) and the response regulator, a signaling effector. The receptor gets this name because it controls the phosphorylation state of its response regulator by transferring a phosphate from a histidine residue to the RR in a signaling-dependent manner. When no signaling is occurring, it may also act as a phosphatase.

\begin{center}
\includegraphics[width=0.5\textwidth]{two_component_diagram.pdf}
\end{center}

Whereas the serine/threonine and tyrosine receptor families have expanded substantially in eukaryotes, histidine kinases are the major expanded family in bacteria: some species have as many as 200 two-component pathways, though most have only around 30. In many cases both a histidine kinase and its corresponding response regulator are found in a single operon, facilitating duplication of the pair. Despite their common origins, these two-component pathways are very effective at avoiding crosstalk.  To show this, Skerker et al. (2005) added $^{32}$P-labeled ATP to purified histidine kinases, which transferred the labeled phosphate to their histidines. They then added the same response regulator to each reaction and checked for transfer from the HK to the response regulator after 10 seconds or one hour. Only the correct pair showed transfer on the fast timescale. (Slower reactions were apparent for a pair isolated in vitro, but would likely not be relevant physiologically due to competition between response regulators within the cell.)

\begin{center}
\includegraphics[width=0.8\textwidth]{skerker2005.pdf}
\end{center}

Attaining specificity of interactions following duplication and divergence is a major concern for the neofunctionalization by duplication and divergence. Even before the first HK-RR crystal structure was solved, the Laub lab was searching for the interactions between histidine kinases and their response regulators that conferred specificity. These proteins had accumulated many sequence differences over a very long period of divergence, so that the relevant changes could not be identified simply by alignment and inspection. Skerker et al. (2008) approached this problem computationally, taking advantage of the fact that mutations at the interaction interface on one partner might be compensated by mutations on the other. Thirteen hundred HK-RR sequence pairs were known to science at that time: within this dataset, perhaps the same compensating mutations would have arisen independently multiple times.\\

The authors used a metric called mutual information to search for cases where knowing the amino acid at one residue on one partner gave a better-than-chance prediction of the amino acid at a residue on the other partner. If $p_i$ is the probability of finding amino acid $i$ at residue $x$ on the HK, and $p_j$ is the proboably of finding amino acid $j$ at residue $y$ on the RR, and $p_{ij}$ is the probability of both occurring, then
\[ \textrm{MI } = \sum_{i=1}^{20} \sum_{j=1}^{20} p_{ij} \ln \left( \frac{p_{ij}}{p_i p_j} \right) \]
If the two events are independent, then $p_{ij} \approx p_i p_j$ and the ratio within the logarithm is 1, so the mutual information is zero. Additional co-occurrence would elevate the score.

\begin{center}
\includegraphics[width=0.6\textwidth]{mi.pdf}
\end{center}

This purely mathematical approach identified several pairs of sites that spanned these two proteins which appeared to be ``evolving together," so that certain sequence combinations were much more common than others. Mapping these residues to the closest cognate structure available at that time, the authors found that they seemed to fall at the interface between the two partners just as one would predict.

\begin{center}
\includegraphics[width=0.4\textwidth]{spo0f.pdf}
\end{center}

Following duplication, specificity of the new pair for the other could evolve in one of two broad ways. One protein could acquire a mutation by which it loses the ability to interact with its partner (creating a non-functional interaction), and then the partner could happen to evolve a compensatory mutation before either becomes a pseudogene. Alternatively, it might be possible for the specificity residues to mutate in some order by which the two partners never lose the ability to interact with one another. Determining which of these is more likely requires understanding the range of sequence combinations that confer specificity.\\

A related question is how well two-component systems have sampled the space of sequences conferring specificity. Do specificity sequences become trapped in local minima, or are extant two-component systems representative of the full range of possible sequence combinations conferring specificity?\\

In a recent attempt to address these question, Pedgornaia and Laub (2015) generate variants of the histidine kinase PhoQ that differ from the wildtype sequence at four residues in the interaction interface. They estimate that all 160,000 possible amino acid sequence variants are covered by their random library. To assess whether these variants are functional, they select cells which express a fluorescent reporter activated by PhoQ's response regulator, PhoP. (Under these conditions, if the PhoQ is not specific, it may transfer its phosphate to other response regulators in the cell which will decrease reporter expression.) The authors find a vast network of sequences which differ from each other by only one amino acid. However, all known PhoQ sequences fall within one region of this network. This may reflect constraints in the ability to mutate from some amino acids to others (some changes require only one base pair mutation; others, more).

\begin{center}
\includegraphics[width=0.8\textwidth]{podgornaia.pdf}
\end{center}

\end{document}

lectures/Lecture 30 - Modularity and Evolvability/lecture notes/lecture 30 notes.tex


\chapter{Basic Types} \label{chap:basics}

Since C++ is a statically typed language \cite{Stroustrup:cpp}, the basic mathematical building blocks such as constants or variables are represented as types.
The possiblity of manipulations at compiletime or runtime is accomplished by essentially
two different implementations of these primitives.
Basic types for runtime evaluations are discussed first, since their interface and handling is potentially more familiar to average C++ programmers.
Sec.~\ref{sec:compiletime-types} then provides an overview of the basic types used for compiletime manipulations.

The main include file for {\ViennaMath} is \lstinline|viennamath/expression.hpp| and includes all the types discussed in the remainder of this chapter.

\TIP{Include \lstinline|viennamath/expression.hpp| to make all {\ViennaMath} types available.}

\TIP{Note that all types reside in namespace \lstinline|viennamath|. The namespace is not written explicitly in the following, thus either \lstinline|viennamath::| prefixes or certain \lstinline|using| declarations need to be added by the user in order to make the code valid.}

\section{Types Evaluated at Runtime} \label{sec:runtime-types}
Common to all types represented at runtime is that they inherit from the same abstract base class
and can thus be accessed and manipulated using a pointer to that interface.
The interface is not fixed a-priori and can be adjusted via a template parameter, which is in the following called \lstinline|InterfaceType|.
Library users should use the expression wrapper objects discussed next, because it provides an automatic memory management and does not involve complicated pointer manipulation.

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

 \subsection{Expression Wrapper \lstinline|expr|}
The main expression wrapper type in {\ViennaMath} is \lstinline|rt_expr<InterfaceType>|.
The prefix \lstinline|rt| refers to \emph{runtime} and aids in distinguishing between types processed at runtime, and types processed at compiletime.
In most cases, the default parameter for the runtime interface \lstinline|InterfaceType| is used, in which case users would have to write
\begin{lstlisting}
 rt_expr<> my_expression = /* any expression here */;
\end{lstlisting}
for instantiating an expression wrapper object \lstinline|my_expression|.
In order to avoid users from having to write the \lstinline|rt_| and the lower-than and greater-than signs,
there is a convenience shortcut \lstinline|expr| provided. The previous code line thus becomes
\begin{lstlisting}
 expr my_expression = /* any expression here */;
\end{lstlisting}
The \lstinline|expr|-type can be evaluated and manipulated using operator overloads.
For example, the addition of two expressions is accomplished by
\begin{lstlisting}
 expr ex1 = /* any expression here */;
 expr ex2 = /* any expression here */;
 expr result = ex1 + ex2;
\end{lstlisting}
The initalization of expression objects is accomplished by any of the fundamental types discussed in the next subsections.
Note that objects of type \lstinline|expr| are default-constructible, yet they can only be used after an expression has been assigned to them.

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

 \subsection{Constant}
Constants in C++ have their own types \lstinline|double|, \lstinline|long|, etc.
These types can be used with {\ViennaMath} directly.
In order to also represent constants using a pointer to the runtime interface, a separate class \lstinline|rt_constant<NumericT, InterfaceType>| is provided.
The template parameter \lstinline|NumericT| denotes the underlying numerical type such as \lstinline|double|, \lstinline|long|, or high precision types.
There is again a convenience shortcut \lstinline|constant| provided for the case of the commonly used \lstinline|rt_constant<double>|, hence a user can write code such as
\begin{lstlisting}
 constant pi = 3.1415;
 constant pi_squared = pi * pi;
\end{lstlisting}
An exemplary use with the expression wrapper \lstinline|expr| is
\begin{lstlisting}
 constant pi = 3.1415;
 expr pi_squared = pi * pi;
 expr result = pi + pi_squared;
\end{lstlisting}

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

 \subsection{Variable}
A mathematical variable in {\ViennaMath} is modeled by \lstinline|rt_variable<InterfaceType>|.
and refers to the mapping
\begin{align*}
 \left( x_0, x_1, \ldots, x_{N-1}  \right) \mapsto x_j \ ,
\end{align*}
where the value of $j$ is provided to the constructor of the variable.
By default, the index $j=0$ is used. Any vector type offering access to its values using \lstinline|operator[]| such as \lstinline|std::vector<T>| can be used for an evaluation of the variable or a compounded expression.

A simple example leading to the mapping $(x,y) \mapsto x(y+\pi)$ using the types introduced so far is as follows:
\begin{lstlisting}
 constant pi = 3.1415;
 variable x(0);
 variable y(1);
 expr f = x * (y + pi);
\end{lstlisting}
An evaluation of \lstinline|f| at $(1,2)$ can be accomplished by using the functor interface provided by an overload of the parenthesis operator
and the {\ViennaMath} helper function \lstinline|make_vector()|, which conveniently creates a suitable vector for evaluation.
\begin{lstlisting}
 std::cout << f( make_vector(1,2) ) << std::endl;  //prints 5.1415
\end{lstlisting}

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

 \subsection{Unary Expression}
Mappings of the form $x \mapsto \sin(x)$ are modeled by the \lstinline|rt_unary_expr<InterfaceType>| class.
Thus, they represent a unary function acting on a constant, a variable or an expression. An overview of the unary functions provided with {\ViennaMath} is given in Tab.~\ref{tab:unary-functions}.

\begin{table}
\centering
\begin{tabular}{|l|l||l|l|}
\hline
Name & {\ViennaMath} Function   & Name & {\ViennaMath} Function \\
\hline
Exponential   & \lstinline|exp()| & Modulus & \lstinline|fabs()| \\
Sine          & \lstinline|sin()| & Square Root & \lstinline|sqrt()| \\
Cosine        & \lstinline|cos()| & Natural Logarithm & \lstinline|log()| \\
Tangent       & \lstinline|tan()| & Logarithm, Base 10 & \lstinline|log10()| \\
\hline
\end{tabular}
\caption{Overview of unary functions defined in {\ViennaMath}.\label{tab:unary-functions}}
\end{table}

\TIP{Function names in Tab.~\ref{tab:unary-functions} are intentionally chosen such that they coincide with the standard functions for floating point types.
When calling these functions with floating point types, compilation might fail due to ambiguity. In such case the namespace should be specified explicitly.}

Typically, unary expressions are not instantiated explicitly by the library user.
Instead, they are generated implicitly by one of the unary functions and then assigned to an object of type \lstinline|expr| as in the following example:
\begin{lstlisting}
 variable x;
 expr g = sin(2.0 * x);  // wraps a unary expression into 'g'
\end{lstlisting}

 \subsection{Binary Expression}
Similar to unary expressions, binary expressions at runtime are mostly handled in the background only.
They are created whenever one of the operator overloads for addition, subtraction, multiplication, or division is triggered.
In particular, the argument \lstinline|2.0 * x| to \lstinline|sin()| in
\begin{lstlisting}
 expr g = sin(2.0 * x);
\end{lstlisting}
is a binary expression. Binary expressions are central for compile time evaluations in Sec.~\ref{sec:compiletime-types}.

 \subsection{Expression Vector}
For the cases where a vector-valued expression is required, a user can either instantiate a vector of \lstinline|expr|, which allows for storing multiple scalar-valued function only,
or use the \lstinline|rt_vector_expr<InterfaceType>| class provided by {\ViennaMath}. A convenience shortcut \lstinline|vector_expr| is provided.
The benefit of using the \lstinline|vector_expr| class is that it provides the usual operator overloads directly:
\begin{lstlisting}
 variable x(0), y(1);
 vector_expr vec(3); vec[0] = x; vec[1] = y; vec[2] = x + y;
 vector_expr vec2 = x * vec + y * vec;
\end{lstlisting}
The dot-product of two vector-valued expressions is provided as well:
\begin{lstlisting}
 expr h = vec * vec2;
\end{lstlisting}

\section{Types Evaluated at Compiletime} \label{sec:compiletime-types}
The runtime types discussed in the previous section enable a convenient handling of expressions.
However, there are numerous runtime dispatches required when evaluating such runtime expressions, which are too costly in a high performance setting.
The compiletime types discussed in this section avoid any additional runtime dispatches and their use thus result in faster code in general.
This gain in performance comes at the price of a few additional restrictions:
Since the expression is entirely encoded in the type, there is no equivalent to \lstinline|expr| in order to assign an expression to a another object\footnote{The new C++11 standard addresses this issue and provides the \lstinline|auto| keyword for automatic type deduction. However, {\ViennaMath} intentionally does not use any C++11 features yet.}.
Furthermore, compilation times increase due to the additional work to be done for the compiler.
Excessive use of compiletime evaluations and manipulations can even result in minutes to hours of compilation time, even though this is rarely encountered in practice.
Another complication stems from the fact that no floating point template arguments are allowed, thus reducing any compiletime calculations to integer calculations.
Fractional numbers can be emulated this way, but they cannot resolve all problems.

 \subsection{Constant}
Since no floating point type is allowed as template argument, only integer values \lstinline|val| are represented by the class \lstinline|ct_constant<val>|.
Operators are overloaded in the same way as for the runtime evaluation types in Sec.~\ref{sec:runtime-types}. One example of a compiletime calculation is given as follows:
\begin{lstlisting}
 ct_constant<2> c2;   //the constant '2'
 ct_constant<5> c5;   //the constant '5'
 std::cout << c2 + c5 << std::endl;  //prints '7' (computed at compiletime)
\end{lstlisting}
Note that \lstinline|ct_constant<>| can in principle also be mixed with ordinary constants such as
\begin{lstlisting}
 std::cout << 2 + c5 << std::endl;  //prints '7'
\end{lstlisting}
However, depending on the optimization capabilities of the C++ compiler used, ordinary constants may or may not be used for compiletime computations, while the compiler is forced to do it in the introductory snippet.

\TIP{A general guideline is to use \lstinline|ct_constant<val>| for encoding an integer \lstinline|val| already known at compile time rather than writing the value explicitly in code. }

 \subsection{Variable}
A mathematical variable for compiletime manipulations is represented by \lstinline|ct_variable<id>|, where \lstinline|id| refers to the coordinate entry in the evaluation vector.
The meaning of \lstinline|id| is identical to the constructor argument of a \lstinline|variable| in the runtime case.

Operators are again overloaded as usual. For example, consider
\begin{lstlisting}
 ct_variable<0> x;
 ct_variable<1> y;
 std::cout << x * y << std::endl;
\end{lstlisting}

 \subsection{Unary Expression}
The unary functions in Tab.~\ref{tab:unary-functions} can also be called with compiletime types.
The corresponding type for the compiletime representation is provided by \lstinline|ct_unary_expr<E, OP>|, where \lstinline|E| refers to the expression on which the unary function encoded by the tag \lstinline|OP| acts.
Unary operation tags start with \lstinline|op_| and are defined in \lstinline|viennamath/compiletime/unary_op_tags.hpp|. Their type name can be deduced from the function names in Tab.~\ref{tab:unary-functions} by adding the prefix.
Note that all unary functions are evaluated at runtime, because the underlying C-functions are called for evaluation.
For example, the type \lstinline|T| of the compiletime unary expression
\begin{lstlisting}
 ct_variable<0> x;
 T t = sin(x);
\end{lstlisting}
is \lstinline|ct_unary_expr< ct_variable<0>, op_sin<NumericT> >|, where \lstinline|NumericT| is the floating point type used for the evaluation at runtime (typically \lstinline|double|).

 \subsection{Binary Expression}
The binary expression \lstinline|ct_binary_expr<L, OP, R>| with left hand side expression \lstinline|L|, operation tag \lstinline|OP| and right hand side expression \lstinline|R| are the main types for building more complex expressions.
Currently, four binary operations are supported: addition (with tag \lstinline|op_plus<NumericT>|), subtraction (\lstinline|op_minus<NumericT>|), multiplication (\lstinline|op_mult<NumericT>|), and division (\lstinline|op_div<NumericT>|).
Similar to unary expressions, binary expressions are seldomly set up by hand.
Two examples of binary expressions are as follows:
\begin{lstlisting}
 ct_binary_expr< ct_variable<0>,    // x
                 op_plus<double>,   // +
                 ct_variable<1> >   // y

 ct_binary_expr< ct_constant<1>,    // 1
                 op_div<double>,    // /
                 ct_variable<0> >   // x
\end{lstlisting}

Typical uses of binary expressions are within the manipulation of compiletime expressions in metafunctions. As an example, outputting the first term of a polynomial is considered:
\begin{lstlisting}
 ct_variable<0> x;
 ct_variable<1> y;
 print_first( x*y + x*x*y - y*y );
\end{lstlisting}
Only two versions of the \lstinline|print_first| function are required.
The first one recursively traverses the binary expression along the left hand side argument:
\begin{lstlisting}
 template <typename L, typename OP, typename R>
 print_first(ct_binary_expr<L, OP, R> const & b)
 { print_first(b.lhs()); } //recursion along left hand side
\end{lstlisting}
The recursion terminates with a general implementation for printing the left-most entry:
\begin{lstlisting}
 template <typename T>
 print_first(T const & t)
 { std::cout << t << std::endl; }
\end{lstlisting}

\NOTE{If a binary operation consists of one object for compiletime and one for runtime evaluation, the compiletime object is converted to a runtime object and then processed as usual in the runtime setting.}

 %\subsection{Expression List}