1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | #FIXME: Seperate the tests for mesh and general_mesh |
---|
4 | |
---|
5 | import unittest |
---|
6 | from math import sqrt |
---|
7 | |
---|
8 | from mesh import * |
---|
9 | from mesh_factory import rectangular |
---|
10 | from config import epsilon |
---|
11 | from Numeric import allclose, array |
---|
12 | |
---|
13 | def distance(x, y): |
---|
14 | return sqrt( sum( (array(x)-array(y))**2 )) |
---|
15 | |
---|
16 | class Test_Mesh(unittest.TestCase): |
---|
17 | def setUp(self): |
---|
18 | pass |
---|
19 | |
---|
20 | def tearDown(self): |
---|
21 | pass |
---|
22 | |
---|
23 | def test_triangle_inputs(self): |
---|
24 | points = [[0.0, 0.0], [4.0, 0.0], [0.0, 3.0]] |
---|
25 | vertices = [0,1,2] #Wrong |
---|
26 | |
---|
27 | try: |
---|
28 | mesh = Mesh(points, vertices) |
---|
29 | except: |
---|
30 | pass |
---|
31 | else: |
---|
32 | msg = 'Should have raised exception' |
---|
33 | raise msg |
---|
34 | |
---|
35 | |
---|
36 | def test_basic_triangle(self): |
---|
37 | |
---|
38 | a = [0.0, 0.0] |
---|
39 | b = [4.0, 0.0] |
---|
40 | c = [0.0, 3.0] |
---|
41 | |
---|
42 | points = [a, b, c] |
---|
43 | vertices = [[0,1,2]] |
---|
44 | mesh = Mesh(points, vertices) |
---|
45 | |
---|
46 | #Centroid |
---|
47 | centroid = mesh.centroid_coordinates[0] |
---|
48 | assert centroid[0] == 4.0/3 |
---|
49 | assert centroid[1] == 1.0 |
---|
50 | |
---|
51 | #Area |
---|
52 | assert mesh.areas[0] == 6.0,\ |
---|
53 | 'Area was %f, should have been 6.0' %mesh.areas[0] |
---|
54 | |
---|
55 | #Normals |
---|
56 | normals = mesh.get_normals() |
---|
57 | assert allclose(normals[0, 0:2], [3.0/5, 4.0/5]) |
---|
58 | assert allclose(normals[0, 2:4], [-1.0, 0.0]) |
---|
59 | assert allclose(normals[0, 4:6], [0.0, -1.0]) |
---|
60 | |
---|
61 | assert allclose(mesh.get_normal(0,0), [3.0/5, 4.0/5]) |
---|
62 | assert allclose(mesh.get_normal(0,1), [-1.0, 0.0]) |
---|
63 | assert allclose(mesh.get_normal(0,2), [0.0, -1.0]) |
---|
64 | |
---|
65 | #Edge lengths |
---|
66 | assert allclose(mesh.edgelengths[0], [5.0, 3.0, 4.0]) |
---|
67 | |
---|
68 | |
---|
69 | #Vertex coordinates |
---|
70 | V = mesh.get_vertex_coordinates() |
---|
71 | assert allclose(V[0], [0.0, 0.0, 4.0, 0.0, 0.0, 3.0]) |
---|
72 | |
---|
73 | V0 = mesh.get_vertex_coordinate(0, 0) |
---|
74 | assert allclose(V0, [0.0, 0.0]) |
---|
75 | |
---|
76 | V1 = mesh.get_vertex_coordinate(0, 1) |
---|
77 | assert allclose(V1, [4.0, 0.0]) |
---|
78 | |
---|
79 | V2 = mesh.get_vertex_coordinate(0, 2) |
---|
80 | assert allclose(V2, [0.0, 3.0]) |
---|
81 | |
---|
82 | |
---|
83 | #General tests: |
---|
84 | |
---|
85 | #Test that points are arranged in a counter clock wise order etc |
---|
86 | mesh.check_integrity() |
---|
87 | |
---|
88 | |
---|
89 | #Test that the centroid is located 2/3 of the way |
---|
90 | #from each vertex to the midpoint of the opposite side |
---|
91 | |
---|
92 | V = mesh.get_vertex_coordinates() |
---|
93 | |
---|
94 | x0 = V[0,0] |
---|
95 | y0 = V[0,1] |
---|
96 | x1 = V[0,2] |
---|
97 | y1 = V[0,3] |
---|
98 | x2 = V[0,4] |
---|
99 | y2 = V[0,5] |
---|
100 | |
---|
101 | m0 = [(x1 + x2)/2, (y1 + y2)/2] |
---|
102 | m1 = [(x0 + x2)/2, (y0 + y2)/2] |
---|
103 | m2 = [(x1 + x0)/2, (y1 + y0)/2] |
---|
104 | |
---|
105 | d0 = distance(centroid, [x0, y0]) |
---|
106 | d1 = distance(m0, [x0, y0]) |
---|
107 | assert d0 == 2*d1/3 |
---|
108 | # |
---|
109 | d0 = distance(centroid, [x1, y1]) |
---|
110 | d1 = distance(m1, [x1, y1]) |
---|
111 | assert abs(d0 - 2*d1/3) < epsilon, '%e, %e' %(d0, 2*d1/3) |
---|
112 | |
---|
113 | d0 = distance(centroid, [x2, y2]) |
---|
114 | d1 = distance(m2, [x2, y2]) |
---|
115 | assert abs(d0 - 2*d1/3) < epsilon, '%e, %e' %(d0, 2*d1/3) |
---|
116 | |
---|
117 | #Radius |
---|
118 | d0 = distance(centroid, m0) |
---|
119 | assert d0 == 5.0/6 |
---|
120 | |
---|
121 | d1 = distance(centroid, m1) |
---|
122 | assert d1 == sqrt(73.0/36) |
---|
123 | |
---|
124 | d2 = distance(centroid, m2) |
---|
125 | assert d2 == sqrt(13.0/9) |
---|
126 | |
---|
127 | assert mesh.radii[0] == min(d0, d1, d2) |
---|
128 | assert mesh.radii[0] == 5.0/6 |
---|
129 | |
---|
130 | |
---|
131 | #Let x be the centroid of triangle abc. |
---|
132 | #Test that areas of the three triangles axc, cxb, and bxa are equal. |
---|
133 | points = [a, b, c, centroid] |
---|
134 | vertices = [[0,3,2], [2,3,1], [1,3,0]] |
---|
135 | new_mesh = Mesh(points, vertices) |
---|
136 | |
---|
137 | assert new_mesh.areas[0] == new_mesh.areas[1] |
---|
138 | assert new_mesh.areas[1] == new_mesh.areas[2] |
---|
139 | assert new_mesh.areas[1] == new_mesh.areas[2] |
---|
140 | |
---|
141 | assert new_mesh.areas[1] == mesh.areas[0]/3 |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | def test_general_triangle(self): |
---|
146 | a = [2.0, 1.0] |
---|
147 | b = [6.0, 2.0] |
---|
148 | c = [1.0, 3.0] |
---|
149 | |
---|
150 | points = [a, b, c] |
---|
151 | vertices = [[0,1,2]] |
---|
152 | |
---|
153 | mesh = Mesh(points, vertices) |
---|
154 | centroid = mesh.centroid_coordinates[0] |
---|
155 | |
---|
156 | |
---|
157 | #Test that the centroid is located 2/3 of the way |
---|
158 | #from each vertex to the midpoint of the opposite side |
---|
159 | |
---|
160 | V = mesh.get_vertex_coordinates() |
---|
161 | |
---|
162 | x0 = V[0,0] |
---|
163 | y0 = V[0,1] |
---|
164 | x1 = V[0,2] |
---|
165 | y1 = V[0,3] |
---|
166 | x2 = V[0,4] |
---|
167 | y2 = V[0,5] |
---|
168 | |
---|
169 | m0 = [(x1 + x2)/2, (y1 + y2)/2] |
---|
170 | m1 = [(x0 + x2)/2, (y0 + y2)/2] |
---|
171 | m2 = [(x1 + x0)/2, (y1 + y0)/2] |
---|
172 | |
---|
173 | d0 = distance(centroid, [x0, y0]) |
---|
174 | d1 = distance(m0, [x0, y0]) |
---|
175 | assert abs(d0 - 2*d1/3) < epsilon, '%e, %e' %(d0, 2*d1/3) |
---|
176 | # |
---|
177 | d0 = distance(centroid, [x1, y1]) |
---|
178 | d1 = distance(m1, [x1, y1]) |
---|
179 | assert abs(d0 - 2*d1/3) < epsilon, '%e, %e' %(d0, 2*d1/3) |
---|
180 | |
---|
181 | d0 = distance(centroid, [x2, y2]) |
---|
182 | d1 = distance(m2, [x2, y2]) |
---|
183 | assert abs(d0 - 2*d1/3) < epsilon, '%e, %e' %(d0, 2*d1/3) |
---|
184 | |
---|
185 | #Radius |
---|
186 | d0 = distance(centroid, m0) |
---|
187 | d1 = distance(centroid, m1) |
---|
188 | d2 = distance(centroid, m2) |
---|
189 | assert mesh.radii[0] == min(d0, d1, d2) |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | #Let x be the centroid of triangle abc. |
---|
194 | #Test that areas of the three triangles axc, cxb, and bxa are equal. |
---|
195 | |
---|
196 | points = [a, b, c, centroid] |
---|
197 | vertices = [[0,3,2], [2,3,1], [1,3,0]] |
---|
198 | new_mesh = Mesh(points, vertices) |
---|
199 | |
---|
200 | assert new_mesh.areas[0] == new_mesh.areas[1] |
---|
201 | assert new_mesh.areas[1] == new_mesh.areas[2] |
---|
202 | assert new_mesh.areas[1] == new_mesh.areas[2] |
---|
203 | |
---|
204 | assert new_mesh.areas[1] == mesh.areas[0]/3 |
---|
205 | |
---|
206 | |
---|
207 | #Test that points are arranged in a counter clock wise order |
---|
208 | mesh.check_integrity() |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | |
---|
213 | def test_two_triangles(self): |
---|
214 | a = [0.0, 0.0] |
---|
215 | b = [0.0, 2.0] |
---|
216 | c = [2.0,0.0] |
---|
217 | e = [2.0, 2.0] |
---|
218 | points = [a, b, c, e] |
---|
219 | vertices = [ [1,0,2], [1,2,3] ] #bac, bce |
---|
220 | mesh = Mesh(points, vertices) |
---|
221 | |
---|
222 | assert mesh.areas[0] == 2.0 |
---|
223 | |
---|
224 | assert allclose(mesh.centroid_coordinates[0], [2.0/3, 2.0/3]) |
---|
225 | |
---|
226 | |
---|
227 | #Test that points are arranged in a counter clock wise order |
---|
228 | mesh.check_integrity() |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | def test_more_triangles(self): |
---|
233 | |
---|
234 | a = [0.0, 0.0] |
---|
235 | b = [0.0, 2.0] |
---|
236 | c = [2.0, 0.0] |
---|
237 | d = [0.0, 4.0] |
---|
238 | e = [2.0, 2.0] |
---|
239 | f = [4.0, 0.0] |
---|
240 | |
---|
241 | points = [a, b, c, d, e, f] |
---|
242 | #bac, bce, ecf, dbe, daf, dae |
---|
243 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
244 | mesh = Mesh(points, vertices) |
---|
245 | |
---|
246 | #Test that points are arranged in a counter clock wise order |
---|
247 | mesh.check_integrity() |
---|
248 | |
---|
249 | assert mesh.areas[0] == 2.0 |
---|
250 | assert mesh.areas[1] == 2.0 |
---|
251 | assert mesh.areas[2] == 2.0 |
---|
252 | assert mesh.areas[3] == 2.0 |
---|
253 | |
---|
254 | assert mesh.edgelengths[1,0] == 2.0 |
---|
255 | assert mesh.edgelengths[1,1] == 2.0 |
---|
256 | assert mesh.edgelengths[1,2] == sqrt(8.0) |
---|
257 | |
---|
258 | assert allclose(mesh.centroid_coordinates[0], [2.0/3, 2.0/3]) |
---|
259 | assert allclose(mesh.centroid_coordinates[1], [4.0/3, 4.0/3]) |
---|
260 | assert allclose(mesh.centroid_coordinates[2], [8.0/3, 2.0/3]) |
---|
261 | assert allclose(mesh.centroid_coordinates[3], [2.0/3, 8.0/3]) |
---|
262 | |
---|
263 | def test_mesh_and_neighbours(self): |
---|
264 | a = [0.0, 0.0] |
---|
265 | b = [0.0, 2.0] |
---|
266 | c = [2.0,0.0] |
---|
267 | d = [0.0, 4.0] |
---|
268 | e = [2.0, 2.0] |
---|
269 | f = [4.0,0.0] |
---|
270 | |
---|
271 | |
---|
272 | points = [a, b, c, d, e, f] |
---|
273 | |
---|
274 | #bac, bce, ecf, dbe |
---|
275 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
276 | mesh = Mesh(points, vertices) |
---|
277 | |
---|
278 | mesh.check_integrity() |
---|
279 | |
---|
280 | |
---|
281 | T = mesh |
---|
282 | tid = 0 |
---|
283 | assert T.number_of_boundaries[tid] == 2 |
---|
284 | assert T.neighbours[tid, 0] < 0 #Opposite point b (0,2) |
---|
285 | assert T.neighbours[tid, 1] == 1 #Opposite point a (0,0) |
---|
286 | assert T.neighbours[tid, 2] < 0 #Opposite point c (2,0) |
---|
287 | |
---|
288 | tid = 1 |
---|
289 | assert T.number_of_boundaries[tid] == 0 |
---|
290 | assert T.neighbours[tid, 0] == 2 #Opposite point b (0,2) |
---|
291 | assert T.neighbours[tid, 1] == 3 #Opposite point c (2,0) |
---|
292 | assert T.neighbours[tid, 2] == 0 #Opposite point e (2,2) |
---|
293 | |
---|
294 | tid = 2 |
---|
295 | assert T.number_of_boundaries[tid] == 2 |
---|
296 | assert T.neighbours[tid, 0] < 0 #Opposite point e (2,2) |
---|
297 | assert T.neighbours[tid, 1] < 0 #Opposite point c (2,0) |
---|
298 | assert T.neighbours[tid, 2] == 1 #Opposite point f (4,0) |
---|
299 | |
---|
300 | tid = 3 |
---|
301 | assert T.number_of_boundaries[tid] == 2 |
---|
302 | assert T.neighbours[tid, 0] == 1 #Opposite point d (0,4) |
---|
303 | assert T.neighbours[tid, 1] < 0 #Opposite point b (0,3) |
---|
304 | assert T.neighbours[tid, 2] < 0 #Opposite point e (2,2) |
---|
305 | |
---|
306 | #Neighbouring edges |
---|
307 | tid = 0 |
---|
308 | assert T.neighbour_edges[tid, 0] < 0 #Opposite point b (0,2) |
---|
309 | assert T.neighbour_edges[tid, 1] == 2 #Opposite point a (0,0) |
---|
310 | assert T.neighbour_edges[tid, 2] < 0 #Opposite point c (2,0) |
---|
311 | |
---|
312 | tid = 1 |
---|
313 | assert T.neighbour_edges[tid, 0] == 2 #Opposite point b (0,2) |
---|
314 | assert T.neighbour_edges[tid, 1] == 0 #Opposite point c (2,0) |
---|
315 | assert T.neighbour_edges[tid, 2] == 1 #Opposite point e (2,2) |
---|
316 | |
---|
317 | tid = 2 |
---|
318 | assert T.neighbour_edges[tid, 0] < 0 #Opposite point e (2,2) |
---|
319 | assert T.neighbour_edges[tid, 1] < 0 #Opposite point c (2,0) |
---|
320 | assert T.neighbour_edges[tid, 2] == 0 #Opposite point f (4,0) |
---|
321 | |
---|
322 | tid = 3 |
---|
323 | assert T.neighbour_edges[tid, 0] == 1 #Opposite point d (0,4) |
---|
324 | assert T.neighbour_edges[tid, 1] < 0 #Opposite point b (0,3) |
---|
325 | assert T.neighbour_edges[tid, 2] < 0 #Opposite point e (2,2) |
---|
326 | |
---|
327 | |
---|
328 | def test_build_neighbour_structure_duplicates(self): |
---|
329 | p0 = [-66.0, 14.0] |
---|
330 | p1 = [14.0, -66.0] |
---|
331 | p2 = [14.0, 14.0] |
---|
332 | p3 = [60.0, 20.0] |
---|
333 | p4 = [10.0, 60.0] |
---|
334 | p5 = [60.0, 60.0] |
---|
335 | |
---|
336 | points = [p0, p1, p2, p3, p4, p5] |
---|
337 | triangles = [ [0, 1, 2], |
---|
338 | [3, 2, 1], |
---|
339 | [0, 2, 4], |
---|
340 | [0, 2, 4], |
---|
341 | [4, 2, 5], |
---|
342 | [5, 2, 3]] |
---|
343 | try: |
---|
344 | mesh = Mesh(points, triangles) |
---|
345 | except: |
---|
346 | pass |
---|
347 | else: |
---|
348 | raise "triangle edge duplicates not caught" |
---|
349 | |
---|
350 | def test_rectangular_mesh_basic(self): |
---|
351 | M=1 |
---|
352 | N=1 |
---|
353 | |
---|
354 | points, vertices, boundary = rectangular(M, N) |
---|
355 | mesh = Mesh(points, vertices, boundary) |
---|
356 | |
---|
357 | #Test that points are arranged in a counter clock wise order |
---|
358 | mesh.check_integrity() |
---|
359 | |
---|
360 | M=2 |
---|
361 | N=2 |
---|
362 | points, vertices, boundary = rectangular(M, N) |
---|
363 | mesh = Mesh(points, vertices, boundary) |
---|
364 | |
---|
365 | #Test that points are arranged in a counter clock wise order |
---|
366 | mesh.check_integrity() |
---|
367 | |
---|
368 | #assert mesh.boundary[(7,1)] == 2 # top |
---|
369 | assert mesh.boundary[(7,1)] == 'top' # top |
---|
370 | assert mesh.boundary[(3,1)] == 'top' # top |
---|
371 | |
---|
372 | |
---|
373 | def test_boundary_tags(self): |
---|
374 | |
---|
375 | |
---|
376 | points, vertices, boundary = rectangular(4, 4) |
---|
377 | mesh = Mesh(points, vertices, boundary) |
---|
378 | |
---|
379 | |
---|
380 | #Test that points are arranged in a counter clock wise order |
---|
381 | mesh.check_integrity() |
---|
382 | |
---|
383 | #print mesh.get_boundary_tags() |
---|
384 | #print mesh.boundary |
---|
385 | |
---|
386 | for k in [1, 3, 5, 7]: |
---|
387 | assert mesh.boundary[(k,2)] == 'left' |
---|
388 | |
---|
389 | for k in [24, 26, 28, 30]: |
---|
390 | assert mesh.boundary[(k,2)] == 'right' |
---|
391 | |
---|
392 | for k in [7, 15, 23, 31]: |
---|
393 | assert mesh.boundary[(k,1)] == 'top' |
---|
394 | for k in [0, 8, 16, 24]: |
---|
395 | assert mesh.boundary[(k,1)] == 'bottom' |
---|
396 | |
---|
397 | |
---|
398 | |
---|
399 | def test_rectangular_mesh(self): |
---|
400 | M=4 |
---|
401 | N=16 |
---|
402 | len1 = 100.0 |
---|
403 | len2 = 17.0 |
---|
404 | |
---|
405 | points, vertices, boundary = rectangular(M, N, len1, len2) |
---|
406 | mesh = Mesh(points, vertices, boundary) |
---|
407 | |
---|
408 | assert len(mesh) == 2*M*N |
---|
409 | |
---|
410 | for i in range(len(mesh)): |
---|
411 | assert mesh.areas[i] == len1*len2/(2*M*N) |
---|
412 | |
---|
413 | hypo = sqrt((len1/M)**2 + (len2/N)**2) #hypothenuse |
---|
414 | assert mesh.edgelengths[i, 0] == hypo |
---|
415 | assert mesh.edgelengths[i, 1] == len1/M #x direction |
---|
416 | assert mesh.edgelengths[i, 2] == len2/N #y direction |
---|
417 | |
---|
418 | #Test that points are arranged in a counter clock wise order |
---|
419 | mesh.check_integrity() |
---|
420 | |
---|
421 | |
---|
422 | def test_rectangular_mesh2(self): |
---|
423 | #Check that integers don't cause trouble |
---|
424 | N = 16 |
---|
425 | |
---|
426 | points, vertices, boundary = rectangular(2*N, N, len1=10, len2=10) |
---|
427 | mesh = Mesh(points, vertices, boundary) |
---|
428 | |
---|
429 | |
---|
430 | |
---|
431 | def test_surrogate_neighbours(self): |
---|
432 | a = [0.0, 0.0] |
---|
433 | b = [0.0, 2.0] |
---|
434 | c = [2.0,0.0] |
---|
435 | d = [0.0, 4.0] |
---|
436 | e = [2.0, 2.0] |
---|
437 | f = [4.0,0.0] |
---|
438 | |
---|
439 | points = [a, b, c, d, e, f] |
---|
440 | |
---|
441 | #bac, bce, ecf, dbe |
---|
442 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
443 | mesh = Mesh(points, vertices) |
---|
444 | mesh.check_integrity() |
---|
445 | |
---|
446 | |
---|
447 | T = mesh |
---|
448 | tid = 0 |
---|
449 | assert T.number_of_boundaries[tid] == 2 |
---|
450 | assert T.surrogate_neighbours[tid, 0] == tid |
---|
451 | assert T.surrogate_neighbours[tid, 1] == 1 |
---|
452 | assert T.surrogate_neighbours[tid, 2] == tid |
---|
453 | |
---|
454 | tid = 1 |
---|
455 | assert T.number_of_boundaries[tid] == 0 |
---|
456 | assert T.surrogate_neighbours[tid, 0] == 2 |
---|
457 | assert T.surrogate_neighbours[tid, 1] == 3 |
---|
458 | assert T.surrogate_neighbours[tid, 2] == 0 |
---|
459 | |
---|
460 | tid = 2 |
---|
461 | assert T.number_of_boundaries[tid] == 2 |
---|
462 | assert T.surrogate_neighbours[tid, 0] == tid |
---|
463 | assert T.surrogate_neighbours[tid, 1] == tid |
---|
464 | assert T.surrogate_neighbours[tid, 2] == 1 |
---|
465 | |
---|
466 | tid = 3 |
---|
467 | assert T.number_of_boundaries[tid] == 2 |
---|
468 | assert T.surrogate_neighbours[tid, 0] == 1 |
---|
469 | assert T.surrogate_neighbours[tid, 1] == tid |
---|
470 | assert T.surrogate_neighbours[tid, 2] == tid |
---|
471 | |
---|
472 | |
---|
473 | def test_boundary_inputs(self): |
---|
474 | a = [0.0, 0.0] |
---|
475 | b = [0.0, 2.0] |
---|
476 | c = [2.0,0.0] |
---|
477 | d = [0.0, 4.0] |
---|
478 | e = [2.0, 2.0] |
---|
479 | f = [4.0,0.0] |
---|
480 | |
---|
481 | points = [a, b, c, d, e, f] |
---|
482 | |
---|
483 | #bac, bce, ecf, dbe |
---|
484 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
485 | |
---|
486 | boundary = { (0, 0): 'First', |
---|
487 | (0, 2): 'Second', |
---|
488 | (2, 0): 'Third', |
---|
489 | (2, 1): 'Fourth', |
---|
490 | (3, 1): 'Fifth', |
---|
491 | (3, 2): 'Sixth'} |
---|
492 | |
---|
493 | |
---|
494 | mesh = Mesh(points, vertices, boundary) |
---|
495 | mesh.check_integrity() |
---|
496 | |
---|
497 | |
---|
498 | #Check enumeration |
---|
499 | #for k, (vol_id, edge_id) in enumerate(mesh.boundary_segments): |
---|
500 | # b = -k-1 |
---|
501 | # assert mesh.neighbours[vol_id, edge_id] == b |
---|
502 | |
---|
503 | |
---|
504 | |
---|
505 | def test_boundary_inputs_using_one_default(self): |
---|
506 | a = [0.0, 0.0] |
---|
507 | b = [0.0, 2.0] |
---|
508 | c = [2.0,0.0] |
---|
509 | d = [0.0, 4.0] |
---|
510 | e = [2.0, 2.0] |
---|
511 | f = [4.0,0.0] |
---|
512 | |
---|
513 | points = [a, b, c, d, e, f] |
---|
514 | |
---|
515 | #bac, bce, ecf, dbe |
---|
516 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
517 | |
---|
518 | boundary = { (0, 0): 'First', |
---|
519 | (0, 2): 'Second', |
---|
520 | (2, 0): 'Third', |
---|
521 | (2, 1): 'Fourth', |
---|
522 | #(3, 1): 'Fifth', #Skip this |
---|
523 | (3, 2): 'Sixth'} |
---|
524 | |
---|
525 | |
---|
526 | mesh = Mesh(points, vertices, boundary) |
---|
527 | mesh.check_integrity() |
---|
528 | |
---|
529 | from config import default_boundary_tag |
---|
530 | assert mesh.boundary[ (3, 1) ] == default_boundary_tag |
---|
531 | |
---|
532 | |
---|
533 | #Check enumeration |
---|
534 | #for k, (vol_id, edge_id) in enumerate(mesh.boundary_segments): |
---|
535 | # b = -k-1 |
---|
536 | # assert mesh.neighbours[vol_id, edge_id] == b |
---|
537 | |
---|
538 | def test_boundary_inputs_using_all_defaults(self): |
---|
539 | a = [0.0, 0.0] |
---|
540 | b = [0.0, 2.0] |
---|
541 | c = [2.0,0.0] |
---|
542 | d = [0.0, 4.0] |
---|
543 | e = [2.0, 2.0] |
---|
544 | f = [4.0,0.0] |
---|
545 | |
---|
546 | points = [a, b, c, d, e, f] |
---|
547 | |
---|
548 | #bac, bce, ecf, dbe |
---|
549 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
550 | |
---|
551 | boundary = { (0, 0): 'First', |
---|
552 | (0, 2): 'Second', |
---|
553 | (2, 0): 'Third', |
---|
554 | (2, 1): 'Fourth', |
---|
555 | #(3, 1): 'Fifth', #Skip this |
---|
556 | (3, 2): 'Sixth'} |
---|
557 | |
---|
558 | |
---|
559 | mesh = Mesh(points, vertices) #, boundary) |
---|
560 | mesh.check_integrity() |
---|
561 | |
---|
562 | from config import default_boundary_tag |
---|
563 | assert mesh.boundary[ (0, 0) ] == default_boundary_tag |
---|
564 | assert mesh.boundary[ (0, 2) ] == default_boundary_tag |
---|
565 | assert mesh.boundary[ (2, 0) ] == default_boundary_tag |
---|
566 | assert mesh.boundary[ (2, 1) ] == default_boundary_tag |
---|
567 | assert mesh.boundary[ (3, 1) ] == default_boundary_tag |
---|
568 | assert mesh.boundary[ (3, 2) ] == default_boundary_tag |
---|
569 | |
---|
570 | |
---|
571 | #Check enumeration |
---|
572 | #for k, (vol_id, edge_id) in enumerate(mesh.boundary_segments): |
---|
573 | # b = -k-1 |
---|
574 | # assert mesh.neighbours[vol_id, edge_id] == b |
---|
575 | |
---|
576 | |
---|
577 | |
---|
578 | |
---|
579 | |
---|
580 | |
---|
581 | def test_inputs(self): |
---|
582 | a = [0.0, 0.0] |
---|
583 | b = [0.0, 2.0] |
---|
584 | c = [2.0,0.0] |
---|
585 | d = [0.0, 4.0] |
---|
586 | e = [2.0, 2.0] |
---|
587 | f = [4.0,0.0] |
---|
588 | |
---|
589 | points = [a, b, c, d, e, f] |
---|
590 | |
---|
591 | #bac, bce, ecf, dbe |
---|
592 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] |
---|
593 | |
---|
594 | #Too few points |
---|
595 | try: |
---|
596 | mesh = Mesh([points[0]], vertices) |
---|
597 | except AssertionError: |
---|
598 | pass |
---|
599 | else: |
---|
600 | raise 'Should have raised an exception' |
---|
601 | |
---|
602 | #Too few points - 1 element |
---|
603 | try: |
---|
604 | mesh = Mesh([points[0]], [vertices[0]]) |
---|
605 | except AssertionError: |
---|
606 | pass |
---|
607 | else: |
---|
608 | raise 'Should have raised an exception' |
---|
609 | |
---|
610 | #Wrong dimension of vertices |
---|
611 | try: |
---|
612 | mesh = Mesh(points, vertices[0]) |
---|
613 | except AssertionError: |
---|
614 | pass |
---|
615 | else: |
---|
616 | raise 'Should have raised an exception' |
---|
617 | |
---|
618 | #Unsubscriptable coordinates object raises exception |
---|
619 | try: |
---|
620 | mesh = Mesh(points[0], [vertices[0]]) |
---|
621 | except AssertionError: |
---|
622 | pass |
---|
623 | else: |
---|
624 | raise 'Should have raised an exception' |
---|
625 | |
---|
626 | #FIXME: This has been commented out pending a decision |
---|
627 | #whether to allow partial boundary tags or not |
---|
628 | # |
---|
629 | #Not specifying all boundary tags |
---|
630 | #try: |
---|
631 | # mesh = Mesh(points, vertices, {(3,0): 'x'}) |
---|
632 | #except AssertionError: |
---|
633 | # pass |
---|
634 | #else: |
---|
635 | # raise 'Should have raised an exception' |
---|
636 | |
---|
637 | #Specifying wrong non existing segment |
---|
638 | try: |
---|
639 | mesh = Mesh(points, vertices, {(5,0): 'x'}) |
---|
640 | except AssertionError: |
---|
641 | pass |
---|
642 | else: |
---|
643 | raise 'Should have raised an exception' |
---|
644 | |
---|
645 | |
---|
646 | |
---|
647 | |
---|
648 | def test_internal_boundaries(self): |
---|
649 | """ |
---|
650 | get values based on triangle lists. |
---|
651 | """ |
---|
652 | from mesh_factory import rectangular |
---|
653 | from shallow_water import Domain |
---|
654 | from Numeric import zeros, Float |
---|
655 | |
---|
656 | #Create basic mesh |
---|
657 | points, vertices, boundary = rectangular(1, 3) |
---|
658 | |
---|
659 | # Add an internal boundary |
---|
660 | boundary[(2,0)] = 'internal' |
---|
661 | boundary[(1,0)] = 'internal' |
---|
662 | |
---|
663 | #Create shallow water domain |
---|
664 | domain = Domain(points, vertices, boundary) |
---|
665 | domain.build_tagged_elements_dictionary({'bottom':[0,1], |
---|
666 | 'top':[4,5], |
---|
667 | 'all':[0,1,2,3,4,5]}) |
---|
668 | |
---|
669 | |
---|
670 | def test_boundary_polygon(self): |
---|
671 | from mesh_factory import rectangular |
---|
672 | from mesh import Mesh |
---|
673 | from Numeric import zeros, Float |
---|
674 | from util import inside_polygon |
---|
675 | |
---|
676 | #Create basic mesh |
---|
677 | points, vertices, boundary = rectangular(2, 2) |
---|
678 | mesh = Mesh(points, vertices, boundary) |
---|
679 | |
---|
680 | |
---|
681 | P = mesh.get_boundary_polygon() |
---|
682 | |
---|
683 | assert len(P) == 8 |
---|
684 | assert allclose(P, [[0.0, 0.0], [0.5, 0.0], [1.0, 0.0], |
---|
685 | [1.0, 0.5], [1.0, 1.0], [0.5, 1.0], |
---|
686 | [0.0, 1.0], [0.0, 0.5]]) |
---|
687 | for p in points: |
---|
688 | #print p, P |
---|
689 | assert inside_polygon(p, P) |
---|
690 | |
---|
691 | |
---|
692 | def test_boundary_polygon_II(self): |
---|
693 | from mesh import Mesh |
---|
694 | from Numeric import zeros, Float |
---|
695 | from util import inside_polygon |
---|
696 | |
---|
697 | #Points |
---|
698 | a = [0.0, 0.0] #0 |
---|
699 | b = [0.0, 0.5] #1 |
---|
700 | c = [0.0, 1.0] #2 |
---|
701 | d = [0.5, 0.0] #3 |
---|
702 | e = [0.5, 0.5] #4 |
---|
703 | f = [1.0, 0.0] #5 |
---|
704 | g = [1.0, 0.5] #6 |
---|
705 | h = [1.0, 1.0] #7 |
---|
706 | i = [1.5, 0.5] #8 |
---|
707 | |
---|
708 | points = [a, b, c, d, e, f, g, h, i] |
---|
709 | |
---|
710 | #dea, bae, bec, fgd, |
---|
711 | #edg, ghe, gfi, gih |
---|
712 | vertices = [ [3,4,0], [1,0,4], [1,4,2], [5,6,3], |
---|
713 | [4,3,6], [6,7,4], [6,5,8], [6,8,7]] |
---|
714 | |
---|
715 | mesh = Mesh(points, vertices) |
---|
716 | |
---|
717 | mesh.check_integrity() |
---|
718 | |
---|
719 | P = mesh.get_boundary_polygon() |
---|
720 | |
---|
721 | assert len(P) == 8 |
---|
722 | assert allclose(P, [a, d, f, i, h, e, c, b]) |
---|
723 | |
---|
724 | for p in points: |
---|
725 | #print p, P |
---|
726 | assert inside_polygon(p, P) |
---|
727 | |
---|
728 | |
---|
729 | def test_boundary_polygon_III(self): |
---|
730 | """Same as II but vertices ordered differently |
---|
731 | """ |
---|
732 | |
---|
733 | from mesh import Mesh |
---|
734 | from Numeric import zeros, Float |
---|
735 | from util import inside_polygon |
---|
736 | |
---|
737 | #Points |
---|
738 | a = [0.0, 0.0] #0 |
---|
739 | b = [0.0, 0.5] #1 |
---|
740 | c = [0.0, 1.0] #2 |
---|
741 | d = [0.5, 0.0] #3 |
---|
742 | e = [0.5, 0.5] #4 |
---|
743 | f = [1.0, 0.0] #5 |
---|
744 | g = [1.0, 0.5] #6 |
---|
745 | h = [1.0, 1.0] #7 |
---|
746 | i = [1.5, 0.5] #8 |
---|
747 | |
---|
748 | points = [a, b, c, d, e, f, g, h, i] |
---|
749 | |
---|
750 | #edg, ghe, gfi, gih |
---|
751 | #dea, bae, bec, fgd, |
---|
752 | vertices = [[4,3,6], [6,7,4], [6,5,8], [6,8,7], |
---|
753 | [3,4,0], [1,0,4], [1,4,2], [5,6,3]] |
---|
754 | |
---|
755 | |
---|
756 | mesh = Mesh(points, vertices) |
---|
757 | mesh.check_integrity() |
---|
758 | |
---|
759 | |
---|
760 | P = mesh.get_boundary_polygon() |
---|
761 | |
---|
762 | assert len(P) == 8 |
---|
763 | assert allclose(P, [a, d, f, i, h, e, c, b]) |
---|
764 | |
---|
765 | for p in points: |
---|
766 | assert inside_polygon(p, P) |
---|
767 | |
---|
768 | |
---|
769 | |
---|
770 | #------------------------------------------------------------- |
---|
771 | if __name__ == "__main__": |
---|
772 | suite = unittest.makeSuite(Test_Mesh,'test') |
---|
773 | runner = unittest.TextTestRunner() |
---|
774 | runner.run(suite) |
---|
775 | |
---|
776 | |
---|
777 | |
---|
778 | |
---|
779 | |
---|