% !TEX root = report.tex
\section{Examples Of Alloy Model Construction}

Let us recap the previous section with a couple of comprehensive examples.

\smallskip
\begin{example}
    \phantomsection
    \addcontentsline{toc}{subsection}{First full example}
    \label{ex:major-ex1}
    Let us revisit \autoref{ex:not-follow}. We have the database table  $\rel{Follows}(\field{fan},\field{idol})$ and the following query.
    \[
        Q_\text{\,not\,following\,Alice} =
            \{ x \mid \neg\,\rel{Follows}(x, \str{Alice}) \}
    \]

    We construct the corresponding Alloy program to verify whether the query is safe. The full source code is shown in \autoref{src:major-ex1}. Notice that, at \autoref{li:alice}, we created the singleton constant table called \alloy{Alice}. This represents the scalar value which can be used as a constant in the query (such as on \autoref{li:usealice}).

    Once the source code was executed, the Alloy Analyzer finds multiple counterexamples to the safety assertion of the query. One counterexample instance among multiple counterexamples is shown in \autoref{fig:not-follow-alice}.

    It is easy to see that these results are correct with respect to their own domain sets, which implies that the aforementioned query $Q_\text{\,not\,following\,Alice}$ is \textbf{not safe}.\hfill$\blacksquare$
\end{example}

\begin{figure}[!h]
    % \begin{mdframed}[style=standard,skipabove=0pc,skipbelow=0pc]
        \centering
        \includegraphics[width=0.99\linewidth]{figures/not-follow-alice.eps}
        \smallskip
        \caption[One counterexample of the Alloy program in \protect{\autoref{src:major-ex1}}.]{
            One counterexample of the Alloy program in \protect{\autoref{src:major-ex1}}. For an extensive explanation of this diagram, see the gray box on \hyperref[fig:not-follow-alice-exp]{page \pageref*{fig:not-follow-alice-exp}}.
        }
        \label{fig:not-follow-alice}
    % \end{mdframed}
    \vspace*{-1pc}
\end{figure}

\begin{figure}[!b]
    \begin{mdframed}[style=standard,innerbottommargin=0pc,skipabove=0pc,skipbelow=0pc]
        \ContinuedFloat
        \caption[One counterexample of the Alloy program in \protect{\autoref{src:major-ex1}}.]{
            One counterexample of the Alloy program in \protect{\autoref{src:major-ex1}}, \emph{continued from \hyperref[fig:not-follow-alice]{page \pageref*{fig:not-follow-alice}}}.

            \captionpar
            In this database model, \mbox{\textcolor[HTML]{48466f}{\dotuline{$\blacksquare$ dotted blue}}} \alloyfn{Element} arrows indicate that the first domain, \alloyfn{UniverseAlpha}, contains three scalar values whereas the second domain, \alloyfn{UniverseBeta}, has only two values (and is incidentally the subset of the first). In addition, \alloyfn{Alice} is another name for \alloyfn{Superparticle2}.

            \captionpar
            The instance of the \rel{Follows} (denoted with \mbox{\textcolor[HTML]{bf1363}{\uline{$\blacksquare$ solid pink}}} \alloyfn{Follows} arrows) contains two rows, namely
                $
                    \;\{\hrsp(\str{Superparticle1},\, \underbrace{\str{Superparticle2}}_\str{Alice}),\; (\underbrace{\str{Superparticle2}}_\str{Alice},\, \underbrace{\str{Superparticle2}}_\str{Alice})\hrsp\}
                $
            This table instance is \emph{valid} because both values \str{Superparticle1} and \str{Superparticle2} belong to \emph{both} domains.

            \captionpar
            The result of query under \alloyfn{UniverseAlpha} is a single row data $\{\str{Superparticle0}\}$ as shown by \mbox{\textcolor[HTML]{065143}{\dashuline{$\blacksquare$ dashed green}}} \alloyfn{Output} dashed arrows). On the other hand, \alloyfn{UniverseBeta} yields empty results.
        }
        \label{fig:not-follow-alice-exp}
    \end{mdframed}
    \vspace*{-1pc}
\end{figure}

\newpage
\begin{lstlisting}[language=alloy,basicstyle={\footnotesize\ttfamily},caption={The complete Alloy program which verifies the query {\protect $Q_\text{\,not\,following\,Alice}$} as shown in \autoref{ex:major-ex1}. Alloy codes highlighted in yellow indicate portions of code which are translated according to and depending on the original database schema and the \textsc{drc} query.},label={src:major-ex1},aboveskip=0pc,belowskip=0pc]
/* Scalar values */
sig Superparticle {} {
	Superparticle = Universe.Element
}

/* Domains */
abstract sig Universe { Element: some Superparticle }
one sig UniverseAlpha, UniverseBeta extends Universe {}

/* Common domain */
some sig Particle in Superparticle {} {
	Particle = UniverseAlpha.Element & UniverseBeta.Element
}

/* Database Instance */
one sig Table {
    <|\hll{ex1table}|>Follows: Particle -> Particle<|\hlr{ex1table}|>
}

/* Constant Values */
one sig Constant {
    <|\hll{ex1alice}|>Alice: Particle<|\hlr{ex1alice}\label{li:alice}|>
}

/* Lists all people who are not following Alice */
fun query[u: Universe]: <|\hll{ex1sig}|>set Superparticle<|\hlr{ex1sig}|> {
    { <|\hll{ex1x}|>x<|\hlr{ex1x}|>: u.Element | <|\hll{ex1pred}|>not (x -> Constant.Alice in Table.Follows)<|\hlr{ex1pred}\label{li:usealice}|> }
}

/* Safety assertion */
assert queryIsSafe {
    all u, u': Universe | query[u] = query[u']
}

/* Results placeholder */
abstract sig Result {
    Output: <|\hll{ex1result}|>set Superparticle<|\hlr{ex1result}|>
}
one sig ResultAlpha, ResultBeta extends Result {} {
    ResultAlpha.@Output = query[UniverseAlpha]
    ResultBeta.@Output = query[UniverseBeta]
}

/* Invoke the verification on the assertion */
check queryIsSafe for 4
\end{lstlisting}

\vspace*{3pc}
\begin{lstlisting}[language=alloy,basicstyle={\footnotesize\ttfamily},caption={The complete Alloy programw which verifies the query {\protect $Q_\text{\,follows\,all\,v2}$} as shown in \autoref{ex:major-ex2}. Alloy codes highlighted in yellow indicates portions of code which are translated according and depending on the original database schema and \textsc{drc} query. Also noted that, unlike \autoref{src:major-ex1}, this Alloy program does not require the \alloy{Constant} model.},label={src:major-ex2},aboveskip=0pc,belowskip=0pc]
/* Scalar values */
sig Superparticle {} {
	Superparticle = Universe.Element
}

/* Domains */
abstract sig Universe { Element: some Superparticle }
one sig UniverseAlpha, UniverseBeta extends Universe {}

/* Common domain */
some sig Particle in Superparticle {} {
	Particle = UniverseAlpha.Element & UniverseBeta.Element
}

/* Database Instance */ <|\label{li:ex2-replace-case1}|>
one sig Table {
    <|\hll{ex2table}|>Follows: Particle -> Particle<|\hlr{ex2table}|>
} <|\label{li:ex2-fix}|>

/* Lists all follows who follows every idols */
fun query[u: Universe]: <|\hll{ex2sig}|>set Superparticle<|\hlr{ex2sig}|> {
    { <|\hll{ex2x}|>x<|\hlr{ex2x}|>: u.Element | <|\hll{ex2pred1}|>all y: u.Element | <|\hlr{ex2pred1}\SuppressNumber|>
        <|\hll{ex2pred2}|>(some z: u.Element | z -> y in Table.Follows) <|\hlr{ex2pred2}|>
        <|\hll{ex2pred3}|>implies (x -> y in Table.Follows)<|\hlr{ex2pred3}\ReactivateNumber|> }
}

/* Safety assertion */ <|\label{li:ex2-replace-case2}|>
assert queryIsSafe {
    all u, u': Universe | query[u] = query[u']
}

/* Results placeholder */
abstract sig Result {
    Output: <|\hll{ex2result}|>set Superparticle<|\hlr{ex2result}|>
}
one sig ResultAlpha, ResultBeta extends Result {} {
    ResultAlpha.@Output = query[UniverseAlpha]
    ResultBeta.@Output = query[UniverseBeta]
}

/* Invoke the verification on the assertion */
check queryIsSafe for 4
\end{lstlisting}

\bigskip
Now let us consider another example, particularly \autoref{ex:follow-all}. In the original scenario, the query $Q_\text{\,follows\,all}$ is unsafe because the universal quantification may probe on domain values which lie outside the database. We are going to fix this query in the next example.

\newpage
\begin{example}
    \phantomsection
    \addcontentsline{toc}{subsection}{Second full example}
    \label{ex:major-ex2}
    Consider the database table $\rel{Follows}(\field{fan},\field{idol})$ and the \textsc{drc} query
    \[
        Q_\text{\,follows\,all\,v2} =
            \{ x \mid \forall y[ \exists z [\rel{Follows}(z, y)] \Rightarrow \rel{Follows}(x, y)] \}
    \]
    By additionally injecting ``$\exists z [\rel{Follows}(z, y)] \Rightarrow$'' into the original query, it \emph{hopefully} guarantees that only idols (represented by $y$) having \emph{at least} one follower in the database are considered. This should disregard the effect of dealing with different domain sets.

    We translated the database schema as well as the above query into the Alloy program as shown in \autoref{src:major-ex2}. However, once Alloy Analyzer was executed, we got unexpected results: it produces counterexamples. After a careful consideration the table \rel{Follows} in every counterexample is empty.

    \marginhead{Using Alloy to debug the query}
    Upon the second glance of the query $Q_\text{\,follows\,all\,v2}$, the result returned by Alloy Analyzer actually makes sense. If the table \rel{Follows} is empty, then the query result would be the same set of people in the domain. \emph{This is one prime example of Alloy could help us finding bugs in the design.}

    We need to fix the program to guarantee that we only considered nonempty table \rel{Follows}, which could be stated as an additional fact towards the \alloy{Table} signature declaration. Therefore, after the closing brace at the beginning of \autoref{li:ex2-fix}, we insert the fact (as highlighted) that the table \alloy{Follows} must contain some (i.e., at least one) rows.

    % FIXME USE THIS INSTEAD OF HARDCODING NUMBERS: \ref*{li:ex2-replace-case1}]}

\begin{lstlisting}[language=alloy,firstnumber=15]
/* Database Instance */
one sig Table {
    Follows: Particle -> Particle
} <|\hll{ex2fact}|>{ some Follows }<|\hlr{ex2fact}|>
\end{lstlisting}

    Once the the modified Alloy program is executed again, it no longer finds a counterexample. Even if we increase the upper limit of the number of each type of model objects from \alloy{4} to \alloy{8}, or even to \alloy{12}, Alloy still does not find a counterexample. Therefore, we may say that ``the \textsc{drc} query $Q_\text{\,follows\,all\,v2}$ \emph{could be} safe, at least as long as the table \rel{Follows} is \emph{nonempty}.''\; This is only a speculation since we could only hope that if Alloy were to find a counterexample, it should have found a small one already.

    \subsubsection*{Alternative Approach}

    Instead of checking for database instances in which the table \rel{Follows} is not empty, we could modify the query to make sure that the \field{fan} must reside in the database. Hence, let us say that the new \textsc{drc} query is
    \[
        Q_\text{\,follows\,all\,v3} =
            \{ x \mid \exists w [\rel{Follows}(x, w)] \wedge \forall y[ \exists z [\rel{Follows}(z, y)] \Rightarrow \rel{Follows}(x, y)] \}
    \]
    The first clause of the conjuction demands that the \field{fan} $x$ must follow at least one person. In other words, that \field{fan} must at least exists in the database.

    The Alloy function implementation of this query can be written as
\begin{lstlisting}[language=alloy,firstnumber=20]
/* Lists all follows who follows every idols */
fun query[u: Universe]: set Superparticle {
    { x : u.Element | <|\SuppressNumber|>
        <|\hll{ex2xtra}|>(some w: u.Element | x -> w in Table.Follows) and <|\hlr{ex2xtra}|>
        (all y: u.Element |
            (some z: u.Element | z -> y in Table.Follows)
            implies (x -> y in Table.Follows))<|\ReactivateNumber|> }
}
\end{lstlisting}
    The only difference between this new code and the original one is shown as highlighted. Everything else remains the same apart from reformatting for clarity.

    When we run this new code, the Alloy Analyzer also does not find any counterexamples, and thus the new query $Q_\text{\,follows\,all\,v3}$ may be safe.
\end{example}

Now that we have established how any simple database schemata and \textsc{drc} queries can be translated into a verification task in Alloy language, in the next section we will experiment with more examples of safe and unsafe queries and see how accurate Alloy performs the verification tasks.


\chapter{Appendix: Bonus triangles under $\equiv_{p}$}
\label{ch:appendix:bonus:triangles}

In this appendix we collect a set of modular representations as coloured
triangles for some Riordan arrays. They are here to remove clutter from
previous chapters, where they are not really necessary to ease the discussion;
however some of them are pretty interesting for the patterns they show.
Although we have not been able to characterize those patterns formally, it is
pleasant to have a collection of them, to be used as a starting point for
further studies.

We prefer to report an \emph{aggregate}\marginpar{aggregate representation
$\mathcal{M}_{\stackrel{\circ}\equiv_{p}}$} representation of the modular
transformation in order to make patterns appear clearly. Formally, given a
Riordan array $\mathcal{M}$ and a congruence relation $\equiv_{p}$, where $p$
is a prime number, we report the representation of
$\mathcal{M}_{\stackrel{\circ}\equiv_{p}}$, which uses only \emph{two} colours,
namely $b$ (for \textcolor{blue}{blue}) and $o$ (for
\textcolor{orange}{orange}).

Let $\lbrace m_{nk}\rbrace_{n,k\in\mathbb{N}}$ be the matrix of coefficients of
array $\mathcal{M}$, then coefficient at row $r$ and column $k$ of
$\mathcal{M}_{\stackrel{\circ}\equiv_{p}}$ satisfies the following relation:
\begin{displaymath}
    \left(\mathcal{M}_{\stackrel{\circ}\equiv_{p}}\right)_{nk} = b
    \leftrightarrow m_{nk}\equiv_{p}  0
\end{displaymath}
therefore all coefficients belonging to remainder classes:
\begin{displaymath}
    \left\lbrace[1]_{p},[2]_{p},\ldots,[p-1]_{p}\right\rbrace
\end{displaymath}
get associated with colour $c$, while all those coefficients which are
multiples of $p$, therefore belonging to class $[0]_{p}$, get associated with
colour $b$.

Every congruence relation $\equiv_{p}$ used in the following representations,
uses a \emph{prime} module $p$ since it seems that interesting patterns appear
only under this constraint. We tried to represent an array under $\equiv_{p}$
where $p\in\lbrace4,6,8\rbrace$, but the resulting representation was difficult
to study and not so much appealing as the ones produced where $p$ is a prime
number. In \autoref{subsection:pascal:congruence:characterization:p:not:prime}
is discussed the case about Pascal array $\mathcal{P}$ and in
\autoref{fig:pascal-standard-ignore-negatives-centered-colouring-127-rows-multiples-of-4-partitioning-triangle}
$\mathcal{P}_{\stackrel{\circ}{\equiv_{4}}}$ is shown.

In the following table we recap formal definitions of represented Riordan
arrays:
\begin{displaymath}
    \hspace{-4cm}
    \begin{split}
        \mathcal{P}\left(\frac{1}{1-t},\frac{t}{1-t}\right) &\quad
            \mathcal{P}^{-1}\left(\frac{t}{1+t}, \frac{t}{1+t}\right) \\
        \mathcal{C}\left(\frac{1-\sqrt{1-4\,t}}{2t},\frac{1-\sqrt{1-4\,t}}{2}\right) &\quad
            \mathcal{C}^{-1}\left(t,t-t^{2}\right) \\
        \mathcal{M}\left(\frac{1-t - \sqrt{1- 2 \, t -3 \, t^{2} }}{2 \, t^{2}},
                \frac{1-t - \sqrt{1 - 2 \, t-3 \, t^{2}}}{2 \, t}\right) &\quad
            \mathcal{M}^{-1}\left(\frac{2\,t^{2}}{t^{4}+t^{3}-t^{2}-t-2}, \frac{t}{1+t+t^{2}}\right) \\
        \mathcal{D}\left(\frac{1}{1-t},\frac{t(1+t)}{1-t}\right) &\quad
            \mathcal{D}^{-1}\left(\frac{3+t-\sqrt{t^{2} + 6 \, t + 1}}{2},
                \frac{\sqrt{t^{2} + 6 \, t + 1}-t-1}{2}\right) \\
        \mathcal{F}\left(\frac{1}{1-t-t^{2}}, \frac{1-\sqrt{1-4 \, t}}{2}\right) &\quad
            \mathcal{F}^{-1}\left(1-t + 2 \, t^{3} -t^{4}, t - t^{2}\right) \\
    \end{split}
\end{displaymath}

% Pascal triangles
\input{Chapters/appendix-bonus-triangles/pascal-standard-ignore-negatives-centered-colouring-127-rows-multiples-of-3-partitioning-include-figure}
\input{Chapters/appendix-bonus-triangles/pascal-standard-ignore-negatives-centered-colouring-127-rows-multiples-of-5-partitioning-include-figure}
\input{Chapters/appendix-bonus-triangles/pascal-standard-ignore-negatives-centered-colouring-127-rows-multiples-of-7-partitioning-include-figure}

% Catalan triangles
\input{Chapters/appendix-bonus-triangles/catalan-traditional-standard-handle-negatives-centered-colouring-127-rows-multiples-of-3-partitioning-include-figure}
\input{Chapters/appendix-bonus-triangles/catalan-traditional-inverse-handle-negatives-centered-colouring-127-rows-multiples-of-3-partitioning-include-figure}

\input{Chapters/appendix-bonus-triangles/catalan-traditional-standard-handle-negatives-centered-colouring-127-rows-multiples-of-5-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/catalan-traditional-inverse-handle-negatives-centered-colouring-127-rows-multiples-of-5-partitioning-include-figure}

%\input{Chapters/appendix-bonus-triangles/catalan-traditional-standard-handle-negatives-centered-colouring-127-rows-multiples-of-7-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/catalan-traditional-inverse-handle-negatives-centered-colouring-127-rows-multiples-of-7-partitioning-include-figure}

%\input{Chapters/appendix-bonus-triangles/catalan-traditional-standard-handle-negatives-centered-colouring-127-rows-multiples-of-11-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/catalan-traditional-inverse-handle-negatives-centered-colouring-127-rows-multiples-of-11-partitioning-include-figure}

% Motzkin triangles
\input{Chapters/appendix-bonus-triangles/motzkin-standard-ignore-negatives-centered-colouring-127-rows-mod2-partitioning-include-figure}
\input{Chapters/appendix-bonus-triangles/motzkin-standard-ignore-negatives-centered-colouring-127-rows-multiples-of-3-partitioning-include-figure}
\input{Chapters/appendix-bonus-triangles/motzkin-standard-ignore-negatives-centered-colouring-127-rows-multiples-of-5-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/motzkin-standard-ignore-negatives-centered-colouring-127-rows-multiples-of-7-partitioning-include-figure}

% Delannoy triangles
\input{Chapters/appendix-bonus-triangles/delannoy-standard-handle-negatives-centered-colouring-127-rows-multiples-of-3-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/delannoy-inverse-handle-negatives-centered-colouring-127-rows-multiples-of-3-partitioning-include-figure}
\input{Chapters/appendix-bonus-triangles/delannoy-standard-handle-negatives-centered-colouring-127-rows-multiples-of-5-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/delannoy-inverse-handle-negatives-centered-colouring-127-rows-multiples-of-5-partitioning-include-figure}
\input{Chapters/appendix-bonus-triangles/delannoy-standard-handle-negatives-centered-colouring-127-rows-multiples-of-7-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/delannoy-inverse-handle-negatives-centered-colouring-127-rows-multiples-of-7-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/delannoy-standard-handle-negatives-centered-colouring-127-rows-multiples-of-11-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/delannoy-inverse-handle-negatives-centered-colouring-127-rows-multiples-of-11-partitioning-include-figure}

% Fibonacci triangles
\input{Chapters/appendix-bonus-triangles/fibonacci-standard-handle-negatives-centered-colouring-127-rows-mod2-partitioning-include-figure}
\input{Chapters/appendix-bonus-triangles/fibonacci-inverse-handle-negatives-centered-colouring-127-rows-mod2-partitioning-include-figure}
\input{Chapters/appendix-bonus-triangles/fibonacci-standard-handle-negatives-centered-colouring-127-rows-multiples-of-3-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/fibonacci-inverse-handle-negatives-centered-colouring-127-rows-multiples-of-3-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/fibonacci-standard-handle-negatives-centered-colouring-127-rows-multiples-of-5-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/fibonacci-inverse-handle-negatives-centered-colouring-127-rows-multiples-of-5-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/fibonacci-standard-handle-negatives-centered-colouring-127-rows-multiples-of-7-partitioning-include-figure}
%\input{Chapters/appendix-bonus-triangles/fibonacci-inverse-handle-negatives-centered-colouring-127-rows-multiples-of-7-partitioning-include-figure}


\section{Vectors and Matrices}
\input{../common/vectorsMatrices/vectors.tex}
\input{../common/vectorsMatrices/matrices.tex}


\subsection{Simulation Efficiency}
\paragraph{}
It is also worth noting that the method that I am using to simulate the orbits is generally known as being a brute force method, where every body in the simulation has all of its forces simulated based on every other body in the simulation, while this enables the most accurate method of simulation, it is not particularly efficient, and only really works well for small numbers of bodies, meaning that it would suit the purposes of this program.

\paragraph{}
There are some forces that will be calculated to be so insignificant that it will not majorly affect the outcome of the simulation. For example, the orbit of two bodies will not be massively affected by a third body that is extremely far away.

\paragraph{}
This principle is applied in the Barnes-Hut Algorithm, in which space is divided up into a quadtree. (octree for 3D simulation.) In this method, only particles/bodies that are close to the particle being simulated will be individually simulated. Particles that are further away will be simulated as one large particle based on its center of mass. This mainly increases the effectiveness if there are a large number of particles in a particular area, as this greatly reduces the number of interactions that need to be calculated.

\paragraph{}
While this method would have some benefits, the method is mainly designed for simulations with thousands of bodies or particles, something that is outside the scope for this project, it also makes use of a relatively complex algorithm that could be difficult to replicate in the time that I have available.

\paragraph{}
This may be something that is worth investigating at a later date should there be time, particularly if there is a need to simulate multiple thousands of bodies.

\paragraph{}
It may be possible to save some computational time by computing the values force forces inside a matrix. This would describe every particle to particle permutation that is possible and thus the forces between them in a undirected, weighted graph. While it won't really be used as a graph, it is a valid abstraction for the computation, removing duplication of a calculation that would decrease calculation performance.

\subsection{Simulation Prototyping}
\begin{wrapfigure}{r}{0.5\textwidth}
  \centering
  \includegraphics[width=0.45\textwidth]{img/notworking0.png}
  \caption{Inverted Negative Quadrant}
  \vspace{-10pt}
\end{wrapfigure}

\paragraph{}
The main thing that I wanted to check in the design of the project is that the mechanics that I plan to use will work correctly and as intended, due to the sheer volume of data involved, the easiest way to deal with this is to write a small program that runs through a simple scenario.

\paragraph{}
I started by writing a program that ran through a two body problem, using the intended algorithm and a fixed one second time step, I adjusted the gravitational constant to a more manageable number (1) that would work for small values of mass and velocity.

\paragraph{}
After the program was running, I set up a quick terminal output of the position of the body, these values were then pasted directly into a spreadsheet application and displayed on a graph, much to my dismay, it did not quite work correctly, the expected result of this should be a near perfect circle that loops back on itself, the system is theoretically closed and should remain constant.

\paragraph{}
However this output clearly shows that the object completes half an orbit, and then suddenly decides to veer off in completely the wrong direction, this results in the distance to the body becoming greater and the force diminishing.

\paragraph{}
When looking at the other values, particularly the angle of the orbiting body to the barycentre (0, 0, fixed mass), the angle flips its sign, from ~-1.5 Radians to ~1.5 Radians, this causes the forces to also flip their sign and the forces then act in the opposite direction, causing acceleration away from the central body into infinity.

\paragraph{}
The fix for this was to check when the orbit crosses the X axis and flip the sign of the resultant forces when it does so that the system remains in orbit, this works in preventing this problem, however it manages to highlight a new issue.

\begin{wrapfigure}{!hl}{0.5\textwidth}
  \centering
  \includegraphics[width=0.45\textwidth]{img/notworking1amp.png}
  \caption{Extreme KE Increase}
  \vspace{-10pt}
\end{wrapfigure}

\paragraph{}
As the body orbits, its height increases dramatically, and mainly on the negative X side of the graph, it is pretty clear that energy is not being conserved in this closed system and the orbiting body is gaining energy allowing it to reach a higher and higher orbit.

\paragraph{}
Upon further reading, it appears that this is simply a flaw of the system that I have elected to use, known generally as Euler's Method, while this method is relatively simple to implement, it comes with significant disadvantages, the main one being is that a much smaller time base is required when bodies become extremely close together, meaning that a fixed time base will not be an option. The method does not maintain conservation of energy and orbits will become higher over time due to error accumulated in the equations, the sheer amount of calculations that need to be done only worsen this effect, this is more obvious when looking at a highly eccentric orbit. while I understood that this would be the case, I did not consider that it would be quite this severe.

\begin{wrapfigure}{r}{0.45\textwidth}
  \centering
  \includegraphics[width=0.4\textwidth]{img/orbitbad.png}
  \caption{Integration Deviation}
  \vspace{-10pt}
\end{wrapfigure}

\paragraph{}
At a mathematical level, the algorithm is performing a method of numerical integration through the equation $s=ut+\frac{1}{2}at^2$ which in essence will find the area under a velocity / time graph which equates to the distance travelled. This method performs this equation at every single time step, during the time the acceleration is constant and it is performed over the provided time step. This results in an issue when the orbiting body is close to the central body, as the distance travelled over the fixed time step becomes too large to correctly map to the intended orbit path, which causes the accumulation of error and instability over time. This is widely known as Euler Integration, Acceleration, Velocity and Position ($a \rightarrow v \rightarrow r$) are all updated sequentially in each time step.

\subsubsection{Leapfrog Intergration}
\paragraph{}
After some research into this problem, it was clear that this is not a problem that can be solved while still retaining the simple sequential $a \rightarrow v \rightarrow r$ calculation flow and would require some investigation into alternate methods. One of the numerical integration methods that is commonly used in gravitational particle simulations is Leapfrog integration, this method involves updating the velocity half time step out of phase with the acceleration and position, hence the name leapfrog.

\begin{figure}[!ht]
  \centering
  \includegraphics[width=0.6\textwidth]{img/leapfrog.png}
  \caption{Leapfrog Flow}
\end{figure}

\paragraph{}
Because it is somewhat difficult to step for half an iteration as integer loop counters are generally preferred for performance reasons, there are versions of these equations that are synchronised to update simultaneously. This effectively then becomes a calculation flow of $\frac{1}{2}v \rightarrow r \rightarrow a \rightarrow \frac{1}{2}v$.

\begin{figure}[!ht]
  $$x_{i+1}=x_i+v_i\Delta{t}+\frac{1}{2}a_i\Delta{t}^2$$
  $$v_{i+1}=v_i+\frac{1}{2}(a_i+a_{i+1})\Delta{t}$$
  \caption{Time-step Synchronised Leapfrog Formula}
\end{figure}

\subsubsection{Vector Equations}
\paragraph{}
These equations will apply to the the components of the vectors, the acceleration of the body is calculated using the universal gravitation calculation and the resulting force can be divided by the mass of the body in order to get the acceleration. This can then be broken into x and y components and calculations can be performed on individual axis. The issue with this is that this requires the use of trigonometric functions both to find angles and to work out the ratios for components, these can come a quite high computational cost, while in low volumes this should not be an issue, with a larger number of bodies it could cause significant slow down.

\paragraph{}
Another issue that these functions can cause is when it comes to moving through a coordinate system, when an object goes negative on the x axis the sign on the angle becomes negative which results in the forces flipping, while this can be corrected it is somewhat messy and could cause issues further down the line.

\paragraph{}
A better way to compute the components is to directly calculate the forces as components by using a vector equation on both axis, this means that the trigonometric functions will not be necessary for the main simulation loop of the program, greatly improving the performance of said simulation.\\\

In this form the equation for force becomes:

$$F_{xy}=\frac{Gm_1m_2}{r^3} \cdot \hat{r_{xy}}$$

The proof for this formula can be found in the document appendix.

\paragraph{}
The benefit of using this equation over the trigonometric equation is found in small benefits to performance, when it comes to the large amount of calculations that are required per second, but also when it comes to the complexity of the code, resulting in a program that is far easier to maintain and understand in the long run.

\paragraph{}
There will still be some cases where the trigonometric functions are required, for example it may be necessary when setting up bodies and the direction of their initial velocity, as it would be more understandable to the user to define starting conditions in terms of angle and magnitude.


\chapter{Examples}\label{ch:examples}

\section{Mesh Generation Input File}

The first example shown in this section is the point definition portion of an input
file for a mesh generation code. First, the locations of the arc center points
1, 2, and 5 are specified. Then, the radius of each arc is defined ( \cmd{\{Rad1\}},
\cmd{\{Rad2\}}, and \cmd{\{Rad5\}}). Note that the lines are started with a dollar sign, which
is a comment character to the mesh generation code. Following this, the locations
of points 10, 20, 30, 40, and 50 are defined in algebraic terms. Then, the points
for the inner wall are defined simply by subtracting the wall thickness from the
radius values.
\begin{apinp}
Title
Example for Aprepro
\$  Center  Points
Point  1  \{x1  =  6.31952E+01\}  \{y1  =  7.57774E+01\}
Point  2  \{x2  =  0.00000E+00\}  \{y2  =  -3.55000E+01\}
Point  5  \{x5  =  0.00000E+00\}  \{y5  =  3.62966E+01\}
\$  Wth  =  \{Wth  =  3.0\}
\ldots Wall thickness
\$  Rad5  =  \{Rad5  =  207.00\}
\$  Rad2  =  \{Rad2  =  203.2236\}
\$  Rad1  =  \{Rad1  =  Rad2  -  dist(x1,y1;  x2,y2)\}
\$  Angle  between  Points  2  and  1:  \{Th12  =  atan2d((y1-y2),(x1-x2))\}
Point  10  0.00    \{y5  -  Rad5\}
Point  20  \{x20  =  x1+Rad1\}    \{y5-sqrt(Rad5\^{}2-x20\^{}2)\}
Point  30  \{x20\}    \{y1\}
Point  40  \{x1+Rad1*cosd(Th12)\}  \{y1+Rad1*sind(Th12)\}
Point  50  0.00    \{y2  +  Rad2\}
\$  Inner Wall (3  mm  thick)
\$  \{Rad5  -=  Wth\}
\$  \{Rad2  -=  Wth\}
\$  \{Rad1  -=  Wth\}
\ldots Rad1, Rad2, and Rad5 are reduced by the wall thickness
Point  110  0.00    \{y5  -  Rad5\}
Point  120  \{x20  =  x1+Rad1\}    \{y5-sqrt(Rad5\^{}2-x20\^{}2)\}
Point  130  \{x20\}    \{y1\}
Point  140  \{x1+Rad1*cosd(Th12)\}  \{y1+Rad1*sind(Th12)\}
Point  150  0.00    \{y2  +  Rad2\}
\end{apinp}
The output obtained from processing the above input file by \aprepro{} is shown below.
\begin{apout}
Title
Example for  Aprepro
\$  Center  Points
Point  1  63.1952  75.7774
Point  2  0  -35.5
Point  5  0  36.2966
\$  Rad5  =  207
\$  Rad2  =  203.2236
\$  Rad1  =  75.2537088
\$  Angle between  Points  2  and  1:  60.40745947
Point  10  0.00  -170.7034
Point  20  138.4489088  -117.5893956
Point  30  138.4489088  75.7774
Point  40  100.3576382  141.214957
Point  50  0.00  167.7236
\$  Inner Wall  (3  mm  thick)
\$  204
\$  200.2236
\$  72.2537088
Point  110  0.00  -167.7034
Point  120  135.4489088  -116.2471416
Point  130  135.4489088  75.7774
Point  140  98.87615226  138.6062794
Point  150  0.00  164.7236
\end{apout}

\section{Macro Examples}

Aprepro can also be used as a simple macro definition program. For example, a mesh
input file may have many lines with the same number of intervals. If those lines
are defined using a variable name for the number of intervals, then preprocessing
the file with Aprepro will set all of the intervals to the same value, and simply
changing one value will change them all. The following input file fragment illustrates
this
\begin{apinp}
\$ \{intA = 11\} \{intB = int(intA / 2)\} line 10 str 10 20 0 \{intA\}

line 20 str 20 30 0 \{intB\}
line 30 str 30 40 0 \{intA\}
line 40 str 40 10 0 \{intB\}
\end{apinp}
Which when processed looks like:
\begin{apout}
\$  11  5

line 10 str 10 20 0 11
line 20 str 20 30 0 5
line 30 str 30 40 0 11
line 40 str 40 10 0 5
\end{apout}

\section{Command Line Variable Assignment}\label{example:varval}

This example illustrates the use of assigning variables on the command line. While
generating a complicated 2D or 3D mesh, it is often necessary to reposition the
mesh using GREPOS. If the following file called shift.grp is created:
\begin{apinp}
Offset  X  \{xshift\}  Y  \{yshift\}
Exit
\end{apinp}
then, the mesh can be repositioned simply by typing:
\begin{apinp}
Aprepro  xshift=100.0  yshift=-200.0  shift.grp  temp.grp
Grepos  input.mesh  output.mesh  temp.grp
\end{apinp}

\section{Loop Example}

This example illustrates the use of the loop construct to print a table of sines
and cosines from 0 to 90 degrees in 5 degree increments.

Input:
\begin{apinp}
\$  Test  looping  -  print  sin,  cos  from  0  to  90  by  5
\{angle  =  -5\}
\{Loop(19)\}
\{angle  +=  5\}  \{sind(angle)\}  \{cosd(angle)\}
\{EndLoop\}
\end{apinp}
Output:
\begin{apout}
\$  Test  looping  -  print  sin,  cos  from  0  to 90  by  5
-5
 0  0    1
 5  0.08715574275 0.9961946981
10  0.1736481777  0.984807753
15  0.2588190451  0.9659258263
20  0.3420201433  0.9396926208
25  0.4226182617  0.906307787
30  0.5    0.8660254038
35  0.5735764364  0.8191520443
40  0.6427876097  0.7660444431
45  0.7071067812  0.7071067812
50  0.7660444431  0.6427876097
55  0.8191520443  0.5735764364
60  0.8660254038  0.5
65  0.906307787  0.4226182617
70  0.9396926208  0.3420201433
75  0.9659258263  0.2588190451
80  0.984807753  0.1736481777
85  0.9961946981  0.08715574275
90  1    6.123233765e-17
\end{apout}

\section{If Example}
This example illustrates the if conditional construct.

\begin{apinp}
\{diff = sqrt(3)*sqrt(3) - 3\}
$ Test if - else lines
\{if(sqrt(3)*sqrt(3) - 3 == diff)\}
 complex if works
\{else\}
 complex if does not work
\{endif\}

\{if (sqrt(4) == 2)\}
 \{if (sqrt(9) == 3)\}
  \{if (sqrt(16) == 4)\}
    square roots work
  \{else\}
    sqrt(16) does not work
  \{endif\}
 \{else\}
   sqrt(9) does not work
 \{endif\}
\{else\}
  sqrt(4) does not work
\{endif\}

\{v1 = 1\} \{v2 = 2\}
\{if (v1 == v2)\}
  Bad if
  \{if (v1 != v2)\}
   should not see (1)
  \{else\}
   should not see (2)
  \{endif\}
   should not see (3)
\{else\}
  \{if (v1 != v2)\}
   good nested if
  \{else\}
   bad nested if
  \{endif\}
  good
  make sure it is still good
\{endif\}
\end{apinp}

The output of this is:
\begin{apout}
-4.440892099e-16
\$ Test if - else lines
 complex if works

       square roots work

1 2
     good nested if
  good
  make sure it is still good
\end{apout}

\section{Aprepro Exodus Example}

The input below illustrates the use of the \cmd{exodus\_meta} and \cmd{exodus\_info} functions.
\begin{apinp}
\{exodus_meta("exodus.g")\}

        Title = \{ex_title\}
    Dimension = \{ex_dimension\}
   Node Count = \{ex_node_count\}
Element Count = \{ex_element_count\}

Element Block Info:
        Count = \{ex_block_count\}
        Names = \{ex_block_names\}
     Topology = \{ex_block_topology\}
          Ids = \{print_array(transpose(ex_block_ids))\}
\{print_array(ex_block_info)\}

Nodeset Info:
        Count = \{ex_nodeset_count\}
        Names = \{ex_nodeset_names\}
          Ids = \{print_array(transpose(ex_nodeset_ids))\}
\{print_array(ex_nodeset_info)\}

Sideset Info:
        Count = \{ex_sideset_count\}
        Names = \{ex_sideset_names\}
          Ids = \{print_array(transpose(ex_sideset_ids))\}
\{print_array(ex_sideset_info)\}

Timestep Info:
        Count = \{ex_timestep_count\}
        Times = \{print_array(transpose(ex_timestep_times))\}

NOTE: Array index are 0-based by default; get_word is 1-based... \{i_=0\}
\{loop(ex_block_count)\}
Element block \{ex_block_ids[i_]\} named '\{get_word(++i_,ex_block_names,",")\}' has topology '\{get_word(i_,ex_block_topology,",")\}'
\{endloop\}

Extract Information Records using begin \ldots end
\{info1 = exodus_info("exodus.g", "start extract", "end extract")\}
Rescan String:
\{rescan(info1)\}

Extract Information Records using prefix and then rescan:
\{info2 = exodus_info("exodus.g", "PRE: ")\}
\{rescan(info2)\}
\end{apinp}

When processed by \aprepro{}, the following output will be produced:
\begin{apout}
        Title = GeneratedMesh: 2x3x4+shell:xYz+nodeset:XyZ+sideset:xyzXYZ+times:7+variables:glob
    Dimension = 3
   Node Count = 60
Element Count = 50

Element Block Info:
        Count = 4
        Names = inner_core,Shell-MinX,Shell-MaxY,Shell-MinZ
     Topology = hex8,shell4,shell4,shell4
          Ids = 	1	2	3	4
	1	24	8	0
	2	12	4	1
	3	8	4	1
	4	6	4	1

Nodeset Info:
        Count = 3
        Names = nodelist_1,nodelist_2,nodelist_3
          Ids = 	1	2	3
	1	20	20
	2	15	15
	3	12	12

Sideset Info:
        Count = 6
        Names = surface_1,surface_2,surface_3,surface_4,surface_5,surface_6
          Ids = 	1	2	3	4	5	6
	1	12	48
	2	8	32
	3	6	24
	4	12	48
	5	8	32
	6	6	24

Timestep Info:
        Count = 7
        Times = 	0	1	2	3	4	5	6

NOTE: Array index are 0-based by default; get_word is 1-based... 0
Element block 1 named 'inner_core' has topology 'hex8'
Element block 2 named 'Shell-MinX' has topology 'shell4'
Element block 3 named 'Shell-MaxY' has topology 'shell4'
Element block 4 named 'Shell-MinZ' has topology 'shell4'

Extract Information Records using begin \ldots end
{loop(6)}
{a_++^2}
{endloop}

Rescan String:
0
1
4
9
16
25

Extract Information Records using prefix and then rescan:
{ECHO(OFF)}
{Units("si")}
{ECHO(ON)}
1 foot = {1~foot} {lout}
12 inch = {12~inch} {lout}
1728 in^3 = {1728~in^3} {Vout}
{10~foot * 14~in}
60 mph = {60~mph} {vout}
88 fps = {88~fps} {vout}

1 foot = 0.3048 meter
12 inch = 0.3048 meter
1728 in^3 = 0.02831684659 meter^3
1.0838688
60 mph = 26.8224 meter/sec
88 fps = 26.8224 meter/sec
\end{apout}

\section{Aprepro Test File Example}
The input below is from one of the aprepro test files.  It illustrates
looping, assignments, trigonometric functions, ifdefs, string
processing, and many other \aprepro{} constructs.
\begin{apinp}
$ Test program for Aprepro
$
Test number representations
\{1\}	\{10e-1\}	\{10.e-1\}	\{.1e+1\}	\{.1e1\}
\{1\}	\{10E-1\}	\{10.E-1\}	\{.1E+1\}	\{.1E1\}

Test assign statements:
\{_a = 5\}	\{b=_a\}	$ Should print 5 5
\{_a +=b\}	\{_a\} 	$ Should print 10 10
\{_a -=b\}	\{_a\}	$ Should print 5 5
\{_a *=b\}	\{_a\}	$ Should print 25 25
\{_a /=b\}	\{_a\}	$ Should print 5 5
\{_a ^=b\}	\{_a\}	$ Should print 3125 3125
\{_a = b\}	\{_a**=b\}	\{_a\}	$ Should print 5 3125 3125

Test trigonometric functions (radians)
\{pi = d2r(180)\} \{atan2(0,-1)\} \{4*atan(1.0)\} $ Three values of pi
\{_a = sin(pi/4)\}	\{pi-4*asin(_a)\}	$ sin(pi/4)
\{_b = cos(pi/4)\}	\{pi-4*acos(_b)\}	$ cos(pi/4)
\{_c = tan(pi/4)\}	\{pi-4*atan(_c)\}	$ tan(pi/4)

Test trigonometric functions (degrees)
\{r2d(pi)\}	\{pid = atan2d(0,-1)\}	\{4 * atand(1.0)\}
\{ad = sind(180/4)\}	\{180-4*asind(ad)\}	$ sin(180/4)
\{bd = cosd(180/4)\}	\{180-4*acosd(bd)\}	$ cos(180/4)
\{cd = tand(180/4)\}	\{180-4*atand(cd)\}	$ tan(180/4)

Test max, min, sign, dim, abs
\{pmin = min(0.5, 1.0)\}	\{nmin = min(-0.5, -1.0)\} $ Should be 0.5, -1
\{pmax = max(0.5, 1.0)\}	\{nmax = max(-0.5, -1.0)\} $ Should be 1.0, -0.5
\{zero = 0\} \{sign(0.5, zero) + sign(0.5, -zero)\}	$ Should be 0 1
\{nonzero = 1\} \{sign(0.5, nonzero) + sign(0.5, -nonzero)\} $ Should be 1 0
\{dim(5.5, 4.5)\}	\{dim(4.5, 5.5)\}	$ Should be 1 0

$ Test ifdef lines
\{ifyes = 1\} \{ifno = 0\}
	\{Ifdef(ifyes)\}
This line should be echoed. (a)
 \{Endif\}
This line should be echoed. (b)
     \{Ifdef(ifno)\}
This line should not be echoed
 	 \{Endif\}
This line should be echoed. (c)
  \{Ifdef(ifundefined)\}
This line should not be echoed
        \{Endif\}
This line should be echoed. (d)

$ Test if - else lines
             \{Ifdef(ifyes)\}
This line should be echoed. (1)
			\{Else\}
This line should not be echoed (2)
	\{Endif\}
		\{Ifdef(ifno)\}
This line should not be echoed. (3)
 \{Else\}
This line should be echoed (4)
   \{Endif\}

$ Test if - else lines
 \{Ifndef(ifyes)\}
This line should not be echoed. (5)
  \{Else\}
This line should be echoed (6)
   \{Endif\}
    \{Ifndef(ifno)\}
This line should be echoed. (7)
 \{Else\}
This line should not be echoed (8)
  \{Endif\}
$ Lines a, b, c, d, 1, 4, 6, 7 should be echoed
$ Check line counting -- should be on line 74: \{Parse Error\}
\{ifdef(ifyes)\} \{This should be an error\}
\{endif\}

$ Test int and [] (shortcut for int)
\{int(5.01)\}	\{int(-5.01)\}
\{[5.01]\}	\{[-5.01]\}

$ Test looping - print sin, cos from 0 to 90 by 5
\{_angle = -5\}
\{Loop(19)\}
\{_angle += 5\}	\{_sa=sind(_angle)\}	\{_ca=cosd(_angle)\} \{hypot(_sa, _ca)\}
\{EndLoop\}

$$$$ Test formatting and string concatenation
\{_i = 0\} \{_SAVE = _FORMAT\}
\{loop(20)\}
\{IO(++_i)\} Using the format \{_FORMAT = "%." // tostring(_i) // "g"\},	PI = \{PI\}
\{endloop\}
Reset format to default: \{_FORMAT = _SAVE\}

$$$$ Test string rescanning and executing
\{ECHO(OFF)\}
\{Test = '	This is line 1: \{a = atan2(0,-1)\}
        This is line 2: \{sin(a/4)\}
	This is line 3: \{cos(a/4)\}'\}
\{Test2 = 'This has an embedded string: \{T = "This is a string"\}'\}
\{ECHO(ON)\}
Original String:
\{Test\}
Rescanned String:
\{rescan(Test)\}
Original String:
\{Test2\}
Print Value of variable T = \{T\}
Rescanned String:
\{rescan(Test2)\}
Print Value of variable T = \{T\}

Original String: \{t1 = "atan2(0,-1)"\}
Executed String: \{execute(t1)\}

string = \{_string = " one two, three"\}
delimiter "\{_delm = " ,"\}"
word count = \{word_count(_string,_delm)\}
second word = "\{get_word(2,_string,_delm)\}"

string = \{_string = " (one two, three * four - five"\}
delimiter "\{_delm = " ,(*-"\}"
word count = \{word_count(_string,_delm)\}
second word = "\{get_word(2,_string,_delm)\}"

string = \{_string = " one two, three"\}
delimiter "\{_delm = " ,"\}"
word count = \{ iwords = word_count(_string,_delm)\}

\{_n = 0\}
	\{loop(iwords)\}
word \{++_n\} = "\{get_word(_n,_string,_delm)\}"
   \{endloop\}

$ Check parsing of escaped braces...
\verb+\+\{ int a = b + \{PI/2\} \verb+\+\}
\verb+\+\{ \verb+\+\}
\end{apinp}
When processec by \aprepro{}, there will be two warning messages and
one error message:
\begin{apout}
Aprepro: WARN: Undefined variable 'Parse' (test.inp_app, line 74)
Aprepro: ERROR:  syntax error (test.inp_app, line 74)
Aprepro: WARN: Undefined variable 'T' (test.inp_app, line 106)
\end{apout}
The processed output from this example is:
\begin{apout}
$ Aprepro (Revision: 2.28) Mon Jan 21 10:58:23 2013
$ Test program for Aprepro
$
Test number representations
1	1	1	1	1
1	1	1	1	1

Test assign statements:
5	5	$ Should print 5 5
10	10 	$ Should print 10 10
5	5	$ Should print 5 5
25	25	$ Should print 25 25
5	5	$ Should print 5 5
3125	3125	$ Should print 3125 3125
5	3125	3125	$ Should print 5 3125 3125

Test trigonometric functions (radians)
3.141592654 3.141592654 3.141592654 $ Three values of pi
0.7071067812	4.440892099e-16	$ sin(pi/4)
0.7071067812	0	$ cos(pi/4)
1	0	$ tan(pi/4)

Test trigonometric functions (degrees)
180	180	180
0.7071067812	2.842170943e-14	$ sin(180/4)
0.7071067812	0	$ cos(180/4)
1	0	$ tan(180/4)

Test max, min, sign, dim, abs
0.5	-1 $ Should be 0.5, -1
1	-0.5 $ Should be 1.0, -0.5
0 1	$ Should be 0 1
1 0 $ Should be 1 0
1	0	$ Should be 1 0

$ Test ifdef lines
1 0
This line should be echoed. (a)
This line should be echoed. (b)
This line should be echoed. (c)
This line should be echoed. (d)

$ Test if - else lines
This line should be echoed. (1)
This line should be echoed (4)

$ Test if - else lines
This line should be echoed (6)
This line should be echoed. (7)
$ Lines a, b, c, d, 1, 4, 6, 7 should be echoed
$ Check line counting -- should be on line 74:

$ Test int and [] (shortcut for int)
5	-5
5	-5

$ Test looping - print sin, cos from 0 to 90 by 5
-5
0	0	1 1
5	0.08715574275	0.9961946981 1
10	0.1736481777	0.984807753 1
15	0.2588190451	0.9659258263 1
20	0.3420201433	0.9396926208 1
25	0.4226182617	0.906307787 1
30	0.5	0.8660254038 1
35	0.5735764364	0.8191520443 1
40	0.6427876097	0.7660444431 1
45	0.7071067812	0.7071067812 1
50	0.7660444431	0.6427876097 1
55	0.8191520443	0.5735764364 1
60	0.8660254038	0.5 1
65	0.906307787	0.4226182617 1
70	0.9396926208	0.3420201433 1
75	0.9659258263	0.2588190451 1
80	0.984807753	0.1736481777 1
85	0.9961946981	0.08715574275 1
90	1	6.123233996e-17 1

$$$$ Test formatting and string concatenation
0 %.10g
1 Using the format %.1g,	PI = 3
2 Using the format %.2g,	PI = 3.1
3 Using the format %.3g,	PI = 3.14
4 Using the format %.4g,	PI = 3.142
5 Using the format %.5g,	PI = 3.1416
6 Using the format %.6g,	PI = 3.14159
7 Using the format %.7g,	PI = 3.141593
8 Using the format %.8g,	PI = 3.1415927
9 Using the format %.9g,	PI = 3.14159265
10 Using the format %.10g,	PI = 3.141592654
11 Using the format %.11g,	PI = 3.1415926536
12 Using the format %.12g,	PI = 3.14159265359
13 Using the format %.13g,	PI = 3.14159265359
14 Using the format %.14g,	PI = 3.1415926535898
15 Using the format %.15g,	PI = 3.14159265358979
16 Using the format %.16g,	PI = 3.141592653589793
17 Using the format %.17g,	PI = 3.1415926535897931
18 Using the format %.18g,	PI = 3.14159265358979312
19 Using the format %.19g,	PI = 3.141592653589793116
20 Using the format %.20g,	PI = 3.141592653589793116
Reset format to default: %.10g

$$$$ Test string rescanning and executing

Original String:
	This is line 1: {a = atan2(0,-1)}
        This is line 2: {sin(a/4)}
	This is line 3: {cos(a/4)}
Rescanned String:
	This is line 1: 3.141592654
        This is line 2: 0.7071067812
	This is line 3: 0.7071067812
Original String:
This has an embedded string: {T = "This is a string"}
Print Value of variable T = 0
Rescanned String:
This has an embedded string: This is a string
Print Value of variable T = This is a string

Original String: atan2(0,-1)
Executed String: 3.141592654

string =  one two, three
delimiter " ,"
word count = 3
second word = "two"

string =  (one two, three * four - five
delimiter " ,(*-"
word count = 5
second word = "two"

string =  one two, three
delimiter " ,"
word count = 3

0
word 1 = "one"
word 2 = "two"
word 3 = "three"

$ Check parsing of escaped braces...
\{ int a = b + 1.570796327 \}
\{ \}

\end{apout}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Header                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Revisions: 2017-04-10 Martin Rädel <martin.raedel@dlr.de>
%                       Initial draft
%
% Contact:   Christian Willberg,  christian.willberg@dlr.de
%            DLR Composite Structures and Adaptive Systems
%
%                                 __/|__
%                                /_/_/_/
%            www.dlr.de/fa/en      |/ DLR
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Content                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Implemented time integration methods}
\subsection{Numerical damping added to Velocity-Verlet}
\subsubsection{Theory}

  The theory for the Velocity-Verlet solver with damping is derived in the following. The equations to compute position and velocity at
  the next time step without damping are given by
  \begin{equation}
  \begin{aligned}
   x_{n+1} \, &= \, x_n \, + \, \Delta t \, v_n \, + \, \dfrac{1}{2} \, \Delta t^2 \, a_n \\
   v_{n+1} \, &= \, v_n \, + \, \dfrac{1}{2} \, \Delta t \, (\, a_{n+1} \, + \, a_n \, ) ~.
  \end{aligned}
  \end{equation}
  $x$ denotes the position, $\Delta t$ the time increment, $v$ the velocity, $a$ the acceleration and subscript $n$ the time step number.
  The Velocity-Verlet algorithm with damping introduces an additional parameter $\gamma$ connected to the damping term and the velocity can be described by
  \begin{equation}
   v_{n} \, = \, \dfrac{x_{n+1} \, - \, x_{n-1}}{2 \, \Delta t} ~.
  \label{velointerpolation}
  \end{equation}
  Starting now with the Taylor expansion series
  \begin{equation}
  \begin{aligned}
   x_{n+1} \, &= \, x_n + \Delta t \, v_n \, + \, \dfrac{1}{2} \Delta t^2 \, a_n \, + \, \dfrac{1}{6} \Delta t^3 \dot{a}_n + O(\Delta t^4 ) \\
   x_{n-1} \, &= \, x_n - \Delta t \, v_n \, + \, \dfrac{1}{2} \Delta t^2 \, a_n \, - \, \dfrac{1}{6} \Delta t^3 \dot{a}_n + O(\Delta t^4 )
  \end{aligned}
  \end{equation}
  and adding both equations gives the Verlet algorithm
  \begin{equation}
   x_{n+1} \, = \, 2 \, x_n \, - \, x_{n-1} \, + \, \Delta t^2 a_n + O(\Delta t^4 ) ~.
  \end{equation}
  Expressing the accerlation in terms of force $F$ and mass $m$ allows to introduce a damping force $G$.
  \begin{equation}
   x_{n+1} \, = \, 2 \, x_n \, - \, x_{n-1} \, + \, \dfrac{\Delta t^2}{m} (\, F_n \, - \, G_n \, ) \, + \, O(\Delta t^4 )
  \label{verlet1}
  \end{equation}
  The damping force $G$ is defined by a damping coefficient $\gamma$ and the velocity
  \begin{equation}
   G(v_n) \, = \, \gamma \, v_n \, = \, \gamma \, \dfrac{x_{n+1} \, - \, x_{n-1}}{2 \, \Delta t} ~.
  \end{equation}
  Setting eq. \eqref{verlet1} back by one time step leads to
  \begin{equation}
   x_{n} \, = \, 2 \, x_{n-1} \, - \, x_{n-2} \, + \, \dfrac{\Delta t^2}{m} (\, F_n \, - \, \gamma \, v_n \, ) \, + \, O(\Delta t^4 ) ~.
  \label{verlet2}
  \end{equation}
  By adding eq. \eqref{verlet1} and eq. \eqref{verlet2} the resulting equation reads
  \begin{equation}
   x_{n+1} \, + \, x_n \, = \, 2 \, x_n \, + \, x_{n-1} \, - \, x_{n-2} \, + \, \dfrac{\Delta t^2}{m} (\, F_n \, - \, v_n \, + \, F_{n-1} \, + \, v_{n-1} \, ) \, + \, O(\Delta t^4 )~.
  \end{equation}
  Reformulating the equation, using the velocity interpolation for $v_n$ and $v_{n-1}$, shown in eq. \eqref{velointerpolation} and setting the equation one time step forward leads
  to the velocity integration for the Velocity-Verlet algorithm with damping
  \begin{equation}
   v_{n+1} \, = \, \dfrac{1}{1 \, + \, \gamma \dfrac{\Delta t}{2 \, m}} \, [ \, v_n \, ( \, 1 \, - \, \gamma \, \dfrac{\Delta t}{2 \, m} \, ) \, + \, \dfrac{\Delta t}{2} \,
   ( \, a_{n+1} \, + \, a_n \, ) \, ] ~.
  \end{equation}
  The time integration for the position stays the same as for Verlet-Velocity without damping. The parameter $\gamma$ is defined in Peridigm in the Verlet solver list \\
  \texttt{
  	<ParameterList name=``Verlet''> \\
	<Parameter name=``Numerical Damping'' type=``double'' value=``0.01''/> \\
  }
  The value should be chosen with respect to the material parameters in the input file.

\subsubsection{Changed files}
\begin{code}
./src/core/Peridigm.cpp
\end{code}
\subsubsection{Documentation of changes}
\paragraph{Peridigm.cpp}
The following changes have been made.
\begin{code}
  double numericalDamping = 0.0;
  if(verletParams->isParameter("Numerical Damping")){
    numericalDamping = verletParams->get<double>("Numerical Damping");
  }
\end{code}
If the parameter for numerical damping is defined in the input file, it will be read.
\begin{code}
    if(verletParams->isParameter("Numerical Damping")){
    	for (int i = 0; i < a->MyLength(); ++i) {
    		(*a)[i] = (*a)[i] / (1 + numericalDamping * dt2
    		/ (*density)[i/3]);
    		(*v)[i] = (*v)[i] * (1 - numericalDamping * dt2
    		/ (*density)[i/3]) / (1 + numericalDamping * dt2
    		/ (*density)[i/3]);
    	}
    }
\end{code}
The so-called mothership vectors are updated with the respective coeffcients for the step $v_{n+{\frac{1}{2}}}$.
\begin{code}
    if(verletParams->isParameter("Numerical Damping")){
	for (int i = 0; i < a->MyLength(); ++i) {
		(*a)[i] = (*a)[i] / (1 + numericalDamping * dt2
		/ (*density)[i/3]);
	}
    }
\end{code}
The mothership vector is updated with the respective coeffcients for the step $v_{n+1}$.


\documentclass[a4paper,man,natbib]{apa6}
\usepackage[english]{babel}

\usepackage[cache=false]{minted}
\usemintedstyle{vs}
\usepackage{xcolor}
\definecolor{bg}{rgb}{.95,.95,.95}

\graphicspath{ {./images/} }
\usepackage{graphicx}
\usepackage{caption}

\usepackage{setspace}
%\usepackage{titlesec}
%\titleformat{\subsection}[runin]% runin puts it in the same paragraph
%	{\normalfont\bfseries}% formatting commands to apply to the whole heading
%	{\thesubsection}% the label and number
%	{0.5em}% space between label/number and subsection title
%	{}% formatting commands applied just to subsection title
%	[]% punctuation or other commands following subsection title
% End Packages %

\title{Advanced Statistical Methods Homework 6}
\shorttitle{DAT 530 HW6}
\author{Brandon Hosley}
\date{\today}
\affiliation{University of Illinois - Springfield}
%\abstract{}

\begin{document}
\maketitle
\singlespacing

\section{Introduction to Statistical Learning \\ Chapter 6.8 : Problem 11}
\emph{We will now try to predict per capita crime rate in the
	\textbf{\textcolor{red}{Boston}} data set.}

Prepare the data set:

\begin{minted}[bgcolor=bg]{r}
library(MASS)
attach(Boston)
library(leaps)
\end{minted}

\subsection{(a)}
\emph{Try out some of the regression methods explored in this chapter,
	such as best subset selection, the lasso, ridge regression, and
	PCR. Present and discuss results for the approaches that you
	consider.}

\subsubsection{Best Subset Selection} \vspace{2em}

Determine the order of subset values: \\

\begin{minted}[bgcolor=bg]{r}
regfit.full=regsubsets(crim~.,Boston)
summary(regfit.full)
\end{minted}

Providing the following results: \\

\begin{minted}{r}
Subset selection object
Call: regsubsets.formula(crim ~ ., Boston)
13 Variables  (and intercept)
Forced in Forced out
zn          FALSE      FALSE
indus       FALSE      FALSE
chas        FALSE      FALSE
nox         FALSE      FALSE
rm          FALSE      FALSE
age         FALSE      FALSE
dis         FALSE      FALSE
rad         FALSE      FALSE
tax         FALSE      FALSE
ptratio     FALSE      FALSE
black       FALSE      FALSE
lstat       FALSE      FALSE
medv        FALSE      FALSE
1 subsets of each size up to 8
Selection Algorithm: exhaustive
zn  indus chas nox rm  age dis rad tax ptratio black lstat medv
1  ( 1 ) " " " "   " "  " " " " " " " " "*" " " " "     " "   " "   " "
2  ( 1 ) " " " "   " "  " " " " " " " " "*" " " " "     " "   "*"   " "
3  ( 1 ) " " " "   " "  " " " " " " " " "*" " " " "     "*"   "*"   " "
4  ( 1 ) "*" " "   " "  " " " " " " "*" "*" " " " "     " "   " "   "*"
5  ( 1 ) "*" " "   " "  " " " " " " "*" "*" " " " "     "*"   " "   "*"
6  ( 1 ) "*" " "   " "  "*" " " " " "*" "*" " " " "     "*"   " "   "*"
7  ( 1 ) "*" " "   " "  "*" " " " " "*" "*" " " "*"     "*"   " "   "*"
8  ( 1 ) "*" " "   " "  "*" " " " " "*" "*" " " "*"     "*"   "*"   "*"
\end{minted}

\begin{minted}[bgcolor=bg]{r}
regfit.full=regsubsets (crim~.,data=Boston ,nvmax=13)
reg.summary =summary (regfit.full)

par(mfrow=c(2,2))
plot(reg.summary$rss ,xlab="Number of Variables ",ylab="RSS", type="l")
plot(reg.summary$adjr2 ,xlab="Number of Variables ", ylab="Adjusted RSq",
	type="l")
which.max(reg.summary$adjr2)
points (8,reg.summary$adjr2[8], col="red",cex=2,pch =20)
plot(reg.summary$cp ,xlab="Number of Variables ",ylab="Cp", type='l')
which.min(reg.summary$cp)
points (8,reg.summary$cp [8], col ="red",cex=2,pch =20)
plot(reg.summary$bic ,xlab="Number of Variables ",ylab="BIC", type='l')
which.min(reg.summary$bic )
points (3,reg.summary$bic [3],col="red",cex=2,pch =20)
\end{minted}

\includegraphics[width=\linewidth]{subset}

Determine the best subset using Cross-validation as the evaluation metric. \\

\begin{minted}[bgcolor=bg]{r}
set.seed(1)
train=sample(c(TRUE ,FALSE), nrow(Boston),rep=TRUE)
test=(!train)
regfit.best=regsubsets(crim~.,data=Boston[train ,], nvmax=13)
test.mat=model.matrix(crim~.,data=Boston[test ,])
val.errors =rep(NA ,13)
for(i in 1:13){coefi=coef(regfit.best ,id=i)
pred=test.mat[,names(coefi)]%*%coefi
val.errors[i]=mean((Boston$crim[test]-pred)^2)
}
## Cross-Validation
predict.regsubsets = function(object , newdata ,id ,...){
form=as.formula (object$call [[2]])
mat=model.matrix(form ,newdata )
coefi=coef(object ,id=id)
xvars=names(coefi)
mat[,xvars]%*%coefi
}
k <- 10
folds=sample (1:k,nrow(Boston),replace=TRUE)
cv.errors=matrix (NA,k,13, dimnames=list(NULL, paste (1:13)))
for(j in 1:k){
best.fit <- regsubsets(crim~., data=Boston[folds!=j,],nvmax=13)
for(i in 1:13){
pred <- predict(best.fit ,Boston[folds ==j,], id=i)
cv.errors[j,i] <- mean(( Boston$crim[folds==j]-pred)^2)
}
}
mean.cv.errors=apply(cv.errors ,2, mean)
mean.cv.errors
par(mfrow=c(1,1))
plot(mean.cv.errors ,type='b')
reg.best=regsubsets (crim~.,data=Boston , nvmax=13)
coef(reg.best ,12)
\end{minted}

\includegraphics[width=\linewidth]{crossValSubset}

\clearpage
\subsubsection{Ridge Regression}.\\

\begin{minted}[bgcolor=bg]{r}
library(glmnet)

x=model.matrix(crim~.,Boston )[,-1]
y=Boston$crim

grid=10^seq(10,-2, length =100)
ridge.mod=glmnet (x,y,alpha=0, lambda=grid)
dim(coef(ridge.mod))

train=sample (1: nrow(x), nrow(x)/2)
test=(-train)
y.test=y[test]
## Cross-validation
cv.out=cv.glmnet(x[train ,],y[ train],alpha=0)
plot(cv.out)
bestlam =cv.out$lambda.min
bestlam
ridge.pred=predict (ridge.mod ,s=bestlam ,newx=x[test ,])
mean((ridge.pred -y.test)^2)
\end{minted}

This provides an $MSE$ of $40.62748$

\includegraphics[width=\linewidth]{CVRidge}

\subsubsection{The Lasso}.\\

\begin{minted}[bgcolor=bg]{r}
lasso.mod=glmnet(x[train ,],y[ train],alpha=1, lambda =grid)
plot(lasso.mod)
cv.out=cv.glmnet(x[train ,],y[ train],alpha=1)

plot(cv.out)
bestlam =cv.out$lambda.min
lasso.pred=predict (lasso.mod ,s=bestlam ,newx=x[test ,])
mean((lasso.pred -y.test)^2)

out=glmnet (x,y,alpha=1, lambda=grid)
lasso.coef=predict (out ,type="coefficients",s= bestlam) [1:14,]
lasso.coef
\end{minted}

The Lasso provided an $MSE$ of $42.21187$ and eliminated $2$ of the $13$ variables.

\subsubsection{PCR} \emph{Principle Components Regression}.\\

\begin{minted}[bgcolor=bg]{r}
library(pls)
set.seed(1)
pcr.fit=pcr(crim~., data=Boston, scale=TRUE, validation ="CV")
validationplot(pcr.fit ,val.type="MSEP")

pcr.pred=predict (pcr.fit ,x[test ,],ncomp =7)
mean((pcr.pred -y.test)^2)
\end{minted}

PCR provides an $MSE$ of $43.6911$.

\includegraphics[width=\linewidth]{CVPCR}

\subsection{(b)}
\emph{Propose a model (or set of models) that seem to perform well on
	this data set, and justify your answer. Make sure that you are
	evaluating model performance using validation set error, cross-validation, or some other reasonable alternative, as opposed to
	using training error.}

Of the above models the subset selection provided option with the lowest $MSE$.

\subsection{(c)}
\emph{Does your chosen model involve all of the features in the data
	set? Why or why not?}

The two lowest $MSE$ subsets were the 12 feature and the 9 feature subsets.
The while the 12 feature set provides marginal improvement over the 9 feature set, if there is an issue with over-fitting the 12 feature set is more likely to be over-fit.
For this model the efficiency is not really a concern, but if it needed to be applied on a large scale, the 9 feature set will be significantly more efficient in calculating predictions.

\end{document}

\begin{minted}[bgcolor=bg]{r}

\end{minted}

2020-Fall Advanced Statistical Methods/HW 06/HW 06.2_Hosley.tex


\documentclass[]{article}

%opening
\title{Mathematical modeling of hydrogen diffusion in biphasic steel.}
\author{Danilo de Freitas Naiff}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{hyperref}
% \usepackage{booktabs}
\usepackage{physics}
\usepackage{siunitx}
\usepackage[version=4]{mhchem}
\usepackage{graphicx}
\usepackage{indentfirst}
\usepackage{caption}
\usepackage{tikz}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{gensymb}
\usepackage{nomencl}
\usepackage{multirow}
\usepackage{algpseudocode}
\usepackage{subfig}
\usepackage{listings}
\usepackage{tikz}
\algnewcommand{\LineComment}[1]{\State \(\triangleright\) #1}
\makenomenclature

\newcommand{\red}{\textcolor{red}}
\newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}}

\newcommand{\argmax}{\operatorname{argmax}}
\newcommand{\argmin}{\operatorname{argmin}}
\def \gu {{\bar{g}}}
\def \Ev {{\mathbb E}}
\def \Pr {{\mathbb P}}
\def \f {{\mathbf f}}
\def \u {{\mathbf u}}
\def \y {{\mathbf y}}
\def \x {{\mathbf x}}
\def \m {{\mathbf m}}
\newcommand{\Var}{\mathrm{Var}}
\newcommand{\Cov}{\mathrm{Cov}}

\definecolor{darkgreen}{rgb}{0,0.5,0}

% My own definitions of colors
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
% Definitions on how to display listings
\lstdefinestyle{mystyle}{
	backgroundcolor=\color{white},
	commentstyle=\color{codegreen},
	keywordstyle=\color{blue},
	numberstyle=\tiny\color{codegray},
	stringstyle=\color{codepurple},
	basicstyle=\tiny,
	language=Python,
	breakatwhitespace=false,
	breaklines=true,
	captionpos= top,%b
	keepspaces=true,
	numbers=left,
	numbersep=5pt,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	%stepnumber=3,
	tabsize=2
}

\lstset{style=mystyle}

\begin{document}

\maketitle

\begin{abstract}

\end{abstract}

\section{Model}
The main model consists of hydrogen diffusion two-phase system, in which there is a bulk phase, with spherical precipitates in it, disperse enough so that we can do homogenization. Moreover, consider that the precipitates have a radius of $R$, that the bulk diffusivity is $D$, the precipitate diffusivity is $\alpha$, and there is $\Gamma$ precipitates per unit volume (we can get this statistics from the volume fraction, assuming spherical particles).

We denote by $c(x, t)$ the hydrogen concentration at the bulk phase, while we denote by $n(r, t; x)$ the concentration of hydrogen in the precipitate at $x$, on radius $r$ (of the precipitate) and time $t$.

The interface between two phases satisfies both the continuity of chemical potential, which translates to
\begin{equation}
	\frac{c}{n} = \frac{S_c}{S_n} = \exp \left( \frac{\mu_c - \mu_n}{RT} \right) =: K,
\end{equation}
and the continuities of fluxes
\begin{equation}
	J_c = J_n = J.
\end{equation}

Now, consider an interface at $(x, t)$. From the point of view of the bulk, we can think of this interface as being a sink for the bulk concentration, at the boundary. Then, \textit{each precipitate} results in a volumetric sink of strength $4 \pi R^2 J$. Of course, then we have that the sink per unit volume becomes $4 \pi R^2 \Gamma J$. Therefore, we can consider the bulk transport as following a diffusion equation with sink
\begin{equation}
	\pderiv{c}{t}(x,t) = D \frac{\partial^2 c}{\partial x^2}(x,t) - 4 \pi R^2 \Gamma J.
\end{equation}

Now, from the point of view of the precipitate at $x$, we have that the continuity of chemical potentials makes that, at the interface \textit{from the precipitate size}, we can consider a time-varying inlet concentration dependant on $c(x,t)$. That is, at $r = R$ (and $r = -R$), we must have
\begin{equation}
	n(R, t; x) = c(x, t).
\end{equation}
As for the precipitate itself, it follows that it must obey the spherically symmetric form of the diffusion equation
\begin{equation}
	\pderiv{n}{t}(r,t,x) = \alpha \frac{1}{r^2} \pderiv{}{r} \left( r^2 \pderiv{n}{r}(r,t,x)\right)
\end{equation}

And, of course, Fick's first law says that we can calculate the flux $J$ from the precipitate side, given by
\begin{equation}
	J = -\alpha \pderiv{n}{r}(R,t).
\end{equation}.
Therefore, we end up with the model as in the next section.

\section{Derivation of bulk equation}
Consider then

\begin{equation}\label{originalproblem}
	\begin{split}
	& \pderiv{c}{t}(x,t) = D \frac{\partial^2 c}{\partial x^2}(x,t) - 4 \pi R^2 \Gamma J; t \geq 0, 0 \leq x \leq L\\
	& \pderiv{n}{t}(r,t,x) = \alpha \frac{1}{r^2} \pderiv{}{r} \left( r^2 \pderiv{n}{r}(r,t,x)\right); \quad t \geq 0, -R \leq x \leq R \\
	& J = -\alpha \pderiv{n}{r}(R,t) \\
	& n(R,t,x) = n(-R,t,x) = K c(x,t) \\
	\end{split}
\end{equation}

First: solving $n$ differential equation. Ommiting $x$, let $m(r,t) := n(r,t) - K c(t)$. Hence,
\begin{equation}\label{precipitatemproblem}
	\pderiv{m}{t}(r,t) = \alpha \mathcal{O}[m](r,t) + c'(t); \quad m(r,t) = m(-R,t) = 0,
\end{equation}
with
\begin{equation}
	\mathcal{O}(m) = \frac{1}{r^2} \pderiv{}{r} \left( r^2 \pderiv{m}{r} \right)
\end{equation}
Using separation of variables, let $m(r,t) = T(t) \varphi(r)$. Hence, we have
\begin{equation}
 T' \varphi = \alpha \mathcal{O}[\varphi] T - K c'(t)
\end{equation}
We look then at the eigenvalue problem
\begin{equation}
\mathcal{O}[\varphi] = -\lambda \varphi,
\end{equation}
or
\begin{equation}
r^2 \varphi'' + 2 r \varphi' + \lambda r^2 \varphi = 0.
\end{equation}
The above equation equals to
\begin{equation}
(r \varphi)'' + \lambda (r \varphi) = 0.
\end{equation}
Considering $\varphi(R) = \varphi(-R) = 0$, for $\lambda_0 <= 0$, $\varphi(R) = 0$, and, for $\lambda > 0$,
\begin{equation}
(r \varphi) = R \sin (\sqrt{\lambda_k} r); \quad \sqrt{\lambda_k} = \frac{\pi k}{R}
\end{equation}
Hence, the eigenvalue problem is
\begin{equation}\label{eigenvaluemproblem}
\varphi_k(r) = \pi k j_0(\pi k r/R); \quad \lambda_k = \frac{\pi^2 k^2}{R^2}; \quad k = 1, 2, ...
\end{equation}
with $j_0(x) = \sin(x)/x$ being the zeroth spherical Bessel function of the first kind (who is also the sinc function).

Getting already some things off the table, we have that $\varphi_k$ forms an base of functions, that has the orthonormality relations
\begin{equation}\label{orthonomalityrelations}
\frac{1}{R} \int_{-R}^R (r/R)^2 \varphi_k(r) \varphi_l(r) dr = \delta_{m,l}.
\end{equation}
due to
\begin{equation}
\begin{split}
& \frac{1}{R} \int_{-R}^R (r/R)^2 \varphi_k(r) \varphi_l(r) dr = \\
& 2 \pi^2 k^2 \int_{0}^1 u^2 j_o(\varphi k u) j_0(\varphi l u) du = \\
& 2 \pi^2 k^2 \frac{\delta_{k,l}}{2} (j_1(\pi k))^2 = \\
& \delta_{k,l} \pi^2 k^2 \left(\frac{-(-1)^k}{\pi k}\right)^2 = \\
& \delta_{k,l}
\end{split},
\end{equation}
with $j_1(x)$ being the first spherical Bessel function of the first kind. Hence, for any (suitable) $f(r)$, we can decompose
\begin{equation}
\begin{split}
& f(r) = \sum_{k=1}^\infty f_k \varphi_k \\
& f_k(r) = \frac{1}{R} \int_{-R}^R (r/R)^2 f(r) \varphi_k(r) dr.
\end{split}
\end{equation}.
In particular, we use the decomposition
\begin{equation}
K c'(t) = \sum_{k=1}^\infty K c'_k(t) \varphi_k(t).
\end{equation}
We have that, using the eigenvalue problem, decomposing
\begin{equation}
m(r,t) = \sum_{k=1}^\infty \psi_k(t) \varphi_k(r),
\end{equation}
we have that
\begin{equation}
\sum_{k=1}^{\infty} \left(\psi_k'(t) + \lambda_k \alpha \psi_t + K c'_k(t) \right) \varphi(k) = 0,
\end{equation}
Hence, we have
\begin{equation}\label{fullphikdefinition}
\psi_k(t) = e^{-\alpha \lambda_k t} \left(\psi_k(0) - \int_0^t e^{\alpha \lambda k \tau} K c'_k(\tau) d \tau \right),
\end{equation}
or, assuming $\psi_k(0) = 0$ (because we will consider $n(0,t,x) = 0$ and $c(x,0) = 0$, although this definition \textit{still works} for $n(r,t,x) = n(0,t,x) = K c(x,t)$,
\begin{equation}\label{phikdefinitions}
\begin{split}
\psi_k(t) & = -K e^{-\alpha \lambda_k t} \int_0^t e^{\alpha \lambda k \tau} c'(\tau) \frac{1} R \int_{-R}^R (r/R)^2 \varphi_k(r) dr d \tau = \\
& = 2 K \frac{(-1)^k}{\pi k} e^{-\alpha \lambda_k t} \int_0^t e^{\alpha \lambda_k \tau} c'(\tau) d \tau
\end{split}
\end{equation}
And, we remember that $m(r,t) = n(0,t) - K c(t)$. Finally, we can then find
\begin{equation}
J = \alpha \pderiv{n}{r} (t,R) = -\alpha \sum_{k=1}^\infty \psi_k(t) \pderiv{\varphi_k}{r}(R).
\end{equation}.
But, we have that $\pderiv{\varphi_k}{r}(R) = (-1)^k \pi k/R$. Hence, we can join everything and find that
\begin{equation}
J = \frac{2}{R} \alpha K \sum_{k=1}^\infty e^{-\alpha \lambda_k t} \int_0^t e^{\alpha \lambda_k \tau} c'(\tau) d \tau
\end{equation}
Therefore, our equation for $c$ becomes (showing again $x$):
\begin{equation}\label{bulkproblemform2a}
\pderiv{c}{t}(x,t) = D \frac{\partial^2 c}{\partial x^2}(x,t) - 8 \pi R \Gamma \alpha K \sum_{k=1}^\infty e^{-\alpha \lambda_k t} \int_0^t e^{\alpha \lambda_k \tau} c'(x, \tau) d \tau
\end{equation}
or
\begin{equation}\label{bulkproblemform2b}
\begin{split}
& \pderiv{c}{t}(x,t) = D \frac{\partial^2 c}{\partial x^2}(x,t) - 8 \pi R \Gamma \alpha K \sum_{k=1}^\infty (\omega_k *_t c')(x,t) \\
& \omega_k(t) = \exp \left( -\frac{\alpha \pi^2 k^2 t}{R^2} \right),
\end{split}
\end{equation}
with $(f *_t g)(x,t) = \int_0^t f(x, t - \tau) g(x, tau) d\tau $ being the convolution evaluated in the $t$ argument.

Of course, we can simplify things by making
\begin{equation}\label{auxiliarydefinitions2}
	\begin{split}
	& \beta := 8 \pi R \Gamma \alpha K \\
	& \omega := \sum_{k=1}^\infty \omega_k = \sum_{k=1}^\infty \exp \left( -\frac{\alpha \pi^2 k^2 t}{R^2} \right)
	\end{split},
\end{equation}
resulting in \eqref{bulkproblemform2b} being written as
\begin{equation}\label{bulkproblemform2}
	\pderiv{c}{t} + \beta \left(\omega *_t \pderiv{c}{s}\right) = D \frac{\partial^2 c}{\partial x^2},
\end{equation}
or, expanding the convolution,
\begin{equation}\label{bulkproblemform3}
	\pderiv{}{t} c(x, t) + \beta \int_0^t \omega(t - s) \pderiv{}{s} c(x, s) ds = D \frac{\partial^2 c}{\partial x^2}(x,t)
\end{equation}
From \eqref{phikdefinitions}, we can also compute $n(r, t, x)$ as
\begin{equation}
	\begin{split}
	n(r,t,x) = & K c(t, x) + \sum_{k=1}^\infty \psi_k(t) \varphi_k(r) = \\
	& K c(t, x) + 2 K \sum_{k=1}^\infty \frac{(-1)^k}{\pi k} e^{-\alpha \lambda_k t} \int_0^t e^{\alpha \lambda_k \tau} c'(\tau) d \tau \varphi_k(r) = \\
	& K c(t,x) + 2 K \int_0^t \sum_{k=1}^\infty \frac{(-1)^k}{\pi k} e^{\alpha \lambda_k (\tau-t)} \varphi_k(r) c'(\tau) d \tau,
	\end{split}
\end{equation}
or
\begin{equation}
n(r,t,x) = K \left(c(x, t) - \int_0^t \xi(r, t - s) \pderiv{}{s} c(x, s) ds\right)
\end{equation},
where
\begin{equation}
	\xi(r, t) := -2 \sum_{k=1}^\infty \frac{(-1)^k}{\pi k} e^{\alpha \lambda_k t} \varphi_k(r).
\end{equation}
If we are interested in a more "global" information (not depending on $r$), we then define the mean amount of hydrogen per precipitate $c_p = \frac{1}{V} \int_V n(r) dr$, we have
\begin{equation}
	\begin{split}
	c_p(x, t) := K \left( c(x,t) - \int_0^t \omega_p(t-s) \pderiv{}{s} c(x, s) ds \right)
	\end{split},
\end{equation}
with $\omega_p(t)$ being given by
\begin{equation}
	\frac{3}{2} \sum_{k=1}^k \frac{1}{\pi^2 k^2} \exp \left( -\frac{\alpha \pi^2 k^2 t}{R^2} \right)
\end{equation}

\subsection{Cylindric precipitates, \textit{mutatis mutandis}}
If we consider the approximation by cylindric precipitates, then, defining $\Gamma_c$ as the amount precipitates per cross-sectional area, we can follow the same arguments above (now using Bessel functions of the first kind), and arrive at almost the same equations, now with
\begin{equation}
	\begin{split}
		& \beta = 4 \pi \Gamma \alpha K \\
		& \omega = \sum_{k=1}^\infty \omega_k = \sum_{k=1}^\infty \exp \left( -\frac{\alpha u_{0, k}^2 t}{R^2} \right) \\
		& c_p(x,t) = 4 \pi R^2 K \left( c(x,t) - \int_0^t \omega_p(t-s) \pderiv{}{s} c(x, s) ds \right) \\
		& \omega_p(t) = \frac{1}{4} \sum_{k=1}^k \frac{1}{u_{0, k}^2} \exp \left( -\frac{\alpha \pi^2 u_{0, k}^2 t}{R^2} \right)
	\end{split},
\end{equation}
with $u_{0, k}$ being the k-th zero of the zeroth Bessel function of the first kind.

\section{Discretization}
Let's discretize this. Forgetting about $x$ for now, consider only the operator
\begin{equation}
	\pderiv{}{t} c(t) + \beta \int_0^t \omega(t - s) \pderiv{}{s} c(s) ds.
\end{equation}
Now, consider a discretization
\begin{equation}
	\begin{split}
	& t_0, t_1, \ldots, t_n, t_{n+1} \\
	& c_0, c_1, \ldots, c_n, c_{n+1} \\
	& h_i := t_{i} - t_{i-1} \\
	& t_{i+1/2} = (t_{i+1} - t_{i})/2
	\end{split}
\end{equation}
Then, we can discretize the integral at time $t_{n+1}$ by
\begin{equation}
	\int_0^t \omega(t - s) \pderiv{}{s} c(s) ds \approx \sum_{i=0}^n \omega(t_{n+1} - t_{i+{1/2}}) (c_{i+1} - c_{i}).
\end{equation}
Separating the $c_{n+1}$ from the rest, and defining $\omega^{n+1, i}$ as $\omega(t_{n+1} - t_{i+{1/2}})$, we have the discretization as a function of $c_{n+1}$
\begin{equation}
	\frac{c_{n+1} - c_{n}}{h_{n+1}} + \beta (c_{n+1} - c_n) \omega^{n+1, n} + \beta \sum_{i=0}^{n-1} (c_{i+1} - c_i) \omega^{n+1, i},
\end{equation}
or
\begin{equation}
	\left(\frac{1}{h_{n+1}} + \beta \omega^{n+1, n} \right) c_{n+1} - \left(\frac{1}{h_{n+1}} + \beta \omega^{n+1, n} \right) c_{n} + \beta \sum_{i=0}^{n-1} (c_{i+1} - c_i) \omega^{n+1, i}.
\end{equation}

Now, considering the space discretization \footnote{can be finite differences, elements, or whatever}, let the discretized operator matrix be $A$ letting $\mathbf{c}$ be the space discretization vector, we have that, using the implicit Euler method, the above problem translates to (rewritten so that we guarantee a positive diagonal)
\begin{equation}
 \begin{split}
 & \left(-A + (h_{n+1}^{-1} + \beta \omega^{n+1, n}) I \right)\mathbf{c}_{n+1} = \\
 & -\beta \sum_{i=0}^{n-1} (\mathbf{c}_{i+1} - \mathbf{c}_i) \omega^{n+1, i} + \left(h_{n+1}^{-1} + \beta \omega^{n+1, n} \right) \mathbf{c}_{n},
 \end{split}
\end{equation}
which we can then solve at each time step. Some notes here
\begin{itemize}
	\item In practice, $\omega$ decays as $t_{n+1} - t_i$ grows large. Can probably do some cutoff here.
	\item If we use a constant time step, we can define $\hat{\omega}^j := \omega^{n+1, n-j} = \omega(h(j + 1/2))$. We just calculate beforehand for some values of $j$ and hold the values (remembering the cutoff note above).
\end{itemize}

\section{Results}
There are the results of a simulation of this model, with unit concentration at the inlet and zero concentration at the outlet.
\begin{itemize}
	\item Physical parameters (some taken from Olden)
	\begin{itemize}
		\item $D$ - 6.0*1e-1 $\mu m^2/s$
		\item $\alpha$ - 1.4*1e-4 $\mu m^2/s$
		\item $K$ - 32.51/0.033
		\item $R$ = 5 $\mu m$
		\item $\Gamma$ - 0.0007639437268410977 $\mu m^{-3}$ (from 0.1 of volumetric fraction)
		\item $L$ - 100 $\mu m$
	\end{itemize}
	\item Solver parameters
	\begin{itemize}
		\item Space discretization points = 21
		\item Maximum kernel expansion = 1000
		\item Time step = 0.25
		\item Decay limit = 0.01
		\item Memory's maximum size = 1000
	\end{itemize}
\end{itemize}

\begin{figure}[h]
	\centering
	\includegraphics[width=\linewidth]{figs/flux}
	\label{fig:flux}
\end{figure}

\begin{figure}[h]
	\centering
	\includegraphics[width=\linewidth]{figs/profile999}
	\label{fig:profile999}
\end{figure}

\begin{figure}[h]
	\centering
	\includegraphics[width=\linewidth]{figs/profile4999}
	\label{fig:profile4999}
\end{figure}

\section{Transform approaches}
\subsection{Fourier transforming bulk equation}
We can do other things. Let's try to Fourier transform this (in $t$). Let $C(x,s) = \mathcal{F}_t(c(x,t))(s)$. Hence, we have that
\begin{equation}
\mathcal{F}_t\left(\pderiv{}{t}c(x,t) \right)(s) = 2 \pi i s C(x,s)
\end{equation}
\begin{equation}
\mathcal{F}_t\left(\sum_{k=1}^\infty (\omega_k *_t c')(x,t)\right) = C(x,s) \sum_{k=1}^\infty \frac{1}{1 - i\frac{\alpha \pi^2 k^2}{2 R^2 \pi s}},
\end{equation}
or
\begin{equation}
\mathcal{F}_t\left(\sum_{k=1}^\infty (\omega_k *_t c')(x,t)\right) = C(x,s) \sum_{k=1}^\infty \frac{1}{1 + \left(\frac{\alpha \pi^2 k^2}{2 R^2 \pi s}\right)^2}\left(1 + i\frac{\alpha \pi^2 k^2}{2 R^2 \pi s}\right),
\end{equation}
and the diffusive term stays the same. Hence, joining everything
\begin{equation}
C(x,s) \zeta(s) = D \frac{\partial^2 C}{\partial x^2}(x,s),
\end{equation}
with
\begin{equation}
\zeta(s) = 2 \pi i s + 8 \pi R \Gamma \alpha K \sum_{k=1}^\infty \frac{1}{1 - i \frac{A k^2}{s}}; \quad A = \frac{\alpha \pi}{2 R^2}
\end{equation}

\subsection{Laplace transforming bulk equation}
In a similar manner, we try Laplace transform then. Let $\mathcal{C}(x,s) = \mathcal{L}_t(c(x,t))(s)$.
He have that
\begin{equation}
\mathcal{L}_t\left(\pderiv{}{t}c(x,t) \right)(s) = s \mathcal{C}(x,s) - c(x,0),
\end{equation}
and
\begin{equation}
\mathcal{L}_t\left(\sum_{k=1}^\infty (\omega_k *_t c')(x,t)\right) = (s \mathcal{C}(x,s) - c(x,0)) \sum_{k=1}^\infty \frac{1}{1 + \frac{\alpha \pi^2 k^2}{R^2} s}
\end{equation}
\begin{equation}
\sum_{k=1}^\infty \frac{1}{1 + \frac{\alpha \pi^2 k^2}{R^2} s} = \frac{1}{2}\left(\sqrt{\frac{R}{\alpha}} s^{-1/2} \coth \left(\sqrt{\frac{R}{\alpha}} s^{-1/2}\right) - 1 \right).
\end{equation}
Since we assumed $c(x,0) = 0$, the equation for $\mathcal{C}$ becomes
\begin{equation}
\begin{split}
& \mathcal{C}(x,s) \xi(s) = \frac{\partial^2 \mathcal{C}}{\partial x^2}(x,s) \\
& \xi(s) = \frac{s}{D} \left(1 + \frac{\beta}{2} \left(\sqrt{\frac{R}{\alpha s}} \coth \left(\sqrt{\frac{R}{\alpha s}} \right) - 1 \right) \right)
\end{split}.
\end{equation}

We must have
\begin{equation}
\mathcal{C}(x,s) = A(s) e^{\sqrt{\xi(s)} x} + B(s) e^{-\sqrt{\xi(s)} x}
\end{equation}

Using Dirichlet conditions $c(0,s) = c_0$, $c(L,s) = 0$, we have that $\mathcal{C}(0,s) = c_0/s$, $\mathcal{C}(L,0) = 0$, and we find $A(s), B(s)$ solving the system
\begin{equation}
\begin{bmatrix}
1 & 1 \\
e^{\sqrt{\xi(s)} L} & e^{-\sqrt{\xi(s)} L}
\end{bmatrix} \begin{bmatrix} A(s) \\ B(s) \end{bmatrix} = \begin{bmatrix} c_0/s \\ 0 \end{bmatrix},
\end{equation}
We have that
\begin{equation}
\begin{split}
& A(s) = \frac{e^{-\sqrt{\xi(s)} L}}{e^{-\sqrt{\xi(s)} L} - e^{\sqrt{\xi(s)} L}} \frac{c_0}{s} \\
& B(s) = -\frac{e^{\sqrt{\xi(s)} L}}{e^{-\sqrt{\xi(s)} L} - e^{\sqrt{\xi(s)} L}} \frac{c_0}{s}
\end{split}
\end{equation}
Then, we have that
\begin{equation}
\mathcal{C}(x,s) = \frac{c_0}{s \left(e^{-\sqrt{\xi(s)} L} - e^{\sqrt{\xi(s)} L}\right)} \left(e^{-\sqrt{\xi(s)}(L-x)} - e^{\sqrt{\xi(s)}(L-x)}\right)
\end{equation}
Or, simplifying
\begin{equation}
\frac{c_0}{s} \frac{\sinh(\sqrt{\xi(s)}(L-x))}{\sinh(\sqrt{\xi(s)} L)}
\end{equation}
For each $x$, Laplace-invert $\mathcal{C}(x,s)$. Have no idea how tough this is numerically.

\end{document}


\documentclass[nofootinbib,preprintnumbers,superscriptaddress,notitlepage]{revtex4-1}

\usepackage{times}
\usepackage[usenames]{color}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{dcolumn}
\usepackage{enumerate}
\usepackage{epsfig}
\usepackage{graphicx}
\usepackage{graphics}
\usepackage[latin1]{inputenc}
\usepackage{latexsym}
\usepackage{rotating}
\usepackage{hyperref}
\usepackage[caption=false]{subfig}

%setup packages
\hypersetup{
    colorlinks=true,
    linkcolor=red,
    filecolor=magenta,
    urlcolor=magenta,
    citecolor=magenta,
}

%Define equation environment shorthand
\newcommand{\<}{\begin{equation}}
\newcommand{\?}{\end{equation}}
%=========================================================================
\begin{document}
%=========================================================================
\title{Solving the scalar wave equation using spacetime discretization}
\date{\today}
\maketitle
\subsection{Discretizing the Action}
Consider the Lagrangian density $\mathcal{L}$ for a free scalar wave equation,
that is used to extremize the action $\mathcal{S}$
\begin{equation}
\label{action_continous}
S = \int \textrm{d}x\,\textrm{d}t~\mathcal{L} =  \int  \left[\dfrac{1}{2}
\eta^{\alpha \beta}\, \partial_{\alpha} u\, \partial_{\beta} u\right]~
\textrm{d}x\,\textrm{d}t.
\end{equation}
We wish to find a solution vector $\mathbf{u}$ over a $N\times N$ spacetime
grid. Thus $\mathbf{u}$ is a vector of length $N\times N$. Now, we write the
discretized version of Eq.~(\ref{action_continous}), by first replacing the
continous integral with a summation, and replacing the derivatives by
derivative operators. Note, that the summation is over spacetime points.
\begin{equation}
\label{action_discrete}
S_d = \dfrac{1}{2} \sum\limits_{p=0}^{N\times N}\sum\limits_{q=0}^{N\times N} w_{pq}\, \eta^{ab} \, D_{ak}^p u^{k}\, D_{bl}^q u^{l}
\end{equation}
The matrix $w^{pq}$ (an outer product of one-dimensional Curtis-Clenshaw
weights) corresponds to the integration weights at each point on the spacetime
grid, arranged along the diagonal of a $(N\times N)$ matrix. Further, the
derivative operator $D^a_{pk}$ corresponds to the derivative of the vector
$u^k$ evaluated at $p$. Concretly, the operator can be expressed as
\begin{equation}
\eta^{ab} \, D_{ak}^p\, D_{bl}^q = - \rm{I}\otimes D_t^2 +  D_x^2\otimes\rm{I}
\end{equation}
for a (1+1) dimensionsal case, which gives a square matrix of dimension
$(N\times N)$. Eq.~(\ref{action_discrete}), therefore, can be written in terms
of a quadratic form as
\begin{equation}
S_d = \dfrac{1}{2} \mathbf{u}^T \mathbf{C}\, \mathbf{u}; \qquad \mathbf{C} = w^{pq}\, \eta^{ab} \, D_{ak}^p \, D_{bl}^q.
\end{equation}
Now, given the method of integration, and the choice of basis functions we can
construct the matrix $C$. It's a quadratic form, i.e. $S_d$ is a scalar.
Therefore, our problem reduces to finding the vector $u^l$, such that the
scalar $S_d$ is extremum. This is done by requiring
\begin{equation}
\label{main}
\dfrac{\partial S_d}{d\mathbf{u}} = 0 = \dfrac{1}{2}\mathbf{u}^T (\mathbf{C} + \mathbf{C}^T)
\end{equation}
where the last equality is a standard result applicable to quadratic forms.
Therefore, to get the solution, we solve the homogenous system of linear
equations given by Eq.~(\ref{main}). For us, the form of Eq.~(\ref{main})
isn't the most intuitive. We can also write the result of the minization as
\begin{equation}
\label{solve}
\dfrac{1}{2}(\mathbf{C} + \mathbf{C}^T)~\mathbf{u} = \mathbf{b}
\end{equation}
by manipulating the second term of the derivative instead of the first
term.\footnote{For a quick reference, see Eq.~(46) in
\href{http://www.atmos.washington.edu/~dennis/MatrixCalculus.pdf}{this
document.}} Note, however, the matrix $\mathbf{C}$ is
symmetric\footnote{\textcolor{red}{This needs checking. In our implementation,
the matrix A is not symmetric. However, symmetrizing the matrix leads to the
wrong solution.}}, we can just solve the system $\mathbf{C}\mathbf{u} =
\mathbf{b}  $, where $\mathbf{b} $ contains the conditions imposed by the
evolution equation and the boundary conditions.\\

The above formalism has been succesfully implemented in Python for the 1D
scalar wave equation in Cartesian coordinates, and extending it to higher
dimensions seems straightforward at the moment. We know wish to evolve the
scalar wave equation in null coordinates. To begin, we define two new coordinates
$(u,v)$ such that
\begin{equation}
u = t - r, \qquad v = t + r,
\end{equation}
which immediately leads us to the inverse transformation
\begin{equation}
t = \dfrac{u + v}{2}, \qquad r = \dfrac{v - u}{2}.
\end{equation}
Since we'd still be working in 1D, the notation is consistent with 1D scalar
wave equation in Cartesian coordinates. We now calculate how the
action (see Eq.~\ref{action_continous}) looks, in these new coordinates. We get
\begin{equation}
S = \dfrac{1}{2} \int \eta^{uv}\partial_u\phi ~\partial_v\phi  \sqrt{-\eta}\,du\,dv,
\end{equation}
where $\eta^{uv}$ is the Minkowski metric in $(u,v)$ coordinates. However, if
we further introduce a conformal compactification, we get an additional term
since the Ricci scalar is no longer zero. Thus, we'd now have
\begin{equation}
S = \dfrac{1}{2}\int \left[ \tilde{\eta}^{uv}\partial_u\phi ~\partial_v\phi + \tilde{R} \right] \sqrt{-\tilde{\eta}}\,du\,dv,
\end{equation}
where $\tilde{\eta}^{uv}$ and $\tilde{R}$ are the metric tensor, and the Ricci
scalar for the conformally compactified Minkowski spacetime. Further, if we
consider a massive scalar field with a time-varying potential, we can add two
additional terms to get such an action for the system:
\begin{equation}
S = \dfrac{1}{2}\int \left[ -\tilde{\eta}^{uv}\partial_u\phi ~\partial_v\phi + m^2\phi + V(\phi) + \tilde{R} \right] \sqrt{-\tilde{\eta}}\,du\,dv.
\end{equation}
If we adopt the hyperboloidal scri-fixing coordinates the authors of \cite{Zenginolu} adopt, then we have

% \appendix
% %=========================================================================
% \section{Finite Element Approach}
% %=========================================================================

% %=========================================================================
% \subsection{Introduction}
% %=========================================================================

% We are interested in finding solutions to the scalar wave equation using a
% finite element approach using spacetime discretization. Specifically, we want
% to find solutions $u(x, t)$ to
% \begin{equation}
% \label{eq:wave_equation}
% u_{tt} - c^2 \bigtriangleup u = 0, \qquad \text{in} \quad \Omega \times (0, T],
% \end{equation}
% with boundary and initial conditions (we will check if these are necessary
% and/or sufficient below)
% \begin{eqnarray}
% \label{eq:boundary_conditions}
% u =& 0,\quad \partial_{n} u &= 0 \quad \text{in} \quad \partial\Omega,\\
% \label{eq:initial_data}
% u(\cdot, 0) =& f,\quad u_t(\cdot, 0) &=  0 \quad \text{in} \quad \Omega,
% \end{eqnarray}
% where Eq.~(\ref{eq:boundary_conditions}) enforces a reflecting boundary
% condition at the spatial boundaries, and Eq.~(\ref{eq:initial_data}) specifies
% the initial data in terms of the wave profile and it's derivative at $t=0$.\\

% %=========================================================================
% \subsection{Variational Formulation}
% %=========================================================================

% We start by computing the variational formulation of
% Eq.~(\ref{eq:wave_equation}). If $u(x,t)$ is a solution to
% Eq.~(\ref{eq:wave_equation}), then for any reasonable test function $v \in
% \Omega \times (0, T]$
% \begin{equation}
% u_{tt}\,v - c^2 (\bigtriangleup u)\, v = 0.
% \end{equation}
% Integrating over the domain $\mathcal{M} = \Omega \times (0, T]$, and using
% integration by parts for converting the second order derivatives to first
% order derivatives, we get
% \begin{eqnarray}
% \int\limits_{\mathcal{M}} u_{tt}\,v \mathrm{\,dt\, d\Omega}
% - c^2 \int\limits_{\mathcal{M}} \nabla\cdot(\nabla u)\, v \mathrm{\,dt\, d\Omega}
% &=& 0,\\
% \label{eq:intbyparts}
% \int\limits_{\Omega}\left[u_t v \right]_{0}^{T}\,\mathrm{d\Omega}
% - \int\limits_{\Omega}\mathrm{d\Omega}\int\limits_{T} u_t v_t \mathrm{\,dt}
% + c^2\int\limits_{T}\,\mathrm{dt}\int\limits_{\Omega} \nabla u \cdot \nabla v \,\mathrm{\,d\Omega}
% - c^2\int\limits_{T}\,\mathrm{dt}\int\limits_{\Omega} \nabla \cdot \left(v\,\nabla u\right) \,\mathrm{\,d\Omega}
% &=& 0.
% \end{eqnarray}
% Applying Gauss law to the fourth term in the right hand side of Eq.~(\ref{eq:intbyparts}), we get
% \begin{equation}
% \label{eq:initialweakform}
% \int\limits_{\Omega}\left[u_t v \right]_{0}^{T}\,\mathrm{d\Omega}
% - \int\limits_{\mathcal{M}} u_t v_t \mathrm{\,dtd\Omega}
% + c^2\int\limits_{\mathcal{M}} \nabla u \cdot \nabla v \,\mathrm{\,dt d\Omega}
% - c^2\int\limits_{T}\,\mathrm{dt}\oint\limits_{\partial\Omega} v\, \left(\nabla u \cdot \mathbf{n}\right) \,\mathrm{\,d\Gamma}
% = 0.
% \end{equation}
% The above equation is the intial weak form of the scalar wave equation. We now
% apply the boundary conditions to set some of the terms to
% zero\footnote{\textcolor{red}{How we apply these boundary conditions
% explicitly in the code is something I'm still working on.}}. Consider the
% fourth term in Eq.~(\ref{eq:initialweakform}). We set this to zero, since our
% boundary conditions demand that the flux $(\nabla u \cdot \mathbf{n})$ at the
% boundary is zero; see Eq.~(\ref{eq:boundary_conditions}).The first term can
% also be (partially) eliminated using using the initial condtions.
% \textcolor{red}{However, the term $u_t v |_{T}$ remains, since we do not have
% a boundary condition there. We could, however, choose a function $v$, which
% vanishes at the final time slice, but I'm not sure if that's allowed. For the
% time, we assume that the first term can also be set to zero in
% Eq.~(\ref{eq:boundary_conditions})}.\\

% Therefore, we have, together with the Dirichlet boundary conditions
% \begin{eqnarray}
% \label{eq:variationalform}
% - \int\limits_{\mathcal{M}} u_t v_t \mathrm{\,dtd\Omega}
% + c^2\int\limits_{\mathcal{M}} \nabla u \cdot \nabla v \,\mathrm{\,dt d\Omega}
% &=& 0, \quad \text{in}~\Omega,\\
% u &=& 0, \quad \text{in}~\partial\Omega.
% \end{eqnarray}

% %=========================================================================
% \subsection{Spacetime discretization}
% %=========================================================================

% For the sake of simplicity, we consider Eq.~(\ref{eq:variationalform}) in 1D,
% where Eq.~(\ref{eq:variationalform}) becomes much simpler, and reduces to
% \begin{equation}
% \label{eq:variationalform_final}
% - \int\limits_{\mathcal{M}} u_t v_t \mathrm{\,dt dx}
% + c^2\int\limits_{\mathcal{M}} u_x v_x \,\mathrm{\,dt dx}
% = 0.
% \end{equation}
% The above equation has been derived directly in \cite{Zumbusch}. We now
% discretize Eq.~(\ref{eq:variationalform_final}) using a spacetime
% discretization approach, and express $u$ in terms of $M$ basis functions in
% time $\tilde{\psi}_l(t)$, and $N$ basis functions in space $\tilde{\phi}_i(x)$
% as
% \begin{equation}
% u = \sum\limits_{l=0}^{M}\sum\limits_{i=0}^{N}\tilde{u}_i^l \, \tilde{\phi}_i(x)\, \tilde{\psi}_l(t), \\
% \end{equation}
% and choose the test function as any of the basis functions
% \begin{equation}
% v = \tilde{\phi}_j(x)\, \tilde{\psi}_m(t),
% \end{equation}
% as we are free to choose any (reasonable) function in $\Omega$. Plugging these
% into Eq.~(\ref{eq:variationalform_final}), we arrive at (setting $c=1$ for convinience)
% \begin{equation}
% \label{eq:summation_expanded}
% - \sum\limits_{i=0}^{N}\sum\limits_{l=0}^{M} u_i^l
% \int\limits_{x} \mathrm{dx}\, \tilde{\phi}_i(x)\tilde{\phi}_j(x)
% \int\limits_{T} \mathrm{dt}\, \partial_t\tilde{\psi}_l(t)\partial_t\tilde{\psi}_m^0(t)
% + \sum\limits_{i=0}^{M}\sum\limits_{l=0}^{N} u_i^l
% \int\limits_{T} \mathrm{dt}\, \tilde{\psi}_l(t)\tilde{\psi}_m(t)
% \int\limits_{x} \partial_x\tilde{\phi}_i(x)\partial_x\tilde{\phi}_j(x) = 0.
% \end{equation}
% Defining the mass matrix M and stiffness matrix A in the spatial dimension,
% \begin{eqnarray}
% \label{eq:matrix_space}
% M := m_{ij} &=& \int\limits_{x} \mathrm{dx}\, \tilde{\phi}_i(x)\tilde{\phi}_j(x), \\
% A := a_{ij} &=& \int\limits_{x} \partial_x\tilde{\phi}_i(x)\partial_x\tilde{\phi}_j(x),
% \end{eqnarray}
% and two additional matrices
% \begin{eqnarray}
% \label{eq:matrix_time}
% k_{lm} &=& \int\limits_{T} \mathrm{dt}\, \tilde{\psi}_l(t)\tilde{\psi}_m(t), \\
% p_{lm} &=& \int\limits_{T} \mathrm{dt}\, \partial_t\tilde{\psi}_l(t)\partial_t\tilde{\psi}_m(t),
% \end{eqnarray}
% we can express Eq.~(\ref{eq:summation_expanded}) as
% \begin{equation}
% - \sum\limits_{l=0}^{M}\sum\limits_{i=0}^{N} u_i^l\, m_{ij}\, p_{lm}
% + \sum\limits_{l=0}^{M}\sum\limits_{i=0}^{N} u_i^l\, a_{ik}\, k_{lm} = 0.
% \end{equation}
% Notice that we have two free indices $(j,m)$ and hence $(j \times m)$
% independent equations, the same number as the number of unknowns $(i\times
% l)$, so things look consistent till now. \textcolor{red}{However, we have
% boundary conditions, and how many degrees of freedom do they take away?}. If
% we use hat functions as basis functions for both space and time basis
% dimensions Eqs.~(\ref{eq:matrix_space}--\ref{eq:matrix_time}), the integrals
% are simple to evaluate. We will discuss the case for $k_{lm}$ and $p_{lm}$,
% since it will give us an implicit time-stepping equation. Let
% \begin{equation}
% \psi_j(t) =
% \begin{cases}
% 0 & \text{if $t\in \left[t_{l-1},t_{l+1}\right]$} \\
% \dfrac{t - t_{l-1}}{t_l - t_{l-1}} & \text{if $t\in \left[t_{l-1},t_{l}\right]$} \\
% \dfrac{t_{l+1} - t}{t_{l+1} - t_{l}} & \text{if $t\in \left[t_{l},t_{l+1}\right]$}
% \end{cases}.
% \end{equation}
% Then, we immediately notice that the product of two functions (or their
% derivatives) would only be non-zero, if there are in the same element, i.e.
% $[t_{l-1}, t_{l+1}]$. Thus, Eq.~(\ref{eq:summation_expanded}) now reduces to
% (localized in a single element in time)
% \begin{equation}
% \label{eq:summation_reduced}
% - \sum\limits_{l=0}^{M}\sum\limits_{i=0}^{N} u_i^l\, m_{ij}\,
% \int\limits_{t_{l-1}}^{{t_{l+1}}} \mathrm{dt}\, \partial_t\tilde{\psi}_l(t)\partial_t\tilde{\psi}_m^0(t)
% + \sum\limits_{l=0}^{M}\sum\limits_{i=0}^{N} u_i^l\, a_{ik}\,
% \int\limits_{t_{l-1}}^{{t_{l+1}}} \mathrm{dt}\, \tilde{\psi}_l(t)\tilde{\psi}_m(t) = 0.
% \end{equation}
% The only surviving elements in $k_{lm}$ and $p_{lm}$
% \begin{eqnarray}
% \label{time-stepping}
%  k_{l-1, l} = h_0/6, \quad k_{ll} &=& 2 h_0/3, \quad k_{l, l+1} = h_0/6, \\
%  p_{l-1, l} = -1/h_0, \quad p_{ll} &=& 2/h_0, \quad p_{l, l+1} = -1/h_0,
% \end{eqnarray}
% where $h_0 = t_l - t_{l-1}$ is the unform spacing in time. Finally, plugging Eq.~(\ref{time-stepping}) into
% Eq.~(\ref{eq:summation_reduced}), gives us an implicit step in time, as
% \begin{eqnarray}
% - \sum\limits_{i=0}^{N} m_{ij}\, \left(u_i^{l-1} p_{l-1, l} + u_i^{l} p_{l, l} + u_i^{l+1} p_{l, l+1} \right)
% + \sum\limits_{i=0}^{N} a_{ij}\, \left(u_i^{l-1} k_{l-1, l} + u_i^{l} k_{l, l} + u_i^{l+1} k_{l, l+1} \right) = 0, \\
% M  \left( \dfrac{1}{h_0}\mathbf{u}^{l+1} -  \dfrac{2}{h_0}\mathbf{u}^{l} + \dfrac{1}{h_0}\mathbf{u}^{l-1} \right)
% = - A \left( \dfrac{h_0}{6}\mathbf{u}^{l+1} +  \dfrac{2 h_0}{3}\mathbf{u}^{l} + \dfrac{h_0}{6}\mathbf{u}^{l-1} \right)
% \end{eqnarray}
% Therfore, given $\mathbf{u}^r$ and $\mathbf{u}^{r-1}$, both vectors of length $N$, we can solve for $\mathbf{u}^{k+1}$, using the equation
% \begin{equation}
% M  \left(\mathbf{u}^{r+1} -  2\mathbf{u}^{r} + \mathbf{u}^{r-1} \right)
% = - A h_0^2 \left( \dfrac{1}{6}\mathbf{u}^{r+1} +  \dfrac{2}{3}\mathbf{u}^{r} + \dfrac{1}{6}\mathbf{u}^{r-1} \right),
% \end{equation}
% which can be cast in the form of $\tilde{A}\, x = b$, where

% \begin{eqnarray}
% \tilde{A} &=& M + \dfrac{h_0^2}{6}A, \\
% x &=& \mathbf{u}^{r+1}, \\
% b &=& 2\,\mathbf{u}^{r}\left(M - \dfrac{2 h_0^2}{3}A\right) - \mathbf{u}^{r-1} \left(M + \dfrac{h_0^2}{6}A\right)
% \end{eqnarray}

% %=========================================================================
% \section{Discretized Euler-Lagrange Equations Approach}
% %=========================================================================

% In the previous section, we started with the equations of motion followed by a
% spacetime discretization to arrive at the numerical implementation of the
% problem. However, we can instead start by discretizing the Lagrangian to
% arrive at a similar implicit time-stepping scheme, which is the focus of this
% section. For a discrete formulation of the action princple, we begin by
% discretizing in time ($t_n$), and approximating the discrete Lagrangian for a
% time interval $(t_n, t_{n+1})$
% \begin{equation}
% \label{discretized_lagrangian}
% L_d(q_{n}, q_{n+1}) \approx \int\limits_{t_n}^{t_{n+1}} L(q, \dot{q})\, \textrm{d}t,
% \end{equation}
% where $q_n = q(t_n)$ is a sequence of configuations in time. We can then
% approximate the action integral as
% \begin{equation}
% S_d(q_0, q_1, ..., q_N) = \sum\limits_{n=0}^{N-1} \,L_d (q_n, q_{n+1})
% \approx \int\limits_{0}^{T} L(q, \dot{q})\, \textrm{d}t.
% \end{equation}
% Now, if we deman that the variation $\delta S_d(q_0, q_1, ..., q_N) = 0$, for
% the true configuration at each time level, then the variation of the discrete
% action can be written as
% \begin{equation}
% \label{variation}
% \delta S_d(q_0, q_1, ..., q_N) = \sum\limits_{n=0}^{N-1} \left[D_1 \,L_d (q_n, q_{n+1})\cdot \delta q_n
% + D_2\,L_d (q_n, q_{n+1})\cdot \delta q_{n+1}\right].
% \end{equation}
% Using summation by parts on the second term in Eq.~(\ref{variation}), and the
% fact that the variation at the end points vanish $(\delta q_0 = \delta q_N = 0)$,
% we get
% \begin{equation}
% \delta S_d(q_0, q_1, ..., q_N) = \sum\limits_{n=1}^{N-1}  \left[ D_1 \,L_d (q_{n}, q_{n+1})\cdot \delta q_n
% + D_2\,L_d (q_{n-1}, q_{n}) \right] \cdot \delta q_{n},
% \end{equation}
% which gives us
% \begin{equation}
% \label{finalform_variational}
%  \left[ D_1 \,L_d (q_{n}, q_{n+1})\cdot \delta q_n
% + D_2\,L_d (q_{n-1}, q_{n}) \right] = 0, \quad n = 1, 2,...,N-1,
% \end{equation}
% which gives an implicit time-stepping scheme to go from $(q_{n-1}, q_n)$ to $(q_n, q_n+1)$ at each time level $n$. \\

\bibliography{notes}
\end{document}