1 |
jgs |
117 |
import sys |
2 |
|
|
import unittest |
3 |
|
|
import os |
4 |
|
|
|
5 |
jgs |
149 |
from esys.escript import * |
6 |
jgs |
153 |
from esys import bruce |
7 |
jgs |
117 |
|
8 |
|
|
import numarray |
9 |
jgs |
126 |
from numarray import array,Float64,ones,greater |
10 |
jgs |
117 |
|
11 |
|
|
""" |
12 |
|
|
|
13 |
|
|
Miscellaneous escript/Data tests. |
14 |
|
|
|
15 |
|
|
Version $Id$ |
16 |
|
|
|
17 |
|
|
""" |
18 |
|
|
|
19 |
|
|
arglist = [ \ |
20 |
jgs |
126 |
3.0, \ |
21 |
jgs |
117 |
[3,4], \ |
22 |
|
|
[[1,2],[3,4]], \ |
23 |
|
|
[[15,8],[12,8]], \ |
24 |
jgs |
126 |
[[[15,8],[12,8]],[[-9,9],[13,8]]] \ |
25 |
jgs |
117 |
] |
26 |
|
|
|
27 |
|
|
def turnToArray(val): |
28 |
|
|
out=array(val,Float64) |
29 |
|
|
return out |
30 |
|
|
|
31 |
|
|
def prepareArg(val,ex,wh): |
32 |
|
|
if ex=="Expanded": |
33 |
|
|
exx=True |
34 |
|
|
else: |
35 |
|
|
exx=False |
36 |
|
|
out=Data(val,what=wh,expand=exx) |
37 |
|
|
return out |
38 |
|
|
|
39 |
|
|
def checkResult(text,res,val0,val1,val2,wh): |
40 |
|
|
ref=Data(val0,what=wh,expand=False) |
41 |
|
|
ref.setTaggedValue(Tag1,val1) |
42 |
|
|
ref.setTaggedValue(Tag2,val2) |
43 |
|
|
norm=Lsup(ref)+tol |
44 |
|
|
error=Lsup(ref-res)/norm |
45 |
|
|
print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error) |
46 |
|
|
if error>tol: |
47 |
|
|
#raise SystemError,"@@ %s at %s: error is too large"%(text,wh) |
48 |
|
|
print "**** %s : error is too large"%(text) |
49 |
|
|
|
50 |
|
|
def getRank(arg): |
51 |
|
|
if isinstance(arg,Data): |
52 |
|
|
return arg.getRank() |
53 |
|
|
else: |
54 |
|
|
g=array(arg) |
55 |
|
|
if g.rank==0: |
56 |
|
|
return 1 |
57 |
|
|
else: |
58 |
|
|
return g.rank |
59 |
|
|
|
60 |
|
|
def isScalar(arg): |
61 |
|
|
if isinstance(arg,Data): |
62 |
|
|
if arg.getRank()==1 and arg.getShape()[0]==1: |
63 |
|
|
return not None |
64 |
|
|
else: |
65 |
|
|
return None |
66 |
|
|
else: |
67 |
|
|
g=array(arg) |
68 |
|
|
if g.rank==0: |
69 |
|
|
return not None |
70 |
|
|
else: |
71 |
|
|
if g.rank==1 and g.shape[0]==1: |
72 |
|
|
return not None |
73 |
|
|
else: |
74 |
|
|
return None |
75 |
|
|
|
76 |
|
|
# |
77 |
|
|
# ============================================================== |
78 |
|
|
|
79 |
|
|
print "\n\n" |
80 |
|
|
|
81 |
jgs |
153 |
msh=bruce.Rectangle() |
82 |
jgs |
117 |
|
83 |
jgs |
121 |
for wh in [ContinuousFunction(msh),Function(msh)]: |
84 |
jgs |
117 |
|
85 |
jgs |
147 |
print wh |
86 |
jgs |
117 |
|
87 |
jgs |
121 |
for ex in ["Constant","Expanded"]: |
88 |
jgs |
117 |
|
89 |
jgs |
121 |
for a in arglist: |
90 |
jgs |
117 |
|
91 |
jgs |
121 |
print "\n", ex, a, "==>" |
92 |
jgs |
117 |
|
93 |
jgs |
121 |
arg=prepareArg(a,ex,wh) |
94 |
jgs |
117 |
|
95 |
jgs |
126 |
print "\n\nTests of copy method:" |
96 |
jgs |
117 |
|
97 |
jgs |
126 |
arg_copy = Data() |
98 |
|
|
arg_copy.copy(arg) |
99 |
|
|
print arg |
100 |
|
|
print arg_copy |
101 |
|
|
|
102 |
jgs |
123 |
print "\n\nTests of conversion to numarray:" |
103 |
|
|
|
104 |
jgs |
126 |
narray1 = arg.convertToNumArray() |
105 |
|
|
narray2 = arg.convertToNumArrayFromSampleNo(0) |
106 |
|
|
narray3 = arg.convertToNumArrayFromDPNo(0,0) |
107 |
|
|
|
108 |
jgs |
121 |
print "arg.convertToNumArray()" |
109 |
jgs |
126 |
print narray1 |
110 |
jgs |
117 |
|
111 |
jgs |
121 |
print "arg.convertToNumArrayFromSampleNo(0)" |
112 |
jgs |
126 |
print narray2 |
113 |
jgs |
117 |
|
114 |
jgs |
121 |
print "arg.convertToNumArrayFromDPNo(0,0)" |
115 |
jgs |
126 |
print narray3 |
116 |
jgs |
121 |
|
117 |
jgs |
149 |
if (ex == "Constant"): |
118 |
|
|
print "\n\nTest of getTagNumber:" |
119 |
|
|
arg_copy = Data() |
120 |
|
|
arg_copy.copy(arg) |
121 |
|
|
arg_copy.tag() |
122 |
|
|
for dpno in range(narray1.shape[0]): |
123 |
|
|
print arg_copy.getTagNumber(dpno) |
124 |
|
|
print wh.getTagFromDataPointNo(dpno) |
125 |
|
|
|
126 |
jgs |
126 |
if (ex == "Expanded"): |
127 |
|
|
print "\n\nTests of get/setRefValue functions:" |
128 |
|
|
result_array = numarray.array(a) |
129 |
|
|
print result_array |
130 |
|
|
result_array+=1 |
131 |
|
|
arg.setRefValue(0,result_array) |
132 |
|
|
arg.getRefValue(0,result_array) |
133 |
|
|
print result_array |
134 |
|
|
|
135 |
jgs |
123 |
print "\n\nTests of misc python functions:" |
136 |
jgs |
121 |
|
137 |
jgs |
149 |
print "\nmindp:" |
138 |
|
|
print arg.mindp() |
139 |
|
|
|
140 |
jgs |
121 |
print "\nabs:" |
141 |
jgs |
314 |
print arg._abs() |
142 |
jgs |
121 |
|
143 |
|
|
print "\nmaxval:" |
144 |
jgs |
314 |
print arg._maxval() |
145 |
jgs |
121 |
|
146 |
|
|
print "\nminval:" |
147 |
jgs |
314 |
print arg._minval() |
148 |
jgs |
121 |
|
149 |
jgs |
314 |
#print "\nlength" |
150 |
|
|
#print arg.length() |
151 |
jgs |
121 |
|
152 |
jgs |
123 |
print "\ntrace" |
153 |
jgs |
314 |
print arg._trace() |
154 |
jgs |
123 |
|
155 |
jgs |
121 |
print "\nsign" |
156 |
jgs |
314 |
print arg._sign() |
157 |
jgs |
121 |
|
158 |
|
|
print "\nexp" |
159 |
jgs |
314 |
print arg._exp() |
160 |
jgs |
121 |
|
161 |
|
|
print "\nsqrt" |
162 |
jgs |
314 |
print arg._sqrt() |
163 |
jgs |
121 |
|
164 |
|
|
print "\nneg" |
165 |
jgs |
314 |
print arg._neg() |
166 |
jgs |
121 |
|
167 |
|
|
print "\npos" |
168 |
jgs |
314 |
print arg._pos() |
169 |
jgs |
121 |
|
170 |
|
|
print "\nsin" |
171 |
jgs |
314 |
print arg._sin() |
172 |
jgs |
121 |
|
173 |
|
|
print "\ncos" |
174 |
jgs |
314 |
print arg._cos() |
175 |
jgs |
121 |
|
176 |
|
|
print "\ntan" |
177 |
jgs |
314 |
print arg._tan() |
178 |
jgs |
121 |
|
179 |
jgs |
150 |
print "\nasin" |
180 |
jgs |
314 |
print arg._asin() |
181 |
jgs |
150 |
|
182 |
|
|
print "\nacos" |
183 |
jgs |
314 |
print arg._acos() |
184 |
jgs |
150 |
|
185 |
|
|
print "\natan" |
186 |
jgs |
314 |
print arg._atan() |
187 |
jgs |
150 |
|
188 |
|
|
print "\nsinh" |
189 |
jgs |
314 |
print arg._sinh() |
190 |
jgs |
150 |
|
191 |
|
|
print "\ncosh" |
192 |
jgs |
314 |
print arg._cosh() |
193 |
jgs |
150 |
|
194 |
|
|
print "\ntanh" |
195 |
jgs |
314 |
print arg._tanh() |
196 |
jgs |
150 |
|
197 |
|
|
print "\nasinh" |
198 |
jgs |
314 |
print arg._asinh() |
199 |
jgs |
150 |
|
200 |
|
|
print "\nacosh" |
201 |
jgs |
314 |
print arg._acosh() |
202 |
jgs |
150 |
|
203 |
|
|
print "\natanh" |
204 |
jgs |
314 |
print arg._atanh() |
205 |
jgs |
150 |
|
206 |
jgs |
121 |
print "\nlog" |
207 |
jgs |
314 |
print arg._log() |
208 |
jgs |
121 |
|
209 |
jgs |
314 |
#print "\nln" |
210 |
|
|
#print arg.ln() |
211 |
jgs |
121 |
|
212 |
jgs |
123 |
print "\nLsup" |
213 |
jgs |
314 |
print arg._Lsup() |
214 |
jgs |
119 |
|
215 |
jgs |
123 |
print "\nLinf" |
216 |
jgs |
314 |
print arg._Linf() |
217 |
jgs |
123 |
|
218 |
|
|
print "\nsup" |
219 |
jgs |
314 |
print arg._sup() |
220 |
jgs |
123 |
|
221 |
|
|
print "\ninf" |
222 |
jgs |
314 |
print arg._inf() |
223 |
jgs |
123 |
|
224 |
|
|
print "\n\nTests of archiveData and extractData:" |
225 |
|
|
|
226 |
jgs |
119 |
print "\nDataExpanded:" |
227 |
jgs |
123 |
archDataE=Data([[1.00001],[2.00001]],Function(msh),True) |
228 |
jgs |
119 |
archDataE.archiveData("data-archiveE") |
229 |
|
|
exDataE=Data() |
230 |
|
|
exDataE.extractData("data-archiveE",Function(msh)) |
231 |
|
|
exDataE.archiveData("data-archive2E"); |
232 |
|
|
|
233 |
|
|
print "\nDataTagged:" |
234 |
jgs |
123 |
archDataT=Data([[1.00001],[2.00001]],Function(msh)) |
235 |
jgs |
119 |
archDataT.tag() |
236 |
|
|
archDataT.archiveData("data-archiveT") |
237 |
|
|
exDataT=Data() |
238 |
|
|
exDataT.extractData("data-archiveT",Function(msh)) |
239 |
|
|
exDataT.archiveData("data-archive2T"); |
240 |
|
|
|
241 |
|
|
print "\nDataConstant:" |
242 |
jgs |
123 |
archDataC=Data([1.00001], Function(msh)) |
243 |
jgs |
119 |
archDataC.archiveData("data-archiveC") |
244 |
|
|
exDataC=Data() |
245 |
|
|
exDataC.extractData("data-archiveC",Function(msh)) |
246 |
|
|
exDataC.archiveData("data-archive2C"); |
247 |
|
|
|
248 |
|
|
print "\nDataEmpty:" |
249 |
|
|
archDataM=Data() |
250 |
|
|
archDataM.archiveData("data-archiveE") |
251 |
|
|
exDataM=Data() |
252 |
|
|
exDataM.extractData("data-archiveE",FunctionSpace()) |
253 |
|
|
exDataM.archiveData("data-archive2E"); |
254 |
|
|
|
255 |
jgs |
123 |
print "\n\nDo some Tagged data tests:" |
256 |
|
|
|
257 |
jgs |
121 |
print "\nCreate a Tagged data:" |
258 |
|
|
tagData=Data([ [1.0,1.1],[2.0,2.1] ],Function(msh)) |
259 |
|
|
tagData.setTaggedValue(1,[[3.0,3.1],[4.0,4.1]]) |
260 |
|
|
tagData.setTaggedValue(2,[[5.0,5.1],[6.0,6.1]]) |
261 |
|
|
print tagData |
262 |
|
|
print "\nSlice it [0:1,:]" |
263 |
|
|
print tagData[0:1] |
264 |
|
|
print "\nSlice it [0,0:1]" |
265 |
|
|
print tagData[0] |
266 |
|
|
print "\nSlice it [1:2]" |
267 |
|
|
print tagData[1:2] |
268 |
|
|
print "\nSlice it [1,1]" |
269 |
|
|
print tagData[1,1] |
270 |
|
|
|
271 |
|
|
print "\nSlice to it [1,1]" |
272 |
|
|
tagData[0,0] = tagData[1,1] |
273 |
|
|
print tagData |
274 |
|
|
|
275 |
|
|
print "\nSlice to it [1:2,0:1]" |
276 |
|
|
tagData[1:2,0:1] = tagData[0,1] |
277 |
|
|
print tagData |
278 |
|
|
|
279 |
|
|
print "\nSlice to it [0,1]" |
280 |
|
|
tagData[0,1] = 9.0 |
281 |
|
|
print tagData |
282 |
|
|
|
283 |
jgs |
147 |
sys.exit(0) |
284 |
jgs |
117 |
# end |