/[escript]/trunk/esys2/escript/test/python/SliceTest.py
ViewVC logotype

Annotation of /trunk/esys2/escript/test/python/SliceTest.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 102 - (hide annotations)
Wed Dec 15 07:08:39 2004 UTC (16 years, 4 months ago) by jgs
File MIME type: text/x-python
File size: 12851 byte(s)
*** empty log message ***

1 jgs 102 import sys
2     import unittest
3     import os
4    
5     esys_root=os.getenv('ESYS_ROOT')
6     sys.path.append(esys_root+'/finley/lib')
7     sys.path.append(esys_root+'/escript/lib')
8     sys.path.append(esys_root+'/escript/py_src')
9    
10     from escript import *
11     import finley
12     import numarray
13     from util import *
14    
15     """
16    
17     Tests various slicing operations on Data.
18    
19     the results are compared to reference result. if the relative difference is
20     bigger than tol an exception is raised.
21    
22     by Lutz Gross, ACcESS, University of Queensland, Australia, 2003.
23    
24     Version $Id$
25    
26     """
27    
28     from numarray import array,Float64,ones,greater
29    
30     Tag1=10
31     Tag2=11
32    
33     tol=1.E-15
34    
35     #
36     # list of arguments: a list item has the form [a0,a1,a2]
37     # what a0 is the default value and a1 is used for tag Tag1
38     # and a2 for Tag2. a0,a1,a2 are converted into numarrays.
39     #
40     # binary operations are tested on all pairs from arglist
41     #
42     # each item in the arglist are used to construct the following 5 argument
43     # types arg for arithmetic operations:
44     #
45     # 1) arg is a numarray/list a0
46     # 2) arg is a Data with default value a0
47     # 3) arg is an DataArray with constant value a0
48     # 4) arg is a Data object with constant value a0 and value a1 for tag Tag1
49     # 5) arg is a Data object with constant value a0 and value a1 for tag Tag1
50     # and value a2 for Tag2.
51     #
52     # i.e for a single binary arithmetic operation (len(arglist)*5)**2
53     # test are performed.
54     #
55    
56     arglist = [ \
57     [ [3,4], [-5,6.], [2,3] ], \
58     [ [[1,2],[3,4]], [[5,6],[7,8]], [[-5,-6],[7,8]] ], \
59     [ [[15,8],[12,8]], [[-9,9],[13,8]], [[7,34],[19,7]] ], \
60     [ [[[15,8],[12,8]],[[-9,9],[13,8]]], [[[3,4],[-9,4]],[[1,-9],[7,4]]], [[[5,2],[6,2]],[[-6,4],[7,5]]] ], \
61     [ [3.], [6.], [3] ] \
62     ]
63    
64     # these are used to test slicing:
65    
66     a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ]
67    
68     a_r1_in=[ [1./1,2,3], [-1./1,-1./2,-1./3], [1./100,1./200,1./300] ]
69    
70     a_r4=[ \
71     [ [ [[ 1,2,3],[11,12,13]], [[21,22,23],[31,32,33]], [[41,42,43],[51,52,53]] ], [ [[101,102,103],[111,112,113]], [[121,122,123],[131,132,133]], [[141,142,143],[151,152,153]] ], [ [[201,202,203],[211,212,213]], [[221,222,223],[231,232,233]], [[241,242,243],[251,252,253]] ] ], \
72     [ [ [[-1,-2,-3],[-11,-12,-13]], [[-21,-22,-23],[-31,-32,-33]], [[-41,-42,-43],[-51,-52,-53]] ], [ [[-101,-102,-103],[-111,-112,-113]], [[-121,-122,-123],[-131,-132,-133]], [[-141,-142,-143],[-151,-152,-153]] ], [ [[-201,-202,-203],[-211,-212,-213]], [[-221,-222,-223],[-231,-232,-233]], [[-241,-242,-243],[-251,-252,-253]] ] ], \
73     [ [[[11,12,13],[111,112,113]], [[121,122,123],[131,132,133]], [[141,142,143],[151,152,153]] ], [ [[1101,1102,1103],[1111,1112,1113]], [[1121,1122,1123],[1131,1132,1133]], [[1141,1142,1143],[1151,1152,1153]] ], [ [[1201,1202,1203],[1211,1212,1213]], [[1221,1222,1223],[1231,1232,1233]], [[1241,1242,1243],[1251,1252,1253]] ] ] ]
74    
75     a_r4_in=[ \
76     [ [ [[1./1,1./2,1./3],[1./11,1./12,1./13]], [[1./21,1./22,1./23],[1./31,1./32,1./33]], [[1./41,1./42,1./43],[1./51,1./52,1./53]] ], [ [[1./101,1./102,1./103],[1./111,1./112,1./113]], [[1./121,1./122,1./123],[1./131,1./132,1./133]], [[1./141,1./142,1./143],[1./151,1./152,1./153]] ], [ [[1./201,1./202,1./203],[1./211,1./212,1./213]], [[1./221,1./222,1./223],[1./231,1./232,1./233]], [[1./241,1./242,1./243],[1./251,1./252,1./253]] ] ], \
77     [ [ [[-1./1,-1./2,-1./3],[-1./11,-1./12,-1./13]], [[-1./21,-1./22,-1./23],[-1./31,-1./32,-1./33]], [[-1./41,-1./42,-1./43],[-1./51,-1./52,-1./53]] ], [ [[-1./101,-1./102,-1./103],[-1./111,-1./112,-1./113]], [[-1./121,-1./122,-1./123],[-1./131,-1./132,-1./133]], [[-1./141,-1./142,-1./143],[1./-151,-1./152,-1./153]] ], [ [[-1./201,-1./202,-1./203],[-1./211,-1./212,-1./213]], [[-1./221,-1./222,-1./223],[-1./231,-1./232,-1./233]], [[-1./241,-1./242,-1./243],[-1./251,-1./252,-1./253]] ] ], \
78     [ [ [[1./11,1./12,1./13],[1./111,1./112,1./113]], [[1./121,1./122,1./123],[1./131,1./132,1./133]], [[1./141,1./142,1./143],[1./151,1./152,1./153]] ], [ [[1./1101,1./1102,1./1103],[1./1111,1./1112,1./1113]], [[1./1121,1./1122,1./1123],[1./1131,1./1132,1./1133]], [[1./1141,1./1142,1./1143],[1./1151,1./1152,1./1153]] ], [ [[1./1201,1./1202,1./1203],[1./1211,1./1212,1./1213]], [[1./1221,1./1222,1./1223],[1./1231,1./1232,1./1233]], [[1./1241,1./1242,1./1243],[1./1251,1./1252,1./1253]] ] ] ]
79    
80    
81     def turnToArray(val,tagged):
82     if tagged=="Tagged1":
83     out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
84     elif tagged=="Tagged2":
85     out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
86     else:
87     out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
88     return out
89    
90     def prepareArg(val,ex,wh):
91     if ex=="Array":
92     out=val[0]
93     else:
94     if ex=="Expanded":
95     exx=True
96     else:
97     exx=False
98     out=Data(val[0],what=wh,expand=exx)
99     if ex=="Tagged1":
100     out.setTaggedValue(Tag1,val[1])
101     elif ex=="Tagged2":
102     out.setTaggedValue(Tag1,val[1])
103     out.setTaggedValue(Tag2,val[2])
104     return out
105    
106     def getRank(arg):
107     if isinstance(arg,Data):
108     return arg.getRank()
109     else:
110     g=array(arg)
111     if g.rank==0:
112     return 1
113     else:
114     return g.rank
115    
116     def isScalar(arg):
117     if isinstance(arg,Data):
118     if arg.getRank()==1 and arg.getShape()[0]==1:
119     return not None
120     else:
121     return None
122     else:
123     g=array(arg)
124     if g.rank==0:
125     return not None
126     else:
127     if g.rank==1 and g.shape[0]==1:
128     return not None
129     else:
130     return None
131    
132     def checkResult(text,org,res,val0,val1,val2,wh):
133     print "%s" % (text)
134     print "%s shape %s" % ("argument",org.getShape())
135     print "%s shape %s" % ("result",res.getShape())
136    
137     ref=Data(val0,what=wh,expand=False)
138     ref.setTaggedValue(Tag1,val1)
139     ref.setTaggedValue(Tag2,val2)
140     norm=Lsup(ref)+tol
141     error=Lsup(ref-res)/norm
142     print "error = %e\n" % (error)
143     if error>tol:
144     raise SystemError,"@@ %s at %s: error is too large" % (text,wh)
145    
146     #
147     # test slicing
148     #
149    
150     msh=finley.Rectangle(20,6,1)
151    
152     for wh in [ContinuousFunction(msh),Function(msh)]:
153    
154     for ex1 in ["Constant","Expanded","Tagged1","Tagged2"]:
155    
156     print "\nData type: ", ex1
157     print "******************************************\n"
158    
159     # rank 1
160     arg=prepareArg(a_r1,ex1,wh)
161     arrays=turnToArray(a_r1,ex1)
162    
163     if isinstance(arg,Data):
164    
165     checkResult("slicing: rank=1,[:]",
166     arg, \
167     arg[:], \
168     arrays[0][:], \
169     arrays[1][:], \
170     arrays[2][:], \
171     wh)
172    
173     checkResult("slicing: rank=1 [1]",
174     arg, \
175     arg[1], \
176     arrays[0][1], \
177     arrays[1][1], \
178     arrays[2][1], \
179     wh)
180    
181     checkResult("slicing: rank=1 [1:3]",
182     arg, \
183     arg[1:3], \
184     arrays[0][1:3], \
185     arrays[1][1:3], \
186     arrays[2][1:3], \
187     wh)
188    
189     checkResult("slicing: rank=1 [2:]",
190     arg, \
191     arg[2:], \
192     arrays[0][2:], \
193     arrays[1][2:], \
194     arrays[2][2:], \
195     wh)
196    
197     checkResult("slicing: rank=1 [:1]",
198     arg, \
199     arg[:1], \
200     arrays[0][:1], \
201     arrays[1][:1], \
202     arrays[2][:1], \
203     wh)
204    
205     # rank 4
206     arg=prepareArg(a_r4,ex1,wh)
207     arrays=turnToArray(a_r4,ex1)
208    
209     if isinstance(arg,Data):
210    
211     checkResult("slicing: rank=4,[:]",
212     arg, \
213     arg[:], \
214     arrays[0][:], \
215     arrays[1][:], \
216     arrays[2][:], \
217     wh)
218    
219     checkResult("slicing: rank=4 [1]",
220     arg, \
221     arg[1], \
222     arrays[0][1], \
223     arrays[1][1], \
224     arrays[2][1], \
225     wh)
226    
227     checkResult("slicing: rank=4 [1,1,1,1]",
228     arg, \
229     arg[1,1,1,1], \
230     arrays[0][1,1,1,1], \
231     arrays[1][1,1,1,1], \
232     arrays[2][1,1,1,1], \
233     wh)
234    
235     checkResult("slicing: rank=4 [1:3]",
236     arg, \
237     arg[1:3], \
238     arrays[0][1:3], \
239     arrays[1][1:3], \
240     arrays[2][1:3], \
241     wh)
242    
243     checkResult("slicing: rank=4 [:2]",
244     arg, \
245     arg[:2], \
246     arrays[0][:2], \
247     arrays[1][:2], \
248     arrays[2][:2], \
249     wh)
250    
251     checkResult("slicing: rank=4 [1:]",
252     arg, \
253     arg[1:], \
254     arrays[0][1:], \
255     arrays[1][1:], \
256     arrays[2][1:], \
257     wh)
258    
259     checkResult("slicing: rank=4,[:,:]",
260     arg, \
261     arg[:,:], \
262     arrays[0][:,:], \
263     arrays[1][:,:], \
264     arrays[2][:,:], \
265     wh)
266    
267     checkResult("slicing: rank=4 [:,1]",
268     arg, \
269     arg[:,1], \
270     arrays[0][:,1], \
271     arrays[1][:,1], \
272     arrays[2][:,1], \
273     wh)
274    
275     checkResult("slicing: rank=4 [:,1:3]",
276     arg, \
277     arg[:,1:3], \
278     arrays[0][:,1:3], \
279     arrays[1][:,1:3], \
280     arrays[2][:,1:3], \
281     wh)
282    
283     checkResult("slicing: rank=4 [1:2,1:3]",
284     arg, \
285     arg[1:2,1:3], \
286     arrays[0][1:2,1:3], \
287     arrays[1][1:2,1:3], \
288     arrays[2][1:2,1:3], \
289     wh)
290    
291     checkResult("slicing: rank=4 [:,1:2,1:2]",
292     arg, \
293     arg[:,1:2,1:2], \
294     arrays[0][:,1:2,1:2], \
295     arrays[1][:,1:2,1:2], \
296     arrays[2][:,1:2,1:2], \
297     wh)
298    
299     checkResult("slicing: rank=4 [:,:,1:2,1:3]",
300     arg, \
301     arg[:,:,1:2,1:3], \
302     arrays[0][:,:,1:2,1:3], \
303     arrays[1][:,:,1:2,1:3], \
304     arrays[2][:,:,1:2,1:3], \
305     wh)
306    
307     checkResult("slicing: rank=4 [1:2,1:3,1,1]",
308     arg, \
309     arg[1:2,1:3,1,1], \
310     arrays[0][1:2,1:3,1,1], \
311     arrays[1][1:2,1:3,1,1], \
312     arrays[2][1:2,1:3,1,1], \
313     wh)
314    
315     checkResult("slicing: rank=4 [1:2,1:3,:,1] ",
316     arg, \
317     arg[1:2,1:3,:,1], \
318     arrays[0][1:2,1:3,:,1], \
319     arrays[1][1:2,1:3,:,1], \
320     arrays[2][1:2,1:3,:,1], \
321     wh)
322    
323     checkResult("slicing: rank=4 [1:2,1:3,0,:] ",
324     arg, \
325     arg[1:2,1:3,0,:], \
326     arrays[0][1:2,1:3,0,:], \
327     arrays[1][1:2,1:3,0,:], \
328     arrays[2][1:2,1:3,0,:], \
329     wh)
330    
331     checkResult("slicing: rank=4 [1:2,1:3,0,1] ",
332     arg, \
333     arg[1:2,1:3,0,1], \
334     arrays[0][1:2,1:3,0,1], \
335     arrays[1][1:2,1:3,0,1], \
336     arrays[2][1:2,1:3,0,1], \
337     wh)
338    
339     checkResult("slicing: rank=4 [1,1,0,1] ",
340     arg, \
341     arg[1,1,0,1], \
342     arrays[0][1,1,0,1], \
343     arrays[1][1,1,0,1], \
344     arrays[2][1,1,0,1], \
345     wh)

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26