13 


14 
\section{One Dimensional Heat Diffusion in an Iron Rod} 
\section{One Dimensional Heat Diffusion in an Iron Rod} 
15 
%\label{Sec:1DHDv0} 
%\label{Sec:1DHDv0} 
16 
We will start by examining a simple one dimensional heat diffusion example. While this exact problem is not strictly relevant to earth sciences; it will provide a good launch pad to build our knowledge of Escript and how to solve simple PDEs. 
We will start by examining a simple one dimensional heat diffusion example. While this exact problem is not strictly relevant to earth sciences; it will provide a good launch pad to build our knowledge of \ESCRIPT and how to solve simple partial differential equations (PDEs) \footnote{In case you wonder what a 
17 

\textit{partial differential equation} wikipedia provides a 
18 

comprehensive introducton at 
19 

\url{http://en.wikipedia.org/wiki/Partial_differential_equation}, 
20 

but things should become a bit clearer when you read further in the cookbook.} 
21 



Start by imagining we have a simple cold iron bar at a constant temperature of zero. The bar is perfectly insulated on all sides and at one end we will apply a heating element of some description. Intuition tells us that as heat is applied, that energy will disperse through the bar with time until the bar reaches the same temperature as the heat source. At this point the temperature in the bar will be constant and the same as the heat source. 

22 


23 
We can model this problem using the one dimensional heat diffusion equation. It is defined as: 

24 

Start by imagining we have a simple cold iron bar at a constant temperature of zero. 
25 

\TODO{Add a diagram to explain the setup.} 
26 

The bar is perfectly insulated on all sides and at one end we will apply a heating element of some description. Intuition tells us that as heat is applied, that energy will disperse through the bar with time until the bar reaches the same temperature as the heat source. At this point the temperature in the bar will be constant and the same as the heat source. 
27 


28 

We can model this problem using the one dimensional heat diffusion equation. A detailed discussion on how the heat diffusion equation is derived can be found at 
29 

\url{http://online.redwoods.edu/instruct/darnold/DEProj/sp02/AbeRichards/paper.pdf} 
30 

Heat diffusion equation is defined as: 
31 
\begin{equation} 
\begin{equation} 
32 
\rho c\hackscore p \frac{\partial T}{\partial t}  \kappa \frac{\partial^{2} T}{\partial x^{2}} = q\hackscore H 
\rho c\hackscore p \frac{\partial T}{\partial t}  \kappa \frac{\partial^{2} T}{\partial x^{2}} = q\hackscore H 
33 
\label{eqn:hd} 
\label{eqn:hd} 
34 
\end{equation} 
\end{equation} 
35 
where $\rho$ is the material density, $c\hackscore p$ is the specific heat and $\kappa$ is the temperature diffusion constant. All of these values are readily available for most materials or can be established through predefined experimentation techniques. The heatsource is on the RHS of \eqref{eqn:hd} as $q_{H}$, this could be a constant or defined by an expression. There are also two partial derivatives in \eqref{eqn:hd}, $\frac{\partial T}{\partial t}$ describes the change in temperature with time while $\frac{\partial T}{\partial x}$ describes the spatial change to temperature. Ther is only a single spatial dimension to our problem, and so our Temperature solution $T$ is only dependent on the time $t$ and our position along the iron bar $x$ . 
where $\rho$ is the material density, $c\hackscore p$ is the specific heat and $\kappa$ is the temperature diffusion constant. All of these values are readily available for most materials or can be established through predefined experimentation techniques. \TODO{Give some numbers for eg. granite}. 
36 

The heatsource is on the right hand side of \eqref{eqn:hd} as $q_{H}$, this could be a constant or defined by an expression \TODO{Give an example}. There are also two partial derivatives in \eqref{eqn:hd}, $\frac{\partial T}{\partial t}$ describes the change in temperature with time while $\frac{\partial T}{\partial x}$ describes the spatial change to temperature. There is only a single spatial dimension to our problem, and so our temperature solution $T$ is only dependent on the time $t$ and our position along the iron bar $x$. 
37 


38 
To solve this equation we will write a simple python script. The first step is to import the necessary libraries. 
\TODO{Explain the concept of time discritzation} 
39 


40 

To solve this equation we will write a simple python script which uses \ESCRIPT and 
41 

\FINLEY of the \ESYS module. At this point we assume that you have some basic 
42 

understanding of the python programming language. There are in fact a large number of 
43 

python tutorial available online, for instance 
44 

\begin{itemize} 
45 

\item This is a very crisp introduction \url{http://hetland.org/writing/instantpython.html}. It covers everthing you need to get started with \ESCRIPT. 
46 

\item A nice and easy to follow introduction: \url{http://www.sthurlow.com/python/} 
47 

\item An other crip tutorial \url{http://www.zetcode.com/tutorials/pythontutorial/}. 
48 

\item A very comprehensive tutorial from the python authors: \url{http://www.python.org/doc/2.5.2/tut/tut.html}. It covers much more than what you will ever need for \ESCRIPT. 
49 

\item an other comprehensive tutorial: \url{http://www.tutorialspoint.com/python/index.htm} 
50 

\end{itemize} 
51 

In the following we will develop the script to solve the heat equation stepbystep. 
52 

The first step is to import the necessary libraries. 
53 
\begin{verbatim} 
\begin{verbatim} 
54 
from esys.escript import * 
from esys.escript import * 
55 
from esys.escript.linearPDEs import LinearPDE 
from esys.escript.linearPDEs import SingleLinearPDE 
56 
from esys.finley import Rectangle 
from esys.finley import Rectangle 
57 
import os 
import os 
58 
\end{verbatim} 
\end{verbatim} 
61 
Once our libraries dependancies 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 escript 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 will need values. Firstly, the domain upon which we wish to solve our problem needs to be defined. There are many different types of domains in escript. We will demonstrate a few in later tutorials but for our iron rod we will simply use a rectangular domain. 
Once our libraries dependancies 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 escript 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 will need values. Firstly, the domain upon which we wish to solve our problem needs to be defined. There are many different types of domains in escript. We will demonstrate a few in later tutorials but for our iron rod we will simply use a rectangular domain. 
62 


63 
Using a rectangular domain simplifies a \textit{3D} object into a single dimension. The iron rod will have a lengthways cross section that looks like a rectangle. As a result we do not need to model the volume of the rod because a cylinder is symmetrical about its center. There are four arguments we must consider when we decide to create a rectangular domain, the model length, width and step size in each direction. When defining the size of our problem it will help us determine appropriate values for our domain arguments. If we make our dimensions large but our step sizes very small we will to a point, 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 our \textit{1D} problem we will define our bar as being 1 metre long. An appropriate \verbndx would be 1 to 10\% of the length. Our \verbndy need only be 1, uhis is because our problem stipulates no partial derivatives in the $y$ direction. This means the temperature does not vary with $y$. Thus the domain perameters can be defined as follows: 
Using a rectangular domain simplifies a \textit{3D} object into a single dimension. The iron rod will have a lengthways cross section that looks like a rectangle. As a result we do not need to model the volume of the rod because a cylinder is symmetrical about its center. There are four arguments we must consider when we decide to create a rectangular domain, the model length, width and step size in each direction. When defining the size of our problem it will help us determine appropriate values for our domain arguments. If we make our dimensions large but our step sizes very small we will to a point, 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 our \textit{1D} problem we will define our bar as being 1 metre long. An appropriate \verbndx would be 1 to 10\% of the length. Our \verbndy need only be 1, uhis is because our problem stipulates no partial derivatives in the $y$ direction. This means the temperature does not vary with $y$. Thus the domain perameters can be defined as follows: 
64 

\TODO{Use the Unit module!} 
65 
\begin{verbatim} 
\begin{verbatim} 
66 
#Domain related. 
#Domain related. 
67 
mx = 1 #meters  model lenght 
mx = 1 #meters  model lenght 
80 
eta = 0 #radiation condition 
eta = 0 #radiation condition 
81 
kappa = 68. #temperature diffusion constant 
kappa = 68. #temperature diffusion constant 
82 
\end{verbatim} 
\end{verbatim} 
83 

\TODO{remove radiation condition and if required introduce in a second example} 
84 
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: 
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: 
85 
\begin{verbatim} 
\begin{verbatim} 
86 
#Script/Iteration Related 
#Script/Iteration Related 
101 
\begin{verbatim} 
\begin{verbatim} 
102 
x = rod.getX() 
x = rod.getX() 
103 
\end{verbatim} 
\end{verbatim} 
104 
With a domain and all our required variables established, it is now possible to set up our PDE so that it can be solved by escript. The first step is to define the type of PDE that we are trying to solve. In this example it is a Linear PDE and we can define it by: 
With a domain and all our required variables established, it is now possible to set up our PDE so that it can be solved by escript. 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 (in comparison to a system of PDEs which will discussed later) 
105 

we can define it by: 
106 
\begin{verbatim} 
\begin{verbatim} 
107 
mypde=LinearPDE(rod) 
mypde=LinearSinglePDE(rod) 

\end{verbatim} 


Because we have a symmetrical problem we will also need to set the symmetry on by: 


\begin{verbatim} 


myPDE.setSymmetryOn() 

108 
\end{verbatim} 
\end{verbatim} 
109 
To input the PDE into \esc it must be compared with the general form\footnote{Available in section ?? of the users guide to \esc }. For most simple PDEs however, the general form is over complicated and confusing. Thus for this example, we will use a simplified version that suits our heat diffusion problem. This simpler form is described by; 
In the next step we need to define the coefficients of the PDE. The linear 
110 
\begin{equation}\label{eqn:simpfrm} 
PDEs in \ESCRIPT provide a general interface to do this. Here we will only discuss a simplified form that suits our heat diffusion problem and refer to the \ESCRIPT user's guide for the general case. This simpler form 
111 
(A\hackscore{jl} u\hackscore{,l})\hackscore{,j}+D u =+Y 
\footnote{In the form of the \ESCRIPT users guide which uses the Einstein convention 
112 
\end{equation} 
this equation is written as 
113 
This can be written in the form; 
$(A\hackscore{jl} u\hackscore{,l})\hackscore{,j}+D u =Y$} 
114 
\begin{equation} 
is described by 
115 

\begin{equation}\label{eqn:commonform nabla} 
116 
\nabla.(A.\nabla u) + Du = f 
\nabla.(A.\nabla u) + Du = f 
117 
\end{equation} 
\end{equation} 
118 
or; 
where $A$, $D$ and $f$ are known values. 
119 

The symbol $\nabla$ which is called the \textit{Nabla operator} or \textit{del operator} represents 
120 

the spatial derivative of what ever stands next to the right from it. Lets assume for a moment that we deal with a onedimensional problem then 
121 

\begin{equation} 
122 

\nabla = \frac{\partial}{\partial x} 
123 

\end{equation} 
124 

and we can write equation \ref{eqn:commonform nabla} as 
125 
\begin{equation}\label{eqn:commonform} 
\begin{equation}\label{eqn:commonform} 
126 
A\frac{\partial^{2}u}{\partial x^{2}\hackscore{i}} + Du = f 
A\frac{\partial^{2}u}{\partial x^{2}} + Du = f 
127 
\end{equation} 
\end{equation} 
128 
When comparing equations \eqref{eqn:hd} and \eqref{eqn:commonform} we see that; 
if $A$ is constant. This is exactly the equation we need to solve 
129 

in each time step as described in equation~\ref{XXXXX}. When comparing equations \eqref{eqn:hd} and \eqref{eqn:commonform} we see that; 
130 
\begin{equation} 
\begin{equation} 
131 
A = \kappa . \delta; D = \frac{\rho c \hackscore{p}}{h}; f = q \hackscore{H} 
A = \kappa; D = \frac{\rho c \hackscore{p}}{h}; f = q \hackscore{H} 
132 

\end{equation} 
133 


134 

We need to revisit the general PDE~\ref{eqn:commonform nabla} under the light of a 
135 

two dimensional domain. As pointed out earlier \ESCRIPT is not designed 
136 

to solve onedimensional problems so the general PDE~\ref{eqn:commonform nabla} 
137 

needs to be read as a higher dimensional problem. In the case of 
138 

two spatial dimensions the Nable operator has in fact 
139 

two components $\nabla = (\frac{\partial}{\partial x} 
140 

\frac{\partial}{\partial y})$. If we spell out the general PDE~\ref{eqn:commonform nabla} and assume a constant coefficient $A$ it then takes the form 
141 

\begin{equation}\label{eqn:commonform2D} 
142 

A\hackscore{00}\frac{\partial^{2}u}{\partial x^{2}} 
143 

A\hackscore{01}\frac{\partial^{2}u}{\partial x\partial y} 
144 

A\hackscore{10}\frac{\partial^{2}u}{\partial y\partial x} 
145 

A\hackscore{11}\frac{\partial^{2}u}{\partial y^{2}} 
146 

+ Du = f 
147 

\end{equation} 
148 

We notice that for the higher dimensional case $A$ becomes a matrix. It is also 
149 

important to notice that the usage of the Nable operator creates 
150 

a compact formulation which is also independend from the spatial dimension. 
151 

So to make the general PDE~\ref{eqn:commonform2D} one dimensional as 
152 

shown in~\ref{eqn:commonform} we need to set 
153 

\begin{equation}\label{eqn:commonform2D} 
154 

A\hackscore{00}=A; A\hackscore{01}=A\hackscore{10}=A\hackscore{11} 
155 
\end{equation} 
\end{equation} 
156 


157 


158 

BLABLA 
159 

\TODO{explain boundray condition; ignore radiation} 
160 


161 


162 


163 


164 


165 

Because we have a symmetrical problem we will also need to set the symmetry on by: 
166 

\begin{verbatim} 
167 

myPDE.setSymmetryOn() 
168 

\end{verbatim} 
169 

To input the PDE into \esc it must be compared with the general form\footnote{Available in section ?? of the users guide to \esc }. For most simple PDEs however, the general form is over complicated and confusing. Thus for this example, we will use a simplified version 
170 


171 


172 


173 


174 
Additionally we must also consider the boundary conditions of our PDE. They take the form: 
Additionally we must also consider the boundary conditions of our PDE. They take the form: 
175 
\begin{equation} 
\begin{equation} 
176 
\eta \hackscore{j} A\hackscore{jl} u\hackscore{,l} + du = y 
\eta \hackscore{j} A\hackscore{jl} u\hackscore{,l} + du = y 
187 


188 
END WORK ON THIS SECTION 
END WORK ON THIS SECTION 
189 


190 

\section{Plot total heat} 
191 

\TODO{show the script} 
192 


193 

\TODO{explain how to use matlibplot to visualize the total heat integral(rho*c*T) over time} 
194 


195 

\section{Plot Temperature Distribution} 
196 

\TODO{explain how to use matlibplot to visualize T} 
197 


198 


199 

\TODO{Move this to the 2D section as an advanced topic} 
200 
The final stage to our problem is exporting the data we have generated and turn our data and visualisation. It is best to export the calculated solutions at each time increment. escript has the inbuilt function \verbsaveVTK() which makes this step very easy. saveVTK takes two arguments, the path and the filename. We are goind to use the \verbos.path.join command to join a subdirectory which must already exist with a file name. The string opperator \verb% allows us to increment our file names with the value \verbi. In substring \verb %03d does a number of things; 
The final stage to our problem is exporting the data we have generated and turn our data and visualisation. It is best to export the calculated solutions at each time increment. escript has the inbuilt function \verbsaveVTK() which makes this step very easy. saveVTK takes two arguments, the path and the filename. We are goind to use the \verbos.path.join command to join a subdirectory which must already exist with a file name. The string opperator \verb% allows us to increment our file names with the value \verbi. In substring \verb %03d does a number of things; 
201 
\begin{itemize} 
\begin{itemize} 
202 
\item \verb 0 becomes the padding number; 
\item \verb 0 becomes the padding number; 