lmfluid skill

Use lmfluid from an lmscript program to solve small finite-element problems (Poisson, heat, linear elasticity) and 2D lattice-Boltzmann flows, check the numbers against known answers and export CSV. This page is the operating procedure for an agent; the model behind it is explained in the guide.

Purpose

lmfluid is an lmcode host: a native binary that executes compiled _lmcode_ blocks. Its schema exposes one context object, fluid, from which a block builds meshes, finite-element problems, lattice-Boltzmann simulations and result matrices. The finite-element part is serial MFEM with H1 elements of order 1 to 3; the flow part is an original D2Q9 BGK solver in lattice units.

When to use it

Install

npm install -g @lmctl-ai/lmctl

That installs lmscript and lmctl. The host itself needs no manual install: a block that declares install="github:lmctl-ai/lmfluid@latest" makes lmscript download the release binary (0.2.2 at the time of writing) into its tool cache on the first real run. Linux binaries need dynamic glibc 2.35 or newer; macOS arm64 and x64 and Windows x64 need no extra runtime libraries. Set GITHUB_TOKEN when running many scripts, because resolving @latest uses the rate-limited GitHub API. The repository states lmscript 0.2.460 as its tested compiler; the examples on this page were run with lmscript 0.2.482.

Commands

# run a script (compiles each _lmcode_ block, runs it in lmfluid)
lmscript model.lms

# compile-check only; needs the host schema cached by one earlier real run
lmscript --check model.lms

# run a published example straight from the repository
lmctl https://raw.githubusercontent.com/lmctl-ai/lmfluid/main/examples/quickstart.lms

# host protocol, normally used by lmscript rather than by you
lmfluid --version
lmfluid --notices
lmfluid --lmcode-schema --json
lmfluid --lmcode-signatures
lmfluid --lmcode FILE.dslb
lmfluid --dry-run --lmcode FILE.dslb

# raise the memory budget (MiB, default 4096)
LMFLUID_MAX_MEMORY_MB=16384 lmscript big.lms

Inside a script the block form is fixed: assign the block to a variable, end it with end _lmcode_, then call run_lmcode on the variable and test the ok field of what it returns.

Inputs and outputs

Safety rules

Worked examples

All output below was produced with lmscript 0.2.482 and the released lmfluid 0.2.2 on Linux x64.

Poisson with an exact answer

The problem -Δu = -4 with u = x² + y² on every edge has that same polynomial as its solution, which order 2 elements reproduce.

prog = _lmcode_ cli="lmfluid" install="github:lmctl-ai/lmfluid@latest";
let field = fluid.poisson(fluid.rect(12, 12, 1.0, 1.0), 2).source("-4").dirichlet(0, "x*x+y*y").solve();
fluid.assert_close(field.value(0.5, 0.5), 0.5, 0.000001);
fluid.assert_close(field.l2_error("x*x+y*y"), 0.0, 0.000001);
field.sample(41, 41).save_csv("poisson.csv");
lmcode_return_put("center", field.value(0.5, 0.5));
lmcode_return_put("dofs", field.dofs());
end _lmcode_
r = run_lmcode(prog);
println(json_stringify(r));
assert(r.ok);
lmscript poisson.lms
{"ok":true,"result":{"center":0.4999999999940105,"dofs":625}}

The file poisson.csv then has 1681 rows of x, y, z, u.

Heat eigenmode with a returned profile

prog = _lmcode_ cli="lmfluid" install="github:lmctl-ai/lmfluid@latest";
let mesh = fluid.rect(20, 20, 1.0, 1.0);
let problem = fluid.heat(mesh, 1).diffusion("1.0").initial("sin(pi*x)*sin(pi*y)").dirichlet(0, "0.0");
let field = problem.evolve(0.01, 0.001);
fluid.assert_close(field.value(0.5, 0.5), 0.8208687174155399, 0.003);
let samples = float_list();
samples.add(field.value(0.25, 0.5)).add(field.value(0.5, 0.5)).add(field.value(0.75, 0.5));
lmcode_return_put("time", field.time());
lmcode_return_put("profile", samples);
end _lmcode_
r = run_lmcode(prog);
println(json_stringify(r));
assert(r.ok);
lmscript heat.lms
{"ok":true,"result":{"time":0.01,"profile":[0.5813274444465232,0.8221211561090354,0.5813274444401473]}}

The exact centre value is exp(-2π²t) = 0.82087 at t = 0.01; the tolerance covers the backward Euler time error.

Elasticity patch test with a gradient

prog = _lmcode_ cli="lmfluid" install="github:lmctl-ai/lmfluid@latest";
let mesh = fluid.rect(8, 8, 1.0, 1.0);
let field = fluid.elasticity(mesh, 1).lame(1.0, 1.0).displacement(0, "x", "2.0*y", "0.0").solve();
fluid.assert_close(field.l2_error_vector("x", "2.0*y", "0.0"), 0.0, 0.02);
lmcode_return_put("components", field.components());
lmcode_return_put("ux", field.value(0.5, 0.5, 0.0, 0));
lmcode_return_put("duy_dy", field.gradient(0.5, 0.5, 0.0, 1, 1));
end _lmcode_
r = run_lmcode(prog);
println(json_stringify(r));
assert(r.ok);
lmscript beam.lms
{"ok":true,"result":{"components":2,"ux":0.499999999974519,"duy_dy":1.999999999965893}}

Body-force channel flow

prog = _lmcode_ cli="lmfluid" install="github:lmctl-ai/lmfluid@latest";
let flow = fluid.channel(24, 12, 0.1, 0.0001).run(2000);
let exact = 0.0001 * 12.0 * 12.0 / (8.0 * 0.1);
fluid.print("tau = " + str(flow.tau()));
lmcode_return_put("u_center", flow.velocity(12, 6, 0));
lmcode_return_put("u_exact_max", exact);
lmcode_return_put("mass", flow.mass());
lmcode_return_put("steps", flow.steps());
flow.sample().save_csv("channel.csv");
end _lmcode_
r = run_lmcode(prog);
println(json_stringify(r));
assert(r.ok);
lmscript channel.lms
tau = 0.8
{"ok":true,"result":{"u_center":0.01780998058205736,"u_exact_max":0.018000000000000002,"mass":287.99999999996584,"steps":2000}}

The value u_exact_max is the steady Poiseuille maximum gH²/8ν for H = 12, and the sampled cell near the centreline is within about 1 percent of it after 2000 steps. The tau line is fluid.print output on stderr.

Errors and exit codes

Host exitPhaseMeaningFix
0Every part loaded and ran (or was validated in dry mode)
2cliUsage or invocation metadata error, including an invalid LMFLUID_MAX_MEMORY_MB or --dry-run contradicting real-mode metadata; code CLI_USAGEUse only the documented flags; set the budget to a positive whole number of MiB
3load or runBytecode could not be read or verified, or a host call failed: failed assertion, invalid argument, limit, unstable flow, memory budgetRead message, line and column; correct the model
4appApplication or output contract failure reported by the SDK result frameCheck what the block returns with the lmcode_return functions

Through run_lmcode these arrive as a map rather than as a process exit. A finite-element order of 4 gives:

prog = _lmcode_ cli="lmfluid" install="github:lmctl-ai/lmfluid@latest";
let field = fluid.poisson(fluid.rect(4, 4, 1.0, 1.0), 4).solve();
end _lmcode_
r = run_lmcode(prog);
println(json_stringify(r));
lmscript fail.lms
dsl line 2:19: FEM order outside allowed range
{"ok":false,"phase":"run","code":"RUN_HOST","message":"dsl line 2:19: FEM order outside allowed range","line":2,"column":19}

A failed assertion has the same shape, with a message such as dsl line 2:7: assert_close failed: 1 vs 2. Calling the host directly shows the frame and the exit code:

lmfluid --lmcode missing.dslb
LMCODE_RETURN_BEGIN
{"ok":false,"phase":"load","code":"LOAD_BAD_BYTECODE","message":"cannot read missing.dslb"}
LMCODE_RETURN_END

That command exits 3. An unknown flag prints the usage text and a CLI_USAGE frame and exits 2. Compile-time problems (unknown method, wrong argument type, no cached schema for --check) are reported by lmscript before the host starts, with exit 2 from lmscript.

How to verify

  1. Run lmscript --check model.lms after one real run has cached the schema; exit 0 means every block compiles against the host schema.
  2. Run the script and require r.ok. Print json_stringify(r) on failure to see phase, code, message, line and column.
  3. Assert a known answer inside the block: an exact solution through l2_error, a conservation law such as flow.mass, or a closed form such as the Poiseuille maximum.
  4. Refine once (more elements, higher order, more steps) and confirm the value moves toward the reference at the expected rate: L2 error orders 2, 3 and 4 for orders 1, 2 and 3 on smooth solutions.
  5. Rerun and compare CSV bytes: output is byte-identical on the same platform and agrees to rounding across platforms.
  6. Confirm the host with lmfluid --version and, when in doubt about a method, list lmfluid --lmcode-signatures.