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

revision 2495 by ahallam, Fri Jun 26 03:39:21 2009 UTC revision 2606 by ahallam, Thu Aug 13 04:32:23 2009 UTC
# Line 33  where $\rho$ is the material density, $c Line 33 where$\rho$is the material density,$c
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$.  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
35  \subsection{Escript, PDEs and The General Form}  \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.  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 - \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  \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  \ESCRIPT 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 \ESCRIPT user's guide. A simplified form that suits our heat diffusion problem\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  and is described by;  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
# Line 50  and we can write \ref{eqn:commonform nab Line 50  and we can write \ref{eqn:commonform nab
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  \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;  if $A$ is constant then  \ref{eqn:commonform} is consistent with our heat diffusion problem in \ref{eqn:hd} with the exception of $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
# Line 75  where $n$ denotes the n\textsuperscript{ Line 75  where $n$ denotes the n\textsuperscript{
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 \ref{eqn:hddisc} so that;  To fit our simplified general form we can rearrange \ref{eqn:hddisc};
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 and 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 \ref{eqn:hdgenf} with \ref{eqn:commonform} we see that;  In the following the upper index ${(n)}$ refers to a value at time $t^{(n)}$. Now when comparing \ref{eqn:hdgenf} with \ref{eqn:commonform} it can be seen 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  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;  Now that the general form has been established, it can be submitted to \ESCRIPT. 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
92  T(x,0) = T\hackscore{ref} = 0  T(x,0) = T\hackscore{ref} = 0
93
94  for all $x$ in the domain.  for all $x$ in the domain.
95
96  \subsection{Boundary Conditions}  \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.    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. 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  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  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}$;  to the difference of the current temperature to the surrounding temperature $T\hackscore{ref}$;
101
102   \kappa T\hackscore{,i} n\hackscore i = \eta (T\hackscore{ref}-T)   \kappa T\hackscore{,i} n\hackscore i = \eta (T\hackscore{ref}-T)
103  \label{eqn:hdbc}  \label{eqn:hdbc}
104
105  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.  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. Due to the perfect insulation in our model $\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.
106
107  \subsection{A \textit{1D} Clarification}  \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;  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 \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;
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 113  It is necessary for clarification that w Line 113  It is necessary for clarification that w
113  -A\hackscore{11}\frac{\partial^{2}u}{\partial y^{2}}  -A\hackscore{11}\frac{\partial^{2}u}{\partial y^{2}}
114  + Du = f  + Du = f
115
116  We notice that for the higher dimensional case $A$ becomes a matrix. It is also  Notice that for the higher dimensional case $A$ becomes a matrix. It is also
117  important to notice that the usage of the Nabla operator creates  important to notice that the usage of the Nabla operator creates
118  a compact formulation which is also independent 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}
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
# Line 129  Our goal here is to develop a script for Line 129  Our goal here is to develop a script for
129  that we will require.  that we will require.
130  \begin{verbatim}  \begin{verbatim}
131  from esys.escript import *  from esys.escript import *
132  from esys.escript.linearPDEs import SingleLinearPDE  # This defines the LinearPDE module as LinearPDE
133  from esys.finley import Rectangle  from esys.escript.linearPDEs import LinearPDE
134  from esys.escript.unitsSI import *  # This imports the rectangle domain function from finley.
135  import os  from esys.finley import Rectangle
136    # A useful unit handling package which will make sure all our units
137    # match up in the equations under SI.
138    from esys.escript.unitsSI import *
139    import pylab as pl #Plotting package.
140    import numpy as np #Array package.
141    import os #This package is necessary to handle saving our data.
142  \end{verbatim}  \end{verbatim}
143  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.  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. \verb pylab  and \verb numpy  are modules developed independently of \esc. They are used because they have efficient plotting and array handling capabilities.
144
145  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.  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.
146
147  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.  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.
148  \begin{verbatim}  \begin{verbatim}
149  #Domain related.  #Domain related.
150  mx = 1*m #meters - model lenght  mx = 1*m #meters - model length
151  my = .1*m #meters - model width  my = .1*m #meters - model width
152  ndx = 100 # steps in x direction  ndx = 100 # mesh steps in x direction
153  ndy = 1 # steps in y direction  ndy = 1 # mesh steps in y direction - one dimension means one element
154  \end{verbatim}  \end{verbatim}
155  The material constants and the temperature variables must also be defined. For the iron rod in the model they are defined as:  The material constants and the temperature variables must also be defined. For the iron rod in the model they are defined as:
156  \begin{verbatim}  \begin{verbatim}
157  #PDE related  #PDE related
158  q=200. * Celsius #Kelvin - our heat source temperature  q=200. * Celsius #Kelvin - our heat source temperature
159  Tref = 0. * Celsius # Kelvin - starting temp of iron bar  Tref = 0. * Celsius #Kelvin - starting temp of iron bar
160  rho = 7874. *kg/m**3 #kg/m^{3} density of iron  rho = 7874. *kg/m**3 #kg/m^{3} density of iron
161  cp = 449.*J/(kg*K) #jules/Kg.K thermal capacity  cp = 449.*J/(kg*K) #j/Kg.K thermal capacity
162  rhocp = rho*cp  rhocp = rho*cp
163  kappa = 80.*W/m/K #watts/m.K thermal conductivity  kappa = 80.*W/m/K #watts/m.Kthermal conductivity
164  \end{verbatim}  \end{verbatim}
165  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:  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:
166  \begin{verbatim}  \begin{verbatim}
#Script/Iteration Related
167  t=0 #our start time, usually zero  t=0 #our start time, usually zero
168  tend=5.*60. #seconds - time to end simulation  tend=5.*minute #seconds - time to end simulation
169  outputs = 200 # number of time steps required.  outputs = 200 # number of time steps required.
170  h=(tend-t)/outputs #size of time step  h=(tend-t)/outputs #size of time step
171  i=0 #loop counter  #user warning statement
172  #the folder to put our outputs in, leave blank "" for script path  print "Expected Number of time outputs is: ", (tend-t)/h
173  #note this folder path must exist to work  i=0 #loop counter
174  save_path = "data/onedheatdiff001"  #the folder to put our outputs in, leave blank "" for script path
175    save_path="data/onedheatdiff001"
176  \end{verbatim}  \end{verbatim}
177  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:  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:
178  \begin{verbatim}  \begin{verbatim}
179   rod = Rectangle(l0=mx,l1=my,n0=ndx,n1=ndy)  #generate domain using rectangle
180    rod = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)
181  \end{verbatim}  \end{verbatim}
182  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;  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, halfway along each side of the cell and so on. 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;
183  \begin{verbatim}  \begin{verbatim}
184   x = rod.getX()  #extract finite points - the solution points
185    x=rod.getX()
186  \end{verbatim}  \end{verbatim}
187  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.  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.
188  \begin{verbatim}  \begin{verbatim}
# Line 186  In a few special cases it may be possibl Line 194  In a few special cases it may be possibl
194  \label{eqn:symm}  \label{eqn:symm}
195  A\hackscore{jl}=A\hackscore{lj}  A\hackscore{jl}=A\hackscore{lj}
196
197  Note that from the general form $D$ and $d$ may have any value. The right hand sides $Y$, $y$ as well as the constraints  Symmetry is only dependent on the $A$ coefficient in the general form and the others $D$ and $d$ as well as the RHS coefficients $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;
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;
198  \begin{verbatim}  \begin{verbatim}
199   myPDE.setSymmetryOn()   myPDE.setSymmetryOn()
200  \end{verbatim}  \end{verbatim}
# Line 196  We now need to specify our boundary cond Line 203  We now need to specify our boundary cond
203  \begin{verbatim}  \begin{verbatim}
204   T = Tref   T = Tref
205  \end{verbatim}  \end{verbatim}
206  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 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$ . \ESCRIPT makes this easy by letting us define areas in our domain. The finite points in the domain 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 \ESCRIPT. 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.
207  \begin{verbatim}  \begin{verbatim}
# extract finite points
x=rod.getX()
208  # ... set heat source: ....  # ... set heat source: ....
209  qH=q*whereZero(x[0])  qH=q*whereZero(x[0])
210  \end{verbatim}  \end{verbatim}
211
212  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.  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 timestep is \verb T  the heat distribution and \verb totT  the total heat in the system.
213  \begin{verbatim}  \begin{verbatim}
# ... start iteration:
214  while t<=tend:  while t<=tend:
215        i+=1      i+=1 #increment the counter
216        t+=h      t+=h #increment the current time
217        mypde.setValue(Y=qH+rhocp/h*T)      mypde.setValue(Y=qH+rhocp/h*T) #set variable PDE coefficients
218        T=mypde.getSolution()      T=mypde.getSolution() #get the PDE solution
219        #some plotting stuff here - waiting on matplotlib binary distribution      totT = rhocp*T #get the total heat solution in the system
220    \end{verbatim}
221
222    \subsection{Plotting the heat solutions}
223    Visualisation of the solution can be achieved using \mpl a module contained with \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  .
224    \begin{verbatim}
225    #convert solution points for plotting
226    plx = x.toListOfTuples()
227    plx = np.array(plx) #convert to tuple to numpy array
228    plx = plx[:,0] #extract x locations
229    \end{verbatim}
230    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 labeling. 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. The figure is then saved to a *.png file and cleared for the following iteration.
231    \begin{verbatim}
232        #establish figure 1 for temperature vs x plots
233        tempT = T.toListOfTuples(scalarastuple=False)
234        pl.figure(1) #current figure
235        pl.plot(plx,tempT) #plot solution
236            #define axis extents and title
237        pl.axis([0,1.0,273.14990+0.00008,0.004+273.1499])
238        pl.title("Temperature accross Rod")
239            #save figure to file
240        pl.savefig(os.path.join(save_path+"/tempT","rodpyplot%03d.png") %i)
241        pl.clf() #clear figure
242
243        #establish figure 2 for total temperature vs x plots and repeat
244        tottempT = totT.toListOfTuples(scalarastuple=False)
245        pl.figure(2)
246        pl.plot(plx,tottempT)
247        pl.axis([0,1.0,9.657E08,12000+9.657E08])
248        pl.title("Total temperature accross Rod")
249        pl.savefig(os.path.join(save_path+"/totT","ttrodpyplot%03d.png")%i)
250        pl.clf()
251    \end{verbatim}
252    \subsection{Make a video}
253    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.
254    \begin{verbatim}
255    # compile the *.png files to create two *.avi videos that show T change
256    # with time. This opperation uses linux mencoder. For other operating
257    # systems it is possible to use your favourite video compiler to
258    # convert image files to videos.
259
260    os.system("mencoder mf://"+save_path+"/tempT"+"/*.png -mf type=png:\
261    w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
262    onedheatdiff001tempT.avi")
263
264    os.system("mencoder mf://"+save_path+"/totT"+"/*.png -mf type=png:\
265    w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
266    onedheatdiff001totT.avi")
267  \end{verbatim}  \end{verbatim}

\subsection{Plot total heat}
\TODO{show the script}

\TODO{explain how to use matlibplot to visualize the total heat integral(rho*c*T) over time}

\subsection{Plot Temperature Distribution}
\TODO{explain how to use matlibplot to visualize T}

268

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