1 | """Script for running a tsunami inundation scenario for geraldton, WA, Australia. |
---|
2 | |
---|
3 | The scenario is defined by a triangular mesh created from project.polygon, |
---|
4 | the elevation data is compiled into a pts file through build_geraldton.py |
---|
5 | and a simulated tsunami is generated through an sts file from build_boundary.py. |
---|
6 | |
---|
7 | Input: sts file (build_boundary.py for respective event) |
---|
8 | pts file (build_geraldton.py) |
---|
9 | information from project file |
---|
10 | Outputs: sww file stored in project.output_run_time_dir |
---|
11 | The export_results_all.py and get_timeseries.py is reliant |
---|
12 | on the outputs of this script |
---|
13 | |
---|
14 | Ole Nielsen and Duncan Gray, GA - 2005, Jane Sexton, Nick Bartzis, GA - 2006 |
---|
15 | Ole Nielsen, Jane Sexton and Kristy Van Putten - 2008 |
---|
16 | """ |
---|
17 | |
---|
18 | #------------------------------------------------------------------------------ |
---|
19 | # Import necessary modules |
---|
20 | #------------------------------------------------------------------------------ |
---|
21 | |
---|
22 | # Standard modules |
---|
23 | from os import sep |
---|
24 | import os |
---|
25 | from os.path import dirname, basename |
---|
26 | from os import mkdir, access, F_OK |
---|
27 | from shutil import copy |
---|
28 | import time |
---|
29 | import sys |
---|
30 | |
---|
31 | # Related major packages |
---|
32 | from anuga.shallow_water import Domain |
---|
33 | from anuga.shallow_water import Dirichlet_boundary |
---|
34 | from anuga.shallow_water import File_boundary |
---|
35 | from anuga.shallow_water import Reflective_boundary |
---|
36 | from anuga.shallow_water import Field_boundary |
---|
37 | from Numeric import allclose |
---|
38 | from anuga.shallow_water.data_manager import export_grid, create_sts_boundary |
---|
39 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
40 | from anuga.pmesh.mesh import importMeshFromFile |
---|
41 | from anuga.shallow_water.data_manager import start_screen_catcher, copy_code_files,store_parameters |
---|
42 | from anuga_parallel.parallel_abstraction import get_processor_name |
---|
43 | from anuga.caching import myhash |
---|
44 | from anuga.damage_modelling.inundation_damage import add_depth_and_momentum2csv, inundation_damage |
---|
45 | from anuga.fit_interpolate.benchmark_least_squares import mem_usage |
---|
46 | from anuga.utilities.polygon import read_polygon, plot_polygons, polygon_area, is_inside_polygon |
---|
47 | from anuga.geospatial_data.geospatial_data import find_optimal_smoothing_parameter,Geospatial_data |
---|
48 | from polygon import Polygon_function |
---|
49 | |
---|
50 | |
---|
51 | # Application specific imports |
---|
52 | import project # Definition of file names and polygons |
---|
53 | numprocs = 1 |
---|
54 | myid = 0 |
---|
55 | |
---|
56 | def run_model(**kwargs): |
---|
57 | |
---|
58 | #------------------------------------------------------------------------------ |
---|
59 | # Copy scripts to time stamped output directory and capture screen |
---|
60 | # output to file |
---|
61 | #------------------------------------------------------------------------------ |
---|
62 | print "Processor Name:",get_processor_name() |
---|
63 | |
---|
64 | #copy script must be before screen_catcher |
---|
65 | |
---|
66 | print 'output_dir',kwargs['output_dir'] |
---|
67 | |
---|
68 | copy_code_files(kwargs['output_dir'],__file__, |
---|
69 | dirname(project.__file__)+sep+ project.__name__+'.py' ) |
---|
70 | |
---|
71 | store_parameters(**kwargs) |
---|
72 | |
---|
73 | start_screen_catcher(kwargs['output_dir'], myid, numprocs) |
---|
74 | |
---|
75 | print "Processor Name:",get_processor_name() |
---|
76 | |
---|
77 | #----------------------------------------------------------------------- |
---|
78 | # Domain definitions |
---|
79 | #----------------------------------------------------------------------- |
---|
80 | |
---|
81 | # Read in boundary from ordered sts file |
---|
82 | urs_bounding_polygon=create_sts_boundary(os.path.join(project.boundaries_dir_event,project.scenario_name)) |
---|
83 | |
---|
84 | # Reading the landward defined points, this incorporates the original clipping |
---|
85 | # polygon minus the 100m contour |
---|
86 | landward_bounding_polygon = read_polygon(project.landward_dir) |
---|
87 | |
---|
88 | # Combine sts polyline with landward points |
---|
89 | bounding_polygon = urs_bounding_polygon + landward_bounding_polygon |
---|
90 | |
---|
91 | # counting segments |
---|
92 | N = len(urs_bounding_polygon)-1 |
---|
93 | |
---|
94 | # boundary tags refer to project.landward 4 points equals 5 segments start at N |
---|
95 | boundary_tags={'back': [N+2,N+3], 'side': [N,N+1,N+4], 'ocean': range(N)} |
---|
96 | |
---|
97 | #-------------------------------------------------------------------------- |
---|
98 | # Create the triangular mesh based on overall clipping polygon with a tagged |
---|
99 | # boundary and interior regions defined in project.py along with |
---|
100 | # resolutions (maximal area of per triangle) for each polygon |
---|
101 | #-------------------------------------------------------------------------- |
---|
102 | |
---|
103 | # IMPORTANT don't cache create_mesh_from_region and Domain(mesh....) as it |
---|
104 | # causes problems with the ability to cache set quantity which takes alot of times |
---|
105 | |
---|
106 | print 'start create mesh from regions' |
---|
107 | |
---|
108 | create_mesh_from_regions(bounding_polygon, |
---|
109 | boundary_tags=boundary_tags, |
---|
110 | maximum_triangle_area=project.res_poly_all, |
---|
111 | interior_regions=project.interior_regions, |
---|
112 | filename=project.meshes_dir_name, |
---|
113 | use_cache=False, |
---|
114 | verbose=True) |
---|
115 | |
---|
116 | m = importMeshFromFile(project.meshes_dir_name) |
---|
117 | # load points from a points file |
---|
118 | m.add_points_and_segments(Geospatial_data(project.barrier_dir_name)) |
---|
119 | m.add_points_and_segments(Geospatial_data(project.barrier_dir_name1)) |
---|
120 | m.generate_mesh() |
---|
121 | m.export_mesh_file(project.meshes_dir_name) |
---|
122 | |
---|
123 | #------------------------------------------------------------------------- |
---|
124 | # Setup computational domain |
---|
125 | #------------------------------------------------------------------------- |
---|
126 | print 'Setup computational domain' |
---|
127 | |
---|
128 | domain = Domain(project.meshes_dir_name, use_cache=False, verbose=True) |
---|
129 | print 'memory usage before del domain',mem_usage() |
---|
130 | |
---|
131 | print domain.statistics() |
---|
132 | print 'triangles',len(domain) |
---|
133 | |
---|
134 | kwargs['act_num_trigs']=len(domain) |
---|
135 | |
---|
136 | |
---|
137 | #------------------------------------------------------------------------- |
---|
138 | # Setup initial conditions |
---|
139 | #------------------------------------------------------------------------- |
---|
140 | print 'Setup initial conditions' |
---|
141 | |
---|
142 | # sets the initial stage in the offcoast region only |
---|
143 | IC = Polygon_function( [(project.poly_mainland, 0)], default = kwargs['tide'], |
---|
144 | geo_reference = domain.geo_reference) |
---|
145 | domain.set_quantity('stage', IC) |
---|
146 | #domain.set_quantity('stage',kwargs['tide'] ) |
---|
147 | domain.set_quantity('friction', kwargs['friction']) |
---|
148 | |
---|
149 | print 'Start Set quantity',kwargs['elevation_file'] |
---|
150 | |
---|
151 | domain.set_quantity('elevation', |
---|
152 | filename = kwargs['elevation_file'], |
---|
153 | use_cache = False, |
---|
154 | verbose = True, |
---|
155 | alpha = kwargs['alpha']) |
---|
156 | print 'Finished Set quantity' |
---|
157 | |
---|
158 | ## #------------------------------------------------------ |
---|
159 | ## # Distribute domain to implement parallelism !!! |
---|
160 | ## #------------------------------------------------------ |
---|
161 | ## |
---|
162 | ## if numprocs > 1: |
---|
163 | ## domain=distribute(domain) |
---|
164 | |
---|
165 | #------------------------------------------------------ |
---|
166 | # Set domain parameters |
---|
167 | #------------------------------------------------------ |
---|
168 | print 'domain id', id(domain) |
---|
169 | domain.set_name(kwargs['scenario_name']) |
---|
170 | domain.set_datadir(kwargs['output_dir']) |
---|
171 | domain.set_default_order(2) # Apply second order scheme |
---|
172 | domain.set_minimum_storable_height(0.01) # Don't store anything less than 1cm |
---|
173 | domain.set_store_vertices_uniquely(False) |
---|
174 | domain.set_quantities_to_be_stored(['stage', 'xmomentum', 'ymomentum']) |
---|
175 | domain.tight_slope_limiters = 1 |
---|
176 | print 'domain id', id(domain) |
---|
177 | |
---|
178 | #------------------------------------------------------------------------- |
---|
179 | # Setup boundary conditions |
---|
180 | #------------------------------------------------------------------------- |
---|
181 | print 'Available boundary tags', domain.get_boundary_tags() |
---|
182 | print 'domain id', id(domain) |
---|
183 | |
---|
184 | boundary_urs_out=project.boundaries_dir_event + sep + project.scenario_name |
---|
185 | |
---|
186 | Br = Reflective_boundary(domain) |
---|
187 | Bd = Dirichlet_boundary([kwargs['tide'],0,0]) |
---|
188 | |
---|
189 | print 'Available boundary tags', domain.get_boundary_tags() |
---|
190 | Bf = Field_boundary(boundary_urs_out+'.sts', # Change from file_boundary |
---|
191 | domain, mean_stage= project.tide, |
---|
192 | time_thinning=1, |
---|
193 | default_boundary=Bd, |
---|
194 | use_cache=True, |
---|
195 | verbose = True, |
---|
196 | boundary_polygon=bounding_polygon) |
---|
197 | |
---|
198 | domain.set_boundary({'back': Br, |
---|
199 | 'side': Bd, |
---|
200 | 'ocean': Bf}) |
---|
201 | |
---|
202 | kwargs['input_start_time']=domain.starttime |
---|
203 | |
---|
204 | print'finish set boundary' |
---|
205 | |
---|
206 | #---------------------------------------------------------------------------- |
---|
207 | # Evolve system through time |
---|
208 | #-------------------------------------------------------------------- |
---|
209 | t0 = time.time() |
---|
210 | |
---|
211 | for t in domain.evolve(yieldstep = project.yieldstep, finaltime = kwargs['finaltime'] |
---|
212 | ,skip_initial_step = False): |
---|
213 | domain.write_time() |
---|
214 | domain.write_boundary_statistics(tags = 'ocean') |
---|
215 | |
---|
216 | # these outputs should be checked with the resultant inundation map |
---|
217 | x, y = domain.get_maximum_inundation_location() |
---|
218 | q = domain.get_maximum_inundation_elevation() |
---|
219 | print 'Maximum runup observed at (%.2f, %.2f) with elevation %.2f' %(x,y,q) |
---|
220 | |
---|
221 | print 'Simulation took %.2f seconds' %(time.time()-t0) |
---|
222 | |
---|
223 | #kwargs 'completed' must be added to write the final parameters to file |
---|
224 | kwargs['completed']=str(time.time()-t0) |
---|
225 | |
---|
226 | store_parameters(**kwargs) |
---|
227 | |
---|
228 | print 'memory usage before del domain1',mem_usage() |
---|
229 | |
---|
230 | |
---|
231 | #------------------------------------------------------------- |
---|
232 | if __name__ == "__main__": |
---|
233 | |
---|
234 | kwargs={} |
---|
235 | kwargs['file_name']=project.dir_comment |
---|
236 | kwargs['finaltime']=project.finaltime |
---|
237 | kwargs['output_dir']=project.output_run_time_dir |
---|
238 | kwargs['elevation_file']=project.combined_dir_name+'.pts' |
---|
239 | kwargs['scenario_name']=project.scenario_name |
---|
240 | kwargs['tide']=project.tide |
---|
241 | kwargs['alpha'] = project.alpha |
---|
242 | kwargs['friction']=project.friction |
---|
243 | #kwargs['num_cpu']=numprocs |
---|
244 | #kwargs['host']=project.host |
---|
245 | #kwargs['starttime']=project.starttime |
---|
246 | #kwargs['yieldstep']=project.yieldstep |
---|
247 | #kwargs['user']=project.user |
---|
248 | #kwargs['time_thinning'] = project.time_thinning |
---|
249 | |
---|
250 | run_model(**kwargs) |
---|
251 | |
---|
252 | |
---|