1 | """Script for running a tsunami inundation scenario for Patong Beach, Thailand. |
---|
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_patong.py |
---|
5 | and a simulated tsunami for the 2004 event is generated through |
---|
6 | an sts file from build_boundary.py. |
---|
7 | |
---|
8 | Input: sts file (build_boundary.py) |
---|
9 | pts file (build_patong.py) |
---|
10 | information from project file |
---|
11 | Outputs: sww file stored in project.output_run_time_dir |
---|
12 | The export_results_all.py and get_timeseries.py is reliant |
---|
13 | on the outputs of this script |
---|
14 | |
---|
15 | """ |
---|
16 | |
---|
17 | #------------------------------------------------------------------------------ |
---|
18 | # Import necessary modules |
---|
19 | #------------------------------------------------------------------------------ |
---|
20 | |
---|
21 | # Standard modules |
---|
22 | from os import sep |
---|
23 | import os |
---|
24 | from os.path import dirname |
---|
25 | import time |
---|
26 | |
---|
27 | # Related major packages |
---|
28 | from anuga.interface import create_domain_from_regions |
---|
29 | from anuga.interface import Dirichlet_boundary |
---|
30 | from anuga.interface import Transmissive_stage_zero_momentum_boundary |
---|
31 | from anuga.interface import Reflective_boundary |
---|
32 | from anuga.interface import Field_boundary |
---|
33 | from anuga.interface import create_sts_boundary |
---|
34 | from anuga.interface import csv2building_polygons |
---|
35 | from file_length import file_length |
---|
36 | |
---|
37 | from anuga.caching import cache |
---|
38 | from anuga.shallow_water.data_manager import start_screen_catcher, copy_code_files,store_parameters |
---|
39 | from anuga.utilities.polygon import read_polygon |
---|
40 | from polygon import Polygon_function |
---|
41 | |
---|
42 | # Application specific imports |
---|
43 | import project # Definition of file names and polygons |
---|
44 | numprocs = 1 |
---|
45 | myid = 0 |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | #------------------------------------------------------------------------------ |
---|
50 | # Copy scripts to time stamped output directory and capture screen |
---|
51 | # output to file |
---|
52 | #------------------------------------------------------------------------------ |
---|
53 | |
---|
54 | #copy script must be before screen_catcher |
---|
55 | |
---|
56 | output_dir = project.output_run_time_dir |
---|
57 | print 'output_dir', output_dir |
---|
58 | |
---|
59 | copy_code_files(output_dir,__file__, |
---|
60 | dirname(project.__file__)+sep+ project.__name__+'.py' ) |
---|
61 | |
---|
62 | start_screen_catcher(output_dir, myid, numprocs) |
---|
63 | |
---|
64 | |
---|
65 | #----------------------------------------------------------------------- |
---|
66 | # Domain definitions |
---|
67 | #----------------------------------------------------------------------- |
---|
68 | |
---|
69 | # Read in boundary from ordered sts file |
---|
70 | urs_boundary_name = os.path.join(project.boundaries_dir, |
---|
71 | project.scenario_name) |
---|
72 | urs_bounding_polygon = create_sts_boundary(urs_boundary_name) |
---|
73 | |
---|
74 | # Reading the landward defined points, this incorporates the original clipping |
---|
75 | # polygon minus the 100m contour |
---|
76 | landward_bounding_polygon = read_polygon(project.landward_dir) |
---|
77 | |
---|
78 | # Combine sts polyline with landward points |
---|
79 | bounding_polygon = urs_bounding_polygon + landward_bounding_polygon |
---|
80 | |
---|
81 | # Counting segments |
---|
82 | N = len(urs_bounding_polygon)-1 |
---|
83 | # Number of landward_boundary points |
---|
84 | M = file_length(project.landward_dir) |
---|
85 | |
---|
86 | |
---|
87 | # Boundary tags refer to project.landward_boundary |
---|
88 | # 4 points equals 5 segments start at N |
---|
89 | boundary_tags={'back': range(N+1,N+M), |
---|
90 | 'side': [N,N+M], |
---|
91 | 'ocean': range(N)} |
---|
92 | |
---|
93 | #-------------------------------------------------------------------------- |
---|
94 | # Create the computational domain based on overall clipping polygon with |
---|
95 | # a tagged boundary and interior regions defined in project.py along with |
---|
96 | # resolutions (maximal area of per triangle) for each polygon |
---|
97 | #-------------------------------------------------------------------------- |
---|
98 | |
---|
99 | domain = create_domain_from_regions(bounding_polygon, |
---|
100 | boundary_tags=boundary_tags, |
---|
101 | maximum_triangle_area=project.res_poly_all, |
---|
102 | interior_regions=project.interior_regions, |
---|
103 | mesh_filename=project.meshes_dir_name, |
---|
104 | use_cache=True, |
---|
105 | verbose=True) |
---|
106 | |
---|
107 | print domain.statistics() |
---|
108 | |
---|
109 | #------------------------------------------------------------------------- |
---|
110 | # Setup initial conditions |
---|
111 | #------------------------------------------------------------------------- |
---|
112 | print 'Setup initial conditions' |
---|
113 | |
---|
114 | # sets the initial stage in the offcoast region only |
---|
115 | IC = Polygon_function([(project.poly_mainland, 0)], |
---|
116 | default=project.tide, |
---|
117 | geo_reference=domain.geo_reference) |
---|
118 | print 'stage' |
---|
119 | domain.set_quantity('stage', IC, |
---|
120 | use_cache=True, |
---|
121 | verbose=True) |
---|
122 | print 'friction' |
---|
123 | domain.set_quantity('friction', project.friction) |
---|
124 | |
---|
125 | print 'elevation' |
---|
126 | domain.set_quantity('elevation', |
---|
127 | filename=project.combined_dir_name+'.pts', |
---|
128 | alpha=project.alpha, |
---|
129 | use_cache=True, |
---|
130 | verbose=True) |
---|
131 | |
---|
132 | if project.use_buildings: |
---|
133 | # Add buildings from file |
---|
134 | print 'Reading building polygons' |
---|
135 | building_polygons, building_heights = csv2building_polygons(project.building_polygon_file) |
---|
136 | #clipping_polygons=project.building_area_polygons) |
---|
137 | |
---|
138 | print 'Creating %d building polygons' % len(building_polygons) |
---|
139 | def create_polygon_function(building_polygons, geo_reference=None): |
---|
140 | L = [] |
---|
141 | for i, key in enumerate(building_polygons): |
---|
142 | if i%100==0: print i |
---|
143 | poly = building_polygons[key] |
---|
144 | elev = building_heights[key] |
---|
145 | L.append((poly, elev)) |
---|
146 | |
---|
147 | buildings = Polygon_function(L, default=0.0, |
---|
148 | geo_reference=geo_reference) |
---|
149 | return buildings |
---|
150 | |
---|
151 | buildings = cache(create_polygon_function, |
---|
152 | building_polygons, |
---|
153 | {'geo_reference': domain.geo_reference}, |
---|
154 | verbose=True) |
---|
155 | |
---|
156 | print 'Adding buildings' |
---|
157 | domain.add_quantity('elevation', |
---|
158 | buildings, |
---|
159 | use_cache=True, |
---|
160 | verbose=True) |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | #------------------------------------------------------ |
---|
165 | # Distribute domain to implement parallelism !!! |
---|
166 | #------------------------------------------------------ |
---|
167 | |
---|
168 | # if numprocs > 1: |
---|
169 | # domain=distribute(domain) |
---|
170 | |
---|
171 | #------------------------------------------------------ |
---|
172 | # Set domain parameters |
---|
173 | #------------------------------------------------------ |
---|
174 | domain.set_name(project.scenario_name) |
---|
175 | domain.set_datadir(output_dir) |
---|
176 | domain.set_default_order(2) # Apply second order scheme |
---|
177 | domain.set_minimum_storable_height(0.01) # Don't store anything less than 1cm |
---|
178 | |
---|
179 | #------------------------------------------------------------------------- |
---|
180 | # Setup boundary conditions |
---|
181 | #------------------------------------------------------------------------- |
---|
182 | print 'Set boundary conditions' |
---|
183 | |
---|
184 | Br = Reflective_boundary(domain) |
---|
185 | Bd = Dirichlet_boundary([project.tide,0,0]) |
---|
186 | Bt = Transmissive_stage_zero_momentum_boundary(domain) |
---|
187 | Bf = Field_boundary(urs_boundary_name+'.sts', |
---|
188 | domain, |
---|
189 | mean_stage= project.tide, |
---|
190 | time_thinning=project.time_thinning, |
---|
191 | default_boundary=Bd, |
---|
192 | use_cache=True, |
---|
193 | verbose=True, |
---|
194 | boundary_polygon=bounding_polygon) |
---|
195 | |
---|
196 | domain.set_boundary({'back': Br, |
---|
197 | 'side': Bt, |
---|
198 | 'ocean': Bf}) |
---|
199 | |
---|
200 | |
---|
201 | #---------------------------------------------------------------------------- |
---|
202 | # Evolve system through time |
---|
203 | #-------------------------------------------------------------------- |
---|
204 | t0 = time.time() |
---|
205 | |
---|
206 | # Skip over the first 6000 seconds |
---|
207 | for t in domain.evolve(yieldstep=2000, |
---|
208 | finaltime=6000): |
---|
209 | print domain.timestepping_statistics() |
---|
210 | print domain.boundary_statistics(tags='ocean') |
---|
211 | |
---|
212 | # Start detailed model |
---|
213 | for t in domain.evolve(yieldstep=project.yieldstep, |
---|
214 | finaltime=project.finaltime, |
---|
215 | skip_initial_step=True): |
---|
216 | print domain.timestepping_statistics() |
---|
217 | print domain.boundary_statistics(tags='ocean') |
---|
218 | |
---|
219 | print 'Simulation took %.2f seconds' %(time.time()-t0) |
---|
220 | |
---|
221 | |
---|