[2848] | 1 | """Script for running a tsunami inundation scenario for Onslow, WA, Australia. |
---|
| 2 | |
---|
| 3 | Source data such as elevation and boundary data is assumed to be available in |
---|
| 4 | directories specified by project.py |
---|
| 5 | The output sww file is stored in project.outputtimedir |
---|
| 6 | |
---|
| 7 | The scenario is defined by a triangular mesh created from project.polygon, |
---|
| 8 | the elevation data and a simulated submarine landslide. |
---|
| 9 | |
---|
| 10 | Ole Nielsen and Duncan Gray, GA - 2005 and Nick Bartzis, GA - 2006 |
---|
| 11 | """ |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | #-------------------------------------------------------------------------------# Import necessary modules |
---|
| 15 | #------------------------------------------------------------------------------- |
---|
| 16 | |
---|
| 17 | # Standard modules |
---|
[2902] | 18 | import os |
---|
| 19 | |
---|
| 20 | from os import sep |
---|
| 21 | from os.path import dirname, basename |
---|
| 22 | |
---|
[2848] | 23 | import time |
---|
| 24 | |
---|
| 25 | # Related major packages |
---|
| 26 | from pyvolution.shallow_water import Domain, Reflective_boundary, \ |
---|
| 27 | Dirichlet_boundary, Time_boundary, File_boundary |
---|
| 28 | from pyvolution.data_manager import convert_dem_from_ascii2netcdf, dem2pts |
---|
| 29 | from pyvolution.combine_pts import combine_rectangular_points_files |
---|
| 30 | from pyvolution.pmesh2domain import pmesh_to_domain_instance |
---|
| 31 | from geospatial_data import add_points_files |
---|
| 32 | |
---|
| 33 | # Application specific imports |
---|
| 34 | import project # Definition of file names and polygons |
---|
| 35 | from smf import slump_tsunami # Function for submarine mudslide |
---|
| 36 | |
---|
| 37 | from shutil import copy |
---|
| 38 | from os import mkdir, access, F_OK |
---|
| 39 | |
---|
| 40 | from geospatial_data import * |
---|
| 41 | import sys |
---|
| 42 | from pyvolution.util import Screen_Catcher |
---|
| 43 | |
---|
| 44 | #------------------------------------------------------------------------------- |
---|
| 45 | # Preparation of topographic data |
---|
| 46 | # |
---|
| 47 | # Convert ASC 2 DEM 2 PTS using source data and store result in source data |
---|
| 48 | # Do for coarse and fine data |
---|
| 49 | # Fine pts file to be clipped to area of interest |
---|
| 50 | #------------------------------------------------------------------------------- |
---|
| 51 | |
---|
| 52 | # filenames |
---|
[2902] | 53 | print 'run home', project.home |
---|
[2848] | 54 | onshore_dem_name = project.onshore_dem_name |
---|
| 55 | offshore_points1 = project.offshore_dem_name1 |
---|
| 56 | offshore_points2 = project.offshore_dem_name2 |
---|
| 57 | meshname = project.meshname+'.msh' |
---|
| 58 | source_dir = project.boundarydir |
---|
| 59 | |
---|
| 60 | |
---|
[2902] | 61 | |
---|
| 62 | |
---|
| 63 | #import sys; sys.exit() |
---|
| 64 | |
---|
[2848] | 65 | # creates copy of code in output dir if dir doesn't exist |
---|
| 66 | if access(project.outputtimedir,F_OK) == 0 : |
---|
| 67 | mkdir (project.outputtimedir) |
---|
[2902] | 68 | copy (dirname(project.__file__) +sep+ project.__name__+'.py', project.outputtimedir + project.__name__+'.py') |
---|
| 69 | copy (__file__, project.outputtimedir + basename(__file__)) |
---|
[2848] | 70 | |
---|
[2902] | 71 | |
---|
[2848] | 72 | #normal screen output is stored in |
---|
| 73 | screen_output_name = project.outputtimedir + "screen_output.txt" |
---|
| 74 | screen_error_name = project.outputtimedir + "screen_error.txt" |
---|
| 75 | |
---|
| 76 | #used to catch screen output to file |
---|
| 77 | sys.stdout = Screen_Catcher(screen_output_name) |
---|
[2902] | 78 | sys.stderr = Screen_Catcher(screen_error_name) |
---|
[2848] | 79 | |
---|
[2902] | 80 | |
---|
[2848] | 81 | ''' |
---|
| 82 | # fine data (clipping the points file to smaller area) |
---|
| 83 | # creates DEM from asc data |
---|
| 84 | convert_dem_from_ascii2netcdf(onshore_dem_name, use_cache=True, verbose=True) |
---|
| 85 | |
---|
| 86 | #creates pts file from DEM |
---|
| 87 | dem2pts(onshore_dem_name, |
---|
| 88 | easting_min=project.eastingmin, |
---|
| 89 | easting_max=project.eastingmax, |
---|
| 90 | northing_min=project.northingmin, |
---|
| 91 | northing_max= project.northingmax, |
---|
| 92 | use_cache=True, |
---|
| 93 | verbose=True) |
---|
| 94 | |
---|
| 95 | print'create G1' |
---|
| 96 | G1 = Geospatial_data(file_name = project.offshore_dem_name1 + '.xya') |
---|
| 97 | |
---|
| 98 | print'create G2' |
---|
| 99 | G2 = Geospatial_data(file_name = project.offshore_dem_name2 + '.xya') |
---|
| 100 | |
---|
| 101 | print'create G3' |
---|
| 102 | G3 = Geospatial_data(file_name = project.onshore_dem_name + '.pts') |
---|
| 103 | |
---|
| 104 | print'add G1+G2+G3' |
---|
| 105 | G = G1 + G2 + G3 |
---|
| 106 | |
---|
| 107 | print'export G' |
---|
| 108 | G.export_points_file(project.combined_dem_name + '.pts') |
---|
| 109 | |
---|
[2902] | 110 | ''' |
---|
[2848] | 111 | #------------------------------------------------------------------------------- |
---|
| 112 | # Create the triangular mesh based on overall clipping polygon with a tagged |
---|
| 113 | # boundary and interior regions defined in project.py along with |
---|
| 114 | # resolutions (maximal area of per triangle) for each polygon |
---|
| 115 | #------------------------------------------------------------------------------- |
---|
| 116 | |
---|
| 117 | from pmesh.mesh_interface import create_mesh_from_regions |
---|
| 118 | |
---|
[2902] | 119 | region_res = 50000 |
---|
[2848] | 120 | coast_res = 2500 |
---|
[2902] | 121 | pt_hedland_res = 5000 |
---|
[2848] | 122 | # derive poly_coast from project.coast_name using alpha_shape |
---|
[2902] | 123 | interior_regions = [[project.poly_pt_hedland, pt_hedland_res], |
---|
| 124 | [project.poly_region, region_res]] |
---|
[2848] | 125 | |
---|
| 126 | print 'number of interior regions', len(interior_regions) |
---|
| 127 | |
---|
[2913] | 128 | from utilities.polygon import inside_polygon, plot_polygons |
---|
[2903] | 129 | |
---|
| 130 | bounding_polygon = project.polyAll |
---|
| 131 | count = 0 |
---|
| 132 | for i in range(len(interior_regions)): |
---|
| 133 | region = interior_regions[i] |
---|
| 134 | interior_polygon = region[0] |
---|
| 135 | if len(inside_polygon(interior_polygon, bounding_polygon, |
---|
| 136 | closed = True, verbose = False)) <> len(interior_polygon): |
---|
| 137 | print 'WARNING: interior polygon %d is outside bounding polygon' %(i) |
---|
| 138 | count += 1 |
---|
| 139 | |
---|
[2908] | 140 | figname = 'pt_hedland_polys' |
---|
[2913] | 141 | plot_polygons([project.polyAll,project.poly_pt_hedland,project.poly_region], |
---|
[2908] | 142 | figname, |
---|
[2904] | 143 | verbose = True) |
---|
| 144 | |
---|
[2903] | 145 | if count == 0: |
---|
| 146 | print 'interior regions OK' |
---|
| 147 | else: |
---|
| 148 | print 'check out your interior polygons' |
---|
[2908] | 149 | print 'check %s in production directory' %figname |
---|
[2903] | 150 | import sys; sys.exit() |
---|
| 151 | |
---|
| 152 | |
---|
[2848] | 153 | from caching import cache |
---|
| 154 | _ = cache(create_mesh_from_regions, |
---|
| 155 | project.polyAll, |
---|
[2902] | 156 | # {'boundary_tags': {'right': [0], 'bottomright': [1], |
---|
| 157 | # 'bottomleft': [2], 'left': [3], 'top': [4]}, |
---|
| 158 | {'boundary_tags': {'top': [0], 'left': [1], |
---|
| 159 | 'bottomleft': [2], 'bottomright': [3], 'right': [4]}, |
---|
| 160 | 'maximum_triangle_area': 400000, |
---|
[2848] | 161 | 'filename': meshname, |
---|
| 162 | 'interior_regions': interior_regions}, |
---|
| 163 | verbose = True) |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | #------------------------------------------------------------------------------- |
---|
| 167 | # Setup computational domain |
---|
| 168 | #------------------------------------------------------------------------------- |
---|
[2902] | 169 | ''' |
---|
[2848] | 170 | domain = pmesh_to_domain_instance(meshname, Domain, |
---|
| 171 | use_cache = False, |
---|
| 172 | verbose = True) |
---|
[2902] | 173 | ''' |
---|
[2848] | 174 | |
---|
[2902] | 175 | domain = Domain(meshname, use_cache = False, verbose = True) |
---|
| 176 | |
---|
[2848] | 177 | print 'Number of triangles = ', len(domain) |
---|
| 178 | print 'The extent is ', domain.get_extent() |
---|
| 179 | print domain.statistics() |
---|
| 180 | |
---|
| 181 | domain.set_name(project.basename) |
---|
| 182 | domain.set_datadir(project.outputtimedir) |
---|
| 183 | domain.set_quantities_to_be_stored(['stage', 'xmomentum', 'ymomentum']) |
---|
| 184 | |
---|
| 185 | #------------------------------------------------------------------------------- |
---|
| 186 | # Setup initial conditions |
---|
| 187 | #------------------------------------------------------------------------------- |
---|
| 188 | |
---|
| 189 | tide = 0. |
---|
| 190 | |
---|
| 191 | domain.set_quantity('stage', tide) |
---|
| 192 | domain.set_quantity('friction', 0.0) |
---|
| 193 | print 'hi and file',project.combined_dem_name + '.pts' |
---|
| 194 | |
---|
| 195 | domain.set_quantity('elevation', |
---|
| 196 | filename = project.combined_dem_name + '.pts', |
---|
| 197 | use_cache = True, |
---|
| 198 | verbose = True, |
---|
| 199 | alpha = 0.1 |
---|
| 200 | ) |
---|
| 201 | |
---|
| 202 | print 'hi1' |
---|
[2902] | 203 | #print 'have sent quantities OK - now exiting' |
---|
| 204 | #import sys; sys.exit() |
---|
[2848] | 205 | |
---|
| 206 | #------------------------------------------------------------------------------- |
---|
| 207 | # Setup boundary conditions (all reflective) |
---|
| 208 | #------------------------------------------------------------------------------- |
---|
| 209 | print 'start ferret2sww' |
---|
[2902] | 210 | # skipped as results in file SU-AU_clipped is correct for all WA |
---|
| 211 | |
---|
[2848] | 212 | from pyvolution.data_manager import ferret2sww |
---|
| 213 | |
---|
| 214 | south = project.south |
---|
| 215 | north = project.north |
---|
| 216 | west = project.west |
---|
| 217 | east = project.east |
---|
| 218 | |
---|
| 219 | #note only need to do when an SWW file for the MOST boundary doesn't exist |
---|
| 220 | cache(ferret2sww, |
---|
| 221 | (source_dir + project.boundary_basename, |
---|
| 222 | source_dir + project.boundary_basename), |
---|
| 223 | {'verbose': True, |
---|
| 224 | 'minlat': south, |
---|
| 225 | 'maxlat': north, |
---|
| 226 | 'minlon': west, |
---|
| 227 | 'maxlon': east, |
---|
| 228 | # 'origin': project.mesh_origin, |
---|
| 229 | 'origin': domain.geo_reference.get_origin(), |
---|
| 230 | 'mean_stage': tide, |
---|
| 231 | 'zscale': 1, #Enhance tsunami |
---|
| 232 | 'fail_on_NaN': False, |
---|
| 233 | 'inverted_bathymetry': True}, |
---|
| 234 | #evaluate = True, |
---|
| 235 | verbose = True) |
---|
| 236 | |
---|
| 237 | |
---|
| 238 | print 'Available boundary tags', domain.get_boundary_tags() |
---|
| 239 | |
---|
| 240 | Bf = File_boundary(source_dir + project.boundary_basename + '.sww', |
---|
| 241 | domain, verbose = True) |
---|
| 242 | Br = Reflective_boundary(domain) |
---|
| 243 | Bd = Dirichlet_boundary([tide,0,0]) |
---|
| 244 | |
---|
| 245 | # 7 min square wave starting at 1 min, 6m high |
---|
| 246 | Bw = Time_boundary(domain = domain, |
---|
| 247 | f=lambda t: [(60<t<480)*6, 0, 0]) |
---|
| 248 | |
---|
[2902] | 249 | #domain.set_boundary( {'right': Br, 'bottomright': Br, |
---|
| 250 | # 'bottomleft': Br, 'left': Br, 'top': Bf} ) |
---|
| 251 | domain.set_boundary( {'top': Bf, 'left': Br, 'bottomleft': Br, 'bottomright': Br, 'right': Br} ) |
---|
[2848] | 252 | |
---|
| 253 | #------------------------------------------------------------------------------- |
---|
| 254 | # Evolve system through time |
---|
| 255 | #------------------------------------------------------------------------------- |
---|
| 256 | import time |
---|
| 257 | t0 = time.time() |
---|
| 258 | |
---|
[2902] | 259 | for t in domain.evolve(yieldstep = 240, finaltime = 7200): |
---|
[2848] | 260 | domain.write_time() |
---|
| 261 | domain.write_boundary_statistics(tags = 'top') |
---|
| 262 | |
---|
[2902] | 263 | for t in domain.evolve(yieldstep = 240, finaltime = 12600 |
---|
[2848] | 264 | ,skip_initial_step = True): |
---|
| 265 | domain.write_time() |
---|
| 266 | domain.write_boundary_statistics(tags = 'top') |
---|
| 267 | |
---|
[2902] | 268 | for t in domain.evolve(yieldstep = 240, finaltime = 19800 |
---|
[2848] | 269 | ,skip_initial_step = True): |
---|
| 270 | domain.write_time() |
---|
| 271 | domain.write_boundary_statistics(tags = 'top') |
---|
| 272 | |
---|
[2902] | 273 | for t in domain.evolve(yieldstep = 240, finaltime = 25200 |
---|
[2848] | 274 | ,skip_initial_step = True): |
---|
| 275 | domain.write_time() |
---|
| 276 | domain.write_boundary_statistics(tags = 'top') |
---|
| 277 | |
---|
[2902] | 278 | for t in domain.evolve(yieldstep = 240, finaltime = 36000 |
---|
[2848] | 279 | ,skip_initial_step = True): |
---|
| 280 | domain.write_time() |
---|
| 281 | domain.write_boundary_statistics(tags = 'top') |
---|
| 282 | |
---|
| 283 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
| 284 | |
---|
| 285 | print 'finished' |
---|