source: trunk/anuga_core/source/anuga_parallel/test_parallel_distribute_mesh.py @ 8283

Last change on this file since 8283 was 8281, checked in by steve, 12 years ago

added in some parallel data to allow sww_merge to create a merged sww file

File size: 13.8 KB
Line 
1#!/usr/bin/env python
2
3import unittest
4import sys
5import os
6from math import sqrt
7
8       
9from anuga import Domain
10from anuga import rectangular_cross
11
12from anuga_parallel.distribute_mesh import pmesh_divide_metis
13from anuga_parallel.distribute_mesh import build_submesh
14from anuga_parallel.distribute_mesh import submesh_full, submesh_ghost, submesh_quantities
15from anuga_parallel.distribute_mesh import extract_hostmesh, rec_submesh, send_submesh
16
17from anuga_parallel import myid, numprocs, barrier, finalize
18
19import numpy as num
20
21
22def topography(x,y): 
23    return -x/2
24
25
26def xcoord(x,y):
27    return x
28
29def ycoord(x,y):
30    return y
31
32
33def 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, \
198                ghost_recv_dict, full_send_dict, tri_map, node_map  =\
199        extract_hostmesh(submesh, triangles_per_proc)
200
201
202        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]]
203
204        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]]
205
206
207        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])]}
208
209
210        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])]}
211
212        assert_(num.allclose(points,   true_points))
213        assert_(num.allclose(vertices, true_vertices))
214        assert_(num.allclose(ghost_recv_dict[1],true_ghost_recv[1]))
215        assert_(num.allclose(ghost_recv_dict[2],true_ghost_recv[2]))
216        assert_(num.allclose(full_send_dict[1],true_full_send[1]))
217        assert_(num.allclose(full_send_dict[2],true_full_send[2]))
218
219        #print triangles_per_proc
220
221    else:
222        #----------------------------------------------------------------------------------
223        # Test rec_submesh
224        #----------------------------------------------------------------------------------
225        points, vertices, boundary, quantities, \
226                ghost_recv_dict, full_send_dict, \
227                no_full_nodes, no_full_trigs, tri_map, node_map  = \
228                rec_submesh(0, verbose=False)   
229
230        if myid == 1:
231
232
233            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]] 
234
235            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]]
236
237            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])]}
238
239            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])]}
240
241            true_tri_map  = num.array([ 6,  7,  8, -1,  9,  0, 1,  2,  3,  4,  5, 10, 11])
242
243            true_node_map = num.array([ 0,  1,  2,  7,  3,  4, -1,  8,  9,  5,  6, 10, 11])
244
245            assert_(num.allclose(tri_map,   true_tri_map))
246            assert_(num.allclose(node_map,   true_node_map))
247            assert_(num.allclose(points,   true_points))
248            assert_(num.allclose(vertices, true_vertices))
249            assert_(num.allclose(ghost_recv_dict[0],true_ghost_recv[0]))
250            assert_(num.allclose(ghost_recv_dict[2],true_ghost_recv[2]))
251            assert_(num.allclose(full_send_dict[0],true_full_send[0]))
252            assert_(num.allclose(full_send_dict[2],true_full_send[2])) 
253
254
255        if myid == 2:
256
257            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]]
258
259            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]]
260
261            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])]}
262
263            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])]}
264
265            true_tri_map  = num.array([5, 6, 7, 8, -1, 9, 10, -1, -1, -1, -1, 0, 1, 2, 3, 4, -1])
266
267            true_node_map = num.array([ 0,  7, -1,  1,  2,  8 , 3,  4,  9,  5, -1,  6, 10])
268
269            assert_(num.allclose(tri_map,   true_tri_map))
270            assert_(num.allclose(node_map,   true_node_map))
271            assert_(num.allclose(points,   true_points))
272            assert_(num.allclose(vertices, true_vertices))
273            assert_(num.allclose(ghost_recv_dict[0],true_ghost_recv[0]))
274            assert_(num.allclose(ghost_recv_dict[1],true_ghost_recv[1]))
275            assert_(num.allclose(full_send_dict[0],true_full_send[0]))
276            assert_(num.allclose(full_send_dict[1],true_full_send[1])) 
277
278
279
280
281
282
283###############################################################
284
285class Test_parallel_distribute_mesh(unittest.TestCase):
286
287    def test_distribute_three_processors(self):
288        # Expect this test to fail if not run from the parallel directory.
289        result = os.system("mpirun -np 3 python test_parallel_distribute_mesh.py")
290        assert_(result == 0)
291
292
293# Because we are doing assertions outside of the TestCase class
294# the PyUnit defined assert_ function can't be used.
295def assert_(condition, msg="Assertion Failed"):
296    if condition == False:
297        #import pypar
298        #pypar.finalize()
299        raise AssertionError, msg
300        #import sys
301        #sys.exit(1)
302   
303
304
305
306
307
308
309#-------------------------------------------------------------
310if __name__=="__main__":
311    if numprocs == 1: 
312        runner = unittest.TextTestRunner()
313        suite = unittest.makeSuite(Test_parallel_distribute_mesh, 'test')
314        runner.run(suite)
315    else:
316        distibute_three_processors()
317
318    finalize()
319       
320
321       
Note: See TracBrowser for help on using the repository browser.