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

Annotation of /trunk/esys2/escript/py_src/util.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 108 - (hide annotations)
Thu Jan 27 06:21:59 2005 UTC (14 years, 8 months ago) by jgs
File MIME type: text/x-python
File size: 7426 byte(s)
*** empty log message ***

1 jgs 82 # $Id$
2    
3     ## @file util.py
4    
5     """
6     @brief Utility functions for escript
7     """
8    
9     import numarray
10 jgs 102 import escript
11 jgs 82 #
12 jgs 102 # escript constants (have to be consistent witj utilC.h
13 jgs 82 #
14     UNKNOWN=-1
15     EPSILON=1.e-15
16 jgs 102 Pi=numarray.pi
17 jgs 82 # some solver options:
18     NO_REORDERING=0
19     MINIMUM_FILL_IN=1
20     NESTED_DISSECTION=2
21 jgs 102 # solver methods
22 jgs 82 DEFAULT_METHOD=0
23 jgs 102 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 jgs 82 # 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 jgs 102 # functions involving the underlying Domain:
58     #
59 jgs 82 def grad(arg,where=None):
60     """
61 jgs 102 @brief returns the spatial gradient of the Data object arg
62 jgs 82
63 jgs 102 @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 jgs 82 """
67 jgs 108 if isinstance(arg,escript.Data):
68     if where==None:
69     return arg.grad()
70     else:
71     return arg.grad(where)
72 jgs 82 else:
73 jgs 108 return arg*0.
74 jgs 82
75 jgs 108 def integrate(arg,what=None):
76 jgs 82 """
77 jgs 102 @brief return the integral if the function represented by Data object arg over its domain.
78 jgs 82
79     @param arg
80     """
81 jgs 108 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 jgs 82
90     def interpolate(arg,where):
91     """
92 jgs 102 @brief interpolates the function represented by Data object arg into the FunctionSpace where.
93 jgs 82
94     @param arg
95     @param where
96     """
97 jgs 108 if isinstance(arg,escript.Data):
98     return arg.interpolate(where)
99     else:
100     return arg
101 jgs 82
102 jgs 102 # functions returning Data objects:
103    
104     def transpose(arg,axis=None):
105 jgs 82 """
106 jgs 102 @brief returns the transpose of the Data object arg.
107 jgs 82
108     @param arg
109     """
110 jgs 102 if isinstance(arg,escript.Data):
111 jgs 108 # 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 jgs 102 if axis==None: axis=arg.getRank()/2
122     return arg.transpose(axis)
123 jgs 82 else:
124 jgs 102 if axis==None: axis=arg.rank/2
125     return numarray.transpose(arg,axis=axis)
126 jgs 82
127     def trace(arg):
128     """
129     @brief
130    
131     @param arg
132     """
133 jgs 102 if isinstance(arg,escript.Data):
134 jgs 108 # 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 jgs 82 return arg.trace()
144     else:
145 jgs 102 return numarray.trace(arg)
146 jgs 82
147     def exp(arg):
148     """
149     @brief
150    
151     @param arg
152     """
153 jgs 102 if isinstance(arg,escript.Data):
154 jgs 82 return arg.exp()
155     else:
156     return numarray.exp(arg)
157    
158     def sqrt(arg):
159     """
160     @brief
161    
162     @param arg
163     """
164 jgs 102 if isinstance(arg,escript.Data):
165 jgs 88 return arg.sqrt()
166     else:
167 jgs 82 return numarray.sqrt(arg)
168    
169     def sin(arg):
170     """
171     @brief
172    
173     @param arg
174     """
175 jgs 102 if isinstance(arg,escript.Data):
176 jgs 82 return arg.sin()
177     else:
178     return numarray.sin(arg)
179    
180 jgs 102 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 jgs 82 def cos(arg):
192     """
193     @brief
194    
195     @param arg
196     """
197 jgs 102 if isinstance(arg,escript.Data):
198 jgs 82 return arg.cos()
199     else:
200     return numarray.cos(arg)
201    
202     def maxval(arg):
203     """
204     @brief
205    
206     @param arg
207     """
208 jgs 102 if isinstance(arg,escript.Data):
209     return arg.maxval()
210 jgs 108 elif isinstance(arg,float) or isinstance(arg,int):
211     return arg
212 jgs 102 else:
213     return arg.max()
214 jgs 82
215     def minval(arg):
216     """
217     @brief
218    
219     @param arg
220     """
221 jgs 102 if isinstance(arg,escript.Data):
222     return arg.minval()
223 jgs 108 elif isinstance(arg,float) or isinstance(arg,int):
224     return arg
225 jgs 102 else:
226 jgs 108 return arg.min()
227 jgs 82
228 jgs 102 def length(arg):
229     """
230     @brief
231    
232     @param arg
233     """
234     if isinstance(arg,escript.Data):
235 jgs 108 if arg.isEmpty(): return escript.Data()
236     if arg.getRank()==0:
237     return abs(arg)
238     elif arg.getRank()==1:
239 jgs 104 sum=escript.Scalar(0,arg.getFunctionSpace())
240     for i in range(arg.getShape()[0]):
241     sum+=arg[i]**2
242     return sqrt(sum)
243 jgs 108 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 jgs 104 else:
265     raise SystemError,"length is not been implemented yet"
266     # return arg.length()
267 jgs 102 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 jgs 82 def sup(arg):
292     """
293     @brief
294    
295     @param arg
296     """
297 jgs 102 if isinstance(arg,escript.Data):
298     return arg.sup()
299 jgs 108 elif isinstance(arg,float) or isinstance(arg,int):
300     return arg
301 jgs 102 else:
302     return arg.max()
303 jgs 82
304     def inf(arg):
305     """
306     @brief
307    
308     @param arg
309     """
310 jgs 102 if isinstance(arg,escript.Data):
311     return arg.inf()
312 jgs 108 elif isinstance(arg,float) or isinstance(arg,int):
313     return arg
314 jgs 102 else:
315     return arg.min()
316 jgs 82
317 jgs 102 def L2(arg):
318 jgs 82 """
319 jgs 102 @brief returns the L2-norm of the
320 jgs 82
321     @param arg
322     """
323 jgs 108 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 jgs 82
330 jgs 102 def Lsup(arg):
331 jgs 82 """
332     @brief
333    
334     @param arg
335     """
336 jgs 102 if isinstance(arg,escript.Data):
337     return arg.Lsup()
338 jgs 108 elif isinstance(arg,float) or isinstance(arg,int):
339     return abs(arg)
340 jgs 102 else:
341 jgs 108 return max(numarray.abs(arg))
342 jgs 82
343 jgs 102 def dot(arg1,arg2):
344 jgs 82 """
345     @brief
346    
347     @param arg
348     """
349 jgs 102 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