📚 The CoCalc Library - books, templates and other resources
License: OTHER
Self-organized criticality
Code examples from Think Complexity, 2nd edition.
Copyright 2016 Allen Downey, MIT License
Sand pile
Sand.py
contains an implementation of the sand pile model.
Here's a small example starting with two cells ready to topple. n
is the number of rows, m
is the number of columns.
To execute one step, first we find cells that are above the toppling threshold, K
.
Then we use correlate2d
to make a copy of the update kernel around each toppling cell.
Finally, we add the result back into the array:
Any grains that topple off the edge disappear.
Animation
Let's look at a bigger pile, with n=20
. All cells are initialized to level
, which is meant to be substantially bigger than K
.
The run
function invokes step
until no more cells topple and returns the number of time steps and the number of affected cells.
Here's what it looks like. Starting with level>K
produces all kinds of interesting patterns.
Now let's look at an animation, starting from this initialized pile.
Each step of the animation drops a single grain at a random location and runs until no more cells topple.
After a while, the pile looks pretty random.
Here's a plot of the number of cells toppled after each step
.
The following figure shows the progression of the pile from ordered to apparently random.
Long tailed distributions
If the sand pile is in a critical state, we expect quantities like the duration of an avalanche, T
, and the number of cells affected, S
, to have long-tailed distributions.
Following Bak, Tang, and Wiesenseld, we start with a 50x50 array and plot the PMFs of S
and T
on a log-log scale.
Now we run the pile for many time steps and keep track of the durations and number of cells affected.
We can use np.transpose
to extract the results as two NumPy arrays.
A large majority of drops have duration 1 and no toppled cells. If we filter them out, we get a clearer picture of the rest of the distribution.
We can use Pmf
from the empiricaldist
module to compute PMFs of T
and S
.
The distributions of T
and S
have many small values and a few very large ones.
Here are the PMFs on linear axes, showing only small values.
To see whether these distributions follow a power law, we plot the PMFs on a log-log scale.
The gray lines have slopes near -1. The distribution of avalanche duration is approximately straight between 1 and 100, but then drops off. The distribution of size follows a power law more closely and over a greater range, but it also seems to drop off for values above a few hundred.
Exercise: Try running the model longer to see if you can get a less noisy plot of the distributions of T
and S
.
Fractals
If the sand pile is in a critical state, we expect to see fractal geometry.
To estimate the fractal dimension, I'll start with a bigger pile and a higher initial level.
The initial state sure looks like a fractal.
Since it contains four different levels (0, 1, 2, and 3), we can extract 4 binary patterns.
Here's what they look like:
Now we can apply a box-counting algorithm to each level.
count_cells
starts with a single cell in the middle, gradually increases the size of the box, and counts the number of cells in each box.
box_count
takes a pile and a level, extracts the cells that have the given level, calls count_cells
, and estimates the fractal dimension.
If plot
is True
, it also generates a graph of cell count versus box size on a log-log scale.
Finally box_count_four
applies the box counting algorithm for each value in the sand pile.
Here are the results:
The lines are reasonably straight, which indicates that we are running the algorithm over a valid range of box sizes. Here are the estimated slopes:
The fractal dimension for levels 0, 1, and 2 seems to be clearly non-integer, which indicates that the image is fractal.
The fractal dimension for value 3 is indistinguishable from 2, but given the results for the other levels, the apparent curvature of the line, and the appearance of the pattern, it seems likely that it is also fractal.
Exercise: Choose a different value of n
and/or the initial level
and run this analysis again. Are the estimated fractal dimensions consistent?
Spectral density
Suppose the sandpile made a little click each time a cell toppled. What would it sound like?
toppled_seq
contains the number of cells that toppled during each time step. We can use Welch's algorithm to estimate its power spectral density.
And here's what it looks like.
The slope of the line is -1.58, which indicates that this spectrum is pink noise with parameter .
Exercise: Choose a different value of nperseg
and run this analysis again. What are the pros and cons of larger segment lengths? Modify the code to run the model longer and see if you can get a less noisy estimate of the spectrum.
Exercises
Exercise: To test whether the distributions of T
and S
are heavy-tailed, we plotted their Pmf
on a log-log scale, which is what Bak, Tang and Wiesenfeld show in their paper. But as we saw in Chapter 4, this visualization can obscure the shape of the distribution. Using the same data, make a plot that shows the CDFs of S
and T
. What can you say about the shape of these distributions? Do they follow a power law? Are they heavy tailed?
You might find it helpful to plot the CDFs on a log-x scale and on a log-log scale.
Exercise: In Section 8.5 we showed that the initial equilibrium of the sand pile model produces fractal patterns. But after we drop a large number of random grains, the patterns look more random.
Starting with the example in Section 8.5, run the sand pile model for a while and then compute fractal dimensions for each of the 4 levels. Is the sand pile model fractal in steady state?
Exercise: Another version of the sand pile model, called the "single source" model, starts from a different initial condition: instead of all cells at the same level, all cells are set to 0 except the center cell, which is set to a very large value.
Write a function that creates a SandPile
object, sets up the single source initial condition, and runs until the pile reaches equilibrium. Does the results appear to be fractal?
You can read more about this version of the sand pile model at http://math.cmu.edu/~wes/sandgallery.html