source: trunk/anuga_core/source/anuga/pmesh/test_meshquad.py @ 7753

Last change on this file since 7753 was 7751, checked in by hudson, 14 years ago

Refactorings from May ANUGA meeting.

File size: 4.7 KB
Line 
1import unittest
2import numpy as num
3
4from aabb import AABB
5from quad import Cell
6from mesh_quadtree import MeshQuadtree
7from anuga.abstract_2d_finite_volumes.general_mesh import General_mesh as Mesh
8
9import types, sys
10
11#-------------------------------------------------------------
12
13class Test_Quad(unittest.TestCase):
14
15    def setUp(self):
16        pass
17
18    def tearDown(self):
19        pass
20
21    def test_build_quadtree(self):
22
23        a = [3, 7]
24        b = [5, 7]
25        c = [5, 5]
26        d = [7, 7]
27        e = [15, 15]
28        f = [15, 30]
29        g = [30, 10]
30        h = [30, 30]
31
32        points = [a, b, c, d, e, f, g, h]
33       
34        #bac, bce, ecf, dbe, daf, dae
35        vertices = [[1,0,2], [1,3,4], [1,2,3], [5,4,7], [4,6,7]]
36
37        mesh = Mesh(points, vertices)
38   
39        Q = MeshQuadtree(mesh)
40        #Q.show()
41        #print Q.count()
42        self.assertEqual(Q.count(), len(vertices))
43
44        # test a point that falls within a triangle
45        result = Q.search([10, 10])
46        assert type(result) in [types.ListType,types.TupleType],\
47                            'should be a list'
48                           
49        self.assertEqual(result[0][0][0], 1)
50
51
52    def test_build_quadtreeII(self):
53
54        self.cell = Cell(AABB(100, 140, 0, 40), 'cell')
55
56        p0 = [34.6292076111,-7999.92529297]
57        p1 = [8000.0, 7999.0]
58        p2 = [-7999.96630859, 7999.0]
59        p3 = [34, 7999.97021484]
60
61        points = [p0,p1,p2, p3]
62        #bac, bce, ecf, dbe, daf, dae
63        vertices = [[0,1,2],[0,2,3]]
64
65        mesh = Mesh(points, vertices)
66
67        #This was causing round off error
68        Q = MeshQuadtree(mesh)
69       
70    def NOtest_interpolate_one_point_many_triangles(self):
71        # this test has 10 triangles that share the same vert.
72        # If the number of points per cell in  a quad tree is less
73        # than 10 it should crash
74        z0 = [2.0, 5.0]
75        z1 = [2.0, 5.0]
76        z2 = [2.0, 5.0]
77        z3 = [2.0, 5.0]
78        z4 = [2.0, 5.0]
79        z5 = [2.0, 5.0]
80        z6 = [2.0, 5.0]
81        z7 = [2.0, 5.0]
82        z8 = [2.0, 5.0]
83        z9 = [2.0, 5.0]
84        z10 = [2.0, 5.0]
85       
86        v0 = [0.0, 0.0]
87        v1 = [1.0, 0.0]
88        v2 = [2.0, 0.0]
89        v3 = [3.0, 0.0]
90        v4 = [4.0, 0.0]
91        v5 = [0.0, 10.0]
92        v6 = [1.0, 10.0]
93        v7 = [2.0, 10.0]
94        v8 = [3.0, 10.0]
95        v9 = [4.0, 10.0]
96
97        vertices = [z0,v0, v1, v2, v3,v4 ,v5, v6, v7, v8, v9,
98                    z1, z2, z3, z4, z5, z6, z7, z8, z9]
99        triangles = [
100                      [11,1,2],
101                      [12,2,3],
102                      [13,3,4],
103                      [14,4,5],
104                      [7,6,15],
105                      [8,7,16],
106                      [9,8,17],
107                      [10,9,18],
108                      [6,1,19],
109                      [5,10,0]
110                      ]
111       
112        mesh = Mesh(vertices, triangles)
113        try:
114            Q = MeshQuadtree(mesh, max_points_per_cell = 9)
115        except RuntimeError:
116            pass
117        else:
118            self.failUnless(0 ==1,  'many verts at the same position no  \
119            longer causes as error')
120   
121    def test_retrieve_triangles(self):
122
123        cell = Cell(AABB(0, 6, 0, 6), 'cell')
124
125        p0 = [2,1]
126        p1 = [4,1]
127        p2 = [4.,4]
128        p3 = [2,4]
129        p4 = [5,4]
130
131        points = [p0,p1,p2, p3, p4]
132        #
133        vertices = [[0,1,2],[0,2,3],[1,4,2]]
134
135        mesh = Mesh(points, vertices)
136
137        Q = MeshQuadtree(mesh)
138        results = Q.search([4.5, 3])
139        assert len(results) == 1
140        self.assertEqual(results[0][0][0], 2)
141        results = Q.search([5,4.])
142        self.assertEqual(len(results),1)
143        self.assertEqual(results[0][0][0], 2)
144       
145    def NOtest_num_visits(self):
146        """ Test optimisation code.
147        """
148        a = [3, 7]
149        b = [5, 7]
150        c = [5, 5]
151        d = [7, 7]
152        e = [15, 15]
153        f = [15, 30]
154        g = [30, 10]
155        h = [30, 30]
156
157        points = [a, b, c, d, e, f, g, h]
158       
159        #bac, bce, ecf, dbe, daf, dae
160        vertices = [[1,0,2], [1,3,4], [1,2,3], [5,4,7], [4,6,7]]
161
162        mesh = Mesh(points, vertices)
163
164        Q = MeshQuadtree(mesh)   
165
166
167        results = Q.search_fast([5.5, 5.5])
168        print 'visits: ', Q.count_visits()
169       
170        Q.clear_visits()
171        results = Q.search_fast([30, 10])
172        print 'visits: ', Q.count_visits()
173       
174        print 'second time:'
175
176        Q.clear_visits()       
177        results = Q.search_fast([5.5, 5.5])
178        print 'visits: ', Q.count_visits()
179################################################################################
180
181if __name__ == "__main__":
182    mysuite = unittest.makeSuite(Test_Quad,'test')
183    runner = unittest.TextTestRunner()
184    runner.run(mysuite)
Note: See TracBrowser for help on using the repository browser.