File size: 13,777 Bytes
2afa69c | 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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | import numpy as np
from .grid import WorldGrid
class ErosionProcess:
"""
์งํ ๋ณ๊ฒฝ ์ปค๋ (Erosion/Deposition Kernel)
๋ฌผ๋ฆฌ ๊ณต์์ ์ ์ฉํ์ฌ ์งํ(๊ณ ๋)์ ๋ณ๊ฒฝํฉ๋๋ค.
1. Stream Power Law: ํ์ฒ ์นจ์ (E = K * A^m * S^n)
2. Hillslope Diffusion: ์ฌ๋ฉด ๋ถ๊ดด/ํ์ฐ (dz/dt = D * del^2 z)
"""
def __init__(self, grid: WorldGrid, K: float = 1e-4, m: float = 0.5, n: float = 1.0, D: float = 0.01):
self.grid = grid
self.K = K # ์นจ์ ๊ณ์
self.m = m # ์ ๋ ์ง์
self.n = n # ๊ฒฝ์ฌ ์ง์
self.D = D # ํ์ฐ ๊ณ์ (์ฌ๋ฉด)
def stream_power_erosion(self, discharge: np.ndarray, dt: float = 1.0) -> np.ndarray:
"""Stream Power Law ๊ธฐ๋ฐ ํ์ฒ ์นจ์"""
slope, _ = self.grid.get_gradient()
# E = K * Q^m * S^n
# (์ ๋ Q๋ฅผ ์ ์ญ๋ฉด์ A ๋์ ์ฌ์ฉ)
erosion_rate = self.K * np.power(discharge, self.m) * np.power(slope, self.n)
# ์ค์ ์นจ์๋ = rate * time
erosion_amount = erosion_rate * dt
# ๊ธฐ๋ฐ์ ์ดํ๋ก๋ ์นจ์ ๋ถ๊ฐ (available sediment first, then bedrock)
# ์ฌ๊ธฐ์๋ ๋จ์ํ๋ฅผ ์ํด Topography(elevation)์ ๋ฐ๋ก ๊น์.
# ๋จ, ํด์๋ฉด ์๋๋ ์นจ์ ์์ฉ ๊ฐ์ (๋ฌผ ์์์๋ Stream Power๊ฐ ์๋)
underwater = self.grid.is_underwater()
erosion_amount[underwater] *= 0.1
# ์งํ ์
๋ฐ์ดํธ
self.grid.elevation -= erosion_amount
# ๋จ์ํ: ๊น์ธ ๋งํผ ํด์ ๋ฌผ๋ก ๋ณํ๋์ด ์ด๋๊ฐ๋ก ๊ฐ์ผ ํ์ง๋ง,
# Stream Power Model(SPL)์ ๋ณดํต Detachment-limited ๋ชจ๋ธ์ด๋ผ ํด์ ์ ๋ช
์์ ์ผ๋ก ๋ค๋ฃจ์ง ์์.
# ํตํฉ ๋ชจ๋ธ์ ์ํด, ์นจ์๋ ์์ ํด์ ๋ฌผ ํ๋ญ์ค์ ๋ํด์ค ์ ์์ (๊ตฌํ ์์ )
return erosion_amount
def hillslope_diffusion(self, dt: float = 1.0) -> np.ndarray:
"""์ฌ๋ฉด ํ์ฐ ํ๋ก์ธ์ค (Linear Diffusion)"""
elev = self.grid.elevation
# Laplacian calculation (์ด์ฐํ)
# del^2 z = (z_up + z_down + z_left + z_right - 4*z) / dx^2
# Numpy roll์ ์ด์ฉํ ๋น ๋ฅธ ๊ณ์ฐ
up = np.roll(elev, -1, axis=0)
down = np.roll(elev, 1, axis=0)
left = np.roll(elev, -1, axis=1)
right = np.roll(elev, 1, axis=1)
dx2 = self.grid.cell_size ** 2
laplacian = (up + down + left + right - 4 * elev) / dx2
# ๊ฒฝ๊ณ ์กฐ๊ฑด ์ฒ๋ฆฌ (๊ฐ์ฅ์๋ฆฌ๋ ๊ณ์ฐ ์ ์ธ or 0)
laplacian[0, :] = 0
laplacian[-1, :] = 0
laplacian[:, 0] = 0
laplacian[:, -1] = 0
# dz/dt = D * del^2 z
change = self.D * laplacian * dt
self.grid.elevation += change
return change
def overbank_deposition(self, discharge: np.ndarray,
bankfull_capacity: float = 100.0,
decay_rate: float = 0.1,
dt: float = 1.0) -> np.ndarray:
"""
๋ฒ๋์ ํด์ (Overbank Deposition)
ํ์ฒ ์ฉ๋ ์ด๊ณผ ์ ๋ฒ๋ํ์ฌ ์ฃผ๋ณ์ ์ธ๋ฆฝ์ง ํด์ .
ํ๋์์ ๋ฉ์ด์ง์๋ก ํด์ ๋ ๊ฐ์ (์์ฐ์ ๋ฐฉ ํ์ฑ).
Args:
discharge: ์ ๋ ๋ฐฐ์ด
bankfull_capacity: ํ๋ ์ฉ๋ (์ด๊ณผ ์ ๋ฒ๋)
decay_rate: ๊ฑฐ๋ฆฌ์ ๋ฐ๋ฅธ ํด์ ๊ฐ์ ์จ
dt: ์๊ฐ ๊ฐ๊ฒฉ
Returns:
deposition: ํด์ ๋ ๋ฐฐ์ด
"""
from scipy.ndimage import distance_transform_edt
h, w = self.grid.height, self.grid.width
# 1. ๋ฒ๋ ์ง์ ์๋ณ (์ฉ๋ ์ด๊ณผ)
overflow = np.maximum(0, discharge - bankfull_capacity)
flood_mask = overflow > 0
if not np.any(flood_mask):
return np.zeros((h, w))
# 2. ํ๋๋ก๋ถํฐ์ ๊ฑฐ๋ฆฌ ๊ณ์ฐ
# flood_mask๊ฐ ์๋ ๊ณณ์ด ํ๋
channel_mask = discharge > bankfull_capacity * 0.5
if not np.any(channel_mask):
return np.zeros((h, w))
# Distance Transform (ํ๋๋ก๋ถํฐ์ ๊ฑฐ๋ฆฌ)
distance = distance_transform_edt(~channel_mask) * self.grid.cell_size
# 3. ํด์ ๋ ๊ณ์ฐ (์ง์ ๊ฐ์ )
# Deposition = overflow * exp(-k * distance)
# ํ๋ ๊ทผ์ฒ(์์ฐ์ ๋ฐฉ)์ ๋ง์ด, ๋ฉ์๋ก(๋ฐฐํ์ต์ง) ์ ๊ฒ
max_overflow = overflow.max()
if max_overflow <= 0:
return np.zeros((h, w))
normalized_overflow = overflow / max_overflow
# ๋ฒ๋ ์ํฅ ๋ฒ์ (์ต๋ 50 ์
)
max_distance = 50 * self.grid.cell_size
influence = np.exp(-decay_rate * distance / self.grid.cell_size)
influence[distance > max_distance] = 0
# ํด์ ๋
deposition = normalized_overflow.max() * influence * 0.1 * dt
# ํด์๋ฉด ์๋๋ ์ ์ธ
underwater = self.grid.is_underwater()
deposition[underwater] = 0
# ํ๋ ์์ฒด๋ ์ ์ธ (ํ๋๋ ์นจ์์ด ์ฐ์ธ)
deposition[channel_mask] = 0
# 4. ํด์ ์ธต์ ์ถ๊ฐ
self.grid.add_sediment(deposition)
return deposition
def transport_and_deposit(self, discharge: np.ndarray, dt: float = 1.0, Kf: float = 0.01) -> np.ndarray:
"""
ํด์ ๋ฌผ ์ด๋ฐ ๋ฐ ํด์ (Sediment Transport & Deposition)
Transport Capacity Law:
Q_cap = Kf * Q^m * S^n
- Q_cap > Q_sed: ์นจ์ (Erosion) -> ํด์ ๋ฌผ ์ฆ๊ฐ
- Q_cap < Q_sed: ํด์ (Deposition) -> ํด์ ๋ฌผ ๊ฐ์, ์งํ ์์น
Args:
discharge: ์ ๋
dt: ์๊ฐ ๊ฐ๊ฒฉ
Kf: ์ด๋ฐ ํจ์จ ๊ณ์ (Transport Efficiency)
"""
slope, _ = self.grid.get_gradient()
# ๊ฒฝ์ฌ๊ฐ 0์ด๋ฉด ๋ฌดํ ํด์ ๋ฐฉ์ง๋ฅผ ์ํด ์ต์๊ฐ ์ค์
slope = np.maximum(slope, 0.001)
# 1. ์ด๋ฐ ๋ฅ๋ ฅ (Transport Capacity) ๊ณ์ฐ
# ์นจ์ ๊ณ์ K ๋์ ์ด๋ฐ ๊ณ์ Kf ์ฌ์ฉ (์ผ๋ฐ์ ์ผ๋ก K๋ณด๋ค ํผ)
capacity = Kf * np.power(discharge, self.m) * np.power(slope, self.n)
# 2. ํ์ฌ ๋ถ์ ์ฌ(Suspended Sediment) ๊ฐ์
# ์๋ฅ์์ ๋ค์ด์ค๋ ์ ์ฌ๋์ ์ด์ ๋จ๊ณ์ ์นจ์๋์ด๋ ๊ธฐ์ ์
๋์ ์์กด
# ์ฌ๊ธฐ์๋ ๋จ์ํ๋ฅผ ์ํด 'Local Equilibrium'์ ๊ฐ์ ํ์ง ์๊ณ ,
# ์ ๋์ ๋น๋กํ๋ ์ด๊ธฐ ์ ์ฌ๋์ ๊ฐ์ ํ๊ฑฐ๋,
# ์ด์ ์คํ
์ ์นจ์ ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉํด์ผ ํจ.
# ํตํฉ ๋ชจ๋ธ์ ์ํด: "Erosion" ํจ์๊ฐ ๊น์๋ธ ํ์ ๋ฐํํ๋๋ก ํ๊ณ ,
# ์ด๋ฅผ capacity์ ๋น๊ตํ์ฌ ์ฌํด์ ์ํค๊ฑฐ๋ ํ๋ฅ๋ก ๋ณด๋.
# ํ์ง๋ง D8 ์๊ณ ๋ฆฌ์ฆ ์ ํ๋ฅ๋ก์ '์ ๋ฌ(Routing)'์ด ํ์ํจ.
# ์ฌ๊ธฐ์๋ Simplified Landform Evolution Model (SLEM) ๋ฐฉ์ ์ ์ฉ:
# dZs/dt = U - E + D
# D = (Q_cap - Q_sed) / Length_scale (if Q_sed > Q_cap)
# E = Stream Power (detachment limited)
# Delta ์๋ฎฌ๋ ์ด์
์ ์ํ ์ ๊ทผ:
# ํด์ ๋ฌผ ํ๋ญ์ค(Flux)๋ฅผ ํ๋ฅ๋ก ๋ฐ์ด๋ด๋ ๋ก์ง ์ถ๊ฐ.
h, w = self.grid.height, self.grid.width
sediment_flux = np.zeros((h, w))
# ์ ๋ ์์๋๋ก ์ฒ๋ฆฌ (Upstream -> Downstream)
# discharge๊ฐ ๋ฎ์ ๊ณณ(์๋ฅ)์์ ๋์ ๊ณณ(ํ๋ฅ)์ผ๋ก?
# D8 ํ๋ฆ ๋ฐฉํฅ์ ๋ค์ ์ถ์ ํด์ผ ์ ํํจ.
# ์ฌ๊ธฐ์๋ ๊ฐ๋จํ 'Capacity ์ด๊ณผ๋ถ ํด์ '๋ง ๊ตฌํํ๊ณ ,
# Flux Routing์ HydroKernel๊ณผ ์ฐ๋๋์ด์ผ ํจ.
# ์์: Capacity based deposition only (Local)
# ํด์ ๋ = (ํ์ฌ ์ ์ฌ๋ - ์ฉ๋) * ๋น์จ
# ํ์ฌ ์ ์ฌ๋์ด ์์ผ๋ฏ๋ก, ์นจ์๋ ํ(Stream Power ๊ฒฐ๊ณผ)์ด
# ํด๋น ์
์ Capacity๋ฅผ ๋์ผ๋ฉด ์ฆ์ ํด์ ๋๋ค๊ณ ๊ฐ์ .
pass
# TODO: Flux Routing ๊ตฌํ ํ์. ํ์ฌ ๊ตฌ์กฐ์์๋ ์ด๋ ค์.
# ErosionProcess๋ฅผ ์์ ํ์ฌ 'simulate_transport' ๋ฉ์๋๋ก ํตํฉ.
return capacity
def simulate_transport(self, discharge: np.ndarray, dt: float = 1.0,
sediment_influx_map: np.ndarray = None) -> np.ndarray:
"""
ํตํฉ ํด์ ๋ฌผ ์ด์ก ์๋ฎฌ๋ ์ด์
(Flux-based)
1. ์๋ฅ์์ ํด์ ๋ฌผ ์ ์
(Flux In)
2. ๋ก์ปฌ ์นจ์/ํด์ (Erosion/Deposition)
3. ํ๋ฅ๋ก ๋ฐฐ์ถ (Flux Out)
"""
h, w = self.grid.height, self.grid.width
elev = self.grid.elevation
# 1. ์ ๋ ฌ (๋์ ๊ณณ -> ๋ฎ์ ๊ณณ)
indices = np.argsort(elev.ravel())[::-1]
# ํด์ ๋ฌผ ํ๋ญ์ค ์ด๊ธฐํ (์ ์
์ ๋ฐ์)
flux = np.zeros((h, w))
if sediment_influx_map is not None:
flux += sediment_influx_map
slope, _ = self.grid.get_gradient()
slope = np.maximum(slope, 0.001)
change = np.zeros((h, w))
# D8 Neighbors (Lookup for flow_dir)
d8_dr = [-1, -1, -1, 0, 0, 1, 1, 1]
d8_dc = [-1, 0, 1, -1, 1, -1, 0, 1]
# Check if flow_dir is available
use_flow_dir = (self.grid.flow_dir is not None)
for idx in indices:
r, c = idx // w, idx % w
# ํด์๋ฉด ์๋ ๊น์ ๊ณณ์ ํด์ ์์ฃผ
underwater = self.grid.is_underwater()[r, c]
# A. ์ด๋ฐ ๋ฅ๋ ฅ (Capacity)
# ๋ฌผ ์์์๋ ์ ์์ด ๊ธ๊ฐํ๋ค๊ณ ๊ฐ์ -> Capacity ๊ฐ์
# eff_slope calculation fix: slope can be very small on flat land
eff_slope = slope[r, c] if not underwater else slope[r, c] * 0.01
# Kf (Transportation efficiency) should be high enough
# Use 'self.K * 100' or similar
qs_cap = self.K * 500 * np.power(discharge[r, c], self.m) * np.power(eff_slope, self.n)
# B. ํ์ฌ ํ๋ญ์ค (์๋ฅ์์ ๋ค์ด์จ ๊ฒ + ๋ก์ปฌ ์นจ์ ์ ์ฌ๋)
qs_in = flux[r, c]
# C. ์นจ์ vs ํด์ ๊ฒฐ์
# ๊ธฐ๊ณ์ ์นจ์ (Stream Power)
potential_erosion = self.K * np.power(discharge[r, c], self.m) * np.power(slope[r, c], self.n) * dt
# ๋ง์ฝ ๋ค์ด์จ ํ(qs_in)์ด ์ฉ๋(qs_cap)๋ณด๋ค ๋ง์ผ๋ฉด -> ํด์
if qs_in > qs_cap:
# ํด์ ๋ = ์ด๊ณผ๋ถ * 1.0 (์ผ๋จ 100% ํด์ ๊ฐ์ ํ์ฌ ํจ๊ณผ ํ์ธ)
deposition_amount = (qs_in - qs_cap) * 1.0
change[r, c] += deposition_amount
qs_out = qs_cap # ๋๋จธ์ง๋ ํ๋ฅ๋ก? ์๋, ํด์ ํ ๋จ์๊ฑด qs_cap์ (Transport-limited)
else:
# ์ฉ๋์ด ๋จ์ผ๋ฉด -> ์นจ์ํ์ฌ ํ์ ๋ ์ฃ๊ณ ๊ฐ
# ์ค์ ์นจ์ = ์ ์ฌ ์นจ์ (๊ธฐ๋ฐ์๋ ์นจ์ ๊ฐ๋ฅ)
erosion_amount = potential_erosion
change[r, c] -= erosion_amount
qs_out = qs_in + erosion_amount
# D. ํ๋ฅ๋ก ์ ๋ฌ (Qs Out Routing)
# Use pre-calculated flow direction if available
target_r, target_c = -1, -1
if use_flow_dir:
k = self.grid.flow_dir[r, c]
# k could be default 0 even if no flow?
# Usually sink nodes have special value (e.g. -1 or point to self)
# But here we initialized to 0.
# Need to check constraints.
# If discharge[r,c] > 0, flow_dir should be valid.
if discharge[r, c] > 0:
nr = r + d8_dr[k]
nc = c + d8_dc[k]
if 0 <= nr < h and 0 <= nc < w:
target_r, target_c = nr, nc
else:
# Fallback: Local Seek (Slow)
min_z = elev[r, c]
for k in range(8):
nr = r + d8_dr[k]
nc = c + d8_dc[k]
if 0 <= nr < h and 0 <= nc < w:
if elev[nr, nc] < min_z:
min_z = elev[nr, nc]
target_r, target_c = nr, nc
if target_r != -1:
flux[target_r, target_c] += qs_out
else:
# ๊ฐํ ๊ณณ(Sink) -> ๊ทธ ์๋ฆฌ์ ํด์
# ์นจ์์ด ๋ฐ์ํ๋ค๋ฉด ๋๋๋ ค๋๊ณ ํด์
change[r, c] += qs_out
# ์งํ ์
๋ฐ์ดํธ
# ์นจ์์ elevation ๊ฐ์, ํด์ ์ sediment ์ฆ๊ฐ์ด์ง๋ง
# ์ฌ๊ธฐ์๋ ํตํฉํ์ฌ elevation/sediment ์กฐ์
# ํด์ ๋ถ: sediment ์ธต์ ์ถ๊ฐ
self.grid.add_sediment(np.maximum(change, 0))
# ์นจ์๋ถ: elevation ๊ฐ์ (grid.add_sediment๊ฐ ์์๋ ์ฒ๋ฆฌํ๋? ์๋)
# ์นจ์์ bedrock์ด๋ sediment๋ฅผ ๊น์์ผ ํจ.
# erosion_process.py์ ์ญํ ์ ์ง์ grid ์์ ์ ํด๋ ๋จ.
erosion_mask = change < 0
loss = -change[erosion_mask]
# ํด์ ์ธต ๋จผ์ ๊น๊ณ ๊ธฐ๋ฐ์ ๊น๊ธฐ
sed_thickness = self.grid.sediment[erosion_mask]
sed_loss = np.minimum(loss, sed_thickness)
rock_loss = loss - sed_loss
self.grid.sediment[erosion_mask] -= sed_loss
self.grid.bedrock[erosion_mask] -= rock_loss
self.grid.update_elevation()
return change
|