Sandpile preview image

5 collaborators

Uri_dolphin3 Uri Wilensky (Author)
Default-person Robert Tinker (Author)
Default-person David Weintrop (Author)
79107734_n00-1 Seth Tisue (Author)
Default-person Bryan Head (Author)



Tagged by Modeling Commons System over 12 years ago

chemistry and physics 

Tagged by Reuven M. Lerner about 11 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 693 times • Downloaded 61 times • Run 0 times
Download the 'Sandpile' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)

Info tab cannot be displayed because of an encoding error

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [
  ;; By always keeping track of how much sand is on the table, we can compute the 
  ;; average number of grains per patch instantly, without having to count.
  ;; We don't want the average monitor to updating wildly, so we only have it 
  ;; update every tick.
  ;; Keep track of avalanche sizes so we can histogram them
  ;; Size of the most recent run
  ;; Keep track of avalanche lifetimes so we can histogram them
  ;; Lifetime of the most recent run
  ;; The patch the mouse hovers over while exploring
  ;; These colors define how the patches look normally, after being fired, and in 
  ;; explore mode.

patches-own [
  ;; how many grains of sand are on this patch
  ;; A list of stored n so that we can easily pop back to a previous state. See
  ;; the NETLOGO FEATURES section of the Info tab for a description of how stacks
  ;; work
  ;; Determines what color to scale when coloring the patch. 

;; The input task says what each patch should do at setup time
;; to compute its initial value for n.  (See the Tasks section
;; of the Programming Guide for information on tasks.)

to setup [setup-task]
  set default-color blue
  set fired-color red
  set selected-color green
  set selected-patch nobody
  ask patches [
    set n runresult setup-task
    set n-stack []
    set base-color default-color
  let ignore stabilize false
  ask patches [ recolor ]
  set total sum [ n ] of patches
  ;; set this to the empty list so we can add items to it later
  set sizes []
  set lifetimes []

;; For example, "setup-uniform 2" gives every patch a task which reports 2.

to setup-uniform [initial]
  setup task [ initial ]

;; Every patch uses a task which reports a random value.

to setup-random
  setup task [ random 4 ]

;; patch procedure; the colors are like a stoplight

to recolor
  set pcolor scale-color base-color n 0 4

to go
  let drop drop-patch
  if drop != nobody [
    ask drop [
      update-n 1
    let results stabilize animate-avalanches?
    let avalanche-patches first results
    let lifetime last results
    ;; compute the size of the avalanche and throw it on the end of the sizes list
    if any? avalanche-patches [
      set sizes lput (count avalanche-patches) sizes
      set lifetimes lput lifetime lifetimes
    ;; Display the avalanche and guarantee that the border of the avalanche is updated
    ask avalanche-patches [ recolor ask neighbors4 [ recolor ] ]
    ;; Erase the avalanche
    ask avalanche-patches [ set base-color default-color recolor ]
    ;; Updates the average monitor
    set total-on-tick total

to explore
  ifelse mouse-inside? [
    let p patch mouse-xcor mouse-ycor
    set selected-patch p
    ask patches [ push-n ]
    ask selected-patch [ update-n 1 ]
    let results stabilize false
    ask patches [ pop-n ]
    ask patches [ set base-color default-color recolor ]
    let avalanche-patches first results
    ask avalanche-patches [ set base-color selected-color recolor ]
  ] [
    if selected-patch != nobody [
      set selected-patch nobody
      ask patches [ set base-color default-color recolor ]

;; Stabilizes the sandpile. Reports which sites fired and how many iterations it took to
;; stabilize.

to-report stabilize [animate?]
  let active-patches patches with [ n > 3 ]
  ;; The number iterations the avalanche has gone for. Use to calculate lifetimes.
  let iters 0

  ;; we want to count how many patches became overloaded at some point
  ;; during the avalanche, and also flash those patches. so as we go, we'll
  ;; keep adding more patches to to this initially empty set.
  let avalanche-patches no-patches
  while [ any? active-patches ] [
    let overloaded-patches active-patches with [ n > 3 ]
    if any? overloaded-patches [
      set iters iters + 1
    ask overloaded-patches [
      set base-color fired-color
      ;; subtract 4 from this patch
      update-n -4
      if animate? [ recolor ]
      ;; edge patches have less than four neighbors, so some sand may fall off the edge
      ask neighbors4 [
        update-n 1
        if animate? [ recolor ]
    if animate? [ display ]
    ;; add the current round of overloaded patches to our record of the avalanche
    ;; the patch-set primitive combines agentsets, removing duplicates
    set avalanche-patches (patch-set avalanche-patches overloaded-patches)
    ;; find the set of patches which *might* be overloaded, so we will check
    ;; them the next time through the loop
    set active-patches patch-set [ neighbors4 ] of overloaded-patches
  report (list avalanche-patches iters)

;; patch procedure. input might be positive or negative, to add or subtract sand

to update-n [ how-much ]
  set n n + how-much
  set total total + how-much

to-report drop-patch
  if drop-location = "center" [ report patch 0 0 ]
  if drop-location = "random" [ report one-of patches ]
  if drop-location = "mouse-click" and mouse-down? [
    every 0.3 [ report patch mouse-xcor mouse-ycor ]
  report nobody

;; Save the patches state

to push-n ;; patch procedure
  set n-stack fput n n-stack

;; restore the patches state

to pop-n ;; patch procedure
  ; need to go through update-n to keep total statistic correct
  update-n ((first n-stack) - n)
  set n-stack but-last n-stack

; Public Domain:
; To the extent possible under law, Uri Wilensky has waived all
; copyright and related or neighboring rights to this model.

There are 4 versions of this model.

Uploaded by When Description Download
Uri Wilensky about 11 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky over 11 years ago Updated version tag Download this version
Uri Wilensky almost 12 years ago Updated to version from NetLogo 5.0.3 distribution Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 5.0 Download this version

Attached files

File Type Description Last updated
Sandpile.png preview Preview for 'Sandpile' over 11 years ago, by Uri Wilensky Download
Sandpile.png preview Preview for 'Sandpile' about 11 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.