# 求救，界面不显示

``````import taichi as ti

# 设定模拟参数
ti.init(arch=ti.cpu)
num_cells = 200
cell_size = 5
width, height = num_cells * cell_size, 200
fluid_velocity = 1.0
fluid_density = 1.0
gravity = 9.81

# 创建变量
density = ti.field(ti.f32, shape=(num_cells + 2,))
velocity = ti.Vector.field(2, ti.f32, shape=(num_cells + 1,))
pressure = ti.field(ti.f32, shape=(num_cells + 2,))

# 初始化场景
@ti.kernel
def init():
for i in range(num_cells + 2):
density[i] = fluid_density
pressure[i] = 0.0
for i in range(num_cells + 1):
velocity[i][0] = fluid_velocity
velocity[i][1] = 0.0

# 绘制场景
gui = ti.GUI("Open Channel Flow", res=(width, height))
while gui.running:

gui.get_event()
# 绘制流态
rho = density.to_numpy()
vel = velocity.to_numpy()
pos = [[i, (rho[i] - fluid_density) * 100] for i in range(num_cells + 1)]
for i in range(num_cells):
start_pos = (i * cell_size, height - pos[i][1])
end_pos = ((i+1) * cell_size, height - pos[i+1][1])
gui.line(start_pos, end_pos, radius=cell_size // 2, color=0xFFFFFF)

for i in range(num_cells + 1):
gui.line((i * cell_size, height), (i * cell_size + vel[i][0] * 50, height - vel[i][1] * 50),

# 计算下一帧状态
dt = 0.001
inv_dx = 1.0 / cell_size
for i in range(20):
# 根据密度计算压强
for j in range(1, num_cells + 1):
pressure[j] = 0.5 * (density[j+1] + density[j-1]) * gravity * cell_size \
+ 0.5 * (pressure[j+1] + pressure[j-1])
# 计算速度
for j in range(num_cells + 1):
velocity[j][0] -= dt * inv_dx * (pressure[j+1] - pressure[j])
# 处理边界条件
velocity[0][0] = fluid_velocity
velocity[num_cells][0] = -fluid_velocity
# 计算密度
for j in range(1, num_cells + 1):
density[j] = fluid_density - 0.5 * (
velocity[j + 1][0] - velocity[j - 1][0]) * inv_dx * (density[j + 1] - density[j - 1])

# 刷新GUI
gui.show()
``````

``````gui = ti.GUI('Title', resolution, background_color=0xFFFFFF)
``````