Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
repository:solar_system_springs [2020/09/03 17:59]
porcaro1 [Activity]
repository:solar_system_springs [2021/04/07 23:08]
porcaro1
Line 15: Line 15:
 ===Handout=== ===Handout===
 ** Solar System Springs **  ** Solar System Springs ** 
 +{{ :​repository:​spring_spring.png?​nolink&​600|}}
  
 You and your team have the opportunity to board the Sol Spacecraft to explore the solar system. Should you choose to accept the mission, NASA requires you to study the action of springs on each of the planets. Some planets do not have a solid surface, so your experiment will be conducted as you hover deep within the planet'​s atmosphere. Your experiments will examine the effect of each planet'​s gravitational field on the oscillation of a spring. You also have another container filled with many different masses. You and your team have the opportunity to board the Sol Spacecraft to explore the solar system. Should you choose to accept the mission, NASA requires you to study the action of springs on each of the planets. Some planets do not have a solid surface, so your experiment will be conducted as you hover deep within the planet'​s atmosphere. Your experiments will examine the effect of each planet'​s gravitational field on the oscillation of a spring. You also have another container filled with many different masses.
Line 38: Line 39:
     - Case 5: The mass is between the equilibrium and bottom  ​     - Case 5: The mass is between the equilibrium and bottom  ​
   - Create a diagram showing the acceleration and velocity vectors for each of the aforementioned cases.   - Create a diagram showing the acceleration and velocity vectors for each of the aforementioned cases.
-  - Run the [[https://​trinket.io/​glowscript/​bf533ef826 | minimally working code]]. The code allows the user to input the value for the spring constant ($k$) and change the mass of the crate ($m$) using a slider. What problem(s) do you see with the simulation?+  - Run the minimally working code (below). The code allows the user to input the value for the spring constant ($k$) and change the mass of the crate ($m$) using a slider. What problem(s) do you see with the simulation?
  
 Fortunately,​ the code is not entirely complete; you have a fantastic opportunity to fix it! Modify the code to make the spring oscillate appropriately. Also, add some lines of code so that you can change the acceleration due to gravity and perform a check to make sure the value is in the "​down"​ direction (the "​up"​ direction is set as positive). Make sure that the acceleration due to gravity is reported in the window that prints the mass and spring constant. Once you have verified that your code is running properly, complete the following questions. Fortunately,​ the code is not entirely complete; you have a fantastic opportunity to fix it! Modify the code to make the spring oscillate appropriately. Also, add some lines of code so that you can change the acceleration due to gravity and perform a check to make sure the value is in the "​down"​ direction (the "​up"​ direction is set as positive). Make sure that the acceleration due to gravity is reported in the window that prints the mass and spring constant. Once you have verified that your code is running properly, complete the following questions.
Line 52: Line 53:
  
 ===Code=== ===Code===
-[[https://trinket.io/glowscript/bf533ef826 ​| Link]]+[[https://www.glowscript.org/#/​user/​porcaro1/​folder/​RepositoryPrograms/​program/SolarSystemSprings-Incomplete ​| Link]]
 <code Python [enable_line_numbers="​true"​]>​ <code Python [enable_line_numbers="​true"​]>​
 GlowScript 2.7 VPython GlowScript 2.7 VPython
Line 158: Line 159:
 ====Answer Key==== ====Answer Key====
 ===Handout=== ===Handout===
 +==Pre-coding Questions==
 +  - {{:​repository:​freebody1fr.jpg?​nolink&​600|}}
 +  - {{:​repository:​pendulumvectors.png?​nolink&​600|}}
 +  - There are 2 major problems with the code:
 +    - The program does not stop, despite there being code alluding to a 20 second run-time (line 5)
 +    - The spring does not oscillate, but rather extends infinitely. This is due to the "​Fnet"​ in line 69 only including the force of gravity but not the force of the spring ​
 +
 +==Post-coding Questions==
 +  - {{:​repository:​spring_graphs.png?​nolink&​600|}}
 ===Code=== ===Code===
-<code Python [enable_line_numbers="​true",​ highlight_lines_extra=""​]>​+[[https://​www.glowscript.org/#/​user/​porcaro1/​folder/​RepositoryPrograms/​program/​SolarSystemSprings-Solution | Link]] 
 +<code Python [enable_line_numbers="​true",​ highlight_lines_extra="​8,​9,​10,​11,​63,​64,​69,​74,​84"]> 
 +GlowScript 2.7 VPython 
 + 
 +running=True ​    #Sets up condition for pause/run button 
 + 
 +totalTime=20 #in seconds 
 + 
 +#​Variables/​Objects 
 +g=float(prompt("​Acceleration due to Gravity?: ")) 
 +while g>0: 
 +  g=0 
 +  g=float(prompt("​ERROR:​ Acceleration due to Gravity is down: ")) 
 +k=int(prompt("​Spring constant?: "​))#​prompt allows user to set spring constant 
 + 
 +#Graphs below measure position, velocity, and acceleration as a function of time 
 +f4 = graph(xmin=0,​xmax=totalTime,​xtitle='<​i>​t</​i>​ in seconds'​) 
 +f5 = gcurve(color=color.green,​ label='​position',​fast=True) 
 +f6 = gcurve(type='​scatter',​ color=color.blue,​ label='​velocity',​fast=True) 
 +f7 = gcurve(type='​scatter',​ color=color.red,​ label='​acceleration',​fast=True) 
 +f1 = graph(xmin=0,​xmax=totalTime,​xtitle='<​i>​t</​i>​ in seconds'​) 
 +f1 = gcurve(color=color.green,​ label='​position',​fast=True) 
 +f2 = graph(xmin=0,​xmax=totalTime,​xtitle='<​i>​t</​i>​ in seconds'​) 
 +f2 = gcurve(type='​scatter',​ color=color.blue,​ label='​velocity',​fast=True) 
 +f3 = graph(xmin=0,​xmax=totalTime,​xtitle='<​i>​t</​i>​ in seconds'​) 
 +f3 = gcurve(type='​scatter',​ color=color.red,​ label='​acceleration',​fast=True) 
 + 
 +#The mass and spring hang from the ceiling 
 +ceiling = box(pos=vec(0,​11.5,​0),​size=vec(8,​1,​1),​ color=color.red) 
 + 
 +Mass = box(pos=vector(0,​-10,​0),​size=vec(2,​2,​2),​velocity=vector(0,​0,​0),​color=color.green,​mass=1) 
 + 
 +def setmass(s):​ 
 +  return 
 +#Using a slider for the mass allows the user to change the mass, ranging from 1 to 5 kg 
 +mass = slider( min=1, max=5, value=1,​label='​Mass'​ ,​align='​right',​step=0.5,​ length=220, bind=setmass) 
 + 
 +pivot = vector(0,​10,​0) 
 + 
 +spring = helix(pos=pivot,​axis=Mass.pos-pivot,​radius=0.8,​coils=20,​constant=k,​thicnkess=0.1,​color=color.green) 
 +#When you hang a mass from a spring, the spring stretches and the equilibrium point gets moved down 
 +eq = vector(0,​Mass.mass*g/​k,​0) 
 + 
 +#We are starting with time t=0, and changing every .05 seconds 
 +t = 0 
 +dt = .05 #if time step is smaller than this, then completion time will not be the total time duration. 
 +print('​mass = 1 kg | ', '​spring constant = ', k,'​N/​m',​ ' | gravity is:',​g,​ '​m/​s/​s'​) 
 + 
 +#The code below allows the user to pause/run the program by pushing a button at the top 
 +def Run(b): 
 +      global running 
 +      running = not running 
 +      if running: 
 +        b.text = "​Pause"​ 
 +        running=True 
 +      else:  
 +        b.text = "​Run"​ 
 +        running=False 
 +button(text="​Pause",​ pos=scene.title_anchor,​ bind=Run) ​  
 + 
 +#In the while loop below, the forces (gravity, spring, and net) are being updated 
 +#Also, the acceleration,​ velocity, and position of the box are being updated 
 + 
 +while True: 
 +    if t > totalTime:​ 
 +      running=False 
 +   
 +    if running==True:​ 
 +      frameRate = 1/dt 
 +      rate(frameRate) 
 +      Fg = vec(0,​Mass.mass*g,​0) 
 +      if eq.y < Mass.pos.y:​ 
 +        Fs = vec(0, -k*mag(Mass.pos-eq),​0) 
 +      else: 
 +        Fs = vec(0, k*mag(Mass.pos-eq),​0) 
 +      Fnet = Fg + Fs 
 +      acc = Fnet/​(Mass.mass) 
 + 
 +       
 +      Mass.velocity = Mass.velocity+acc*dt 
 +      Mass.pos = Mass.pos+Mass.velocity*dt 
 +      spring.axis = Mass.pos-spring.pos 
 + 
 +       
 +      if mass.value != Mass.mass:​ 
 +        print('​mass =  ', mass.value,'​kg'​ ,'| spring constant = ', k,'​N/​m',​ '| gravity is:', g, '​m/​s/​s'​) 
 +     
 +      Mass.mass=mass.value 
 +     
 +     
 +      f1.plot(t,​Mass.pos.y) 
 +      f2.plot(t,​Mass.velocity.y) 
 +      f3.plot(t,​acc.y) 
 +      f5.plot(t,​Mass.pos.y) 
 +      f6.plot(t,​Mass.velocity.y) 
 +      f7.plot(t,​acc.y) 
 + 
 +   
 +      t = t + dt 
 +    if running==False:​ 
 +       
 +      frameRate =1/dt 
 +      rate(frameRate) 
 +      acc = (eq-Mass.pos)*(spring.constant/​Mass.mass)*0 
 +      Mass.velocity = Mass.velocity 
 +      Mass.pos = Mass.pos 
 +      spring.axis = spring.axis
 </​code>​ </​code>​
  
 ---- ----
 ====See Also==== ​ ====See Also==== ​
 +  * [[bungee_jump | Bungee Jump]]
  • repository/solar_system_springs.txt
  • Last modified: 2021/04/07 23:30
  • by porcaro1