f_nematic
Type: Generator (no texture inlet, three outlets: two tensors and a look) — temporal / has memoryStatus: Built through Phase 3 and benched (stages, whole chain and module, GPU against the NumPy mirror); Matt has looked live at the stroke direction (f_tf_hatch follows the director) and that the pattern keeps moving (2026-11-27). Dial ranges, the panel layout, the glyph and aniso_blur looks and the name are still open. The cost target is set (5 ms at 1080p, Matt 2026-11-27; see Cost). README row marked Unverified.
Reference written 2026-11-27 (Claude, offline) from src/f_nematic/ (definition and codeboxes), .specify/f_nematic/{spec,plan,tasks}.md, the mirror tests in tests/modules/nematic/ and the bench logs named below. Where this doc and the shipped module differ, the shipped module wins. "Observed" means a number from a NumPy run or a bench log; "inferred" means worked out and not run; nothing about a real consumer chain beyond the two live remarks above has been looked at.
What it does
An active nematic generator, solved on the GPU: a field of rodlike molecules (a liquid crystal) that push the fluid around them. The state is the Q-tensor (two real components, with the order parameter S going to zero at a defect). Comet-shaped +1/2 defects self-propel, defects nucleate and annihilate in pairs, and the pattern keeps changing with no input and never settles. It is the library's first emergent tensor source: until now the only tensor producer was f_tf_tensormap, which reads structure out of an image.
The outlets are f_ tensors (docs/f-reference/f_tensor_type.md): the raw tensor and the packed tensor at strength 1, plus a schlieren look (the classic crossed-polarizer liquid-crystal picture). f_tf_hatch, f_tf_glyph, f_tf_aniso_blur, f_tf_smooth, f_tf_sharpen and, through f_tf_axisflow, the vecfield consumers all get a demo source with nothing patched in. There is no vecfield outlet: an unoriented axis must not be forced into a signed vector (type doc rule).
A second source, Kinem, ignores the solver and draws an analytic field of 1 to 4 pairs of +1/2 and -1/2 defects moving on closed paths. It is deterministic and controllable, but saves no cost: the solver runs underneath in both modes.
Typical use: patch outlet 1 into f_tf_hatch or f_tf_glyph for strokes or marks that follow a living oriented field; outlet 3 alone for a schlieren-style texture.
The solver is a fixed 128 x 128 float32 periodic grid; the three outlets are render-size textures (the last stage follows the render size).
Signal Flow
One control inlet (messages and the draw bang, no texture), three outlets.
hold -> pass (in 1) seed -> pass (in 2)
hold -> rhs (in 2) seed -> rhs (in 3)
pass -> fx -> fy -> flow -> iy -> ix -> rhs -> fx2 -> fy2 -> upd -> iy2 -> ix2 -> nl (nl -> hold: the feedback edge)
nl -> enc (render resolution) -> outlets 1, 2, 3| Stage | Job |
|---|---|
hold | Identity. One-frame delay, the loop break |
seed | Deterministic arithmetic hash noise, two components (no sin()), changed by salt |
pass | Holds last frame's state, or replaces it with the quench (uniform noise, std 0.1 per component) when reseed = 1; also reseeds any texel that is exactly (0, 0) |
fx fy | Forward DFT of the two components (one texture carries two complex fields) |
flow | Stokes flow per Fourier bin: active force, divergence-free projection, divide by gamma + k^2 |
iy ix | Inverse DFT; ix keeps the real part and zaps non-finite values |
rhs | Real space: advection, corotation, strain, Landau term; outputs q + dt * rhs |
fx2 fy2 | Forward DFT of that |
upd | Per bin: divide by 1 + dt k^2, the implicit elastic term |
iy2 ix2 | Inverse DFT |
nl | Containment: non-finite zapped, each component clamped to +/-1.5. This is the state and the feedback edge |
enc | Render resolution: periodic bilinear upsample of q (or the analytic field), clamp ` |
The r draw bang goes to seed, pass and enc; the feedback and noise arrive on cold inlets, so the loop advances exactly once per rendered frame. rhs re-resolves the state from hold and seed exactly as pass does, instead of taking it from pass: a cord fan-out from pass has no guaranteed firing order in Max, so rhs could otherwise pair this frame's flow with last frame's state (a design reason from writing it, not an observed failure). For the same reason rhs outputs q + dt * rhs so upd needs no saved spectrum.
Eight separable DFT passes per frame (f_ks has four), each a 128-term loop per texel (inferred: about 1/8 the work of a 256 pass, as the loop is O(N) per texel; cost not measured, see Cost).
Outlets
| # | Content | Encoding | Neutral (bypass) |
|---|---|---|---|
| 1 | Raw tensor | (Jxx, Jxy * 0.5 + 0.5, Jyy, 1) at strength 1, J = 1/2 [[1 - c1, -c2], [-c2, 1 + c1]], c = q / max(1, abs q); trace 1, coherence = S | (0, 0.5, 0, 1), the zero tensor (not black: all zeros decode to a non-SPD phantom, f_tensor_type.md rule 5) |
| 2 | Packed tensor | (cos 2t * 0.5 + 0.5, sin 2t * 0.5 + 0.5, trace = 1, coherence = S) with (cos 2t, sin 2t) = -c / abs c (no atan2, abs c biased by 1e-7); angle-doubled, do not feed R, G to sign-sensitive consumers | (0.5, 0.5, 0, 0) |
| 3 | Schlieren look | (c2 cos 2p - c1 sin 2p)^2 = S^2 sin^2(2 (phi - p)) replicated to RGB, A = 1; p is Look | black, A = 1 |
Set a consumer's own strength dial to 1 (for f_tf_aniso_blur: its strength pairing note): the tensor is already unit scale, trace 1.
The dominant eigenvector of J is perpendicular to the director: the f_tf_tensormap convention (gradient across, structure along theta + 90 degrees), so a consumer that draws along the structure draws along the director. Checked on paper by Claude and in the mirror (trace 1, coherence = S, axis equals director, packed equals the eigen-decomposition; tests/logs/20261127-151135-offline.log); on the GPU the raw outlet decodes to trace 1 (3e-8), coherence = S (1.9e-6) and axis = director (3.8e-6 rad) (tests/logs/20261127-153236-bench.log); live, Matt, 2026-11-27: "Strokes run along the director" (which consumer and settings not given). The glyph, aniso_blur and core looks (spec N14, N15) were not reported and remain open.
Parameters
| Name | Type | Range | Default | Description |
|---|---|---|---|---|
zeta (Active) | float | 0 to 4 | 2 | Activity. Sustained above about 0.3 (observed, finite-size NumPy run, 64 box); defect density roughly proportional to it |
align (Align) | float | -1 to 1 | 0 | Flow alignment, lambda. 0: the sign of Active does not matter. Positive lowers the defect density, negative raises it (observed, NumPy) |
gamma (Friction) | float | 0.1 to 1 | 0.1 | Flow friction (screening length): low gives long swirls, more defects and a stronger flow; high gives a calmer, more ordered pattern. Observed (NumPy, zeta 2): late defect mean 56 / 48 / 21 and u_rms 1.75 / 1.0 / 0.44 at 0.1 / 0.3 / 1.0. The floor is 0.1 because at 0.05 the containment clamp is hit at the top step (observed) |
scale (Scale) | float | 48 to 192 | 64 | Domain size in core sizes; dx = scale / 128. Bigger: more, finer defects. Observed (NumPy, zeta 2, dt 0.2): means 31 / 56 / 237 / 567 defects at 48 / 64 / 128 / 192 |
rate (Rate) | float | 0 to 0.2 | 0.2 | Time step per frame. 0.2 is the ceiling Matt set 2026-11-27. The effective step is also capped by activity (see Algorithm) |
defects (Pairs) | int | 1 to 4 | 2 | Kinem mode: number of +1/2, -1/2 pairs |
orbit (Orbit) | float | -0.02 to 0.02 | 0.002 | Kinem mode: turns of the orbit phase per frame; 0 freezes the defects exactly. No pix cord: feeds a per-frame accumulator |
look (Look) | float | 0 to 180 | 0 | Polarizer angle of the schlieren outlet, degrees |
source (Source) | text button | Active / Kinem | Active | The solver, or the analytic field |
reset | button / message | Re-quenches from fresh noise; fires once on load (after 200 ms); also a reset message | ||
bypass | toggle | 0/1 | 0 | Neutral outlets (table above); the solver keeps running |
Internal, never set by hand: reseed, salt (reset logic), ph (orbit clock), bypass_gate, width, height (render size).
Reset logic as f_ks: button, message or load fires [t b b b]: restart the frame count, param reseed 1 to pass and rhs, count up salt into seed; reseed is held three draw bangs (never a timer) and the three sel 2 outlets release it.
Algorithm
Each frame, on q = (q1, q2) (units: core size 1, relaxation time 1, elastic constant and Landau coefficient 1, eta 1 — the spike's):
- Flow.
q-hat(forward DFT). Forcef = zeta div Q:fx = zeta i (kx q1 + ky q2),fy = zeta i (kx q2 - ky q1), with the operator wavenumbers zeroed at the Nyquist bin. Project out the compressive part,u-hat = f-hat / (gamma + k^2),k = 0bin zero. Inverse DFT:u. - Right-hand side, periodic central differences,
dx = L / 128:rhs1 = -(u . grad q1) + q1 (1 - |q|^2) - 2 om q2 + lam exx,rhs2 = -(u . grad q2) + q2 (1 - |q|^2) + 2 om q1 + lam exy, vorticityom = (d_x uy - d_y ux) / 2, strainexx = d_x ux,exy = (d_x uy + d_y ux) / 2. - Update.
q-new-hat = F[q + dt rhs] / (1 + dt k^2)(the elastic term implicit, the rest explicit). - Step.
dt = min(rate, 0.4 / (zeta (1 + |lam|))). The 0.4 constant is provisional: it sat at or under the usable step in all 24 cells of a (zeta, lambda) grid in the NumPy scan (observed, Phase 0, one state each), and exactly at it for zeta 4, lambda -1. At high activity the apparent speed saturates because the step shrinks. - Containment. Non-finite zapped, each component clamped to +/-1.5. This clamp is not inert at the 0.2 ceiling: NumPy runs reached
|q|max1.2 to 1.5 at dt 0.2 (observed), so it is part of the dynamics at the top of the range. - Emit. Upsample
qto render size (periodic bilinear), clamp|q| <= 1, emit the three outlets.
The quench: uniform noise, standard deviation 0.1 per component (half-width 0.1 sqrt 3); the NumPy spike used Gaussian noise and the two give the same defect statistics (observed, 3 seeds). Order develops within about 30 draws: mean coherence 0.38 at frame 30, 0.79 at frame 120, 0.77 at frame 600 (bench, tests/logs/20261127-155355-bench.log), matching the NumPy mirror's S of about 0.79.
Cost
ms per pass of each shipped codebox on the bench (Matt's go 2026-11-27, tests/logs/20261127-161148-bench.log). Nearly every row is an UPPER BOUND at the bench's own per-frame floor, not a resolved cost: the pass fits inside the CPU overhead. Only the 512 x 512 DFT pass resolved.
| Stage | 128 x 128 | 256 x 256 | 512 x 512 |
|---|---|---|---|
DFT pass (fx) | <= 0.21 | <= 0.43 | 0.67 (resolved) |
flow | <= 0.22 | <= 0.26 | <= 0.21 |
rhs | <= 0.25 | <= 0.24 | <= 0.26 |
upd | <= 0.20 | <= 0.45 | <= 0.31 |
nl | <= 0.33 | <= 0.44 | <= 0.46 |
Render stage enc (three outlets): 512 x 512 Active <= 0.49 active / <= 0.40 bypassed, Kinem <= 0.23 / <= 0.24; 1080p Active <= 0.74 / <= 0.74, Kinem <= 0.77 / <= 0.75 (all upper bounds; bypassed equals active, as the solver and loop keep running).
Per-frame sum at the solver size 128 and 1080p, an upper bound by addition (inferred, not a measured frame): 8 DFT passes at <= 0.21 plus flow, rhs, upd, nl at <= 0.25, 0.20, 0.33 and 0.22 plus enc <= 0.77 is about 3.4 ms, plus the small hold, seed and pass stages (not measured separately, each at most about the 0.2 ms floor). The true cost is likely well under this. For comparison f_ks measured 2.39 ms per frame for its whole chain at 256 (f_ks.md), and f_vf_resonate's target was at most 5 ms at 1080p. Cost target (Matt, 2026-11-27): at most 5 ms per frame at 1080p, the f_tf_hatch budget and f_vf_resonate's target. The summed upper bound (about 3.4 ms plus the small stages) is already under it; the measured whole-frame cost has not been taken (only per-stage upper bounds). Float32 is the type, so no char comparison applies.
Notes
- What was observed and where. NumPy (float64, the spike,
ideas/f_nematic.md"Spike result", and Phase 0 in.specify/f_nematic/tasks.md): defect counts of the two signs equal at every record; level B settles to 0 defects; the passive control (zeta0) decays; +1/2 defects move about twice as fast as -1/2; a long run at the shipped step stays alive (128 squared, T = 600, three seeds, float32: late mean 59 to 60 defects at dt 0.2, 68 to 69 at dt 0.35, 51 to 54 at dt 0.05, so the count is not step-converged: it drifts up about 12 to 15 percent at 0.2). GPU (bench): every stage against the mirror at 1e-7 to 1e-6, twelve whole frames at 6.4e-6, the module reaches order and keeps changing, bypass exact (tests/logs/20261127-155009-bench.log,20261127-155355-bench.log,20261127-155350-bench.log). - Kinem mode saves no cost. The solver stages run in both modes (as f_ks and f_vf_fluid run under bypass); only the picture differs. Its field is analytic, not periodic across the frame edge.
- Cores. At a defect the order S goes to zero and the tensor is isotropic (coherence 0), not absent: the trace stays 1, so a trace-gated consumer such as
f_tf_glyphstill draws a (round) glyph there. The look of the cores is Matt's call (spec N14). - Bypass does not use the native pix
@bypass; the three neutrals are in the Outlets table. The raw neutral is the zero tensor(0, 0.5, 0, 1), not black. - Fixed 128 squared solver, independent of render size. 256 squared was the fallback if the render looked coarse at 1080p (live look owed); each DFT pass is a 128-term loop per texel.
- A frame at about 30 draws is not yet ordered (mean S 0.38); a reset takes about half a second to a second to look like a nematic (inferred from the frame counts at 60 fps).
Known issues / open
- Cost target set (5 ms at 1080p, Matt 2026-11-27); whole-frame cost not measured, only stage upper bounds. Measured 2026-11-27 (bench, Matt's go,
tests/logs/20261127-161148-bench.log,bench_nematic_cost.py): see the Cost section above. - Panel layout is the builder's default grid (190 x 150), not looked at in Max;
G-ui-densitydecides. - Live looks owed (Matt): glyph, aniso_blur and
f_tf_smoothon this source, the cores, the schlieren look, dial feel and ranges, thescale192 density (about 570 defects in the frame, NumPy), and the 128 against 256 question at 1080p (tasks T051 to T053, T072, T081, T082). - Sign naming. Which sign of activity is "extensile" in the literature was not checked; the dial is not labeled. With
align0 the sign does not matter (observed), so the dial is nonnegative; the full symmetry(q, zeta, lambda)to(-q, -zeta, -lambda)holds as an identity of the scheme (tested 1e-12). - Not tested:
eta, the elastic back-stress, dealiasing, other seed shapes, non-square or non-periodic domains, runs longer than 600 time units, float32 FFT arithmetic on the GPU over long runs, how it looks at video resolution. - The flow
uis computed every step but has no outlet (a vecfield outlet is spec story US7, not built).
Verification
tests/modules/nematic/test_nematic.py— NumPy mirror, 21 tests: emission hand cases and round trips, flow and update closed forms, the passive single mode, the sign symmetry, the stage chain against the monolithic step, 300 chain steps alive with equal counts, level B settling, passive decay, containment, level A closed form and winding, schlieren formula. Logtests/logs/20261127-154327-offline.log.tests/modules/nematic/bench_nematic.py— GPU against mirror, 10/10: the render stage (three outlets, three cases, bypass, phase wrap), every solver stage, seed statistics, twelve chain frames.tests/logs/20261127-155009-bench.log.tests/modules/nematic/bench_nematic_module.py— the real patcher, 2/2.tests/logs/20261127-155355-bench.log.- Module contract bench: bypass expectations are the three neutrals; passes for
f_nematic(tests/logs/20261127-155350-bench.log). tests/modules/nematic/measure_phase0.py— the offline measurements behind the ranges (not a test).
References
The model is an overdamped active nematic in two dimensions: a Q-tensor relaxing under a Landau-type bulk term and an elastic term, coupled to an incompressible Stokes flow driven by an active stress proportional to zeta Q, with advection, corotation and flow-alignment terms. It was implemented from the equations in development (the NumPy spike, then the stages); the physics and the sign conventions are from memory and were not checked against a source, no source implementation was consulted and no code was taken from elsewhere. In two dimensions the cubic Landau-de Gennes term vanishes, so the passive limit is the real-coefficient Ginzburg-Landau relaxation.
- Standard background to look up before citing anything: Landau-de Gennes theory of nematic liquid crystals, and hydrodynamics of active nematics (Simha and Ramaswamy; Marchetti and co-authors review). Names only: titles and years were not looked up.
- Verification is against closed forms and the project's own NumPy spike, not against a published simulation.
Source Files
src/f_nematic/definition.py, src/f_nematic/gen_dft.py, src/f_nematic/codebox_{seed,pass,flow,rhs,upd,nl,enc}.gen, src/f_nematic/codebox_dft_{fx,fy,iy,ix}.gen (generated), package/patchers/f_nematic.maxpat, .specify/f_nematic/{spec,plan,tasks}.md, ideas/f_nematic.md, ideas/f_nematic_spike.py.