#!/usr/bin/env python # import tempfile import unittest from mesh import * from load_mesh.loadASCII import * class meshTestCase(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def testPointDistance(self): a = Point(0.0, 0.0) b = Point(0.0, 10.0) self.failUnless( a.DistanceToPoint(b) == 10.0, 'Point DistanceToPoint is wrong!') def testVertexDistance(self): a = Vertex (0.0, 0.0) b = Vertex (0.0, 10.0) self.failUnless( a.DistanceToPoint(b) == 10.0, 'Point DistanceToPoint is wrong!') def testTriangle(self): a = Vertex (0.0, 0.0) b = Vertex (0.0, 2.0) c = Vertex (2.0,0.0) d = Vertex (0.0, 4.0) e = Vertex (2.0, 2.0) f = Vertex (4.0,0.0) t1 = Triangle(b,a,c) t2 = Triangle(b,c,e) t3 = Triangle(e,c,f) t4 = Triangle(d,b,e) t2.setNeighbors(t3,t4,t1) self.failUnless( t2.neighbors[2].vertices[0] == b, 'Triangle initialisation is wrong!') def testSegment(self): a = Vertex (0.0, 0.0) b = Vertex (0.0, 10.0) s = Segment(a,b, marker = 20) self.failUnless( s.vertices[0].DistanceToPoint(s.vertices[1]) == 10.0, 'vertices in a segment are wrong') self.failUnless( s.marker == 20.0, 'marker in a segment are wrong') def testdeleteUserVertex(self): mesh = Mesh() a = mesh.addUserVertex(0.0, 0.0) b = mesh.addUserVertex (0.0, 2.0) c = mesh.addUserVertex (2.0,0.0) s1 = mesh.addUserSegment(a,b) s2 = mesh.addUserSegment(a,c) s3 = mesh.addUserSegment(c,b) mesh.deleteMeshObject (a) self.failUnless(mesh.userSegments[0] == s3, 'Bad segment. ') self.failUnless(len(mesh.userSegments) ==1, 'Segments not deleted.') self.failUnless(len(mesh.userVertices) == 2, 'Vertex not deleted.') def testgenerateMesh(self): a = Vertex (0.0, 0.0) d = Vertex (0.0, 4.0) f = Vertex (4.0,0.0) s1 = Segment(a,d) s2 = Segment(d,f) s3 = Segment(a,f) r1 = Region(0.3, 0.3,tag = 1.3,maxArea = .6) #print r1 m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3], regions=[r1] ) m.generateMesh("QApz", maxArea = 2.1 ) #print m #m.plotMeshTriangle() result = 1.414214 delta = 0.00001 self.failUnless((m.meshTriangles[1].vertices[0].x < result + delta) or (m.meshTriangles[1].vertices[0].x > result - delta), 'generated mesh is wrong!') def test_regionalMaxArea(self): v0 = Vertex (0.0, 0.0) v1 = Vertex (6.0, 0.0) v2 = Vertex (6.0,6.0) v3 = Vertex (0.0,6.0) s1 = Segment(v0,v1) s2 = Segment(v1,v2) s3 = Segment(v3,v2) s4 = Segment(v3,v0) s5 = Segment(v2,v0) r1 = Region(3, 1,tag = 1.3) #print r1 m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], regions=[r1] ) m.generateMesh("Apz", maxArea = 36 ) #m.plotMeshTriangle() #print "len(m.meshTriangles)",len(m.meshTriangles) self.failUnless(len(m.meshTriangles) == 2, 'test_regionalMaxArea 1:generated mesh is wrong!') ## Another test case r1 = Region(3, 1,tag = 1.3) r2 = Region(1, 3,tag = 1.3, maxArea = 8) m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], regions=[r1,r2] ) m.generateMesh("Apz", maxArea = 36 ) self.failUnless(len(m.meshTriangles) == 6, 'test_regionalMaxArea 2:generated mesh is wrong!') ## Another test case r1 = Region(3, 1, tag = 1.3, maxArea = 8) r2 = Region(1, 3, tag = 1.3, maxArea = 8) m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], regions=[r1,r2] ) m.generateMesh("Apz", maxArea = 36 ) print "len(m.meshTriangles)",len(m.meshTriangles) self.failUnless(len(m.meshTriangles) == 8, 'test_regionalMaxArea 3:generated mesh is wrong!') ## Another test case r1 = Region(3, 1, tag = 1.3 ) r2 = Region(1, 3, tag = 1.3, maxArea = 8) m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], regions=[r1,r2] ) m.generateMesh("Apz", maxArea = 8 ) self.failUnless(len(m.meshTriangles) == 8, 'test_regionalMaxArea 4:generated mesh is wrong!') ## Another test case r1 = Region(3, 1,tag = 1.3, maxArea = 8) r2 = Region(1, 3,tag = 1.3, maxArea = 8) m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], regions=[r1,r2] ) m.generateMesh("Apz", maxArea = 36,isRegionalMaxAreas = False ) self.failUnless(len(m.meshTriangles) == 2, 'test_regionalMaxArea 5:generated mesh is wrong!') def testdeleteUserVertex(self): mesh = Mesh() a = mesh.addUserVertex(0.0, 0.0) b = mesh.addUserVertex (0.0, 2.0) c = mesh.addUserVertex (2.0,0.0) s1 = mesh.addUserSegment(a,b) s2 = mesh.addUserSegment(a,c) s3 = mesh.addUserSegment(c,b) mesh.deleteMeshObject (s2) #print ",s2 in mesh.userSegments" ,s2 in mesh.userSegments self.failUnless(not(s2 in mesh.userSegments), 'Bad segment. ') self.failUnless(len(mesh.userSegments) ==2, 'Segments not deleted.') self.failUnless(len(mesh.userVertices) == 3, 'Vertex deleted, instead of segment.') def testTriangleArea(self): a = Vertex (10.0, 10.0) b = Vertex (10.0, 20.0) c = Vertex (20.0,10.0) d = Vertex (-20.0, 0.0) e = Vertex (-20.0, -20.0) f = Vertex (20.0,-20.0) t1 = Triangle(b,a,c) t2 = Triangle(e,d,f) # print "t1", t1 # print "t1 area ", t1.calcArea() # print "t2", t2 # print "t2 area ", t2.calcArea() self.failUnless( t1.calcArea() == 50 and t2.calcArea() == 400, 'Triangle area is wrong!') def testisUserSegmentNew (self): mesh = Mesh() a = mesh.addUserVertex(0.0, 0.0) b = mesh.addUserVertex (0.0, 2.0) c = mesh.addUserVertex (2.0,0.0) d = mesh.addUserVertex (2.0,3.0) s1 = mesh.addUserSegment(a,b) s2 = mesh.addUserSegment(a,c) s3 = mesh.addUserSegment(c,b) self.failUnless(mesh.isUserSegmentNew(a,d) , 'Segment should be new. ') self.failUnless(not(mesh.isUserSegmentNew(a,b)) , 'Segment should not be new. ') def testautoSegment(self): a = Vertex (0.0, 0.0) d = Vertex (0.0, 4.0) f = Vertex (4.0,4.0) g = Vertex (4.0,0.0) s1 = Segment(a,d) #s2 = Segment(a,f) #s3 = Segment(f,d) m = Mesh(userVertices=[a,d,f,g], userSegments=[s1] ) #print m m.autoSegment() #print m #print 'Len', len(m.userSegments) self.failUnless(len(m.userSegments) == 4 , 'userSegments is wrong!') def testRegions(self): a = Vertex (0.0, 0.0) b = Vertex (0.0, 2.0) c = Vertex (2.0,0.0) d = Vertex (0.0, 4.0) e = Vertex (2.0, 2.0) f = Vertex (4.0,0.0) g = Vertex (0.0,-2.0) Segment.set_default_tag("") s1 = Segment(a,b) s2 = Segment(b,e) s3 = Segment(e,c) s4 = Segment(c,a) s5 = Segment(b,d) s6 = Segment(e,d) s7 = Segment(e,f) s8 = Segment(c,f) s9 = Segment(g,c) s10 = Segment(g,a) r1 = Region(0.1,0.1,tag="22") r2 = Region(0.1,2.1,tag="11") r3 = Region(2.1,0.1) 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] ) m.generateMesh("QApz", maxArea = 2.1 ) #print m Triangulation = m.getTriangulation() #print Triangulation[0].attribute #print Triangulation[1].attribute #print Triangulation[2].attribute #print Triangulation[3].attribute #print Triangulation[4].attribute self.failUnless(Triangulation[0].attribute == "" and Triangulation[1].attribute == "22" and Triangulation[2].attribute == "" and Triangulation[3].attribute == "11" and Triangulation[4].attribute == "22" , 'region attributes are wrong!') def test_vertexAttribs(self): a = Vertex (0.0, 0.0, attributes = [12.0,2.0]) d = Vertex (0.0, 4.0, attributes = [9.0,7.0]) f = Vertex (4.0,0.0, attributes = [14.0,3.0]) Segment.set_default_tag("") s1 = Segment(a,d) s2 = Segment(d,f) s3 = Segment(a,f) r1 = Region(0.3, 0.3, tag = 88.9) m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3], regions=[r1]) m.generateMesh("QApz", maxArea = 2.1) vert = m.getMeshVertices() self.failUnless(vert[0].attributes == [12.0, 2.0] and vert[1].attributes == [9.0, 7.0] and vert[2].attributes == [14.0,3.0] and vert[3].attributes == [12.232233047033631, 4.4142135623730949] and vert[4].attributes == [13.0, 2.5] , 'vertex attributes are wrong!') def test_vertexAttribs2(self): a = Vertex (0.0, 0.0) d = Vertex (0.0, 4.0) f = Vertex (4.0,0.0) Segment.set_default_tag("") s1 = Segment(a,d) s2 = Segment(d,f) s3 = Segment(a,f) r1 = Region(0.3, 0.3, tag = 88.9) m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3], regions=[r1]) m.generateMesh("QApz", maxArea = 2.1 ) vert = m.getMeshVertices() self.failUnless(vert[0].attributes == [] and vert[1].attributes == [] and vert[2].attributes == [] and vert[3].attributes == [] and vert[4].attributes == [], 'vertex attributes are wrong!') def test_segmarker(self): a = Vertex (0.0, 0.0) d = Vertex (0.0, 4.0) f = Vertex (4.0,0.0) s1 = Segment(a,d,marker = 5) s2 = Segment(d,f,marker = 7) s3 = Segment(a,f,marker = 9) m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3]) m.generateMesh("QApz", maxArea = 2.1 ) seg = m.getMeshSegments() #print "seg[0].marker" #print seg[0].marker #print "seg[0].marker" self.failUnless(seg[0].marker == 5 and seg[1].marker == 7 and seg[2].marker == 9 and seg[3].marker == 7 and seg[4].marker == 9 and seg[5].marker == 7 and seg[6].marker == 5, 'seg markers are wrong') def test_segmarker2(self): a = Vertex (0.0, 0.0) d = Vertex (0.0, 4.0) f = Vertex (4.0,0.0) e = Vertex (1.0,1.0) s1 = Segment(a,d) s2 = Segment(d,f) s3 = Segment(a,f) s4 = Segment(a,e) m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4]) m.generateMesh("QApz", maxArea = 2.1) seg = m.getMeshSegments() self.failUnless(seg[0].marker == "external" and seg[1].marker == "external" and seg[2].marker == "external" and seg[3].marker == "" and seg[4].marker == "external", '2nd seg markers are wrong') def test_asciiFile(self): a = Vertex (0.0, 0.0) #, attributes = [1.1]) d = Vertex (0.0, 4.0) #, attributes = [1.2]) f = Vertex (4.0,0.0) #, attributes = [1.3]) e = Vertex (1.0,1.0) #, attributes = [1.4]) s1 = Segment(a,d) s2 = Segment(d,f) s3 = Segment(a,f) s4 = Segment(a,e) m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4]) m.generateMesh("QApz", maxArea = 2.1 ) seg = m.getMeshSegments() fileName = tempfile.mktemp(".txt") m.exportASCIItrianglulationfile(fileName) file = open(fileName) lFile = file.read().split('\n') file.close() os.remove(fileName) #print "@^@^" #for l in lFile: # print l,"<" #print "@^@^" self.failUnless(lFile[0] == "5 0 # [attributes] ...Triangulation Vertices..." , 'Ascii file is wrong, vertex title') self.failUnless(lFile[1] == "0 0.0 0.0 " and #1.1 " and lFile[2] == "1 0.0 4.0 " and #1.2 " and lFile[3] == "2 4.0 0.0 " and #1.3 " and lFile[4] == "3 1.0 1.0 " and #1.4 " and lFile[5] == "4 2.0 2.0 " #1.25 " , 'Ascii file is wrong, vertex') self.failUnless(lFile[6] == "# attribute column titles ...Triangulation Vertex Titles..." , 'Ascii file is wrong, attribute column title') self.failUnless(lFile[7] == "4 # [] [] [attribute of region] ...Triangulation Triangles..." and lFile[8] == "0 3 2 4 -1 2 3 " and lFile[9] == "1 1 0 3 3 2 -1 " and lFile[10] == "2 3 4 1 -1 1 0 " and lFile[11] == "3 2 3 0 1 -1 0 " , 'Ascii file is wrong, triangle') self.failUnless(lFile[12] == "5 # [boundary marker] ...Triangulation Segments..." and lFile[13] == "0 0 1 external" and lFile[14] == "1 1 4 external" and lFile[15] == "2 2 0 external" and lFile[16] == "3 0 3 " and lFile[17] == "4 4 2 external" , 'Ascii file is wrong, segment') self.failUnless(lFile[18] == '4 0 # [attributes] ...Mesh Vertices...', 'Ascii file is wrong, Mesh Vertices Title') self.failUnless(lFile[19] == '0 0.0 0.0 ' and #1.1 ' and lFile[20] == '1 0.0 4.0 ' and #1.2 ' and lFile[21] == '2 4.0 0.0 ' and #1.3 ' and lFile[22] == '3 1.0 1.0 ' #1.4 ' , 'Ascii file is wrong, Mesh Vertices II') self.failUnless(lFile[23] == '4 # [boundary marker] ...Mesh Segments...' and lFile[24] == '0 0 1 ' and lFile[25] == '1 1 2 ' and lFile[26] == '2 0 2 ' and lFile[27] == '3 0 3 ' and lFile[28] == '0 # ...Mesh Holes...' and lFile[29] == '0 # ...Mesh Regions...' , 'Ascii file is wrong, Mesh Segments') def test_ascii_file(self): a = Vertex (0.0, 0.0) #, attributes = [1.1]) d = Vertex (0.0, 4.0) #, attributes = [1.2]) f = Vertex (4.0,0.0) #, attributes = [1.3]) e = Vertex (1.0,1.0) #, attributes = [1.4]) s1 = Segment(a,d) s2 = Segment(d,f) s3 = Segment(a,f) s4 = Segment(a,e) m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4]) m.generateMesh("QApz", maxArea = 2.1 ) seg = m.getMeshSegments() fileName = tempfile.mktemp(".txt") m.exportASCIItrianglulationfile(fileName) file = open(fileName) lFile = file.read().split('\n') file.close() os.remove(fileName) #print "@^@^" #for l in lFile: # print l,"<" #print "@^@^" self.failUnless(lFile[0] == "5 0 # [attributes] ...Triangulation Vertices..." , 'Ascii file is wrong, vertex title') self.failUnless(lFile[1] == "0 0.0 0.0 " and #1.1 " and lFile[2] == "1 0.0 4.0 " and #1.2 " and lFile[3] == "2 4.0 0.0 " and #1.3 " and lFile[4] == "3 1.0 1.0 " and #1.4 " and lFile[5] == "4 2.0 2.0 " #1.25 " , 'Ascii file is wrong, vertex') self.failUnless(lFile[6] == "# attribute column titles ...Triangulation Vertex Titles..." , 'Ascii file is wrong, attribute column title') self.failUnless(lFile[7] == "4 # [] [] [attribute of region] ...Triangulation Triangles..." and lFile[8] == "0 3 2 4 -1 2 3 " and lFile[9] == "1 1 0 3 3 2 -1 " and lFile[10] == "2 3 4 1 -1 1 0 " and lFile[11] == "3 2 3 0 1 -1 0 " , 'Ascii file is wrong, triangle') self.failUnless(lFile[12] == "5 # [boundary marker] ...Triangulation Segments..." and lFile[13] == "0 0 1 external" and lFile[14] == "1 1 4 external" and lFile[15] == "2 2 0 external" and lFile[16] == "3 0 3 " and lFile[17] == "4 4 2 external" , 'Ascii file is wrong, segment') self.failUnless(lFile[18] == '4 0 # [attributes] ...Mesh Vertices...', 'Ascii file is wrong, Mesh Vertices Title') self.failUnless(lFile[19] == '0 0.0 0.0 ' and #1.1 ' and lFile[20] == '1 0.0 4.0 ' and #1.2 ' and lFile[21] == '2 4.0 0.0 ' and #1.3 ' and lFile[22] == '3 1.0 1.0 ' #1.4 ' , 'Ascii file is wrong, Mesh Vertices II') self.failUnless(lFile[23] == '4 # [boundary marker] ...Mesh Segments...' and lFile[24] == '0 0 1 ' and lFile[25] == '1 1 2 ' and lFile[26] == '2 0 2 ' and lFile[27] == '3 0 3 ' and lFile[28] == '0 # ...Mesh Holes...' and lFile[29] == '0 # ...Mesh Regions...' , 'Ascii file is wrong, Mesh Segments') def test_thinoutVertices(self): v1 = Vertex(-20,-20) v2 = Vertex(-11,-11) v3 = Vertex(-10,-10) v4 = Vertex(-9,-1) v5 = Vertex(-8,2) v6 = Vertex(6,3) v7 = Vertex(12,9) v8 = Vertex(15,3) v9 = Vertex(24,3) m = Mesh(userVertices = [v1,v2,v3,v4,v5,v6,v7,v8,v9]) m.thinoutVertices(10) self.failUnless(v1 in m.userVertices, 'test_thinoutVertices, test 1 failed') self.failUnless(v3 in m.userVertices, 'test_thinoutVertices, test 2 failed') self.failUnless(v4 in m.userVertices, 'test_thinoutVertices, test 3 failed') self.failUnless(v6 in m.userVertices, 'test_thinoutVertices, test 4 failed') self.failUnless(v7 in m.userVertices, 'test_thinoutVertices, test 5 failed') self.failUnless(v9 in m.userVertices, 'test_thinoutVertices, test 6 failed') self.failUnless(v5 not in m.userVertices, 'test_thinoutVertices, test 7 failed') self.failUnless(v2 not in m.userVertices, 'test_thinoutVertices, test 8 failed') self.failUnless(v8 not in m.userVertices, 'test_thinoutVertices, test 9 failed') def test_same_x_y(self): v = Point(7,8) f = Point(7,8) f.same_x_y(v) self.failUnless(f.same_x_y(v), 'same_x_y True failed') e = Point(7,9) self.failUnless(not f.same_x_y(e), 'same_x_y False failed') def test_import_mesh(self): a_att = [5.0,2.0] d_att =[4.0,2.0] f_att = [3.0,2.0] e_att = [2.0,2.0] a_xy = [0.0, 0.0] a = Vertex ( a_xy[0],a_xy[1]) #, attributes =a_att) d = Vertex (0.0, 4.0) #, attributes =d_att) f = Vertex (4.0,0.0) #, attributes =f_att) e = Vertex (1.0,1.0) #, attributes =e_att) s1 = Segment(a,d, marker = "50") s2 = Segment(d,f, marker = "40") s3 = Segment(a,f, marker = "30") s4 = Segment(a,e, marker = "20") r1 = Region(0.3, 0.3,tag = "1.3") m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4], regions=[r1]) m.generateMesh("QApz", maxArea = 2.1) fileName = tempfile.mktemp(".tsh") #print "dgs!!!" #print "****************** fileName", fileName m.exportASCIItrianglulationfile(fileName) #print "******************" #print "m", m #print "******************" m_returned = importMeshFromFile(fileName) #print "m_returned",m_returned #print "******************" #print "****************** fileName", fileName os.remove(fileName) self.failUnless(0 == m.__cmp__(m_returned), 'loading and saving of a mesh failed') def test_normaliseMesh(self): a_att = [5.0,2.0] d_att =[4.0,2.0] f_att = [3.0,2.0] e_att = [2.0,2.0] a_xy = [10.0, 10.0] a = Vertex ( a_xy[0],a_xy[1], attributes =a_att) d = Vertex (15.0, 10.0, attributes =d_att) f = Vertex (10.0,20.0, attributes =f_att) e = Vertex (15.0,20.0, attributes =e_att) s1 = Segment(a,d, marker = 50) s2 = Segment(d,e, marker = 40) s3 = Segment(e,f, marker = 30) s4 = Segment(f,a, marker = 20) r1 = Region(0.3, 0.3,tag = 1.3) m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4], regions=[r1]) m.normaliseMesh(1,0,1) [xmin, ymin, xmax, ymax] = m.boxsize() [attmin, attmax] = m.maxMinVertAtt(0) self.failUnless(attmin == 0.0 and attmax == 1.0, 'normalise failed') self.failUnless(xmin == 0.0 and ymin == 0.0 and xmax == 0.5 and ymax == 1.0, 'normalise failed') m.normaliseMesh(200,-100,5) [xmin, ymin, xmax, ymax] = m.boxsize() [attmin, attmax] = m.maxMinVertAtt(0) self.failUnless(attmin == 0.0 and attmax == 5.0, 'normalise failed') self.failUnless(xmin == -100.0 and ymin == -100.0 and xmax == 0.0 and ymax == 100.0, 'normalise failed') def test_exportASCIIsegmentoutlinefile(self): a = Vertex (0,0) b = Vertex (0,3) c = Vertex (3,3) d = Vertex (1,2) e = Vertex (3,1) s1 = Segment(a,b, marker = "50") s2 = Segment(b,c, marker = "40") s3 = Segment(c,a, marker = "30") r1 = Region(2, 1,tag = "1.3") h1 = Hole(1,4) m = Mesh(userVertices=[a,b,c,d,e], userSegments=[s1,s2,s3], regions=[r1], holes = [h1]) m.generateMesh("QApz", maxArea = 2.1) #print "mesh ***************dsg*", m fileName = tempfile.mktemp(".tsh") m.exportASCIIsegmentoutlinefile(fileName) m_returned = importMeshFromFile(fileName) #print "m_returned ****",m_returned #print "****************** fileName", fileName os.remove(fileName) #Trim mesh, so it should like like m_returned m.meshVertices = [] m.meshTriangles = [] m.meshSegments = [] m.userVertices=[a,b,c] #print "(m.__cmp__(m_returned)", m.__cmp__(m_returned) self.failUnless(0 == m.__cmp__(m), 'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed') self.failUnless(0 == m.__cmp__(m_returned), 'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed') def test_exportxyafile(self): a = Vertex (0,0) b = Vertex (0,3) c = Vertex (3,3) d = Vertex (1,2) e = Vertex (3,1) f = Vertex (3,1) s1 = Segment(a,b, marker = 50) s2 = Segment(b,c, marker = 40) s3 = Segment(c,a, marker = 30) r1 = Region(2, 1,tag = 1.3) h1 = Hole(1,4) # Warning mesh can't produce this type of data structure its self m = Mesh(userVertices=[a,b,c,d,e], userSegments=[s1,s2,s3], regions=[r1], holes = [h1]) fileName = tempfile.mktemp(".txt") m.exportxyafile(fileName) file = open(fileName) lFile = file.read().split('\n') file.close() #print "**********************" #print lFile #print "**********************" os.remove(fileName) self.failUnless(lFile[0] == "5 0 # [attributes]" and lFile[1] == "0 0 " and lFile[2] == "0 3 " and lFile[3] == "3 3 " and lFile[4] == "1 2 " and lFile[5] == "3 1 " , 'exported Ascii xya file is wrong') m.generateMesh("QApz", maxArea = 2.1) fileName = tempfile.mktemp(".txt") m.exportxyafile(fileName) file = open(fileName) lFile = file.read().split('\n') file.close() os.remove(fileName) self.failUnless(lFile[0] == "6 0 # [attributes]" and lFile[1] == "0.0 0.0 " and lFile[2] == "0.0 3.0 " and lFile[3] == "3.0 3.0 " and lFile[4] == "1.0 2.0 " and lFile[5] == "3.0 1.0 " and lFile[6] == "1.5 1.5 " , 'exported Ascii xya file is wrong') def test_strings2ints(self): list = ["sea","river inlet","","sea","","moat"] preset = ["moat", "internal boundary"] [intlist, converter] = segment_strings2ints(list,preset ) self.failUnless(intlist == [2,3 ,0 ,2 ,0 ,0 ] , 'test_strings2ints produces bad intlist') self.failUnless(converter == ['moat', 'internal boundary', 'sea', 'river inlet'] , 'test_strings2ints produces bad converter') def test_ints2strings(self): list = ["internal boundary","sea","river inlet", "","sea","","moat","internal boundary"] outlist = ['internal boundary', 'sea', 'river inlet', 'moat', 'sea', 'moat', 'moat', 'internal boundary'] preset = ["moat", "internal boundary"] [intlist, converter] = segment_strings2ints(list,preset ) newlist = segment_ints2strings(intlist, converter) self.failUnless(outlist == newlist , 'test_strings2ints produces bad intlist') self.failUnless(converter == ['moat', 'internal boundary', 'sea', 'river inlet'] , 'test_strings2ints produces bad converter') def test_ints2strings2(self): list = ["","",""] preset = ["moat", "internal boundary"] [intlist, converter] = segment_strings2ints(list,preset ) newlist = segment_ints2strings(intlist, converter) outlist = ['moat', 'moat', 'moat'] self.failUnless(outlist == newlist , 'test_strings2ints produces bad intlist') self.failUnless(converter == ['moat', 'internal boundary'] , 'test_strings2ints produces bad converter') def test_removeDuplicatedVertices(self): a = Vertex (0,0) a.index = 0 b = Vertex (0,3) b.index = 1 c = Vertex (3,3) c.index = 2 d = Vertex (1,1) d.index = 3 e = Vertex (3,1) e.index = 4 f = Vertex (1,1) f.index = 5 g = Vertex (1,1) g.index = 6 inputVerts_noDups = [a,b,c,d,e] m = Mesh(userVertices=[a,b,c,d,e,f,g]) counter = m.removeDuplicatedUserVertices() UserVerts = m.getUserVertices() self.failUnless(UserVerts == inputVerts_noDups, 'duplicate verts not removed') #for userVert, inputVert in map(None, UserVerts, inputVerts_noDups): # self.failUnless(userVert.x == inputVert.x, # 'x duplicate verts not removed') # self.failUnless(userVert.y == inputVert.y, # 'y duplicate verts not removed') def test_ungenerateFileLoading(self): fileName = tempfile.mktemp(".txt") file = open(fileName,"w") file.write(" 1 ?? ??\n\ 0.0 0.0\n\ 1.0 0.0\n\ 1.0 1.0\n\ 0.0 1.0\n\ 0.0 0.0\n\ END\n\ 2 ?? ??\n\ 10.0 10.0\n\ 10.0 20.0\n\ 20.0 20.0\n\ 10.0 10.0\n\ END\n\ END\n") file.close() a = Vertex (0.0, 0.0) #, attributes = [1.1]) b = Vertex (0.0, 40.0) #, attributes = [1.2]) c = Vertex (40.0,40.0) #, attributes = [1.3]) d = Vertex (40.0,0.0) #, attributes = [1.4]) s1 = Segment(a,b) s2 = Segment(b,c) s3 = Segment(c,d) s4 = Segment(d,a) m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4]) dict = importUngenerateFile(fileName) tag = "DSG" Segment.set_default_tag(tag) m.addVertsSegs(dict) # have to reset this , since it's a class attribute Segment.set_default_tag("") self.failUnless(len(m.userSegments) ==11, 'Wrong segment list length.') self.failUnless(len(m.userVertices) == 11, 'Wrong vertex list length.') self.failUnless(m.userSegments[10].vertices[0] == m.userVertices[10], 'bad vertex on segment.') self.failUnless(m.userSegments[10].vertices[1] == m.userVertices[8], 'Bad segment.') self.failUnless(m.userSegments[10].marker == tag, 'wrong tag.') def test_ungenerateFileLoadingII(self): fileName = tempfile.mktemp(".txt") file = open(fileName,"w") file.write(" 1 ?? ??\n\ 0.0 0.0\n\ 1.0 0.0\n\ 1.0 1.0\n\ 0.0 1.0\n\ 0.0 0.0\n\ END\n\ 2 ?? ??\n\ 10.0 10.0\n\ 10.0 20.0\n\ 20.0 20.0\n\ END\n\ END\n") file.close() a = Vertex (0.0, 0.0) #, attributes = [1.1]) b = Vertex (0.0, 40.0) #, attributes = [1.2]) c = Vertex (40.0,40.0) #, attributes = [1.3]) d = Vertex (40.0,0.0) #, attributes = [1.4]) s1 = Segment(a,b) s2 = Segment(b,c) s3 = Segment(c,d) s4 = Segment(d,a) m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4]) dict = importUngenerateFile(fileName) tag = "DSG" Segment.set_default_tag(tag) m.addVertsSegs(dict) # have to reset this , since it's a class attribute Segment.set_default_tag("") self.failUnless(len(m.userSegments) ==10, 'Wrong segment list length.') self.failUnless(len(m.userVertices) == 11, 'Wrong vertex list length.') #------------------------------------------------------------- if __name__ == "__main__": suite = unittest.makeSuite(meshTestCase,'test') runner = unittest.TextTestRunner() runner.run(suite)