Changeset 8398


Ignore:
Timestamp:
Apr 12, 2012, 9:00:50 PM (13 years ago)
Author:
davies
Message:

New util

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_work/development/gareth/tests/util.py

    r8396 r8398  
    1616                               them according to their 'time'. This means that
    1717                               they can be stuck together using 'combine_outputs' correctly
    18  
     18
     19    util.triangle_areas -- compute the areas of every triangle in a get_outputs object [ must be vertex-based]
     20
     21    util.water_volume -- compute the water volume at every time step in an sww file (needs both vertex and centroid value input).
    1922
    2023    Here is an example ipython session which uses some of these functions:
     
    372375
    373376    return near_points, local_coord
     377
     378########################
     379# TRIANGLE AREAS, WATER VOLUME
     380def triangle_areas(p, subset='null'):
     381    # Compute areas of triangles in p -- assumes p contains vertex information
     382    # subset = vector of centroid indices to include in the computation.
     383
     384    if(subset=='null'):
     385        subset=range(len(p.vols[:,0]))
     386   
     387    x0=p.x[p.vols[subset,0]]
     388    x1=p.x[p.vols[subset,1]]
     389    x2=p.x[p.vols[subset,2]]
     390   
     391    y0=p.y[p.vols[subset,0]]
     392    y1=p.y[p.vols[subset,1]]
     393    y2=p.y[p.vols[subset,2]]
     394   
     395    # Vectors for cross-product
     396    v1_x=x0-x1
     397    v1_y=y0-y1
     398    #
     399    v2_x=x2-x1
     400    v2_y=y2-y1
     401    # Area
     402    area=(v1_x*v2_y-v1_y*v2_x)*0.5
     403    area=abs(area)
     404    return area
     405
     406###
     407
     408def water_volume(p,p2, per_unit_area=False, subset='null'):
     409    # Compute the water volume from p(vertex values) and p2(centroid values)
     410
     411    if(subset=='null'):
     412        subset=range(len(p2.x))
     413
     414    l=len(p2.time)
     415    area=triangle_areas(p, subset=subset)
     416   
     417    total_area=area.sum()
     418    volume=p2.time*0.
     419   
     420    # This accounts for how volume is measured in ANUGA
     421    # Compute in 2 steps to reduce precision error (important sometimes)
     422    for i in range(l):
     423        #volume[i]=((p2.stage[i,subset]-p2.elev[subset])*(p2.stage[i,subset]>p2.elev[subset])*area).sum()
     424        volume[i]=((p2.stage[i,subset])*(p2.stage[i,subset]>p2.elev[subset])*area).sum()
     425        volume[i]=volume[i]((-p2.elev[subset])*(p2.stage[i,subset]>p2.elev[subset])*area).sum()
     426   
     427    if(per_unit_area):
     428        volume=volume/total_area
     429   
     430    return volume
     431
     432
     433
Note: See TracChangeset for help on using the changeset viewer.