Ignore:
Timestamp:
Aug 1, 2007, 10:22:11 AM (17 years ago)
Author:
duncan
Message:

Optimising search_functions.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/fit_interpolate/search_functions.py

    r4599 r4649  
    9797
    9898        # Find triangle that contains x (if any) and interpolate
    99         element_found, sigma0, sigma1, sigma2, k =\
    100                        find_triangle_compute_interpolation(mesh,
    101                                                            triangle_list,
    102                                                            x)
     99       
     100        for k, _ in triangle_list:
     101            element_found, sigma0, sigma1, sigma2, k =\
     102                           find_triangle_compute_interpolation(mesh,
     103                                                               k,
     104                                                               x)
     105
     106            if element_found is True:
     107                # Don't look for any other triangles in the triangle list
     108                break
    103109
    104110        if element_found is True:
    105             # Don't look for any other triangle           
     111            # Don't look for any other triangle_lists from the
     112            # candidate_vertices
    106113            break
    107 
    108114       
    109115    return element_found, sigma0, sigma1, sigma2, k
     
    111117
    112118           
    113 def find_triangle_compute_interpolation(mesh, triangle_list, x):
     119def find_triangle_compute_interpolation(mesh, k, x):
    114120    """Compute linear interpolation of point x and triangle k in mesh.
    115121    It is assumed that x belongs to triangle k.
    116122    """
    117123
    118     element_found = False
    119     for k, _ in triangle_list:
    120         # Get the three vertex_points of candidate triangle k
    121         xi0, xi1, xi2 = mesh.get_vertex_coordinates(triangle_id=k)           
    122        
    123         # Get the three normals
    124         n0 = mesh.get_normal(k, 0)
    125         n1 = mesh.get_normal(k, 1)
    126         n2 = mesh.get_normal(k, 2)           
     124    # Get the three vertex_points of candidate triangle k
     125    xi0, xi1, xi2 = mesh.get_vertex_coordinates(triangle_id=k)
     126
     127    # this is where we can call some fast c code.
     128    xmax = max(xi0[0], xi1[0], xi2[0])
     129    xmin = min(xi0[0], xi1[0], xi2[0])
     130    ymax = max(xi0[1], xi1[1], xi2[1])
     131    ymin = min(xi0[1], xi1[1], xi2[1])
     132
     133    # Integrity check - machine precision is too hard
     134    # so we use hardwired single precision
     135    epsilon = 1.0e-6
     136   
     137    if  x[0] > xmax + epsilon:
     138        return False,0,0,0,0
     139    if  x[0] < xmin - epsilon:
     140        return False,0,0,0,0
     141    if  x[1] > ymax + epsilon:
     142        return False,0,0,0,0
     143    if  x[1] < ymin - epsilon:
     144        return False,0,0,0,0
     145   
     146   
     147    # Get the three normals
     148    n0 = mesh.get_normal(k, 0)
     149    n1 = mesh.get_normal(k, 1)
     150    n2 = mesh.get_normal(k, 2)           
    127151       
    128152       
    129         # Compute interpolation
    130         sigma2 = dot((x-xi0), n2)/dot((xi2-xi0), n2)
    131         sigma0 = dot((x-xi1), n0)/dot((xi0-xi1), n0)
    132         sigma1 = dot((x-xi2), n1)/dot((xi1-xi2), n1)
     153    # Compute interpolation
     154    sigma2 = dot((x-xi0), n2)/dot((xi2-xi0), n2)
     155    sigma0 = dot((x-xi1), n0)/dot((xi0-xi1), n0)
     156    sigma1 = dot((x-xi2), n1)/dot((xi1-xi2), n1)
    133157
    134         # Integrity check - machine precision is too hard
    135         # so we use hardwired single precision
    136         epsilon = 1.0e-6
    137         delta = abs(sigma0+sigma1+sigma2-1.0) # Should be close to zero
    138         msg = 'abs(sigma0+sigma1+sigma2-1) = %.15e, eps = %.15e'\
    139               %(delta, epsilon)
    140         assert delta < epsilon, msg
     158    delta = abs(sigma0+sigma1+sigma2-1.0) # Should be close to zero
     159    msg = 'abs(sigma0+sigma1+sigma2-1) = %.15e, eps = %.15e'\
     160          %(delta, epsilon)
     161    assert delta < epsilon, msg
    141162
    142163
    143         # Check that this triangle contains the data point
    144         # Sigmas are allowed to get negative within
    145         # machine precision on some machines (e.g. nautilus)
    146         epsilon = get_machine_precision() * 2
    147         if sigma0 >= -epsilon and sigma1 >= -epsilon and sigma2 >= -epsilon:
    148             element_found = True
    149             break
    150            
     164    # Check that this triangle contains the data point
     165    # Sigmas are allowed to get negative within
     166    # machine precision on some machines (e.g. nautilus)
     167    epsilon = get_machine_precision() * 2
     168    if sigma0 >= -epsilon and sigma1 >= -epsilon and sigma2 >= -epsilon:
     169        element_found = True
     170    else:
     171        element_found = False
    151172    return element_found, sigma0, sigma1, sigma2, k
Note: See TracChangeset for help on using the changeset viewer.