19 |
http://www.opensource.org/licenses/osl-3.0.php""" |
http://www.opensource.org/licenses/osl-3.0.php""" |
20 |
__url__="http://www.uq.edu.au/esscc/escript-finley" |
__url__="http://www.uq.edu.au/esscc/escript-finley" |
21 |
|
|
22 |
import sys |
import sys, math, re |
23 |
import unittest |
import unittest |
24 |
from esys.escript import * |
from esys.escript import * |
25 |
from esys.finley import ReadMesh |
from esys.finley import ReadMesh |
40 |
FINLEY_WORKDIR_PATH=FINLEY_WORKDIR+"/" |
FINLEY_WORKDIR_PATH=FINLEY_WORKDIR+"/" |
41 |
|
|
42 |
class Test_VisualizationInterface(unittest.TestCase): |
class Test_VisualizationInterface(unittest.TestCase): |
43 |
def check_vtk(self,f,reference_f): |
def check_vtk_old(self,f,reference_f): |
44 |
# if reference_f.startswith("tet_"): os.link(os.path.join(FINLEY_WORKDIR_PATH,f),os.path.join(FINLEY_TEST_MESH_PATH,reference_f)) |
# if reference_f.startswith("tet_"): os.link(os.path.join(FINLEY_WORKDIR_PATH,f),os.path.join(FINLEY_TEST_MESH_PATH,reference_f)) |
45 |
out_string=open(os.path.join(FINLEY_WORKDIR_PATH,f)).read().splitlines() |
out_string=open(os.path.join(FINLEY_WORKDIR_PATH,f)).read().splitlines() |
46 |
ref_string=open(os.path.join(FINLEY_TEST_MESH_PATH,reference_f)).read().splitlines() |
ref_string=open(os.path.join(FINLEY_TEST_MESH_PATH,reference_f)).read().splitlines() |
55 |
self.failUnlessEqual(line,ref_string[l].strip(),"line %d (%s) in vtk files does not match reference (%s)"%(c,line,ref_string[l].strip())) |
self.failUnlessEqual(line,ref_string[l].strip(),"line %d (%s) in vtk files does not match reference (%s)"%(c,line,ref_string[l].strip())) |
56 |
c+=1 |
c+=1 |
57 |
|
|
58 |
|
# Compare two lists of numbers (stored as space-separated strings) using the L2 norm |
59 |
|
def numericCompareL2(self, s1, s2): |
60 |
|
if s2 == None: return False |
61 |
|
TOL = 1.0e-6 |
62 |
|
vector1 = s1.split() |
63 |
|
vector2 = s2.split() |
64 |
|
if len(vector1) != len(vector2): return False |
65 |
|
diff = 0.0 |
66 |
|
for i in range(0, len(vector1)): |
67 |
|
tmp = float(vector1[i]) - float(vector2[i]) |
68 |
|
diff += tmp * tmp |
69 |
|
if math.sqrt(diff) > TOL: return False |
70 |
|
return True |
71 |
|
|
72 |
|
# Compare two elements (stored as space-separated strings of integers) after mapping node labels |
73 |
|
def elementCompareWithMap(self, e1, e2, map): |
74 |
|
if e2 == None: return False |
75 |
|
vector1 = e1.split() |
76 |
|
vector2 = e2.split() |
77 |
|
if len(vector1) != len(vector2): return False |
78 |
|
for i in range(0, len(vector1)): |
79 |
|
if int(vector1[i]) < 0: return False |
80 |
|
if int(vector1[i]) >= len(map): return False |
81 |
|
if map[int(vector1[i])] != int(vector2[i]): return False |
82 |
|
return True |
83 |
|
|
84 |
|
def compareDataSetWithMap(self, d1, d2, map): |
85 |
|
if len(d1) != len(d2): return False |
86 |
|
for i in range(0, len(d1)): |
87 |
|
if map[i] < 0: return False |
88 |
|
if map[i] >= len(d1): return False |
89 |
|
if not self.numericCompareL2(d1[i], d2[map[i]]): return False |
90 |
|
return True |
91 |
|
|
92 |
|
# Compare two VTK files which were generated by Finley method saveVTK |
93 |
|
def saveVtkCompare(self, file1, file2): |
94 |
|
lineList1 = open(file1).read().splitlines() |
95 |
|
lineList2 = open(file2).read().splitlines() |
96 |
|
|
97 |
|
# lineList1: Trim spaces and delete comments and empty lines |
98 |
|
for i in range(len(lineList1)-1, -1, -1): |
99 |
|
lineList1[i] = lineList1[i].strip() |
100 |
|
if lineList1[i][:2] == "<!": del(lineList1[i]) |
101 |
|
elif lineList1[i] == "": del(lineList1[i]) |
102 |
|
|
103 |
|
# lineList2: Trim spaces and delete comments and empty lines |
104 |
|
for i in range(len(lineList2)-1, -1, -1): |
105 |
|
lineList2[i] = lineList2[i].strip() |
106 |
|
if lineList2[i][:2] == "<!": del(lineList2[i]) |
107 |
|
elif lineList2[i] == "": del(lineList2[i]) |
108 |
|
|
109 |
|
if len(lineList1) != len(lineList2): |
110 |
|
return False, "Error: The two files have a different number of lines" |
111 |
|
|
112 |
|
# Now corresponding XML tags are guaranteed to be on the same line numbers |
113 |
|
|
114 |
|
# Do a simple string comparison except for data which might be permuted during optimization |
115 |
|
doStringComparison = 1 |
116 |
|
for i in range(0, len(lineList1)): |
117 |
|
if lineList1[i].startswith('</DataArray'): doStringComparison = 1 |
118 |
|
if doStringComparison and lineList1[i] != lineList2[i]: |
119 |
|
return False, "Files differ at line %d" % int(i+1) |
120 |
|
if lineList1[i].startswith('<DataArray'): doStringComparison = 0 |
121 |
|
if lineList1[i].startswith('<DataArray Name="offsets"'): doStringComparison = 1 |
122 |
|
if lineList1[i].startswith('<DataArray Name="types"'): doStringComparison = 1 |
123 |
|
|
124 |
|
# Find the list of nodes |
125 |
|
nodeList1 = [] |
126 |
|
nodeList2 = [] |
127 |
|
nodeDict1 = {} # These dictionaries are used to detect duplicate nodes, as with contact elements |
128 |
|
nodeDict2 = {} |
129 |
|
withinTheNodeList = False |
130 |
|
for i in range(0, len(lineList1)): |
131 |
|
if withinTheNodeList and lineList1[i].startswith('</DataArray'): break # Finished reading nodes |
132 |
|
if withinTheNodeList: |
133 |
|
nodeList1.append(lineList1[i]) # The two files are guaranteed to have the nodes on |
134 |
|
nodeList2.append(lineList2[i]) # the same lines at this point |
135 |
|
nodeDict1[lineList1[i]] = 1 |
136 |
|
nodeDict2[lineList2[i]] = 1 |
137 |
|
if lineList1[i].startswith('<DataArray'): withinTheNodeList = True |
138 |
|
|
139 |
|
# Check for contact elements |
140 |
|
if len(nodeList1) != len(nodeDict1): |
141 |
|
print "\n\nContact elements present, cannot compare nodes/elements due to duplicate nodes in %s\n" % file1 |
142 |
|
return True, "Contact elements present, cannot compare nodes/elements due to duplicate nodes in %s" % file1 |
143 |
|
if len(nodeList2) != len(nodeDict2): |
144 |
|
print "\n\nContact elements present, cannot compare nodes/elements due to duplicate nodes in %s\n" % file2 |
145 |
|
return True, "Contact elements present, cannot compare nodes/elements due to duplicate nodes in %s" % file2 |
146 |
|
|
147 |
|
if len(nodeList1) != len(nodeList2): |
148 |
|
return False, "Error: The two files have different numbers of nodes" |
149 |
|
|
150 |
|
# Compute the node mapping from file1 to file2 |
151 |
|
nodeMap1to2 = [] |
152 |
|
for i in range(0, len(nodeList1)): |
153 |
|
nodeMap1to2.append(-1) |
154 |
|
for i in range(0, len(nodeList1)): |
155 |
|
str = nodeList1[i] |
156 |
|
for j in range(len(nodeList2)-1, -1, -1): |
157 |
|
if self.numericCompareL2(str, nodeList2[j]): |
158 |
|
nodeMap1to2[i] = j |
159 |
|
nodeList2[j] = None |
160 |
|
break |
161 |
|
|
162 |
|
if nodeMap1to2.count(-1) > 0: |
163 |
|
return False, "Error: Some nodes didn't correspond in the two files" |
164 |
|
|
165 |
|
# Find the list of elements |
166 |
|
elementList1 = [] |
167 |
|
elementList2 = [] |
168 |
|
withinTheElementList = False |
169 |
|
for i in range(0, len(lineList1)): |
170 |
|
if withinTheElementList and lineList1[i].startswith('</DataArray'): break # Finished reading elements |
171 |
|
if withinTheElementList: |
172 |
|
elementList1.append(lineList1[i]) # The two files are guaranteed to have the elements on |
173 |
|
elementList2.append(lineList2[i]) # the same lines at this point |
174 |
|
if lineList1[i].startswith('<DataArray Name="connectivity"'): withinTheElementList = True |
175 |
|
|
176 |
|
if len(elementList1) != len(elementList2): |
177 |
|
return False, "Error: The two files have different numbers of elements" |
178 |
|
|
179 |
|
# Compute the element mapping from file1 to file2 |
180 |
|
elementMap1to2 = [] |
181 |
|
for i in range(0, len(elementList1)): |
182 |
|
elementMap1to2.append(-1) |
183 |
|
for i in range(0, len(elementList1)): |
184 |
|
str = elementList1[i] |
185 |
|
for j in range(len(elementList2)-1, -1, -1): |
186 |
|
if self.elementCompareWithMap(str, elementList2[j], nodeMap1to2): |
187 |
|
elementMap1to2[i] = j |
188 |
|
elementList2[j] = None |
189 |
|
break |
190 |
|
|
191 |
|
if elementMap1to2.count(-1) > 0: |
192 |
|
return False, "Error: Some elements didn't correspond in the two files" |
193 |
|
|
194 |
|
# Find the data sets and compare them |
195 |
|
dataList1 = [] |
196 |
|
dataList2 = [] |
197 |
|
withinCelldata = False |
198 |
|
withinPointdata = False |
199 |
|
withinDataSet = False |
200 |
|
for i in range(0, len(lineList1)): |
201 |
|
if (withinCelldata or withinPointdata) and withinDataSet and lineList1[i].startswith('</DataArray'): |
202 |
|
withinDataSet = False # Finished reading one of the data sets |
203 |
|
if withinCelldata and not self.compareDataSetWithMap(dataList1, dataList2, elementMap1to2): |
204 |
|
return False, "Error: element data in '%s' did not match" % dataName |
205 |
|
if withinPointdata and not self.compareDataSetWithMap(dataList1, dataList2, nodeMap1to2): |
206 |
|
return False, "Error: point data in '%s' did not match" % dataName |
207 |
|
if withinDataSet: |
208 |
|
dataList1.append(lineList1[i]) # The two files are guaranteed to have the elements on |
209 |
|
dataList2.append(lineList2[i]) # the same lines at this point |
210 |
|
if (withinCelldata or withinPointdata) and lineList1[i].startswith('<DataArray'): |
211 |
|
dataName = re.sub('" .*', '', lineList1[i]) |
212 |
|
dataName = re.sub('.*"', '', dataName) |
213 |
|
withinDataSet = True |
214 |
|
dataList1 = [] |
215 |
|
dataList2 = [] |
216 |
|
if lineList1[i].startswith('<PointData'): withinPointdata = True |
217 |
|
if lineList1[i].startswith('</PointData'): withinPointdata = False |
218 |
|
if lineList1[i].startswith('<CellData'): withinCelldata = True |
219 |
|
if lineList1[i].startswith('</CellData'): withinCelldata = False |
220 |
|
|
221 |
|
return True, "Your VTK files match" |
222 |
|
# End of method self.saveVtkCompare |
223 |
|
|
224 |
|
def check_vtk(self,f,reference_f): |
225 |
|
success, reason = self.saveVtkCompare(os.path.join(FINLEY_WORKDIR_PATH,f), os.path.join(FINLEY_TEST_MESH_PATH,reference_f)) |
226 |
|
self.failUnless(success, reason) |
227 |
|
|
228 |
def check_dx(self,f,reference_f): |
def check_dx(self,f,reference_f): |
229 |
out_string=open(FINLEY_WORKDIR_PATH+f).read().splitlines() |
out_string=open(FINLEY_WORKDIR_PATH+f).read().splitlines() |
230 |
ref_string=open(FINLEY_TEST_MESH_PATH+reference_f).read().splitlines() |
ref_string=open(FINLEY_TEST_MESH_PATH+reference_f).read().splitlines() |