Grégoire-Chaté cohesive flocks
The Grégoire-Chaté system.
- Class:
GregoireChateSystem - Import:
from skcomplex.systems.physics.active_matter import GregoireChateSystem - Field:
physics/active_matter - Topics:
active matter,flocking,cohesion,discrete time,neighborhood - References:
- Grégoire and Chaté 2004
- Vicsek and Zafeiris 2012
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 GregoireChateSystem
trajectory = GregoireChateSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_particles |
200 |
size |
5.0 |
v0 |
0.05 |
eta |
0.1 |
alpha |
1.0 |
beta |
0.5 |
seed |
0 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | discrete |
| Space | continuous |
| Laws | update |
| Randomness | unknown |
| Domains | real, vector |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class GregoireChateSystem(BaseSystem):
"""The Grégoire-Chaté system.
Aligning particles also attract and repel, forming cohesive flocks.
At every step particle `i` takes the direction of `alpha sum_j exp(i theta_j) + beta sum_j f(r_ij) e_ij + eta n_i
xi`, over the `n_i` particles within the unit distance, where `e_ij` points from `i` to `j`, `xi` is a random unit
vector, and the force `f` is a strong repulsion below `r_c = 0.2`, zero at the preferred distance `r_e = 0.5`, and
attractive up to `r_a = 0.8`. The particle then moves `v0` along the new heading. At low noise and moderate cohesion
the particles gather into cohesive, aligned flocks, and the polar order parameter grows towards 1. A strong cohesion
locks them in place, and the order is lost. The system has the positions `flock.x`, the headings `flock.theta`, and
the observables `polarization` and `nematic_order`.
Args:
n_particles:
The number of particles.
size:
The side of the periodic box, in units of the interaction radius.
v0:
The distance a particle moves in one step.
eta:
The strength of the vectorial noise.
alpha:
The strength of the alignment.
beta:
The strength of the cohesion.
seed:
The seed of the uniform initial positions and headings.
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 GregoireChateSystem
>>> GregoireChateSystem(10, beta=2.0).prepare().system_.interactions[1].beta
2.0
"""
field = 'physics/active_matter'
topics = ('active matter', 'flocking', 'cohesion', 'discrete time', 'neighborhood')
references = ('Grégoire and Chaté 2004', 'Vicsek and Zafeiris 2012')
until = 300.0
dt = 1.0
observables = tuple(_get_orders('flock'))
def __init__(
self,
n_particles: int = 200,
*,
size: float = 5.0,
v0: float = 0.05,
eta: float = 0.1,
alpha: float = 1.0,
beta: float = 0.5,
seed: int = 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.v0 = v0
self.eta = eta
self.alpha = alpha
self.beta = beta
self.seed = seed
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the Grégoire-Chaté system of aligning particles that also attract and repel.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
x, theta = GregoireChateSystem._get_flockers(self.n_particles, self.size, self.seed)
flock = CohesiveParticleElement(
'flock',
x=x,
theta=theta,
size=self.n_particles,
space=ContinuousSpace(extent=(self.size, self.size), boundary='periodic'),
)
cohesion = CohesiveAlignmentInteraction(
'cohesion',
target='flock',
eta=self.eta,
alpha=self.alpha,
beta=self.beta,
)
self.system_ = System(
self.label,
elements=[flock],
interactions=[PropulsionStepInteraction('propulsion', target='flock', speed=self.v0), cohesion],
)
@staticmethod
def _get_flockers(n_particles: int, size: float, seed: int) -> tuple[Array, Array]:
"""Return uniform positions in a square and uniform headings."""
rng = np.random.default_rng(seed)
return rng.uniform(0.0, size, (n_particles, 2)), rng.uniform(-math.pi, math.pi, n_particles)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
CohesiveParticleElement, fromElement
Interactions:
PropulsionStepInteraction, fromInteractionCohesiveAlignmentInteraction, fromInteraction
class CohesiveParticleElement(Element):
"""Particles of a Grégoire-Chaté flock with the positions `x` and the headings `theta`.
Args:
label:
The label of the particles.
x:
The initial positions, one row per particle or shared.
theta:
The initial headings, 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.active_matter import CohesiveParticleElement
>>> CohesiveParticleElement('bird', theta=1.0).theta
1.0
"""
visual = HEADINGS
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 PropulsionStepInteraction(Interaction):
"""Step `x(t + 1) = x + speed (cos theta, sin theta)` of a position in the plane.
The step is along a heading, in discrete time.
The step reads the heading at the start of the step, as every map of the step does.
Args:
label:
The label of the interaction, unique among the components of its system.
target:
The path of the particle or the group whose positions move.
speed:
The distance moved in one step.
state:
The name of the vector state that holds the position.
heading:
The name of the state that holds the heading.
Examples:
>>> from types import SimpleNamespace
>>> from sage.all__sagemath_symbolics import var, vector
>>> from skcomplex.systems.physics.common.particles import PropulsionStepInteraction
>>> target = SimpleNamespace(x=vector([var('x1'), var('x2')]), theta=var('theta'))
>>> step = PropulsionStepInteraction('propulsion', target='flock')
>>> [law.value for law in step.prepare(SimpleNamespace(target=target, speed=var('v'), t=var('t'))).laws_]
[(v*cos(theta) + x1, v*sin(theta) + x2)]
"""
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 step of the position along the heading.
Args:
context:
The namespace of the target, the time `t`, and the parameters.
"""
super()._prepare(context)
position = getattr(context.target, self.state)
self.laws_ = [UpdateLaw(position, position + self._step(context), increment=True)]
def _step(self, namespace: SimpleNamespace) -> FreeModuleElement:
"""Return the step `speed (cos theta, sin theta)`."""
heading = getattr(namespace.target, self.heading)
return namespace.speed * vector(SR, [cos(heading), sin(heading)])
class CohesiveAlignmentInteraction(Interaction):
"""Alignment with cohesion of a Grégoire-Chaté flock.
The particles align, repel close neighbours, and attract distant ones.
Every step the new heading is the direction of `alpha sum_j exp(i theta_j) + beta sum_j f(r_ij) e_ij + eta n_i xi`
over the `n_i` particles within the unit distance, where `e_ij` points from `i` to `j`, `xi` is a random unit
vector, and the force `f` is a strong repulsion below `r_c`, zero at `r_e`, and attractive up to `r_a`. It is a
school of fish or a flock of birds that stays together.
Args:
label:
The label of the interaction.
target:
The path of the particles, with the positions `x` and the headings `theta`.
eta:
The strength of the vectorial noise.
alpha:
The strength of the alignment.
beta:
The strength of the cohesion.
r_c:
The distance below which the neighbours repel strongly.
r_e:
The preferred distance.
r_a:
The distance of the strongest attraction.
Examples:
>>> from skcomplex.systems.physics.active_matter import CohesiveAlignmentInteraction
>>> CohesiveAlignmentInteraction('cohesion', target='flock').r_e
0.5
"""
law_classes = (UpdateLaw,)
def __init__(
self,
label: str,
*,
target: Role,
eta: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
alpha: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
beta: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
r_c: Annotated[float, RealSet.open(0, oo)] = CLOSE,
r_e: Annotated[float, RealSet.open(0, oo)] = 0.5,
r_a: Annotated[float, RealSet.open(0, oo)] = 0.8,
) -> None:
super().__init__(label)
self.target = target
self.eta = eta
self.alpha = alpha
self.beta = beta
self.r_c = r_c
self.r_e = r_e
self.r_a = r_a
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the update of the heading of the target by alignment, cohesion, and vectorial noise.
Args:
context:
The namespace of the target, the time `t`, and the parameters.
"""
super()._prepare(context)
s, member = context, context.target
near, others = RadiusNeighborhood(1.0, center=True), RadiusNeighborhood(1.0)
dx, dy = member.other.x[0] - member.x[0], member.other.x[1] - member.x[1]
distance = sqrt(dx**2 + dy**2)
force = where(distance < s.r_c, -REPULSION, (distance - s.r_e) / (4 * (s.r_a - s.r_e)))
count = neighbors(1, over=near)
angle = uniform('noise', -math.pi, math.pi)
along_x = (
s.alpha * neighbors(cos(member.other.theta), over=near)
+ s.beta * neighbors(force * dx / distance, over=others)
+ s.eta * count * cos(angle)
)
along_y = (
s.alpha * neighbors(sin(member.other.theta), over=near)
+ s.beta * neighbors(force * dy / distance, over=others)
+ s.eta * count * sin(angle)
)
self.laws_ = [UpdateLaw(member.theta, atan2(along_y, along_x))]