1 | """Run a tsunami inundation scenario for Busselton, WA, Australia. |
---|
2 | |
---|
3 | The scenario is defined by a triangular mesh created from project.polygon, the |
---|
4 | elevation data is compiled into a pts file through build_elevation.py and a |
---|
5 | 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_elevation.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 | import os |
---|
24 | import os.path |
---|
25 | import time |
---|
26 | from time import localtime, strftime, gmtime |
---|
27 | |
---|
28 | # Related major packages |
---|
29 | from Scientific.IO.NetCDF import NetCDFFile |
---|
30 | import Numeric as num |
---|
31 | |
---|
32 | from anuga.interface import create_domain_from_regions |
---|
33 | from anuga.interface import Transmissive_stage_zero_momentum_boundary |
---|
34 | from anuga.interface import Dirichlet_boundary |
---|
35 | from anuga.interface import Reflective_boundary |
---|
36 | from anuga.interface import Field_boundary |
---|
37 | from anuga.interface import create_sts_boundary |
---|
38 | from anuga.interface import csv2building_polygons |
---|
39 | from file_length import file_length |
---|
40 | |
---|
41 | from anuga.shallow_water.data_manager import start_screen_catcher |
---|
42 | from anuga.shallow_water.data_manager import copy_code_files |
---|
43 | from anuga.shallow_water.data_manager import urs2sts |
---|
44 | from anuga.utilities.polygon import read_polygon, Polygon_function |
---|
45 | |
---|
46 | # Application specific imports |
---|
47 | from setup_model import project |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | ## |
---|
52 | # @brief Build boundary STS files from one or more MUX files. |
---|
53 | # @param mux_dir Directory containing the MUX files. |
---|
54 | # @param event_file Path to meta-file containing mux files+weight data. |
---|
55 | # @param output_dir Directory to write STS data to. |
---|
56 | # @note 'event_file' is produced by EventSelection. |
---|
57 | def build_urs_boundary(mux_dir, event_file, output_dir): |
---|
58 | '''Build a boundary STS file from a set of MUX files.''' |
---|
59 | |
---|
60 | print 'build_urs_boundary: mux_dir=%s' % mux_dir |
---|
61 | print 'build_urs_boundary: event_file=%s' % event_file |
---|
62 | print 'build_urs_boundary: output_dir=%s' % output_dir |
---|
63 | |
---|
64 | # if we are using an EventSelection multi-mux file |
---|
65 | if project.multi_mux: |
---|
66 | # get the mux+weight data from the event file |
---|
67 | mux_event_file = os.path.join(mux_dir, event_file) |
---|
68 | try: |
---|
69 | fd = open(mux_event_file, 'r') |
---|
70 | mux_data = fd.readlines() |
---|
71 | fd.close() |
---|
72 | except IOError, e: |
---|
73 | msg = 'File %s cannot be read: %s' % (mux_event_file, str(e)) |
---|
74 | raise Exception, msg |
---|
75 | except: |
---|
76 | raise |
---|
77 | |
---|
78 | # first line of file is # filenames+weight in rest of file |
---|
79 | num_lines = int(mux_data[0].strip()) |
---|
80 | mux_data = mux_data[1:] |
---|
81 | print 'number of sources %d' % num_lines |
---|
82 | |
---|
83 | # quick sanity check on input mux meta-file |
---|
84 | if num_lines != len(mux_data): |
---|
85 | msg = ('Bad file %s: %d data lines, but line 1 count is %d' |
---|
86 | % (event_file, len(mux_data), num_lines)) |
---|
87 | raise Exception, msg |
---|
88 | |
---|
89 | # Create filename and weights lists. |
---|
90 | # Must chop GRD filename just after '*.grd'. |
---|
91 | mux_filenames = [] |
---|
92 | for line in mux_data: |
---|
93 | muxname = line.strip().split()[0] |
---|
94 | split_index = muxname.index('.grd') |
---|
95 | muxname = muxname[:split_index+len('.grd')] |
---|
96 | muxname = os.path.join(mux_dir, muxname) |
---|
97 | mux_filenames.append(muxname) |
---|
98 | |
---|
99 | mux_weights = [float(line.strip().split()[1]) for line in mux_data] |
---|
100 | |
---|
101 | # Call legacy function to create STS file. |
---|
102 | # This should be replaced in the future. |
---|
103 | print 'reading', project.urs_order |
---|
104 | print 'creating STS file' |
---|
105 | print 'mux_filenames=%s' % str(mux_filenames) |
---|
106 | print 'basename_out=%s' % str(output_dir) |
---|
107 | print 'ordering_filename=%s' % str(project.urs_order) |
---|
108 | print 'weights=%s' % str(mux_weights) |
---|
109 | print 'mean_stage=%s' % str(project.tide) |
---|
110 | urs2sts(mux_filenames, |
---|
111 | basename_out=output_dir, |
---|
112 | ordering_filename=project.urs_order, |
---|
113 | weights=mux_weights, |
---|
114 | mean_stage=project.tide, |
---|
115 | verbose=True) |
---|
116 | else: # a single mux stem file |
---|
117 | urs_filenames = [os.path.join(mux_dir, event_file)] |
---|
118 | |
---|
119 | weight_factor = 1.0 |
---|
120 | weights = weight_factor*num.ones(len(urs_filenames), num.float) |
---|
121 | |
---|
122 | order_filename = os.path.join(project.order_filename_dir) |
---|
123 | |
---|
124 | print 'reading', order_filename |
---|
125 | # Create ordered sts file |
---|
126 | print 'creating sts file' |
---|
127 | urs2sts(urs_filenames, |
---|
128 | basename_out=os.path.join(project.boundaries_dir, |
---|
129 | project.scenario_name), |
---|
130 | ordering_filename=order_filename, |
---|
131 | weights=weights, |
---|
132 | mean_stage=project.tide, |
---|
133 | verbose=True) |
---|
134 | |
---|
135 | # report on stuff so far |
---|
136 | quantities, elevation, time = get_sts_gauge_data(project.event_folder, |
---|
137 | verbose=False) |
---|
138 | print len(elevation), len(quantities['stage'][0,:]) |
---|
139 | |
---|
140 | #------------------------------------------------------------------------------- |
---|
141 | # Copy scripts to time stamped output directory and capture screen |
---|
142 | # output to file. Copy script must be before screen_catcher |
---|
143 | #------------------------------------------------------------------------------- |
---|
144 | |
---|
145 | copy_code_files(project.output_run, __file__, |
---|
146 | os.path.join(os.path.dirname(project.__file__), |
---|
147 | project.__name__+'.py')) |
---|
148 | start_screen_catcher(project.output_run, 0, 1) |
---|
149 | |
---|
150 | #------------------------------------------------------------------------------- |
---|
151 | # Create the computational domain based on overall clipping polygon with |
---|
152 | # a tagged boundary and interior regions defined in project.py along with |
---|
153 | # resolutions (maximal area of per triangle) for each polygon |
---|
154 | #------------------------------------------------------------------------------- |
---|
155 | |
---|
156 | print 'Create computational domain' |
---|
157 | |
---|
158 | # Create the STS file |
---|
159 | print 'project.mux_data_folder=%s' % project.mux_data_folder |
---|
160 | bub.build_urs_boundary(project.mux_input_filename, |
---|
161 | os.path.join(project.event_folder, |
---|
162 | project.scenario_name)) |
---|
163 | |
---|
164 | # Read in boundary from ordered sts file |
---|
165 | event_sts = create_sts_boundary(project.event_sts) |
---|
166 | |
---|
167 | # Reading the landward defined points, this incorporates the original clipping |
---|
168 | # polygon minus the 100m contour |
---|
169 | landward_boundary = read_polygon(project.landward_boundary) |
---|
170 | |
---|
171 | # Combine sts polyline with landward points |
---|
172 | bounding_polygon_sts = event_sts + landward_boundary |
---|
173 | |
---|
174 | # Number of boundary segments |
---|
175 | num_ocean_segments = len(event_sts) - 1 |
---|
176 | # Number of landward_boundary points |
---|
177 | num_land_points = file_length(project.landward_boundary) |
---|
178 | |
---|
179 | # Boundary tags refer to project.landward_boundary |
---|
180 | # 4 points equals 5 segments start at N |
---|
181 | boundary_tags={'back': range(num_ocean_segments+1, |
---|
182 | num_ocean_segments+num_land_points), |
---|
183 | 'side': [num_ocean_segments, |
---|
184 | num_ocean_segments+num_land_points], |
---|
185 | 'ocean': range(num_ocean_segments)} |
---|
186 | |
---|
187 | # Build mesh and domain |
---|
188 | domain = create_domain_from_regions(bounding_polygon_sts, |
---|
189 | boundary_tags=boundary_tags, |
---|
190 | maximum_triangle_area=project.bounding_maxarea, |
---|
191 | interior_regions=project.interior_regions, |
---|
192 | mesh_filename=project.meshes, |
---|
193 | use_cache=True, |
---|
194 | verbose=True) |
---|
195 | print domain.statistics() |
---|
196 | |
---|
197 | domain.set_name(project.scenario_name) |
---|
198 | domain.set_datadir(project.output_run) |
---|
199 | domain.set_minimum_storable_height(0.01) # Don't store depth less than 1cm |
---|
200 | |
---|
201 | #------------------------------------------------------------------------------- |
---|
202 | # Setup initial conditions |
---|
203 | #------------------------------------------------------------------------------- |
---|
204 | |
---|
205 | print 'Setup initial conditions' |
---|
206 | |
---|
207 | # Set the initial stage in the offcoast region only |
---|
208 | IC = Polygon_function(project.land_initial_conditions, |
---|
209 | default=project.tide, |
---|
210 | geo_reference=domain.geo_reference) |
---|
211 | domain.set_quantity('stage', IC, use_cache=True, verbose=True) |
---|
212 | domain.set_quantity('friction', project.friction) |
---|
213 | domain.set_quantity('elevation', |
---|
214 | filename=project.combined_elevation+'.pts', |
---|
215 | use_cache=True, |
---|
216 | verbose=True, |
---|
217 | alpha=project.alpha) |
---|
218 | |
---|
219 | #------------------------------------------------------------------------------- |
---|
220 | # Setup boundary conditions |
---|
221 | #------------------------------------------------------------------------------- |
---|
222 | |
---|
223 | print 'Set boundary - available tags:', domain.get_boundary_tags() |
---|
224 | |
---|
225 | Br = Reflective_boundary(domain) |
---|
226 | Bt = Transmissive_stage_zero_momentum_boundary(domain) |
---|
227 | Bd = Dirichlet_boundary([kwargs['tide'], 0, 0]) |
---|
228 | Bf = Field_boundary(project.event_sts+'.sts', |
---|
229 | domain, mean_stage=project.tide, |
---|
230 | time_thinning=1, |
---|
231 | default_boundary=Bd, |
---|
232 | boundary_polygon=bounding_polygon_sts, |
---|
233 | use_cache=True, |
---|
234 | verbose=True) |
---|
235 | |
---|
236 | domain.set_boundary({'back': Br, |
---|
237 | 'side': Bt, |
---|
238 | 'ocean': Bf}) |
---|
239 | |
---|
240 | #------------------------------------------------------------------------------- |
---|
241 | # Evolve system through time |
---|
242 | #------------------------------------------------------------------------------- |
---|
243 | |
---|
244 | t0 = time.time() |
---|
245 | for t in domain.evolve(yieldstep=project.yieldstep, |
---|
246 | finaltime=project.finaltime, |
---|
247 | skip_initial_step=False): |
---|
248 | print domain.timestepping_statistics() |
---|
249 | print domain.boundary_statistics(tags='ocean') |
---|
250 | |
---|
251 | print 'Simulation took %.2f seconds' % (time.time()-t0) |
---|