\section{Concepts}

We begin our discussion around the crux of avoiding undesirable behavior: \textit{Ordering} (in the literature: \textit{linearization} and \textit{serialization}).

In our definition of a CDS (\ref{cds}), we discussed reading and writing data to a structure as well as changing the structure of the CDS itself. We will label all of these processes as: \textit{access types}. More specifically: these are procedural types. Every time we try to execute an access type we will call this an: \textit{access procedure}.

We do not want to limit ourselves to some pre-defined set of operations such as ``reads, writes, appends, removals, etc.''. These do represent a sort of canonical basis for all possible access types, but we want to be flexible enough to allow for any and all possible methods of accessing a CDS.

Thus, we need to find a way to ensure the linearization, or correct ordering, of access procedures.

For example, when and how will we guarantee that one access procedure completes before another one is started? The how is simple: we can sequentially order our access procedures i.e. apply a total ordering to all access procedures across our system.

But that can be accomplished by using only a single process without the necessity for concurrent processes. Thus we need to address the \textit{when} of ordering access procedures using multiple concurrent processes.

Not all access procedures will need to be totally-ordered i.e. a complete (mathematical) linearization of our access procedures is not always a necessity. It is this \textit{lack of necessity of a total ordering} that allows us to have the possibility of concurrency in the first place.

The degree to which our system can behave non-linearly is exactly the minimal degree to which it can be composed of concurrent processes. (Note: by "system" we are referring to a set of processes plus our CDS).

So, when is ordering a necessity? The only time two access procedures need an order is if one access procedure is \textit{dependent} on the other. We do not specify the reason for the dependency, but if a dependency exists (at all) then it is easy to see that an ordering is necessary.

Since the necessity for an ordering is not always required, we see that a complete linearization of our system is not always a necessity. Rather, the access procedures which carry a dependency on other access procedures create a \textit{partial ordering} for our system.

More specifically: all of our dependencies allows us to have a (disjoint )\textit{dependency graph} for our access procedures.

All good, right?! Sort of. Sadly, this does not resolve all of our problems. We have not discussed exactly how we will order access procedures in terms of processes.

\subsection{Spatial Dependencies}

First, let's diverge quickly back into a discussion of our CDS. Let's try to formalize it:

\begin{con-def}[Concurrent Data Structure (2)]
	\label{cds-graph}
	A \textit{concurrent data structure} is a graph $\mathcal{G} = \{\mathsf{N}, \mathsf{E}\}$ composed of $|\mathsf{N}|$ nodes and $|\mathsf{E}|$ edges, that is used to index data of type $\mathcal{T}$, and is allowed to be accessed by some set of processes $\mathcal{P}_{i}$ via some set of access types $\mathcal{A}_{t}$.
\end{con-def}

This helps us to remember that a data structure is a spatial object. It carries a certain topology and is invariant through time unless modified. Thus, it is completely possible that while access procedures may not carry a dependency: nodes of our CDS just might! The set of all nodular-dependencies will be called the: \textit{set of spatial dependencies}. These dependencies may be the dependencies that an access procedure ``adopts'' in order to consider itself dependent on another access procedure.

For example: if access procedure $\mathit{A}$ is trying to write to node $\mathrm{n}_{1} \in \mathsf{N}$ and access procedure $\mathit{B}$ is trying to write to node $\mathrm{n}_{2} \in \mathsf{N}$, but $\mathrm{n}_{2} < \mathrm{n}_{1}$ (where $<$ is a dependency arrow), then we would want to perform $\mathit{B}$ before $\mathit{A}$.

This set of spatial dependencies only covers the dependency relationships between certain nodes in our graph. But what if we wanted sub-graphs of our CDS to be dependent or more importantly \textit{independent} of other sub-graphs of our CDS?

Let's define this:

\begin{con-def}[Unique-Independent]
	\label{independents}
	Any sub-graph $\mathcal{S} \subseteq \mathcal{G}$ that does not contain any internal spatial dependencies is called an \textit{unique-independent} of our CDS.
\end{con-def}

Unique-Independents (UIs) can almost be seen as a \textit{virtualization} of our CDS's spatial topology.

Unique-Independents can obviously have external dependencies i.e. be dependent on each other. Thus, the set of UIs of our CDS forms a dependency graph. More specifically, they form a disjoint directed acyclic graph.

\begin{con-def}[Disjoint Directed Acyclic Graph]
	\label{ddag}
	The set of UIs and their dependencies on one another forms a \textit{disjoint directed-acyclic graph} (ddag) i.e. a disjoint dependencies graph. It is disjointed since UIs are not required to have dependency relations with one another and thus some UIs (nodes) will not have edges in the graph.
\end{con-def}

Also note that dependency graphs will never have multiple edges between any two nodes.

But since UIs do not have to have other UIs as dependencies it will be simpler to consider: a dependencies list.

\begin{con-def}[Dependency List]
	\label{dependency-list}
	A \textit{dependencies list} for a CDS is the adjacency list of our ddag of unique-independents.
\end{con-def}

\if
For any algebraist reading this: our dependencies list can almost be considered an \textit{anti-group} since inverses of ordered pairs \textit{cannot} exist in our list. An identity element cannot exist as the composition of any two objects cannot return to itself as that implies a cyclic dependency. Associativity and closure can exist for a basic composition operation, but objects in our dependency list can only be composed functionally (i.e. the last element of the ordered pair must be the first element of the ordered pair it is being composed with) -- so this composition only applies to a subset of our dependencies list. Thus, our dependencies list really becomes a sort of quasi-antithesis to a group.
\fi

Don't let the name ``unique-independent'' fool you. There is no requirement for an independent to be spatially-unique. In other words, a UI can share nodes from the CDS with other UIs. The only thing that makes a unique-independent ``unique'' is that it is in totality unique as to the nodes it applies too.

So while, a UI can be completely subsumed by another UI spatially, it will never refer to the exact same set of nodes that another UI is referring too. And it is this notion of ``uniqueness'' that allows it to be an independent.

This may seem like a unique-independent that subsumes UIs that have dependencies which are also subsumed UIs, conflicts with our definition (\ref{independents}) of an unique-independent.

However, what we have to notice is that this unique-independent can/should be evaluated separately from other UIs i.e. a UI that subsumes other UIs and their dependencies should only be addressed after all those other UIs have been addressed.

So, while an independent has ``no spatial dependencies'' it can still subsume UI-dependencies.

One of the most important points to take away from this section is the fact that: the structure of the CDS itself has zero semblance of how we design our concurrent accesses, only the structure of our UI-DDAG (disjoint directed acyclic graph) does.

Now that we have a way to virtualize a CDS using UIs (in a spatial manner), let's tie UIs back in with our previous discussion about ordering access procedures.

We can now use the UI (spatial) virtualization of our CDS to assign an access procedure to a particular UI. We can provide a brief pseudo-code description (written in the Go programming language syntax).

\begin{minted}{go}
type AccessProcedure struct{
	A AccessType
	UID int // unique-independent id
}
\end{minted}

\subsection{Temporal Dependencies}

Since our dependency list of UIs based off this virtualization represents all possible spatial dependencies in our CDS it can be used to help us apply an ordering to access procedures. However, a spatial dependency is not the only way access procedures can be ordered i.e. be dependent on each other. The other way access procedures can have an ordering is: \textit{per UI}.

In other words, we can still order all access procedures that have been assigned to the same UI. For example, will read access procedures always take precedence over write access procedures for a particular UI? What if we wanted to be more specific (fine-grained) than that and give \textit{certain} access procedures precedence over \textit{certain} write access procedures?

It would be impossible to suggest that access procedures that apply to a specific UI could always be totally-ordered. Thus, we arrive at the same situation we were in before with trying to linearize our system. Thankfully, we have the answer: a dependency graph (list). Except, this time our dependencies list are access procedures assigned to the same UI.

Whereas we had an UIs dependency list that represented a virtualization of the spatial dependencies, we now have a procedures dependency list that virtualizes the temporal dependencies of our CDS (per UI).

Every UI will have it's own temporal dependencies list and every node in this disjoint dependencies graph will be a specific access procedure.

\begin{con-def}[Temporal DAG]
	\label{temporal-dag}
	A temporal directed acyclic graph for a specific UI is the partial ordering of all access procedures acting on the CDS sub-graph at that UI. Unlike the UI dependency graph, this graph is not (and cannot be) disjointed.
\end{con-def}

We can also write out any temporal DAG as a temporal dependencies list for a particular UI. In this way we can redefine our access procedure code from above to include a temporal DAG node assignment:

\begin{minted}{go}
type AccessProcedure struct{
	A AccessType
	UID int // unique-independent id
	TID int // temporal dag node id
}
\end{minted}

\subsection{Acidity}

We still have to come back around to combining our ordering of dependencies with the processes in our system. But, first we should consider when two access procedures can be simply sequentially ordered. If some set of access procedures which apply to the same UI must be linearly ordered then we can consider the sequential composition of those procedures as being a new access procedure.

This can technically also apply to any sequence of access procedures (assigned to the same UI) that are not dependent on one another. If we want to reduce the total number of individual procedures in our system (i.e. reduce the number of nodes in our temporal dependency graph for a UI) then we can combine access procedures that are linearly dependent (or entirely independent) into a single access procedure.

The allowance for this combination is what we call: \textit{acidity}.

\begin{con-def}[Acidic Procedures]
	\label{acidic-procedures}
	Any access procedure or sequence of access procedures that appear to the rest of the system as a single operation on a UI are called: \textit{acidic procedures}.
\end{con-def}

All access procedures \textit{should already be} acidic procedures. But, arbitrary sequences of procedures are not necessarily demanded to be acidic. We want to specifically classify sequences of access procedures that \textit{are} acidic.

\begin{con-def}[Thread]
	\label{thread}
	A \textit{thread} is any sequence of access procedures that can be classified as a single acidic procedure. A thread can also be a sequence of acidic procedures (which may be sequences of access procedures in of themselves).
\end{con-def}

In terms of a dependency graph: a thread is assigned to a node and is also assigned to the node's dependencies iff the dependent node has only one dependency \textit{and} the dependency has only one dependent. Any sequence of nodes each with at most one dependency and one dependent can be "combined" and considered: a single Thread.

Using threads makes our virtualization complexes more succinct, as any and all sequences of access procedures are either necessarily ordered or are considered single acidic procedures. It also becomes that every node in our disjoint dependency graphs are now a thread.

\begin{props}[Nodes are Threads]
	\label{nodes-are-threads}
	Every node in our temporal dependency graphs is a thread (iff we maintain that all access procedures must be acidic procedures).
\end{props}

\begin{con-cor}[Thread DAGs]
	\label{thread-ddag}
	For every dependency graph of access procedures there is an equivalent dependency graph of threads.
\end{con-cor}

The fact of the matter is that the total number of UIs determines the minimum number of threads required to allow our CDS system to not be missing concurrency improvements (but is usually used when a maximum number of threads is a requirement).

If we choose to use fewer threads than we have UIs, then each thread will likely be assigned to multiple UIs. Can our system still offer a degree of undesired behavior avoidance in this case? The answer is yes, but the management of behavior must be handled internal to each thread.

In this case the threads will need to have internal sequential consistency. In other words, the ordering of dependent operations will only be based on the sequential ordering of access procedures within the thread. So, the thread should be well-ordered according to access procedures that apply to first: UI dependencies then UI dependents.

If the UIs that a thread is assigned to have no dependency relationship with each other (even composed dependency arrows) then the thread will not need to be ordered internally. Sometimes, ensuring this simple property makes the concurrency of the system simpler to implement and verify.

In the next section of this paper we will discuss how we will ensure acidity for access procedures. But, for now, we simply hold this assumption as a given precondition.

\subsection{Dynamic Dependencies}

Now that we have methods for defining both spatial and temporal dependencies in our system, the question arises: what happens if a dependency has a lifecycle? What if a dependency needs to only hold true some of the time?

To solve these dynamic situations we can introduce the idea of a \textit{Virtual Dependency Graph} (VDG) which is a dependency graph that can apply to multiple UIs. The key is that it \textit{must} have a limited lifespan. If it does not have a limited lifespan then it just becomes part of a spatial DDAG or a temporal DAG.

\begin{con-def}[Virtual Dependency Graph]
	\label{vdg}
	A dependency directed acyclic graph that can apply to one or more UIs for our CDS, but whose lifespan must be less than the lifespan of the CDS (system). Used to define dynamic dependencies in a concurrent system.
\end{con-def}

A VDG will always have a root node that connects to each UI that the VDG involves. Thus, a VDG can never be a disjoint graph in of itself. The root node represents the final acidic procedure that the VDG will finish before completing its lifecycle.

For example, what if a particular write on one UI needed to happen before a different write on another UI, but that this was \textit{not} necessarily true for \textit{all} writes for these two UIs?

The final write would be the VDG's root node, and the dependency write would be a single child node to the root node. This also means that the two nodes could be a single thread that sequentially ordered a write to the first UI then a write to the second.

So, VDGs can be assigned to a single UI or too multiple UIs.

But, \textit{why} exactly must a VDG always have a limited lifespan (relative to the lifecycle of the system)?

Well, in our previous mentioned example, if that VDG that applied to two UIs lasted the lifespan of the CDS then it would simply be acting as a spatial dependency "representation" (since it would signify that all writes for the dependent UI -- that it is attached to -- must wait on all writes for the dependency UI that it is also attached to).

And if the VDG had only applied to one of the UIs then it would have represented a permanent temporal DAG for similar reasons. The only thing that makes a VDG dynamic is the fact that it's lifespan is always less then that of the CDS.

Similar to the discussion in the Acidity subsection on assigning the same thread to multiple UIs, assigning a thread to multiple VDGs can possibly lead to an underutilized concurrency potential. Threads that are assigned to VDGs are essentially virtual themselves, as it is their lifecycle that is the lifecycle of a node in a VDG.

Another dynamic property of a CDS system we want to briefly discuss is the idea of: \textit{dynamic UIs}.

If UIs are allowed a lifecycle shorter than the system then the UI dependency graph could potentially need to be constantly redrawn. The only way to outright avoid this scenario is to create another entity that would behave like the nodes in a VDG (and that we specify must have a lifespan less than that of the CDS).

If a UI were to be dynamic, but have dependencies, there would have to be a way to say that it could not end its lifecycle until its dependencies ended their lifecycles (as well as ensuring that it's lifecycle was shorter than it's dependents).

Actually, this leads us to a very important (not yet discussed) lemma:

\begin{con-lem}[]
	UIs must cover all nodes in the CDS i.e. every node in the CDS must be addressed by at least one UI at all times.
\end{con-lem}

So, if we allow UIs to be dynamic then we would have to check that before a UI could end its lifecycle that every CDS node that it addresses must be addressed by some other UI.

Seeing as this would be unnecessarily computationally inefficient, we thus note that dynamic UIs are only feasible if we create the secondary notion of: \textit{Virtual UIs}.

\begin{con-def}[Virtual UI]
	\label{virtual-ui}
	A \textit{Virtual UI} is a dynamic UI which has a lifespan shorter than the CDS (UI) or another virtual UI that it has a dependency relationship to.
\end{con-def}

Because a Virtual UI (VUI) is not dependent on the same premises that real UIs are dependent on, we provide the following lemma to show an important difference between virtual and real UIs.

\begin{con-lem}[]
	Virtual UIs do \textit{not} need to be unique  (either relative to real UIs or other virtual UIs) in the sense that real UIs are "unique" relative to other real UIs.
\end{con-lem}

This lack of a necessity for any uniqueness of VUIs is what gives them a significant degree of flexibility in use (even if there use can only be relatively temporary). Also, unlike VDGs which mimic Temporal DAGs, VUIs (and their dependency relationships) are more of a dynamic version of spatial UI DDAGs.

This allows us to have: \textit{Virtual UI Dependency Graphs}. But, then this also implies that every VUI could potentially have a VDG attached to it (that must have a lifecycle less than or equal to the VUI node itself). All VUIs must have a lifecycle less than or equal to the VUI they are dependencies of. VUIs can apply to any set of nodes, but they must either have at least one real or one virtual dependent.

Every VDG and VUI node is a thread that must make it's dependents aware of its existence. In fact, all nodes in any dependency graph (virtual or real) are just threads. The only difference is that real thread nodes do not control declaring themselves dependencies of one another (this should be controlled by a "main" process), only virtual thread nodes can declare themselves as a (temporary) dependency of other thread nodes.

The final thing we would like to mention in this dependencies section is the fact that virtual (dynamic) nodes (both VDG and VUI nodes) can still have real nodes (spatial and temporal) as their dependencies. This implies that VDG nodes and VUI nodes with real dependencies need to be checked for cyclic dependencies, as it essentially becomes a (temporary) extension of the permanent spatial and temporal dependency graphs for the CDS. In other words, all extensions of any of our graphs must always preserve them as being DAGs (even if disjoint).

\subsection{Invariance}

Another part of discussing disjoint directed acyclic graphs and their equivalent dependencies lists is determining if an access type is allowed or not allowed to be used on a specific UI (or even at a specific time).

Boundary Nodes on our UI dependency graph can be useful for determining what access types are allowed for a UI. For example, it might be simpler to allow CDS-structure modifying access types for boundary UI nodes only: specifically appending and pruning sub-graphs to the CDS.

But, we should first formalize what boundary nodes are and then look at an example of how this can be useful.

\begin{con-def}[Leaf Boundaries]
	\label{leaf-boundaries}
	Any real DDAG node which has no dependencies is considered a \textit{leaf boundary} node on that DDAG.
\end{con-def}

\begin{con-lem}[]
	\label{leaf-boundaries-infinite}
	Since a leaf boundary node is a node without dependencies it can always iterate through its acidic operations infinitely often without waiting for other nodes.
\end{con-lem}

Every real (spatial and temporal) node can be represented by 4 numbers: spatial dependents, temporal dependents, spatial dependencies, and temporal dependencies.

\begin{minted}{go}
type RealNode struct{
	SpatialDependents uint
	TemporalDependents uint
	SpatialDependencies uint
	TemporalDependencies uint
}
\end{minted}

The basic difference between leaf-boundary and non-boundary real nodes can be expressed with these four variables succinctly:

\noindent\fbox{%
    \parbox{0.9\columnwidth}{%
	    \begin{itemize}
			\item Spatial Nodes: \{w, x, y, 0\}
			\item Spatial Leaf Boundary Nodes: \{0, x, y, 0\}
			\item Temporal Nodes: \{0, x, y, z\}
			\item Temporal Leaf Boundary Nodes: \{0, 0, y, z\}
		\end{itemize}
    }%
}

There is another kind of boundary node for real DDAGs:

\begin{con-def}[Root Boundaries]
	\label{root-boundaries}
	Any real dependency graph node which may or may not have dependencies but does not have any dependents is considered a \textit{root boundary} node on that dependency graph.
\end{con-def}

The classes for root boundaries can be expressed using our primary 4 variables succinctly as well:

\noindent\fbox{%
    \parbox{0.9\columnwidth}{%
	    \begin{itemize}
			\item Spatial Root Boundary Nodes: \{0, 0, y, z\}
		\end{itemize}
    }%
}

Notice, that we do not have Temporal Root Boundary Nodes defined. This is because of the following corollary.

\begin{con-cor}[No Temporal Root Boundaries]
	\label{no-temp-root-bounds}
	There are no Temporal root boundaries as every Temporal node must have at least one Spatial node dependency.
\end{con-cor}

\begin{con-ex} [2-Regular Tree]
	\label{2-regular-tree}
	We will consider a 3 node 2-Regular Tree for which we assign one node to a UI and make the root node's UI dependent on the the two leaf nodes UIs. This makes our UI DDAG have one root-boundary and two leaf-boundary nodes.

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm,
                   main node/.style={circle,draw,font=\small}]

  \node[main node] (1) {root};
  \node[main node] (2) [below left of=1] {leaf-1};
  \node[main node] (3) [below right of=1] {leaf-2};

  \path[every node/.style={}]
    (1) edge node [left] {} (2)
        edge node [right] {} (3);
\end{tikzpicture}

In this case, we will allow structure appending access types on the two leaf nodes. Specifically, we would like to ensure that these two UI nodes can only create new nodes on our 2-regular tree for each of the two branches of the tree (so that each UI has control over its own half of the 2-Regular Tree).

Thus, we can help ensure that the tree remains 2-Regular by not giving the root boundary UI node any append sub-graph access type rights.
\end{con-ex}

In Example \ref{2-regular-tree}, we see that modifying the structure of our tree is dependent on what we want to preserve (which in this case is a 2-Regular Tree structure). Thus, we did not allow the root boundary UI with it's dependencies to have the ability to use access types that modify our CDS structure (as there was no need for it too).

We divided the tree into 3 UIs which established an easy way to divide access procedures as well. However, we still would not want any of our 3 original CDS nodes to be deleted, \textit{yet} simultaneously we do want to be able to remove CDS nodes that have been appended to the two leaf CDS nodes if we want too. Therefore we still need a way to more precisely determine access type allowances.

Afterall, the above example is a very simple example. What would happen if we wanted to apply access rules for an arbitrary CDS graph structure or a more complex UI DDAG? As we have seen: one way to define a rule is by defining what is we want to preserve (at a given time) and what is allowed to change.

For example, we did not want the original three CDS nodes in Example \ref{2-regular-tree} to be deleted. But, we could have if we wanted too.

In fact, each UI could have been assigned a list of \textit{immutable} CDS sub-graphs that cannot be removed or even have their values modified. More specifically, we could have had a list of sub-graphs in the CDS that are \textit{invariant} to certain access types.

\begin{con-def}[Invariants]
	\label{invariants}
	Any data $\mathsf{d}$ stored in our CDS, or any subgraph $\mathsf{S} \subset \mathsf{G}$ that does not change when acted upon by a specific Access Type $\mathcal{A}_{t}$ is called an invariant of that access type.
	\[ \mathcal{A}_{t}\mathsf{(d)} \rightarrow \mathsf{d} \]
	\[ \mathcal{A}_{t}\mathsf{(S)} \rightarrow \mathsf{S} \]
\end{con-def}

\begin{con-aside}
Immutability is only a kind of invariance. A low-resolution invariance which blanket-denies all modification access types.

UIs are not necessarily immutable, although we could have immutable UIs if we wanted to not allow structure modification access types to change that UI.

An UI can only be immutable if it is strictly-unique i.e. iff it only accesses nodes and edges that no other UI can access. The reason for this is that if a UI covers a part of the structure that another UI addresses, then the other UI could modify the underlying CDS structure and this UI would then technically be changed.

This is why it is better to assign immutability to CDS nodes. That way, regardless of what UI a node or edge is in, if it is immutable it can never be changed by any access procedure.
\end{con-aside}

Every node in our CDS Graph $\mathsf{G}$ has a set of access types $\mathcal{A}$ that it is invariant under. Either the CDS node and its value remain invariant under an access type (e.g. a ``read'' or "copy" access type), or only the node itself remains invariant (e.g. a ``write'' or "increment" access type). Otherwise, the node and the value are mutable (e.g. in a ``delete-subgraph'' access type).

Thus, these are the canonical invariance classes for access types:

\noindent\fbox{%
    \parbox{0.7\columnwidth}{%
		\begin{itemize}
			\item Node and value are invariant.
			\item Only node is invariant.
			\item No Invariance.
		\end{itemize}
    }%
}

We could of course describe invariants at a higher level e.g. in terms of sub-graphs which are or not invariant under an access type. But, for now we just leave this as a pattern that can be used to help determine access types that are allowed (or not allowed) to be used by a particular process accessing the CDS.

\subsection{Partial Orderings}

The last thing we want to mention about dependency graphs is rules for how the graph structure should be organized. One approach is by defining a partial ordering for the nodes that will be in the graph. We will not expound completely on this topic in this paper, instead only mention a single possible approach to temporal DAG orderings.

We want to discuss \textit{access type priorities} and how they can be used to define an ordering on a temporal DAG. We know that access types can be simple reads and writes to much more complex calculation-based overwrites. However, our example will focus on simple read and write priority operations in regards to implementing a degree of ``consistency''.

By consistency we are referring to determining how often a read can be guaranteed to be reading after the latest write. One obvious simple way is to specify that if any write access procedures are in progress that a read must wait for them to signal completion before reading a value. This of course translates to a simple dependency graph where reads become dependent on write access procedures to complete before commencing themselves.

The way in which we can implement this is via: access type priorities. If the write access type has a higher priority than a read, then every write is added as a dependency to any existing read operation for a particular UI. Priorities can also vary from UI to UI.

The key point is that priorities of access types automatically grants us an ordering for access procedures for a UI (or for an entire CDS if applied to all UIs). And we get an automated ordering if all access types receive an ordering.

\subsection{Review}

\begin{con-def}[Spatial Threads]
	\label{spatial-threads}
	\textit{Spatial Threads} are threads that last the lifespan of the CDS and are attached to a single UI.
\end{con-def}

\begin{con-def}[Temporal Threads]
	\label{temporal-threads}
	\textit{Temporal Threads} are threads that last the lifespan of the CDS and are attached to a single UI (in this case temporal threads represent all (permanent) threads beyond the initial spatial thread attached to a UI).
\end{con-def}

\begin{con-def}[Spawn Threads]
	\label{temporal-threads}
	\textit{Virtual Spawn Threads} are threads are spawned by temporal threads, attached to the same UI, have lifespans shorter than the CDS, and are commonly used to avoid cyclic dependencies by becoming a temporary dependent of one of the temporal threads dependent nodes.
\end{con-def}

\begin{con-def}[VDG Threads]
	\label{virtual-threads}
	\textit{VDG Threads} are threads that have a lifespan shorter than the CDS (or "Thread Node" that they are a dependency of) and can be assigned to multiple UIs. But VDGs are processed independently
\end{con-def}


\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[osf]{libertine}
\usepackage[scaled=0.8]{beramono}
\usepackage[margin=1.5in]{geometry}
\usepackage{url}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{nicefrac}
\usepackage{microtype}

\usepackage{sectsty}
\sectionfont{\large}
\subsectionfont{\normalsize}

\usepackage{titlesec}
\titlespacing{\section}{0pt}{10pt plus 2pt minus 2pt}{0pt plus 2pt minus 0pt}
\titlespacing{\subsection}{0pt}{5pt plus 2pt minus 2pt}{0pt plus 2pt minus 0pt}
\titlespacing{\subsubsection}{0pt}{5pt plus 2pt minus 2pt}{0pt plus 2pt minus 0pt}

\usepackage{pgfplots}
\pgfplotsset{
  compat=newest,
  plot coordinates/math parser=false,
  tick label style={font=\footnotesize, /pgf/number format/fixed},
  label style={font=\small},
  legend style={font=\small},
  every axis/.append style={
    tick align=outside,
    clip mode=individual,
    scaled ticks=false,
    thick,
    tick style={semithick, black}
  }
}

\pgfkeys{/pgf/number format/.cd, set thousands separator={\,}}

\usepgfplotslibrary{external}
\tikzexternalize[prefix=tikz/]

\newlength\figurewidth
\newlength\figureheight

\setlength{\figurewidth}{8cm}
\setlength{\figureheight}{6cm}

\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex}

\newcommand{\acro}[1]{\textsc{\MakeLowercase{#1}}}
\newcommand{\given}{\mid}
\newcommand{\mc}[1]{\mathcal{#1}}
\newcommand{\data}{\mc{D}}
\newcommand{\intd}[1]{\,\mathrm{d}{#1}}
\newcommand{\inv}{^{-1}}
\newcommand{\E}{\mathbb{E}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\ci}{\text{CI}}

\DeclareMathOperator{\var}{var}
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator*{\argmax}{arg\,max}

\begin{document}

\section*{Hypothesis testing}

Let's return to the issue of hypothesis testing. Suppose we are reasoning about
a parameter $\theta$ in light of data $\data$, and wish to consider a hypothesis
$\theta \in \mc{H}$, where $\mc{H} \subseteq \Theta$ is some set of possible
values for this parameter.

We have seen that the Bayesian approach to hypothesis testing is
straightforward. We first derive the posterior distribution $p(\theta \given
\data)$ and then may compute the probability of the hypothesis directly:
\[
  \Pr(\theta \in \mc{H} \given \data)
  =
  \int_{\mc{H}} p(\theta \given \data) \intd \theta.
\]

Let's consider an explicit example. Suppose we are interested in the unknown
bias of a coin $\theta \in (0, 1)$, and begin with the uniform prior on the
interval $(0, 1)$:
\[
  p(\theta) = \mc{U}\bigl(\theta; 0, 1\bigr)
            = \mc{B}(\theta; \alpha = 1, \beta = 1).
\]
Let's collect some data to further inform our belief about $\theta$. Suppose we
flip the coin independently $n = 50$ times and observe $x = 30$ heads. After
gathering this data, we wish to consider the natural question of whether the
coin is fair: that is, whether $\theta = \nicefrac{1}{2}$.

From the developments in the last lecture, we can compute the posterior
distribution easily. It is an updated beta distribution:
\[
  p(\theta \given \data) = \mc{B}(\theta; 31, 21).
\]
We may now compute the posterior probability of the hypothesis that the coin is
fair:
\[
  \Pr(\theta = \nicefrac{1}{2} \given \data)
  =
  \int_{\nicefrac{1}{2}}^{\nicefrac{1}{2}}
  p(\theta \given \data)
  \intd\theta
  =
  0.
\]
The posterior probability of the coin being \emph{exactly} fair is zero! This
should not be surprising, as suggesting that we could possibly know the bias of
the coin with infinite precision is unfathomable.

We may however relax the question a bit to get some more insight. One option
would be to consider a parameterized family of hypotheses of the form
\[
  \mc{H}(\varepsilon) = (\nicefrac{1}{2} - \varepsilon, \nicefrac{1}{2} + \varepsilon).
\]
Thus a high probability of the hypothesis $\mc{H}(\varepsilon)$ corresponds to
the notion that the coin is ``near fair'' with an allowed error of
$\varepsilon$. We may then compute the posterior probability of these hypotheses
and consider how they vary as a function of $\varepsilon$. Figure
\ref{near_fair_probabilities} shows the results for the coin-flipping example
above. We can see that there's approximately a 50\% posterior probability that
the bias of the coin is in the interval $(0.4, 0.6)$, corresponding to
$\varepsilon = 0.1$. We also have evidence to conclude $\theta \in (0.25, 0.75)$
with near certainty. These probabilities help constrain exactly how ``fair'' or
``not fair'' we believe the coin to be in light of our evidence.

\begin{figure}
  \centering
  \input{figures/near_fair_probabilities}
  \caption{The posterior probability of the hypotheses $\mc{H}(\varepsilon)$ for
    $0 < \varepsilon < \nicefrac{1}{2}$.}
  \label{near_fair_probabilities}
\end{figure}

We briefly discussed the classical approach to hypothesis testing in the last
lecture, and will expand upon that procedure here. The idea is to create a
so-called ``null hypothesis'' $\mc{H}_0$ that serves to define what ``typical''
data may look like assuming that hypothesis. For example, for reasoning about
the fairness of a coin, we may choose the natural null hypothesis
$\mc{H}_0\colon \theta = \nicefrac{1}{2}$. Now we can use the likelihood
\[
  \Pr(x \given n, \theta = \nicefrac{1}{2})
\]
to reason about what observed data would look like if this hypothesis were
true. This is a critical point: the null hypothesis exists to define what sort
of data we would expect to see under an assumed value of $\theta$.

The classical procedure is then to define a statistic summarizing a given
dataset $s(\data)$ in some way. An example for coin flipping would be the sample
mean $s(\data) = \hat{\theta} = \nicefrac{x}{n}$. This happens to be a common
estimator for $\theta$ as well, but this is a coincidence. We now compute a
so-called \emph{critical set} $C(\alpha)$ with the property
\[
  \Pr\bigl(s(\data) \in C(\alpha) \given \mc{H}_0\bigr) = 1 - \alpha,
\]
where $\alpha$ is called the \emph{significance level} of the test. The
interpretation of the critical set is that the statistic computed from datasets
generated assuming the null hypothesis ``usually'' have values in this range.

Finally, we compute the statistic for a particular set of observed data and
determine whether it lay inside the critical set $C(\alpha)$ we have defined. If
so, the dataset appears, according to the statistic, typical for datasets
generated from the null hypothesis. If not, the dataset appears unusual, in the
sense that data generated assuming the null hypothesis would have such extreme
values of the statistic only a small portion of the time ($100\alpha$\%). In
this case, you ``reject'' the null hypothesis with significance $1 - \alpha$.

What is a $p$-value? It must be the probability that the null hypothesis is
true, right? No, it can't be: the null hypothesis cannot be associated with a
probability in the classical interpretation of probability. A $p$-value is
actually the minimum $\alpha$ for which you would reject the null hypothesis
using this procedure. That is, a $p$-value is not the probability that the null
hypothesis is true, but rather the probability that we would observe results as
extreme as those in our dataset, as measured by the chosen statistic, \emph{if
  the null hypothesis were true!} The $p$-value is thus only a probability that
is well-defined when already assuming the null hypothesis to be true.  A
$p$-value does \emph{not} say how extreme our results would appear under
alternative hypotheses.

Bayesian model selection will eventually allow us to explicitly quantify the
plausibility of a collection of models having generated the observed data.

To interpret the above procedure in the frequency interpretation of probability,
the critical sets are constructed by reasoning about the following experiment:
\begin{itemize}
\item
  generate $\data$ assuming $\mc{H}_0$;
\item
  compute $s(\data)$;
\item
  state $s(\data) \in C(\alpha)$.
\end{itemize}
In the limit of infinitely many repetitions of this experiment, the final claim
will be true exactly $100(1 - \alpha)\%$ of the time. Recall this is the
definition of probability in this context: the frequency of occurrence in the
limit of infinitely many trials. Note that the experiment we repeat here
\emph{includes generating data from the null hypothesis} as its first step! This
is not the experiment we are conducting, since we have a dataset in front of us
that we want to analyze, which may have been generated in any number of ways.

\section*{Summarizing Distributions}

In the Bayesian method, the posterior distribution $p(\theta \given \data)$ is
the main object of interest and contains all relevant information about $\theta$
in light of the observations $\data$.  A natural task is to provide a summary of
the posterior distribution, for example to efficiently convey its relevant
properties.

In the next lecture we will consider point estimation, which is one common
summarization method. Another commonly considered problem is \emph{interval
  summarization,} where we provide an interval $(\ell, u)$ indicating plausible
values of the parameter $\theta$ in light of the observed data. Classical
interval estimates are known as \emph{confidence intervals,} and we will discuss
them in more detail shortly.

The Bayesian approach to interval estimation is straightforward. Again we use
the posterior distribution $p(\theta \given \data)$ to guide the construction of
an interval summary. If we can find an interval $(\ell, u)$ such that the
posterior probability that $\theta \in (\ell, u)$ is ``large'' (say, has
probability $\alpha$):
\[
  \Pr\bigl(\theta \in (\ell, u) \given \data\bigr)
  =
  \int_\ell^u p(\theta \given \data) \intd\theta
  =
  \alpha,
\]
then we call $(\ell, u)$ an $\alpha$\emph{-credible interval} for $\theta$.
Note the parallel in this definition to our treatment of hypothesis testing
above! Effectively, an $\alpha$-credible interval is simply a hypothesis that
has posterior probability equal to $\alpha$ and happens to take the form of an
interval.

Examining our coin flipping example from before, we can construct some credible
intervals immediately from the data in Figure \ref{near_fair_probabilities}.  We
have that $\mathcal{H}(\varepsilon = 0.1) = (0.4, 0.5)$ is a 50\%-credible
interval for the bias of the coin, and $\mathcal{H}(\varepsilon = 0.2) = (0.3,
0.7)$ is a 95\%-credible interval. The slightly wider interval
$\mathcal{H}(\varepsilon = 0.25) = (0.25, 0.75)$ represents a very high
probability credible interval, corresponding to $\alpha > 99\%$.

It is clear from the definition that multiple intervals (in fact, often
uncountably many) can serve as a credible interval for a particular value of
$\alpha$. Exactly which interval should we construct to summarize a given
distribution? This is a question for which we will need to develop Bayesian
decision theory before we can continue, which we will discuss in the next
lecture. In short, we will first need to quantify how ``desirable'' a given
credible interval is in some way, then select the one maximizing this measure.
For example, we may want to construct the narrowest possible interval, or we may
wish it to be centered on a particular point (such as the posterior mean,
median, or mode), or we may wish the interval to have some other property.

The classical approach to interval summarization is to construct a so-called
confidence interval for the parameter of interest $\theta$. Again a confidence
interval is described in terms of repeating a particular experiment infinitely
many times. The experiment we consider will proceed as follows. First we are
going to define a function $\ci(\data)$ that will map a given dataset $\data$ to
an interval $(\ell, u) = \ci(\data)$. Now we consider repeating the following
experiment:
\begin{itemize}
\item
  collect data $\data$
\item
  compute the interval $(\ell, u) = \ci(\data)$
\item
  state $\theta \in (\ell, u)$.
\end{itemize}
In the limit of infinitely many repetitions of this experiment, if the final
statement is true with probability $\alpha$, then the procedure $\ci(\data)$ is
called an $\alpha$\emph{-confidence interval procedure,} and we will write
$\ci(\data; \alpha)$ to indicate the confidence level $\alpha$ when required.

This might sound like exactly the same definition as a Bayesian credible
interval. For example, if we have an $\alpha$-confidence interval procedure
available, then when we plug in a given dataset $\data$, we must have
\begin{equation}
  \Pr\bigl(\theta \in \ci(\data; \alpha) \given \data) = \alpha, \tag{$\star$}
  \label{wrong}
\end{equation}
right? \emph{No!} This interpretation is widespread, but it is wrong.  The
conclusion in \eqref{wrong} is sometimes known as the \emph{fundamental
  confidence fallacy,}%
%
\footnote{See the following reference for some excellent extended discussion on
  confidence intervals:  Richard D.\ Morey, et al.\ (2015). The fallacy of placing
  confidence in confidence intervals. \emph{Psychonomic Bulletin \& Review}
  23(1): 103--123}
%
and confuses the nature of prior information with that of posterior
information. Namely, note that the experiment we consider when defining the
confidence interval procedure \emph{includes gathering a random dataset} as its
first step. All we know is that if we repeat the confidence interval procedure
on \emph{infinitely many datasets,} that it will succeed with probability
$\alpha$. However, we usually have only one particular dataset in front of us to
analyze that we care about, and we cannot say anything about the interval
produced for this dataset in isolation.

Here is a simple example that shows how \eqref{wrong} can fail. Suppose we are
going to observe two values $x_1, x_2 \in \R$ generated independently from some
unknown distribution $p(x)$ and wish to construct a confidence interval for the
mean of the distribution generating the data, $\theta = \E[x]$. Consider the
following procedure:
\[
  \ci(\data) =
  \begin{cases}
    (-\infty, \infty) & x_1  <   x_2 \\
    \emptyset         & x_2 \geq x_1.
  \end{cases}
\]
Obviously this trivial map is a 50\%-confidence interval procedure! Because the
values are generated independently, $x_1$ will be the lesser value exactly 50\%
of the time.  In this case, the absurdly large interval produced will
\emph{definitely} contain $\theta$.  The other 50\% of the time, the interval
will be empty, and \emph{definitely will not} contain $\theta$. Therefore the
procedure succeeds exactly 50\% of the time. However, in half the cases, the
posterior probability that $\theta$ is inside the interval produced is 100\%,
and otherwise this probability is 0\%. In no case is this probability equal to
the confidence level.

Another fallacy in the interpretation of confidence intervals is the so called
\emph{precision fallacy,} that shorter confidence intervals indicate the data
provide more precise information about $\theta$. A striking illustration of this
fallacy is provided by the ``lost submarine'' example provided by Morey, et
al.\ in the reference given below. I encourage you to read this paper and
reflect!

\end{document}


\subsection{Lazy Approach as Abstraction Refinement}

\begin{frame}
  \frametitle{Abstraction}
  Assigment relations
  \vfill
  \begin{center}
  \begin{tabular}{ccc}

    \begin{minipage}{.2\textwidth}

    $\babst{\varphi}$ \\
    \\
    \\
    \\
    \\
    \\
    $\varphi$

    \end{minipage}

    &

    \begin{minipage}{.55\textwidth}
      \begin{overlayarea}{\textwidth}{5cm}
	\only<1-3|handout:0>{\scalebox{.4}{\input{assignments_1.pdf_t}}}
	\only<4|handout:0>{\scalebox{.4}{\input{assignments_2.pdf_t}}}
	\only<5|handout:0>{\scalebox{.4}{\input{assignments_3.pdf_t}}}
	\only<6|handout:0>{\scalebox{.4}{\input{assignments_4.pdf_t}}}
	\only<7>{\scalebox{.4}{\input{assignments_5.pdf_t}}}
      \end{overlayarea}
    \end{minipage}

    &

    \begin{minipage}{.3\textwidth}

    \onslide<3->{$2^n$} \\
    \onslide<3->{($n = |\{ a_i \}|$)}\\
    \\
    \\
    \\
    \onslide<2->{$\infty$} \\
    \onslide<2->(can be)

    \end{minipage}

  \end{tabular}
  \end{center}

\end{frame}

\begin{frame}
  \frametitle{Abstraction}
  Model relations
  \begin{itemize}
    \item<2-> if $\mu$ is a model for $\varphi$, then $\babst{\mu}$ is a model for $\babst{\varphi}$
    \item<3-> if $\babst{\mu}$ is not a model for $\babst{\varphi}$, then there is no $\mu$ that is a model for $\varphi$
    \item<4-> there may be some model $\babst{\mu}$ for $\babst{\varphi}$ that does not map to any model
	      $\mu$ for $\varphi$
  \end{itemize}
  \vfill
  \begin{center}
  \begin{tabular}{cc}

    \begin{minipage}{.2\textwidth}

    $\babst{\varphi}$ \\
    \\
    \\
    \\
    \\
    \\
    $\varphi$

    \end{minipage}

    &

    \begin{minipage}{.7\textwidth}
      \begin{overlayarea}{\textwidth}{5cm}
	\only<1|handout:0>{\scalebox{.4}{\input{models_1.pdf_t}}}
	\only<2|handout:0>{\scalebox{.4}{\input{models_2.pdf_t}}}
	\only<3|handout:0>{\scalebox{.4}{\input{models_3.pdf_t}}}
	\only<4>{\scalebox{.4}{\input{models_4.pdf_t}}}
      \end{overlayarea}
    \end{minipage}

  \end{tabular}
  \end{center}

\end{frame}

\begin{frame}
  \frametitle{Abstraction Refinement}

  \scriptsize

  Notice that
  \begin{itemize}
    \item Assignments $\mu$ of $\varphi$ are many (potentially $\infty$),
          infeasible to check if any of them is a model {\bf systematically}
    \item Models $\babst{\mu}$ of $\babst{\varphi}$ are finite in number,
          and easy to enumerate with a SAT-solver
    \item A model $\babst{\mu}$ is nothing but a {\bf conjunction of \tatoms},
          can be checked efficiently with a \tsolver
  \end{itemize}
  \vfill
  \pause
  These observations suggest us a methodology
  to tackle the SMT(\T) problem
  \begin{itemize}
    \item Enumerate a Boolean model $\babst{\mu}$ of $\babst{\varphi}$ (abstraction). If no model
	  exist we are done ($\varphi$ is unsatisfiable) \pause
    \item Check if $\babst{\mu}$ is satisfiable using the \tsolver. If so $\babst{\mu}$ can be extended
          to a model $\mu$ of $\varphi$, and so we are done ! ($\varphi$ is satisfiable) \pause
    \item It not, we tell the SAT-solver not to enumerate $\babst{\mu}$ again,
          thus {\bf cutting away systematically an infinite number}
	  of assignments for $\varphi$ (refinement) \pause
    \item It can be blocked by adding a clause $\neg \babst{\mu}$. Go up \pause
    \item It terminates because there are finite Boolean models
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Abstraction Refinement}

  \scriptsize

  The lazy approach falls into the so-called {\bf abstraction-refinement}
  paradigm
  \vfill
  \begin{center}
  \scalebox{.5}{\input{ar.pdf_t}}
  \end{center}

\end{frame}


\section{Sediment Transport}\label{erosion.sec.full_trans}
Ice sheets and glaciers modify their beds by eroding it, transporting sediments within a basal ice layer and a deformable bed and depositing these sediments again. These processes leave a wealth of geological data once the ice sheet has disappeared. Modelling these processes adds to our understanding of the cryospherice system. The model can be tested by comparing model output with geological observations. As usual there are feedback mechanisms between various subsystems.

There are three possible basal boundary conditions. The most simple boundary condition is when the ice is frozen to the bed which can be hard bed rock or frozen sediments (see Fig. \ref{erosion.fig.velos}A). In this case ice moves through internal deformation driven by gravity only.  The second boundary condition occurs when the ice sheet resting on hard bedrock is melting. In this case basal d\'ecollement occurs and the ice sheet can slide over the bed (see Fig. \ref{erosion.fig.velos}B). Empirical sliding laws usually take the form
\begin{equation}
  \vec{v}_{\text{slide}} = a\tau_b^pN^{-q},
\end{equation}
where $\tau_b$ is the basal shear stress, $N$ the effective pressure and $a$, $p$ and $q$ are parameters \citep{Paterson1994}. The third boundary condition occurs when the sediments at the ice bed can deform (see Fig. \ref{erosion.fig.velos}C).

\begin{figure}[htbp]
  \centering
  \includegraphics{\dir/figs/pressure_stress_strain.eps}
  \label{fig.stress_etc}
\end{figure}

This document focuses on the theoretical background and implementation of the third type of boundary condition. The model used here is based on the theory developed in \citet{Boulton1996a}.

\begin{figure}[htbp]
  \centering
  \includegraphics{\dir/figs/ice_velo.eps}
  \caption{Profiles showing horizontal velocities when the basal ice is frozen to the bed (A), the ice sheet is sliding over a rigid bed (B) and when the ice sheet is sliding over a bed of deforming sediments (C). There are three possible velocity components: the internal ice velocity, $\vec{v}_{\text{ice}}$, the sliding velocity, $\vec{v}_{\text{slide}}$ and the sediment velocity, $\vec{v}_{\text{sed}}$ \citep[after][]{Boulton1996a}.}
  \label{erosion.fig.velos}
\end{figure}

Here, the till layer is treated as a perfectly plastic material, i.e. it does not deform until the applied stress exceeds the yield strength, $\sigma_0$, of the till layer. The yield strength of the sediment is given by a Mohr--Coulomb failure criterion:
\begin{equation}
  \sigma_0=N\tan\phi+c,
\end{equation}
where $c$ is the cohesion and $\phi$ the angle of internal friction. The effective pressure at depth is
\begin{equation}
  N(z)=N_0+\frac{dN}{dz}z
\end{equation}
where $N_0$ is the effective pressure at the ice bed. The effective pressure can be assumed to increase linearly with depth as a function of the weight of the overlying sediments. If the applied stress $\tau_b$ is assumed to be constant with depth, sediment motion will cease at a depth, $z_a$, where $\tau_b$ is equal to the yield strenght of the material, i.e.
\begin{equation}
  \tau_b=\left(N_0+z_a\frac{dN}{dz}\right)\tan\phi+c
\end{equation}
or
\begin{equation}
  \label{erosion.eq.sed_thick}
  z_a=\left(\frac{\tau_b-c}{\tan\phi}-N_0\right)\left({\frac{dN}{dz}}\right)^{-1}.
\end{equation}

\begin{subequations}
Assuming a non--linearly viscous medium, the flow law for the deforming sediment layer can be written as
\begin{equation}
  \dot\epsilon=A\frac{\tau_b^n}{N^m}
  \label{erosion.eq.tillflow1}
\end{equation}
where $\dot\epsilon$ is the shear strain rate and $A$, $m$ and $n$ are constants \citep{Boulton1996a}. A similar flow law can be formulated by assuming that strain rates depend on the amount by which the shear stress exceeds the yield stress \citep{Boulton1987}, i.e.
\begin{equation}
  \dot\epsilon=A\frac{(\tau_b-\sigma_0)^n}{N^m}.
  \label{erosion.eq.tillflow2}
\end{equation}
\end{subequations}
The velocity of the till, $v_{\text{sed}}$, as a function of depth is found by integrating the flow law \eqref{erosion.eq.tillflow1} from the base of the deforming sediment layer to the surface:
\begin{equation}
  \label{erosion.eq.tillvelo}
  v_{\text{sed}}(z)=\int_{z_a}^z\dot\epsilon(z') dz'.
\end{equation}
The sediment flux $Q_{\text{sed}}$ is then
\begin{equation}
  \label{erosion.eq.tillflux}
  Q_{\text{sed}}=z_a\overline{v}_{\text{sed}}=\int_{z_a}^0v_{\text{sed}}(z)dz=\int_{z_a}^0\int_{z_a}^z\dot\epsilon(z') dz'dz
\end{equation}
where $\overline{v}_{\text{sed}}$ is the average velocity in the deforming sediment layer.
\subsection{Simplifications}
The effective pressure at the ice base, $N_0=p_{\text{ice}}-p_{\text{water}}$, depends on the basal hydrology which we currently do not simulate. This omission forces us to make some simplifying assumptions. Assuming values of $\phi$ and $c$ for a typical till (see Table \ref{erosion.tab.typical_till}) and basal shear stress of about 100kPa, sediment deformation only occurs when the effective pressure is less than 145kPa \citep{Paterson1994}. This value fits well with effective pressures between 20 and 200kPa calculated by \citet{Boulton1996a}. In \citet{Boulton1996} constant effective pressure is assumed with values of 50kPa and 100kPa.

Assuming the sediment is relatively well drained, the potential gradient $dN/dz=N_z=\text{const}$ is the typical gravitational gradient of about 10kPam$^{-1}$ \citep{Boulton1996a}. Finally, the basal shear stress is given by the ice sheet model and depends on the ice thickness and surface slope \citep{Paterson1994}:
\begin{equation}
  \vec\tau_b=-\rho_{\text{ice}}gH\vec\nabla s.
\end{equation}

Now all quantities of Equations \eqref{erosion.eq.sed_thick} and \eqref{erosion.eq.tillflux} are specified and the thickness of the actively deforming sediment bed can be expressed as a linear function of the basal shear stress,
\begin{subequations}
  \begin{equation}
    \label{erosion.eq.sed_thick_param}
    z_a(\tau_b)=\alpha\tau_b+\beta,
  \end{equation}
  where
  \begin{equation}
    \alpha=\frac1{N_z\tan\phi}\quad\text{and}\quad\beta=-\frac1{N_z}\left({N_0}+\frac{c}{\tan\phi}\right)
  \end{equation}
\end{subequations}

\begin{table}[htbp]
  \centering
  \begin{tabular}{|l|cc|}
    \hline
    Material & $c$ [kPa] & $\phi$ [$^\circ$]\\
    \hline
    Breidamerkurj\"okull$^\dag$ & 3.75 & 32\\
    typical till$^\ddag$ & 15 & 30\\
    soft glacial clay & 30-70 & 27-32\\
    stiff glacial clay & 70-150 & 30-32\\
    till (mixed grain size) & 150-250 & 32-35\\
    \hline
  \end{tabular}
  \caption{Values for cohesion $c$ and angle of internal friction $\phi$ for glacial tills. Values from \citet{Benn1998} except $^\dag$ which is from \citet{Boulton1987} and $^\ddag$ which is from \citet{Clarke1987}.}
  \label{erosion.tab.typical_till}
\end{table}

The parameters for the flow laws \eqref{erosion.eq.tillflow1} and \eqref{erosion.eq.tillflow2} are found by fitting them to observations from Breidamerkurj\"okull \citep{Boulton1987}. In Model C the flow law exponents $m$ and $n$ are assumed to be integers. Figure \ref{erosion.fig.stress-strain-fit} shows a plot of the observed strain rates vs calculated strain rates. The quality of the fit is not surprising considering that the data set consists of only 7 triplets of strain rate, shear stress and effective pressure. Table \ref{erosion.tab.models} shows the best--fitting parameter values. Figure \ref{erosion.fig.stress-strain} shows the observations together with lines of constant strain rate.

\begin{table}[htbp]
  \centering
  \begin{tabular}{|c|c|ccc|}
    \hline
    \multicolumn{2}{c|}{} & $B$ & $m$ & $n$ \\
    \hline
    Model A& $\dot\epsilon=B_1{\tau_b^{n_1}}{N^{-m_1}}$            & 32.97  & 1.8 & 1.35 \\
    Model B& $\dot\epsilon=B_2{(\tau_b-\sigma_0)^{n_2}}{N^{-m_2}}$ & 107.11 & 1.35 & 0.77 \\
    Model C& $\dot\epsilon=B_3{(\tau_b-\sigma_0)}{N^{-2}}$         & 380.86 & \multicolumn{2}{c|}{}\\
    \hline
  \end{tabular}
  \caption{Models}
  \label{erosion.tab.models}
\end{table}

\begin{figure}[htbp]
  \centering
  \includegraphics{\dir/gnu/stress-strain-fit.eps}
  \caption{Observed strain rates versus calculated strain rates of Models A, B and C.}
  \label{erosion.fig.stress-strain-fit}
\end{figure}

\begin{figure}[htbp]
  \centering
  \includegraphics{\dir/gnu/stress-strain.eps}
  \caption{Measured values of shear stress, effective pressure and strain rate, points A-G from \citet{Boulton1987} and lines of constant strain rates (30a$^{-1}$-5a$^{-1}$) for Models A,B and C. The green line indicates the yield stress.}
  \label{erosion.fig.stress-strain}
\end{figure}

The repeated integral over the flow law, Equation \eqref{erosion.eq.tillflow2}, can be simplified to a single integral. The sediment flux between the lower boundary $z_a$ and some level within the deforming layer $a$ is
\begin{equation}
  Q_{\text{sed}}(a)=\int_{z_a}^a\int_{z_a}^{z'}A\frac{(\tau_b-\sigma_0)^n}{N^m}dz'dz = A\int_{z_a}^a\frac{(a-z)(\tau_b-\sigma_0)^n}{N^m}dz.
\end{equation}
The sediment flux in a layer of thickness $z_{\text{seds}}$ which is smaller than the maximum thickness, $z_a$ is then
\begin{equation}
  Q_{\text{sed}} = Q_{\text{sed}}(0) - Q_{\text{sed}}(z_{\text{seds}})
\end{equation}
and the transport sediment velocity
\begin{equation}
  \overline{v}_{\text{sed}} = \frac{Q_{\text{sed}}(0) - Q_{\text{sed}}(z_{\text{seds}})}{z_{\text{seds}}}.
\end{equation}

The integrals over the flow law which need to be evaluated to get the sliding velocity, $v_{\text{sed}}(0)$, and the transport velocity, $\overline{v}_{\text{sed}}$, are found by numerical integration. Figure \ref{erosion.fig.sed_velos} shows these velocities and the thickness of the deforming sediment layer as a function of shear stress.

\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.9\textwidth]{\dir/figs/plot_basal.eps}
  \caption{Thickness of deforming sediment layer, sliding velocity and average sediment velocity as a function of applied shear stress. Velocities plotted with solid lines are calculated with flow law Model B and dashed lines with Model C.}
  \label{erosion.fig.sed_velos}
\end{figure}

The sediment velocity $v_{\text{sed}}(z_{\text{seds}})$ could be used for the erosion calculation described in Section \ref{erosion.sec.hb}.

\subsection{The Sediment Model}
The simplifications described above suggest a simple subglacial sediment model with 3 layers and 2 boundaries. The 3 layers are
\begin{enumerate}
\item \textbf{basal ice layer:} a thin layer of ice which carries debris gained
by regelation processes. The layer is assumed to have a uniform thickness over the entire ice sheet.
\item \textbf{deformable soft bed:} a layer of deformable sediments may accumulate below the ice sheet. The thickness of this layer is given by Equation \eqref{erosion.eq.sed_thick_param}.
\item \textbf{non--deformable soft bed:} a layer of glaciogenic sediments which is not deforming.
\end{enumerate}
The two boundary layers are the clean ice above the dirty, basal ice layer carrying debris and the hard bed rock below the non--deformable soft bed. Figure \ref{erosion.fig.ice_sed_model} illustrates this model.

\begin{figure}[htbp]
  \centering
  \includegraphics{\dir/figs/erosion_layers.eps}
  \caption{Schematic illustration of the ice sheet/sediment model.}
  \label{erosion.fig.ice_sed_model}
\end{figure}

Sediment erosion/deposition and transport are a consequence of the applied basal stress and hydrology. These processes are:
\begin{description}
\item[Erosion:] There are two possibilities why the subsurfaces can be eroded. The first process actually erodes the subsurfaces by abrasion/plucking, etc. Hard bedrock erosion is described in Section \ref{erosion.sec.hb}. Secondly, the thickness of the deformable sediment layer is linked to the applied basal stress via Equation \eqref{erosion.eq.sed_thick_param}. It can only grow if there is an undeformable soft bed layer underneath (case 2, Fig. \ref{erosion.fig.ice_sed_model}).
\item[Deposition:] is a consequence of the sediment carrying capacity of the layers being exceeded. The thickness of the dirty basal ice layer is assumed to be
constant. Excess sediment in this layer is lost to the underlying deformable soft bed (case 1, Fig. \ref{erosion.fig.ice_sed_model}). In a similar manner sediment is transferred from the deformable to the non--deformable soft bed layer when the maximum thickness of the deforming layer, given by Equation \eqref{erosion.eq.sed_thick_param}, is exceeded (case 2, Fig.\ref{erosion.fig.ice_sed_model}).
\item[Transport:] of sediment can occur in the dirty basal ice and in the deforming soft bed layer. The transport velocity in the basal ice layer is equal to the basal ice velocity, whereas the transport velocity in the deforming soft bed is given by Equation \eqref{erosion.eq.tillflux}.
\end{description}
Numerical treatment of the sediment transport is described in detail in the next section.

\subsection{Numerical Advection of Sediments}
Considering the flux of sediments through the sides of a small test volume and the continuity equation we have
\begin{equation}
  \label{erosion.eq.continuity_eqn}
  \frac{\pd s}{\pd t}+\vec\nabla\cdot\vec{q}_s=\dot{S},
\end{equation}
where $s$ is the thickness of the thickness of the sediment layer\footnote{Here we are assuming that the sediment layer is incompressible}, $\dot{S}$ is sediment erosion/deposition. The sediment flux through the faces of the control volume, $\vec{q}_s$, is defined by
\begin{equation}
  \vec{q}_s=\vec{v}_{\text{sed}}s-(D+\epsilon)\vec\nabla s,
\end{equation}
where $\vec{v}_{\text{sed}}$ is the transport velocity of the sediment, $D$ the diffusion coefficient and $\epsilon$ the turbulent mixing coefficients. In the case of sediment transport we can ignore diffusion and turbulent mixing so that Equation \eqref{erosion.eq.continuity_eqn} reduces to
\begin{equation}
  \label{erosion.eq.transport_eqn}
  \frac{\pd s}{\pd t}+\vec\nabla\cdot(\vec{v}_{\text{sed}}s)=\dot{S}.
\end{equation}

There are two fundamentally different approaches to solving the advection equation \eqref{erosion.eq.transport_eqn} numerically:
\begin{enumerate}
\item In the \textbf{Eulerian approach} an observer watches the system evolve at fixed points in space. This scheme is easily implemented on a fixed cartesian grid. However, a numerically stable method is only found if the \emph{Courant number} is smaller than 1 \citep{Press1992}, i.e.
  \begin{equation}
    \frac{|\vec{v}_{\text{sed}}|\Delta t}{\Delta x}\le1.
  \end{equation}
  One major drawback of these schemes is that they often exhibit excessive non--physical oscillations \citep{Celia1990}.
\item In a \textbf{Lagrangian approach} an observer watches the system evolve as he travels with a fluid particle. This approach has the advantage that the time step can be much larger than the time step of an Eulerian scheme. However, an initially regular spaced set of particles will evolve to a highly irregularly set at later times \citep{Staniforth1991}.
\end{enumerate}
Semi--Lagrangian advection schemes combine the regular resolution of the Eulerian approach with the enhanced stability of the Lagrangian approach. Semi--Lagrangian schemes are also knwon as Method of Characteristics where the advected species is tracked only along those fluid trajectories (characteristics) which terminate at nodal points of the fixed grid \citep{Manson2000}.

The Courant number of the sediment transport problem is naturally smaller than 1, considering typical ice velocities of the order of up to 1kma$^{-1}$ and a grid spacing of 5-10km. The conservative, semi-Lagrangian method of \citet{Manson1999} works best for Courant numbers larger than 1. Similar to \citet{Hildes2004}, we also use the Eulerian method developed by \citet{Prather1986} which conserves second--order moments and includes a flux--limiter.


\section{\module{colorsys} ---
         Conversions between color systems}

\declaremodule{standard}{colorsys}
\modulesynopsis{Conversion functions between RGB and other color systems.}
\sectionauthor{David Ascher}{da@python.net}

The \module{colorsys} module defines bidirectional conversions of
color values between colors expressed in the RGB (Red Green Blue)
color space used in computer monitors and three other coordinate
systems: YIQ, HLS (Hue Lightness Saturation) and HSV (Hue Saturation
Value).  Coordinates in all of these color spaces are floating point
values.  In the YIQ space, the Y coordinate is between 0 and 1, but
the I and Q coordinates can be positive or negative.  In all other
spaces, the coordinates are all between 0 and 1.

More information about color spaces can be found at
\url{http://www.poynton.com/ColorFAQ.html}.

The \module{colorsys} module defines the following functions:

\begin{funcdesc}{rgb_to_yiq}{r, g, b}
Convert the color from RGB coordinates to YIQ coordinates.
\end{funcdesc}

\begin{funcdesc}{yiq_to_rgb}{y, i, q}
Convert the color from YIQ coordinates to RGB coordinates.
\end{funcdesc}

\begin{funcdesc}{rgb_to_hls}{r, g, b}
Convert the color from RGB coordinates to HLS coordinates.
\end{funcdesc}

\begin{funcdesc}{hls_to_rgb}{h, l, s}
Convert the color from HLS coordinates to RGB coordinates.
\end{funcdesc}

\begin{funcdesc}{rgb_to_hsv}{r, g, b}
Convert the color from RGB coordinates to HSV coordinates.
\end{funcdesc}

\begin{funcdesc}{hsv_to_rgb}{h, s, v}
Convert the color from HSV coordinates to RGB coordinates.
\end{funcdesc}

Example:

\begin{verbatim}
>>> import colorsys
>>> colorsys.rgb_to_hsv(.3, .4, .2)
(0.25, 0.5, 0.4)
>>> colorsys.hsv_to_rgb(0.25, 0.5, 0.4)
(0.3, 0.4, 0.2)
\end{verbatim}


\documentclass[letterpaper]{article}

\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{October 31, 2014}
\maketitle
\section*{4.1 \#9,12,13}
\subsection*{9}
$(a^{-1})^{-1}=e(a^{-1})^{-1}=a(a^{-1}(a^{-1})^{-1})=ae=a$

Note that $-e\cdot-e=e$
$(-a)^{-1}=(e\cdot -a)^{-1}=-e\cdot a)^{-1}=(-e)^{-1}a^{-1}=-ea^{-1}=e\cdot-a^{-1}=-a^{-1}$

\section*{last time}
examples always keep in mind, $\mathbb{R}\mathbb{Q} \mathbb{C}\mathbb{Z}_p$

$\mathbb{Z}_n$ is a field iff $n$ is prime

$\to$ n not prime take $m|n, m<n$ then $[m]$ is not invertible so $n$ is prime

\subsection*{def}
we say that for polynomials $g(x)|f(x)\in K[x]$ if $\exists h(x)$ such that $f(x)=g(x)h(x)$

for example $(x+1)|(x^2-1)$ in $\mathbb{R}[x]$. $(x+1)\not|(x^2+1)$ in $\mathbb{R}[x]$ but it does in $\mathbb{Z}_2[x]$: $(x+1)(x+1)=x^2+x+x+1=x^2+x(1+1)+1=x^2+x(0)+1=x^2+1$

in $\mathbb{Z}_p$ $(a+b)^p=a^p+b^p$.

\subsection*{thm}
if $K$ is a field and $c\in K$ and $f(x)\in K[x]$ then there exists a unique $g(x)\in K[x]$ such that $f(x)=g(x)(x-c)+f(c)$.
\subsubsection*{proof}
claim $x-c|f(x)-f(c)$.
$f(x)=a_mx^m+\dots+a_1x+a_0$
$f(c)=a_mc^m+\dots+a_1c+a_0$
$f(x)-f(c)=a_m(x-c^m)+\dots+a_1(x-c)$

$x^t-c^t=(x-c)(x^{t-1}+x^{t-2}c+x^{t-3}c^2+\dots+xc^{t-2}+c^{t-1}$

and so $f(x)-f(c)=g(x)(x-c)\to f(x)=g(x)(x-c)+f(c)$.

now assume we have $g'(x)$ and $g(x)$ that satisfy then $g(x)(x-c)-f(c)=g'(x)(x-c)-f(c)\to (x-c)(g(x)-g'(x))=0$. $x-c$ has a coefficient of 1 and so is not zero so $g(x)-g'(x)=0$ and is unique.

\subsection*{def}
$c\in K$ is called a root of $f(x)\in K[x]$ if $f(c)=0$.

$c$ is a root of $f(x)$ iff $x-c$ divides $f(x)$

$\to$ assume $c$ is root, then $f(c)=0$. by previous theorem $\exists q(x)\in K[x]$ such that $f(x)=q(x)(x-c)+f(c)=q(x)(x-c)$ so $(x-c)$ divides $f(x)$

$\leftarrow$ assume $(x-c)$ divides $f(x)$. then $f(x)=h(x)(x-c)\to f(c)=h(c)(c-c)=h(c)\cdot 0=0$.

\subsubsection*{corollary}
$f(x)\in K[x]$, $\deg f=n$. then $f(x)$ has at most $n$ distinct roots. (assuming non-zero polynomial)

induction  on n.

$n=0$ then $f(x)=c\ne 0$. $n=1$ then $f(x)=a_1x+a_0$, $a_1\ne 0$.

assume $c\ne d$ are solutions. then $f(x)=(x-c)q(x)$ and $f(d)=(d-c)q(d)$. note that $(d-c)\ne 0$ then $q(d)=0$ and then

now take a polynomial of degree $n-1$ that has $n-1$. if it has no roots, we are done.
\end{document}


\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage[parfill]{parskip}
\usepackage{relsize}
\newcommand\numberthis{\addtocounter{equation}{1}\tag{\theequation}}
\newcommand*{\vertbar}{\rule[1ex]{0.5pt}{2.5ex}}
\newcommand*{\horzbar}{\rule[.5ex]{2.5ex}{0.5pt}}
\usepackage{algorithm}% use sudo apt-get install texlive-science
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}

\title{Meta Learning overview}
\maketitle
\tableofcontents

\section{Hyper-parameter optimization}

We can optimize over weight initialization, learning rates, neural net architecture using a variety of algorithms like Random Search, Grid Search, Evolutionary Strategies etc.

\section{Learning to learn by gradient descent by gradient descent}

$f$ is the objective function which we try to optimize by training an optimizee with parameters $\theta$. We do so by using an optimizer $g$ that determines how $f$ should update the params given the gradient information:

$\theta_{t+1} = \theta_{t} + g_{t}(\nabla f(\theta_{t}), \phi)$

We basically have an LSTM that we train with param trajectories $\theta_{0}, \theta_{1}, \ldots$ as inputs and the gradient information, how to propose the next update of the parameter $\theta_{t+1}$.

The paper uses a `Coordinatewise LSTM optimizer`:

To make the learning problem computationally tractable, we update the optimzee parameters coordinatewise, much like other successful optimization methods such as Adam, RMSprop, and AdaGrad.
To this end, we create nn LSTM cells, where nn is the number of dimensions of the parameter of the objective function. We setup the architecture so that the parameters for LSTM cells are shared, but each has a different hidden state.

The coordinatewise architecture above treats each dimension independently, which ignore the effect of the correlations between coordinates. To address this issue, the paper introduces more sophisticated methods. The following two models allow different LSTM cells to communicate each other.

\end{document}


\chapter{Introduction}
\label{ch_intro}
% \chapterhead{Introduction}
\markright{Introduction}

% \proj (\defproj)
\proj is a
set of software components
developed by CEA (Saclay, France) and Nice Observatory. This
project originated in astronomy, and involved the development of a range of
innovative methods built around multiscale analysis.
Multiresolution techniques have
been developed  in recent years, and furnish a powerful and
insightful representation
of the data. By means of multiresolution or multiscale analysis, an
image can be decomposed into a set of images (or scales), each scale
containing only structures of a given size.
This data representation, associated with noise modeling,
has been applied to very different applications such as data filtering,
deconvolution, compression, object detection, and so on. Results are
enhanced in all such processing because the multiresolution approach
allows a better understanding of how the data values
are distributed in an image,
and how the signal can be separated from the noise.

The \proj software
components include almost all applications
presented in the book {\em Image and Data Analysis: the Multiscale Approach}
 \cite{starck:book98}. The goal of
\proj is not to replace
existing image processing  packages, but to complement
them, offering the user a complete set of multiresolution tools. These tools
are executable programs, which work on a wide range of platforms,
independently of current
image processing systems.  They allow the user to perform various tasks
using multiresolution, such as wavelet transforms, filtering,
deconvolution, and so on.

The programs, written in C++, are built on three classes: the
``image" class, the ``multiresolution" class, and the
``noise\_modeling class".
Fig. \ref{fig_sadam1} illustrates this architecture. A multiresolution
transform is applied to the input data, and noise modeling is performed.
Hence the multiple scales  can be derived, and the programs can use this
in order to know at which scales, and at which positions, significant
signal has been detected.
A wide range of
multiresolution transforms are available (see Fig.~\ref{fig_sadam2}),
allowing significant flexibility.
Fig.\ \ref{fig_modelnoise} summarizes how the multiresolution support data
structure
is derived from the data and the noise-modeling.

A set of  IDL\footnote{Research Systems
Inc., 2995 Wilderness Place, Boulder, Colorado 80301.} (Interactive
Data Language)
and PV$\sim$Wave\footnote{Visual Numerics Inc.,  6230 Lookout Road,
Boulder, Colorado 80301, USA.}
routines
are included in the package which interface the executables to these
image processing packages.

\proj is an important package,
introducing front-line methods to scientists
in the physical, space and medical domains among other fields; to engineers in
such disciplines as geology and electrical engineering; and to financial
engineers and those in  other fields requiring control and analysis of
large quantities of noisy data.

% The first release of the package is at the
% beginning of 1997.
% Further information on the pre-release version, codenamed
% \proj, can be found at  \\
% http://www.dapnia.cea.fr/Sadam

\begin{figure}[t]
\centerline{
\hbox{
\psfig{figure=ch_annex2_sadam.ps,bbllx=2.5cm,bblly=3.cm,bburx=17.5cm,bbury=25cm,height=12cm,width=12cm,clip=}
}}
\caption{\proj diagram.}
\label{fig_sadam1}
\end{figure}

\begin{figure}[htb]
\centerline{
\hbox{
% \psfig{figure=ch_annex2_mr_trans.ps,bbllx=0.5cm,bblly=3.5cm,bburx=20.5cm,bbury=24.5cm,height=16cm,width=15cm,clip=}
\psfig{figure=fig_mr1_transf.ps,bbllx=0.2cm,bblly=3.5cm,bburx=20.5cm,bbury=24.5cm,height=16cm,width=15cm,clip=}
}}
\caption{Multiresolution transforms available in \proj (a selection).}
\label{fig_sadam2}
\end{figure}

\begin{figure}[htb]
\centerline{
\hbox{
\psfig{figure=ch2_noise.ps,bbllx=0.5cm,bblly=1.5cm,bburx=22cm,bbury=26.5cm,width=13cm,height=16.66cm,clip=}}
}
\caption{Determination of multiresolution support from noise modeling.}
\label{fig_modelnoise}
\index{multiresolution support}
\index{support, multiresolution}
\end{figure}


\documentclass[a4paper,11pt,twoside,titlepage,openright]{book}

\usepackage[english]{babel}
\usepackage{color}
\usepackage{graphicx}
\usepackage{amsmath}
\numberwithin{equation}{section}
\usepackage[margin=3cm]{geometry}
\usepackage{hyperref}
\usepackage{epsfig,amsfonts}
\usepackage{transparent}
\usepackage{cases}

%
\usepackage{xcolor,import}

\pagestyle{plain}

\newcommand{\ud}[1]{\underline{#1}}
\newcommand{\lt}{\left}
\newcommand{\rt}{\right}
\DeclareMathOperator{\e0}{\epsilon_0}
\newcommand{\wdg}{\wedge}
\newcommand{\emis}{\emph{emissivity}}
\newcommand{\ema}{\epsilon^{\eta}}
\newcommand{\hypot}[1]{\textbf{\textcolor{green}{#1}}}

\newcommand{\norm}[1]{\left\lVert#1\right\rVert}

\begin{document}

\title{ToFu geometric tools\\ Intersection of a LOS with a cone}
\author{Didier VEZINET \and Laura S. Mendoza}
\date{02.06.2017}
\maketitle

%\tableofcontents

\chapter{Definitions}

\section{Geometry definition in ToFu}

The definition of a fusion device in ToFu is done by defining the edge of a poloidal plane as a set of segments in a 2D plane. The 3D volume is obtained by an extrusion for cylinders or a revolution for tori.
We consider an orthonormal direct cylindrical coordinate system $(O,\ud{e}_R,\ud{e}_{\theta},\ud{e}_Z)$ associated to the orthonormal direct cartesian coordinate system $(O,\ud{e}_X,\ud{e}_Y,\ud{e}_Z)$. We suppose that all poloidal planes live in $(R,Z)$ and can be obtained after a revolution around the $Z$ axis of the user-defined poloidal plane at $\theta=0$, $\mathcal{P}_0$. Thus, the torus is axisymmetric around the $(O,Z)$ axis (see Figure~\ref{fig:tok-ab}).

\begin{figure}[h]
\centering{
\def\svgwidth{0.75\linewidth}
\import{figures/}{tore_cones12.pdf_tex}
\caption{Two examples of a circular torus approximated by a revolved octagon. For each segment $\overline{AB}$ of the octagon there is a cone with origin on the $(O,Z)$ axis.}
\label{fig:tok-ab}
}
\end{figure}

\section{Notations}

In order to simplify the computations, let $A$ and $B$ be the end points of a segment $\mathcal{S}_i$ such that $A\neq B$ and $\mathcal{P} = \cup_{i=1}^{n} \mathcal{S}_i = \cup_{i=1}^n \overline{{\rm A}_i{\rm B}_i}$ with $n$ the number of segments given by the user defining the plane $\mathcal{P}$. We define a right circular cone $\mathcal{C}$ of origin $P = ({\rm A},{\rm B}) \cap ({\rm O}, {\rm Z})$ of generatrix $(A,B)$ and of axis $(O,Z)$ (see Figure~\ref{fig:tok-ab}). Thus we can define the edge of the torus as the union of the edges of the frustums $\mathcal{F}_i$ defined by truncating the cones $\mathcal{C}_i$ to the segment $\overline{AB}_i$.

Then, any point $M$ with coordinates $(X,Y,Z)$ or $(R,\theta,Z)$ belongs to the frustum $\mathcal{F}$ if and only if
$$
\exists q \in [0;1] /
\left\{ \begin{array}{ll}
R-R_A = q(R_B-R_A)\\
Z-Z_A = q(Z_B-Z_A)
\end{array}\right.
$$

Now let us consider a LOS $L$ (i.e.: a half-infinite line) defined by a point $D$ and a normalized directing vector $u$, of respective coordinates $(X_D,Y_D,Z_D)$ or $(R_D,\theta_D,Z_D)$ and $(u_X,u_Y,u_Z)$.
Then, point M belongs to $L$ if and only if:
$$
\exists k \in [0;\infty[ / \ud{DM} = k\ud{u}
$$

%===================================================================

\chapter{Computing shortest distance between LOS and Frustum}

We want to calculate the shortest distance between a 3D ray $\mathcal{R}$ defined by its origin $\vec{D}$ and its unit directional vector $\vec{u}$ and a frustum $\mathcal{F}$ defined by a segment $AB$ extruded around the axis $\vec{N}$ of coordinates $(0,0,1)$.
We want to compute the shortest distance between a point $P$ on the ray $\mathcal{R}$ and a point $Q$ on the frustum $\mathcal{F}$.

\begin{figure}[h]
\centering{
\def\svgwidth{0.3\linewidth}
\import{figures/}{inter_LOS_Poly.pdf_tex}~
\def\svgwidth{0.45\linewidth}
\import{figures/}{inter_LOS_Poly_plane.pdf_tex}
\caption{Example of closest point between ray and Frustum: 3D space and (R,Q) plane.}
\label{fig:hoz-frus-hoz-los}
}
\end{figure}

First, let us write some of the equations that $Q$ respects

\begin{align*}
(Q-C) \cdot N &= \norm{Q-C} \cos(\tau_\mathcal{C})\\
R_q-R_A &= q(R_B-R_A)\\
Z_q-Z_A &= q(Z_B-Z_A)
\end{align*}

where $\tau_\mathcal{C}$ is the angle between $\vec{AB}$ and $-\vec{N}$.

\begin{align*}
-\vec{N}\cdot\vec{AB} & = \norm{N} \norm{AB} \cos(\tau_\mathcal{C})\\
z_A - z_B &= \norm{AB} \cos(\tau_\mathcal{C})\\
\tau_\mathcal{C} &= \arccos\left(\dfrac{z_A - z_B}{\norm{AB}}\right)
\end{align*}

We are looking to minimize the distance between $P$ and $Q$ which is equivalent to solve the following system.

\begin{numcases}{}
\dfrac{\partial}{\partial k} \norm{P-Q}^2 = 0\\[0.2cm]
\label{eq:201}
\dfrac{\partial}{\partial q} \norm{P-Q}^2 = 0
\label{eq:202}
\end{numcases}

with

\begin{align*}
\norm{P-Q}^2 &= (x_p - x_q)^2 + (y_p - y_q)^2 + (z_p - z_q)^2 \\
	& = x_p^2 + y_p^2 + z_p^2 - 2(x_p x_q + y_p y_q + z_p z_q) + x_q^2 + y_q^2 + z_q^2\\
	& = \norm{P}^2 - 2<P, Q> + \norm{Q}^2
\end{align*}

and

\begin{numcases}{}
\dfrac{\partial}{\partial k} <P, Q> = \dfrac{\partial}{\partial k} \left(  (x_D + k u_x) x_q + (y_D + k u_y) y_q + (z_D + k u_z) z_q \right)\\[0.2cm]
\label{eq:eq203}
\dfrac{\partial}{\partial q} <P, Q> = \dfrac{\partial}{\partial q} \left(x_p R_q \cos(\theta_q) + y_p R_q \sin(\theta_q) + z_p (q(z_B-z_A) - z_A)\right)
\label{eq:eq204}
\end{numcases}

\begin{figure}[h]
\centering{

\def\svgwidth{0.35\linewidth}
\import{figures/}{inter_above.pdf_tex}
\caption{Example of closest point between ray and Frustum:  (X,Y) plane.}
\label{fig:inter-above}
}
\end{figure}

We can see in Figure~\ref{fig:inter-above}, that $\theta_q = \theta_p$. By definition $cos(\theta_p) = x_p/R_p$ and $sin(\theta_p) = y_p/R_p$. Thus $x_p \cos(\theta_q) + y_p \sin(\theta_q) = (x_p^2 + y_p^2)/R_p = R_p$. We introduce this in Equation~\eqref{eq:eq204}. The derivation of Equation~\eqref{eq:eq203} is straightforward. We obtain

\begin{numcases}{}
\dfrac{\partial}{\partial k} <P, Q> = u_x x_q + u_y y_q + u_z z_q = \, <u, Q>\\[0.2cm]
\label{eq:eq205}
\dfrac{\partial}{\partial q} <P, Q> = R_p (R_B - R_A) + Z_p (Z_B - Z_A)
\label{eq:eq206}
\end{numcases}

Now, let us derivate the remaining terms in $\norm{P-Q}^2$

$$
\begin{cases}{}
\dfrac{\partial}{\partial k} \norm{P}^2 &= \dfrac{\partial}{\partial k} \left(  (x_D + k u_x)^2 + (y_D + k u_y)^2 + (z_D + k u_z)^2 \right)\\[0.2cm]
\label{eq:eq207}
\dfrac{\partial}{\partial q} \norm{Q}^2 &= \dfrac{\partial}{\partial q} \left( R_q^2 + Z_q^2\right) \\[0.2cm]
&= \dfrac{\partial}{\partial q} \left( (q(R_B-R_A)+R_A)^2 + (q(Z_B-Z_A)+Z_A)^2\right)
\label{eq:eq208}
\end{cases}
$$

$$
\begin{cases}{}
\dfrac{\partial}{\partial k} \norm{P}^2 &= 2k(u_x + u_y + u_z) + 2(u_x x_D + u_y y_D + u_z z_D)\\[0.2cm]
& = 2k \norm{u}^2 + 2 <u, D>\\[0.2cm]
\dfrac{\partial}{\partial q} \norm{Q}^2 &= 2q((R_B - R_A)^2 + (Z_B - Z_A)^2) + 2(R_A(R_B-R_A) + Z_A(Z_B-Z_A))\\[0.2cm]
& = 2q \norm{AB}^2 + 2 <OA, AB>
\end{cases}
$$

Thus, Equations~\eqref{eq:201}-\eqref{eq:202} become

$$
\begin{cases}{}
\dfrac{\partial}{\partial k} \norm{P-Q}^2 &= 2k \norm{u}^2 + 2 <u, D> - 2 <u, Q> = 0\\[0.2cm]
\dfrac{\partial}{\partial q} \norm{P-Q}^2 &=2q \norm{AB}^2 + 2 <OA, AB> - 2(R_p (R_B - R_A) +Z_p (Z_B - Z_A)) = 0
\end{cases}
$$

$$
\begin{cases}{}
k &= \dfrac{<u, Q> - <u, D>}{\norm{u}^2}\\[0.2cm]
q  &=  \dfrac{(R_p (R_B - R_A) +Z_p (Z_B - Z_A)) - <OA, AB>}{\norm{AB}^2}
\end{cases}
$$

\end{document}