ViewVC logotype

Annotation of /trunk/doc/cookbook/example01.tex

Parent Directory Parent Directory | Revision Log Revision Log

Revision 2477 - (hide annotations)
Wed Jun 17 06:22:15 2009 UTC (10 years, 3 months ago) by gross
Original Path: trunk/doc/cookbook/TEXT/onedheatdiff001.tex
File MIME type: application/x-tex
File size: 14664 byte(s)
modification to the cookbook
1 ahallam 2401
2     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3     %
4     % Copyright (c) 2003-2009 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/osl-3.0.php
11     %
12     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14     \section{One Dimensional Heat Diffusion in an Iron Rod}
15     %\label{Sec:1DHDv0}
16 gross 2477 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 ahallam 2401
23 gross 2477
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 set-up.}
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.
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 ahallam 2401 \begin{equation}
32     \rho c\hackscore p \frac{\partial T}{\partial t} - \kappa \frac{\partial^{2} T}{\partial x^{2}} = q\hackscore H
33     \label{eqn:hd}
34     \end{equation}
35 gross 2477 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 pre-defined 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 ahallam 2401
38 gross 2477 \TODO{Explain the concept of time discritzation}
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 on-line, for instance
44     \begin{itemize}
45     \item This is a very crisp introduction \url{http://hetland.org/writing/instant-python.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 step-by-step.
52     The first step is to import the necessary libraries.
53 ahallam 2401 \begin{verbatim}
54     from esys.escript import *
55 gross 2477 from esys.escript.linearPDEs import SingleLinearPDE
56 ahallam 2401 from esys.finley import Rectangle
57     import os
58     \end{verbatim}
59     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 \verb|LinearPDE| has been imported individually for ease of use later in the script. \verb|Rectangle| is going to be our type of domain and the \verb|os| package is needed to handle file outputs once our PDE has been solved.
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.
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 \verb|ndx| would be 1 to 10\% of the length. Our \verb|ndy| 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 gross 2477 \TODO{Use the Unit module!}
65 ahallam 2401 \begin{verbatim}
66     #Domain related.
67     mx = 1 #meters - model lenght
68     my = .1 #meters - model width
69     ndx = 100 # steps in x direction
70     ndy = 1 # steps in y direction
71     \end{verbatim}
72     The material constants and the temperature variables must also be defined. For the iron rod in the model they are defined as:
73     \begin{verbatim}
74     #PDE related
75     q=473. #Kelvin - our heat source temperature
76     Tref = 273. # Kelvin - starting temp of iron bar
77     rho = 7874. #kg/m^{3} density of iron
78     cp = 449. #j/Kg.K
79     rhocp = rho*cp
80     eta = 0 #radiation condition
81     kappa = 68. #temperature diffusion constant
82     \end{verbatim}
83 gross 2477 \TODO{remove radiation condition and -if required- introduce in a second example}
84 ahallam 2401 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}
86     #Script/Iteration Related
87     t=0 #our start time, usually zero
88     tend=5.*60. #seconds - time to end simulation
89     outputs = 200 # number of time steps required.
90     h=(tend-t)/outputs #size of time step
91     i=0 #loop counter
92     #the folder to put our outputs in, leave blank "" for script path
93     #note this folder path must exist to work
94     save_path = "data/onedheatdiff001"
95     \end{verbatim}
96     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:
97     \begin{verbatim}
98     rod = Rectangle(l0=mx,l1=my,n0=ndx,n1=ndy)
99     \end{verbatim}
100     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 \verb|rod| using the domain property function \verb|getX()| . This function uses the vertices of each cell as finite points to solve in the solution. If we let \verb|x| be these finite points, then:
101     \begin{verbatim}
102     x = rod.getX()
103     \end{verbatim}
104 gross 2477 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 ahallam 2401 \begin{verbatim}
107 gross 2477 mypde=LinearSinglePDE(rod)
108 ahallam 2401 \end{verbatim}
109 gross 2477 In the next step we need to define the coefficients of the PDE. The linear
110     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     \footnote{In the form of the \ESCRIPT users guide which uses the Einstein convention
112     this equation is written as
113     $-(A\hackscore{jl} u\hackscore{,l})\hackscore{,j}+D u =Y$}
114     is described by
115     \begin{equation}\label{eqn:commonform nabla}
116 ahallam 2411 -\nabla.(A.\nabla u) + Du = f
117     \end{equation}
118 gross 2477 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 one-dimensional 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 ahallam 2411 \begin{equation}\label{eqn:commonform}
126 gross 2477 -A\frac{\partial^{2}u}{\partial x^{2}} + Du = f
127 ahallam 2411 \end{equation}
128 gross 2477 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 ahallam 2411 \begin{equation}
131 gross 2477 A = \kappa; D = \frac{\rho c \hackscore{p}}{h}; f = q \hackscore{H}
132 ahallam 2411 \end{equation}
133 gross 2477
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 one-dimensional 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}
158     BLA-BLA
159     \TODO{explain boundray condition; ignore radiation}
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
174 ahallam 2411 Additionally we must also consider the boundary conditions of our PDE. They take the form:
175     \begin{equation}
176     \eta \hackscore{j} A\hackscore{jl} u\hackscore{,l} + du = y
177     \end{equation}
178 ahallam 2401
182     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:
183     \begin{verbatim}
184     T = Tref
185     \end{verbatim}
186     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
190 gross 2477 \section{Plot total heat}
191     \TODO{show the script}
193     \TODO{explain how to use matlibplot to visualize the total heat integral(rho*c*T) over time}
195     \section{Plot Temperature Distribution}
196     \TODO{explain how to use matlibplot to visualize T}
199     \TODO{Move this to the 2D section as an advanced topic}
200 ahallam 2401 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 \verb|saveVTK()| which makes this step very easy. saveVTK takes two arguments, the path and the filename. We are goind to use the \verb|os.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 \verb|i|. In substring \verb %03d does a number of things;
201     \begin{itemize}
202     \item \verb 0 becomes the padding number;
203     \item \verb 3 tells us the number of padding numbers that are required; and
204     \item \verb d indicates the end of the \verb % operator.
205     \end{itemize}
206     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:
207     \begin{verbatim}
208     saveVTK(os.path.join(save_path,"data%03d.xml") %i,sol=T)
209     \end{verbatim}
210     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.

  ViewVC Help
Powered by ViewVC 1.1.26