Changeset 346


Ignore:
Timestamp:
Sep 28, 2004, 4:18:34 PM (21 years ago)
Author:
duncan
Message:

added test for fit_to_mesh_file, removed bugs from loadASCII.

Location:
inundation/ga/storm_surge/pyvolution
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/least_squares.py

    r342 r346  
    2525from LinearAlgebra import solve_linear_equations
    2626
     27ALPHA_INITIAL = 0.001
     28
     29def 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       
    2757
    2858def fit_to_mesh(vertex_coordinates,
     
    3060                              point_coordinates,
    3161                              point_attributes,
    32                               alpha = 0.0):
     62                              alpha = ALPHA_INITIAL):
    3363    """
    3464    Fit a smooth surface to a trianglulation,
     
    6696                 triangles,
    6797                 point_coordinates = None,
    68                  alpha = 0.001):
     98                 alpha = ALPHA_INITIAL):
    6999
    70100       
     
    155185
    156186                #FIXME: Maybe move out to test or something
    157                 epsilon = 1.0e-13
     187                epsilon = 1.0e-6
    158188                assert abs(sigma0 + sigma1 + sigma2 - 1.0) < epsilon
    159189
     
    329359#-------------------------------------------------------------
    330360if __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    """
    332366    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])
    336368
    337369    if len(sys.argv) < 4:
     
    341373        point_file = sys.argv[2]
    342374        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  
    4242    #for fragment in fragments:
    4343    #    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]
    4650    points = []
    4751    pointattributes = []
     
    140144
    141145def 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    """
    142150    delimiter = " " # warning: split() calls are using default whitespace
    143151   
     
    147155    fragments = line.split()
    148156    #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]
    152163    points = []
    153164    pointattributes = []
     
    173184    #for fragment in fragments:
    174185    #    print fragment
    175     NumOfSegments = fragments[0]
     186    if fragments ==[]:
     187        NumOfSegments = 0
     188    else:
     189        NumOfSegments = fragments[0]
    176190    segments = []
    177191    segmentmarkers = []
     
    199213    #for fragment in fragments:
    200214    #    print fragment
    201     numOfHoles = fragments[0]
     215    if fragments ==[]:
     216        numOfHoles = 0
     217    else:
     218        numOfHoles = fragments[0]
    202219    holes = []
    203220    for index in range(int(numOfHoles)):       
     
    216233    #for fragment in fragments:
    217234    #    print fragment
    218     numOfRegions = fragments[0]
     235    if fragments ==[]:
     236        numOfRegions = 0
     237    else:
     238        numOfRegions = fragments[0]
    219239    regions = []
    220240    regionattributes = []
     
    321341                else:
    322342                    neighbors +=  "-1 "
    323                
     343        if triangles_attributes[index] == ['']:
     344            att = ""
     345        else:
     346            att = str(triangles_attributes[index])
    324347        fd.write(str(index) + " "
    325348                 + str(tri[0]) + " "
     
    327350                 + str(tri[2]) + " "
    328351                 + neighbors + " "
    329                  + str(triangles_attributes[index]) + "\n")
     352                 + att + "\n")
    330353           
    331354    #One line:  <# of segments>
  • inundation/ga/storm_surge/pyvolution/test_least_squares.py

    r333 r346  
    489489        z1 = interp.interpolate(f)
    490490        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)
    491534       
    492535#-------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.