# Program Name: fluid_diffusion_sim.py
# Creation Date: 20250813
# Overview: 2D fluid simulation showing velocity field at t = 0.5 using diffusion only
# Usage: Run to visualize u, v fields with matplotlib quiver plot
import numpy as np
import matplotlib.pyplot as plt
# --- シミュレーションパラメータ(Simulation parameters)---
N = 64 # グリッド分割数(Number of grid points)
L = 1.0 # 領域サイズ(Length of domain)
dx = L / N # 格子間隔(Grid spacing)
nu = 0.01 # 動粘性係数 ν(Kinematic viscosity)
dt = 0.001 # 時間ステップ(Time step size)
t_final = 0.5 # 最終時刻(Final time)
steps = int(t_final / dt)
# --- グリッド(Grid) ---
x = np.linspace(0, L, N)
y = np.linspace(0, L, N)
X, Y = np.meshgrid(x, y)
# --- 初期条件(Initial condition):小さな渦 ---
u = -np.cos(np.pi * X) * np.sin(np.pi * Y)
v = np.sin(np.pi * X) * np.cos(np.pi * Y)
# --- ラプラシアン関数(離散的) ---
def laplacian(f):
return (np.roll(f, -1, axis=0) + np.roll(f, 1, axis=0) +
np.roll(f, -1, axis=1) + np.roll(f, 1, axis=1) -
4 * f) / dx**2
# --- メインループ(時間発展) ---
for step in range(steps):
u_new = u + dt * nu * laplacian(u)
v_new = v + dt * nu * laplacian(v)
u, v = u_new, v_new
# --- 可視化(Quiver plot)---
plt.figure(figsize=(6, 6))
plt.quiver(X, Y, u, v, scale=10, color='blue')
plt.title('Velocity Field at t = 0.5')
plt.xlabel('x')
plt.ylabel('y')
plt.axis('equal')
plt.grid(True)
plt.tight_layout()
plt.show()