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

Revision 5674 - (show annotations)
Mon Jun 22 02:46:40 2015 UTC (4 years ago) by caltinay
File MIME type: application/x-tex
File size: 78743 byte(s)
Fixed a few typos


 1 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 % Copyright (c) 2003-2015 by The University of Queensland 4 5 % 6 % Primary Business: Queensland, Australia 7 % Licensed under the Open Software License version 3.0 8 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{DiracDeltaFunctions(mydomain)}: functions defined on a set of points 58 \item \var{FunctionOnContact0(mydomain)}: functions on side $0$ of a discontinuity 59 \item \var{FunctionOnContact1(mydomain)}: functions on side $1$ of a discontinuity 60 \end{itemize} 61 In some cases under-integration is used. For these cases the user may use a 62 \FunctionSpace from the following list: 63 \begin{itemize} 64 \item \var{ReducedFunction(mydomain)} 65 \item \var{ReducedFunctionOnBoundary(mydomain)} 66 \item \var{ReducedFunctionOnContact0(mydomain)} 67 \item \var{ReducedFunctionOnContact1(mydomain)} 68 \end{itemize} 69 In comparison to the corresponding full version they use a reduced number of 70 integration nodes (typically one only) to represent values. 71 72 \begin{figure} 73 \centering 74 \includegraphics{EscriptDiagram1} 75 \caption{\label{ESCRIPT DEP}Dependency of function spaces in \finley. 76 An arrow indicates that a function in the \FunctionSpace at the starting point 77 can be interpolated to the \FunctionSpace of the arrow target. 78 All function spaces above the dotted line can be interpolated to any of 79 the function spaces below the line. See also \Sec{SEC Projection}.} 80 \end{figure} 81 82 The reduced smoothness for a PDE solution is often used to fulfill the 83 Ladyzhenskaya-Babuska-Brezzi condition~\cite{LBB} when solving saddle point 84 problems\index{saddle point problems}, e.g. the Stokes equation. 85 A discontinuity\index{discontinuity} is a region within the domain across 86 which functions may be discontinuous. 87 The location of a discontinuity is defined in the \Domain object. 88 \fig{ESCRIPT DEP} shows the dependency between the types of function spaces 89 in \finley (other libraries may have different relationships). 90 91 The solution of a PDE is a continuous function. Any continuous function can 92 be seen as a general function on the domain and can be restricted to the 93 boundary as well as to one side of a discontinuity (the result will be 94 different depending on which side is chosen). Functions on any side of the 95 discontinuity can be seen as a function on the corresponding other side. 96 97 A function on the boundary or on one side of the discontinuity cannot be seen 98 as a general function on the domain as there are no values defined for the 99 interior. For most PDE solver libraries the space of the solution and 100 continuous functions is identical, however in some cases, for example when 101 periodic boundary conditions are used in \finley, a solution fulfills periodic 102 boundary conditions while a continuous function does not have to be periodic. 103 104 The concept of function spaces describes the properties of functions and 105 allows abstraction from the actual representation of the function in the 106 context of a particular application. For instance, in the FEM context a 107 function of the \Function type (written as \emph{Function()} in \fig{ESCRIPT DEP}) 108 is usually represented by its values at the element center, 109 but in a finite difference scheme the edge midpoint of cells is preferred. 110 By changing its function space you can use the same function in a Finite 111 Difference scheme instead of Finite Element scheme. 112 Changing the function space of a particular function will typically lead to 113 a change of its representation. 114 So, when seen as a general function, a continuous function which is typically 115 represented by its values on the nodes of the FEM mesh or finite difference 116 grid must be interpolated to the element centers or the cell edges, 117 respectively. Interpolation happens automatically in \escript whenever it is 118 required\index{interpolation}. The user needs to be aware that an 119 interpolation is not always possible, see \fig{ESCRIPT DEP} for \finley. 120 An alternative approach to change the representation (=\FunctionSpace) is 121 projection\index{projection}, see \Sec{SEC Projection}. 122 123 \subsection{\Data Objects} 124 In \escript the class that stores these functions is called \Data. 125 The function is represented through its values on \DataSamplePoints where 126 the \DataSamplePoints are chosen according to the function space of the 127 function. 128 \Data class objects are used to define the coefficients of the PDEs to be 129 solved by a PDE solver library and also to store the solutions of the PDE. 130 131 The values of the function have a rank which gives the number of indices, 132 and a \Shape defining the range of each index. 133 The rank in \escript is limited to the range 0 through 4 and it is assumed 134 that the rank and \Shape is the same for all \DataSamplePoints. 135 The \Shape of a \Data object is a tuple (list) \var{s} of integers. 136 The length of \var{s} is the rank of the \Data object and the \var{i}-th 137 index ranges between 0 and $\var{s[i]}-1$. 138 For instance, a stress field has rank 2 and \Shape $(d,d)$ where $d$ is the 139 number of spatial dimensions. 140 The following statement creates the \Data object \var{mydat} representing a 141 continuous function with values of \Shape $(2,3)$ and rank $2$: 142 \begin{python} 143 mydat=Data(value=1, what=ContinuousFunction(myDomain), shape=(2,3)) 144 \end{python} 145 The initial value is the constant 1 for all \DataSamplePoints and all 146 components. 147 148 \Data objects can also be created from any \numpy array or any object, such 149 as a list of floating point numbers, that can be converted into 150 a \numpyNDA\cite{NUMPY}. 151 The following two statements create objects which are equivalent 152 to \var{mydat}: 153 \begin{python} 154 mydat1=Data(value=numpy.ones((2,3)), what=ContinuousFunction(myDomain)) 155 mydat2=Data(value=[[1,1], [1,1], [1,1]], what=ContinuousFunction(myDomain)) 156 \end{python} 157 In the first case the initial value is \var{numpy.ones((2,3))} which generates 158 a $2 \times 3$ matrix as an instance of \numpyNDA filled with ones. 159 The \Shape of the created \Data object is taken from the \Shape of the array. 160 In the second case, the creator converts the initial value, which is a list of 161 lists, into a \numpyNDA before creating the actual \Data object. 162 163 For convenience \escript provides creators for the most common types 164 of \Data objects in the following forms (\var{d} defines the spatial 165 dimensionality): 166 \begin{itemize} 167 \item \code{Scalar(0, Function(mydomain))} is the same as \code{Data(0, Function(myDomain),(,))}\\ 168 (each value is a scalar), e.g. a temperature field 169 \item \code{Vector(0, Function(mydomain))} is the same as \code{Data(0, Function(myDomain),(d,))}\\ 170 (each value is a vector), e.g. a velocity field 171 \item \code{Tensor(0, Function(mydomain))} equals \code{Data(0, Function(myDomain), (d,d))}, 172 e.g. a stress field 173 \item \code{Tensor4(0,Function(mydomain))} equals \code{Data(0,Function(myDomain), (d,d,d,d))}, 174 e.g. a Hook tensor field 175 \end{itemize} 176 Here the initial value is 0 but any object that can be converted into 177 a \numpyNDA and whose \Shape is consistent with \Shape of the \Data object to 178 be created can be used as the initial value. 179 180 \Data objects can be manipulated by applying unary operations (e.g. cos, sin, 181 log), and they can be combined point-wise by applying arithmetic operations 182 (e.g. +, - ,* , /). 183 We emphasize that \escript itself does not handle any spatial dependencies as 184 it does not know how values are interpreted by the processing PDE solver library. 185 However \escript invokes interpolation if this is needed during data manipulations. 186 Typically, this occurs in binary operations when the arguments belong to 187 different function spaces or when data are handed over to a PDE solver library 188 which requires functions to be represented in a particular way. 189 190 The following example shows the usage of \Data objects. Assume we have a 191 displacement field $u$ and we want to calculate the corresponding stress field 192 $\sigma$ using the linear-elastic isotropic material model 193 \begin{eqnarray}\label{eq: linear elastic stress} 194 \sigma_{ij}=\lambda u_{k,k} \delta_{ij} + \mu ( u_{i,j} + u_{j,i}) 195 \end{eqnarray} 196 where $\delta_{ij}$ is the Kronecker symbol and 197 $\lambda$ and $\mu$ are the Lam\'e coefficients. The following function 198 takes the displacement \var{u} and the Lam\'e coefficients \var{lam} and \var{mu} 199 as arguments and returns the corresponding stress: 200 \begin{python} 201 from esys.escript import * 202 def getStress(u, lam, mu): 203 d=u.getDomain().getDim() 204 g=grad(u) 205 stress=lam*trace(g)*kronecker(d)+mu*(g+transpose(g)) 206 return stress 207 \end{python} 208 The variable \var{d} gives the spatial dimensionality of the domain on which 209 the displacements are defined. 210 \var{kronecker} returns the Kronecker symbol with indices $i$ and $j$ running 211 from 0 to \var{d}-1. 212 The call \var{grad(u)} requires the displacement field \var{u} to be in 213 the \var{Solution} or \ContinuousFunction. 214 The result \var{g} as well as the returned stress will be in the \Function. 215 If, for example, \var{u} is the solution of a PDE then \code{getStress} might 216 be called in the following way: 217 \begin{python} 218 s=getStress(u, 1., 2.) 219 \end{python} 220 However \code{getStress} can also be called with \Data objects as values for 221 \var{lam} and \var{mu} which, for instance in the case of a temperature 222 dependency, are calculated by an expression. 223 The following call is equivalent to the previous example: 224 \begin{python} 225 lam=Scalar(1., ContinuousFunction(mydomain)) 226 mu=Scalar(2., Function(mydomain)) 227 s=getStress(u, lam, mu) 228 \end{python} 229 % 230 The function \var{lam} belongs to the \ContinuousFunction but with \var{g} the 231 function \var{trace(g)} is in the \Function. 232 In the evaluation of the product \var{lam*trace(g)} we have different function 233 spaces (on the nodes versus in the centers) and at first glance we have incompatible data. 234 \escript converts the arguments into an appropriate function space according 235 to \fig{ESCRIPT DEP}. 236 In this example that means \escript sees \var{lam} as a function of the \Function. 237 In the context of FEM this means the nodal values of \var{lam} are 238 interpolated to the element centers. 239 The interpolation is automatic and requires no special handling. 240 241 \begin{figure} 242 \centering 243 \includegraphics{EscriptDiagram2} 244 \caption{\label{Figure: tag}Element Tagging. A rectangular mesh over a region 245 with two rock types {\it white} and {\it gray} is shown. 246 The number in each cell refers to the major rock type present in the cell 247 ($1$ for {\it white} and $2$ for {\it gray}).} 248 \end{figure} 249 250 \subsection{Tagged, Expanded and Constant Data} 251 Material parameters such as the Lam\'e coefficients are typically dependent on 252 rock types present in the area of interest. 253 A common technique to handle these kinds of material parameters is 254 \emph{tagging}\index{tagging}, which uses storage efficiently. 255 \fig{Figure: tag} shows an example. In this case two rock types {\it white} 256 and {\it gray} can be found in the domain. 257 The domain is subdivided into triangular shaped cells. 258 Each cell has a tag indicating the rock type predominantly found in this cell. 259 Here $1$ is used to indicate rock type {\it white} and $2$ for rock type {\it gray}. 260 The tags are assigned at the time when the cells are generated and stored in 261 the \Domain class object. To allow easier usage of tags, names can be used 262 instead of numbers. These names are typically defined at the time when the 263 geometry is generated. 264 265 The following statements show how to use tagged values for \var{lam} as shown 266 in \fig{Figure: tag} for the stress calculation discussed above: 267 \begin{python} 268 lam=Scalar(value=2., what=Function(mydomain)) 269 insertTaggedValue(lam, white=30., gray=5000.) 270 s=getStress(u, lam, 2.) 271 \end{python} 272 In this example \var{lam} is set to $30$ for those cells with tag {\it white} 273 (=$1$) and to $5000$ for cells with tag {\it gray} (=$2$). 274 The initial value $2$ of \var{lam} is used as a default value for the case 275 when a tag is encountered which has not been linked with a value. 276 The \code{getStress} method does not need to be changed now that we are using tags. 277 \escript resolves the tags when \var{lam*trace(g)} is calculated. 278 279 This brings us to a very important point about \escript. 280 You can develop a simulation with constant Lam\'e coefficients, and then later 281 switch to tagged Lam\'e coefficients without otherwise changing your \PYTHON script. 282 In short, you can use the same script for models with different domains and 283 different types of input data. 284 285 There are three main ways in which \Data objects are represented internally -- 286 constant, tagged, and expanded. 287 In the constant case, the same value is used at each sample point while only a 288 single value is stored to save memory. 289 In the expanded case, each sample point has an individual value (such as for the solution of a PDE). 290 This is where your largest data sets will be created because the values are 291 stored as a complete array. 292 The tagged case has already been discussed above. 293 Expanded data is created when specifying \code{expanded=True} in the \Data 294 object constructor, while tagged data requires calling the \member{insertTaggedValue} 295 method as shown above. 296 297 Values are accessed through a sample reference number. 298 Operations on expanded \Data objects have to be performed for each sample 299 point individually. 300 When tagged values are used, the values are held in a dictionary. 301 Operations on tagged data require processing the set of tagged values only, 302 rather than processing the value for each individual sample point. 303 \escript allows any mixture of constant, tagged and expanded data in a single expression. 304 305 \subsection{Saving and Restoring Simulation Data} 306 \Data objects can be written to disk files with the \member{dump} method and 307 read back using the \member{load} method, both of which use the 308 \netCDF\cite{NETCDF} file format. 309 Use these to save data for checkpoint/restart or simply to save and reuse data 310 that was expensive to compute. 311 For instance, to save the coordinates of the data points of a 312 \ContinuousFunction to the file \file{x.nc} use 313 \begin{python} 314 x=ContinuousFunction(mydomain).getX() 315 x.dump("x.nc") 316 mydomain.dump("dom.nc") 317 \end{python} 318 To recover the object \var{x}, and you know that \var{mydomain} was an \finley 319 mesh, use 320 \begin{python} 321 from esys.finley import LoadMesh 322 mydomain=LoadMesh("dom.nc") 323 x=load("x.nc", mydomain) 324 \end{python} 325 Obviously, it is possible to execute the same steps that were originally used 326 to generate \var{mydomain} to recreate it. However, in most cases using 327 \member{dump} and \member{load} is faster, particularly if optimization has 328 been applied. 329 If \escript is running on more than one \MPI process \member{dump} will create 330 an individual file for each process containing the local data. 331 In order to avoid conflicts the \MPI processor 332 rank is appended to the file names. 333 That is instead of one file \file{dom.nc} you would get 334 \file{dom.nc.0000}, \file{dom.nc.0001}, etc. 335 You still call \code{LoadMesh("dom.nc")} to load the domain but you have to 336 make sure that the appropriate file is accessible from the corresponding rank, 337 and loading will only succeed if you run with as many processes as were used 338 when calling \member{dump}. 339 340 The function space of the \Data is stored in \file{x.nc}. 341 If the \Data object is expanded, the number of data points in the file and of 342 the \Domain for the particular \FunctionSpace must match. 343 Moreover, the ordering of the values is checked using the reference 344 identifiers provided by the \FunctionSpace on the \Domain. 345 In some cases, data points will be reordered so be aware and confirm that you 346 get what you wanted. 347 348 A more flexible way of saving and restoring \escript simulation data 349 is through an instance of the \class{DataManager} class. 350 It has the advantage of allowing to save and load not only a \Domain and 351 \Data objects but also other values\footnote{The \PYTHON \emph{pickle} module 352 is used for other types.} you compute in your simulation script. 353 Further, \class{DataManager} objects can simultaneously create files for 354 visualization so no extra calls to \code{saveVTK} etc. are needed. 355 356 The following example shows how the \class{DataManager} class can be used. 357 For an explanation of all member functions and options see the class reference 358 Section \ref{sec:datamanager}. 359 \begin{python} 360 from esys.escript import DataManager, Scalar, Function 361 from esys.finley import Rectangle 362 363 dm = DataManager(formats=[DataManager.RESTART, DataManager.VTK]) 364 if dm.hasData(): 365 mydomain=dm.getDomain() 366 val=dm.getValue("val") 367 t=dm.getValue("t") 368 t_max=dm.getValue("t_max") 369 else: 370 mydomain=Rectangle() 371 val=Function(mydomain).getX() 372 t=0. 373 t_max=2.5 374 375 while t$\var{maxval} the value \var{maxval} is used. 1376 1377 Now we produce our new \Data object: 1378 1379 \begin{python} 1380 result=interpolateTable(sine_table, x[0], minval, step, toobig) 1381 \end{python} 1382 Any values which interpolate to larger than \var{toobig} will raise an 1383 exception. You can switch on boundary checking by adding 1384 \code{check_boundaries=True} to the argument list. 1385 1386 Now consider a 2D example. We will interpolate from a plane where$\forall x,y\in[0,9]:(x,y)=x+y\cdot10$. 1387 1388 \begin{python} 1389 from esys.escript import whereZero 1390 table2=[] 1391 for y in range(0,10): 1392 r=[] 1393 for x in range(0,10): 1394 r.append(x+y*10) 1395 table2.append(r) 1396 xstep=(maxval-minval)/(10-1) 1397 ystep=(maxval-minval)/(10-1) 1398 1399 xmin=minval 1400 ymin=minval 1401 1402 result2=interpolateTable(table2, x2, (xmin, ymin), (xstep, ystep), toobig) 1403 \end{python} 1404 1405 We can check the values using \function{whereZero}. 1406 For example, for$x=0$: 1407 \begin{python} 1408 print(result2*whereZero(x[0])) 1409 \end{python} 1410 1411 Finally let us look at a 3D example. Note that the parameter tuples should be 1412$(x,y,z)$but that in the interpolation table,$x$is the innermost dimension. 1413 \begin{python} 1414 b=Brick(n,n,n) 1415 x3=b.getX() 1416 toobig=1000000 1417 1418 table3=[] 1419 for z in range(0,10): 1420 face=[] 1421 for y in range(0,10): 1422 r=[] 1423 for x in range(0,10): 1424 r.append(x+y*10+z*100) 1425 face.append(r) 1426 table3.append(face); 1427 1428 zstep=(maxval-minval)/(10-1) 1429 1430 zmin=minval 1431 1432 result3=interpolateTable(table3, x3, (xmin, ymin, zmin), (xstep, ystep, zstep), toobig) 1433 \end{python} 1434 1435 1436 \subsubsection{Non-uniform Interpolation} 1437 Non-uniform interpolation is also supported for the one dimensional case. 1438 \begin{python} 1439 Data.nonuniformInterpolate(in, out, check_boundaries) 1440 Data.nonuniformSlope(in, out, check_boundaries) 1441 \end{python} 1442 1443 Will produce a new \Data object by mapping the given \Data object through the user-defined function 1444 specified by \texttt{in} and \texttt{out}. 1445 The \ldots Interpolate version gives the value of the function at the specified point and the 1446 \ldots Slope version gives the slope at those points. 1447 The check_boundaries boolean argument specifies what the function should do if the \Data object contains 1448 values outside the range specified by the \texttt{in} parameter. 1449 If the argument is \texttt{False}, then those datapoints will be interpolated to the value of the edge 1450 they are closest to (or assigned a slope of zero). 1451 If the argument is \texttt{True}, then an exception will be thrown if out of bounds values are detected. 1452 Note that the values given by the \texttt{in} parameter must be monotonically increasing. 1453 1454 \noindent For example:\\ 1455 If \texttt{d} contains the values \texttt{\{1,2,3,4,5\}}, then 1456 \begin{python} 1457 d.nonuniformInterpolate([1.5, 2, 2.8, 4.6], [4, 5, -1, 1], False) 1458 \end{python} 1459 would produce a \Data object containing \texttt{\{4, 5, -0.7777, 0.3333, 1\}}.\\ 1460 A similar call to \texttt{nonuniformSlope} would produce a \Data object containing \texttt{\{0, 2, 1.1111, 1.1111, 0\}}. 1461 % 1462 % 1463 % We will interpolate a surface such that the bottom 1464 % edge is the sine curve described above. 1465 % The amplitude of the curve decreases as we move towards the top edge. 1466 % Our interpolation table will have three rows: 1467 % 1468 % \begin{python} 1469 % st=numpy.array(sine_table) 1470 % table=[st, 0.5*st, 0*st] 1471 % \end{python} 1472 % % 1473 % The use of \numpy and multiplication here is just to save typing. 1474 % 1475 % % result2=x1.interpolateTable(table, 0, 0.55, x0, minval, step, toobig) 1476 % \begin{python} 1477 % result=interpolateTable(table, x (minval,0), (0.55, step), toobig) 1478 % \end{python} 1479 % 1480 % In the 2D case the start and step parameters are tuples$(x,y)$. 1481 % By default, if a point is specified which is outside the boundary, then 1482 % \var{interpolateTable} will operate as if the point was on the boundary. 1483 % Passing \code{check_boundaries=True} will lead to the rejection of any points 1484 % outside the boundaries by \var{interpolateTable}. 1485 % 1486 % This method can also be called with three dimensional tables and \Data objects. 1487 % Tuples should be ordered$(x,y,z)$. 1488 1489 \subsection{The \var{DataManager} Class} 1490 \label{sec:datamanager} 1491 1492 The \var{DataManager} class can be used to conveniently add checkpoint/restart 1493 functionality to \escript simulations. 1494 Once an instance is created \Data objects and other values can be added and 1495 dumped to disk by a single method call. 1496 If required the object can be set up to also save the data in a format suitable 1497 for visualization. 1498 Internally the \var{DataManager} interfaces with \weipa for this. 1499 1500 \begin{classdesc}{DataManager}{formats=[RESTART], work_dir=".", restart_prefix="restart", do_restart=\True} 1501 initializes a new \var{DataManager} object which can be used to save, 1502 restore and export simulation data in a number of formats. 1503 All files and directories saved or restored by this object are located 1504 under the directory specified by \var{work_dir}. 1505 If \var{RESTART} is specified in \var{formats}, the \var{DataManager} will 1506 look for directories whose name starts with \var{restart_prefix}. 1507 In case \var{do_restart} is \True, the last of these directories is used 1508 to restore simulation data while all others are deleted. 1509 If \var{do_restart} is \False, then all of those directories are deleted. 1510 The \var{restart_prefix} and \var{do_restart} parameters are ignored if 1511 \var{RESTART} is not specified in \var{formats}. 1512 \end{classdesc} 1513 1514 \noindent Valid values for the \var{formats} parameter are: 1515 \begin{memberdesc}[DataManager]{RESTART} 1516 enables writing of checkpoint files to be able to continue simulations 1517 as explained in the class description. 1518 \end{memberdesc} 1519 \begin{memberdesc}[DataManager]{SILO} 1520 exports simulation data in the \SILO file format. \escript must have 1521 been compiled with \SILO support for this to work. 1522 \end{memberdesc} 1523 \begin{memberdesc}[DataManager]{VISIT} 1524 enables the \VisIt simulation interface which allows connecting to and 1525 interacting with the running simulation from a compatible \VisIt client. 1526 \escript must have been compiled with \VisIt (version 2) support and the 1527 version of the client has to match the version used at compile time. 1528 In order to connect to the simulation the client needs to have access and 1529 load the file \file{escriptsim.sim2} located under the work directory. 1530 \end{memberdesc} 1531 \begin{memberdesc}[DataManager]{VTK} 1532 exports simulation data in the \VTK file format. 1533 \end{memberdesc} 1534 1535 \noindent The \var{DataManager} class has the following methods: 1536 \begin{methoddesc}[DataManager]{addData}{**data} 1537 adds \Data objects and other data to the manager. Calling this method does 1538 not save or export the data yet so it is allowed to incrementally add data 1539 at various points in the simulation script if required. 1540 Note, that only a single domain is supported so all \Data objects have to 1541 be defined on the same one or an exception is raised. 1542 \end{methoddesc} 1543 1544 \begin{methoddesc}[DataManager]{setDomain}{domain} 1545 explicitly sets the domain for this manager. 1546 It is generally not required to call this method directly. 1547 Instead, the \var{addData} method will set the domain used by the \Data 1548 objects. 1549 An exception is raised if the domain was set to a different domain before 1550 (explicitly or implicitly). 1551 \end{methoddesc} 1552 1553 \begin{methoddesc}[DataManager]{hasData}{} 1554 returns \True if the manager has loaded simulation data for a restart. 1555 \end{methoddesc} 1556 1557 \begin{methoddesc}[DataManager]{getDomain}{} 1558 returns the domain as recovered from a restart. 1559 \end{methoddesc} 1560 1561 \begin{methoddesc}[DataManager]{getValue}{value_name} 1562 returns a \Data object or other value with the name \var{value_name} that 1563 has been recovered after a restart. 1564 \end{methoddesc} 1565 1566 \begin{methoddesc}[DataManager]{getCycle}{} 1567 returns the export cycle, i.e. the number of times \var{export()} has been 1568 called. 1569 \end{methoddesc} 1570 1571 \begin{methoddesc}[DataManager]{setCheckpointFrequency}{freq} 1572 sets the frequency with which checkpoint files are created. This is only 1573 useful if the \var{DataManager} object was created with at least one other 1574 format next to \var{RESTART}. The frequency is 1 by default which means 1575 that checkpoint files are created every time \var{export()} is called. 1576 Unlike visualization output, a simulation checkpoint is usually not 1577 required at every time step. Thus, the frequency can be decreased by 1578 calling this method with$\var{freq}>1$which would then create restart 1579 files every \var{freq} times \var{export()} is called. 1580 \end{methoddesc} 1581 1582 \begin{methoddesc}[DataManager]{setTime}{time} 1583 sets the simulation time stamp. This floating point number is stored in 1584 the metadata of exported data but not used by \var{RESTART}. 1585 \end{methoddesc} 1586 1587 \begin{methoddesc}[DataManager]{setMeshLabels}{x, y, z=""} 1588 sets labels for the mesh axes. These are currently only used by the \SILO 1589 exporter. 1590 \end{methoddesc} 1591 1592 \begin{methoddesc}[DataManager]{setMeshUnits}{x, y, z=""} 1593 sets units for the mesh axes. These are currently only used by the \SILO 1594 exporter. 1595 \end{methoddesc} 1596 1597 \begin{methoddesc}[DataManager]{setMetadataSchemaString}{schema, metadata=""} 1598 sets metadata namespaces and the corresponding metadata. These are 1599 currently only used by the \VTK exporter. 1600 \var{schema} is a dictionary that maps prefixes to namespace names, e.g.\\ 1601 \code{\{"gml": "http://www.opengis.net/gml"\}} and \var{metadata} is a 1602 string with the actual content which will be enclosed in \var{} 1603 tags. 1604 \end{methoddesc} 1605 1606 \begin{methoddesc}[DataManager]{export}{} 1607 executes the actual data export. Depending on the \var{formats} parameter 1608 used in the constructor all data added by \var{addData()} is written to 1609 disk (\var{RESTART,SILO,VTK}) or made available through the \VisIt 1610 simulation interface (\var{VISIT}). 1611 At least the domain must be set for something to be exported. 1612 \end{methoddesc} 1613 1614 \subsection{Saving Data as CSV} 1615 \label{sec:savedatacsv} 1616 \index{saveDataCSV}\index{CSV} 1617 For simple post-processing, \Data objects can be saved in comma separated 1618 value (\emph{CSV}) format. 1619 If \var{mydata1} and \var{mydata2} are scalar data, the command 1620 \begin{python} 1621 saveDataCSV('output.csv', U=mydata1, V=mydata2) 1622 \end{python} 1623 will record the values in \file{output.csv} in the following format: 1624 \begin{verbatim} 1625 U, V 1626 1.0000000e+0, 2.0000000e-1 1627 5.0000000e-0, 1.0000000e+1 1628 ... 1629 \end{verbatim} 1630 1631 The names of the keyword parameters form the names of columns in the output. 1632 If the data objects are over different function spaces, then \var{saveDataCSV} 1633 will attempt to interpolate to a common function space. 1634 If this is not possible, then an exception is raised. 1635 1636 Output can be restricted using a scalar mask as follows: 1637 \begin{python} 1638 saveDataCSV('outfile.csv', U=mydata1, V=mydata2, mask=myscalar) 1639 \end{python} 1640 This command will only output those rows which correspond to to positive 1641 values of \var{myscalar}. 1642 Some aspects of the output can be tuned using additional parameters: 1643 \begin{python} 1644 saveDataCSV('data.csv', append=True, sep=' ', csep='/', mask=mymask, e=mat1) 1645 \end{python} 1646 1647 \begin{itemize} 1648 \item \var{append} -- specifies that the output should be written to the end of an existing file 1649 \item \var{sep} -- defines the separator between fields 1650 \item \var{csep} -- defines the separator between components in the header 1651 line. For example between the components of a matrix. 1652 \end{itemize} 1653 % 1654 The above command would produce output like this: 1655 \begin{verbatim} 1656 e/0/0 e/1/0 e/0/1 e/1/1 1657 1.0000000000e+00 2.0000000000e+00 3.0000000000e+00 4.0000000000e+00 1658 ... 1659 \end{verbatim} 1660 1661 Note that while the order in which rows are output can vary, all the elements 1662 in a given row always correspond to the same input. 1663 When run on more than one \MPI rank, \function{saveDataCSV} is currently 1664 limited to certain domain and function space combinations throwing an exception 1665 in other cases. Writing data on \ContinuousFunction is always supported. 1666 1667 \subsection{The \Operator Class} 1668 The \Operator class provides an abstract access to operators built 1669 within the \LinearPDE class. \Operator objects are created 1670 when a PDE is handed over to a PDE solver library and handled 1671 by the \LinearPDE object defining the PDE. The user can gain access 1672 to the \Operator of a \LinearPDE object through the \var{getOperator} 1673 method. 1674 1675 \begin{classdesc}{Operator}{} 1676 creates an empty \Operator object. 1677 \end{classdesc} 1678 1679 \begin{methoddesc}[Operator]{isEmpty}{fileName} 1680 returns \True is the object is empty, \False otherwise. 1681 \end{methoddesc} 1682 1683 \begin{methoddesc}[Operator]{resetValues}{} 1684 resets all entries in the operator. 1685 \end{methoddesc} 1686 1687 \begin{methoddesc}[Operator]{solve}{rhs} 1688 returns the solution \var{u} of: operator * \var{u} = \var{rhs}. 1689 \end{methoddesc} 1690 1691 \begin{methoddesc}[Operator]{of}{u} 1692 applies the operator to the \Data object \var{u}, i.e. performs a matrix-vector 1693 multiplication. 1694 \end{methoddesc} 1695 1696 \begin{methoddesc}[Operator]{saveMM}{fileName}\index{Matrix Market} 1697 saves the object to a Matrix Market format file with name \var{fileName}, see 1698 \url{http://math.nist.gov/MatrixMarket} 1699 \end{methoddesc} 1700 1701 \section{Physical Units} 1702 \escript provides support for physical units in the SI system\index{SI units} 1703 including unit conversion. So the user can define variables in the form 1704 \begin{python} 1705 from esys.escript.unitsSI import * 1706 l=20*m 1707 w=30*kg 1708 w2=40*lb 1709 T=100*Celsius 1710 \end{python} 1711 In the two latter cases a conversion from pounds\index{pounds} and degrees 1712 Celsius\index{Celsius} is performed into the appropriate SI units \emph{kg} 1713 and \emph{Kelvin}. 1714 In addition, composed units can be used, for instance 1715 \begin{python} 1716 from esys.escript.unitsSI import * 1717 rho=40*lb/cm**3 1718 \end{python} 1719 defines the density in the units of pounds per cubic centimeter. 1720 The value$40$will be converted into SI units, in this case kg per cubic 1721 meter. Moreover unit prefixes are supported: 1722 \begin{python} 1723 from esys.escript.unitsSI import * 1724 p=40*Mega*Pa 1725 \end{python} 1726 The pressure \var{p} is set to 40 Mega Pascal. Units can also be converted 1727 back from the SI system into a desired unit, e.g. 1728 \begin{python} 1729 from esys.escript.unitsSI import * 1730 print(p/atm) 1731 \end{python} 1732 can be used print the pressure in units of atmosphere\index{atmosphere}. 1733 1734 The following is an incomplete list of supported physical units: 1735 1736 \begin{datadesc}{km} 1737 unit of kilometer 1738 \end{datadesc} 1739 1740 \begin{datadesc}{m} 1741 unit of meter 1742 \end{datadesc} 1743 1744 \begin{datadesc}{cm} 1745 unit of centimeter 1746 \end{datadesc} 1747 1748 \begin{datadesc}{mm} 1749 unit of millimeter 1750 \end{datadesc} 1751 1752 \begin{datadesc}{sec} 1753 unit of second 1754 \end{datadesc} 1755 1756 \begin{datadesc}{minute} 1757 unit of minute 1758 \end{datadesc} 1759 1760 \begin{datadesc}{h} 1761 unit of hour 1762 \end{datadesc} 1763 1764 \begin{datadesc}{day} 1765 unit of day 1766 \end{datadesc} 1767 1768 \begin{datadesc}{yr} 1769 unit of year 1770 \end{datadesc} 1771 1772 \begin{datadesc}{gram} 1773 unit of gram 1774 \end{datadesc} 1775 1776 \begin{datadesc}{kg} 1777 unit of kilogram 1778 \end{datadesc} 1779 1780 \begin{datadesc}{lb} 1781 unit of pound 1782 \end{datadesc} 1783 1784 \begin{datadesc}{ton} 1785 metric ton 1786 \end{datadesc} 1787 1788 \begin{datadesc}{A} 1789 unit of Ampere 1790 \end{datadesc} 1791 1792 \begin{datadesc}{Hz} 1793 unit of Hertz 1794 \end{datadesc} 1795 1796 \begin{datadesc}{N} 1797 unit of Newton 1798 \end{datadesc} 1799 1800 \begin{datadesc}{Pa} 1801 unit of Pascal 1802 \end{datadesc} 1803 1804 \begin{datadesc}{atm} 1805 unit of atmosphere 1806 \end{datadesc} 1807 1808 \begin{datadesc}{J} 1809 unit of Joule 1810 \end{datadesc} 1811 1812 \begin{datadesc}{W} 1813 unit of Watt 1814 \end{datadesc} 1815 1816 \begin{datadesc}{C} 1817 unit of Coulomb 1818 \end{datadesc} 1819 1820 \begin{datadesc}{V} 1821 unit of Volt 1822 \end{datadesc} 1823 1824 \begin{datadesc}{F} 1825 unit of Farad 1826 \end{datadesc} 1827 1828 \begin{datadesc}{Ohm} 1829 unit of Ohm 1830 \end{datadesc} 1831 1832 \begin{datadesc}{K} 1833 unit of degrees Kelvin 1834 \end{datadesc} 1835 1836 \begin{datadesc}{Celsius} 1837 unit of degrees Celsius 1838 \end{datadesc} 1839 1840 \begin{datadesc}{Fahrenheit} 1841 unit of degrees Fahrenheit 1842 \end{datadesc} 1843 1844 \noindent Supported unit prefixes: 1845 1846 \begin{datadesc}{Yotta} 1847 prefix yotta =$10^{24}$1848 \end{datadesc} 1849 1850 \begin{datadesc}{Zetta} 1851 prefix zetta =$10^{21}$1852 \end{datadesc} 1853 1854 \begin{datadesc}{Exa} 1855 prefix exa =$10^{18}$1856 \end{datadesc} 1857 1858 \begin{datadesc}{Peta} 1859 prefix peta =$10^{15}$1860 \end{datadesc} 1861 1862 \begin{datadesc}{Tera} 1863 prefix tera =$10^{12}$1864 \end{datadesc} 1865 1866 \begin{datadesc}{Giga} 1867 prefix giga =$10^9$1868 \end{datadesc} 1869 1870 \begin{datadesc}{Mega} 1871 prefix mega =$10^6$1872 \end{datadesc} 1873 1874 \begin{datadesc}{Kilo} 1875 prefix kilo =$10^3$1876 \end{datadesc} 1877 1878 \begin{datadesc}{Hecto} 1879 prefix hecto =$10^2$1880 \end{datadesc} 1881 1882 \begin{datadesc}{Deca} 1883 prefix deca =$10^1$1884 \end{datadesc} 1885 1886 \begin{datadesc}{Deci} 1887 prefix deci =$10^{-1}$1888 \end{datadesc} 1889 1890 \begin{datadesc}{Centi} 1891 prefix centi =$10^{-2}$1892 \end{datadesc} 1893 1894 \begin{datadesc}{Milli} 1895 prefix milli =$10^{-3}$1896 \end{datadesc} 1897 1898 \begin{datadesc}{Micro} 1899 prefix micro =$10^{-6}$1900 \end{datadesc} 1901 1902 \begin{datadesc}{Nano} 1903 prefix nano =$10^{-9}$1904 \end{datadesc} 1905 1906 \begin{datadesc}{Pico} 1907 prefix pico =$10^{-12}$1908 \end{datadesc} 1909 1910 \begin{datadesc}{Femto} 1911 prefix femto =$10^{-15}$1912 \end{datadesc} 1913 1914 \begin{datadesc}{Atto} 1915 prefix atto =$10^{-18}$1916 \end{datadesc} 1917 1918 \begin{datadesc}{Zepto} 1919 prefix zepto =$10^{-21}$1920 \end{datadesc} 1921 1922 \begin{datadesc}{Yocto} 1923 prefix yocto =$10^{-24}$1924 \end{datadesc} 1925 1926 \section{Utilities} 1927 The \class{FileWriter} class provides a mechanism to write data to a file. 1928 In essence, this class wraps the standard \PYTHON \class{file} class to write 1929 data that are global in \MPI to a file. In fact, data are written on the 1930 processor with \MPI rank 0 only. It is recommended to use \class{FileWriter} 1931 rather than \class{open} in order to write code that will run with and without 1932 \MPI. It is safe to use \class{open} under \MPI to \emph{read} data which are 1933 global under \MPI. 1934 1935 \begin{classdesc}{FileWriter}{fn\optional{,append=\False, \optional{createLocalFiles=\False}})} 1936 Opens a file with name \var{fn} for writing. If \var{append} is set to \True 1937 data are appended at the end of the file. 1938 If running under \MPI, only the first processor (rank==0) will open the file 1939 and write to it. 1940 If \var{createLocalFiles} is set each individual processor will create a file 1941 where for any processor with rank$> 0\$ the file name is extended by its rank. 1942 This option is normally used for debugging purposes only. 1943 \end{classdesc} 1944 1945 \vspace{1em}\noindent The following methods are available: 1946 \begin{methoddesc}[FileWriter]{close}{} 1947 closes the file. 1948 \end{methoddesc} 1949 \begin{methoddesc}[FileWriter]{flush}{} 1950 flushes the internal buffer to disk. 1951 \end{methoddesc} 1952 \begin{methoddesc}[FileWriter]{write}{txt} 1953 writes string \var{txt} to the file. Note that a newline is not added. 1954 \end{methoddesc} 1955 \begin{methoddesc}[FileWriter]{writelines}{txts} 1956 writes the list \var{txts} of strings to the file. 1957 Note that newlines are not added. 1958 This method is equivalent to calling \var{write()} for each string. 1959 \end{methoddesc} 1960 \begin{memberdesc}[FileWriter]{closed} 1961 this member is \True if the file is closed. 1962 \end{memberdesc} 1963 \begin{memberdesc}[FileWriter]{mode} 1964 holds the access mode. 1965 \end{memberdesc} 1966 \begin{memberdesc}[FileWriter]{name} 1967 holds the file name. 1968 \end{memberdesc} 1969 \begin{memberdesc}[FileWriter]{newlines} 1970 holds the line separator. 1971 \end{memberdesc} 1972 1973 \noindent The following additional functions are available in the \escript 1974 module: 1975 \begin{funcdesc}{setEscriptParamInt}{name,value} 1976 assigns the integer value \var{value} to the internal Escript parameter 1977 \var{name}. This should be considered an advanced feature and it is generally 1978 not required to call this function. One parameter worth mentioning is 1979 \var{name}="TOO_MANY_LINES" which affects the conversion of \Data objects to a 1980 string. If more than \var{value} lines would be created, a condensed format is 1981 used instead which reports the minimum and maximum values and general 1982 information about the \Data object rather than all values. 1983 \end{funcdesc} 1984 1985 \begin{funcdesc}{getEscriptParamInt}{name} 1986 returns the current value of internal Escript parameter \var{name}. 1987 \end{funcdesc} 1988 1989 \begin{funcdesc}{listEscriptParams}{a} 1990 returns a list of valid Escript parameters and their description. 1991 \end{funcdesc} 1992 1993 \begin{funcdesc}{getMPISizeWorld}{} 1994 returns the number of \MPI processes in use in the \env{MPI_COMM_WORLD} 1995 process group. If \MPI is not used 1 is returned. 1996 \end{funcdesc} 1997 1998 \begin{funcdesc}{getMPIRankWorld}{} 1999 returns the rank of the current process within the \env{MPI_COMM_WORLD} 2000 process group. If \MPI is not used 0 is returned. 2001 \end{funcdesc} 2002 2003 \begin{funcdesc}{MPIBarrierWorld}{} 2004 performs a barrier synchronization across all processes within the 2005 \env{MPI_COMM_WORLD} process group. 2006 \end{funcdesc} 2007 2008 \begin{funcdesc}{getMPIWorldMax}{a} 2009 returns the maximum value of the integer \var{a} across all processes within 2010 \env{MPI_COMM_WORLD}. 2011 \end{funcdesc} 2012

## Properties

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