Skip to content

Moment equations of Langevin traffic with a jam instability

The density and flux equations of Langevin traffic on a ring road.

  • Class: TrafficDensityEquationSystem
  • Import: from skcomplex.systems.engineering.traffic import TrafficDensityEquationSystem
  • Field: engineering/traffic
  • Topics: traffic, field, jams, instability, pde
  • References:
    • Bertin 2021

Use this system

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

from skcomplex.systems.engineering.traffic import TrafficDensityEquationSystem

trajectory = TrafficDensityEquationSystem().prepare().simulate()

Parameters

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

Parameter Default
n 100
length 50.0
density 0.5
gamma 1.0
u0 1.0
rho_max 1.0
D1 0.2
perturbation 0.01
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 continuous
Space lattice
Laws rate
Randomness deterministic
Domains real
Structure fixed

Class

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

class TrafficDensityEquationSystem(BaseSystem):
    """The density and flux equations of Langevin traffic on a ring road.

    Their uniform flow breaks into jams. The density `rho` and the flux `w` of cars that relax to the desired speed
    `u(rho) = u0 (1 - rho / rho_max)` at the rate `gamma`, with the diffusion coefficient `D(rho) = D1 rho`, follow the
    moment equations `rho_t + w_x = (D rho_x)_x` and `w_t + (2 u w - rho u^2)_x = -gamma (w - rho u) + (D w_x)_x`,
    closed by expanding the velocity distribution around `u(rho)`. A long density wave of the uniform flow grows when
    the effective diffusion coefficient `D(rho) + ((rho u^2)' - (rho u)'^2) / gamma = D(rho) - rho^2 u'(rho)^2 / gamma`
    is negative, so the flow is unstable above the density `D1 gamma rho_max^2 / u0^2`, and the perturbation grows into
    dense jams. The uniform density starts with independent Gaussian perturbations of relative size `perturbation` in
    every cell, at the flux `rho u(rho)`.

    The system has the density `road.rho` and the flux `road.w`.

    Args:
        n:
            The number of cells.

        length:
            The length of the ring road.

        density:
            The mean density.

        gamma:
            The relaxation rate of the speed.

        u0:
            The desired speed on an empty road.

        rho_max:
            The jam density, at which the desired speed vanishes.

        D1:
            The diffusion coefficient per unit density.

        perturbation:
            The relative standard deviation of the initial density perturbations.

        random_state:
            The seed or generator of the initial perturbations.

        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.engineering.traffic import TrafficDensityEquationSystem
        >>> TrafficDensityEquationSystem(10).prepare().system_.interactions[0].D1
        0.2
    """

    field = 'engineering/traffic'
    topics = ('traffic', 'field', 'jams', 'instability', 'pde')
    references = ('Bertin 2021',)
    until = 400.0
    dt = 4.0

    def __init__(
        self,
        n: int = 100,
        *,
        length: float = 50.0,
        density: float = 0.5,
        gamma: float = 1.0,
        u0: float = 1.0,
        rho_max: float = 1.0,
        D1: float = 0.2,
        perturbation: float = 0.01,
        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 = n
        self.length = length
        self.density = density
        self.gamma = gamma
        self.u0 = u0
        self.rho_max = rho_max
        self.D1 = D1
        self.perturbation = perturbation
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set `system_` to the components of the system built from its model parameters."""
        super()._prepare(context)
        rng = np.random.default_rng(self.random_state)
        rho = self.density * (1 + self.perturbation * rng.standard_normal(self.n))
        space = LatticeSpace(self.n, spacing=self.length / self.n)
        road = TrafficMomentsElement('road', rho=rho, w=rho * self.u0 * (1 - rho / self.rho_max), space=space)
        moments = TrafficMomentsInteraction(
            'moments',
            target='road',
            gamma=self.gamma,
            u0=self.u0,
            rho_max=self.rho_max,
            D1=self.D1,
        )
        self.system_ = System(self.label, elements=[road], interactions=[moments], solver=RK4Solver(dt=0.1))

Components

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

Elements:

  • TrafficMomentsElement, from Element

Interactions:

  • TrafficMomentsInteraction, from Interaction
class TrafficMomentsElement(Element):
    """Density `rho` and flow `w` of Langevin traffic on a road.

    They are the first two moments of its speeds.

    Args:
        label:
            The label of the road.

        rho:
            The initial density, one value per cell or one for all.

        w:
            The initial flow, one value per cell or one for all.

        space:
            The lattice of the cells of the road, or `None` for one cell.

    Examples:
        >>> from skcomplex.systems.engineering.traffic import TrafficMomentsElement
        >>> TrafficMomentsElement('road').w
        0.25
    """

    def __init__(
        self,
        label: str,
        *,
        rho: Annotated[object, RealSet.closed_open(0, oo)] = 0.5,
        w: object = 0.25,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space)
        self.rho = rho
        self.w = w

class TrafficMomentsInteraction(Interaction):
    """Closed moment equations of the density `rho` and the flow `w` of Langevin traffic.

    They are `rho_t + w_x = (D rho_x)_x` and `w_t + (2 u w - rho u^2)_x = -gamma (w - rho u) + (D w_x)_x`, with the
    desired speed `u(rho) = u0 (1 - rho / rho_max)` and the diffusion coefficient `D = D1 rho`.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the road.

        gamma:
            The relaxation rate of the speed.

        u0:
            The desired speed on an empty road.

        rho_max:
            The jam density.

        D1:
            The diffusion coefficient per unit density.

    Examples:
        >>> from skcomplex.systems.engineering.traffic import TrafficMomentsInteraction
        >>> TrafficMomentsInteraction('moments', target='road').D1
        0.2
    """

    law_classes = (RateLaw,)

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        gamma: Annotated[float, RealSet.open(0, oo)] = 1.0,
        u0: Annotated[float, RealSet.open(0, oo)] = 1.0,
        rho_max: Annotated[float, RealSet.open(0, oo)] = 1.0,
        D1: Annotated[float, RealSet.closed_open(0, oo)] = 0.2,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.gamma = gamma
        self.u0 = u0
        self.rho_max = rho_max
        self.D1 = D1

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the closed moment equations of the density and the flow.

        Args:
            context:
                The namespace of the road, the time `t`, and the parameters.
        """
        super()._prepare(context)
        s, t = context, context.t
        rho, w = s.target.rho, s.target.w
        speed = LangevinDrivingInteraction._get_target_speed(rho, s)
        self.laws_: list[BaseLaw] = [
            RateLaw(rho.diff(t), -divergence([w]) + laplacian(s.D1 * rho**2 / 2)),
            RateLaw(
                w.diff(t),
                -divergence([2 * speed * w - rho * speed**2])
                - s.gamma * (w - rho * speed)
                + s.D1 * (laplacian(rho * w) + rho * laplacian(w) - w * laplacian(rho)) / 2,
            ),
        ]