Skip to content

coph_golden: the C2 cases (G2 plant, G3 potential and command, G11 allocation) - #170

Merged
transfix merged 1 commit into
mainfrom
feat/coph-golden-c2
Nov 13, 2026
Merged

transfix merged 1 commit into
mainfrom
feat/coph-golden-c2

Conversation

@transfix

Copy link
Copy Markdown
Contributor

This adds the golden cases libcvc's C2 needs (docs/roadmap/NAV-COPH-ROADMAP.md §5): G2, G3 and G11. It extends python -m grl_snam.tools.coph_golden from CoPH T5 (#168), and adds grl_snam.coph.allocation, which G11 pins.

What lands

  • G2, the plant (plant): model.flow and model.balance over 40 (x, u, d, dt) cases. That is 10 dampings, each under 4 (x, u, dt):

    • d from 1e-8 to 10, including the profile's bounds (0.15, 1.5), its initial 0.6 and the demos' world's 0.85;
    • dt of 0.12 (the profile), 0.6 (the Humvee step, §3.3.1), 0.05 and 1.
  • G3, the potential and command (potential): model.potential, its gradient (torch autograd) and model.command at 25 points on five scenes. Each point is evaluated under legacy shaping (Learner.act and replay call command without cfg) and progress shaping (the profile's routed controller), so 50 cases. The scenes are:

    • crossing: the two door slabs;
    • crossing_with_disks: doors and disks, which takes surface_distance's path;
    • hazard: a risk bump and no obstacles;
    • disks: disks only, which takes the disk branch, as the cars' obstacles (§3.3.3) will;
    • fresh_alpha_1: a fresh stage with the box (0, 2), where alpha is 1.

    The forces fall on both sides of the limit in both modes, and alpha is both clipped and 1. The barrier is active at 10 or more points. No point sits where V is not smooth: a slab's segment ends or core, or a disk's centre.

  • G11, the allocation (allocation): 60 cases, 10 speeds by 6 forces, over three cars (a RoboRacer, a Humvee, and a RoboRacer with a lower lock and clip). Among them:

    • at rest (12) and in reverse (12);
    • at the steer lock (29) and inside it;
    • the acceleration clip both ways, and unclipped.
  • grl_snam/coph/allocation.py (new, GRL-SNAM code, listed in VENDORED.md). It is §3.3.2 as written, and returns the acceleration, the steer, kappa*, u_used and the unallocated force e. Its conventions are those of sim_world::override_command: the steer is the bicycle angle (rad, + left), and the acceleration is along the heading.

Each section carries a rules text (plant_rules, potential_rules, allocation_rules), as G4 carries stage_rules. G1 and G4 are unchanged.

The test

  • Regeneration. G1, G4, the rules and every input must match exactly. The G2, G3 and G11 outputs are the platform's float64 torch and libm results, so Linux CI may differ from the Windows-written fixture in the last bits. Those outputs are compared at their tier, FLOAT64: rtol 1e-12, atol 1e-12. On the platform that wrote the fixture, it is still byte for byte. A new test checks that 2-ulp nudges pass and that real changes, to an output or an input, fail.
  • Replays. As with G4, every G2, G3 and G11 output is rebuilt in plain Python from the file's inputs and its rules text alone, to the tier. G3's replay uses the analytic gradient, which is the form C2's coph_grad_potential will take.
  • Coverage. A test checks every claim above (counts, d down to 1e-8, both sides of each clip, both modes per point).
  • tests/test_coph_allocation.py. This checks what the allocation means:
    • within the limits, forward or in reverse, the car takes the whole force (e = 0);
    • at rest the lateral force is unallocated, with the wheels turned to the lock;
    • at the lock and the clip, e books the rest.

Findings for C2

  • The Windows CRT's expm1 is not correctly rounded. At small d, the vendored closed forms are ill-conditioned: dt − I cancels to about d dt²/2, and heat sums terms of size |u|²/d. So q_after, heat and residual hold to the tier only with a correctly rounded expm1. torch's expm1 is correctly rounded at all 40 G2 arguments, and a test pins that. The Windows CRT's (CPython's math.expm1, and MSVC's std::expm1) is not correctly rounded at most of them. C2 needs its own correctly rounded expm1 at small arguments, not std::expm1 as §4.2 says. The plant replay here uses mpmath's.
  • balance loses heat at tiny damping. At d = 1e-8, heat comes out exactly 0.0, against a true value near 6e-8, and the residual is about 1e-7. The golden pins the vendored result. This never arises in the profile, where the damping stays in [0.15, 1.5].
  • The Wendland residual is out of G3. The demos never set Stage.local_residual: receiver_localization_mode stays 'global'. So the compact-feature term that §4.2 lists is outside the demos' profile (D7).
  • v_floor is new. §3.3.2 leaves v_min open. allocation.SPEED_FLOOR is 0.5 m/s, and G11 varies it (0.25, 0.5, 1).

libcvc's copy of the golden (transfix/libcvc#1210, C1) is the G1/G4 file. C2 copies this one when it lands.

Checks

  • Local, Windows, cp312 (torch 2.8.0 CPU): tests/test_coph_golden.py and tests/test_coph_allocation.py pass, 26 tests.
  • black 26.5.1 and ruff are clean on the repo.
  • Linux CI will exercise the cross-platform comparison.

…location)

The golden libcvc's C++ twin reproduces gains the three FLOAT64 cases C2 needs:
- G2: model.flow and model.balance over 40 (x, u, d, dt), d from 1e-8 to 10 and the
  profile's damping bounds.
- G3: model.potential, its gradient and model.command at 25 points on five scenes (the
  crossing's doors, doors with disks, a hazard bump, disks alone, a fresh stage), each under
  legacy and progress shaping, both sides of the force limit and of progress's alpha clip.
- G11: the new grl_snam.coph.allocation (NAV-COPH-ROADMAP 3.3.2, the Ackermann allocation
  T4 and C5 share) over 60 cases, at rest, in reverse and at the steer lock among them.

Each has a rules text, and the test rebuilds every output from the file's inputs and that text
in plain Python. G1, G4 and every input stay exact; the FLOAT64 outputs are compared at their
tier (rtol 1e-12, atol 1e-12) when another platform regenerates the file.
@transfix
transfix merged commit 4ac2fab into main Nov 13, 2026
4 checks passed
transfix added a commit to transfix/libcvc that referenced this pull request Nov 16, 2026
#1228)

C2 of docs/roadmap/NAV-COPH-ROADMAP.md: the C++ twin of GRL-SNAM's
grl_snam.coph.model and grl_snam.coph.allocation.

inc/cvc/nav/coph_model.h (coph_model.cpp, built with -ffp-contract=off):
- coph_flow and coph_balance: the exact held-force flow and its energy
  balance, in the Python's order. At small damping the closed forms are
  ill-conditioned (dt - I cancels to about d dt^2 / 2; the heat sums terms
  of size |u|^2 / d), and one ULP of expm1 moves q_after by about 1e-8. So
  the plant uses coph_exp and coph_expm1, correctly rounded (double-double,
  rounded once), not std::expm1, which MSVC's CRT misses at most of G2's
  arguments.
- coph_risk, coph_potential and coph_grad_potential (analytic, replacing
  autograd), legacy and progress shaping, the barrier over disks and slabs;
  coph_shaping_parameters and coph_shaping_for (alpha and its bound, once per
  stage); coph_command (-grad V - 1.4 v, scaled to the limit, applied and
  proposed). Templated on the scalar and instantiated in coph_model.cpp
  (double here; C3 adds its duals). No Wendland term: the demos never set
  Stage.local_residual (D7). Transcendentals are cvc::algo::det's.
- coph_energy: work, heat, kinetic change and residual summed by step, the
  shaping jumps, and the unallocated work e . dq. Its plant_ledger items come
  with C5, which attaches CoPH to a vehicle (D11).

inc/cvc/nav/coph_allocation.h: coph_allocate, the Ackermann allocation of
§3.3.2 (speed floor 0.5 m/s, as GRL-SNAM's SPEED_FLOOR), which C5 will use.

The golden in src/cvc/tests/data is now GRL-SNAM main's (CVC-Lab/GRL-SNAM#170,
blob b96cc0c), with G2, G3 and G11 beside C1's G1 and G4. nav_coph_model_test
reproduces them at the FLOAT64 tier (rtol 1e-12, atol 1e-12): on Windows, G2
bit for bit in all 320 values, G3 and G11 within 0.1 % of the allowance. It
also checks coph_exp and coph_expm1 bit for bit against mpmath at 198
arguments (G2's 71 among them), the analytic gradient against central
differences, and that the energy account closes over 2,000 steps (summed
residual 2e-16 against 3.8 of work). nav_coph_test passes on the new golden.

The roadmap's §3.3.2 (the speed floor), §3.4 (the tiered golden), §4.1, §4.2
and §5 (G3's scenes, atol) now say what landed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant