-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorph_lib.py
216 lines (164 loc) · 6.38 KB
/
morph_lib.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
''' Written as a function '''
import copy
import numpy as np
import math
from scipy.integrate import simps
def morphmodel_2ndO_upwind(x,zb,depth,qf,timesteps,dt,use_slope_correction=True):
dx = x[1] - x[0]
number_nodes = len(x)
z = np.ones(number_nodes) * depth
qbs = np.zeros(number_nodes)
grdqb = np.zeros((number_nodes,2))
#qf = 0.45
A = 1.0
u = qf/(z-zb)
print( 'here 2')
# This is a simple sediment transport model
sf = calculate_slope_factor(x,zb)
qbs = (A * u) * sf
print('Number of timesteps: %s' % timesteps)
for t in range(timesteps):
zbn = copy.deepcopy(zb)
for stage in range(2):
''' ----------------------------- '''
''' Calculate the derivative '''
''' ----------------------------- '''
for i in range(2,number_nodes-1):
''' Using a second order upwind difference equation '''
grdqb[i,stage] = (1./(6. * dx))*(2.*qbs[i+1] + 3.*qbs[i] - 6.*qbs[i-1] + qbs[i-2])
if abs(grdqb[i,stage]) > 0.01:
grdqb[i,stage] = (1./(dx))*(qbs[i]-qbs[i-1])
''' Update the boundary and near-boundary conditions '''
''' i = 1'''
grdqb[1,stage] = (1./(6. * dx))*(2.*qbs[2] + 3.*qbs[1] - 6.*qbs[0] + qbs[number_nodes-1])
''' i = 0'''
grdqb[0,stage] = (1./(6. * dx))*(2.*qbs[1] + 3.*qbs[0] - 6.*qbs[number_nodes-1] + qbs[number_nodes-2])
''' i = number_nodes -1 '''
grdqb[number_nodes-1,stage] = (1./(6. * dx))*(2.*qbs[0] + 3.*qbs[number_nodes-1] - 6.*qbs[number_nodes-2] + qbs[number_nodes-3])
for i in range(number_nodes):
if stage == 1:
zb[i] = zbn[i] - dt*grdqb[i,0]
else:
zb[i] = zbn[i] - 0.5*dt*(grdqb[i,0] + grdqb[i,1])
''' Update the velocity and bedload '''
h = z - zb
u = qf/h
qbs = (A * u)
if use_slope_correction == True:
sf = calculate_slope_factor_dey(x,zb)
qbs = qbs * sf
return x,zb,u,qbs
def morphmodel_upwind(x,zb,depth,qf,timesteps,dt,use_slope_correction=True):
dx = x[1] - x[0]
number_nodes = len(x)
z = np.ones(number_nodes) * depth
qbs = np.zeros(number_nodes)
grdqb = np.zeros((number_nodes,2))
#qf = 0.45
A = 1.0
u = qf/(z-zb)
# This is a simple sediment transport model
sf = calculate_slope_factor(x,zb)
qbs = (A * u) * sf
print('Number of timesteps: %s' % timesteps)
for t in range(timesteps):
zbn = copy.deepcopy(zb)
for stage in range(2):
''' ----------------------------- '''
''' Calculate the derivative '''
''' ----------------------------- '''
for i in range(1,number_nodes):
grdqb[i,stage] = (1./(dx))*(qbs[i]-qbs[i-1])
''' Update the boundary and near-boundary conditions '''
''' i = 1'''
grdqb[1,stage] = (1./(dx))*(qbs[0]-qbs[number_nodes-1])
for i in range(number_nodes):
if stage == 1:
zb[i] = zbn[i] - dt*grdqb[i,0]
else:
zb[i] = 0.5*(zb[i] + zbn[i]) - 0.5*dt*(grdqb[i,0] + grdqb[i,1])
''' Update the velocity and bedload '''
h = z - zb
u = qf/h
qbs = (A * u)
if use_slope_correction == True:
sf = calculate_slope_factor_dey(x,zb)
qbs = qbs * sf
return x,zb,u,qbs
def calculate_bed_slope(x,zb):
slope = np.zeros(len(zb))
for i in range(len(zb)-1):
slope[i] =(zb[i+1] - zb[i-1])/(x[i+1] - x[i-1])
num_node = len(zb)
slope[num_node-1] =(zb[1] - zb[num_node-2])/(x[1] - x[num_node-2])
return slope
def calculate_bed_slope_angle(x,zb):
slope = np.zeros(len(zb))
for i in range(len(zb)-1):
slope[i] =max( (math.atan(zb[i] - zb[i-1])/(x[i-1] - x[i])) ,
math.atan(zb[i+1] - zb[i])/(x[i] - x[i+1]) )
num_node = len(zb)-1
slope[num_node] =max( math.atan(zb[num_node] - zb[num_node-1])/(x[num_node-1] - x[num_node]) ,
math.atan(zb[0] - zb[num_node])/(x[num_node] - x[0]))
return slope
def calculate_slope_factor_paarl(x,zb,ang_repose=33.0):
ang_repose_rads = ang_repose * math.pi / 180.
ang_repose_slope = math.atan(ang_repose_rads)
''' This is eq 10 '''
slope_param = 1./math.tan(ang_repose_rads)
sf = np.zeros(len(x))
slope = calculate_bed_slope(x,zb)
for i in range(len(x)):
theta = math.atan(slope[i])
dzdx = slope[i]
if abs(theta) >= ang_repose_rads:
dzdx = ang_repose_slope*cmp(theta,0)
sf[i] = (1. + slope_param*dzdx)**-1.
return sf
def calculate_slope_factor_dey(x,zb,ang_repose=33.0):
''' =COS(C3)*(1-(TAN(C3)/TAN($D$1)))'''
ang_repose_rads = ang_repose * math.pi / 180.
slope = calculate_bed_slope(x,zb)
sf = np.zeros(len(x))
for i in range(len(x)):
theta = math.atan(slope[i])
if abs(theta) >= ang_repose_rads:
theta = ang_repose_rads*cmp(theta,0)
sf[i] = math.cos(theta) * (1. - (math.tan(theta)/math.tan(ang_repose_rads) ) )
return sf
def calculate_slope_factor(x,zb,ang_repose=33.0):
sf = np.zeros(len(x))
slope = calculate_bed_slope_angle(x,zb)
tan_ang_repose = math.tan(ang_repose * math.pi / 180.)
for i in range(len(x)):
tan_slope = math.tan(slope[i])
cos_slope = math.cos(slope[i])
if tan_slope >= tan_ang_repose:
sf[i] = 31.3
else:
sf[i] = tan_ang_repose / ((tan_ang_repose-tan_slope) * cos_slope)
return sf
def cmp(a, b):
return (a > b) - (a < b)
if __name__ == "__main__":
import morph_geom_lib
import matplotlib.pyplot as plt
x,zb = morph_geom_lib.single_hump( 20., 101 )
zb0 = copy.deepcopy(zb)
''' These are the settings from Cowles 2013 '''
depth = 3.0
qf = 1.0
tf = 3.01
dt = 0.0001
timesteps = int(tf/dt)
x,zb,u,qbs = morphmodel_upwind(x,zb,depth,qf,timesteps,dt,True)
fig = plt.figure(figsize=(16, 4))
ax1 = fig.add_subplot(211)
ax1.plot(x,zb, label='bed')
ax1.plot(x,zb0, label='initial bed')
slope = calculate_bed_slope(x,zb)
sf = calculate_slope_factor(x,zb)
ax2 = fig.add_subplot(212)
ax2.plot(x,slope,'r')
ax2.plot(x,sf,'b')
plt.show()