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

Last change on this file since 8125 was 8125, checked in by wilsonr, 13 years ago

Changes to address ticket 360.

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