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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 614 - (hide annotations)
Wed Mar 22 01:37:07 2006 UTC (13 years, 5 months ago) by elspeth
File MIME type: text/x-python
File size: 8034 byte(s)
Corrected spelling of 'license' in url so that the link actually points to the license.

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26