After a long day of work, you return home to be greeted by your favorite feline companion, TicTac. Most days, you would usually play with TicTac by tossing his favorite toy (a bright, red block stuffed full of catnip) around the room and watching him chase it from one end to the other. Unfortunately, today you are just too tired to throw TicTac's block. Instead you decide to fix the block to a spring so that TicTac can get his exercise with as little effort as possible on your end. You attach the apparatus to a wall, giving the block a slight push, and it begins oscillating back and forth. TicTac is more excited than ever, as his block is now moving in a way that he never knew possible. Deciding that you are curious about this unique form of motion, you choose to computationally model the motion in GlowScript.
GlowScript 2.7 VPython L0 = 5 #Spring length k = 1 #Spring constant track = box(pos = vec(0,0,0), size = vec(10,0.1,2), color = color.gray(0.7)) wall = box(pos = vec(-4.75,1,0), size = vec(0.5,2,2), color = color.gray(0.7)) block = box(pos = vec(0,1,0), size = vec(2,2,2), color = color.red) spring = helix(pos = wall.pos, size = vec(mag(block.pos-wall.pos),0.75,0.75), axis = block.pos- wall.pos, color = color.red) spring.thickness = 0.1 L = block.pos - spring.pos Lhat = L/mag(L) s = mag(L) - L0 Fspring = vec(0,0,0) Fnet = vec(0,0,0) block.mass = 0.3 block.p = vec(1.05,0,0) t = 0 dt = 0.05 while t < 100: rate(100) L = block.pos - spring.pos Lhat = L/mag(L) s = mag(L) - L0 block.p = block.p + Fnet*dt block.pos = block.pos + block.p/block.mass*dt spring.axis = Lhat spring.size = vec(mag(L),0.75,0.75) t = t + dt
Link Horizontal Spring (with Optional Linear Damping)
GlowScript 2.7 VPython L0 = 5 #Spring length k = 1 #Spring constant c = 0.05 #Damping coefficient stretch_initial = -3 #Negative means compress, positive means elongate t_final = 100 #Use 3.4 for 2 full periods, use 100 for a long run track = box(pos = vec(0,0,0), size = vec(10,0.1,2), color = color.gray(0.7)) wall = box(pos = vec(-5,1,0), size = vec(0.5,2,2), color = color.gray(0.7)) block = box(pos = vec(stretch_initial,1,0), size = vec(2,2,2), color = color.red) spring = helix(pos = wall.pos, size = vec(mag(block.pos-wall.pos),0.75,0.75), axis = block.pos-wall.pos, color = color.red) spring.thickness = 0.1 L = block.pos - spring.pos Lhat = L/mag(L) s = mag(L) - L0 Fspring = vec(0,0,0) Fnet = vec(0,0,0) block.mass = 0.3 block.p = vec(0,0,0) Uspring=0.5*k*s**2 Kspring=0.5*block.mass*(mag(block.p/block.mass))**2 Espring=Uspring+Kspring UGraph = series( color=color.red ) KGraph = series( color=color.blue ) EGraph = series( color=color.green ) t = 0 dt = 0.001 while t < t_final: rate(5000) L = block.pos - spring.pos Lhat = L/mag(L) s = mag(L) - L0 Fspring=-1*k*s*Lhat Fnet=Fspring #(Linear damping)# -c*block.p/block.mass block.p = block.p + Fnet*dt block.pos = block.pos + block.p/block.mass*dt Uspring=0.5*k*s**2 Kspring=0.5*block.mass*(mag(block.p/block.mass))**2 Espring=Uspring+Kspring spring.axis = Lhat spring.size = vec(mag(L),0.75,0.75) UGraph.plot(t,Uspring) KGraph.plot(t,Kspring) EGraph.plot(t,Espring) t = t + dt
Vertical Spring with Gravity Link
GlowScript 2.7 VPython #System parameters: Length of spring at equilibrium (L0), spring constant (k), damping constant (c) g = 10 mBlock = 0.3 L0 = 5 k = 500 c = 0.3 #Objects: track, wall, block, and spring #track = box(pos = vec(0,0,0), size = vec(10,0.1,2), color = color.gray(0.7)) wall = box(pos = vec(0,5,0), size = vec(3,0.5,3), color = color.gray(0.7)) block = box(pos = vec(0,2.5,0), size = vec(2,2,2), color = color.red) spring = helix(pos = wall.pos, size = vec(mag(block.pos-wall.pos),0.75,0.75), axis = block.pos-wall.pos, color = color.red) spring.thickness = 0.1 #Spring thickness attribute #Initializing system variables: Length of spring (L), length unit vector (Lhat), and stretch or displacement from equilibrium (s) L = block.pos - spring.pos Lhat = L/mag(L) s = mag(L) - L0 #Initializing empty force vectors Fspring = vec(0,0,0) Fdamp = vec(0,0,0) Fgrav = vec(0,0,0) Fnet = vec(0,0,0) #Block attributes block.mass = mBlock block.p = vec(0,0,0) #Initialize data series for U, K, and E graphs. http://www.glowscript.org/docs/GlowScriptDocs/graph.html Ugraph = series(color=color.green) Kgraph = series(color=color.red) Egraph = series(color=color.blue) #Time and time step t = 0 dt = 0.00005 #Trigger for program to begin with mouse click print("Click the program window to begin.") ev = scene.waitfor('click') print("The program is running.") #Begin update loop while t<10: rate(5000) L = block.pos - spring.pos Lhat = L/mag(L) s = mag(L) - L0 Fspring = -1*k*s*Lhat Fdamp = -1*c*block.p/block.mass Fgrav = block.mass*vec(0,-g,0) Fnet = Fspring+Fdamp+Fgrav block.p = block.p + Fnet*dt block.pos = block.pos + block.p/block.mass*dt spring.axis = Lhat spring.size = vec(mag(L),0.75,0.75) U = 0.5*k*s**2 K = 0.5*block.mass*mag(block.p/block.mass)**2 E = U + K Ugraph.plot(t,U) Kgraph.plot(t,K) Egraph.plot(t,E) t = t + dt print("the program is complete.")