/[escript]/trunk/escript/test/python/SliceSetting.py
ViewVC logotype

Annotation of /trunk/escript/test/python/SliceSetting.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 155 - (hide annotations)
Wed Nov 9 02:02:19 2005 UTC (14 years, 8 months ago) by jgs
File MIME type: text/x-python
File size: 15899 byte(s)
move all directories from trunk/esys2 into trunk and remove esys2

1 jgs 102 import sys
2     import unittest
3     import os
4    
5 jgs 149 from esys.escript import *
6 jgs 153 from esys import bruce
7 jgs 102
8     import numarray
9    
10     """
11    
12     Test slice setting for Data objects.
13    
14     Version $Id$
15    
16     """
17    
18     from numarray import array,Float64,ones,greater
19    
20     Tag1=10
21     Tag2=11
22    
23     tol=1.E-15
24    
25     #
26     # list of arguments: a list item has the form [a0,a1,a2]
27     # what a0 is the default value and a1 is used for tag Tag1
28     # and a2 for tag2. a0,a1,a2 are converted into numarrays.
29     #
30    
31     arglist = [ \
32     [ [3,4], [-5,6.], [2,3] ], \
33     [ [[1,2],[3,4]], [[5,6],[7,8]], [[-5,-6],[7,8]] ], \
34     [ [[15,8],[12,8]], [[-9,9],[13,8]], [[7,34],[19,7]] ], \
35     [ [[[15,8],[12,8]],[[-9,9],[13,8]]], [[[3,4],[-9,4]],[[1,-9],[7,4]]], [[[5,2],[6,2]],[[-6,4],[7,5]]] ], \
36     [ 3.0, 6.0, 3 ] \
37     ]
38    
39     # these are used to test slicing:
40     a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ]
41     a_r1_in=[ [1./1,2,3], [-1./1,-1./2,-1./3], [1./100,1./200,1./300] ]
42     a_r4=[ \
43     [ [ [[ 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]] ] ], \
44     [ [ [[ -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]] ] ], \
45     [ [[[ 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]] ] ] ]
46     a_r4_in=[ \
47     [ [ [[ 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]] ] ], \
48     [ [ [[ -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]] ] ], \
49     [ [[[ 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]] ] ] ]
50    
51     def turnToArray(val,tagged):
52     if tagged=="Tagged1":
53     out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
54     elif tagged=="Tagged2":
55     out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
56     else:
57     out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
58     return out
59    
60     def prepareArg(val,ex,wh):
61     if ex=="Array":
62     out=val[0]
63     else:
64     if ex=="Expanded":
65     exx=True
66     else:
67     exx=False
68     out=Data(val[0],what=wh,expand=exx)
69     if ex=="Tagged1":
70     out.setTaggedValue(Tag1,val[1])
71     elif ex=="Tagged2":
72     out.setTaggedValue(Tag1,val[1])
73     out.setTaggedValue(Tag2,val[2])
74     return out
75    
76     def checkResult(text,res,val0,val1,val2,wh):
77     ref=Data(val0,what=wh,expand=False)
78     ref.setTaggedValue(Tag1,val1)
79     ref.setTaggedValue(Tag2,val2)
80     norm=Lsup(ref)+tol
81     error=Lsup(ref-res)/norm
82     print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error)
83     if error>tol:
84     print "**** %s: error is too large"%(text)
85 jgs 147 raise SystemError,"@@ %s: error is too large"%(text)
86     sys.exit(1)
87 jgs 102
88     def getRank(arg):
89     if isinstance(arg,Data):
90     return arg.getRank()
91     else:
92     g=array(arg)
93     if g.rank==0:
94     return 1
95     else:
96     return g.rank
97    
98     def isScalar(arg):
99     if isinstance(arg,Data):
100     if arg.getRank()==1 and arg.getShape()[0]==1:
101     return not None
102     else:
103     return None
104     else:
105     g=array(arg)
106     if g.rank==0:
107     return not None
108     else:
109     if g.rank==1 and g.shape[0]==1:
110     return not None
111     else:
112     return None
113    
114     #
115     # ==============================================================
116     #
117     # test slice setting:
118     #
119    
120 jgs 153 msh=bruce.Rectangle(20,6)
121 jgs 102 for wh in [ContinuousFunction(msh),Function(msh)]:
122    
123     print wh
124    
125 jgs 121 for ex1 in ["Constant","Expanded"]:
126 jgs 102
127 jgs 121 for ex2 in ["Array","Constant","Expanded"]:
128 jgs 102
129     print "Slice setting: ", ex1, ",", ex2, ":"
130    
131     # rank 1
132    
133     arrays_in=turnToArray(a_r1_in,ex2)
134    
135     arg2=prepareArg(a_r1,ex1,wh)
136     arrays2=turnToArray(a_r1,ex1)
137     a_in=[arrays_in[0][:], \
138     arrays_in[1][:], \
139     arrays_in[2][:]]
140     expArg=prepareArg(a_in,ex2,wh)
141     arg2[:]=expArg
142     arrays2[0][:]=a_in[0]
143     arrays2[1][:]=a_in[1]
144     arrays2[2][:]=a_in[2]
145     checkResult("slicing, set: rank=1 [:] "+ex1+","+ex2, \
146     arg2, \
147     arrays2[0], \
148     arrays2[1], \
149     arrays2[2], \
150     wh)
151    
152     arg2=prepareArg(a_r1,ex1,wh)
153     arrays2=turnToArray(a_r1,ex1)
154     a_in=[arrays_in[0][1], \
155     arrays_in[1][1], \
156     arrays_in[2][1]]
157     expArg=prepareArg(a_in,ex2,wh)
158     arg2[1]=expArg
159     arrays2[0][1]=a_in[0]
160     arrays2[1][1]=a_in[1]
161     arrays2[2][1]=a_in[2]
162     checkResult("slicing, set: rank=1 [1] "+ex1+","+ex2, \
163     arg2, \
164     arrays2[0], \
165     arrays2[1], \
166     arrays2[2], \
167     wh)
168    
169     arg2=prepareArg(a_r1,ex1,wh)
170     arrays2=turnToArray(a_r1,ex1)
171     a_in=[arrays_in[0][1:3], \
172     arrays_in[1][1:3], \
173     arrays_in[2][1:3]]
174     expArg=prepareArg(a_in,ex2,wh)
175     arg2[1:3]=expArg
176     arrays2[0][1:3]=a_in[0]
177     arrays2[1][1:3]=a_in[1]
178     arrays2[2][1:3]=a_in[2]
179     checkResult("slicing, set: rank=1 [1:3] "+ex1+","+ex2, \
180     arg2, \
181     arrays2[0], \
182     arrays2[1], \
183     arrays2[2], \
184     wh)
185    
186     arg2=prepareArg(a_r1,ex1,wh)
187     arrays2=turnToArray(a_r1,ex1)
188     a_in=[arrays_in[0][:2], \
189     arrays_in[1][:2], \
190     arrays_in[2][:2]]
191     expArg=prepareArg(a_in,ex2,wh)
192     arg2[:2]=expArg
193     arrays2[0][:2]=a_in[0]
194     arrays2[1][:2]=a_in[1]
195     arrays2[2][:2]=a_in[2]
196     checkResult("slicing, set: rank=1 [:2] "+ex1+","+ex2, \
197     arg2, \
198     arrays2[0], \
199     arrays2[1], \
200     arrays2[2], \
201     wh)
202    
203     arg2=prepareArg(a_r1,ex1,wh)
204     arrays2=turnToArray(a_r1,ex1)
205     a_in=[arrays_in[0][2:], \
206     arrays_in[1][2:], \
207     arrays_in[2][2:]]
208     expArg=prepareArg(a_in,ex2,wh)
209     arg2[2:]=expArg
210     arrays2[0][2:]=a_in[0]
211     arrays2[1][2:]=a_in[1]
212     arrays2[2][2:]=a_in[2]
213     checkResult("slicing, set: rank=1 [2:] "+ex1+","+ex2, \
214     arg2, \
215     arrays2[0], \
216     arrays2[1], \
217     arrays2[2], \
218     wh)
219    
220     # rank 4
221    
222     arrays_in=turnToArray(a_r4_in,ex2)
223    
224     arg2=prepareArg(a_r4,ex1,wh)
225     arrays2=turnToArray(a_r4,ex1)
226     a_in=[arrays_in[0][:], \
227     arrays_in[1][:], \
228     arrays_in[2][:]]
229     expArg=prepareArg(a_in,ex2,wh)
230     arg2[:]=expArg
231     arrays2[0][:]=a_in[0]
232     arrays2[1][:]=a_in[1]
233     arrays2[2][:]=a_in[2]
234     checkResult("slicing, set: rank=4 [:] "+ex1+","+ex2, \
235     arg2, \
236     arrays2[0], \
237     arrays2[1], \
238     arrays2[2], \
239     wh)
240    
241     arg2=prepareArg(a_r4,ex1,wh)
242     arrays2=turnToArray(a_r4,ex1)
243     a_in=[arrays_in[0][1], \
244     arrays_in[1][1], \
245     arrays_in[2][1]]
246     expArg=prepareArg(a_in,ex2,wh)
247     arg2[1]=expArg
248     arrays2[0][1]=a_in[0]
249     arrays2[1][1]=a_in[1]
250     arrays2[2][1]=a_in[2]
251     checkResult("slicing, set: rank=4 [1] "+ex1+","+ex2, \
252     arg2, \
253     arrays2[0], \
254     arrays2[1], \
255     arrays2[2], \
256     wh)
257    
258     arg2=prepareArg(a_r4,ex1,wh)
259     arrays2=turnToArray(a_r4,ex1)
260     a_in=[arrays_in[0][1:3], \
261     arrays_in[1][1:3], \
262     arrays_in[2][1:3]]
263     expArg=prepareArg(a_in,ex2,wh)
264     arg2[1:3]=expArg
265     arrays2[0][1:3]=a_in[0]
266     arrays2[1][1:3]=a_in[1]
267     arrays2[2][1:3]=a_in[2]
268     checkResult("slicing, set: rank=4 [1:3] "+ex1+","+ex2, \
269     arg2, \
270     arrays2[0], \
271     arrays2[1], \
272     arrays2[2], \
273     wh)
274    
275     arg2=prepareArg(a_r4,ex1,wh)
276     arrays2=turnToArray(a_r4,ex1)
277     a_in=[arrays_in[0][:,:], \
278     arrays_in[1][:,:], \
279     arrays_in[2][:,:]]
280     expArg=prepareArg(a_in,ex2,wh)
281     arg2[:,:]=expArg
282     arrays2[0][:,:]=a_in[0]
283     arrays2[1][:,:]=a_in[1]
284     arrays2[2][:,:]=a_in[2]
285     checkResult("slicing, set: rank=4 [:,:] "+ex1+","+ex2, \
286     arg2, \
287     arrays2[0], \
288     arrays2[1], \
289     arrays2[2], \
290     wh)
291    
292     arg2=prepareArg(a_r4,ex1,wh)
293     arrays2=turnToArray(a_r4,ex1)
294     a_in=[arrays_in[0][:,1], \
295     arrays_in[1][:,1], \
296     arrays_in[2][:,1]]
297     expArg=prepareArg(a_in,ex2,wh)
298     arg2[:,1]=expArg
299     arrays2[0][:,1]=a_in[0]
300     arrays2[1][:,1]=a_in[1]
301     arrays2[2][:,1]=a_in[2]
302     checkResult("slicing, set: rank=4 [:,1] "+ex1+","+ex2, \
303     arg2, \
304     arrays2[0], \
305     arrays2[1], \
306     arrays2[2], \
307     wh)
308    
309     arg2=prepareArg(a_r4,ex1,wh)
310     arrays2=turnToArray(a_r4,ex1)
311     a_in=[arrays_in[0][:,1:3], \
312     arrays_in[1][:,1:3], \
313     arrays_in[2][:,1:3]]
314     expArg=prepareArg(a_in,ex2,wh)
315     arg2[:,1:3]=expArg
316     arrays2[0][:,1:3]=a_in[0]
317     arrays2[1][:,1:3]=a_in[1]
318     arrays2[2][:,1:3]=a_in[2]
319     checkResult("slicing, set: rank=4 [:,1:3] "+ex1+","+ex2, \
320     arg2, \
321     arrays2[0], \
322     arrays2[1], \
323     arrays2[2], \
324     wh)
325    
326     arg2=prepareArg(a_r4,ex1,wh)
327     arrays2=turnToArray(a_r4,ex1)
328     a_in=[arrays_in[0][1:2,1:3], \
329     arrays_in[1][1:2,1:3], \
330     arrays_in[2][1:2,1:3]]
331     expArg=prepareArg(a_in,ex2,wh)
332     arg2[1:2,1:3]=expArg
333     arrays2[0][1:2,1:3]=a_in[0]
334     arrays2[1][1:2,1:3]=a_in[1]
335     arrays2[2][1:2,1:3]=a_in[2]
336     checkResult("slicing, set: rank=4 [1:2,1:3] "+ex1+","+ex2, \
337     arg2, \
338     arrays2[0], \
339     arrays2[1], \
340     arrays2[2], \
341     wh)
342    
343     arg2=prepareArg(a_r4,ex1,wh)
344     arrays2=turnToArray(a_r4,ex1)
345     a_in=[arrays_in[0][1:2,1:3,1,1], \
346     arrays_in[1][1:2,1:3,1,1], \
347     arrays_in[2][1:2,1:3,1,1]]
348     expArg=prepareArg(a_in,ex2,wh)
349     arg2[1:2,1:3,1,1]=expArg
350     arrays2[0][1:2,1:3,1,1]=a_in[0]
351     arrays2[1][1:2,1:3,1,1]=a_in[1]
352     arrays2[2][1:2,1:3,1,1]=a_in[2]
353     checkResult("slicing, set: rank=4 [1:2,1:3,1,1] "+ex1+","+ex2, \
354     arg2, \
355     arrays2[0], \
356     arrays2[1], \
357     arrays2[2], \
358     wh)
359    
360     arg2=prepareArg(a_r4,ex1,wh)
361     arrays2=turnToArray(a_r4,ex1)
362     a_in=[arrays_in[0][1:2,1:3,:,1], \
363     arrays_in[1][1:2,1:3,:,1], \
364     arrays_in[2][1:2,1:3,:,1]]
365     expArg=prepareArg(a_in,ex2,wh)
366     arg2[1:2,1:3,:,1]=expArg
367     arrays2[0][1:2,1:3,:,1]=a_in[0]
368     arrays2[1][1:2,1:3,:,1]=a_in[1]
369     arrays2[2][1:2,1:3,:,1]=a_in[2]
370     checkResult("slicing, set: rank=4 [1:2,1:3,:,1] "+ex1+","+ex2, \
371     arg2, \
372     arrays2[0], \
373     arrays2[1], \
374     arrays2[2], \
375     wh)
376    
377     arg2=prepareArg(a_r4,ex1,wh)
378     arrays2=turnToArray(a_r4,ex1)
379     a_in=[arrays_in[0][1:2,1:3,0,:], \
380     arrays_in[1][1:2,1:3,0,:], \
381     arrays_in[2][1:2,1:3,0,:]]
382     expArg=prepareArg(a_in,ex2,wh)
383     arg2[1:2,1:3,0,:]=expArg
384     arrays2[0][1:2,1:3,0,:]=a_in[0]
385     arrays2[1][1:2,1:3,0,:]=a_in[1]
386     arrays2[2][1:2,1:3,0,:]=a_in[2]
387     checkResult("slicing, set: rank=4 [1:2,1:3,0,:] "+ex1+","+ex2, \
388     arg2, \
389     arrays2[0], \
390     arrays2[1], \
391     arrays2[2], \
392     wh)
393    
394     arg2=prepareArg(a_r4,ex1,wh)
395     arrays2=turnToArray(a_r4,ex1)
396     a_in=[arrays_in[0][1:2,1:3,0,1], \
397     arrays_in[1][1:2,1:3,0,1], \
398     arrays_in[2][1:2,1:3,0,1]]
399     expArg=prepareArg(a_in,ex2,wh)
400     arg2[1:2,1:3,0,1]=expArg
401     arrays2[0][1:2,1:3,0,1]=a_in[0]
402     arrays2[1][1:2,1:3,0,1]=a_in[1]
403     arrays2[2][1:2,1:3,0,1]=a_in[2]
404     checkResult("slicing, set: rank=4 [1:2,1:3,0,1] "+ex1+","+ex2, \
405     arg2, \
406     arrays2[0], \
407     arrays2[1], \
408     arrays2[2], \
409     wh)
410    
411     arg2=prepareArg(a_r4,ex1,wh)
412     arrays2=turnToArray(a_r4,ex1)
413     a_in=[arrays_in[0][1,1,0,1], \
414     arrays_in[1][1,1,0,1], \
415     arrays_in[2][1,1,0,1]]
416     expArg=prepareArg(a_in,ex2,wh)
417     arg2[1,1,0,1]=expArg
418     arrays2[0][1,1,0,1]=a_in[0]
419     arrays2[1][1,1,0,1]=a_in[1]
420     arrays2[2][1,1,0,1]=a_in[2]
421     checkResult("slicing, set: rank=4 [1,1,0,1] "+ex1+","+ex2, \
422     arg2, \
423     arrays2[0], \
424     arrays2[1], \
425     arrays2[2], \
426     wh)
427    
428 jgs 147 sys.exit(0)
429 jgs 102 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26