source: trunk/anuga_core/source/anuga_parallel/test_parallel.py @ 7775

Last change on this file since 7775 was 7519, checked in by steve, 16 years ago

Have been playing with using a slope limited velocity to calculate
fluxes (hence the addition of evolved_quantities as well as conserved
quantities.

But the commit is to fix a problem Rudy found with sww2dem. Seems
numpy.array2string is a little too clever, in that it summarizes
output if there is a long sequence of zeros to
[0.0, 0.0, 0.0, ... 0.0, 0.0 ,0.0] To get around this I have added
a call to numpy.set_options(threshold=sys.max_int) to turn this
behaviour off!

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