Changeset 2407


Ignore:
Timestamp:
Feb 14, 2006, 5:19:29 PM (19 years ago)
Author:
ole
Message:

Pushed caching into conversion functions and beautified the Sydney scenario

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • inundation/pyvolution/data_manager.py

    r2344 r2407  
    10741074
    10751075
    1076 def dem2pts(basename_in, basename_out=None, verbose=False,
     1076def dem2pts(basename_in, basename_out=None,
    10771077            easting_min=None, easting_max=None,
    1078             northing_min=None, northing_max=None):
     1078            northing_min=None, northing_max=None,
     1079            use_cache=False, verbose=False,):
    10791080    """Read Digitial Elevation model from the following NetCDF format (.dem)
    10801081
     
    10931094    points:  (Nx2) Float array
    10941095    elevation: N Float array
     1096    """
     1097
     1098
     1099
     1100    kwargs = {'basename_out': basename_out, 
     1101              'easting_min': easting_min,
     1102              'easting_max': easting_max,
     1103              'northing_min': northing_min,
     1104              'northing_max': northing_max,
     1105              'verbose': verbose}
     1106
     1107    if use_cache is True:
     1108        from caching import cache
     1109        result = cache(_dem2pts, basename_in, kwargs,
     1110                       dependencies = [basename_in + '.dem'],
     1111                       verbose = verbose)
     1112
     1113    else:
     1114        result = apply(_dem2pts, [basename_in], kwargs)
     1115       
     1116    return result
     1117
     1118
     1119def _dem2pts(basename_in, basename_out=None, verbose=False,
     1120            easting_min=None, easting_max=None,
     1121            northing_min=None, northing_max=None):
     1122    """Read Digitial Elevation model from the following NetCDF format (.dem)
     1123
     1124    Internal function. See public function dem2pts for details.   
    10951125    """
    10961126
     
    18311861
    18321862
    1833 
    18341863def convert_dem_from_ascii2netcdf(basename_in, basename_out = None,
    1835                                   verbose=False):
    1836     """Read Digitial Elevation model from the following ASCII format (.asc)
     1864                                  use_cache = False,
     1865                                  verbose = False):
     1866    """Read Digitial Elevation model from the following ASCII format (.asc)   
    18371867
    18381868    Example:
     
    18641894    Yshift        10000000.0000000000
    18651895    Parameters
     1896    """
     1897
     1898
     1899
     1900    kwargs = {'basename_out': basename_out, 'verbose': verbose}
     1901
     1902    if use_cache is True:
     1903        from caching import cache
     1904        result = cache(_convert_dem_from_ascii2netcdf, basename_in, kwargs,
     1905                       dependencies = [basename_in + '.asc'],
     1906                       verbose = verbose)
     1907
     1908    else:
     1909        result = apply(_convert_dem_from_ascii2netcdf, [basename_in], kwargs)
     1910       
     1911    return result
     1912
     1913   
     1914   
     1915
     1916
     1917
     1918def _convert_dem_from_ascii2netcdf(basename_in, basename_out = None,
     1919                                  verbose = False):
     1920    """Read Digitial Elevation model from the following ASCII format (.asc)
     1921
     1922    Internal function. See public function convert_dem_from_ascii2netcdf for details.
    18661923    """
    18671924
  • inundation/pyvolution/general_mesh.py

    r1894 r2407  
    5050    """
    5151
     52    #FIXME: It would be a good idea to use geospatial data as an alternative
     53    #input
    5254    def __init__(self, coordinates, triangles,
    5355                 geo_reference=None):
  • inundation/pyvolution/pmesh2domain.py

    r2325 r2407  
    1616    vertex_coordinates, vertices, tag_dict, vertex_quantity_dict \
    1717                        ,tagged_elements_dict, geo_reference = \
    18                 _pmesh_to_domain(mesh_instance=mesh)
     18                        _pmesh_to_domain(mesh_instance=mesh)
    1919
    2020
     
    3636    return domain
    3737
    38 def pmesh_to_domain_instance(file_name, DomainClass):
     38
     39
     40def pmesh_to_domain_instance(file_name, DomainClass, use_cache = False, verbose = False):
    3941    """
    4042    Converts a mesh file(.tsh or .msh), to a Domain instance.
     
    4446    DomainClass is the Class that will be returned.
    4547    It must be a subclass of Domain, with the same interface as domain.
    46    
     48
     49    use_cache: True means that caching is attempted for the computed domain.   
     50    """
     51
     52    if use_cache is True:
     53        from caching import cache
     54        result = cache(_pmesh_to_domain_instance, (file_name, DomainClass),
     55                       dependencies = [file_name],                     
     56                       verbose = verbose)
     57
     58    else:
     59        result = apply(_pmesh_to_domain_instance, (file_name, DomainClass))       
     60       
     61    return result
     62
     63
     64
     65
     66def _pmesh_to_domain_instance(file_name, DomainClass):
     67    """
     68    Converts a mesh file(.tsh or .msh), to a Domain instance.
     69
     70    Internal function. See public interface pmesh_to_domain_instance for details
    4771    """
    4872   
     
    5276    vertex_coordinates, vertices, tag_dict, vertex_quantity_dict \
    5377                        ,tagged_elements_dict, geo_reference = \
    54                 _pmesh_to_domain(file_name=file_name)
     78                        _pmesh_to_domain(file_name=file_name)
    5579
    5680
     
    98122    generated segment list: [(point1,point2),(p3,p4),...] (Tuples of integers)
    99123    generated segment tag list: [S1Tag, S2Tag, ...] (list of ints)
    100     triangle list:  [(point1,point2, point3),(p5,p4, p1),...] (Tuples of integers)
     124    triangle list:  [(point1, point2, point3),(p5,p4, p1),...] (Tuples of integers)
    101125    triangle neighbor list: [(triangle1,triangle2, triangle3),(t5,t4, t1),...] (Tuples of integers) -1 means there's no triangle neighbor
    102126    triangle attribute list: [T1att, T2att, ...] (list of strings)
     
    146170    sides = calc_sides(triangles)
    147171    tag_dict = {}
    148     for seg, tag in map(None,mesh_dict['segments'],
    149                            mesh_dict['segment_tags']):
     172    for seg, tag in map(None, mesh_dict['segments'],
     173                        mesh_dict['segment_tags']):
    150174        v1 = seg[0]
    151175        v2 = seg[1]
  • production/sydney_2006/project.py

    r2403 r2407  
    33"""
    44
    5 from os import sep
     5from os import sep, environ
    66from os.path import expanduser
     7from utilities.polygon import read_polygon
    78import sys
     9
     10from pmesh.create_mesh import convert_points_from_latlon_to_utm
     11               
     12
     13
    814
    915#Making assumptions about the location of scenario data
     
    1420finename = 'bathy_dem25' # get from Neil/Ingo (DEM or topo data) Wed 25 Jan
    1521
     22
     23
    1624# creating easting and northing max and min for fine data - region of interest
    1725eastingmin = 332090
     
    2937
    3038if sys.platform == 'win32':
    31     home = '..\..\..\..\..'     #Sandpit's parent dir
     39    home = environ['INUNDATIONHOME']     #Sandpit's parent dir
    3240else:   
    3341    home = expanduser('~')
     42   
    3443
    3544
     
    3847datadir = home+sep+scenario_dir_name+sep+'topographies'+sep
    3948outputdir = home+sep+scenario_dir_name+sep+'output'+sep
     49polygondir = home+sep+scenario_dir_name+sep+'polygons'+sep
    4050
    4151meshname = meshdir + basename
     
    4454combineddemname = datadir + 'sydneytopo'
    4555outputname = outputdir + basename  #Used by post processing
     56
     57#csv file of coastline 50m epsilon belt
     58manly_polygonname = polygondir + 'manly_polygon_UTM56_coarse'
     59manly_polygon = read_polygon(manly_polygonname + '.csv')
     60#print manly_polygon
     61
    4662gauge_filename = outputdir + 'sydney_gauges.xya'
    4763gauge_outname = outputdir + 'gauges_max_output.xya'
     
    5167
    5268# define clipping polygon
     69south = degminsec2decimal_degrees(-34,05,0)
     70north = degminsec2decimal_degrees(-33,33,0)
     71west = degminsec2decimal_degrees(151,1,0)
     72east = degminsec2decimal_degrees(151,30,0)
     73p0 = [south, west]
     74p1 = [south, east]
     75p2 = [north, east]
     76p3 = [north, west]
     77   
     78polygonall, zone = convert_points_from_latlon_to_utm([p0, p1, p2, p3])
     79refzone = zone
     80
     81print 'Got refzone', refzone
     82
    5383dsouth = degminsec2decimal_degrees(-34,05,0)
    5484dnorth = degminsec2decimal_degrees(-33,33,0)
     
    72102dp8 = [dnorth, dwest]
    73103   
    74 diffpolygonall = [dp0, dp1, dp2, dp3, dp4, dp5, dp6, dp7]
    75 
     104diffpolygonall, zone = convert_points_from_latlon_to_utm([dp0, dp1, dp2, dp3, dp4, dp5, dp6, dp7])
     105# to put chunk back in
     106#diffpolygonall = [dp0, dp1, dp2, dp3, dp4, dp8]
     107
     108
     109
     110#Interior regions - the Harbour - take 2
    76111#Interior regions - the Harbour
    77112harbour_1x = degminsec2decimal_degrees(-33,51,0)
     
    122157k142 = [harbour_15x, harbour_15y]
    123158
    124 harbour_polygon_2 = [k02, k112, k122, k12, k22, k62, k72, k82, k102, k42, k52] #worked
     159harbour_polygon_2, zone = convert_points_from_latlon_to_utm([k02, k112, k122, k12, k22, k62, k72, k82, k102, k42, k52]) #worked
     160assert zone == refzone
     161
    125162
    126163#Interior region - Botany Bay
     164
     165#Interior region - Botany Bay - take 2
    127166bb_1x = degminsec2decimal_degrees(-34,3,0)
    128167bb_1y = degminsec2decimal_degrees(151,2,30)
     
    157196j92 = [bb_10x, bb_10y]
    158197
    159 botanybay_polygon_2 = [j92, j12, j22, j62, j82, j72, j42] # worked
    160 
    161 # around 42km across from harbour(385000,6255000)
     198botanybay_polygon_2, zone = convert_points_from_latlon_to_utm([j92, j12, j22, j62, j82, j72, j42]) # worked
     199
     200
     201# close to botany bay opening (340000,6236000)
     202# x0 = 25964
     203# y0 = 11049
     204# around 10km from botany bay opening (350000,6236000)
     205# x0 = 35964
     206# y0 = 11049
     207# around 21km from botany bay opening (361000,6236000)
     208#x0 = 46964
     209#y0 = 11049
     210
     211# not used for sydney scenario, original interior regions listed though
     212# setting up problem area for doing just around the harbour
     213hsouth = degminsec2decimal_degrees(-33,54,0)
     214hnorth = degminsec2decimal_degrees(-33,48,0)
     215hwest = degminsec2decimal_degrees(151,0,0)
     216heast = degminsec2decimal_degrees(151,30,0)
     217
     218hp0 = [hsouth, hwest]
     219hp1 = [hsouth, heast]
     220hp2 = [hnorth, heast]
     221hp3 = [hnorth, hwest]
     222polygon_h, zone = convert_points_from_latlon_to_utm([hp0, hp1, hp2, hp3])
     223
     224#Interior regions - the Harbour - take 1
     225harbour_south = degminsec2decimal_degrees(-33,53,0)
     226harbour_north = degminsec2decimal_degrees(-33,47,0)
     227harbour_west = degminsec2decimal_degrees(151,5,0)
     228harbour_east = degminsec2decimal_degrees(151,19,0)
     229
     230#harbour_south1 = degminsec2decimal_degrees(-33,53,0)
     231#harbour_south2 = degminsec2decimal_degrees(-33,52,0)
     232#harbour_north1 = degminsec2decimal_degrees(-33,45,0)
     233#harbour_north2 = degminsec2decimal_degrees(-33,48,0)
     234#harbour_west = degminsec2decimal_degrees(151,5,0)
     235#harbour_east = degminsec2decimal_degrees(151,19,0)
     236
     237k0 = [harbour_south, harbour_west]
     238k1 = [harbour_south, harbour_east]
     239k2 = [harbour_north, harbour_east]
     240k3 = [harbour_north, harbour_west]   
     241
     242harbour_polygon, zone = convert_points_from_latlon_to_utm([k0, k1, k2, k3])
     243
     244# setting up problem area for doing just around Botany Bay
     245bsouth = degminsec2decimal_degrees(-33,56,0)
     246bnorth = degminsec2decimal_degrees(-34,3,0)
     247bwest = degminsec2decimal_degrees(151,0,0)
     248beast = degminsec2decimal_degrees(151,30,0)
     249
     250bp0 = [bsouth, bwest]
     251bp1 = [bsouth, beast]
     252bp2 = [bnorth, beast]
     253bp3 = [bnorth, bwest]
     254polygon_bb, zone = convert_points_from_latlon_to_utm([bp0, bp1, bp2, bp3])
     255
     256#Interior region - Botany Bay - take 1
     257botanybay_south = degminsec2decimal_degrees(-33,58,0)
     258botanybay_north = degminsec2decimal_degrees(-34,1,0)
     259botanybay_west = degminsec2decimal_degrees(151,5,0)
     260botanybay_east = degminsec2decimal_degrees(151,18,0)
     261
     262j0 = [botanybay_south, botanybay_west]
     263j1 = [botanybay_south, botanybay_east]
     264j2 = [botanybay_north, botanybay_east]
     265j3 = [botanybay_north, botanybay_west]   
     266
     267botanybay_polygon, zone = convert_points_from_latlon_to_utm([j0, j1, j2, j3])
     268assert zone == refzone
     269
    162270#x0 = 28964 + 42000
    163271#y0 = 30049
  • production/sydney_2006/run_sydney_smf.py

    r2400 r2407  
    66
    77The scenario is defined by a triangular mesh created from project.polygon,
    8 the elevation data and boundary data obtained from a tsunami simulation done with MOST.
     8the elevation data and a simulated submarine landslide.
    99
    10 Ole Nielsen, GA - 2005 and Adrian Hitchman and Jane Sexton, GA - 2006
     10Ole Nielsen and Duncan Gray, GA - 2005 and Adrian Hitchman and Jane Sexton, GA - 2006
    1111"""
    1212
    13 tide = 0       #Australian Height Datum (mean sea level)
    1413
     14#-------------------------------------------------------------------------------# Import necessary modules
     15#-------------------------------------------------------------------------------
     16
     17# Standard modules
    1518import os
    1619import time
    1720
     21# Related major packages
     22from pyvolution.shallow_water import Domain, Reflective_boundary
     23from pyvolution.data_manager import convert_dem_from_ascii2netcdf, dem2pts
     24from pyvolution.combine_pts import combine_rectangular_points_files
     25from pyvolution.pmesh2domain import pmesh_to_domain_instance
    1826
    19 from pyvolution.shallow_water import Domain, Reflective_boundary, File_boundary,\
    20      Dirichlet_boundary, Time_boundary, Transmissive_boundary
    21 from pyvolution.data_manager import convert_dem_from_ascii2netcdf,\
    22      dem2pts
    23 from pyvolution.pmesh2domain import pmesh_to_domain_instance
    24 from pyvolution.combine_pts import combine_rectangular_points_files
    25 from caching import cache
    26 import project
    27 from math import pi, sin
    28 from smf import slump_tsunami, slide_tsunami, Double_gaussian
    29 from pyvolution.least_squares import fit_to_mesh_file, DEFAULT_ALPHA
     27# Application specific imports
     28import project                 # Definition of file names and polygons
     29from smf import slump_tsunami  # Function for submarine mudslide
    3030
    31 # Data preparation
     31
     32#-------------------------------------------------------------------------------
     33# Preparation of topographic data
     34#
    3235# Convert ASC 2 DEM 2 PTS using source data and store result in source data
    3336# Do for coarse and fine data
    3437# Fine pts file to be clipped to area of interest
     38#-------------------------------------------------------------------------------
     39
     40# filenames
    3541coarsedemname = project.coarsedemname
    3642finedemname = project.finedemname
     
    3844
    3945# coarse data
    40 cache(convert_dem_from_ascii2netcdf, coarsedemname, {'verbose': True},
    41       dependencies = [coarsedemname + '.asc'],
    42       verbose = True)
    43       #evaluate = True)
     46convert_dem_from_ascii2netcdf(coarsedemname, use_cache=True, verbose=True)
     47dem2pts(coarsedemname, use_cache=True, verbose=True)
    4448
    45 cache(dem2pts, coarsedemname, {'verbose': True},
    46       dependencies = [coarsedemname + '.dem'],     
    47       verbose = True)
     49# fine data (clipping the points file to smaller area)
     50convert_dem_from_ascii2netcdf(finedemname, use_cache=True, verbose=True)
     51dem2pts(finedemname,
     52        easting_min=project.eastingmin,
     53        easting_max=project.eastingmax,
     54        northing_min=project.northingmin,
     55        northing_max= project.northingmax,
     56        use_cache=True,
     57        verbose=True)
    4858
    49 # fine data
    50 cache(convert_dem_from_ascii2netcdf, finedemname, {'verbose': True},
    51       dependencies = [finedemname + '.asc'],
    52       verbose = True)
    53       #evaluate = True)
    54 
    55 # clipping the fine data
    56 cache(dem2pts, finedemname, {'verbose': True,
    57       'easting_min': project.eastingmin,
    58       'easting_max': project.eastingmax,
    59       'northing_min': project.northingmin,
    60       'northing_max': project.northingmax},
    61       dependencies = [finedemname + '.dem'],
    62       #evaluate = True,
    63       verbose = True)
    6459
    6560# combining the coarse and fine data
     
    6762                                 project.coarsedemname + '.pts',
    6863                                 project.combineddemname + '.pts')
    69                                  
    70 # Create Triangular Mesh
    71 # Overall clipping polygon and interior regions defined in project.py
     64
     65
     66#-------------------------------------------------------------------------------                                 
     67# Create the triangular mesh based on overall clipping polygon with a tagged
     68# boundary and interior regions defined in project.py along with
     69# resolutions (maximal area of per triangle) for each polygon
     70#-------------------------------------------------------------------------------                                 
     71
    7272from pmesh.create_mesh import create_mesh_from_regions
    7373
    74 # for whole region
    75 interior_res = 5000 # maximal area of per triangle
     74interior_res = 5000
    7675interior_regions = [[project.harbour_polygon_2, interior_res],
    77                     [project.botanybay_polygon_2, interior_res]] 
     76                    [project.botanybay_polygon_2, interior_res]]
    7877
    79 m = cache(create_mesh_from_regions,
    80             project.diffpolygonall,
    81             {'boundary_tags': {'bottom': [0],
    82                             'right1': [1], 'right0': [2],
    83                             'right2': [3], 'top': [4], 'left1': [5],
    84                             'left2': [6], 'left3': [7]},
    85             'resolution': 100000,
    86             'filename': meshname,           
    87             'interior_regions': interior_regions},
    88             #evaluate=True,   
    89             verbose = True)
    9078
    91 # Setup domain
    92 domain = cache(pmesh_to_domain_instance, (meshname, Domain),
    93                dependencies = [meshname],                     
    94                verbose = True)
     79#FIXME: Fix caching of this one once the mesh_interface is ready
     80from caching import cache
     81_ = cache(create_mesh_from_regions,
     82          project.diffpolygonall,
     83          {'boundary_tags': {'bottom': [0],
     84                             'right1': [1], 'right0': [2],
     85                             'right2': [3], 'top': [4], 'left1': [5],
     86                             'left2': [6], 'left3': [7]},
     87           'resolution': 100000,
     88           'filename': meshname,           
     89           'interior_regions': interior_regions,
     90           'UTM': True,
     91           'refzone': project.refzone},
     92          verbose = True)
    9593
    96 # Bring in elevation data
    97 domain.set_quantity('elevation',
    98                      filename = project.combineddemname + '.pts',
    99                      use_cache = True,
    100                      verbose = True)
    101  
     94
     95#-------------------------------------------------------------------------------                                 
     96# Setup computational domain
     97#-------------------------------------------------------------------------------                                 
     98
     99domain = pmesh_to_domain_instance(meshname, Domain,
     100                                  use_cache = True,
     101                                  verbose = True)
     102
    102103print 'Number of triangles = ', len(domain)
    103104print 'The extent is ', domain.get_extent()
     
    105106domain.set_name(project.basename)
    106107domain.set_datadir(project.outputdir)
    107 domain.store = True
    108 domain.quantities_to_be_stored = ['stage', 'xmomentum', 'ymomentum']
     108domain.set_quantities_to_be_stored(['stage', 'xmomentum', 'ymomentum'])
    109109
    110 # Setup Initial conditions
    111 t = slump_tsunami(length=30000.0,
    112                   depth=400.0,
    113                   slope=6.0,
    114                   thickness=176.0,
    115                   radius=3330,
    116                   dphi=0.23,
    117                   x0=project.slump_origin[0],
    118                   y0=project.slump_origin[1],
    119                   alpha=0.0,
    120                   domain=domain)
    121 domain.set_quantity('stage', t)
     110
     111#-------------------------------------------------------------------------------
     112# Set up scenario (tsunami_source is a callable object used with set_quantity)
     113#-------------------------------------------------------------------------------
     114
     115tsunami_source = slump_tsunami(length=30000.0,
     116                               depth=400.0,
     117                               slope=6.0,
     118                               thickness=176.0,
     119                               radius=3330,
     120                               dphi=0.23,
     121                               x0=project.slump_origin[0],
     122                               y0=project.slump_origin[1],
     123                               alpha=0.0,
     124                               domain=domain)
     125
     126
     127#-------------------------------------------------------------------------------                                 
     128# Setup initial conditions
     129#-------------------------------------------------------------------------------
     130
     131domain.set_quantity('stage', tsunami_source)
    122132domain.set_quantity('friction', 0)
    123    
    124 # Setup Boundary Conditions
    125 print domain.get_boundary_tags()
     133domain.set_quantity('elevation',
     134                    filename = project.combineddemname + '.pts',
     135                    use_cache = True,
     136                    verbose = True)
     137
     138
     139#-------------------------------------------------------------------------------                                 
     140# Setup boundary conditions (all reflective)
     141#-------------------------------------------------------------------------------
     142
     143print 'Available boundary tags', domain.get_boundary_tags()
    126144
    127145Br = Reflective_boundary(domain)
    128 Bt = Transmissive_boundary(domain)
    129 Bd = Dirichlet_boundary([0,0,0])
    130 # 10 min square wave starting at 1 min, 6m high
    131 Bw = Time_boundary(domain=domain,
    132                    f=lambda t: [(6<t<606)*6, 0, 0])
    133 
    134146domain.set_boundary( {'bottom': Br, 'right1': Br, 'right0': Br,
    135147                      'right2': Br, 'top': Br, 'left1': Br,
    136148                      'left2': Br, 'left3': Br} )
    137149
    138 # Evolve
     150
     151#-------------------------------------------------------------------------------                                 
     152# Evolve system through time
     153#-------------------------------------------------------------------------------
     154
    139155import time
    140156t0 = time.time()
Note: See TracChangeset for help on using the changeset viewer.