Skip to content

FHP lattice gas

The FHP-I lattice gas on a periodic triangular lattice.

  • Class: FHPLatticeGasSystem
  • Import: from skcomplex.systems.physics.fluids import FHPLatticeGasSystem
  • Field: physics/fluids
  • Topics: lattice, lattice gas, stochastic, conservation
  • References:
    • Frisch, Hasslacher, and Pomeau 1986
    • Dilão 2023

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 FHPLatticeGasSystem

trajectory = FHPLatticeGasSystem().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
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 FHPLatticeGasSystem(BaseSystem):
    """The FHP-I lattice gas on a periodic triangular lattice.

    A dense disc spreads into the gas.

    Every channel of every site is occupied at random with probability `density`, or `bump` inside a disc at the centre
    of the lattice, so the excess density spreads as a sound wave and then diffuses. The system has the channels
    `gas.n0` to `gas.n5` and the particle numbers `gas.density`.

    Args:
        shape:
            The number of rows and columns of the triangular lattice.

        density:
            The occupation probability of a channel outside the disc.

        bump:
            The occupation probability of a channel inside the disc.

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

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

    field = 'physics/fluids'
    topics = ('lattice', 'lattice gas', 'stochastic', 'conservation')
    references = ('Frisch, Hasslacher, and Pomeau 1986', 'Dilão 2023')
    until = 200
    dt = 1

    def __init__(
        self,
        shape: Sequence[int] = (64, 64),
        *,
        density: float = 0.2,
        bump: float = 0.6,
        radius: float | None = None,
        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.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), geometry='triangular').prepare()
        positions = space.positions_
        centre = positions.mean(axis=0)
        size = min(space.shape_) / 6 if self.radius is None else self.radius
        inside = np.linalg.norm(positions - centre, axis=1) < size
        probability = np.where(inside, self.bump, self.density)[:, None]
        occupied = (np.random.default_rng(self.random_state).random((space.size_, DIRECTIONS)) < probability).astype(
            float,
        )
        channels = {name: occupied[:, direction] for direction, name in enumerate(CHANNELS)}
        gas = FHPSiteElement('gas', density=occupied.sum(axis=1), space=space, **channels)
        self.system_ = System(
            self.label,
            elements=[gas],
            interactions=[
                LatticeGasCollisionInteraction('collision', target='gas', geometry='triangular'),
                LatticeGasPropagationInteraction('propagation', target='gas', geometry='triangular'),
                ParticleCountInteraction('count', target='gas', channels=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 FHPSiteElement(Element):
    """Sites of the FHP-I lattice gas.

    Every site has one boolean channel per direction of the triangular 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 at 0 degrees.

        n1:
            The initial occupation of the channel at 60 degrees.

        n2:
            The initial occupation of the channel at 120 degrees.

        n3:
            The initial occupation of the channel at 180 degrees.

        n4:
            The initial occupation of the channel at 240 degrees.

        n5:
            The initial occupation of the channel at 300 degrees.

        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 FHPSiteElement
        >>> FHPSiteElement('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,
        n4: object = 0.0,
        n5: 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.n4 = n4
        self.n5 = n5
        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]