Datasets:
Download scipy/double_spring/596/simulation_code.py from SourajaKundu123/PhySimCode: direct link, hf CLI and curl.
- Browser
- Download file 2.75 kB
-
https://huggingface.co/datasets/SourajaKundu123/PhySimCode/resolve/main/scipy/double_spring/596/simulation_code.py
- Command line
-
hf download hf://datasets/SourajaKundu123/PhySimCode/scipy/double_spring/596/simulation_code.py
-
curl -L -o simulation_code.py https://huggingface.co/datasets/SourajaKundu123/PhySimCode/resolve/main/scipy/double_spring/596/simulation_code.py
2.75 kB
| """ | |
| Double Spring System — auto-generated simulation script. | |
| Physics (Newton's 2nd Law for each block): | |
| v̇₁ = [-k(x₁-wall₁-R) + k(x₂-x₁-R) - b·v₁] / m₁ | |
| v̇₂ = [-k(x₂-x₁-R) + k(wall₂-x₂-R) - b·v₂] / m₂ | |
| Layout: [wall1=0] ~k~ [block1] ~k~ [block2] ~k~ [wall2=6.00] | |
| """ | |
| 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, m1, m2, b = 2.3748262467903514, 0.7858876248888906, 0.9939150201480946, 0.3116920419359951 | |
| R = 2.0 | |
| wall1, wall2 = 0.0, 6.0000 | |
| x1_0 = 2.0000 + -0.452533746323561 | |
| x2_0 = 4.0000 + 0.7554128570203134 | |
| v1_0, v2_0 = 0.0000, -0.2135 | |
| duration = 12 | |
| def ode(t, y): | |
| x1, x2, v1, v2 = y | |
| s1 = (x1-wall1)-R; s2 = (x2-x1)-R; s3 = (wall2-x2)-R | |
| dv1 = (-k*s1 + k*s2 - b*v1)/m1 | |
| dv2 = (-k*s2 + k*s3 - b*v2)/m2 | |
| return [v1, v2, dv1, dv2] | |
| fps=30; t_eval=np.linspace(0,duration,int(duration*fps)+1) | |
| sol=solve_ivp(ode,(0,duration),[x1_0,x2_0,v1_0,v2_0],t_eval=t_eval,method="RK45",rtol=1e-9,atol=1e-11) | |
| x1a,x2a=sol.y[0],sol.y[1] | |
| bw,bh,ty=0.3,0.35,0.0 | |
| fig,ax=plt.subplots(figsize=(9,3),facecolor="#1a1a2e") | |
| ax.set_facecolor("#16213e"); ax.set_xlim(wall1-0.4,wall2+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,sgn in [(wall1,1),(wall2,-1)]: | |
| ax.add_patch(patches.Rectangle((wx+(sgn*0.02-(0.15 if sgn<0 else 0)),ty-0.6),0.15,1.2, | |
| facecolor="#4a4e69",edgecolor="#adb5bd",lw=1,hatch="////")) | |
| def sp_path(x1,x2,n=6,r=0.18): | |
| t=np.linspace(0,1,n*20+2) | |
| env=np.where((t<0.08)|(t>0.92),0,np.sin(n*2*np.pi*(t-0.08)/(1-2*0.08))) | |
| return x1+t*(x2-x1), ty+r*env | |
| s1l,=ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) | |
| s2l,=ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) | |
| s3l,=ax.plot([],[],"-",color="#a8dadc",lw=2.5,zorder=3) | |
| b1p=patches.FancyBboxPatch((x1_0-bw/2,ty-bh/2),bw,bh,boxstyle="round,pad=0.02", | |
| lw=1.5,edgecolor="white",facecolor="#e63946"); ax.add_patch(b1p) | |
| b2p=patches.FancyBboxPatch((x2_0-bw/2,ty-bh/2),bw,bh,boxstyle="round,pad=0.02", | |
| lw=1.5,edgecolor="white",facecolor="#f4d35e"); ax.add_patch(b2p) | |
| def update(f): | |
| b1,b2=x1a[f],x2a[f] | |
| b1p.set_x(b1-bw/2); b2p.set_x(b2-bw/2) | |
| s1l.set_data(*sp_path(wall1,b1-bw/2)); s2l.set_data(*sp_path(b1+bw/2,b2-bw/2)) | |
| s3l.set_data(*sp_path(b2+bw/2,wall2)); return s1l,s2l,s3l | |
| ani=animation.FuncAnimation(fig,update,frames=len(sol.t),interval=1000/fps,blit=False) | |
| ani.save("double_spring.mp4",writer=animation.FFMpegWriter(fps=fps,bitrate=1800),dpi=120) | |
| plt.close(); print("Saved double_spring.mp4") | |