This page was automatically generated by NetLogo 5.0.5.

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 Oracle's Java site.

In order for this to work, this file, your model file (Mandala2.nlogo), and the files NetLogoLite.jar and NetLogoLite.jar.pack.gz must all be in the same directory. (You can copy NetLogoLite.jar and NetLogoLite.jar.pack.gz from the directory where you installed NetLogo.)

On some systems, you can test the applet locally on your computer before uploading it to a web server. It doesn't work on all systems, though, so if it doesn't work from your hard drive, please try uploading it to a web server.

You don't need to include everything in this file in your page. If you want, you can just take the HTML code beginning with <applet> and ending with </applet>, and paste it into any HTML file you want. It's even OK to put multiple <applet> tags on a single page.

If the NetLogoLite files and your model are in different directories, you must modify the archive= and value= lines in the HTML code to point to their actual locations. (For example, if you have multiple applets in different directories on the same web server, you may want to put a single copy of the NetLogoLite files in one central place and change the archive= lines of all the HTML files to point to that one central copy. This will save disk space for you and download time for your users.)

powered by NetLogo

view/download model file: Mandala2.nlogo


This model is from Chapter Eight of the book “Introduction to Agent-Based Modeling: Modeling Natural, Social and Engineered Complex Systems with NetLogo”, by Uri Wilensky & William Rand.

Wilensky, U & Rand, W. (2015). Introduction to Agent-Based Modeling: Modeling Natural, Social and Engineered Complex Systems with NetLogo. Cambridge, Ma. MIT Press.


The Bak–Tang–Wiesenfeld sandpile model demonstrates the concept of “self-organized criticality”. It further demonstrates that complexity can emerge from simple rules and that a system can arrive at a critical state spontaneously rather than through the fine tuning of precise parameters.


Imagine a table with sand on it. The surface of the table is a grid of squares. Each square can comfortably hold up to three grains of sand.

Now drop grains of sand on the table, one at a time. When a square reaches the overload threshold of four or more grains, all of the grains (not just the extras) are redistributed to the four neighboring squares. The neighbors may in turn become overloaded, triggering an “avalanche” of further redistributions.

Sand grains can fall off the edge of the table, helping ensure the avalanche eventually ends.

Real sand grains, of course, don’t behave quite like this. You might prefer to imagine that each square is a bureaucrat’s desk, with folders of work piling up. When a bureaucrat’s desk fills up, she clears her desk by passing the folders to her neighbors.


Press one of the SETUP buttons to clear and refill the table. You can start with random sand, or two grains per square, or an empty table.

The color scheme in the view is as follows: 0 grains = black, 1 grain = green, 2 grains = yellow, 3 grains = red. If the DISPLAY-AVALANCHES? switch is on, overloaded patches are white.

Press GO to start dropping sand. You can choose where to drop with DROP-LOCATION.

When DISPLAY-AVALANCHES? is on, you can watch each avalanche happening, and then when the avalanche is done, the areas touched by the avalanche flash white.

Push the speed slider to the right to get results faster.


The white flashes help you distinguish successive avalanches. They also give you an idea of how big each avalanche was.

Most avalanches are small. Occasionally a much larger one happens. How is it possible that adding one grain of sand at a time can cause so many squares to be affected?

Can you predict when a big avalanche is about to happen? What do you look for?

Leaving DISPLAY-AVALANCHES? on lets you watch the pattern each avalanche makes. How would you describe the patterns you see?

Observe the AVERAGE GRAIN COUNT plot. What happens to the average height of sand over time?

Observe the AVALANCHE SIZES plot. This histogram is on a log-log scale, which means both axes are logarithmic. What is the shape of the plot for a long run? You can use the CLEAR SIZE DATA button to throw away size data collected before the system reaches equilibrium.


Try all the different combinations of initial setups and drop locations. How does what you see near the beginning of a run differ? After many ticks have passed, is the behavior still different?

Use an empty initial setup and drop sand grains in the center. This makes the system deterministic. What kind of patterns do you see? Is the sand pile symmetrical and if so, what type of symmetry is it displaying? Why does this happen? Each cell only knows about its neighbors, so how do opposite ends of the pile produce the same pattern if they can’t see each other? What shape is the pile, and why is this? If each cell only adds sand to the cell above, below, left and right of it, shouldn’t the resulting pile be cross-shaped too?


Add code that lets you use the mouse to choose where to drop the next grain yourself. Can you find places where adding one grain will result in an avalanche? If you have a symmetrical pile, then add a few strategic random grains of sand, then continue adding sand to the center of the pile — what happens to the pattern?

Try a larger threshold than 4.

Try including diagonal neighbors in the redistribution, too.

Try redistributing sand to neighbors randomly, rather than always one per neighbor.

This model exhibits characteristics commonly observed in complex natural systems, such as self-organized criticality, fractal geometry, 1/f noise, and power laws. These concepts are explained in more detail in Per Bak’s book (see reference below). Add code to the model to measure these characteristics.


In the world settings, wrapping at the world edges is turned off. Therefore the neighbors4 primitive sometimes returns only two or three patches.

In order for the model to run fast, we need to avoid doing any operations that require iterating over all the patches. Avoiding that means keeping track of the set of patches currently involved in an avalanche. The key line of code is:

set active-patches patch-set [neighbors4] of overloaded-patches

The same setup procedure is used to create a uniform initial setup or a random one. The difference is what task we pass it for each pass to run. See the Tasks section of the Programming Guide in the User Manual for more information on Tasks.


CREDITS AND REFERENCES–Tang–Wiesenfeld_sandpile

Bak, P. 1996. How nature works: the science of self-organized criticality. Copernicus, (Springer).

Bak, P., Tang, C., & Wiesenfeld, K. 1987. Self-organized criticality: An explanation of the 1/f noise. Physical Review Letters, 59(4), 381.

The bureaucrats-and-folders metaphor is due to Peter Grassberger.


globals [

;; 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 
  crt N [
    set color blue
    set size 0
    set heading (who / N * 360)
  ask patches [set pcolor 0]

to go
  ask turtles [
    fd 1.5
    let change ([pcolor] of patch-here)
    ifelse chaos? [
      set change (change / 5)
      set heading (heading + (change * random 2) - (change * random 2) + (change * random 2) - (change * random 2) + (change * random 2) - (change * random 2) + (change * random 2) - (change * random 2) + (change * random 2) - (change * random 2))
      set heading (heading + change)
  ask patches [
    let x (count turtles-here) * softness
    set pcolor (pcolor + x)

;; patch procedure. input might be positive or negative, to add or subtract sand
to update-n [how-much]
  set n n + how-much

;; black green yellow red sky orange brown cyan