1 

2 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
3 
% 
4 
% Copyright (c) 20032009 by University of Queensland 
5 
% Earth Systems Science Computational Center (ESSCC) 
6 
% http://www.uq.edu.au/esscc 
7 
% 
8 
% Primary Business: Queensland, Australia 
9 
% Licensed under the Open Software License version 3.0 
10 
% http://www.opensource.org/licenses/osl3.0.php 
11 
% 
12 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
13 

14 
\section{One Dimensional Heat Diffusion in an Iron Rod} 
15 
%\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. 
17 

18 
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. 
19 

20 
We can model this problem using the one dimensional heat diffusion equation. It is defined as: 
21 
\begin{equation} 
22 
\rho c\hackscore p \frac{\partial T}{\partial t}  \kappa \frac{\partial^{2} T}{\partial x^{2}} = q\hackscore H 
23 
\label{eqn:hd} 
24 
\end{equation} 
25 
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$ . 
26 

27 
To solve this equation we will write a simple python script. The first step is to import the necessary libraries. 
28 
\begin{verbatim} 
29 
from esys.escript import * 
30 
from esys.escript.linearPDEs import LinearPDE 
31 
from esys.finley import Rectangle 
32 
import os 
33 
\end{verbatim} 
34 
It is generally a good idea to import all of \verb escript , although if you know the packages you need you can specify them individually. The function \verbLinearPDE has been imported individually for ease of use later in the script. \verbRectangle is going to be our type of domain and the \verbos package is needed to handle file outputs once our PDE has been solved. 
35 

36 
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. 
37 

38 
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: 
39 
\begin{verbatim} 
40 
#Domain related. 
41 
mx = 1 #meters  model lenght 
42 
my = .1 #meters  model width 
43 
ndx = 100 # steps in x direction 
44 
ndy = 1 # steps in y direction 
45 
\end{verbatim} 
46 
The material constants and the temperature variables must also be defined. For the iron rod in the model they are defined as: 
47 
\begin{verbatim} 
48 
#PDE related 
49 
q=473. #Kelvin  our heat source temperature 
50 
Tref = 273. # Kelvin  starting temp of iron bar 
51 
rho = 7874. #kg/m^{3} density of iron 
52 
cp = 449. #j/Kg.K 
53 
rhocp = rho*cp 
54 
eta = 0 #radiation condition 
55 
kappa = 68. #temperature diffusion constant 
56 
\end{verbatim} 
57 
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: 
58 
\begin{verbatim} 
59 
#Script/Iteration Related 
60 
t=0 #our start time, usually zero 
61 
tend=5.*60. #seconds  time to end simulation 
62 
outputs = 200 # number of time steps required. 
63 
h=(tendt)/outputs #size of time step 
64 
i=0 #loop counter 
65 
#the folder to put our outputs in, leave blank "" for script path 
66 
#note this folder path must exist to work 
67 
save_path = "data/onedheatdiff001" 
68 
\end{verbatim} 
69 
Now that we know our inputs we will build a domain using the \verb Rectangle() function from \verb Finley . The four arguments allow us to define our domain \verb rod as: 
70 
\begin{verbatim} 
71 
rod = Rectangle(l0=mx,l1=my,n0=ndx,n1=ndy) 
72 
\end{verbatim} 
73 
In this form \verb rod does not represent any discrete points, but rather an area of \verb ndx*ndy cells that fit into a rectangular space with opposing vertices at the origin and the point \verb [mx,my] . The reason for this is so that the discrete points used in the solution can be specified by the user. That is, are discrete points used at the corners of each cell, the middle point of a cell or halfway along each side of the cell etc. Fortunately \verb escript offers an easy way to extract finite points from the domain \verbrod using the domain property function \verbgetX() . This function uses the vertices of each cell as finite points to solve in the solution. If we let \verbx be these finite points, then: 
74 
\begin{verbatim} 
75 
x = rod.getX() 
76 
\end{verbatim} 
77 
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: 
78 
\begin{verbatim} 
79 
mypde=LinearPDE(rod) 
80 
\end{verbatim} 
81 
Because we have a symmetrical problem we will also need to set the symmetry on by: 
82 
\begin{verbatim} 
83 
myPDE.setSymmetryOn() 
84 
\end{verbatim} 
85 
To input the PDE into \verb escript it must be compared with the general form equation of a linear PDE which is described by: 
86 
\begin{equation}\label{eqn:genfrm} 
87 
(A\hackscore{jl} u\hackscore{,l})\hackscore{,j}(B\hackscore{j} u)\hackscore{,j}+C\hackscore{l} u\hackscore{,l}+D u =X\hackscore{j,j}+Y \; . 
88 
\end{equation} 
89 

90 

91 
NEED TO WORK ON THIS SECTION 
92 

93 
We now need to specify Our boundary conditions and initial values. The initial values required to solve this PDE are temperatures for each discrete point in our domain that we wish to solve for. We will set our bar to: 
94 
\begin{verbatim} 
95 
T = Tref 
96 
\end{verbatim} 
97 
Boundary conditions are a little more difficult. Fortunately the escript solver will handle our insulated boundary conditions. However, we will need to apply our heat source $q_{H}$ to the end of the bar at $x=0$ . escript makes this easy by letting us define areas in our domain. To retrieve all the finite points in our domain we will use 
98 

99 
END WORK ON THIS SECTION 
100 

101 
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; 
102 
\begin{itemize} 
103 
\item \verb 0 becomes the padding number; 
104 
\item \verb 3 tells us the number of padding numbers that are required; and 
105 
\item \verb d indicates the end of the \verb % operator. 
106 
\end{itemize} 
107 
To increment a \verb %i is required directly after the operation the string is involed in. The second arugment of the \verb saveVTK function is the \verb sol=T where \verb T is the solution of our PDE for a given time steo. Thus the export command becomes: 
108 
\begin{verbatim} 
109 
saveVTK(os.path.join(save_path,"data%03d.xml") %i,sol=T) 
110 
\end{verbatim} 
111 
Visualisation is then implemented through \verb mayavi which provides a command line of gui interface to develop plots to suit our output data. For this example a surface plot is appropriate. 