This repository was archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflow_class.py
199 lines (167 loc) · 8.29 KB
/
flow_class.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 15:14:39 2019
This is the flow class
Copyright (C) <2020> <Michael Neuhauser>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import numpy as np
import math
class Cell:
def __init__(self, rowindex, colindex, dem_ng, cellsize, flux, z_delta, parent, alpha, exp, flux_threshold, max_z_delta, startcell):
'''This class handles the spreading over the DEM!
Depending on the process different alpha angles are used for energy dissipation.'''
self.rowindex = rowindex
self.colindex = colindex
self.altitude = dem_ng[1, 1]
self.dem_ng = dem_ng
self.cellsize = cellsize
self.tan_beta = np.zeros_like(self.dem_ng)
self.dist = np.zeros_like(self.dem_ng)
self.persistence = np.zeros_like(self.dem_ng)
self.r_t = np.zeros_like(self.dem_ng)
self.no_flow = np.ones_like(self.dem_ng)
self.flux = flux
self.z_delta = z_delta
self.alpha = float(alpha)
self.exp = int(exp)
self.max_z_delta = float(max_z_delta)
self.flux_threshold = float(flux_threshold)
self.min_distance = 0
self.max_distance = 0
self.min_gamma = 0
self.max_gamma = 0
self.sl_gamma = 0
if type(startcell) == bool: # check, if start cell exist (start cell is release point)
self.is_start = True # set is_start to True
else:
self.startcell = startcell # give startcell to cell
self.is_start = False # set is_start to False
self.parent = []
if type(parent) == Cell:
self.parent.append(parent)
def add_os(self, flux):
self.flux += flux
def add_parent(self, parent):
self.parent.append(parent)
def calc_fp_travelangle(self):
dist_min = []
dh = self.startcell.altitude - self.altitude
for parent in self.parent:
dx = abs(parent.colindex - self.colindex)
dy = abs(parent.rowindex - self.rowindex)
dist_min.append(math.sqrt(dx ** 2 + dy ** 2) * self.cellsize + parent.min_distance)
self.min_distance = np.amin(dist_min)
self.max_gamma = np.rad2deg(np.arctan(dh / self.min_distance))
def calc_sl_travelangle(self):
dx = abs(self.startcell.colindex - self.colindex)
dy = abs(self.startcell.rowindex - self.rowindex)
dh = self.startcell.altitude - self.altitude
ds = math.sqrt(dx ** 2 + dy ** 2) * self.cellsize
self.sl_gamma = np.rad2deg(np.arctan(dh / ds))
def calc_z_delta(self):
self.z_delta_neighbour = np.zeros((3, 3))
self.z_gamma = self.altitude - self.dem_ng
ds = np.array([[np.sqrt(2), 1, np.sqrt(2)], [1, 0, 1], [np.sqrt(2), 1, np.sqrt(2)]])
tan_alpha = np.tan(np.deg2rad(self.alpha))
self.z_alpha = ds * self.cellsize * tan_alpha
self.z_delta_neighbour = self.z_delta + self.z_gamma - self.z_alpha
self.z_delta_neighbour[self.z_delta_neighbour < 0] = 0
self.z_delta_neighbour[self.z_delta_neighbour > self.max_z_delta] = self.max_z_delta
def calc_tanbeta(self):
ds = np.array([[np.sqrt(2), 1, np.sqrt(2)], [1, 1, 1], [np.sqrt(2), 1, np.sqrt(2)]])
distance = ds * self.cellsize
beta = np.arctan((self.altitude - self.dem_ng) / distance) + np.deg2rad(90)
self.tan_beta = np.tan(beta/2)
self.tan_beta[self.z_delta_neighbour <= 0] = 0
self.tan_beta[self.persistence <= 0] = 0
self.tan_beta[1, 1] = 0
if abs(np.sum(self.tan_beta)) > 0:
self.r_t = self.tan_beta ** self.exp / np.sum(self.tan_beta ** self.exp)
def calc_persistence(self):
self.persistence = np.zeros_like(self.dem_ng)
if self.is_start:
self.persistence += 1
elif self.parent[0].is_start:
self.persistence += 1
else:
for parent in self.parent:
dx = (parent.colindex - self.colindex)
dy = (parent.rowindex - self.rowindex)
self.no_flow[dy + 1,dx + 1] = 0 # 3x3 Matrix of ones, every parent gets a 0, so no flow to a parent field.
maxweight = parent.z_delta
# Old Calculation
if dx == -1:
if dy == -1:
self.persistence[2, 2] += maxweight
self.persistence[2, 1] += 0.707 * maxweight
self.persistence[1, 2] += 0.707 * maxweight
if dy == 0:
self.persistence[1, 2] += maxweight
self.persistence[2, 2] += 0.707 * maxweight
self.persistence[0, 2] += 0.707 * maxweight
if dy == 1:
self.persistence[0, 2] += maxweight
self.persistence[0, 1] += 0.707 * maxweight
self.persistence[1, 2] += 0.707 * maxweight
if dx == 0:
if dy == -1:
self.persistence[2, 1] += maxweight
self.persistence[2, 0] += 0.707 * maxweight
self.persistence[2, 2] += 0.707 * maxweight
if dy == 1:
self.persistence[0, 1] += maxweight
self.persistence[0, 0] += 0.707 * maxweight
self.persistence[0, 2] += 0.707 * maxweight
if dx == 1:
if dy == -1:
self.persistence[2, 0] += maxweight
self.persistence[1, 0] += 0.707 * maxweight
self.persistence[2, 1] += 0.707 * maxweight
if dy == 0:
self.persistence[1, 0] += maxweight
self.persistence[0, 0] += 0.707 * maxweight
self.persistence[2, 0] += 0.707 * maxweight
if dy == 1:
self.persistence[0, 0] += maxweight
self.persistence[0, 1] += 0.707 * maxweight
self.persistence[1, 0] += 0.707 * maxweight
# =============================================================================
# # New Calculation:
# theta_child = np.array([[np.pi*5/4, np.pi*3/2 , np.pi*7/4], [np.pi, 0, 0], [np.pi*3/4, np.pi/2 , np.pi/4]])
# theta_parent = (np.arctan2(dy, dx))
#
# pers1 = theta_parent - theta_child - np.pi
# pers = np.zeros((3,3))
#
# for idx, element in np.ndenumerate(pers1):
# pers[idx] = max(0, np.cos(element))
# if pers[idx] < 2*np.finfo(np.float64).eps:
# pers[idx] = 0
# pers[1, 1] = 0
# self.persistence += pers * maxweight
# =============================================================================
def calc_distribution(self):
self.calc_z_delta()
self.calc_persistence()
self.persistence *= self.no_flow
self.calc_tanbeta()
if not self.is_start:
self.calc_fp_travelangle()
self.calc_sl_travelangle()
threshold = self.flux_threshold
if np.sum(self.r_t) > 0:
self.dist = (self.persistence * self.r_t) / np.sum(self.persistence * self.r_t) * self.flux
row_local, col_local = np.where(self.dist > threshold)
return list(self.rowindex - 1 + row_local), list(self.colindex - 1 + col_local), list(self.dist[row_local, col_local]), list(self.z_delta_neighbour[row_local, col_local])