lmcell guide: the model, its numerics and its limits
This guide explains what an lmcell simulation is, how the host is built, how a program flows from an _lmcode_ block to CSV files, and where the model stops. For commands and copy-ready examples see the skill page; for exact signatures see the reference.
Overview
lmcell is a scripting-only multicellular simulation host. A program builds one simulation in memory with chained builder calls, runs it once, and reads the outcome as numeric matrices. The computing core is serial PhysiCell 1.14.2 with BioFVM, linked statically into a single executable; no XML configuration, kernels or runtime data files are involved. The host follows the common lmcode protocol, so lmscript compiles the block against the schema embedded in the binary, hands the bytecode to the host and receives one result envelope.
The design goal is checkable science: each of the 16 repository examples asserts a known answer (a closed form, a threshold or a conservation law) before it draws anything, and a wrong number stops the run.
Architecture
| Layer | What it does |
|---|---|
| lmscript | Compiles the _lmcode_ block against the lmcell schema, downloads the release named by install, starts the host through run_lmcode and returns the envelope |
| lmcell host | Registers the cell context and the Sim, CellType, Result and Matrix types, validates every argument, enforces object and memory limits, writes CSV |
| Coordinator | lmcell's own time loop: applies rules, asks PhysiCell for cycle, death and motility decisions, places daughters, moves cells, reflects them at walls |
| PhysiCell | Cell objects, the single-phase live cycle, stochastic death checks, apoptosis clearance and the motility vector update |
| BioFVM | Implicit 2D diffusion and decay solver and cell secretion and uptake |
| lmcode_std_v3 | Standard library available in every block: strings, math, collections, Json, files and the result sink |
OpenMP is disabled through a small serial compatibility header, so runs are single threaded and deterministic for a seed on a given platform. PhysiCell and BioFVM keep process-wide state, which is why one host process can run exactly one simulation.
Workflow
- Create the builder with cell.sim(seed). The seed drives both the placement generator and the stochastic cell decisions.
- Set the domain. Lengths must be whole multiples of the voxel size. Without a domain call the grid is -100..100 on both axes with 20 micrometer voxels.
- Declare substrates, then optional per-side edge values and a diffusion substep.
- Declare cell types and chain their phenotype: cycle, death, radius, motility, chemotaxis, exchange and rules.
- Place cells with seed, scatter or seed_disk.
- Optionally request snapshots with record.
- Call run(duration, max_dt). This validates the whole configuration, freezes the builders and returns an immutable Result.
- Pull matrices from the result, assert on values, save CSV, and return summary values with lmcode_return_put.
- In the outer script, test r.ok and pass the CSV files to a plotting host or to your own analysis.
Configuration errors are detected before any native global state is touched, so a typo in a rule target fails cleanly. A failure during native initialization or stepping consumes the process: the simulation cannot be retried in the same block.
Domain model
Space, time and units
The domain is a rectangle of square voxels, one voxel thick. Lengths are micrometers and times are minutes; rates are per minute and diffusion coefficients are square micrometers per minute. Concentration units are whatever the caller chooses. Nothing is converted automatically.
The requested max_dt is turned into a constant actual step, duration divided by the ceiling of duration over max_dt, so the final step is never a partial one. The counts table therefore has that many steps plus one row.
Cells are point agents
A cell has a position, a type, a radius and an alive flag. Its volume is the sphere volume 4·π·r³/3 even though the domain is 2D, and that volume divided by the voxel volume scales its exchange with the substrate fields. Division places a daughter at the same position. Cells do not push, pull or adhere to each other and do not grow in volume; they reflect at the domain boundary. Crowding can influence behavior only through the neighbors signal.
Apoptotic cells stay in the cell table with alive 0 until PhysiCell's clearance time, so the cells matrix can have more rows than the live count.
Substrates
Each substrate is a field with a uniform initial value, a diffusion coefficient and a decay rate. The boundary argument is either -1 for no flux on all sides or a value at which every edge voxel center is held. edge overrides one side at a time; the global boundary applies first, then edges in call order, so the last call wins at a corner. A domain that is a single row of voxels has only x edges.
Every step the cells first secrete and take up, then BioFVM advances diffusion and decay. Secretion relaxes a voxel toward saturation 1.
Motility and chemotaxis
motility(speed, persistence_time) gives a persistent random walk. With chemotaxis(substrate, bias) each new heading is the normalized blend of a random direction, weighted 1 − bias, and the unit gradient of the substrate in the cell's voxel, weighted bias. Bias 0 is an unbiased walk and bias 1 follows the gradient exactly; a zero gradient falls back to the unbiased walk. In a linear gradient the mean drift speed is 0, 2v/π and v for bias 0, 1/2 and 1.
Hill rules
A rule reads a signal s and computes the Hill response H = s^n / (K^n + s^n) from half_max K and hill n. The signal is a declared substrate, sampled in the cell's voxel, or the reserved name neighbors: the number of other live cells in the same voxel, counted once at the start of the step. A substrate may not be named neighbors.
| Form | Effect on the target rate | Note |
|---|---|---|
| rule with effect increases | rate × (1 + H) | At most doubles the base rate; cannot lift a zero base rate |
| rule with effect decreases | rate × (1 − H) | Switches the rate off at saturation |
| rule_toward | rate + (saturation − rate) × H | Reaches any nonnegative rate, also from a zero base |
Targets are cycle entry (the division rate), apoptosis (the death rate) and migration speed. Several rules on one type apply in call order, each starting from the result of the previous one, and every step starts again from the type's base rates. This is a builder-level subset of the PhysiCell hypothesis rules grammar, not an importer for rules files.
Results and matrices
run returns a Result that never changes. Each accessor such as counts or profiles copies a table into a new Matrix; the copy is independent of the result. Matrices are zero-based and are saved without a header. Grid rows follow BioFVM ordering with x varying fastest. The record interval produces snapshots at time 0 and at the first step at or after each multiple of the interval.
Numerics and accuracy
- Stochastic rates: division and death are per-step probabilities, so rate × step must stay at or below 0.1; the host refuses coarser steps.
- Pure birth has mean N0·exp(r·t) and pure death N0·exp(−d·t); the repository tests start with 10000 agents and compare with three standard errors plus a small-step allowance.
- Diffusion uses BioFVM's implicit locally one-dimensional solver. It re-applies fixed edges between its split sweeps, so a profile next to a fixed edge sits low by roughly 0.05·D·dt/h² relative, about 5 percent at D·dt/h² = 1. Strong uptake needs uptake × step much smaller than 1.
- diffusion_dt refines only the exchange and diffusion substeps, to step divided by the ceiling of step over dt, without slowing the cell step. Use it when a fixed-edge or uptake-dominated field matters.
- Placements from scatter and seed_disk come from a portable splitmix64 stream derived from the seed and are the same on every platform. The stochastic trajectory itself uses standard-library distributions and can differ between platforms, so compare statistics, not paths, across machines.
- CSV output is written in binary mode with the classic locale and 17 significant digits, so a rerun with the same seed on the same platform is byte-identical.
Patterns from the examples
| Question | Building blocks | Known answer checked |
|---|---|---|
| Necrotic core of a spheroid | Fixed nutrient edges, exchange uptake, two rule_toward rules, seed_disk, record | Core live fraction below 0.45, rim above 0.8 |
| Drug penetration from a vessel | edge on xmin, decay, diffusion_dt | cosh profile with length √(D/k) |
| Contact inhibition | rule_toward on neighbors for cycle entry, scatter | Carrying capacity 1 + K·√(r/d − 1) per voxel |
| Chemotactic drift | Linear gradient between fixed edges, motility, chemotaxis | Drift 0, 2v/π and v |
| Clonal selection under therapy | Two cell types, type_counts, live_of, two host processes | Each clone grows as exp((b − d)·t) |
| Secreted signal | exchange with secretion and uptake | Voxel relaxes toward S/(S + U) |
Sweeps over a parameter use one block inside an lmscript function with interpolated parameters, one host process per case. The block language has no arrays of host objects, callbacks or ternary operator; loops and if statements are available, and the standard collections can hold numbers and strings.
Limits
| Limit | Value |
|---|---|
| Simulations per host process | 1 |
| Cell types, substrates | 32 each |
| Rules | 1000 |
| Cells (seeded and total population) | 100000 |
| Voxels, cell steps | 1000000 each |
| Voxels × steps × diffusion substeps | 200 million |
| Recorded history values, recorded profile values | 20 million each |
| Host objects per kind (Sim, CellType, Result, Matrix) | 10000 |
| Total memory | 4096 MiB by default, LMCELL_MAX_MEMORY_MB to change |
Outside the model entirely: 3D domains and arbitrary geometry, mechanical interactions and adhesion, continuous volume growth, cell-cell contact such as killing or phagocytosis, intracellular models, PhysiCell XML or rules-file import, checkpoints and MultiCellDS exchange. lmcell is a research modeling host with documented units and step effects; it does not claim a calibrated tissue model from default parameters.
Building from source
scripts/sync-deps.sh
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --parallel 4
./test.sh
The build needs CMake 3.24 or newer, a C++17 compiler, git and Python 3; the tests also need lmscript. Dependencies are pinned in deps.lock and the lmcode-cpp SDK in sdk.lock. The host is MIT licensed; PhysiCell and BioFVM are BSD-3-Clause, and lmcell --notices prints all notices.
Where to go next
- Skill for commands, safety rules and verified examples.
- Reference for every signature, matrix layout and error.
- Repository examples for the 16 gallery programs.