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

Contents of /trunk/esys2/escript/test/python/SliceSetting.py

Parent Directory Parent Directory | Revision Log Revision Log


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

1 import sys
2 import unittest
3 import os
4
5 from esys.escript import *
6 from esys import finley
7
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 #raise SystemError,"@@ %s: error is too large"%(text)
85 print "**** %s: error is too large"%(text)
86
87 def getRank(arg):
88 if isinstance(arg,Data):
89 return arg.getRank()
90 else:
91 g=array(arg)
92 if g.rank==0:
93 return 1
94 else:
95 return g.rank
96
97 def isScalar(arg):
98 if isinstance(arg,Data):
99 if arg.getRank()==1 and arg.getShape()[0]==1:
100 return not None
101 else:
102 return None
103 else:
104 g=array(arg)
105 if g.rank==0:
106 return not None
107 else:
108 if g.rank==1 and g.shape[0]==1:
109 return not None
110 else:
111 return None
112
113 #
114 # ==============================================================
115 #
116 # test slice setting:
117 #
118
119 msh=finley.Rectangle(20,6,1)
120 for wh in [ContinuousFunction(msh),Function(msh)]:
121
122 print wh
123
124 #for ex1 in ["Array","Constant","Expanded","Tagged1","Tagged2"]:
125 for ex1 in ["Constant","Expanded","Tagged1","Tagged2"]:
126
127 #for ex2 in ["Array","Constant","Expanded","Tagged1","Tagged2"]:
128 for ex2 in ["Array","Constant","Expanded","Tagged1","Tagged2"]:
129
130 print "Slice setting: ", ex1, ",", ex2, ":"
131
132 # rank 1
133
134 arrays_in=turnToArray(a_r1_in,ex2)
135
136 arg2=prepareArg(a_r1,ex1,wh)
137 arrays2=turnToArray(a_r1,ex1)
138 a_in=[arrays_in[0][:], \
139 arrays_in[1][:], \
140 arrays_in[2][:]]
141 expArg=prepareArg(a_in,ex2,wh)
142 arg2[:]=expArg
143 arrays2[0][:]=a_in[0]
144 arrays2[1][:]=a_in[1]
145 arrays2[2][:]=a_in[2]
146 checkResult("slicing, set: rank=1 [:] "+ex1+","+ex2, \
147 arg2, \
148 arrays2[0], \
149 arrays2[1], \
150 arrays2[2], \
151 wh)
152
153 arg2=prepareArg(a_r1,ex1,wh)
154 arrays2=turnToArray(a_r1,ex1)
155 a_in=[arrays_in[0][1], \
156 arrays_in[1][1], \
157 arrays_in[2][1]]
158 expArg=prepareArg(a_in,ex2,wh)
159 arg2[1]=expArg
160 arrays2[0][1]=a_in[0]
161 arrays2[1][1]=a_in[1]
162 arrays2[2][1]=a_in[2]
163 checkResult("slicing, set: rank=1 [1] "+ex1+","+ex2, \
164 arg2, \
165 arrays2[0], \
166 arrays2[1], \
167 arrays2[2], \
168 wh)
169
170 arg2=prepareArg(a_r1,ex1,wh)
171 arrays2=turnToArray(a_r1,ex1)
172 a_in=[arrays_in[0][1:3], \
173 arrays_in[1][1:3], \
174 arrays_in[2][1:3]]
175 expArg=prepareArg(a_in,ex2,wh)
176 arg2[1:3]=expArg
177 arrays2[0][1:3]=a_in[0]
178 arrays2[1][1:3]=a_in[1]
179 arrays2[2][1:3]=a_in[2]
180 checkResult("slicing, set: rank=1 [1:3] "+ex1+","+ex2, \
181 arg2, \
182 arrays2[0], \
183 arrays2[1], \
184 arrays2[2], \
185 wh)
186
187 arg2=prepareArg(a_r1,ex1,wh)
188 arrays2=turnToArray(a_r1,ex1)
189 a_in=[arrays_in[0][:2], \
190 arrays_in[1][:2], \
191 arrays_in[2][:2]]
192 expArg=prepareArg(a_in,ex2,wh)
193 arg2[:2]=expArg
194 arrays2[0][:2]=a_in[0]
195 arrays2[1][:2]=a_in[1]
196 arrays2[2][:2]=a_in[2]
197 checkResult("slicing, set: rank=1 [:2] "+ex1+","+ex2, \
198 arg2, \
199 arrays2[0], \
200 arrays2[1], \
201 arrays2[2], \
202 wh)
203
204 arg2=prepareArg(a_r1,ex1,wh)
205 arrays2=turnToArray(a_r1,ex1)
206 a_in=[arrays_in[0][2:], \
207 arrays_in[1][2:], \
208 arrays_in[2][2:]]
209 expArg=prepareArg(a_in,ex2,wh)
210 arg2[2:]=expArg
211 arrays2[0][2:]=a_in[0]
212 arrays2[1][2:]=a_in[1]
213 arrays2[2][2:]=a_in[2]
214 checkResult("slicing, set: rank=1 [2:] "+ex1+","+ex2, \
215 arg2, \
216 arrays2[0], \
217 arrays2[1], \
218 arrays2[2], \
219 wh)
220
221 # rank 4
222
223 arrays_in=turnToArray(a_r4_in,ex2)
224
225 arg2=prepareArg(a_r4,ex1,wh)
226 arrays2=turnToArray(a_r4,ex1)
227 a_in=[arrays_in[0][:], \
228 arrays_in[1][:], \
229 arrays_in[2][:]]
230 expArg=prepareArg(a_in,ex2,wh)
231 arg2[:]=expArg
232 arrays2[0][:]=a_in[0]
233 arrays2[1][:]=a_in[1]
234 arrays2[2][:]=a_in[2]
235 checkResult("slicing, set: rank=4 [:] "+ex1+","+ex2, \
236 arg2, \
237 arrays2[0], \
238 arrays2[1], \
239 arrays2[2], \
240 wh)
241
242 arg2=prepareArg(a_r4,ex1,wh)
243 arrays2=turnToArray(a_r4,ex1)
244 a_in=[arrays_in[0][1], \
245 arrays_in[1][1], \
246 arrays_in[2][1]]
247 expArg=prepareArg(a_in,ex2,wh)
248 arg2[1]=expArg
249 arrays2[0][1]=a_in[0]
250 arrays2[1][1]=a_in[1]
251 arrays2[2][1]=a_in[2]
252 checkResult("slicing, set: rank=4 [1] "+ex1+","+ex2, \
253 arg2, \
254 arrays2[0], \
255 arrays2[1], \
256 arrays2[2], \
257 wh)
258
259 arg2=prepareArg(a_r4,ex1,wh)
260 arrays2=turnToArray(a_r4,ex1)
261 a_in=[arrays_in[0][1:3], \
262 arrays_in[1][1:3], \
263 arrays_in[2][1:3]]
264 expArg=prepareArg(a_in,ex2,wh)
265 arg2[1:3]=expArg
266 arrays2[0][1:3]=a_in[0]
267 arrays2[1][1:3]=a_in[1]
268 arrays2[2][1:3]=a_in[2]
269 checkResult("slicing, set: rank=4 [1:3] "+ex1+","+ex2, \
270 arg2, \
271 arrays2[0], \
272 arrays2[1], \
273 arrays2[2], \
274 wh)
275
276 arg2=prepareArg(a_r4,ex1,wh)
277 arrays2=turnToArray(a_r4,ex1)
278 a_in=[arrays_in[0][:,:], \
279 arrays_in[1][:,:], \
280 arrays_in[2][:,:]]
281 expArg=prepareArg(a_in,ex2,wh)
282 arg2[:,:]=expArg
283 arrays2[0][:,:]=a_in[0]
284 arrays2[1][:,:]=a_in[1]
285 arrays2[2][:,:]=a_in[2]
286 checkResult("slicing, set: rank=4 [:,:] "+ex1+","+ex2, \
287 arg2, \
288 arrays2[0], \
289 arrays2[1], \
290 arrays2[2], \
291 wh)
292
293 arg2=prepareArg(a_r4,ex1,wh)
294 arrays2=turnToArray(a_r4,ex1)
295 a_in=[arrays_in[0][:,1], \
296 arrays_in[1][:,1], \
297 arrays_in[2][:,1]]
298 expArg=prepareArg(a_in,ex2,wh)
299 arg2[:,1]=expArg
300 arrays2[0][:,1]=a_in[0]
301 arrays2[1][:,1]=a_in[1]
302 arrays2[2][:,1]=a_in[2]
303 checkResult("slicing, set: rank=4 [:,1] "+ex1+","+ex2, \
304 arg2, \
305 arrays2[0], \
306 arrays2[1], \
307 arrays2[2], \
308 wh)
309
310 arg2=prepareArg(a_r4,ex1,wh)
311 arrays2=turnToArray(a_r4,ex1)
312 a_in=[arrays_in[0][:,1:3], \
313 arrays_in[1][:,1:3], \
314 arrays_in[2][:,1:3]]
315 expArg=prepareArg(a_in,ex2,wh)
316 arg2[:,1:3]=expArg
317 arrays2[0][:,1:3]=a_in[0]
318 arrays2[1][:,1:3]=a_in[1]
319 arrays2[2][:,1:3]=a_in[2]
320 checkResult("slicing, set: rank=4 [:,1:3] "+ex1+","+ex2, \
321 arg2, \
322 arrays2[0], \
323 arrays2[1], \
324 arrays2[2], \
325 wh)
326
327 arg2=prepareArg(a_r4,ex1,wh)
328 arrays2=turnToArray(a_r4,ex1)
329 a_in=[arrays_in[0][1:2,1:3], \
330 arrays_in[1][1:2,1:3], \
331 arrays_in[2][1:2,1:3]]
332 expArg=prepareArg(a_in,ex2,wh)
333 arg2[1:2,1:3]=expArg
334 arrays2[0][1:2,1:3]=a_in[0]
335 arrays2[1][1:2,1:3]=a_in[1]
336 arrays2[2][1:2,1:3]=a_in[2]
337 checkResult("slicing, set: rank=4 [1:2,1:3] "+ex1+","+ex2, \
338 arg2, \
339 arrays2[0], \
340 arrays2[1], \
341 arrays2[2], \
342 wh)
343
344 arg2=prepareArg(a_r4,ex1,wh)
345 arrays2=turnToArray(a_r4,ex1)
346 a_in=[arrays_in[0][1:2,1:3,1,1], \
347 arrays_in[1][1:2,1:3,1,1], \
348 arrays_in[2][1:2,1:3,1,1]]
349 expArg=prepareArg(a_in,ex2,wh)
350 arg2[1:2,1:3,1,1]=expArg
351 arrays2[0][1:2,1:3,1,1]=a_in[0]
352 arrays2[1][1:2,1:3,1,1]=a_in[1]
353 arrays2[2][1:2,1:3,1,1]=a_in[2]
354 checkResult("slicing, set: rank=4 [1:2,1:3,1,1] "+ex1+","+ex2, \
355 arg2, \
356 arrays2[0], \
357 arrays2[1], \
358 arrays2[2], \
359 wh)
360
361 arg2=prepareArg(a_r4,ex1,wh)
362 arrays2=turnToArray(a_r4,ex1)
363 a_in=[arrays_in[0][1:2,1:3,:,1], \
364 arrays_in[1][1:2,1:3,:,1], \
365 arrays_in[2][1:2,1:3,:,1]]
366 expArg=prepareArg(a_in,ex2,wh)
367 arg2[1:2,1:3,:,1]=expArg
368 arrays2[0][1:2,1:3,:,1]=a_in[0]
369 arrays2[1][1:2,1:3,:,1]=a_in[1]
370 arrays2[2][1:2,1:3,:,1]=a_in[2]
371 checkResult("slicing, set: rank=4 [1:2,1:3,:,1] "+ex1+","+ex2, \
372 arg2, \
373 arrays2[0], \
374 arrays2[1], \
375 arrays2[2], \
376 wh)
377
378 arg2=prepareArg(a_r4,ex1,wh)
379 arrays2=turnToArray(a_r4,ex1)
380 a_in=[arrays_in[0][1:2,1:3,0,:], \
381 arrays_in[1][1:2,1:3,0,:], \
382 arrays_in[2][1:2,1:3,0,:]]
383 expArg=prepareArg(a_in,ex2,wh)
384 arg2[1:2,1:3,0,:]=expArg
385 arrays2[0][1:2,1:3,0,:]=a_in[0]
386 arrays2[1][1:2,1:3,0,:]=a_in[1]
387 arrays2[2][1:2,1:3,0,:]=a_in[2]
388 checkResult("slicing, set: rank=4 [1:2,1:3,0,:] "+ex1+","+ex2, \
389 arg2, \
390 arrays2[0], \
391 arrays2[1], \
392 arrays2[2], \
393 wh)
394
395 arg2=prepareArg(a_r4,ex1,wh)
396 arrays2=turnToArray(a_r4,ex1)
397 a_in=[arrays_in[0][1:2,1:3,0,1], \
398 arrays_in[1][1:2,1:3,0,1], \
399 arrays_in[2][1:2,1:3,0,1]]
400 expArg=prepareArg(a_in,ex2,wh)
401 arg2[1:2,1:3,0,1]=expArg
402 arrays2[0][1:2,1:3,0,1]=a_in[0]
403 arrays2[1][1:2,1:3,0,1]=a_in[1]
404 arrays2[2][1:2,1:3,0,1]=a_in[2]
405 checkResult("slicing, set: rank=4 [1:2,1:3,0,1] "+ex1+","+ex2, \
406 arg2, \
407 arrays2[0], \
408 arrays2[1], \
409 arrays2[2], \
410 wh)
411
412 arg2=prepareArg(a_r4,ex1,wh)
413 arrays2=turnToArray(a_r4,ex1)
414 a_in=[arrays_in[0][1,1,0,1], \
415 arrays_in[1][1,1,0,1], \
416 arrays_in[2][1,1,0,1]]
417 expArg=prepareArg(a_in,ex2,wh)
418 arg2[1,1,0,1]=expArg
419 arrays2[0][1,1,0,1]=a_in[0]
420 arrays2[1][1,1,0,1]=a_in[1]
421 arrays2[2][1,1,0,1]=a_in[2]
422 checkResult("slicing, set: rank=4 [1,1,0,1] "+ex1+","+ex2, \
423 arg2, \
424 arrays2[0], \
425 arrays2[1], \
426 arrays2[2], \
427 wh)
428
429 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26