/[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 613 - (show annotations)
Wed Mar 22 01:12:32 2006 UTC (14 years, 8 months ago) by elspeth
File MIME type: text/x-python
File size: 15211 byte(s)
More copyright information inserted.

1 """
2
3 Test slice setting for Data objects.
4
5 Version $Id$
6
7 """
8
9 __copyright__=""" Copyright (c) 2006 by ACcESS MNRF
10 http://www.access.edu.au
11 Primary Business: Queensland, Australia"""
12 __licence__="""Licensed under the Open Software License version 3.0
13 http://www.opensource.org/licences/osl-3.0.php"""
14 import sys
15 import unittest
16 import os
17
18 from esys.escript import *
19 from esys import bruce
20
21 import numarray
22
23 from numarray import array,Float64,ones,greater
24
25 Tag1=1
26 Tag2=2
27
28 tol=1.E-15
29
30 # rank 1 test arguments
31 a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ]
32 a_r1_in=[ [1./1,2,3], [-1./1,-1./2,-1./3], [1./100,1./200,1./300] ]
33
34 # rank 4 test arguments
35 a_r4=[ \
36 [ [ [[ 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]] ] ], \
37 [ [ [[ -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]] ] ], \
38 [ [[[ 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]] ] ] ]
39 a_r4_in=[ \
40 [ [ [[ 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]] ] ], \
41 [ [ [[ -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]] ] ], \
42 [ [[[ 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]] ] ] ]
43
44 def prepareArg(val,ex,wh):
45 if ex=="Array":
46 out=val[0]
47 else:
48 if ex=="Expanded":
49 exx=True
50 else:
51 exx=False
52 out=Data(val[0],what=wh,expand=exx)
53 if ex=="Tagged0":
54 out.tag()
55 elif ex=="Tagged1":
56 out.setTaggedValue(Tag1,val[1])
57 elif ex=="Tagged2":
58 out.setTaggedValue(Tag1,val[1])
59 out.setTaggedValue(Tag2,val[2])
60 return out
61
62 def turnToArray(val,tagged):
63 if tagged=="Tagged1":
64 out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
65 elif tagged=="Tagged2":
66 out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
67 else:
68 out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
69 return out
70
71 def checkResult(text,res,val0,val1,val2,wh):
72 ref=Data(val0,what=wh,expand=False)
73 ref.setTaggedValue(Tag1,val1)
74 ref.setTaggedValue(Tag2,val2)
75 norm=Lsup(ref)+tol
76 error=Lsup(ref-res)/norm
77 print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error)
78 if error>tol:
79 print "**** %s: error is too large"%(text)
80 raise SystemError,"@@ %s: error is too large"%(text)
81 sys.exit(1)
82
83 #
84 # test slice setting:
85 #
86
87 msh=bruce.Rectangle(20,6)
88 for wh in [ContinuousFunction(msh),Function(msh)]:
89
90 print wh
91
92 for ex1 in ["Constant","Expanded","Tagged0","Tagged1","Tagged2"]:
93
94 for ex2 in ["Array","Constant","Expanded","Tagged0","Tagged1","Tagged2"]:
95
96 print "Slice setting: ", ex1, ",", ex2, ":"
97
98 # rank 1
99
100 arrays_in=turnToArray(a_r1_in,ex2)
101
102 arg2=prepareArg(a_r1,ex1,wh)
103 arrays2=turnToArray(a_r1,ex1)
104 a_in=[arrays_in[0][:], \
105 arrays_in[1][:], \
106 arrays_in[2][:]]
107 expArg=prepareArg(a_in,ex2,wh)
108 arg2[:]=expArg
109 arrays2[0][:]=a_in[0]
110 arrays2[1][:]=a_in[1]
111 arrays2[2][:]=a_in[2]
112 checkResult("slicing, set: rank=1 [:] "+ex1+","+ex2, \
113 arg2, \
114 arrays2[0], \
115 arrays2[1], \
116 arrays2[2], \
117 wh)
118
119 arg2=prepareArg(a_r1,ex1,wh)
120 arrays2=turnToArray(a_r1,ex1)
121 a_in=[arrays_in[0][1], \
122 arrays_in[1][1], \
123 arrays_in[2][1]]
124 expArg=prepareArg(a_in,ex2,wh)
125 arg2[1]=expArg
126 arrays2[0][1]=a_in[0]
127 arrays2[1][1]=a_in[1]
128 arrays2[2][1]=a_in[2]
129 checkResult("slicing, set: rank=1 [1] "+ex1+","+ex2, \
130 arg2, \
131 arrays2[0], \
132 arrays2[1], \
133 arrays2[2], \
134 wh)
135
136 arg2=prepareArg(a_r1,ex1,wh)
137 arrays2=turnToArray(a_r1,ex1)
138 a_in=[arrays_in[0][1:3], \
139 arrays_in[1][1:3], \
140 arrays_in[2][1:3]]
141 expArg=prepareArg(a_in,ex2,wh)
142 arg2[1:3]=expArg
143 arrays2[0][1:3]=a_in[0]
144 arrays2[1][1:3]=a_in[1]
145 arrays2[2][1:3]=a_in[2]
146 checkResult("slicing, set: rank=1 [1:3] "+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][:2], \
156 arrays_in[1][:2], \
157 arrays_in[2][:2]]
158 expArg=prepareArg(a_in,ex2,wh)
159 arg2[:2]=expArg
160 arrays2[0][:2]=a_in[0]
161 arrays2[1][:2]=a_in[1]
162 arrays2[2][:2]=a_in[2]
163 checkResult("slicing, set: rank=1 [:2] "+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][2:], \
173 arrays_in[1][2:], \
174 arrays_in[2][2:]]
175 expArg=prepareArg(a_in,ex2,wh)
176 arg2[2:]=expArg
177 arrays2[0][2:]=a_in[0]
178 arrays2[1][2:]=a_in[1]
179 arrays2[2][2:]=a_in[2]
180 checkResult("slicing, set: rank=1 [2:] "+ex1+","+ex2, \
181 arg2, \
182 arrays2[0], \
183 arrays2[1], \
184 arrays2[2], \
185 wh)
186
187 # rank 4
188
189 arrays_in=turnToArray(a_r4_in,ex2)
190
191 arg2=prepareArg(a_r4,ex1,wh)
192 arrays2=turnToArray(a_r4,ex1)
193 a_in=[arrays_in[0][:], \
194 arrays_in[1][:], \
195 arrays_in[2][:]]
196 expArg=prepareArg(a_in,ex2,wh)
197 arg2[:]=expArg
198 arrays2[0][:]=a_in[0]
199 arrays2[1][:]=a_in[1]
200 arrays2[2][:]=a_in[2]
201 checkResult("slicing, set: rank=4 [:] "+ex1+","+ex2, \
202 arg2, \
203 arrays2[0], \
204 arrays2[1], \
205 arrays2[2], \
206 wh)
207
208 arg2=prepareArg(a_r4,ex1,wh)
209 arrays2=turnToArray(a_r4,ex1)
210 a_in=[arrays_in[0][1], \
211 arrays_in[1][1], \
212 arrays_in[2][1]]
213 expArg=prepareArg(a_in,ex2,wh)
214 arg2[1]=expArg
215 arrays2[0][1]=a_in[0]
216 arrays2[1][1]=a_in[1]
217 arrays2[2][1]=a_in[2]
218 checkResult("slicing, set: rank=4 [1] "+ex1+","+ex2, \
219 arg2, \
220 arrays2[0], \
221 arrays2[1], \
222 arrays2[2], \
223 wh)
224
225 arg2=prepareArg(a_r4,ex1,wh)
226 arrays2=turnToArray(a_r4,ex1)
227 a_in=[arrays_in[0][1:3], \
228 arrays_in[1][1:3], \
229 arrays_in[2][1:3]]
230 expArg=prepareArg(a_in,ex2,wh)
231 arg2[1:3]=expArg
232 arrays2[0][1:3]=a_in[0]
233 arrays2[1][1:3]=a_in[1]
234 arrays2[2][1:3]=a_in[2]
235 checkResult("slicing, set: rank=4 [1:3] "+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][:,:], \
245 arrays_in[1][:,:], \
246 arrays_in[2][:,:]]
247 expArg=prepareArg(a_in,ex2,wh)
248 arg2[:,:]=expArg
249 arrays2[0][:,:]=a_in[0]
250 arrays2[1][:,:]=a_in[1]
251 arrays2[2][:,:]=a_in[2]
252 checkResult("slicing, set: rank=4 [:,:] "+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], \
262 arrays_in[1][:,1], \
263 arrays_in[2][:,1]]
264 expArg=prepareArg(a_in,ex2,wh)
265 arg2[:,1]=expArg
266 arrays2[0][:,1]=a_in[0]
267 arrays2[1][:,1]=a_in[1]
268 arrays2[2][:,1]=a_in[2]
269 checkResult("slicing, set: rank=4 [:,1] "+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][:,1:3], \
279 arrays_in[1][:,1:3], \
280 arrays_in[2][:,1:3]]
281 expArg=prepareArg(a_in,ex2,wh)
282 arg2[:,1:3]=expArg
283 arrays2[0][:,1:3]=a_in[0]
284 arrays2[1][:,1:3]=a_in[1]
285 arrays2[2][:,1:3]=a_in[2]
286 checkResult("slicing, set: rank=4 [:,1:3] "+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:2,1:3], \
296 arrays_in[1][1:2,1:3], \
297 arrays_in[2][1:2,1:3]]
298 expArg=prepareArg(a_in,ex2,wh)
299 arg2[1:2,1:3]=expArg
300 arrays2[0][1:2,1:3]=a_in[0]
301 arrays2[1][1:2,1:3]=a_in[1]
302 arrays2[2][1:2,1:3]=a_in[2]
303 checkResult("slicing, set: rank=4 [1:2,1:3] "+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:2,1:3,1,1], \
313 arrays_in[1][1:2,1:3,1,1], \
314 arrays_in[2][1:2,1:3,1,1]]
315 expArg=prepareArg(a_in,ex2,wh)
316 arg2[1:2,1:3,1,1]=expArg
317 arrays2[0][1:2,1:3,1,1]=a_in[0]
318 arrays2[1][1:2,1:3,1,1]=a_in[1]
319 arrays2[2][1:2,1:3,1,1]=a_in[2]
320 checkResult("slicing, set: rank=4 [1:2,1:3,1,1] "+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,:,1], \
330 arrays_in[1][1:2,1:3,:,1], \
331 arrays_in[2][1:2,1:3,:,1]]
332 expArg=prepareArg(a_in,ex2,wh)
333 arg2[1:2,1:3,:,1]=expArg
334 arrays2[0][1:2,1:3,:,1]=a_in[0]
335 arrays2[1][1:2,1:3,:,1]=a_in[1]
336 arrays2[2][1:2,1:3,:,1]=a_in[2]
337 checkResult("slicing, set: rank=4 [1:2,1:3,:,1] "+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,0,:], \
347 arrays_in[1][1:2,1:3,0,:], \
348 arrays_in[2][1:2,1:3,0,:]]
349 expArg=prepareArg(a_in,ex2,wh)
350 arg2[1:2,1:3,0,:]=expArg
351 arrays2[0][1:2,1:3,0,:]=a_in[0]
352 arrays2[1][1:2,1:3,0,:]=a_in[1]
353 arrays2[2][1:2,1:3,0,:]=a_in[2]
354 checkResult("slicing, set: rank=4 [1:2,1:3,0,:] "+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,0,1], \
364 arrays_in[1][1:2,1:3,0,1], \
365 arrays_in[2][1:2,1:3,0,1]]
366 expArg=prepareArg(a_in,ex2,wh)
367 arg2[1:2,1:3,0,1]=expArg
368 arrays2[0][1:2,1:3,0,1]=a_in[0]
369 arrays2[1][1:2,1:3,0,1]=a_in[1]
370 arrays2[2][1:2,1:3,0,1]=a_in[2]
371 checkResult("slicing, set: rank=4 [1:2,1:3,0,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,1,0,1], \
381 arrays_in[1][1,1,0,1], \
382 arrays_in[2][1,1,0,1]]
383 expArg=prepareArg(a_in,ex2,wh)
384 arg2[1,1,0,1]=expArg
385 arrays2[0][1,1,0,1]=a_in[0]
386 arrays2[1][1,1,0,1]=a_in[1]
387 arrays2[2][1,1,0,1]=a_in[2]
388 checkResult("slicing, set: rank=4 [1,1,0,1] "+ex1+","+ex2, \
389 arg2, \
390 arrays2[0], \
391 arrays2[1], \
392 arrays2[2], \
393 wh)
394
395 sys.exit(0)
396 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26