source: branches/numpy/anuga/fit_interpolate/test_search_functions.py @ 6304

Last change on this file since 6304 was 6304, checked in by rwilson, 15 years ago

Initial commit of numpy changes. Still a long way to go.

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