-1
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?

  • 4
    *"... it can but only images ..."* - What does that mean? *" ... here are no problems but still cannot be run ..."* - What does that mean? *"Computation of Physics"* - Your question doesn't appear to be about that. It seems to be something to do with displaying you results of a simulation ... not the simulation itself. If you want people to understand what you are asking, your question needs to be clearer than this. – Stephen C Jun 10 '23 at 09:49
  • 1
    A more specific and appropriate and less all-encompassing title for the question is required. – user19077881 Jun 10 '23 at 11:08

1 Answers1

0

Yes, you can use matplotlib.animation. Here is the example that visualizes every step of your simulation:

fig = plt.figure(figsize=(12, 6))
ax1 = fig.add_subplot(1, 2, 1, projection='3d')
ax1.set_xlabel('x')
X, Y = np.meshgrid(np.linspace(0, Lx, nx), np.linspace(0, Ly, ny))
sim = []
for i in range(n_steps):
    h, u, v = time_step(h, u, v)
    sim_step = ax1.plot_surface(X, Y, h.T, cmap='Blues', animated=True)
    sim.append([sim_step])
any = animation.ArtistAnimation(fig, sim, interval=50, blit=True,    repeat_delay=1000)

plt.show()

Note: You have to add import matplotlib.animation as animation.