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

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

When fitting, cell data - triangle vertices and norms - are calculated the first time a point is looked for in a cell. This is to speed things up. Also, the old point info is deleted to save memory.

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