source: inundation/ga/storm_surge/parallel/mg2ga.py @ 1452

Last change on this file since 1452 was 1409, checked in by matthew, 20 years ago

the function readmg in mg2ga.py now returns triangles_per_proc instead of nodes_per_proc, since, in contrast to nodes (vertices), triangles are unique to a processor. This allows for cleaner construction of Linda's submesh in the case where
some nodes are common to several processors.

File size: 5.1 KB
Line 
1from string import atoi
2from string import atof
3import os
4from mesh import Mesh
5from Numeric import shape
6##################################################################################
7#                   TO BE REPLACED BY SPLIT
8##################################################################################
9def str2array(str,conv):
10    l = len(str)
11    a = 0
12    b = 0
13    data = []
14    while str[a] == ' ':
15        a = a+1
16    while str[a] != '\n':
17        b = a+1
18        while str[b] != ' ' and str[b] != '\n':
19           b = b+1
20        data.append(conv(str[a:b]))
21        a = b
22    return data
23##################################################################################
24#  Read in a data file stored in the mg_cell data format                 
25##################################################################################
26def readmg(f):
27    # read the number of processors
28    f.readline()
29    no_proc = str2array(f.readline(), atoi)[0]
30    for i in range(5):
31        f.readline();
32    # nodes list
33    nodes = []
34    # triangle list
35    triangles = []
36    # know first nodes_per_proc[0] belong to cell 0, next
37    # nodes_per_proc[1] belong to cell 1 etc.
38    #nodes_per_proc = [] This is worked out later
39    triangles_per_proc = [] #each triangle belongs to a unique processor
40    # loop over the processors
41    for q in range(no_proc):
42        print q
43        # read the nodes
44        no_nodes = str2array(f.readline(), atoi)[1]
45        #nodes_per_proc.append(no_nodes)
46        f.readline()
47        for i in range(no_nodes):
48            line = str2array(f.readline(), atof)
49            line[0] = int(line[0])
50            nodes.append(line[0:3])
51        # skip over the ghost nodes
52        f.readline()
53        no_ghost_nodes = str2array(f.readline(), atoi)[1]
54        f.readline()
55        for i in range(no_ghost_nodes):
56            f.readline()
57        # skip over the edges
58        f.readline()
59        f.readline()
60        f.readline()
61        no_edges = str2array(f.readline(), atoi)[1]
62        f.readline()
63        for i in range(no_edges):
64            f.readline()
65        # read the triangles
66        f.readline()
67        f.readline()
68        f.readline()
69        #STARTMH160505
70        no_triangles = str2array(f.readline(), atoi)[1] #number of triangles listed in mg output file
71        #triangles_per_proc.append(no_triangles)
72        f.readline()
73        for i in range(no_triangles): #loop over all triangles in input file, checking for duplication
74            line = str2array(f.readline(), atoi)
75            line.sort() #for easy comparison with triangles listed so far
76            if (i==0):#always list the first triangle. This ensures that loop over t below is never empty
77                triangles.append(line)
78                continue
79            for t in range(shape(triangles)[0]): #for all the (non-duplicated) triangles we've seen so far
80                if (triangles[t]==line): #check to see if any of them match the triangle just read
81                    break
82            if (triangles[t]!=line): #need to add line to list
83               triangles.append(line)
84        no_triangles=shape(triangles)[0]-sum(triangles_per_proc)
85        triangles_per_proc.append(no_triangles)
86        #ENDMH160505
87        # skip extra space
88        f.readline()
89        f.readline()
90        f.readline()
91        f.readline()
92    return nodes, triangles, triangles_per_proc
93##################################################################################
94# Convert the format of the data to that used by pyvolution
95##################################################################################
96#Now renumber the nodes as 0,1,2,... and make the corresponding changes to the triangle definitions,
97#changing the orientation if necessary.
98def restructure(nodes, triangles):
99    Nnodes=shape(nodes)[0] #the number of nodes (or vertices)
100    Ntriangles=shape(triangles)[0] #the number of triangles
101    index={} #a dictionary mapping existing node ids to the new ids 0,1,2,...
102    GAnodes = []
103    for node_idnew in range(Nnodes): #move through the list of nodes, renumbering to 0,1,2, ...
104        index[nodes[node_idnew][0]]=node_idnew #make the dictionary entry, for later use by triangles
105        GAnodes.append(nodes[node_idnew][1:3])
106#        nodes[node_idnew][0]=node_idnew #renumber the node
107    #Now loop over the triangles, changing the node ids and orientation.
108    for t in range(Ntriangles):
109        x=[] #x-coordinates of the three nodes (vertices)
110        y=[] #y-coordinates of the three nodes (vertices)
111        for i in range(3):
112            n=index[triangles[t][i]] #use the dictionary to get the new node id
113            triangles[t][i]=n #relabel the node
114            x.append(nodes[n][1])
115            y.append(nodes[n][2])
116        if ((x[1]-x[0])*(y[2]-y[0])-(x[2]-x[0])*(y[1]-y[0])<0): #need to change the orientation
117            triangles[t][2]=triangles[t][0]#swap the 0th and 2nd nodes
118            triangles[t][0]=n
119    del(x)
120    del(y)           
121    del(index) #delete the dictionary
122    return GAnodes, triangles
123def mg2ga(f):
124    [nodes, triangles, triangles_per_proc] = readmg(f)
125    [nodes, triangles] = restructure(nodes, triangles)
126    return nodes, triangles, triangles_per_proc
Note: See TracBrowser for help on using the repository browser.