source: trunk/anuga_core/source/anuga_parallel/distribute_mesh.py @ 8545

Last change on this file since 8545 was 8545, checked in by steve, 13 years ago

Changed vanilla communication to array communication

File size: 41.9 KB
Line 
1#########################################################
2#
3#
4#  Read in a data file and subdivide the triangle list
5#
6#
7#  The final routine, pmesh_divide_metis, does automatic
8# grid partitioning. Once testing has finished on this
9# routine the others should be removed.
10#
11#  Authors: Linda Stals and Matthew Hardy, June 2005
12#  Modified: Linda Stals, Nov 2005
13#            Jack Kelly, Nov 2005
14#            Steve Roberts, Aug 2009 (updating to numpy)
15#
16#
17#########################################################
18
19
20import sys
21from os import sep
22from sys import path
23from math import floor
24
25import numpy as num
26
27from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
28from anuga import indent
29
30try:
31    import local_config as config
32except:
33    import anuga_parallel.config as config
34
35
36#########################################################
37#
38# If the triangles list is reordered, the quantities
39# assigned to the triangles must also be reorded.
40#
41# *) quantities contain the quantites in the old ordering
42# *) proc_sum[i] contains the number of triangles in
43# processor i
44# *) tri_index is a map from the old triangle ordering to
45# the new ordering, where the new number for triangle
46# i is proc_sum[tri_index[i][0]]+tri_index[i][1]
47#
48# -------------------------------------------------------
49#
50# *) The quantaties are returned in the new ordering
51#
52#########################################################
53
54def reorder(quantities, tri_index, proc_sum):
55
56    # Find the number triangles
57
58    N = len(tri_index)
59
60    # Temporary storage area
61
62    index = num.zeros(N, num.int)
63    q_reord = {}
64
65    # Find the new ordering of the triangles
66
67    for i in xrange(N):
68        bin = tri_index[i][0]
69        bin_off_set = tri_index[i][1]
70        index[i] = proc_sum[bin]+bin_off_set
71
72    # Reorder each quantity according to the new ordering
73
74    for k in quantities:
75        q_reord[k] = num.zeros((N, 3), num.float)
76        for i in range(N):
77            q_reord[k][index[i]]=quantities[k].vertex_values[i]
78    del index
79
80    return q_reord
81
82
83#########################################################
84#
85# Divide the mesh using a call to metis, through pymetis.
86#
87# -------------------------------------------------------
88#
89# *)  The nodes, triangles, boundary, and quantities are
90# returned. triangles_per_proc defines the subdivision.
91# The first triangles_per_proc[0] triangles are assigned
92# to processor 0, the next triangles_per_proc[1] are
93# assigned to processor 1 etc. The boundary and quantites
94# are ordered the same way as the triangles
95#
96#########################################################
97
98#path.append('..' + sep + 'pymetis')
99
100try:
101    from pymetis.metis import partMeshNodal
102except ImportError:
103    print "***************************************************"
104    print "         Metis is probably not compiled."
105    print "         Read \anuga_core\source\pymetis\README"
106    print "***************************************************"
107    raise ImportError
108
109def pmesh_divide_metis(domain, n_procs):
110    # Wrapper for old pmesh_divide_metis which does not return tri_index or r_tri_index
111    nodes, ttriangles, boundary, triangles_per_proc, quantities, tri_index, r_tri_index = pmesh_divide_metis_helper(domain, n_procs)
112    return nodes, ttriangles, boundary, triangles_per_proc, quantities
113
114def pmesh_divide_metis_with_map(domain, n_procs):
115    return pmesh_divide_metis_helper(domain, n_procs)
116
117def pmesh_divide_metis_helper(domain, n_procs):
118   
119    # Initialise the lists
120    # List, indexed by processor of # triangles.
121   
122    triangles_per_proc = []
123   
124    # List of lists, indexed by processor of vertex numbers
125   
126    tri_list = []
127
128    # Serial to Parallel and Parallel to Serial Triangle index maps
129    tri_index = {}
130    r_tri_index = {} # reverse tri index, parallel to serial triangle index mapping
131   
132    # List indexed by processor of cumulative total of triangles allocated.
133   
134    proc_sum = []
135    for i in xrange(n_procs):
136        tri_list.append([])
137        triangles_per_proc.append(0)
138        proc_sum.append([])
139
140    # Prepare variables for the metis call
141   
142    n_tri = len(domain.triangles)
143    if n_procs != 1: #Because metis chokes on it...
144        n_vert = domain.get_number_of_nodes()
145        t_list = domain.triangles.copy()
146        t_list = num.reshape(t_list, (-1,))
147   
148        # The 1 here is for triangular mesh elements.
149        edgecut, epart, npart = partMeshNodal(n_tri, n_vert, t_list, 1, n_procs)
150        # print edgecut
151        # print npart
152        # print epart
153        del edgecut
154        del npart
155
156        # Sometimes (usu. on x86_64), partMeshNodal returns an array of zero
157        # dimensional arrays. Correct this.
158        if type(epart[0]) == num.ndarray:
159            epart_new = num.zeros(len(epart), num.int)
160            for i in range(len(epart)):
161                epart_new[i] = epart[i][0]
162            epart = epart_new
163            del epart_new
164        # Assign triangles to processes, according to what metis told us.
165       
166        # tri_index maps triangle number -> processor, new triangle number
167        # (local to the processor)
168       
169        triangles = []       
170        for i in xrange(n_tri):
171            triangles_per_proc[epart[i]] = triangles_per_proc[epart[i]] + 1
172            tri_list[epart[i]].append(domain.triangles[i])
173            tri_index[i] = ([epart[i], len(tri_list[epart[i]]) - 1])
174            r_tri_index[epart[i], len(tri_list[epart[i]]) - 1] = i
175       
176        # Order the triangle list so that all of the triangles belonging
177        # to processor i are listed before those belonging to processor
178        # i+1
179
180        for i in xrange(n_procs):
181            for t in tri_list[i]:
182                triangles.append(t)
183           
184        # The boundary labels have to changed in accoradance with the
185        # new triangle ordering, proc_sum and tri_index help with this
186
187        proc_sum[0] = 0
188        for i in xrange(n_procs - 1):
189            proc_sum[i+1]=proc_sum[i]+triangles_per_proc[i]
190
191        # Relabel the boundary elements to fit in with the new triangle
192        # ordering
193
194        boundary = {}
195        for b in domain.boundary:
196            t =  tri_index[b[0]]
197            boundary[proc_sum[t[0]]+t[1], b[1]] = domain.boundary[b]
198
199        quantities = reorder(domain.quantities, tri_index, proc_sum)
200    else:
201        boundary = domain.boundary.copy()
202        triangles_per_proc[0] = n_tri
203        triangles = domain.triangles.copy()
204       
205        # This is essentially the same as a chunk of code from reorder.
206       
207        quantities = {}
208        for k in domain.quantities:
209            quantities[k] = num.zeros((n_tri, 3), num.float)
210            for i in range(n_tri):
211                quantities[k][i] = domain.quantities[k].vertex_values[i]
212       
213    # Extract the node list
214   
215    nodes = domain.get_nodes().copy()
216   
217    # Convert the triangle datastructure to be an array type,
218    # this helps with the communication
219
220    ttriangles = num.zeros((len(triangles), 3), num.int)
221    for i in xrange(len(triangles)):
222        ttriangles[i] = triangles[i]
223
224    #return nodes, ttriangles, boundary, triangles_per_proc, quantities
225   
226    return nodes, ttriangles, boundary, triangles_per_proc, quantities, tri_index, r_tri_index
227
228
229#########################################################
230#
231# Subdivide the domain. This module is primarily
232# responsible for building the ghost layer and
233# communication pattern
234#
235#
236#  Author: Linda Stals, June 2005
237#  Modified: Linda Stals, Nov 2005 (optimise python code)
238#            Steve Roberts, Aug 2009 (convert to numpy)
239#
240#
241#########################################################
242
243
244
245#########################################################
246#
247# Subdivide the triangles into non-overlapping domains.
248#
249#  *)  The subdivision is controlled by triangles_per_proc.
250# The first triangles_per_proc[0] triangles are assigned
251# to the first processor, the second triangles_per_proc[1]
252# are assigned to the second processor etc.
253#
254#  *) nodes, triangles and boundary contains all of the
255# nodes, triangles and boundary tag information for the
256# whole domain. The triangles should be orientated in the
257# correct way and the nodes number consecutively from 0.
258#
259# -------------------------------------------------------
260#
261#  *) A dictionary containing the full_nodes, full_triangles
262# and full_boundary information for each processor is
263# returned. The node information consists of
264# [global_id, x_coord, y_coord].
265#
266#########################################################
267
268def submesh_full(mesh, triangles_per_proc):
269
270    # Initialise
271
272
273    nodes = mesh.nodes
274    triangles = mesh.triangles
275    boundary = mesh.boundary
276
277    tlower = 0
278    nproc = len(triangles_per_proc)
279    nnodes = len(nodes)
280    node_list = []
281    triangle_list = []
282    boundary_list = []
283    submesh = {}
284    node_range = num.reshape(num.arange(nnodes),(nnodes,1))
285
286    #print node_range
287    tsubnodes = num.concatenate((node_range, nodes), 1)
288
289
290    # Loop over processors
291
292    for p in xrange(nproc):
293
294        # Find triangles on processor p
295
296        tupper = triangles_per_proc[p]+tlower
297        subtriangles = triangles[tlower:tupper]
298        triangle_list.append(subtriangles)
299
300        # Find the boundary edges on processor p
301
302        subboundary = {}
303        for k in boundary:
304            if (k[0] >=tlower and k[0] < tupper):
305                subboundary[k]=boundary[k]
306        boundary_list.append(subboundary)
307
308        # Find nodes in processor p
309
310        nodemap = num.zeros(nnodes, 'i')
311        for t in subtriangles:
312            nodemap[t[0]]=1
313            nodemap[t[1]]=1
314            nodemap[t[2]]=1
315
316       
317        node_list.append(tsubnodes.take(num.flatnonzero(nodemap),axis=0))
318
319        # Move to the next processor
320
321        tlower = tupper
322
323    # Put the results in a dictionary
324
325    submesh["full_nodes"] = node_list
326    submesh["full_triangles"] = triangle_list
327    submesh["full_boundary"] = boundary_list
328
329    # Clean up before exiting
330
331    del (nodemap)
332
333    return submesh
334
335
336#########################################################
337#
338# Build the ghost layer of triangles
339#
340#  *) Given the triangle subpartion for the processor
341# build a ghost layer of triangles. The ghost layer
342# consists of two layers of neighbouring triangles.
343#
344#  *) The vertices in the ghost triangles must also
345# be added to the node list for the current processor
346#
347#
348# -------------------------------------------------------
349#
350#  *) The extra triangles and nodes are returned.
351#
352#  *)  The node information consists of
353# [global_id, x_coord, y_coord].
354#
355#  *) The triangle information consists of
356# [triangle number, t], where t = [v1, v2, v3].
357#
358#########################################################
359
360def ghost_layer(submesh, mesh, p, tupper, tlower, parameters = None):
361
362    ncoord = mesh.number_of_nodes
363    ntriangles = mesh.number_of_triangles
364
365    if parameters is None:
366        layer_width  = 2
367    else:
368        layer_width = parameters['ghost_layer_width']
369
370
371    trianglemap = num.zeros(ntriangles, 'i')
372
373    # Find the first layer of boundary triangles
374    for t in range(tlower, tupper):
375       
376        n = mesh.neighbours[t, 0]
377        if n >= 0:
378            if n < tlower or n >= tupper:
379                trianglemap[n] = 1
380
381        n = mesh.neighbours[t, 1]
382        if n >= 0:
383            if n < tlower or n >= tupper:
384                trianglemap[n] = 1
385
386        n = mesh.neighbours[t, 2]
387        if n >= 0:
388            if n < tlower or n >= tupper:
389                trianglemap[n] = 1
390
391
392    # Find the subsequent layers of ghost triangles
393    for i in range(layer_width-1):
394
395        for t in range(len(trianglemap)):
396            if trianglemap[t]==i+1:
397
398                n = mesh.neighbours[t, 0]
399                if n >= 0:
400                    if (n < tlower or n >= tupper) and trianglemap[n] == 0:
401                        trianglemap[n] = i+2
402
403                n = mesh.neighbours[t, 1]
404                if n >= 0:
405                    if (n < tlower or n >= tupper) and trianglemap[n] == 0:
406                        trianglemap[n] = i+2
407
408                n = mesh.neighbours[t, 2]
409                if n >= 0:
410                    if (n < tlower or n >= tupper) and trianglemap[n] == 0:
411                        trianglemap[n] = i+2
412
413    # Build the triangle list and make note of the vertices
414
415
416    nodemap = num.zeros(ncoord, 'i')
417    fullnodes = submesh["full_nodes"][p]
418
419    subtriangles = []
420    for i in xrange(len(trianglemap)):
421        if trianglemap[i] != 0:
422            t = list(mesh.triangles[i])
423            nodemap[t[0]] = 1
424            nodemap[t[1]] = 1
425            nodemap[t[2]] = 1
426
427    trilist = num.reshape(num.arange(ntriangles),(ntriangles,1))
428    tsubtriangles = num.concatenate((trilist, mesh.triangles), 1)
429    subtriangles = tsubtriangles.take(num.flatnonzero(trianglemap),axis=0)
430
431   
432    # Keep a record of the triangle vertices, if they are not already there
433
434    subnodes = []
435    for n in fullnodes:
436        nodemap[int(n[0])] = 0
437
438    nodelist = num.reshape(num.arange(ncoord),(ncoord,1))
439    tsubnodes = num.concatenate((nodelist, mesh.get_nodes()), 1)
440    subnodes = tsubnodes.take(num.flatnonzero(nodemap),axis=0)
441
442    # Clean up before exiting
443
444    del (nodelist)
445    del (trilist)
446    del (tsubnodes)
447    del (nodemap)
448    del (trianglemap)
449
450    # Return the triangles and vertices sitting on the boundary layer
451
452    return subnodes, subtriangles, layer_width
453
454#########################################################
455#
456# Find the edges of the ghost trianlges that do not
457# have a neighbour in the current cell. These are
458# treated as a special type of boundary edge.
459#
460#  *) Given the ghost triangles in a particular
461# triangle, use the mesh to find its neigbours. If
462# the neighbour is not in the processor set it to
463# be a boundary edge
464#
465#  *) The vertices in the ghost triangles must also
466# be added to the node list for the current processor
467#
468#  *) The boundary edges for the ghost triangles are
469# ignored.
470#
471# -------------------------------------------------------
472#
473#  *) The type assigned to the ghost boundary edges is 'ghost'
474#
475#  *)  The boundary information is returned as a directorier
476# with the key = (triangle id, edge no) and the values
477# assigned to the key is 'ghost'
478#
479#
480#########################################################
481def is_in_processor(ghost_list, tlower, tupper, n):
482
483    return num.equal(ghost_list,n).any() or (tlower <= n and tupper > n)
484
485
486def ghost_bnd_layer(ghosttri, tlower, tupper, mesh, p):
487
488
489    boundary = mesh.boundary
490
491    ghost_list = []
492    subboundary = {}
493
494
495    # FIXME SR: For larger layers need to pass through the correct
496    # boundary tag!
497
498    for t in ghosttri:
499        ghost_list.append(t[0])
500   
501    for t in ghosttri:
502
503        n = mesh.neighbours[t[0], 0]
504        if not is_in_processor(ghost_list, tlower, tupper, n):
505            if boundary.has_key( (t[0], 0) ):
506                subboundary[t[0], 0] = boundary[t[0],0]
507            else:
508                subboundary[t[0], 0] = 'ghost'
509
510
511        n = mesh.neighbours[t[0], 1]
512        if not is_in_processor(ghost_list, tlower, tupper, n):
513            if boundary.has_key( (t[0], 1) ):
514                subboundary[t[0], 1] = boundary[t[0],1]
515            else:
516                subboundary[t[0], 1] = 'ghost'
517
518
519        n = mesh.neighbours[t[0], 2]
520        if not is_in_processor(ghost_list, tlower, tupper, n):
521            if boundary.has_key( (t[0], 2) ):
522                subboundary[t[0], 2] = boundary[t[0],2]
523            else:
524                subboundary[t[0], 2] = 'ghost'
525           
526    return subboundary
527
528#########################################################
529#
530# The ghost triangles on the current processor will need
531# to get updated information from the neighbouring
532# processor containing the corresponding full triangles.
533#
534#  *) The tri_per_proc is used to determine which
535# processor contains the full node copy.
536#
537# -------------------------------------------------------
538#
539#  *) The ghost communication pattern consists of
540# [global node number, neighbour processor number].
541#
542#########################################################
543
544def ghost_commun_pattern(subtri, p, tri_per_proc):
545
546    # Loop over the ghost triangles
547
548    ghost_commun = num.zeros((len(subtri), 2), num.int)
549
550    for i in xrange(len(subtri)):
551        global_no = subtri[i][0]
552
553        # Find which processor contains the full triangle
554
555        nproc = len(tri_per_proc)
556        neigh = nproc-1
557        sum = 0
558        for q in xrange(nproc-1):
559            if (global_no < sum+tri_per_proc[q]):
560                neigh = q
561                break
562            sum = sum+tri_per_proc[q]
563
564        # Keep a copy of the neighbour processor number
565
566        ghost_commun[i] = [global_no, neigh]
567
568    return ghost_commun
569
570#########################################################
571#
572# The full triangles in this processor must communicate
573# updated information to neighbouring processor that
574# contain ghost triangles
575#
576#  *) The ghost communication pattern for all of the
577# processor must be built before calling this processor.
578#
579#  *) The full communication pattern is found by looping
580# through the ghost communication pattern for all of the
581# processors. Recall that this information is stored in
582# the form [global node number, neighbour processor number].
583# The full communication for the neighbour processor is
584# then updated.
585#
586# -------------------------------------------------------
587#
588#  *) The full communication pattern consists of
589# [global id, [p1, p2, ...]], where p1, p2 etc contain
590# a ghost node copy of the triangle global id.
591#
592#########################################################
593
594def full_commun_pattern(submesh, tri_per_proc):
595    tlower = 0
596    nproc = len(tri_per_proc)
597    full_commun = []
598
599    # Loop over the processor
600
601    for p in xrange(nproc):
602
603        # Loop over the full triangles in the current processor
604        # and build an empty dictionary
605
606        fcommun = {}
607        tupper = tri_per_proc[p]+tlower
608        for i in xrange(tlower, tupper):
609            fcommun[i] = []
610        full_commun.append(fcommun)
611        tlower = tupper
612
613    # Loop over the processor again
614
615    for p in xrange(nproc):
616
617        # Loop over the ghost triangles in the current processor,
618        # find which processor contains the corresponding full copy
619        # and note that the processor must send updates to this
620        # processor
621
622        for g in submesh["ghost_commun"][p]:
623            neigh = g[1]
624            full_commun[neigh][g[0]].append(p)
625
626    return full_commun
627
628
629#########################################################
630#
631# Given the non-overlapping grid partition, an extra layer
632# of triangles are included to help with the computations.
633# The triangles in this extra layer are not updated by
634# the processor, their updated values must be sent by the
635# processor containing the original, full, copy of the
636# triangle. The communication pattern that controls these
637# updates must also be built.
638#
639#  *) Assumes that full triangles, nodes etc have already
640# been found and stored in submesh
641#
642#  *) See the documentation for ghost_layer,
643# ghost_commun_pattern and full_commun_pattern
644#
645# -------------------------------------------------------
646#
647#  *) The additional information is added to the submesh
648# dictionary. See the documentation for ghost_layer,
649# ghost_commun_pattern and full_commun_pattern
650#
651#  *) The ghost_triangles, ghost_nodes, ghost_boundary,
652# ghost_commun and full_commun is added to submesh
653#########################################################
654
655def submesh_ghost(submesh, mesh, triangles_per_proc, parameters = None):
656
657    nproc = len(triangles_per_proc)
658    tlower = 0
659    ghost_triangles = []
660    ghost_nodes = []
661    ghost_commun = []
662    ghost_bnd = []
663    ghost_layer_width = []
664
665    # Loop over the processors
666
667    for p in xrange(nproc):
668
669        # Find the full triangles in this processor
670
671        tupper = triangles_per_proc[p]+tlower
672
673        # Build the ghost boundary layer
674
675        [subnodes, subtri, layer_width] = \
676                   ghost_layer(submesh, mesh, p, tupper, tlower, parameters)
677        ghost_layer_width.append(layer_width)
678        ghost_triangles.append(subtri)
679        ghost_nodes.append(subnodes)
680
681
682        # Find the boundary layer formed by the ghost triangles
683       
684        subbnd = ghost_bnd_layer(subtri, tlower, tupper, mesh, p)
685        ghost_bnd.append(subbnd)
686       
687        # Build the communication pattern for the ghost nodes
688
689        gcommun = \
690                ghost_commun_pattern(subtri, p, triangles_per_proc)
691        ghost_commun.append(gcommun)
692
693        # Move to the next processor
694
695        tlower = tupper
696
697
698    # Record the ghost layer and communication pattern
699    submesh["ghost_layer_width"] = ghost_layer_width
700    submesh["ghost_nodes"] = ghost_nodes
701    submesh["ghost_triangles"] = ghost_triangles
702    submesh["ghost_commun"] = ghost_commun
703    submesh["ghost_boundary"] = ghost_bnd
704   
705    # Build the communication pattern for the full triangles
706
707    full_commun = full_commun_pattern(submesh, triangles_per_proc)
708    submesh["full_commun"] = full_commun
709
710    # Return the submesh
711
712    return submesh
713
714
715#########################################################
716#
717# Certain quantities may be assigned to the triangles,
718# these quantities must be subdivided in the same way
719# as the triangles
720#
721#  *) The quantities are ordered in the same way as the
722# triangles
723#
724# -------------------------------------------------------
725#
726#  *) The quantites attached to the full triangles are
727# stored in full_quan
728#
729#  *) The quantities attached to the ghost triangles are
730# stored in ghost_quan
731#########################################################
732
733def submesh_quantities(submesh, quantities, triangles_per_proc):
734
735    nproc = len(triangles_per_proc)
736
737    lower = 0
738
739    # Build an empty dictionary to hold the quantites
740
741    submesh["full_quan"] = {}
742    submesh["ghost_quan"] = {}
743    for k in quantities:
744        submesh["full_quan"][k] = []
745        submesh["ghost_quan"][k] = []
746
747    # Loop trough the subdomains
748
749    for p in xrange(nproc):
750        upper =   lower+triangles_per_proc[p]
751
752        # Find the global ID of the ghost triangles
753
754        global_id = []
755        M = len(submesh["ghost_triangles"][p])
756        for j in xrange(M):
757            global_id.append(submesh["ghost_triangles"][p][j][0])
758
759        # Use the global ID to extract the quantites information from
760        # the full domain
761
762        for k in quantities:
763            submesh["full_quan"][k].append(quantities[k][lower:upper])
764            submesh["ghost_quan"][k].append(num.zeros( (M,3) , num.float))
765            for j in range(M):
766                submesh["ghost_quan"][k][p][j] = \
767                                               quantities[k][global_id[j]]
768
769        lower = upper
770
771    return submesh
772
773#########################################################
774#
775# Build the grid partition on the host.
776#
777#  *) See the documentation for submesh_ghost and
778# submesh_full
779#
780# -------------------------------------------------------
781#
782#  *) A dictionary containing the full_triangles,
783# full_nodes, full_boundary, ghost_triangles, ghost_nodes,
784# ghost_boundary, ghost_commun and full_commun and true boundary polygon is returned.
785#
786#########################################################
787
788def build_submesh(nodes, triangles, boundary, quantities,
789                  triangles_per_proc, parameters = None):
790
791    # Temporarily build the mesh to find the neighbouring
792    # triangles and true boundary polygon
793
794    mesh = Mesh(nodes, triangles, boundary)
795    boundary_polygon = mesh.get_boundary_polygon()
796   
797
798    # Subdivide into non-overlapping partitions
799
800    submeshf = submesh_full(mesh, triangles_per_proc)
801   
802    # Add any extra ghost boundary layer information
803
804    submeshg = submesh_ghost(submeshf, mesh, triangles_per_proc, parameters)
805
806    # Order the quantities information to be the same as the triangle
807    # information
808
809    submesh = submesh_quantities(submeshg, quantities, \
810                                 triangles_per_proc)
811
812    submesh["boundary_polygon"] = boundary_polygon
813    return submesh
814
815#########################################################
816#
817#  Given the subdivision of the grid assigned to the
818# current processor convert it into a form that is
819# appropriate for the GA datastructure.
820#
821#  The main function of these modules is to change the
822# node numbering. The GA datastructure assumes they
823# are numbered consecutively from 0.
824#
825#  The module also changes the communication pattern
826# datastructure into a form needed by parallel_advection
827#
828#  Authors: Linda Stals and Matthew Hardy, June 2005
829#  Modified: Linda Stals, Nov 2005 (optimise python code)
830#            Steve Roberts, Aug 2009 (updating to numpy)
831#
832#
833#########################################################
834
835 
836#########################################################
837# Convert the format of the data to that used by ANUGA
838#
839#
840# *) Change the nodes global ID's to an integer value,
841#starting from 0.
842#
843# *) The triangles and boundary edges must also be
844# updated accordingly.
845#
846# -------------------------------------------------------
847#
848# *) The nodes, triangles and boundary edges defined by
849# the new numbering scheme are returned
850#
851#########################################################
852
853def build_local_GA(nodes, triangles, boundaries, tri_map):
854
855    Nnodes =len(nodes)
856    Ntriangles = len(triangles)
857   
858    # Extract the nodes (using the local ID)
859   
860    GAnodes = num.take(nodes, (1, 2), 1)
861
862    # Build a global ID to local ID mapping
863
864    NGlobal = 0
865    for i in xrange(Nnodes):
866        if nodes[i][0] > NGlobal:
867            NGlobal = nodes[i][0]
868
869    node_map = -1*num.ones(int(NGlobal)+1, num.int)
870
871    num.put(node_map, num.take(nodes, (0,), 1).astype(num.int), \
872        num.arange(Nnodes))
873       
874    # Change the global IDs in the triangles to the local IDs
875
876    GAtriangles = num.zeros((Ntriangles, 3), num.int)
877    GAtriangles[:,0] = num.take(node_map, triangles[:,0])
878    GAtriangles[:,1] = num.take(node_map, triangles[:,1])
879    GAtriangles[:,2] = num.take(node_map, triangles[:,2])
880
881    # Change the triangle numbering in the boundaries
882
883    GAboundaries = {}
884    for b in boundaries:
885        GAboundaries[tri_map[b[0]], b[1]] = boundaries[b]
886       
887   
888    return GAnodes, GAtriangles, GAboundaries, node_map
889
890
891#########################################################
892# Change the communication format to that needed by the
893# parallel advection file.
894#
895# *) The index contains [global triangle no,
896# local triangle no.]
897#
898# -------------------------------------------------------
899#
900# *) The ghost_recv and full_send dictionaries are
901# returned.
902#
903# *) ghost_recv dictionary is local id, global id, value
904#
905# *) full_recv dictionary is local id, global id, value
906#
907# *) The information is ordered by the global id. This
908# means that the communication order is predetermined and
909# local and global id do not need to be
910# compared when the information is sent/received.
911#
912#########################################################
913
914def build_local_commun(tri_map, ghostc, fullc, nproc):
915
916    # Initialise
917
918    full_send = {}
919    ghost_recv = {}
920
921    # Build the ghost_recv dictionary (sort the
922    # information by the global numbering)
923   
924    ghostc = num.sort(ghostc, 0)
925   
926    for c in xrange(nproc):
927        s = ghostc[:,0]
928        d = num.compress(num.equal(ghostc[:,1],c), s)
929        if len(d) > 0:
930            ghost_recv[c] = [0, 0]
931            ghost_recv[c][1] = d
932            ghost_recv[c][0] = num.take(tri_map, d)
933           
934    # Build a temporary copy of the full_send dictionary
935    # (this version allows the information to be stored
936    # by the global numbering)
937
938    tmp_send = {}
939    for global_id in fullc:
940        for i in xrange(len(fullc[global_id])):
941            neigh = fullc[global_id][i]
942            if not tmp_send.has_key(neigh):
943                tmp_send[neigh] = []
944            tmp_send[neigh].append([global_id, \
945                                    tri_map[global_id]])
946
947    # Extract the full send information and put it in the form
948    # required for the full_send dictionary
949
950    for neigh in tmp_send:
951        neigh_commun = num.sort(tmp_send[neigh], 0)
952        full_send[neigh] = [0, 0]
953        full_send[neigh][0] = neigh_commun[:,1]
954        full_send[neigh][1] = neigh_commun[:,0]
955
956    return ghost_recv, full_send
957
958
959#########################################################
960# Convert the format of the data to that used by ANUGA
961#
962#
963# *) Change the nodes global ID's to an integer value,
964# starting from 0. The node numbering in the triangles
965# must also be updated to take this into account.
966#
967# *) The triangle number will also change, which affects
968# the boundary tag information and the communication
969# pattern.
970#
971# -------------------------------------------------------
972#
973# *) The nodes, triangles, boundary edges and communication
974# pattern defined by the new numbering scheme are returned
975#
976#########################################################
977
978def build_local_mesh(submesh, lower_t, upper_t, nproc):
979
980    # Combine the full nodes and ghost nodes
981
982    nodes = num.concatenate((submesh["full_nodes"], \
983                         submesh["ghost_nodes"]))
984
985    ghost_layer_width = submesh["ghost_layer_width"]
986   
987    # Combine the full triangles and ghost triangles
988
989    gtri =  num.take(submesh["ghost_triangles"],(1, 2, 3),1)
990    triangles = num.concatenate((submesh["full_triangles"], gtri))
991
992    # Combine the full boundaries and ghost boundaries
993
994    boundaries = submesh["full_boundary"]
995    for b in submesh["ghost_boundary"]:
996        boundaries[b]=submesh["ghost_boundary"][b]
997
998    # Make note of the new triangle numbers, including the ghost
999    # triangles
1000
1001    NGlobal = upper_t
1002    for i in xrange(len(submesh["ghost_triangles"])):
1003        id = submesh["ghost_triangles"][i][0]
1004        if id > NGlobal:
1005            NGlobal = id
1006    #index = num.zeros(int(NGlobal)+1, num.int)
1007    tri_map = -1*num.ones(int(NGlobal)+1, num.int)
1008    tri_map[lower_t:upper_t]=num.arange(upper_t-lower_t)
1009    for i in xrange(len(submesh["ghost_triangles"])):
1010        tri_map[submesh["ghost_triangles"][i][0]] = i+upper_t-lower_t
1011   
1012    # Change the node numbering (and update the numbering in the
1013    # triangles)
1014
1015    [GAnodes, GAtriangles, GAboundary, node_map] = \
1016    build_local_GA(nodes, triangles, boundaries, tri_map)
1017
1018    # Extract the local quantities
1019   
1020    quantities ={}
1021    for k in submesh["full_quan"]:
1022        Nf = len(submesh["full_quan"][k])
1023        Ng = len(submesh["ghost_quan"][k])
1024        quantities[k] = num.zeros((Nf+Ng, 3), num.float)
1025        quantities[k][0:Nf] = submesh["full_quan"][k] 
1026        quantities[k][Nf:Nf+Ng] = submesh["ghost_quan"][k]
1027                             
1028    # Change the communication pattern into a form needed by
1029    # the parallel_adv
1030
1031    gcommun = submesh["ghost_commun"]
1032    fcommun = submesh["full_commun"]
1033    [ghost_rec, full_send] = \
1034                build_local_commun(tri_map, gcommun, fcommun, nproc)
1035
1036
1037    return GAnodes, GAtriangles, GAboundary, quantities, ghost_rec, \
1038           full_send, tri_map, node_map, ghost_layer_width
1039
1040
1041#########################################################
1042#
1043# Handle the communication between the host machine
1044# (processor 0) and the processors. The host machine is
1045# responsible for the doing the initial grid partitioning.
1046#
1047# The routines given below should be moved to the
1048# build_submesh.py and build_local.py file to allow
1049# overlapping of  communication and computation.
1050# This should be done after more debugging.
1051#
1052#
1053#  Author: Linda Stals, June 2005
1054#  Modified: Linda Stals, Nov 2005 (optimise python code)
1055#            Steve Roberts, Aug 2009 (update to numpy)
1056#
1057#
1058#########################################################
1059
1060
1061#########################################################
1062#
1063# Send the submesh to processor p.
1064#
1065# *) The order and form is strongly coupled with
1066# rec_submesh.
1067#
1068# -------------------------------------------------------
1069#
1070# *) All of the information has been sent to processor p.
1071#
1072#########################################################
1073
1074def send_submesh(submesh, triangles_per_proc, p, verbose=True):
1075
1076    import pypar
1077   
1078    myid = pypar.rank()
1079    nprocs = pypar.size()
1080   
1081    if verbose: print 'P%d: Sending submesh to P%d' %(myid, p)
1082   
1083    # build and send the tagmap for the boundary conditions
1084   
1085    tagmap = {}
1086    counter = 1
1087    for b in submesh["full_boundary"][p]:
1088         bkey = submesh["full_boundary"][p][b]
1089         if not tagmap.has_key(bkey):
1090             tagmap[bkey] = counter
1091             counter = counter+1
1092    for b in submesh["ghost_boundary"][p]:
1093         bkey = submesh["ghost_boundary"][p][b]
1094         if not tagmap.has_key(bkey):
1095             tagmap[bkey] = counter
1096             counter = counter+1
1097
1098
1099    # send boundary tags
1100    pypar.send(tagmap, p)
1101
1102    # send the quantities key information
1103    pypar.send(submesh["full_quan"].keys(), p)
1104
1105    # compress full_commun
1106    flat_full_commun = []
1107
1108    for c in submesh["full_commun"][p]:
1109        for i in range(len(submesh["full_commun"][p][c])):
1110            flat_full_commun.append([c,submesh["full_commun"][p][c][i]])
1111
1112    # send the array sizes so memory can be allocated
1113
1114    setup_array = num.zeros((9,),num.int)
1115    setup_array[0] = len(submesh["full_nodes"][p])
1116    setup_array[1] = len(submesh["ghost_nodes"][p])
1117    setup_array[2] = len(submesh["full_triangles"][p])
1118    setup_array[3] = len(submesh["ghost_triangles"][p])
1119    setup_array[4] = len(submesh["full_boundary"][p])
1120    setup_array[5] = len(submesh["ghost_boundary"][p])
1121    setup_array[6] = len(submesh["ghost_commun"][p])
1122    setup_array[7] = len(flat_full_commun)
1123    setup_array[8] = len(submesh["full_quan"])
1124
1125    x = num.array(setup_array, num.int)
1126    pypar.send(x, p, bypass=True)
1127
1128
1129    # ghost layer width
1130    x = num.array(submesh["ghost_layer_width"][p], num.int)
1131    pypar.send(x, p, bypass=True)
1132
1133
1134    # send the number of triangles per processor
1135    x = num.array(triangles_per_proc)
1136    pypar.send(x, p, bypass=True)
1137
1138    # send the nodes
1139    x = num.array(submesh["full_nodes"][p], num.float)
1140    pypar.send(x, p, bypass=True)
1141
1142    x = num.array(submesh["ghost_nodes"][p], num.float)
1143    pypar.send(x, p, bypass=True)
1144
1145    # send the triangles
1146    x = num.array(submesh["full_triangles"][p], num.int)
1147    pypar.send(x, p, bypass=True)
1148
1149    # send ghost triangles
1150    x = num.array(submesh["ghost_triangles"][p], num.int)
1151    pypar.send(x, p, bypass=True)
1152
1153
1154    # send the boundary
1155    bc = []
1156    for b in submesh["full_boundary"][p]:
1157        bc.append([b[0], b[1], tagmap[submesh["full_boundary"][p][b]]])
1158
1159    x = num.array(bc, num.int)
1160    pypar.send(x, p, bypass=True)
1161
1162
1163    bc = []
1164    for b in submesh["ghost_boundary"][p]:
1165        bc.append([b[0], b[1], tagmap[submesh["ghost_boundary"][p][b]]])
1166
1167    x = num.array(bc, num.int)
1168    pypar.send(x, p, bypass=True)
1169
1170
1171
1172    # send the communication pattern
1173    x = submesh["ghost_commun"][p]
1174    pypar.send(x, p, bypass=True)
1175
1176
1177    x = num.array(flat_full_commun, num.int)
1178    pypar.send(x, p, bypass=True)
1179
1180
1181    # send the quantities
1182    for k in submesh["full_quan"]:
1183        x = num.array(submesh["full_quan"][k][p], num.float)
1184        pypar.send(x, p, bypass=True)
1185       
1186    for k in submesh["ghost_quan"]:
1187        x = num.array(submesh["ghost_quan"][k][p], num.float)
1188        pypar.send(x,p, bypass=True)
1189       
1190
1191#########################################################
1192#
1193# Receive the submesh from processor p.
1194#
1195# *) The order and form is strongly coupled with
1196# send_submesh.
1197#
1198# -------------------------------------------------------
1199#
1200# *) All of the information has been received by the
1201# processor p and passed into build_local.
1202#
1203# *) The information is returned in a form needed by the
1204# GA datastructure.
1205#
1206#########################################################
1207
1208def rec_submesh_flat(p, verbose=True):
1209
1210    import pypar
1211   
1212    numprocs = pypar.size()
1213    myid = pypar.rank()
1214
1215    submesh_cell = {}
1216   
1217    if verbose: print indent+'P%d: Receiving submesh from P%d' %(myid, p)
1218
1219    # receive the tagmap for the boundary conditions
1220   
1221    tagmap = pypar.receive(p)
1222
1223    itagmap = {}
1224    for t in tagmap:
1225        itagmap[tagmap[t]]=t
1226
1227    # receive the quantities key information
1228    qkeys = pypar.receive(p)
1229
1230    # recieve information about the array sizes
1231    x = num.zeros((9,),num.int)
1232    pypar.receive(p, buffer=x,  bypass=True)
1233    setup_array = x
1234
1235    no_full_nodes      = setup_array[0]
1236    no_ghost_nodes     = setup_array[1]
1237    no_full_triangles  = setup_array[2]
1238    no_ghost_triangles = setup_array[3]
1239    no_full_boundary   = setup_array[4]
1240    no_ghost_boundary  = setup_array[5]
1241    no_ghost_commun    = setup_array[6]
1242    no_full_commun     = setup_array[7]
1243    no_quantities      = setup_array[8]
1244
1245   
1246    # ghost layer width
1247    x = num.zeros((1,),num.int)
1248    pypar.receive(p, buffer=x,  bypass=True)
1249    submesh_cell["ghost_layer_width"] = x[0]
1250
1251
1252    # receive the number of triangles per processor
1253    x = num.zeros((numprocs,),num.int)
1254    pypar.receive(p, buffer=x,  bypass=True)
1255    triangles_per_proc = x
1256
1257    # receive the full nodes
1258    x = num.zeros((no_full_nodes,3),num.float)
1259    pypar.receive(p, buffer=x,  bypass=True)
1260    submesh_cell["full_nodes"] = x
1261
1262
1263    # receive the ghost nodes
1264    x = num.zeros((no_ghost_nodes,3),num.float)
1265    pypar.receive(p, buffer=x,  bypass=True)
1266    submesh_cell["ghost_nodes"] = x
1267   
1268    # receive the full triangles
1269    x = num.zeros((no_full_triangles,3),num.int)
1270    pypar.receive(p, buffer=x,  bypass=True)
1271    submesh_cell["full_triangles"] = x
1272
1273   
1274    # receive the ghost triangles
1275    x = num.zeros((no_ghost_triangles,4),num.int)
1276    pypar.receive(p, buffer=x,  bypass=True)
1277    submesh_cell["ghost_triangles"] = x
1278
1279
1280
1281    # receive the full boundary
1282    x = num.zeros((no_full_boundary,3),num.int)
1283    pypar.receive(p, buffer=x,  bypass=True)
1284    bnd_c = x
1285
1286    submesh_cell["full_boundary"] = {}
1287    for b in bnd_c:
1288        submesh_cell["full_boundary"][b[0],b[1]]=itagmap[b[2]]
1289
1290
1291    # receive the ghost boundary
1292    x = num.zeros((no_ghost_boundary,3),num.int)
1293    pypar.receive(p, buffer=x,  bypass=True)
1294    bnd_c = x
1295
1296    submesh_cell["ghost_boundary"] = {}
1297    for b in bnd_c:
1298        submesh_cell["ghost_boundary"][b[0],b[1]]=itagmap[b[2]]
1299
1300    # receive the ghost communication pattern
1301    x = num.zeros((no_ghost_commun,2),num.int)
1302    pypar.receive(p, buffer=x,  bypass=True)
1303    submesh_cell["ghost_commun"] = x
1304   
1305    # receive the full communication pattern
1306    x = num.zeros((no_full_commun,2),num.int)
1307    pypar.receive(p, buffer=x,  bypass=True)
1308    full_commun = x
1309
1310    submesh_cell["full_commun"] = {}
1311    for c in full_commun:
1312        submesh_cell["full_commun"][c[0]] = []
1313    for c in full_commun:
1314        submesh_cell["full_commun"][c[0]].append(c[1])
1315
1316    # receive the quantities
1317
1318    submesh_cell["full_quan"]={}
1319    for i in range(no_quantities):
1320        x = num.zeros((no_full_triangles,3), num.float)
1321        pypar.receive(p, buffer=x, bypass=True)
1322        submesh_cell["full_quan"][qkeys[i]]= x
1323
1324    submesh_cell["ghost_quan"]={}
1325    for i in range(no_quantities):
1326        x = num.zeros((no_ghost_triangles,3), num.float)
1327        pypar.receive(p, buffer=x, bypass=True)
1328        submesh_cell["ghost_quan"][qkeys[i]]= x
1329   
1330    return submesh_cell, triangles_per_proc,\
1331           no_full_nodes, no_full_triangles
1332
1333
1334
1335#########################################################
1336#
1337# Receive the submesh from processor p.
1338#
1339# *) The order and form is strongly coupled with
1340# send_submesh.
1341#
1342# -------------------------------------------------------
1343#
1344# *) All of the information has been received by the
1345# processor p and passed into build_local.
1346#
1347# *) The information is returned in a form needed by the
1348# GA datastructure.
1349#
1350#########################################################
1351
1352def rec_submesh(p, verbose=True):
1353
1354    import pypar
1355   
1356    numproc = pypar.size()
1357    myid = pypar.rank()
1358
1359    [submesh_cell, triangles_per_proc,\
1360     number_of_full_nodes, number_of_full_triangles] = rec_submesh_flat(p,verbose)
1361   
1362    # find the full triangles assigned to this processor
1363
1364    lower_t = 0
1365    for i in range(myid):
1366        lower_t = lower_t+triangles_per_proc[i]
1367    upper_t = lower_t+triangles_per_proc[myid]
1368
1369    # convert the information into a form needed by the GA
1370    # datastructure
1371
1372    [GAnodes, GAtriangles, boundary, quantities, \
1373     ghost_rec, full_send, tri_map, node_map, ghost_layer_width] = \
1374              build_local_mesh(submesh_cell, lower_t, upper_t, \
1375                               numproc)
1376   
1377    return GAnodes, GAtriangles, boundary, quantities,\
1378           ghost_rec, full_send,\
1379           number_of_full_nodes, number_of_full_triangles, tri_map, node_map,\
1380           ghost_layer_width
1381         
1382
1383
1384#########################################################
1385#
1386# Extract the submesh that will belong to the
1387# "host processor" (i.e. processor zero)
1388#
1389#  *) See the documentation for build_submesh
1390#
1391# -------------------------------------------------------
1392#
1393#  *) A dictionary containing the full_triangles,
1394# full_nodes, full_boundary, ghost_triangles, ghost_nodes,
1395# ghost_boundary, ghost_commun and full_commun belonging
1396# to processor zero are returned.
1397#
1398#########################################################
1399def extract_hostmesh(submesh, triangles_per_proc):
1400
1401   
1402    submesh_cell = {}
1403    submesh_cell["ghost_layer_width"] = submesh["ghost_layer_width"][0]
1404    submesh_cell["full_nodes"] = submesh["full_nodes"][0]
1405    submesh_cell["ghost_nodes"] = submesh["ghost_nodes"][0]
1406    submesh_cell["full_triangles"] = submesh["full_triangles"][0]
1407    submesh_cell["ghost_triangles"] = submesh["ghost_triangles"][0]
1408    submesh_cell["full_boundary"] = submesh["full_boundary"][0]
1409    submesh_cell["ghost_boundary"] = submesh["ghost_boundary"][0]
1410    submesh_cell["ghost_commun"] = submesh["ghost_commun"][0]
1411    submesh_cell["full_commun"] = submesh["full_commun"][0]
1412    submesh_cell["full_quan"] ={}
1413    submesh_cell["ghost_quan"]={}
1414    for k in submesh["full_quan"]:
1415        submesh_cell["full_quan"][k] = submesh["full_quan"][k][0]
1416        submesh_cell["ghost_quan"][k] = submesh["ghost_quan"][k][0]
1417
1418    numprocs = len(triangles_per_proc)
1419    points, vertices, boundary, quantities, ghost_recv_dict, \
1420            full_send_dict, tri_map, node_map, ghost_layer_width = \
1421            build_local_mesh(submesh_cell, 0, triangles_per_proc[0], numprocs)
1422
1423
1424    return  points, vertices, boundary, quantities, ghost_recv_dict, \
1425           full_send_dict, tri_map, node_map, ghost_layer_width
1426           
1427
1428
1429
1430
Note: See TracBrowser for help on using the repository browser.