File size: 5,605 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 | """
Geo-Lab AI Engine: ๊ธฐ๋ณธ ์งํ ํด๋์ค
"""
import numpy as np
from dataclasses import dataclass, field
from typing import Optional, Tuple
@dataclass
class Terrain:
"""2D ๋์ด๋งต ๊ธฐ๋ฐ ์งํ ๋ฐ์ดํฐ ๊ตฌ์กฐ"""
width: int = 100 # X ๋ฐฉํฅ ์
์
height: int = 100 # Y ๋ฐฉํฅ ์
์
cell_size: float = 10.0 # ์
๋น ์ค์ ๊ฑฐ๋ฆฌ (m)
# ๋์ด๋งต (m)
elevation: np.ndarray = field(default=None)
# ์์ ์์ฑ
rock_hardness: np.ndarray = field(default=None) # 0-1, 1์ด ๊ฐ์ฅ ๋จ๋จํจ
def __post_init__(self):
if self.elevation is None:
self.elevation = np.zeros((self.height, self.width))
if self.rock_hardness is None:
self.rock_hardness = np.ones((self.height, self.width)) * 0.5
@classmethod
def create_slope(cls, width: int, height: int,
max_elevation: float = 1000.0,
slope_direction: str = 'south') -> 'Terrain':
"""๊ฒฝ์ฌ๋ฉด ์งํ ์์ฑ"""
terrain = cls(width=width, height=height)
if slope_direction == 'south':
# ๋ถ์ชฝ์ด ๋๊ณ ๋จ์ชฝ์ด ๋ฎ์
for y in range(height):
terrain.elevation[y, :] = max_elevation * (1 - y / height)
elif slope_direction == 'east':
for x in range(width):
terrain.elevation[:, x] = max_elevation * (1 - x / width)
return terrain
@classmethod
def create_v_valley_initial(cls, width: int = 100, height: int = 100,
valley_depth: float = 50.0) -> 'Terrain':
"""V์๊ณก ์๋ฎฌ๋ ์ด์
์ด๊ธฐ ์งํ"""
terrain = cls(width=width, height=height)
# ๊ธฐ๋ณธ ๊ฒฝ์ฌ (๋ถโ๋จ)
for y in range(height):
terrain.elevation[y, :] = 500 * (1 - y / height)
# ์ค์์ ์ด๊ธฐ ํ์ฒ ์ฑ๋ (์ฝ๊ฐ์ ํจ์)
center = width // 2
for x in range(width):
dist = abs(x - center)
if dist < 5:
terrain.elevation[:, x] -= valley_depth * (1 - dist / 5)
return terrain
def get_slope(self) -> np.ndarray:
"""๊ฐ ์
์ ๊ฒฝ์ฌ๋ ๊ณ์ฐ (๋ผ๋์)"""
dy, dx = np.gradient(self.elevation, self.cell_size)
return np.arctan(np.sqrt(dx**2 + dy**2))
def get_flow_direction(self) -> Tuple[np.ndarray, np.ndarray]:
"""๋ฌผ ํ๋ฆ ๋ฐฉํฅ ๋ฒกํฐ (๊ฐ์ฅ ๊ฐํ๋ฅธ ํ๊ฐ ๋ฐฉํฅ)"""
dy, dx = np.gradient(self.elevation, self.cell_size)
magnitude = np.sqrt(dx**2 + dy**2) + 1e-10
return -dx / magnitude, -dy / magnitude
@dataclass
class Water:
"""ํ์ฒ ์๋ฌธ ๋ฐ์ดํฐ"""
terrain: Terrain
# ๊ฐ ์
์ ์๋ (mยณ/s)
discharge: np.ndarray = field(default=None)
# ์ ์ (m/s)
velocity: np.ndarray = field(default=None)
# ํ๋ฆ ๋ฐฉํฅ (๋จ์ ๋ฒกํฐ)
flow_x: np.ndarray = field(default=None)
flow_y: np.ndarray = field(default=None)
def __post_init__(self):
shape = (self.terrain.height, self.terrain.width)
if self.discharge is None:
self.discharge = np.zeros(shape)
if self.velocity is None:
self.velocity = np.zeros(shape)
if self.flow_x is None:
self.flow_x, self.flow_y = self.terrain.get_flow_direction()
def add_precipitation(self, rate: float = 0.001):
"""๊ฐ์ ์ถ๊ฐ (m/s per cell)"""
self.discharge += rate
def accumulate_flow(self):
"""ํ๋ฆ ๋์ ๊ณ์ฐ (๊ฐ๋จํ D8 ์๊ณ ๋ฆฌ์ฆ)"""
h, w = self.terrain.height, self.terrain.width
accumulated = self.discharge.copy()
# ๋์ ๊ณณ์์ ๋ฎ์ ๊ณณ์ผ๋ก ์ ๋ ฌ
indices = np.argsort(self.terrain.elevation.ravel())[::-1]
for idx in indices:
y, x = idx // w, idx % w
if accumulated[y, x] <= 0:
continue
# ๊ฐ์ฅ ๋ฎ์ ์ด์ ์ฐพ๊ธฐ
min_elev = self.terrain.elevation[y, x]
min_neighbor = None
for dy, dx in [(-1,0), (1,0), (0,-1), (0,1)]:
ny, nx = y + dy, x + dx
if 0 <= ny < h and 0 <= nx < w:
if self.terrain.elevation[ny, nx] < min_elev:
min_elev = self.terrain.elevation[ny, nx]
min_neighbor = (ny, nx)
if min_neighbor:
accumulated[min_neighbor] += accumulated[y, x]
self.discharge = accumulated
# ์ ์ ๊ณ์ฐ (Manning ๋ฐฉ์ ์ ๋จ์ํ)
slope = self.terrain.get_slope() + 0.001 # 0 ๋ฐฉ์ง
self.velocity = 2.0 * np.sqrt(slope) * np.power(self.discharge + 0.1, 0.4)
@dataclass
class SimulationState:
"""์๋ฎฌ๋ ์ด์
์ํ ๊ด๋ฆฌ"""
terrain: Terrain
water: Water
time_step: float = 1.0 # ์๋ฎฌ๋ ์ด์
1์คํ
= 1๋
current_time: float = 0.0
# ์ ์ญ ๋ณ์ (Master Plan์ Global Controllers)
climate_level: float = 1.0 # ๊ธฐํ (๊ฐ์๋ ๊ณ์)
sea_level: float = 0.0 # ํด์๋ฉด (m)
tectonic_energy: float = 0.0 # ์ง๊ฐ ์๋์ง (์ต๊ธฐ์จ m/year)
def step(self):
"""1 ํ์์คํ
์งํ"""
self.current_time += self.time_step
# ๊ฐ์ ์ถ๊ฐ
self.water.add_precipitation(rate=0.001 * self.climate_level)
# ํ๋ฆ ๋์
self.water.accumulate_flow()
|