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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 542 - (show annotations)
Mon Feb 20 06:53:12 2006 UTC (15 years, 7 months ago) by jgs
File MIME type: text/x-python
File size: 14853 byte(s)
reinstate sliceSetting testing of DataTagged

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26