1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import unittest |
---|
4 | import sys |
---|
5 | import os |
---|
6 | from math import sqrt |
---|
7 | |
---|
8 | |
---|
9 | from anuga import Domain |
---|
10 | from anuga import rectangular_cross |
---|
11 | |
---|
12 | from anuga_parallel.distribute_mesh import pmesh_divide_metis |
---|
13 | from anuga_parallel.distribute_mesh import build_submesh |
---|
14 | from anuga_parallel.distribute_mesh import submesh_full, submesh_ghost, submesh_quantities |
---|
15 | from anuga_parallel.distribute_mesh import extract_hostmesh, rec_submesh, send_submesh |
---|
16 | |
---|
17 | from anuga_parallel.interface import myid, numprocs, barrier, finalize |
---|
18 | |
---|
19 | import numpy as num |
---|
20 | |
---|
21 | |
---|
22 | def topography(x,y): |
---|
23 | return -x/2 |
---|
24 | |
---|
25 | |
---|
26 | def xcoord(x,y): |
---|
27 | return x |
---|
28 | |
---|
29 | def ycoord(x,y): |
---|
30 | return y |
---|
31 | |
---|
32 | |
---|
33 | def distibute_three_processors(): |
---|
34 | """ |
---|
35 | Do a parallel test of distributing a rectangle onto 3 processors |
---|
36 | """ |
---|
37 | |
---|
38 | import pypar |
---|
39 | |
---|
40 | myid = pypar.rank() |
---|
41 | numprocs = pypar.size() |
---|
42 | |
---|
43 | if not numprocs == 3: return |
---|
44 | |
---|
45 | #print numprocs |
---|
46 | |
---|
47 | barrier() |
---|
48 | |
---|
49 | if myid == 0: |
---|
50 | |
---|
51 | points, vertices, boundary = rectangular_cross(2,2) |
---|
52 | |
---|
53 | domain = Domain(points, vertices, boundary) |
---|
54 | |
---|
55 | |
---|
56 | domain.set_quantity('elevation', topography) # Use function for elevation |
---|
57 | domain.set_quantity('friction', 0.0) # Constant friction |
---|
58 | domain.set_quantity('stage', expression='elevation') # Dry initial stage |
---|
59 | domain.set_quantity('xmomentum', expression='friction + 2.0') # |
---|
60 | domain.set_quantity('ymomentum', ycoord) # |
---|
61 | |
---|
62 | #---------------------------------------------------------------------------------- |
---|
63 | # Test pmesh_divide_metis |
---|
64 | #---------------------------------------------------------------------------------- |
---|
65 | nodes, triangles, boundary, triangles_per_proc, quantities = pmesh_divide_metis(domain,numprocs) |
---|
66 | |
---|
67 | assert_(num.allclose(nodes,points)) |
---|
68 | |
---|
69 | true_vertices = [[0, 9, 1], [3, 9, 0], [4, 9, 3], [1, 9, 4], [1, 10, 2], [4, 10, 1], [5, 10, 4], [2, 10, 5], [3, 11, 4], [6, 11, 3], [7, 11, 6], [4, 11, 7], [4, 12, 5], [7, 12, 4], [8, 12, 7], [5, 12, 8]] |
---|
70 | |
---|
71 | true_triangles = [[4, 9, 3], [4, 12, 5], [7, 12, 4], [8, 12, 7], [5, 12, 8], [0, 9, 1], [1, 9, 4], [1, 10, 2], [4, 10, 1], [5, 10, 4], [2, 10, 5], [3, 9, 0], [3, 11, 4], [6, 11, 3], [7, 11, 6], [4, 11, 7]] |
---|
72 | |
---|
73 | assert_(num.allclose(vertices,true_vertices)) |
---|
74 | assert_(num.allclose(triangles,true_triangles)) |
---|
75 | |
---|
76 | assert_(num.allclose(triangles_per_proc,[5,6,5])) |
---|
77 | |
---|
78 | |
---|
79 | #---------------------------------------------------------------------------------- |
---|
80 | # Test build_submesh |
---|
81 | #---------------------------------------------------------------------------------- |
---|
82 | submesh = build_submesh(nodes, triangles, boundary, quantities, triangles_per_proc) |
---|
83 | |
---|
84 | |
---|
85 | assert_(num.allclose(submesh['full_nodes'][0],[[3.0, 0.5, 0.0], [4.0, 0.5, 0.5], [5.0, 0.5, 1.0], [7.0, 1.0, 0.5], [8.0, 1.0, 1.0], [9.0, 0.25, 0.25], [12.0, 0.75, 0.75]])) |
---|
86 | assert_(num.allclose(submesh['full_nodes'][1],[[0.0, 0.0, 0.0], [1.0, 0.0, 0.5], [2.0, 0.0, 1.0], [4.0, 0.5, 0.5], [5.0, 0.5, 1.0], [9.0, 0.25, 0.25], [10.0, 0.25, 0.75]])) |
---|
87 | assert_(num.allclose(submesh['full_nodes'][2],[[0.0, 0.0, 0.0], [3.0, 0.5, 0.0], [4.0, 0.5, 0.5], [6.0, 1.0, 0.0], [7.0, 1.0, 0.5], [9.0, 0.25, 0.25], [11.0, 0.75, 0.25]])) |
---|
88 | |
---|
89 | |
---|
90 | assert_(num.allclose(submesh['ghost_nodes'][0],[[0.0, 0.0, 0.0], [1.0, 0.0, 0.5], [2.0, 0.0, 1.0], [6.0, 1.0, 0.0], [10.0, 0.25, 0.75], [11.0, 0.75, 0.25]])) |
---|
91 | assert_(num.allclose(submesh['ghost_nodes'][1],[[3.0, 0.5, 0.0], [7.0, 1.0, 0.5], [8.0, 1.0, 1.0], [11.0, 0.75, 0.25], [12.0, 0.75, 0.75]])) |
---|
92 | assert_(num.allclose(submesh['ghost_nodes'][2],[[1.0, 0.0, 0.5], [5.0, 0.5, 1.0], [8.0, 1.0, 1.0], [12.0, 0.75, 0.75]])) |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | true_full_triangles = [num.array([[ 4, 9, 3], |
---|
97 | [ 4, 12, 5], |
---|
98 | [ 7, 12, 4], |
---|
99 | [ 8, 12, 7], |
---|
100 | [ 5, 12, 8]]), |
---|
101 | num.array([[ 0, 9, 1], |
---|
102 | [ 1, 9, 4], |
---|
103 | [ 1, 10, 2], |
---|
104 | [ 4, 10, 1], |
---|
105 | [ 5, 10, 4], |
---|
106 | [ 2, 10, 5]]), |
---|
107 | num.array([[ 3, 9, 0], |
---|
108 | [ 3, 11, 4], |
---|
109 | [ 6, 11, 3], |
---|
110 | [ 7, 11, 6], |
---|
111 | [ 4, 11, 7]])] |
---|
112 | |
---|
113 | |
---|
114 | assert_(num.allclose(submesh['full_triangles'][0],true_full_triangles[0])) |
---|
115 | assert_(num.allclose(submesh['full_triangles'][1],true_full_triangles[1])) |
---|
116 | assert_(num.allclose(submesh['full_triangles'][2],true_full_triangles[2])) |
---|
117 | |
---|
118 | true_ghost_triangles = [num.array([[ 5, 0, 9, 1], |
---|
119 | [ 6, 1, 9, 4], |
---|
120 | [ 8, 4, 10, 1], |
---|
121 | [ 9, 5, 10, 4], |
---|
122 | [10, 2, 10, 5], |
---|
123 | [11, 3, 9, 0], |
---|
124 | [12, 3, 11, 4], |
---|
125 | [13, 6, 11, 3], |
---|
126 | [14, 7, 11, 6], |
---|
127 | [15, 4, 11, 7]]), |
---|
128 | num.array([[ 0, 4, 9, 3], |
---|
129 | [ 1, 4, 12, 5], |
---|
130 | [ 2, 7, 12, 4], |
---|
131 | [ 4, 5, 12, 8], |
---|
132 | [11, 3, 9, 0], |
---|
133 | [12, 3, 11, 4]]), |
---|
134 | num.array([[ 0, 4, 9, 3], |
---|
135 | [ 1, 4, 12, 5], |
---|
136 | [ 2, 7, 12, 4], |
---|
137 | [ 3, 8, 12, 7], |
---|
138 | [ 5, 0, 9, 1], |
---|
139 | [ 6, 1, 9, 4]])] |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | assert_(num.allclose(submesh['ghost_triangles'][0],true_ghost_triangles[0])) |
---|
144 | assert_(num.allclose(submesh['ghost_triangles'][1],true_ghost_triangles[1])) |
---|
145 | assert_(num.allclose(submesh['ghost_triangles'][2],true_ghost_triangles[2])) |
---|
146 | |
---|
147 | true_full_commun = [{0: [1, 2], 1: [1, 2], 2: [1, 2], 3: [2], 4: [1]}, {5: [0, 2], 6: [0, 2], 7: [], 8: [0], 9: [0], 10: [0]}, {11: [0, 1], 12: [0, 1], 13: [0], 14: [0], 15: [0]}] |
---|
148 | |
---|
149 | assert_(true_full_commun == submesh['full_commun']) |
---|
150 | |
---|
151 | |
---|
152 | true_ghost_commun = [num.array([[ 5, 1], |
---|
153 | [ 6, 1], |
---|
154 | [ 8, 1], |
---|
155 | [ 9, 1], |
---|
156 | [10, 1], |
---|
157 | [11, 2], |
---|
158 | [12, 2], |
---|
159 | [13, 2], |
---|
160 | [14, 2], |
---|
161 | [15, 2]]), |
---|
162 | num.array([[ 0, 0], |
---|
163 | [ 1, 0], |
---|
164 | [ 2, 0], |
---|
165 | [ 4, 0], |
---|
166 | [11, 2], |
---|
167 | [12, 2]]), |
---|
168 | num.array([[0, 0], |
---|
169 | [1, 0], |
---|
170 | [2, 0], |
---|
171 | [3, 0], |
---|
172 | [5, 1], |
---|
173 | [6, 1]])] |
---|
174 | |
---|
175 | assert_(num.allclose(submesh['ghost_commun'][0],true_ghost_commun[0])) |
---|
176 | assert_(num.allclose(submesh['ghost_commun'][1],true_ghost_commun[1])) |
---|
177 | assert_(num.allclose(submesh['ghost_commun'][2],true_ghost_commun[2])) |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | barrier() |
---|
182 | #-------------------------------- |
---|
183 | # Now do the comunnication part |
---|
184 | #-------------------------------- |
---|
185 | |
---|
186 | |
---|
187 | if myid == 0: |
---|
188 | #---------------------------------------------------------------------------------- |
---|
189 | # Test send_submesh |
---|
190 | #---------------------------------------------------------------------------------- |
---|
191 | for p in range(1, numprocs): |
---|
192 | send_submesh(submesh, triangles_per_proc, p, verbose=False) |
---|
193 | |
---|
194 | #---------------------------------------------------------------------------------- |
---|
195 | # Test extract_hostmesh |
---|
196 | #---------------------------------------------------------------------------------- |
---|
197 | points, vertices, boundary, quantities, ghost_recv_dict, full_send_dict =\ |
---|
198 | extract_hostmesh(submesh, triangles_per_proc) |
---|
199 | |
---|
200 | |
---|
201 | true_points = [[0.5, 0.0], [0.5, 0.5], [0.5, 1.0], [1.0, 0.5], [1.0, 1.0], [0.25, 0.25], [0.75, 0.75], [0.0, 0.0], [0.0, 0.5], [0.0, 1.0], [1.0, 0.0], [0.25, 0.75], [0.75, 0.25]] |
---|
202 | |
---|
203 | true_vertices = [[1, 5, 0], [1, 6, 2], [3, 6, 1], [4, 6, 3], [2, 6, 4], [7, 5, 8], [8, 5, 1], [1, 11, 8], [2, 11, 1], [9, 11, 2], [0, 5, 7], [0, 12, 1], [10, 12, 0], [3, 12, 10], [1, 12, 3]] |
---|
204 | |
---|
205 | |
---|
206 | true_ghost_recv = {1: [num.array([5, 6, 7, 8, 9]), num.array([ 5, 6, 8, 9, 10])], 2: [num.array([10, 11, 12, 13, 14]), num.array([11, 12, 13, 14, 15])]} |
---|
207 | |
---|
208 | |
---|
209 | true_full_send = {1: [num.array([0, 1, 2, 4]), num.array([0, 1, 2, 4])], 2: [num.array([0, 1, 2, 3]), num.array([0, 1, 2, 3])]} |
---|
210 | |
---|
211 | assert_(num.allclose(points, true_points)) |
---|
212 | assert_(num.allclose(vertices, true_vertices)) |
---|
213 | assert_(num.allclose(ghost_recv_dict[1],true_ghost_recv[1])) |
---|
214 | assert_(num.allclose(ghost_recv_dict[2],true_ghost_recv[2])) |
---|
215 | assert_(num.allclose(full_send_dict[1],true_full_send[1])) |
---|
216 | assert_(num.allclose(full_send_dict[2],true_full_send[2])) |
---|
217 | |
---|
218 | #print triangles_per_proc |
---|
219 | |
---|
220 | else: |
---|
221 | #---------------------------------------------------------------------------------- |
---|
222 | # Test rec_submesh |
---|
223 | #---------------------------------------------------------------------------------- |
---|
224 | points, vertices, boundary, quantities, ghost_recv_dict, full_send_dict, no_full_nodes, no_full_trigs = rec_submesh(0, verbose=False) |
---|
225 | |
---|
226 | if myid == 1: |
---|
227 | |
---|
228 | |
---|
229 | true_points = [[0.0, 0.0], [0.0, 0.5], [0.0, 1.0], [0.5, 0.5], [0.5, 1.0], [0.25, 0.25], [0.25, 0.75], [0.5, 0.0], [1.0, 0.5], [1.0, 1.0], [0.75, 0.25], [0.75, 0.75]] |
---|
230 | |
---|
231 | true_vertices = [[0, 5, 1], [1, 5, 3], [1, 6, 2], [3, 6, 1], [4, 6, 3], [2, 6, 4], [3, 5, 7], [3, 11, 4], [8, 11, 3], [4, 11, 9], [7, 5, 0], [7, 10, 3]] |
---|
232 | |
---|
233 | true_ghost_recv = {0: [num.array([6, 7, 8, 9]), num.array([0, 1, 2, 4])], 2: [num.array([10, 11]), num.array([11, 12])]} |
---|
234 | |
---|
235 | true_full_send = {0: [num.array([0, 1, 3, 4, 5]), num.array([ 5, 6, 8, 9, 10])], 2: [num.array([0, 1]), num.array([5, 6])]} |
---|
236 | |
---|
237 | assert_(num.allclose(points, true_points)) |
---|
238 | assert_(num.allclose(vertices, true_vertices)) |
---|
239 | assert_(num.allclose(ghost_recv_dict[0],true_ghost_recv[0])) |
---|
240 | assert_(num.allclose(ghost_recv_dict[2],true_ghost_recv[2])) |
---|
241 | assert_(num.allclose(full_send_dict[0],true_full_send[0])) |
---|
242 | assert_(num.allclose(full_send_dict[2],true_full_send[2])) |
---|
243 | |
---|
244 | |
---|
245 | if myid == 2: |
---|
246 | |
---|
247 | true_points = [[0.0, 0.0], [0.5, 0.0], [0.5, 0.5], [1.0, 0.0], [1.0, 0.5], [0.25, 0.25], [0.75, 0.25], [0.0, 0.5], [0.5, 1.0], [1.0, 1.0], [0.75, 0.75]] |
---|
248 | |
---|
249 | true_vertices = [[1, 5, 0], [1, 6, 2], [3, 6, 1], [4, 6, 3], [2, 6, 4], [2, 5, 1], [2, 10, 8], [4, 10, 2], [9, 10, 4], [0, 5, 7], [7, 5, 2]] |
---|
250 | |
---|
251 | |
---|
252 | true_ghost_recv = {0: [num.array([5, 6, 7, 8]), num.array([0, 1, 2, 3])], 1: [num.array([ 9, 10]), num.array([5, 6])]} |
---|
253 | |
---|
254 | true_full_send = {0: [num.array([0, 1, 2, 3, 4]), num.array([11, 12, 13, 14, 15])], 1: [num.array([0, 1]), num.array([11, 12])]} |
---|
255 | |
---|
256 | |
---|
257 | assert_(num.allclose(points, true_points)) |
---|
258 | assert_(num.allclose(vertices, true_vertices)) |
---|
259 | assert_(num.allclose(ghost_recv_dict[0],true_ghost_recv[0])) |
---|
260 | assert_(num.allclose(ghost_recv_dict[1],true_ghost_recv[1])) |
---|
261 | assert_(num.allclose(full_send_dict[0],true_full_send[0])) |
---|
262 | assert_(num.allclose(full_send_dict[1],true_full_send[1])) |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | |
---|
269 | ############################################################### |
---|
270 | |
---|
271 | class Test_parallel_distribute_mesh(unittest.TestCase): |
---|
272 | |
---|
273 | def test_distribute_three_processors(self): |
---|
274 | # Expect this test to fail if not run from the parallel directory. |
---|
275 | result = os.system("mpirun -np 3 python test_parallel_distribute_mesh.py") |
---|
276 | assert_(result == 0) |
---|
277 | |
---|
278 | |
---|
279 | # Because we are doing assertions outside of the TestCase class |
---|
280 | # the PyUnit defined assert_ function can't be used. |
---|
281 | def assert_(condition, msg="Assertion Failed"): |
---|
282 | if condition == False: |
---|
283 | #import pypar |
---|
284 | #pypar.finalize() |
---|
285 | raise AssertionError, msg |
---|
286 | #import sys |
---|
287 | #sys.exit(1) |
---|
288 | |
---|
289 | |
---|
290 | |
---|
291 | |
---|
292 | |
---|
293 | |
---|
294 | |
---|
295 | #------------------------------------------------------------- |
---|
296 | if __name__=="__main__": |
---|
297 | if numprocs == 1: |
---|
298 | runner = unittest.TextTestRunner() |
---|
299 | suite = unittest.makeSuite(Test_parallel_distribute_mesh, 'test') |
---|
300 | runner.run(suite) |
---|
301 | else: |
---|
302 | distibute_three_processors() |
---|
303 | |
---|
304 | finalize() |
---|
305 | |
---|
306 | |
---|
307 | |
---|