%!TEX root = kPtx_paper.tex
\section*{Results}

Figure \ref{fig:ErrorMap} shows simulated excitation patterns and error maps for the reference spatial and k-space domain designs.
Both designs were performed with 16 parallel threads and the 64$\times$64$\times$48 grid size (3 mm isotropic resolution),
and the excitation patterns were evaluated against the target pattern on a 128$\times$128$\times$96 grid size (1.5 mm iso-resolution).
The k-space domain design used patch and inclusion widths of 4.
The calculated root-mean-squared errors (RMSEs) were 2.42\% (spatial domain) and 2.68\% (k-space domain),
respectively.
For both design methods, most of the errors appeared at the edges of the transition band,
and errors elsewhere were lower than 5\% of the target flip angle.
This indicates uniform inner volume excitation while maintaining the outer volume intact.
The parallelized k-space domain design required 2.9 seconds computation versus 31.8 seconds for the spatial domain method, a 91\% decrease.

\subsection*{k-Space Algorithm Parameters}
Figure \ref{fig:ComputationTime}a plots mean computation time versus number of parallel threads,
holding the patch and inclusion widths fixed at 4 cycles/FOV.
The computation time decreased rapidly with increasing thread number up to 12 threads,
and then plateaued, likely due to the overhead involved in initiating threads after that point.
Based on this result, the number of threads was held fixed at 16 for subsequent designs when off-resonance was not compensated.

\par Figure \ref{fig:ComputationTime}b plots mean computation time and RMSE with different patch widths.
The computation time decreased up to a patch width of 4 cycles/FOV (corresponding to $4^3 = 64$ simultaneously solved columns of $\bm{W}$)
and then increased sharply for larger patch widths.
RMSE decreased slowly as the patch width increased since the number of excitation trajectory points included in the calculation of weights for each
target point increases on average (and especially for target locations in the middle of each patch) as the patch width increases,
even when the inclusion width stays fixed.
Based on this result, a patch width of 4 cycles/FOV was used for subsequent designs.

\par Figure \ref{fig:ComputationTime}c plots mean computation time and RMSE with different inclusion widths.
The solid lines and dashed lines were obtained with patch widths of 4 and 8 cycles/FOV, respectively.
Computation time increased and error decreased with increasing inclusion width,
since more excitation trajectory points were included in each target location's calculation for increasing inclusion width,
corresponding to an increased $\bm{S}^H\bm{S}$ matrix size.
The RMSE was only slightly lower for a patch width of 8 versus 4 cycles/FOV,
but the computation time was much higher for 8 cycles/FOV, across all inclusion widths.
The knees in the curves occurred approximately at an inclusion width of 4 cycles/FOV,
so this value was used in subsequent designs.

\par Table \ref{fig:wsize} lists the size of the final matrix $\bm{W}$ in gigabytes,
versus inclusion width.
As inclusion width increases, more excitation trajectory points are used in the solution of the weights for each target location,
until the entire trajectory is used for each location (Inclusion width = $\infty$ in the table),
corresponding to a full solution.
With the inclusion width of 4 cycles/FOV used here,
the matrix size was 98\% smaller than that of a full solution.

\subsection*{L-Curves}
Figure \ref{fig:LCurves} plots flip angle RMSE versus RF RMS amplitude for the spatial domain method
and different configurations of the k-space domain method.
The spatial domain method achieves the best overall tradeoff between error and RMS amplitude (dashed black curve),
which is matched by the k-space domain method when all target locations are solved simultaneously with
all excitation trajectory points included and exact $\bm{S}^H\bm{S}$ matrix construction (orange curve).
When the inclusion and patch widths are limited to 4 cycles/FOV but the $\bm{S}^H\bm{S}$
matrices are still constructed exactly, there is an increase in error and RMS amplitude (solid blue curve).
However, a larger penalty is incurred by interpolating the entries of the $\bm{S}^H\bm{S}$ matrices (dashed orange curve)
than by limiting the inclusion and patch widths.
Combining interpolation of the $\bm{S}^H\bm{S}$ matrix entries and patch and inclusion widths of 4 cycles/FOV yield
the dashed blue curve, which has a higher error and RF amplitude than when interpolation or small patch and inclusion widths are
used alone.
Overall, these results and the results in Figure \ref{fig:ComputationTime} and Table \ref{fig:wsize}
show that the k-space domain method allows a tradeoff between computation time and memory usage versus
excitation error and RMS RF amplitude.
The dots on the spatial domain and k-space domain curves indicate the knees of of the curves corresponding to
the $\lambda$ values used for the designs in Figures \ref{fig:ErrorMap}, \ref{fig:ComputationTime}, \ref{fig:kspace_PTX_Acceleration}, and \ref{fig:kspace_PTX_B0}.
Note that the RMSE's in Figure \ref{fig:LCurves} are slightly lower for the spatial domain designs
and slightly higher for the k-space domain designs compared to other figures,
because the flip angle errors in Figure \ref{fig:LCurves} were calculated using
spatial domain non-uniform fast Fourier transforms instead of Bloch equation simulations,
to provide a more direct measure of the design error.

\subsection*{Gibbs Ringing}
Figure \ref{fig:GibbsRing}a shows slices of the excitation error pattern produced by pulses designed by the spatial domain method
on a 32$\times$32$\times$24 grid, which were Bloch-simulated on the original 128$\times$128$\times$96 grid.
There is significant Gibbs ringing in the pattern (indicated by the red arrows),
and the pulses incur a higher RMSE (4.64\%) than pulses designed using either the spatial domain method with a finer 64$\times$64$\times$48 grid
(2.66\%; Figure \ref{fig:GibbsRing}b) or the k-space domain method with a 32$\times$32$\times$24 grid (3.00\%; Figure \ref{fig:GibbsRing}c).
Gibbs ringing is not apparent in either the 64$\times$64$\times$48 spatial domain error pattern or the 32$\times$32$\times$24 k-space domain error pattern.
The RMS RF amplitudes of the low resolution designs were both 0.005.
Note that the error of the 64$\times$64$\times$48 spatial domain design is slightly higher than designs presented in other figures
due to the lower-resolution excitation trajectory.
From a spatial domain point of view,
the Gibbs ringing in the low-resolution spatial domain design was caused by the design's inability to observe and limit the ringing in the low-resolution design grid.
From a k-space domain point of view, the Gibbs ringing in
Figure \ref{fig:GibbsRing}a was due to implicit circulant end conditions at the edges of excitation k-space in the low-resolution
spatial domain design,
which led RF samples at one edge of k-space to wrap-around and affect target locations at the opposite edge of k-space.
This effect is mitigated using a high-resolution spatial domain design grid, as illustrated in Figure \ref{fig:GibbsRing}b.
However, even for a low-resolution k-space domain design there is no wrap-around effect in excitation k-space
because the trajectory points that are incorporated in the weights solution for each patch of target locations are explicitly specified to be those in the immediate vicinity of
the target points, without circulant end conditions.
%Particularly, for the instance problems whose patches are at the edges of the k-space FOV, the excitation trajectory points at the other ends of the k-space FOV will not be incorporated into the design.

%Due to implicit circulant end conditions in k-space for the spatial domain design, t

\subsection*{Excitation k-Space Undersampling}
Figure \ref{fig:kspace_PTX_Acceleration} compares excitation error patterns produced by spatial domain-designed pulses (top row)
and k-space domain-designed pulses (middle row),
for different trajectory reduction factors which resulted in the SPINS trajectories plotted in the third row.
For each design the k-space domain-designed pulses had higher error,
but error increased smoothly with increasing reduction factor, as it did for the spatial domain designs.

\subsection*{Off-Resonance}
Figure \ref{fig:kspace_PTX_B0}a shows the off-resonance maps containing a Gaussian distortion which was centered above the frontal sinus,
and scaled to peak amplitudes of 0, 200, and 400 Hz for the pulse designs and Bloch simulations.
Figure \ref{fig:kspace_PTX_B0}b shows excitation error patterns and RMSEs for spatial domain designs with off-resonance compensation,
and k-space domain designs without and with off-resonance compensation.
Without off-resonance compensation,
the k-space domain-designed pulses produced large ($>$ 10\% of $M_0$) excitation errors both inside and outside the target ellipse.
When the off-resonance map was incorporated in the spatial domain and k-space domain designs,
the distortion was nearly fully corrected when it had a peak amplitude of 200 Hz.
When the map was scaled to a peak of 400 Hz, some large errors remained, with the k-space domain-designed pulses achieving slightly lower RMSE.
%It was also able to provide excitation pattern comparable to spatial domain design with 400 Hz maximum off-resonance. The k-space domain design has a weaker performance with higher off-resonance due to the fact that the time segmentation approximation in Ref \cite{fessler2005toeplitz} was meant to approximate forward models from RF to excitation patterns and does not serve as an accurate approximation to the backward model as we need in the k-space domain design.


\chapter{Conclusions}
\label{ch:end}

We studied the effect of electrical noise on the identification and
reconstruction of output pulses in DarkSide20k silicon photomultipliers (SiPM)
Tiles in \autoref{ch:snr}, \ref{ch:timeres} and~\ref{ch:rate}, and measured the
correlated noise of a Tile in \autoref{ch:anal}. In the following paragraphs we
summarize and comment the results from all chapters.

The main takeaway from \autoref{ch:snr} is in \autoref{fig:changebs}. The
maximum signal to noise ratio (SNR) that can be reached with filtering on that
data is~19, shown on the left panel with the cross correlation filter and
precise baseline calculation. However the constraints of the available
resources on the digitizers may turn out to impose a moving average filter with
imprecise baseline, lowering the SNR to~13, as shown in the central panel. So
the SNR at the digitizer stage can be as low as \SI{65}\% of the optimal one.

In the DarkSide collaboration it was proposed to use an ``autoregressive moving
average'' (ARMA) filter to implement a long cross correlation filter in the
digitizers. The ARMA combines linearly two filters: 1)~a cross correlation with
a template only for the peak of the pulse, and 2)~an autoregressive filter, run
on the \emph{temporally reversed} waveform. This reproduces a cross correlation
with the peak template plus an exponentially decaying tail. Although the
asymptotical complexity of the algorithm is excellent on paper, the actual
amount of logic resources necessary for the implementation on FPGA may exceed
the available ones, mainly due to the necessity of keeping a buffer for the
temporal reversal, and to glue appropriately the reversed temporal slices with
dynamical boundary conditions or partially overlapping slices. From
\autoref{fig:changebs} it is clear that a simple moving average can achieve
performances comparable to a cross correlation filter, thus it may not be
necessary to devise a way to implement the cross correlation filter on the
digitizers.

In \autoref{ch:timeres} there are various things to learn:

\begin{itemize}

    \item The temporal resolution diverges below a certain SNR, giving a
    somewhat rigid bound on the working point, and the behavior at low SNR
    heavily depends on the noise spectrum, \autoref{fig:rescomp}. The
    DarkSide20k simulation currently implements only white noise.

    \item For what concerns temporal resolution, it is sufficient to extract
    \SI{1}{\micro s} of waveform per pulse from the digitizers,
    \autoref{fig:windowtempres}.

    \item Downsampling from \SI{125}{MSa/s} to \SI{62.5}{MSa/s} does not
    deteriorate the temporal resolution, \autoref{fig:tempresdowns} and
    \autoref{tab:filtsnrdowns}. If a compromise with the data rate is
    necessary, \SI{31.2}{MSa/s} might still be good enough.

    \item Upsampling is not necessary, \autoref{fig:rescomp}.

\end{itemize}

The temporal resolution required by the specifications of DarkSide20k is
\SI{10}{ns} \cite[30]{aalseth2018}. A worse resolution would decrease the
selective power of pulse shape discrimination (PSD). With the noise spectrum of
the Proto0 prototype, we find that the temporal resolution reaches \SI{10}{ns}
at pre-filter SNR 2.6, to be compared with the SNR observed in the Proto0 data
of at least 3.3, see \autoref{fig:rescomp}.

Chapter~\ref{ch:rate} has its own summary in \autoref{sec:rateconcl}. In
general the formula for the fake rate is sufficiently precise, but there is an
exception with higher rate than expected that should be investigated better,
see \autoref{fig:fakerate}. With the tested filter, which matches the one
mentioned above for the $\mathrm{SNR} = 13$ figure, the fake rate is
\SI{10}{cps} with the threshold set at 5 filtered noise standard deviations
(including baseline subtraction), to be compared to the maximum total primary
noise rate required, \SI{250}{cps/PDM} \cite[30]{aalseth2018}.

The chapter on correlated noise contains more material, so it includes a rather
long discussion of the results in \autoref{sec:analconcl}. To summarize
further:

\begin{itemize}

    \item Compared to the Fondazione Bruno Kessler (FBK) Tiles, the analyzed
    LFoundry Tile has similar direct cross talk (DiCT) and much less
    afterpulsing (AP).

    \item The afterpulse models should be studied and validated better.

    \item It should be determined whether Tile~21 enters a nonlinear regime
    between \SI{7.5}{VoV} and \SI{9.5}{VoV}, or if the \SI{9.5}{VoV} data is
    mislabeled.

    \item Tile~21 should be analyzed with charge spectrum methods and the
    results compared to ours.

    \item The SiPM simulation must be consistent with the model used in the
    analysis of real data.

\end{itemize}

We put upper bounds on the dark count rate (DCR), respectively \SI{50}{cps},
\SI{170}{cps}, and~\SI{120}{cps} at overvoltages \SI{5.5}V, \SI{7.5}V,
and~\SI{9.5}V, to be compared with the required upper limit \SI{250}{cps}. We
give upper bounds for AP probabilities of \SI{2.5}\%, \SI{3.5}\% and
\SI{6.5}\%, and estimate DiCT probabilities \SI{20}\%, \SI{30}\% and~\SI{50}\%.
These are the probabilities of said noises being generated by a \SI1{PE} pulse.
The DarkSide20k specifications require the sum of correlated noise
probabilities to be less than \SI{60}\% \cite[30]{aalseth2018} in order to
avoid performance degradation, mainly dynamic range reduction on ionization
signals, where there is a lot of pile-up. Furthermore, some safety margin
should be considered for the aging of the SiPMs, which is not well studied, in
case the correlated noise probability turned out to increase with time. Recall
that, for a correlated noise probability $p$, the ratio of total pulses to
primary pulses is $1/(1-p)$, which diverges quite fast as $p$ goes to~1.
\cite[8]{gola2019} mention that in practice the maximum manageable probability
is \SI{50}\%.

\marginpar{Explain the stuff from Simone in the TF2 report.}


%-----------------------------------------------------------------------------
%
%               Template for sigplanconf LaTeX Class
%
% Name:         sigplanconf-template.tex
%
% Purpose:      A template for sigplanconf.cls, which is a LaTeX 2e class
%               file for SIGPLAN conference proceedings.
%
% Guide:        Refer to "Author's Guide to the ACM SIGPLAN Class,"
%               sigplanconf-guide.pdf
%
% Author:       Paul C. Anagnostopoulos
%               Windfall Software
%               978 371-2316
%               paul@windfall.com
%
% Created:      15 February 2005
%
%-----------------------------------------------------------------------------

\documentclass[nocopyrightspace,blockstyle,numbers]{sigplanconf}

% The following \documentclass options may be useful:

% preprint      Remove this option only once the paper is in final form.
% 10pt          To set in 10-point type instead of 9-point.
% 11pt          To set in 11-point type instead of 9-point.
% numbers       To obtain numeric citation style instead of author/year.

\usepackage{amsmath}
\usepackage[utf8]{inputenc}

\newcommand{\cL}{{\cal L}}

\begin{document}

\special{papersize=8.5in,11in}
\setlength{\pdfpageheight}{\paperheight}
\setlength{\pdfpagewidth}{\paperwidth}

\conferenceinfo{CONF 'yy}{Month d--d, 20yy, City, ST, Country}
\copyrightyear{20yy}
\copyrightdata{978-1-nnnn-nnnn-n/yy/mm}
\copyrightdoi{nnnnnnn.nnnnnnn}

% Uncomment the publication rights you want to use.
%\publicationrights{transferred}
%\publicationrights{licensed}     % this is the default
%\publicationrights{author-pays}

%\titlebanner{banner above paper title}        % These are ignored unless
%\preprintfooter{short description of paper}   % 'preprint' option specified.

\title{Coq's Prolog and application to defining semi-automatic tactics}
%\subtitle{Subtitle Text, if any}

\authorinfo{Théo Zimmermann \and Hugo Herbelin}
           {IRIF, Université Paris Diderot, Université Sorbonne Paris-Cité\\ $\Pi R ^2$, INRIA}
           {theo.zimmermann@univ-paris-diderot.fr / hugo.herbelin@inria.fr}
%\authorinfo{Name1}
%           {Affiliation1}
%           {Email1}
%\authorinfo{Name2\and Name3}
%           {Affiliation2/3}
%           {Email2/3}

\maketitle

\begin{abstract}
  We report on a work-in-progress to re-implement Coq's \texttt{apply}
  tactic in order to embed some form of simple automation. We design
  it in a declarative way, relying on \texttt{typeclasses eauto}, a tactic
  which gives access to the proof-search mechanism behind type classes.
  We qualify this mechanism of ``Coq's Prolog'' and describe it in a
  generic way and explain how it can be used to support the
  construction of automatic and semi-automatic tactics.
\end{abstract}

%\category{CR-number}{subcategory}{third-level}

% general terms are not compulsory anymore,
% you may leave them out
%\terms
%term1, term2

\keywords
apply, automation, Coq, proof assistant, reflection,
type theory, views.

\section{Introduction}

Since version 8.2 \cite{coq86manual}, Coq has included a type class
mechanism which can intervene during type-inference. The resolution
phase of this mechanism is also accessible via the user-side tactic
\texttt{typeclasses eauto}. In fact \texttt{typeclasses eauto}
implements a general Prolog-like proof-search mechanism that can be
instrumented to do much more. While by default, it uses the specific
\texttt{typeclass\_instances} hint database, one can use it with
alternative hint databases. In this case, despite the name of the
tactic, it has nothing to do with the type class mechanism anymore
(apart from the underlying implementation). It can be viewed as a
re-implementation of the former \texttt{eauto} tactic, dating back
from Coq version 5.10.

Many partially automated tactics have already been built upon Coq's type
class mechanism. One example is the re-implementation of the \texttt{rewrite}
tactic \cite{sozeau2010new}, which relies on the mechanism in
a second phase of constraint resolution. Another is our \texttt{transfer}
library \cite{zimmermann}. We argue that both of these applications (and
many others) abuse the type class mechanism, by polluting the
\texttt{typeclass\_\\instances} hint database, when they should instead
define their own disjoint hint databases and rely on the underlying
Prolog-like proof-search mechanism that is made accessible via the
\texttt{typeclasses eauto} tactic.

We demonstrate the usefulness of this ``Coq's Prolog'' with a small
re-implementation of the \texttt{apply} tactic. This tactic is parame\-trized
by a set of ``views'' à la SSReflect \cite{gonthier} which it is
able to apply automatically. It also demonstrates a new way of instrumenting
\texttt{typeclasses eauto} to progress on a goal instead of solving it
(``forward mode'', proposed in particular by Michael Soegtrop
on the Coq-Club mailing list).

\section{Presentation of Coq's Prolog}

The \texttt{typeclasses eauto} tactic is documented in the chapter on
type classes of Coq's user manual \cite[Chapter~20]{coq86manual}.
Like \texttt{eauto}, it is possible to specify on which hint database it
should operate using the \texttt{with myhintdb} clause.
By default, the proof-search depth is unbounded and the traversing
strategy is depth-first. It is also possible to limit the proof-search
depth, and an alternative iterative-deepening traversing strategy exists.

Most hints are lemmas which can be read as Prolog Horn clauses.
For instance:

\begin{verbatim}
Lemma or_introl : forall A B, A -> A \/ B.
\end{verbatim}
can be read as the clause\footnote{We introduce the Prolog predicate
  \texttt{prove} to denote the idea that the hints are used when looking
  for a proof of a statement. When using \text{typeclasses eauto} to
  solve type class goals, there is always a head-constant (the type class)
  which can serve the same role as \texttt{prove} but when using it on
  non-type-class goals, it is not necessarily the case.}:

\begin{verbatim}
prove(or(A,B)) :- prove(A).
\end{verbatim}
Such hints can be introduced with the \texttt{Hint Resolve} command:

\begin{verbatim}
Hint Resolve or_introl : myhintdb.
\end{verbatim}

Some more complex hints can be introduced thanks to the
\texttt{Hint Extern} command. One very special application of
such external hints is to put some sub-goals ``on the shelf'':

\begin{verbatim}
Hint Extern 0 (solveLater _) =>
  unfold solveLater; shelve : myhintdb.
\end{verbatim}

The shelf is a special place in the Coq proof engine which is
normally reserved to sub-goals that will be solved by solving other
sub-goals that depend on them. It can be instrumented to temporarily
store away some sub-goals that we want to keep for later (at which
time they will be ``unshelved'').
The ``unshelve'' tactical can be used to put back in the list of
sub-goals to solve those which had been shelved by the tactic it is
applied on.
Thus, a combination of the previous external hint plus a call to
\texttt{unshelve typeclasses eauto with myhintdb} is a way to use
this Prolog-like proof-search mechanism in forward mode, instead of
the normal solving mode.

Finally, a control operator on the search space is available. It is
not comparable to Prolog's cut operator in that it allows to specify
regular expressions of search-paths which should be cut but does
not restrict backtracking. It is still
limited in that regular expressions can only talk about hints
declared with \texttt{Hint Resolve} and not \texttt{Hint Extern}.
Additionally, there is no negative expressions and even if regular
expressions are known to be closed under complement, the construction
requires to know all the alphabet, which is not the case here since
new hints can be added dynamically. We can expect to see improvements
in control operators for \texttt{typeclasses eauto} in the next versions
of Coq. Here is an example\footnote{Be careful when using it that the
precedence levels are not what one would expect. They should get fixed
in Coq 8.7 but, in the meantime, the best way of writing
forward-compatible code is to parenthesize everything.}
of using this control operator to forbid any proof using \texttt{or\_introl} twice:

\begin{verbatim}
Hint Cut [(_*) or_introl (_*) or_introl] : myhintdb.
\end{verbatim}

\section{An \texttt{apply} tactic with views}

Our work is part of a larger effort to make the life of mathematicians
using Coq easier. The specific issue we address here is to design an
extension of the \texttt{apply} tactic which embeds some bits of
trivial reasoning, such as reasoning modulo symmetry of equations
(identifying \texttt{u = t} and \texttt{t = u}).
There is already a little bit of hard-coded trivial reasoning in the
current implementation (automatic decomposition of single-constructor
inductive types): for instance it supports applying a theorem
of the form \verb|A -> B /\ C| to a goal of the form \texttt{B}.

SSReflect defines views which are basically small theorems of
the form \texttt{A -> B} where \texttt{A} is a different way of
viewing \texttt{B}. Our re-implementation of \texttt{apply} tries to
solve the problem we were describing by allowing the user to
parametrize it with such ``views''. Without any view, it is less
powerful than the current implementation (because of the absence of
decomposition of single-constructor inductive types). With many
views, it can be much more powerful. The various levels of
parametrization could reveal especially useful for teaching (from a
level where everything must be done by hand, to a level where most
details are handled automatically).

Given the inspiration source we described in the previous paragraph,
an obvious application of our work will be to support automatic
insertion of views in the context of small scale reflection. In
particular, it will be possible to simply register the reflection
lemma:

\begin{verbatim}
Lemma andP : forall b1 b2 : bool, reflect
  (b1 = true /\ b2 = true) ((b1 && b2) = true).
\end{verbatim}
in a special hint database and \texttt{apply} will know about it
and use it when necessary.

There are two main ideas in our implementation. The first is that
we are launching a proof search to prove that the theorem
we wish to apply implies the current goal:

\begin{verbatim}
?prove : arrow theorem goal
\end{verbatim}
where \texttt{arrow} is a relation that is definitionally equal to Coq's implication.
We use it because otherwise the proof-search mechanism would introduce
the premise \texttt{theorem} in the proof context and then try
to prove \texttt{goal} with it, and this is not what we want.

The second is that although we cannot actually prove this implication
most of the time, we can provide an incomplete proof and let the
user fill the holes (this is after all the principle of
\texttt{apply}):

\begin{verbatim}
solveLater A -> arrow B C -> arrow (A -> B) C
\end{verbatim}
where \texttt{solveLater} is a dummy constant introduced to call the
external hint we described earlier, which will shelve the sub-goal
\texttt{A} until the full search succeeds and then unshelve it for the
user to prove. In fact, because the conclusion \texttt{B} might be
dependent on the premise \texttt{A}, we rather write:

\begin{verbatim}
forall (t : T), arrow (U t) V ->
  arrow (forall x : T, U x) V
\end{verbatim}
and then shelve \texttt{t}.
A few other generic rules are present, one of them allowing to
go under quantifiers and, of course, the reflexivity of arrow:

\begin{verbatim}
(forall x : A, arrow (f x) (g x)) ->
  arrow (forall x : A, f x) (forall x : A, g x)

arrow T T
\end{verbatim}

With these rules only, we can reproduce the behavior of
\texttt{apply}, except for the built-in handling of
single-constructor inductive types.
Actually already at that point, we do not reproduce the
exact same behavior because our implementation allows
applying a theorem \texttt{forall x y, P x y}
to a goal \texttt{forall x, P x 0}.

With this basic infrastructure in place, we can start
adding views, such as:

\begin{verbatim}
arrow P P' -> arrow (P /\ Q) P'
arrow Q Q' -> arrow (P /\ Q) Q'
\end{verbatim}
which will be useful in emulating the current behavior
of \texttt{apply}. We can also add a very simple rule to
handle symmetry (one could think of more complex and
powerful ways):

\begin{verbatim}
arrow (u = t) (t = u)
\end{verbatim}
and finally, we register some more generic rules like:

\begin{verbatim}
reflect P b -> arrow P (b = true)
reflect b P -> arrow (b = true) P
\end{verbatim}
so that people can easily extend the hint database with
existing reflection lemmas such as the one seen above.

\section{Conclusion}

This work is very preliminary but shows an interesting path
to removing some of the hindrances there are in using
both SSReflect and vanilla Coq.
It is also a demonstration of the power of
\texttt{typeclasses eauto}, even when not working with
type classes.
Sometimes, the type class mechanism has been used while what
was really wanted was this Coq's Prolog that it gives access to.
One such example is the implementation of \texttt{rewrite}:
it probably could and should be based on a specific hint
database instead of the \texttt{typeclass\_instances} database.

We are planning first to continue to test and improve our
re-implementation of \texttt{apply}. We would in particular like
to have views for applying a theorem modulo commutativity /
associativity. We are also planning to merge this work with our
previous work on applying theorems modulo isomorphisms
\cite{zimmermann}. A lot of ideas from this earlier work were
reused here and the two implementations could very likely be
combined together.

%\appendix
%\section{Appendix Title}

%This is the text of the appendix, if you need one.

%\acks

%Acknowledgments, if needed.

% We recommend abbrvnat bibliography style.

\bibliography{coq-prolog}
\bibliographystyle{abbrvnat}

% The bibliography should be embedded for final submission.

%\begin{thebibliography}{}
%\softraggedright

%\bibitem[Smith et~al.(2009)Smith, Jones]{smith02}
%P. Q. Smith, and X. Y. Jones. ...reference text...

%\end{thebibliography}

\end{document}


% BEGIN LICENSE BLOCK
% Version: CMPL 1.1
%
% The contents of this file are subject to the Cisco-style Mozilla Public
% License Version 1.1 (the "License"); you may not use this file except
% in compliance with the License.  You may obtain a copy of the License
% at www.eclipse-clp.org/license.
%
% Software distributed under the License is distributed on an "AS IS"
% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
% the License for the specific language governing rights and limitations
% under the License.
%
% The Original Code is  The ECLiPSe Constraint Logic Programming System.
% The Initial Developer of the Original Code is  Cisco Systems, Inc.
% Portions created by the Initial Developer are
% Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
%
% Contributor(s):
%
% END LICENSE BLOCK

%----------------------------------------------------------------------
\chapter{The Integer Sets Library}
\label{icsets}
\index{library!ic_sets|(}
%HEVEA\cutdef[1]{section}
%----------------------------------------------------------------------

%----------------------------------------------------------------------
\section{Why Sets}
%----------------------------------------------------------------------

\index{finite sets}
The {\em ic_sets} library is a solver for constraints over the domain
of finite sets of integers.
Modelling with sets is useful for problems where one is not
interested in each item as a specific individual, but in a
collection of item where no specific distinction is made and thus
where symmetries among the element values need to be avoided.

%----------------------------------------------------------------------
\section{Finite Sets of Integers}
%----------------------------------------------------------------------

In the context of the {\em ic_sets} library, (ground) integer sets are
simply sorted, duplicate-free lists of integers e.g.
\begin{quote}\begin{verbatim}
SetOfThree = [1,3,7]
EmptySet = []
\end{verbatim}\end{quote}
Lists which contain non-integers, are unsorted or contain duplicates,
are not sets in the sense of this library.

%----------------------------------------------------------------------
\section{Set Variables}
\index{set variable}
%----------------------------------------------------------------------

%\subsection{Declaring}
Set variables are variables which can eventually take a ground integer
set as their value.  They are characterized by a lower bound (the set
of elements that are definitely in the set) and an upper bound (the
set of elements that may be in the set).  A set variable can be
declared as follows:
\begin{quote}\begin{verbatim}
SetVar :: []..[1,2,3,4,5,6,7]
\end{verbatim}\end{quote}
%If the lower bound is the empty set (like in this case) this can be written as
%\begin{verbatim}
%        SetVar subset [1,2,3,4,5,6,7]
%\end{verbatim}
If the lower bound is the empty set and the upper bound is a set of
consecutive integers, one can also declare it like
\begin{quote}\begin{verbatim}
intset(SetVar, 1, 7)
\end{verbatim}\end{quote}
which is equivalent to the above.
\quickref{Declaring Set Variables}{
\begin{description}
\item[\biptxtrefni{?Set :: ++Lwb..++Upb}{::/2!ic_sets}{../bips/lib/ic_sets/NN-2.html}]
\index{::/2@\texttt{::/2}!ic_sets} Set is an integer set within the given bounds
\item[\biptxtref{intset(?Set, +Min, +Max)}{intset/3}{../bips/lib/ic_sets/intset-3.html}]
         Set is a set containing numbers between Min and Max
\item[\biptxtref{intsets(?Sets, ?N, +Min, +Max)}{intsets/4}{../bips/lib/ic_sets/intsets-4.html}]
         Sets is a list of N sets containing numbers between Min and Max
\end{description}
}

The system prints set variables in a particular way, for instance:
\begin{quote}\begin{verbatim}
?- lib(ic_sets).
?- X :: [2,3]..[1,2,3,4].
X = X{[2, 3] \/ ([] .. [1, 4]) : _308{[2 .. 4]}}
\end{verbatim}\end{quote}
The curly brackets contain the description of the current domain
of the set variable in the form of
\begin{enumerate}
\item the lower bound of the set (values which definitely are in the set)
\item the union symbol \verb.\/.
\item the set of optional values (which may or may not be in the set)
\item a colon
\item a finite domain variable indicating the admissible cardinality for the set
\end{enumerate}

%----------------------------------------------------------------------
\section{Constraints}
%----------------------------------------------------------------------

\index{membership constraint}
\index{cardinality constraint}
The constraints that {\em ic_sets} implements are the usual relations
over sets.
The membership (in/2, notin/2) and cardinality constraints
(\#/2) establish
relationships between set variables and integer variables:
\quickref{Membership and Cardinality Constraints}{
\begin{description}
\item[\biptxtrefni{?X in ?Set}{in/2!ic_sets}{../bips/lib/ic_sets/in-2.html}]
         \index{in/2@\texttt{in/2}!ic_sets} The integer X is member of the integer set Set
\item[\biptxtrefni{?X notin ?Set}{notin/2!ic_sets}{../bips/lib/ic_sets/notin-2.html}]
         \index{notin/2@\texttt{notin/2}!ic_sets} The integer X is not a member of the integer set Set
%\item[\biptxtref{membership_booleans(?Set, ?BoolArr)}{membership_booleans/2!ic_sets}{../bips/lib/ic_sets/membership_booleans-2.html}]
%         BoolArr is an array of booleans describing Set
\item[\biptxtrefni{\#(?Set, ?Card)}{\#/2!ic_sets}{../bips/lib/ic_sets/H-2.html}]
         \index{\#/2@\texttt{\#/2}!ic_sets} Card is the cardinality of the integer set Set
\end{description}
}
\begin{quote}\begin{verbatim}
?- X ::[]..[1, 2, 3], 2 in X, 3 in X, #(X, 2).
X = [2, 3]
Yes (0.01s cpu)

?- X :: []..[1, 2, 3, 4], 3 in X, 4 notin X.
X = X{[3] \/ ([] .. [1, 2]) : _2161{1 .. 3}}
Yes (0.00s cpu)
\end{verbatim}\end{quote}
Possible constraints between two sets are equality, inclusion/subset
and disjointness:
\index{inclusion constraint}
\index{disjointness constraint}
\index{subset constraint}
\begin{quote}\begin{verbatim}
?- X subset [1, 2, 3, 4].
X = X{([] .. [1, 2, 3, 4]) : _2139{0 .. 4}}
Yes (0.00s cpu)

?- X :: []..[1, 2, 3, 4], Y :: []..[3, 4, 5, 6], X subset Y.
X = X{([] .. [3, 4]) : _2176{0 .. 2}}
Y = Y{([] .. [3, 4, 5, 6]) : _2367{0 .. 4}}
There are 4 delayed goals.
Yes (0.00s cpu)

?- X :: [2] .. [1, 2, 3, 4], Y :: [3] .. [1, 2, 3, 4], X disjoint Y.
X = X{[2] \/ ([] .. [1, 4]) : _2118{1 .. 3}}
Y = Y{[3] \/ ([] .. [1, 4]) : _2213{1 .. 3}}
There are 2 delayed goals.
Yes (0.00s cpu)
\end{verbatim}\end{quote}
\quickref{Basic Set Relations}{
\begin{description}
\item[\biptxtrefni{?Set1 sameset ?Set2}{sameset/2!ic_sets}{../bips/lib/ic_sets/sameset-2.html}]
         \index{sameset/2@\texttt{sameset/2}!ic_sets} The sets Set1 and Set2 are equal
\item[\biptxtrefni{?Set1 disjoint ?Set2}{disjoint/2!ic_sets}{../bips/lib/ic_sets/disjoint-2.html}]
         \index{disjoint/2@\texttt{disjoint/2}!ic_sets} The integer sets Set1 and Set2 are disjoint
\item[\biptxtrefni{?Set1 includes ?Set2}{includes/2!ic_sets}{../bips/lib/ic_sets/includes-2.html}]
         \index{includes/2@\texttt{includes/2}!ic_sets} Set1 includes (is a superset) of the integer set Set2
\item[\biptxtrefni{?Set1 subset ?Set2}{subset/2!ic_sets}{../bips/lib/ic_sets/subset-2.html}]
         \index{subset/2@\texttt{subset/2}!ic_sets} Set1 is a (non-strict) subset of the integer set Set2
\item[\biptxtrefni{intersection(?Set1, ?Set2, ?Set3)}{intersection/3!ic_sets}{../bips/lib/ic_sets/intersection-3.html}]
         \index{intersection/3@\texttt{intersection/3}!ic_sets} Set3 is the intersection of the integer sets Set1 and Set2
\item[\biptxtrefni{union(?Set1, ?Set2, ?Set3)}{union/3!ic_sets}{../bips/lib/ic_sets/union-3.html}]
         \index{union/3@\texttt{union/3}!ic_sets} Set3 is the union of the integer sets Set1 and Set2
\item[\biptxtrefni{difference(?Set1, ?Set2, ?Set3)}{difference/3!ic_sets}{../bips/lib/ic_sets/difference-3.html}]
         Set3 is the difference of the integer sets Set1 and Set2
\item[\biptxtrefni{symdiff(?Set1, ?Set2, ?Set3)}{symdiff/3!ic_sets}{../bips/lib/ic_sets/symdiff-3.html}]
         \index{symdiff/3@\texttt{symdiff/3}!ic_sets} Set3 is the symmetric difference of the integer sets Set1 and Set2
\end{description}
}
Possible constraints between three sets are for example
intersection, union, difference and symmetric difference.
For example:
\begin{quote}\begin{verbatim}
?- X :: [2, 3] .. [1, 2, 3, 4],
   Y :: [3, 4] .. [3, 4, 5, 6],
   ic_sets : intersection(X, Y, Z).
X = X{[2, 3] \/ ([] .. [1, 4]) : _2127{2 .. 4}}
Y = Y{[3, 4] \/ ([] .. [5, 6]) : _2222{2 .. 4}}
Z = Z{[3] \/ ([] .. [4]) : _2302{[1, 2]}}
There are 6 delayed goals.
Yes (0.00s cpu)
\end{verbatim}\end{quote}
\Note{Note that we needed to qualify the intersection/3 constraint with
the {\em ic_sets} module prefix because of a name conflict with a predicate from
the {\em lists} library of the same name.}
\Note{Note the lack of a complement constraint: this is because the complement
of a finite set is infinite and cannot be represented. Complements can be
modelled using an explicit universal set and a difference constraint.}

\ignore{
\subsection{Domain Access}

\begin{description}
\item[\biptxtref{potential_members(?Set, -List)}{potential_members/2}{../bips/lib/ic_sets/potential_members-2.html}]
         List is the list of elements of whose membership in Set is currently uncertain
\item[\biptxtref{set_range(?Set, -Lwb, -Upb)}{set_range/3}{../bips/lib/ic_sets/set_range-3.html}]
         Lwb and Upb are the current lower and upper bounds on Set
\end{description}
}

Finally, there are a number of n-ary constraints that apply to lists of sets:
disjointness, union and intersection. For example:
\quickref{N-ary Set Relations}{
\begin{description}
\item[\biptxtref{all_disjoint(+Sets)}{all_disjoint/1}{../bips/lib/ic_sets/all_disjoint-1.html}]
         Sets is a list of integers sets which are all disjoint
\item[\biptxtref{all_union(+Sets, ?SetUnion)}{all_union/2}{../bips/lib/ic_sets/all_union-2.html}]
         SetUnion is the union of all the sets in the list Sets
\item[\biptxtref{all_intersection(+Sets, ?SetIntersection)}{all_intersection/2}{../bips/lib/ic_sets/all_intersection-2.html}]
         SetIntersection is the intersection of all the sets in the list Sets
\end{description}
}
\begin{quote}\begin{verbatim}
?- intsets(Sets, 5, 1, 5), all_intersection(Sets, Common).
Sets = [_2079{([] .. [1, 2, 3, 4, 5]) : _2055{0 .. 5}}, ... ]
Common = Common{([] .. [1, 2, 3, 4, 5]) : _3083{0 .. 5}}
There are 24 delayed goals.
Yes (0.00s cpu)
\end{verbatim}\end{quote}

%----------------------------------------------------------------------
%\section{Set Expressions}
%----------------------------------------------------------------------

In most positions where a set or set variable is expected one can also
use a set expression. A set expression is composed from ground sets
(integer lists), set variables, and the following set operators:
\begin{quote}\begin{verbatim}
Set1 /\ Set2       % intersection
Set1 \/ Set2       % union
Set1 \ Set2        % difference
\end{verbatim}\end{quote}
When such set expressions occur, they are translated into auxiliary
\bipref{intersection/3}{../bips/lib/ic_sets/intersection-3.html},
\bipref{union/3}{../bips/lib/ic_sets/union-3.html} and
\bipref{difference/3}{../bips/lib/ic_sets/difference-3.html}
constraints, respectively.

%----------------------------------------------------------------------
\section{Search Support}
%----------------------------------------------------------------------

The
\bipref{insetdomain/4}{../bips/lib/ic_sets/insetdomain-4.html}
predicate can be used to enumerate all ground instantiations of a set
variable, much like
\bipref{indomain/1}{../bips/lib/ic/indomain-1.html}
in the finite domain case.
Here is an example of the default enumeration strategy:
\begin{quote}\begin{verbatim}
?-  X::[]..[1,2,3], insetdomain(X,_,_,_), writeln(X), fail.
[1, 2, 3]
[1, 2]
[1, 3]
[1]
[2, 3]
[2]
[3]
[]
\end{verbatim}\end{quote}
Other enumeration strategies can be selected (see the Reference Manual
on insetdomain/4).

%\begin{description}
%\item[\biptxtref{insetdomain(?Set, ?CardSel, ?ElemSel, ?Order)}{insetdomain/4}{../bips/lib/ic_sets/insetdomain-4.html}]
%         Instantiate Set to a possible value
%\end{description}

%----------------------------------------------------------------------
\section{Example}
%----------------------------------------------------------------------

\index{Steiner problem}
The following program computes so-called Steiner triplets.
The problem is to compute triplets of numbers between 1 and N,
such that any two triplets have at most one element in common.
\begin{code}
:- lib(ic_sets).
:- lib(ic).

steiner(N, Sets) :-
        NB is N * (N-1) // 6,           % compute number of triplets
        intsets(Sets, NB, 1, N),        % initialise the set variables
        ( foreach(S,Sets) do
            #(S,3)                      % constrain their cardinality
        ),
        ( fromto(Sets,[S1|Ss],Ss,[]) do
            ( foreach(S2,Ss), param(S1) do
                #(S1 /\verb.\. S2, C),         % constrain the cardinality
                C #=< 1                 % of pairwise intersections
            )
        ),
        label_sets(Sets).               % search

label_sets([]).
label_sets([S|Ss]) :-
        insetdomain(S,_,_,_),
        label_sets(Ss).
\end{code}
Running this program yields the following first solution:
\begin{quote}\begin{verbatim}
?- steiner(9,X).

X = [[1, 2, 3], [1, 4, 5], [1, 6, 7], [1, 8, 9],
     [2, 4, 6], [2, 5, 8], [2, 7, 9], [3, 4, 9],
     [3, 5, 7], [3, 6, 8], [4, 7, 8], [5, 6, 9]] More? (;)
\end{verbatim}\end{quote}

%----------------------------------------------------------------------
\section{Weight Constraints}
\label{weight-constraint}
\index{weight constraint}
%----------------------------------------------------------------------

\index{knapsack}
\index{bin packing}
Another constraint between sets and integers is the weight/3 constraint.
It allows the association of weights to set elements, and can help when
solving problems of the knapsack or bin packing type.
The constraint takes a set and an array of element weights and
constrains the weight of the whole set:
\begin{quote}\begin{verbatim}
?- ic_sets:(Container :: [] .. [1, 2, 3, 4, 5]),
   Weights = [](20, 34, 9, 12, 19),
   weight(Container, Weights, W).
Container = Container{([] .. [1, 2, 3, 4, 5]) : _2127{0 .. 5}}
Weights = [](20, 34, 9, 12, 19)
W = W{0 .. 94}
There is 1 delayed goal.
Yes (0.01s cpu)
\end{verbatim}\end{quote}
By adding a capacity limit and a search primitive, we can solve a
knapsack problem:
\begin{quote}\begin{verbatim}
?- ic_sets:(Container :: [] .. [1, 2, 3, 4, 5]),
   Weights = [](20, 34, 9, 12, 19),
   weight(Container, Weights, W),
   W #=< 50,
   insetdomain(Container,_,_,_).
Weights = [](20, 34, 9, 12, 19)
W = 41
Container = [1, 3, 4]
More (0.00s cpu)
\end{verbatim}\end{quote}

By using the heuristic options provided by insetdomain, we can
implement a greedy heuristic, which finds the optimal solution
(in terms of greatest weight) straight away:
\index{greedy heuristic}
\begin{quote}\begin{verbatim}
?- ic_sets:(Container :: [] .. [1, 2, 3, 4, 5]),
   Weights = [](20, 34, 9, 12, 19),
   weight(Container, Weights, W),
   W #=< 50,
   insetdomain(Container,decreasing,heavy_first(Weights),_).
W = 48
Container = [1, 3, 5]
Weights = [](20, 34, 9, 12, 19)
More (0.00s cpu)
\end{verbatim}\end{quote}
\quickref{Set Weight Constraint}{
\begin{description}
\item[\biptxtref{weight(?Set, ++ElementWeights, ?Weight)}{weight/3}{../bips/lib/ic_sets/weight-3.html}]
         According to the array of element weights, the weight of set Set1 is Weight
\end{description}
}

\section{Exercises}

\begin{enumerate}

\item

Consider the knapsack problem in section~\ref{weight-constraint}.
Suppose that the items each have an associated profit, namely 17, 38, 18, 10
and 5, respectively.  Which items should be included to maximise profit?

\item

Write a predicate which, given a list of sizes of items and a list of
capacities of buckets, returns a list of (ground) sets indicating which
items should go into each bucket.  Obviously each item should go into
exactly one bucket.

Try it out with 5 items of sizes 20, 34, 9, 12 and 19, into 3 buckets of
sizes 60, 20 and 20.

\end{enumerate}

\index{library!ic_sets|)}

%HEVEA\cutend


\section{Theoretical Foundations}
\label{sec:Theoretical_Foundations}
In this experiment the speed of sound in various solids and liquids was determined. This section summarizes the theory and formulas necessary to understand the following experiments on ultrasound in section \ref{sec:Evaluation}.

%-------------------------------------------------------------------------------------------
\subsection{Wave Propagation}
\label{subsec:Wave_Propagation}
Wave propagation is the way in which waves travel. It can be distinguished between transverse waves and longitudinal waves. These waveforms are defined as follows:

\textbf{Transverse wave:} The displacement of the medium the wave travels through is perpendicular to the direction in which the wave propagates. Figure \ref{fig:transverse} shows a graphical representation of this sinusoidal waveform in a rope \cite{waves}.

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.4]{transverse}
	\caption{Representation of a periodic transverse sinusoidal wave in a rope. The up and down motion of the rope is perpendicular to the direction in which the wave propagates. This leads to each particle of the rope moving up and down \cite{waves}. - partially modified}
	\label{fig:transverse}
\end{figure}

\textbf{Longitudinal wave:} The displacement of the medium the wave travels through is parallel to the direction in which the wave propagates. Sound waves in air behave like this. The air molecules oscillate back and forth and their energy is propagated in the same direction as their motion. Figure \ref{fig:longitudinal} shows a graphical representation of this waveform in a mechanical spring \cite{waves}.

\begin{figure}[H]
	\centering
	\includegraphics[scale=0.45]{longitudinal}
	\caption{Representation of a longitudinal wave in a mechanical spring. The elastic deflection (compression and extension) of the spring is parallel to the direction in which the wave propagates. This produces areas of increased and decreased pressure \cite{waves}.}
	\label{fig:longitudinal}
\end{figure}

\newpage
%-------------------------------------------------------------------------------------------
\subsection{Reflection and Refraction}
\label{subsec:Reflection_and_Refraction}
Similar to optics, reflection and refraction also occur when sound waves hit the boundary between two different media. Contrary to optics, the refraction index is not the important quantity. The phase velocity leads to refraction and the impedance leads to reflection. It can be distinguished between a vertical and an oblique angle of incidence. The following two sections explain this in more detail \cite{ultrasound}.

%-------------------------------------------------------------------------------------------
\subsubsection{Vertical Angle of Incidence}
\label{subsubsec:Vertical_Angle_of_Incidence}
A sound wave with the pressure $\hat{p}_e$ and velocity $v_e$ that travels trough a medium with the impedance $Z_1$ hits a boundary between two media with a vertical angle of incidence. This results in a different ratio between the pressure $\hat{p}_t$ and velocity $v_t$ in the medium with the impedance $Z_2$. At the same time the pressure and the velocity have to be equal at the boundary between the two media. This leads to a third reflected partial wave. Figure \ref{fig:vertical_angle_of_incidence} shows the just described phenomenon \cite{ultrasound}.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1.5]{vertical_angle_of_incidence}
	\caption{Representation of a sound wave $e$ in blue that hits a boundary between the two media with the impedances $Z_1$ and $Z_2$. The transmitted wave $t$ is shown in green and the reflected partial wave $r$ is shown in red \cite{ultrasound}. - partially modified}
	\label{fig:vertical_angle_of_incidence}
\end{figure}

The calculation of the amplitude reflection coefficient $r$ is shown in equation \ref{eq:vertical_reflection} and the amplitude transmission coefficient $t$ is shown in equation \ref{eq:vertical_transmission}. They can be derived by utilizing the continuity conditions that are valid at the interface between the two media \cite{ultrasound}.

\begin{equation}
r = \dfrac{\hat{p}_r}{\hat{p}_e} = \dfrac{Z_2-Z_1}{Z_2+Z_1}
\label{eq:vertical_reflection}
\end{equation}
\begin{equation}
t = \dfrac{\hat{p}_t}{\hat{p}_e} = \dfrac{2 Z_2}{Z_2+Z_1}
\label{eq:vertical_transmission}
\end{equation}
where:
\begin{multicols}{2}
\begin{conditions}
	r & reflection coefficient \\
	t & transmission coefficient \\
	Z_1 \text{, } Z_2 & medium impedance
\end{conditions}
\begin{conditions}
	\hat{p}_r & reflected pressure \\
	\hat{p}_t & transmitted pressure \\
	\hat{p}_e & pressure amplitude
\end{conditions}
\end{multicols}

If $Z_2$ is smaller than $Z_1$ the reflection coefficient turns out negative which means the reflected wave is phase-shifted by 180\textdegree\ \cite{ultrasound}.
\newpage
%-------------------------------------------------------------------------------------------
\subsubsection{Oblique Angle of Incidence}
\label{subsubsec:Oblique_Angle_of_Incidence}
The reflection and transmission coefficients of a sound wave $e$ that hits an interface between two media with an oblique angle $\alpha$ depend on this angle of incidence. Furthermore, the transmitted wave will be refracted. If the medium $Z_2$ happens to be a solid, a transversal and a longitudinal wave will form. Figure \ref{fig:oblique_angle_of_incidence} shows this phenomenon which is called \flqq mode conversion\frqq\ \cite{ultrasound}.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1.5]{oblique_angle_of_incidence}
	\caption{Representation of a sound wave $e$ shown in blue that hits an interface between the two media $Z_1$ and $Z_2$ with an oblique angle of incidence. The reflected wave $r$ is shown in red and the refracted transverse and longitudinal waves are shown in green \cite{ultrasound}.}
	\label{fig:oblique_angle_of_incidence}
\end{figure}

To calculate the angle of reflection shown in equation \ref{eq:oblique_angle_of_incidence} and the refraction shown in equation \ref{eq:oblique_refraction}, the same known laws from optics apply. Equation \ref{eq:oblique_refraction} allows the use of the transversal or longitudinal sound velocity for the variable $c_2$ depending on the desired result \cite{ultrasound}.

\begin{equation}
\alpha = \alpha^\prime
\label{eq:oblique_angle_of_incidence}
\end{equation}
\begin{equation}
\dfrac{\sin{(\alpha)}}{\sin{(\beta)}} = \dfrac{c_1}{c_2}
\label{eq:oblique_refraction}
\end{equation}
where:
\begin{multicols}{2}
\begin{conditions}
	\alpha & angle of incidence \\
	\beta & angle of refraction
\end{conditions}
\begin{conditions}
	\alpha^\prime & angle of reflection \\
	c_1 \text{, } c_2 & sound velocity in the medium
\end{conditions}
\end{multicols}

%-------------------------------------------------------------------------------------------
\subsection{Total Internal Reflection}
\label{subsec:Total_Internal_Reflection}
Total internal reflection can take place at the interface between two media. This happens when the following equation is valid:

\[
\sin{(\alpha_\text{crit})} = \dfrac{c_1}{c_2}
\]

This is a variation of the above shown equation \ref{eq:oblique_refraction}. Thus, the angle of refraction $\beta$ has to be 90\textdegree\ for the equation to be valid. For $\alpha \geq \alpha_\text{crit}$ the wave is totally reflected \cite{ultrasound}.

%-------------------------------------------------------------------------------------------
\subsection{Absorption}
\label{subsec:Absorption}
Sound waves are attenuated when they propagate in a medium due to absorption and scattering. Absorption usually converts a part of the ultrasound wave into heat. Scattering leads a particle that has been excited by the incoming wave to emit a wave in all direction itself. Equation \ref{eq:absorption} shows the exponential attenuation for monochromatic radiation. The \textbf{higher the frequency}, the \textbf{stronger the absorption} and scattering \cite{ultrasound}.

\begin{equation}
\hat{p}(x) = \hat{p}_0 \cdot e^{-\mu x}
\label{eq:absorption}
\end{equation}
where:
\begin{multicols}{2}
\begin{conditions}
	\hat{p}(x) & attenuated amplitude \\
	\mu & attenuation coefficient in m$^{-1}$
\end{conditions}
\begin{conditions}
	\hat{p}_0 & initial amplitude \\
	x & distance in m
\end{conditions}
\end{multicols}

%-------------------------------------------------------------------------------------------
\subsection{Ultrasound Generation}
\label{subsec:Ultrasound_Generation}
Ultrasound can be generated and received by using a ultrasonic transducer. In this experiment the piezoelectric effect is used in the ultrasonic transducer. Certain crystals expand proportional to the applied voltage. Contrary to this, the piezoelectric transducer induces a voltage when deformed. Thus, it can be used as a transmitter or receiver \cite{ultrasound}.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1.5]{ultrasound_generation}
	\caption{Graphical representation of a piezoelectric transducer. The active part is the thin piezo disk at the bottom of the transducer. The mechanical dimensions determine the resonance frequency of the oscillation. The transducer is impedance matched to the amplifier \cite{ultrasound}.}
	\label{fig:ultrasound_generation}
\end{figure}

\newpage
%-------------------------------------------------------------------------------------------
\subsection{Ultrasound Measurement}
\label{subsec:Ultrasound_Measurement}
There are two common ways to measure ultrasound, the A-Scan and the B-Scan. This experiment uses the A-Scan as measurement method and therefore only this one will be explained.

The A-Scan is a one-dimensional procedure where short ultrasound impulses ($f > 20 \text{kHz}$) are transmitted. These are partially reflected on the interfaces between media and are received again by the transmitter. From the time difference between the sent impulse and the received echo and a known distance, the sound velocity of a medium can be determined. Equation \ref{eq:sound_velocity} shows this relationship \cite{ultrasound}.

\begin{equation}
c_m = \dfrac{2x}{\Delta t}
\label{eq:sound_velocity}
\end{equation}
where:
\begin{multicols}{2}
	\begin{conditions}
		c_m & sound velocity of a medium in $\,^{\text{m}}\!/_{\text{s}}$ \\
		\Delta t & time difference in s
	\end{conditions}
	\begin{conditions}
		x & distance to interface in m
	\end{conditions}
\end{multicols}

This technique is often used to check the integrity of materials. If the part has defects such as cracks or inclusions of different substances, there are detectable echoes. Furthermore, the wall thickness of work pieces can be determined by this method \cite{ultrasound}.


\subsubsection{\stid {3.01} xSDK Sub-project: multiprecision}
\paragraph{Overview}
Within the past years, hardware vendors have started designing low precision special function units
in response to the demand of the Machine Learning community for high compute power
in low precision formats. At the same time, the gap between compute power
on the one hand and memory bandwidth on the other hand keeps increasing,
making data access and communication prohibitively expensive compared to arithmetic operations.
Having the choice between ignoring the hardware trends and continuing the traditional path
or adjusting the software stack to the changing hardware designs, the US Exascale Computing Project
decided to build a multiprecision focus effort to take on the challenge of designing and engineering novel algorithms capable to exploit the compute power available in low precision and to adjust the communication format to application specific needs.
As part of the xSDK project, the multiprecision focus effort is a coordinated effort creating synergies via cross-institutional collaboration.

\paragraph{Key Challenges}
Generally, there exists a strong relationship between the precision used in arithmetic operations and the accuracy of the computed result. Since scientific applications need to provide high quality output, replacing high precision formats with low precision formats throughout a complete application code is generally not feasible. Instead, to utilize lower precision formats, the underlying numerical algorithms have to be redesigned to employ low precision formats for the most time-consuming parts while preserving  high accuracy in the solution. In this context, the arithmetic operations are only one aspect. As the execution time of many scientific applications is dominated by communication and memory access, the algorithm redesign also has to include strategies for compressing data to reduce the pressure on the memory bandwidth. This aspect becomes even more relevant as the arithmetic power continues to grow faster than the memory bandwidth, therewith widening the gap between arithmetic performance and memory performance, see Figure~\ref{fig:xsdk-machinebalance}.

\begin{figure}[htb]
	\centering
	\includegraphics[width=.8\columnwidth]{projects/2.3.3-MathLibs/2.3.3.01-xSDK/xSDK-machinebalance.pdf}
	%\includegraphics[width=.8\columnwidth]{xSDK-machinebalance.pdf}
	\caption{\label{fig:xsdk-machinebalance} Evolution of the machine balance of processors over different hardware generations.}
\end{figure}

\paragraph{Solution Strategy}
In the multiprecision effort, the team assesses current status and functionalities, advances the theoretical knowledge on multiprecision algorithms, designs prototype implementations and multiprecision interoperability layers, deploys production-ready multiprecision algorithms in the xSDK math libraries, ensures multiprecision cross-library interoperability and integrates multiprecision algorithms into ECP application projects. The long list of project activities is organized in a multi-phase approach:
The first stage is dedicated to the exploration of the design space and existing research efforts focusing on low precision, mixed precision, and extended precision. To this end, the effort  includes internationally-renown external experts for mixed precision algorithms. In the second stage, the multiprecision effort increasingly focuses on developing production code ready to be used in the ECP application projects. Relevant algorithmic functionality includes sparse linear algebra, multigrid methods, preconditioners, iterative solvers, low-rank approximations, and mixed precision machine learning.
On the hardware side, special focus is put on low-precision special function units like NVIDIA’s tensor cores that -- originally designed for machine learning algorithms -- offer about an order of magnitude higher arithmetic performance than conventional fp64 units.
In the third phase of the multiprecision effort, the project team will aid the ECP application projects with the adoption of multiprecision functionality and continue to adapt to new hardware technologies.

\paragraph{Recent Progress}
As a first step, the multiprecision team surveyed the state of the art in terms of mixed precision algorithms, low precision and extended precision algorithms, and the algorithmic needs of the ECP application projects. The results of this landscape assessment are made publicly available as ``A Survey of Numerical Methods Utilizing Mixed Precision Arithmetic''~\cite{Anztetal2020}.
A more compact version of this survey -- exclusively focusing on numerical linear algebra -- has been submitted as a journal article.

Based on the findings of the multiprecision landscape assessment, we created a set of cross-institutional focus groups that address different algorithms and aspects of the ECP software ecosystem including dense and sparse direct solvers, eigensolvers, Krylov-based iterative solvers, multigrid methods, preconditioners, Fast Fourier Transform, and machine learning technology. Orthogonal to those, we created focus groups on the design on a memory accessor that separates the arithmetic precision from the communication precision, and the efficient realization of multiprecision basic building blocks like sparse matrix vector multiply.
In bi-weekly virtual meetings, the progress on the different efforts is presented and discussed as many of these efforts are closely related. As another integral part of the bi-weekly phone calls we established a series of short talks where each meeting is commenced by an invited talk presenting an idea, success story, or progress update on mixed precision functionality to the audience.

Following the idea of decoupling the memory precision from the arithmetic precision to reduce the pressure on the memory bandwidth, the team released an accessor design document detailing the implementation and usage of a memory accessor that compresses data, e.g. by converting to a lower precision format, before invoking memory operations.
The document has been made available to the ECP community.

\paragraph{Next Steps}

Our next efforts include
\begin{itemize}
    \item the publication of a compacted version of the multiprecision landscape assessement as a scientific journal paper,
    \item the deployment of the accessor separating memory precision and arithmetic precision in the Ginkgo library with support for AMD GPUs, NVIDIA GPUs, and multicore CPUs,
    \item the implementation of compressed basis Krylov solvers that utilize the memory accessor to compact the Krylov search directions,
    \item the advancement of multiprecision capabilities for solvers, preconditioners, and other ECP-relevant kernels in xSDK libraries, including
    \subitem - the design and implementation of mixed precision eigensolvers,
    \subitem - the research and realization of mixed precision multigrid solvers,
    \subitem - the design and implementation of mixed precision sparse factorizations.
\end{itemize}


\paragraph{Influence of Blur Factor} \label{influence_of_blur_factor}
The blur factor for a sliding window filter is expanding the interval that is accepting time series windows. A too small
interval can lead to many false positive classifications and a too large interval may lead to too much 1NN-DTW calls.
The underlying time series measure for a sliding window filter is affecting the best performing blur factor for the
filter. Eleven different blur factors were tested for the two different underlying time series measures. The influence of
the blur factor for the filter is tested for the two underlying time series measures and compared to the
dominating configuration without filter. The right plot of figure \ref{fig:blur_factor_result} illustrates the influence
of the blur factor on the $F_{1}score_{\mu}$. Only the LNCE estimate is reaching with the given blur factors the same
$F_{1}score_{\mu}$ as the dominating configuration without filter. However, the two filter types come also with small
blur factors very close to the top $F_{1}score_{\mu}$ value.

\begin{figure}[H]
    \begin{center}
        \resizebox {\textwidth} {!} {
            \begin{tabular}{cc}
                \resizebox {!} {\height} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            legend pos=south east,
                            xmin=100,
                            xmax=300,
                            ymin=0.6,
                            ymax=0.75,
                            xlabel=filter interval size in \%,
                            ylabel=$F_{1}score_{\mu}$,
                            width=\axisdefaultwidth,
                            height=0.7*\axisdefaultheight]
                            \addplot[blue, ultra thick] table {../data/fig/blur_factor_result/lnce.dat};
                            \addlegendentry{LNCE}
                            \addplot[red, ultra thick] table {../data/fig/blur_factor_result/var.dat};
                            \addlegendentry{VAR}
                            \addplot[dotted, black, domain=100:300] {0.738393631276109};
                            \addlegendentry{No Filter}
                        \end{axis}
                    \end{tikzpicture}
                } &
                \resizebox {!} {\height} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            legend pos=south east,
                            xmin=100,
                            xmax=300,
                            ymin=0,
                            ymax=5500,
                            xlabel=filter interval size in \%,
                            ylabel=\# 1NN-DTW calls,
                            width=\axisdefaultwidth,
                            height=0.7*\axisdefaultheight]
                            \addplot[blue, ultra thick] table {../data/fig/nnc_calls_result/lnce.dat};
                            \addlegendentry{LNCE}
                            \addplot[red, ultra thick] table {../data/fig/nnc_calls_result/var.dat};
                            \addlegendentry{VAR}
                            \addplot[dotted, black, domain=100:300] {4893};
                            \addlegendentry{No Filter}
                        \end{axis}
                    \end{tikzpicture}
                }
            \end{tabular}
        }
    \end{center}
    \caption{The left plot shows the $F_{1}score_{\mu}$ depending on the blur factor that expands the size of the filter
    interval in \%. The right plot shows the amount of 1NN-DTW calls depending on the blur factor that expands the size
    of the filter interval in \%.}
    \label{fig:blur_factor_result}
\end{figure}

The left plot of figure \ref{fig:blur_factor_result} and also table \ref{tab:result} shows that the presented
filters can reach the same $F_{1}score_{\mu}$ value as the dominating simulation without filter. The question remains on
how often are the presented filters block the 1NN-DTW calls. The right plot of figure \ref{fig:blur_factor_result}
illustrates the influence of the blur factor on the amount of 1NN-DTW calls. All two filter types remain clearly below
the amount of 1NN-DTW calls of the dominating configuration without filter. Figure \ref{fig:blur_factor_tradeoff}
combines both plots of figure \ref{fig:blur_factor_result} to trade-off curves for both filters.

\begin{figure}[H]
    \begin{center}
        \resizebox {\textwidth} {!} {
            \begin{tabular}{cc}
                \resizebox {!} {\height} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            legend pos=south west,
                            xlabel=$F_{1}score_{\mu}$,
                            ylabel=\# blocked 1NN-DTW calls,
                            width=\axisdefaultwidth,
                            height=\axisdefaultwidth]
                            \addplot[blue, only marks, mark=*] table[y=Y] {../data/fig/nnc_calls_result/tradeoff-lnce.dat};
                            \addplot[gray, domain=0.7:0.74] {-83543.0872*x^2+55746.6816*x+4892.4835};
                            \addlegendentry{LNCE}
                        \end{axis}
                    \end{tikzpicture}
                } &
                \resizebox {!} {\height} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            legend pos=south west,
                            xlabel=$F_{1}score_{\mu}$,
                            ylabel=\# blocked 1NN-DTW calls,
                            width=\axisdefaultwidth,
                            height=\axisdefaultwidth]
                            \addplot[red, only marks, mark=*] table[y=Y] {../data/fig/nnc_calls_result/tradeoff-var.dat};
                            \addplot[gray, domain=0.63:0.74] {-34477.4805*x^2+20729.4890*x+4884.5834};
                            \addlegendentry{VAR}
                        \end{axis}
                    \end{tikzpicture}
                }
            \end{tabular}
        }
    \end{center}
    \caption{Trade-off between the $F_{1}score_{\mu}$ and the amount of blocked 1NN-DTW calls for both filter
    types. The gray graphs are quadratic regressions to illustrate the trade-off as curve on the observed intervals.}
    \label{fig:blur_factor_tradeoff}
\end{figure}


\chapter{How to model a system}\label{How to model a system}
\begin{aquote}{The Chemical Basis of Morphogenesis. A. Turing 1952.}
\textit{This model will be a simplification and an idealization, and consequently a falsification. It is to be hoped that the features retained for discussion are those of greatest importance in the present state of knowledge.}
\end{aquote}
\begin{aquote}{Empirical Model-Building and Response Surfaces. G. Box \& N. Draper 1987.}
\textit{All models are wrong, but some are useful.}
\end{aquote}
Modelling a system, whether it be physical, chemical, or biological, is, in some ways, more of art than a science. You try and strip away all extraneous information and mathematically describe that which is left. Sometimes there are physical laws to help you, \eg gravity, conservation of energy and mass. Other times we only have experimental intuition, \eg predator-prey interactions from population data. In either case, the central idea of modelling is that it should always form part of a cyclical process \see{Modelling_loop}.

You try to start with physical intuition (experiment), represent the important parts mathematically (model), hopefully reproduce reality (test) and, finally, use your mathematical model to predict unknown outcomes (predict). These prediction can then feed back into experiment and the process begins anew.
\begin{figure}[!!!h!!!tb]
\centering
\includegraphics[width=\tp]{../Pictures/Modelling_loop.png}
\caption{\label{Modelling_loop}Diagram of the modelling cycle.}
\end{figure}

In this chapter we are going to review some of the methods that can be used to produce a mathematical interpretation of reality.

\section{Physical laws}
\begin{defin}
A \textbf{constitutive relation} (or `physical law') is a rule that the modeller adds to the system based on their experimental experience, which relates interacting components.
\end{defin}
Physics has many laws such as: conservation of energy, general relativity and the laws of thermodynamics. There is (as yet) no fundamental reason for these laws to hold. We just take them as laws because they fit the data that we observe.

Here are just a few examples of the laws that you might come across.
\begin{itemize}

\item \textbf{Newton's Law of Cooling.}

\textit{The rate of cooling of a body is proportional to the difference between the bodies temperature and the temperature of its environment.}

\COL{Let $T$ and $T_e$ be the temperatures of the object and the environment, respectively, then
\bb
\dot{T}=k(T_e-T),\label{Law_of_cooling}
\ee
where $k$ is defined to be the heat transfer coefficient.}
\begin{example}[frametitle=Cooling tea]\label{Cooling_tea}
Suppose I prepare two cups of tea at exactly the same time, so initially they both start at $100^o$C. I quickly add enough milk to cup 1 to cool it to $90^o$C. Ten minutes later I add milk to cup 2, which cools cup 2 by $10^o$C. Which cup is hotter at that point?
\COL{
The general solution to \eqn{Law_of_cooling} is
\bb
T(t)=T_e+\l T(0)-T_e\r e^{-kt},
\ee
where $T(0)$ is the initial temperature. For cup 1 we have
\bb
T_1(10)=T_{e}+\l 90-T_{e}\r e^{-k10}.
\ee
For cup 2 we have
\bb
T_2(10)=T_{e}+\l 100-T_{e}\r e^{-k10}-10.
\ee
Subtracting $T_1$ from $T_2$ gives
\bb
T_1(10)-T_2(10)=10\l 1- e^{-k10}\r>0.
\ee
Since $\exp(0)=1$ and $\exp(-kt)$ is a strictly monotonically decreasing function of time. Hence, cup 1 is hotter than cup 2 at time $t=10$ minutes. This means that the earlier you put your milk in the hotter your tea will stay!

Note that neither the ambient temperature, nor the heat transfer coefficient were needed.}
\end{example}

\item \textbf{Newton's Second Law of Motion}

\textit{The rate of change of momentum of a body is directly proportional to the force applied to the body.}

\COL{This is the standard $F=ma$ that everyone knows and loves (assuming that the mass remains that same throughout the interaction), where the acceleration, $a$, is the second derivative of the location with respect to time, $a=\ddot{x}$.}

\item \textbf{Newton's Law of Gravitation}\footnote{Newton devised the laws of optics, the laws of motion and invented calculus practically on a dare... then he turned 26. What have you done today? }

\textit{A particle attracts every other particle in the universe using a force that is directly proportional to the product of their masses and inversely proportional to the square of the distance between their centres.}

\COL{Suppose body $i$ has mass $m_i$ and is at position $\bm{r}_i=(x_i,y_i)$. Body $i$ is then separated from body $j$ by a distance $r_{ij}=\sqrt{\l x_i-x_j\r^2+\l y_i-y_j \r^2}=|\bm{r}_i-\bm{r}_j|$. Let $G$ be the universal gravitational constant then the force, $\bm{F}_{ij}$, acting on body $i$ from body $j$ is
\bb
\bm{F}_{ij}=-G\frac{m_im_j}{r^2_{ij}}\bm{\hat{r}}_{ji},
\ee
where
\bb
\bm{\hat{r}}_{ji}=\frac{r_i-r_j}{|r_i-r_j|}
\ee
is the unit vector from body $j$ to body $i$.

Note that the force is vector valued quantity because it has a magnitude, but also a direction, as it acts in the direction of the line joining the bodies.}
\begin{figure}[!!!h!!!tb]
\centering
\includegraphics[width=\ttp]{../Pictures/Three_body_problem.png}
\caption{\label{Three_body}Schematic diagram of the three-body problem.}
\end{figure}

\begin{example}[frametitle=Three body problem]\label{Three_body_problem}
We can combine the Second Law of Motion and the Law of Gravitation in order to predict the position of planets interacting through their gravitational fields \see{Three_body}. Consider three planets with the same mass, $m$, and positions $\bm{r}_1(t)$, $\bm{r}_2(t)$ and $\bm{r}_3(t)$, respectively. Further, we note that since we are dealing with acceleration (a second order equation) we will need to specify two initial conditions, the position and velocity. Let the initial positions be $\bm{r}_i(0)=\bm{r}_{i0}$ and the initial velocities $\dot{\bm{r}}_i(0)=\bm{v}_{i0}$.\COL{ The governing equations are
\begin{align}
&m\ddot{\bm{r}}_1=-G\frac{m^2}{|\bm{r}_1-\bm{r}_2|^3}\l\bm{r}_1-\bm{r}_2\r-G\frac{m^2}{|\bm{r}_1-\bm{r}_3|^3}\l\bm{r}_1-\bm{r}_3\r,\label{g1}\\
&m\ddot{\bm{r}}_2=-G\frac{m^2}{|\bm{r}_2-\bm{r}_1|^3}\l\bm{r}_2-\bm{r}_1\r-G\frac{m^2}{|\bm{r}_2-\bm{r}_3|^3}\l\bm{r}_2-\bm{r}_3\r\label{g2},\\
&m\ddot{\bm{r}}_3=-G\frac{m^2}{|\bm{r}_3-\bm{r}_1|^3}\l\bm{r}_3-\bm{r}_1\r-G\frac{m^2}{|\bm{r}_3-\bm{r}_2|^3}\l\bm{r}_3-\bm{r}_2\r\label{g3}.
\end{align}
where we remember that this is a vector equation, $\bm{r}_i=(x_i,y_i)$, so there are actually six, second order ODEs here, rather than three.}

The three body problem illustrates chaotic behaviour, in the sense that the outcome is extremely sensitive to the initial conditions. This can be seen in the simulations of \fig{Three_body_sim}.

Simulation tip:
\begin{itemize}
\item when solving \eqnto{g1}{g3} numerically we could separate each equation into its Cartesian components and reduce the second order equation to two first order equations. Namely, we would introduce $(v_{ix},v_{iy})=(\dot{x}_i,\dot{y}_i)$. Thus, we would have a system of twelve ODEs to solve, with variables
\bb
(x_1,y_1,v_{1x},v_{1y},x_2,y_2,v_{2x},v_{2y},x_3,y_3,v_{3x},v_{3y}).
\ee
However, since $x_i$ and $y_i$ are perpendicular Cartesian coordinates it turns out to be a good idea to use complex numbers. Specifically, instead of writing two ODEs for each of $x_i$ and $y_i$ we can simply solve one ODE in terms of the complex quantity $\bm{r}_i=x_i+Iy_i$, which can be handled by numerical solvers. Thus, we simplify the numerical solution from twelve to six equations.
\end{itemize}
\end{example}
\begin{figure}[!!!h!!!tb]
\centering
\includegraphics[width=\tp]{../Pictures/Three_body_sim.png}
\caption{\label{Three_body_sim}Two simulations of the three body problem. The green and red trajectories are identically initiated. The blue trajectory is initiated at $(-1,0.01)$ in the left figure and at $(-1,0)$ in the right figure.}
\end{figure}
\item \textbf{Hooke's law}

\textit{The force, $F$, needed to extend or compress a spring by some distance, $x$, scales linearly with respect to that distance,}
\COL{
\bb
F=kx.
\ee

The constant of proportionality, $k$, defined by this law is known as the spring constant and is measured in units of Force per distance, \eg N/m.

Depending on the material Hooke's law only holds true for small extensions and compressions. For example, this law suggests that given enough force a spring can pass through itself. Further, biological materials may not follow the law because they break if stretched too far (\eg bone), or they may grow, or permanently deform\footnote{Consider, for example, the ear. Small earrings to not stretch the skin very much and, thus, once the earring is removed the skin can heal. Alternatively, people who use large gauge earrings stretch their ear holes beyond the elastic limit of the skin so that they have a permanent hole. }, thus, reducing the force needed to give the same extension (\eg skin).
}

\end{itemize}
\subsubsection{Pendulums}\label{Pendulums_section}
In this section we take an extended look at pendulums depending on Hooke's law and simple Newtonian mechanics. Specifically, we consider a spring, oscillating up and down, and a bob, oscillating side to side \see{Pendulums}.
\begin{figure}[!!!h!!!tb]
\centering
\subfigure[\label{Spring}]{\includegraphics[width=\ttp]{../Pictures/Spring.png}}
\subfigure[\label{Bob}]{\includegraphics[width=\ttp]{../Pictures/Pendulum.png}}
\caption{Two types of pendulums: (a) an oscillating spring. (b) a weight on a string.\label{Pendulums}}
\end{figure}

\COL{Assuming that the spring conforms to Hooke's Law and applying Newton's Second Law of motion the equation of motion for the spring is
\bb
m\ddot{y}=-ky,\label{Spring_eqn}
\ee
where $y$ is the vertical displacement of the spring, $m$ is the mass attached to the spring and $k$ is the spring constant. The negative sign shows that the force is always directed to the resting position of the spring (here, taken to be the origin). If the negative sign was not there then we would be saying that the pendulums position would grow exponentially with an applied force, which is not very realistic.

The pendulum bob is slightly more complicated as we have to account for two-dimensional motion. Complicating the matter further is that the pendulum weight is confined to move on the arc of a circle, meaning that problem is easier to solve in polar coordinates.

To derive the equations of motion we split the component of force acting on the pendulum into components acting along the radial and angular directions of the system, as shown in \fig{Bob}. Critically, we only need to consider the angular acceleration, which can be derived to be $r\ddot{\theta}$. The derivation will be seen on problem sheet two. Using Newton's Second Law again we derive that
\bb
mr\ddot{\theta}=-mg\sin(\theta).\label{Bob_eqn}
\ee
One interesting point we can immediately see from \eqn{Bob_eqn} is that the mass of the pendulum does not influence the solution of the equation, which can be compared with the dependence of \eqn{Spring_eqn} on the mass.

Equation \ref{Bob_eqn} can be solved directly in terms of `elliptic integrals', but this accounts to little more than integrating the equation twice and leaving the equation written in integral form. More insight to the solution can be gained if the angle of oscillation is small. In this case we can linearise the right-hand side of \eqn{Bob_eqn} by using Taylor series about zero, \ie $\sin(\theta)\approx\theta$. Hence, \eqn{Bob_eqn} can be approximated by
\bb
r\ddot{\theta}=-g\theta\label{Bob_eqn_approx},
\ee
which can be seen to be analogous to \eqn{Spring_eqn}.

If the different pendulums are displaced and released from rest then the amount of error introduced into the equation is determined by the initial displacement. \fig{Different_ICs} compares\footnote{Comparing $y$ and $\theta$ is a little dodgy as $y$ is a dimensional length and the $\theta$ is dimensionless, as we work in radians. However, if this bothers you we can fix this is in either of two ways. Either, we consider $y$ normalised by its natural length (taken here to be of unit length, regardless of the dimensions involved), or, we can consider \fig{Different_ICs} comparing \eqn{Bob_eqn} and its approximation in \eqn{Bob_eqn_approx}.} \eqns{Spring_eqn}{Bob_eqn} with different initial conditions. Thus, we see that increasing the initial amplitude of the bob pendulum causes the wave length of the oscillation to increase, or frequency of oscillation to decrease.}
\begin{figure}[!!!h!!!tb]
\centering
\subfigure[\label{IC_0.1}]{\includegraphics[width=\ttp]{../Pictures/Comparing_pendulums_IC_1.png}}
\subfigure[\label{IC_1}]{\includegraphics[width=\ttp]{../Pictures/Comparing_pendulums_IC_10.png}}
\caption{\label{Different_ICs}Comparing \eqns{Spring_eqn}{Bob_eqn} with initial conditions (a) $y=0.1=\theta$ and (b) $y=1=\theta$. Parameter values $r=g=k=m=1$.}
\end{figure}

\begin{defin}
Any system defined by an equation of the form
\bb
\ddot{u}=-k^2u.\label{SHM}
\ee
is said to under go \textbf{simple harmonic motion}.
\end{defin}
Equation \ref{SHM} can be solved to produce the solution
\bb
u=A\cos(kt)+B\sin(kt),
\ee
where $A$ and $B$ are specified through the initial conditions.

%\subsection{Euler-Bernoulli beam}
\section{Law of Mass Action}
All of the above physical laws are very specific in their application. In this section we will learn about a much more general technique that will allow us to build an ODE system out of multiple interacting populations. These populations could represent chemical compounds, humans, cells or animals as well as different states within a population \ie infected humans and susceptible humans. The law presented in this section is applied whenever the populations of the system are able to: (i) change identities; (ii) create more population members; or (iii) cause populations to decay. Specific examples of each of these interactions are, respectively: (i) susceptible humans becoming infected through interactions with a diseased person; (ii) animals giving birth; (iii) predators eating prey. Note that a change-of-identity interaction can itself be thought as a combination of creation and degradation operations. For example, in the above case of infection a member of the susceptible human population is removed from the system, whilst an infected human is added to the system. Thus, all interactions can be made through combining creation and degradation operations.

We use chemical reaction notation to specify the outcomes of population interactions. Consider a system composed of $n$ different interacting populations $(u_1,\dots,u_n)$. We assume that all interactions between the population elements lead to the creation, or destruction, of one (or more) of the $n$ populations.
\begin{defin}
A \textbf{rate equation} specifies that an interaction involves $a_1$ members of population $u_1$, $a_2$ members of population $u_2$, etc. and produces $b_1$ members of population $u_1$, $b_2$ members of population $u_2$, etc. The equation is written as
\bb
a_1u_1+a_2u_2+\dots+a_nu_n \stackrel{r}{\rightarrow} b_1u_1+b_2u_2+\dots+b_nu_n,
\ee
where $r>0$ is the \textbf{reaction rate}.
\end{defin}
Note that some of the $a_i$ and $b_i$ values can be zero.

\begin{example}[frametitle=Reaction equation examples]\label{Reaction equation examples}
\begin{itemize}
\item \textbf{Birth}

Two agents of population $u$ come together to produce a third,
\COL{\bb
2u\stackrel{r}{\rightarrow}3u.
\ee}
\item \textbf{Death}

An agent of population $u$ dies (or is destroyed) due to natural causes,
\COL{
\bb
u\stackrel{r}{\rightarrow}\slashed{0}.
\ee}

\item \textbf{Predation}

A predator population, $v$, converts energy from eating prey, $u$, into offspring,
\COL{
\bb
u+v\stackrel{r}{\rightarrow}2v.
\ee}

\item \textbf{Infection}

Consider a population of infected people, $I$, who are able to infect a susceptible population, $S$. Further, over time, the infected people recover and become susceptible again,
\COL{\begin{align}
I+S&\stackrel{r_1}{\rightarrow}2I,\\
I&\stackrel{r_2}{\rightarrow}S.
\end{align}}
\end{itemize}
\end{example}

Rate equations provide a rigorous way of defining all of the interactions a system is assumed to undergo. However, we still require a method of converting the rate equation into an ODE. This is the power of the Law of Mass Action.
\begin{defin}
The \textbf{Law of Mass Action} states that production rate of a reaction is directly proportional to the product of the input population sizes. Specifically, if
\bb
a_1u_1+a_2u_2+\dots+a_nu_n \stackrel{r}{\rightarrow} b_1u_1+b_2u_2+\dots+b_nu_n \nonumber
\ee
is the reaction of interest then the production rate is proportional to
\bb
ru_1^{a_1}u_2^{a_2}\dots u_n^{a_n}
\ee
and the accompanying ODEs are
\begin{align}
&\dot{u}_1=(b_1-a_1)ru_1^{a_1}u_2^{a_2}\dots u_n^{a_n},\\
&\dot{u}_2=(b_2-a_2)ru_1^{a_1}u_2^{a_2}\dots u_n^{a_n},\\
&\vdots\\
&\dot{u}_n=(b_n-a_n)ru_1^{a_1}u_2^{a_2}\dots u_n^{a_n}.
\end{align}
\end{defin}
Note that in converting from reaction equation to the ODE of $u_i$ we to account for the stoichiometry, \ie $(a_i-b_i)$. Further, when multiple reactions are considered, the terms arising from the Law of Mass Action are simply added together as independent terms.

\begin{example}[frametitle=Law of Mass Action examples]\label{Law of Mass Action examples}
\begin{itemize}
\item \textbf{Birth}
\COL{\begin{align}
2u&\stackrel{r}{\rightarrow}3u,\nonumber\\
\implies &\dot{u}=ru^2.
\end{align}}
\item \textbf{Death}
\COL{\begin{align}
u&\stackrel{r}{\rightarrow}\slashed{0},\nonumber\\
\implies &\dot{u}=-ru.
\end{align}}

\item \textbf{Predation}
\COL{\begin{align}
u+v&\stackrel{r}{\rightarrow}2v,\nonumber\\
\implies &\dot{u}=-ruv,\\
&\dot{v}=ruv.
\end{align}}

\item \textbf{Infection}
\COL{\begin{align}
I+S&\stackrel{r_1}{\rightarrow}2I,\quad I\stackrel{r_2}{\rightarrow}S,\nonumber\\
\implies &\dot{S}=-r_1IS+r_2I,\\
&\dot{I}=r_1IS-r_2I.
\end{align}}
\end{itemize}
\end{example}

\begin{example}[frametitle=Zombies]\label{Zombies}
Humans, $H$, and zombies, $Z$, interact through the following three interactions \see{Zombie_picture}:
\begin{enumerate}
\item humans kill zombies at a rate $a$;
\item zombies kill humans at a rate $b$;
\item zombies infect humans at a rate $c$.
\end{enumerate}
The reaction equations for this system are,
\COL{\begin{align}
&H+Z\stackrel{a}{\rightarrow}H;\\
&H+Z\stackrel{b}{\rightarrow}Z;\\
&H+Z\stackrel{c}{\rightarrow}2Z.
\end{align}
}\COL{The ODE form of the system is
\begin{align}
&\dot{H}=-bHZ-cHZ=-\alpha HZ\\
&\dot{Z}=-aHZ+cHZ=\beta HZ.
\end{align}
Since $\alpha=b+c>0$ the population of $H$ is always decreasing. However, $\beta=c-a$, which could be either positive or negative. Critically, if $\beta>0\implies c>a$ then the zombie population will grow. Alternatively, if $\beta<0 \implies c<a$ then  the zombie population decreases. Thus, the survival of the human race all depends on the sign of $c-a$, which, explicitly, is the `net rate increase of zombies', \ie zombie production minus zombie destruction.}
\end{example}
\begin{figure}[!!!h!!!tb]
\centering
\includegraphics[width=\tp]{../Pictures/Zombies.png}
\caption{\label{Zombie_picture} Possible outcomes of human-zombie interactions.}
\end{figure}

\section{Check list}
By the end of this chapter you should be able to:
\begin{todolist}
\item define all of the constitutive laws;
\item solve problems involving Newton's laws, Hooke's law and simple harmonic motion;
\item convert a system of population interactions into reaction equations;
\item convert reaction equations into ODEs using the Law of Mass Action.
\end{todolist}