import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Parameter simulasi
Lx = 10.0 # Panjang domain pada sumbu x
Ly = 5.0 # Panjang domain pada sumbu y
nx = 100 # Jumlah grid pada sumbu x
ny = 50 # Jumlah grid pada sumbu y
dt = 0.01 # Langkah waktu
T = 5.0 # Waktu total simulasi
# Fungsi untuk menginisialisasi kondisi awal
def initial_condition():
h = np.zeros((nx, ny))
u = np.zeros((nx, ny))
v = np.zeros((nx, ny))
# Mengatur tinggi air dan kecepatan awal pada bagian kiri bendungan
h[:, :int(ny/2)] = 5.0
u[:, :int(ny/2)] = 5.0
return h, u, v
# Fungsi untuk menghitung langkah waktu selanjutnya
def time_step(h, u, v):
g = 9.81 # Gravitasi
# Menghitung gradien tekanan hidrostatis
grad_p_x = np.zeros((nx, ny))
grad_p_y = np.zeros((nx, ny))
grad_p_x[:, :-1] = (h[:, 1:] - h[:, :-1]) / Lx
grad_p_y[:-1, :] = (h[1:, :] - h[:-1, :]) / Ly
# Menghitung gradien kecepatan
grad_u_x = np.zeros((nx, ny))
grad_v_y = np.zeros((nx, ny))
grad_u_x[:, :-1] = (u[:, 1:] - u[:, :-1]) / Lx
grad_v_y[:-1, :] = (v[1:, :] - v[:-1, :]) / Ly
# Menghitung divisi kecepatan
div_u = grad_u_x + grad_v_y
# Menghitung percepatan
a_x = -grad_p_x
a_y = -grad_p_y - g
# Mengupdate tinggi air dan kecepatan
h_new = h - dt * div_u
u_new = u - dt * a_x
v_new = v - dt * a_y
return h_new, u_new, v_new
# Menginisialisasi kondisi awal
h, u, v = initial_condition()
# Menghitung jumlah langkah waktu
n_steps = int(T / dt)
# Simulasi dam break
for i in range(n_steps):
h, u, v = time_step(h, u, v)
# Visualisasi dam break
X, Y = np.meshgrid(np.linspace(0, Lx, nx), np.linspace(0, Ly, ny))
fig = plt.figure(figsize=(12, 6))
ax1 = fig.add_subplot(1, 2, 1, projection='3d')
ax1.plot_surface(X, Y, h.T, cmap='Blues')
ax1.set_xlabel('x')
i have problem with this code, This code is about a 2d simulation dam break, where when running in google collab it can but only images, when in vscode there are no problems but still cannot be run, maybe there is a library that I have to import? is this module matplotlib.animation.FuncAnimation or is there another module?