1 | """ |
---|
2 | |
---|
3 | Script for running a breaking wave simulation of Jon Hinwoods wave tank. |
---|
4 | Note: this is based on the frinction_ua_flume_2006 structure. |
---|
5 | |
---|
6 | |
---|
7 | Duncan Gray, GA - 2007 |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | """ |
---|
12 | |
---|
13 | |
---|
14 | #---------------------------------------------------------------------------- |
---|
15 | # Import necessary modules |
---|
16 | #---------------------------------------------------------------------------- |
---|
17 | |
---|
18 | # Standard modules |
---|
19 | import time |
---|
20 | from time import localtime, strftime |
---|
21 | import sys |
---|
22 | from shutil import copy |
---|
23 | from os import path, sep |
---|
24 | from os.path import dirname, join #, basename |
---|
25 | from Numeric import zeros, size, Float |
---|
26 | |
---|
27 | # Related major packages |
---|
28 | from anuga.shallow_water import Domain, Reflective_boundary, \ |
---|
29 | Dirichlet_boundary, Time_boundary, \ |
---|
30 | File_boundary, \ |
---|
31 | Transmissive_Momentum_Set_Stage_boundary |
---|
32 | from anuga.fit_interpolate.interpolate import interpolate_sww2csv |
---|
33 | from anuga.abstract_2d_finite_volumes.util import start_screen_catcher, \ |
---|
34 | file_function |
---|
35 | from anuga.shallow_water.data_manager import copy_code_files |
---|
36 | from anuga.abstract_2d_finite_volumes.generic_boundary_conditions\ |
---|
37 | import File_boundary_time |
---|
38 | |
---|
39 | # Scenario specific imports |
---|
40 | import project # Definition of file names and polygons |
---|
41 | import create_mesh |
---|
42 | from prepare_time_boundary import prepare_time_boundary |
---|
43 | from interp import interp |
---|
44 | |
---|
45 | |
---|
46 | class Elevation_function: |
---|
47 | def __init__(self, slope): |
---|
48 | self.xslope_position = [slope['xleft'][0],slope['xtoe'][0], |
---|
49 | slope['xbeach'][0],slope['xright'][0]] |
---|
50 | self.yslope_height = [slope['xleft'][1],slope['xtoe'][1], |
---|
51 | slope['xbeach'][1],slope['xright'][1]] |
---|
52 | |
---|
53 | def __call__(self, x,y): |
---|
54 | |
---|
55 | z = interp(self.yslope_height, self.xslope_position, x) |
---|
56 | return z |
---|
57 | |
---|
58 | def main(boundary_file, |
---|
59 | metadata_dic, |
---|
60 | boundary_path=None, |
---|
61 | friction=0.01, |
---|
62 | outputdir_name=None, |
---|
63 | run_type=0): |
---|
64 | |
---|
65 | |
---|
66 | basename = 'zz_' + metadata_dic['scenario_id'] |
---|
67 | if run_type == 1: |
---|
68 | outputdir_name += '_test_C' |
---|
69 | yieldstep = 1.0 |
---|
70 | finaltime = 15. |
---|
71 | maximum_triangle_area=0.1 |
---|
72 | |
---|
73 | elif run_type == 2: |
---|
74 | outputdir_name += '_test_long_time' |
---|
75 | yieldstep = 0.5 |
---|
76 | finaltime = None |
---|
77 | maximum_triangle_area=0.01 |
---|
78 | |
---|
79 | elif run_type == 3: |
---|
80 | outputdir_name += '_test_good_time_mesh' |
---|
81 | yieldstep = 0.1 |
---|
82 | finaltime = None |
---|
83 | maximum_triangle_area=0.001 |
---|
84 | elif run_type == 4: |
---|
85 | outputdir_name += '_good_tri_area_0.01_C' |
---|
86 | # this is not a test |
---|
87 | # Output will go to a file |
---|
88 | # The sww file will be interpolated |
---|
89 | yieldstep = 0.01 |
---|
90 | finaltime = None |
---|
91 | maximum_triangle_area=0.01 |
---|
92 | elif run_type == 5: |
---|
93 | outputdir_name += '_good_tri_area_0.001_C' |
---|
94 | # this is not a test |
---|
95 | # Output will go to a file |
---|
96 | # The sww file will be interpolated |
---|
97 | yieldstep = 0.01 |
---|
98 | finaltime = None |
---|
99 | maximum_triangle_area=0.001 |
---|
100 | |
---|
101 | metadata_dic = set_z_origin_to_water_depth(metadata_dic) |
---|
102 | |
---|
103 | pro_instance = project.Project(['data','flumes','Hinwood_2008'], |
---|
104 | outputdir_name=outputdir_name) |
---|
105 | print "The output dir is", pro_instance.outputdir |
---|
106 | copy_code_files(pro_instance.outputdir,__file__, |
---|
107 | dirname(project.__file__) \ |
---|
108 | + sep + project.__name__+'.py') |
---|
109 | copy (pro_instance.codedir + 'run_dam.py', |
---|
110 | pro_instance.outputdir + 'run_dam.py') |
---|
111 | copy (pro_instance.codedir + 'create_mesh.py', |
---|
112 | pro_instance.outputdir + 'create_mesh.py') |
---|
113 | |
---|
114 | boundary_final_time = prepare_time_boundary(metadata_dic, |
---|
115 | pro_instance.raw_data_dir, |
---|
116 | pro_instance.boundarydir) |
---|
117 | if finaltime is None: |
---|
118 | finaltime = boundary_final_time |
---|
119 | # Boundary file manipulation |
---|
120 | if boundary_path is None: |
---|
121 | boundary_path = pro_instance.boundarydir |
---|
122 | boundary_file_path = join(boundary_path, boundary_file) |
---|
123 | # # Convert the boundary file, .csv to .tsm |
---|
124 | # try: |
---|
125 | # temp = open(boundary_file_path) |
---|
126 | # temp.close() |
---|
127 | # except IOError: |
---|
128 | # prepare_time_boundary(boundary_file_path) |
---|
129 | |
---|
130 | mesh_filename = pro_instance.meshdir + basename + '.msh' |
---|
131 | |
---|
132 | #-------------------------------------------------------------------------- |
---|
133 | # Copy scripts to output directory and capture screen |
---|
134 | # output to file |
---|
135 | #-------------------------------------------------------------------------- |
---|
136 | |
---|
137 | # creates copy of code in output dir |
---|
138 | if run_type >= 2: |
---|
139 | #start_screen_catcher(pro_instance.outputdir, rank, pypar.size()) |
---|
140 | start_screen_catcher(pro_instance.outputdir) |
---|
141 | |
---|
142 | print 'USER: ', pro_instance.user |
---|
143 | #------------------------------------------------------------------------- |
---|
144 | # Create the triangular mesh |
---|
145 | #------------------------------------------------------------------------- |
---|
146 | |
---|
147 | # this creates the mesh |
---|
148 | #gate_position = 12.0 |
---|
149 | create_mesh.generate(mesh_filename, metadata_dic, |
---|
150 | maximum_triangle_area=maximum_triangle_area) |
---|
151 | |
---|
152 | head,tail = path.split(mesh_filename) |
---|
153 | copy (mesh_filename, |
---|
154 | pro_instance.outputdir + tail ) |
---|
155 | #------------------------------------------------------------------------- |
---|
156 | # Setup computational domain |
---|
157 | #------------------------------------------------------------------------- |
---|
158 | domain = Domain(mesh_filename, use_cache = False, verbose = True) |
---|
159 | |
---|
160 | |
---|
161 | print 'Number of triangles = ', len(domain) |
---|
162 | print 'The extent is ', domain.get_extent() |
---|
163 | print domain.statistics() |
---|
164 | |
---|
165 | |
---|
166 | domain.set_name(basename) |
---|
167 | domain.set_datadir(pro_instance.outputdir) |
---|
168 | domain.set_quantities_to_be_stored(['stage', 'xmomentum', 'ymomentum']) |
---|
169 | domain.set_minimum_storable_height(0.001) |
---|
170 | #domain.set_store_vertices_uniquely(True) # for writting to sww |
---|
171 | |
---|
172 | #------------------------------------------------------------------------- |
---|
173 | # Setup initial conditions |
---|
174 | #------------------------------------------------------------------------- |
---|
175 | |
---|
176 | domain.set_quantity('stage', 0.) #the origin is the still water level |
---|
177 | domain.set_quantity('friction', friction) |
---|
178 | elevation_function = Elevation_function(metadata_dic) |
---|
179 | domain.set_quantity('elevation', elevation_function) |
---|
180 | |
---|
181 | |
---|
182 | print 'Available boundary tags', domain.get_boundary_tags() |
---|
183 | |
---|
184 | # Create boundary function from timeseries provided in file |
---|
185 | #function = file_function(project.boundary_file, domain, verbose=True) |
---|
186 | #Bts = Transmissive_Momentum_Set_Stage_boundary(domain, function) |
---|
187 | try: |
---|
188 | function = file_function(boundary_file_path, domain, |
---|
189 | verbose=True) |
---|
190 | except IOError: |
---|
191 | msg = 'Run prepare_time_boundary.py. File "%s" could not be opened.'\ |
---|
192 | %(pro_instance.boundary_file) |
---|
193 | raise msg |
---|
194 | |
---|
195 | Br = Reflective_boundary(domain) |
---|
196 | Bd = Dirichlet_boundary([0.3,0,0]) |
---|
197 | Bts = Time_boundary(domain, function) |
---|
198 | domain.set_boundary( {'wall': Br, 'wave': Bts} ) |
---|
199 | #domain.set_boundary( {'wall': Br, 'wave': Bd} ) |
---|
200 | |
---|
201 | #------------------------------------------------------------------------- |
---|
202 | # Evolve system through time |
---|
203 | #------------------------------------------------------------------------- |
---|
204 | t0 = time.time() |
---|
205 | |
---|
206 | # It seems that ANUGA can't handle a starttime that is >0. |
---|
207 | domain.starttime = 1.0 |
---|
208 | for t in domain.evolve(yieldstep, finaltime): |
---|
209 | domain.write_time() |
---|
210 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
211 | print 'finished' |
---|
212 | |
---|
213 | flume_y_middle = 0.5 |
---|
214 | points = [] |
---|
215 | for gauge_x in metadata_dic['gauge_x']: |
---|
216 | points.append([gauge_x, flume_y_middle]) |
---|
217 | print "points",points |
---|
218 | |
---|
219 | |
---|
220 | #------------------------------------------------------------------------- |
---|
221 | # Calculate gauge info |
---|
222 | #------------------------------------------------------------------------- |
---|
223 | |
---|
224 | if run_type >= 1: |
---|
225 | id = metadata_dic['scenario_id'] + ".csv" |
---|
226 | interpolate_sww2csv(pro_instance.outputdir + basename +".sww", |
---|
227 | points, |
---|
228 | pro_instance.outputdir + "depth_" + id, |
---|
229 | pro_instance.outputdir + "velocity_x_" + id, |
---|
230 | pro_instance.outputdir + "velocity_y_" + id, |
---|
231 | pro_instance.outputdir + "stage_" + id) |
---|
232 | |
---|
233 | return pro_instance |
---|
234 | |
---|
235 | def set_z_origin_to_water_depth(seabed_coords): |
---|
236 | offset = seabed_coords['offshore_water_depth'] |
---|
237 | keys = ['xleft', 'xtoe', 'xbeach', 'xright'] |
---|
238 | for x in keys: |
---|
239 | seabed_coords[x][1] -= offset |
---|
240 | return seabed_coords |
---|
241 | #------------------------------------------------------------- |
---|
242 | if __name__ == "__main__": |
---|
243 | |
---|
244 | from scenarios import scenarios |
---|
245 | from slope import gauges_for_slope |
---|
246 | #from plot import plot |
---|
247 | |
---|
248 | |
---|
249 | # 4 is 0.01 |
---|
250 | # 5 is 0.001 |
---|
251 | run_type = 1 |
---|
252 | #for run_data in [scenarios[5]]: |
---|
253 | for run_data in scenarios: |
---|
254 | pro_instance = main( run_data['scenario_id'] + '_boundary.tsm' , |
---|
255 | run_data, |
---|
256 | run_type = run_type, |
---|
257 | outputdir_name=run_data['scenario_id']) |
---|
258 | gauges_for_slope(pro_instance.outputdir,[run_data]) |
---|