Skip to content

Diffusion automaton

A cellular automaton of diffusion, c' = c + r Σ (c_neighbour - c).

  • Class: DiffusionAutomatonSystem
  • Import: from skcomplex.systems.computer_science.automata import DiffusionAutomatonSystem
  • Field: computer_science/automata
  • Topics: lattice, diffusion, conservation
  • References:
    • Downey 2018

Use this system

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

from skcomplex.systems.computer_science.automata import DiffusionAutomatonSystem

trajectory = DiffusionAutomatonSystem().prepare().simulate()

Parameters

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

Parameter Default
shape (50, 50)
r 0.1
width 10
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 unknown
Domains real
Structure fixed

Class

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

class DiffusionAutomatonSystem(BaseSystem):
    """A cellular automaton of diffusion, `c' = c + r Σ (c_neighbour - c)`.

    It starts from a square of ones. The total content is conserved on the torus, and the square spreads into a Gaussian
    bump. The system has the contents `cells.level`.

    Args:
        shape:
            The number of rows and columns of the torus.

        r:
            The share of the difference with a neighbour exchanged in a step.

        width:
            The side of the central square of cells that start with content 1.

        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.computer_science.automata import DiffusionAutomatonSystem
        >>> float(DiffusionAutomatonSystem((20, 20), width=4).prepare().system_.elements[0].level.sum())
        16.0
    """

    field = 'computer_science/automata'
    topics = ('lattice', 'diffusion', 'conservation')
    references = ('Downey 2018',)
    until = 100
    dt = 1

    def __init__(
        self,
        shape: Sequence[int] = (50, 50),
        *,
        r: float = 0.1,
        width: int = 10,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.shape = shape
        self.r = r
        self.width = width

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the cells, a central square of ones, and their diffusion."""
        super()._prepare(context)
        level = np.zeros(tuple(self.shape))
        rows, columns = (self.shape[0] - self.width) // 2, (self.shape[1] - self.width) // 2
        level[rows : rows + self.width, columns : columns + self.width] = 1.0
        self.system_ = System(
            self.label,
            elements=[DiffusionCellElement('cells', level=level.ravel(), space=LatticeSpace(tuple(self.shape)))],
            interactions=[DiscreteDiffusionInteraction('diffusion', target='cells', r=self.r)],
        )

Components

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

Elements:

  • DiffusionCellElement, from Element

Interactions:

  • DiscreteDiffusionInteraction, from Interaction
class DiffusionCellElement(Element):
    """Cell of a lattice with a nonnegative content.

    A `DiscreteDiffusionInteraction` exchanges a share of the difference of contents with every neighbour. With `space`,
    the element holds one cell on every site.

    Args:
        label:
            The label of the cells.

        level:
            The initial content, or one content per cell.

        space:
            The space with one cell on every site, such as a lattice, or `None` for one cell.

    Examples:
        >>> from skcomplex.systems.computer_science.automata import DiffusionCellElement
        >>> DiffusionCellElement('cell', level=2.0).level
        2.0
    """

    def __init__(
        self,
        label: str,
        *,
        level: Annotated[float | Array, RealSet.closed_open(0, oo)] = 0.0,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space)
        self.level = level

class DiscreteDiffusionInteraction(Interaction):
    """Discrete diffusion of the contents of cells, `c' = c + r Σ (c_neighbour - c)`.

    Every cell exchanges the share `r` of the difference of contents with each of its four nearest neighbours, so the
    total content is conserved on a torus.

    Args:
        label:
            The label of the interaction.

        target:
            The cells whose state `level` diffuses.

        r:
            The share of the difference with a neighbour exchanged in a step, below `1 / (2 d)` in `d` dimensions.

    Examples:
        >>> from skcomplex.systems.computer_science.automata import DiscreteDiffusionInteraction
        >>> DiscreteDiffusionInteraction('diffusion', target='cells', r=0.2).r
        0.2
    """

    def __init__(self, label: str, *, target: Role = 'cells', r: float = 0.1) -> None:
        super().__init__(label)
        self.target = target
        self.r = r

    def _get_function(self) -> Callable[..., Array]:
        """Return the content after the exchange of a share of the difference with every neighbour."""
        r = self.r

        def diffuse(level: Array, *around: Array, **kwargs: object) -> Array:
            return level + r * (sum(around) - len(around) * level)

        return diffuse

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the exchange of contents as the law.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        level = context.target.level
        self.laws_: list[BaseLaw] = [
            UpdateLaw(level, self._get_function(), neighborhood=VON_NEUMANN, label=self.label),
        ]