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: formation_hooke.nlogo
This is our first physics-based model of a swarm, for the book entitled “Physicomimetics: Physics-Based Swarm Intelligence.”
Multiple particles use F = ma and Hooke’s law to self-organize into a triangular lattice.
Click SETUP AGENTS to initialize the particles, and click MOVE AGENTS to have them move.
The CLEAR button will clear the graphics, which becomes handy when particles have their pens down (more on this below).
The NUMBER_OF_PARTICLES slider allows you to control the number of particles created at initialization. Changing this slider while the simulation is running will have no effect. You can change the number of particles while the simulation is running by using the ONE IS BORN and KILL ONE buttons. However, the ONE IS BORN and KILL ONE buttons do not change the number of initial particles when the simulation is restarted by clicking SETUP AGENTS.
The ONE IS BORN button creates a new particle.
The KILL ONE button randomly kills an existing particle.
All other sliders will affect the simulation when it is running.
Particles are initialized in a random cluster in the middle of the graphics pane, and self-organize into a triangular lattice. The lattice is not perfect, but that is OK because we are interested in “satisficing systems,” as opposed to “optimal systems.”
The SPRING_CONSTANT controls the stiffness of the springs that connect all pairs of particles that are within sensor range. The DESIRED_SEPARATION is the same as the desired spring length between particles.
Unlike the prior models, FRICTION is enabled by default. This allows the system to stabilize.
This simulation serves to teach you about physics-based swarms, as well as the Conservation of Linear and Angular Momenta. This is covered in detail in Chapter 3 of the book.
The red dot in the simulation shows the center of mass of the system. If the Conservation of Linear Momentum holds in both the x- and y-dimensions, the red dot will not move. This simulation includes a monitor for the Angular Momentum and you will see that it does not change over time, if the system is closed.
This model allows you to add and remove particles. Removing particles allows you to test how robust the system is to particle failure. Adding particles allows you to see how scalable the system is, and illustrates just how nicely new particles are incorporated into the lattice. Adding and removing particles opens the system, and temporary changes in the momenta can occur during those events.
Although it doesn’t look like the linear momentum is shown on the graph, this is because it is overdrawn by the angular momentum curve. If you slow the simulation down (with the speed slider) and add a particle, you will usually see all three curves.
See how the FRICTION changes behavior. Click SETUP AGENTS, lower the friction, and click MOVE AGENTS. What happens?
Similarly, change the SPRING_CONSTANT.
Change the DESIRED_SEPARATION while the system is running. Try changing it slowly and then change it quickly. What happens?
Add and remove particles.
Set the NUMBER_OF_PARTICLES to five and maximize the other sliders (slide them to the right). Also, increase the speed with the speed slider at the top of the simulation. What happens? Why does it happen? You may need to try this a couple of times to get a connected formation with all five particles. What if only four particles are used?
In this model all particles have the same mass. What happens if particles have different masses?
What happens when you change the sensor view?
What happens if you change the force law?
Note, in order to change any NetLogo simulation, you must have the source code (i.e., “formation_hooke.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.
This simulation allows the user to create new particles and kill existing ones.
Killing a particle is accomplished via a call to the NetLogo procedure “die.”
To create a particle, the “hatch” command is used - this clones an existing particle and moves it away from the original. Also, the new particle has the “pen down,” which means that you will see the path that the particle takes. If the graphics pane becomes too busy, click on the CLEAR button.
This is our first physics-based swarm simulation. However, it builds naturally on our early two-particle physics models from Chapter 2.
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 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.
; William M. Spears September 2011
; Triangular Lattice using Hooke's Law Tutorial Code
; For research and educational use only
breed [particles particle] ; Introduce the "particle" breed
globals [total_lmx total_lmy total_angular_mom k FR D
center_of_mass_x center_of_mass_y FMAX DeltaT]
turtles-own [hood deltax deltay r F Fx Fy v vx vy dvx dvy mass
lmx lmy theta lever_arm_x lever_arm_y lever_arm_r angular_mom]
to setup
clear-all ; Clear everything
; Create and initialize particles
create-particles Number_of_Particles [setup-particles]
update-info
; Computes center of mass and displays location
set center_of_mass_x (sum [xcor * mass] of particles) / (sum [mass] of particles)
set center_of_mass_y (sum [ycor * mass] of particles) / (sum [mass] of particles)
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 particles [ap-particles]
ask turtles [move]
; Computes center of mass and displays location
set center_of_mass_x (sum [xcor * mass] of particles) / (sum [mass] of particles)
set center_of_mass_y (sum [ycor * mass] of particles) / (sum [mass] of particles)
ask patch (round center_of_mass_x) (round center_of_mass_y)
[ask patches in-radius 4 [set pcolor red]]
set total_lmx sum [lmx] of particles ; Total linear momentum, x-component
set total_lmy sum [lmy] of particles ; Total linear momentum, y-component
set total_angular_mom sum [angular_mom] of particles
tick
do-plots
end
to setup-particles ; Set up the particles
setxy (random-normal 0 20.0)
(random-normal 0 20.0) ; Start in a cluster
set heading random 360 ; Everyone has a random heading
set vx 0 set vy 0 set mass 1 ; Start with no motion and mass = 1
set color white set shape "circle" set size 5
set theta 0
end
to ap-particles ; Run artificial physics
set Fx 0 set Fy 0 ; Initialize force components to zero
set vx (1 - FR) * vx ; Slow down according to friction
set vy (1 - FR) * vy
set hood [who] of other particles ; Get the IDs of your neighbors
foreach hood [
set deltax (([xcor] of particle ?) - xcor)
if ((abs deltax) < 1.5 * D) [ ; Speed improvement for the code
set deltay (([ycor] of particle ?) - ycor)
set r sqrt (deltax * deltax + deltay * deltay)
if (r < 1.5 * D) [
set F (k * (r - D)) ; The spring law
if (F > FMAX) [set F FMAX] ; Bounds check on force magnitude
set Fx (Fx + F * (deltax / r)) ; The x-component of force
set Fy (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 v sqrt (vx * vx + vy * vy)
set deltax DeltaT * vx
set deltay DeltaT * vy
if ((deltax != 0) or (deltay != 0))
[set heading (atan deltax deltay)]
end
to move ; Move the turtle
fd sqrt (deltax * deltax + deltay * deltay)
set lmx (mass * vx) ; Linear momentum of the turtle
set lmy (mass * vy)
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 FMAX Force_Maximum
set FR Friction
set DeltaT Time_Step
set D Desired_Separation
end
to do-plots
set-current-plot "Linear and Angular Momenta" ; Select the Momenta plot
set-current-plot-pen "Lmx" ; Select the Lmx pen
plot total_lmx ; Plot the linear momentum, x-component
set-current-plot-pen "Lmy"
plot total_lmy ; Plot the linear momentum, y-component
set-current-plot-pen "Angular"
plot total_angular_mom ; Plot the angular momentum
end
; Kill a particle
to one-dies
if (count (particles with [mass = 1]) > 1) [ ; Don't kill last particle
ask one-of particles with [mass = 1] [die] ; Ask one particle to die
clear-drawing ; A little cleanup is required
]
end
; Create a new particle
to one-is-born
if (count (particles with [mass = 1]) > 0) [
ask one-of particles with [mass = 1]
[hatch 1 [set deltax 0 set deltay 0 pd ; Clone an existing particle
setxy xcor + (random-normal 0 (D / 2))
ycor + (random-normal 0 (D / 2))]]
]
end