\begin{intro}
  In the previous chapter, we studied discretizations with
  $\div V_h = Q_h$ with two advantages. First, due to
  \slideref{Corollary}{galerkin-mixed-u-kerb} the velocity error is
  independent of the pressure. Second, the divergence converges faster
  than the gradient. A natural question arising is whether we can do
  something similar for the Stokes problem. There, the equation
  \begin{gather}
    \form(\div v_h, q_h) = 0 \qquad\forall q_h\in Q_h,
  \end{gather}
  would immediately imply $\div v_h=0$, that is, the discrete solution
  is exactly divergence free.

  The answer to this question is a current research topic. So far,
  beginning with the element by Scott and Vogelius, several methods
  have been proposed for special mesh geometry or macro meshes. The
  difficulty is balancing the condition $\div V_h = Q_h$ with the
  $H^1$-conformity of the velocity space. All the spaces in the
  previous chapter were only $\Hdiv$-conforming with discontinuous
  tangential components.

  A fairly simple solution to this question though can be obtained by
  using discontinuous Galerkin methods. These were introduced to
  obtain formulations \emph{consistent} with $H^1$ while not
  \emph{conforming}. Thus, we can apply them directly to
  Raviart-Thomas and Brezzi-Douglas-Marini elements to obtain a
  consistent method with divergence free solutions.

  We begin this chapter by a quick review of the interior penalty
  method before diving into divergence conforming methods.
\end{intro}

\section{The interior penalty method}
\input{../fem/ip}
\subsection{Bounded formulation in $H^1$}
\input{../fem/ip-lifting}

\section{Divergence conforming IP}
\begin{remark}
  The extension of the interior penalty method to vector-valued
  problems is obvious. Furthermore, since the method generates an
  elliptic bilinear form on the discontinuous space $V_h$, this
  ellipticity is inherited by any subspace of
  $V_h\cap\Hdiv(\domain)$. Thus, we can write down the weak
  formulation of a divergence conforming DG method for the Stokes
  equations. In the following definition, we assume slip or no-slip
  boundary conditions, that is, $v\cdot\n=0$ on the whole boundary.
\end{remark}

\begin{Definition}{hdiv-ip}
  A divergence conforming DG method for the Stokes equations consists
  of a discrete velocity space $V_h\subset \Hdiv_0(\domain)$ and a
  pressure space $Q_h\subset L^2_0(\domain)$ such that
  \begin{gather}
    \label{eq:hdivdg:1}
    \div V_h = Q_h.
  \end{gather}
  Using the interior penalty bilinear form $a_h(.,.)$, we search for
  solutions $(u_h,p_h)\in V_h\times Q_h$ such that for all $(v,q)\in
  V_h\times Q_h$ there holds
  \begin{gather}
    \label{eq:hdivdg:2}
    a_h(u_h,v) +\form(\div v,p_h)+\form(\div u_h,q) = f(v).
  \end{gather}
\end{Definition}

\begin{remark}
  Due to the fact that $V_h\not\subset V$, we have introduced the  norm
  $\norm{.}_{1,h}$ on $V_h$. In particular, the norm $\norm{.}_1$ is
  not defined for all elements of $V_h$. Therefore, we need a
  modification of Fortin's lemma (\slideref{Lemma}{fortin}), where the
  norm on the left hand side of the stability
  estimate~\eqref{eq:galerkin:16} uses the discrete norm, namely,
  \begin{gather}
    \norm{\Pi_{V_h}v}_{V_h} \le c \norm{v}_V,
  \end{gather}
\end{remark}

\begin{Lemma}{dg-fortin}
  Let $\{\mesh_h\}$ be a shape-regular sequence of meshes. Then, the
  \putindex{canonical interpolation} operators of the
  Brezzi-Douglas-Marini and Raviart-Thomas elements admit the bound
  \begin{gather}
    \label{eq:hdivdg:4}
    \norm{I_h v}_{1,h} \le c \snorm{v}_1
  \end{gather}
\end{Lemma}

\begin{proof}
  First, we note that all degrees of freedom are defined as cell or
  face integrals with smooth weight functions. Thus, they are bounded
  on $H^1$. Thus, since the local polynomial spaces are finite
  dimensional, there holds on the reference cell $\refcell$ and its
  faces $\refface$:
  \begin{align}
    \norm{I_{\refcell} v}_{1;\refcell} &\le c \snorm{v}_{1;\refcell},
    \\
    \norm{I_{\refcell} v}_{0;\refface} &\le c \snorm{v}_{1;\refcell}.
  \end{align}
  On shape regular meshes, we have the scaling property
  \begin{align}
    \snorm{f}_{m;\cell} &\simeq h_\cell^{\frac{d}{2}-m},
    \\
    \snorm{f}_{m;\face} &\simeq h_\face^{\frac{d-1}{2}-m},
  \end{align}
  such that for a face $\face$ of cell $\cell$
  \begin{align}
    \norm{I_{\cell} v}_{1;\cell} & \le c \snorm{v}_{1;\cell},
    \\
    \norm{I_{\cell} v}_{0;\refface} &\le c h^{\frac12} \snorm{v}_{1;\cell}.
  \end{align}
  We conclude
  \begin{gather}
    \norm{I_h v}_{1,h}^2 \le \sum_{\cell\in\mesh_h}
    \biggl[\norm{I_{\cell} v}_{1;\cell}^2
    + 4 \sum_{\face\subset\d\cell}
    \norm*{\tfrac{\ipp_0}{h_F} I_{\cell} v}_{0;\face}^2
    \biggr] \le c \snorm{v}_{1}^2.
  \end{gather}
\end{proof}

\begin{Corollary}{hdivdg-infsup}
  Assume that the inf-sup condition~\eqref{eq:stokes:1} in
  \slideref{Theorem}{stokes-infsup} holds.
  Then, the method in \slideref{Definition}{hdiv-ip}
  admits the inf-sup condition
  \begin{gather}
    \label{eq:hdivdg:3}
    \inf_{q_h\in Q_h} \sup_{v_h\in V_h}
    \frac{\form(\div v_h,q_h)}{\norm{v_h}_{1,h}\norm{q_h}_0} \ge \beta,
  \end{gather}
  with a constant $\beta >0$ independent of $h$.
\end{Corollary}

\begin{proof}
  First, we make use of the fact that $q_h\in Q_h \subset Q$ to deduce
  from \slideref{Theorem}{stokes-infsup} the there is a function $w\in
  V$ with $\div v=q_h$ and $\norm{v}_1 \le \norm{q_h}_0$. To this
  function, we apply the \putindex{Fortin operator} to define $v_h =
  I_h v$. By the preceding lemma, we have
  \begin{gather}
    \norm{v_h}_{1,h} \le c \norm{v}_1 \le \norm{q_h}_0,
  \end{gather}
  which proves the inf-sup condition.
\end{proof}

\begin{Theorem}{hdivdg-convergence}
  Assume that $(u_h,p_h)\in V_h\times Q_h$ is the solution to the
  divergence conforming DG method in \slideref{Definition}{hdiv-ip} and
  that the continuous Stokes problem is well-posed as in
  \slideref{Theorem}{stokes-infsup}. Then, for the Raviart-Thomas
  pairs $RT_k/\P_k$ and $RT_{[k]}/\Q_k$ with $k\ge 1$ and
  $u$ sufficiently smooth there holds
  \begin{align}
    \label{eq:hdivdg:5}
    \norm{u-u_h}_{1,h} &\le h^k \snorm{u}_{k+1}, \\
    \label{eq:hdivdg:7}
    \norm{p-p_h}_{0} &\le h^k \bigl(\snorm{u}_{k+1} + \snorm{p}_{k}\bigr).
  \end{align}
  Furthermore,
  \begin{gather}
    \label{eq:hdivdg:6}
    \div u_h = 0.
  \end{gather}
\end{Theorem}

\begin{proof}
  The proof follows the lines of the abstract theory of
  \slideref{Theorem}{galerkin-mixed-u-kerbh} and
  \slideref{Theorem}{galerkin-mixed-p}. But since the setting with
  $V_h\not\subset V$ exceeds the assumptions of the abstract theory,
  we adapt the proofs instead of using the results.

  Due to consistency of the method, we have
  \begin{gather}
    a_h(u-u_h, v_h) + \form(\div v_h, p-p_h)
    + \form(\div u-\div u_h, q_h) = 0.
  \end{gather}
  Testing with $v_h=0$ and using $\div V_h = Q_h$ immediately yields
  $\div u_h = \div u = 0$, or
  \begin{gather}
    \ker{B_h} \subset \ker B.
  \end{gather}
  In order to use the ellipticity of $a_h(.,.)$, we insert
  arbitrary functions $w_h \in \ker{B_h}$ and $r_h\in Q_h$. Choosing
  $q_h = 0$ yields the error equation
  \begin{gather}
    \label{eq:hdivdg:8}
    a_h(u_h-w_h, v_h) + \form(\div v_h, p_h-r_h)
    = a_h(u-w_h, v_h) + \form(\div v_h, p-r_h).
  \end{gather}
  Testing with $v_h=u_h-w_h$ and employing $\div v_h=0$, we obtain
  \begin{gather}
    \ellipa \norm{u_h-w_h}_{1,h}^2
    \le a_h(u_h-w_h,u_h-w_h)
    = a_h(u-w_h,u_h-w_h).
  \end{gather}
  Now, we use the canonical interpolation $w_h = I_h u$ to obtain
  \begin{gather}
    \ellipa \norm{u_h-w_h}_{1,h}^2
    \le \frac\ellipa2 \norm{u_h-w_h}_{1,h}^2
    + \frac{c}{2\ellipa} h^{2k} \snorm{u}_{k+1}^2.
  \end{gather}

  Finally, we use the inf-sup condition to find a test function
  $v_h\in V_h$ such that $\div v_h = p_h-r_h$ and $\beta \norm{v_h}_{1,h}
  \le \norm{p_h-r_h}$. Then, the error equation~\eqref{eq:hdivdg:8}
  yields
  \begin{multline}
    \norm{p_h-r_h}
    = \frac{\form(\div v_h,p_h-r_h)}{\norm{p_h-r_h}}
    \\
    = \frac{a_h(u-u_h, v_h) + \form(\div v_h, p-r_h)}
    {\norm{p_h-r_h}}
    \le \tfrac{\norm{a_h}}\beta \norm{u-u_h}_{1,h}
    + \norm{p-r_h}_0
    .
  \end{multline}
  Using the previously proven error estimate for $u_h$ and the
  $L^2$-projection $r_h = \Pi_h p$ yields the result.
\end{proof}

\section{Error estimates by duality}

\begin{intro}
  So far, we have only considered estimates in the so called
  \putindex{energy norm}, that is, a norm such that $a_h(.,.)$ is
  bounded and elliptic\footnote{We use the term energy norm loosely
    here. Strictly speaking, the energy norm would be
    $\norm{v}_A = \sqrt{a_h(v,v)}$.}.

  In the context of elliptic equations, we have seen the duality
  argument of Aubin and Nitsche, which allows us to obtain optimal
  estimates in weaker norms, for instance in $L^2$.

  A particular difficulty here is the fact, that we have to test the
  dual solution with the error \emph{and} exploit some kind of
  Galerkin orthogonality. Thus, we cannot use consistency as before
  and will introduce residual operators later. The analysis here is a
  simplified version of the corresponding results
  in~\cite{GiraultKanschatRiviere14}.
\end{intro}

\begin{Definition}{dual-stokes}
  The \putindex{dual problem} to the Stokes problem in weak for
  consists of finding $(u^*,p^*)\in V_h\times Q_h$ such that for all
  $v\in V$ and $q\in Q$ there holds
  \begin{gather}
    \label{eq:hdivdg:9}
    \form(\nabla v,\nabla u^*) + \form(\div u^*,q) + \form(\div v,p^*)
    = \form(\psi,v).
  \end{gather}
\end{Definition}

\begin{Assumption}{stokes-regularity}
  The dual Stokes problem admits the elliptic regularity estimate
  \begin{gather}
    \label{eq:hdivdg:10}
    \norm{u^*}_{2} \le c \norm{f}_0.
  \end{gather}
\end{Assumption}

\begin{remark}
  Like for scalar elliptic equations, the elliptic regularity
  assumption holds for domains with smooth boundary or with piecewise
  smooth boundary where every corner is convex.
\end{remark}

\begin{Definition}{hdivdg-residual-operators}
  For the solutions $(u,p)\in V\times Q$ and $(u^*,p^*)\in V\times Q$
  of the primal and dual Stokes problem, respectively, we define the
  residual operators
  \begin{align}
    \operatorname{Res}(u,p;v) &= a_h(u,v)+\form(\div v,p) - \form(f,v),
    \\
    \operatorname{Res}^*(v;u^*,p^*)
                              &= a_h(v,u^*)+\form(\div v,p^*) -
                                \form(\psi,v),
  \end{align}
  for $v\in V+V_h$.
\end{Definition}

% From Girault/Kanschat/Riviere

\begin{Lemma}{hdivdg-residual-1}
  Let $(u,p)\in V\times Q$ be the solution to the Stokes problem with
  right hand side $f\in L^2(\domain;\R^d)$. Assume $u\in
  H^s(\domain;\R^d)$ and $p\in H^{s-1}(\domain)$ with $s>3/2$. Then,
  we have for $v\in V+V_h$:
  \begin{gather}
    \label{eq:hdivdg:11}
    \form(f,v) = \form(\nabla u,\nabla v)_{\mesh_h}
    -\forme(\nabla u,\mvl{v\otimes n})_{\faces_h^i}
    -\forme(\d_n u,v)_{\faces_h^\d}
    + \form(\div v,p).
  \end{gather}
\end{Lemma}

\begin{proof}
  We set out from the strong form of the Stokes equations and
  integrate by parts.
  \begin{align}
    \form(f,v) &= \form(-\Delta u + \nabla p, v)
    \\
    &= \form(\nabla u,\nabla v)_{\mesh_h}
      - \sum_{\cell\in\mesh_h} \forme(\d_n u,v)_{\d\cell}
      - \form(\div v,p)
      .
  \end{align}
  Under the regularity assumptions of the lemma, all of these
  integrals make sense at least as duality pairings. In particular,
  $\d_n u\in L^2(\d\cell)$, and thus we can split $\d\cell$ into
  individual faces. Therefore,
  \begin{gather}
    \sum_{\cell\in\mesh_h} \forme(\d_n u,v)_{\d\cell}
    = \forme(\nabla u,\mvl{v\otimes n})_{\faces_h^i}
    +\forme(\d_n u,v)_{\faces_h^\d}.
  \end{gather}
  The proof concludes by collecting the results.
\end{proof}

\begin{Corollary}{hdivdg-residual-2}
  The residual operators can be expressed as
  \begin{gather}
    \label{eq:hdivdg:12}
    \begin{split}
    \operatorname{Res}(u,p;v)
    &= a_h(u,v)
      - \form(\nabla u,\nabla v)_{\mesh_h}
      \\
      &\qquad
      + \forme(\nabla u,\mvl{v\otimes n})_{\faces_h^i}
      +\forme(\d_n u,v)_{\faces_h^\d}.
      \\
    \operatorname{Res}^*(u^*,p^*;v)
    &= a_h(v,u^*)
      - \form(\nabla u,\nabla v)_{\mesh_h}
      \\
      &\qquad
      + \forme(\nabla u,\mvl{v\otimes n})_{\faces_h^i}
      +\forme(\d_n u,v)_{\faces_h^\d}.
    \end{split}
  \end{gather}
  In particular, the residual operators do not depend on the pressure
  solutions.
\end{Corollary}

\begin{Theorem}{hdivdg-l2}
  Let the assumptions of \slideref{Theorem}{hdivdg-convergence}
  and \slideref{Assumption}{stokes-regularity} hold. Then,
  \begin{gather}
    \norm{u-u_h}_0 \le c h^{k+1} \snorm{u}_{k+1}.
  \end{gather}
\end{Theorem}

\begin{Problem}{hdivdg-l2}
  Adapt the proof of \slideref{Theorem}{ip-lifting-l2} to prove
  \slideref{Theorem}{hdivdg-l2}.
\begin{solution}
   We again consider the auxiliary problem
  \begin{gather}
    a(v,u^*) +(\nabla\cdot v, p^*)= \form(u-u_h,v),\qquad\forall v\in V.
  \end{gather}
  Using the definition of the dual residual, we obtain the equation
  \begin{gather}
    \form(u-u_h, v) = a_h(v,u^*) +(\nabla\cdot v, p^*)- \Res^*(u^*,p^*;v),\qquad\forall v\in V+V_h.
  \end{gather}
  Testing with $v=u-u_h$ yields
  \begin{gather}
    \norm{u-u_h}^2 = a_h(u-u_h,u^*) +(\nabla\cdot (u-u_h), p^*)- \Res^*(u^*, p^*;u-u^*).
  \end{gather}
  Additionally, we us the error equation
  \begin{gather}
    a_h(u-u_h, v_h)+(\nabla\cdot v_h, p-p_h) = \Res(u,p;v_h),
  \end{gather}
  tested with $v_h = I_h u^*$, to obtain
  \begin{align}
    \norm{u-u_h}^2 &= a_h(u-u_h,u^*-I_h u^*) +(\nabla\cdot (u-u_h), p^*-q_h)\\
    &\quad- \Res^*(u^*, p^*;u-u^*)
    -\underbrace{(\nabla\cdot I_h u^*, p-p_h)}_{0} + \Res(u,p;I_h u^*).
  \end{align}
  Using the regularity of $u^*$, the first term on the right
  admits the estimate
  \begin{gather}
    \abs{a_h(u-u_h, u^*-I_h u^*)}
    \le \norm{u-u_h}_{1,h}\norm{u^*-I_hu^*}_{1,h}
    \le c h \norm{u-u_h}_{1,h}.
  \end{gather}
  as before.

  For the second term consider $q_h=\Pi_{Q_h} p^*$
  \begin{align}
   (\nabla\cdot (u-u_h), p^*-\Pi_{Q_h} p^*)\leq \norm{\nabla \cdot  (u-u_h)} \norm{p^*-\Pi_{Q_h} p^*}=0
  \end{align}

  For the third term we use \slideref{Lemma}{ip-lifting-residual-2}
  to obtain
  \begin{gather}
    \abs{\Res^*(u^*,u-u_h)} \le c h \snorm{u^*}_2 \norm{u-u_h}_{1,h}.
  \end{gather}
  Finally, using $\jmp{u^*} = 0$, the same lemma yields
  \begin{align}
    \abs{\Res(u, I_h u^*)}
    &\le c h \snorm{u}_2
      \bigl(\norm{\sqrt{\ipp_h}\jmp{I_h u^*}}_{\faces_h^i}
      + \norm{\sqrt{\ipp_h}I_h u^*}_{\faces_h^\d}\bigr)
    \\
    & = c h \snorm{u}_2
      \bigl(\norm{\sqrt{\ipp_h}\jmp{u^*-I_h u^*}}_{\faces_h^i}
      + \norm{\sqrt{\ipp_h}(u^*-I_h u^*)}_{\faces_h^\d}\bigr)
    \\
    & \le c h \snorm{u}_2 h^k \snorm{u^*}_{k+1}
  \end{align}
  This is exactly the same proof we used before.
  Using the energy estimate in \slideref{Theorem}{ip-lifting-h1} we
  can conclude the prove.
\end{solution}

\end{Problem}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "main"
%%% End:


\subsubsection{\stid{3.15} Sake: Trilinos/PEEKS} \label{subsubsect:trilinos}
\paragraph{Overview}
Trilinos is a large and widely used toolkit for scientific computing, with many users both at DOE labs, in academia, and in industry.
This project is focused on making Trilinos ready for exascale. One part is to port a core set of Trilinos packages to relevant architectures
(including NVIDIA, AMD, and Intel GPUs). The other part is to design algorithms that work well on accelerators and at large scale (the focus of
the PEEKS sub-project).

\paragraph{Key  Challenges}
The Trilinos software library needs to be adapted to the new architectures. This is a large undertaking as there are many packages. Therefore, we focus on a core subset of Trilinos related to linear algebra and solvers. We are sensitive that Trilinos has a large user base and therefore need to minimize any user interface changes. Performance portability across a wide range of platforms is a key challenge.

Developing scalable iterative (Krylov) solvers for the US leadership supercomputers
deployed in ECP, we acknowledge three major challenges coming from the hardware
architecture:
\begin{enumerate}
\item
Performance portability for Krylov solvers to perform well on different architectures with a single code base
such that the applications relying on Trilinos for their linear solver needs can smoothly transition to
the new ECP machines.
\item
Fine-grained parallelism in a single node that has to be exploited efficiently
by the iterative solver and the preconditioner.
\item
Rising communication and synchronization cost as the
computational power is growing much faster than memory power, resulting in
increased pressure on the bandwidth of all cache/memory levels.
\end{enumerate}

These challenges require the redesign of existing iterative solvers with respect
to higher parallelism, a reduced number of
communication and synchronization points, favoring computations over
communication, and possibly adopting multiprecision algorithms for efficient hardware
utilization.

\paragraph{Solution Strategy}

The primary thrusts of the Trilinos/PEEKS project are:
\begin{enumerate}
  \item Performance portability:
        We plan to rely heavily on the Kokkos and Kokkos Kernels libraries as that provide kernels that are performance portable across a variety of platforms, including CPU and GPU (NVIDIA, AMD, Intel). There is still much porting work, as some packages rely on UVM (Unified Virtual Memory), which is not widely supported.
        We will ensure the readiness of the following four solver packages on ECP platforms:
        distributed linear algebra (Tpetra), Krylov solvers (Belos), algebraic preconditioners and smoothers (Ifpack2),
        and direct solver interfaces (Amesos2).
  \item Low-synchronization Krylov methods:
    	We will develop and deploy pipelined and
	communication-avoiding Krylov methods in production-quality code, and
	we are actively collaborating with the ECP ExaWind project to integrate
        our new features into their application. Another bottleneck we will address is the orthogonalization needed in GMRES (such as CGS and MGS).
\end{enumerate}

\paragraph{Recent Progress}
\begin{enumerate}
\item Removal of dependency on UVM:
we have removed the dependence of the four solver packages on UVM.
On NVIDIA GPUs, UVM simplifies coding by providing automatic data migration between the CPU and GPU memory. However, there are concerns about how well this will be supported, especially in term of performance, on future GPU systems such as Frontier and Aurora. Hence, removing UVM usage was a prerequisite for preparation to run on Frontier and Aurora platforms.
More generally, the dependence on UVM posed a risk in term of the solver performance, while explicitly managing the data movement will likely improve solver performance.

\item HIP backend support:
we have ensured the Trilinos solver stacks run on the Frontier early access system (Spock) using HIP backend (without UVM) and resolved initial performance problems. Several performance optimizations remain open which we plan to address this year.

\item Polynomial preconditioning: We implemented and deployed a GMRES-based polynomial preconditioner in Trilinos/Belos. This can be used as a preconditioner by itself, as a smoother in multigrid (MueLu), or be combined (nested) with any existing preconditioner. Our code is portable to both CPU and GPU. We showed the method is robust and it does not need any user knowledge of eigenvalues of the matrix/operator, unlike the Chebyshev method. In collaboration with the Exagraph ECP project, it has been integrated into the Sphynx spectral partitioner (Trilinos/Zoltan2).

\item Low-synch orthogonalization: In collaboration with CU Denver and NREL, we developed and implemented a novel low-synch version of Classical Gram-Schmidt (CGS), called DCGS2. By delaying orthonalization and norms, we reduce the synchronization requirement to once per iteration (even with two passes of CGS). The method is more numerically stable than previous attempts at low-synch orthogonalization. Preliminary results on Summit show that the new DCGS2 outperforms the current CGS2 and MGS methods, and achieves up to 66 Gflop/s on 192 GPUs.
\end{enumerate}

\paragraph{Next Steps}
Our next efforts are:
\begin{enumerate}
\item Support OpenMP and SYCL backends and
      perform baseline performance assessments to determine which kernels or algorithms need performance optimization
      on platforms that are relevant to ECP.
\item Conduct performance optimization using HIP, SYCL, and OpenMP backends on the platforms and problems
      that are relevant to ECP.
\item Implement graph assembly on a GPU to enable faster matrix assembly and to avoid data movement between host and device.
\item Design an unified solver interface to all the Trilinos solvers in order to ease the effort of ECP applications
      to switch between solvers and also to compose different solvers in one framework, both of which are needed for the success of ECP application projects especially when the solver options differ across architectures.
\item Deploy DCGS2 (or related orthogonalization method) in Trilinos/Belos, likely as an option in GMRES (which is used by many ECP applications).
\item Study Block Krylov methods, which may be particularly well suited to GPUs, and also appear to reduce communication.
\end{enumerate}


%\begin{document}
\section{Integrated Autocorrelation Time}%
\label{sec:integrated_autocorrelation_time}
%
We include below plots of the integrated autocorrelation time for the
topological charge, \(\tau_{\mathrm{int}}^{\mathcal{Q}}\), for \(\mathcal{Q}
\in \mathbb{Z}\).
%
\begin{figure}[htpb]
  \centering
  \begin{subfigure}{\textwidth}
    \centering
    \includegraphics[width=\textwidth]{autocorrs/tau_int_vs_draws}%
    \caption{\label{fig:tau_int_vs_draws}Estimate of \(\tau_\mathrm{int}\) vs
      number of draws (length of chain) across \(\beta = 5\) (left), \(\beta =
      6\) (center), and \(\beta = 7\) (right), for both HMC (grayscale) and the
    trained sampler (pink).}
    % \caption{Estimate of \(\tau_{\mathrm{int}}^{\mathcal{Q}}\) vs number of
    %   samples, $N$ for \(\beta = 5\) (left), \(6\) (middle), and \(7\)
    %   (right).}%
  \end{subfigure}
  \begin{subfigure}{\textwidth}
    \centering
    \includegraphics[width=\textwidth]{autocorrs/tau_int_vs_traj_len}
    \caption{\label{fig:tau_int_vs_traj_len}Estimate of \(\tau_{\mathrm{int}}\)
      vs trajectory length, \(\lambda\) across \(\beta = 5\) (left), \(\beta =
      6\) (center), and \(\beta = 7\) (right), for both HMC (grayscale) and the
    trained sampler (pink).}
    % \caption{Estimate of \(\tau_{\mathrm{int}}^{\mathcal{Q}}\) vs trajectory
    %   length, \(N_\mathrm{LF} \cdot \varepsilon\) for \(\beta = 5\) (left), \(6\)
    %   (middle), and \(7\) (right).}%
      % \label{fig:tau_int_vs_traj_len}
  \end{subfigure}%
  \caption{Intermediate steps in the calculation of the integrated
    autocorrelation time \(\tau_{\mathrm{int}}^{\mathcal{Q}}\) vs \(\beta\) for
  \(\beta = 5\) (left), \(6\) (middle) \(7\) (right).}
  % \begin{subfigure}{\textwidth}
  %   \centering
  %   \includegraphics[width=0.7\textwidth]{autocorrs/tau_int_vs_beta}
  %   \caption{Estimate of \(\tau_{\mathrm{int}}^{\mathcal{Q}}\) vs \(\beta\) for
  %     both HMC and L2HMC samplers.}%
  %   \label{fig:tau_int_vs_beta}
  % \end{subfigure}
\end{figure}
%
\begin{figure}[htpb]
  \centering
  \includegraphics[width=0.475\textwidth]{autocorrs/tau_int_vs_beta}
  \caption{Estimate of \(\tau_{\mathrm{int}}^{\mathcal{Q}}\) vs \(\beta\) for both
    HMC and L2HMC samplers.}%
  \label{fig:tau_int_vs_beta}
\end{figure}


% First parameter can be changed eg to "Glossary" or something.
% Second parameter is the max length of bold terms.
\begin{mclistof}{List of Variables}{3.2cm}

\section*{Network Theory}

\item[$k$] Node degree, ring size
\item[$p_k$] Node degree distribution, ring statistics
\item[$q_k$] Edge degree distribution
\item[$e_{jk}$] Edge joint degree distribution
\item[$N,E,V$] Number of rings, edges, vertices
\item[$c$] Coordination number
\item[$x_c$] Coordination distribution
\item[$\chi$] Euler characteristic
\item[$m_k$] Mean ring size distribution
\item[$\alpha$] \aw{} parameter
\item[$r$] Assortativity

\section*{Stat Mech}
\item[$\mathcal{U}$] Potential energy
\item[$\mathcal{S}$] Entropy

\end{mclistof}


% intro:
\section{Introduction}
\label{sec:intro}
% context, problem defn:
Many critical problems in program verification can be reduced to
solving systems of Constrained Horn Clauses (CHCs), a class of
logic-programming
problems~\cite{bjorner13,flanagan03,rummer13a,rummer13b}.
%
A CHC is a logical implication with the following form:
$$
  R_1(\vec{v_1}) \leftarrow R_2(\vec{v_2}) \land R_3(\vec{v_3}) \land
  ... \land \varphi(\vec{v_0}, \vec{v_1}, \vec{v_2}, \vec{v_3},...)
$$
Here, the left side of the implication, called the head, contains an
uninterpreted relational predicate applied to a vector of variables.
%
The right side has any number of such predicates conjoined together
with a \emph{constraint} ($\varphi$). The constraint is a logical formula in a background
theory and may use variables named by the predicates.
%
A CHC system is a set of CHCs.
%
The goal of the CHC solving problem is to find suitable interpretations
for each predicate such that each CHC is logically
consistent in isolation.

% introduce the problem of solving recursion-free systems:
In this work we focus on the subclass of CHC systems which are known
as \emph{recursion-free}. In a recursion-free CHC system, no
derivation of a predicate will invoke that predicate.
%
Less formally, a recursion-free CHC system is one where following
implication arrows through the system will never reach the same clause
twice.
%
Recursion-free CHC systems are an important subclass for two reasons.
%
First, recursion-free systems can be used to model safety properties
for hierarchical programs~\cite{lal-qadeer15,lal-qadeer-lahiri12}
(programs with only bounded iteration and recursion).
%
Second and most importantly, a well-known approach for solving a
general CHC system reduces the input problem to solving a sequence
of recursion-free systems.
%
Such approaches attempt to synthesize a solution for the   original
system from the solutions of recursion-free systems~\cite{bjorner13}.
%
The performance of such solvers relies
heavily on the performance of solving recursion-free CHC systems.
%

% current general techniques for solving recursion-free systems
Typically, even recursion-free CHC systems are not solved directly.
%
Instead, they are reduced to a more specific subclass of
recursion-free CHC system.
%
These classes include those of
\emph{body-disjoint} (or \emph{derivation tree})
systems~\cite{heizmann10,bjorner13,mcmillan14,rummer13a,rummer13b} and
of \emph{linear} systems~\cite{albarghouthi12a}.
%
We will discuss these classes in \autoref{sec:overview} and
\autoref{sec:related-work}.
%
Such classes can be solved by issuing
\emph{interpolation queries} to find suitable definitions for the
uninterpreted predicates.
%

% Time complexity
In general, solving a recursion-free CHC system for
propositional logic and the theory of linear integer arithmetic is
co-NEXPTIME-complete~\cite{rummer13b}.
%
In contrast, solving a linear system or body-disjoint system with the
same logic and theories is in co-NP~\cite{rummer13b}.
%
We refer to such classes that are solvable in co-NP time as
\emph{directly solvable}.
%
Because solving an arbitrary recursion-free system is harder than
solving a directly solvable system, solvers which reduce to directly
solvable systems are highly reliant on the size of the reductions.

% contribution of this paper: CDD systems:
The first contribution of this paper is the introduction of a novel
class of directly solvable systems that we refer to as
\emph{Clause-Dependence Disjoint} (CDD).
%
The formal definition of CDD is given at ~\autoref{defn:cdds}.
%
CDD is a strict superset of the union of previously introduced classes
of directly solvable systems.
%
The key characteristic of this class is that when an arbitrary
recursion-free system is reduced to a CDD system and to a system from
a different directly solvable class, the CDD system is frequently the
smaller of the two.
%
Therefore, solving recursion-free systems by reducing them to CDD form
is often less computationally expensive than reducing them to a
system in a different class.

% a new solver
The second contribution of this paper is a solver for CHC systems,
named \sys.
%
Given a recursion-free system $S$, \sys reduces the problem of solving
$S$ to solving a CDD system $S'$.
%
In the worst case, it is possible that the size of $S'$ may be
exponential in the size of $S$.
%
However, empirically we have found that the size of $S'$ is usually
close enough to the size of $S$ that \sys frequently outperforms
\duality, one of the best known CHC solvers.
%
The procedure implemented in \sys is a generalization of existing
techniques that synthesize compact verification conditions for
hierarchical programs~\cite{flanagan01,lal-qadeer15}.
%
Given a general (possibly recursive) CHC system, \sys solves a
sequence of recursion-free systems.
%
Each subsystem is a bounded unwinding of the original system. \sys
attempts to combine the solutions of these recursion-free systems to
synthesize a solution to the original problem, as has been proposed
in previous work~\cite{rummer13b}.

% experience:
We implemented \sys within the \duality CHC solver~\cite{bjorner13},
which is implemented within the \zthree automatic theorem
prover~\cite{moura08}.
%
We evaluated the effectiveness of \sys on standard benchmarks drawn
from SVCOMP15~\cite{svcomp15}.
%
The results indicate that \sys outperforms modern solvers many cases.
%
Futhermore, the results indicate that combining the strengths of \sys
with that of other existing approaches (as discussed in
\autoref{sec:evaluation}) is a promising direction for the future of
CHC solving.

% paper outline:
The rest of this paper is organized as follows.
%
\autoref{sec:overview} illustrates the operation of \sys on a
recursion-free CHC system.
%
\autoref{sec:background} reviews technical work on which \sys is
based.
%
\autoref{sec:approach} describes \sys in technical detail.
%
\autoref{sec:evaluation} gives the results of our empirical evaluation
of \sys.
%
\autoref{sec:related-work} compares \sys to related work.


\documentclass{llncs}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage{amsmath}

\usepackage{llncsdoc}
% \usepackage{color}
% \everymath{\color{blue}}
%\everydisplay{\color{blue}}
\let\displaystyle\textstyle

\newcommand{\ie}[1] {
  \begin{itemize}
    #1
  \end{itemize}
}
% \usepackage{aaai}
\usepackage{times}
\usepackage{helvet}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{courier}
\usepackage{multirow}
    \usepackage[bottom]{footmisc}
\usepackage{microtype}
\usepackage{tikz}
\usepackage{comment}
\usepackage{chngcntr}
\usepackage{float}
\usepackage[algo2e,ruled,linesnumbered,vlined]{algorithm2e}
\counterwithin{figure}{section}
\usepackage{algorithmic}
\usepackage{algorithm}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}

% \usepackage[timestamp, dark]{draftcopy}
% \usepackage{doc}
\usepackage{url}
% \usepackage[sort]{natbib}
%\usepackage{amsmath,amssymb, amsthm}
\usepackage{balance}
% \usepackage[switch, pagewise, mathlines, displaymath]{lineno}

% \setcounter{secnumdepth}{2}
 \usepackage{fancyvrb}
\usepackage{graphicx}

\usepackage{listings}
%opening
\title{}
\author{}

\begin{document}

\section{Definitions}
\begin{definition}[Formula] \label{formula}
{\rm
\ie{
\item $\emptyset$ is a {\em formula}
\item \ $T \in D$, where T is a variable, a %\sparc\
ground term or an arithmetic term,
and $D$ is a set of %\sparc\
ground terms, is a {\em formula},
\item  $t_1\diamond t_2$, where $t_1$ and $t_2$ are terms and
$\diamond \in \{ = ,\neq,  \prec, \preceq\}$, is a {\em formula}, and
\item if $A$ and $B$ are formulas then ($A$ $\land$ $B$), ($A$ $\lor$ $B$), and  $\neg A$ are {\em formulas}.
}
}
\end{definition}

\begin{definition}[Empty Formula] \label{empty formula}
\begin{itemize}
 \item $\emptyset$ is an  {\em empty formula}
 \item if $A$ and $B$ are empty formulas then ($A$ $\land$ $B$), ($A$ $\lor$ $B$), and  $\neg A$ are {\em empty formulas}.
\end{itemize}
 \end{definition}
 In what follows  any empty formula is interpreted as \textbf{false}.

 \begin{definition}[Primitive Formula] \label{primitive formula}
  {\rm
\ie{
\item \ $T \in D$, where T is a variable, a %\sparc\
ground term or an arithmetic term,
and $D$ is a set of %\sparc\
ground terms, is a {\em primitive formula},
\item  $t_1\diamond t_2$, where $t_1$ and $t_2$ are terms and
$\diamond \in \{ = ,\neq,  \prec, \preceq\}$, is a {\em primitive formula}, and
\item if $A$ and $B$ are formulas then    $\neg A$  is a {\em  primitive formula}.
}
}
 \end{definition}

 \begin{definition}[Primitive Conjunction] \label{primitive conjunction}\\
 {\rm
  A formula $\mathcal{F}$ is  a \textit{primitive conjunction} if it is of the form $G_1 \land \dots \land G_n$, where $G_1,\dots,G_n$ are primitive formulas.
}
 \end{definition}

 \begin{definition}[Arithmetic Variable] \label{arithmetic variable} \\
  {\rm
    A variable $X$ occuring in a primitive conjunction $\mathcal{C}=G_1 \land \dots \land G_n$ is called arithmetic with respect to $\mathcal{C}$ if one of the following conditions holds:
    \begin{itemize}
      \item X occurs in an arithmetic term containing at least one arithmetic operation.
      \item one of $G_i$ is of the form  $X \in D$, where $D$ is a range of natural numbers.
      \item one of $G_i$ is of the form  $X \diamond Y$ or $Y \diamond X$, where Y is an arithmetic variable and $\diamond \in \{ \prec,<,\preceq,=\}$.
   \end{itemize}

  }
 \end{definition}
\begin{definition}[Arithmetic Term] \label{aterm}
{\rm
 A term $t$ occuring in a primitive conjunction $\mathcal{C}=G_1 \land \dots \land G_n$ is called \textit{arithmetic}
with respect to $\mathcal{C}$ if one of the following conditions holds:
 \begin{enumerate}
  \item $t$ is a number
  \item $t$ contains an arithmetic operation ('+','-', or '*').
  \item $t$ is not a record and all variables in $t$ are arithmetic with respect to $\mathcal{C}$
 \end{enumerate}
}
\end{definition}

 \begin{definition}[Primitive Arithmetic Constraint] \label{primitive arithmetic constraint}
{\rm
   A primitive constraint $G$ occurring in a primitive conjunction $\mathcal{C}=G_1 \land \dots \land G_n$
is called \textit{arithmetic} with respect to $\mathcal{C}$ if one of the following conditions holds:
 \begin{itemize}
  \item $G$ is of the form $T \in D$, where $T$ is an arithmetic term with respect to $\mathcal{C}$ and $D$ is of the form $n1..n2$.
  \item $G$ is of the form $t_1\diamond t_2$, where both $t_1$ and $t_2$ are arithmetic terms with respect to $\mathcal{C}$.
 \end{itemize}
}
 \end{definition}

\section{Algorithms}
\begin{algorithm2e}[H]\caption{ExpandSolve}
 \DontPrintSemicolon
 %\SetAlgoBlockMarkers{begin}{end}
 \KwIn{Formulas $\mathcal{F}$, ${\cal TODO}$, ${\cal C}$,
         such that $\mathcal{F} \land {\cal TODO} \land {\cal C}$ is non-empty}
 \KwOut{\textbf{True} if $\mathcal{F} \land {\cal TODO} \land {\cal C}$ is satisfiable and \textbf{false} otherwise }
\If{ $\mathcal{F}=\emptyset$}
{
 \If{$\mathcal{TODO}=\emptyset$}
 {
   \Return Solve(Simplify($\mathcal{C}$),$maxint$) \;
 }
 \Else
 {
   Let $\mathcal{TODO}$ be $G_1 \land \dots \land  G_n$ \;
   \Return {ExpandSolve($G_1$,$G_2 \land \dots \land G_n$,$\mathcal{C}$)}\;
 }
}
\ElseIf{$\mathcal{F}=\neg(A \land B)$} {
  ExpandSolve($\neg A \lor \neg B, \mathcal{TODO},\mathcal{C})$\;
}
\ElseIf{$\mathcal{F}=\neg(A \lor B)$} {
    ExpandSolve($\neg A \land \neg B, \mathcal{TODO},\mathcal{C})$\;
}
\ElseIf{$\mathcal{F}=A \lor B$} {
  \If{ExpandSolve($A,\mathcal{TODO},\mathcal{C})$=\textbf{false}}
  {
   \Return ExpandSolve($B,\mathcal{TODO},\mathcal{C}$) \;
  }
  \Else {
   \Return \textbf{true}\;
  }
}
\ElseIf{$\mathcal{F}=A \land B$} {
  \Return ExpandSolve($A, B\land \mathcal{TODO},\mathcal{C}$) \;
}
\Else
{
\Return ExpandSolve($\emptyset,\mathcal{TODO},\mathcal{F} \land \mathcal{C}$)
}
 \Return {\em true} \;
\end{algorithm2e}

\begin{algorithm2e}[H]\caption{Simplify}
 \DontPrintSemicolon
  \KwIn{Primitive conjunction $G_1 \land \dots \land G_n$, the upper limit for natural numbers}
  \KwOut {Primitive conjunction $G_1 \land \dots \land G_m$ after simplification}
  $\mathcal{C}$ := $G_1 \land \dots \land G_n$ \;

  \ForEach {$G_i$ in $G_1 \land \dots \land G_n$}
  {
    \If{$G_i$ is of the form $T \in D$, and T is a ground term }
   {
      \If{T is in D}
      {
        Remove $G_i$ from $\mathcal{C}$\;
      }
      \Else
      {
      \Return \textbf{false}\;
      }
   }

   \If{$G_i$ is of the form $X \in D$, X is an arithmetic variable in $\mathcal{C}$, and D does not contain a number}
   {
     \Return \textbf{false}
   }

   \If{$G_i$ is of the form $T \in D$, T is an arithmetic term with at least one operation, and D does not contain a number}
   {
      \Return \textbf{false}\;
   }
  \If{$G_i$ is of the form  $t_1 \diamond t_2$ and both $t_1$ and $t_2$ are ground terms}
   {
     Let $G_i^\prime$ be obtained from $G_i$ where $<,\leq$ replaced with $\prec,\preceq$ respectively.
     \If {$G_i^\prime$ is true}
     {
        Remove $G_i$ from $\mathcal{C}$\;
     }
     \Else {
        \Return \textbf{false} \;
     }
   }

    \If{$G_i$ is of the form  $t_1 \diamond t_2$, where one of $t_1$ and $t_2$ is an arithmetic term with at least one operation, number, arithmetic variable; and another one is a symbolic term (a string constant or a term built from a functional symbol),
    and diamond is either $=,<,\leq,\prec, or \preceq$ }
   {
        \Return \textbf{false} \;
   }

   \Return $\mathcal{C}$
  }
\end{algorithm2e}

\begin{algorithm2e}[H]\caption{Split}
 \DontPrintSemicolon
  \KwIn{A clingcon rule of the form $r(X_1,\dots X_n):-BODY$}
  \KwOut{A collection of clingcon rules $R$}
  Let $BODY$ be $A_1,A_2,\dots A_m$.
  Let $\mathcal{G}$ be undirected graph with m nodes $N_1,\dots N_m$, such that there is an edge between $N_i$ and $N_j$ iff atoms $A_i$ and $A_j$ share a common variable.
  Let $C_1,\dots, C_k$ be connected components of $\mathcal{G}$. \;
  $R:=\emptyset$ \;
  \ForEach {connected component $C_i$ in   $C_1,\dots, C_k$}
  {
    Let $C_i$ consists of nodes $N_{i_1},\dots N_{i_t}$.
    Let $X_1,\dots, X_p$ be all variables in $A_{i_1},\dots A_{i_t}$. \;
    $R:=R \cup r_i:-A_{i_1},\dots A_{i_t}$ \;
  }
  $R:=R \cup r:-r_1,\dots,r_k.$ \;
  $R:=R \cup  :-not~r.$ \;
\end{algorithm2e}

\begin{algorithm2e}[H]\caption{Solve}
 \DontPrintSemicolon
  \KwIn{Primitive conjunction $G_1 \land \dots \land G_n$, the upper limit for natural numbers $maxint$.}
  \KwOut{\textbf{true} if $G_1 \land \dots \land G_n$ is satisfiable and \textbf{false} otherwise.}
   \tcc*[l]{Build rules for arithmetic constraints}
   $\Pi_{prolog}:= :-use\_module(library(clpfd)).$ \;
   $BODY:= {\bf true}$ \;

   \ForEach{primitive arithmetic $G_i$   of the form $t_1 \diamond t_2$}
   {
     Replace $\prec, = , !=,<=$ with $\#<,\#=,\#=,\#=<$ respectively \;
     $BODY := BODY \land G_i$ \;
   }

   \ForEach{primitive arithmetic  $G_i$   of the form $(t\in D)$,where $D$ is of the form $[n1..n2]$}{
   $BODY := BODY \land  t~in~n_1..n_2$ \;
   }

   \ForEach{primitive arithmetic  $G_i$   of the form $\neg(t\in D)$,where $D$ is of the form $[n1..n2]$, and $g_i$ is an unique label for $G_i$}{
      $\Pi_{prolog} := \Pi_{prolog} \cup g_i(t) :- n\#>n_2. \cup g_i(t) :- n\#<n_2$.
      $BODY := BODY \land  g_i(t)$ \;
   }

   \ForEach{primitive arithmetic $G_i$ of the form $(t \in D)$ and $\neg(t \in D)$ , where $D$ is not of the form $[n1..n2]$}
   {
      Remove all symbolic terms from D\;
      Add the following rule to $\Pi_{prolog}$: (d is an unique label for D) \;
     \texttt{ set\_d(X):-member(X,[t1,\dots tn]).}\;

   }

   \ForEach{primitive non-arithmetic $G_i$ of the form $\neg (t \in D)$:}
   {
    $BODY:=BODY \land \\+~set_d(t)$ \;
   }

  \ForEach{primitive non-arithmetic $G_i$ of the form $t \in D$:}
   {
    $BODY:=BODY \land set_d(t)$ \;
   }

   \ForEach{primitive non-arithmetic $G_i$ of the form $t_1 \diamond t_2$:}
   {
    $BODY:=BODY \land G_i$ \;
   }

    \ForEach {arithmetic variable $X$ in $BODY$} {
      $BODY:=integer(X) \land BODY$
    }

   Let $Y_1,\dots Y_n$ be the set of all variables in $BODY$ \;
   $\mathcal{R}:= p :-BODY$ \;

   $\Pi_{prolog}:=\Pi_{prolog} ~\cup~ \mathcal{R} $\;
   \If{$\Pi_{prolog}$ outputs 'yes' for query ?-p}
   {
     \Return \textbf{true} \;
   }
   \Else
   {
     \Return \textbf{false}\;
   }
 \end{algorithm2e}
\end{document}


\documentclass[fontsize=11pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage[margin=0.75in]{geometry}

\title{CSC110 Fall 2021 Assignment 2: Logic, Constraints, and Nested Data}
\author{Azalea Gui & Peter Lin}
\date{\today}

\begin{document}
\maketitle

\section*{Part 1: Predicate Logic}

\begin{enumerate}

\item[1.]
    \begin{enumerate}
        \item[1.] When $D_1 = [0,\infty) $ \\
            Statement 1 is True because every number $x \in D_1$ is smaller than a $y \in D_1$ (For example, $y=x+1>x$). \\
            Statement 2 is False because when $y=0$, there isn't an $x \in D_1$ smaller than $y$.
        \item[2.] When $D_2 = \mathbb{Z}$ \\
            Statement 1 is True because every integer $x$ is smaller than some integer $y$ (For example, $y=x+1>x$). \\
            Statement 2 is True because every integer $y$ is greater than some integer $x$ (For example, $x=y-1<y$).
        \item[3.] When $D_3 = \{0\}$ \\
            Statement 1 is False because when $x=0$, there isn't a $y \in D_3$ greater than $x$. \\
            Statement 2 is False because when $y=0$, there isn't an $x \in D_3$ smaller than $y$.
    \end{enumerate}

\item[2.]
    \begin{enumerate}
        \item[1.] $P(x): 1 < x < 8$ where $x \in S$
        \item[2.] $Q(x): x < 8$ where $x \in S$
    \end{enumerate}
    When $x \leq 1$ and $x \geq 8$, $P(x)$ is False, so $P(x) \land Q(x)$ would be False and $P(x) \implies Q(x)$ would be True. \\
    When $1 < x < 8$, $P(x)$ and $Q(x)$ are both True, so both $P(x) \land Q(x)$ and $P(x) \implies Q(x)$ would be True. \\
    Since $S = \{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\}$, \\
    Statement 3 would be False because $P(x) \land Q(x)$ is False when $x=0$, \\
    Statement 4 would be True because $P(x) \implies Q(x)$ is true for all values of $x \in S$.

\item[3.]
Complete this part in the provided \texttt{a2\_part1.py} starter file.
Do \textbf{not} include your solution in this file.

\item[4.]
Complete this part in the provided \texttt{a2\_part1.py} starter file.
Do \textbf{not} include your solution in this file.

\end{enumerate}

\section*{Part 2: Conditional Execution}

Complete this part in the provided \texttt{a2\_part2.py} starter file.
Do \textbf{not} include your solution in this file.

\newpage

\section*{Part 3: Generating a Timetable}

\begin{enumerate}

\item[1.]
Complete this part in the provided \texttt{a2\_part3.py} starter file.
Do \textbf{not} include your solution in this file.

\item[2.]

\begin{enumerate}
\item[(a)]

\emph{IMPORTANT DEFINITIONS/NOTATION} (don't change this text!)

We define the following sets:

\begin{itemize}
\item $C$: the set of all possible courses
\item $S$: the set of all possible sections
\item $M$: the set of all possible meeting times
\item $SC$: the set of all possible schedules
\end{itemize}

We also define the following notation for expressions involving the elements of these sets:

\begin{itemize}
\item
The first three (courses/sections/meeting times) are represented as tuples (as described in the assignment handout), and you can use the indexing operation on these values. For example, you could translate ``every section term is in $\{'F', 'S', 'Y'\}$'' into predicate logic as the statement:

    \[\forall s \in S,~ s[1] \in \{'F', 'S', 'Y' \} \]

\item
The start and end times of a meeting time can be compared chronologically using the standard $<$, $\leq$, $>$, and $\geq$ operators.

\item
For a section $s \in S$, $s[2]$ represents a tuple of meeting times.
You may use standard set operations and quantifiers for these tuples (pretend they are sets).
For example, we can say:

    \begin{itemize}
    \item $\forall s \in S,~ s[2] \subseteq M$
    \item $\forall s \in S,~ \forall m \in s[2],~ m[1] < m[2]$
    \end{itemize}

\item
Finally, for a schedule $sc \in SC$, you can use the notation $sc.sections$ to refer to a set of all sections in that schedule.
You can use quantifiers with that set of schedules as well, e.g.
$\forall s \in sc.sections,~ ...$
\end{itemize}

\textbf{Predicate for meeting times conflicting:}
% TODO: fill in the predicate definition for two meeting times conflicting

\begin{align*}
MeetingTimesConflict(m_1, m_2) : m_1[0] == m_2[0] \land m_1[2] > m_2[1] \land m_2[2] > m_1[1] \\
\qquad \text{where $m_1, m_2 \in M$}
\end{align*}

\smallskip

\textbf{Predicate for sections conflicting:}
% TODO: fill in the predicate definition for two sections conflicting.
% Use the MeetingTimesConflict predicate in your response.

\begin{align*}
SectionsConflict(s_1, s_2) : (s_1[1]=\text{'Y'} \lor s_2[1]=\text{'Y'} \lor s_1[1] = s_2[1]) \land \\
\exists m_1 \in s_1[2], \exists m_2 \in s_2[2], \text { s.t. } MeetingTimesConflict(m_1, m_2) \\
\qquad \text{where $s_1, s_2 \in S$}
\end{align*}

\smallskip

\textbf{Predicate for valid schedule:}
% TODO: fill in the predicate definition for a schedule being valid.
% Use the SectionsConflict predicate in your response.

\begin{align*}
IsValidSchedule(sc) : \forall s_1, s_2 \in sc.sections, SectionsConflict(s_1, s_2) \Rightarrow s_1 = s_2
\qquad \text{where $sc \in SC$}
\end{align*}

\item[(b)]
Complete this part in the provided \texttt{a2\_part3.py} starter file.
Do \textbf{not} include your solution in this file.
\end{enumerate}

\item[3.]

\begin{enumerate}
\item[(a)]

You may use all notation from question 2(a).
Note that a course $c \in C$ is a tuple, and $c[2]$ is a set of sections, and so can be quantified over: $\forall s \in c[2], ...$.

\smallskip

\textbf{Predicate for section-schedule compatibility:}
% TODO: fill in the predicate definition for a section being compatible with a schedule.

\begin{align*}
IsCompatibleSection(sc, s) : \forall s_1 \in sc.sections, \neg SectionsConflict(s, s_1)
\qquad \text{where $sc \in SC, s \in S$}
\end{align*}

\smallskip

\textbf{Predicate for course-schedule compatibility:}
% TODO: fill in the predicate definition for a course being compatible with a schedule.
% Use IsCompatibleSection in your response.

\begin{align*}
IsCompatibleCourse(sc, c) : \exists s \in c[2] \text{ s.t. } IsCompatibleSection(sc, s)
\qquad \text{where $sc \in SC, c \in C$}
\end{align*}

\item[(b)]
Complete this part in the provided \texttt{a2\_part3.py} starter file.
Do \textbf{not} include your solution in this file.
\end{enumerate}

\end{enumerate}

\section*{Part 4: Processing Raw Data}
Complete this part in the provided \texttt{a2\_part4.py} starter file.
Do \textbf{not} include your solution in this file.

\end{document}


\documentclass[addpoints,12pt]{exam}
\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%

\usepackage{alltt}
\usepackage{mathtools}
%%\usepackage{marginnote}
%%\usepackage[top=1in, bottom=1in, outer=5.5in, inner=1in, heightrounded, marginparwidth=1in, marginparsep=1in]{geometry}
\usepackage{enumerate}
%% mess with the fonts
%%\usepackage{fontspec}
%%\defaultfontfeatures{Ligatures=TeX} % To support LaTeX quoting style
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
% For package xtable
\usepackage{booktabs}  % Nice toprules and bottomrules
\heavyrulewidth=1.5pt  % Change the default to heavier lines
\usepackage{longtable}
%%\usepackage{tabularx}  % To control the width of the table
% this should make caption font bold.
%%\usepackage{xstring}
%%\usepackage{etoolbox}
%%\usepackage{url}
%% xetex only \usepackage{breakurl}
\usepackage{float} % for fig.pos='H'
%%\usepackage{wrapfig}
%%\usepackage{tikz}
\usepackage{colortbl,xcolor}

\newcommand{\dev}[1] {Dev_{\bar{#1}}}

\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
\header{Math 2300 Section 2}{Equations 1}{February 27, 2015}
\section{Measures of Center}
Mean
\begin{equation*}
\bar{x}=\frac{\sum\limits_{i=1}^{N} x_i }{N}
\end{equation*}
\section{Measures of Spread}
Deviation
\begin{equation*}
Dev_{\bar{x}}=(x_i-\bar{x})
\end{equation*}
\begin{equation*}
Dev_{\bar{x}}^2=(x_i-\bar{x})^2
\end{equation*}
Variance
\begin{multline*}
Var(X)=\frac{\sum_{i=1}^{N} Dev_{\bar{x}}^2}{N}=\frac{\sum_{i=1}^{N} (x_i-\bar{x})^2}{N}
\end{multline*}
Standard Deviation
\begin{equation*}
StdDev(X)=\sqrt{Var(X)}
\end{equation*}
\section{Measures of Association}
Covariance
\begin{equation*}
Cov(X,Y)=\frac{\Sigma_{i=1}^{N} Dev_{\bar{x}}Dev_{\bar{y}}}{N}
\end{equation*}
Correlation
\begin{equation*}
Corr(X,Y)=\frac{Cov(Y,X)}{(StdDev(X)StdDev(Y))}
\end{equation*}
Regression Equation
\begin{equation*}
Y=\alpha + \beta X + \epsilon
\end{equation*}
\begin{equation*}
{\beta}=\frac{Cov(X,Y)}{Var(X)}=Corr(X,Y)\frac{StdDev(Y)}{StdDev(X)}
\end{equation*}
\begin{equation*}
{\alpha}=\bar{y}-{\beta}\bar{x}
\end{equation*}
\end{document}


%%
%%  appendixPolarCalib.tex - Obstacle Detection and Planning for Autonomous Vehicles based on Computer Vision Techniques
%%
%%  Copyright 2014 Néstor Morales <nestor@isaatc.ull.es>
%%
%%  This work is licensed under a Creative Commons Attribution 4.0 International License.
%%

\graphicspath{{./images/chapter04/bmps/}{./images/chapter04/vects/}{./images/chapter04/}}

\chapter{Polar Rectification}\label{ch:appendix_polar_calib}

In chapter \ref{ch:chapter04}, we registered a pair of images obtained between frames, with the aim of distinguishing real from fake obstacles. The best way to do such a registration is through a polar rectification process \citep{pollefeys1999simple}. This non-linear polar rectification method allows registering the images along the time, helping in the detection of motion. The basis of this method is to reparameterize the images by setting the coordinates of their pixels in terms of the epipoles of a stereo pair. In this process, a linear transformation has to be found for every wedge part of the image with vertex at the epipole. This allows rectifying the whole image for every possible epipolar configuration, while keeping the size of resulting image reasonably large. The algorithm is designed in such way that no pixel loss is guaranteed. Also the length of original epipolar lines is preserved. The resulting image is upper-bounded by a size of $(2 (W + H) \times \sqrt{W^2 + H^2})$, where $W$ is the image width and $H$ its height. An implementation of this method is made available at \url{https://github.com/nestormh/PolarCalibration}.

In order to reduce the matching ambiguity to a half of the epipolar line in a case of epipole inside the image, it uses the concept of oriented epipolar geometry. To do this, one point correspondence is needed in both views. With this information, just positive coordinates over the epipolar line will be taken into account. The transfer of corresponding epipolar lines is obtained from the expression:

\begin{equation}\label{eq:cp04_epipolar_lines}
l_{t - 1} \sim H^{-T}l_t\text{, or }l_t \sim H^T l_{t - 1}
\end{equation}

Here, $l'$ is the epipolar line at image at frame $t$, and $l$ the corresponding epipolar line at frame $t - 1$. $H$ is an homography for an arbitrary plane, which can be obtained from the fundamental matrix $F$ \citep{luong1996fundamental}:

\begin{equation}\label{eq:cp04_homography}
H = [e_{t - 1}] \times F + e_{t - 1}^T a
\end{equation}

, where $a$ is a random vector for which $det(H) \neq 0$, so that $H$ is invertible. $e$ is the epipole.

The first step in the polar rectification process consists on the definition of the common region between both images. To do that, we first need to compute the epipoles for both images and the homography $H$. So we need to know first the fundamental matrix $F$. The epipolar geometry is described by the following equation:

\begin{equation}\label{eq:cp04_fundamental_matrix}
m_{L,t - 1}^T \cdot F \cdot m_{L,t}= 0
\end{equation}

, where $m_{L,t - 1}$ and $m_{L,t}$ are homogenous representations of corresponding image points in the left image of the frames $t$ and $t - 1$, respectively. So we need to compute these matches. To ensure that we just get correct matches, we compute the correspondences between image pairs in the following order: $I_{L, t} \rightarrow I_{R, t} \rightarrow I_{R, t - 1} \rightarrow I_{L, t - 1} \rightarrow I_{L, t}$, where $I_{\{L,R\},t}$ is the left ($L$) or right ($R$) image at frame $t$. From a initial set of features in $I_{L, t}$, we get the valid matches in $I_{R, t}$, and the cycle is completed until we reach $I_{L, t}$ again, keeping just the valid matches. A match is valid if satisfies the following rules:
\begin{itemize}
 \item At the end of the cycle, points obtained should be the same as those from which we started the process. If not, a wrong match was found in the way.
 \item Features in $I_{L, t}$ must be in the same row as $I_{R, t}$. As images are rectified, the vertical component should be the same. Same applies to $I_{L, t - 1}$ and $I_{R, t - 1}$.
 \item The 2D distance between features from frame $t$ and $t - 1$ should not be too big, since the frame rate is high and images do not change too much between frames.
\end{itemize}

The result of this matching process is represented at figure \ref{fig:cp04_polar_fund_matrix_computation}. There, each matching cycle is represented by the same random color.

\begin{figure*}[h!]
\centering
\includegraphics{fundamentalMatrixComputation}
\captionof{figure}{Looking for the common points in frames $t$ and $t - 1$.}\label{fig:cp04_polar_fund_matrix_computation}
\end{figure*}

\begin{figure}[p]
\centering
\includegraphics[width=\textwidth]{polar_common_region}
\captionof{figure}{Three possible cases depending on the position of the epipoles.}\label{fig:cp04_polar_common_region}
\end{figure}

\begin{figure}[p]
    \centering
    \begin{tabular}{ cc }
      \includegraphics[width=0.6\textwidth]{polarRectification}\label{fig:cp04_polarRectification} &
      \includegraphics[width=0.3\textwidth]{polarDiff}\label{fig:cp04_polarDiff}
    \end{tabular}
  \caption{Example of a rectified pair of frames. Red line shows that the alignment is correct. On the right, the absolute difference of both rectified images is shown.}\label{fig:cp04_polarRectification_example}
\end{figure}

Once we get these matches, we solve the system described by equation \ref{eq:cp04_fundamental_matrix}, obtaining $F$. From $F$, it is possible to get also the homography $H$ and the epipoles $e_t$ and $e_{t-1}$. So we can start looking for the common region between the images. This common region will be determined by the extremal epipolar lines, which will be those that touch the outer image corners. We must deal with three possible cases depending on
whether the epipoles are located inside or outside the image. These three cases are shown at figure \ref{fig:cp04_polar_common_region}.

There, $I_i^j$ refer to the extremal lines for each image, where $i$ indicates if it is the current ($t$) or the previous ($t - 1$) image, and $j$ is the corner with which the line intersects (1 and 2 for the current image and 3 and 4 for the previous one), so $I_i^j = [e_i] \times c_j$. If the epipole is located inside the image we need to decide which half-epipolar lines, pointing in oposite direction, is the correct one.

% \begin{figure}[h!]
% \centering
% \begin{tabular}{cc}
% \includegraphics[width=0.40\textwidth]{epipolarExtremeLeft1}\label{fig:cp04_epipolarExtremeLeft1} &
% \includegraphics[width=0.40\textwidth]{epipolarExtremeRight1}\label{fig:cp04_epipolarExtremeRight1} \\
% \includegraphics[width=0.40\textwidth]{epipolarExtremeLeft2}\label{fig:cp04_epipolarExtremeLeft2} &
% \includegraphics[width=0.40\textwidth]{epipolarExtremeRight2}\label{fig:cp04_epipolarExtremeRight2} \\
% \end{tabular}
% \captionof{figure}{Two examples of the external epipolar lines found for a case in which the epipole is inside the image for both images (top row); and out, also in both images (bottom row).}\label{fig:cp04_epipolarExtreme}
% \end{figure}

% In image \ref{fig:cp04_epipolarExtreme}, two examples of the outer epipolar lines found are shown.
From these, the common region is defined, which will be represented by the beginning and ending epipolar lines $I_i^B$ and $I_i^E$. If both epipoles are inside the image, an arbitrary epipolar line can be used. In that case, we can avoid boundary effects by adding a small overlap. That is, we will use a region a little bit bigger than $360\textdegree$.

Then, starting from $I_i^B$, we start constructing the rectified image, line by line, until we reach the epipolar line $I_i^E$. This process is repeated for $i=t$ and $i=t-1$ so, at the end of it, we will have both images rectified. In these images, each of the rows will have a correspondence with a certain epipolar line. The distance between two consecutive epipolar lines is determined independently for each of the lines so we avoid pixel compression. The benefits of doing such non-linear warping are that this allows getting the smallest image without information loss.

Using this method, we compute a transformation map that allows knowing the correspondences of the pixels of each image in euclidean and in polar coordinates easily and without a meaningful computational cost. With such a map, we compute a pair of images as those shown in the left side of figure \ref{fig:cp04_polarRectification_example}. Red line demonstrates that the alignment obtained is correct.


\subsection{Choosing Ewald Sum Variables}
\label{ewaldoptim}

\subsubsection{Ewald sum and SPME}

This section outlines how to optimise the accuracy of the Ewald sum
parameters for a given simulation. In what follows the directive {\bf
spme} may be used anywhere in place of the directive {\bf ewald} if
the user wishes to use the Smoothed Particle Mesh Ewald
\index{Ewald!SPME} method.

As a guide\index{Ewald!optimisation} to beginners \D{} will calculate
reasonable parameters if the {\bf ewald precision} directive is used
in the CONTROL file (see section \ref{controlfile}). A relative error
(see below) of 10$^{-6}$ is normally sufficient so the directive

\vskip 1em
\noindent
{\bf ewald precision 1d-6}

\vskip 1em
\noindent
will cause \D{} to evaluate its best guess at the Ewald parameters
$\alpha$, {\tt kmax1}, {\tt kmax2} and {\tt kmax3}.
(The user should note that this represents an {\em estimate}, and there
are sometimes circumstances where the estimate can be improved
upon. This is especially the case when the system contains a strong
directional anisotropy, such as a surface.) These four parameters
may also be set explicitly by the {\bf ewald sum } directive in the
CONTROL file. For example the directive

\vskip 1em
\noindent
{\bf ewald sum 0.35 6 6 8}

\vskip 1em
\noindent
would set $\alpha= 0.35$ \AA$^{-1}$, {\tt kmax1} = 6, {\tt kmax2 = 6}
and {\tt kmax3 } = 8. The quickest check on the accuracy of the Ewald
sum\index{Ewald!summation} is to compare the Coulombic energy ($U$) and the coulombic virial
($\cal W$) in a short simulation.  Adherence to the relationship $U =
-{\cal W}$ shows the extent to which the Ewald sum\index{Ewald!summation} is correctly
converged. These variables can be found under the columns headed {\tt
eng\_cou} and {\tt vir\_cou} in the OUTPUT file (see section
\ref{outputfile}).

The remainder of this section explains the meanings of these
parameters and how they can be chosen.  The Ewald
sum\index{Ewald!summation} can only be used in a three dimensional
periodic system.  There are three variables that control the accuracy:
$\alpha$, the Ewald convergence parameter; $r_{\rm cut}$ the real
space forces cutoff; and the {\tt kmax1,2,3} integers \footnote{{\bf
Important note:} For the SPME method the values of {\tt kmax1,2,3}
should be double those obtained in this prescription, since they
specify the sides of a cube, not a radius of convergence.}  that
effectively define the range of the reciprocal space sum (one integer
for each of the three axis directions).  These variables are not
independent, and it is usual to regard one of them as pre-determined
and adjust the other two accordingly. In this treatment we assume that
$r_{\rm cut}$ (defined by the {\bf cutoff} directive in the CONTROL
file) is fixed for the given system.

The Ewald sum splits the (electrostatic) sum for the infinite,
periodic, system into a damped real space sum and a reciprocal space
sum. The rate of convergence of both sums is governed by $\alpha$.
Evaluation of the real space sum is truncated at $r=r_{\rm cut}$ so it
is important that $\alpha$ be chosen so that contributions to the real
space sum are negligible for terms with $r>r_{\rm cut}$.  The relative
error ($\epsilon$) in the real space sum truncated at $r_{\rm cut}$ is
given approximately by\index{Ewald!optimisation}
\begin{equation}
\epsilon \approx {\rm erfc}(\alpha r_{\rm cut})/r_{\rm cut}
\approx \exp[-(\alpha.r_{\rm cut})^2]/r_{\rm cut} \label{relerr}
\end{equation}

The recommended value for $\alpha$ is 3.2/$r_{\rm cut}$ or greater
(too large a value will make the reciprocal space sum very slowly
convergent). This gives a relative error in the energy of no greater
than $\epsilon = 4\times 10^{-5}$ in the real space sum. When using
the directive {\bf ewald precision} \D{} makes use of a more sophisticated
approximation:
\begin{equation}
{\rm erfc}(x) \approx 0.56 \exp(-x^2)/x
\end{equation}
to solve recursively for $\alpha$, using equation \ref{relerr} to give
the first guess.

The relative error in the reciprocal space term is approximately
\begin{equation}
\epsilon \approx \exp(- k_{max}^2/4\alpha^2)/k_{max}^2
\end{equation}
where
\begin{equation}
k_{max} = \frac{2\pi}{L}~{\tt kmax}
\end{equation}
is the largest $k$-vector considered in reciprocal space, $L$ is the
width of the cell in the specified direction and {\tt kmax} is an integer.

For a relative error of $4\times 10^{-5}$ this means using $k_{max}
\approx 6.2 \alpha$.  {\tt kmax} is then
\begin{equation}
{\tt kmax} > 3.2~L/r_{\rm cut}
\end {equation}

In a cubic system, $r_{\rm cut}~=~L/2$ implies ${\tt kmax}~=~7$.  In
practice the above equation slightly over estimates the value of {\tt
kmax} required, so optimal values need to be found experimentally.  In
the above example {\tt kmax}~=~5 or 6 would be adequate.

If your simulation cell is a truncated octahedron or a rhombic
dodecahedron then the estimates for the {\tt kmax} need to be
multiplied by $2^{1/3}$. This arises because twice the normal number
of $k$-vectors are required (half of which are redundant by symmetry)
for these boundary contributions \cite{smith-93b}.

If you wish to set the Ewald parameters manually (via the {\bf ewald
sum} or {\em spme sum} directives) the recommended approach is as follows\index{Ewald!optimisation}. Preselect the
value of $r_{\rm cut}$, choose a working a value of $\alpha$ of about
$3.2/r_{\rm cut}$ and a large value for the {\tt kmax} (say 10 10 10
or more).  Then do a series of ten or so {\em single} step simulations
with your initial configuration and with $\alpha$ ranging over the
value you have chosen plus and minus 20\%. Plot the Coulombic energy
(and $-{\cal W}$) versus $\alpha$. If the Ewald sum\index{Ewald!summation} is correctly
converged you will see a plateau in the plot.  Divergence from the
plateau at small $\alpha$ is due to non-convergence in the real space
sum. Divergence from the plateau at large $\alpha$ is due to
non-convergence of the reciprocal space sum.  Redo the series of
calculations using smaller {\tt kmax} values. The optimum values for
{\tt kmax} are the smallest values that reproduce the correct
Coulombic energy (the plateau value) and virial at the value of
$\alpha$ to be used in the simulation.

Note that one needs to specify the three integers ({\tt kmax1, kmax2,
kmax3}) referring to the three spatial directions, to ensure the
reciprocal space sum is equally accurate in all directions. The values
of {\tt kmax1}, {\tt kmax2} and {\tt kmax3} must be commensurate with
the cell geometry to ensure the same minimum wavelength is used in all
directions.  For a cubic cell set {\tt kmax1} = {\tt kmax2} = {\tt
kmax3}.  However, for example, in a cell with dimensions $2A = 2B = C$
(ie. a tetragonal cell, longer in the c direction than the a and b
directions) use 2{\tt kmax}1 = 2{\tt kmax}2 = ({\tt kmax}3).

If the values for the {\tt kmax} used are too small, the Ewald sum\index{Ewald!summation} will
produce spurious results. If values that are too large are used, the
results will be correct but the calculation will consume unnecessary
amounts of {\em cpu} time. The amount of {\em cpu} time increases with
${\tt kmax1}\times{\tt kmax2} \times {\tt kmax3}$.

\subsubsection{Hautman Klein Ewald Optimisation}

Setting the HKE \index{Ewald!Hautman Klein} parameters can also be
achieved rather simply, by the use of a {\bf hke precision}
directive in the CONTROL file e.g.

\vskip 1em
\noindent
{\bf hke precision 1d-6 1 1}

\vskip 1em
\noindent
which specifies the required accuracy of the HKE convergence
functions, plus two additional integers; the first specifying the
order of the HKE expansion ({\tt nhko}) and the second the maximum
lattice parameter ({\tt nlatt}). \D{} will permit values of {\tt nhko}
from 1-3, meaning the HKE Taylor series expansion may range from
zeroth to third order. Also {\tt nlatt} may range from 1-2, meaning
that (1) the nearest neighbour, and (2) and next nearest neighbour,
cells are explicitly treated in the real space part of the Ewald
sum. Increasing either of these parameters will increase the accuracy,
but also substantially increase the cpu time of a simulation. The
recommended value for both these parameters is 1 and if {\em both} these
integers are left out, the default values will be adopted.

As with the standard Ewald and SPME methods, the user may set alternative
control parameters with the CONTROL file {\bf hke sum} directive e.g.

\vskip 1em
\noindent
{\bf hke sum 0.05 6 6 1 1}

\vskip 1em
\noindent
which would set $\alpha=0.05~$\AA$^{-1}$, {\tt kmax1 = 6}, {\tt kmax2 =
6}. Once again one may check the accuracy by comparing the Coulombic
energy with the virial, as described above. The last two integers
specify, once again, the values of {\tt nhko} and {\tt nlatt}
respectively. (Note it is possible to set either of these to zero in
this case.)

Estimating the parameters required for a given simulation follows a
similar procedure as for the standard Ewald method (above), but is
complicated by the occurrence of higher orders of the convergence
functions. Firstly a suitable value for $\alpha$ may be obtained when
{\tt nlatt}=0 from the rule: $\alpha=\beta/r_{cut}$, where $r_{cut}$
is the largest real space cutoff compatible with a single MD cell and
$\beta$=(3.46,4.37,5.01,5.55) when {\tt nhko}=(0,1,2,3)
respectively. Thus in the usual case where {\tt nhko}=1, $\beta$=4.37.
When {\tt nlatt}$\ne$0, this $\beta$ value is multiplied by a factor
$1/(2*nlatt+1)$.

The estimation of {\tt kmax1,2} is the same as that for the standard
Ewald method above. Note that if any of these parameters prove to be
insufficiently accurate, \D{} will issue an error in the OUTPUT file,
and indicate whether it is the real or reciprocal space sums that is
questionable.


\XtoCBlock{I}
\label{block:I}
\begin{figure}[H]\includegraphics{I}\end{figure}

\begin{XtoCtabular}{Inports}
In & Control error input\tabularnewline
\hline
Init & Value which is loaded at initialization function call\tabularnewline
\hline
Enable & Enable == 0: Deactivation of block; Out set to 0

Enable 0->1: Preload of integral part

Enable == 1: Activation of block\tabularnewline
\hline
\end{XtoCtabular}

\begin{XtoCtabular}{Outports}
Out & Control value\tabularnewline
\hline
\end{XtoCtabular}

\begin{XtoCtabular}{Mask Parameters}
Ki & Integral Factor\tabularnewline
\hline
ts\_fact & Multiplication factor of base sampling time (in integer format)\tabularnewline
\hline
\end{XtoCtabular}

\subsubsection*{Description:}
I controller:

    G(s) = Ki/s = 1/(Ti*s)

% include optional documentation file
\InputIfFileExists{\XcHomePath/Library/Control/Doc/I_Info.tex}{\vspace{1ex}}{}

\subsubsection*{Implementations:}
\begin{tabular}{l l}
\textbf{FiP8} & 8 Bit Fixed Point Implementation\tabularnewline
\textbf{FiP16} & 16 Bit Fixed Point Implementation\tabularnewline
\textbf{FiP32} & 32 Bit Fixed Point Implementation\tabularnewline
\textbf{Float32} & 32 Bit Floating Point Implementation\tabularnewline
\textbf{Float64} & 64 Bit Floating Point Implementation\tabularnewline
\end{tabular}

\XtoCImplementation{FiP8}
\index{Block ID!3200}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP8 \tabularnewline
\textbf{ID} & 3200 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & I\_FiP8.c \tabularnewline
\textbf{H filename} & I\_FiP8.h \tabularnewline
\end{tabular}
\vspace{1ex}

8 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
b0 & Integral coefficient\tabularnewline
\hline
sfr & Shift factor for I coefficient b0\tabularnewline
\hline
i\_old & Integrator value from previous cycle\tabularnewline
\hline
enable\_old & Enable value of previous cycle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int8          *In;
     int8          *Init;
     int8          *Enable;
     int8          Out;
     int8          b0;
     int8          sfr;
     int16         i_old;
     int8          enable_old;
} I_FIP8;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_I_FiP8.tex}{}{}
\fi
\XtoCImplementation{FiP16}
\index{Block ID!3201}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP16 \tabularnewline
\textbf{ID} & 3201 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & I\_FiP16.c \tabularnewline
\textbf{H filename} & I\_FiP16.h \tabularnewline
\end{tabular}
\vspace{1ex}

16 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
b0 & Integral coefficient\tabularnewline
\hline
sfr & Shift factor for I coefficient b0\tabularnewline
\hline
i\_old & Integrator value from previous cycle\tabularnewline
\hline
enable\_old & Enable value of previous cycle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int16         *In;
     int16         *Init;
     int8          *Enable;
     int16         Out;
     int16         b0;
     int8          sfr;
     int32         i_old;
     int8          enable_old;
} I_FIP16;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_I_FiP16.tex}{}{}
\fi
\XtoCImplementation{FiP32}
\index{Block ID!3202}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & FiP32 \tabularnewline
\textbf{ID} & 3202 \tabularnewline
\textbf{Revision} & 1.0 \tabularnewline
\textbf{C filename} & I\_FiP32.c \tabularnewline
\textbf{H filename} & I\_FiP32.h \tabularnewline
\end{tabular}
\vspace{1ex}

32 Bit Fixed Point Implementation

\begin{XtoCtabular}{Controller Parameters}
b0 & Integral coefficient\tabularnewline
\hline
sfr & Shift factor for I coefficient b0\tabularnewline
\hline
i\_old & Integrator value from previous cycle\tabularnewline
\hline
enable\_old & Enable value of previous cycle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     int32         *In;
     int32         *Init;
     int8          *Enable;
     int32         Out;
     int32         b0;
     int8          sfr;
     int64         i_old;
     int8          enable_old;
} I_FIP32;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_I_FiP32.tex}{}{}
\fi
\XtoCImplementation{Float32}
\index{Block ID!3203}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & Float32 \tabularnewline
\textbf{ID} & 3203 \tabularnewline
\textbf{Revision} & 0.1 \tabularnewline
\textbf{C filename} & I\_Float32.c \tabularnewline
\textbf{H filename} & I\_Float32.h \tabularnewline
\end{tabular}
\vspace{1ex}

32 Bit Floating Point Implementation

\begin{XtoCtabular}{Controller Parameters}
b0 & Integral coefficient\tabularnewline
\hline
i\_old & Integrator value from previous cycle\tabularnewline
\hline
enable\_old & Enable value of previous cycle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     float32       *In;
     float32       *Init;
     int8          *Enable;
     float32       Out;
     float32       b0;
     float32       i_old;
     int8          enable_old;
} I_FLOAT32;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_I_Float32.tex}{}{}
\fi
\XtoCImplementation{Float64}
\index{Block ID!3204}
\nopagebreak[0]
% Implementation details
\begin{tabular}{l l}
\textbf{Name} & Float64 \tabularnewline
\textbf{ID} & 3204 \tabularnewline
\textbf{Revision} & 0.1 \tabularnewline
\textbf{C filename} & I\_Float64.c \tabularnewline
\textbf{H filename} & I\_Float64.h \tabularnewline
\end{tabular}
\vspace{1ex}

64 Bit Floating Point Implementation

\begin{XtoCtabular}{Controller Parameters}
b0 & Integral coefficient\tabularnewline
\hline
i\_old & Integrator value from previous cycle\tabularnewline
\hline
enable\_old & Enable value of previous cycle\tabularnewline
\hline
\end{XtoCtabular}

% Implementation data structure
\XtoCDataStruct{Data Structure:}
\begin{lstlisting}
typedef struct {
     uint16        ID;
     float64       *In;
     float64       *Init;
     int8          *Enable;
     float64       Out;
     float64       b0;
     float64       i_old;
     int8          enable_old;
} I_FLOAT64;
\end{lstlisting}

\ifdefined \AddTestReports
\InputIfFileExists{\XcHomePath/Library/Control/Doc/Test_I_Float64.tex}{}{}
\fi


\documentclass{article}

\usepackage{arxiv}

\usepackage[utf8]{inputenc} % allow utf-8 input
\usepackage[T1]{fontenc}    % use 8-bit T1 fonts
\usepackage{hyperref}       % hyperlinks
\usepackage{url}            % simple URL typesetting
\usepackage{booktabs}       % professional-quality tables
\usepackage{amsfonts}       % blackboard math symbols
\usepackage{nicefrac}       % compact symbols for 1/2, etc.
\usepackage{microtype}      % microtypography
\usepackage{lipsum}		% Can be removed after putting your text content
\usepackage{amssymb,amsmath}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{apacite}

\title{Finding the maximum a-posteriori orbit of an agent-based model\\
*** UNFINISHED DRAFT ***}

%\date{September 9, 1985}	% Here you can change the date presented in the paper title
%\date{} 					% Or removing it

\author{
  Daniel Tang\\
  Leeds Institute for Data Analytics\\
  University of Leeds\\
  Leeds, UK\\
  \texttt{D.Tang@leeds.ac.uk} \\
  %% examples of more authors
  %% \AND
  %% Coauthor \\
  %% Affiliation \\
  %% Address \\
}

\begin{document}
\maketitle

\begin{abstract}
We describe an algorithm to find the orbit from time $t=0$ to $t=T$ of an agent based model that has the maximum posterior probability given partial observations of the state at time $t=T$.

This is an unfinished draft which may contain errors and is subject to change.
\end{abstract}

% keywords can be removed
\keywords{Data assimilation, Agent based model, Quantum field theory, Probabilistic programming}

\section{Introduction}
%##########################################

It has been shown\cite{tang2019data} that a probability distribution over states of an agent based model can be described as an operator made up of creation and annihilation operators acting on an empty model-state, $\emptyset$. For a certain class of agent-based models, the behaviour of the agents can be expressed as a Hamiltonian operator that transforms a probability distribution over model states into the rate-of-change of that distribution.

Given the Hamiltonian, $H$, the probability distribution over the agent-based model states at time $t$ is given by
\[
\psi_t = e^{Ht}\psi_0
\]
where $\psi_0$ is the distribution at time $t=0$.

\section{Factorized integration}
%################################

Suppose we want to calculate
\[
e^{Ht}S_0\emptyset
\]

From equation \ref{Hcommutation} we have
\[
e^{Ht}S_0\emptyset = e^{[H,.]t}S_0\emptyset
\]

Now using equation \ref{uniformisation} we have
\[
e^{Ht}S_0\emptyset = \sum_{n=0}^\infty  \frac{(\gamma t)^n e^{-\gamma t}}{n!}\left(I + \frac{[H,.]}{\gamma}\right)^nS_0\emptyset
\]
But if we set $\gamma = \sum_n \rho_n$ we can split the term inside the brackets into separate acttions and interactions of the form
\[
I + \frac{[H,.]}{\gamma} =
\sum_a \frac{\rho_a}{\gamma}\left(I + [(a^\dag_{j_a}\ldots - a^\dag_{i_a})a_{i_a},.] \right) +
\sum_b \frac{\rho_b}{\gamma}\left(I + [(a^\dag_{k_b}\dots a^\dag_{l_b} - a^\dag_{j_b}a^\dag_{i_b})a_{j_b}a_{i_b},.] \right)
\]

So, if we let
\[
\alpha = \left\{ \frac{\rho_a}{\gamma}\left(I + [(a^\dag_{j_a}\ldots - a^\dag_{i_a})a_{i_a},.] \right) \right\}
\]
be the set of commutated, uniformatized actions of $H$ and
\[
\beta = \left\{ \frac{\rho_b}{\gamma}\left(I + [(a^\dag_{k_b}\dots a^\dag_{l_b} - a^\dag_{j_b}a^\dag_{i_b})a_{j_b}a_{i_b},.] \right) \right\}
\]
be the set of commutated, uniformatized interactions of $H$, and let $\chi = \alpha \cup \beta$ we have
\[
e^{Ht}S_0\emptyset = \sum_{n=0}^\infty  \frac{(\gamma t)^n e^{-\gamma t}}{n!}\left(\sum_{A\in \chi} A\right)^nS_0\emptyset
\]
As long as multiple agents do not occupy the same state, each term in the expansion of this sum (in terms of products of commutated, uniformatized actions and interactions) represents a possible orbit of the model from $S_0$ over time $t$.

\section{Finding the MAP over the orbits}
%##########################################

Suppose we have a posterior distribution
\[
\Omega e^{Ht}S_0\emptyset = \Omega \sum_{n=0}^\infty  \frac{(\gamma t)^n e^{-\gamma t}}{n!}\left(\sum_{A\in \chi} A\right)^nS_0\emptyset
\]
and we wish to find the term in the expansion of
\[
\left(\sum_{A\in \chi} A\right)^n
\]
that has maximum probability.

Suppose we label the memebers of $\chi$, $A_1...A_m$ and let $w_i = \frac{\rho_i}{\gamma}$ be the weight of the $i^{th}$ act, $A_i$. Let $R_k$ be the number of annihilation operators of state $k$ in $\Omega$, and let $S_k$ be the number of creation operators of state $k$ in $S_0$.

Let an orbit consist of a list of integers $t_1...t_n$ where $1 \le t_i \le m$, corresponding to the acts $A_{t_1}\dots A_{t_n}$.

Let $r_{ik}$ be the number of annihilation operators of state $k$ in $A_i$, and let $c_{ik}$ be the number of creation operators of state $k$ in the term $A_i\prod_j a^{\dag r_{ij}}_j$.

An orbit is feasible iff
\[
\forall i,k: S_k- r_{t_ik} + \sum_{j=i+1}^n c_{t_jk} -r_{t_jk}  \ge 0
\]
and for the observations
\[
\forall k: S_k + \sum_{i=1}^n c_{t_ik} - r_{t_ik} \ge R_k
\]

This can be expressed as a constrained optimisation problem in the following way. Let
\[
0 \le b_{ij} \le 1
\]
be a set of integer indicator variables.

For each pair of acts $(A_p,A_q)$ that do not commute, add the constraint
\[
b_{ip} + b_{iq} \le 1
\]
and we want $n$ terms in total, so
\[
\sum_i\sum_j b_{ij} = n
\]

The feasibility constraints can be expressed as
\[
\forall i,k:    S_k - \sum_j r_{jk}b_{ij} + \sum_{l=i+1}^n\sum_m \left(c_{mk} - r_{mk}\right)b_{lm}  \ge 0
\]
and
\[
\forall k: S_k + \sum_{l=1}^n\sum_j \left(c_{jk} - r_{jk}\right)b_{lj} -R_k \ge 0
\]

Within these constraints, we wish to find the assignment to the $b_{ij}$ that maximises
\[
W = \sum_i\sum_j b_{ij}\log(w_j)
\]

This is an integer programming problem which can be solved by the branch-and-cut algorithm.

Once we have a solution, we can read off the $t_1...t_n$ by taking the set $\left\{(i,j): b_{ij}=1\right\}$ and ordering the members $(i_1,j_1) \dots (i_n,j_n)$ such that $\forall k: i_k \le i_{k+1}$\footnote{Since members with the same $i$ value correspond to acts that commute, it doesn't matter which order they are put in.}. The orbit is now given by $j_1\dots j_n$ which corresponds to acts $A_{j_1}\dots A_{j_n}$.

By solving for different values of $n$ and adding
\[
\log\left(\frac{(\gamma t)^n e^{-\gamma t}}{n!}\right)
\]
to each solution, we then simply choose the maximum to give the MAP orbit. As $n$ increases above $\gamma t$ it becomes increasingly unlikely that we'll find a better orbit.

\section{Extension to any timestepping ABM}

Although this algorithm was developed for use with models whose dynamics are described in terms of annihilation and creation operators, it would seem that the same approach could be used with a little modification to find the MAP orbit of any timestepping agent based model. In place of the observation operator we add the relevant constraints to the integer programming problem and in place of the commutated, uniformatised actions and interactions we put the timesteps of an agent along with its pre-requisites. As long as all these can be expressed as linear constraints, we can perform the same optimisation to find the MAP. Finally, rather than summing over all path lengths, we have a fixed number of timesteps.

More formally, suppose we have an ABM such that the probability that the model in state $S_t$ will transition into state $S_{t+1}$ in a timestep can be expressed in the form
\[
P(S_{t+1},S_t) = \sum_{S_{t+1} = \cup_a C_a \cup \cup_{ab} C_{ab}} \prod_a P(C_{a} | s_{a}) + \prod_{a,b} P(C_{ab}|s_a, s_b)
\]

i.e. each agent has a set of "proprensities to act", while pairs of agents have additional propensities to act as a group. In this case we can express the timestep on the model state as an operator that is the sum of commutated, uniformised acts.

Or, even more generally, we have a single primary requirement (i.e. the agent itself) and a set of secondary requirements. A legel timestep then consists of a set of acts whose primary and secondary requirements are met, with the additional constraint that the set of primary requirements should exactly cover the set of agents (i.e. there is a matching between agents and primary requirements). This is equivalent, in the quantum version, to saying that an interaction must replace at least one agent back in its original state. So, in-fact it is less general than the operator that is a set of acts! (but the requirement allows us to activate multiple acts in one factor when performing branch-and-cut)

%\bibliographystyle{unsrtnat}
%\bibliographystyle{apalike}
\bibliographystyle{apacite}
\bibliography{references}

\newpage
\appendix

\section{Appendix: Proof that $e^{Ht}X\emptyset = e^{[H,.]t}X\emptyset$}
% ####################################################

By definition
\begin{equation}
e^{Ht}X\emptyset = \sum_{n=0}^\infty \frac{t^n}{n!} H^nX\emptyset
\label{exponential}
\end{equation}
but
\[
H^nX\emptyset = H^{n-1}(XH + [H,X])\emptyset
\]
However, since all terms in $H$ have annihilation operators, $XH\emptyset = 0$ for all $X$ so
\begin{equation}
H^nX\emptyset = H^{n-1}[H,X]\emptyset
\label{recurrence}
\end{equation}

Let $[H,.]$ be the ``commute H with'' operator, so that
\[
[H,\,.\,](X) = [H,X]
\]
and
\[
[H,\,.\,]^n(X) = [H,\dots [H,[H,X]]\dots ]
\]
is the $n$-fold application of the commutator to $X$. Note that $[H,.](X)Y \ne [H,.](XY)$ so we use brackets where there is any ambiguity.

From equation \ref{recurrence}
\[
H^nX\emptyset = [H,.]^n(X)\emptyset
\]
Substituting into equation \ref{exponential}
\begin{equation}
e^{Ht}X\emptyset = \sum_{n=0}^\infty \frac{t^n}{n!} [H,.]^n X\emptyset = e^{[H,.]t}X\emptyset
\label{Hcommutation}
\end{equation}

\section{Uniformisation of the Hamiltonian}
% ####################################################
The numerical properties of the exponential of the Hamiltonian can be improved in a way analogous to that of a continuous time Markov chain \cite{reibman1988numerical}.

Let $I$ be the identity operator. By definition
\[
e^{kI} = \sum_{n=0}^\infty \frac{(kI)^n}{n!}
\]
but since $I^n=I$ for all $n$
\[
e^{kI} = \sum_{n=0}^\infty \frac{k^n}{n!}I = e^k
\]
So

\begin{equation*}
e^{At} = e^{\frac{A}{\gamma}\gamma t} = e^{\left(I - I + \frac{A}{\gamma}\right)\gamma t} = e^{-\gamma t}e^{\left( I + \frac{A}{\gamma}\right)\gamma t}
\end{equation*}

\begin{equation}
e^{At} = \sum_{n=0}^\infty  \left(I + \frac{A}{\gamma}\right) ^n\frac{(\gamma t)^n e^{-\gamma t}}{n!}
\label{uniformisation}
\end{equation}

\end{document}


\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb,amsfonts}
\usepackage{listings}

\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}

\newenvironment{problem}[2][Problem]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}
%If you want to title your bold things something different just make another thing exactly like this but replace "problem" with the name of the thing you want, like theorem or lemma or whatever

\begin{document}

%\renewcommand{\qedsymbol}{\filledbox}
%Good resources for looking up how to do stuff:
%Binary operators: http://www.access2science.com/latex/Binary.html
%General help: http://en.wikibooks.org/wiki/LaTeX/Mathematics
%Or just google stuff

\title{Assignment 2\\ MEEN 357}
\author{Jacob Hartzer}
\maketitle

\section*{Task 4}

\begin{lstlisting}
 format long e;
(0.6 + 0.6 + 0.6) - 1.8

ans =

	-2.220446049250313e-16

\end{lstlisting}

This is not the result that one would expect from the perspective of mathematics. From a math point of view, the answer is clearly exactly zero. However, these floating point numbers aren’t representable as a simple, finite string of binary code. Therefore, there is rounding during each of the above operations. The total rounding is summarized by the above non-zero answer.

\end{document}