# Diff of /trunk/doc/cookbook/TEXT/onedheatdiff001.tex

revision 2494 by ahallam, Thu Jun 25 05:00:57 2009 UTC revision 2495 by ahallam, Fri Jun 26 03:39:21 2009 UTC
# Line 13  Line 13
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 partial differential equations (PDEs)  We will start by examining a simple one dimensional heat diffusion example. This problem will provide a good launch pad to build our knowledge of \ESCRIPT and how to solve simple partial differential equations (PDEs)\footnote{Wikipedia provides an excellent and comprehensive introduction to \textit{Partial Differential Equations} \url{http://en.wikipedia.org/wiki/Partial_differential_equation}, however their relevance to \ESCRIPT and implementation should become a clearer as we develop our understanding further into the cookbook.}
\footnote{Wikipedia provides an excellent and comprehensive introduction to \textit{Partial Differential Equations} \url{http://en.wikipedia.org/wiki/Partial_differential_equation}, however their relevance to \ESCRIPT and implementation should become a clearer as we develop our understanding further into the cookbook.}
17
The first model consists of a simple cold iron bar at a constant temperature of zero Figure \ref{fig:onedhdmodel}. The bar is perfectly insulated on all sides with a heating element of some description at one end. Intuition tells us that as heat is applied; energy will disperse along the bar via convection. With time the bar will reach a constant temperature equivalent to the heat source.
18  \begin{figure}[h!]  \begin{figure}[h!]
19  \centerline{\includegraphics[width=4.in]{figures/onedheatdiff}}  \centerline{\includegraphics[width=4.in]{figures/onedheatdiff}}
20  \caption{One dimensional model of an Iron bar.}  \caption{One dimensional model of an Iron bar.}
21  \label{fig:onedhdmodel}  \label{fig:onedhdmodel}
22  \end{figure}  \end{figure}
23    The first model consists of a simple cold iron bar at a constant temperature of zero \reffig{fig:onedhdmodel}. The bar is perfectly insulated on all sides with a heating element at one end. Intuition tells us that as heat is applied; energy will disperse along the bar via conduction. With time the bar will reach a constant temperature equivalent to the heat source.
24
25  We can model the heat distribution of this problem in time using the one dimensional heat diffusion equation  \subsection{1D Heat Diffusion Equation}
26  \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}};  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}};
27  which is defined as:  which is defined as:
28
29  \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
30  \label{eqn:hd}  \label{eqn:hd}
31
32  where $\rho$ is the material density, $c\hackscore p$ is the specific heat and $\kappa$ is the thermal conductivity constant for a given material  where $\rho$ is the material density, $c\hackscore p$ is the specific heat and $\kappa$ is the thermal conductivity constant for a given material\footnote{A list of some common thermal conductivities is available from Wikipedia \url{http://en.wikipedia.org/wiki/List_of_thermal_conductivities}}.
33  \footnote{A list of some common thermal conductivities is available from Wikipedia \url{http://en.wikipedia.org/wiki/List_of_thermal_conductivities}}.  The heat source is defined by the right hand side of \ref{eqn:hd} as $q\hackscore{H}$; this can take the form of a constant or a function of time and space. For example $q\hackscore{H} = Te^{-\gamma t}$ where we have the output of our heat source decaying with time. There are also two partial derivatives in \ref{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 position along the iron bar $x$.
The heatsource is defined by the right hand side of \eqref{eqn:hd} as $q\hackscore{H}$, this could be a constant or a function of time. For example $q\hackscore{H} = Te^{\gamma t}$ where we have the output of our heatsource decaying with time. 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 ^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 position along the iron bar $x$.
34
35  When solving a PDE analytically we are finding an exact solution to our equation, however it is not always possible to solve a problem this way. Computers can be used in these cases when a large number of sums or visualisation is required. Computers require a numerical approach to solving problems - \ESCRIPT is one example -  and it becomes necessary to discretize the equation so that we are left with a finite number of equations for a finite number of spatial and time steps in the model. While this is of course an approximation and introduces a degree of error, we find that a sufficiently sampled model is generally accurate enough for the requirements of the modeller.  \subsection{Escript, PDEs and The General Form}
36    Potentially, it is now possible to solve \ref{eqn:hd} analytically. This would produce an exact solution to our problem, however, it is not always possible or practical to solve a problem this way. Alternatively, computers can be used to solve these kinds of problems when a large number of sums or visualisation is required. To do this, a numerical approach is required - \ESCRIPT can help us here -  and it becomes necessary to discretize the equation so that we are left with a finite number of equations for a finite number of spatial and time steps in the model. While discretization introduces approximations and a degree of error, we find that a sufficiently sampled model is generally accurate enough for the requirements of the modeller.
37
38  To solve this equation we will write a simple python script which uses \ESCRIPT and \FINLEY of the \ESYS module. At this point we assume that you have some basic understanding of the python programming language. If not there are some pointers and links available in Section \ref{sec:escpybas} .  \ESCRIPT interfaces with any generic PDE via a general form. In this example we will illustrate a simpler version of the full linear PDE general form which is availabe in the user's guide. The first step is to define the coefficients of the general form using our PDE \ref{eqn:hd}. This simplified form suits our heat diffusion problem\footnote{In the form of the \ESCRIPT users guide which using the Einstein convention is written as

In the following section we will develop a script for \ESCRIPT to solve the heat equation step-by-step. 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 sin and cos functions or more complicated like those from our \ESCRIPT library.}
that we will require.
\begin{verbatim}
from esys.escript import *
from esys.escript.linearPDEs import SingleLinearPDE
from esys.finley import Rectangle
from esys.escript.unitsSI import *
import os
\end{verbatim}
It is generally a good idea to import all of the \verb escript  library, although if you know the packages you need you can specify them individually. The function \verb|LinearPDE| has been imported for ease of use later in the script. \verb|Rectangle| is going to be our type of domain. The package \verb unitsSI  is a module of \esc that provides support for units definitions with our variables; and the \verb|os| package is needed to handle file outputs once our PDE has been solved.

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.

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 \verb|ndx| would be 1 to 10\% of the length. Our \verb|ndy| need only be 1, This is because our problem stipulates no partial derivatives in the $y$ direction so the temperature does not vary with $y$. Thus the domain perameters 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{verbatim}
#Domain related.
mx = 1*m #meters - model lenght
my = .1*m #meters - model width
ndx = 100 # steps in x direction
ndy = 1 # steps in y direction
\end{verbatim}
The material constants and the temperature variables must also be defined. For the iron rod in the model they are defined as:
\begin{verbatim}
#PDE related
q=200. * Celsius #Kelvin - our heat source temperature
Tref = 0. * Celsius # Kelvin - starting temp of iron bar
rho = 7874. *kg/m**3 #kg/m^{3} density of iron
cp = 449.*J/(kg*K) #jules/Kg.K thermal capacity
rhocp = rho*cp
kappa = 80.*W/m/K #watts/m.K thermal conductivity
\end{verbatim}
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{verbatim}
#Script/Iteration Related
t=0 #our start time, usually zero
tend=5.*60. #seconds - time to end simulation
outputs = 200 # number of time steps required.
h=(tend-t)/outputs #size of time step
i=0 #loop counter
#the folder to put our outputs in, leave blank "" for script path
#note this folder path must exist to work
save_path = "data/onedheatdiff001"
\end{verbatim}
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:
\begin{verbatim}
rod = Rectangle(l0=mx,l1=my,n0=ndx,n1=ndy)
\end{verbatim}
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] . Our domain is contructed this way to allow the user to determine where the discrete points of each model will be located. Discretisation may be at the corners of each cell, the middle point of a cell or halfway along each side of the cell etc. Depending on the PDE or the model there may be advantages and disadvantages for each case. Fortunately \verb escript offers an easy way to extract finite points from the domain \verb|rod| using the domain property function \verb|getX()| . This function sets the vertices of each cell as finite points to solve in the solution. If we let \verb|x| be these finite points, then;
\begin{verbatim}
x = rod.getX()
\end{verbatim}
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
\footnote{in comparison to a system of PDEs which will be discussed later.}
we can define by:
\begin{verbatim}
mypde=LinearSinglePDE(rod)
\end{verbatim}
In the next step we need to define the coefficients of the PDE. The linear
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
\footnote{In the form of the \ESCRIPT users guide which using the Einstein convention is written as
39  $-(A\hackscore{jl} u\hackscore{,l})\hackscore{,j}+D u =Y$}  $-(A\hackscore{jl} u\hackscore{,l})\hackscore{,j}+D u =Y$}
40  is described by;  and is described by;
41  \label{eqn:commonform nabla}  \label{eqn:commonform nabla}
42  -\nabla.(A.\nabla u) + Du = f  -\nabla.(A.\nabla u) + Du = f
43
44  where $A$, $D$ and $f$ are known values. The symbol $\nabla$ which is called the \textit{Nabla operator} or \textit{del operator} represents  where $A$, $D$ and $f$ are known values. The symbol $\nabla$ which is called the \textit{Nabla operator} or \textit{del operator} represents
45  the spatial derivative of its subject - in this case $u$. Lets assume for a moment that we deal with a one-dimensional problem then  the spatial derivative of its subject - in this case $u$. Lets assume for a moment that we deal with a one-dimensional problem then ;
46
47  \nabla = \frac{\partial}{\partial x}  \nabla = \frac{\partial}{\partial x}
48
49  and we can write equation \ref{eqn:commonform nabla} as  and we can write \ref{eqn:commonform nabla} as;
50  \label{eqn:commonform}  \label{eqn:commonform}
51  -A\frac{\partial^{2}u}{\partial x^{2}} + Du = f  -A\frac{\partial^{2}u}{\partial x^{2}} + Du = f
52
53  if $A$ is constant then equation \ref{eqn:commonform} is consistent with our heat diffusion problem in Equation \ref{eqn:hd} - except for $u$. When comparing equations \eqref{eqn:hd} and \eqref{eqn:commonform} we see that;  if $A$ is constant then  \ref{eqn:commonform} is consistent with our heat diffusion problem in \ref{eqn:hd} - except for $u$ and when comparing equations \ref{eqn:hd} and \ref{eqn:commonform} we see that;
54
55  A = \kappa; D = \rho c \hackscore{p}; f = q \hackscore{H}  A = \kappa; D = \rho c \hackscore{p}; f = q \hackscore{H}
56
57
58  We can write the partial $\frac{\partial T}{\partial t}$ in terms of $u$ by discretising the time of our solution. Many methods could be used here but we have decided on the Backwards Euler approximation which states;  We can write the partial $\frac{\partial T}{\partial t}$ in terms of $u$ by discretising the time of our solution. The method we will use is the Backwards Euler approximation, which states;
59
60  f'(x) \approx \frac{f(x+h)-f(x)}{h}  f'(x) \approx \frac{f(x+h)-f(x)}{h}
61  \label{eqn:beuler}  \label{eqn:beuler}
62
63  where h is the the discrete step size $\Delta x$.  where h is the the discrete step size $\Delta x$.
64  Now let $f(x) = T(t)$ and from equation \ref{eqn:beuler} we see that;  Now let $f(x) = T(t)$ and from \ref{eqn:beuler} we see that;
65
66  T'(t) \approx \frac{T(t+h) - T(t)}{h}  T'(t) \approx \frac{T(t+h) - T(t)}{h}
67
# Line 133  which can also be written as; Line 70  which can also be written as;
70  T\hackscore{,t}^{(n)} \approx \frac{T^{(n)} - T^{(n-1)}}{h}  T\hackscore{,t}^{(n)} \approx \frac{T^{(n)} - T^{(n-1)}}{h}
71  \label{eqn:Tbeuler}  \label{eqn:Tbeuler}
72
73  where $n$ denotes the n\textsuperscript{th} time step. Substituting equation \ref{eqn:Tbeuler} into equation \ref{eqn:hd} we get;  where $n$ denotes the n\textsuperscript{th} time step. Substituting \ref{eqn:Tbeuler} into \ref{eqn:hd} we get;
74
75  \frac{\rho c\hackscore p}{h} (T^{(n)} - T^{(n-1)}) - \kappa \frac{\partial^{2} T}{\partial x^{2}} = q\hackscore H  \frac{\rho c\hackscore p}{h} (T^{(n)} - T^{(n-1)}) - \kappa \frac{\partial^{2} T}{\partial x^{2}} = q\hackscore H
76  \label{eqn:hddisc}  \label{eqn:hddisc}
77
78  To fit our simplified general form we can rearrange so that;  To fit our simplified general form we can rearrange \ref{eqn:hddisc} so that;
79
80  \frac{\rho c\hackscore p}{h} T^{(n)} - (\kappa T^{(n)}\hackscore{,i})\hackscore{,i} = q\hackscore H +  \frac{\rho c\hackscore p}{h} T^{(n-1)}  \frac{\rho c\hackscore p}{h} T^{(n)} - (\kappa T^{(n)}\hackscore{,i})\hackscore{,i} = q\hackscore H +  \frac{\rho c\hackscore p}{h} T^{(n-1)}
81  \label{eqn:hdgenf}  \label{eqn:hdgenf}
82
83  This is the form required for escript to solve our PDE across the domain for successive time nodes $t^{(n)}$ where  This is the form required for \ESCRIPT to solve our PDE across the domain for successive time nodes $t^{(n)}$ where
84  $t^{(0)}=0$ and  $t^{(n)}=t^{(n-1)}+h$ where $h>0$ is the step size which is assumed to be constant.  $t^{(0)}=0$ and  $t^{(n)}=t^{(n-1)}+h$ where $h>0$ is the step size and assumed to be constant.
85  In the following the upper index ${(n)}$ refers to a value at time $t^{(n)}$. When comparing equation \ref{eqn:hdgenf} with equation \ref{eqn:commonform} we see that;  In the following the upper index ${(n)}$ refers to a value at time $t^{(n)}$. When comparing \ref{eqn:hdgenf} with \ref{eqn:commonform} we see that;
86
87  A = \kappa; D = \frac{\rho c \hackscore{p}}{h}; f = q \hackscore{H} + \frac{\rho c\hackscore p}{h} T^{(n-1)}  A = \kappa; D = \frac{\rho c \hackscore{p}}{h}; f = q \hackscore{H} + \frac{\rho c\hackscore p}{h} T^{(n-1)}
88
89
90  \TODO{DISCUSS BOUNDARY CONDITIONS}  Now that we have established the general form we will submit to \ESCRIPT; it is necessary to establish the state of our system at time zero or $T^{(n=0)}$. This is due to the time derivative approximation we have used from \ref{eqn:Tbeuler}. Are model stipulates a starting temperature in our iron bar of 0\textcelsius. Thus the temperature distribution is simply;
91  It is pointed out that the initial conditions satisfy the boundary condition defined by
92    T(x,0) = T\hackscore{ref} = 0
93
94    for all $x$ in the domain.
95
96    \subsection{Boundary Conditions}
97    With our PDE sorted, consideration must now be given to the boundary conditions of our model. Typically there are two main types of boundary conditions known as Neumann and Dirichlet\footnote{More information on Boundary Conditions is available at wikipedia \url{http://en.wikipedia.org/wiki/Boundary_conditions}}. In this example, we have utilised both conditions. Dirichlet is conceptually simpler and is used to prescribe a known value to the model on its boundary. This is like holding a snake by the tail; we know where the tail will be as we hold it however, we have no control over the rest of the snake. Dirichlet boundary conditions exist where we have applied our heat source, As the heat source is a constant, we can simulate its presence on that boundary. This is done by continuously resetting the temperature of the boundary, so that is is the same as the heat source.
98
99  Together with the natural boundary condition  Neumann boundary conditions describe the radiation or flux normal to the surface. This aptly describes our insulation conditions as we do not want to exert a constant temperature as with the heat source, however, we do want to prevent any loss of energy from the system. These natural boundary conditions can be described by specifying a radiation condition which prescribes the normal component of the flux $\kappa T\hackscore{,i}$ to be proportional
100    to the difference of the current temperature to the surrounding temperature $T\hackscore{ref}$;
101
102   \kappa T\hackscore{,i}^{(n)} n\hackscore i = \eta (T\hackscore{ref}-T^{(n)})   \kappa T\hackscore{,i} n\hackscore i = \eta (T\hackscore{ref}-T)
103  \label{DIFFUSION TEMP EQ 2222}  \label{eqn:hdbc}
104
105  taken from  where $\eta$ is a given material coefficient depending on the material of the block and the surrounding medium and $n\hackscore i$ is the $i$-th component of the outer normal field \index{outer normal field} at the surface of the domain. These two conditions form a boundary value problem that has to be solved for each time step. Note as $\eta = 0$ due to zero flux - no energy in or out - we do not need to worry about the Neumann terms for this example.
this forms a boundary value problem that has to be solved for each time step.
As a first step to implement a solver for the temperature diffusion problem we will
first implement a solver for the  boundary value problem that has to be solved at each time step.
106
107  We need to revisit the general PDE equation ~\ref{eqn:commonform nabla} under the light of a two dimensional domain. \ESCRIPT is inherently designed to solve problems that are greater than one dimension and so \ref{eqn:commonform nabla} needs to be read as a higher dimensional problem. In the case of two spatial dimensions the Nabla operator has in fact two components $\nabla = (\frac{\partial}{\partial x}, \frac{\partial}{\partial y})$. In full the general equation ~\ref{eqn:commonform nabla} assuming a constant coefficient $A$ takes the form;  \subsection{A \textit{1D} Clarification}
108    It is necessary for clarification that we revisit the general PDE from \refeq{eqn:commonform nabla} under the light of a two dimensional domain. \ESCRIPT is inherently designed to solve problems that are greater than one dimension and so \ref{eqn:commonform nabla} needs to be read as a higher dimensional problem. In the case of two spatial dimensions the Nabla operator has in fact two components $\nabla = (\frac{\partial}{\partial x}, \frac{\partial}{\partial y})$. In full \ref{eqn:commonform nabla} assuming a constant coefficient $A$ takes the form;
109  \label{eqn:commonform2D}  \label{eqn:commonform2D}
110  -A\hackscore{00}\frac{\partial^{2}u}{\partial x^{2}}  -A\hackscore{00}\frac{\partial^{2}u}{\partial x^{2}}
111  -A\hackscore{01}\frac{\partial^{2}u}{\partial x\partial y}  -A\hackscore{01}\frac{\partial^{2}u}{\partial x\partial y}
# Line 172  We need to revisit the general PDE equat Line 114  We need to revisit the general PDE equat
114  + Du = f  + Du = f
115
116  We notice that for the higher dimensional case $A$ becomes a matrix. It is also  We notice that for the higher dimensional case $A$ becomes a matrix. It is also
117  important to notice that the usage of the Nable operator creates  important to notice that the usage of the Nabla operator creates
118  a compact formulation which is also independant from the spatial dimension.  a compact formulation which is also independent from the spatial dimension.
119  So to make the general PDE~\ref{eqn:commonform2D} one dimensional as  So to make the general PDE~\ref{eqn:commonform2D} one dimensional as
120  shown in~\ref{eqn:commonform} we need to set  shown in~\ref{eqn:commonform} we need to set
121  \label{eqn:commonform2D}  \label{eqn:commonform2D}
122  A\hackscore{00}=A; A\hackscore{01}=A\hackscore{10}=A\hackscore{11}=0  A\hackscore{00}=A; A\hackscore{01}=A\hackscore{10}=A\hackscore{11}=0
123
124
125  Now that we have established the general form we will submit to escript it is necessary to establish the state of our system at time zero or $T^{(n=0)}$. This is due to the time derivative approximation we have used. We have chosen the starting temperature of our iron bar to be 0$\deg C$ . The temperature becomes;  \subsection{Developing a PDE Solution Script}
126    To solve \ref{eqn:hd} we will write a simple python script which uses the \modescript, \modfinley and \modmpl modules. At this point we assume that you have some basic understanding of the python programming language. If not there are some pointers and links available in Section \ref{sec:escpybas} .
T(x,0) = T\hackscore{ref} = 0

for all $x$ in the domain.
127
128  Because we have a symmetrical problem we will also need to set the symmetry on by:  Our goal here is to develop a script for \ESCRIPT that will solve the heat equation at successive time steps for a predefined period using our general form \ref{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 sin and cos functions or more complicated like those from our \ESCRIPT library.}
129    that we will require.
130  \begin{verbatim}  \begin{verbatim}
131   myPDE.setSymmetryOn()  from esys.escript import *
132    from esys.escript.linearPDEs import SingleLinearPDE
133    from esys.finley import Rectangle
134    from esys.escript.unitsSI import *
135    import os
136  \end{verbatim}  \end{verbatim}
137    It is generally a good idea to import all of the \modescript library, although if you know the packages you need you can specify them individually. The function \verb|LinearPDE| has been imported for ease of use later in the script. \verb|Rectangle| is going to be our type of domain. The package \verb unitsSI  is a module of \esc that provides support for units definitions with our variables; and the \verb|os| package is needed to handle file outputs once our PDE has been solved.
138
139  Additionally we must also consider the boundary conditions of our PDE. They take the form:  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 \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 \modescript which we will demonstrate in later tutorials but for our iron rod, we will simply use a rectangular domain.

\eta \hackscore{j} A\hackscore{jl} u\hackscore{,l} + du = y

140
141    Using a rectangular domain simplifies our rod which would probably be 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 centre. There are four arguments we must consider when we decide to create a rectangular domain, the model \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 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 \verb|ndx| would be 1 to 10\% of the length. Our \verb|ndy| need only be 1, This is because our problem stipulates no partial derivatives in the $y$ direction so the temperature does not vary with $y$. Thus the domain 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.
142    \begin{verbatim}
143    #Domain related.
144    mx = 1*m #meters - model lenght
145    my = .1*m #meters - model width
146    ndx = 100 # steps in x direction
147    ndy = 1 # steps in y direction
148    \end{verbatim}
149    The material constants and the temperature variables must also be defined. For the iron rod in the model they are defined as:
150    \begin{verbatim}
151    #PDE related
152    q=200. * Celsius #Kelvin - our heat source temperature
153    Tref = 0. * Celsius # Kelvin - starting temp of iron bar
154    rho = 7874. *kg/m**3 #kg/m^{3} density of iron
155    cp = 449.*J/(kg*K) #jules/Kg.K thermal capacity
156    rhocp = rho*cp
157    kappa = 80.*W/m/K #watts/m.K thermal conductivity
158    \end{verbatim}
159    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:
160    \begin{verbatim}
161    #Script/Iteration Related
162    t=0 #our start time, usually zero
163    tend=5.*60. #seconds - time to end simulation
164    outputs = 200 # number of time steps required.
165    h=(tend-t)/outputs #size of time step
166    i=0 #loop counter
167    #the folder to put our outputs in, leave blank "" for script path
168    #note this folder path must exist to work
169    save_path = "data/onedheatdiff001"
170    \end{verbatim}
171    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:
172    \begin{verbatim}
173     rod = Rectangle(l0=mx,l1=my,n0=ndx,n1=ndy)
174    \end{verbatim}
175    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] . Our domain is constructed this way to allow the user to determine where the discrete points of each model will be located. Discretisation may be at the corners of each cell, the middle point of a cell or halfway along each side of the cell etc. Depending on the PDE or the model there may be advantages and disadvantages for each case. Fortunately \modescript offers an easy way to extract finite points from the domain \verb|rod| using the domain property function \verb|getX()| . This function sets the vertices of each cell as finite points to solve in the solution. If we let \verb|x| be these finite points, then;
176    \begin{verbatim}
177     x = rod.getX()
178    \end{verbatim}
179    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\footnote{in comparison to a system of PDEs which will be discussed later.}. We also need to state the values of our general form variables.
180    \begin{verbatim}
181    mypde=LinearSinglePDE(rod)
182    mypde.setValue(A=kappa*kronecker(rod),D=rhocp/h)
183    \end{verbatim}
184
185  NEED TO WORK ON THIS SECTION  In a few special cases it may be possible to decrease the computational time of the solver if our PDE is symmetric. Symmetry of a PDE is defined by;
186    \label{eqn:symm}
187    A\hackscore{jl}=A\hackscore{lj}
188
189    Note that from the general form $D$ and $d$ may have any value. The right hand sides $Y$, $y$ as well as the constraints
190    are not relevant either. 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 will enable symmetry via;
191    \begin{verbatim}
192     myPDE.setSymmetryOn()
193    \end{verbatim}
194
195  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:  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. We will set our bar to:
196  \begin{verbatim}  \begin{verbatim}
197   T = Tref   T = Tref
198  \end{verbatim}  \end{verbatim}
199  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  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
200    \begin{verbatim}
201    # extract finite points
202    x=rod.getX()
203    # ... set heat source: ....
204    qH=q*whereZero(x[0])
205    \end{verbatim}
206
207  END WORK ON THIS SECTION  Finally we will initialise an iteration loop to solve our PDE for all the time steps we specified in the variable section. As the RHS of the general form is dependent on the previous values for temperature \verb T  across the bar this must be updated in the loop.
208    \begin{verbatim}
209    # ... start iteration:
210    while t<=tend:
211          i+=1
212          t+=h
213          mypde.setValue(Y=qH+rhocp/h*T)
214          T=mypde.getSolution()
215          #some plotting stuff here - waiting on matplotlib binary distribution
216    \end{verbatim}
217
218  \section{Plot total heat}  \subsection{Plot total heat}
219  \TODO{show the script}  \TODO{show the script}
220
221  \TODO{explain how to use matlibplot to visualize the total heat integral(rho*c*T) over time}  \TODO{explain how to use matlibplot to visualize the total heat integral(rho*c*T) over time}
222
223  \section{Plot Temperature Distribution}  \subsection{Plot Temperature Distribution}
224  \TODO{explain how to use matlibplot to visualize T}  \TODO{explain how to use matlibplot to visualize T}
225
226

Legend:
 Removed from v.2494 changed lines Added in v.2495