% !TEX options=--shell-escape
\documentclass [12pt]{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{float}
\usepackage{multirow}
\usepackage{xcolor}
\usepackage [ruled,vlined,commentsnumbered,titlenotnumbered]{algorithm2e} \usepackage{array}
\usepackage{booktabs}
\usepackage{url}
\usepackage{parskip}
\usepackage [margin=1in]{geometry}
\usepackage [T1]{fontenc}
\usepackage{cmbright}
\usepackage [many]{tcolorbox}
\usepackage [colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}
\usepackage{enumitem}
\usepackage{xparse}
\usepackage{verbatim}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{csquotes}
\usepackage[cache=false]{minted}
\usepackage{mdframed}
\usepackage{tikz}
\usepackage{algpseudocode}
\usepackage{wrapfig}

\usetikzlibrary{shapes.symbols}
\newtheorem{theorem}{Theorem}

\BeforeBeginEnvironment{minted}{\begin{mdframed}}
\AfterEndEnvironment{minted}{\end{mdframed}}

\DeclareTColorBox{Solution}{}{breakable, title={Solution}}
\DeclareTColorBox{Solution*}{}{breakable, title={Solution (provided)}}
\DeclareTColorBox{Instruction}{}{boxrule=0pt, boxsep=0pt, left=0.5em, right=0.5em, top=0.5em, bottom=0.5em, arc=0pt, toprule=1pt, bottomrule=1pt}
\DeclareDocumentCommand{\Expecting }{+m}{\textbf{[We are expecting:} #1\textbf{]}}
\DeclareDocumentCommand{\Points }{m}{\textbf{(#1 pt.)}}
\newcommand{\hint }[1]{\noindent{[\textbf{HINT:} \em #1 \em ]}} \newcommand{\pts }[1]{\textbf{(#1 pt.)}}

\begin{document}

{\LARGE \textbf{COMP 285 (NC A\&T, Spr `22)}\hfill \textbf{Homework 10} }
\vspace{1em}
\begin{Instruction}

\paragraph{Due.} Friday, April 29th, 2022 @ 11:59 PM!
\end{Instruction}

\vspace{1em}
\begin{Instruction} \paragraph{Homework Expectations:} Please see \href{https://www.comp285.ml/homework/#general-homework-information}{Homework}.
\end{Instruction}

\vspace{1em}
\begin{Instruction}

\paragraph{Exercises} The following questions are exercises. We encourage you to work with a group and discuss solutions to make sure you understand the material.

\paragraph{Points} This assignment is graded out of 30 points.

\end{Instruction}

\begin{centering}
\section*{Fun with MSTs, Flows, and Cuts}
\end{centering}

\begin{Instruction}

\paragraph{Written Problems} The following questions are to be submitted in written/typed form to gradescope.

\end{Instruction}

\section{Learning for Fun \Points{0}}
Watch 1 (<30 minute video) or read 2 before you start the following two questions:

\begin{enumerate}
  \item If you prefer a lecture format, this is a \href{https://www.youtube.com/watch?v=wU6udHRIkcc}{really good video resource} (as a general note, this person's lectures are high quality across the board). Slight terminology differences in this video from the HW and textbook: he uses “weighted union” which is similar to the “union by rank”, and he says “collapsing find” which is essentially “path compression”.
  \item If you prefer a written resource, Ch 21 in the CLRS textbook is fully dedicated to the topic. You can skip over the exercises and detailed math proofs to understand the main ideas, and they are very similar to the ideas explained in the video.
\end{enumerate}

\pagebreak
\section{Understanding Kruskal's Algorithm \Points{8}}
In class, we showed an abbreviated version of Kruskal's pseudocode. The actual pseudocode looks like this:

\begin{verbatim}
algorithm kruskal(G, w)
  Input: A connected undirected graph G = (V, E) with edge weights w(u, v)
    for all (u,v) in E
  Output: A minimum spanning tree defined by the edges X

  for all u in V:
    makeset(u)
  X = {}
  Sort the edges E by weight
  for all edges {u, v} in E, in increasing order of weight:
    if find(u) != find(v):
      add edge {u, v} to X
      union(u, v)
  return X
\end{verbatim}

Recall during the Kruskal's lecture (\href{https://www.comp285.ml/lectures/#kruskal-s-algorithm-and-max-flow}{Lecture 32}) how we would make sure that adding an edge between two nodes would not create a cycle. A data structure called "Disjoint Sets" allow us to do this efficiently by keeping track of / updating a "set" for each connected component.

We have 3 functions related to Disjoint Sets:

\begin{enumerate}
  \item \texttt{makeset}(x), which takes in a node x and creates a new set with just x in it.
  \item \texttt{find}(x), which takes in a node and returns which set it belongs to.
  \item \texttt{union}(x, y), which takes in two sets, and combines them into one big set.
\end{enumerate}

\subsection{\Points{2}}
How many times will we call \texttt{makeset} within Kruskal's? Put your answer in terms of $n = |V|$ and $m = |E|$.

\Expecting{A mathematical expression using $n$ and $m$.}

\subsection{\Points{2}}
How many times will we call \texttt{find} within Kruskal's? Put your answer in terms of $n = |V|$ and $m = |E|$.

\Expecting{A mathematical expression using $n$ and $m$.}

\subsection{\Points{2}}
How many times will we call \texttt{union} within Kruskal's? Put your answer in terms of $n = |V|$ and $m = |E|$.

\Expecting{A mathematical expression using $n$ and $m$.}

\subsection{\Points{2}}
Describe using your own words why \texttt{find}(u) != \texttt{find}(v) is the same as "if this edge doesn't cause a cycle".

\Expecting{One sentence explaining the above.}

\pagebreak
\section{Disjoint-Set Data Structure \Points{8}}
Now here's the code for the Disjoint Set functions above:

\begin{verbatim}
algorithm makeset(x)
  Input: a graph node x
  Output: modify x such that it has a "rank" and a "parent"

  x.rank = 0
  x.parent = x

algorithm find(x)
  Input: a graph node x
  Output: the ancestor

  if x != x.parent
    x.parent = find(x.parent)
  return x.parent

algorithm union(x, y)
  Input: graph nodes x and y
  Output: modify x and y so that they are now connected in the same "set"

  x = find(x)
  y = find(y)

  if x == y:
    return

  if x.rank > y.rank
    y.parent = x
  else
    x.parent = y
    if x.rank == y.rank
      y.rank = y.rank + 1
\end{verbatim}

\subsection{\Points{2}}
Suppose we make the following sequence of calls: `\texttt{makeset}(A); \texttt{makeset}(B); \texttt{makeset}(C); \texttt{makeset}(D); \texttt{union}(C, D); \texttt{union}(A, B); \texttt{union}(B, D);`. Draw what the set(s) look like at this point (either a picture or text representation is fine).

\Expecting{Include an image or text representation of what the data structure looks like.}

\subsection{\Points{2}}
You call `\texttt{find}(x)` on the above and it takes multiple recursive calls, then you call `\texttt{find}(x)` again on the same node and it takes fewer. What is `x`? This phenomena is called "path compression".

\Expecting{One of $A,B,C,D$ that corresponds to $X$ above.}

\subsection{\Points{2}}
Draw what the set(s) look like after the additional `\texttt{find}` calls.

\Expecting{Include an image or text representation of what the data structure looks like.}

\subsection{\Points{2}}
What does rank signify for each node?

\Expecting{A short explanation of what the `rank' represents}.

\pagebreak
\section{Network Flow Cuts \Points{14}}

An \textbf{s-t cut} of a flow network is a partitioning of nodes into two groups, one which contains the source $s$ and the other which contains the sink $t$.

\begin{figure}[h!]
\centering
\includegraphics[scale=0.5]{cut-example.png}
\caption{An example of an s-t cut.}
\label{fig:cut_example}
\end{figure}

The image in Figure \ref{fig:cut_example} represents the cut $\{s, v\} / \{u, t\}$. The \textbf{cut capacity} is the sum of all edge capacities that \textbf{go from the set containing s to the set containing t}. So the cut capacity shown above is $7 + 8 = 15$ (note that we do not include $3$).

\subsection{\Points{9}}

Define all 4 s-t cuts in the graph above and calculate their capacities. The \href{https://en.wikipedia.org/wiki/Max-flow_min-cut_theorem}{MaxFlow-MinCut Theorem} states that the max flow in a network is equal to the capacity of the $s-t$ cut with minimum capacity. Using this, what is the max flow of this network?

\Expecting{The 4 s-t cuts along with their capacities and the value of the max flow.}

\subsection{\Points{5}}

Find and list flow values for each edge that will give this max-flow. What do you notice about the flows of the edges along the minimum s-t cut? Use this observation to explain why the max-flow min-cut theorem makes intuitive sense.

\Expecting{Flows for each edge and a short explanation of why the max-flow min-cut theorem makes sense.}

\pagebreak
\section{Negative Prim? \Points{5}}
\label{sec:last}
We want to now consider a similar algorithm to Prim's called Negative-Prim for computing minimum spanning tree in graphs with negative edge weights.

This algorithm adds some number to all of the edge weights to make them all nonnegative, then runs Prim's algorithm on the resulting graph, and argues that the Minimum Spanning Tree in the new graph are the same as the MST in the old graph. You can assume that all the edge weights are unique integers.

\begin{verbatim}
Negative-Prim(G, s):
  minWeight = minimum edge weight in G
  for e in E: # iterate through all edges in G
    modifiedWeight(e) = w(e) - minWeight
    modifiedG = G with weights modifiedWeight
  T = Prim(modifiedG, s) # run Prim's algorithm starting from s
  update T with edges that corresponds to graph G
  return T
\end{verbatim}

\Expecting{Either an informal explanation of why Negative-Prim computes the
correct MST, or a counter-example of an undirected graph with negative edge weights where
Negative-Prim does not output}

\section*{Submitting the Assignment}

The assignment should be submitted through \href{https://www.gradescope.com/courses/350304}{Gradescope}.

The "Homework 10: Fun with MSTs, Flow, and Cuts" assignment is the written portion, for which you should submit a \textbf{typed} response to questions 1-\ref{sec:last}. Each response should clearly be marked with its corresponding number. You are free to use the provided templates, print the questions and write your answers, or to simply type your responses on a blank document (whatever works for you).

\end{document}


\chapter{Conclusions and future work}
\chaptermark{Conclusions}

\section{Conclusions}

In this thesis we have introduced a complete numerical method for the solution of dynamic micromagnetic problems using the finite element method with the Newton-Raphson linearisation, a variety of implicit time integration schemes, hybrid FEM/BEM magnetostatics calculations, and efficient iterative linear solvers.
The methods have been validated against an analytical solution for a problem without magnetostatics and against the \mumag standard problem \#4 with magnetostatics.

In \cref{sec:adaptive-imr} we have demonstrated a novel and widely applicable adaptive time step selection algorithm for the implicit midpoint rule.
We have also shown that the time step selection works well for a wide variety of ODE test cases, as well as for a number of PDE test cases using the LLG equation.
Additionally we have shown that the geometrical integration properties of the constant time step IMR extend to the adaptive time step version.

In \cref{sec:solut-coupl-syst,sec:numer-exper-fem-bem-systems,sec:mumag-stand-probl} we introduced and tested efficient, robust and scalable solution methods for the monolithically coupled LLG-FEM/BEM magnetostatics problem provided that a good preconditioner for the LLG sub-problem is available.
Such monolithic couplings are required to obtain the energy property of the implicit midpoint rule and may offer advantages in stochastic integration methods.

In \cref{sec:imr-ode-llg-numer-exper,sec:numer-exper} we studied the performance of common implicit time integration schemes on some micromagnetic problems with exact solutions.
We found that the overall accuracy of the BDF2 scheme is always poor compared to the TR and IMR schemes, this is probably due to a combination of the larger local truncation error and the spurious numerical damping of BDF2.
% Unfortunately we were unable to analyse the effect of geometric integration on the error accumulation due to the fact that TR also displays some geometrical integration properties for these simple examples.

Finally in \cref{cha:stiffn-llg-equat} we studied the effect of spatial discretisation on the relative performance of implicit and explicit time integration schemes (stiffness).
We found that stiffness in micromagnetics can arise from the spatial discretisation alone, and that the stiffness increases as the element size is decreased, as expected from standard PDE theory.
We also found that the introduction of FEM/BEM magnetostatics calculations increases the stiffness.

\section{Future work}
\label{sec:future-work}

In the course of our numerical experiments we uncovered some issues with the geometrical integration properties of our complete model.
Firstly we found that the conservation properties of IMR with FEM and nodal quadrature was much less effective when applied to meshes of triangular elements, at least in our implementation.
This effect is fairly small and is not contradicted by any numerical results in the literature that we are aware of due to the common use of comparatively loose linearisation tolerances.
An alternative implementation of IMR with FEM and nodal quadrature should be used with a tight linearisation tolerance in order to find out if this effect is an artefact of our implementation or a real issue.

Secondly, we found that when FEM/BEM magnetostatics calculations discretised by a collocation based approach were included the energy conservation property of IMR was lost.
This is probably due to the asymmetry of the discrete BEM operator, which could be corrected by the use of alternative formulations of the method.
In particular the use of the Garc\'{i}a-Cervera-Roma formulation \cite{Garcia-Cervera2006} \cite[19]{Knittel2011} with a Galerkin discretisation approach \cite[75]{Wrobel2002} should resolve this issue.

Once these issues have been corrected the relative performance of schemes with geometric integration properties, in terms of the accumulation of the temporal error, should be evaluated for realistic problems.

In the area of linear solvers a general, efficient, robust and scalable preconditioner for the Newton-Raphson linearised LLG equation is still required.
One approach to the construction of such a preconditioner could be to exploit the block structure of the Jacobian matrix and to use multigrid-based methods to approximate the Laplacian-like skew-symmetric off-diagonal blocks resulting from the exchange effective field.
A less general approach for the case of granular or patterned media could be the use of a domain decomposition preconditioner.
In such a method the small matrix block associated with each grain/island would be inverted by a direct solver and the combination used as a block diagonal preconditioner.
Due to the weak coupling between grains/islands this would provide a good approximation for the inverse of the entire LLG block.
With either of these enhanced LLG preconditioners the effectiveness of the preconditioner discussed in \cref{sec:solution-strategies} on extremely large problems could be investigated.

In the time integration of the stochastic LLG only a few time integration schemes are known to converge to the correct solution, one of which is the implicit midpoint rule \cite{DAquino2006}.
Since the use of a semi-implicit magnetostatics coupling modifies the time integration scheme a monolithic coupling scheme is required to maintain this property.
The preconditioner developed in \cref{sec:solution-strategies} should be tested in this capacity once effective preconditioners for the LLG sub-problem are available.

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


\FPset \Months{36}
\FPset \Years{3}

\FPset \GroupAbaSalery{472.40}
\FPset \GroupAmaSalery{472.40}
\FPset \GroupBbaSalery{505.56}
\FPset \GroupBmaSalery{687.72}

\FPset \wiMASalery{66400} %http://www.dfg.de/formulare/60_12/60_12_de.pdf

\FPmul \itemWiMASalery \Years \wiMASalery
\FPmul \iiktWiMASalery \Years \wiMASalery
\FPadd \totalWiMASalery \itemWiMASalery \iiktWiMASalery
\FPround \itemWiMASalery \itemWiMASalery {0}
\FPround \iiktWiMASalery \iiktWiMASalery {0}
\FPround \totalWiMASalery \totalWiMASalery {0}

\FPmul \GroupAbaSaleryTotal \Months \GroupAbaSalery
\FPmul \GroupAmaSaleryTotal \Months \GroupAmaSalery
\FPmul \GroupBbaSaleryTotal \Months \GroupBbaSalery
\FPmul \GroupBmaSaleryTotal \Months \GroupBmaSalery

\FPadd \HiWiTotalSalery {0} \GroupAbaSaleryTotal
\FPadd \HiWiTotalSalery \HiWiTotalSalery \GroupAmaSaleryTotal
\FPadd \HiWiTotalSalery \HiWiTotalSalery \GroupBmaSaleryTotal

\FPadd \HiWiGroupATotalSalery \GroupAbaSaleryTotal \GroupAmaSaleryTotal
\FPadd \HiWiGroupBTotalSalery \GroupBbaSaleryTotal \GroupBmaSaleryTotal

\FPround \HiWiGroupATotalSalery \HiWiGroupATotalSalery {2}
\FPround \HiWiGroupBTotalSalery \HiWiGroupBTotalSalery {2}

\FPround \HiWiTotalSalery \HiWiTotalSalery {2}

\FPround \GroupAbaSaleryTotal \GroupAbaSaleryTotal {2}
\FPround \GroupAmaSaleryTotal \GroupAmaSaleryTotal {2}
\FPround \GroupBbaSaleryTotal \GroupBbaSaleryTotal {2}
\FPround \GroupBmaSaleryTotal \GroupBmaSaleryTotal {2}

\FPadd \totalSalery \HiWiTotalSalery \totalWiMASalery
\FPround \totalSalery \totalSalery {0}

\section{Requested modules/funds}
We are applying for the "Basic Module" in the research grants programme.

\subsection{Funding for staff}
\subsubsection{Research staff}

\paragraph{Postdoctoral researcher or comparable}

\noindent Not applicable.

\paragraph{Doctoral researcher or comparable}

\noindent We apply for \textbf{a total amount of \euro \num[group-separator={,}]{\totalSalery}}.

For \GroupAProf\ (\GroupAlg--\GroupA) and \GroupBProf\ (\GroupBlg--\GroupB) we
apply for \euro \num[group-separator={,}]{\itemWiMASalery} each for \Months months of funding for one doctoral
researcher. The researchers will work 100\% of the standard work week
on the project and conduct the research. Employing a researcher in
wage group TVL E-13 for one year costs \euro \num[group-separator={,}]{\totalWiMASalery}.

\vspace{6pt}
\noindent\begin{tabular}{lrrrr}
\bfseries Position && \bfseries Amount &\bfseries Factor &\bfseries Subtotal \\\hline
Doctoral researcher at \GroupA & TVL E-13 per year & \euro \num[group-separator={,}]{\wiMASalery} & \Years & \euro \num[group-separator={,}]{\itemWiMASalery}\\
Doctoral researcher at \GroupB & TVL E-13 per year & \euro \num[group-separator={,}]{\wiMASalery} & \Years & \euro \num[group-separator={,}]{\iiktWiMASalery}
\\\cline{5-5}
\bfseries Total & && &  \bfseries \euro \num[group-separator={,}]{\totalWiMASalery}\\
\end{tabular}
\vspace{6pt}

\paragraph{Other research assistant}

\noindent Not applicable.

\subsubsection{Non-academic staff member}

\noindent Not applicable.

\subsubsection{Miscellaneous staff}

\paragraph{Support staff (research support staff and student assistants)}

\noindent We apply for a {\bfseries total amount of \euro \num[group-separator={,}]{\HiWiTotalSalery}} for support staff.

For \GroupAProf~(\GroupA) we apply for funding of two student assistants to support the academic staff with implementation, simulation, and evaluation with the amount of \euro \num[group-separator={,}]{\HiWiGroupATotalSalery}. Each assistant will work 40 hours per month during the duration of the project. The pre-tax wage at \GroupA\ is \euro \num[group-separator={,}]{\GroupAbaSalery} independent of the students degree.

For \GroupBProf~(\GroupB) we also apply for funding of two student assistants with the amount of \euro \num[group-separator={,}]{\HiWiGroupBTotalSalery}. The pre-tax wage at \GroupB\ is \euro \num[group-separator={,}]{\GroupBbaSalery} for bachelor students per month and \euro \num[group-separator={,}]{\GroupBmaSalery} a for master student.

\vspace{6pt}
\noindent\begin{tabular}{lrrr}
\bfseries Position & \bfseries Amount &\bfseries Factor &\bfseries Subtotal \\\hline
Bachelor student at \GroupA, 40 hours & \euro \GroupAbaSalery & \Months & \euro \num[group-separator={,}]{\GroupAbaSaleryTotal}\\
Master student at \GroupA, 40 hours & \euro \GroupAmaSalery & \Months & \euro \num[group-separator={,}]{\GroupAmaSaleryTotal}\\
Bachelor student at \GroupB, 40 hours & \euro \GroupBbaSalery & \Months & \euro\num[group-separator={,}]{\GroupBbaSaleryTotal}\\
Master student at \GroupB, 40 hours & \euro \GroupBmaSalery & \Months & \euro \num[group-separator={,}]{\GroupBmaSaleryTotal}\\
\bfseries Total & & &  \bfseries \euro \num[group-separator={,}]{\HiWiTotalSalery}\\
\end{tabular}
\vspace{6pt}

\paragraph{Other staff}
\noindent Not applicable.

\subsection{Funding for direct project costs}
\subsubsection{Equipment up to \euro10,000, software and consumables}

\noindent We apply for a {\bfseries total amount of \euro 16,500} for equipment.

We apply for the amount of \euro 5,500 for each of the groups
(\GroupAProf and \GroupBProf ) for a "Xilinx
Virtex UltraScale FPGA VCU108 Evaluation Kit".  An state-of-the-art
FPGA board is required for prototyping. After finishing this project the
prototyping boards can also be used for other research activities.

\subsubsection{Travel}

\noindent We apply for a {\bfseries total amount of \euro 20,000} for travel expenses.

We apply for funding of the expenses for conference travels to the amount of \euro 8,000 for \GroupAProf~(\GroupA) for two European and two International conferences; we apply for funding of the expenses for conference travels to the amount of \euro 8,000 for \GroupBProf~(\GroupB) for two European and two International conferences:

\vspace{6pt}
\noindent\begin{tabular}{lrrr}
\bfseries Position & \bfseries Amount &\bfseries Factor &\bfseries Subtotal \\\hline
European conference (\GroupA) & \euro 1,500 & 2 & \euro 3,000\\
International conference (\GroupA) & \euro 2,500 & 2 & \euro 5,000\\
European conference (\GroupB) & \euro 1,500 & 2 & \euro 3,000\\
International conference (\GroupB) & \euro 2,500 & 2 & \euro 5,000\\
\bfseries Total & & &  \bfseries \euro 20,000\\
\end{tabular}
\vspace{6pt}

\noindent In addition, for cooperation within this project we apply
for \euro 4,210 for \GroupAProf\ (\GroupA) , \euro 4,210 for
\GroupBProf~(\GroupB). There are five
joined work packages (WP 5.1, WP 5.2, WP 5.3, WP 7.3, WP 7.4) and five
work packages requiring close cooperation (WP 1.3, WP 2.3, WP 3, WP 4,
WP 7.2). Therefore we ask for the travel expenses for 10 meetings, in
which two persons are visiting the project partner.
We assume a duration of half a work week per meeting (2 nights). Thus,
per person and trip two railway tickets, two nights at a hotel, and
three daily allowances are required, which amounts to:

\vspace{6pt}
\noindent\begin{tabular}{lrrr}
\bfseries Position & \bfseries Amount &\bfseries Factor &\bfseries Subtotal \\\hline
Railway ticket, single trip per person & \euro 65.00 & 40=10*2*2 & \euro 2,600\\
Accommodation per night and person & \euro 80.00 & 40=10*2*2 & \euro 3,200\\
Daily allowance & \euro 24.00 & 60=10*3*2 & \euro 1,440 \\\cline{4-4}
\bfseries Total & & &  \bfseries \euro 8,240\\
\end{tabular}
\vspace{6pt}

\subsubsection{Visiting researchers}

\noindent Not applicable.

\subsubsection{Experimental animals}

\noindent Not applicable.

\subsubsection{Other}

\noindent Not applicable.

\subsubsection{Project-related publication expenses}
\noindent At least four open-access journal publications are expected. This requires additional fees for handling
and open access services of about 1,500 EUR. Our universities employ
an open access strategy already. However, researchers are encouraged
to acquire additional funding for open access publishing. Therefore We
apply for a {\bfseries total amount of \euro 1,500} to pay fees of
journal publications. For \GroupAProf~(\GroupA) we apply for \euro 750 and
for \GroupBProf~(\GroupB) we apply for \euro 750 as well.

\subsection{Funding for instrumentation}

\noindent Not applicable.