File size: 3,101 Bytes
92f32a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Driven String + Fixed End — auto-generated.
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.patches import Circle, Rectangle

L          = 4.033479948082178
mu         = 0.0796094507252327
T          = 4.600910663324425
A          = 0.08755610035054236
f_drive    = 2.216843577784222
gamma_w    = 0.6330378388780827
N          = 200
n_per_frame_in = 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)
n_per_frame = n_per_frame_in
dt = 1.0 / (fps * n_per_frame)
factor = (c * dt / dx) ** 2
while factor >= 1.0:
    n_per_frame *= 2
    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

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)
        u_next[-1] = 0.0
        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 = -0.40; x_hi_d = L + 0.40
y_lo_d = -4.5 * A; y_hi_d = +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)
ax.add_patch(Rectangle((L, y_lo), 0.12, y_hi - y_lo, facecolor="#4a4e69", edgecolor="#adb5bd", lw=1.5, zorder=2))
ax.plot([L, L], [y_lo, y_hi], color="#adb5bd", lw=2.0, zorder=3)
anchor = Circle((L, 0), 0.05, facecolor="#e63946", edgecolor="#adb5bd", lw=1.0, zorder=5)
ax.add_patch(anchor)
str_line, = ax.plot([], [], color="#a8dadc", lw=2.4, zorder=4)

def update(f):
    u = U_frames[f]
    str_line.set_data(x_grid, u)
    driver_dot.center = (0, u[0])
    return str_line, driver_dot, anchor

ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000.0/fps, blit=False)
ani.save("driven_string_fixed.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120)
plt.close(); print("Saved driven_string_fixed.mp4")