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

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

CommandUse
lmscript FILE.lmsCompile the block, start the host, return the result envelope to the script
lmscript --check FILE.lmsCompile and type-check the block against the lmcell schema without running
lmscript --dry-run FILE.lmsPrint the plan; the host run is simulated, no simulation executes and no CSV is written
lmctl URLRun an example script directly from a raw GitHub URL in the current folder
lmcell --version, --noticesVersion line and embedded license notices
lmcell --lmcode-schema --jsonEmbedded schema JSON
lmcell --lmcode-signaturesAll 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

MatrixColumnsRows
result.counts()time, liveEvery step, including time 0 and the exact end time
result.type_counts()time, then live cells per type in declaration orderEvery step
result.cells()x, y, type_index, aliveOne per cell at the end; apoptotic cells stay with alive 0 until cleared
result.profiles()x, y, then one column per substrateOne per voxel at the end, x varying fastest
result.history()time, x, y, type_index, aliveOne per cell per recorded time; needs record
result.profile_history()time, x, y, substratesOne per voxel per recorded time; needs record

Safety rules

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 exitPhase in the envelopeMeaningFix
0noneEvery program loaded and ranNothing
2cliBad flags, no file, contradictory dry-run metadata, or an invalid LMCELL_MAX_MEMORY_MB value; code CLI_USAGEUse the documented flags; set the budget to a positive whole number of MiB
3load or runUnreadable 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
4appApplication or output failure reported by the programRead 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}
MessageCause and fix
lmcell supports one PhysiCell simulation per processSecond run in one block; split into one block per simulation
simulation already run; builders are frozenA builder call after run; configure everything first
rate*dt must not exceed 0.1; reduce dtStep too coarse for a cycle or death rate; lower max_dt
domain lengths must be integer multiples of voxel sizeAdjust domain bounds or spacing
placement region must lie inside the domain; set domain(...) firstCall domain before scatter or seed_disk and keep the region inside it
unknown cell type, unknown edge substrate, duplicate substrate, duplicate cell typeDeclare 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 targetUse cycle entry, apoptosis or migration speed
domain/time resource limit exceeded, cell population limit exceededShrink 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

  1. Compile: lmscript --check FILE.lms exits 0.
  2. Run and require r.ok in the script, so a failed simulation fails the script.
  3. 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.
  4. Check shapes: counts has duration/step + 1 rows; profiles has one row per voxel and 2 + substrates columns.
  5. 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.