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