source: anuga_core/source/anuga/fit_interpolate/test_search_functions.py @ 5485

Last change on this file since 5485 was 4859, checked in by duncan, 16 years ago

check the last triangle fit/interp speed up. Upgrading the code to time runs

File size: 6.4 KB
Line 
1#!/usr/bin/env python
2
3
4import unittest
5from search_functions import search_tree_of_vertices, set_last_triangle
6from search_functions import _search_triangles_of_vertices
7
8
9from Numeric import zeros, array, allclose
10
11from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
12from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular
13from anuga.utilities.polygon import is_inside_polygon
14from anuga.utilities.quad import build_quadtree, Cell
15
16
17class Test_search_functions(unittest.TestCase):
18    def setUp(self):
19        pass
20
21    def tearDown(self):
22        pass
23
24    def NOtest_that_C_extension_compiles(self):
25        FN = 'search_functions_ext.c'
26        try:
27            import search_functions_ext
28        except:
29            from compile import compile
30
31            try:
32                compile(FN)
33            except:
34                raise 'Could not compile %s' %FN
35            else:
36                import search_functions_ext
37
38
39
40    def test_small(self):
41        """test_small: Two triangles
42        """
43
44        points, vertices, boundary = rectangular(1, 1, 1, 1)
45        mesh = Mesh(points, vertices, boundary)
46
47        #Test that points are arranged in a counter clock wise order
48        mesh.check_integrity()
49
50        root = build_quadtree(mesh, max_points_per_cell = 1)
51        set_last_triangle()
52
53        x = [0.2, 0.7]
54        found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x)
55        assert k == 1 # Triangle one
56        assert found is True
57
58    def test_bigger(self):
59        """test_larger mesh
60        """
61
62        points, vertices, boundary = rectangular(4, 4, 1, 1)
63        mesh = Mesh(points, vertices, boundary)
64
65        #Test that points are arranged in a counter clock wise order
66        mesh.check_integrity()
67
68        root = build_quadtree(mesh, max_points_per_cell = 4)
69        set_last_triangle()
70
71        for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
72                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
73                  [10, 3]]:
74               
75            found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x)
76
77            if k >= 0:
78                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
79                assert is_inside_polygon(x, V)
80                assert found is True
81                #print k, x
82            else:
83                assert found is False               
84
85       
86
87    def test_large(self):
88        """test_larger mesh and different quad trees
89        """
90
91        points, vertices, boundary = rectangular(10, 12, 1, 1)
92        mesh = Mesh(points, vertices, boundary)
93
94        #Test that points are arranged in a counter clock wise order
95        mesh.check_integrity()
96
97       
98        for m in range(8):
99            root = build_quadtree(mesh, max_points_per_cell = m)
100            set_last_triangle()
101            #print m, root.show()
102
103            for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
104                      [0.1,0.9], [0.4,0.6], [0.9,0.1],
105                      [10, 3]]:
106               
107                found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x)
108
109                if k >= 0:
110                    V = mesh.get_vertex_coordinates(k) # nodes for triangle k
111                    assert is_inside_polygon(x, V)
112                    assert found is True
113                else:
114                    assert found is False               
115
116               
117
118    def test_underlying_function(self):
119        """test_larger mesh and different quad trees
120        """
121
122        points, vertices, boundary = rectangular(2, 2, 1, 1)
123        mesh = Mesh(points, vertices, boundary)
124
125        root = build_quadtree(mesh, max_points_per_cell = 4)
126        set_last_triangle()
127
128        # One point
129        x = [0.5, 0.5]
130        candidate_vertices = root.search(x[0], x[1])
131
132        #print x, candidate_vertices
133        found, sigma0, sigma1, sigma2, k = \
134               _search_triangles_of_vertices(mesh,
135                                             candidate_vertices,
136                                             x)
137
138        if k >= 0:
139            V = mesh.get_vertex_coordinates(k) # nodes for triangle k
140            assert is_inside_polygon(x, V)
141            assert found is True
142        else:
143            assert found is False               
144
145       
146
147        # More points   
148        for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
149                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
150                  [10, 3]]:
151               
152            candidate_vertices = root.search(x[0], x[1])
153
154            #print x, candidate_vertices
155            found, sigma0, sigma1, sigma2, k = \
156                   _search_triangles_of_vertices(mesh,
157                                                 candidate_vertices,
158                                                 x)
159            if k >= 0:
160                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
161                assert is_inside_polygon(x, V)
162                assert found is True
163            else:
164                assert found is False
165
166               
167
168    def expanding_search(self):
169        """test_larger mesh and different quad trees
170        """
171       
172        p0 = [2,1]
173        p1 = [4,1]
174        p2 = [4.,4]
175        p3 = [2,4]
176        p4 = [5,4]
177
178        p5 = [-1,-1]
179        p6 = [1,-1]
180        p7 = [1,1]
181        p8 = [-1,1]
182
183        points = [p0,p1,p2, p3,p4,p5,p6,p7,p8]
184        #
185        vertices = [[0,1,2],[0,2,3],[1,4,2],[5,6,7], [5,7,8]]
186        mesh = Mesh(points, vertices)
187
188        # Don't do this, want to control the max and mins
189        #root = build_quadtree(mesh, max_points_per_cell=4)
190   
191
192        root = Cell(-3, 9, -3, 9, mesh,
193                    max_points_per_cell = 4)
194        #Insert indices of all vertices
195        root.insert( range(mesh.number_of_nodes) )
196
197        #Build quad tree and return
198        root.split()
199       
200        # One point
201        #x = [3.5, 1.5]
202        x = [2.5, 1.5]
203        element_found, sigma0, sigma1, sigma2, k = \
204                       search_tree_of_vertices(root, mesh, x)
205        # One point
206        x = [3.00005, 2.999994]
207        element_found, sigma0, sigma1, sigma2, k = \
208                       search_tree_of_vertices(root, mesh, x)
209        assert element_found is True
210        assert k == 1
211       
212
213#-------------------------------------------------------------
214if __name__ == "__main__":
215    suite = unittest.makeSuite(Test_search_functions,'test')
216    #suite = unittest.makeSuite(Test_search_functions,'expanding_search')
217    runner = unittest.TextTestRunner(verbosity=1)
218    runner.run(suite)
219   
Note: See TracBrowser for help on using the repository browser.