在 98901 引擎中加入「任意不規則障礙物(迷宮滲流均衡)

megapx
megapx
import numpy as np # 9801 單指針垂直/水平剖面 (99 x 99) W, H = 99, 99 TOTAL_PTR = W * H S = 16 ONE = 1 << S p = np.zeros(TOTAL_PTR, dtype=np.int64) # 壓力/水位高度 u = np.zeros(TOTAL_PTR, dtype=np.int64) # X 速度 v = np.zeros(TOTAL_PTR, dtype=np.int64) # Y 速度 solid = np.zeros(TOTAL_PTR, dtype=np.uint8) # 1-bit 障礙物遮罩 (0=流體, 1=固體) # 1. 設定複雜不規則障礙物 (例如中間隔板留下上下兩個狹窄孔隙,模擬滲流均衡) solid_2d = solid.reshape((H, W)) solid_2d[0, :] = 1; solid_2d[-1, :] = 1 # 上下容器壁 solid_2d[:, 0] = 1; solid_2d[:, -1] = 1 # 左右容器壁 solid_2d[15:45, 48:52] = 1 # 中間阻擋牆 1 solid_2d[55:85, 48:52] = 1 # 中間阻擋牆 2 (僅留上、中、下三個窄縫) # 2. 初始失衡狀態:左側注滿高水位 (2.0),右側為低水位 (0.2) p_2d = p.reshape((H, W)) p_2d[1:-1, 1:48] = 2 * ONE p_2d[1:-1, 52:-1] = (2 * ONE) // 10 # 預先篩選出「非固體」的活躍流體指針 fluid_ptrs = np.where(solid == 0)[0] # 3. 演化 500 步:觀察水流穿過狹縫走向兩側均衡的過程 for step in range(500): # 若相鄰鄰居是固體,壓力梯度視為 0 (不穿透壁面反彈效應) p_xp = np.where(solid[fluid_ptrs + 1] == 1, p[fluid_ptrs], p[fluid_ptrs + 1]) p_xm = np.where(solid[fluid_ptrs - 1] == 1, p[fluid_ptrs], p[fluid_ptrs - 1]) p_yp = np.where(solid[fluid_ptrs + W] == 1, p[fluid_ptrs], p[fluid_ptrs + W]) p_ym = np.where(solid[fluid_ptrs - W] == 1, p[fluid_ptrs], p[fluid_ptrs - W]) # g = -1/rho * grad(p) u[fluid_ptrs] = u[fluid_ptrs] - ((p_xp - p_xm) >> 3) - (u[fluid_ptrs] >> 6) v[fluid_ptrs] = v[fluid_ptrs] - ((p_yp - p_ym) >> 3) - (v[fluid_ptrs] >> 6) # 固體內部速度強制歸零 u[solid == 1] = 0 v[solid == 1] = 0 # 單次掃描更新壓力場 div = (u[fluid_ptrs + 1] - u[fluid_ptrs - 1]) + (v[fluid_ptrs + W] - v[fluid_ptrs - W]) p[fluid_ptrs] -= (div >> 3) print(f"【狹縫滲流均衡測試】") print(f"左側水池最終平均水位: {np.mean(p_2d[1:-1, 1:48]) / ONE:.4f}") print(f"右側水池最終平均水位: {np.mean(p_2d[1:-1, 52:-1]) / ONE:.4f}") print("-> 兩側水位成功穿過複雜阻擋牆狹縫,動態趨向物理均衡!")
megapx
愛心
77
留言
encourage first comment
有些話想說嗎 快分享出來彼此交流吧!
留言……