/[escript]/trunk/doc/user/firststep.tex
ViewVC logotype

Diff of /trunk/doc/user/firststep.tex

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 993 by gross, Fri Feb 23 06:39:38 2007 UTC revision 2337 by gross, Thu Mar 26 07:07:42 2009 UTC
# Line 1  Line 1 
1  % $Id$  
2    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3    %
4    % Copyright (c) 2003-2008 by University of Queensland
5    % Earth Systems Science Computational Center (ESSCC)
6    % http://www.uq.edu.au/esscc
7  %  %
8  %           Copyright © 2006 by ACcESS MNRF  % Primary Business: Queensland, Australia
9  %               \url{http://www.access.edu.au  % Licensed under the Open Software License version 3.0
10  %         Primary Business: Queensland, Australia.  % http://www.opensource.org/licenses/osl-3.0.php
 %   Licensed under the Open Software License version 3.0  
 %      http://www.opensource.org/licenses/osl-3.0.php  
11  %  %
12    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13    
14    
15  \section{The First Steps}  \section{The First Steps}
16  \label{FirstSteps}  \label{FirstSteps}
17    
 \begin{figure}  
 \centerline{\includegraphics[width=\figwidth]{figures/FirstStepDomain}}  
 \caption{Domain $\Omega=[0,1]^2$ with outer normal field $n$.}  
 \label{fig:FirstSteps.1}  
 \end{figure}  
18    
 In this chapter we will give an introduction how to use \escript to solve  
 a partial differential equation \index{partial differential equation} (PDE \index{partial differential equation!PDE}). The reader should be familiar with Python. The knowledge presented at the Python tutorial at \url{http://docs.python.org/tut/tut.html}  
 is sufficient. It is helpful if the reader has some basic knowledge of PDEs \index{partial differential equation}.  
19    
20  The PDE \index{partial differential equation} we wish to solve is the Poisson equation \index{Poisson equation}  In this chapter we give an introduction how to use \escript to solve
21    a partial differential equation \index{partial differential equation} (PDE \index{partial differential equation!PDE}). We assume you are at least a little familiar with Python. The knowledge presented at the Python tutorial at \url{http://docs.python.org/tut/tut.html}
22    is more than sufficient.
23    
24    The PDE \index{partial differential equation} we wish to solve i  s the Poisson equation \index{Poisson equation}
25  \begin{equation}  \begin{equation}
26  -\Delta u =f  -\Delta u =f
27  \label{eq:FirstSteps.1}  \label{eq:FirstSteps.1}
28  \end{equation}  \end{equation}
29  for the solution $u$. The function $f$ is the given right hand side. The domain of interest, denoted by $\Omega$  for the solution $u$. The function $f$ is the given right hand side. The domain of interest, denoted by $\Omega$,
30  is the unit square  is the unit square
31  \begin{equation}  \begin{equation}
32  \Omega=[0,1]^2=\{ (x\hackscore 0;x\hackscore 1) | 0\le x\hackscore{0} \le 1 \mbox{ and } 0\le x\hackscore{1} \le 1 \}  \Omega=[0,1]^2=\{ (x\hackscore 0;x\hackscore 1) | 0\le x\hackscore{0} \le 1 \mbox{ and } 0\le x\hackscore{1} \le 1 \}
33  \label{eq:FirstSteps.1b}  \label{eq:FirstSteps.1b}
34  \end{equation}  \end{equation}
35  The domain is shown in \fig{fig:FirstSteps.1}.  The domain is shown in \fig{fig:FirstSteps.1}.
36    \begin{figure} [h!]
37    \centerline{\includegraphics[width=\figwidth]{figures/FirstStepDomain}}
38    \caption{Domain $\Omega=[0,1]^2$ with outer normal field $n$.}
39    \label{fig:FirstSteps.1}
40    \end{figure}
41    
42  $\Delta$ denotes the Laplace operator\index{Laplace operator} which is defined by  $\Delta$ denotes the Laplace operator\index{Laplace operator}, which is defined by
43  \begin{equation}  \begin{equation}
44  \Delta u = (u\hackscore {,0})\hackscore{,0}+(u\hackscore{,1})\hackscore{,1}  \Delta u = (u\hackscore {,0})\hackscore{,0}+(u\hackscore{,1})\hackscore{,1}
45  \label{eq:FirstSteps.1.1}  \label{eq:FirstSteps.1.1}
46  \end{equation}  \end{equation}
47  where, for any function $w$ and any direction $i$, $u\hackscore{,i}$  where, for any function $u$ and any direction $i$, $u\hackscore{,i}$
48  denotes the partial derivative \index{partial derivative} of $u$ with respect to $i$.    denotes the partial derivative \index{partial derivative} of $u$ with respect to $i$.  
49  \footnote{Some readers  \footnote{You
50  may be more familiar with the Laplace operator\index{Laplace operator} being written  may be more familiar with the Laplace operator\index{Laplace operator} being written
51  as $\nabla^2$, and written in the form  as $\nabla^2$, and written in the form
52  \begin{equation*}  \begin{equation*}
# Line 54  and \eqn{eq:FirstSteps.1} as Line 59  and \eqn{eq:FirstSteps.1} as
59  \end{equation*}  \end{equation*}
60  }  }
61  Basically, in the subindex of a function, any index to the left of the comma denotes a spatial derivative with respect  Basically, in the subindex of a function, any index to the left of the comma denotes a spatial derivative with respect
62  to the index. To get a more compact form we will write $w\hackscore{,ij}=(w\hackscore {,i})\hackscore{,j}$  to the index. To get a more compact form we will write $u\hackscore{,ij}=(u\hackscore {,i})\hackscore{,j}$
63  which leads to  which leads to
64  \begin{equation}  \begin{equation}
65  \Delta u = u\hackscore{,00}+u\hackscore{,11}=\sum\hackscore{i=0}^2 u\hackscore{,ii}  \Delta u = u\hackscore{,00}+u\hackscore{,11}=\sum\hackscore{i=0}^2 u\hackscore{,ii}
66  \label{eq:FirstSteps.1.1b}  \label{eq:FirstSteps.1.1b}
67  \end{equation}  \end{equation}
68  In some cases, and we will see examples for this in the next chapter,  We often find that use
69  the usage of the nested $\sum$ symbols blows up the formulas and therefore  of nested $\sum$ symbols makes formulas cumbersome, and we use the more
70  it is convenient to use the Einstein summation convention \index{summation convention}. This  convenient Einstein summation convention \index{summation convention}. This
71  drops the $\sum$ sign and assumes that a summation over a repeated index is performed  drops the $\sum$ sign and assumes that a summation is performed over any repeated index.
72  ("repeated index means summation"). For instance we write  For instance we write
73  \begin{eqnarray}  \begin{eqnarray}
74  x\hackscore{i}y\hackscore{i}=\sum\hackscore{i=0}^2 x\hackscore{i}y\hackscore{i}   \\  x\hackscore{i}y\hackscore{i}=\sum\hackscore{i=0}^2 x\hackscore{i}y\hackscore{i}   \\
75  x\hackscore{i}u\hackscore{,i}=\sum\hackscore{i=0}^2 x\hackscore{i}u\hackscore{,i}   \\  x\hackscore{i}u\hackscore{,i}=\sum\hackscore{i=0}^2 x\hackscore{i}u\hackscore{,i}   \\
# Line 93  are applying the Einstein summation conv Line 98  are applying the Einstein summation conv
98  $n\hackscore{i} u\hackscore{,i}= n\hackscore{0} u\hackscore{,0} +  $n\hackscore{i} u\hackscore{,i}= n\hackscore{0} u\hackscore{,0} +
99  n\hackscore{1} u\hackscore{,1}$.  n\hackscore{1} u\hackscore{,1}$.
100  \footnote{Some readers may familiar with the notation  \footnote{Some readers may familiar with the notation
101  \begin{equation*}  $
102  \frac{\partial u}{\partial n} = n\hackscore{i} u\hackscore{,i}  \frac{\partial u}{\partial n} = n\hackscore{i} u\hackscore{,i}
103  \end{equation*}  $
104  for the normal derivative.}  for the normal derivative.}
105  The Neumann boundary condition of \eqn{eq:FirstSteps.2} should be fulfilled on the  The Neumann boundary condition of \eqn{eq:FirstSteps.2} should be fulfilled on the
106  set $\Gamma^N$ which is the top and right edge of the domain:  set $\Gamma^N$ which is the top and right edge of the domain:
# Line 120  with the Neumann boundary condition \eqn Line 125  with the Neumann boundary condition \eqn
125  Dirichlet boundary condition in \eqn{eq:FirstSteps.2d} form a so  Dirichlet boundary condition in \eqn{eq:FirstSteps.2d} form a so
126  called boundary value  called boundary value
127  problem\index{boundary value problem} (BVP\index{boundary value problem!BVP}) for  problem\index{boundary value problem} (BVP\index{boundary value problem!BVP}) for
128  the unknown  the unknown function~$u$.
 function $u$.  
129    
130    
131  \begin{figure}  \begin{figure}[h]
132  \centerline{\includegraphics[width=\figwidth]{figures/FirstStepMesh.eps}}  \centerline{\includegraphics[width=\figwidth]{figures/FirstStepMesh}}
133  \caption{Mesh of $4 \time 4$ elements on a rectangular domain.  Here  \caption{Mesh of $4 \time 4$ elements on a rectangular domain.  Here
134  each element is a quadrilateral and described by four nodes, namely  each element is a quadrilateral and described by four nodes, namely
135  the corner points. The solution is interpolated by a bi-linear  the corner points. The solution is interpolated by a bi-linear
# Line 162  to solve the PDE. Here we are using the Line 166  to solve the PDE. Here we are using the
166  method}. The following statements create the \Domain object \var{mydomain} from the  method}. The following statements create the \Domain object \var{mydomain} from the
167  \finley method \method{Rectangle}  \finley method \method{Rectangle}
168  \begin{python}  \begin{python}
169  from esys.finley import Rectangle    from esys.finley import Rectangle
170  mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)    mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
171  \end{python}  \end{python}
172  In this case the domain is a rectangle with the lower, left corner at point $(0,0)$ and  In this case the domain is a rectangle with the lower, left corner at point $(0,0)$ and
173  the right, upper corner at $(\var{l0},\var{l1})=(1,1)$.  the right, upper corner at $(\var{l0},\var{l1})=(1,1)$.
# Line 175  see \Chap{CHAPTER ON FINLEY}. Line 179  see \Chap{CHAPTER ON FINLEY}.
179  The following statements define the \Poisson class object \var{mypde} with domain \var{mydomain} and  The following statements define the \Poisson class object \var{mypde} with domain \var{mydomain} and
180  the right hand side $f$ of the PDE to constant $1$:  the right hand side $f$ of the PDE to constant $1$:
181  \begin{python}  \begin{python}
182  from esys.escript.linearPDEs import Poisson    from esys.escript.linearPDEs import Poisson
183  mypde = Poisson(mydomain)    mypde = Poisson(mydomain)
184  mypde.setValue(f=1)    mypde.setValue(f=1)
185  \end{python}  \end{python}
186  We have not specified any boundary condition but the  We have not specified any boundary condition but the
187  \Poisson class implicitly assumes homogeneous Neuman boundary conditions \index{Neumann  \Poisson class implicitly assumes homogeneous Neuman boundary conditions \index{Neumann
# Line 189  by defining a characteristic function \i Line 193  by defining a characteristic function \i
193  which has positive values at locations $x=(x\hackscore{0},x\hackscore{1})$ where Dirichlet boundary condition is set  which has positive values at locations $x=(x\hackscore{0},x\hackscore{1})$ where Dirichlet boundary condition is set
194  and $0$ elsewhere. In our case of $\Gamma^D$ defined by \eqn{eq:FirstSteps.2c},  and $0$ elsewhere. In our case of $\Gamma^D$ defined by \eqn{eq:FirstSteps.2c},
195  we need to construct a function \var{gammaD} which is positive for the cases $x\hackscore{0}=0$ or $x\hackscore{1}=0$. To get  we need to construct a function \var{gammaD} which is positive for the cases $x\hackscore{0}=0$ or $x\hackscore{1}=0$. To get
196  an object \var{x} which represents locations in the domain one uses  an object \var{x} which contains the coordinates of the nodes in the domain use
197  \begin{python}  \begin{python}
198  x=mydomain.getX()    x=mydomain.getX()
199  \end{python}  \end{python}
200  The method \method{getX} of the \Domain \var{mydomain}  The method \method{getX} of the \Domain \var{mydomain}
201  gives access to locations  gives access to locations
202  in the domain defined by \var{mydomain}. The object \var{x} is actually a \Data object which is  in the domain defined by \var{mydomain}. The object \var{x} is actually a \Data object which will be
203  discussed in \Chap{ESCRIPT CHAP} in more details. What we need to know here is that  discussed in \Chap{ESCRIPT CHAP} in more detail. What we need to know here is that
204    
205  \var{x} has \Rank (=number of dimensions) and a \Shape (=tuple of dimensions) which can be checked by  \var{x} has \Rank (number of dimensions) and a \Shape (list of dimensions) which can be viewed by
206  calling the \method{getRank} and \method{getShape} methods:  calling the \method{getRank} and \method{getShape} methods:
207  \begin{python}  \begin{python}
208  print "rank ",x.getRank(),", shape ",x.getShape()    print "rank ",x.getRank(),", shape ",x.getShape()
209  \end{python}  \end{python}
210  will print something like  This will print something like
211  \begin{python}  \begin{python}
212  rank 1, shape (2,)    rank 1, shape (2,)
213  \end{python}  \end{python}
214  The \Data object also maintains type information which is represented by the  The \Data object also maintains type information which is represented by the
215  \FunctionSpace of the object. For instance  \FunctionSpace of the object. For instance
216  \begin{python}  \begin{python}
217  print x.getFunctionSpace()    print x.getFunctionSpace()
218  \end{python}  \end{python}
219  will print  will print
220  \begin{python}  \begin{python}
221  Function space type: Finley_Nodes on FinleyMesh    Function space type: Finley_Nodes on FinleyMesh
222  \end{python}  \end{python}
223  which tells us that the coordinates are stored on the nodes of a \finley mesh.  which tells us that the coordinates are stored on the nodes of (rather than on points in the interior of) a \finley mesh.
224  To get the  $x\hackscore{0}$ coordinates of the locations we use the  To get the  $x\hackscore{0}$ coordinates of the locations we use the
225  statement  statement
226  \begin{python}  \begin{python}
227  x0=x[0]    x0=x[0]
228  \end{python}  \end{python}
229  Object \var{x0}  Object \var{x0}
230  is again a \Data object now with \Rank $0$ and  is again a \Data object now with \Rank $0$ and
231  \Shape $()$. It inherits the \FunctionSpace from \var{x}:  \Shape $()$. It inherits the \FunctionSpace from \var{x}:
232  \begin{python}  \begin{python}
233  print x0.getRank(),x0.getShape(),x0.getFunctionSpace()    print x0.getRank(),x0.getShape(),x0.getFunctionSpace()
234  \end{python}  \end{python}
235  will print  will print
236  \begin{python}  \begin{python}
237  0 () Function space type: Finley_Nodes on FinleyMesh    0 () Function space type: Finley_Nodes on FinleyMesh
238  \end{python}  \end{python}
239  We can now construct the function \var{gammaD} by  We can now construct a function \var{gammaD} which is only non-zero on the bottom and left edges
240    of the domain with
241  \begin{python}  \begin{python}
242  from esys.escript import whereZero    from esys.escript import whereZero
243  gammaD=whereZero(x[0])+whereZero(x[1])    gammaD=whereZero(x[0])+whereZero(x[1])
244  \end{python}  \end{python}
245  where  
246  \code{whereZero(x[0])} creates function which equals $1$ where \code{x[0]} is (allmost) equal to zero  \code{whereZero(x[0])} creates function which equals $1$ where \code{x[0]} is (almost) equal to zero
247  and $0$ elsewhere.  and $0$ elsewhere.
248  Similarly, \code{whereZero(x[1])} creates function which equals $1$ where \code{x[1]} is  Similarly, \code{whereZero(x[1])} creates function which equals $1$ where \code{x[1]} is
249  equal to zero and $0$ elsewhere.  equal to zero and $0$ elsewhere.
250  The sum of the results of \code{whereZero(x[0])} and \code{whereZero(x[1])}  The sum of the results of \code{whereZero(x[0])} and \code{whereZero(x[1])}
251  gives a function on the domain \var{mydomain} which is exactly positive where $x\hackscore{0}$ or $x\hackscore{1}$ is equal to zero.  gives a function on the domain \var{mydomain} which is strictly positive where $x\hackscore{0}$ or $x\hackscore{1}$ is equal to zero.
252  Note that \var{gammaD} has the same \Rank, \Shape and \FunctionSpace like \var{x0} used to define it. So from  Note that \var{gammaD} has the same \Rank, \Shape and \FunctionSpace like \var{x0} used to define it. So from
253  \begin{python}  \begin{python}
254  print gammaD.getRank(),gammaD.getShape(),gammaD.getFunctionSpace()    print gammaD.getRank(),gammaD.getShape(),gammaD.getFunctionSpace()
255  \end{python}  \end{python}
256  one gets  one gets
257  \begin{python}  \begin{python}
258  0 () Function space type: Finley_Nodes on FinleyMesh    0 () Function space type: Finley_Nodes on FinleyMesh
259  \end{python}  \end{python}
260  The additional parameter \var{q} of the \code{setValue} method of the \Poisson class defines the  An additional parameter \var{q} of the \code{setValue} method of the \Poisson class defines the
261  characteristic function \index{characteristic function} of the locations  characteristic function \index{characteristic function} of the locations
262  of the domain where homogeneous Dirichlet boundary condition \index{Dirichlet boundary condition!homogeneous}  of the domain where homogeneous Dirichlet boundary condition \index{Dirichlet boundary condition!homogeneous}
263  are set. The complete definition of our example is now:  are set. The complete definition of our example is now:
264  \begin{python}  \begin{python}
265  from esys.linearPDEs import Poisson    from esys.linearPDEs import Poisson
266  x = mydomain.getX()    x = mydomain.getX()
267  gammaD = whereZero(x[0])+whereZero(x[1])    gammaD = whereZero(x[0])+whereZero(x[1])
268  mypde = Poisson(domain=mydomain)    mypde = Poisson(domain=mydomain)
269  mypde.setValue(f=1,q=gammaD)    mypde.setValue(f=1,q=gammaD)
270  \end{python}  \end{python}
271  The first statement imports the \Poisson class definition from the \linearPDEs module \escript package.  The first statement imports the \Poisson class definition from the \linearPDEs module \escript package.
272  To get the solution of the Poisson equation defined by \var{mypde} we just have to call its  To get the solution of the Poisson equation defined by \var{mypde} we just have to call its
273  \method{getSolution}.  \method{getSolution}.
274    
275  Now we can write the script to solve our test problem (Remember that  Now we can write the script to solve our Poisson problem
276  lines starting with '\#' are comment lines in Python) (available as \file{poisson.py}  \begin{python}
277  in the \ExampleDirectory):    from esys.escript import *
278  \begin{python}    from esys.escript.linearPDEs import Poisson
279  from esys.escript import *    from esys.finley import Rectangle
280  from esys.escript.linearPDEs import Poisson    # generate domain:
281  from esys.finley import Rectangle    mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
282  # generate domain:    # define characteristic function of Gamma^D
283  mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)    x = mydomain.getX()
284  # define characteristic function of Gamma^D    gammaD = whereZero(x[0])+whereZero(x[1])
285  x = mydomain.getX()    # define PDE and get its solution u
286  gammaD = whereZero(x[0])+whereZero(x[1])    mypde = Poisson(domain=mydomain)
287  # define PDE and get its solution u    mypde.setValue(f=1,q=gammaD)
288  mypde = Poisson(domain=mydomain)    u = mypde.getSolution()
289  mypde.setValue(f=1,q=gammaD)    # write u to an external file
290  u = mypde.getSolution()    saveVTK("u.xml",sol=u)
291  # write u to an external file  \end{python}
292  saveVTK("u.xml",sol=u)  The entire code is available as \file{poisson.py} in the \ExampleDirectory
293  \end{python}  
294  The last statement writes the solution tagged with the name "sol" to the external file \file{u.xml} in  The last statement writes the solution (tagged with the name "sol") to a file named \file{u.xml} in
295  \VTK file format. \VTK is a software library  \VTK file format.
296  for the visualization of scientific, engineering and analytical data and is freely available  Now you may run the script and visualize the solution using \mayavi:
297  from \url{http://www.vtk.org}. There are a variety of graphical user interfaces  \begin{verbatim}
298  for \VTK available, for instance \mayavi which can be downloaded from \url{http://mayavi.sourceforge.net/} but is also available on most    python poisson.py
299  \LINUX distributions.    mayavi -d u.xml -m SurfaceMap
300    \end{verbatim}
301    See \fig{fig:FirstSteps.3}.
302    
303  \begin{figure}  \begin{figure}
304  \centerline{\includegraphics[width=\figwidth]{figures/FirstStepResult.eps}}  \centerline{\includegraphics[width=\figwidth]{figures/FirstStepResult}}
305  \caption{Visualization of the Poisson Equation Solution for $f=1$}  \caption{Visualization of the Poisson Equation Solution for $f=1$}
306  \label{fig:FirstSteps.3}  \label{fig:FirstSteps.3}
307  \end{figure}  \end{figure}
308    
 You can edit the script file using your favourite text editor (or the Integrated DeveLopment Environment IDLE  
 for Python, see \url{http://idlefork.sourceforge.net}). If the script file has the name \file{poisson.py} \index{scripts!\file{poisson.py}} you can run the  
 script from any shell using the command:  
 \begin{python}  
 python poisson.py  
 \end{python}  
 After the script has (hopefully successfully) been completed you will find the file \file{u.xml} in the current  
 directory. An easy way to visualize the results is the command  
 \begin{python}  
 mayavi -d u.xml -m SurfaceMap &  
 \end{python}  
 to show the results, see \fig{fig:FirstSteps.3}.  
   
 \subsection{Visualization with \pyvisi}  
   
 \subsection{Domain description using with \pycad}  
   
   
   

Legend:
Removed from v.993  
changed lines
  Added in v.2337

  ViewVC Help
Powered by ViewVC 1.1.26