NetLogo Code for the Simulations
-
There are 4 subroutines which do most of the work:
-
setup: initializes and resets the simulation. Also a button in the GUI.
-
go: the main routine to run the simulation. GUI button starts and stops the sim.
-
recolor: changes the color of the turtles based on their position, to give a 3D effect.
-
propagate: the math that controls the actual wave behavior.
//***************************************************//
to setup
ca
set clock 0 ;; reset the clock
;; setup the size and shape of wave tank
set membrane-edge-x floor (screen-edge-x / 2)
set membrane-edge-y floor (screen-edge-y / 2)
;; set the initial shape, color and position for turtles
set-default-shape turtles "star"
ask patches[set pcolor lime - 2] ;; background color
ask patches with [(abs pxcor <= membrane-edge-x) and
(abs pycor <= membrane-edge-y)]
[ sprout 1
[ set edge? (abs xcor = membrane-edge-x) or
(abs ycor = membrane-edge-y)
if edge? [ set color lime - 3 ]
set driver? (abs (xcor - driver-x) <= driver-size) and
(abs (ycor - driver-y) <= driver-size)
if driver? [ set color orange + 2 ]
set x xcor
set y ycor
set z 0
set velocity 0
recolor ]
]
;; make the connections between neighbor turtles
ask turtles
[ set neighbor-turtles turtles-on neighbors4 ]
project ;; display the graphics
end
//***************************************************//
to go
;; call the propagate subroutine for all turtles except edge and driver
ask turtles with [not driver? and not edge?]
[ propagate ]
;; set position of driver turtles based on sine wave
ask turtles
[ ifelse driver?
[ set z (driver-amplitude * (sin (0.1 * driver-frequency * clock))) ]
[ set z (z + velocity) recolor ]
]
project ;; display the simulation graphically
set clock clock + 1 ;; increment the frame counter for each sim time step
end
//***************************************************//
to recolor
;; set turtle color based on position. keep edge and driver colors constant
if not edge? and not driver?
[ set color scale-color blue z -3 3 ]
end
//***************************************************//
to propagate ;; propagate the wave forces from neighboring turtles
;; compute new velocity based on neighbor forces
set velocity (velocity +
(stiffness * 0.01 * (sum values-from neighbor-turtles [z] - 4 * z)))
;; use friction parameter to reduce energy
;; energy is stored in the system as the sum of all turtles' velocities
set velocity (((1000 - friction) / 1000) * velocity)
end
//***************************************************//