Changeset 2190


Ignore:
Timestamp:
Jan 9, 2006, 5:17:32 PM (19 years ago)
Author:
duncan
Message:

refactoring out search functions

Location:
inundation/fit_interpolate
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/fit_interpolate/interpolate.py

    r2189 r2190  
    2222from pyvolution.cg_solve import conjugate_gradient, VectorShapeError
    2323from coordinate_transforms.geo_reference import Geo_reference
    24 
    2524from pyvolution.quad import build_quadtree
    2625from utilities.numerical_tools import ensure_numeric
    2726from utilities.polygon import inside_polygon
    2827
     28from search_functions import search_tree_of_vertices
    2929
    3030class Interpolate:
     
    138138        A = Sparse(n,m)
    139139
    140 
    141         # I think this (along with other expanded_quad_searches stuff) can be removed
    142         #self.expanded_quad_searches = []
    143 
    144140        #Compute matrix elements
    145141        for i in range(n):
     
    151147           
    152148            element_found, sigma0, sigma1, sigma2, k = \
    153                            self._search_tree_of_vertices(x)   
     149                           search_tree_of_vertices(self.root, self.mesh, x)
     150           
    154151            #Update interpolation matrix A if necessary
    155152            if element_found is True:
     
    170167        return A
    171168
    172     def _search_tree_of_vertices(self, x):
     169    def _search_tree_of_vertices(self, root, mesh, x):
    173170        """
    174171        Find the triangle (element) that the point x is in.
    175172
     173        root: A quad tree of the vertices
    176174        Return the associated sigma and k values
    177175           (and if the element was found) .
     
    187185           
    188186        #Find vertices near x
    189         candidate_vertices = self.root.search(x[0], x[1])
     187        candidate_vertices = root.search(x[0], x[1])
    190188        is_more_elements = True
    191189
    192190        element_found, sigma0, sigma1, sigma2, k = \
    193                        self._search_triangles_of_vertices(candidate_vertices, x)
    194         #FIXME Do we need this var?
    195         # Exclude points outside the mesh before removing this.
    196         #first_expansion = True
     191                       self._search_triangles_of_vertices(mesh,
     192                                                          candidate_vertices, x)
    197193        while not element_found and is_more_elements:
    198             #if verbose: print 'Expanding search'
    199             #if first_expansion == True:
    200             #self.expanded_quad_searches.append(1)
    201             #first_expansion = False
    202             #else:
    203             #end = len(self.expanded_quad_searches) - 1
    204             #assert end >= 0
    205             #self.expanded_quad_searches[end] += 1
    206             candidate_vertices, branch = self.root.expand_search()
     194            candidate_vertices, branch = root.expand_search()
    207195            if branch == []:
    208196                # Searching all the verts from the root cell that haven't
    209197                # been searched.  This is the last try
    210198                element_found, sigma0, sigma1, sigma2, k = \
    211                       self._search_triangles_of_vertices(candidate_vertices, x)
     199                      self._search_triangles_of_vertices(mesh,
     200                                                         candidate_vertices, x)
    212201                is_more_elements = False
    213202            else:
    214203                element_found, sigma0, sigma1, sigma2, k = \
    215                       self._search_triangles_of_vertices(candidate_vertices, x)
     204                      self._search_triangles_of_vertices(mesh,
     205                                                         candidate_vertices, x)
    216206
    217207        return element_found, sigma0, sigma1, sigma2, k
    218208
    219     def _search_triangles_of_vertices(self,
    220                                  candidate_vertices, x):
    221             #Find triangle containing x:
    222             element_found = False
    223 
    224             # This will be returned if element_found = False
    225             sigma2 = -10.0
    226             sigma0 = -10.0
    227             sigma1 = -10.0
    228             k = -10.0
    229             #print "*$* candidate_vertices", candidate_vertices
    230             #For all vertices in same cell as point x
    231             for v in candidate_vertices:
    232                 #FIXME (DSG-DSG): this catches verts with no triangle.
    233                 #Currently pmesh is producing these.
    234                 #this should be stopped,
    235                 if self.mesh.vertexlist[v] is None:
    236                     continue
    237                 #for each triangle id (k) which has v as a vertex
    238                 for k, _ in self.mesh.vertexlist[v]:
    239                     #Get the three vertex_points of candidate triangle
    240                     xi0 = self.mesh.get_vertex_coordinate(k, 0)
    241                     xi1 = self.mesh.get_vertex_coordinate(k, 1)
    242                     xi2 = self.mesh.get_vertex_coordinate(k, 2)
    243 
    244                     #Get the three normals
    245                     n0 = self.mesh.get_normal(k, 0)
    246                     n1 = self.mesh.get_normal(k, 1)
    247                     n2 = self.mesh.get_normal(k, 2)
    248 
    249                     #Compute interpolation
    250                     sigma2 = dot((x-xi0), n2)/dot((xi2-xi0), n2)
    251                     sigma0 = dot((x-xi1), n0)/dot((xi0-xi1), n0)
    252                     sigma1 = dot((x-xi2), n1)/dot((xi1-xi2), n1)
    253 
    254                     #FIXME: Maybe move out to test or something
    255                     epsilon = 1.0e-6
    256                     assert abs(sigma0 + sigma1 + sigma2 - 1.0) < epsilon
    257 
    258                     #Check that this triangle contains the data point
    259 
    260                     #Sigmas can get negative within
    261                     #machine precision on some machines (e.g nautilus)
    262                     #Hence the small eps
    263                     eps = 1.0e-15
    264                     if sigma0 >= -eps and sigma1 >= -eps and sigma2 >= -eps:
    265                         element_found = True
    266                         break
    267 
    268                 if element_found is True:
    269                     #Don't look for any other triangle
     209    def _search_triangles_of_vertices(self, mesh, candidate_vertices, x):
     210        #Find triangle containing x:
     211        element_found = False
     212
     213        # This will be returned if element_found = False
     214        sigma2 = -10.0
     215        sigma0 = -10.0
     216        sigma1 = -10.0
     217        k = -10.0
     218        #print "*$* candidate_vertices", candidate_vertices
     219        #For all vertices in same cell as point x
     220        for v in candidate_vertices:
     221            #FIXME (DSG-DSG): this catches verts with no triangle.
     222            #Currently pmesh is producing these.
     223            #this should be stopped,
     224            if mesh.vertexlist[v] is None:
     225                continue
     226            #for each triangle id (k) which has v as a vertex
     227            for k, _ in mesh.vertexlist[v]:
     228                #Get the three vertex_points of candidate triangle
     229                xi0 = mesh.get_vertex_coordinate(k, 0)
     230                xi1 = mesh.get_vertex_coordinate(k, 1)
     231                xi2 = mesh.get_vertex_coordinate(k, 2)
     232
     233                #Get the three normals
     234                n0 = mesh.get_normal(k, 0)
     235                n1 = mesh.get_normal(k, 1)
     236                n2 = mesh.get_normal(k, 2)
     237
     238                #Compute interpolation
     239                sigma2 = dot((x-xi0), n2)/dot((xi2-xi0), n2)
     240                sigma0 = dot((x-xi1), n0)/dot((xi0-xi1), n0)
     241                sigma1 = dot((x-xi2), n1)/dot((xi1-xi2), n1)
     242                   
     243                #FIXME: Maybe move out to test or something
     244                epsilon = 1.0e-6
     245                assert abs(sigma0 + sigma1 + sigma2 - 1.0) < epsilon
     246
     247                #Check that this triangle contains the data point
     248               
     249                #Sigmas can get negative within
     250                #machine precision on some machines (e.g nautilus)
     251                #Hence the small eps
     252                eps = 1.0e-15
     253                if sigma0 >= -eps and sigma1 >= -eps and sigma2 >= -eps:
     254                    element_found = True
    270255                    break
    271             return element_found, sigma0, sigma1, sigma2, k
     256
     257            if element_found is True:
     258                #Don't look for any other triangle
     259                break
     260        return element_found, sigma0, sigma1, sigma2, k
    272261
    273262
  • inundation/fit_interpolate/test_interpolate.py

    r2189 r2190  
    235235
    236236
    237         results = interp._build_interpolation_matrix_A(data).todense()
    238         assert allclose(results, answer)
     237        #results = interp._build_interpolation_matrix_A(data).todense()
     238
     239        assert allclose(A, answer)
    239240
    240241
Note: See TracChangeset for help on using the changeset viewer.