Datasets:
Download scipy/driven_string_ring/391/simulation_code.py from SourajaKundu123/PhySimCode: direct link, hf CLI and curl.
- Browser
- Download file 3.28 kB
-
https://huggingface.co/datasets/SourajaKundu123/PhySimCode/resolve/main/scipy/driven_string_ring/391/simulation_code.py
- Command line
-
hf download hf://datasets/SourajaKundu123/PhySimCode/scipy/driven_string_ring/391/simulation_code.py
-
curl -L -o simulation_code.py https://huggingface.co/datasets/SourajaKundu123/PhySimCode/resolve/main/scipy/driven_string_ring/391/simulation_code.py
3.28 kB
| """Driven string + free-end ring standalone script.""" | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import matplotlib.animation as animation | |
| from matplotlib.patches import Arc, Circle | |
| L = 4.454427366146007 | |
| mu = 0.06500404519588465 | |
| T = 4.696805493022789 | |
| A = 0.12937720064774744 | |
| f_drive = 2.185502419572134 | |
| gamma_w = 0.6292612342022222 | |
| N = 200 | |
| n_per_frame = 16 | |
| duration = 12 | |
| fps = 30 | |
| omega = 2*np.pi*f_drive | |
| c = np.sqrt(T/mu) | |
| dx = L/N | |
| x_grid = np.linspace(0.0, L, N+1) | |
| dt = 1.0/(fps*n_per_frame) | |
| factor = (c*dt/dx)**2 | |
| damp_n = 1.0/(1.0+gamma_w*dt) | |
| damp_p = 1.0 - gamma_w*dt | |
| assert factor < 1.0, f"CFL violated: factor={factor:.3f}" | |
| u_curr = np.zeros(N+1); u_prev = np.zeros(N+1) | |
| n_frames = int(duration*fps)+1 | |
| U_frames = np.zeros((n_frames, N+1)) | |
| U_frames[0] = u_curr.copy() | |
| for frame in range(1, n_frames): | |
| for sub in range(n_per_frame): | |
| t_curr = (frame-1)/fps + (sub+1)*dt | |
| u_next = np.empty_like(u_curr) | |
| lap_int = u_curr[2:] - 2*u_curr[1:-1] + u_curr[:-2] | |
| u_next[1:-1] = damp_n*(2*u_curr[1:-1] - damp_p*u_prev[1:-1] + factor*lap_int) | |
| lap_right = 2*(u_curr[-2] - u_curr[-1]) | |
| u_next[-1] = damp_n*(2*u_curr[-1] - damp_p*u_prev[-1] + factor*lap_right) | |
| u_next[0] = A*np.sin(omega*t_curr) | |
| u_prev, u_curr = u_curr, u_next | |
| U_frames[frame] = u_curr.copy() | |
| fig_w, fig_h = 12.0, 4.0 | |
| fig_aspect = fig_w/fig_h | |
| x_lo_d, x_hi_d = -0.40, L+0.40 | |
| y_lo_d, y_hi_d = -4.5*A, 4.5*A | |
| dx_d, dy_d = x_hi_d-x_lo_d, y_hi_d-y_lo_d | |
| if dx_d/dy_d < fig_aspect: | |
| extra = (fig_aspect*dy_d - dx_d)/2.0 | |
| x_lo, x_hi = x_lo_d-extra, x_hi_d+extra | |
| y_lo, y_hi = y_lo_d, y_hi_d | |
| else: | |
| extra = (dx_d/fig_aspect - dy_d)/2.0 | |
| x_lo, x_hi = x_lo_d, x_hi_d | |
| y_lo, y_hi = y_lo_d-extra, y_hi_d+extra | |
| fig = plt.figure(figsize=(fig_w,fig_h), facecolor="#1a1a2e") | |
| ax = fig.add_axes([0,0,1,1]); ax.set_facecolor("#16213e") | |
| ax.set_xlim(x_lo,x_hi); ax.set_ylim(y_lo,y_hi) | |
| ax.set_aspect("equal"); ax.axis("off") | |
| ax.axhline(0.0, color="#3a4357", lw=1, ls="--", zorder=0) | |
| ax.plot([0,0],[y_lo,y_hi], color="#4a4e69", lw=1.5, ls=":", zorder=1) | |
| driver_dot = Circle((0,0), 0.04, facecolor="#e63946", | |
| edgecolor="#adb5bd", lw=1.0, zorder=5) | |
| ax.add_patch(driver_dot) | |
| r_x = 0.22; r_y = 0.11 | |
| back_arc = Arc((L,0), 2*r_x, 2*r_y, theta1=0, theta2=180, | |
| edgecolor="#e63946", lw=10, zorder=2) | |
| ax.add_patch(back_arc) | |
| ax.plot([L,L],[y_lo,y_hi], color="#adb5bd", lw=14, solid_capstyle="butt", zorder=3) | |
| front_arc = Arc((L,0), 2*r_x, 2*r_y, theta1=180, theta2=360, | |
| edgecolor="#e63946", lw=10, zorder=5) | |
| ax.add_patch(front_arc) | |
| n_cut = int(round(r_x/dx)) | |
| n_inside = (N+1) - n_cut | |
| str_x_plot = x_grid[:n_inside] | |
| str_line, = ax.plot([],[], color="#a8dadc", lw=2.4, zorder=4) | |
| def update(f): | |
| u = U_frames[f]; yy = u[-1] | |
| str_y = u[:n_inside].copy(); str_y[-1] = yy | |
| str_line.set_data(str_x_plot, str_y) | |
| driver_dot.center = (0, u[0]) | |
| back_arc.set_center((L, yy)) | |
| front_arc.set_center((L, yy)) | |
| return str_line, driver_dot, back_arc, front_arc | |
| ani = animation.FuncAnimation(fig, update, frames=n_frames, | |
| interval=1000.0/fps, blit=False) | |
| ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) | |
| plt.close() | |
| print("Saved video.mp4") | |