1 |
# $Id$ |
2 |
|
3 |
## @file util.py |
4 |
|
5 |
""" |
6 |
@brief Utility functions for escript |
7 |
""" |
8 |
|
9 |
import numarray |
10 |
import escript |
11 |
# |
12 |
# escript constants (have to be consistent witj utilC.h |
13 |
# |
14 |
UNKNOWN=-1 |
15 |
EPSILON=1.e-15 |
16 |
Pi=numarray.pi |
17 |
# some solver options: |
18 |
NO_REORDERING=0 |
19 |
MINIMUM_FILL_IN=1 |
20 |
NESTED_DISSECTION=2 |
21 |
# solver methods |
22 |
DEFAULT_METHOD=0 |
23 |
DIRECT=1 |
24 |
CHOLEVSKY=2 |
25 |
PCG=3 |
26 |
CR=4 |
27 |
CGS=5 |
28 |
BICGSTAB=6 |
29 |
SSOR=7 |
30 |
ILU0=8 |
31 |
ILUT=9 |
32 |
JACOBI=10 |
33 |
GMRES=11 |
34 |
PRES20=12 |
35 |
|
36 |
METHOD_KEY="method" |
37 |
SYMMETRY_KEY="symmetric" |
38 |
TOLERANCE_KEY="tolerance" |
39 |
|
40 |
# supported file formats: |
41 |
VRML=1 |
42 |
PNG=2 |
43 |
JPEG=3 |
44 |
JPG=3 |
45 |
PS=4 |
46 |
OOGL=5 |
47 |
BMP=6 |
48 |
TIFF=7 |
49 |
OPENINVENTOR=8 |
50 |
RENDERMAN=9 |
51 |
PNM=10 |
52 |
# |
53 |
# wrapper for various functions: if the argument has attribute the function name |
54 |
# as an argument it calls the correspong methods. Otherwise the coresponsing numarray |
55 |
# function is called. |
56 |
# |
57 |
# functions involving the underlying Domain: |
58 |
# |
59 |
def grad(arg,where=None): |
60 |
""" |
61 |
@brief returns the spatial gradient of the Data object arg |
62 |
|
63 |
@param arg: Data object representing the function which gradient to be calculated. |
64 |
@param where: FunctionSpace in which the gradient will be. If None Function(dom) where dom is the |
65 |
domain of the Data object arg. |
66 |
""" |
67 |
if where==None: |
68 |
return arg.grad() |
69 |
else: |
70 |
return arg.grad(where) |
71 |
|
72 |
def integrate(arg): |
73 |
""" |
74 |
@brief return the integral if the function represented by Data object arg over its domain. |
75 |
|
76 |
@param arg |
77 |
""" |
78 |
return arg.integrate() |
79 |
|
80 |
def interpolate(arg,where): |
81 |
""" |
82 |
@brief interpolates the function represented by Data object arg into the FunctionSpace where. |
83 |
|
84 |
@param arg |
85 |
@param where |
86 |
""" |
87 |
return arg.interpolate(where) |
88 |
|
89 |
# functions returning Data objects: |
90 |
|
91 |
def transpose(arg,axis=None): |
92 |
""" |
93 |
@brief returns the transpose of the Data object arg. |
94 |
|
95 |
@param arg |
96 |
""" |
97 |
if isinstance(arg,escript.Data): |
98 |
if axis==None: axis=arg.getRank()/2 |
99 |
return arg.transpose(axis) |
100 |
else: |
101 |
if axis==None: axis=arg.rank/2 |
102 |
return numarray.transpose(arg,axis=axis) |
103 |
|
104 |
def trace(arg): |
105 |
""" |
106 |
@brief |
107 |
|
108 |
@param arg |
109 |
""" |
110 |
if isinstance(arg,escript.Data): |
111 |
return arg.trace() |
112 |
else: |
113 |
return numarray.trace(arg) |
114 |
|
115 |
def exp(arg): |
116 |
""" |
117 |
@brief |
118 |
|
119 |
@param arg |
120 |
""" |
121 |
if isinstance(arg,escript.Data): |
122 |
return arg.exp() |
123 |
else: |
124 |
return numarray.exp(arg) |
125 |
|
126 |
def sqrt(arg): |
127 |
""" |
128 |
@brief |
129 |
|
130 |
@param arg |
131 |
""" |
132 |
if isinstance(arg,escript.Data): |
133 |
return arg.sqrt() |
134 |
else: |
135 |
return numarray.sqrt(arg) |
136 |
|
137 |
def sin(arg): |
138 |
""" |
139 |
@brief |
140 |
|
141 |
@param arg |
142 |
""" |
143 |
if isinstance(arg,escript.Data): |
144 |
return arg.sin() |
145 |
else: |
146 |
return numarray.sin(arg) |
147 |
|
148 |
def tan(arg): |
149 |
""" |
150 |
@brief |
151 |
|
152 |
@param arg |
153 |
""" |
154 |
if isinstance(arg,escript.Data): |
155 |
return arg.tan() |
156 |
else: |
157 |
return numarray.tan(arg) |
158 |
|
159 |
def cos(arg): |
160 |
""" |
161 |
@brief |
162 |
|
163 |
@param arg |
164 |
""" |
165 |
if isinstance(arg,escript.Data): |
166 |
return arg.cos() |
167 |
else: |
168 |
return numarray.cos(arg) |
169 |
|
170 |
def maxval(arg): |
171 |
""" |
172 |
@brief |
173 |
|
174 |
@param arg |
175 |
""" |
176 |
if isinstance(arg,escript.Data): |
177 |
return arg.maxval() |
178 |
else: |
179 |
return arg.max() |
180 |
|
181 |
def minval(arg): |
182 |
""" |
183 |
@brief |
184 |
|
185 |
@param arg |
186 |
""" |
187 |
if isinstance(arg,escript.Data): |
188 |
return arg.minval() |
189 |
else: |
190 |
return arg.max() |
191 |
|
192 |
def length(arg): |
193 |
""" |
194 |
@brief |
195 |
|
196 |
@param arg |
197 |
""" |
198 |
if isinstance(arg,escript.Data): |
199 |
return arg.length() |
200 |
else: |
201 |
return sqrt((arg**2).sum()) |
202 |
|
203 |
def sign(arg): |
204 |
""" |
205 |
@brief |
206 |
|
207 |
@param arg |
208 |
""" |
209 |
if isinstance(arg,escript.Data): |
210 |
return arg.sign() |
211 |
else: |
212 |
return numarray.greater(arg,numarray.zeros(arg.shape))-numarray.less(arg,numarray.zeros(arg.shape)) |
213 |
|
214 |
# reduction operations: |
215 |
|
216 |
def sum(arg): |
217 |
""" |
218 |
@brief |
219 |
|
220 |
@param arg |
221 |
""" |
222 |
return arg.sum() |
223 |
|
224 |
def sup(arg): |
225 |
""" |
226 |
@brief |
227 |
|
228 |
@param arg |
229 |
""" |
230 |
if isinstance(arg,escript.Data): |
231 |
return arg.sup() |
232 |
else: |
233 |
return arg.max() |
234 |
|
235 |
def inf(arg): |
236 |
""" |
237 |
@brief |
238 |
|
239 |
@param arg |
240 |
""" |
241 |
if isinstance(arg,escript.Data): |
242 |
return arg.inf() |
243 |
else: |
244 |
return arg.min() |
245 |
|
246 |
def L2(arg): |
247 |
""" |
248 |
@brief returns the L2-norm of the |
249 |
|
250 |
@param arg |
251 |
""" |
252 |
return arg.L2() |
253 |
|
254 |
def Lsup(arg): |
255 |
""" |
256 |
@brief |
257 |
|
258 |
@param arg |
259 |
""" |
260 |
if isinstance(arg,escript.Data): |
261 |
return arg.Lsup() |
262 |
else: |
263 |
return arg.max(numarray.abs(arg)) |
264 |
|
265 |
def dot(arg1,arg2): |
266 |
""" |
267 |
@brief |
268 |
|
269 |
@param arg |
270 |
""" |
271 |
if isinstance(arg1,escript.Data): |
272 |
return arg1.dot(arg2) |
273 |
elif isinstance(arg1,escript.Data): |
274 |
return arg2.dot(arg1) |
275 |
else: |
276 |
return numarray.dot(arg1,arg2) |
277 |
# |
278 |
# $Log$ |
279 |
# Revision 1.3 2004/12/14 05:39:26 jgs |
280 |
# *** empty log message *** |
281 |
# |
282 |
# Revision 1.2.2.4 2004/12/07 03:19:51 gross |
283 |
# options for GMRES and PRES20 added |
284 |
# |
285 |
# Revision 1.2.2.3 2004/12/06 04:55:18 gross |
286 |
# function wraper extended |
287 |
# |
288 |
# Revision 1.2.2.2 2004/11/22 05:44:07 gross |
289 |
# a few more unitary functions have been added but not implemented in Data yet |
290 |
# |
291 |
# Revision 1.2.2.1 2004/11/12 06:58:15 gross |
292 |
# a lot of changes to get the linearPDE class running: most important change is that there is no matrix format exposed to the user anymore. the format is chosen by the Domain according to the solver and symmetry |
293 |
# |
294 |
# Revision 1.2 2004/10/27 00:23:36 jgs |
295 |
# fixed minor syntax error |
296 |
# |
297 |
# Revision 1.1.1.1 2004/10/26 06:53:56 jgs |
298 |
# initial import of project esys2 |
299 |
# |
300 |
# Revision 1.1.2.3 2004/10/26 06:43:48 jgs |
301 |
# committing Lutz's and Paul's changes to brach jgs |
302 |
# |
303 |
# Revision 1.1.4.1 2004/10/20 05:32:51 cochrane |
304 |
# Added incomplete Doxygen comments to files, or merely put the docstrings that already exist into Doxygen form. |
305 |
# |
306 |
# Revision 1.1 2004/08/05 03:58:27 gross |
307 |
# Bug in Assemble_NodeCoordinates fixed |
308 |
# |
309 |
# |