~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to test/mesh/python/test.py

  • Committer: Johannes Ring
  • Date: 2008-03-05 22:43:06 UTC
  • Revision ID: johannr@simula.no-20080305224306-2npsdyhfdpl2esji
The BIG commit!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Unit tests for the mesh library"""
 
2
 
 
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"
 
7
 
 
8
import unittest
 
9
from dolfin import *
 
10
 
 
11
class SimpleShapes(unittest.TestCase):
 
12
 
 
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)
 
18
 
 
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)
 
24
 
 
25
class MeshRefinement(unittest.TestCase):
 
26
 
 
27
    def testRefineUnitSquare(self):
 
28
        """Refine mesh of unit square"""
 
29
        mesh = UnitSquare(5, 7)
 
30
        mesh.refine()
 
31
        self.assertEqual(mesh.numVertices(), 165)
 
32
        self.assertEqual(mesh.numCells(), 280)
 
33
 
 
34
    def testRefineUnitCube(self):
 
35
        """Refine mesh of unit cube"""
 
36
        mesh = UnitCube(5, 7, 9)
 
37
        mesh.refine()
 
38
        self.assertEqual(mesh.numVertices(), 3135)
 
39
        self.assertEqual(mesh.numCells(), 15120)
 
40
 
 
41
class MeshIterators(unittest.TestCase):
 
42
 
 
43
    def testVertexIterators(self):
 
44
        """Iterate over vertices"""
 
45
        mesh = UnitCube(5, 5, 5)
 
46
        n = 0
 
47
        for v in vertices(mesh):
 
48
            n += 1
 
49
        self.assertEqual(n, mesh.numVertices())
 
50
 
 
51
    def testEdgeIterators(self):
 
52
        """Iterate over edges"""
 
53
        mesh = UnitCube(5, 5, 5)
 
54
        n = 0
 
55
        for e in edges(mesh):
 
56
            n += 1
 
57
        self.assertEqual(n, mesh.numEdges())
 
58
 
 
59
    def testFaceIterators(self):
 
60
        """Iterate over faces"""
 
61
        mesh = UnitCube(5, 5, 5)
 
62
        n = 0
 
63
        for f in faces(mesh):
 
64
            n += 1
 
65
        self.assertEqual(n, mesh.numFaces())
 
66
 
 
67
    def testFacetIterators(self):
 
68
        """Iterate over facets"""
 
69
        mesh = UnitCube(5, 5, 5)
 
70
        n = 0
 
71
        for f in facets(mesh):
 
72
            n += 1
 
73
        self.assertEqual(n, mesh.numFacets())
 
74
 
 
75
    def testCellIterators(self):
 
76
        """Iterate over cells"""
 
77
        mesh = UnitCube(5, 5, 5)
 
78
        n = 0
 
79
        for c in cells(mesh):
 
80
            n += 1
 
81
        self.assertEqual(n, mesh.numCells())
 
82
        
 
83
    def testMixedIterators(self):
 
84
        """Iterate over vertices of cells"""
 
85
        mesh = UnitCube(5, 5, 5)
 
86
        n = 0
 
87
        for c in cells(mesh):
 
88
            for v in vertices(c):
 
89
                n += 1
 
90
        self.assertEqual(n, 4*mesh.numCells())
 
91
 
 
92
class BoundaryExtraction(unittest.TestCase):
 
93
 
 
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)
 
100
 
 
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)
 
108
 
 
109
class MeshFunctions(unittest.TestCase):
 
110
 
 
111
     def testAssign(self):
 
112
         """Assign value of mesh function"""
 
113
         mesh = UnitSquare(3, 3)
 
114
         f = MeshFunction('int', mesh, 0)
 
115
         f.set(3, 10)
 
116
         v = Vertex(mesh, 3)
 
117
         self.assertEqual(f(v), 10)
 
118
      
 
119
class InputOutput(unittest.TestCase):
 
120
 
 
121
     def testMeshXML2D(self):
 
122
         """Write and read 2D mesh to/from file"""
 
123
         mesh_out = UnitSquare(3, 3)
 
124
         mesh_in  = Mesh()
 
125
         file = File("unitsquare.xml")
 
126
         file << mesh_out
 
127
         file >> mesh_in
 
128
         self.assertEqual(mesh_in.numVertices(), 16)
 
129
 
 
130
     def testMeshXML3D(self):
 
131
         """Write and read 3D mesh to/from file"""
 
132
         mesh_out = UnitCube(3, 3, 3)
 
133
         mesh_in  = Mesh()
 
134
         file = File("unitcube.xml")
 
135
         file << mesh_out
 
136
         file >> mesh_in
 
137
         self.assertEqual(mesh_in.numVertices(), 64)
 
138
 
 
139
     def testMeshMatlab2D(self):
 
140
         """Write matlab format (no real test)"""
 
141
         mesh = UnitSquare(5, 5)
 
142
         file = File("unitsquare.m")
 
143
         file << mesh
 
144
         self.assertEqual(0, 0)
 
145
 
 
146
     def testMeshFunction(self):
 
147
         """Write and read mesh function to/from file"""
 
148
         mesh = UnitSquare(1, 1)
 
149
         f = MeshFunction('int', mesh, 0)
 
150
         f.set(0, 2)
 
151
         f.set(1, 4)
 
152
         f.set(2, 6)
 
153
         f.set(3, 8)
 
154
         file = File("meshfunction.xml")
 
155
         file << f
 
156
         g = MeshFunction('int', mesh, 0)
 
157
         file >> g
 
158
         for v in vertices(mesh):
 
159
             self.assertEqual(f(v), g(v))
 
160
 
 
161
class PyCCInterface(unittest.TestCase):
 
162
 
 
163
     def testGetGeometricalDimension(self):
 
164
         """Get geometrical dimension of mesh"""
 
165
         mesh = UnitSquare(5, 5)
 
166
         self.assertEqual(mesh.geometry().dim(), 2)
 
167
 
 
168
     def testGetCoordinates(self):
 
169
         """Get coordinates of vertices"""
 
170
         mesh = UnitSquare(5, 5)
 
171
         self.assertEqual(len(mesh.coordinates()), 36)
 
172
 
 
173
     def testGetCells(self):
 
174
         """Get cells of mesh"""
 
175
         mesh = UnitSquare(5, 5)
 
176
         self.assertEqual(len(mesh.cells()), 50)
 
177
 
 
178
if __name__ == "__main__":
 
179
    unittest.main()