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

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

Change Numeric imports to general form - ready to change to NumPy?.

File size: 7.2 KB
Line 
1import unittest
2import Numeric as num
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_interpolate_one_point_many_triangles(self):
154        # this test has 10 triangles that share the same vert.
155        # If the number of points per cell in  a quad tree is less
156        # than 10 it should crash
157        z0 = [2.0, 5.0]
158        z1 = [2.0, 5.0]
159        z2 = [2.0, 5.0]
160        z3 = [2.0, 5.0]
161        z4 = [2.0, 5.0]
162        z5 = [2.0, 5.0]
163        z6 = [2.0, 5.0]
164        z7 = [2.0, 5.0]
165        z8 = [2.0, 5.0]
166        z9 = [2.0, 5.0]
167        z10 = [2.0, 5.0]
168       
169        v0 = [0.0, 0.0]
170        v1 = [1.0, 0.0]
171        v2 = [2.0, 0.0]
172        v3 = [3.0, 0.0]
173        v4 = [4.0, 0.0]
174        v5 = [0.0, 10.0]
175        v6 = [1.0, 10.0]
176        v7 = [2.0, 10.0]
177        v8 = [3.0, 10.0]
178        v9 = [4.0, 10.0]
179
180        vertices = [z0,v0, v1, v2, v3,v4 ,v5, v6, v7, v8, v9,
181                    z1, z2, z3, z4, z5, z6, z7, z8, z9]
182        triangles = [
183                      [11,1,2],
184                      [12,2,3],
185                      [13,3,4],
186                      [14,4,5],
187                      [7,6,15],
188                      [8,7,16],
189                      [9,8,17],
190                      [10,9,18],
191                      [6,1,19],
192                      [5,10,0]
193                      ]
194       
195        mesh = Mesh(vertices, triangles)
196        try:
197            Q = build_quadtree(mesh, max_points_per_cell = 9)
198        except RuntimeError:
199            pass
200        else:
201            self.failUnless(0 ==1,  'many verts at the same position no  \
202            longer causes as error')
203   
204    def test_retrieve_triangles(self):
205
206        cell = Cell(0, 6, 0, 6, 'cell', max_points_per_cell=4)
207
208        p0 = [2,1]
209        p1 = [4,1]
210        p2 = [4.,4]
211        p3 = [2,4]
212        p4 = [5,4]
213
214        points = [p0,p1,p2, p3, p4]
215        #
216        vertices = [[0,1,2],[0,2,3],[1,4,2]]
217
218        mesh = Mesh(points, vertices)
219
220        Q = build_quadtree(mesh)
221        results = Q.search(5,1)
222        assert len(results),2
223        #print "results", results
224        #print "results[0][0]", results[0][0]
225        assert results[0],0
226        assert results[1],2
227        assert results[0][1],[[ 2.,  1.],
228                     [ 4.,  1.],
229                     [ 4.,  4.]]
230        assert results[1][1],[[ 4.,  1.],
231                     [ 5.,  4.],
232                     [ 4.,  4.]]
233        # this is the normals
234        assert results[0][1][1],[[1.,  0.],
235                     [-0.83205029,  0.5547002],
236                     [ 0.,  -1.]]
237                     
238        # assert num.allclose(num.array(results),[[[ 2.,  1.],
239        #[ 4.,  1.], [ 4.,  4.]], [[ 4.,  1.],[ 5.,  4.],[ 4.,  4.]]] )
240        results = Q.search(5,4.)
241        ### print "results",results
242        # results_dic={}
243        # results_dic.update(results)
244        assert len(results),3
245        #print "results_dic[0]", results_dic[0]
246        assert results[0][1],[[ 2.,  1.],
247                     [ 4.,  1.],
248                     [ 4.,  4.]]
249        assert results[1][1],[[ 2.,  1.],
250                     [ 4.,  4.],
251                     [ 2.,  4.]]
252        assert results[2][1],[[ 4.,  1.],
253                     [ 5.,  4.],
254                     [ 4.,  4.]]
255        #assert allclose(array(results),[[[ 2.,  1.],[ 4.,  1.], [ 4.,  4.]]
256         #                               ,[[ 2.,  1.],[ 4.,  4.], [ 2.,  4.]],
257        #[[ 4.,  1.],  [ 5.,  4.], [ 4.,  4.]],
258         #                               [[ 4.,  1.], [ 5.,  4.], [ 4.,  4.]]])
259       
260       
261#-------------------------------------------------------------
262if __name__ == "__main__":
263
264    mysuite = unittest.makeSuite(Test_Quad,'test')
265    #mysuite = unittest.makeSuite(Test_Quad,'test_interpolate_one_point_many_triangles')
266    runner = unittest.TextTestRunner()
267    runner.run(mysuite)
Note: See TracBrowser for help on using the repository browser.