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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 564 - (show annotations)
Thu Feb 23 05:22:31 2006 UTC (13 years, 7 months ago) by jgs
File MIME type: text/x-python
File size: 7746 byte(s)
extend testing to include DataTagged objects with no
tags, only default values

1 """
2
3 Tests slice getting 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 from numarray import array,Float64,ones,greater
18
19 Tag1=1
20 Tag2=2
21
22 tol=1.E-15
23
24 # a rank 1 test argument
25 a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ]
26
27 # a rank 4 test argument
28 a_r4=[ \
29 [ [ [[ 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]] ] ], \
30 [ [ [[ -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]] ] ], \
31 [ [[[ 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]] ] ] ]
32
33 def prepareArg(val,ex,wh):
34 if ex=="Expanded":
35 exx=True
36 else:
37 exx=False
38 out=Data(val[0],what=wh,expand=exx)
39 if ex=="Tagged0":
40 out.tag()
41 elif ex=="Tagged1":
42 out.setTaggedValue(Tag1,val[1])
43 elif ex=="Tagged2":
44 out.setTaggedValue(Tag1,val[1])
45 out.setTaggedValue(Tag2,val[2])
46 return out
47
48 def turnToArray(val,tagged):
49 if tagged=="Tagged1":
50 out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
51 elif tagged=="Tagged2":
52 out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
53 else:
54 out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
55 return out
56
57 def checkResult(text,res,val0,val1,val2,wh):
58 ref=Data(val0,what=wh,expand=False)
59 ref.setTaggedValue(Tag1,val1)
60 ref.setTaggedValue(Tag2,val2)
61 norm=Lsup(ref)+tol
62 error=Lsup(ref-res)/norm
63 print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error)
64 if error>tol:
65 print "**** %s: error is too large"%(text)
66 raise SystemError,"@@ %s at %s: error is too large"%(text,wh)
67 sys.exit(1)
68
69 #
70 # ==============================================================
71 #
72 # test slice getting:
73 #
74
75 msh=bruce.Rectangle(20,6)
76
77 for wh in [ContinuousFunction(msh), Function(msh)]:
78
79 print wh
80
81 for ex1 in ["Constant", "Expanded", "Tagged0", "Tagged1", "Tagged2"]:
82
83 print "Slice getting: ", ex1
84
85 # rank 1
86
87 arg=prepareArg(a_r1,ex1,wh)
88 arrays=turnToArray(a_r1,ex1)
89
90 checkResult("slicing: rank=1 [:] "+ex1, \
91 arg[:], \
92 arrays[0][:], \
93 arrays[1][:], \
94 arrays[2][:], \
95 wh)
96
97 checkResult("slicing: rank=1 [1] "+ex1, \
98 arg[1], \
99 arrays[0][1], \
100 arrays[1][1], \
101 arrays[2][1], \
102 wh)
103
104 checkResult("slicing: rank=1 [1:3] "+ex1, \
105 arg[1:3], \
106 arrays[0][1:3], \
107 arrays[1][1:3], \
108 arrays[2][1:3], \
109 wh)
110
111 checkResult("slicing: rank=1 [2:] "+ex1, \
112 arg[2:], \
113 arrays[0][2:], \
114 arrays[1][2:], \
115 arrays[2][2:], \
116 wh)
117
118 checkResult("slicing: rank=1 [:1] "+ex1, \
119 arg[:1], \
120 arrays[0][:1], \
121 arrays[1][:1], \
122 arrays[2][:1], \
123 wh)
124
125 # rank 4
126
127 arg=prepareArg(a_r4,ex1,wh)
128 arrays=turnToArray(a_r4,ex1)
129
130 checkResult("slicing: rank=4 [:] "+ex1, \
131 arg[:], \
132 arrays[0][:], \
133 arrays[1][:], \
134 arrays[2][:], \
135 wh)
136
137 checkResult("slicing: rank=4 [1] "+ex1, \
138 arg[1], \
139 arrays[0][1], \
140 arrays[1][1], \
141 arrays[2][1], \
142 wh)
143
144 checkResult("slicing: rank=4 [1,1,1,1] "+ex1, \
145 arg[1,1,1,1], \
146 arrays[0][1,1,1,1], \
147 arrays[1][1,1,1,1], \
148 arrays[2][1,1,1,1], \
149 wh)
150
151 checkResult("slicing: rank=4 [1:3] "+ex1, \
152 arg[1:3], \
153 arrays[0][1:3], \
154 arrays[1][1:3], \
155 arrays[2][1:3], \
156 wh)
157
158 checkResult("slicing: rank=4 [:2] "+ex1, \
159 arg[:2], \
160 arrays[0][:2], \
161 arrays[1][:2], \
162 arrays[2][:2], \
163 wh)
164
165 checkResult("slicing: rank=4 [1:] "+ex1, \
166 arg[1:], \
167 arrays[0][1:], \
168 arrays[1][1:], \
169 arrays[2][1:], \
170 wh)
171
172 checkResult("slicing: rank=4 [:,:] "+ex1, \
173 arg[:,:], \
174 arrays[0][:,:], \
175 arrays[1][:,:], \
176 arrays[2][:,:], \
177 wh)
178
179 checkResult("slicing: rank=4 [:,1] "+ex1, \
180 arg[:,1], \
181 arrays[0][:,1], \
182 arrays[1][:,1], \
183 arrays[2][:,1], \
184 wh)
185
186 checkResult("slicing: rank=4 [:,1:3] "+ex1, \
187 arg[:,1:3], \
188 arrays[0][:,1:3], \
189 arrays[1][:,1:3], \
190 arrays[2][:,1:3], \
191 wh)
192
193 checkResult("slicing: rank=4 [1:2,1:3] "+ex1, \
194 arg[1:2,1:3], \
195 arrays[0][1:2,1:3], \
196 arrays[1][1:2,1:3], \
197 arrays[2][1:2,1:3], \
198 wh)
199
200 checkResult("slicing: rank=4 [:,1:2,1:2] "+ex1, \
201 arg[:,1:2,1:2], \
202 arrays[0][:,1:2,1:2], \
203 arrays[1][:,1:2,1:2], \
204 arrays[2][:,1:2,1:2], \
205 wh)
206
207 checkResult("slicing: rank=4 [:,:,1:2,1:3] "+ex1, \
208 arg[:,:,1:2,1:3], \
209 arrays[0][:,:,1:2,1:3], \
210 arrays[1][:,:,1:2,1:3], \
211 arrays[2][:,:,1:2,1:3], \
212 wh)
213
214 checkResult("slicing: rank=4 [1:2,1:3,1,1] "+ex1, \
215 arg[1:2,1:3,1,1], \
216 arrays[0][1:2,1:3,1,1], \
217 arrays[1][1:2,1:3,1,1], \
218 arrays[2][1:2,1:3,1,1], \
219 wh)
220
221 checkResult("slicing: rank=4 [1:2,1:3,:,1] "+ex1, \
222 arg[1:2,1:3,:,1], \
223 arrays[0][1:2,1:3,:,1], \
224 arrays[1][1:2,1:3,:,1], \
225 arrays[2][1:2,1:3,:,1], \
226 wh)
227
228 checkResult("slicing: rank=4 [1:2,1:3,0,:] "+ex1, \
229 arg[1:2,1:3,0,:], \
230 arrays[0][1:2,1:3,0,:], \
231 arrays[1][1:2,1:3,0,:], \
232 arrays[2][1:2,1:3,0,:], \
233 wh)
234
235 checkResult("slicing: rank=4 [1:2,1:3,0,1] "+ex1, \
236 arg[1:2,1:3,0,1], \
237 arrays[0][1:2,1:3,0,1], \
238 arrays[1][1:2,1:3,0,1], \
239 arrays[2][1:2,1:3,0,1], \
240 wh)
241
242 checkResult("slicing: rank=4 [1,1,0,1] "+ex1, \
243 arg[1,1,0,1], \
244 arrays[0][1,1,0,1], \
245 arrays[1][1,1,0,1], \
246 arrays[2][1,1,0,1], \
247 wh)
248
249 sys.exit(0)
250 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26