Changeset 4593
- Timestamp:
- Jul 5, 2007, 12:43:43 PM (17 years ago)
- 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 262 262 263 263 264 def get_vertex_coordinates(self, absolute=False): 264 def get_vertex_coordinates(self, 265 triangle_id=None, 266 absolute=False): 265 267 """Return vertex coordinates for all triangles. 266 268 … … 269 271 M the number of triangles in the mesh. 270 272 273 if triangle_id is specified (an integer) the 3 vertex coordinates 274 for triangle_id are returned. 275 271 276 Boolean keyword argument absolute determines whether coordinates 272 277 are to be made absolute by taking georeference into account … … 278 283 if not self.geo_reference.is_absolute(): 279 284 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 280 293 281 return V 294 i3 = 3*i 295 return array([V[i3,:], V[i3+1,:], V[i3+2,:]]) 282 296 283 297 … … 288 302 """ 289 303 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 292 311 293 312 -
anuga_core/source/anuga/abstract_2d_finite_volumes/test_general_mesh.py
r4478 r4593 41 41 k = triangles[i,j] #Index of vertex j in triangle i 42 42 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]) 43 69 44 70 … … 197 223 #------------------------------------------------------------- 198 224 if __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') 200 227 runner = unittest.TextTestRunner() 201 228 runner.run(suite) -
anuga_core/source/anuga/fit_interpolate/search_functions.py
r4590 r4593 13 13 """ 14 14 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 15 20 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 19 29 """ 20 30 #Find triangle containing x: … … 55 65 This is called by search_tree_of_vertices once the appropriate node 56 66 has been found from the quad tree. 67 57 68 58 69 This function is responsible for most of the compute time in … … 67 78 sigma0 = -10.0 68 79 sigma1 = -10.0 69 k = -10 .070 #print "*$* candidate_vertices", candidate_vertices80 k = -10 81 71 82 #For all vertices in same cell as point x 72 83 for v in candidate_vertices: … … 79 90 80 91 #for each triangle id (k) which has v as a vertex 81 82 92 vertexlist = mesh.get_triangles_and_vertices_per_node(node=v) 83 93 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) 88 96 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 93 102 94 103 #Compute interpolation … … 97 106 sigma1 = dot((x-xi2), n1)/dot((xi1-xi2), n1) 98 107 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 102 110 epsilon = 1.0e-6 103 111 msg = 'abs(sigma0+sigma1+sigma2-1) = %.15e, eps = %.15e'\ … … 105 113 assert abs(sigma0 + sigma1 + sigma2 - 1.0) < epsilon, msg 106 114 115 107 116 #Check that this triangle contains the data point 108 117 109 118 # 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) 111 120 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: 113 122 element_found = True 114 123 break 115 124 116 125 if element_found is True: 117 # Don't look for any other triangle126 # Don't look for any other triangle 118 127 break 128 119 129 return element_found, sigma0, sigma1, sigma2, k 120 130 -
anuga_core/source/anuga/fit_interpolate/test_search_functions.py
r4590 r4593 3 3 4 4 import unittest 5 from search_functions import * 5 from search_functions import search_tree_of_vertices 6 from search_functions import _search_triangles_of_vertices 7 6 8 7 9 from Numeric import zeros, array, allclose … … 9 11 from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh 10 12 from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular 11 13 from anuga.utilities.polygon import is_inside_polygon 12 14 from anuga.utilities.quad import build_quadtree 13 15 … … 49 51 mesh.check_integrity() 50 52 51 #print mesh.nodes52 #print mesh.triangles53 53 root = build_quadtree(mesh, max_points_per_cell = 1) 54 #print 'root', root.show()55 54 56 x = [0. 7, 0.7]55 x = [0.2, 0.7] 57 56 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 60 58 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 61 168 62 169
Note: See TracChangeset
for help on using the changeset viewer.