-
Notifications
You must be signed in to change notification settings - Fork 2
/
stepUVE.py
255 lines (217 loc) · 12.9 KB
/
stepUVE.py
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
import numpy as np
from oceanState import *
import math
import threading
from utils import getUV, calcMean
from superbeeAdv import superbeeAdv
from biharmon import biharmon
def isplit(imax,n):
intervals = []
lastEnd = 0
for i in range(0,n):
start = lastEnd
lastEnd = math.floor(imax*(i+1)/n)
intervals.append((start,lastEnd))
return intervals
def stepUVE(os, sp):
imax = os.imax
jmax = os.jmax
kmax = os.kmax
# Based on the values in os.W[i,j,0] - the vertical speeds calculated at the top edge
# of the surface layer cells, calculate the updated elevation:
os.E_next[1:-1,1:-1] = os.E[1:-1,1:-1] + sp.dt*os.W[1:-1,1:-1,0]
# Create temporary arrays:
p_above = sp.p_atm0*np.ones((imax,jmax))
p_diff = np.zeros((imax,jmax))
p_gradx = np.zeros((imax-1,jmax,kmax))
p_grady = np.zeros((imax, jmax-1, kmax))
# Calculate pressure gradients:
for k in range(0,kmax):
for i in range(0,imax):
for j in range(0,jmax):
if k<os.kmm[i,j]:
p_diff[i,j] = os.cellHeights[i,j,k]*9.81*os.rho[i,j,k]
else:
p_diff[i,j] = 0
for i in range(0,imax-1):
for j in range(0,jmax):
if os.maskU[i,j,k]>0:
# height measured from below:
meanCellHeight = 0.5 * (os.cellHeights[i,j,k] + os.cellHeights[i+1,j,k]);
if k==0: # Surface layer
p_gradx[i,j,k] = (p_above[i+1,j] + p_diff[i+1,j]*(os.cellHeights[i+1,j,k]-0.5*meanCellHeight)/os.cellHeights[i+1,j,k] \
- (p_above[i,j] + p_diff[i,j]*(os.cellHeights[i,j,k]-0.5*meanCellHeight)/os.cellHeights[i,j,k])) / sp.dx;
else: # Mid or bottom layer:
p_gradx[i,j,k] = (p_above[i+1,j] + p_diff[i+1,j]*0.5*meanCellHeight/os.cellHeights[i+1,j,k] \
- (p_above[i,j] + p_diff[i,j]* 0.5*meanCellHeight/os.cellHeights[i,j,k])) / sp.dx
for i in range(0,imax):
for j in range(0,jmax-1):
if os.maskV[i,j,k]>0:
# height measured from below:
meanCellHeight = 0.5 * (os.cellHeights[i,j,k] + os.cellHeights[i,j+1,k]);
if k==0: # Surface layer
p_grady[i,j,k] = (p_above[i,j+1] + p_diff[i,j+1]*(os.cellHeights[i,j+1,k]-0.5*meanCellHeight)/os.cellHeights[i,j+1,k] \
- (p_above[i,j] + p_diff[i,j]*(os.cellHeights[i,j,k]-0.5*meanCellHeight)/os.cellHeights[i,j,k])) / sp.dx;
else: # Mid or bottom layer:
p_grady[i,j,k] = (p_above[i,j+1] + p_diff[i,j+1]*0.5*meanCellHeight/os.cellHeights[i,j+1,k] \
- (p_above[i,j] + p_diff[i,j]* 0.5*meanCellHeight/os.cellHeights[i,j,k])) / sp.dx
p_above = p_above + p_diff
# Calculate horizontal accelerations in U direction:
for i in range(0,imax-1):
for j in range(0,jmax):
if os.kmm[i,j] >= 0:
for k in range(0, os.kmax):
if not os.maskU[i,j,k]:
os.U_next[i,j,k:os.kmax] = math.nan
break
# Get the value at this point:
val = os.U[i,j,k]
# Estimate the local V and W values by interpolation:
vMean = calcMean((getUV(os.V,i,j-1,k,math.nan), getUV(os.V,i,j,k,math.nan),
getUV(os.V,i+1,j-1, k, math.nan), getUV(os.V,i+1,j,k,math.nan)))
wMean = calcMean((getUV(os.W,i,j,k, math.nan), getUV(os.W,i+1,j,k, math.nan)))
# Estimate the local d2u/dz2 (double derivative):
if k>0:
dz_up = 0.5*(os.cellHeights[i,j,k]+os.cellHeights[i,j,k-1])
else:
dz_up = os.cellHeights[i,j,k]
if k<kmax-1:
dz_down = 0.5*(os.cellHeights[i,j,k]+os.cellHeights[i,j,k+1])
else:
dz_down = os.cellHeights[i,j,k]
d2u_dz2 = ((getUV(os.U,i,j,k-1,val) - val)/dz_up \
- (val - getUV(os.U,i,j,k+1,val))/dz_down)/(0.5*(dz_up+dz_down))
if sp.biharmonic:
# If biharmonic is activated the diffusion is handled later, so we
# can set it to 0 for now:
diffUV = 0
else:
# Estimate the local d2u/dx2 (double derivative):
d2u_dx2 = (getUV(os.U,i-1,j,k,val) - 2*val + getUV(os.U,i+1,j,k,val))/(sp.dx*sp.dx)
# Estimate the local d2u/dy2 (double derivative):
d2u_dy2 = (getUV(os.U,i,j-1,k,val) - 2*val + getUV(os.U,i,j+1,k,val))/(sp.dx*sp.dx)
# Calculate diffusion term:
diffUV = os.AH[i,j,k]*(d2u_dx2 + d2u_dy2)
# Calculate nonlinear (advective) terms:
if sp.advectiveTermsOn:
# Calculate the advection (nonlinear) terms using the
# Superbee flux limiter to limit oscillations while
# suppressing numerical diffusion:
advU = superbeeAdv(sp.dt, sp.dx, getUV(os.U,i-2,j,k,val), getUV(os.U,i-1,j,k,val), val,
getUV(os.U,i+1,j,k,val), getUV(os.U,i+2,j,k,val), val, val)
advV = superbeeAdv(sp.dt, sp.dx, getUV(os.U,i,j-2,k,val), getUV(os.U,i,j-1,k,val), val,
getUV(os.U,i,j+1,k,val), getUV(os.U,i,j+2,k,val), vMean, vMean)
advW = superbeeAdv(sp.dt, sp.dx, getUV(os.U,i,j,k-2,val), getUV(os.U,i,j,k-1,val), val,
getUV(os.U,i,j,k+1,val), getUV(os.U,i,j+2,k+2,val), wMean, wMean)
else:
advU = 0
advV = 0
advW = 0
# Sum up the terms and calculate next time step value:
os.U_next[i,j,k] = os.U[i,j,k] + sp.dt*(
- p_gradx[i,j,k]/sp.rho_0 # Pressure term
+ advU + advV + advW # Advective terms
+ sp.A_z*d2u_dz2 # Vertical eddy viscosity
+ diffUV # Horizontal diffusion
+ 2*sp.omega*math.sin(sp.phi0)*vMean) # Coriolis
#if np.isnan(os.U_next[i,j,k]) or np.isinf(os.U_next[i,j,k]) or np.isinf(-os.U_next[i,j,k]):
# print("nan")
# Calculate horizontal accelerations in V direction:
for i in range(0,imax):
for j in range(0,jmax-1):
if os.kmm[i,j] >= 0:
for k in range(0, os.kmm[i,j]):
if not os.maskV[i,j,k]:
os.V_next[i,j,k:os.kmax] = math.nan
break
# Get the value at this point:
val = os.V[i, j, k]
# Estimate the local U and W values by interpolation:
uMean = calcMean((getUV(os.U,i-1,j,k,math.nan), getUV(os.U,i,j,k,math.nan),
getUV(os.U, i-1, j+1,k,math.nan), getUV(os.U,i,j+1,k,math.nan)))
wMean = calcMean((getUV(os.W, i, j, k, math.nan), getUV(os.W, i, j+1, k, math.nan)))
# Estimate the local d2u/dz2 (double derivative):
if k>0:
dz_up = 0.5*(os.cellHeights[i,j,k]+os.cellHeights[i,j,k-1])
else:
dz_up = os.cellHeights[i,j,k]
if k<kmax-1:
dz_down = 0.5*(os.cellHeights[i,j,k]+os.cellHeights[i,j,k+1])
else:
dz_down = os.cellHeights[i,j,k]
d2u_dz2 = ((getUV(os.V,i,j,k-1,val) - val)/dz_up \
- (val - getUV(os.V,i,j,k+1,val))/dz_down)/(0.5*(dz_up+dz_down))
if sp.biharmonic:
# If biharmonic is activated the diffusion is handled later, so we
# can set it to 0 for now:
diffUV = 0
else:
# Estimate the local d2u/dx2 (double derivative):
d2u_dx2 = (getUV(os.V,i-1,j,k,val) - 2*val + getUV(os.V,i+1,j,k,val))/(sp.dx*sp.dx)
# Estimate the local d2u/dy2 (double derivative):
d2u_dy2 = (getUV(os.V,i,j-1,k,val) - 2*val + getUV(os.V,i,j+1,k,val))/(sp.dx*sp.dx)
# Calculate diffusion term:
diffUV = os.AH[i,j,k]*(d2u_dx2 + d2u_dy2)
# Calculate nonlinear (advective) terms:
if sp.advectiveTermsOn:
# Calculate the advection (nonlinear) terms using the
# Superbee flux limiter to limit oscillations while
# suppressing numerical diffusion:
advU = superbeeAdv(sp.dt, sp.dx, getUV(os.V,i-2,j,k,val), getUV(os.V,i-1,j,k,val), val,
getUV(os.V,i+1,j,k,val), getUV(os.V,i+2,j,k,val), val, val)
advV = superbeeAdv(sp.dt, sp.dx, getUV(os.V,i,j-2,k,val), getUV(os.V,i,j-1,k,val), val,
getUV(os.V,i,j+1,k,val), getUV(os.V,i,j+2,k,val), vMean, vMean)
advW = superbeeAdv(sp.dt, sp.dx, getUV(os.V,i,j,k-2,val), getUV(os.V,i,j,k-1,val), val,
getUV(os.V,i,j,k+1,val), getUV(os.V,i,j+2,k+2,val), wMean, wMean)
else:
advU = 0
advV = 0
advW = 0
# Sum up the terms and calculate next time step value:
os.V_next[i,j,k] = os.V[i,j,k] + sp.dt*(
- p_grady[i,j,k]/sp.rho_0 # Pressure term
+ advU + advV + advW # Advective terms
+ sp.A_z * d2u_dz2 # Vertical eddy viscosity
+ diffUV # Horizontal diffusion
-2*sp.omega*math.sin(sp.phi0)*uMean) # Coriolis
# If we are using biharmonic diffusion of velocities, do it here:
if sp.biharmonic:
(diffU, diffV) = biharmon(os, sp)
os.U_next = os.U_next - sp.dt*diffU
os.V_next = os.V_next - sp.dt*diffV
# Wind stress and bottom friction, U:
for i in range(0,os.imax-1):
for j in range(0,os.jmax):
if os.maskU[i,j,0] > 0: # Check if there is a valid current vector at this position
# Surface cell, average height on cell border:
dz_mean = 0.5*(os.cellHeights[i,j,0]+os.cellHeights[i+1,j,0])
os.U_next[i,j,0] = os.U_next[i,j,0] + sp.dt*sp.windStressCoeff*os.windU[i,j]/dz_mean
# Bottom friction. Apply at the minimum kmax of the
# neighbouring cells. We need to calculate the absolute value
# of the current speed here, based on U and interpolated V values:
k = min(os.kmm[i,j], os.kmm[i+1,j])-1
# Bottom cell, average height on cell border:
dz_mean = 0.5*(os.cellHeights[i,j,k]+os.cellHeights[i+1,j,k])
# V value interpolated here:
meanV = calcMean([getUV(os.V,i,j-1,k,math.nan), getUV(os.V,i+1,j-1,k,math.nan),
getUV(os.V,i,j,k,math.nan), getUV(os.V,i+1,j,k,math.nan)])
speed = math.sqrt(os.U[i,j,k]*os.U[i,j,k] + meanV*meanV);
os.U_next[i,j,k] = os.U_next[i,j,k] - sp.dt*sp.C_b*os.U[i,j,k]*speed/dz_mean
# Wind stress and bottom friction, V:
for i in range(0,os.imax):
for j in range(0,os.jmax-1):
if os.maskV[i,j,0] > 0: # Check if there is a valid current vector at this position
# Surface cell, average height on cell border:
dz_mean = 0.5*(os.cellHeights[i,j,0]+os.cellHeights[i,j+1,0])
os.V_next[i,j,0] = os.V_next[i,j,0] + sp.dt*sp.windStressCoeff*sp.windV[i,j]/dz_mean
# Bottom friction. Apply at the minimum kmax of the
# neighbouring cells. We need to calculate the absolute value
# of the current speed here, based on U and interpolated V values:
k = min(os.kmm[i,j], os.kmm[i,j+1])-1
# Bottom cell, average height on cell border:
dz_mean = 0.5*(os.cellHeights[i,j,k]+os.cellHeights[i,j+1,k])
# V value interpolated here:
meanU = calcMean([getUV(os.U,i-1,j,k,math.nan), getUV(os.U,i-1,j+1,k,math.nan),
getUV(os.U,i,j,k,math.nan), getUV(os.U,i,j+1,k,math.nan)])
speed = math.sqrt(os.V[i,j,k]*os.V[i,j,k] + meanV*meanV);
os.V_next[i,j,k] = os.V_next[i,j,k] - sp.dt*sp.C_b*os.V[i,j,k]*speed/dz_mean