1 | """Validation of the AnuGA implementation of the shallow water wave equation. |
---|
2 | |
---|
3 | This script sets up 2D circular island..... |
---|
4 | |
---|
5 | """ |
---|
6 | |
---|
7 | # Module imports |
---|
8 | from anuga.shallow_water import Domain |
---|
9 | from anuga.shallow_water import Reflective_boundary |
---|
10 | from anuga.shallow_water import Transmissive_Momentum_Set_Stage_boundary |
---|
11 | from anuga.shallow_water import Dirichlet_boundary, Time_boundary |
---|
12 | from anuga.shallow_water.data_manager import get_maximum_inundation_data |
---|
13 | from anuga.abstract_2d_finite_volumes.util import file_function |
---|
14 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
15 | from anuga.utilities.polygon import read_polygon, plot_polygons |
---|
16 | #from cmath import cos,sin,cosh,pi |
---|
17 | from math import cos,pi,sin,tan,sqrt |
---|
18 | from Numeric import array, zeros, Float, allclose |
---|
19 | |
---|
20 | #import project |
---|
21 | |
---|
22 | #------------------------- |
---|
23 | # Create Domain from mesh |
---|
24 | #------------------------- |
---|
25 | |
---|
26 | def get_xy(x=0,y=0,r=1,angle=1): |
---|
27 | x1 = x + cos(angle)*r |
---|
28 | y1 = y + sin(-angle)*r |
---|
29 | # print "get",x,y,r,x1,y1,"angle",angle, cos(angle),sin(angle) |
---|
30 | return x1,y1, |
---|
31 | |
---|
32 | def prepare_timeboundary(filename): |
---|
33 | """Converting benchmark 2 time series to NetCDF tms file. |
---|
34 | This is a 'throw-away' code taylor made for files like |
---|
35 | 'Benchmark_2_input.txt' from the LWRU2 benchmark |
---|
36 | """ |
---|
37 | |
---|
38 | textversion = filename[:-4] + '.txt' |
---|
39 | |
---|
40 | print 'Preparing time boundary from %s' %textversion |
---|
41 | from Numeric import array |
---|
42 | |
---|
43 | fid = open(textversion) |
---|
44 | |
---|
45 | #Skip first line |
---|
46 | line = fid.readline() |
---|
47 | |
---|
48 | #Read remaining lines |
---|
49 | lines = fid.readlines() |
---|
50 | fid.close() |
---|
51 | |
---|
52 | |
---|
53 | N = len(lines) |
---|
54 | T = zeros(N, Float) #Time |
---|
55 | Q = zeros(N, Float) #Values |
---|
56 | |
---|
57 | for i, line in enumerate(lines): |
---|
58 | fields = line.split() |
---|
59 | # print 'fields',i,line |
---|
60 | |
---|
61 | T[i] = float(fields[0]) |
---|
62 | Q[i] = float(fields[1]) |
---|
63 | |
---|
64 | |
---|
65 | #Create tms file |
---|
66 | from Scientific.IO.NetCDF import NetCDFFile |
---|
67 | |
---|
68 | print 'Writing to', filename |
---|
69 | fid = NetCDFFile(filename[:-4] + '.tms', 'w') |
---|
70 | |
---|
71 | fid.institution = 'Geoscience Australia' |
---|
72 | fid.description = 'Input wave for Benchmark 2' |
---|
73 | fid.starttime = 0.0 |
---|
74 | fid.createDimension('number_of_timesteps', len(T)) |
---|
75 | fid.createVariable('time', Float, ('number_of_timesteps',)) |
---|
76 | fid.variables['time'][:] = T |
---|
77 | |
---|
78 | fid.createVariable('stage', Float, ('number_of_timesteps',)) |
---|
79 | fid.variables['stage'][:] = Q[:] |
---|
80 | |
---|
81 | fid.createVariable('xmomentum', Float, ('number_of_timesteps',)) |
---|
82 | fid.variables['xmomentum'][:] = 0.0 |
---|
83 | |
---|
84 | fid.createVariable('ymomentum', Float, ('number_of_timesteps',)) |
---|
85 | fid.variables['ymomentum'][:] = 0.0 |
---|
86 | |
---|
87 | fid.close() |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | angles = (0.0, 22.5, 45.0, 67.5, 75.0, 80.0, 85.0, 87.5, 90.0, 92.5, |
---|
92 | 95.0, 100.0, 105.0, 112.5, 135.0, 157.5, 180.0, 202.5, 225.0, |
---|
93 | 247.5, 270.0, 292.5, 315.0, 337.5) |
---|
94 | angles1 = (0.0, 22.5, 45.0, 67.5, 90.0, 112.5, 135.0, 157.5, 180.0, 202.5, 225.0, |
---|
95 | 247.5, 270.0, 292.5, 315.0, 337.5) |
---|
96 | |
---|
97 | x = 12.96 |
---|
98 | y = 13.80 |
---|
99 | r1 = 1.5 |
---|
100 | r2 = 2.5 |
---|
101 | d=0.75 |
---|
102 | res=0.0005 |
---|
103 | |
---|
104 | poly = [] |
---|
105 | poly1 = [] |
---|
106 | internal_poly=[] |
---|
107 | for i,angle in enumerate(angles1): |
---|
108 | #convert Degs to Rads |
---|
109 | angle = ((angle-180)/180.0)*pi |
---|
110 | # p1 = get_xy(x,y,r1,angle-0.01745*d) |
---|
111 | # p2 = get_xy(x,y,r2,angle-0.01745*d) |
---|
112 | # p3 = get_xy(x,y,r2,angle+0.01745*d) |
---|
113 | # p4 = get_xy(x,y,r1,angle+0.01745*d) |
---|
114 | # poly[i] = [[p1[0],p1[1]],[p2[0],p2[1]],[p3[0],p3[1]],[p4[0],p4[1]]] |
---|
115 | |
---|
116 | p = get_xy(x,y,3.6,angle) |
---|
117 | poly1.append([p[0],p[1]]) |
---|
118 | |
---|
119 | poly.append(poly1) |
---|
120 | internal_poly.append([poly1,.1]) |
---|
121 | |
---|
122 | poly2 = [] |
---|
123 | for i,angle in enumerate(angles1): |
---|
124 | #convert Degs to Rads |
---|
125 | angle = ((angle-180)/180.0)*pi |
---|
126 | p = get_xy(x,y,2.4,angle) |
---|
127 | poly2.append([p[0],p[1]]) |
---|
128 | |
---|
129 | poly.append(poly2) |
---|
130 | internal_poly.append([poly2,.01]) |
---|
131 | |
---|
132 | poly3 = [] |
---|
133 | for i,angle in enumerate(angles1): |
---|
134 | #convert Degs to Rads |
---|
135 | angle = ((angle-180)/180.0)*pi |
---|
136 | p = get_xy(x,y,1.6,angle) |
---|
137 | poly3.append([p[0],p[1]]) |
---|
138 | |
---|
139 | poly.append(poly3) |
---|
140 | internal_poly.append([poly3,1]) |
---|
141 | |
---|
142 | poly4 = [] |
---|
143 | for i,angle in enumerate(angles1): |
---|
144 | #convert Degs to Rads |
---|
145 | angle = ((angle-180)/180.0)*pi |
---|
146 | p = get_xy(x,y,1.1,angle) |
---|
147 | poly4.append([p[0],p[1]]) |
---|
148 | |
---|
149 | poly.append(poly4) |
---|
150 | internal_poly.append([poly2,.01]) |
---|
151 | |
---|
152 | plot_polygons(poly,'poly.png') |
---|
153 | |
---|
154 | poly_all= [[0,7],[0,25],[30,25],[30,7]] |
---|
155 | create_mesh_from_regions(poly_all, |
---|
156 | boundary_tags={'wall': [0,2],'wave': [3], 'buffer':[1]}, |
---|
157 | maximum_triangle_area=1, |
---|
158 | interior_regions=internal_poly, |
---|
159 | filename='temp.msh', |
---|
160 | use_cache=False, |
---|
161 | verbose=True) |
---|
162 | |
---|
163 | domain = Domain('temp.msh', use_cache=True, verbose=True) |
---|
164 | print domain.statistics() |
---|
165 | |
---|
166 | #------------------------- |
---|
167 | # Initial Conditions |
---|
168 | #------------------------- |
---|
169 | domain.set_quantity('friction', 0.0) |
---|
170 | water_height = 0.322 |
---|
171 | domain.set_quantity('stage', water_height) |
---|
172 | |
---|
173 | def test(): |
---|
174 | fileName = "test.csv" |
---|
175 | file = open(fileName,"w") |
---|
176 | file.write("x,y,elevation \n") |
---|
177 | x = 12.96 |
---|
178 | y = 13.80 |
---|
179 | for i in range(0,30): |
---|
180 | for j in range(0,25): |
---|
181 | a = (x-i)**2 |
---|
182 | b = (y-j)**2 |
---|
183 | z = -sqrt((x-i)**2+(y-j)**2) |
---|
184 | z = z*.25+(0.8975) |
---|
185 | if z <0: |
---|
186 | z=0 |
---|
187 | if z > .625: |
---|
188 | z=0.625 |
---|
189 | |
---|
190 | # print 'x,y,f',i,j,a,b,z |
---|
191 | file.write("%s, %s, %s\n" %(i, j, z)) |
---|
192 | file.close() |
---|
193 | |
---|
194 | test() |
---|
195 | |
---|
196 | domain.set_quantity('elevation',filename = "test.csv", alpha=0.1) |
---|
197 | |
---|
198 | |
---|
199 | #------------------------- |
---|
200 | # Set simulation parameters |
---|
201 | #------------------------- |
---|
202 | domain.set_name('test1') # Name of output sww file |
---|
203 | domain.set_default_order(1) # Apply second order scheme |
---|
204 | domain.set_all_limiters(0.9) # Max second order scheme (old lim) |
---|
205 | domain.set_minimum_storable_height(0.01) # Don't store w < 0.001m |
---|
206 | domain.set_maximum_allowed_speed(0.1) # Allow a little runoff (0.1 is OK) |
---|
207 | domain.tight_slope_limiters = 1 |
---|
208 | domain.beta_h = 0.0 |
---|
209 | |
---|
210 | #Timings on AMD64-242 (beta_h=0) |
---|
211 | # tight_slope_limiters = 0: |
---|
212 | # 3035s - 3110s |
---|
213 | # tight_slope_limiters = 1: |
---|
214 | # 3000s - 3008s |
---|
215 | # |
---|
216 | # beta_h>0: In the order of 3200s |
---|
217 | |
---|
218 | #------------------------- |
---|
219 | # Boundary Conditions |
---|
220 | #------------------------- |
---|
221 | |
---|
222 | # Create boundary function from timeseries provided in file |
---|
223 | #function = file_function(project.boundary_filename, |
---|
224 | # domain, verbose=True) |
---|
225 | def waveform(t): |
---|
226 | return (sin(t*2*pi)) |
---|
227 | |
---|
228 | # Create and assign boundary objects |
---|
229 | boundary_filename="ts2cnew1_input" |
---|
230 | prepare_timeboundary(boundary_filename+'.txt') |
---|
231 | |
---|
232 | function = file_function(boundary_filename+'.tms', |
---|
233 | domain, verbose=True) |
---|
234 | |
---|
235 | # Create and assign boundary objects |
---|
236 | Bts = Transmissive_Momentum_Set_Stage_boundary(domain, function) |
---|
237 | |
---|
238 | #Bts = Transmissive_Momentum_Set_Stage_boundary(domain, waveform) |
---|
239 | Bw = Time_boundary(domain=domain, |
---|
240 | f=lambda t: [(0.1*sin(t*2*pi)), 0.0, 0.0]) |
---|
241 | Br = Reflective_boundary(domain) |
---|
242 | Bd = Dirichlet_boundary([water_height,0,0]) |
---|
243 | domain.set_boundary({'wave': Bts, 'wall': Br, 'buffer':Bd}) |
---|
244 | |
---|
245 | #------------------------- |
---|
246 | # Evolve through time |
---|
247 | #------------------------- |
---|
248 | import time |
---|
249 | t0 = time.time() |
---|
250 | |
---|
251 | for t in domain.evolve(yieldstep = 1, finaltime = 32): |
---|
252 | domain.write_time() |
---|
253 | # domain.write_time(track_speeds=False) |
---|
254 | |
---|
255 | for t in domain.evolve(yieldstep = 0.1, finaltime = 36 |
---|
256 | ,skip_initial_step = True): |
---|
257 | domain.write_time() |
---|
258 | |
---|
259 | for t in domain.evolve(yieldstep = 1, finaltime = 75 |
---|
260 | ,skip_initial_step = True): |
---|
261 | domain.write_time() |
---|
262 | |
---|
263 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
264 | |
---|
265 | |
---|
266 | |
---|
267 | #angles = (0.0, 22.5, 45.0, 67.5, 75.0, 80.0, 85.0, 87.5, 90.0, 92.5, |
---|
268 | # 95.0, 100.0, 105.0, 112.5, 135.0, 157.5, 180.0, 202.5, 225.0, |
---|
269 | # 247.5, 270.0, 292.5, 315.0, 337.5) |
---|
270 | #x = 12.96 |
---|
271 | #y = 13.80 |
---|
272 | #r1 = 1.1 |
---|
273 | #r2 = 3.6 |
---|
274 | #d=2 |
---|
275 | #poly = {} |
---|
276 | for i,angle in enumerate(angles): |
---|
277 | # angle = ((angle-180)/180.0)*pi |
---|
278 | ## angle = angle1*3.14157 |
---|
279 | # p1 = get_xy(x,y,r1,angle-0.01745*d) |
---|
280 | # p2 = get_xy(x,y,r2,angle-0.01745*d) |
---|
281 | # p3 = get_xy(x,y,r2,angle+0.01745*d) |
---|
282 | # p4 = get_xy(x,y,r1,angle+0.01745*d) |
---|
283 | # poly[i] = [[p1[0],p1[1]],[p2[0],p2[1]],[p3[0],p3[1]],[p4[0],p4[1]]] |
---|
284 | ## print i,poly[i] |
---|
285 | # |
---|
286 | |
---|
287 | run_up, x_y = get_maximum_inundation_data(filename='test.sww',polygon=poly[i], verbose=False) |
---|
288 | |
---|
289 | print 'maximum_inundation_data',angle, run_up, x_y |
---|
290 | |
---|
291 | |
---|
292 | |
---|
293 | |
---|
294 | |
---|
295 | |
---|