/[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 141 - (show annotations)
Mon Jul 25 05:15:41 2005 UTC (13 years, 9 months ago) by jgs
File MIME type: text/x-python
File size: 15878 byte(s)
fixed to work with new python source/install directory structure

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26