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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 102 - (show annotations)
Wed Dec 15 07:08:39 2004 UTC (14 years, 10 months ago) by jgs
Original Path: trunk/esys2/doc/user/firststep.tex
File MIME type: application/x-tex
File size: 11799 byte(s)
*** empty log message ***

1 % $Id$
2
3 \chapter{The First Steps}
4 \label{FirstSteps}
5
6 \begin{figure}
7 \centerline{\includegraphics[width=\figwidth]{FirstStepDomain}}
8 \caption{Domain $\Omega=[0,1]^2$ with outer normal field $n$.}
9 \label{fig:FirstSteps.1}
10 \end{figure}
11
12 In this chapter we will show the basics of 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
14 the basics of Python. The knowledge presented the Python tutorial at \url{http://docs.python.org/tut/tut.html}
15 is sufficient. It is helpful if the reader has some basic knowledge on PDEs \index{PDE}.
16
17 The \index{PDE} we want to solve is the Poisson equation \index{Poisson equation}
18 \begin{equation}
19 -\Delta u =f
20 \label{eq:FirstSteps.1}
21 \end{equation}
22 for the solution $u$. The domain of interest which we denote by $\Omega$
23 is the unit square
24 \begin{equation}
25 \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 \}
26 \label{eq:FirstSteps.1b}
27 \end{equation}
28 The domain is shown in Figure~\fig{fig:FirstSteps.1}.
29
30 $\Delta$ denotes the Laplace operator\index{Laplace operator} which is defined by
31 \begin{equation}
32 \Delta u = (u\hackscore {,0})\hackscore{,0}+(u\hackscore{,1})\hackscore{,1}
33 \label{eq:FirstSteps.1.1}
34 \end{equation}
35 where for any function $w$ and any direction $i$ $u\hackscore{,i}$
36 denotes the partial derivative \index{partial derivative} of $w$ with respect to $i$.
37 \footnote{Some readers
38 may be more familiar with the Laplace operator\index{Laplace operator} being written
39 as $\nabla^2$, and written in the form
40 \begin{equation*}
41 \nabla^2 u = \frac{\partial^2 u}{\partial x\hackscore 0^2}
42 + \frac{\partial^2 u}{\partial x\hackscore 1^2}
43 \end{equation*}
44 and \eqn{eq:FirstSteps.1} as
45 \begin{equation*}
46 -\nabla^2 u = f
47 \end{equation*}
48 }
49 Basically in the subindex of a function any index left to the comma denotes a spatial derivative with respect
50 to the index. To get a more compact form we will write $w\hackscore{,ij}=(w\hackscore {,i})\hackscore{,j}$
51 which leads to
52 \begin{equation}
53 \Delta u = u\hackscore{,00}+u\hackscore{,11}=\sum\hackscore{i=0}^2 u\hackscore{,ii}
54 \label{eq:FirstSteps.1.1b}
55 \end{equation}
56 In some cases, and we will see examples for this in the next chapter,
57 the usage of the nested $\sum$ symbols blows up the formulas and therefore
58 it is convenient to use Einstein summation convention \index{summation convention} which
59 says that $\sum$ sign is dropped and a summation over a repeated index is performed
60 ("repeated index means summation"). For instance we write
61 \begin{eqnarray}
62 x\hackscore{i}y\hackscore{i}=\sum\hackscore{i=0}^2 x\hackscore{i}y\hackscore{i} \\
63 x\hackscore{i}u\hackscore{,i}=\sum\hackscore{i=0}^2 x\hackscore{i}u\hackscore{,i} \\
64 u\hackscore{,ii}=\sum\hackscore{i=0}^2 u\hackscore{,ii} \\
65 \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 On the boundary of the domain $\Omega$ the normal derivative $n\hackscore{i} u\hackscore{,i}$
73 of the solution $u$ shall be zero, ie. $u$ shall fulfill
74 the homogeneous Neumann boundary condition\index{Neumann
75 boundary condition!homogeneous}
76 \begin{equation}
77 n\hackscore{i} u\hackscore{,i}= 0 \;.
78 \label{eq:FirstSteps.2}
79 \end{equation}
80 $n=(n\hackscore{i})$ denotes the outer normal field
81 of the domain, see \fig{fig:FirstSteps.1}. Remember that we
82 are applying the Einstein summation convention \index{summation convention}, i.e
83 $n\hackscore{i} u\hackscore{,i}= n\hackscore1 u\hackscore{,1} +
84 n\hackscore2 u\hackscore{,2}$.
85 \footnote{Some readers may familiar with the notation
86 \begin{equation*}
87 \frac{\partial u}{\partial n} = n\hackscore{i} u\hackscore{,i}
88 \end{equation*}
89 for the normal derivative.}
90 The Neumann boundary condition of \eqn{eq:FirstSteps.2} should be fulfilled on the
91 set $\Gamma^N$ which is the top and right edge of the domain:
92 \begin{equation}
93 \Gamma^N=\{(x\hackscore 0;x\hackscore 1) \in \Omega | x\hackscore{0}=1 \mbox{ or } x\hackscore{1}=1 \}
94 \label{eq:FirstSteps.2b}
95 \end{equation}
96 On the bottom and the left edge of the domain which defined
97 as
98 \begin{equation}
99 \Gamma^D=\{(x\hackscore 0;x\hackscore 1) \in \Omega | x\hackscore{0}=0 \mbox{ or } x\hackscore{1}=0 \}
100 \label{eq:FirstSteps.2c}
101 \end{equation}
102 the solution shall be identically zero:
103 \begin{equation}
104 u=0 \; .
105 \label{eq:FirstSteps.2d}
106 \end{equation}
107 The kind of boundary condition is called a homogeneous Dirichlet boundary condition
108 \index{Dirichlet boundary condition!homogeneous}. The partial differential equation in \eqn{eq:FirstSteps.1.sum} together
109 with the Neumann boundary condition \eqn{eq:FirstSteps.2} and
110 Dirichlet boundary condition in \eqn{eq:FirstSteps.2d} form a so
111 called boundary value
112 problem\index{boundary value problem} (BVP\index{boundary value problem!BVP}) for unknown
113 function $u$.
114
115
116 \begin{figure}
117 \centerline{\includegraphics[width=\figwidth]{FirstStepMesh}}
118 \caption{Mesh of $4 \time 4$ elements on a rectangular domain. Here
119 each element is a quadrilateral and described by four nodes, namely
120 the corner points. The solution is interpolated by a bi-linear
121 polynomial.}
122 \label{fig:FirstSteps.2}
123 \end{figure}
124
125 In general the BVP\index{boundary value problem!BVP} cannot be solved analytically and numerical
126 methods have to be used construct an approximation of the solution
127 $u$. Here we will use the finite element method\index{finite element
128 method} (FEM\index{finite element
129 method!FEM}). The basic idea is to fill the domain with a
130 set of points, so called nodes. The solution is approximated by its
131 values on the nodes\index{finite element
132 method!nodes}. Moreover, the domain is subdivide into small
133 sub-domain, so-called elements \index{finite element
134 method!element}. On each element the solution is
135 represented by a polynomial of a certain degree through its values at
136 the nodes located in the element. The nodes and its connection through
137 elements is called a mesh\index{finite element
138 method!mesh}. Figure~\fig{fig:FirstSteps.2} shows an
139 example of a FEM mesh with four elements in the $x_0$ and four elements
140 in the $x_1$ direction over the unit square.
141 For more details we refer the reader to the literature, for instance
142 \Ref{Zienc,NumHand}.
143
144 \escript provides the class \Poisson to define a Poisson equation \index{Poisson equation}.
145 (We will discuss a more general form of a PDE \index{partial differential equation!PDE}
146 that can be defined through the \LinearPDE class later). The instantiation of
147 a \Poisson class object requires the specification of the domain $\Omega$. In \escript
148 the \Domain class objects are used to describe the geometry of a domain but it also
149 contains information about the discretization methods and the actual solver which is used
150 to solve the PDE. Here we are using the FEM library \finley \index{finite element
151 method}. The following statements create the \Domain object \var{mydomain} from the
152 \finley method \method{Rectangle}
153 \begin{python}
154 import finley
155 mydomain = finley.Rectangle(l0=1.,l1=1.,n0=40, n1=20)
156 \end{python}
157 In this case the domain is a rectangle with the lower, left corner at point $(0,0)$ and
158 the right, upper corner at $(\var{l0},\var{l1})=(1,1)$.
159 The arguments \var{l0} and \var{l1} define the number of elements in $x\hackscore{0}$ and
160 $x\hackscore{1}$-direction respectively. For more details on \method{Rectangle} and
161 other \Domain generators within the \finley module,
162 see \Chap{CHAPTER ON FINLEY}.
163
164 The following statements define the \Poisson object \var{mypde} with domain var{mydomain} and
165 the right hand side $f$ of the PDE to constant $1$:
166 \begin{python}
167 import escript
168 mypde = escript.Poisson(domain=mydomain,f=1)
169 \end{python}
170 We have not specified any boundary condition but the
171 \Poisson class implicitly assumes homogeneous Neuman boundary conditions \index{Neumann
172 boundary condition!homogeneous} defined by \eqn{eq:FirstSteps.2}. With this boundary
173 condition the BVP\index{boundary value problem!BVP} we have defined has no unique solution. In fact, with any solution $u$
174 and any constant $C$ the function $u+C$ becomes a solution as well. We have to add
175 a Dirichlet boundary condition \index{Dirichlet boundary condition}. This is done
176 by defines a characteristic function \index{characteristic function}
177 which has a positive values at locations $(x_0,x_1)$ where Dirichlet boundary condition is set
178 and $0$ elsewhere. In our case of $\Gamma^D$ defined by \eqn{eq:FirstSteps.2c},
179 we need a function which is positive for the cases $x_0=0$ or $x_1=0$:
180 \begin{python}
181 x=mydomain.getX()
182 gammaD=x[0].whereZero()+x[1].whereZero()
183 \end{python}
184 In first statement returns, the method \method{getX} of the \Domain \var{mydomain} access to the locations
185 in the domain defined by \var{mydomain}. The object \var{x} is actually an \Data object
186 which we will learn more about later. \code{x[0]} returns the $x_0$ coordinates of the locations and
187 \code{x[0].whereZero()} creates function which equals $1$ where \code{x[0]} is (nearly) equal to zero
188 and $0$ elsewhere. The sum of the results of \code{x[0].whereZero()} and \code{x[1].whereZero()} gives a function on the domain \var{mydomain} which is exactly positive where $x_0$ or $x_1$ is equal to zero.
189
190 The additional parameter \var{q} of the \Poisson object creater defines the
191 characteristic function \index{characteristic function} of the locations
192 of the domain where homogeneous Dirichlet boundary condition \index{Dirichlet boundary condition!homogeneous}
193 are set. The complete definition of our example is now:
194 \begin{python}
195 from linearPDEs import Poisson
196 x = mydomain.getX()
197 gammaD = x[0].whereZero()+x[1].whereZero()
198 mypde = Poisson(domain=mydomain,f=1,q=gammaD)
199 \end{python}
200 The first statement imports the \Poisson class definition form the \linearPDEsPack module which is part of the \escript module.
201 To get the solution of the Poisson equation defines by \var{mypde} we just have to call its
202 \method{getSolution}.
203
204 Now we can write the script to solve our test problem (Remember that
205 lines starting with '\#' are commend lines in Python) (available as \file{mypoisson.py}
206 in the \ExampleDirectory):
207 \begin{python}
208 import esys.finley
209 from esys.linearPDEs import Poisson
210 # generate domain:
211 mydomain = esys.finley.Rectangle(l0=1.,l1=1.,n0=40, n1=20)
212 # define characteristic function of Gamma^D
213 x = mydomain.getX()
214 gammaD = x[0].whereZero()+x[1].whereZero()
215 # define PDE and get its solution u
216 mypde = Poisson(domain=mydomain,f=1,q=gammaD)
217 u = mypde.getSolution()
218 # write u to an external file
219 u.saveDX("u.dx")
220 \end{python}
221 The last statement writes the solution to the external file \file{u.dx} in
222 \OpenDX file format. \OpenDX is a software package
223 for the visualization of scientific, engineering and analytical data and is freely available
224 from \url{http://www.opendx.org}.
225
226 \begin{figure}
227 \centerline{\includegraphics[width=\figwidth]{FirstStepResult.eps}}
228 \caption{\OpenDX visualization of the Possion equation soluition for $f=1$}
229 \label{fig:FirstSteps.3}
230 \end{figure}
231
232 You can edit this script using your favourite text editor (or the Integrated DeveLopment Environment IDLE
233 for Python). If the script file has the name \file{mypoisson.py} \index{scripts!\file{mypoisson.py}} you can run the
234 script from any shell using the command:
235 \begin{verbatim}
236 python mypoisson.py
237 \end{verbatim}
238 After the script has (hopefully successfully) been completed you will find the file \file{u.dx} in the current
239 directory. An easy way to visualize the results is the command
240 \begin{verbatim}
241 dx -prompter
242 \end{verbatim}
243 to start the generic data visualization interface of \OpenDX. \fig{fig:FirstSteps.3} shows the result.
244
245
246
247

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26