Skip to content

Diffusion-limited aggregation

Diffusion-limited aggregation on a periodic grid.

  • Class: DiffusionLimitedAggregationSystem
  • Import: from skcomplex.systems.physics.statistical_mechanics import DiffusionLimitedAggregationSystem
  • Field: physics/statistical_mechanics
  • Topics: agents, aggregation, fractal, random walk
  • References:
    • Witten and Sander 1981
    • Charbonneau 2017

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.physics.statistical_mechanics import DiffusionLimitedAggregationSystem

trajectory = DiffusionLimitedAggregationSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
n_walkers 400
size 41
random_state 0
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time discrete
Space lattice
Laws update
Randomness deterministic
Domains boolean, integer
Structure variable membership

Class

The source of the class shows how it builds its components from its model parameters.

class DiffusionLimitedAggregationSystem(BaseSystem):
    """Diffusion-limited aggregation on a periodic grid.

    Random walkers stick to a seed at the center.

    Walkers that touch the aggregate join it, so the tips of the aggregate catch most walkers and it grows into a
    branched fractal of dimension about 1.7. The system has the agent group `walkers`.

    Args:
        n_walkers:
            The number of walkers, the seed included.

        size:
            The side of the grid.

        random_state:
            The seed or generator of the initial cells of the walkers.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for the one the system chooses.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Examples:
        >>> from skcomplex.systems.physics.statistical_mechanics import DiffusionLimitedAggregationSystem
        >>> DiffusionLimitedAggregationSystem(10, size=11).prepare().system_.elements[0].size
        10
    """

    field = 'physics/statistical_mechanics'
    topics = ('agents', 'aggregation', 'fractal', 'random walk')
    references = ('Witten and Sander 1981', 'Charbonneau 2017')
    until = 200
    dt = 1

    def __init__(
        self,
        n_walkers: int = 400,
        *,
        size: int = 41,
        random_state: int | np.random.Generator | None = 0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.n_walkers = n_walkers
        self.size = size
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set `system_` to the walkers at random cells around a seed at the center.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        rng = np.random.default_rng(self.random_state)
        middle = self.size // 2
        cells = rng.integers(0, self.size, (self.n_walkers, 2))
        cells[0] = middle
        stuck = np.zeros(self.n_walkers)
        stuck[0] = 1.0
        walkers = WalkerElement(
            'walkers',
            stuck=stuck,
            x=cells[:, 0],
            y=cells[:, 1],
            size=self.n_walkers,
            space=LatticeSpace((self.size, self.size), boundary='periodic', metric='manhattan'),
        )
        self.system_ = System(
            self.label,
            elements=[walkers],
            interactions=[StickingWalkInteraction('walk', target='walkers')],
        )

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

Interactions:

  • StickingWalkInteraction, from Interaction
class WalkerElement(Element):
    """Random walkers of diffusion-limited aggregation at the cells `(x, y)`.

    A `StickingWalkInteraction` moves the walkers and sticks them to the aggregate.

    Args:
        label:
            The label of the agent or group.

        stuck:
            Whether the walker belongs to the aggregate.

        x:
            The first coordinate of the cell of the walker.

        y:
            The second coordinate of the cell of the walker.

        size:
            The number of walkers, or `None` for one.

        space:
            The grid of the walkers, or `None` for none.

    Examples:
        >>> from skcomplex.systems.physics.statistical_mechanics import WalkerElement
        >>> WalkerElement('walker').stuck
        0.0
    """

    visual = VisualSpec('agents', {'color_by': 'stuck', 'color_range': (0.0, 1.0)})

    def __init__(
        self,
        label: str,
        *,
        stuck: Annotated[object, FiniteEnumeratedSet([0, 1])] = 0.0,
        x: object = 0,
        y: object = 0,
        size: int | None = None,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, size=size, space=space)
        self.stuck = stuck
        self.x = x
        self.y = y

class StickingWalkInteraction(Interaction):
    """Random walk of walkers that stick when they reach the aggregate.

    Every free walker joins the aggregate when a stuck walker is on a neighbouring cell, and otherwise steps to a
    random neighbouring cell. It is a particle of soot that joins a cluster, a metal ion that deposits on an electrode,
    or a bacterium that settles on a colony.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the walkers.

    Examples:
        >>> from skcomplex.systems.physics.statistical_mechanics import StickingWalkInteraction
        >>> StickingWalkInteraction('walk', target='walkers').target
        'walkers'
    """

    law_classes = (UpdateLaw,)

    def __init__(self, label: str, *, target: Role) -> None:
        super().__init__(label)
        self.target = target

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the walk of the walkers.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        self.laws_ = [UpdateLaw(context.target, self.walk)]

    def walk(self, agent: WalkerView, system: SystemView) -> None:
        """Stick next to the aggregate, or step to a random neighbouring cell.

        Args:
            agent:
                The walker.

            system:
                The system.
        """
        if agent.stuck:
            return
        if any(isinstance(other, WalkerView) and other.stuck for other in agent.get_neighbors()):
            agent.stuck = 1.0
            return
        cells = agent.get_neighbor_cells()
        agent.move_to(cells[int(system.rng.integers(len(cells)))])