diff --git a/scipy/ballistic_pendulum/071/video.mp4 b/scipy/ballistic_pendulum/071/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..bec1b606ffffa6060143e726100093e8e8a2b09c --- /dev/null +++ b/scipy/ballistic_pendulum/071/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e65b5547217d660dff6dc94d3f9fa3b52218332dbb4840f31907fdd953edf24e +size 17540 diff --git a/scipy/ballistic_pendulum/081/cot.json b/scipy/ballistic_pendulum/081/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..5ae55147392db81e9fd0159b11f4ef44c9133add --- /dev/null +++ b/scipy/ballistic_pendulum/081/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.278 kg moves horizontally at v_b=6.067 m/s and embeds itself in a stationary block of mass M=0.626 kg hanging from a rigid massless rod of length L=1.323 m. After the perfectly inelastic impact at t \\approx 0.327 s, the loaded pendulum (combined mass m+M=0.903 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.002 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-1.984 m, impact when x=0 at t=0.3271 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 1.8658 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 3.5410 J (69.2% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 1.4100 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 30.03^{\\circ} \\rightarrow h_max \\approx 0.1776 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-1.98 m to x=0 in 0.327 s.", + "At impact, 69.2% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 30.03^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.309 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0003 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.277854727670406, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 0.6256439206110995, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 6.066916198044913, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 1.3232553605102273, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.002156972960240755, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -1.9841942553255316, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/081/params.json b/scipy/ballistic_pendulum/081/params.json new file mode 100644 index 0000000000000000000000000000000000000000..1efa0b72077fba534653980c51ab161fa89304b5 --- /dev/null +++ b/scipy/ballistic_pendulum/081/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.277854727670406, + "M": 0.6256439206110995, + "v_b": 6.066916198044913, + "L": 1.3232553605102273, + "g": 9.8, + "b": 0.002156972960240755, + "x_b0": -1.9841942553255316 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/081/simulation_code.py b/scipy/ballistic_pendulum/081/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c8394e611cc6eee32576dd2c1f23c98197e12ba8 --- /dev/null +++ b/scipy/ballistic_pendulum/081/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.277854727670406 kg, M=0.6256439206110995 kg, v_b=6.066916198044913 m/s, L=1.3232553605102273 m, + g=9.8 m/s², b=0.002156972960240755 kg·m²/s, x_b0=-1.9841942553255316 m + v_after = m·v_b/(m+M) = 1.8658 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.277854727670406, 0.6256439206110995 +v_b = 6.066916198044913 +L = 1.3232553605102273 +g = 9.8 +b = 0.002156972960240755 +x_b0 = -1.9841942553255316 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/081/video.mp4 b/scipy/ballistic_pendulum/081/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..4b2fdf8528cb6fc3c980e9368226cf33b8546566 --- /dev/null +++ b/scipy/ballistic_pendulum/081/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5f31c0e8f5de1e9f393b52d3fec0ad8a2f34dc4ea5abbceb354d08d94722b3aa +size 19293 diff --git a/scipy/ballistic_pendulum/103/cot.json b/scipy/ballistic_pendulum/103/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..2143743e29d35803fba71422498cf97347ce1d7a --- /dev/null +++ b/scipy/ballistic_pendulum/103/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.296 kg moves horizontally at v_b=7.805 m/s and embeds itself in a stationary block of mass M=1.400 kg hanging from a rigid massless rod of length L=1.127 m. After the perfectly inelastic impact at t \\approx 0.316 s, the loaded pendulum (combined mass m+M=1.696 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.005 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-2.464 m, impact when x=0 at t=0.3157 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 1.3609 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 7.4367 J (82.6% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 1.2073 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 23.63^{\\circ} \\rightarrow h_max \\approx 0.0945 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-2.46 m to x=0 in 0.316 s.", + "At impact, 82.6% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 23.63^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.131 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0004 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.2956730046674745, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 1.4002250627312618, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 7.805472534926552, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 1.1271662214877365, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.004588180863164593, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -2.464067779299508, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/103/params.json b/scipy/ballistic_pendulum/103/params.json new file mode 100644 index 0000000000000000000000000000000000000000..9c8831b753494676824ca7d8f890400733b0660f --- /dev/null +++ b/scipy/ballistic_pendulum/103/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.2956730046674745, + "M": 1.4002250627312618, + "v_b": 7.805472534926552, + "L": 1.1271662214877365, + "g": 9.8, + "b": 0.004588180863164593, + "x_b0": -2.464067779299508 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/103/simulation_code.py b/scipy/ballistic_pendulum/103/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..1e376cf9ad795a855367bb9dce97eb2352bee117 --- /dev/null +++ b/scipy/ballistic_pendulum/103/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.2956730046674745 kg, M=1.4002250627312618 kg, v_b=7.805472534926552 m/s, L=1.1271662214877365 m, + g=9.8 m/s², b=0.004588180863164593 kg·m²/s, x_b0=-2.464067779299508 m + v_after = m·v_b/(m+M) = 1.3609 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.2956730046674745, 1.4002250627312618 +v_b = 7.805472534926552 +L = 1.1271662214877365 +g = 9.8 +b = 0.004588180863164593 +x_b0 = -2.464067779299508 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/103/video.mp4 b/scipy/ballistic_pendulum/103/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1ec8ee4890a2ab2112d40ec4a785a1f1a7328485 --- /dev/null +++ b/scipy/ballistic_pendulum/103/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aa18f59010439c9a850bf1a83b28e1d2a169cdfb8cf960b75c3127c7c23c7528 +size 15178 diff --git a/scipy/ballistic_pendulum/196/cot.json b/scipy/ballistic_pendulum/196/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..59143d0bc01656247c85a2e0c9b7782adea19b20 --- /dev/null +++ b/scipy/ballistic_pendulum/196/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.333 kg moves horizontally at v_b=9.630 m/s and embeds itself in a stationary block of mass M=0.798 kg hanging from a rigid massless rod of length L=1.244 m. After the perfectly inelastic impact at t \\approx 0.194 s, the loaded pendulum (combined mass m+M=1.131 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.164 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-1.867 m, impact when x=0 at t=0.1939 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 2.8336 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 10.8881 J (70.6% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 2.2776 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 47.88^{\\circ} \\rightarrow h_max \\approx 0.4097 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-1.87 m to x=0 in 0.194 s.", + "At impact, 70.6% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 47.88^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.239 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0167 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.33272108048438775, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 0.7980350539079105, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 9.629940574585856, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 1.2441308245379847, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.16372877169616085, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -1.8669171778069973, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/196/params.json b/scipy/ballistic_pendulum/196/params.json new file mode 100644 index 0000000000000000000000000000000000000000..ce16400273deb39085076082499545667ed86652 --- /dev/null +++ b/scipy/ballistic_pendulum/196/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.33272108048438775, + "M": 0.7980350539079105, + "v_b": 9.629940574585856, + "L": 1.2441308245379847, + "g": 9.8, + "b": 0.16372877169616085, + "x_b0": -1.8669171778069973 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/196/simulation_code.py b/scipy/ballistic_pendulum/196/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..8e6ce3103a51023eb803bafea087573c89436ac7 --- /dev/null +++ b/scipy/ballistic_pendulum/196/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.33272108048438775 kg, M=0.7980350539079105 kg, v_b=9.629940574585856 m/s, L=1.2441308245379847 m, + g=9.8 m/s², b=0.16372877169616085 kg·m²/s, x_b0=-1.8669171778069973 m + v_after = m·v_b/(m+M) = 2.8336 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.33272108048438775, 0.7980350539079105 +v_b = 9.629940574585856 +L = 1.2441308245379847 +g = 9.8 +b = 0.16372877169616085 +x_b0 = -1.8669171778069973 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/196/video.mp4 b/scipy/ballistic_pendulum/196/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7e1b4877daa2cbcc2b1e06be9b7ee31d07c3809f --- /dev/null +++ b/scipy/ballistic_pendulum/196/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8c49756846e9b9db23a5590a6fd3d3d77de559429731b7f2917b98dcca2e31dc +size 21273 diff --git a/scipy/ballistic_pendulum/233/cot.json b/scipy/ballistic_pendulum/233/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..2082a678abb89c83be68bfa01dc7403de901dbec --- /dev/null +++ b/scipy/ballistic_pendulum/233/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.231 kg moves horizontally at v_b=7.831 m/s and embeds itself in a stationary block of mass M=0.698 kg hanging from a rigid massless rod of length L=1.216 m. After the perfectly inelastic impact at t \\approx 0.344 s, the loaded pendulum (combined mass m+M=0.929 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.060 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-2.697 m, impact when x=0 at t=0.3445 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 1.9493 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 5.3283 J (75.1% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 1.6033 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 32.80^{\\circ} \\rightarrow h_max \\approx 0.1939 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-2.70 m to x=0 in 0.344 s.", + "At impact, 75.1% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 32.80^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.213 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0076 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.23136581642631313, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 0.6981118089917856, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 7.830975332029041, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 1.2157684831760527, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.059512999463699506, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -2.6974548720002978, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/233/params.json b/scipy/ballistic_pendulum/233/params.json new file mode 100644 index 0000000000000000000000000000000000000000..bc2465e7e2396d869d9cade2332e1c0fe65c53fe --- /dev/null +++ b/scipy/ballistic_pendulum/233/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.23136581642631313, + "M": 0.6981118089917856, + "v_b": 7.830975332029041, + "L": 1.2157684831760527, + "g": 9.8, + "b": 0.059512999463699506, + "x_b0": -2.6974548720002978 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/233/simulation_code.py b/scipy/ballistic_pendulum/233/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..b5f728739cf01d5ee0fafc02844357e7e135d8e7 --- /dev/null +++ b/scipy/ballistic_pendulum/233/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.23136581642631313 kg, M=0.6981118089917856 kg, v_b=7.830975332029041 m/s, L=1.2157684831760527 m, + g=9.8 m/s², b=0.059512999463699506 kg·m²/s, x_b0=-2.6974548720002978 m + v_after = m·v_b/(m+M) = 1.9493 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.23136581642631313, 0.6981118089917856 +v_b = 7.830975332029041 +L = 1.2157684831760527 +g = 9.8 +b = 0.059512999463699506 +x_b0 = -2.6974548720002978 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/233/video.mp4 b/scipy/ballistic_pendulum/233/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..30f07f38e56da28fe04b9d4d7ac3c038cd6b11db --- /dev/null +++ b/scipy/ballistic_pendulum/233/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bb83e8d23c8f6704f8eea3ce24e00c9c75e0b8fcdf7ddc9d48650e2629554cb1 +size 16584 diff --git a/scipy/ballistic_pendulum/238/cot.json b/scipy/ballistic_pendulum/238/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..25012f41448d5400e6e23142dc79cc765886c658 --- /dev/null +++ b/scipy/ballistic_pendulum/238/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.361 kg moves horizontally at v_b=9.018 m/s and embeds itself in a stationary block of mass M=0.806 kg hanging from a rigid massless rod of length L=1.301 m. After the perfectly inelastic impact at t \\approx 0.208 s, the loaded pendulum (combined mass m+M=1.167 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.131 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-1.872 m, impact when x=0 at t=0.2076 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 2.7888 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 10.1356 J (69.1% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 2.1439 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 45.98^{\\circ} \\rightarrow h_max \\approx 0.3968 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-1.87 m to x=0 in 0.208 s.", + "At impact, 69.1% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 45.98^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.289 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0121 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.36087622474370185, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 0.8060352909881511, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 9.017838224762661, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 1.3008299864937731, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.13115678214663504, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -1.8722626254798886, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/238/params.json b/scipy/ballistic_pendulum/238/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c259c23a0c0c97f9c2defb2e8e2c32b203cf47c8 --- /dev/null +++ b/scipy/ballistic_pendulum/238/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.36087622474370185, + "M": 0.8060352909881511, + "v_b": 9.017838224762661, + "L": 1.3008299864937731, + "g": 9.8, + "b": 0.13115678214663504, + "x_b0": -1.8722626254798886 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/238/simulation_code.py b/scipy/ballistic_pendulum/238/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..b7d7189dea8ef560bb1532051ef402b70616eecb --- /dev/null +++ b/scipy/ballistic_pendulum/238/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.36087622474370185 kg, M=0.8060352909881511 kg, v_b=9.017838224762661 m/s, L=1.3008299864937731 m, + g=9.8 m/s², b=0.13115678214663504 kg·m²/s, x_b0=-1.8722626254798886 m + v_after = m·v_b/(m+M) = 2.7888 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.36087622474370185, 0.8060352909881511 +v_b = 9.017838224762661 +L = 1.3008299864937731 +g = 9.8 +b = 0.13115678214663504 +x_b0 = -1.8722626254798886 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/238/video.mp4 b/scipy/ballistic_pendulum/238/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..de8249974e2fbafe203720c31bb29e2bbd519a25 --- /dev/null +++ b/scipy/ballistic_pendulum/238/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4546103e548b98f332adf74feb4e33a3d82102cebe2a9d4ca77f7f912f53e15e +size 21099 diff --git a/scipy/ballistic_pendulum/284/cot.json b/scipy/ballistic_pendulum/284/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..a61341308ae1aa3055ceebf4c781edd5424c9085 --- /dev/null +++ b/scipy/ballistic_pendulum/284/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.207 kg moves horizontally at v_b=7.329 m/s and embeds itself in a stationary block of mass M=1.361 kg hanging from a rigid massless rod of length L=0.961 m. After the perfectly inelastic impact at t \\approx 0.362 s, the loaded pendulum (combined mass m+M=1.568 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.166 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-2.654 m, impact when x=0 at t=0.3621 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 0.9673 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 4.8256 J (86.8% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 1.0070 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 18.14^{\\circ} \\rightarrow h_max \\approx 0.0477 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-2.65 m to x=0 in 0.362 s.", + "At impact, 86.8% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 18.14^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 1.967 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0180 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.20701369973520545, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 1.3613513926580583, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 7.328711384042167, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 0.9606327137861861, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.16596822945010736, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -2.6537813600815108, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/284/params.json b/scipy/ballistic_pendulum/284/params.json new file mode 100644 index 0000000000000000000000000000000000000000..ac1062b07e9c20075e66707fd34d312e1b490159 --- /dev/null +++ b/scipy/ballistic_pendulum/284/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.20701369973520545, + "M": 1.3613513926580583, + "v_b": 7.328711384042167, + "L": 0.9606327137861861, + "g": 9.8, + "b": 0.16596822945010736, + "x_b0": -2.6537813600815108 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/284/simulation_code.py b/scipy/ballistic_pendulum/284/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..fae93ef2320bad56f2cd7ecbbe4342a67e1641e4 --- /dev/null +++ b/scipy/ballistic_pendulum/284/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.20701369973520545 kg, M=1.3613513926580583 kg, v_b=7.328711384042167 m/s, L=0.9606327137861861 m, + g=9.8 m/s², b=0.16596822945010736 kg·m²/s, x_b0=-2.6537813600815108 m + v_after = m·v_b/(m+M) = 0.9673 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.20701369973520545, 1.3613513926580583 +v_b = 7.328711384042167 +L = 0.9606327137861861 +g = 9.8 +b = 0.16596822945010736 +x_b0 = -2.6537813600815108 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/284/video.mp4 b/scipy/ballistic_pendulum/284/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..37f7cbe1affd100eb851aa776b119c57ab7bff32 --- /dev/null +++ b/scipy/ballistic_pendulum/284/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a31a3ffeb306a9dd325f08264b1caf6afaa45a104221ecf918b1fe86ba5404b0 +size 12404 diff --git a/scipy/ballistic_pendulum/296/cot.json b/scipy/ballistic_pendulum/296/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..0cd5e927c35128c1a3ed3230daaaec3cc9ce8ec7 --- /dev/null +++ b/scipy/ballistic_pendulum/296/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.230 kg moves horizontally at v_b=5.771 m/s and embeds itself in a stationary block of mass M=1.375 kg hanging from a rigid massless rod of length L=0.994 m. After the perfectly inelastic impact at t \\approx 0.456 s, the loaded pendulum (combined mass m+M=1.604 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.121 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-2.630 m, impact when x=0 at t=0.4558 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 0.8257 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 3.2754 J (85.7% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 0.8305 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 15.20^{\\circ} \\rightarrow h_max \\approx 0.0348 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-2.63 m to x=0 in 0.456 s.", + "At impact, 85.7% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 15.20^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.001 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0121 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.22955961604757902, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 1.3748651298448733, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 5.7706881828296455, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 0.9942069151835331, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.12086343072208233, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -2.6303622718108217, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/296/params.json b/scipy/ballistic_pendulum/296/params.json new file mode 100644 index 0000000000000000000000000000000000000000..9cda8586ed4b75f2080de7976381b72e208ac5e8 --- /dev/null +++ b/scipy/ballistic_pendulum/296/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.22955961604757902, + "M": 1.3748651298448733, + "v_b": 5.7706881828296455, + "L": 0.9942069151835331, + "g": 9.8, + "b": 0.12086343072208233, + "x_b0": -2.6303622718108217 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/296/simulation_code.py b/scipy/ballistic_pendulum/296/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..8d12bc90b5f0f6e58d912eb5ae96f14fd68072ae --- /dev/null +++ b/scipy/ballistic_pendulum/296/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.22955961604757902 kg, M=1.3748651298448733 kg, v_b=5.7706881828296455 m/s, L=0.9942069151835331 m, + g=9.8 m/s², b=0.12086343072208233 kg·m²/s, x_b0=-2.6303622718108217 m + v_after = m·v_b/(m+M) = 0.8257 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.22955961604757902, 1.3748651298448733 +v_b = 5.7706881828296455 +L = 0.9942069151835331 +g = 9.8 +b = 0.12086343072208233 +x_b0 = -2.6303622718108217 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/296/video.mp4 b/scipy/ballistic_pendulum/296/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..993f76dac645340c967b66a704715c9ab12270ae --- /dev/null +++ b/scipy/ballistic_pendulum/296/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bef95779603ca39032c7ac60b89e4f548376cb55c288cf314460c99446bfd9d0 +size 12283 diff --git a/scipy/ballistic_pendulum/300/cot.json b/scipy/ballistic_pendulum/300/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..edadba9aa2185c43eea82f4c6db1dd396ae6ac67 --- /dev/null +++ b/scipy/ballistic_pendulum/300/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.341 kg moves horizontally at v_b=8.849 m/s and embeds itself in a stationary block of mass M=1.171 kg hanging from a rigid massless rod of length L=1.336 m. After the perfectly inelastic impact at t \\approx 0.317 s, the loaded pendulum (combined mass m+M=1.512 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.100 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-2.802 m, impact when x=0 at t=0.3167 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 1.9981 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 10.3488 J (77.4% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 1.4953 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 32.05^{\\circ} \\rightarrow h_max \\approx 0.2037 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-2.80 m to x=0 in 0.317 s.", + "At impact, 77.4% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 32.05^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.320 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0068 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.34140775028832265, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 1.170627099286853, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 8.849013423019514, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 1.3361843924056986, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.1001355750309848, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -2.802402198347533, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/300/params.json b/scipy/ballistic_pendulum/300/params.json new file mode 100644 index 0000000000000000000000000000000000000000..7d39bba34b4d37d0556c413b8e4fe479cb2c5fc0 --- /dev/null +++ b/scipy/ballistic_pendulum/300/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.34140775028832265, + "M": 1.170627099286853, + "v_b": 8.849013423019514, + "L": 1.3361843924056986, + "g": 9.8, + "b": 0.1001355750309848, + "x_b0": -2.802402198347533 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/300/simulation_code.py b/scipy/ballistic_pendulum/300/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..2650c6ed0dfbd454b13c8e4249d32b466c9bff86 --- /dev/null +++ b/scipy/ballistic_pendulum/300/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.34140775028832265 kg, M=1.170627099286853 kg, v_b=8.849013423019514 m/s, L=1.3361843924056986 m, + g=9.8 m/s², b=0.1001355750309848 kg·m²/s, x_b0=-2.802402198347533 m + v_after = m·v_b/(m+M) = 1.9981 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.34140775028832265, 1.170627099286853 +v_b = 8.849013423019514 +L = 1.3361843924056986 +g = 9.8 +b = 0.1001355750309848 +x_b0 = -2.802402198347533 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/300/video.mp4 b/scipy/ballistic_pendulum/300/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..33b02ee2c6ea03aea9c4f4db83f6dba1d8a3c1aa --- /dev/null +++ b/scipy/ballistic_pendulum/300/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fde0763c8bc221593b6377985c5e8acb54f97fea8ba701b81a869ea0ab6aafdc +size 16481 diff --git a/scipy/ballistic_pendulum/370/cot.json b/scipy/ballistic_pendulum/370/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..74d0b29428cc3307c0ba7d7b5985146757b2b12d --- /dev/null +++ b/scipy/ballistic_pendulum/370/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.376 kg moves horizontally at v_b=7.118 m/s and embeds itself in a stationary block of mass M=0.828 kg hanging from a rigid massless rod of length L=1.315 m. After the perfectly inelastic impact at t \\approx 0.374 s, the loaded pendulum (combined mass m+M=1.204 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.149 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-2.663 m, impact when x=0 at t=0.3742 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 2.2222 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 6.5498 J (68.8% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 1.6901 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 36.06^{\\circ} \\rightarrow h_max \\approx 0.2519 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-2.66 m to x=0 in 0.374 s.", + "At impact, 68.8% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 36.06^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.301 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0131 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.37586387793789355, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 0.8281380197518744, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 7.118267834510721, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 1.3148326528708325, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.14860774580133482, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -2.6634988868603466, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/370/params.json b/scipy/ballistic_pendulum/370/params.json new file mode 100644 index 0000000000000000000000000000000000000000..ec3d91d214f1c15d5dfdca00eeb2e9d5dd3cd0fc --- /dev/null +++ b/scipy/ballistic_pendulum/370/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.37586387793789355, + "M": 0.8281380197518744, + "v_b": 7.118267834510721, + "L": 1.3148326528708325, + "g": 9.8, + "b": 0.14860774580133482, + "x_b0": -2.6634988868603466 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/370/simulation_code.py b/scipy/ballistic_pendulum/370/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d4673860dbfec184d3177729a88c72fae5aff069 --- /dev/null +++ b/scipy/ballistic_pendulum/370/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.37586387793789355 kg, M=0.8281380197518744 kg, v_b=7.118267834510721 m/s, L=1.3148326528708325 m, + g=9.8 m/s², b=0.14860774580133482 kg·m²/s, x_b0=-2.6634988868603466 m + v_after = m·v_b/(m+M) = 2.2222 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.37586387793789355, 0.8281380197518744 +v_b = 7.118267834510721 +L = 1.3148326528708325 +g = 9.8 +b = 0.14860774580133482 +x_b0 = -2.6634988868603466 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/370/video.mp4 b/scipy/ballistic_pendulum/370/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..94e68a92e8aaf52ab2b04b37c2a834c9f6ff3274 --- /dev/null +++ b/scipy/ballistic_pendulum/370/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4f21ad4fc4a74c8991bc9e389b706ce761ea5fe2a6de6c8b3ec3a32e11e0c69 +size 17073 diff --git a/scipy/ballistic_pendulum/389/cot.json b/scipy/ballistic_pendulum/389/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..9b7f3fd80635759e772c1343221c624a91eb1b0e --- /dev/null +++ b/scipy/ballistic_pendulum/389/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.137 kg moves horizontally at v_b=10.537 m/s and embeds itself in a stationary block of mass M=1.272 kg hanging from a rigid massless rod of length L=1.289 m. After the perfectly inelastic impact at t \\approx 0.175 s, the loaded pendulum (combined mass m+M=1.409 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.002 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-1.848 m, impact when x=0 at t=0.1754 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 1.0227 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 6.8544 J (90.3% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 0.7931 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 16.54^{\\circ} \\rightarrow h_max \\approx 0.0534 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-1.85 m to x=0 in 0.175 s.", + "At impact, 90.3% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 16.54^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.279 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0002 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.1367495085430944, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 1.2721931750134614, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 10.536762883484913, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 1.2894580530885746, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.002460173781960262, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -1.8481107949142412, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/389/params.json b/scipy/ballistic_pendulum/389/params.json new file mode 100644 index 0000000000000000000000000000000000000000..ca4e62b014d6d66fac65283e2933ec962d60525a --- /dev/null +++ b/scipy/ballistic_pendulum/389/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.1367495085430944, + "M": 1.2721931750134614, + "v_b": 10.536762883484913, + "L": 1.2894580530885746, + "g": 9.8, + "b": 0.002460173781960262, + "x_b0": -1.8481107949142412 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/389/simulation_code.py b/scipy/ballistic_pendulum/389/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..74dcc656e28d679f59630f6ac2b2de80aee9421e --- /dev/null +++ b/scipy/ballistic_pendulum/389/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.1367495085430944 kg, M=1.2721931750134614 kg, v_b=10.536762883484913 m/s, L=1.2894580530885746 m, + g=9.8 m/s², b=0.002460173781960262 kg·m²/s, x_b0=-1.8481107949142412 m + v_after = m·v_b/(m+M) = 1.0227 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.1367495085430944, 1.2721931750134614 +v_b = 10.536762883484913 +L = 1.2894580530885746 +g = 9.8 +b = 0.002460173781960262 +x_b0 = -1.8481107949142412 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/389/video.mp4 b/scipy/ballistic_pendulum/389/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..4faad7cea298e80de8cf09774a3aeb9e82cceddb --- /dev/null +++ b/scipy/ballistic_pendulum/389/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:74d784da1446ad538a3ffc93a6583029aaa0dce4eac8aa991a38ab9158b23ab4 +size 16316 diff --git a/scipy/ballistic_pendulum/464/cot.json b/scipy/ballistic_pendulum/464/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..6a5d33497c48be1aa5db1faaaeb2732441899bee --- /dev/null +++ b/scipy/ballistic_pendulum/464/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.143 kg moves horizontally at v_b=8.600 m/s and embeds itself in a stationary block of mass M=1.091 kg hanging from a rigid massless rod of length L=1.326 m. After the perfectly inelastic impact at t \\approx 0.247 s, the loaded pendulum (combined mass m+M=1.234 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.081 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-2.126 m, impact when x=0 at t=0.2473 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 0.9972 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 4.6797 J (88.4% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 0.7522 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 15.90^{\\circ} \\rightarrow h_max \\approx 0.0507 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-2.13 m to x=0 in 0.247 s.", + "At impact, 88.4% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 15.90^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.311 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0068 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.14314511159886073, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 1.09135106633226, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 8.59995509786118, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 1.3257054289917123, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.08062926759352221, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -2.1264181561810274, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/464/params.json b/scipy/ballistic_pendulum/464/params.json new file mode 100644 index 0000000000000000000000000000000000000000..7c56edeabe261c8c4f727a9b6eb43a64aa6f2f99 --- /dev/null +++ b/scipy/ballistic_pendulum/464/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.14314511159886073, + "M": 1.09135106633226, + "v_b": 8.59995509786118, + "L": 1.3257054289917123, + "g": 9.8, + "b": 0.08062926759352221, + "x_b0": -2.1264181561810274 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/464/simulation_code.py b/scipy/ballistic_pendulum/464/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d993446da10ff2f2b94933aaa54d8dc85a4b8eea --- /dev/null +++ b/scipy/ballistic_pendulum/464/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.14314511159886073 kg, M=1.09135106633226 kg, v_b=8.59995509786118 m/s, L=1.3257054289917123 m, + g=9.8 m/s², b=0.08062926759352221 kg·m²/s, x_b0=-2.1264181561810274 m + v_after = m·v_b/(m+M) = 0.9972 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.14314511159886073, 1.09135106633226 +v_b = 8.59995509786118 +L = 1.3257054289917123 +g = 9.8 +b = 0.08062926759352221 +x_b0 = -2.1264181561810274 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/464/video.mp4 b/scipy/ballistic_pendulum/464/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f64232cef468eab289f3597ec022909b3c3b109b --- /dev/null +++ b/scipy/ballistic_pendulum/464/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2d2f3c19447e5fe432d1f09a0f9d3cb002211f15653ea2dfac7d53daa0d284f6 +size 15180 diff --git a/scipy/ballistic_pendulum/565/cot.json b/scipy/ballistic_pendulum/565/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ba7ce55bad3e9ced34f271ce0853d5d3334ccf17 --- /dev/null +++ b/scipy/ballistic_pendulum/565/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.243 kg moves horizontally at v_b=7.454 m/s and embeds itself in a stationary block of mass M=0.509 kg hanging from a rigid massless rod of length L=1.378 m. After the perfectly inelastic impact at t \\approx 0.376 s, the loaded pendulum (combined mass m+M=0.752 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.139 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-2.803 m, impact when x=0 at t=0.3760 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 2.4091 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 4.5702 J (67.7% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 1.7485 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 38.27^{\\circ} \\rightarrow h_max \\approx 0.2961 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-2.80 m to x=0 in 0.376 s.", + "At impact, 67.7% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 38.27^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.356 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0182 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.24308081369008974, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 0.5090227035207093, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 7.4538203382365475, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 1.377794309528464, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.13871425723151906, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -2.8026935662800985, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/565/params.json b/scipy/ballistic_pendulum/565/params.json new file mode 100644 index 0000000000000000000000000000000000000000..932b6e6123f9b453d817ff6b039d084c3dd07fbe --- /dev/null +++ b/scipy/ballistic_pendulum/565/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.24308081369008974, + "M": 0.5090227035207093, + "v_b": 7.4538203382365475, + "L": 1.377794309528464, + "g": 9.8, + "b": 0.13871425723151906, + "x_b0": -2.8026935662800985 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/565/simulation_code.py b/scipy/ballistic_pendulum/565/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..b1fd0ec72f168f501faf1d5491e2db9d9695d250 --- /dev/null +++ b/scipy/ballistic_pendulum/565/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.24308081369008974 kg, M=0.5090227035207093 kg, v_b=7.4538203382365475 m/s, L=1.377794309528464 m, + g=9.8 m/s², b=0.13871425723151906 kg·m²/s, x_b0=-2.8026935662800985 m + v_after = m·v_b/(m+M) = 2.4091 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.24308081369008974, 0.5090227035207093 +v_b = 7.4538203382365475 +L = 1.377794309528464 +g = 9.8 +b = 0.13871425723151906 +x_b0 = -2.8026935662800985 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/565/video.mp4 b/scipy/ballistic_pendulum/565/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..bdea6b703205b1792bc1d26049ff1a2b964bf3de --- /dev/null +++ b/scipy/ballistic_pendulum/565/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:409ac173a2bc4b0df8a69beefd51202c292fc6edd6150d0b4829abab1b32c084 +size 16724 diff --git a/scipy/ballistic_pendulum/591/cot.json b/scipy/ballistic_pendulum/591/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ebf79f19d6269b67484169ffaae1419a3cf6cd69 --- /dev/null +++ b/scipy/ballistic_pendulum/591/cot.json @@ -0,0 +1,112 @@ +{ + "simulation": "ballistic_pendulum", + "physical_observation": "A small projectile of mass m=0.315 kg moves horizontally at v_b=8.055 m/s and embeds itself in a stationary block of mass M=0.554 kg hanging from a rigid massless rod of length L=1.232 m. After the perfectly inelastic impact at t \\approx 0.248 s, the loaded pendulum (combined mass m+M=0.869 kg) swings up under gravity to a maximum angle, then oscillates with light damping (b=0.067 kg \\cdot m^2/s).", + "physical_law": { + "name": "Conservation of Linear Momentum + Conservation of Mechanical Energy", + "statement": "(1) Across an instantaneous collision with no external impulse, total linear momentum is conserved: \\Sigma p_before = \\Sigma p_after. (2) After the perfectly inelastic impact, only conservative forces and (small) damping act on the loaded pendulum, so mechanical energy is conserved up to the work done by damping; in the b \\rightarrow 0 limit, the post-impact KE equals the gain in gravitational PE at the highest point.", + "formula": "Collision: m \\cdot v_b = (m+M) \\cdot v_after \\rightarrow v_after = m \\cdot v_b/(m+M). Swing (b=0): \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max) \\rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + }, + "ode_system": { + "state_variables": [ + "\\theta (rod angle from downward vertical, rad)", + "\\omega = \\dot{\\theta} (angular velocity, rad/s)" + ], + "equations_latex": [ + "Pre-impact (t < t_impact): bullet at constant velocity, \\theta = 0, \\omega = 0.", + "Impact (t = t_impact): \\theta unchanged; \\omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + " \\dot{\\theta} = \\omega", + " \\dot{\\omega} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\omega" + ], + "equations_python": [ + "Pre-impact (t < t_impact): bullet at constant velocity, theta = 0, omega = 0.", + "Impact (t = t_impact): theta unchanged; omega jumps to v_after/L by momentum conservation.", + "Post-impact (t > t_impact):", + "theta_dot = omega", + "omega_dot = -(g/L) * np.sin ( theta ) - (b/((m+M) * L**2)) * omega" + ] + }, + "derivation_steps": [ + "Phase 1 --- Pre-impact ballistic flight:", + " Bullet flies horizontally at constant velocity v_b (gravity neglected over the brief flight).", + " x_bullet(t) = x_b0 + v_b \\cdot t (x_b0=-1.999 m, impact when x=0 at t=0.2482 s)", + "Phase 2 --- Inelastic collision (instantaneous):", + " External horizontal forces (rod tension, gravity) are negligible during contact \\Rightarrow linear momentum is conserved.", + " Conservation of linear momentum: m \\cdot v_b = (m+M) \\cdot v_after", + " \\Rightarrow v_after = m \\cdot v_b/(m+M) = 2.9223 m/s", + " Energy lost to deformation/heat: \\DeltaKE = \\tfrac{1}{2} m v_b^2 - \\tfrac{1}{2} (m+M)v_after^2 = 6.5184 J (63.7% of pre-impact KE)", + "Phase 3 --- Loaded pendulum:", + " Treat (m+M) as a point mass at distance L from the pivot \\Rightarrow I = (m+M) \\cdot L^2.", + " Torques about the pivot:", + " \\tau_gravity = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) (restoring)", + " \\tau_damping = -b \\cdot \\dot{\\theta} (b is angular damping, kg \\cdot m^2/s)", + " Newton's 2nd law for rotation: I \\cdot \\ddot{\\theta} = \\Sigma\\tau", + " (m+M) \\cdot L^2 \\cdot \\ddot{\\theta} = -(m+M) \\cdot g \\cdot L \\cdot \\sin(\\theta) - b \\cdot \\dot{\\theta}", + " \\Rightarrow \\ddot{\\theta} = -(g/L) \\cdot \\sin(\\theta) - (b/((m+M) \\cdot L^2)) \\cdot \\dot{\\theta}", + "Initial conditions at t = t_impact: \\theta = 0, \\dot{\\theta} = \\omega_after = v_after/L", + " \\omega_after = v_after/L = 2.3728 rad/s", + "Phase 4 --- Inferring v_b from observed \\theta_max (b \\rightarrow 0 limit):", + " Energy conservation during the swing: \\tfrac{1}{2} (m+M)v_after^2 = (m+M) \\cdot g \\cdot h_max", + " Geometry: h_max = L \\cdot (1 - \\cos(\\theta)_max)", + " \\Rightarrow v_after = \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Substituting v_after = m \\cdot v_b/(m+M):", + " \\Rightarrow v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))", + " Predicted (undamped) \\theta_max \\approx 49.74^{\\circ} \\rightarrow h_max \\approx 0.4357 m" + ], + "numerical_method": { + "solver": "Two-phase: analytic constant-velocity flight, then RK45 for the loaded pendulum", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "The collision is treated analytically (instantaneous, perfectly inelastic) by applying linear-momentum conservation as a discontinuous jump in the angular velocity at t = t_impact. The post-impact ODE is integrated with scipy.solve_ivp's RK45 (Dormand-Prince) at tight tolerances." + }, + "code_validation": { + "ode_source": "Newton's 2nd law for rotation about a fixed axis applied to the loaded (m+M) pendulum; momentum conservation enforces the impact jump.", + "momentum_check": "m \\cdot v_b \\equiv (m+M) \\cdot v_after at the impact instant (analytic, exact).", + "energy_check": "Mechanical energy E = \\tfrac{1}{2} (m+M)L^2\\omega^2 + (m+M)gL(1 - \\cos(\\theta)) decreases monotonically after impact when b > 0 and is conserved when b = 0.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Pre-impact: bullet travels straight from x=-2.00 m to x=0 in 0.248 s.", + "At impact, 63.7% of the bullet's kinetic energy is dissipated (perfectly inelastic).", + "Predicted maximum swing angle (undamped): \\theta_max \\approx 49.74^{\\circ}.", + "Natural small-angle period of the loaded pendulum: T_0 \\approx 2.227 s.", + "Damping ratio \\zeta = b/(2(m+M)L^2 \\cdot \\omega_0) = 0.0090 < 1 \\rightarrow underdamped oscillations decay slowly.", + "v_b can be reconstructed from the observed \\theta_max via v_b = ((m+M)/m) \\cdot \\sqrt (2 \\cdot g \\cdot L \\cdot (1 - \\cos(\\theta)_max))." + ], + "parameters": { + "m": { + "value": 0.31533280636498684, + "unit": "kg", + "description": "Bullet/projectile mass" + }, + "M": { + "value": 0.5538448002990061, + "unit": "kg", + "description": "Hanging-block mass" + }, + "v_b": { + "value": 8.05493930612291, + "unit": "m/s", + "description": "Bullet initial speed" + }, + "L": { + "value": 1.2315939710262125, + "unit": "m", + "description": "Pendulum rod length" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "Gravitational acceleration" + }, + "b": { + "value": 0.06711845691466987, + "unit": "kg \\cdot m^2/s", + "description": "Angular damping coefficient" + }, + "x_b0": { + "value": -1.9991113017150832, + "unit": "m", + "description": "Bullet initial x-position" + } + } +} diff --git a/scipy/ballistic_pendulum/591/params.json b/scipy/ballistic_pendulum/591/params.json new file mode 100644 index 0000000000000000000000000000000000000000..41fa1a6afbfd6d1bc22ca7ab4746edcf9c308607 --- /dev/null +++ b/scipy/ballistic_pendulum/591/params.json @@ -0,0 +1,9 @@ +{ + "m": 0.31533280636498684, + "M": 0.5538448002990061, + "v_b": 8.05493930612291, + "L": 1.2315939710262125, + "g": 9.8, + "b": 0.06711845691466987, + "x_b0": -1.9991113017150832 +} \ No newline at end of file diff --git a/scipy/ballistic_pendulum/591/simulation_code.py b/scipy/ballistic_pendulum/591/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..9b232090a58b2c4ebe3e49b2120efc0c1b70d738 --- /dev/null +++ b/scipy/ballistic_pendulum/591/simulation_code.py @@ -0,0 +1,107 @@ +""" +Ballistic Pendulum — auto-generated simulation script. + +Physics: + 1. Inelastic collision (instantaneous): m·v_b = (m+M)·v_after + 2. Loaded pendulum after impact: + θ̈ = -(g/L)·sin(θ) - (b/((m+M)·L²))·θ̇ + 3. From the swing's max angle θ_max one recovers v_b via + v_b = ((m+M)/m)·sqrt(2·g·L·(1 - cos θ_max)) (b → 0 limit) + +Parameters: m=0.31533280636498684 kg, M=0.5538448002990061 kg, v_b=8.05493930612291 m/s, L=1.2315939710262125 m, + g=9.8 m/s², b=0.06711845691466987 kg·m²/s, x_b0=-1.9991113017150832 m + v_after = m·v_b/(m+M) = 2.9223 m/s +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +m, M = 0.31533280636498684, 0.5538448002990061 +v_b = 8.05493930612291 +L = 1.2315939710262125 +g = 9.8 +b = 0.06711845691466987 +x_b0 = -1.9991113017150832 +duration = 12 +fps = 30 + + +t_impact = max(0.0, -x_b0 / v_b) +v_after = m * v_b / (m + M) +omega_after = v_after / L + + +def ode(t, y): + theta, omega = y + Iover = (m + M) * L * L + dtheta = omega + domega = -(g / L) * np.sin(theta) - (b / Iover) * omega + return [dtheta, domega] + + +n_frames = int(duration * fps) + 1 +t_eval = np.linspace(0, duration, n_frames) +pre_mask = t_eval < t_impact +post_mask = ~pre_mask + +bullet_x = np.empty(n_frames) +bullet_y = np.full(n_frames, -L) +theta = np.zeros(n_frames) +embedded = np.zeros(n_frames) + +bullet_x[pre_mask] = x_b0 + v_b * t_eval[pre_mask] +bullet_x[post_mask] = 0.0 + +if np.any(post_mask): + post_t_local = t_eval[post_mask] - t_impact + if post_t_local[-1] > 0: + sol = solve_ivp(ode, (0.0, post_t_local[-1]), + [0.0, omega_after], t_eval=post_t_local, + method="RK45", rtol=1e-9, atol=1e-11) + theta[post_mask] = sol.y[0] + bullet_x[post_mask] = L * np.sin(theta[post_mask]) + bullet_y[post_mask] = -L * np.cos(theta[post_mask]) + embedded[post_mask] = 1.0 + + +bw = max(0.16, 0.10 * L); bh = max(0.16, 0.10 * L) +pad = 0.4 +xlim = (x_b0 - 0.3, L + pad) +ylim = (-L - max(bh, 0.4), 0.6) + +fig, ax = plt.subplots(figsize=(7, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(xlim); ax.set_ylim(ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.plot([0], [0], "o", color="white", ms=7, zorder=6) +ax.axvline(0, color="#4a4e69", lw=0.8, ls="--", alpha=0.5, zorder=1) +ax.axhline(-L, color="#4a4e69", lw=0.6, ls=":", alpha=0.4, zorder=1) +ax.plot([x_b0, 0], [-L, -L], color="#4a4e69", lw=0.8, ls=":", alpha=0.5, zorder=1) + +rod, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3) +block_p = patches.FancyBboxPatch((-bw/2, -L-bh/2), bw, bh, + boxstyle="round,pad=0.02", linewidth=1.5, + edgecolor="white", facecolor="#457b9d") +ax.add_patch(block_p) +bullet_p, = ax.plot([], [], "o", color="#e63946", ms=10, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.35, lw=1.0, zorder=2) +tx, ty = [], [] + +def update(f): + th = theta[f] + bx = L * np.sin(th); by = -L * np.cos(th) + rod.set_data([0, bx], [0, by]) + block_p.set_x(bx - bw/2); block_p.set_y(by - bh/2) + bullet_p.set_data([bullet_x[f]], [bullet_y[f]]) + if embedded[f] > 0.5: + tx.append(bx); ty.append(by) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return rod, block_p, bullet_p, trail +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000/fps, blit=False) +ani.save("ballistic_pendulum.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved ballistic_pendulum.mp4") diff --git a/scipy/ballistic_pendulum/591/video.mp4 b/scipy/ballistic_pendulum/591/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..5c05aff8b46684c2e6442f841a3ce8c732c30df5 --- /dev/null +++ b/scipy/ballistic_pendulum/591/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:af99718107332c37f04fa9a11bb667e71937dc4c600b06a1a6624c951cd92887 +size 20616 diff --git a/scipy/balls_in_basin/1041/cot.json b/scipy/balls_in_basin/1041/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..84ab74bda914942b6e18a4c3191a7efff7937b1a --- /dev/null +++ b/scipy/balls_in_basin/1041/cot.json @@ -0,0 +1,164 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "3 spherical balls of distinct masses (masses=[0.6919784172097052, 0.6444754668582651, 0.8104640145202884]) and radii (radii=[0.08666224807298277, 0.07653236176346742, 0.09901623704843288]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.78 and tangential floor friction mu_floor=0.039, and collide with each other (restitution e_ball=0.90).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 3, + "total_mass_kg": 2.1469, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.26915624017421813, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.7803725860872348, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.903489410328732, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.03872767506545087, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 3, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.6919784172097052, + 0.6444754668582651, + 0.8104640145202884 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.08666224807298277, + 0.07653236176346742, + 0.09901623704843288 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.7667257527186538, + 0.8121103787453456 + ], + [ + 0.13427832794361136, + 0.7994588552874573 + ], + [ + 0.7234558019223771, + 0.7889503017261115 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + 0.5010870852778643, + 0.0 + ], + [ + 0.30390452804322865, + 0.0 + ], + [ + 0.31695645911882586, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/1041/params.json b/scipy/balls_in_basin/1041/params.json new file mode 100644 index 0000000000000000000000000000000000000000..bd8052d2259d1994a7c57afa7c8abb9c1b33a84a --- /dev/null +++ b/scipy/balls_in_basin/1041/params.json @@ -0,0 +1,51 @@ +{ + "g": 9.8, + "seed": 0.26915624017421813, + "e_wall": 0.7803725860872348, + "e_ball": 0.903489410328732, + "mu_floor": 0.03872767506545087, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 3, + "masses": [ + 0.6919784172097052, + 0.6444754668582651, + 0.8104640145202884 + ], + "radii": [ + 0.08666224807298277, + 0.07653236176346742, + 0.09901623704843288 + ], + "pos_0": [ + [ + -0.7667257527186538, + 0.8121103787453456 + ], + [ + 0.13427832794361136, + 0.7994588552874573 + ], + [ + 0.7234558019223771, + 0.7889503017261115 + ] + ], + "vel_0": [ + [ + 0.5010870852778643, + 0.0 + ], + [ + 0.30390452804322865, + 0.0 + ], + [ + 0.31695645911882586, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/1041/simulation_code.py b/scipy/balls_in_basin/1041/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..461546daff377da8d84d55a5b40d620234a55d5c --- /dev/null +++ b/scipy/balls_in_basin/1041/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 3 +masses = np.array([0.6919784172097052, 0.6444754668582651, 0.8104640145202884]) +radii = np.array([0.08666224807298277, 0.07653236176346742, 0.09901623704843288]) +e_wall = 0.7803725860872348 +e_ball = 0.903489410328732 +mu_floor = 0.03872767506545087 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.7667257527186538, 0.8121103787453456], [0.13427832794361136, 0.7994588552874573], [0.7234558019223771, 0.7889503017261115]], dtype=float) +vel_0 = np.array([[0.5010870852778643, 0.0], [0.30390452804322865, 0.0], [0.31695645911882586, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/1041/video.mp4 b/scipy/balls_in_basin/1041/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..9ef96f38256842c3b50d402d50d8d6f6d9fb2fb5 --- /dev/null +++ b/scipy/balls_in_basin/1041/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:10c6342736f805f7346e33805e76246a8753ae5e0f75431181c3f44d5328cd7d +size 12396 diff --git a/scipy/balls_in_basin/1211/cot.json b/scipy/balls_in_basin/1211/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..d4dd40216498a77fc53d58339db192de28766231 --- /dev/null +++ b/scipy/balls_in_basin/1211/cot.json @@ -0,0 +1,164 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "3 spherical balls of distinct masses (masses=[0.9708504212397695, 0.6554156983636501, 0.9673089885935566]) and radii (radii=[0.10323438183801836, 0.07916425724521317, 0.10652601885401178]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.83 and tangential floor friction mu_floor=0.075, and collide with each other (restitution e_ball=0.95).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 3, + "total_mass_kg": 2.5936, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.028164175810053593, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.8301218550191043, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.954291421179307, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.07523722287195253, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 3, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.9708504212397695, + 0.6554156983636501, + 0.9673089885935566 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.10323438183801836, + 0.07916425724521317, + 0.10652601885401178 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.593605237915011, + 1.074345922153395 + ], + [ + 0.011937691588354421, + 0.7122882152711533 + ], + [ + 0.6625420589188917, + 0.9384866798021613 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + 0.5147457605766865, + 0.0 + ], + [ + 0.43632049205359325, + 0.0 + ], + [ + -0.4733995449071806, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/1211/params.json b/scipy/balls_in_basin/1211/params.json new file mode 100644 index 0000000000000000000000000000000000000000..1db90c0d0dfe24b158c83b7e34079f06a8e25059 --- /dev/null +++ b/scipy/balls_in_basin/1211/params.json @@ -0,0 +1,51 @@ +{ + "g": 9.8, + "seed": 0.028164175810053593, + "e_wall": 0.8301218550191043, + "e_ball": 0.954291421179307, + "mu_floor": 0.07523722287195253, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 3, + "masses": [ + 0.9708504212397695, + 0.6554156983636501, + 0.9673089885935566 + ], + "radii": [ + 0.10323438183801836, + 0.07916425724521317, + 0.10652601885401178 + ], + "pos_0": [ + [ + -0.593605237915011, + 1.074345922153395 + ], + [ + 0.011937691588354421, + 0.7122882152711533 + ], + [ + 0.6625420589188917, + 0.9384866798021613 + ] + ], + "vel_0": [ + [ + 0.5147457605766865, + 0.0 + ], + [ + 0.43632049205359325, + 0.0 + ], + [ + -0.4733995449071806, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/1211/simulation_code.py b/scipy/balls_in_basin/1211/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..72dfa17c834ffb24e9a298b810786e0ea0b52fc6 --- /dev/null +++ b/scipy/balls_in_basin/1211/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 3 +masses = np.array([0.9708504212397695, 0.6554156983636501, 0.9673089885935566]) +radii = np.array([0.10323438183801836, 0.07916425724521317, 0.10652601885401178]) +e_wall = 0.8301218550191043 +e_ball = 0.954291421179307 +mu_floor = 0.07523722287195253 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.593605237915011, 1.074345922153395], [0.011937691588354421, 0.7122882152711533], [0.6625420589188917, 0.9384866798021613]], dtype=float) +vel_0 = np.array([[0.5147457605766865, 0.0], [0.43632049205359325, 0.0], [-0.4733995449071806, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/1211/video.mp4 b/scipy/balls_in_basin/1211/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..267f2cbdd30d29a6958124026a0f19eca190a3d0 --- /dev/null +++ b/scipy/balls_in_basin/1211/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:135d2f195b94c2df749e2d1ef3f5c8f4c833668e9a2fd90d9608c8b9ca1a48e9 +size 15316 diff --git a/scipy/balls_in_basin/1299/cot.json b/scipy/balls_in_basin/1299/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..7a7af945a7b31751cbdc03b8bcaafaec10881227 --- /dev/null +++ b/scipy/balls_in_basin/1299/cot.json @@ -0,0 +1,194 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "6 spherical balls of distinct masses (masses=[0.9754529143296282, 0.6921409900053497, 0.8473811869657129, 1.0063985079200255, 1.1905431218825557, 0.7034250407231285]) and radii (radii=[0.11317679920049475, 0.08763902142657512, 0.09512596414696181, 0.11200205083776288, 0.11603944577264882, 0.07742049152525643]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.69 and tangential floor friction mu_floor=0.021, and collide with each other (restitution e_ball=0.95).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 6, + "total_mass_kg": 5.4153, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.47316005162779984, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6919124371189707, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9458791708522799, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.02060369648098177, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 6, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.9754529143296282, + 0.6921409900053497, + 0.8473811869657129, + 1.0063985079200255, + 1.1905431218825557, + 0.7034250407231285 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.11317679920049475, + 0.08763902142657512, + 0.09512596414696181, + 0.11200205083776288, + 0.11603944577264882, + 0.07742049152525643 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.7831128077749796, + 1.2979076667817804 + ], + [ + -0.4836069105871744, + 0.8418468977585665 + ], + [ + -0.18330417514111946, + 1.0922785477516828 + ], + [ + 0.19171020553921364, + 1.252106764457284 + ], + [ + 0.5278935261382698, + 0.777755702293592 + ], + [ + 0.7190344042267529, + 0.9936199930915924 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + 0.4292889576824247, + 0.0 + ], + [ + -0.4841923811366328, + 0.0 + ], + [ + 0.3242625929234143, + 0.0 + ], + [ + -0.405618249744474, + 0.0 + ], + [ + -0.3657686392122602, + 0.0 + ], + [ + 0.3748193960428946, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/1299/params.json b/scipy/balls_in_basin/1299/params.json new file mode 100644 index 0000000000000000000000000000000000000000..577720a9eef7cfb44b86688c76da7ea28b1d04be --- /dev/null +++ b/scipy/balls_in_basin/1299/params.json @@ -0,0 +1,81 @@ +{ + "g": 9.8, + "seed": 0.47316005162779984, + "e_wall": 0.6919124371189707, + "e_ball": 0.9458791708522799, + "mu_floor": 0.02060369648098177, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 6, + "masses": [ + 0.9754529143296282, + 0.6921409900053497, + 0.8473811869657129, + 1.0063985079200255, + 1.1905431218825557, + 0.7034250407231285 + ], + "radii": [ + 0.11317679920049475, + 0.08763902142657512, + 0.09512596414696181, + 0.11200205083776288, + 0.11603944577264882, + 0.07742049152525643 + ], + "pos_0": [ + [ + -0.7831128077749796, + 1.2979076667817804 + ], + [ + -0.4836069105871744, + 0.8418468977585665 + ], + [ + -0.18330417514111946, + 1.0922785477516828 + ], + [ + 0.19171020553921364, + 1.252106764457284 + ], + [ + 0.5278935261382698, + 0.777755702293592 + ], + [ + 0.7190344042267529, + 0.9936199930915924 + ] + ], + "vel_0": [ + [ + 0.4292889576824247, + 0.0 + ], + [ + -0.4841923811366328, + 0.0 + ], + [ + 0.3242625929234143, + 0.0 + ], + [ + -0.405618249744474, + 0.0 + ], + [ + -0.3657686392122602, + 0.0 + ], + [ + 0.3748193960428946, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/1299/simulation_code.py b/scipy/balls_in_basin/1299/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..28fd64b65b7f0a97090ca142817765e5b3b8ad97 --- /dev/null +++ b/scipy/balls_in_basin/1299/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 6 +masses = np.array([0.9754529143296282, 0.6921409900053497, 0.8473811869657129, 1.0063985079200255, 1.1905431218825557, 0.7034250407231285]) +radii = np.array([0.11317679920049475, 0.08763902142657512, 0.09512596414696181, 0.11200205083776288, 0.11603944577264882, 0.07742049152525643]) +e_wall = 0.6919124371189707 +e_ball = 0.9458791708522799 +mu_floor = 0.02060369648098177 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.7831128077749796, 1.2979076667817804], [-0.4836069105871744, 0.8418468977585665], [-0.18330417514111946, 1.0922785477516828], [0.19171020553921364, 1.252106764457284], [0.5278935261382698, 0.777755702293592], [0.7190344042267529, 0.9936199930915924]], dtype=float) +vel_0 = np.array([[0.4292889576824247, 0.0], [-0.4841923811366328, 0.0], [0.3242625929234143, 0.0], [-0.405618249744474, 0.0], [-0.3657686392122602, 0.0], [0.3748193960428946, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/1299/video.mp4 b/scipy/balls_in_basin/1299/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..2c41db9c700e4f66971aed84893ff93bf5ec1033 --- /dev/null +++ b/scipy/balls_in_basin/1299/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:27b8ad1a445b0b78189ac14c85bfeb10c04aa48a221c252bcebe491cc88bcb96 +size 20342 diff --git a/scipy/balls_in_basin/1405/cot.json b/scipy/balls_in_basin/1405/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..8ec19c578d3ea099a5cea2dee8f3eb10b1bec026 --- /dev/null +++ b/scipy/balls_in_basin/1405/cot.json @@ -0,0 +1,174 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "4 spherical balls of distinct masses (masses=[1.0228207283724322, 1.1093301798148734, 1.0200868084485577, 0.7857005463032274]) and radii (radii=[0.10823185056770632, 0.111321711949178, 0.11607236526464831, 0.08527362051731342]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.81 and tangential floor friction mu_floor=0.042, and collide with each other (restitution e_ball=0.85).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 4, + "total_mass_kg": 3.9379, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.3509903047535178, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.8078939239249079, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.8544575597411281, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.04202397348654384, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 4, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.0228207283724322, + 1.1093301798148734, + 1.0200868084485577, + 0.7857005463032274 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.10823185056770632, + 0.111321711949178, + 0.11607236526464831, + 0.08527362051731342 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.6101702364357178, + 0.9830629551035615 + ], + [ + -0.3118620469880709, + 0.9403813411132385 + ], + [ + 0.29612523647060196, + 0.7250870864457649 + ], + [ + 0.6465955322296114, + 1.1536057114176235 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + 0.37941744491373414, + 0.0 + ], + [ + -0.49166583376597234, + 0.0 + ], + [ + 0.3077334792349481, + 0.0 + ], + [ + 0.4055064209775217, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/1405/params.json b/scipy/balls_in_basin/1405/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5dd9db6097c6d15f820921cfb9f7cdc3f8ea0321 --- /dev/null +++ b/scipy/balls_in_basin/1405/params.json @@ -0,0 +1,61 @@ +{ + "g": 9.8, + "seed": 0.3509903047535178, + "e_wall": 0.8078939239249079, + "e_ball": 0.8544575597411281, + "mu_floor": 0.04202397348654384, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 4, + "masses": [ + 1.0228207283724322, + 1.1093301798148734, + 1.0200868084485577, + 0.7857005463032274 + ], + "radii": [ + 0.10823185056770632, + 0.111321711949178, + 0.11607236526464831, + 0.08527362051731342 + ], + "pos_0": [ + [ + -0.6101702364357178, + 0.9830629551035615 + ], + [ + -0.3118620469880709, + 0.9403813411132385 + ], + [ + 0.29612523647060196, + 0.7250870864457649 + ], + [ + 0.6465955322296114, + 1.1536057114176235 + ] + ], + "vel_0": [ + [ + 0.37941744491373414, + 0.0 + ], + [ + -0.49166583376597234, + 0.0 + ], + [ + 0.3077334792349481, + 0.0 + ], + [ + 0.4055064209775217, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/1405/simulation_code.py b/scipy/balls_in_basin/1405/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..b9f37fcef4920dd61659cef823229364e0cd22ba --- /dev/null +++ b/scipy/balls_in_basin/1405/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 4 +masses = np.array([1.0228207283724322, 1.1093301798148734, 1.0200868084485577, 0.7857005463032274]) +radii = np.array([0.10823185056770632, 0.111321711949178, 0.11607236526464831, 0.08527362051731342]) +e_wall = 0.8078939239249079 +e_ball = 0.8544575597411281 +mu_floor = 0.04202397348654384 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.6101702364357178, 0.9830629551035615], [-0.3118620469880709, 0.9403813411132385], [0.29612523647060196, 0.7250870864457649], [0.6465955322296114, 1.1536057114176235]], dtype=float) +vel_0 = np.array([[0.37941744491373414, 0.0], [-0.49166583376597234, 0.0], [0.3077334792349481, 0.0], [0.4055064209775217, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/1405/video.mp4 b/scipy/balls_in_basin/1405/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..679e6cbeff6fce69026c3f689b1125eed30164a6 --- /dev/null +++ b/scipy/balls_in_basin/1405/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c27c594904c41494932d7da20230479eed8341bb4aba35e5d7d5bb750c7a1370 +size 18420 diff --git a/scipy/balls_in_basin/157/cot.json b/scipy/balls_in_basin/157/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..c996af63d6ad7219e0a7b2375c6bb31c68efaf8e --- /dev/null +++ b/scipy/balls_in_basin/157/cot.json @@ -0,0 +1,164 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "3 spherical balls of distinct masses (masses=[0.6069095091143077, 0.6531653572215605, 0.8549665495656698]) and radii (radii=[0.07175594931005629, 0.07733862783618746, 0.09700703997989768]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.79 and tangential floor friction mu_floor=0.033, and collide with each other (restitution e_ball=0.92).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 3, + "total_mass_kg": 2.115, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.21657261199468514, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.793687093150309, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9204540633744427, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.03339724510470834, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 3, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.6069095091143077, + 0.6531653572215605, + 0.8549665495656698 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.07175594931005629, + 0.07733862783618746, + 0.09700703997989768 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.49111731894932054, + 0.8401734659811955 + ], + [ + -0.04067856521757529, + 1.0265008684232066 + ], + [ + 0.605125269864519, + 0.7939447820538411 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + 0.5008206396895427, + 0.0 + ], + [ + -0.267559529673179, + 0.0 + ], + [ + -0.435425317075353, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/157/params.json b/scipy/balls_in_basin/157/params.json new file mode 100644 index 0000000000000000000000000000000000000000..7a65af54f5b8989b5ea93b9385093342d5906772 --- /dev/null +++ b/scipy/balls_in_basin/157/params.json @@ -0,0 +1,51 @@ +{ + "g": 9.8, + "seed": 0.21657261199468514, + "e_wall": 0.793687093150309, + "e_ball": 0.9204540633744427, + "mu_floor": 0.03339724510470834, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 3, + "masses": [ + 0.6069095091143077, + 0.6531653572215605, + 0.8549665495656698 + ], + "radii": [ + 0.07175594931005629, + 0.07733862783618746, + 0.09700703997989768 + ], + "pos_0": [ + [ + -0.49111731894932054, + 0.8401734659811955 + ], + [ + -0.04067856521757529, + 1.0265008684232066 + ], + [ + 0.605125269864519, + 0.7939447820538411 + ] + ], + "vel_0": [ + [ + 0.5008206396895427, + 0.0 + ], + [ + -0.267559529673179, + 0.0 + ], + [ + -0.435425317075353, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/157/simulation_code.py b/scipy/balls_in_basin/157/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e337c4a5a8b631a164e5b1803974391e72ca4966 --- /dev/null +++ b/scipy/balls_in_basin/157/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 3 +masses = np.array([0.6069095091143077, 0.6531653572215605, 0.8549665495656698]) +radii = np.array([0.07175594931005629, 0.07733862783618746, 0.09700703997989768]) +e_wall = 0.793687093150309 +e_ball = 0.9204540633744427 +mu_floor = 0.03339724510470834 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.49111731894932054, 0.8401734659811955], [-0.04067856521757529, 1.0265008684232066], [0.605125269864519, 0.7939447820538411]], dtype=float) +vel_0 = np.array([[0.5008206396895427, 0.0], [-0.267559529673179, 0.0], [-0.435425317075353, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/157/video.mp4 b/scipy/balls_in_basin/157/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3da6501a30c69f2607fa432659897a7705175217 --- /dev/null +++ b/scipy/balls_in_basin/157/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2292f102f83453a97a0aab0d0137ee0b4af85bae6449d1f403e24ede3db12251 +size 13217 diff --git a/scipy/balls_in_basin/1588/cot.json b/scipy/balls_in_basin/1588/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..9b20a8fd4fe7964d8efa89d838062b0465d7edff --- /dev/null +++ b/scipy/balls_in_basin/1588/cot.json @@ -0,0 +1,164 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "3 spherical balls of distinct masses (masses=[0.5876095293714894, 1.0998136583900924, 1.4281763101747889]) and radii (radii=[0.07424231040692585, 0.1143849866646158, 0.127923073289063]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.68 and tangential floor friction mu_floor=0.053, and collide with each other (restitution e_ball=0.95).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 3, + "total_mass_kg": 3.1156, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.9317815885090982, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6830490348033018, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9454526945359343, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.053204841193302696, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 3, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.5876095293714894, + 1.0998136583900924, + 1.4281763101747889 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.07424231040692585, + 0.1143849866646158, + 0.127923073289063 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.7429483673417565, + 1.2885324567029643 + ], + [ + 0.06153848220623781, + 1.2977701722627484 + ], + [ + 0.5278213040789127, + 1.2395886680506472 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + -0.5320976229950507, + 0.0 + ], + [ + 0.38060533594459045, + 0.0 + ], + [ + -0.4721825513861491, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/1588/params.json b/scipy/balls_in_basin/1588/params.json new file mode 100644 index 0000000000000000000000000000000000000000..bef8acec71c5f17ad2c45e94d9d4d6d090aae9ac --- /dev/null +++ b/scipy/balls_in_basin/1588/params.json @@ -0,0 +1,51 @@ +{ + "g": 9.8, + "seed": 0.9317815885090982, + "e_wall": 0.6830490348033018, + "e_ball": 0.9454526945359343, + "mu_floor": 0.053204841193302696, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 3, + "masses": [ + 0.5876095293714894, + 1.0998136583900924, + 1.4281763101747889 + ], + "radii": [ + 0.07424231040692585, + 0.1143849866646158, + 0.127923073289063 + ], + "pos_0": [ + [ + -0.7429483673417565, + 1.2885324567029643 + ], + [ + 0.06153848220623781, + 1.2977701722627484 + ], + [ + 0.5278213040789127, + 1.2395886680506472 + ] + ], + "vel_0": [ + [ + -0.5320976229950507, + 0.0 + ], + [ + 0.38060533594459045, + 0.0 + ], + [ + -0.4721825513861491, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/1588/simulation_code.py b/scipy/balls_in_basin/1588/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..612822224bef99197691a942ea7ccdd2c5f7f179 --- /dev/null +++ b/scipy/balls_in_basin/1588/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 3 +masses = np.array([0.5876095293714894, 1.0998136583900924, 1.4281763101747889]) +radii = np.array([0.07424231040692585, 0.1143849866646158, 0.127923073289063]) +e_wall = 0.6830490348033018 +e_ball = 0.9454526945359343 +mu_floor = 0.053204841193302696 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.7429483673417565, 1.2885324567029643], [0.06153848220623781, 1.2977701722627484], [0.5278213040789127, 1.2395886680506472]], dtype=float) +vel_0 = np.array([[-0.5320976229950507, 0.0], [0.38060533594459045, 0.0], [-0.4721825513861491, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/1588/video.mp4 b/scipy/balls_in_basin/1588/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..38d0f0fba2d4012842773585a54e5321c02f628d --- /dev/null +++ b/scipy/balls_in_basin/1588/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9870d9178851e03f6f56a9edbabc51efe59ffc9c401bb2c9a516f4d7725420c1 +size 11257 diff --git a/scipy/balls_in_basin/1598/cot.json b/scipy/balls_in_basin/1598/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..5dd349cb82d3882b05edc30796127dddeaa7787c --- /dev/null +++ b/scipy/balls_in_basin/1598/cot.json @@ -0,0 +1,174 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "4 spherical balls of distinct masses (masses=[0.5946842767711928, 1.2594452445705495, 1.3056736228386907, 0.6062750510716366]) and radii (radii=[0.07493010824487993, 0.12055502160734666, 0.12247671896636234, 0.07654370424541825]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.85 and tangential floor friction mu_floor=0.032, and collide with each other (restitution e_ball=0.91).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 4, + "total_mass_kg": 3.7661, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.7761852447033024, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.849189309327254, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9071921117482438, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.03223474345399455, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 4, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.5946842767711928, + 1.2594452445705495, + 1.3056736228386907, + 0.6062750510716366 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.07493010824487993, + 0.12055502160734666, + 0.12247671896636234, + 0.07654370424541825 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.8015703446312713, + 1.2484945613309437 + ], + [ + -0.12561062371073156, + 1.146655583405333 + ], + [ + 0.24848552564607468, + 1.1642411601747034 + ], + [ + 0.6342272745209242, + 1.0664234556494905 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + -0.4949434551630856, + 0.0 + ], + [ + 0.49347517545463637, + 0.0 + ], + [ + 0.3771177059790133, + 0.0 + ], + [ + 0.34940701026257737, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/1598/params.json b/scipy/balls_in_basin/1598/params.json new file mode 100644 index 0000000000000000000000000000000000000000..2271edaeeab194c5aa22a290e336faa9731bd2ce --- /dev/null +++ b/scipy/balls_in_basin/1598/params.json @@ -0,0 +1,61 @@ +{ + "g": 9.8, + "seed": 0.7761852447033024, + "e_wall": 0.849189309327254, + "e_ball": 0.9071921117482438, + "mu_floor": 0.03223474345399455, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 4, + "masses": [ + 0.5946842767711928, + 1.2594452445705495, + 1.3056736228386907, + 0.6062750510716366 + ], + "radii": [ + 0.07493010824487993, + 0.12055502160734666, + 0.12247671896636234, + 0.07654370424541825 + ], + "pos_0": [ + [ + -0.8015703446312713, + 1.2484945613309437 + ], + [ + -0.12561062371073156, + 1.146655583405333 + ], + [ + 0.24848552564607468, + 1.1642411601747034 + ], + [ + 0.6342272745209242, + 1.0664234556494905 + ] + ], + "vel_0": [ + [ + -0.4949434551630856, + 0.0 + ], + [ + 0.49347517545463637, + 0.0 + ], + [ + 0.3771177059790133, + 0.0 + ], + [ + 0.34940701026257737, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/1598/simulation_code.py b/scipy/balls_in_basin/1598/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..b9ce2bc86ce29dc06b325e7ff5fef97d27243cda --- /dev/null +++ b/scipy/balls_in_basin/1598/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 4 +masses = np.array([0.5946842767711928, 1.2594452445705495, 1.3056736228386907, 0.6062750510716366]) +radii = np.array([0.07493010824487993, 0.12055502160734666, 0.12247671896636234, 0.07654370424541825]) +e_wall = 0.849189309327254 +e_ball = 0.9071921117482438 +mu_floor = 0.03223474345399455 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.8015703446312713, 1.2484945613309437], [-0.12561062371073156, 1.146655583405333], [0.24848552564607468, 1.1642411601747034], [0.6342272745209242, 1.0664234556494905]], dtype=float) +vel_0 = np.array([[-0.4949434551630856, 0.0], [0.49347517545463637, 0.0], [0.3771177059790133, 0.0], [0.34940701026257737, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/1598/video.mp4 b/scipy/balls_in_basin/1598/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..525aeb8a41b2434353eab7afbf1627a4750c476d --- /dev/null +++ b/scipy/balls_in_basin/1598/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d619bafcb7fda3768703704659217dccc282f842090a349a38c9bb65333930ca +size 21987 diff --git a/scipy/balls_in_basin/247/cot.json b/scipy/balls_in_basin/247/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..50977be4c2ee1840d2790a851a230c247ca4349a --- /dev/null +++ b/scipy/balls_in_basin/247/cot.json @@ -0,0 +1,164 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "3 spherical balls of distinct masses (masses=[0.7257356763985462, 0.6929299115317802, 1.2705408922287347]) and radii (radii=[0.07877235342634177, 0.08310664492666145, 0.12937989898045263]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.71 and tangential floor friction mu_floor=0.061, and collide with each other (restitution e_ball=0.91).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 3, + "total_mass_kg": 2.6892, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.8712313358226016, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.7108963194918455, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9136970382043114, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.06143456699302843, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 3, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.7257356763985462, + 0.6929299115317802, + 1.2705408922287347 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.07877235342634177, + 0.08310664492666145, + 0.12937989898045263 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.6740519112003619, + 0.8247534894424539 + ], + [ + -0.07112127411820383, + 1.0591846340335418 + ], + [ + 0.6690150410172941, + 1.0466435975726835 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + -0.30850587349947045, + 0.0 + ], + [ + -0.2960268427998633, + 0.0 + ], + [ + -0.31008232315711987, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/247/params.json b/scipy/balls_in_basin/247/params.json new file mode 100644 index 0000000000000000000000000000000000000000..91611cc75f47828368567af30b56f8ae31cba527 --- /dev/null +++ b/scipy/balls_in_basin/247/params.json @@ -0,0 +1,51 @@ +{ + "g": 9.8, + "seed": 0.8712313358226016, + "e_wall": 0.7108963194918455, + "e_ball": 0.9136970382043114, + "mu_floor": 0.06143456699302843, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 3, + "masses": [ + 0.7257356763985462, + 0.6929299115317802, + 1.2705408922287347 + ], + "radii": [ + 0.07877235342634177, + 0.08310664492666145, + 0.12937989898045263 + ], + "pos_0": [ + [ + -0.6740519112003619, + 0.8247534894424539 + ], + [ + -0.07112127411820383, + 1.0591846340335418 + ], + [ + 0.6690150410172941, + 1.0466435975726835 + ] + ], + "vel_0": [ + [ + -0.30850587349947045, + 0.0 + ], + [ + -0.2960268427998633, + 0.0 + ], + [ + -0.31008232315711987, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/247/simulation_code.py b/scipy/balls_in_basin/247/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..30102bef08c44e86fe2e5dcd1adcff1c4664e2d0 --- /dev/null +++ b/scipy/balls_in_basin/247/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 3 +masses = np.array([0.7257356763985462, 0.6929299115317802, 1.2705408922287347]) +radii = np.array([0.07877235342634177, 0.08310664492666145, 0.12937989898045263]) +e_wall = 0.7108963194918455 +e_ball = 0.9136970382043114 +mu_floor = 0.06143456699302843 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.6740519112003619, 0.8247534894424539], [-0.07112127411820383, 1.0591846340335418], [0.6690150410172941, 1.0466435975726835]], dtype=float) +vel_0 = np.array([[-0.30850587349947045, 0.0], [-0.2960268427998633, 0.0], [-0.31008232315711987, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/247/video.mp4 b/scipy/balls_in_basin/247/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..51c4acbd694fc931511cf8a6c00f1219826716d6 --- /dev/null +++ b/scipy/balls_in_basin/247/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:88b68e562c955fadb5a510e036417c4a46ed807cf33e783d29bde5d68e44efeb +size 11072 diff --git a/scipy/balls_in_basin/301/cot.json b/scipy/balls_in_basin/301/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..d952925e271deb2196905dfffb5f567b82b06ad3 --- /dev/null +++ b/scipy/balls_in_basin/301/cot.json @@ -0,0 +1,194 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "6 spherical balls of distinct masses (masses=[0.7833789365507623, 1.000635778754609, 1.0712833592237696, 1.1591703041560046, 1.1106686957951042, 1.1853443271727313]) and radii (radii=[0.08515764226778857, 0.10264026546958063, 0.11786992648656536, 0.11907243569616313, 0.11952685185171787, 0.12118030807722663]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.79 and tangential floor friction mu_floor=0.063, and collide with each other (restitution e_ball=0.88).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 6, + "total_mass_kg": 6.3105, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.8764438160952348, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.7911587567484099, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.8760415461591444, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.06252240209517007, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 6, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.7833789365507623, + 1.000635778754609, + 1.0712833592237696, + 1.1591703041560046, + 1.1106686957951042, + 1.1853443271727313 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.08515764226778857, + 0.10264026546958063, + 0.11786992648656536, + 0.11907243569616313, + 0.11952685185171787, + 0.12118030807722663 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.7190468532779541, + 0.8208427116707723 + ], + [ + -0.4003492273286944, + 1.1180586341504541 + ], + [ + -0.17047477138007391, + 0.8883500046608119 + ], + [ + 0.17632787938995542, + 0.8899460044021025 + ], + [ + 0.40092995832352796, + 0.9132420905134455 + ], + [ + 0.7411171432501683, + 1.0383066226188826 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + 0.41766923314160187, + 0.0 + ], + [ + 0.2497164118669524, + 0.0 + ], + [ + 0.24630518356999104, + 0.0 + ], + [ + -0.49507642002457913, + 0.0 + ], + [ + -0.4701210185823563, + 0.0 + ], + [ + -0.20880209426531582, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/301/params.json b/scipy/balls_in_basin/301/params.json new file mode 100644 index 0000000000000000000000000000000000000000..fa32caf5ee950cfe973cee636d9f04ffd27cbbfe --- /dev/null +++ b/scipy/balls_in_basin/301/params.json @@ -0,0 +1,81 @@ +{ + "g": 9.8, + "seed": 0.8764438160952348, + "e_wall": 0.7911587567484099, + "e_ball": 0.8760415461591444, + "mu_floor": 0.06252240209517007, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 6, + "masses": [ + 0.7833789365507623, + 1.000635778754609, + 1.0712833592237696, + 1.1591703041560046, + 1.1106686957951042, + 1.1853443271727313 + ], + "radii": [ + 0.08515764226778857, + 0.10264026546958063, + 0.11786992648656536, + 0.11907243569616313, + 0.11952685185171787, + 0.12118030807722663 + ], + "pos_0": [ + [ + -0.7190468532779541, + 0.8208427116707723 + ], + [ + -0.4003492273286944, + 1.1180586341504541 + ], + [ + -0.17047477138007391, + 0.8883500046608119 + ], + [ + 0.17632787938995542, + 0.8899460044021025 + ], + [ + 0.40092995832352796, + 0.9132420905134455 + ], + [ + 0.7411171432501683, + 1.0383066226188826 + ] + ], + "vel_0": [ + [ + 0.41766923314160187, + 0.0 + ], + [ + 0.2497164118669524, + 0.0 + ], + [ + 0.24630518356999104, + 0.0 + ], + [ + -0.49507642002457913, + 0.0 + ], + [ + -0.4701210185823563, + 0.0 + ], + [ + -0.20880209426531582, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/301/simulation_code.py b/scipy/balls_in_basin/301/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..73a2c53ecdbdce4733bc2f70ad6c928e6bf6d000 --- /dev/null +++ b/scipy/balls_in_basin/301/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 6 +masses = np.array([0.7833789365507623, 1.000635778754609, 1.0712833592237696, 1.1591703041560046, 1.1106686957951042, 1.1853443271727313]) +radii = np.array([0.08515764226778857, 0.10264026546958063, 0.11786992648656536, 0.11907243569616313, 0.11952685185171787, 0.12118030807722663]) +e_wall = 0.7911587567484099 +e_ball = 0.8760415461591444 +mu_floor = 0.06252240209517007 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.7190468532779541, 0.8208427116707723], [-0.4003492273286944, 1.1180586341504541], [-0.17047477138007391, 0.8883500046608119], [0.17632787938995542, 0.8899460044021025], [0.40092995832352796, 0.9132420905134455], [0.7411171432501683, 1.0383066226188826]], dtype=float) +vel_0 = np.array([[0.41766923314160187, 0.0], [0.2497164118669524, 0.0], [0.24630518356999104, 0.0], [-0.49507642002457913, 0.0], [-0.4701210185823563, 0.0], [-0.20880209426531582, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/301/video.mp4 b/scipy/balls_in_basin/301/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..2d28205d6653e0f45f52d95cc221716c3cd4b00b --- /dev/null +++ b/scipy/balls_in_basin/301/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4804b7df36bc4c94d2d8745cc01a1ccca0865a4e650b7707e7a3228b7ded6496 +size 21701 diff --git a/scipy/balls_in_basin/327/cot.json b/scipy/balls_in_basin/327/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..e0466964adafe51af8f20f424099f71d00a62865 --- /dev/null +++ b/scipy/balls_in_basin/327/cot.json @@ -0,0 +1,184 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "5 spherical balls of distinct masses (masses=[0.6857217603386685, 1.03828701062368, 1.3857248141499428, 0.671069333495932, 0.6089111539649662]) and radii (radii=[0.07837141288286985, 0.11020863596134896, 0.12860485043921233, 0.07824353941714604, 0.07691095393767003]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.68 and tangential floor friction mu_floor=0.041, and collide with each other (restitution e_ball=0.88).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 5, + "total_mass_kg": 4.3897, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.6024186373625485, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6824163374306864, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.8842113075272143, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.04088520104044856, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 5, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.6857217603386685, + 1.03828701062368, + 1.3857248141499428, + 0.671069333495932, + 0.6089111539649662 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.07837141288286985, + 0.11020863596134896, + 0.12860485043921233, + 0.07824353941714604, + 0.07691095393767003 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.7002749670184936, + 0.7468290614273486 + ], + [ + -0.4238529326735948, + 0.7035224157033083 + ], + [ + -0.01392638444476084, + 1.1685780096379441 + ], + [ + 0.3118360306002542, + 1.163801859910879 + ], + [ + 0.8111092648174725, + 0.9741182439674714 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + 0.2907284150301107, + 0.0 + ], + [ + 0.24440059763998392, + 0.0 + ], + [ + 0.5354307642065959, + 0.0 + ], + [ + 0.46685283461051036, + 0.0 + ], + [ + -0.3764569562768346, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/327/params.json b/scipy/balls_in_basin/327/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b3b35265f098727d4b6fcc7c655a000f4fb06144 --- /dev/null +++ b/scipy/balls_in_basin/327/params.json @@ -0,0 +1,71 @@ +{ + "g": 9.8, + "seed": 0.6024186373625485, + "e_wall": 0.6824163374306864, + "e_ball": 0.8842113075272143, + "mu_floor": 0.04088520104044856, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 5, + "masses": [ + 0.6857217603386685, + 1.03828701062368, + 1.3857248141499428, + 0.671069333495932, + 0.6089111539649662 + ], + "radii": [ + 0.07837141288286985, + 0.11020863596134896, + 0.12860485043921233, + 0.07824353941714604, + 0.07691095393767003 + ], + "pos_0": [ + [ + -0.7002749670184936, + 0.7468290614273486 + ], + [ + -0.4238529326735948, + 0.7035224157033083 + ], + [ + -0.01392638444476084, + 1.1685780096379441 + ], + [ + 0.3118360306002542, + 1.163801859910879 + ], + [ + 0.8111092648174725, + 0.9741182439674714 + ] + ], + "vel_0": [ + [ + 0.2907284150301107, + 0.0 + ], + [ + 0.24440059763998392, + 0.0 + ], + [ + 0.5354307642065959, + 0.0 + ], + [ + 0.46685283461051036, + 0.0 + ], + [ + -0.3764569562768346, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/327/simulation_code.py b/scipy/balls_in_basin/327/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..19cc37568859d89c415733911ffbf8dcc2674299 --- /dev/null +++ b/scipy/balls_in_basin/327/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 5 +masses = np.array([0.6857217603386685, 1.03828701062368, 1.3857248141499428, 0.671069333495932, 0.6089111539649662]) +radii = np.array([0.07837141288286985, 0.11020863596134896, 0.12860485043921233, 0.07824353941714604, 0.07691095393767003]) +e_wall = 0.6824163374306864 +e_ball = 0.8842113075272143 +mu_floor = 0.04088520104044856 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.7002749670184936, 0.7468290614273486], [-0.4238529326735948, 0.7035224157033083], [-0.01392638444476084, 1.1685780096379441], [0.3118360306002542, 1.163801859910879], [0.8111092648174725, 0.9741182439674714]], dtype=float) +vel_0 = np.array([[0.2907284150301107, 0.0], [0.24440059763998392, 0.0], [0.5354307642065959, 0.0], [0.46685283461051036, 0.0], [-0.3764569562768346, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/327/video.mp4 b/scipy/balls_in_basin/327/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..94924c8d3e863bdda2dbe21498976e4d54db7712 --- /dev/null +++ b/scipy/balls_in_basin/327/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:029f2ba68499dc75ef2432cea46009e912a86535ccac94b7882f738a9c45b29c +size 14733 diff --git a/scipy/balls_in_basin/372/cot.json b/scipy/balls_in_basin/372/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..67ef6793c3fc28185c65f70440e43854f708db36 --- /dev/null +++ b/scipy/balls_in_basin/372/cot.json @@ -0,0 +1,214 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "8 spherical balls of distinct masses (masses=[0.7924636610722057, 0.9582956304659378, 0.6541425405057868, 0.6161181842562208, 0.9249944854952514, 0.8185160899893806, 0.7026486464845463, 1.2001883466668213]) and radii (radii=[0.09076874949447691, 0.10692321422235707, 0.07494206537001691, 0.07872289197753923, 0.09711119411772245, 0.09847811296345413, 0.0830653714673444, 0.12256372332045895]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.68 and tangential floor friction mu_floor=0.054, and collide with each other (restitution e_ball=0.91).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 8, + "total_mass_kg": 6.6674, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.4839836796807686, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6797823280384788, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9064561628849509, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.05385898160304395, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 8, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.7924636610722057, + 0.9582956304659378, + 0.6541425405057868, + 0.6161181842562208, + 0.9249944854952514, + 0.8185160899893806, + 0.7026486464845463, + 1.2001883466668213 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.09076874949447691, + 0.10692321422235707, + 0.07494206537001691, + 0.07872289197753923, + 0.09711119411772245, + 0.09847811296345413, + 0.0830653714673444, + 0.12256372332045895 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.8115997019435144, + 0.8727968400800977 + ], + [ + -0.6373395409572724, + 0.7534926338357651 + ], + [ + -0.32241973374990013, + 0.9773669005460033 + ], + [ + -0.09307076131080483, + 1.0863186161681433 + ], + [ + 0.06002328119887917, + 1.2496031756478152 + ], + [ + 0.3486497146497808, + 0.8950636441521512 + ], + [ + 0.5343727717747596, + 1.2777364690063455 + ], + [ + 0.7548169594291843, + 1.2614770976376017 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + 0.4987507140000378, + 0.0 + ], + [ + 0.2153604128646442, + 0.0 + ], + [ + 0.4799366728661126, + 0.0 + ], + [ + 0.514973024550816, + 0.0 + ], + [ + 0.3935346462638514, + 0.0 + ], + [ + -0.5315627395736494, + 0.0 + ], + [ + -0.26434024816989327, + 0.0 + ], + [ + -0.4191055849895219, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/372/params.json b/scipy/balls_in_basin/372/params.json new file mode 100644 index 0000000000000000000000000000000000000000..d7fb8a8a9d3918ada2c382a7592fc616dd6c0c25 --- /dev/null +++ b/scipy/balls_in_basin/372/params.json @@ -0,0 +1,101 @@ +{ + "g": 9.8, + "seed": 0.4839836796807686, + "e_wall": 0.6797823280384788, + "e_ball": 0.9064561628849509, + "mu_floor": 0.05385898160304395, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 8, + "masses": [ + 0.7924636610722057, + 0.9582956304659378, + 0.6541425405057868, + 0.6161181842562208, + 0.9249944854952514, + 0.8185160899893806, + 0.7026486464845463, + 1.2001883466668213 + ], + "radii": [ + 0.09076874949447691, + 0.10692321422235707, + 0.07494206537001691, + 0.07872289197753923, + 0.09711119411772245, + 0.09847811296345413, + 0.0830653714673444, + 0.12256372332045895 + ], + "pos_0": [ + [ + -0.8115997019435144, + 0.8727968400800977 + ], + [ + -0.6373395409572724, + 0.7534926338357651 + ], + [ + -0.32241973374990013, + 0.9773669005460033 + ], + [ + -0.09307076131080483, + 1.0863186161681433 + ], + [ + 0.06002328119887917, + 1.2496031756478152 + ], + [ + 0.3486497146497808, + 0.8950636441521512 + ], + [ + 0.5343727717747596, + 1.2777364690063455 + ], + [ + 0.7548169594291843, + 1.2614770976376017 + ] + ], + "vel_0": [ + [ + 0.4987507140000378, + 0.0 + ], + [ + 0.2153604128646442, + 0.0 + ], + [ + 0.4799366728661126, + 0.0 + ], + [ + 0.514973024550816, + 0.0 + ], + [ + 0.3935346462638514, + 0.0 + ], + [ + -0.5315627395736494, + 0.0 + ], + [ + -0.26434024816989327, + 0.0 + ], + [ + -0.4191055849895219, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/372/simulation_code.py b/scipy/balls_in_basin/372/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..5d640e0532e3bd90e520f8151ef21a95a45acf33 --- /dev/null +++ b/scipy/balls_in_basin/372/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 8 +masses = np.array([0.7924636610722057, 0.9582956304659378, 0.6541425405057868, 0.6161181842562208, 0.9249944854952514, 0.8185160899893806, 0.7026486464845463, 1.2001883466668213]) +radii = np.array([0.09076874949447691, 0.10692321422235707, 0.07494206537001691, 0.07872289197753923, 0.09711119411772245, 0.09847811296345413, 0.0830653714673444, 0.12256372332045895]) +e_wall = 0.6797823280384788 +e_ball = 0.9064561628849509 +mu_floor = 0.05385898160304395 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.8115997019435144, 0.8727968400800977], [-0.6373395409572724, 0.7534926338357651], [-0.32241973374990013, 0.9773669005460033], [-0.09307076131080483, 1.0863186161681433], [0.06002328119887917, 1.2496031756478152], [0.3486497146497808, 0.8950636441521512], [0.5343727717747596, 1.2777364690063455], [0.7548169594291843, 1.2614770976376017]], dtype=float) +vel_0 = np.array([[0.4987507140000378, 0.0], [0.2153604128646442, 0.0], [0.4799366728661126, 0.0], [0.514973024550816, 0.0], [0.3935346462638514, 0.0], [-0.5315627395736494, 0.0], [-0.26434024816989327, 0.0], [-0.4191055849895219, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/372/video.mp4 b/scipy/balls_in_basin/372/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..12208fcf0eed11bef429ecb4247c9b4a8a0e76a9 --- /dev/null +++ b/scipy/balls_in_basin/372/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e208c3bd4a0b1db086b9fd0111fe5614020c00d51c8ae4de5607e0313fd0b12b +size 22917 diff --git a/scipy/balls_in_basin/399/cot.json b/scipy/balls_in_basin/399/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..3d03acb239ccba395374e0f922aa44fee3dbc097 --- /dev/null +++ b/scipy/balls_in_basin/399/cot.json @@ -0,0 +1,214 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "8 spherical balls of distinct masses (masses=[0.9889154112091929, 0.9515505511331617, 0.6577998679780545, 0.7520381921862338, 1.2455604106486533, 0.7424591156074027, 0.9919912330068748, 1.366295368691577]) and radii (radii=[0.10460190507172525, 0.11047163450910968, 0.07949284221517193, 0.08804571249384024, 0.12289833318737291, 0.08959803638725514, 0.1011510870155174, 0.12789780735305226]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.84 and tangential floor friction mu_floor=0.074, and collide with each other (restitution e_ball=0.88).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 8, + "total_mass_kg": 7.6966, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.21967060072383504, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.8374060334182114, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.8798037909265465, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.07444507082694406, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 8, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.9889154112091929, + 0.9515505511331617, + 0.6577998679780545, + 0.7520381921862338, + 1.2455604106486533, + 0.7424591156074027, + 0.9919912330068748, + 1.366295368691577 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.10460190507172525, + 0.11047163450910968, + 0.07949284221517193, + 0.08804571249384024, + 0.12289833318737291, + 0.08959803638725514, + 0.1011510870155174, + 0.12789780735305226 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.7544663087797417, + 0.7582124153667615 + ], + [ + -0.6248438019259824, + 0.7735486517325506 + ], + [ + -0.3100653230956832, + 1.2540261511849118 + ], + [ + -0.16385768754837496, + 0.816653774207407 + ], + [ + 0.08596563516784864, + 0.8694988371696188 + ], + [ + 0.36909572875200997, + 0.8741576780730848 + ], + [ + 0.5601116873708519, + 1.168876036417066 + ], + [ + 0.7504381941912207, + 1.2867526401203684 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + -0.46963302657519507, + 0.0 + ], + [ + -0.46119805817380144, + 0.0 + ], + [ + -0.46768905711501274, + 0.0 + ], + [ + 0.38219520089544023, + 0.0 + ], + [ + -0.24097964115103593, + 0.0 + ], + [ + 0.3457403377803504, + 0.0 + ], + [ + -0.3678492602459004, + 0.0 + ], + [ + -0.3432990834635469, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/399/params.json b/scipy/balls_in_basin/399/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5ac381a4cfedf1cdbbd07fc09b4b9c2f806e28d3 --- /dev/null +++ b/scipy/balls_in_basin/399/params.json @@ -0,0 +1,101 @@ +{ + "g": 9.8, + "seed": 0.21967060072383504, + "e_wall": 0.8374060334182114, + "e_ball": 0.8798037909265465, + "mu_floor": 0.07444507082694406, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 8, + "masses": [ + 0.9889154112091929, + 0.9515505511331617, + 0.6577998679780545, + 0.7520381921862338, + 1.2455604106486533, + 0.7424591156074027, + 0.9919912330068748, + 1.366295368691577 + ], + "radii": [ + 0.10460190507172525, + 0.11047163450910968, + 0.07949284221517193, + 0.08804571249384024, + 0.12289833318737291, + 0.08959803638725514, + 0.1011510870155174, + 0.12789780735305226 + ], + "pos_0": [ + [ + -0.7544663087797417, + 0.7582124153667615 + ], + [ + -0.6248438019259824, + 0.7735486517325506 + ], + [ + -0.3100653230956832, + 1.2540261511849118 + ], + [ + -0.16385768754837496, + 0.816653774207407 + ], + [ + 0.08596563516784864, + 0.8694988371696188 + ], + [ + 0.36909572875200997, + 0.8741576780730848 + ], + [ + 0.5601116873708519, + 1.168876036417066 + ], + [ + 0.7504381941912207, + 1.2867526401203684 + ] + ], + "vel_0": [ + [ + -0.46963302657519507, + 0.0 + ], + [ + -0.46119805817380144, + 0.0 + ], + [ + -0.46768905711501274, + 0.0 + ], + [ + 0.38219520089544023, + 0.0 + ], + [ + -0.24097964115103593, + 0.0 + ], + [ + 0.3457403377803504, + 0.0 + ], + [ + -0.3678492602459004, + 0.0 + ], + [ + -0.3432990834635469, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/399/simulation_code.py b/scipy/balls_in_basin/399/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0e9edc531bc7edf69ac488f736b9096cfac5459b --- /dev/null +++ b/scipy/balls_in_basin/399/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 8 +masses = np.array([0.9889154112091929, 0.9515505511331617, 0.6577998679780545, 0.7520381921862338, 1.2455604106486533, 0.7424591156074027, 0.9919912330068748, 1.366295368691577]) +radii = np.array([0.10460190507172525, 0.11047163450910968, 0.07949284221517193, 0.08804571249384024, 0.12289833318737291, 0.08959803638725514, 0.1011510870155174, 0.12789780735305226]) +e_wall = 0.8374060334182114 +e_ball = 0.8798037909265465 +mu_floor = 0.07444507082694406 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.7544663087797417, 0.7582124153667615], [-0.6248438019259824, 0.7735486517325506], [-0.3100653230956832, 1.2540261511849118], [-0.16385768754837496, 0.816653774207407], [0.08596563516784864, 0.8694988371696188], [0.36909572875200997, 0.8741576780730848], [0.5601116873708519, 1.168876036417066], [0.7504381941912207, 1.2867526401203684]], dtype=float) +vel_0 = np.array([[-0.46963302657519507, 0.0], [-0.46119805817380144, 0.0], [-0.46768905711501274, 0.0], [0.38219520089544023, 0.0], [-0.24097964115103593, 0.0], [0.3457403377803504, 0.0], [-0.3678492602459004, 0.0], [-0.3432990834635469, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/399/video.mp4 b/scipy/balls_in_basin/399/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..ff0a78ae5f6d04ea818f0c158a92c7a258d08a22 --- /dev/null +++ b/scipy/balls_in_basin/399/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a82daf50409ae1ab9e155007c866d49e7e2b3caa8c0b67c4e4955ccf75415a1a +size 29617 diff --git a/scipy/balls_in_basin/437/cot.json b/scipy/balls_in_basin/437/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..467c85360141a276bf2d6c93ec3954eec654cf8e --- /dev/null +++ b/scipy/balls_in_basin/437/cot.json @@ -0,0 +1,214 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "8 spherical balls of distinct masses (masses=[1.0128568632993507, 1.3896063816769129, 1.1552713359265794, 0.6531119570431962, 0.877460009278318, 0.7472227707448351, 1.0207093336720268, 0.8753717125059148]) and radii (radii=[0.1018622760933045, 0.1279402699915947, 0.1131060019669655, 0.0769735375567729, 0.09999313297796776, 0.08804566151341348, 0.11753629601954313, 0.09180102914952432]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.67 and tangential floor friction mu_floor=0.064, and collide with each other (restitution e_ball=0.92).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 8, + "total_mass_kg": 7.7316, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.9551930060698234, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6685419141537838, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9214877494972593, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.06414550950129226, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 8, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.0128568632993507, + 1.3896063816769129, + 1.1552713359265794, + 0.6531119570431962, + 0.877460009278318, + 0.7472227707448351, + 1.0207093336720268, + 0.8753717125059148 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.1018622760933045, + 0.1279402699915947, + 0.1131060019669655, + 0.0769735375567729, + 0.09999313297796776, + 0.08804566151341348, + 0.11753629601954313, + 0.09180102914952432 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.8170108836008426, + 0.9038524077102473 + ], + [ + -0.5364479580165685, + 1.0339709918766342 + ], + [ + -0.33371065203029115, + 1.1964198499360608 + ], + [ + -0.152997447693958, + 0.9376847872310888 + ], + [ + 0.17061598052917104, + 0.933615516645681 + ], + [ + 0.36046043519686133, + 0.97937927437732 + ], + [ + 0.610941862802459, + 0.7874871861139117 + ], + [ + 0.8510000896183195, + 0.7043084504284358 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + -0.5070323348784361, + 0.0 + ], + [ + -0.34511899028045345, + 0.0 + ], + [ + 0.24851290446097107, + 0.0 + ], + [ + -0.5074058006486388, + 0.0 + ], + [ + -0.3104051763842707, + 0.0 + ], + [ + -0.3724052345681842, + 0.0 + ], + [ + 0.25032419835036473, + 0.0 + ], + [ + 0.5283278736475159, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/437/params.json b/scipy/balls_in_basin/437/params.json new file mode 100644 index 0000000000000000000000000000000000000000..cf3b2e57684c6add03d10924752cae4f74538393 --- /dev/null +++ b/scipy/balls_in_basin/437/params.json @@ -0,0 +1,101 @@ +{ + "g": 9.8, + "seed": 0.9551930060698234, + "e_wall": 0.6685419141537838, + "e_ball": 0.9214877494972593, + "mu_floor": 0.06414550950129226, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 8, + "masses": [ + 1.0128568632993507, + 1.3896063816769129, + 1.1552713359265794, + 0.6531119570431962, + 0.877460009278318, + 0.7472227707448351, + 1.0207093336720268, + 0.8753717125059148 + ], + "radii": [ + 0.1018622760933045, + 0.1279402699915947, + 0.1131060019669655, + 0.0769735375567729, + 0.09999313297796776, + 0.08804566151341348, + 0.11753629601954313, + 0.09180102914952432 + ], + "pos_0": [ + [ + -0.8170108836008426, + 0.9038524077102473 + ], + [ + -0.5364479580165685, + 1.0339709918766342 + ], + [ + -0.33371065203029115, + 1.1964198499360608 + ], + [ + -0.152997447693958, + 0.9376847872310888 + ], + [ + 0.17061598052917104, + 0.933615516645681 + ], + [ + 0.36046043519686133, + 0.97937927437732 + ], + [ + 0.610941862802459, + 0.7874871861139117 + ], + [ + 0.8510000896183195, + 0.7043084504284358 + ] + ], + "vel_0": [ + [ + -0.5070323348784361, + 0.0 + ], + [ + -0.34511899028045345, + 0.0 + ], + [ + 0.24851290446097107, + 0.0 + ], + [ + -0.5074058006486388, + 0.0 + ], + [ + -0.3104051763842707, + 0.0 + ], + [ + -0.3724052345681842, + 0.0 + ], + [ + 0.25032419835036473, + 0.0 + ], + [ + 0.5283278736475159, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/437/simulation_code.py b/scipy/balls_in_basin/437/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..405ddc1b71a7e04d7c0f05da7eaeea569b57874b --- /dev/null +++ b/scipy/balls_in_basin/437/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 8 +masses = np.array([1.0128568632993507, 1.3896063816769129, 1.1552713359265794, 0.6531119570431962, 0.877460009278318, 0.7472227707448351, 1.0207093336720268, 0.8753717125059148]) +radii = np.array([0.1018622760933045, 0.1279402699915947, 0.1131060019669655, 0.0769735375567729, 0.09999313297796776, 0.08804566151341348, 0.11753629601954313, 0.09180102914952432]) +e_wall = 0.6685419141537838 +e_ball = 0.9214877494972593 +mu_floor = 0.06414550950129226 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.8170108836008426, 0.9038524077102473], [-0.5364479580165685, 1.0339709918766342], [-0.33371065203029115, 1.1964198499360608], [-0.152997447693958, 0.9376847872310888], [0.17061598052917104, 0.933615516645681], [0.36046043519686133, 0.97937927437732], [0.610941862802459, 0.7874871861139117], [0.8510000896183195, 0.7043084504284358]], dtype=float) +vel_0 = np.array([[-0.5070323348784361, 0.0], [-0.34511899028045345, 0.0], [0.24851290446097107, 0.0], [-0.5074058006486388, 0.0], [-0.3104051763842707, 0.0], [-0.3724052345681842, 0.0], [0.25032419835036473, 0.0], [0.5283278736475159, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/437/video.mp4 b/scipy/balls_in_basin/437/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..5c00c09168f04890b8bbb006c60dfa7ff850237e --- /dev/null +++ b/scipy/balls_in_basin/437/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b949aa090b86d92206b39a042c59adb987d3e05137b5b5646a4c708c3a46366b +size 19893 diff --git a/scipy/balls_in_basin/708/cot.json b/scipy/balls_in_basin/708/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ffaa723616c1aab951532109be0d7eeed1422bb2 --- /dev/null +++ b/scipy/balls_in_basin/708/cot.json @@ -0,0 +1,174 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "4 spherical balls of distinct masses (masses=[1.149138970682586, 1.1154620181776351, 1.207492986896481, 1.2288032374566846]) and radii (radii=[0.1257179131255322, 0.11863590929412737, 0.12843406031698246, 0.12385005500662438]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.77 and tangential floor friction mu_floor=0.049, and collide with each other (restitution e_ball=0.92).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 4, + "total_mass_kg": 4.7009, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.9005373331936655, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.7720370494393619, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9166966972105212, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.049488043351991994, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 4, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.149138970682586, + 1.1154620181776351, + 1.207492986896481, + 1.2288032374566846 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.1257179131255322, + 0.11863590929412737, + 0.12843406031698246, + 0.12385005500662438 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.5810664244656806, + 0.9123676918312349 + ], + [ + -0.3097856228520016, + 1.2884251728701437 + ], + [ + 0.1770479660363066, + 0.7485983547332584 + ], + [ + 0.6610518121859804, + 1.156687734259257 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + 0.4097590175467677, + 0.0 + ], + [ + -0.26355832184496336, + 0.0 + ], + [ + 0.4780443532950715, + 0.0 + ], + [ + -0.3149073723216984, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/708/params.json b/scipy/balls_in_basin/708/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5bfaf89c1884f4c7e3b7ff99fceeedd7c5278b3c --- /dev/null +++ b/scipy/balls_in_basin/708/params.json @@ -0,0 +1,61 @@ +{ + "g": 9.8, + "seed": 0.9005373331936655, + "e_wall": 0.7720370494393619, + "e_ball": 0.9166966972105212, + "mu_floor": 0.049488043351991994, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 4, + "masses": [ + 1.149138970682586, + 1.1154620181776351, + 1.207492986896481, + 1.2288032374566846 + ], + "radii": [ + 0.1257179131255322, + 0.11863590929412737, + 0.12843406031698246, + 0.12385005500662438 + ], + "pos_0": [ + [ + -0.5810664244656806, + 0.9123676918312349 + ], + [ + -0.3097856228520016, + 1.2884251728701437 + ], + [ + 0.1770479660363066, + 0.7485983547332584 + ], + [ + 0.6610518121859804, + 1.156687734259257 + ] + ], + "vel_0": [ + [ + 0.4097590175467677, + 0.0 + ], + [ + -0.26355832184496336, + 0.0 + ], + [ + 0.4780443532950715, + 0.0 + ], + [ + -0.3149073723216984, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/708/simulation_code.py b/scipy/balls_in_basin/708/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..1f9323afb9d3876ebb422ae96229f893224790d6 --- /dev/null +++ b/scipy/balls_in_basin/708/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 4 +masses = np.array([1.149138970682586, 1.1154620181776351, 1.207492986896481, 1.2288032374566846]) +radii = np.array([0.1257179131255322, 0.11863590929412737, 0.12843406031698246, 0.12385005500662438]) +e_wall = 0.7720370494393619 +e_ball = 0.9166966972105212 +mu_floor = 0.049488043351991994 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.5810664244656806, 0.9123676918312349], [-0.3097856228520016, 1.2884251728701437], [0.1770479660363066, 0.7485983547332584], [0.6610518121859804, 1.156687734259257]], dtype=float) +vel_0 = np.array([[0.4097590175467677, 0.0], [-0.26355832184496336, 0.0], [0.4780443532950715, 0.0], [-0.3149073723216984, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/708/video.mp4 b/scipy/balls_in_basin/708/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..9c6d634ed01739ac7029563fcc560ec29b6b1968 --- /dev/null +++ b/scipy/balls_in_basin/708/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:450ba91b882918d9658ba153af39286274b9a144bdd6e024a563b86521e7066d +size 18562 diff --git a/scipy/balls_in_basin/918/cot.json b/scipy/balls_in_basin/918/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..7d0fd32fa6ed2ee793243df6211acdd667ad590f --- /dev/null +++ b/scipy/balls_in_basin/918/cot.json @@ -0,0 +1,194 @@ +{ + "simulation": "balls_in_basin", + "physical_observation": "6 spherical balls of distinct masses (masses=[1.2610259827754962, 0.860405919794166, 0.9789468886634793, 0.8183743422241678, 0.9694000052911333, 0.9786212466232043]) and radii (radii=[0.11994587620173383, 0.09497947955372085, 0.10944700372771508, 0.09548565320869715, 0.11428319683546172, 0.10417041603488858]) are released into a U-shaped basin with vertical walls. Inside the basin, balls fall under gravity (g=9.800 m/s^2), bounce off walls/floor with restitution e_wall=0.83 and tangential floor friction mu_floor=0.055, and collide with each other (restitution e_ball=0.94).", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution", + "statement": "Free-flight under gravity; impulsive ball-wall and ball-ball collisions resolved with line-of-centres impulse and restitution.", + "formula": "Jn = -(1+e) v_rel \\cdot n / (1/m_i + 1/m_j); v_n -> -e v_n at walls" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "vel += -g e_y dt (between collisions)", + "pos += vel dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ], + "equations_python": [ + "vel += -g e_y dt (between collisions)", + "pos += vel * dt", + "if ball-wall: v_n = -e_wall v_n", + "if ball-ball: Jn = -(1+e_ball) * v_rel.n / (1/m_i + 1/m_j); apply equal/opposite impulses" + ] + }, + "derivation_steps": [ + "Between collisions, each ball satisfies \\dot{v} = (0, -g) with no air drag.", + "Ball-wall: snap penetrating coordinate to wall, reflect normal velocity with restitution e_wall, multiply tangential by (1-mu_floor) on floor.", + "Ball-ball: line-of-centres impulse Jn = -(1+e_ball) v_rel.n / (1/m_i + 1/m_j); positions snapped apart by overlap to remove penetration.", + "Time-step: velocity Verlet with dt small enough for stable contact." + ], + "physical_constants_derived": { + "N_balls": 6, + "total_mass_kg": 5.8668, + "duration_s": 12.0 + }, + "numerical_method": { + "solver": "Velocity Verlet (fixed-step) with impulsive collision resolution", + "tolerance": "dt = 0.002 s; ~6000 steps for 12 s", + "justification": "More robust than RK45 events for many simultaneous pairwise contacts." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved at each binary collision.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Initial fall under gravity; first floor contact at t ~ \\sqrt(2 \\cdot 1.0/g) = 0.452 s.", + "Multiple ball-ball and ball-wall collisions producing energy decay.", + "Eventually all balls settle near the basin floor with low residual KE." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "seed": { + "value": 0.9523302888229979, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.8308338765823168, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.942777483544319, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.0551548539932562, + "unit": "", + "description": "" + }, + "x_left": { + "value": -1.1, + "unit": "", + "description": "" + }, + "x_right": { + "value": 1.1, + "unit": "", + "description": "" + }, + "y_wall": { + "value": 1.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.002, + "unit": "", + "description": "" + }, + "N": { + "value": 6, + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.2610259827754962, + 0.860405919794166, + 0.9789468886634793, + 0.8183743422241678, + 0.9694000052911333, + 0.9786212466232043 + ], + "unit": "", + "description": "" + }, + "radii": { + "value": [ + 0.11994587620173383, + 0.09497947955372085, + 0.10944700372771508, + 0.09548565320869715, + 0.11428319683546172, + 0.10417041603488858 + ], + "unit": "", + "description": "" + }, + "pos_0": { + "value": [ + [ + -0.7673856307307445, + 1.2705310959835576 + ], + [ + -0.49132631757174144, + 1.1089965510927158 + ], + [ + -0.21880193577828752, + 1.0539428339265378 + ], + [ + 0.19113394552687613, + 1.2288838158110347 + ], + [ + 0.4894697025738779, + 1.18609402253571 + ], + [ + 0.7502367929162339, + 1.193299578897598 + ] + ], + "unit": "", + "description": "" + }, + "vel_0": { + "value": [ + [ + 0.4352804890460279, + 0.0 + ], + [ + 0.4849806249569407, + 0.0 + ], + [ + -0.26565708226120843, + 0.0 + ], + [ + 0.21263415084522252, + 0.0 + ], + [ + 0.4171316770159964, + 0.0 + ], + [ + -0.36522586982451666, + 0.0 + ] + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/balls_in_basin/918/params.json b/scipy/balls_in_basin/918/params.json new file mode 100644 index 0000000000000000000000000000000000000000..d94b4a034c64f5461f06794f01f900aa0221ad9e --- /dev/null +++ b/scipy/balls_in_basin/918/params.json @@ -0,0 +1,81 @@ +{ + "g": 9.8, + "seed": 0.9523302888229979, + "e_wall": 0.8308338765823168, + "e_ball": 0.942777483544319, + "mu_floor": 0.0551548539932562, + "x_left": -1.1, + "x_right": 1.1, + "y_wall": 1.6, + "duration": 12.0, + "dt": 0.002, + "N": 6, + "masses": [ + 1.2610259827754962, + 0.860405919794166, + 0.9789468886634793, + 0.8183743422241678, + 0.9694000052911333, + 0.9786212466232043 + ], + "radii": [ + 0.11994587620173383, + 0.09497947955372085, + 0.10944700372771508, + 0.09548565320869715, + 0.11428319683546172, + 0.10417041603488858 + ], + "pos_0": [ + [ + -0.7673856307307445, + 1.2705310959835576 + ], + [ + -0.49132631757174144, + 1.1089965510927158 + ], + [ + -0.21880193577828752, + 1.0539428339265378 + ], + [ + 0.19113394552687613, + 1.2288838158110347 + ], + [ + 0.4894697025738779, + 1.18609402253571 + ], + [ + 0.7502367929162339, + 1.193299578897598 + ] + ], + "vel_0": [ + [ + 0.4352804890460279, + 0.0 + ], + [ + 0.4849806249569407, + 0.0 + ], + [ + -0.26565708226120843, + 0.0 + ], + [ + 0.21263415084522252, + 0.0 + ], + [ + 0.4171316770159964, + 0.0 + ], + [ + -0.36522586982451666, + 0.0 + ] + ] +} \ No newline at end of file diff --git a/scipy/balls_in_basin/918/simulation_code.py b/scipy/balls_in_basin/918/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..119bf67c77fc9202e29123761a122848a1a4b310 --- /dev/null +++ b/scipy/balls_in_basin/918/simulation_code.py @@ -0,0 +1,135 @@ +"""Balls in basin - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +N = 6 +masses = np.array([1.2610259827754962, 0.860405919794166, 0.9789468886634793, 0.8183743422241678, 0.9694000052911333, 0.9786212466232043]) +radii = np.array([0.11994587620173383, 0.09497947955372085, 0.10944700372771508, 0.09548565320869715, 0.11428319683546172, 0.10417041603488858]) +e_wall = 0.8308338765823168 +e_ball = 0.942777483544319 +mu_floor = 0.0551548539932562 +x_left = -1.1 +x_right = 1.1 +y_wall = 1.6 +duration = 12 +pos_0 = np.array([[-0.7673856307307445, 1.2705310959835576], [-0.49132631757174144, 1.1089965510927158], [-0.21880193577828752, 1.0539428339265378], [0.19113394552687613, 1.2288838158110347], [0.4894697025738779, 1.18609402253571], [0.7502367929162339, 1.193299578897598]], dtype=float) +vel_0 = np.array([[0.4352804890460279, 0.0], [0.4849806249569407, 0.0], [-0.26565708226120843, 0.0], [0.21263415084522252, 0.0], [0.4171316770159964, 0.0], [-0.36522586982451666, 0.0]], dtype=float) + +dt = 0.002 +n_steps = int(round(duration / dt)) +fps = 30 +stride = max(1, int(round((1.0 / fps) / dt))) +n_frames = n_steps // stride + 1 +a_grav = np.array([0.0, -g]) + +pos = pos_0.copy() +vel = vel_0.copy() +frames_pos = np.zeros((n_frames, N, 2)) +frames_pos[0] = pos +frame_idx = 1 + +def resolve_walls(): + for i in range(N): + r = radii[i] + if pos[i, 0] - r < x_left: + pos[i, 0] = x_left + r + if vel[i, 0] < 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 0] + r > x_right: + pos[i, 0] = x_right - r + if vel[i, 0] > 0.0: + vel[i, 0] = -e_wall * vel[i, 0] + if pos[i, 1] - r < 0.0: + pos[i, 1] = r + if vel[i, 1] < 0.0: + vel[i, 1] = -e_wall * vel[i, 1] + vel[i, 0] *= (1.0 - mu_floor) + +def resolve_pairs(): + for _it in range(4): + any_overlap = False + for i in range(N): + for j in range(i+1, N): + d = pos[j] - pos[i] + dist = float(np.hypot(d[0], d[1])) + rsum = radii[i] + radii[j] + if dist >= rsum or dist < 1e-12: + continue + nhat = d / dist + v_rel = vel[j] - vel[i] + vn = float(v_rel @ nhat) + overlap = rsum - dist + inv_mi = 1.0 / masses[i] + inv_mj = 1.0 / masses[j] + inv_sum = inv_mi + inv_mj + pos[i] -= (overlap * inv_mi / inv_sum) * nhat + pos[j] += (overlap * inv_mj / inv_sum) * nhat + if vn < 0.0: + Jn = -(1.0 + e_ball) * vn / inv_sum + vel[i] -= (Jn * inv_mi) * nhat + vel[j] += (Jn * inv_mj) * nhat + any_overlap = True + if not any_overlap: + break + +for step in range(1, n_steps + 1): + vel += 0.5 * a_grav * dt + pos += vel * dt + vel += 0.5 * a_grav * dt + resolve_walls() + resolve_pairs() + if step % stride == 0 and frame_idx < n_frames: + frames_pos[frame_idx] = pos + frame_idx += 1 + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = x_left - 0.20, x_right + 0.20 +y_lo_d, y_hi_d = -0.20, y_wall + 0.40 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +wall_thick = 0.10 +ax.add_patch(patches.Rectangle((x_left - wall_thick, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((x_right, 0.0), wall_thick, y_wall, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_left, x_left], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([x_right, x_right], [0, y_wall], color="#adb5bd", lw=2.5, zorder=1) + +ball_colors = ["#e63946", "#ffd166", "#06d6a0", "#118ab2", "#7b2cbf", + "#f4a261", "#9b5de5", "#00bbf9", "#fb6f92", "#80ed99"] +circles = [] +for i in range(N): + c = patches.Circle(tuple(frames_pos[0, i]), float(radii[i]), + facecolor=ball_colors[i % len(ball_colors)], + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(c) + circles.append(c) + +def update(f): + for i, c in enumerate(circles): + c.center = (float(frames_pos[f, i, 0]), float(frames_pos[f, i, 1])) + return circles + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/balls_in_basin/918/video.mp4 b/scipy/balls_in_basin/918/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d9d14841b6551fa28e66d567f61522694cf5305b --- /dev/null +++ b/scipy/balls_in_basin/918/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8d1f7ff60a02be1294fb85917be4b1508ca7eeebbef2526e0de947faa0464cd2 +size 30079 diff --git a/scipy/belt_gear_rack_train/032/cot.json b/scipy/belt_gear_rack_train/032/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..58822dccb6372a6b7462d5798849a200dad7b134 --- /dev/null +++ b/scipy/belt_gear_rack_train/032/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.310 m, N=31 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.445 rad/s in t_ramp=2.104 s). Top gear meshes externally with a middle gear (R=0.180, N=18); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/032/video.mp4 b/scipy/belt_gear_rack_train/032/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..eee74537b9696dbb97f5599e206022616f207d70 --- /dev/null +++ b/scipy/belt_gear_rack_train/032/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f3a32e1b6ba6b862555c034427b63accd22a90065ec5d1e34ffaefdad69731d6 +size 33544 diff --git a/scipy/belt_gear_rack_train/057/cot.json b/scipy/belt_gear_rack_train/057/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..398fe8656d79aefd6c9a5fb4bc6538dd339a4bba --- /dev/null +++ b/scipy/belt_gear_rack_train/057/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.310 m, N=31 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.730 rad/s in t_ramp=1.606 s). Top gear meshes externally with a middle gear (R=0.210, N=21); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/057/video.mp4 b/scipy/belt_gear_rack_train/057/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..c9014adc78f504f301c27f643da7556767a16e7b --- /dev/null +++ b/scipy/belt_gear_rack_train/057/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e5b5df84f47f797cbda9611b61b3bc76c5d86e2c8a61acf7424ad004d3c18ae0 +size 28847 diff --git a/scipy/belt_gear_rack_train/167/cot.json b/scipy/belt_gear_rack_train/167/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..160fd6bb324db8c3e5e41443ee850c159194eafb --- /dev/null +++ b/scipy/belt_gear_rack_train/167/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.290 m, N=29 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.751 rad/s in t_ramp=2.341 s). Top gear meshes externally with a middle gear (R=0.200, N=20); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/167/video.mp4 b/scipy/belt_gear_rack_train/167/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d915ff003f9fcbc73e5fe4f56d591121e2c5ff39 --- /dev/null +++ b/scipy/belt_gear_rack_train/167/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5c3cdba424efb7dd3c7f2063d7091c11c54629994f5bf7733464e655cbd6d4ab +size 32201 diff --git a/scipy/belt_gear_rack_train/175/cot.json b/scipy/belt_gear_rack_train/175/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..f951cfd3fbae83106db99f00942d5aa4b5fc0b04 --- /dev/null +++ b/scipy/belt_gear_rack_train/175/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.220 m, N=22 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.265 rad/s in t_ramp=1.703 s). Top gear meshes externally with a middle gear (R=0.130, N=13); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/175/video.mp4 b/scipy/belt_gear_rack_train/175/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f1ef5aec8b637b7459631aee85b9033cac5d427c --- /dev/null +++ b/scipy/belt_gear_rack_train/175/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2f71044635672eae4d66e1860a705ef818761806d736ecc4feeec488e2387663 +size 35092 diff --git a/scipy/belt_gear_rack_train/224/cot.json b/scipy/belt_gear_rack_train/224/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ea65806159fb5abc6c7e6c3367701f1a5a972465 --- /dev/null +++ b/scipy/belt_gear_rack_train/224/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.320 m, N=32 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.142 rad/s in t_ramp=2.302 s). Top gear meshes externally with a middle gear (R=0.170, N=17); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/224/video.mp4 b/scipy/belt_gear_rack_train/224/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7067e5d50ff72dc29e110553f291473ac9701ac1 --- /dev/null +++ b/scipy/belt_gear_rack_train/224/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:52cc014306abc27e96bf63ab76e632e8c61532ac4f5559c5ec55a4e3d8789f34 +size 38686 diff --git a/scipy/belt_gear_rack_train/234/cot.json b/scipy/belt_gear_rack_train/234/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ff98155383a8b4e2d1f0237c639dd8142dc41b70 --- /dev/null +++ b/scipy/belt_gear_rack_train/234/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.240 m, N=24 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.382 rad/s in t_ramp=2.283 s). Top gear meshes externally with a middle gear (R=0.180, N=18); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/234/video.mp4 b/scipy/belt_gear_rack_train/234/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a27836861c6cb72e14ecd6de60ad7214b9a4e310 --- /dev/null +++ b/scipy/belt_gear_rack_train/234/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:74a30ee95b53e8079146ede9e4458861c034c6f8a57ad2a3d1df4900c08cc3f9 +size 36593 diff --git a/scipy/belt_gear_rack_train/250/cot.json b/scipy/belt_gear_rack_train/250/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..7b90001eda58de061cf4224d5c26f733a1b20102 --- /dev/null +++ b/scipy/belt_gear_rack_train/250/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.350 m, N=35 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.255 rad/s in t_ramp=1.602 s). Top gear meshes externally with a middle gear (R=0.160, N=16); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/250/video.mp4 b/scipy/belt_gear_rack_train/250/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3b8ef49074cfde03152f99607ee2ef7dd9afaaae --- /dev/null +++ b/scipy/belt_gear_rack_train/250/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6efb4fa427f48ab84f8995ef4c9d0d6e801ae9f6a4251cdcbeab7aeb668446c9 +size 32899 diff --git a/scipy/belt_gear_rack_train/276/cot.json b/scipy/belt_gear_rack_train/276/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..9916b3b6dd49f96e9f52b64569998dc4c6afce86 --- /dev/null +++ b/scipy/belt_gear_rack_train/276/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.330 m, N=33 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.128 rad/s in t_ramp=2.083 s). Top gear meshes externally with a middle gear (R=0.160, N=16); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/276/video.mp4 b/scipy/belt_gear_rack_train/276/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f54dd5011051d6e9d60cf8d4da7959dae599baf5 --- /dev/null +++ b/scipy/belt_gear_rack_train/276/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:179c51e1ecc213c13b905197ae1b307b269f831fbc29275cc114fbc04b111337 +size 38541 diff --git a/scipy/belt_gear_rack_train/323/cot.json b/scipy/belt_gear_rack_train/323/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..1229298c3094cdb0678b5ad219e1ba77714d57c9 --- /dev/null +++ b/scipy/belt_gear_rack_train/323/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.310 m, N=31 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.397 rad/s in t_ramp=2.285 s). Top gear meshes externally with a middle gear (R=0.130, N=13); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/323/video.mp4 b/scipy/belt_gear_rack_train/323/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..86247c24c1d7843bdb60bd08651c5372c76c7db8 --- /dev/null +++ b/scipy/belt_gear_rack_train/323/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0a8bf8de9d802a836d33b38588fbd9a5838345f499d78a37352e2639e55a5205 +size 33947 diff --git a/scipy/belt_gear_rack_train/332/cot.json b/scipy/belt_gear_rack_train/332/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..bbd02c1097572daa0cb81ee9a00023e2012e7a02 --- /dev/null +++ b/scipy/belt_gear_rack_train/332/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.350 m, N=35 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.334 rad/s in t_ramp=1.519 s). Top gear meshes externally with a middle gear (R=0.120, N=12); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/332/video.mp4 b/scipy/belt_gear_rack_train/332/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..029a6218da28ff73551f573baf1881eb8fa06c9e --- /dev/null +++ b/scipy/belt_gear_rack_train/332/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f3cbc7c86afb168a36ec5b1bdbcad3b795757b2410b2ca5d89beb4baa018d1c2 +size 33333 diff --git a/scipy/belt_gear_rack_train/388/cot.json b/scipy/belt_gear_rack_train/388/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..909719415385b37de587a708c4552403fcf064bc --- /dev/null +++ b/scipy/belt_gear_rack_train/388/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.300 m, N=30 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.223 rad/s in t_ramp=2.010 s). Top gear meshes externally with a middle gear (R=0.190, N=19); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/388/video.mp4 b/scipy/belt_gear_rack_train/388/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..909190d8f8e4d3b5b58a6494dcef2c149496c2e9 --- /dev/null +++ b/scipy/belt_gear_rack_train/388/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e6afd9df67ec26e617f11b69aaf4fcd4391f4e3ca215b79aae574b3a7722d7a4 +size 39620 diff --git a/scipy/belt_gear_rack_train/410/cot.json b/scipy/belt_gear_rack_train/410/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..65347cc63a3f9ad08364de9da555b17be16c6fc6 --- /dev/null +++ b/scipy/belt_gear_rack_train/410/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.300 m, N=30 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.607 rad/s in t_ramp=2.022 s). Top gear meshes externally with a middle gear (R=0.170, N=17); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/410/video.mp4 b/scipy/belt_gear_rack_train/410/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a7917bd67aa26265f3e906de774ced576b58e8cc --- /dev/null +++ b/scipy/belt_gear_rack_train/410/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:90dc80b4c37304fda1783e200a82d38291f073a12f5a0435429c1ebcc507c5f7 +size 33154 diff --git a/scipy/belt_gear_rack_train/473/cot.json b/scipy/belt_gear_rack_train/473/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..93f4cfabaac510c7123542e3abbd1184b7b68ed6 --- /dev/null +++ b/scipy/belt_gear_rack_train/473/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.330 m, N=33 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.433 rad/s in t_ramp=2.026 s). Top gear meshes externally with a middle gear (R=0.180, N=18); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/473/video.mp4 b/scipy/belt_gear_rack_train/473/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f4fbd65334bdc75853885ad278b2d05fdee13344 --- /dev/null +++ b/scipy/belt_gear_rack_train/473/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:36111160f6f34d3553db544ea8a347e7fd3d9f47dba754d340fb0de92eff684c +size 30825 diff --git a/scipy/belt_gear_rack_train/546/cot.json b/scipy/belt_gear_rack_train/546/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..e187e71e0874e76a4ebececd74890da77c2731c5 --- /dev/null +++ b/scipy/belt_gear_rack_train/546/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.340 m, N=34 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.385 rad/s in t_ramp=2.166 s). Top gear meshes externally with a middle gear (R=0.200, N=20); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/546/video.mp4 b/scipy/belt_gear_rack_train/546/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1a9d9a0f39a8a8970695a64b158d55a001767ae0 --- /dev/null +++ b/scipy/belt_gear_rack_train/546/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f8cd13c6ca136d0440e591b8939be61421ade08423f7b4cc7125ae7357df272e +size 32942 diff --git a/scipy/belt_gear_rack_train/570/cot.json b/scipy/belt_gear_rack_train/570/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..6b63cc54b4eff81796b7d285cb082ca5e02cead7 --- /dev/null +++ b/scipy/belt_gear_rack_train/570/cot.json @@ -0,0 +1,162 @@ +{ + "simulation": "belt_gear_rack_train", + "physical_observation": "Vertical stack: belt drives a top gear (R=0.240 m, N=24 teeth) counter-clockwise via a pulley (omega ramps to omega_max=1.734 rad/s in t_ramp=1.617 s). Top gear meshes externally with a middle gear (R=0.210, N=21); middle gear meshes with a horizontal rack of length 4.00 m which translates leftward.", + "physical_law": { + "name": "Rigid-body kinematics with rolling constraints (no slip)", + "statement": "Top-mid: omega_mid = -(R_top/R_mid) omega_top. Mid-rack: v_rack = omega_mid R_mid = -omega_top R_top.", + "formula": "theta_mid = -(R_top/R_mid) theta_top; x_rack = x_rack_0 - R_top theta_top" + }, + "ode_system": { + "state_variables": [ + "theta_top", + "theta_mid", + "x_rack" + ], + "equations_latex": [ + "omega_top(t) = omega_max \\cdot min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ], + "equations_python": [ + "omega_top(t) = omega_max * min(t/t_ramp, 1)", + "theta_top(t) = integral of omega_top from 0 to t", + "theta_mid(t) = -(R_top/R_mid) * theta_top(t)", + "x_rack(t) = x_rack_0 - R_top theta_top(t); frozen after disengagement" + ] + }, + "derivation_steps": [ + "Top gear angular speed ramps linearly from 0 to omega_max over t_ramp.", + "theta_top(t) = (omega_max/t_ramp) t^2/2 for t x_right: continue + x0c = max(x0, x_left); x1c = min(x1, x_right) + if x0 >= x_left: + top_pts.append((x0c, y_ded)); top_pts.append((x0c, y_add)) + else: + top_pts.append((x_left, y_add)) + if x1 <= x_right: + top_pts.append((x1c, y_add)); top_pts.append((x1c, y_ded)) + else: + top_pts.append((x_right, y_add)) + top_pts.append((x_right, y_ded)) + bot_pts = [(x_right, y_bot), (x_left, y_bot)] + return np.array(top_pts + bot_pts) + + +cTop = np.array([x_axle, y_top]) +cMid = np.array([x_axle, y_mid]) +rack_disp_total = -R_top * theta_top_int(duration) +x_rack_min = x_rack_0 + rack_disp_total +x_rack_max = x_rack_0 +rack_left_edge_min = x_rack_min - 0.5 * rack_length +rack_right_edge_max = x_rack_max + 0.5 * rack_length + +fig_w, fig_h = 12.0, 6.5 +fig_aspect = fig_w / fig_h +x_lo_d = rack_left_edge_min - 0.30 +x_hi_d = rack_right_edge_max + 0.30 +y_lo_d = y_rack - rack_height - 0.30 +y_hi_d = y_top + R_top + addendum + 1.30 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(Polygon([(x_lo, y_lo), (x_hi, y_lo), + (x_hi, y_rack - dedendum - rack_height - 0.04), + (x_lo, y_rack - dedendum - rack_height - 0.04)], + facecolor="#0f1525", edgecolor="none", zorder=0)) + +gear_pitch_mid = 2.0 * np.pi / N_mid +theta_mid_0 = -np.pi / 2 + np.pi / N_mid +theta_mid_0 -= np.floor(theta_mid_0 / gear_pitch_mid) * gear_pitch_mid +gear_pitch_top = 2.0 * np.pi / N_top +theta_top_0 = -np.pi / 2 +theta_top_0 -= np.floor(theta_top_0 / gear_pitch_top) * gear_pitch_top +rack_phase_offset_0 = x_axle + +gTop_patch = Polygon(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=theta_top_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +gMid_patch = Polygon(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=theta_mid_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=4) +ax.add_patch(gTop_patch); ax.add_patch(gMid_patch) +ax.add_patch(Circle(cTop, R_top - dedendum * 1.4, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +ax.add_patch(Circle(cMid, R_mid - dedendum * 1.5, facecolor="#3a4357", + edgecolor="none", zorder=4.2)) +spokeTop_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeTop_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +spokeMid_a, = ax.plot([], [], color="#e63946", lw=3.0, zorder=4.5) +spokeMid_b, = ax.plot([], [], color="#f9c74f", lw=2.5, zorder=4.5) +rack_patch = Polygon(rack_polygon(x_rack_0, y_rack, rack_length, rack_height, + rack_pitch, x_offset=rack_phase_offset_0), + facecolor="#a8dadc", edgecolor="#adb5bd", lw=1.2, zorder=3.5) +ax.add_patch(rack_patch) +rack_hash_lc = LineCollection([], colors="#3a4357", linewidths=2.0, zorder=3.7) +ax.add_collection(rack_hash_lc) + +y_top_off = y_hi + 0.10 +n_arc = 60 +arc_angles = np.linspace(np.pi, 2.0 * np.pi, n_arc) +arc_pts = np.column_stack([cTop[0] + r_pulley * np.cos(arc_angles), + cTop[1] + r_pulley * np.sin(arc_angles)]) +belt_path = np.concatenate([ + [[cTop[0] - r_pulley, y_top_off]], arc_pts, + [[cTop[0] + r_pulley, y_top_off]], +]) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#4a4e69", + lw=10, solid_capstyle="butt", zorder=6) +ax.plot(belt_path[:, 0], belt_path[:, 1], color="#3a4357", + lw=7, solid_capstyle="butt", zorder=6.5) +ax.add_patch(Circle(cTop, r_pulley * 0.85, facecolor="#3a4357", + edgecolor="#adb5bd", lw=1.0, zorder=8)) +ax.add_patch(Circle(cTop, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) +ax.add_patch(Circle(cMid, 0.030, facecolor="#e63946", + edgecolor="#adb5bd", lw=0.8, zorder=9)) + +rack_hash_spacing = 0.20 + +def update(f): + t = f / fps + th_top = theta_top_0 + theta_top(t) + th_mid = theta_mid_0 + theta_mid(t) + gTop_patch.set_xy(gear_polygon(cTop[0], cTop[1], R_top, N_top, theta=th_top)) + gMid_patch.set_xy(gear_polygon(cMid[0], cMid[1], R_mid, N_mid, theta=th_mid)) + rmTop = R_top + addendum * 0.5 + spokeTop_a.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top)]) + spokeTop_b.set_data([cTop[0], cTop[0] + rmTop * np.cos(th_top + np.pi/2)], + [cTop[1], cTop[1] + rmTop * np.sin(th_top + np.pi/2)]) + rmMid = R_mid + addendum * 0.5 + spokeMid_a.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid)]) + spokeMid_b.set_data([cMid[0], cMid[0] + rmMid * np.cos(th_mid + np.pi/2)], + [cMid[1], cMid[1] + rmMid * np.sin(th_mid + np.pi/2)]) + cx_rack = x_rack_centre(t) + rack_disp = cx_rack - x_rack_0 + tooth_phase = rack_phase_offset_0 + rack_disp + rack_patch.set_xy(rack_polygon(cx_rack, y_rack, rack_length, rack_height, + rack_pitch, x_offset=tooth_phase)) + y_bot_top = y_rack - dedendum - rack_height + x_left = cx_rack - 0.5 * rack_length + x_right = cx_rack + 0.5 * rack_length + n_marks = int(np.floor(rack_length / rack_hash_spacing)) + segs = [] + for k in range(n_marks): + u_k = (k + 0.5) * rack_hash_spacing - 0.5 * rack_length + xm = cx_rack + u_k + if x_left + 0.02 <= xm <= x_right - 0.02: + segs.append([(xm, y_bot_top + 0.01), + (xm, y_bot_top + rack_height * 0.85)]) + rack_hash_lc.set_segments(segs) + return (gTop_patch, gMid_patch, rack_patch, + spokeTop_a, spokeTop_b, spokeMid_a, spokeMid_b, rack_hash_lc) + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/belt_gear_rack_train/570/video.mp4 b/scipy/belt_gear_rack_train/570/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..4da3132d474ed176666a5a0956c7b2458eff08b3 --- /dev/null +++ b/scipy/belt_gear_rack_train/570/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fedd53c91f78180585cc5a0a2bc369c32a4f4a9bb20ee4dc234f5dca4fb34874 +size 32499 diff --git a/scipy/billiards_break/067/cot.json b/scipy/billiards_break/067/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..415b987856a1a76d79f90bf2c013083c753b2be9 --- /dev/null +++ b/scipy/billiards_break/067/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (8.39, 0.18) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0099) decelerates each ball; ball-ball restitution e_ball=0.97, ball-cushion restitution e_wall=0.94.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 8.391 m/s; deceleration ~ mu \\cdot g = 0.0967 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 8.391, + "deceleration_mug_m_s2": 0.0967, + "stop_time_no_collision_s": 86.733 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.173 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9681041004201743, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.9375158718802888, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.009871635289539566, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": -0.012134991502794082, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 8.388722050065523, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": 0.18142816433463888, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/067/params.json b/scipy/billiards_break/067/params.json new file mode 100644 index 0000000000000000000000000000000000000000..9765607e3cf7b29e3d7e81f92dad68443e2249f1 --- /dev/null +++ b/scipy/billiards_break/067/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9681041004201743, + "e_wall": 0.9375158718802888, + "mu": 0.009871635289539566, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": -0.012134991502794082, + "vx_cue0": 8.388722050065523, + "vy_cue0": 0.18142816433463888, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/067/simulation_code.py b/scipy/billiards_break/067/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..9d519ccaa50b64ff2b3f759ffd692fb77f708299 --- /dev/null +++ b/scipy/billiards_break/067/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9681041004201743; e_wall = 0.9375158718802888; mu = 0.009871635289539566 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = -0.012134991502794082; vx_cue0 = 8.388722050065523; vy_cue0 = 0.18142816433463888 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/067/video.mp4 b/scipy/billiards_break/067/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f1fa2cb30871d0de9493a5d8d8c4440563ef6731 --- /dev/null +++ b/scipy/billiards_break/067/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4ab3c7222d01d6b71c0feff7e781fa7d6eb36a5b6e743b34fc4ee6a5dde8699 +size 65618 diff --git a/scipy/billiards_break/142/cot.json b/scipy/billiards_break/142/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..458fb8745092213a51d5b35771eb6ce3ef6907f7 --- /dev/null +++ b/scipy/billiards_break/142/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (7.54, 0.04) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0129) decelerates each ball; ball-ball restitution e_ball=0.91, ball-cushion restitution e_wall=0.92.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 7.543 m/s; deceleration ~ mu \\cdot g = 0.1265 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 7.543, + "deceleration_mug_m_s2": 0.1265, + "stop_time_no_collision_s": 59.627 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.192 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9146563197661238, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.9151529752576893, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.01290774353205792, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": 0.06572726595679451, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 7.542484514645326, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": 0.0382554798271158, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/142/params.json b/scipy/billiards_break/142/params.json new file mode 100644 index 0000000000000000000000000000000000000000..4cd6df1a63540ea2d9440274228f593859cc3feb --- /dev/null +++ b/scipy/billiards_break/142/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9146563197661238, + "e_wall": 0.9151529752576893, + "mu": 0.01290774353205792, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": 0.06572726595679451, + "vx_cue0": 7.542484514645326, + "vy_cue0": 0.0382554798271158, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/142/simulation_code.py b/scipy/billiards_break/142/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..72791f2a31a6368867c83fafbe7c13e6d4251c44 --- /dev/null +++ b/scipy/billiards_break/142/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9146563197661238; e_wall = 0.9151529752576893; mu = 0.01290774353205792 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = 0.06572726595679451; vx_cue0 = 7.542484514645326; vy_cue0 = 0.0382554798271158 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/142/video.mp4 b/scipy/billiards_break/142/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..b57952b654d3163047b77c77d860f8d73fa69246 --- /dev/null +++ b/scipy/billiards_break/142/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:516cb6114c231292c492e01d9b7c17bea21c5ad627768456fea239031ee166ca +size 54718 diff --git a/scipy/billiards_break/146/cot.json b/scipy/billiards_break/146/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ee246c3b3e23d78d2958946b833ed6cb932c1887 --- /dev/null +++ b/scipy/billiards_break/146/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (7.31, -0.08) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0090) decelerates each ball; ball-ball restitution e_ball=0.92, ball-cushion restitution e_wall=0.92.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 7.313 m/s; deceleration ~ mu \\cdot g = 0.0882 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 7.313, + "deceleration_mug_m_s2": 0.0882, + "stop_time_no_collision_s": 82.902 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.198 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9243156458548974, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.922103487792493, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.009000724239792358, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": 0.0862878866398457, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 7.312146002931289, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": -0.07595680815093196, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/146/params.json b/scipy/billiards_break/146/params.json new file mode 100644 index 0000000000000000000000000000000000000000..9d784a8bdbfc9d8804163b5f5b66460cdc3f1688 --- /dev/null +++ b/scipy/billiards_break/146/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9243156458548974, + "e_wall": 0.922103487792493, + "mu": 0.009000724239792358, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": 0.0862878866398457, + "vx_cue0": 7.312146002931289, + "vy_cue0": -0.07595680815093196, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/146/simulation_code.py b/scipy/billiards_break/146/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..1fd26977a95c3c2f287afe1e08d39b14ba53338c --- /dev/null +++ b/scipy/billiards_break/146/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9243156458548974; e_wall = 0.922103487792493; mu = 0.009000724239792358 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = 0.0862878866398457; vx_cue0 = 7.312146002931289; vy_cue0 = -0.07595680815093196 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/146/video.mp4 b/scipy/billiards_break/146/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..30616383496855a02a8243180e950b58ba1df944 --- /dev/null +++ b/scipy/billiards_break/146/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bc759a5671930c4e8647f983ed2311d61150a5b44727844897aed67a1febbfe4 +size 53497 diff --git a/scipy/billiards_break/216/cot.json b/scipy/billiards_break/216/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..15ed2fa81e981da0a170228787743d049bf8f692 --- /dev/null +++ b/scipy/billiards_break/216/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (7.83, -0.11) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0150) decelerates each ball; ball-ball restitution e_ball=0.92, ball-cushion restitution e_wall=0.87.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 7.830 m/s; deceleration ~ mu \\cdot g = 0.1469 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 7.83, + "deceleration_mug_m_s2": 0.1469, + "stop_time_no_collision_s": 53.302 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.185 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9161887993132826, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.8680123474938282, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.014990234800571628, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": 0.08733989447933123, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 7.829457171733064, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": -0.11209461932828689, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/216/params.json b/scipy/billiards_break/216/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b5e0a11beaf0eb4df4cd532b63ffc98c1ab86ca4 --- /dev/null +++ b/scipy/billiards_break/216/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9161887993132826, + "e_wall": 0.8680123474938282, + "mu": 0.014990234800571628, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": 0.08733989447933123, + "vx_cue0": 7.829457171733064, + "vy_cue0": -0.11209461932828689, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/216/simulation_code.py b/scipy/billiards_break/216/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..299cd284724f95a24c6529db9e80b9b577d072b0 --- /dev/null +++ b/scipy/billiards_break/216/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9161887993132826; e_wall = 0.8680123474938282; mu = 0.014990234800571628 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = 0.08733989447933123; vx_cue0 = 7.829457171733064; vy_cue0 = -0.11209461932828689 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/216/video.mp4 b/scipy/billiards_break/216/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..cd2073d765f5a04767c3597ba99e9af86f7672f5 --- /dev/null +++ b/scipy/billiards_break/216/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:58d56bbd047fe6bd9409aaad020de9ff7e859306a07f6ab4cbedec5cf85ff980 +size 49718 diff --git a/scipy/billiards_break/217/cot.json b/scipy/billiards_break/217/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..5517a7d8a21eb7041c11f65a2a11f932b818226d --- /dev/null +++ b/scipy/billiards_break/217/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (7.73, -0.17) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0099) decelerates each ball; ball-ball restitution e_ball=0.94, ball-cushion restitution e_wall=0.94.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 7.737 m/s; deceleration ~ mu \\cdot g = 0.0970 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 7.737, + "deceleration_mug_m_s2": 0.097, + "stop_time_no_collision_s": 79.754 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.187 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9449642037429545, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.9408780419379739, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.009898688349905982, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": -0.02009212952646608, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 7.734920567977893, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": -0.16522229051701529, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/217/params.json b/scipy/billiards_break/217/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b544c9c9b9df945a43dce1ea73120592bb0dd738 --- /dev/null +++ b/scipy/billiards_break/217/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9449642037429545, + "e_wall": 0.9408780419379739, + "mu": 0.009898688349905982, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": -0.02009212952646608, + "vx_cue0": 7.734920567977893, + "vy_cue0": -0.16522229051701529, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/217/simulation_code.py b/scipy/billiards_break/217/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c44a9cbbadbd282d063b1b422c8614c410a19ace --- /dev/null +++ b/scipy/billiards_break/217/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9449642037429545; e_wall = 0.9408780419379739; mu = 0.009898688349905982 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = -0.02009212952646608; vx_cue0 = 7.734920567977893; vy_cue0 = -0.16522229051701529 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/217/video.mp4 b/scipy/billiards_break/217/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8ac7fc35c9b725a999cd4b93db03df4862231a3b --- /dev/null +++ b/scipy/billiards_break/217/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:af9386a5614d4d39f10b002d8ddcd3d76bf0565c964371680b3cb20eb35a298c +size 66030 diff --git a/scipy/billiards_break/252/cot.json b/scipy/billiards_break/252/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..dd668c2d05f3fe4115ded728ddac6b89a0c1f7c8 --- /dev/null +++ b/scipy/billiards_break/252/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (8.43, 0.10) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0115) decelerates each ball; ball-ball restitution e_ball=0.93, ball-cushion restitution e_wall=0.89.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 8.427 m/s; deceleration ~ mu \\cdot g = 0.1122 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 8.427, + "deceleration_mug_m_s2": 0.1122, + "stop_time_no_collision_s": 75.071 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.172 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.932664038353717, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.8869053912574444, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.011453875364478094, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": 0.020438297993424878, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 8.426019767251333, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": 0.09907836807153192, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/252/params.json b/scipy/billiards_break/252/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5f3b8b6499303a80a017f184b5da14d1dc177da4 --- /dev/null +++ b/scipy/billiards_break/252/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.932664038353717, + "e_wall": 0.8869053912574444, + "mu": 0.011453875364478094, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": 0.020438297993424878, + "vx_cue0": 8.426019767251333, + "vy_cue0": 0.09907836807153192, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/252/simulation_code.py b/scipy/billiards_break/252/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..560a03d1398aeaa6b3d7a4114af2a2c390cceff1 --- /dev/null +++ b/scipy/billiards_break/252/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.932664038353717; e_wall = 0.8869053912574444; mu = 0.011453875364478094 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = 0.020438297993424878; vx_cue0 = 8.426019767251333; vy_cue0 = 0.09907836807153192 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/252/video.mp4 b/scipy/billiards_break/252/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1cbc20ae7ae6e9fb1c2ed0564df6d5395c5d4f1e --- /dev/null +++ b/scipy/billiards_break/252/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:15971efe879d1523718e55d92ebd1cfd0ec82773d060a9e8f10f736eb7f99c26 +size 51949 diff --git a/scipy/billiards_break/271/cot.json b/scipy/billiards_break/271/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..53ed895f3125768ce8522f00dd275b9e5b356518 --- /dev/null +++ b/scipy/billiards_break/271/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (8.28, -0.00) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0115) decelerates each ball; ball-ball restitution e_ball=0.96, ball-cushion restitution e_wall=0.88.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 8.284 m/s; deceleration ~ mu \\cdot g = 0.1124 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 8.284, + "deceleration_mug_m_s2": 0.1124, + "stop_time_no_collision_s": 73.728 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.175 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9563815953853565, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.8816904479959439, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.011465067531445425, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": -0.01482203220312682, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 8.28388421975059, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": -0.0011387470647850062, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/271/params.json b/scipy/billiards_break/271/params.json new file mode 100644 index 0000000000000000000000000000000000000000..14605c3e5c8bfeb3ad67e816e059011f5c33e8f4 --- /dev/null +++ b/scipy/billiards_break/271/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9563815953853565, + "e_wall": 0.8816904479959439, + "mu": 0.011465067531445425, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": -0.01482203220312682, + "vx_cue0": 8.28388421975059, + "vy_cue0": -0.0011387470647850062, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/271/simulation_code.py b/scipy/billiards_break/271/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e9b2abf4d05229ca0482adeca39530d3509ec3db --- /dev/null +++ b/scipy/billiards_break/271/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9563815953853565; e_wall = 0.8816904479959439; mu = 0.011465067531445425 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = -0.01482203220312682; vx_cue0 = 8.28388421975059; vy_cue0 = -0.0011387470647850062 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/271/video.mp4 b/scipy/billiards_break/271/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..c6229937d2f2149962833ee1107a8895debfc266 --- /dev/null +++ b/scipy/billiards_break/271/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:69ef2ded0a3eb75b777253fd043324a92ca343586ebd52753080dc0a55f996e4 +size 56013 diff --git a/scipy/billiards_break/274/cot.json b/scipy/billiards_break/274/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..2cbe571fe0ea37e37d0778980cf1bd78e3244a13 --- /dev/null +++ b/scipy/billiards_break/274/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (8.71, 0.05) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0092) decelerates each ball; ball-ball restitution e_ball=0.96, ball-cushion restitution e_wall=0.87.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 8.709 m/s; deceleration ~ mu \\cdot g = 0.0901 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 8.709, + "deceleration_mug_m_s2": 0.0901, + "stop_time_no_collision_s": 96.714 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.166 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9592363640008033, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.8702493465879193, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.00918889904283222, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": -0.0013595684164175517, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 8.709032721923109, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": 0.0507166083277944, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/274/params.json b/scipy/billiards_break/274/params.json new file mode 100644 index 0000000000000000000000000000000000000000..da92b54d2289894cc453075a88c340d2ab7ee403 --- /dev/null +++ b/scipy/billiards_break/274/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9592363640008033, + "e_wall": 0.8702493465879193, + "mu": 0.00918889904283222, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": -0.0013595684164175517, + "vx_cue0": 8.709032721923109, + "vy_cue0": 0.0507166083277944, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/274/simulation_code.py b/scipy/billiards_break/274/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a771eaab5e1e14f6d36dfae538ed46b0a9e89bf3 --- /dev/null +++ b/scipy/billiards_break/274/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9592363640008033; e_wall = 0.8702493465879193; mu = 0.00918889904283222 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = -0.0013595684164175517; vx_cue0 = 8.709032721923109; vy_cue0 = 0.0507166083277944 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/274/video.mp4 b/scipy/billiards_break/274/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..998f661a1447293b2392aad63d8d25ef75696440 --- /dev/null +++ b/scipy/billiards_break/274/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d556aef0666a4a46e6a797f79da305908e2d1156b496056dd9ea4337dcee7548 +size 57002 diff --git a/scipy/billiards_break/322/cot.json b/scipy/billiards_break/322/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..32dc932f93c305d092216196f352540e1024dd2a --- /dev/null +++ b/scipy/billiards_break/322/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (8.21, -0.05) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0119) decelerates each ball; ball-ball restitution e_ball=0.93, ball-cushion restitution e_wall=0.91.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 8.215 m/s; deceleration ~ mu \\cdot g = 0.1166 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 8.215, + "deceleration_mug_m_s2": 0.1166, + "stop_time_no_collision_s": 70.446 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.177 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.930513984451745, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.905626855663914, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.011899497603494926, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": -0.04415556662708109, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 8.214919393530337, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": -0.053164813863491894, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/322/params.json b/scipy/billiards_break/322/params.json new file mode 100644 index 0000000000000000000000000000000000000000..6a9c05d8f43599383d014c8ca1e402e9dc7a36eb --- /dev/null +++ b/scipy/billiards_break/322/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.930513984451745, + "e_wall": 0.905626855663914, + "mu": 0.011899497603494926, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": -0.04415556662708109, + "vx_cue0": 8.214919393530337, + "vy_cue0": -0.053164813863491894, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/322/simulation_code.py b/scipy/billiards_break/322/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c556d8ac859c916db9e76af99a0c5eac2944fb59 --- /dev/null +++ b/scipy/billiards_break/322/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.930513984451745; e_wall = 0.905626855663914; mu = 0.011899497603494926 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = -0.04415556662708109; vx_cue0 = 8.214919393530337; vy_cue0 = -0.053164813863491894 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/322/video.mp4 b/scipy/billiards_break/322/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..44a177660fe4b86f4db89e9828e354cb35bfad7b --- /dev/null +++ b/scipy/billiards_break/322/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:64fa1279a0886e223e6191ce081057d8e02637763e993916a61a517215d27789 +size 66306 diff --git a/scipy/billiards_break/405/cot.json b/scipy/billiards_break/405/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..4f4dbc19d8c7e242631fcb179aad5667a74a54a9 --- /dev/null +++ b/scipy/billiards_break/405/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (7.82, 0.18) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0101) decelerates each ball; ball-ball restitution e_ball=0.95, ball-cushion restitution e_wall=0.91.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 7.821 m/s; deceleration ~ mu \\cdot g = 0.0985 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 7.821, + "deceleration_mug_m_s2": 0.0985, + "stop_time_no_collision_s": 79.392 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.185 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9533918199982104, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.9067173508162867, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.010052066011243616, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": 0.02017157404733888, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 7.81888889675984, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": 0.17821254832346195, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/405/params.json b/scipy/billiards_break/405/params.json new file mode 100644 index 0000000000000000000000000000000000000000..009f080326d38e19e90094cad453e7301ac630da --- /dev/null +++ b/scipy/billiards_break/405/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9533918199982104, + "e_wall": 0.9067173508162867, + "mu": 0.010052066011243616, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": 0.02017157404733888, + "vx_cue0": 7.81888889675984, + "vy_cue0": 0.17821254832346195, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/405/simulation_code.py b/scipy/billiards_break/405/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..673ae354dd91e6b7ef2198d912e1ec34cb689325 --- /dev/null +++ b/scipy/billiards_break/405/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9533918199982104; e_wall = 0.9067173508162867; mu = 0.010052066011243616 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = 0.02017157404733888; vx_cue0 = 7.81888889675984; vy_cue0 = 0.17821254832346195 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/405/video.mp4 b/scipy/billiards_break/405/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..00805c86b0e4e5392757aec45754214f32901a7b --- /dev/null +++ b/scipy/billiards_break/405/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e019518c1e17f2598c61029ec774005f06930069b02b167c9e78da6df21268a1 +size 56545 diff --git a/scipy/billiards_break/469/cot.json b/scipy/billiards_break/469/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..1cfc364bb38e82278eea9a28e48af634202b4e79 --- /dev/null +++ b/scipy/billiards_break/469/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (6.56, 0.11) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0159) decelerates each ball; ball-ball restitution e_ball=0.92, ball-cushion restitution e_wall=0.91.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 6.563 m/s; deceleration ~ mu \\cdot g = 0.1560 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 6.563, + "deceleration_mug_m_s2": 0.156, + "stop_time_no_collision_s": 42.06 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.221 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9221127020863715, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.9117517726571535, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.01592182263397802, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": 0.016013962590806133, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 6.5618957889311265, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": 0.11196444822152363, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/469/params.json b/scipy/billiards_break/469/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5f7beb8cd2a8ec19eadeae0040de19651d8da9b8 --- /dev/null +++ b/scipy/billiards_break/469/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9221127020863715, + "e_wall": 0.9117517726571535, + "mu": 0.01592182263397802, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": 0.016013962590806133, + "vx_cue0": 6.5618957889311265, + "vy_cue0": 0.11196444822152363, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/469/simulation_code.py b/scipy/billiards_break/469/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..3041e842adb1d5e7702c1aec8bc4bba6fdfebf19 --- /dev/null +++ b/scipy/billiards_break/469/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9221127020863715; e_wall = 0.9117517726571535; mu = 0.01592182263397802 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = 0.016013962590806133; vx_cue0 = 6.5618957889311265; vy_cue0 = 0.11196444822152363 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/469/video.mp4 b/scipy/billiards_break/469/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..6ab2e09c660a4e06f1936ead47e7bcb6f73635ea --- /dev/null +++ b/scipy/billiards_break/469/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b079858c605e082336fe33137764fbf02f1c5f8f312392674f23cb698a041b82 +size 41858 diff --git a/scipy/billiards_break/511/cot.json b/scipy/billiards_break/511/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..57f6a61281694817cd4805c89bf10da4e5fc06ab --- /dev/null +++ b/scipy/billiards_break/511/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (8.67, 0.12) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0133) decelerates each ball; ball-ball restitution e_ball=0.91, ball-cushion restitution e_wall=0.94.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 8.666 m/s; deceleration ~ mu \\cdot g = 0.1306 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 8.666, + "deceleration_mug_m_s2": 0.1306, + "stop_time_no_collision_s": 66.332 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.167 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9102993226573458, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.9415191635402498, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.013331332073164368, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": -0.0119247672066074, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 8.66521630419374, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": 0.12366811863690041, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/511/params.json b/scipy/billiards_break/511/params.json new file mode 100644 index 0000000000000000000000000000000000000000..a036550ab3e3be66b49fcf9050c25c14900bd351 --- /dev/null +++ b/scipy/billiards_break/511/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9102993226573458, + "e_wall": 0.9415191635402498, + "mu": 0.013331332073164368, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": -0.0119247672066074, + "vx_cue0": 8.66521630419374, + "vy_cue0": 0.12366811863690041, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/511/simulation_code.py b/scipy/billiards_break/511/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..bb2327db02d9f5f8b6e39cdaee17d24dbce09bc1 --- /dev/null +++ b/scipy/billiards_break/511/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9102993226573458; e_wall = 0.9415191635402498; mu = 0.013331332073164368 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = -0.0119247672066074; vx_cue0 = 8.66521630419374; vy_cue0 = 0.12366811863690041 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/511/video.mp4 b/scipy/billiards_break/511/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..896402f0b8704ff51e9ab75706a222f52e7e0e09 --- /dev/null +++ b/scipy/billiards_break/511/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:853ababfcb9b45f16aa097ca9c96ccd1cd6dc44e43bac4cbb5687f30e401884f +size 51246 diff --git a/scipy/billiards_break/551/cot.json b/scipy/billiards_break/551/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..9414d122728f0224cd4bdb88f5d5c028f6da682f --- /dev/null +++ b/scipy/billiards_break/551/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (7.16, -0.14) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0141) decelerates each ball; ball-ball restitution e_ball=0.95, ball-cushion restitution e_wall=0.86.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 7.160 m/s; deceleration ~ mu \\cdot g = 0.1382 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 7.16, + "deceleration_mug_m_s2": 0.1382, + "stop_time_no_collision_s": 51.818 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.203 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9493759017733747, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.8637761954443647, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.01409930245044126, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": 0.0705764886916223, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 7.158558704574885, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": -0.13632495557245222, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/551/params.json b/scipy/billiards_break/551/params.json new file mode 100644 index 0000000000000000000000000000000000000000..22e9fd24ae644660bd6aa3f704c9d1e1858a258d --- /dev/null +++ b/scipy/billiards_break/551/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9493759017733747, + "e_wall": 0.8637761954443647, + "mu": 0.01409930245044126, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": 0.0705764886916223, + "vx_cue0": 7.158558704574885, + "vy_cue0": -0.13632495557245222, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/551/simulation_code.py b/scipy/billiards_break/551/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..6c6387efa1845d3e6c22c940a4b31ea34db6e11b --- /dev/null +++ b/scipy/billiards_break/551/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9493759017733747; e_wall = 0.8637761954443647; mu = 0.01409930245044126 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = 0.0705764886916223; vx_cue0 = 7.158558704574885; vy_cue0 = -0.13632495557245222 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/551/video.mp4 b/scipy/billiards_break/551/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8eab058ee161e5a501025a2761bfd710259c6684 --- /dev/null +++ b/scipy/billiards_break/551/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3f5174202c66ebc4914631aa48138ed22b84d6aff50a9e8544942af66c01cae5 +size 51278 diff --git a/scipy/billiards_break/574/cot.json b/scipy/billiards_break/574/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..2bab55b4e8c8be0182bf45221cf3d010c0e23027 --- /dev/null +++ b/scipy/billiards_break/574/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (6.87, 0.15) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0160) decelerates each ball; ball-ball restitution e_ball=0.92, ball-cushion restitution e_wall=0.94.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 6.874 m/s; deceleration ~ mu \\cdot g = 0.1570 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 6.874, + "deceleration_mug_m_s2": 0.157, + "stop_time_no_collision_s": 43.77 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.211 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9189209066594404, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.9351653636383312, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.016024787666393307, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": -0.00538749757046339, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 6.872155745654876, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": 0.14600795658891247, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/574/params.json b/scipy/billiards_break/574/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5efafbccf108018ca79482b5023959ace24f8374 --- /dev/null +++ b/scipy/billiards_break/574/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9189209066594404, + "e_wall": 0.9351653636383312, + "mu": 0.016024787666393307, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": -0.00538749757046339, + "vx_cue0": 6.872155745654876, + "vy_cue0": 0.14600795658891247, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/574/simulation_code.py b/scipy/billiards_break/574/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..70bb624980b034021175856445932d504de0a135 --- /dev/null +++ b/scipy/billiards_break/574/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9189209066594404; e_wall = 0.9351653636383312; mu = 0.016024787666393307 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = -0.00538749757046339; vx_cue0 = 6.872155745654876; vy_cue0 = 0.14600795658891247 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/574/video.mp4 b/scipy/billiards_break/574/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..25d866fec8e1af37bfdee844788db17ab9c2686c --- /dev/null +++ b/scipy/billiards_break/574/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5a9ccecabf0c72632dcaf33d9d4f31bb14f3effbd7ef4ed3e92fbc65134b051a +size 45344 diff --git a/scipy/billiards_break/580/cot.json b/scipy/billiards_break/580/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..d3719141b51e9af92b92051f95360e084de3e39e --- /dev/null +++ b/scipy/billiards_break/580/cot.json @@ -0,0 +1,144 @@ +{ + "simulation": "billiards_break", + "physical_observation": "Top-down 2D view of a flat billiards table (no pockets) with 4 cushions. 10 stationary balls in a triangular rack and one cue ball arriving at (7.87, -0.01) m/s. All 11 balls equal mass m=0.160 kg, radius r=0.057 m. Cloth friction (mu=0.0125) decelerates each ball; ball-ball restitution e_ball=0.96, ball-cushion restitution e_wall=0.86.", + "physical_law": { + "name": "Newton's 2nd law + impulsive collision restitution + Coulomb dry friction", + "statement": "Free-flight under cloth friction; impulsive line-of-centres collisions for both ball-ball and ball-cushion contacts.", + "formula": "\\dot{v} = -mu g v_hat; Jn = -(1+e) m v_rel.n / 2 (equal mass)" + }, + "ode_system": { + "state_variables": [ + "pos[N,2]", + "vel[N,2]" + ], + "equations_latex": [ + "\\dot{v} = -\\mu \\cdot g \\cdot v / |v| if |v|>eps else 0", + "ball-wall: v_n \\to -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) m v_rel.n / 2" + ], + "equations_python": [ + "v_dot = - mu * g * v / |v| if |v|>eps else 0", + "ball-wall: v_n -> -e_wall v_n", + "ball-ball (equal mass): Jn = -(1+e_ball) * m v_rel.n / 2" + ] + }, + "derivation_steps": [ + "Each ball treated as a 2D point of mass m; rotation neglected.", + "Cloth friction: a = -mu \\cdot g \\cdot v/|v|; speed decays linearly to zero with deceleration mu \\cdot g.", + "Ball-cushion: snap to wall, reflect normal velocity component with restitution e_wall.", + "Ball-ball (equal mass): along contact normal n, exchange normal velocity components scaled by (1+e_ball)/2.", + "Cue ball initial speed = 7.870 m/s; deceleration ~ mu \\cdot g = 0.1228 m/s^2." + ], + "physical_constants_derived": { + "cue_speed_m_s": 7.87, + "deceleration_mug_m_s2": 0.1228, + "stop_time_no_collision_s": 64.095 + }, + "numerical_method": { + "solver": "Velocity Verlet, dt=0.001 s", + "tolerance": "Fixed-step; ~12000 steps for 12 s", + "justification": "Robust for many simultaneous contacts; events resolved as instantaneous impulses." + }, + "code_validation": { + "ode_source": "Standard rigid-body collision impulse with Newton restitution.", + "energy_check": "KE strictly decreases; momentum preserved at each binary collision (modulo cushion friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cue ball strikes the rack apex around t = 0.184 s.", + "Energy and momentum cascade through the rack producing many ball-ball collisions.", + "All balls eventually decelerate to rest under cloth friction." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.16, + "unit": "", + "description": "" + }, + "r": { + "value": 0.057, + "unit": "", + "description": "" + }, + "e_ball": { + "value": 0.9604313619822145, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.8561602634346729, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.01252985884897721, + "unit": "", + "description": "" + }, + "x_min": { + "value": -1.27, + "unit": "", + "description": "" + }, + "x_max": { + "value": 1.27, + "unit": "", + "description": "" + }, + "y_min": { + "value": -0.635, + "unit": "", + "description": "" + }, + "y_max": { + "value": 0.635, + "unit": "", + "description": "" + }, + "x_apex": { + "value": 0.5, + "unit": "", + "description": "" + }, + "y_apex": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x_cue0": { + "value": -0.95, + "unit": "", + "description": "" + }, + "y_cue0": { + "value": 0.07850257302211423, + "unit": "", + "description": "" + }, + "vx_cue0": { + "value": 7.870380336031909, + "unit": "", + "description": "" + }, + "vy_cue0": { + "value": -0.0076182135829293285, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "dt": { + "value": 0.001, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/billiards_break/580/params.json b/scipy/billiards_break/580/params.json new file mode 100644 index 0000000000000000000000000000000000000000..19ce02e789e15d9447d2202e0c9a47dcf337e36a --- /dev/null +++ b/scipy/billiards_break/580/params.json @@ -0,0 +1,20 @@ +{ + "g": 9.8, + "m": 0.16, + "r": 0.057, + "e_ball": 0.9604313619822145, + "e_wall": 0.8561602634346729, + "mu": 0.01252985884897721, + "x_min": -1.27, + "x_max": 1.27, + "y_min": -0.635, + "y_max": 0.635, + "x_apex": 0.5, + "y_apex": 0.0, + "x_cue0": -0.95, + "y_cue0": 0.07850257302211423, + "vx_cue0": 7.870380336031909, + "vy_cue0": -0.0076182135829293285, + "duration": 12.0, + "dt": 0.001 +} \ No newline at end of file diff --git a/scipy/billiards_break/580/simulation_code.py b/scipy/billiards_break/580/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..4e10199fcb1f9537aa4ec0588c373d07109e688d --- /dev/null +++ b/scipy/billiards_break/580/simulation_code.py @@ -0,0 +1,144 @@ +"""Billiards break - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m = 0.16; r = 0.057 +e_ball = 0.9604313619822145; e_wall = 0.8561602634346729; mu = 0.01252985884897721 +x_min = -1.27; x_max = 1.27; y_min = -0.635; y_max = 0.635 +x_apex = 0.5; y_apex = 0.0 +x_cue0 = -0.95; y_cue0 = 0.07850257302211423; vx_cue0 = 7.870380336031909; vy_cue0 = -0.0076182135829293285 +duration = 12; dt = 0.001; fps = 30 +v_eps = 1e-3 +N = 11 +pos = np.zeros((N, 2)); vel = np.zeros((N, 2)) +pos[0] = (x_cue0, y_cue0); vel[0] = (vx_cue0, vy_cue0) +dx_row = r * np.sqrt(3.0); nudge = 1e-6 +idx = 1 +for k in range(4): + n_in_row = k + 1 + x_row = x_apex + k * dx_row + k * nudge + for j in range(n_in_row): + y_pos = (j - (n_in_row - 1) / 2.0) * (2.0 * r + nudge) + pos[idx] = (x_row, y_pos); idx += 1 + +n_steps = int(round(duration / dt)) +sample_every = max(1, int(round((1.0 / fps) / dt))) +n_frames = int(round(duration * fps)) + 1 +P_hist = np.zeros((n_frames, N, 2)); P_hist[0] = pos + + +def all_accels(V): + A = np.zeros_like(V) + speeds = np.hypot(V[:, 0], V[:, 1]) + moving = speeds > v_eps + A[moving] = -mu * g * V[moving] / speeds[moving, None] + return A + + +def resolve_walls(P, V): + for i in range(N): + if P[i, 0] < x_min + r: + P[i, 0] = x_min + r + if V[i, 0] < 0.0: V[i, 0] = -e_wall * V[i, 0] + elif P[i, 0] > x_max - r: + P[i, 0] = x_max - r + if V[i, 0] > 0.0: V[i, 0] = -e_wall * V[i, 0] + if P[i, 1] < y_min + r: + P[i, 1] = y_min + r + if V[i, 1] < 0.0: V[i, 1] = -e_wall * V[i, 1] + elif P[i, 1] > y_max - r: + P[i, 1] = y_max - r + if V[i, 1] > 0.0: V[i, 1] = -e_wall * V[i, 1] + + +def resolve_ball_ball(P, V, max_passes=6): + two_r = 2.0 * r + for _ in range(max_passes): + moved = False + for i in range(N): + for j in range(i+1, N): + dx = P[j, 0] - P[i, 0]; dy = P[j, 1] - P[i, 1] + d2 = dx*dx + dy*dy + if d2 >= two_r * two_r: continue + d = np.sqrt(d2) if d2 > 0.0 else 1e-12 + nx, ny = dx / d, dy / d + vrx = V[j, 0] - V[i, 0]; vry = V[j, 1] - V[i, 1] + vrn = vrx * nx + vry * ny + if vrn < 0.0: + Jn = -(1.0 + e_ball) * m * vrn / 2.0 + V[i, 0] -= (Jn / m) * nx; V[i, 1] -= (Jn / m) * ny + V[j, 0] += (Jn / m) * nx; V[j, 1] += (Jn / m) * ny + overlap = two_r - d + if overlap > 0.0: + sx = 0.5 * overlap * nx; sy = 0.5 * overlap * ny + P[i, 0] -= sx; P[i, 1] -= sy + P[j, 0] += sx; P[j, 1] += sy + moved = True + if not moved: break + + +A = all_accels(vel) +frame_idx = 1 +for step in range(1, n_steps + 1): + vel += 0.5 * A * dt + pos += vel * dt + A = all_accels(vel) + vel += 0.5 * A * dt + resolve_walls(pos, vel) + resolve_ball_ball(pos, vel, max_passes=6) + speeds = np.hypot(vel[:, 0], vel[:, 1]) + vel[speeds < v_eps] = 0.0 + A = all_accels(vel) + if step % sample_every == 0 and frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 +while frame_idx < n_frames: + P_hist[frame_idx] = pos; frame_idx += 1 + +margin = 0.10 +x_lo, x_hi = x_min - margin, x_max + margin +y_lo, y_hi = y_min - margin, y_max + margin +fig = plt.figure(figsize=(8.0, 4.0), facecolor="#1a1a2e") +ax = fig.add_axes([0, 0, 1, 1]) +ax.set_facecolor("#0f5132") +ax.set_xlim(x_lo, x_hi); ax.set_ylim(y_lo, y_hi) +ax.set_aspect("equal") +ax.set_xticks([]); ax.set_yticks([]) +for sp in ax.spines.values(): sp.set_visible(False) + +cush_color = "#7c552d"; cush_thick = 0.05 +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min - cush_thick), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_max), + (x_max - x_min) + 2 * cush_thick, cush_thick, + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_min - cush_thick, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) +ax.add_patch(patches.Rectangle((x_max, y_min), + cush_thick, (y_max - y_min), + facecolor=cush_color, edgecolor="none", zorder=1)) + +rack_colors = ["#f4d35e", "#1d3557", "#e63946", "#6a0572", "#f08a4b", + "#2a9d8f", "#9d2c2c", "#222222", "#ffd166", "#118ab2"] +cue = patches.Circle(P_hist[0, 0], r, facecolor="white", + edgecolor="#dddddd", lw=0.8, zorder=4) +ax.add_patch(cue) +ball_patches = [cue] +for k in range(10): + bp = patches.Circle(P_hist[0, k+1], r, facecolor=rack_colors[k], + edgecolor="white", lw=0.8, zorder=4) + ax.add_patch(bp); ball_patches.append(bp) + +def update(f): + for i, bp in enumerate(ball_patches): + bp.center = (float(P_hist[f, i, 0]), float(P_hist[f, i, 1])) + return ball_patches + +ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=2400), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/billiards_break/580/video.mp4 b/scipy/billiards_break/580/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e318fa02800e9447a5e8534e955dc5a2e97a97cb --- /dev/null +++ b/scipy/billiards_break/580/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a4a45cfd44707cee241c1da9303c626850976c8a1d6b2a8c3cf7877bfec54e5e +size 49730 diff --git a/scipy/block_belt_spring_stickslip/024/cot.json b/scipy/block_belt_spring_stickslip/024/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..06f1e85c0280fa4246c9677861eba4c977c8a3c7 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/024/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=0.798 kg) on a conveyor belt moving at v_belt=0.247 m/s (in -x), tethered by a Hookean spring (k=11.196 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.474 > mu_k=0.293 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 3.7466 rad/s; cycle period ~ 2.4343 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.3312 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.2048 m with omega = \\sqrt(k/m) = 3.7466 rad/s.", + "u(0) = x_break - x_new = -0.1265 m, u'(0) = -v_belt = -0.2474 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.0954 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 3.7466, + "natural_period_s": 1.677, + "stick_duration_s": 1.3389, + "slip_duration_s": 1.0954, + "cycle_period_s": 2.4343, + "n_cycles_in_duration": 4.93 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.247 m/s during stick (~1.339 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.095 s).", + "Cycle repeats with period 2.434 s; ~4.9 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.7975975419106518, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 11.196122553777794, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.4744249390673707, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.29328905834999314, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.24738100995993423, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/024/params.json b/scipy/block_belt_spring_stickslip/024/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e8a1a8180ddc732f0078cb366f8c2572b521b9ee --- /dev/null +++ b/scipy/block_belt_spring_stickslip/024/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 0.7975975419106518, + "s": 0.2, + "k": 11.196122553777794, + "L_s": 0.5, + "mu_s": 0.4744249390673707, + "mu_k": 0.29328905834999314, + "v_belt": 0.24738100995993423, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/024/simulation_code.py b/scipy/block_belt_spring_stickslip/024/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..2bf5177042bbd00f08a0863d1606a3bf9a0a2979 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/024/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 0.7975975419106518; s = 0.2; k = 11.196122553777794 +L_s = 0.5; mu_s = 0.4744249390673707; mu_k = 0.29328905834999314 +v_belt = 0.24738100995993423; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/024/video.mp4 b/scipy/block_belt_spring_stickslip/024/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..9cd67051288f3782984bf217a12aa26f2aab9b28 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/024/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b09a5c77ed4b509155731014d25dfafcd5e85558ac2a76f592755846c0646e83 +size 33899 diff --git a/scipy/block_belt_spring_stickslip/046/cot.json b/scipy/block_belt_spring_stickslip/046/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..4b33a2805b96c3db2b549eb3f6012e1f9ebdcae4 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/046/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=1.222 kg) on a conveyor belt moving at v_belt=0.184 m/s (in -x), tethered by a Hookean spring (k=8.993 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.381 > mu_k=0.221 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 2.7131 rad/s; cycle period ~ 4.1433 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.5067 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.2948 m with omega = \\sqrt(k/m) = 2.7131 rad/s.", + "u(0) = x_break - x_new = -0.2119 m, u'(0) = -v_belt = -0.1838 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.3860 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 2.7131, + "natural_period_s": 2.3159, + "stick_duration_s": 2.7573, + "slip_duration_s": 1.386, + "cycle_period_s": 4.1433, + "n_cycles_in_duration": 2.9 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.184 m/s during stick (~2.757 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.386 s).", + "Cycle repeats with period 4.143 s; ~2.9 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 1.2217020937667193, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 8.992788018444585, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.3805818217484346, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.22142555873364708, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.18376301754381807, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/046/params.json b/scipy/block_belt_spring_stickslip/046/params.json new file mode 100644 index 0000000000000000000000000000000000000000..86006cae0343850a97e4e9661a3d9242f5b68c30 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/046/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 1.2217020937667193, + "s": 0.2, + "k": 8.992788018444585, + "L_s": 0.5, + "mu_s": 0.3805818217484346, + "mu_k": 0.22142555873364708, + "v_belt": 0.18376301754381807, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/046/simulation_code.py b/scipy/block_belt_spring_stickslip/046/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c099018b7279d91408da3994f276d75bf63a42d0 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/046/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 1.2217020937667193; s = 0.2; k = 8.992788018444585 +L_s = 0.5; mu_s = 0.3805818217484346; mu_k = 0.22142555873364708 +v_belt = 0.18376301754381807; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/046/video.mp4 b/scipy/block_belt_spring_stickslip/046/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..5901b35114c3630df455dbf555385d72a0e16570 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/046/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0ca160ec2386f09ed14742d315393ef9b15c494eeb0a06f6476430e816ecd66c +size 31214 diff --git a/scipy/block_belt_spring_stickslip/070/cot.json b/scipy/block_belt_spring_stickslip/070/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..f340b236a6169f3096b48d7b1e04e87941d1cb7e --- /dev/null +++ b/scipy/block_belt_spring_stickslip/070/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=1.365 kg) on a conveyor belt moving at v_belt=0.161 m/s (in -x), tethered by a Hookean spring (k=11.386 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.429 > mu_k=0.280 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 2.8879 rad/s; cycle period ~ 4.4413 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.5046 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.3290 m with omega = \\sqrt(k/m) = 2.8879 rad/s.", + "u(0) = x_break - x_new = -0.1757 m, u'(0) = -v_belt = -0.1607 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.3002 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 2.8879, + "natural_period_s": 2.1757, + "stick_duration_s": 3.141, + "slip_duration_s": 1.3002, + "cycle_period_s": 4.4413, + "n_cycles_in_duration": 2.7 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.161 m/s during stick (~3.141 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.300 s).", + "Cycle repeats with period 4.441 s; ~2.7 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 1.3652381179101234, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 11.386181622371353, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.4294540796493419, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.27995836649748707, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.1606574366760122, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/070/params.json b/scipy/block_belt_spring_stickslip/070/params.json new file mode 100644 index 0000000000000000000000000000000000000000..bc70cb898a5bed04f8309273fd0863f1ca93cbd8 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/070/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 1.3652381179101234, + "s": 0.2, + "k": 11.386181622371353, + "L_s": 0.5, + "mu_s": 0.4294540796493419, + "mu_k": 0.27995836649748707, + "v_belt": 0.1606574366760122, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/070/simulation_code.py b/scipy/block_belt_spring_stickslip/070/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..7d948ab827d5c0b17ac560b8d43d59a7a874fe4a --- /dev/null +++ b/scipy/block_belt_spring_stickslip/070/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 1.3652381179101234; s = 0.2; k = 11.386181622371353 +L_s = 0.5; mu_s = 0.4294540796493419; mu_k = 0.27995836649748707 +v_belt = 0.1606574366760122; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/070/video.mp4 b/scipy/block_belt_spring_stickslip/070/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..64df9151eee0bdaa8a40dbccaff10adaa4b72056 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/070/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:091f612b8a9296d0194e2863f0c6cfa9ad6b602cdab1249bb80a315becda527e +size 31566 diff --git a/scipy/block_belt_spring_stickslip/112/cot.json b/scipy/block_belt_spring_stickslip/112/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..37d1872cbd3ba3b7f453a99516e85b5d3cd132b0 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/112/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=1.221 kg) on a conveyor belt moving at v_belt=0.185 m/s (in -x), tethered by a Hookean spring (k=12.497 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.477 > mu_k=0.302 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 3.1995 rad/s; cycle period ~ 3.6565 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.4570 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.2893 m with omega = \\sqrt(k/m) = 3.1995 rad/s.", + "u(0) = x_break - x_new = -0.1676 m, u'(0) = -v_belt = -0.1853 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.1898 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 3.1995, + "natural_period_s": 1.9638, + "stick_duration_s": 2.4667, + "slip_duration_s": 1.1898, + "cycle_period_s": 3.6565, + "n_cycles_in_duration": 3.28 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.185 m/s during stick (~2.467 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.190 s).", + "Cycle repeats with period 3.656 s; ~3.3 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 1.220791335330214, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 12.497203137643401, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.47733820214522005, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.30221400846429464, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.1852523056732403, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/112/params.json b/scipy/block_belt_spring_stickslip/112/params.json new file mode 100644 index 0000000000000000000000000000000000000000..ec4118446049e1591a94fcc577617f9d2a784c04 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/112/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 1.220791335330214, + "s": 0.2, + "k": 12.497203137643401, + "L_s": 0.5, + "mu_s": 0.47733820214522005, + "mu_k": 0.30221400846429464, + "v_belt": 0.1852523056732403, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/112/simulation_code.py b/scipy/block_belt_spring_stickslip/112/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e0f0434f578eb19072537232d84d6e525747b695 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/112/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 1.220791335330214; s = 0.2; k = 12.497203137643401 +L_s = 0.5; mu_s = 0.47733820214522005; mu_k = 0.30221400846429464 +v_belt = 0.1852523056732403; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/112/video.mp4 b/scipy/block_belt_spring_stickslip/112/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..ce055d1c7f2a73af1df4206952047f577b039a15 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/112/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:01b9c40243a292a98cccf5d6c60b52f1d301a92d285d636b1f946c8263808809 +size 31908 diff --git a/scipy/block_belt_spring_stickslip/134/cot.json b/scipy/block_belt_spring_stickslip/134/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..db3155a71c33823867f3c750fd80d5c85de465b5 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/134/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=0.912 kg) on a conveyor belt moving at v_belt=0.155 m/s (in -x), tethered by a Hookean spring (k=11.381 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.393 > mu_k=0.211 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 3.5322 rad/s; cycle period ~ 3.0478 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.3089 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.1659 m with omega = \\sqrt(k/m) = 3.5322 rad/s.", + "u(0) = x_break - x_new = -0.1430 m, u'(0) = -v_belt = -0.1553 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.0583 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 3.5322, + "natural_period_s": 1.7788, + "stick_duration_s": 1.9895, + "slip_duration_s": 1.0583, + "cycle_period_s": 3.0478, + "n_cycles_in_duration": 3.94 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.155 m/s during stick (~1.990 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.058 s).", + "Cycle repeats with period 3.048 s; ~3.9 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.9121733263772925, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 11.380747084017036, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.393272625972311, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.2112386074860794, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.1552673085226321, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/134/params.json b/scipy/block_belt_spring_stickslip/134/params.json new file mode 100644 index 0000000000000000000000000000000000000000..050fe74f2f433c63bcb50b7e996ea70c080332f5 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/134/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 0.9121733263772925, + "s": 0.2, + "k": 11.380747084017036, + "L_s": 0.5, + "mu_s": 0.393272625972311, + "mu_k": 0.2112386074860794, + "v_belt": 0.1552673085226321, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/134/simulation_code.py b/scipy/block_belt_spring_stickslip/134/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..05dedceb50cafd23d5872a09ef2717d6e065d7a7 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/134/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 0.9121733263772925; s = 0.2; k = 11.380747084017036 +L_s = 0.5; mu_s = 0.393272625972311; mu_k = 0.2112386074860794 +v_belt = 0.1552673085226321; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/134/video.mp4 b/scipy/block_belt_spring_stickslip/134/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..cf7caa896991c6fc17185bf958c994588750c478 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/134/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2876115adea74c09110a6fc15cb730b373bc1da85b496b803e1569e54cdf4a90 +size 30368 diff --git a/scipy/block_belt_spring_stickslip/185/cot.json b/scipy/block_belt_spring_stickslip/185/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..986e340c246f0cfcd8db68b63de8e2600999fb4f --- /dev/null +++ b/scipy/block_belt_spring_stickslip/185/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=1.248 kg) on a conveyor belt moving at v_belt=0.252 m/s (in -x), tethered by a Hookean spring (k=11.757 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.416 > mu_k=0.219 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 3.0692 rad/s; cycle period ~ 2.9906 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.4326 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.2281 m with omega = \\sqrt(k/m) = 3.0692 rad/s.", + "u(0) = x_break - x_new = -0.2045 m, u'(0) = -v_belt = -0.2518 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.2722 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 3.0692, + "natural_period_s": 2.0472, + "stick_duration_s": 1.7184, + "slip_duration_s": 1.2722, + "cycle_period_s": 2.9906, + "n_cycles_in_duration": 4.01 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.252 m/s during stick (~1.718 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.272 s).", + "Cycle repeats with period 2.991 s; ~4.0 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 1.248094509123895, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 11.756860273809757, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.4158317213528916, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.21929266765058267, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.2517569584017613, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/185/params.json b/scipy/block_belt_spring_stickslip/185/params.json new file mode 100644 index 0000000000000000000000000000000000000000..ab530f1b020950aedc87e311171241f8b0d1e42e --- /dev/null +++ b/scipy/block_belt_spring_stickslip/185/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 1.248094509123895, + "s": 0.2, + "k": 11.756860273809757, + "L_s": 0.5, + "mu_s": 0.4158317213528916, + "mu_k": 0.21929266765058267, + "v_belt": 0.2517569584017613, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/185/simulation_code.py b/scipy/block_belt_spring_stickslip/185/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..fb36726daf989f1f17849972a2a7f1538e096aac --- /dev/null +++ b/scipy/block_belt_spring_stickslip/185/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 1.248094509123895; s = 0.2; k = 11.756860273809757 +L_s = 0.5; mu_s = 0.4158317213528916; mu_k = 0.21929266765058267 +v_belt = 0.2517569584017613; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/185/video.mp4 b/scipy/block_belt_spring_stickslip/185/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..26beba52ff6d31d156e804c3c1dfe2909c43e35f --- /dev/null +++ b/scipy/block_belt_spring_stickslip/185/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e22a81769d36723e6f9214138d582764e34cfd1304cf81e15cbcfd6236d793cf +size 37661 diff --git a/scipy/block_belt_spring_stickslip/204/cot.json b/scipy/block_belt_spring_stickslip/204/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..898ee48a2504935104cefb9d517fab2066b7687e --- /dev/null +++ b/scipy/block_belt_spring_stickslip/204/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=0.823 kg) on a conveyor belt moving at v_belt=0.258 m/s (in -x), tethered by a Hookean spring (k=12.024 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.476 > mu_k=0.210 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 3.8211 rad/s; cycle period ~ 2.2476 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.3193 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.1410 m with omega = \\sqrt(k/m) = 3.8211 rad/s.", + "u(0) = x_break - x_new = -0.1783 m, u'(0) = -v_belt = -0.2584 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.0119 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 3.8211, + "natural_period_s": 1.6443, + "stick_duration_s": 1.2357, + "slip_duration_s": 1.0119, + "cycle_period_s": 2.2476, + "n_cycles_in_duration": 5.34 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.258 m/s during stick (~1.236 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.012 s).", + "Cycle repeats with period 2.248 s; ~5.3 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.8234735661947923, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 12.023534942295434, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.4757086231349468, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.2100421303496794, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.25838183284545385, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/204/params.json b/scipy/block_belt_spring_stickslip/204/params.json new file mode 100644 index 0000000000000000000000000000000000000000..54ba45e53f3bb594c40016282c2c22a5a55cb485 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/204/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 0.8234735661947923, + "s": 0.2, + "k": 12.023534942295434, + "L_s": 0.5, + "mu_s": 0.4757086231349468, + "mu_k": 0.2100421303496794, + "v_belt": 0.25838183284545385, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/204/simulation_code.py b/scipy/block_belt_spring_stickslip/204/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..1dfb2a07d412d0ecbdb7bc8147ee4ae69ff6ee20 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/204/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 0.8234735661947923; s = 0.2; k = 12.023534942295434 +L_s = 0.5; mu_s = 0.4757086231349468; mu_k = 0.2100421303496794 +v_belt = 0.25838183284545385; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/204/video.mp4 b/scipy/block_belt_spring_stickslip/204/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..daa8ad95aba3e630f244574a4fff6ab090d5d2e4 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/204/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3f8637142dc59d35028f160747c3b2ce43e3439202f54c6562c6bffa4b8faa35 +size 35687 diff --git a/scipy/block_belt_spring_stickslip/219/cot.json b/scipy/block_belt_spring_stickslip/219/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..8dac0839b3ae7a6fb668482097702fd44b78fd31 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/219/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=0.767 kg) on a conveyor belt moving at v_belt=0.239 m/s (in -x), tethered by a Hookean spring (k=10.323 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.431 > mu_k=0.260 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 3.6685 rad/s; cycle period ~ 2.4323 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.3142 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.1893 m with omega = \\sqrt(k/m) = 3.6685 rad/s.", + "u(0) = x_break - x_new = -0.1249 m, u'(0) = -v_belt = -0.2392 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.1187 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 3.6685, + "natural_period_s": 1.7128, + "stick_duration_s": 1.3136, + "slip_duration_s": 1.1187, + "cycle_period_s": 2.4323, + "n_cycles_in_duration": 4.93 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.239 m/s during stick (~1.314 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.119 s).", + "Cycle repeats with period 2.432 s; ~4.9 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.7670545599039175, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 10.322783468419825, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.4314665769436801, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.25997935893338125, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.239196646973465, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/219/params.json b/scipy/block_belt_spring_stickslip/219/params.json new file mode 100644 index 0000000000000000000000000000000000000000..7701fa3d1b3a2c4d6774b5976eaf27e693fe281e --- /dev/null +++ b/scipy/block_belt_spring_stickslip/219/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 0.7670545599039175, + "s": 0.2, + "k": 10.322783468419825, + "L_s": 0.5, + "mu_s": 0.4314665769436801, + "mu_k": 0.25997935893338125, + "v_belt": 0.239196646973465, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/219/simulation_code.py b/scipy/block_belt_spring_stickslip/219/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a1ee03aab9115e7108fc7daa90c628705013a8bd --- /dev/null +++ b/scipy/block_belt_spring_stickslip/219/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 0.7670545599039175; s = 0.2; k = 10.322783468419825 +L_s = 0.5; mu_s = 0.4314665769436801; mu_k = 0.25997935893338125 +v_belt = 0.239196646973465; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/219/video.mp4 b/scipy/block_belt_spring_stickslip/219/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..b445d803fad02312121797cc17d809a54e8fe6e9 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/219/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3ba47b6f571d8b24c0739668967dbbd9a9c06680febc13f4020b6ff4d314d75c +size 34681 diff --git a/scipy/block_belt_spring_stickslip/252/cot.json b/scipy/block_belt_spring_stickslip/252/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..371c3c53585f28db2b06229beae49a65fbdc813c --- /dev/null +++ b/scipy/block_belt_spring_stickslip/252/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=0.807 kg) on a conveyor belt moving at v_belt=0.165 m/s (in -x), tethered by a Hookean spring (k=9.519 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.411 > mu_k=0.311 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 3.4347 rad/s; cycle period ~ 3.2844 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.3415 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.2579 m with omega = \\sqrt(k/m) = 3.4347 rad/s.", + "u(0) = x_break - x_new = -0.0836 m, u'(0) = -v_belt = -0.1654 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.2189 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 3.4347, + "natural_period_s": 1.8293, + "stick_duration_s": 2.0654, + "slip_duration_s": 1.2189, + "cycle_period_s": 3.2844, + "n_cycles_in_duration": 3.65 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.165 m/s during stick (~2.065 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.219 s).", + "Cycle repeats with period 3.284 s; ~3.7 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.8068395700683968, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 9.518549115182793, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.41114021738613116, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.3105150129801122, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.16535537936867992, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/252/params.json b/scipy/block_belt_spring_stickslip/252/params.json new file mode 100644 index 0000000000000000000000000000000000000000..14f222b453173f30b63a020262a755d35e40e377 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/252/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 0.8068395700683968, + "s": 0.2, + "k": 9.518549115182793, + "L_s": 0.5, + "mu_s": 0.41114021738613116, + "mu_k": 0.3105150129801122, + "v_belt": 0.16535537936867992, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/252/simulation_code.py b/scipy/block_belt_spring_stickslip/252/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..9a164a3dea335217619959479f0bc6af0c56e7f6 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/252/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 0.8068395700683968; s = 0.2; k = 9.518549115182793 +L_s = 0.5; mu_s = 0.41114021738613116; mu_k = 0.3105150129801122 +v_belt = 0.16535537936867992; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/252/video.mp4 b/scipy/block_belt_spring_stickslip/252/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3ba0526aebce55e891369878d56dd45a36c8b930 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/252/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d9ceb3297cad029f4da85d9815ec164d491528459efa75b0b7e24a3b349170a5 +size 30177 diff --git a/scipy/block_belt_spring_stickslip/260/cot.json b/scipy/block_belt_spring_stickslip/260/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..daf1fe5a637320ef1f319e19c0a1c7ca4086b6df --- /dev/null +++ b/scipy/block_belt_spring_stickslip/260/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=1.000 kg) on a conveyor belt moving at v_belt=0.288 m/s (in -x), tethered by a Hookean spring (k=8.558 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.443 > mu_k=0.219 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 2.9255 rad/s; cycle period ~ 3.0875 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.5071 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.2508 m with omega = \\sqrt(k/m) = 2.9255 rad/s.", + "u(0) = x_break - x_new = -0.2563 m, u'(0) = -v_belt = -0.2876 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.3242 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 2.9255, + "natural_period_s": 2.1477, + "stick_duration_s": 1.7633, + "slip_duration_s": 1.3242, + "cycle_period_s": 3.0875, + "n_cycles_in_duration": 3.89 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.288 m/s during stick (~1.763 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.324 s).", + "Cycle repeats with period 3.087 s; ~3.9 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.9999098265605892, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 8.557776757232988, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.4428916058935773, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.21901838200169155, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.28761363923824235, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/260/params.json b/scipy/block_belt_spring_stickslip/260/params.json new file mode 100644 index 0000000000000000000000000000000000000000..bc2cd7a0b0a648d8651612dbf2c15d27bb5b6b4d --- /dev/null +++ b/scipy/block_belt_spring_stickslip/260/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 0.9999098265605892, + "s": 0.2, + "k": 8.557776757232988, + "L_s": 0.5, + "mu_s": 0.4428916058935773, + "mu_k": 0.21901838200169155, + "v_belt": 0.28761363923824235, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/260/simulation_code.py b/scipy/block_belt_spring_stickslip/260/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..9600e8db5defe22df789aa0f0760d5b94c13f437 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/260/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 0.9999098265605892; s = 0.2; k = 8.557776757232988 +L_s = 0.5; mu_s = 0.4428916058935773; mu_k = 0.21901838200169155 +v_belt = 0.28761363923824235; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/260/video.mp4 b/scipy/block_belt_spring_stickslip/260/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d52b91c181335c73e6b5edbf840731c17aaf8554 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/260/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3c60594590a5301780659f7c5916994a9ffd474c56d4437418effa98fca14956 +size 37905 diff --git a/scipy/block_belt_spring_stickslip/298/cot.json b/scipy/block_belt_spring_stickslip/298/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..062ec039b6fd3a5a3e778e9cf1e674eb8f1550f3 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/298/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=1.197 kg) on a conveyor belt moving at v_belt=0.156 m/s (in -x), tethered by a Hookean spring (k=10.620 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.471 > mu_k=0.214 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 2.9783 rad/s; cycle period ~ 4.5082 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.5209 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.2361 m with omega = \\sqrt(k/m) = 2.9783 rad/s.", + "u(0) = x_break - x_new = -0.2848 m, u'(0) = -v_belt = -0.1564 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.1773 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 2.9783, + "natural_period_s": 2.1096, + "stick_duration_s": 3.3309, + "slip_duration_s": 1.1773, + "cycle_period_s": 4.5082, + "n_cycles_in_duration": 2.66 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.156 m/s during stick (~3.331 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.177 s).", + "Cycle repeats with period 4.508 s; ~2.7 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 1.1972440365709764, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 10.620104213865892, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.47147029441249566, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.2137254832109737, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.1563764207100162, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/298/params.json b/scipy/block_belt_spring_stickslip/298/params.json new file mode 100644 index 0000000000000000000000000000000000000000..f84c451f6e76ec0b842eb8c8ed569733fb5e6ed0 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/298/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 1.1972440365709764, + "s": 0.2, + "k": 10.620104213865892, + "L_s": 0.5, + "mu_s": 0.47147029441249566, + "mu_k": 0.2137254832109737, + "v_belt": 0.1563764207100162, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/298/simulation_code.py b/scipy/block_belt_spring_stickslip/298/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..37ccd12103af4b3e1fa30cb55e3bb17455a833e6 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/298/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 1.1972440365709764; s = 0.2; k = 10.620104213865892 +L_s = 0.5; mu_s = 0.47147029441249566; mu_k = 0.2137254832109737 +v_belt = 0.1563764207100162; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/298/video.mp4 b/scipy/block_belt_spring_stickslip/298/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e76cebc41e768c46ed32142acd44b2f9357c0bbd --- /dev/null +++ b/scipy/block_belt_spring_stickslip/298/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2d8fa8cd6bf577e6add7f111366799e6023c5915af66989b037f503ad76ce39a +size 31010 diff --git a/scipy/block_belt_spring_stickslip/299/cot.json b/scipy/block_belt_spring_stickslip/299/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..91d1b9d54277e89aaefece63d2a75d325f085e4e --- /dev/null +++ b/scipy/block_belt_spring_stickslip/299/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=1.080 kg) on a conveyor belt moving at v_belt=0.154 m/s (in -x), tethered by a Hookean spring (k=13.013 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.353 > mu_k=0.261 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 3.4718 rad/s; cycle period ~ 3.0793 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.2872 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.2120 m with omega = \\sqrt(k/m) = 3.4718 rad/s.", + "u(0) = x_break - x_new = -0.0752 m, u'(0) = -v_belt = -0.1537 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.2117 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 3.4718, + "natural_period_s": 1.8098, + "stick_duration_s": 1.8677, + "slip_duration_s": 1.2117, + "cycle_period_s": 3.0793, + "n_cycles_in_duration": 3.9 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.154 m/s during stick (~1.868 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.212 s).", + "Cycle repeats with period 3.079 s; ~3.9 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0796565235578472, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 13.013400114090116, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.35317779039194186, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.2607472770282492, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.15374815787171583, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/299/params.json b/scipy/block_belt_spring_stickslip/299/params.json new file mode 100644 index 0000000000000000000000000000000000000000..1fbaca383cb5845dce0b4eb2cb74b135fcc55c52 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/299/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 1.0796565235578472, + "s": 0.2, + "k": 13.013400114090116, + "L_s": 0.5, + "mu_s": 0.35317779039194186, + "mu_k": 0.2607472770282492, + "v_belt": 0.15374815787171583, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/299/simulation_code.py b/scipy/block_belt_spring_stickslip/299/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d7977a088555d6e28d5b6ecf9053ba0b129e18b3 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/299/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 1.0796565235578472; s = 0.2; k = 13.013400114090116 +L_s = 0.5; mu_s = 0.35317779039194186; mu_k = 0.2607472770282492 +v_belt = 0.15374815787171583; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/299/video.mp4 b/scipy/block_belt_spring_stickslip/299/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e1997c746f090eac60c9ec1a9ae25892925b6cae --- /dev/null +++ b/scipy/block_belt_spring_stickslip/299/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9c99907a50f89abd6e1c7d60b860c6c5c0fe98b675e228ded1404ace55043f58 +size 29515 diff --git a/scipy/block_belt_spring_stickslip/382/cot.json b/scipy/block_belt_spring_stickslip/382/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..5bf4373d80635349737cc09431f3a4a1e08f8772 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/382/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=0.944 kg) on a conveyor belt moving at v_belt=0.155 m/s (in -x), tethered by a Hookean spring (k=13.049 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.493 > mu_k=0.211 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 3.7177 rad/s; cycle period ~ 3.2063 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.3494 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.1495 m with omega = \\sqrt(k/m) = 3.7177 rad/s.", + "u(0) = x_break - x_new = -0.1999 m, u'(0) = -v_belt = -0.1553 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 0.9558 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 3.7177, + "natural_period_s": 1.6901, + "stick_duration_s": 2.2505, + "slip_duration_s": 0.9558, + "cycle_period_s": 3.2063, + "n_cycles_in_duration": 3.74 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.155 m/s during stick (~2.250 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~0.956 s).", + "Cycle repeats with period 3.206 s; ~3.7 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.9440942263882436, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 13.048834597458164, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.4927725612054587, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.21083971176502053, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.1552531548948315, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/382/params.json b/scipy/block_belt_spring_stickslip/382/params.json new file mode 100644 index 0000000000000000000000000000000000000000..afc767b0309444b2626fa499dbd044737b4b7351 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/382/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 0.9440942263882436, + "s": 0.2, + "k": 13.048834597458164, + "L_s": 0.5, + "mu_s": 0.4927725612054587, + "mu_k": 0.21083971176502053, + "v_belt": 0.1552531548948315, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/382/simulation_code.py b/scipy/block_belt_spring_stickslip/382/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..235a244337730ee2437f0c2b8cb1359eb5e2ecbf --- /dev/null +++ b/scipy/block_belt_spring_stickslip/382/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 0.9440942263882436; s = 0.2; k = 13.048834597458164 +L_s = 0.5; mu_s = 0.4927725612054587; mu_k = 0.21083971176502053 +v_belt = 0.1552531548948315; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/382/video.mp4 b/scipy/block_belt_spring_stickslip/382/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..0feec4695f067594f2bf42c408f9738d5981aa2e --- /dev/null +++ b/scipy/block_belt_spring_stickslip/382/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:341b8ed7646f2287b7fb21f8d56fa0c8e4d059abaf2a899c008a9a69e6c13122 +size 30893 diff --git a/scipy/block_belt_spring_stickslip/386/cot.json b/scipy/block_belt_spring_stickslip/386/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..d2892af807c329607ee860776ddf8d0b470fdc9e --- /dev/null +++ b/scipy/block_belt_spring_stickslip/386/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=1.211 kg) on a conveyor belt moving at v_belt=0.252 m/s (in -x), tethered by a Hookean spring (k=12.014 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.419 > mu_k=0.205 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 3.1497 rad/s; cycle period ~ 2.8725 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.4141 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.2025 m with omega = \\sqrt(k/m) = 3.1497 rad/s.", + "u(0) = x_break - x_new = -0.2116 m, u'(0) = -v_belt = -0.2516 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.2266 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 3.1497, + "natural_period_s": 1.9948, + "stick_duration_s": 1.646, + "slip_duration_s": 1.2266, + "cycle_period_s": 2.8725, + "n_cycles_in_duration": 4.18 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.252 m/s during stick (~1.646 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.227 s).", + "Cycle repeats with period 2.873 s; ~4.2 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 1.2110534318650428, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 12.014428549496088, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.4191940289239461, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.2049581468899018, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.25158453773844375, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/386/params.json b/scipy/block_belt_spring_stickslip/386/params.json new file mode 100644 index 0000000000000000000000000000000000000000..30ead14edd6dc856fad2d81a9b3548ab24fad8fa --- /dev/null +++ b/scipy/block_belt_spring_stickslip/386/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 1.2110534318650428, + "s": 0.2, + "k": 12.014428549496088, + "L_s": 0.5, + "mu_s": 0.4191940289239461, + "mu_k": 0.2049581468899018, + "v_belt": 0.25158453773844375, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/386/simulation_code.py b/scipy/block_belt_spring_stickslip/386/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a73e4e866a00894cd2dceea8765ca4f5b579c3e7 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/386/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 1.2110534318650428; s = 0.2; k = 12.014428549496088 +L_s = 0.5; mu_s = 0.4191940289239461; mu_k = 0.2049581468899018 +v_belt = 0.25158453773844375; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/386/video.mp4 b/scipy/block_belt_spring_stickslip/386/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d849bbb6e88ac5d8afbeb36bdd194e23703e8611 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/386/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f46379ebf70cb278adf601ab614e273ea569db5d299d1995ef4c7f481b78dea3 +size 36441 diff --git a/scipy/block_belt_spring_stickslip/459/cot.json b/scipy/block_belt_spring_stickslip/459/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..694abc5be80accc0c8e360cea4e98cf10e1e9f2f --- /dev/null +++ b/scipy/block_belt_spring_stickslip/459/cot.json @@ -0,0 +1,107 @@ +{ + "simulation": "block_belt_spring_stickslip", + "physical_observation": "A cubic block (m=0.839 kg) on a conveyor belt moving at v_belt=0.282 m/s (in -x), tethered by a Hookean spring (k=13.546 N/m) to a fixed wall at +x. Coulomb friction with mu_s=0.407 > mu_k=0.207 produces stick-slip oscillations.", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb static/kinetic friction", + "statement": "STICK: block carried by belt, static friction balances spring up to mu_s m g. SLIP: m \\ddot{x} = -k(x - x_eq) - mu_k m g sign(v_rel).", + "formula": "omega = \\sqrt(k/m) = 4.0193 rad/s; cycle period ~ 1.9187 s" + }, + "ode_system": { + "state_variables": [ + "x (block centre, m)", + "v = \\dot{x} (m/s)" + ], + "equations_latex": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + \\omega^2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ], + "equations_python": [ + "STICK: x(t) = x_start - v_belt (t - t_start)", + "SLIP: u'' + omega **2 u = 0, where u = x - x_new, x_new = x_eq - mu_k m g / k", + "Re-stick when v = -v_belt and spring force within static-friction bound" + ] + }, + "derivation_steps": [ + "Stick phase: block moves with belt at v_block = -v_belt. Static friction provides whatever horizontal force is needed up to mu_s m g.", + "Stick ends at x_break = x_eq - mu_s m g / k = x_eq - 0.2468 m, when spring force exceeds mu_s m g.", + "Slip phase: SHM about x_new = x_eq - mu_k m g / k = x_eq - 0.1255 m with omega = \\sqrt(k/m) = 4.0193 rad/s.", + "u(0) = x_break - x_new = -0.1213 m, u'(0) = -v_belt = -0.2816 m/s.", + "Re-stick when v_block = -v_belt; closed-form half-angle solution gives slip duration 1.0423 s." + ], + "physical_constants_derived": { + "omega_rad_per_s": 4.0193, + "natural_period_s": 1.5633, + "stick_duration_s": 0.8764, + "slip_duration_s": 1.0423, + "cycle_period_s": 1.9187, + "n_cycles_in_duration": 6.25 + }, + "numerical_method": { + "solver": "Closed-form per phase; no ODE integration", + "tolerance": "Exact within IEEE float", + "justification": "Stick-slip dynamics have closed-form analytic solutions in each phase." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb friction; standard textbook stick-slip oscillator.", + "energy_check": "Energy is pumped in by belt-friction during stick and partially dissipated during slip.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block is carried left at v_belt=0.282 m/s during stick (~0.876 s).", + "Spring releases block; SHM about shifted equilibrium during slip (~1.042 s).", + "Cycle repeats with period 1.919 s; ~6.3 cycles in 12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.8385145622976949, + "unit": "", + "description": "" + }, + "s": { + "value": 0.2, + "unit": "", + "description": "" + }, + "k": { + "value": 13.54596136935202, + "unit": "", + "description": "" + }, + "L_s": { + "value": 0.5, + "unit": "", + "description": "" + }, + "mu_s": { + "value": 0.40679752315615764, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.20691457162846572, + "unit": "", + "description": "" + }, + "v_belt": { + "value": 0.28158807637770567, + "unit": "", + "description": "" + }, + "x_eq": { + "value": 0.0, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_belt_spring_stickslip/459/params.json b/scipy/block_belt_spring_stickslip/459/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b4fc51beee0a825235e8ec67fe8e6a648e3a31eb --- /dev/null +++ b/scipy/block_belt_spring_stickslip/459/params.json @@ -0,0 +1,12 @@ +{ + "g": 9.8, + "m": 0.8385145622976949, + "s": 0.2, + "k": 13.54596136935202, + "L_s": 0.5, + "mu_s": 0.40679752315615764, + "mu_k": 0.20691457162846572, + "v_belt": 0.28158807637770567, + "x_eq": 0.0, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_belt_spring_stickslip/459/simulation_code.py b/scipy/block_belt_spring_stickslip/459/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a0f95ddfae21062421b03ca416a58a6dc64ad5c4 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/459/simulation_code.py @@ -0,0 +1,150 @@ +"""Block-belt-spring stick-slip - standalone script.""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +from matplotlib.patches import Rectangle +from matplotlib.collections import LineCollection + +g = 9.8; m = 0.8385145622976949; s = 0.2; k = 13.54596136935202 +L_s = 0.5; mu_s = 0.40679752315615764; mu_k = 0.20691457162846572 +v_belt = 0.28158807637770567; x_eq = 0.0 +duration = 12; fps = 30 + +omega = np.sqrt(k / m) +x_break = x_eq - mu_s * m * g / k +x_new = x_eq - mu_k * m * g / k +x_wall = x_eq + s / 2 + L_s + +EPS = 1e-12 +segments = [] +t_cur = 0.0; x_cur = x_eq; phase = "stick" +for _ in range(1000): + if t_cur >= duration - EPS: break + if phase == "stick": + dx_to_break = x_cur - x_break + t_end = t_cur + (dx_to_break / v_belt if dx_to_break > EPS else 0.0) + t_end_clipped = min(t_end, duration) + segments.append({"kind": "stick", "t0": t_cur, "t1": t_end_clipped, "x0": x_cur}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_break; phase = "slip" + continue + A = x_break - x_new + B = -v_belt / omega + half = np.arctan2(-A, B) + if half <= 0: half += np.pi + t_slip_dur = 2.0 * half / omega + u_end = A * np.cos(omega * t_slip_dur) + B * np.sin(omega * t_slip_dur) + x_slip_end = x_new + u_end + t_end = t_cur + t_slip_dur + t_end_clipped = min(t_end, duration) + segments.append({"kind": "slip", "t0": t_cur, "t1": t_end_clipped, + "x0": x_break, "A": A, "B": B}) + if t_end >= duration - EPS: break + t_cur = t_end; x_cur = x_slip_end; phase = "stick" + + +def block_x_at(t): + for seg in segments: + if seg["t0"] - EPS <= t <= seg["t1"] + EPS: + tau = t - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + seg = segments[-1] + tau = seg["t1"] - seg["t0"] + if seg["kind"] == "stick": + return seg["x0"] - v_belt * tau + u = seg["A"] * np.cos(omega * tau) + seg["B"] * np.sin(omega * tau) + return x_new + u + + +t_grid = np.linspace(0.0, duration, int(duration * fps) + 1) +x_arr = np.array([block_x_at(t) for t in t_grid]) +x_min_block = float(min(x_arr.min(), x_break)) +belt_thickness = 0.10 +y_belt_top = 0.0 +y_block_bottom = y_belt_top +y_block_top = y_block_bottom + s +x_belt_left = x_min_block - 0.40 - s/2 +x_belt_right = x_wall + +fig_w, fig_h = 10.0, 5.0 +fig_aspect = fig_w / fig_h +x_lo_d = x_belt_left - 0.05; x_hi_d = x_wall + 0.20 +y_lo_d = -belt_thickness - 0.20; y_hi_d = y_block_top + 0.60 +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.add_patch(Rectangle((x_belt_left, y_belt_top - belt_thickness), + x_belt_right - x_belt_left, belt_thickness, + facecolor="#4a4e69", edgecolor="none", zorder=1)) +ax.plot([x_belt_left, x_belt_right], [y_belt_top, y_belt_top], + color="#adb5bd", lw=2.0, zorder=2) +ax.plot([x_belt_left, x_belt_right], + [y_belt_top - belt_thickness, y_belt_top - belt_thickness], + color="#adb5bd", lw=1.5, zorder=2) +wall_w = 0.10 +ax.add_patch(Rectangle((x_wall, y_lo), wall_w, y_hi - y_lo, + facecolor="#adb5bd", edgecolor="none", zorder=1)) + +hash_spacing = 0.30; hash_w = 0.025 +hash_lc = LineCollection([], colors="#adb5bd", linewidths=2.0, zorder=3) +ax.add_collection(hash_lc) +block_patch = Rectangle((x_eq - s/2, y_block_bottom), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=5) +ax.add_patch(block_patch) +spring_line, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +n_zigzag = 12; zigzag_amp = 0.06 + + +def spring_path(x_left, x_right, y_centre): + if x_right - x_left < 1e-3: + return np.array([x_left, x_right]), np.array([y_centre, y_centre]) + n_pts = n_zigzag * 2 + 3 + t_param = np.linspace(0.0, 1.0, n_pts) + xs = x_left + t_param * (x_right - x_left) + lead = 0.06 + env = np.where((t_param < lead) | (t_param > 1 - lead), 0.0, + np.sin(n_zigzag * 2.0 * np.pi * + (t_param - lead) / (1.0 - 2.0 * lead))) + ys = y_centre + zigzag_amp * env + return xs, ys + + +def update(f): + ti = f / fps + cx = block_x_at(ti) + block_patch.set_xy((cx - s/2, y_block_bottom)) + sx, sy = spring_path(cx + s/2, x_wall, y_block_bottom + s/2) + spring_line.set_data(sx, sy) + s0 = (-v_belt * ti) % hash_spacing + n_marks = int(np.ceil((x_belt_right - x_belt_left) / hash_spacing)) + 2 + segs = [] + for i in range(n_marks): + x_m = x_belt_left + s0 + i * hash_spacing - hash_spacing + if x_belt_left <= x_m <= x_belt_right: + segs.append([(x_m, y_belt_top - hash_w), (x_m, y_belt_top + hash_w)]) + hash_lc.set_segments(segs) + return block_patch, spring_line, hash_lc + + +n_frames = int(duration * fps) + 1 +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") diff --git a/scipy/block_belt_spring_stickslip/459/video.mp4 b/scipy/block_belt_spring_stickslip/459/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e502e69daafe49bf51a2f9d7c05f74337d1fc965 --- /dev/null +++ b/scipy/block_belt_spring_stickslip/459/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bd8cede54807f50d6f4bef5d54395576c12587a0e7bdee3ddcaa851b68fcede0 +size 35539 diff --git a/scipy/block_chain_collision/011/cot.json b/scipy/block_chain_collision/011/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..b64ede5cbe81d3e831fca725567441fdca8ab49c --- /dev/null +++ b/scipy/block_chain_collision/011/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "5 stationary blocks (mass m=0.566 kg, side 0.200 m, gap 0.061 m) sit in a row on a frictional floor. A striker (block #6) enters from the right with v=-2.506 m/s. Pairwise 1D elastic collisions (e=0.936) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.170) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 1.7784 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 2.5063765336982113, + "striker_initial_KE_J": 1.7784, + "deceleration_mug_m_s2": 1.6646 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~1.5 s after isolation." + ], + "parameters": { + "N_static": { + "value": 5, + "unit": "", + "description": "" + }, + "m": { + "value": 0.5662088732411181, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -2.5063765336982113, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.06115652178918622, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9355998010303397, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.1696828527396929, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 6, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/011/params.json b/scipy/block_chain_collision/011/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c3c1ea8c6274e15ddc646a8151efb204446d5535 --- /dev/null +++ b/scipy/block_chain_collision/011/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 5, + "m": 0.5662088732411181, + "v_incoming": -2.5063765336982113, + "block_size": 0.2, + "gap": 0.06115652178918622, + "e_rest": 0.9355998010303397, + "mu_k": 0.1696828527396929, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 6 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/011/simulation_code.py b/scipy/block_chain_collision/011/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..9f36162f1adfa875b377c04c460f167062f6a0a6 --- /dev/null +++ b/scipy/block_chain_collision/011/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 6; N_STATIC = 5 +m = 0.5662088732411181; v_incoming = -2.5063765336982113; block_size = 0.2 +gap = 0.06115652178918622; e_rest = 0.9355998010303397; mu_k = 0.1696828527396929; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/011/video.mp4 b/scipy/block_chain_collision/011/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..6fa68dabda0091d01cea9f2e496eb3fbbdd29510 --- /dev/null +++ b/scipy/block_chain_collision/011/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:71352f57bc21d97b566c820eb3bca51105bbdd720a7e016b0ad879f5798709e5 +size 5257 diff --git a/scipy/block_chain_collision/065/cot.json b/scipy/block_chain_collision/065/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..69652a8eb810dbc179a3f2530d0e87b604141dc6 --- /dev/null +++ b/scipy/block_chain_collision/065/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "5 stationary blocks (mass m=0.550 kg, side 0.200 m, gap 0.052 m) sit in a row on a frictional floor. A striker (block #6) enters from the right with v=-3.055 m/s. Pairwise 1D elastic collisions (e=0.931) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.172) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 2.5684 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 3.054759781526898, + "striker_initial_KE_J": 2.5684, + "deceleration_mug_m_s2": 1.6875 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~1.8 s after isolation." + ], + "parameters": { + "N_static": { + "value": 5, + "unit": "", + "description": "" + }, + "m": { + "value": 0.5504800168383955, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -3.054759781526898, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.0520088401801267, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9312485994202528, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.17201542428888061, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 6, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/065/params.json b/scipy/block_chain_collision/065/params.json new file mode 100644 index 0000000000000000000000000000000000000000..bf26233eef70ca55843ffd4f910ac747a748a292 --- /dev/null +++ b/scipy/block_chain_collision/065/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 5, + "m": 0.5504800168383955, + "v_incoming": -3.054759781526898, + "block_size": 0.2, + "gap": 0.0520088401801267, + "e_rest": 0.9312485994202528, + "mu_k": 0.17201542428888061, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 6 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/065/simulation_code.py b/scipy/block_chain_collision/065/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0ee229ab09407ef4432379b237b65bb830159f19 --- /dev/null +++ b/scipy/block_chain_collision/065/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 6; N_STATIC = 5 +m = 0.5504800168383955; v_incoming = -3.054759781526898; block_size = 0.2 +gap = 0.0520088401801267; e_rest = 0.9312485994202528; mu_k = 0.17201542428888061; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/065/video.mp4 b/scipy/block_chain_collision/065/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..b45f935a8ce3619fad811e878e230eae9334508f --- /dev/null +++ b/scipy/block_chain_collision/065/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:91d9ef730ad6757f423f333dced73bb8392b75621011e4477eec597e702514f9 +size 5189 diff --git a/scipy/block_chain_collision/112/cot.json b/scipy/block_chain_collision/112/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ff3a5c2a69707e9aae19863a5b86dbe078e48285 --- /dev/null +++ b/scipy/block_chain_collision/112/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "7 stationary blocks (mass m=0.586 kg, side 0.200 m, gap 0.041 m) sit in a row on a frictional floor. A striker (block #8) enters from the right with v=-2.664 m/s. Pairwise 1D elastic collisions (e=0.954) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.155) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 2.0779 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 2.663665110622586, + "striker_initial_KE_J": 2.0779, + "deceleration_mug_m_s2": 1.521 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~1.8 s after isolation." + ], + "parameters": { + "N_static": { + "value": 7, + "unit": "", + "description": "" + }, + "m": { + "value": 0.5857282063691976, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -2.663665110622586, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.04148417179194887, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9544850469315913, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.15504790990691145, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 8, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/112/params.json b/scipy/block_chain_collision/112/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c85b91f9d6a3c0c980982e912007dfe7219a18a3 --- /dev/null +++ b/scipy/block_chain_collision/112/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 7, + "m": 0.5857282063691976, + "v_incoming": -2.663665110622586, + "block_size": 0.2, + "gap": 0.04148417179194887, + "e_rest": 0.9544850469315913, + "mu_k": 0.15504790990691145, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 8 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/112/simulation_code.py b/scipy/block_chain_collision/112/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..308ebb255f41a23cda517cdf1939f6f21a0d9a5a --- /dev/null +++ b/scipy/block_chain_collision/112/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 8; N_STATIC = 7 +m = 0.5857282063691976; v_incoming = -2.663665110622586; block_size = 0.2 +gap = 0.04148417179194887; e_rest = 0.9544850469315913; mu_k = 0.15504790990691145; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/112/video.mp4 b/scipy/block_chain_collision/112/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a252a31266bfa052f542b2e723756b2c417c8f82 --- /dev/null +++ b/scipy/block_chain_collision/112/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7677827233dbed23cfe69240c04c2ff9bc44527817f1eb78bbde1939d0749d8d +size 5424 diff --git a/scipy/block_chain_collision/117/cot.json b/scipy/block_chain_collision/117/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..a9b5d8d1a41f4f893d73afafa50aa2e8063d8ce4 --- /dev/null +++ b/scipy/block_chain_collision/117/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "8 stationary blocks (mass m=0.469 kg, side 0.200 m, gap 0.056 m) sit in a row on a frictional floor. A striker (block #9) enters from the right with v=-2.752 m/s. Pairwise 1D elastic collisions (e=0.885) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.175) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 1.7751 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 2.7521764589149336, + "striker_initial_KE_J": 1.7751, + "deceleration_mug_m_s2": 1.7149 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~1.6 s after isolation." + ], + "parameters": { + "N_static": { + "value": 8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.4687149633324936, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -2.7521764589149336, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.0563432038345658, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.8850620635534769, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.17481267718642074, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 9, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/117/params.json b/scipy/block_chain_collision/117/params.json new file mode 100644 index 0000000000000000000000000000000000000000..8af4142a1098fe086cbdbfd53ddbdced084721f4 --- /dev/null +++ b/scipy/block_chain_collision/117/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 8, + "m": 0.4687149633324936, + "v_incoming": -2.7521764589149336, + "block_size": 0.2, + "gap": 0.0563432038345658, + "e_rest": 0.8850620635534769, + "mu_k": 0.17481267718642074, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 9 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/117/simulation_code.py b/scipy/block_chain_collision/117/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..819f18aad509e5a5239247f029d72b46e07f393b --- /dev/null +++ b/scipy/block_chain_collision/117/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 9; N_STATIC = 8 +m = 0.4687149633324936; v_incoming = -2.7521764589149336; block_size = 0.2 +gap = 0.0563432038345658; e_rest = 0.8850620635534769; mu_k = 0.17481267718642074; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/117/video.mp4 b/scipy/block_chain_collision/117/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..64334817a1a7eea27a088466f32a6ef536938fd9 --- /dev/null +++ b/scipy/block_chain_collision/117/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:631c1af4abde6d168f12e51c1ba24d8c109f3d3a36f2f98da5b4c686d974c213 +size 5316 diff --git a/scipy/block_chain_collision/156/cot.json b/scipy/block_chain_collision/156/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..44fea548d4732a5647164d678922b2fe8fc70630 --- /dev/null +++ b/scipy/block_chain_collision/156/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "8 stationary blocks (mass m=0.456 kg, side 0.200 m, gap 0.061 m) sit in a row on a frictional floor. A striker (block #9) enters from the right with v=-3.091 m/s. Pairwise 1D elastic collisions (e=0.963) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.123) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 2.1792 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 3.0908905363465777, + "striker_initial_KE_J": 2.1792, + "deceleration_mug_m_s2": 1.2047 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~2.6 s after isolation." + ], + "parameters": { + "N_static": { + "value": 8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.4562094484486716, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -3.0908905363465777, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.060988162841233075, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9633655330947389, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.12280161341618391, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 9, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/156/params.json b/scipy/block_chain_collision/156/params.json new file mode 100644 index 0000000000000000000000000000000000000000..ac8b6ee72d2d04efbb150d078febe49913122bf1 --- /dev/null +++ b/scipy/block_chain_collision/156/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 8, + "m": 0.4562094484486716, + "v_incoming": -3.0908905363465777, + "block_size": 0.2, + "gap": 0.060988162841233075, + "e_rest": 0.9633655330947389, + "mu_k": 0.12280161341618391, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 9 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/156/simulation_code.py b/scipy/block_chain_collision/156/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..5f24978463191345ee2db119e098767b04cbe952 --- /dev/null +++ b/scipy/block_chain_collision/156/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 9; N_STATIC = 8 +m = 0.4562094484486716; v_incoming = -3.0908905363465777; block_size = 0.2 +gap = 0.060988162841233075; e_rest = 0.9633655330947389; mu_k = 0.12280161341618391; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/156/video.mp4 b/scipy/block_chain_collision/156/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..2bb35dc59cb70361ad3e9aeb8b98e873a3a4546a --- /dev/null +++ b/scipy/block_chain_collision/156/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a3661c6caf16620ad2d95f336d54f7f3759616a62a876f0020393a0b90895b56 +size 5633 diff --git a/scipy/block_chain_collision/163/cot.json b/scipy/block_chain_collision/163/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..f8af28acfadf8b1b6a0eafc0aa0a71fbda1f75ac --- /dev/null +++ b/scipy/block_chain_collision/163/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "3 stationary blocks (mass m=0.559 kg, side 0.200 m, gap 0.052 m) sit in a row on a frictional floor. A striker (block #4) enters from the right with v=-3.441 m/s. Pairwise 1D elastic collisions (e=0.961) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.129) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 3.3067 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 3.4406078898966745, + "striker_initial_KE_J": 3.3067, + "deceleration_mug_m_s2": 1.2625 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~2.7 s after isolation." + ], + "parameters": { + "N_static": { + "value": 3, + "unit": "", + "description": "" + }, + "m": { + "value": 0.5586715316194819, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -3.4406078898966745, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.05244821858997908, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9608331194200236, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.12869823619586518, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 4, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/163/params.json b/scipy/block_chain_collision/163/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b963285f58b11f40dcf96019d34e3b22750e3088 --- /dev/null +++ b/scipy/block_chain_collision/163/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 3, + "m": 0.5586715316194819, + "v_incoming": -3.4406078898966745, + "block_size": 0.2, + "gap": 0.05244821858997908, + "e_rest": 0.9608331194200236, + "mu_k": 0.12869823619586518, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 4 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/163/simulation_code.py b/scipy/block_chain_collision/163/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..99bd5a06689732079646644015e3cb25a6b1b4ee --- /dev/null +++ b/scipy/block_chain_collision/163/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 4; N_STATIC = 3 +m = 0.5586715316194819; v_incoming = -3.4406078898966745; block_size = 0.2 +gap = 0.05244821858997908; e_rest = 0.9608331194200236; mu_k = 0.12869823619586518; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/163/video.mp4 b/scipy/block_chain_collision/163/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..c53e716dfc130e78b3063a321371af4c86c88265 --- /dev/null +++ b/scipy/block_chain_collision/163/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:71418e77ff2e74b307210a7528224df5dee7df387047c056f91357271affd5dc +size 5407 diff --git a/scipy/block_chain_collision/257/cot.json b/scipy/block_chain_collision/257/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..126921696280ce5f2f4d7565bb76a0fd56d7e785 --- /dev/null +++ b/scipy/block_chain_collision/257/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "8 stationary blocks (mass m=0.441 kg, side 0.200 m, gap 0.069 m) sit in a row on a frictional floor. A striker (block #9) enters from the right with v=-2.762 m/s. Pairwise 1D elastic collisions (e=0.883) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.143) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 1.6809 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 2.761893506257948, + "striker_initial_KE_J": 1.6809, + "deceleration_mug_m_s2": 1.4049 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~2.0 s after isolation." + ], + "parameters": { + "N_static": { + "value": 8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.44070802497128536, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -2.761893506257948, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.0688225533560763, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.8825267965187372, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.1432068052192736, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 9, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/257/params.json b/scipy/block_chain_collision/257/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5e50571892de8563a7a3c0b13b2cdc1765137759 --- /dev/null +++ b/scipy/block_chain_collision/257/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 8, + "m": 0.44070802497128536, + "v_incoming": -2.761893506257948, + "block_size": 0.2, + "gap": 0.0688225533560763, + "e_rest": 0.8825267965187372, + "mu_k": 0.1432068052192736, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 9 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/257/simulation_code.py b/scipy/block_chain_collision/257/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..2a666f063330ad8d73f7e953b8784e7d229b766d --- /dev/null +++ b/scipy/block_chain_collision/257/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 9; N_STATIC = 8 +m = 0.44070802497128536; v_incoming = -2.761893506257948; block_size = 0.2 +gap = 0.0688225533560763; e_rest = 0.8825267965187372; mu_k = 0.1432068052192736; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/257/video.mp4 b/scipy/block_chain_collision/257/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..5474dc364909cf31396bd725a959f2c587d2b0f4 --- /dev/null +++ b/scipy/block_chain_collision/257/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cfea3fabdd93fee565f473f4ed6f7fa46c62d80008dc2d60726d93dc82893b36 +size 5589 diff --git a/scipy/block_chain_collision/273/cot.json b/scipy/block_chain_collision/273/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..379a2309bc6ad7536a64cd5fd2a5d3184aa59f48 --- /dev/null +++ b/scipy/block_chain_collision/273/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "5 stationary blocks (mass m=0.684 kg, side 0.200 m, gap 0.050 m) sit in a row on a frictional floor. A striker (block #6) enters from the right with v=-3.045 m/s. Pairwise 1D elastic collisions (e=0.926) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.174) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 3.1706 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 3.0450447348987333, + "striker_initial_KE_J": 3.1706, + "deceleration_mug_m_s2": 1.704 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~1.8 s after isolation." + ], + "parameters": { + "N_static": { + "value": 5, + "unit": "", + "description": "" + }, + "m": { + "value": 0.6838901633899044, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -3.0450447348987333, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.0498261388802324, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9264621610698771, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.17370099015252005, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 6, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/273/params.json b/scipy/block_chain_collision/273/params.json new file mode 100644 index 0000000000000000000000000000000000000000..8722c8c1ad155eb3d294900bf7bef13b074bac29 --- /dev/null +++ b/scipy/block_chain_collision/273/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 5, + "m": 0.6838901633899044, + "v_incoming": -3.0450447348987333, + "block_size": 0.2, + "gap": 0.0498261388802324, + "e_rest": 0.9264621610698771, + "mu_k": 0.17370099015252005, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 6 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/273/simulation_code.py b/scipy/block_chain_collision/273/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..567ccd91407fb28f6fda5e342c907b43630eb0b5 --- /dev/null +++ b/scipy/block_chain_collision/273/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 6; N_STATIC = 5 +m = 0.6838901633899044; v_incoming = -3.0450447348987333; block_size = 0.2 +gap = 0.0498261388802324; e_rest = 0.9264621610698771; mu_k = 0.17370099015252005; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/273/video.mp4 b/scipy/block_chain_collision/273/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..0e59393410de1e152fcaae8bf79fe23f1b3d90e6 --- /dev/null +++ b/scipy/block_chain_collision/273/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c8985bf5b7b5c4e0275619cbaca5d933fd4efb95a56cd54c039e959e12724fdb +size 5124 diff --git a/scipy/block_chain_collision/390/cot.json b/scipy/block_chain_collision/390/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..dbf0a1bafa2d15ab53bf90ce172a64bde833063b --- /dev/null +++ b/scipy/block_chain_collision/390/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "6 stationary blocks (mass m=0.521 kg, side 0.200 m, gap 0.059 m) sit in a row on a frictional floor. A striker (block #7) enters from the right with v=-3.420 m/s. Pairwise 1D elastic collisions (e=0.960) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.119) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 3.0465 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 3.420134969422583, + "striker_initial_KE_J": 3.0465, + "deceleration_mug_m_s2": 1.1631 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~2.9 s after isolation." + ], + "parameters": { + "N_static": { + "value": 6, + "unit": "", + "description": "" + }, + "m": { + "value": 0.5208842667844806, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -3.420134969422583, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.059455584571136255, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9603772354864917, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.11855972893632634, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 7, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/390/params.json b/scipy/block_chain_collision/390/params.json new file mode 100644 index 0000000000000000000000000000000000000000..9091c5730a836a74b9c15cdde7b0672161f658bf --- /dev/null +++ b/scipy/block_chain_collision/390/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 6, + "m": 0.5208842667844806, + "v_incoming": -3.420134969422583, + "block_size": 0.2, + "gap": 0.059455584571136255, + "e_rest": 0.9603772354864917, + "mu_k": 0.11855972893632634, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 7 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/390/simulation_code.py b/scipy/block_chain_collision/390/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..b4d6df8ec6ffe816db6906fb3a4d1f98e72ee45b --- /dev/null +++ b/scipy/block_chain_collision/390/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 7; N_STATIC = 6 +m = 0.5208842667844806; v_incoming = -3.420134969422583; block_size = 0.2 +gap = 0.059455584571136255; e_rest = 0.9603772354864917; mu_k = 0.11855972893632634; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/390/video.mp4 b/scipy/block_chain_collision/390/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d09a2666714f3244bf25d9f8b15d795c6426440d --- /dev/null +++ b/scipy/block_chain_collision/390/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:936cdf9f0df1a51474742996ae604bdb7829640dbd4745eab35e09191b102b51 +size 6070 diff --git a/scipy/block_chain_collision/394/cot.json b/scipy/block_chain_collision/394/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..1714d803a734fc7ee1ac890536173455ac00e807 --- /dev/null +++ b/scipy/block_chain_collision/394/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "6 stationary blocks (mass m=0.429 kg, side 0.200 m, gap 0.067 m) sit in a row on a frictional floor. A striker (block #7) enters from the right with v=-2.637 m/s. Pairwise 1D elastic collisions (e=0.955) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.109) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 1.4909 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 2.6365225264212158, + "striker_initial_KE_J": 1.4909, + "deceleration_mug_m_s2": 1.0721 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~2.5 s after isolation." + ], + "parameters": { + "N_static": { + "value": 6, + "unit": "", + "description": "" + }, + "m": { + "value": 0.4289602084159742, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -2.6365225264212158, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.06696304126223562, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9549390686437633, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.10928206733951762, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 7, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/394/params.json b/scipy/block_chain_collision/394/params.json new file mode 100644 index 0000000000000000000000000000000000000000..327908533bfcd90ad0980a8dee5477d41fd8b867 --- /dev/null +++ b/scipy/block_chain_collision/394/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 6, + "m": 0.4289602084159742, + "v_incoming": -2.6365225264212158, + "block_size": 0.2, + "gap": 0.06696304126223562, + "e_rest": 0.9549390686437633, + "mu_k": 0.10928206733951762, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 7 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/394/simulation_code.py b/scipy/block_chain_collision/394/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..30e810229ec4e8038376b223ac17a013732fff5f --- /dev/null +++ b/scipy/block_chain_collision/394/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 7; N_STATIC = 6 +m = 0.4289602084159742; v_incoming = -2.6365225264212158; block_size = 0.2 +gap = 0.06696304126223562; e_rest = 0.9549390686437633; mu_k = 0.10928206733951762; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/394/video.mp4 b/scipy/block_chain_collision/394/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..c2f7f41a1cd639ba8a34e8cb2c4080cf7ce51552 --- /dev/null +++ b/scipy/block_chain_collision/394/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:30aede8b6e7e1d378f0802fb6d3175d3990eb50959368fc08cf0b45483e5ac1e +size 5559 diff --git a/scipy/block_chain_collision/432/cot.json b/scipy/block_chain_collision/432/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..518bf6497fbd68324b00f685c190560aed91ad08 --- /dev/null +++ b/scipy/block_chain_collision/432/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "3 stationary blocks (mass m=0.401 kg, side 0.200 m, gap 0.068 m) sit in a row on a frictional floor. A striker (block #4) enters from the right with v=-3.431 m/s. Pairwise 1D elastic collisions (e=0.925) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.162) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 2.3612 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 3.4312297765816058, + "striker_initial_KE_J": 2.3612, + "deceleration_mug_m_s2": 1.5905 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~2.2 s after isolation." + ], + "parameters": { + "N_static": { + "value": 3, + "unit": "", + "description": "" + }, + "m": { + "value": 0.40110896088482967, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -3.4312297765816058, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.06825721046530892, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9247571605239654, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.1621275064618431, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 4, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/432/params.json b/scipy/block_chain_collision/432/params.json new file mode 100644 index 0000000000000000000000000000000000000000..1ffbd066007f0df0592c2475a6358abc43681da9 --- /dev/null +++ b/scipy/block_chain_collision/432/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 3, + "m": 0.40110896088482967, + "v_incoming": -3.4312297765816058, + "block_size": 0.2, + "gap": 0.06825721046530892, + "e_rest": 0.9247571605239654, + "mu_k": 0.1621275064618431, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 4 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/432/simulation_code.py b/scipy/block_chain_collision/432/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..113eeea2858a8acef312bb39fb334f4bfc8c2776 --- /dev/null +++ b/scipy/block_chain_collision/432/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 4; N_STATIC = 3 +m = 0.40110896088482967; v_incoming = -3.4312297765816058; block_size = 0.2 +gap = 0.06825721046530892; e_rest = 0.9247571605239654; mu_k = 0.1621275064618431; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/432/video.mp4 b/scipy/block_chain_collision/432/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e1824e518475b9a2a1a1f7d56d8d471a5ad9013a --- /dev/null +++ b/scipy/block_chain_collision/432/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3398c223e179bd013779cecca22ac1e51f0998f572b488ffc5051741319b9515 +size 5358 diff --git a/scipy/block_chain_collision/479/cot.json b/scipy/block_chain_collision/479/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..0099613695cd2f9e9baae297bec2a69d5aafafae --- /dev/null +++ b/scipy/block_chain_collision/479/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "3 stationary blocks (mass m=0.584 kg, side 0.200 m, gap 0.041 m) sit in a row on a frictional floor. A striker (block #4) enters from the right with v=-3.047 m/s. Pairwise 1D elastic collisions (e=0.941) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.104) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 2.7115 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 3.04728204184221, + "striker_initial_KE_J": 2.7115, + "deceleration_mug_m_s2": 1.018 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~3.0 s after isolation." + ], + "parameters": { + "N_static": { + "value": 3, + "unit": "", + "description": "" + }, + "m": { + "value": 0.5840102517691512, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -3.04728204184221, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.040809404630985764, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9405611126052039, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.10376814004552692, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 4, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/479/params.json b/scipy/block_chain_collision/479/params.json new file mode 100644 index 0000000000000000000000000000000000000000..628b91df5edf82be45290162d75cb1aafd9f5a48 --- /dev/null +++ b/scipy/block_chain_collision/479/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 3, + "m": 0.5840102517691512, + "v_incoming": -3.04728204184221, + "block_size": 0.2, + "gap": 0.040809404630985764, + "e_rest": 0.9405611126052039, + "mu_k": 0.10376814004552692, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 4 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/479/simulation_code.py b/scipy/block_chain_collision/479/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..5c8b7af83c012806eda290e6a84647490a1a5feb --- /dev/null +++ b/scipy/block_chain_collision/479/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 4; N_STATIC = 3 +m = 0.5840102517691512; v_incoming = -3.04728204184221; block_size = 0.2 +gap = 0.040809404630985764; e_rest = 0.9405611126052039; mu_k = 0.10376814004552692; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/479/video.mp4 b/scipy/block_chain_collision/479/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a4e45b10d316db48c396d65aacc606a942e4de7c --- /dev/null +++ b/scipy/block_chain_collision/479/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c6ba1d3c5a178c704e5c0cfaa334c5334e548827c244c6daaae3d6512123f327 +size 5339 diff --git a/scipy/block_chain_collision/541/cot.json b/scipy/block_chain_collision/541/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..758edd9608b11d5695980d9e9dd4211cae08801c --- /dev/null +++ b/scipy/block_chain_collision/541/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "8 stationary blocks (mass m=0.501 kg, side 0.200 m, gap 0.046 m) sit in a row on a frictional floor. A striker (block #9) enters from the right with v=-2.445 m/s. Pairwise 1D elastic collisions (e=0.959) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.177) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 1.4966 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 2.444660870923505, + "striker_initial_KE_J": 1.4966, + "deceleration_mug_m_s2": 1.7379 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~1.4 s after isolation." + ], + "parameters": { + "N_static": { + "value": 8, + "unit": "", + "description": "" + }, + "m": { + "value": 0.500837589286185, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -2.444660870923505, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.045572481657669583, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9589950169294567, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.17715255147589515, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 9, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/541/params.json b/scipy/block_chain_collision/541/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e5900812ba8d929fbbf15a4a23e33ec02a8a9620 --- /dev/null +++ b/scipy/block_chain_collision/541/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 8, + "m": 0.500837589286185, + "v_incoming": -2.444660870923505, + "block_size": 0.2, + "gap": 0.045572481657669583, + "e_rest": 0.9589950169294567, + "mu_k": 0.17715255147589515, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 9 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/541/simulation_code.py b/scipy/block_chain_collision/541/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..42319b077ae29598a136fe5d61470bbccc0f8376 --- /dev/null +++ b/scipy/block_chain_collision/541/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 9; N_STATIC = 8 +m = 0.500837589286185; v_incoming = -2.444660870923505; block_size = 0.2 +gap = 0.045572481657669583; e_rest = 0.9589950169294567; mu_k = 0.17715255147589515; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/541/video.mp4 b/scipy/block_chain_collision/541/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..ac358caa0bc38c1773803a32913e7969401ea8e6 --- /dev/null +++ b/scipy/block_chain_collision/541/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:01ceb195b1c825120935416a10bd07a7366b01ac1b546217cd79b33f22eca109 +size 5509 diff --git a/scipy/block_chain_collision/549/cot.json b/scipy/block_chain_collision/549/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..cae9fb064c970148d907ac02d416fd1fd6646fd7 --- /dev/null +++ b/scipy/block_chain_collision/549/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "3 stationary blocks (mass m=0.433 kg, side 0.200 m, gap 0.054 m) sit in a row on a frictional floor. A striker (block #4) enters from the right with v=-2.558 m/s. Pairwise 1D elastic collisions (e=0.890) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.136) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 1.4146 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 2.5575568884997812, + "striker_initial_KE_J": 1.4146, + "deceleration_mug_m_s2": 1.3341 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~1.9 s after isolation." + ], + "parameters": { + "N_static": { + "value": 3, + "unit": "", + "description": "" + }, + "m": { + "value": 0.4325378306232014, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -2.5575568884997812, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.05363485030602195, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.8895286541471366, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.13599164133588332, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 4, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/549/params.json b/scipy/block_chain_collision/549/params.json new file mode 100644 index 0000000000000000000000000000000000000000..6bb1ae2d30abf3e6a802db2ee77858573907b6c1 --- /dev/null +++ b/scipy/block_chain_collision/549/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 3, + "m": 0.4325378306232014, + "v_incoming": -2.5575568884997812, + "block_size": 0.2, + "gap": 0.05363485030602195, + "e_rest": 0.8895286541471366, + "mu_k": 0.13599164133588332, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 4 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/549/simulation_code.py b/scipy/block_chain_collision/549/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0f6719f29493388a847d53b7286fbce7f015e7ab --- /dev/null +++ b/scipy/block_chain_collision/549/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 4; N_STATIC = 3 +m = 0.4325378306232014; v_incoming = -2.5575568884997812; block_size = 0.2 +gap = 0.05363485030602195; e_rest = 0.8895286541471366; mu_k = 0.13599164133588332; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/549/video.mp4 b/scipy/block_chain_collision/549/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7a5edc3b091e30c8b939c2eb236e8d0ee07738d4 --- /dev/null +++ b/scipy/block_chain_collision/549/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:10246a51c6f6213d85c96c6115d5580130a308d7160cfeb9b74ef5f8d5809b1d +size 5089 diff --git a/scipy/block_chain_collision/566/cot.json b/scipy/block_chain_collision/566/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..97ee52327bff8f0f6eac5cc4b70ab2b9e90020f6 --- /dev/null +++ b/scipy/block_chain_collision/566/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "block_chain_collision", + "physical_observation": "5 stationary blocks (mass m=0.485 kg, side 0.200 m, gap 0.045 m) sit in a row on a frictional floor. A striker (block #6) enters from the right with v=-2.400 m/s. Pairwise 1D elastic collisions (e=0.933) propagate momentum Newton's-cradle-style; floor friction (mu_k=0.158) decelerates moving blocks.", + "physical_law": { + "name": "Newton's 2nd law + 1D restitution collisions + Coulomb floor friction", + "statement": "Between collisions: \\dot{v} = -mu_k g sign(v). At contact: 1D elastic-restitution velocity exchange between equal-mass blocks.", + "formula": "v_i' = ((m_i - e m_j)v_i + (1+e)m_j v_j)/(m_i+m_j); equal mass: v_i' = (v_i(1-e) + v_j(1+e))/2" + }, + "ode_system": { + "state_variables": [ + "x_i for i=1..N", + "v_i = \\dot{x}_i" + ], + "equations_latex": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) if |v_i|>eps else 0", + "Event: x_{i+1} - x_i - block_size = 0 (gap closure) \\to apply 1D restitution" + ], + "equations_python": [ + "x_i' = v_i", + "v_i' = -mu_k g sign(v_i) * if |v_i|>eps else 0", + "Event: x_i_1 - x_i - block_size = 0 (gap closure) -> apply 1 * D restitution" + ] + }, + "derivation_steps": [ + "Newton's 2nd law per block: m a_i = -mu_k m g sign(v_i) (kinetic friction).", + "Inter-block collisions are detected as gap-closure events.", + "Apply 1D Newton-restitution between blocks i and i+1; tension transmitted instantly.", + "Equal masses + e<1 means each collision reduces total KE by (1-e^2)/2 of the relative-velocity-squared term.", + "Striker initial KE = 0.5 \\cdot m \\cdot v^2 = 1.3950 J." + ], + "physical_constants_derived": { + "striker_initial_speed_m_s": 2.39965134397265, + "striker_initial_KE_J": 1.395, + "deceleration_mug_m_s2": 1.5512 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal events for each gap closure", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.01", + "justification": "Hybrid integrator handles smooth segments + impulsive collisions correctly." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + 1D Newton restitution; standard textbook chain-collision.", + "energy_check": "KE strictly decreases (e<1, mu>0); momentum conserved per collision and decreases by friction impulse.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Striker reaches the chain and triggers cascading 1D collisions.", + "Like a Newton's cradle, the leftmost block ejects with most of the velocity.", + "Friction stops all blocks within ~1.5 s after isolation." + ], + "parameters": { + "N_static": { + "value": 5, + "unit": "", + "description": "" + }, + "m": { + "value": 0.4845240895441555, + "unit": "", + "description": "" + }, + "v_incoming": { + "value": -2.39965134397265, + "unit": "", + "description": "" + }, + "block_size": { + "value": 0.2, + "unit": "", + "description": "" + }, + "gap": { + "value": 0.04508483824433956, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.9333636691828602, + "unit": "", + "description": "" + }, + "mu_k": { + "value": 0.15812417681572227, + "unit": "", + "description": "" + }, + "g": { + "value": 9.81, + "unit": "", + "description": "" + }, + "x6_init": { + "value": 2.5, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + }, + "N_blocks": { + "value": 6, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_chain_collision/566/params.json b/scipy/block_chain_collision/566/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c521fccc31ba9253734a9db51006fb0ea81e4022 --- /dev/null +++ b/scipy/block_chain_collision/566/params.json @@ -0,0 +1,14 @@ +{ + "N_static": 5, + "m": 0.4845240895441555, + "v_incoming": -2.39965134397265, + "block_size": 0.2, + "gap": 0.04508483824433956, + "e_rest": 0.9333636691828602, + "mu_k": 0.15812417681572227, + "g": 9.81, + "x6_init": 2.5, + "duration": 12.0, + "fps": 30, + "N_blocks": 6 +} \ No newline at end of file diff --git a/scipy/block_chain_collision/566/simulation_code.py b/scipy/block_chain_collision/566/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..8cbf2565d09c824868a7927d231d4a40c6fc64b2 --- /dev/null +++ b/scipy/block_chain_collision/566/simulation_code.py @@ -0,0 +1,153 @@ +"""Block chain collision - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +N_BLOCKS = 6; N_STATIC = 5 +m = 0.4845240895441555; v_incoming = -2.39965134397265; block_size = 0.2 +gap = 0.04508483824433956; e_rest = 0.9333636691828602; mu_k = 0.15812417681572227; g = 9.81 +duration = 12; fps = 30; x6_init = 2.5 +V_EPS = 1e-4; GAP_EPS = 1e-9 + +total_static_width = N_STATIC * block_size + (N_STATIC - 1) * gap +left_edge_first = -total_static_width / 2.0 +x_static = np.array([left_edge_first + block_size / 2.0 + i * (block_size + gap) + for i in range(N_STATIC)]) +x0 = np.concatenate([x_static, [x6_init]]) +v0 = np.zeros(N_BLOCKS); v0[-1] = v_incoming +state0 = np.concatenate([x0, v0]) +masses = np.full(N_BLOCKS, m) + + +def rhs(t, y): + v = y[N_BLOCKS:] + dxdt = v.copy() + dvdt = np.zeros(N_BLOCKS) + moving = np.abs(v) > V_EPS + dvdt[moving] = -mu_k * g * np.sign(v[moving]) + return np.concatenate([dxdt, dvdt]) + + +def make_event(i): + def ev(t, y): + return (y[i + 1] - y[i]) - block_size + ev.terminal = True; ev.direction = -1.0 + return ev + + +events = [make_event(i) for i in range(N_BLOCKS - 1)] + + +def apply_collision(state, i, j, e): + v = state[N_BLOCKS:] + mi, mj = masses[i], masses[j] + vi, vj = v[i], v[j] + new_vi = ((1.0 - e) * mi * vi + (1.0 + e) * mj * vj) / (mi + mj) + new_vj = ((1.0 + e) * mi * vi + (1.0 - e) * mj * vj) / (mi + mj) + state[N_BLOCKS + i] = new_vi + state[N_BLOCKS + j] = new_vj + return state + + +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +times = [0.0]; states = [state0.copy()] +t_now = 0.0; y_now = state0.copy() +n_collisions = 0 +MAX_COLLISIONS = 5000 + +while t_now < duration and n_collisions < MAX_COLLISIONS: + sub_eval = t_eval[t_eval > t_now + 1e-12] + if sub_eval.size == 0: sub_eval = None + sol = solve_ivp(rhs, (t_now, duration), y_now, t_eval=sub_eval, + events=events, method="RK45", rtol=1e-9, atol=1e-11, max_step=0.01) + sol_t = np.atleast_1d(np.asarray(sol.t)).ravel() + if sol_t.size > 0: + sol_y = np.asarray(sol.y) + for k in range(sol_t.size): + times.append(sol_t[k]); states.append(sol_y[:, k].copy()) + if sol.status == 1: + ev_t = None; ev_pair = -1 + for i, te in enumerate(sol.t_events): + if te.size > 0: + t_cand = te[-1] + if ev_t is None or t_cand < ev_t - 1e-15: + ev_t = t_cand; ev_pair = i + y_at_event = sol.y_events[ev_pair][-1].copy() + v_i = y_at_event[N_BLOCKS + ev_pair] + v_j = y_at_event[N_BLOCKS + ev_pair + 1] + if v_i - v_j > 0: + y_at_event = apply_collision(y_at_event, ev_pair, ev_pair + 1, e_rest) + n_collisions += 1 + else: + y_at_event[ev_pair + 1] += 2 * GAP_EPS + times.append(ev_t); states.append(y_at_event.copy()) + t_now = ev_t; y_now = y_at_event + else: + t_now = duration + break + +t_arr = np.array(times); s_arr = np.array(states) +order = np.argsort(t_arr, kind="stable") +t_arr = t_arr[order]; s_arr = s_arr[order] +unique_times = []; last_idx = [] +for k in range(t_arr.size): + if k + 1 < t_arr.size and abs(t_arr[k+1] - t_arr[k]) < 1e-12: + continue + unique_times.append(t_arr[k]); last_idx.append(k) +unique_times = np.array(unique_times); last_idx = np.array(last_idx) +s_unique = s_arr[last_idx] + + +def sample(comp_idx): + return np.interp(t_eval, unique_times, s_unique[:, comp_idx]) + + +frame_x = np.stack([sample(i) for i in range(N_BLOCKS)], axis=1) + +x_lo_d = float(np.min(frame_x) - block_size) +x_hi_d = float(np.max(frame_x) + block_size) +x_lo_d = min(x_lo_d, -3.0); x_hi_d = max(x_hi_d, 3.0) +y_lo_d, y_hi_d = -0.3, 0.6 +fig_w, fig_h = 8, 4 +fig_aspect = fig_w / fig_h +dx = x_hi_d - x_lo_d; dy = y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 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 / fig_aspect - dy) / 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.0, zorder=1) + +block_patches = [] +for i in range(N_BLOCKS): + color = "#e63946" if i == N_BLOCKS - 1 else "#a8dadc" + rect = patches.Rectangle((frame_x[0, i] - block_size/2.0, 0.0), + block_size, block_size, + facecolor=color, edgecolor="#1d3557", lw=1.0, zorder=3) + ax.add_patch(rect); block_patches.append(rect) + + +def update(f): + for i, rect in enumerate(block_patches): + rect.set_xy((frame_x[f, i] - block_size/2.0, 0.0)) + return tuple(block_patches) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_chain_collision/566/video.mp4 b/scipy/block_chain_collision/566/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7312a3e591ede7e026298423ac943255501181f1 --- /dev/null +++ b/scipy/block_chain_collision/566/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:31cd33b0f9f5dfb121f04f2defb43fe41523b56ba1ec23ecde4d5670faa07a7f +size 5002 diff --git a/scipy/block_on_sphere_basin/006/cot.json b/scipy/block_on_sphere_basin/006/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..194d1418afd328d5eee38a50c5fab584fd8fef17 --- /dev/null +++ b/scipy/block_on_sphere_basin/006/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.437 kg, r_s=0.158 m) sits in a U-basin. A block (M_b=1.176 kg, side s=0.300 m) is released from height h_drop=1.280 m. On contact: block-sphere collision (e_bs=0.46, mu_bs=0.36). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4785 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.46 and Coulomb friction mu_bs=0.36.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.004369, + "free_fall_time_s": 0.4785, + "impact_speed_m_s": 4.689 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.48 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.4367771236887634, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.1581337025327113, + "unit": "", + "description": "" + }, + "M_b": { + "value": 1.1760050790575496, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.4614089415529551, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.35937225007174556, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.26178195795965997, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.23002988629587023, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.5966352121490045, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.057660810161359946, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.2994043250673676, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.2799096071915512, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.20398564837657998, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": 0.00823858041577194, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/006/params.json b/scipy/block_on_sphere_basin/006/params.json new file mode 100644 index 0000000000000000000000000000000000000000..eaef04a0a1a74c51286d45b8e68fdfce25eb3cb1 --- /dev/null +++ b/scipy/block_on_sphere_basin/006/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.4367771236887634, + "r_s": 0.1581337025327113, + "M_b": 1.1760050790575496, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.4614089415529551, + "mu_bs": 0.35937225007174556, + "e_floor_block": 0.26178195795965997, + "mu_floor_block": 0.23002988629587023, + "e_wall": 0.5966352121490045, + "mu_wall": 0.057660810161359946, + "mu_floor_sphere": 0.2994043250673676, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.2799096071915512, + "theta_0": 0.20398564837657998, + "X_b_0": 0.00823858041577194, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/006/simulation_code.py b/scipy/block_on_sphere_basin/006/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..6c5d631bdb240d0c01a18110355c1ef01e7a9a35 --- /dev/null +++ b/scipy/block_on_sphere_basin/006/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.4367771236887634 +r_s = 0.1581337025327113 +M_b = 1.1760050790575496 +s = 0.3 +I_b = 0.015 +e_bs = 0.4614089415529551 +mu_bs = 0.35937225007174556 +e_floor_block = 0.26178195795965997 +mu_floor_block = 0.23002988629587023 +e_wall = 0.5966352121490045 +mu_wall = 0.057660810161359946 +mu_floor_sphere = 0.2994043250673676 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.2799096071915512 +theta_0 = 0.20398564837657998 +X_b_0 = 0.00823858041577194 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/006/video.mp4 b/scipy/block_on_sphere_basin/006/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3e97e882e391929e3192024ac6e81f695f5ef80e --- /dev/null +++ b/scipy/block_on_sphere_basin/006/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:849848738a831a5f6b0b389312a3b7c66b19e86b26a20401057abba027b2df24 +size 16403 diff --git a/scipy/block_on_sphere_basin/1106/cot.json b/scipy/block_on_sphere_basin/1106/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..9852f99754095593ca0f21d1fff38f02d13e69ff --- /dev/null +++ b/scipy/block_on_sphere_basin/1106/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.454 kg, r_s=0.129 m) sits in a U-basin. A block (M_b=0.925 kg, side s=0.300 m) is released from height h_drop=1.260 m. On contact: block-sphere collision (e_bs=0.45, mu_bs=0.27). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4805 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.45 and Coulomb friction mu_bs=0.27.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.003019, + "free_fall_time_s": 0.4805, + "impact_speed_m_s": 4.7089 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.48 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.4542859775158633, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.1288929911122114, + "unit": "", + "description": "" + }, + "M_b": { + "value": 0.9252830269188286, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.45445408428709166, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.26573586948782624, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.23812664916094758, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.38864499046286305, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.7444974540235911, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.14714793017256772, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.19335613544970015, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.260195084434685, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.15010992672538326, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": 0.10404497697249993, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/1106/params.json b/scipy/block_on_sphere_basin/1106/params.json new file mode 100644 index 0000000000000000000000000000000000000000..92bfafda7c705913a94be5d9ba98308f8d8a4e1b --- /dev/null +++ b/scipy/block_on_sphere_basin/1106/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.4542859775158633, + "r_s": 0.1288929911122114, + "M_b": 0.9252830269188286, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.45445408428709166, + "mu_bs": 0.26573586948782624, + "e_floor_block": 0.23812664916094758, + "mu_floor_block": 0.38864499046286305, + "e_wall": 0.7444974540235911, + "mu_wall": 0.14714793017256772, + "mu_floor_sphere": 0.19335613544970015, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.260195084434685, + "theta_0": 0.15010992672538326, + "X_b_0": 0.10404497697249993, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/1106/simulation_code.py b/scipy/block_on_sphere_basin/1106/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..bd3b4b8d779b25be4f47350945c3a2aa67d24a88 --- /dev/null +++ b/scipy/block_on_sphere_basin/1106/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.4542859775158633 +r_s = 0.1288929911122114 +M_b = 0.9252830269188286 +s = 0.3 +I_b = 0.015 +e_bs = 0.45445408428709166 +mu_bs = 0.26573586948782624 +e_floor_block = 0.23812664916094758 +mu_floor_block = 0.38864499046286305 +e_wall = 0.7444974540235911 +mu_wall = 0.14714793017256772 +mu_floor_sphere = 0.19335613544970015 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.260195084434685 +theta_0 = 0.15010992672538326 +X_b_0 = 0.10404497697249993 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/1106/video.mp4 b/scipy/block_on_sphere_basin/1106/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..ce6c6a48a60bfbf718b187125b06987aded3c16a --- /dev/null +++ b/scipy/block_on_sphere_basin/1106/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1f116370c25962c7807f8c567089e7e42e94da5c768063b743eeeb918c68b701 +size 13518 diff --git a/scipy/block_on_sphere_basin/1116/cot.json b/scipy/block_on_sphere_basin/1116/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..d56e5f7461a7917594de84afde17627f4ebc51f1 --- /dev/null +++ b/scipy/block_on_sphere_basin/1116/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.486 kg, r_s=0.162 m) sits in a U-basin. A block (M_b=1.268 kg, side s=0.300 m) is released from height h_drop=1.378 m. On contact: block-sphere collision (e_bs=0.54, mu_bs=0.28). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4981 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.54 and Coulomb friction mu_bs=0.28.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.005108, + "free_fall_time_s": 0.4981, + "impact_speed_m_s": 4.8815 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.50 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.4861315737632874, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.16207620194138098, + "unit": "", + "description": "" + }, + "M_b": { + "value": 1.2678169691551826, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.5404544229670969, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.28190767516339577, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.25198840012955825, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.2030115352426386, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6014000316427095, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.08657606147536646, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.15103788622727027, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.3778246981065772, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.1442850858424329, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": -0.09268081266631664, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/1116/params.json b/scipy/block_on_sphere_basin/1116/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e836d626cf2db4f92baa4239b00fce52a543a861 --- /dev/null +++ b/scipy/block_on_sphere_basin/1116/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.4861315737632874, + "r_s": 0.16207620194138098, + "M_b": 1.2678169691551826, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.5404544229670969, + "mu_bs": 0.28190767516339577, + "e_floor_block": 0.25198840012955825, + "mu_floor_block": 0.2030115352426386, + "e_wall": 0.6014000316427095, + "mu_wall": 0.08657606147536646, + "mu_floor_sphere": 0.15103788622727027, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.3778246981065772, + "theta_0": 0.1442850858424329, + "X_b_0": -0.09268081266631664, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/1116/simulation_code.py b/scipy/block_on_sphere_basin/1116/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..db75c631a698abb4545029e91cc47151399c0321 --- /dev/null +++ b/scipy/block_on_sphere_basin/1116/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.4861315737632874 +r_s = 0.16207620194138098 +M_b = 1.2678169691551826 +s = 0.3 +I_b = 0.015 +e_bs = 0.5404544229670969 +mu_bs = 0.28190767516339577 +e_floor_block = 0.25198840012955825 +mu_floor_block = 0.2030115352426386 +e_wall = 0.6014000316427095 +mu_wall = 0.08657606147536646 +mu_floor_sphere = 0.15103788622727027 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.3778246981065772 +theta_0 = 0.1442850858424329 +X_b_0 = -0.09268081266631664 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/1116/video.mp4 b/scipy/block_on_sphere_basin/1116/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..82a4e377f0f2934702a88fac3802c48bce678ca6 --- /dev/null +++ b/scipy/block_on_sphere_basin/1116/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:67c161f135583578624b6f784cda982ae0d553d87c117cb927cd6084d57465ce +size 15716 diff --git a/scipy/block_on_sphere_basin/1202/cot.json b/scipy/block_on_sphere_basin/1202/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..bd0521c26f1d1cc87f8f655a12b5a6f0e03dbc8d --- /dev/null +++ b/scipy/block_on_sphere_basin/1202/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.534 kg, r_s=0.162 m) sits in a U-basin. A block (M_b=0.927 kg, side s=0.300 m) is released from height h_drop=1.145 m. On contact: block-sphere collision (e_bs=0.54, mu_bs=0.22). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4481 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.54 and Coulomb friction mu_bs=0.22.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.005584, + "free_fall_time_s": 0.4481, + "impact_speed_m_s": 4.3911 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.45 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.5342059376018196, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.1616568198078496, + "unit": "", + "description": "" + }, + "M_b": { + "value": 0.9271609535022707, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.5441834232298333, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.21734139749305212, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.3517659945974519, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.3790650163049468, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6921461986993163, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.05341157609820865, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.23354423846556083, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.145432823060955, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.19520610132113883, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": 0.11579311804477074, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/1202/params.json b/scipy/block_on_sphere_basin/1202/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b1623197a0129a36cc0ebfe6c32de514390d6103 --- /dev/null +++ b/scipy/block_on_sphere_basin/1202/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.5342059376018196, + "r_s": 0.1616568198078496, + "M_b": 0.9271609535022707, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.5441834232298333, + "mu_bs": 0.21734139749305212, + "e_floor_block": 0.3517659945974519, + "mu_floor_block": 0.3790650163049468, + "e_wall": 0.6921461986993163, + "mu_wall": 0.05341157609820865, + "mu_floor_sphere": 0.23354423846556083, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.145432823060955, + "theta_0": 0.19520610132113883, + "X_b_0": 0.11579311804477074, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/1202/simulation_code.py b/scipy/block_on_sphere_basin/1202/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..4c9d689df731849cd3ed5246160b2ecb7973bdb2 --- /dev/null +++ b/scipy/block_on_sphere_basin/1202/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.5342059376018196 +r_s = 0.1616568198078496 +M_b = 0.9271609535022707 +s = 0.3 +I_b = 0.015 +e_bs = 0.5441834232298333 +mu_bs = 0.21734139749305212 +e_floor_block = 0.3517659945974519 +mu_floor_block = 0.3790650163049468 +e_wall = 0.6921461986993163 +mu_wall = 0.05341157609820865 +mu_floor_sphere = 0.23354423846556083 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.145432823060955 +theta_0 = 0.19520610132113883 +X_b_0 = 0.11579311804477074 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/1202/video.mp4 b/scipy/block_on_sphere_basin/1202/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..00b49b6f7625435206700526e15712ded14b4b61 --- /dev/null +++ b/scipy/block_on_sphere_basin/1202/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f68bd3f2fbfef4da76810ca35b818352e8c4d00ac245846fc445d2851d4962e8 +size 16472 diff --git a/scipy/block_on_sphere_basin/1240/cot.json b/scipy/block_on_sphere_basin/1240/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..1d8c8765228ca3daa76672cd849647ea9328f26a --- /dev/null +++ b/scipy/block_on_sphere_basin/1240/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.490 kg, r_s=0.170 m) sits in a U-basin. A block (M_b=0.943 kg, side s=0.300 m) is released from height h_drop=1.314 m. On contact: block-sphere collision (e_bs=0.38, mu_bs=0.28). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4832 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.38 and Coulomb friction mu_bs=0.28.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.005672, + "free_fall_time_s": 0.4832, + "impact_speed_m_s": 4.7352 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.48 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.49031109279784796, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.1700646112731945, + "unit": "", + "description": "" + }, + "M_b": { + "value": 0.9425822417019711, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.37924727748880455, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.2793487820100016, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.29363179150812985, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.2855636988298723, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.7013477022278747, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.07696680345081147, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.19045383541925875, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.3140536002232555, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.14508559892886896, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": -0.07403699971238945, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/1240/params.json b/scipy/block_on_sphere_basin/1240/params.json new file mode 100644 index 0000000000000000000000000000000000000000..a7e23391d1ce5823802864ea63e97705a1b17fb4 --- /dev/null +++ b/scipy/block_on_sphere_basin/1240/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.49031109279784796, + "r_s": 0.1700646112731945, + "M_b": 0.9425822417019711, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.37924727748880455, + "mu_bs": 0.2793487820100016, + "e_floor_block": 0.29363179150812985, + "mu_floor_block": 0.2855636988298723, + "e_wall": 0.7013477022278747, + "mu_wall": 0.07696680345081147, + "mu_floor_sphere": 0.19045383541925875, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.3140536002232555, + "theta_0": 0.14508559892886896, + "X_b_0": -0.07403699971238945, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/1240/simulation_code.py b/scipy/block_on_sphere_basin/1240/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..31f378b28970270440391c82e156564f15b149fc --- /dev/null +++ b/scipy/block_on_sphere_basin/1240/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.49031109279784796 +r_s = 0.1700646112731945 +M_b = 0.9425822417019711 +s = 0.3 +I_b = 0.015 +e_bs = 0.37924727748880455 +mu_bs = 0.2793487820100016 +e_floor_block = 0.29363179150812985 +mu_floor_block = 0.2855636988298723 +e_wall = 0.7013477022278747 +mu_wall = 0.07696680345081147 +mu_floor_sphere = 0.19045383541925875 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.3140536002232555 +theta_0 = 0.14508559892886896 +X_b_0 = -0.07403699971238945 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/1240/video.mp4 b/scipy/block_on_sphere_basin/1240/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..073f5c447973454cb19590c9462a242634f99b4c --- /dev/null +++ b/scipy/block_on_sphere_basin/1240/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:90c5452b52c00805f85cceb5c9fbf8f2e0af2a4eafe363d8deced0896a445c4a +size 17228 diff --git a/scipy/block_on_sphere_basin/239/cot.json b/scipy/block_on_sphere_basin/239/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..1c3c51e739695d9aaf503cddf4eb11dd0fb0d936 --- /dev/null +++ b/scipy/block_on_sphere_basin/239/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.423 kg, r_s=0.127 m) sits in a U-basin. A block (M_b=0.940 kg, side s=0.300 m) is released from height h_drop=1.110 m. On contact: block-sphere collision (e_bs=0.50, mu_bs=0.39). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4481 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.50 and Coulomb friction mu_bs=0.39.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.002714, + "free_fall_time_s": 0.4481, + "impact_speed_m_s": 4.3911 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.45 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.4232592749830625, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.12660818594034687, + "unit": "", + "description": "" + }, + "M_b": { + "value": 0.9404463733423281, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.5014509196833431, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.38765129546892046, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.22233997433200722, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.24223741905705706, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6513188708747557, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.12968472820435945, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.16994785780900598, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.110352869694175, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.22134954889826977, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": 0.017187510713969983, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/239/params.json b/scipy/block_on_sphere_basin/239/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b721581d5951439fdd3e54af571453f9321d93f8 --- /dev/null +++ b/scipy/block_on_sphere_basin/239/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.4232592749830625, + "r_s": 0.12660818594034687, + "M_b": 0.9404463733423281, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.5014509196833431, + "mu_bs": 0.38765129546892046, + "e_floor_block": 0.22233997433200722, + "mu_floor_block": 0.24223741905705706, + "e_wall": 0.6513188708747557, + "mu_wall": 0.12968472820435945, + "mu_floor_sphere": 0.16994785780900598, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.110352869694175, + "theta_0": 0.22134954889826977, + "X_b_0": 0.017187510713969983, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/239/simulation_code.py b/scipy/block_on_sphere_basin/239/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..700bb1b5a59818637970882432b469a317c0221f --- /dev/null +++ b/scipy/block_on_sphere_basin/239/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.4232592749830625 +r_s = 0.12660818594034687 +M_b = 0.9404463733423281 +s = 0.3 +I_b = 0.015 +e_bs = 0.5014509196833431 +mu_bs = 0.38765129546892046 +e_floor_block = 0.22233997433200722 +mu_floor_block = 0.24223741905705706 +e_wall = 0.6513188708747557 +mu_wall = 0.12968472820435945 +mu_floor_sphere = 0.16994785780900598 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.110352869694175 +theta_0 = 0.22134954889826977 +X_b_0 = 0.017187510713969983 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/239/video.mp4 b/scipy/block_on_sphere_basin/239/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7a870ea951817b9f0b86b7930c72d4fac8b15343 --- /dev/null +++ b/scipy/block_on_sphere_basin/239/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:507db3790e71d8d58405ee4faaa7cca29174cbe3fb238e11286b12601f7adc99 +size 14687 diff --git a/scipy/block_on_sphere_basin/301/cot.json b/scipy/block_on_sphere_basin/301/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..f653cb8a005d813528149463ca82bad51291e26d --- /dev/null +++ b/scipy/block_on_sphere_basin/301/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.331 kg, r_s=0.158 m) sits in a U-basin. A block (M_b=1.092 kg, side s=0.300 m) is released from height h_drop=1.292 m. On contact: block-sphere collision (e_bs=0.31, mu_bs=0.25). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4810 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.31 and Coulomb friction mu_bs=0.25.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.003312, + "free_fall_time_s": 0.481, + "impact_speed_m_s": 4.7142 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.48 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.3314173773825183, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.1580563598470049, + "unit": "", + "description": "" + }, + "M_b": { + "value": 1.0919432566439027, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.31196137640948457, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.24971014476312486, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.36681950910993666, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.3627162891586403, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6086280646457096, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.07925337333137579, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.25824991318752744, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.2918971320597012, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.13010112196788876, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": 0.08444074414491981, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/301/params.json b/scipy/block_on_sphere_basin/301/params.json new file mode 100644 index 0000000000000000000000000000000000000000..1c6ead540517551cc45adcff609637bd6a4c67e4 --- /dev/null +++ b/scipy/block_on_sphere_basin/301/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.3314173773825183, + "r_s": 0.1580563598470049, + "M_b": 1.0919432566439027, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.31196137640948457, + "mu_bs": 0.24971014476312486, + "e_floor_block": 0.36681950910993666, + "mu_floor_block": 0.3627162891586403, + "e_wall": 0.6086280646457096, + "mu_wall": 0.07925337333137579, + "mu_floor_sphere": 0.25824991318752744, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.2918971320597012, + "theta_0": 0.13010112196788876, + "X_b_0": 0.08444074414491981, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/301/simulation_code.py b/scipy/block_on_sphere_basin/301/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a77c30ac6c2c069175d0a1aed576a83c20dc6760 --- /dev/null +++ b/scipy/block_on_sphere_basin/301/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.3314173773825183 +r_s = 0.1580563598470049 +M_b = 1.0919432566439027 +s = 0.3 +I_b = 0.015 +e_bs = 0.31196137640948457 +mu_bs = 0.24971014476312486 +e_floor_block = 0.36681950910993666 +mu_floor_block = 0.3627162891586403 +e_wall = 0.6086280646457096 +mu_wall = 0.07925337333137579 +mu_floor_sphere = 0.25824991318752744 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.2918971320597012 +theta_0 = 0.13010112196788876 +X_b_0 = 0.08444074414491981 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/301/video.mp4 b/scipy/block_on_sphere_basin/301/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..78c6534b8bd5ab064d2f682dd82da3bb0907c250 --- /dev/null +++ b/scipy/block_on_sphere_basin/301/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fbced49aeeda5f2f64f1b1f83aa94b505fe8303920885e4ad4e03c8958d42b13 +size 16033 diff --git a/scipy/block_on_sphere_basin/311/cot.json b/scipy/block_on_sphere_basin/311/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..f445b1901d5664830ab0dd8787311d7de20d7f61 --- /dev/null +++ b/scipy/block_on_sphere_basin/311/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.372 kg, r_s=0.167 m) sits in a U-basin. A block (M_b=0.834 kg, side s=0.300 m) is released from height h_drop=1.076 m. On contact: block-sphere collision (e_bs=0.53, mu_bs=0.24). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4307 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.53 and Coulomb friction mu_bs=0.24.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.004172, + "free_fall_time_s": 0.4307, + "impact_speed_m_s": 4.2206 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.43 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.372339572201268, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.16736412004037432, + "unit": "", + "description": "" + }, + "M_b": { + "value": 0.834436124699816, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.5270053056435255, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.24144957883189622, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.22258102963520754, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.2006050273914545, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6194288091581073, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.1049218905190756, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.22372853477456867, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.076201384564878, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.23157025308296458, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": 0.1496254887844856, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/311/params.json b/scipy/block_on_sphere_basin/311/params.json new file mode 100644 index 0000000000000000000000000000000000000000..35193cc58c9f0631e05e1d9797404d7ee07ab610 --- /dev/null +++ b/scipy/block_on_sphere_basin/311/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.372339572201268, + "r_s": 0.16736412004037432, + "M_b": 0.834436124699816, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.5270053056435255, + "mu_bs": 0.24144957883189622, + "e_floor_block": 0.22258102963520754, + "mu_floor_block": 0.2006050273914545, + "e_wall": 0.6194288091581073, + "mu_wall": 0.1049218905190756, + "mu_floor_sphere": 0.22372853477456867, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.076201384564878, + "theta_0": 0.23157025308296458, + "X_b_0": 0.1496254887844856, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/311/simulation_code.py b/scipy/block_on_sphere_basin/311/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..5a3befe09a957f48b1e18d1a81b8facaf973326f --- /dev/null +++ b/scipy/block_on_sphere_basin/311/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.372339572201268 +r_s = 0.16736412004037432 +M_b = 0.834436124699816 +s = 0.3 +I_b = 0.015 +e_bs = 0.5270053056435255 +mu_bs = 0.24144957883189622 +e_floor_block = 0.22258102963520754 +mu_floor_block = 0.2006050273914545 +e_wall = 0.6194288091581073 +mu_wall = 0.1049218905190756 +mu_floor_sphere = 0.22372853477456867 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.076201384564878 +theta_0 = 0.23157025308296458 +X_b_0 = 0.1496254887844856 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/311/video.mp4 b/scipy/block_on_sphere_basin/311/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..fca9d4ac79ea9b6012ee587f2efe7373292403b5 --- /dev/null +++ b/scipy/block_on_sphere_basin/311/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f22ebba6985c948cd4ecdb58f8826aab3d9e8a5453508540715ab4836f988718 +size 16377 diff --git a/scipy/block_on_sphere_basin/396/cot.json b/scipy/block_on_sphere_basin/396/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..db61b40ea9eaa059fca73f3dd04593eaf29bbd14 --- /dev/null +++ b/scipy/block_on_sphere_basin/396/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.396 kg, r_s=0.179 m) sits in a U-basin. A block (M_b=0.906 kg, side s=0.300 m) is released from height h_drop=1.353 m. On contact: block-sphere collision (e_bs=0.40, mu_bs=0.24). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4895 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.40 and Coulomb friction mu_bs=0.24.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.005069, + "free_fall_time_s": 0.4895, + "impact_speed_m_s": 4.7969 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.49 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.39594347145638714, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.1789006195624414, + "unit": "", + "description": "" + }, + "M_b": { + "value": 0.90567890801085, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.40055228804065734, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.24112572553828118, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.26230002221275267, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.39813155210900847, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.5590111092556382, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.1146224069693999, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.22673853162931984, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.3528821508690707, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.19676633449819314, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": -0.021701851344275547, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/396/params.json b/scipy/block_on_sphere_basin/396/params.json new file mode 100644 index 0000000000000000000000000000000000000000..26526b63719cae088e433b70a5fd87102a5b5d94 --- /dev/null +++ b/scipy/block_on_sphere_basin/396/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.39594347145638714, + "r_s": 0.1789006195624414, + "M_b": 0.90567890801085, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.40055228804065734, + "mu_bs": 0.24112572553828118, + "e_floor_block": 0.26230002221275267, + "mu_floor_block": 0.39813155210900847, + "e_wall": 0.5590111092556382, + "mu_wall": 0.1146224069693999, + "mu_floor_sphere": 0.22673853162931984, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.3528821508690707, + "theta_0": 0.19676633449819314, + "X_b_0": -0.021701851344275547, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/396/simulation_code.py b/scipy/block_on_sphere_basin/396/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..163a651163467f3486d8a49c91b44e909127c14b --- /dev/null +++ b/scipy/block_on_sphere_basin/396/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.39594347145638714 +r_s = 0.1789006195624414 +M_b = 0.90567890801085 +s = 0.3 +I_b = 0.015 +e_bs = 0.40055228804065734 +mu_bs = 0.24112572553828118 +e_floor_block = 0.26230002221275267 +mu_floor_block = 0.39813155210900847 +e_wall = 0.5590111092556382 +mu_wall = 0.1146224069693999 +mu_floor_sphere = 0.22673853162931984 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.3528821508690707 +theta_0 = 0.19676633449819314 +X_b_0 = -0.021701851344275547 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/396/video.mp4 b/scipy/block_on_sphere_basin/396/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..2adaceee49d3e1bb79eb45f8f6d81ef65c129bd0 --- /dev/null +++ b/scipy/block_on_sphere_basin/396/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1a0e838a109af3c22697fa51b9e148ec5634e1ad44ece8e04efa70b102d5d0e6 +size 16269 diff --git a/scipy/block_on_sphere_basin/590/cot.json b/scipy/block_on_sphere_basin/590/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..e80e09b824a7741fc00a8284a86f8ecead7c8a34 --- /dev/null +++ b/scipy/block_on_sphere_basin/590/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.528 kg, r_s=0.146 m) sits in a U-basin. A block (M_b=0.815 kg, side s=0.300 m) is released from height h_drop=1.339 m. On contact: block-sphere collision (e_bs=0.38, mu_bs=0.23). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4934 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.38 and Coulomb friction mu_bs=0.23.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.004524, + "free_fall_time_s": 0.4934, + "impact_speed_m_s": 4.8358 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.49 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.5278648368256285, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.14636987183975023, + "unit": "", + "description": "" + }, + "M_b": { + "value": 0.8152786099868756, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.38254785485290793, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.22616852743548588, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.3983942635576391, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.32698290856422674, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6319141232126199, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.06733469896444304, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.2245833751964607, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.3394568248919503, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.14346213789188783, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": 0.09134743644404941, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/590/params.json b/scipy/block_on_sphere_basin/590/params.json new file mode 100644 index 0000000000000000000000000000000000000000..4fa09e0fef47fa35e134f11de48a6a3f7ec8f0cb --- /dev/null +++ b/scipy/block_on_sphere_basin/590/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.5278648368256285, + "r_s": 0.14636987183975023, + "M_b": 0.8152786099868756, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.38254785485290793, + "mu_bs": 0.22616852743548588, + "e_floor_block": 0.3983942635576391, + "mu_floor_block": 0.32698290856422674, + "e_wall": 0.6319141232126199, + "mu_wall": 0.06733469896444304, + "mu_floor_sphere": 0.2245833751964607, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.3394568248919503, + "theta_0": 0.14346213789188783, + "X_b_0": 0.09134743644404941, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/590/simulation_code.py b/scipy/block_on_sphere_basin/590/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..3a86cd6e4b18d750e03d948c8a1edf881e52261f --- /dev/null +++ b/scipy/block_on_sphere_basin/590/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.5278648368256285 +r_s = 0.14636987183975023 +M_b = 0.8152786099868756 +s = 0.3 +I_b = 0.015 +e_bs = 0.38254785485290793 +mu_bs = 0.22616852743548588 +e_floor_block = 0.3983942635576391 +mu_floor_block = 0.32698290856422674 +e_wall = 0.6319141232126199 +mu_wall = 0.06733469896444304 +mu_floor_sphere = 0.2245833751964607 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.3394568248919503 +theta_0 = 0.14346213789188783 +X_b_0 = 0.09134743644404941 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/590/video.mp4 b/scipy/block_on_sphere_basin/590/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..9b847ab0b2c7bfe065db5ddea4b77e7a851051fa --- /dev/null +++ b/scipy/block_on_sphere_basin/590/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:720442767a332857e0e92c4ac27b3448d1d9a34d29b1f6ce29a43b73d8a310f2 +size 15474 diff --git a/scipy/block_on_sphere_basin/629/cot.json b/scipy/block_on_sphere_basin/629/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..8f9026ea363c63cece2065d7bfa4cfd043615b31 --- /dev/null +++ b/scipy/block_on_sphere_basin/629/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.548 kg, r_s=0.132 m) sits in a U-basin. A block (M_b=1.189 kg, side s=0.300 m) is released from height h_drop=1.237 m. On contact: block-sphere collision (e_bs=0.48, mu_bs=0.34). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4748 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.48 and Coulomb friction mu_bs=0.34.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.003834, + "free_fall_time_s": 0.4748, + "impact_speed_m_s": 4.6532 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.47 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.5477485474010998, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.1322821289463513, + "unit": "", + "description": "" + }, + "M_b": { + "value": 1.189059816931209, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.4824594663551828, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.3439735346620182, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.2173240529266565, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.3890505758312509, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.7383660584592315, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.07757958816869619, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.23730128648288135, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.2370034794922828, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.20296240170389085, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": 0.14470338970093707, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/629/params.json b/scipy/block_on_sphere_basin/629/params.json new file mode 100644 index 0000000000000000000000000000000000000000..9e3862456a98cb88e307e08c76d054d8176eebc4 --- /dev/null +++ b/scipy/block_on_sphere_basin/629/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.5477485474010998, + "r_s": 0.1322821289463513, + "M_b": 1.189059816931209, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.4824594663551828, + "mu_bs": 0.3439735346620182, + "e_floor_block": 0.2173240529266565, + "mu_floor_block": 0.3890505758312509, + "e_wall": 0.7383660584592315, + "mu_wall": 0.07757958816869619, + "mu_floor_sphere": 0.23730128648288135, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.2370034794922828, + "theta_0": 0.20296240170389085, + "X_b_0": 0.14470338970093707, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/629/simulation_code.py b/scipy/block_on_sphere_basin/629/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..fddcdf0ad589042c8064b448ba5b7b647fa5c60b --- /dev/null +++ b/scipy/block_on_sphere_basin/629/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.5477485474010998 +r_s = 0.1322821289463513 +M_b = 1.189059816931209 +s = 0.3 +I_b = 0.015 +e_bs = 0.4824594663551828 +mu_bs = 0.3439735346620182 +e_floor_block = 0.2173240529266565 +mu_floor_block = 0.3890505758312509 +e_wall = 0.7383660584592315 +mu_wall = 0.07757958816869619 +mu_floor_sphere = 0.23730128648288135 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.2370034794922828 +theta_0 = 0.20296240170389085 +X_b_0 = 0.14470338970093707 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/629/video.mp4 b/scipy/block_on_sphere_basin/629/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..73bd6b233f16173bcb1d73380d4006b13ca82a73 --- /dev/null +++ b/scipy/block_on_sphere_basin/629/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e4327d59e4d671e3ec398602d3759d6bf65e966cf9e08fc6ab7079349bf855f9 +size 15269 diff --git a/scipy/block_on_sphere_basin/725/cot.json b/scipy/block_on_sphere_basin/725/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..01a5a168080a8a185001abbae3e2a426e7b5c0be --- /dev/null +++ b/scipy/block_on_sphere_basin/725/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.544 kg, r_s=0.179 m) sits in a U-basin. A block (M_b=1.143 kg, side s=0.300 m) is released from height h_drop=1.244 m. On contact: block-sphere collision (e_bs=0.40, mu_bs=0.24). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4662 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.40 and Coulomb friction mu_bs=0.24.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.006979, + "free_fall_time_s": 0.4662, + "impact_speed_m_s": 4.5683 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.47 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.5436134541783771, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.1791502411426538, + "unit": "", + "description": "" + }, + "M_b": { + "value": 1.1433155547010174, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.40306864675680454, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.24154434511641046, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.38573495719521367, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.3940234321493795, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6148703257682067, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.06996268765585015, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.15276474626774597, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.2439184570654052, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.2476819030321642, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": -0.04786415281925979, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/725/params.json b/scipy/block_on_sphere_basin/725/params.json new file mode 100644 index 0000000000000000000000000000000000000000..1952667134cbc0192b52665ae43774e92c928f91 --- /dev/null +++ b/scipy/block_on_sphere_basin/725/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.5436134541783771, + "r_s": 0.1791502411426538, + "M_b": 1.1433155547010174, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.40306864675680454, + "mu_bs": 0.24154434511641046, + "e_floor_block": 0.38573495719521367, + "mu_floor_block": 0.3940234321493795, + "e_wall": 0.6148703257682067, + "mu_wall": 0.06996268765585015, + "mu_floor_sphere": 0.15276474626774597, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.2439184570654052, + "theta_0": 0.2476819030321642, + "X_b_0": -0.04786415281925979, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/725/simulation_code.py b/scipy/block_on_sphere_basin/725/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a49ef385f92df056cf1d6b1633695c71adb6267c --- /dev/null +++ b/scipy/block_on_sphere_basin/725/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.5436134541783771 +r_s = 0.1791502411426538 +M_b = 1.1433155547010174 +s = 0.3 +I_b = 0.015 +e_bs = 0.40306864675680454 +mu_bs = 0.24154434511641046 +e_floor_block = 0.38573495719521367 +mu_floor_block = 0.3940234321493795 +e_wall = 0.6148703257682067 +mu_wall = 0.06996268765585015 +mu_floor_sphere = 0.15276474626774597 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.2439184570654052 +theta_0 = 0.2476819030321642 +X_b_0 = -0.04786415281925979 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/725/video.mp4 b/scipy/block_on_sphere_basin/725/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a4246ccc36d394a90bd806392fe0ed7684000832 --- /dev/null +++ b/scipy/block_on_sphere_basin/725/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fb0c04ab2ed78a95fcf3811772f2a8d279b3e3488dd204cdcea524d65fdd77b2 +size 19235 diff --git a/scipy/block_on_sphere_basin/739/cot.json b/scipy/block_on_sphere_basin/739/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..b04910e9c1e1df6aef720ce1a35d08ecd06dfc40 --- /dev/null +++ b/scipy/block_on_sphere_basin/739/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.403 kg, r_s=0.177 m) sits in a U-basin. A block (M_b=1.144 kg, side s=0.300 m) is released from height h_drop=1.383 m. On contact: block-sphere collision (e_bs=0.35, mu_bs=0.34). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4961 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.35 and Coulomb friction mu_bs=0.34.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.005044, + "free_fall_time_s": 0.4961, + "impact_speed_m_s": 4.862 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.50 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.4027983302374981, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.1769399905477863, + "unit": "", + "description": "" + }, + "M_b": { + "value": 1.1440856580864651, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.34607862370728243, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.3447793674260109, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.25541086355951814, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.3648972570974979, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6858712940088105, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.10252642183872449, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.2753812494930511, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.382989932223456, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.11126802603112969, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": 0.004521765213199497, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/739/params.json b/scipy/block_on_sphere_basin/739/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c539f7680ba6522812729bd943b15aecd2371de8 --- /dev/null +++ b/scipy/block_on_sphere_basin/739/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.4027983302374981, + "r_s": 0.1769399905477863, + "M_b": 1.1440856580864651, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.34607862370728243, + "mu_bs": 0.3447793674260109, + "e_floor_block": 0.25541086355951814, + "mu_floor_block": 0.3648972570974979, + "e_wall": 0.6858712940088105, + "mu_wall": 0.10252642183872449, + "mu_floor_sphere": 0.2753812494930511, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.382989932223456, + "theta_0": 0.11126802603112969, + "X_b_0": 0.004521765213199497, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/739/simulation_code.py b/scipy/block_on_sphere_basin/739/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..578ef57b4fadba456e86b56a2427fa94a5c445bc --- /dev/null +++ b/scipy/block_on_sphere_basin/739/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.4027983302374981 +r_s = 0.1769399905477863 +M_b = 1.1440856580864651 +s = 0.3 +I_b = 0.015 +e_bs = 0.34607862370728243 +mu_bs = 0.3447793674260109 +e_floor_block = 0.25541086355951814 +mu_floor_block = 0.3648972570974979 +e_wall = 0.6858712940088105 +mu_wall = 0.10252642183872449 +mu_floor_sphere = 0.2753812494930511 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.382989932223456 +theta_0 = 0.11126802603112969 +X_b_0 = 0.004521765213199497 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/739/video.mp4 b/scipy/block_on_sphere_basin/739/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..65e6aa0ac9f02d952ece62694eb1a453e9b7fdf8 --- /dev/null +++ b/scipy/block_on_sphere_basin/739/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:848ec49b058d94266c7eca34e8729390af8984ce0dae17092778f9340967ccd6 +size 17559 diff --git a/scipy/block_on_sphere_basin/947/cot.json b/scipy/block_on_sphere_basin/947/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..cfea46bb0a6ec438a9a3efdacabd98d9600710e2 --- /dev/null +++ b/scipy/block_on_sphere_basin/947/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.535 kg, r_s=0.139 m) sits in a U-basin. A block (M_b=0.961 kg, side s=0.300 m) is released from height h_drop=1.162 m. On contact: block-sphere collision (e_bs=0.55, mu_bs=0.25). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4570 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.55 and Coulomb friction mu_bs=0.25.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.004114, + "free_fall_time_s": 0.457, + "impact_speed_m_s": 4.479 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.46 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.5351147769940204, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.13864251304187392, + "unit": "", + "description": "" + }, + "M_b": { + "value": 0.9614411025283749, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.5479697146699893, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.2528805634642914, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.25178797664526575, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.31216929550465833, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6478662840517923, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.11145807958160192, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.26985945534541383, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.1621795572459608, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.17590444119818827, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": 0.09081576402566005, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/947/params.json b/scipy/block_on_sphere_basin/947/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e1fe2d1f78ba0750afec78969c8a24da81e31a2a --- /dev/null +++ b/scipy/block_on_sphere_basin/947/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.5351147769940204, + "r_s": 0.13864251304187392, + "M_b": 0.9614411025283749, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.5479697146699893, + "mu_bs": 0.2528805634642914, + "e_floor_block": 0.25178797664526575, + "mu_floor_block": 0.31216929550465833, + "e_wall": 0.6478662840517923, + "mu_wall": 0.11145807958160192, + "mu_floor_sphere": 0.26985945534541383, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.1621795572459608, + "theta_0": 0.17590444119818827, + "X_b_0": 0.09081576402566005, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/947/simulation_code.py b/scipy/block_on_sphere_basin/947/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..472060dfd1dadb337376920ada748e855c53fb3b --- /dev/null +++ b/scipy/block_on_sphere_basin/947/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.5351147769940204 +r_s = 0.13864251304187392 +M_b = 0.9614411025283749 +s = 0.3 +I_b = 0.015 +e_bs = 0.5479697146699893 +mu_bs = 0.2528805634642914 +e_floor_block = 0.25178797664526575 +mu_floor_block = 0.31216929550465833 +e_wall = 0.6478662840517923 +mu_wall = 0.11145807958160192 +mu_floor_sphere = 0.26985945534541383 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.1621795572459608 +theta_0 = 0.17590444119818827 +X_b_0 = 0.09081576402566005 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/947/video.mp4 b/scipy/block_on_sphere_basin/947/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..37df6c17081e4bff534b1b1835458dba9495e9d3 --- /dev/null +++ b/scipy/block_on_sphere_basin/947/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:817a0a58788101315b03e8ffe8500755fb726c6f3d6cc78a5d12bff5db945f95 +size 13895 diff --git a/scipy/block_on_sphere_basin/980/cot.json b/scipy/block_on_sphere_basin/980/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..1cba2f9fb1c759fdd08c3e6b9d9275d9d5ae9148 --- /dev/null +++ b/scipy/block_on_sphere_basin/980/cot.json @@ -0,0 +1,149 @@ +{ + "simulation": "block_on_sphere_basin", + "physical_observation": "2D side view: a sphere (m_s=0.466 kg, r_s=0.126 m) sits in a U-basin. A block (M_b=1.220 kg, side s=0.300 m) is released from height h_drop=1.227 m. On contact: block-sphere collision (e_bs=0.51, mu_bs=0.33). Sphere then rolls/slides on basin floor with kinetic friction; bounces off walls.", + "physical_law": { + "name": "Newton-Euler rigid-body dynamics + impulsive contact resolution", + "statement": "Free-flight: gravity + floor friction on sphere. Contacts: decoupled normal/Coulomb-tangent rigid-body impulses with restitution.", + "formula": "J_n = -(1+e) v_rel.n / inv_m_n; |J_t| <= mu |J_n|" + }, + "ode_system": { + "state_variables": [ + "Xb, Yb, theta_b, Vxb, Vyb, omb (block COM and rotation)", + "Xs, Ys, theta_s, Vxs, Vys, oms (sphere)" + ], + "equations_latex": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: \\dot{Vxs} = -sgn(v_slip) mu_floor_sphere g; \\dot{om}_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ], + "equations_python": [ + "block: ddot Xb = 0; ddot Yb = -g; ddot theta_b = 0 (free flight)", + "sphere: Vxs_dot = -sgn(v_slip) * mu_floor_sphere g; om_dot_s depends on slip", + "Events: block-sphere gap=0; block corners hit floor/walls; sphere reaches walls" + ] + }, + "derivation_steps": [ + "Block free-fall: t_drop ~ \\sqrt(2(h_drop - r_s)/g) = 0.4740 s.", + "On block-sphere contact, apply 2D rigid-body impulse with normal restitution e_bs=0.51 and Coulomb friction mu_bs=0.33.", + "Sphere is constrained to floor (Vys=0); can roll/slide; bounces off vertical basin walls.", + "Block continues in flight until it lands on the floor; corner-by-corner impulse handles tilted impacts.", + "Settling logic: chatter detection snaps block flat when KE < threshold." + ], + "physical_constants_derived": { + "I_sphere_kg_m2": 0.00297, + "free_fall_time_s": 0.474, + "impact_speed_m_s": 4.645 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) per smooth segment + analytic impulses at events", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02; up to 200 segments", + "justification": "Standard hybrid integrator for systems with terminal contact events." + }, + "code_validation": { + "ode_source": "Newton-Euler rigid bodies; Coulomb friction cone; Newton restitution.", + "energy_check": "KE non-increasing (e<1, mu>0); momentum conserved at each contact (modulo external impulses).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block free-falls for ~0.47 s before striking sphere.", + "Sphere kicked into motion, rolls and bounces off walls.", + "Block lands on floor and settles flat; system at rest within ~12.0 s." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m_s": { + "value": 0.46569317305841684, + "unit": "", + "description": "" + }, + "r_s": { + "value": 0.12627621289394306, + "unit": "", + "description": "" + }, + "M_b": { + "value": 1.2200138612241793, + "unit": "", + "description": "" + }, + "s": { + "value": 0.3, + "unit": "", + "description": "" + }, + "I_b": { + "value": 0.015, + "unit": "", + "description": "" + }, + "e_bs": { + "value": 0.5124578417423811, + "unit": "", + "description": "" + }, + "mu_bs": { + "value": 0.33039836438083836, + "unit": "", + "description": "" + }, + "e_floor_block": { + "value": 0.21613432162041546, + "unit": "", + "description": "" + }, + "mu_floor_block": { + "value": 0.35927103257752235, + "unit": "", + "description": "" + }, + "e_wall": { + "value": 0.6974367933719785, + "unit": "", + "description": "" + }, + "mu_wall": { + "value": 0.11572882163360394, + "unit": "", + "description": "" + }, + "mu_floor_sphere": { + "value": 0.2305976070237209, + "unit": "", + "description": "" + }, + "x_left_wall": { + "value": -1.2, + "unit": "", + "description": "" + }, + "x_right_wall": { + "value": 1.2, + "unit": "", + "description": "" + }, + "h_drop": { + "value": 1.2271035459762138, + "unit": "", + "description": "" + }, + "theta_0": { + "value": 0.20322771853790267, + "unit": "", + "description": "" + }, + "X_b_0": { + "value": -0.015937156700347732, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_on_sphere_basin/980/params.json b/scipy/block_on_sphere_basin/980/params.json new file mode 100644 index 0000000000000000000000000000000000000000..acc93e0666e267a74a5f8b55e5e05f991b2d088d --- /dev/null +++ b/scipy/block_on_sphere_basin/980/params.json @@ -0,0 +1,21 @@ +{ + "g": 9.8, + "m_s": 0.46569317305841684, + "r_s": 0.12627621289394306, + "M_b": 1.2200138612241793, + "s": 0.3, + "I_b": 0.015, + "e_bs": 0.5124578417423811, + "mu_bs": 0.33039836438083836, + "e_floor_block": 0.21613432162041546, + "mu_floor_block": 0.35927103257752235, + "e_wall": 0.6974367933719785, + "mu_wall": 0.11572882163360394, + "mu_floor_sphere": 0.2305976070237209, + "x_left_wall": -1.2, + "x_right_wall": 1.2, + "h_drop": 1.2271035459762138, + "theta_0": 0.20322771853790267, + "X_b_0": -0.015937156700347732, + "duration": 12.0 +} \ No newline at end of file diff --git a/scipy/block_on_sphere_basin/980/simulation_code.py b/scipy/block_on_sphere_basin/980/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..ba0f1a58d232219e43789329a9cba191df233dfe --- /dev/null +++ b/scipy/block_on_sphere_basin/980/simulation_code.py @@ -0,0 +1,461 @@ +"""Block on sphere in basin - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches +from matplotlib.transforms import Affine2D + + +g = 9.8 +m_s = 0.46569317305841684 +r_s = 0.12627621289394306 +M_b = 1.2200138612241793 +s = 0.3 +I_b = 0.015 +e_bs = 0.5124578417423811 +mu_bs = 0.33039836438083836 +e_floor_block = 0.21613432162041546 +mu_floor_block = 0.35927103257752235 +e_wall = 0.6974367933719785 +mu_wall = 0.11572882163360394 +mu_floor_sphere = 0.2305976070237209 +x_left_wall = -1.2 +x_right_wall = 1.2 +h_drop = 1.2271035459762138 +theta_0 = 0.20322771853790267 +X_b_0 = -0.015937156700347732 +duration = 12 + + +def _run_sim(): + I_s = (2.0/5.0) * m_s * r_s * r_s + fps = 30 + n_frames = int(duration * fps) + 1 + t_grid = np.linspace(0.0, duration, n_frames) + + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + def initial_state(): + y0 = np.zeros(12) + y0[IDX['Xb']] = X_b_0; y0[IDX['Yb']] = h_drop; y0[IDX['thb']] = theta_0 + y0[IDX['Xs']] = 0.0; y0[IDX['Ys']] = r_s + return y0 + + def block_corners(Xb, Yb, th): + half = s / 2.0 + body = np.array([[half, half], [-half, half], [-half, -half], [half, -half]]) + c, sn = np.cos(th), np.sin(th) + R = np.array([[c, -sn], [sn, c]]) + return body @ R.T + np.array([Xb, Yb]) + + def block_sphere_gap(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + qx = max(abs(bx) - half, 0.0); qy = max(abs(by) - half, 0.0) + outside = np.hypot(qx, qy) + inside = min(max(abs(bx) - half, abs(by) - half), 0.0) + return outside + inside - r_s + + def block_sphere_contact(Xb, Yb, th, Xs, Ys): + c, sn = np.cos(th), np.sin(th) + dx = Xs - Xb; dy = Ys - Yb + bx = c * dx + sn * dy; by = -sn * dx + c * dy + half = s / 2.0 + cx = max(-half, min(half, bx)); cy_ = max(-half, min(half, by)) + cp_world = np.array([Xb + c * cx - sn * cy_, Yb + sn * cx + c * cy_]) + n_sphere_to_block = cp_world - np.array([Xs, Ys]) + nrm = np.linalg.norm(n_sphere_to_block) + n_hat = (n_sphere_to_block / nrm) if nrm >= 1e-12 else np.array([0.0, 1.0]) + r_b = cp_world - np.array([Xb, Yb]) + return cp_world, n_hat, r_b + + _settled = [False] + _bs_cooldown_until = [-1e9] + + def free_flight(t, y): + dy = np.zeros(12) + if not _settled[0]: + dy[IDX['Xb']] = y[IDX['Vxb']]; dy[IDX['Yb']] = y[IDX['Vyb']] + dy[IDX['thb']] = y[IDX['omb']]; dy[IDX['Vxb']] = 0.0 + dy[IDX['Vyb']] = -g; dy[IDX['omb']] = 0.0 + Vxs = y[IDX['Vxs']]; oms = y[IDX['oms']] + v_slip = Vxs - r_s * oms + if abs(v_slip) > 1e-6: + sgn = np.sign(v_slip) + a_x = -sgn * mu_floor_sphere * g + a_om = +sgn * mu_floor_sphere * g * (m_s * r_s) / I_s + else: + a_x = 0.0; a_om = 0.0 + dy[IDX['Xs']] = Vxs; dy[IDX['Ys']] = 0.0; dy[IDX['ths']] = oms + dy[IDX['Vxs']] = a_x; dy[IDX['Vys']] = 0.0; dy[IDX['oms']] = a_om + return dy + + def make_events(): + def ev_bs(t, y): + if _settled[0] or t < _bs_cooldown_until[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_bs.terminal = True; ev_bs.direction = -1.0 + + def ev_bf(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 1].min()) + ev_bf.terminal = True; ev_bf.direction = -1.0 + + def ev_sl(t, y): + return (y[IDX['Xs']] - r_s) - x_left_wall + ev_sl.terminal = True; ev_sl.direction = -1.0 + + def ev_sr(t, y): + return x_right_wall - (y[IDX['Xs']] + r_s) + ev_sr.terminal = True; ev_sr.direction = -1.0 + + def ev_bl(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return float(pts[:, 0].min()) - x_left_wall + ev_bl.terminal = True; ev_bl.direction = -1.0 + + def ev_br(t, y): + if _settled[0]: return 1.0 + pts = block_corners(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']]) + return x_right_wall - float(pts[:, 0].max()) + ev_br.terminal = True; ev_br.direction = -1.0 + + def ev_sbs(t, y): + if not _settled[0]: return 1.0 + return block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + ev_sbs.terminal = True; ev_sbs.direction = -1.0 + return [ev_bs, ev_bf, ev_sl, ev_sr, ev_bl, ev_br, ev_sbs] + + EV_NAMES = ['block_sphere', 'block_floor', + 'sphere_left', 'sphere_right', + 'block_left', 'block_right', 'sphere_block_settled'] + + def resolve_block_sphere(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Xs, Ys = y[IDX['Xs']], y[IDX['Ys']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + Vxs, Vys, oms = y[IDX['Vxs']], y[IDX['Vys']], y[IDX['oms']] + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + pre_gap = block_sphere_gap(Xb, Yb, thb, Xs, Ys) + if pre_gap < 0.0: + push = -pre_gap + Xb += push * n_hat[0]; Yb += push * n_hat[1] + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb + cp, n_hat, r_b = block_sphere_contact(Xb, Yb, thb, Xs, Ys) + r_sv = cp - np.array([Xs, Ys]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + v_b_cp = np.array([Vxb - omb * r_b[1], Vyb + omb * r_b[0]]) + v_s_cp = np.array([Vxs - oms * r_sv[1], Vys + oms * r_sv[0]]) + v_rel = v_b_cp - v_s_cp + v_rel_n = float(v_rel @ n_hat) + if v_rel_n >= 0.0: return y + rb_x_n = r_b[0] * n_hat[1] - r_b[1] * n_hat[0] + rs_x_n = r_sv[0] * n_hat[1] - r_sv[1] * n_hat[0] + rb_x_t = r_b[0] * t_hat[1] - r_b[1] * t_hat[0] + rs_x_t = r_sv[0] * t_hat[1] - r_sv[1] * t_hat[0] + inv_m_n = (1.0/M_b + 1.0/m_s + rb_x_n*rb_x_n/I_b + rs_x_n*rs_x_n/I_s) + inv_m_t = (1.0/M_b + 1.0/m_s + rb_x_t*rb_x_t/I_b + rs_x_t*rs_x_t/I_s) + J_n = -(1.0 + e_bs) * v_rel_n / inv_m_n + v_rel_t = float(v_rel @ t_hat) + J_t_stick = -v_rel_t / inv_m_t + cone = mu_bs * abs(J_n) + J_t = J_t_stick if abs(J_t_stick) <= cone else np.sign(J_t_stick) * cone + J_vec_b = J_n * n_hat + J_t * t_hat + J_vec_s = -J_vec_b + y[IDX['Vxb']] = Vxb + J_vec_b[0]/M_b; y[IDX['Vyb']] = Vyb + J_vec_b[1]/M_b + y[IDX['omb']] = omb + (r_b[0]*J_vec_b[1] - r_b[1]*J_vec_b[0])/I_b + y[IDX['Vxs']] = Vxs + J_vec_s[0]/m_s; y[IDX['Vys']] = Vys + J_vec_s[1]/m_s + y[IDX['oms']] = oms + (r_sv[0]*J_vec_s[1] - r_sv[1]*J_vec_s[0])/I_s + eps_snap = 1e-6 + for _ in range(8): + gap = block_sphere_gap(y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + if gap >= eps_snap: break + _, n_hat_now, _ = block_sphere_contact( + y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']], + y[IDX['Xs']], y[IDX['Ys']]) + push = max(-gap, 0.0) + eps_snap + y[IDX['Xb']] += push * n_hat_now[0] + y[IDX['Yb']] += push * n_hat_now[1] + if y[IDX['Vys']] < 0.0: y[IDX['Vys']] = 0.0 + y[IDX['Vys']] = 0.0 + return y + + def resolve_block_floor(state): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + for _ in range(12): + applied = False + pts = block_corners(Xb, Yb, thb) + for k in range(4): + cy_ = pts[k, 1] + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + if cy_ > 1e-9 or v_cy > -1e-9: continue + inv_m_n = 1.0/M_b + r_x*r_x/I_b + inv_m_t = 1.0/M_b + r_y*r_y/I_b + J_n = -(1.0 + e_floor_block) * v_cy / inv_m_n + J_t_stick = -v_cx / inv_m_t + cone = mu_floor_block * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Vxb += J_t/M_b; Vyb += J_n/M_b + omb += (r_x*J_n - r_y*J_t)/I_b + Yb -= cy_ + applied = True + pts = block_corners(Xb, Yb, thb) + if not applied: break + pts = block_corners(Xb, Yb, thb) + low = float(pts[:, 1].min()) + if low < 1e-7: Yb += (1e-7 - low) + y[IDX['Xb']] = Xb; y[IDX['Yb']] = Yb; y[IDX['thb']] = thb + y[IDX['Vxb']] = Vxb; y[IDX['Vyb']] = Vyb; y[IDX['omb']] = omb + return y + + def resolve_sphere_wall(state, side): + y = state.copy() + Vx = y[IDX['Vxs']] + if side < 0 and Vx < 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_left_wall + r_s + elif side > 0 and Vx > 0.0: + y[IDX['Vxs']] = -e_wall * Vx + y[IDX['oms']] *= (1.0 - mu_wall) + y[IDX['Xs']] = x_right_wall - r_s + return y + + def resolve_block_wall(state, side): + y = state.copy() + Xb, Yb, thb = y[IDX['Xb']], y[IDX['Yb']], y[IDX['thb']] + Vxb, Vyb, omb = y[IDX['Vxb']], y[IDX['Vyb']], y[IDX['omb']] + pts = block_corners(Xb, Yb, thb) + if side < 0: + k = int(np.argmin(pts[:, 0])) + n_hat = np.array([+1.0, 0.0]) + else: + k = int(np.argmax(pts[:, 0])) + n_hat = np.array([-1.0, 0.0]) + t_hat = np.array([-n_hat[1], n_hat[0]]) + r_x = pts[k, 0] - Xb; r_y = pts[k, 1] - Yb + v_cx = Vxb - omb * r_y; v_cy = Vyb + omb * r_x + v_n = v_cx * n_hat[0] + v_cy * n_hat[1] + if v_n >= 0.0: return y + v_t = v_cx * t_hat[0] + v_cy * t_hat[1] + rxn = r_x * n_hat[1] - r_y * n_hat[0] + rxt = r_x * t_hat[1] - r_y * t_hat[0] + inv_m_n = 1.0/M_b + rxn*rxn/I_b + inv_m_t = 1.0/M_b + rxt*rxt/I_b + J_n = -(1.0 + e_wall) * v_n / inv_m_n + J_t_stick = -v_t / inv_m_t + J_t_stick *= (1.0 - mu_wall) + cone = mu_wall * abs(J_n) + J_t = max(-cone, min(cone, J_t_stick)) + Jx = J_n * n_hat[0] + J_t * t_hat[0] + Jy = J_n * n_hat[1] + J_t * t_hat[1] + y[IDX['Vxb']] = Vxb + Jx/M_b; y[IDX['Vyb']] = Vyb + Jy/M_b + y[IDX['omb']] = omb + (r_x*Jy - r_y*Jx)/I_b + pts2 = block_corners(Xb, Yb, thb) + if side < 0: + overlap = x_left_wall - pts2[k, 0] + y[IDX['Xb']] = Xb + max(overlap, 0.0) + else: + overlap = pts2[k, 0] - x_right_wall + y[IDX['Xb']] = Xb - max(overlap, 0.0) + return y + + events = make_events() + state = initial_state() + t_now = 0.0 + samples_t = [0.0]; samples_y = [state.copy()] + seg = 0; MAX_SEGS = 200 + chatter_count = 0; last_block_floor_t = -1e9 + bs_chatter = 0; last_bs_t = -1e9 + + while t_now < duration - 1e-9 and seg < MAX_SEGS: + seg += 1 + sub_eval = t_grid[(t_grid > t_now + 1e-12) & (t_grid <= duration + 1e-12)] + sol = solve_ivp(free_flight, (t_now, duration), state, + t_eval=sub_eval if sub_eval.size > 0 else None, + events=events, method="RK45", rtol=1e-9, atol=1e-11, + max_step=0.02, dense_output=True) + sol_t = np.asarray(sol.t) + if sol_t.size > 0: + for i in range(sol_t.size): + ti = float(sol_t[i]) + if ti <= t_now + 1e-12: continue + samples_t.append(ti); samples_y.append(sol.y[:, i].copy()) + triggered = -1; t_event = None; y_event = None + for k_, te in enumerate(sol.t_events): + if te.size > 0: + t_e = te[0] + if t_event is None or t_e < t_event: + t_event = t_e; y_event = sol.y_events[k_][0]; triggered = k_ + if triggered < 0: + state = sol.y[:, -1].copy(); t_now = duration; break + name = EV_NAMES[triggered] + state = y_event.copy(); t_now = float(t_event) + if name == 'block_sphere': + state = resolve_block_sphere(state) + if (t_now - last_bs_t) < 2e-3: bs_chatter += 1 + else: bs_chatter = 0 + last_bs_t = t_now + _bs_cooldown_until[0] = t_now + 1e-4 + if bs_chatter >= 6: + _, n_hat_esc, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v_b = np.array([state[IDX['Vxb']], state[IDX['Vyb']]]) + vn = float(v_b @ n_hat_esc) + if vn < 0.0: v_b = v_b - vn * n_hat_esc + v_b = v_b + 0.20 * n_hat_esc + state[IDX['Vxb']] = float(v_b[0]); state[IDX['Vyb']] = float(v_b[1]) + bs_chatter = 0 + elif name == 'block_floor': + state = resolve_block_floor(state) + elif name == 'sphere_left': + state = resolve_sphere_wall(state, -1) + elif name == 'sphere_right': + state = resolve_sphere_wall(state, +1) + elif name == 'block_left': + state = resolve_block_wall(state, -1) + elif name == 'block_right': + state = resolve_block_wall(state, +1) + elif name == 'sphere_block_settled': + cp, n_hat, _ = block_sphere_contact( + state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']], + state[IDX['Xs']], state[IDX['Ys']]) + v = np.array([state[IDX['Vxs']], state[IDX['Vys']]]) + vn = float(v @ n_hat) + if vn > 0.0: + v_new = v - (1.0 + e_wall) * vn * n_hat + state[IDX['Vxs']] = v_new[0]; state[IDX['Vys']] = v_new[1] + state[IDX['oms']] *= (1.0 - mu_wall) + new_centre = cp - r_s * n_hat + state[IDX['Xs']] = float(new_centre[0]); state[IDX['Ys']] = r_s + samples_t.append(t_now); samples_y.append(state.copy()) + pts_now = block_corners(state[IDX['Xb']], state[IDX['Yb']], state[IDX['thb']]) + block_low = float(pts_now[:, 1].min()) + block_KE = (0.5 * M_b * (state[IDX['Vxb']]**2 + state[IDX['Vyb']]**2) + + 0.5 * I_b * state[IDX['omb']]**2) + sphere_KE = (0.5 * m_s * state[IDX['Vxs']]**2 + + 0.5 * I_s * state[IDX['oms']]**2) + if name == 'block_floor': + if (t_now - last_block_floor_t) < 2e-3: chatter_count += 1 + else: chatter_count = 0 + last_block_floor_t = t_now + if chatter_count >= 3 and abs(block_low) < 0.05: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + chatter_count = 0; _settled[0] = True + if (block_KE + sphere_KE) < 5e-4 and abs(block_low) < 0.02: + th_round = round(state[IDX['thb']] / (np.pi/2.0)) * (np.pi/2.0) + state[IDX['thb']] = th_round; state[IDX['Yb']] = s/2.0 + state[IDX['Vxb']] = 0.0; state[IDX['Vyb']] = 0.0; state[IDX['omb']] = 0.0 + state[IDX['Vxs']] = 0.0; state[IDX['oms']] = 0.0 + break + + if t_now < duration - 1e-9: + last_state = state.copy() + for ti in t_grid: + if ti > t_now + 1e-12: + samples_t.append(float(ti)); samples_y.append(last_state.copy()) + samples_t = np.array(samples_t); samples_y = np.array(samples_y) + order = np.argsort(samples_t, kind='stable') + samples_t = samples_t[order]; samples_y = samples_y[order] + keep = np.concatenate(([True], np.diff(samples_t) > 1e-9)) + samples_t = samples_t[keep]; samples_y = samples_y[keep] + + def interp_frame(ti): + j = np.searchsorted(samples_t, ti) + if j <= 0: return samples_y[0].copy() + if j >= len(samples_t): return samples_y[-1].copy() + t0, t1 = samples_t[j-1], samples_t[j] + if t1 - t0 < 1e-12: return samples_y[j].copy() + a = (ti - t0) / (t1 - t0) + return (1.0 - a) * samples_y[j-1] + a * samples_y[j] + + frames_y = np.array([interp_frame(ti) for ti in t_grid]) + return t_grid, frames_y + +def render_video(t, y, output_path, fps=30): + frames_y = y.T + n_frames = frames_y.shape[0] + IDX = dict(Xb=0, Yb=1, thb=2, Vxb=3, Vyb=4, omb=5, + Xs=6, Ys=7, ths=8, Vxs=9, Vys=10, oms=11) + + fig_w, fig_h = 8.0, 5.0 + fig_aspect = fig_w / fig_h + x_lo_d = x_left_wall - 0.20; x_hi_d = x_right_wall + 0.20 + y_lo_d = -0.10; y_hi_d = h_drop + 0.30 + dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d + if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d + else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) + wall_thick = 0.10 + ax.add_patch(patches.Rectangle((x_left_wall - wall_thick, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.add_patch(patches.Rectangle((x_right_wall, 0.0), wall_thick, + h_drop + 0.50, facecolor="#4a4e69", + edgecolor="none", zorder=0)) + ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_left_wall, x_left_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + ax.plot([x_right_wall, x_right_wall], [0, h_drop + 0.50], + color="#adb5bd", lw=2.5, zorder=1) + + sphere_patch = patches.Circle((frames_y[0, IDX['Xs']], frames_y[0, IDX['Ys']]), + r_s, facecolor="#ffd166", + edgecolor="white", lw=1.5, zorder=3) + ax.add_patch(sphere_patch) + sphere_tick, = ax.plot([], [], color="#1a1a2e", lw=2.0, zorder=4) + block_patch = patches.Rectangle((frames_y[0, IDX['Xb']] - s/2.0, + frames_y[0, IDX['Yb']] - s/2.0), + s, s, facecolor="#e63946", + edgecolor="white", lw=1.5, zorder=5) + ax.add_patch(block_patch) + + def update(f): + Xs_f = float(frames_y[f, IDX['Xs']]); Ys_f = float(frames_y[f, IDX['Ys']]) + th_s = float(frames_y[f, IDX['ths']]) + sphere_patch.set_center((Xs_f, Ys_f)) + sphere_tick.set_data([Xs_f, Xs_f + r_s * np.cos(th_s)], + [Ys_f, Ys_f + r_s * np.sin(th_s)]) + Xb_f = float(frames_y[f, IDX['Xb']]); Yb_f = float(frames_y[f, IDX['Yb']]) + th_b = float(frames_y[f, IDX['thb']]) + block_patch.set_xy((Xb_f - s/2.0, Yb_f - s/2.0)) + transform = (Affine2D().rotate_around(Xb_f, Yb_f, th_b) + ax.transData) + block_patch.set_transform(transform) + return sphere_patch, sphere_tick, block_patch + + ani = animation.FuncAnimation(fig, update, frames=n_frames, + interval=1000.0/fps, blit=True) + ani.save(output_path, writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) + plt.close(fig) + + +t, frames_y = _run_sim() +render_video(t, frames_y.T, "video.mp4") +print("Saved video.mp4") diff --git a/scipy/block_on_sphere_basin/980/video.mp4 b/scipy/block_on_sphere_basin/980/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..618c701369860902e2b8b624a30c8cf8e0c988e8 --- /dev/null +++ b/scipy/block_on_sphere_basin/980/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eb8805b269c42dfdd2f4cd815ba8c572c771cb350dc5eb5d969e00052e53c397 +size 15066 diff --git a/scipy/block_strikes_spring_pair/000/cot.json b/scipy/block_strikes_spring_pair/000/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ba92218b1a4927991bcb161c9114077755eb0196 --- /dev/null +++ b/scipy/block_strikes_spring_pair/000/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.412, m2=0.520, m3=0.536 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=3.510 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=97.776 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.79). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.097).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.79 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 19.2468 rad/s; period T = 0.3265 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 19.2468, + "spring_period_s": 0.3265, + "deceleration_mug_m_s2": 0.9541, + "v0_initial_m_s": 3.510485427358959 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.46 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.33 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.41230822304009673, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.5202805281421533, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.5357252298547022, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 97.77573059883963, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 3.510485427358959, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7883137150773423, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.09735888357009231, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/000/params.json b/scipy/block_strikes_spring_pair/000/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c53400c68535ae7c8483f9c9535b6f36034b97ff --- /dev/null +++ b/scipy/block_strikes_spring_pair/000/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.41230822304009673, + "m2": 0.5202805281421533, + "m3": 0.5357252298547022, + "s": 0.18, + "k": 97.77573059883963, + "L0": 0.5, + "v0": 3.510485427358959, + "e_rest": 0.7883137150773423, + "mu_floor": 0.09735888357009231, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/000/simulation_code.py b/scipy/block_strikes_spring_pair/000/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..65dce79fc32916e739ef73561f4066877fc5e66b --- /dev/null +++ b/scipy/block_strikes_spring_pair/000/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.41230822304009673; m2 = 0.5202805281421533; m3 = 0.5357252298547022 +s = 0.18; k = 97.77573059883963; L0 = 0.5; v0 = 3.510485427358959 +e_rest = 0.7883137150773423; mu_floor = 0.09735888357009231 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/000/video.mp4 b/scipy/block_strikes_spring_pair/000/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..938237025bbb392b1925fdb6e5d543e868abbe24 --- /dev/null +++ b/scipy/block_strikes_spring_pair/000/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5d68bb45e944cdf52ed71299c8f0877e89db990edb362c31b325f132abde1c45 +size 8749 diff --git a/scipy/block_strikes_spring_pair/019/cot.json b/scipy/block_strikes_spring_pair/019/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..a9e99fb12a60abfeff2a4e3f34867a109aca86aa --- /dev/null +++ b/scipy/block_strikes_spring_pair/019/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.402, m2=0.497, m3=0.426 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=3.519 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=105.217 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.58). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.131).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.58 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 21.4227 rad/s; period T = 0.2933 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 21.4227, + "spring_period_s": 0.2933, + "deceleration_mug_m_s2": 1.2848, + "v0_initial_m_s": 3.519238122612895 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.45 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.29 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.402120425429807, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.4968483730012949, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.4257006402350615, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 105.21743079186149, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 3.519238122612895, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.5830240074369771, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.13109819951144389, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/019/params.json b/scipy/block_strikes_spring_pair/019/params.json new file mode 100644 index 0000000000000000000000000000000000000000..9395e43267fa98b9f9b087e1e48aebcfd118f968 --- /dev/null +++ b/scipy/block_strikes_spring_pair/019/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.402120425429807, + "m2": 0.4968483730012949, + "m3": 0.4257006402350615, + "s": 0.18, + "k": 105.21743079186149, + "L0": 0.5, + "v0": 3.519238122612895, + "e_rest": 0.5830240074369771, + "mu_floor": 0.13109819951144389, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/019/simulation_code.py b/scipy/block_strikes_spring_pair/019/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e790ed1dbad27a37418b71c4858d9c08d05d21e2 --- /dev/null +++ b/scipy/block_strikes_spring_pair/019/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.402120425429807; m2 = 0.4968483730012949; m3 = 0.4257006402350615 +s = 0.18; k = 105.21743079186149; L0 = 0.5; v0 = 3.519238122612895 +e_rest = 0.5830240074369771; mu_floor = 0.13109819951144389 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/019/video.mp4 b/scipy/block_strikes_spring_pair/019/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..ef751cff31e3af9139f85afc6b5e251adc9c5eaf --- /dev/null +++ b/scipy/block_strikes_spring_pair/019/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8ea5211fdd2903cbfa70e5462c85b8d2d03eecc41096b8159928da65db1064b0 +size 7776 diff --git a/scipy/block_strikes_spring_pair/114/cot.json b/scipy/block_strikes_spring_pair/114/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ce9176ace1bfa982a4c9bad57cbcaae455ea7e38 --- /dev/null +++ b/scipy/block_strikes_spring_pair/114/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.559, m2=0.485, m3=0.574 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=2.568 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=77.862 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.70). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.069).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.70 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 17.2133 rad/s; period T = 0.3650 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 17.2133, + "spring_period_s": 0.365, + "deceleration_mug_m_s2": 0.6737, + "v0_initial_m_s": 2.5678720901519783 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.62 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.37 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.5593913461649689, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.484638036538072, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.574047586353145, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 77.86227282595634, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 2.5678720901519783, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7044650034935876, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.06874418575443156, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/114/params.json b/scipy/block_strikes_spring_pair/114/params.json new file mode 100644 index 0000000000000000000000000000000000000000..cec6cae2a111d4ac43b817d594872b20a93d122c --- /dev/null +++ b/scipy/block_strikes_spring_pair/114/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.5593913461649689, + "m2": 0.484638036538072, + "m3": 0.574047586353145, + "s": 0.18, + "k": 77.86227282595634, + "L0": 0.5, + "v0": 2.5678720901519783, + "e_rest": 0.7044650034935876, + "mu_floor": 0.06874418575443156, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/114/simulation_code.py b/scipy/block_strikes_spring_pair/114/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..ef15046087e480e5cd5d101c2455f63c0dfae59b --- /dev/null +++ b/scipy/block_strikes_spring_pair/114/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.5593913461649689; m2 = 0.484638036538072; m3 = 0.574047586353145 +s = 0.18; k = 77.86227282595634; L0 = 0.5; v0 = 2.5678720901519783 +e_rest = 0.7044650034935876; mu_floor = 0.06874418575443156 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/114/video.mp4 b/scipy/block_strikes_spring_pair/114/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..0610fc0f649ab856e7d1def20542d159aa4cf836 --- /dev/null +++ b/scipy/block_strikes_spring_pair/114/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e12665e7bb58f2157a83fd861265a5882467815e35854c0579f7605c5f854aec +size 9033 diff --git a/scipy/block_strikes_spring_pair/156/cot.json b/scipy/block_strikes_spring_pair/156/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..575f426bf7b8156b8110a8c1e273a7733b11edd8 --- /dev/null +++ b/scipy/block_strikes_spring_pair/156/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.503, m2=0.539, m3=0.603 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=3.222 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=85.815 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.68). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.068).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.68 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 17.3670 rad/s; period T = 0.3618 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 17.367, + "spring_period_s": 0.3618, + "deceleration_mug_m_s2": 0.6649, + "v0_initial_m_s": 3.2220486658730265 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.50 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.36 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.5031256003294368, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.5390818622288224, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.6025265009392176, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 85.8146184870327, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 3.2220486658730265, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.6790526078811395, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.06785175941485416, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/156/params.json b/scipy/block_strikes_spring_pair/156/params.json new file mode 100644 index 0000000000000000000000000000000000000000..8906f8967e03a0c8a06b9b4a6b87bd2846f1addd --- /dev/null +++ b/scipy/block_strikes_spring_pair/156/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.5031256003294368, + "m2": 0.5390818622288224, + "m3": 0.6025265009392176, + "s": 0.18, + "k": 85.8146184870327, + "L0": 0.5, + "v0": 3.2220486658730265, + "e_rest": 0.6790526078811395, + "mu_floor": 0.06785175941485416, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/156/simulation_code.py b/scipy/block_strikes_spring_pair/156/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a9e4532d1d234043fcae67174e820555376c001b --- /dev/null +++ b/scipy/block_strikes_spring_pair/156/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.5031256003294368; m2 = 0.5390818622288224; m3 = 0.6025265009392176 +s = 0.18; k = 85.8146184870327; L0 = 0.5; v0 = 3.2220486658730265 +e_rest = 0.6790526078811395; mu_floor = 0.06785175941485416 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/156/video.mp4 b/scipy/block_strikes_spring_pair/156/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..718f75b42ae098933a595a58261013776501fc47 --- /dev/null +++ b/scipy/block_strikes_spring_pair/156/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cc0281530ee47e293be20e30521550e6cbb1d5a0ef6c494adb8b23b6515bc664 +size 9127 diff --git a/scipy/block_strikes_spring_pair/165/cot.json b/scipy/block_strikes_spring_pair/165/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..f7669d97a95ba28bdc6b518e7dd874d76f5b1783 --- /dev/null +++ b/scipy/block_strikes_spring_pair/165/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.599, m2=0.480, m3=0.681 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=2.866 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=85.116 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.84). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.105).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.84 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 17.3836 rad/s; period T = 0.3614 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 17.3836, + "spring_period_s": 0.3614, + "deceleration_mug_m_s2": 1.0278, + "v0_initial_m_s": 2.8664701812805222 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.56 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.36 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.5991335698470738, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.4804403052307937, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.6807769407148347, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 85.11592741243794, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 2.8664701812805222, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.8433496244391174, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.10487709046545202, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/165/params.json b/scipy/block_strikes_spring_pair/165/params.json new file mode 100644 index 0000000000000000000000000000000000000000..210e785cf8037bd830c241a05af281bcd67bb8db --- /dev/null +++ b/scipy/block_strikes_spring_pair/165/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.5991335698470738, + "m2": 0.4804403052307937, + "m3": 0.6807769407148347, + "s": 0.18, + "k": 85.11592741243794, + "L0": 0.5, + "v0": 2.8664701812805222, + "e_rest": 0.8433496244391174, + "mu_floor": 0.10487709046545202, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/165/simulation_code.py b/scipy/block_strikes_spring_pair/165/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..fc4c6e5dec2e64559ffdc3116ca13040269f8f08 --- /dev/null +++ b/scipy/block_strikes_spring_pair/165/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.5991335698470738; m2 = 0.4804403052307937; m3 = 0.6807769407148347 +s = 0.18; k = 85.11592741243794; L0 = 0.5; v0 = 2.8664701812805222 +e_rest = 0.8433496244391174; mu_floor = 0.10487709046545202 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/165/video.mp4 b/scipy/block_strikes_spring_pair/165/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a71ef30dbab621ce23508c73977c2e21547617fd --- /dev/null +++ b/scipy/block_strikes_spring_pair/165/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e6f65af6df514c2f46c4f0fc5611141b16d7331893eb75926f8539c2b4c32d63 +size 8748 diff --git a/scipy/block_strikes_spring_pair/203/cot.json b/scipy/block_strikes_spring_pair/203/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..857774111f6225125756822d0a23bc7e143d8a46 --- /dev/null +++ b/scipy/block_strikes_spring_pair/203/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.530, m2=0.484, m3=0.570 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=2.763 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=83.026 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.68). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.093).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.68 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 17.8124 rad/s; period T = 0.3527 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 17.8124, + "spring_period_s": 0.3527, + "deceleration_mug_m_s2": 0.9068, + "v0_initial_m_s": 2.7625857129290763 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.58 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.35 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.5295282370689245, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.48381835741411955, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.5699404157939799, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 83.02596797334509, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 2.7625857129290763, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.6779534989091123, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.09252865553114922, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/203/params.json b/scipy/block_strikes_spring_pair/203/params.json new file mode 100644 index 0000000000000000000000000000000000000000..6c561292fb99a97ad4320c02250f039bffbfd71b --- /dev/null +++ b/scipy/block_strikes_spring_pair/203/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.5295282370689245, + "m2": 0.48381835741411955, + "m3": 0.5699404157939799, + "s": 0.18, + "k": 83.02596797334509, + "L0": 0.5, + "v0": 2.7625857129290763, + "e_rest": 0.6779534989091123, + "mu_floor": 0.09252865553114922, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/203/simulation_code.py b/scipy/block_strikes_spring_pair/203/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..597bfaa8c5214e83af9359282fe0b71659086c89 --- /dev/null +++ b/scipy/block_strikes_spring_pair/203/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.5295282370689245; m2 = 0.48381835741411955; m3 = 0.5699404157939799 +s = 0.18; k = 83.02596797334509; L0 = 0.5; v0 = 2.7625857129290763 +e_rest = 0.6779534989091123; mu_floor = 0.09252865553114922 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/203/video.mp4 b/scipy/block_strikes_spring_pair/203/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..acdaef6f0958cc41838b1e2b2c32571a788b6319 --- /dev/null +++ b/scipy/block_strikes_spring_pair/203/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:28cec07df7163d340a4c816926955d7315374d307fbd5194cee475b6aaae2532 +size 8685 diff --git a/scipy/block_strikes_spring_pair/245/cot.json b/scipy/block_strikes_spring_pair/245/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..8214f345fa0741d5c580250761b47c57cbe2d68e --- /dev/null +++ b/scipy/block_strikes_spring_pair/245/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.647, m2=0.552, m3=0.522 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=2.884 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=75.089 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.75). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.068).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.75 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 16.7338 rad/s; period T = 0.3755 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 16.7338, + "spring_period_s": 0.3755, + "deceleration_mug_m_s2": 0.6621, + "v0_initial_m_s": 2.88379394423024 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.55 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.38 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.6468063945708598, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.5517086429454972, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.5217490954005902, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 75.08899054107529, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 2.88379394423024, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7478797261947387, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.06756604551071703, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/245/params.json b/scipy/block_strikes_spring_pair/245/params.json new file mode 100644 index 0000000000000000000000000000000000000000..24332b184e529cfdd5a4d172b13a399d1cab62fb --- /dev/null +++ b/scipy/block_strikes_spring_pair/245/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.6468063945708598, + "m2": 0.5517086429454972, + "m3": 0.5217490954005902, + "s": 0.18, + "k": 75.08899054107529, + "L0": 0.5, + "v0": 2.88379394423024, + "e_rest": 0.7478797261947387, + "mu_floor": 0.06756604551071703, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/245/simulation_code.py b/scipy/block_strikes_spring_pair/245/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..cbde807665aabfff74ad561ffc91b334bd4e4d0d --- /dev/null +++ b/scipy/block_strikes_spring_pair/245/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.6468063945708598; m2 = 0.5517086429454972; m3 = 0.5217490954005902 +s = 0.18; k = 75.08899054107529; L0 = 0.5; v0 = 2.88379394423024 +e_rest = 0.7478797261947387; mu_floor = 0.06756604551071703 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/245/video.mp4 b/scipy/block_strikes_spring_pair/245/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..5ce789f5f5cd009867ebb370e15586808925baf9 --- /dev/null +++ b/scipy/block_strikes_spring_pair/245/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:75f4177cd8e59de8369108c8660de5ce10601306fc805d0093aa827898a9f1df +size 9517 diff --git a/scipy/block_strikes_spring_pair/314/cot.json b/scipy/block_strikes_spring_pair/314/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ca16c6b1a19e0ce66ab5eeca7e383eb147166279 --- /dev/null +++ b/scipy/block_strikes_spring_pair/314/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.627, m2=0.469, m3=0.550 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=3.464 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=79.088 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.78). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.093).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.78 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 17.6781 rad/s; period T = 0.3554 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 17.6781, + "spring_period_s": 0.3554, + "deceleration_mug_m_s2": 0.9128, + "v0_initial_m_s": 3.4636690073817187 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.46 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.36 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.6273430070330152, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.4685843227619099, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.5502400268371977, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 79.0882119907307, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 3.4636690073817187, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.777069295165291, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.09313892835148493, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/314/params.json b/scipy/block_strikes_spring_pair/314/params.json new file mode 100644 index 0000000000000000000000000000000000000000..729d5c00be106a6d0ac34649955c961694682d19 --- /dev/null +++ b/scipy/block_strikes_spring_pair/314/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.6273430070330152, + "m2": 0.4685843227619099, + "m3": 0.5502400268371977, + "s": 0.18, + "k": 79.0882119907307, + "L0": 0.5, + "v0": 3.4636690073817187, + "e_rest": 0.777069295165291, + "mu_floor": 0.09313892835148493, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/314/simulation_code.py b/scipy/block_strikes_spring_pair/314/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..ba229f26cdc8cfd2a4e1a14a613bdbb4bbe37237 --- /dev/null +++ b/scipy/block_strikes_spring_pair/314/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.6273430070330152; m2 = 0.4685843227619099; m3 = 0.5502400268371977 +s = 0.18; k = 79.0882119907307; L0 = 0.5; v0 = 3.4636690073817187 +e_rest = 0.777069295165291; mu_floor = 0.09313892835148493 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/314/video.mp4 b/scipy/block_strikes_spring_pair/314/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..38ced05c3135690c96716e937c6dbe16fd53b9ea --- /dev/null +++ b/scipy/block_strikes_spring_pair/314/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5e6826f2fde2c1bda3dab0a76cbe36d70d9526f452608937638cdb8410f270d1 +size 8742 diff --git a/scipy/block_strikes_spring_pair/331/cot.json b/scipy/block_strikes_spring_pair/331/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..b6e44ec01f3da272043c0941cd339bb5de87c1de --- /dev/null +++ b/scipy/block_strikes_spring_pair/331/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.555, m2=0.413, m3=0.656 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=3.453 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=82.740 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.74). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.130).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.74 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 18.0657 rad/s; period T = 0.3478 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 18.0657, + "spring_period_s": 0.3478, + "deceleration_mug_m_s2": 1.2722, + "v0_initial_m_s": 3.4526086558993923 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.46 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.35 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.5549153444622559, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.41318360364658596, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.656039021070494, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 82.73954973746046, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 3.4526086558993923, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7435400765314604, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.12982048927993287, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/331/params.json b/scipy/block_strikes_spring_pair/331/params.json new file mode 100644 index 0000000000000000000000000000000000000000..0b8d42951f22bc658c45cd13f866a2792b4a7d22 --- /dev/null +++ b/scipy/block_strikes_spring_pair/331/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.5549153444622559, + "m2": 0.41318360364658596, + "m3": 0.656039021070494, + "s": 0.18, + "k": 82.73954973746046, + "L0": 0.5, + "v0": 3.4526086558993923, + "e_rest": 0.7435400765314604, + "mu_floor": 0.12982048927993287, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/331/simulation_code.py b/scipy/block_strikes_spring_pair/331/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a180c469c4029211ca415d38736ea1699ce788a9 --- /dev/null +++ b/scipy/block_strikes_spring_pair/331/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.5549153444622559; m2 = 0.41318360364658596; m3 = 0.656039021070494 +s = 0.18; k = 82.73954973746046; L0 = 0.5; v0 = 3.4526086558993923 +e_rest = 0.7435400765314604; mu_floor = 0.12982048927993287 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/331/video.mp4 b/scipy/block_strikes_spring_pair/331/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1c8549e9e9c8d4d839433133f7ff6cb98c11c9f5 --- /dev/null +++ b/scipy/block_strikes_spring_pair/331/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:17cdad657358cec6ea047e09f51620e772a0599ae846286a1e0f66643421c70f +size 8760 diff --git a/scipy/block_strikes_spring_pair/371/cot.json b/scipy/block_strikes_spring_pair/371/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..101b398e17347ce9858ec7c157a00bb4356b2965 --- /dev/null +++ b/scipy/block_strikes_spring_pair/371/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.619, m2=0.675, m3=0.411 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=3.360 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=87.995 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.84). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.086).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.84 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 18.5629 rad/s; period T = 0.3385 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 18.5629, + "spring_period_s": 0.3385, + "deceleration_mug_m_s2": 0.8472, + "v0_initial_m_s": 3.3596324699298243 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.48 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.34 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.6188308597984196, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.6754887267632255, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.41059149523026367, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 87.99457161575599, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 3.3596324699298243, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.8436531122758755, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.0864540482758559, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/371/params.json b/scipy/block_strikes_spring_pair/371/params.json new file mode 100644 index 0000000000000000000000000000000000000000..afbb906a4d44cc67d8205850d6faec4431dd6cd6 --- /dev/null +++ b/scipy/block_strikes_spring_pair/371/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.6188308597984196, + "m2": 0.6754887267632255, + "m3": 0.41059149523026367, + "s": 0.18, + "k": 87.99457161575599, + "L0": 0.5, + "v0": 3.3596324699298243, + "e_rest": 0.8436531122758755, + "mu_floor": 0.0864540482758559, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/371/simulation_code.py b/scipy/block_strikes_spring_pair/371/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a327eb5cd67b1eeb90954f1e5a0235073ff2ee21 --- /dev/null +++ b/scipy/block_strikes_spring_pair/371/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.6188308597984196; m2 = 0.6754887267632255; m3 = 0.41059149523026367 +s = 0.18; k = 87.99457161575599; L0 = 0.5; v0 = 3.3596324699298243 +e_rest = 0.8436531122758755; mu_floor = 0.0864540482758559 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/371/video.mp4 b/scipy/block_strikes_spring_pair/371/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..28377c8b999ce01397deb32bdc487c733f5e8f4d --- /dev/null +++ b/scipy/block_strikes_spring_pair/371/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:70c0038b4b890dac325aaba765eaafecc427b4902868c8fc8762e9274eec3e0c +size 8565 diff --git a/scipy/block_strikes_spring_pair/382/cot.json b/scipy/block_strikes_spring_pair/382/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..26543fe44d434daa7be86d765c0b05b60aed743c --- /dev/null +++ b/scipy/block_strikes_spring_pair/382/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.573, m2=0.640, m3=0.444 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=3.074 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=75.476 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.70). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.077).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.70 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 16.9633 rad/s; period T = 0.3704 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 16.9633, + "spring_period_s": 0.3704, + "deceleration_mug_m_s2": 0.7519, + "v0_initial_m_s": 3.07404253372341 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.52 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.37 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.5734282400193919, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.640215134744377, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.44434321096981777, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 75.4763021780202, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 3.07404253372341, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.6963353663383178, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.07672492982802265, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/382/params.json b/scipy/block_strikes_spring_pair/382/params.json new file mode 100644 index 0000000000000000000000000000000000000000..0e0b7b1eff48aa7d4b40b953cb5a5a69932035d7 --- /dev/null +++ b/scipy/block_strikes_spring_pair/382/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.5734282400193919, + "m2": 0.640215134744377, + "m3": 0.44434321096981777, + "s": 0.18, + "k": 75.4763021780202, + "L0": 0.5, + "v0": 3.07404253372341, + "e_rest": 0.6963353663383178, + "mu_floor": 0.07672492982802265, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/382/simulation_code.py b/scipy/block_strikes_spring_pair/382/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..16cb05d8211817a228191ec48af4d1e453b748c8 --- /dev/null +++ b/scipy/block_strikes_spring_pair/382/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.5734282400193919; m2 = 0.640215134744377; m3 = 0.44434321096981777 +s = 0.18; k = 75.4763021780202; L0 = 0.5; v0 = 3.07404253372341 +e_rest = 0.6963353663383178; mu_floor = 0.07672492982802265 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/382/video.mp4 b/scipy/block_strikes_spring_pair/382/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..07d5f3f54045e73816437cd3e37fcc76758aa64d --- /dev/null +++ b/scipy/block_strikes_spring_pair/382/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e7405108a18e25bebee780bb9235cccbd1d25ea88bc160f25b358b807f94c866 +size 8921 diff --git a/scipy/block_strikes_spring_pair/500/cot.json b/scipy/block_strikes_spring_pair/500/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..a0670d6be9a4fd12d753dcbfecfd8c1b561e46b3 --- /dev/null +++ b/scipy/block_strikes_spring_pair/500/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.639, m2=0.551, m3=0.456 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=3.255 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=79.559 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.62). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.137).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.62 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 17.8606 rad/s; period T = 0.3518 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 17.8606, + "spring_period_s": 0.3518, + "deceleration_mug_m_s2": 1.3436, + "v0_initial_m_s": 3.2553917280632074 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.49 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.35 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.6386586528171296, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.5506271235728905, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.4558958663866093, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 79.55908539455366, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 3.2553917280632074, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.6151266970883559, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.13710013818510264, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/500/params.json b/scipy/block_strikes_spring_pair/500/params.json new file mode 100644 index 0000000000000000000000000000000000000000..3003cd2dca8e6a0a084895736011f371746e3dc3 --- /dev/null +++ b/scipy/block_strikes_spring_pair/500/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.6386586528171296, + "m2": 0.5506271235728905, + "m3": 0.4558958663866093, + "s": 0.18, + "k": 79.55908539455366, + "L0": 0.5, + "v0": 3.2553917280632074, + "e_rest": 0.6151266970883559, + "mu_floor": 0.13710013818510264, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/500/simulation_code.py b/scipy/block_strikes_spring_pair/500/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d3aa1496197b00541cf3e1010bbe3c383ffa8dee --- /dev/null +++ b/scipy/block_strikes_spring_pair/500/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.6386586528171296; m2 = 0.5506271235728905; m3 = 0.4558958663866093 +s = 0.18; k = 79.55908539455366; L0 = 0.5; v0 = 3.2553917280632074 +e_rest = 0.6151266970883559; mu_floor = 0.13710013818510264 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/500/video.mp4 b/scipy/block_strikes_spring_pair/500/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..40a3b1221219fa422d3090a1236430bdfc098f8c --- /dev/null +++ b/scipy/block_strikes_spring_pair/500/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d17d08ffc625d54e5aae1b0458246b630f098097d2fb1f07f37182f0067f1f78 +size 8543 diff --git a/scipy/block_strikes_spring_pair/543/cot.json b/scipy/block_strikes_spring_pair/543/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..6af291984a4fa656c4ad93c39ea853f907746a4f --- /dev/null +++ b/scipy/block_strikes_spring_pair/543/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.680, m2=0.547, m3=0.435 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=2.831 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=60.069 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.82). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.107).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.82 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 15.7450 rad/s; period T = 0.3991 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 15.745, + "spring_period_s": 0.3991, + "deceleration_mug_m_s2": 1.0492, + "v0_initial_m_s": 2.831047832897196 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.57 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.40 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.6802145645916347, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.5470741205799047, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.4349494790553279, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 60.0686690585335, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 2.831047832897196, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.8224232506098574, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.10705633067101919, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/543/params.json b/scipy/block_strikes_spring_pair/543/params.json new file mode 100644 index 0000000000000000000000000000000000000000..3cd37532d99901983c70830bfcabd3b2949ae418 --- /dev/null +++ b/scipy/block_strikes_spring_pair/543/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.6802145645916347, + "m2": 0.5470741205799047, + "m3": 0.4349494790553279, + "s": 0.18, + "k": 60.0686690585335, + "L0": 0.5, + "v0": 2.831047832897196, + "e_rest": 0.8224232506098574, + "mu_floor": 0.10705633067101919, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/543/simulation_code.py b/scipy/block_strikes_spring_pair/543/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..85c5f650f02534d5b15fbd8a797bcdb19db0aa38 --- /dev/null +++ b/scipy/block_strikes_spring_pair/543/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.6802145645916347; m2 = 0.5470741205799047; m3 = 0.4349494790553279 +s = 0.18; k = 60.0686690585335; L0 = 0.5; v0 = 2.831047832897196 +e_rest = 0.8224232506098574; mu_floor = 0.10705633067101919 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/543/video.mp4 b/scipy/block_strikes_spring_pair/543/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f01b72eba53307189a4dc115637dd89a78efeab9 --- /dev/null +++ b/scipy/block_strikes_spring_pair/543/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8a1b5d727e7839c191d99b48feece9cc7db3784640cc692791e00336cff2fc9b +size 8206 diff --git a/scipy/block_strikes_spring_pair/552/cot.json b/scipy/block_strikes_spring_pair/552/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..3e51958663ab07a9afed1ac8c658d816e055807d --- /dev/null +++ b/scipy/block_strikes_spring_pair/552/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.528, m2=0.481, m3=0.422 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=3.516 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=66.799 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.75). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.119).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.75 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 17.2433 rad/s; period T = 0.3644 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 17.2433, + "spring_period_s": 0.3644, + "deceleration_mug_m_s2": 1.1695, + "v0_initial_m_s": 3.5156675539114186 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.46 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.36 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.5283142879817668, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.4807921408064695, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.421720991752753, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 66.79879604488889, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 3.5156675539114186, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7460795222871386, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.11933420292670444, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/552/params.json b/scipy/block_strikes_spring_pair/552/params.json new file mode 100644 index 0000000000000000000000000000000000000000..fa05336a813ab80088d00b33280376fd3ec3fff1 --- /dev/null +++ b/scipy/block_strikes_spring_pair/552/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.5283142879817668, + "m2": 0.4807921408064695, + "m3": 0.421720991752753, + "s": 0.18, + "k": 66.79879604488889, + "L0": 0.5, + "v0": 3.5156675539114186, + "e_rest": 0.7460795222871386, + "mu_floor": 0.11933420292670444, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/552/simulation_code.py b/scipy/block_strikes_spring_pair/552/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e7ae08fd3a6c8f34dafb4b7eea4b89fd302a4921 --- /dev/null +++ b/scipy/block_strikes_spring_pair/552/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.5283142879817668; m2 = 0.4807921408064695; m3 = 0.421720991752753 +s = 0.18; k = 66.79879604488889; L0 = 0.5; v0 = 3.5156675539114186 +e_rest = 0.7460795222871386; mu_floor = 0.11933420292670444 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/552/video.mp4 b/scipy/block_strikes_spring_pair/552/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..729f6b2a5690f4462c9a1b0674e205ac803a20f2 --- /dev/null +++ b/scipy/block_strikes_spring_pair/552/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bfb5e3ac7134f560aad647410ac30368f0d3456e8c789cdb5f1457dee8d646a6 +size 7466 diff --git a/scipy/block_strikes_spring_pair/613/cot.json b/scipy/block_strikes_spring_pair/613/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..b50e2c562de45c515454907a67f8930e6333003a --- /dev/null +++ b/scipy/block_strikes_spring_pair/613/cot.json @@ -0,0 +1,133 @@ +{ + "simulation": "block_strikes_spring_pair", + "physical_observation": "Three cubical blocks (m1=0.445, m2=0.631, m3=0.528 kg) on a frictional floor. Block 1 starts on the left moving right at v_0=3.203 m/s; blocks 2 and 3 are at rest, connected by a Hookean spring (k=109.660 N/m, L_0=0.500 m). On contact, blocks 1 and 2 undergo Newton restitution (e=0.66). Then 2 and 3 oscillate via the spring; all decelerate by Coulomb floor friction (mu=0.076).", + "physical_law": { + "name": "Newton's 2nd law + Hooke's law + Coulomb friction + 1D Newton restitution", + "statement": "Free-body Newton II per block; spring between 2-3; Coulomb friction; impulsive 1D collision at 1-2 contact.", + "formula": "v1' = ((m1-e m2)/(m1+m2)) v1; v2' = ((1+e) m1/(m1+m2)) v1" + }, + "ode_system": { + "state_variables": [ + "x_i, v_i for i=1,2,3" + ], + "equations_latex": [ + "\\dot{x}_i = v_i", + "\\dot{v}_2 = (k(x_3 - x_2 - L_0) - \\mu m_2 g sign(v_2)) / m_2", + "\\dot{v}_3 = (-k(x_3 - x_2 - L_0) - \\mu m_3 g sign(v_3)) / m_3", + "\\dot{v}_1 = -\\mu g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 \\to apply 1D restitution" + ], + "equations_python": [ + "x_dot_i = v_i", + "v_dot_2 = (k(x_3 - x_2 - L_0) - mu * m_2 * g * sign(v_2)) / m_2", + "v_dot_3 = (-k(x_3 - x_2 - L_0) - mu * m_3 * g * sign(v_3)) / m_3", + "v_dot_1 = - mu * g sign(v_1)", + "Event: x_1 + s/2 = x_2 - s/2 -> apply 1 * D * restitution" + ] + }, + "derivation_steps": [ + "Phase 1 (block 1 free flight): m1 dv1/dt = -mu m1 g sign(v1).", + "Phase 2 (impulsive collision): Newton restitution e=0.66 between blocks 1 and 2.", + "Phase 3 (coupled spring): m2 dv2/dt = k(x3-x2-L0) - friction; m3 dv3/dt = -k(x3-x2-L0) - friction.", + "Static-friction shelf via \\tanh(v/V_EPS) regularization for Lipschitz RHS.", + "Spring natural omega = \\sqrt(k(1/m2+1/m3)) = 19.5355 rad/s; period T = 0.3216 s." + ], + "physical_constants_derived": { + "spring_omega_rad_per_s": 19.5355, + "spring_period_s": 0.3216, + "deceleration_mug_m_s2": 0.7438, + "v0_initial_m_s": 3.203481620575179 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) with terminal contact event for 1-2 collision; \\tanh-regularized friction.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=5e-3", + "justification": "Hybrid: smooth ODE between collisions + impulsive restitution at contact." + }, + "code_validation": { + "ode_source": "Newton + Hooke + Coulomb + Newton restitution.", + "energy_check": "KE+PE non-increasing; momentum conserved at impulsive collisions.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Block 1 decelerates and strikes block 2 around t = 0.50 s.", + "Spring oscillation between blocks 2 and 3 with period ~0.32 s.", + "Possible re-collision between blocks 1 and 2 if spring rebounds 2 backward." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "m1": { + "value": 0.4446735150837814, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.6309398424877691, + "unit": "", + "description": "" + }, + "m3": { + "value": 0.5276395171464199, + "unit": "", + "description": "" + }, + "s": { + "value": 0.18, + "unit": "", + "description": "" + }, + "k": { + "value": 109.6603293420403, + "unit": "", + "description": "" + }, + "L0": { + "value": 0.5, + "unit": "", + "description": "" + }, + "v0": { + "value": 3.203481620575179, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.6623450762536893, + "unit": "", + "description": "" + }, + "mu_floor": { + "value": 0.0758934190985233, + "unit": "", + "description": "" + }, + "x1_0": { + "value": -1.5, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 0.1, + "unit": "", + "description": "" + }, + "x3_0": { + "value": 0.6, + "unit": "", + "description": "" + }, + "duration": { + "value": 12.0, + "unit": "", + "description": "" + }, + "fps": { + "value": 30, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/block_strikes_spring_pair/613/params.json b/scipy/block_strikes_spring_pair/613/params.json new file mode 100644 index 0000000000000000000000000000000000000000..4dc7674edd24d1f27254e927ae481568026bfa69 --- /dev/null +++ b/scipy/block_strikes_spring_pair/613/params.json @@ -0,0 +1,17 @@ +{ + "g": 9.8, + "m1": 0.4446735150837814, + "m2": 0.6309398424877691, + "m3": 0.5276395171464199, + "s": 0.18, + "k": 109.6603293420403, + "L0": 0.5, + "v0": 3.203481620575179, + "e_rest": 0.6623450762536893, + "mu_floor": 0.0758934190985233, + "x1_0": -1.5, + "x2_0": 0.1, + "x3_0": 0.6, + "duration": 12.0, + "fps": 30 +} \ No newline at end of file diff --git a/scipy/block_strikes_spring_pair/613/simulation_code.py b/scipy/block_strikes_spring_pair/613/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..8327297c966a636b3f9a91d746c16f6e0a6ce386 --- /dev/null +++ b/scipy/block_strikes_spring_pair/613/simulation_code.py @@ -0,0 +1,156 @@ +"""Block strikes spring pair - standalone script.""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8; m1 = 0.4446735150837814; m2 = 0.6309398424877691; m3 = 0.5276395171464199 +s = 0.18; k = 109.6603293420403; L0 = 0.5; v0 = 3.203481620575179 +e_rest = 0.6623450762536893; mu_floor = 0.0758934190985233 +x1_0 = -1.5; x2_0 = 0.1; x3_0 = 0.6 +duration = 12; fps = 30 +V_EPS = 1e-2 +F_STATIC = (mu_floor * m1 * g, mu_floor * m2 * g, mu_floor * m3 * g) + + +def friction_accel(v, F_other, m, F_stat): + return (F_other - F_stat * np.tanh(v / V_EPS)) / m + + +def phase1_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + return [v1, 0.0, 0.0, dv1, 0.0, 0.0] + + +def phase3_rhs(t, y): + x1, x2, x3, v1, v2, v3 = y + dxs = (x3 - x2) - L0 + F_s2 = +k * dxs; F_s3 = -k * dxs + dv1 = friction_accel(v1, 0.0, m1, F_STATIC[0]) + dv2 = friction_accel(v2, F_s2, m2, F_STATIC[1]) + dv3 = friction_accel(v3, F_s3, m3, F_STATIC[2]) + return [v1, v2, v3, dv1, dv2, dv3] + + +def make_contact_event(direction=+1): + def ev(t, y): + return (y[0] + s / 2.0) - (y[1] - s / 2.0) + ev.terminal = True; ev.direction = direction + return ev + + +t_eval_g = np.linspace(0.0, duration, int(duration * fps) + 1) +S = np.zeros((6, len(t_eval_g))) +state = np.array([x1_0, x2_0, x3_0, v0, 0.0, 0.0]) +S[:, 0] = state; last_idx = 1; t_cur = 0.0 + +contact_ev = make_contact_event(direction=+1) +sol1 = solve_ivp(phase1_rhs, (0.0, duration), state, + events=[contact_ev], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) +seg_end = sol1.t[-1] +while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol1.sol(t_eval_g[last_idx]); last_idx += 1 +state = sol1.y[:, -1].copy() +t_cur = seg_end + +if len(sol1.t_events[0]) > 0: + v1_pre = state[3] + v1_post = ((m1 - e_rest * m2) / (m1 + m2)) * v1_pre + v2_post = ((1.0 + e_rest) * m1 / (m1 + m2)) * v1_pre + state[3] = v1_post; state[4] = v2_post + +for _seg in range(50): + if t_cur >= duration - 1e-12: break + sol = solve_ivp(phase3_rhs, (t_cur, duration), state, + events=[make_contact_event(direction=+1)], dense_output=True, + method="RK45", rtol=1e-9, atol=1e-11, max_step=5e-3) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + if state[3] - state[4] > 0.0: + v1_pre, v2_pre = state[3], state[4] + state[3] = ((m1 - e_rest * m2) * v1_pre + (1.0 + e_rest) * m2 * v2_pre) / (m1 + m2) + state[4] = ((m2 - e_rest * m1) * v2_pre + (1.0 + e_rest) * m1 * v1_pre) / (m1 + m2) + else: + t_cur += 1e-9 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] +x1_arr, x2_arr, x3_arr = S[0], S[1], S[2] + +pad = 0.30 +x_lo_d = float(min(x1_arr.min(), x2_arr.min(), x3_arr.min())) - s/2.0 - pad +x_hi_d = float(max(x1_arr.max(), x2_arr.max(), x3_arr.max())) + s/2.0 + pad +y_lo_d = -0.30; y_hi_d = s + 0.40 +fig_w, fig_h = 9, 5 +fig_aspect = fig_w / fig_h +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.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, + facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.0, zorder=1) + +b1 = patches.Rectangle((x1_arr[0] - s/2, 0), s, s, + facecolor="#e63946", edgecolor="white", lw=1.5, zorder=4) +b2 = patches.Rectangle((x2_arr[0] - s/2, 0), s, s, + facecolor="#8ecae6", edgecolor="white", lw=1.5, zorder=4) +b3 = patches.Rectangle((x3_arr[0] - s/2, 0), s, s, + facecolor="#ffd166", edgecolor="white", lw=1.5, zorder=4) +ax.add_patch(b1); ax.add_patch(b2); ax.add_patch(b3) + +N_COILS = 12; COIL_W = 0.045; N_PTS = 4 * N_COILS + 2 + + +def coil_xy(p1, p2): + x1_, y1_ = p1; x2_, y2_ = p2 + dx, dy = x2_ - x1_, y2_ - y1_ + L = np.hypot(dx, dy) + if L < 1e-9: return np.array([x1_, x2_]), np.array([y1_, y2_]) + ux, uy = dx / L, dy / L; nx, ny = -uy, ux + margin = 0.05 + t_par = np.linspace(0.0, 1.0, N_PTS) + envelope = np.where((t_par < margin) | (t_par > 1.0 - margin), 0.0, + np.sin(N_COILS * 2.0 * np.pi * + (t_par - margin) / (1.0 - 2.0 * margin))) + xs = x1_ + t_par * dx + COIL_W * envelope * nx + ys = y1_ + t_par * dy + COIL_W * envelope * ny + return xs, ys + + +y_mid = s / 2.0 +xs0, ys0 = coil_xy((x2_arr[0] + s/2.0, y_mid), (x3_arr[0] - s/2.0, y_mid)) +spring_line, = ax.plot(xs0, ys0, color="#a8dadc", lw=2.2, zorder=3) + + +def update(f): + b1.set_x(x1_arr[f] - s/2.0) + b2.set_x(x2_arr[f] - s/2.0) + b3.set_x(x3_arr[f] - s/2.0) + xs, ys = coil_xy((x2_arr[f] + s/2.0, y_mid), (x3_arr[f] - s/2.0, y_mid)) + spring_line.set_data(xs, ys) + return (b1, b2, b3, spring_line) + + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), + interval=1000.0/fps, blit=True) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/block_strikes_spring_pair/613/video.mp4 b/scipy/block_strikes_spring_pair/613/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a0e0137446e308ae66b87cc9de1f708047956929 --- /dev/null +++ b/scipy/block_strikes_spring_pair/613/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1592f4534a5bd6182ab37ae6aa1f166f5b109bc7ff5bb222d041e2716ff93360 +size 8681 diff --git a/scipy/bouncing_rod/059/cot.json b/scipy/bouncing_rod/059/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..2276fac8307a7bad06290a2aaaf267de2575abf6 --- /dev/null +++ b/scipy/bouncing_rod/059/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=1.421 m and mass m=1.000 kg is released at angle theta0=33.81 deg above horizontal with COM height yc=2.902 m. It is given a small horizontal drift vx=-0.314 m/s, vertical velocity vy=0.000 m/s and spin omega=0.138 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.559 and Coulomb friction mu=0.163.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.5585045032617257) \\cdot vp_y / (1/1.0 + rx^2/0.168287)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.168287), -0.16274482771730092 \\cdot Jn, 0.16274482771730092 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.5585045032617257) * vp_y / (1/1.0 + rx**2/0.168287)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.168287), -0.16274482771730092 * Jn, 0.16274482771730092 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=1.421 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.16829 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.5585045032617257.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.16274482771730092. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.6881 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.715 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.16828731744275346, + "energy_loss_fraction_per_bounce_normal": 0.688072719836373, + "first_endpoint_contact_time_s_estimate": 0.7152190172170426 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.715 s.", + "Each bounce loses ~68.8% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.16) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 1.4210727670717787, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.5585045032617257, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.16274482771730092, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.9019349560280827, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.5901402780584502, + "unit": "", + "description": "" + }, + "vx0": { + "value": -0.31390694856435286, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.13814543323452977, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/059/params.json b/scipy/bouncing_rod/059/params.json new file mode 100644 index 0000000000000000000000000000000000000000..68dec741a9e704848b014152b4be2e39cc6b032a --- /dev/null +++ b/scipy/bouncing_rod/059/params.json @@ -0,0 +1,13 @@ +{ + "L": 1.4210727670717787, + "m": 1.0, + "g": 9.8, + "e_rest": 0.5585045032617257, + "mu": 0.16274482771730092, + "xc0": 0.0, + "yc0": 2.9019349560280827, + "theta0": 0.5901402780584502, + "vx0": -0.31390694856435286, + "vy0": 0.0, + "omega0": 0.13814543323452977 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/059/simulation_code.py b/scipy/bouncing_rod/059/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..dba86d2c11f530378a2329f5eeb207c9835e6917 --- /dev/null +++ b/scipy/bouncing_rod/059/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 1.4210727670717787 +m = 1.0 +g = 9.8 +e_rest = 0.5585045032617257 +mu = 0.16274482771730092 +xc0 = 0.0 +yc0 = 2.9019349560280827 +theta0 = 0.5901402780584502 +vx0 = -0.31390694856435286 +vy0 = 0.0 +omega0 = 0.13814543323452977 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/059/video.mp4 b/scipy/bouncing_rod/059/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..6a043734d56d611dc572d2e1bfb236982f59f725 --- /dev/null +++ b/scipy/bouncing_rod/059/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:39829e7b6b8b0885a24b767d827a818cbe2afc60404115c07fc659d31cd6914b +size 7563 diff --git a/scipy/bouncing_rod/070/cot.json b/scipy/bouncing_rod/070/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..2aa1bff4739a220dbd32ebb015e737fa37731917 --- /dev/null +++ b/scipy/bouncing_rod/070/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=1.285 m and mass m=1.000 kg is released at angle theta0=31.81 deg above horizontal with COM height yc=2.939 m. It is given a small horizontal drift vx=0.473 m/s, vertical velocity vy=0.000 m/s and spin omega=0.283 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.672 and Coulomb friction mu=0.339.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.6722103341646049) \\cdot vp_y / (1/1.0 + rx^2/0.137689)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.137689), -0.33897918915219194 \\cdot Jn, 0.33897918915219194 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.6722103341646049) * vp_y / (1/1.0 + rx**2/0.137689)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.137689), -0.33897918915219194 * Jn, 0.33897918915219194 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=1.285 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.13769 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.6722103341646049.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.33897918915219194. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.5481 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.728 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.1376892666736538, + "energy_loss_fraction_per_bounce_normal": 0.5481332666423102, + "first_endpoint_contact_time_s_estimate": 0.7284129538455385 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.728 s.", + "Each bounce loses ~54.8% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.34) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 1.2854070172843486, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.6722103341646049, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.33897918915219194, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.9386403400334036, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.5551893413675609, + "unit": "", + "description": "" + }, + "vx0": { + "value": 0.4733011300151423, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.2829629962493383, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/070/params.json b/scipy/bouncing_rod/070/params.json new file mode 100644 index 0000000000000000000000000000000000000000..f04cfbb78c3e99a731f07532d7cf54ec13f92015 --- /dev/null +++ b/scipy/bouncing_rod/070/params.json @@ -0,0 +1,13 @@ +{ + "L": 1.2854070172843486, + "m": 1.0, + "g": 9.8, + "e_rest": 0.6722103341646049, + "mu": 0.33897918915219194, + "xc0": 0.0, + "yc0": 2.9386403400334036, + "theta0": 0.5551893413675609, + "vx0": 0.4733011300151423, + "vy0": 0.0, + "omega0": 0.2829629962493383 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/070/simulation_code.py b/scipy/bouncing_rod/070/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..2742d693e571357a1b24b6e9fae387b5da465d0b --- /dev/null +++ b/scipy/bouncing_rod/070/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 1.2854070172843486 +m = 1.0 +g = 9.8 +e_rest = 0.6722103341646049 +mu = 0.33897918915219194 +xc0 = 0.0 +yc0 = 2.9386403400334036 +theta0 = 0.5551893413675609 +vx0 = 0.4733011300151423 +vy0 = 0.0 +omega0 = 0.2829629962493383 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/070/video.mp4 b/scipy/bouncing_rod/070/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..43b04668df9554eda3cf7a204eebc7597c7010b1 --- /dev/null +++ b/scipy/bouncing_rod/070/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:867d750b49333e4647de487191a4e8e986a0fe791ea938c2e9b211785d9c8c73 +size 10028 diff --git a/scipy/bouncing_rod/080/cot.json b/scipy/bouncing_rod/080/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..2e2b60c3d5ad0ebe154a50af141cc182a761be88 --- /dev/null +++ b/scipy/bouncing_rod/080/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=1.494 m and mass m=1.000 kg is released at angle theta0=16.32 deg above horizontal with COM height yc=2.515 m. It is given a small horizontal drift vx=0.742 m/s, vertical velocity vy=0.000 m/s and spin omega=-0.375 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.664 and Coulomb friction mu=0.156.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.6635992766125433) \\cdot vp_y / (1/1.0 + rx^2/0.186099)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.186099), -0.15562532442756202 \\cdot Jn, 0.15562532442756202 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.6635992766125433) * vp_y / (1/1.0 + rx**2/0.186099)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.186099), -0.15562532442756202 * Jn, 0.15562532442756202 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=1.494 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.18610 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.6635992766125433.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.15562532442756202. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.5596 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.686 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.18609853743080682, + "energy_loss_fraction_per_bounce_normal": 0.5596360000793092, + "first_endpoint_contact_time_s_estimate": 0.6858667759831439 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.686 s.", + "Each bounce loses ~56.0% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.16) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 1.4943836352053919, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.6635992766125433, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.15562532442756202, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.515035436538686, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.28490522556985587, + "unit": "", + "description": "" + }, + "vx0": { + "value": 0.7415067521447273, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": -0.37467113223039994, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/080/params.json b/scipy/bouncing_rod/080/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c761c74a3c39b637810e92e3316723442630235d --- /dev/null +++ b/scipy/bouncing_rod/080/params.json @@ -0,0 +1,13 @@ +{ + "L": 1.4943836352053919, + "m": 1.0, + "g": 9.8, + "e_rest": 0.6635992766125433, + "mu": 0.15562532442756202, + "xc0": 0.0, + "yc0": 2.515035436538686, + "theta0": 0.28490522556985587, + "vx0": 0.7415067521447273, + "vy0": 0.0, + "omega0": -0.37467113223039994 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/080/simulation_code.py b/scipy/bouncing_rod/080/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..660dba42b85de6b01047a453a3919173bb94c8c6 --- /dev/null +++ b/scipy/bouncing_rod/080/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 1.4943836352053919 +m = 1.0 +g = 9.8 +e_rest = 0.6635992766125433 +mu = 0.15562532442756202 +xc0 = 0.0 +yc0 = 2.515035436538686 +theta0 = 0.28490522556985587 +vx0 = 0.7415067521447273 +vy0 = 0.0 +omega0 = -0.37467113223039994 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/080/video.mp4 b/scipy/bouncing_rod/080/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7cb57ff4a7121e949b4af2ebfae22dde5e77274e --- /dev/null +++ b/scipy/bouncing_rod/080/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a90bcd5e2d665e377ff6f5628e8f871debf80a8ba34da08717b7bc89a821518b +size 8605 diff --git a/scipy/bouncing_rod/087/cot.json b/scipy/bouncing_rod/087/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..768c388de5433843f7fd83d74c99d346ee6a3b30 --- /dev/null +++ b/scipy/bouncing_rod/087/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=1.576 m and mass m=1.000 kg is released at angle theta0=31.02 deg above horizontal with COM height yc=2.314 m. It is given a small horizontal drift vx=0.475 m/s, vertical velocity vy=0.000 m/s and spin omega=-0.323 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.726 and Coulomb friction mu=0.133.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7260093551826017) \\cdot vp_y / (1/1.0 + rx^2/0.206969)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.206969), -0.13310227053177445 \\cdot Jn, 0.13310227053177445 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7260093551826017) * vp_y / (1/1.0 + rx**2/0.206969)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.206969), -0.13310227053177445 * Jn, 0.13310227053177445 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=1.576 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.20697 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.7260093551826017.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.13310227053177445. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.4729 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.624 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.2069691219676113, + "energy_loss_fraction_per_bounce_normal": 0.472910416187343, + "first_endpoint_contact_time_s_estimate": 0.6240285825347138 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.624 s.", + "Each bounce loses ~47.3% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.13) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 1.5759535093432597, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7260093551826017, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.13310227053177445, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.3142001180666227, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.5414146857002855, + "unit": "", + "description": "" + }, + "vx0": { + "value": 0.47533373483059527, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": -0.32250108681469375, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/087/params.json b/scipy/bouncing_rod/087/params.json new file mode 100644 index 0000000000000000000000000000000000000000..56612db4bd8d8f76040640de6da3e5d6800a1254 --- /dev/null +++ b/scipy/bouncing_rod/087/params.json @@ -0,0 +1,13 @@ +{ + "L": 1.5759535093432597, + "m": 1.0, + "g": 9.8, + "e_rest": 0.7260093551826017, + "mu": 0.13310227053177445, + "xc0": 0.0, + "yc0": 2.3142001180666227, + "theta0": 0.5414146857002855, + "vx0": 0.47533373483059527, + "vy0": 0.0, + "omega0": -0.32250108681469375 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/087/simulation_code.py b/scipy/bouncing_rod/087/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0dd452715f4d5f8bc7cb6cc478995db1167085e8 --- /dev/null +++ b/scipy/bouncing_rod/087/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 1.5759535093432597 +m = 1.0 +g = 9.8 +e_rest = 0.7260093551826017 +mu = 0.13310227053177445 +xc0 = 0.0 +yc0 = 2.3142001180666227 +theta0 = 0.5414146857002855 +vx0 = 0.47533373483059527 +vy0 = 0.0 +omega0 = -0.32250108681469375 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/087/video.mp4 b/scipy/bouncing_rod/087/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d12296dc904f8b219f1a91df76535fc9b372326d --- /dev/null +++ b/scipy/bouncing_rod/087/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:701c8a76fe7fb3a88524c11d163e4ccfd3572c2d11e26776ff7c0fa0c8d318a7 +size 11419 diff --git a/scipy/bouncing_rod/128/cot.json b/scipy/bouncing_rod/128/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..503a9805a5313be73ab40f1d1eda9c9f0fec611e --- /dev/null +++ b/scipy/bouncing_rod/128/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=1.337 m and mass m=1.000 kg is released at angle theta0=27.23 deg above horizontal with COM height yc=2.570 m. It is given a small horizontal drift vx=0.755 m/s, vertical velocity vy=0.000 m/s and spin omega=0.320 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.709 and Coulomb friction mu=0.304.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7086590939233005) \\cdot vp_y / (1/1.0 + rx^2/0.149015)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.149015), -0.30436589701444583 \\cdot Jn, 0.30436589701444583 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7086590939233005) * vp_y / (1/1.0 + rx**2/0.149015)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.149015), -0.30436589701444583 * Jn, 0.30436589701444583 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=1.337 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.14902 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.7086590939233005.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.30436589701444583. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.4978 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.680 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.14901538824973234, + "energy_loss_fraction_per_bounce_normal": 0.4978022885998068, + "first_endpoint_contact_time_s_estimate": 0.6797756281332658 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.680 s.", + "Each bounce loses ~49.8% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.30) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 1.3372302191458239, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7086590939233005, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.30436589701444583, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.5702012267967476, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.4752569959048142, + "unit": "", + "description": "" + }, + "vx0": { + "value": 0.7551068174681584, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.3197379692349722, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/128/params.json b/scipy/bouncing_rod/128/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e818918820b83b6667a1c6b9a6ce544a7b352b65 --- /dev/null +++ b/scipy/bouncing_rod/128/params.json @@ -0,0 +1,13 @@ +{ + "L": 1.3372302191458239, + "m": 1.0, + "g": 9.8, + "e_rest": 0.7086590939233005, + "mu": 0.30436589701444583, + "xc0": 0.0, + "yc0": 2.5702012267967476, + "theta0": 0.4752569959048142, + "vx0": 0.7551068174681584, + "vy0": 0.0, + "omega0": 0.3197379692349722 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/128/simulation_code.py b/scipy/bouncing_rod/128/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..f0209a9f33384b750f17b6ead4bd5ee087b8e554 --- /dev/null +++ b/scipy/bouncing_rod/128/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 1.3372302191458239 +m = 1.0 +g = 9.8 +e_rest = 0.7086590939233005 +mu = 0.30436589701444583 +xc0 = 0.0 +yc0 = 2.5702012267967476 +theta0 = 0.4752569959048142 +vx0 = 0.7551068174681584 +vy0 = 0.0 +omega0 = 0.3197379692349722 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/128/video.mp4 b/scipy/bouncing_rod/128/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d3ecb0f5761ef0de65b1c1ee2964c783182c21c9 --- /dev/null +++ b/scipy/bouncing_rod/128/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:54c5398ce0b15fd8cbcda626e9a40ebe4ff96390d35531bbf48faee7e9814fb7 +size 12965 diff --git a/scipy/bouncing_rod/131/cot.json b/scipy/bouncing_rod/131/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..42a794bf3d9ebae34016ffd0d9774bd695a53079 --- /dev/null +++ b/scipy/bouncing_rod/131/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=1.044 m and mass m=1.000 kg is released at angle theta0=32.65 deg above horizontal with COM height yc=2.074 m. It is given a small horizontal drift vx=-0.232 m/s, vertical velocity vy=0.000 m/s and spin omega=-0.318 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.755 and Coulomb friction mu=0.350.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7547081092104722) \\cdot vp_y / (1/1.0 + rx^2/0.090776)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.090776), -0.34957677483759797 \\cdot Jn, 0.34957677483759797 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7547081092104722) * vp_y / (1/1.0 + rx**2/0.090776)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.090776), -0.34957677483759797 * Jn, 0.34957677483759797 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=1.044 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.09078 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.7547081092104722.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.34957677483759797. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.4304 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.605 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.09077633725448513, + "energy_loss_fraction_per_bounce_normal": 0.430415669891954, + "first_endpoint_contact_time_s_estimate": 0.6048326666919766 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.605 s.", + "Each bounce loses ~43.0% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.35) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 1.04370304543669, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7547081092104722, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.34957677483759797, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.0740520144990464, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.5698035621811068, + "unit": "", + "description": "" + }, + "vx0": { + "value": -0.2315192760261396, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": -0.31824531613292, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/131/params.json b/scipy/bouncing_rod/131/params.json new file mode 100644 index 0000000000000000000000000000000000000000..770c5a54a01b281192931dcafae8c5b96725e011 --- /dev/null +++ b/scipy/bouncing_rod/131/params.json @@ -0,0 +1,13 @@ +{ + "L": 1.04370304543669, + "m": 1.0, + "g": 9.8, + "e_rest": 0.7547081092104722, + "mu": 0.34957677483759797, + "xc0": 0.0, + "yc0": 2.0740520144990464, + "theta0": 0.5698035621811068, + "vx0": -0.2315192760261396, + "vy0": 0.0, + "omega0": -0.31824531613292 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/131/simulation_code.py b/scipy/bouncing_rod/131/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c87f6084dda60561f64cdd30110d346655160769 --- /dev/null +++ b/scipy/bouncing_rod/131/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 1.04370304543669 +m = 1.0 +g = 9.8 +e_rest = 0.7547081092104722 +mu = 0.34957677483759797 +xc0 = 0.0 +yc0 = 2.0740520144990464 +theta0 = 0.5698035621811068 +vx0 = -0.2315192760261396 +vy0 = 0.0 +omega0 = -0.31824531613292 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/131/video.mp4 b/scipy/bouncing_rod/131/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..68560e6b54cb8e6ded2665df38a9b6789a9f200b --- /dev/null +++ b/scipy/bouncing_rod/131/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2e4a6f502b8805bbf8a2ea1b71034d7f15038258d9a4bde328bab4c1f493d9fc +size 16054 diff --git a/scipy/bouncing_rod/169/cot.json b/scipy/bouncing_rod/169/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..bfa7d8fa7e961d4ae923b699d0e298cf18c52fe8 --- /dev/null +++ b/scipy/bouncing_rod/169/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=1.442 m and mass m=1.000 kg is released at angle theta0=34.17 deg above horizontal with COM height yc=2.345 m. It is given a small horizontal drift vx=0.012 m/s, vertical velocity vy=0.000 m/s and spin omega=-0.390 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.710 and Coulomb friction mu=0.301.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7101781060443162) \\cdot vp_y / (1/1.0 + rx^2/0.173396)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.173396), -0.301254648238824 \\cdot Jn, 0.301254648238824 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7101781060443162) * vp_y / (1/1.0 + rx**2/0.173396)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.173396), -0.301254648238824 * Jn, 0.301254648238824 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=1.442 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.17340 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.7101781060443162.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.301254648238824. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.4956 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.629 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.17339576720780994, + "energy_loss_fraction_per_bounce_normal": 0.49564705769530804, + "first_endpoint_contact_time_s_estimate": 0.6292698495822463 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.629 s.", + "Each bounce loses ~49.6% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.30) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 1.442480227418636, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7101781060443162, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.301254648238824, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.345424459677372, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.596437845583823, + "unit": "", + "description": "" + }, + "vx0": { + "value": 0.012226853318900555, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": -0.3896456946175755, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/169/params.json b/scipy/bouncing_rod/169/params.json new file mode 100644 index 0000000000000000000000000000000000000000..8e0916292729d567ad0ecd1b9e362c705e0b5ff6 --- /dev/null +++ b/scipy/bouncing_rod/169/params.json @@ -0,0 +1,13 @@ +{ + "L": 1.442480227418636, + "m": 1.0, + "g": 9.8, + "e_rest": 0.7101781060443162, + "mu": 0.301254648238824, + "xc0": 0.0, + "yc0": 2.345424459677372, + "theta0": 0.596437845583823, + "vx0": 0.012226853318900555, + "vy0": 0.0, + "omega0": -0.3896456946175755 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/169/simulation_code.py b/scipy/bouncing_rod/169/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..8128780b1f83b086585d48b4d2d4024eca7125e5 --- /dev/null +++ b/scipy/bouncing_rod/169/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 1.442480227418636 +m = 1.0 +g = 9.8 +e_rest = 0.7101781060443162 +mu = 0.301254648238824 +xc0 = 0.0 +yc0 = 2.345424459677372 +theta0 = 0.596437845583823 +vx0 = 0.012226853318900555 +vy0 = 0.0 +omega0 = -0.3896456946175755 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/169/video.mp4 b/scipy/bouncing_rod/169/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..22ed0b44d9cda8b7cbbc54cff417070a78d2f725 --- /dev/null +++ b/scipy/bouncing_rod/169/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ed63b4918d8ba529b640f09e96fdd7b2401222e3d6238dfb16a987ae658fdbc4 +size 14916 diff --git a/scipy/bouncing_rod/246/cot.json b/scipy/bouncing_rod/246/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..32b6e6abea441e61a643fa45cb0ee0692acc9950 --- /dev/null +++ b/scipy/bouncing_rod/246/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=0.964 m and mass m=1.000 kg is released at angle theta0=33.68 deg above horizontal with COM height yc=2.221 m. It is given a small horizontal drift vx=0.324 m/s, vertical velocity vy=0.000 m/s and spin omega=-0.204 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.701 and Coulomb friction mu=0.336.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7006059445940394) \\cdot vp_y / (1/1.0 + rx^2/0.077483)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.077483), -0.3363113382144117 \\cdot Jn, 0.3363113382144117 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7006059445940394) * vp_y / (1/1.0 + rx**2/0.077483)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.077483), -0.3363113382144117 * Jn, 0.3363113382144117 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=0.964 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.07748 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.7006059445940394.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.3363113382144117. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.5092 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.631 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.07748266806437105, + "energy_loss_fraction_per_bounce_normal": 0.5091513103994938, + "first_endpoint_contact_time_s_estimate": 0.6314771219070009 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.631 s.", + "Each bounce loses ~50.9% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.34) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 0.9642572357895235, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7006059445940394, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.3363113382144117, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.2213025482303994, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.5878163215759915, + "unit": "", + "description": "" + }, + "vx0": { + "value": 0.3243540991720635, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": -0.20446224917270556, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/246/params.json b/scipy/bouncing_rod/246/params.json new file mode 100644 index 0000000000000000000000000000000000000000..4a0bfc70a2a9299826f951e2637941d7236bc2c4 --- /dev/null +++ b/scipy/bouncing_rod/246/params.json @@ -0,0 +1,13 @@ +{ + "L": 0.9642572357895235, + "m": 1.0, + "g": 9.8, + "e_rest": 0.7006059445940394, + "mu": 0.3363113382144117, + "xc0": 0.0, + "yc0": 2.2213025482303994, + "theta0": 0.5878163215759915, + "vx0": 0.3243540991720635, + "vy0": 0.0, + "omega0": -0.20446224917270556 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/246/simulation_code.py b/scipy/bouncing_rod/246/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..ee96636089d6c72053d7ee386fb3541bc872b69a --- /dev/null +++ b/scipy/bouncing_rod/246/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 0.9642572357895235 +m = 1.0 +g = 9.8 +e_rest = 0.7006059445940394 +mu = 0.3363113382144117 +xc0 = 0.0 +yc0 = 2.2213025482303994 +theta0 = 0.5878163215759915 +vx0 = 0.3243540991720635 +vy0 = 0.0 +omega0 = -0.20446224917270556 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/246/video.mp4 b/scipy/bouncing_rod/246/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d8ed95f2d84752eb3fee0bbe8aa20e2fbd32040b --- /dev/null +++ b/scipy/bouncing_rod/246/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3f73b5134eee09c5e59986d58dfd184b7a14f7b7c65242f3b11bfd03e434200f +size 10326 diff --git a/scipy/bouncing_rod/271/cot.json b/scipy/bouncing_rod/271/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..10aa76a21db899d68aab69cacfc0cdb017539b75 --- /dev/null +++ b/scipy/bouncing_rod/271/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=1.322 m and mass m=1.000 kg is released at angle theta0=38.40 deg above horizontal with COM height yc=2.467 m. It is given a small horizontal drift vx=0.312 m/s, vertical velocity vy=0.000 m/s and spin omega=-0.283 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.734 and Coulomb friction mu=0.184.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7340231748893771) \\cdot vp_y / (1/1.0 + rx^2/0.145534)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.145534), -0.18365648358003628 \\cdot Jn, 0.18365648358003628 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7340231748893771) * vp_y / (1/1.0 + rx**2/0.145534)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.145534), -0.18365648358003628 * Jn, 0.18365648358003628 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=1.322 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.14553 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.7340231748893771.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.18365648358003628. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.4612 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.648 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.14553367999278496, + "energy_loss_fraction_per_bounce_normal": 0.461209978725319, + "first_endpoint_contact_time_s_estimate": 0.6478881481310776 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.648 s.", + "Each bounce loses ~46.1% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.18) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 1.3215158568528111, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7340231748893771, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.18365648358003628, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.467215846104368, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.6701449632566182, + "unit": "", + "description": "" + }, + "vx0": { + "value": 0.31171964798045404, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": -0.2832024880950602, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/271/params.json b/scipy/bouncing_rod/271/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c190fbc52b98e2f14c21d993ee6f32b8ca3c177d --- /dev/null +++ b/scipy/bouncing_rod/271/params.json @@ -0,0 +1,13 @@ +{ + "L": 1.3215158568528111, + "m": 1.0, + "g": 9.8, + "e_rest": 0.7340231748893771, + "mu": 0.18365648358003628, + "xc0": 0.0, + "yc0": 2.467215846104368, + "theta0": 0.6701449632566182, + "vx0": 0.31171964798045404, + "vy0": 0.0, + "omega0": -0.2832024880950602 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/271/simulation_code.py b/scipy/bouncing_rod/271/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0df6eb8d4d1ae5bdf8bfd9e9793afff9ea872c58 --- /dev/null +++ b/scipy/bouncing_rod/271/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 1.3215158568528111 +m = 1.0 +g = 9.8 +e_rest = 0.7340231748893771 +mu = 0.18365648358003628 +xc0 = 0.0 +yc0 = 2.467215846104368 +theta0 = 0.6701449632566182 +vx0 = 0.31171964798045404 +vy0 = 0.0 +omega0 = -0.2832024880950602 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/271/video.mp4 b/scipy/bouncing_rod/271/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..36aa6452e59295625876910d8bcaffb15e3777ed --- /dev/null +++ b/scipy/bouncing_rod/271/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3850e4939f443e294199817556db22a73268257e14dfa16db03671914a5f6837 +size 11061 diff --git a/scipy/bouncing_rod/486/cot.json b/scipy/bouncing_rod/486/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..01d160543917b00ec1ac378015328f98bbd87d8c --- /dev/null +++ b/scipy/bouncing_rod/486/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=0.991 m and mass m=1.000 kg is released at angle theta0=34.59 deg above horizontal with COM height yc=2.068 m. It is given a small horizontal drift vx=0.798 m/s, vertical velocity vy=0.000 m/s and spin omega=-0.179 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.765 and Coulomb friction mu=0.346.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7654642654197188) \\cdot vp_y / (1/1.0 + rx^2/0.081870)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.081870), -0.3458317492687683 \\cdot Jn, 0.3458317492687683 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7654642654197188) * vp_y / (1/1.0 + rx**2/0.081870)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.081870), -0.3458317492687683 * Jn, 0.3458317492687683 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=0.991 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.08187 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.7654642654197188.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.3458317492687683. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.4141 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.604 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.08187048519340076, + "energy_loss_fraction_per_bounce_normal": 0.41406445836545025, + "first_endpoint_contact_time_s_estimate": 0.6038432960241046 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.604 s.", + "Each bounce loses ~41.4% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.35) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 0.991184050679191, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7654642654197188, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.3458317492687683, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.0680169017800036, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.6037052586433709, + "unit": "", + "description": "" + }, + "vx0": { + "value": 0.7979864142219261, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": -0.17921861923424837, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/486/params.json b/scipy/bouncing_rod/486/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5074eb0d41db431eeae40468158199275f88b8ae --- /dev/null +++ b/scipy/bouncing_rod/486/params.json @@ -0,0 +1,13 @@ +{ + "L": 0.991184050679191, + "m": 1.0, + "g": 9.8, + "e_rest": 0.7654642654197188, + "mu": 0.3458317492687683, + "xc0": 0.0, + "yc0": 2.0680169017800036, + "theta0": 0.6037052586433709, + "vx0": 0.7979864142219261, + "vy0": 0.0, + "omega0": -0.17921861923424837 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/486/simulation_code.py b/scipy/bouncing_rod/486/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..7250a484eb5f646e3eec5f0a98c9271bda86338a --- /dev/null +++ b/scipy/bouncing_rod/486/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 0.991184050679191 +m = 1.0 +g = 9.8 +e_rest = 0.7654642654197188 +mu = 0.3458317492687683 +xc0 = 0.0 +yc0 = 2.0680169017800036 +theta0 = 0.6037052586433709 +vx0 = 0.7979864142219261 +vy0 = 0.0 +omega0 = -0.17921861923424837 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/486/video.mp4 b/scipy/bouncing_rod/486/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..655148045056177818d2c8c19979ba9f72374b38 --- /dev/null +++ b/scipy/bouncing_rod/486/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a5490d436a4c6df02d8b07b9e69c7e5aa87035bb6f6758c0ee5b23beadfe729e +size 12369 diff --git a/scipy/bouncing_rod/497/cot.json b/scipy/bouncing_rod/497/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..92b0f85953771651f37f73ff9d427acf73782de3 --- /dev/null +++ b/scipy/bouncing_rod/497/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=0.821 m and mass m=1.000 kg is released at angle theta0=29.99 deg above horizontal with COM height yc=2.753 m. It is given a small horizontal drift vx=-0.256 m/s, vertical velocity vy=0.000 m/s and spin omega=0.563 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.606 and Coulomb friction mu=0.277.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.6059017235173947) \\cdot vp_y / (1/1.0 + rx^2/0.056191)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.056191), -0.27689036893280566 \\cdot Jn, 0.27689036893280566 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.6059017235173947) * vp_y / (1/1.0 + rx**2/0.056191)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.056191), -0.27689036893280566 * Jn, 0.27689036893280566 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=0.821 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.05619 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.6059017235173947.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.27689036893280566. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.6329 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.721 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.05619144955683497, + "energy_loss_fraction_per_bounce_normal": 0.6328831014386507, + "first_endpoint_contact_time_s_estimate": 0.7210214341920343 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.721 s.", + "Each bounce loses ~63.3% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.28) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 0.8211561329503785, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.6059017235173947, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.27689036893280566, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.752589219795076, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.5233958311133999, + "unit": "", + "description": "" + }, + "vx0": { + "value": -0.2564386711480231, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.5628722014174649, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/497/params.json b/scipy/bouncing_rod/497/params.json new file mode 100644 index 0000000000000000000000000000000000000000..8d5c500fc1a141d4b4e12d94d999d88ebee75e0d --- /dev/null +++ b/scipy/bouncing_rod/497/params.json @@ -0,0 +1,13 @@ +{ + "L": 0.8211561329503785, + "m": 1.0, + "g": 9.8, + "e_rest": 0.6059017235173947, + "mu": 0.27689036893280566, + "xc0": 0.0, + "yc0": 2.752589219795076, + "theta0": 0.5233958311133999, + "vx0": -0.2564386711480231, + "vy0": 0.0, + "omega0": 0.5628722014174649 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/497/simulation_code.py b/scipy/bouncing_rod/497/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..f0ef185df86ae97d4866394843a035b6916b6832 --- /dev/null +++ b/scipy/bouncing_rod/497/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 0.8211561329503785 +m = 1.0 +g = 9.8 +e_rest = 0.6059017235173947 +mu = 0.27689036893280566 +xc0 = 0.0 +yc0 = 2.752589219795076 +theta0 = 0.5233958311133999 +vx0 = -0.2564386711480231 +vy0 = 0.0 +omega0 = 0.5628722014174649 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/497/video.mp4 b/scipy/bouncing_rod/497/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3e1770aba419b59e3dd30feba281cc2d82af2f45 --- /dev/null +++ b/scipy/bouncing_rod/497/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ac5166cedd9459901b35400bae6fea53ba74ce2a93a38884711717fc1e650774 +size 14170 diff --git a/scipy/bouncing_rod/545/cot.json b/scipy/bouncing_rod/545/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..6f6430b466a742ebfbeafa9e962b6ec368f8d0d7 --- /dev/null +++ b/scipy/bouncing_rod/545/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=1.550 m and mass m=1.000 kg is released at angle theta0=25.35 deg above horizontal with COM height yc=2.906 m. It is given a small horizontal drift vx=0.342 m/s, vertical velocity vy=0.000 m/s and spin omega=-0.474 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.727 and Coulomb friction mu=0.296.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7266763254868277) \\cdot vp_y / (1/1.0 + rx^2/0.200088)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.200088), -0.2958665166818626 \\cdot Jn, 0.2958665166818626 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.7266763254868277) * vp_y / (1/1.0 + rx**2/0.200088)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.200088), -0.2958665166818626 * Jn, 0.2958665166818626 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=1.550 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.20009 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.7266763254868277.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.2958665166818626. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.4719 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.725 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.20008831950546604, + "energy_loss_fraction_per_bounce_normal": 0.47194151797696204, + "first_endpoint_contact_time_s_estimate": 0.7248674739255319 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.725 s.", + "Each bounce loses ~47.2% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.30) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 1.5495353607019082, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.7266763254868277, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.2958665166818626, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.906375704716468, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.4424988392642486, + "unit": "", + "description": "" + }, + "vx0": { + "value": 0.34233936449011504, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": -0.4737336180986604, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/545/params.json b/scipy/bouncing_rod/545/params.json new file mode 100644 index 0000000000000000000000000000000000000000..82c40eff7282768f29113f89241c24fb04773236 --- /dev/null +++ b/scipy/bouncing_rod/545/params.json @@ -0,0 +1,13 @@ +{ + "L": 1.5495353607019082, + "m": 1.0, + "g": 9.8, + "e_rest": 0.7266763254868277, + "mu": 0.2958665166818626, + "xc0": 0.0, + "yc0": 2.906375704716468, + "theta0": 0.4424988392642486, + "vx0": 0.34233936449011504, + "vy0": 0.0, + "omega0": -0.4737336180986604 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/545/simulation_code.py b/scipy/bouncing_rod/545/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..ca37c9e4717d1553d937c9026d7f6b519fe569c4 --- /dev/null +++ b/scipy/bouncing_rod/545/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 1.5495353607019082 +m = 1.0 +g = 9.8 +e_rest = 0.7266763254868277 +mu = 0.2958665166818626 +xc0 = 0.0 +yc0 = 2.906375704716468 +theta0 = 0.4424988392642486 +vx0 = 0.34233936449011504 +vy0 = 0.0 +omega0 = -0.4737336180986604 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/545/video.mp4 b/scipy/bouncing_rod/545/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..45c305d1907c4554912155f7bd7cb1931f0fca56 --- /dev/null +++ b/scipy/bouncing_rod/545/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ead3b2028920f30f4255ed08fc5e6ba8f149c66c4b0de2350606444d387e495e +size 12208 diff --git a/scipy/bouncing_rod/562/cot.json b/scipy/bouncing_rod/562/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..bada6caccfa698a9be3112b00a689848ac79ea94 --- /dev/null +++ b/scipy/bouncing_rod/562/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=1.074 m and mass m=1.000 kg is released at angle theta0=23.71 deg above horizontal with COM height yc=2.548 m. It is given a small horizontal drift vx=0.190 m/s, vertical velocity vy=0.000 m/s and spin omega=0.390 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.677 and Coulomb friction mu=0.309.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.677252557543671) \\cdot vp_y / (1/1.0 + rx^2/0.096048)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.096048), -0.3089653389225234 \\cdot Jn, 0.3089653389225234 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.677252557543671) * vp_y / (1/1.0 + rx**2/0.096048)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.096048), -0.3089653389225234 * Jn, 0.3089653389225234 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=1.074 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.09605 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.677252557543671.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.3089653389225234. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.5413 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.690 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.09604836454240949, + "energy_loss_fraction_per_bounce_normal": 0.5413289733005566, + "first_endpoint_contact_time_s_estimate": 0.6898919668914929 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.690 s.", + "Each bounce loses ~54.1% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.31) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 1.073582961167377, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.677252557543671, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.3089653389225234, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.548001111563499, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.413804643529163, + "unit": "", + "description": "" + }, + "vx0": { + "value": 0.1902521962088909, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.3900008799953182, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/562/params.json b/scipy/bouncing_rod/562/params.json new file mode 100644 index 0000000000000000000000000000000000000000..4120f0e23b81dfb611489e8a09c6bf8e83977e78 --- /dev/null +++ b/scipy/bouncing_rod/562/params.json @@ -0,0 +1,13 @@ +{ + "L": 1.073582961167377, + "m": 1.0, + "g": 9.8, + "e_rest": 0.677252557543671, + "mu": 0.3089653389225234, + "xc0": 0.0, + "yc0": 2.548001111563499, + "theta0": 0.413804643529163, + "vx0": 0.1902521962088909, + "vy0": 0.0, + "omega0": 0.3900008799953182 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/562/simulation_code.py b/scipy/bouncing_rod/562/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..5bff78132788712bf020f4274305f47669199a75 --- /dev/null +++ b/scipy/bouncing_rod/562/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 1.073582961167377 +m = 1.0 +g = 9.8 +e_rest = 0.677252557543671 +mu = 0.3089653389225234 +xc0 = 0.0 +yc0 = 2.548001111563499 +theta0 = 0.413804643529163 +vx0 = 0.1902521962088909 +vy0 = 0.0 +omega0 = 0.3900008799953182 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/562/video.mp4 b/scipy/bouncing_rod/562/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..6a2a10e015126b2ffc1e7eb533358388cd22df48 --- /dev/null +++ b/scipy/bouncing_rod/562/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a0c81fe91bc64b544820b54c2f4d97079ddcc879b95bd9dc3a5ea277857a0530 +size 10907 diff --git a/scipy/bouncing_rod/580/cot.json b/scipy/bouncing_rod/580/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..306f8f8ccd837e354d7edb9e3fe2538842355182 --- /dev/null +++ b/scipy/bouncing_rod/580/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=0.974 m and mass m=1.000 kg is released at angle theta0=13.53 deg above horizontal with COM height yc=2.878 m. It is given a small horizontal drift vx=-0.167 m/s, vertical velocity vy=0.000 m/s and spin omega=0.058 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.594 and Coulomb friction mu=0.327.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.5942276003336568) \\cdot vp_y / (1/1.0 + rx^2/0.079132)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.079132), -0.32653828524243184 \\cdot Jn, 0.32653828524243184 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.5942276003336568) * vp_y / (1/1.0 + rx**2/0.079132)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.079132), -0.32653828524243184 * Jn, 0.32653828524243184 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=0.974 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.07913 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.5942276003336568.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.32653828524243184. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.6469 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.751 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.07913162846334436, + "energy_loss_fraction_per_bounce_normal": 0.6468935590017038, + "first_endpoint_contact_time_s_estimate": 0.7510554786080159 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.751 s.", + "Each bounce loses ~64.7% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.33) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 0.9744637199814739, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.5942276003336568, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.32653828524243184, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.877987912075932, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.2361105747094807, + "unit": "", + "description": "" + }, + "vx0": { + "value": -0.16730554037015577, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.05809972821777476, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/580/params.json b/scipy/bouncing_rod/580/params.json new file mode 100644 index 0000000000000000000000000000000000000000..480471ff881f3a3722d12999c07ffc894ae27b47 --- /dev/null +++ b/scipy/bouncing_rod/580/params.json @@ -0,0 +1,13 @@ +{ + "L": 0.9744637199814739, + "m": 1.0, + "g": 9.8, + "e_rest": 0.5942276003336568, + "mu": 0.32653828524243184, + "xc0": 0.0, + "yc0": 2.877987912075932, + "theta0": 0.2361105747094807, + "vx0": -0.16730554037015577, + "vy0": 0.0, + "omega0": 0.05809972821777476 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/580/simulation_code.py b/scipy/bouncing_rod/580/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d77bfd47c9271a1bc5186b9bcd0e8cc284010230 --- /dev/null +++ b/scipy/bouncing_rod/580/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 0.9744637199814739 +m = 1.0 +g = 9.8 +e_rest = 0.5942276003336568 +mu = 0.32653828524243184 +xc0 = 0.0 +yc0 = 2.877987912075932 +theta0 = 0.2361105747094807 +vx0 = -0.16730554037015577 +vy0 = 0.0 +omega0 = 0.05809972821777476 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/580/video.mp4 b/scipy/bouncing_rod/580/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..55cccd32b4c3b8cbe001b2656b3cea693035a6c3 --- /dev/null +++ b/scipy/bouncing_rod/580/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:10780d8615aa70e875f5575043ed29fbee94d498658e6455c62ec3467cd9090f +size 8123 diff --git a/scipy/bouncing_rod/675/cot.json b/scipy/bouncing_rod/675/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..956eff41360aeeb4e3c21ad9f7fc966a416e5a7c --- /dev/null +++ b/scipy/bouncing_rod/675/cot.json @@ -0,0 +1,131 @@ +{ + "simulation": "bouncing_rod", + "physical_observation": "A thin rigid rod of length L=1.101 m and mass m=1.000 kg is released at angle theta0=33.15 deg above horizontal with COM height yc=2.249 m. It is given a small horizontal drift vx=-0.333 m/s, vertical velocity vy=0.000 m/s and spin omega=-0.575 rad/s. It falls under gravity g=9.800 m/s^2 and impacts a flat ground with restitution e=0.694 and Coulomb friction mu=0.119.", + "physical_law": { + "name": "Newton II for a planar rigid body + impulse-momentum theorem with Newton's restitution and Coulomb friction", + "statement": "Between contacts the COM is in projectile motion (m r_c'' = (0,-mg)) and orientation evolves at constant angular velocity (I theta'' = 0). At an instantaneous endpoint contact, an impulse J=(J_t,J_n) at the contact point updates linear and angular momenta; the normal component is set by Newton's restitution and the tangential component is bounded by the Coulomb cone.", + "formula": "free flight: m r_c''=(0,-mg), I theta''=0; contact: J_n = -(1+e) \\cdot v_ey/(1/m + r_x^2/I), |J_t| <= mu*|J_n|; dv_c = J/m, domega = (r_x J_n - r_y J_t)/I" + }, + "ode_system": { + "state_variables": [ + "x_c (COM x, m)", + "y_c (COM y, m)", + "theta (orientation from horizontal, rad)", + "v_x (m/s)", + "v_y (m/s)", + "omega (rad/s)" + ], + "equations_latex": [ + "\\dot{xc} = vx", + "\\dot{yc} = vy", + "\\dot{theta} = \\omega", + "\\dot{vx} = 0.0", + "\\dot{vy} = -g", + "\\dot{omega} = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.6936622751764076) \\cdot vp_y / (1/1.0 + rx^2/0.101046)", + "Jt = clip(-vp_x/(1/1.0 + ry^2/0.101046), -0.11907626055593712 \\cdot Jn, 0.11907626055593712 \\cdot Jn)", + "vx += Jt/m; vy += Jn/m; \\omega += (rx \\cdot Jn - ry \\cdot Jt)/I_cm" + ], + "equations_python": [ + "xc_dot = vx", + "yc_dot = vy", + "theta_dot = omega", + "vx_dot = 0.0", + "vy_dot = -g", + "omega_dot = 0.0", + "# impulsive at contact y_e=0 with v_ey<0:", + "Jn = -(1+0.6936622751764076) * vp_y / (1/1.0 + rx**2/0.101046)", + "Jt = clip(-vp_x/(1/1.0 + ry**2/0.101046), -0.11907626055593712 * Jn, 0.11907626055593712 * Jn)", + "vx += Jt/m; vy += Jn/m; omega += (rx * Jn - ry * Jt)/I_cm" + ] + }, + "derivation_steps": [ + "Rod length L=1.101 m, mass m=1.000 kg. Moment of inertia about COM: I = m L^2/12 = 0.10105 kg m^2.", + "Endpoint positions: e_+- = (x_c +- (L/2) \\cos theta, y_c +- (L/2) \\sin theta).", + "Endpoint velocities: v_e_x = v_x - r_y omega, v_e_y = v_y + r_x omega, with r=(r_x,r_y) the contact lever arm from COM.", + "Free flight: ddot x_c = 0, ddot y_c = -g = -9.800, ddot theta = 0.", + "Contact event: when y_e = 0 and v_ey < 0, apply impulse (J_t, J_n). Newton restitution: J_n = -(1+e) \\cdot v_ey / (1/m + r_x^2/I) with e = 0.6936622751764076.", + "Coulomb friction cone: |J_t| <= mu * |J_n| with mu = 0.11907626055593712. Stick candidate: J_t_stk = -v_ex / (1/m + r_y^2/I); clamp to the cone.", + "Each normal impulse loses fraction 1 - e^2 = 0.5188 of the normal kinetic energy at contact.", + "First-contact estimate (lower endpoint reaches ground): t1 ~ \\sqrt(2 \\cdot (yc - (L/2)*|\\sin theta0|)/g) = 0.631 s." + ], + "physical_constants_derived": { + "I_cm_kg_m2": 0.10104623608990926, + "energy_loss_fraction_per_bounce_normal": 0.5188326479970898, + "first_endpoint_contact_time_s_estimate": 0.6305484237802671 + }, + "numerical_method": { + "solver": "Hybrid: scipy.integrate.solve_ivp (RK45) on the smooth free-flight ODE with two terminal events tracking endpoints reaching y=0 from above; analytic impulse update at every event, then RK45 resumes.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.02 s", + "justification": "Velocity is piecewise-continuous with impulsive jumps; treating those analytically is more robust than asking RK45 to resolve a near-discontinuous RHS." + }, + "code_validation": { + "ode_source": "Newton II for rigid-body translation + Euler's rotation equation; impulse-momentum at contact (decoupled normal/tangent Coulomb collision matches standard rigid-body engines).", + "energy_check": "Between impacts E = (1/2) m (v_x^2+v_y^2) + (1/2) I omega^2 + m g y_c is conserved; each impulse dissipates a non-negative amount.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Lower endpoint reaches ground in t ~ 0.631 s.", + "Each bounce loses ~51.9% of normal KE; bouncing dies out within several impacts.", + "Coulomb friction (mu=0.12) damps horizontal drift and tangential motion at every contact.", + "Final state: rod horizontal, y_c = 0, v_x = v_y = omega = 0." + ], + "parameters": { + "L": { + "value": 1.1011606754143153, + "unit": "", + "description": "" + }, + "m": { + "value": 1.0, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "e_rest": { + "value": 0.6936622751764076, + "unit": "", + "description": "" + }, + "mu": { + "value": 0.11907626055593712, + "unit": "", + "description": "" + }, + "xc0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "yc0": { + "value": 2.249235403935709, + "unit": "", + "description": "" + }, + "theta0": { + "value": 0.5784954588872689, + "unit": "", + "description": "" + }, + "vx0": { + "value": -0.3334214670336114, + "unit": "", + "description": "" + }, + "vy0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "omega0": { + "value": -0.5752223696959697, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/bouncing_rod/675/params.json b/scipy/bouncing_rod/675/params.json new file mode 100644 index 0000000000000000000000000000000000000000..563fe0ac2d4adb5db813196a4488fb99e31c3b9f --- /dev/null +++ b/scipy/bouncing_rod/675/params.json @@ -0,0 +1,13 @@ +{ + "L": 1.1011606754143153, + "m": 1.0, + "g": 9.8, + "e_rest": 0.6936622751764076, + "mu": 0.11907626055593712, + "xc0": 0.0, + "yc0": 2.249235403935709, + "theta0": 0.5784954588872689, + "vx0": -0.3334214670336114, + "vy0": 0.0, + "omega0": -0.5752223696959697 +} \ No newline at end of file diff --git a/scipy/bouncing_rod/675/simulation_code.py b/scipy/bouncing_rod/675/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..026030669caac9e91a047fcbe3c215d9b09f91d4 --- /dev/null +++ b/scipy/bouncing_rod/675/simulation_code.py @@ -0,0 +1,130 @@ +""" +Bouncing Rod — auto-generated simulation script. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +L = 1.1011606754143153 +m = 1.0 +g = 9.8 +e_rest = 0.6936622751764076 +mu = 0.11907626055593712 +xc0 = 0.0 +yc0 = 2.249235403935709 +theta0 = 0.5784954588872689 +vx0 = -0.3334214670336114 +vy0 = 0.0 +omega0 = -0.5752223696959697 +duration = 12 + +I_cm = m * L * L / 12.0 +fps = 30 + +def free_flight(t, y): + return [y[3], y[4], y[5], 0.0, -g, 0.0] + +def ev_plus(t, y): + return y[1] + (L/2.0)*np.sin(y[2]) +ev_plus.terminal = True; ev_plus.direction = -1 +def ev_minus(t, y): + return y[1] - (L/2.0)*np.sin(y[2]) +ev_minus.terminal = True; ev_minus.direction = -1 + +def contact_step(state): + s = state.copy() + for _ in range(8): + any_imp = False + for sgn in (+1.0, -1.0): + xc, yc, th, vx, vy, om = s + rx = sgn * (L/2.0) * np.cos(th) + ry = sgn * (L/2.0) * np.sin(th) + ey = yc + ry + vp_y = vy + rx * om + if ey > 1e-9 or vp_y > -1e-12: continue + vp_x = vx - ry * om + Jn = -(1.0 + e_rest) * vp_y / (1.0/m + rx*rx / I_cm) + Jt_s = -vp_x / (1.0/m + ry*ry / I_cm) + mt = mu * Jn + Jt = max(-mt, min(mt, Jt_s)) + s[3] = vx + Jt / m + s[4] = vy + Jn / m + s[5] = om + (rx * Jn - ry * Jt) / I_cm + s[1] = yc - ey + any_imp = True + if not any_imp: break + return s + +t_eval_global = np.linspace(0, duration, int(duration * fps) + 1) +xc_arr = np.zeros_like(t_eval_global) +yc_arr = np.zeros_like(t_eval_global) +th_arr = np.zeros_like(t_eval_global) + +state = np.array([xc0, yc0, theta0, vx0, vy0, omega0], dtype=float) +xc_arr[0], yc_arr[0], th_arr[0] = state[0], state[1], state[2] +last_idx = 1 +t_cur = 0.0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free_flight, (t_cur, duration), state, + events=[ev_plus, ev_minus], dense_output=True, + max_step=0.02, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_global) and t_eval_global[last_idx] <= seg_end + 1e-12: + st = sol.sol(t_eval_global[last_idx]) + xc_arr[last_idx], yc_arr[last_idx], th_arr[last_idx] = st[0], st[1], st[2] + last_idx += 1 + state = sol.y[:, -1].copy() + t_cur = seg_end + if sol.status != 1: break + state = contact_step(state) + KE = 0.5 * m * (state[3]**2 + state[4]**2) + 0.5 * I_cm * state[5]**2 + if KE < 0.03 and abs(state[1]) < 0.05 and abs(np.sin(state[2])) < 0.10: + th_rest = round(state[2] / np.pi) * np.pi + state = np.array([state[0], 0.0, th_rest, 0.0, 0.0, 0.0]) + break + +if last_idx < len(t_eval_global): + xc_arr[last_idx:] = state[0]; yc_arr[last_idx:] = state[1]; th_arr[last_idx:] = state[2] + +xs_p = xc_arr + (L/2)*np.cos(th_arr); ys_p = yc_arr + (L/2)*np.sin(th_arr) +xs_m = xc_arr - (L/2)*np.cos(th_arr); ys_m = yc_arr - (L/2)*np.sin(th_arr) + +fig_w, fig_h = 8, 5 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.5, 0.4 +y_lo = -0.4 +x_lo_d = min(xs_m.min(), xs_p.min()) - xpad +x_hi_d = max(xs_m.max(), xs_p.max()) + xpad +y_hi_d = max(ys_p.max(), ys_m.max(), yc_arr.max()) + ypad +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_hi = x_lo_d - extra, x_hi_d + extra, y_hi_d +else: + extra = dx / fig_aspect - dy + x_lo, x_hi, y_hi = x_lo_d, x_hi_d, 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.axhspan(y_lo, 0.0, color="#4a4e69", alpha=0.55, zorder=0) +ax.axhline(0, color="#adb5bd", lw=2, zorder=1) + +rod_ln, = ax.plot([], [], "-", color="#e63946", lw=12, + solid_capstyle="round", zorder=3) + +def update(f): + rod_ln.set_data([xs_m[f], xs_p[f]], [ys_m[f], ys_p[f]]) + return (rod_ln,) + +ani = animation.FuncAnimation(fig, update, frames=t_eval_global.size, + interval=1000/fps, blit=True) +ani.save("bouncing_rod.mp4", + writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved bouncing_rod.mp4") diff --git a/scipy/bouncing_rod/675/video.mp4 b/scipy/bouncing_rod/675/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1bd9c36c44a89416d98a9a0bb955bb0b2a4c76e5 --- /dev/null +++ b/scipy/bouncing_rod/675/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5a8d38a2e8b9533acf68218ad8471738241649780728de2f7df4acf07d58e756 +size 9312 diff --git a/scipy/brachistochrone/102/cot.json b/scipy/brachistochrone/102/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..c02954cb7c7868d00ab4120fd9ea71afe29cb728 --- /dev/null +++ b/scipy/brachistochrone/102/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=0.845 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=2.240 m. Gravity g=9.800 m/s^2, damping b=0.183 kg/s. Initial \\theta_0=0.099 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 2.1241 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 2.1241, + "isochronous_omega_rad_s": 1.479, + "cycloid_width_x_m": 14.0741, + "cycloid_height_y_m": 4.4799 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 2.124 s (independent of initial angle \\theta_0=0.099 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.183 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 14.074 m, height = 2r = 4.480 m." + ], + "parameters": { + "r": { + "value": 2.2399547469619656, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 0.8450593248424081, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.18322454736899796, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.09936373031930912, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/102/params.json b/scipy/brachistochrone/102/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b51492e25797d5de9b36a34a966f9717b981608a --- /dev/null +++ b/scipy/brachistochrone/102/params.json @@ -0,0 +1,8 @@ +{ + "r": 2.2399547469619656, + "m": 0.8450593248424081, + "b": 0.18322454736899796, + "g": 9.8, + "theta0": 0.09936373031930912, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/102/simulation_code.py b/scipy/brachistochrone/102/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c7ba254c25e823e2c08c9e03c30ce1b910b07e5f --- /dev/null +++ b/scipy/brachistochrone/102/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 2.2399547469619656 +m = 0.8450593248424081 +b = 0.18322454736899796 +g = 9.8 +theta0 = 0.099364 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/102/video.mp4 b/scipy/brachistochrone/102/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..b39cfd3f88fc4a817f07478e27708ddac918d5b2 --- /dev/null +++ b/scipy/brachistochrone/102/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:43337c7ba7002810ba330856bd38032b677fbc86989ed35c75180751e39a5ac2 +size 11444 diff --git a/scipy/brachistochrone/108/cot.json b/scipy/brachistochrone/108/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..5388f0d24acc36ec8ddfc8ea0269a22bd1e35b9d --- /dev/null +++ b/scipy/brachistochrone/108/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=1.535 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=2.448 m. Gravity g=9.800 m/s^2, damping b=0.137 kg/s. Initial \\theta_0=0.121 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 2.2206 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 2.2206, + "isochronous_omega_rad_s": 1.4147, + "cycloid_width_x_m": 15.3823, + "cycloid_height_y_m": 4.8963 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 2.221 s (independent of initial angle \\theta_0=0.121 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.137 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 15.382 m, height = 2r = 4.896 m." + ], + "parameters": { + "r": { + "value": 2.4481712792467443, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 1.5354692719117051, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.1374925549337024, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.12085845282321056, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/108/params.json b/scipy/brachistochrone/108/params.json new file mode 100644 index 0000000000000000000000000000000000000000..a68e0125dd16d1cf6b9235e42344a13807f73da9 --- /dev/null +++ b/scipy/brachistochrone/108/params.json @@ -0,0 +1,8 @@ +{ + "r": 2.4481712792467443, + "m": 1.5354692719117051, + "b": 0.1374925549337024, + "g": 9.8, + "theta0": 0.12085845282321056, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/108/simulation_code.py b/scipy/brachistochrone/108/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..93cdd5cc018bd06498a4f678a1e47f84fc55f886 --- /dev/null +++ b/scipy/brachistochrone/108/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 2.4481712792467443 +m = 1.5354692719117051 +b = 0.1374925549337024 +g = 9.8 +theta0 = 0.120858 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/108/video.mp4 b/scipy/brachistochrone/108/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7469e369a8099f1821e90f8b96894d7cfb4fb29c --- /dev/null +++ b/scipy/brachistochrone/108/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:73590dfe3622048ed257a3a93a32823052317e3faf551f91762087ce85efd207 +size 11478 diff --git a/scipy/brachistochrone/159/cot.json b/scipy/brachistochrone/159/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..8a1d3553ca573274024b52af18b122129083f094 --- /dev/null +++ b/scipy/brachistochrone/159/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=1.520 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=2.298 m. Gravity g=9.800 m/s^2, damping b=0.093 kg/s. Initial \\theta_0=0.255 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 2.1516 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 2.1516, + "isochronous_omega_rad_s": 1.4601, + "cycloid_width_x_m": 14.4406, + "cycloid_height_y_m": 4.5966 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 2.152 s (independent of initial angle \\theta_0=0.255 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.093 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 14.441 m, height = 2r = 4.597 m." + ], + "parameters": { + "r": { + "value": 2.298296495422596, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 1.5198391514368716, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.09276705819763889, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.255094675145009, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/159/params.json b/scipy/brachistochrone/159/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e9d0e705ef9a886608cb248b27bbf3b6f58c318d --- /dev/null +++ b/scipy/brachistochrone/159/params.json @@ -0,0 +1,8 @@ +{ + "r": 2.298296495422596, + "m": 1.5198391514368716, + "b": 0.09276705819763889, + "g": 9.8, + "theta0": 0.255094675145009, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/159/simulation_code.py b/scipy/brachistochrone/159/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..286893c72174607e0f068c54395d6eca8bb2e935 --- /dev/null +++ b/scipy/brachistochrone/159/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 2.298296495422596 +m = 1.5198391514368716 +b = 0.09276705819763889 +g = 9.8 +theta0 = 0.255095 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/159/video.mp4 b/scipy/brachistochrone/159/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..193bef495211c2a1515ffcd9035885a8168c98b7 --- /dev/null +++ b/scipy/brachistochrone/159/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0164f147cb8d7b597713df5a1eb3ea2fae6b06f3496072d73b5a5b6e3df1f55a +size 11636 diff --git a/scipy/brachistochrone/191/cot.json b/scipy/brachistochrone/191/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..9916394226aea93745c3717096b5cb8123b565e5 --- /dev/null +++ b/scipy/brachistochrone/191/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=1.942 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=1.430 m. Gravity g=9.800 m/s^2, damping b=0.089 kg/s. Initial \\theta_0=0.075 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 1.6970 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 1.697, + "isochronous_omega_rad_s": 1.8513, + "cycloid_width_x_m": 8.9834, + "cycloid_height_y_m": 2.8595 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 1.697 s (independent of initial angle \\theta_0=0.075 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.089 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 8.983 m, height = 2r = 2.860 m." + ], + "parameters": { + "r": { + "value": 1.429753411507966, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 1.9418902892197785, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.08902904145782092, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.07456877223545837, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/191/params.json b/scipy/brachistochrone/191/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e897cb577f23fc9f36a9d8571854b158b7566c35 --- /dev/null +++ b/scipy/brachistochrone/191/params.json @@ -0,0 +1,8 @@ +{ + "r": 1.429753411507966, + "m": 1.9418902892197785, + "b": 0.08902904145782092, + "g": 9.8, + "theta0": 0.07456877223545837, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/191/simulation_code.py b/scipy/brachistochrone/191/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..033b28c22a4665e7244e08efcfdebd1c09250eab --- /dev/null +++ b/scipy/brachistochrone/191/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 1.429753411507966 +m = 1.9418902892197785 +b = 0.08902904145782092 +g = 9.8 +theta0 = 0.074569 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/191/video.mp4 b/scipy/brachistochrone/191/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3c9033822d9953c1a45df5c7dd7fce65c11c9424 --- /dev/null +++ b/scipy/brachistochrone/191/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8693defa946db72399a0f4feeb40bae08d08ba49815bbf60eb7eadfb5c80e292 +size 11996 diff --git a/scipy/brachistochrone/204/cot.json b/scipy/brachistochrone/204/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..934209165fbedaab00379f1395c4d993ee567df9 --- /dev/null +++ b/scipy/brachistochrone/204/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=0.815 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=2.163 m. Gravity g=9.800 m/s^2, damping b=0.030 kg/s. Initial \\theta_0=0.378 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 2.0871 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 2.0871, + "isochronous_omega_rad_s": 1.5053, + "cycloid_width_x_m": 13.5879, + "cycloid_height_y_m": 4.3252 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 2.087 s (independent of initial angle \\theta_0=0.378 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.030 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 13.588 m, height = 2r = 4.325 m." + ], + "parameters": { + "r": { + "value": 2.162577383329468, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 0.8146767387463443, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.02959063292922317, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.3784076735056856, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/204/params.json b/scipy/brachistochrone/204/params.json new file mode 100644 index 0000000000000000000000000000000000000000..01f389e7cbd6899f20d50c4dfec52b8443e3b24d --- /dev/null +++ b/scipy/brachistochrone/204/params.json @@ -0,0 +1,8 @@ +{ + "r": 2.162577383329468, + "m": 0.8146767387463443, + "b": 0.02959063292922317, + "g": 9.8, + "theta0": 0.3784076735056856, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/204/simulation_code.py b/scipy/brachistochrone/204/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e9c7cd5ec6cd55c35fe432191e3c197232c7dbda --- /dev/null +++ b/scipy/brachistochrone/204/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 2.162577383329468 +m = 0.8146767387463443 +b = 0.02959063292922317 +g = 9.8 +theta0 = 0.378408 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/204/video.mp4 b/scipy/brachistochrone/204/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..633f41a50d34cf8de1ee9ff5f1ea1cf151e966ab --- /dev/null +++ b/scipy/brachistochrone/204/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bb992f01994b432e86502055e82df89b06e218d70e78bc5b3cb4dd9d7ed6da2c +size 11892 diff --git a/scipy/brachistochrone/216/cot.json b/scipy/brachistochrone/216/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..0603773585406af4a8a9e5043811fedb2ee8661b --- /dev/null +++ b/scipy/brachistochrone/216/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=1.331 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=2.521 m. Gravity g=9.800 m/s^2, damping b=0.283 kg/s. Initial \\theta_0=0.213 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 2.2533 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 2.2533, + "isochronous_omega_rad_s": 1.3942, + "cycloid_width_x_m": 15.8387, + "cycloid_height_y_m": 5.0416 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 2.253 s (independent of initial angle \\theta_0=0.213 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.283 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 15.839 m, height = 2r = 5.042 m." + ], + "parameters": { + "r": { + "value": 2.520802175682938, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 1.331027118652586, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.283147280312263, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.21259453954438112, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/216/params.json b/scipy/brachistochrone/216/params.json new file mode 100644 index 0000000000000000000000000000000000000000..7aa26950ca6f95fcadf847179b924120ebe179c4 --- /dev/null +++ b/scipy/brachistochrone/216/params.json @@ -0,0 +1,8 @@ +{ + "r": 2.520802175682938, + "m": 1.331027118652586, + "b": 0.283147280312263, + "g": 9.8, + "theta0": 0.21259453954438112, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/216/simulation_code.py b/scipy/brachistochrone/216/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..5f3cdeb4c933e07f58d861795b25444a82ed1e7b --- /dev/null +++ b/scipy/brachistochrone/216/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 2.520802175682938 +m = 1.331027118652586 +b = 0.283147280312263 +g = 9.8 +theta0 = 0.212595 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/216/video.mp4 b/scipy/brachistochrone/216/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7212654114fbfa163ea132b7f26416c5f74bdf2c --- /dev/null +++ b/scipy/brachistochrone/216/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b0e411cbca48a9fbc717429f994a9ab6402c30c58a67e29d44c300c93947b0ad +size 11338 diff --git a/scipy/brachistochrone/270/cot.json b/scipy/brachistochrone/270/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..0fb7f98de0b7d277a6390c017a98600519c472e1 --- /dev/null +++ b/scipy/brachistochrone/270/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=0.898 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=1.671 m. Gravity g=9.800 m/s^2, damping b=0.099 kg/s. Initial \\theta_0=0.281 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 1.8343 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 1.8343, + "isochronous_omega_rad_s": 1.7126, + "cycloid_width_x_m": 10.4964, + "cycloid_height_y_m": 3.3411 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 1.834 s (independent of initial angle \\theta_0=0.281 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.099 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 10.496 m, height = 2r = 3.341 m." + ], + "parameters": { + "r": { + "value": 1.6705499294307882, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 0.8983214710480626, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.09905338114207636, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.2806157375643577, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/270/params.json b/scipy/brachistochrone/270/params.json new file mode 100644 index 0000000000000000000000000000000000000000..7796d2ae3ba6de9b3cb012cdc24b81357a7331de --- /dev/null +++ b/scipy/brachistochrone/270/params.json @@ -0,0 +1,8 @@ +{ + "r": 1.6705499294307882, + "m": 0.8983214710480626, + "b": 0.09905338114207636, + "g": 9.8, + "theta0": 0.2806157375643577, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/270/simulation_code.py b/scipy/brachistochrone/270/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..df4167c2c5318a66c95c3546460764eb23b0369b --- /dev/null +++ b/scipy/brachistochrone/270/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 1.6705499294307882 +m = 0.8983214710480626 +b = 0.09905338114207636 +g = 9.8 +theta0 = 0.280616 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/270/video.mp4 b/scipy/brachistochrone/270/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..718e9e6c4679de9c027911cb8d90fbc9f4fc5f84 --- /dev/null +++ b/scipy/brachistochrone/270/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:699b4d4231089085000775f9a4249908d65f0b810afd83295542872417519252 +size 11524 diff --git a/scipy/brachistochrone/276/cot.json b/scipy/brachistochrone/276/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..9a089613dd0d313ab229beabf0dc2c2e7b668db2 --- /dev/null +++ b/scipy/brachistochrone/276/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=1.848 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=2.988 m. Gravity g=9.800 m/s^2, damping b=0.061 kg/s. Initial \\theta_0=0.456 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 2.4532 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 2.4532, + "isochronous_omega_rad_s": 1.2806, + "cycloid_width_x_m": 18.7726, + "cycloid_height_y_m": 5.9755 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 2.453 s (independent of initial angle \\theta_0=0.456 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.061 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 18.773 m, height = 2r = 5.976 m." + ], + "parameters": { + "r": { + "value": 2.9877550063003433, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 1.847691630265713, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.060617958223980924, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.45592749009102906, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/276/params.json b/scipy/brachistochrone/276/params.json new file mode 100644 index 0000000000000000000000000000000000000000..809085bcee01e2d115f137feb33dc67e4427f073 --- /dev/null +++ b/scipy/brachistochrone/276/params.json @@ -0,0 +1,8 @@ +{ + "r": 2.9877550063003433, + "m": 1.847691630265713, + "b": 0.060617958223980924, + "g": 9.8, + "theta0": 0.45592749009102906, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/276/simulation_code.py b/scipy/brachistochrone/276/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..50ec8c042ae0b9dcd75f77871e26ebe4305b5b94 --- /dev/null +++ b/scipy/brachistochrone/276/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 2.9877550063003433 +m = 1.847691630265713 +b = 0.060617958223980924 +g = 9.8 +theta0 = 0.455927 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/276/video.mp4 b/scipy/brachistochrone/276/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..360d294b88ee37e33ac7e450a04ed7013d3e84d4 --- /dev/null +++ b/scipy/brachistochrone/276/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6f9b60c3d1186fa619b4e5d5a764c916a0fc6fd7ef8081a9698445f490b53102 +size 11343 diff --git a/scipy/brachistochrone/310/cot.json b/scipy/brachistochrone/310/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..c1f96e28716f3b55c167d0acdd772eff2d5617f1 --- /dev/null +++ b/scipy/brachistochrone/310/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=1.755 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=1.404 m. Gravity g=9.800 m/s^2, damping b=0.011 kg/s. Initial \\theta_0=0.111 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 1.6815 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 1.6815, + "isochronous_omega_rad_s": 1.8683, + "cycloid_width_x_m": 8.8202, + "cycloid_height_y_m": 2.8076 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 1.682 s (independent of initial angle \\theta_0=0.111 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.011 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 8.820 m, height = 2r = 2.808 m." + ], + "parameters": { + "r": { + "value": 1.4037805535581434, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 1.7551202000109154, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.010622340882679526, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.11140715122433352, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/310/params.json b/scipy/brachistochrone/310/params.json new file mode 100644 index 0000000000000000000000000000000000000000..478151824efdd13bc47fbd750de1a96a2d3c3b6f --- /dev/null +++ b/scipy/brachistochrone/310/params.json @@ -0,0 +1,8 @@ +{ + "r": 1.4037805535581434, + "m": 1.7551202000109154, + "b": 0.010622340882679526, + "g": 9.8, + "theta0": 0.11140715122433352, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/310/simulation_code.py b/scipy/brachistochrone/310/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..052f496931bade51a96c19e516e7e9a849fdf95f --- /dev/null +++ b/scipy/brachistochrone/310/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 1.4037805535581434 +m = 1.7551202000109154 +b = 0.010622340882679526 +g = 9.8 +theta0 = 0.111407 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/310/video.mp4 b/scipy/brachistochrone/310/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..16987f020016a32533fc2895fdf14781b113922c --- /dev/null +++ b/scipy/brachistochrone/310/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c91d8aaff75b21453fafdefbb0c2e030edf2418de5f401d7325f51e433bf24f1 +size 12113 diff --git a/scipy/brachistochrone/332/cot.json b/scipy/brachistochrone/332/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..e92a36e7f4cbd62680385be76d7284af76246322 --- /dev/null +++ b/scipy/brachistochrone/332/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=1.973 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=1.929 m. Gravity g=9.800 m/s^2, damping b=0.152 kg/s. Initial \\theta_0=0.115 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 1.9712 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 1.9712, + "isochronous_omega_rad_s": 1.5937, + "cycloid_width_x_m": 12.1216, + "cycloid_height_y_m": 3.8584 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 1.971 s (independent of initial angle \\theta_0=0.115 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.152 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 12.122 m, height = 2r = 3.858 m." + ], + "parameters": { + "r": { + "value": 1.929208440061822, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 1.9725807163331606, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.15203116164133043, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.11509741752547206, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/332/params.json b/scipy/brachistochrone/332/params.json new file mode 100644 index 0000000000000000000000000000000000000000..aab16070f70a10d24e2c8affb5835b6637847033 --- /dev/null +++ b/scipy/brachistochrone/332/params.json @@ -0,0 +1,8 @@ +{ + "r": 1.929208440061822, + "m": 1.9725807163331606, + "b": 0.15203116164133043, + "g": 9.8, + "theta0": 0.11509741752547206, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/332/simulation_code.py b/scipy/brachistochrone/332/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..7dbf8edaec3249fb12b07bdf103ab393f630e7a5 --- /dev/null +++ b/scipy/brachistochrone/332/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 1.929208440061822 +m = 1.9725807163331606 +b = 0.15203116164133043 +g = 9.8 +theta0 = 0.115097 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/332/video.mp4 b/scipy/brachistochrone/332/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..2416e51672a44c3ce6edd0620fc4961781ae5b5a --- /dev/null +++ b/scipy/brachistochrone/332/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a8b63dd6f14e79f22ec03f3fca1d1e8b52eb43652c5e4ce9466d35a9c8f0a409 +size 11844 diff --git a/scipy/brachistochrone/343/cot.json b/scipy/brachistochrone/343/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..9ca8433010000f4a7a39a979fe6252eb07f65a80 --- /dev/null +++ b/scipy/brachistochrone/343/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=0.945 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=2.195 m. Gravity g=9.800 m/s^2, damping b=0.289 kg/s. Initial \\theta_0=0.073 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 2.1027 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 2.1027, + "isochronous_omega_rad_s": 1.4941, + "cycloid_width_x_m": 13.7923, + "cycloid_height_y_m": 4.3902 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 2.103 s (independent of initial angle \\theta_0=0.073 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.289 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 13.792 m, height = 2r = 4.390 m." + ], + "parameters": { + "r": { + "value": 2.1951089248089164, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 0.9445737866247063, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.2886530951848396, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.07309304295632266, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/343/params.json b/scipy/brachistochrone/343/params.json new file mode 100644 index 0000000000000000000000000000000000000000..4f2e2f4ae5e2ab3702c321fa05e8307bc66df9ca --- /dev/null +++ b/scipy/brachistochrone/343/params.json @@ -0,0 +1,8 @@ +{ + "r": 2.1951089248089164, + "m": 0.9445737866247063, + "b": 0.2886530951848396, + "g": 9.8, + "theta0": 0.07309304295632266, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/343/simulation_code.py b/scipy/brachistochrone/343/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..021df14bdba1c8bc69feb9be1a81c9d6f6fb56d4 --- /dev/null +++ b/scipy/brachistochrone/343/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 2.1951089248089164 +m = 0.9445737866247063 +b = 0.2886530951848396 +g = 9.8 +theta0 = 0.073093 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/343/video.mp4 b/scipy/brachistochrone/343/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..c62b47e7b515f9f25259ab93cf55f120bc078e58 --- /dev/null +++ b/scipy/brachistochrone/343/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a8ad099cc7b88a0100e98389133e360aec35637e1d8a3bd43c894cf5883832d3 +size 11325 diff --git a/scipy/brachistochrone/353/cot.json b/scipy/brachistochrone/353/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..79f6d7dc419847dcb8043c1ddafa98a5320fb75c --- /dev/null +++ b/scipy/brachistochrone/353/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=0.517 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=1.396 m. Gravity g=9.800 m/s^2, damping b=0.007 kg/s. Initial \\theta_0=0.263 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 1.6769 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 1.6769, + "isochronous_omega_rad_s": 1.8735, + "cycloid_width_x_m": 8.7716, + "cycloid_height_y_m": 2.7921 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 1.677 s (independent of initial angle \\theta_0=0.263 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.007 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 8.772 m, height = 2r = 2.792 m." + ], + "parameters": { + "r": { + "value": 1.3960396436551714, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 0.5171605274776911, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.007050416011892824, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.26341607091661956, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/353/params.json b/scipy/brachistochrone/353/params.json new file mode 100644 index 0000000000000000000000000000000000000000..4881153da4e0eb8c3da2326d4f5e133dc9cfb165 --- /dev/null +++ b/scipy/brachistochrone/353/params.json @@ -0,0 +1,8 @@ +{ + "r": 1.3960396436551714, + "m": 0.5171605274776911, + "b": 0.007050416011892824, + "g": 9.8, + "theta0": 0.26341607091661956, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/353/simulation_code.py b/scipy/brachistochrone/353/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d53e1075f1d0eee239bf9b8c451884035553fb7c --- /dev/null +++ b/scipy/brachistochrone/353/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 1.3960396436551714 +m = 0.5171605274776911 +b = 0.007050416011892824 +g = 9.8 +theta0 = 0.263416 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/353/video.mp4 b/scipy/brachistochrone/353/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..5c65d68a1caf27f693fdd035ddf20fa220289a3c --- /dev/null +++ b/scipy/brachistochrone/353/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:80d7b6c6b2d29e565755272754846afaf85c97f36383af9f4be4a76a9c017f79 +size 12235 diff --git a/scipy/brachistochrone/365/cot.json b/scipy/brachistochrone/365/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..b24e8d55617c2dff0fb8b37b352317af71b1069a --- /dev/null +++ b/scipy/brachistochrone/365/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=1.827 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=2.183 m. Gravity g=9.800 m/s^2, damping b=0.050 kg/s. Initial \\theta_0=0.278 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 2.0967 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 2.0967, + "isochronous_omega_rad_s": 1.4983, + "cycloid_width_x_m": 13.7139, + "cycloid_height_y_m": 4.3653 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 2.097 s (independent of initial angle \\theta_0=0.278 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.050 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 13.714 m, height = 2r = 4.365 m." + ], + "parameters": { + "r": { + "value": 2.1826389367780115, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 1.8270087247257096, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.04980423259509822, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.2781185664741778, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/365/params.json b/scipy/brachistochrone/365/params.json new file mode 100644 index 0000000000000000000000000000000000000000..edf6836f677bd22ae7137a1512c4e7b05e63e9f1 --- /dev/null +++ b/scipy/brachistochrone/365/params.json @@ -0,0 +1,8 @@ +{ + "r": 2.1826389367780115, + "m": 1.8270087247257096, + "b": 0.04980423259509822, + "g": 9.8, + "theta0": 0.2781185664741778, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/365/simulation_code.py b/scipy/brachistochrone/365/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..2642ae4b155f83b0834cef479415ad7e19e07684 --- /dev/null +++ b/scipy/brachistochrone/365/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 2.1826389367780115 +m = 1.8270087247257096 +b = 0.04980423259509822 +g = 9.8 +theta0 = 0.278119 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/365/video.mp4 b/scipy/brachistochrone/365/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3885a31536e882931f4c23122aa12c6625b0c6e6 --- /dev/null +++ b/scipy/brachistochrone/365/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:102e037791d5afdbfb054ac4de10bf7125ee7b60b37ed3016e1d4fa56eabfdd5 +size 11903 diff --git a/scipy/brachistochrone/373/cot.json b/scipy/brachistochrone/373/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..59bf71f19c7834960bdfc1b71258691c6a5e5d41 --- /dev/null +++ b/scipy/brachistochrone/373/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=1.952 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=1.987 m. Gravity g=9.800 m/s^2, damping b=0.100 kg/s. Initial \\theta_0=0.267 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 2.0007 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 2.0007, + "isochronous_omega_rad_s": 1.5703, + "cycloid_width_x_m": 12.4863, + "cycloid_height_y_m": 3.9745 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 2.001 s (independent of initial angle \\theta_0=0.267 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.100 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 12.486 m, height = 2r = 3.975 m." + ], + "parameters": { + "r": { + "value": 1.9872642597992485, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 1.951925060498327, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.0998287222377691, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.2668825003761504, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/373/params.json b/scipy/brachistochrone/373/params.json new file mode 100644 index 0000000000000000000000000000000000000000..bb90035fb8f28b04ec4df5b8e273d95fc319ea0a --- /dev/null +++ b/scipy/brachistochrone/373/params.json @@ -0,0 +1,8 @@ +{ + "r": 1.9872642597992485, + "m": 1.951925060498327, + "b": 0.0998287222377691, + "g": 9.8, + "theta0": 0.2668825003761504, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/373/simulation_code.py b/scipy/brachistochrone/373/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..df838250727079fca1fa60c393401039c1ecef91 --- /dev/null +++ b/scipy/brachistochrone/373/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 1.9872642597992485 +m = 1.951925060498327 +b = 0.0998287222377691 +g = 9.8 +theta0 = 0.266883 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/373/video.mp4 b/scipy/brachistochrone/373/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..75dbc5244233ec3315b8242a090faca0f080539d --- /dev/null +++ b/scipy/brachistochrone/373/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:faad24ee41c1960934c6aa6a04516067ccbbd75a0c7265d1ebd544bfda45e7d9 +size 11847 diff --git a/scipy/brachistochrone/386/cot.json b/scipy/brachistochrone/386/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..e4d994079e3da9cd207833c75c88a2a8f6b852f6 --- /dev/null +++ b/scipy/brachistochrone/386/cot.json @@ -0,0 +1,92 @@ +{ + "simulation": "brachistochrone", + "physical_observation": "A bead (m=0.611 kg) slides along a frictionless cycloid curve --- the brachistochrone --- the curve of fastest descent from rest. Parametrized by cycloid parameter \\theta: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)) with r=2.746 m. Gravity g=9.800 m/s^2, damping b=0.254 kg/s. Initial \\theta_0=0.282 rad.", + "physical_law": { + "name": "Brachistochrone (Bernoulli 1696) + Tautochrone / Isochronous Property", + "statement": "The cycloid is simultaneously the curve of fastest descent (brachistochrone) and the isochronous curve: the period of oscillation is independent of amplitude. With y measured downward, Euler-Lagrange on the cycloid gives the implemented theta-coordinate equation below.", + "formula": "Cycloid: x=r(\\theta-\\sin(\\theta)), y=r(1-\\cos(\\theta))\nEOM: \\ddot{\\theta} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} - 2r^2 \\cdot \\sin(\\theta/2)\\cos(\\theta/2) \\cdot \\dot{\\theta}^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]\nIsochronous period: T = \\pi \\cdot \\sqrt(2r/g) (independent of \\theta_0)" + }, + "ode_system": { + "state_variables": [ + "\\theta (cycloid parameter, rad)", + "\\omega = \\dot{\\theta} (rad/s)" + ], + "equations_latex": [ + "\\dot{\\theta} = \\omega", + "\\dot{\\omega} = [g \\cdot r \\cdot \\sin(\\theta) - (b/m) \\cdot (2r \\cdot \\sin(\\theta/2)) \\cdot \\omega - (2r \\cdot \\sin(\\theta/2))(r \\cdot \\cos(\\theta/2)) \\cdot \\omega^2] / [4r^2 \\cdot \\sin^2(\\theta/2)]", + "where ds/d\\theta = 2r \\cdot \\sin(\\theta/2) and d^2s/d\\theta^2 = r \\cdot \\cos(\\theta/2)" + ], + "equations_python": [ + "theta_dot = omega", + "omega_dot = (g*r*np.sin(theta) - (b/m)*(2*r*np.sin(theta/2))*omega - (2*r*np.sin(theta/2))*(r*np.cos(theta/2))*omega**2) / (4*r**2*np.sin(theta/2)**2)", + "where ds_dtheta = 2*r*np.sin(theta/2) and d2s_dtheta2 = r*np.cos(theta/2)" + ] + }, + "derivation_steps": [ + "Cycloid parametrization: x(\\theta)=r(\\theta-\\sin(\\theta)), y(\\theta)=r(1-\\cos(\\theta)).", + "Arc length element: ds^2 = dx^2+dy^2 = 2r^2(1-\\cos(\\theta))d\\theta^2 = 4r^2\\sin^2(\\theta/2)d\\theta^2.", + "Arc length velocity: \\dot{s} = 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta}.", + "Kinetic energy: T = \\tfrac{1}{2} m \\cdot \\dot{s}^2 = 2m \\cdot r^2 \\cdot \\sin^2(\\theta/2) \\cdot \\dot{\\theta}^2.", + "Potential energy (taking y=0 as reference): V = -m \\cdot g \\cdot r(1-\\cos(\\theta)).", + "Lagrangian L = T - V; apply Euler-Lagrange for \\theta:", + " S_1^2 \\ddot{\\theta} + S_1 S_2 \\dot{\\theta}^2 = g\\cdot dy/d\\theta - (b/m)S_1\\dot{\\theta}, where S_1=ds/d\\theta and S_2=dS_1/d\\theta.", + " Therefore \\ddot{\\theta} = [g\\cdot r\\cdot \\sin\\theta - (b/m)S_1\\dot{\\theta} - S_1S_2\\dot{\\theta}^2] / S_1^2.", + "Using \\sin(\\theta) = 2\\sin(\\theta/2)\\cos(\\theta/2): isochronous period T=\\pi \\sqrt (2r/g) (independent of \\theta_0).", + "Add damping F_damp = -b \\cdot \\dot{s} = -b \\cdot 2r \\cdot \\sin(\\theta/2) \\cdot \\dot{\\theta} as generalized force.", + "Isochronous period T = \\pi \\cdot \\sqrt(2r/g) = 2.3518 s." + ], + "physical_constants_derived": { + "isochronous_period_T_s": 2.3518, + "isochronous_omega_rad_s": 1.3358, + "cycloid_width_x_m": 17.2539, + "cycloid_height_y_m": 5.4921 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Cycloid EOM has a \\sin^2(\\theta/2) denominator; adaptive step prevents issues near \\theta=0." + }, + "code_validation": { + "ode_source": "Euler-Lagrange on cycloid curve; matches BrachistochroneSim.ts evaluate()", + "energy_check": "E = 2mr^2\\sin^2(\\theta/2)\\dot{\\theta}^2 - mgr(1-\\cos(\\theta)) decreases when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Isochronous period T = 2.352 s (independent of initial angle \\theta_0=0.282 rad).", + "This is remarkable: the period is the same for any starting angle \\theta_0 \\in (0, 2\\pi).", + "With damping b=0.254 kg/s, oscillations decay toward \\theta=\\pi (bottom of the cycloid).", + "Cycloid shape: width = 2\\pir = 17.254 m, height = 2r = 5.492 m." + ], + "parameters": { + "r": { + "value": 2.7460505531088426, + "unit": "m", + "description": "Cycloid radius" + }, + "m": { + "value": 0.6108027127526061, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.2535319449366436, + "unit": "kg/s", + "description": "Damping coefficient" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "theta0": { + "value": 0.2819500103540793, + "unit": "", + "description": "" + }, + "omega0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone/386/params.json b/scipy/brachistochrone/386/params.json new file mode 100644 index 0000000000000000000000000000000000000000..763c66e0f9ee63dca851239a24f4e081dc164ed4 --- /dev/null +++ b/scipy/brachistochrone/386/params.json @@ -0,0 +1,8 @@ +{ + "r": 2.7460505531088426, + "m": 0.6108027127526061, + "b": 0.2535319449366436, + "g": 9.8, + "theta0": 0.2819500103540793, + "omega0": 0.0 +} \ No newline at end of file diff --git a/scipy/brachistochrone/386/simulation_code.py b/scipy/brachistochrone/386/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c8361c150ce230141f7547469446a470a61af9f7 --- /dev/null +++ b/scipy/brachistochrone/386/simulation_code.py @@ -0,0 +1,83 @@ +""" +Brachistochrone (Cycloid) — auto-generated simulation script. +Physics: domega/dt = [-g*(dy/dtheta) - (b/m)*S1*omega - S1*S2*omega^2] / S1^2 +Track: x=r*(theta-sin(theta)), y=-r*(1-cos(theta)) [screen coords] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +r = 2.7460505531088426 +m = 0.6108027127526061 +b = 0.2535319449366436 +g = 9.8 +theta0 = 0.281950 +omega0 = 0.000000 +duration = 12 + + +def cycloid(theta): + x = r * (theta - np.sin(theta)) + y = r * (1.0 - np.cos(theta)) + return x, y + + + + + + +def ode(t, state): + theta, omega = state + half = 0.5 * theta + S1 = 2.0 * r * np.sin(half) + S2 = r * np.cos(half) + dydth = r * np.sin(theta) + S1_sq = S1 * S1 + if S1_sq < 1e-14: + alpha = g / (2.0 * r) + else: + drag = (b / m) * S1 * omega + alpha = (g * dydth - drag - S1 * S2 * omega * omega) / S1_sq + return [omega, alpha] + + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [theta0, omega0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +t, theta_arr = sol.t, sol.y[0] +x_ball, y_down = cycloid(theta_arr) +y_ball = -y_down + + +th_tr = np.linspace(0, 2*np.pi, 600) +x_tr, y_tr_d = cycloid(th_tr) +y_tr = -y_tr_d + + +pad = r * 0.3 +fig, ax = plt.subplots(figsize=(8, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(x_tr.min() - pad, x_tr.max() + pad) +ax.set_ylim(y_tr.min() - pad, y_tr.max() + pad) +ax.set_aspect("equal"); ax.axis("off") +ax.plot(x_tr, y_tr, "-", color="#a8dadc", lw=2.0, alpha=0.7) + +ball, = ax.plot([], [], "o", color="#e63946", ms=12, zorder=5) +trail, = ax.plot([], [], "-", color="#e63946", alpha=0.4, lw=1.5) +tx, ty = [], [] + +def update(frame): + ball.set_data([x_ball[frame]], [y_ball[frame]]) + tx.append(x_ball[frame]); ty.append(y_ball[frame]) + if len(tx) > 60: tx.pop(0); ty.pop(0) + trail.set_data(tx, ty) + return ball, trail + +ani = animation.FuncAnimation(fig, update, frames=len(t), interval=1000/fps, blit=True) +writer = animation.FFMpegWriter(fps=fps, bitrate=1800) +ani.save("brachistochrone.mp4", writer=writer, dpi=120) +plt.close() +print("Saved brachistochrone.mp4") diff --git a/scipy/brachistochrone/386/video.mp4 b/scipy/brachistochrone/386/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..fc424c97a72a330940802d5fe1b30a5deed0606b --- /dev/null +++ b/scipy/brachistochrone/386/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:082401767adb93dfb7decc8ed6b704af34e28dcfec19f320c1e9a800aedad01b +size 10824 diff --git a/scipy/brachistochrone_race/001/cot.json b/scipy/brachistochrone_race/001/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..f56c2c4a29ebecc05cfb6f6f0a224637ee1e1eaa --- /dev/null +++ b/scipy/brachistochrone_race/001/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=1.879 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=2.967 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=0.939738, clamp at theta_e=3.149439", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 5.24417 \\cdot t^2, clamp at L=3.512200", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=0.939738, clamp * at * theta_e=3.149439", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 5.24417 * t**2, clamp * at * L=3.512200", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,1.879), B=(2.967,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.14944 rad (180.45 deg). R = 0.93974 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 0.9753 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 3.5122 m, \\sin alpha = H/L, a = g \\cdot H/L = 5.2442 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.1574 s. Difference Delta T = 0.1821 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.149439404236977, + "theta_e_deg": 180.44958569497518, + "R_m": 0.9397383724206053, + "T_brach_s": 0.9752673820478949, + "T_straight_s": 1.1573535891885585, + "delta_T_s": 0.1820862071406636, + "chord_length_L_m": 3.51219999979845, + "chord_along_ramp_acceleration_m_s2": 5.244174187853594 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 0.975 s.", + "Chord bead reaches B at t ~ 1.157 s, ~0.182 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 1.8794478144410645, + "unit": "", + "description": "" + }, + "D": { + "value": 2.967022876786922, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/001/params.json b/scipy/brachistochrone_race/001/params.json new file mode 100644 index 0000000000000000000000000000000000000000..6629fe71212f20501ccf7817af62f34f6722a7aa --- /dev/null +++ b/scipy/brachistochrone_race/001/params.json @@ -0,0 +1,6 @@ +{ + "H": 1.8794478144410645, + "D": 2.967022876786922, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/001/simulation_code.py b/scipy/brachistochrone_race/001/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/scipy/brachistochrone_race/001/video.mp4 b/scipy/brachistochrone_race/001/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1a75c09c0c33f7460782a387f64e64188e7cca8f --- /dev/null +++ b/scipy/brachistochrone_race/001/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3a3e918e155a3b5b4d3e1730d427688b3b669456bea9449abb8aff482a931bbc +size 10792 diff --git a/scipy/brachistochrone_race/005/cot.json b/scipy/brachistochrone_race/005/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..a61dc5262ef686195f82e8df3e63efc5bc181649 --- /dev/null +++ b/scipy/brachistochrone_race/005/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=2.306 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=3.033 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=1.176781, clamp at theta_e=2.857737", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 5.93163 \\cdot t^2, clamp at L=3.810657", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=1.176781, clamp * at * theta_e=2.857737", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 5.93163 * t**2, clamp * at * L=3.810657", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,2.306), B=(3.033,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 2.85774 rad (163.74 deg). R = 1.17678 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 0.9903 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 3.8107 m, \\sin alpha = H/L, a = g \\cdot H/L = 5.9316 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.1335 s. Difference Delta T = 0.1432 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 2.8577370271328513, + "theta_e_deg": 163.7362706129752, + "R_m": 1.1767814804985586, + "T_brach_s": 0.9902780131052977, + "T_straight_s": 1.1335162876563154, + "delta_T_s": 0.14323827455101767, + "chord_length_L_m": 3.810656870843877, + "chord_along_ramp_acceleration_m_s2": 5.931633515675043 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 0.990 s.", + "Chord bead reaches B at t ~ 1.134 s, ~0.143 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 2.306471429779074, + "unit": "", + "description": "" + }, + "D": { + "value": 3.0333636990843225, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/005/params.json b/scipy/brachistochrone_race/005/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c3c2f8122b26e4dd5c8b8e7b33490075ef66e98c --- /dev/null +++ b/scipy/brachistochrone_race/005/params.json @@ -0,0 +1,6 @@ +{ + "H": 2.306471429779074, + "D": 3.0333636990843225, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/005/simulation_code.py b/scipy/brachistochrone_race/005/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..56f5422d15af5702b42bb254e976bac8f0f9b179 --- /dev/null +++ b/scipy/brachistochrone_race/005/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 2.306471429779074 +D = 3.0333636990843225 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/005/video.mp4 b/scipy/brachistochrone_race/005/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..eae67cbb3f15760d6682a736fcb4a16b1069110f --- /dev/null +++ b/scipy/brachistochrone_race/005/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e04dc053412fe89bdfcf52fc726c081060f12f99e986f236a2b15ad39d8cbf95 +size 10411 diff --git a/scipy/brachistochrone_race/016/cot.json b/scipy/brachistochrone_race/016/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..7f616b0c7cec0224616a4abdd91f66ee0f149044 --- /dev/null +++ b/scipy/brachistochrone_race/016/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=2.220 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=3.830 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=1.115957, clamp at theta_e=3.287200", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 4.91441 \\cdot t^2, clamp at L=4.427190", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=1.115957, clamp * at * theta_e=3.287200", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 4.91441 * t**2, clamp * at * L=4.427190", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,2.220), B=(3.830,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.28720 rad (188.34 deg). R = 1.11596 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 1.1093 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 4.4272 m, \\sin alpha = H/L, a = g \\cdot H/L = 4.9144 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.3423 s. Difference Delta T = 0.2330 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.2872000099173655, + "theta_e_deg": 188.3426869836274, + "R_m": 1.1159573664293057, + "T_brach_s": 1.1092690438134427, + "T_straight_s": 1.3422806640378655, + "delta_T_s": 0.2330116202244228, + "chord_length_L_m": 4.4271903327504685, + "chord_along_ramp_acceleration_m_s2": 4.914411526818448 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 1.109 s.", + "Chord bead reaches B at t ~ 1.342 s, ~0.233 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 2.220105632927357, + "unit": "", + "description": "" + }, + "D": { + "value": 3.8302930985818078, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/016/params.json b/scipy/brachistochrone_race/016/params.json new file mode 100644 index 0000000000000000000000000000000000000000..409915fee49057219b992c5513a38c79c9859f94 --- /dev/null +++ b/scipy/brachistochrone_race/016/params.json @@ -0,0 +1,6 @@ +{ + "H": 2.220105632927357, + "D": 3.8302930985818078, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/016/simulation_code.py b/scipy/brachistochrone_race/016/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..46f66bd9fc4061a2eded8a5d7ea9ff029c5a7b79 --- /dev/null +++ b/scipy/brachistochrone_race/016/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 2.220105632927357 +D = 3.8302930985818078 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/016/video.mp4 b/scipy/brachistochrone_race/016/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e5c44703c756cd395992e6099ed6f7b70c5f4bf3 --- /dev/null +++ b/scipy/brachistochrone_race/016/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a933941e2c83bde30d783f13211b7f250ab32afeaeaf13ad6d967bbff2e6f4a4 +size 11138 diff --git a/scipy/brachistochrone_race/018/cot.json b/scipy/brachistochrone_race/018/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..6f959ac58258538e17442411e48828b9aa711df1 --- /dev/null +++ b/scipy/brachistochrone_race/018/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=1.833 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=2.693 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=0.918904, clamp at theta_e=3.036092", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 5.51348 \\cdot t^2, clamp at L=3.257551", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=0.918904, clamp * at * theta_e=3.036092", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 5.51348 * t**2, clamp * at * L=3.257551", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,1.833), B=(2.693,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.03609 rad (173.96 deg). R = 0.91890 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 0.9297 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 3.2576 m, \\sin alpha = H/L, a = g \\cdot H/L = 5.5135 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.0870 s. Difference Delta T = 0.1574 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.0360921979450275, + "theta_e_deg": 173.9552691548478, + "R_m": 0.9189040600647125, + "T_brach_s": 0.9296875243833816, + "T_straight_s": 1.087045297783686, + "delta_T_s": 0.15735777340030432, + "chord_length_L_m": 3.2575512225150636, + "chord_along_ramp_acceleration_m_s2": 5.513482056858195 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 0.930 s.", + "Chord bead reaches B at t ~ 1.087 s, ~0.157 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 1.8326990014931916, + "unit": "", + "description": "" + }, + "D": { + "value": 2.6931123885265618, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/018/params.json b/scipy/brachistochrone_race/018/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c9d3d298479d79b8d09a0711b537316dc499ebff --- /dev/null +++ b/scipy/brachistochrone_race/018/params.json @@ -0,0 +1,6 @@ +{ + "H": 1.8326990014931916, + "D": 2.6931123885265618, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/018/simulation_code.py b/scipy/brachistochrone_race/018/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..b557a0ec5213d875108d0dcabbad0be1013f9e32 --- /dev/null +++ b/scipy/brachistochrone_race/018/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 1.8326990014931916 +D = 2.6931123885265618 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/018/video.mp4 b/scipy/brachistochrone_race/018/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..908d81fecd0c0bd9e844ae9ab714c991cf53f1d5 --- /dev/null +++ b/scipy/brachistochrone_race/018/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bf5ea994b9e6e7d79aa14848209c6b2b0181bacafb2c75e07a0c2b79bc1e05d4 +size 10369 diff --git a/scipy/brachistochrone_race/030/cot.json b/scipy/brachistochrone_race/030/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..4dcf4593f5d53a919f9486cdaa3f037e826f9fb0 --- /dev/null +++ b/scipy/brachistochrone_race/030/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=2.104 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=3.596 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=1.056361, clamp at theta_e=3.273164", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 4.94810 \\cdot t^2, clamp at L=4.166290", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=1.056361, clamp * at * theta_e=3.273164", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 4.94810 * t**2, clamp * at * L=4.166290", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,2.104), B=(3.596,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.27316 rad (187.54 deg). R = 1.05636 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 1.0746 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 4.1663 m, \\sin alpha = H/L, a = g \\cdot H/L = 4.9481 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.2977 s. Difference Delta T = 0.2231 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.273163733130722, + "theta_e_deg": 187.53846756367528, + "R_m": 1.0563614505363348, + "T_brach_s": 1.0746349597005318, + "T_straight_s": 1.2976889963668352, + "delta_T_s": 0.2230540366663034, + "chord_length_L_m": 4.166290009104884, + "chord_along_ramp_acceleration_m_s2": 4.948097501245731 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 1.075 s.", + "Chord bead reaches B at t ~ 1.298 s, ~0.223 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 2.1035927738282583, + "unit": "", + "description": "" + }, + "D": { + "value": 3.5962299539746776, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/030/params.json b/scipy/brachistochrone_race/030/params.json new file mode 100644 index 0000000000000000000000000000000000000000..cba33e433d086874e0fb4a471f564d46a4ebcd42 --- /dev/null +++ b/scipy/brachistochrone_race/030/params.json @@ -0,0 +1,6 @@ +{ + "H": 2.1035927738282583, + "D": 3.5962299539746776, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/030/simulation_code.py b/scipy/brachistochrone_race/030/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..faef0d2b6fdeaec4368241cfb4683ac7adaed904 --- /dev/null +++ b/scipy/brachistochrone_race/030/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 2.1035927738282583 +D = 3.5962299539746776 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/030/video.mp4 b/scipy/brachistochrone_race/030/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..b75bd4bd0bceac4927a7bcd5d622bc1bed2eb798 --- /dev/null +++ b/scipy/brachistochrone_race/030/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:de8bbef1e58369ba64fcf2b1dc36883ba7527e42b022ea3e82b8117bb409669a +size 11139 diff --git a/scipy/brachistochrone_race/056/cot.json b/scipy/brachistochrone_race/056/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..23ebf387e268b5d6b080d6f57dc4bde74692a18c --- /dev/null +++ b/scipy/brachistochrone_race/056/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=2.164 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=3.907 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=1.094673, clamp at theta_e=3.356149", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 4.74876 \\cdot t^2, clamp at L=4.466348", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=1.094673, clamp * at * theta_e=3.356149", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 4.74876 * t**2, clamp * at * L=4.466348", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,2.164), B=(3.907,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.35615 rad (192.29 deg). R = 1.09467 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 1.1217 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 4.4663 m, \\sin alpha = H/L, a = g \\cdot H/L = 4.7488 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.3715 s. Difference Delta T = 0.2498 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.3561485885787263, + "theta_e_deg": 192.29314954434915, + "R_m": 1.0946728146042166, + "T_brach_s": 1.1216834418146842, + "T_straight_s": 1.3715169174194797, + "delta_T_s": 0.24983347560479552, + "chord_length_L_m": 4.466347525950584, + "chord_along_ramp_acceleration_m_s2": 4.748759444188453 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 1.122 s.", + "Chord bead reaches B at t ~ 1.372 s, ~0.250 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 2.164245917845466, + "unit": "", + "description": "" + }, + "D": { + "value": 3.9069553145197533, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/056/params.json b/scipy/brachistochrone_race/056/params.json new file mode 100644 index 0000000000000000000000000000000000000000..96a4ead5d73d791bf5eee7c3bc8e1db4b035b7e8 --- /dev/null +++ b/scipy/brachistochrone_race/056/params.json @@ -0,0 +1,6 @@ +{ + "H": 2.164245917845466, + "D": 3.9069553145197533, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/056/simulation_code.py b/scipy/brachistochrone_race/056/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d0847a518c6d66a26b925cf9dd5f3d1b6b484bb1 --- /dev/null +++ b/scipy/brachistochrone_race/056/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 2.164245917845466 +D = 3.9069553145197533 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/056/video.mp4 b/scipy/brachistochrone_race/056/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3e68917e6d51c4d7ac6bc77a7b3ded69c735dd0c --- /dev/null +++ b/scipy/brachistochrone_race/056/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:908ec007a01bc93b691861feba71f195b3099a7ac64aecea64b89af39685ca23 +size 11190 diff --git a/scipy/brachistochrone_race/057/cot.json b/scipy/brachistochrone_race/057/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..b8d79f266502fa3c1537269079320cd71d330a77 --- /dev/null +++ b/scipy/brachistochrone_race/057/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=1.923 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=2.584 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=0.976621, clamp at theta_e=2.892514", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 5.85075 \\cdot t^2, clamp at L=3.221196", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=0.976621, clamp * at * theta_e=2.892514", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 5.85075 * t**2, clamp * at * L=3.221196", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,1.923), B=(2.584,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 2.89251 rad (165.73 deg). R = 0.97662 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 0.9131 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 3.2212 m, \\sin alpha = H/L, a = g \\cdot H/L = 5.8507 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.0493 s. Difference Delta T = 0.1362 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 2.892513799493906, + "theta_e_deg": 165.72883289435083, + "R_m": 0.9766205338769703, + "T_brach_s": 0.9131146850797053, + "T_straight_s": 1.0493440703399313, + "delta_T_s": 0.136229385260226, + "chord_length_L_m": 3.2211961039613177, + "chord_along_ramp_acceleration_m_s2": 5.850747225230328 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 0.913 s.", + "Chord bead reaches B at t ~ 1.049 s, ~0.136 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 1.9231024660382063, + "unit": "", + "description": "" + }, + "D": { + "value": 2.5841403300311194, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/057/params.json b/scipy/brachistochrone_race/057/params.json new file mode 100644 index 0000000000000000000000000000000000000000..06c07e6e78edf3c166e496598168d344134e746a --- /dev/null +++ b/scipy/brachistochrone_race/057/params.json @@ -0,0 +1,6 @@ +{ + "H": 1.9231024660382063, + "D": 2.5841403300311194, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/057/simulation_code.py b/scipy/brachistochrone_race/057/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d3b50e1573f41d5772907b32fb63ac969808d5bd --- /dev/null +++ b/scipy/brachistochrone_race/057/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 1.9231024660382063 +D = 2.5841403300311194 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/057/video.mp4 b/scipy/brachistochrone_race/057/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..41363ce764be3c57d97a798bb0b86d29905ea8d3 --- /dev/null +++ b/scipy/brachistochrone_race/057/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:934e6a4414f2eda61f017bbbba319ec2c7dc01101586322736607b1178614cb0 +size 10318 diff --git a/scipy/brachistochrone_race/058/cot.json b/scipy/brachistochrone_race/058/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..a1431dde28bde29e8ad8949158c12e84264d5b68 --- /dev/null +++ b/scipy/brachistochrone_race/058/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=2.070 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=3.156 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=1.035783, clamp at theta_e=3.094437", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 5.37514 \\cdot t^2, clamp at L=3.774798", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=1.035783, clamp * at * theta_e=3.094437", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 5.37514 * t**2, clamp * at * L=3.774798", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,2.070), B=(3.156,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.09444 rad (177.30 deg). R = 1.03578 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 1.0060 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 3.7748 m, \\sin alpha = H/L, a = g \\cdot H/L = 5.3751 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.1851 s. Difference Delta T = 0.1791 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.094436931696665, + "theta_e_deg": 177.2981761556311, + "R_m": 1.0357829892680468, + "T_brach_s": 1.006011591942049, + "T_straight_s": 1.185132785984676, + "delta_T_s": 0.17912119404262716, + "chord_length_L_m": 3.7747979547518367, + "chord_along_ramp_acceleration_m_s2": 5.3751387730556255 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 1.006 s.", + "Chord bead reaches B at t ~ 1.185 s, ~0.179 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 2.0704145762283335, + "unit": "", + "description": "" + }, + "D": { + "value": 3.156340140374592, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/058/params.json b/scipy/brachistochrone_race/058/params.json new file mode 100644 index 0000000000000000000000000000000000000000..dfe5cbc9e3ce9ec2b1df0c4c2ed1c5fc9fcc6185 --- /dev/null +++ b/scipy/brachistochrone_race/058/params.json @@ -0,0 +1,6 @@ +{ + "H": 2.0704145762283335, + "D": 3.156340140374592, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/058/simulation_code.py b/scipy/brachistochrone_race/058/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..baebdb0a0f114805f368cd5e099697121f122e17 --- /dev/null +++ b/scipy/brachistochrone_race/058/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 2.0704145762283335 +D = 3.156340140374592 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/058/video.mp4 b/scipy/brachistochrone_race/058/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..4f086360a6ffc11ee88292fab0eab1c8504a5629 --- /dev/null +++ b/scipy/brachistochrone_race/058/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f93204002012d8bde8cd16081abdfcde930dedda14194be4f1dcfa626bb8af4c +size 10706 diff --git a/scipy/brachistochrone_race/063/cot.json b/scipy/brachistochrone_race/063/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..35e9abfc6e2d0f589f7e29c0718ee1ea9372b9e8 --- /dev/null +++ b/scipy/brachistochrone_race/063/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=1.926 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=4.310 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=1.032986, clamp at theta_e=3.669041", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 3.99753 \\cdot t^2, clamp at L=4.720592", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=1.032986, clamp * at * theta_e=3.669041", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 3.99753 * t**2, clamp * at * L=4.720592", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,1.926), B=(4.310,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.66904 rad (210.22 deg). R = 1.03299 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 1.1912 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 4.7206 m, \\sin alpha = H/L, a = g \\cdot H/L = 3.9975 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.5368 s. Difference Delta T = 0.3456 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.6690410385376957, + "theta_e_deg": 210.22056636850638, + "R_m": 1.032986323944198, + "T_brach_s": 1.191205849686057, + "T_straight_s": 1.5367996710101146, + "delta_T_s": 0.3455938213240577, + "chord_length_L_m": 4.7205924868927776, + "chord_along_ramp_acceleration_m_s2": 3.997532366459577 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 1.191 s.", + "Chord bead reaches B at t ~ 1.537 s, ~0.346 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 1.925583801553039, + "unit": "", + "description": "" + }, + "D": { + "value": 4.310002372447732, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/063/params.json b/scipy/brachistochrone_race/063/params.json new file mode 100644 index 0000000000000000000000000000000000000000..7e331e9115b3d972a0f8c00bb891993902b0692f --- /dev/null +++ b/scipy/brachistochrone_race/063/params.json @@ -0,0 +1,6 @@ +{ + "H": 1.925583801553039, + "D": 4.310002372447732, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/063/simulation_code.py b/scipy/brachistochrone_race/063/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d3dbd978c35fae8eaad7c363d4d212584c47682c --- /dev/null +++ b/scipy/brachistochrone_race/063/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 1.925583801553039 +D = 4.310002372447732 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/063/video.mp4 b/scipy/brachistochrone_race/063/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..b52e77ceb51b26234dd76c8ebcf8ae5091888d7a --- /dev/null +++ b/scipy/brachistochrone_race/063/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b6591f94ac45f2b4ef1866d5b9587469955f4ccfacc49e4afc3af3b7da1c6d0 +size 11660 diff --git a/scipy/brachistochrone_race/086/cot.json b/scipy/brachistochrone_race/086/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..d8839da3ef93df6e8d175b98647a21061003d293 --- /dev/null +++ b/scipy/brachistochrone_race/086/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=2.153 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=4.112 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=1.100841, clamp at theta_e=3.440481", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 4.54593 \\cdot t^2, clamp at L=4.641110", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=1.100841, clamp * at * theta_e=3.440481", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 4.54593 * t**2, clamp * at * L=4.641110", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,2.153), B=(4.112,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.44048 rad (197.13 deg). R = 1.10084 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 1.1531 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 4.6411 m, \\sin alpha = H/L, a = g \\cdot H/L = 4.5459 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.4289 s. Difference Delta T = 0.2758 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.440481411615102, + "theta_e_deg": 197.12506437875712, + "R_m": 1.1008405614433805, + "T_brach_s": 1.1531037632372505, + "T_straight_s": 1.428941576501083, + "delta_T_s": 0.2758378132638326, + "chord_length_L_m": 4.6411101672542525, + "chord_along_ramp_acceleration_m_s2": 4.545931924513326 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 1.153 s.", + "Chord bead reaches B at t ~ 1.429 s, ~0.276 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 2.15287457903107, + "unit": "", + "description": "" + }, + "D": { + "value": 4.1115732550390724, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/086/params.json b/scipy/brachistochrone_race/086/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c8c367a83a83902a2ed38833c3008119486ff99d --- /dev/null +++ b/scipy/brachistochrone_race/086/params.json @@ -0,0 +1,6 @@ +{ + "H": 2.15287457903107, + "D": 4.1115732550390724, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/086/simulation_code.py b/scipy/brachistochrone_race/086/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..612dd71955ce2ef07934215e091b4154f0daea2a --- /dev/null +++ b/scipy/brachistochrone_race/086/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 2.15287457903107 +D = 4.1115732550390724 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/086/video.mp4 b/scipy/brachistochrone_race/086/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..b3398554423091ec85c809c9bcbdfb29adb5e242 --- /dev/null +++ b/scipy/brachistochrone_race/086/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:392472df52f6b54026700d6e71a880107fa220bd62703e582df9bb167883bf7e +size 11524 diff --git a/scipy/brachistochrone_race/112/cot.json b/scipy/brachistochrone_race/112/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..562918f7b04de69057b894e6911e918d2439c488 --- /dev/null +++ b/scipy/brachistochrone_race/112/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=1.883 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=3.909 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=0.984534, clamp at theta_e=3.562169", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 4.25347 \\cdot t^2, clamp at L=4.339050", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=0.984534, clamp * at * theta_e=3.562169", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 4.25347 * t**2, clamp * at * L=4.339050", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,1.883), B=(3.909,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.56217 rad (204.10 deg). R = 0.98453 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 1.1291 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 4.3390 m, \\sin alpha = H/L, a = g \\cdot H/L = 4.2535 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.4284 s. Difference Delta T = 0.2993 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.5621691551723544, + "theta_e_deg": 204.09725850305796, + "R_m": 0.9845336600981572, + "T_brach_s": 1.1290593856420732, + "T_straight_s": 1.4283690849620079, + "delta_T_s": 0.2993096993199347, + "chord_length_L_m": 4.339049909501235, + "chord_along_ramp_acceleration_m_s2": 4.253473754502743 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 1.129 s.", + "Chord bead reaches B at t ~ 1.428 s, ~0.299 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 1.8832688683205108, + "unit": "", + "description": "" + }, + "D": { + "value": 3.909047516564292, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/112/params.json b/scipy/brachistochrone_race/112/params.json new file mode 100644 index 0000000000000000000000000000000000000000..d5ee4be83613a674d8a5572ab03a94300dec0ad6 --- /dev/null +++ b/scipy/brachistochrone_race/112/params.json @@ -0,0 +1,6 @@ +{ + "H": 1.8832688683205108, + "D": 3.909047516564292, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/112/simulation_code.py b/scipy/brachistochrone_race/112/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..9e97b925e913b551d6947080c4fadfe66e4de9c4 --- /dev/null +++ b/scipy/brachistochrone_race/112/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 1.8832688683205108 +D = 3.909047516564292 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/112/video.mp4 b/scipy/brachistochrone_race/112/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..dd24a8a6ce22ada9848434d090f1ae0dc08bc7b5 --- /dev/null +++ b/scipy/brachistochrone_race/112/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4f612bad9f078388a5812d7d0ab2ed7f6e4a31f1a2be6a617e67ad4dc5b972fc +size 11324 diff --git a/scipy/brachistochrone_race/115/cot.json b/scipy/brachistochrone_race/115/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..0069b75509b2a9a696f8c5888f9755266237f37c --- /dev/null +++ b/scipy/brachistochrone_race/115/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=2.121 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=4.455 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=1.112823, clamp at theta_e=3.579308", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 4.21235 \\cdot t^2, clamp at L=4.933865", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=1.112823, clamp * at * theta_e=3.579308", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 4.21235 * t**2, clamp * at * L=4.933865", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,2.121), B=(4.455,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.57931 rad (205.08 deg). R = 1.11282 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 1.2061 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 4.9339 m, \\sin alpha = H/L, a = g \\cdot H/L = 4.2124 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.5305 s. Difference Delta T = 0.3244 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.579308021203641, + "theta_e_deg": 205.07924319229082, + "R_m": 1.1128231573417706, + "T_brach_s": 1.2061438676331988, + "T_straight_s": 1.5305456825720825, + "delta_T_s": 0.32440181493888365, + "chord_length_L_m": 4.933864692606734, + "chord_along_ramp_acceleration_m_s2": 4.21235182773518 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 1.206 s.", + "Chord bead reaches B at t ~ 1.531 s, ~0.324 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 2.1207320362959234, + "unit": "", + "description": "" + }, + "D": { + "value": 4.454830685354887, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/115/params.json b/scipy/brachistochrone_race/115/params.json new file mode 100644 index 0000000000000000000000000000000000000000..15999bcab4445dbefa3231d176d18e9e71ea593e --- /dev/null +++ b/scipy/brachistochrone_race/115/params.json @@ -0,0 +1,6 @@ +{ + "H": 2.1207320362959234, + "D": 4.454830685354887, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/115/simulation_code.py b/scipy/brachistochrone_race/115/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..8e75d2746c41c9974f7870afcb7a293b3ae85561 --- /dev/null +++ b/scipy/brachistochrone_race/115/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 2.1207320362959234 +D = 4.454830685354887 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/115/video.mp4 b/scipy/brachistochrone_race/115/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..cea042b9a7100a4401fc60d35c170c27b65247f7 --- /dev/null +++ b/scipy/brachistochrone_race/115/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b2a5124545ce944f0a86da6e591275602037a4fd65d6da544b0c0269f0f78703 +size 11521 diff --git a/scipy/brachistochrone_race/132/cot.json b/scipy/brachistochrone_race/132/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..bf41921a4026b75e780b93364e0f631208b38376 --- /dev/null +++ b/scipy/brachistochrone_race/132/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=1.525 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=3.448 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=0.821397, clamp at theta_e=3.682653", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 3.96503 \\cdot t^2, clamp at L=3.770357", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=0.821397, clamp * at * theta_e=3.682653", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 3.96503 * t**2, clamp * at * L=3.770357", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,1.525), B=(3.448,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.68265 rad (211.00 deg). R = 0.82140 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 1.0662 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 3.7704 m, \\sin alpha = H/L, a = g \\cdot H/L = 3.9650 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.3791 s. Difference Delta T = 0.3129 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.682652877346823, + "theta_e_deg": 211.00046728368176, + "R_m": 0.8213968884801958, + "T_brach_s": 1.0661646369019042, + "T_straight_s": 1.379059087908082, + "delta_T_s": 0.31289445100617774, + "chord_length_L_m": 3.7703569183744494, + "chord_along_ramp_acceleration_m_s2": 3.965032129420492 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 1.066 s.", + "Chord bead reaches B at t ~ 1.379 s, ~0.313 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 1.5254679919119924, + "unit": "", + "description": "" + }, + "D": { + "value": 3.4479760291489945, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/132/params.json b/scipy/brachistochrone_race/132/params.json new file mode 100644 index 0000000000000000000000000000000000000000..24e243a61ad5067c25212406bac10172d8fb91db --- /dev/null +++ b/scipy/brachistochrone_race/132/params.json @@ -0,0 +1,6 @@ +{ + "H": 1.5254679919119924, + "D": 3.4479760291489945, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/132/simulation_code.py b/scipy/brachistochrone_race/132/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..736666095fa64398c5e4bbab5e27179f998bd5fa --- /dev/null +++ b/scipy/brachistochrone_race/132/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 1.5254679919119924 +D = 3.4479760291489945 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/132/video.mp4 b/scipy/brachistochrone_race/132/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..977dc917804067b1eff5717c587bc2d19e64f970 --- /dev/null +++ b/scipy/brachistochrone_race/132/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b440f89ecdbe71ff19b86a38ca4287fa331b7fed7cee7f43fd20bc75e555679b +size 11543 diff --git a/scipy/brachistochrone_race/141/cot.json b/scipy/brachistochrone_race/141/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..d3971183a8afa89902dbb7ed41ae1476b39f3781 --- /dev/null +++ b/scipy/brachistochrone_race/141/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=2.261 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=4.196 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=1.149395, clamp at theta_e=3.397604", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 4.64907 \\cdot t^2, clamp at L=4.766767", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=1.149395, clamp * at * theta_e=3.397604", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 4.64907 * t**2, clamp * at * L=4.766767", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,2.261), B=(4.196,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.39760 rad (194.67 deg). R = 1.14939 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 1.1636 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 4.7668 m, \\sin alpha = H/L, a = g \\cdot H/L = 4.6491 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.4320 s. Difference Delta T = 0.2684 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.3976041938854764, + "theta_e_deg": 194.66838076558605, + "R_m": 1.1493948849848337, + "T_brach_s": 1.1635749856222888, + "T_straight_s": 1.4320033796813076, + "delta_T_s": 0.26842839405901886, + "chord_length_L_m": 4.766766628759772, + "chord_along_ramp_acceleration_m_s2": 4.649066946087663 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 1.164 s.", + "Chord bead reaches B at t ~ 1.432 s, ~0.268 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 2.2613282830082424, + "unit": "", + "description": "" + }, + "D": { + "value": 4.196243378252124, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/141/params.json b/scipy/brachistochrone_race/141/params.json new file mode 100644 index 0000000000000000000000000000000000000000..3cc9430b75f85e62a9cd4ab39ed1ca7e3d9c8ee7 --- /dev/null +++ b/scipy/brachistochrone_race/141/params.json @@ -0,0 +1,6 @@ +{ + "H": 2.2613282830082424, + "D": 4.196243378252124, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/141/simulation_code.py b/scipy/brachistochrone_race/141/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0e6157b35517e90b9e4485cb3a364f763d1c9eb5 --- /dev/null +++ b/scipy/brachistochrone_race/141/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 2.2613282830082424 +D = 4.196243378252124 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/141/video.mp4 b/scipy/brachistochrone_race/141/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..48a07166774d3a54ea57a1c2852cee6e3a57751a --- /dev/null +++ b/scipy/brachistochrone_race/141/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1defcd05ac7cb07f2e71a0f06f676bd7b97fd556e855c77084a2e9a90c32cdef +size 11582 diff --git a/scipy/brachistochrone_race/150/cot.json b/scipy/brachistochrone_race/150/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..de3d177997d3f0a7b54c9faaaecc2770e3920099 --- /dev/null +++ b/scipy/brachistochrone_race/150/cot.json @@ -0,0 +1,82 @@ +{ + "simulation": "brachistochrone_race", + "physical_observation": "Two identical point-mass beads start at rest at A=(0,H) on a vertical wall (H=2.380 m) and slide frictionlessly under gravity (g=9.800 m/s^2) to B=(D,0) on the floor (D=3.843 m). One descends along the straight chord A->B; the other along the brachistochrone (cycloid arc). The cycloid bead reaches B first; both then rest at B.", + "physical_law": { + "name": "Brachistochrone problem (Bernoulli, 1696) and energy conservation on a frictionless ramp", + "statement": "Among smooth curves connecting two points in a uniform gravitational field, the cycloid with cusp at A minimises the time of descent of a bead released from rest. v=\\sqrt(2 g h) on any frictionless wire.", + "formula": "Cycloid: x=R(theta-\\sin theta), y=H-R(1-\\cos theta); endpoint: (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H, R=H/(1-\\cos theta_e); theta(t)=\\sqrt(g/R) t; T_brach=\\sqrt(R/g) theta_e. Chord: L=\\sqrt(D^2+H^2), a=g H/L, T_straight=\\sqrt(2 L^2/(g H))." + }, + "ode_system": { + "state_variables": [ + "theta(t): cycloid parameter (rad)", + "s(t): arc length along the chord (m)" + ], + "equations_latex": [ + "\\theta(t) = \\sqrt(g/R) \\cdot t, R=1.190503, clamp at theta_e=3.184687", + "x_cyc(t) = R \\cdot (\\theta - \\sin(\\theta)); y_cyc(t) = H - R \\cdot (1 - \\cos(\\theta))", + "s(t) = 0.5 \\cdot (g \\cdot H/L) \\cdot t^2 = 0.5 \\cdot 5.16001 \\cdot t^2, clamp at L=4.519958", + "x_str(t) = (D/L) \\cdot s; y_str(t) = H - (H/L) \\cdot s" + ], + "equations_python": [ + "theta (t) = np.sqrt (g/R) * t, R=1.190503, clamp * at * theta_e=3.184687", + "x_cyc(t) = R * ( theta - np.sin ( theta )); y_cyc(t) = H - R * (1 - np.cos ( theta ))", + "s(t) = 0.5 * (g * H/L) * t**2 = 0.5 * 5.16001 * t**2, clamp * at * L=4.519958", + "x_str(t) = (D/L) * s; y_str(t) = H - (H/L) * s" + ] + }, + "derivation_steps": [ + "A=(0,2.380), B=(3.843,0). Gravity g=9.800.", + "Cycloid with cusp at A: x=R(theta-\\sin theta), y=H-R(1-\\cos theta). Endpoint constraints R(theta_e-\\sin theta_e)=D, R(1-\\cos theta_e)=H give (theta_e-\\sin theta_e)/(1-\\cos theta_e)=D/H.", + "Numerical brentq solve: theta_e = 3.18469 rad (182.47 deg). R = 1.19050 m.", + "Energy conservation along the cycloid yields (d theta/dt)^2 = g/R, so theta(t) = \\sqrt(g/R) t and T_brach = \\sqrt(R/g) theta_e = 1.1100 s.", + "Chord A->B: L = \\sqrt(D^2+H^2) = 4.5200 m, \\sin alpha = H/L, a = g \\cdot H/L = 5.1600 m/s^2.", + "s(t) = (1/2) a t^2; T_straight = \\sqrt(2L^2/(gH)) = 1.3236 s. Difference Delta T = 0.2136 s, so cycloid wins." + ], + "physical_constants_derived": { + "theta_e_rad": 3.184686663201309, + "theta_e_deg": 182.46910487303606, + "R_m": 1.1905032867528522, + "T_brach_s": 1.1099897558578111, + "T_straight_s": 1.3236005286303276, + "delta_T_s": 0.2136107727725165, + "chord_length_L_m": 4.5199584824265795, + "chord_along_ramp_acceleration_m_s2": 5.160010405963365 + }, + "numerical_method": { + "solver": "scipy.optimize.brentq for the cycloid endpoint equation; closed-form kinematics for both beads.", + "tolerance": "brentq xtol=1e-12 on theta_e in (1e-6, 2 \\cdot pi - 1e-6).", + "justification": "Both motions are integrable in closed form; sampling x(t),y(t) on the time grid avoids ODE-integrator drift." + }, + "code_validation": { + "ode_source": "Cycloid endpoint equations and closed-form kinematics on chord and cycloid.", + "energy_check": "(1/2)(\\dot{s})^2 = g(H - y) holds identically on the cycloid given theta(t)=\\sqrt(g/R)t.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Both beads start at A; cycloid bead reaches B at t ~ 1.110 s.", + "Chord bead reaches B at t ~ 1.324 s, ~0.214 s later.", + "After arrival, beads rest at B for the remainder of the window." + ], + "parameters": { + "H": { + "value": 2.3799013065146455, + "unit": "", + "description": "" + }, + "D": { + "value": 3.8426676221226668, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "ball_radius": { + "value": 0.1, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/brachistochrone_race/150/params.json b/scipy/brachistochrone_race/150/params.json new file mode 100644 index 0000000000000000000000000000000000000000..0fda563a314eef5824b874b4b4e4e964b314287c --- /dev/null +++ b/scipy/brachistochrone_race/150/params.json @@ -0,0 +1,6 @@ +{ + "H": 2.3799013065146455, + "D": 3.8426676221226668, + "g": 9.8, + "ball_radius": 0.1 +} \ No newline at end of file diff --git a/scipy/brachistochrone_race/150/simulation_code.py b/scipy/brachistochrone_race/150/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a46acc60b66eef736f66fc811770263fe3e9a443 --- /dev/null +++ b/scipy/brachistochrone_race/150/simulation_code.py @@ -0,0 +1,89 @@ +""" +Brachistochrone Race — auto-generated simulation script. +""" +import numpy as np +from scipy.optimize import brentq +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +H = 2.3799013065146455 +D = 3.8426676221226668 +g = 9.8 +ball_radius = 0.1 +duration = 12 +fps = 30 + +def f_theta(th): + return (th - np.sin(th)) / (1.0 - np.cos(th)) - D / H + +theta_e = brentq(f_theta, 1e-6, 2.0 * np.pi - 1e-6, xtol=1e-12) +R = H / (1.0 - np.cos(theta_e)) +T_brach = np.sqrt(R / g) * theta_e +L_chord = np.sqrt(D * D + H * H) +T_straight = np.sqrt(2.0 * L_chord * L_chord / (g * H)) + +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +omega_cyc = np.sqrt(g / R) +theta_t = np.minimum(omega_cyc * t_eval, theta_e) +x_cyc = R * (theta_t - np.sin(theta_t)) +y_cyc = H - R * (1.0 - np.cos(theta_t)) + +a_straight = g * H / L_chord +s_t = np.minimum(0.5 * a_straight * t_eval * t_eval, L_chord) +x_str = (D / L_chord) * s_t +y_str = H - (H / L_chord) * s_t + +th_track = np.linspace(0.0, theta_e, 240) +x_track = R * (th_track - np.sin(th_track)) +y_track = H - R * (1.0 - np.cos(th_track)) + +fig_w, fig_h = 10, 6 +fig_aspect = fig_w / fig_h +xpad, ypad = 0.55, 0.45 +x_lo_d, x_hi_d, y_lo_d, y_hi_d = -xpad, D + xpad, -ypad, H + ypad +dx_v, dy_v = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx_v / dy_v < fig_aspect: + extra = (fig_aspect * dy_v - dx_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx_v / fig_aspect - dy_v) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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") +wall = patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0) +floor = patches.Rectangle((0.0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0) +ax.add_patch(wall); ax.add_patch(floor) +ax.plot([0.0, x_hi], [0.0, 0.0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, 0.0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0.0, D], [H, 0.0], "-", color="#a8dadc", lw=2.5, zorder=2) +ax.plot(x_track, y_track, "-", color="#f4d35e", lw=2.5, zorder=2) +ax.plot([0.0], [H], "o", color="#adb5bd", ms=6, zorder=3) +ax.plot([D], [0.0], "o", color="#adb5bd", ms=6, zorder=3) + +trail_str, = ax.plot([], [], "-", color="#e63946", alpha=0.45, lw=1.6, zorder=3) +trail_cyc, = ax.plot([], [], "-", color="#f4d35e", alpha=0.45, lw=1.6, zorder=3) +tx_s, ty_s, tx_c, ty_c = [], [], [], [] +ball_str = plt.Circle((x_str[0], y_str[0]), ball_radius, facecolor="#e63946", edgecolor="white", lw=1.4, zorder=5) +ball_cyc = plt.Circle((x_cyc[0], y_cyc[0]), ball_radius, facecolor="#f4d35e", edgecolor="white", lw=1.4, zorder=5) +ax.add_patch(ball_str); ax.add_patch(ball_cyc) + +def update(f): + ball_str.set_center((x_str[f], y_str[f])) + ball_cyc.set_center((x_cyc[f], y_cyc[f])) + tx_s.append(x_str[f]); ty_s.append(y_str[f]) + tx_c.append(x_cyc[f]); ty_c.append(y_cyc[f]) + if len(tx_s) > 60: tx_s.pop(0); ty_s.pop(0) + if len(tx_c) > 60: tx_c.pop(0); ty_c.pop(0) + trail_str.set_data(tx_s, ty_s) + trail_cyc.set_data(tx_c, ty_c) + return ball_str, ball_cyc, trail_str, trail_cyc + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval), interval=1000/fps, blit=True) +ani.save("brachistochrone_race.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved brachistochrone_race.mp4") diff --git a/scipy/brachistochrone_race/150/video.mp4 b/scipy/brachistochrone_race/150/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..54bed9d27caae9d87aaab7dcb71329f4af3daaa6 --- /dev/null +++ b/scipy/brachistochrone_race/150/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:56840942db0cf3bf5fdb284d364f8e4966edc87a9d263034497d71ca4d515ed6 +size 11023 diff --git a/scipy/cart_pendulum/032/cot.json b/scipy/cart_pendulum/032/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..2bbc37d25673a4a388316a0e9821ba12c52606c5 --- /dev/null +++ b/scipy/cart_pendulum/032/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=0.901 kg) on a frictionless track is connected to a fixed wall by a spring (k=6.326 N/m). A pendulum bob (m=1.025 kg, L=1.104 m) hangs from the cart. Cart starts displaced x_0=1.597 m from equilibrium; pendulum angle h_0=0.262 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 1.8123, + "pendulum_natural_frequency_approx": 2.9796 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 1.812 rad/s, pendulum \\omega \\approx 2.980 rad/s (decoupled approx).", + "Frequencies are well-separated; modes are largely decoupled." + ], + "parameters": { + "M": { + "value": 0.9008523250536109, + "unit": "kg", + "description": "" + }, + "m": { + "value": 1.0251149117165144, + "unit": "kg", + "description": "" + }, + "L": { + "value": 1.1038229294211037, + "unit": "m", + "description": "" + }, + "k": { + "value": 6.325534206284867, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.4567376870523604, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.11911235986127992, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 1.5966817763100487, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.26234425781343707, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/032/params.json b/scipy/cart_pendulum/032/params.json new file mode 100644 index 0000000000000000000000000000000000000000..6fc2553ed89f167a51c876eedb30c0bb9dfb42e0 --- /dev/null +++ b/scipy/cart_pendulum/032/params.json @@ -0,0 +1,13 @@ +{ + "M": 0.9008523250536109, + "m": 1.0251149117165144, + "L": 1.1038229294211037, + "k": 6.325534206284867, + "g": 9.8, + "d_c": 0.4567376870523604, + "b": 0.11911235986127992, + "x0": 1.5966817763100487, + "h0": 0.26234425781343707, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/032/simulation_code.py b/scipy/cart_pendulum/032/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..f6449bdd93c4c56079c560f875328577961f3c1a --- /dev/null +++ b/scipy/cart_pendulum/032/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 0.9008523250536109, 1.0251149117165144, 1.1038229294211037 +k = 6.325534206284867 +g = 9.8 +d_c = 0.4567376870523604 +b = 0.11911235986127992 +x0, h0 = 1.596682, 0.262344 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/032/video.mp4 b/scipy/cart_pendulum/032/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..35aa7f32809eb8aeac792e60d003f26f74c36be1 --- /dev/null +++ b/scipy/cart_pendulum/032/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b980c0f87ec1fe76b404275781e97d634480527f484fa4df1f5f78598b647c3e +size 12785 diff --git a/scipy/cart_pendulum/060/cot.json b/scipy/cart_pendulum/060/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..a565ba1558965cfa7351844a3b0b31f7c6b116dc --- /dev/null +++ b/scipy/cart_pendulum/060/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=0.856 kg) on a frictionless track is connected to a fixed wall by a spring (k=4.160 N/m). A pendulum bob (m=0.803 kg, L=1.258 m) hangs from the cart. Cart starts displaced x_0=0.666 m from equilibrium; pendulum angle h_0=-0.471 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 1.5836, + "pendulum_natural_frequency_approx": 2.7909 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 1.584 rad/s, pendulum \\omega \\approx 2.791 rad/s (decoupled approx).", + "Frequencies are well-separated; modes are largely decoupled." + ], + "parameters": { + "M": { + "value": 0.8564211961072874, + "unit": "kg", + "description": "" + }, + "m": { + "value": 0.8025764486489018, + "unit": "kg", + "description": "" + }, + "L": { + "value": 1.258181531037963, + "unit": "m", + "description": "" + }, + "k": { + "value": 4.160276116801026, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.1672166705202546, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.1719315121236363, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 0.6658386599901744, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": -0.47114382258735066, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/060/params.json b/scipy/cart_pendulum/060/params.json new file mode 100644 index 0000000000000000000000000000000000000000..74620547622b496535761af063e9279590c8a025 --- /dev/null +++ b/scipy/cart_pendulum/060/params.json @@ -0,0 +1,13 @@ +{ + "M": 0.8564211961072874, + "m": 0.8025764486489018, + "L": 1.258181531037963, + "k": 4.160276116801026, + "g": 9.8, + "d_c": 0.1672166705202546, + "b": 0.1719315121236363, + "x0": 0.6658386599901744, + "h0": -0.47114382258735066, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/060/simulation_code.py b/scipy/cart_pendulum/060/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..8e21d4c7e058cc6d623ffd6b999ea78f3c3315fd --- /dev/null +++ b/scipy/cart_pendulum/060/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 0.8564211961072874, 0.8025764486489018, 1.258181531037963 +k = 4.160276116801026 +g = 9.8 +d_c = 0.1672166705202546 +b = 0.1719315121236363 +x0, h0 = 0.665839, -0.471144 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/060/video.mp4 b/scipy/cart_pendulum/060/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..9ec3543adf3738e69bc3879f2033f3d0fc2c3aec --- /dev/null +++ b/scipy/cart_pendulum/060/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:adb9afafcab42475917a32f13eed7751ad4e989c782f94de96f2441e4eaef8d8 +size 13060 diff --git a/scipy/cart_pendulum/069/cot.json b/scipy/cart_pendulum/069/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..d38b845b93da489c7f75274fd9b9e8a33c7da79a --- /dev/null +++ b/scipy/cart_pendulum/069/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=0.764 kg) on a frictionless track is connected to a fixed wall by a spring (k=7.518 N/m). A pendulum bob (m=0.758 kg, L=0.785 m) hangs from the cart. Cart starts displaced x_0=1.674 m from equilibrium; pendulum angle h_0=0.051 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 2.2227, + "pendulum_natural_frequency_approx": 3.5327 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 2.223 rad/s, pendulum \\omega \\approx 3.533 rad/s (decoupled approx).", + "Frequencies are well-separated; modes are largely decoupled." + ], + "parameters": { + "M": { + "value": 0.7635972335115686, + "unit": "kg", + "description": "" + }, + "m": { + "value": 0.758261566555934, + "unit": "kg", + "description": "" + }, + "L": { + "value": 0.7852664385133175, + "unit": "m", + "description": "" + }, + "k": { + "value": 7.518454484035922, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.15865725025312882, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.1291402306267303, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 1.673723993953538, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.051440426534017036, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/069/params.json b/scipy/cart_pendulum/069/params.json new file mode 100644 index 0000000000000000000000000000000000000000..09ad1bd22fc61e3578096f0bc219dde1a905ad38 --- /dev/null +++ b/scipy/cart_pendulum/069/params.json @@ -0,0 +1,13 @@ +{ + "M": 0.7635972335115686, + "m": 0.758261566555934, + "L": 0.7852664385133175, + "k": 7.518454484035922, + "g": 9.8, + "d_c": 0.15865725025312882, + "b": 0.1291402306267303, + "x0": 1.673723993953538, + "h0": 0.051440426534017036, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/069/simulation_code.py b/scipy/cart_pendulum/069/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..44d923c5ac1e326417236727aa5d1f04ab1a7d0e --- /dev/null +++ b/scipy/cart_pendulum/069/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 0.7635972335115686, 0.758261566555934, 0.7852664385133175 +k = 7.518454484035922 +g = 9.8 +d_c = 0.15865725025312882 +b = 0.1291402306267303 +x0, h0 = 1.673724, 0.051440 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/069/video.mp4 b/scipy/cart_pendulum/069/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..4d63c978038f0f1127c4e1668dd08a0c1034c572 --- /dev/null +++ b/scipy/cart_pendulum/069/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:732d748c6756116813f0ceb08617194a36d0d0d893aafd9f6bb590d786ce33da +size 12888 diff --git a/scipy/cart_pendulum/072/cot.json b/scipy/cart_pendulum/072/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..2346ef061bf0e895e5b43aa7b697781d578ca841 --- /dev/null +++ b/scipy/cart_pendulum/072/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=1.426 kg) on a frictionless track is connected to a fixed wall by a spring (k=6.850 N/m). A pendulum bob (m=0.917 kg, L=0.625 m) hangs from the cart. Cart starts displaced x_0=1.604 m from equilibrium; pendulum angle h_0=0.271 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 1.7097, + "pendulum_natural_frequency_approx": 3.9605 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 1.710 rad/s, pendulum \\omega \\approx 3.961 rad/s (decoupled approx).", + "Frequencies are well-separated; modes are largely decoupled." + ], + "parameters": { + "M": { + "value": 1.4259088113692913, + "unit": "kg", + "description": "" + }, + "m": { + "value": 0.9174906778740435, + "unit": "kg", + "description": "" + }, + "L": { + "value": 0.6247638552930485, + "unit": "m", + "description": "" + }, + "k": { + "value": 6.849860188360335, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.14031298152925606, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.25340547626008453, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 1.6044307608367652, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.2712311488824085, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/072/params.json b/scipy/cart_pendulum/072/params.json new file mode 100644 index 0000000000000000000000000000000000000000..a64424080669b1be6ade4589dff9c98dfbb4b721 --- /dev/null +++ b/scipy/cart_pendulum/072/params.json @@ -0,0 +1,13 @@ +{ + "M": 1.4259088113692913, + "m": 0.9174906778740435, + "L": 0.6247638552930485, + "k": 6.849860188360335, + "g": 9.8, + "d_c": 0.14031298152925606, + "b": 0.25340547626008453, + "x0": 1.6044307608367652, + "h0": 0.2712311488824085, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/072/simulation_code.py b/scipy/cart_pendulum/072/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..cad3641cbb39719d3bb04ed19961c3ca0394a9e7 --- /dev/null +++ b/scipy/cart_pendulum/072/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 1.4259088113692913, 0.9174906778740435, 0.6247638552930485 +k = 6.849860188360335 +g = 9.8 +d_c = 0.14031298152925606 +b = 0.25340547626008453 +x0, h0 = 1.604431, 0.271231 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/072/video.mp4 b/scipy/cart_pendulum/072/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8a552981255e2e41bb803c8f946c52cca50df5a3 --- /dev/null +++ b/scipy/cart_pendulum/072/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d3b5d00020a91d4e0836b2b174ba83d8e0013d7e3d68863bb2f15bf2ed0a06c0 +size 10240 diff --git a/scipy/cart_pendulum/219/cot.json b/scipy/cart_pendulum/219/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..f93c59552dad820b237d12f9d032cf54a5ad914c --- /dev/null +++ b/scipy/cart_pendulum/219/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=0.890 kg) on a frictionless track is connected to a fixed wall by a spring (k=2.778 N/m). A pendulum bob (m=1.739 kg, L=0.879 m) hangs from the cart. Cart starts displaced x_0=1.998 m from equilibrium; pendulum angle h_0=0.247 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 1.0279, + "pendulum_natural_frequency_approx": 3.3397 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 1.028 rad/s, pendulum \\omega \\approx 3.340 rad/s (decoupled approx).", + "Frequencies are well-separated; modes are largely decoupled." + ], + "parameters": { + "M": { + "value": 0.8903993198516298, + "unit": "kg", + "description": "" + }, + "m": { + "value": 1.7387369135696868, + "unit": "kg", + "description": "" + }, + "L": { + "value": 0.8786293403731689, + "unit": "m", + "description": "" + }, + "k": { + "value": 2.777665069229376, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.7645492233717938, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.04675118203733836, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 1.9978502038772274, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.24694742428874739, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/219/params.json b/scipy/cart_pendulum/219/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b343e20d22e49ae60bec49a6c6252ac2ae3483a0 --- /dev/null +++ b/scipy/cart_pendulum/219/params.json @@ -0,0 +1,13 @@ +{ + "M": 0.8903993198516298, + "m": 1.7387369135696868, + "L": 0.8786293403731689, + "k": 2.777665069229376, + "g": 9.8, + "d_c": 0.7645492233717938, + "b": 0.04675118203733836, + "x0": 1.9978502038772274, + "h0": 0.24694742428874739, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/219/simulation_code.py b/scipy/cart_pendulum/219/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0fabb0b82d13acf462e024c039bbc4f9d47fdb94 --- /dev/null +++ b/scipy/cart_pendulum/219/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 0.8903993198516298, 1.7387369135696868, 0.8786293403731689 +k = 2.777665069229376 +g = 9.8 +d_c = 0.7645492233717938 +b = 0.04675118203733836 +x0, h0 = 1.997850, 0.246947 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/219/video.mp4 b/scipy/cart_pendulum/219/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..c8cbd26b0ad5c413b7a5a595b80b4a9c9ca5d59c --- /dev/null +++ b/scipy/cart_pendulum/219/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:561b588ddbc3b96e34034c5be2797bfb2f3762158456c9aad60d24e1bdb6be2c +size 9389 diff --git a/scipy/cart_pendulum/234/cot.json b/scipy/cart_pendulum/234/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ebfe593f87348774deff68ddb9aa5e8fed70beb6 --- /dev/null +++ b/scipy/cart_pendulum/234/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=0.846 kg) on a frictionless track is connected to a fixed wall by a spring (k=9.808 N/m). A pendulum bob (m=1.507 kg, L=1.329 m) hangs from the cart. Cart starts displaced x_0=0.435 m from equilibrium; pendulum angle h_0=-0.152 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 2.042, + "pendulum_natural_frequency_approx": 2.7157 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 2.042 rad/s, pendulum \\omega \\approx 2.716 rad/s (decoupled approx).", + "Energy transfers between cart and pendulum, creating beats." + ], + "parameters": { + "M": { + "value": 0.8455757067770162, + "unit": "kg", + "description": "" + }, + "m": { + "value": 1.5065565076955318, + "unit": "kg", + "description": "" + }, + "L": { + "value": 1.3288397913186798, + "unit": "m", + "description": "" + }, + "k": { + "value": 9.808174811214823, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.6702516372562791, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.011011466499625889, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 0.4352483752158205, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": -0.15153904616716407, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/234/params.json b/scipy/cart_pendulum/234/params.json new file mode 100644 index 0000000000000000000000000000000000000000..4ab94d4453a0ab3b4924fac4ecdd13f28713fd79 --- /dev/null +++ b/scipy/cart_pendulum/234/params.json @@ -0,0 +1,13 @@ +{ + "M": 0.8455757067770162, + "m": 1.5065565076955318, + "L": 1.3288397913186798, + "k": 9.808174811214823, + "g": 9.8, + "d_c": 0.6702516372562791, + "b": 0.011011466499625889, + "x0": 0.4352483752158205, + "h0": -0.15153904616716407, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/234/simulation_code.py b/scipy/cart_pendulum/234/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..cf7610c173ae958d3904080a0e59adada31ce17e --- /dev/null +++ b/scipy/cart_pendulum/234/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 0.8455757067770162, 1.5065565076955318, 1.3288397913186798 +k = 9.808174811214823 +g = 9.8 +d_c = 0.6702516372562791 +b = 0.011011466499625889 +x0, h0 = 0.435248, -0.151539 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/234/video.mp4 b/scipy/cart_pendulum/234/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..9c64744394b112eac337aaee665b9b1487f202ff --- /dev/null +++ b/scipy/cart_pendulum/234/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:441961ae31621b756da0f166d7ca61897e7e125a690f12a6eb26718a9ab89cfc +size 10589 diff --git a/scipy/cart_pendulum/243/cot.json b/scipy/cart_pendulum/243/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..39bd00f372242a6daee2d3c9a42a34464fbdb3ca --- /dev/null +++ b/scipy/cart_pendulum/243/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=1.987 kg) on a frictionless track is connected to a fixed wall by a spring (k=4.279 N/m). A pendulum bob (m=1.708 kg, L=1.244 m) hangs from the cart. Cart starts displaced x_0=0.750 m from equilibrium; pendulum angle h_0=0.017 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 1.0762, + "pendulum_natural_frequency_approx": 2.807 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 1.076 rad/s, pendulum \\omega \\approx 2.807 rad/s (decoupled approx).", + "Frequencies are well-separated; modes are largely decoupled." + ], + "parameters": { + "M": { + "value": 1.9868385672092697, + "unit": "kg", + "description": "" + }, + "m": { + "value": 1.7075872418119151, + "unit": "kg", + "description": "" + }, + "L": { + "value": 1.2437646998375946, + "unit": "m", + "description": "" + }, + "k": { + "value": 4.278854220038412, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.49033269150292713, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.015771529288951303, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 0.7495250196809501, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.016658523567688377, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/243/params.json b/scipy/cart_pendulum/243/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e536dee4f9f5b8d7aaca91709cc460a82da215e3 --- /dev/null +++ b/scipy/cart_pendulum/243/params.json @@ -0,0 +1,13 @@ +{ + "M": 1.9868385672092697, + "m": 1.7075872418119151, + "L": 1.2437646998375946, + "k": 4.278854220038412, + "g": 9.8, + "d_c": 0.49033269150292713, + "b": 0.015771529288951303, + "x0": 0.7495250196809501, + "h0": 0.016658523567688377, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/243/simulation_code.py b/scipy/cart_pendulum/243/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..686090e7b60842818413a081c9269e3a11f6ab9a --- /dev/null +++ b/scipy/cart_pendulum/243/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 1.9868385672092697, 1.7075872418119151, 1.2437646998375946 +k = 4.278854220038412 +g = 9.8 +d_c = 0.49033269150292713 +b = 0.015771529288951303 +x0, h0 = 0.749525, 0.016659 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/243/video.mp4 b/scipy/cart_pendulum/243/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..c27bfb3c9a0bf1dcb0ec82ca12716fffa16afa10 --- /dev/null +++ b/scipy/cart_pendulum/243/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c649ea90a3895cca7b297c88c724f66247f4a0c21faeea4742cfdf89667705b7 +size 9881 diff --git a/scipy/cart_pendulum/285/cot.json b/scipy/cart_pendulum/285/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..5b7a3ac457d6e3a24c4fc1a34d01dd50234018d2 --- /dev/null +++ b/scipy/cart_pendulum/285/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=0.785 kg) on a frictionless track is connected to a fixed wall by a spring (k=4.219 N/m). A pendulum bob (m=0.445 kg, L=1.262 m) hangs from the cart. Cart starts displaced x_0=1.867 m from equilibrium; pendulum angle h_0=0.293 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 1.852, + "pendulum_natural_frequency_approx": 2.7872 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 1.852 rad/s, pendulum \\omega \\approx 2.787 rad/s (decoupled approx).", + "Energy transfers between cart and pendulum, creating beats." + ], + "parameters": { + "M": { + "value": 0.785461086877655, + "unit": "kg", + "description": "" + }, + "m": { + "value": 0.44466472237518534, + "unit": "kg", + "description": "" + }, + "L": { + "value": 1.2615033687539232, + "unit": "m", + "description": "" + }, + "k": { + "value": 4.2192471707242545, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.7048380237199895, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.2670941927450352, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 1.8670620488863652, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.29314183616503886, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/285/params.json b/scipy/cart_pendulum/285/params.json new file mode 100644 index 0000000000000000000000000000000000000000..a32cdae77090874105e432c99264f34ef8badf8c --- /dev/null +++ b/scipy/cart_pendulum/285/params.json @@ -0,0 +1,13 @@ +{ + "M": 0.785461086877655, + "m": 0.44466472237518534, + "L": 1.2615033687539232, + "k": 4.2192471707242545, + "g": 9.8, + "d_c": 0.7048380237199895, + "b": 0.2670941927450352, + "x0": 1.8670620488863652, + "h0": 0.29314183616503886, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/285/simulation_code.py b/scipy/cart_pendulum/285/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..8dff0cd35aeae7b83e042fd8c1fa12e163860d02 --- /dev/null +++ b/scipy/cart_pendulum/285/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 0.785461086877655, 0.44466472237518534, 1.2615033687539232 +k = 4.2192471707242545 +g = 9.8 +d_c = 0.7048380237199895 +b = 0.2670941927450352 +x0, h0 = 1.867062, 0.293142 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/285/video.mp4 b/scipy/cart_pendulum/285/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..68324e83c9483e5204d944141488a0b724b350bf --- /dev/null +++ b/scipy/cart_pendulum/285/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:52e63a877ba1683a5749375068f34eeb55572f10bb9a9b293331b87ec950da34 +size 12777 diff --git a/scipy/cart_pendulum/338/cot.json b/scipy/cart_pendulum/338/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..dc5bb9f2de658bdef60511e28988632b8e9e32f3 --- /dev/null +++ b/scipy/cart_pendulum/338/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=0.725 kg) on a frictionless track is connected to a fixed wall by a spring (k=5.963 N/m). A pendulum bob (m=0.516 kg, L=0.676 m) hangs from the cart. Cart starts displaced x_0=1.441 m from equilibrium; pendulum angle h_0=0.188 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 2.1913, + "pendulum_natural_frequency_approx": 3.8073 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 2.191 rad/s, pendulum \\omega \\approx 3.807 rad/s (decoupled approx).", + "Frequencies are well-separated; modes are largely decoupled." + ], + "parameters": { + "M": { + "value": 0.725491284983518, + "unit": "kg", + "description": "" + }, + "m": { + "value": 0.5162496972685757, + "unit": "kg", + "description": "" + }, + "L": { + "value": 0.6760782389326043, + "unit": "m", + "description": "" + }, + "k": { + "value": 5.962833599588906, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.6150929749591695, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.2288313455025367, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 1.441108389688925, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.18790263546867536, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/338/params.json b/scipy/cart_pendulum/338/params.json new file mode 100644 index 0000000000000000000000000000000000000000..05262d4c9555c7555973860a9cfee9a2e9359a49 --- /dev/null +++ b/scipy/cart_pendulum/338/params.json @@ -0,0 +1,13 @@ +{ + "M": 0.725491284983518, + "m": 0.5162496972685757, + "L": 0.6760782389326043, + "k": 5.962833599588906, + "g": 9.8, + "d_c": 0.6150929749591695, + "b": 0.2288313455025367, + "x0": 1.441108389688925, + "h0": 0.18790263546867536, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/338/simulation_code.py b/scipy/cart_pendulum/338/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..3df0e97eceea51b69d548b159f97a3ffa4999a0e --- /dev/null +++ b/scipy/cart_pendulum/338/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 0.725491284983518, 0.5162496972685757, 0.6760782389326043 +k = 5.962833599588906 +g = 9.8 +d_c = 0.6150929749591695 +b = 0.2288313455025367 +x0, h0 = 1.441108, 0.187903 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/338/video.mp4 b/scipy/cart_pendulum/338/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..9a807b46633735a6abf63871301db155ef99328c --- /dev/null +++ b/scipy/cart_pendulum/338/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c0bad6751f8362f501d41624167ea694c4227f98c2900bb52279e2eb26fd9c0e +size 10471 diff --git a/scipy/cart_pendulum/497/cot.json b/scipy/cart_pendulum/497/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..65dbe592cd730728a126b8144c69d208476f649a --- /dev/null +++ b/scipy/cart_pendulum/497/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=0.960 kg) on a frictionless track is connected to a fixed wall by a spring (k=3.752 N/m). A pendulum bob (m=0.381 kg, L=0.750 m) hangs from the cart. Cart starts displaced x_0=1.384 m from equilibrium; pendulum angle h_0=0.365 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 1.6731, + "pendulum_natural_frequency_approx": 3.6156 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 1.673 rad/s, pendulum \\omega \\approx 3.616 rad/s (decoupled approx).", + "Frequencies are well-separated; modes are largely decoupled." + ], + "parameters": { + "M": { + "value": 0.9599255944236771, + "unit": "kg", + "description": "" + }, + "m": { + "value": 0.3805599135915853, + "unit": "kg", + "description": "" + }, + "L": { + "value": 0.7496611141218763, + "unit": "m", + "description": "" + }, + "k": { + "value": 3.7523051275961166, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.29847289134468574, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.06029407274687387, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 1.3838297834195676, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.36450780182096043, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/497/params.json b/scipy/cart_pendulum/497/params.json new file mode 100644 index 0000000000000000000000000000000000000000..a4f02a82e44db6f361511db74aeb7f423f150fb1 --- /dev/null +++ b/scipy/cart_pendulum/497/params.json @@ -0,0 +1,13 @@ +{ + "M": 0.9599255944236771, + "m": 0.3805599135915853, + "L": 0.7496611141218763, + "k": 3.7523051275961166, + "g": 9.8, + "d_c": 0.29847289134468574, + "b": 0.06029407274687387, + "x0": 1.3838297834195676, + "h0": 0.36450780182096043, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/497/simulation_code.py b/scipy/cart_pendulum/497/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..07294b50de1654276df2cbc53a1f6604a8218719 --- /dev/null +++ b/scipy/cart_pendulum/497/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 0.9599255944236771, 0.3805599135915853, 0.7496611141218763 +k = 3.7523051275961166 +g = 9.8 +d_c = 0.29847289134468574 +b = 0.06029407274687387 +x0, h0 = 1.383830, 0.364508 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/497/video.mp4 b/scipy/cart_pendulum/497/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d3be3af0cfb295952e1d613c4c4c3442c52202a5 --- /dev/null +++ b/scipy/cart_pendulum/497/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7f8f4aeda267724bacca2d07c4f13c1878ff364d8d5d1d525d83b087bf0606ce +size 10518 diff --git a/scipy/cart_pendulum/526/cot.json b/scipy/cart_pendulum/526/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..86e8dc6d92fffe19647d1224e33d2857db421c24 --- /dev/null +++ b/scipy/cart_pendulum/526/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=2.170 kg) on a frictionless track is connected to a fixed wall by a spring (k=8.307 N/m). A pendulum bob (m=1.853 kg, L=1.782 m) hangs from the cart. Cart starts displaced x_0=0.590 m from equilibrium; pendulum angle h_0=0.065 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 1.437, + "pendulum_natural_frequency_approx": 2.345 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 1.437 rad/s, pendulum \\omega \\approx 2.345 rad/s (decoupled approx).", + "Energy transfers between cart and pendulum, creating beats." + ], + "parameters": { + "M": { + "value": 2.17039390982369, + "unit": "kg", + "description": "" + }, + "m": { + "value": 1.852654525016116, + "unit": "kg", + "description": "" + }, + "L": { + "value": 1.7821877614979185, + "unit": "m", + "description": "" + }, + "k": { + "value": 8.30743708533062, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.4919679445948723, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.282355007674465, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 0.5897406041160151, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.06504285727427606, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/526/params.json b/scipy/cart_pendulum/526/params.json new file mode 100644 index 0000000000000000000000000000000000000000..fe13a803fb8272820fe622dd09d4aa9403358701 --- /dev/null +++ b/scipy/cart_pendulum/526/params.json @@ -0,0 +1,13 @@ +{ + "M": 2.17039390982369, + "m": 1.852654525016116, + "L": 1.7821877614979185, + "k": 8.30743708533062, + "g": 9.8, + "d_c": 0.4919679445948723, + "b": 0.282355007674465, + "x0": 0.5897406041160151, + "h0": 0.06504285727427606, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/526/simulation_code.py b/scipy/cart_pendulum/526/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..7fd5292d6fe6748e19b67bba92f2e325488c280c --- /dev/null +++ b/scipy/cart_pendulum/526/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 2.17039390982369, 1.852654525016116, 1.7821877614979185 +k = 8.30743708533062 +g = 9.8 +d_c = 0.4919679445948723 +b = 0.282355007674465 +x0, h0 = 0.589741, 0.065043 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/526/video.mp4 b/scipy/cart_pendulum/526/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..550e085e3ed60f1e8f5ceba9885ba3991702541c --- /dev/null +++ b/scipy/cart_pendulum/526/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0c3c9d94a63cae376d58af0753926dfb473f8ff0782ed050cf8afad0f598d12a +size 10845 diff --git a/scipy/cart_pendulum/552/cot.json b/scipy/cart_pendulum/552/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..706eec16086b17669f24cd0cb5590af8cbf1779b --- /dev/null +++ b/scipy/cart_pendulum/552/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=2.024 kg) on a frictionless track is connected to a fixed wall by a spring (k=7.014 N/m). A pendulum bob (m=1.851 kg, L=1.671 m) hangs from the cart. Cart starts displaced x_0=1.280 m from equilibrium; pendulum angle h_0=0.120 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 1.3454, + "pendulum_natural_frequency_approx": 2.4218 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 1.345 rad/s, pendulum \\omega \\approx 2.422 rad/s (decoupled approx).", + "Frequencies are well-separated; modes are largely decoupled." + ], + "parameters": { + "M": { + "value": 2.023913329873271, + "unit": "kg", + "description": "" + }, + "m": { + "value": 1.8509012420234834, + "unit": "kg", + "description": "" + }, + "L": { + "value": 1.6709373571126362, + "unit": "m", + "description": "" + }, + "k": { + "value": 7.013613769876519, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.3998965457023088, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.12059011815541736, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 1.2804631224794694, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.1203616311045439, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/552/params.json b/scipy/cart_pendulum/552/params.json new file mode 100644 index 0000000000000000000000000000000000000000..a781a52d1822ebe32a33983bb242221462223de7 --- /dev/null +++ b/scipy/cart_pendulum/552/params.json @@ -0,0 +1,13 @@ +{ + "M": 2.023913329873271, + "m": 1.8509012420234834, + "L": 1.6709373571126362, + "k": 7.013613769876519, + "g": 9.8, + "d_c": 0.3998965457023088, + "b": 0.12059011815541736, + "x0": 1.2804631224794694, + "h0": 0.1203616311045439, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/552/simulation_code.py b/scipy/cart_pendulum/552/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..90e8338d8cb948156ded1432476611e1d5f92a4e --- /dev/null +++ b/scipy/cart_pendulum/552/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 2.023913329873271, 1.8509012420234834, 1.6709373571126362 +k = 7.013613769876519 +g = 9.8 +d_c = 0.3998965457023088 +b = 0.12059011815541736 +x0, h0 = 1.280463, 0.120362 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/552/video.mp4 b/scipy/cart_pendulum/552/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..32d28fccbad2b2432f88a0abdb965d6367c28d88 --- /dev/null +++ b/scipy/cart_pendulum/552/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:427a16565fa908c6574e3efd075a7fc4f1eefb0aecc366861080bd5e82a3b2ee +size 12534 diff --git a/scipy/cart_pendulum/646/cot.json b/scipy/cart_pendulum/646/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..77e3447a3d31a925ded273fe6a625f17b18fb80d --- /dev/null +++ b/scipy/cart_pendulum/646/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=1.099 kg) on a frictionless track is connected to a fixed wall by a spring (k=9.997 N/m). A pendulum bob (m=1.327 kg, L=1.770 m) hangs from the cart. Cart starts displaced x_0=1.056 m from equilibrium; pendulum angle h_0=-0.058 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 2.0303, + "pendulum_natural_frequency_approx": 2.3528 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 2.030 rad/s, pendulum \\omega \\approx 2.353 rad/s (decoupled approx).", + "Energy transfers between cart and pendulum, creating beats." + ], + "parameters": { + "M": { + "value": 1.0987848414589438, + "unit": "kg", + "description": "" + }, + "m": { + "value": 1.3265690483248098, + "unit": "kg", + "description": "" + }, + "L": { + "value": 1.7703949987337624, + "unit": "m", + "description": "" + }, + "k": { + "value": 9.99745871768396, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.6114085273107372, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.002693425478060307, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 1.0558292301612837, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": -0.05751751568923469, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/646/params.json b/scipy/cart_pendulum/646/params.json new file mode 100644 index 0000000000000000000000000000000000000000..44990e0b9e5c707772b4650a60fa9f4118683725 --- /dev/null +++ b/scipy/cart_pendulum/646/params.json @@ -0,0 +1,13 @@ +{ + "M": 1.0987848414589438, + "m": 1.3265690483248098, + "L": 1.7703949987337624, + "k": 9.99745871768396, + "g": 9.8, + "d_c": 0.6114085273107372, + "b": 0.002693425478060307, + "x0": 1.0558292301612837, + "h0": -0.05751751568923469, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/646/simulation_code.py b/scipy/cart_pendulum/646/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..7af0069364922ccb4836169feee2b9415f64041c --- /dev/null +++ b/scipy/cart_pendulum/646/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 1.0987848414589438, 1.3265690483248098, 1.7703949987337624 +k = 9.99745871768396 +g = 9.8 +d_c = 0.6114085273107372 +b = 0.002693425478060307 +x0, h0 = 1.055829, -0.057518 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/646/video.mp4 b/scipy/cart_pendulum/646/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..dbbd71229a0c7be5565299e718a9a171701533d8 --- /dev/null +++ b/scipy/cart_pendulum/646/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5db26e1cb1a66ba33dd3aaa160546ab04259ed1feecb025dc26ba31b20b3ca22 +size 14022 diff --git a/scipy/cart_pendulum/685/cot.json b/scipy/cart_pendulum/685/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..b6220033a76ef90522bcbbcd096d4a874e064f95 --- /dev/null +++ b/scipy/cart_pendulum/685/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=1.992 kg) on a frictionless track is connected to a fixed wall by a spring (k=3.271 N/m). A pendulum bob (m=1.773 kg, L=1.650 m) hangs from the cart. Cart starts displaced x_0=0.501 m from equilibrium; pendulum angle h_0=0.349 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 0.932, + "pendulum_natural_frequency_approx": 2.4369 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 0.932 rad/s, pendulum \\omega \\approx 2.437 rad/s (decoupled approx).", + "Frequencies are well-separated; modes are largely decoupled." + ], + "parameters": { + "M": { + "value": 1.9921866610666426, + "unit": "kg", + "description": "" + }, + "m": { + "value": 1.7732282296185162, + "unit": "kg", + "description": "" + }, + "L": { + "value": 1.6502946655594322, + "unit": "m", + "description": "" + }, + "k": { + "value": 3.2708486661100578, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.7118372662021528, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.266475584770404, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 0.5014133948108715, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.34871842286879684, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/685/params.json b/scipy/cart_pendulum/685/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b16ac521cc1abd738b266cdbd76097f782078e7f --- /dev/null +++ b/scipy/cart_pendulum/685/params.json @@ -0,0 +1,13 @@ +{ + "M": 1.9921866610666426, + "m": 1.7732282296185162, + "L": 1.6502946655594322, + "k": 3.2708486661100578, + "g": 9.8, + "d_c": 0.7118372662021528, + "b": 0.266475584770404, + "x0": 0.5014133948108715, + "h0": 0.34871842286879684, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/685/simulation_code.py b/scipy/cart_pendulum/685/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..aec1af7f40e03e1892c549a9a160100ffc062f6c --- /dev/null +++ b/scipy/cart_pendulum/685/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 1.9921866610666426, 1.7732282296185162, 1.6502946655594322 +k = 3.2708486661100578 +g = 9.8 +d_c = 0.7118372662021528 +b = 0.266475584770404 +x0, h0 = 0.501413, 0.348718 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/685/video.mp4 b/scipy/cart_pendulum/685/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..2880bba64891113ad2c6ae272e6a25673ab0794a --- /dev/null +++ b/scipy/cart_pendulum/685/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:edd9ed8b65b7b31fa346ae4b51a9c3543bfcb802f5c9b1e36b55b4785d01342d +size 12623 diff --git a/scipy/cart_pendulum/724/cot.json b/scipy/cart_pendulum/724/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..83f370185d09894f417876c43deab17c7736b5ec --- /dev/null +++ b/scipy/cart_pendulum/724/cot.json @@ -0,0 +1,116 @@ +{ + "simulation": "cart_pendulum", + "physical_observation": "A cart (M=2.044 kg) on a frictionless track is connected to a fixed wall by a spring (k=9.817 N/m). A pendulum bob (m=1.460 kg, L=1.179 m) hangs from the cart. Cart starts displaced x_0=1.848 m from equilibrium; pendulum angle h_0=0.374 rad.", + "physical_law": { + "name": "Lagrangian Mechanics for Constrained Multi-Body System", + "statement": "Euler-Lagrange equations for generalized coordinates q=(x,h).", + "formula": "\\ddot{x} = (m w^2 L \\sin(h) + mg \\sin(h) \\cos(h) - kx - d_c \\dot{x} + b w \\cos(h)/L) / (M + m \\sin^2h)\n\\ddot{h} = (-m w^2 L \\sin(h) \\cos(h) + kx \\cos(h) - (M+m)g \\sin(h) + d_c \\dot{x} \\cos(h) - (m+M)b w/(mL)) / (L(M + m \\sin^2h))" + }, + "ode_system": { + "state_variables": [ + "x (cart displacement, m)", + "h (pendulum angle, rad)", + "v=\\dot{x}", + "w=\\dot{h}" + ], + "equations_latex": [ + "\\dot{x} = v", + "\\dot{h} = w", + "\\dot{v} = (m w^2 L \\sin(h) + m g \\sin(h) \\cos(h) - k x - d_c v + b w \\cos(h) / L) / (M + m \\sin^2h)", + "\\dot{w} = (-m w^2 L \\sin(h) \\cos(h) + k x \\cos(h) - (M+m) g \\sin(h) + d_c v \\cos(h) - (m+M) b w/(m L)) / (L(M + m \\sin^2h))" + ], + "equations_python": [ + "x_dot = v", + "h_dot = w", + "v_dot = (m * w**2 * L * np.sin (h) + m * g np.sin (h) * np.cos (h) - k * x - d_c * v + b * w np.cos (h) / L) / (M + m * np.sin **2 * h)", + "w_dot = (-m * w**2 * L * np.sin (h) * np.cos (h) + k * x np.cos (h) - (M+m) * g * np.sin (h) + d_c * v np.cos (h) - (m+M) * b * w/(m * L)) / (L(M + m * np.sin **2 * h))" + ] + }, + "derivation_steps": [ + "Generalized coordinates: x (cart position from spring equilibrium), h (pendulum angle from vertical).", + "Cart position in 2D: (x, 0).", + "Bob position: (x + L \\sin(h), -L \\cos(h)).", + "Bob velocity squared: (\\dot{x} + L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2 = \\dot{x}^2 + L^2\\dot{h}^2 + 2\\dot{x}L\\dot{h} \\cos(h).", + "Kinetic energy: T = \\tfrac{1}{2} M \\dot{x}^2 + \\tfrac{1}{2} m[(\\dot{x}+L\\dot{h} \\cos(h))^2 + (L\\dot{h} \\sin(h))^2].", + "Potential energy: V = \\tfrac{1}{2} kx^2 - mgL \\cos(h).", + "Lagrangian L = T - V; apply Euler-Lagrange for x and h.", + "Solve the resulting 2 \\times 2 system (with denominator M + m \\sin^2h) for \\ddot{x} and \\ddot{h}.", + "Add dissipation terms (cart damping d_c, pendulum damping b) as generalized forces.", + "Result: a coupled 2-DOF nonlinear ODE system in (x, h, \\dot{x}, \\dot{h}) --- the standard cart-pendulum equations of motion." + ], + "physical_constants_derived": { + "cart_natural_frequency_approx": 1.6737, + "pendulum_natural_frequency_approx": 2.8835 + }, + "numerical_method": { + "solver": "RK45", + "tolerance": "rtol=1e-9, atol=1e-11" + }, + "code_validation": { + "ode_source": "Euler-Lagrange equations for the cart + pendulum 2-DOF system (textbook formulation), with dissipation forces appended.", + "energy_check": "E = \\tfrac{1}{2} Mv^2 + \\tfrac{1}{2} m|v_bob|^2 + \\tfrac{1}{2} kx^2 - mgL \\cos(h) decreases monotonically when d_c>0 or b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Cart oscillates around x=0; pendulum oscillates around h=0.", + "Two coupled modes: cart \\omega \\approx 1.674 rad/s, pendulum \\omega \\approx 2.884 rad/s (decoupled approx).", + "Frequencies are well-separated; modes are largely decoupled." + ], + "parameters": { + "M": { + "value": 2.0443961746890205, + "unit": "kg", + "description": "" + }, + "m": { + "value": 1.4598794286056778, + "unit": "kg", + "description": "" + }, + "L": { + "value": 1.1786398810633723, + "unit": "m", + "description": "" + }, + "k": { + "value": 9.81678745844257, + "unit": "N/m", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "m/s^2", + "description": "" + }, + "d_c": { + "value": 0.4607949270167603, + "unit": "kg/s", + "description": "" + }, + "b": { + "value": 0.2340721513342684, + "unit": "kg \\cdot m^2/s", + "description": "" + }, + "x0": { + "value": 1.8481783365498339, + "unit": "m", + "description": "Cart displacement from spring equilibrium" + }, + "h0": { + "value": 0.37373813374223497, + "unit": "rad", + "description": "Pendulum angle from vertical" + }, + "v0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_pendulum/724/params.json b/scipy/cart_pendulum/724/params.json new file mode 100644 index 0000000000000000000000000000000000000000..7632c9361746ffede646813a3a05b751fb8ee745 --- /dev/null +++ b/scipy/cart_pendulum/724/params.json @@ -0,0 +1,13 @@ +{ + "M": 2.0443961746890205, + "m": 1.4598794286056778, + "L": 1.1786398810633723, + "k": 9.81678745844257, + "g": 9.8, + "d_c": 0.4607949270167603, + "b": 0.2340721513342684, + "x0": 1.8481783365498339, + "h0": 0.37373813374223497, + "v0": 0.0, + "w0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_pendulum/724/simulation_code.py b/scipy/cart_pendulum/724/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a1cb65542e6f410d38656bf59d2b4a2fa5fb9e5f --- /dev/null +++ b/scipy/cart_pendulum/724/simulation_code.py @@ -0,0 +1,74 @@ +""" +Cart-Pendulum — auto-generated simulation script. +Physics (Lagrangian mechanics): + ẍ = (mẇ²L sin(h) + mg sin(h)cos(h) - kx - d_c ẋ + b ẇ cos(h)/L) + / (M + m sin²(h)) + ḧ = (-mẇ²L sin(h)cos(h) + kx cos(h) - (M+m)g sin(h) + d_c ẋ cos(h) - (m+M)b ẇ/(mL)) + / (L(M + m sin²(h))) +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +M, m, L = 2.0443961746890205, 1.4598794286056778, 1.1786398810633723 +k = 9.81678745844257 +g = 9.8 +d_c = 0.4607949270167603 +b = 0.2340721513342684 +x0, h0 = 1.848178, 0.373738 +v0, w0 = 0.000000, 0.000000 +duration = 12 + +def ode(t, y): + x, h, v, w = y + sh, csh = np.sin(h), np.cos(h) + denom = M + m*sh*sh + dv = (m*w*w*L*sh + m*g*sh*csh - k*x - d_c*v + b*w*csh/L) / denom + dw = (-m*w*w*L*sh*csh + k*x*csh - (M+m)*g*sh + d_c*v*csh - (m+M)*b*w/(m*L)) / (L*denom) + return [v, w, dv, dw] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration*fps)+1) +sol = solve_ivp(ode,(0,duration),[x0,h0,v0,w0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) +x_arr, h_arr = sol.y[0], sol.y[1] + +cart_w, cart_h = 0.6, 0.3 +track_y, wall_x = 0.0, -3.0 +cy = cart_h/2 +fig, ax = plt.subplots(figsize=(8,5),facecolor="#1a1a2e") +ax.set_facecolor("#16213e"); ax.set_xlim(wall_x-0.3, max(3.5,np.max(np.abs(x_arr))+1.2)) +ax.set_ylim(-L*1.3, L*0.4+cart_h); ax.set_aspect("equal"); ax.axis("off") +ax.axhline(track_y,color="#4a4e69",lw=1.5,zorder=1) +ax.add_patch(patches.Rectangle((wall_x-0.15,track_y-0.6),0.15,1.2, + facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) + +def spring_path_fn(x1,x2,n=6,r=0.12): + t=np.linspace(0,1,n*20+2); m2=(1-2*0.08) + env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/m2)) + return x1+t*(x2-x1)+r*env*0, np.full_like(t,cy) + +sp, = ax.plot([],[],"-",color="#a8dadc",lw=2,zorder=2) +cpatch = patches.FancyBboxPatch((-cart_w/2,cy-cart_h/2),cart_w,cart_h, + boxstyle="round,pad=0.02",lw=1.5,edgecolor="white",facecolor="#457b9d") +ax.add_patch(cpatch) +w1=plt.Circle((-cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +w2=plt.Circle(( cart_w/3,track_y),0.07,color="#a8dadc",zorder=3) +ax.add_patch(w1); ax.add_patch(w2) +rod, = ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) +bob, = ax.plot([],[], "o",color="#e63946",ms=14,zorder=4) + +def update(f): + cx=x_arr[f] + cpatch.set_x(cx-cart_w/2) + w1.center=(cx-cart_w/3,track_y); w2.center=(cx+cart_w/3,track_y) + spx,spy=spring_path_fn(wall_x, cx-cart_w/2) + sp.set_data(spx,spy) + bx=cx+L*np.sin(h_arr[f]); by=cy-L*np.cos(h_arr[f]) + rod.set_data([cx,bx],[cy,by]); bob.set_data([bx],[by]) + return sp,rod,bob + +ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) +ani.save("cart_pendulum.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) +plt.close(); print("Saved cart_pendulum.mp4") diff --git a/scipy/cart_pendulum/724/video.mp4 b/scipy/cart_pendulum/724/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..89dd5839a8306343a5960abd275e565ce1662eba --- /dev/null +++ b/scipy/cart_pendulum/724/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:295b5ba9d119b6cb9de46765ab5f3dcdc28d2033e03d998e17860da0f521b6a9 +size 13328 diff --git a/scipy/cart_two_metronomes/096/cot.json b/scipy/cart_two_metronomes/096/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..cc10eed813532cbcec975f9b493ada4ef2f57122 --- /dev/null +++ b/scipy/cart_two_metronomes/096/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=2.291 kg) on frictionless rails carries two pendulums of length L1=0.502 m, L2=0.674 m and bob masses m1=0.368 kg, m2=0.626 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.271 rad and theta2=-0.291 rad. Cart motion couples them; viscous angular damping b1=0.0034, b2=0.0096 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 3.285 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.422 s, T2 = 2 pi \\sqrt(L2/g) = 1.648 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 3.2846542362957334, + "T1_natural_s": 1.421755996841288, + "T2_natural_s": 1.6479838975491532 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.42 s and 1.65 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 2.2910781528911093, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.36774598171505607, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.6258301016895679, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.5017836155733943, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.6741744146695245, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.003370253824819579, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.009634906782267484, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.2708897482710845, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.29097341316386494, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/096/params.json b/scipy/cart_two_metronomes/096/params.json new file mode 100644 index 0000000000000000000000000000000000000000..8beb68c7d23cf761cb7a2472915bb8eef1caac4b --- /dev/null +++ b/scipy/cart_two_metronomes/096/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 2.2910781528911093, + "m_1": 0.36774598171505607, + "m_2": 0.6258301016895679, + "L_1": 0.5017836155733943, + "L_2": 0.6741744146695245, + "b_1": 0.003370253824819579, + "b_2": 0.009634906782267484, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.2708897482710845, + "theta2_0": -0.29097341316386494, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/096/simulation_code.py b/scipy/cart_two_metronomes/096/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..677ea0eaa4d1b3d0bb085cb7de1994bcd15c372b --- /dev/null +++ b/scipy/cart_two_metronomes/096/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 2.2910781528911093 +m1 = 0.36774598171505607 +m2 = 0.6258301016895679 +L1 = 0.5017836155733943 +L2 = 0.6741744146695245 +b1 = 0.003370253824819579 +b2 = 0.009634906782267484 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.2708897482710845 +theta2_0= -0.29097341316386494 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/096/video.mp4 b/scipy/cart_two_metronomes/096/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..9bf5cca1bd6c9504eee7ee9f0af6097c56d6ea3f --- /dev/null +++ b/scipy/cart_two_metronomes/096/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3745e691fe554a074e7ff2ca6cc8363db8ab074ed67531fba771ee35592001a9 +size 25528 diff --git a/scipy/cart_two_metronomes/099/cot.json b/scipy/cart_two_metronomes/099/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..d5e5594e383927ae3f28ad78cac5b7433f9138e3 --- /dev/null +++ b/scipy/cart_two_metronomes/099/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=3.667 kg) on frictionless rails carries two pendulums of length L1=0.478 m, L2=0.744 m and bob masses m1=0.379 kg, m2=0.967 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.270 rad and theta2=-0.312 rad. Cart motion couples them; viscous angular damping b1=0.0036, b2=0.0082 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 5.013 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.388 s, T2 = 2 pi \\sqrt(L2/g) = 1.731 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 5.013472866373509, + "T1_natural_s": 1.3878708979444627, + "T2_natural_s": 1.7309288250115131 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.39 s and 1.73 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 3.6669153832184316, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.37944761399609594, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.9671098691589819, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.47815034931025086, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.7437461994389918, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.003634722394197194, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.008174182110492128, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.2696033613277401, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.3116306661299151, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/099/params.json b/scipy/cart_two_metronomes/099/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e174bb126f6e0a86b9c981e1371c2fcef5c0c6ee --- /dev/null +++ b/scipy/cart_two_metronomes/099/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 3.6669153832184316, + "m_1": 0.37944761399609594, + "m_2": 0.9671098691589819, + "L_1": 0.47815034931025086, + "L_2": 0.7437461994389918, + "b_1": 0.003634722394197194, + "b_2": 0.008174182110492128, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.2696033613277401, + "theta2_0": -0.3116306661299151, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/099/simulation_code.py b/scipy/cart_two_metronomes/099/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0eb87cc15fc4c5af56d17194ea9a6da066f46231 --- /dev/null +++ b/scipy/cart_two_metronomes/099/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 3.6669153832184316 +m1 = 0.37944761399609594 +m2 = 0.9671098691589819 +L1 = 0.47815034931025086 +L2 = 0.7437461994389918 +b1 = 0.003634722394197194 +b2 = 0.008174182110492128 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.2696033613277401 +theta2_0= -0.3116306661299151 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/099/video.mp4 b/scipy/cart_two_metronomes/099/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..0a0bbfcb58f60553b336929fadb168c4cce0cf51 --- /dev/null +++ b/scipy/cart_two_metronomes/099/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f094b8ddf2a6e362ed83587231d9337ffb5c0bc001be20b6a7aed2b854b78b7c +size 27968 diff --git a/scipy/cart_two_metronomes/135/cot.json b/scipy/cart_two_metronomes/135/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..16b8313b21f80a647cea4920752646cd8205aef5 --- /dev/null +++ b/scipy/cart_two_metronomes/135/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=2.801 kg) on frictionless rails carries two pendulums of length L1=0.445 m, L2=0.733 m and bob masses m1=0.480 kg, m2=0.631 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.528 rad and theta2=-0.300 rad. Cart motion couples them; viscous angular damping b1=0.0055, b2=0.0099 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 3.912 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.339 s, T2 = 2 pi \\sqrt(L2/g) = 1.718 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 3.911927275213122, + "T1_natural_s": 1.3386000694327536, + "T2_natural_s": 1.7184442575329184 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.34 s and 1.72 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 2.8007033113855258, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.4797846784330112, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.6314392853945854, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.4448033253424639, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.7330561427070679, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.00550949971046476, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.009855945224454241, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.5282614035837963, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.30035295111377364, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/135/params.json b/scipy/cart_two_metronomes/135/params.json new file mode 100644 index 0000000000000000000000000000000000000000..a44ffa78937e9418f0b27a44ffe46fa71c627808 --- /dev/null +++ b/scipy/cart_two_metronomes/135/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 2.8007033113855258, + "m_1": 0.4797846784330112, + "m_2": 0.6314392853945854, + "L_1": 0.4448033253424639, + "L_2": 0.7330561427070679, + "b_1": 0.00550949971046476, + "b_2": 0.009855945224454241, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.5282614035837963, + "theta2_0": -0.30035295111377364, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/135/simulation_code.py b/scipy/cart_two_metronomes/135/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d0d074a144db81bb8a84bb9f1eb3466abe72f78b --- /dev/null +++ b/scipy/cart_two_metronomes/135/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 2.8007033113855258 +m1 = 0.4797846784330112 +m2 = 0.6314392853945854 +L1 = 0.4448033253424639 +L2 = 0.7330561427070679 +b1 = 0.00550949971046476 +b2 = 0.009855945224454241 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.5282614035837963 +theta2_0= -0.30035295111377364 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/135/video.mp4 b/scipy/cart_two_metronomes/135/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..827e2541d1ec7fb2c537d412c6caf2f830d1228b --- /dev/null +++ b/scipy/cart_two_metronomes/135/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e49f3c118d6b72a26879e43f6839451bc21170a0f27cb7d6719fa5f43366563c +size 29514 diff --git a/scipy/cart_two_metronomes/194/cot.json b/scipy/cart_two_metronomes/194/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..a5a33af8eba1214da009889480423ad86da15e8d --- /dev/null +++ b/scipy/cart_two_metronomes/194/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=2.323 kg) on frictionless rails carries two pendulums of length L1=0.548 m, L2=0.598 m and bob masses m1=0.590 kg, m2=0.791 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.319 rad and theta2=-0.328 rad. Cart motion couples them; viscous angular damping b1=0.0041, b2=0.0066 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 3.705 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.486 s, T2 = 2 pi \\sqrt(L2/g) = 1.552 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 3.70460555236061, + "T1_natural_s": 1.485613583982485, + "T2_natural_s": 1.5517822894638238 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.49 s and 1.55 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 2.3233316583391983, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.5904002688224899, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.7908736251989219, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.547870684223242, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.5977614736400287, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.004079408830260501, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.006593560893523524, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.3192279559829628, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.32770268880804515, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/194/params.json b/scipy/cart_two_metronomes/194/params.json new file mode 100644 index 0000000000000000000000000000000000000000..8c4384367a962d1c1b9b2c3ad72b1bce3b115b7a --- /dev/null +++ b/scipy/cart_two_metronomes/194/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 2.3233316583391983, + "m_1": 0.5904002688224899, + "m_2": 0.7908736251989219, + "L_1": 0.547870684223242, + "L_2": 0.5977614736400287, + "b_1": 0.004079408830260501, + "b_2": 0.006593560893523524, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.3192279559829628, + "theta2_0": -0.32770268880804515, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/194/simulation_code.py b/scipy/cart_two_metronomes/194/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..faf0571297035e5e00b29cb943f82e2a9303bd32 --- /dev/null +++ b/scipy/cart_two_metronomes/194/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 2.3233316583391983 +m1 = 0.5904002688224899 +m2 = 0.7908736251989219 +L1 = 0.547870684223242 +L2 = 0.5977614736400287 +b1 = 0.004079408830260501 +b2 = 0.006593560893523524 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.3192279559829628 +theta2_0= -0.32770268880804515 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/194/video.mp4 b/scipy/cart_two_metronomes/194/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f129d6ea65d10581b58f30252a433884cab19515 --- /dev/null +++ b/scipy/cart_two_metronomes/194/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:599b510cc23b9778806506826d6907348388ec975fb932b6e983b807e22d345c +size 25800 diff --git a/scipy/cart_two_metronomes/248/cot.json b/scipy/cart_two_metronomes/248/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..1084c4fb798b8d31f5ef612b6f50949f94e15eb2 --- /dev/null +++ b/scipy/cart_two_metronomes/248/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=3.426 kg) on frictionless rails carries two pendulums of length L1=0.357 m, L2=0.726 m and bob masses m1=0.639 kg, m2=0.907 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.438 rad and theta2=-0.280 rad. Cart motion couples them; viscous angular damping b1=0.0028, b2=0.0097 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 4.972 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.199 s, T2 = 2 pi \\sqrt(L2/g) = 1.710 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 4.971783900646196, + "T1_natural_s": 1.1991847276789434, + "T2_natural_s": 1.7101173893866115 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.20 s and 1.71 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 3.425854472786294, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.6389271241026477, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.9070023037572548, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.35697558726895473, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.7259691825785068, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.002849618133879823, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.009723918535149315, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.437809509920205, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.27985844610499816, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/248/params.json b/scipy/cart_two_metronomes/248/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e14e2ddfa750318d36af83f9cf7fd6c4a119f29b --- /dev/null +++ b/scipy/cart_two_metronomes/248/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 3.425854472786294, + "m_1": 0.6389271241026477, + "m_2": 0.9070023037572548, + "L_1": 0.35697558726895473, + "L_2": 0.7259691825785068, + "b_1": 0.002849618133879823, + "b_2": 0.009723918535149315, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.437809509920205, + "theta2_0": -0.27985844610499816, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/248/simulation_code.py b/scipy/cart_two_metronomes/248/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..8d5ae4067162a47375e6ea1c5df5bf32cd93768a --- /dev/null +++ b/scipy/cart_two_metronomes/248/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 3.425854472786294 +m1 = 0.6389271241026477 +m2 = 0.9070023037572548 +L1 = 0.35697558726895473 +L2 = 0.7259691825785068 +b1 = 0.002849618133879823 +b2 = 0.009723918535149315 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.437809509920205 +theta2_0= -0.27985844610499816 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/248/video.mp4 b/scipy/cart_two_metronomes/248/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..062dc9f59067b46cdce2e755295e66d5c66f8e2c --- /dev/null +++ b/scipy/cart_two_metronomes/248/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bbe8d6fc3c59c4d9eb15180b84583e003efde3dcbbfbbf2dd532c5d77bf82f89 +size 28278 diff --git a/scipy/cart_two_metronomes/362/cot.json b/scipy/cart_two_metronomes/362/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..895b9471385da93ba296fcffce4b24d001a41e1e --- /dev/null +++ b/scipy/cart_two_metronomes/362/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=3.800 kg) on frictionless rails carries two pendulums of length L1=0.305 m, L2=0.743 m and bob masses m1=0.438 kg, m2=0.982 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.544 rad and theta2=-0.358 rad. Cart motion couples them; viscous angular damping b1=0.0067, b2=0.0038 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 5.220 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.108 s, T2 = 2 pi \\sqrt(L2/g) = 1.730 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 5.219668852154799, + "T1_natural_s": 1.1079901465195623, + "T2_natural_s": 1.7299760751505915 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.11 s and 1.73 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 3.799672698862397, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.43832493199801975, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.9816712212943819, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.30474608520174357, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.7429276688784637, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.006710130790697596, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.0038359095562718365, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.544250632810822, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.3580460228528256, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/362/params.json b/scipy/cart_two_metronomes/362/params.json new file mode 100644 index 0000000000000000000000000000000000000000..bdeff1533eaf6c291729ab971ec955d440e40572 --- /dev/null +++ b/scipy/cart_two_metronomes/362/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 3.799672698862397, + "m_1": 0.43832493199801975, + "m_2": 0.9816712212943819, + "L_1": 0.30474608520174357, + "L_2": 0.7429276688784637, + "b_1": 0.006710130790697596, + "b_2": 0.0038359095562718365, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.544250632810822, + "theta2_0": -0.3580460228528256, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/362/simulation_code.py b/scipy/cart_two_metronomes/362/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..3cc768ab68f0f676588eab46e5be6ceacaad1494 --- /dev/null +++ b/scipy/cart_two_metronomes/362/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 3.799672698862397 +m1 = 0.43832493199801975 +m2 = 0.9816712212943819 +L1 = 0.30474608520174357 +L2 = 0.7429276688784637 +b1 = 0.006710130790697596 +b2 = 0.0038359095562718365 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.544250632810822 +theta2_0= -0.3580460228528256 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/362/video.mp4 b/scipy/cart_two_metronomes/362/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3fef652f3b341686e9bd390054d693bde20bb915 --- /dev/null +++ b/scipy/cart_two_metronomes/362/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:69f5475ead5243dd10851b123fcf1a545856e7ce3b4757d17d78bf08fcf14ec1 +size 28437 diff --git a/scipy/cart_two_metronomes/416/cot.json b/scipy/cart_two_metronomes/416/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..5fc9fd62ebb2ad909220c1e4afd0dd8f10153398 --- /dev/null +++ b/scipy/cart_two_metronomes/416/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=3.525 kg) on frictionless rails carries two pendulums of length L1=0.360 m, L2=0.569 m and bob masses m1=0.663 kg, m2=0.996 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.369 rad and theta2=-0.384 rad. Cart motion couples them; viscous angular damping b1=0.0063, b2=0.0101 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 5.184 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.204 s, T2 = 2 pi \\sqrt(L2/g) = 1.514 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 5.1835862903665095, + "T1_natural_s": 1.204442750297607, + "T2_natural_s": 1.514331736578323 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.20 s and 1.51 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 3.5247327844079104, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.663065064526991, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.9957884414316076, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.36011288654402845, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.5692570099344871, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.0062601443131428194, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.010087945108753044, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.3691129887149667, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.3836719994431912, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/416/params.json b/scipy/cart_two_metronomes/416/params.json new file mode 100644 index 0000000000000000000000000000000000000000..71501801686335103018927d1bac52a48f73e968 --- /dev/null +++ b/scipy/cart_two_metronomes/416/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 3.5247327844079104, + "m_1": 0.663065064526991, + "m_2": 0.9957884414316076, + "L_1": 0.36011288654402845, + "L_2": 0.5692570099344871, + "b_1": 0.0062601443131428194, + "b_2": 0.010087945108753044, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.3691129887149667, + "theta2_0": -0.3836719994431912, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/416/simulation_code.py b/scipy/cart_two_metronomes/416/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..65df5b09e51d4b535c05891b27e52dfc12e7c0d4 --- /dev/null +++ b/scipy/cart_two_metronomes/416/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 3.5247327844079104 +m1 = 0.663065064526991 +m2 = 0.9957884414316076 +L1 = 0.36011288654402845 +L2 = 0.5692570099344871 +b1 = 0.0062601443131428194 +b2 = 0.010087945108753044 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.3691129887149667 +theta2_0= -0.3836719994431912 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/416/video.mp4 b/scipy/cart_two_metronomes/416/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8a6c95ee886c33be53b948b933196bc7359f527e --- /dev/null +++ b/scipy/cart_two_metronomes/416/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:22ab26cae859961d70392eacbc54d236aff9037584388aaa29ec2875c04de648 +size 25548 diff --git a/scipy/cart_two_metronomes/441/cot.json b/scipy/cart_two_metronomes/441/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..4dcb985cb29ea13811953e2d47db8463d7a99bb6 --- /dev/null +++ b/scipy/cart_two_metronomes/441/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=3.961 kg) on frictionless rails carries two pendulums of length L1=0.401 m, L2=0.519 m and bob masses m1=0.427 kg, m2=0.733 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.333 rad and theta2=-0.376 rad. Cart motion couples them; viscous angular damping b1=0.0028, b2=0.0079 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 5.121 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.271 s, T2 = 2 pi \\sqrt(L2/g) = 1.447 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 5.121289284286709, + "T1_natural_s": 1.270797138704522, + "T2_natural_s": 1.446541538377841 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.27 s and 1.45 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 3.961128957907735, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.42707515097795723, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.7330851754010159, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.4008840668958641, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.5194313496448606, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.0028498268917015747, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.00790396736949784, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.33282133704749584, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.3761290947279753, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/441/params.json b/scipy/cart_two_metronomes/441/params.json new file mode 100644 index 0000000000000000000000000000000000000000..20d792be89c267379c78d3e596ba3290ae5e5026 --- /dev/null +++ b/scipy/cart_two_metronomes/441/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 3.961128957907735, + "m_1": 0.42707515097795723, + "m_2": 0.7330851754010159, + "L_1": 0.4008840668958641, + "L_2": 0.5194313496448606, + "b_1": 0.0028498268917015747, + "b_2": 0.00790396736949784, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.33282133704749584, + "theta2_0": -0.3761290947279753, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/441/simulation_code.py b/scipy/cart_two_metronomes/441/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..5117527dccc201ffd56fb1df877795e065348339 --- /dev/null +++ b/scipy/cart_two_metronomes/441/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 3.961128957907735 +m1 = 0.42707515097795723 +m2 = 0.7330851754010159 +L1 = 0.4008840668958641 +L2 = 0.5194313496448606 +b1 = 0.0028498268917015747 +b2 = 0.00790396736949784 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.33282133704749584 +theta2_0= -0.3761290947279753 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/441/video.mp4 b/scipy/cart_two_metronomes/441/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1feef3416993e3bf4ac6dc0bb41bbe79ce0c95a2 --- /dev/null +++ b/scipy/cart_two_metronomes/441/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f66a8def9d1b5c67dc7dd9baf7ace7ee8ecc4f0e4a731ca3aa7737e0e7ceba7b +size 24752 diff --git a/scipy/cart_two_metronomes/484/cot.json b/scipy/cart_two_metronomes/484/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..058cc4da76372fc55dcecce06dfc137c0d1f8ecb --- /dev/null +++ b/scipy/cart_two_metronomes/484/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=3.939 kg) on frictionless rails carries two pendulums of length L1=0.356 m, L2=0.591 m and bob masses m1=0.460 kg, m2=0.502 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.416 rad and theta2=-0.417 rad. Cart motion couples them; viscous angular damping b1=0.0047, b2=0.0035 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 4.902 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.198 s, T2 = 2 pi \\sqrt(L2/g) = 1.543 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 4.90179318816229, + "T1_natural_s": 1.1983780246343985, + "T2_natural_s": 1.5433130881005375 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.20 s and 1.54 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 3.9392192639811157, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.4603069650831426, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.5022669590980315, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.35649546702519547, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.5912544432598368, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.0046682748892454195, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.0035248755524595744, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.41562232882164496, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.4165058822422879, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/484/params.json b/scipy/cart_two_metronomes/484/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c2115a9ec97ecd99e79c903a04311b9fa85696ee --- /dev/null +++ b/scipy/cart_two_metronomes/484/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 3.9392192639811157, + "m_1": 0.4603069650831426, + "m_2": 0.5022669590980315, + "L_1": 0.35649546702519547, + "L_2": 0.5912544432598368, + "b_1": 0.0046682748892454195, + "b_2": 0.0035248755524595744, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.41562232882164496, + "theta2_0": -0.4165058822422879, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/484/simulation_code.py b/scipy/cart_two_metronomes/484/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d2e00b0a73db21a6e2c046052887a4776f5e2617 --- /dev/null +++ b/scipy/cart_two_metronomes/484/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 3.9392192639811157 +m1 = 0.4603069650831426 +m2 = 0.5022669590980315 +L1 = 0.35649546702519547 +L2 = 0.5912544432598368 +b1 = 0.0046682748892454195 +b2 = 0.0035248755524595744 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.41562232882164496 +theta2_0= -0.4165058822422879 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/484/video.mp4 b/scipy/cart_two_metronomes/484/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e28643c57f43e6a8909bb3274a05fb39fc612ce7 --- /dev/null +++ b/scipy/cart_two_metronomes/484/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c0c1c945e5e67b6a8d81cb16bea1e8b8130ab578872bf56ace2fd601deedc653 +size 26995 diff --git a/scipy/cart_two_metronomes/584/cot.json b/scipy/cart_two_metronomes/584/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..f09cd0c447b85d7d8bca6ee0813286529f2d0b34 --- /dev/null +++ b/scipy/cart_two_metronomes/584/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=3.899 kg) on frictionless rails carries two pendulums of length L1=0.548 m, L2=0.620 m and bob masses m1=0.680 kg, m2=0.995 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.349 rad and theta2=-0.317 rad. Cart motion couples them; viscous angular damping b1=0.0059, b2=0.0113 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 5.573 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.486 s, T2 = 2 pi \\sqrt(L2/g) = 1.580 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 5.573282112765588, + "T1_natural_s": 1.4855521187779623, + "T2_natural_s": 1.5803480511283736 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.49 s and 1.58 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 3.898612813851095, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.6797556213924782, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.9949136775220153, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.5478253503795117, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.6199716483015761, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.005894953234035272, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.011253056408407586, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.3492262067704299, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.3171117470402641, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/584/params.json b/scipy/cart_two_metronomes/584/params.json new file mode 100644 index 0000000000000000000000000000000000000000..9810790d47bc428682c82dd42f03261193fee14a --- /dev/null +++ b/scipy/cart_two_metronomes/584/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 3.898612813851095, + "m_1": 0.6797556213924782, + "m_2": 0.9949136775220153, + "L_1": 0.5478253503795117, + "L_2": 0.6199716483015761, + "b_1": 0.005894953234035272, + "b_2": 0.011253056408407586, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.3492262067704299, + "theta2_0": -0.3171117470402641, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/584/simulation_code.py b/scipy/cart_two_metronomes/584/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..5878313f13591bd4715d9456884f09f9cf490078 --- /dev/null +++ b/scipy/cart_two_metronomes/584/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 3.898612813851095 +m1 = 0.6797556213924782 +m2 = 0.9949136775220153 +L1 = 0.5478253503795117 +L2 = 0.6199716483015761 +b1 = 0.005894953234035272 +b2 = 0.011253056408407586 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.3492262067704299 +theta2_0= -0.3171117470402641 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/584/video.mp4 b/scipy/cart_two_metronomes/584/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..159072733e326f0bc127d5e2092f51a69aa85be9 --- /dev/null +++ b/scipy/cart_two_metronomes/584/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2acdfd7a06fd79037472af417f6c9cfc3a9e52618bd2b8563d291bc9993098f8 +size 26112 diff --git a/scipy/cart_two_metronomes/590/cot.json b/scipy/cart_two_metronomes/590/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..b370edecd760338623fc6d7c4648bf318ef5178d --- /dev/null +++ b/scipy/cart_two_metronomes/590/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=2.125 kg) on frictionless rails carries two pendulums of length L1=0.316 m, L2=0.477 m and bob masses m1=0.637 kg, m2=0.592 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.478 rad and theta2=-0.274 rad. Cart motion couples them; viscous angular damping b1=0.0039, b2=0.0068 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 3.354 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.128 s, T2 = 2 pi \\sqrt(L2/g) = 1.387 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 3.3541577902967523, + "T1_natural_s": 1.1276968690920401, + "T2_natural_s": 1.38672703408481 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.13 s and 1.39 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 2.124955738405981, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.6369681582113884, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.5922338936793825, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.3156829223700277, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.47736250439592426, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.0038580463779664563, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.0067964050655156885, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.4775037243901905, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.2742125620249186, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/590/params.json b/scipy/cart_two_metronomes/590/params.json new file mode 100644 index 0000000000000000000000000000000000000000..ef3e2ad9d4db40532e682a47b68a72a729cea91f --- /dev/null +++ b/scipy/cart_two_metronomes/590/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 2.124955738405981, + "m_1": 0.6369681582113884, + "m_2": 0.5922338936793825, + "L_1": 0.3156829223700277, + "L_2": 0.47736250439592426, + "b_1": 0.0038580463779664563, + "b_2": 0.0067964050655156885, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.4775037243901905, + "theta2_0": -0.2742125620249186, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/590/simulation_code.py b/scipy/cart_two_metronomes/590/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..db3460d1133fe365a62163bfc7e75373aaab5302 --- /dev/null +++ b/scipy/cart_two_metronomes/590/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 2.124955738405981 +m1 = 0.6369681582113884 +m2 = 0.5922338936793825 +L1 = 0.3156829223700277 +L2 = 0.47736250439592426 +b1 = 0.0038580463779664563 +b2 = 0.0067964050655156885 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.4775037243901905 +theta2_0= -0.2742125620249186 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/590/video.mp4 b/scipy/cart_two_metronomes/590/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..6f67d7c8dc1bbe9f61ce0baedee5faa40c6971b7 --- /dev/null +++ b/scipy/cart_two_metronomes/590/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b2009f21aef73168e4c78c89d41dc3330f81e1f83e10f9a227fdff44e8292e2 +size 22790 diff --git a/scipy/cart_two_metronomes/674/cot.json b/scipy/cart_two_metronomes/674/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..52a38e6468c4c58b1937b1b4c8815735e12de342 --- /dev/null +++ b/scipy/cart_two_metronomes/674/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=3.947 kg) on frictionless rails carries two pendulums of length L1=0.468 m, L2=0.597 m and bob masses m1=0.461 kg, m2=0.813 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.504 rad and theta2=-0.536 rad. Cart motion couples them; viscous angular damping b1=0.0075, b2=0.0077 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 5.221 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.373 s, T2 = 2 pi \\sqrt(L2/g) = 1.551 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 5.2214940343326095, + "T1_natural_s": 1.3727704732986112, + "T2_natural_s": 1.5510044971755688 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.37 s and 1.55 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 3.947352515851968, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.4614390170516432, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.8127025014289981, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.4678021331608392, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.5971623976624263, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.007520742995345226, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.007655400676447586, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.5037086471331711, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.5356423313346049, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/674/params.json b/scipy/cart_two_metronomes/674/params.json new file mode 100644 index 0000000000000000000000000000000000000000..7971e6ec10af28a1070453dc65d71740ecc4e2d6 --- /dev/null +++ b/scipy/cart_two_metronomes/674/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 3.947352515851968, + "m_1": 0.4614390170516432, + "m_2": 0.8127025014289981, + "L_1": 0.4678021331608392, + "L_2": 0.5971623976624263, + "b_1": 0.007520742995345226, + "b_2": 0.007655400676447586, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.5037086471331711, + "theta2_0": -0.5356423313346049, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/674/simulation_code.py b/scipy/cart_two_metronomes/674/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c12a4a520618f1dfe91db6cc590e69870289a652 --- /dev/null +++ b/scipy/cart_two_metronomes/674/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 3.947352515851968 +m1 = 0.4614390170516432 +m2 = 0.8127025014289981 +L1 = 0.4678021331608392 +L2 = 0.5971623976624263 +b1 = 0.007520742995345226 +b2 = 0.007655400676447586 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.5037086471331711 +theta2_0= -0.5356423313346049 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/674/video.mp4 b/scipy/cart_two_metronomes/674/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..60b84f112182da17c802fa357afaa6a4b72dcb25 --- /dev/null +++ b/scipy/cart_two_metronomes/674/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2b084da955996e86ebd087a13afb836764eddff611d237f0d547973cb529d82a +size 28815 diff --git a/scipy/cart_two_metronomes/740/cot.json b/scipy/cart_two_metronomes/740/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..8759fe63836756e39a7df4fd509833fe89ee6236 --- /dev/null +++ b/scipy/cart_two_metronomes/740/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=2.403 kg) on frictionless rails carries two pendulums of length L1=0.397 m, L2=0.484 m and bob masses m1=0.405 kg, m2=0.698 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.423 rad and theta2=-0.265 rad. Cart motion couples them; viscous angular damping b1=0.0028, b2=0.0065 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 3.505 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.264 s, T2 = 2 pi \\sqrt(L2/g) = 1.397 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 3.5052850387651353, + "T1_natural_s": 1.2641096208559066, + "T2_natural_s": 1.3968906842787474 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.26 s and 1.40 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 2.4027491024471015, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.4045866954873154, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.6979492408307183, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.3966758968314895, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.48438555245871484, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.0027945258346604076, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.00651132458097315, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.4225611794558892, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.26534473167935435, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/740/params.json b/scipy/cart_two_metronomes/740/params.json new file mode 100644 index 0000000000000000000000000000000000000000..d3bceca0a09ac49eb08d0c1653723f36fbf73d82 --- /dev/null +++ b/scipy/cart_two_metronomes/740/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 2.4027491024471015, + "m_1": 0.4045866954873154, + "m_2": 0.6979492408307183, + "L_1": 0.3966758968314895, + "L_2": 0.48438555245871484, + "b_1": 0.0027945258346604076, + "b_2": 0.00651132458097315, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.4225611794558892, + "theta2_0": -0.26534473167935435, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/740/simulation_code.py b/scipy/cart_two_metronomes/740/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..92426ec60e7aa9f0b9e88d787da49f03f4d3e488 --- /dev/null +++ b/scipy/cart_two_metronomes/740/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 2.4027491024471015 +m1 = 0.4045866954873154 +m2 = 0.6979492408307183 +L1 = 0.3966758968314895 +L2 = 0.48438555245871484 +b1 = 0.0027945258346604076 +b2 = 0.00651132458097315 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.4225611794558892 +theta2_0= -0.26534473167935435 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/740/video.mp4 b/scipy/cart_two_metronomes/740/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1b043de004f6d89c20f27101a71a50fc74056b79 --- /dev/null +++ b/scipy/cart_two_metronomes/740/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6d59df9163d7981e763249c79521278427ecb51d08ee843a1dddedfd2c0bff36 +size 23129 diff --git a/scipy/cart_two_metronomes/803/cot.json b/scipy/cart_two_metronomes/803/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..273decfe5196d36107a8eaf0053685af66897dd8 --- /dev/null +++ b/scipy/cart_two_metronomes/803/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=3.544 kg) on frictionless rails carries two pendulums of length L1=0.398 m, L2=0.718 m and bob masses m1=0.686 kg, m2=0.822 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.393 rad and theta2=-0.279 rad. Cart motion couples them; viscous angular damping b1=0.0033, b2=0.0057 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 5.053 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.267 s, T2 = 2 pi \\sqrt(L2/g) = 1.701 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 5.052677782276621, + "T1_natural_s": 1.2666684366359588, + "T2_natural_s": 1.701058412676275 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.27 s and 1.70 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 3.5443913568678336, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.6860447193938533, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.8222417060149346, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.3982834280644621, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.718298224941265, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.0033407995901647033, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.00569501025545221, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.39262742858749317, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.279160880999087, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/803/params.json b/scipy/cart_two_metronomes/803/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5de3ab744aedc66d2d3074e410457505f8672b0f --- /dev/null +++ b/scipy/cart_two_metronomes/803/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 3.5443913568678336, + "m_1": 0.6860447193938533, + "m_2": 0.8222417060149346, + "L_1": 0.3982834280644621, + "L_2": 0.718298224941265, + "b_1": 0.0033407995901647033, + "b_2": 0.00569501025545221, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.39262742858749317, + "theta2_0": -0.279160880999087, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/803/simulation_code.py b/scipy/cart_two_metronomes/803/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..86447b8ac279afafff0d4d22b53ca0b3eae64d87 --- /dev/null +++ b/scipy/cart_two_metronomes/803/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 3.5443913568678336 +m1 = 0.6860447193938533 +m2 = 0.8222417060149346 +L1 = 0.3982834280644621 +L2 = 0.718298224941265 +b1 = 0.0033407995901647033 +b2 = 0.00569501025545221 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.39262742858749317 +theta2_0= -0.279160880999087 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/803/video.mp4 b/scipy/cart_two_metronomes/803/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f5b3cda33b7e652195cf674b225d6e0bd5f76632 --- /dev/null +++ b/scipy/cart_two_metronomes/803/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2dbd0525bd2af2086b568c2543edd19e1ecfea336582345038d7890a03d2144c +size 27235 diff --git a/scipy/cart_two_metronomes/826/cot.json b/scipy/cart_two_metronomes/826/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..0b51208c4559b89544062f99a813775edfc34c36 --- /dev/null +++ b/scipy/cart_two_metronomes/826/cot.json @@ -0,0 +1,145 @@ +{ + "simulation": "cart_two_metronomes", + "physical_observation": "A cart (M=2.901 kg) on frictionless rails carries two pendulums of length L1=0.354 m, L2=0.709 m and bob masses m1=0.506 kg, m2=0.930 kg, with pivots at x-offsets dx1=-0.300 m and dx2=0.300 m. The pendulums are released at theta1=0.542 rad and theta2=-0.483 rad. Cart motion couples them; viscous angular damping b1=0.0043, b2=0.0048 dissipates energy.", + "physical_law": { + "name": "Lagrangian mechanics + horizontal-momentum conservation", + "statement": "Generalised coordinates (X, theta1, theta2). The Euler-Lagrange equations yield a 3x3 mass matrix system; with frictionless rails horizontal momentum is conserved.", + "formula": "M_tot Xdd + sum_i m_i L_i \\cos(theta_i) theta_i_dd = sum_i m_i L_i \\sin(theta_i) w_i^2; m_i L_i^2 theta_i_dd + m_i L_i \\cos(theta_i) Xdd = -m_i g L_i \\sin(theta_i) - b_i w_i" + }, + "ode_system": { + "state_variables": [ + "X (m)", + "theta1 (rad)", + "theta2 (rad)", + "Xdot (m/s)", + "w1 (rad/s)", + "w2 (rad/s)" + ], + "equations_latex": [ + "A = [[M_tot, m1 L1 c1, m2 L2 c2], [m1 L1 c1, m1 L1^2, 0], [m2 L2 c2, 0, m2 L2^2]]", + "b = [m1 L1 s1 w1^2 + m2 L2 s2 w2^2, -m1 g L1 s1 - b1 w1, -m2 g L2 s2 - b2 w2]", + "[Xdd, t1dd, t2dd] = solve(A, b)" + ], + "equations_python": [ + "A = [[M_tot, m1 * L1 * c1, m2 * L2 * c2], [m1 * L1 * c1, m1 * L1**2, 0], [m2 * L2 * c2, 0, m2 * L2**2]]", + "b = [m1 * L1 * s1 * w1**2 + m2 * L2 * s2 * w2**2, -m1 * g * L1 * s1 - b1 * w1, -m2 * g * L2 * s2 - b2 * w2]", + "[Xdd, t1 * dd, t2 * dd] = solve(A, b)" + ] + }, + "derivation_steps": [ + "Total mass M_tot = M + m1 + m2 = 4.337 kg.", + "Bob i position: x_i = X + dx_i + L_i \\sin(theta_i), y_i = h_pivot - L_i \\cos(theta_i).", + "Form Lagrangian L = T - V with T = (1/2) M Xdot^2 + sum_i (1/2) m_i [vx_i^2 + vy_i^2], V = -sum_i m_i g L_i \\cos(theta_i).", + "Euler-Lagrange equations give a 3x3 linear-in-accelerations system; solve at every RHS call.", + "Pendulum natural periods (small angle): T1 = 2 pi \\sqrt(L1/g) = 1.195 s, T2 = 2 pi \\sqrt(L2/g) = 1.690 s.", + "Damping is Rayleigh dissipation per pendulum: -b_i theta_i_dot.", + "Horizontal momentum P_x = M_tot Xdot + sum_i m_i L_i \\cos(theta_i) w_i is conserved." + ], + "physical_constants_derived": { + "M_tot_kg": 4.336858039769654, + "T1_natural_s": 1.1949580813526541, + "T2_natural_s": 1.6903283358913308 + }, + "numerical_method": { + "solver": "scipy.integrate.solve_ivp (RK45) on the 6-D state; numpy.linalg.solve for the 3x3 mass matrix at each RHS evaluation.", + "tolerance": "rtol=1e-9, atol=1e-11.", + "justification": "Smooth ODE; mass matrix is well-conditioned for moderate angles." + }, + "code_validation": { + "ode_source": "Lagrangian for cart + two pendulums; standard derivation.", + "energy_check": "With damping, total energy decreases monotonically; momentum is conserved exactly to integrator tolerance.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Two oscillators with periods ~ 1.19 s and 1.69 s exchange energy via cart motion.", + "Cart drifts/recoils to keep horizontal momentum constant.", + "Damping eventually settles both pendulums to equilibrium." + ], + "parameters": { + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "M": { + "value": 2.9007253498665824, + "unit": "", + "description": "" + }, + "m_1": { + "value": 0.5059058898045095, + "unit": "", + "description": "" + }, + "m_2": { + "value": 0.9302268000985621, + "unit": "", + "description": "" + }, + "L_1": { + "value": 0.35446362969516815, + "unit": "", + "description": "" + }, + "L_2": { + "value": 0.7092649238164388, + "unit": "", + "description": "" + }, + "b_1": { + "value": 0.004258643492133947, + "unit": "", + "description": "" + }, + "b_2": { + "value": 0.004789814193712105, + "unit": "", + "description": "" + }, + "dx_1": { + "value": -0.3, + "unit": "", + "description": "" + }, + "dx_2": { + "value": 0.3, + "unit": "", + "description": "" + }, + "h_pivot": { + "value": 0.95, + "unit": "", + "description": "" + }, + "X0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "theta1_0": { + "value": 0.5419465452843205, + "unit": "", + "description": "" + }, + "theta2_0": { + "value": -0.482591261631824, + "unit": "", + "description": "" + }, + "Xdot0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "w2_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/cart_two_metronomes/826/params.json b/scipy/cart_two_metronomes/826/params.json new file mode 100644 index 0000000000000000000000000000000000000000..aa1b0bbe106f72fdb613f5c3d465ae9308a779ba --- /dev/null +++ b/scipy/cart_two_metronomes/826/params.json @@ -0,0 +1,19 @@ +{ + "g": 9.8, + "M": 2.9007253498665824, + "m_1": 0.5059058898045095, + "m_2": 0.9302268000985621, + "L_1": 0.35446362969516815, + "L_2": 0.7092649238164388, + "b_1": 0.004258643492133947, + "b_2": 0.004789814193712105, + "dx_1": -0.3, + "dx_2": 0.3, + "h_pivot": 0.95, + "X0": 0.0, + "theta1_0": 0.5419465452843205, + "theta2_0": -0.482591261631824, + "Xdot0": 0.0, + "w1_0": 0.0, + "w2_0": 0.0 +} \ No newline at end of file diff --git a/scipy/cart_two_metronomes/826/simulation_code.py b/scipy/cart_two_metronomes/826/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..2252b4b8f20fc72346f83740c9192a17cbca80b8 --- /dev/null +++ b/scipy/cart_two_metronomes/826/simulation_code.py @@ -0,0 +1,103 @@ +""" +Cart with Two Pendulums (Huygens-coupled metronomes) — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +g = 9.8 +M = 2.9007253498665824 +m1 = 0.5059058898045095 +m2 = 0.9302268000985621 +L1 = 0.35446362969516815 +L2 = 0.7092649238164388 +b1 = 0.004258643492133947 +b2 = 0.004789814193712105 +dx1 = -0.3 +dx2 = 0.3 +h_pivot = 0.95 +X0 = 0.0 +theta1_0= 0.5419465452843205 +theta2_0= -0.482591261631824 +Xdot0 = 0.0 +w1_0 = 0.0 +w2_0 = 0.0 +duration= 12 +M_tot = M + m1 + m2 + +def ode(t, y): + X, t1, t2, Xd, w1, w2 = y + c1, s1 = np.cos(t1), np.sin(t1) + c2, s2 = np.cos(t2), np.sin(t2) + A = np.array([ + [M_tot, m1 * L1 * c1, m2 * L2 * c2 ], + [m1 * L1 * c1, m1 * L1 * L1, 0.0 ], + [m2 * L2 * c2, 0.0, m2 * L2 * L2 ], + ]) + b_vec = np.array([ + m1 * L1 * s1 * w1 * w1 + m2 * L2 * s2 * w2 * w2, + -m1 * g * L1 * s1 - b1 * w1, + -m2 * g * L2 * s2 - b2 * w2, + ]) + Xdd, t1dd, t2dd = np.linalg.solve(A, b_vec) + return [Xd, w1, w2, Xdd, t1dd, t2dd] + +fps = 30 +t_eval = np.linspace(0.0, duration, int(duration * fps) + 1) +y0 = [X0, theta1_0, theta2_0, Xdot0, w1_0, w2_0] +sol = solve_ivp(ode, (0.0, duration), y0, t_eval=t_eval, + method="RK45", rtol=1e-9, atol=1e-11) +X_arr = sol.y[0]; t1_arr = sol.y[1]; t2_arr = sol.y[2] + +cart_w, cart_h = 0.9, 0.20 +cart_bottom = 0.02 +beam_y = cart_bottom + cart_h +x1_max_swing = np.max(np.abs(L1 * np.sin(t1_arr))) +x2_max_swing = np.max(np.abs(L2 * np.sin(t2_arr))) +x_extent = max(np.max(np.abs(X_arr)) + cart_w / 2.0, + np.max(np.abs(X_arr + dx1)) + x1_max_swing, + np.max(np.abs(X_arr + dx2)) + x2_max_swing) + 0.30 +xlim = (-x_extent, x_extent); ylim = (-0.25, h_pivot + 0.25) + +fig, ax = plt.subplots(figsize=(9, 5), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(*xlim); ax.set_ylim(*ylim) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0.0, color="#4a4e69", lw=1.5, zorder=1) +for tx in np.arange(np.ceil(xlim[0]), np.floor(xlim[1]) + 1e-9, 0.25): + ax.plot([tx, tx], [-0.04, 0.0], color="#4a4e69", lw=1.0, zorder=1) +cart_patch = patches.FancyBboxPatch( + (-cart_w / 2.0, cart_bottom), cart_w, cart_h, + boxstyle="round,pad=0.01", linewidth=1.5, + edgecolor="white", facecolor="#8ecae6", zorder=3, +) +ax.add_patch(cart_patch) +beam_line, = ax.plot([], [], "-", color="#cdd5e0", lw=1.5, zorder=3) +pivot1_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +pivot2_dot, = ax.plot([], [], "o", color="white", ms=5, zorder=6) +rod1, = ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=4) +rod2, = ax.plot([], [], "-", color="#ffd166", lw=2.5, zorder=4) +ms_ref = 14.0 +bob1_ms = ms_ref * (m1 / max(m1, m2)) ** (1.0 / 3.0) +bob2_ms = ms_ref * (m2 / max(m1, m2)) ** (1.0 / 3.0) +bob1, = ax.plot([], [], "o", color="#e63946", ms=bob1_ms, zorder=5) +bob2, = ax.plot([], [], "o", color="#ef476f", ms=bob2_ms, zorder=5) + +def update(f): + cx = X_arr[f] + cart_patch.set_x(cx - cart_w / 2.0) + p1x = cx + dx1; p2x = cx + dx2; py = h_pivot + beam_line.set_data([p1x, p1x, p2x, p2x], [beam_y, py, py, beam_y]) + pivot1_dot.set_data([p1x], [py]); pivot2_dot.set_data([p2x], [py]) + b1x = p1x + L1 * np.sin(t1_arr[f]); b1y = py - L1 * np.cos(t1_arr[f]) + b2x = p2x + L2 * np.sin(t2_arr[f]); b2y = py - L2 * np.cos(t2_arr[f]) + rod1.set_data([p1x, b1x], [py, b1y]) + rod2.set_data([p2x, b2x], [py, b2y]) + bob1.set_data([b1x], [b1y]); bob2.set_data([b2x], [b2y]) + return cart_patch, beam_line, pivot1_dot, pivot2_dot, rod1, rod2, bob1, bob2 + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000.0/fps, blit=False) +ani.save("cart_two_metronomes.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved cart_two_metronomes.mp4") diff --git a/scipy/cart_two_metronomes/826/video.mp4 b/scipy/cart_two_metronomes/826/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7fd708eda00a373d199041ff7d46590785243e8c --- /dev/null +++ b/scipy/cart_two_metronomes/826/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c9beff947faf8c41ce76a60eeeb0cd0f4581327e0ef2a0b9fcab6f1d7580070c +size 30752 diff --git a/scipy/chain_of_springs/055/cot.json b/scipy/chain_of_springs/055/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..13fc0131bd2734fa65f4a8242dbf0ff7547b6a49 --- /dev/null +++ b/scipy/chain_of_springs/055/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=1.857 kg each) are connected in a chain by four identical springs (k=5.726 N/m, rest length R=2.566 m) between two fixed walls. Viscous damping b=0.191 kg/s. Initial displacements: u_0=-0.508, u_1=0.088, u_2=-0.420 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 1.8065 \\rightarrow \\omega_1 = 1.3441 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 6.1678 \\rightarrow \\omega_2 = 2.4835 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 10.5292 \\rightarrow \\omega_3 = 3.2449 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 1.3441, + "omega2_rad_s": 2.4835, + "omega3_rad_s": 3.2449, + "T1_s": 4.6748, + "T2_s": 2.53, + "T3_s": 1.9363 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=1.344, \\omega_2=2.484, \\omega_3=3.245 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.191 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 5.726493417583077, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 1.8568874797331048, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.19131537248014308, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 2.5662199847919105, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": -0.5076117610243286, + "unit": "", + "description": "" + }, + "du1": { + "value": 0.08799338523317957, + "unit": "", + "description": "" + }, + "du2": { + "value": -0.42038376124584587, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/055/params.json b/scipy/chain_of_springs/055/params.json new file mode 100644 index 0000000000000000000000000000000000000000..08f90f6f7216861ed548c5f83b84fbe388288914 --- /dev/null +++ b/scipy/chain_of_springs/055/params.json @@ -0,0 +1,9 @@ +{ + "k": 5.726493417583077, + "m": 1.8568874797331048, + "b": 0.19131537248014308, + "R": 2.5662199847919105, + "du0": -0.5076117610243286, + "du1": 0.08799338523317957, + "du2": -0.42038376124584587 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/055/simulation_code.py b/scipy/chain_of_springs/055/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..af57739f85ba8d227aebc7b8c7a6111c375f5fb1 --- /dev/null +++ b/scipy/chain_of_springs/055/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=10.26] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 5.726493417583077, 1.8568874797331048, 0.19131537248014308 +R = 2.5662199847919105 +wall_left, wall_right = 0.0, 10.2649 +x0_0 = 2.5662 + -0.5076117610243286 +x1_0 = 5.1324 + 0.08799338523317957 +x2_0 = 7.6987 + -0.42038376124584587 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(2.5662, "#e63946"), (5.1324, "#f4d35e"), (7.6987, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/055/video.mp4 b/scipy/chain_of_springs/055/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1528edda9a5d961f7dcffc021dab96429201d8d7 --- /dev/null +++ b/scipy/chain_of_springs/055/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:01191c858b11b72c8c724522f068f87adefafc62098bef06092b1c0d606767e1 +size 57309 diff --git a/scipy/chain_of_springs/062/cot.json b/scipy/chain_of_springs/062/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..1664396ff67d2b0af65ab54bd95b8822e01beffc --- /dev/null +++ b/scipy/chain_of_springs/062/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=1.516 kg each) are connected in a chain by four identical springs (k=9.255 N/m, rest length R=1.357 m) between two fixed walls. Viscous damping b=0.388 kg/s. Initial displacements: u_0=0.510, u_1=0.367, u_2=0.595 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 3.5749 \\rightarrow \\omega_1 = 1.8907 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 12.2055 \\rightarrow \\omega_2 = 3.4936 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 20.8361 \\rightarrow \\omega_3 = 4.5647 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 1.8907, + "omega2_rad_s": 3.4936, + "omega3_rad_s": 4.5647, + "T1_s": 3.3231, + "T2_s": 1.7985, + "T3_s": 1.3765 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=1.891, \\omega_2=3.494, \\omega_3=4.565 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.388 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 9.254787491281446, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 1.5164919272022628, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.38845494434802696, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 1.3565833710400863, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": 0.5103104478990973, + "unit": "", + "description": "" + }, + "du1": { + "value": 0.36698887189922036, + "unit": "", + "description": "" + }, + "du2": { + "value": 0.5949474233031572, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/062/params.json b/scipy/chain_of_springs/062/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e17cee814642fe87acee7184c5c2cf84e1086eb3 --- /dev/null +++ b/scipy/chain_of_springs/062/params.json @@ -0,0 +1,9 @@ +{ + "k": 9.254787491281446, + "m": 1.5164919272022628, + "b": 0.38845494434802696, + "R": 1.3565833710400863, + "du0": 0.5103104478990973, + "du1": 0.36698887189922036, + "du2": 0.5949474233031572 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/062/simulation_code.py b/scipy/chain_of_springs/062/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a3f65a7c14c34c3ed5ead0a8f65df28f3084cd92 --- /dev/null +++ b/scipy/chain_of_springs/062/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=5.43] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 9.254787491281446, 1.5164919272022628, 0.38845494434802696 +R = 1.3565833710400863 +wall_left, wall_right = 0.0, 5.4263 +x0_0 = 1.3566 + 0.5103104478990973 +x1_0 = 2.7132 + 0.36698887189922036 +x2_0 = 4.0698 + 0.5949474233031572 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(1.3566, "#e63946"), (2.7132, "#f4d35e"), (4.0698, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/062/video.mp4 b/scipy/chain_of_springs/062/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..198b860fe4e7af9119f7342a863b8e1846350c17 --- /dev/null +++ b/scipy/chain_of_springs/062/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6b02259804d0d0da005d2ac264ccb76f52150dde0e70c07650be27eb0fe6ab1a +size 56237 diff --git a/scipy/chain_of_springs/100/cot.json b/scipy/chain_of_springs/100/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..4d2d1660c2d77a436aac23bc0df431af1f7cf244 --- /dev/null +++ b/scipy/chain_of_springs/100/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=0.551 kg each) are connected in a chain by four identical springs (k=7.143 N/m, rest length R=2.571 m) between two fixed walls. Viscous damping b=0.355 kg/s. Initial displacements: u_0=0.540, u_1=0.011, u_2=-0.361 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 7.5971 \\rightarrow \\omega_1 = 2.7563 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 25.9380 \\rightarrow \\omega_2 = 5.0929 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 44.2789 \\rightarrow \\omega_3 = 6.6542 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 2.7563, + "omega2_rad_s": 5.0929, + "omega3_rad_s": 6.6542, + "T1_s": 2.2796, + "T2_s": 1.2337, + "T3_s": 0.9442 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=2.756, \\omega_2=5.093, \\omega_3=6.654 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.355 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 7.142557775895874, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 0.5507409354354038, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.3551944690859368, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 2.5708018879603154, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": 0.540082125071725, + "unit": "", + "description": "" + }, + "du1": { + "value": 0.010754578974718654, + "unit": "", + "description": "" + }, + "du2": { + "value": -0.3605095733303919, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/100/params.json b/scipy/chain_of_springs/100/params.json new file mode 100644 index 0000000000000000000000000000000000000000..501c1927e487e1ebe8ac623264a5dd6600158402 --- /dev/null +++ b/scipy/chain_of_springs/100/params.json @@ -0,0 +1,9 @@ +{ + "k": 7.142557775895874, + "m": 0.5507409354354038, + "b": 0.3551944690859368, + "R": 2.5708018879603154, + "du0": 0.540082125071725, + "du1": 0.010754578974718654, + "du2": -0.3605095733303919 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/100/simulation_code.py b/scipy/chain_of_springs/100/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a27b9f596dcc8460691641450414c59de9f89644 --- /dev/null +++ b/scipy/chain_of_springs/100/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=10.28] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 7.142557775895874, 0.5507409354354038, 0.3551944690859368 +R = 2.5708018879603154 +wall_left, wall_right = 0.0, 10.2832 +x0_0 = 2.5708 + 0.540082125071725 +x1_0 = 5.1416 + 0.010754578974718654 +x2_0 = 7.7124 + -0.3605095733303919 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(2.5708, "#e63946"), (5.1416, "#f4d35e"), (7.7124, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/100/video.mp4 b/scipy/chain_of_springs/100/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..df5fe9adffbc0e8373f32d9f6820743c852bbcb2 --- /dev/null +++ b/scipy/chain_of_springs/100/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9b06992c3824c7dc948289f86552510b5b9bce4fe381fb302b5243d3379bca35 +size 48641 diff --git a/scipy/chain_of_springs/111/cot.json b/scipy/chain_of_springs/111/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..473ea085a52a0e04f48adc6923f7305af4b84704 --- /dev/null +++ b/scipy/chain_of_springs/111/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=1.236 kg each) are connected in a chain by four identical springs (k=9.031 N/m, rest length R=1.766 m) between two fixed walls. Viscous damping b=0.434 kg/s. Initial displacements: u_0=0.305, u_1=-0.030, u_2=0.110 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 4.2806 \\rightarrow \\omega_1 = 2.0690 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 14.6149 \\rightarrow \\omega_2 = 3.8229 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 24.9492 \\rightarrow \\omega_3 = 4.9949 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 2.069, + "omega2_rad_s": 3.8229, + "omega3_rad_s": 4.9949, + "T1_s": 3.0369, + "T2_s": 1.6435, + "T3_s": 1.2579 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=2.069, \\omega_2=3.823, \\omega_3=4.995 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.434 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 9.031190820413416, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 1.2358869107374166, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.43436217505580754, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 1.76554639705907, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": 0.30530437091089413, + "unit": "", + "description": "" + }, + "du1": { + "value": -0.029660070303006436, + "unit": "", + "description": "" + }, + "du2": { + "value": 0.11013808734643582, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/111/params.json b/scipy/chain_of_springs/111/params.json new file mode 100644 index 0000000000000000000000000000000000000000..7f490c26673f156ff368e3ea43768e8d6e6ad686 --- /dev/null +++ b/scipy/chain_of_springs/111/params.json @@ -0,0 +1,9 @@ +{ + "k": 9.031190820413416, + "m": 1.2358869107374166, + "b": 0.43436217505580754, + "R": 1.76554639705907, + "du0": 0.30530437091089413, + "du1": -0.029660070303006436, + "du2": 0.11013808734643582 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/111/simulation_code.py b/scipy/chain_of_springs/111/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..260d645271cad20ebbbbf2de3dd1ea77c0f7c576 --- /dev/null +++ b/scipy/chain_of_springs/111/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=7.06] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 9.031190820413416, 1.2358869107374166, 0.43436217505580754 +R = 1.76554639705907 +wall_left, wall_right = 0.0, 7.0622 +x0_0 = 1.7655 + 0.30530437091089413 +x1_0 = 3.5311 + -0.029660070303006436 +x2_0 = 5.2966 + 0.11013808734643582 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(1.7655, "#e63946"), (3.5311, "#f4d35e"), (5.2966, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/111/video.mp4 b/scipy/chain_of_springs/111/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..634a7eea6913720fc11c288a582e2518ac4e3cd5 --- /dev/null +++ b/scipy/chain_of_springs/111/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:52f59f18ad6ea93c3d6cfeb42fc96cb1bd3bfdfd81c8f949b6f84222a5e9b8c4 +size 53832 diff --git a/scipy/chain_of_springs/194/cot.json b/scipy/chain_of_springs/194/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..b26068a1a2aa1e7cbfe1053f8c440b646042cf5c --- /dev/null +++ b/scipy/chain_of_springs/194/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=1.679 kg each) are connected in a chain by four identical springs (k=7.817 N/m, rest length R=1.406 m) between two fixed walls. Viscous damping b=0.239 kg/s. Initial displacements: u_0=-0.293, u_1=-0.134, u_2=-0.523 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 2.7276 \\rightarrow \\omega_1 = 1.6515 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 9.3125 \\rightarrow \\omega_2 = 3.0516 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 15.8974 \\rightarrow \\omega_3 = 3.9872 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 1.6515, + "omega2_rad_s": 3.0516, + "omega3_rad_s": 3.9872, + "T1_s": 3.8045, + "T2_s": 2.059, + "T3_s": 1.5759 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=1.652, \\omega_2=3.052, \\omega_3=3.987 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.239 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 7.816905236621487, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 1.6787974810892492, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.23875703012225297, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 1.4060880019649913, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": -0.2926210075432519, + "unit": "", + "description": "" + }, + "du1": { + "value": -0.13355541592052111, + "unit": "", + "description": "" + }, + "du2": { + "value": -0.5230614735437414, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/194/params.json b/scipy/chain_of_springs/194/params.json new file mode 100644 index 0000000000000000000000000000000000000000..3fc36ee7629be89dd68cdbfc7caa0fd1a070fa18 --- /dev/null +++ b/scipy/chain_of_springs/194/params.json @@ -0,0 +1,9 @@ +{ + "k": 7.816905236621487, + "m": 1.6787974810892492, + "b": 0.23875703012225297, + "R": 1.4060880019649913, + "du0": -0.2926210075432519, + "du1": -0.13355541592052111, + "du2": -0.5230614735437414 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/194/simulation_code.py b/scipy/chain_of_springs/194/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..76b803a2f3118b5ebd2abcbb409d652977c6c8f1 --- /dev/null +++ b/scipy/chain_of_springs/194/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=5.62] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 7.816905236621487, 1.6787974810892492, 0.23875703012225297 +R = 1.4060880019649913 +wall_left, wall_right = 0.0, 5.6244 +x0_0 = 1.4061 + -0.2926210075432519 +x1_0 = 2.8122 + -0.13355541592052111 +x2_0 = 4.2183 + -0.5230614735437414 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(1.4061, "#e63946"), (2.8122, "#f4d35e"), (4.2183, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/194/video.mp4 b/scipy/chain_of_springs/194/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..2eb39f2b8b863ec8e639780474849fe5fc2d5408 --- /dev/null +++ b/scipy/chain_of_springs/194/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a3900a495eb8accc9882df49c0057cd4826fb1d08a6ad116fabaa59efda650b2 +size 58650 diff --git a/scipy/chain_of_springs/196/cot.json b/scipy/chain_of_springs/196/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..b886ae625ecb39e091b1626f91173cdcb8710d7c --- /dev/null +++ b/scipy/chain_of_springs/196/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=1.061 kg each) are connected in a chain by four identical springs (k=8.821 N/m, rest length R=1.328 m) between two fixed walls. Viscous damping b=0.377 kg/s. Initial displacements: u_0=0.254, u_1=0.132, u_2=0.570 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 4.8721 \\rightarrow \\omega_1 = 2.2073 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 16.6342 \\rightarrow \\omega_2 = 4.0785 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 28.3964 \\rightarrow \\omega_3 = 5.3288 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 2.2073, + "omega2_rad_s": 4.0785, + "omega3_rad_s": 5.3288, + "T1_s": 2.8466, + "T2_s": 1.5406, + "T3_s": 1.1791 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=2.207, \\omega_2=4.079, \\omega_3=5.329 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.377 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 8.821025254512616, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 1.06058634882887, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.37731868782015143, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 1.3279977235208995, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": 0.2543114009296258, + "unit": "", + "description": "" + }, + "du1": { + "value": 0.13206223684768537, + "unit": "", + "description": "" + }, + "du2": { + "value": 0.5697453325191396, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/196/params.json b/scipy/chain_of_springs/196/params.json new file mode 100644 index 0000000000000000000000000000000000000000..6c18faefd8f452bd98110bc6e854d30c14c9042e --- /dev/null +++ b/scipy/chain_of_springs/196/params.json @@ -0,0 +1,9 @@ +{ + "k": 8.821025254512616, + "m": 1.06058634882887, + "b": 0.37731868782015143, + "R": 1.3279977235208995, + "du0": 0.2543114009296258, + "du1": 0.13206223684768537, + "du2": 0.5697453325191396 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/196/simulation_code.py b/scipy/chain_of_springs/196/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..8d55675e3bd6aba88d0739926e8ace4ae76a2703 --- /dev/null +++ b/scipy/chain_of_springs/196/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=5.31] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 8.821025254512616, 1.06058634882887, 0.37731868782015143 +R = 1.3279977235208995 +wall_left, wall_right = 0.0, 5.3120 +x0_0 = 1.3280 + 0.2543114009296258 +x1_0 = 2.6560 + 0.13206223684768537 +x2_0 = 3.9840 + 0.5697453325191396 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(1.3280, "#e63946"), (2.6560, "#f4d35e"), (3.9840, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/196/video.mp4 b/scipy/chain_of_springs/196/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f14e45e0da355097a4ad0871c880ff6c94231e88 --- /dev/null +++ b/scipy/chain_of_springs/196/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c78a68f310ab55ffb5ca01c8758005ebffc609003fc29e00e5e38c312b0febc8 +size 54451 diff --git a/scipy/chain_of_springs/254/cot.json b/scipy/chain_of_springs/254/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..4c440050600c340db0e33a0a3e9d906f3feb8a6e --- /dev/null +++ b/scipy/chain_of_springs/254/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=0.945 kg each) are connected in a chain by four identical springs (k=5.357 N/m, rest length R=2.725 m) between two fixed walls. Viscous damping b=0.263 kg/s. Initial displacements: u_0=0.213, u_1=-0.592, u_2=-0.459 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 3.3199 \\rightarrow \\omega_1 = 1.8221 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 11.3348 \\rightarrow \\omega_2 = 3.3667 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 19.3497 \\rightarrow \\omega_3 = 4.3988 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 1.8221, + "omega2_rad_s": 3.3667, + "omega3_rad_s": 4.3988, + "T1_s": 3.4484, + "T2_s": 1.8663, + "T3_s": 1.4284 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=1.822, \\omega_2=3.367, \\omega_3=4.399 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.263 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 5.357155703221827, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 0.9452595033365179, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.2628416725800478, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 2.7251587511608086, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": 0.21303800532742512, + "unit": "", + "description": "" + }, + "du1": { + "value": -0.5917092141062311, + "unit": "", + "description": "" + }, + "du2": { + "value": -0.45881637194888625, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/254/params.json b/scipy/chain_of_springs/254/params.json new file mode 100644 index 0000000000000000000000000000000000000000..6cfdb24206c51fa51624e61eea469ddeb01b42e1 --- /dev/null +++ b/scipy/chain_of_springs/254/params.json @@ -0,0 +1,9 @@ +{ + "k": 5.357155703221827, + "m": 0.9452595033365179, + "b": 0.2628416725800478, + "R": 2.7251587511608086, + "du0": 0.21303800532742512, + "du1": -0.5917092141062311, + "du2": -0.45881637194888625 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/254/simulation_code.py b/scipy/chain_of_springs/254/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..9b3ba532f173c404363d2aac59b61eee14efd1b5 --- /dev/null +++ b/scipy/chain_of_springs/254/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=10.90] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 5.357155703221827, 0.9452595033365179, 0.2628416725800478 +R = 2.7251587511608086 +wall_left, wall_right = 0.0, 10.9006 +x0_0 = 2.7252 + 0.21303800532742512 +x1_0 = 5.4503 + -0.5917092141062311 +x2_0 = 8.1755 + -0.45881637194888625 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(2.7252, "#e63946"), (5.4503, "#f4d35e"), (8.1755, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/254/video.mp4 b/scipy/chain_of_springs/254/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f195954fd212900dd6b70bed7f5d92db5981a595 --- /dev/null +++ b/scipy/chain_of_springs/254/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0fa74728d8fffbf9ab8e148af4dfe47a9dfdefc01f26a6006df4ddb494a6cf84 +size 47050 diff --git a/scipy/chain_of_springs/347/cot.json b/scipy/chain_of_springs/347/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..8c1f592deb738552a94074151f59f26ccc47b331 --- /dev/null +++ b/scipy/chain_of_springs/347/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=0.576 kg each) are connected in a chain by four identical springs (k=9.026 N/m, rest length R=2.800 m) between two fixed walls. Viscous damping b=0.102 kg/s. Initial displacements: u_0=0.026, u_1=-0.126, u_2=0.128 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 9.1738 \\rightarrow \\omega_1 = 3.0288 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 31.3214 \\rightarrow \\omega_2 = 5.5966 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 53.4690 \\rightarrow \\omega_3 = 7.3122 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 3.0288, + "omega2_rad_s": 5.5966, + "omega3_rad_s": 7.3122, + "T1_s": 2.0745, + "T2_s": 1.1227, + "T3_s": 0.8593 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=3.029, \\omega_2=5.597, \\omega_3=7.312 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.102 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 9.026251117852652, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 0.5763633469328732, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.1023633310101092, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 2.800188344096199, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": 0.026268781348688375, + "unit": "", + "description": "" + }, + "du1": { + "value": -0.12640340278699103, + "unit": "", + "description": "" + }, + "du2": { + "value": 0.1275462388035309, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/347/params.json b/scipy/chain_of_springs/347/params.json new file mode 100644 index 0000000000000000000000000000000000000000..69b4fea77f80ef31f1b65f4124260b5cc0824fcb --- /dev/null +++ b/scipy/chain_of_springs/347/params.json @@ -0,0 +1,9 @@ +{ + "k": 9.026251117852652, + "m": 0.5763633469328732, + "b": 0.1023633310101092, + "R": 2.800188344096199, + "du0": 0.026268781348688375, + "du1": -0.12640340278699103, + "du2": 0.1275462388035309 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/347/simulation_code.py b/scipy/chain_of_springs/347/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..f85fda17b0e50daed74a04bc812d173d90804388 --- /dev/null +++ b/scipy/chain_of_springs/347/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=11.20] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 9.026251117852652, 0.5763633469328732, 0.1023633310101092 +R = 2.800188344096199 +wall_left, wall_right = 0.0, 11.2008 +x0_0 = 2.8002 + 0.026268781348688375 +x1_0 = 5.6004 + -0.12640340278699103 +x2_0 = 8.4006 + 0.1275462388035309 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(2.8002, "#e63946"), (5.6004, "#f4d35e"), (8.4006, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/347/video.mp4 b/scipy/chain_of_springs/347/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..eb5cd7ca9ec26ec4f8e10cfd3966ec2422d15f3b --- /dev/null +++ b/scipy/chain_of_springs/347/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b3c7c688307a746eaacb6fb72c2afdc7d615f9d1127b4abaca003ab72b1edfb8 +size 41647 diff --git a/scipy/chain_of_springs/412/cot.json b/scipy/chain_of_springs/412/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..58cbc96347e3cdd44b75536c5b8844583fd5f603 --- /dev/null +++ b/scipy/chain_of_springs/412/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=0.695 kg each) are connected in a chain by four identical springs (k=3.702 N/m, rest length R=1.765 m) between two fixed walls. Viscous damping b=0.003 kg/s. Initial displacements: u_0=-0.064, u_1=-0.062, u_2=0.200 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 3.1210 \\rightarrow \\omega_1 = 1.7666 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 10.6556 \\rightarrow \\omega_2 = 3.2643 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 18.1903 \\rightarrow \\omega_3 = 4.2650 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 1.7666, + "omega2_rad_s": 3.2643, + "omega3_rad_s": 4.265, + "T1_s": 3.5566, + "T2_s": 1.9248, + "T3_s": 1.4732 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=1.767, \\omega_2=3.264, \\omega_3=4.265 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.003 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 3.7018861149867917, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 0.6948234466853175, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.0026125341726823126, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 1.764559164740392, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": -0.06393508505931733, + "unit": "", + "description": "" + }, + "du1": { + "value": -0.061814289319882754, + "unit": "", + "description": "" + }, + "du2": { + "value": 0.20040527995354496, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/412/params.json b/scipy/chain_of_springs/412/params.json new file mode 100644 index 0000000000000000000000000000000000000000..16c7b60058cddc76f1a869dfd4ccb7852a59d4ee --- /dev/null +++ b/scipy/chain_of_springs/412/params.json @@ -0,0 +1,9 @@ +{ + "k": 3.7018861149867917, + "m": 0.6948234466853175, + "b": 0.0026125341726823126, + "R": 1.764559164740392, + "du0": -0.06393508505931733, + "du1": -0.061814289319882754, + "du2": 0.20040527995354496 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/412/simulation_code.py b/scipy/chain_of_springs/412/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..6c849b75fa9948b3350bb67b34fd26b2acaea00a --- /dev/null +++ b/scipy/chain_of_springs/412/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=7.06] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 3.7018861149867917, 0.6948234466853175, 0.0026125341726823126 +R = 1.764559164740392 +wall_left, wall_right = 0.0, 7.0582 +x0_0 = 1.7646 + -0.06393508505931733 +x1_0 = 3.5291 + -0.061814289319882754 +x2_0 = 5.2937 + 0.20040527995354496 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(1.7646, "#e63946"), (3.5291, "#f4d35e"), (5.2937, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/412/video.mp4 b/scipy/chain_of_springs/412/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..16058f71ca3eb58efbcf6d5123ad3052e140e8cb --- /dev/null +++ b/scipy/chain_of_springs/412/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:578386064638e4840006517225b3adefc550bad1c2c424408833ebf5f09976a3 +size 53449 diff --git a/scipy/chain_of_springs/420/cot.json b/scipy/chain_of_springs/420/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ef8ea20dd59f603ed4ddf193b765b3f0ff7cc4e3 --- /dev/null +++ b/scipy/chain_of_springs/420/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=0.864 kg each) are connected in a chain by four identical springs (k=9.470 N/m, rest length R=1.737 m) between two fixed walls. Viscous damping b=0.499 kg/s. Initial displacements: u_0=0.104, u_1=-0.368, u_2=-0.250 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 6.4213 \\rightarrow \\omega_1 = 2.5340 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 21.9237 \\rightarrow \\omega_2 = 4.6823 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 37.4261 \\rightarrow \\omega_3 = 6.1177 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 2.534, + "omega2_rad_s": 4.6823, + "omega3_rad_s": 6.1177, + "T1_s": 2.4795, + "T2_s": 1.3419, + "T3_s": 1.0271 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=2.534, \\omega_2=4.682, \\omega_3=6.118 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.499 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 9.46987272965333, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 0.8638939811180101, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.49939768737231516, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 1.736725408015911, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": 0.10441339195071564, + "unit": "", + "description": "" + }, + "du1": { + "value": -0.3676538351689085, + "unit": "", + "description": "" + }, + "du2": { + "value": -0.2500251501764384, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/420/params.json b/scipy/chain_of_springs/420/params.json new file mode 100644 index 0000000000000000000000000000000000000000..44ef012f89fab868bc9e1099cefcf8ca784a2540 --- /dev/null +++ b/scipy/chain_of_springs/420/params.json @@ -0,0 +1,9 @@ +{ + "k": 9.46987272965333, + "m": 0.8638939811180101, + "b": 0.49939768737231516, + "R": 1.736725408015911, + "du0": 0.10441339195071564, + "du1": -0.3676538351689085, + "du2": -0.2500251501764384 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/420/simulation_code.py b/scipy/chain_of_springs/420/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..bef26501fb5a7252332301aa33e73eea16384e95 --- /dev/null +++ b/scipy/chain_of_springs/420/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=6.95] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 9.46987272965333, 0.8638939811180101, 0.49939768737231516 +R = 1.736725408015911 +wall_left, wall_right = 0.0, 6.9469 +x0_0 = 1.7367 + 0.10441339195071564 +x1_0 = 3.4735 + -0.3676538351689085 +x2_0 = 5.2102 + -0.2500251501764384 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(1.7367, "#e63946"), (3.4735, "#f4d35e"), (5.2102, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/420/video.mp4 b/scipy/chain_of_springs/420/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..6a98002fc7a405cb5e6ce0583d4fed2a9a51d537 --- /dev/null +++ b/scipy/chain_of_springs/420/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:55c6513abefd480f4ac683fe11d772a4fb1def15d5ad0e61ac6badf18a6e674c +size 50557 diff --git a/scipy/chain_of_springs/433/cot.json b/scipy/chain_of_springs/433/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..26dc2a2d1ccba0c4b5d0861291d5a2f87422c58e --- /dev/null +++ b/scipy/chain_of_springs/433/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=1.894 kg each) are connected in a chain by four identical springs (k=9.683 N/m, rest length R=1.860 m) between two fixed walls. Viscous damping b=0.386 kg/s. Initial displacements: u_0=0.532, u_1=0.346, u_2=-0.560 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 2.9950 \\rightarrow \\omega_1 = 1.7306 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 10.2257 \\rightarrow \\omega_2 = 3.1978 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 17.4563 \\rightarrow \\omega_3 = 4.1781 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 1.7306, + "omega2_rad_s": 3.1978, + "omega3_rad_s": 4.1781, + "T1_s": 3.6306, + "T2_s": 1.9649, + "T3_s": 1.5038 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=1.731, \\omega_2=3.198, \\omega_3=4.178 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.386 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 9.683308931066804, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 1.8939252508957631, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.38565652033348585, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 1.8603443209409771, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": 0.5318507035307775, + "unit": "", + "description": "" + }, + "du1": { + "value": 0.3459271518727748, + "unit": "", + "description": "" + }, + "du2": { + "value": -0.5600651387339685, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/433/params.json b/scipy/chain_of_springs/433/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c8ccc6fd3abb4d16a59ef43e6dd54de0a91a0550 --- /dev/null +++ b/scipy/chain_of_springs/433/params.json @@ -0,0 +1,9 @@ +{ + "k": 9.683308931066804, + "m": 1.8939252508957631, + "b": 0.38565652033348585, + "R": 1.8603443209409771, + "du0": 0.5318507035307775, + "du1": 0.3459271518727748, + "du2": -0.5600651387339685 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/433/simulation_code.py b/scipy/chain_of_springs/433/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..f22d2837663bc8a15f71c76aeda41950c840cd02 --- /dev/null +++ b/scipy/chain_of_springs/433/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=7.44] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 9.683308931066804, 1.8939252508957631, 0.38565652033348585 +R = 1.8603443209409771 +wall_left, wall_right = 0.0, 7.4414 +x0_0 = 1.8603 + 0.5318507035307775 +x1_0 = 3.7207 + 0.3459271518727748 +x2_0 = 5.5810 + -0.5600651387339685 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(1.8603, "#e63946"), (3.7207, "#f4d35e"), (5.5810, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/433/video.mp4 b/scipy/chain_of_springs/433/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..209a04876d9566b72fb1ad5dd7ecb8d9ac3fa66e --- /dev/null +++ b/scipy/chain_of_springs/433/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:45f611972917fa44fb37800685952f15a05e585bd6e63bc76703c3ccfe67ffe1 +size 71816 diff --git a/scipy/chain_of_springs/478/cot.json b/scipy/chain_of_springs/478/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..e278eb9e360c3a97beaa3ea507fbfc748c5597f8 --- /dev/null +++ b/scipy/chain_of_springs/478/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=1.537 kg each) are connected in a chain by four identical springs (k=7.193 N/m, rest length R=2.053 m) between two fixed walls. Viscous damping b=0.305 kg/s. Initial displacements: u_0=0.000, u_1=0.273, u_2=-0.215 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 2.7419 \\rightarrow \\omega_1 = 1.6559 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 9.3614 \\rightarrow \\omega_2 = 3.0596 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 15.9809 \\rightarrow \\omega_3 = 3.9976 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 1.6559, + "omega2_rad_s": 3.0596, + "omega3_rad_s": 3.9976, + "T1_s": 3.7945, + "T2_s": 2.0536, + "T3_s": 1.5717 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=1.656, \\omega_2=3.060, \\omega_3=3.998 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.305 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 7.193145705521999, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 1.5367706453121714, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.3045113602972925, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 2.053442653450329, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": 0.00017550004076083425, + "unit": "", + "description": "" + }, + "du1": { + "value": 0.2725082944289601, + "unit": "", + "description": "" + }, + "du2": { + "value": -0.2150979498496276, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/478/params.json b/scipy/chain_of_springs/478/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b72060cd1e9e9d30851e71265d87d4b757bb0317 --- /dev/null +++ b/scipy/chain_of_springs/478/params.json @@ -0,0 +1,9 @@ +{ + "k": 7.193145705521999, + "m": 1.5367706453121714, + "b": 0.3045113602972925, + "R": 2.053442653450329, + "du0": 0.00017550004076083425, + "du1": 0.2725082944289601, + "du2": -0.2150979498496276 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/478/simulation_code.py b/scipy/chain_of_springs/478/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..ea07bd76c33501670a65d8b61d5684511c66a2b7 --- /dev/null +++ b/scipy/chain_of_springs/478/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=8.21] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 7.193145705521999, 1.5367706453121714, 0.3045113602972925 +R = 2.053442653450329 +wall_left, wall_right = 0.0, 8.2138 +x0_0 = 2.0534 + 0.00017550004076083425 +x1_0 = 4.1069 + 0.2725082944289601 +x2_0 = 6.1603 + -0.2150979498496276 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(2.0534, "#e63946"), (4.1069, "#f4d35e"), (6.1603, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/478/video.mp4 b/scipy/chain_of_springs/478/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e702f1e3f6f177e9784a2bddd084532276a954b3 --- /dev/null +++ b/scipy/chain_of_springs/478/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c1ea94e1aca8cc9c9c58be985c217b9196ba51f484057ad1b033cad044cf8946 +size 54278 diff --git a/scipy/chain_of_springs/661/cot.json b/scipy/chain_of_springs/661/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..bfd7a8add2f8899dbc9c8c217e89fc48dabf36fe --- /dev/null +++ b/scipy/chain_of_springs/661/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=1.614 kg each) are connected in a chain by four identical springs (k=5.933 N/m, rest length R=2.516 m) between two fixed walls. Viscous damping b=0.225 kg/s. Initial displacements: u_0=-0.460, u_1=-0.095, u_2=0.401 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 2.1527 \\rightarrow \\omega_1 = 1.4672 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 7.3498 \\rightarrow \\omega_2 = 2.7110 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 12.5468 \\rightarrow \\omega_3 = 3.5422 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 1.4672, + "omega2_rad_s": 2.711, + "omega3_rad_s": 3.5422, + "T1_s": 4.2824, + "T2_s": 2.3176, + "T3_s": 1.7738 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=1.467, \\omega_2=2.711, \\omega_3=3.542 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.225 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 5.932783061022569, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 1.6144144043789086, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.22489593717931783, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 2.5157988668697797, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": -0.4597054995911857, + "unit": "", + "description": "" + }, + "du1": { + "value": -0.09525983410583339, + "unit": "", + "description": "" + }, + "du2": { + "value": 0.40129015672007873, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/661/params.json b/scipy/chain_of_springs/661/params.json new file mode 100644 index 0000000000000000000000000000000000000000..3533354fcd0585dd9c3203b5154139bbfb4fb7dd --- /dev/null +++ b/scipy/chain_of_springs/661/params.json @@ -0,0 +1,9 @@ +{ + "k": 5.932783061022569, + "m": 1.6144144043789086, + "b": 0.22489593717931783, + "R": 2.5157988668697797, + "du0": -0.4597054995911857, + "du1": -0.09525983410583339, + "du2": 0.40129015672007873 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/661/simulation_code.py b/scipy/chain_of_springs/661/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..efb65729e28e6c8e7f125d5525b00cfccc013d8b --- /dev/null +++ b/scipy/chain_of_springs/661/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=10.06] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 5.932783061022569, 1.6144144043789086, 0.22489593717931783 +R = 2.5157988668697797 +wall_left, wall_right = 0.0, 10.0632 +x0_0 = 2.5158 + -0.4597054995911857 +x1_0 = 5.0316 + -0.09525983410583339 +x2_0 = 7.5474 + 0.40129015672007873 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(2.5158, "#e63946"), (5.0316, "#f4d35e"), (7.5474, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/661/video.mp4 b/scipy/chain_of_springs/661/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..311f48b7b0e03eb34e6897db3fcf8a19f548e65e --- /dev/null +++ b/scipy/chain_of_springs/661/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8ee7a6e7cd3050d70790435d8a977e4840fde85f4b91d107ec0cea3e0bee3975 +size 52991 diff --git a/scipy/chain_of_springs/669/cot.json b/scipy/chain_of_springs/669/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..0a9c7908068a841a2c98e454c167730e3444f24d --- /dev/null +++ b/scipy/chain_of_springs/669/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=0.612 kg each) are connected in a chain by four identical springs (k=5.593 N/m, rest length R=2.625 m) between two fixed walls. Viscous damping b=0.093 kg/s. Initial displacements: u_0=0.301, u_1=0.250, u_2=0.343 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 5.3533 \\rightarrow \\omega_1 = 2.3137 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 18.2773 \\rightarrow \\omega_2 = 4.2752 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 31.2012 \\rightarrow \\omega_3 = 5.5858 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 2.3137, + "omega2_rad_s": 4.2752, + "omega3_rad_s": 5.5858, + "T1_s": 2.7156, + "T2_s": 1.4697, + "T3_s": 1.1248 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=2.314, \\omega_2=4.275, \\omega_3=5.586 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.093 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 5.593411060494143, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 0.612062312555326, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.09329974415903836, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 2.625081381504821, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": 0.3010259525226834, + "unit": "", + "description": "" + }, + "du1": { + "value": 0.24973964669925852, + "unit": "", + "description": "" + }, + "du2": { + "value": 0.34282762124079813, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/669/params.json b/scipy/chain_of_springs/669/params.json new file mode 100644 index 0000000000000000000000000000000000000000..43a4e170bdcd2e60e0152b5cdc0f68e0d38d722a --- /dev/null +++ b/scipy/chain_of_springs/669/params.json @@ -0,0 +1,9 @@ +{ + "k": 5.593411060494143, + "m": 0.612062312555326, + "b": 0.09329974415903836, + "R": 2.625081381504821, + "du0": 0.3010259525226834, + "du1": 0.24973964669925852, + "du2": 0.34282762124079813 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/669/simulation_code.py b/scipy/chain_of_springs/669/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..15ab5af38edac387e035272c14615b6225adf0ec --- /dev/null +++ b/scipy/chain_of_springs/669/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=10.50] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 5.593411060494143, 0.612062312555326, 0.09329974415903836 +R = 2.625081381504821 +wall_left, wall_right = 0.0, 10.5003 +x0_0 = 2.6251 + 0.3010259525226834 +x1_0 = 5.2502 + 0.24973964669925852 +x2_0 = 7.8752 + 0.34282762124079813 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(2.6251, "#e63946"), (5.2502, "#f4d35e"), (7.8752, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/669/video.mp4 b/scipy/chain_of_springs/669/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3fc9b4016a8e9be1563476bed6f0fe69ea00806a --- /dev/null +++ b/scipy/chain_of_springs/669/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e0d0f6d7555a7a1d749e221ab37e56f0ba65780c441c10e832c6cce1c2390e62 +size 43909 diff --git a/scipy/chain_of_springs/689/cot.json b/scipy/chain_of_springs/689/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..896701cd5306c373a5f82fef6784db36e34e83aa --- /dev/null +++ b/scipy/chain_of_springs/689/cot.json @@ -0,0 +1,110 @@ +{ + "simulation": "chain_of_springs", + "physical_observation": "Three identical blocks (mass m=1.872 kg each) are connected in a chain by four identical springs (k=7.616 N/m, rest length R=2.062 m) between two fixed walls. Viscous damping b=0.419 kg/s. Initial displacements: u_0=-0.128, u_1=0.041, u_2=0.183 m from equilibrium.", + "physical_law": { + "name": "Newton's 2nd Law for Coupled Oscillators --- Normal Mode Analysis", + "statement": "N coupled oscillators possess N normal modes. For a uniform chain of N blocks and (N+1) springs with equal endpoints, the eigenfrequencies follow \\omega_n^2 = (2k/m)(1 - \\cos(n\\pi/(N+1))).", + "formula": "M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, K = k \\cdot tridiag(-1, 2, -1) + boundary terms" + }, + "ode_system": { + "state_variables": [ + "u_0", + "v_0=\\dot{u}_0", + "u_1", + "v_1=\\dot{u}_1", + "u_2", + "v_2=\\dot{u}_2" + ], + "equations_latex": [ + "\\dot{u}_0 = v_0", + "\\dot{v}_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b \\cdot v_0] / m (spring to left wall + spring to block 1)", + "\\dot{u}_1 = v_1", + "\\dot{v}_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b \\cdot v_1] / m", + "\\dot{u}_2 = v_2", + "\\dot{v}_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b \\cdot v_2] / m (spring to block 1 + spring to right wall)" + ], + "equations_python": [ + "u_dot_0 = v_0", + "v_dot_0 = [-k(u_0 + R - wall_1) + k(u_1 - u_0) - b * v_0] / m (spring to left wall + spring to block 1)", + "u_dot_1 = v_1", + "v_dot_1 = [-k(u_1 - u_0) + k(u_2 - u_1) - b * v_1] / m", + "u_dot_2 = v_2", + "v_dot_2 = [-k(u_2 - u_1) + k(wall_2 - u_2 - R) - b * v_2] / m (spring to block 1 + spring to right wall)" + ] + }, + "derivation_steps": [ + "Label blocks 0,1,2 at equilibrium positions x_eq_i = (i+1) \\cdot R (walls at x=0 and x=4R).", + "Displacements from equilibrium: u_i = x_i - x_eq_i.", + "Spring stretches in terms of displacements (linearized about equilibrium):", + " s_0 = u_0 [spring between wall and block 0]", + " s_1 = u_1 - u_0 [spring between block 0 and block 1]", + " s_2 = u_2 - u_1 [spring between block 1 and block 2]", + " s_3 = -u_2 [spring between block 2 and wall]", + "Newton's law: m \\cdot \\ddot{u}_i = -b \\cdot \\dot{u}_i + (spring force from right) - (spring force from left).", + "Matrix form: M \\cdot \\ddot{u} = -K \\cdot u - B \\cdot \\dot{u}, with K = k \\cdot [[2,-1,0],[-1,2,-1],[0,-1,2]].", + "Eigenvalues of K/m give the squared eigenfrequencies:", + " \\omega_1^2 = (2- \\sqrt 2)k/m = 2.3826 \\rightarrow \\omega_1 = 1.5436 rad/s (in-phase mode)", + " \\omega_2^2 = 2k/m = 8.1346 \\rightarrow \\omega_2 = 2.8521 rad/s (alternating mode)", + " \\omega_3^2 = (2+ \\sqrt 2)k/m = 13.8867 \\rightarrow \\omega_3 = 3.7265 rad/s (anti-phase mode)" + ], + "physical_constants_derived": { + "omega1_rad_s": 1.5436, + "omega2_rad_s": 2.8521, + "omega3_rad_s": 3.7265, + "T1_s": 4.0706, + "T2_s": 2.203, + "T3_s": 1.6861 + }, + "numerical_method": { + "solver": "RK45 (Dormand-Prince, 4th/5th order Runge-Kutta)", + "tolerance": "rtol=1e-9, atol=1e-11", + "justification": "Coupled linear ODEs; RK45 is robust and matches myphysicslab TS solver." + }, + "code_validation": { + "ode_source": "Newton's 2nd Law for each block; matches ChainOfSpringsSim.ts evaluate()", + "energy_check": "E = \\Sigma( \\tfrac{1}{2} m \\cdot v_i^2) + \\Sigma( \\tfrac{1}{2} k \\cdot s_j^2) decreases monotonically when b>0", + "physical_plausibility": true + }, + "expected_behavior": [ + "Three normal modes with frequencies \\omega_1=1.544, \\omega_2=2.852, \\omega_3=3.726 rad/s.", + "Beat patterns visible when initial conditions excite multiple modes simultaneously.", + "With damping b=0.419 kg/s, all modes decay; highest frequency mode (\\omega_3) decays fastest." + ], + "parameters": { + "k": { + "value": 7.615746763648469, + "unit": "N/m", + "description": "" + }, + "m": { + "value": 1.8724302331151743, + "unit": "kg", + "description": "" + }, + "b": { + "value": 0.41868832651987586, + "unit": "kg/s", + "description": "" + }, + "R": { + "value": 2.0620823747967374, + "unit": "m", + "description": "Spring rest length" + }, + "du0": { + "value": -0.12779155038397638, + "unit": "", + "description": "" + }, + "du1": { + "value": 0.040882919711374255, + "unit": "", + "description": "" + }, + "du2": { + "value": 0.18327829254138506, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/chain_of_springs/689/params.json b/scipy/chain_of_springs/689/params.json new file mode 100644 index 0000000000000000000000000000000000000000..926384848a571e01d940b873e7b888880a425574 --- /dev/null +++ b/scipy/chain_of_springs/689/params.json @@ -0,0 +1,9 @@ +{ + "k": 7.615746763648469, + "m": 1.8724302331151743, + "b": 0.41868832651987586, + "R": 2.0620823747967374, + "du0": -0.12779155038397638, + "du1": 0.040882919711374255, + "du2": 0.18327829254138506 +} \ No newline at end of file diff --git a/scipy/chain_of_springs/689/simulation_code.py b/scipy/chain_of_springs/689/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..58d95c8839d3c7364a721f6fd69cccfd41777377 --- /dev/null +++ b/scipy/chain_of_springs/689/simulation_code.py @@ -0,0 +1,88 @@ +""" +Chain of Springs (N=3 masses) — auto-generated simulation script. +Physics (Newton's 2nd Law for each mass, horizontal, all same k/m/b): + Stretches: + s0 = x0 - 0 - R (left wall -> mass 0) + s1 = x1 - x0 - R (mass 0 -> mass 1) + s2 = x2 - x1 - R (mass 1 -> mass 2) + s3 = wr - x2 - R (mass 2 -> right wall) + Accelerations: + dv0/dt = (-k*s0 + k*s1 - b*v0) / m + dv1/dt = (-k*s1 + k*s2 - b*v1) / m + dv2/dt = (-k*s2 + k*s3 - b*v2) / m +Layout: [wall=0] ~k~ [m0] ~k~ [m1] ~k~ [m2] ~k~ [wall=8.25] +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +k, m, b = 7.615746763648469, 1.8724302331151743, 0.41868832651987586 +R = 2.0620823747967374 +wall_left, wall_right = 0.0, 8.2483 +x0_0 = 2.0621 + -0.12779155038397638 +x1_0 = 4.1242 + 0.040882919711374255 +x2_0 = 6.1862 + 0.18327829254138506 +duration = 12 + +def ode(t, y): + x0, v0, x1, v1, x2, v2 = y + s0 = x0 - wall_left - R + s1 = x1 - x0 - R + s2 = x2 - x1 - R + s3 = wall_right - x2 - R + dv0 = (-k*s0 + k*s1 - b*v0) / m + dv1 = (-k*s1 + k*s2 - b*v1) / m + dv2 = (-k*s2 + k*s3 - b*v2) / m + return [v0, dv0, v1, dv1, v2, dv2] + +fps = 30 +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), [x0_0, 0.0, x1_0, 0.0, x2_0, 0.0], + t_eval=t_eval, method="RK45", rtol=1e-8, atol=1e-10) +x0a, x1a, x2a = sol.y[0], sol.y[2], sol.y[4] + +bw, bh, ty = 0.3, 0.35, 0.0 +fig, ax = plt.subplots(figsize=(12, 3), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(wall_left - 0.4, wall_right + 0.4) +ax.set_ylim(-1, 1) +ax.set_aspect("equal") +ax.axis("off") +ax.axhline(ty - bh / 2, color="#4a4e69", lw=1.5, zorder=1) + +for wx, flip in [(wall_left, False), (wall_right, True)]: + rx = wx - 0.15 if flip else wx + ax.add_patch(patches.Rectangle((rx, ty - 0.6), 0.15, 1.2, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +def sp_path(xa, xb, n=5, r=0.18): + t = np.linspace(0, 1, n * 20 + 2) + margin = 0.08 + env = np.where((t < margin) | (t > 1 - margin), 0, + np.sin(n * 2 * np.pi * (t - margin) / (1 - 2 * margin))) + return xa + t * (xb - xa), ty + r * env + +spl = [ax.plot([], [], "-", color="#a8dadc", lw=2.5, zorder=3)[0] for _ in range(4)] +bps = [] +for xeq, col in [(2.0621, "#e63946"), (4.1242, "#f4d35e"), (6.1862, "#06d6a0")]: + bp = patches.FancyBboxPatch((xeq - bw / 2, ty - bh / 2), bw, bh, + boxstyle="round,pad=0.02", lw=1.5, edgecolor="white", facecolor=col) + ax.add_patch(bp) + bps.append(bp) + +def update(f): + bx = [x0a[f], x1a[f], x2a[f]] + for i, bp in enumerate(bps): + bp.set_x(bx[i] - bw / 2) + anchors = [wall_left + 0.15] + [bx[i] + bw / 2 for i in range(3)] + ends = [bx[i] - bw / 2 for i in range(3)] + [wall_right] + for i, sp in enumerate(spl): + sp.set_data(*sp_path(anchors[i], ends[i])) + return spl + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), interval=1000/fps, blit=False) +ani.save("chain_of_springs.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close() +print("Saved chain_of_springs.mp4") diff --git a/scipy/chain_of_springs/689/video.mp4 b/scipy/chain_of_springs/689/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..ac4cdb89daa92b929657cc704dbda65e078b8799 --- /dev/null +++ b/scipy/chain_of_springs/689/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5e52bdc62ae749f8de06fe63d2a9cfd90d8e4ed1dca04d78767768408ab47c04 +size 44460 diff --git a/scipy/colliding_blocks/015/cot.json b/scipy/colliding_blocks/015/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..204c50462f2b0217b73c976f3f1a5d1594468629 --- /dev/null +++ b/scipy/colliding_blocks/015/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.323 m) at rest on a frictionless surface sits at x1_0=1.148 m to the right of a vertical wall at x=0. A larger block (m2=15.344 kg, side s2=0.655 m) starts at x2_0=4.145 m moving toward the wall with v2_0=-1.511 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 15.344.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -23.1797 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 17.5082 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 12 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 15.344106869961553, + "initial_kinetic_energy_J": 17.50823225305394, + "initial_momentum_kg_m_s": -23.179654302640813, + "predicted_total_collisions_if_elastic": 12 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 12.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.3228388088616917, + "unit": "", + "description": "" + }, + "m2": { + "value": 15.344106869961553, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.6549405591895919, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.148196311160672, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 4.144523572700276, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -1.5106551654706308, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/015/params.json b/scipy/colliding_blocks/015/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e197c136dcdd8a5f9a1ad52cfa1cbacc4194439a --- /dev/null +++ b/scipy/colliding_blocks/015/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.3228388088616917, + "m2": 15.344106869961553, + "s2": 0.6549405591895919, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.148196311160672, + "v1_0": 0.0, + "x2_0": 4.144523572700276, + "v2_0": -1.5106551654706308 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/015/simulation_code.py b/scipy/colliding_blocks/015/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..3f3aa323e516c5b34535318d3e8e726a6149acd5 --- /dev/null +++ b/scipy/colliding_blocks/015/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.3228388088616917 +m2, s2 = 15.344106869961553, 0.6549405591895919 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.148196311160672, 0.0 +x2_0, v2_0 = 4.144523572700276, -1.5106551654706308 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/015/video.mp4 b/scipy/colliding_blocks/015/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..5893995f83fbcd89fbbfaf0c6423959eebbbd443 --- /dev/null +++ b/scipy/colliding_blocks/015/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7b2018daf4e924688291eb93b01192688478bdc8b6a30a79a6568f5fae827400 +size 11332 diff --git a/scipy/colliding_blocks/051/cot.json b/scipy/colliding_blocks/051/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..5c192f2724a6c44bc01b8eb9f3d0631d0d3016ec --- /dev/null +++ b/scipy/colliding_blocks/051/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.321 m) at rest on a frictionless surface sits at x1_0=1.493 m to the right of a vertical wall at x=0. A larger block (m2=37.674 kg, side s2=0.597 m) starts at x2_0=4.203 m moving toward the wall with v2_0=-2.163 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 37.674.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -81.4833 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 88.1189 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 19 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 37.67370952985927, + "initial_kinetic_energy_J": 88.11889512786479, + "initial_momentum_kg_m_s": -81.48331926399796, + "predicted_total_collisions_if_elastic": 19 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 19.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.3214545751678557, + "unit": "", + "description": "" + }, + "m2": { + "value": 37.67370952985927, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.5973210303540054, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.4928815710047936, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 4.20292594875826, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -2.1628695522912667, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/051/params.json b/scipy/colliding_blocks/051/params.json new file mode 100644 index 0000000000000000000000000000000000000000..1dce671ab1afd5d64f191317374c845f1edbe076 --- /dev/null +++ b/scipy/colliding_blocks/051/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.3214545751678557, + "m2": 37.67370952985927, + "s2": 0.5973210303540054, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.4928815710047936, + "v1_0": 0.0, + "x2_0": 4.20292594875826, + "v2_0": -2.1628695522912667 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/051/simulation_code.py b/scipy/colliding_blocks/051/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c9c8d0c3eb1ca68e059c5b8625f69bc43de19bb6 --- /dev/null +++ b/scipy/colliding_blocks/051/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.3214545751678557 +m2, s2 = 37.67370952985927, 0.5973210303540054 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.4928815710047936, 0.0 +x2_0, v2_0 = 4.20292594875826, -2.1628695522912667 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/051/video.mp4 b/scipy/colliding_blocks/051/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f3a2154c2c37a05b90b6b7fde7bc8a5a8b2b8b2a --- /dev/null +++ b/scipy/colliding_blocks/051/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3684d2cf30d76f3a9529c2282ab7fa099bb7cdf77fbbc68e5dd0e24064044996 +size 8805 diff --git a/scipy/colliding_blocks/077/cot.json b/scipy/colliding_blocks/077/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..fff3812b2d3dfad054b874ef240aff2efbb77f54 --- /dev/null +++ b/scipy/colliding_blocks/077/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.362 m) at rest on a frictionless surface sits at x1_0=1.379 m to the right of a vertical wall at x=0. A larger block (m2=63.436 kg, side s2=0.675 m) starts at x2_0=4.655 m moving toward the wall with v2_0=-1.571 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 63.436.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -99.6656 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 78.2935 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 25 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 63.43593986970327, + "initial_kinetic_energy_J": 78.29347850426227, + "initial_momentum_kg_m_s": -99.66564497946409, + "predicted_total_collisions_if_elastic": 25 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 25.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.36230852656127865, + "unit": "", + "description": "" + }, + "m2": { + "value": 63.43593986970327, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.6748405931317438, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.3790152470784285, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 4.654865501998935, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -1.5711226977037975, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/077/params.json b/scipy/colliding_blocks/077/params.json new file mode 100644 index 0000000000000000000000000000000000000000..f7559275002bbf0ac40f93685badff9a6fdd6576 --- /dev/null +++ b/scipy/colliding_blocks/077/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.36230852656127865, + "m2": 63.43593986970327, + "s2": 0.6748405931317438, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.3790152470784285, + "v1_0": 0.0, + "x2_0": 4.654865501998935, + "v2_0": -1.5711226977037975 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/077/simulation_code.py b/scipy/colliding_blocks/077/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..7992c79a897cd295930864608b44acd91ef4cbc2 --- /dev/null +++ b/scipy/colliding_blocks/077/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.36230852656127865 +m2, s2 = 63.43593986970327, 0.6748405931317438 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.3790152470784285, 0.0 +x2_0, v2_0 = 4.654865501998935, -1.5711226977037975 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/077/video.mp4 b/scipy/colliding_blocks/077/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..dd2183aefcff76dc8aa082f24f173bedd4755ea4 --- /dev/null +++ b/scipy/colliding_blocks/077/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:416c7e9a81b5dccf3fa516b6f45585198b6bc4aaf6e8b4d7f3441e350ac426db +size 10705 diff --git a/scipy/colliding_blocks/100/cot.json b/scipy/colliding_blocks/100/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..e1e81f7863724a5de39032dac43a6f54428e0d87 --- /dev/null +++ b/scipy/colliding_blocks/100/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.288 m) at rest on a frictionless surface sits at x1_0=1.434 m to the right of a vertical wall at x=0. A larger block (m2=41.042 kg, side s2=0.725 m) starts at x2_0=3.820 m moving toward the wall with v2_0=-1.816 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 41.042.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -74.5124 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 67.6400 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 20 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 41.0415105209839, + "initial_kinetic_energy_J": 67.64004545221955, + "initial_momentum_kg_m_s": -74.5124102021549, + "predicted_total_collisions_if_elastic": 20 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 20.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.2877265922624717, + "unit": "", + "description": "" + }, + "m2": { + "value": 41.0415105209839, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.7254762577368563, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.4336778174102147, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 3.8202685651470114, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -1.8155377142870468, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/100/params.json b/scipy/colliding_blocks/100/params.json new file mode 100644 index 0000000000000000000000000000000000000000..94c340bc9a0576ea8eff2770cf8c8fad34f1e5fd --- /dev/null +++ b/scipy/colliding_blocks/100/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.2877265922624717, + "m2": 41.0415105209839, + "s2": 0.7254762577368563, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.4336778174102147, + "v1_0": 0.0, + "x2_0": 3.8202685651470114, + "v2_0": -1.8155377142870468 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/100/simulation_code.py b/scipy/colliding_blocks/100/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c3b80ca4829523f87bc46d808c5b6c403d4ca097 --- /dev/null +++ b/scipy/colliding_blocks/100/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.2877265922624717 +m2, s2 = 41.0415105209839, 0.7254762577368563 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.4336778174102147, 0.0 +x2_0, v2_0 = 3.8202685651470114, -1.8155377142870468 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/100/video.mp4 b/scipy/colliding_blocks/100/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..65a4c8bb22f4d546d92325fe326f59b8236ef29d --- /dev/null +++ b/scipy/colliding_blocks/100/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:88337cca0013d5a52361d9bca5efa9422addab3a2758117a1a5b4ad066325d0f +size 10425 diff --git a/scipy/colliding_blocks/143/cot.json b/scipy/colliding_blocks/143/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..50ac2db79fc97f05e6ac2e72fa11eebaaa306dee --- /dev/null +++ b/scipy/colliding_blocks/143/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.322 m) at rest on a frictionless surface sits at x1_0=1.317 m to the right of a vertical wall at x=0. A larger block (m2=50.028 kg, side s2=0.641 m) starts at x2_0=4.824 m moving toward the wall with v2_0=-2.206 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 50.028.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -110.3504 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 121.7048 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 22 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 50.0276331653308, + "initial_kinetic_energy_J": 121.7048452209341, + "initial_momentum_kg_m_s": -110.35039964727136, + "predicted_total_collisions_if_elastic": 22 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 22.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.32215277029980693, + "unit": "", + "description": "" + }, + "m2": { + "value": 50.0276331653308, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.6408807155552635, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.317402660604855, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 4.824115932134961, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -2.2057889343392785, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/143/params.json b/scipy/colliding_blocks/143/params.json new file mode 100644 index 0000000000000000000000000000000000000000..43b900a267ba0df89f2b182225a34cfb4b188b05 --- /dev/null +++ b/scipy/colliding_blocks/143/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.32215277029980693, + "m2": 50.0276331653308, + "s2": 0.6408807155552635, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.317402660604855, + "v1_0": 0.0, + "x2_0": 4.824115932134961, + "v2_0": -2.2057889343392785 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/143/simulation_code.py b/scipy/colliding_blocks/143/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0b240f43db1b7ae4c83d7e3badb3d766554e323d --- /dev/null +++ b/scipy/colliding_blocks/143/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.32215277029980693 +m2, s2 = 50.0276331653308, 0.6408807155552635 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.317402660604855, 0.0 +x2_0, v2_0 = 4.824115932134961, -2.2057889343392785 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/143/video.mp4 b/scipy/colliding_blocks/143/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8588ecc6ee1d4c3b78dd786a37627f474a68f442 --- /dev/null +++ b/scipy/colliding_blocks/143/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cffae0425e581936240add010160420642feba455fe7822b1e403b78e172658d +size 10788 diff --git a/scipy/colliding_blocks/187/cot.json b/scipy/colliding_blocks/187/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..aeef34dee60d5daeb18b8ccef25545d15c0e46c8 --- /dev/null +++ b/scipy/colliding_blocks/187/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.271 m) at rest on a frictionless surface sits at x1_0=1.017 m to the right of a vertical wall at x=0. A larger block (m2=61.059 kg, side s2=0.842 m) starts at x2_0=4.254 m moving toward the wall with v2_0=-1.681 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 61.059.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -102.6274 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 86.2475 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 24 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 61.059043230583335, + "initial_kinetic_energy_J": 86.24747222750857, + "initial_momentum_kg_m_s": -102.62736608982988, + "predicted_total_collisions_if_elastic": 24 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 24.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.270630905191185, + "unit": "", + "description": "" + }, + "m2": { + "value": 61.059043230583335, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.8420091882206717, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.0173702234360096, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 4.254190267057343, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -1.6807889652359593, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/187/params.json b/scipy/colliding_blocks/187/params.json new file mode 100644 index 0000000000000000000000000000000000000000..94ef0e334add9d25de6c61a4af25e86bf2366ade --- /dev/null +++ b/scipy/colliding_blocks/187/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.270630905191185, + "m2": 61.059043230583335, + "s2": 0.8420091882206717, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.0173702234360096, + "v1_0": 0.0, + "x2_0": 4.254190267057343, + "v2_0": -1.6807889652359593 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/187/simulation_code.py b/scipy/colliding_blocks/187/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c3fae36637e5fd297f88e258838a7d6cb523d27b --- /dev/null +++ b/scipy/colliding_blocks/187/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.270630905191185 +m2, s2 = 61.059043230583335, 0.8420091882206717 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.0173702234360096, 0.0 +x2_0, v2_0 = 4.254190267057343, -1.6807889652359593 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/187/video.mp4 b/scipy/colliding_blocks/187/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..dd44980229bd7d86055c2fd37da9fee0233af753 --- /dev/null +++ b/scipy/colliding_blocks/187/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:26aad9cb4da0adba43468a7c2fa1f58a4ad039c34786146d7d0e375d4fe03d1a +size 11123 diff --git a/scipy/colliding_blocks/255/cot.json b/scipy/colliding_blocks/255/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..9f56c609044c24d9dfd3ae7ed071b0cae9cae2e4 --- /dev/null +++ b/scipy/colliding_blocks/255/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.362 m) at rest on a frictionless surface sits at x1_0=1.442 m to the right of a vertical wall at x=0. A larger block (m2=36.937 kg, side s2=0.757 m) starts at x2_0=4.970 m moving toward the wall with v2_0=-1.897 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 36.937.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -70.0751 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 66.4709 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 19 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 36.9373625451433, + "initial_kinetic_energy_J": 66.47088346693, + "initial_momentum_kg_m_s": -70.07509002939582, + "predicted_total_collisions_if_elastic": 19 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 19.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.36229702541966763, + "unit": "", + "description": "" + }, + "m2": { + "value": 36.9373625451433, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.7569950288637503, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.442068710536012, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 4.969809773545386, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -1.8971330165696856, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/255/params.json b/scipy/colliding_blocks/255/params.json new file mode 100644 index 0000000000000000000000000000000000000000..85509e479c040e3bd1c71b33fea7f6b96d091c49 --- /dev/null +++ b/scipy/colliding_blocks/255/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.36229702541966763, + "m2": 36.9373625451433, + "s2": 0.7569950288637503, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.442068710536012, + "v1_0": 0.0, + "x2_0": 4.969809773545386, + "v2_0": -1.8971330165696856 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/255/simulation_code.py b/scipy/colliding_blocks/255/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..32f3e0104618781b0fdd798a7ecd51a8673225ec --- /dev/null +++ b/scipy/colliding_blocks/255/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.36229702541966763 +m2, s2 = 36.9373625451433, 0.7569950288637503 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.442068710536012, 0.0 +x2_0, v2_0 = 4.969809773545386, -1.8971330165696856 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/255/video.mp4 b/scipy/colliding_blocks/255/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d3d882a3e70dd3942551a9d1ffbf19789f8f9d25 --- /dev/null +++ b/scipy/colliding_blocks/255/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:acacb7343b45d0575b2e8b3d23779dbab36446f46400ed315e0800a1d11746b9 +size 10040 diff --git a/scipy/colliding_blocks/285/cot.json b/scipy/colliding_blocks/285/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..02fcc598d450ad7bbef020a79cfbab72f3d80b94 --- /dev/null +++ b/scipy/colliding_blocks/285/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.252 m) at rest on a frictionless surface sits at x1_0=1.136 m to the right of a vertical wall at x=0. A larger block (m2=56.563 kg, side s2=0.721 m) starts at x2_0=3.789 m moving toward the wall with v2_0=-1.141 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 56.563.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -64.5201 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 36.7984 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 23 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 56.56268863716174, + "initial_kinetic_energy_J": 36.79844034973382, + "initial_momentum_kg_m_s": -64.52005461614495, + "predicted_total_collisions_if_elastic": 23 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 23.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.2516400076997305, + "unit": "", + "description": "" + }, + "m2": { + "value": 56.56268863716174, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.7208740522337481, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.1357350975964868, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 3.788537806129847, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -1.1406822442622575, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/285/params.json b/scipy/colliding_blocks/285/params.json new file mode 100644 index 0000000000000000000000000000000000000000..8ee0f7a7cd81d22640a615a6840b555ee90e6615 --- /dev/null +++ b/scipy/colliding_blocks/285/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.2516400076997305, + "m2": 56.56268863716174, + "s2": 0.7208740522337481, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.1357350975964868, + "v1_0": 0.0, + "x2_0": 3.788537806129847, + "v2_0": -1.1406822442622575 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/285/simulation_code.py b/scipy/colliding_blocks/285/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..da208af3d7e98f479d0af6fbcaaceeb5379a394a --- /dev/null +++ b/scipy/colliding_blocks/285/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.2516400076997305 +m2, s2 = 56.56268863716174, 0.7208740522337481 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.1357350975964868, 0.0 +x2_0, v2_0 = 3.788537806129847, -1.1406822442622575 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/285/video.mp4 b/scipy/colliding_blocks/285/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..fec015916f3c866c0eb2c4cad9940228bdd87040 --- /dev/null +++ b/scipy/colliding_blocks/285/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:43f539b6ed5bf500e8346a4555249e3890f2c33263635657c27d20b96eecdf4b +size 11456 diff --git a/scipy/colliding_blocks/432/cot.json b/scipy/colliding_blocks/432/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..40c0a3a1e2e98be26ab0fdefcb57645e75297160 --- /dev/null +++ b/scipy/colliding_blocks/432/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.266 m) at rest on a frictionless surface sits at x1_0=1.159 m to the right of a vertical wall at x=0. A larger block (m2=58.000 kg, side s2=0.771 m) starts at x2_0=4.284 m moving toward the wall with v2_0=-2.034 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 58.000.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -117.9929 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 120.0198 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 24 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 58.00014842211161, + "initial_kinetic_energy_J": 120.01981961935087, + "initial_momentum_kg_m_s": -117.99294344593166, + "predicted_total_collisions_if_elastic": 24 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 24.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.2662966032332943, + "unit": "", + "description": "" + }, + "m2": { + "value": 58.00014842211161, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.7710160699790172, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.1591220007958494, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 4.283633768535863, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -2.0343558879747414, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/432/params.json b/scipy/colliding_blocks/432/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5804db7d10732331fdf15626b2a2a60d1f62bde7 --- /dev/null +++ b/scipy/colliding_blocks/432/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.2662966032332943, + "m2": 58.00014842211161, + "s2": 0.7710160699790172, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.1591220007958494, + "v1_0": 0.0, + "x2_0": 4.283633768535863, + "v2_0": -2.0343558879747414 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/432/simulation_code.py b/scipy/colliding_blocks/432/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..99496d586f660229b68ef2982566e744b07a65fc --- /dev/null +++ b/scipy/colliding_blocks/432/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.2662966032332943 +m2, s2 = 58.00014842211161, 0.7710160699790172 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.1591220007958494, 0.0 +x2_0, v2_0 = 4.283633768535863, -2.0343558879747414 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/432/video.mp4 b/scipy/colliding_blocks/432/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..185513d2923a42f53b85fe878021e7844dc528ae --- /dev/null +++ b/scipy/colliding_blocks/432/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4b5095277c2122c911a2709e4b2c69eb41aaca7662f7a66d3efad142034efdd9 +size 10436 diff --git a/scipy/colliding_blocks/453/cot.json b/scipy/colliding_blocks/453/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..17e04c705e18183bd18f215ae60d70ec480f37ea --- /dev/null +++ b/scipy/colliding_blocks/453/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.326 m) at rest on a frictionless surface sits at x1_0=1.498 m to the right of a vertical wall at x=0. A larger block (m2=23.196 kg, side s2=0.586 m) starts at x2_0=3.618 m moving toward the wall with v2_0=-2.166 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 23.196.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -50.2512 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 54.4320 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 15 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 23.195760797858924, + "initial_kinetic_energy_J": 54.43195830122476, + "initial_momentum_kg_m_s": -50.25118276248313, + "predicted_total_collisions_if_elastic": 15 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 15.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.32613118757264553, + "unit": "", + "description": "" + }, + "m2": { + "value": 23.195760797858924, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.5857287052362874, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.4981479260919004, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 3.6178050309340737, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -2.1663951098823864, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/453/params.json b/scipy/colliding_blocks/453/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5b34e851f55957acc430f077fb73f6e82ff9c44c --- /dev/null +++ b/scipy/colliding_blocks/453/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.32613118757264553, + "m2": 23.195760797858924, + "s2": 0.5857287052362874, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.4981479260919004, + "v1_0": 0.0, + "x2_0": 3.6178050309340737, + "v2_0": -2.1663951098823864 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/453/simulation_code.py b/scipy/colliding_blocks/453/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..664fd17eaab6946b0dcf932f87646f4764c66126 --- /dev/null +++ b/scipy/colliding_blocks/453/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.32613118757264553 +m2, s2 = 23.195760797858924, 0.5857287052362874 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.4981479260919004, 0.0 +x2_0, v2_0 = 3.6178050309340737, -2.1663951098823864 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/453/video.mp4 b/scipy/colliding_blocks/453/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e6be7b99b38e995061cf9bf334f07e539bbb812f --- /dev/null +++ b/scipy/colliding_blocks/453/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:be93d0027f27a276314957f40d19bce39995a4960cbabf792c315efaf5ec895b +size 8317 diff --git a/scipy/colliding_blocks/459/cot.json b/scipy/colliding_blocks/459/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..c2b36564d0b822d14621c530e489f701baab94b7 --- /dev/null +++ b/scipy/colliding_blocks/459/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.313 m) at rest on a frictionless surface sits at x1_0=1.464 m to the right of a vertical wall at x=0. A larger block (m2=12.250 kg, side s2=0.705 m) starts at x2_0=4.787 m moving toward the wall with v2_0=-2.064 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 12.250.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -25.2888 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 26.1029 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 11 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 12.250034207084253, + "initial_kinetic_energy_J": 26.1028896239531, + "initial_momentum_kg_m_s": -25.28878371124915, + "predicted_total_collisions_if_elastic": 11 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 11.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.3127396084414354, + "unit": "", + "description": "" + }, + "m2": { + "value": 12.250034207084253, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.7051677548679903, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.4643021933674338, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 4.7872758141998215, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -2.0643847424217414, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/459/params.json b/scipy/colliding_blocks/459/params.json new file mode 100644 index 0000000000000000000000000000000000000000..ed49cc6fd31ea3274e0875090428e184a048256c --- /dev/null +++ b/scipy/colliding_blocks/459/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.3127396084414354, + "m2": 12.250034207084253, + "s2": 0.7051677548679903, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.4643021933674338, + "v1_0": 0.0, + "x2_0": 4.7872758141998215, + "v2_0": -2.0643847424217414 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/459/simulation_code.py b/scipy/colliding_blocks/459/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..9bc1dbe4ca53f337626d54582d12cdcda4c73481 --- /dev/null +++ b/scipy/colliding_blocks/459/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.3127396084414354 +m2, s2 = 12.250034207084253, 0.7051677548679903 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.4643021933674338, 0.0 +x2_0, v2_0 = 4.7872758141998215, -2.0643847424217414 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/459/video.mp4 b/scipy/colliding_blocks/459/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..78f1e0c4a6aba974e097d440bf766d48230c15a3 --- /dev/null +++ b/scipy/colliding_blocks/459/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0a8804aeb669883b24aa4536ea906fba918fd26f55bbcfa66af910a1550ca1d3 +size 8799 diff --git a/scipy/colliding_blocks/473/cot.json b/scipy/colliding_blocks/473/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..0dfd90ababc57818bb213b80966d48052019c99f --- /dev/null +++ b/scipy/colliding_blocks/473/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.372 m) at rest on a frictionless surface sits at x1_0=1.462 m to the right of a vertical wall at x=0. A larger block (m2=27.701 kg, side s2=0.593 m) starts at x2_0=4.536 m moving toward the wall with v2_0=-1.267 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 27.701.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -35.1082 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 22.2484 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 16 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 27.700609354697114, + "initial_kinetic_energy_J": 22.248365368142025, + "initial_momentum_kg_m_s": -35.10821208331391, + "predicted_total_collisions_if_elastic": 16 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 16.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.37176261982147607, + "unit": "", + "description": "" + }, + "m2": { + "value": 27.700609354697114, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.5930808661075885, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.4618563357404466, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 4.5363457585140665, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -1.267416598449691, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/473/params.json b/scipy/colliding_blocks/473/params.json new file mode 100644 index 0000000000000000000000000000000000000000..00804d1d764d282780bede2c55d2cfeb3a3777b2 --- /dev/null +++ b/scipy/colliding_blocks/473/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.37176261982147607, + "m2": 27.700609354697114, + "s2": 0.5930808661075885, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.4618563357404466, + "v1_0": 0.0, + "x2_0": 4.5363457585140665, + "v2_0": -1.267416598449691 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/473/simulation_code.py b/scipy/colliding_blocks/473/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..613110dfe08efd84837fdf17a09bc7b12afbe6c6 --- /dev/null +++ b/scipy/colliding_blocks/473/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.37176261982147607 +m2, s2 = 27.700609354697114, 0.5930808661075885 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.4618563357404466, 0.0 +x2_0, v2_0 = 4.5363457585140665, -1.267416598449691 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/473/video.mp4 b/scipy/colliding_blocks/473/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..4e9d610d2c2797ddb1ca249366d1bb6efa871c71 --- /dev/null +++ b/scipy/colliding_blocks/473/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:99497864fa0672caa5f1ebffc8a5abf16fd8528e49f2bd2c71f9c58347ca6e39 +size 12247 diff --git a/scipy/colliding_blocks/549/cot.json b/scipy/colliding_blocks/549/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..0e5837051ce3e22d3c947b8781bca6ca1d32c767 --- /dev/null +++ b/scipy/colliding_blocks/549/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.329 m) at rest on a frictionless surface sits at x1_0=1.235 m to the right of a vertical wall at x=0. A larger block (m2=36.223 kg, side s2=0.596 m) starts at x2_0=4.903 m moving toward the wall with v2_0=-1.441 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 36.223.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -52.1960 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 37.6065 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 19 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 36.22279331627194, + "initial_kinetic_energy_J": 37.60647993101407, + "initial_momentum_kg_m_s": -52.19601038189895, + "predicted_total_collisions_if_elastic": 19 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 19.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.3290548195684527, + "unit": "", + "description": "" + }, + "m2": { + "value": 36.22279331627194, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.595898728739314, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.2352922733209628, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 4.902857797777839, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -1.4409714327153105, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/549/params.json b/scipy/colliding_blocks/549/params.json new file mode 100644 index 0000000000000000000000000000000000000000..78cd253c66f2bf27de06a3c67b82e7ab48345510 --- /dev/null +++ b/scipy/colliding_blocks/549/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.3290548195684527, + "m2": 36.22279331627194, + "s2": 0.595898728739314, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.2352922733209628, + "v1_0": 0.0, + "x2_0": 4.902857797777839, + "v2_0": -1.4409714327153105 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/549/simulation_code.py b/scipy/colliding_blocks/549/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..2d7810a0448f9614885d929bae70c05368abfd80 --- /dev/null +++ b/scipy/colliding_blocks/549/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.3290548195684527 +m2, s2 = 36.22279331627194, 0.595898728739314 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.2352922733209628, 0.0 +x2_0, v2_0 = 4.902857797777839, -1.4409714327153105 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/549/video.mp4 b/scipy/colliding_blocks/549/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..de9b7ebb31860d8b20078419a968502292591f8b --- /dev/null +++ b/scipy/colliding_blocks/549/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:226ce18f0c07e9d4182a291ee0370b3792902371d11db53d4e8760bf87ef7f3e +size 10839 diff --git a/scipy/colliding_blocks/553/cot.json b/scipy/colliding_blocks/553/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..c9c9e26b3918aba0193bebf4b7e3e991f6f479fc --- /dev/null +++ b/scipy/colliding_blocks/553/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.324 m) at rest on a frictionless surface sits at x1_0=1.093 m to the right of a vertical wall at x=0. A larger block (m2=20.732 kg, side s2=0.628 m) starts at x2_0=3.631 m moving toward the wall with v2_0=-2.240 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 20.732.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -46.4483 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 52.0315 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 14 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 20.732092365991246, + "initial_kinetic_energy_J": 52.03146668474119, + "initial_momentum_kg_m_s": -46.448276033584946, + "predicted_total_collisions_if_elastic": 14 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 14.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.324221201227167, + "unit": "", + "description": "" + }, + "m2": { + "value": 20.732092365991246, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.6281487091175849, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.0926176571507293, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 3.6314028034910355, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -2.2404046448190784, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/553/params.json b/scipy/colliding_blocks/553/params.json new file mode 100644 index 0000000000000000000000000000000000000000..8fc62a6562e21f535d09f7a9a97a1e4d0860f541 --- /dev/null +++ b/scipy/colliding_blocks/553/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.324221201227167, + "m2": 20.732092365991246, + "s2": 0.6281487091175849, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.0926176571507293, + "v1_0": 0.0, + "x2_0": 3.6314028034910355, + "v2_0": -2.2404046448190784 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/553/simulation_code.py b/scipy/colliding_blocks/553/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..1d92251eb2a7a77bf64fb6a3a48de55feeab6d55 --- /dev/null +++ b/scipy/colliding_blocks/553/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.324221201227167 +m2, s2 = 20.732092365991246, 0.6281487091175849 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.0926176571507293, 0.0 +x2_0, v2_0 = 3.6314028034910355, -2.2404046448190784 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/553/video.mp4 b/scipy/colliding_blocks/553/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7a5e451232e6e1636f1e2a6ae76074a5044bc760 --- /dev/null +++ b/scipy/colliding_blocks/553/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:26aa33f8fb3f5913b7ddd3b4445088f5e0c0fb95c76b62e74c62c11e16ceaf53 +size 10004 diff --git a/scipy/colliding_blocks/563/cot.json b/scipy/colliding_blocks/563/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..94907181ad81a165e5566c190523fac2d9ad18bb --- /dev/null +++ b/scipy/colliding_blocks/563/cot.json @@ -0,0 +1,114 @@ +{ + "simulation": "colliding_blocks", + "physical_observation": "A small block (m1=1.000 kg, side s1=0.275 m) at rest on a frictionless surface sits at x1_0=1.110 m to the right of a vertical wall at x=0. A larger block (m2=12.780 kg, side s2=0.605 m) starts at x2_0=4.172 m moving toward the wall with v2_0=-2.387 m/s. Wall restitution e_w=1.0; block-block restitution e_b=1.0. Mass ratio m2/m1 = 12.780.", + "physical_law": { + "name": "Conservation of momentum and energy at impulsive contacts (Newton restitution)", + "statement": "Between contacts, free motion (no friction). Block-wall and block-block collisions are instantaneous impulses governed by Newton restitution.", + "formula": "wall: v1 -> -e_w v1. block-block: v1' = ((m1 - e_b m2) v1 + (1+e_b) m2 v2)/(m1+m2), v2' = ((m2 - e_b m1) v2 + (1+e_b) m1 v1)/(m1+m2)." + }, + "ode_system": { + "state_variables": [ + "x1 (m)", + "v1 (m/s)", + "x2 (m)", + "v2 (m/s)" + ], + "equations_latex": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 \\cdot v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 \\cdot m2) \\cdot v1 + (1+1.0) \\cdot m2 \\cdot v2)/(m1+m2), ((m2 - 1.0 \\cdot m1) \\cdot v2 + (1+1.0) \\cdot m1 \\cdot v1)/(m1+m2)" + ], + "equations_python": [ + "dx1/dt = v1; dv1/dt = 0", + "dx2/dt = v2; dv2/dt = 0", + "# wall event: x1 - s1/2 = 0 with v1<0", + "v1 <- -1.0 * v1", + "# block-block event: (x2 - s2/2) - (x1 + s1/2) = 0", + "v1, v2 <- ((m1 - 1.0 * m2) * v1 + (1+1.0) * m2 * v2)/(m1+m2), ((m2 - 1.0 * m1) * v2 + (1+1.0) * m1 * v1)/(m1+m2)" + ] + }, + "derivation_steps": [ + "Initial momentum: m1 v1_0 + m2 v2_0 = -30.5077 kg m/s.", + "Initial kinetic energy: KE0 = (1/2) m1 v1_0^2 + (1/2) m2 v2_0^2 = 36.4129 J.", + "Free motion between events: each block coasts at constant velocity.", + "Wall event (v1 -> -1.0 v1) flips small-block velocity (with restitution).", + "Block-block event applies the 1D Newton-restitution collision laws (above).", + "For elastic e=1, total KE and momentum (between contacts) are exactly conserved; total collision count is N = floor(pi / \\atan(\\sqrt(m1/m2))) = 11 (Galperin 2003)." + ], + "physical_constants_derived": { + "mass_ratio_m2_over_m1": 12.78010721003496, + "initial_kinetic_energy_J": 36.41286602257871, + "initial_momentum_kg_m_s": -30.50771481423003, + "predicted_total_collisions_if_elastic": 11 + }, + "numerical_method": { + "solver": "Hybrid free-flight RK45 (scipy.solve_ivp) with two terminal events (wall, block-block); analytic impulse jumps applied at each contact.", + "tolerance": "rtol=1e-9, atol=1e-11; max_step=0.05 s.", + "justification": "Velocities have impulsive jumps; analytic event handling avoids near-discontinuous RHS issues." + }, + "code_validation": { + "ode_source": "1D Newton-restitution collisions; standard textbook formulas.", + "energy_check": "For e_w=e_b=1, sum (1/2 m_i v_i^2) is conserved exactly between contacts.", + "physical_plausibility": true + }, + "expected_behavior": [ + "Large block approaches, hits small block, which bounces between large block and wall.", + "Predicted collisions (elastic): 11.", + "Eventually small block is moving rightward faster than large block and they separate; large block recoils toward +x." + ], + "parameters": { + "m1": { + "value": 1.0, + "unit": "", + "description": "" + }, + "s1": { + "value": 0.27499431405179775, + "unit": "", + "description": "" + }, + "m2": { + "value": 12.78010721003496, + "unit": "", + "description": "" + }, + "s2": { + "value": 0.6053129682847612, + "unit": "", + "description": "" + }, + "e_w": { + "value": 1.0, + "unit": "", + "description": "" + }, + "e_b": { + "value": 1.0, + "unit": "", + "description": "" + }, + "x1_0": { + "value": 1.1103494157229288, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "x2_0": { + "value": 4.172260178014513, + "unit": "", + "description": "" + }, + "v2_0": { + "value": -2.3871251087999736, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/colliding_blocks/563/params.json b/scipy/colliding_blocks/563/params.json new file mode 100644 index 0000000000000000000000000000000000000000..29bc4290d74c9ab26fefa2aa38c4dab0b543416c --- /dev/null +++ b/scipy/colliding_blocks/563/params.json @@ -0,0 +1,12 @@ +{ + "m1": 1.0, + "s1": 0.27499431405179775, + "m2": 12.78010721003496, + "s2": 0.6053129682847612, + "e_w": 1.0, + "e_b": 1.0, + "x1_0": 1.1103494157229288, + "v1_0": 0.0, + "x2_0": 4.172260178014513, + "v2_0": -2.3871251087999736 +} \ No newline at end of file diff --git a/scipy/colliding_blocks/563/simulation_code.py b/scipy/colliding_blocks/563/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..a897b9488787eba848aa70dbfe44fb1ba11259c8 --- /dev/null +++ b/scipy/colliding_blocks/563/simulation_code.py @@ -0,0 +1,89 @@ +""" +Colliding Blocks Against a Wall — auto-generated. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, s1 = 1.0, 0.27499431405179775 +m2, s2 = 12.78010721003496, 0.6053129682847612 +e_w = 1.0 +e_b = 1.0 +x1_0, v1_0 = 1.1103494157229288, 0.0 +x2_0, v2_0 = 4.172260178014513, -2.3871251087999736 +duration = 12 +fps = 30 + +def free(t, y): return [y[1], 0.0, y[3], 0.0] +def ev_wall(t, y): return y[0] - s1 / 2.0 +ev_wall.terminal = True; ev_wall.direction = -1 +def ev_block(t, y): return (y[2] - s2 / 2.0) - (y[0] + s1 / 2.0) +ev_block.terminal = True; ev_block.direction = -1 + +t_eval_g = np.linspace(0, duration, int(duration * fps) + 1) +S = np.zeros((4, len(t_eval_g))) +state = np.array([x1_0, v1_0, x2_0, v2_0], dtype=float) +S[:, 0] = state +last_idx = 1 +t_cur = 0.0 +n_collisions = 0 + +for _seg in range(2000): + if t_cur >= duration - 1e-9: break + sol = solve_ivp(free, (t_cur, duration), state, + events=[ev_wall, ev_block], dense_output=True, + max_step=0.05, rtol=1e-9, atol=1e-11) + seg_end = sol.t[-1] + while last_idx < len(t_eval_g) and t_eval_g[last_idx] <= seg_end + 1e-12: + S[:, last_idx] = sol.sol(t_eval_g[last_idx]); last_idx += 1 + state = sol.y[:, -1].copy(); t_cur = seg_end + if sol.status != 1: break + t_w, t_b = sol.t_events[0], sol.t_events[1] + is_wall = len(t_w) > 0 and (len(t_b) == 0 or t_w[-1] >= t_b[-1]) + if is_wall: + state[1] = -e_w * state[1] + else: + v1, v2 = state[1], state[3] + state[1] = ((m1 - e_b*m2)*v1 + (1+e_b)*m2*v2) / (m1 + m2) + state[3] = ((m2 - e_b*m1)*v2 + (1+e_b)*m1*v1) / (m1 + m2) + n_collisions += 1 + +if last_idx < len(t_eval_g): + S[:, last_idx:] = state[:, None] + +x1_arr, x2_arr = S[0], S[2] + +fig_w, fig_h = 10, 4 +fig_aspect = fig_w / fig_h +x_lo_d, x_hi_d = -0.55, (x2_0 + s2/2) + 0.50 +y_lo_d, y_hi_d = -0.30, max(s1, s2) + 0.50 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx / dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx / fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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.add_patch(patches.Rectangle((x_lo, y_lo), -x_lo, y_hi - y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.add_patch(patches.Rectangle((0, y_lo), x_hi, -y_lo, facecolor="#4a4e69", edgecolor="none", alpha=0.55, zorder=0)) +ax.plot([0, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=1) +ax.plot([0, 0], [y_lo, y_hi], color="#adb5bd", lw=2.5, zorder=1) + +b1 = patches.Rectangle((x1_0 - s1/2, 0), s1, s1, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=3) +b2 = patches.Rectangle((x2_0 - s2/2, 0), s2, s2, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=3) +ax.add_patch(b1); ax.add_patch(b2) + +def update(f): + b1.set_x(x1_arr[f] - s1/2); b2.set_x(x2_arr[f] - s2/2) + return b1, b2 + +ani = animation.FuncAnimation(fig, update, frames=len(t_eval_g), interval=1000/fps, blit=True) +ani.save("colliding_blocks.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved colliding_blocks.mp4") diff --git a/scipy/colliding_blocks/563/video.mp4 b/scipy/colliding_blocks/563/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..54735e8d791c11c88bc581f1e20fe50e55195f98 --- /dev/null +++ b/scipy/colliding_blocks/563/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c2ca0dab2d5cd93d81740d44cacf8a2a06ecb4b450374c585e439221464d99c2 +size 9008 diff --git a/scipy/complex_collision_plane/008/cot.json b/scipy/complex_collision_plane/008/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..b451521b86fd28cb7f624b31ac65dd776ea396c1 --- /dev/null +++ b/scipy/complex_collision_plane/008/cot.json @@ -0,0 +1,201 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 5 rigid bodies are placed and given initial velocities. Composition: 1 sphere(s), 3 block(s), 1 block(s) with attached spring bumper(s), and 0 diatomic spring-pair(s). Newton's restitution e=0.423 is enforced on every hard contact. Total initial momentum p_0=4.2035 kg \\cdot m/s, initial kinetic energy KE_0=5.6324 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "x_5", + "v_1", + "v_2", + "v_3", + "v_4", + "v_5" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{x}_5 = v_5", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4", + "\\dot{v}_5 = (1/m_5) \\cdot \\SigmaF_5" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "x_dot_5 = v_5", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4", + "v_dot_5 = (1/m_5) * SigmaF_5" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 5; total state dim = 2M = 10.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.4232 \\Rightarrow \\zeta = 0.2640 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.423", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-0.667, -0.305, 0.062, 0.354, 0.655] m and velocities v_0 = [-1.314, 1.391, 0.55, 0.372, 1.921] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = +4.2035 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 5.6324 J.", + "With e = 0.423, hard collisions retain a fraction e^2 = 0.179 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "1 attached-spring bumper(s): during contact the spring stores PE = \\tfrac{1}{2} k_a \\cdot compression^2 and converts it back to KE, providing a smooth (non-impulsive) momentum exchange whose duration depends on \\sqrt (m_eff / k_a).", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 5):", + " body[0]: kind=sphere, m=1.403 kg, half_size=0.106 m, x0=-0.667 m, v0=-1.314 m/s", + " body[1]: kind=block, m=1.753 kg, half_size=0.157 m, x0=-0.305 m, v0=+1.391 m/s", + " body[2]: kind=block, m=0.472 kg, half_size=0.110 m, x0=0.062 m, v0=+0.550 m/s", + " body[3]: kind=block, m=1.951 kg, half_size=0.083 m, x0=0.354 m, v0=+0.372 m/s", + " body[4]: kind=spring_a, m=1.367 kg, half_size=0.118 m, x0=0.655 m, v0=+1.921 m/s", + "Springs:", + " spring_a on body[4] side=+x: k=74.65 N/m, L_natural=0.182 m" + ], + "parameters": { + "setup_seed": { + "value": 495101.5014690795, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.4231680254534449, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "sphere", + "block", + "block", + "block", + "spring_a" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.4030781061202864, + 1.752607745563831, + 0.4718029898248285, + 1.9513761133544139, + 1.366545785726945 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.10553730359754815, + 0.15654931803649622, + 0.1099782319555529, + 0.08262755612873957, + 0.11764467896594691 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -0.6667997850867358, + -0.3047131634526914, + 0.06181438653935789, + 0.3544201746236504, + 0.6546924097183369 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + -1.313742184748579, + 1.39051806167058, + 0.5496599050702091, + 0.37178310741159803, + 1.9208802412554382 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [ + { + "body_idx": 4, + "k": 74.65249210096519, + "L": 0.18222102746484314, + "side": 1 + } + ], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/008/params.json b/scipy/complex_collision_plane/008/params.json new file mode 100644 index 0000000000000000000000000000000000000000..01271b9bc6a32d213d14eeb9b975266aa06ebed3 --- /dev/null +++ b/scipy/complex_collision_plane/008/params.json @@ -0,0 +1,51 @@ +{ + "setup_seed": 495101.5014690795, + "restitution": 0.4231680254534449, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "sphere", + "block", + "block", + "block", + "spring_a" + ], + "masses": [ + 1.4030781061202864, + 1.752607745563831, + 0.4718029898248285, + 1.9513761133544139, + 1.366545785726945 + ], + "half_sizes": [ + 0.10553730359754815, + 0.15654931803649622, + 0.1099782319555529, + 0.08262755612873957, + 0.11764467896594691 + ], + "x0": [ + -0.6667997850867358, + -0.3047131634526914, + 0.06181438653935789, + 0.3544201746236504, + 0.6546924097183369 + ], + "v0": [ + -1.313742184748579, + 1.39051806167058, + 0.5496599050702091, + 0.37178310741159803, + 1.9208802412554382 + ], + "spring_a": [ + { + "body_idx": 4, + "k": 74.65249210096519, + "L": 0.18222102746484314, + "side": 1 + } + ], + "spring_pair": [] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/008/simulation_code.py b/scipy/complex_collision_plane/008/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..6bac9eaeae42d4c9893a844f11f386be4e3f7921 --- /dev/null +++ b/scipy/complex_collision_plane/008/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.4232 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.4231680254534449 +k_contact = 5000.0 +body_kinds = ['sphere', 'block', 'block', 'block', 'spring_a'] +masses = [1.4030781061202864, 1.752607745563831, 0.4718029898248285, 1.9513761133544139, 1.366545785726945] +half_sizes = [0.10553730359754815, 0.15654931803649622, 0.1099782319555529, 0.08262755612873957, 0.11764467896594691] +x0_list = [-0.6667997850867358, -0.3047131634526914, 0.06181438653935789, 0.3544201746236504, 0.6546924097183369] +v0_list = [-1.313742184748579, 1.39051806167058, 0.5496599050702091, 0.37178310741159803, 1.9208802412554382] +spring_a = [{'body_idx': 4, 'k': 74.65249210096519, 'L': 0.18222102746484314, 'side': 1}] +spring_pair = [] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/008/video.mp4 b/scipy/complex_collision_plane/008/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..cf4f15108ba1b1d354b0d42562e61c0a0e94d2ba --- /dev/null +++ b/scipy/complex_collision_plane/008/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dfbca80d2eb755ccee1c6de60adbf623203795eba38a5bd07b67f5475a392950 +size 14036 diff --git a/scipy/complex_collision_plane/1127/cot.json b/scipy/complex_collision_plane/1127/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..70525b2060c62a9ae991e1556fed79499a4c52a9 --- /dev/null +++ b/scipy/complex_collision_plane/1127/cot.json @@ -0,0 +1,189 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 4 rigid bodies are placed and given initial velocities. Composition: 2 sphere(s), 1 block(s), 1 block(s) with attached spring bumper(s), and 0 diatomic spring-pair(s). Newton's restitution e=0.978 is enforced on every hard contact. Total initial momentum p_0=-6.5076 kg \\cdot m/s, initial kinetic energy KE_0=5.5677 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "v_1", + "v_2", + "v_3", + "v_4" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 4; total state dim = 2M = 8.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.9783 \\Rightarrow \\zeta = 0.0070 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.978", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-0.537, -0.243, 0.04, 0.488] m and velocities v_0 = [-0.083, -2.043, -0.994, -1.58] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = -6.5076 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 5.5677 J.", + "With e = 0.978, hard collisions retain a fraction e^2 = 0.957 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "1 attached-spring bumper(s): during contact the spring stores PE = \\tfrac{1}{2} k_a \\cdot compression^2 and converts it back to KE, providing a smooth (non-impulsive) momentum exchange whose duration depends on \\sqrt (m_eff / k_a).", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 4):", + " body[0]: kind=sphere, m=1.939 kg, half_size=0.077 m, x0=-0.537 m, v0=-0.083 m/s", + " body[1]: kind=block, m=1.420 kg, half_size=0.116 m, x0=-0.243 m, v0=-2.043 m/s", + " body[2]: kind=sphere, m=0.429 kg, half_size=0.067 m, x0=0.040 m, v0=-0.994 m/s", + " body[3]: kind=spring_a, m=1.911 kg, half_size=0.126 m, x0=0.488 m, v0=-1.580 m/s", + "Springs:", + " spring_a on body[3] side=-x: k=45.19 N/m, L_natural=0.156 m" + ], + "parameters": { + "setup_seed": { + "value": 967400.3295677855, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.9782620100815713, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "sphere", + "block", + "sphere", + "spring_a" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.9386031903931977, + 1.4197956068652515, + 0.429263854047583, + 1.91089526119102 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.07743190772286368, + 0.11639612633509103, + 0.06678041448499353, + 0.12557820741043418 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -0.5365070095589375, + -0.24267897550098239, + 0.040497565319102424, + 0.48836070987136804 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + -0.08284777156874235, + -2.0431489512970864, + -0.9937703541278204, + -1.580163475048653 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [ + { + "body_idx": 3, + "k": 45.194872102467684, + "L": 0.15550452265683787, + "side": -1 + } + ], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/1127/params.json b/scipy/complex_collision_plane/1127/params.json new file mode 100644 index 0000000000000000000000000000000000000000..9553a09aec72a784ce03a7c4fd96d4d7fac15b93 --- /dev/null +++ b/scipy/complex_collision_plane/1127/params.json @@ -0,0 +1,46 @@ +{ + "setup_seed": 967400.3295677855, + "restitution": 0.9782620100815713, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "sphere", + "block", + "sphere", + "spring_a" + ], + "masses": [ + 1.9386031903931977, + 1.4197956068652515, + 0.429263854047583, + 1.91089526119102 + ], + "half_sizes": [ + 0.07743190772286368, + 0.11639612633509103, + 0.06678041448499353, + 0.12557820741043418 + ], + "x0": [ + -0.5365070095589375, + -0.24267897550098239, + 0.040497565319102424, + 0.48836070987136804 + ], + "v0": [ + -0.08284777156874235, + -2.0431489512970864, + -0.9937703541278204, + -1.580163475048653 + ], + "spring_a": [ + { + "body_idx": 3, + "k": 45.194872102467684, + "L": 0.15550452265683787, + "side": -1 + } + ], + "spring_pair": [] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/1127/simulation_code.py b/scipy/complex_collision_plane/1127/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..6a6872f3af69a0f55b004487ba5ce0450b316ec5 --- /dev/null +++ b/scipy/complex_collision_plane/1127/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.9783 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.9782620100815713 +k_contact = 5000.0 +body_kinds = ['sphere', 'block', 'sphere', 'spring_a'] +masses = [1.9386031903931977, 1.4197956068652515, 0.429263854047583, 1.91089526119102] +half_sizes = [0.07743190772286368, 0.11639612633509103, 0.06678041448499353, 0.12557820741043418] +x0_list = [-0.5365070095589375, -0.24267897550098239, 0.040497565319102424, 0.48836070987136804] +v0_list = [-0.08284777156874235, -2.0431489512970864, -0.9937703541278204, -1.580163475048653] +spring_a = [{'body_idx': 3, 'k': 45.194872102467684, 'L': 0.15550452265683787, 'side': -1}] +spring_pair = [] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/1127/video.mp4 b/scipy/complex_collision_plane/1127/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1d612ee0176006cfe432b0ee1cd5e6ed5e6b8a37 --- /dev/null +++ b/scipy/complex_collision_plane/1127/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d48973c0b0108e094e6856669f68b5832de086722fb96b574ec4a78d35c37b40 +size 13700 diff --git a/scipy/complex_collision_plane/1256/cot.json b/scipy/complex_collision_plane/1256/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..bb680cb31650c06ed5b198430da0ee883be9482f --- /dev/null +++ b/scipy/complex_collision_plane/1256/cot.json @@ -0,0 +1,155 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 2 rigid bodies are placed and given initial velocities. Composition: 1 sphere(s), 1 block(s), 0 block(s) with attached spring bumper(s), and 0 diatomic spring-pair(s). Newton's restitution e=0.787 is enforced on every hard contact. Total initial momentum p_0=3.3075 kg \\cdot m/s, initial kinetic energy KE_0=3.6000 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "v_1", + "v_2" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 2; total state dim = 2M = 4.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.7866 \\Rightarrow \\zeta = 0.0762 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.787", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-0.181, 0.154] m and velocities v_0 = [2.402, 0.343] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = +3.3075 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 3.6000 J.", + "With e = 0.787, hard collisions retain a fraction e^2 = 0.619 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 2):", + " body[0]: kind=sphere, m=1.226 kg, half_size=0.104 m, x0=-0.181 m, v0=+2.402 m/s", + " body[1]: kind=block, m=1.054 kg, half_size=0.131 m, x0=0.154 m, v0=+0.343 m/s" + ], + "parameters": { + "setup_seed": { + "value": 828037.8472598152, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.7865910191443772, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "sphere", + "block" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.226285031324944, + 1.053774756577289 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.10437843747863276, + 0.13062952283843882 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -0.18062952283843892, + 0.15437843747863278 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + 2.4021109479932896, + 0.3433683649745465 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/1256/params.json b/scipy/complex_collision_plane/1256/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e5ccbde7e304ff249c95e02f6fddb6ca04fd7a46 --- /dev/null +++ b/scipy/complex_collision_plane/1256/params.json @@ -0,0 +1,29 @@ +{ + "setup_seed": 828037.8472598152, + "restitution": 0.7865910191443772, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "sphere", + "block" + ], + "masses": [ + 1.226285031324944, + 1.053774756577289 + ], + "half_sizes": [ + 0.10437843747863276, + 0.13062952283843882 + ], + "x0": [ + -0.18062952283843892, + 0.15437843747863278 + ], + "v0": [ + 2.4021109479932896, + 0.3433683649745465 + ], + "spring_a": [], + "spring_pair": [] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/1256/simulation_code.py b/scipy/complex_collision_plane/1256/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..7bfb3ec7d4232c0fc0f60fea8ea73497ffcf1fd2 --- /dev/null +++ b/scipy/complex_collision_plane/1256/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.7866 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.7865910191443772 +k_contact = 5000.0 +body_kinds = ['sphere', 'block'] +masses = [1.226285031324944, 1.053774756577289] +half_sizes = [0.10437843747863276, 0.13062952283843882] +x0_list = [-0.18062952283843892, 0.15437843747863278] +v0_list = [2.4021109479932896, 0.3433683649745465] +spring_a = [] +spring_pair = [] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/1256/video.mp4 b/scipy/complex_collision_plane/1256/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..20c610e0035b959ab48905aeb0d565224386af4f --- /dev/null +++ b/scipy/complex_collision_plane/1256/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5c667d3d9a1ae5aa2e378cb3153453d6c3ac2306ef5179f539005e58d7cfc7f3 +size 9715 diff --git a/scipy/complex_collision_plane/1521/cot.json b/scipy/complex_collision_plane/1521/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..a00eeefa6a827ed7cddc723e4a2f095d9a38d10a --- /dev/null +++ b/scipy/complex_collision_plane/1521/cot.json @@ -0,0 +1,260 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 8 rigid bodies are placed and given initial velocities. Composition: 1 sphere(s), 1 block(s), 2 block(s) with attached spring bumper(s), and 2 diatomic spring-pair(s). Newton's restitution e=0.954 is enforced on every hard contact. Total initial momentum p_0=0.7896 kg \\cdot m/s, initial kinetic energy KE_0=1.6225 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "x_5", + "x_6", + "x_7", + "x_8", + "v_1", + "v_2", + "v_3", + "v_4", + "v_5", + "v_6", + "v_7", + "v_8" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{x}_5 = v_5", + "\\dot{x}_6 = v_6", + "\\dot{x}_7 = v_7", + "\\dot{x}_8 = v_8", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4", + "\\dot{v}_5 = (1/m_5) \\cdot \\SigmaF_5", + "\\dot{v}_6 = (1/m_6) \\cdot \\SigmaF_6", + "\\dot{v}_7 = (1/m_7) \\cdot \\SigmaF_7", + "\\dot{v}_8 = (1/m_8) \\cdot \\SigmaF_8" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "x_dot_5 = v_5", + "x_dot_6 = v_6", + "x_dot_7 = v_7", + "x_dot_8 = v_8", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4", + "v_dot_5 = (1/m_5) * SigmaF_5", + "v_dot_6 = (1/m_6) * SigmaF_6", + "v_dot_7 = (1/m_7) * SigmaF_7", + "v_dot_8 = (1/m_8) * SigmaF_8" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 8; total state dim = 2M = 16.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.9545 \\Rightarrow \\zeta = 0.0148 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.954", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-1.413, -1.043, -0.478, 0.025, 0.341, 0.648, 1.14, 1.457] m and velocities v_0 = [0.455, 0.793, -0.384, -0.384, -0.487, -0.197, -0.197, 1.301] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = +0.7896 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 1.6225 J.", + "With e = 0.954, hard collisions retain a fraction e^2 = 0.911 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "2 attached-spring bumper(s): during contact the spring stores PE = \\tfrac{1}{2} k_a \\cdot compression^2 and converts it back to KE, providing a smooth (non-impulsive) momentum exchange whose duration depends on \\sqrt (m_eff / k_a).", + "2 diatomic spring-pair(s): each pair has a centre-of-mass mode (free translation; no internal force, no energy change) plus an internal vibration mode at \\omega = \\sqrt (k_p / \\mu_pair); collisions with external bodies excite both.", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 8):", + " body[0]: kind=spring_a, m=1.309 kg, half_size=0.136 m, x0=-1.413 m, v0=+0.455 m/s", + " body[1]: kind=spring_a, m=1.643 kg, half_size=0.134 m, x0=-1.043 m, v0=+0.793 m/s", + " body[2]: kind=pair_left, m=1.731 kg, half_size=0.148 m, x0=-0.478 m, v0=-0.384 m/s", + " body[3]: kind=pair_right, m=1.229 kg, half_size=0.120 m, x0=0.025 m, v0=-0.384 m/s", + " body[4]: kind=sphere, m=0.507 kg, half_size=0.096 m, x0=0.341 m, v0=-0.487 m/s", + " body[5]: kind=pair_left, m=1.767 kg, half_size=0.111 m, x0=0.648 m, v0=-0.197 m/s", + " body[6]: kind=pair_right, m=1.716 kg, half_size=0.125 m, x0=1.140 m, v0=-0.197 m/s", + " body[7]: kind=block, m=0.738 kg, half_size=0.092 m, x0=1.457 m, v0=+1.301 m/s", + "Springs:", + " spring_a on body[0] side=-x: k=103.79 N/m, L_natural=0.184 m", + " spring_a on body[1] side=+x: k=92.01 N/m, L_natural=0.183 m", + " spring_pair (body[2], body[3]): k=175.74 N/m, L_natural=0.235 m", + " spring_pair (body[5], body[6]): k=37.66 N/m, L_natural=0.257 m" + ], + "parameters": { + "setup_seed": { + "value": 596244.679719915, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.9544906186240609, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "spring_a", + "spring_a", + "pair_left", + "pair_right", + "sphere", + "pair_left", + "pair_right", + "block" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.3094074071706645, + 1.642571211375448, + 1.73083121935056, + 1.2294348097179584, + 0.5072907990164196, + 1.7673231496095998, + 1.716205906042761, + 0.7380185268052474 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.13645238844476498, + 0.13357182115036975, + 0.14834819016662953, + 0.11974786208937471, + 0.09621110156792596, + 0.11059184203436725, + 0.12500776218435908, + 0.09216254955762143 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -1.4130351015855158, + -1.0430108919903809, + -0.47809890466687, + 0.024904091304172482, + 0.3408630549614733, + 0.6476659985637665, + 1.1401546287306787, + 1.4573249404726591 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + 0.45473246165888614, + 0.7934443600850405, + -0.3837600621966306, + -0.3837600621966306, + -0.48688083077806255, + -0.19695155688194876, + -0.19695155688194876, + 1.3007595401345045 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [ + { + "body_idx": 0, + "k": 103.78701897880858, + "L": 0.18437685300542894, + "side": -1 + }, + { + "body_idx": 1, + "k": 92.00522628083843, + "L": 0.18299197600651146, + "side": 1 + } + ], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [ + { + "left_body_idx": 2, + "right_body_idx": 3, + "k": 175.73892889801158, + "L": 0.23490694371503826 + }, + { + "left_body_idx": 5, + "right_body_idx": 6, + "k": 37.655721072570586, + "L": 0.25688902594818597 + } + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/1521/params.json b/scipy/complex_collision_plane/1521/params.json new file mode 100644 index 0000000000000000000000000000000000000000..73d015bc90f6810fe5f3b3584f37ff1f348012cb --- /dev/null +++ b/scipy/complex_collision_plane/1521/params.json @@ -0,0 +1,85 @@ +{ + "setup_seed": 596244.679719915, + "restitution": 0.9544906186240609, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "spring_a", + "spring_a", + "pair_left", + "pair_right", + "sphere", + "pair_left", + "pair_right", + "block" + ], + "masses": [ + 1.3094074071706645, + 1.642571211375448, + 1.73083121935056, + 1.2294348097179584, + 0.5072907990164196, + 1.7673231496095998, + 1.716205906042761, + 0.7380185268052474 + ], + "half_sizes": [ + 0.13645238844476498, + 0.13357182115036975, + 0.14834819016662953, + 0.11974786208937471, + 0.09621110156792596, + 0.11059184203436725, + 0.12500776218435908, + 0.09216254955762143 + ], + "x0": [ + -1.4130351015855158, + -1.0430108919903809, + -0.47809890466687, + 0.024904091304172482, + 0.3408630549614733, + 0.6476659985637665, + 1.1401546287306787, + 1.4573249404726591 + ], + "v0": [ + 0.45473246165888614, + 0.7934443600850405, + -0.3837600621966306, + -0.3837600621966306, + -0.48688083077806255, + -0.19695155688194876, + -0.19695155688194876, + 1.3007595401345045 + ], + "spring_a": [ + { + "body_idx": 0, + "k": 103.78701897880858, + "L": 0.18437685300542894, + "side": -1 + }, + { + "body_idx": 1, + "k": 92.00522628083843, + "L": 0.18299197600651146, + "side": 1 + } + ], + "spring_pair": [ + { + "left_body_idx": 2, + "right_body_idx": 3, + "k": 175.73892889801158, + "L": 0.23490694371503826 + }, + { + "left_body_idx": 5, + "right_body_idx": 6, + "k": 37.655721072570586, + "L": 0.25688902594818597 + } + ] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/1521/simulation_code.py b/scipy/complex_collision_plane/1521/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..b3b51176251ce39e1e411f3c07bcd187601a3461 --- /dev/null +++ b/scipy/complex_collision_plane/1521/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.9545 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.9544906186240609 +k_contact = 5000.0 +body_kinds = ['spring_a', 'spring_a', 'pair_left', 'pair_right', 'sphere', 'pair_left', 'pair_right', 'block'] +masses = [1.3094074071706645, 1.642571211375448, 1.73083121935056, 1.2294348097179584, 0.5072907990164196, 1.7673231496095998, 1.716205906042761, 0.7380185268052474] +half_sizes = [0.13645238844476498, 0.13357182115036975, 0.14834819016662953, 0.11974786208937471, 0.09621110156792596, 0.11059184203436725, 0.12500776218435908, 0.09216254955762143] +x0_list = [-1.4130351015855158, -1.0430108919903809, -0.47809890466687, 0.024904091304172482, 0.3408630549614733, 0.6476659985637665, 1.1401546287306787, 1.4573249404726591] +v0_list = [0.45473246165888614, 0.7934443600850405, -0.3837600621966306, -0.3837600621966306, -0.48688083077806255, -0.19695155688194876, -0.19695155688194876, 1.3007595401345045] +spring_a = [{'body_idx': 0, 'k': 103.78701897880858, 'L': 0.18437685300542894, 'side': -1}, {'body_idx': 1, 'k': 92.00522628083843, 'L': 0.18299197600651146, 'side': 1}] +spring_pair = [{'left_body_idx': 2, 'right_body_idx': 3, 'k': 175.73892889801158, 'L': 0.23490694371503826}, {'left_body_idx': 5, 'right_body_idx': 6, 'k': 37.655721072570586, 'L': 0.25688902594818597}] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/1521/video.mp4 b/scipy/complex_collision_plane/1521/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..16be916285e9ddb641498cdb444b76721a40a4bf --- /dev/null +++ b/scipy/complex_collision_plane/1521/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f15cece177e90d4f109dee07797b8c91293dd7a5deff9057bebdebb85cef0357 +size 23650 diff --git a/scipy/complex_collision_plane/1528/cot.json b/scipy/complex_collision_plane/1528/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..712d93c40d6011265f5ccf30ad4c420251599bfc --- /dev/null +++ b/scipy/complex_collision_plane/1528/cot.json @@ -0,0 +1,201 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 5 rigid bodies are placed and given initial velocities. Composition: 2 sphere(s), 2 block(s), 1 block(s) with attached spring bumper(s), and 0 diatomic spring-pair(s). Newton's restitution e=0.308 is enforced on every hard contact. Total initial momentum p_0=-1.5429 kg \\cdot m/s, initial kinetic energy KE_0=8.5120 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "x_5", + "v_1", + "v_2", + "v_3", + "v_4", + "v_5" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{x}_5 = v_5", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4", + "\\dot{v}_5 = (1/m_5) \\cdot \\SigmaF_5" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "x_dot_5 = v_5", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4", + "v_dot_5 = (1/m_5) * SigmaF_5" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 5; total state dim = 2M = 10.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.3075 \\Rightarrow \\zeta = 0.3514 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.308", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-0.659, -0.366, 0.006, 0.27, 0.611] m and velocities v_0 = [1.663, -2.087, 0.838, -0.011, -2.216] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = -1.5429 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 8.5120 J.", + "With e = 0.308, hard collisions retain a fraction e^2 = 0.095 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "1 attached-spring bumper(s): during contact the spring stores PE = \\tfrac{1}{2} k_a \\cdot compression^2 and converts it back to KE, providing a smooth (non-impulsive) momentum exchange whose duration depends on \\sqrt (m_eff / k_a).", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 5):", + " body[0]: kind=sphere, m=1.455 kg, half_size=0.104 m, x0=-0.659 m, v0=+1.663 m/s", + " body[1]: kind=spring_a, m=1.914 kg, half_size=0.089 m, x0=-0.366 m, v0=-2.087 m/s", + " body[2]: kind=sphere, m=1.858 kg, half_size=0.077 m, x0=0.006 m, v0=+0.838 m/s", + " body[3]: kind=block, m=0.899 kg, half_size=0.087 m, x0=0.270 m, v0=-0.011 m/s", + " body[4]: kind=block, m=0.684 kg, half_size=0.153 m, x0=0.611 m, v0=-2.216 m/s", + "Springs:", + " spring_a on body[1] side=+x: k=144.23 N/m, L_natural=0.105 m" + ], + "parameters": { + "setup_seed": { + "value": 128496.36141355203, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.307504243452638, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "sphere", + "spring_a", + "sphere", + "block", + "block" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.4549267851519712, + 1.9139692764969964, + 1.8576359266629727, + 0.8991492682535063, + 0.6836439125245577 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.10423173028376678, + 0.08911085538438468, + 0.0771856542910224, + 0.08747490069581221, + 0.15281269812811998 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -0.6592826645059437, + -0.36594007883779245, + 0.005753542850823301, + 0.2704140978376577, + 0.6107016966615904 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + 1.66337269927255, + -2.087301557892458, + 0.8377139773522413, + -0.01050302551747917, + -2.21562408671436 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [ + { + "body_idx": 1, + "k": 144.2334833982614, + "L": 0.10539711201320888, + "side": 1 + } + ], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/1528/params.json b/scipy/complex_collision_plane/1528/params.json new file mode 100644 index 0000000000000000000000000000000000000000..119eede5cfa199ba80a810ffaa15ec97431a4ab5 --- /dev/null +++ b/scipy/complex_collision_plane/1528/params.json @@ -0,0 +1,51 @@ +{ + "setup_seed": 128496.36141355203, + "restitution": 0.307504243452638, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "sphere", + "spring_a", + "sphere", + "block", + "block" + ], + "masses": [ + 1.4549267851519712, + 1.9139692764969964, + 1.8576359266629727, + 0.8991492682535063, + 0.6836439125245577 + ], + "half_sizes": [ + 0.10423173028376678, + 0.08911085538438468, + 0.0771856542910224, + 0.08747490069581221, + 0.15281269812811998 + ], + "x0": [ + -0.6592826645059437, + -0.36594007883779245, + 0.005753542850823301, + 0.2704140978376577, + 0.6107016966615904 + ], + "v0": [ + 1.66337269927255, + -2.087301557892458, + 0.8377139773522413, + -0.01050302551747917, + -2.21562408671436 + ], + "spring_a": [ + { + "body_idx": 1, + "k": 144.2334833982614, + "L": 0.10539711201320888, + "side": 1 + } + ], + "spring_pair": [] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/1528/simulation_code.py b/scipy/complex_collision_plane/1528/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..93f76009d3dd3a471d77fd834a4ee6b5dfe2e388 --- /dev/null +++ b/scipy/complex_collision_plane/1528/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.3075 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.307504243452638 +k_contact = 5000.0 +body_kinds = ['sphere', 'spring_a', 'sphere', 'block', 'block'] +masses = [1.4549267851519712, 1.9139692764969964, 1.8576359266629727, 0.8991492682535063, 0.6836439125245577] +half_sizes = [0.10423173028376678, 0.08911085538438468, 0.0771856542910224, 0.08747490069581221, 0.15281269812811998] +x0_list = [-0.6592826645059437, -0.36594007883779245, 0.005753542850823301, 0.2704140978376577, 0.6107016966615904] +v0_list = [1.66337269927255, -2.087301557892458, 0.8377139773522413, -0.01050302551747917, -2.21562408671436] +spring_a = [{'body_idx': 1, 'k': 144.2334833982614, 'L': 0.10539711201320888, 'side': 1}] +spring_pair = [] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/1528/video.mp4 b/scipy/complex_collision_plane/1528/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..4343adf5ec17e72e54dc45141d899a99843bad90 --- /dev/null +++ b/scipy/complex_collision_plane/1528/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:070cc60693198489ed7ba8a70b902c089768ae81dfa21d7d3867e5794ee8925b +size 13285 diff --git a/scipy/complex_collision_plane/1704/cot.json b/scipy/complex_collision_plane/1704/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..bd5f7623355d678be434492d226bb7a2b6a41d80 --- /dev/null +++ b/scipy/complex_collision_plane/1704/cot.json @@ -0,0 +1,244 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 8 rigid bodies are placed and given initial velocities. Composition: 1 sphere(s), 3 block(s), 0 block(s) with attached spring bumper(s), and 2 diatomic spring-pair(s). Newton's restitution e=0.574 is enforced on every hard contact. Total initial momentum p_0=6.5044 kg \\cdot m/s, initial kinetic energy KE_0=4.0528 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "x_5", + "x_6", + "x_7", + "x_8", + "v_1", + "v_2", + "v_3", + "v_4", + "v_5", + "v_6", + "v_7", + "v_8" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{x}_5 = v_5", + "\\dot{x}_6 = v_6", + "\\dot{x}_7 = v_7", + "\\dot{x}_8 = v_8", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4", + "\\dot{v}_5 = (1/m_5) \\cdot \\SigmaF_5", + "\\dot{v}_6 = (1/m_6) \\cdot \\SigmaF_6", + "\\dot{v}_7 = (1/m_7) \\cdot \\SigmaF_7", + "\\dot{v}_8 = (1/m_8) \\cdot \\SigmaF_8" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "x_dot_5 = v_5", + "x_dot_6 = v_6", + "x_dot_7 = v_7", + "x_dot_8 = v_8", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4", + "v_dot_5 = (1/m_5) * SigmaF_5", + "v_dot_6 = (1/m_6) * SigmaF_6", + "v_dot_7 = (1/m_7) * SigmaF_7", + "v_dot_8 = (1/m_8) * SigmaF_8" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 8; total state dim = 2M = 16.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.5737 \\Rightarrow \\zeta = 0.1742 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.574", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-1.145, -0.747, -0.464, -0.182, 0.109, 0.421, 0.825, 1.174] m and velocities v_0 = [0.371, 0.371, 1.2, 1.459, -0.45, 0.698, 0.698, 1.836] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = +6.5044 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 4.0528 J.", + "With e = 0.574, hard collisions retain a fraction e^2 = 0.329 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "2 diatomic spring-pair(s): each pair has a centre-of-mass mode (free translation; no internal force, no energy change) plus an internal vibration mode at \\omega = \\sqrt (k_p / \\mu_pair); collisions with external bodies excite both.", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 8):", + " body[0]: kind=pair_left, m=1.358 kg, half_size=0.134 m, x0=-1.145 m, v0=+0.371 m/s", + " body[1]: kind=pair_right, m=1.309 kg, half_size=0.085 m, x0=-0.747 m, v0=+0.371 m/s", + " body[2]: kind=block, m=0.526 kg, half_size=0.099 m, x0=-0.464 m, v0=+1.200 m/s", + " body[3]: kind=block, m=0.694 kg, half_size=0.083 m, x0=-0.182 m, v0=+1.459 m/s", + " body[4]: kind=sphere, m=0.876 kg, half_size=0.109 m, x0=0.109 m, v0=-0.450 m/s", + " body[5]: kind=pair_left, m=1.854 kg, half_size=0.102 m, x0=0.421 m, v0=+0.698 m/s", + " body[6]: kind=pair_right, m=1.300 kg, half_size=0.142 m, x0=0.825 m, v0=+0.698 m/s", + " body[7]: kind=block, m=1.123 kg, half_size=0.106 m, x0=1.174 m, v0=+1.836 m/s", + "Springs:", + " spring_pair (body[0], body[1]): k=119.58 N/m, L_natural=0.179 m", + " spring_pair (body[5], body[6]): k=180.77 N/m, L_natural=0.160 m" + ], + "parameters": { + "setup_seed": { + "value": 718066.100988352, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.5737279471891562, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "pair_left", + "pair_right", + "block", + "block", + "sphere", + "pair_left", + "pair_right", + "block" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.3576264077539937, + 1.3087957593157773, + 0.5261049351829521, + 0.6943851933272389, + 0.8758464273499056, + 1.8542987571199467, + 1.2996159245446948, + 1.1228942678548979 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.13429806013710283, + 0.08470395080405002, + 0.09898482142636207, + 0.08266084384097604, + 0.1089498703635394, + 0.10214934139546439, + 0.14245627438550623, + 0.10569893225249485 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -1.1450360721379247, + -0.747482586840293, + -0.4637938146098808, + -0.18214814934254253, + 0.10946256486197292, + 0.4205617766209766, + 0.8254799933845323, + 1.1736352000225334 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + 0.37111525689251257, + 0.37111525689251257, + 1.1996452865686407, + 1.4590904619246272, + -0.45034865813236014, + 0.6984786701017669, + 0.6984786701017669, + 1.836328130709103 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [ + { + "left_body_idx": 0, + "right_body_idx": 1, + "k": 119.58288207099248, + "L": 0.17855147435647914 + }, + { + "left_body_idx": 5, + "right_body_idx": 6, + "k": 180.7731382865328, + "L": 0.1603126009825849 + } + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/1704/params.json b/scipy/complex_collision_plane/1704/params.json new file mode 100644 index 0000000000000000000000000000000000000000..30857175f4c5620bb4525a42f76e401932addf17 --- /dev/null +++ b/scipy/complex_collision_plane/1704/params.json @@ -0,0 +1,72 @@ +{ + "setup_seed": 718066.100988352, + "restitution": 0.5737279471891562, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "pair_left", + "pair_right", + "block", + "block", + "sphere", + "pair_left", + "pair_right", + "block" + ], + "masses": [ + 1.3576264077539937, + 1.3087957593157773, + 0.5261049351829521, + 0.6943851933272389, + 0.8758464273499056, + 1.8542987571199467, + 1.2996159245446948, + 1.1228942678548979 + ], + "half_sizes": [ + 0.13429806013710283, + 0.08470395080405002, + 0.09898482142636207, + 0.08266084384097604, + 0.1089498703635394, + 0.10214934139546439, + 0.14245627438550623, + 0.10569893225249485 + ], + "x0": [ + -1.1450360721379247, + -0.747482586840293, + -0.4637938146098808, + -0.18214814934254253, + 0.10946256486197292, + 0.4205617766209766, + 0.8254799933845323, + 1.1736352000225334 + ], + "v0": [ + 0.37111525689251257, + 0.37111525689251257, + 1.1996452865686407, + 1.4590904619246272, + -0.45034865813236014, + 0.6984786701017669, + 0.6984786701017669, + 1.836328130709103 + ], + "spring_a": [], + "spring_pair": [ + { + "left_body_idx": 0, + "right_body_idx": 1, + "k": 119.58288207099248, + "L": 0.17855147435647914 + }, + { + "left_body_idx": 5, + "right_body_idx": 6, + "k": 180.7731382865328, + "L": 0.1603126009825849 + } + ] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/1704/simulation_code.py b/scipy/complex_collision_plane/1704/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0ce71d006b7d939a20f7cfe9dce1ba9cbf98adb8 --- /dev/null +++ b/scipy/complex_collision_plane/1704/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.5737 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.5737279471891562 +k_contact = 5000.0 +body_kinds = ['pair_left', 'pair_right', 'block', 'block', 'sphere', 'pair_left', 'pair_right', 'block'] +masses = [1.3576264077539937, 1.3087957593157773, 0.5261049351829521, 0.6943851933272389, 0.8758464273499056, 1.8542987571199467, 1.2996159245446948, 1.1228942678548979] +half_sizes = [0.13429806013710283, 0.08470395080405002, 0.09898482142636207, 0.08266084384097604, 0.1089498703635394, 0.10214934139546439, 0.14245627438550623, 0.10569893225249485] +x0_list = [-1.1450360721379247, -0.747482586840293, -0.4637938146098808, -0.18214814934254253, 0.10946256486197292, 0.4205617766209766, 0.8254799933845323, 1.1736352000225334] +v0_list = [0.37111525689251257, 0.37111525689251257, 1.1996452865686407, 1.4590904619246272, -0.45034865813236014, 0.6984786701017669, 0.6984786701017669, 1.836328130709103] +spring_a = [] +spring_pair = [{'left_body_idx': 0, 'right_body_idx': 1, 'k': 119.58288207099248, 'L': 0.17855147435647914}, {'left_body_idx': 5, 'right_body_idx': 6, 'k': 180.7731382865328, 'L': 0.1603126009825849}] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/1704/video.mp4 b/scipy/complex_collision_plane/1704/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e6db2b4ab1ef6abe068972ed6295a16541c84d7c --- /dev/null +++ b/scipy/complex_collision_plane/1704/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:167dfe7955b59eb9bf9ec0e2074b41d1b47ac14af3c449a63d1aaad00503bd68 +size 18749 diff --git a/scipy/complex_collision_plane/1789/cot.json b/scipy/complex_collision_plane/1789/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..7f01e5fcb101314ab1c5235ead331e13b8c27aa4 --- /dev/null +++ b/scipy/complex_collision_plane/1789/cot.json @@ -0,0 +1,220 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 6 rigid bodies are placed and given initial velocities. Composition: 1 sphere(s), 1 block(s), 0 block(s) with attached spring bumper(s), and 2 diatomic spring-pair(s). Newton's restitution e=0.395 is enforced on every hard contact. Total initial momentum p_0=-2.6548 kg \\cdot m/s, initial kinetic energy KE_0=4.4547 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "x_5", + "x_6", + "v_1", + "v_2", + "v_3", + "v_4", + "v_5", + "v_6" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{x}_5 = v_5", + "\\dot{x}_6 = v_6", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4", + "\\dot{v}_5 = (1/m_5) \\cdot \\SigmaF_5", + "\\dot{v}_6 = (1/m_6) \\cdot \\SigmaF_6" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "x_dot_5 = v_5", + "x_dot_6 = v_6", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4", + "v_dot_5 = (1/m_5) * SigmaF_5", + "v_dot_6 = (1/m_6) * SigmaF_6" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 6; total state dim = 2M = 12.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.3947 \\Rightarrow \\zeta = 0.2838 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.395", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-0.984, -0.672, -0.209, 0.152, 0.653, 0.987] m and velocities v_0 = [-2.492, 0.072, 0.072, 0.873, 0.873, -0.16] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = -2.6548 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 4.4547 J.", + "With e = 0.395, hard collisions retain a fraction e^2 = 0.156 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "2 diatomic spring-pair(s): each pair has a centre-of-mass mode (free translation; no internal force, no energy change) plus an internal vibration mode at \\omega = \\sqrt (k_p / \\mu_pair); collisions with external bodies excite both.", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 6):", + " body[0]: kind=block, m=1.309 kg, half_size=0.108 m, x0=-0.984 m, v0=-2.492 m/s", + " body[1]: kind=pair_left, m=0.446 kg, half_size=0.104 m, x0=-0.672 m, v0=+0.072 m/s", + " body[2]: kind=pair_right, m=0.766 kg, half_size=0.135 m, x0=-0.209 m, v0=+0.072 m/s", + " body[3]: kind=pair_left, m=0.481 kg, half_size=0.127 m, x0=0.152 m, v0=+0.873 m/s", + " body[4]: kind=pair_right, m=0.469 kg, half_size=0.129 m, x0=0.653 m, v0=+0.873 m/s", + " body[5]: kind=sphere, m=1.937 kg, half_size=0.105 m, x0=0.987 m, v0=-0.160 m/s", + "Springs:", + " spring_pair (body[1], body[2]): k=38.42 N/m, L_natural=0.224 m", + " spring_pair (body[3], body[4]): k=176.24 N/m, L_natural=0.245 m" + ], + "parameters": { + "setup_seed": { + "value": 954684.0622685737, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.3946597473076895, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "block", + "pair_left", + "pair_right", + "pair_left", + "pair_right", + "sphere" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.3087453017015622, + 0.44554695157273394, + 0.7661280179115448, + 0.4813259317159534, + 0.4690958204116699, + 1.9366710557354851 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.10752584259737075, + 0.10439220195074243, + 0.13454972292292117, + 0.12678487129680105, + 0.1288263825805805, + 0.10502661459816792 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -0.9842888917242361, + -0.6723708471761225, + -0.2091316438080164, + 0.15220295041170617, + 0.6529351225446902, + 0.9867881197234389 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + -2.4922833395580835, + 0.07222688911763764, + 0.07222688911763764, + 0.8728428859911335, + 0.8728428859911335, + -0.16015433855542316 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [ + { + "left_body_idx": 1, + "right_body_idx": 2, + "k": 38.41641497701482, + "L": 0.22429727849444211 + }, + { + "left_body_idx": 3, + "right_body_idx": 4, + "k": 176.2408755269882, + "L": 0.24512091825560225 + } + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/1789/params.json b/scipy/complex_collision_plane/1789/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b74a13e701d02b52d74a47ebfd4c7b689215c54f --- /dev/null +++ b/scipy/complex_collision_plane/1789/params.json @@ -0,0 +1,62 @@ +{ + "setup_seed": 954684.0622685737, + "restitution": 0.3946597473076895, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "block", + "pair_left", + "pair_right", + "pair_left", + "pair_right", + "sphere" + ], + "masses": [ + 1.3087453017015622, + 0.44554695157273394, + 0.7661280179115448, + 0.4813259317159534, + 0.4690958204116699, + 1.9366710557354851 + ], + "half_sizes": [ + 0.10752584259737075, + 0.10439220195074243, + 0.13454972292292117, + 0.12678487129680105, + 0.1288263825805805, + 0.10502661459816792 + ], + "x0": [ + -0.9842888917242361, + -0.6723708471761225, + -0.2091316438080164, + 0.15220295041170617, + 0.6529351225446902, + 0.9867881197234389 + ], + "v0": [ + -2.4922833395580835, + 0.07222688911763764, + 0.07222688911763764, + 0.8728428859911335, + 0.8728428859911335, + -0.16015433855542316 + ], + "spring_a": [], + "spring_pair": [ + { + "left_body_idx": 1, + "right_body_idx": 2, + "k": 38.41641497701482, + "L": 0.22429727849444211 + }, + { + "left_body_idx": 3, + "right_body_idx": 4, + "k": 176.2408755269882, + "L": 0.24512091825560225 + } + ] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/1789/simulation_code.py b/scipy/complex_collision_plane/1789/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..865b41fd4d01b5b90e5f9697a48e5c7095a9d379 --- /dev/null +++ b/scipy/complex_collision_plane/1789/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.3947 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.3946597473076895 +k_contact = 5000.0 +body_kinds = ['block', 'pair_left', 'pair_right', 'pair_left', 'pair_right', 'sphere'] +masses = [1.3087453017015622, 0.44554695157273394, 0.7661280179115448, 0.4813259317159534, 0.4690958204116699, 1.9366710557354851] +half_sizes = [0.10752584259737075, 0.10439220195074243, 0.13454972292292117, 0.12678487129680105, 0.1288263825805805, 0.10502661459816792] +x0_list = [-0.9842888917242361, -0.6723708471761225, -0.2091316438080164, 0.15220295041170617, 0.6529351225446902, 0.9867881197234389] +v0_list = [-2.4922833395580835, 0.07222688911763764, 0.07222688911763764, 0.8728428859911335, 0.8728428859911335, -0.16015433855542316] +spring_a = [] +spring_pair = [{'left_body_idx': 1, 'right_body_idx': 2, 'k': 38.41641497701482, 'L': 0.22429727849444211}, {'left_body_idx': 3, 'right_body_idx': 4, 'k': 176.2408755269882, 'L': 0.24512091825560225}] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/1789/video.mp4 b/scipy/complex_collision_plane/1789/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..03e8daba03be184932f778c7dfdc3955c2596f4b --- /dev/null +++ b/scipy/complex_collision_plane/1789/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bcd2fd1842dbf4bd250867009100f17a5833af97d119730f736392a3838cfd9a +size 16098 diff --git a/scipy/complex_collision_plane/1896/cot.json b/scipy/complex_collision_plane/1896/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..77fbec0f4c5dd70265fdaf8734815e4bdce36144 --- /dev/null +++ b/scipy/complex_collision_plane/1896/cot.json @@ -0,0 +1,189 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 4 rigid bodies are placed and given initial velocities. Composition: 1 sphere(s), 1 block(s), 0 block(s) with attached spring bumper(s), and 1 diatomic spring-pair(s). Newton's restitution e=0.715 is enforced on every hard contact. Total initial momentum p_0=-2.9603 kg \\cdot m/s, initial kinetic energy KE_0=1.8264 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "v_1", + "v_2", + "v_3", + "v_4" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 4; total state dim = 2M = 8.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.7149 \\Rightarrow \\zeta = 0.1062 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.715", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-0.569, -0.085, 0.315, 0.642] m and velocities v_0 = [-1.458, -1.458, -0.201, -0.416] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = -2.9603 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 1.8264 J.", + "With e = 0.715, hard collisions retain a fraction e^2 = 0.511 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "1 diatomic spring-pair(s): each pair has a centre-of-mass mode (free translation; no internal force, no energy change) plus an internal vibration mode at \\omega = \\sqrt (k_p / \\mu_pair); collisions with external bodies excite both.", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 4):", + " body[0]: kind=pair_left, m=0.691 kg, half_size=0.148 m, x0=-0.569 m, v0=-1.458 m/s", + " body[1]: kind=pair_right, m=0.927 kg, half_size=0.146 m, x0=-0.085 m, v0=-1.458 m/s", + " body[2]: kind=block, m=0.843 kg, half_size=0.153 m, x0=0.315 m, v0=-0.201 m/s", + " body[3]: kind=sphere, m=1.036 kg, half_size=0.075 m, x0=0.642 m, v0=-0.416 m/s", + "Springs:", + " spring_pair (body[0], body[1]): k=12.76 N/m, L_natural=0.189 m" + ], + "parameters": { + "setup_seed": { + "value": 769423.8867740774, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.7148699396432463, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "pair_left", + "pair_right", + "block", + "sphere" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.6914283536366135, + 0.9274772806392542, + 0.8429657092031739, + 1.035912049824906 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.1484416220345252, + 0.14626606053825048, + 0.15324115782714587, + 0.07458839039764323 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -0.5685737966604747, + -0.0849097382928301, + 0.3145974800725666, + 0.6424270282973561 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + -1.4575765744015974, + -1.4575765744015974, + -0.20145990595704166, + -0.4158759375856613 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [ + { + "left_body_idx": 0, + "right_body_idx": 1, + "k": 12.757742140605245, + "L": 0.1889563757948688 + } + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/1896/params.json b/scipy/complex_collision_plane/1896/params.json new file mode 100644 index 0000000000000000000000000000000000000000..17a7e65c1ca7c3caefef41c6bd888fd73d42d46d --- /dev/null +++ b/scipy/complex_collision_plane/1896/params.json @@ -0,0 +1,46 @@ +{ + "setup_seed": 769423.8867740774, + "restitution": 0.7148699396432463, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "pair_left", + "pair_right", + "block", + "sphere" + ], + "masses": [ + 0.6914283536366135, + 0.9274772806392542, + 0.8429657092031739, + 1.035912049824906 + ], + "half_sizes": [ + 0.1484416220345252, + 0.14626606053825048, + 0.15324115782714587, + 0.07458839039764323 + ], + "x0": [ + -0.5685737966604747, + -0.0849097382928301, + 0.3145974800725666, + 0.6424270282973561 + ], + "v0": [ + -1.4575765744015974, + -1.4575765744015974, + -0.20145990595704166, + -0.4158759375856613 + ], + "spring_a": [], + "spring_pair": [ + { + "left_body_idx": 0, + "right_body_idx": 1, + "k": 12.757742140605245, + "L": 0.1889563757948688 + } + ] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/1896/simulation_code.py b/scipy/complex_collision_plane/1896/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..b2750473ab2b0449eab2c7dff22843ed6ff4fd94 --- /dev/null +++ b/scipy/complex_collision_plane/1896/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.7149 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.7148699396432463 +k_contact = 5000.0 +body_kinds = ['pair_left', 'pair_right', 'block', 'sphere'] +masses = [0.6914283536366135, 0.9274772806392542, 0.8429657092031739, 1.035912049824906] +half_sizes = [0.1484416220345252, 0.14626606053825048, 0.15324115782714587, 0.07458839039764323] +x0_list = [-0.5685737966604747, -0.0849097382928301, 0.3145974800725666, 0.6424270282973561] +v0_list = [-1.4575765744015974, -1.4575765744015974, -0.20145990595704166, -0.4158759375856613] +spring_a = [] +spring_pair = [{'left_body_idx': 0, 'right_body_idx': 1, 'k': 12.757742140605245, 'L': 0.1889563757948688}] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/1896/video.mp4 b/scipy/complex_collision_plane/1896/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..409f04b2430100f37529cfff740c2b6697ebe797 --- /dev/null +++ b/scipy/complex_collision_plane/1896/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4e74261b3dd0be1ef89147ae2a4db66952ffb97d13d811624caad2f6fbc0ca98 +size 14500 diff --git a/scipy/complex_collision_plane/232/cot.json b/scipy/complex_collision_plane/232/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..c6d96e94bd7f5194a88ce93d7e8180fefde2deef --- /dev/null +++ b/scipy/complex_collision_plane/232/cot.json @@ -0,0 +1,248 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 7 rigid bodies are placed and given initial velocities. Composition: 2 sphere(s), 0 block(s), 3 block(s) with attached spring bumper(s), and 1 diatomic spring-pair(s). Newton's restitution e=0.353 is enforced on every hard contact. Total initial momentum p_0=3.2326 kg \\cdot m/s, initial kinetic energy KE_0=6.6234 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "x_5", + "x_6", + "x_7", + "v_1", + "v_2", + "v_3", + "v_4", + "v_5", + "v_6", + "v_7" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{x}_5 = v_5", + "\\dot{x}_6 = v_6", + "\\dot{x}_7 = v_7", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4", + "\\dot{v}_5 = (1/m_5) \\cdot \\SigmaF_5", + "\\dot{v}_6 = (1/m_6) \\cdot \\SigmaF_6", + "\\dot{v}_7 = (1/m_7) \\cdot \\SigmaF_7" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "x_dot_5 = v_5", + "x_dot_6 = v_6", + "x_dot_7 = v_7", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4", + "v_dot_5 = (1/m_5) * SigmaF_5", + "v_dot_6 = (1/m_6) * SigmaF_6", + "v_dot_7 = (1/m_7) * SigmaF_7" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 7; total state dim = 2M = 14.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.3533 \\Rightarrow \\zeta = 0.3144 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.353", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-1.234, -0.698, -0.225, 0.111, 0.502, 0.813, 1.144] m and velocities v_0 = [-0.043, 0.669, -1.88, 1.085, 1.085, 1.444, -2.399] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = +3.2326 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 6.6234 J.", + "With e = 0.353, hard collisions retain a fraction e^2 = 0.125 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "3 attached-spring bumper(s): during contact the spring stores PE = \\tfrac{1}{2} k_a \\cdot compression^2 and converts it back to KE, providing a smooth (non-impulsive) momentum exchange whose duration depends on \\sqrt (m_eff / k_a).", + "1 diatomic spring-pair(s): each pair has a centre-of-mass mode (free translation; no internal force, no energy change) plus an internal vibration mode at \\omega = \\sqrt (k_p / \\mu_pair); collisions with external bodies excite both.", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 7):", + " body[0]: kind=sphere, m=1.891 kg, half_size=0.070 m, x0=-1.234 m, v0=-0.043 m/s", + " body[1]: kind=spring_a, m=1.963 kg, half_size=0.127 m, x0=-0.698 m, v0=+0.669 m/s", + " body[2]: kind=spring_a, m=0.827 kg, half_size=0.088 m, x0=-0.225 m, v0=-1.880 m/s", + " body[3]: kind=pair_left, m=1.532 kg, half_size=0.148 m, x0=0.111 m, v0=+1.085 m/s", + " body[4]: kind=pair_right, m=1.755 kg, half_size=0.140 m, x0=0.502 m, v0=+1.085 m/s", + " body[5]: kind=sphere, m=0.998 kg, half_size=0.071 m, x0=0.813 m, v0=+1.444 m/s", + " body[6]: kind=spring_a, m=0.606 kg, half_size=0.160 m, x0=1.144 m, v0=-2.399 m/s", + "Springs:", + " spring_a on body[1] side=-x: k=32.98 N/m, L_natural=0.238 m", + " spring_a on body[2] side=-x: k=58.96 N/m, L_natural=0.158 m", + " spring_a on body[6] side=+x: k=113.25 N/m, L_natural=0.174 m", + " spring_pair (body[3], body[4]): k=191.55 N/m, L_natural=0.104 m" + ], + "parameters": { + "setup_seed": { + "value": 328022.6557155434, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.3533068902077271, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "sphere", + "spring_a", + "spring_a", + "pair_left", + "pair_right", + "sphere", + "spring_a" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.8908807328378612, + 1.9631711235270344, + 0.8269035004486573, + 1.5324777279418587, + 1.7547981310258671, + 0.9979250744637924, + 0.6059596448614941 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.0700553924372746, + 0.1272293904577582, + 0.08802749131526517, + 0.14750250488753386, + 0.13992924548593297, + 0.07137224144872138, + 0.15953890167934542 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -1.2337742095009205, + -0.6982270294266737, + -0.2246499930622683, + 0.11088000314053081, + 0.5020780701961294, + 0.8133795571307838, + 1.1442907002588507 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + -0.04297354655451713, + 0.6690654811014336, + -1.8802781985342243, + 1.085464399687699, + 1.085464399687699, + 1.443717938719863, + -2.399172776803671 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [ + { + "body_idx": 1, + "k": 32.97664229264669, + "L": 0.23826239717921383, + "side": -1 + }, + { + "body_idx": 2, + "k": 58.95881208078588, + "L": 0.1583201545913817, + "side": -1 + }, + { + "body_idx": 6, + "k": 113.25487656677352, + "L": 0.1737049222450902, + "side": 1 + } + ], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [ + { + "left_body_idx": 3, + "right_body_idx": 4, + "k": 191.5514042117262, + "L": 0.10376631668213186 + } + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/232/params.json b/scipy/complex_collision_plane/232/params.json new file mode 100644 index 0000000000000000000000000000000000000000..4cc6d2b8487e5c04a1f4f8e300cfa97e0bd6ac3c --- /dev/null +++ b/scipy/complex_collision_plane/232/params.json @@ -0,0 +1,80 @@ +{ + "setup_seed": 328022.6557155434, + "restitution": 0.3533068902077271, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "sphere", + "spring_a", + "spring_a", + "pair_left", + "pair_right", + "sphere", + "spring_a" + ], + "masses": [ + 1.8908807328378612, + 1.9631711235270344, + 0.8269035004486573, + 1.5324777279418587, + 1.7547981310258671, + 0.9979250744637924, + 0.6059596448614941 + ], + "half_sizes": [ + 0.0700553924372746, + 0.1272293904577582, + 0.08802749131526517, + 0.14750250488753386, + 0.13992924548593297, + 0.07137224144872138, + 0.15953890167934542 + ], + "x0": [ + -1.2337742095009205, + -0.6982270294266737, + -0.2246499930622683, + 0.11088000314053081, + 0.5020780701961294, + 0.8133795571307838, + 1.1442907002588507 + ], + "v0": [ + -0.04297354655451713, + 0.6690654811014336, + -1.8802781985342243, + 1.085464399687699, + 1.085464399687699, + 1.443717938719863, + -2.399172776803671 + ], + "spring_a": [ + { + "body_idx": 1, + "k": 32.97664229264669, + "L": 0.23826239717921383, + "side": -1 + }, + { + "body_idx": 2, + "k": 58.95881208078588, + "L": 0.1583201545913817, + "side": -1 + }, + { + "body_idx": 6, + "k": 113.25487656677352, + "L": 0.1737049222450902, + "side": 1 + } + ], + "spring_pair": [ + { + "left_body_idx": 3, + "right_body_idx": 4, + "k": 191.5514042117262, + "L": 0.10376631668213186 + } + ] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/232/simulation_code.py b/scipy/complex_collision_plane/232/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..ed6b8446b349d1b973bd36c1618c002c6a731b7d --- /dev/null +++ b/scipy/complex_collision_plane/232/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.3533 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.3533068902077271 +k_contact = 5000.0 +body_kinds = ['sphere', 'spring_a', 'spring_a', 'pair_left', 'pair_right', 'sphere', 'spring_a'] +masses = [1.8908807328378612, 1.9631711235270344, 0.8269035004486573, 1.5324777279418587, 1.7547981310258671, 0.9979250744637924, 0.6059596448614941] +half_sizes = [0.0700553924372746, 0.1272293904577582, 0.08802749131526517, 0.14750250488753386, 0.13992924548593297, 0.07137224144872138, 0.15953890167934542] +x0_list = [-1.2337742095009205, -0.6982270294266737, -0.2246499930622683, 0.11088000314053081, 0.5020780701961294, 0.8133795571307838, 1.1442907002588507] +v0_list = [-0.04297354655451713, 0.6690654811014336, -1.8802781985342243, 1.085464399687699, 1.085464399687699, 1.443717938719863, -2.399172776803671] +spring_a = [{'body_idx': 1, 'k': 32.97664229264669, 'L': 0.23826239717921383, 'side': -1}, {'body_idx': 2, 'k': 58.95881208078588, 'L': 0.1583201545913817, 'side': -1}, {'body_idx': 6, 'k': 113.25487656677352, 'L': 0.1737049222450902, 'side': 1}] +spring_pair = [{'left_body_idx': 3, 'right_body_idx': 4, 'k': 191.5514042117262, 'L': 0.10376631668213186}] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/232/video.mp4 b/scipy/complex_collision_plane/232/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..6544d05632b037520f8c428198ec6cf7e221ed8e --- /dev/null +++ b/scipy/complex_collision_plane/232/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f5ee2b47364b0f4d547e44e7ebfbf6ee07696afa28331559d09f2ba6f58bf1ea +size 17714 diff --git a/scipy/complex_collision_plane/2362/cot.json b/scipy/complex_collision_plane/2362/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..9893c865dda72213c1221777b5eaec03fcc33804 --- /dev/null +++ b/scipy/complex_collision_plane/2362/cot.json @@ -0,0 +1,232 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 7 rigid bodies are placed and given initial velocities. Composition: 1 sphere(s), 2 block(s), 0 block(s) with attached spring bumper(s), and 2 diatomic spring-pair(s). Newton's restitution e=0.957 is enforced on every hard contact. Total initial momentum p_0=-2.2915 kg \\cdot m/s, initial kinetic energy KE_0=3.8289 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "x_5", + "x_6", + "x_7", + "v_1", + "v_2", + "v_3", + "v_4", + "v_5", + "v_6", + "v_7" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{x}_5 = v_5", + "\\dot{x}_6 = v_6", + "\\dot{x}_7 = v_7", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4", + "\\dot{v}_5 = (1/m_5) \\cdot \\SigmaF_5", + "\\dot{v}_6 = (1/m_6) \\cdot \\SigmaF_6", + "\\dot{v}_7 = (1/m_7) \\cdot \\SigmaF_7" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "x_dot_5 = v_5", + "x_dot_6 = v_6", + "x_dot_7 = v_7", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4", + "v_dot_5 = (1/m_5) * SigmaF_5", + "v_dot_6 = (1/m_6) * SigmaF_6", + "v_dot_7 = (1/m_7) * SigmaF_7" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 7; total state dim = 2M = 14.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.9572 \\Rightarrow \\zeta = 0.0139 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.957", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-1.116, -0.805, -0.482, -0.115, 0.405, 0.795, 1.159] m and velocities v_0 = [2.221, -1.707, -0.613, 0.346, 0.346, -1.074, -1.074] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = -2.2915 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 3.8289 J.", + "With e = 0.957, hard collisions retain a fraction e^2 = 0.916 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "2 diatomic spring-pair(s): each pair has a centre-of-mass mode (free translation; no internal force, no energy change) plus an internal vibration mode at \\omega = \\sqrt (k_p / \\mu_pair); collisions with external bodies excite both.", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 7):", + " body[0]: kind=block, m=0.492 kg, half_size=0.127 m, x0=-1.116 m, v0=+2.221 m/s", + " body[1]: kind=sphere, m=1.182 kg, half_size=0.084 m, x0=-0.805 m, v0=-1.707 m/s", + " body[2]: kind=block, m=1.644 kg, half_size=0.140 m, x0=-0.482 m, v0=-0.613 m/s", + " body[3]: kind=pair_left, m=0.706 kg, half_size=0.127 m, x0=-0.115 m, v0=+0.346 m/s", + " body[4]: kind=pair_right, m=0.900 kg, half_size=0.141 m, x0=0.405 m, v0=+0.346 m/s", + " body[5]: kind=pair_left, m=0.307 kg, half_size=0.150 m, x0=0.795 m, v0=-1.074 m/s", + " body[6]: kind=pair_right, m=0.544 kg, half_size=0.084 m, x0=1.159 m, v0=-1.074 m/s", + "Springs:", + " spring_pair (body[3], body[4]): k=195.46 N/m, L_natural=0.251 m", + " spring_pair (body[5], body[6]): k=90.66 N/m, L_natural=0.130 m" + ], + "parameters": { + "setup_seed": { + "value": 831256.4084715546, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.9571812621488576, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "block", + "sphere", + "block", + "pair_left", + "pair_right", + "pair_left", + "pair_right" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.4915115801881481, + 1.1820744606279836, + 1.6441162282614838, + 0.7062128594117664, + 0.9002593990483063, + 0.3065198375066986, + 0.5442885286497666 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.1272330601314849, + 0.08379488379087519, + 0.139805351468521, + 0.1271873427962918, + 0.14062040919526486, + 0.14956246661244485, + 0.08442935773639929 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -1.1161667028973725, + -0.8051387589750121, + -0.4815385237156158, + -0.11454582945080283, + 0.4045734047510243, + 0.7947562805587343, + 1.158970405292459 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + 2.220815763720405, + -1.706679562075733, + -0.6126084438355661, + 0.3457398389523113, + 0.3457398389523113, + -1.0740637742042138, + -1.0740637742042138 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [ + { + "left_body_idx": 3, + "right_body_idx": 4, + "k": 195.45952227358612, + "L": 0.25131148221027 + }, + { + "left_body_idx": 5, + "right_body_idx": 6, + "k": 90.65607182396103, + "L": 0.13022230038488058 + } + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/2362/params.json b/scipy/complex_collision_plane/2362/params.json new file mode 100644 index 0000000000000000000000000000000000000000..8861771db287d29e00a8f68cfdb579fc8198edef --- /dev/null +++ b/scipy/complex_collision_plane/2362/params.json @@ -0,0 +1,67 @@ +{ + "setup_seed": 831256.4084715546, + "restitution": 0.9571812621488576, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "block", + "sphere", + "block", + "pair_left", + "pair_right", + "pair_left", + "pair_right" + ], + "masses": [ + 0.4915115801881481, + 1.1820744606279836, + 1.6441162282614838, + 0.7062128594117664, + 0.9002593990483063, + 0.3065198375066986, + 0.5442885286497666 + ], + "half_sizes": [ + 0.1272330601314849, + 0.08379488379087519, + 0.139805351468521, + 0.1271873427962918, + 0.14062040919526486, + 0.14956246661244485, + 0.08442935773639929 + ], + "x0": [ + -1.1161667028973725, + -0.8051387589750121, + -0.4815385237156158, + -0.11454582945080283, + 0.4045734047510243, + 0.7947562805587343, + 1.158970405292459 + ], + "v0": [ + 2.220815763720405, + -1.706679562075733, + -0.6126084438355661, + 0.3457398389523113, + 0.3457398389523113, + -1.0740637742042138, + -1.0740637742042138 + ], + "spring_a": [], + "spring_pair": [ + { + "left_body_idx": 3, + "right_body_idx": 4, + "k": 195.45952227358612, + "L": 0.25131148221027 + }, + { + "left_body_idx": 5, + "right_body_idx": 6, + "k": 90.65607182396103, + "L": 0.13022230038488058 + } + ] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/2362/simulation_code.py b/scipy/complex_collision_plane/2362/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..4507ee5ca600a9730e9f353c2bc1d2a54084d444 --- /dev/null +++ b/scipy/complex_collision_plane/2362/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.9572 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.9571812621488576 +k_contact = 5000.0 +body_kinds = ['block', 'sphere', 'block', 'pair_left', 'pair_right', 'pair_left', 'pair_right'] +masses = [0.4915115801881481, 1.1820744606279836, 1.6441162282614838, 0.7062128594117664, 0.9002593990483063, 0.3065198375066986, 0.5442885286497666] +half_sizes = [0.1272330601314849, 0.08379488379087519, 0.139805351468521, 0.1271873427962918, 0.14062040919526486, 0.14956246661244485, 0.08442935773639929] +x0_list = [-1.1161667028973725, -0.8051387589750121, -0.4815385237156158, -0.11454582945080283, 0.4045734047510243, 0.7947562805587343, 1.158970405292459] +v0_list = [2.220815763720405, -1.706679562075733, -0.6126084438355661, 0.3457398389523113, 0.3457398389523113, -1.0740637742042138, -1.0740637742042138] +spring_a = [] +spring_pair = [{'left_body_idx': 3, 'right_body_idx': 4, 'k': 195.45952227358612, 'L': 0.25131148221027}, {'left_body_idx': 5, 'right_body_idx': 6, 'k': 90.65607182396103, 'L': 0.13022230038488058}] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/2362/video.mp4 b/scipy/complex_collision_plane/2362/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..08d126928b162ac2abcd23525c886be46de78367 --- /dev/null +++ b/scipy/complex_collision_plane/2362/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:23f7c48229dd336438596fb12b4e951e646e454b0f0765598499631750d737db +size 19760 diff --git a/scipy/complex_collision_plane/2401/cot.json b/scipy/complex_collision_plane/2401/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..2105456b2335b1ab5aab0c2f83b2637125bcdef7 --- /dev/null +++ b/scipy/complex_collision_plane/2401/cot.json @@ -0,0 +1,229 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 6 rigid bodies are placed and given initial velocities. Composition: 2 sphere(s), 0 block(s), 2 block(s) with attached spring bumper(s), and 1 diatomic spring-pair(s). Newton's restitution e=0.796 is enforced on every hard contact. Total initial momentum p_0=-2.9219 kg \\cdot m/s, initial kinetic energy KE_0=7.3640 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "x_5", + "x_6", + "v_1", + "v_2", + "v_3", + "v_4", + "v_5", + "v_6" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{x}_5 = v_5", + "\\dot{x}_6 = v_6", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4", + "\\dot{v}_5 = (1/m_5) \\cdot \\SigmaF_5", + "\\dot{v}_6 = (1/m_6) \\cdot \\SigmaF_6" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "x_dot_5 = v_5", + "x_dot_6 = v_6", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4", + "v_dot_5 = (1/m_5) * SigmaF_5", + "v_dot_6 = (1/m_6) * SigmaF_6" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 6; total state dim = 2M = 12.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.7962 \\Rightarrow \\zeta = 0.0724 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.796", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-1.094, -0.473, 0.077, 0.36, 0.826, 1.129] m and velocities v_0 = [-1.704, 1.076, -2.131, 0.591, 0.591, 0.462] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = -2.9219 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 7.3640 J.", + "With e = 0.796, hard collisions retain a fraction e^2 = 0.634 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "2 attached-spring bumper(s): during contact the spring stores PE = \\tfrac{1}{2} k_a \\cdot compression^2 and converts it back to KE, providing a smooth (non-impulsive) momentum exchange whose duration depends on \\sqrt (m_eff / k_a).", + "1 diatomic spring-pair(s): each pair has a centre-of-mass mode (free translation; no internal force, no energy change) plus an internal vibration mode at \\omega = \\sqrt (k_p / \\mu_pair); collisions with external bodies excite both.", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 6):", + " body[0]: kind=spring_a, m=1.308 kg, half_size=0.135 m, x0=-1.094 m, v0=-1.704 m/s", + " body[1]: kind=spring_a, m=0.788 kg, half_size=0.108 m, x0=-0.473 m, v0=+1.076 m/s", + " body[2]: kind=sphere, m=1.899 kg, half_size=0.069 m, x0=0.077 m, v0=-2.131 m/s", + " body[3]: kind=pair_left, m=1.213 kg, half_size=0.114 m, x0=0.360 m, v0=+0.591 m/s", + " body[4]: kind=pair_right, m=1.835 kg, half_size=0.103 m, x0=0.826 m, v0=+0.591 m/s", + " body[5]: kind=sphere, m=1.526 kg, half_size=0.099 m, x0=1.129 m, v0=+0.462 m/s", + "Springs:", + " spring_a on body[0] side=+x: k=75.53 N/m, L_natural=0.279 m", + " spring_a on body[1] side=+x: k=123.77 N/m, L_natural=0.273 m", + " spring_pair (body[3], body[4]): k=44.55 N/m, L_natural=0.249 m" + ], + "parameters": { + "setup_seed": { + "value": 661579.5865698372, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.7961514306187746, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "spring_a", + "spring_a", + "sphere", + "pair_left", + "pair_right", + "sphere" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.3084953083361412, + 0.7875442498009034, + 1.8991498926756143, + 1.2128366608623946, + 1.8354740895137136, + 1.5257768908840565 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.13472996935980916, + 0.10795923888496335, + 0.06902277864077344, + 0.1135471193972501, + 0.10347704466888306, + 0.09939627463115064 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -1.0939809240674716, + -0.47259121783214253, + 0.07748082244270993, + 0.3600507204807335, + 0.8264412994960966, + 1.1293146187961303 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + -1.7035545613749377, + 1.0760792510533976, + -2.1313366772125475, + 0.5912994128469877, + 0.5912994128469877, + 0.46203974090320177 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [ + { + "body_idx": 0, + "k": 75.52664639807216, + "L": 0.27870049799055674, + "side": 1 + }, + { + "body_idx": 1, + "k": 123.77391168998261, + "L": 0.2730900227491152, + "side": 1 + } + ], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [ + { + "left_body_idx": 3, + "right_body_idx": 4, + "k": 44.54733808449359, + "L": 0.24936641494922998 + } + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/2401/params.json b/scipy/complex_collision_plane/2401/params.json new file mode 100644 index 0000000000000000000000000000000000000000..35936e158d26e42233ca7596b8770a6fed441152 --- /dev/null +++ b/scipy/complex_collision_plane/2401/params.json @@ -0,0 +1,69 @@ +{ + "setup_seed": 661579.5865698372, + "restitution": 0.7961514306187746, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "spring_a", + "spring_a", + "sphere", + "pair_left", + "pair_right", + "sphere" + ], + "masses": [ + 1.3084953083361412, + 0.7875442498009034, + 1.8991498926756143, + 1.2128366608623946, + 1.8354740895137136, + 1.5257768908840565 + ], + "half_sizes": [ + 0.13472996935980916, + 0.10795923888496335, + 0.06902277864077344, + 0.1135471193972501, + 0.10347704466888306, + 0.09939627463115064 + ], + "x0": [ + -1.0939809240674716, + -0.47259121783214253, + 0.07748082244270993, + 0.3600507204807335, + 0.8264412994960966, + 1.1293146187961303 + ], + "v0": [ + -1.7035545613749377, + 1.0760792510533976, + -2.1313366772125475, + 0.5912994128469877, + 0.5912994128469877, + 0.46203974090320177 + ], + "spring_a": [ + { + "body_idx": 0, + "k": 75.52664639807216, + "L": 0.27870049799055674, + "side": 1 + }, + { + "body_idx": 1, + "k": 123.77391168998261, + "L": 0.2730900227491152, + "side": 1 + } + ], + "spring_pair": [ + { + "left_body_idx": 3, + "right_body_idx": 4, + "k": 44.54733808449359, + "L": 0.24936641494922998 + } + ] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/2401/simulation_code.py b/scipy/complex_collision_plane/2401/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e7c82f396d2f3fe0215101a93b26460d960ce630 --- /dev/null +++ b/scipy/complex_collision_plane/2401/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.7962 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.7961514306187746 +k_contact = 5000.0 +body_kinds = ['spring_a', 'spring_a', 'sphere', 'pair_left', 'pair_right', 'sphere'] +masses = [1.3084953083361412, 0.7875442498009034, 1.8991498926756143, 1.2128366608623946, 1.8354740895137136, 1.5257768908840565] +half_sizes = [0.13472996935980916, 0.10795923888496335, 0.06902277864077344, 0.1135471193972501, 0.10347704466888306, 0.09939627463115064] +x0_list = [-1.0939809240674716, -0.47259121783214253, 0.07748082244270993, 0.3600507204807335, 0.8264412994960966, 1.1293146187961303] +v0_list = [-1.7035545613749377, 1.0760792510533976, -2.1313366772125475, 0.5912994128469877, 0.5912994128469877, 0.46203974090320177] +spring_a = [{'body_idx': 0, 'k': 75.52664639807216, 'L': 0.27870049799055674, 'side': 1}, {'body_idx': 1, 'k': 123.77391168998261, 'L': 0.2730900227491152, 'side': 1}] +spring_pair = [{'left_body_idx': 3, 'right_body_idx': 4, 'k': 44.54733808449359, 'L': 0.24936641494922998}] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/2401/video.mp4 b/scipy/complex_collision_plane/2401/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d52ed43eab12c69f3c06d2470b1698b546e76477 --- /dev/null +++ b/scipy/complex_collision_plane/2401/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4a8d92ab4232b6ae98722ef65f9946817900491bb9b90c34ab5bd05a45bcace6 +size 20340 diff --git a/scipy/complex_collision_plane/2421/cot.json b/scipy/complex_collision_plane/2421/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..1f7f8c4814638c25a14a66046bcfb44648913ac5 --- /dev/null +++ b/scipy/complex_collision_plane/2421/cot.json @@ -0,0 +1,260 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 8 rigid bodies are placed and given initial velocities. Composition: 0 sphere(s), 2 block(s), 2 block(s) with attached spring bumper(s), and 2 diatomic spring-pair(s). Newton's restitution e=0.475 is enforced on every hard contact. Total initial momentum p_0=8.6066 kg \\cdot m/s, initial kinetic energy KE_0=15.8885 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "x_5", + "x_6", + "x_7", + "x_8", + "v_1", + "v_2", + "v_3", + "v_4", + "v_5", + "v_6", + "v_7", + "v_8" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{x}_5 = v_5", + "\\dot{x}_6 = v_6", + "\\dot{x}_7 = v_7", + "\\dot{x}_8 = v_8", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4", + "\\dot{v}_5 = (1/m_5) \\cdot \\SigmaF_5", + "\\dot{v}_6 = (1/m_6) \\cdot \\SigmaF_6", + "\\dot{v}_7 = (1/m_7) \\cdot \\SigmaF_7", + "\\dot{v}_8 = (1/m_8) \\cdot \\SigmaF_8" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "x_dot_5 = v_5", + "x_dot_6 = v_6", + "x_dot_7 = v_7", + "x_dot_8 = v_8", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4", + "v_dot_5 = (1/m_5) * SigmaF_5", + "v_dot_6 = (1/m_6) * SigmaF_6", + "v_dot_7 = (1/m_7) * SigmaF_7", + "v_dot_8 = (1/m_8) * SigmaF_8" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 8; total state dim = 2M = 16.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.4751 \\Rightarrow \\zeta = 0.2305 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.475", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-1.32, -0.992, -0.709, -0.267, 0.066, 0.521, 0.822, 1.338] m and velocities v_0 = [2.186, 2.052, -1.612, -1.612, 1.805, 1.805, 0.514, -0.591] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = +8.6066 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 15.8885 J.", + "With e = 0.475, hard collisions retain a fraction e^2 = 0.226 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "2 attached-spring bumper(s): during contact the spring stores PE = \\tfrac{1}{2} k_a \\cdot compression^2 and converts it back to KE, providing a smooth (non-impulsive) momentum exchange whose duration depends on \\sqrt (m_eff / k_a).", + "2 diatomic spring-pair(s): each pair has a centre-of-mass mode (free translation; no internal force, no energy change) plus an internal vibration mode at \\omega = \\sqrt (k_p / \\mu_pair); collisions with external bodies excite both.", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 8):", + " body[0]: kind=block, m=1.564 kg, half_size=0.132 m, x0=-1.320 m, v0=+2.186 m/s", + " body[1]: kind=block, m=1.994 kg, half_size=0.095 m, x0=-0.992 m, v0=+2.052 m/s", + " body[2]: kind=pair_left, m=1.973 kg, half_size=0.089 m, x0=-0.709 m, v0=-1.612 m/s", + " body[3]: kind=pair_right, m=0.446 kg, half_size=0.098 m, x0=-0.267 m, v0=-1.612 m/s", + " body[4]: kind=pair_left, m=1.474 kg, half_size=0.135 m, x0=0.066 m, v0=+1.805 m/s", + " body[5]: kind=pair_right, m=1.334 kg, half_size=0.082 m, x0=0.521 m, v0=+1.805 m/s", + " body[6]: kind=spring_a, m=0.748 kg, half_size=0.119 m, x0=0.822 m, v0=+0.514 m/s", + " body[7]: kind=spring_a, m=0.773 kg, half_size=0.114 m, x0=1.338 m, v0=-0.591 m/s", + "Springs:", + " spring_a on body[6] side=+x: k=94.99 N/m, L_natural=0.182 m", + " spring_a on body[7] side=+x: k=47.45 N/m, L_natural=0.145 m", + " spring_pair (body[2], body[3]): k=37.16 N/m, L_natural=0.255 m", + " spring_pair (body[4], body[5]): k=57.64 N/m, L_natural=0.237 m" + ], + "parameters": { + "setup_seed": { + "value": 769208.1810429055, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.475090124657752, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "block", + "block", + "pair_left", + "pair_right", + "pair_left", + "pair_right", + "spring_a", + "spring_a" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.563997133634667, + 1.9940112486538935, + 1.973097763181906, + 0.4461226889064944, + 1.474014422087024, + 1.3339892755846363, + 0.7484101809907852, + 0.772536404846361 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.13222461075363273, + 0.09525635681844581, + 0.08865576229391608, + 0.09831948790756859, + 0.13462052752403897, + 0.08241576385422857, + 0.11945571067973279, + 0.11427562057474204 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -1.319919827179656, + -0.992438859607577, + -0.7085267404952149, + -0.26688716665634615, + 0.06605284877526163, + 0.5205704384009795, + 0.822441912934941, + 1.337868817358547 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + 2.18637045320635, + 2.0516909437173068, + -1.6122883113301458, + -1.6122883113301458, + 1.8052143471831719, + 1.8052143471831719, + 0.5135435113693712, + -0.5914620554974659 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [ + { + "body_idx": 6, + "k": 94.98786339678254, + "L": 0.18169557316913132, + "side": 1 + }, + { + "body_idx": 7, + "k": 47.45309745122669, + "L": 0.14456065576738647, + "side": 1 + } + ], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [ + { + "left_body_idx": 2, + "right_body_idx": 3, + "k": 37.15766192785136, + "L": 0.254664323637384 + }, + { + "left_body_idx": 4, + "right_body_idx": 5, + "k": 57.643740571926564, + "L": 0.2374812982474501 + } + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/2421/params.json b/scipy/complex_collision_plane/2421/params.json new file mode 100644 index 0000000000000000000000000000000000000000..e9bc71422a7a77076bd11f3a6f09ae3c435928cf --- /dev/null +++ b/scipy/complex_collision_plane/2421/params.json @@ -0,0 +1,85 @@ +{ + "setup_seed": 769208.1810429055, + "restitution": 0.475090124657752, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "block", + "block", + "pair_left", + "pair_right", + "pair_left", + "pair_right", + "spring_a", + "spring_a" + ], + "masses": [ + 1.563997133634667, + 1.9940112486538935, + 1.973097763181906, + 0.4461226889064944, + 1.474014422087024, + 1.3339892755846363, + 0.7484101809907852, + 0.772536404846361 + ], + "half_sizes": [ + 0.13222461075363273, + 0.09525635681844581, + 0.08865576229391608, + 0.09831948790756859, + 0.13462052752403897, + 0.08241576385422857, + 0.11945571067973279, + 0.11427562057474204 + ], + "x0": [ + -1.319919827179656, + -0.992438859607577, + -0.7085267404952149, + -0.26688716665634615, + 0.06605284877526163, + 0.5205704384009795, + 0.822441912934941, + 1.337868817358547 + ], + "v0": [ + 2.18637045320635, + 2.0516909437173068, + -1.6122883113301458, + -1.6122883113301458, + 1.8052143471831719, + 1.8052143471831719, + 0.5135435113693712, + -0.5914620554974659 + ], + "spring_a": [ + { + "body_idx": 6, + "k": 94.98786339678254, + "L": 0.18169557316913132, + "side": 1 + }, + { + "body_idx": 7, + "k": 47.45309745122669, + "L": 0.14456065576738647, + "side": 1 + } + ], + "spring_pair": [ + { + "left_body_idx": 2, + "right_body_idx": 3, + "k": 37.15766192785136, + "L": 0.254664323637384 + }, + { + "left_body_idx": 4, + "right_body_idx": 5, + "k": 57.643740571926564, + "L": 0.2374812982474501 + } + ] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/2421/simulation_code.py b/scipy/complex_collision_plane/2421/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..89d8c2e9a43cc70b8807d6e3cbc959885ed16b8b --- /dev/null +++ b/scipy/complex_collision_plane/2421/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.4751 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.475090124657752 +k_contact = 5000.0 +body_kinds = ['block', 'block', 'pair_left', 'pair_right', 'pair_left', 'pair_right', 'spring_a', 'spring_a'] +masses = [1.563997133634667, 1.9940112486538935, 1.973097763181906, 0.4461226889064944, 1.474014422087024, 1.3339892755846363, 0.7484101809907852, 0.772536404846361] +half_sizes = [0.13222461075363273, 0.09525635681844581, 0.08865576229391608, 0.09831948790756859, 0.13462052752403897, 0.08241576385422857, 0.11945571067973279, 0.11427562057474204] +x0_list = [-1.319919827179656, -0.992438859607577, -0.7085267404952149, -0.26688716665634615, 0.06605284877526163, 0.5205704384009795, 0.822441912934941, 1.337868817358547] +v0_list = [2.18637045320635, 2.0516909437173068, -1.6122883113301458, -1.6122883113301458, 1.8052143471831719, 1.8052143471831719, 0.5135435113693712, -0.5914620554974659] +spring_a = [{'body_idx': 6, 'k': 94.98786339678254, 'L': 0.18169557316913132, 'side': 1}, {'body_idx': 7, 'k': 47.45309745122669, 'L': 0.14456065576738647, 'side': 1}] +spring_pair = [{'left_body_idx': 2, 'right_body_idx': 3, 'k': 37.15766192785136, 'L': 0.254664323637384}, {'left_body_idx': 4, 'right_body_idx': 5, 'k': 57.643740571926564, 'L': 0.2374812982474501}] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/2421/video.mp4 b/scipy/complex_collision_plane/2421/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e0ff9f91e682fe1c2f0aa6db6a869aa581f6ac19 --- /dev/null +++ b/scipy/complex_collision_plane/2421/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f788b61a78d9f2c5727c0bb23da116b7ebcd1f51a9a87091113a5cce93f1b3a0 +size 23510 diff --git a/scipy/complex_collision_plane/363/cot.json b/scipy/complex_collision_plane/363/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..93b81e293a28ad1a6997db27982c71130d687cbf --- /dev/null +++ b/scipy/complex_collision_plane/363/cot.json @@ -0,0 +1,260 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 8 rigid bodies are placed and given initial velocities. Composition: 0 sphere(s), 1 block(s), 1 block(s) with attached spring bumper(s), and 3 diatomic spring-pair(s). Newton's restitution e=0.551 is enforced on every hard contact. Total initial momentum p_0=-2.6410 kg \\cdot m/s, initial kinetic energy KE_0=3.0774 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "x_5", + "x_6", + "x_7", + "x_8", + "v_1", + "v_2", + "v_3", + "v_4", + "v_5", + "v_6", + "v_7", + "v_8" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{x}_5 = v_5", + "\\dot{x}_6 = v_6", + "\\dot{x}_7 = v_7", + "\\dot{x}_8 = v_8", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4", + "\\dot{v}_5 = (1/m_5) \\cdot \\SigmaF_5", + "\\dot{v}_6 = (1/m_6) \\cdot \\SigmaF_6", + "\\dot{v}_7 = (1/m_7) \\cdot \\SigmaF_7", + "\\dot{v}_8 = (1/m_8) \\cdot \\SigmaF_8" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "x_dot_5 = v_5", + "x_dot_6 = v_6", + "x_dot_7 = v_7", + "x_dot_8 = v_8", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4", + "v_dot_5 = (1/m_5) * SigmaF_5", + "v_dot_6 = (1/m_6) * SigmaF_6", + "v_dot_7 = (1/m_7) * SigmaF_7", + "v_dot_8 = (1/m_8) * SigmaF_8" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 8; total state dim = 2M = 16.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.5506 \\Rightarrow \\zeta = 0.1866 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.551", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-1.456, -1.108, -0.71, -0.229, 0.149, 0.597, 0.957, 1.405] m and velocities v_0 = [-1.066, -0.492, -0.314, -0.314, 1.246, 1.246, -0.729, -0.729] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = -2.6410 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 3.0774 J.", + "With e = 0.551, hard collisions retain a fraction e^2 = 0.303 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "1 attached-spring bumper(s): during contact the spring stores PE = \\tfrac{1}{2} k_a \\cdot compression^2 and converts it back to KE, providing a smooth (non-impulsive) momentum exchange whose duration depends on \\sqrt (m_eff / k_a).", + "3 diatomic spring-pair(s): each pair has a centre-of-mass mode (free translation; no internal force, no energy change) plus an internal vibration mode at \\omega = \\sqrt (k_p / \\mu_pair); collisions with external bodies excite both.", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 8):", + " body[0]: kind=spring_a, m=1.506 kg, half_size=0.110 m, x0=-1.456 m, v0=-1.066 m/s", + " body[1]: kind=block, m=1.292 kg, half_size=0.138 m, x0=-1.108 m, v0=-0.492 m/s", + " body[2]: kind=pair_left, m=0.555 kg, half_size=0.160 m, x0=-0.710 m, v0=-0.314 m/s", + " body[3]: kind=pair_right, m=1.415 kg, half_size=0.148 m, x0=-0.229 m, v0=-0.314 m/s", + " body[4]: kind=pair_left, m=0.448 kg, half_size=0.130 m, x0=0.149 m, v0=+1.246 m/s", + " body[5]: kind=pair_right, m=1.217 kg, half_size=0.134 m, x0=0.597 m, v0=+1.246 m/s", + " body[6]: kind=pair_left, m=1.616 kg, half_size=0.126 m, x0=0.957 m, v0=-0.729 m/s", + " body[7]: kind=pair_right, m=0.934 kg, half_size=0.160 m, x0=1.405 m, v0=-0.729 m/s", + "Springs:", + " spring_a on body[0] side=-x: k=136.80 N/m, L_natural=0.268 m", + " spring_pair (body[2], body[3]): k=177.62 N/m, L_natural=0.173 m", + " spring_pair (body[4], body[5]): k=150.15 N/m, L_natural=0.184 m", + " spring_pair (body[6], body[7]): k=68.43 N/m, L_natural=0.162 m" + ], + "parameters": { + "setup_seed": { + "value": 637576.5588404658, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.5505923085609602, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "spring_a", + "block", + "pair_left", + "pair_right", + "pair_left", + "pair_right", + "pair_left", + "pair_right" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.5057844495656227, + 1.2918652052150899, + 0.5552684896368043, + 1.4151873435497162, + 0.4483266804256617, + 1.21692633318589, + 1.616322906926178, + 0.9335314574546227 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.10963983501881067, + 0.1384101983986189, + 0.1596168950882081, + 0.14797935351604452, + 0.13041240643873137, + 0.13393267243921697, + 0.1263040530700948, + 0.15996557566438074 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -1.4557805415975862, + -1.1077305081801567, + -0.7097034146933296, + -0.2293701831573296, + 0.1490215767974461, + 0.5968848665164337, + 0.9571215920257454, + 1.4054548009520167 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + -1.0655182640648642, + -0.4917304738090751, + -0.3138737351671559, + -0.3138737351671559, + 1.2459998337567313, + 1.2459998337567313, + -0.7285593599475053, + -0.7285593599475053 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [ + { + "body_idx": 0, + "k": 136.7970190836381, + "L": 0.267871613605264, + "side": -1 + } + ], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [ + { + "left_body_idx": 2, + "right_body_idx": 3, + "k": 177.6227760887989, + "L": 0.172736982931747 + }, + { + "left_body_idx": 4, + "right_body_idx": 5, + "k": 150.14521877565977, + "L": 0.1835182108410393 + }, + { + "left_body_idx": 6, + "right_body_idx": 7, + "k": 68.42647336468994, + "L": 0.1620635801917958 + } + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/363/params.json b/scipy/complex_collision_plane/363/params.json new file mode 100644 index 0000000000000000000000000000000000000000..26ce1208469479ee89f6966c89f90b0ea2c94e70 --- /dev/null +++ b/scipy/complex_collision_plane/363/params.json @@ -0,0 +1,85 @@ +{ + "setup_seed": 637576.5588404658, + "restitution": 0.5505923085609602, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "spring_a", + "block", + "pair_left", + "pair_right", + "pair_left", + "pair_right", + "pair_left", + "pair_right" + ], + "masses": [ + 1.5057844495656227, + 1.2918652052150899, + 0.5552684896368043, + 1.4151873435497162, + 0.4483266804256617, + 1.21692633318589, + 1.616322906926178, + 0.9335314574546227 + ], + "half_sizes": [ + 0.10963983501881067, + 0.1384101983986189, + 0.1596168950882081, + 0.14797935351604452, + 0.13041240643873137, + 0.13393267243921697, + 0.1263040530700948, + 0.15996557566438074 + ], + "x0": [ + -1.4557805415975862, + -1.1077305081801567, + -0.7097034146933296, + -0.2293701831573296, + 0.1490215767974461, + 0.5968848665164337, + 0.9571215920257454, + 1.4054548009520167 + ], + "v0": [ + -1.0655182640648642, + -0.4917304738090751, + -0.3138737351671559, + -0.3138737351671559, + 1.2459998337567313, + 1.2459998337567313, + -0.7285593599475053, + -0.7285593599475053 + ], + "spring_a": [ + { + "body_idx": 0, + "k": 136.7970190836381, + "L": 0.267871613605264, + "side": -1 + } + ], + "spring_pair": [ + { + "left_body_idx": 2, + "right_body_idx": 3, + "k": 177.6227760887989, + "L": 0.172736982931747 + }, + { + "left_body_idx": 4, + "right_body_idx": 5, + "k": 150.14521877565977, + "L": 0.1835182108410393 + }, + { + "left_body_idx": 6, + "right_body_idx": 7, + "k": 68.42647336468994, + "L": 0.1620635801917958 + } + ] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/363/simulation_code.py b/scipy/complex_collision_plane/363/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..9cc3b23e75b14cad9017d5889e9e88b10df888b2 --- /dev/null +++ b/scipy/complex_collision_plane/363/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.5506 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.5505923085609602 +k_contact = 5000.0 +body_kinds = ['spring_a', 'block', 'pair_left', 'pair_right', 'pair_left', 'pair_right', 'pair_left', 'pair_right'] +masses = [1.5057844495656227, 1.2918652052150899, 0.5552684896368043, 1.4151873435497162, 0.4483266804256617, 1.21692633318589, 1.616322906926178, 0.9335314574546227] +half_sizes = [0.10963983501881067, 0.1384101983986189, 0.1596168950882081, 0.14797935351604452, 0.13041240643873137, 0.13393267243921697, 0.1263040530700948, 0.15996557566438074] +x0_list = [-1.4557805415975862, -1.1077305081801567, -0.7097034146933296, -0.2293701831573296, 0.1490215767974461, 0.5968848665164337, 0.9571215920257454, 1.4054548009520167] +v0_list = [-1.0655182640648642, -0.4917304738090751, -0.3138737351671559, -0.3138737351671559, 1.2459998337567313, 1.2459998337567313, -0.7285593599475053, -0.7285593599475053] +spring_a = [{'body_idx': 0, 'k': 136.7970190836381, 'L': 0.267871613605264, 'side': -1}] +spring_pair = [{'left_body_idx': 2, 'right_body_idx': 3, 'k': 177.6227760887989, 'L': 0.172736982931747}, {'left_body_idx': 4, 'right_body_idx': 5, 'k': 150.14521877565977, 'L': 0.1835182108410393}, {'left_body_idx': 6, 'right_body_idx': 7, 'k': 68.42647336468994, 'L': 0.1620635801917958}] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/363/video.mp4 b/scipy/complex_collision_plane/363/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..21397554292f3384e1f5c29cc039875819c5dbd1 --- /dev/null +++ b/scipy/complex_collision_plane/363/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5ad60e5ddd5bfc1a911e7b8fb48ae8a34d14e18cc5ec657f45a9b87cdf4f5597 +size 20203 diff --git a/scipy/complex_collision_plane/471/cot.json b/scipy/complex_collision_plane/471/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ce4cfd771f5371d694720a352cfd334f9b29a863 --- /dev/null +++ b/scipy/complex_collision_plane/471/cot.json @@ -0,0 +1,234 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 7 rigid bodies are placed and given initial velocities. Composition: 1 sphere(s), 3 block(s), 1 block(s) with attached spring bumper(s), and 1 diatomic spring-pair(s). Newton's restitution e=0.458 is enforced on every hard contact. Total initial momentum p_0=12.0414 kg \\cdot m/s, initial kinetic energy KE_0=11.5525 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "x_3", + "x_4", + "x_5", + "x_6", + "x_7", + "v_1", + "v_2", + "v_3", + "v_4", + "v_5", + "v_6", + "v_7" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{x}_3 = v_3", + "\\dot{x}_4 = v_4", + "\\dot{x}_5 = v_5", + "\\dot{x}_6 = v_6", + "\\dot{x}_7 = v_7", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2", + "\\dot{v}_3 = (1/m_3) \\cdot \\SigmaF_3", + "\\dot{v}_4 = (1/m_4) \\cdot \\SigmaF_4", + "\\dot{v}_5 = (1/m_5) \\cdot \\SigmaF_5", + "\\dot{v}_6 = (1/m_6) \\cdot \\SigmaF_6", + "\\dot{v}_7 = (1/m_7) \\cdot \\SigmaF_7" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "x_dot_3 = v_3", + "x_dot_4 = v_4", + "x_dot_5 = v_5", + "x_dot_6 = v_6", + "x_dot_7 = v_7", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2", + "v_dot_3 = (1/m_3) * SigmaF_3", + "v_dot_4 = (1/m_4) * SigmaF_4", + "v_dot_5 = (1/m_5) * SigmaF_5", + "v_dot_6 = (1/m_6) * SigmaF_6", + "v_dot_7 = (1/m_7) * SigmaF_7" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 7; total state dim = 2M = 14.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.4576 \\Rightarrow \\zeta = 0.2415 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.458", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-1.14, -0.832, -0.314, 0.025, 0.428, 0.717, 1.054] m and velocities v_0 = [0.951, 1.613, 2.109, -0.031, -0.031, 1.361, 2.481] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = +12.0414 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 11.5525 J.", + "With e = 0.458, hard collisions retain a fraction e^2 = 0.209 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "1 attached-spring bumper(s): during contact the spring stores PE = \\tfrac{1}{2} k_a \\cdot compression^2 and converts it back to KE, providing a smooth (non-impulsive) momentum exchange whose duration depends on \\sqrt (m_eff / k_a).", + "1 diatomic spring-pair(s): each pair has a centre-of-mass mode (free translation; no internal force, no energy change) plus an internal vibration mode at \\omega = \\sqrt (k_p / \\mu_pair); collisions with external bodies excite both.", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 7):", + " body[0]: kind=sphere, m=1.437 kg, half_size=0.060 m, x0=-1.140 m, v0=+0.951 m/s", + " body[1]: kind=spring_a, m=0.493 kg, half_size=0.149 m, x0=-0.832 m, v0=+1.613 m/s", + " body[2]: kind=block, m=1.626 kg, half_size=0.145 m, x0=-0.314 m, v0=+2.109 m/s", + " body[3]: kind=pair_left, m=1.786 kg, half_size=0.095 m, x0=0.025 m, v0=-0.031 m/s", + " body[4]: kind=pair_right, m=0.902 kg, half_size=0.099 m, x0=0.428 m, v0=-0.031 m/s", + " body[5]: kind=block, m=1.919 kg, half_size=0.090 m, x0=0.717 m, v0=+1.361 m/s", + " body[6]: kind=block, m=1.580 kg, half_size=0.147 m, x0=1.054 m, v0=+2.481 m/s", + "Springs:", + " spring_a on body[1] side=+x: k=174.89 N/m, L_natural=0.124 m", + " spring_pair (body[3], body[4]): k=177.83 N/m, L_natural=0.209 m" + ], + "parameters": { + "setup_seed": { + "value": 487761.3538615443, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.4576048697369792, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "sphere", + "spring_a", + "block", + "pair_left", + "pair_right", + "block", + "block" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 1.4368560307936091, + 0.4932672673697032, + 1.626058591806971, + 1.7860732221145335, + 0.9021347599861849, + 1.9192109440159624, + 1.5802256241270725 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.060244345553176544, + 0.14855804766834846, + 0.1449477329553301, + 0.09479499965971452, + 0.09909708161338325, + 0.09012977458179983, + 0.14656849533187838 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -1.1403223817353645, + -0.8315199885138393, + -0.31429951667294675, + 0.02544321594209764, + 0.42807310584780245, + 0.7172999620429854, + 1.0539982319566636 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + 0.951250186961385, + 1.6128394391941505, + 2.109455332017424, + -0.031354629786611854, + -0.031354629786611854, + 1.3613654036750038, + 2.4809643694742043 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [ + { + "body_idx": 1, + "k": 174.89092387116105, + "L": 0.12371469121721426, + "side": 1 + } + ], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [ + { + "left_body_idx": 3, + "right_body_idx": 4, + "k": 177.82683662382487, + "L": 0.20873780863260724 + } + ], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/471/params.json b/scipy/complex_collision_plane/471/params.json new file mode 100644 index 0000000000000000000000000000000000000000..f2ccdd4af1b4a0a8861dfb88388adad0c4db6b9a --- /dev/null +++ b/scipy/complex_collision_plane/471/params.json @@ -0,0 +1,68 @@ +{ + "setup_seed": 487761.3538615443, + "restitution": 0.4576048697369792, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "sphere", + "spring_a", + "block", + "pair_left", + "pair_right", + "block", + "block" + ], + "masses": [ + 1.4368560307936091, + 0.4932672673697032, + 1.626058591806971, + 1.7860732221145335, + 0.9021347599861849, + 1.9192109440159624, + 1.5802256241270725 + ], + "half_sizes": [ + 0.060244345553176544, + 0.14855804766834846, + 0.1449477329553301, + 0.09479499965971452, + 0.09909708161338325, + 0.09012977458179983, + 0.14656849533187838 + ], + "x0": [ + -1.1403223817353645, + -0.8315199885138393, + -0.31429951667294675, + 0.02544321594209764, + 0.42807310584780245, + 0.7172999620429854, + 1.0539982319566636 + ], + "v0": [ + 0.951250186961385, + 1.6128394391941505, + 2.109455332017424, + -0.031354629786611854, + -0.031354629786611854, + 1.3613654036750038, + 2.4809643694742043 + ], + "spring_a": [ + { + "body_idx": 1, + "k": 174.89092387116105, + "L": 0.12371469121721426, + "side": 1 + } + ], + "spring_pair": [ + { + "left_body_idx": 3, + "right_body_idx": 4, + "k": 177.82683662382487, + "L": 0.20873780863260724 + } + ] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/471/simulation_code.py b/scipy/complex_collision_plane/471/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..85e49da01346c0d930187c30a8d20f9938af84eb --- /dev/null +++ b/scipy/complex_collision_plane/471/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.4576 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.4576048697369792 +k_contact = 5000.0 +body_kinds = ['sphere', 'spring_a', 'block', 'pair_left', 'pair_right', 'block', 'block'] +masses = [1.4368560307936091, 0.4932672673697032, 1.626058591806971, 1.7860732221145335, 0.9021347599861849, 1.9192109440159624, 1.5802256241270725] +half_sizes = [0.060244345553176544, 0.14855804766834846, 0.1449477329553301, 0.09479499965971452, 0.09909708161338325, 0.09012977458179983, 0.14656849533187838] +x0_list = [-1.1403223817353645, -0.8315199885138393, -0.31429951667294675, 0.02544321594209764, 0.42807310584780245, 0.7172999620429854, 1.0539982319566636] +v0_list = [0.951250186961385, 1.6128394391941505, 2.109455332017424, -0.031354629786611854, -0.031354629786611854, 1.3613654036750038, 2.4809643694742043] +spring_a = [{'body_idx': 1, 'k': 174.89092387116105, 'L': 0.12371469121721426, 'side': 1}] +spring_pair = [{'left_body_idx': 3, 'right_body_idx': 4, 'k': 177.82683662382487, 'L': 0.20873780863260724}] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/471/video.mp4 b/scipy/complex_collision_plane/471/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..14e6b9d7051064a1f76a522855ac04724e142251 --- /dev/null +++ b/scipy/complex_collision_plane/471/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b8b2853f7f549be5a2d9783211c43442cf37fa77c6aab7e5bae390c7e67f0418 +size 17172 diff --git a/scipy/complex_collision_plane/489/cot.json b/scipy/complex_collision_plane/489/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..bee36d904e58832bf5633cc93f91d4021030442b --- /dev/null +++ b/scipy/complex_collision_plane/489/cot.json @@ -0,0 +1,155 @@ +{ + "simulation": "complex_collision_plane", + "physical_observation": "On a frictionless 1-D plane bounded by walls at x=-4.00 m and x=4.00 m, 2 rigid bodies are placed and given initial velocities. Composition: 2 sphere(s), 0 block(s), 0 block(s) with attached spring bumper(s), and 0 diatomic spring-pair(s). Newton's restitution e=0.870 is enforced on every hard contact. Total initial momentum p_0=-3.4847 kg \\cdot m/s, initial kinetic energy KE_0=3.3608 J.", + "physical_law": { + "name": "Newton's Laws + Hooke's Law + Newton's Law of Restitution", + "statement": "Each rigid body obeys Newton's 2nd Law (\\SigmaF = ma). Pairwise impulsive contacts obey Newton's law of restitution v_rel_after = -e \\cdot v_rel_before, leaving total momentum exactly conserved and removing fraction (1-e^2) of the relative kinetic energy in the centre-of-mass frame. Spring contacts (attached bumper, internal diatomic) obey Hooke's law F = -k \\Deltax and exchange KE \\leftrightarrow PE continuously.", + "formula": "m_i \\ddot{x}_i = \\Sigma_j F_{ij}; hard contact: |F| = k_c \\cdot d, e = \\exp(-\\pi \\zeta / \\sqrt (1-\\zeta^2)); spring contact: F = -k \\cdot \\Deltax; conservation: \\Sigma m_i v_i = const between collisions; \\DeltaKE_pair = -(1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 per pairwise impulsive contact." + }, + "ode_system": { + "state_variables": [ + "x_1", + "x_2", + "v_1", + "v_2" + ], + "equations_latex": [ + "\\dot{x}_1 = v_1", + "\\dot{x}_2 = v_2", + "\\dot{v}_1 = (1/m_1) \\cdot \\SigmaF_1", + "\\dot{v}_2 = (1/m_2) \\cdot \\SigmaF_2" + ], + "equations_python": [ + "x_dot_1 = v_1", + "x_dot_2 = v_2", + "v_dot_1 = (1/m_1) * SigmaF_1", + "v_dot_2 = (1/m_2) * SigmaF_2" + ] + }, + "derivation_steps": [ + "Generalised coordinates: x_i (1-D position) for each body i; state per body is (x_i, v_i).", + "Number of moving bodies M = 2; total state dim = 2M = 4.", + "Newton's 2nd Law for each body: m_i \\ddot{x}_i = \\SigmaF_i, where F_i is the sum of all contact forces acting on body i.", + "", + "Contact forces (between pairs of bodies, using one-sided penalty):", + " (a) Hard contact (sphere-sphere, sphere-block, block-block, etc.). When body L's right face overlaps body R's left face by amount d > 0:", + " F_normal = k_c \\cdot d", + " F_damping = -c \\cdot v_rel, where v_rel = v_R - v_L,", + " c = 2 \\zeta \\sqrt (m_eff \\cdot k_c), m_eff = m_L \\cdot m_R / (m_L + m_R),", + " and \\zeta is chosen so e = \\exp(-\\pi \\zeta / \\sqrt (1 - \\zeta^2)).", + " The total normal+damping force pushes the two bodies apart, transferring momentum while dissipating KE in proportion to (1 - e^2).", + " (b) Spring-mediated contact (spring_a's spring side). While the spring is compressed:", + " F_spring = k_a \\cdot compression (Hooke's law),", + " pushing the bodies apart. If compression exceeds the natural length L_a, an additional hard-contact term (a) is added on top.", + " (c) Spring-pair internal force (always active):", + " F = -k_p \\cdot (separation - rest_separation), acting equal-and-opposite on the two bodies. The pair has CoM motion plus an internal vibration mode at \\omega_vib = \\sqrt (k_p / \\mu_pair), where \\mu_pair = m_L \\cdot m_R / (m_L + m_R).", + " (d) Wall contact (left and right walls): one-sided penalty spring identical to (a) with the wall acting as an infinite-mass partner.", + "", + "Conservation laws:", + " Momentum: P = \\Sigma m_i v_i is conserved between collisions; impulsive forces are internal (Newton's 3rd) so the lab-frame total momentum is exactly preserved.", + " Energy: With e=1 the system is conservative; with e<1, each pairwise impulsive collision removes (1 - e^2) \\cdot \\tfrac{1}{2} \\mu v_rel^2 of kinetic energy from the relative motion in the centre-of-mass frame; the CoM kinetic energy is unaffected.", + "", + "For this run: e = 0.8703 \\Rightarrow \\zeta = 0.0442 \\Rightarrow damping coefficient c = 2 \\zeta \\sqrt (m_eff \\cdot k_c)." + ], + "numerical_method": { + "solver": "LSODA (adaptive multi-method: BDF for stiff regions, Adams for non-stiff)", + "tolerance": "rtol=1e-7, atol=1e-9, max_step=0.005", + "justification": "Penalty contacts are stiff (k_contact = 5000 N/m \\rightarrow contact timescale ~10 ms); LSODA auto-switches to BDF during contact and back to Adams during free flight. max_step=5 ms guarantees several integrator steps per contact." + }, + "code_validation": { + "ode_source": "Newton's 2nd law + Hooke's law for explicit springs + penalty contact model", + "momentum_check": "\\Sigmam_iv_i\\Sigma exactly conserved between contacts (no external horizontal force)", + "energy_check": "\\DeltaKE per impulsive collision \\approx -(1 - e^2) \\cdot \\tfrac{1}{2} \\cdot \\mu \\cdot v_rel^2 with e=0.870", + "physical_plausibility": true + }, + "expected_behavior": [ + "At t = 0, bodies have positions x_0 = [-0.132, 0.14] m and velocities v_0 = [-2.225, -1.566] m/s.", + "Total initial linear momentum P_0 = \\Sigma m_i v_i = -3.4847 kg \\cdot m/s (this value is exactly conserved between collisions).", + "Total initial kinetic energy KE_0 = \\tfrac{1}{2} \\Sigma m_i v_i^2 = 3.3608 J.", + "With e = 0.870, hard collisions retain a fraction e^2 = 0.757 of the relative kinetic energy in the CoM frame (the remaining (1-e^2) is lost per pairwise impulsive contact).", + "Walls reflect bodies with the same restitution e (ideal one-sided penalty); the impulsive wall force inverts the body's velocity component along the wall normal up to a factor e." + ], + "_layout_summary": [ + "Track: x \\in [-4.00, 4.00] m", + "Bodies (M = 2):", + " body[0]: kind=sphere, m=0.863 kg, half_size=0.090 m, x0=-0.132 m, v0=-2.225 m/s", + " body[1]: kind=sphere, m=0.999 kg, half_size=0.082 m, x0=0.140 m, v0=-1.566 m/s" + ], + "parameters": { + "setup_seed": { + "value": 212963.39959233257, + "unit": "", + "description": "" + }, + "restitution": { + "value": 0.8703352144697811, + "unit": "", + "description": "Newton's restitution coefficient" + }, + "track_left": { + "value": -4.0, + "unit": "m", + "description": "" + }, + "track_right": { + "value": 4.0, + "unit": "m", + "description": "" + }, + "k_contact": { + "value": 5000.0, + "unit": "N/m", + "description": "Penalty stiffness for hard contacts" + }, + "body_kinds": { + "value": [ + "sphere", + "sphere" + ], + "unit": "", + "description": "" + }, + "masses": { + "value": [ + 0.863176549544485, + 0.9991228783043138 + ], + "unit": "kg", + "description": "" + }, + "half_sizes": { + "value": [ + 0.0897768200662488, + 0.08217012444980792 + ], + "unit": "m", + "description": "" + }, + "x0": { + "value": [ + -0.13217012444980814, + 0.13977682006624859 + ], + "unit": "m", + "description": "" + }, + "v0": { + "value": [ + -2.224868799752536, + -1.5655817229758924 + ], + "unit": "m/s", + "description": "" + }, + "spring_a": { + "value": [], + "unit": "", + "description": "" + }, + "spring_pair": { + "value": [], + "unit": "", + "description": "" + } + } +} diff --git a/scipy/complex_collision_plane/489/params.json b/scipy/complex_collision_plane/489/params.json new file mode 100644 index 0000000000000000000000000000000000000000..2b5ec74dea2e75b53a95a8b4ed9a02c9a85b0738 --- /dev/null +++ b/scipy/complex_collision_plane/489/params.json @@ -0,0 +1,29 @@ +{ + "setup_seed": 212963.39959233257, + "restitution": 0.8703352144697811, + "track_left": -4.0, + "track_right": 4.0, + "k_contact": 5000.0, + "body_kinds": [ + "sphere", + "sphere" + ], + "masses": [ + 0.863176549544485, + 0.9991228783043138 + ], + "half_sizes": [ + 0.0897768200662488, + 0.08217012444980792 + ], + "x0": [ + -0.13217012444980814, + 0.13977682006624859 + ], + "v0": [ + -2.224868799752536, + -1.5655817229758924 + ], + "spring_a": [], + "spring_pair": [] +} \ No newline at end of file diff --git a/scipy/complex_collision_plane/489/simulation_code.py b/scipy/complex_collision_plane/489/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e98e2b6d3034b2cb9aaa6f30a14814bac5a7585f --- /dev/null +++ b/scipy/complex_collision_plane/489/simulation_code.py @@ -0,0 +1,277 @@ +""" +Complex Collision Plane — auto-generated standalone simulation script. + +Physics: + - 1-D motion along a frictionless plane bounded by walls at + x=-4.000 and x=4.000. + - Each body i obeys m_i ẍ_i = ΣF_i. + - Hard contacts use a one-sided penalty spring (k_c=5000 N/m) + with damping calibrated to deliver Newton's restitution e=0.8703 + via e = exp(-π ζ / sqrt(1-ζ²)) ⇒ ζ = -ln(e)/sqrt(π²+ln²(e)). + - Spring-A bodies have a free helical spring on one side: while compressed, + F_spring = -k * compression (Hooke's Law). + - Spring-pair: two blocks coupled by a permanent internal helical spring. +""" +import numpy as np +from scipy.integrate import solve_ivp +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + + +track_left = -4.0 +track_right = 4.0 +restitution = 0.8703352144697811 +k_contact = 5000.0 +body_kinds = ['sphere', 'sphere'] +masses = [0.863176549544485, 0.9991228783043138] +half_sizes = [0.0897768200662488, 0.08217012444980792] +x0_list = [-0.13217012444980814, 0.13977682006624859] +v0_list = [-2.224868799752536, -1.5655817229758924] +spring_a = [] +spring_pair = [] +duration = 12 +fps = 30 +M = len(body_kinds) + +if restitution <= 0.001: zeta = 1.0 +elif restitution >= 0.999: zeta = 0.0 +else: + log_e = np.log(restitution) + zeta = -log_e / np.sqrt(np.pi ** 2 + log_e ** 2) + +spring_pair_set = set() +for sp in spring_pair: + spring_pair_set.add(tuple(sorted([sp["left_body_idx"], sp["right_body_idx"]]))) + + +def ode(t, y): + x = np.array([y[2 * i] for i in range(M)]) + v = np.array([y[2 * i + 1] for i in range(M)]) + F = np.zeros(M) + + for i in range(M): + for j in range(i + 1, M): + if (i, j) in spring_pair_set: + continue + left, right = (i, j) if x[i] < x[j] else (j, i) + sign_right = +1 if right == j else -1 + left_face_right = x[left] + half_sizes[left] + right_face_left = x[right] - half_sizes[right] + + spring_data = None + for sa in spring_a: + if sa["body_idx"] == left and sa["side"] == +1: + spring_data = ("LR", sa); break + if sa["body_idx"] == right and sa["side"] == -1: + spring_data = ("RL", sa); break + + if spring_data is not None: + kind, sa = spring_data + if kind == "LR": + spring_tip = left_face_right + sa["L"] + if right_face_left < spring_tip: + compression = spring_tip - right_face_left + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + spring_tip = right_face_left - sa["L"] + if left_face_right > spring_tip: + compression = left_face_right - spring_tip + F_s = sa["k"] * compression + F[right] += sign_right * F_s + F[left] -= sign_right * F_s + if compression > sa["L"]: + extra = compression - sa["L"] + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * extra + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + else: + if right_face_left < left_face_right: + overlap = left_face_right - right_face_left + v_rel = v[right] - v[left] + m_eff = masses[left] * masses[right] / (masses[left] + masses[right]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + F_h = k_contact * overlap + F_d = -c_damp * v_rel + F[right] += sign_right * (F_h + F_d) + F[left] -= sign_right * (F_h + F_d) + + for i in range(M): + sa_left = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == -1), None) + sa_right = next((sa for sa in spring_a + if sa["body_idx"] == i and sa["side"] == +1), None) + + lf = x[i] - half_sizes[i] + if sa_left is not None: + spring_tip = lf - sa_left["L"] + if spring_tip < track_left: + compression = track_left - spring_tip + F[i] += sa_left["k"] * compression + if compression > sa_left["L"]: + extra = compression - sa_left["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * extra - c_damp * v[i] + else: + if lf < track_left: + overlap = track_left - lf + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += k_contact * overlap - c_damp * v[i] + + rf = x[i] + half_sizes[i] + if sa_right is not None: + spring_tip = rf + sa_right["L"] + if spring_tip > track_right: + compression = spring_tip - track_right + F[i] -= sa_right["k"] * compression + if compression > sa_right["L"]: + extra = compression - sa_right["L"] + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * extra - c_damp * v[i] + else: + if rf > track_right: + overlap = rf - track_right + c_damp = 2 * zeta * np.sqrt(masses[i] * k_contact) + F[i] += -k_contact * overlap - c_damp * v[i] + + for sp in spring_pair: + l, r = sp["left_body_idx"], sp["right_body_idx"] + sep = x[r] - x[l] + rest_sep = sp["L"] + half_sizes[l] + half_sizes[r] + stretch = sep - rest_sep + F[l] += sp["k"] * stretch + F[r] -= sp["k"] * stretch + face_sep = sep - half_sizes[l] - half_sizes[r] + if face_sep < 0: + overlap = -face_sep + v_rel = v[r] - v[l] + m_eff = masses[l] * masses[r] / (masses[l] + masses[r]) + c_damp = 2 * zeta * np.sqrt(m_eff * k_contact) + extra = k_contact * overlap - c_damp * v_rel + F[l] -= extra + F[r] += extra + + derivs = [] + for i in range(M): + derivs.append(v[i]) + derivs.append(F[i] / masses[i]) + return derivs + + +y0 = [] +for i in range(M): + y0.append(x0_list[i]); y0.append(v0_list[i]) +t_eval = np.linspace(0, duration, int(duration * fps) + 1) +sol = solve_ivp(ode, (0, duration), y0, t_eval=t_eval, method="LSODA", + rtol=1e-7, atol=1e-9, max_step=0.005) +y = sol.y + + +def spring_path(x1, y1, x2, y2, n=8, r=0.05): + dx, dy = x2 - x1, y2 - y1 + L = np.hypot(dx, dy) + if L < 1e-10: + return np.array([x1, x2]), np.array([y1, y2]) + ux, uy = dx / L, dy / L + nx_, ny_ = -uy, ux + pts = n * 20 + 2 + tt = np.linspace(0, 1, pts) + margin = 0.08 + env = np.where((tt < margin) | (tt > 1 - margin), 0, + np.sin(n * 2 * np.pi * (tt - margin) / (1 - 2 * margin))) + return x1 + tt * dx + r * env * nx_, y1 + tt * dy + r * env * ny_ + + +fig, ax = plt.subplots(figsize=(12, 4), facecolor="#1a1a2e") +ax.set_facecolor("#16213e") +ax.set_xlim(track_left - 0.4, track_right + 0.4); ax.set_ylim(-0.55, 0.55) +ax.set_aspect("equal"); ax.axis("off") +ax.axhline(0, color="#3a3a4f", lw=1.5, zorder=1) +wall_h = 0.4 +for wx in [track_left, track_right]: + x_off = -0.06 if wx == track_left else 0.0 + ax.add_patch(patches.Rectangle( + (wx + x_off, -wall_h), 0.06, 2 * wall_h, + facecolor="#4a4e69", edgecolor="#adb5bd", lw=1, hatch="////")) + +palette = ["#e63946", "#457b9d", "#2a9d8f", "#f4a261", "#e76f51", + "#06d6a0", "#118ab2", "#ef476f", "#9d4edd"] +body_patches = [] +for i, kind in enumerate(body_kinds): + c = palette[i % len(palette)] + if kind == "sphere": + p_ = patches.Circle((x0_list[i], 0), half_sizes[i], + facecolor=c, edgecolor="white", lw=1.5) + else: + hs = half_sizes[i] + p_ = patches.FancyBboxPatch( + (x0_list[i] - hs, -hs), 2 * hs, 2 * hs, + boxstyle="round,pad=0.01", lw=1.5, edgecolor="white", facecolor=c) + ax.add_patch(p_); body_patches.append(p_) + +spring_a_lines = [(sa, ax.plot([], [], "-", color="#ffd166", + lw=2, zorder=2)[0]) for sa in spring_a] +spring_pair_lines = [(sp, ax.plot([], [], "-", color="#a8dadc", + lw=2, zorder=2)[0]) for sp in spring_pair] + + +def update(frame): + for i in range(M): + xi = y[2 * i, frame] + if body_kinds[i] == "sphere": + body_patches[i].center = (xi, 0) + else: + body_patches[i].set_x(xi - half_sizes[i]) + for sa, line in spring_a_lines: + i = sa["body_idx"]; xi = y[2 * i, frame]; hs = half_sizes[i] + if sa["side"] == +1: + x_a = xi + hs; tip = x_a + sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] > xi: + jl = y[2 * j, frame] - half_sizes[j] + if jl < tip: tip = jl; break + if tip > track_right: tip = track_right + else: + x_a = xi - hs; tip = x_a - sa["L"] + for j in range(M): + if j != i and y[2 * j, frame] < xi: + jr = y[2 * j, frame] + half_sizes[j] + if jr > tip: tip = jr; break + if tip < track_left: tip = track_left + sx, sy_ = spring_path(x_a, 0, tip, 0, + n=max(3, int(sa["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + for sp, line in spring_pair_lines: + l, r = sp["left_body_idx"], sp["right_body_idx"] + xl = y[2 * l, frame] + half_sizes[l] + xr = y[2 * r, frame] - half_sizes[r] + if xr - xl > 0.01: + sx, sy_ = spring_path(xl, 0, xr, 0, + n=max(3, int(sp["L"] * 15)), r=0.06) + line.set_data(sx, sy_) + else: + line.set_data([], []) + return body_patches + [l for _, l in spring_a_lines] + [l for _, l in spring_pair_lines] + + +ani = animation.FuncAnimation(fig, update, frames=len(sol.t), + interval=1000 / fps, blit=False) +ani.save("video.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), + dpi=120) +plt.close() +print("Saved video.mp4") diff --git a/scipy/complex_collision_plane/489/video.mp4 b/scipy/complex_collision_plane/489/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..21ad6936d1e4449e6b0673fe8f8ebf936feae128 --- /dev/null +++ b/scipy/complex_collision_plane/489/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:55f0e6dbb69500fd7d88d13cb5368ab4513fc6f0eb6021d7ce193b36678b545a +size 8931 diff --git a/scipy/compound_atwood/025/cot.json b/scipy/compound_atwood/025/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ba3823b18ec43c8f8f7df8664c045d243a3db1b6 --- /dev/null +++ b/scipy/compound_atwood/025/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=3.787 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=1.598 kg and m3=2.318 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = 0.004448; a2 = -1.806169; a3 = 1.797273; aB = -0.004448" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = 0.004448; a2 = -1.806169; a3 = 1.797273; aB = -0.004448" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 37.0983 N, T_B = 18.5492 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = +0.0044 m/s^2, a_2 = -1.8062, a_3 = +1.7973, a_B = -0.0044." + ], + "physical_constants_derived": { + "T_A_N": 37.09833332979703, + "T_B_N": 18.549166664898515, + "a_1_m_s2": 0.004447883454476553, + "a_2_m_s2": -1.8061685786086468, + "a_3_m_s2": 1.7972728116996937, + "a_B_m_s2": -0.004447883454476553 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = +0.004, -1.806, +1.797, -0.004 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 3.787263125999276, + "unit": "", + "description": "" + }, + "m2": { + "value": 1.5982162019502733, + "unit": "", + "description": "" + }, + "m3": { + "value": 2.3178556795009473, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/025/params.json b/scipy/compound_atwood/025/params.json new file mode 100644 index 0000000000000000000000000000000000000000..f6bba7c7aa7156e7fb5c1f5514941b4d8ffd7987 --- /dev/null +++ b/scipy/compound_atwood/025/params.json @@ -0,0 +1,21 @@ +{ + "m1": 3.787263125999276, + "m2": 1.5982162019502733, + "m3": 2.3178556795009473, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/025/simulation_code.py b/scipy/compound_atwood/025/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..61e383acb15fee522f0014756aff15243c004901 --- /dev/null +++ b/scipy/compound_atwood/025/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 3.787263125999276, 1.5982162019502733, 2.3178556795009473 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4") diff --git a/scipy/compound_atwood/025/video.mp4 b/scipy/compound_atwood/025/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..90b4d360694609a70f7866d194ee94c0180272a6 --- /dev/null +++ b/scipy/compound_atwood/025/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:21d18bc64c89d597af5f6d81b10f0d3d8d67a33b9e4653f2343cd9b7080ef5c1 +size 7457 diff --git a/scipy/compound_atwood/029/cot.json b/scipy/compound_atwood/029/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..f3732fc9757163b00073d62e39307fdeffc2a554 --- /dev/null +++ b/scipy/compound_atwood/029/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=3.767 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=0.973 kg and m3=1.152 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = 2.763669; a2 = -3.822172; a3 = -1.705167; aB = -2.763669" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = 2.763669; a2 = -3.822172; a3 = -1.705167; aB = -2.763669" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 26.5071 N, T_B = 13.2535 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = +2.7637 m/s^2, a_2 = -3.8222, a_3 = -1.7052, a_B = -2.7637." + ], + "physical_constants_derived": { + "T_A_N": 26.50709185940287, + "T_B_N": 13.253545929701435, + "a_1_m_s2": 2.763669249378478, + "a_2_m_s2": -3.822171610012859, + "a_3_m_s2": -1.705166888744099, + "a_B_m_s2": -2.763669249378478 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = +2.764, -3.822, -1.705, -2.764 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 3.767175364384553, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.9729392867110506, + "unit": "", + "description": "" + }, + "m3": { + "value": 1.1519646831605577, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/029/params.json b/scipy/compound_atwood/029/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b58f2b8438fdc974d2b3c4b12daca8c2258414b4 --- /dev/null +++ b/scipy/compound_atwood/029/params.json @@ -0,0 +1,21 @@ +{ + "m1": 3.767175364384553, + "m2": 0.9729392867110506, + "m3": 1.1519646831605577, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/029/simulation_code.py b/scipy/compound_atwood/029/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..c00a7822597bf39e40178fffbb2f16229dda2d9b --- /dev/null +++ b/scipy/compound_atwood/029/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 3.767175364384553, 0.9729392867110506, 1.1519646831605577 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4") diff --git a/scipy/compound_atwood/029/video.mp4 b/scipy/compound_atwood/029/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..aa88c397292a87c35fdf6c46643d12721c48a284 --- /dev/null +++ b/scipy/compound_atwood/029/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a11dd2e18e724a96ecc836c0b58ffecb677364a85aabd7ea66d4d9ff51b97231 +size 9655 diff --git a/scipy/compound_atwood/031/cot.json b/scipy/compound_atwood/031/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..26d8325eaa65fa8778d7c77466d374c7739bbd25 --- /dev/null +++ b/scipy/compound_atwood/031/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=1.973 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=1.392 kg and m3=2.257 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = -2.661723; a2 = 0.969950; a3 = 4.353495; aB = 2.661723" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = -2.661723; a2 = 0.969950; a3 = 4.353495; aB = 2.661723" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 24.5819 N, T_B = 12.2910 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = -2.6617 m/s^2, a_2 = +0.9699, a_3 = +4.3535, a_B = +2.6617." + ], + "physical_constants_derived": { + "T_A_N": 24.581908072598836, + "T_B_N": 12.290954036299418, + "a_1_m_s2": -2.6617225239898286, + "a_2_m_s2": 0.9699495942281526, + "a_3_m_s2": 4.353495453751502, + "a_B_m_s2": 2.6617225239898286 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = -2.662, +0.970, +4.353, +2.662 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 1.9725931166639814, + "unit": "", + "description": "" + }, + "m2": { + "value": 1.3919460786164162, + "unit": "", + "description": "" + }, + "m3": { + "value": 2.256668278146451, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/031/params.json b/scipy/compound_atwood/031/params.json new file mode 100644 index 0000000000000000000000000000000000000000..c702359df190f0f14142426eaf99ed1e3990f542 --- /dev/null +++ b/scipy/compound_atwood/031/params.json @@ -0,0 +1,21 @@ +{ + "m1": 1.9725931166639814, + "m2": 1.3919460786164162, + "m3": 2.256668278146451, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/031/simulation_code.py b/scipy/compound_atwood/031/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..90045964d55596572cd332d2939f1c01775e2375 --- /dev/null +++ b/scipy/compound_atwood/031/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 1.9725931166639814, 1.3919460786164162, 2.256668278146451 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4") diff --git a/scipy/compound_atwood/031/video.mp4 b/scipy/compound_atwood/031/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..788a443c8873164f1f305e011209beb4932f9929 --- /dev/null +++ b/scipy/compound_atwood/031/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ace66480688eb9713982a526086d1ff1c1b42348f29837d8401d4b4b7bc8c570 +size 7927 diff --git a/scipy/compound_atwood/079/cot.json b/scipy/compound_atwood/079/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..181061da699b9d68f59b8125b70971dd4d8b8c5c --- /dev/null +++ b/scipy/compound_atwood/079/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=3.835 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=1.872 kg and m3=2.072 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = -0.124240; a2 = -0.364799; a3 = 0.613279; aB = 0.124240" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = -0.124240; a2 = -0.364799; a3 = 0.613279; aB = 0.124240" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 38.0633 N, T_B = 19.0316 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = -0.1242 m/s^2, a_2 = -0.3648, a_3 = +0.6133, a_B = +0.1242." + ], + "physical_constants_derived": { + "T_A_N": 38.063271528591926, + "T_B_N": 19.031635764295963, + "a_1_m_s2": -0.12424004790246634, + "a_2_m_s2": -0.3647988590793272, + "a_3_m_s2": 0.6132789548842617, + "a_B_m_s2": 0.12424004790246634 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = -0.124, -0.365, +0.613, +0.124 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 3.835384003698779, + "unit": "", + "description": "" + }, + "m2": { + "value": 1.8723081517050053, + "unit": "", + "description": "" + }, + "m3": { + "value": 2.0716462022556374, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/079/params.json b/scipy/compound_atwood/079/params.json new file mode 100644 index 0000000000000000000000000000000000000000..99c6dac36cee2e3a629da989ecbe71b209a1fdbb --- /dev/null +++ b/scipy/compound_atwood/079/params.json @@ -0,0 +1,21 @@ +{ + "m1": 3.835384003698779, + "m2": 1.8723081517050053, + "m3": 2.0716462022556374, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/079/simulation_code.py b/scipy/compound_atwood/079/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..25d7db525240ec1794e21bd1789098475959e514 --- /dev/null +++ b/scipy/compound_atwood/079/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 3.835384003698779, 1.8723081517050053, 2.0716462022556374 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4") diff --git a/scipy/compound_atwood/079/video.mp4 b/scipy/compound_atwood/079/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..fe760607463fc6d4c87971123dabd35e7b105cf6 --- /dev/null +++ b/scipy/compound_atwood/079/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:57c2bcac11fca549ce5f9918a1e12a0e4ffa9bb66038cc16bbab1fd395bff5ad +size 10701 diff --git a/scipy/compound_atwood/097/cot.json b/scipy/compound_atwood/097/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..d041165ce9e2bada019bfd4024b808dfc5db7144 --- /dev/null +++ b/scipy/compound_atwood/097/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=2.241 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=1.607 kg and m3=1.670 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = -1.838727; a2 = 1.686717; a3 = 1.990736; aB = 1.838727" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = -1.838727; a2 = 1.686717; a3 = 1.990736; aB = 1.838727" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 26.0840 N, T_B = 13.0420 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = -1.8387 m/s^2, a_2 = +1.6867, a_3 = +1.9907, a_B = +1.8387." + ], + "physical_constants_derived": { + "T_A_N": 26.08404828210294, + "T_B_N": 13.04202414105147, + "a_1_m_s2": -1.8387266066170351, + "a_2_m_s2": 1.6867169874717884, + "a_3_m_s2": 1.9907362257622836, + "a_B_m_s2": 1.8387266066170351 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = -1.839, +1.687, +1.991, +1.839 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 2.2411427954045435, + "unit": "", + "description": "" + }, + "m2": { + "value": 1.6074903489638521, + "unit": "", + "description": "" + }, + "m3": { + "value": 1.6700708950408756, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/097/params.json b/scipy/compound_atwood/097/params.json new file mode 100644 index 0000000000000000000000000000000000000000..5319ca72263b02980a07addfd485a0ddf88d88a2 --- /dev/null +++ b/scipy/compound_atwood/097/params.json @@ -0,0 +1,21 @@ +{ + "m1": 2.2411427954045435, + "m2": 1.6074903489638521, + "m3": 1.6700708950408756, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/097/simulation_code.py b/scipy/compound_atwood/097/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..d9894ab2736cc28c5a008b1657d147f0d95b7f32 --- /dev/null +++ b/scipy/compound_atwood/097/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 2.2411427954045435, 1.6074903489638521, 1.6700708950408756 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4") diff --git a/scipy/compound_atwood/097/video.mp4 b/scipy/compound_atwood/097/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..526fbba65487c7d4ba68a1ab497757ef90ec4f08 --- /dev/null +++ b/scipy/compound_atwood/097/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4bfe5037220ca3a939c5cf2ded128b9eed3096892b4dc78f9a7b5e76a09706ba +size 7735 diff --git a/scipy/compound_atwood/111/cot.json b/scipy/compound_atwood/111/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..491bd797fce1fb557b934e362133880889929271 --- /dev/null +++ b/scipy/compound_atwood/111/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=1.640 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=1.962 kg and m3=2.332 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = -4.353241; a2 = 3.884174; a3 = 4.822309; aB = 4.353241" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = -4.353241; a2 = 3.884174; a3 = 4.822309; aB = 4.353241" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 23.2117 N, T_B = 11.6058 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = -4.3532 m/s^2, a_2 = +3.8842, a_3 = +4.8223, a_B = +4.3532." + ], + "physical_constants_derived": { + "T_A_N": 23.211670851371405, + "T_B_N": 11.605835425685703, + "a_1_m_s2": -4.353241361880253, + "a_2_m_s2": 3.88417403103693, + "a_3_m_s2": 4.822308692723574, + "a_B_m_s2": 4.353241361880253 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = -4.353, +3.884, +4.822, +4.353 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 1.6400250838573802, + "unit": "", + "description": "" + }, + "m2": { + "value": 1.9618284051246322, + "unit": "", + "description": "" + }, + "m3": { + "value": 2.331569940610862, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/111/params.json b/scipy/compound_atwood/111/params.json new file mode 100644 index 0000000000000000000000000000000000000000..873c3b681b62f7ad75ef64ffbb86d4960a24e44b --- /dev/null +++ b/scipy/compound_atwood/111/params.json @@ -0,0 +1,21 @@ +{ + "m1": 1.6400250838573802, + "m2": 1.9618284051246322, + "m3": 2.331569940610862, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/111/simulation_code.py b/scipy/compound_atwood/111/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0381f22fbe3fd7d753dda162873ee1e689b56b56 --- /dev/null +++ b/scipy/compound_atwood/111/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 1.6400250838573802, 1.9618284051246322, 2.331569940610862 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4") diff --git a/scipy/compound_atwood/111/video.mp4 b/scipy/compound_atwood/111/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..09e9b051eaad150b16d8de5d0fc456a4af572487 --- /dev/null +++ b/scipy/compound_atwood/111/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ac1e6533f8da001180af863f2dfd146eace0c597e7d0001408e10cde21389817 +size 7164 diff --git a/scipy/compound_atwood/146/cot.json b/scipy/compound_atwood/146/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..7baa0bcba20822315dcd35375afdd095cca711bf --- /dev/null +++ b/scipy/compound_atwood/146/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=3.354 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=0.663 kg and m3=2.191 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = 2.397704; a2 = -8.928606; a3 = 4.133199; aB = -2.397704" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = 2.397704; a2 = -8.928606; a3 = 4.133199; aB = -2.397704" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 24.8278 N, T_B = 12.4139 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = +2.3977 m/s^2, a_2 = -8.9286, a_3 = +4.1332, a_B = -2.3977." + ], + "physical_constants_derived": { + "T_A_N": 24.827787552682082, + "T_B_N": 12.413893776341041, + "a_1_m_s2": 2.397703636101423, + "a_2_m_s2": -8.928605804698517, + "a_3_m_s2": 4.133198532495674, + "a_B_m_s2": -2.397703636101423 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = +2.398, -8.929, +4.133, -2.398 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 3.354065594261886, + "unit": "", + "description": "" + }, + "m2": { + "value": 0.6628306402405415, + "unit": "", + "description": "" + }, + "m3": { + "value": 2.190635025336109, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/146/params.json b/scipy/compound_atwood/146/params.json new file mode 100644 index 0000000000000000000000000000000000000000..f438fcf898422a6fe6a30a1b29c08e810c2ccd49 --- /dev/null +++ b/scipy/compound_atwood/146/params.json @@ -0,0 +1,21 @@ +{ + "m1": 3.354065594261886, + "m2": 0.6628306402405415, + "m3": 2.190635025336109, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/146/simulation_code.py b/scipy/compound_atwood/146/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..da242ca566986da5f97f3997e8218df0425ad820 --- /dev/null +++ b/scipy/compound_atwood/146/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 3.354065594261886, 0.6628306402405415, 2.190635025336109 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4") diff --git a/scipy/compound_atwood/146/video.mp4 b/scipy/compound_atwood/146/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..b983be7c77ed5380be37e1842c98efa0fa8b5d93 --- /dev/null +++ b/scipy/compound_atwood/146/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bc3c8eeba4ad727f057ee91cd4f6af1d07046de3ee0f2eba89a5486a55dcfac5 +size 8521 diff --git a/scipy/compound_atwood/151/cot.json b/scipy/compound_atwood/151/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..9498f0624fd97f419f030a20beef1cc6985e9481 --- /dev/null +++ b/scipy/compound_atwood/151/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=3.075 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=1.612 kg and m3=2.238 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = -0.965502; a2 = -0.471109; a3 = 2.402112; aB = 0.965502" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = -0.965502; a2 = -0.471109; a3 = 2.402112; aB = 0.965502" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 33.1069 N, T_B = 16.5534 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = -0.9655 m/s^2, a_2 = -0.4711, a_3 = +2.4021, a_B = +0.9655." + ], + "physical_constants_derived": { + "T_A_N": 33.10689831244634, + "T_B_N": 16.55344915622317, + "a_1_m_s2": -0.9655017550881055, + "a_2_m_s2": -0.47110890407194894, + "a_3_m_s2": 2.402112414248161, + "a_B_m_s2": 0.9655017550881055 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = -0.966, -0.471, +2.402, +0.966 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 3.0752768487357316, + "unit": "", + "description": "" + }, + "m2": { + "value": 1.6116516055691519, + "unit": "", + "description": "" + }, + "m3": { + "value": 2.237591334599992, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/151/params.json b/scipy/compound_atwood/151/params.json new file mode 100644 index 0000000000000000000000000000000000000000..509fbc0ea88d6862a3e38ce7bf79067fe49886d7 --- /dev/null +++ b/scipy/compound_atwood/151/params.json @@ -0,0 +1,21 @@ +{ + "m1": 3.0752768487357316, + "m2": 1.6116516055691519, + "m3": 2.237591334599992, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/151/simulation_code.py b/scipy/compound_atwood/151/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..f52a3398c6c18a57cb11e09e5de2a7acb107d797 --- /dev/null +++ b/scipy/compound_atwood/151/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 3.0752768487357316, 1.6116516055691519, 2.237591334599992 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4") diff --git a/scipy/compound_atwood/151/video.mp4 b/scipy/compound_atwood/151/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..baac48b5b09e1897f94a7f0c93c4621d6ec80bd1 --- /dev/null +++ b/scipy/compound_atwood/151/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6526b992a4b3e391a3f61c7172ffeff256667026259464d3aadf7d5987136169 +size 9023 diff --git a/scipy/compound_atwood/160/cot.json b/scipy/compound_atwood/160/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..d273ef480c7b7b19aa865a2a511abb9e5723fb07 --- /dev/null +++ b/scipy/compound_atwood/160/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=2.820 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=1.387 kg and m3=2.178 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = -0.899485; a2 = -1.075098; a3 = 2.874068; aB = 0.899485" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = -0.899485; a2 = -1.075098; a3 = 2.874068; aB = 0.899485" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 30.1700 N, T_B = 15.0850 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = -0.8995 m/s^2, a_2 = -1.0751, a_3 = +2.8741, a_B = +0.8995." + ], + "physical_constants_derived": { + "T_A_N": 30.16995752507337, + "T_B_N": 15.084978762536686, + "a_1_m_s2": -0.8994847820903438, + "a_2_m_s2": -1.0750984008986588, + "a_3_m_s2": 2.874067965079342, + "a_B_m_s2": 0.8994847820903438 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = -0.899, -1.075, +2.874, +0.899 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 2.819757973353471, + "unit": "", + "description": "" + }, + "m2": { + "value": 1.3871119328253751, + "unit": "", + "description": "" + }, + "m3": { + "value": 2.178043140833318, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/160/params.json b/scipy/compound_atwood/160/params.json new file mode 100644 index 0000000000000000000000000000000000000000..867bfa2b6e11d7a19caef6a9bbec54fa863ab283 --- /dev/null +++ b/scipy/compound_atwood/160/params.json @@ -0,0 +1,21 @@ +{ + "m1": 2.819757973353471, + "m2": 1.3871119328253751, + "m3": 2.178043140833318, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/160/simulation_code.py b/scipy/compound_atwood/160/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..8dd0eccc8b45a216e64dafb9a986917c286cc07f --- /dev/null +++ b/scipy/compound_atwood/160/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 2.819757973353471, 1.3871119328253751, 2.178043140833318 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4") diff --git a/scipy/compound_atwood/160/video.mp4 b/scipy/compound_atwood/160/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..de05a58aad09f8561d65d1cf3bfd3c90358fb016 --- /dev/null +++ b/scipy/compound_atwood/160/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:44946cb926a6f4b77cc0aa0eedcfe81781e700e871975ead1408b3bba98d1390 +size 8447 diff --git a/scipy/compound_atwood/216/cot.json b/scipy/compound_atwood/216/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..ede24ef25d98ca1d19c344c7f9448b4f79ae3b3c --- /dev/null +++ b/scipy/compound_atwood/216/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=1.521 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=1.221 kg and m3=2.147 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = -3.368263; a2 = 1.600391; a3 = 5.136135; aB = 3.368263" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = -3.368263; a2 = 1.600391; a3 = 5.136135; aB = 3.368263" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 20.0237 N, T_B = 10.0118 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = -3.3683 m/s^2, a_2 = +1.6004, a_3 = +5.1361, a_B = +3.3683." + ], + "physical_constants_derived": { + "T_A_N": 20.02368042215167, + "T_B_N": 10.011840211075835, + "a_1_m_s2": -3.368262806160315, + "a_2_m_s2": 1.6003908790556114, + "a_3_m_s2": 5.136134733265016, + "a_B_m_s2": 3.368262806160315 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = -3.368, +1.600, +5.136, +3.368 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 1.5206015187352036, + "unit": "", + "description": "" + }, + "m2": { + "value": 1.221014326829609, + "unit": "", + "description": "" + }, + "m3": { + "value": 2.1466829847091162, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/216/params.json b/scipy/compound_atwood/216/params.json new file mode 100644 index 0000000000000000000000000000000000000000..379b15c837970f299268c79fd189546ff8c415d1 --- /dev/null +++ b/scipy/compound_atwood/216/params.json @@ -0,0 +1,21 @@ +{ + "m1": 1.5206015187352036, + "m2": 1.221014326829609, + "m3": 2.1466829847091162, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/216/simulation_code.py b/scipy/compound_atwood/216/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e1d148bad7feaf5bf1992c7fa1d3d9105f5994fc --- /dev/null +++ b/scipy/compound_atwood/216/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 1.5206015187352036, 1.221014326829609, 2.1466829847091162 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4") diff --git a/scipy/compound_atwood/216/video.mp4 b/scipy/compound_atwood/216/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7eb739e15e1fe94f2542d4aad3d54eaae9d6714a --- /dev/null +++ b/scipy/compound_atwood/216/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a5d77347136da104e05a56cb665989df6c267a7cb51cf7046606f4146ca18462 +size 7900 diff --git a/scipy/compound_atwood/249/cot.json b/scipy/compound_atwood/249/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..5f7061dbbebd5973fe06b4a826d1132ab0b860e0 --- /dev/null +++ b/scipy/compound_atwood/249/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=3.937 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=1.636 kg and m3=2.054 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = 0.380085; a2 = -1.534902; a3 = 0.774731; aB = -0.380085" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = 0.380085; a2 = -1.534902; a3 = 0.774731; aB = -0.380085" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 37.0848 N, T_B = 18.5424 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = +0.3801 m/s^2, a_2 = -1.5349, a_3 = +0.7747, a_B = -0.3801." + ], + "physical_constants_derived": { + "T_A_N": 37.084757017458024, + "T_B_N": 18.542378508729012, + "a_1_m_s2": 0.38008548573529133, + "a_2_m_s2": -1.5349015296690922, + "a_3_m_s2": 0.7747305581985113, + "a_B_m_s2": -0.38008548573529133 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = +0.380, -1.535, +0.775, -0.380 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 3.9368464502836047, + "unit": "", + "description": "" + }, + "m2": { + "value": 1.6358658661651675, + "unit": "", + "description": "" + }, + "m3": { + "value": 2.054495838412095, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/249/params.json b/scipy/compound_atwood/249/params.json new file mode 100644 index 0000000000000000000000000000000000000000..b1dcadff7d385b300f3ee05b46c0198a3bd014d4 --- /dev/null +++ b/scipy/compound_atwood/249/params.json @@ -0,0 +1,21 @@ +{ + "m1": 3.9368464502836047, + "m2": 1.6358658661651675, + "m3": 2.054495838412095, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/249/simulation_code.py b/scipy/compound_atwood/249/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..0765e50538eab164016c96cb0d532810d1a40931 --- /dev/null +++ b/scipy/compound_atwood/249/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 3.9368464502836047, 1.6358658661651675, 2.054495838412095 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4") diff --git a/scipy/compound_atwood/249/video.mp4 b/scipy/compound_atwood/249/video.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..d1b82b9574b77d76cb0f50cdce21d40deff1904e --- /dev/null +++ b/scipy/compound_atwood/249/video.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4e84f3f7e2bde54b271bd650555b5373335b26e2df8c35c8acc2a7ef62f740be +size 9920 diff --git a/scipy/compound_atwood/277/cot.json b/scipy/compound_atwood/277/cot.json new file mode 100644 index 0000000000000000000000000000000000000000..3135245b6fee749d074f0fb750e7c22eefac99fc --- /dev/null +++ b/scipy/compound_atwood/277/cot.json @@ -0,0 +1,150 @@ +{ + "simulation": "compound_atwood", + "physical_observation": "Compound (double) Atwood machine: a fixed pulley A on the ceiling carries a string with mass m1=2.268 kg on one side and the axle of a movable pulley B on the other. A rope over B carries mass m2=1.061 kg and m3=2.052 kg. Gravity g=9.800 m/s^2 drives the system; all motions start from rest.", + "physical_law": { + "name": "Newton II + massless inextensible-cord constraints + massless pulleys", + "statement": "Each mass obeys m_i \\ddot{y}_i = m_i g - T_i; the cord constraints couple accelerations; a massless movable pulley enforces T_A = 2 T_B.", + "formula": "T_B = 4 g / (1/m2 + 1/m3 + 4/m1); T_A = 2 T_B; a_i = g - T_i/m_i; a_B = -a_1." + }, + "ode_system": { + "state_variables": [ + "y1, y2, y3 (cube depths, m, +y down)", + "yB (movable pulley depth)" + ], + "equations_latex": [ + "\\dot{y}_i = v_i; \\dot{v}_i = a_i (constants)", + "a1 = -1.025325; a2 = -1.767390; a3 = 3.818041; aB = 1.025325" + ], + "equations_python": [ + "y_dot_i = v_i; v_dot_i = a_i (constants)", + "a1 = -1.025325; a2 = -1.767390; a3 = 3.818041; aB = 1.025325" + ] + }, + "derivation_steps": [ + "Massless movable pulley B: tension balance gives T_A = 2 T_B (so T_A = 24.5477 N, T_B = 12.2739 N).", + "Constraint on string A: y1 + yB = const => \\ddot{y}1 + \\ddot{y}B = 0 => a_B = -a_1.", + "Constraint on rope B: (y2 - yB) + (y3 - yB) = const => a_2 + a_3 - 2 a_B = 0.", + "Newton II for each mass (down = +): m_i \\ddot{y}_i = m_i g - T_i (T_1 = T_A on m1; T_2 = T_3 = T_B on m2, m3).", + "Solving the linear system yields T_B = 4 g / (1/m2 + 1/m3 + 4/m1).", + "Therefore a_1 = -1.0253 m/s^2, a_2 = -1.7674, a_3 = +3.8180, a_B = +1.0253." + ], + "physical_constants_derived": { + "T_A_N": 24.54770426098705, + "T_B_N": 12.273852130493525, + "a_1_m_s2": -1.025325253233257, + "a_2_m_s2": -1.7673904232220607, + "a_3_m_s2": 3.81804092968858, + "a_B_m_s2": 1.025325253233257 + }, + "numerical_method": { + "solver": "Closed form (constant accelerations); positions are quadratic in t. No ODE integrator required.", + "tolerance": "Machine precision.", + "justification": "All four accelerations are time-independent, so y_i(t) = y_i_0 + v_i_0 t + 0.5 a_i t^2." + }, + "code_validation": { + "ode_source": "Newton II for three point masses with two cord constraints and a tension ratio T_A=2 T_B.", + "energy_check": "\\SigmaKE - \\SigmaPE evolves consistent with constant accelerations (no friction).", + "physical_plausibility": true + }, + "expected_behavior": [ + "Mass with the largest a_i descends fastest. a_1, a_2, a_3, a_B = -1.025, -1.767, +3.818, +1.025 m/s^2.", + "Motion stops in the render once the descender reaches the floor." + ], + "parameters": { + "m1": { + "value": 2.267618171902526, + "unit": "", + "description": "" + }, + "m2": { + "value": 1.0610735594999205, + "unit": "", + "description": "" + }, + "m3": { + "value": 2.051811452774542, + "unit": "", + "description": "" + }, + "g": { + "value": 9.8, + "unit": "", + "description": "" + }, + "x_A": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_B": { + "value": 2.5, + "unit": "", + "description": "" + }, + "r_pulley": { + "value": 0.5, + "unit": "", + "description": "" + }, + "cube_side": { + "value": 0.45, + "unit": "", + "description": "" + }, + "x_m1": { + "value": 1.5, + "unit": "", + "description": "" + }, + "x_m2": { + "value": 2.0, + "unit": "", + "description": "" + }, + "x_m3": { + "value": 3.0, + "unit": "", + "description": "" + }, + "y1_0": { + "value": 1.5, + "unit": "", + "description": "" + }, + "y2_0": { + "value": 5.0, + "unit": "", + "description": "" + }, + "y3_0": { + "value": 3.5, + "unit": "", + "description": "" + }, + "yB_0": { + "value": 2.5, + "unit": "", + "description": "" + }, + "v1_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v2_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "v3_0": { + "value": 0.0, + "unit": "", + "description": "" + }, + "vB_0": { + "value": 0.0, + "unit": "", + "description": "" + } + } +} diff --git a/scipy/compound_atwood/277/params.json b/scipy/compound_atwood/277/params.json new file mode 100644 index 0000000000000000000000000000000000000000..d3632ff54bd93accfc314e54fe76e9eddf71fb73 --- /dev/null +++ b/scipy/compound_atwood/277/params.json @@ -0,0 +1,21 @@ +{ + "m1": 2.267618171902526, + "m2": 1.0610735594999205, + "m3": 2.051811452774542, + "g": 9.8, + "x_A": 2.0, + "x_B": 2.5, + "r_pulley": 0.5, + "cube_side": 0.45, + "x_m1": 1.5, + "x_m2": 2.0, + "x_m3": 3.0, + "y1_0": 1.5, + "y2_0": 5.0, + "y3_0": 3.5, + "yB_0": 2.5, + "v1_0": 0.0, + "v2_0": 0.0, + "v3_0": 0.0, + "vB_0": 0.0 +} \ No newline at end of file diff --git a/scipy/compound_atwood/277/simulation_code.py b/scipy/compound_atwood/277/simulation_code.py new file mode 100644 index 0000000000000000000000000000000000000000..5bcb5f05231d79a2c4c801929811faa7b3a9a82b --- /dev/null +++ b/scipy/compound_atwood/277/simulation_code.py @@ -0,0 +1,112 @@ +""" +Compound Atwood machine — auto-generated. +""" +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import matplotlib.patches as patches + +m1, m2, m3 = 2.267618171902526, 1.0610735594999205, 2.051811452774542 +g = 9.8 +r_pulley = 0.5 +cube_side = 0.45 +x_A, x_B = 2.0, 2.5 +x_m1, x_m2, x_m3 = 1.5, 2.0, 3.0 +y1_0 = 1.5; y2_0 = 5.0; y3_0 = 3.5; yB_0 = 2.5 +v1_0 = 0.0; v2_0 = 0.0; v3_0 = 0.0; vB_0 = 0.0 +duration = 12 +fps = 30 + +T_B = 4.0 * g / (1.0/m2 + 1.0/m3 + 4.0/m1) +T_A = 2.0 * T_B +a1 = g - T_A/m1 +a2 = g - T_B/m2 +a3 = g - T_B/m3 +aB = -a1 + +y_floor = max(y1_0, y2_0, y3_0) + 2.5 +y_A = r_pulley + 0.05 + +n_frames = int(duration * fps) + 1 +t_grid = np.linspace(0, duration, n_frames) +y1 = y1_0 + v1_0 * t_grid + 0.5 * a1 * t_grid ** 2 +y2 = y2_0 + v2_0 * t_grid + 0.5 * a2 * t_grid ** 2 +y3 = y3_0 + v3_0 * t_grid + 0.5 * a3 * t_grid ** 2 +yB = yB_0 + vB_0 * t_grid + 0.5 * aB * t_grid ** 2 + +ok = ((y1 >= y_A) & (y2 >= yB + r_pulley) & (y3 >= yB + r_pulley) + & (yB >= r_pulley) + & (y1 + cube_side <= y_floor) + & (y2 + cube_side <= y_floor) + & (y3 + cube_side <= y_floor) + & (yB + r_pulley <= y_floor)) +if not ok.all(): + first_bad = int(np.argmax(~ok)) + last_good = max(first_bad - 1, 0) + y1[first_bad:] = y1[last_good] + y2[first_bad:] = y2[last_good] + y3[first_bad:] = y3[last_good] + yB[first_bad:] = yB[last_good] + +y_A = r_pulley + 0.05 +y_floor_render = max(y1.max(), y2.max(), y3.max()) + cube_side + 0.5 + +fig_w, fig_h = 5.0, 8.0 +fig_aspect = fig_w / fig_h +x_lo_d = min(x_m1, x_m2, x_m3) - cube_side/2 - 0.4 +x_hi_d = max(x_m1, x_m2, x_m3) + cube_side/2 + 0.4 +y_lo_d = -0.4 +y_hi_d = y_floor_render + 0.4 +dx, dy = x_hi_d - x_lo_d, y_hi_d - y_lo_d +if dx/dy < fig_aspect: + extra = (fig_aspect * dy - dx) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d - extra, x_hi_d + extra, y_lo_d, y_hi_d +else: + extra = (dx/fig_aspect - dy) / 2.0 + x_lo, x_hi, y_lo, y_hi = x_lo_d, x_hi_d, 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_hi, y_lo) +ax.set_aspect("equal"); ax.axis("off") + +ax.add_patch(patches.Rectangle((x_lo, y_lo), x_hi - x_lo, -y_lo, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [0, 0], color="#adb5bd", lw=2.5, zorder=2) +ax.add_patch(patches.Rectangle((x_lo, y_floor_render), x_hi - x_lo, y_hi - y_floor_render, facecolor="#4a4e69", edgecolor="none", zorder=0)) +ax.plot([x_lo, x_hi], [y_floor_render, y_floor_render], color="#adb5bd", lw=2.5, zorder=2) + +ax.add_patch(patches.Circle((x_A, y_A), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4)) +ax.plot([x_A, x_A], [0, y_A - r_pulley], color="#adb5bd", lw=2.5, zorder=3) +ax.add_patch(patches.Arc((x_A, y_A), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5)) +ax.add_patch(patches.Circle((x_A, y_A), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6)) + +pulley_B_body = patches.Circle((x_B, yB_0), r_pulley, facecolor="#4a4e69", edgecolor="white", lw=1.6, zorder=4) +arc_B = patches.Arc((x_B, yB_0), 2*r_pulley, 2*r_pulley, angle=0, theta1=180, theta2=360, color="#a8dadc", lw=2.5, zorder=5) +hub_B = patches.Circle((x_B, yB_0), r_pulley * 0.18, facecolor="#1a1a2e", edgecolor="#adb5bd", lw=1.0, zorder=6) +ax.add_patch(pulley_B_body); ax.add_patch(arc_B); ax.add_patch(hub_B) + +cube1 = patches.Rectangle((x_m1 - cube_side/2, y1_0), cube_side, cube_side, facecolor="#e63946", edgecolor="white", lw=1.5, zorder=7) +cube2 = patches.Rectangle((x_m2 - cube_side/2, y2_0), cube_side, cube_side, facecolor="#f4d35e", edgecolor="white", lw=1.5, zorder=7) +cube3 = patches.Rectangle((x_m3 - cube_side/2, y3_0), cube_side, cube_side, facecolor="#a8dadc", edgecolor="white", lw=1.5, zorder=7) +ax.add_patch(cube1); ax.add_patch(cube2); ax.add_patch(cube3) + +str_A_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +str_A_right, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_left, = ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) +rope_B_right,= ax.plot([], [], color="#a8dadc", lw=2.5, zorder=5) + +def update(f): + y1f, y2f, y3f, yBf = y1[f], y2[f], y3[f], yB[f] + cube1.set_y(y1f); cube2.set_y(y2f); cube3.set_y(y3f) + pulley_B_body.center = (x_B, yBf) + hub_B.center = (x_B, yBf) + arc_B.set_center((x_B, yBf)) + str_A_left.set_data([x_A - r_pulley, x_A - r_pulley], [y_A, y1f]) + str_A_right.set_data([x_A + r_pulley, x_B], [y_A, yBf - r_pulley]) + rope_B_left.set_data([x_B - r_pulley, x_B - r_pulley], [yBf, y2f]) + rope_B_right.set_data([x_B + r_pulley, x_B + r_pulley], [yBf, y3f]) + return cube1, cube2, cube3, pulley_B_body, hub_B, arc_B, str_A_left, str_A_right, rope_B_left, rope_B_right + +ani = animation.FuncAnimation(fig, update, frames=n_frames, interval=1000/fps, blit=True) +ani.save("compound_atwood.mp4", writer=animation.FFMpegWriter(fps=fps, bitrate=1800), dpi=120) +plt.close(); print("Saved compound_atwood.mp4")