Changeset 4859
- Timestamp:
- Nov 28, 2007, 11:42:09 AM (17 years ago)
- Location:
- anuga_core/source/anuga
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/fit_interpolate/benchmark_least_squares.py
r4839 r4859 22 22 import tempfile 23 23 import profile , pstats 24 from math import sqrt 25 from Numeric import array 26 27 from anuga.fit_interpolate.search_functions import search_times, \ 28 reset_search_times 24 29 25 30 from anuga.fit_interpolate.interpolate import Interpolate … … 31 36 from anuga.fit_interpolate.interpolate import benchmark_interpolate 32 37 from anuga.coordinate_transforms.geo_reference import Geo_reference 38 from anuga.fit_interpolate.general_fit_interpolate import \ 39 get_build_quadtree_time 40 41 """ 42 43 Code from the web; 44 45 from ctypes import * 46 from ctypes.wintypes import DWORD 47 48 SIZE_T = c_ulong 49 50 class _MEMORYSTATUS(Structure): 51 _fields_ = [("dwLength", DWORD), 52 ("dwMemoryLength", DWORD), 53 ("dwTotalPhys", SIZE_T), 54 ("dwAvailPhys", SIZE_T), 55 ("dwTotalPageFile", SIZE_T), 56 ("dwAvailPageFile", SIZE_T), 57 ("dwTotalVirtual", SIZE_T), 58 ("dwAvailVirtualPhys", SIZE_T)] 59 def show(self): 60 for field_name, field_type in self._fields_: 61 print field_name, getattr(self, field_name) 62 63 memstatus = _MEMORYSTATUS() 64 windll.kernel32.GlobalMemoryStatus(byref(memstatus )) 65 memstatus.show() 66 67 68 _______________________________ 69 70 from ctypes import * 71 from ctypes.wintypes import * 72 73 class MEMORYSTATUS(Structure): 74 _fields_ = [ 75 ('dwLength', DWORD), 76 ('dwMemoryLoad', DWORD), 77 ('dwTotalPhys', DWORD), 78 ('dwAvailPhys', DWORD), 79 ('dwTotalPageFile', DWORD), 80 ('dwAvailPageFile', DWORD), 81 ('dwTotalVirtual', DWORD), 82 ('dwAvailVirtual', DWORD), 83 ] 84 85 def winmem(): 86 x = MEMORYSTATUS() 87 windll.kernel32.GlobalMemoryStatus(byref(x)) 88 return x 89 90 """ 33 91 34 92 def mem_usage(): … … 76 134 save=False, 77 135 verbose=False, 78 run_profile=False): 136 run_profile=False, 137 gridded=True): 79 138 ''' 80 139 num_of_points … … 84 143 #print "max_points_per_cell", max_points_per_cell 85 144 86 geo = Geo_reference(xllcorner = 2.0, 87 yllcorner = 2.0) 145 geo = None #Geo_reference(xllcorner = 2.0, yllcorner = 2.0) 88 146 mesh_dict = self._build_regular_mesh_dict(maxArea=maxArea, 89 147 is_segments=segments_in_mesh, … … 91 149 geo=geo) 92 150 points_dict = self._build_points_dict(num_of_points=num_of_points, 93 geo=geo) 94 95 151 gridded=gridded, 152 verbose=verbose) 153 154 #print "len(mesh_dict['triangles'])",len(mesh_dict['triangles']) 96 155 if is_fit is True: 97 156 op = "Fit_" … … 107 166 108 167 domain = Domain(mesh_dict['vertices'], mesh_dict['triangles'], 109 use_cache=False, verbose= False,168 use_cache=False, verbose=verbose, 110 169 geo_reference=geo) 111 170 #Initial time and memory … … 119 178 points_dict['point_attributes'], 120 179 geo_reference=geo) 180 del points_dict 121 181 if is_fit is True: 122 182 … … 152 212 else: 153 213 domain.set_quantity('elevation',points,filename=filename, 154 use_cache=False )214 use_cache=False, verbose=verbose) 155 215 if not use_file_type == None: 156 216 os.remove(fileName) … … 189 249 geospatial, 190 250 mesh_origin=geo, 191 max_points_per_cell=max_points_per_cell) 251 max_points_per_cell=max_points_per_cell, 252 verbose=verbose) 192 253 time_taken_sec = (time.time()-t0) 193 254 m1 = mem_usage() … … 197 258 memory_used = (m1 - m0) 198 259 #print 'That took %.2f seconds' %time_taken_sec 199 return time_taken_sec, memory_used, len(mesh_dict['triangles']) 260 261 # return the times spent in first cell searching and 262 # backing up. 263 264 search_one_cell_time, search_more_cells_time = search_times() 265 reset_search_times() 266 #print "bench - search_one_cell_time",search_one_cell_time 267 #print "bench - search_more_cells_time", search_more_cells_time 268 #print "bench - build_quadtree_time", get_build_quadtree_time() 269 return time_taken_sec, memory_used, len(mesh_dict['triangles']), \ 270 search_one_cell_time, search_more_cells_time, \ 271 get_build_quadtree_time() 272 200 273 201 274 def _build_regular_mesh_dict(self, … … 207 280 # pretty regular size, with some segments thrown in. 208 281 209 #x_min = 210 m = Mesh() 282 # don't pass in the geo ref. 283 # it is applied in domain 284 m = Mesh() #geo_reference=geo) 211 285 m.addUserVertex(0,0) 212 286 m.addUserVertex(1.0,0) … … 254 328 return mesh_dict 255 329 256 def _build_points_dict(self, num_of_points=20000 ,257 geo=None):330 def _build_points_dict(self, num_of_points=20000 331 , gridded=True, verbose=False): 258 332 259 333 points_dict = {} … … 261 335 point_atts = [] 262 336 337 if gridded is True: 338 grid = int(sqrt(num_of_points)) 339 263 340 for point in range(num_of_points): 264 points.append([random(), random()]) 341 if gridded is True: 342 343 # point starts at 0.0 344 # the 2 and 0.25 is to make sure all points are in the 345 # range 0 - 1 346 points.append([float(point/grid)/float(grid*1.1)+0.0454, 347 float(point%grid)/float(grid*1.1)+0.0454]) 348 else: 349 points.append([random(), random()]) 265 350 point_atts.append(10.0) 266 351 267 352 points_dict['points'] = points 268 353 points_dict['point_attributes'] = point_atts 354 355 for point in points: 356 assert point[0] < 1.0 357 assert point[1] < 1.0 358 assert point[0] > 0.0 359 assert point[1] > 0.0 360 361 if verbose is True: 362 pass 363 #print "points", points 364 #import sys; sys.exit() 365 366 367 269 368 return points_dict 270 369 … … 272 371 #------------------------------------------------------------- 273 372 if __name__ == "__main__": 274 b = BenchmarkLeastSquares()275 b._build_regular_mesh_dict()276 373 b._build_points_dict() -
anuga_core/source/anuga/fit_interpolate/fit.py
r4838 r4859 37 37 from anuga.utilities.polygon import in_and_outside_polygon 38 38 from anuga.fit_interpolate.search_functions import search_tree_of_vertices 39 39 40 from anuga.utilities.cg_solve import conjugate_gradient 40 41 from anuga.utilities.numerical_tools import ensure_numeric, gradient … … 91 92 """ 92 93 # Initialise variabels 93 94 94 if alpha is None: 95 95 … … 255 255 assert z.shape[0] == point_coordinates.shape[0] 256 256 257 self.AtA = Sparse(m,m)257 AtA = Sparse(m,m) 258 258 # The memory damage has been done by now. 259 259 else: 260 AtA = self.AtA #Did this for speed, did ~nothing 260 261 self.point_count += point_coordinates.shape[0] 261 262 #print "_build_matrix_AtA_Atz - self.point_count", self.point_count … … 276 277 if verbose: print 'Building fitting matrix from %d points' %n 277 278 #Compute matrix elements for points inside the mesh 279 triangles = self.mesh.triangles #Did this for speed, did ~nothing 278 280 for d, i in enumerate(inside_poly_indices): 279 281 #For each data_coordinate point … … 284 286 285 287 if element_found is True: 286 j0 = self.mesh.triangles[k,0] #Global vertex id for sigma0287 j1 = self.mesh.triangles[k,1] #Global vertex id for sigma1288 j2 = self.mesh.triangles[k,2] #Global vertex id for sigma2288 j0 = triangles[k,0] #Global vertex id for sigma0 289 j1 = triangles[k,1] #Global vertex id for sigma1 290 j2 = triangles[k,2] #Global vertex id for sigma2 289 291 290 292 sigmas = {j0:sigma0, j1:sigma1, j2:sigma2} … … 300 302 301 303 for k in js: 302 self.AtA[j,k] += sigmas[j]*sigmas[k]304 AtA[j,k] += sigmas[j]*sigmas[k] 303 305 else: 304 306 msg = 'Could not find triangle for point', x 305 307 raise Exception(msg) 306 308 self.AtA = AtA 307 309 308 310 def fit(self, point_coordinates_or_filename=None, z=None, … … 324 326 325 327 """ 326 327 328 # use blocking to load in the point info 328 329 if type(point_coordinates_or_filename) == types.StringType: … … 337 338 338 339 for i, geo_block in enumerate(G_data): 339 if verbose is True and 0 == i%200: # round every 5 minutes340 # But this is dependant on the # of Triangles, so it341 # isn't every 5 minutes.340 if verbose is True and 0 == i%200: 341 # The time this will take 342 # is dependant on the # of Triangles 342 343 343 344 print 'Processing Block %d' %i … … 353 354 354 355 points = geo_block.get_data_points(absolute=True) 356 #print "fit points", points 355 357 z = geo_block.get_attributes(attribute_name=attribute_name) 356 358 self.build_fit_subset(points, z, verbose=verbose) -
anuga_core/source/anuga/fit_interpolate/general_fit_interpolate.py
r4779 r4859 36 36 from anuga.geospatial_data.geospatial_data import Geospatial_data, \ 37 37 ensure_absolute 38 from anuga.fit_interpolate.search_functions import set_last_triangle 38 39 39 40 # tests fail if 2 is used 40 41 MAX_VERTICES_PER_CELL = 13 # A value of 8 or lower can cause problems, 41 42 # if a vert has 9 triangles. 43 44 build_quadtree_time = 0 45 46 def get_build_quadtree_time(): 47 return build_quadtree_time 42 48 43 49 class FitInterpolate: … … 82 88 a mesh origin, since geospatial has its own mesh origin. 83 89 """ 84 90 global build_quadtree_time 85 91 if max_vertices_per_cell == None: 86 92 max_vertices_per_cell = MAX_VERTICES_PER_CELL 87 88 93 if mesh is None: 89 94 # Fixme (DSG) Throw errors if triangles or vertex_coordinates … … 103 108 if verbose: print 'FitInterpolate: Building quad tree' 104 109 # This stores indices of vertices 110 t0 = time.time() 111 #print "self.mesh.get_extent(absolute=True)", \ 112 #self.mesh.get_extent(absolute=True) 105 113 self.root = build_quadtree(self.mesh, 106 114 max_points_per_cell = max_vertices_per_cell) 107 #print "self.root",self.root.show() 115 #print "self.root",self.root.show() 108 116 117 build_quadtree_time = time.time()-t0 118 set_last_triangle() 109 119 110 120 def __repr__(self): -
anuga_core/source/anuga/fit_interpolate/profile_long_benchmark.py
r4839 r4859 12 12 ben = BenchmarkLeastSquares() 13 13 14 ofile = 'lbm_results.csv'15 14 delimiter = ',' 16 15 17 16 use_least_squares_list = [False] 18 is_fit_list = [True] 19 num_of_points_list = [1000] #, 500, 10000, 100000] #, 10000000] 20 maxArea_list = [0.0001] #,0.00001] #, 0.0000001] #, 0.06, 0.00001, 0.0000001] 21 max_points_per_cell_list = [8] 17 is_fit_list = [True, False] 18 # 45 is for the interp example 19 # 4617 is 3 points per triangle 20 #30780 is 20 points per triangle 21 # 92340 is 60 points per triangle 22 num_of_points_list = [92340] #[45,4617,30780,92340,] #, 500, 10000, 100000] #, 10000000] 23 maxArea_list = [0.001] #,0.00001] #, 0.0000001] #, 0.06, 0.00001, 0.0000001] 24 max_points_per_cell_list = [13] 22 25 use_file_type_list = ['pts'] 26 run_profile = True 23 27 28 29 if run_profile is True: 30 ofile = 'profiling_lbm_results.csv' 31 else: 32 ofile = 'lbm_results.csv' 24 33 fd = open(ofile,'a') 25 34 # write the title line … … 31 40 "max_points_per_cell" + delimiter + 32 41 "is_fit" + delimiter + 42 "search_one_cell_time" + delimiter + 43 "search_more_cells_time" + delimiter + 44 "build_quadtree_time" + delimiter + 33 45 "mem" + delimiter + 34 46 "time" + delimiter + "\n") … … 41 53 for max_points_per_cell in max_points_per_cell_list: 42 54 43 time, mem, num_tri = ben.trial(55 time, mem, num_tri, one_t, more_t, quad_t = ben.trial( 44 56 num_of_points=num_of_points 45 57 ,maxArea=maxArea … … 49 61 ,use_file_type=use_file_type 50 62 ,save=True 51 ,run_profile= True63 ,run_profile=run_profile 52 64 ) 53 65 print "time",time … … 60 72 str(max_points_per_cell) + delimiter + 61 73 str(is_fit) + delimiter + 74 str(one_t) + delimiter + 75 str(more_t) + delimiter + 76 str(quad_t) + delimiter + 62 77 str(mem) + delimiter + 63 78 str(time) + delimiter + "\n") -
anuga_core/source/anuga/fit_interpolate/run_long_benchmark.py
r4839 r4859 12 12 ben = BenchmarkLeastSquares() 13 13 14 ofile = 'lbm_results.csv'15 14 delimiter = ',' 16 15 17 16 use_least_squares_list = [False] 18 is_fit_list = [True, False] 19 num_of_points_list = [50, 1000] #, 500, 10000, 100000] #, 10000000] 20 maxArea_list = [0.01, 0.001, 0.0001] #,0.00001] #, 0.0000001] #, 0.06, 0.00001, 0.0000001] 21 max_points_per_cell_list = [8] 17 is_fit_list = [True] #[True, False] 18 19 # a maxArea of 0.00001 gives 155195 triangles 20 # Simulating Cairns. Seemed to take too long to run. Need to Try again. 21 22 #maxArea_list = [0.00001] 23 #num_of_points_list = [1863558] 24 25 # a maxArea of 0.0001 gives 15568 triangles 26 #maxArea_list = [0.0001] 27 #num_of_points_list = [46704] 28 29 # a maxArea of 0.001 gives 1539 triangles 30 # 3 points/tri is 4617 31 # 20 points/tri is 30780 32 # 132 points/tri is 203148 33 #maxArea_list = [0.001] 34 #num_of_points_list = [4617,30780,203148] 35 #num_of_points_list = [203148] 36 37 # a maxArea of 0.005 gives 319 triangles 38 # 3 points/tri is 957 39 # 20 points/tri is 6380 40 # 132 points/tri is 42108 41 #maxArea_list = [0.005] 42 #num_of_points_list = [957,6380,42108] 43 44 # a maxArea of 0.01 gives 150 triangles 45 # 3 points/tri is 450 46 # 20 points/tri is 3000 47 # 132 points/tri is 19800 48 #maxArea_list = [0.01] 49 #num_of_points_list = [450,3000] #,19800] 50 51 # Quick check 52 maxArea_list = [0.61] 53 num_of_points_list = [4] 54 55 56 57 58 max_points_per_cell_list = [13] 22 59 use_file_type_list = ['pts'] 60 run_profile = False # True #True # False # 61 gridded_list = [True] #, False] 23 62 63 64 if run_profile is True: 65 ofile = 'profiling_lbm_results.csv' 66 else: 67 ofile = 'lbm_results.csv' 24 68 fd = open(ofile,'a') 25 69 # write the title line … … 31 75 "max_points_per_cell" + delimiter + 32 76 "is_fit" + delimiter + 77 "is_gridded" + delimiter + 78 "search_one_cell_time" + delimiter + 79 "search_more_cells_time" + delimiter + 80 "build_quadtree_time" + delimiter + 33 81 "mem" + delimiter + 34 82 "time" + delimiter + "\n") … … 36 84 37 85 for is_fit in is_fit_list: 38 for maxArea in maxArea_list: 39 for use_file_type in use_file_type_list: 40 for num_of_points in num_of_points_list: 41 for max_points_per_cell in max_points_per_cell_list: 86 for gridded in gridded_list: 87 for maxArea in maxArea_list: 88 for use_file_type in use_file_type_list: 89 for num_of_points in num_of_points_list: 90 for max_points_per_cell in max_points_per_cell_list: 42 91 43 time, mem, num_tri = ben.trial( 44 num_of_points=num_of_points 45 ,maxArea=maxArea 46 ,max_points_per_cell=max_points_per_cell 47 ,is_fit=is_fit 48 ,segments_in_mesh=False 49 ,use_file_type=use_file_type 50 ,save=True 92 time, mem, num_tri, one_t, more_t, quad_t = ben.trial( 93 num_of_points=num_of_points 94 ,maxArea=maxArea 95 ,max_points_per_cell=max_points_per_cell 96 ,is_fit=is_fit 97 ,segments_in_mesh=False 98 ,use_file_type=use_file_type 99 ,save=True 100 ,verbose=False 101 ,run_profile=run_profile 102 ,gridded=gridded 51 103 ) 52 104 print "time",time … … 59 111 str(max_points_per_cell) + delimiter + 60 112 str(is_fit) + delimiter + 113 str(gridded) + delimiter + 114 str(one_t) + delimiter + 115 str(more_t) + delimiter + 116 str(quad_t) + delimiter + 61 117 str(mem) + delimiter + 62 118 str(time) + delimiter + "\n") -
anuga_core/source/anuga/fit_interpolate/search_functions.py
r4791 r4859 7 7 """ 8 8 from Numeric import dot 9 import time 10 11 from Numeric import array 9 12 10 13 from anuga.utilities.numerical_tools import get_machine_precision 14 from anuga.config import max_float 15 16 initial_search_value = 'uncomment search_functions code first'#0 17 search_one_cell_time = initial_search_value 18 search_more_cells_time = initial_search_value 19 20 #FIXME test what happens if a 21 LAST_TRIANGLE = [[-10,[(array([max_float,max_float]), 22 array([max_float,max_float]), 23 array([max_float,max_float])), 24 (array([1,1]),array([0,0]),array([-1.1,-1.1]))]]] 11 25 12 26 def search_tree_of_vertices(root, mesh, x): … … 28 42 29 43 """ 44 global search_one_cell_time 45 global search_more_cells_time 46 30 47 #Find triangle containing x: 31 48 element_found = False … … 36 53 sigma1 = -10.0 37 54 k = -10.0 38 55 56 # Search the last triangle first 57 element_found, sigma0, sigma1, sigma2, k = \ 58 _search_triangles_of_vertices(mesh, 59 last_triangle, x) 60 #print "last_triangle", last_triangle 61 if element_found is True: 62 #print "last_triangle", last_triangle 63 return element_found, sigma0, sigma1, sigma2, k 64 65 66 67 #t0 = time.time() 39 68 # Get triangles in the cell that the point is in. 40 69 # Triangle is a list, first element triangle_id, … … 42 71 triangles = root.search(x[0], x[1]) 43 72 is_more_elements = True 44 73 45 74 element_found, sigma0, sigma1, sigma2, k = \ 46 75 _search_triangles_of_vertices(mesh, 47 76 triangles, x) 77 #search_one_cell_time += time.time()-t0 78 #print "search_one_cell_time",search_one_cell_time 79 #t0 = time.time() 48 80 while not element_found and is_more_elements: 49 81 triangles, branch = root.expand_search() … … 57 89 element_found, sigma0, sigma1, sigma2, k = \ 58 90 _search_triangles_of_vertices(mesh,triangles, x) 59 91 #search_more_cells_time += time.time()-t0 92 #print "search_more_cells_time", search_more_cells_time 93 60 94 return element_found, sigma0, sigma1, sigma2, k 61 95 … … 71 105 fit and interpolate. 72 106 """ 73 107 global last_triangle 108 74 109 # these statments are needed if triangles is empty 75 110 #Find triangle containing x: … … 93 128 if element_found is True: 94 129 # Don't look for any other triangles in the triangle list 130 last_triangle = [[k,tri_verts_norms]] 95 131 break 96 132 return element_found, sigma0, sigma1, sigma2, k … … 100 136 def find_triangle_compute_interpolation(triangle, n0, n1, n2, x): 101 137 """Compute linear interpolation of point x and triangle k in mesh. 102 It is assumed that x belongs to triangle k. 138 It is assumed that x belongs to triangle k.max_float 103 139 """ 104 140 … … 113 149 114 150 if x[0] > max(xi0[0], xi1[0], xi2[0]) + epsilon: 151 # print "max(xi0[0], xi1[0], xi2[0])", max(xi0[0], xi1[0], xi2[0]) 115 152 return False,0,0,0 116 153 if x[0] < min(xi0[0], xi1[0], xi2[0]) - epsilon: … … 125 162 126 163 # Compute interpolation - return as soon as possible 164 # print "(xi0-xi1)", (xi0-xi1) 165 # print "n0", n0 166 # print "dot((xi0-xi1), n0)", dot((xi0-xi1), n0) 127 167 128 168 sigma0 = dot((x-xi1), n0)/dot((xi0-xi1), n0) … … 152 192 # element_found = False 153 193 return True, sigma0, sigma1, sigma2 194 195 def set_last_triangle(): 196 global last_triangle 197 last_triangle = LAST_TRIANGLE 198 199 def search_times(): 200 201 global search_one_cell_time 202 global search_more_cells_time 203 204 return search_one_cell_time, search_more_cells_time 205 206 def reset_search_times(): 207 208 global search_one_cell_time 209 global search_more_cells_time 210 search_one_cell_time = initial_search_value 211 search_more_cells_time = initial_search_value -
anuga_core/source/anuga/fit_interpolate/test_fit.py
r4838 r4859 131 131 132 132 def test_smooth_attributes_to_meshIV(self): 133 """Testing 2 attributes smoothed to the mesh134 """133 # Testing 2 attributes smoothed to the mesh 134 135 135 136 136 a = [0.0, 0.0] … … 708 708 709 709 def test_smooth_attributes_to_mesh_function(self): 710 """Testing 2 attributes smoothed to the mesh711 """710 #Testing 2 attributes smoothed to the mesh 711 712 712 713 713 a = [0.0, 0.0] … … 1096 1096 if __name__ == "__main__": 1097 1097 suite = unittest.makeSuite(Test_Fit,'test') 1098 #suite = unittest.makeSuite(Test_Fit,'test_ fit_to_mesh_pts_passing_mesh_in')1098 #suite = unittest.makeSuite(Test_Fit,'test_smooth_attributes_to_mesh_function') 1099 1099 #suite = unittest.makeSuite(Test_Fit,'') 1100 1100 runner = unittest.TextTestRunner(verbosity=1) -
anuga_core/source/anuga/fit_interpolate/test_search_functions.py
r4666 r4859 3 3 4 4 import unittest 5 from search_functions import search_tree_of_vertices 5 from search_functions import search_tree_of_vertices, set_last_triangle 6 6 from search_functions import _search_triangles_of_vertices 7 7 … … 49 49 50 50 root = build_quadtree(mesh, max_points_per_cell = 1) 51 set_last_triangle() 51 52 52 53 x = [0.2, 0.7] … … 66 67 67 68 root = build_quadtree(mesh, max_points_per_cell = 4) 69 set_last_triangle() 68 70 69 71 for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7], … … 96 98 for m in range(8): 97 99 root = build_quadtree(mesh, max_points_per_cell = m) 100 set_last_triangle() 98 101 #print m, root.show() 99 102 … … 121 124 122 125 root = build_quadtree(mesh, max_points_per_cell = 4) 126 set_last_triangle() 123 127 124 128 # One point -
anuga_core/source/anuga/utilities/sparse.py
r4769 r4859 57 57 58 58 i,j = key 59 # removing these asserts will not speed things up 59 60 assert 0 <= i < self.M 60 61 assert 0 <= j < self.N … … 69 70 70 71 i,j = key 72 # removing these asserts will not speed things up 71 73 assert 0 <= i < self.M 72 74 assert 0 <= j < self.N
Note: See TracChangeset
for help on using the changeset viewer.