Changeset 1403
- Timestamp:
- May 16, 2005, 6:06:14 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/parallel/mg2ga.py
r1398 r1403 1 import sys2 from os import sep3 sys.path.append('..'+sep+'pyvolution')4 5 6 7 1 from string import atoi 8 2 from string import atof 9 10 11 3 from general_mesh import General_mesh as Mesh 12 4 from Numeric import shape 13 14 5 ################################################################################## 15 6 # TO BE REPLACED BY SPLIT 16 7 ################################################################################## 17 18 19 8 def str2array(str,conv): 20 9 l = len(str) … … 31 20 a = b 32 21 return data 33 34 35 22 ################################################################################## 36 # Read in a data file stored in the mg_cell data format 23 # Read in a data file stored in the mg_cell data format 37 24 ################################################################################## 38 39 40 25 def readmg(f): 41 42 # read the nuber of processors 43 26 # read the number of processors 44 27 f.readline() 45 28 no_proc = str2array(f.readline(), atoi)[0] 46 47 29 for i in range(5): 48 30 f.readline(); 49 50 31 # nodes list 51 52 32 nodes = [] 53 54 33 # triangle list 55 56 34 triangles = [] 57 58 35 # know first nodes_per_proc[0] belong to cell 0, next 59 36 # nodes_per_proc[1] belong to cell 1 etc. 60 61 nodes_per_proc = [] 37 nodes_per_proc = [] 62 38 triangles_per_proc = [] 63 64 39 # loop over the processors 65 66 40 for q in range(no_proc): 67 68 41 print q 69 70 42 # read the nodes 71 72 43 no_nodes = str2array(f.readline(), atoi)[1] 73 44 nodes_per_proc.append(no_nodes) 74 45 f.readline() 75 76 46 for i in range(no_nodes): 77 47 line = str2array(f.readline(), atof) 78 48 line[0] = int(line[0]) 79 49 nodes.append(line[0:3]) 80 81 50 # skip over the ghost nodes 82 83 51 f.readline() 84 52 no_ghost_nodes = str2array(f.readline(), atoi)[1] 85 53 f.readline() 86 87 54 for i in range(no_ghost_nodes): 88 55 f.readline() 89 90 56 # skip over the edges 91 92 57 f.readline() 93 58 f.readline() … … 95 60 no_edges = str2array(f.readline(), atoi)[1] 96 61 f.readline() 97 98 62 for i in range(no_edges): 99 63 f.readline() 100 101 64 # read the triangles 102 103 65 f.readline() 104 66 f.readline() 105 67 f.readline() 106 no_triangles = str2array(f.readline(), atoi)[1] 68 #STARTMH160505 69 no_triangles = str2array(f.readline(), atoi)[1] #number of triangles listed in mg output file 70 #triangles_per_proc.append(no_triangles) 71 f.readline() 72 for i in range(no_triangles): #loop over all triangles in input file, checking for duplication 73 line = str2array(f.readline(), atoi) 74 line.sort() #for easy comparison with triangles listed so far 75 if (i==0):#always list the first triangle. This ensures that loop over t below is never empty 76 triangles.append(line) 77 continue 78 for t in range(shape(triangles)[0]): #for all the (non-duplicated) triangles we've seen so far 79 if (triangles[t]==line): #check to see if any of them match the triangle just read 80 break 81 if (triangles[t]!=line): #need to add line to list 82 triangles.append(line) 83 no_triangles=shape(triangles)[0]-sum(triangles_per_proc) 107 84 triangles_per_proc.append(no_triangles) 108 f.readline() 109 110 for i in range(no_triangles): 111 line = str2array(f.readline(), atoi) 112 triangles.append(line) 113 85 #ENDMH160505 114 86 # skip extra space 115 116 87 f.readline() 117 88 f.readline() 118 89 f.readline() 119 90 f.readline() 120 121 91 return nodes, triangles, nodes_per_proc 122 123 92 ################################################################################## 124 93 # Convert the format of the data to that used by pyvolution 125 94 ################################################################################## 126 127 95 #Now renumber the nodes as 0,1,2,... and make the corresponding changes to the triangle definitions, 128 96 #changing the orientation if necessary. 129 130 97 def restructure(nodes, triangles): 131 132 98 Nnodes=shape(nodes)[0] #the number of nodes (or vertices) 133 99 Ntriangles=shape(triangles)[0] #the number of triangles 134 100 index={} #a dictionary mapping existing node ids to the new ids 0,1,2,... 135 136 101 GAnodes = [] 137 138 102 for node_idnew in range(Nnodes): #move through the list of nodes, renumbering to 0,1,2, ... 139 103 index[nodes[node_idnew][0]]=node_idnew #make the dictionary entry, for later use by triangles 140 104 GAnodes.append(nodes[node_idnew][1:3]) 141 105 # nodes[node_idnew][0]=node_idnew #renumber the node 142 143 144 145 #Now loop over the triangles, changing the node ids and orientation. 106 #Now loop over the triangles, changing the node ids and orientation. 146 107 for t in range(Ntriangles): 147 108 x=[] #x-coordinates of the three nodes (vertices) … … 153 114 y.append(nodes[n][2]) 154 115 if ((x[1]-x[0])*(y[2]-y[0])-(x[2]-x[0])*(y[1]-y[0])<0): #need to change the orientation 155 156 157 116 triangles[t][2]=triangles[t][0]#swap the 0th and 2nd nodes 158 117 triangles[t][0]=n 159 118 del(x) 119 del(y) 160 120 del(index) #delete the dictionary 161 162 121 return GAnodes, triangles 163 164 165 122 def mg2ga(f): 166 167 123 [nodes, triangles, nodes_per_proc] = readmg(f) 168 124 [nodes, triangles] = restructure(nodes, triangles) 169 170 125 return nodes, triangles, nodes_per_proc
Note: See TracChangeset
for help on using the changeset viewer.