└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # water-physics-simulation 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import matplotlib.animation as animation 5 | 6 | # Simulation settings 7 | WIDTH = 100 # Grid width 8 | HEIGHT = 50 # Grid height 9 | DAMPING = 0.99 # Damping factor to reduce oscillations 10 | 11 | # Initialize wave height grids 12 | current_wave = np.zeros((HEIGHT, WIDTH)) 13 | previous_wave = np.zeros((HEIGHT, WIDTH)) 14 | 15 | # Introduce an initial disturbance 16 | def create_wave(): 17 | cx, cy = WIDTH // 2, HEIGHT // 2 # Center of the grid 18 | current_wave[cy, cx] = 1 # Initial disturbance 19 | 20 | # Update wave propagation 21 | def update_wave(): 22 | global current_wave, previous_wave 23 | next_wave = np.zeros((HEIGHT, WIDTH)) 24 | for y in range(1, HEIGHT - 1): 25 | for x in range(1, WIDTH - 1): 26 | next_wave[y, x] = ( 27 | (current_wave[y - 1, x] + current_wave[y + 1, x] + 28 | current_wave[y, x - 1] + current_wave[y, x + 1]) / 2 29 | - previous_wave[y, x] 30 | ) * DAMPING 31 | previous_wave = np.copy(current_wave) 32 | current_wave[:] = next_wave[:] 33 | 34 | # Animation function 35 | def animate(i): 36 | update_wave() 37 | ax.clear() 38 | ax.set_xticks([]) 39 | ax.set_yticks([]) 40 | ax.imshow(current_wave, cmap='Blues', interpolation='bilinear') 41 | 42 | # Initialize figure 43 | fig, ax = plt.subplots() 44 | create_wave() 45 | ani = animation.FuncAnimation(fig, animate, frames=100, interval=50) 46 | plt.show() 47 | --------------------------------------------------------------------------------