\documentclass[]{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{graphicx}

%opening
\title{Modeling and Implementing a simple Heat-Convection}
\author{Florian Nikos Kitzka}

\begin{document}

\maketitle

\begin{abstract}
The aim of this document is to provide a simple guide how to create and implement a model for a heat-convection problem. The approach is to start from underlying physics to arrive at model-equations and then to explain difficulties arising during implementation. Basic knowledge of Analysis and Linear Algebra as well as some understanding of
Fluid Dynamics is required. Chapters are written quite independent of each other so that the reader
can skip parts in which no interest exists. Numerical treatment of Fluid Dynamic is a very challenging and fascinating area. This text tries to transfer the basic ideas but leaves out many details which can be found in more specialized literature.
\end{abstract}

\tableofcontents

\section{Simulation Target}
We consider a room of cubical shape.\\
We assume all walls except the bottom to allow transfer of air (open walls).\\
The bottom is assumed to be heated at a constant temperature.\\
Our goal is to simulate how this heat is being transferred through the room over time.

\includegraphics{setup}

\section{Creation of Model} \label{model_creation}
There are two main mechanism of heat transfer in nature. One is diffusion the other is advection. We will shortly explain these two mechanisms and how to incorporate this into a model.\\
\subsection{Heat Diffusion}
Consider a small volume $V$ with edge length $\Delta x$. We denote the temperature at the left surface by $T_L$ and at the right surface by $T_R$. Then Fourier's law states the heat-flux through left surface is
\begin{equation} \label{heat_flux_left}
I(x_0)=-\lambda\frac{\partial T}{\partial x}(x_0)
\end{equation}
and through right surface is
\begin{equation} \label{heat_flux_right}
I(x_1)=-\lambda\frac{\partial T}{\partial x}(x_1)
\end{equation}
$I$ is the amount of heat-energy passing per unit of time through unit of area.
The coefficient $\lambda$ depends on the material and is called thermal conductivity.
Furthermore each material which is added heat-energy is changing its temperature according to
\begin{equation*}
c_p \Delta T=\Delta q
\end{equation*}
$q$ denotes the heat-energy per unit of mass and $c_p$ which is dependent on the material is the specific heat capacity. By using the density $\rho$ we can rewrite this as
\begin{equation} \label{specific_heat}
c_p \Delta T=\frac{1}{\rho V} \Delta Q
\end{equation}
where $Q$ denotes the total heat energy added to the volume $V$.
Analogous consideration as done in (\ref{heat_flux_left}) and (\ref{heat_flux_right}) can be made for all other surfaces. That is, each surface
contributes either positive or negative depending on the direction of flux to the internal change of heat energy per unit of time.
We can formulate this by
\begin{equation*}
\Delta Q = A\Delta t \sum_{j}-I_{j}(x_{j}+\Delta x)+I_{j}(x_{j})
\end{equation*}
where $A$ denotes the area of a surface, $I_{j}(x_j)$ the heat-flux through surface $S_j$
and $I_{j}(x_j+\Delta x)$ the heat-flux through the opposite surface in $V$. Summation is carried out only over the three surfaces which span the volume.
By setting $A=V/\Delta x$ and using equations (\ref{heat_flux_left}) we obtain
\begin{equation*}
\rho c_p V\Delta T = \lambda A\Delta t\sum_{j}\frac{\partial T}{\partial x_j}(x_j+\Delta x)-\frac{\partial T}{\partial x_j}(x_j)
\end{equation*}
By using $V/A=\Delta x$ this equation is equivalent to
\begin{equation*}
\rho c_p \frac{\Delta T}{\Delta t} = \frac{\lambda}{\Delta x}\sum_{j}\frac{\partial T}{\partial x_j}(x_j+\Delta x)-\frac{\partial T}{\partial x_j}(x_j)
\end{equation*}
Finally be letting $\Delta x\rightarrow 0$ we find the heat-diffusion equation
\begin{equation}
\frac{\partial T}{\partial t}=\frac{\lambda}{\rho\alpha}\Delta T
\end{equation}
Note: here $\Delta T$ refers to the Laplace operator ($\Delta T=\nabla\cdot\nabla T$).

\subsection{Advection}
Advection describes the mechanism a physical property to be transported by a bulk movement. In our example the bulk movement is presented by the wind and the physical property is either momentum or heat.\\
We consider the same volume $V$ as in previous section and denote the physical (scalar) property by $\phi$. Here $\phi$ shall be given per unit of mass. In addition we denote by $\textbf{v}$ the current velocity field. At the left surface let us consider the normal directed velocity component $v_x$. During the interval $\Delta t$ we have a displacement of mass by
\begin{equation} \label{displacement}
\xi_{x}=v_x\Delta t
\end{equation}
Thus the net balance of mass flowing into and out of $V$ through the left and right surface is
\begin{equation} \label{net_flow}
-\rho A\cdot\xi_{x+\Delta x} \cdot \phi(x+\Delta x)+\rho A\cdot\xi_{x} \cdot \phi(x)
\end{equation}
which by using (\ref{displacement}) becomes
\begin{equation*}
-\rho A\cdot  v_{x+\Delta x}\cdot\Delta t \cdot \phi(x+\Delta x)+\rho A\cdot  v_{x}\cdot\Delta t \cdot \phi(x)
\end{equation*}
By 'adding a zero' we can rewrite this as
\begin{align*}
&-\rho A\cdot v_{x+\Delta x}\cdot\Delta t \cdot \phi(x+\Delta x) \\ &+
\rho A\cdot v_{x}\cdot\Delta t \cdot \phi(x+\Delta x) -
\rho A\cdot v_{x}\cdot\Delta t \cdot \phi(x+\Delta x) \\ &+
\rho A\cdot v_{x}\cdot\Delta t \cdot \phi(x)
\end{align*}
Further by gathering terms
\begin{align*}
&\rho A\cdot (v_{x}-v_{x+\Delta x})\cdot\Delta t \cdot \phi(x+\Delta x) +
\rho A\cdot v_{x}\cdot\Delta t \cdot (\phi(x)-\phi(x+\Delta x))
\end{align*}
By using $A=V/\Delta x$ we can write this as
\begin{align*}
&\rho V \frac{v_{x}-v_{x+\Delta x}}{\Delta x}\cdot\Delta t \cdot \phi(x+\Delta x) +
\rho V v_{x}\cdot\Delta t\frac{\phi(x)-\phi(x+\Delta x)}{\Delta x}
\end{align*}
By letting $\Delta x\rightarrow 0$ we obtain
\begin{equation*}
-\rho \phi(x)\Delta t\cdot V\frac{\partial v_x}{\partial x}
-\rho v_x\Delta t\cdot V \frac{\partial \phi}{\partial x}(x)
\end{equation*}
The same consideration can be done in $y$ and $z$ direction and by adding all these net balances we obtain as net change of $\phi$
\begin{equation*}
\rho V \Delta \phi =
-\rho V \Delta t\phi(x)\left(\frac{\partial v_x}{\partial x}
+\frac{\partial v_y}{\partial y}+\frac{\partial v_z}{\partial z}\right)
 -\rho V \Delta t \left(v_x \frac{\partial \phi}{\partial x}+
 v_y \frac{\partial \phi}{\partial y}+
 +v_z \frac{\partial \phi}{\partial z}\right)
\end{equation*}
Dividing by $\rho V \Delta t$ and letting $\Delta t\rightarrow 0$, we obtain
\begin{equation} \label{transp_equation}
\frac{\partial\phi}{\partial t}=-\phi\nabla\cdot\textbf{v} -\textbf{v}\cdot\nabla\phi
\end{equation}
Note, in very generic contexts one also takes the change of density into account, but here we will assume the density to be a global constant.
Also for our needs it will suffice to assume incrompressibility , that is, $\nabla\cdot\textbf{v}=0$. It expresses in essence that all mass flowing into the volume must flow out somewhere else.

We can now use this equation to express transport of heat-energy. Heat-energy and temperature are
related by $c_p T=u$ where $u$ denotes the inner energy. So what in fact is transported is $u$ and by inserting we get
\begin{equation} \label{transp_heat}
\frac{\partial T}{\partial t}=-\textbf{v}\cdot\nabla T
\end{equation}
Since the moment applies into all three directions we have to consider as $\phi$ separately all components of velocity, $v_x, v_y, v_z$. This gives a set of three equation which we can write compactly as
\begin{equation}
\frac{\partial\textbf{v}}{\partial t}=-\textbf{v}\cdot\nabla\textbf{v}
\end{equation}

\subsection{Momentum Sources}
In reality many forces a acting on our considered volume. For simplicity we restrict our attention buoyancy. This force always arises when
a region of warmer fluid is surrounded by colder fluid. It only applies in vertical direction and
can be formulated in terms of an acceleration as
\begin{equation}
\textbf{F}_{B}=g\frac{T-T_{A}}{T_{A}}\textbf{e}_{z}
\end{equation}
Here $g$ denotes the gravity acceleration and $T_A$ the surrounding temperature.
Note, in our implementation we will assume this $T_A$ is just given by the temperature above
the considered grid-point.
\subsection{Final Model}

A final very simplified model looks like this
\begin{align}
\frac{\partial T}{\partial t}&=-\textbf{v}\cdot \nabla T + \frac{k}{\rho\alpha}\Delta T \label{model_equations} \\
\frac{\partial v_{z}}{\partial t}&=g\frac{T-T_{A}}{T_{A}}-v_{z}\frac{\partial v_{z}}{\partial z} \nonumber
\end{align}

Although we could restrict computation on dimension 1 we will do it for dimension 2 in order to
get more inside into the techniques used.

\section{Numerical Scheme}
In order to solve the model equations (\ref{model_equations}) numerically we are going to use the so called Finite Difference Method. This requires to lay a fixed grid over our domain (the room) and replace all derivative expressions by discrete approximations.\\
As we have seen in section \ref{model_creation}, physical properties like
heat and momentum are transported by diffusion and advection processes.\\
Our goal is to provide a generic scheme for both processes.
Hereby we are going to consider both processes independent of each other and restrict
attention to the 1-dimensional case. It will turn out later that this treatment is already
sufficient for the final implementation.\\
A small remark on our assumed incompressibility condition ($\nabla\cdot\textbf v=0$): Actually after each time-iteration it must be ensured this property to be withhold. Usually this is done by adjusting the pressure ('pressure correction'). But, in order to simplify things we will just skip this part.

\subsection{Derivative Approximations} \label{derivate_approx}
The main method to obtain approximations for derivatives is by using Taylor expansion.
We give a detailed description for one specific derivative expression but leave the remaining for the reader since they are produced in analogy.\\
Also, for reasons becoming clear later, we restrict our attention to 1-dimensional functions.
Consider the two Taylor expansions,
\begin{equation*}
u(x+h)=u(x)+u'(x)h+\frac{1}{2}u''(x)h^2+o(2)
\end{equation*}
\begin{equation*}
u(x-h)=u(x)-u'(x)h+\frac{1}{2}u''(x)h^2+o(2)
\end{equation*}
By adding both equations we obtain
\begin{equation*}
u(x+h)+u(x-h)=2u(x)+u''(x)h^2
\end{equation*}
By solving the later for $u''(x)$ we get a second-order approximation for the second derivative
of $u$.
\begin{equation*}
u''(x)=\frac{1}{h^2}(u(x+h)+u(x-h)-2u(x)) + o(2)
\end{equation*}

For the numerical treatment we have to consider $u$ discrete in space and time.
We define
\begin{equation*}
u_{i}^{n}=u(t_{n}, x_{i})
\end{equation*}
or in case of 2-dimensions
\begin{equation*}
u_{i,j}^{n}=u(t_{n}, x_{i}, y_{j})
\end{equation*}

With this we can now easily list the approximations we are going to use, especially the one
we just obtained for second-order derivatives.

\begin{eqnarray}
	\left[\frac{\partial u}{\partial t}\right]_{i}^{n} & \approx \frac{1}{\Delta t}(u_{i}^{n+1}-u_{i}^{n}) \nonumber  \\
	\left[\frac{\partial u}{\partial x}\right]_{i}^{n} & \approx \frac{1}{\Delta x}(u_{i}^{n}-u_{i-1}^{n})  \label{backward_in_space} \\
	\left[\frac{\partial u}{\partial x}\right]_{i}^{n} & \approx \frac{1}{\Delta x}(u_{i+1}^{n}-u_{i}^{n}) \label{forward_in_space}  \\
	\left[\frac{\partial^2 u}{\partial x^2}\right]_{i}^{n} & \approx
	\frac{1}{\Delta x^2}(u_{i-1}^{n}-2u_{i}^{n}+u_{i+1}^{n}) \nonumber
\end{eqnarray}

For first derivative we have provided two approximations, (\ref{forward_in_space}) is called
forward in space and (\ref{backward_in_space}) is called backward in space.

Note, we only present a small selection of possible ways to approximate derivatives. In  this text we only focus on so called explicit schemes, but the reader should be made aware that there exists another important category of so called implicit schemes.

\subsection{Advection Scheme}

In general advaction processes have the form
\begin{equation*}
\frac{\partial u}{\partial t} = -c \frac{\partial u}{\partial x} + S
\end{equation*}
Here $S$ denotes a possible source term for the property $u$.\\
By using approximations for derivatives as given in section \ref{derivate_approx}
we can formulate the following scheme:

For positive velocity ($c\geq 0$):
\begin{equation} \label{advection_scheme_upwind}
u_{i}^{n+1}=u_{i}^{n}-c\frac{\Delta t}{\Delta x} (u_{i}^{n}-u_{i-1}^{n}) + S_{i}^{n}
\end{equation}

For negative velocity:
\begin{equation} \label{advection_scheme_downwind}
u_{i}^{n+1}=u_{i}^{n}-c\frac{\Delta t}{\Delta x} (u_{i+1}^{n}-u_{i-1}^{n}) +S_{i}^{n}
\end{equation}

The reason for splitting the scheme based on the direction of velocity becomes clear in section \ref{stability}.

\subsection{Diffusion Scheme}
Diffusion processes are of the form
\begin{equation*}
\frac{\partial u}{\partial t} = \alpha \frac{\partial^2 u}{\partial x^2}
\end{equation*}
and again by using derivative approximations from section \ref{derivate_approx} we
formulate the following scheme:
\begin{equation} \label{diffusion_scheme}
u_{i}^{n+1}=u_{i}^ {n}+\alpha\frac{\Delta t}{\Delta x^{2}} (u_{i-1}^{n}-2u_{i}^{n}+u_{i+1}^{n})
\end{equation}

\subsection{Stability} \label{stability}
One of the most important things with the use of approximation
schemes is to ensure stability.
Actually one could argue that since we have justified all our derivative approximations by Taylor-expansions we are ready and can blindly implement the discrete equations into
a computer-system. This approach turns out to be too naive.\\
In general each computation is accompanied with rounding errors. Although these can be very small they can sum-up drastically when we have to repeat calculations in iterations.
Our scheme has the form
\begin{equation} \label{stability_scheme}
u_{n+1}=A(u_{n})
\end{equation}
where $A$ is some linear function. \\
We denote by $u_{n}$ the hypothetically calculated value without any rounding errors, and by $\tilde{u}_{n}$ the actual calculated value. Then we have
\begin{equation}
\tilde{u}_{n}=u_{n}+\epsilon_{n}
\end{equation}
where $\epsilon_{n}$ denotes the deviation of step $n$.
Therefore instead of (\ref{stability_scheme}) we actually deal with
\begin{equation} \label{deviated_scheme}
u_{n+1}+\epsilon_{n+1}=A(u_{n}+\epsilon_{n})
\end{equation}
At step $n$ we obtain a deviated solution which we use at step $n+1$.
Thus at step $n+1$, on the one hand we have to deal with pure rounding errors from the current step and on the other hand by the fact that actually we are using the deviated solution $u_{n}+\epsilon_{n}$ as input.
So the very important question arises: How does the $\epsilon_{n}$'s evolve over time?\\
Combining equations (\ref{stability_scheme}), (\ref{deviated_scheme}) and using linearity of $A$ we obtain
\begin{equation} \label{error_scheme}
\epsilon_{n+1}=A(\epsilon_{n})
\end{equation}
This gives us a direct relation between errors at different steps.
The striking idea is to formulate a scheme, that is choose $A$, so that for all $n$
\begin{equation} \label{scheme_error}
|\epsilon_{n+1}| \leq |\epsilon_{n} |
\end{equation}
Note, if above equation holds, then we can be sure that although we still have to face
rounding errors, the fact that we use a deviated version $u_{n}+\epsilon_{n}$ as input does not impact the overall outcome of the iteration.\\
In order to show a given scheme to fulfill (\ref{scheme_error}) we use a method invented by John von Neumann.\\
The idea behind this is to assume the errors can be approximated (or estimated) by a Fourier-expansion
\begin{equation} \label{error_fourier_exp}
\epsilon(t,x)=e^{at}\sum_{k}e^{jkx}
\end{equation}
In other words, some function which decreases or increases exponentially in time. Our aim is to find a scheme $A$ which enforces $a<0$ in above representation of $\epsilon(t,x)$.\\

\subsubsection{Diffusion Scheme}
We replace $\epsilon(t,x)$ in equation (\ref{scheme_error}) by using for $A$ the scheme given in (\ref{diffusion_scheme}). Since $A$ is linear we can restrict attention to one specific index $k$ in (\ref{error_fourier_exp}) to obtain
\begin{equation*}
e^{a(t+\Delta t)}e^{jkx_{i}}=e^{at}e^{jkx_{i}}+\alpha\frac{\Delta t}{\Delta x^2}
\left(e^{at}e^{jk(x_{i}-\Delta x)} -2e^{at}e^{jkx_{i}}+e^{at}e^{jk(x_{i}+\Delta x)}\right)
\end{equation*}
Note the we have used $x_{i-1}=x_{i}-\Delta x$, $x_{i+1}=x_{i}+\Delta x$ and $\epsilon(t_{n+1}, x_{i})=\epsilon(t_{n}+\Delta t, x_{i})$.\\
Further by canceling out factors on both sides we get
\begin{equation*}
e^{a\Delta t}=1+\alpha\frac{\Delta t}{\Delta x^2}
\left(e^{-jk\Delta x} -2+e^{jk\Delta x}\right)
\end{equation*}
Further by use of Euler's formula the r.h.s can be combined to get
\begin{equation*}
e^{a\Delta t}=1+\alpha\frac{\Delta t}{\Delta x^2}
\left(2cos(k\Delta x) -2\right)
\end{equation*}
Since the cosinus always has absolute value below $1$, a sufficient condition the l.h.s to be lower than $1$ is $2\alpha\frac{\Delta t}{\Delta x^2}\leq\frac{1}{2}$ or equivalent,
\begin{equation} \label{stability_restr_diffusion}
\Delta t\leq \frac{1}{4}\frac{\Delta x^2}{\alpha}
\end{equation}
In other words, in order to ensure our diffusion-scheme to be stable, we have to impose (\ref{stability_restr_diffusion}) onto our implementation - we cannot choose arbitrary large time steps $\Delta t$ for a given $\Delta x$.

\subsubsection{Advection Scheme}
The same steps as above but by using as $A$ the scheme (\ref{advection_scheme_upwind}) we obtain
\begin{equation*}
e^{at}=1-c\frac{\Delta t}{\Delta x}\left(1-e^{-jk\Delta x}\right)
\end{equation*}
We can use the triangle inequality to estimate
\begin{equation*}
e^{at}\leq \left|1-c\frac{\Delta t}{\Delta x}\right| + c\frac{\Delta t}{\Delta x}
\end{equation*}
Note, we have used the known identity $|e^{jy}|=1$.\\
From the we can conclude the r.h.s to be lower $1$ if $0\leq c\Delta t/\Delta x\leq 1$ or equivalent
\begin{equation} \label{stability_crit_advect}
\Delta t\leq \frac{\Delta x}{c}
\end{equation}
For the case of $c<0$ above consideration would show scheme (\ref{advection_scheme_upwind}) to never be stable. But if the same steps as above are applied on scheme (\ref{advection_scheme_downwind}) instead we find the same stability criterion (\ref{stability_crit_advect}).

\subsection{Operator and Term Splitting}
You might have wondered that our approximation schemes all were targeted at 1-dimensional problems, but our model is formulated in two dimensions. Moreover
we have treated diffusion and advection independent from each other whereas in our
model they interact with each other.\\
All this is due to an elegant trick referred as Operator and Term Splitting which allows to separate dimensions and transport mechanisms in the implementation. In other words we can implement for each dimension and each
transport mechanism a scheme and then combine these schemes with each other.\\

\subsubsection{Dimensional Split}
Let us use the advection-scheme as explanatory example. Once understanding the idea, the techniques easily can be adapted to other schemes.
We can write scheme (\ref{advection_scheme_upwind}) in an operator form
\begin{equation*}
L_{x} := id - c\Delta t \delta_{x}+S
\end{equation*}
Hereby $\delta_{x}$ stands for an operator which takes a function $u$ as argument and
returns the approximated first derivative on a given grid, that is
\begin{equation*}
\delta_{x}(u_{i}) = \frac{u_{i}-u_{i-1}}{\Delta x}
\end{equation*}
Further $id$ denotes the identity operator
\begin{equation*}
id(u_{i})=u_{i}
\end{equation*}
 and $S$ a constant operator
\begin{equation*}
S(u_{i})=S_{i}
\end{equation*}
With this definitions our scheme (\ref{advection_scheme_upwind}) can be written as
\begin{equation*}
u^{n+1}_{i} = L_{x}(u^{n}_{i})
\end{equation*}
In analogy we can define the operator $L_{y}$ which applies in $y$-direction
\begin{equation*}
L_{y}:=id - c\Delta t \delta_{y}+S
\end{equation*}
and build the composition of both
\begin{equation} \label{L_x_comp_L_y}
L_{x}\circ L_{y}=id-c\Delta t\delta_{x}-c\Delta t\delta_{y}+o(2)
\end{equation}
In the later we have skipped terms of order $2$ in $\Delta t$. \\
A general advaction-process in dimension 2 has the form,
\begin{equation*}
\frac{\partial u}{\partial t} = -c_{x}\frac{\partial u}{\partial x}-c_{y} \frac{\partial u}{\partial y}
\end{equation*}
We can use derivative approximations from section \ref{derivate_approx} to obtain a scheme in the form
\begin{equation} \label{L_xy_scheme}
u_{i,j}^{n+1}=u_{i,j}^{n}-c_{x}\frac{\Delta t}{\Delta x}(u_{i,j}^{n}-u_{i-1,j}^{n})
-c_{y}\frac{\Delta t}{\Delta x}(u_{i,j}^{n}-u_{i,j-1}^{n})
\end{equation}
Now it is an easy task to verify that
\begin{equation*}
L_{x}\circ L_{y}(u_{i,j})=u_{i,j}^{n}-c_{x}\frac{\Delta t}{\Delta x}(u_{i,j}^{n}-u_{i-1,j}^{n})
-c_{y}\frac{\Delta t}{\Delta x}(u_{i,j}^{n}-u_{i,j-1}^{n}) +o(2)
\end{equation*}
and by comparing with (\ref{L_xy_scheme}) to see that the 2-dimensional scheme coincides
up to order 2 with the composition of the 1-dimensional scheme.\\
Therefore, in our implementation instead of writing the scheme (\ref{L_xy_scheme}) we will first apply the advection scheme (\ref{advection_scheme_upwind}) for $x$-direction and on the result we will again apply (\ref{advection_scheme_upwind}) but for $y$-direction.

\subsubsection{Term Splitting}
The idea of term splitting is quite the same as for dimension splitting. But this times the split is done w.r.t different physical processes, that is diffusion and advection.\\
Like for the advection scheme we can introduce an operator which describes scheme (\ref{diffusion_scheme})
\begin{equation*}
L_{d}:=id+\alpha\Delta t\delta_{xx}
\end{equation*}
whereas
\begin{equation*}
\delta_{xx}(u_{i})=\frac{u_{i-1}-2u_{i}+u_{i+1}}{\Delta x^2}
\end{equation*}
When we compose $L_{d}$ with $L_{x}$ from previous section we obtain
\begin{equation*}
L_{x}\circ L_{d}=id-c\Delta t\delta_{x}+\alpha\Delta t\delta_{xx}+o(2)
\end{equation*}
A system containing diffusion and advection has in general the form
\begin{equation*}
\frac{\partial u}{\partial t} = -c\frac{\partial u}{\partial x}+\alpha\frac{\partial^2 u}{\partial x^2}
\end{equation*}
Again we can use the derivative approximations from section \ref{derivate_approx} to obtain a scheme
\begin{equation} \label{diff_adv_scheme}
u_{i}^{n+1}=u_{i}^{n}-c\Delta t\delta_{x}(u_{i}^{n})+\alpha\Delta t\delta_{xx}(u_{i}^{n})
\end{equation}
We observe that the composition $L_{x}\circ L_{d}$ coincides up to order 2 with above scheme.\\
In our implementation, instead of writing (\ref{diff_adv_scheme}), we will first apply $L_{d}$ and on the result $L_{x}$.

\subsection{Final Scheme}
In previous sections we were describing things a little bit more general than they actually are in our specific model. Since buoyancy acts into $z$-direction we have to take into account only the $z$-component of velocity. Due to our specific boundary conditions we actually could model the entire system in 1-dimension but for educational reasons we do it in 2-dimensions.\\
The final scheme can be written by using above operator expressions as
\begin{equation} \label{final_scheme_expr}
u_{i,j}^{n+1}=L_{x}\circ L_{D,y}\circ L_{D,x}(u_{i,j}^{n})
\end{equation}
$L_{D,x}$, $L_{D,y}$ denote the 1-dimensional diffusion operators applied in $x$-direction resp. $y$-direction.\\
This is an explicit scheme which becomes very handy to implement in this form as we will see next.

\section{Implementation}
The entire implementation as available at: \\ \url{https://github.com/applied-math-coding/basic-diffusion-transport}.\\
The code is written in Typescript (by utilizing the package \cite{lina}) and another version in Python.
An online running example can be found here,\\
 \url{https://applied-math-coding.github.io/basic-diffusion-transport/}.\\
In the following we will give explanations for the Python code by focusing on the important snippets. The Typescript code is similar structured and due to the use of \cite{lina} operator implementation are quite similar.\\

Model parameter are defined in the file 'params.py'.
We initialize the temperature and velocity field by using three matrices. Note, the matrices
include the boundaries:
\begin{lstlisting}[language=Python]
T = np.empty((params.n_grid, params.n_grid))
T.fill(params.T_c)
v_y = np.zeros((params.n_grid, params.n_grid))
v_x = np.zeros((params.n_grid, params.n_grid))
\end{lstlisting}

We iterate the application of operators and directly write the result
into above matrices:
\begin{lstlisting}[language=Python]
def simulate():
  t = 0
  while t <= params.duration:
    utils.adjust_boundary(T, v_x, v_y)
    utils.diffusion_x_op(T, alpha, delta_t, delta_x)
    utils.diffusion_y_op(T, alpha, delta_t, delta_x)
    utils.heat_convection_y_op(T, v_y, delta_t, delta_x)
    utils.mom_convection_y_op(T, v_y, delta_t, delta_x)
    utils.adjust_boundary(T, v_x, v_y)
    t = t+delta_t
\end{lstlisting}

Each operator is defined in file 'utils.py'.
The boundary conditions are ensured by:
\begin{lstlisting}[language=Python]
def adjust_boundary(T, v_x, v_y):
  T[0, :] = params.T_h
  T[-1, :] = T[-2, :]
  T[:, 0] = T[:, 1]
  T[:, -1] = T[:, -2]
  v_y[0, :] = 0
  v_y[-1, :] = v_y[-2, :]
  v_y[:, 0] = v_y[:, 1]
  v_y[:, -1] = v_y[:, -2]
\end{lstlisting}

In order to implement the operators, instead of using loops we are element-wise combining (adding, ...) sub-matrices (slices). The indexes of these sub-matrices are shifted against each other in order to achieve the
specific schemes receipt. This way the implementation is quite optimized and moreover resembles the actual scheme's definition:
\begin{lstlisting}[language=Python]
def diffusion_x_op(T, alpha, delta_t, delta_x):
  T[1:-1, 1:-1] = T[1:-1, 1:-1] + alpha * delta_t / \
  pow(delta_x, 2)
    * (T[1:-1, 0:-2]-2*T[1:-1, 1:-1]+T[1:-1, 2:])

def diffusion_y_op(T, alpha, delta_t, delta_x):
  T_cen = T[1:-1, 1:-1]
  T_down = T[0:-2, 1:-1]
  T_up = T[2:, 1:-1]
  T[1:-1, 1:-1] = T_cen + alpha * delta_t / \
  pow(delta_x, 2) * (T_down-2*T_cen+T_up)

def heat_convection_y_op(T, v_y, delta_t, delta_x):
  T_cen = T[1:-1, 1:-1]
  T_down = T[0:-2, 1:-1]
  v_y_cen = v_y[1:-1, 1:-1]
  T[1:-1, 1:-1] = T_cen - delta_t / delta_x * v_y_cen
    * (T_cen-T_down)

def mom_convection_y_op(T, v_y, delta_t, delta_x):
  T_cen = T[1:-1, 1:-1]
  v_y_cen = v_y[1:-1, 1:-1]
  v_y_down = v_y[0:-2, 1:-1]
  T_up = T[2:, 1:-1]
  b = params.g * np.maximum(np.zeros(T_cen.shape),
  (T_cen-T_up)/T_up)
  v_y[1:-1, 1:-1] = v_y_cen + delta_t * b - \
  delta_t / delta_x * v_y_cen * (v_y_cen-v_y_down)
\end{lstlisting}
\noindent
All code provided in this section is not intended for use in production environments. It is aimed for educational purposes only. For any mistakes the author does not take any responsibilities.
\newpage
\section{Further Reading}
This article intended to give an introduction into all treated areas.
There are many good books or online tutorials about numeric of partial
differential equations. Or if you are more specialized on fluid dynamics you will find
many good accounts on this field either. Please feel encouraged to clone the entire project from
\url{https://github.com/applied-math-coding/basic-diffusion-transport} and to extend or play around.

\begin{thebibliography}{9}
	\bibitem{lina}
	applied.math.coding
	\url{https://www.npmjs.com/package/@applied.math.coding/lina}
\end{thebibliography}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2003-2018 by The University of Queensland
% http://www.uq.edu.au
%
% Primary Business: Queensland, Australia
% Licensed under the Apache License, version 2.0
% http://www.apache.org/licenses/LICENSE-2.0
%
% Development until 2012 by Earth Systems Science Computational Center (ESSCC)
% Development 2012-2013 by School of Earth Sciences
% Development from 2014 by Centre for Geoscience Computing (GeoComp)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Example 1: One Dimensional Heat Diffusion in Granite}
\label{Sec:1DHDv00}

The first model consists of two blocks of isotropic material, for instance
granite, sitting next to each other (\autoref{fig:onedgbmodel}).
Initial temperature in \textit{Block 1} is \verb|T1| and in \textit{Block 2} is
\verb|T2|.
We assume that the system is insulated.
What would happen to the temperature distribution in each block over time?
Intuition tells us that heat will be transported from the hotter block to the
cooler one until both
blocks have the same temperature.

\begin{figure}[ht]
\centerline{\includegraphics[width=4.in]{figures/onedheatdiff001}}
\caption{Example 1: Temperature differential along a single interface between
two granite blocks.}
\label{fig:onedgbmodel}
\end{figure}

\subsection{1D Heat Diffusion Equation}
We can model the heat distribution of this problem over time using the one
dimensional heat diffusion equation\footnote{A detailed discussion on how the
heat diffusion equation is derived can be found at
\url{
http://online.redwoods.edu/instruct/darnold/DEProj/sp02/AbeRichards/paper.pdf}};
which is defined as:
\begin{equation}
\rho c_p \frac{\partial T}{\partial t} - \kappa \frac{\partial^{2}
T}{\partial x^{2}} = q_H
\label{eqn:hd}
\end{equation}
where $\rho$ is the material density, $c_p$ is the specific heat and
$\kappa$ is the thermal
conductivity\footnote{A list of some common thermal conductivities is available
from Wikipedia
\url{http://en.wikipedia.org/wiki/List_of_thermal_conductivities}}. Here we
assume that these material
parameters are \textbf{constant}.
The heat source is defined by the right hand side of \refEq{eqn:hd} as
$q_{H}$; this can take the form of a constant or a function of time and
space. For example $q_{H} = q_{0}e^{-\gamma t}$ where we have
the output of our heat source decaying with time. There are also two partial
derivatives in \refEq{eqn:hd}; $\frac{\partial T}{\partial t}$ describes the
change in temperature with time while $\frac{\partial ^2 T}{\partial x^2}$ is
the spatial change of temperature. As there is only a single spatial dimension
to our problem, our temperature solution $T$ is only dependent on the time $t$
and our signed distance from the block-block interface $x$.

\subsection{PDEs and the General Form}
It is possible to solve PDE \refEq{eqn:hd} analytically and obtain an exact
solution to our problem. However, it is not always practical to solve the
problem this way. Alternatively, computers can be used to find the solution. To
do this, a numerical approach is required to discretise
the PDE \refEq{eqn:hd} across time and space, this reduces the problem to a
finite number of equations for a finite number of spatial points and time steps.
These parameters together define the model. While discretisation introduces
approximations and a degree of error, a sufficiently sampled model is generally
accurate enough to satisfy the accuracy requirements for the final solution.

Firstly, we discretise the PDE \refEq{eqn:hd} in time. This leaves us with a
steady linear PDE which involves spatial derivatives only and needs to be solved
in each time step to progress in time. \esc can help us here.

For time discretisation we use the Backward Euler approximation
scheme\footnote{see \url{http://en.wikipedia.org/wiki/Euler_method}}. It is
based on the approximation
\begin{equation}
\frac{\partial T(t)}{\partial t} \approx \frac{T(t)-T(t-h)}{h}
\label{eqn:beuler}
\end{equation}
for  $\frac{\partial T}{\partial t}$  at time $t$
where $h$ is the time step size. This can also be written as;
\begin{equation}
\frac{\partial T}{\partial t}(t^{(n)}) \approx \frac{T^{(n)} - T^{(n-1)}}{h}
\label{eqn:Tbeuler}
\end{equation}
where the upper index $n$ denotes the n\textsuperscript{th} time step. So one
has
\begin{equation}
\begin{array}{rcl}
t^{(n)} & = & t^{(n-1)}+h \\
T^{(n)} & = & T(t^{(n-1)}) \\
\end{array}
\label{eqn:Neuler}
\end{equation}
Substituting \refEq{eqn:Tbeuler} into \refEq{eqn:hd} we get;
\begin{equation}
\frac{\rho c_p}{h} (T^{(n)} - T^{(n-1)}) - \kappa \frac{\partial^{2}
T^{(n)}}{\partial x^{2}} = q_H
\label{eqn:hddisc}
\end{equation}
Notice that we evaluate the spatial derivative term at the current time
$t^{(n)}$ - therefore the name \textbf{backward Euler} scheme. Alternatively,
one can evaluate the spatial derivative term at the previous time $t^{(n-1)}$.
This approach is called the \textbf{forward Euler} scheme. This scheme can
provide some computational advantages, which
are not discussed here. However, the \textbf{forward Euler} scheme has a major
disadvantage. Namely, depending on the
material parameters as well as the domain discretization of the spatial
derivative term, the time step size $h$ needs to be chosen sufficiently small to
achieve a stable temperature when progressing in time. Stability is achieved if
the temperature does not grow beyond its initial bounds and becomes
non-physical.
The backward Euler scheme, which we use here, is unconditionally stable meaning
that under the assumption of a
physically correct problem set-up the temperature approximation remains physical
for all time steps.
The user needs to keep in mind that the discretisation error introduced by
\refEq{eqn:beuler}
is sufficiently small, thus a good approximation of the true temperature is
computed. It is therefore very important that any results are viewed with
caution. For example, one may compare the results for different time and
spatial step sizes.

To get the temperature $T^{(n)}$ at time $t^{(n)}$ we need to solve the linear
differential equation \refEq{eqn:hddisc} which only includes spatial
derivatives. To solve this problem we want to use \esc.

In \esc any given PDE can be described by the general form. For the purpose of
this introduction we illustrate a simpler version of the general form for full
linear PDEs which is available in the \esc user's guide. A simplified form that
suits our heat diffusion problem\footnote{The form in the \esc users guide which
uses the Einstein convention is written as
$-(A_{jl} u_{,l})_{,j}+D u =Y$}
is described by;
\begin{equation}\label{eqn:commonform nabla}
-\nabla\cdot(A\cdot\nabla u) + Du = f
\end{equation}
where $A$, $D$ and $f$ are known values and $u$ is the unknown solution. The
symbol $\nabla$ which is called the \textit{Nabla operator} or \textit{del
operator} represents
the spatial derivative of its subject - in this case $u$. Lets assume for a
moment that we deal with a one-dimensional problem then ;
\begin{equation}
\nabla = \frac{\partial}{\partial x}
\end{equation}
and we can write \refEq{eqn:commonform nabla} as;
\begin{equation}\label{eqn:commonform}
-A\frac{\partial^{2}u}{\partial x^{2}} + Du = f
\end{equation}
if $A$ is constant. To match this simplified general form to our problem
\refEq{eqn:hddisc}
we rearrange \refEq{eqn:hddisc};
\begin{equation}
\frac{\rho c_p}{h} T^{(n)} - \kappa \frac{\partial^2 T^{(n)}}{\partial
x^2} = q_H +  \frac{\rho c_p}{h} T^{(n-1)}
\label{eqn:hdgenf}
\end{equation}
The PDE is now in a form that satisfies \refEq{eqn:commonform nabla} which is
required for \esc to solve our PDE. This can be done by generating a solution
for successive increments in the time nodes $t^{(n)}$ where
$t^{(0)}=0$ and  $t^{(n)}=t^{(n-1)}+h$ where $h>0$ is the step size and assumed
to be constant.
In the following the upper index ${(n)}$ refers to a value at time $t^{(n)}$.
Finally, by comparing \refEq{eqn:hdgenf} with \refEq{eqn:commonform} one can see
that;
\begin{equation}\label{ESCRIPT SET}
u=T^{(n)};
A = \kappa; D = \frac{\rho c _{p}}{h}; f = q _{H} + \frac{\rho
c_p}{h} T^{(n-1)}
\end{equation}

\subsection{Boundary Conditions}
\label{SEC BOUNDARY COND}
With the PDE sufficiently modified, consideration must now be given to the
boundary conditions of our model. Typically there are two main types of boundary
conditions known as \textbf{Neumann} and \textbf{Dirichlet} boundary
conditions\footnote{More information on Boundary Conditions is available at
Wikipedia \url{http://en.wikipedia.org/wiki/Boundary_conditions}},
respectively.
A \textbf{Dirichlet boundary condition} is conceptually simpler and is used to
prescribe a known value to the unknown solution (in our example the temperature)
on parts of the boundary or on the entire boundary of the region of interest.
We discuss the Dirichlet boundary condition in our second example presented in
Section~\ref{Sec:1DHDv0}.

However, for this example we have made the model assumption that the system is
insulated, so we need to add an appropriate boundary condition to prevent
any loss or inflow of energy at the boundary of our domain. Mathematically this
is expressed by prescribing
the heat flux $\kappa \frac{\partial T}{\partial x}$  to zero. In our simplified
one dimensional model this is expressed
in the form;
\begin{equation}
\kappa \frac{\partial T}{\partial x}  = 0
\end{equation}
or in a more general case as
\begin{equation}\label{NEUMAN 1}
\kappa \nabla T \cdot n  = 0
\end{equation}
where $n$  is the outer normal field \index{outer normal field} at the surface
of the domain.
The $\cdot$ (dot) refers to the dot product of the vectors $\nabla T$ and $n$.
In fact, the term $\nabla T \cdot n$ is the normal derivative of
the temperature $T$. Other notations used here are\footnote{The \esc notation
for the normal
derivative is $T_{,i} n_i$.};
\begin{equation}
\nabla T \cdot n  = \frac{\partial T}{\partial n} \; .
\end{equation}
A condition of the type \refEq{NEUMAN 1} defines a \textbf{Neumann boundary
condition} for the PDE.

The PDE \refEq{eqn:hdgenf}
and the Neumann boundary condition~\ref{eqn:hdgenf} (potentially together with
the Dirichlet boundary conditions)  define a \textbf{boundary value problem}.
It is the nature of a boundary value problem to allow making statements about
the solution in the
interior of the domain from information known on the boundary only. In most
cases we use the term partial differential equation but in fact it is a
boundary value problem.
It is important to keep in mind that boundary conditions need to be complete and
consistent in the sense that
at any point on the boundary either a Dirichlet or a Neumann boundary condition
must be set.

Conveniently, \esc makes a default assumption on the boundary conditions which
the user may modify where appropriate.
For a problem of the form in~\refEq{eqn:commonform nabla} the default
condition\footnote{In the \esc user guide which uses the Einstein convention
this is written as
$n_{j}A_{jl} u_{,l}=0$.} is;
\begin{equation}\label{NEUMAN 2}
-n\cdot A \cdot\nabla u = 0
\end{equation}
which is used everywhere on the boundary. Again $n$ denotes the outer normal
field.
Notice that the coefficient $A$ is the same as in the \esc
PDE~\ref{eqn:commonform nabla}.
With the settings for the coefficients we have already identified in
\refEq{ESCRIPT SET} this
condition translates into
\begin{equation}\label{NEUMAN 2b}
\kappa \frac{\partial T}{\partial x} = 0
\end{equation}
for the boundary of the domain. This is identical to the Neumann boundary
condition we want to set. \esc will take care of this condition for us. We
discuss the Dirichlet boundary condition later.

\subsection{Outline of the Implementation}
\label{sec:outline}
To solve the heat diffusion equation (\refEq{eqn:hd}) we write a simple \pyt
script. At this point we assume that you have some basic understanding of the
\pyt programming language. If not, there are some pointers and links available
in Section \ref{sec:escpybas}. The script (discussed in \refSec{sec:key}) has
four major steps. Firstly, we need to define the domain where we want to
calculate the temperature. For our problem this is the joint blocks of granite
which has a rectangular shape. Secondly, we need to define the PDE to solve in
each time step to get the updated temperature. Thirdly, we need to define the
coefficients of the PDE and finally we need to solve the PDE. The last two steps
need to be repeated until the final time marker has been reached. The work flow
is described in \reffig{fig:wf}.
% \begin{enumerate}
%  \item create domain
%  \item create PDE
%  \item while end time not reached:
% \begin{enumerate}
%  \item set PDE coefficients
%  \item solve PDE
%  \item update time marker
% \end{enumerate}
% \item end of calculation
% \end{enumerate}

\begin{figure}[h!]
 \centering
   \includegraphics[width=1in]{figures/workflow.png}
   \caption{Workflow for developing an \esc model and solution}
   \label{fig:wf}
\end{figure}

In the terminology of \pyt, the domain and PDE are represented by
\textbf{objects}. The nice feature of an object is that it is defined by its
usage and features
rather than its actual representation. So we will create a domain object to
describe the geometry of the two
granite blocks. Then we define PDEs and spatially distributed values such as the
temperature
on this domain. Similarly, to define a PDE object we use the fact that one needs
only to define the coefficients of the PDE and solve the PDE. The PDE object has
advanced features, but these are not required in simple cases.

\begin{figure}[htp]
 \centering
   \includegraphics[width=6in]{figures/functionspace.pdf}
   \caption{\esc domain construction overview}
   \label{fig:fs}
\end{figure}

\subsection{The Domain Constructor in \esc}
\label{ss:domcon}
Whilst it is not strictly relevant or necessary, a better understanding of
how values are spatially distributed (\textit{e.g.} Temperature) and how PDE
coefficients are interpreted in \esc can be helpful.

There are various ways to construct domain objects. The simplest form is a
rectangular shaped region with a length and height. There is
a ready to use function for this named \verb rectangle(). Besides the spatial
dimensions this function requires to specify the number of
elements or cells to be used along the length and height, see \reffig{fig:fs}.
Any spatially distributed value
and the PDE is represented in discrete form using this element
representation\footnote{We use the finite element method (FEM), see
\url{http://en.wikipedia.org/wiki/Finite_element_method} for details.}.
Therefore we will have access to an approximation of the true PDE solution
only.
The quality of the approximation depends - besides other factors - mainly on the
number of elements being used. In fact, the
approximation becomes better when more elements are used. However, computational
cost grows with the number of
elements being used. It is therefore important that you find the right balance
between the demand in accuracy and acceptable resource usage.

In general, one can think about a domain object as a composition of nodes and
elements.
As shown in \reffig{fig:fs}, an element is defined by the nodes that are used to
describe its vertices.
To represent spatially distributed values the user can use
the values at the nodes, at the elements in the interior of the domain or at the
elements located on the surface of the domain.
The different approach used to represent values is called \textbf{function
space} and is attached to all objects
in \esc representing a spatially distributed value such as the solution of
a PDE. The three function spaces we use at the moment are;
\begin{enumerate}
\item the nodes, called by \verb|ContinuousFunction(domain)| ;
\item the elements/cells, called by \verb|Function(domain)| ; and
\item the boundary, called by \verb|FunctionOnBoundary(domain)|.
\end{enumerate}
A function space object such as \verb|ContinuousFunction(domain)| has the method
\verb|getX| attached to it. This method returns the
location of the so-called \textbf{sample points} used to represent values of the
particular function space. So the
call \verb|ContinuousFunction(domain).getX()| will return the coordinates of the
nodes used to describe the domain while
\verb|Function(domain).getX()| returns the coordinates of numerical
integration points within elements, see \reffig{fig:fs}.

This distinction between different representations of spatially distributed
values
is important in order to be able to vary the degrees of smoothness in a PDE
problem.
The coefficients of a PDE do not need to be continuous, thus this qualifies as a
\verb|Function()| type.
On the other hand a temperature distribution must be continuous and needs to be
represented with a \verb|ContinuousFunction()| function space.
An influx may only be defined at the boundary and is therefore a
\verb|FunctionOnBoundary()| object.
\esc allows certain transformations of the function spaces. A
\verb|ContinuousFunction()| can be transformed into a
\verb|FunctionOnBoundary()| or \verb|Function()|. On the other hand there is
not enough information in a \verb|FunctionOnBoundary()| to transform it to a
\verb|ContinuousFunction()|.
These transformations, which are called \textbf{interpolation} are invoked
automatically by \esc if needed.

Later in this introduction we discuss how
to define specific areas of geometry with different materials which are
represented by different material coefficients such as the
thermal conductivities $\kappa$. A very powerful technique to define these types
of PDE
coefficients is tagging. Blocks of materials and boundaries can be named and
values can be defined on subregions based on their names.
This is a method for simplifying PDE coefficient and flux definitions. It makes
scripting much easier and we will discuss this technique in
Section~\ref{STEADY-STATE HEAT REFRACTION}.

\subsection{A Clarification for the 1D Case}
\label{SEC: 1D CLARIFICATION}
It is necessary for clarification that we revisit our general PDE from
\refeq{eqn:commonform nabla} for a two dimensional domain. \esc is inherently
designed to solve problems that are multi-dimensional and so
\refEq{eqn:commonform nabla} needs to be read as a higher dimensional problem.
In the case of two spatial dimensions the \textit{Nabla operator} has in fact
two components $\nabla = (\frac{\partial}{\partial x}, \frac{\partial}{\partial
y})$. Assuming the coefficient $A$ is constant, the \refEq{eqn:commonform nabla}
takes the following form;
\begin{equation}\label{eqn:commonform2D}
-A_{00}\frac{\partial^{2}u}{\partial x^{2}}
-A_{01}\frac{\partial^{2}u}{\partial x\partial y}
-A_{10}\frac{\partial^{2}u}{\partial y\partial x}
-A_{11}\frac{\partial^{2}u}{\partial y^{2}}
+ Du = f
\end{equation}
Notice that for the higher dimensional case $A$ becomes a matrix. It is also
important to notice that the usage of the Nabla operator creates
a compact formulation which is also independent from the spatial dimension.
To make the general PDE \refEq{eqn:commonform2D} one dimensional as
shown in \refEq{eqn:commonform} we need to set
\begin{equation}
A_{00}=A; A_{01}=A_{10}=A_{11}=0
\end{equation}

\subsection{Developing a PDE Solution Script}
\label{sec:key}
\sslist{example01a.py}
We write a simple \pyt script which uses the \modescript, \modfinley and \modmpl
modules.
By developing a script for \esc, the heat diffusion equation can be solved at
successive time steps for a predefined period using our general form
\refEq{eqn:hdgenf}. Firstly it is necessary to import all the
libraries\footnote{The libraries contain predefined scripts that are required to
solve certain problems, these can be simple like sine and cosine functions or
more complicated like those from our \esc library.}
that we will require.
\begin{python}
from esys.escript import *
# This defines the LinearPDE module as LinearPDE
from esys.escript.linearPDEs import LinearPDE
# This imports the rectangle domain function from finley.
from esys.finley import Rectangle
# A useful unit handling package which will make sure all our units
# match up in the equations under SI.
from esys.escript.unitsSI import *
\end{python}
It is generally a good idea to import all of the \modescript library, although
if the functions and classes required are known they can be specified
individually. The function \verb|LinearPDE| has been imported explicitly for
ease of use later in the script. \verb|Rectangle| is going to be our type of
domain. The module \verb|unitsSI| provides support for SI unit definitions with
our variables.

Once our library dependencies have been established, defining the problem
specific variables is the next step. In general the number of variables needed
will vary between problems. These variables belong to two categories. They are
either directly related to the PDE and can be used as inputs into the \esc
solver, or they are script variables used to control internal functions and
iterations in our problem. For this PDE there are a number of constants which
need values. Firstly, the domain upon which we wish to solve our problem needs
to be defined. There are different types of domains in \modescript which we
demonstrate in later tutorials but for our granite blocks, we simply use a
rectangular domain.

Using a rectangular domain simplifies our granite blocks (which would in reality
be a \textit{3D} object) into a single dimension. The granite blocks will have a
lengthways cross section that looks like a rectangle.  As a result we do not
need to model the volume of the block due to symmetry. There are four arguments
we must consider when we decide to create a rectangular domain, the domain
\textit{length}, \textit{width} and \textit{step size} in each direction. When
defining the size of our problem it will help us determine appropriate values
for our model arguments. If we make our dimensions large but our step sizes very
small we increase the accuracy of our solution. Unfortunately we also increase
the number of calculations that must be solved per time step. This means more
computational time is required to produce a solution. In this \textit{1D}
problem, the bar is defined as being 1 metre long. An appropriate step size
\verb|ndx| would be 1 to 10\% of the length. Our \verb|ndy| needs only be 1,
this is because our problem stipulates no partial derivatives in the $y$
direction.
Thus the temperature does not vary with $y$. Hence, the model parameters can be
defined as follows; note we have used the \verb|unitsSI| convention to make sure
all our input units are converted to SI.
\begin{python}
mx = 500.*m #meters - model length
my = 100.*m #meters - model width
ndx = 50 # mesh steps in x direction
ndy = 1 # mesh steps in y direction
boundloc = mx/2 # location of boundary between the two blocks
\end{python}
The material constants and the temperature variables must also be defined. For
the granite in the model they are defined as:
\begin{python}
#PDE related
rho = 2750. *kg/m**3 #kg/m^{3} density of iron
cp = 790.*J/(kg*K) # J/Kg.K thermal capacity
rhocp = rho*cp
kappa = 2.2*W/m/K   # watts/m.Kthermal conductivity
qH=0 * J/(sec*m**3) # J/(sec.m^{3}) no heat source
T1=20 * Celsius # initial temperature at Block 1
T2=2273. * Celsius # base temperature at Block 2
\end{python}
Finally, to control our script we will have to specify our timing controls and
where we would like to save the output from the solver. This is simple enough:
\begin{python}
t=0 * day  # our start time, usually zero
tend=50 * yr # - time to end simulation
outputs = 200 # number of time steps required.
h=(tend-t)/outputs #size of time step
#user warning statement
print("Expected Number of time outputs is: ", (tend-t)/h)
i=0 #loop counter
\end{python}
Now that we know our inputs we will build a domain using the
\verb|Rectangle()| function from \FINLEY. The four arguments allow us to
define our domain \verb|model| as:
\begin{python}
#generate domain using rectangle
blocks = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)
\end{python}
\verb|blocks| now describes a domain in the manner of Section \ref{ss:domcon}.

With a domain and all the required variables established, it is now possible to
set up our PDE so that it can be solved by \esc. The first step is to define the
type of PDE that we are trying to solve in each time step. In this example it is
a single linear PDE\footnote{in contrast to a system of PDEs which we discuss
later.}. We also need to state the values of our general form variables.
\begin{python}
mypde=LinearPDE(blocks)
A=zeros((2,2)))
A[0,0]=kappa
mypde.setValue(A=A, D=rhocp/h)
\end{python}
In many cases it may be possible to decrease the computational time of the
solver if the PDE is symmetric.
Symmetry of a PDE is defined by;
\begin{equation}\label{eqn:symm}
A_{jl}=A_{lj}
\end{equation}
Symmetry is only dependent on the $A$ coefficient in the general form and the
other coefficients $D$ as well as the right hand side $Y$. From the above
definition we can see that our PDE is symmetric. The \verb|LinearPDE| class
provides the method \method{checkSymmetry} to check if the given PDE is
symmetric. As our PDE is symmetrical we enable symmetry via;
\begin{python}
myPDE.setSymmetryOn()
\end{python}
Next we need to establish the initial temperature distribution \verb|T|. We need
to
assign the value \verb|T1| to all sample points left to the contact interface at
$x_{0}=\frac{mx}{2}$
and the value \verb|T2| right to the contact interface. \esc
provides the \verb|whereNegative| function to construct this. More
specifically, \verb|whereNegative| returns the value $1$ at those sample points
where the argument has a negative value. Otherwise zero is returned.
If \verb|x| are the $x_{0}$
coordinates of the sample points used to represent the temperature distribution
then \verb|x[0]-boundloc| gives us a negative value for
all sample points left to the interface and non-negative value to
the right of the interface. So with;
\begin{python}
# ... set initial temperature ....
T= T1*whereNegative(x[0]-boundloc)+T2*(1-whereNegative(x[0]-boundloc))
\end{python}
we get the desired temperature distribution. To get the actual sample points
\verb|x| we use the \verb|getX()| method of the function space
\verb|Solution(blocks)| which is used to represent the solution of a PDE;
\begin{python}
x=Solution(blocks).getX()
\end{python}
As \verb|x| are the sample points for the function space
\verb|Solution(blocks)|
the initial temperature \verb|T| is using these sample points for
representation.
Although \esc is trying to be forgiving with the choice of sample points and to
convert
where necessary the adjustment of the function space is not always possible. So
it is advisable to make a careful choice on the function space used.

Finally we initialise an iteration loop to solve our PDE for all the time steps
we specified in the variable section. As the right hand side of the general form
is dependent on the previous values for temperature \verb T  across the bar this
must be updated in the loop. Our output at each time step is \verb T  the heat
distribution and \verb totT  the total heat in the system.
\begin{python}
while t < tend:
	i+=1 #increment the counter
	t+=h #increment the current time
	mypde.setValue(Y=qH+rhocp/h*T) # set variable PDE coefficients
	T=mypde.getSolution() #get the PDE solution
	totE = integrate(rhocp*T) #get the total heat (energy) in the system
\end{python}
The last statement in this script calculates the total energy in the system as
the volume integral of $\rho c_{p} T$ over the block.
As the blocks are insulated no energy should be lost or added.
The total energy should stay constant for the example discussed here.

\subsection{Running the Script}
The script presented so far is available under
\verb|example01a.py|. You can edit this file with your favourite text editor.
On most operating systems\footnote{The \texttt{run-escript} launcher is not
supported under {\it MS Windows}.} you can use the
\program{run-escript} command
to launch {\it escript} scripts. For the example script use;
\begin{verbatim}
run-escript example01a.py
\end{verbatim}
The program will print a progress report. Alternatively, you can use
the python interpreter directly;
\begin{verbatim}
python example01a.py
\end{verbatim}
if the system is configured correctly (please talk to your system
administrator).

\subsection{Plotting the Total Energy}
\sslist{example01b.py}

\esc does not include its own plotting capabilities. However, it is possible to
use a variety of free \pyt packages for visualisation.
Two types will be demonstrated in this cookbook;
\mpl\footnote{\url{http://matplotlib.sourceforge.net/}} and
\verb|VTK|\footnote{\url{http://www.vtk.org/}}.
The \mpl package is a component of SciPy\footnote{\url{http://www.scipy.org}}
and is good for basic graphs and plots.
For more complex visualisation tasks, in particular two and three dimensional
problems we recommend the use of more advanced tools. For instance, \mayavi
\footnote{\url{http://code.enthought.com/projects/mayavi/}}
which is based upon the \verb|VTK| toolkit. The usage of \verb|VTK| based
visualisation is discussed in Chapter~\ref{Sec:2DHD} which focuses on a two
dimensional PDE.

For our simple granite block problem, we have two plotting tasks. Firstly, we
are interested in showing the
behaviour of the total energy over time and secondly, how the temperature
distribution within the block is developing over time.
Let us start with the first task.

The idea is to create a record of the time marks and the corresponding total
energies observed.
\pyt provides the concept of lists for this. Before
the time loop is opened we create empty lists for the time marks \verb|t_list|
and the total energies \verb|E_list|.
After the new temperature has been calculated by solving the PDE we append the
new time marker and the total energy value for that time
to the corresponding list using the \verb|append| method. With these
modifications our script looks as follows:
\begin{python}
t_list=[]
E_list=[]
# ... start iteration:
while t<tend:
      t+=h
      mypde.setValue(Y=qH+rhocp/h*T) # set variable PDE coefficients
      T=mypde.getSolution() #get the PDE solution
      totE=integrate(rhocp*T)
      t_list.append(t)   # add current time mark to record
      E_list.append(totE) # add current total energy to record
\end{python}
To plot $t$ over $totE$ we use \mpl a module contained within \pylab which needs
to be loaded before use;
\begin{python}
import pylab as pl # plotting package.
\end{python}
Here we are not using \verb|from pylab import *| in order to avoid name
clashes for function names within \esc.

The following statements are added to the script after the time loop has been
completed;
\begin{python}
pl.plot(t_list,E_list)
pl.title("Total Energy")
pl.axis([0,max(t_list),0,max(E_list)*1.1])
pl.savefig("totE.png")
\end{python}
The first statement hands over the time marks and corresponding total energies
to the plotter.
The second statement sets the title for the plot. The third statement
sets the axis ranges. In most cases these are set appropriately by the plotter.

The last statement generates the plot and writes the result into the file
\verb|totE.png| which can be displayed by (almost) any image viewer.
As expected the total energy is constant over time, see
\reffig{fig:onedheatout1}.

\begin{figure}[ht]
\begin{center}
\includegraphics[width=4in]{figures/ttblockspyplot150}
\caption{Example 1b: Total Energy in the Blocks over Time (in seconds)}
\label{fig:onedheatout1}
\end{center}
\end{figure}
\clearpage

\subsection{Plotting the Temperature Distribution}
\label{sec: plot T}
\sslist{example01c.py}
For plotting the spatial distribution of the temperature we need to modify the
strategy we have used for the total energy.
Instead of producing a final plot at the end we will generate a
picture at each time step which can be browsed as a slide show or composed into
a movie.
The first problem we encounter is that if we produce an image at each time step
we need to make sure that the images previously generated are not overwritten.

To develop an incrementing file name we can use the following convention. It is
convenient to put all image files showing the same variable - in our case the
temperature distribution - into a separate directory.
As part of the \verb|os| module\footnote{The \texttt{os} module provides
a powerful interface to interact with the operating system, see
\url{http://docs.python.org/library/os.html}.} \pyt
provides the \verb|os.path.join| command to build file and directory names in a
platform independent way. Assuming that
\verb|save_path| is the name of the directory we want to put the results in the
command is;
\begin{python}
import os
os.path.join(save_path, "tempT%03d.png"%i )
\end{python}
where \verb|i| is the time step counter.
There are two arguments to the \verb|join| command. The \verb|save_path|
variable is a predefined string pointing to the directory we want to save our
data, for example a single sub-folder called \verb|data| would be defined by;
\begin{verbatim}
save_path = "data"
\end{verbatim}
while a sub-folder of \verb|data| called \verb|example01| would be defined by;
\begin{verbatim}
save_path = os.path.join("data","example01")
\end{verbatim}
The second argument of \verb|join| contains a string which is the file
name or subdirectory name. We can use the operator \verb|%| to use the value of
\verb|i| as part of our filename. The sub-string \verb|%03d| indicates that we
want to substitute a value into the name;
\begin{itemize}
 \item \verb 0  means that small numbers should have leading zeroes;
 \item \verb 3  means that numbers should be written using at least 3 digits;
and
 \item \verb d  means that the value to substitute will be a decimal integer.
\end{itemize}

To actually substitute the value of \verb|i| into the name write \verb|%i| after
the string.
When done correctly, the output files from this command will be placed in the
directory defined by \verb save_path  as;
\begin{verbatim}
blockspyplot001.png
blockspyplot002.png
blockspyplot003.png
...
\end{verbatim}
and so on.

A sub-folder check/constructor is available in \esc. The command;
\begin{verbatim}
mkDir(save_path)
\end{verbatim}
will check for the existence of \verb save_path  and if missing, create the
required directories.

We start by modifying our solution script.
Prior to the \verb|while| loop we need to extract our finite solution
points to a data object that is compatible with \mpl. First we create the node
coordinates of the sample points used to represent
the temperature as a \pyt list of tuples or a \numpy array as requested by the
plotting function.
We need to convert the array \verb|x| previously set as
\verb|Solution(blocks).getX()| into a \pyt list
and then to a \numpy array. The $x_{0}$ component is then extracted via
an array slice to the variable \verb|plx|;
\begin{python}
import numpy as np # array package.
#convert solution points for plotting
plx = x.toListOfTuples()
plx = np.array(plx) # convert to tuple to numpy array
plx = plx[:,0] # extract x locations
\end{python}

\begin{figure}
\begin{center}
\includegraphics[width=4in]{figures/blockspyplot001}
\includegraphics[width=4in]{figures/blockspyplot050}
\includegraphics[width=4in]{figures/blockspyplot200}
\caption{Example 1c: Temperature ($T$) distribution in the blocks at time steps
$1$, $50$ and $200$}
\label{fig:onedheatout}
\end{center}
\end{figure}
\clearpage

We use the same techniques provided by \mpl as we have used to plot the total
energy over time.
For each time step we generate a plot of the temperature distribution and save
each to a file.
The following is appended to the end of the \verb|while| loop and creates one
figure of the temperature distribution. We start by converting the solution to a
tuple and then plotting this against our \textit{x coordinates} \verb|plx| we
have generated before. We add a title to the diagram before it is rendered into
a file.
Finally, the figure is saved to a \verb|*.png| file and cleared for the
following iteration.
\begin{python}
# ... start iteration:
while t<tend:
      i+=1
      t+=h
      mypde.setValue(Y=qH+rhocp/h*T)
      T=mypde.getSolution()
      totE=integrate(rhocp*T)
      print("time step %s at t=%e days completed. total energy = %e."%(i,t/day,totE))
      t_list.append(t)
      E_list.append(totE)

      #establish figure 1 for temperature vs x plots
      tempT = T.toListOfTuples()
      pl.figure(1) #current figure
      pl.plot(plx,tempT) #plot solution
      # add title
      pl.axis([0,mx,T1*.9,T2*1.1])
      pl.title("Temperature across blocks at time %d days"%(t/day))
      #save figure to file
      pl.savefig(os.path.join(save_path,"tempT", "blockspyplot%03d.png"%i))
      pl.clf() #clear figure
\end{python}
Some results are shown in \reffig{fig:onedheatout}.

\subsection{Making a Video}
Our saved plots from the previous section can be cast into a video using the
following command appended to the end of the script. The \verb mencoder command
is not available on every platform, so some users need to use an alternative
video encoder.
\begin{python}
# compile the *.png files to create a *.avi video that shows T change
# with time. This operation uses Linux mencoder. For other operating
# systems it is possible to use your favourite video compiler to
# convert image files to videos.

os.system("mencoder mf://"+save_path+"/tempT"+"/*.png -mf type=png:\
           w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
           example01tempT.avi")
\end{python}


\documentclass{article}
\pagestyle{empty}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{graphicx}
\usepackage{multicol}
\setlength{\oddsidemargin}{0in} \setlength{\evensidemargin}{0in}
\setlength{\topmargin}{0in} \setlength{\textheight}{8.5in}
\setlength{\textwidth}{6.5in}

\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
	\hskip -\arraycolsep
	\let\@ifnextchar\new@ifnextchar
	\array{#1}}
\makeatother

\begin{document}
\begin{flushleft}
	\bfseries{MATH 260, Linear Systems and Matrices, Fall `14}\\
	\bfseries{Activity 10:  Introduction to Eigenvalues and Eigenvectors}\\
	%\bfseries{Honor Code:} \hspace{3.5in}\bfseries{Names:}\\
\end{flushleft}

\begin{flushleft}

\section*{Warm-up:  Matrix Multiplication}

Given the matrix
$\textbf{A}=
\begin{bmatrix}
1 & 1 \\ 4&1
\end{bmatrix}$
 and the vectors:
$\vec{v}_1=\begin{bmatrix} 2 \\ 2 \end{bmatrix}$,
$\vec{v}_2=\begin{bmatrix} 1 \\ 2 \end{bmatrix}$,
$\vec{v}_3=\begin{bmatrix} -2 \\ 0 \end{bmatrix}$,
$\vec{v}_4=\begin{bmatrix} -1 \\ -4 \end{bmatrix}$,
$\vec{v}_5=\begin{bmatrix} 1 \\ -2 \end{bmatrix}$.

\vspace{0.1in}

Find $\textbf{A}\vec{v}_i$ for $i=1,...,5$.

\vspace{2in}

\newpage

\section*{Visualizing Eigenvectors}

1) Sketch each $\vec{v}_i$ and the result of $\textbf{A}\vec{v}_i$ from the warm-up in the $xy$-plane. You should have 5 pictures when you are done. Be sure to clearly label which was your initial, and which your resultant vector.

\vspace{4in}

2) Two of the $v_i$ vectors behaved differently, which ones? Describe how they behaved differently.

\vspace{1in}

3) For the two vectors that behaved differently, you should be able to write the eigenvector equation: $\textbf{A}\vec{v}=\lambda \vec{v}$. Find $\lambda_i$ for each pair of vectors.

\vspace{.75in}

\newpage

\section*{Practice}

Let's practice finding eigenvalues and eigenvectors.

\vspace{0.1in}

4) For each matrix, take the determinant $|\textbf{A}- \lambda \textbf{I}  | $ then set the resulting polynomial equal to 0 and solve for $\lambda$. (They should all be factorable, so even the $3 \times 3$s should not be too hard to do). Once you have found ALL the eigenvalues ($\lambda$s) find at least one eigenvector for each matrix.\\

\begin{center}

$\textbf{W}=\begin{bmatrix}[rr] 1 & 4\\-4&11 \end{bmatrix}$
\hspace{0.3in}
$\textbf{G}=\begin{bmatrix}[rrr] 1 & 1 & -2\\ -1 & 2 & 1\\ 0 & 1 & -1 \end{bmatrix}$
\hspace{0.3in}
$\textbf{R}=\begin{bmatrix}[rrr] 1 & 2 & 3 \\ 0 & 4 & 5 \\ 0 & 0 & 6 \end{bmatrix}$

\end{center}

\vspace{6in}

5) Go back and look at $\textbf{R}$.  What do you notice about the eigenvalues you found for \textbf{R}?  Can you generalize this to a property of certain types of matrices?

\newpage

\section*{Some Properties of Eigenvalues}

\textit{ Note: We will find these for $2 \times 2$ matrices, but these properties generalize to any $n \times n$ matrix. }

\vspace{0.2in}

6) We find eigenvalues by solving: $|\textbf{A}- \lambda \textbf{I}  | =0 $. What happens if $\lambda = 0 $? What must be true about the matrix \textbf{A} for \textbf{A} to have eigenvalues of 0?

\vspace{1.5in}

7)a) Find $\textbf{W}^{T}$.

\vspace{1in}

b) What is the polynomial from $|\textbf{W}^{T}- \lambda \textbf{I}  | $ and how does it compare to the polynomial for $|\textbf{W}- \lambda \textbf{I}  | $?

\vspace{1in}

c) What can you say about the eigenvalues (and vectors) of $\textbf{A}$ and $\textbf{A}^{T}$?

\vspace{0.75in}

8)a) Find $\textbf{W}^{-1}$.

\vspace{1in}

b) What is the polynomial from $|\textbf{W}^{-1}- \lambda \textbf{I}  | $? Set the polynomial equal to 0, and solve for $\lambda$

\vspace{1in}

c) Look carefully, how do the eigenvalues of $\textbf{W}^{-1}$ relate to the eigenvalues of \textbf{W}?

\end{flushleft}
\end{document}

Fall 2014 - Capaldi A/Activities/Activity10_EigenPart1.tex


\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage[shortlabels]{enumitem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{mathtools}
\usepackage{lmodern}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}
\setlength{\parskip}{0.5em}

\usepackage{clrscode3e}

\title{Recursion: A Brief Introduction}
\author{Competitive Programming UTEC}

\begin{document}

\maketitle

\section{Introduction}

\subsection{Definition}

\textbf{Recursion} occurs when a thing is defined in terms of itself or its type. This definition is a bit weird and doesn't tell us much, so lets consider the following problem. We want to define a rule set for balanced brackets. How would you define that rule set? Recursion allow us to have a very elegant and simple solution: We say that a sequence $L$ of brackets is balanced if $L = L_1(L_2)$, where $L_1$ and $L_2$ are balanced brackets, or $L$ is empty. By defining balanced brackets with smaller balanced brackets we can simplify how we describe what balanced brackets are. A nice definition of recursion therefore is:

\begin{center}
	\parbox{0.7\linewidth}{Recursion (rĭ-kûr’-zhn) \textit{noun.} If you still don't get it, see \textbf{recursion}.}
\end{center}

Recursion appears in many fields of knowledge such as mathematics, linguistics and computer science. This is due to the fact that it is a very powerful tool, that allows us to abstract concepts that might be hard to define and describe them in a very simple way. In computer science when we talk about recursion we usually refer to problems that can be solved by solving smaller instances of themselves. Now you might be asking: what makes a problem \textit{smaller} or \textit{bigger}? Intuitively, we say that a problem is bigger or harder if it takes more time to solve. For example, we can say that going up 5 stairs is harder than going up 3 stairs as we know that the more stairs there are, the longer it is going to take. A more formal definition would be that a problem is said to be smaller if it is fewer iterations away from the base case. This definition will probably make more sense as we further discuss the specifics and terminology of recursion.

\subsection{Conditions for Recursion}

There are three conditions that must be fulfilled for recursion to be possible:

\subsubsection{Recursive Call}

This is probably the easiest of all the conditions. It is obvious that in order to have a recursive algorithm at some point it must call the same algorithm but with different input, in other words, it must have a \textit{recursive call}. We can have more than one recursive call, or have the recursive calls inside conditionals, it is all fair game.

\subsubsection{Base Case}

Lets consider the following algorithm:

\begin{codebox}
\Procname{$\proc{factorial}(n)$}
	\li \Return  $n * \proc{factorial}(n - 1)$
\end{codebox}

It is simple to realize that there is one huge mistake in this algorithm: \textbf{it will never stop running!} This is why we need base cases, as they tell our algorithm when to stop. Formally a base case is a special behaviour inside the recursive algorithm that activates when it receives a specific parameter:

\begin{codebox}
\Procname{$\proc{factorial}(n)$}
\li \If $n = 0$
	\li \Then
		\Return 1
	\End
\li \Return  $n * \proc{factorial}(n - 1)$
\end{codebox}

In this example the base case occurs when $n = 0$ and the function returns $1$. In general the base case can be anything that doesn't use recursion. In other words we must calculate the answer for the base case ``manually'' using an iterative approach. It is important to note that just as there can be more than one recursive call, there can also be more than one base case.

\subsubsection{Convergence}

This is probably the least intuitive of the three conditions, but it is still very simple to understand. Lets consider the following recursive algorithm:

\begin{codebox}
\Procname{$\proc{factorial}(n)$}
\li \If $n = 0$
	\li \Then
		\Return 1
	\End
\li \Return $n * \proc{factorial}(n - 2)$
\end{codebox}

At first sight, it looks like this algorithm has no problems as there is a recursive call and a base case; however, there is a very important detail that has been overlooked. Lets say we call $\proc{factoria}(3)$, this would call $\proc{factorial}(1)$, which would call $\proc{factorial}(-1)$, which would call $\proc{factorial}(-3)$, and so on. Even though we had a base case, our algorithm \textbf{never reached it}, meaning it will be stuck in an infinite loop. This is not good. This is why \textit{convergence} is important, as it ensures that regardless of the parameters we use to call the algorithm, we should reach a base case after a finite number of steps. A corrected version of the algorithm above should consider two base cases, when $n$ reaches $0$ and when $n$ reaches $1$:

\begin{codebox}
\Procname{$\proc{factorial}(n)$}
\li \If $n \leq 1$
	\li \Then
		\Return 1
	\End
\li \Return $n * \proc{factorial}(n - 2)$
\end{codebox}

\section{The Recursion Tree}

Lets consider the following algrithm for finding the $n$-th term of the Fibonacci sequence:

\begin{codebox}
\Procname{$\proc{fibonacci}(n)$}
\li \If $n \leq 1$
	\li \Then
		\Return n
	\End
\li \Return $\proc{fibonacci}(n - 1) + \proc{fibonacci}(n - 2)$
\end{codebox}

What we are going to do now is build the \textit{recursion tree} for this algorithm. This is a graphical representation of how the recursive calls behave and interact, and are very usefull to anlayze the algorithms behaviour. In this case, for $n = 4$ the tree would look like this:

\begin{center}
	\includegraphics[width=0.8\linewidth]{images/fibtree}
\end{center}

Every node in this tree represents a call to the function $\proc{fibonacci}$, and the children of a node represents the recursive calls it make. The nodes that have no children are called leaf nodes, and in recursion trees they represent a base case. The first node is called the \textit{root} and it represents the first call to the algorithm.

Just by looking at this tree try to answer the following questions: Approximately how many nodes the tree will have for $n = 5$? What about for a general values of $n$?

\section{Complexity}

\subsection{Recurrence Relations}

Before we can jump into the algorithmic analysis we need to make a brief stop in the realm of discrete mathematics to understand recurrence relations. A \textit{difference equation} or \textit{recurrence relation} is an equation that tells us how different terms of a sequence relate. Lets consider the equation:

$$a_n = 2 a_{n - 1}, a_0 = 1$$

It is simple to see that this recurrence generates the sequence: $1, 2, 4, 8, 16, \dots 2^n \dots$ As you might have realized, this means that $a_n = 2^n$. This can be formally proved using \textit{mathematical induction}, but right now the intuition of why this is true is more than enough.

This process of turining a recurrence into a sequence defined only in terms of $n$ is usually called \textit{solving} the recurrence, we are esentially finding a recursion-free expression for the sequence. This is very usefull as for some large value of $n$ we don't need to solve for all the smaller values, we can just evaluate the expression. Another technique for solving recurrences is building a recursion tree and adding up to the solution. Some of this equations, like the example above, are very intuitive and simple to solve, while others are much more difficult and require advanced techniques.

\subsection{Time as a Recurrence}

When dealing with iterative algorithms, calculating their time complexity was usually a very trivial task, we only needed to count the number of instructions that were going to be executed. This changes bit with recursion as it might be harder to tell how many times a given instruction will be executed, or how many times the recursive function will be called. For this reason, it is fitting that in order to analyze the time complexity of a recursive algorithm we use a recursive function. In general we can express the time complexity of a recursive algorithm as:

$$T(n) = \sum T(n_i) + T'(n)$$

Where $n_i$ is the parameter used to call the $i$-th recursive call ans $T'$ is the complexity of all non-recursive steps. Lets consider the previous code for finding factorials. We can see that in that case there is only one recursive call and a multiplication. This means that we can express the complexity of that code as:

$$T(n) = T(n - 1) + O(1)$$

This is a very straight forward recurrence that solves to $T(n) = O(n)$. Now lets do something a bit harder, lets try to determine the complexity of the algorithm for finding Fibonacci numbers. It is simple to realize that the complexity can be expressed as:

$$T(n) = T(n - 1) + T(n - 2) + O(1)$$

This recurrence is definitely trickier, so we must find another way of solving it. Lets remember that when we are using \textit{Big Oh} notation, we only want to find an estimate upper bound for the time complexity, therefore a good idea is to start by looking at the recursion tree and guessing. As in almost every step the number of nodes doubles, it is logical to guess that $T(n) = O(2^n)$. However, how can we prove this? A simple and straight forward way of doing it is by substituting our guess and checking:

$$T(n) = 2^{n - 1} + 2^{n - 2} + O(1)$$
$$T(n) \leq 2 * 2^{n - 1} + O(1)$$
$$T(n) \leq 2^n + O(1)$$
$$T(n) = O(2^n)$$

Even though this is not the most formal proof, it is enough to get an idea on how fast our solution will run. Later on, you will learn more advanced techniques for solving recurrences and estimating complexities.

Intuitively we can see that the more recursive calls we have, the bigger our time complexity will be. Therefore we must attempt to always minimize the number of recursive calls. We should also try to minimize the size of the smaller problems we target. However, this might be hard to achieve.

\subsection{Memory}

One important aspect to take into consideration when using recursion is that we are going to implicitly use memory of the stack. This happens because in every recursive call we need to store the current state of the algorithm in order to jump to the next function call. The auxiliary memory we will use in our algorithm will therefore be proportional to the height of our recursion tree, that is, the maximum distance between a leaf node and the root.

If we make too many recursive calls, this can lead to a stack overflow, in other words, we can run out of memory. This is one of the things that we must take into consideration when using recursion, as it can lead to our solution failing when dealing with bigger inputs.

\section{Strategy and Correctness}

Even though the problems that can be solved using recursion are very different, the solution for most of them follows a very similar pattern. First we find the solution for all the smaller problems we need, then we merge this solutions to find the general solution to the problem. Because of the versatility of recursion, lots of paradigms use it heavily.

Therefore the correctness of our algorithm depends on two things:
\begin{enumerate}
	\item \textbf{Base Case:} All of our base cases must give a correct result to our answer.
	\item \textbf{Merge:} The way in which we combine smaller solutions ensure that the general solution is also correct.
\end{enumerate}

One of the key advantages of this is that we can now give a lot more importance to the semantic meaning of our algorithms. This is to say that if we can describe what our algorithm does in words, whenever we use recursion in our solution we can assume that the result this yields is correct. For this reason recursion is a very abstraction heavy strategy that requires some practice in order to really get the most of this technique.

\end{document}


%%
% The BIThesis Template for Bachelor Graduation Thesis
%
% 北京理工大学毕业设计（论文）第二章节 —— 使用 XeLaTeX 编译
%
% Copyright 2020-2022 BITNP
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Feng Kaiyu.
%%

\chapter{另一个章节}

\section{代码片段}

\begin{lstlisting}[language=Python, caption={Python Code}, label={lst:pythonfile}]
import numpy as np

def incmatrix(genl1,genl2):
    m = len(genl1)
    n = len(genl2)
    M = None #to become the incidence matrix
    VT = np.zeros((n*m,1), int)  #dummy variable

    #compute the bitwise xor matrix
    M1 = bitxormatrix(genl1)
    M2 = np.triu(bitxormatrix(genl2),1)

    for i in range(m-1):
        for j in range(i+1, m):
            [r,c] = np.where(M2 == M1[i,j])
            for k in range(len(r)):
                VT[(i)*n + r[k]] = 1;
                VT[(i)*n + c[k]] = 1;
                VT[(j)*n + r[k]] = 1;
                VT[(j)*n + c[k]] = 1;

                if M is None:
                    M = np.copy(VT)
                else:
                    M = np.concatenate((M, VT), 1)

                VT = np.zeros((n*m,1), int)

    return M
\end{lstlisting}


\chapter{LPE}

\section{Introduction}
The techniques that are described in the following chapters must be applied to a \emph{linear process equation}, or LPE for short.

\section{Requirements}

An LPE must satisfy the following requirements:

\begin{itemize}

\item Given are the definition of a single process \emph{and} its instantiation.
The instantiation must be a closed expression, and otherwise satisfy the usual requirements of \txs{} of process instantiations.
There are no requirements for the channel parameters and data parameters beyond the usual requirements of \txs{} of process definitions.

\item The body of the process consists of one or more \emph{summands}.
In this context, a summand must contain exactly the following, in the given order:

\begin{itemize}

\item One or more \emph{channel communications}; that is, channel references followed by the variables that are used to communicate over those channels.
For example, \inlinecode{OUTPUT ? x ? y}, or simply \inlinecode{OUTPUT}.

Channel communications are combined with \inlinecode{|}.
To give another example, \inlinecode{INPUT ? x | OUTPUT ? y}.

The variables that are used to communicate over a channel must be \emph{fresh}.

\item A guard, yielding a boolean value.
The guard is not always explicitly written if it is semantically equivalent to \inlinecode{true}.

\item A sequence operator, \inlinecode{>->}.

\item The expression for deadlock (\inlinecode{STOP}) \emph{or} a recursive process instantiation.
The instantiated process must use the process definition given by the LPE, which satisfies the requirements of a \txs{} process signature.
This includes channel parameters: these must be assigned their current values of the instantiating process.
There are no requirements for the values assigned to the data parameters beyond the usual requirements of \txs{} (such as type compatibility).

\end{itemize}

\end{itemize}

\section{Example}

The following code snippet gives a valid example of an LPE:

\begin{lstlisting}
//Process definition:
PROCDEF example[A :: Int, B](state, curr, prev :: Int)
  = A ? i [[state==0]] >-> example[A, B](2, i, prev)
  + A ? i [[state==1 && i!=prev]] >-> example[A, B](2, i, prev)
  + A ? i [[state==2 && i==curr]] >-> example[A, B](1, curr, curr)
  + B >-> STOP
  ;

//Initialization:
example[A, B](0, 0, 0);
\end{lstlisting}

The process only accepts input sequences in which every number is repeated exactly once.
The process terminates non-deterministically.


%% SECTION HEADER /////////////////////////////////////////////////////////////////////////////////////
\section{2D Spectral Modelling}
\label{sec:2Dmodel}

%% SECTION CONTENT ////////////////////////////////////////////////////////////////////////////////////

According to the first-order shear deformation theory~\cite{reissner1945effect, mindlin1951influence}, the displacement field is expressed as:
\begin{eqnarray}
	\left \{ \begin{array}{c}
		\textbf{u}^e(\xi,\eta) \\
		\textbf{v}^e(\xi,\eta) \\
		\textbf{w}^e(\xi,\eta)
	\end{array} \right\} =
	\left \{ \begin{array}{c}
		\textbf{u}_0^e(\xi,\eta) + z\boldsymbol{\varphi}_x^e(\xi,\eta)\\
		\textbf{v}_0^e(\xi,\eta) + z\boldsymbol{\varphi}_y^e(\xi,\eta)\\
		\textbf{w}_0^e(\xi,\eta) \\
	\end{array} \right\},
\end{eqnarray}
where \(\textbf{u}_0^e\), \(\textbf{v}_0^e\) and \(\textbf{w}_0^e\) are nodal displacements, \(\boldsymbol{\varphi}_x^e\), \(\boldsymbol{\varphi}_y^e\) are the rotations of the normal to the mid-plane with respect to the axes \textit{x} and \textit{y}, respectively. They are defined as:
\begin{eqnarray}
	\left \{\begin{array}{c}
		\textbf{u}_0^e(\xi,\eta) \\
		\textbf{v}_0^e(\xi,\eta) \\
		\textbf{w}_0^e(\xi,\eta) \\
		\boldsymbol{\varphi}_x^e(\xi,\eta) \\
		\boldsymbol{\varphi}_y^e(\xi,\eta)
	\end{array} \right\}
	= \textbf{N}^e(\xi,\eta)\widehat{\textbf{d}}^e
	= \sum_{n=1}^q\sum_{m=1}^p\textbf{N}_m^e(\xi)\textbf{N}_n^e(\eta)
	\left \{ \begin{array}{c}
		\widehat{\textbf{u}}_0^e \\
		\widehat{\textbf{v}}_0^e \\
		\widehat{\textbf{w}}_0^e \\
		\widehat{\boldsymbol{\varphi}}_x^e \\
		\widehat{\boldsymbol{\varphi}}_y^e
	\end{array} \right \},
\end{eqnarray}
where $\widehat{\textbf{d}}^e$ is a nodal displacements vector of the element $e$.

The nodal bending strain--displacement relations are given in the form:
\begin{eqnarray}
	\boldsymbol{\epsilon}_b^e =
	\textbf{B}_b^e\widehat{\textbf{d}}^e =
	\left [
	\begin{array}{ccccc}
		\frac{\partial N^e}{\partial x} & 0 & 0 & 0 & 0\\
		0 & \frac{\partial N^e}{\partial y} & 0 & 0 & 0\\
		\frac{\partial N^e}{\partial y} & \frac{\partial N^e}{\partial x} & 0 & 0 & 0\\
		0 & 0 & 0 & -\frac{\partial N^e}{\partial x} & 0\\
		0 & 0 & 0 & 0 & -\frac{\partial N^e}{\partial y}\\
		0 & 0 & 0 & -\frac{\partial N^e}{\partial y} & -\frac{\partial N^e}{\partial x}
	\end{array} \right]
	\left \{ \begin{array}{c}
		\widehat{\textbf{u}}_0^e \\
		\widehat{\textbf{v}}_0^e \\
		\widehat{\textbf{w}}_0^e \\
		\widehat{\boldsymbol{\varphi}}_x^e \\
		\widehat{\boldsymbol{\varphi}}_y^e
	\end{array} \right\}.
\end{eqnarray}

The nodal shear strain--displacement relations are given in the form:
\begin{eqnarray}
	\boldsymbol{\epsilon}_s^e =
	\textbf{B}_s^e\widehat{\textbf{d}}^e =
	\left [
	\begin{array}{ccccc}
		0 & 0 & \frac{\partial N^e}{\partial y} & -1 & 0\\
		0 & 0 & \frac{\partial N^e}{\partial y} & 0 & -1
	\end{array} \right]
	\left \{ \begin{array}{c}
		\widehat{\textbf{u}}_0^e \\
		\widehat{\textbf{v}}_0^e \\
		\widehat{\textbf{w}}_0^e \\
		\boldsymbol{\varphi}_x^e \\
		\boldsymbol{\varphi}_y^e
	\end{array} \right\}.
\end{eqnarray}

The mass and stiffness matrices for \ac{2d} elements are defined as:
\begin{eqnarray}
	\textbf{M}_{dd}^e & = &
	\left [
	\begin{array}{cc}
		\textbf{M}^e & 0\\
		0 & \textbf{J}^e
	\end{array}
	\right] =
	\int_{\Omega_e}\textbf{N}^T\rho
	\left [
	\begin{array}{ccccc}
		h & 0 & 0 & 0 & 0 \\
		& h & 0 & 0 & 0 \\
		&  & h & 0 & 0\\
		&  &  & \frac{h^3}{12} & 0\\
		Sym. &  &  &  & \frac{h^3}{12}
	\end{array} \right]
	\textbf{N} \diff\Omega_e,\\
	\textbf{K}_{dd}^e & = & \int_{\Omega_e}{\textbf{B}_b^e}^T
	\left[
	\begin{array}{cc}
		\textbf{A} & \textbf{B}\\
		\textbf{B} & \textbf{D}
	\end{array} \right]
	\textbf{B}_b^e \diff \Omega_e+\int_{\Omega_e}{\textbf{B}_s^e}^T\hat{\textbf{A}}\textbf{B}_s^e\diff \Omega_e,
\end{eqnarray}
where \(h=h_t+h_b\) is the element thickness, while \(h_{t(b)}\) is the distance between mid-plane and top(bottom) surface of the element, and \(\Omega_e\) is the element area:
\begin{eqnarray}
	\textbf{A} & = & \textbf{c}_{ij}\,(h_t-h_b),\qquad i,j=1,2,6\nonumber\\
	\textbf{B} & = & 1/2\, \textbf{c}_{ij}\,(h_t^2-h_b^2),\qquad i,j=1,2,6\nonumber\\
	\textbf{D} & = & 1/3\, \textbf{c}_{ij}\,(h_t^3-h_b^3),\qquad i,j=1,2,6\nonumber\\
	\hat{\textbf{A}} & = & 5/4\, \textbf{c}_{ij}\,\left[h_t-h_b-4/3\left(h_t^3-h_b^3\right)/h^2\right],\qquad i,j=4,5.
\end{eqnarray}


\chapter{Logistic Regression models}
\label{ch:Models}

The Logistic Regression models obtained in the results of this work is shown in tables \ref{tab:model1} and \ref{tab:model2}. To clarify, $\pi(x)$ indicates the probability that a new instance $x$ belongs to the default class, which, in this case, is a student passing the course.

\begin{table}[htb]
\centering
\begin{tabular}{cc} \hline
\textbf{Week} & \textbf{Model} \\ \hline
1 & $logit[\pi(x)] = -2.1+0.14*pp_1+0.2*pv_1$ \\ \hline
2 & $logit[\pi(x)] =-3.31+0.1*pp_1+0.16*pp_2+0.06*pv_1+0.22*pv_2$ \\ \hline
3 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-3.5+0.07*pp_1+0.1*pp_2+0.13*pp_3+0.04*pv_1$\\$ +0.08*pv_2+0.19*pv_3$\end{tabular} \\ \hline
4 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-4.4+0.06*pp_1+0.08*pp_2+0.09*pp_3+0.17*pp_4$\\$ +0.02*pv_1+0.01*pv_2+0.08*pv_3+0.25*pv_4$\end{tabular} \\ \hline
5 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-4.3+0.05*pp_1+0.05*pp_2+0.06*pp_3+0.1*pp_4$\\$ +0.16*pp_5+0.03*pv_1+0.02*pv_2+0.05*pv_3+0.1*pv_4+0.17*pv_5$\end{tabular} \\ \hline
6 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-4.63+0.04*pp_1+0.04*pp_2+0.05*pp_3+0.08*pp_4$\\$ +0.12*pp_5+0.14*pp_6+0.02*pv_1+0.01*pv_2$\\$ +0.03*pv_3+0.07*pv_4+0.11*pv_5+0.11*pv_6$\end{tabular} \\ \hline
7 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-4.75+0.04*pp_1+0.03*pp_2+0.04*pp_3+0.06*pp_4$\\$ +0.09*pp_5+0.1*pp_6+0.14*pp_7+0.02*pv_1$\\$ +0.01*pv_2+0.03*pv_3+0.06*pv_4+0.08*pv_5+0.05*pv_6+0.12*pv_7$\end{tabular} \\ \hline
8 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-5.14+0.06*pp_1+0.02*pp_2+0.04*pp_3+0.04*pp_4$\\$ +0.07*pp_5+0.09*pp_6+0.11*pp_7+0.11*pp_8$\\$ +0.01*pv_1+0.01*pv_2+0.02*pv_3+0.06*pv_4$\\$ +0.06*pv_5-0.03*pv_6+0.03*pv_7+0.24*pv_8$\end{tabular} \\ \hline
9 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-5.29+0.05*pp_1+0.02*pp_2+0.04*pp_3$\\$ +0.04*pp_4+0.07*pp_5+0.07*pp_6+0.09*pp_7+0.08*pp_8$\\$ +0.1*pp_9+0.0*pv_1+0.01*pv_2+0.0*pv_3+0.06*pv_4$\\$ +0.05*pv_5-0.03*pv_6-0.01*pv_7+0.11*pv_8+0.23*pv_9$\end{tabular} \\ \hline
10 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-5.36+0.05*pp_1+0.01*pp_2+0.03*pp_3$\\$ +0.04*pp_4+0.06*pp_5+0.06*pp_6+0.08*pp_7+0.07*pp_8$\\$ +0.08*pp_9+0.06*pp_{10}+0.0*pv_1+0.02*pv_2$\\$ -0.01*pv_3+0.05*pv_4+0.05*pv_5$\\$ -0.03*pv_6-0.02*pv_7+0.08*pv_8+0.11*pv_9+0.21*pv_{10}$\end{tabular} \\ \hline
11 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-5.49+0.06*pp_1+0.01*pp_2+0.03*pp_3$\\$ +0.04*pp_4+0.07*pp_5+0.05*pp_6+0.08*pp_7+0.05*pp_8$\\$ +0.07*pp_9+0.03*pp_{10}+0.08*pp_{11}-0.0*pv_1$\\$ +0.01*pv_2-0.01*pv_3+0.05*pv_4+0.04*pv_5-0.03*pv_6$\\$ -0.02*pv_7+0.06*pv_8+0.06*pv_9+0.11*pv_{10}+0.25*pv_{11}$\end{tabular} \\ \hline
12 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-5.42+0.06*pp_1+0.01*pp_2+0.02*pp_3$\\$ +0.03*pp_4+0.07*pp_5+0.05*pp_6+0.09*pp_7+0.05*pp_8$\\$ +0.06*pp_9+0.01*pp_{10}+0.05*pp_{11}+0.08*pp_{12}$\\$ -0.01*pv_1+0.01*pv_2-0.01*pv_3+0.06*pv_4$\\$ +0.04*pv_5-0.04*pv_6-0.05*pv_7+0.04*pv_8$\\$ +0.05*pv_9+0.07*pv_{10}+0.15*pv_{11}+0.22*pv_{12}$\end{tabular} \\ \hline
\end{tabular}
\caption{Logistic Regression models (weeks 1-12)}
\label{tab:model1}
\end{table}

\begin{table}[htb]
\centering
\resizebox{12cm}{!}{%
\begin{tabular}{cc} \hline
\textbf{Week} & \textbf{Model} \\ \hline
13 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-5.25+0.04*pp_1+0.01*pp_2+0.01*pp_3$\\$ +0.02*pp_4+0.06*pp_5+0.04*pp_6+0.06*pp_7+0.04*pp_8$\\$ +0.05*pp_9+0.01*pp_{10}+0.04*pp_{11}+0.05*pp_{12}$\\$ +0.1*pp_{13}+0.0*pv_1+0.01*pv_2+0.0*pv_3+0.04*pv_4$\\$ +0.03*pv_5-0.02*pv_6-0.01*pv_7+0.03*pv_8+0.04*pv_9$\\$ +0.06*pv_{10}+0.09*pv_{11}+0.09*pv_{12}+0.14*pv_{13}$\end{tabular} \\ \hline
14 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-5.31+0.04*pp_1+0.02*pp_2+0.01*pp_3$\\$ +0.03*pp_4+0.06*pp_5+0.04*pp_6+0.05*pp_7+0.04*pp_8$\\$ +0.04*pp_9-0.0*pp_{10}+0.02*pp_{11}+0.03*pp_{12}$\\$ +0.07*pp_{13}+0.12*pp_{14}+0.0*pv_1+0.01*pv_2$\\$ +0.0*pv_3+0.03*pv_4+0.04*pv_5-0.02*pv_6$\\$ -0.02*pv_7+0.03*pv_8+0.04*pv_9+0.05*pv_{10}$\\$ +0.08*pv_{11}+0.07*pv_{12}+0.08*pv_{13}+0.12*pv_{14}$\end{tabular} \\ \hline
15 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-5.47+0.04*pp_1+0.01*pp_2+0.01*pp_3$\\$ +0.03*pp_4+0.06*pp_5+0.03*pp_6+0.06*pp_7+0.04*pp_8$\\$ +0.04*pp_9-0.01*pp_{10}+0.01*pp_{11}+0.02*pp_{12}$\\$ +0.05*pp_{13}+0.09*pp_{14}+0.09*pp_{15}+0.01*pv_1$\\$ +0.0*pv_2+0.0*pv_3+0.04*pv_4+0.03*pv_5$\\$ -0.02*pv_6-0.02*pv_7+0.02*pv_8+0.04*pv_9$\\$ +0.05*pv_{10}+0.07*pv_{11}+0.05*pv_{12}$\\$ +0.05*pv_{13}+0.08*pv_{14}+0.12*pv_{15}$\end{tabular} \\ \hline
16 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-5.82+0.05*pp_1+0.01*pp_2+0.01*pp_3$\\$ +0.04*pp_4+0.07*pp_5+0.04*pp_6+0.06*pp_7+0.05*pp_8$\\$ +0.04*pp_9-0.02*pp_{10}+0.0*pp_{11}+0.0*pp_{12}$\\$ +0.04*pp_{13}+0.08*pp_{14}+0.08*pp_{15}+0.07*pp_{16}$\\$ +0.01*pv_1+0.0*pv_2+0.0*pv_3$\\$ +0.06*pv_4+0.05*pv_5-0.05*pv_6$\\$ -0.05*pv_7+0.01*pv_8+0.06*pv_9+0.06*pv_{10}$\\$ +0.09*pv_{11}+0.05*pv_{12}+0.03*pv_{13}+0.05*pv_{14}$\\$ +0.11*pv_{15}+0.11*pv_{16}$\end{tabular} \\ \hline
17 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-5.56+0.04*pp_1+0.01*pp_2+0.01*pp_3$\\$ +0.03*pp_4+0.05*pp_5+0.03*pp_6+0.05*pp_7+0.04*pp_8$\\$ +0.04*pp_9-0.02*pp_{10}+0.01*pp_{11}+0.0*pp_{12}$\\$ +0.03*pp_{13}+0.06*pp_{14}+0.06*pp_{15}+0.04*pp_{16}$\\$ +0.1*pp_{17}+0.01*pv_1+0.01*pv_2$\\$ +0.0*pv_3+0.04*pv_4+0.04*pv_5-0.02*pv_6$\\$ -0.02*pv_7+0.02*pv_8+0.04*pv_9+0.05*pv_{10}$\\$ +0.07*pv_{11}+0.05*pv_{12}+0.04*pv_{13}+0.04*pv_{14}$\\$ +0.07*pv_{15}+0.06*pv_{16}+0.08*pv_{17}$\end{tabular} \\ \hline
18 & \begin{tabular}[c]{@{}c@{}}$logit[\pi(x)] =-4.62+0.02*pp_1+0.01*pp_2+0.01*pp_3$\\$ +0.02*pp_4+0.03*pp_5+0.02*pp_6+0.03*pp_7+0.03*pp_8$\\$ +0.03*pp_9+0.01*pp_{10}+0.02*pp_{11}+0.02*pp_{12}$\\$ +0.03*pp_{13}+0.04*pp_{14}+0.04*pp_{15}+0.04*pp_{16}$\\$ +0.05*pp_{17}+0.06*pp_{18}+0.01*pv_1+0.01*pv_2$\\$ +0.01*pv_3+0.02*pv_4+0.02*pv_5+0.01*pv_6$\\$ +0.01*pv_7+0.02*pv_8+0.03*pv_9+0.03*pv_{10}$\\$ +0.04*pv_{11}+0.03*pv_{12}+0.03*pv_{13}+0.03*pv_{14}$\\$ +0.04*pv_{15}+0.04*pv_{16}+0.04*pv_{17}+0.05*pv_{18}$\end{tabular}\\ \hline
\end{tabular}%
}
\caption{Logistic Regression models (weeks 13-18)}
\label{tab:model2}
\end{table}