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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 599 - (hide annotations)
Fri Mar 17 05:48:59 2006 UTC (13 years, 8 months ago) by gross
File MIME type: application/x-tex
File size: 14335 byte(s)
update on escript function
1 jgs 102 % $Id$
2    
3 jgs 121 \section{The First Steps}
4 jgs 102 \label{FirstSteps}
5    
6     \begin{figure}
7 gross 599 \centerline{\includegraphics[width=\figwidth]{figures/FirstStepDomain}}
8 jgs 102 \caption{Domain $\Omega=[0,1]^2$ with outer normal field $n$.}
9     \label{fig:FirstSteps.1}
10     \end{figure}
11    
12 jgs 107 In this chapter we will give an introduction how to use \escript to solve
13     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}
14     is sufficient. It is helpful if the reader has some basic knowledge of PDEs \index{partial differential equation}.
15 jgs 102
16 jgs 107 The PDE \index{partial differential equation} we wish to solve is the Poisson equation \index{Poisson equation}
17 jgs 102 \begin{equation}
18     -\Delta u =f
19     \label{eq:FirstSteps.1}
20     \end{equation}
21 jgs 107 for the solution $u$. The function $f$ is the given right hand side. The domain of interest, denoted by $\Omega$
22 jgs 102 is the unit square
23     \begin{equation}
24     \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 \}
25     \label{eq:FirstSteps.1b}
26     \end{equation}
27 jgs 107 The domain is shown in \fig{fig:FirstSteps.1}.
28 jgs 102
29     $\Delta$ denotes the Laplace operator\index{Laplace operator} which is defined by
30     \begin{equation}
31     \Delta u = (u\hackscore {,0})\hackscore{,0}+(u\hackscore{,1})\hackscore{,1}
32     \label{eq:FirstSteps.1.1}
33     \end{equation}
34 jgs 107 where, for any function $w$ and any direction $i$, $u\hackscore{,i}$
35     denotes the partial derivative \index{partial derivative} of $u$ with respect to $i$.
36 jgs 102 \footnote{Some readers
37     may be more familiar with the Laplace operator\index{Laplace operator} being written
38     as $\nabla^2$, and written in the form
39     \begin{equation*}
40 jgs 110 \nabla^2 u = \nabla^t \cdot \nabla u = \frac{\partial^2 u}{\partial x\hackscore 0^2}
41 jgs 102 + \frac{\partial^2 u}{\partial x\hackscore 1^2}
42     \end{equation*}
43     and \eqn{eq:FirstSteps.1} as
44     \begin{equation*}
45     -\nabla^2 u = f
46     \end{equation*}
47     }
48 jgs 107 Basically, in the subindex of a function, any index to the left of the comma denotes a spatial derivative with respect
49 jgs 102 to the index. To get a more compact form we will write $w\hackscore{,ij}=(w\hackscore {,i})\hackscore{,j}$
50     which leads to
51     \begin{equation}
52     \Delta u = u\hackscore{,00}+u\hackscore{,11}=\sum\hackscore{i=0}^2 u\hackscore{,ii}
53     \label{eq:FirstSteps.1.1b}
54     \end{equation}
55     In some cases, and we will see examples for this in the next chapter,
56     the usage of the nested $\sum$ symbols blows up the formulas and therefore
57 jgs 107 it is convenient to use the Einstein summation convention \index{summation convention}. This
58     drops the $\sum$ sign and assumes that a summation over a repeated index is performed
59 jgs 102 ("repeated index means summation"). For instance we write
60     \begin{eqnarray}
61     x\hackscore{i}y\hackscore{i}=\sum\hackscore{i=0}^2 x\hackscore{i}y\hackscore{i} \\
62     x\hackscore{i}u\hackscore{,i}=\sum\hackscore{i=0}^2 x\hackscore{i}u\hackscore{,i} \\
63     u\hackscore{,ii}=\sum\hackscore{i=0}^2 u\hackscore{,ii} \\
64 jgs 107 x\hackscore{ij}u\hackscore{i,j}=\sum\hackscore{j=0}^2\sum\hackscore{i=0}^2 x\hackscore{ij}u\hackscore{i,j} \\
65 jgs 102 \label{eq:FirstSteps.1.1c}
66     \end{eqnarray}
67     With the summation convention we can write the Poisson equation \index{Poisson equation} as
68     \begin{equation}
69     - u\hackscore{,ii} =1
70     \label{eq:FirstSteps.1.sum}
71     \end{equation}
72 lkettle 575 where $f=1$ in this example.
73    
74 jgs 102 On the boundary of the domain $\Omega$ the normal derivative $n\hackscore{i} u\hackscore{,i}$
75     of the solution $u$ shall be zero, ie. $u$ shall fulfill
76     the homogeneous Neumann boundary condition\index{Neumann
77     boundary condition!homogeneous}
78     \begin{equation}
79     n\hackscore{i} u\hackscore{,i}= 0 \;.
80     \label{eq:FirstSteps.2}
81     \end{equation}
82     $n=(n\hackscore{i})$ denotes the outer normal field
83     of the domain, see \fig{fig:FirstSteps.1}. Remember that we
84     are applying the Einstein summation convention \index{summation convention}, i.e
85 jgs 107 $n\hackscore{i} u\hackscore{,i}= n\hackscore{0} u\hackscore{,0} +
86     n\hackscore{1} u\hackscore{,1}$.
87 jgs 102 \footnote{Some readers may familiar with the notation
88     \begin{equation*}
89     \frac{\partial u}{\partial n} = n\hackscore{i} u\hackscore{,i}
90     \end{equation*}
91     for the normal derivative.}
92     The Neumann boundary condition of \eqn{eq:FirstSteps.2} should be fulfilled on the
93     set $\Gamma^N$ which is the top and right edge of the domain:
94     \begin{equation}
95     \Gamma^N=\{(x\hackscore 0;x\hackscore 1) \in \Omega | x\hackscore{0}=1 \mbox{ or } x\hackscore{1}=1 \}
96     \label{eq:FirstSteps.2b}
97     \end{equation}
98 jgs 107 On the bottom and the left edge of the domain which is defined
99 jgs 102 as
100     \begin{equation}
101     \Gamma^D=\{(x\hackscore 0;x\hackscore 1) \in \Omega | x\hackscore{0}=0 \mbox{ or } x\hackscore{1}=0 \}
102     \label{eq:FirstSteps.2c}
103     \end{equation}
104     the solution shall be identically zero:
105     \begin{equation}
106     u=0 \; .
107     \label{eq:FirstSteps.2d}
108     \end{equation}
109 jgs 107 This kind of boundary condition is called a homogeneous Dirichlet boundary condition
110 jgs 102 \index{Dirichlet boundary condition!homogeneous}. The partial differential equation in \eqn{eq:FirstSteps.1.sum} together
111     with the Neumann boundary condition \eqn{eq:FirstSteps.2} and
112     Dirichlet boundary condition in \eqn{eq:FirstSteps.2d} form a so
113     called boundary value
114 jgs 107 problem\index{boundary value problem} (BVP\index{boundary value problem!BVP}) for
115     the unknown
116 jgs 102 function $u$.
117    
118    
119     \begin{figure}
120 gross 599 \centerline{\includegraphics[width=\figwidth]{figures/FirstStepMesh.eps}}
121 jgs 102 \caption{Mesh of $4 \time 4$ elements on a rectangular domain. Here
122     each element is a quadrilateral and described by four nodes, namely
123     the corner points. The solution is interpolated by a bi-linear
124     polynomial.}
125     \label{fig:FirstSteps.2}
126     \end{figure}
127    
128     In general the BVP\index{boundary value problem!BVP} cannot be solved analytically and numerical
129     methods have to be used construct an approximation of the solution
130     $u$. Here we will use the finite element method\index{finite element
131     method} (FEM\index{finite element
132     method!FEM}). The basic idea is to fill the domain with a
133 jgs 107 set of points called nodes. The solution is approximated by its
134 jgs 102 values on the nodes\index{finite element
135 lkettle 573 method!nodes}. Moreover, the domain is subdivided into smaller
136     sub-domains called elements \index{finite element
137 jgs 102 method!element}. On each element the solution is
138     represented by a polynomial of a certain degree through its values at
139     the nodes located in the element. The nodes and its connection through
140     elements is called a mesh\index{finite element
141 jgs 107 method!mesh}. \fig{fig:FirstSteps.2} shows an
142 jgs 102 example of a FEM mesh with four elements in the $x_0$ and four elements
143     in the $x_1$ direction over the unit square.
144     For more details we refer the reader to the literature, for instance
145     \Ref{Zienc,NumHand}.
146    
147     \escript provides the class \Poisson to define a Poisson equation \index{Poisson equation}.
148     (We will discuss a more general form of a PDE \index{partial differential equation!PDE}
149     that can be defined through the \LinearPDE class later). The instantiation of
150     a \Poisson class object requires the specification of the domain $\Omega$. In \escript
151     the \Domain class objects are used to describe the geometry of a domain but it also
152     contains information about the discretization methods and the actual solver which is used
153     to solve the PDE. Here we are using the FEM library \finley \index{finite element
154     method}. The following statements create the \Domain object \var{mydomain} from the
155     \finley method \method{Rectangle}
156     \begin{python}
157 jgs 107 from esys.finley import Rectangle
158     mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
159 jgs 102 \end{python}
160     In this case the domain is a rectangle with the lower, left corner at point $(0,0)$ and
161     the right, upper corner at $(\var{l0},\var{l1})=(1,1)$.
162 jgs 107 The arguments \var{n0} and \var{n1} define the number of elements in $x\hackscore{0}$ and
163 jgs 102 $x\hackscore{1}$-direction respectively. For more details on \method{Rectangle} and
164     other \Domain generators within the \finley module,
165     see \Chap{CHAPTER ON FINLEY}.
166    
167 jgs 107 The following statements define the \Poisson class object \var{mypde} with domain \var{mydomain} and
168 jgs 102 the right hand side $f$ of the PDE to constant $1$:
169     \begin{python}
170 gross 565 from esys.escript.linearPDEs import Poisson
171 jgs 107 mypde = Poisson(mydomain)
172     mypde.setValue(f=1)
173 jgs 102 \end{python}
174     We have not specified any boundary condition but the
175     \Poisson class implicitly assumes homogeneous Neuman boundary conditions \index{Neumann
176     boundary condition!homogeneous} defined by \eqn{eq:FirstSteps.2}. With this boundary
177     condition the BVP\index{boundary value problem!BVP} we have defined has no unique solution. In fact, with any solution $u$
178     and any constant $C$ the function $u+C$ becomes a solution as well. We have to add
179     a Dirichlet boundary condition \index{Dirichlet boundary condition}. This is done
180 jgs 107 by defining a characteristic function \index{characteristic function}
181     which has positive values at locations $x=(x\hackscore{0},x\hackscore{1})$ where Dirichlet boundary condition is set
182 jgs 102 and $0$ elsewhere. In our case of $\Gamma^D$ defined by \eqn{eq:FirstSteps.2c},
183 gross 565 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
184     an object \var{x} which represents locations in the domain one uses
185 jgs 102 \begin{python}
186 lkettle 573 x=mydomain.getX()
187 jgs 102 \end{python}
188 gross 565 In fact \var{x} is a \Data object which we will learn more about in Chapter~\ref{X}. At this stage we only have to know
189     that \var{x} has a
190    
191 jgs 107 In the first statement, the method \method{getX} of the \Domain \var{mydomain}
192     gives access to locations
193 gross 565 in the domain defined by \var{mydomain}. The object \var{x} is actually a \Data object which is
194     discussed in Chpater\ref{X} in more details. What we need to know here is that
195     \var{x} has \Rank (=number of dimensions) and a \Shape (=tuple of dimensions) which can be checked by
196     calling the \method{getRank} and \method{getShape} methods:
197     \begin{python}
198     print "rank ",x.getRank(),", shape ",x.getShape()
199     \end{python}
200     will print something like
201     \begin{python}
202     rank 1, shape (2,)
203     \end{python}
204     The \Data object also maintains type information which is represented by the
205     \FunctionSpace of the object. For instance
206     \begin{python}
207     print x.getFunctionSpace()
208     \end{python}
209     will print
210     \begin{python}
211     Function space type: Finley_Nodes on FinleyMesh
212     \end{python}
213     which tells us that the coordinates are stored on the nodes of a \finley mesh.
214     To get the $x\hackscore{0}$ coordinates of the locations we use the
215     statement
216     \begin{python}
217     x0=x[0]
218     \end{python}
219     Object \var{x0}
220     is again a \Data object now with \Rank $0$ and
221     \Shape $()$. It inherits the \FunctionSpace from \var{x}:
222     \begin{python}
223     print x0.getRank(),x0.getShape(),x0.getFunctionSpace()
224     \end{python}
225     will print
226     \begin{python}
227     0 () Function space type: Finley_Nodes on FinleyMesh
228     \end{python}
229     We can now construct the function \var{gammaD} by
230     \begin{python}
231 lkettle 573 from esys.escript import whereZero
232 gross 565 gammaD=whereZero(x[0])+whereZero(x[1])
233     \end{python}
234     where
235     \code{whereZero(x[0])} creates function which equals $1$ where \code{x[0]} is (allmost) equal to zero
236 jgs 107 and $0$ elsewhere.
237 gross 565 Similarly, \code{whereZero(x[1])} creates function which equals $1$ where \code{x[1]} is
238 jgs 107 equal to zero and $0$ elsewhere.
239 gross 565 The sum of the results of \code{whereZero(x[0])} and \code{whereZero(x[1])}
240     gives a function on the domain \var{mydomain} which is exactly positive where $x\hackscore{0}$ or $x\hackscore{1}$ is equal to zero.
241     Note that \var{gammaD} has the same \Rank, \Shape and \FunctionSpace like \var{x0} used to define it. So from
242     \begin{python}
243     print gammaD.getRank(),gammaD.getShape(),gammaD.getFunctionSpace()
244     \end{python}
245     one gets
246     \begin{python}
247     0 () Function space type: Finley_Nodes on FinleyMesh
248     \end{python}
249 jgs 107 The additional parameter \var{q} of the \code{setValue} method of the \Poisson class defines the
250 jgs 102 characteristic function \index{characteristic function} of the locations
251     of the domain where homogeneous Dirichlet boundary condition \index{Dirichlet boundary condition!homogeneous}
252     are set. The complete definition of our example is now:
253     \begin{python}
254 jgs 107 from esys.linearPDEs import Poisson
255 jgs 102 x = mydomain.getX()
256 gross 565 gammaD = whereZero(x[0])+whereZero(x[1])
257 jgs 107 mypde = Poisson(domain=mydomain)
258 lkettle 573 mypde.setValue(f=1,q=gammaD)
259 jgs 102 \end{python}
260 lkettle 573 The first statement imports the \Poisson class definition from the \linearPDEs module \escript package.
261 jgs 107 To get the solution of the Poisson equation defined by \var{mypde} we just have to call its
262 jgs 102 \method{getSolution}.
263    
264     Now we can write the script to solve our test problem (Remember that
265 gross 569 lines starting with '\#' are comment lines in Python) (available as \file{poisson.py}
266 jgs 102 in the \ExampleDirectory):
267     \begin{python}
268 gross 565 from esys.escript import *
269 gross 569 from esys.escript.linearPDEs import Poisson
270 jgs 107 from esys.finley import Rectangle
271 jgs 102 # generate domain:
272 jgs 107 mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
273 jgs 102 # define characteristic function of Gamma^D
274     x = mydomain.getX()
275 gross 565 gammaD = whereZero(x[0])+whereZero(x[1])
276 jgs 102 # define PDE and get its solution u
277 gross 565 mypde = Poisson(domain=mydomain)
278     mypde.setValue(f=1,q=gammaD)
279 jgs 102 u = mypde.getSolution()
280     # write u to an external file
281 gross 565 saveVTK("u.xml",sol=u)
282 jgs 102 \end{python}
283 gross 565 The last statement writes the solution tagged with the name "sol" to the external file \file{u.xml} in
284     \VTK file format. \VTK is a software library
285 jgs 102 for the visualization of scientific, engineering and analytical data and is freely available
286 lkettle 573 from \url{http://www.vtk.org}. There are a variety of graphical user interfaces
287 gross 565 for \VTK available, for instance \mayavi which can be downloaded from \url{http://mayavi.sourceforge.net/} but is also available on most
288     \LINUX distributions.
289 jgs 102
290     \begin{figure}
291 gross 599 \centerline{\includegraphics[width=\figwidth]{figures/FirstStepResult.eps}}
292 lkettle 573 \caption{Visualization of the Poisson Equation Solution for $f=1$}
293 jgs 102 \label{fig:FirstSteps.3}
294     \end{figure}
295    
296 jgs 107 You can edit the script file using your favourite text editor (or the Integrated DeveLopment Environment IDLE
297 gross 569 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
298 jgs 102 script from any shell using the command:
299 gross 565 \begin{python}
300 gross 569 python poisson.py
301 gross 565 \end{python}
302 gross 569 After the script has (hopefully successfully) been completed you will find the file \file{u.xml} in the current
303 jgs 102 directory. An easy way to visualize the results is the command
304 gross 565 \begin{python}
305     mayavi -d u.xml -m SurfaceMap &
306     \end{python}
307     to show the results, see \fig{fig:FirstSteps.3}.

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26