Run-and-tumble
Run-and-tumble agents in a periodic square.
- Class:
RunAndTumbleSystem - Import:
from skcomplex.systems.physics.active_matter import RunAndTumbleSystem - Field:
physics/active_matter - Topics:
active matter,agents,bacteria,self-propulsion - References:
- Berg 2004
- Bechinger et al. 2016
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 RunAndTumbleSystem
trajectory = RunAndTumbleSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_agents |
100 |
speed |
0.1 |
tumble_rate |
0.1 |
size |
10.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 | discrete |
| Space | continuous |
| Laws | update |
| Randomness | stochastic |
| Domains | real |
| Structure | variable membership |
Class
The source of the class shows how it builds its components from its model parameters.
class RunAndTumbleSystem(BaseSystem):
"""Run-and-tumble agents in a periodic square.
The agents diffuse with `v^2 (1 + q) / (4 (1 - q))` per step.
The persistence `q = exp(-tumble_rate)` is the probability that an agent keeps its direction for one step. The
system has the agent group `bacteria`.
Args:
n_agents:
The number of agents.
speed:
The distance run per step.
tumble_rate:
The rate of tumbles per step.
size:
The side of the periodic square.
random_state:
The seed or generator of the initial positions and directions.
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 RunAndTumbleSystem
>>> RunAndTumbleSystem(10).prepare().system_.elements[0].size
10
"""
field = 'physics/active_matter'
topics = ('active matter', 'agents', 'bacteria', 'self-propulsion')
references = ('Berg 2004', 'Bechinger et al. 2016')
until = 100
dt = 1
def __init__(
self,
n_agents: int = 100,
*,
speed: float = 0.1,
tumble_rate: float = 0.1,
size: float = 10.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_agents = n_agents
self.speed = speed
self.tumble_rate = tumble_rate
self.size = size
self.random_state = random_state
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of run-and-tumble agents in a periodic square.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
positions, angles = ActiveBrownianParticlesSystem._get_swimmers(self.n_agents, self.size, self.random_state)
bacteria = TumblerElement(
'bacteria',
x=positions[:, 0],
y=positions[:, 1],
theta=angles,
speed=self.speed,
tumble_rate=self.tumble_rate,
width=self.size,
size=self.n_agents,
space=ContinuousSpace(extent=(self.size, self.size), boundary='periodic'),
)
run = RunAndTumbleInteraction('run', target='bacteria')
self.system_ = System(self.label, elements=[bacteria], interactions=[run])
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
TumblerElement, fromElement
Interactions:
RunAndTumbleInteraction, fromInteraction
class TumblerElement(Element):
"""Run-and-tumble agents at the points `x` and `y` of a periodic square, each with its direction `theta`.
`RunAndTumbleInteraction` runs every agent straight and tumbles it to a random direction, as bacteria such as E.
coli do. The direction wraps around the circle.
Args:
label:
The label of the agent or group.
x:
The initial first coordinate, one per agent or shared.
y:
The initial second coordinate, one per agent or shared.
theta:
The initial direction, one per agent or shared.
speed:
The distance run per step.
tumble_rate:
The rate of tumbles per step.
width:
The side of the periodic square.
size:
The number of agents, or `None` for one.
space:
The continuous space of the agents, or `None` for none.
Examples:
>>> from skcomplex.systems.physics.active_matter import TumblerElement
>>> TumblerElement('bacterium').speed
0.1
"""
visual = VisualSpec('agents', {'color_by': 'theta'})
def __init__(
self,
label: str,
*,
x: object = 0.0,
y: object = 0.0,
theta: Annotated[object, Period(2 * math.pi)] = 0.0,
speed: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
tumble_rate: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
width: Annotated[float, RealSet.open(0, oo)] = 10.0,
size: int | None = None,
space: BaseSpace | None = None,
) -> None:
super().__init__(label, size=size, space=space)
self.x = x
self.y = y
self.theta = theta
self.speed = speed
self.tumble_rate = tumble_rate
self.width = width
class RunAndTumbleInteraction(Interaction):
"""Run of every agent along its direction, then a tumble to a random direction.
Every step the agent moves `speed` along its direction in a periodic square, then tumbles with probability
`1 - exp(-tumble_rate)` to a direction drawn uniformly, as bacteria swim and robots search.
Args:
label:
The label of the interaction.
target:
The agents, with the points `x` and `y`, the direction `theta`, and the parameters of the run.
Examples:
>>> from skcomplex.systems.physics.active_matter import RunAndTumbleInteraction
>>> RunAndTumbleInteraction('run', target='bacteria').target
'bacteria'
"""
law_classes = (UpdateLaw,)
draws = True
def __init__(self, label: str, *, target: Role) -> None:
super().__init__(label)
self.target = target
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the run and tumble of every agent as the rule.
Args:
context:
The namespace of the target, the time `t`, and the parameters.
"""
super()._prepare(context)
self.laws_ = [UpdateLaw(context.target, self._run_and_tumble)]
def _run_and_tumble(self, agent: TumblerView, system: SystemView) -> None:
"""Run one step along the direction, then tumble at random.
Args:
agent:
The agent.
system:
The system.
"""
agent.x = float(np.mod(agent.x + agent.speed * math.cos(agent.theta), agent.width))
agent.y = float(np.mod(agent.y + agent.speed * math.sin(agent.theta), agent.width))
if system.rng.random() < -math.expm1(-agent.tumble_rate):
agent.theta = float(system.rng.uniform(0.0, 2 * math.pi))