1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | import tempfile |
---|
4 | import unittest |
---|
5 | from mesh import * |
---|
6 | from load_mesh.loadASCII import * |
---|
7 | from coordinate_transforms.geo_reference import Geo_reference |
---|
8 | |
---|
9 | class meshTestCase(unittest.TestCase): |
---|
10 | def setUp(self): |
---|
11 | pass |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | def tearDown(self): |
---|
16 | pass |
---|
17 | |
---|
18 | def testPointDistance(self): |
---|
19 | a = Point(0.0, 0.0) |
---|
20 | b = Point(0.0, 10.0) |
---|
21 | |
---|
22 | self.failUnless( a.DistanceToPoint(b) == 10.0, |
---|
23 | 'Point DistanceToPoint is wrong!') |
---|
24 | |
---|
25 | def testVertexDistance(self): |
---|
26 | a = Vertex (0.0, 0.0) |
---|
27 | b = Vertex (0.0, 10.0) |
---|
28 | |
---|
29 | self.failUnless( a.DistanceToPoint(b) == 10.0, |
---|
30 | 'Point DistanceToPoint is wrong!') |
---|
31 | |
---|
32 | def testTriangle(self): |
---|
33 | a = Vertex (0.0, 0.0) |
---|
34 | b = Vertex (0.0, 2.0) |
---|
35 | c = Vertex (2.0,0.0) |
---|
36 | d = Vertex (0.0, 4.0) |
---|
37 | e = Vertex (2.0, 2.0) |
---|
38 | f = Vertex (4.0,0.0) |
---|
39 | |
---|
40 | t1 = Triangle(b,a,c) |
---|
41 | t2 = Triangle(b,c,e) |
---|
42 | t3 = Triangle(e,c,f) |
---|
43 | t4 = Triangle(d,b,e) |
---|
44 | t2.setNeighbors(t3,t4,t1) |
---|
45 | |
---|
46 | self.failUnless( t2.neighbors[2].vertices[0] == b, 'Triangle initialisation is wrong!') |
---|
47 | |
---|
48 | |
---|
49 | def testSegment(self): |
---|
50 | a = Vertex (0.0, 0.0) |
---|
51 | b = Vertex (0.0, 10.0) |
---|
52 | s = Segment(a,b, tag = 20) |
---|
53 | |
---|
54 | self.failUnless( s.vertices[0].DistanceToPoint(s.vertices[1]) == 10.0, |
---|
55 | 'vertices in a segment are wrong') |
---|
56 | |
---|
57 | self.failUnless( s.tag == 20.0, |
---|
58 | 'tag in a segment are wrong') |
---|
59 | |
---|
60 | def testdeleteUserVertex(self): |
---|
61 | |
---|
62 | |
---|
63 | mesh = Mesh() |
---|
64 | a = mesh.addUserVertex(0.0, 0.0) |
---|
65 | b = mesh.addUserVertex (0.0, 2.0) |
---|
66 | c = mesh.addUserVertex (2.0,0.0) |
---|
67 | |
---|
68 | s1 = mesh.addUserSegment(a,b) |
---|
69 | s2 = mesh.addUserSegment(a,c) |
---|
70 | s3 = mesh.addUserSegment(c,b) |
---|
71 | |
---|
72 | mesh.deleteMeshObject (a) |
---|
73 | self.failUnless(mesh.userSegments[0] == s3, |
---|
74 | 'Bad segment. ') |
---|
75 | self.failUnless(len(mesh.userSegments) ==1, |
---|
76 | 'Segments not deleted.') |
---|
77 | self.failUnless(len(mesh.userVertices) == 2, |
---|
78 | 'Vertex not deleted.') |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | def testgenerateMesh(self): |
---|
83 | a = Vertex (0.0, 0.0) |
---|
84 | d = Vertex (0.0, 4.0) |
---|
85 | f = Vertex (4.0,0.0) |
---|
86 | |
---|
87 | s1 = Segment(a,d) |
---|
88 | s2 = Segment(d,f) |
---|
89 | s3 = Segment(a,f) |
---|
90 | |
---|
91 | r1 = Region(0.3, 0.3,tag = 1.3,maxArea = .6) |
---|
92 | #print r1 |
---|
93 | m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3], regions=[r1] ) |
---|
94 | |
---|
95 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
96 | |
---|
97 | #print m |
---|
98 | |
---|
99 | #m.plotMeshTriangle() |
---|
100 | |
---|
101 | result = 1.414214 |
---|
102 | delta = 0.00001 |
---|
103 | |
---|
104 | self.failUnless((m.meshTriangles[1].vertices[0].x < result + delta) or |
---|
105 | (m.meshTriangles[1].vertices[0].x > result - delta), |
---|
106 | 'generated mesh is wrong!') |
---|
107 | |
---|
108 | def test_regionalMaxArea(self): |
---|
109 | v0 = Vertex (0.0, 0.0) |
---|
110 | v1 = Vertex (6.0, 0.0) |
---|
111 | v2 = Vertex (6.0,6.0) |
---|
112 | v3 = Vertex (0.0,6.0) |
---|
113 | |
---|
114 | s1 = Segment(v0,v1) |
---|
115 | s2 = Segment(v1,v2) |
---|
116 | s3 = Segment(v3,v2) |
---|
117 | s4 = Segment(v3,v0) |
---|
118 | s5 = Segment(v2,v0) |
---|
119 | |
---|
120 | r1 = Region(3, 1,tag = 1.3) |
---|
121 | #print r1 |
---|
122 | m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], regions=[r1] ) |
---|
123 | |
---|
124 | m.generateMesh("QApz", maxArea = 36 ) |
---|
125 | |
---|
126 | #m.plotMeshTriangle() |
---|
127 | #print "len(m.meshTriangles)",len(m.meshTriangles) |
---|
128 | |
---|
129 | self.failUnless(len(m.meshTriangles) == 2, |
---|
130 | 'test_regionalMaxArea 1:generated mesh is wrong!') |
---|
131 | |
---|
132 | ## Another test case |
---|
133 | r1 = Region(3, 1,tag = 1.3) |
---|
134 | r2 = Region(1, 3,tag = 1.3, maxArea = 8) |
---|
135 | m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], |
---|
136 | regions=[r1,r2] ) |
---|
137 | m.generateMesh("QApz", maxArea = 36 ) |
---|
138 | |
---|
139 | self.failUnless(len(m.meshTriangles) >= 6, |
---|
140 | 'testregion_with_maxarea 2: # of tris is wrong!') |
---|
141 | |
---|
142 | |
---|
143 | ## Another test case |
---|
144 | r1 = Region(3, 1, tag = 1.3, maxArea = 8) |
---|
145 | r2 = Region(1, 3, tag = 1.3, maxArea = 8) |
---|
146 | m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], |
---|
147 | regions=[r1,r2] ) |
---|
148 | m.generateMesh("QApz", maxArea = 36 ) |
---|
149 | #print "len(m.meshTriangles)",len(m.meshTriangles) |
---|
150 | |
---|
151 | self.failUnless(len(m.meshTriangles) >= 8, |
---|
152 | 'testregion_with_maxarea 3: # of tris is wrong!') |
---|
153 | |
---|
154 | |
---|
155 | ## Another test case |
---|
156 | r1 = Region(3, 1, tag = 1.3 ) |
---|
157 | r2 = Region(1, 3, tag = 1.3, maxArea = 8) |
---|
158 | m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], |
---|
159 | regions=[r1,r2] ) |
---|
160 | m.generateMesh("QApz", maxArea = 8 ) |
---|
161 | self.failUnless(len(m.meshTriangles) >= 8, |
---|
162 | 'testregion_with_maxarea 4: # of tris is wrong!') |
---|
163 | |
---|
164 | |
---|
165 | ## Another test case |
---|
166 | r1 = Region(3, 1,tag = 1.3, maxArea = 8) |
---|
167 | r2 = Region(1, 3,tag = 1.3, maxArea = 8) |
---|
168 | m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], |
---|
169 | regions=[r1,r2] ) |
---|
170 | m.generateMesh("QApz", maxArea = 36,isRegionalMaxAreas = False ) |
---|
171 | self.failUnless(len(m.meshTriangles) == 2, |
---|
172 | 'test_regionalMaxArea 5:generated mesh is wrong!') |
---|
173 | |
---|
174 | def testdeleteUserVertex(self): |
---|
175 | mesh = Mesh() |
---|
176 | a = mesh.addUserVertex(0.0, 0.0) |
---|
177 | b = mesh.addUserVertex (0.0, 2.0) |
---|
178 | c = mesh.addUserVertex (2.0,0.0) |
---|
179 | |
---|
180 | s1 = mesh.addUserSegment(a,b) |
---|
181 | s2 = mesh.addUserSegment(a,c) |
---|
182 | s3 = mesh.addUserSegment(c,b) |
---|
183 | |
---|
184 | mesh.deleteMeshObject (s2) |
---|
185 | |
---|
186 | #print ",s2 in mesh.userSegments" ,s2 in mesh.userSegments |
---|
187 | self.failUnless(not(s2 in mesh.userSegments), |
---|
188 | 'Bad segment. ') |
---|
189 | self.failUnless(len(mesh.userSegments) ==2, |
---|
190 | 'Segments not deleted.') |
---|
191 | self.failUnless(len(mesh.userVertices) == 3, |
---|
192 | 'Vertex deleted, instead of segment.') |
---|
193 | |
---|
194 | def testTriangleArea(self): |
---|
195 | a = Vertex (10.0, 10.0) |
---|
196 | b = Vertex (10.0, 20.0) |
---|
197 | c = Vertex (20.0,10.0) |
---|
198 | |
---|
199 | d = Vertex (-20.0, 0.0) |
---|
200 | e = Vertex (-20.0, -20.0) |
---|
201 | f = Vertex (20.0,-20.0) |
---|
202 | |
---|
203 | t1 = Triangle(b,a,c) |
---|
204 | t2 = Triangle(e,d,f) |
---|
205 | |
---|
206 | # print "t1", t1 |
---|
207 | # print "t1 area ", t1.calcArea() |
---|
208 | # print "t2", t2 |
---|
209 | # print "t2 area ", t2.calcArea() |
---|
210 | self.failUnless( t1.calcArea() == 50 and t2.calcArea() == 400, 'Triangle area is wrong!') |
---|
211 | def testisUserSegmentNew (self): |
---|
212 | mesh = Mesh() |
---|
213 | a = mesh.addUserVertex(0.0, 0.0) |
---|
214 | b = mesh.addUserVertex (0.0, 2.0) |
---|
215 | c = mesh.addUserVertex (2.0,0.0) |
---|
216 | d = mesh.addUserVertex (2.0,3.0) |
---|
217 | |
---|
218 | s1 = mesh.addUserSegment(a,b) |
---|
219 | s2 = mesh.addUserSegment(a,c) |
---|
220 | s3 = mesh.addUserSegment(c,b) |
---|
221 | |
---|
222 | self.failUnless(mesh.isUserSegmentNew(a,d) , |
---|
223 | 'Segment should be new. ') |
---|
224 | self.failUnless(not(mesh.isUserSegmentNew(a,b)) , |
---|
225 | 'Segment should not be new. ') |
---|
226 | |
---|
227 | |
---|
228 | def testisUserSegmentNew (self): |
---|
229 | mesh = Mesh() |
---|
230 | a = mesh.addUserVertex(0.0, 0.0) |
---|
231 | b = mesh.addUserVertex (0.0, 2.0) |
---|
232 | c = mesh.addUserVertex (2.0,0.0) |
---|
233 | d = mesh.addUserVertex (2.0,3.0) |
---|
234 | |
---|
235 | s1 = mesh.addUserSegment(a,b) |
---|
236 | s2 = mesh.addUserSegment(a,c) |
---|
237 | s3 = mesh.addUserSegment(c,b) |
---|
238 | |
---|
239 | self.failUnless(mesh.representedUserSegment(a,d) == None, |
---|
240 | 'Segment should be new. ') |
---|
241 | self.failUnless(mesh.representedUserSegment(a,b) == s1 , |
---|
242 | 'Segment should not be new. ') |
---|
243 | |
---|
244 | def testautoSegment(self): |
---|
245 | p0 = Vertex (0.0, 0.0) |
---|
246 | p1 = Vertex (0.0, 4.0) |
---|
247 | p2 = Vertex (4.0,4.0) |
---|
248 | p3 = Vertex (4.0,0.0) |
---|
249 | |
---|
250 | s1 = Segment(p0,p1) |
---|
251 | |
---|
252 | m = Mesh(userVertices=[p0, p1, p2, p3], userSegments=[s1] ) |
---|
253 | m.autoSegment() |
---|
254 | |
---|
255 | #print 'Len', len(m.userSegments) |
---|
256 | self.failUnless(len(m.getUserSegments()) == 4 , |
---|
257 | 'userSegments is wrong!') |
---|
258 | |
---|
259 | m.autoSegment() |
---|
260 | self.failUnless(len(m.getUserSegments()) == 4 , |
---|
261 | 'userSegments is wrong!') |
---|
262 | |
---|
263 | def testautoSegmentII(self): |
---|
264 | p1 = Vertex (3.0, 4.0) |
---|
265 | p2 = Vertex (3.0,2.0) |
---|
266 | p3 = Vertex (3.0,0.0) |
---|
267 | p4 = Vertex (6.0, 4.0) |
---|
268 | p5 = Vertex (6.0,2.0) |
---|
269 | p0 = Vertex (6.0,0.0) |
---|
270 | |
---|
271 | |
---|
272 | s1 = Segment(p2,p3) |
---|
273 | s2 = Segment(p4,p5) |
---|
274 | |
---|
275 | m = Mesh(userVertices=[p0, p1, p2, p3, p4, p5], |
---|
276 | userSegments=[s1, s2]) |
---|
277 | |
---|
278 | m.autoSegment() |
---|
279 | |
---|
280 | s3 = m.representedAlphaUserSegment(p3,p0) |
---|
281 | self.failUnless(not (s3 == None) , |
---|
282 | 'userSegments is wrong!') |
---|
283 | |
---|
284 | |
---|
285 | s6 = m.representedAlphaUserSegment(p1,p4) |
---|
286 | self.failUnless(not (s6 == None) , |
---|
287 | 'userSegments is wrong!') |
---|
288 | |
---|
289 | # remove a segment, add a point, autosegment |
---|
290 | m.alphaUserSegments.remove(s3) |
---|
291 | p6 = Vertex (1.0, 2.0) |
---|
292 | m.userVertices.append(p6) |
---|
293 | |
---|
294 | m.autoSegment() |
---|
295 | |
---|
296 | s1_now = m.representedUserSegment(p3,p2) |
---|
297 | self.failUnless(s1_now == s1 , |
---|
298 | 'userSegments is wrong!') |
---|
299 | |
---|
300 | s2_now = m.representedUserSegment(p5,p4) |
---|
301 | self.failUnless(s2_now == s2 , |
---|
302 | 'userSegments is wrong!') |
---|
303 | |
---|
304 | s3 = m.representedAlphaUserSegment(p3,p6) |
---|
305 | self.failUnless(not (s3 == None) , |
---|
306 | 'userSegments is wrong!') |
---|
307 | |
---|
308 | s4 = m.representedAlphaUserSegment(p3,p6) |
---|
309 | self.failUnless(not (s4 == None) , |
---|
310 | 'userSegments is wrong!') |
---|
311 | |
---|
312 | s5 = m.representedAlphaUserSegment(p4,p6) |
---|
313 | self.failUnless(s5 == None , |
---|
314 | 'userSegments is wrong!') |
---|
315 | #print m |
---|
316 | |
---|
317 | def testRegions(self): |
---|
318 | a = Vertex (0.0, 0.0) |
---|
319 | b = Vertex (0.0, 2.0) |
---|
320 | c = Vertex (2.0,0.0) |
---|
321 | d = Vertex (0.0, 4.0) |
---|
322 | e = Vertex (2.0, 2.0) |
---|
323 | f = Vertex (4.0,0.0) |
---|
324 | g = Vertex (0.0,-2.0) |
---|
325 | |
---|
326 | Segment.set_default_tag("") |
---|
327 | s1 = Segment(a,b) |
---|
328 | s2 = Segment(b,e) |
---|
329 | s3 = Segment(e,c) |
---|
330 | s4 = Segment(c,a) |
---|
331 | s5 = Segment(b,d) |
---|
332 | s6 = Segment(e,d) |
---|
333 | s7 = Segment(e,f) |
---|
334 | s8 = Segment(c,f) |
---|
335 | s9 = Segment(g,c) |
---|
336 | s10 = Segment(g,a) |
---|
337 | |
---|
338 | r1 = Region(0.1,0.1,tag="22") |
---|
339 | r2 = Region(0.1,2.1,tag="11") |
---|
340 | r3 = Region(2.1,0.1) |
---|
341 | |
---|
342 | m = Mesh(userVertices=[a,b,c,d,e,f,g], userSegments=[s1,s2,s3,s4,s5,s6,s7,s8,s9,s10], regions=[r1,r2,r3] ) |
---|
343 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
344 | #print m |
---|
345 | Triangulation = m.getTriangulation() |
---|
346 | #print Triangulation[0].attribute |
---|
347 | #print Triangulation[1].attribute |
---|
348 | #print Triangulation[2].attribute |
---|
349 | #print Triangulation[3].attribute |
---|
350 | #print Triangulation[4].attribute |
---|
351 | |
---|
352 | self.failUnless(Triangulation[0].attribute == "" and |
---|
353 | Triangulation[1].attribute == "22" and |
---|
354 | Triangulation[2].attribute == "" and |
---|
355 | Triangulation[3].attribute == "11" and |
---|
356 | Triangulation[4].attribute == "22" , |
---|
357 | 'region attributes are wrong!') |
---|
358 | |
---|
359 | def test_vertexAttribs(self): |
---|
360 | a = Vertex (0.0, 0.0, attributes = [12.0,2.0]) |
---|
361 | d = Vertex (0.0, 4.0, attributes = [9.0,7.0]) |
---|
362 | f = Vertex (4.0,0.0, attributes = [14.0,3.0]) |
---|
363 | |
---|
364 | Segment.set_default_tag("") |
---|
365 | s1 = Segment(a,d) |
---|
366 | s2 = Segment(d,f) |
---|
367 | s3 = Segment(a,f) |
---|
368 | |
---|
369 | r1 = Region(0.3, 0.3, tag = 88.9) |
---|
370 | |
---|
371 | m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3], regions=[r1]) |
---|
372 | |
---|
373 | m.generateMesh("QApz", maxArea = 2.1) |
---|
374 | |
---|
375 | vert = m.getMeshVertices() |
---|
376 | |
---|
377 | self.failUnless(vert[0].attributes == [12.0, 2.0] and |
---|
378 | vert[1].attributes == [9.0, 7.0] and |
---|
379 | vert[2].attributes == [14.0,3.0] and |
---|
380 | vert[3].attributes == [12.232233047033631, 4.4142135623730949] and |
---|
381 | vert[4].attributes == [13.0, 2.5] , |
---|
382 | 'vertex attributes are wrong!') |
---|
383 | |
---|
384 | |
---|
385 | def test_vertexAttribs2(self): |
---|
386 | |
---|
387 | a = Vertex (0.0, 0.0) |
---|
388 | d = Vertex (0.0, 4.0) |
---|
389 | f = Vertex (4.0,0.0) |
---|
390 | |
---|
391 | Segment.set_default_tag("") |
---|
392 | s1 = Segment(a,d) |
---|
393 | s2 = Segment(d,f) |
---|
394 | s3 = Segment(a,f) |
---|
395 | |
---|
396 | r1 = Region(0.3, 0.3, tag = 88.9) |
---|
397 | |
---|
398 | m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3], regions=[r1]) |
---|
399 | |
---|
400 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
401 | |
---|
402 | vert = m.getMeshVertices() |
---|
403 | self.failUnless(vert[0].attributes == [] and |
---|
404 | vert[1].attributes == [] and |
---|
405 | vert[2].attributes == [] and |
---|
406 | vert[3].attributes == [] and |
---|
407 | vert[4].attributes == [], |
---|
408 | 'vertex attributes are wrong!') |
---|
409 | |
---|
410 | def test_segtag(self): |
---|
411 | |
---|
412 | a = Vertex (0.0, 0.0) |
---|
413 | d = Vertex (0.0, 4.0) |
---|
414 | f = Vertex (4.0,0.0) |
---|
415 | |
---|
416 | s1 = Segment(a,d,tag = 5) |
---|
417 | s2 = Segment(d,f,tag = 7) |
---|
418 | s3 = Segment(a,f,tag = 9) |
---|
419 | |
---|
420 | m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3]) |
---|
421 | |
---|
422 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
423 | |
---|
424 | m.export_mesh_file("from_test_mesh.tsh") |
---|
425 | seg = m.getMeshSegments() |
---|
426 | #print "seg",seg |
---|
427 | #print "seg[0].tag" |
---|
428 | #print seg[0].tag |
---|
429 | #print "seg[0].tag" |
---|
430 | |
---|
431 | self.failUnless(seg[0].tag == 5 and |
---|
432 | seg[1].tag == 7 and |
---|
433 | seg[2].tag == 9 and |
---|
434 | seg[3].tag == 7 and |
---|
435 | seg[4].tag == 9, |
---|
436 | 'seg tags are wrong') |
---|
437 | |
---|
438 | |
---|
439 | def test_segtag2(self): |
---|
440 | |
---|
441 | a = Vertex (0.0, 0.0) |
---|
442 | d = Vertex (0.0, 4.0) |
---|
443 | f = Vertex (4.0,0.0) |
---|
444 | e = Vertex (1.0,1.0) |
---|
445 | |
---|
446 | s1 = Segment(a,d) |
---|
447 | s2 = Segment(d,f) |
---|
448 | s3 = Segment(a,f) |
---|
449 | s4 = Segment(a,e) |
---|
450 | |
---|
451 | m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4]) |
---|
452 | |
---|
453 | m.generateMesh("QApz", maxArea = 2.1) |
---|
454 | |
---|
455 | seg = m.getMeshSegments() |
---|
456 | self.failUnless(seg[0].tag == "exterior" and |
---|
457 | seg[1].tag == "exterior" and |
---|
458 | seg[2].tag == "exterior" and |
---|
459 | seg[3].tag == "" and |
---|
460 | seg[4].tag == "exterior", |
---|
461 | '2nd seg tags are wrong') |
---|
462 | |
---|
463 | def test_asciiFile(self): |
---|
464 | |
---|
465 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
466 | d = Vertex (0.0, 4.0) #, attributes = [1.2]) |
---|
467 | f = Vertex (4.0,0.0) #, attributes = [1.3]) |
---|
468 | e = Vertex (1.0,1.0) #, attributes = [1.4]) |
---|
469 | |
---|
470 | s1 = Segment(a,d) |
---|
471 | s2 = Segment(d,f) |
---|
472 | s3 = Segment(a,f) |
---|
473 | s4 = Segment(a,e) |
---|
474 | |
---|
475 | m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4]) |
---|
476 | |
---|
477 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
478 | seg = m.getMeshSegments() |
---|
479 | |
---|
480 | fileName = tempfile.mktemp(".tsh") |
---|
481 | m.export_mesh_file(fileName) |
---|
482 | file = open(fileName) |
---|
483 | lFile = file.read().split('\n') |
---|
484 | file.close() |
---|
485 | os.remove(fileName) |
---|
486 | |
---|
487 | #print "@^@^" |
---|
488 | #for l in lFile: |
---|
489 | # print l,"<" |
---|
490 | #print "@^@^" |
---|
491 | |
---|
492 | # no need to check the title again |
---|
493 | #self.failUnless(lFile[0] == "5 0 # <vertex #> <x> <y> [attributes] ...Triangulation Vertices..." |
---|
494 | # ,'Ascii file is wrong, vertex title') |
---|
495 | self.failUnless(lFile[1] == "0 0.0 0.0 " and #1.1 " and |
---|
496 | lFile[2] == "1 0.0 4.0 " and #1.2 " and |
---|
497 | lFile[3] == "2 4.0 0.0 " and #1.3 " and |
---|
498 | lFile[4] == "3 1.0 1.0 " and #1.4 " and |
---|
499 | lFile[5] == "4 2.0 2.0 " #1.25 " |
---|
500 | , |
---|
501 | 'Ascii file is wrong, vertex') |
---|
502 | |
---|
503 | #self.failUnless(lFile[6] == "# attribute column titles ...Triangulation Vertex Titles..." |
---|
504 | # ,'Ascii file is wrong, attribute column title') |
---|
505 | self.failUnless(lFile[8] == "0 3 2 4 -1 2 3 " and |
---|
506 | lFile[9] == "1 1 0 3 3 2 -1 " and |
---|
507 | lFile[10] == "2 3 4 1 -1 1 0 " and |
---|
508 | lFile[11] == "3 2 3 0 1 -1 0 " |
---|
509 | , |
---|
510 | 'Ascii file is wrong, triangle') |
---|
511 | |
---|
512 | self.failUnless( lFile[13] == "0 0 1 exterior" and |
---|
513 | lFile[14] == "1 1 4 exterior" and |
---|
514 | lFile[15] == "2 2 0 exterior" and |
---|
515 | lFile[16] == "3 0 3 " and |
---|
516 | lFile[17] == "4 4 2 exterior" , |
---|
517 | 'Ascii file is wrong, segment') |
---|
518 | |
---|
519 | # self.failUnless(lFile[18] == '4 0 # <vertex #> <x> <y> [attributes] ...Mesh Vertices...', |
---|
520 | # 'Ascii file is wrong, Mesh Vertices Title') |
---|
521 | |
---|
522 | self.failUnless(lFile[19] == '0 0.0 0.0 ' and #1.1 ' and |
---|
523 | lFile[20] == '1 0.0 4.0 ' and #1.2 ' and |
---|
524 | lFile[21] == '2 4.0 0.0 ' and #1.3 ' and |
---|
525 | lFile[22] == '3 1.0 1.0 ' #1.4 ' |
---|
526 | , |
---|
527 | 'Ascii file is wrong, Mesh Vertices II') |
---|
528 | |
---|
529 | self.failUnless(lFile[24] == '0 0 1 ' and |
---|
530 | lFile[25] == '1 1 2 ' and |
---|
531 | lFile[26] == '2 0 2 ' and |
---|
532 | lFile[27] == '3 0 3 ' |
---|
533 | ,'Ascii file is wrong, Mesh Segments') |
---|
534 | |
---|
535 | |
---|
536 | def test_ascii_file(self): |
---|
537 | |
---|
538 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
539 | d = Vertex (0.0, 4.0) #, attributes = [1.2]) |
---|
540 | f = Vertex (4.0,0.0) #, attributes = [1.3]) |
---|
541 | e = Vertex (1.0,1.0) #, attributes = [1.4]) |
---|
542 | |
---|
543 | s1 = Segment(a,d) |
---|
544 | s2 = Segment(d,f) |
---|
545 | s3 = Segment(a,f) |
---|
546 | s4 = Segment(a,e) |
---|
547 | |
---|
548 | m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4]) |
---|
549 | |
---|
550 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
551 | |
---|
552 | seg = m.getMeshSegments() |
---|
553 | |
---|
554 | fileName = tempfile.mktemp(".tsh") |
---|
555 | m.export_mesh_file(fileName) |
---|
556 | file = open(fileName) |
---|
557 | lFile = file.read().split('\n') |
---|
558 | file.close() |
---|
559 | os.remove(fileName) |
---|
560 | |
---|
561 | #print "@^@^" |
---|
562 | #for l in lFile: |
---|
563 | # print l,"<" |
---|
564 | #print "@^@^" |
---|
565 | self.failUnless(lFile[0] == "5 0 # <# of verts> <# of vert attributes>, next lines <vertex #> <x> <y> [attributes] ...Triangulation Vertices..." |
---|
566 | , |
---|
567 | 'Ascii file is wrong, vertex title') |
---|
568 | self.failUnless(lFile[1] == "0 0.0 0.0 " and #1.1 " and |
---|
569 | lFile[2] == "1 0.0 4.0 " and #1.2 " and |
---|
570 | lFile[3] == "2 4.0 0.0 " and #1.3 " and |
---|
571 | lFile[4] == "3 1.0 1.0 " and #1.4 " and |
---|
572 | lFile[5] == "4 2.0 2.0 " #1.25 " |
---|
573 | , |
---|
574 | 'Ascii file is wrong, vertex') |
---|
575 | |
---|
576 | self.failUnless(lFile[6] == "# attribute column titles ...Triangulation Vertex Titles..." |
---|
577 | , |
---|
578 | 'Ascii file is wrong, attribute column title') |
---|
579 | self.failUnless(lFile[7] == "4 # <# of triangles>, next lines <triangle #> [<vertex #>] [<neigbouring triangle #>] [attribute of region] ...Triangulation Triangles..." and |
---|
580 | lFile[8] == "0 3 2 4 -1 2 3 " and |
---|
581 | lFile[9] == "1 1 0 3 3 2 -1 " and |
---|
582 | lFile[10] == "2 3 4 1 -1 1 0 " and |
---|
583 | lFile[11] == "3 2 3 0 1 -1 0 " |
---|
584 | , |
---|
585 | 'Ascii file is wrong, triangle') |
---|
586 | |
---|
587 | self.failUnless(lFile[12] == "5 # <# of segments>, next lines <segment #> <vertex #> <vertex #> [boundary tag] ...Triangulation Segments..." and |
---|
588 | lFile[13] == "0 0 1 exterior" and |
---|
589 | lFile[14] == "1 1 4 exterior" and |
---|
590 | lFile[15] == "2 2 0 exterior" and |
---|
591 | lFile[16] == "3 0 3 " and |
---|
592 | lFile[17] == "4 4 2 exterior" , |
---|
593 | 'Ascii file is wrong, segment') |
---|
594 | |
---|
595 | self.failUnless(lFile[18] == '4 0 # <# of verts> <# of vert attributes>, next lines <vertex #> <x> <y> [attributes] ...Mesh Vertices...', |
---|
596 | 'Ascii file is wrong, Mesh Vertices Title') |
---|
597 | |
---|
598 | self.failUnless(lFile[19] == '0 0.0 0.0 ' and #1.1 ' and |
---|
599 | lFile[20] == '1 0.0 4.0 ' and #1.2 ' and |
---|
600 | lFile[21] == '2 4.0 0.0 ' and #1.3 ' and |
---|
601 | lFile[22] == '3 1.0 1.0 ' #1.4 ' |
---|
602 | , |
---|
603 | 'Ascii file is wrong, Mesh Vertices II') |
---|
604 | |
---|
605 | self.failUnless(lFile[23] == '4 # <# of segments>, next lines <segment #> <vertex #> <vertex #> [boundary tag] ...Mesh Segments...' and |
---|
606 | lFile[24] == '0 0 1 ' and |
---|
607 | lFile[25] == '1 1 2 ' and |
---|
608 | lFile[26] == '2 0 2 ' and |
---|
609 | lFile[27] == '3 0 3 ' and |
---|
610 | lFile[28] == '0 # <# of holes>, next lines <Hole #> <x> <y> ...Mesh Holes...' and |
---|
611 | lFile[29] == '0 # <# of regions>, next lines <Region #> <x> <y> <tag>...Mesh Regions...' |
---|
612 | , |
---|
613 | 'Ascii file is wrong, Mesh Segments') |
---|
614 | |
---|
615 | |
---|
616 | def test_thinoutVertices(self): |
---|
617 | |
---|
618 | v1 = Vertex(-20,-20) |
---|
619 | v2 = Vertex(-11,-11) |
---|
620 | v3 = Vertex(-10,-10) |
---|
621 | v4 = Vertex(-9,-1) |
---|
622 | v5 = Vertex(-8,2) |
---|
623 | v6 = Vertex(6,3) |
---|
624 | v7 = Vertex(12,9) |
---|
625 | v8 = Vertex(15,3) |
---|
626 | v9 = Vertex(24,3) |
---|
627 | m = Mesh(userVertices = [v1,v2,v3,v4,v5,v6,v7,v8,v9]) |
---|
628 | m.thinoutVertices(10) |
---|
629 | |
---|
630 | self.failUnless(v1 in m.userVertices, |
---|
631 | 'test_thinoutVertices, test 1 failed') |
---|
632 | self.failUnless(v3 in m.userVertices, |
---|
633 | 'test_thinoutVertices, test 2 failed') |
---|
634 | self.failUnless(v4 in m.userVertices, |
---|
635 | 'test_thinoutVertices, test 3 failed') |
---|
636 | self.failUnless(v6 in m.userVertices, |
---|
637 | 'test_thinoutVertices, test 4 failed') |
---|
638 | self.failUnless(v7 in m.userVertices, |
---|
639 | 'test_thinoutVertices, test 5 failed') |
---|
640 | self.failUnless(v9 in m.userVertices, |
---|
641 | 'test_thinoutVertices, test 6 failed') |
---|
642 | self.failUnless(v5 not in m.userVertices, |
---|
643 | 'test_thinoutVertices, test 7 failed') |
---|
644 | self.failUnless(v2 not in m.userVertices, |
---|
645 | 'test_thinoutVertices, test 8 failed') |
---|
646 | self.failUnless(v8 not in m.userVertices, |
---|
647 | 'test_thinoutVertices, test 9 failed') |
---|
648 | |
---|
649 | def test_same_x_y(self): |
---|
650 | v = Point(7,8) |
---|
651 | f = Point(7,8) |
---|
652 | f.same_x_y(v) |
---|
653 | |
---|
654 | self.failUnless(f.same_x_y(v), |
---|
655 | 'same_x_y True failed') |
---|
656 | e = Point(7,9) |
---|
657 | self.failUnless(not f.same_x_y(e), |
---|
658 | 'same_x_y False failed') |
---|
659 | |
---|
660 | def test_import_tsh(self): |
---|
661 | |
---|
662 | a_att = [5.0,2.0] |
---|
663 | d_att =[4.0,2.0] |
---|
664 | f_att = [3.0,2.0] |
---|
665 | e_att = [2.0,2.0] |
---|
666 | a_xy = [0.0, 0.0] |
---|
667 | a = Vertex ( a_xy[0],a_xy[1]) #, attributes =a_att) |
---|
668 | d = Vertex (0.0, 4.0) #, attributes =d_att) |
---|
669 | f = Vertex (4.0,0.0) #, attributes =f_att) |
---|
670 | e = Vertex (1.0,1.0) #, attributes =e_att) |
---|
671 | |
---|
672 | s1 = Segment(a,d, tag = "50") |
---|
673 | s2 = Segment(d,f, tag = "40") |
---|
674 | s3 = Segment(a,f, tag = "30") |
---|
675 | s4 = Segment(a,e, tag = "20") |
---|
676 | |
---|
677 | r1 = Region(0.3, 0.3,tag = "1.3") |
---|
678 | geo = Geo_reference(8.9,8.9,65) |
---|
679 | m = Mesh(userVertices=[a,d,f,e], |
---|
680 | userSegments=[s1,s2,s3,s4], |
---|
681 | regions=[r1], |
---|
682 | geo_reference=geo) |
---|
683 | |
---|
684 | m.generateMesh("QApz", maxArea = 2.1) |
---|
685 | fileName = tempfile.mktemp(".tsh") |
---|
686 | #print "dgs!!!" |
---|
687 | #print "****************** fileName", fileName |
---|
688 | m.export_mesh_file(fileName) |
---|
689 | #print "******************" |
---|
690 | #print "m", m |
---|
691 | #print "******************" |
---|
692 | m_returned = importMeshFromFile(fileName) |
---|
693 | #print "m_returned",m_returned |
---|
694 | #print "******************" |
---|
695 | #print "****************** fileName", fileName |
---|
696 | os.remove(fileName) |
---|
697 | self.failUnless(0 == m.__cmp__(m_returned), |
---|
698 | 'loading and saving of a mesh failed') |
---|
699 | # do this when .msh supports geo_refs |
---|
700 | #self.failUnless(m.geo_reference == m_returned.geo_reference, |
---|
701 | # 'loading and saving of a mesh geo refs failed') |
---|
702 | |
---|
703 | def test_import_mesh(self): |
---|
704 | |
---|
705 | a_att = [5.0,2.0] |
---|
706 | d_att =[4.0,2.0] |
---|
707 | f_att = [3.0,2.0] |
---|
708 | e_att = [2.0,2.0] |
---|
709 | a_xy = [0.0, 0.0] |
---|
710 | a = Vertex ( a_xy[0],a_xy[1]) #, attributes =a_att) |
---|
711 | d = Vertex (0.0, 4.0) #, attributes =d_att) |
---|
712 | f = Vertex (4.0,0.0) #, attributes =f_att) |
---|
713 | e = Vertex (1.0,1.0) #, attributes =e_att) |
---|
714 | |
---|
715 | s1 = Segment(a,d, tag = "50") |
---|
716 | s2 = Segment(d,f, tag = "40") |
---|
717 | s3 = Segment(a,f, tag = "30") |
---|
718 | s4 = Segment(a,e, tag = "20") |
---|
719 | |
---|
720 | r1 = Region(0.3, 0.3,tag = "1.3") |
---|
721 | geo = Geo_reference(65,8.9,8.9) |
---|
722 | m = Mesh(userVertices=[a,d,f,e], |
---|
723 | userSegments=[s1,s2,s3,s4], |
---|
724 | regions=[r1], |
---|
725 | geo_reference=geo) |
---|
726 | |
---|
727 | m.generateMesh("QApz", maxArea = 2.1) |
---|
728 | fileName = tempfile.mktemp(".msh") |
---|
729 | #print "dgs!!!" |
---|
730 | #print "****************** fileName", fileName |
---|
731 | m.export_mesh_file(fileName) |
---|
732 | #print "******************" |
---|
733 | #print "m", m |
---|
734 | #print "******************" |
---|
735 | m_returned = importMeshFromFile(fileName) |
---|
736 | #print "m_returned",m_returned |
---|
737 | #print "******************" |
---|
738 | #print "****************** fileName", fileName |
---|
739 | os.remove(fileName) |
---|
740 | #print "m.geo_reference",m.geo_reference |
---|
741 | #print "m_returned.geo_reference,",m_returned.geo_reference |
---|
742 | self.failUnless(0 == m.__cmp__(m_returned), |
---|
743 | 'loading and saving of a mesh failed') |
---|
744 | self.failUnless(m.geo_reference == m_returned.geo_reference, |
---|
745 | 'loading and saving of a mesh geo refs failed') |
---|
746 | |
---|
747 | def test_normaliseMesh(self): |
---|
748 | |
---|
749 | a_att = [5.0,2.0] |
---|
750 | d_att =[4.0,2.0] |
---|
751 | f_att = [3.0,2.0] |
---|
752 | e_att = [2.0,2.0] |
---|
753 | a_xy = [10.0, 10.0] |
---|
754 | a = Vertex ( a_xy[0],a_xy[1], attributes =a_att) |
---|
755 | d = Vertex (15.0, 10.0, attributes =d_att) |
---|
756 | f = Vertex (10.0,20.0, attributes =f_att) |
---|
757 | e = Vertex (15.0,20.0, attributes =e_att) |
---|
758 | |
---|
759 | s1 = Segment(a,d, tag = 50) |
---|
760 | s2 = Segment(d,e, tag = 40) |
---|
761 | s3 = Segment(e,f, tag = 30) |
---|
762 | s4 = Segment(f,a, tag = 20) |
---|
763 | |
---|
764 | r1 = Region(0.3, 0.3,tag = 1.3) |
---|
765 | m = Mesh(userVertices=[a,d,f,e], |
---|
766 | userSegments=[s1,s2,s3,s4], |
---|
767 | regions=[r1]) |
---|
768 | m.normaliseMesh(1,0,1) |
---|
769 | [xmin, ymin, xmax, ymax] = m.boxsize() |
---|
770 | [attmin, attmax] = m.maxMinVertAtt(0) |
---|
771 | self.failUnless(attmin == 0.0 and attmax == 1.0, |
---|
772 | 'normalise failed') |
---|
773 | self.failUnless(xmin == 0.0 and ymin == 0.0 and xmax == 0.5 and ymax == 1.0, |
---|
774 | 'normalise failed') |
---|
775 | m.normaliseMesh(200,-100,5) |
---|
776 | [xmin, ymin, xmax, ymax] = m.boxsize() |
---|
777 | [attmin, attmax] = m.maxMinVertAtt(0) |
---|
778 | self.failUnless(attmin == 0.0 and attmax == 5.0, |
---|
779 | 'normalise failed') |
---|
780 | self.failUnless(xmin == -100.0 and ymin == -100.0 and xmax == 0.0 and ymax == 100.0, |
---|
781 | 'normalise failed') |
---|
782 | |
---|
783 | def test_exportASCIIsegmentoutlinefile(self): |
---|
784 | a = Vertex (0,0) |
---|
785 | b = Vertex (0,3) |
---|
786 | c = Vertex (3,3) |
---|
787 | d = Vertex (1,2) |
---|
788 | e = Vertex (3,1) |
---|
789 | |
---|
790 | s1 = Segment(a,b, tag = "50") |
---|
791 | s2 = Segment(b,c, tag = "40") |
---|
792 | s3 = Segment(c,a, tag = "30") |
---|
793 | |
---|
794 | r1 = Region(2, 1,tag = "1.3") |
---|
795 | h1 = Hole(1,4) |
---|
796 | m = Mesh(userVertices=[a,b,c,d,e], |
---|
797 | userSegments=[s1,s2,s3], |
---|
798 | regions=[r1], |
---|
799 | holes = [h1]) |
---|
800 | |
---|
801 | m.generateMesh("QApz", maxArea = 2.1) |
---|
802 | #print "mesh ***************dsg*", m |
---|
803 | |
---|
804 | fileName = tempfile.mktemp(".tsh") |
---|
805 | m.exportASCIIsegmentoutlinefile(fileName) |
---|
806 | |
---|
807 | m_returned = importMeshFromFile(fileName) |
---|
808 | |
---|
809 | #print "m_returned ****",m_returned |
---|
810 | #print "****************** fileName", fileName |
---|
811 | os.remove(fileName) |
---|
812 | |
---|
813 | #Trim mesh, so it should like like m_returned |
---|
814 | m.meshVertices = [] |
---|
815 | m.meshTriangles = [] |
---|
816 | m.meshSegments = [] |
---|
817 | m.userVertices=[a,b,c] |
---|
818 | #print "mesh ***************dsg*", m |
---|
819 | #print "(m.__cmp__(m_returned)", m.__cmp__(m_returned) |
---|
820 | self.failUnless(0 == m.__cmp__(m), |
---|
821 | 'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed') |
---|
822 | # Having problems with this on linux. |
---|
823 | #The ordering of the dictionary values wasn't the same as the windows |
---|
824 | #returned value (verts.values()) |
---|
825 | #self.failUnless(0 == m.__cmp__(m_returned), |
---|
826 | # 'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed') |
---|
827 | |
---|
828 | self.failUnless(3 == len(m_returned.userVertices), |
---|
829 | 'segmentoutlinefile:IO of a mesh failed') |
---|
830 | self.failUnless(len(m.userSegments) == len(m_returned.userSegments), |
---|
831 | 'segmentoutlinefile:IO of a mesh failed') |
---|
832 | for i in range(len(m.userSegments)): |
---|
833 | self.failUnless(m.userSegments[i].vertices[0].x == |
---|
834 | m_returned.userSegments[i].vertices[0].x, |
---|
835 | 'loading and saving of a mesh outline fialed') |
---|
836 | self.failUnless(m.userSegments[i].vertices[0].y == |
---|
837 | m_returned.userSegments[i].vertices[0].y, |
---|
838 | 'loading and saving of a mesh outline fialed') |
---|
839 | self.failUnless(m.userSegments[i].vertices[1].x == |
---|
840 | m_returned.userSegments[i].vertices[1].x, |
---|
841 | 'loading and saving of a mesh outline fialed') |
---|
842 | self.failUnless(m.userSegments[i].vertices[1].y == |
---|
843 | m_returned.userSegments[i].vertices[1].y, |
---|
844 | 'loading and saving of a mesh outline fialed') |
---|
845 | |
---|
846 | |
---|
847 | def test_exportASCIIsegmentoutlinefile2(self): |
---|
848 | a = Vertex (0,0) |
---|
849 | b = Vertex (0,1) |
---|
850 | c = Vertex (1,0) |
---|
851 | d = Vertex (1,1) |
---|
852 | e = Vertex (0.5,0.5) |
---|
853 | f = Vertex (0.6,0.6) |
---|
854 | |
---|
855 | s1 = Segment(a,e, tag = "50") |
---|
856 | s2 = Segment(b,e, tag = "40") |
---|
857 | s3 = Segment(c,e, tag = "30") |
---|
858 | s4 = Segment(d,e, tag = "30") |
---|
859 | |
---|
860 | r1 = Region(2, 1,tag = "1.3") |
---|
861 | h1 = Hole(1,4) |
---|
862 | m = Mesh(userVertices=[a,b,c,d,e], |
---|
863 | userSegments=[s1,s2,s3,s4], |
---|
864 | regions=[r1], |
---|
865 | holes = [h1]) |
---|
866 | |
---|
867 | fileName = tempfile.mktemp(".tsh") |
---|
868 | m.exportASCIIsegmentoutlinefile(fileName) |
---|
869 | |
---|
870 | m_returned = importMeshFromFile(fileName) |
---|
871 | #print "****************** fileName", fileName |
---|
872 | os.remove(fileName) |
---|
873 | |
---|
874 | #Trim mesh, so it should like like m_returned |
---|
875 | m.meshVertices = [] |
---|
876 | m.meshTriangles = [] |
---|
877 | m.meshSegments = [] |
---|
878 | m.userVertices=[a,e,d,b,c] |
---|
879 | #print "mesh ***************dsg*", m |
---|
880 | #print "(m.__cmp__(m_returned)", m.__cmp__(m_returned) |
---|
881 | self.failUnless(0 == m.__cmp__(m), |
---|
882 | 'loading and saving of a mesh failed') |
---|
883 | |
---|
884 | self.failUnless(5 == len(m_returned.userVertices), |
---|
885 | 'segmentoutlinefile:IO of a mesh failed') |
---|
886 | self.failUnless(len(m.userSegments) == len(m_returned.userSegments), |
---|
887 | 'segmentoutlinefile:IO of a mesh failed') |
---|
888 | for i in range(len(m.userSegments)): |
---|
889 | self.failUnless(m.userSegments[i].vertices[0].x == |
---|
890 | m_returned.userSegments[i].vertices[0].x, |
---|
891 | 'loading and saving of a mesh outline fialed') |
---|
892 | self.failUnless(m.userSegments[i].vertices[0].y == |
---|
893 | m_returned.userSegments[i].vertices[0].y, |
---|
894 | 'loading and saving of a mesh outline fialed') |
---|
895 | self.failUnless(m.userSegments[i].vertices[1].x == |
---|
896 | m_returned.userSegments[i].vertices[1].x, |
---|
897 | 'loading and saving of a mesh outline fialed') |
---|
898 | self.failUnless(m.userSegments[i].vertices[1].y == |
---|
899 | m_returned.userSegments[i].vertices[1].y, |
---|
900 | 'loading and saving of a mesh outline fialed') |
---|
901 | |
---|
902 | |
---|
903 | def test_loadxy(self): |
---|
904 | """ |
---|
905 | To test the mesh side of loading xya files. |
---|
906 | Not the loading of xya files |
---|
907 | """ |
---|
908 | import os |
---|
909 | import tempfile |
---|
910 | |
---|
911 | fileName = tempfile.mktemp(".xya") |
---|
912 | file = open(fileName,"w") |
---|
913 | file.write("elevation speed \n\ |
---|
914 | 1.0 0.0 10.0 0.0\n\ |
---|
915 | 0.0 1.0 0.0 10.0\n\ |
---|
916 | 1.0 0.0 10.4 40.0\n") |
---|
917 | file.close() |
---|
918 | #print fileName |
---|
919 | m = importMeshFromFile(fileName) |
---|
920 | os.remove(fileName) |
---|
921 | self.failUnless(m.userVertices[0].x == 1.0, |
---|
922 | 'loadxy, test 1 failed') |
---|
923 | self.failUnless(m.userVertices[0].y == 0.0, |
---|
924 | 'loadxy, test 2 failed') |
---|
925 | #self.failUnless(m.userVertices[0].attributes == [10.0,0.0], |
---|
926 | # 'loadxy, test 2.2 failed') |
---|
927 | self.failUnless(m.userVertices[1].x == 0.0, |
---|
928 | 'loadxy, test 3 failed') |
---|
929 | self.failUnless(m.userVertices[1].y == 1.0, |
---|
930 | 'loadxy, test 4 failed') |
---|
931 | #self.failUnless(m.userVertices[1].attributes == [0.0,10.0], |
---|
932 | # 'loadxy, test 5 failed') |
---|
933 | |
---|
934 | def test_exportPointsFile(self): |
---|
935 | a = Vertex (0,0) |
---|
936 | b = Vertex (0,3) |
---|
937 | c = Vertex (3,3) |
---|
938 | d = Vertex (1,2) |
---|
939 | e = Vertex (3,1) |
---|
940 | f = Vertex (3,1) |
---|
941 | |
---|
942 | s1 = Segment(a,b, tag = 50) |
---|
943 | s2 = Segment(b,c, tag = 40) |
---|
944 | s3 = Segment(c,a, tag = 30) |
---|
945 | |
---|
946 | r1 = Region(2, 1,tag = 1.3) |
---|
947 | h1 = Hole(1,4) |
---|
948 | # Warning mesh can't produce this type of data structure its self |
---|
949 | m = Mesh(userVertices=[a,b,c,d,e], |
---|
950 | userSegments=[s1,s2,s3], |
---|
951 | regions=[r1], |
---|
952 | holes = [h1]) |
---|
953 | |
---|
954 | fileName = tempfile.mktemp(".xya") |
---|
955 | #fileName = 't.xya' |
---|
956 | #os.remove(fileName) |
---|
957 | m.exportPointsFile(fileName) |
---|
958 | file = open(fileName) |
---|
959 | lFile = file.read().split('\n') |
---|
960 | file.close() |
---|
961 | |
---|
962 | os.remove(fileName) |
---|
963 | self.failUnless(lFile[0] == "" and |
---|
964 | lFile[1] == "0,0" and |
---|
965 | lFile[2] == "0,3" and |
---|
966 | lFile[3] == "3,3" |
---|
967 | , |
---|
968 | 'exported Ascii xya file is wrong') |
---|
969 | self.failUnless(lFile[4] == "1,2" and |
---|
970 | lFile[5] == "3,1" |
---|
971 | , |
---|
972 | 'exported Ascii xya file is wrong') |
---|
973 | |
---|
974 | m.generateMesh("QApz", maxArea = 2.1) |
---|
975 | fileName = tempfile.mktemp(".xya") |
---|
976 | fileName = 't.xya' |
---|
977 | m.export_mesh_file('m.tsh') |
---|
978 | m.exportPointsFile(fileName) |
---|
979 | file = open(fileName) |
---|
980 | lFile = file.read().split('\n') |
---|
981 | file.close() |
---|
982 | #os.remove(fileName) |
---|
983 | |
---|
984 | self.failUnless(lFile[0] == "" and |
---|
985 | lFile[1] == "0.0,0.0" and |
---|
986 | lFile[2] == "0.0,3.0" and |
---|
987 | lFile[3] == "3.0,3.0" and |
---|
988 | lFile[4] == "1.0,2.0" and |
---|
989 | lFile[5] == "3.0,1.0" |
---|
990 | , |
---|
991 | 'exported Ascii xya file is wrong') |
---|
992 | |
---|
993 | def test_exportPointsFilefile2(self): |
---|
994 | m = Mesh() |
---|
995 | |
---|
996 | fileName = tempfile.mktemp(".xya") |
---|
997 | m.exportPointsFile(fileName) |
---|
998 | file = open(fileName) |
---|
999 | lFile = file.read().split('\n') |
---|
1000 | file.close() |
---|
1001 | |
---|
1002 | os.remove(fileName) |
---|
1003 | #print "************* test_mesh exportPointsFilefile" |
---|
1004 | #print "lFile",lFile |
---|
1005 | #print "************* test_mesh exportPointsFilefile" |
---|
1006 | self.failUnless(lFile[0] == "" |
---|
1007 | , |
---|
1008 | 'exported Ascii xya file is wrong') |
---|
1009 | |
---|
1010 | def test_strings2ints(self): |
---|
1011 | list = ["sea","river inlet","","sea","","moat"] |
---|
1012 | preset = ["moat", "internal boundary"] |
---|
1013 | [intlist, converter] = segment_strings2ints(list,preset ) |
---|
1014 | self.failUnless(intlist == [2,3 ,0 ,2 ,0 ,0 ] |
---|
1015 | , |
---|
1016 | 'test_strings2ints produces bad intlist') |
---|
1017 | self.failUnless(converter == ['moat', 'internal boundary', |
---|
1018 | 'sea', 'river inlet'] |
---|
1019 | , |
---|
1020 | 'test_strings2ints produces bad converter') |
---|
1021 | |
---|
1022 | def test_ints2strings(self): |
---|
1023 | list = ["internal boundary","sea","river inlet", |
---|
1024 | "","sea","","moat","internal boundary"] |
---|
1025 | outlist = ['internal boundary', 'sea', 'river inlet', 'moat', |
---|
1026 | 'sea', 'moat', 'moat', 'internal boundary'] |
---|
1027 | preset = ["moat", "internal boundary"] |
---|
1028 | [intlist, converter] = segment_strings2ints(list,preset ) |
---|
1029 | newlist = segment_ints2strings(intlist, converter) |
---|
1030 | self.failUnless(outlist == newlist |
---|
1031 | , |
---|
1032 | 'test_strings2ints produces bad intlist') |
---|
1033 | self.failUnless(converter == ['moat', 'internal boundary', |
---|
1034 | 'sea', 'river inlet'] |
---|
1035 | , |
---|
1036 | 'test_strings2ints produces bad converter') |
---|
1037 | |
---|
1038 | def test_ints2strings2(self): |
---|
1039 | list = ["","",""] |
---|
1040 | preset = ["moat", "internal boundary"] |
---|
1041 | [intlist, converter] = segment_strings2ints(list,preset ) |
---|
1042 | newlist = segment_ints2strings(intlist, converter) |
---|
1043 | outlist = ['moat', 'moat', 'moat'] |
---|
1044 | self.failUnless(outlist == newlist |
---|
1045 | , |
---|
1046 | 'test_strings2ints produces bad intlist') |
---|
1047 | self.failUnless(converter == ['moat', 'internal boundary'] |
---|
1048 | , |
---|
1049 | 'test_strings2ints produces bad converter') |
---|
1050 | |
---|
1051 | |
---|
1052 | def test_removeDuplicatedVertices(self): |
---|
1053 | a = Vertex (0,0) |
---|
1054 | a.index = 0 |
---|
1055 | b = Vertex (0,3) |
---|
1056 | b.index = 1 |
---|
1057 | c = Vertex (3,3) |
---|
1058 | c.index = 2 |
---|
1059 | d = Vertex (1,1) |
---|
1060 | d.index = 3 |
---|
1061 | e = Vertex (3,1) |
---|
1062 | e.index = 4 |
---|
1063 | f = Vertex (1,1) |
---|
1064 | f.index = 5 |
---|
1065 | g = Vertex (1,1) |
---|
1066 | g.index = 6 |
---|
1067 | inputVerts_noDups = [a,b,c,d,e] |
---|
1068 | |
---|
1069 | m = Mesh(userVertices=[a,b,c,d,e,f,g]) |
---|
1070 | counter = m.removeDuplicatedUserVertices() |
---|
1071 | UserVerts = m.getUserVertices() |
---|
1072 | |
---|
1073 | |
---|
1074 | self.failUnless(UserVerts == inputVerts_noDups, |
---|
1075 | 'duplicate verts not removed') |
---|
1076 | #for userVert, inputVert in map(None, UserVerts, inputVerts_noDups): |
---|
1077 | # self.failUnless(userVert.x == inputVert.x, |
---|
1078 | # 'x duplicate verts not removed') |
---|
1079 | # self.failUnless(userVert.y == inputVert.y, |
---|
1080 | # 'y duplicate verts not removed') |
---|
1081 | |
---|
1082 | |
---|
1083 | def test_ungenerateFileLoading(self): |
---|
1084 | |
---|
1085 | fileName = tempfile.mktemp(".txt") |
---|
1086 | file = open(fileName,"w") |
---|
1087 | file.write(" 1 ?? ??\n\ |
---|
1088 | 0.0 0.0\n\ |
---|
1089 | 1.0 0.0\n\ |
---|
1090 | 1.0 1.0\n\ |
---|
1091 | 0.0 1.0\n\ |
---|
1092 | 0.0 0.0\n\ |
---|
1093 | END\n\ |
---|
1094 | 2 ?? ??\n\ |
---|
1095 | 10.0 10.0\n\ |
---|
1096 | 10.0 20.0\n\ |
---|
1097 | 20.0 20.0\n\ |
---|
1098 | 10.0 10.0\n\ |
---|
1099 | END\n\ |
---|
1100 | END\n") |
---|
1101 | file.close() |
---|
1102 | |
---|
1103 | |
---|
1104 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
1105 | b = Vertex (0.0, 40.0) #, attributes = [1.2]) |
---|
1106 | c = Vertex (40.0,40.0) #, attributes = [1.3]) |
---|
1107 | d = Vertex (40.0,0.0) #, attributes = [1.4]) |
---|
1108 | |
---|
1109 | s1 = Segment(a,b) |
---|
1110 | s2 = Segment(b,c) |
---|
1111 | s3 = Segment(c,d) |
---|
1112 | s4 = Segment(d,a) |
---|
1113 | |
---|
1114 | m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4]) |
---|
1115 | dict = importUngenerateFile(fileName) |
---|
1116 | os.remove(fileName) |
---|
1117 | |
---|
1118 | tag = "DSG" |
---|
1119 | Segment.set_default_tag(tag) |
---|
1120 | m.addVertsSegs(dict) |
---|
1121 | |
---|
1122 | # have to reset this , since it's a class attribute |
---|
1123 | Segment.set_default_tag("") |
---|
1124 | |
---|
1125 | self.failUnless(len(m.userSegments) ==11, |
---|
1126 | 'Wrong segment list length.') |
---|
1127 | self.failUnless(len(m.userVertices) == 11, |
---|
1128 | 'Wrong vertex list length.') |
---|
1129 | self.failUnless(m.userSegments[10].vertices[0] == m.userVertices[10], |
---|
1130 | 'bad vertex on segment.') |
---|
1131 | self.failUnless(m.userSegments[10].vertices[1] == m.userVertices[8], |
---|
1132 | 'Bad segment.') |
---|
1133 | self.failUnless(m.userSegments[10].tag == tag, |
---|
1134 | 'wrong tag.') |
---|
1135 | |
---|
1136 | def test_ungenerateFileLoadingII(self): |
---|
1137 | |
---|
1138 | fileName = tempfile.mktemp(".txt") |
---|
1139 | file = open(fileName,"w") |
---|
1140 | file.write(" 1 ?? ??\n\ |
---|
1141 | 0.0 0.0\n\ |
---|
1142 | 1.0 0.0\n\ |
---|
1143 | 1.0 1.0\n\ |
---|
1144 | 0.0 1.0\n\ |
---|
1145 | 0.0 0.0\n\ |
---|
1146 | END\n\ |
---|
1147 | 2 ?? ??\n\ |
---|
1148 | 10.0 10.0\n\ |
---|
1149 | 10.0 20.0\n\ |
---|
1150 | 20.0 20.0\n\ |
---|
1151 | END\n\ |
---|
1152 | END\n") |
---|
1153 | file.close() |
---|
1154 | |
---|
1155 | |
---|
1156 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
1157 | b = Vertex (0.0, 40.0) #, attributes = [1.2]) |
---|
1158 | c = Vertex (40.0,40.0) #, attributes = [1.3]) |
---|
1159 | d = Vertex (40.0,0.0) #, attributes = [1.4]) |
---|
1160 | |
---|
1161 | s1 = Segment(a,b) |
---|
1162 | s2 = Segment(b,c) |
---|
1163 | s3 = Segment(c,d) |
---|
1164 | s4 = Segment(d,a) |
---|
1165 | |
---|
1166 | m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4]) |
---|
1167 | dict = importUngenerateFile(fileName) |
---|
1168 | os.remove(fileName) |
---|
1169 | |
---|
1170 | tag = "DSG" |
---|
1171 | Segment.set_default_tag(tag) |
---|
1172 | m.addVertsSegs(dict) |
---|
1173 | |
---|
1174 | self.failUnless(len(m.userSegments) ==10, |
---|
1175 | 'Wrong segment list length.') |
---|
1176 | self.failUnless(len(m.userVertices) == 11, |
---|
1177 | 'Wrong vertex list length.') |
---|
1178 | |
---|
1179 | def test_addVertsSegs(self): |
---|
1180 | m = Mesh() |
---|
1181 | Segment.set_default_tag("food") |
---|
1182 | dict = {} |
---|
1183 | dict['points'] = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]] |
---|
1184 | dict['segments'] = [[0, 1], [1, 2]] |
---|
1185 | dict['segment_tags'] = ['','do-op'] |
---|
1186 | m.addVertsSegs(dict) |
---|
1187 | # have to reset this , since it's a class attribute |
---|
1188 | Segment.set_default_tag("") |
---|
1189 | |
---|
1190 | |
---|
1191 | self.failUnless(len(m.userSegments) ==2, |
---|
1192 | 'Wrong segment list length.') |
---|
1193 | self.failUnless(len(m.userVertices) == 3, |
---|
1194 | 'Wrong vertex list length.') |
---|
1195 | self.failUnless(m.userSegments[0].tag =='food', |
---|
1196 | 'Wrong segment tag length.') |
---|
1197 | self.failUnless(m.userSegments[1].tag =='do-op', |
---|
1198 | 'Wrong segment tag.') |
---|
1199 | |
---|
1200 | def test_exportASCIImeshfile(self): |
---|
1201 | |
---|
1202 | #a_att = [5,2] |
---|
1203 | #d_att =[4,2] |
---|
1204 | #f_att = [3,2] |
---|
1205 | #e_att = [2,2] |
---|
1206 | a_xy = [0.0, 0.0] |
---|
1207 | a = Vertex ( a_xy[0],a_xy[1]) #, attributes =a_att) |
---|
1208 | d = Vertex (0.0, 4.0) #, attributes =d_att) |
---|
1209 | f = Vertex (4.0,0.0) #, attributes =f_att) |
---|
1210 | e = Vertex (1.0,1.0) #, attributes =e_att) |
---|
1211 | |
---|
1212 | s1 = Segment(a,d, tag = "50") |
---|
1213 | s2 = Segment(d,f, tag = "40") |
---|
1214 | s3 = Segment(a,f, tag = "30") |
---|
1215 | s4 = Segment(a,e, tag = "20") |
---|
1216 | |
---|
1217 | r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 36) |
---|
1218 | |
---|
1219 | |
---|
1220 | h1 = Hole(0.2,0.6) |
---|
1221 | |
---|
1222 | m = Mesh(userVertices=[a,d,f,e], |
---|
1223 | userSegments=[s1,s2,s3,s4], |
---|
1224 | regions=[r1], |
---|
1225 | holes=[h1]) |
---|
1226 | |
---|
1227 | seg = m.getUserSegments() |
---|
1228 | points = m.getUserVertices() |
---|
1229 | holes = m.getHoles() |
---|
1230 | regions = m.getRegions() |
---|
1231 | fileName = tempfile.mktemp(".tsh") |
---|
1232 | m.export_mesh_file(fileName) |
---|
1233 | #print "***************************fileName", fileName |
---|
1234 | new_m = importMeshFromFile(fileName) |
---|
1235 | os.remove(fileName) |
---|
1236 | |
---|
1237 | |
---|
1238 | #print '**@@@@@******' |
---|
1239 | #print "new_m",new_m |
---|
1240 | #print '**@@@@@******' |
---|
1241 | #print "m",m |
---|
1242 | #print '**@@@@@******' |
---|
1243 | |
---|
1244 | self.failUnless( new_m == m, |
---|
1245 | 'loadASCIITestCase failed. test new 1') |
---|
1246 | |
---|
1247 | def test_Mesh2MeshList(self): |
---|
1248 | |
---|
1249 | a_att = [5,2] |
---|
1250 | d_att =[4,2] |
---|
1251 | f_att = [3,2] |
---|
1252 | e_att = [2,2] |
---|
1253 | a_xy = [0.0, 0.0] |
---|
1254 | a = Vertex ( a_xy[0],a_xy[1]) #, attributes =a_att) |
---|
1255 | d = Vertex (0.0, 4.0) #, attributes =d_att) |
---|
1256 | f = Vertex (4.0,0.0) #, attributes =f_att) |
---|
1257 | e = Vertex (1.0,1.0) #, attributes =e_att) |
---|
1258 | |
---|
1259 | s1 = Segment(a,d, tag = "50") |
---|
1260 | s2 = Segment(d,f, tag = "40") |
---|
1261 | s3 = Segment(a,f, tag = "30") |
---|
1262 | s4 = Segment(a,e, tag = "20") |
---|
1263 | |
---|
1264 | r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 45) |
---|
1265 | m = Mesh(userVertices=[a,d,f,e], |
---|
1266 | userSegments=[s1,s2,s3,s4], |
---|
1267 | regions=[r1]) |
---|
1268 | |
---|
1269 | m.generateMesh("QApza2.1") |
---|
1270 | |
---|
1271 | seg = m.getMeshSegments() |
---|
1272 | points = m.getMeshVertices() |
---|
1273 | dict = m.Mesh2MeshList() |
---|
1274 | #print "dict",dict |
---|
1275 | # test not finished... |
---|
1276 | |
---|
1277 | def test_Mesh2IOTriangulationDict(self): |
---|
1278 | |
---|
1279 | a_att = [5,2] |
---|
1280 | d_att =[4,2] |
---|
1281 | f_att = [3,2] |
---|
1282 | e_att = [2,2] |
---|
1283 | a_xy = [0.0, 0.0] |
---|
1284 | a = Vertex ( a_xy[0],a_xy[1] , attributes =a_att) |
---|
1285 | d = Vertex (0.0, 4.0 , attributes =d_att) |
---|
1286 | f = Vertex (4.0,0.0 , attributes =f_att) |
---|
1287 | e = Vertex (1.0,1.0 , attributes =e_att) |
---|
1288 | |
---|
1289 | s1 = Segment(a,d, tag = "50") |
---|
1290 | s2 = Segment(d,f, tag = "40") |
---|
1291 | s3 = Segment(a,f, tag = "30") |
---|
1292 | s4 = Segment(a,e, tag = "20") |
---|
1293 | |
---|
1294 | r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 45) |
---|
1295 | m = Mesh(userVertices=[a,d,f,e], |
---|
1296 | userSegments=[s1,s2,s3,s4], |
---|
1297 | regions=[r1]) |
---|
1298 | titles = ['ele','friction'] |
---|
1299 | m.attributeTitles = titles |
---|
1300 | m.generateMesh("QApza2.1") |
---|
1301 | |
---|
1302 | seg = m.getMeshSegments() |
---|
1303 | verts = m.getMeshVertices() |
---|
1304 | dict = m.Mesh2IOTriangulationDict() |
---|
1305 | #print "dict",dict |
---|
1306 | |
---|
1307 | self.failUnless( dict['vertex_attribute_titles'] == titles, |
---|
1308 | 'test_Mesh2IOTriangulationDict failed. test 1') |
---|
1309 | answer = [a_xy,[0.0, 4.0],[4.0,0.0], [1.0,1.0], [2.0,2.0]] |
---|
1310 | #print "answer",answer |
---|
1311 | #print "dict['vertices']",dict['vertices'] |
---|
1312 | |
---|
1313 | self.failUnless( dict['vertices'] == answer, |
---|
1314 | 'test_Mesh2IOTriangulationDict failed. test 2') |
---|
1315 | |
---|
1316 | |
---|
1317 | for pimport,pactual,pimpatt in map(None,dict['vertices'], |
---|
1318 | verts,dict['vertex_attributes']): |
---|
1319 | #assert all_close( pimport, (pactual.x,pactual.y)) |
---|
1320 | self.failUnless( pimport == [pactual.x,pactual.y], |
---|
1321 | 'test_Mesh2IOTriangulationDict failed. test 2.1') |
---|
1322 | self.failUnless( pimpatt == pactual.attributes, |
---|
1323 | 'test_Mesh2IOTriangulationDict failed. test 2.2') |
---|
1324 | self.failUnless( dict['segments'][0] == [0,1], |
---|
1325 | 'test_Mesh2IOTriangulationDict failed. test 3') |
---|
1326 | for segimp,segactual in map(None,dict['segment_tags'],seg): |
---|
1327 | self.failUnless( segimp == segactual.tag, |
---|
1328 | 'test_Mesh2IOTriangulationDict failed. test 4') |
---|
1329 | #print " dict['triangles'][0]", dict['triangles'][0] |
---|
1330 | self.failUnless( dict['triangles'][0] == [3,2,4], |
---|
1331 | 'test_Mesh2IOTriangulationDict failed. test 5') |
---|
1332 | self.failUnless( dict['triangle_neighbors'][0] == [-1,2,3], |
---|
1333 | 'test_Mesh2IOTriangulationDict failed. test 6') |
---|
1334 | self.failUnless( dict['triangle_tags'][0] == "1.3", |
---|
1335 | 'test_Mesh2IOTriangulationDict failed. test 7') |
---|
1336 | |
---|
1337 | |
---|
1338 | def test_Mesh2IODict(self): |
---|
1339 | |
---|
1340 | a_att = [5,2] |
---|
1341 | d_att =[4,2] |
---|
1342 | f_att = [3,2] |
---|
1343 | e_att = [2,2] |
---|
1344 | a_xy = [0.0, 0.0] |
---|
1345 | a = Vertex ( a_xy[0],a_xy[1] , attributes =a_att) |
---|
1346 | d = Vertex (0.0, 4.0 , attributes =d_att) |
---|
1347 | f = Vertex (4.0,0.0 , attributes =f_att) |
---|
1348 | e = Vertex (1.0,1.0 , attributes =e_att) |
---|
1349 | |
---|
1350 | s1 = Segment(a,d, tag = "50") |
---|
1351 | s2 = Segment(d,f, tag = "40") |
---|
1352 | s3 = Segment(a,f, tag = "30") |
---|
1353 | s4 = Segment(a,e, tag = "20") |
---|
1354 | |
---|
1355 | r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 45) |
---|
1356 | m = Mesh(userVertices=[a,d,f,e], |
---|
1357 | userSegments=[s1,s2,s3,s4], |
---|
1358 | regions=[r1]) |
---|
1359 | titles = ['ele','friction'] |
---|
1360 | m.attributeTitles = titles |
---|
1361 | m.generateMesh("QApza2.1") |
---|
1362 | |
---|
1363 | seg = m.getMeshSegments() |
---|
1364 | verts = m.getMeshVertices() |
---|
1365 | dict = m.Mesh2IODict() |
---|
1366 | #print "dict",dict |
---|
1367 | |
---|
1368 | self.failUnless( dict['vertex_attribute_titles'] == titles, |
---|
1369 | 'test_Mesh2IOTriangulationDict failed. test 1') |
---|
1370 | answer = [a_xy,[0.0, 4.0],[4.0,0.0], [1.0,1.0], [2.0,2.0]] |
---|
1371 | #print "answer",answer |
---|
1372 | #print "dict['vertices']",dict['vertices'] |
---|
1373 | |
---|
1374 | self.failUnless( dict['vertices'] == answer, |
---|
1375 | 'test_Mesh2IOTriangulationDict failed. test 2') |
---|
1376 | |
---|
1377 | |
---|
1378 | for pimport,pactual,pimpatt in map(None,dict['vertices'], |
---|
1379 | verts,dict['vertex_attributes']): |
---|
1380 | #assert all_close( pimport, (pactual.x,pactual.y)) |
---|
1381 | self.failUnless( pimport == [pactual.x,pactual.y], |
---|
1382 | 'test_Mesh2IODict failed. test 2.1') |
---|
1383 | self.failUnless( pimpatt == pactual.attributes, |
---|
1384 | 'test_Mesh2IODict failed. test 2.2') |
---|
1385 | self.failUnless( dict['segments'][0] == [0,1], |
---|
1386 | 'test_Mesh2IODict failed. test 3') |
---|
1387 | for segimp,segactual in map(None,dict['segment_tags'],seg): |
---|
1388 | self.failUnless( segimp == segactual.tag, |
---|
1389 | 'test_Mesh2IODict failed. test 4') |
---|
1390 | #print " dict['triangles'][0]", dict['triangles'][0] |
---|
1391 | self.failUnless( dict['triangles'][0] == [3,2,4], |
---|
1392 | 'test_Mesh2IODict failed. test 5') |
---|
1393 | self.failUnless( dict['triangle_neighbors'][0] == [-1,2,3], |
---|
1394 | 'test_Mesh2IODict failed. test 6') |
---|
1395 | self.failUnless( dict['triangle_tags'][0] == "1.3", |
---|
1396 | 'test_Mesh2IODict failed. test 7') |
---|
1397 | |
---|
1398 | seg = m.getUserSegments() |
---|
1399 | points = m.getUserVertices() |
---|
1400 | holes = m.getHoles() |
---|
1401 | regions = m.getRegions() |
---|
1402 | |
---|
1403 | for pimport,pactual,pimpatt in map(None,dict['points'],points,dict['point_attributes']): |
---|
1404 | self.failUnless( pimport == [pactual.x,pactual.y], |
---|
1405 | 'test_Mesh2IODict failed. test 1') |
---|
1406 | self.failUnless( pimpatt == pactual.attributes, |
---|
1407 | 'test_Mesh2IODict failed. test 1.1') |
---|
1408 | self.failUnless( dict['outline_segments'][0] == [0,1], |
---|
1409 | 'test_Mesh2IODict failed. test 3') |
---|
1410 | for segimp,segactual in map(None,dict['outline_segment_tags'],seg): |
---|
1411 | self.failUnless( segimp == segactual.tag, |
---|
1412 | 'test_Mesh2IODict failed. test 4') |
---|
1413 | for holeimp,holeactual in map(None,dict['holes'],holes): |
---|
1414 | self.failUnless( holeimp == [holeactual.x,holeactual.y], |
---|
1415 | 'test_Mesh2IODict failed. test 5') |
---|
1416 | |
---|
1417 | for regimp,regactual,regattimp, regmaxarea in map(None,dict['regions'],regions, dict['region_tags'], dict['region_max_areas']): |
---|
1418 | self.failUnless( regimp == [regactual.x,regactual.y], |
---|
1419 | 'loadASCIITestCase failed. test 6') |
---|
1420 | self.failUnless( regattimp == regactual.getTag(), |
---|
1421 | 'loadASCIITestCase failed. test 7') |
---|
1422 | self.failUnless( regmaxarea == regactual.getMaxArea(), |
---|
1423 | 'loadASCIITestCase failed. test 7') |
---|
1424 | |
---|
1425 | |
---|
1426 | |
---|
1427 | def test_Mesh2IOOutlineDict(self): |
---|
1428 | |
---|
1429 | a_att = [5,2] |
---|
1430 | d_att =[4,2] |
---|
1431 | f_att = [3,2] |
---|
1432 | e_att = [2,2] |
---|
1433 | a_xy = [0.0, 0.0] |
---|
1434 | a = Vertex ( a_xy[0],a_xy[1] , attributes =a_att) |
---|
1435 | d = Vertex (0.0, 4.0 , attributes =d_att) |
---|
1436 | f = Vertex (4.0,0.0 , attributes =f_att) |
---|
1437 | e = Vertex (1.0,1.0 , attributes =e_att) |
---|
1438 | |
---|
1439 | s1 = Segment(a,d, tag = "50") |
---|
1440 | s2 = Segment(d,f, tag = "40") |
---|
1441 | s3 = Segment(a,f, tag = "30") |
---|
1442 | s4 = Segment(a,e, tag = "20") |
---|
1443 | |
---|
1444 | r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 45) |
---|
1445 | m = Mesh(userVertices=[a,d,f,e], |
---|
1446 | userSegments=[s1,s2,s3,s4], |
---|
1447 | regions=[r1]) |
---|
1448 | titles = ['ele','friction'] |
---|
1449 | m.attributeTitles = titles |
---|
1450 | m.generateMesh("QApza2.1") |
---|
1451 | |
---|
1452 | seg = m.getMeshSegments() |
---|
1453 | verts = m.getMeshVertices() |
---|
1454 | dict = m.Mesh2IOOutlineDict() |
---|
1455 | |
---|
1456 | seg = m.getUserSegments() |
---|
1457 | points = m.getUserVertices() |
---|
1458 | holes = m.getHoles() |
---|
1459 | regions = m.getRegions() |
---|
1460 | |
---|
1461 | for pimport,pactual,pimpatt in map(None,dict['points'],points,dict['point_attributes']): |
---|
1462 | self.failUnless( pimport == [pactual.x,pactual.y], |
---|
1463 | 'loadASCIITestCase failed. test 1') |
---|
1464 | self.failUnless( pimpatt == pactual.attributes, |
---|
1465 | 'loadASCIITestCase failed. test 1.1') |
---|
1466 | self.failUnless( dict['outline_segments'][0] == [0,1], |
---|
1467 | 'loadASCIITestCase failed. test 3') |
---|
1468 | for segimp,segactual in map(None,dict['outline_segment_tags'],seg): |
---|
1469 | self.failUnless( segimp == segactual.tag, |
---|
1470 | 'loadASCIITestCase failed. test 4') |
---|
1471 | for holeimp,holeactual in map(None,dict['holes'],holes): |
---|
1472 | self.failUnless( holeimp == [holeactual.x,holeactual.y], |
---|
1473 | 'loadASCIITestCase failed. test 5') |
---|
1474 | #for regimp,regactual in map(None,dict['regions'],regions): |
---|
1475 | # self.failUnless( [regimp[0],regimp[1]]==[regactual.x,regactual.y], |
---|
1476 | # 'loadASCIITestCase failed. test 6') |
---|
1477 | # self.failUnless( regimp[2] == regactual.getTag(), |
---|
1478 | # 'loadASCIITestCase failed. test 7') |
---|
1479 | #self.failUnless( regimp[3] == regactual.getMaxArea(), |
---|
1480 | # 'loadASCIITestCase failed. test 7') |
---|
1481 | |
---|
1482 | |
---|
1483 | for regimp,regactual,regattimp, regmaxarea in map(None,dict['regions'],regions, dict['region_tags'], dict['region_max_areas']): |
---|
1484 | self.failUnless( regimp == [regactual.x,regactual.y], |
---|
1485 | 'loadASCIITestCase failed. test 6') |
---|
1486 | self.failUnless( regattimp == regactual.getTag(), |
---|
1487 | 'loadASCIITestCase failed. test 7') |
---|
1488 | self.failUnless( regmaxarea == regactual.getMaxArea(), |
---|
1489 | 'loadASCIITestCase failed. test 7') |
---|
1490 | |
---|
1491 | |
---|
1492 | #___________begining of Peters tests |
---|
1493 | |
---|
1494 | def test_set_stuff(self): |
---|
1495 | """ |
---|
1496 | Documentation |
---|
1497 | """ |
---|
1498 | #making a test mesh |
---|
1499 | p0=[0.,2.] |
---|
1500 | p1=[1.,2.] |
---|
1501 | p2=[0.,1.] |
---|
1502 | p3=[1.,1.] |
---|
1503 | p4=[0.,0.] |
---|
1504 | p5=[2.,0.] |
---|
1505 | p6=[-1.,1.] |
---|
1506 | point_list = [p0,p1,p2,p3,p4,p5,p6] |
---|
1507 | |
---|
1508 | a0=[0] |
---|
1509 | a1=[0] |
---|
1510 | a2=[100] |
---|
1511 | a3=[0] |
---|
1512 | a4=[0] |
---|
1513 | a5=[0] |
---|
1514 | a6=[0] |
---|
1515 | attribute=[a0,a1,a2,a3,a4,a5,a6] |
---|
1516 | |
---|
1517 | t0=[0,3,1] |
---|
1518 | t1=[0,2,3] |
---|
1519 | t2=[2,4,3] |
---|
1520 | t3=[4,5,3] |
---|
1521 | t4=[1,3,5] |
---|
1522 | t5=[2,6,4] |
---|
1523 | |
---|
1524 | n0=[4,-1,2] |
---|
1525 | n1=[2,0,-1] |
---|
1526 | n2=[3,1,5] |
---|
1527 | n3=[4,2,-1] |
---|
1528 | n4=[3,-1,0] |
---|
1529 | n5=[-1,2,-1] |
---|
1530 | |
---|
1531 | tri_list = [t0,t1,t2,t3,t4,t5] |
---|
1532 | n_list = [n0,n1,n2,n3,n4,n5] |
---|
1533 | for i in range(6): |
---|
1534 | for j in (0,1,2): |
---|
1535 | a=attribute[tri_list[i][j]] |
---|
1536 | tri_list[i][j]=point_list[tri_list[i][j]] |
---|
1537 | tri_list[i][j]=Vertex(tri_list[i][j][0]\ |
---|
1538 | ,tri_list[i][j][1],a) |
---|
1539 | neighbours=n_list[i] |
---|
1540 | tri_list[i]=Triangle(tri_list[i][0],\ |
---|
1541 | tri_list[i][1],tri_list[i][2]\ |
---|
1542 | ,neighbors=neighbours) |
---|
1543 | |
---|
1544 | #testing selectAll |
---|
1545 | mesh = Mesh() |
---|
1546 | mesh.attributeTitles=['attrib'] |
---|
1547 | |
---|
1548 | mesh.meshTriangles=tri_list |
---|
1549 | |
---|
1550 | mesh.selectAllTriangles() |
---|
1551 | A=mesh.sets[mesh.setID['All']] |
---|
1552 | assert list_comp(tri_list,A) |
---|
1553 | |
---|
1554 | #testing threshold |
---|
1555 | mesh = Mesh() |
---|
1556 | mesh.attributeTitles=['attrib'] |
---|
1557 | |
---|
1558 | mesh.meshTriangles=tri_list |
---|
1559 | mesh.selectAllTriangles() |
---|
1560 | mesh.threshold('All',min=30,max=35,attribute_name = 'attrib') |
---|
1561 | A = [tri_list[1],tri_list[2],tri_list[5]] |
---|
1562 | B = mesh.sets[mesh.setID['All']] |
---|
1563 | assert list_comp(A,B) |
---|
1564 | |
---|
1565 | |
---|
1566 | A = [tri_list[3],tri_list[2],tri_list[5]] |
---|
1567 | assert not list_comp(A,B) |
---|
1568 | |
---|
1569 | #testing |
---|
1570 | |
---|
1571 | def test_Discretised_Tuple_Set_rounding(self): |
---|
1572 | #This is the hardest bit of DST |
---|
1573 | from mesh import Discretised_Tuple_Set |
---|
1574 | |
---|
1575 | tol = 0.1 |
---|
1576 | a=Discretised_Tuple_Set(p_rel=1,t_rel= tol) |
---|
1577 | m = 0.541 |
---|
1578 | m_up = 0.6 |
---|
1579 | m_down = 0.5 |
---|
1580 | assert m_up == a.round_up_rel(m) |
---|
1581 | assert m_down == a.round_down_rel(m) |
---|
1582 | |
---|
1583 | tol = 0.1 |
---|
1584 | a=Discretised_Tuple_Set(p_rel=1,t_rel = tol) |
---|
1585 | m = 0.539 |
---|
1586 | m_up = 0.5 |
---|
1587 | m_down = 0.5 |
---|
1588 | assert m_up == a.round_up_rel(m) |
---|
1589 | assert m_down == a.round_down_rel(m) |
---|
1590 | |
---|
1591 | tol = 0.5 |
---|
1592 | a=Discretised_Tuple_Set(p_rel=1,t_rel = tol) |
---|
1593 | |
---|
1594 | |
---|
1595 | m = 0.6 |
---|
1596 | m_up = 0.7 |
---|
1597 | m_down = 0.5 |
---|
1598 | assert m_up == a.round_up_rel(m) |
---|
1599 | assert m_down == a.round_down_rel(m) |
---|
1600 | |
---|
1601 | m = 0.599 |
---|
1602 | m_up = 0.6 |
---|
1603 | m_down = 0.5 |
---|
1604 | assert m_up == a.round_up_rel(m) |
---|
1605 | assert m_down == a.round_down_rel(m) |
---|
1606 | |
---|
1607 | def test_Discretised_Tuple_Set_get(self): |
---|
1608 | from mesh import Discretised_Tuple_Set |
---|
1609 | tol = 0.25 |
---|
1610 | a=Discretised_Tuple_Set(p_rel=1,t_rel = tol) |
---|
1611 | b = (1.1,1.1) |
---|
1612 | a.append(b) |
---|
1613 | list = [(1.2,1.),(1.,1.),(1.,1.2),(1.2,1.2)] |
---|
1614 | for key in list: |
---|
1615 | assert a[key][0]==b |
---|
1616 | assert len(a[key])==1 |
---|
1617 | |
---|
1618 | c = (2.1,1.) |
---|
1619 | a.append(c) |
---|
1620 | assert a[(2.,1.)][0]==c |
---|
1621 | assert a[(2.2,1.)][0]==c |
---|
1622 | |
---|
1623 | def test_mapped_Discretised_Tuple_Set(self): |
---|
1624 | |
---|
1625 | from mesh import Mapped_Discretised_Tuple_Set |
---|
1626 | from mesh import Discretised_Tuple_Set |
---|
1627 | |
---|
1628 | def map(sequence): |
---|
1629 | return [len(sequence)] |
---|
1630 | |
---|
1631 | tol = 0.5 |
---|
1632 | a=Mapped_Discretised_Tuple_Set(map,p_rel=1,t_rel = tol) |
---|
1633 | b = range(20) |
---|
1634 | a.append(b) |
---|
1635 | assert b in a[range(17)] |
---|
1636 | assert b in a[range(22)] |
---|
1637 | |
---|
1638 | tol = 0.01 |
---|
1639 | a=Mapped_Discretised_Tuple_Set(map,p_rel=1,t_rel = tol) |
---|
1640 | b = range(20) |
---|
1641 | a.append(b) |
---|
1642 | assert b in a[range(20)] |
---|
1643 | assert b in a[range(19)] |
---|
1644 | assert not range(17) in a |
---|
1645 | |
---|
1646 | |
---|
1647 | def list_comp(A,B): |
---|
1648 | yes = len(A)==len(B) |
---|
1649 | for item in A: |
---|
1650 | if not item in B: |
---|
1651 | yes = False |
---|
1652 | return yes |
---|
1653 | |
---|
1654 | #___________end of Peters tests |
---|
1655 | |
---|
1656 | #------------------------------------------------------------- |
---|
1657 | if __name__ == "__main__": |
---|
1658 | suite = unittest.makeSuite(meshTestCase,'test') |
---|
1659 | #suite = unittest.makeSuite(meshTestCase,'test_asciiFile') |
---|
1660 | runner = unittest.TextTestRunner() #verbosity=2) |
---|
1661 | runner.run(suite) |
---|
1662 | |
---|