/[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 564 - (show annotations)
Thu Feb 23 05:22:31 2006 UTC (15 years, 6 months ago) by jgs
File MIME type: text/x-python
File size: 14923 byte(s)
extend testing to include DataTagged objects with no
tags, only default values

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26