Skip to content

Abelian Sandpile

cax.cs.sandpile.cs.Sandpile

Bases: ComplexSystem[Array, Array]

Abelian Sandpile model.

A discrete cellular automaton demonstrating self-organized criticality. The state is a grid of non-negative integers representing chip counts. When a cell reaches the critical threshold, it topples, distributing chips to neighbors. Cascading avalanches of topplings produce power-law distributed events.

Two boundary modes are supported
  • "CIRCULAR": periodic (toroidal) boundaries conserving total mass.
  • "ZERO": dissipative boundaries where sand falling off the edge is lost, which is required for proper self-organized criticality.
Source code in src/cax/cs/sandpile/cs.py
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class Sandpile(ComplexSystem[Array, Array]):
    """Abelian Sandpile model.

    A discrete cellular automaton demonstrating self-organized criticality. The state
    is a grid of non-negative integers representing chip counts. When a cell reaches
    the critical threshold, it topples, distributing chips to neighbors. Cascading
    avalanches of topplings produce power-law distributed events.

    Two boundary modes are supported:
        - "CIRCULAR": periodic (toroidal) boundaries conserving total mass.
        - "ZERO": dissipative boundaries where sand falling off the edge is lost,
            which is required for proper self-organized criticality.
    """

    def __init__(
        self,
        *,
        num_spatial_dims: int = 2,
        threshold: int | None = None,
        padding: Literal["CIRCULAR", "ZERO"] = "CIRCULAR",
    ):
        """Initialize Sandpile.

        Args:
            num_spatial_dims: Number of spatial dimensions (default 2).
            threshold: Critical chip count for toppling. Defaults to
                2 * num_spatial_dims (4 in 2D, 6 in 3D).
            padding: Boundary condition mode. "CIRCULAR" for periodic boundaries,
                "ZERO" for dissipative boundaries (required for SOC).

        """
        self.num_spatial_dims = num_spatial_dims
        self.threshold = threshold if threshold is not None else 2 * num_spatial_dims
        self.perceive = SandpilePerceive(
            num_spatial_dims=num_spatial_dims,
            padding=padding,
        )
        self.update = SandpileUpdate(
            num_spatial_dims=num_spatial_dims,
            threshold=self.threshold,
        )

    @override
    def _step(self, state: Array, input: Array | None = None) -> Array:
        # Add dropped grains before perceiving, so every cell topples against the
        # same post-drop snapshot and chips are conserved away from the boundary
        if input is not None:
            state = state + input
        perception = self.perceive(state)
        next_state = self.update(state, perception)

        return next_state

    @nnx.jit
    @override
    def render(self, state: Array) -> Array:
        """Render state to RGB image.

        Maps chip counts to distinct colors using the classic sandpile palette:
        0 chips → dark blue, 1 chip → cyan, 2 chips → yellow, 3 chips → orange.
        Cells at or above the critical threshold are rendered in red.

        Args:
            state: Array with shape (..., *spatial_dims, 1) containing integer chip
                counts stored as float32.

        Returns:
            RGB image with dtype uint8 and shape (..., *spatial_dims, 3).

        """
        chips = state[..., 0]

        r = jnp.where(
            chips == 0,
            0.1,
            jnp.where(
                chips == 1,
                0.0,
                jnp.where(chips == 2, 0.9, jnp.where(chips == 3, 1.0, 0.8)),
            ),
        )
        g = jnp.where(
            chips == 0,
            0.1,
            jnp.where(
                chips == 1,
                0.7,
                jnp.where(chips == 2, 0.9, jnp.where(chips == 3, 0.5, 0.0)),
            ),
        )
        b = jnp.where(
            chips == 0,
            0.4,
            jnp.where(
                chips == 1,
                0.8,
                jnp.where(chips == 2, 0.1, jnp.where(chips == 3, 0.0, 0.0)),
            ),
        )

        rgb = jnp.stack([r, g, b], axis=-1)
        return clip_and_uint8(rgb)

__init__(*, num_spatial_dims=2, threshold=None, padding='CIRCULAR')

Initialize Sandpile.

Parameters:

Name Type Description Default
num_spatial_dims int

Number of spatial dimensions (default 2).

2
threshold int | None

Critical chip count for toppling. Defaults to 2 * num_spatial_dims (4 in 2D, 6 in 3D).

None
padding Literal['CIRCULAR', 'ZERO']

Boundary condition mode. "CIRCULAR" for periodic boundaries, "ZERO" for dissipative boundaries (required for SOC).

'CIRCULAR'
Source code in src/cax/cs/sandpile/cs.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def __init__(
    self,
    *,
    num_spatial_dims: int = 2,
    threshold: int | None = None,
    padding: Literal["CIRCULAR", "ZERO"] = "CIRCULAR",
):
    """Initialize Sandpile.

    Args:
        num_spatial_dims: Number of spatial dimensions (default 2).
        threshold: Critical chip count for toppling. Defaults to
            2 * num_spatial_dims (4 in 2D, 6 in 3D).
        padding: Boundary condition mode. "CIRCULAR" for periodic boundaries,
            "ZERO" for dissipative boundaries (required for SOC).

    """
    self.num_spatial_dims = num_spatial_dims
    self.threshold = threshold if threshold is not None else 2 * num_spatial_dims
    self.perceive = SandpilePerceive(
        num_spatial_dims=num_spatial_dims,
        padding=padding,
    )
    self.update = SandpileUpdate(
        num_spatial_dims=num_spatial_dims,
        threshold=self.threshold,
    )

render(state)

Render state to RGB image.

Maps chip counts to distinct colors using the classic sandpile palette: 0 chips → dark blue, 1 chip → cyan, 2 chips → yellow, 3 chips → orange. Cells at or above the critical threshold are rendered in red.

Parameters:

Name Type Description Default
state Array

Array with shape (..., *spatial_dims, 1) containing integer chip counts stored as float32.

required

Returns:

Type Description
Array

RGB image with dtype uint8 and shape (..., *spatial_dims, 3).

Source code in src/cax/cs/sandpile/cs.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@nnx.jit
@override
def render(self, state: Array) -> Array:
    """Render state to RGB image.

    Maps chip counts to distinct colors using the classic sandpile palette:
    0 chips → dark blue, 1 chip → cyan, 2 chips → yellow, 3 chips → orange.
    Cells at or above the critical threshold are rendered in red.

    Args:
        state: Array with shape (..., *spatial_dims, 1) containing integer chip
            counts stored as float32.

    Returns:
        RGB image with dtype uint8 and shape (..., *spatial_dims, 3).

    """
    chips = state[..., 0]

    r = jnp.where(
        chips == 0,
        0.1,
        jnp.where(
            chips == 1,
            0.0,
            jnp.where(chips == 2, 0.9, jnp.where(chips == 3, 1.0, 0.8)),
        ),
    )
    g = jnp.where(
        chips == 0,
        0.1,
        jnp.where(
            chips == 1,
            0.7,
            jnp.where(chips == 2, 0.9, jnp.where(chips == 3, 0.5, 0.0)),
        ),
    )
    b = jnp.where(
        chips == 0,
        0.4,
        jnp.where(
            chips == 1,
            0.8,
            jnp.where(chips == 2, 0.1, jnp.where(chips == 3, 0.0, 0.0)),
        ),
    )

    rgb = jnp.stack([r, g, b], axis=-1)
    return clip_and_uint8(rgb)

__call__(state, input=None, *, num_steps=1, input_in_axis=None, return_states=False)

Step the system for multiple time steps.

This method wraps _step inside a JAX scan for efficiency and JIT-compiles the loop. If input is time-varying, set input_in_axis to the axis containing the time dimension so that each step receives the corresponding slice of input.

Under return_states=True, the per-step states are also returned as the scan's stacked outputs, mirroring the (carry, ys) convention of jax.lax.scan. The trajectory holds the state after each step, stacked along a new leading axis of size num_steps — its first element is the state after one step, its last equals the final state, and the initial state is not included.

When remat is enabled, the scan body is wrapped with nnx.remat to reduce memory usage during backpropagation at the cost of recomputing intermediates.

Note that num_steps, input_in_axis, and return_states are static: each distinct combination compiles once, so sweeps over horizons should batch their step counts.

Parameters:

Name Type Description Default
state State

Current state.

required
input Input | None

Optional input.

None
num_steps int

Number of steps.

1
input_in_axis int | None

Axis for input if provided for each step.

None
return_states bool

Whether to also return the stacked per-step states.

False

Returns:

Type Description
State | tuple[State, State]

Final state after num_steps applications of _step, or a (final_state, states) tuple under return_states=True, where states stacks the per-step states along a new leading axis of size num_steps.

Source code in src/cax/core/cs.py
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
@nnx.jit(static_argnames=("num_steps", "input_in_axis", "return_states"))
def __call__(
    self,
    state: State,
    input: Input | None = None,
    *,
    num_steps: int = 1,
    input_in_axis: int | None = None,
    return_states: bool = False,
) -> State | tuple[State, State]:
    """Step the system for multiple time steps.

    This method wraps `_step` inside a JAX scan for efficiency and JIT-compiles the
    loop. If `input` is time-varying, set `input_in_axis` to the axis containing the
    time dimension so that each step receives the corresponding slice of input.

    Under `return_states=True`, the per-step states are also returned as the scan's
    stacked outputs, mirroring the `(carry, ys)` convention of `jax.lax.scan`. The
    trajectory holds the state *after* each step, stacked along a new leading axis
    of size `num_steps` — its first element is the state after one step, its last
    equals the final state, and the initial state is not included.

    When `remat` is enabled, the scan body is wrapped with `nnx.remat` to reduce
    memory usage during backpropagation at the cost of recomputing intermediates.

    Note that `num_steps`, `input_in_axis`, and `return_states` are static: each
    distinct combination compiles once, so sweeps over horizons should batch their
    step counts.

    Args:
        state: Current state.
        input: Optional input.
        num_steps: Number of steps.
        input_in_axis: Axis for input if provided for each step.
        return_states: Whether to also return the stacked per-step states.

    Returns:
        Final state after `num_steps` applications of `_step`, or a
            `(final_state, states)` tuple under `return_states=True`, where `states`
            stacks the per-step states along a new leading axis of size `num_steps`.

    """

    def step_fn(
        cs: ComplexSystem[State, Input], state: State, input: Input | None
    ) -> tuple[State, State | None]:
        next_state = cs._step(state, input)
        return next_state, (next_state if return_states else None)

    if self.remat:
        step_fn = nnx.remat(step_fn)

    state, states = nnx.scan(
        step_fn,
        in_axes=(nnx.StateAxes({...: nnx.Carry}), nnx.Carry, input_in_axis),
        out_axes=(nnx.Carry, 0),
        length=num_steps,
    )(self, state, input)

    return (state, states) if return_states else state