Changeset 1403


Ignore:
Timestamp:
May 16, 2005, 6:06:14 PM (20 years ago)
Author:
matthew
Message:

No longer storing duplicate triangles

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/parallel/mg2ga.py

    r1398 r1403  
    1 import sys
    2 from os import sep
    3 sys.path.append('..'+sep+'pyvolution')
    4 
    5 
    6 
    71from string import atoi
    82from string import atof
    9 
    10 
    113from general_mesh import General_mesh as Mesh
    124from Numeric import shape
    13 
    145##################################################################################
    156#                   TO BE REPLACED BY SPLIT
    167##################################################################################
    17 
    18 
    198def str2array(str,conv):
    209    l = len(str)
     
    3120        a = b
    3221    return data
    33 
    34 
    3522##################################################################################
    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                 
    3724##################################################################################
    38 
    39 
    4025def readmg(f):
    41 
    42     # read the nuber of processors
    43 
     26    # read the number of processors
    4427    f.readline()
    4528    no_proc = str2array(f.readline(), atoi)[0]
    46 
    4729    for i in range(5):
    4830        f.readline();
    49 
    5031    # nodes list
    51 
    5232    nodes = []
    53 
    5433    # triangle list
    55 
    5634    triangles = []
    57 
    5835    # know first nodes_per_proc[0] belong to cell 0, next
    5936    # nodes_per_proc[1] belong to cell 1 etc.
    60 
    61     nodes_per_proc = []
     37    nodes_per_proc = []
    6238    triangles_per_proc = []
    63 
    6439    # loop over the processors
    65 
    6640    for q in range(no_proc):
    67 
    6841        print q
    69 
    7042        # read the nodes
    71 
    7243        no_nodes = str2array(f.readline(), atoi)[1]
    7344        nodes_per_proc.append(no_nodes)
    7445        f.readline()
    75 
    7646        for i in range(no_nodes):
    7747            line = str2array(f.readline(), atof)
    7848            line[0] = int(line[0])
    7949            nodes.append(line[0:3])
    80 
    8150        # skip over the ghost nodes
    82 
    8351        f.readline()
    8452        no_ghost_nodes = str2array(f.readline(), atoi)[1]
    8553        f.readline()
    86 
    8754        for i in range(no_ghost_nodes):
    8855            f.readline()
    89 
    9056        # skip over the edges
    91 
    9257        f.readline()
    9358        f.readline()
     
    9560        no_edges = str2array(f.readline(), atoi)[1]
    9661        f.readline()
    97 
    9862        for i in range(no_edges):
    9963            f.readline()
    100 
    10164        # read the triangles
    102 
    10365        f.readline()
    10466        f.readline()
    10567        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)
    10784        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
    11486        # skip extra space
    115 
    11687        f.readline()
    11788        f.readline()
    11889        f.readline()
    11990        f.readline()
    120 
    12191    return nodes, triangles, nodes_per_proc
    122 
    12392##################################################################################
    12493# Convert the format of the data to that used by pyvolution
    12594##################################################################################
    126 
    12795#Now renumber the nodes as 0,1,2,... and make the corresponding changes to the triangle definitions,
    12896#changing the orientation if necessary.
    129 
    13097def restructure(nodes, triangles):
    131 
    13298    Nnodes=shape(nodes)[0] #the number of nodes (or vertices)
    13399    Ntriangles=shape(triangles)[0] #the number of triangles
    134100    index={} #a dictionary mapping existing node ids to the new ids 0,1,2,...
    135 
    136101    GAnodes = []
    137 
    138102    for node_idnew in range(Nnodes): #move through the list of nodes, renumbering to 0,1,2, ...
    139103        index[nodes[node_idnew][0]]=node_idnew #make the dictionary entry, for later use by triangles
    140104        GAnodes.append(nodes[node_idnew][1:3])
    141105#        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.
    146107    for t in range(Ntriangles):
    147108        x=[] #x-coordinates of the three nodes (vertices)
     
    153114            y.append(nodes[n][2])
    154115        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 
    157116            triangles[t][2]=triangles[t][0]#swap the 0th and 2nd nodes
    158117            triangles[t][0]=n
    159 
     118    del(x)
     119    del(y)           
    160120    del(index) #delete the dictionary
    161 
    162121    return GAnodes, triangles
    163 
    164 
    165122def mg2ga(f):
    166 
    167123    [nodes, triangles, nodes_per_proc] = readmg(f)
    168124    [nodes, triangles] = restructure(nodes, triangles)
    169 
    170125    return nodes, triangles, nodes_per_proc
Note: See TracChangeset for help on using the changeset viewer.