1 | """Example of shallow water wave equation. |
---|
2 | |
---|
3 | This is called Netherlands because it shows a dam with a gap in it and |
---|
4 | stylised housed behind it and below the water surface. |
---|
5 | |
---|
6 | """ |
---|
7 | |
---|
8 | ###################### |
---|
9 | # Module imports |
---|
10 | # |
---|
11 | from shallow_water import Domain, Reflective_boundary, Dirichlet_boundary,\ |
---|
12 | Transmissive_boundary, Constant_height |
---|
13 | |
---|
14 | from mesh_factory import rectangular |
---|
15 | from Numeric import array |
---|
16 | |
---|
17 | |
---|
18 | class Weir: |
---|
19 | """Set a bathymetry for simple weir with a hole. |
---|
20 | x,y are assumed to be in the unit square |
---|
21 | """ |
---|
22 | |
---|
23 | def __init__(self, stage): |
---|
24 | self.inflow_stage = stage |
---|
25 | |
---|
26 | def __call__(self, x, y): |
---|
27 | from Numeric import zeros, Float |
---|
28 | |
---|
29 | N = len(x) |
---|
30 | assert N == len(y) |
---|
31 | |
---|
32 | z = zeros(N, Float) |
---|
33 | for i in range(N): |
---|
34 | z[i] = -x[i]/20 #General slope |
---|
35 | |
---|
36 | #Flattish bit to the left |
---|
37 | if x[i] <= 0.3: |
---|
38 | #z[i] = -x[i]/5 |
---|
39 | z[i] = -x[i]/20 |
---|
40 | |
---|
41 | |
---|
42 | #Weir |
---|
43 | if x[i] > 0.3 and x[i] < 0.4: |
---|
44 | z[i] = -x[i]/20+1.2 |
---|
45 | |
---|
46 | #Dip |
---|
47 | #if x[i] > 0.6 and x[i] < 0.9: |
---|
48 | # z[i] = -x[i]/20-0.5 #-y[i]/5 |
---|
49 | |
---|
50 | #Hole in weir |
---|
51 | #if x[i] > 0.3 and x[i] < 0.4 and y[i] > 0.2 and y[i] < 0.4: |
---|
52 | if x[i] > 0.3 and x[i] < 0.4 and y[i] > 0.4 and y[i] < 0.6: |
---|
53 | #z[i] = -x[i]/5 |
---|
54 | z[i] = -x[i]/20 |
---|
55 | |
---|
56 | #Poles |
---|
57 | #if x[i] > 0.65 and x[i] < 0.8 and y[i] > 0.55 and y[i] < 0.65 or\ |
---|
58 | # x[i] > 0.75 and x[i] < 0.9 and y[i] > 0.35 and y[i] < 0.45: |
---|
59 | # z[i] = -x[i]/20+0.4 |
---|
60 | |
---|
61 | if (x[i] - 0.72)**2 + (y[i] - 0.6)**2 < 0.05**2:# or\ |
---|
62 | #x[i] > 0.75 and x[i] < 0.9 and y[i] > 0.35 and y[i] < 0.45: |
---|
63 | z[i] = -x[i]/20+0.4 |
---|
64 | |
---|
65 | #Wall |
---|
66 | if x[i] > 0.995: |
---|
67 | z[i] = -x[i]/20+0.3 |
---|
68 | |
---|
69 | return z/2 |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | ###################### |
---|
74 | # Domain |
---|
75 | # |
---|
76 | |
---|
77 | N = 250 |
---|
78 | #N= 8 |
---|
79 | N = 16 |
---|
80 | #N = 4 |
---|
81 | #N = 102 |
---|
82 | N = 25 |
---|
83 | N = 16 |
---|
84 | N = 60 |
---|
85 | N = 150 #size = 45000 |
---|
86 | N = 130 #size = 33800 |
---|
87 | #N = 60 |
---|
88 | #N = 40 |
---|
89 | N = 260 |
---|
90 | #N = 150 |
---|
91 | N = 264 |
---|
92 | |
---|
93 | N = 600 #Size = 720000 |
---|
94 | N = 20 |
---|
95 | #N = 150 |
---|
96 | N = 110 |
---|
97 | N = 60 |
---|
98 | |
---|
99 | N = 40 |
---|
100 | #N = 140 |
---|
101 | #N = 15 |
---|
102 | |
---|
103 | print 'Creating domain' |
---|
104 | #Create basic mesh |
---|
105 | points, vertices, boundary = rectangular(N, N) |
---|
106 | |
---|
107 | #Create shallow water domain |
---|
108 | domain = Domain(points, vertices, boundary) |
---|
109 | |
---|
110 | domain.check_integrity() |
---|
111 | domain.default_order = 2 |
---|
112 | #domain.beta_h=0 |
---|
113 | |
---|
114 | #Output params |
---|
115 | domain.smooth = True |
---|
116 | domain.reduction = min #Looks a lot better on top of steep slopes |
---|
117 | |
---|
118 | print "Number of triangles = ", len(domain) |
---|
119 | |
---|
120 | |
---|
121 | if N > 40: |
---|
122 | domain.visualise = False |
---|
123 | domain.checkpoint = False |
---|
124 | domain.store = True #Store for visualisation purposes |
---|
125 | domain.format = 'sww' #Native netcdf visualisation format |
---|
126 | import sys, os |
---|
127 | #FIXME: This was os.path.splitext but caused weird filenames based on root |
---|
128 | base = os.path.basename(sys.argv[0]) |
---|
129 | domain.filename, _ = os.path.splitext(base) |
---|
130 | else: |
---|
131 | domain.visualise = False |
---|
132 | domain.checkpoint = False |
---|
133 | domain.store = False |
---|
134 | |
---|
135 | |
---|
136 | #Set bed-slope and friction |
---|
137 | inflow_stage = 0.08 |
---|
138 | manning = 0.02 |
---|
139 | Z = Weir(inflow_stage) |
---|
140 | |
---|
141 | print 'Field values' |
---|
142 | domain.set_quantity('elevation', Z) |
---|
143 | domain.set_quantity('friction', manning) |
---|
144 | |
---|
145 | |
---|
146 | ###################### |
---|
147 | # Boundary conditions |
---|
148 | # |
---|
149 | print 'Boundaries' |
---|
150 | Br = Reflective_boundary(domain) |
---|
151 | Bt = Transmissive_boundary(domain) |
---|
152 | |
---|
153 | #Constant inflow |
---|
154 | Bd = Dirichlet_boundary([2*inflow_stage, 0.0, 0.0]) |
---|
155 | |
---|
156 | |
---|
157 | #Set boundary conditions |
---|
158 | domain.set_boundary({'left': Bd, 'right': Br, 'bottom': Br, 'top': Br}) |
---|
159 | |
---|
160 | |
---|
161 | ###################### |
---|
162 | #Initial condition |
---|
163 | # |
---|
164 | print 'Initial condition' |
---|
165 | domain.set_quantity('stage', Constant_height(Z, 0.)) |
---|
166 | |
---|
167 | |
---|
168 | visualize = True |
---|
169 | if visualize: |
---|
170 | from anuga.visualiser import RealtimeVisualiser |
---|
171 | vis = RealtimeVisualiser(domain) |
---|
172 | vis.render_quantity_height("elevation", zScale=100, offset = 5.0, dynamic=False) |
---|
173 | vis.render_quantity_height("stage", zScale =100, dynamic=True, opacity = 0.3) |
---|
174 | vis.colour_height_quantity('stage', (lambda q:q['stage'], -0.5, 0.5)) |
---|
175 | vis.start() |
---|
176 | |
---|
177 | time.sleep(2.0) |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | #Evolve |
---|
182 | import time |
---|
183 | t0 = time.time() |
---|
184 | |
---|
185 | for t in domain.evolve(yieldstep = 0.5, finaltime = 1.0): |
---|
186 | domain.write_time() |
---|
187 | |
---|
188 | if visualize: vis.update() |
---|
189 | |
---|
190 | if visualize: vis.evolveFinished() |
---|
191 | |
---|
192 | |
---|
193 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
194 | print 'time', domain.write_time() |
---|
195 | |
---|
196 | print domain.coordinates |
---|
197 | print '*****' |
---|
198 | print domain.vertex_coordinates |
---|
199 | print '*****' |
---|
200 | print domain.quantities['xmomentum'].centroid_values |
---|
201 | print '*****' |
---|
202 | print domain.quantities['xmomentum'].edge_values |
---|
203 | print '*****' |
---|
204 | print domain.quantities['stage'].vertex_values |
---|
205 | print '*****' |
---|
206 | print domain.quantities['stage'].explicit_update |
---|
207 | |
---|
208 | from shallow_water import * |
---|
209 | |
---|
210 | def compute_fluxes_python(domain): |
---|
211 | """Compute all fluxes and the timestep suitable for all volumes |
---|
212 | in domain. |
---|
213 | |
---|
214 | Compute total flux for each conserved quantity using "flux_function" |
---|
215 | |
---|
216 | Fluxes across each edge are scaled by edgelengths and summed up |
---|
217 | Resulting flux is then scaled by area and stored in |
---|
218 | explicit_update for each of the three conserved quantities |
---|
219 | stage, xmomentum and ymomentum |
---|
220 | |
---|
221 | The maximal allowable speed computed by the flux_function for each volume |
---|
222 | is converted to a timestep that must not be exceeded. The minimum of |
---|
223 | those is computed as the next overall timestep. |
---|
224 | |
---|
225 | Post conditions: |
---|
226 | domain.explicit_update is reset to computed flux values |
---|
227 | domain.timestep is set to the largest step satisfying all volumes. |
---|
228 | """ |
---|
229 | |
---|
230 | import sys |
---|
231 | from Numeric import zeros, Float |
---|
232 | |
---|
233 | N = domain.number_of_elements |
---|
234 | |
---|
235 | #Shortcuts |
---|
236 | Stage = domain.quantities['stage'] |
---|
237 | Xmom = domain.quantities['xmomentum'] |
---|
238 | Ymom = domain.quantities['ymomentum'] |
---|
239 | Bed = domain.quantities['elevation'] |
---|
240 | |
---|
241 | #Arrays |
---|
242 | stage = Stage.edge_values |
---|
243 | xmom = Xmom.edge_values |
---|
244 | ymom = Ymom.edge_values |
---|
245 | bed = Bed.edge_values |
---|
246 | |
---|
247 | stage_bdry = Stage.boundary_values |
---|
248 | xmom_bdry = Xmom.boundary_values |
---|
249 | ymom_bdry = Ymom.boundary_values |
---|
250 | |
---|
251 | flux = zeros((N,3), Float) #Work array for summing up fluxes |
---|
252 | |
---|
253 | #Loop |
---|
254 | timestep = float(sys.maxint) |
---|
255 | for k in range(N): |
---|
256 | |
---|
257 | for i in range(3): |
---|
258 | #Quantities inside volume facing neighbour i |
---|
259 | ql = [stage[k, i], xmom[k, i], ymom[k, i]] |
---|
260 | zl = bed[k, i] |
---|
261 | |
---|
262 | #Quantities at neighbour on nearest face |
---|
263 | n = domain.neighbours[k,i] |
---|
264 | if n < 0: |
---|
265 | m = -n-1 #Convert negative flag to index |
---|
266 | qr = [stage_bdry[m], xmom_bdry[m], ymom_bdry[m]] |
---|
267 | zr = zl #Extend bed elevation to boundary |
---|
268 | else: |
---|
269 | m = domain.neighbour_edges[k,i] |
---|
270 | qr = [stage[n, m], xmom[n, m], ymom[n, m]] |
---|
271 | zr = bed[n, m] |
---|
272 | |
---|
273 | |
---|
274 | #Outward pointing normal vector |
---|
275 | normal = domain.normals[k, 2*i:2*i+2] |
---|
276 | |
---|
277 | #Flux computation using provided function |
---|
278 | edgeflux, max_speed = flux_function(normal, ql, qr, zl, zr) |
---|
279 | |
---|
280 | flux[k,:] = edgeflux |
---|
281 | |
---|
282 | return flux |
---|
283 | |
---|
284 | flux = compute_fluxes_python(domain) |
---|
285 | print 'flux' |
---|
286 | print flux |
---|
287 | |
---|
288 | |
---|
289 | # THis was pulled out of |
---|