/
FSC_1.py
263 lines (223 loc) · 7.21 KB
/
FSC_1.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
256
257
258
259
260
261
262
263
import numpy as np, matplotlib.pyplot as plt, seaborn as sns
sns.set(style='darkgrid')
sns.set_context("paper")
plt.close()
def boundary_conditions(BC):
if BC == 0:
def make_nbor(xx):
# main points
if xx == 0:
def nbor_list(ij):
return [ij-nx-1,ij-nx,ij-nx+1,ij-1,ij+1,ij+nx-1,ij+nx,ij+nx+1]
# left side
if xx == 1:
def nbor_list(ij):
return [ij,ij-nx,ij-nx+1,ij,ij+1,ij,ij+nx,ij+nx+1]
# right side
if xx == 2:
def nbor_list(ij):
return [ij-nx-1,ij-nx,ij,ij-1,ij,ij+nx-1,ij+nx,ij]
# bottom side
if xx == 3:
def nbor_list(ij):
return [ij,ij,ij,ij-1,ij+1,ij+nx-1,ij+nx,ij+nx+1]
# top side
if xx == 4:
def nbor_list(ij):
return [ij-nx-1,ij-nx,ij-nx+1,ij-1,ij+1,ij,ij,ij]
# bottom left corner
if xx == 5:
def nbor_list(ij):
return [ij,ij,ij,ij,ij+1,ij,ij+nx,ij+nx+1]
# bottom right corner
if xx == 6:
def nbor_list(ij):
return [ij,ij,ij,ij-1,ij,ij+nx-1,ij+nx,ij]
# top left corner
if xx == 7:
def nbor_list(ij):
return [ij,ij-nx,ij-nx+1,ij,ij+1,ij,ij,ij]
# top right corner
if xx == 8:
def nbor_list(ij):
return [ij-nx-1,ij-nx,ij,ij-1,ij,ij,ij,ij]
return nbor_list
return make_nbor
def receiver():
rec = np.arange(nn) # receiver array
vector = np.zeros(nn) # vector array
direction = 4*np.ones(nn) # direction array
len_nbor = [dd, dy, dd, dx, dx, dd, dy, dd] # distance to each neighbour
loc_nbor = [0, 1, 2, 3, 5, 6, 7, 8]
ii_ranges = [range(1,nx-1), [0], [nx-1], range(1,nx-1), range(1,nx-1), [0], [nx-1], [0], [nx-1]]
jj_ranges = [range(1,ny-1), range(1,ny-1), range(1,ny-1), [0], [ny-1], [0], [0], [ny-1], [ny-1]]
for xx in range(9):
nbor_list = make_nbor(xx)
for jj in jj_ranges[xx]:
for ii in ii_ranges[xx]:
ij = ii + jj*nx # linear index
nbor = nbor_list(ij) # neighbours
slopes = (h[ij] - h[nbor]) / len_nbor # slope between neighbours and node ij
smax = 0.
for ind, slope in enumerate(slopes): # iterate over neighbours
if slope > smax: # find max slope
smax = slope # save new max slope
vector[ij] = slope
rec[ij] = nbor[ind]
direction[ij] = loc_nbor[ind]
return rec, vector, direction
def donor_list():
ndon = np.zeros(nn, dtype=int)
donor = -1*np.ones((nn,8), dtype=int)
for ij in range(nn):
if rec[ij] != ij:
ijk = rec[ij]
donor[ijk, ndon[ijk]] = ij
ndon[ijk] += 1
return donor, ndon
def make_stack():
# make nstack global so it works in recursive algy
global nstack
# find all baselevel points
base_levels = rec[rec == range(nn)]
# initialise stack
stack = np.zeros(nn, dtype=int)
colour = np.zeros(nn, dtype=int)
# initialise stack counter
nstack = 0
# define recursive stack function
def add_to_stack(ijk, c_flag):
# make nstack global so it works in recursive algy
global nstack
# but node on the stack
stack[nstack] = ijk
colour[ijk] = c_flag
# increase counter by one
nstack += 1
# iterate over nodes donors
for donor in donors[ijk,:ndon[ijk]]:
# start process over by calling stack algy on donor
add_to_stack(donor, c_flag)
# call recursive algy on all base level points
c_count = 0
for base in base_levels:
add_to_stack(base, c_count)
c_count += 1
# return the stack
return stack, colour
def calculate_area():
# reverse the stack
rstack = np.flipud(stack)
# iterate over the stack, adding the area
A = np.ones(nn) * dx * dy
for ij in rstack:
if rec[ij] != ij:
A[rec[ij]] += A[ij]
return A
def pland(h_in):
cmap = sns.cubehelix_palette(8, as_cmap=True)
plt.pcolormesh(h_in.reshape(ny, nx, order='C'), cmap=cmap)
# plt.contourf(h_in.reshape(ny, nx, order='C'), cmap=cmap)
plt.axis('equal')
plt.colorbar()
plt.show()
def v_plot(h_in, d_in, s_in):
fig = plt.figure(1)
h_in = h_in.reshape(ny, nx, order='C')
cmap = sns.cubehelix_palette(8, as_cmap=True, dark=0.3)
plt.pcolormesh(h_in.reshape(ny, nx, order='C'), cmap=cmap)
plt.colorbar()
# plt.contourf(h_in.reshape(ny, nx, order='C'), cmap=cmap)
s_in = s_in/s_in.max()
U = np.zeros(nn)
V = np.zeros(nn)
for ij in range(nn):
if d_in[ij] == 0:
U[ij] = -1
V[ij] = -1
if d_in[ij] == 1:
U[ij] = 0
V[ij] = -1
if d_in[ij] == 2:
U[ij] = 1
V[ij] = -1
if d_in[ij] == 3:
U[ij] = -1
V[ij] = 0
if d_in[ij] == 4:
U[ij] = 0
V[ij] = 0
if d_in[ij] == 5:
U[ij] = 1
V[ij] = 0
if d_in[ij] == 6:
U[ij] = -1
V[ij] = 1
if d_in[ij] == 7:
U[ij] = 0
V[ij] = 1
if d_in[ij] == 8:
U[ij] = 1
V[ij] = 1
qx = np.arange(nx)*dx + dx/2.
qy = np.arange(ny)*dy + dy/2.
qU = (dx*U).reshape(ny,nx)
qV = (dy*V).reshape(ny,nx)
Q = plt.quiver(qx,qy,qU,qV, scale=max([xl,yl])*2.)
plt.axis('equal')
plt.xlim([0, nx])
plt.ylim([0, ny])
plt.show()
# set the scale of the grid
xl, yl = (100.e3, 100.e3) # meters
# set the resolution of the grid
nx, ny = (31, 51)
dx, dy = (xl/(nx-1), yl/(ny-1))
dd = np.sqrt(dx**2 + dy**2)
nn = nx*ny
# set the timestep vector
dt = 1000. # years
# number of timesteps
nstep = 1000
# set the parameters of the stream power law
n = 1.
m = n*0.4
# initial conditions
h = np.random.rand(nn)
# test grid
h = np.array([9,0,0,0,6,6,6,5,4,3,
2,2,2,2,5,5,5,4,4,2,
3,3,3,3,5,4,3,2,1,0,
2,2,2,2,5,5,5,4,4,2,
0,0,0,0,6,6,6,5,4,3])
nx, ny = (10, 5)
xl, yl = (10, 5) # meters
dx, dy = (xl/(nx-1), yl/(ny-1))
dd = np.sqrt(dx**2 + dy**2)
nn = nx*ny
# pland(h)
# boundary conditions
# all boundaries are base level
make_nbor = boundary_conditions(0)
# h = 0 at y = 0 and yl, cyclic at x = 0 and xl
# make_nbor = boundary_conditions(1)
# h = 0 at y = 0 and yl, reflective at x = 0 and xl
# make_nbor = boundary_conditions(2)
# calculate the receiver array
rec, vector, direction = receiver()
# print 'receiver:\n', np.reshape(rec, (ny,nx))
# pland(rec)
# calculate the donor array
donors, ndon = donor_list()
# print 'ndon:\n', np.reshape(ndon, (ny,nx))
# pland(ndon)
# calculate the stack
stack, colour = make_stack()
# print 'stack:\n', np.reshape(stack, (ny,nx))
# pland(stack)
# pland(colour)
# calculate the catchment area for each node
A = calculate_area()
v_plot(h, direction, vector)
v_plot(colour, direction, vector)
v_plot(A, direction, vector)