lmcell skill: run agent-based cell simulations from LMScript
Use this page to write, run and verify an lmcell program on the first try. Every command and output below was produced with lmscript 0.2.482 and the released lmcell 0.1.4 on Linux.
Purpose
lmcell simulates point-agent cells in a 2D voxel grid: stochastic division and apoptosis, persistent random walks, chemotaxis, substrate diffusion with decay, uptake and secretion, and Hill rules that make rates depend on a substrate or on crowding. You describe a simulation with builder calls inside an _lmcode_ block, run it once, check numbers with assertions and save matrices as CSV. PhysiCell and BioFVM do the computing; lmcell draws nothing itself.
When to use it
- Use it for population-level questions about tissue: growth and death curves, nutrient or drug penetration, necrotic cores, wound closure, clonal selection, contact inhibition, chemotactic drift.
- Use it when a result must be reproducible from a seed and checkable against a closed form.
- Do not use it for cell mechanics (adhesion, repulsion, volume growth), cell-cell contact such as killing, intracellular networks, 3D domains, or PhysiCell XML and rules-file import: none of these exist in the host.
- For well-mixed reaction kinetics without space, a reaction-network host is the better fit; for charts, pass the CSV files to a plotting host such as lmplot.
Install
npm install -g @lmctl-ai/lmctl
lmscript --version
Nothing else is installed by hand. A block that starts with cli="lmcell" install="github:lmctl-ai/lmcell@latest" makes lmscript download the release binary into its tool cache on the first real run and read the embedded schema from it. Linux binaries require dynamic glibc 2.35 or newer. lmscript --check needs the schema in the cache, so do one real run before the first check on a new machine. If another program named lmcell is first on PATH, lmscript picks it and schema discovery fails; remove it from PATH.
Commands
| Command | Use |
|---|---|
| lmscript FILE.lms | Compile the block, start the host, return the result envelope to the script |
| lmscript --check FILE.lms | Compile and type-check the block against the lmcell schema without running |
| lmscript --dry-run FILE.lms | Print the plan; the host run is simulated, no simulation executes and no CSV is written |
| lmctl URL | Run an example script directly from a raw GitHub URL in the current folder |
| lmcell --version, --notices | Version line and embedded license notices |
| lmcell --lmcode-schema --json | Embedded schema JSON |
| lmcell --lmcode-signatures | All callable signatures as text, standard library included |
| lmcell [--dry-run] --lmcode FILE.dslb ... | The host protocol that run_lmcode drives; rarely typed by hand |
Inside a block the build order is fixed by the model: create the simulation with cell.sim, set domain before any scatter or seed_disk placement, declare substrates before edge, declare cell types before seeding them, then call run exactly once. After run the builders are frozen.
Inputs and outputs
- Input is the block text only. There are no input files, XML settings or runtime data.
- Outer values enter a block through ${expr} interpolation. A whole number arrives as an int, so convert with float(${n}) where the API needs a float.
- Numeric literals passed to float parameters need a decimal point: write 2.0, not 2. Counts, seeds and indexes are ints.
- Outputs are CSV files written by matrix.save_csv(path): no header, comma separated, LF line ends, 17 significant digits. Paths are relative to the working directory; the parent directory must exist.
- run_lmcode returns a map. On success ok is true and result holds what the block set with lmcode_return_set or lmcode_return_put (null otherwise). On failure ok is false with phase, code, message, line and column.
- cell.print writes to stderr, as does the solver banner printed by BioFVM. stdout of the host is reserved for the result frame.
| Matrix | Columns | Rows |
|---|---|---|
| result.counts() | time, live | Every step, including time 0 and the exact end time |
| result.type_counts() | time, then live cells per type in declaration order | Every step |
| result.cells() | x, y, type_index, alive | One per cell at the end; apoptotic cells stay with alive 0 until cleared |
| result.profiles() | x, y, then one column per substrate | One per voxel at the end, x varying fastest |
| result.history() | time, x, y, type_index, alive | One per cell per recorded time; needs record |
| result.profile_history() | time, x, y, substrates | One per voxel per recorded time; needs record |
Safety rules
- Always test r.ok after run_lmcode. A failed simulation does not abort the outer script by itself: the script exits 0 unless you call require or assert on r.ok.
- Run one simulation per block. A second run in the same host process fails with the message lmcell supports one PhysiCell simulation per process. For sweeps, put the block in an lmscript fn and call it once per case.
- Keep every rate times the step at or below 0.1, or the run stops with rate*dt must not exceed 0.1; reduce dt.
- save_csv overwrites existing files without asking. Write into a build directory and create it first with mkdir in the outer script.
- The standard file_write, file_append and file_read functions are unrestricted byte file access relative to the working directory; treat a block from an untrusted source like any other program.
- Memory is capped at 4096 MiB per program by default. Raise it deliberately with LMCELL_MAX_MEMORY_MB rather than looping over result.counts() copies, since every matrix accessor call copies the table and objects live until the process exits.
- Do not compare stochastic trajectories across operating systems byte for byte. A fixed seed gives byte-identical CSV on the same platform; across platforms compare statistics with a tolerance.
- State units in comments. lmcell assumes micrometers and minutes and performs no unit conversion; concentration units are yours.
Worked examples
A tumour that starves in the middle
Oxygen is held at 1 on every edge and consumed by cells. Two rule_toward rules make cells divide when fed and die when starved. The run records snapshots every 30 minutes.
prog = _lmcode_ cli="lmcell" install="github:lmctl-ai/lmcell@latest";
let s = cell.sim(7).domain(-200.0, 200.0, -200.0, 200.0, 20.0);
s.substrate("oxygen", 2000.0, 0.0, 1.0, 1.0);
let t = s.cell_type("tumour").radius(8.0).exchange("oxygen", 0.5, 0.0);
t.rule_toward("oxygen", "cycle entry", 0.002, 0.4, 2.0);
t.rule_toward("oxygen", "apoptosis", 0.0, 0.15, 4.0).death(0.004);
let r = s.seed_disk("tumour", 1000, 0.0, 0.0, 120.0).record(30.0).run(360.0, 0.1);
r.cells().save_csv("cells.csv");
r.history().save_csv("history.csv");
r.profiles().save_csv("oxygen.csv");
cell.print("live cells: " + str(r.live()));
lmcode_return_put("live", r.live());
lmcode_return_put("substrate", r.substrate(0));
lmcode_return_put("history_rows", r.history().rows());
end _lmcode_
r = run_lmcode(prog);
println(json_stringify(r));
require(r.ok, "tumour failed");
lmscript tumour.lms 2>/dev/null
{"ok":true,"result":{"live":935,"substrate":"oxygen","history_rows":14327}}
The run wrote cells.csv (1210 rows, live and apoptotic cells), history.csv (14327 rows) and oxygen.csv (400 rows, one per voxel of the 20 by 20 grid). These numbers are from Linux x86_64; other platforms can differ in the stochastic counts.
A fixed-edge gradient with diffusion substeps
No cells: one substrate between a source edge and a sink edge relaxes to a straight line. diffusion_dt keeps the profile next to the fixed edges accurate while the outer step stays at 1 minute.
prog = _lmcode_ cli="lmcell" install="github:lmctl-ai/lmcell@latest";
let s = cell.sim(3).domain(0.0, 200.0, 0.0, 20.0, 10.0);
s.substrate("drug", 20.0, 0.0, 0.0, -1.0);
s.edge("drug", "xmin", 1.0).edge("drug", "xmax", 0.0).diffusion_dt(0.05);
let r = s.run(2000.0, 1.0);
let p = r.profiles();
cell.assert_true(p.cols() == 3);
cell.assert_close(p.get(0, 2), 1.0, 0.000001);
cell.assert_close(p.get(10, 2), 0.4737, 0.01);
p.save_csv("drug.csv");
lmcode_return_put("rows", p.rows());
lmcode_return_put("mid", p.get(10, 2));
end _lmcode_
r = run_lmcode(prog);
println(json_stringify(r));
require(r.ok, "gradient failed");
lmscript grad.lms 2>/dev/null
{"ok":true,"result":{"rows":40,"mid":0.4737000431196065}}
A parameter sweep, one host process per case
The block sits inside an lmscript function, so each call starts a fresh lmcell process. The closing end _lmcode_ line must start in column 1 even inside a function body.
fn run_case(name, permille) {
prog = _lmcode_ cli="lmcell" install="github:lmctl-ai/lmcell@latest";
let d = float(${permille}) / 1000.0;
let s = cell.sim(11);
s.cell_type("dying").death(d).radius(1.0);
let r = s.seed("dying", 5000, 0.0, 0.0).run(10.0, 0.05);
r.counts().save_csv(${"death_" + name + ".csv"});
lmcode_return_set(r.live());
end _lmcode_
r = run_lmcode(prog);
require(r.ok, json_stringify(r));
return r.result;
}
println("d=0.05 live " + str(run_case("slow", 50)));
println("d=0.20 live " + str(run_case("fast", 200)));
lmscript sweep.lms 2>/dev/null d=0.05 live 3004 d=0.20 live 689
The expected survivors are 5000·exp(−d·t): 3033 and 677 for these two rates, so both seeded runs sit within sampling noise.
Errors and exit codes
| Host exit | Phase in the envelope | Meaning | Fix |
|---|---|---|---|
| 0 | none | Every program loaded and ran | Nothing |
| 2 | cli | Bad flags, no file, contradictory dry-run metadata, or an invalid LMCELL_MAX_MEMORY_MB value; code CLI_USAGE | Use the documented flags; set the budget to a positive whole number of MiB |
| 3 | load or run | Unreadable or mismatched bytecode (for example LOAD_BAD_BYTECODE), a host validation error, a failed assertion or an exceeded memory budget (RUN_HOST) | Read message, line and column; fix the block |
| 4 | app | Application or output failure reported by the program | Read message |
lmscript itself exits 2 for compile errors such as an unknown method or a missing cached schema. A run error does not change the exit code of lmscript: the envelope carries it. These are real failure envelopes:
lmscript fail.lms
dsl line 4:35: rate*dt must not exceed 0.1; reduce dt
{"ok":false,"phase":"run","code":"RUN_HOST","message":"dsl line 4:35: rate*dt must not exceed 0.1; reduce dt","line":4,"column":35}
lmscript asrt.lms
dsl line 2:6: assert_close failed: 2.7182818284590451 vs 2.5
{"ok":false,"phase":"run","code":"RUN_HOST","message":"dsl line 2:6: assert_close failed: 2.7182818284590451 vs 2.5","line":2,"column":6}
| Message | Cause and fix |
|---|---|
| lmcell supports one PhysiCell simulation per process | Second run in one block; split into one block per simulation |
| simulation already run; builders are frozen | A builder call after run; configure everything first |
| rate*dt must not exceed 0.1; reduce dt | Step too coarse for a cycle or death rate; lower max_dt |
| domain lengths must be integer multiples of voxel size | Adjust domain bounds or spacing |
| placement region must lie inside the domain; set domain(...) first | Call domain before scatter or seed_disk and keep the region inside it |
| unknown cell type, unknown edge substrate, duplicate substrate, duplicate cell type | Declare each name once, before it is used |
| exchange for substrate ... is already set on cell type ... | Call exchange once per substrate and type |
| unsupported rule target | Use cycle entry, apoptosis or migration speed |
| domain/time resource limit exceeded, cell population limit exceeded | Shrink the grid, steps or population; see the limits in the reference |
| memory budget exceeded ... (set LMCELL_MAX_MEMORY_MB to raise it) | Reduce the model or raise the budget |
| No cached host schema for "lmcell" | From lmscript --check before any real run; run the script once with the install attribute |
How to verify
- Compile: lmscript --check FILE.lms exits 0.
- Run and require r.ok in the script, so a failed simulation fails the script.
- Assert a known answer inside the block with cell.assert_close: an exponential for pure birth or death, a cosh or linear profile for diffusion, a carrying capacity for crowding. cell.eval computes the expected value from an expression string.
- Check shapes: counts has duration/step + 1 rows; profiles has one row per voxel and 2 + substrates columns.
- Rerun with the same seed and compare file hashes: the CSV must be byte-identical on the same platform.
lmscript --check colony.lms
lmscript colony.lms
sha256sum counts.csv
lmscript colony.lms
sha256sum counts.csv
See the guide for the model and its numerical limits and the reference for every signature.