import psutil
L = psutil.Process().parent().cmdline()
if any("jupyter-notebook" in string for string in L):
%matplotlib notebook
elif any("jupyterhub" in string for string in L):
%matplotlib widget
else:
%matplotlib inline
from numpy import sin, cos
import numpy as np
import time
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import scipy.integrate as integrate
import matplotlib.animation as animation
from collections import deque
from scipy.integrate import solve_ivp
%matplotlib notebook
m1=20 #kg
m2=375 #kg
m = [m1,m2]
b =9800 #N/(m/s)
kw=1e6 #N/m
ks=1.3e5 #N/m
# create a time array from 0..t_stop sampled at 0.02 second steps
dt = 0.01 # time step
t_stop = 2 # how many seconds to simulate
TSPAN = [0, t_stop] #simulation time spa
TIME = np.arange(0, t_stop, dt) # simulation time serie
y0 = [0,0,0,0]
history_len = 1000 # how many trajectory points to display
r=0
offset1=0.3 #The offset used by the wheel equilibrium equations [m]
offset2=0.5#The offset used by the wheel equilibrium equations [m]
L=0.5
H=0.2
def odefun(t, y, m, b, kw, ks ):
if t<1:
u=0
else:
u=0.2
m1=m[0];
m2=m[1];
dx = y[2] # third element of y(t) => vx
dy = y[3] # fourth element of y(t) => vy
dvx= kw/m1*u-b/m1*(dx-dy)-ks/m1*(y[0]-y[1])-kw/m1*y[0]
dvy= -b/m2*(dy-dx)-ks/m2*(y[1]-y[0])
return [dx,dy,dvx,dvy]
yout = solve_ivp(odefun, TSPAN, y0, args=(m, b, kw, ks ), dense_output=True)
x = yout.sol(TIME)[0,:]
y = yout.sol(TIME)[1,:]
vx= yout.sol(TIME)[2,:]
vy= yout.sol(TIME)[3,:]
plt.close("all")
fig = plt.figure(figsize=(9, 9))
ax = fig.add_subplot(3,1,1,autoscale_on=False, xlim=(0,t_stop),ylim=(x.min(),y.max()+2*offset1+offset2))
trace, = ax.plot([], [], ',-', lw=2)
#ax.set_aspect('equal')
plt.xlabel("Positions of both masses [m]")
ax.grid()
ax1 = fig.add_subplot(3,1,2,autoscale_on=False, xlim =(0,t_stop),ylim=(x.min(),x.max()))
trace1, = ax1.plot([], [], ',-', lw=2)
plt.xlabel("Travel time [s]")
plt.ylabel("Position of mass 1 [m/s]")
ax1.grid()
ax2 = fig.add_subplot(3,1,3,autoscale_on=False, xlim =(0,t_stop),ylim=(y.min(),y.max()))
trace2, = ax2.plot([], [], ',-', lw=2)
plt.xlabel("Travel time [s]")
plt.ylabel("Position of mass 2 [m/s]")
ax2.grid()
line, = ax.plot([], [], '-', lw=2)
patch = plt.Circle((r, -r), r)
ax.add_patch(patch)
time_template = 'time = %.1fs'
time_text = ax.text(0.03, 0.8, '', transform=ax.transAxes)
history_x, history_y = deque(maxlen=history_len), deque(maxlen=history_len)
history_x1, history_y1 = deque(maxlen=history_len), deque(maxlen=history_len)
history_x2, history_y2 = deque(maxlen=history_len), deque(maxlen=history_len)
plt.show()
def animate(i):
t = time.time()
if TIME[i]<1:
u=0
else:
u=0.2
thisx = [TIME[i]-L,TIME[i]+L,TIME[i]+L, TIME[i]-L ,TIME[i]-L ]
thisy = [offset2+y[i]+H,offset2+y[i]+H,offset2+y[i]-H,offset2+y[i]-H,offset2+y[i]+H]
line.set_data(thisx, thisy)
time_text.set_text(time_template % (i*dt))
patch.center = (TIME[i],(x[i]+offset1)-(x[i]+offset1-u)/2)
patch.radius = (x[i]+offset1-u)/2
ax.add_patch(patch)
if i == 0:
history_x.clear()
history_y.clear()
history_x.appendleft(TIME[i])
history_y.appendleft(u)
trace.set_data(history_x, history_y)
if i == 0:
history_x1.clear()
history_y1.clear()
history_x2.clear()
history_y2.clear()
history_x1.appendleft(TIME[i])
history_y1.appendleft(x[i])
trace1.set_data(history_x1, history_y1)
history_x2.appendleft(TIME[i])
history_y2.appendleft(y[i])
trace2.set_data(history_x2, history_y2)
elapsed = time.time() - t
time.sleep(dt-elapsed)
return line, time_text
ani = animation.FuncAnimation(fig, animate, frames=len(x), interval=dt*1000, blit=True,repeat=False)
plt.show()