Skip to content

Ideal gas

An ideal gas of non-interacting molecules in a reflecting box.

  • Class: IdealGasSystem
  • Import: from skcomplex.systems.physics.statistical_mechanics import IdealGasSystem
  • Field: physics/statistical_mechanics
  • Topics: gas, ode, boundary
  • References:
    • Maxwell 1860

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 IdealGasSystem

trajectory = IdealGasSystem().prepare().simulate()

Parameters

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

Parameter Default
n_molecules 500
kT 1.0
size 1.0
n_dim 2
mass 1.0
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 any
Space continuous
Laws any
Randomness deterministic
Domains vector
Structure fixed

Class

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

class IdealGasSystem(BaseSystem):
    """An ideal gas of non-interacting molecules in a reflecting box.

    Positions are uniform in the box and velocity components are normal with variance `kT / mass`, with the mean
    velocity removed. The molecules need no law: given their starting velocities `x'`, they keep them between the
    walls, as Newton's first law says. The molecules are drawn as points colored by speed. NOTE-RETURNS: The gas.

    Args:
        n_molecules:
            The number of molecules.

        kT:
            The temperature in energy units.

        size:
            The side of the box.

        n_dim:
            The number of dimensions.

        mass:
            The mass of every molecule.

        random_state:
            The seed or generator of the initial positions and velocities.

        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 IdealGasSystem
        >>> gas = IdealGasSystem(1000, random_state=0).prepare().system_.elements[0]
        >>> gas.label, type(gas).__name__, gas.size
        ('gas', 'GasMoleculeElement', 1000)
    """

    field = 'physics/statistical_mechanics'
    topics = ('gas', 'ode', 'boundary')
    references = ('Maxwell 1860',)
    until = 2.0
    dt = 0.01

    def __init__(
        self,
        n_molecules: int = 500,
        *,
        kT: float = 1.0,
        size: float = 1.0,
        n_dim: int = 2,
        mass: float = 1.0,
        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_molecules = n_molecules
        self.kT = kT
        self.size = size
        self.n_dim = n_dim
        self.mass = mass
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set `system_` to the molecules at uniform positions with thermal velocities.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        rng = np.random.default_rng(self.random_state)
        velocities = rng.normal(0.0, np.sqrt(self.kT / self.mass), size=(self.n_molecules, self.n_dim))
        velocities -= velocities.mean(axis=0)
        box = ContinuousSpace(extent=[self.size] * self.n_dim, boundary='reflecting')
        molecules = GasMoleculeElement(
            'gas',
            x=rng.uniform(0.0, self.size, size=(self.n_molecules, self.n_dim)),
            m=self.mass,
            values={"x'": velocities},
            size=self.n_molecules,
            space=box,
        )
        self.system_ = System(self.label, elements=[molecules], space=box)

Components

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

Elements:

class GasMoleculeElement(MassPointElement):
    """Molecule of an ideal gas, drawn as a point coloured by its velocity.

    Examples:
        >>> from skcomplex.systems.physics.statistical_mechanics import GasMoleculeElement
        >>> GasMoleculeElement('molecule', m=2.0).m
        2.0
    """

    visual = VisualSpec('points', {'position': 'x', 'color_by': "x'", 'states': ()})