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: spring1D.nlogo

WHAT IS IT?

This is a model of a one-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.

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.

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 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).

The graphics pane in NetLogo is toroidal. This means that when a particle agent moves off the pane to the North (South), it re-enters from the South (North). Similarly when a particle moves off the pane to the East (West), it re-enters from the West (East). However, Newtonian physics assumes Euclidean geometry. Hence, we use short spring lengths to ensure that neither particle crosses the edge of the graphics pane.

The toroidal world is the NetLogo default, but can be changed - see http://ccl.northwestern.edu/netlogo/docs/programming.html#topology

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 moving the MASS_RATIO slider (this must be done before you click SETUP AGENTS)?

Edit the DESIRED_SPRING_LENGTH to be a large value, such as 500. What happens? See Chapter 2 for details.

EXTENDING THE MODEL

Currently, the MASS_RATIO slider is only used during the initialization of the two particles. Modify the code to always monitor this slider (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. If you do this, what is the behavior of the system?

Note, in order to change any NetLogo simulation, you must have the source code (i.e., “spring1D.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 first simulation. 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
; 1D Spring Law Tutorial Code
; For research and educational use only

globals [total_lm total_ke total_pe total_energy center_of_mass_x
         k FR DeltaT D S maxS minS]

turtles-own [hood deltax r F v dv mass ke lm]

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

   set S abs(([xcor] of turtle 1) - 
             ([xcor] of turtle 0))             ; Separation between the turtles

                                               ; 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))
   ask patch (round center_of_mass_x) 0
      [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]

   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))
   ask patch (round center_of_mass_x) 0
      [ask patches in-radius 4 [set pcolor red]]
   
   if (S > maxS) [set maxS S]
   if (S < minS) [set minS S]
   
   set total_lm sum [lm] of turtles            ; Total linear momentum, x-component
   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                             ; Color the particles white
   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 particles along horizontal line
   ifelse (who = 0)                            ; To allow for unequal masses of the two-particle system
      [set mass 1                              ; Particle 0 has mass = 1
       set heading 90                          ; Turn to the right
       fd (1 + random (D / 2))]                ; Move a random amount forward
      [set mass Mass_Ratio                     ; Particle 1 has mass = Mass_Ratio > 1
       set heading 270                         ; Turn to the left
       fd (1 + random (D / 2))]                ; Move a random amount forward
end

to ap                                          ; Run artificial physics
   set v (1 - FR) * v                          ; Slow down according to friction
   
   set hood [who] of other turtles             ; Get the IDs of your neighbors
   foreach hood [                     
      set deltax (([xcor] of turtle ?) - xcor) ; See how far you are from that neighbor
      set r abs(deltax)                        ; The range is the absolute value
      set S r
      ifelse (deltax > 0)                      ; Is the neighbor to your right or left?
         [set F (k * (r - D))]                 ; The spring law if your neighbor is on the right
         [set F (k * (D - r))]                 ; Note sign change to have equal and opposite!
   ]
   set dv DeltaT * (F / mass)
   set v  (v + dv)
   set deltax DeltaT * v 
end

to move                                        ; Move the turtle
   set xcor (xcor + deltax)
   set ke (v * v * mass / 2)                   ; Kinetic energy of the turtle
   set lm (mass * v)                           ; Linear 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