 Contents of /trunk/doc/cookbook/TEXT/onedheatdiff.tex

Revision 2397 - (show annotations)
Fri Apr 24 04:24:28 2009 UTC (10 years, 5 months ago) by ahallam
File MIME type: application/x-tex
File size: 8195 byte(s)
End Week 4, Finalised 1D examples, need work on text but script ok.
Begining of 2D example builds on 1D. Script semi-finalised, text to complete.
 1 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 % 4 % Copyright (c) 2003-2009 by University of Queensland 5 % Earth Systems Science Computational Center (ESSCC) 6 7 % 8 % Primary Business: Queensland, Australia 9 % Licensed under the Open Software License version 3.0 10 11 % 12 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 13 14 \documentclass{manual} 15 \title{One Dimensional Heat Equation} 16 \newcommand{\editor} {\textcolor{red}{#1}} 17 18 \begin{document} 19 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. 20 21 Start by imagining we have a simple cold iron bar at a constant temperature of zero which is perfectly insulated on either side. 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. Essentially the heat will spread along the bar 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 \begin{equation} 25 \rho c\hackscore p \frac{\partial T}{\partial t} - \kappa \frac{\partial^{2} T}{\partial x^{2}} = q\hackscore H 26 \label{DIFFUSION TEMP EQ 1} 27 \end{equation} 28 where $\rho c\hackscore p$ and $\kappa$ are given material constants and $q\hackscore H$ is our heat source. Equation \eqn{DIFFUSION TEMP EQ 1} contains two partial derivatives. $\frac{\partial T}{\partial t}$ describes the change in temperature with time while $\frac{\partial T}{\partial x}$ describes the spatial change to temperature. Because we only have a single dimension, our problem is thus only dependent on time and our position along the iron bar $x$ . 29 30 To solve this equation we will write a simple python script. The first step is to import the necessary libraries. 31 \begin{verbatim} 32 from esys.escript import * 33 from esys.escript.linearPDEs import LinearPDE 34 from esys.finley import Rectangle 35 import os 36 \end{verbatim} 37 It is generally a good idea to import all of 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. The \verb|os| package is needed to handle file outputs once our PDE has been solved. 38 39 Once our libraries have been imported we will assign values to our problem specific variables. 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. 40 For this PDE there are a number of constants which will need values. They are: 41 \begin{verbatim} 42 q #our heat souce temperature 43 Tref #the starting temperature of our ironbar 44 rho 45 eta 46 kappa 47 T #The current temperature - this is our solutions. 48 \end{verbatim} 49 To control our script we will have to specify: 50 \begin{verbatim} 51 t #our start time, usually zero 52 tend #the time we want to end the simulation 53 h #size of the time step 54 55 i #our loop counter 56 save_path #the folder to put our outputs in, leave blank "" for script path 57 58 \end{verbatim} 59 The next step is to create a domain upon which we wish to solve our problem. There are many different types of domains in escript. We will use many variations in later tutorials but for our Iron rod we will simply use a rectangular domain. This can be thought of as the lengthways cross section of our rod. 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. These arguments are: 60 \begin{verbatim} 61 lenbr # length of the bar 62 widbr # the width/diameter of the bar 63 xstep # the number of discrete spatial steps along x direction 64 ystep # the number of discrete spatial steps along y direction 65 \end{verbatim} 66 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 calculated per time step. This means more computational time is required to produce a solution. 67 In our \textit{1D} problem we will define our bar as being 1 metre long. An appropriate \verb|xstep| would be 1 to 10\% of the length. Our \verb|ystep| need only be 1. This is because our problem stipulates no partial derivatives in the y direction. This means our temperature does not vary in the y direction. 68 Now that we know our inputs we will define our domain as: 69 \begin{verbatim} 70 rod = Rectangle(l0=lenbr,l1=widbr,n0=xstep,n1=ystep) 71 \end{verbatim} 72 Escript offers an easy way to extract our finite points from the domain \verb|rod| this is the property \verb|getX()| . If we let \verb|x| be these finite points, then: 73 \begin{verbatim} 74 x = rod.getX() 75 \end{verbatim} 76 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 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: 77 \begin{verbatim} 78 mypde=LinearPDE(rod) 79 \end{verbatim} 80 Because we have a symmetrical problem we will also need to set the symmetry on by: 81 \begin{verbatim} 82 myPDE.setSymmetryOn() 83 \end{verbatim} 84 The final step is to apply our PDE to the general form of a linear PDE as described in the escript manual. The general form equation is: 85 \begin{equation}\label{LINEARPDE.SINGLE.1} 86 -(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 \; . 87 \end{equation} 88 89 NEED TO WORK ON THIS SECTION 90 91 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: 92 \begin{verbatim} 93 T = Tref 94 \end{verbatim} 95 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 96 97 END WORK ON THIS SECTION 98 99 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; 100 \begin{itemize} 101 \item \verb 0 becomes the padding number; 102 \item \verb 3 tells us the number of padding numbers that are required; and 103 \item \verb d indicates the end of the \verb % operator. 104 \end{itemize} 105 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: 106 \begin{verbatim} 107 saveVTK(os.path.join(save_path,"data%03d.xml") %i,sol=T) 108 \end{verbatim} 109 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. 110 \end{document}