/[escript]/trunk/escript/py_src/util.py
ViewVC logotype

Contents of /trunk/escript/py_src/util.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 108 - (show annotations)
Thu Jan 27 06:21:59 2005 UTC (14 years, 8 months ago) by jgs
Original Path: trunk/esys2/escript/py_src/util.py
File MIME type: text/x-python
File size: 7426 byte(s)
*** empty log message ***

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 isinstance(arg,escript.Data):
68 if where==None:
69 return arg.grad()
70 else:
71 return arg.grad(where)
72 else:
73 return arg*0.
74
75 def integrate(arg,what=None):
76 """
77 @brief return the integral if the function represented by Data object arg over its domain.
78
79 @param arg
80 """
81 if not what==None:
82 arg2=escript.Data(arg,what)
83 else:
84 arg2=arg
85 if arg2.getRank()==0:
86 return arg2.integrate()[0]
87 else:
88 return arg2.integrate()
89
90 def interpolate(arg,where):
91 """
92 @brief interpolates the function represented by Data object arg into the FunctionSpace where.
93
94 @param arg
95 @param where
96 """
97 if isinstance(arg,escript.Data):
98 return arg.interpolate(where)
99 else:
100 return arg
101
102 # functions returning Data objects:
103
104 def transpose(arg,axis=None):
105 """
106 @brief returns the transpose of the Data object arg.
107
108 @param arg
109 """
110 if isinstance(arg,escript.Data):
111 # hack for transpose
112 r=arg.getRank()
113 if r!=2: raise ValueError,"Tranpose only avalaible for rank 2 objects"
114 s=arg.getShape()
115 out=escript.Data(0.,(s[1],s[0]),arg.getFunctionSpace())
116 for i in range(s[0]):
117 for j in range(s[1]):
118 out[j,i]=arg[i,j]
119 return out
120 # end hack for transpose
121 if axis==None: axis=arg.getRank()/2
122 return arg.transpose(axis)
123 else:
124 if axis==None: axis=arg.rank/2
125 return numarray.transpose(arg,axis=axis)
126
127 def trace(arg):
128 """
129 @brief
130
131 @param arg
132 """
133 if isinstance(arg,escript.Data):
134 # hack for trace
135 r=arg.getRank()
136 if r!=2: raise ValueError,"trace only avalaible for rank 2 objects"
137 s=arg.getShape()
138 out=escript.Scalar(0,arg.getFunctionSpace())
139 for i in range(min(s)):
140 out+=arg[i,i]
141 return out
142 # end hack for trace
143 return arg.trace()
144 else:
145 return numarray.trace(arg)
146
147 def exp(arg):
148 """
149 @brief
150
151 @param arg
152 """
153 if isinstance(arg,escript.Data):
154 return arg.exp()
155 else:
156 return numarray.exp(arg)
157
158 def sqrt(arg):
159 """
160 @brief
161
162 @param arg
163 """
164 if isinstance(arg,escript.Data):
165 return arg.sqrt()
166 else:
167 return numarray.sqrt(arg)
168
169 def sin(arg):
170 """
171 @brief
172
173 @param arg
174 """
175 if isinstance(arg,escript.Data):
176 return arg.sin()
177 else:
178 return numarray.sin(arg)
179
180 def tan(arg):
181 """
182 @brief
183
184 @param arg
185 """
186 if isinstance(arg,escript.Data):
187 return arg.tan()
188 else:
189 return numarray.tan(arg)
190
191 def cos(arg):
192 """
193 @brief
194
195 @param arg
196 """
197 if isinstance(arg,escript.Data):
198 return arg.cos()
199 else:
200 return numarray.cos(arg)
201
202 def maxval(arg):
203 """
204 @brief
205
206 @param arg
207 """
208 if isinstance(arg,escript.Data):
209 return arg.maxval()
210 elif isinstance(arg,float) or isinstance(arg,int):
211 return arg
212 else:
213 return arg.max()
214
215 def minval(arg):
216 """
217 @brief
218
219 @param arg
220 """
221 if isinstance(arg,escript.Data):
222 return arg.minval()
223 elif isinstance(arg,float) or isinstance(arg,int):
224 return arg
225 else:
226 return arg.min()
227
228 def length(arg):
229 """
230 @brief
231
232 @param arg
233 """
234 if isinstance(arg,escript.Data):
235 if arg.isEmpty(): return escript.Data()
236 if arg.getRank()==0:
237 return abs(arg)
238 elif arg.getRank()==1:
239 sum=escript.Scalar(0,arg.getFunctionSpace())
240 for i in range(arg.getShape()[0]):
241 sum+=arg[i]**2
242 return sqrt(sum)
243 elif arg.getRank()==2:
244 sum=escript.Scalar(0,arg.getFunctionSpace())
245 for i in range(arg.getShape()[0]):
246 for j in range(arg.getShape()[1]):
247 sum+=arg[i,j]**2
248 return sqrt(sum)
249 elif arg.getRank()==3:
250 sum=escript.Scalar(0,arg.getFunctionSpace())
251 for i in range(arg.getShape()[0]):
252 for j in range(arg.getShape()[1]):
253 for k in range(arg.getShape()[2]):
254 sum+=arg[i,j,k]**2
255 return sqrt(sum)
256 elif arg.getRank()==4:
257 sum=escript.Scalar(0,arg.getFunctionSpace())
258 for i in range(arg.getShape()[0]):
259 for j in range(arg.getShape()[1]):
260 for k in range(arg.getShape()[2]):
261 for l in range(arg.getShape()[3]):
262 sum+=arg[i,j,k,l]**2
263 return sqrt(sum)
264 else:
265 raise SystemError,"length is not been implemented yet"
266 # return arg.length()
267 else:
268 return sqrt((arg**2).sum())
269
270 def sign(arg):
271 """
272 @brief
273
274 @param arg
275 """
276 if isinstance(arg,escript.Data):
277 return arg.sign()
278 else:
279 return numarray.greater(arg,numarray.zeros(arg.shape))-numarray.less(arg,numarray.zeros(arg.shape))
280
281 # reduction operations:
282
283 def sum(arg):
284 """
285 @brief
286
287 @param arg
288 """
289 return arg.sum()
290
291 def sup(arg):
292 """
293 @brief
294
295 @param arg
296 """
297 if isinstance(arg,escript.Data):
298 return arg.sup()
299 elif isinstance(arg,float) or isinstance(arg,int):
300 return arg
301 else:
302 return arg.max()
303
304 def inf(arg):
305 """
306 @brief
307
308 @param arg
309 """
310 if isinstance(arg,escript.Data):
311 return arg.inf()
312 elif isinstance(arg,float) or isinstance(arg,int):
313 return arg
314 else:
315 return arg.min()
316
317 def L2(arg):
318 """
319 @brief returns the L2-norm of the
320
321 @param arg
322 """
323 if isinstance(arg,escript.Data):
324 return arg.L2()
325 elif isinstance(arg,float) or isinstance(arg,int):
326 return abs(arg)
327 else:
328 return numarry.sqrt(dot(arg,arg))
329
330 def Lsup(arg):
331 """
332 @brief
333
334 @param arg
335 """
336 if isinstance(arg,escript.Data):
337 return arg.Lsup()
338 elif isinstance(arg,float) or isinstance(arg,int):
339 return abs(arg)
340 else:
341 return max(numarray.abs(arg))
342
343 def dot(arg1,arg2):
344 """
345 @brief
346
347 @param arg
348 """
349 if isinstance(arg1,escript.Data):
350 return arg1.dot(arg2)
351 elif isinstance(arg1,escript.Data):
352 return arg2.dot(arg1)
353 else:
354 return numarray.dot(arg1,arg2)

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26