import numpy as np
import matplotlib.pyplot as plt

# 模型参数
L = 1000  # 河流长度(单位:米)
dx = 10  # 网格间距(单位:米)
dt = 1  # 时间步长(单位:秒)
T = 3600  # 总模拟时间(单位:秒)
g = 9.81  # 重力加速度(单位:米/秒^2)

# 初始化水位和流速
h = np.zeros(int(L / dx))
u = np.zeros(int(L / dx))

# 模拟时间步进
for t in range(int(T / dt)):
    # 计算水位和流速的变化
    dh_dt = -np.diff(u) / dx
    du_dt = -g * np.diff(h) / dx

    # 更新水位和流速
    h[1:-1] += dh_dt*dt
    u[1:-1] += du_dt*dt

    # 边界条件(开放边界)
    h[0] = h[1]
    h[-1] = h[-2]
    u[0] = u[1]
    u[-1] = u[-2]

# 绘制水位和流速随时间的变化
x = np.arange(0, L, dx)
plt.plot(x, h)
plt.xlabel('Distance (m)')
plt.ylabel('Water Level (m)')
plt.title('Water Level vs. Distance')
plt.show()

plt.plot(x, u)
plt.xlabel('Distance (m)')
plt.ylabel('Flow Velocity (m/s)')
plt.title('Flow Velocity vs. Distance')
plt.show()
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐