Changeset 4551


Ignore:
Timestamp:
Jun 20, 2007, 6:34:00 PM (17 years ago)
Author:
ole
Message:

Introduced maximal_inundation_elevation (runup) for sww files

Location:
anuga_core
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/documentation/user_manual/demos/runup.py

    r3980 r4551  
    2525
    2626domain = Domain(points, vertices, boundary) # Create domain
    27 domain.set_name('runup')                    # Output to bedslope.sww
     27domain.set_name('runup')                    # Output to file runup.sww
    2828domain.set_datadir('.')                     # Use current directory for output
    2929#domain.set_quantities_to_be_stored(['stage',# Store all conserved quantities
  • anuga_core/source/anuga/shallow_water/data_manager.py

    r4550 r4551  
    6969from Numeric import concatenate, array, Float, Int, Int32, resize, sometrue, \
    7070     searchsorted, zeros, allclose, around, reshape, transpose, sort, \
    71      NewAxis, ArrayType
     71     NewAxis, ArrayType, compress, take, arange, argmax
    7272from Scientific.IO.NetCDF import NetCDFFile
    7373#from shutil import copy
     
    18601860    #if verbose: bye= nsuadsfd[0] # uncomment to check catching verbose errors
    18611861   
    1862     #Read sww file
     1862    # Read sww file
    18631863    if verbose:
    18641864        print 'Reading from %s' %swwfile
     
    18821882    if origin is None:
    18831883
    1884         #Get geo_reference
    1885         #sww files don't have to have a geo_ref
     1884        # Get geo_reference
     1885        # sww files don't have to have a geo_ref
    18861886        try:
    18871887            geo_reference = Geo_reference(NetCDFObject=fid)
    18881888        except AttributeError, e:
    1889             geo_reference = Geo_reference() #Default georef object
     1889            geo_reference = Geo_reference() # Default georef object
    18901890
    18911891        xllcorner = geo_reference.get_xllcorner()
     
    18991899
    19001900
    1901     #FIXME: Refactor using code from Interpolation_function.statistics (in interpolate.py)
    1902     #Something like print swwstats(swwname)
     1901    # FIXME: Refactor using code from Interpolation_function.statistics (in interpolate.py)
     1902    # Something like print swwstats(swwname)
    19031903    if verbose:
    19041904        print '------------------------------------------------'
     
    53795379
    53805380
     5381
     5382
     5383
     5384def get_maximum_inundation_elevation(sww_filename, region=None, timesteps=None, verbose=False):
     5385    """Compute maximum run up height from sww file.
     5386
     5387    Algorithm is as in get_maximum_inundation_elevation from shallow_water_domain
     5388    except that this function works with the sww file and computes the maximal
     5389    runup height over multiple timesteps.
     5390   
     5391    Optional argument region restricts this to specified polygon.
     5392    Optional argument timesteps restricts this to a specified time step (an index)
     5393    or time interval (a list of indices).
     5394    """
     5395
     5396    # We are using nodal values here as that is what is stored in sww files.
     5397
     5398    # Water depth below which it is considered to be 0 in the model
     5399    # FIXME (Ole): Allow this to be specified as a keyword argument as well
     5400   
     5401    from anuga.config import minimum_allowed_height
     5402
     5403    if region is not None:
     5404        msg = 'region not yet implemented in get_maximum_inundation_elevation'
     5405        raise Exception, msg
     5406
     5407
     5408    root, extension = os.path.splitext(sww_filename)
     5409    if extension == '':
     5410        sww_filename += '.sww'
     5411   
     5412    # Read sww file
     5413    if verbose:
     5414        print 'Reading from %s' %sww_filename
     5415   
     5416    from Scientific.IO.NetCDF import NetCDFFile
     5417    fid = NetCDFFile(sww_filename)
     5418
     5419    # Get extent and reference
     5420    x = fid.variables['x'][:]
     5421    y = fid.variables['y'][:]
     5422    volumes = fid.variables['volumes'][:]
     5423
     5424
     5425    time = fid.variables['time'][:]
     5426    if timesteps is not None:
     5427        if type(timesteps) is list or type(timesteps) is ArrayType: 
     5428            timesteps = ensure_numeric(timesteps, Int)
     5429        elif type(timesteps) is type(0):
     5430            timesteps = [timesteps]
     5431        else:
     5432            msg = 'timesteps must be either an integer or a sequence of integers. '
     5433            msg += 'I got timesteps==%s, %s' %(timesteps, type(timesteps))
     5434            raise Exception, msg
     5435    else:
     5436        # Take them all
     5437        timesteps = arange(len(time))   
     5438   
     5439
     5440    # Get the relevant quantities
     5441    elevation = fid.variables['elevation'][:]
     5442    stage = fid.variables['stage'][:]
     5443
     5444
     5445    # Compute maximal runup for each timestep
     5446    maximal_runup = None
     5447    for i in timesteps:
     5448        #print 'time', time[i]
     5449        depth = stage[i,:] - elevation[:]
     5450   
     5451        # Get wet nodes i.e. nodes with depth>0 within given region and timesteps
     5452        wet_nodes = compress(depth > minimum_allowed_height, arange(len(depth)))
     5453
     5454        # Find maximum elevation among wet nodes and return
     5455        #runup_index = argmax(take(elevation, wet_nodes)) #FIXME Maybe get loc as well
     5456        runup = max(take(elevation, wet_nodes))
     5457
     5458        if runup > maximal_runup:
     5459            maximal_runup = runup  # This works even if maximal_runup is None
     5460               
     5461
     5462    return maximal_runup
     5463
     5464
    53815465#-------------------------------------------------------------
    53825466if __name__ == "__main__":
  • anuga_core/source/anuga/shallow_water/test_shallow_water_domain.py

    r4437 r4551  
    814814        """test_get_maximum_inundation_3(self)
    815815
    816         Test real runup example
     816        Test of real runup example:
    817817        """
    818818
     
    946946        assert allclose(wet_elevations, z)       
    947947       
     948
     949
     950    def test_get_maximum_inundation_from_sww(self):
     951        """test_get_maximum_inundation_from_sww(self)
     952
     953        Test of get_maximum_inundation_elevation(sww_filename) from data_manager.py
     954       
     955        This is based on test_get_maximum_inundation_3(self) but works with the
     956        stored results instead of with the internal data structure.
     957       
     958        """
     959
     960        from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross
     961        from data_manager import get_maximum_inundation_elevation
     962       
     963
     964        initial_runup_height = -0.4
     965        final_runup_height = -0.3
     966
     967
     968        #--------------------------------------------------------------
     969        # Setup computational domain
     970        #--------------------------------------------------------------
     971        N = 10
     972        points, vertices, boundary = rectangular_cross(N, N)
     973        domain = Domain(points, vertices, boundary)
     974        domain.set_name('runup_test')
     975        #domain.limit2007 = 1 #FIXME: This works better with old limiters
     976
     977        #--------------------------------------------------------------
     978        # Setup initial conditions
     979        #--------------------------------------------------------------
     980        def topography(x,y):
     981            return -x/2                             # linear bed slope
     982           
     983
     984        domain.set_quantity('elevation', topography)       # Use function for elevation
     985        domain.set_quantity('friction', 0.)                # Zero friction
     986        domain.set_quantity('stage', initial_runup_height) # Constant negative initial stage
     987
     988
     989        #--------------------------------------------------------------
     990        # Setup boundary conditions
     991        #--------------------------------------------------------------
     992        Br = Reflective_boundary(domain)              # Reflective wall
     993        Bd = Dirichlet_boundary([final_runup_height,  # Constant inflow
     994                                 0,
     995                                 0])
     996
     997        # All reflective to begin with (still water)
     998        domain.set_boundary({'left': Br, 'right': Br, 'top': Br, 'bottom': Br})
     999
     1000
     1001        #--------------------------------------------------------------
     1002        # Test initial inundation height
     1003        #--------------------------------------------------------------
     1004
     1005        indices = domain.get_wet_elements()
     1006        z = domain.get_quantity('elevation').\
     1007            get_values(location='centroids', indices=indices)
     1008        assert alltrue(z<initial_runup_height)
     1009
     1010        q_ref = domain.get_maximum_inundation_elevation()
     1011        assert allclose(q_ref, initial_runup_height, rtol = 1.0/N) # First order accuracy
     1012
     1013       
     1014        #--------------------------------------------------------------
     1015        # Let triangles adjust
     1016        #--------------------------------------------------------------
     1017        for t in domain.evolve(yieldstep = 0.1, finaltime = 1.0):
     1018            pass
     1019
     1020
     1021        #--------------------------------------------------------------
     1022        # Test inundation height again
     1023        #--------------------------------------------------------------
     1024       
     1025        q_ref = domain.get_maximum_inundation_elevation()
     1026        q = get_maximum_inundation_elevation('runup_test.sww')
     1027        msg = 'We got %f, should have been %f' %(q, q_ref)
     1028        assert allclose(q, q_ref, rtol=1.0/N), msg
     1029
     1030
     1031        q = get_maximum_inundation_elevation('runup_test.sww')
     1032        msg = 'We got %f, should have been %f' %(q, initial_runup_height)
     1033        assert allclose(q, initial_runup_height, rtol = 1.0/N), msg
     1034
     1035       
     1036
     1037
     1038        #--------------------------------------------------------------
     1039        # Update boundary to allow inflow
     1040        #--------------------------------------------------------------
     1041        domain.set_boundary({'right': Bd})
     1042
     1043       
     1044        #--------------------------------------------------------------
     1045        # Evolve system through time
     1046        #--------------------------------------------------------------
     1047        q_max = None
     1048        for t in domain.evolve(yieldstep = 0.1, finaltime = 3.0):
     1049            q = domain.get_maximum_inundation_elevation()
     1050            if q > q_max: q_max = q
     1051
     1052   
     1053        #--------------------------------------------------------------
     1054        # Test inundation height again
     1055        #--------------------------------------------------------------
     1056
     1057        indices = domain.get_wet_elements()
     1058        z = domain.get_quantity('elevation').\
     1059            get_values(location='centroids', indices=indices)
     1060
     1061        assert alltrue(z<final_runup_height)
     1062
     1063        q = domain.get_maximum_inundation_elevation()
     1064        assert allclose(q, final_runup_height, rtol = 1.0/N) # First order accuracy
     1065
     1066        q = get_maximum_inundation_elevation('runup_test.sww', timesteps=31)
     1067        msg = 'We got %f, should have been %f' %(q, final_runup_height)
     1068        assert allclose(q, final_runup_height, rtol=1.0/N), msg       
     1069
     1070
     1071        q = get_maximum_inundation_elevation('runup_test.sww')
     1072        msg = 'We got %f, should have been %f' %(q, q_max)
     1073        assert allclose(q, q_max, rtol=1.0/N), msg
     1074
     1075        q = get_maximum_inundation_elevation('runup_test.sww', timesteps=range(32))
     1076        msg = 'We got %f, should have been %f' %(q, q_max)
     1077        assert allclose(q, q_max, rtol=1.0/N), msg               
     1078
     1079
    9481080
    9491081
     
    44264558if __name__ == "__main__":
    44274559    suite = unittest.makeSuite(Test_Shallow_Water,'test')
    4428     #suite = unittest.makeSuite(Test_Shallow_Water,'test_get_maximum_inundation_3')
     4560    #suite = unittest.makeSuite(Test_Shallow_Water,'test_get_maximum_inundation_from_sww')
    44294561    #suite = unittest.makeSuite(Test_Shallow_Water,'test_temp')   
    44304562   
  • anuga_core/source/anuga/utilities/numerical_tools.py

    r4341 r4551  
    246246                     If not, an attempt is made to convert it to a Numeric
    247247                     array
     248        A: Scalar.   Return 0-dimensional array of length 1, containing that value
     249        A: String.   Array of ASCII values
    248250        typecode: Numeric type. If specified, use this in the conversion.
    249251                                If not, let Numeric decide
  • anuga_core/source/anuga/utilities/test_numerical_tools.py

    r3849 r4551  
    7777    def test_ensure_numeric(self):
    7878        from numerical_tools import ensure_numeric
    79         from Numeric import ArrayType, Float, array
     79        from Numeric import ArrayType, Float, Int, array
    8080
    8181        A = [1,2,3,4]
     
    122122        assert A is not B   #Not the same object
    123123
     124        # Check scalars
     125        A = 1
     126        B = ensure_numeric(A, Float)
     127        #print A, B[0], len(B), type(B)
     128        #print B.shape
     129        assert A == B
     130
     131        B = ensure_numeric(A, Int)       
     132        #print A, B
     133        #print B.shape
     134        assert A == B
     135
     136        # Error situation
     137
     138        B = ensure_numeric('hello', Int)               
     139        assert allclose(B, [104, 101, 108, 108, 111])
    124140
    125141    def test_gradient(self):
Note: See TracChangeset for help on using the changeset viewer.