Changeset 4478


Ignore:
Timestamp:
May 23, 2007, 12:13:06 PM (17 years ago)
Author:
ole
Message:

Retired old structure vertexlist completely and replaced all references to it with the new inverted triangle structured defined in general_mesh.py. This closes ticket:154

All tests and autovalidation passed.

Location:
anuga_core/source/anuga
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/abstract_2d_finite_volumes/general_mesh.py

    r4474 r4478  
    210210
    211211       
    212         # Build vertex list
    213         if verbose: print 'Building vertex list'         
    214         self.build_vertexlist()
     212        # Build structure listing which trianglse belong to which nodet.
     213        if verbose: print 'Building inverted triangle structure'         
     214        self.build_inverted_triangle_structure()
    215215
    216216           
     
    379379
    380380
    381     def build_vertexlist(self):
    382         """Build information about which triangles belong to each node
     381    def get_triangles_and_vertices_per_node(self, node=None):
     382        """Get triangles associated with given node.
     383
     384        Return list of triangle_ids, vertex_ids for specified node.
     385        If node in None or absent, this information will be returned
     386        for all (full) nodes.
     387        """
     388
     389        triangle_list = []
     390        if node is not None:
     391            # Get index for this node
     392            first = sum(self.number_of_triangles_per_node[:node])
     393           
     394            # Get number of triangles for this node
     395            count = self.number_of_triangles_per_node[node]
     396
     397            for i in range(count):
     398                index = self.vertex_value_indices[first+i]
     399
     400                volume_id = index / 3
     401                vertex_id = index % 3
     402
     403                triangle_list.append( (volume_id, vertex_id) )
     404        else:
     405            # Get info for all nodes recursively.
     406            # If need be, we can speed this up by
     407            # working directly with the inverted triangle structure
     408            for i in range(self.number_of_full_nodes):
     409                L = self.get_triangles_and_vertices_per_node(node=i)
     410                triangle_list.append(L)
     411
     412        return triangle_list
     413       
     414
     415    def build_inverted_triangle_structure(self):
     416        """Build structure listing triangles belonging to each node
    383417
    384418        Two arrays are created and store as mesh attributes
     
    441475        """
    442476
    443         # FIXME (Ole): Refactor this based on algorithm in test and get
    444         # rid of the old vertexlist
    445        
     477        # Count number of triangles per node
     478        number_of_triangles_per_node = zeros(self.number_of_nodes)
     479        for volume_id, triangle in enumerate(self.triangles):
     480            for vertex_id in triangle:
     481                number_of_triangles_per_node[vertex_id] += 1
     482
     483        # Allocate space for inverted structure
     484        number_of_entries = sum(number_of_triangles_per_node)
     485        vertex_value_indices = zeros(number_of_entries)
     486
     487        # Register (triangle, vertex) indices for each node
    446488        vertexlist = [None]*self.number_of_nodes
    447         for i in range(self.number_of_triangles):
    448 
    449             a = self.triangles[i, 0]
    450             b = self.triangles[i, 1]
    451             c = self.triangles[i, 2]
    452 
    453             #Register the vertices v as lists of
    454             #(triangle_id, vertex_id) tuples associated with them
    455             #This is used for averaging multiple vertex values.
    456             for vertex_id, v in enumerate([a,b,c]):
    457                 if vertexlist[v] is None:
    458                     vertexlist[v] = []
    459 
    460                 vertexlist[v].append( (i, vertex_id) )
    461 
    462 
    463         # Register number of triangles touching each nodes
    464         number_of_triangles_per_node = zeros(self.number_of_nodes)       
    465         number_of_entries = 0       
    466         for i, vertices in enumerate(vertexlist):
    467             try:
    468                 v = len(vertices)
    469             except:
    470                 v = 0 # Lone node - e.g. not part of the mesh
    471                
    472             number_of_triangles_per_node[i] = v
    473             number_of_entries += v
    474                
    475 
    476         # Build vertex value index array
    477         vertex_value_indices = zeros(number_of_entries)
     489        for volume_id in range(self.number_of_triangles):
     490
     491            a = self.triangles[volume_id, 0]
     492            b = self.triangles[volume_id, 1]
     493            c = self.triangles[volume_id, 2]
     494
     495            for vertex_id, node_id in enumerate([a,b,c]):
     496                if vertexlist[node_id] is None:
     497                    vertexlist[node_id] = []
     498       
     499                vertexlist[node_id].append( (volume_id, vertex_id) )
     500
     501
     502        # Build inverted triangle index array
    478503        k = 0
    479504        for vertices in vertexlist:
    480505            if vertices is not None:
    481                 for i, v_id in vertices:
    482                     vertex_value_indices[k] = 3*i + v_id
    483 
     506                for volume_id, vertex_id in vertices:
     507                    vertex_value_indices[k] = 3*volume_id + vertex_id
     508                                       
    484509                    k += 1
    485510
    486         assert k == number_of_entries   
    487         self.vertexlist = vertexlist
    488 
     511        # Save structure
    489512        self.number_of_triangles_per_node = number_of_triangles_per_node
    490513        self.vertex_value_indices = vertex_value_indices
    491        
    492 
    493         #print
    494         #print number_of_triangles_per_node
    495         #print vertex_value_indices
    496         #print vertexlist
    497 
     514
     515
     516       
    498517
    499518    def get_extent(self, absolute=False):
  • anuga_core/source/anuga/abstract_2d_finite_volumes/quantity.py

    r4471 r4478  
    407407        elif location == 'unique vertices':
    408408            if indices is None:
    409                 self.edge_values[:] = X
     409                self.edge_values[:] = X  #FIXME (Ole): Shouldn't this be vertex_values?
    410410            else:
    411411
    412412                #Go through list of unique vertices
    413413                for unique_vert_id in indices:
    414                     triangles = self.domain.vertexlist[unique_vert_id]
    415 
     414
     415                    triangles = self.domain.get_triangles_and_vertices_per_node(node=unique_vert_id)
     416                   
    416417                    #In case there are unused points
    417                     if triangles is None: continue
    418 
     418                    if len(triangles) == 0:
     419                        continue
     420                   
    419421                    #Go through all triangle, vertex pairs
    420422                    #and set corresponding vertex value
     
    932934            #Go through list of unique vertices
    933935            for unique_vert_id in indices:
    934                 triangles = self.domain.vertexlist[unique_vert_id]
    935 
     936                triangles = self.domain.get_triangles_and_vertices_per_node(node=unique_vert_id)
     937                   
    936938                #In case there are unused points
    937                 if triangles is None:
     939                if len(triangles) == 0:
    938940                    msg = 'Unique vertex not associated with triangles'
    939941                    raise msg
     
    958960        """Set vertex values for all unique vertices based on input array A
    959961        which has one entry per unique vertex, i.e.
    960         one value for each row in array self.domain.coordinates or
    961         one value for each row in vertexlist.
     962        one value for each row in array self.domain.nodes.
    962963
    963964        indices is the list of vertex_id's that will be set.
     
    982983
    983984        #Go through list of unique vertices
     985       
    984986        for i_index, unique_vert_id in enumerate(vertex_list):
    985             triangles = self.domain.vertexlist[unique_vert_id]
    986 
    987             if triangles is None: continue #In case there are unused points
     987
     988
     989            triangles = self.domain.get_triangles_and_vertices_per_node(node=unique_vert_id)
     990                   
     991            #In case there are unused points
     992            if len(triangles) == 0: continue
    988993
    989994            #Go through all triangle, vertex pairs
  • anuga_core/source/anuga/abstract_2d_finite_volumes/test_general_mesh.py

    r4474 r4478  
    9696                    count[i] += 1
    9797
     98            #print count
     99            #
    98100            assert allclose(count, domain.number_of_triangles_per_node)
    99 
     101           
    100102            # Check indices
    101103            current_node = 0
     
    115117               
    116118
    117 
     119    def test_get_triangles_and_vertices_per_node(self):
     120        """test_get_triangles_and_vertices_per_node -
     121
     122        Test that tuples of triangle, vertex can be extracted
     123        from inverted triangles structure
     124
     125        """
     126
     127        a = [0.0, 0.0]
     128        b = [0.0, 2.0]
     129        c = [2.0, 0.0]
     130        d = [0.0, 4.0]
     131        e = [2.0, 2.0]
     132        f = [4.0, 0.0]
     133
     134        nodes = array([a, b, c, d, e, f])
     135        #bac, bce, ecf, dbe, daf, dae
     136        triangles = array([[1,0,2], [1,2,4], [4,2,5], [3,1,4]])
     137
     138        domain = General_mesh(nodes, triangles)
     139
     140        # One node
     141        L = domain.get_triangles_and_vertices_per_node(node=2)
     142
     143        assert allclose(L[0], [0, 2])
     144        assert allclose(L[1], [1, 1])
     145        assert allclose(L[2], [2, 1])
     146
     147        # All nodes
     148        ALL = domain.get_triangles_and_vertices_per_node()
     149
     150        assert len(ALL) == 6
     151        for i, Lref in enumerate(ALL):
     152            L = domain.get_triangles_and_vertices_per_node(node=i)
     153            assert allclose(L, Lref)
     154           
     155
     156       
     157
     158       
    118159
    119160
     
    156197#-------------------------------------------------------------
    157198if __name__ == "__main__":
    158     suite = unittest.makeSuite(Test_General_Mesh,'test')
     199    suite = unittest.makeSuite(Test_General_Mesh,'test_vertex_value_indices')
    159200    runner = unittest.TextTestRunner()
    160201    runner.run(suite)
  • anuga_core/source/anuga/fit_interpolate/search_functions.py

    r3865 r4478  
    6464        #FIXME (DSG-DSG): this catches verts with no triangle.
    6565        #Currently pmesh is producing these.
    66         #this should be stopped,
    67         if mesh.vertexlist[v] is None:
     66        #this should be stopped,
     67
     68        if mesh.number_of_triangles_per_node[v] == 0:
    6869            continue
     70       
    6971        #for each triangle id (k) which has v as a vertex
    70         for k, _ in mesh.vertexlist[v]:
     72
     73        vertexlist = mesh.get_triangles_and_vertices_per_node(node=v)
     74        for k, _ in vertexlist:
    7175            #Get the three vertex_points of candidate triangle
    7276            xi0 = mesh.get_vertex_coordinate(k, 0)
Note: See TracChangeset for help on using the changeset viewer.