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, Time_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 = 220 |
---|
95 | N = 55 |
---|
96 | |
---|
97 | |
---|
98 | print 'Creating domain' |
---|
99 | #Create basic mesh |
---|
100 | points, vertices, boundary = rectangular(N, N) |
---|
101 | |
---|
102 | #Create shallow water domain |
---|
103 | domain = Domain(points, vertices, boundary) |
---|
104 | |
---|
105 | domain.check_integrity() |
---|
106 | domain.default_order = 2 |
---|
107 | domain.smooth = True |
---|
108 | domain.reduction = min #Looks a lot better on top of steep slopes |
---|
109 | |
---|
110 | print "Number of triangles = ", len(domain) |
---|
111 | |
---|
112 | |
---|
113 | if N > 50: |
---|
114 | domain.visualise = False |
---|
115 | domain.checkpoint = False |
---|
116 | domain.store = True #Store for visualisation purposes |
---|
117 | domain.format = 'sww' #Native netcdf visualisation format |
---|
118 | import sys, os |
---|
119 | root, ext = os.path.splitext(sys.argv[0]) |
---|
120 | if domain.smooth is True: |
---|
121 | s = 'smooth' |
---|
122 | else: |
---|
123 | s = 'nonsmooth' |
---|
124 | domain.filename = root + '_' + s |
---|
125 | else: |
---|
126 | domain.visualise = True |
---|
127 | domain.checkpoint = False |
---|
128 | domain.store = False |
---|
129 | |
---|
130 | |
---|
131 | #Set bed-slope and friction |
---|
132 | inflow_stage = 0.08 |
---|
133 | manning = 0.0 |
---|
134 | Z = Weir(inflow_stage) |
---|
135 | |
---|
136 | print 'Field values' |
---|
137 | domain.set_quantity('elevation', Z) |
---|
138 | domain.set_quantity('friction', manning) |
---|
139 | |
---|
140 | |
---|
141 | ###################### |
---|
142 | # Boundary conditions |
---|
143 | # |
---|
144 | print 'Boundaries' |
---|
145 | Br = Reflective_boundary(domain) |
---|
146 | Bt = Transmissive_boundary(domain) |
---|
147 | |
---|
148 | #Constant inflow |
---|
149 | Bd = Dirichlet_boundary([2*inflow_stage, 0.0, 0.0]) |
---|
150 | |
---|
151 | |
---|
152 | #Time dependent inflow |
---|
153 | from math import sin, pi, exp |
---|
154 | Bw = Time_boundary(domain=domain, |
---|
155 | f=lambda t: [0.7*(1 + sin(t*pi/2.5))*\ |
---|
156 | (inflow_stage*(sin(t*pi)+0.7)),0,0]) |
---|
157 | |
---|
158 | #Set boundary conditions |
---|
159 | #domain.set_boundary({'left': Bw, 'right': Br, 'bottom': Br, 'top': Br}) |
---|
160 | domain.set_boundary({'left': Bd, 'right': Bt, 'bottom': Bt, 'top': Bt}) |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | ###################### |
---|
165 | #Initial condition |
---|
166 | # |
---|
167 | print 'Initial condition' |
---|
168 | domain.set_quantity('level', Constant_height(Z, 0.)) |
---|
169 | |
---|
170 | #Evolve |
---|
171 | import time |
---|
172 | t0 = time.time() |
---|
173 | |
---|
174 | for t in domain.evolve(yieldstep = 0.01, finaltime = 1.0): |
---|
175 | domain.write_time() |
---|
176 | |
---|
177 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
178 | |
---|