Changeset 346
- Timestamp:
- Sep 28, 2004, 4:18:34 PM (21 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/least_squares.py
r342 r346 25 25 from LinearAlgebra import solve_linear_equations 26 26 27 ALPHA_INITIAL = 0.001 28 29 def fit_to_mesh_file(mesh_file, point_file, mesh_output_file, alpha= ALPHA_INITIAL): 30 """ 31 Given a mesh file (tsh) and a point attribute file (xya), fit 32 point attributes to the mesh and write a mesh file with the 33 results. 34 """ 35 from load_mesh.loadASCII import mesh_file_to_mesh_dictionary, \ 36 load_xya_file, export_trianglulation_file 37 # load in the .tsh file 38 mesh_dic = mesh_file_to_mesh_dictionary(mesh_file) 39 vertex_coordinates = mesh_dic['generatedpointlist'] 40 triangles = mesh_dic['generatedtrianglelist'] 41 42 # load in the .xya file 43 point_dict = load_xya_file(point_file) 44 point_coordinates = point_dict['pointlist'] 45 point_attributes = point_dict['pointattributelist'] 46 47 48 f = fit_to_mesh(vertex_coordinates, 49 triangles, 50 point_coordinates, 51 point_attributes, 52 alpha = alpha) 53 # convert array to list of lists 54 mesh_dic['generatedpointattributelist'] = f.tolist() 55 export_trianglulation_file(mesh_output_file, mesh_dic) 56 27 57 28 58 def fit_to_mesh(vertex_coordinates, … … 30 60 point_coordinates, 31 61 point_attributes, 32 alpha = 0.0):62 alpha = ALPHA_INITIAL): 33 63 """ 34 64 Fit a smooth surface to a trianglulation, … … 66 96 triangles, 67 97 point_coordinates = None, 68 alpha = 0.001):98 alpha = ALPHA_INITIAL): 69 99 70 100 … … 155 185 156 186 #FIXME: Maybe move out to test or something 157 epsilon = 1.0e- 13187 epsilon = 1.0e-6 158 188 assert abs(sigma0 + sigma1 + sigma2 - 1.0) < epsilon 159 189 … … 329 359 #------------------------------------------------------------- 330 360 if __name__ == "__main__": 331 361 """ 362 Load in a mesh and data points with attributes. 363 Fit the attributes to the mesh. 364 Save a new mesh file. 365 """ 332 366 import os, sys 333 from load_mesh.loadASCII import mesh_file_to_mesh_dictionary, \ 334 load_xya_file, export_trianglulation_file 335 usage = "usage: %s mesh_input.tsh point.xya mesh_output.tsh" % os.path.basename(sys.argv[0]) 367 usage = "usage: %s mesh_input.tsh point.xya mesh_output.tsh alpha" % os.path.basename(sys.argv[0]) 336 368 337 369 if len(sys.argv) < 4: … … 341 373 point_file = sys.argv[2] 342 374 mesh_output_file = sys.argv[3] 343 344 # load in the .tsh file 345 mesh_dic = mesh_file_to_mesh_dictionary(mesh_file) 346 vertex_coordinates = mesh_dic['generatedpointlist'] 347 triangles = mesh_dic['generatedtrianglelist'] 348 349 # load in the .xya file 350 point_dict = load_xya_file(point_file) 351 point_coordinates = point_dict['pointlist'] 352 point_attributes = point_dict['pointattributelist'] 353 354 355 f = fit_to_mesh(vertex_coordinates, 356 triangles, 357 point_coordinates, 358 point_attributes) 359 # convert array to list of lists 360 mesh_dic['generatedpointattributelist'] = f.tolist() 361 export_trianglulation_file(mesh_output_file, mesh_dic) 362 #FIXME do unit test 363 364 365 366 367 368 369 370 371 372 373 374 375 375 if len(sys.argv) > 4: 376 alpha = sys.argv[4] 377 else: 378 alpha = ALPHA_INITIAL 379 fit_to_mesh_file(mesh_file, point_file, mesh_output_file, alpha) 380 381 382 383 384 385 386 387 388 389 390 391 -
inundation/ga/storm_surge/pyvolution/load_mesh/loadASCII.py
r342 r346 42 42 #for fragment in fragments: 43 43 # print fragment 44 NumOfVertices = fragments[0] 45 NumOfVertAttributes = fragments[1] 44 if fragments ==[]: 45 NumOfVertices = 0 46 NumOfVertAttributes = 0 47 else: 48 NumOfVertices = fragments[0] 49 NumOfVertAttributes = fragments[1] 46 50 points = [] 47 51 pointattributes = [] … … 140 144 141 145 def read_mesh(fd): 146 """ 147 Note, if a file has no mesh info, it can still be read - the meshdic 148 returned will be 'empty'. 149 """ 142 150 delimiter = " " # warning: split() calls are using default whitespace 143 151 … … 147 155 fragments = line.split() 148 156 #for fragment in fragments: 149 # print fragment 150 NumOfVertices = fragments[0] 151 NumOfVertAttributes = fragments[1] 157 if fragments ==[]: 158 NumOfVertices = 0 159 NumOfVertAttributes = 0 160 else: 161 NumOfVertices = fragments[0] 162 NumOfVertAttributes = fragments[1] 152 163 points = [] 153 164 pointattributes = [] … … 173 184 #for fragment in fragments: 174 185 # print fragment 175 NumOfSegments = fragments[0] 186 if fragments ==[]: 187 NumOfSegments = 0 188 else: 189 NumOfSegments = fragments[0] 176 190 segments = [] 177 191 segmentmarkers = [] … … 199 213 #for fragment in fragments: 200 214 # print fragment 201 numOfHoles = fragments[0] 215 if fragments ==[]: 216 numOfHoles = 0 217 else: 218 numOfHoles = fragments[0] 202 219 holes = [] 203 220 for index in range(int(numOfHoles)): … … 216 233 #for fragment in fragments: 217 234 # print fragment 218 numOfRegions = fragments[0] 235 if fragments ==[]: 236 numOfRegions = 0 237 else: 238 numOfRegions = fragments[0] 219 239 regions = [] 220 240 regionattributes = [] … … 321 341 else: 322 342 neighbors += "-1 " 323 343 if triangles_attributes[index] == ['']: 344 att = "" 345 else: 346 att = str(triangles_attributes[index]) 324 347 fd.write(str(index) + " " 325 348 + str(tri[0]) + " " … … 327 350 + str(tri[2]) + " " 328 351 + neighbors + " " 329 + str(triangles_attributes[index])+ "\n")352 + att + "\n") 330 353 331 354 #One line: <# of segments> -
inundation/ga/storm_surge/pyvolution/test_least_squares.py
r333 r346 489 489 z1 = interp.interpolate(f) 490 490 assert allclose(z, z1) 491 492 def test_fit_to_mesh_file(self): 493 from load_mesh.loadASCII import mesh_file_to_mesh_dictionary, \ 494 export_trianglulation_file 495 import tempfile 496 import os 497 498 # create a .tsh file, no user outline 499 mesh_dic = {} 500 mesh_dic['generatedpointlist'] = [[0.0, 0.0], 501 [0.0, 5.0], 502 [5.0, 0.0]] 503 mesh_dic['generatedtrianglelist'] = [[0, 2, 1]] 504 mesh_dic['generatedsegmentlist'] = [[0, 1], [2, 0], [1, 2]] 505 mesh_dic['generatedtriangleattributelist'] = [['']] 506 mesh_dic['generatedpointattributelist'] = [[], [], []] 507 mesh_dic['generatedtriangleneighborlist'] = [[-1, -1, -1]] 508 mesh_dic['generatedsegmentmarkerlist'] = ['external', 509 'external', 510 'external'] 511 mesh_file = tempfile.mktemp(".tsh") 512 export_trianglulation_file(mesh_file,mesh_dic) 513 514 # create an .xya file 515 point_file = tempfile.mktemp(".xya") 516 fd = open(point_file,'w') 517 fd.write("# demo \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n") 518 fd.close() 519 520 mesh_output_file = "new_trianlge.tsh" 521 fit_to_mesh_file(mesh_file, 522 point_file, 523 mesh_output_file, 524 alpha = 0.0) 525 # load in the .tsh file we jusdt wrote 526 mesh_dic = mesh_file_to_mesh_dictionary(mesh_output_file) 527 528 assert allclose(mesh_dic['generatedpointattributelist'], 529 [[0.0, 0.0], [5.0, 10.0], [5.0,10.0]]) 530 531 #clean up 532 os.remove(mesh_file) 533 os.remove(point_file) 491 534 492 535 #-------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.