Fireflies+

What the Sliders Are

(this is copied from the Info window of the program)
spatial
This slider determines whether spatial or non-spatial mode
is to be used.  A value of 0 indicates spatial mode, a
value of 1 non-spatial model.  As discussed above, the two
modes differ in the behavior of the fireflies and in the
type of display which appears.  With this slider, you must
click on setup for its new value to take effect.

clock
This slider is only relevant for spatial mode (spatial = 1).
When it is set to 1, the "clock hands" indicating phase
angles and periods of fireflies are displayed.

output?
When this slider is set to 1, information about the
fireflies (number flashing; phase angle, period, and
resting period of each firefly) is display in the Output
Window on every 10th time slice.

howmany
This value of this slider determines the number of fireflies
in the system.  With this slider, you must click on setup
for its new value to take effect.

fl-rad
This slider is relevant only in spatial mode.  It indicates
the "flashing radius" of the fireflies, that is, the
maximum distance from a flashing firefly at which another
firefly is affected.  With this slider, you must click on
the setup button for its new value to take effect.

mv-prob
This slider is also relevant only in spatial mode.  It
indicates the probability with which each firefly moves on
each time slice.  When it is 0, the fireflies never move;
when it is 100, they always move.

flash%
The value of this slider is the percentage of a firefly's
cycle during which it flashes.  If this is set to 10, for
example, each firefly flashes from a phase angle of .95
until a phase angle of .05.

pd-rng
The value of this slider indicates the range of initial
periods of the fireflies.  The average period is 100.  If
this slider is set to 100 (its maximum), the initial periods
of the fireflies range from 50 to 150.  With this slider,
you must click on the setup button for its new value to take
effect.

pa-C
This slider sets the phase angle coupling factor.  When it
is set to 0, there is no phase angle coupling.

pd-C
This slider sets the period coupling factor.  When it is set
to 0, there is no period coupling.

How the Program Works

(you aren't responsible for the details)
The program with comments (text following semicolons).
;;; Each firefly (or turtle if you like) has the following:

turtles-own 
  [phase        ; between 0 and 100 (= 1)
   period       ; average of these is always 100
   on?          ; T or F; is is flashing?
   xlum ylum    ; these 3 are just for helping out
     temp]      ;    in drawing the flashes

;;; Each patch (little square on the screen) has light,
;;; which varies from 0 to 100.

patches-own [light]

;;; These variables are involved in running the
;;; whole system.

globals
  [ll-squared   ; this just helps in calculating light
   count        ; this helps in writing output
   slice        ; this keeps track of how many time
                ;    slices have elapsed
   nillum]      ; this keeps track of how many fireflies
                ;    are on

;;; What to do when starting up a new set of fireflies.

to setup
;; First clear the screen and get rid of all of the
;; old fireflies.
ca
;; Get rid of anything in the Output window.
clear-output
;; If this is non-spatial mode, draw the gray rings.
if spatial = 0
  [draw-phase-background]
;; Make the fireflies (see below).
make-fireflies
;; Set every firefly's light to 0.
setlight 0
;; Set the time slice and number of flashing fireflies
;; to 0.
setslice 0
setnillum 0
end

;;; This just draws the gray circles for the background
;;; in non-spatial mode.

to draw-phase-background
if ((distance 1 1) >= 5) and ((distance 1 1) < 10)
   [setpc 1]
if ((distance 1 1) >= 15) and ((distance 1 1) < 20)
   [setpc 1]
if ((distance 1 1) >= 25) and ((distance 1 1) < 30)
   [setpc 1]
if ((distance 1 1) >= 35) and ((distance 1 1) < 40)
   [setpc 1]
if ((distance 1 1) >= 45) and ((distance 1 1) < 50)
   [setpc 1]
end

;;; Create a new set of fireflies.

to make-fireflies
;; First make "howmany" fireflies (turtles).
crt howmany
;; Set the value of this variable which will be used
;; to figure lumination later on.
setll-squared (fl-rad * fl-rad)
;; Set the color of the fireflies to be sky.
setc sky
;; Set the phases of the fireflies to be random numbers
;; between 0 and 100.
setphase random 100
;; Set the periods of the fireflies to be random
;; numbers around 100 within a range specified by
;; "pd-rng".
setperiod (100 + (random pd-rng) - pd-rng / 2)
;; Set the value of this place-holding variable to
;; be 0.
settemp 0
;; Set the value of on for all fireflies to be false.
seton? false
;; Place the fireflies on the screen.
ifelse spatial = 0
;; In non-spatial mode, put them so that their
;; angle (counterclockwise from 3:00) is their
;; phase angle and their distance from the center
;; is their period.
    [setx (period * .25 * cos (3.6 * phase))
     sety (period * .25 * sin (3.6 * phase))]
;; In spatial mode, put them at random locations on
;; the screen.
    [setxy random 99 random 99]
end

;;; This what to do on every time slice.

to go
;; If "output?" is 1 and the time slice is
;; divisible by 10, print out a message to the
;; Output window (see below).
if (output? = 1) and ((slice mod 10) = 0)
   [print slice
    write-all]
;; Set the number of flashing fireflies to be 0.
setnillum 0
;; Set the light in all patches to be 0.
setlight 0
;; If a firefly's phase angle is within the range
;; specified by flash%, flash it (see below).
;; Otherwise make it (or keep it) dark.
ifelse (phase > (99 - (flash% / 2)))
       or (phase < (flash% / 2 ))
  [flash]
  [darken]
;; If this is spatial mode, illuminate the region
;; around each flashing firefly (see below).
if on? and (spatial = 1)
  [setxlum 0 illuminate]
;; Set the number of flashing fireflies to be the
;; number of ones which are white.
setnillum color-count white
;; Update the phase angles (see below).
update-phase
;; Update the periods (see below).
update-period
;; If we're in spatial mode and a random number we
;; pick is less that the value of "mv-prob", then
;; wiggle the firefly (see below).
if (spatial = 1) and ((random 100) < mv-prob)
  [wiggle]
;; Update the display (see below).
update-display
;; Add one to the current time slice.
setslice slice + 1
end

;;; Update the periods of the fireflies.

to update-period
;; Do the period coupling (see below).
adapt-period
;; Make sure no periods are less than 0.
if period < 0
   [setperiod 1]
end

;;; Update the phase angles of the fireflies.
to update-phase
;; Do the phase-angle coupling (the , means
;; to finish doing this for all fireflies before
;; continuing).
adapt-phase,
;; Move each firefly forward according to its
;; period.
setphase phase + (100 / period)
;; Make sure no phase is greater than 100 or less
;; than 0.
if phase > 100
   [setphase phase - 100]
if phase < 0
   [setphase phase + 100]
end

;;; Move the fireflies one step in some direction.
to wiggle
;; Turn them to the right by some random angle less
;; than 50 degrees.
rt random 50
;; Do the same to the left.
lt random 50
;; Go forward one square.
fd 1
;; If there's already a turtle here, wiggle again.
if turtles-here > 1 [wiggle]
end

;;; Flash a firefly.
to flash
;; Just set on? to be true.
seton? true
end

;;; Darken a firefly.
to darken
;; Just set on? to be false.
seton? false
end

;;; Update the display: in non-spatial mode, move the
;;; fireflies to a new position.  In spatial mode,
;;; color the patches according to how much light is
;;; on them.  In both modes, set the color of the
;;; fireflies.
to update-display
if spatial = 0
   [ifelse period >= 198
       [setx (49 * cos (3.6 * phase))
        sety (49 * sin (3.6 * phase))]
       [setx (period * .25 * cos (3.6 * phase))
        sety (period * .25 * sin (3.6 * phase))]]
ifelse on?
  [setc white]
  [ifelse light > 0
          [setc pink]
          [ifelse period >= 198
             [setc red]
             [ifelse turtles-here > 1
                 [setc 97]
                 [setc sky]]]]
if spatial = 1
   [scale-pc yellow light 0 30
    if clock = 1
       [hatch
          [setc 31
           seth (90 - (3.6 * phase))
           pd fd (period / 10)
           die]]]
end

;;; Do phase-angle coupling.

to adapt-phase
;; Do it only if the firefly is not flashing.
if not on?
;; How we change phase depends on the mode.
   [ifelse
      spatial = 0
;; In non-spatial mode, figure the phase change
;; (see below), then multiply by the number of
;; flashing oscillators and .25.  Then add this
;; to the phase.     
      [setphase phase + (phase-change * nillum * .25)]
;; In spatial mode, adjust the phase only if there
;; is light on the square under the firefly.
;; Just figure phase-change (see below), and add
;; this to the phase.
      [if light > 0
          [setphase phase + phase-change]]]
end

;;; Do period coupling.

to adapt-period
;; Do it only if the firefly is not flashing.
if not on?
;; How we change period depends on the mode.
   [ifelse
      spatial = 0
;; In non-spatial mode, figure the period change
;; (see below), then multiply by the number of
;; flashing oscillators and .25.  Then add this
;; to the period.
      [setperiod period + (pd-change * nillum * .25)]
;; In spatial mode, adjust the period only if there
;; is light on the square under the firefly.
;; Just figure pd-change (see below), and add
;; this to the period.
      [if light > 0
          [setperiod period + pd-change]]]   
end

;;; Calculate the phase change, which depends on the
;;; sine of the phase of the firefly (converted to
;;; degrees).  Multiply by .05 just to scale things.

to phase-change
output
  (pa-C * .05 * (0 - sin (3.6 * phase)))
end

;;; Calculate the period change, which depends on the
;;; sine of the phase of the firefly (converted to
;;; degrees).  Multiply by .05 just to scale things.

to pd-change
output
  (pd-C * .05 * sin (3.6 * phase))
end

;;; Illuminate the region around a flashing firefly:
;;; Set the value of light on each patch appropriately.

to illuminate
if xlum > fl-rad
[stop]
setylum 0
illuminate-y
setxlum xlum + 1
illuminate
end

;;; Used by illuminate.

to illuminate-y
settemp (ylum * ylum) + (xlum * xlum)
if temp > ll-squared
[stop]
if temp = 0 [settemp 1]
tsetlight-at xlum ylum
    (light-at xlum ylum) + (ll-squared / temp)
if xlum > 0
[tsetlight-at (0 - xlum) ylum
    (light-at (0 - xlum) ylum) + (ll-squared / temp)]
if ylum > 0
[tsetlight-at xlum (0 - ylum)
    (light-at xlum (0 - ylum)) + (ll-squared / temp)]
if (ylum > 0 and xlum > 0)
[tsetlight-at (0 - xlum) (0 - ylum)
    (light-at (0 - xlum) (0 - ylum)) + (ll-squared / temp)]
setylum ylum + 1
illuminate-y
end

;;; Write a message about all fireflies to the Output
;;; window.

to write-all
type "|Flashing: | type nillum type "/ print howmany
setcount 0
write-fireflies
end

;;; Used by write-all.

to write-fireflies
if count >= howmany
  [print "| | stop]
write-firefly count
setcount count + 1
write-fireflies
end

;;; Used by write-fireflies

to write-firefly :n
type "| | type :n
type "|: PA=| type round (phase-of :n) type "|, |
type "Pd= type round (period-of :n)
print "| |
end

;;; When the program starts up, clear the screen
;;; and start with no turtles.

to startup
ca
end

[Go to the Rhythm and Cognition Home Page]


Last updated: 3 October 1995
URL: http://www.indiana.edu/~gasser/fireflies.html
Comments: gasser@salsa.indiana.edu
Copyright 1995, The Trustees of Indiana University