Skip to content

Lévy walk

The Lévy walk.

  • Class: LevyWalkSystem
  • Import: from skcomplex.systems.physics.soft_matter import LevyWalkSystem
  • Field: physics/soft_matter
  • Topics: anomalous diffusion, renewal process, superdiffusion, random walk
  • References:
    • Argun et al. 2021
    • Zaburdaev, Denisov, and Klafter 2015
    • Shlesinger, West, and Klafter 1987

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.soft_matter import LevyWalkSystem

trajectory = LevyWalkSystem().prepare().simulate()

Parameters

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

Parameter Default
n_walkers 500
sigma 1.5
tau0 1.0
speed 1.0
n_dim 2
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 continuous
Laws jump, rate
Randomness stochastic
Domains vector
Structure fixed

Class

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

class LevyWalkSystem(BaseSystem):
    """The Lévy walk.

    Flights at constant speed in random directions last times with the tail `tau^(-1 - sigma)`.

    For `1 < sigma < 2` the flight times have a mean but no variance, so long flights dominate and the ensemble mean
    squared displacement grows as `t^(3 - sigma)`, faster than Brownian motion and slower than ballistic motion. The
    system has the positions `walkers.x`, the velocities `walkers.v`, and the turn count `turns.renewals`.

    Args:
        n_walkers:
            The number of independent walkers.

        sigma:
            The tail exponent of the flight times.

        tau0:
            The shortest flight time.

        speed:
            The speed of every flight.

        n_dim:
            The number of dimensions.

        random_state:
            The seed or generator of the directions of the first flights.

        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.soft_matter import LevyWalkSystem
        >>> LevyWalkSystem(10, sigma=1.2).prepare().system_.interactions[0].sigma
        1.2
    """

    field = 'physics/soft_matter'
    topics = ('anomalous diffusion', 'renewal process', 'superdiffusion', 'random walk')
    references = ('Argun et al. 2021', 'Zaburdaev, Denisov, and Klafter 2015', 'Shlesinger, West, and Klafter 1987')
    until = 500.0
    dt = 2.5

    def __init__(
        self,
        n_walkers: int = 500,
        *,
        sigma: float = 1.5,
        tau0: float = 1.0,
        speed: float = 1.0,
        n_dim: int = 2,
        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_walkers = n_walkers
        self.sigma = sigma
        self.tau0 = tau0
        self.speed = speed
        self.n_dim = n_dim
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of walkers that fly at constant speed for power-law flight times.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        directions = np.random.default_rng(self.random_state).standard_normal((self.n_walkers, self.n_dim))
        velocities = self.speed * directions / np.linalg.norm(directions, axis=1, keepdims=True)
        walkers = LevyWalkerElement(
            'walkers',
            x=np.zeros((self.n_walkers, self.n_dim)),
            v=velocities,
            size=self.n_walkers,
        )
        turns = LevyFlightTimeInteraction(
            'turns',
            target='walkers',
            sigma=self.sigma,
            tau0=self.tau0,
            speed=self.speed,
        )
        self.system_ = System(
            self.label,
            elements=[walkers],
            interactions=[turns],
            space=ContinuousSpace(n_dim=self.n_dim),
            solver=RK4Solver(dt=0.5),
        )

Components

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

Elements:

Interactions:

  • LevyFlightTimeInteraction, from Interaction
class LevyWalkerElement(Element):
    """Walkers at the positions `x` that fly with the velocities `v`.

    Args:
        label:
            The label of the walkers.

        x:
            The initial position, one row per walker or shared.

        v:
            The initial velocity, one row per walker or shared.

        size:
            The number of walkers, or `None` for one.

    Examples:
        >>> from skcomplex.systems.physics.soft_matter import LevyWalkerElement
        >>> LevyWalkerElement('walkers', v=(1.0, 0.0), size=10).v
        (1.0, 0.0)
    """

    def __init__(self, label: str, *, x: object = (0.0, 0.0), v: object = (0.0, 0.0), size: int | None = None) -> None:
        super().__init__(label, size=size)
        self.x = x
        self.v = v

class LevyFlightTimeInteraction(Interaction):
    """Flights at constant speed in random directions that last power-law flight times.

    The position `x` moves with the velocity `v`, and after a flight time `tau0 r^(-1 / sigma)` for `r` uniform the
    velocity turns to a uniformly random direction at the speed `speed`. It is a bacterium that runs and tumbles, a
    tracer in a turbulent flow, or the gaze of an eye that scans a scene.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the walkers.

        sigma:
            The tail exponent of the flight times, between 1 and 2 for superdiffusion.

        tau0:
            The shortest flight time.

        speed:
            The speed of every flight.

    Examples:
        >>> from skcomplex.systems.physics.soft_matter import LevyFlightTimeInteraction
        >>> LevyFlightTimeInteraction('turns', target='walkers', speed=2.0).speed
        2.0
    """

    law_classes = (RateLaw, JumpLaw)

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        sigma: Annotated[float, RealSet.open(0, oo)] = 1.5,
        tau0: Annotated[float, RealSet.open(0, oo)] = 1.0,
        speed: Annotated[float, RealSet.open(0, oo)] = 1.0,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.sigma = sigma
        self.tau0 = tau0
        self.speed = speed

    def _get_direction(self, size: int) -> list[Expression]:
        """Return a unit vector of uniformly random direction, drawn from standard normal components."""
        components = [normal(f'direction{index}') for index in range(size)]
        length = sqrt(sum(component**2 for component in components))
        return [component / length for component in components]

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the flight of the positions and the turns of the velocities of the target.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        s = context
        x, v = s.target.x, s.target.v
        waiting = draw('flight', 'pareto', b=s.sigma, scale=s.tau0)
        directions = self._get_direction(len(list(v)))
        reset = {item: s.speed * direction for item, direction in zip(v, directions, strict=True)}
        self.laws_ = [RateLaw(x, v), JumpLaw(waiting=waiting, reset=reset, label=self.label)]