Skip to content

Living crystals

Active colloids of radius 1 with phoretic attraction.

  • Class: LivingCrystalsSystem
  • Import: from skcomplex.systems.physics.active_matter import LivingCrystalsSystem
  • Field: physics/active_matter
  • Topics: active matter, sde, clusters, phoresis, pair interaction
  • References:
    • Argun et al. 2021
    • Palacci 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 LivingCrystalsSystem

trajectory = LivingCrystalsSystem().prepare().simulate()

Parameters

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

Parameter Default
n_particles 100
size 50.0
speed 3.0
D_r 1.0
v0 20.0
cutoff 10.0
k 200.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 continuous
Laws noise, rate
Randomness stochastic
Domains real, vector
Structure fixed

Class

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

class LivingCrystalsSystem(BaseSystem):
    """Active colloids of radius 1 with phoretic attraction.

    The colloids gather into clusters that slowly coarsen.

    The colloids swim as active Brownian particles, drift towards each other at the speed `v0 / r^2` within the cutoff,
    and a stiff soft repulsion stands in for the hard-sphere correction at contact. The system has the positions
    `swimmers.x` and orientations `swimmers.theta`.

    Args:
        n_particles:
            The number of colloids.

        size:
            The side of the periodic box.

        speed:
            The self-propulsion speed.

        D_r:
            The rotational diffusion coefficient.

        v0:
            The phoretic drift speed at the distance of one radius.

        cutoff:
            The distance beyond which the attraction is neglected.

        k:
            The stiffness of the repulsion times the mobility.

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

        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 LivingCrystalsSystem
        >>> LivingCrystalsSystem(16).prepare().system_.interactions[3].v0
        20.0
    """

    field = 'physics/active_matter'
    topics = ('active matter', 'sde', 'clusters', 'phoresis', 'pair interaction')
    references = ('Argun et al. 2021', 'Palacci et al. 2013')
    until = 20.0
    dt = 0.1

    def __init__(
        self,
        n_particles: int = 100,
        *,
        size: float = 50.0,
        speed: float = 3.0,
        D_r: float = 1.0,
        v0: float = 20.0,
        cutoff: float = 10.0,
        k: float = 200.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_particles = n_particles
        self.size = size
        self.speed = speed
        self.D_r = D_r
        self.v0 = v0
        self.cutoff = cutoff
        self.k = k
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of active colloids with phoretic attraction.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        swimmers, interactions = MotilityInducedPhaseSeparationSystem._get_active_disks(
            self.n_particles,
            self.size,
            self.speed,
            self.D_r,
            self.random_state,
        )
        repulsion = SoftSphereInteraction('contact', target='swimmers', cutoff=2.0, k=self.k)
        attraction = PhoreticAttractionInteraction(
            'phoresis',
            target='swimmers',
            v0=self.v0,
            R=1.0,
            cutoff=self.cutoff,
        )
        self.system_ = System(
            self.label,
            elements=[swimmers],
            interactions=[*interactions, repulsion, attraction],
            solver=EulerMaruyamaSolver(dt=0.002),
        )

Components

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

Elements:

  • ActiveParticleElement, from Element

Interactions:

  • ActiveDiffusionInteraction, from Interaction
  • SelfPropulsionInteraction, from Interaction
  • SoftSphereInteraction, from Interaction
  • PhoreticAttractionInteraction, from Interaction
class ActiveParticleElement(Element):
    """Active particle in the plane with the position `x` and the orientation `theta`.

    A `SelfPropulsionInteraction` moves the position along the orientation, and an `ActiveDiffusionInteraction` makes
    both diffuse and the orientation turn.

    Args:
        label:
            The label of the particle or the group.

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

        theta:
            The initial orientation, one per particle or shared.

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

        space:
            The continuous space of the particles, or `None` for none.

    Examples:
        >>> from skcomplex.systems.physics.common.particles import ActiveParticleElement
        >>> ActiveParticleElement('swimmer', theta=1.0).theta
        1.0
    """

    visual = POINTS

    def __init__(
        self,
        label: str,
        *,
        x: object = (0.0, 0.0),
        theta: Annotated[object, Period(2 * math.pi)] = 0.0,
        size: int | None = None,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, size=size, space=space)
        self.x = x
        self.theta = theta

class ActiveDiffusionInteraction(Interaction):
    """Diffusion of the position and the orientation of active particles.

    The position follows `dx = sqrt(2 D_t) dW` and the orientation `dtheta = omega dt + sqrt(2 D_r) dW`. With `omega =
    0` it is an active Brownian particle, and otherwise a chiral one. It is a bacterium, a Janus colloid, or a robot
    that loses its heading.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the particles, whose position is `x` and orientation `theta`.

        D_t:
            The translational diffusion coefficient.

        D_r:
            The rotational diffusion coefficient.

        omega:
            The angular velocity of a chiral particle.

    Examples:
        >>> import numpy as np
        >>> from skcomplex.core.components.spaces import ContinuousSpace
        >>> from skcomplex.core.engine.solvers import EulerMaruyamaSolver
        >>> from skcomplex.core.systems import System
        >>> from skcomplex.systems.physics.common.particles import ActiveDiffusionInteraction, ActiveParticleElement
        >>> plane = ContinuousSpace(n_dim=2)
        >>> swimmers = ActiveParticleElement('swimmers', x=np.zeros((4000, 2)), size=4000, space=plane)
        >>> diffusion = ActiveDiffusionInteraction('diffusion', target='swimmers', D_r=0.5)
        >>> system = System(elements=[swimmers], interactions=[diffusion], solver=EulerMaruyamaSolver(dt=0.1))
        >>> angles = system.prepare().simulate(until=2.0, dt=2.0, random_state=0).get_values('swimmers.theta')[-1]
        >>> round(float((angles**2).mean()), 1)
        2.0
    """

    law_classes = (NoiseLaw, RateLaw)

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        D_t: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
        D_r: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
        omega: float = 0.0,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.D_t = D_t
        self.D_r = D_r
        self.omega = omega

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the noise of the position, and the turning and the noise of the orientation of the target.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        s = context
        theta = s.target.theta
        self.laws_ = [
            NoiseLaw(s.target.x, sqrt(2 * s.D_t)),
            RateLaw(theta.diff(s.t), s.omega),
            NoiseLaw(theta, sqrt(2 * s.D_r)),
        ]

class SelfPropulsionInteraction(Interaction):
    """Self-propulsion `x' += speed (cos theta, sin theta)` of a position in the plane.

    The position drifts along a heading `theta`, which other laws turn and diffuse. It is a swimming bacterium, a
    Janus colloid, or a walking robot.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the particle or the group whose positions move.

        speed:
            The self-propulsion speed.

        state:
            The name of the vector value that holds the position.

        heading:
            The name of the value that holds the heading.

    Examples:
        >>> import math
        >>> from skcomplex.core.components.spaces import ContinuousSpace
        >>> from skcomplex.core.systems import System
        >>> from skcomplex.systems.physics.common.particles import ActiveParticleElement, SelfPropulsionInteraction
        >>> swimmer = ActiveParticleElement('swimmer', theta=math.pi / 2)
        >>> propulsion = SelfPropulsionInteraction('propulsion', target='swimmer', speed=2.0)
        >>> system = System(elements=[swimmer], interactions=[propulsion], space=ContinuousSpace(n_dim=2))
        >>> trajectory = system.prepare().simulate(until=1.0, dt=1.0)
        >>> trajectory.get_values('swimmer.x')[-1].round(6).tolist()
        [0.0, 2.0]
    """

    law_classes = (RateLaw,)

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        speed: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
        state: str = 'x',
        heading: str = 'theta',
    ) -> None:
        super().__init__(label)
        self.target = target
        self.speed = speed
        self.state = state
        self.heading = heading

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the drift of the position of the target along its heading.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        heading = getattr(context.target, self.heading)
        velocity = context.speed * vector(SR, [cos(heading), sin(heading)])
        self.laws_ = [RateLaw(getattr(context.target, self.state), velocity)]

class SoftSphereInteraction(Interaction):
    """Soft repulsion between overlapping spheres.

    The energy is `k (cutoff - r)^2 / 2` closer than the cutoff, so every member is pushed by `k (cutoff - r)` along the
    line of centres from each member that overlaps it, `x_i' += sum_j k (cutoff - r_ij) (x_i - x_j) / r_ij`. In a
    periodic box the separations are minimum images. The push moves an overdamped sphere, and with `inertia` it moves
    the acceleration instead, `RateLaw(x.diff(t, 2), push / m)`, divided by the mass `m` of a sphere that has one. It is
    the contact of disks in a granular layer, of cells in a tissue, or of swimmers that bump into each other.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the group of spheres.

        cutoff:
            The contact distance, the sum of the two radii.

        k:
            The stiffness.

        inertia:
            Whether the push moves the acceleration of a sphere, divided by its mass `m` when it has one.

    Examples:
        >>> from skcomplex.systems.physics.active_matter._collective import SoftSphereInteraction
        >>> SoftSphereInteraction('contact', target='gas', cutoff=0.1, k=50.0).k
        50.0
    """

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        cutoff: Annotated[float, RealSet.open(0, oo)] = 1.0,
        k: Annotated[float, RealSet.open(0, oo)] = 100.0,
        inertia: bool = False,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.cutoff = cutoff
        self.k = k
        self.inertia = inertia

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the push of the overlapping neighbours on every sphere.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        s, x = context, context.target.x
        separation = x - s.target.other.x
        r = sqrt(separation.dot_product(separation))
        distance = SR.var('r', domain='positive')
        slope = (s.k * (s.cutoff - distance) ** 2 / 2).diff(distance).subs({distance: r})
        push = neighbors(-slope * separation / r, RadiusNeighborhood(self.cutoff))
        if self.inertia:
            mass = getattr(s.target, 'm', None)
            self.laws_ = [RateLaw(x.diff(s.t, 2), push if mass is None else push / mass)]
        else:
            self.laws_ = [RateLaw(x.diff(s.t), push)]

class PhoreticAttractionInteraction(Interaction):
    """Phoretic attraction between active colloids, the pair potential `-v0 R^2 / r`.

    Every member of a pair closer than the cutoff drifts towards the other at the speed `v0 R^2 / r^2`. It is a
    light-activated colloid that gathers others by the chemicals it releases.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the colloids, with the positions `x`.

        v0:
            The drift speed at the distance `R`.

        R:
            The particle radius.

        cutoff:
            The distance beyond which the attraction is neglected.

    Examples:
        >>> from skcomplex.systems.physics.active_matter import PhoreticAttractionInteraction
        >>> PhoreticAttractionInteraction('phoresis', target='colloids', v0=20.0).v0
        20.0
    """

    law_classes = (RateLaw,)

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        v0: Annotated[float, RealSet.closed_open(0, oo)] = 20.0,
        R: Annotated[float, RealSet.open(0, oo)] = 1.0,
        cutoff: Annotated[float, RealSet.open(0, oo)] = 1.0,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.v0 = v0
        self.R = R
        self.cutoff = cutoff

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the drift of every colloid of the target towards its neighbours within the cutoff.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        s = context
        x = s.target.x
        separation = x - s.target.other.x
        r = sqrt(separation.dot_product(separation))
        drift = -s.v0 * s.R**2 / r**2 * separation / r
        self.laws_ = [RateLaw(x, neighbors(drift, RadiusNeighborhood(self.cutoff)))]