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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5297 - (show annotations)
Thu Dec 4 01:58:53 2014 UTC (4 years, 4 months ago) by caltinay
File MIME type: application/x-tex
File size: 78659 byte(s)
More user doco updates.

1
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % Copyright (c) 2003-2014 by University of Queensland
4 % http://www.uq.edu.au
5 %
6 % Primary Business: Queensland, Australia
7 % Licensed under the Open Software License version 3.0
8 % http://www.opensource.org/licenses/osl-3.0.php
9 %
10 % Development until 2012 by Earth Systems Science Computational Center (ESSCC)
11 % Development 2012-2013 by School of Earth Sciences
12 % Development from 2014 by Centre for Geoscience Computing (GeoComp)
13 %
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15
16 \chapter{The \escript Module}\label{ESCRIPT CHAP}
17
18 \section{Concepts}
19 \escript is a \PYTHON module that allows you to represent the values of
20 a function at points in a \Domain in such a way that the function will
21 be useful for the Finite Element Method (FEM) simulation. It also
22 provides what we call a function space that describes how the data is
23 used in the simulation. Stored along with the data is information
24 about the elements and nodes which will be used by the domain (e.g. \finley).
25
26 \subsection{Function spaces}
27 In order to understand what we mean by the term 'function space',
28 consider that the solution of a partial differential
29 equation\index{partial differential equation} (PDE) is a function on a domain
30 $\Omega$. When solving a PDE using FEM, the solution is
31 piecewise-differentiable but, in general, its gradient is discontinuous.
32 To reflect these different degrees of smoothness, different function spaces
33 are used.
34 For instance, in FEM, the displacement field is represented by its values at
35 the nodes of the mesh, and so is continuous.
36 The strain, which is the symmetric part of the gradient of the displacement
37 field, is stored on the element centers, and so is considered to be
38 discontinuous.
39
40 A function space is described by a \FunctionSpace object.
41 The following statement generates the object \var{solution_space} which is
42 a \FunctionSpace object and provides access to the function space of
43 PDE solutions on the \Domain \var{mydomain}:
44
45 \begin{python}
46 solution_space=Solution(mydomain)
47 \end{python}
48 The following generators for function spaces on a \Domain \var{mydomain} are commonly used:
49 \begin{itemize}
50 \item \var{Solution(mydomain)}: solutions of a PDE
51 \item \var{ReducedSolution(mydomain)}: solutions of a PDE with a reduced
52 smoothness requirement, e.g. using a lower order approximation on the same
53 element or using macro elements\index{macro elements}
54 \item \var{ContinuousFunction(mydomain)}: continuous functions, e.g. a temperature distribution
55 \item \var{Function(mydomain)}: general functions which are not necessarily continuous, e.g. a stress field
56 \item \var{FunctionOnBoundary(mydomain)}: functions on the boundary of the domain, e.g. a surface pressure
57 \item \var{FunctionOnContact0(mydomain)}: functions on side $0$ of a discontinuity
58 \item \var{FunctionOnContact1(mydomain)}: functions on side $1$ of a discontinuity
59 \end{itemize}
60 In some cases under-integration is used. For these cases the user may use a
61 \FunctionSpace from the following list:
62 \begin{itemize}
63 \item \var{ReducedFunction(mydomain)}
64 \item \var{ReducedFunctionOnBoundary(mydomain)}
65 \item \var{ReducedFunctionOnContact0(mydomain)}
66 \item \var{ReducedFunctionOnContact1(mydomain)}
67 \end{itemize}
68 In comparison to the corresponding full version they use a reduced number of
69 integration nodes (typically one only) to represent values.
70
71 \begin{figure}
72 \centering
73 \includegraphics{EscriptDiagram1}
74 \caption{\label{ESCRIPT DEP}Dependency of function spaces in \finley.
75 An arrow indicates that a function in the \FunctionSpace at the starting point
76 can be interpolated to the \FunctionSpace of the arrow target.
77 All function spaces above the dotted line can be interpolated to any of
78 the function spaces below the line. See also \Sec{SEC Projection}.}
79 \end{figure}
80
81 The reduced smoothness for a PDE solution is often used to fulfill the
82 Ladyzhenskaya-Babuska-Brezzi condition~\cite{LBB} when solving saddle point
83 problems\index{saddle point problems}, e.g. the Stokes equation.
84 A discontinuity\index{discontinuity} is a region within the domain across
85 which functions may be discontinuous.
86 The location of a discontinuity is defined in the \Domain object.
87 \fig{ESCRIPT DEP} shows the dependency between the types of function spaces
88 in \finley (other libraries may have different relationships).
89
90 The solution of a PDE is a continuous function. Any continuous function can
91 be seen as a general function on the domain and can be restricted to the
92 boundary as well as to one side of a discontinuity (the result will be
93 different depending on which side is chosen). Functions on any side of the
94 discontinuity can be seen as a function on the corresponding other side.
95
96 A function on the boundary or on one side of the discontinuity cannot be seen
97 as a general function on the domain as there are no values defined for the
98 interior. For most PDE solver libraries the space of the solution and
99 continuous functions is identical, however in some cases, for example when
100 periodic boundary conditions are used in \finley, a solution fulfills periodic
101 boundary conditions while a continuous function does not have to be periodic.
102
103 The concept of function spaces describes the properties of functions and
104 allows abstraction from the actual representation of the function in the
105 context of a particular application. For instance, in the FEM context a
106 function of the \Function type (written as \emph{Function()} in \fig{ESCRIPT DEP})
107 is usually represented by its values at the element center,
108 but in a finite difference scheme the edge midpoint of cells is preferred.
109 By changing its function space you can use the same function in a Finite
110 Difference scheme instead of Finite Element scheme.
111 Changing the function space of a particular function will typically lead to
112 a change of its representation.
113 So, when seen as a general function, a continuous function which is typically
114 represented by its values on the nodes of the FEM mesh or finite difference
115 grid must be interpolated to the element centers or the cell edges,
116 respectively. Interpolation happens automatically in \escript whenever it is
117 required\index{interpolation}. The user needs to be aware that an
118 interpolation is not always possible, see \fig{ESCRIPT DEP} for \finley.
119 An alternative approach to change the representation (=\FunctionSpace) is
120 projection\index{projection}, see \Sec{SEC Projection}.
121
122 \subsection{\Data Objects}
123 In \escript the class that stores these functions is called \Data.
124 The function is represented through its values on \DataSamplePoints where
125 the \DataSamplePoints are chosen according to the function space of the
126 function.
127 \Data class objects are used to define the coefficients of the PDEs to be
128 solved by a PDE solver library and also to store the solutions of the PDE.
129
130 The values of the function have a rank which gives the number of indices,
131 and a \Shape defining the range of each index.
132 The rank in \escript is limited to the range 0 through 4 and it is assumed
133 that the rank and \Shape is the same for all \DataSamplePoints.
134 The \Shape of a \Data object is a tuple (list) \var{s} of integers.
135 The length of \var{s} is the rank of the \Data object and the \var{i}-th
136 index ranges between 0 and $\var{s[i]}-1$.
137 For instance, a stress field has rank 2 and \Shape $(d,d)$ where $d$ is the
138 number of spatial dimensions.
139 The following statement creates the \Data object \var{mydat} representing a
140 continuous function with values of \Shape $(2,3)$ and rank $2$:
141 \begin{python}
142 mydat=Data(value=1, what=ContinuousFunction(myDomain), shape=(2,3))
143 \end{python}
144 The initial value is the constant 1 for all \DataSamplePoints and all
145 components.
146
147 \Data objects can also be created from any \numpy array or any object, such
148 as a list of floating point numbers, that can be converted into
149 a \numpyNDA\cite{NUMPY}.
150 The following two statements create objects which are equivalent
151 to \var{mydat}:
152 \begin{python}
153 mydat1=Data(value=numpy.ones((2,3)), what=ContinuousFunction(myDomain))
154 mydat2=Data(value=[[1,1], [1,1], [1,1]], what=ContinuousFunction(myDomain))
155 \end{python}
156 In the first case the initial value is \var{numpy.ones((2,3))} which generates
157 a $2 \times 3$ matrix as an instance of \numpyNDA filled with ones.
158 The \Shape of the created \Data object is taken from the \Shape of the array.
159 In the second case, the creator converts the initial value, which is a list of
160 lists, into a \numpyNDA before creating the actual \Data object.
161
162 For convenience \escript provides creators for the most common types
163 of \Data objects in the following forms (\var{d} defines the spatial
164 dimensionality):
165 \begin{itemize}
166 \item \code{Scalar(0, Function(mydomain))} is the same as \code{Data(0, Function(myDomain),(,))}\\
167 (each value is a scalar), e.g. a temperature field
168 \item \code{Vector(0, Function(mydomain))} is the same as \code{Data(0, Function(myDomain),(d,))}\\
169 (each value is a vector), e.g. a velocity field
170 \item \code{Tensor(0, Function(mydomain))} equals \code{Data(0, Function(myDomain), (d,d))},
171 e.g. a stress field
172 \item \code{Tensor4(0,Function(mydomain))} equals \code{Data(0,Function(myDomain), (d,d,d,d))},
173 e.g. a Hook tensor field
174 \end{itemize}
175 Here the initial value is 0 but any object that can be converted into
176 a \numpyNDA and whose \Shape is consistent with \Shape of the \Data object to
177 be created can be used as the initial value.
178
179 \Data objects can be manipulated by applying unary operations (e.g. cos, sin,
180 log), and they can be combined point-wise by applying arithmetic operations
181 (e.g. +, - ,* , /).
182 We emphasize that \escript itself does not handle any spatial dependencies as
183 it does not know how values are interpreted by the processing PDE solver library.
184 However \escript invokes interpolation if this is needed during data manipulations.
185 Typically, this occurs in binary operations when the arguments belong to
186 different function spaces or when data are handed over to a PDE solver library
187 which requires functions to be represented in a particular way.
188
189 The following example shows the usage of \Data objects. Assume we have a
190 displacement field $u$ and we want to calculate the corresponding stress field
191 $\sigma$ using the linear-elastic isotropic material model
192 \begin{eqnarray}\label{eq: linear elastic stress}
193 \sigma_{ij}=\lambda u_{k,k} \delta_{ij} + \mu ( u_{i,j} + u_{j,i})
194 \end{eqnarray}
195 where $\delta_{ij}$ is the Kronecker symbol and
196 $\lambda$ and $\mu$ are the Lam\'e coefficients. The following function
197 takes the displacement \var{u} and the Lam\'e coefficients \var{lam} and \var{mu}
198 as arguments and returns the corresponding stress:
199 \begin{python}
200 from esys.escript import *
201 def getStress(u, lam, mu):
202 d=u.getDomain().getDim()
203 g=grad(u)
204 stress=lam*trace(g)*kronecker(d)+mu*(g+transpose(g))
205 return stress
206 \end{python}
207 The variable \var{d} gives the spatial dimensionality of the domain on which
208 the displacements are defined.
209 \var{kronecker} returns the Kronecker symbol with indices $i$ and $j$ running
210 from 0 to \var{d}-1.
211 The call \var{grad(u)} requires the displacement field \var{u} to be in
212 the \var{Solution} or \ContinuousFunction.
213 The result \var{g} as well as the returned stress will be in the \Function.
214 If, for example, \var{u} is the solution of a PDE then \code{getStress} might
215 be called in the following way:
216 \begin{python}
217 s=getStress(u, 1., 2.)
218 \end{python}
219 However \code{getStress} can also be called with \Data objects as values for
220 \var{lam} and \var{mu} which, for instance in the case of a temperature
221 dependency, are calculated by an expression.
222 The following call is equivalent to the previous example:
223 \begin{python}
224 lam=Scalar(1., ContinuousFunction(mydomain))
225 mu=Scalar(2., Function(mydomain))
226 s=getStress(u, lam, mu)
227 \end{python}
228 %
229 The function \var{lam} belongs to the \ContinuousFunction but with \var{g} the
230 function \var{trace(g)} is in the \Function.
231 In the evaluation of the product \var{lam*trace(g)} we have different function
232 spaces (on the nodes versus in the centers) and at first glance we have incompatible data.
233 \escript converts the arguments into an appropriate function space according
234 to \fig{ESCRIPT DEP}.
235 In this example that means \escript sees \var{lam} as a function of the \Function.
236 In the context of FEM this means the nodal values of \var{lam} are
237 interpolated to the element centers.
238 The interpolation is automatic and requires no special handling.
239
240 \begin{figure}
241 \centering
242 \includegraphics{EscriptDiagram2}
243 \caption{\label{Figure: tag}Element Tagging. A rectangular mesh over a region
244 with two rock types {\it white} and {\it gray} is shown.
245 The number in each cell refers to the major rock type present in the cell
246 ($1$ for {\it white} and $2$ for {\it gray}).}
247 \end{figure}
248
249 \subsection{Tagged, Expanded and Constant Data}
250 Material parameters such as the Lam\'e coefficients are typically dependent on
251 rock types present in the area of interest.
252 A common technique to handle these kinds of material parameters is
253 \emph{tagging}\index{tagging}, which uses storage efficiently.
254 \fig{Figure: tag} shows an example. In this case two rock types {\it white}
255 and {\it gray} can be found in the domain.
256 The domain is subdivided into triangular shaped cells.
257 Each cell has a tag indicating the rock type predominantly found in this cell.
258 Here $1$ is used to indicate rock type {\it white} and $2$ for rock type {\it gray}.
259 The tags are assigned at the time when the cells are generated and stored in
260 the \Domain class object. To allow easier usage of tags, names can be used
261 instead of numbers. These names are typically defined at the time when the
262 geometry is generated.
263
264 The following statements show how to use tagged values for \var{lam} as shown
265 in \fig{Figure: tag} for the stress calculation discussed above:
266 \begin{python}
267 lam=Scalar(value=2., what=Function(mydomain))
268 insertTaggedValue(lam, white=30., gray=5000.)
269 s=getStress(u, lam, 2.)
270 \end{python}
271 In this example \var{lam} is set to $30$ for those cells with tag {\it white}
272 (=$1$) and to $5000$ for cells with tag {\it gray} (=$2$).
273 The initial value $2$ of \var{lam} is used as a default value for the case
274 when a tag is encountered which has not been linked with a value.
275 The \code{getStress} method does not need to be changed now that we are using tags.
276 \escript resolves the tags when \var{lam*trace(g)} is calculated.
277
278 This brings us to a very important point about \escript.
279 You can develop a simulation with constant Lam\'e coefficients, and then later
280 switch to tagged Lam\'e coefficients without otherwise changing your \PYTHON script.
281 In short, you can use the same script for models with different domains and
282 different types of input data.
283
284 There are three main ways in which \Data objects are represented internally --
285 constant, tagged, and expanded.
286 In the constant case, the same value is used at each sample point while only a
287 single value is stored to save memory.
288 In the expanded case, each sample point has an individual value (such as for the solution of a PDE).
289 This is where your largest data sets will be created because the values are
290 stored as a complete array.
291 The tagged case has already been discussed above.
292 Expanded data is created when specifying \code{expanded=True} in the \Data
293 object constructor, while tagged data requires calling the \member{insertTaggedValue}
294 method as shown above.
295
296 Values are accessed through a sample reference number.
297 Operations on expanded \Data objects have to be performed for each sample
298 point individually.
299 When tagged values are used, the values are held in a dictionary.
300 Operations on tagged data require processing the set of tagged values only,
301 rather than processing the value for each individual sample point.
302 \escript allows any mixture of constant, tagged and expanded data in a single expression.
303
304 \subsection{Saving and Restoring Simulation Data}
305 \Data objects can be written to disk files with the \member{dump} method and
306 read back using the \member{load} method, both of which use the
307 \netCDF\cite{NETCDF} file format.
308 Use these to save data for checkpoint/restart or simply to save and reuse data
309 that was expensive to compute.
310 For instance, to save the coordinates of the data points of a
311 \ContinuousFunction to the file \file{x.nc} use
312 \begin{python}
313 x=ContinuousFunction(mydomain).getX()
314 x.dump("x.nc")
315 mydomain.dump("dom.nc")
316 \end{python}
317 To recover the object \var{x}, and you know that \var{mydomain} was an \finley
318 mesh, use
319 \begin{python}
320 from esys.finley import LoadMesh
321 mydomain=LoadMesh("dom.nc")
322 x=load("x.nc", mydomain)
323 \end{python}
324 Obviously, it is possible to execute the same steps that were originally used
325 to generate \var{mydomain} to recreate it. However, in most cases using
326 \member{dump} and \member{load} is faster, particularly if optimization has
327 been applied.
328 If \escript is running on more than one \MPI process \member{dump} will create
329 an individual file for each process containing the local data.
330 In order to avoid conflicts the \MPI processor
331 rank is appended to the file names.
332 That is instead of one file \file{dom.nc} you would get
333 \file{dom.nc.0000}, \file{dom.nc.0001}, etc.
334 You still call \code{LoadMesh("dom.nc")} to load the domain but you have to
335 make sure that the appropriate file is accessible from the corresponding rank,
336 and loading will only succeed if you run with as many processes as were used
337 when calling \member{dump}.
338
339 The function space of the \Data is stored in \file{x.nc}.
340 If the \Data object is expanded, the number of data points in the file and of
341 the \Domain for the particular \FunctionSpace must match.
342 Moreover, the ordering of the values is checked using the reference
343 identifiers provided by the \FunctionSpace on the \Domain.
344 In some cases, data points will be reordered so be aware and confirm that you
345 get what you wanted.
346
347 A more flexible way of saving and restoring \escript simulation data
348 is through an instance of the \class{DataManager} class.
349 It has the advantage of allowing to save and load not only a \Domain and
350 \Data objects but also other values\footnote{The \PYTHON \emph{pickle} module
351 is used for other types.} you compute in your simulation script.
352 Further, \class{DataManager} objects can simultaneously create files for
353 visualization so no extra calls to \code{saveVTK} etc. are needed.
354
355 The following example shows how the \class{DataManager} class can be used.
356 For an explanation of all member functions and options see the class reference
357 Section \ref{sec:datamanager}.
358 \begin{python}
359 from esys.escript import DataManager, Scalar, Function
360 from esys.finley import Rectangle
361
362 dm = DataManager(formats=[DataManager.RESTART, DataManager.VTK])
363 if dm.hasData():
364 mydomain=dm.getDomain()
365 val=dm.getValue("val")
366 t=dm.getValue("t")
367 t_max=dm.getValue("t_max")
368 else:
369 mydomain=Rectangle()
370 val=Function(mydomain).getX()
371 t=0.
372 t_max=2.5
373
374 while t<t_max:
375 t+=.01
376 val=val+t/2
377 dm.addData(val=val, t=t, t_max=t_max)
378 dm.export()
379 \end{python}
380 In the constructor we specify that we want \code{RESTART} (i.e. dump) files
381 and \code{VTK} files to be saved.
382 By default, the constructor will look for previously saved \code{RESTART}
383 files under the current directory and load them.
384 We can then enquire if such files were found by calling the \member{hasData}
385 method. If it returns \True we retrieve the domain and values into local
386 variables. Otherwise the same variables are initialized with appropriate
387 values to start a new simulation.
388 Note, that \var{t} and \var{t_max} are regular floating point values and not
389 \Data objects. Yet they are treated the same way by the \class{DataManager}.
390
391 After this initialization step the script enters the main simulation loop
392 where calculations are performed.
393 When these are finalized for a time step we call the \member{addData} method
394 to let the manager know which variables to store on disk.
395 This does not actually save the data yet and it is allowed to call
396 \member{addData} more than once to add information incrementally, e.g. from
397 separate functions that have access to the \class{DataManager} instance.
398 Once all variables have been added the \member{export} method has to be called
399 to flush all data to disk and clear the manager.
400 In this example, this call dumps \var{mydomain} and \var{val} to files
401 in a restart directory and also stores \var{t} and \var{t_max} on disk.
402 Additionally, it generates a \VTK file for visualization of the data.
403 If the script would stop running before its completion for some reason (e.g.
404 because its runtime limit was exceeded in a batch job environment), you could
405 simply run it again and it would resume at the point it stopped before.
406
407 \section{\escript Classes}
408
409 \subsection{The \Domain class}
410 \begin{classdesc}{Domain}{}
411 A \Domain object is used to describe a geometric region together with
412 a way of representing functions over this region.
413 The \Domain class provides an abstract interface to the domain of \FunctionSpace and \Data objects.
414 \Domain needs to be subclassed in order to provide a complete implementation.
415 \end{classdesc}
416
417 \vspace{1em}\noindent The following methods are available:
418 \begin{methoddesc}[Domain]{getDim}{}
419 returns the number of spatial dimensions of the \Domain.
420 \end{methoddesc}
421 %
422 \begin{methoddesc}[Domain]{dump}{filename}
423 writes the \Domain to the file \var{filename} using the \netCDF file format.
424 \end{methoddesc}
425 %
426 \begin{methoddesc}[Domain]{getX}{}
427 returns the locations in the \Domain. The \FunctionSpace of the returned
428 \Data object is chosen by the \Domain implementation. Typically it will be
429 in the \ContinuousFunction.
430 \end{methoddesc}
431 %
432 \begin{methoddesc}[Domain]{setX}{newX}
433 assigns new locations to the \Domain. \var{newX} has to have \Shape $(d,)$
434 where $d$ is the spatial dimensionality of the domain. Typically \var{newX}
435 must be in the \ContinuousFunction but the space actually to be used
436 depends on the \Domain implementation. Not all domain families support
437 setting locations.
438 \end{methoddesc}
439 %
440 \begin{methoddesc}[Domain]{getNormal}{}
441 returns the surface normals on the boundary of the \Domain as a \Data object.
442 \end{methoddesc}
443 %
444 \begin{methoddesc}[Domain]{getSize}{}
445 returns the local sample size, i.e. the element diameter, as a \Data object.
446 \end{methoddesc}
447 %
448 \begin{methoddesc}[Domain]{setTagMap}{tag_name, tag}
449 defines a mapping of the tag name \var{tag_name} to the \var{tag}.
450 \end{methoddesc}
451 %
452 \begin{methoddesc}[Domain]{getTag}{tag_name}
453 returns the tag associated with the tag name \var{tag_name}.
454 \end{methoddesc}
455 %
456 \begin{methoddesc}[Domain]{isValidTagName}{tag_name}
457 returns \True if \var{tag_name} is a valid tag name.
458 \end{methoddesc}
459 %
460 \begin{methoddesc}[Domain]{__eq__}{arg}
461 (\PYTHON \var{==} operator) returns \True if the \Domain \var{arg}
462 describes the same domain, \False otherwise.
463 \end{methoddesc}
464 %
465 \begin{methoddesc}[Domain]{__ne__}{arg}
466 (\PYTHON \var{!=} operator) returns \True if the \Domain \var{arg} does
467 not describe the same domain, \False otherwise.
468 \end{methoddesc}
469 %
470 \begin{methoddesc}[Domain]{__str__}{}
471 (\PYTHON \var{str()} function) returns a string representation of the
472 \Domain.
473 \end{methoddesc}
474 %
475 \begin{methoddesc}[Domain]{onMasterProcessor}{}
476 returns \True if the process is the master process within the \MPI
477 process group used by the \Domain. This is the process with rank 0.
478 If \MPI support is not enabled the return value is always \True.
479 \end{methoddesc}
480 %
481 \begin{methoddesc}[Domain]{getMPISize}{}
482 returns the number of \MPI processes used for this \Domain. If \MPI
483 support is not enabled 1 is returned.
484 \end{methoddesc}
485 %
486 \begin{methoddesc}[Domain]{getMPIRank}{}
487 returns the rank of the process executing the statement within the
488 \MPI process group used by the \Domain. If \MPI support is not enabled
489 0 is returned.
490 \end{methoddesc}
491 %
492 \begin{methoddesc}[Domain]{MPIBarrier}{}
493 executes barrier synchronization within the \MPI process group used by
494 the \Domain. If \MPI support is not enabled, this command does nothing.
495 \end{methoddesc}
496
497 \subsection{The \FunctionSpace class}
498 \begin{classdesc}{FunctionSpace}{}
499 \FunctionSpace objects, which are instantiated by generator functions, are
500 used to define properties of \Data objects such as continuity.
501 A \Data object in a particular \FunctionSpace is represented by its values at
502 \DataSamplePoints which are defined by the type and the \Domain of the \FunctionSpace.
503 \end{classdesc}
504
505 \vspace{1em}\noindent The following methods are available:
506 %
507 \begin{methoddesc}[FunctionSpace]{getDim}{}
508 returns the spatial dimensionality of the \Domain of the \FunctionSpace.
509 \end{methoddesc}
510 %
511 \begin{methoddesc}[FunctionSpace]{getX}{}
512 returns the location of the \DataSamplePoints.
513 \end{methoddesc}
514 %
515 \begin{methoddesc}[FunctionSpace]{getNormal}{}
516 If the domain of functions in the \FunctionSpace is a hyper-manifold (e.g.
517 the boundary of a domain) the method returns the outer normal at each of
518 the \DataSamplePoints. Otherwise an exception is raised.
519 \end{methoddesc}
520 %
521 \begin{methoddesc}[FunctionSpace]{getSize}{}
522 returns a \Data object measuring the spacing of the \DataSamplePoints.
523 The size may be zero.
524 \end{methoddesc}
525 %
526 \begin{methoddesc}[FunctionSpace]{getDomain}{}
527 returns the \Domain of the \FunctionSpace.
528 \end{methoddesc}
529 %
530 \begin{methoddesc}[FunctionSpace]{setTags}{new_tag, mask}
531 assigns a new tag \var{new_tag} to all data samples where \var{mask} is
532 positive for a least one data point.
533 \var{mask} must be defined on this \FunctionSpace.
534 Use the \var{setTagMap} to assign a tag name to \var{new_tag}.
535 \end{methoddesc}
536 %
537 \begin{methoddesc}[FunctionSpace]{__eq__}{arg}
538 (\PYTHON \var{==} operator) returns \True if the \FunctionSpace \var{arg}
539 describes the same function space, \False otherwise.
540 \end{methoddesc}
541 %
542 \begin{methoddesc}[FunctionSpace]{__ne__}{arg}
543 (\PYTHON \var{!=} operator) returns \True if the \FunctionSpace \var{arg}
544 does not describe the same function space, \False otherwise.
545 \end{methoddesc}
546
547 \begin{methoddesc}[Domain]{__str__}{}
548 (\PYTHON \var{str()} function) returns a string representation of the
549 \FunctionSpace.
550 \end{methoddesc}
551
552 \noindent The following functions provide generators for \FunctionSpace objects:
553
554 \begin{funcdesc}{Function}{domain}
555 returns the \Function on the \Domain \var{domain}. \Data objects in this
556 type of \Function are defined over the whole geometric region defined by
557 \var{domain}.
558 \end{funcdesc}
559 %
560 \begin{funcdesc}{ContinuousFunction}{domain}
561 returns the \ContinuousFunction on the \Domain domain. \Data objects in
562 this type of \Function are defined over the whole geometric region defined
563 by \var{domain} and assumed to represent a continuous function.
564 \end{funcdesc}
565 %
566 \begin{funcdesc}{FunctionOnBoundary}{domain}
567 returns the \FunctionOnBoundary on the \Domain domain. \Data objects in
568 this type of \Function are defined on the boundary of the geometric region
569 defined by \var{domain}.
570 \end{funcdesc}
571 %
572 \begin{funcdesc}{FunctionOnContactZero}{domain}
573 returns the \FunctionOnContactZero the \Domain domain. \Data objects in
574 this type of \Function are defined on side 0 of a discontinuity within
575 the geometric region defined by \var{domain}.
576 The discontinuity is defined when \var{domain} is instantiated.
577 \end{funcdesc}
578 %
579 \begin{funcdesc}{FunctionOnContactOne}{domain}
580 returns the \FunctionOnContactOne on the \Domain domain. \Data objects in
581 this type of \Function are defined on side 1 of a discontinuity within
582 the geometric region defined by \var{domain}.
583 The discontinuity is defined when \var{domain} is instantiated.
584 \end{funcdesc}
585 %
586 \begin{funcdesc}{Solution}{domain}
587 returns the \SolutionFS on the \Domain domain. \Data objects in this type
588 of \Function are defined on the geometric region defined by \var{domain}
589 and are solutions of partial differential equations\index{partial differential equation}.
590 \end{funcdesc}
591 %
592 \begin{funcdesc}{ReducedSolution}{domain}
593 returns the \ReducedSolutionFS on the \Domain domain. \Data objects in
594 this type of \Function are defined on the geometric region defined by
595 \var{domain} and are solutions of partial differential
596 equations\index{partial differential equation} with a reduced smoothness
597 for the solution approximation.
598 \end{funcdesc}
599
600 \subsection{The \Data Class}
601 \label{SEC ESCRIPT DATA}
602
603 The following table shows arithmetic operations that can be performed
604 point-wise on \Data objects:
605 \begin{center}
606 \begin{tabular}{l|l}
607 \textbf{Expression} & \textbf{Description}\\
608 \hline
609 \code{+arg} & identical to \var{arg}\index{+}\\
610 \code{-arg} & negation of \var{arg}\index{-}\\
611 \code{arg0+arg1} & adds \var{arg0} and \var{arg1}\index{+}\\
612 \code{arg0*arg1} & multiplies \var{arg0} and \var{arg1}\index{*}\\
613 \code{arg0-arg1} & subtracts \var{arg1} from \var{arg0}\index{-}\\
614 \code{arg0/arg1} & divides \var{arg0} by \var{arg1}\index{/}\\
615 \code{arg0**arg1} & raises \var{arg0} to the power of \var{arg1}\index{**}\\
616 \end{tabular}
617 \end{center}
618 At least one of the arguments \var{arg0} or \var{arg1} must be a \Data object.
619 Either of the arguments may be a \Data object, a \PYTHON number or a \numpy
620 object.
621 If \var{arg0} or \var{arg1} are not defined on the same \FunctionSpace, then
622 an attempt is made to convert \var{arg0} to the \FunctionSpace of \var{arg1}
623 or to convert \var{arg1} to the \FunctionSpace of \var{arg0}.
624 Both arguments must have the same \Shape or one of the arguments may be of
625 rank 0 (a constant).
626 The returned \Data object has the same \Shape and is defined on
627 the \DataSamplePoints as \var{arg0} or \var{arg1}.
628
629 The following table shows the update operations that can be applied to
630 \Data objects:
631 \begin{center}
632 \begin{tabular}{l|l}
633 \textbf{Expression} & \textbf{Description}\\
634 \hline
635 \code{arg0+=arg1} & adds \var{arg1} to \var{arg0}\index{+}\\
636 \code{arg0*=arg1} & multiplies \var{arg0} by \var{arg1}\index{*}\\
637 \code{arg0-=arg1} & subtracts \var{arg1} from\var{arg0}\index{-}\\
638 \code{arg0/=arg1} & divides \var{arg0} by \var{arg1}\index{/}\\
639 \code{arg0**=arg1} & raises \var{arg0} to the power of \var{arg1}\index{**}\\
640 \end{tabular}
641 \end{center}
642 \var{arg0} must be a \Data object. \var{arg1} must be a \Data object or an
643 object that can be converted into a \Data object.
644 \var{arg1} must have the same \Shape as \var{arg0} or have rank 0.
645 In the latter case it is assumed that the values of \var{arg1} are constant
646 for all components. \var{arg1} must be defined in the same \FunctionSpace as
647 \var{arg0} or it must be possible to interpolate \var{arg1} onto the
648 \FunctionSpace of \var{arg0}.
649
650 The \Data class supports taking slices as well as assigning new values to a
651 slice of an existing \Data object\index{slicing}.
652 The following expressions for taking and setting slices are valid:
653 \begin{center}
654 \begin{tabular}{l|ll}
655 \textbf{Rank of \var{arg}} & \textbf{Slicing expression} & \textbf{\Shape of returned and assigned object}\\
656 \hline
657 0 & no slicing & N/A\\
658 1 & \var{arg[l0:u0]} & (\var{u0}-\var{l0},)\\
659 2 & \var{arg[l0:u0,l1:u1]} & (\var{u0}-\var{l0},\var{u1}-\var{l1})\\
660 3 & \var{arg[l0:u0,l1:u1,l2:u2]} & (\var{u0}-\var{l0},\var{u1}-\var{l1},\var{u2}-\var{l2})\\
661 4 & \var{arg[l0:u0,l1:u1,l2:u2,l3:u3]} & (\var{u0}-\var{l0},\var{u1}-\var{l1},\var{u2}-\var{l2},\var{u3}-\var{l3})\\
662 \end{tabular}
663 \end{center}
664 Let \var{s} be the \Shape of \var{arg}, then
665 \begin{align*}
666 0 \le \var{l0} \le \var{u0} \le \var{s[0]},\\
667 0 \le \var{l1} \le \var{u1} \le \var{s[1]},\\
668 0 \le \var{l2} \le \var{u2} \le \var{s[2]},\\
669 0 \le \var{l3} \le \var{u3} \le \var{s[3]}.
670 \end{align*}
671 Any of the lower indexes \var{l0}, \var{l1}, \var{l2} and \var{l3} may not be
672 present in which case $0$ is assumed.
673 Any of the upper indexes \var{u0}, \var{u1}, \var{u2} and \var{u3} may be
674 omitted, in which case the upper limit for that dimension is assumed.
675 The lower and upper index may be identical in which case the column and the
676 lower or upper index may be dropped.
677 In the returned or in the object assigned to a slice, the corresponding
678 component is dropped, i.e. the rank is reduced by one in comparison to \var{arg}.
679 The following examples show slicing in action:
680 \begin{python}
681 t=Data(1., (4,4,6,6), Function(mydomain))
682 t[1,1,1,0]=9.
683 s=t[:2,:,2:6,5] # s has rank 3
684 s[:,:,1]=1.
685 t[:2,:2,5,5]=s[2:4,1,:2]
686 \end{python}
687
688
689 \subsection{Generation of \Data objects}
690 \begin{classdesc}{Data}{value=0, shape=(,), what=FunctionSpace(), expanded=\False}
691 creates a \Data object with \Shape \var{shape} in the \FunctionSpace \var{what}.
692 The values at all \DataSamplePoints are set to the double value \var{value}.
693 If \var{expanded} is \True the \Data object is represented in expanded form.
694 \end{classdesc}
695
696 \begin{classdesc}{Data}{value, what=FunctionSpace(), expanded=\False}
697 creates a \Data object in the \FunctionSpace \var{what}.
698 The value for each data sample point is set to \var{value}, which could be a
699 \numpy object, \Data object or a dictionary of \numpy or floating point
700 numbers. In the latter case the keys must be integers and are used as tags.
701 The \Shape of the returned object is equal to the \Shape of \var{value}.
702 If \var{expanded} is \True the \Data object is represented in expanded form.
703 \end{classdesc}
704
705 \begin{classdesc}{Data}{}
706 creates an \EmptyData object. The \EmptyData object is used to indicate that
707 an argument is not present where a \Data object is required.
708 \end{classdesc}
709
710 \begin{funcdesc}{Scalar}{value=0., what=FunctionSpace(), expanded=\False}
711 returns a \Data object of rank 0 (a constant) in the \FunctionSpace \var{what}.
712 Values are initialized with \var{value}, a double precision quantity.
713 If \var{expanded} is \True the \Data object is represented in expanded form.
714 \end{funcdesc}
715
716 \begin{funcdesc}{Vector}{value=0., what=FunctionSpace(), expanded=\False}
717 returns a \Data object of \Shape \var{(d,)} in the \FunctionSpace \var{what},
718 where \var{d} is the spatial dimension of the \Domain of \var{what}.
719 Values are initialized with \var{value}, a double precision quantity.
720 If \var{expanded} is \True the \Data object is represented in expanded form.
721 \end{funcdesc}
722
723 \begin{funcdesc}{Tensor}{value=0., what=FunctionSpace(), expanded=\False}
724 returns a \Data object of \Shape \var{(d,d)} in the \FunctionSpace \var{what},
725 where \var{d} is the spatial dimension of the \Domain of \var{what}.
726 Values are initialized with \var{value}, a double precision quantity.
727 If \var{expanded} is \True the \Data object is represented in expanded form.
728 \end{funcdesc}
729
730 \begin{funcdesc}{Tensor3}{value=0., what=FunctionSpace(), expanded=\False}
731 returns a \Data object of \Shape \var{(d,d,d)} in the \FunctionSpace \var{what},
732 where \var{d} is the spatial dimension of the \Domain of \var{what}.
733 Values are initialized with \var{value}, a double precision quantity.
734 If \var{expanded} is \True the \Data object is represented in expanded form.
735 \end{funcdesc}
736
737 \begin{funcdesc}{Tensor4}{value=0., what=FunctionSpace(), expanded=\False}
738 returns a \Data object of \Shape \var{(d,d,d,d)} in the \FunctionSpace \var{what},
739 where \var{d} is the spatial dimension of the \Domain of \var{what}.
740 Values are initialized with \var{value}, a double precision quantity.
741 If \var{expanded} is \True the \Data object is represented in expanded form.
742 \end{funcdesc}
743
744 \begin{funcdesc}{load}{filename, domain}
745 recovers a \Data object on \Domain \var{domain} from the file \var{filename},
746 which was created by \function{dump}.
747 \end{funcdesc}
748
749 \subsection{Generating random \Data objects}
750 A \Data object filled with random values can be produced using the
751 \function{RandomData} function.
752 By default values are drawn uniformly at random from the interval $[0,1]$ (i.e.
753 including end points).
754 The function takes a shape for the data points and a \FunctionSpace for the new
755 \Data as arguments.
756 For example:
757 \begin{python}
758 from esys.finley import *
759 from esys.escript import *
760
761 domain=Rectangle(11,11)
762 fs=ContinuousFunction(domain)
763 d=RandomData((), fs)
764 \end{python}
765 would result in \var{d} being filled with scalar random data since \texttt{()}
766 is an empty tuple.
767
768 \begin{python}
769 from esys.finley import *
770 from esys.escript import *
771
772 domain=Rectangle(11,11)
773 fs=ContinuousFunction(domain)
774 d=RandomData((2,2), fs)
775 \end{python}
776 would give \var{d} the same number of data points, but each point would be a
777 $2\times 2$ matrix instead of a scalar.
778
779 By default, the seed used to generate the random values will be different each
780 time.
781 If required, you can specify a seed to ensure the same sequence is produced.
782 \begin{python}
783 from esys.dudley import *
784 from esys.escript import *
785
786 seed=-17171717
787 domain=Brick(10,10,10)
788 fs=Function(domain)
789 d=RandomData((2,2), fs, seed)
790 \end{python}
791
792 The \var{seed} can be any integer value\footnote{which can be converted to a
793 C++ long} but 0 is special.
794 A seed of zero will cause \escript to use a different seed each time.
795 Also, note that the mechanism used to produce the random values could be
796 different in different releases.
797
798 \noindent\textbf{Note for MPI users:}
799 \textsl{
800 Even if you specify a seed, you will only get the same results if you are running with the same
801 number of ranks.
802 If you change the number of ranks, you will get different values for the same seed.
803 }
804
805 \subsubsection{Smoothed randoms}
806 The \ripley domains (see Chapter \ref{chap:ripley}) support generating random
807 scalars which are smoothed using Gaussian blur.
808 To use this, you need to supply the radius of the filter kernel (in elements)
809 and the \var{sigma} value used in the filter.
810 For example:
811 \begin{python}
812 from esys.ripley import *
813 from esys.escript import *
814
815 fs=ContinuousFunction(Rectangle(11,11, d1=2,d0=2))
816 d=RandomData((), fs, 0, ('gaussian', 1, 0.5))
817 \end{python}
818 will use a filter that uses the immediate neighbours of each point with a sigma
819 value of $0.5$.
820 The random values will be different each time this code is executed due to the
821 seed of $0$.
822
823 Ripley's Gaussian smoothing has the following requirements:
824 \begin{enumerate}
825 \item If \MPI is in use, then each rank must have at least $5$ elements in
826 it \emph{in each dimension}. This value increases as the radius of
827 the blur increases.
828 \item The data being generated must be scalar. (You can generate random
829 data objects for \ripley domains with whatever shape you require, you
830 just can't smooth them unless that shape is scalar).
831 \end{enumerate}
832 An exception will be raised if either of these requirements is not met.
833
834 The components of the matrix used in the kernal for the 2D case are
835 defined\cite{gaussfilter} by:
836
837 \[ G(x,y) = \frac{1}{2\pi\sigma^2} e^{-\frac{x^2+y^2}{2\sigma^2}} \]
838
839 \noindent For the 3D case, we use:
840
841 \[ G(x,y) = \frac{1}{(\sqrt{2\pi\sigma^2})^3} e^{-\frac{x^2+y^2+z^2}{2\sigma^2}} \]
842
843 All distances ($x$,$y$,$z$) refer to the number of points from the centre point.
844 That is, the closest neighbours have at least one distance of $1$, the next
845 ``ring'' of neighbours have at least one $2$ and so on.
846 The matrix is normalised before use.
847
848 \subsection{\Data methods}
849 These are the most frequently used methods of the \Data class.
850 A complete list of methods can be found in the reference guide,
851 see \ReferenceGuide.
852
853 \begin{methoddesc}[Data]{getFunctionSpace}{}
854 returns the \FunctionSpace of the object.
855 \end{methoddesc}
856
857 \begin{methoddesc}[Data]{getDomain}{}
858 returns the \Domain of the object.
859 \end{methoddesc}
860
861 \begin{methoddesc}[Data]{getShape}{}
862 returns the \Shape of the object as a \class{tuple} of integers.
863 \end{methoddesc}
864
865 \begin{methoddesc}[Data]{getRank}{}
866 returns the rank of the data on each data point\index{rank}.
867 \end{methoddesc}
868
869 \begin{methoddesc}[Data]{isEmpty}{}
870 returns \True if the \Data object is the \EmptyData object, \False otherwise.
871 Note that this is not the same as asking if the object contains no \DataSamplePoints.
872 \end{methoddesc}
873
874 \begin{methoddesc}[Data]{setTaggedValue}{tag_name, value}
875 assigns the \var{value} to all \DataSamplePoints which have the tag
876 assigned to \var{tag_name}. \var{value} must be an object of class
877 \class{numpy.ndarray} or must be convertible into a \class{numpy.ndarray} object.
878 \var{value} (or the corresponding \class{numpy.ndarray} object) must be of
879 rank $0$ or must have the same rank as the object.
880 If a value has already been defined for tag \var{tag_name} within the object
881 it is overwritten by the new \var{value}. If the object is expanded,
882 the value assigned to \DataSamplePoints with tag \var{tag_name} is replaced by
883 \var{value}. If no value is assigned the tag name \var{tag_name}, no value is set.
884 \end{methoddesc}
885
886 \begin{methoddesc}[Data]{dump}{filename}
887 dumps the \Data object to the file \var{filename}. The file stores the
888 function space but not the \Domain. It is the responsibility of the user to
889 save the \Domain in order to be able to recover the \Data object.
890 \end{methoddesc}
891
892 \begin{methoddesc}[Data]{__str__}{}
893 returns a string representation of the object.
894 \end{methoddesc}
895
896 \subsection{Functions of \Data objects}
897 This section lists the most important functions for \Data class objects.
898 A complete list and a more detailed description of the functionality can be
899 found on \ReferenceGuide.
900
901 \begin{funcdesc}{kronecker}{d}
902 returns a \RankTwo in \FunctionSpace \var{d} such that
903 \begin{equation}
904 \code{kronecker(d)}\left[ i,j\right] = \left\{
905 \begin{array}{l l}
906 1 & \quad \text{if $i=j$}\\
907 0 & \quad \text{otherwise}
908 \end{array}
909 \right.
910 \end{equation}
911 If \var{d} is an integer a $(d,d)$ \numpy array is returned.
912 \end{funcdesc}
913
914 \begin{funcdesc}{identityTensor}{d}
915 is a synonym for \code{kronecker} (see above).
916 \end{funcdesc}
917
918 \begin{funcdesc}{identityTensor4}{d}
919 returns a \RankFour in \FunctionSpace \var{d} such that
920 \begin{equation}
921 \code{identityTensor(d)}\left[ i,j,k,l\right] = \left\{
922 \begin{array}{l l}
923 1 & \quad \text{if $i=k$ and $j=l$}\\
924 0 & \quad \text{otherwise}
925 \end{array}
926 \right.
927 \end{equation}
928 If \var{d} is an integer a $(d,d,d,d)$ \numpy array is returned.
929 \end{funcdesc}
930
931 \begin{funcdesc}{unitVector}{i,d}
932 returns a \RankOne in \FunctionSpace \var{d} such that
933 \begin{equation}
934 \code{identityTensor(d)}\left[ j \right] = \left\{
935 \begin{array}{l l}
936 1 & \quad \text{if $j=i$}\\
937 0 & \quad \text{otherwise}
938 \end{array}
939 \right.
940 \end{equation}
941 If \var{d} is an integer a $(d,)$ \numpy array is returned.
942 \end{funcdesc}
943
944 \begin{funcdesc}{Lsup}{a}
945 returns the $L^{sup}$ norm of \var{arg}. This is the maximum of the absolute
946 values over all components and all \DataSamplePoints of \var{a}.
947 \end{funcdesc}
948
949 \begin{funcdesc}{sup}{a}
950 returns the maximum value over all components and all \DataSamplePoints of \var{a}.
951 \end{funcdesc}
952
953 \begin{funcdesc}{inf}{a}
954 returns the minimum value over all components and all \DataSamplePoints of \var{a}
955 \end{funcdesc}
956
957 \begin{funcdesc}{minval}{a}
958 returns at each data sample point the minimum value over all components.
959 \end{funcdesc}
960
961 \begin{funcdesc}{maxval}{a}
962 returns at each data sample point the maximum value over all components.
963 \end{funcdesc}
964
965 \begin{funcdesc}{length}{a}
966 returns the Euclidean norm at each data sample point.
967 For a \RankFour \var{a} this is
968 \begin{equation}
969 \code{length(a)}=\sqrt{\sum_{ijkl} \var{a} \left[i,j,k,l\right]^2}
970 \end{equation}
971 \end{funcdesc}
972
973 \begin{funcdesc}{trace}{a\optional{, axis_offset=0}}
974 returns the trace of \var{a}. This is the sum over components \var{axis_offset}
975 and \var{axis_offset+1} with the same index.
976 For instance, in the case of a \RankTwo this is
977 \begin{equation}
978 \code{trace(a)}=\sum_{i} \var{a} \left[i,i\right]
979 \end{equation}
980 and for a \RankFour and \code{axis_offset=1} this is
981 \begin{equation}
982 \code{trace(a,1)}\left[i,j\right]=\sum_{k} \var{a} \left[i,k,k,j\right]
983 \end{equation}
984 \end{funcdesc}
985
986 \begin{funcdesc}{transpose}{a\optional{, axis_offset=None}}
987 returns the transpose of \var{a}. This swaps the first \var{axis_offset}
988 components of \var{a} with the rest. If \var{axis_offset} is not
989 present \code{int(r/2)} is used where \var{r} is the rank of \var{a}.
990 For instance, in the case of a \RankTwo this is
991 \begin{equation}
992 \code{transpose(a)}\left[i,j\right]=\var{a} \left[j,i\right]
993 \end{equation}
994 and for a \RankFour and \code{axis_offset=1} this is
995 \begin{equation}
996 \code{transpose(a,1)}\left[i,j,k,l\right]=\var{a} \left[j,k,l,i\right]
997 \end{equation}
998 \end{funcdesc}
999
1000 \begin{funcdesc}{swap_axes}{a\optional{, axis0=0 \optional{, axis1=1 }}}
1001 returns \var{a} but with swapped components \var{axis0} and \var{axis1}.
1002 The argument \var{a} must be at least of rank 2. For instance, if \var{a}
1003 is a \RankFour, \code{axis0=1} and \code{axis1=2}, the result is
1004 \begin{equation}
1005 \code{swap_axes(a,1,2)}\left[i,j,k,l\right]=\var{a} \left[i,k,j,l\right]
1006 \end{equation}
1007 \end{funcdesc}
1008
1009 \begin{funcdesc}{symmetric}{a}
1010 returns the symmetric part of \var{a}. This is \code{(a+transpose(a))/2}.
1011 \end{funcdesc}
1012
1013 \begin{funcdesc}{nonsymmetric}{a}
1014 returns the non-symmetric part of \var{a}. This is \code{(a-transpose(a))/2}.
1015 \end{funcdesc}
1016
1017 \begin{funcdesc}{inverse}{a}
1018 return the inverse of \var{a} so that
1019 \begin{equation}
1020 \code{matrix_mult(inverse(a),a)=kronecker(d)}
1021 \end{equation}
1022 if \var{a} has shape \code{(d,d)}. The current implementation is restricted to
1023 arguments of shape \code{(2,2)} and \code{(3,3)}.
1024 \end{funcdesc}
1025
1026 \begin{funcdesc}{eigenvalues}{a}
1027 returns the eigenvalues of \var{a} so that
1028 \begin{equation}
1029 \code{matrix_mult(a,V)=e[i]*V}
1030 \end{equation}
1031 where \code{e=eigenvalues(a)} and \var{V} is a suitable non-zero vector.
1032 The eigenvalues are ordered in increasing size.
1033 The argument \var{a} has to be symmetric, i.e. \code{a=symmetric(a)}.
1034 The current implementation is restricted to arguments of shape \code{(2,2)}
1035 and \code{(3,3)}.
1036 \end{funcdesc}
1037
1038 \begin{funcdesc}{eigenvalues_and_eigenvectors}{a}
1039 returns the eigenvalues and eigenvectors of \var{a}.
1040 \begin{equation}
1041 \code{matrix_mult(a,V[:,i])=e[i]*V[:,i]}
1042 \end{equation}
1043 where \code{e,V=eigenvalues_and_eigenvectors(a)}. The eigenvectors \var{V} are
1044 orthogonal and normalized, i.e.
1045 \begin{equation}
1046 \code{matrix_mult(transpose(V),V)=kronecker(d)}
1047 \end{equation}
1048 if \var{a} has shape \code{(d,d)}. The eigenvalues are ordered in increasing
1049 size. The argument \var{a} has to be the symmetric, i.e. \code{a=symmetric(a)}.
1050 The current implementation is restricted to arguments of shape \code{(2,2)}
1051 and \code{(3,3)}.
1052 \end{funcdesc}
1053
1054 \begin{funcdesc}{maximum}{*a}
1055 returns the maximum value over all arguments at all \DataSamplePoints and for each component.
1056 \begin{equation}
1057 \code{maximum(a0,a1)}\left[i,j\right]=max(\var{a0} \left[i,j\right],\var{a1} \left[i,j\right])
1058 \end{equation}
1059 at all \DataSamplePoints.
1060 \end{funcdesc}
1061
1062 \begin{funcdesc}{minimum}{*a}
1063 returns the minimum value over all arguments at all \DataSamplePoints and for each component.
1064 \begin{equation}
1065 \code{minimum(a0,a1)}\left[i,j\right]=min(\var{a0} \left[i,j\right],\var{a1} \left[i,j\right])
1066 \end{equation}
1067 at all \DataSamplePoints.
1068 \end{funcdesc}
1069
1070 \begin{funcdesc}{clip}{a\optional{, minval=0.}\optional{, maxval=1.}}
1071 cuts back \var{a} into the range between \var{minval} and \var{maxval}.
1072 A value in the returned object equals \var{minval} if the corresponding value
1073 of \var{a} is less than \var{minval}, equals \var{maxval} if the corresponding
1074 value of \var{a} is greater than \var{maxval}, or corresponding value of
1075 \var{a} otherwise.
1076 \end{funcdesc}
1077
1078 \begin{funcdesc}{inner}{a0, a1}
1079 returns the inner product of \var{a0} and \var{a1}. For instance in the
1080 case of a \RankTwo:
1081 \begin{equation}
1082 \code{inner(a)}=\sum_{ij}\var{a0} \left[j,i\right] \cdot \var{a1} \left[j,i\right]
1083 \end{equation}
1084 and for a \RankFour:
1085 \begin{equation}
1086 \code{inner(a)}=\sum_{ijkl}\var{a0} \left[i,j,k,l\right] \cdot \var{a1} \left[j,i,k,l\right]
1087 \end{equation}
1088 \end{funcdesc}
1089
1090 \begin{funcdesc}{matrix_mult}{a0, a1}
1091 returns the matrix product of \var{a0} and \var{a1}.
1092 If \var{a1} is a \RankOne this is
1093 \begin{equation}
1094 \code{matrix_mult(a)}\left[i\right]=\sum_{k}\var{a0} \cdot \left[i,k\right]\var{a1} \left[k\right]
1095 \end{equation}
1096 and if \var{a1} is a \RankTwo this is
1097 \begin{equation}
1098 \code{matrix_mult(a)}\left[i,j\right]=\sum_{k}\var{a0} \cdot \left[i,k\right]\var{a1} \left[k,j\right]
1099 \end{equation}
1100 \end{funcdesc}
1101
1102 \begin{funcdesc}{transposed_matrix_mult}{a0, a1}
1103 returns the matrix product of the transposed of \var{a0} and \var{a1}.
1104 The function is equivalent to \code{matrix_mult(transpose(a0),a1)}.
1105 If \var{a1} is a \RankOne this is
1106 \begin{equation}
1107 \code{transposed_matrix_mult(a)}\left[i\right]=\sum_{k}\var{a0} \cdot \left[k,i\right]\var{a1} \left[k\right]
1108 \end{equation}
1109 and if \var{a1} is a \RankTwo this is
1110 \begin{equation}
1111 \code{transposed_matrix_mult(a)}\left[i,j\right]=\sum_{k}\var{a0} \cdot \left[k,i\right]\var{a1} \left[k,j\right]
1112 \end{equation}
1113 \end{funcdesc}
1114
1115 \begin{funcdesc}{matrix_transposed_mult}{a0, a1}
1116 returns the matrix product of \var{a0} and the transposed of \var{a1}.
1117 The function is equivalent to \code{matrix_mult(a0,transpose(a1))}.
1118 If \var{a1} is a \RankTwo this is
1119 \begin{equation}
1120 \code{matrix_transposed_mult(a)}\left[i,j\right]=\sum_{k}\var{a0} \cdot \left[i,k\right]\var{a1} \left[j,k\right]
1121 \end{equation}
1122 \end{funcdesc}
1123
1124 \begin{funcdesc}{outer}{a0, a1}
1125 returns the outer product of \var{a0} and \var{a1}.
1126 For instance, if both, \var{a0} and \var{a1} is a \RankOne then
1127 \begin{equation}
1128 \code{outer(a)}\left[i,j\right]=\var{a0} \left[i\right] \cdot \var{a1}\left[j\right]
1129 \end{equation}
1130 and if \var{a0} is a \RankOne and \var{a1} is a \RankThree:
1131 \begin{equation}
1132 \code{outer(a)}\left[i,j,k\right]=\var{a0} \left[i\right] \cdot \var{a1}\left[j,k\right]
1133 \end{equation}
1134 \end{funcdesc}
1135
1136 \begin{funcdesc}{tensor_mult}{a0, a1}
1137 returns the tensor product of \var{a0} and \var{a1}.
1138 If \var{a1} is a \RankTwo this is
1139 \begin{equation}
1140 \code{tensor_mult(a)}\left[i,j\right]=\sum_{kl}\var{a0}\left[i,j,k,l\right] \cdot \var{a1} \left[k,l\right]
1141 \end{equation}
1142 and if \var{a1} is a \RankFour this is
1143 \begin{equation}
1144 \code{tensor_mult(a)}\left[i,j,k,l\right]=\sum_{mn}\var{a0} \left[i,j,m,n\right] \cdot \var{a1} \left[m,n,k,l\right]
1145 \end{equation}
1146 \end{funcdesc}
1147
1148 \begin{funcdesc}{transposed_tensor_mult}{a0, a1}
1149 returns the tensor product of the transposed of \var{a0} and \var{a1}.
1150 The function is equivalent to \code{tensor_mult(transpose(a0),a1)}.
1151 If \var{a1} is a \RankTwo this is
1152 \begin{equation}
1153 \code{transposed_tensor_mult(a)}\left[i,j\right]=\sum_{kl}\var{a0}\left[k,l,i,j\right] \cdot \var{a1} \left[k,l\right]
1154 \end{equation}
1155 and if \var{a1} is a \RankFour this is
1156 \begin{equation}
1157 \code{transposed_tensor_mult(a)}\left[i,j,k,l\right]=\sum_{mn}\var{a0} \left[m,n,i,j\right] \cdot \var{a1} \left[m,n,k,l\right]
1158 \end{equation}
1159 \end{funcdesc}
1160
1161 \begin{funcdesc}{tensor_transposed_mult}{a0, a1}
1162 returns the tensor product of \var{a0} and the transposed of \var{a1}.
1163 The function is equivalent to \code{tensor_mult(a0,transpose(a1))}.
1164 If \var{a1} is a \RankTwo this is
1165 \begin{equation}
1166 \code{tensor_transposed_mult(a)}\left[i,j\right]=\sum_{kl}\var{a0}\left[i,j,k,l\right] \cdot \var{a1} \left[l,k\right]
1167 \end{equation}
1168 and if \var{a1} is a \RankFour this is
1169 \begin{equation}
1170 \code{tensor_transposed_mult(a)}\left[i,j,k,l\right]=\sum_{mn}\var{a0} \left[i,j,m,n\right] \cdot \var{a1} \left[k,l,m,n\right]
1171 \end{equation}
1172 \end{funcdesc}
1173
1174 \begin{funcdesc}{grad}{a\optional{, where=None}}
1175 returns the gradient of \var{a}. If \var{where} is present the gradient will
1176 be calculated in the \FunctionSpace \var{where}, otherwise a default
1177 \FunctionSpace is used. In case that \var{a} is a \RankTwo one has
1178 \begin{equation}
1179 \code{grad(a)}\left[i,j,k\right]=\frac{\partial \var{a} \left[i,j\right]}{\partial x_{k}}
1180 \end{equation}
1181 \end{funcdesc}
1182
1183 \begin{funcdesc}{integrate}{a\optional{, where=None}}
1184 returns the integral of \var{a} where the domain of integration is defined by
1185 the \FunctionSpace of \var{a}. If \var{where} is present the argument is
1186 interpolated into \FunctionSpace \var{where} before integration.
1187 For instance in the case of a \RankTwo in \ContinuousFunction it is
1188 \begin{equation}
1189 \code{integrate(a)}\left[i,j\right]=\int_{\Omega}\var{a} \left[i,j\right] \; d\Omega
1190 \end{equation}
1191 where $\Omega$ is the spatial domain and $d\Omega$ volume integration.
1192 To integrate over the boundary of the domain one uses
1193 \begin{equation}
1194 \code{integrate(a,where=FunctionOnBoundary(a.getDomain))}\left[i,j\right]=\int_{\partial \Omega} a\left[i,j\right] \; ds
1195 \end{equation}
1196 where $\partial \Omega$ is the surface of the spatial domain and $ds$ area or
1197 line integration.
1198 \end{funcdesc}
1199
1200 \begin{funcdesc}{interpolate}{a, where}
1201 interpolates argument \var{a} into the \FunctionSpace \var{where}.
1202 \end{funcdesc}
1203
1204 \begin{funcdesc}{div}{a\optional{, where=None}}
1205 returns the divergence of \var{a}:
1206 \begin{equation}
1207 \code{div(a)=trace(grad(a),where)}
1208 \end{equation}
1209 \end{funcdesc}
1210
1211 \begin{funcdesc}{jump}{a\optional{, domain=None}}
1212 returns the jump of \var{a} over the discontinuity in its domain or if
1213 \Domain \var{domain} is present in \var{domain}.
1214 \begin{equation}
1215 \begin{array}{rcl}
1216 \code{jump(a)}& = &\code{interpolate(a,FunctionOnContactOne(domain))} \\
1217 & & \hfill - \code{interpolate(a,FunctionOnContactZero(domain))}
1218 \end{array}
1219 \end{equation}
1220 \end{funcdesc}
1221
1222 \begin{funcdesc}{L2}{a}
1223 returns the $L^2$-norm of \var{a} in its \FunctionSpace. This is
1224 \begin{equation}
1225 \code{L2(a)=integrate(length(a)}^2\code{)} \; .
1226 \end{equation}
1227 \end{funcdesc}
1228
1229 \noindent The following functions operate ``point-wise''.
1230 That is, the operation is applied to each component of each point individually.
1231
1232 \begin{funcdesc}{sin}{a}
1233 applies the sine function to \var{a}.
1234 \end{funcdesc}
1235
1236 \begin{funcdesc}{cos}{a}
1237 applies the cosine function to \var{a}.
1238 \end{funcdesc}
1239
1240 \begin{funcdesc}{tan}{a}
1241 applies the tangent function to \var{a}.
1242 \end{funcdesc}
1243
1244 \begin{funcdesc}{asin}{a}
1245 applies the arc (inverse) sine function to \var{a}.
1246 \end{funcdesc}
1247
1248 \begin{funcdesc}{acos}{a}
1249 applies the arc (inverse) cosine function to \var{a}.
1250 \end{funcdesc}
1251
1252 \begin{funcdesc}{atan}{a}
1253 applies the arc (inverse) tangent function to \var{a}.
1254 \end{funcdesc}
1255
1256 \begin{funcdesc}{sinh}{a}
1257 applies the hyperbolic sine function to \var{a}.
1258 \end{funcdesc}
1259
1260 \begin{funcdesc}{cosh}{a}
1261 applies the hyperbolic cosine function to \var{a}.
1262 \end{funcdesc}
1263
1264 \begin{funcdesc}{tanh}{a}
1265 applies the hyperbolic tangent function to \var{a}.
1266 \end{funcdesc}
1267
1268 \begin{funcdesc}{asinh}{a}
1269 applies the arc (inverse) hyperbolic sine function to \var{a}.
1270 \end{funcdesc}
1271
1272 \begin{funcdesc}{acosh}{a}
1273 applies the arc (inverse) hyperbolic cosine function to \var{a}.
1274 \end{funcdesc}
1275
1276 \begin{funcdesc}{atanh}{a}
1277 applies the arc (inverse) hyperbolic tangent function to \var{a}.
1278 \end{funcdesc}
1279
1280 \begin{funcdesc}{exp}{a}
1281 applies the exponential function to \var{a}.
1282 \end{funcdesc}
1283
1284 \begin{funcdesc}{sqrt}{a}
1285 applies the square root function to \var{a}.
1286 \end{funcdesc}
1287
1288 \begin{funcdesc}{log}{a}
1289 takes the natural logarithm of \var{a}.
1290 \end{funcdesc}
1291
1292 \begin{funcdesc}{log10}{a}
1293 takes the base-$10$ logarithm of \var{a}.
1294 \end{funcdesc}
1295
1296 \begin{funcdesc}{sign}{a}
1297 applies the sign function to \var{a}. The result is $1$ where \var{a} is
1298 positive, $-1$ where \var{a} is negative, and $0$ otherwise.
1299 \end{funcdesc}
1300
1301 \begin{funcdesc}{wherePositive}{a}
1302 returns a function which is $1$ where \var{a} is positive and $0$ otherwise.
1303 \end{funcdesc}
1304
1305 \begin{funcdesc}{whereNegative}{a}
1306 returns a function which is $1$ where \var{a} is negative and $0$ otherwise.
1307 \end{funcdesc}
1308
1309 \begin{funcdesc}{whereNonNegative}{a}
1310 returns a function which is $1$ where \var{a} is non-negative and $0$ otherwise.
1311 \end{funcdesc}
1312
1313 \begin{funcdesc}{whereNonPositive}{a}
1314 returns a function which is $1$ where \var{a} is non-positive and $0$ otherwise.
1315 \end{funcdesc}
1316
1317 \begin{funcdesc}{whereZero}{a\optional{, tol=None\optional{, rtol=1.e-8}}}
1318 returns a function which is $1$ where \var{a} equals zero with tolerance
1319 \var{tol} and $0$ otherwise. If \var{tol} is not present, the absolute maximum
1320 value of \var{a} times \var{rtol} is used.
1321 \end{funcdesc}
1322
1323 \begin{funcdesc}{whereNonZero}{a\optional{, tol=None\optional{, rtol=1.e-8}}}
1324 returns a function which is $1$ where \var{a} is non-zero with tolerance
1325 \var{tol} and $0$ otherwise. If \var{tol} is not present, the absolute maximum
1326 value of \var{a} times \var{rtol} is used.
1327 \end{funcdesc}
1328
1329 \subsection{Interpolating Data}
1330 \index{interpolateTable}
1331 \label{sec:interpolation}
1332 In some cases, it may be useful to produce Data objects which fit some user
1333 defined function.
1334 Manually modifying each value in the Data object is not a good idea since it
1335 depends on knowing the location and order of each data point in the domain.
1336 Instead, \escript can use an interpolation table to produce a \Data object.
1337
1338 The following example is available as \file{int_save.py} in the \ExampleDirectory.
1339 We will produce a \Data object which approximates a sine curve.
1340
1341 \begin{python}
1342 from esys.escript import saveDataCSV, sup, interpolateTable
1343 import numpy
1344 from esys.finley import Rectangle
1345
1346 n=4
1347 r=Rectangle(n,n)
1348 x=r.getX()
1349 toobig=100
1350 \end{python}
1351
1352 \noindent First we produce an interpolation table:
1353 \begin{python}
1354 sine_table=[0, 0.70710678118654746, 1, 0.70710678118654746, 0,
1355 -0.70710678118654746, -1, -0.70710678118654746, 0]
1356 \end{python}
1357 %
1358 We wish to identify $0$ and $1$ with the ends of the curve, that is
1359 with the first and eighth value in the table.
1360
1361 \begin{python}
1362 numslices=len(sine_table)-1
1363 minval=0.
1364 maxval=1.
1365 step=sup(maxval-minval)/numslices
1366 \end{python}
1367 %
1368 So the values $v$ from the input lie in the interval
1369 \var{minval} $\leq v <$ \var{maxval}.
1370 \var{step} represents the gap (in the input range) between entries in the table.
1371 By default, values of $v$ outside the table argument range (minval, maxval)
1372 will be pushed back into the range, i.e. if $v <$ \var{minval} the value
1373 \var{minval} will be used to evaluate the table.
1374 Similarly, for values $v>$ \var{maxval} the value \var{maxval} is used.
1375
1376 Now we produce our new \Data object:
1377
1378 \begin{python}
1379 result=interpolateTable(sine_table, x[0], minval, step, toobig)
1380 \end{python}
1381 Any values which interpolate to larger than \var{toobig} will raise an
1382 exception. You can switch on boundary checking by adding
1383 \code{check_boundaries=True} to the argument list.
1384
1385 Now consider a 2D example. We will interpolate from a plane where $\forall x,y\in[0,9]:(x,y)=x+y\cdot10$.
1386
1387 \begin{python}
1388 from esys.escript import whereZero
1389 table2=[]
1390 for y in range(0,10):
1391 r=[]
1392 for x in range(0,10):
1393 r.append(x+y*10)
1394 table2.append(r)
1395 xstep=(maxval-minval)/(10-1)
1396 ystep=(maxval-minval)/(10-1)
1397
1398 xmin=minval
1399 ymin=minval
1400
1401 result2=interpolateTable(table2, x2, (xmin, ymin), (xstep, ystep), toobig)
1402 \end{python}
1403
1404 We can check the values using \function{whereZero}.
1405 For example, for $x=0$:
1406 \begin{python}
1407 print(result2*whereZero(x[0]))
1408 \end{python}
1409
1410 Finally let us look at a 3D example. Note that the parameter tuples should be
1411 $(x,y,z)$ but that in the interpolation table, $x$ is the innermost dimension.
1412 \begin{python}
1413 b=Brick(n,n,n)
1414 x3=b.getX()
1415 toobig=1000000
1416
1417 table3=[]
1418 for z in range(0,10):
1419 face=[]
1420 for y in range(0,10):
1421 r=[]
1422 for x in range(0,10):
1423 r.append(x+y*10+z*100)
1424 face.append(r)
1425 table3.append(face);
1426
1427 zstep=(maxval-minval)/(10-1)
1428
1429 zmin=minval
1430
1431 result3=interpolateTable(table3, x3, (xmin, ymin, zmin), (xstep, ystep, zstep), toobig)
1432 \end{python}
1433
1434
1435 \subsubsection{Non-uniform Interpolation}
1436 Non-uniform interpolation is also supported for the one dimensional case.
1437 \begin{python}
1438 Data.nonuniformInterpolate(in, out, check_boundaries)
1439 Data.nonuniformSlope(in, out, check_boundaries)
1440 \end{python}
1441
1442 Will produce a new \Data object by mapping the given \Data object through the user-defined function
1443 specified by \texttt{in} and \texttt{out}.
1444 The \ldots Interpolate version gives the value of the function at the specified point and the
1445 \ldots Slope version gives the slope at those points.
1446 The check_boundaries boolean argument specifies what the function should do if the \Data object contains
1447 values outside the range specified by the \texttt{in} parameter.
1448 If the argument is \texttt{False}, then those datapoints will be interpolated to the value of the edge
1449 they are closest to (or assigned a slope of zero).
1450 If the argument is \texttt{True}, then an exception will be thrown if out of bounds values are detected.
1451 Note that the values given by the \texttt{in} parameter must be monotonically increasing.
1452
1453 \noindent For example:\\
1454 If \texttt{d} contains the values \texttt{\{1,2,3,4,5\}}, then
1455 \begin{python}
1456 d.nonuniformInterpolate([1.5, 2, 2.8, 4.6], [4, 5, -1, 1], False)
1457 \end{python}
1458 would produce a \Data object containing \texttt{\{4, 5, -0.7777, 0.3333, 1\}}.\\
1459 A similar call to \texttt{nonuniformSlope} would produce a \Data object containing \texttt{\{0, 2, 1.1111, 1.1111, 0\}}.
1460 %
1461 %
1462 % We will interpolate a surface such that the bottom
1463 % edge is the sine curve described above.
1464 % The amplitude of the curve decreases as we move towards the top edge.
1465 % Our interpolation table will have three rows:
1466 %
1467 % \begin{python}
1468 % st=numpy.array(sine_table)
1469 % table=[st, 0.5*st, 0*st]
1470 % \end{python}
1471 % %
1472 % The use of \numpy and multiplication here is just to save typing.
1473 %
1474 % % result2=x1.interpolateTable(table, 0, 0.55, x0, minval, step, toobig)
1475 % \begin{python}
1476 % result=interpolateTable(table, x (minval,0), (0.55, step), toobig)
1477 % \end{python}
1478 %
1479 % In the 2D case the start and step parameters are tuples $(x,y)$.
1480 % By default, if a point is specified which is outside the boundary, then
1481 % \var{interpolateTable} will operate as if the point was on the boundary.
1482 % Passing \code{check_boundaries=True} will lead to the rejection of any points
1483 % outside the boundaries by \var{interpolateTable}.
1484 %
1485 % This method can also be called with three dimensional tables and \Data objects.
1486 % Tuples should be ordered $(x,y,z)$.
1487
1488 \subsection{The \var{DataManager} Class}
1489 \label{sec:datamanager}
1490
1491 The \var{DataManager} class can be used to conveniently add checkpoint/restart
1492 functionality to \escript simulations.
1493 Once an instance is created \Data objects and other values can be added and
1494 dumped to disk by a single method call.
1495 If required the object can be set up to also save the data in a format suitable
1496 for visualization.
1497 Internally the \var{DataManager} interfaces with \weipa for this.
1498
1499 \begin{classdesc}{DataManager}{formats=[RESTART], work_dir=".", restart_prefix="restart", do_restart=\True}
1500 initializes a new \var{DataManager} object which can be used to save,
1501 restore and export simulation data in a number of formats.
1502 All files and directories saved or restored by this object are located
1503 under the directory specified by \var{work_dir}.
1504 If \var{RESTART} is specified in \var{formats}, the \var{DataManager} will
1505 look for directories whose name starts with \var{restart_prefix}.
1506 In case \var{do_restart} is \True, the last of these directories is used
1507 to restore simulation data while all others are deleted.
1508 If \var{do_restart} is \False, then all of those directories are deleted.
1509 The \var{restart_prefix} and \var{do_restart} parameters are ignored if
1510 \var{RESTART} is not specified in \var{formats}.
1511 \end{classdesc}
1512
1513 \noindent Valid values for the \var{formats} parameter are:
1514 \begin{memberdesc}[DataManager]{RESTART}
1515 enables writing of checkpoint files to be able to continue simulations
1516 as explained in the class description.
1517 \end{memberdesc}
1518 \begin{memberdesc}[DataManager]{SILO}
1519 exports simulation data in the \SILO file format. \escript must have
1520 been compiled with \SILO support for this to work.
1521 \end{memberdesc}
1522 \begin{memberdesc}[DataManager]{VISIT}
1523 enables the \VisIt simulation interface which allows connecting to and
1524 interacting with the running simulation from a compatible \VisIt client.
1525 \escript must have been compiled with \VisIt (version 2) support and the
1526 version of the client has to match the version used at compile time.
1527 In order to connect to the simulation the client needs to have access and
1528 load the file \file{escriptsim.sim2} located under the work directory.
1529 \end{memberdesc}
1530 \begin{memberdesc}[DataManager]{VTK}
1531 exports simulation data in the \VTK file format.
1532 \end{memberdesc}
1533
1534 \noindent The \var{DataManager} class has the following methods:
1535 \begin{methoddesc}[DataManager]{addData}{**data}
1536 adds \Data objects and other data to the manager. Calling this method does
1537 not save or export the data yet so it is allowed to incrementally add data
1538 at various points in the simulation script if required.
1539 Note, that only a single domain is supported so all \Data objects have to
1540 be defined on the same one or an exception is raised.
1541 \end{methoddesc}
1542
1543 \begin{methoddesc}[DataManager]{setDomain}{domain}
1544 explicitly sets the domain for this manager.
1545 It is generally not required to call this method directly.
1546 Instead, the \var{addData} method will set the domain used by the \Data
1547 objects.
1548 An exception is raised if the domain was set to a different domain before
1549 (explicitly or implicitly).
1550 \end{methoddesc}
1551
1552 \begin{methoddesc}[DataManager]{hasData}{}
1553 returns \True if the manager has loaded simulation data for a restart.
1554 \end{methoddesc}
1555
1556 \begin{methoddesc}[DataManager]{getDomain}{}
1557 returns the domain as recovered from a restart.
1558 \end{methoddesc}
1559
1560 \begin{methoddesc}[DataManager]{getValue}{value_name}
1561 returns a \Data object or other value with the name \var{value_name} that
1562 has been recovered after a restart.
1563 \end{methoddesc}
1564
1565 \begin{methoddesc}[DataManager]{getCycle}{}
1566 returns the export cycle, i.e. the number of times \var{export()} has been
1567 called.
1568 \end{methoddesc}
1569
1570 \begin{methoddesc}[DataManager]{setCheckpointFrequency}{freq}
1571 sets the frequency with which checkpoint files are created. This is only
1572 useful if the \var{DataManager} object was created with at least one other
1573 format next to \var{RESTART}. The frequency is 1 by default which means
1574 that checkpoint files are created every time \var{export()} is called.
1575 Unlike visualization output, a simulation checkpoint is usually not
1576 required at every time step. Thus, the frequency can be decreased by
1577 calling this method with $\var{freq}>1$ which would then create restart
1578 files every \var{freq} times \var{export()} is called.
1579 \end{methoddesc}
1580
1581 \begin{methoddesc}[DataManager]{setTime}{time}
1582 sets the simulation time stamp. This floating point number is stored in
1583 the metadata of exported data but not used by \var{RESTART}.
1584 \end{methoddesc}
1585
1586 \begin{methoddesc}[DataManager]{setMeshLabels}{x, y, z=""}
1587 sets labels for the mesh axes. These are currently only used by the \SILO
1588 exporter.
1589 \end{methoddesc}
1590
1591 \begin{methoddesc}[DataManager]{setMeshUnits}{x, y, z=""}
1592 sets units for the mesh axes. These are currently only used by the \SILO
1593 exporter.
1594 \end{methoddesc}
1595
1596 \begin{methoddesc}[DataManager]{setMetadataSchemaString}{schema, metadata=""}
1597 sets metadata namespaces and the corresponding metadata. These are
1598 currently only used by the \VTK exporter.
1599 \var{schema} is a dictionary that maps prefixes to namespace names, e.g.\\
1600 \code{\{"gml": "http://www.opengis.net/gml"\}} and \var{metadata} is a
1601 string with the actual content which will be enclosed in \var{<MetaData>}
1602 tags.
1603 \end{methoddesc}
1604
1605 \begin{methoddesc}[DataManager]{export}{}
1606 executes the actual data export. Depending on the \var{formats} parameter
1607 used in the constructor all data added by \var{addData()} is written to
1608 disk (\var{RESTART,SILO,VTK}) or made available through the \VisIt
1609 simulation interface (\var{VISIT}).
1610 At least the domain must be set for something to be exported.
1611 \end{methoddesc}
1612
1613 \subsection{Saving Data as CSV}
1614 \label{sec:savedatacsv}
1615 \index{saveDataCSV}\index{CSV}
1616 For simple post-processing, \Data objects can be saved in comma separated
1617 value (\emph{CSV}) format.
1618 If \var{mydata1} and \var{mydata2} are scalar data, the command
1619 \begin{python}
1620 saveDataCSV('output.csv', U=mydata1, V=mydata2)
1621 \end{python}
1622 will record the values in \file{output.csv} in the following format:
1623 \begin{verbatim}
1624 U, V
1625 1.0000000e+0, 2.0000000e-1
1626 5.0000000e-0, 1.0000000e+1
1627 ...
1628 \end{verbatim}
1629
1630 The names of the keyword parameters form the names of columns in the output.
1631 If the data objects are over different function spaces, then \var{saveDataCSV}
1632 will attempt to interpolate to a common function space.
1633 If this is not possible, then an exception is raised.
1634
1635 Output can be restricted using a scalar mask as follows:
1636 \begin{python}
1637 saveDataCSV('outfile.csv', U=mydata1, V=mydata2, mask=myscalar)
1638 \end{python}
1639 This command will only output those rows which correspond to to positive
1640 values of \var{myscalar}.
1641 Some aspects of the output can be tuned using additional parameters:
1642 \begin{python}
1643 saveDataCSV('data.csv', append=True, sep=' ', csep='/', mask=mymask, e=mat1)
1644 \end{python}
1645
1646 \begin{itemize}
1647 \item \var{append} -- specifies that the output should be written to the end of an existing file
1648 \item \var{sep} -- defines the separator between fields
1649 \item \var{csep} -- defines the separator between components in the header
1650 line. For example between the components of a matrix.
1651 \end{itemize}
1652 %
1653 The above command would produce output like this:
1654 \begin{verbatim}
1655 e/0/0 e/1/0 e/0/1 e/1/1
1656 1.0000000000e+00 2.0000000000e+00 3.0000000000e+00 4.0000000000e+00
1657 ...
1658 \end{verbatim}
1659
1660 Note that while the order in which rows are output can vary, all the elements
1661 in a given row always correspond to the same input.
1662 When run on more than one \MPI rank, \function{saveDataCSV} is currently
1663 limited to certain domain and function space combinations throwing an exception
1664 in other cases. Writing data on \ContinuousFunction is always supported.
1665
1666 \subsection{The \Operator Class}
1667 The \Operator class provides an abstract access to operators built
1668 within the \LinearPDE class. \Operator objects are created
1669 when a PDE is handed over to a PDE solver library and handled
1670 by the \LinearPDE object defining the PDE. The user can gain access
1671 to the \Operator of a \LinearPDE object through the \var{getOperator}
1672 method.
1673
1674 \begin{classdesc}{Operator}{}
1675 creates an empty \Operator object.
1676 \end{classdesc}
1677
1678 \begin{methoddesc}[Operator]{isEmpty}{fileName}
1679 returns \True is the object is empty, \False otherwise.
1680 \end{methoddesc}
1681
1682 \begin{methoddesc}[Operator]{resetValues}{}
1683 resets all entries in the operator.
1684 \end{methoddesc}
1685
1686 \begin{methoddesc}[Operator]{solve}{rhs}
1687 returns the solution \var{u} of: operator * \var{u} = \var{rhs}.
1688 \end{methoddesc}
1689
1690 \begin{methoddesc}[Operator]{of}{u}
1691 applies the operator to the \Data object \var{u}, i.e. performs a matrix-vector
1692 multiplication.
1693 \end{methoddesc}
1694
1695 \begin{methoddesc}[Operator]{saveMM}{fileName}\index{Matrix Market}
1696 saves the object to a Matrix Market format file with name \var{fileName}, see
1697 \url{http://math.nist.gov/MatrixMarket}
1698 \end{methoddesc}
1699
1700 \section{Physical Units}
1701 \escript provides support for physical units in the SI system\index{SI units}
1702 including unit conversion. So the user can define variables in the form
1703 \begin{python}
1704 from esys.escript.unitsSI import *
1705 l=20*m
1706 w=30*kg
1707 w2=40*lb
1708 T=100*Celsius
1709 \end{python}
1710 In the two latter cases a conversion from pounds\index{pounds} and degrees
1711 Celsius\index{Celsius} is performed into the appropriate SI units \emph{kg}
1712 and \emph{Kelvin}.
1713 In addition, composed units can be used, for instance
1714 \begin{python}
1715 from esys.escript.unitsSI import *
1716 rho=40*lb/cm**3
1717 \end{python}
1718 defines the density in the units of pounds per cubic centimeter.
1719 The value $40$ will be converted into SI units, in this case kg per cubic
1720 meter. Moreover unit prefixes are supported:
1721 \begin{python}
1722 from esys.escript.unitsSI import *
1723 p=40*Mega*Pa
1724 \end{python}
1725 The pressure \var{p} is set to 40 Mega Pascal. Units can also be converted
1726 back from the SI system into a desired unit, e.g.
1727 \begin{python}
1728 from esys.escript.unitsSI import *
1729 print(p/atm)
1730 \end{python}
1731 can be used print the pressure in units of atmosphere\index{atmosphere}.
1732
1733 The following is an incomplete list of supported physical units:
1734
1735 \begin{datadesc}{km}
1736 unit of kilometer
1737 \end{datadesc}
1738
1739 \begin{datadesc}{m}
1740 unit of meter
1741 \end{datadesc}
1742
1743 \begin{datadesc}{cm}
1744 unit of centimeter
1745 \end{datadesc}
1746
1747 \begin{datadesc}{mm}
1748 unit of millimeter
1749 \end{datadesc}
1750
1751 \begin{datadesc}{sec}
1752 unit of second
1753 \end{datadesc}
1754
1755 \begin{datadesc}{minute}
1756 unit of minute
1757 \end{datadesc}
1758
1759 \begin{datadesc}{h}
1760 unit of hour
1761 \end{datadesc}
1762
1763 \begin{datadesc}{day}
1764 unit of day
1765 \end{datadesc}
1766
1767 \begin{datadesc}{yr}
1768 unit of year
1769 \end{datadesc}
1770
1771 \begin{datadesc}{gram}
1772 unit of gram
1773 \end{datadesc}
1774
1775 \begin{datadesc}{kg}
1776 unit of kilogram
1777 \end{datadesc}
1778
1779 \begin{datadesc}{lb}
1780 unit of pound
1781 \end{datadesc}
1782
1783 \begin{datadesc}{ton}
1784 metric ton
1785 \end{datadesc}
1786
1787 \begin{datadesc}{A}
1788 unit of Ampere
1789 \end{datadesc}
1790
1791 \begin{datadesc}{Hz}
1792 unit of Hertz
1793 \end{datadesc}
1794
1795 \begin{datadesc}{N}
1796 unit of Newton
1797 \end{datadesc}
1798
1799 \begin{datadesc}{Pa}
1800 unit of Pascal
1801 \end{datadesc}
1802
1803 \begin{datadesc}{atm}
1804 unit of atmosphere
1805 \end{datadesc}
1806
1807 \begin{datadesc}{J}
1808 unit of Joule
1809 \end{datadesc}
1810
1811 \begin{datadesc}{W}
1812 unit of Watt
1813 \end{datadesc}
1814
1815 \begin{datadesc}{C}
1816 unit of Coulomb
1817 \end{datadesc}
1818
1819 \begin{datadesc}{V}
1820 unit of Volt
1821 \end{datadesc}
1822
1823 \begin{datadesc}{F}
1824 unit of Farad
1825 \end{datadesc}
1826
1827 \begin{datadesc}{Ohm}
1828 unit of Ohm
1829 \end{datadesc}
1830
1831 \begin{datadesc}{K}
1832 unit of degrees Kelvin
1833 \end{datadesc}
1834
1835 \begin{datadesc}{Celsius}
1836 unit of degrees Celsius
1837 \end{datadesc}
1838
1839 \begin{datadesc}{Fahrenheit}
1840 unit of degrees Fahrenheit
1841 \end{datadesc}
1842
1843 \noindent Supported unit prefixes:
1844
1845 \begin{datadesc}{Yotta}
1846 prefix yotta = $10^{24}$
1847 \end{datadesc}
1848
1849 \begin{datadesc}{Zetta}
1850 prefix zetta = $10^{21}$
1851 \end{datadesc}
1852
1853 \begin{datadesc}{Exa}
1854 prefix exa = $10^{18}$
1855 \end{datadesc}
1856
1857 \begin{datadesc}{Peta}
1858 prefix peta = $10^{15}$
1859 \end{datadesc}
1860
1861 \begin{datadesc}{Tera}
1862 prefix tera = $10^{12}$
1863 \end{datadesc}
1864
1865 \begin{datadesc}{Giga}
1866 prefix giga = $10^9$
1867 \end{datadesc}
1868
1869 \begin{datadesc}{Mega}
1870 prefix mega = $10^6$
1871 \end{datadesc}
1872
1873 \begin{datadesc}{Kilo}
1874 prefix kilo = $10^3$
1875 \end{datadesc}
1876
1877 \begin{datadesc}{Hecto}
1878 prefix hecto = $10^2$
1879 \end{datadesc}
1880
1881 \begin{datadesc}{Deca}
1882 prefix deca = $10^1$
1883 \end{datadesc}
1884
1885 \begin{datadesc}{Deci}
1886 prefix deci = $10^{-1}$
1887 \end{datadesc}
1888
1889 \begin{datadesc}{Centi}
1890 prefix centi = $10^{-2}$
1891 \end{datadesc}
1892
1893 \begin{datadesc}{Milli}
1894 prefix milli = $10^{-3}$
1895 \end{datadesc}
1896
1897 \begin{datadesc}{Micro}
1898 prefix micro = $10^{-6}$
1899 \end{datadesc}
1900
1901 \begin{datadesc}{Nano}
1902 prefix nano = $10^{-9}$
1903 \end{datadesc}
1904
1905 \begin{datadesc}{Pico}
1906 prefix pico = $10^{-12}$
1907 \end{datadesc}
1908
1909 \begin{datadesc}{Femto}
1910 prefix femto = $10^{-15}$
1911 \end{datadesc}
1912
1913 \begin{datadesc}{Atto}
1914 prefix atto = $10^{-18}$
1915 \end{datadesc}
1916
1917 \begin{datadesc}{Zepto}
1918 prefix zepto = $10^{-21}$
1919 \end{datadesc}
1920
1921 \begin{datadesc}{Yocto}
1922 prefix yocto = $10^{-24}$
1923 \end{datadesc}
1924
1925 \section{Utilities}
1926 The \class{FileWriter} class provides a mechanism to write data to a file.
1927 In essence, this class wraps the standard \PYTHON \class{file} class to write
1928 data that are global in \MPI to a file. In fact, data are written on the
1929 processor with \MPI rank 0 only. It is recommended to use \class{FileWriter}
1930 rather than \class{open} in order to write code that will run with and without
1931 \MPI. It is safe to use \class{open} under \MPI to \emph{read} data which are
1932 global under \MPI.
1933
1934 \begin{classdesc}{FileWriter}{fn\optional{,append=\False, \optional{createLocalFiles=\False}})}
1935 Opens a file with name \var{fn} for writing. If \var{append} is set to \True
1936 data are appended at the end of the file.
1937 If running under \MPI, only the first processor (rank==0) will open the file
1938 and write to it.
1939 If \var{createLocalFiles} is set each individual processor will create a file
1940 where for any processor with rank $> 0$ the file name is extended by its rank.
1941 This option is normally used for debugging purposes only.
1942 \end{classdesc}
1943
1944 \vspace{1em}\noindent The following methods are available:
1945 \begin{methoddesc}[FileWriter]{close}{}
1946 closes the file.
1947 \end{methoddesc}
1948 \begin{methoddesc}[FileWriter]{flush}{}
1949 flushes the internal buffer to disk.
1950 \end{methoddesc}
1951 \begin{methoddesc}[FileWriter]{write}{txt}
1952 writes string \var{txt} to the file. Note that a newline is not added.
1953 \end{methoddesc}
1954 \begin{methoddesc}[FileWriter]{writelines}{txts}
1955 writes the list \var{txts} of strings to the file.
1956 Note that newlines are not added.
1957 This method is equivalent to calling \var{write()} for each string.
1958 \end{methoddesc}
1959 \begin{memberdesc}[FileWriter]{closed}
1960 this member is \True if the file is closed.
1961 \end{memberdesc}
1962 \begin{memberdesc}[FileWriter]{mode}
1963 holds the access mode.
1964 \end{memberdesc}
1965 \begin{memberdesc}[FileWriter]{name}
1966 holds the file name.
1967 \end{memberdesc}
1968 \begin{memberdesc}[FileWriter]{newlines}
1969 holds the line separator.
1970 \end{memberdesc}
1971
1972 \noindent The following additional functions are available in the \escript
1973 module:
1974 \begin{funcdesc}{setEscriptParamInt}{name,value}
1975 assigns the integer value \var{value} to the internal Escript parameter
1976 \var{name}. This should be considered an advanced feature and it is generally
1977 not required to call this function. One parameter worth mentioning is
1978 \var{name}="TOO_MANY_LINES" which affects the conversion of \Data objects to a
1979 string. If more than \var{value} lines would be created, a condensed format is
1980 used instead which reports the minimum and maximum values and general
1981 information about the \Data object rather than all values.
1982 \end{funcdesc}
1983
1984 \begin{funcdesc}{getEscriptParamInt}{name}
1985 returns the current value of internal Escript parameter \var{name}.
1986 \end{funcdesc}
1987
1988 \begin{funcdesc}{listEscriptParams}{a}
1989 returns a list of valid Escript parameters and their description.
1990 \end{funcdesc}
1991
1992 \begin{funcdesc}{getMPISizeWorld}{}
1993 returns the number of \MPI processes in use in the \env{MPI_COMM_WORLD}
1994 process group. If \MPI is not used 1 is returned.
1995 \end{funcdesc}
1996
1997 \begin{funcdesc}{getMPIRankWorld}{}
1998 returns the rank of the current process within the \env{MPI_COMM_WORLD}
1999 process group. If \MPI is not used 0 is returned.
2000 \end{funcdesc}
2001
2002 \begin{funcdesc}{MPIBarrierWorld}{}
2003 performs a barrier synchronization across all processes within the
2004 \env{MPI_COMM_WORLD} process group.
2005 \end{funcdesc}
2006
2007 \begin{funcdesc}{getMPIWorldMax}{a}
2008 returns the maximum value of the integer \var{a} across all processes within
2009 \env{MPI_COMM_WORLD}.
2010 \end{funcdesc}
2011

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26