[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 |
---|
| 20 | from os import mkdir, access, F_OK |
---|
| 21 | from shutil import copy |
---|
| 22 | import time |
---|
| 23 | import sys |
---|
| 24 | |
---|
| 25 | # Related major packages |
---|
| 26 | from anuga.shallow_water import Domain |
---|
| 27 | from anuga.shallow_water import Dirichlet_boundary |
---|
| 28 | from anuga.shallow_water import File_boundary |
---|
| 29 | from anuga.shallow_water import Reflective_boundary |
---|
| 30 | from anuga.shallow_water import Field_boundary |
---|
| 31 | from Numeric import allclose |
---|
| 32 | from anuga.shallow_water.data_manager import export_grid |
---|
| 33 | |
---|
| 34 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
| 35 | from anuga.shallow_water.data_manager import start_screen_catcher, copy_code_files,store_parameters |
---|
| 36 | from anuga_parallel.parallel_api import distribute, numprocs, myid, barrier |
---|
| 37 | from anuga_parallel.parallel_abstraction import get_processor_name |
---|
| 38 | from anuga.caching import myhash |
---|
| 39 | from anuga.damage_modelling.inundation_damage import add_depth_and_momentum2csv, inundation_damage |
---|
| 40 | from anuga.fit_interpolate.benchmark_least_squares import mem_usage |
---|
| 41 | |
---|
| 42 | # Application specific imports |
---|
| 43 | import project # Definition of file names and polygons |
---|
| 44 | |
---|
| 45 | def run_model(**kwargs): |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | #------------------------------------------------------------------------------ |
---|
| 49 | # Copy scripts to time stamped output directory and capture screen |
---|
| 50 | # output to file |
---|
| 51 | #------------------------------------------------------------------------------ |
---|
| 52 | print "Processor Name:",get_processor_name() |
---|
| 53 | |
---|
| 54 | #copy script must be before screen_catcher |
---|
| 55 | #print kwargs |
---|
| 56 | |
---|
| 57 | print 'output_dir',kwargs['output_dir'] |
---|
| 58 | if myid == 0: |
---|
| 59 | copy_code_files(kwargs['output_dir'],__file__, |
---|
| 60 | dirname(project.__file__)+sep+ project.__name__+'.py' ) |
---|
| 61 | |
---|
| 62 | store_parameters(**kwargs) |
---|
| 63 | |
---|
| 64 | barrier() |
---|
| 65 | |
---|
| 66 | start_screen_catcher(kwargs['output_dir'], myid, numprocs) |
---|
| 67 | |
---|
| 68 | print "Processor Name:",get_processor_name() |
---|
| 69 | |
---|
| 70 | # filenames |
---|
| 71 | # meshes_dir_name = project.meshes_dir_name+'.msh' |
---|
| 72 | |
---|
| 73 | # creates copy of code in output dir |
---|
| 74 | print 'min triangles', project.trigs_min, |
---|
| 75 | print 'Note: This is generally about 20% less than the final amount' |
---|
| 76 | |
---|
| 77 | #-------------------------------------------------------------------------- |
---|
| 78 | # Create the triangular mesh based on overall clipping polygon with a |
---|
| 79 | # tagged |
---|
| 80 | # boundary and interior regions defined in project.py along with |
---|
| 81 | # resolutions (maximal area of per triangle) for each polygon |
---|
| 82 | #-------------------------------------------------------------------------- |
---|
| 83 | |
---|
| 84 | #IMPORTANT don't cache create_mesh_from_region and Domain(mesh....) as it |
---|
| 85 | # causes problems with the ability to cache set quantity which takes alot of times |
---|
| 86 | if myid == 0: |
---|
| 87 | |
---|
| 88 | print 'start create mesh from regions' |
---|
[5381] | 89 | |
---|
[5000] | 90 | create_mesh_from_regions(project.poly_all, |
---|
| 91 | boundary_tags=project.boundary_tags, |
---|
| 92 | maximum_triangle_area=project.res_poly_all, |
---|
| 93 | interior_regions=project.interior_regions, |
---|
| 94 | filename=project.meshes_dir_name+'.msh', |
---|
| 95 | use_cache=False, |
---|
[5381] | 96 | verbose=False) |
---|
[5000] | 97 | barrier() |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | #------------------------------------------------------------------------- |
---|
| 101 | # Setup computational domain |
---|
| 102 | #------------------------------------------------------------------------- |
---|
| 103 | print 'Setup computational domain' |
---|
| 104 | |
---|
| 105 | #domain = cache(Domain, (meshes_dir_name), {'use_cache':True, 'verbose':True}, verbose=True) |
---|
| 106 | #above don't work |
---|
| 107 | domain = Domain(project.meshes_dir_name+'.msh', use_cache=False, verbose=True) |
---|
| 108 | print 'memory usage before del domain',mem_usage() |
---|
| 109 | |
---|
| 110 | print domain.statistics() |
---|
| 111 | print 'triangles',len(domain) |
---|
| 112 | |
---|
| 113 | kwargs['act_num_trigs']=len(domain) |
---|
| 114 | |
---|
| 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 |
---|
[5408] | 125 | IC = Polygon_function( [(project.poly_mainland, 0)], default = kwargs['tide'], |
---|
[5000] | 126 | geo_reference = domain.geo_reference) |
---|
| 127 | domain.set_quantity('stage', IC) |
---|
| 128 | # domain.set_quantity('stage', kwargs['tide']) |
---|
| 129 | domain.set_quantity('friction', kwargs['friction']) |
---|
| 130 | |
---|
| 131 | print 'Start Set quantity',kwargs['bathy_file'] |
---|
| 132 | |
---|
| 133 | domain.set_quantity('elevation', |
---|
| 134 | filename = kwargs['bathy_file'], |
---|
| 135 | use_cache = True, |
---|
| 136 | verbose = True, |
---|
| 137 | alpha = kwargs['alpha']) |
---|
| 138 | print 'Finished Set quantity' |
---|
| 139 | barrier() |
---|
| 140 | |
---|
| 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']) |
---|
[5121] | 159 | domain.tight_slope_limiters = 1 |
---|
| 160 | #domain.set_maximum_allowed_speed(0.1) # Allow a little runoff (0.1 is OK) |
---|
[5000] | 161 | print 'domain id', id(domain) |
---|
| 162 | domain.beta_h = 0 |
---|
| 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 | |
---|
[5149] | 171 | print'set_boundary' |
---|
[5000] | 172 | |
---|
| 173 | Br = Reflective_boundary(domain) |
---|
| 174 | Bd = Dirichlet_boundary([kwargs['tide'],0,0]) |
---|
[5408] | 175 | |
---|
[5000] | 176 | |
---|
[5408] | 177 | Bf = Field_boundary(kwargs['boundary_file'], |
---|
[5149] | 178 | domain, time_thinning=kwargs['time_thinning'], mean_stage=kwargs['tide'], |
---|
| 179 | use_cache=True, verbose=True) |
---|
[5408] | 180 | print 'finished reading boundary file' |
---|
| 181 | domain.set_boundary({'back': Bd, |
---|
[5149] | 182 | 'side': Bd, |
---|
| 183 | 'ocean': Bf}) |
---|
[5000] | 184 | |
---|
[5381] | 185 | kwargs['input_start_time']=domain.starttime |
---|
[5000] | 186 | |
---|
| 187 | print'finish set boundary' |
---|
| 188 | |
---|
| 189 | #---------------------------------------------------------------------------- |
---|
| 190 | # Evolve system through time |
---|
| 191 | #-------------------------------------------------------------------- |
---|
| 192 | t0 = time.time() |
---|
| 193 | |
---|
[5408] | 194 | for t in domain.evolve(yieldstep = 240, finaltime = kwargs['finaltime']): |
---|
[5000] | 195 | domain.write_time() |
---|
| 196 | domain.write_boundary_statistics(tags = 'ocean') |
---|
| 197 | |
---|
| 198 | x, y = domain.get_maximum_inundation_location() |
---|
| 199 | q = domain.get_maximum_inundation_elevation() |
---|
| 200 | |
---|
| 201 | print 'Maximum runup observed at (%.2f, %.2f) with elevation %.2f' %(x,y,q) |
---|
| 202 | |
---|
| 203 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
| 204 | |
---|
| 205 | #kwargs 'completed' must be added to write the final parameters to file |
---|
| 206 | kwargs['completed']=str(time.time()-t0) |
---|
| 207 | |
---|
| 208 | if myid==0: |
---|
| 209 | store_parameters(**kwargs) |
---|
| 210 | barrier |
---|
| 211 | |
---|
| 212 | print 'memory usage before del domain1',mem_usage() |
---|
[5408] | 213 | |
---|
| 214 | #------------------------------------------------------------- |
---|
[5000] | 215 | if __name__ == "__main__": |
---|
| 216 | |
---|
| 217 | kwargs={} |
---|
| 218 | kwargs['est_num_trigs']=project.trigs_min |
---|
| 219 | kwargs['num_cpu']=numprocs |
---|
| 220 | kwargs['host']=project.host |
---|
| 221 | kwargs['res_factor']=project.res_factor |
---|
| 222 | kwargs['starttime']=project.starttime |
---|
| 223 | kwargs['yieldstep']=project.yieldstep |
---|
| 224 | kwargs['midtime']=project.midtime |
---|
| 225 | kwargs['finaltime']=project.finaltime |
---|
| 226 | |
---|
| 227 | kwargs['output_dir']=project.output_run_time_dir |
---|
[5157] | 228 | kwargs['bathy_file']=project.combined_dir_name+'.txt' |
---|
[5000] | 229 | # kwargs['bathy_file']=project.combined_small_dir_name + '.pts' |
---|
| 230 | kwargs['boundary_file']=project.boundaries_in_dir_name + '.sww' |
---|
| 231 | kwargs['file_name']=project.home+'detail.csv' |
---|
| 232 | kwargs['aa_scenario_name']=project.scenario_name |
---|
| 233 | kwargs['ab_time']=project.time |
---|
| 234 | kwargs['res_factor']= project.res_factor |
---|
| 235 | kwargs['tide']=project.tide |
---|
| 236 | kwargs['user']=project.user |
---|
| 237 | kwargs['alpha'] = project.alpha |
---|
| 238 | kwargs['friction']=project.friction |
---|
| 239 | kwargs['time_thinning'] = project.time_thinning |
---|
| 240 | kwargs['dir_comment']=project.dir_comment |
---|
| 241 | kwargs['export_cellsize']=project.export_cellsize |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | run_model(**kwargs) |
---|
| 245 | |
---|
| 246 | if myid==0: |
---|
| 247 | export_model(**kwargs) |
---|
[5381] | 248 | barrier |
---|
| 249 | |
---|