Skip to content

HPP lattice gas

The HPP lattice gas on a periodic square lattice.

  • Class: HPPLatticeGasSystem
  • Import: from skcomplex.systems.physics.fluids import HPPLatticeGasSystem
  • Field: physics/fluids
  • Topics: lattice, lattice gas, deterministic, conservation, reversible
  • References:
    • Hardy, de Pazzis, and Pomeau 1976
    • Lindgren 2024

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.fluids import HPPLatticeGasSystem

trajectory = HPPLatticeGasSystem().prepare().simulate()

Parameters

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

Parameter Default
shape (64, 64)
density 0.2
bump 0.6
radius None
full False
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 real
Structure fixed

Class

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

class HPPLatticeGasSystem(BaseSystem):
    """The HPP lattice gas on a periodic square lattice.

    Its particles move along four directions and collide.

    Every site holds up to four particles, one per direction. Every step the particles first collide, where a site with
    exactly two particles of opposite directions turns them by a quarter turn and every other site keeps its particles,
    and then every particle moves one site along its direction. The rules are deterministic and reversible, and they
    conserve the number of particles and the momentum, and also the momentum along every row and every column, a
    spurious invariant that keeps the HPP gas from isotropic hydrodynamics. Every channel is occupied at random with
    probability `density`, or `bump` inside a disc at the centre. With `full`, as in Problem 4.3 of Lindgren, the
    particles start only in full cells, which hold all four directions, placed at random with probability `density`. The
    system has the channels `gas.n0` to `gas.n3` and the particle numbers `gas.density`.

    Args:
        shape:
            The number of rows and columns.

        density:
            The occupation probability of a channel outside the disc, or of a full cell with `full`.

        bump:
            The occupation probability of a channel inside the disc, unused with `full`.

        radius:
            The radius of the disc in lattice spacings, or `None` for a sixth of the smaller side.

        full:
            Whether the particles start in full cells only.

        random_state:
            The seed or generator of the initial occupations.

        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.fluids import HPPLatticeGasSystem
        >>> trajectory = HPPLatticeGasSystem((8, 8)).prepare().simulate(until=2)
        >>> trajectory.get_values('gas.density').shape
        (3, 64)
    """

    field = 'physics/fluids'
    topics = ('lattice', 'lattice gas', 'deterministic', 'conservation', 'reversible')
    references = ('Hardy, de Pazzis, and Pomeau 1976', 'Lindgren 2024')
    until = 200
    dt = 1

    def __init__(
        self,
        shape: Sequence[int] = (64, 64),
        *,
        density: float = 0.2,
        bump: float = 0.6,
        radius: float | None = None,
        full: bool = False,
        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.shape = shape
        self.density = density
        self.bump = bump
        self.radius = radius
        self.full = full
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the gas, built from its model parameters.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        space = LatticeSpace(tuple(self.shape), boundary='periodic').prepare()
        rng = np.random.default_rng(self.random_state)
        if self.full:
            occupied = np.repeat((rng.random((space.size_, 1)) < self.density).astype(float), SQUARE_DIRECTIONS, axis=1)
        else:
            rows, columns = np.indices(tuple(self.shape))
            positions = np.column_stack([rows.ravel(), columns.ravel()]).astype(float)
            size = min(self.shape) / 6 if self.radius is None else self.radius
            inside = np.linalg.norm(positions - positions.mean(axis=0), axis=1) < size
            probability = np.where(inside, self.bump, self.density)[:, None]
            occupied = (rng.random((space.size_, SQUARE_DIRECTIONS)) < probability).astype(float)
        channels = {name: occupied[:, direction] for direction, name in enumerate(SQUARE_CHANNELS)}
        gas = HPPSiteElement('gas', density=occupied.sum(axis=1), space=space, **channels)
        self.system_ = System(
            self.label,
            elements=[gas],
            interactions=[
                LatticeGasCollisionInteraction('collision', target='gas', geometry='square'),
                LatticeGasPropagationInteraction('propagation', target='gas', geometry='square'),
                ParticleCountInteraction('count', target='gas', channels=SQUARE_CHANNELS),
            ],
        )

Components

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

Elements:

Interactions:

  • LatticeGasCollisionInteraction, from Interaction
  • LatticeGasPropagationInteraction, from Interaction
  • ParticleCountInteraction, from Interaction
class HPPSiteElement(Element):
    """Sites of the HPP lattice gas.

    Every site has one boolean channel per direction of the square lattice, and the number `density` of its particles.

    Args:
        label:
            The label of the site or the lattice of sites.

        n0:
            The initial occupation of the channel along the columns, towards higher column indices.

        n1:
            The initial occupation of the channel along the rows, towards higher row indices.

        n2:
            The initial occupation of the channel along the columns, towards lower column indices.

        n3:
            The initial occupation of the channel along the rows, towards lower row indices.

        density:
            The initial number of particles at the site.

        space:
            The lattice of the sites, or `None` for one site.

    Examples:
        >>> from skcomplex.systems.physics.fluids import HPPSiteElement
        >>> HPPSiteElement('site').n0
        0.0
    """

    def __init__(
        self,
        label: str,
        *,
        n0: object = 0.0,
        n1: object = 0.0,
        n2: object = 0.0,
        n3: object = 0.0,
        density: object = 0.0,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space)
        self.n0 = n0
        self.n1 = n1
        self.n2 = n2
        self.n3 = n3
        self.density = density

class LatticeGasCollisionInteraction(Interaction):
    """Collisions of the particles at every site of a lattice gas.

    On the square lattice of the HPP gas, a site with exactly two particles of opposite directions turns them by a
    quarter turn. On the triangular lattice of the FHP-I gas, head-on pairs turn by a sixth of a turn to either side
    with equal probability, and symmetric triples turn by a sixth of a turn. Every other site keeps its particles, so
    collisions conserve the number of particles and their momentum at every site.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the sites, whose channels are `n0`, `n1`, and so on.

        geometry:
            The lattice of the gas, `'square'` for HPP or `'triangular'` for FHP-I.

    Raises:
        ValidationError:
            The geometry is not `'square'` or `'triangular'`.

    Examples:
        >>> from skcomplex.systems.physics.fluids import LatticeGasCollisionInteraction
        >>> LatticeGasCollisionInteraction('collision', target='gas', geometry='triangular').geometry
        'triangular'
    """

    law_classes = (UpdateLaw,)

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

    @staticmethod
    def _collide_square(*channels: Array, **kwargs: object) -> tuple[Array, ...]:
        """Return the HPP channels after every head-on pair turns by a quarter turn.

        This is the only collision.
        """
        codes = np.sum(
            [np.asarray(channel, dtype=np.intp) << direction for direction, channel in enumerate(channels)],
            axis=0,
        )
        following = codes.copy()
        for before, after in SQUARE_HEAD_ON.items():
            following = np.where(codes == before, after, following)
        return tuple(((following >> direction) & 1).astype(float) for direction in range(SQUARE_DIRECTIONS))

    @staticmethod
    def _collide_triangular(*channels: Array, random: Array, **kwargs: object) -> tuple[Array, ...]:
        """Return the channels after the collisions of FHP-I at every site.

        The collisions are the head-on and symmetric three-particle ones.
        """
        codes = sum(np.asarray(channel, dtype=np.intp) << direction for direction, channel in enumerate(channels))
        following = COLLISIONS[codes, (random < TURN_PROBABILITY).astype(np.intp)]
        return tuple(((following >> direction) & 1).astype(float) for direction in range(DIRECTIONS))

    def _check_arguments(self) -> None:
        """Raise an error for an argument the interaction cannot take.

        Raises:
            ValidationError:
                The geometry is not `'square'` or `'triangular'`.
        """
        super()._check_arguments()
        if self.geometry not in GEOMETRIES:
            error_msg = f'Unknown lattice gas geometry {self.geometry!r}. Use one of {", ".join(GEOMETRIES)}.'
            raise ValidationError(error_msg)

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the collision rule of the channels of the target.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        if self.geometry == 'square':
            channels = tuple(getattr(context.target, name) for name in SQUARE_CHANNELS)
            self.laws_ = [UpdateLaw(channels, self._collide_square, neighborhood=SITE, label=self.label)]
        else:
            channels = tuple(getattr(context.target, name) for name in CHANNELS)
            law = UpdateLaw(channels, self._collide_triangular, neighborhood=SITE, label=self.label, stochastic=True)
            self.laws_ = [law]

class LatticeGasPropagationInteraction(Interaction):
    """Propagation of every particle of a lattice gas to the neighbouring site along its direction.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the sites, whose channels are `n0`, `n1`, and so on.

        geometry:
            The lattice of the gas, `'square'` for HPP or `'triangular'` for FHP-I.

    Raises:
        ValidationError:
            The geometry is not `'square'` or `'triangular'`.

    Examples:
        >>> from skcomplex.systems.physics.fluids import LatticeGasPropagationInteraction
        >>> LatticeGasPropagationInteraction('propagation', target='gas').geometry
        'square'
    """

    law_classes = (UpdateLaw,)

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

    @staticmethod
    def _propagate_square(*columns: Array, **kwargs: object) -> tuple[Array, ...]:
        """Return the channels of the HPP gas after every particle moves one site.

        Every particle moves along its direction.
        """
        return tuple(columns[SQUARE_DIRECTIONS * direction + direction] for direction in range(SQUARE_DIRECTIONS))

    @staticmethod
    def _propagate_triangular(*columns: Array, **kwargs: object) -> tuple[Array, ...]:
        """Return the channels after every particle moves one site along its direction."""
        return tuple(columns[DIRECTIONS * direction + (direction + 3) % DIRECTIONS] for direction in range(DIRECTIONS))

    def _check_arguments(self) -> None:
        """Raise an error for an argument the interaction cannot take.

        Raises:
            ValidationError:
                The geometry is not `'square'` or `'triangular'`.
        """
        super()._check_arguments()
        if self.geometry not in GEOMETRIES:
            error_msg = f'Unknown lattice gas geometry {self.geometry!r}. Use one of {", ".join(GEOMETRIES)}.'
            raise ValidationError(error_msg)

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the propagation rule of the channels of the target.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        if self.geometry == 'square':
            channels = tuple(getattr(context.target, name) for name in SQUARE_CHANNELS)
            self.laws_ = [UpdateLaw(channels, self._propagate_square, neighborhood=ARRIVALS, label=self.label)]
        else:
            channels = tuple(getattr(context.target, name) for name in CHANNELS)
            self.laws_ = [
                UpdateLaw(channels, self._propagate_triangular, neighborhood=TRIANGULAR, label=self.label),
            ]

class ParticleCountInteraction(Interaction):
    """Count of the particles at every site, the sum of its channels.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the sites, whose value `density` holds the count.

        channels:
            The names of the channels of a site.

    Examples:
        >>> from skcomplex.systems.physics.fluids import ParticleCountInteraction
        >>> ParticleCountInteraction('count', target='gas', channels=('n0', 'n1')).channels
        ('n0', 'n1')
    """

    law_classes = (UpdateLaw,)

    def __init__(self, label: str, *, target: Role, channels: Sequence[str] = SQUARE_CHANNELS) -> None:
        super().__init__(label)
        self.target = target
        self.channels = channels

    @staticmethod
    def _count(*channels: Array, **kwargs: object) -> Array:
        """Return the number of particles at every site."""
        return np.sum(channels, axis=0)

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the count of the particles of the target.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        reads = tuple(getattr(context.target, name) for name in self.channels)
        law = UpdateLaw(context.target.density, self._count, neighborhood=SITE, reads=reads, label=self.label)
        self.laws_ = [law]