-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsw2d_curved.py
More file actions
executable file
·303 lines (222 loc) · 7.04 KB
/
sw2d_curved.py
File metadata and controls
executable file
·303 lines (222 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/python3
'''
Copyright (C) 2017-2019 Waterloo Quantitative Consulting Group, Inc.
See COPYING and LICENSE files at project root for more details.
'''
import numpy as np
import pyblitzdg as dg
import matplotlib.pyplot as plt
from pprint import pprint
from scipy.interpolate import splev, splrep, interp1d, griddata
from swhelpers.flux import sw2dComputeFluxes
from swhelpers.maps import makeMapsPeriodic, correctBCTable
from swhelpers.rhs import sw2dComputeRHS_curved
from swhelpers.limiters import surfaceReconstruction, positivityPreservingLimiter2D
from meshhelpers.curved import adjustStraightEdges, deformAndBlendElements
# Main solver:
# Set scaled density jump.
drho = 1.00100 - 1.000
# compute reduced gravity
g = drho*9.81
# Set depth.
H0 = 7.5
c0 = np.sqrt(g*H0)
finalTime = 24*3600
numOuts = 200
t = 0.0
meshManager = dg.MeshManager()
meshManager.readMesh('input/headlands_highres.msh')
Verts = meshManager.vertices
EToV = meshManager.elements
bcType = meshManager.bcType
# 2 = outflow.
bcType = correctBCTable(bcType, EToV, Verts, 2)
meshManager.setBCType(bcType)
# Numerical parameters:
NOrder = 4
filtOrder = 4
filtCutoff = 0.9*NOrder
nodes = dg.TriangleNodesProvisioner(NOrder, meshManager)
nodes.buildFilter(filtCutoff, filtOrder)
outputter = dg.VtkOutputter(nodes)
ctx = nodes.dgContext()
x = ctx.x
y = ctx.y
indN, indK = np.where(np.hypot(x, y) < 1.9e1)
centreIndN = indN[0]
centreIndK = indK[0]
xFlat = x.flatten('F')
yFlat = y.flatten('F')
mapW = ctx.BCmap[3]
vmapW = ctx.vmapM[mapW]
xW = xFlat[vmapW]
yW = yFlat[vmapW]
topInds = np.logical_and(yW > 200, np.logical_and(xW > 3250, xW < 4750))
xtop = xW[topInds]
ytop = yW[topInds]
isort = np.argsort(xtop)
xtop2 = xtop[isort]
ytop2 = ytop[isort]
# build arc-length parameter for headlands curve.
s = [0.0]
for i in range(1, len(xtop2)):
d = np.hypot(xtop2[i]-xtop2[i-1], ytop2[i]-ytop2[i-1])
s.append(s[i-1] + d)
s = np.array(s)
s256 = np.linspace(s[0], s[-1], 128)
ss = np.linspace(s[0], s[-1], 4096)
xx = griddata(s, xtop2, s256, 'linear')
yy = griddata(s, ytop2, s256, 'linear')
# build parametric curve of 'top curve'/headlands curve.
splx = splrep(s256, xx)
sply = splrep(s256, yy)
xTopSmooth = splev(ss, splx, ext=2)
yTopSmooth = splev(ss, sply, ext=2)
bcInds = np.where(bcType.flatten('F') > 0)
bcFaces = np.transpose(np.unravel_index(bcInds, (ctx.numElements, ctx.numFaces), order='F'))
Verts, modifiedVerts, curvedFaces = adjustStraightEdges(Verts, EToV, bcFaces, xTopSmooth, yTopSmooth, ctx)
x, y, curvedEls = deformAndBlendElements(Verts, EToV, curvedFaces, xTopSmooth, yTopSmooth, ss, splx, sply, ctx, NOrder)
print("Updating physical coordinates")
nodes.setCoordinates(x, y)
xr = np.dot(ctx.Dr, x)
yr = np.dot(ctx.Dr, y)
xs = np.dot(ctx.Ds, x)
ys = np.dot(ctx.Ds, y)
J = xr*ys - xs*yr
gauss_ctx = nodes.buildGaussFaceNodes(2*(NOrder+1))
BCmap = ctx.BCmap
mapW = ctx.BCmap[3]; mapO = ctx.BCmap[2]
vmapO = ctx.vmapM[mapO]; vmapW = ctx.vmapM[mapW]
xFlat = x.flatten('F'); yFlat = y.flatten('F')
gxFlat = np.dot(gauss_ctx.Interp, x).flatten('F')
gyFlat = np.dot(gauss_ctx.Interp, y).flatten('F')
gmapO = np.array(gauss_ctx.BCmap[2])
xO = xFlat[vmapO]
yO = yFlat[vmapO]
gxO = gxFlat[gmapO]
gyO = gyFlat[gmapO]
vmapM = ctx.vmapM
vmapP = ctx.vmapP
gmapM = gauss_ctx.mapM
gmapP = gauss_ctx.mapP
vmapM, vmapP = makeMapsPeriodic(vmapM, vmapP, vmapO, xFlat, yFlat, xO, yO)
gmapM, gmapP = makeMapsPeriodic(gmapM, gmapP, gmapO, gxFlat, gyFlat, gxO, gyO)
print("building cubature context")
cub_ctx = nodes.buildCubatureVolumeMesh(3*(NOrder+1))
print("done.")
Np = ctx.numLocalPoints
K = ctx.numElements
Filt = ctx.filter
f=7.8825e-5
amp = 0.5*.065*H0 # had 0.065*H0
L = 500
W = 200
eta = amp*np.exp(-((y-L)/W)**2)
etay = -2*amp*(y-L)*np.exp(-((y-L)/W)**2) / W**2
u = (-g/f)*etay
vort = -(ctx.ry*np.dot(ctx.Dr, u) + ctx.sy*np.dot(ctx.Ds, u))
# detect damping regions, and use to determine drag coefficient CD
xW = xFlat[vmapW]
yW = yFlat[vmapW]
CD = np.zeros((Np, K))
CD_max = 2.5e-3
CDflat = np.zeros((Np, K)).flatten('F')
length_tol = 2.5e2
for i, _ in enumerate(xFlat):
xi = xFlat[i]
yi = yFlat[i]
dists = np.hypot(xi - xW, yi - yW)
min_dist = np.min(dists)
CDflat[i] = CD_max*0.5*(1-np.tanh((min_dist - 0.5*length_tol)/(0.1*length_tol)))
CD = np.array(np.reshape(CDflat, (Np, K), order='F'), order='C')
umax = np.max(u.flatten('F'))
print("umax: ", umax)
print("rad:" , c0/f)
print("froude: ", umax/c0)
v = 0*eta
r = np.sqrt(x*x + y*y)
H = H0*np.ones((Np,K))
Dr = ctx.Dr
Ds = ctx.Ds
rx = ctx.rx
ry = ctx.ry
sx = ctx.sx
sy = ctx.sy
z = -H
zx = (rx*np.dot(Dr, z) + sx*np.dot(Ds, z))
zy = (ry*np.dot(Dr, z) + sy*np.dot(Ds, z))
Nrad = 3e2
Nx = 4000.0
Ny = 350.0
N = np.exp(-(((x-Nx)/Nrad)**2 + ((y-Ny)/Nrad)**2))
h = H + eta
#h = 5*(0.5*(1 - np.tanh(x/Nrad)))
h[h < 1e-3] = 1e-3
#H = h - eta
eta = h - H
hu = h*u
hv = h*v
hN = h*N
# setup fields dictionary for outputting.
fields = dict()
fields["eta"] = eta
fields["u"] = u
fields["v"] = v
fields["B"] = N
fields["h"] = h
fields["vort"] = vort
outputter.writeFieldsToFiles(fields, 0)
Hbar = np.mean(H)
c = np.sqrt(g*Hbar)*np.ones((Np, K))
CFL = 0.75
dt = CFL / np.max( ((NOrder+1)**2)*0.5*np.abs(ctx.Fscale.flatten('F'))*(c.flatten('F')[vmapM] + np.sqrt(((u.flatten('F'))[vmapM])**2 + ((v.flatten('F'))[vmapM])**2)))
print("dt=", str(dt))
# dt = 1.1
#dt = 0.05
numSteps = int(np.ceil(finalTime/dt))
outputInterval = 50
step = 0
print("Entering main time-loop")
fields = dict()
while t < finalTime:
(RHS1,RHS2,RHS3,RHS4) = sw2dComputeRHS_curved(h, hu, hv, hN, zx, zy, g, H, f, CD, ctx, cub_ctx, gauss_ctx, curvedEls, J, gmapM, gmapP)
RHS1 = np.dot(Filt, RHS1)
RHS2 = np.dot(Filt, RHS2)
RHS3 = np.dot(Filt, RHS3)
RHS4 = np.dot(Filt, RHS4)
# predictor
h1 = h + 0.5*dt*RHS1
hu1 = hu + 0.5*dt*RHS2
hv1 = hv + 0.5*dt*RHS3
hN1 = hN + 0.5*dt*RHS4
#h1, hu1, hv1 = positivityPreservingLimiter2D(h1, hu1, hv1)
#h1[h1 < 1e-3] = 1e-3
(RHS1,RHS2,RHS3,RHS4) = sw2dComputeRHS_curved(h1, hu1, hv1, hN1, zx, zy, g, H, f, CD, ctx, cub_ctx, gauss_ctx, curvedEls, J, gmapM, gmapP)
RHS1 = np.dot(Filt, RHS1)
RHS2 = np.dot(Filt, RHS2)
RHS3 = np.dot(Filt, RHS3)
RHS4 = np.dot(Filt, RHS4)
# corrector - Update solution
h += dt*RHS1
hu += dt*RHS2
hv += dt*RHS3
hN += dt*RHS4
t += dt
u = hu / h
v = hv / h
dt = CFL / np.max( ((NOrder+1)**2)*0.5*np.abs(ctx.Fscale.flatten('F'))*(c.flatten('F')[vmapM] + np.sqrt(((u.flatten('F'))[vmapM])**2 + ((v.flatten('F'))[vmapM])**2)))
h_max = np.max(np.abs(h))
if h_max > 1e8 or np.isnan(h_max):
raise Exception("A numerical instability has occurred.")
step += 1
if step % outputInterval == 0 or step == numSteps:
print('Outputting at t=' + str(t))
eta = h-H
vort = ((ctx.rx*np.dot(ctx.Dr, v) + ctx.sx*np.dot(ctx.Ds, v)) -
(ctx.ry*np.dot(ctx.Dr, u) + ctx.sy*np.dot(ctx.Ds, u)))
fields["eta"] = eta
fields["u"] = hu/h
fields["v"] = hv/h
fields["B"] = hN/h
fields["vort"] = vort
outputter.writeFieldsToFiles(fields, step)