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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7025 - (hide annotations)
Thu Aug 6 23:40:52 2020 UTC (6 months, 3 weeks ago) by uqaeller
File MIME type: application/x-tex
File size: 20666 byte(s)
-Release 5.6
-Fixed a bug that prevented the userguide from building
-Updated the Trilinos config information in the install guide


1 ksteube 1811
2 jfenwick 3989 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 jfenwick 6651 % Copyright (c) 2003-2018 by The University of Queensland
4 jfenwick 3989 % http://www.uq.edu.au
5 gross 625 %
6 ksteube 1811 % Primary Business: Queensland, Australia
7 jfenwick 6112 % Licensed under the Apache License, version 2.0
8     % http://www.apache.org/licenses/LICENSE-2.0
9 gross 625 %
10 jfenwick 3989 % Development until 2012 by Earth Systems Science Computational Center (ESSCC)
11 jfenwick 4657 % Development 2012-2013 by School of Earth Sciences
12     % Development from 2014 by Centre for Geoscience Computing (GeoComp)
13 jfenwick 3989 %
14     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 jgs 102
16 caltinay 3274 \section{The First Steps}\label{FirstSteps}
17 acodd 6927 This chapter is an introduction on how to use \escript to solve
18 caltinay 3274 a partial differential equation\index{partial differential equation} (PDE\index{partial differential equation!PDE}).
19 caltinay 4891 We assume you are at least a little familiar with \PYTHON.
20 caltinay 5295 The knowledge presented in the \PYTHON tutorial at \url{https://docs.python.org/2/tutorial/} is more than sufficient.
21 jgs 102
22 caltinay 3274 The PDE\index{partial differential equation} we wish to solve is the Poisson equation\index{Poisson equation}
23 jgs 102 \begin{equation}
24 caltinay 3274 -\Delta u=f
25     \label{eq:FirstSteps.1}
26 jgs 102 \end{equation}
27 ksteube 1316 for the solution $u$. The function $f$ is the given right hand side. The domain of interest, denoted by $\Omega$,
28 jgs 102 is the unit square
29     \begin{equation}
30 jfenwick 3295 \Omega=[0,1]^2=\{ (x_0;x_1) | 0\le x_{0} \le 1 \mbox{ and } 0\le x_{1} \le 1 \}
31 jgs 102 \label{eq:FirstSteps.1b}
32     \end{equation}
33 jgs 107 The domain is shown in \fig{fig:FirstSteps.1}.
34 caltinay 3274 \begin{figure}[ht]
35 caltinay 3279 \centerline{\includegraphics{FirstStepDomain}}
36 caltinay 3274 \caption{Domain $\Omega=[0,1]^2$ with outer normal field $n$.}
37     \label{fig:FirstSteps.1}
38 artak 1971 \end{figure}
39 jgs 102
40 ksteube 1316 $\Delta$ denotes the Laplace operator\index{Laplace operator}, which is defined by
41 jgs 102 \begin{equation}
42 jfenwick 3295 \Delta u = (u_{,0})_{,0}+(u_{,1})_{,1}
43 jgs 102 \label{eq:FirstSteps.1.1}
44     \end{equation}
45 jfenwick 3295 where, for any function $u$ and any direction $i$, $u_{,i}$
46 caltinay 3274 denotes the partial derivative \index{partial derivative} of $u$ with respect
47     to $i$.\footnote{You may be more familiar with the Laplace
48     operator\index{Laplace operator} being written as $\nabla^2$, and written in
49     the form
50 jgs 102 \begin{equation*}
51 jfenwick 3295 \nabla^2 u = \nabla^t \cdot \nabla u = \frac{\partial^2 u}{\partial x_0^2}
52     + \frac{\partial^2 u}{\partial x_1^2}
53 jgs 102 \end{equation*}
54     and \eqn{eq:FirstSteps.1} as
55     \begin{equation*}
56 caltinay 3274 -\nabla^2 u = f
57 jgs 102 \end{equation*}
58     }
59 gross 4537 Basically, in the subindex of a function, any index to the right of the comma denotes a spatial derivative with respect
60 jfenwick 3295 to the index. To get a more compact form we will write $u_{,ij}=(u_{,i})_{,j}$
61 jgs 102 which leads to
62     \begin{equation}
63 jfenwick 3295 \Delta u = u_{,00}+u_{,11}=\sum_{i=0}^2 u_{,ii}
64 jgs 102 \label{eq:FirstSteps.1.1b}
65     \end{equation}
66 ksteube 1316 We often find that use
67     of nested $\sum$ symbols makes formulas cumbersome, and we use the more
68 jfenwick 3343 compact Einstein summation convention\index{summation convention}. This
69 ksteube 1316 drops the $\sum$ sign and assumes that a summation is performed over any repeated index.
70 acodd 6927 For instance,
71 jgs 102 \begin{eqnarray}
72 jfenwick 3295 x_{i}y_{i}=\sum_{i=0}^2 x_{i}y_{i} \\
73     x_{i}u_{,i}=\sum_{i=0}^2 x_{i}u_{,i} \\
74     u_{,ii}=\sum_{i=0}^2 u_{,ii} \\
75     x_{ij}u_{i,j}=\sum_{j=0}^2\sum_{i=0}^2 x_{ij}u_{i,j} \\
76 jgs 102 \label{eq:FirstSteps.1.1c}
77     \end{eqnarray}
78     With the summation convention we can write the Poisson equation \index{Poisson equation} as
79     \begin{equation}
80 jfenwick 3295 - u_{,ii} =1
81 jgs 102 \label{eq:FirstSteps.1.sum}
82     \end{equation}
83 lkettle 575 where $f=1$ in this example.
84    
85 jfenwick 3295 On the boundary of the domain $\Omega$ the normal derivative $n_{i} u_{,i}$
86 caltinay 3274 of the solution $u$ shall be zero, i.e. $u$ shall fulfill
87 jgs 102 the homogeneous Neumann boundary condition\index{Neumann
88     boundary condition!homogeneous}
89     \begin{equation}
90 jfenwick 3295 n_{i} u_{,i}= 0 \;.
91 jgs 102 \label{eq:FirstSteps.2}
92     \end{equation}
93 jfenwick 3295 $n=(n_{i})$ denotes the outer normal field
94 jgs 102 of the domain, see \fig{fig:FirstSteps.1}. Remember that we
95 acodd 6927 apply the Einstein summation convention \index{summation convention}, i.e. $n_{i} u_{,i}= n_{0} u_{,0} +%
96 acodd 6928 n_{1} u_{,1}$.\footnote{Some readers may be more familiar with the
97     notation $\frac{\partial u}{\partial n} = n_{i} u_{,i}=\mathbf{n}\cdot \nabla u$.}
98 jgs 102 The Neumann boundary condition of \eqn{eq:FirstSteps.2} should be fulfilled on the
99 acodd 6927 set $\Gamma^N$, the top and right edge of the domain:
100 jgs 102 \begin{equation}
101 jfenwick 3295 \Gamma^N=\{(x_0;x_1) \in \Omega | x_{0}=1 \mbox{ or } x_{1}=1 \}
102 caltinay 3274 \label{eq:FirstSteps.2b}
103 jgs 102 \end{equation}
104 acodd 6927 On the bottom and the left edge of the domain, defined
105 jgs 102 as
106     \begin{equation}
107 jfenwick 3295 \Gamma^D=\{(x_0;x_1) \in \Omega | x_{0}=0 \mbox{ or } x_{1}=0 \}
108 caltinay 3274 \label{eq:FirstSteps.2c}
109 jgs 102 \end{equation}
110 caltinay 3274 the solution shall be identical to zero:
111 jgs 102 \begin{equation}
112 caltinay 3274 u=0 \; .
113     \label{eq:FirstSteps.2d}
114 jgs 102 \end{equation}
115 acodd 6927 A homogeneous Dirichlet boundary
116 caltinay 3274 condition\index{Dirichlet boundary condition!homogeneous}.
117     The partial differential equation in \eqn{eq:FirstSteps.1.sum} together
118 acodd 6927 with Neumann \eqn{eq:FirstSteps.2} and
119     Dirichlet boundary conditions in \eqn{eq:FirstSteps.2d} form a so-called
120 caltinay 3274 boundary value
121     problem\index{boundary value problem} (BVP\index{boundary value problem!BVP})
122     for the unknown function~$u$.
123 jgs 102
124 gross 2371 \begin{figure}[ht]
125 caltinay 3279 \centerline{\includegraphics{FirstStepMesh}}
126 caltinay 3274 \caption{Mesh of $4 \times 4$ elements on a rectangular domain. Here
127     each element is a quadrilateral and described by four nodes, namely
128     the corner points. The solution is interpolated by a bi-linear
129     polynomial.}
130     \label{fig:FirstSteps.2}
131 jgs 102 \end{figure}
132    
133 caltinay 3274 In general the BVP\index{boundary value problem!BVP} cannot be solved
134 acodd 6927 analytically and numerical methods are used to construct an
135 caltinay 3274 approximation of the solution $u$.
136     Here we will use the finite element method\index{finite element method}
137     (FEM\index{finite element method!FEM}).
138     The basic idea is to fill the domain with a set of points called nodes.
139     The solution is approximated by its values on the nodes\index{finite element method!nodes}.
140     Moreover, the domain is subdivided into smaller sub-domains called
141     elements\index{finite element method!element}.
142     On each element the solution is represented by a polynomial of a certain
143     degree through its values at the nodes located in the element.
144     The nodes and their connection through elements is called a
145     mesh\index{finite element method!mesh}. \fig{fig:FirstSteps.2} shows an
146 jgs 102 example of a FEM mesh with four elements in the $x_0$ and four elements
147 caltinay 3274 in the $x_1$ direction over the unit square.
148 uqaeller 7025 For more details we refer the reader to the literature, for instance \Refe{Zienc,NumHand}.
149 jgs 102
150 caltinay 4891 The \escript solver we want to use to solve this problem is embedded into the \PYTHON interpreter language.
151 caltinay 3274 So you can solve the problem interactively but you will learn quickly that it
152 acodd 6927 is more efficient to use scripts which can be edited with your favorite editor.
153 caltinay 3274 To enter the escript environment, use the \program{run-escript}
154 caltinay 5295 command\footnote{\program{run-escript} is not available under Windows.
155 caltinay 3274 If you run under Windows you can just use the \program{python} command and the
156     \env{OMP_NUM_THREADS} environment variable to control the number of threads.}:
157 gross 2370 \begin{verbatim}
158 caltinay 3274 run-escript
159 gross 2370 \end{verbatim}
160 caltinay 4891 which will pass you on to the \PYTHON prompt
161 gross 2370 \begin{verbatim}
162 caltinay 4891 Python 2.7.6 (default, Mar 22 2014, 15:40:47)
163     [GCC 4.8.2] on linux2
164 gross 2370 Type "help", "copyright", "credits" or "license" for more information.
165     >>>
166     \end{verbatim}
167 caltinay 4891 Here you can use all available \PYTHON commands and language features\footnote{Throughout our examples, we use the python 3 form of
168     print. That is, print(1) instead of print 1.}, for instance
169 gross 2370 \begin{python}
170 caltinay 3274 >>> x=2+3
171 jfenwick 4853 >>> print("2+3=",x)
172 caltinay 3274 2+3= 5
173 gross 2370 \end{python}
174 caltinay 4891 We refer to the \PYTHON user's guide if you are not familiar with \PYTHON.
175 gross 2370
176 caltinay 3274 \escript provides the class \Poisson to define a Poisson equation\index{Poisson equation}.
177     (We will discuss a more general form of a PDE\index{partial differential equation!PDE}
178     that can be defined through the \LinearPDE class later.)
179     The instantiation of a \Poisson class object requires the specification of the domain $\Omega$.
180 acodd 6927 In \escript \Domain class objects are used to describe the geometry of a
181 caltinay 3274 domain but it also contains information about the discretization methods and
182 acodd 6927 the solver used to solve the PDE.
183     Here we use the FEM\index{finite element method} library \finley.
184 caltinay 3274 The following statements create the \Domain object \var{mydomain} from the
185 caltinay 5295 \finley function \method{Rectangle}:
186 jgs 102 \begin{python}
187 ksteube 1316 from esys.finley import Rectangle
188     mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
189 jgs 102 \end{python}
190 caltinay 5295 In this case the domain is a rectangle with the lower left corner at point $(0,0)$
191     and the upper right corner at $(\var{l0},\var{l1})=(1,1)$.
192 jfenwick 3295 The arguments \var{n0} and \var{n1} define the number of elements in $x_{0}$ and
193     $x_{1}$-direction respectively. For more details on \method{Rectangle} and
194 caltinay 5295 other \Domain generators see \Chap{chap:finley}, \Chap{chap:ripley}, and
195     \Chap{chap:speckley}.
196 jgs 102
197 jgs 107 The following statements define the \Poisson class object \var{mypde} with domain \var{mydomain} and
198 jgs 102 the right hand side $f$ of the PDE to constant $1$:
199     \begin{python}
200 ksteube 1316 from esys.escript.linearPDEs import Poisson
201     mypde = Poisson(mydomain)
202     mypde.setValue(f=1)
203 jgs 102 \end{python}
204 caltinay 3278 We have not specified any boundary condition but the \Poisson class implicitly
205     assumes homogeneous Neuman boundary conditions\index{Neumann boundary condition!homogeneous} defined by \eqn{eq:FirstSteps.2}.
206     With this boundary condition the BVP\index{boundary value problem!BVP} we have
207     defined has no unique solution.
208     In fact, with any solution $u$ and any constant $C$ the function $u+C$ becomes
209     a solution as well.
210     We have to add a Dirichlet boundary condition\index{Dirichlet boundary condition}.
211     This is done by defining a characteristic function\index{characteristic function}
212 jfenwick 3295 which has positive values at locations $x=(x_{0},x_{1})$
213 caltinay 3278 where Dirichlet boundary condition is set and $0$ elsewhere.
214     In our case of $\Gamma^D$ defined by \eqn{eq:FirstSteps.2c}, we need to
215 jfenwick 3295 construct a function \var{gammaD} which is positive for the cases $x_{0}=0$ or $x_{1}=0$.
216 caltinay 3278 To get an object \var{x} which contains the coordinates of the nodes in the domain use
217 jgs 102 \begin{python}
218 ksteube 1316 x=mydomain.getX()
219 jgs 102 \end{python}
220 caltinay 3278 The method \method{getX} of the \Domain \var{mydomain} gives access to locations
221     in the domain defined by \var{mydomain}.
222     The object \var{x} is actually a \Data object which will be discussed in
223     \Chap{ESCRIPT CHAP} in more detail.
224     What we need to know here is that \var{x} has \Rank (number of dimensions) and
225     a \Shape (list of dimensions) which can be viewed by calling the \method{getRank} and \method{getShape} methods:
226 gross 565 \begin{python}
227 jfenwick 4853 print("rank ",x.getRank(),", shape ",x.getShape())
228 gross 565 \end{python}
229 ksteube 1316 This will print something like
230 gross 565 \begin{python}
231 ksteube 1316 rank 1, shape (2,)
232 gross 565 \end{python}
233     The \Data object also maintains type information which is represented by the
234     \FunctionSpace of the object. For instance
235     \begin{python}
236 jfenwick 4853 print(x.getFunctionSpace())
237 gross 565 \end{python}
238     will print
239     \begin{python}
240 caltinay 5295 Finley_Nodes [ContinuousFunction(domain)] on FinleyMesh
241 gross 565 \end{python}
242 caltinay 3278 which tells us that the coordinates are stored on the nodes of (rather than on
243 caltinay 3331 points in the interior of) a Finley mesh.
244 jfenwick 3295 To get the $x_{0}$ coordinates of the locations we use the statement
245 gross 565 \begin{python}
246 ksteube 1316 x0=x[0]
247 gross 565 \end{python}
248 caltinay 3278 Object \var{x0} is again a \Data object now with \Rank $0$ and \Shape $()$.
249     It inherits the \FunctionSpace from \var{x}:
250 gross 565 \begin{python}
251 jfenwick 4853 print(x0.getRank(), x0.getShape(), x0.getFunctionSpace())
252 gross 565 \end{python}
253     will print
254     \begin{python}
255 caltinay 5295 0 () Finley_Nodes [ContinuousFunction(domain)] on FinleyMesh
256 gross 565 \end{python}
257 caltinay 3278 We can now construct a function \var{gammaD} which is only non-zero on the
258     bottom and left edges of the domain with
259 gross 565 \begin{python}
260 ksteube 1316 from esys.escript import whereZero
261     gammaD=whereZero(x[0])+whereZero(x[1])
262 gross 565 \end{python}
263 ksteube 1316
264 caltinay 3278 \code{whereZero(x[0])} creates a function which equals $1$ where \code{x[0]} is (almost) equal to zero and $0$ elsewhere.
265     Similarly, \code{whereZero(x[1])} creates a function which equals $1$ where \code{x[1]} is equal to zero and $0$ elsewhere.
266     The sum of the results of \code{whereZero(x[0])} and \code{whereZero(x[1])}
267 jfenwick 3295 gives a function on the domain \var{mydomain} which is strictly positive where $x_{0}$ or $x_{1}$ is equal to zero.
268 acodd 6927 Note that \var{gammaD} has the same \Rank, \Shape and \FunctionSpace as \var{x0} used to define it.
269 caltinay 3278 So from
270 gross 565 \begin{python}
271 jfenwick 4853 print(gammaD.getRank(), gammaD.getShape(), gammaD.getFunctionSpace())
272 gross 565 \end{python}
273     one gets
274     \begin{python}
275 caltinay 5295 0 () Finley_Nodes [ContinuousFunction(domain)] on FinleyMesh
276 gross 565 \end{python}
277 caltinay 3278 An additional parameter \var{q} of the \code{setValue} method of the \Poisson
278     class defines the characteristic function\index{characteristic function} of
279     the locations of the domain where the homogeneous Dirichlet boundary condition\index{Dirichlet boundary condition!homogeneous} is set.
280     The complete definition of our example is now:
281 jgs 102 \begin{python}
282 caltinay 3278 from esys.escript.linearPDEs import Poisson
283 ksteube 1316 x = mydomain.getX()
284     gammaD = whereZero(x[0])+whereZero(x[1])
285     mypde = Poisson(domain=mydomain)
286     mypde.setValue(f=1,q=gammaD)
287 jgs 102 \end{python}
288 caltinay 3278 The first statement imports the \Poisson class definition from the \linearPDEs module.
289     To get the solution of the Poisson equation defined by \var{mypde} we just have to call its \method{getSolution} method.
290 jgs 102
291 ksteube 1316 Now we can write the script to solve our Poisson problem
292 jgs 102 \begin{python}
293 ksteube 1316 from esys.escript import *
294     from esys.escript.linearPDEs import Poisson
295     from esys.finley import Rectangle
296     # generate domain:
297     mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
298     # define characteristic function of Gamma^D
299     x = mydomain.getX()
300     gammaD = whereZero(x[0])+whereZero(x[1])
301     # define PDE and get its solution u
302     mypde = Poisson(domain=mydomain)
303 caltinay 4891 mypde.setValue(f=1, q=gammaD)
304 ksteube 1316 u = mypde.getSolution()
305 jgs 102 \end{python}
306 caltinay 3278 The question is what we do with the calculated solution \var{u}.
307 caltinay 5295 Besides postprocessing, e.g. calculating the gradient or the average value,
308     which will be discussed later, plotting the solution is one of the things you
309     might want to do.
310     \escript offers two ways to do this, both based on external modules or packages.
311 acodd 6927 The first option uses the \MATPLOTLIB module which allows plotting of 2D
312 caltinay 5295 results relatively quickly from within the \PYTHON script, see~\cite{matplotlib}.
313     However, there are limitations when using this tool, especially for large
314     problems and when solving three-dimensional problems.
315     Therefore, \escript provides functionality to export data as files which can
316     subsequently be read by third-party software packages such as
317     \mayavi\cite{mayavi} or \VisIt~\cite{VisIt}.
318 jgs 102
319 gross 2574 \subsection{Plotting Using \MATPLOTLIB}
320 caltinay 5295 The \MATPLOTLIB module provides a simple and easy-to-use way to visualize PDE
321     solutions (or other \Data objects).
322     To hand over data from \escript to \MATPLOTLIB the values need to be mapped onto
323     a rectangular grid. We will make use of the \numpy module for this.
324 gross 2574
325 caltinay 3278 First we need to create a rectangular grid which is accomplished by the following statements:
326 gross 2574 \begin{python}
327 caltinay 3278 import numpy
328     x_grid = numpy.linspace(0., 1., 50)
329     y_grid = numpy.linspace(0., 1., 50)
330 gross 2574 \end{python}
331 caltinay 3278 \var{x_grid} is an array defining the x coordinates of the grid while
332     \var{y_grid} defines the y coordinates of the grid.
333     In this case we use $50$ points over the interval $[0,1]$ in both directions.
334 gross 2574
335 caltinay 3278 Now the values created by \escript need to be interpolated to this grid.
336     We will use the \MATPLOTLIB \function{mlab.griddata} function to do this.
337     Spatial coordinates are easily extracted as a \var{list} by
338 gross 2574 \begin{python}
339 caltinay 3278 x=mydomain.getX()[0].toListOfTuples()
340     y=mydomain.getX()[1].toListOfTuples()
341 gross 2574 \end{python}
342 caltinay 3278 In principle we can apply the same \member{toListOfTuples} method to extract the values from the PDE solution \var{u}.
343     However, we have to make sure that the \Data object we extract the values from
344     uses the same \FunctionSpace as we have used when extracting \var{x} and \var{y}.
345     We apply the \function{interpolation} to \var{u} before extraction to achieve this:
346 gross 2574 \begin{python}
347 caltinay 3278 z=interpolate(u, mydomain.getX().getFunctionSpace())
348 gross 2574 \end{python}
349 caltinay 3278 The values in \var{z} are the values at the points with the coordinates given by \var{x} and \var{y}.
350     These values are interpolated to the grid defined by \var{x_grid} and \var{y_grid} by using
351 gross 2574 \begin{python}
352 caltinay 3278 import matplotlib
353     z_grid = matplotlib.mlab.griddata(x, y, z, xi=x_grid, yi=y_grid)
354 gross 2574 \end{python}
355 caltinay 3278 Now \var{z_grid} gives the values of the PDE solution \var{u} at the grid which can be plotted using \function{contourf}:
356 gross 2574 \begin{python}
357 caltinay 3278 matplotlib.pyplot.contourf(x_grid, y_grid, z_grid, 5)
358     matplotlib.pyplot.savefig("u.png")
359 gross 2574 \end{python}
360 caltinay 3278 Here we use 5 contours. The last statement writes the plot to the file \file{u.png} in the PNG format.
361     Alternatively, one can use
362 gross 2574 \begin{python}
363 caltinay 3278 matplotlib.pyplot.contourf(x_grid, y_grid, z_grid, 5)
364     matplotlib.pyplot.show()
365 gross 2574 \end{python}
366     which gives an interactive browser window.
367    
368     \begin{figure}
369 caltinay 3279 \centerline{\includegraphics[width=\figwidth]{FirstStepResultMATPLOTLIB}}
370 caltinay 3278 \caption{Visualization of the Poisson Equation Solution for $f=1$ using \MATPLOTLIB}
371 gross 2574 \label{fig:FirstSteps.3b}
372     \end{figure}
373    
374     Now we can write the script to solve our Poisson problem
375     \begin{python}
376 caltinay 3278 from esys.escript import *
377     from esys.escript.linearPDEs import Poisson
378     from esys.finley import Rectangle
379     import numpy
380     import matplotlib
381     import pylab
382     # generate domain:
383     mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
384     # define characteristic function of Gamma^D
385     x = mydomain.getX()
386     gammaD = whereZero(x[0])+whereZero(x[1])
387     # define PDE and get its solution u
388     mypde = Poisson(domain=mydomain)
389     mypde.setValue(f=1,q=gammaD)
390     u = mypde.getSolution()
391     # interpolate u to a matplotlib grid:
392     x_grid = numpy.linspace(0.,1.,50)
393     y_grid = numpy.linspace(0.,1.,50)
394     x=mydomain.getX()[0].toListOfTuples()
395     y=mydomain.getX()[1].toListOfTuples()
396 gross 3666 z=interpolate(u,mydomain.getX().getFunctionSpace()).toListOfTuples()
397 caltinay 3278 z_grid = matplotlib.mlab.griddata(x,y,z,xi=x_grid,yi=y_grid )
398     # interpolate u to a rectangular grid:
399     matplotlib.pyplot.contourf(x_grid, y_grid, z_grid, 5)
400     matplotlib.pyplot.savefig("u.png")
401 gross 2574 \end{python}
402 jfenwick 3295 The entire code is available as \file{poisson_matplotlib.py} in the \ExampleDirectory.
403 gross 2574 You can run the script using the {\it escript} environment
404 ksteube 1316 \begin{verbatim}
405 caltinay 3278 run-escript poisson_matplotlib.py
406 ksteube 1316 \end{verbatim}
407 caltinay 5295 This will create a file called \file{u.png}, see \fig{fig:FirstSteps.3b}.
408 gross 2574 For details on the usage of the \MATPLOTLIB module we refer to the documentation~\cite{matplotlib}.
409 ksteube 1316
410 caltinay 3278 As pointed out, \MATPLOTLIB is restricted to the two-dimensional case and
411     should be used for small problems only.
412     It can not be used under \MPI as the \member{toListOfTuples} method is not
413     safe under \MPI\footnote{The phrase 'safe under \MPI' means that a program
414     will produce correct results when run on more than one processor under \MPI.}.
415 gross 2574
416 gross 2580 \begin{figure}
417 caltinay 3279 \centerline{\includegraphics[width=\figwidth]{FirstStepResult}}
418 gross 2580 \caption{Visualization of the Poisson Equation Solution for $f=1$}
419     \label{fig:FirstSteps.3}
420     \end{figure}
421    
422 caltinay 3278 \subsection{Visualization using export files}
423 gross 2574
424 caltinay 3278 As an alternative to \MATPLOTLIB, {\it escript} supports exporting data to
425     \VTK and \SILO files which can be read by visualization tools such as
426 caltinay 5662 \mayavi\cite{mayavi} and \VisIt~\cite{VisIt}. This method is \MPI safe and
427 caltinay 3278 works with large 2D and 3D problems.
428 gross 2574
429 caltinay 3278 To write the solution \var{u} of the Poisson problem in the \VTK file format
430     to the file \file{u.vtu} one needs to add:
431 gross 2574 \begin{python}
432 caltinay 3348 from esys.weipa import saveVTK
433 caltinay 3278 saveVTK("u.vtu", sol=u)
434 gross 2574 \end{python}
435 caltinay 3278 This file can then be opened in a \VTK compatible visualization tool where the
436 caltinay 3348 solution is accessible by the name {\it sol}. Similarly,
437     \begin{python}
438     from esys.weipa import saveSilo
439     saveSilo("u.silo", sol=u)
440     \end{python}
441 caltinay 5295 will write \var{u} to a \SILO file if escript was compiled with support for
442     LLNL's \SILO library.
443 gross 2574
444     The Poisson problem script is now
445     \begin{python}
446     from esys.escript import *
447     from esys.escript.linearPDEs import Poisson
448     from esys.finley import Rectangle
449 caltinay 3348 from esys.weipa import saveVTK
450 gross 2574 # generate domain:
451     mydomain = Rectangle(l0=1.,l1=1.,n0=40, n1=20)
452     # define characteristic function of Gamma^D
453     x = mydomain.getX()
454     gammaD = whereZero(x[0])+whereZero(x[1])
455     # define PDE and get its solution u
456     mypde = Poisson(domain=mydomain)
457     mypde.setValue(f=1,q=gammaD)
458     u = mypde.getSolution()
459     # write u to an external file
460 caltinay 3278 saveVTK("u.vtu",sol=u)
461 gross 2574 \end{python}
462 jfenwick 3295 The entire code is available as \file{poisson_vtk.py} in the \ExampleDirectory.
463 gross 2574
464 caltinay 3278 You can run the script using the {\it escript} environment and visualize the
465     solution using \mayavi:
466 gross 2574 \begin{verbatim}
467 caltinay 3279 run-escript poisson_vtk.py
468 caltinay 5296 mayavi2 -d u.vtu -m Surface
469 gross 2574 \end{verbatim}
470 caltinay 3278 The result is shown in \fig{fig:FirstSteps.3}.
471    

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26