[5000] | 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 |
---|
[5578] | 20 | import os |
---|
[5000] | 21 | from os import mkdir, access, F_OK |
---|
| 22 | from shutil import copy |
---|
| 23 | import time |
---|
| 24 | import sys |
---|
| 25 | |
---|
| 26 | # Related major packages |
---|
| 27 | from anuga.shallow_water import Domain |
---|
| 28 | from anuga.shallow_water import Dirichlet_boundary |
---|
| 29 | from anuga.shallow_water import File_boundary |
---|
| 30 | from anuga.shallow_water import Reflective_boundary |
---|
| 31 | from anuga.shallow_water import Field_boundary |
---|
| 32 | from Numeric import allclose |
---|
[5578] | 33 | from anuga.shallow_water.data_manager import export_grid, create_sts_boundary |
---|
[5000] | 34 | |
---|
| 35 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
| 36 | from anuga.shallow_water.data_manager import start_screen_catcher, copy_code_files,store_parameters |
---|
[5480] | 37 | #from anuga_parallel.parallel_api import distribute, numprocs, myid, barrier |
---|
[5000] | 38 | from anuga_parallel.parallel_abstraction import get_processor_name |
---|
| 39 | from anuga.caching import myhash |
---|
| 40 | from anuga.damage_modelling.inundation_damage import add_depth_and_momentum2csv, inundation_damage |
---|
| 41 | from anuga.fit_interpolate.benchmark_least_squares import mem_usage |
---|
[5575] | 42 | from anuga.utilities.polygon import read_polygon, plot_polygons, polygon_area, is_inside_polygon |
---|
[5609] | 43 | from anuga.geospatial_data.geospatial_data import find_optimal_smoothing_parameter |
---|
[5669] | 44 | |
---|
[5000] | 45 | # Application specific imports |
---|
| 46 | import project # Definition of file names and polygons |
---|
| 47 | |
---|
[5480] | 48 | numprocs = 1 |
---|
| 49 | myid = 0 |
---|
| 50 | |
---|
[5000] | 51 | def run_model(**kwargs): |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | #------------------------------------------------------------------------------ |
---|
| 55 | # Copy scripts to time stamped output directory and capture screen |
---|
| 56 | # output to file |
---|
| 57 | #------------------------------------------------------------------------------ |
---|
| 58 | print "Processor Name:",get_processor_name() |
---|
| 59 | |
---|
| 60 | #copy script must be before screen_catcher |
---|
| 61 | #print kwargs |
---|
| 62 | |
---|
| 63 | print 'output_dir',kwargs['output_dir'] |
---|
| 64 | if myid == 0: |
---|
| 65 | copy_code_files(kwargs['output_dir'],__file__, |
---|
| 66 | dirname(project.__file__)+sep+ project.__name__+'.py' ) |
---|
| 67 | |
---|
| 68 | store_parameters(**kwargs) |
---|
| 69 | |
---|
[5480] | 70 | #barrier() |
---|
[5000] | 71 | |
---|
| 72 | start_screen_catcher(kwargs['output_dir'], myid, numprocs) |
---|
| 73 | |
---|
| 74 | print "Processor Name:",get_processor_name() |
---|
| 75 | |
---|
[5575] | 76 | #----------------------------------------------------------------------- |
---|
| 77 | # Domain definitions |
---|
| 78 | #----------------------------------------------------------------------- |
---|
[5000] | 79 | |
---|
[5575] | 80 | # Read in boundary from ordered sts file |
---|
[5578] | 81 | urs_bounding_polygon=create_sts_boundary(os.path.join(project.boundaries_dir,project.scenario_name)) |
---|
[5000] | 82 | |
---|
[5575] | 83 | # Reading the landward defined points, this incorporates the original clipping |
---|
| 84 | # polygon minus the 100m contour |
---|
[5578] | 85 | landward_bounding_polygon = read_polygon(project.boundaries_dir+'landward_bounding_polygon.txt') |
---|
[5575] | 86 | |
---|
| 87 | # Combine sts polyline with landward points |
---|
| 88 | bounding_polygon = urs_bounding_polygon + landward_bounding_polygon |
---|
| 89 | |
---|
| 90 | # counting segments |
---|
| 91 | N = len(urs_bounding_polygon)-1 |
---|
| 92 | boundary_tags={'back': [N+1,N+2,N+3,N+4, N+5], 'side': [N,N+6],'ocean': range(N)} |
---|
| 93 | |
---|
| 94 | |
---|
[5000] | 95 | #-------------------------------------------------------------------------- |
---|
| 96 | # Create the triangular mesh based on overall clipping polygon with a |
---|
| 97 | # tagged |
---|
| 98 | # boundary and interior regions defined in project.py along with |
---|
| 99 | # resolutions (maximal area of per triangle) for each polygon |
---|
| 100 | #-------------------------------------------------------------------------- |
---|
| 101 | |
---|
| 102 | #IMPORTANT don't cache create_mesh_from_region and Domain(mesh....) as it |
---|
| 103 | # causes problems with the ability to cache set quantity which takes alot of times |
---|
| 104 | if myid == 0: |
---|
| 105 | |
---|
| 106 | print 'start create mesh from regions' |
---|
[5381] | 107 | |
---|
[5578] | 108 | create_mesh_from_regions(bounding_polygon, |
---|
| 109 | boundary_tags=boundary_tags, |
---|
[5000] | 110 | maximum_triangle_area=project.res_poly_all, |
---|
| 111 | interior_regions=project.interior_regions, |
---|
| 112 | filename=project.meshes_dir_name+'.msh', |
---|
| 113 | use_cache=False, |
---|
[5381] | 114 | verbose=False) |
---|
[5480] | 115 | #barrier() |
---|
[5000] | 116 | |
---|
[5626] | 117 | ## covariance_value,alpha = find_optimal_smoothing_parameter (data_file = kwargs['bathy_file'], |
---|
| 118 | ## alpha_list=[0.001, 0.01, 0.1, 0.15, 0.2, 0.25, 0.3, 0.4, 0.5], |
---|
| 119 | ## mesh_file = project.meshes_dir_name+'.msh') |
---|
| 120 | ## print 'optimal alpha', covariance_value,alpha |
---|
[5000] | 121 | |
---|
| 122 | #------------------------------------------------------------------------- |
---|
| 123 | # Setup computational domain |
---|
| 124 | #------------------------------------------------------------------------- |
---|
| 125 | print 'Setup computational domain' |
---|
| 126 | |
---|
| 127 | domain = Domain(project.meshes_dir_name+'.msh', use_cache=False, verbose=True) |
---|
| 128 | print 'memory usage before del domain',mem_usage() |
---|
| 129 | |
---|
| 130 | print domain.statistics() |
---|
| 131 | print 'triangles',len(domain) |
---|
| 132 | |
---|
| 133 | kwargs['act_num_trigs']=len(domain) |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | #------------------------------------------------------------------------- |
---|
| 137 | # Setup initial conditions |
---|
| 138 | #------------------------------------------------------------------------- |
---|
| 139 | if myid == 0: |
---|
| 140 | |
---|
| 141 | print 'Setup initial conditions' |
---|
| 142 | |
---|
| 143 | from polygon import Polygon_function |
---|
| 144 | #following sets the stage/water to be offcoast only |
---|
[5408] | 145 | IC = Polygon_function( [(project.poly_mainland, 0)], default = kwargs['tide'], |
---|
[5000] | 146 | geo_reference = domain.geo_reference) |
---|
| 147 | domain.set_quantity('stage', IC) |
---|
| 148 | # domain.set_quantity('stage', kwargs['tide']) |
---|
| 149 | domain.set_quantity('friction', kwargs['friction']) |
---|
| 150 | |
---|
| 151 | print 'Start Set quantity',kwargs['bathy_file'] |
---|
| 152 | |
---|
| 153 | domain.set_quantity('elevation', |
---|
| 154 | filename = kwargs['bathy_file'], |
---|
| 155 | use_cache = True, |
---|
| 156 | verbose = True, |
---|
| 157 | alpha = kwargs['alpha']) |
---|
| 158 | print 'Finished Set quantity' |
---|
[5480] | 159 | #barrier() |
---|
[5000] | 160 | |
---|
| 161 | |
---|
| 162 | #------------------------------------------------------ |
---|
| 163 | # Distribute domain to implement parallelism !!! |
---|
| 164 | #------------------------------------------------------ |
---|
| 165 | |
---|
| 166 | if numprocs > 1: |
---|
| 167 | domain=distribute(domain) |
---|
| 168 | |
---|
| 169 | #------------------------------------------------------ |
---|
| 170 | # Set domain parameters |
---|
| 171 | #------------------------------------------------------ |
---|
| 172 | print 'domain id', id(domain) |
---|
| 173 | domain.set_name(kwargs['aa_scenario_name']) |
---|
| 174 | domain.set_datadir(kwargs['output_dir']) |
---|
| 175 | domain.set_default_order(2) # Apply second order scheme |
---|
| 176 | domain.set_minimum_storable_height(0.01) # Don't store anything less than 1cm |
---|
| 177 | domain.set_store_vertices_uniquely(False) |
---|
| 178 | domain.set_quantities_to_be_stored(['stage', 'xmomentum', 'ymomentum']) |
---|
[5121] | 179 | domain.tight_slope_limiters = 1 |
---|
[5000] | 180 | print 'domain id', id(domain) |
---|
| 181 | |
---|
| 182 | #------------------------------------------------------------------------- |
---|
| 183 | # Setup boundary conditions |
---|
| 184 | #------------------------------------------------------------------------- |
---|
| 185 | print 'Available boundary tags', domain.get_boundary_tags() |
---|
| 186 | print 'domain id', id(domain) |
---|
[5575] | 187 | |
---|
| 188 | boundary_urs_out=project.boundaries_dir_name |
---|
[5669] | 189 | |
---|
| 190 | Br = Reflective_boundary(domain) |
---|
| 191 | Bd = Dirichlet_boundary([kwargs['tide'],0,0]) |
---|
[5575] | 192 | |
---|
| 193 | print 'Available boundary tags', domain.get_boundary_tags() |
---|
[5626] | 194 | Bf = Field_boundary(boundary_urs_out+'.sts', # Change from file_boundary |
---|
| 195 | domain, mean_stage=project.tide, |
---|
[5609] | 196 | time_thinning=1, |
---|
[5669] | 197 | default_boundary=Bd, |
---|
[5575] | 198 | use_cache=True, |
---|
[5626] | 199 | verbose = True, |
---|
| 200 | boundary_polygon=bounding_polygon) |
---|
[5575] | 201 | |
---|
[5408] | 202 | domain.set_boundary({'back': Bd, |
---|
[5575] | 203 | 'side': Bd, |
---|
| 204 | 'ocean': Bf}) #changed from Bf to Bd for large wave |
---|
[5000] | 205 | |
---|
[5381] | 206 | kwargs['input_start_time']=domain.starttime |
---|
[5000] | 207 | |
---|
| 208 | print'finish set boundary' |
---|
| 209 | |
---|
| 210 | #---------------------------------------------------------------------------- |
---|
| 211 | # Evolve system through time |
---|
| 212 | #-------------------------------------------------------------------- |
---|
| 213 | t0 = time.time() |
---|
| 214 | |
---|
[5415] | 215 | for t in domain.evolve(yieldstep = project.yieldstep, finaltime = kwargs['finaltime'] |
---|
| 216 | ,skip_initial_step = False ): |
---|
[5000] | 217 | domain.write_time() |
---|
[5415] | 218 | domain.write_boundary_statistics(tags = 'ocean') |
---|
[5000] | 219 | |
---|
| 220 | x, y = domain.get_maximum_inundation_location() |
---|
| 221 | q = domain.get_maximum_inundation_elevation() |
---|
| 222 | |
---|
| 223 | print 'Maximum runup observed at (%.2f, %.2f) with elevation %.2f' %(x,y,q) |
---|
| 224 | |
---|
| 225 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
| 226 | |
---|
| 227 | #kwargs 'completed' must be added to write the final parameters to file |
---|
| 228 | kwargs['completed']=str(time.time()-t0) |
---|
| 229 | |
---|
| 230 | if myid==0: |
---|
| 231 | store_parameters(**kwargs) |
---|
[5480] | 232 | #barrier |
---|
[5000] | 233 | |
---|
| 234 | print 'memory usage before del domain1',mem_usage() |
---|
[5408] | 235 | |
---|
| 236 | #------------------------------------------------------------- |
---|
[5000] | 237 | if __name__ == "__main__": |
---|
| 238 | |
---|
| 239 | kwargs={} |
---|
| 240 | kwargs['est_num_trigs']=project.trigs_min |
---|
| 241 | kwargs['num_cpu']=numprocs |
---|
| 242 | kwargs['host']=project.host |
---|
| 243 | kwargs['res_factor']=project.res_factor |
---|
| 244 | kwargs['starttime']=project.starttime |
---|
| 245 | kwargs['yieldstep']=project.yieldstep |
---|
| 246 | kwargs['finaltime']=project.finaltime |
---|
| 247 | |
---|
| 248 | kwargs['output_dir']=project.output_run_time_dir |
---|
[5157] | 249 | kwargs['bathy_file']=project.combined_dir_name+'.txt' |
---|
[5626] | 250 | kwargs['file_name']=project.home+'detail.csv' |
---|
[5000] | 251 | kwargs['aa_scenario_name']=project.scenario_name |
---|
| 252 | kwargs['ab_time']=project.time |
---|
| 253 | kwargs['res_factor']= project.res_factor |
---|
| 254 | kwargs['tide']=project.tide |
---|
| 255 | kwargs['user']=project.user |
---|
| 256 | kwargs['alpha'] = project.alpha |
---|
| 257 | kwargs['friction']=project.friction |
---|
| 258 | kwargs['time_thinning'] = project.time_thinning |
---|
| 259 | kwargs['dir_comment']=project.dir_comment |
---|
| 260 | kwargs['export_cellsize']=project.export_cellsize |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | run_model(**kwargs) |
---|
| 264 | |
---|
[5480] | 265 | #barrier |
---|
[5381] | 266 | |
---|