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

Revision 2801 - (hide annotations)
Thu Dec 3 01:45:48 2009 UTC (10 years, 1 month ago) by ahallam
Original Path: trunk/doc/cookbook/onedheatdiff001.tex
File MIME type: application/x-tex
File size: 23997 byte(s)
Final updates to first 3 chapters of cookbook before moving on to linear elastic wave equation examples.

 1 ahallam 2401 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 ahallam 2801 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 \esc and demonstrate 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 \esc and implementation should become a clearer as we develop our understanding further into the cookbook.} 15 16 ahallam 2401 \section{One Dimensional Heat Diffusion in an Iron Rod} 17 ahallam 2658 \sslist{onedheatdiff001.py and cblib.py} 18 ahallam 2401 %\label{Sec:1DHDv0} 19 ahallam 2801 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 that of the heat source. 20 ahallam 2494 \begin{figure}[h!] 21 \centerline{\includegraphics[width=4.in]{figures/onedheatdiff}} 22 \caption{One dimensional model of an Iron bar.} 23 \label{fig:onedhdmodel} 24 \end{figure} 25 ahallam 2495 \subsection{1D Heat Diffusion Equation} 26 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 ahallam 2494 which is defined as: 28 ahallam 2401 \begin{equation} 29 \rho c\hackscore p \frac{\partial T}{\partial t} - \kappa \frac{\partial^{2} T}{\partial x^{2}} = q\hackscore H 30 \label{eqn:hd} 31 \end{equation} 32 ahallam 2495 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 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$. 34 ahallam 2401 35 ahallam 2801 \subsection{\esc, PDEs and The General Form} 36 ahallam 2775 Potentially, it is now possible to solve \ref{eqn:hd} analytically and 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 a more complex visualisation is required. To do this, a numerical approach is required - \esc 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 gross 2477 38 ahallam 2775 \esc interfaces with any given PDE via a general form. In this example we will illustrate a simpler version of the full linear PDE general form which is available in the \esc user's guide. A simplified form that suits our heat diffusion problem\footnote{In the form of the \esc users guide which using the Einstein convention is written as 39 gross 2477 $-(A\hackscore{jl} u\hackscore{,l})\hackscore{,j}+D u =Y$} 40 ahallam 2606 is described by; 41 gross 2477 \begin{equation}\label{eqn:commonform nabla} 42 jfenwick 2657 -\nabla\cdot(A\cdot\nabla u) + Du = f 43 ahallam 2411 \end{equation} 44 ahallam 2494 where $A$, $D$ and $f$ are known values. The symbol $\nabla$ which is called the \textit{Nabla operator} or \textit{del operator} represents 45 ahallam 2495 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 gross 2477 \begin{equation} 47 \nabla = \frac{\partial}{\partial x} 48 \end{equation} 49 ahallam 2495 and we can write \ref{eqn:commonform nabla} as; 50 ahallam 2411 \begin{equation}\label{eqn:commonform} 51 gross 2477 -A\frac{\partial^{2}u}{\partial x^{2}} + Du = f 52 ahallam 2411 \end{equation} 53 ahallam 2645 if $A$ is constant then \ref{eqn:commonform} is consistent with our heat diffusion problem in \ref{eqn:hd} with the exception of $u$. Thus when comparing equations \ref{eqn:hd} and \ref{eqn:commonform} we see that; 54 ahallam 2411 \begin{equation} 55 ahallam 2494 A = \kappa; D = \rho c \hackscore{p}; f = q \hackscore{H} 56 ahallam 2411 \end{equation} 57 gross 2477 58 ahallam 2495 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 ahallam 2494 \begin{equation} 60 ahallam 2645 \frac{\partial f(x)}{\partial x} \approx \frac{f(x+h)-f(x)}{h} 61 ahallam 2494 \label{eqn:beuler} 62 \end{equation} 63 where h is the the discrete step size $\Delta x$. 64 ahallam 2645 Now if the temperature $T(t)$ is substituted in by letting $f(x) = T(t)$ then from \ref{eqn:beuler} we see that; 65 ahallam 2494 \begin{equation} 66 T'(t) \approx \frac{T(t+h) - T(t)}{h} 67 \end{equation} 68 which can also be written as; 69 \begin{equation} 70 ahallam 2645 \frac{\partial T^{(n)}}{\partial t} \approx \frac{T^{(n)} - T^{(n-1)}}{h} 71 ahallam 2494 \label{eqn:Tbeuler} 72 \end{equation} 73 ahallam 2495 where $n$ denotes the n\textsuperscript{th} time step. Substituting \ref{eqn:Tbeuler} into \ref{eqn:hd} we get; 74 ahallam 2494 \begin{equation} 75 \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} 77 \end{equation} 78 ahallam 2606 To fit our simplified general form we can rearrange \ref{eqn:hddisc}; 79 ahallam 2494 \begin{equation} 80 ahallam 2645 \frac{\rho c\hackscore p}{h} T^{(n)} - \kappa \frac{\partial^2 T^{(n)}}{\partial x^2} = q\hackscore H + \frac{\rho c\hackscore p}{h} T^{(n-1)} 81 ahallam 2494 \label{eqn:hdgenf} 82 \end{equation} 83 ahallam 2775 The PDE is now in a form that satisfies \refEq{eqn:commonform nabla} which is required for \esc to solve our PDE. This can be done by generating a solution for successive increments in the time nodes $t^{(n)}$ where 84 ahallam 2495 $t^{(0)}=0$ and $t^{(n)}=t^{(n-1)}+h$ where $h>0$ is the step size and assumed to be constant. 85 ahallam 2645 In the following the upper index ${(n)}$ refers to a value at time $t^{(n)}$. Finally, by comparing \ref{eqn:hdgenf} with \ref{eqn:commonform} it can be seen that; 86 ahallam 2494 \begin{equation} 87 A = \kappa; D = \frac{\rho c \hackscore{p}}{h}; f = q \hackscore{H} + \frac{\rho c\hackscore p}{h} T^{(n-1)} 88 \end{equation} 89 90 ahallam 2775 Now that the general form has been established, it can be submitted to \esc. Note that 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}. Our model stipulates a starting temperature in the iron bar of 0\textcelsius. Thus the temperature distribution is simply; 91 ahallam 2495 \begin{equation} 92 T(x,0) = T\hackscore{ref} = 0 93 \end{equation} 94 for all $x$ in the domain. 95 ahallam 2494 96 ahallam 2495 \subsection{Boundary Conditions} 97 ahallam 2801 With the PDE sufficiently modified, 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. For this model 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 it is the same as the heat source. 98 ahallam 2495 99 ahallam 2801 Neumann boundary conditions describe the radiation or flux that is normal to the boundary 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. 100 101 While the flux for this model is zero, it is important to note the requirements for Neumann boundary conditions. For heat diffusion these can be described by specifying a radiation condition which prescribes the normal component of the flux $\kappa T\hackscore{,i}$ to be proportional 102 ahallam 2645 to the difference of the current temperature to the surrounding temperature $T\hackscore{ref}$; in general terms this is; 103 ahallam 2494 \begin{equation} 104 ahallam 2681 \kappa T\hackscore{,i} \hat{n}\hackscore i = \eta (T\hackscore{ref}-T) 105 ahallam 2495 \label{eqn:hdbc} 106 ahallam 2494 \end{equation} 107 ahallam 2645 and simplified to our one dimensional model we have; 108 \begin{equation} 109 ahallam 2681 \kappa \frac{\partial T}{\partial dx} \hat{n}\hackscore x = \eta (T\hackscore{ref}-T) 110 ahallam 2645 \end{equation} 111 ahallam 2681 where $\eta$ is a given material coefficient depending on the material of the block and the surrounding medium and $\hat{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. Due to the perfect insulation in our model we can set $\eta = 0$ which results in zero flux - no energy in or out - we do not need to worry about the Neumann terms of the general form for this example. 112 ahallam 2494 113 ahallam 2495 \subsection{A \textit{1D} Clarification} 114 ahallam 2775 It is necessary for clarification that we revisit the general PDE from \refeq{eqn:commonform nabla} under the light of a two dimensional domain. \esc 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 \textit{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; 115 gross 2477 \begin{equation}\label{eqn:commonform2D} 116 -A\hackscore{00}\frac{\partial^{2}u}{\partial x^{2}} 117 -A\hackscore{01}\frac{\partial^{2}u}{\partial x\partial y} 118 -A\hackscore{10}\frac{\partial^{2}u}{\partial y\partial x} 119 -A\hackscore{11}\frac{\partial^{2}u}{\partial y^{2}} 120 + Du = f 121 \end{equation} 122 ahallam 2606 Notice that for the higher dimensional case $A$ becomes a matrix. It is also 123 ahallam 2495 important to notice that the usage of the Nabla operator creates 124 a compact formulation which is also independent from the spatial dimension. 125 gross 2477 So to make the general PDE~\ref{eqn:commonform2D} one dimensional as 126 shown in~\ref{eqn:commonform} we need to set 127 ahallam 2606 \begin{equation} 128 ahallam 2494 A\hackscore{00}=A; A\hackscore{01}=A\hackscore{10}=A\hackscore{11}=0 129 gross 2477 \end{equation} 130 131 ahallam 2495 \subsection{Developing a PDE Solution Script} 132 ahallam 2801 \label{sec:key} 133 To solve the heat diffusion equation (equation \ref{eqn:hd}) we will write a simple \pyt script which uses the \modescript, \modfinley and \modmpl modules. At this point we assume that you have some basic understanding of the \pyt programming language. If not there are some pointers and links available in Section \ref{sec:escpybas} . 134 gross 2477 135 ahallam 2801 By developing a script for \esc, the heat diffusion equation can be solved 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 $sine$ and $cosine$ functions or more complicated like those from our \esc library.} 136 ahallam 2495 that we will require. 137 ahallam 2775 \begin{python} 138 ahallam 2495 from esys.escript import * 139 ahallam 2606 # This defines the LinearPDE module as LinearPDE 140 from esys.escript.linearPDEs import LinearPDE 141 # This imports the rectangle domain function from finley. 142 from esys.finley import Rectangle 143 # A useful unit handling package which will make sure all our units 144 # match up in the equations under SI. 145 from esys.escript.unitsSI import * 146 import pylab as pl #Plotting package. 147 import numpy as np #Array package. 148 import os #This package is necessary to handle saving our data. 149 ahallam 2775 \end{python} 150 ahallam 2801 It is generally a good idea to import all of the \modescript library, although if the functions and classes required are known they can be specified individually. The function \verb|LinearPDE| has been imported explicitly for ease of use later in the script. \verb|Rectangle| is going to be our type of model. The module \verb unitsSI provides support for SI unit definitions with our variables; and the \verb|os| module is needed to handle file outputs once our PDE has been solved. \verb pylab and \verb numpy are modules developed independently of \esc. They are used because they have efficient plotting and array handling capabilities. 151 gross 2477 152 ahallam 2801 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 \esc 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 model upon which we wish to solve our problem needs to be defined. There are many different types of models in \modescript which we will demonstrate in later tutorials but for our iron rod, we will simply use a rectangular model. 153 ahallam 2401 154 ahallam 2801 Using a rectangular model simplifies our rod which would 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 model, 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 model 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 this \textit{1D} problem, the bar is defined as being 1 metre long. An appropriate step size \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. Thus the temperature does not vary with $y$. Hence, the model 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. 155 ahallam 2775 \begin{python} 156 ahallam 2495 #Domain related. 157 ahallam 2606 mx = 1*m #meters - model length 158 ahallam 2495 my = .1*m #meters - model width 159 ahallam 2606 ndx = 100 # mesh steps in x direction 160 ndy = 1 # mesh steps in y direction - one dimension means one element 161 ahallam 2775 \end{python} 162 ahallam 2495 The material constants and the temperature variables must also be defined. For the iron rod in the model they are defined as: 163 ahallam 2775 \begin{python} 164 ahallam 2495 #PDE related 165 q=200. * Celsius #Kelvin - our heat source temperature 166 ahallam 2606 Tref = 0. * Celsius #Kelvin - starting temp of iron bar 167 ahallam 2495 rho = 7874. *kg/m**3 #kg/m^{3} density of iron 168 ahallam 2606 cp = 449.*J/(kg*K) #j/Kg.K thermal capacity 169 ahallam 2495 rhocp = rho*cp 170 ahallam 2606 kappa = 80.*W/m/K #watts/m.Kthermal conductivity 171 ahallam 2775 \end{python} 172 ahallam 2495 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: 173 ahallam 2775 \begin{python} 174 ahallam 2495 t=0 #our start time, usually zero 175 ahallam 2606 tend=5.*minute #seconds - time to end simulation 176 ahallam 2495 outputs = 200 # number of time steps required. 177 h=(tend-t)/outputs #size of time step 178 ahallam 2606 #user warning statement 179 print "Expected Number of time outputs is: ", (tend-t)/h 180 i=0 #loop counter 181 #the folder to put our outputs in, leave blank "" for script path 182 save_path="data/onedheatdiff001" 183 ahallam 2775 \end{python} 184 ahallam 2606 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: 185 ahallam 2775 \begin{python} 186 ahallam 2606 #generate domain using rectangle 187 rod = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy) 188 ahallam 2775 \end{python} 189 ahallam 2801 \verb rod now describes a domain in the manner of Section \ref{ss:domcon}. As we define our variables, various function spaces will be created to accommodate them. There is 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; 190 ahallam 2775 \begin{python} 191 ahallam 2606 #extract finite points - the solution points 192 x=rod.getX() 193 ahallam 2775 \end{python} 194 ahallam 2801 The data locations of specific function spaces can be returned in a similar manner by extracting the relevant function space from the domain followed by the \verb .getX() operator. 195 ahallam 2658 196 ahallam 2775 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 \esc. 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. 197 \begin{python} 198 ahallam 2495 mypde=LinearSinglePDE(rod) 199 mypde.setValue(A=kappa*kronecker(rod),D=rhocp/h) 200 ahallam 2775 \end{python} 201 ahallam 2401 202 ahallam 2495 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; 203 \begin{equation}\label{eqn:symm} 204 A\hackscore{jl}=A\hackscore{lj} 205 \end{equation} 206 ahallam 2801 Symmetry is only dependent on the $A$ coefficient in the general form and the other coefficients $D$ and $d$ as well as the RHS $Y$ and $y$ may take any value. 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; 207 ahallam 2775 \begin{python} 208 ahallam 2495 myPDE.setSymmetryOn() 209 ahallam 2775 \end{python} 210 ahallam 2401 211 ahallam 2801 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 model. We will set our bar to: 212 ahallam 2775 \begin{python} 213 ahallam 2401 T = Tref 214 ahallam 2775 \end{python} 215 ahallam 2801 Boundary conditions are a little more difficult. Fortunately the \esc solver will handle our insulated boundary conditions by default with a zero flux operator. However, we will need to apply our heat source $q_{H}$ to the end of the bar at $x=0$ . \esc makes this easy by letting us define areas in our model. The finite points in the model were previously defined as \verb x and it is possible to set all of points that satisfy $x=0$ to \verb q via the \verb whereZero() function. There are a few \verb where functions available in \esc. They will return a value \verb 1 where they are satisfied and \verb 0 where they are not. In this case our \verb qH is only applied to the far LHS of our model as required. 216 ahallam 2775 \begin{python} 217 ahallam 2495 # ... set heat source: .... 218 qH=q*whereZero(x) 219 ahallam 2775 \end{python} 220 ahallam 2401 221 ahallam 2801 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. Our output at each time step is \verb T the heat distribution and \verb totT the total heat in the system. 222 ahallam 2775 \begin{python} 223 ahallam 2495 while t<=tend: 224 ahallam 2606 i+=1 #increment the counter 225 t+=h #increment the current time 226 mypde.setValue(Y=qH+rhocp/h*T) #set variable PDE coefficients 227 T=mypde.getSolution() #get the PDE solution 228 totT = rhocp*T #get the total heat solution in the system 229 ahallam 2775 \end{python} 230 ahallam 2401 231 ahallam 2606 \subsection{Plotting the heat solutions} 232 ahallam 2801 Visualisation of the solution can be achieved using \mpl a module contained within \pylab. We start by modifying our solution script from before. Prior to the \verb while loop we will need to extract our finite solution points to a data object that is compatible with \mpl. First it is necessary to convert \verb x to a list of tuples. These are then converted to a \numpy array and the $x$ locations extracted via an array slice to the variable \verb plx . 233 ahallam 2775 \begin{python} 234 ahallam 2606 #convert solution points for plotting 235 plx = x.toListOfTuples() 236 plx = np.array(plx) #convert to tuple to numpy array 237 plx = plx[:,0] #extract x locations 238 ahallam 2775 \end{python} 239 ahallam 2801 As there are two solution outputs, we will generate two plots and save each to a file for every time step in the solution. The following is appended to the end of the \verb while loop and creates two figures. The first figure is for the temperature distribution, and the second the total temperature in the bar. Both cases are similar with a few minor changes for scale and labelling. We start by converting the solution to a tuple and then plotting this against our \textit{x coordinates} \verb plx from before. The axis is then standardised and a title applied. Finally, the figure is saved to a *.png file and cleared for the following iteration. 240 ahallam 2775 \begin{python} 241 ahallam 2606 #establish figure 1 for temperature vs x plots 242 tempT = T.toListOfTuples(scalarastuple=False) 243 pl.figure(1) #current figure 244 pl.plot(plx,tempT) #plot solution 245 #define axis extents and title 246 pl.axis([0,1.0,273.14990+0.00008,0.004+273.1499]) 247 pl.title("Temperature accross Rod") 248 #save figure to file 249 pl.savefig(os.path.join(save_path+"/tempT","rodpyplot%03d.png") %i) 250 pl.clf() #clear figure 251 252 #establish figure 2 for total temperature vs x plots and repeat 253 tottempT = totT.toListOfTuples(scalarastuple=False) 254 pl.figure(2) 255 pl.plot(plx,tottempT) 256 pl.axis([0,1.0,9.657E08,12000+9.657E08]) 257 ahallam 2801 pl.title("Total temperature across Rod") 258 ahallam 2606 pl.savefig(os.path.join(save_path+"/totT","ttrodpyplot%03d.png")%i) 259 pl.clf() 260 ahallam 2775 \end{python} 261 ahallam 2645 \begin{figure} 262 \begin{center} 263 \includegraphics[width=4in]{figures/ttrodpyplot150} 264 \caption{Total temperature ($T$) distribution in rod at $t=150$} 265 \label{fig:onedheatout} 266 \end{center} 267 \end{figure} 268 269 jfenwick 2657 \subsubsection{Parallel scripts (MPI)} 270 ahallam 2801 In some of the example files for this cookbook the plotting commands are a little different. 271 jfenwick 2657 For example, 272 ahallam 2775 \begin{python} 273 jfenwick 2657 if getMPIRankWorld() == 0: 274 pl.savefig(os.path.join(save_path+"/totT","ttrodpyplot%03d.png")%i) 275 pl.clf() 276 ahallam 2775 \end{python} 277 jfenwick 2657 278 The additional \verb if statement is not necessary for normal desktop use. 279 It becomes important for scripts run on parallel computers. 280 Its purpose is to ensure that only one copy of the file is written. 281 ahallam 2801 For more details on writing scripts for parallel computing please consult the \emph{user's guide}. 282 jfenwick 2657 283 ahallam 2606 \subsection{Make a video} 284 Our saved plots from the previous section can be cast into a video using the following command appended to the end of the script. \verb mencoder is linux only however, and other platform users will need to use an alternative video encoder. 285 ahallam 2775 \begin{python} 286 ahallam 2606 # compile the *.png files to create two *.avi videos that show T change 287 jfenwick 2657 # with time. This operation uses linux mencoder. For other operating 288 ahallam 2606 # systems it is possible to use your favourite video compiler to 289 # convert image files to videos. 290 gross 2477 291 ahallam 2606 os.system("mencoder mf://"+save_path+"/tempT"+"/*.png -mf type=png:\ 292 w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \ 293 onedheatdiff001tempT.avi") 294 gross 2477 295 ahallam 2606 os.system("mencoder mf://"+save_path+"/totT"+"/*.png -mf type=png:\ 296 w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \ 297 onedheatdiff001totT.avi") 298 ahallam 2775 \end{python} 299 gross 2477