[5194] | 1 | """Script for running tsunami inundation scenario for Dampier, 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.output_run_time_dir |
---|
| 6 | |
---|
| 7 | The scenario is defined by a triangular mesh created from project.polygon, |
---|
| 8 | the elevation data and a simulated tsunami generated with URS code. |
---|
| 9 | |
---|
| 10 | Ole Nielsen and Duncan Gray, GA - 2005 and Jane Sexton, Nick Bartzis, GA - 2006 |
---|
| 11 | """ |
---|
| 12 | |
---|
| 13 | #------------------------------------------------------------------------------ |
---|
| 14 | # Import necessary modules |
---|
| 15 | #------------------------------------------------------------------------------ |
---|
| 16 | |
---|
| 17 | # Standard modules |
---|
| 18 | from os import sep |
---|
| 19 | from os.path import dirname, basename |
---|
| 20 | from os import mkdir, access, F_OK |
---|
| 21 | from shutil import copy |
---|
| 22 | import time |
---|
| 23 | import sys |
---|
[5212] | 24 | from math import radians |
---|
[5194] | 25 | |
---|
| 26 | # Related major packages |
---|
| 27 | from anuga.shallow_water import Time_boundary |
---|
| 28 | from anuga.shallow_water import Domain |
---|
| 29 | from anuga.shallow_water import Dirichlet_boundary |
---|
| 30 | from anuga.shallow_water import Transmissive_boundary |
---|
| 31 | from anuga.shallow_water import File_boundary |
---|
| 32 | from anuga.shallow_water import Reflective_boundary |
---|
| 33 | from anuga.shallow_water import Field_boundary |
---|
| 34 | from Numeric import allclose |
---|
| 35 | from anuga.shallow_water.data_manager import export_grid |
---|
| 36 | from anuga.shallow_water.data_manager import convert_dem_from_ascii2netcdf, dem2pts |
---|
| 37 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
| 38 | from anuga.shallow_water.data_manager import start_screen_catcher, copy_code_files,store_parameters |
---|
| 39 | from anuga_parallel.parallel_api import distribute, numprocs, myid, barrier |
---|
| 40 | from anuga_parallel.parallel_abstraction import get_processor_name |
---|
| 41 | from anuga.caching import myhash |
---|
| 42 | from anuga.damage_modelling.inundation_damage import add_depth_and_momentum2csv, inundation_damage |
---|
| 43 | from anuga.fit_interpolate.benchmark_least_squares import mem_usage |
---|
| 44 | |
---|
| 45 | # Application specific imports |
---|
| 46 | import project_fangauta # Definition of file names and polygons |
---|
| 47 | |
---|
| 48 | def run_model(**kwargs): |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | #------------------------------------------------------------------------------ |
---|
| 52 | # Copy scripts to time stamped output directory and capture screen |
---|
| 53 | # output to file |
---|
| 54 | #------------------------------------------------------------------------------ |
---|
| 55 | print "Processor Name:",get_processor_name() |
---|
| 56 | |
---|
| 57 | #copy script must be before screen_catcher |
---|
| 58 | #print kwargs |
---|
| 59 | |
---|
| 60 | print 'output_dir',kwargs['output_dir'] |
---|
| 61 | if myid == 0: |
---|
| 62 | copy_code_files(kwargs['output_dir'],__file__, |
---|
| 63 | dirname(project_fangauta.__file__)+sep+ project_fangauta.__name__+'.py' ) |
---|
| 64 | |
---|
| 65 | store_parameters(**kwargs) |
---|
| 66 | |
---|
| 67 | barrier() |
---|
| 68 | |
---|
| 69 | start_screen_catcher(kwargs['output_dir'], myid, numprocs) |
---|
| 70 | |
---|
| 71 | print "Processor Name:",get_processor_name() |
---|
| 72 | |
---|
| 73 | # filenames |
---|
| 74 | # meshes_dir_name = project.meshes_dir_name+'.msh' |
---|
| 75 | |
---|
| 76 | # creates copy of code in output dir |
---|
| 77 | print 'min triangles', project_fangauta.trigs_min, |
---|
| 78 | print 'Note: This is generally about 20% less than the final amount' |
---|
| 79 | |
---|
| 80 | #-------------------------------------------------------------------------- |
---|
| 81 | # Create the triangular mesh based on overall clipping polygon with a |
---|
| 82 | # tagged |
---|
| 83 | # boundary and interior regions defined in project.py along with |
---|
| 84 | # resolutions (maximal area of per triangle) for each polygon |
---|
| 85 | #-------------------------------------------------------------------------- |
---|
| 86 | |
---|
| 87 | if myid == 0: |
---|
| 88 | |
---|
| 89 | print 'start create mesh from regions' |
---|
| 90 | |
---|
| 91 | create_mesh_from_regions(project_fangauta.poly_all, |
---|
| 92 | boundary_tags=project_fangauta.boundary_tags, |
---|
| 93 | maximum_triangle_area=project_fangauta.res_poly_all, |
---|
| 94 | filename=project_fangauta.meshes_dir_name+'.msh', |
---|
| 95 | use_cache=False, |
---|
| 96 | verbose=True) |
---|
| 97 | barrier() |
---|
| 98 | |
---|
| 99 | scenario='fixed_wave' |
---|
| 100 | |
---|
| 101 | #------------------------------------------------------------------------- |
---|
| 102 | # Setup computational domain |
---|
| 103 | #------------------------------------------------------------------------- |
---|
| 104 | print 'Setup computational domain' |
---|
| 105 | |
---|
| 106 | #domain = cache(Domain, (meshes_dir_name), {'use_cache':True, 'verbose':True}, verbose=True) |
---|
| 107 | #above don't work |
---|
| 108 | domain = Domain(project_fangauta.meshes_dir_name+'.msh', use_cache=False, verbose=True) |
---|
| 109 | print 'memory usage before del domain',mem_usage() |
---|
| 110 | |
---|
| 111 | print domain.statistics() |
---|
| 112 | print 'triangles',len(domain) |
---|
| 113 | |
---|
| 114 | kwargs['act_num_trigs']=len(domain) |
---|
| 115 | |
---|
| 116 | #------------------------------------------------------------------------- |
---|
| 117 | # Setup initial conditions |
---|
| 118 | #------------------------------------------------------------------------- |
---|
| 119 | if myid == 0: |
---|
| 120 | |
---|
| 121 | print 'Setup initial conditions' |
---|
| 122 | |
---|
| 123 | from polygon import Polygon_function |
---|
| 124 | #following sets the stage/water to be offcoast only |
---|
| 125 | # IC = Polygon_function( [(project.poly_mainland, -1.0)], default = kwargs['tide'], |
---|
| 126 | # geo_reference = domain.geo_reference) |
---|
| 127 | # domain.set_quantity('stage', IC) |
---|
| 128 | domain.set_quantity('stage',kwargs['tide'] ) |
---|
| 129 | # domain.set_quantity('stage', kwargs['tide']) |
---|
| 130 | domain.set_quantity('friction', kwargs['friction']) |
---|
| 131 | |
---|
| 132 | print 'Start Set quantity',kwargs['bathy_file'] |
---|
| 133 | |
---|
| 134 | domain.set_quantity('elevation', |
---|
| 135 | filename = kwargs['bathy_file'], |
---|
| 136 | use_cache = False, |
---|
| 137 | verbose = True, |
---|
| 138 | alpha = kwargs['alpha']) |
---|
| 139 | print 'Finished Set quantity' |
---|
| 140 | barrier() |
---|
| 141 | |
---|
| 142 | #------------------------------------------------------ |
---|
| 143 | # Distribute domain to implement parallelism !!! |
---|
| 144 | #------------------------------------------------------ |
---|
| 145 | |
---|
| 146 | if numprocs > 1: |
---|
| 147 | domain=distribute(domain) |
---|
| 148 | |
---|
| 149 | #------------------------------------------------------ |
---|
| 150 | # Set domain parameters |
---|
| 151 | #------------------------------------------------------ |
---|
| 152 | print 'domain id', id(domain) |
---|
| 153 | domain.set_name(kwargs['aa_scenario_name']) |
---|
| 154 | domain.set_datadir(kwargs['output_dir']) |
---|
| 155 | domain.set_default_order(2) # Apply second order scheme |
---|
| 156 | domain.set_minimum_storable_height(0.01) # Don't store anything less than 1cm |
---|
| 157 | domain.set_store_vertices_uniquely(False) |
---|
| 158 | domain.set_quantities_to_be_stored(['stage', 'xmomentum', 'ymomentum']) |
---|
| 159 | domain.tight_slope_limiters = 1 |
---|
| 160 | #domain.set_maximum_allowed_speed(0.1) # Allow a little runoff (0.1 is OK) |
---|
| 161 | print 'domain id', id(domain) |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | #------------------------------------------------------------------------- |
---|
| 165 | # Setup boundary conditions |
---|
| 166 | #------------------------------------------------------------------------- |
---|
| 167 | print 'Available boundary tags', domain.get_boundary_tags() |
---|
| 168 | print 'domain id', id(domain) |
---|
| 169 | #print 'Reading Boundary file',project.boundaries_dir_namea + '.sww' |
---|
| 170 | Bt = Transmissive_boundary(domain) |
---|
| 171 | Br = Reflective_boundary(domain) |
---|
| 172 | Bd = Dirichlet_boundary([kwargs['tide'],0,0]) |
---|
| 173 | Bo = Dirichlet_boundary([kwargs['tide']+5.0,0,0]) |
---|
| 174 | |
---|
| 175 | from math import sin, pi |
---|
| 176 | Bw = Time_boundary(domain = domain, |
---|
[5212] | 177 | f=lambda t: [0.5*sin(t*radians(2*pi/60)), 0, 0]) |
---|
[5194] | 178 | |
---|
| 179 | domain.set_boundary({'land':Bt,'mouth':Bw}) |
---|
| 180 | |
---|
| 181 | #---------------------------------------------------------------------------- |
---|
| 182 | # Evolve system through time |
---|
| 183 | #-------------------------------------------------------------------- |
---|
| 184 | import time |
---|
| 185 | t0 = time.time() |
---|
| 186 | |
---|
| 187 | for t in domain.evolve(yieldstep=10, finaltime = 10800): |
---|
| 188 | domain.write_time() |
---|
| 189 | domain.write_boundary_statistics(tags = 'mouth') |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | for t in domain.evolve(10,finaltime=21600,skip_initial_step=True): |
---|
| 193 | |
---|
| 194 | Bw = Time_boundary(domain = domain, |
---|
[5212] | 195 | f=lambda t: [0.5*sin(t*radians(2*pi/180)), 0, 0]) |
---|
[5194] | 196 | domain.set_boundary({'land':Bt,'mouth':Bw}) |
---|
| 197 | domain.write_time() |
---|
| 198 | domain.write_boundary_statistics(tags = 'mouth') |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | for t in domain.evolve(10,finaltime=32400,skip_initial_step=True): |
---|
| 202 | Bw = Time_boundary(domain = domain, |
---|
[5212] | 203 | f=lambda t: [0.5*sin(t*radians(2*pi/300)), 0, 0]) |
---|
[5194] | 204 | domain.set_boundary({'land':Bt,'mouth':Bw}) |
---|
| 205 | domain.write_time() |
---|
| 206 | domain.write_boundary_statistics(tags = 'mouth') |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | for t in domain.evolve(10,finaltime=43200,skip_initial_step=True): |
---|
| 210 | Bw = Time_boundary(domain = domain, |
---|
[5212] | 211 | f=lambda t: [0.5*sin(t*radians(2*pi/420)), 0, 0]) |
---|
[5194] | 212 | domain.set_boundary({'land':Bt,'mouth':Bw}) |
---|
| 213 | domain.write_time() |
---|
| 214 | domain.write_boundary_statistics(tags = 'mouth') |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | for t in domain.evolve(10,finaltime=54001,skip_initial_step=True): |
---|
| 218 | Bw = Time_boundary(domain = domain, |
---|
[5212] | 219 | f=lambda t: [0.5*sin(t*radians(2*pi/540)), 0, 0]) |
---|
[5194] | 220 | domain.set_boundary({'land':Bt,'mouth':Bw}) |
---|
| 221 | domain.write_time() |
---|
| 222 | domain.write_boundary_statistics(tags = 'mouth') |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | for t in domain.evolve(10,finaltime=64800,skip_initial_step=True): |
---|
| 226 | Bw = Time_boundary(domain = domain, |
---|
[5212] | 227 | f=lambda t: [0.5*sin(t*radians(2*pi/660)), 0, 0]) |
---|
[5194] | 228 | domain.set_boundary({'land':Bt,'mouth':Bw}) |
---|
| 229 | domain.write_time() |
---|
| 230 | domain.write_boundary_statistics(tags = 'mouth') |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | for t in domain.evolve(10,finaltime=75600,skip_initial_step=True): |
---|
| 234 | Bw = Time_boundary(domain = domain, |
---|
[5212] | 235 | f=lambda t: [0.5*sin(t*radians(2*pi/780)), 0, 0]) |
---|
[5194] | 236 | domain.set_boundary({'land':Bt,'mouth':Bw}) |
---|
| 237 | domain.write_time() |
---|
| 238 | domain.write_boundary_statistics(tags = 'mouth') |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | for t in domain.evolve(10,finaltime=86400,skip_initial_step=True): |
---|
| 242 | Bw = Time_boundary(domain = domain, |
---|
[5212] | 243 | f=lambda t: [0.5*sin(t*radians(2*pi/900)), 0, 0]) |
---|
[5194] | 244 | domain.set_boundary({'land':Bt,'mouth':Bw}) |
---|
| 245 | domain.write_time() |
---|
| 246 | domain.write_boundary_statistics(tags = 'mouth') |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | for t in domain.evolve(10,finaltime=97200,skip_initial_step=True): |
---|
| 250 | Bw = Time_boundary(domain = domain, |
---|
[5212] | 251 | f=lambda t: [0.5*sin(t*radians(2*pi/1020)), 0, 0]) |
---|
[5194] | 252 | domain.set_boundary({'land':Bt,'mouth':Bw}) |
---|
| 253 | domain.write_time() |
---|
| 254 | domain.write_boundary_statistics(tags = 'mouth') |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | for t in domain.evolve(10,finaltime=108000,skip_initial_step=True): |
---|
| 258 | Bw = Time_boundary(domain = domain, |
---|
[5212] | 259 | f=lambda t: [0.5*sin(t*radians(2*pi/1140)), 0, 0]) |
---|
[5194] | 260 | domain.set_boundary({'land':Bt,'mouth':Bw}) |
---|
| 261 | domain.write_time() |
---|
| 262 | domain.write_boundary_statistics(tags = 'mouth') |
---|
| 263 | |
---|
| 264 | for t in domain.evolve(10,finaltime=11800,skip_initial_step=True): |
---|
| 265 | Bw = Time_boundary(domain = domain, |
---|
[5212] | 266 | f=lambda t: [0.5*sin(t*radians(2*pi/1260)), 0, 0]) |
---|
[5194] | 267 | domain.set_boundary({'land':Bt,'mouth':Bw}) |
---|
| 268 | domain.write_time() |
---|
| 269 | domain.write_boundary_statistics(tags = 'mouth') |
---|
| 270 | |
---|
| 271 | x, y = domain.get_maximum_inundation_location() |
---|
| 272 | q = domain.get_maximum_inundation_elevation() |
---|
| 273 | |
---|
| 274 | print 'Maximum runup observed at (%.2f, %.2f) with elevation %.2f' %(x,y,q) |
---|
| 275 | |
---|
| 276 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
| 277 | |
---|
| 278 | #kwargs 'completed' must be added to write the final parameters to file |
---|
| 279 | kwargs['completed']=str(time.time()-t0) |
---|
| 280 | |
---|
| 281 | if myid==0: |
---|
| 282 | store_parameters(**kwargs) |
---|
| 283 | barrier |
---|
| 284 | |
---|
| 285 | print 'memory usage before del domain1',mem_usage() |
---|
| 286 | |
---|
| 287 | def export_model(**kwargs): |
---|
| 288 | #store_parameters(**kwargs) |
---|
| 289 | |
---|
| 290 | # print 'memory usage before del domain',mem_usage() |
---|
| 291 | #del domain |
---|
| 292 | print 'memory usage after del domain',mem_usage() |
---|
| 293 | |
---|
| 294 | swwfile = kwargs['output_dir']+kwargs['aa_scenario_name'] |
---|
| 295 | print'swwfile',swwfile |
---|
| 296 | |
---|
| 297 | export_grid(swwfile, extra_name_out = 'town', |
---|
| 298 | quantities = ['speed','depth','elevation','stage'], # '(xmomentum**2 + ymomentum**2)**0.5' defaults to elevation |
---|
| 299 | #quantities = ['speed','depth'], # '(xmomentum**2 + ymomentum**2)**0.5' defaults to elevation |
---|
| 300 | timestep = None, |
---|
| 301 | reduction = max, |
---|
| 302 | cellsize = kwargs['export_cellsize'], |
---|
| 303 | NODATA_value = -1E-030, |
---|
| 304 | easting_min = project_fangauta.eastingmin, |
---|
| 305 | easting_max = project_fangauta.eastingmax, |
---|
| 306 | northing_min = project_fangauta.northingmin, |
---|
| 307 | northing_max = project_fangauta.northingmax, |
---|
| 308 | verbose = False, |
---|
| 309 | origin = None, |
---|
| 310 | datum = 'WGS84', |
---|
| 311 | format = 'asc') |
---|
| 312 | |
---|
| 313 | #------------------------------------------------------------- |
---|
| 314 | if __name__ == "__main__": |
---|
| 315 | |
---|
| 316 | kwargs={} |
---|
| 317 | kwargs['est_num_trigs']=project_fangauta.trigs_min |
---|
| 318 | kwargs['num_cpu']=numprocs |
---|
| 319 | kwargs['host']=project_fangauta.host |
---|
| 320 | kwargs['res_factor']=project_fangauta.res_factor |
---|
| 321 | kwargs['starttime']=project_fangauta.starttime |
---|
| 322 | kwargs['yieldstep']=project_fangauta.yieldstep |
---|
| 323 | kwargs['midtime']=project_fangauta.midtime |
---|
| 324 | kwargs['finaltime']=project_fangauta.finaltime |
---|
| 325 | kwargs['output_dir']=project_fangauta.output_run_time_dir |
---|
| 326 | kwargs['bathy_file']=project_fangauta.combined_dir_name+'.txt' |
---|
| 327 | # kwargs['bathy_file']=project.combined_small_dir_name + '.pts' |
---|
| 328 | kwargs['boundary_file']=project_fangauta.boundaries_in_dir_name + '.sww' |
---|
| 329 | kwargs['file_name']=project_fangauta.home+'detail.csv' |
---|
| 330 | kwargs['aa_scenario_name']=project_fangauta.scenario_name |
---|
| 331 | kwargs['ab_time']=project_fangauta.time |
---|
| 332 | kwargs['res_factor']= project_fangauta.res_factor |
---|
| 333 | kwargs['tide']=project_fangauta.tide |
---|
| 334 | kwargs['user']=project_fangauta.user |
---|
| 335 | kwargs['alpha'] = project_fangauta.alpha |
---|
| 336 | kwargs['friction']=project_fangauta.friction |
---|
| 337 | kwargs['time_thinning'] = project_fangauta.time_thinning |
---|
| 338 | kwargs['dir_comment']=project_fangauta.dir_comment |
---|
| 339 | kwargs['export_cellsize']=project_fangauta.export_cellsize |
---|
| 340 | |
---|
| 341 | run_model(**kwargs) |
---|
| 342 | |
---|
| 343 | if myid==0: |
---|
| 344 | export_model(**kwargs) |
---|
| 345 | barrier |
---|