1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | """ |
---|
4 | I removed lone test vert's, since I'm working on removing lone verts at a lower |
---|
5 | level of the code, using the -j flag in triangle. |
---|
6 | """ |
---|
7 | |
---|
8 | |
---|
9 | import sys |
---|
10 | |
---|
11 | import unittest |
---|
12 | from anuga.mesh_engine.mesh_engine import generate_mesh |
---|
13 | |
---|
14 | import numpy as num |
---|
15 | |
---|
16 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
17 | |
---|
18 | from anuga.utilities.anuga_exceptions import ANUGAError |
---|
19 | |
---|
20 | class triangTestCase(unittest.TestCase): |
---|
21 | def setUp(self): |
---|
22 | pass |
---|
23 | |
---|
24 | def tearDown(self): |
---|
25 | pass |
---|
26 | |
---|
27 | def testrectangleIIb(self): |
---|
28 | |
---|
29 | points = [] |
---|
30 | seglist = [] |
---|
31 | holelist = [] |
---|
32 | regionlist = [] |
---|
33 | |
---|
34 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
35 | pointattlist = [] |
---|
36 | regionlist.append( (1.2,1.2,5.0) ) |
---|
37 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
38 | segattlist = [] |
---|
39 | |
---|
40 | mode = "Qzp" |
---|
41 | |
---|
42 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
43 | pointattlist,segattlist, mode, points) |
---|
44 | |
---|
45 | correct = num.array([(1, 0, 2), (2, 3, 1)]) |
---|
46 | self.failUnless(num.alltrue(data['generatedtrianglelist'].flat == \ |
---|
47 | correct.flat), |
---|
48 | 'trianglelist is wrong!') |
---|
49 | correct = num.array([(0, 1), (1, 3), (3, 2), (2, 0)]) |
---|
50 | self.failUnless(num.alltrue(data['generatedsegmentlist'].flat == \ |
---|
51 | correct.flat), |
---|
52 | 'segmentlist is wrong!') |
---|
53 | |
---|
54 | correct = num.array([(0.0, 0.0), (0.0, 10.0), |
---|
55 | (3.0, 0.0), (3.0, 10.0)]) |
---|
56 | self.failUnless(num.allclose(data['generatedpointlist'].flat, \ |
---|
57 | correct.flat), |
---|
58 | 'Failed') |
---|
59 | |
---|
60 | def testrectangleIIc(self): |
---|
61 | points = [] |
---|
62 | seglist = [] |
---|
63 | holelist = [] |
---|
64 | regionlist = [] |
---|
65 | |
---|
66 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
67 | pointattlist = None |
---|
68 | regionlist.append( (1.2,1.2,5.0) ) |
---|
69 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
70 | segattlist = None |
---|
71 | |
---|
72 | mode = "Qzp" |
---|
73 | |
---|
74 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
75 | pointattlist,segattlist, mode, points) |
---|
76 | correct = num.array([(1, 0, 2), (2, 3, 1)]) |
---|
77 | self.failUnless(num.alltrue(data['generatedtrianglelist'].flat == \ |
---|
78 | correct.flat), |
---|
79 | 'trianglelist is wrong!') |
---|
80 | correct = num.array([(0, 1), (1, 3), (3, 2), (2, 0)]) |
---|
81 | self.failUnless(num.alltrue(data['generatedsegmentlist'].flat == \ |
---|
82 | correct.flat), |
---|
83 | 'segmentlist is wrong!') |
---|
84 | |
---|
85 | correct = num.array([(0.0, 0.0), (0.0, 10.0), |
---|
86 | (3.0, 0.0), (3.0, 10.0)]) |
---|
87 | self.failUnless(num.allclose(data['generatedpointlist'].flat, \ |
---|
88 | correct.flat), |
---|
89 | 'Failed') |
---|
90 | |
---|
91 | def test_bad_point_atts(self): |
---|
92 | |
---|
93 | points = [] |
---|
94 | seglist = [] |
---|
95 | holelist = [] |
---|
96 | regionlist = [] |
---|
97 | |
---|
98 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
99 | pointattlist = [[],[],[]] |
---|
100 | regionlist.append( (1.2,1.2,5.0) ) |
---|
101 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
102 | segattlist = [0,0,0,0] |
---|
103 | |
---|
104 | mode = "Qzp" |
---|
105 | |
---|
106 | try: |
---|
107 | data = generate_mesh(points,seglist,holelist, |
---|
108 | regionlist,pointattlist,segattlist, |
---|
109 | mode, points) |
---|
110 | except ANUGAError: |
---|
111 | pass |
---|
112 | else: |
---|
113 | self.failUnless(0 ==1, |
---|
114 | 'bad list did not raise error!') |
---|
115 | |
---|
116 | |
---|
117 | def test_bad_point_attsII(self): |
---|
118 | points = [] |
---|
119 | seglist = [] |
---|
120 | holelist = [] |
---|
121 | regionlist = [] |
---|
122 | |
---|
123 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
124 | pointattlist = [[1],[2],[3],[4,8]] |
---|
125 | regionlist.append( (1.2,1.2,5.0) ) |
---|
126 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
127 | segattlist = [0,0,0,0] |
---|
128 | |
---|
129 | mode = "Qzp" |
---|
130 | |
---|
131 | try: |
---|
132 | data = generate_mesh(points,seglist,holelist, |
---|
133 | regionlist,pointattlist,segattlist, |
---|
134 | mode, points) |
---|
135 | except ANUGAError: |
---|
136 | pass |
---|
137 | else: |
---|
138 | self.failUnless(0 ==1, |
---|
139 | 'bad list did not raise error!') |
---|
140 | |
---|
141 | def testsegmarker(self): |
---|
142 | |
---|
143 | holelist = [] |
---|
144 | regionlist = [] |
---|
145 | |
---|
146 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
147 | pointattlist = [[],[],[],[]] |
---|
148 | regionlist.append( (1.2,1.2,5.0) ) |
---|
149 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
150 | segattlist = [1.0,2.0,3.0,4.0] |
---|
151 | |
---|
152 | mode = "Qzp" |
---|
153 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
154 | pointattlist,segattlist, mode, points) |
---|
155 | |
---|
156 | correct = num.array([(1, 0, 2), (2, 3, 1)]) |
---|
157 | self.failUnless(num.alltrue(data['generatedtrianglelist'].flat == \ |
---|
158 | correct.flat), |
---|
159 | 'trianglelist is wrong!') |
---|
160 | correct = num.array([(0, 1), (1, 3), (3, 2), (2, 0)]) |
---|
161 | self.failUnless(num.alltrue(data['generatedsegmentlist'].flat == \ |
---|
162 | correct.flat), |
---|
163 | 'segmentlist is wrong!') |
---|
164 | |
---|
165 | correct = num.array([(0.0, 0.0), (0.0, 10.0), |
---|
166 | (3.0, 0.0), (3.0, 10.0)]) |
---|
167 | self.failUnless(num.allclose(data['generatedpointlist'].flat, \ |
---|
168 | correct.flat), |
---|
169 | 'Failed') |
---|
170 | |
---|
171 | self.failUnless(num.alltrue(data['generatedsegmentmarkerlist'] == \ |
---|
172 | num.array([1,2,3,4])), |
---|
173 | 'Failed!') |
---|
174 | |
---|
175 | def testbad_region(self): |
---|
176 | |
---|
177 | points = [] |
---|
178 | seglist = [] |
---|
179 | holelist = [] |
---|
180 | regionlist = [(1.2,1.2)] |
---|
181 | |
---|
182 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
183 | pointattlist = [[],[],[],[]] |
---|
184 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
185 | segattlist = [0,0,0,0] |
---|
186 | |
---|
187 | mode = "Qzpn" |
---|
188 | try: |
---|
189 | data = generate_mesh(points,seglist,holelist, |
---|
190 | regionlist,pointattlist,segattlist, |
---|
191 | mode, points) |
---|
192 | except ANUGAError: |
---|
193 | pass |
---|
194 | else: |
---|
195 | self.failUnless(0 ==1, |
---|
196 | 'bad region list did not raise error!') |
---|
197 | |
---|
198 | def testbad_regionII(self): |
---|
199 | |
---|
200 | points = [] |
---|
201 | seglist = [] |
---|
202 | holelist = [] |
---|
203 | regionlist = [(1.2,1.2), (1.2,1.25,1.0)] |
---|
204 | |
---|
205 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
206 | pointattlist = [[],[],[],[]] |
---|
207 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
208 | segattlist = [0,0,0,0] |
---|
209 | |
---|
210 | mode = "Qzpn" |
---|
211 | try: |
---|
212 | data = generate_mesh(points,seglist,holelist, |
---|
213 | regionlist,pointattlist,segattlist, |
---|
214 | mode, points) |
---|
215 | except ANUGAError: |
---|
216 | pass |
---|
217 | else: |
---|
218 | self.failUnless(0==1, |
---|
219 | 'bad region list did not raise error!') |
---|
220 | |
---|
221 | def testregion_with_maxarea(self): |
---|
222 | |
---|
223 | points = [] |
---|
224 | seglist = [] |
---|
225 | holelist = [] |
---|
226 | regionlist = [(3,1,1.0)] |
---|
227 | |
---|
228 | points = [(0.0,0.0),(6.0,0.0),(6.0,6.0),(0.0,6.0)] |
---|
229 | pointattlist = [[],[],[],[]] |
---|
230 | seglist = [(0,1),(1,2),(3,2),(3,0),(0,2)] |
---|
231 | segattlist = [0,0,0,0,0] |
---|
232 | |
---|
233 | mode = "Qzpna36a" |
---|
234 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
235 | pointattlist,segattlist, mode, points) |
---|
236 | |
---|
237 | self.failUnless(len(data['generatedtrianglelist']) == 2, |
---|
238 | 'testregion_with_maxarea 1: # of tris is wrong!') |
---|
239 | ## Another test case |
---|
240 | regionlist = [(3,1,1.0),(1,3,1.0,8.0)] |
---|
241 | mode = "Qzp21na36a" |
---|
242 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
243 | pointattlist,segattlist, mode, points) |
---|
244 | #print "len(data['generatedtrianglelist']",len(data['generatedtrianglelist']) |
---|
245 | # for Duncan On unix this returns a 7 triangle result. |
---|
246 | # for Duncan on Windows returns a 6 triangle result. |
---|
247 | # for Ole on nautilus this returns 6 |
---|
248 | # for Duncan on nautilus this returns 7 |
---|
249 | # It seems to be the results from triangle that is |
---|
250 | # causing the different results, and we are treating |
---|
251 | # triangle as a back box. |
---|
252 | |
---|
253 | self.failUnless(len(data['generatedtrianglelist']) >= 6, |
---|
254 | 'testregion_with_maxarea 2: # of tris is wrong!') |
---|
255 | ## Another test case |
---|
256 | regionlist = [(3,1,1.0,8.0),(1,3,1.0,8.0)] |
---|
257 | mode = "Qzpna36a" |
---|
258 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
259 | pointattlist,segattlist, mode, points) |
---|
260 | #print "len(data['generatedtrianglelist']",len(data['generatedtrianglelist']) |
---|
261 | # On unix this returns a 10 triangle result. |
---|
262 | # Windows returns a 8 triangle result. |
---|
263 | self.failUnless(len(data['generatedtrianglelist']) >= 8, |
---|
264 | 'testregion_with_maxarea 3: # of tris is wrong!') |
---|
265 | |
---|
266 | ## Another test case |
---|
267 | regionlist = [(3,1,1.0),(1,3,1.0,8.0)] |
---|
268 | mode = "Qzpna8a" |
---|
269 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
270 | pointattlist,segattlist, mode, points) |
---|
271 | #print "len(data['generatedtrianglelist']",len(data['generatedtrianglelist']) |
---|
272 | # On unix this returns a 10 triangle result. |
---|
273 | # Windows returns a 8 triangle result. |
---|
274 | |
---|
275 | self.failUnless(len(data['generatedtrianglelist']) >= 8, |
---|
276 | 'testregion_with_maxarea 4: # of tris is wrong!') |
---|
277 | |
---|
278 | |
---|
279 | def FIXME_testbad_hole(self): |
---|
280 | |
---|
281 | holelist = [(9.0)] |
---|
282 | regionlist = [] |
---|
283 | |
---|
284 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
285 | pointattlist = [[],[],[],[]] |
---|
286 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
287 | segattlist = [0,0,0,0] |
---|
288 | |
---|
289 | mode = "Qzpn" |
---|
290 | try: |
---|
291 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
292 | pointattlist,segattlist, mode, points) |
---|
293 | |
---|
294 | except TypeError: |
---|
295 | pass |
---|
296 | else: |
---|
297 | self.failUnless(0 ==1, |
---|
298 | 'bad hole list did not raise error!') |
---|
299 | |
---|
300 | |
---|
301 | def testbad_segattlist(self): |
---|
302 | holelist = [] |
---|
303 | regionlist = [] |
---|
304 | |
---|
305 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
306 | pointattlist = [[],[],[],[]] |
---|
307 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
308 | segattlist = [0,0] |
---|
309 | |
---|
310 | mode = "Qzpn" |
---|
311 | try: |
---|
312 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
313 | pointattlist,segattlist, mode, points) |
---|
314 | |
---|
315 | except ANUGAError: |
---|
316 | pass |
---|
317 | else: |
---|
318 | self.failUnless(0 ==1, |
---|
319 | 'bad segment attribute list did not raise error!') |
---|
320 | |
---|
321 | def testrectangle_regions(self): |
---|
322 | |
---|
323 | points = [] |
---|
324 | seglist = [] |
---|
325 | holelist = [] |
---|
326 | regionlist = [] |
---|
327 | |
---|
328 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
329 | pointattlist = [[],[],[],[]] |
---|
330 | #it seems that |
---|
331 | #triangle only associates one region with a triangle |
---|
332 | regionlist.append( (1.3,1.3,88.33) ) |
---|
333 | regionlist.append( (1.2,1.2,77,55) ) |
---|
334 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
335 | segattlist = [0,0,0,0] |
---|
336 | mode = "QAzpq" |
---|
337 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
338 | pointattlist,segattlist, mode, points) |
---|
339 | |
---|
340 | correct = num.array([[77.0], [77.0], [77.0], [77.0]]) |
---|
341 | self.failUnless(num.allclose(data['generatedtriangleattributelist'].flat, |
---|
342 | correct.flat), |
---|
343 | 'Failed') |
---|
344 | |
---|
345 | def testrectangle_regionsII(self): |
---|
346 | points = [] |
---|
347 | seglist = [] |
---|
348 | holelist = [] |
---|
349 | regionlist = [] |
---|
350 | |
---|
351 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
352 | pointattlist = [[],[],[],[]] |
---|
353 | #it seems that |
---|
354 | #triangle only associates one region with a triangle |
---|
355 | regionlist.append( [1.3,1.3,88.33] ) |
---|
356 | regionlist.append( [1.2,1.2,77,55] ) |
---|
357 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
358 | segattlist = [0,0,0,0] |
---|
359 | |
---|
360 | mode = "QAzpq" |
---|
361 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
362 | pointattlist,segattlist, mode, points) |
---|
363 | |
---|
364 | correct = num.array([[77.0], [77.0], [77.0], [77.0]]) |
---|
365 | self.failUnless(num.allclose(data['generatedtriangleattributelist'].flat, |
---|
366 | correct.flat), |
---|
367 | 'Failed') |
---|
368 | |
---|
369 | |
---|
370 | def test_numeric_arrays(self): |
---|
371 | points = [] |
---|
372 | seglist = [] |
---|
373 | holelist = [] |
---|
374 | regionlist = [] |
---|
375 | points = [(0.0,0.0),(0.0,10.0),(3.0,0.0),(3.0,10.0)] |
---|
376 | pointattlist = [] |
---|
377 | # 5.0 is the region tag, 99.0 is the max area |
---|
378 | tri_tag = 123456.0 |
---|
379 | regionlist.append( [0.2,0.2, tri_tag,99.0] ) |
---|
380 | seglist = [(0,1),(1,3),(3,2),(2,0)] |
---|
381 | segattlist = [21,22,23,24] |
---|
382 | #The 'A' has to be there to get the region marker stuff working |
---|
383 | mode = "QzpnA" |
---|
384 | #mode = "jQpznAa2000.1a" |
---|
385 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
386 | pointattlist,segattlist, mode, points) |
---|
387 | #print "data", data |
---|
388 | |
---|
389 | |
---|
390 | correct = num.array([(1, 0, 2), (2, 3, 1)]) |
---|
391 | self.failUnless(num.alltrue(data['generatedtrianglelist'].flat == \ |
---|
392 | correct.flat), |
---|
393 | 'trianglelist is wrong!') |
---|
394 | correct = num.array([(0, 1), (1, 3), (3, 2), (2, 0)]) |
---|
395 | self.failUnless(num.alltrue(data['generatedsegmentlist'].flat == \ |
---|
396 | correct.flat), |
---|
397 | 'segmentlist is wrong!') |
---|
398 | |
---|
399 | correct = num.array([(0.0, 0.0), (0.0, 10.0), |
---|
400 | (3.0, 0.0), (3.0, 10.0)]) |
---|
401 | self.failUnless(num.allclose(data['generatedpointlist'].flat, \ |
---|
402 | correct.flat), |
---|
403 | 'Failed') |
---|
404 | |
---|
405 | correct = num.array([[tri_tag], [tri_tag]]) |
---|
406 | self.failUnless(num.allclose(data['generatedtriangleattributelist'].flat, \ |
---|
407 | correct.flat), |
---|
408 | 'Failed') |
---|
409 | correct = num.array([(0, 1), (1, 3), (3, 2), (2, 0)]) |
---|
410 | self.failUnless(num.alltrue(data['generatedsegmentlist'].flat == \ |
---|
411 | correct.flat), |
---|
412 | 'Failed!') |
---|
413 | |
---|
414 | correct = num.array(segattlist) |
---|
415 | self.failUnless(num.allclose(data['generatedsegmentmarkerlist'].flat, |
---|
416 | correct.flat), |
---|
417 | 'Failed') |
---|
418 | |
---|
419 | # I copied these answers from the output, so bad test.. |
---|
420 | correct = num.array([(-1, 1, -1), (-1, 0, -1)]) |
---|
421 | self.failUnless(num.alltrue(data['generatedtriangleneighborlist'].flat == \ |
---|
422 | correct.flat), |
---|
423 | 'Failed!') |
---|
424 | |
---|
425 | |
---|
426 | def test_pointattlist(self): |
---|
427 | # segattlist = [] |
---|
428 | points = [] |
---|
429 | seglist = [] |
---|
430 | holelist = [] |
---|
431 | regionlist = [] |
---|
432 | |
---|
433 | points = [(0.0,0.0),(0.0,4.0),(4.0,2.0),(2.0,0.0)] |
---|
434 | pointattlist = [0.,0.,10.,10.] |
---|
435 | regionlist.append( [0.2,0.2,2.1, 99.] ) |
---|
436 | seglist = [(0,1),(1,2),(2,3),(3,0)] |
---|
437 | segattlist = [11,12,13,14] |
---|
438 | mode = "Qzp" |
---|
439 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
440 | pointattlist,segattlist, mode, points) |
---|
441 | |
---|
442 | correct = num.array([[0.0],[0.0],[10],[10]]) |
---|
443 | self.failUnless(num.allclose(data['generatedpointattributelist'].flat, \ |
---|
444 | correct.flat), |
---|
445 | 'Failed') |
---|
446 | |
---|
447 | pointattlist = [[0.],[0.],[10.],[10.]] |
---|
448 | mode = "Qzp" |
---|
449 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
450 | pointattlist,segattlist, mode, points) |
---|
451 | correct = num.array([[0.0],[0.0],[10],[10]]) |
---|
452 | self.failUnless(num.allclose(data['generatedpointattributelist'].flat, \ |
---|
453 | correct.flat), |
---|
454 | 'Failed') |
---|
455 | |
---|
456 | pointattlist = [[0.,1],[0.,1],[10.,20],[10.,20]] |
---|
457 | mode = "Qzp" |
---|
458 | data = generate_mesh(points,seglist,holelist,regionlist, |
---|
459 | pointattlist,segattlist, mode, points) |
---|
460 | #print "data", data |
---|
461 | correct = num.array(pointattlist) |
---|
462 | self.failUnless(num.allclose(data['generatedpointattributelist'].flat, \ |
---|
463 | correct.flat), |
---|
464 | 'Failed') |
---|
465 | |
---|
466 | |
---|
467 | if __name__ == "__main__": |
---|
468 | suite = unittest.makeSuite(triangTestCase,'test') |
---|
469 | runner = unittest.TextTestRunner() #verbosity=2) |
---|
470 | runner.run(suite) |
---|