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