This page was automatically generated by NetLogo 5.0.

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Sun's Java site.


powered by NetLogo

view/download model file: spring2D.nlogo

WHAT IS IT?

This is a model of a two-dimensional spring, for the book entitled “Physicomimetics: Physics-Based Swarm Intelligence.”

HOW IT WORKS

Two particles use F = ma and Hooke’s law to move as a spring. But now the spring is given angular momentum, allowing it to spin around the center of mass.

HOW TO USE IT

Click SETUP AGENTS to initialize the two particles, and click MOVE AGENTS to have them move.

The MASS_RATIO slider allows you to control the relative masses of the particles at the ends of the spring, at initialization. Changing this slider while the simulation is running will have no effect.

The ANGULAR_MOTION slider allows you to impart a spin to the system at initialization. The spin is established very carefully to make sure that there is no linear momentum at initialization. Changing this slider while the simulation is running will have no effect.

All other sliders will affect the simulation when it is running.

THINGS TO NOTICE

This simulation serves to teach you a simple model of a spring, as well as introduce you to more advanced concepts, such as the Conservation of Linear Momentum, Conservation of Angular Momentum, and the Conservation of Energy. This is covered in detail in Chapter 2 of the book.

Note how raising the TIME_STEP introduces very mild variations into the Conservation of Energy graph. The total energy is shown in brown, the kinetic energy is in green, and the potential energy is in blue. The total energy stays relatively constant while there is a constant tradeoff between potential and kinetic energy. Watch the simulation - when is kinetic energy high? When is potential energy high?

What happens when FRICTION is raised from 0.000 to 0.001? Where has the energy gone?

The red dot in the simulation shows the center of mass of the system. If the Conservation of Linear Momentum holds, the red dot will not move. In this simulation, if one of the particles crosses the boundary of the world (re-entering from the other side), the center of mass will change because the standard physics assumption of an Euclidean geometry has been broken (as explained in Chapter 2).

This simulation also includes a monitor for the Angular Momentum and you will see that it does not change over time, unless one of the particles crosses the boundary of the world (re-entering from the other side). Again, this is because the standard physics assumption of an Euclidean geometry has been broken (as explained in Chapter 2).

THINGS TO TRY

See how the SPRING_CONSTANT changes behavior.

What happens when you damp the spring with FRICTION?

What happens if you make one of the particles heavier by using the MASS_RATIO slider (this must be done before you click SETUP AGENTS)?

Try different values of ANGULAR_MOMENTUM (even zero).

EXTENDING THE MODEL

Currently, the MASS_RATIO slider is only used during the initialization of the two particles. Hence, changing this slider while the simulation is running will have no effect. Modify the code to monitor this slider always (hint: you can do this in the “update-info” procedure), so that the mass of particle 1 can be changed dynamically.

Introduce a third particle and create a more complex structure that contains three springs. Use different spring constants for the three springs. One way to do this is to initially create three particles (by saying “crt 3”): 0, 1, and 2. Then you will need to modify “setup-turtles” so that it initializes all three particles in an appropriate manner. Finally, the “ap” procedure needs to be modified so that it knows which pair of particles are interacting (0 and 1, 0 and 2, or 1 and 2). You will want to have a different value of “k” for each pair. Impart a spin to the system. What happens?

Note, in order to change any NetLogo simulation, you must have the source code (i.e., “spring2D.nlogo”) downloaded to your computer, as well as NetLogo itself. You can not change the code when you are running the simulation with your browser.

NETLOGO FEATURES

Since we are using a patch size of one, we wanted the particles to be more visible. This is done with “set size 5” in the code. However, they are still considered to be point particles (with no size) in the simulation.

Note also how the “do-plots” procedure draws the Energy graph and the Separation graph.

NetLogo provides built-in commands to model springs, but for the purposes of this book it is better to see how a spring works from first principles. In fact, one of the core concepts of this book is that the better we understand first principles, the more elegant our solutions will be.

RELATED MODELS

This is our second simulation, which builds on the one-dimensional spring model. It will be generalized more and more throughout the book.

CREDITS AND REFERENCES

HOW TO CITE

If you mention this model in an academic publication, we ask that you include these citations for the model itself and for the NetLogo software:
- Spears, William M. and Spears, Diana F. (eds.) Physicomimetics: Physics-Based Swarm Intelligence, Springer-Verlag, (2011).
- Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

COPYRIGHT NOTICE

Copyright 2011 William M. Spears. All rights reserved.

Permission to use, modify or redistribute this model is hereby granted, provided that both of the following requirements are followed:
a) this copyright notice is included, and
b) this model will not be redistributed for profit without permission from William M. Spears. Contact William M. Spears for appropriate licenses for redistribution for profit.

http://www.swarmotics.com

CODE

; William M. Spears September 2011
; 2D Spring Law Tutorial Code
; For research and educational use only

globals [total_lmx total_lmy total_angular_mom total_ke total_pe total_energy 
         center_of_mass_x center_of_mass_y k FR DeltaT D S maxS minS]

turtles-own [hood deltax deltay r F Fx Fy v vx vy dvx dvy mass ke 
             lmx lmy theta lever_arm_x lever_arm_y lever_arm_r angular_mom]

to setup
   clear-all                                   ; Clear everything
   crt 2                                       ; Create two turtles (particles)
   update-info                                 
   set maxS 0 set minS 100000                  ; Initialize some variables
   ask turtles [setup-turtles]                 ; Set up the two particles
   
                                               ; Compute initial separation
   set S sqrt (((([xcor] of turtle 1) - ([xcor] of turtle 0)) * 
                (([xcor] of turtle 1) - ([xcor] of turtle 0))) + 
               ((([ycor] of turtle 1) - ([ycor] of turtle 0)) * 
                (([ycor] of turtle 1) - ([ycor] of turtle 0))))

                                               ; Computes center of mass and displays location
   set center_of_mass_x (([mass] of turtle 0) * ([xcor] of turtle 0) + 
                         ([mass] of turtle 1) * ([xcor] of turtle 1)) / 
                        (([mass] of turtle 0) + ([mass] of turtle 1))
   set center_of_mass_y (([mass] of turtle 0) * ([ycor] of turtle 0) + 
                         ([mass] of turtle 1) * ([ycor] of turtle 1)) / 
                        (([mass] of turtle 0) + ([mass] of turtle 1))
   ask patch (round center_of_mass_x) (round center_of_mass_y)
      [ask patches in-radius 4 [set pcolor red]]
   reset-ticks
end

to run-and-monitor
   if (count turtles < 1) [user-message "Please click HALT and then SETUP AGENTS first" stop]
   update-info
   ask turtles [ap]
   ask turtles [move]
                                               ; Computes center of mass and displays location
   set center_of_mass_x (([mass] of turtle 0) * ([xcor] of turtle 0) + 
                         ([mass] of turtle 1) * ([xcor] of turtle 1)) / 
                        (([mass] of turtle 0) + ([mass] of turtle 1))
   set center_of_mass_y (([mass] of turtle 0) * ([ycor] of turtle 0) + 
                         ([mass] of turtle 1) * ([ycor] of turtle 1)) / 
                        (([mass] of turtle 0) + ([mass] of turtle 1))
   ask patch (round center_of_mass_x) (round center_of_mass_y)
      [ask patches in-radius 4 [set pcolor red]]

   if (S > maxS) [set maxS S]
   if (S < minS) [set minS S]

   set total_lmx sum [lmx] of turtles          ; Total linear momentum, x-component
   set total_lmy sum [lmy] of turtles          ; Total linear momentum, y-component
   set total_angular_mom sum [angular_mom] of turtles
   set total_ke sum [ke] of turtles            ; Total kinetic energy of both particles
   set total_pe (k * (S - D) * (S - D) / 2)    ; Compute potential energy
   set total_energy (total_ke + total_pe)      ; Total energy of the two-particle system
   tick
   do-plots
end

to setup-turtles
   set color white set vy 0                    ; Start with no y motion on either particle
   home                                        ; Set particles at (0,0) which is the center of the screen
   set shape "circle" set size 5               ; Draw the particle as a large circle
   set heading random 360 fd (1 + random D)    ; Each particle receives a random heading and moves forward
   
   ifelse (who = 0)                            ; To allow for unequal masses of the two-particle system
      [set mass 1 set vx Angular_Motion]       ; Particle 0 has mass = 1
      [set mass Mass_Ratio                     ; Particle 1 has mass = Mass_Ratio > 1
       set vx (- Angular_Motion) / Mass_Ratio]
   set theta 0
end

to ap                                          ; Run artificial physics
   set vx (1 - FR) * vx                        ; Slow down according to friction
   set vy (1 - FR) * vy
   
   set hood [who] of other turtles             ; Get the IDs of your neighbors
   foreach hood [                     
      set deltax (([xcor] of turtle ?) - xcor) 
      set deltay (([ycor] of turtle ?) - ycor) 
      set r sqrt (deltax * deltax + deltay * deltay)
      set S r
      set F (k * (r - D))                      ; The spring law
      set Fx (F * (deltax / r))                ; The x-component of force
      set Fy (F * (deltay / r))                ; The y-component of force
   ]
   
   set dvx DeltaT * (Fx / mass)
   set dvy DeltaT * (Fy / mass)
   set vx  (vx + dvx)                          ; The x-component of velocity
   set vy  (vy + dvy)                          ; The y-component of velocity
   set deltax DeltaT * vx
   set deltay DeltaT * vy 
end

to move                                        ; Move the turtle
   set xcor (xcor + deltax)
   set ycor (ycor + deltay)

   set lmx (mass * vx)                         ; Linear momentum of the turtle
   set lmy (mass * vy)
   
   set v sqrt (vx * vx + vy * vy)
   set ke (v * v * mass / 2)                   ; Kinetic energy of the turtle
   set lever_arm_x (xcor - center_of_mass_x)
   set lever_arm_y (ycor - center_of_mass_y)
   set lever_arm_r sqrt (lever_arm_x * lever_arm_x + lever_arm_y * lever_arm_y)
   if (((vx != 0) or (vy != 0)) and ((lever_arm_x != 0) or (lever_arm_y != 0)))
      [set theta (atan (mass * vy) (mass * vx)) - (atan lever_arm_y lever_arm_x)]
   set angular_mom (lever_arm_r * mass * v * (sin theta)) ; Angular momentum of the turtle
end

to update-info                                 ; Update information from the sliders
   set k Spring_Constant
   set FR Friction
   set DeltaT Time_Step
   set D Desired_Spring_Length
end

to do-plots
   set-current-plot "Energy"                   ; Select the Energy plot
   set-current-plot-pen "Total"                ; Select the Total Energy Pen
   plot total_energy                           ; Plot the total_energy
   set-current-plot-pen "Potential"
   plot total_pe
   set-current-plot-pen "Kinetic"
   plot total_ke

   set-current-plot "Separation"               ; Select the Separation plot
   set-plot-y-range precision (minS - 1) 0     ; Set the range of the y-axis
                    precision (maxS + 1) 0
   set-current-plot-pen "Sep"                  ; Select the Sep pen
   plot S                                      ; Plot the separation
end