Changeset 1657


Ignore:
Timestamp:
Jul 29, 2005, 3:33:32 PM (20 years ago)
Author:
ole
Message:

Comments and work on benchmark 2 (lwru2.py)
New unit test of least squares populating domain

Location:
inundation/ga/storm_surge
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pmesh/load_mesh/loadASCII.py

    r1467 r1657  
    997997            msg = 'Could not open file %s ' %ofile
    998998            raise IOError, msg
    999 
    1000 
    1001 
    1002 
    1003 
    1004 
    1005 
    1006 
    1007999        except IOError:
    10081000            # Catch this to add an error message
  • inundation/ga/storm_surge/pyvolution/data_manager.py

    r1654 r1657  
    1 """Functions to store and retrieve data for the Shallow Water Wave equation.
    2 There are two kinds of data
    3 
    4   1: Constant data: Vertex coordinates and field values. Stored once
    5   2: Variable data: Conserved quantities. Stored once per timestep.
    6 
    7 All data is assumed to reside at vertex locations.
    8 
    9 
    10 Formats used within ANUGA:
     1"""datamanager.py - input output for AnuGA
     2
     3
     4This module takes care of reading and writing datafiles such as topograhies,
     5model output, etc
     6
     7
     8Formats used within AnuGA:
    119
    1210.sww: Netcdf format for storing model output
     
    209207#Class for storing output to e.g. visualisation
    210208class Data_format_sww(Data_format):
    211     """Interface to native NetCDF format (.sww)
     209    """Interface to native NetCDF format (.sww) for storing model output
     210
     211    There are two kinds of data
     212
     213    1: Constant data: Vertex coordinates and field values. Stored once
     214    2: Variable data: Conserved quantities. Stored once per timestep.
     215
     216    All data is assumed to reside at vertex locations.
    212217    """
    213218
  • inundation/ga/storm_surge/pyvolution/general_mesh.py

    r1632 r1657  
    180180        if obj is True, the x/y pairs are returned in a 3*N x 2 array.
    181181        FIXME, we might make that the default.
     182        FIXME Maybe use keyword: continuous?
    182183
    183184       
  • inundation/ga/storm_surge/pyvolution/generic_boundary_conditions.py

    r1102 r1657  
    125125    """
    126126
     127    #FIXME: Is this still necessary
    127128
    128129    def __init__(self, filename, domain):
     
    170171
    171172    The full solution history is not exactly the same as if
    172     the models were couple:
     173    the models were coupled:
    173174    File boundary must read and interpolate from *smoothed* version
    174175    as stored in sww and cannot work with the discontinuos triangles.
  • inundation/ga/storm_surge/pyvolution/quantity.py

    r1632 r1657  
    410410            assert A.shape[0] == len(indexes)
    411411            vertex_list = indexes
     412           
    412413        #Go through list of unique vertices
    413         for i_index,unique_vert_id in enumerate(vertex_list):
     414        for i_index, unique_vert_id in enumerate(vertex_list):
    414415            triangles = self.domain.vertexlist[unique_vert_id]
    415416
  • inundation/ga/storm_surge/pyvolution/shallow_water.py

    r1642 r1657  
    9494
    9595        #Reduction operation for get_vertex_values
    96         #from util import mean
    97         #self.reduction = mean
    98         self.reduction = min  #Looks better near steep slopes
     96        from util import mean
     97        self.reduction = mean
     98        #self.reduction = min  #Looks better near steep slopes
    9999
    100100        self.quantities_to_be_stored = ['stage']
  • inundation/ga/storm_surge/pyvolution/test_least_squares.py

    r1632 r1657  
    831831
    832832        f = interp.fit(z)
    833         #f will be different from answerr due to smoothing
     833        #f will be different from answer due to smoothing
    834834        assert allclose(f, answer,atol=5)
    835835
     
    923923        answer = linear_function(data_points2)
    924924        assert allclose(z1, answer)
     925
     926
     927
     928
    925929
    926930
  • inundation/ga/storm_surge/pyvolution/test_quantity.py

    r1504 r1657  
    88from config import epsilon
    99from Numeric import allclose, array, ones, Float
     10
     11
     12#Aux for least_squares example
     13def linear_function(point):
     14    point = array(point)
     15    return point[:,0]+point[:,1]
    1016
    1117
     
    248254                        [[1,0,20], [1,20,4], [4,20,50], [30,1,4]])
    249255
     256
    250257    def test_set_vertex_values_using_general_interface(self):
    251258        quantity = Quantity(self.mesh4)
     
    266273                                               [2.5, 3.5, 2]])
    267274
     275
     276
     277
     278    def test_set_vertex_values_using_least_squares(self):
     279        from least_squares import Interpolation, fit_to_mesh
     280       
     281        quantity = Quantity(self.mesh4)
     282
     283        #Get (enough) datapoints
     284        data_points = [[ 0.66666667, 0.66666667],
     285                       [ 1.33333333, 1.33333333],
     286                       [ 2.66666667, 0.66666667],
     287                       [ 0.66666667, 2.66666667],
     288                       [ 0.0, 1.0],
     289                       [ 0.0, 3.0],
     290                       [ 1.0, 0.0],
     291                       [ 1.0, 1.0],
     292                       [ 1.0, 2.0],
     293                       [ 1.0, 3.0],
     294                       [ 2.0, 1.0],
     295                       [ 3.0, 0.0],
     296                       [ 3.0, 1.0]]
     297
     298
     299        interp = Interpolation(quantity.domain.coordinates, quantity.domain.triangles,
     300                               data_points, alpha=0.0)
     301
     302        z = linear_function(data_points)
     303        answer = linear_function(quantity.domain.coordinates)
     304
     305        f = interp.fit(z)
     306
     307        #print "f",f
     308        #print "answer",answer
     309        assert allclose(f, answer)
     310
     311
     312        quantity.set_values(f)
     313
     314
     315        answer = linear_function(quantity.domain.get_vertex_coordinates(obj = True))
     316        #print quantity.vertex_values, answer
     317        assert allclose(quantity.vertex_values.flat, answer)
     318
     319        #Now try using the general interface
     320
     321        vertex_attributes = fit_to_mesh(quantity.domain.coordinates,
     322                                        quantity.domain.triangles,
     323                                        data_points,
     324                                        z,
     325                                        alpha = 0,
     326                                        verbose=False)
     327
     328        #print vertex_attributes
     329        quantity.set_values(vertex_attributes)
     330        assert allclose(quantity.vertex_values.flat, answer)       
    268331
    269332
  • inundation/ga/storm_surge/validation/LWRU2/lwru2.py

    r1654 r1657  
    77http://www.cee.cornell.edu/longwave/index.cfm?page=benchmark&problem=2
    88
    9 
    109"""
    1110
    12 ######################
     11
    1312# Module imports
    14 
    15 from pyvolution.shallow_water import Domain, Reflective_boundary,\
    16      Dirichlet_boundary,Transmissive_boundary, Constant_height, Constant_stage
    17 
     13from pyvolution.shallow_water import Domain, Reflective_boundary, File_boundary
    1814from pyvolution.mesh_factory import rectangular
    1915from Numeric import array, zeros, Float, allclose
    2016
     17picklefile = 'domain.pck'
     18from cPickle import dump, load
     19
     20try:
     21    fid = open(picklefile)
     22    domain = load(fid)
     23    print 'Read pickled domain'
     24except:
     25
     26    #Preparing points
     27    from pyvolution.data_manager import xya2pts
     28    xya2pts('bathymetry.txt', verbose = True)
    2129
    2230
    23 #Preparing points
    24 from pyvolution.data_manager import xya2pts
    25 xya2pts('Benchmark_2_Bathymetry.txt', verbose = True)
     31    #######################
     32    # Domain
     33    print 'Creating domain'
     34    points, vertices, boundary = rectangular(200, 200/5*3,
     35                                             len1=5.448, len2=3.402)
     36    domain = Domain(points, vertices, boundary)
     37
     38    domain.check_integrity()
     39    domain.default_order = 2
     40
     41    print "Number of triangles = ", len(domain)
     42    domain.store = True    #Store for visualisation purposes
     43
     44    import sys, os
     45    base = os.path.basename(sys.argv[0])
     46    domain.filename, _ = os.path.splitext(base)
     47
     48    #LS code to be included in set_quantity
     49    from pyvolution import util, least_squares
     50    points, attributes = util.read_xya('bathymetry.pts')
     51   
     52    #Fit attributes to mesh
     53    vertex_attributes = least_squares.fit_to_mesh(domain.coordinates,
     54                                                  domain.triangles,
     55                                                  points,
     56                                                  attributes['elevation'],
     57                                                  verbose=True)
     58
     59    print 'Initial values'
     60    domain.set_quantity('elevation', vertex_attributes)
     61    domain.set_quantity('friction', 0.0)
     62    domain.set_quantity('stage', 0.0)
     63
     64
     65    fid = open('domain.pck', 'w')
     66    dump(domain, fid)
     67    fid.close()
    2668
    2769
    2870
    29 #######################
    30 # Domain
    31 #
    32 
    33 
    34 print 'Creating domain'
    35 #Create basic mesh
    36 #
    37 #The initial condition extends 50km off shore
    38 #and 5,000m is allowed on shore for wetting
    39 #(only about 200m is expected, though)
    40 
    41 #points, vertices, boundary = rectangular(400, 40,
    42 #                                         len1=55000, len2=5000,
    43 #                                         origin = (-5000, 0.0))
    44 
    45 points, vertices, boundary = rectangular(100, 100,
    46                                          len1=5.448, len2=3.402)
    47 
    48 
    49 #Create shallow water domain
    50 domain = Domain(points, vertices, boundary)
    51 
    52 domain.check_integrity()
    53 domain.default_order = 2
    54 
    55 #Output params
    56 domain.smooth = True
    57 domain.reduction = min  #Looks a lot better on top of steep slopes
    58 print "Number of triangles = ", len(domain)
    59 
    60 domain.visualise = False
    61 domain.store = True    #Store for visualisation purposes
    62 domain.format = 'sww'   #Native netcdf visualisation format
    63 
    64 import sys, os
    65 base = os.path.basename(sys.argv[0])
    66 domain.filename, _ = os.path.splitext(base)
    67 
    68 
    69 #LS code to be included in set_quantity
    70 from pyvolution import util, least_squares
    71 
    72 points, attributes = util.read_xya('Benchmark_2_Bathymetry.pts')
    73    
    74 #Fit attributes to mesh
    75 vertex_attributes = least_squares.fit_to_mesh(domain.coordinates,
    76                                               domain.triangles,
    77                                               points,
    78                                               attributes['elevation'],
    79                                               verbose=True)
    80 
    81 
    82 print 'Initial values'
    83 domain.set_quantity('elevation', vertex_attributes)
    84 #domain.set_quantity('elevation', 'Benchmark_2_Bathymetry.pts')
    85 domain.set_quantity('friction', 0.0)
    86 domain.set_quantity('stage', 0.0)
    87 
    88 #import sys; sys.exit()
    89 
    90 #print domain.quantities['stage'].centroid_values
    9171
    9272######################
     
    9575print 'Boundaries'
    9676Br = Reflective_boundary(domain)
    97 Bt = Transmissive_boundary(domain)
    98 
    99 #Constant inflow
    100 Bd = Dirichlet_boundary([0.0, 0.0, 0.0])
     77Bf = File_boundary('input_wave.txt', domain)
    10178
    10279#Set boundary conditions
    103 domain.set_boundary({'left': Br, 'right': Bt, 'bottom': Bt, 'top': Bt})
     80domain.set_boundary({'left': Bf, 'right': Br, 'bottom': Br, 'top': Br})
    10481
    10582
     
    10885t0 = time.time()
    10986
    110 
    111 
    112 
    113 pt = []
    114 xes = []
    115 y = 2500
    116 x0 = -100
    117 step = 50
    118 for i in range(1000):
    119     x = x0+i*step
    120     xes.append(x)
    121     pt.append( [x,y] )
    122 
    123 from pylab import *
    124 from pyvolution.least_squares import Interpolation
    125 
    126  
    127 V = domain.get_vertex_coordinates(obj=True)
    128 T = domain.get_triangles(obj=True)
    129 
    130 I = Interpolation(V,
    131                   T,
    132                   point_coordinates = pt,
    133                   verbose = True)
    134 
    135 
    136 f = domain.quantities['elevation'].vertex_values.flat
    137 z = I.interpolate( f )
    138 
    139 print 'xxxxx'
    140 
    141 ion()
    142 hold(False)
    143 f = domain.quantities['stage'].vertex_values.flat
    144 y = I.interpolate( f )
    145 plot(xes, y, '-b', xes, z, '-k')
    146 set( gca(), Ylim=(-10,10) )
    147 
    148 raw_input('go')
    149 for t in domain.evolve(yieldstep = 1, finaltime = 300.0):
    150 
    151     f = domain.quantities['stage'].vertex_values.flat
    152     y = I.interpolate( f )
    153     ioff()
    154     plot(xes, y, '-b', xes, z, '-k')
    155     ion()
    156     set( gca(), Ylim=(-10,10) )
    157    
    158    
    159     #print y[:], y.shape
    160 
    161 
     87for t in domain.evolve(yieldstep = 0.1, finaltime = 22.5):
    16288    domain.write_time()
    16389
    16490
    16591print 'That took %.2f seconds' %(time.time()-t0)
    166 show()
     92
Note: See TracChangeset for help on using the changeset viewer.