📚 The CoCalc Library - books, templates and other resources
License: OTHER
Physical modeling
Code examples from Think Complexity, 2nd edition.
Copyright 2016 Allen Downey, MIT License
Diffusion
Before we get to a Reaction-Diffusion model, we'll start with simple diffusion.
The kernel computes the difference between each cell and the sum of its neighbors.
At each time step, we compute this difference, multiply by a constant, and add it back in to the array.
Here's a simple example starting with an "island" of material in the middle.
And here's how it behaves over time: the "material" spreads out until the level is equal on the whole array.
Reaction-Diffusion
Now we'll add a second material and let them interact.
The following function helps with setting up the initial conditions.
For the RD model, we have two arrays, one for each chemical.
Following Sims, I'm using a kernel that includes the diagonal elements. They have lower weights because they are farther from the center cell.
The step
function computes these functions:
where is the Laplace operator the kernel is intended to approximate.
The viewer for the CA shows both arrays with some transparency, so we can see where one, the other, or both, levels are high.
Unlike previous CAs, the state of each cell is meant to represent a continuous quantity, so it is appropriate to interpolate.
Note that draw
has to make copies of the arrays because step
updates the arrays in place.
Here's an example using params3
, which yields blue dots that seem to undergo mitosis.
Here's a random starting condition with lots of A, a sprinkling of B everywhere, and an island of B in the middle.
I'll use the following function to generate figures using different parameters.
The following parameters yield pink stripes and spots on a blue background:
The following parameters yield blue stripes on a pink background.
The following parameters yield blue dots on a pink background
Percolation
In the percolation model, each cell is porous with probability p
. We start with a row of wet cells at the time. During each time step, a cell becomes wet if it is porous and at least one neighbor is wet (using a 4-cell neighborhood). For each value of p
we compute the probability that water reaches the bottom row.
Porous cells have state 1
and wet cells have state 5
, so if a cell has a wet neighbor, the sum of the neighbors will by 5
or more.
Here an example that shows the first three time steps.
test_perc
runs a percolation model and returns True
if water reaches the bottom row and False
otherwise.
Run a small example.
And here's the animation
For a given q
we can estimate the probability of a percolating cluster by running several random configurations.
At q=0.55
the probability is low.
At q=0.6
, the probability is close to 50%, which suggests that the critical value is nearby.
At p=0.65
the probability is high.
We can search for the critical value by random walk: if there's a percolating cluster, we decrease q
; otherwise we increase it.
The path should go to the critical point and wander around it.
Let's see whether the critical value depends on the size of the grid.
With n=50
, the random walk wanders around 0.59.
Larger values of n
don't seem to change the critical value.
Fractals
Near the critical point, the cluster of wet cells forms a fractal. We can see that visually in these examples:
To measure fractal dimension, let's start with 1D CAs.
Here's one rule that seems clearly 1D, one that is clearly 2D, and one that we can't obviously classify.
The following function creates a 1D CA and steps through time, counting the number of on cells after each time step.
This function plots the results, comparing the rate of cell growth to size
and size**2
.
And it uses linregress to estimate the slope of the line on a log-log scale.
The linear rule has dimension close to 1.
The triangular rule has dimension close to 2.
And the Sierpinski triangle has fractal dimension approximately 1.57
Mathematically, the fractal dimension is supposed to be:
Fractals in percolation models
We can measure the fractal dimension of a percolation model by measuring how the number of wet cells scales as we increase the size of a bounding box.
The following function takes a percolation model that has run to completion. It computes bounding boxes with sizes from 10 up to n-1
, positioned in the center of the array.
For each bounding box it counts the number of wet cells.
If we plot the number of cells versus the size of the box on a log-log scale, the slope is the fractal dimension.
When q
is near the critical point, the fractal dimension of the wet cells is usually between 1.8 and 2.0, but it varies from one run to the next.
Exercise: In Chapter 7 we showed that the Rule 18 CA produces a fractal. Can you find other rules that produce fractals? For each one, estimate its fractal dimension.
Note: the Cell1D
object in Cell1D.py
does not wrap around from the left edge to the right, which creates some artifacts at the boundaries. You might want to use Wrap1D
, which is a child class of Cell1D
that wraps around. It is also defined in Cell1D.py
.
Exercise: In 1990 Bak, Chen and Tang proposed a cellular automaton that is an abstract model of a forest fire. Each cell is in one of three states: empty, occupied by a tree, or on fire.
The rules of the CA are:
An empty cell becomes occupied with probability .
A cell with a tree burns if any of its neighbors is on fire.
A cell with a tree spontaneously burns, with probability , even if none of its neighbors is on fire.
A cell with a burning tree becomes an empty cell in the next time step.
Write a program that implements it. You might want to inherit from Cell2D
. Typical values for the parameters are and , but you might want to experiment with other values.
Starting from a random initial condition, run the CA until it reaches a steady state where the number of trees no longer increases or decreases consistently.
In steady state, is the geometry of the forest fractal? What is its fractal dimension?