Changeset 9160


Ignore:
Timestamp:
Jun 16, 2014, 1:21:36 PM (10 years ago)
Author:
davies
Message:

Change to plot utils to allow extracting sww subsets, + adding unit tests

Location:
trunk/anuga_core/source/anuga
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/shallow_water/swDE1_domain_ext.c

    r9148 r9160  
    703703                weir_height=max(riverwall_elevation[RiverWall_count-1]-min(zl,zr), 0.); // Reference weir height 
    704704                //weir_height=max(z_half-max(zl,zr), 0.); // Reference weir height 
    705 
    706                 // Get Qfactor index - multiply the idealised weir discharge by this constant factor
    707                 ii=riverwall_rowIndex[RiverWall_count-1]*ncol_riverwall_hydraulic_properties;
    708                 Qfactor=riverwall_hydraulic_properties[ii];
    709                 //printf("%e \n", Qfactor);
    710                 // Get s1, submergence ratio at which we start blending with the shallow water solution
    711                 ii+=1;
    712                 s1=riverwall_hydraulic_properties[ii];
    713                 // Get s2, submergence ratio at which we entirely use the shallow water solution
    714                 ii+=1;
    715                 s2=riverwall_hydraulic_properties[ii];
    716                 // Get h1, tailwater head / weir height at which we start blending with the shallow water solution
    717                 ii+=1;
    718                 h1=riverwall_hydraulic_properties[ii];
    719                 // Get h2, tailwater head / weir height at which we entirely use the shallow water solution
    720                 ii+=1;
    721                 h2=riverwall_hydraulic_properties[ii];
    722 
    723                 //printf("%e, %e, %e, %e, %e \n", Qfactor, s1, s2, h1, h2);
    724 
    725                 adjust_edgeflux_with_weir(edgeflux, h_left, h_right, g,
    726                                           weir_height, Qfactor,
    727                                           s1, s2, h1, h2);
    728                 // NOTE: Should perhaps also adjust the wave-speed here?? Small chance of instability??
    729                 //printf("%e \n", edgeflux[0]);
     705                // If the weir height is zero, avoid the weir compuattion entirely
     706                if(weir_height>0.){
     707                    // Get Qfactor index - multiply the idealised weir discharge by this constant factor
     708                    ii=riverwall_rowIndex[RiverWall_count-1]*ncol_riverwall_hydraulic_properties;
     709                    Qfactor=riverwall_hydraulic_properties[ii];
     710                    //printf("%e \n", Qfactor);
     711                    // Get s1, submergence ratio at which we start blending with the shallow water solution
     712                    ii+=1;
     713                    s1=riverwall_hydraulic_properties[ii];
     714                    // Get s2, submergence ratio at which we entirely use the shallow water solution
     715                    ii+=1;
     716                    s2=riverwall_hydraulic_properties[ii];
     717                    // Get h1, tailwater head / weir height at which we start blending with the shallow water solution
     718                    ii+=1;
     719                    h1=riverwall_hydraulic_properties[ii];
     720                    // Get h2, tailwater head / weir height at which we entirely use the shallow water solution
     721                    ii+=1;
     722                    h2=riverwall_hydraulic_properties[ii];
     723
     724                    //printf("%e, %e, %e, %e, %e \n", Qfactor, s1, s2, h1, h2);
     725
     726                    adjust_edgeflux_with_weir(edgeflux, h_left, h_right, g,
     727                                              weir_height, Qfactor,
     728                                              s1, s2, h1, h2);
     729                    // NOTE: Should perhaps also adjust the wave-speed here?? Small chance of instability??
     730                    //printf("%e \n", edgeflux[0]);
     731                }
    730732            }
    731733           
  • trunk/anuga_core/source/anuga/utilities/plot_utils.py

    r9151 r9160  
    4949from anuga.file.netcdf import NetCDFFile
    5050import numpy
     51import copy
    5152
    5253class combine_outputs:
     
    7879            if verbose: print i, filename
    7980            # Store output from filename
    80             p_tmp = get_output(filename, minimum_allowed_height)
     81            p_tmp = get_output(filename, minimum_allowed_height,verbose=verbose)
    8182            if(i==0):
    8283                # Create self
     
    9697                p1.yvel = numpy.append(p1.yvel, p_tmp.yvel, axis=0)
    9798                p1.vel = numpy.append(p1.vel, p_tmp.vel, axis=0)
    98 
    99         self.x, self.y, self.time, self.vols, self.elev, self.stage, self.xmom, \
    100                 self.ymom, self.xvel, self.yvel, self.vel, self.minimum_allowed_height = \
    101                 p1.x, p1.y, p1.time, p1.vols, p1.elev, p1.stage, p1.xmom, p1.ymom, p1.xvel,\
    102                 p1.yvel, p1.vel, p1.minimum_allowed_height
     99       
     100        self.x, self.y, self.time, self.vols, self.stage, \
     101                self.height, self.elev, self.friction, self.xmom, self.ymom, \
     102                self.xvel, self.yvel, self.vel, self.minimum_allowed_height,\
     103                self.xllcorner, self.yllcorner, self.timeSlices =\
     104                p1.x, p1.y, p1.time, p1.vols, p1.stage, \
     105                p1.height, p1.elev, p1.friction, p1.xmom, p1.ymom, \
     106                p1.xvel, p1.yvel, p1.vel, p1.minimum_allowed_height,\
     107                p1.xllcorner, p1.yllcorner, p1.timeSlices
     108
     109        self.filename = p1.filename
     110        self.verbose = p1.verbose
     111
    103112
    104113####################
     
    130139    return list(output_names)
    131140
    132 ##############
    133 
     141#####################################################################
    134142class get_output:
    135143    """Read in data from an .sww file in a convenient form
     
    139147       p then contains most relevant information as e.g., p.stage, p.elev, p.xmom, etc
    140148    """
    141     def __init__(self, filename, minimum_allowed_height=1.0e-03, verbose=False):
     149    def __init__(self, filename, minimum_allowed_height=1.0e-03, timeSlices='all', verbose=False):
     150                # FIXME: verbose is not used
    142151        self.x, self.y, self.time, self.vols, self.stage, \
    143152                self.height, self.elev, self.friction, self.xmom, self.ymom, \
    144153                self.xvel, self.yvel, self.vel, self.minimum_allowed_height,\
    145                 self.xllcorner, self.yllcorner = \
    146                 read_output(filename, minimum_allowed_height,verbose=verbose)
    147         self.filename=filename
     154                self.xllcorner, self.yllcorner, self.timeSlices = \
     155                read_output(filename, minimum_allowed_height,copy.copy(timeSlices))
     156        self.filename = filename
    148157        self.verbose = verbose
    149158
    150 
    151 def read_output(filename, minimum_allowed_height,verbose):
    152     # Input: The name of an .sww file to read data from,
    153     #                    e.g. read_sww('channel3.sww')
    154     #
    155     # Purpose: To read the sww file, and output a number of variables as arrays that
    156     #          we can then manipulate (e.g. plot, interrogate)
    157     #
    158     # Output: x, y, time, stage, height, elev, xmom, ymom, xvel, yvel, vel
    159     #         x,y are only stored at one time
    160     #         elevation may be stored at one or multiple times
    161     #         everything else is stored every time step for vertices
     159####################################################################
     160def getInds(varIn, timeSlices, absMax=False):
     161    """
     162     Convenience function to get the indices we want in an array.
     163     There are a number of special cases that make this worthwhile
     164     having in its own function
     165   
     166     INPUT: varIn -- numpy array, either 1D (variables in space) or 2D
     167            (variables in time+space)
     168            timeSlices -- times that we want the variable, see read_output or get_output
     169            absMax -- if TRUE and timeSlices is 'max', then get max-absolute-values
     170     OUTPUT:
     171           
     172    """
     173    var=copy.copy(varIn) # avoid python pointer issues
     174    if (len(varIn.shape)==2):
     175        # Get particular time-slices, unless the variable is constant
     176        # (e.g. elevation is often constant)
     177        if timeSlices is 'max':
     178            # Extract the maxima over time, assuming there are multiple
     179            # time-slices, and ensure the var is still a 2D array
     180            if( not absMax):
     181                var=var.max(axis=0)
     182            else:
     183                # For variables xmom,ymom,xvel,yvel we want the 'maximum-absolute-value'
     184                # We could do this everywhere, but I assume the loop is a bit slower
     185                varInds=abs(var).argmax(axis=0)
     186                varNew=varInds*0.
     187                for i in range(len(varInds)):
     188                    varNew[i] = var[varInds[i],i]
     189                #var=[var[varInds[i],i] for i in varInds]
     190                var=varNew
     191            var=var.reshape((1,len(var)))
     192        else:
     193            var=var[timeSlices,:]
     194   
     195    return var
     196
     197############################################################################
     198
     199def read_output(filename, minimum_allowed_height, timeSlices):
     200    """
     201     Purpose: To read the sww file, and output a number of variables as arrays that
     202              we can then e.g. plot, interrogate
     203
     204              See get_output for the typical interface, and get_centroids for
     205                working with centroids directly
     206   
     207     Input: filename -- The name of an .sww file to read data from,
     208                        e.g. read_sww('channel3.sww')
     209            minimum_allowed_height -- zero velocity when height < this
     210            timeSlices -- List of time indices to read (e.g. [100] or [0, 10, 21]), or 'all' or 'last' or 'max'
     211                          If 'max', the time-max of each variable will be computed. For xmom/ymom/xvel/yvel, the
     212                           one with maximum magnitude is reported
     213   
     214   
     215     Output: x, y, time, stage, height, elev, xmom, ymom, xvel, yvel, vel
     216             x,y are only stored at one time
     217             elevation may be stored at one or multiple times
     218             everything else is stored every time step for vertices
     219    """
    162220
    163221    # Import modules
     
    167225    # Open ncdf connection
    168226    fid=NetCDFFile(filename)
     227   
     228    time=fid.variables['time'][:]
     229
     230    # Treat specification of timeSlices
     231    if(timeSlices=='all'):
     232        inds=range(len(time))
     233    elif(timeSlices=='last'):
     234        inds=[len(time)-1]
     235    elif(timeSlices=='max'):
     236        inds='max' #
     237    else:
     238        inds=list(timeSlices)
     239   
     240    if(inds is not 'max'):
     241        time=time[inds]
     242    else:
     243        # We can't really assign a time to 'max', but I guess max(time) is
     244        # technically the right thing -- if not misleading
     245        time=time.max()
     246
    169247   
    170248    # Get lower-left
     
    172250    yllcorner=fid.yllcorner
    173251
    174 
    175252    # Read variables
    176253    x=fid.variables['x'][:]
    177     #x=x.getValue()
    178254    y=fid.variables['y'][:]
    179     #y=y.getValue()
    180 
    181     stage=fid.variables['stage'][:]
    182     #stage=stage.getValue()
    183 
    184     elev=fid.variables['elevation'][:]
     255
     256    stage=getInds(fid.variables['stage'][:], timeSlices=inds)
     257    elev=getInds(fid.variables['elevation'][:], timeSlices=inds)
     258
     259    # Simple approach for volumes
     260    vols=fid.variables['volumes'][:]
     261
     262    # Friction if it exists
     263    if(fid.variables.has_key('friction')):
     264        friction=getInds(fid.variables['friction'][:],timeSlices=inds)
     265    else:
     266        # Set friction to nan if it is not stored
     267        friction=elev*0.+numpy.nan
     268   
     269    #@ Here we get 'all' of height / xmom /ymom
     270    #@ This could be done using less memory/computation in
     271    #@  the case of multiple time-slices
    185272
    186273    if(fid.variables.has_key('height')):
    187         height=fid.variables['height'][:]
     274        heightAll=fid.variables['height'][:]
    188275    else:
    189276        # Back calculate height if it is not stored
    190         height=fid.variables['stage'][:]
    191         if(len(stage.shape)==len(elev.shape)):
    192             for i in range(stage.shape[0]):
    193                 height[i,:]=stage[i,:]-elev[i,:]
     277        heightAll=fid.variables['stage'][:]
     278        if(len(heightAll.shape)==len(elev.shape)):
     279            heightAll=heightAll-elev
    194280        else:
    195             for i in range(stage.shape[0]):
    196                 height[i,:]=stage[i,:]-elev
    197 
    198     if(fid.variables.has_key('friction')):
    199         friction=fid.variables['friction'][:]
    200     else:
    201         # Set friction to nan if it is not stored
    202         friction=height*0.+numpy.nan
    203 
    204     xmom=fid.variables['xmomentum'][:]
    205     #xmom=xmom.getValue()
    206 
    207     ymom=fid.variables['ymomentum'][:]
    208     #ymom=ymom.getValue()
    209 
    210     time=fid.variables['time'][:]
    211     #time=time.getValue()
    212 
    213     vols=fid.variables['volumes'][:]
    214     #vols=vols.getValue()
    215 
    216 
    217     # Calculate velocity = momentum/depth
    218     xvel=xmom*0.0
    219     yvel=ymom*0.0
    220 
    221     for i in range(xmom.shape[0]):
    222         xvel[i,:]=xmom[i,:]/(height[i,:]+1.0e-06)*(height[i,:]>minimum_allowed_height)
    223         yvel[i,:]=ymom[i,:]/(height[i,:]+1.0e-06)*(height[i,:]>minimum_allowed_height)
    224 
    225     vel = (xvel**2+yvel**2)**0.5
    226 
    227     return x, y, time, vols, stage, height, elev, friction, xmom, ymom, xvel, yvel, vel, minimum_allowed_height, xllcorner,yllcorner
    228 
    229 ##############
    230 
    231 
     281            for i in range(heightAll.shape[0]):
     282                heightAll[i,:]=heightAll[i,:]-elev
     283    heightAll=heightAll*(heightAll>0.) # Height could be negative for tsunami algorithm
     284    # Need xmom,ymom for all timesteps
     285    xmomAll=fid.variables['xmomentum'][:]
     286    ymomAll=fid.variables['ymomentum'][:]
     287
     288    height=getInds(heightAll, timeSlices=inds)
     289    # For momenta, we want maximum-absolute-value events
     290    xmom=getInds(xmomAll, timeSlices=inds, absMax=True)
     291    ymom=getInds(ymomAll, timeSlices=inds, absMax=True)
     292
     293    # velocity requires some intermediate calculation in general
     294    tmp = xmomAll/(heightAll+1.0e-12)*(heightAll>minimum_allowed_height)
     295    xvel=getInds(tmp,timeSlices=inds, absMax=True)
     296    tmp = ymomAll/(heightAll+1.0e-12)*(heightAll>minimum_allowed_height)
     297    yvel=getInds(tmp,timeSlices=inds, absMax=True)
     298    tmp = (xmomAll**2+ymomAll**2)**0.5/(heightAll+1.0e-12)*(heightAll>minimum_allowed_height)
     299    vel=getInds(tmp, timeSlices=inds) # Vel is always >= 0.
     300
     301    fid.close()
     302
     303    return x, y, time, vols, stage, height, elev, friction, xmom, ymom,\
     304           xvel, yvel, vel, minimum_allowed_height, xllcorner,yllcorner, inds
     305
     306######################################################################################
    232307
    233308class get_centroids:
    234309    """
    235     Extract centroid values from the output of get_output. 
     310    Extract centroid values from the output of get_output, OR from a
     311        filename 
     312    See read_output or get_centroid_values for further explanation of
     313        arguments
    236314    e.g.
    237         p = util.get_output('my_sww.sww', minimum_allowed_height=0.01) # vertex values
    238         pc=util.get_centroids(p, velocity_extrapolation=True) # centroid values
    239 
    240     NOTE: elevation is only stored once in the output, even if it was stored every timestep
    241          This is done because presently centroid elevations do not change over time.
    242     """
    243     def __init__(self,p, velocity_extrapolation=False, verbose=False):
     315        # Case 1 -- get vertex values first, then centroids
     316        p = util.get_output('my_sww.sww', minimum_allowed_height=0.01)
     317        pc=util.get_centroids(p, velocity_extrapolation=True)
     318
     319        # Case 2 -- get centroids directly
     320        pc=util.get_centroids('my_sww.sww', velocity_extrapolation=True)
     321
     322    NOTE: elevation is only stored once in the output, even if it was
     323          stored every timestep.
     324           This is done because presently centroid elevations in ANUGA
     325           do not change over time. 
     326           Also lots of existing plotting code assumes elevation is a 1D
     327           array
     328    """
     329    def __init__(self,p, velocity_extrapolation=False, verbose=False,
     330                 timeSlices=None, minimum_allowed_height=1.0e-03):
    244331       
    245332        self.time, self.x, self.y, self.stage, self.xmom,\
    246              self.ymom, self.height, self.elev, self.friction, self.xvel, \
    247              self.yvel, self.vel= \
    248              get_centroid_values(p, velocity_extrapolation,verbose=verbose)
     333             self.ymom, self.height, self.elev, self.friction, self.xvel,\
     334             self.yvel, self.vel, self.xllcorner, self.yllcorner, self.timeSlices= \
     335             get_centroid_values(p, velocity_extrapolation,\
     336                         timeSlices=copy.copy(timeSlices),\
     337                         minimum_allowed_height=minimum_allowed_height,\
     338                         verbose=verbose)
    249339                                 
    250340
    251 def get_centroid_values(p, velocity_extrapolation, verbose=False):
    252     # Input: p is the result of e.g. p=util.get_output('mysww.sww'). See the get_output class defined above
    253     # Output: Values of x, y, Stage, xmom, ymom, elev, xvel, yvel, vel at centroids
    254     #import numpy
    255    
     341def get_centroid_values(p, velocity_extrapolation, verbose, timeSlices,
     342                        minimum_allowed_height):
     343    """
     344    Function to get centroid information -- main interface is through
     345        get_centroids.
     346        See get_centroids for usage examples, and read_output or get_output for further relevant info
     347     Input:
     348           p --  EITHER:
     349                  The result of e.g. p=util.get_output('mysww.sww').
     350                  See the get_output class defined above.
     351                 OR:
     352                  Alternatively, the name of an sww file
     353   
     354           velocity_extrapolation -- If true, and centroid values are not
     355            in the file, then compute centroid velocities from vertex velocities, and
     356            centroid momenta from centroid velocities. If false, and centroid values
     357            are not in the file, then compute centroid momenta from vertex momenta,
     358            and centroid velocities from centroid momenta
     359   
     360           timeSlices = list of integer indices when we want output for, or
     361                        'all' or 'last' or 'max'. See read_output
     362   
     363           minimum_allowed_height = height at which velocities are zeroed. See read_output
     364   
     365     Output: Values of x, y, Stage, xmom, ymom, elev, xvel, yvel, vel etc at centroids
     366    """
     367
     368    #@ Figure out if p is a string (filename) or the output of get_output
     369    pIsFile=(type(p) is str)
     370 
     371    if(pIsFile):
     372        fid=NetCDFFile(p)
     373    else:
     374        fid=NetCDFFile(p.filename)
     375
     376    # UPDATE: 15/06/2014 -- below, we now get all variables directly from the file
     377    #         This is more flexible, and allows to get 'max' as well
     378    #         However, potentially it could have performance penalities vs the old approach (?)
     379
    256380    # Make 3 arrays, each containing one index of a vertex of every triangle.
    257     l=len(p.vols)
    258     #vols0=numpy.zeros(l, dtype='int')
    259     #vols1=numpy.zeros(l, dtype='int')
    260     #vols2=numpy.zeros(l, dtype='int')
    261 
    262     # FIXME: 22/2/12/ - I think this loop is slow, should be able to do this
    263     # another way
    264 #    for i in range(l):
    265 #        vols0[i]=p.vols[i][0]
    266 #        vols1[i]=p.vols[i][1]
    267 #        vols2[i]=p.vols[i][2]
    268 
    269 
    270 
    271     vols0=p.vols[:,0]
    272     vols1=p.vols[:,1]
    273     vols2=p.vols[:,2]
    274 
    275     #print vols0.shape
    276     #print p.vols.shape
    277 
    278     # Then use these to compute centroid averages
    279     x_cent=(p.x[vols0]+p.x[vols1]+p.x[vols2])/3.0
    280     y_cent=(p.y[vols0]+p.y[vols1]+p.y[vols2])/3.0
     381    vols=fid.variables['volumes'][:]
     382    vols0=vols[:,0]
     383    vols1=vols[:,1]
     384    vols2=vols[:,2]
     385   
     386    # Get lower-left offset
     387    xllcorner=fid.xllcorner
     388    yllcorner=fid.yllcorner
     389   
     390    #@ Get timeSlices
     391    # It will be either a list of integers, or 'max'
     392    l=len(vols)
     393    time=fid.variables['time'][:]
     394    nts=len(time) # number of time slices in the file
     395    if(timeSlices is None):
     396        if(pIsFile):
     397            # Assume all timeSlices
     398            timeSlices=range(nts)
     399        else:
     400            timeSlices=copy.copy(p.timeSlices)
     401    else:
     402        # Treat word-based special cases
     403        if(timeSlices is 'all'):
     404            timeSlices=range(nts)
     405        if(timeSlices is 'last'):
     406            timeSlices=[nts-1]
     407
     408    #@ Get minimum_allowed_height
     409    if(minimum_allowed_height is None):
     410        if(pIsFile):
     411            minimum_allowed_height=0.
     412        else:
     413            minimum_allowed_height=copy.copy(p.minimum_allowed_height)
     414
     415    # Treat specification of timeSlices
     416    if(timeSlices=='all'):
     417        inds=range(len(time))
     418    elif(timeSlices=='last'):
     419        inds=[len(time)-1]
     420    elif(timeSlices=='max'):
     421        inds='max' #
     422    else:
     423        inds=list(timeSlices)
     424   
     425    if(inds is not 'max'):
     426        time=time[inds]
     427    else:
     428        # We can't really assign a time to 'max', but I guess max(time) is
     429        # technically the right thing -- if not misleading
     430        time=time.max()
     431
     432    # Get coordinates
     433    x=fid.variables['x'][:]
     434    y=fid.variables['y'][:]
     435    x_cent=(x[vols0]+x[vols1]+x[vols2])/3.0
     436    y_cent=(y[vols0]+y[vols1]+y[vols2])/3.0
     437
     438    def getCentVar(varkey_c, timeSlices=inds, absMax=False):
     439        """
     440            Convenience function, assumes knowedge of 'timeSlices' and vols0,1,2
     441        """
     442        if(fid.variables.has_key(varkey_c)==False):
     443            # It looks like centroid values are not stored
     444            # In this case, compute centroid values from vertex values
     445           
     446            newkey=varkey_c.replace('_c','')
     447            tmp = fid.variables[newkey][:]
     448            tmp=(tmp[:,vols0]+tmp[:,vols1]+tmp[:,vols2])/3.0
     449            var_cent=getInds(tmp, timeSlices=timeSlices, absMax=absMax)
     450        else:
     451            var_cent=getInds(fid.variables[varkey_c][:], timeSlices=timeSlices, absMax=absMax)
     452        return var_cent
     453
     454    # Stage and height and elevation
     455    stage_cent=getCentVar('stage_c', timeSlices=inds)
     456    elev_cent=getCentVar('elevation_c', timeSlices=inds)
     457
     458    if(len(elev_cent)==2):
     459        # Coerce to 1D array, since lots of our code assumes it is
     460        elev_cent=elev_cent[0,:]
     461
     462    height_cent=stage_cent*0.
     463    for i in range(stage_cent.shape[0]):
     464        height_cent[i,:]=stage_cent[i,:]-elev_cent
     465
     466    # Friction might not be stored at all
     467    try:
     468        friction_cent=getCentVar('friction_c')
     469    except:
     470        friction_cent=elev_cent*0.+numpy.nan
     471
     472    if(fid.variables.has_key('xmomentum_c')):
     473        # Assume that both xmomentum,ymomentum are stored at centroids
     474        # Because velocity is back computed, and we might want maxima,
     475        # we get all data for convenience
     476        xmomC=getCentVar('xmomentum_c', timeSlices=range(nts))
     477        ymomC=getCentVar('ymomentum_c', timeSlices=range(nts))
     478
     479        # height might not be stored
     480        try:
     481            hC = getCentVar('height_c', timeSlices=range(nts))
     482        except:
     483            # Compute from stage
     484            hC = getCentVar('stage_c', timeSlices=range(nts))
     485            for i in range(hC.shape[0]):
     486                hC[i,:]=hC[i,:]-elev_cent
     487           
     488        xmom_cent = getInds(xmomC*(hC>minimum_allowed_height), timeSlices=inds,absMax=True)
     489        xvel_cent = getInds(xmomC/(hC+1.0e-06)*(hC>minimum_allowed_height), timeSlices=inds, absMax=True)
     490
     491        ymom_cent = getInds(ymomC*(hC>minimum_allowed_height), timeSlices=inds,absMax=True)
     492        yvel_cent = getInds(ymomC/(hC+1.0e-06)*(hC>minimum_allowed_height), timeSlices=inds, absMax=True)
     493
     494        tmp = (xmomC**2 + ymomC**2)**0.5/(hC+1.0e-06)*(hC>minimum_allowed_height)
     495        vel_cent=getInds(tmp, timeSlices=inds)
    281496       
    282     fid=NetCDFFile(p.filename)
    283     if(fid.variables.has_key('stage_c')==False):
    284 
    285         stage_cent=(p.stage[:,vols0]+p.stage[:,vols1]+p.stage[:,vols2])/3.0
    286         height_cent=(p.height[:,vols0]+p.height[:,vols1]+p.height[:,vols2])/3.0
    287 
    288         try:
    289             friction_cent=(p.friction[:,vols0]+p.friction[:,vols1]+p.friction[:,vols2])/3.0
    290         except:
    291             try:
    292                 friction_cent=(p.friction[vols0]+p.friction[vols1]+p.friction[vols2])/3.0
    293             except:
    294                 pass
    295 
    296         # Only store elevation centroid once (since it doesn't change)
    297         if(len(p.elev.shape)==2):
    298             elev_cent=(p.elev[0,vols0]+p.elev[0,vols1]+p.elev[0,vols2])/3.0
     497    else:
     498        #@ COMPUTE CENTROIDS FROM VERTEX VALUES
     499        #@
     500        #@ Here we get 'all' of height / xmom /ymom
     501        #@ This could be done using less memory/computation in
     502        #@  the case of multiple time-slices
     503        if(fid.variables.has_key('height')):
     504            heightAll=fid.variables['height'][:]
    299505        else:
    300             elev_cent=(p.elev[vols0]+p.elev[vols1]+p.elev[vols2])/3.0
    301 
    302         # Here, we need to treat differently the case of momentum extrapolation or
    303         # velocity extrapolation
     506            # Back calculate height if it is not stored
     507            heightAll=fid.variables['stage'][:]
     508            elev = fid.variables['elevation'][:]
     509            if(len(heightAll.shape)==len(elev.shape)):
     510                heightAll=heightAll-elev
     511            else:
     512                for i in range(heightAll.shape[0]):
     513                    heightAll[i,:]=heightAll[i,:]-elev
     514        heightAll=heightAll*(heightAll>0.) # Height could be negative for tsunami algorithm
     515        # Need xmom,ymom for all timesteps
     516        xmomAll=fid.variables['xmomentum'][:]
     517        ymomAll=fid.variables['ymomentum'][:]
     518       
    304519        if velocity_extrapolation:
    305             xvel_cent=(p.xvel[:,vols0]+p.xvel[:,vols1]+p.xvel[:,vols2])/3.0
    306             yvel_cent=(p.yvel[:,vols0]+p.yvel[:,vols1]+p.yvel[:,vols2])/3.0
     520            # Compute velocity from vertex velocities, then back-compute
     521            # momentum from that
     522            tmp = xmomAll/(heightAll+1.0-06)*(heightAll>minimum_allowed_height)
     523            xvel=(tmp[:,vols0]+tmp[:,vols1]+tmp[:,vols2])/3.0
     524            htc = (heightAll[:,vols0] + heightAll[:,vols1] + heightAll[:,vols2])/3.0
     525            xvel_cent=getInds(xvel, timeSlices=inds, absMax=True)
     526            xmom_cent=getInds(xvel*htc, timeSlices=inds, absMax=True)
     527
     528            tmp = ymomAll/(heightAll+1.0-06)*(heightAll>minimum_allowed_height)
     529            yvel=(tmp[:,vols0]+tmp[:,vols1]+tmp[:,vols2])/3.0
     530            yvel_cent=getInds(yvel, timeSlices=inds, absMax=True)
     531            ymom_cent=getInds(yvel*htc, timeSlices=inds, absMax=True)
     532           
     533            vel_cent=getInds( (xvel**2+yvel**2)**0.5, timeSlices=inds)
    307534           
    308             # Now compute momenta
    309             xmom_cent=stage_cent*0.0
    310             ymom_cent=stage_cent*0.0
    311 
    312             t=len(p.time)
    313 
    314             for i in range(t):
    315                 xmom_cent[i,:]=xvel_cent[i,:]*(height_cent[i,:]+1e-06)*\
    316                                                 (height_cent[i,:]>p.minimum_allowed_height)
    317                 ymom_cent[i,:]=yvel_cent[i,:]*(height_cent[i,:]+1e-06)*\
    318                                                 (height_cent[i,:]>p.minimum_allowed_height)
    319535        else:
    320             xmom_cent=(p.xmom[:,vols0]+p.xmom[:,vols1]+p.xmom[:,vols2])/3.0
    321             ymom_cent=(p.ymom[:,vols0]+p.ymom[:,vols1]+p.ymom[:,vols2])/3.0
    322 
    323             # Now compute velocities
    324             xvel_cent=stage_cent*0.0
    325             yvel_cent=stage_cent*0.0
    326 
    327             t=len(p.time)
    328 
    329             for i in range(t):
    330                 xvel_cent[i,:]=xmom_cent[i,:]/(height_cent[i,:]+1.0e-06)*(height_cent[i,:]>p.minimum_allowed_height)
    331                 yvel_cent[i,:]=ymom_cent[i,:]/(height_cent[i,:]+1.0e-06)*(height_cent[i,:]>p.minimum_allowed_height)
    332    
    333     else:
    334         # Get centroid values from file
    335         if verbose: print 'Reading centroids from file'
    336         stage_cent=fid.variables['stage_c'][:]
    337         elev_cent=fid.variables['elevation_c'][:]
    338         if(len(elev_cent.shape)==2):
    339             elev_cent=elev_cent[0,:] # Only store elevation centroid once -- since it is constant
    340         if(fid.variables.has_key('height_c')==True):
    341             height_cent=fid.variables['height_c'][:]
    342         else:
    343             height_cent=1.0*stage_cent
    344             for i in range(len(p.time)):
    345                 height_cent[i,:]=stage_cent[i,:]-elev_cent
    346        
    347         if(fid.variables.has_key('friction_c')==True):
    348             friction_cent=fid.variables['friction_c'][:]
    349         else:
    350             friction_cent=(p.friction[:,vols0]+p.friction[:,vols1]+p.friction[:,vols2])/3.0
    351        
    352         xmom_cent=fid.variables['xmomentum_c'][:]*(height_cent>p.minimum_allowed_height)
    353         ymom_cent=fid.variables['ymomentum_c'][:]*(height_cent>p.minimum_allowed_height)
    354         xvel_cent=xmom_cent/(height_cent+1.0e-06)
    355         yvel_cent=ymom_cent/(height_cent+1.0e-06)
    356        
    357    
    358     # Compute velocity
    359     vel_cent=(xvel_cent**2 + yvel_cent**2)**0.5
    360 
    361     return p.time, x_cent, y_cent, stage_cent, xmom_cent,\
    362              ymom_cent, height_cent, elev_cent, friction_cent, xvel_cent, yvel_cent, vel_cent
     536            # Compute momenta from vertex momenta, then back compute velocity from that
     537            tmp=xmomAll*(heightAll>minimum_allowed_height)
     538            htc = (heightAll[:,vols0] + heightAll[:,vols1] + heightAll[:,vols2])/3.0
     539            xmom=(tmp[:,vols0]+tmp[:,vols1]+tmp[:,vols2])/3.0
     540            xmom_cent=getInds(xmom,  timeSlices=inds, absMax=True)
     541            xvel_cent=getInds(xmom/(htc+1.0e-06), timeSlices=inds, absMax=True)
     542
     543            tmp=ymomAll*(heightAll>minimum_allowed_height)
     544            ymom=(tmp[:,vols0]+tmp[:,vols1]+tmp[:,vols2])/3.0
     545            ymom_cent=getInds(ymom,  timeSlices=inds, absMax=True)
     546            yvel_cent=getInds(ymom/(htc+1.0e-06), timeSlices=inds, absMax=True)
     547            vel_cent = getInds( (xmom**2+ymom**2)**0.5/(htc+1.0e-06), timeSlices=inds)
     548
     549    fid.close()
     550   
     551    return time, x_cent, y_cent, stage_cent, xmom_cent,\
     552             ymom_cent, height_cent, elev_cent, friction_cent,\
     553             xvel_cent, yvel_cent, vel_cent, xllcorner, yllcorner, inds
    363554
    364555
     
    422613        c = -intercept
    423614    else:
    424         #print 'FIXME: Still need to treat 0 and infinite gradients'
    425         #assert 0==1
    426615        a=1.
    427616        b=0.
     
    500689    # This accounts for how volume is measured in ANUGA
    501690    # Compute in 2 steps to reduce precision error (important sometimes)
     691    # Is this really needed?
    502692    for i in range(l):
    503693        #volume[i]=((p2.stage[i,subset]-p2.elev[subset])*(p2.stage[i,subset]>p2.elev[subset])*area).sum()
     
    680870    if(swwFile is not None):
    681871        # Read in ANUGA outputs
    682         # FIXME: It would be good to support reading of data subsets in util.get_output
    683872        if(verbose):
    684873            print 'Reading sww File ...'
    685         p=util.get_output(swwFile,min_allowed_height)
    686         p2=util.get_centroids(p,velocity_extrapolation)
    687         swwIn=scipy.io.netcdf_file(swwFile)
    688         xllcorner=swwIn.xllcorner
    689         yllcorner=swwIn.yllcorner
     874        p2=util.get_centroids(swwFile, velocity_extrapolation, timeSlices=myTimeStep,
     875                              minimum_allowed_height=min_allowed_height)
     876        xllcorner=p2.xllcorner
     877        yllcorner=p2.yllcorner
    690878
    691879        if(myTimeStep=='all'):
    692880            myTimeStep=range(len(p2.time))
    693881        elif(myTimeStep=='last'):
    694             myTimeStep=len(p2.time)-1
     882            # This is [0]!
     883            myTimeStep=[len(p2.time)-1]
     884
    695885        # Ensure myTimeStep is a list
    696886        if type(myTimeStep)!=list:
     
    741931        cut_points=(nxutils.points_inside_poly(gridXY_array, bounding_polygon)==False).nonzero()[0]
    742932       
    743     #import pdb
    744     #pdb.set_trace()
    745 
    746933    # Loop over all output quantities and produce the output
    747934    for myTSi in myTimeStep:
     
    798985                output_name=output_dir+'/'+os.path.splitext(os.path.basename(swwFile))[0] + '_'+\
    799986                            output_quantity+'_'+timestepString+\
    800                             '_'+str(myTS)+'.tif'
     987                            '.tif'
     988                            #'_'+str(myTS)+'.tif'
    801989            else:
    802990                output_name=output_dir+'/'+'PointData_'+output_quantity+'.tif'
  • trunk/anuga_core/source/anuga/validation_utilities/parameters.py

    r9148 r9160  
    77__date__ ="$20/08/2012 11:20:00 PM$"
    88
    9 alg = 'DE0'
     9alg = 'DE1'
    1010cfl = 1.0
    1111
Note: See TracChangeset for help on using the changeset viewer.