source: anuga_core/source/anuga/utilities/test_quad.py @ 4666

Last change on this file since 4666 was 4666, checked in by duncan, 17 years ago

unverified solution to ticket#176

File size: 5.9 KB
Line 
1import unittest
2from Numeric import array, allclose
3
4from quad import Cell, build_quadtree
5from anuga.abstract_2d_finite_volumes.general_mesh import General_mesh as Mesh
6
7import types, sys
8
9#-------------------------------------------------------------
10
11class Test_Quad(unittest.TestCase):
12
13    def setUp(self):
14
15        a = [3, 107]
16        b = [5, 107]
17        c = [5, 105]
18        d = [7, 107]
19        e = [15, 115]
20        f = [15, 130]
21        g = [30, 110]
22        h = [30, 130]
23
24        points = [a, b, c, d, e, f, g, h]
25       
26        #bac, bce, ecf, dbe, daf, dae
27        vertices = [[1,0,2], [1,3,4], [1,2,3], [5,4,7], [4,6,7]]
28
29        mesh = Mesh(points, vertices)
30        self.mesh = mesh
31        self.cell = Cell(100, 140, 0, 40, mesh, 'cell')
32
33    def tearDown(self):
34        pass
35
36    def test_add_points_2_cell(self):
37        self.cell.insert(0)
38        self.cell.insert(1)
39
40        result = self.cell.retrieve()
41        assert type(result) in [types.ListType,types.TupleType],\
42                                'should be a list'
43        self.assertEqual(len(result),2)
44
45    def test_add_points_2_cellII(self):
46        self.cell.insert([0,1,2,3,4,5,6,7])
47
48        result = self.cell.retrieve()
49        assert type(result) in [types.ListType,types.TupleType],\
50                                'should be a list'
51        self.assertEqual(len(result),8)
52
53
54    def test_search(self):
55        self.cell.insert([0,1,2,3,4,5,6,7])
56        self.cell.split(4)
57
58        result =  self.cell.search(x = 1, y = 101, get_vertices=True)
59        assert type(result) in [types.ListType,types.TupleType],\
60                                'should be a list'
61        self.assertEqual(result, [0,1,2,3])
62
63
64    def test_clear_1(self):
65        self.cell.insert([0,1,2,3,4,5,6,7])
66        assert self.cell.count() == 8
67        self.cell.clear()
68
69        #This one actually revealed a bug :-)
70        assert self.cell.count() == 0
71
72    def test_clear_2(self):
73        self.cell.insert([0,1,2,3,4,5,6,7])
74        assert self.cell.count() == 8
75        self.cell.split(2)
76        assert self.cell.count() == 8
77
78        self.cell.clear()
79        assert self.cell.count() == 0
80
81
82
83    def test_split(self):
84        self.cell.insert([0,1,2,3,4,5,6,7], split = False)
85
86        #No children yet
87        assert self.cell.children is None
88        assert self.cell.count() == 8
89
90        #Split
91        self.cell.split(4)
92        #self.cell.show()
93        #self.cell.show_all()
94
95
96        #Now there are children
97        assert self.cell.children is not None
98        assert self.cell.count() == 8
99
100
101
102    def test_collapse(self):
103        self.cell.insert([0,1,2,3,4,5,6,7], split = False)
104
105        #Split maximally
106        self.cell.split(1)
107
108        #Now there are children
109        assert self.cell.children is not None
110        assert self.cell.count() == 8
111
112        #Collapse
113        self.cell.collapse(8)
114
115        #No children
116        assert self.cell.children is None
117        assert self.cell.count() == 8
118
119    def test_build_quadtree(self):
120
121        Q = build_quadtree(self.mesh)
122        #Q.show()
123        #print Q.count()
124        assert Q.count() == 8
125
126
127
128        result = Q.search(3, 105, get_vertices=True)
129        assert type(result) in [types.ListType,types.TupleType],\
130                                'should be a list'
131        #print "result",result
132        self.assertEqual(result, [0,1,2,3])
133
134
135    def test_build_quadtreeII(self):
136
137        self.cell = Cell(100, 140, 0, 40, 'cell')
138
139        p0 = [34.6292076111,-7999.92529297]
140        p1 = [8000.0, 7999.0]
141        p2 = [-7999.96630859, 7999.0]
142        p3 = [34, 7999.97021484]
143
144        points = [p0,p1,p2, p3]
145        #bac, bce, ecf, dbe, daf, dae
146        vertices = [[0,1,2],[0,2,3]]
147
148        mesh = Mesh(points, vertices)
149
150        #This was causing round off error
151        Q = build_quadtree(mesh)
152
153    def test_retrieve_triangles(self):
154
155        cell = Cell(0, 6, 0, 6, 'cell', max_points_per_cell=4)
156
157        p0 = [2,1]
158        p1 = [4,1]
159        p2 = [4.,4]
160        p3 = [2,4]
161        p4 = [5,4]
162
163        points = [p0,p1,p2, p3, p4]
164        #
165        vertices = [[0,1,2],[0,2,3],[1,4,2]]
166
167        mesh = Mesh(points, vertices)
168
169        Q = build_quadtree(mesh)
170        results = Q.search(5,1)
171        assert len(results),2
172        #print "results", results
173        #print "results[0][0]", results[0][0]
174        assert results[0],0
175        assert results[1],2
176        assert results[0][1],[[ 2.,  1.],
177                     [ 4.,  1.],
178                     [ 4.,  4.]]
179        assert results[1][1],[[ 4.,  1.],
180                     [ 5.,  4.],
181                     [ 4.,  4.]]
182        # this is the normals
183        assert results[0][1][1],[[1.,  0.],
184                     [-0.83205029,  0.5547002],
185                     [ 0.,  -1.]]
186                     
187        # assert allclose(array(results),[[[ 2.,  1.],
188        #[ 4.,  1.], [ 4.,  4.]], [[ 4.,  1.],[ 5.,  4.],[ 4.,  4.]]] )
189        results = Q.search(5,4.)
190        ### print "results",results
191        # results_dic={}
192        # results_dic.update(results)
193        assert len(results),3
194        #print "results_dic[0]", results_dic[0]
195        assert results[0][1],[[ 2.,  1.],
196                     [ 4.,  1.],
197                     [ 4.,  4.]]
198        assert results[1][1],[[ 2.,  1.],
199                     [ 4.,  4.],
200                     [ 2.,  4.]]
201        assert results[2][1],[[ 4.,  1.],
202                     [ 5.,  4.],
203                     [ 4.,  4.]]
204        #assert allclose(array(results),[[[ 2.,  1.],[ 4.,  1.], [ 4.,  4.]]
205         #                               ,[[ 2.,  1.],[ 4.,  4.], [ 2.,  4.]],
206        #[[ 4.,  1.],  [ 5.,  4.], [ 4.,  4.]],
207         #                               [[ 4.,  1.], [ 5.,  4.], [ 4.,  4.]]])
208       
209       
210#-------------------------------------------------------------
211if __name__ == "__main__":
212
213    mysuite = unittest.makeSuite(Test_Quad,'test')
214    # mysuite = unittest.makeSuite(Test_Quad,'test_retrieve_triangles')
215    runner = unittest.TextTestRunner()
216    runner.run(mysuite)
Note: See TracBrowser for help on using the repository browser.