Skip to content

Toner-Tu hydrodynamics

The Toner-Tu hydrodynamics of a polar flock on a periodic lattice.

  • Class: TonerTuSystem
  • Import: from skcomplex.systems.physics.active_matter import TonerTuSystem
  • Field: physics/active_matter
  • Topics: field, flocking, hydrodynamics, pde
  • References:
    • Toner and Tu 1995
    • Marchetti et al. 2013

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.active_matter import TonerTuSystem

trajectory = TonerTuSystem().prepare().simulate()

Parameters

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

Parameter Default
shape (32, 32)
rho0 1.0
rho_c 0.5
a 1.0
beta 1.0
lam 1.0
sigma 0.5
D 0.5
spacing 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 continuous
Space lattice
Laws noise, rate
Randomness stochastic
Domains real
Structure fixed

Class

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

class TonerTuSystem(BaseSystem):
    """The Toner-Tu hydrodynamics of a polar flock on a periodic lattice.

    The flock starts from a disordered velocity field.

    Above the critical density the velocity grows and aligns until the whole fluid moves together with the speed `sqrt(a
    (rho0 - rho_c) / beta)`. The system has the fields `flock.rho`, `flock.vx`, and `flock.vy`.

    Args:
        shape:
            The number of sites along each axis.

        rho0:
            The mean density.

        rho_c:
            The critical density of the ordering transition.

        a:
            The rate of the ordering instability per unit density.

        beta:
            The saturation of the speed.

        lam:
            The strength of the self-advection.

        sigma:
            The pressure per unit density.

        D:
            The viscosity of the velocity.

        spacing:
            The distance between sites.

        random_state:
            The seed or generator of the initial 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.active_matter import TonerTuSystem
        >>> TonerTuSystem((8, 8)).prepare().simulate(until=1.0).get_values('flock.rho').shape
        (3, 64)
    """

    field = 'physics/active_matter'
    topics = ('field', 'flocking', 'hydrodynamics', 'pde')
    references = ('Toner and Tu 1995', 'Marchetti et al. 2013')
    until = 20.0
    dt = 0.5

    def __init__(
        self,
        shape: Sequence[int] = (32, 32),
        *,
        rho0: float = 1.0,
        rho_c: float = 0.5,
        a: float = 1.0,
        beta: float = 1.0,
        lam: float = 1.0,
        sigma: float = 0.5,
        D: float = 0.5,
        spacing: 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.shape = shape
        self.rho0 = rho0
        self.rho_c = rho_c
        self.a = a
        self.beta = beta
        self.lam = lam
        self.sigma = sigma
        self.D = D
        self.spacing = spacing
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the Toner-Tu hydrodynamics of a polar flock on a periodic lattice.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        shape = tuple(self.shape)
        rng = np.random.default_rng(self.random_state)
        flock = PolarFluidElement(
            'flock',
            rho=np.full(shape, self.rho0).ravel(),
            vx=rng.normal(0.0, 0.1, shape).ravel(),
            vy=(rng.normal(0.0, 0.1, shape) + 0.05).ravel(),
            space=LatticeSpace(shape, spacing=self.spacing),
        )
        flow = TonerTuInteraction(
            'flow',
            target='flock',
            a=self.a,
            rho_c=self.rho_c,
            beta=self.beta,
            lam=self.lam,
            sigma=self.sigma,
            D=self.D,
        )
        self.system_ = System(self.label, elements=[flock], interactions=[flow], solver=EulerMaruyamaSolver(dt=0.02))

Components

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

Elements:

Interactions:

  • TonerTuInteraction, from Interaction
class PolarFluidElement(Element):
    """Sites of a polar active fluid with the density `rho` and the velocity `(vx, vy)`.

    Args:
        label:
            The label of the fluid.

        rho:
            The initial density, one per site or shared.

        vx:
            The initial velocity along the first axis, one per site or shared.

        vy:
            The initial velocity along the second axis, one per site or shared.

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

    Examples:
        >>> from skcomplex.systems.physics.active_matter import PolarFluidElement
        >>> PolarFluidElement('site', rho=2.0).rho
        2.0
    """

    def __init__(
        self,
        label: str,
        *,
        rho: object = 1.0,
        vx: object = 0.0,
        vy: object = 0.0,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space)
        self.rho = rho
        self.vx = vx
        self.vy = vy

class TonerTuInteraction(Interaction):
    """Toner-Tu hydrodynamics of a polar active fluid.

    The density is conserved, `drho/dt + div(rho v) = 0`, and the velocity follows
    `dv/dt + lam (v . grad) v = a (rho - rho_c) v - beta |v|^2 v - sigma grad(rho) + D laplacian(v)` with additive
    noise of intensity `noise`. Above the critical density the fluid moves with the speed
    `sqrt(a (rho - rho_c) / beta)`. It is a flock of birds, a school of fish, or a swarm of bacteria seen from afar.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the fluid, with the density `rho` and the velocity `(vx, vy)`.

        a:
            The rate of the ordering instability per unit density.

        rho_c:
            The critical density of the ordering transition.

        beta:
            The saturation of the speed.

        lam:
            The strength of the self-advection.

        sigma:
            The pressure per unit density.

        D:
            The viscosity of the velocity.

        noise:
            The intensity of the velocity noise.

    Examples:
        >>> from skcomplex.systems.physics.active_matter import TonerTuInteraction
        >>> TonerTuInteraction('flow', target='flock').rho_c
        0.5
    """

    law_classes = (NoiseLaw, RateLaw)

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        a: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
        rho_c: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
        beta: Annotated[float, RealSet.open(0, oo)] = 1.0,
        lam: float = 1.0,
        sigma: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
        D: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
        noise: Annotated[float, RealSet.closed_open(0, oo)] = 0.0,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.a = a
        self.rho_c = rho_c
        self.beta = beta
        self.lam = lam
        self.sigma = sigma
        self.D = D
        self.noise = noise

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the continuity equation and a Toner-Tu equation per velocity component of the target.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        s = context
        rho, vx, vy = s.target.rho, s.target.vx, s.target.vy
        velocity = [vx, vy]
        pressure = gradient(s.sigma * rho, 2)
        speed_squared = vx**2 + vy**2
        laws: list[BaseLaw] = [RateLaw(rho.diff(s.t), -divergence([rho * vx, rho * vy]))]
        for axis, component in enumerate(velocity):
            transport = advection(component, [s.lam * item for item in velocity])
            growth = (s.a * (rho - s.rho_c) - s.beta * speed_squared) * component
            laws += [
                RateLaw(component.diff(s.t), -transport + growth - pressure[axis] + s.D * laplacian(component)),
                NoiseLaw(component, sqrt(2 * s.noise)),
            ]
        self.laws_ = laws