source: inundation/ga/storm_surge/pmesh/meshHarness.py @ 1001

Last change on this file since 1001 was 1001, checked in by duncan, 20 years ago

fixing issues in loading/saving .msh files

File size: 50.1 KB
Line 
1#!/usr/bin/env python
2#
3import tempfile
4import unittest
5from mesh import *
6from load_mesh.loadASCII import *
7
8class meshTestCase(unittest.TestCase):
9    def setUp(self):
10        pass
11       
12
13
14    def tearDown(self):
15        pass
16
17    def testPointDistance(self):
18        a = Point(0.0, 0.0)
19        b = Point(0.0, 10.0)
20       
21        self.failUnless( a.DistanceToPoint(b) == 10.0,
22                        'Point DistanceToPoint is wrong!')
23   
24    def testVertexDistance(self):
25        a = Vertex (0.0, 0.0)
26        b = Vertex (0.0, 10.0)
27       
28        self.failUnless( a.DistanceToPoint(b) == 10.0,
29                        'Point DistanceToPoint is wrong!')
30       
31    def testTriangle(self):
32        a = Vertex (0.0, 0.0)
33        b = Vertex (0.0, 2.0)
34        c = Vertex (2.0,0.0)
35        d = Vertex (0.0, 4.0)
36        e = Vertex (2.0, 2.0)
37        f = Vertex (4.0,0.0)
38       
39        t1 = Triangle(b,a,c)       
40        t2 = Triangle(b,c,e)     
41        t3 = Triangle(e,c,f)     
42        t4 = Triangle(d,b,e)
43        t2.setNeighbors(t3,t4,t1)
44       
45        self.failUnless( t2.neighbors[2].vertices[0] == b, 'Triangle initialisation is wrong!')
46   
47       
48    def testSegment(self):
49        a = Vertex (0.0, 0.0)
50        b = Vertex (0.0, 10.0)
51        s = Segment(a,b, tag = 20)     
52       
53        self.failUnless( s.vertices[0].DistanceToPoint(s.vertices[1]) == 10.0,
54                        'vertices in a segment are wrong')
55       
56        self.failUnless( s.tag == 20.0,
57                        'tag in a segment are wrong')
58
59    def testdeleteUserVertex(self):
60
61       
62        mesh = Mesh()
63        a = mesh.addUserVertex(0.0, 0.0)
64        b = mesh.addUserVertex (0.0, 2.0)
65        c = mesh.addUserVertex (2.0,0.0)
66       
67        s1 = mesh.addUserSegment(a,b)
68        s2 = mesh.addUserSegment(a,c)
69        s3 = mesh.addUserSegment(c,b)
70
71        mesh.deleteMeshObject (a) 
72        self.failUnless(mesh.userSegments[0] == s3,
73                        'Bad segment. ')       
74        self.failUnless(len(mesh.userSegments) ==1,
75                        'Segments not deleted.')
76        self.failUnless(len(mesh.userVertices) == 2,
77                        'Vertex not deleted.')
78       
79 
80       
81    def testgenerateMesh(self):
82        a = Vertex (0.0, 0.0)
83        d = Vertex (0.0, 4.0)
84        f = Vertex (4.0,0.0)
85
86        s1 = Segment(a,d)
87        s2 = Segment(d,f)
88        s3 = Segment(a,f)
89
90        r1 = Region(0.3, 0.3,tag = 1.3,maxArea = .6)
91        #print r1
92        m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3], regions=[r1] )
93       
94        m.generateMesh("QApz", maxArea = 2.1 )         
95
96        #print m
97
98        #m.plotMeshTriangle()
99
100        result = 1.414214
101        delta  = 0.00001
102       
103        self.failUnless((m.meshTriangles[1].vertices[0].x < result + delta) or
104                        (m.meshTriangles[1].vertices[0].x > result - delta),
105                        'generated mesh is wrong!')
106
107    def test_regionalMaxArea(self):
108        v0 = Vertex (0.0, 0.0)
109        v1 = Vertex (6.0, 0.0)
110        v2 = Vertex (6.0,6.0)
111        v3 = Vertex (0.0,6.0)
112
113        s1 = Segment(v0,v1)
114        s2 = Segment(v1,v2)
115        s3 = Segment(v3,v2)
116        s4 = Segment(v3,v0)
117        s5 = Segment(v2,v0)
118
119        r1 = Region(3, 1,tag = 1.3)
120        #print r1
121        m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], regions=[r1] )
122       
123        m.generateMesh("Apz", maxArea = 36 )         
124
125        #m.plotMeshTriangle()
126        #print "len(m.meshTriangles)",len(m.meshTriangles)
127
128        self.failUnless(len(m.meshTriangles) == 2, 
129                        'test_regionalMaxArea 1:generated mesh is wrong!')
130       
131        ## Another test case
132        r1 = Region(3, 1,tag = 1.3)
133        r2 = Region(1, 3,tag = 1.3, maxArea = 8)
134        m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5],
135                 regions=[r1,r2] )
136        m.generateMesh("Apz", maxArea = 36 )         
137        self.failUnless(len(m.meshTriangles) == 6, 
138                        'test_regionalMaxArea 2:generated mesh is wrong!')
139       
140        ## Another test case
141        r1 = Region(3, 1, tag = 1.3, maxArea = 8)
142        r2 = Region(1, 3, tag = 1.3, maxArea = 8)
143        m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5],
144                 regions=[r1,r2] )
145        m.generateMesh("Apz", maxArea = 36 ) 
146        #print "len(m.meshTriangles)",len(m.meshTriangles)       
147        self.failUnless(len(m.meshTriangles) == 8, 
148                        'test_regionalMaxArea 3:generated mesh is wrong!')
149       
150        ## Another test case
151        r1 = Region(3, 1, tag = 1.3 )
152        r2 = Region(1, 3, tag = 1.3, maxArea = 8)
153        m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5],
154                 regions=[r1,r2] )
155        m.generateMesh("Apz", maxArea = 8 )         
156        self.failUnless(len(m.meshTriangles) == 8, 
157                        'test_regionalMaxArea 4:generated mesh is wrong!')
158       
159        ## Another test case
160        r1 = Region(3, 1,tag = 1.3, maxArea = 8)
161        r2 = Region(1, 3,tag = 1.3, maxArea = 8)
162        m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5],
163                 regions=[r1,r2] )
164        m.generateMesh("Apz", maxArea = 36,isRegionalMaxAreas = False )     
165        self.failUnless(len(m.meshTriangles) == 2, 
166                        'test_regionalMaxArea 5:generated mesh is wrong!')
167       
168    def testdeleteUserVertex(self):
169        mesh = Mesh()
170        a = mesh.addUserVertex(0.0, 0.0)
171        b = mesh.addUserVertex (0.0, 2.0)
172        c = mesh.addUserVertex (2.0,0.0)
173       
174        s1 = mesh.addUserSegment(a,b)
175        s2 = mesh.addUserSegment(a,c)
176        s3 = mesh.addUserSegment(c,b)
177
178        mesh.deleteMeshObject (s2)
179       
180        #print ",s2 in mesh.userSegments" ,s2 in mesh.userSegments
181        self.failUnless(not(s2 in mesh.userSegments),
182                        'Bad segment. ')       
183        self.failUnless(len(mesh.userSegments) ==2,
184                        'Segments not deleted.')
185        self.failUnless(len(mesh.userVertices) == 3,
186                        'Vertex deleted, instead of segment.')
187
188    def testTriangleArea(self):
189        a = Vertex (10.0, 10.0)
190        b = Vertex (10.0, 20.0)
191        c = Vertex (20.0,10.0)
192       
193        d = Vertex (-20.0, 0.0)
194        e = Vertex (-20.0, -20.0)
195        f = Vertex (20.0,-20.0)
196       
197        t1 = Triangle(b,a,c)     
198        t2 = Triangle(e,d,f)
199       
200#         print "t1", t1
201#         print "t1 area ", t1.calcArea()
202#         print "t2", t2
203#         print "t2 area ", t2.calcArea()
204        self.failUnless( t1.calcArea() == 50 and t2.calcArea() == 400, 'Triangle area is wrong!')
205    def testisUserSegmentNew (self):
206        mesh = Mesh()
207        a = mesh.addUserVertex(0.0, 0.0)
208        b = mesh.addUserVertex (0.0, 2.0)
209        c = mesh.addUserVertex (2.0,0.0)
210        d = mesh.addUserVertex (2.0,3.0)
211       
212        s1 = mesh.addUserSegment(a,b)
213        s2 = mesh.addUserSegment(a,c)
214        s3 = mesh.addUserSegment(c,b)
215
216        self.failUnless(mesh.isUserSegmentNew(a,d) ,
217                        'Segment should be new. ')
218        self.failUnless(not(mesh.isUserSegmentNew(a,b)) ,
219                        'Segment should not be new. ')
220
221
222    def testisUserSegmentNew (self):
223        mesh = Mesh()
224        a = mesh.addUserVertex(0.0, 0.0)
225        b = mesh.addUserVertex (0.0, 2.0)
226        c = mesh.addUserVertex (2.0,0.0)
227        d = mesh.addUserVertex (2.0,3.0)
228       
229        s1 = mesh.addUserSegment(a,b)
230        s2 = mesh.addUserSegment(a,c)
231        s3 = mesh.addUserSegment(c,b)
232
233        self.failUnless(mesh.representedUserSegment(a,d) == None,
234                        'Segment should be new. ')
235        self.failUnless(mesh.representedUserSegment(a,b) == s1 ,
236                        'Segment should not be new. ')
237       
238    def ztestautoSegment(self):
239        p0 = Vertex (0.0, 0.0)
240        p1 = Vertex (0.0, 4.0)
241        p2 = Vertex (4.0,4.0)
242        p3 = Vertex (4.0,0.0)
243
244        s1 = Segment(p0,p1)
245       
246        m = Mesh(userVertices=[p0, p1, p2, p3], userSegments=[s1] ) 
247        m.autoSegment()
248       
249        #print 'Len', len(m.userSegments)
250        self.failUnless(len(m.getUserSegments()) == 4 ,
251                        'userSegments is wrong!')
252     
253        m.autoSegment()
254        self.failUnless(len(m.getUserSegments()) == 4 ,
255                        'userSegments is wrong!')
256     
257    def ztestautoSegmentII(self):
258        p1 = Vertex (3.0, 4.0)
259        p2 = Vertex (3.0,2.0)
260        p3 = Vertex (3.0,0.0)
261        p4 = Vertex (6.0, 4.0)
262        p5 = Vertex (6.0,2.0)
263        p0 = Vertex (6.0,0.0)
264
265
266        s1 = Segment(p2,p3)
267        s2 = Segment(p4,p5)
268       
269        m = Mesh(userVertices=[p0, p1, p2, p3, p4, p5],
270                 userSegments=[s1, s2])     
271
272        m.autoSegment()
273       
274        s3 = m.representedAlphaUserSegment(p3,p0)
275        self.failUnless(not (s3 == None) ,
276                        'userSegments is wrong!')
277
278       
279        s6 = m.representedAlphaUserSegment(p1,p4)       
280        self.failUnless(not (s6 == None) ,
281                        'userSegments is wrong!')
282       
283        # remove a segment, add a point, autosegment
284        m.alphaUserSegments.remove(s3)
285        p6 = Vertex (1.0, 2.0)
286        m.userVertices.append(p6)
287        m.autoSegment()
288       
289        s1_now = m.representedUserSegment(p3,p2)
290        self.failUnless(s1_now == s1 ,
291                        'userSegments is wrong!')
292       
293        s2_now = m.representedUserSegment(p5,p4)       
294        self.failUnless(s2_now == s2 ,
295                        'userSegments is wrong!')
296       
297        s3 = m.representedAlphaUserSegment(p3,p6)       
298        self.failUnless(not (s3 == None) ,
299                        'userSegments is wrong!')
300       
301        s4 = m.representedAlphaUserSegment(p3,p6)       
302        self.failUnless(not (s4 == None) ,
303                        'userSegments is wrong!')
304       
305        s5 = m.representedAlphaUserSegment(p4,p6)       
306        self.failUnless(s5 == None ,
307                        'userSegments is wrong!')
308       
309        s6_now = m.representedAlphaUserSegment(p1,p4)
310        self.failUnless(s6_now == s6 ,
311                        'userSegments is wrong!')
312       
313           
314       
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        seg = m.getMeshSegments()
425       
426        #print "seg[0].tag"
427        #print seg[0].tag
428        #print "seg[0].tag"
429        self.failUnless(seg[0].tag == 5 and
430                        seg[1].tag == 7 and
431                        seg[2].tag == 9 and
432                        seg[3].tag == 7 and
433                        seg[4].tag == 9 and
434                        seg[5].tag == 7 and
435                        seg[6].tag == 5,
436                        'seg tags are wrong')
437
438    def test_segtag2(self):
439   
440        a = Vertex (0.0, 0.0)
441        d = Vertex (0.0, 4.0)
442        f = Vertex (4.0,0.0)
443        e = Vertex (1.0,1.0)
444   
445        s1 = Segment(a,d)
446        s2 = Segment(d,f)
447        s3 = Segment(a,f)
448        s4 = Segment(a,e)
449     
450        m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4])
451
452        m.generateMesh("QApz", maxArea = 2.1)
453
454        seg = m.getMeshSegments()
455        self.failUnless(seg[0].tag == "exterior" and
456                        seg[1].tag == "exterior" and
457                        seg[2].tag == "exterior" and
458                        seg[3].tag == "" and
459                        seg[4].tag == "exterior",
460                        '2nd seg tags are wrong')
461
462    def test_asciiFile(self):
463   
464        a = Vertex (0.0, 0.0)  #, attributes = [1.1])
465        d = Vertex (0.0, 4.0)  #, attributes = [1.2])
466        f = Vertex (4.0,0.0)  #, attributes = [1.3])
467        e = Vertex (1.0,1.0)  #, attributes = [1.4])
468   
469        s1 = Segment(a,d)
470        s2 = Segment(d,f)
471        s3 = Segment(a,f)
472        s4 = Segment(a,e)
473     
474        m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4])
475
476        m.generateMesh("QApz", maxArea = 2.1 )
477
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_mesh(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        m = Mesh(userVertices=[a,d,f,e],
679                 userSegments=[s1,s2,s3,s4],
680                 regions=[r1])
681
682        m.generateMesh("QApz", maxArea = 2.1)
683        fileName = tempfile.mktemp(".tsh")
684        #print "dgs!!!"
685        #print "****************** fileName", fileName
686        m.export_mesh_file(fileName)
687        #print "******************"
688        #print "m", m
689        #print "******************"
690        m_returned = importMeshFromFile(fileName)
691        #print "m_returned",m_returned
692        #print "******************"
693        #print "****************** fileName", fileName
694        os.remove(fileName)
695        self.failUnless(0 == m.__cmp__(m_returned),
696                        'loading and saving of a mesh failed')
697
698       
699    def test_normaliseMesh(self):
700       
701        a_att = [5.0,2.0]
702        d_att =[4.0,2.0]
703        f_att = [3.0,2.0]
704        e_att = [2.0,2.0]
705        a_xy = [10.0, 10.0]
706        a = Vertex ( a_xy[0],a_xy[1], attributes =a_att)
707        d = Vertex (15.0, 10.0, attributes =d_att)
708        f = Vertex (10.0,20.0, attributes =f_att)
709        e = Vertex (15.0,20.0, attributes =e_att)
710   
711        s1 = Segment(a,d, tag = 50)
712        s2 = Segment(d,e, tag = 40)
713        s3 = Segment(e,f, tag = 30)
714        s4 = Segment(f,a, tag = 20)
715     
716        r1 = Region(0.3, 0.3,tag = 1.3)
717        m = Mesh(userVertices=[a,d,f,e],
718                 userSegments=[s1,s2,s3,s4],
719                 regions=[r1])
720        m.normaliseMesh(1,0,1)
721        [xmin, ymin, xmax, ymax] = m.boxsize()
722        [attmin, attmax] = m.maxMinVertAtt(0)
723        self.failUnless(attmin == 0.0 and attmax == 1.0,
724                        'normalise failed')
725        self.failUnless(xmin == 0.0 and ymin == 0.0 and xmax == 0.5 and ymax == 1.0,
726                        'normalise failed')
727        m.normaliseMesh(200,-100,5)
728        [xmin, ymin, xmax, ymax] = m.boxsize()
729        [attmin, attmax] = m.maxMinVertAtt(0)
730        self.failUnless(attmin == 0.0 and attmax == 5.0,
731                        'normalise failed')
732        self.failUnless(xmin == -100.0 and ymin == -100.0 and xmax == 0.0 and ymax == 100.0,
733                        'normalise failed')
734    # Fix me, this function has a bug   
735    def zztest_exportASCIIsegmentoutlinefile(self):
736        a = Vertex (0,0)
737        b = Vertex (0,3)
738        c = Vertex (3,3)
739        d = Vertex (1,2)
740        e = Vertex (3,1)
741     
742        s1 = Segment(a,b, tag = "50")
743        s2 = Segment(b,c, tag = "40")
744        s3 = Segment(c,a, tag = "30")
745     
746        r1 = Region(2, 1,tag = "1.3")
747        h1 = Hole(1,4)
748        m = Mesh(userVertices=[a,b,c,d,e],
749                 userSegments=[s1,s2,s3],
750                 regions=[r1],
751                 holes = [h1])     
752       
753        m.generateMesh("QApz", maxArea = 2.1)
754        #print "mesh ***************dsg*", m
755
756        fileName = tempfile.mktemp(".tsh")
757        m.exportASCIIsegmentoutlinefile(fileName)
758       
759        m_returned = importMeshFromFile(fileName)
760
761        #print "m_returned ****",m_returned
762        #print "****************** fileName", fileName
763        os.remove(fileName)
764
765        #Trim mesh, so it should like like m_returned
766        m.meshVertices = []
767        m.meshTriangles = []
768        m.meshSegments = []
769        m.userVertices=[a,b,c]
770        #print "mesh ***************dsg*", m
771        #print "(m.__cmp__(m_returned)", m.__cmp__(m_returned)
772        self.failUnless(0 == m.__cmp__(m),
773                        'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed')
774        self.failUnless(0 == m.__cmp__(m_returned),
775                        'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed')
776
777    def test_loadxy2(self):
778        """
779        To test the mesh side of loading xya files.
780        Not the loading of xya files
781        """
782        import os
783        import tempfile
784       
785        fileName = tempfile.mktemp(".xya")
786        file = open(fileName,"w")
787        file.write("elevation, speed \n\
7881.0, 0.0, 10.0, 0.0\n\
7890.0, 1.0, 0.0, 10.0\n\
7901.0, 0.0, 10.4, 40.0\n")
791        file.close()
792        #print fileName
793        m = importMeshFromFile(fileName)
794        os.remove(fileName)
795       
796    def test_loadxy(self):
797        """
798        To test the mesh side of loading xya files.
799        Not the loading of xya files
800        """
801        import os
802        import tempfile
803       
804        fileName = tempfile.mktemp(".xya")
805        file = open(fileName,"w")
806        file.write("elevation speed \n\
8071.0 0.0 10.0 0.0\n\
8080.0 1.0 0.0 10.0\n\
8091.0 0.0 10.4 40.0\n")
810        file.close()
811        #print fileName
812        m = importMeshFromFile(fileName)
813        os.remove(fileName)
814        self.failUnless(m.userVertices[0].x == 1.0,
815                        'loadxy, test 1 failed')
816        self.failUnless(m.userVertices[0].y == 0.0,
817                        'loadxy, test 2 failed')
818        #self.failUnless(m.userVertices[0].attributes == [10.0,0.0],
819        #                'loadxy, test 2.2 failed')
820        self.failUnless(m.userVertices[1].x == 0.0,
821                        'loadxy, test 3 failed')
822        self.failUnless(m.userVertices[1].y == 1.0,
823                        'loadxy, test 4 failed')
824        #self.failUnless(m.userVertices[1].attributes == [0.0,10.0],
825        #                'loadxy, test 5 failed')
826       
827    def test_exportxyafile(self):
828        a = Vertex (0,0)
829        b = Vertex (0,3)
830        c = Vertex (3,3)
831        d = Vertex (1,2)
832        e = Vertex (3,1)
833        f = Vertex (3,1)
834     
835        s1 = Segment(a,b, tag = 50)
836        s2 = Segment(b,c, tag = 40)
837        s3 = Segment(c,a, tag = 30)
838     
839        r1 = Region(2, 1,tag = 1.3)
840        h1 = Hole(1,4)
841        # Warning mesh can't produce this type of data structure its self
842        m = Mesh(userVertices=[a,b,c,d,e],
843                 userSegments=[s1,s2,s3],
844                 regions=[r1],
845                 holes = [h1])
846       
847        fileName = tempfile.mktemp(".txt")
848        m.exportxyafile(fileName)
849        file = open(fileName)
850        lFile = file.read().split('\n')
851        file.close()
852
853        #print "**********************"
854        #print lFile
855        #print "**********************"
856       
857        os.remove(fileName)
858        self.failUnless(lFile[0] == "5 0 # <vertex #> <x> <y> [attributes]" and
859                        lFile[1] == "0 0 " and
860                        lFile[2] == "0 3 " and
861                        lFile[3] == "3 3 " and
862                        lFile[4] == "1 2 " and
863                        lFile[5] == "3 1 " 
864                        ,
865                        'exported Ascii xya file is wrong')
866       
867        m.generateMesh("QApz", maxArea = 2.1)
868        fileName = tempfile.mktemp(".txt")
869        m.exportxyafile(fileName)
870        file = open(fileName)
871        lFile = file.read().split('\n')
872        file.close()
873        os.remove(fileName)
874        self.failUnless(lFile[0] == "6 0 # <vertex #> <x> <y> [attributes]" and
875                        lFile[1] == "0.0 0.0 " and
876                        lFile[2] == "0.0 3.0 " and
877                        lFile[3] == "3.0 3.0 " and
878                        lFile[4] == "1.0 2.0 " and
879                        lFile[5] == "3.0 1.0 " and
880                        lFile[6] == "1.5 1.5 " 
881                        ,
882                        'exported Ascii xya file is wrong')
883       
884    def test_strings2ints(self):
885        list = ["sea","river inlet","","sea","","moat"]
886        preset = ["moat", "internal boundary"]
887        [intlist, converter] = segment_strings2ints(list,preset )
888        self.failUnless(intlist == [2,3 ,0 ,2 ,0 ,0 ]
889                        ,
890                        'test_strings2ints produces bad intlist')
891        self.failUnless(converter == ['moat', 'internal boundary',
892                                      'sea', 'river inlet']
893                        ,
894                        'test_strings2ints produces bad converter')
895       
896    def test_ints2strings(self):
897        list = ["internal boundary","sea","river inlet",
898            "","sea","","moat","internal boundary"]
899        outlist = ['internal boundary', 'sea', 'river inlet', 'moat',
900                   'sea', 'moat', 'moat', 'internal boundary']
901        preset = ["moat", "internal boundary"]
902        [intlist, converter] = segment_strings2ints(list,preset )
903        newlist = segment_ints2strings(intlist, converter)
904        self.failUnless(outlist == newlist
905                        ,
906                        'test_strings2ints produces bad intlist')
907        self.failUnless(converter == ['moat', 'internal boundary',
908                                      'sea', 'river inlet']
909                        ,
910                        'test_strings2ints produces bad converter')
911       
912    def test_ints2strings2(self):
913        list = ["","",""]
914        preset = ["moat", "internal boundary"]
915        [intlist, converter] = segment_strings2ints(list,preset )
916        newlist = segment_ints2strings(intlist, converter)
917        outlist = ['moat', 'moat', 'moat']
918        self.failUnless(outlist == newlist
919                        ,
920                        'test_strings2ints produces bad intlist')
921        self.failUnless(converter == ['moat', 'internal boundary']
922                        ,
923                        'test_strings2ints produces bad converter')
924
925       
926    def test_removeDuplicatedVertices(self):
927        a = Vertex (0,0)
928        a.index = 0
929        b = Vertex (0,3)
930        b.index = 1
931        c = Vertex (3,3)
932        c.index = 2
933        d = Vertex (1,1)
934        d.index = 3
935        e = Vertex (3,1)
936        e.index = 4
937        f = Vertex (1,1)
938        f.index = 5
939        g = Vertex (1,1)
940        g.index = 6
941        inputVerts_noDups = [a,b,c,d,e]
942       
943        m = Mesh(userVertices=[a,b,c,d,e,f,g])
944        counter = m.removeDuplicatedUserVertices()
945        UserVerts = m.getUserVertices()
946       
947         
948        self.failUnless(UserVerts == inputVerts_noDups,
949                            'duplicate verts not removed')
950        #for userVert, inputVert in map(None, UserVerts, inputVerts_noDups):
951        #    self.failUnless(userVert.x == inputVert.x,
952        #                    'x duplicate verts not removed')
953        #    self.failUnless(userVert.y == inputVert.y,
954        #                    'y duplicate verts not removed')
955
956
957    def test_ungenerateFileLoading(self):
958       
959        fileName = tempfile.mktemp(".txt")
960        file = open(fileName,"w")
961        file.write("         1       ??      ??\n\
962       0.0       0.0\n\
963       1.0       0.0\n\
964       1.0       1.0\n\
965       0.0       1.0\n\
966       0.0       0.0\n\
967END\n\
968         2      ?? ??\n\
969       10.0       10.0\n\
970       10.0       20.0\n\
971       20.0       20.0\n\
972       10.0       10.0\n\
973END\n\
974END\n")
975        file.close()
976       
977       
978        a = Vertex (0.0, 0.0) #, attributes = [1.1])
979        b = Vertex (0.0, 40.0) #, attributes = [1.2])
980        c = Vertex (40.0,40.0) #, attributes = [1.3])
981        d = Vertex (40.0,0.0) #, attributes = [1.4])
982   
983        s1 = Segment(a,b)
984        s2 = Segment(b,c)
985        s3 = Segment(c,d)
986        s4 = Segment(d,a)
987     
988        m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
989        dict = importUngenerateFile(fileName)
990
991        tag = "DSG"
992        Segment.set_default_tag(tag)
993        m.addVertsSegs(dict)
994
995        # have to reset this , since it's a class attribute
996        Segment.set_default_tag("")
997           
998        self.failUnless(len(m.userSegments) ==11,
999                        'Wrong segment list length.')
1000        self.failUnless(len(m.userVertices) == 11,
1001                        'Wrong vertex list length.')
1002        self.failUnless(m.userSegments[10].vertices[0] == m.userVertices[10],
1003                        'bad vertex on segment.')
1004        self.failUnless(m.userSegments[10].vertices[1] == m.userVertices[8],
1005                        'Bad segment.')
1006        self.failUnless(m.userSegments[10].tag == tag,
1007                        'wrong tag.')
1008   
1009    def test_ungenerateFileLoadingII(self):
1010       
1011        fileName = tempfile.mktemp(".txt")
1012        file = open(fileName,"w")
1013        file.write("         1       ??      ??\n\
1014       0.0       0.0\n\
1015       1.0       0.0\n\
1016       1.0       1.0\n\
1017       0.0       1.0\n\
1018       0.0       0.0\n\
1019END\n\
1020         2      ?? ??\n\
1021       10.0       10.0\n\
1022       10.0       20.0\n\
1023       20.0       20.0\n\
1024END\n\
1025END\n")
1026        file.close()
1027       
1028       
1029        a = Vertex (0.0, 0.0) #, attributes = [1.1])
1030        b = Vertex (0.0, 40.0) #, attributes = [1.2])
1031        c = Vertex (40.0,40.0) #, attributes = [1.3])
1032        d = Vertex (40.0,0.0) #, attributes = [1.4])
1033   
1034        s1 = Segment(a,b)
1035        s2 = Segment(b,c)
1036        s3 = Segment(c,d)
1037        s4 = Segment(d,a)
1038     
1039        m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
1040        dict = importUngenerateFile(fileName)
1041
1042        tag = "DSG"
1043        Segment.set_default_tag(tag)
1044        m.addVertsSegs(dict)
1045
1046        # have to reset this , since it's a class attribute
1047        Segment.set_default_tag("")
1048           
1049        self.failUnless(len(m.userSegments) ==10,
1050                        'Wrong segment list length.')
1051        self.failUnless(len(m.userVertices) == 11,
1052                        'Wrong vertex list length.')
1053
1054    def test_exportASCIImeshfile(self):
1055   
1056        #a_att = [5,2]
1057        #d_att =[4,2]
1058        #f_att = [3,2]
1059        #e_att = [2,2]
1060        a_xy = [0.0, 0.0]
1061        a = Vertex ( a_xy[0],a_xy[1]) #, attributes =a_att)
1062        d = Vertex (0.0, 4.0) #, attributes =d_att)
1063        f = Vertex (4.0,0.0) #, attributes =f_att)
1064        e = Vertex (1.0,1.0) #, attributes =e_att)
1065   
1066        s1 = Segment(a,d, tag = "50")
1067        s2 = Segment(d,f, tag = "40")
1068        s3 = Segment(a,f, tag = "30")
1069        s4 = Segment(a,e, tag = "20")
1070     
1071        r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 36)
1072
1073
1074        h1 = Hole(0.2,0.6)
1075       
1076        m = Mesh(userVertices=[a,d,f,e],
1077                 userSegments=[s1,s2,s3,s4],
1078                 regions=[r1],
1079                 holes=[h1])
1080
1081        seg = m.getUserSegments()
1082        points = m.getUserVertices()
1083        holes = m.getHoles()
1084        regions = m.getRegions()
1085        fileName = tempfile.mktemp(".tsh")
1086        m.export_mesh_file(fileName)
1087        #print "***************************fileName", fileName
1088        new_m = importMeshFromFile(fileName)
1089        os.remove(fileName)
1090       
1091
1092        #print '**@@@@@******'
1093        #print "new_m",new_m
1094        #print '**@@@@@******'
1095        #print "m",m
1096        #print '**@@@@@******'
1097       
1098        self.failUnless( new_m == m,
1099                         'loadASCIITestCase failed. test new 1')
1100           
1101    def test_Mesh2MeshList(self):
1102
1103        a_att = [5,2]
1104        d_att =[4,2]
1105        f_att = [3,2]
1106        e_att = [2,2]
1107        a_xy = [0.0, 0.0]
1108        a = Vertex ( a_xy[0],a_xy[1]) #, attributes =a_att)
1109        d = Vertex (0.0, 4.0) #, attributes =d_att)
1110        f = Vertex (4.0,0.0) #, attributes =f_att)
1111        e = Vertex (1.0,1.0) #, attributes =e_att)
1112   
1113        s1 = Segment(a,d, tag = "50")
1114        s2 = Segment(d,f, tag = "40")
1115        s3 = Segment(a,f, tag = "30")
1116        s4 = Segment(a,e, tag = "20")
1117     
1118        r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 45)
1119        m = Mesh(userVertices=[a,d,f,e],
1120                 userSegments=[s1,s2,s3,s4],
1121                 regions=[r1])
1122
1123        m.generateMesh("QApza2.1")
1124
1125        seg = m.getMeshSegments()
1126        points = m.getMeshVertices()
1127        dict = m.Mesh2MeshList()
1128        #print "dict",dict
1129        # test not finished...
1130 
1131    def test_Mesh2IOTriangulationDict(self):
1132
1133        a_att = [5,2]
1134        d_att =[4,2]
1135        f_att = [3,2]
1136        e_att = [2,2]
1137        a_xy = [0.0, 0.0]
1138        a = Vertex ( a_xy[0],a_xy[1] , attributes =a_att)
1139        d = Vertex (0.0, 4.0 , attributes =d_att)
1140        f = Vertex (4.0,0.0 , attributes =f_att)
1141        e = Vertex (1.0,1.0 , attributes =e_att)
1142   
1143        s1 = Segment(a,d, tag = "50")
1144        s2 = Segment(d,f, tag = "40")
1145        s3 = Segment(a,f, tag = "30")
1146        s4 = Segment(a,e, tag = "20")
1147     
1148        r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 45)
1149        m = Mesh(userVertices=[a,d,f,e],
1150                 userSegments=[s1,s2,s3,s4],
1151                 regions=[r1])
1152        titles = ['ele','friction']
1153        m.attributeTitles = titles
1154        m.generateMesh("QApza2.1")
1155
1156        seg = m.getMeshSegments()
1157        verts = m.getMeshVertices()
1158        dict = m.Mesh2IOTriangulationDict()
1159        #print "dict",dict
1160       
1161        self.failUnless( dict['vertex_attribute_titles'] == titles,
1162                         'test_Mesh2IOTriangulationDict failed. test 1')
1163        answer = [a_xy,[0.0, 4.0],[4.0,0.0], [1.0,1.0], [2.0,2.0]]
1164        #print "answer",answer
1165        #print "dict['vertices']",dict['vertices']
1166       
1167        self.failUnless( dict['vertices'] == answer,
1168                         'test_Mesh2IOTriangulationDict failed. test 2')
1169
1170       
1171        for pimport,pactual,pimpatt in map(None,dict['vertices'],
1172                                           verts,dict['vertex_attributes']):
1173            #assert all_close( pimport, (pactual.x,pactual.y))
1174            self.failUnless( pimport == [pactual.x,pactual.y],
1175                        'test_Mesh2IOTriangulationDict failed. test 2.1')
1176            self.failUnless( pimpatt == pactual.attributes,
1177                        'test_Mesh2IOTriangulationDict failed. test 2.2')
1178        self.failUnless( dict['segments'][0] == [0,1],
1179                        'test_Mesh2IOTriangulationDict failed. test 3')
1180        for segimp,segactual in map(None,dict['segment_tags'],seg):
1181            self.failUnless( segimp == segactual.tag,
1182                        'test_Mesh2IOTriangulationDict failed. test 4')
1183        #print " dict['triangles'][0]", dict['triangles'][0]
1184        self.failUnless( dict['triangles'][0] == [3,2,4],
1185                        'test_Mesh2IOTriangulationDict failed. test 5')
1186        self.failUnless( dict['triangle_neighbors'][0] == [-1,2,3],
1187                        'test_Mesh2IOTriangulationDict failed. test 6')
1188        self.failUnless( dict['triangle_tags'][0] == ["1.3"],
1189                         'test_Mesh2IOTriangulationDict failed. test 7')
1190
1191 
1192    def test_Mesh2IODict(self):
1193
1194        a_att = [5,2]
1195        d_att =[4,2]
1196        f_att = [3,2]
1197        e_att = [2,2]
1198        a_xy = [0.0, 0.0]
1199        a = Vertex ( a_xy[0],a_xy[1] , attributes =a_att)
1200        d = Vertex (0.0, 4.0 , attributes =d_att)
1201        f = Vertex (4.0,0.0 , attributes =f_att)
1202        e = Vertex (1.0,1.0 , attributes =e_att)
1203   
1204        s1 = Segment(a,d, tag = "50")
1205        s2 = Segment(d,f, tag = "40")
1206        s3 = Segment(a,f, tag = "30")
1207        s4 = Segment(a,e, tag = "20")
1208     
1209        r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 45)
1210        m = Mesh(userVertices=[a,d,f,e],
1211                 userSegments=[s1,s2,s3,s4],
1212                 regions=[r1])
1213        titles = ['ele','friction']
1214        m.attributeTitles = titles
1215        m.generateMesh("QApza2.1")
1216
1217        seg = m.getMeshSegments()
1218        verts = m.getMeshVertices()
1219        dict = m.Mesh2IODict()
1220        #print "dict",dict
1221       
1222        self.failUnless( dict['vertex_attribute_titles'] == titles,
1223                         'test_Mesh2IOTriangulationDict failed. test 1')
1224        answer = [a_xy,[0.0, 4.0],[4.0,0.0], [1.0,1.0], [2.0,2.0]]
1225        #print "answer",answer
1226        #print "dict['vertices']",dict['vertices']
1227       
1228        self.failUnless( dict['vertices'] == answer,
1229                         'test_Mesh2IOTriangulationDict failed. test 2')
1230
1231       
1232        for pimport,pactual,pimpatt in map(None,dict['vertices'],
1233                                           verts,dict['vertex_attributes']):
1234            #assert all_close( pimport, (pactual.x,pactual.y))
1235            self.failUnless( pimport == [pactual.x,pactual.y],
1236                        'test_Mesh2IODict failed. test 2.1')
1237            self.failUnless( pimpatt == pactual.attributes,
1238                        'test_Mesh2IODict failed. test 2.2')
1239        self.failUnless( dict['segments'][0] == [0,1],
1240                        'test_Mesh2IODict failed. test 3')
1241        for segimp,segactual in map(None,dict['segment_tags'],seg):
1242            self.failUnless( segimp == segactual.tag,
1243                        'test_Mesh2IODict failed. test 4')
1244        #print " dict['triangles'][0]", dict['triangles'][0]
1245        self.failUnless( dict['triangles'][0] == [3,2,4],
1246                        'test_Mesh2IODict failed. test 5')
1247        self.failUnless( dict['triangle_neighbors'][0] == [-1,2,3],
1248                        'test_Mesh2IODict failed. test 6')
1249        self.failUnless( dict['triangle_tags'][0] == ["1.3"],
1250                         'test_Mesh2IODict failed. test 7')
1251
1252        seg = m.getUserSegments()
1253        points = m.getUserVertices()
1254        holes = m.getHoles()
1255        regions = m.getRegions()
1256       
1257        for pimport,pactual,pimpatt in map(None,dict['points'],points,dict['point_attributes']):
1258            self.failUnless( pimport == [pactual.x,pactual.y],
1259                        'test_Mesh2IODict failed. test 1')
1260            self.failUnless( pimpatt == pactual.attributes,
1261                        'test_Mesh2IODict failed. test 1.1')
1262        self.failUnless( dict['outline_segments'][0] == [0,1],
1263                        'test_Mesh2IODict failed. test 3')
1264        for segimp,segactual in map(None,dict['outline_segment_tags'],seg):
1265            self.failUnless( segimp == segactual.tag,
1266                        'test_Mesh2IODict failed. test 4')
1267        for holeimp,holeactual in map(None,dict['holes'],holes):
1268            self.failUnless( holeimp == [holeactual.x,holeactual.y],
1269                        'test_Mesh2IODict failed. test 5')
1270       
1271        for regimp,regactual,regattimp, regmaxarea in map(None,dict['regions'],regions, dict['region_tags'], dict['region_max_areas']):
1272            self.failUnless( regimp == [regactual.x,regactual.y],
1273                        'loadASCIITestCase failed. test 6')
1274            self.failUnless( regattimp == regactual.getTag(),
1275                        'loadASCIITestCase failed. test 7')
1276            self.failUnless( regmaxarea == regactual.getMaxArea(),
1277                        'loadASCIITestCase failed. test 7')
1278   
1279           
1280       
1281    def test_Mesh2IOOutlineDict(self):
1282
1283        a_att = [5,2]
1284        d_att =[4,2]
1285        f_att = [3,2]
1286        e_att = [2,2]
1287        a_xy = [0.0, 0.0]
1288        a = Vertex ( a_xy[0],a_xy[1] , attributes =a_att)
1289        d = Vertex (0.0, 4.0 , attributes =d_att)
1290        f = Vertex (4.0,0.0 , attributes =f_att)
1291        e = Vertex (1.0,1.0 , attributes =e_att)
1292   
1293        s1 = Segment(a,d, tag = "50")
1294        s2 = Segment(d,f, tag = "40")
1295        s3 = Segment(a,f, tag = "30")
1296        s4 = Segment(a,e, tag = "20")
1297     
1298        r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 45)
1299        m = Mesh(userVertices=[a,d,f,e],
1300                 userSegments=[s1,s2,s3,s4],
1301                 regions=[r1])
1302        titles = ['ele','friction']
1303        m.attributeTitles = titles
1304        m.generateMesh("QApza2.1")
1305
1306        seg = m.getMeshSegments()
1307        verts = m.getMeshVertices()
1308        dict = m.Mesh2IOOutlineDict()
1309       
1310        seg = m.getUserSegments()
1311        points = m.getUserVertices()
1312        holes = m.getHoles()
1313        regions = m.getRegions()
1314       
1315        for pimport,pactual,pimpatt in map(None,dict['points'],points,dict['point_attributes']):
1316            self.failUnless( pimport == [pactual.x,pactual.y],
1317                        'loadASCIITestCase failed. test 1')
1318            self.failUnless( pimpatt == pactual.attributes,
1319                        'loadASCIITestCase failed. test 1.1')
1320        self.failUnless( dict['outline_segments'][0] == [0,1],
1321                        'loadASCIITestCase failed. test 3')
1322        for segimp,segactual in map(None,dict['outline_segment_tags'],seg):
1323            self.failUnless( segimp == segactual.tag,
1324                        'loadASCIITestCase failed. test 4')
1325        for holeimp,holeactual in map(None,dict['holes'],holes):
1326            self.failUnless( holeimp == [holeactual.x,holeactual.y],
1327                        'loadASCIITestCase failed. test 5')
1328        #for regimp,regactual in map(None,dict['regions'],regions):
1329         #   self.failUnless( [regimp[0],regimp[1]]==[regactual.x,regactual.y],
1330          #              'loadASCIITestCase failed. test 6')
1331           # self.failUnless( regimp[2] == regactual.getTag(),
1332            #            'loadASCIITestCase failed. test 7')
1333            #self.failUnless( regimp[3] == regactual.getMaxArea(),
1334             #           'loadASCIITestCase failed. test 7')
1335
1336           
1337        for regimp,regactual,regattimp, regmaxarea in map(None,dict['regions'],regions, dict['region_tags'], dict['region_max_areas']):
1338            self.failUnless( regimp == [regactual.x,regactual.y],
1339                        'loadASCIITestCase failed. test 6')
1340            self.failUnless( regattimp == regactual.getTag(),
1341                        'loadASCIITestCase failed. test 7')
1342            self.failUnless( regmaxarea == regactual.getMaxArea(),
1343                        'loadASCIITestCase failed. test 7')
1344   
1345           
1346#-------------------------------------------------------------
1347if __name__ == "__main__":
1348    suite = unittest.makeSuite(meshTestCase,'test')
1349    #suite = unittest.makeSuite(meshTestCase,'test_exportASCIImeshfile')
1350    runner = unittest.TextTestRunner() #verbosity=2)
1351    runner.run(suite)
1352   
Note: See TracBrowser for help on using the repository browser.