Changeset 4593


Ignore:
Timestamp:
Jul 5, 2007, 12:43:43 PM (17 years ago)
Author:
ole
Message:

Work on search_functions testing

Location:
anuga_core/source/anuga
Files:
4 edited

Legend:

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

    r4536 r4593  
    262262       
    263263
    264     def get_vertex_coordinates(self, absolute=False):
     264    def get_vertex_coordinates(self,
     265                               triangle_id=None,
     266                               absolute=False):
    265267        """Return vertex coordinates for all triangles.
    266268       
     
    269271        M the number of triangles in the mesh.
    270272
     273        if triangle_id is specified (an integer) the 3 vertex coordinates
     274        for triangle_id are returned.
     275       
    271276        Boolean keyword argument absolute determines whether coordinates
    272277        are to be made absolute by taking georeference into account
     
    278283            if not self.geo_reference.is_absolute():
    279284                V = self.geo_reference.get_absolute(V)
     285
     286        if triangle_id is None:       
     287            return V
     288        else:
     289            i = triangle_id
     290            msg = 'triangle_id must be an integer'
     291            assert int(i) == i, msg
     292            assert 0 <= i < self.number_of_triangles
    280293           
    281         return V
     294            i3 = 3*i
     295            return array([V[i3,:], V[i3+1,:], V[i3+2,:]])
    282296
    283297
     
    288302        """
    289303
    290         V = self.get_vertex_coordinates(absolute=absolute)
    291         return V[3*i+j, :]
     304        msg = 'vertex id j must be an integer in [0,1,2]'
     305        assert j in [0,1,2], msg
     306       
     307        V = self.get_vertex_coordinates(triangle_id=i,
     308                                        absolute=absolute)
     309        return V[j,:]
     310   
    292311
    293312
  • anuga_core/source/anuga/abstract_2d_finite_volumes/test_general_mesh.py

    r4478 r4593  
    4141                k = triangles[i,j]  #Index of vertex j in triangle i
    4242                assert allclose(V[3*i+j,:], nodes[k])
     43
     44
     45    def test_get_vertex_coordinates_triangle_id(self):
     46        """test_get_vertex_coordinates_triangle_id
     47        Test that vertices for one triangle can be returned.
     48        """
     49        from mesh_factory import rectangular
     50        from Numeric import zeros, Float
     51
     52        #Create basic mesh
     53        nodes, triangles, _ = rectangular(1, 3)
     54        domain = General_mesh(nodes, triangles)
     55
     56
     57        assert allclose(domain.get_nodes(), nodes)
     58
     59
     60        M = domain.number_of_triangles       
     61
     62        for i in range(M):
     63            V = domain.get_vertex_coordinates(triangle_id=i)
     64            assert V.shape[0] == 3
     65
     66            for j in range(3):
     67                k = triangles[i,j]  #Index of vertex j in triangle i
     68                assert allclose(V[j,:], nodes[k])
    4369
    4470
     
    197223#-------------------------------------------------------------
    198224if __name__ == "__main__":
    199     suite = unittest.makeSuite(Test_General_Mesh,'test_vertex_value_indices')
     225    suite = unittest.makeSuite(Test_General_Mesh,'test')   
     226    #suite = unittest.makeSuite(Test_General_Mesh,'test_vertex_value_indices')
    200227    runner = unittest.TextTestRunner()
    201228    runner.run(suite)
  • anuga_core/source/anuga/fit_interpolate/search_functions.py

    r4590 r4593  
    1313    """
    1414    Find the triangle (element) that the point x is in.
     15
     16    Inputs:
     17        root: A quad tree of the vertices
     18        mesh: The underlying mesh
     19        x:    The point being placed
    1520   
    16     root: A quad tree of the vertices
    17     Return the associated sigma and k values
    18     (and if the element was found) .
     21    Return:
     22        element_found, sigma0, sigma1, sigma2, k
     23
     24        where
     25        element_found: True if a triangle containing x was found
     26        sigma0, sigma1, sigma2: The interpolated values
     27        k: Index of triangle (if found)
     28
    1929    """
    2030    #Find triangle containing x:
     
    5565    This is called by search_tree_of_vertices once the appropriate node
    5666    has been found from the quad tree.
     67   
    5768
    5869    This function is responsible for most of the compute time in
     
    6778    sigma0 = -10.0
    6879    sigma1 = -10.0
    69     k = -10.0
    70     #print "*$* candidate_vertices", candidate_vertices
     80    k = -10
     81   
    7182    #For all vertices in same cell as point x
    7283    for v in candidate_vertices:
     
    7990       
    8091        #for each triangle id (k) which has v as a vertex
    81 
    8292        vertexlist = mesh.get_triangles_and_vertices_per_node(node=v)
    8393        for k, _ in vertexlist:
    84             #Get the three vertex_points of candidate triangle
    85             xi0 = mesh.get_vertex_coordinate(k, 0)
    86             xi1 = mesh.get_vertex_coordinate(k, 1)
    87             xi2 = mesh.get_vertex_coordinate(k, 2)
     94            #Get the three vertex_points of candidate triangle k
     95            xi0, xi1, xi2 = mesh.get_vertex_coordinates(triangle_id=k)           
    8896
    89             #Get the three normals
    90             n0 = mesh.get_normal(k, 0)
    91             n1 = mesh.get_normal(k, 1)
    92             n2 = mesh.get_normal(k, 2)
     97            # Get the three normals (faster than using API)
     98            n0 = mesh.normals[k,0:2]
     99            n1 = mesh.normals[k,2:4]
     100            n2 = mesh.normals[k,4:6]           
     101
    93102           
    94103            #Compute interpolation
     
    97106            sigma1 = dot((x-xi2), n1)/dot((xi1-xi2), n1)
    98107
    99             # Integrity check - machine precision is too hard.
    100             #epsilon = get_machine_precision()           
    101             # Use single precision as we used to here
     108            # Integrity check - machine precision is too hard
     109            # so we use hardwired single precision
    102110            epsilon = 1.0e-6
    103111            msg = 'abs(sigma0+sigma1+sigma2-1) = %.15e, eps = %.15e'\
     
    105113            assert abs(sigma0 + sigma1 + sigma2 - 1.0) < epsilon, msg
    106114
     115
    107116            #Check that this triangle contains the data point
    108117           
    109118            # Sigmas are allowed to get negative within
    110             # machine precision on some machines (e.g nautilus)
     119            # machine precision on some machines (e.g. nautilus)
    111120            epsilon = get_machine_precision() * 2
    112             if sigma0 >= -epsilon and sigma1 >= -epsilon and sigma2>= -epsilon:
     121            if sigma0 >= -epsilon and sigma1 >= -epsilon and sigma2 >= -epsilon:
    113122                element_found = True
    114123                break
    115124           
    116125        if element_found is True:
    117             #Don't look for any other triangle
     126            # Don't look for any other triangle
    118127            break
     128       
    119129    return element_found, sigma0, sigma1, sigma2, k
    120130
  • anuga_core/source/anuga/fit_interpolate/test_search_functions.py

    r4590 r4593  
    33
    44import unittest
    5 from search_functions import *
     5from search_functions import search_tree_of_vertices
     6from search_functions import _search_triangles_of_vertices
     7
    68
    79from Numeric import zeros, array, allclose
     
    911from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
    1012from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular
    11 
     13from anuga.utilities.polygon import is_inside_polygon
    1214from anuga.utilities.quad import build_quadtree
    1315
     
    4951        mesh.check_integrity()
    5052
    51         #print mesh.nodes
    52         #print mesh.triangles
    5353        root = build_quadtree(mesh, max_points_per_cell = 1)
    54         #print 'root', root.show()
    5554
    56         x = [0.7, 0.7]
     55        x = [0.2, 0.7]
    5756        found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x)
    58         #print k
    59         # What is k??
     57        assert k == 1 # Triangle one
    6058        assert found is True
     59
     60    def test_bigger(self):
     61        """test_larger mesh
     62        """
     63
     64        points, vertices, boundary = rectangular(4, 4, 1, 1)
     65        mesh = Mesh(points, vertices, boundary)
     66
     67        #Test that points are arranged in a counter clock wise order
     68        mesh.check_integrity()
     69
     70        root = build_quadtree(mesh, max_points_per_cell = 4)
     71
     72        for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
     73                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
     74                  [10, 3]]:
     75               
     76            found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x)
     77
     78            if k >= 0:
     79                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
     80                assert is_inside_polygon(x, V)
     81                assert found is True
     82                #print k, x
     83            else:
     84                assert found is False               
     85
     86       
     87
     88    def test_large(self):
     89        """test_larger mesh and different quad trees
     90        """
     91
     92        points, vertices, boundary = rectangular(10, 12, 1, 1)
     93        mesh = Mesh(points, vertices, boundary)
     94
     95        #Test that points are arranged in a counter clock wise order
     96        mesh.check_integrity()
     97
     98       
     99        for m in range(8):
     100            root = build_quadtree(mesh, max_points_per_cell = m)
     101            #print m, root.show()
     102
     103            for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
     104                      [0.1,0.9], [0.4,0.6], [0.9,0.1],
     105                      [10, 3]]:
     106               
     107                found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x)
     108
     109                if k >= 0:
     110                    V = mesh.get_vertex_coordinates(k) # nodes for triangle k
     111                    assert is_inside_polygon(x, V)
     112                    assert found is True
     113                else:
     114                    assert found is False               
     115
     116               
     117
     118    def test_underlying_function(self):
     119        """test_larger mesh and different quad trees
     120        """
     121
     122        points, vertices, boundary = rectangular(2, 2, 1, 1)
     123        mesh = Mesh(points, vertices, boundary)
     124
     125        root = build_quadtree(mesh, max_points_per_cell = 4)
     126
     127        # One point
     128        x = [0.5, 0.5]
     129        candidate_vertices = root.search(x[0], x[1])
     130
     131        #print x, candidate_vertices
     132        found, sigma0, sigma1, sigma2, k = \
     133               _search_triangles_of_vertices(mesh,
     134                                             candidate_vertices,
     135                                             x)
     136
     137        if k >= 0:
     138            V = mesh.get_vertex_coordinates(k) # nodes for triangle k
     139            assert is_inside_polygon(x, V)
     140            assert found is True
     141        else:
     142            assert found is False               
     143
     144       
     145
     146        # More points   
     147        for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
     148                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
     149                  [10, 3]]:
     150               
     151            candidate_vertices = root.search(x[0], x[1])
     152
     153            #print x, candidate_vertices
     154            found, sigma0, sigma1, sigma2, k = \
     155                   _search_triangles_of_vertices(mesh,
     156                                                 candidate_vertices,
     157                                                 x)
     158            if k >= 0:
     159                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
     160                assert is_inside_polygon(x, V)
     161                assert found is True
     162            else:
     163                assert found is False               
     164           
     165
     166       
     167               
    61168
    62169       
Note: See TracChangeset for help on using the changeset viewer.