1
"""Unit tests for the mesh library"""
3
__author__ = "Anders Logg (logg@simula.no)"
4
__date__ = "2006-08-08 -- 2007-05-23"
5
__copyright__ = "Copyright (C) 2006 Anders Logg"
6
__license__ = "GNU LGPL Version 2.1"
11
class SimpleShapes(unittest.TestCase):
13
def testUnitSquare(self):
14
"""Create mesh of unit square"""
15
mesh = UnitSquare(5, 7)
16
self.assertEqual(mesh.numVertices(), 48)
17
self.assertEqual(mesh.numCells(), 70)
19
def testUnitCube(self):
20
"""Create mesh of unit cube"""
21
mesh = UnitCube(5, 7, 9)
22
self.assertEqual(mesh.numVertices(), 480)
23
self.assertEqual(mesh.numCells(), 1890)
25
class MeshRefinement(unittest.TestCase):
27
def testRefineUnitSquare(self):
28
"""Refine mesh of unit square"""
29
mesh = UnitSquare(5, 7)
31
self.assertEqual(mesh.numVertices(), 165)
32
self.assertEqual(mesh.numCells(), 280)
34
def testRefineUnitCube(self):
35
"""Refine mesh of unit cube"""
36
mesh = UnitCube(5, 7, 9)
38
self.assertEqual(mesh.numVertices(), 3135)
39
self.assertEqual(mesh.numCells(), 15120)
41
class MeshIterators(unittest.TestCase):
43
def testVertexIterators(self):
44
"""Iterate over vertices"""
45
mesh = UnitCube(5, 5, 5)
47
for v in vertices(mesh):
49
self.assertEqual(n, mesh.numVertices())
51
def testEdgeIterators(self):
52
"""Iterate over edges"""
53
mesh = UnitCube(5, 5, 5)
57
self.assertEqual(n, mesh.numEdges())
59
def testFaceIterators(self):
60
"""Iterate over faces"""
61
mesh = UnitCube(5, 5, 5)
65
self.assertEqual(n, mesh.numFaces())
67
def testFacetIterators(self):
68
"""Iterate over facets"""
69
mesh = UnitCube(5, 5, 5)
71
for f in facets(mesh):
73
self.assertEqual(n, mesh.numFacets())
75
def testCellIterators(self):
76
"""Iterate over cells"""
77
mesh = UnitCube(5, 5, 5)
81
self.assertEqual(n, mesh.numCells())
83
def testMixedIterators(self):
84
"""Iterate over vertices of cells"""
85
mesh = UnitCube(5, 5, 5)
90
self.assertEqual(n, 4*mesh.numCells())
92
class BoundaryExtraction(unittest.TestCase):
94
def testBoundaryComputation(self):
95
"""Compute boundary of mesh"""
96
mesh = UnitCube(2, 2, 2)
97
boundary = BoundaryMesh(mesh)
98
self.assertEqual(boundary.numVertices(), 26)
99
self.assertEqual(boundary.numCells(), 48)
101
def testBoundaryBoundary(self):
102
"""Compute boundary of boundary"""
103
mesh = UnitCube(2, 2, 2)
104
b0 = BoundaryMesh(mesh)
105
b1 = BoundaryMesh(b0)
106
self.assertEqual(b1.numVertices(), 0)
107
self.assertEqual(b1.numCells(), 0)
109
class MeshFunctions(unittest.TestCase):
111
def testAssign(self):
112
"""Assign value of mesh function"""
113
mesh = UnitSquare(3, 3)
114
f = MeshFunction('int', mesh, 0)
117
self.assertEqual(f(v), 10)
119
class InputOutput(unittest.TestCase):
121
def testMeshXML2D(self):
122
"""Write and read 2D mesh to/from file"""
123
mesh_out = UnitSquare(3, 3)
125
file = File("unitsquare.xml")
128
self.assertEqual(mesh_in.numVertices(), 16)
130
def testMeshXML3D(self):
131
"""Write and read 3D mesh to/from file"""
132
mesh_out = UnitCube(3, 3, 3)
134
file = File("unitcube.xml")
137
self.assertEqual(mesh_in.numVertices(), 64)
139
def testMeshMatlab2D(self):
140
"""Write matlab format (no real test)"""
141
mesh = UnitSquare(5, 5)
142
file = File("unitsquare.m")
144
self.assertEqual(0, 0)
146
def testMeshFunction(self):
147
"""Write and read mesh function to/from file"""
148
mesh = UnitSquare(1, 1)
149
f = MeshFunction('int', mesh, 0)
154
file = File("meshfunction.xml")
156
g = MeshFunction('int', mesh, 0)
158
for v in vertices(mesh):
159
self.assertEqual(f(v), g(v))
161
class PyCCInterface(unittest.TestCase):
163
def testGetGeometricalDimension(self):
164
"""Get geometrical dimension of mesh"""
165
mesh = UnitSquare(5, 5)
166
self.assertEqual(mesh.geometry().dim(), 2)
168
def testGetCoordinates(self):
169
"""Get coordinates of vertices"""
170
mesh = UnitSquare(5, 5)
171
self.assertEqual(len(mesh.coordinates()), 36)
173
def testGetCells(self):
174
"""Get cells of mesh"""
175
mesh = UnitSquare(5, 5)
176
self.assertEqual(len(mesh.cells()), 50)
178
if __name__ == "__main__":