Ignore:
Timestamp:
Mar 19, 2009, 1:43:34 PM (16 years ago)
Author:
rwilson
Message:

Merged trunk into numpy, all tests and validations work.

Location:
branches/numpy/anuga/fit_interpolate
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/numpy/anuga/fit_interpolate/fit.py

    r6533 r6553  
    280280           
    281281            element_found, sigma0, sigma1, sigma2, k = \
    282                            search_tree_of_vertices(self.root, self.mesh, x)
     282                           search_tree_of_vertices(self.root, x)
    283283           
    284284            if element_found is True:
     
    301301                        AtA[j,k] += sigmas[j]*sigmas[k]
    302302            else:
    303                 # FIXME(Ole): This is the message referred to in ticket:314
    304                
    305303                flag = is_inside_polygon(x,
    306304                                         self.mesh_boundary_polygon,
     
    310308                assert flag is True, msg               
    311309               
     310                # FIXME(Ole): This is the message referred to in ticket:314
    312311                minx = min(self.mesh_boundary_polygon[:,0])
    313312                maxx = max(self.mesh_boundary_polygon[:,0])               
     
    317316                msg += 'Mesh boundary extent is (%.f, %.f), (%.f, %.f)'\
    318317                    % (minx, maxx, miny, maxy)
    319                
    320 
    321                 raise Exception(msg)
     318                raise RuntimeError, msg
     319
    322320            self.AtA = AtA
    323321
     
    375373                self.build_fit_subset(points, z, verbose=verbose)
    376374
     375                # FIXME(Ole): I thought this test would make sense here
     376                # See test_fitting_example_that_crashed_2 in test_shallow_water_domain.py
     377                # Committed 11 March 2009
     378                msg = 'Matrix AtA was not built'
     379                assert self.AtA is not None, msg
     380               
     381                #print 'Matrix was built OK'
     382
    377383               
    378384            point_coordinates = None
     
    382388        if point_coordinates is None:
    383389            if verbose: print 'Warning: no data points in fit'
    384             #assert self.AtA != None, 'no interpolation matrix'
    385             #assert self.Atz != None
    386             assert not self.AtA is None, 'no interpolation matrix'
    387             assert not self.Atz is None
     390            msg = 'No interpolation matrix.'
     391            assert self.AtA is not None, msg
     392            assert self.Atz is not None
    388393           
    389394            # FIXME (DSG) - do  a message
     
    471476                alpha=DEFAULT_ALPHA,
    472477                verbose=False,
    473                 acceptable_overshoot=1.01,
    474                 # FIXME: Move to config - this value is assumed in caching test
    475                 # FIXME(Ole): Just realised that this was never implemented (29 Jan 2009). I suggest removing it altogether.
    476478                mesh_origin=None,
    477479                data_origin=None,
     
    490492              'alpha': alpha,
    491493              'verbose': verbose,
    492               'acceptable_overshoot': acceptable_overshoot,
    493494              'mesh_origin': mesh_origin,
    494495              'data_origin': data_origin,
     
    546547                 alpha=DEFAULT_ALPHA,
    547548                 verbose=False,
    548                  acceptable_overshoot=1.01,
    549549                 mesh_origin=None,
    550550                 data_origin=None,
     
    572572          alpha: Smoothing parameter.
    573573
    574           acceptable overshoot: NOT IMPLEMENTED
    575           controls the allowed factor by which
    576           fitted values
    577           may exceed the value of input data. The lower limit is defined
    578           as min(z) - acceptable_overshoot*delta z and upper limit
    579           as max(z) + acceptable_overshoot*delta z
    580          
    581 
    582574          mesh_origin: A geo_reference object or 3-tuples consisting of
    583575              UTM zone, easting and northing.
    584576              If specified vertex coordinates are assumed to be
    585577              relative to their respective origins.
    586          
    587578
    588579          point_attributes: Vector or array of data at the
  • branches/numpy/anuga/fit_interpolate/interpolate.py

    r6428 r6553  
    484484            x = point_coordinates[i]
    485485            element_found, sigma0, sigma1, sigma2, k = \
    486                            search_tree_of_vertices(self.root, self.mesh, x)
    487 
    488             # Update interpolation matrix A if necessary
     486                           search_tree_of_vertices(self.root, x)
     487           
     488            # Update interpolation matrix A if necessary
    489489            if element_found is True:
    490490                # Assign values to matrix A
  • branches/numpy/anuga/fit_interpolate/search_functions.py

    r6304 r6553  
    88import time
    99
     10from anuga.utilities.polygon import is_inside_triangle
    1011from anuga.utilities.numerical_tools import get_machine_precision
    1112from anuga.config import max_float
     
    1819search_more_cells_time = initial_search_value
    1920
    20 #FIXME test what happens if a
    21 LAST_TRIANGLE = [[-10,[(num.array([max_float,max_float]),
    22                         num.array([max_float,max_float]),
    23                         num.array([max_float,max_float])),
    24                        (num.array([1,1]),
    25                         num.array([0,0]),
    26                         num.array([-1.1,-1.1]))]]]
     21# FIXME(Ole): Could we come up with a less confusing structure?
     22LAST_TRIANGLE = [[-10,
     23                   (num.array([[max_float, max_float],
     24                               [max_float, max_float],
     25                               [max_float, max_float]]),
     26                    (num.array([1.,1.]),     
     27                     num.array([0.,0.]),     
     28                     num.array([-1.1,-1.1])))]]
    2729
    28 def search_tree_of_vertices(root, mesh, x):
     30def search_tree_of_vertices(root, x):
    2931    """
    3032    Find the triangle (element) that the point x is in.
     
    3234    Inputs:
    3335        root: A quad tree of the vertices
    34         mesh: The underlying mesh
    3536        x:    The point being placed
    3637   
     
    4748    global search_more_cells_time
    4849
    49     #Find triangle containing x:
    50     element_found = False
    51 
    52     # This will be returned if element_found = False
    53     sigma2 = -10.0
    54     sigma0 = -10.0
    55     sigma1 = -10.0
    56     k = -10.0
    57 
    5850    # Search the last triangle first
    59     try:
    60         element_found, sigma0, sigma1, sigma2, k = \
    61             _search_triangles_of_vertices(mesh, last_triangle, x)
    62     except:
    63         element_found = False
     51    element_found, sigma0, sigma1, sigma2, k = \
     52        _search_triangles_of_vertices(last_triangle, x)
    6453                   
    65     #print "last_triangle", last_triangle
    6654    if element_found is True:
    67         #print "last_triangle", last_triangle
    6855        return element_found, sigma0, sigma1, sigma2, k
    6956
    70     # This was only slightly faster than just checking the
    71     # last triangle and it significantly slowed down
    72     # non-gridded fitting
    73     # If the last element was a dud, search its neighbours
    74     #print "last_triangle[0][0]", last_triangle[0][0]
    75     #neighbours = mesh.get_triangle_neighbours(last_triangle[0][0])
    76     #print "neighbours", neighbours
    77     #neighbours = []
    78   #   for k in neighbours:
    79 #         if k >= 0:
    80 #             tri = mesh.get_vertex_coordinates(k,
    81 #                                                    absolute=True)
    82 #             n0 = mesh.get_normal(k, 0)
    83 #             n1 = mesh.get_normal(k, 1)
    84 #             n2 = mesh.get_normal(k, 2)
    85 #             triangle =[[k,(tri, (n0, n1, n2))]]
    86 #             element_found, sigma0, sigma1, sigma2, k = \
    87 #                            _search_triangles_of_vertices(mesh,
    88 #                                                          triangle, x)
    89 #             if element_found is True:
    90 #                 return element_found, sigma0, sigma1, sigma2, k
    91            
    92     #t0 = time.time()
     57   
    9358    # Get triangles in the cell that the point is in.
    9459    # Triangle is a list, first element triangle_id,
    9560    # second element the triangle
    9661    triangles = root.search(x[0], x[1])
     62    element_found, sigma0, sigma1, sigma2, k = \
     63                   _search_triangles_of_vertices(triangles, x)
     64
    9765    is_more_elements = True
    9866   
    99     element_found, sigma0, sigma1, sigma2, k = \
    100                    _search_triangles_of_vertices(mesh,
    101                                                  triangles, x)
    102     #search_one_cell_time += time.time()-t0
    103     #print "search_one_cell_time",search_one_cell_time
    104     #t0 = time.time()
    10567    while not element_found and is_more_elements:
    10668        triangles, branch = root.expand_search()
     
    10971            # been searched.  This is the last try
    11072            element_found, sigma0, sigma1, sigma2, k = \
    111                            _search_triangles_of_vertices(mesh, triangles, x)
     73                           _search_triangles_of_vertices(triangles, x)
    11274            is_more_elements = False
    11375        else:
    11476            element_found, sigma0, sigma1, sigma2, k = \
    115                        _search_triangles_of_vertices(mesh, triangles, x)
    116         #search_more_cells_time += time.time()-t0
    117     #print "search_more_cells_time", search_more_cells_time
     77                       _search_triangles_of_vertices(triangles, x)
     78                       
    11879       
    11980    return element_found, sigma0, sigma1, sigma2, k
    12081
    12182
    122 def _search_triangles_of_vertices(mesh, triangles, x):
    123     """Search for triangle containing x amongs candidate_vertices in mesh
     83def _search_triangles_of_vertices(triangles, x):
     84    """Search for triangle containing x amongs candidate_vertices in triangles
    12485
    12586    This is called by search_tree_of_vertices once the appropriate node
     
    13293    global last_triangle
    13394   
    134     # these statments are needed if triangles is empty
    135     #Find triangle containing x:
    136     element_found = False
    137 
    138     # This will be returned if element_found = False
     95    # These statments are needed if triangles is empty
    13996    sigma2 = -10.0
    14097    sigma0 = -10.0
    14198    sigma1 = -10.0
    14299    k = -10
    143    
    144     #For all vertices in same cell as point x
     100    # For all vertices in same cell as point x
     101    element_found = False   
    145102    for k, tri_verts_norms in triangles:
    146103        tri = tri_verts_norms[0]
    147         n0, n1, n2 = tri_verts_norms[1]
    148104        # k is the triangle index
    149105        # tri is a list of verts (x, y), representing a tringle
    150106        # Find triangle that contains x (if any) and interpolate
    151         element_found, sigma0, sigma1, sigma2 =\
    152                        find_triangle_compute_interpolation(tri, n0, n1, n2, x)
    153         if element_found is True:
     107       
     108        # Input check disabled to speed things up.
     109        if is_inside_triangle(x, tri,
     110                              closed=True,
     111                              check_inputs=False):
     112           
     113            n0, n1, n2 = tri_verts_norms[1]       
     114            sigma0, sigma1, sigma2 =\
     115                compute_interpolation_values(tri, n0, n1, n2, x)
     116               
     117            element_found = True   
     118
    154119            # Don't look for any other triangles in the triangle list
    155             last_triangle = [[k,tri_verts_norms]]
    156             break
     120            last_triangle = [[k, tri_verts_norms]]
     121            break           
     122           
    157123    return element_found, sigma0, sigma1, sigma2, k
    158124
    159125
    160126           
    161 def find_triangle_compute_interpolation(triangle, n0, n1, n2, x):
    162     """Compute linear interpolation of point x and triangle k in mesh.
    163     It is assumed that x belongs to triangle k.max_float
     127def compute_interpolation_values(triangle, n0, n1, n2, x):
     128    """Compute linear interpolation of point x and triangle.
     129   
     130    n0, n1, n2 are normal to the tree edges.
    164131    """
    165132
     
    167134    xi0, xi1, xi2 = triangle
    168135
    169     # this is where we can call some fast c code.
    170      
    171     # Integrity check - machine precision is too hard
    172     # so we use hardwired single precision
    173     epsilon = 1.0e-6
    174    
    175     if  x[0] > max(xi0[0], xi1[0], xi2[0]) + epsilon:
    176         # print "max(xi0[0], xi1[0], xi2[0])", max(xi0[0], xi1[0], xi2[0])
    177         return False,0,0,0
    178     if  x[0] < min(xi0[0], xi1[0], xi2[0]) - epsilon:
    179         return False,0,0,0
    180     if  x[1] > max(xi0[1], xi1[1], xi2[1]) + epsilon:
    181         return False,0,0,0
    182     if  x[1] < min(xi0[1], xi1[1], xi2[1]) - epsilon:
    183         return False,0,0,0
    184    
    185     # machine precision on some machines (e.g. nautilus)
    186     epsilon = get_machine_precision() * 2
    187    
    188     # Compute interpolation - return as soon as possible
    189     #  print "(xi0-xi1)", (xi0-xi1)
    190     # print "n0", n0
    191     # print "dot((xi0-xi1), n0)", dot((xi0-xi1), n0)
    192    
    193136    sigma0 = num.dot((x-xi1), n0)/num.dot((xi0-xi1), n0)
    194     if sigma0 < -epsilon:
    195         return False,0,0,0
    196137    sigma1 = num.dot((x-xi2), n1)/num.dot((xi1-xi2), n1)
    197     if sigma1 < -epsilon:
    198         return False,0,0,0
    199138    sigma2 = num.dot((x-xi0), n2)/num.dot((xi2-xi0), n2)
    200     if sigma2 < -epsilon:
    201         return False,0,0,0
    202    
    203     # epsilon = 1.0e-6
    204     # we want to speed this up, so don't do assertions
    205     #delta = abs(sigma0+sigma1+sigma2-1.0) # Should be close to zero
    206     #msg = 'abs(sigma0+sigma1+sigma2-1) = %.15e, eps = %.15e'\
    207     #      %(delta, epsilon)
    208     #assert delta < epsilon, msg
    209139
    210 
    211     # Check that this triangle contains the data point
    212     # Sigmas are allowed to get negative within
    213     # machine precision on some machines (e.g. nautilus)
    214     #if sigma0 >= -epsilon and sigma1 >= -epsilon and sigma2 >= -epsilon:
    215     #    element_found = True
    216     #else:
    217     #    element_found = False
    218     return True, sigma0, sigma1, sigma2
     140    return sigma0, sigma1, sigma2
    219141
    220142def set_last_triangle():
  • branches/numpy/anuga/fit_interpolate/test_search_functions.py

    r6360 r6553  
    55from search_functions import search_tree_of_vertices, set_last_triangle
    66from search_functions import _search_triangles_of_vertices
     7from search_functions import compute_interpolation_values
    78
    89from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
     
    1011from anuga.utilities.polygon import is_inside_polygon
    1112from anuga.utilities.quad import build_quadtree, Cell
    12 
     13from anuga.utilities.numerical_tools import ensure_numeric
     14from anuga.utilities.polygon import is_inside_polygon, is_inside_triangle   
     15
     16import numpy as num
    1317
    1418class Test_search_functions(unittest.TestCase):
     
    4953
    5054        x = [0.2, 0.7]
    51         found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x)
     55        found, s0, s1, s2, k = search_tree_of_vertices(root, x)
    5256        assert k == 1 # Triangle one
    5357        assert found is True
    5458
    5559    def test_bigger(self):
    56         """test_larger mesh
     60        """test_bigger
     61       
     62        test larger mesh
    5763        """
    5864
     
    6975                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
    7076                  [10, 3]]:
    71                
    72             found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x)
     77           
     78            found, s0, s1, s2, k = search_tree_of_vertices(root,
     79                                                           ensure_numeric(x))
    7380
    7481            if k >= 0:
     
    102109                      [10, 3]]:
    103110               
    104                 found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x)
     111                found, s0, s1, s2, k = search_tree_of_vertices(root, x)
    105112
    106113                if k >= 0:
     
    111118                    assert found is False               
    112119
    113                
     120           
     121                if m == k == 0: return   
    114122
    115123    def test_underlying_function(self):
     
    124132
    125133        # One point
    126         x = [0.5, 0.5]
     134        x = ensure_numeric([0.5, 0.5])
    127135        candidate_vertices = root.search(x[0], x[1])
    128136
    129137        #print x, candidate_vertices
    130138        found, sigma0, sigma1, sigma2, k = \
    131                _search_triangles_of_vertices(mesh,
    132                                              candidate_vertices,
     139               _search_triangles_of_vertices(candidate_vertices,
    133140                                             x)
    134141
     
    151158            #print x, candidate_vertices
    152159            found, sigma0, sigma1, sigma2, k = \
    153                    _search_triangles_of_vertices(mesh,
    154                                                  candidate_vertices,
    155                                                  x)
     160                   _search_triangles_of_vertices(candidate_vertices,
     161                                                 ensure_numeric(x))
    156162            if k >= 0:
    157163                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
     
    199205        x = [2.5, 1.5]
    200206        element_found, sigma0, sigma1, sigma2, k = \
    201                        search_tree_of_vertices(root, mesh, x)
     207                       search_tree_of_vertices(root, x)
    202208        # One point
    203209        x = [3.00005, 2.999994]
    204210        element_found, sigma0, sigma1, sigma2, k = \
    205                        search_tree_of_vertices(root, mesh, x)
     211                       search_tree_of_vertices(root, x)
    206212        assert element_found is True
    207213        assert k == 1
    208214       
    209 ################################################################################
    210 
     215       
     216    def test_compute_interpolation_values(self):
     217        """test_compute_interpolation_values
     218       
     219        Test that interpolation values are correc
     220       
     221        This test used to check element_found as output from
     222        find_triangle_compute_interpolation(triangle, n0, n1, n2, x)
     223        and that failed before 18th March 2009.
     224       
     225        Now this function no longer returns this flag, so the test
     226        is merely checknig the sigmas.
     227       
     228        """
     229       
     230        triangle = num.array([[306951.77151059, 6194462.14596986],
     231                              [306952.58403545, 6194459.65001246],
     232                              [306953.55109034, 6194462.0041216]])
     233
     234
     235        n0 = [0.92499377, -0.37998227]
     236        n1 = [0.07945684,  0.99683831]
     237        n2 = [-0.95088404, -0.30954732]
     238       
     239        x = [306953.344, 6194461.5]
     240       
     241        # Test that point is indeed inside triangle
     242        assert is_inside_polygon(x, triangle,
     243                                 closed=True, verbose=False)
     244        assert is_inside_triangle(x, triangle,
     245                                  closed=True, verbose=False)                                 
     246       
     247        sigma0, sigma1, sigma2 = \
     248            compute_interpolation_values(triangle, n0, n1, n2, x)
     249           
     250        msg = 'Point which is clearly inside triangle was not found'
     251        assert abs(sigma0 + sigma1 + sigma2 - 1) < 1.0e-6, msg
     252
     253#-------------------------------------------------------------
    211254if __name__ == "__main__":
    212     suite = unittest.makeSuite(Test_search_functions,'test')
    213     runner = unittest.TextTestRunner() #verbosity=1)
     255    suite = unittest.makeSuite(Test_search_functions, 'test')
     256    runner = unittest.TextTestRunner(verbosity=1)
    214257    runner.run(suite)
    215258   
Note: See TracChangeset for help on using the changeset viewer.