f_defects_spike — does a winding-number readout beat thresholding |A| on f_cgl?
Renamed 2026-11-29: the module built from this spike is f_cores (was f_defects; Matt, chat). This note and the tests/records/*defects* files keep their historical names; module paths below that say f_defects or modules/defects are now f_cores and modules/cores.
Status: 🔵 spike plan, all three offline and bench steps run 2026-11-28 (Claude, with Matt's go in chat; the bench was open): the f_cgl NumPy tier, the f_nematic NumPy tier, and the Max codebox tier. Results below. Not done: the live look (tier 3, Matt's), the 1×1 density reduction, and the reader on the shipped module's own outlet in Max (the bench fed it the mirror's D10 encode of NumPy states). NumPy numbers are observed on float64 and float32 mirrors, not on the modules.
The question
f_cgl makes spiral cores that nucleate and annihilate (up to 174 at once in the NumPy spike, per docs/f-reference/f_cgl.md), and nothing reads them back out. Is a defect reader, a stage that outputs where the cores are and which way each one winds, worth building, or is thresholded |A| already enough?
That second half is a decision already on record. .specify/f_cgl/spec.md D11 parked a defect-marks outlet as "not v1" on the argument that a threshold on |A| already marks cores and f_vf_seeds can take it, with the tripwire "a live look shows thresholded |A| is a poor seed source". User Story 6 and T055 carry it as unbuilt, with no gates row. So the spike does not ask "can cores be marked" (D11 says yes, cheaply). It asks what the winding readout adds that thresholding does not.
What a winding readout would add (the claims to test)
- Sign. Charge (+1 or −1) per core, so pairs can be told from like-sign neighbors. A threshold on
|A|has no sign. - Fewer false marks.
|A|can dip without a core. Whether it does in the regimesf_cglruns in is unobserved; Nozaki-Bekki holes are the case I recall (external, from memory). - A count that obeys a rule. On a periodic grid the net charge is zero, so a detector can be checked against itself every frame.
T016(a)already tests equal + and − counts for the mirror. - Other sources. The same 2×2 loop works on a director field (
f_nematic; observed, see "Result, second source" below: no half-angle step is needed), which a threshold on|A|cannot do. This is the argument for a separate reader rather than anf_cgloutlet (open question 1 in the bridge note), and it is not tested here.
What to find out
- Agreement with the reference. On the float64
A, the mirror'sdefects()sums wrapped phase differences around each 2×2 plaquette and returns the counts. Extend it to return the per-plaquette windingw, and use that as ground truth. How often does each candidate detector (below) agree withw ≠ 0, per regime? - The D11 baseline, measured. Sweep a threshold
τon|A|. At its bestτ, what is the precision and recall of "this plaquette holds a core", and what share of thresholded blobs carry no charge? - A detector with no
atan2. D12 bansatan2in any codebox. Winding can be counted without it: walk the four edges of the loop and add +1 for a counterclockwise crossing of the positive real axis (Imchanges sign across the edge, and the 2D cross product of the two ends is positive), −1 for a clockwise one. Does it agree exactly with the wrapped-sum reference? (It should, except at texels that are exactly zero; the tie-break has to be chosen.) (inferred) - The real encoding. Outlet 2 is not
A: per spec D10,R, G = 0.5 + 0.5 · clamp(Re A, Im A, −1, 1)after theexp(dt/2)scaling of D15a, in float32. Clamping preserves sign, so winding should survive; check it. Count how many reference defects are lost or invented after encoding. - Grid pitch. The solver is 256², but the two encode stages follow the render size. A 2×2 loop at 1080p spans a small fraction of a solver cell and would find almost no windings. The detector probably has to step at the solver pitch (1/256 of the frame), or run on the 256² texture. Which, and does it cost a resample? (inferred; how
encvsamples was not read) Answered in the NumPy tier: the native 2×2 loop works at render size; see Result. - Under-sampling. Where a phase step across one edge nears π the loop miscounts. What fraction of plaquettes have a step above, say, π/2 in defect turbulence at 256²?
- Does the count work as a control? Over time at the three regimes, how smooth is the defect count, and how many create or annihilate events per second at 30 fps? A flat zero in phase turbulence, a busy count in defect turbulence, a constant in frozen spirals would be regime-informative. This is a look, not a pass or fail.
Proposed method (per the f_ verification tiers)
- Tier 1, NumPy (offline, no Max, no API calls). Reuse
cgl_mirror.pystates at N = 256 for one point per regime from the regime map indocs/f-reference/f_cgl.md(phase turbulence from a Uniform seed, defect turbulence from Noise, frozen spirals), a few seeds each. Questions 1 to 4, 6 and 7 are all answerable here. Questions 4 and 5 need the D10 encode applied to the float32 state; the mirror'sfrom_complexand encode path are the place to start. Follows thef_cgl_spike.pyandf_nematic_spike.pypractice. - Tier 2, bench (needs Max in the foreground; Matt's go, announce first). One scratch pix stage beside
f_cgl, reading outlet 2, writing a signed mask and a 1×1 density, and a cost measurement. Read thejit-gen-codeboxskill first (sample(), edge behavior and theatan2NaN hazard are all in it). Do not start before the NumPy numbers say it is worth it. - Tier 3, live look: Matt's. Do the marks sit on the cores, appear and vanish in pairs, behave at the periodic edge, and does the mask drive
f_vf_seedsorf_vf_stampbetter than thresholded|A|? This is also the open T042 (outlet 2 into a consumer) from a second side.
Reuse to check before writing anything
tests/modules/cgl/cgl_mirror.py (defects(), the encode path, to_complex), ideas/f_cgl_spike.py (the regime classifier and its sample points), docs/max-reference/jit_fx.md for jit.fx.an.mean (a 1×1 reduction exists in the platform; the density could come from it or from a reduction of the mask), and f_util_profile as the CPU-side precedent if a count has to leave the GPU. Not read for this note: how encv samples the solver grid, and how f_vf_seeds consumes a mask.
Stop rules
- If thresholded
|A|is good enough, stop. If at its bestτit reproduces the winding mask at roughly 0.9 precision and 0.9 recall in all three regimes, D11 stands: the answer is "threshold|A|and feedf_vf_seeds", and no stage is built. The 0.9 is my choice, not derived. What remains is whether sign alone justifies a stage; that is Matt's call. - If the real encoding breaks it, stop and say how. If the detector on the D10 float32 texture misses or invents more than about 1% of reference defects in any regime, or breaks net charge zero, outlet 2 is not a good enough input as it stands. Record which change would fix it (an unclamped outlet, or the detector on the solver grid) and who decides.
- Cost (tier 2 only). If one pix stage at the solver grid costs more than about 0.5 ms per frame, it is too heavy to sit beside a 3.5 ms solver. Four neighbor reads and a few comparisons should not come near that; the number is a budget I picked, not a derived figure.
Result, NumPy tier (run 2026-11-28; Claude offline, Matt's go)
Run. 4 regimes × 2 seeds, N = 256, L = 200, dt = 0.1 (the module's defaults), T = 300, five samples per run (t = 100 to 300), 76 s total, CPU only. Integrator: ideas/f_cgl_spike.py (float64). Encode and upsample: the mirror's vec_encode and upsample_periodic. Regime points: PT (c1 2, c3 −0.7), DT (2, −1), STAB (1, −0.5), and FRZ (4, −0.3), a frozen-spiral guess that did not freeze: its count fell from 25 to 3 pairs by t = 300, so it is "slowly annealing" and its label is unverified. The whole table is 2528 core plaquettes (DT 1534, STAB 746, FRZ 248, PT 0).
Detector agreement (Q1, Q3, Q4): exact. The atan2-free crossing-count detector agreed with the float64 wrapped-sum winding on every plaquette: 0 missed, 0 invented, 0 sign-flipped, on the float32 state and on the D10 encoded outlet (offset 0.5, clamp ±1, exp(dt/2)). Net charge was zero in every sample, and + and − counts were equal. PT gave no marks (min |A| 0.967). The clamp and float32 did not disturb winding here. Stop rule 2 did not trigger.
Render size (Q5): the native 2×2 loop works. Counts at 1024² and at 1080×1920 (a non-integer ratio) equalled the solver-grid counts exactly, so the encode's bilinear upsample preserves winding and no resample to the solver pitch is needed. My inference in the plan was wrong. Cost of that: each core is then one render texel, so a visible mark needs a dilate. A stride-4 loop at 1024² marks a cell-sized patch instead, but counts come out about 16× too high (DT 1074 against 66; STAB 544 against 34 is exactly 16×), explained by loop area (inferred); read the count from the native loop, not the stride loop.
Under-sampling (Q6). 0.1% (FRZ) to 0.9% (DT) of plaquettes had an edge phase step above π/2 and the detector still made no error. Steps above π (the real failure) were not counted separately.
Count as a signal (Q7), last 150 steps: PT constant 0. STAB and FRZ near constant (count changed in 0 to 1 of 149 steps). DT busy: mean 136 to 155 defects, std 5 to 14, changed in 93 to 106 of 149 steps by about 2 on average (consistent with pair creation and annihilation, not separated). So the count tells the regimes apart: zero, settled, busy.
The D11 baseline, thresholding |A| (Q2). I scored this twice and the first way was unfair.
- Plaquette level (as the plan specified): best F1 0.28 (DT), 0.36 (STAB), 0.31 (FRZ), precision about 0.2 at about 0.9 recall. By the letter of stop rule 1 the threshold fails. But a blob of several texels flags several plaquettes while one holds the winding, so this metric is biased against D11's claim. I changed the baseline after seeing it; both are reported.
- Blob level (a blob of
|A| < τ, pooled over 10 samples):
| Regime | τ | Blobs with a core | Cores covered | Blobs holding 2+ cores |
|---|---|---|---|---|
| STAB | 0.2 to 0.4 | 100% | 100% | 0 |
| FRZ | 0.2 | 98.8% | 100% | 1 of 250 |
| DT | 0.1 | 83.8% | 90.9% | 42 of 1769 |
| DT | 0.15 | 79.6% | 99.7% | 84 of 1925 |
| DT | 0.3 | 78.1% | 100% | 204 of 1627 |
So in the calmer regimes a threshold of about 0.2 reproduces the cores with no charge errors, and D11 stands. In defect turbulence, about 1 blob in 5 is an amplitude dip with no winding (the cause is not examined), and cores begin to merge into one blob from τ about 0.15. The best τ also moves with the regime (0.1 to 0.2), where winding has no threshold dial.
What this says about the stop rules. Rule 2 (encoding breaks it): not triggered. Rule 1 (threshold is good enough): met in STAB and FRZ at blob level, not met in defect turbulence, and not met under the plaquette metric as written. Rule 3 (cost) is a tier-2 question and was not run.
Not tested (f_cgl): charge sign as a visual feature (no look taken); other L, dt or dial ranges; whether f_vf_seeds does better on a winding mask than on thresholded |A| (the D11 tripwire, a live look).
Result, second source: f_nematic (NumPy, run 2026-11-28; Claude offline, Matt's go)
Run. tests/records/spike_defects_nematic.py, 15 s. 4 regimes × 2 seeds on the f_nematic mirror at the module's settings (solver 128², dx = scale/128, uniform quench noise, finite-difference gradients, dt 0.2, containment, gamma 0.1): ACT (zeta 2, scale 64), ACT192 (zeta 2, scale 192), LOWACT (zeta 0.5), COARSEN (zeta 0). Five samples per run. Reference: the mirror's own winding(). 5660 core plaquettes in total (584 + 4884 + 150 + 42). The mirror's q is the angle-doubled director, so a +1/2 or −1/2 defect is a winding of ±1 of that doubled phase.
Detector agreement: exact, with no half-angle step. The same crossing-count detector, run unchanged on the packed outlet's R, G (already the doubled direction) and on the raw tensor outlet (decoded to c1, c2), through float32, agreed with the reference on every plaquette: 0 missed, 0 invented, 0 sign-flipped, net charge zero in every sample. In ACT192, 13.6% to 13.7% of plaquettes had a doubled-phase step above π/2 and the detector was still exact (steps above π were not counted separately).
Render size. The native 2×2 loop on the bilinear-upsampled field matched the solver-grid counts in all sparse runs but found a few extra ± pairs in dense ones: up to +8 of about 250 pairs (3%) at ACT192, +1 of 28 in one ACT run. On f_cgl the counts matched exactly. Cause not examined; a bilinear cell can hold a ± pair that its corner loop does not see (inferred). Read the count from the solver grid, or accept a few percent.
Count as a signal (last 100 steps). ACT mean 59 defects, changed in 76 to 80 of 99 steps (about 2.8 per step); ACT192 about 480, changed in 91 to 92 of 99 (about 8.7 per step); LOWACT about 13, changed in 13 to 14; COARSEN about 4 and static (0 to 1 changes). Defects scale with activity and area, as the f_nematic doc says.
The D11 analogue, thresholding |q| (the order parameter), blob level, pooled over 10 samples:
| Regime | τ | Blobs with a core | Cores covered | Blobs holding 2+ cores |
|---|---|---|---|---|
| COARSEN | 0.2 to 0.4 | 100% | 100% | 0 |
| LOWACT | 0.3 | 90.3% | 100% | 8 of 155 |
| ACT | 0.2 | 72.9% | 100% | 34 of 813 |
| ACT | 0.3 | 80.8% | 100% | 68 of 620 |
| ACT192 | 0.4 | 76.3% | 93.5% | 568 of 5781 |
| ACT192 | 0.5 | 72.6% | 99.7% | 829 of 5923 |
Thresholding works only when defects are few and far apart (COARSEN). In every active regime about 1 blob in 4 is an |q| dip with no defect (cause not examined), and in dense fields cores merge into shared blobs. This is worse than on f_cgl, and a threshold has no sign, which on a nematic separates the self-propelled +1/2 comet from its −1/2 partner (inferred from the module doc; no look taken). Stop rule 1 is not met in any active nematic regime.
Not tested (f_nematic): the GPU module (this is the mirror); the Kinem (analytic) source; the sphere; a live look.
Result, Max tier (run 2026-11-28; Claude, Matt's go; bench open)
What ran. tests/records/codebox_defects_spike.gen, a scratch pix stage (not a module): four nearest() corner reads, a wedge() function for the signed crossings, wrap() for the periodic neighbors, no atan2. It compiled and ran first time. Bench: tests/records/bench_defects_spike.py, 4 of 4 rows pass (log tests/logs/20261128-224456-bench.log). Input was the mirror's D10 encode of the NumPy tier's f_cgl states.
Parity: exact on all 16 inputs. GPU marks equalled the NumPy detector texel for texel (0 mismatching texels) on 12 real states (PT, DT, STAB, FRZ, three each), white complex noise (dense and under-sampled: 10,977 + and 10,977 − marks), a uniform field (0 marks), a ± vortex pair centered on exact texel centers (an exact (0, 0) texel: the half-open tie convention matched), and a 1080×1920 input (66 + and 66 −, equal to the solver grid). GPU counts also equalled the float64 reference on every real state. No orientation or sign convention needed correcting: the + mark (R) matched the mirror's + loop.
Cost (stop rule 3): not resolved, but under the rule. All three readings sit at the bench's own floor, so they are upper bounds, not costs:
| Input size | Defect pass (upper bound) | Empty pass (floor) |
|---|---|---|
| 256 × 256 (solver grid) | 0.418 ms | 0.439 ms |
| 1024 × 1024 | 0.372 ms | 0.379 ms |
| 1920 × 1080 | 0.743 ms | 0.741 ms |
The defect pass is not distinguishable from an empty pass. At the solver grid, which is what the rule names, the bound is 0.418 ms against the 0.5 ms budget, so rule 3 does not trigger. The 1080p bound exceeds 0.5 ms only because the floor does; it says nothing about the pass. A resolved number would need the amplified repeat-loop measurement of bench-test-authoring rule 9, which was not done.
Not done in the Max tier: the 1×1 density reduction (not built, not measured); the reader on the shipped f_cgl outlet in a Vsynth chain; any look at the marks (a mark is one texel at the input's resolution, so seeing it needs a dilate); f_vf_seeds or f_vf_stamp consuming the mask.
Where the stop rules stand
- Threshold good enough? On
f_cgl: yes in STAB and FRZ, no in defect turbulence. Onf_nematic: no in every active regime. So the winding reader earns a stage mainly for dense or nematic fields. - Encoding breaks it? No, on either source (0 errors in 8188 core plaquettes across both NumPy tiers; 0 mismatches on the GPU).
- Cost. Upper bound 0.418 ms at the solver grid, under the 0.5 ms budget, unresolved.
What remains is a decision, not a measurement: whether a signed readout of this kind is wanted as a module, and as one shared reader or a stage per source (bridge note, open question 1; the evidence here favors one shared reader, because the identical detector worked on both).
Gate
None. This touches f_cgl D11 (its revisit tripwire is in the spec, not in .specify/gates.md), User Story 6 and T055, and the open T042. Add a gates row only if Matt wants them tracked, as T055 already says.
Related
field_object_bridge.md, f_cgl.md, f_nematic.md, f_vf_seeds.md, f_rd_stepper_spike.md (the format this follows), .specify/f_cgl/spec.md (D10, D11, D12, D15a, User Story 6).