multi-cell simulations with pymunk 2d physics
Project description
multi-cell
Multi-cell simulations with 2D physics, built on process-bigraph and pymunk.
Demos
Goal
Provide a composable framework for simulating populations of growing, dividing cells with realistic 2D physics and shared chemical environments. Cells are modeled as capsule-shaped rigid bodies (pymunk segments) — or as multi-segment compound bodies that can flex and bend — that grow, divide, secrete particles, attach to surfaces, and interact physically through collisions and confinement. Concentration fields are modeled with a finite-difference diffusion process and coupled to cells through a per-tick exchange step. The framework uses the bigraph process architecture, making it easy to compose new cell behaviors, environmental structures, and analysis pipelines.
Default parameters are calibrated to E. coli proportions (~1 μm wide, ~2 μm at birth, ~4 μm at division, ~30–40 min doubling time).
Quick Start
git clone https://github.com/vivarium-collective/Viva-munk.git
cd Viva-munk
pip install -e .
# Run all experiments and open the HTML report
python -m multi_cell.experiments.test_suite
# Run without auto-opening the browser
python -m multi_cell.experiments.test_suite --no-open
Architecture
multi_cell/
pymunk_agent_type.py # Custom PymunkAgent type with optimized dispatch
types/
positive.py # PositiveFloat / PositiveArray / Concentration / SetFloat
processes/
multibody.py # PymunkProcess — 2D physics (rigid + bending cells)
grow_divide.py # GrowDivide — exponential growth, division, mutation
pressure.py # Pressure — vectorized per-cell mechanical pressure
diffusion_advection.py # DiffusionAdvection — 2D field diffusion + advection
cell_field_exchange.py # CellFieldExchange — cell ↔ field uptake/secretion
secrete_eps.py # SecreteEPS — EPS particle secretion
remove_crossing.py # RemoveCrossing — remove cells past a boundary
plots/
multibody_plots.py # GIF rendering with phylogeny / pressure coloring
# and concentration-field heatmap overlays
experiments/
test_suite.py # Experiment registry, runner, HTML report generator
Processes
- PymunkProcess (Process): Manages the pymunk 2D physics space. Handles three kinds of agents — rigid segment cells, multi-segment bending cells (compound bodies linked by pivot joints + damped rotary springs), and circular particles. Syncs state to bodies, steps the physics with sub-stepping, applies damping/jitter, optionally enforces adhesion to a wall, and emits position/velocity/polyline deltas.
- GrowDivide (Process): Per-cell exponential mass growth. Optionally gated by a Monod factor on a local nutrient (
local[nutrient_key]) and inhibited by mechanical pressure (exp(-pressure / pressure_k)). When mass exceeds a threshold, the cell divides into two daughters along its axis; daughters can inherit mutated parameters and (for bending cells) a fresh straight polyline so they don't inherit the mother's pose. - Pressure (Step): Computes a per-cell mechanical pressure proxy from cell-cell and cell-wall overlap depths (vectorized via numpy). Written back to each cell's
pressurefield for downstream consumers. - DiffusionAdvection (Process): Explicit FTCS diffusion + upwind advection on a 2D
map[mol_id → array]field, with ghost-layer boundary conditions (periodic / neumann / dirichlet / dirichlet_ghost). - CellFieldExchange (Process): Couples
pymunk_agentcells to a 2D field map. Each tick it samples each cell's local field concentration intocell.local, and applies each cell'scell.exchangeamounts back into the corresponding field bin (Δconcentration = Δamount / bin_volume). - SecreteEPS (Process): Per-cell EPS particle secretion at a rate proportional to cell mass. Particles are placed on the cell surface and added to the environment. Optionally gated to attached cells only.
- RemoveCrossing (Step): Removes any cell whose position exceeds a configurable x/y boundary. Used to model the flow channel in mother-machine experiments.
Custom types
pymunk_agent is a Node-subclass type with hand-optimized apply/reconcile/realize/check dispatch (no per-field plum dispatch on the hot path). It carries the geometric and physical state of one cell (mass, radius, length, location, velocity, …) plus optional fields used by downstream processes (polyline, attached, pressure, local, exchange).
The multi_cell.types.positive module provides minimal PositiveFloat, PositiveArray, Concentration, and SetFloat types for the field state, accumulator-with-clamp semantics adapted from spatio-flux.
Experiments
The current registry (multi_cell/experiments/test_suite.py):
- daughter_machine — single cell grows in an open chamber with an absorbing right wall.
- mother_machine — narrow dead-end channels (~1.5 μm wide); cells grow vertically and are removed when they reach the flow channel.
- with_particles — cells grow in a chamber seeded with passive particles; cells push and rearrange them.
- attachment — adhesin-bearing cells attach to the bottom surface via PivotJoints; adhesins split between daughters at division.
- glucose_growth — cells on a 2D glucose field.
DiffusionAdvectionspreads glucose,CellFieldExchangeapplies cell consumption every tick, andGrowDividegates rate by Monod kinetics. Cells stop dividing once their local patch is depleted. - bending_pressure — multi-segment bending capsules grow into a colony.
Pressurecomputes mechanical pressure from neighbor / wall contacts andGrowDivideappliesexp(-pressure / pressure_k)inhibition. Cells visibly bend AND slow. - chemotaxis — twelve non-growing cells run/tumble up a static exponential ligand gradient in a long chamber. Each cell maintains a memory of its local concentration and modulates tumble rate as
λ = λ₀ · exp(-k · dc/dt). - inclusion_bodies — a colony grows while each cell accumulates an inclusion-body aggregate (size in nm, logistic growth toward an 800 nm plateau). Aggregation inhibits growth; at division the IB is transferred entirely to one daughter so the IB-free sibling out-grows the laden one. Cells are soft bending capsules and a
Pressurestep adds a second slowdown as the colony packs. Colored by IB size.
test_suite.py runs each one, captures a GIF, a bigraph composition viz, and a serialized state JSON, and generates an HTML report (out/report.html) with timing, cell counts, descriptions, and embedded media.
Dependencies
- process-bigraph — bigraph process framework
- bigraph-schema — typed schemas
- bigraph-viz — bigraph visualization
- pymunk — 2D physics engine
- numpy, scipy, matplotlib, pillow
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file multi_cell-0.0.1.tar.gz.
File metadata
- Download URL: multi_cell-0.0.1.tar.gz
- Upload date:
- Size: 72.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
256aa18fcd1f32dc904265fb3539263b7297b85220a238ef6682f68bb751e17a
|
|
| MD5 |
e3fda7c0ec1397a1f5f842d5d3155bd9
|
|
| BLAKE2b-256 |
a4fd3ae34767ec5670bdb209a07932b3b0e7274b58558bab1c878ab1d24091b3
|
File details
Details for the file multi_cell-0.0.1-py3-none-any.whl.
File metadata
- Download URL: multi_cell-0.0.1-py3-none-any.whl
- Upload date:
- Size: 79.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee9c08c4aa9e34771f1fe6a14687afecf822b32ca7fa908489fbe8eb45e19ebe
|
|
| MD5 |
945dcfad551cfd86221a99456f0564f3
|
|
| BLAKE2b-256 |
c75f34d81cd4bdbc7034b0cd1f6b697089e62da18d5bb21bba525cae0bf0c034
|