Skip to content

nav: per-step energy books, the explicit-Euler alarm, and context work at the tick - #832

Merged
transfix merged 1 commit into
masterfrom
feat/nav-energy-books
Oct 27, 2026
Merged

transfix merged 1 commit into
masterfrom
feat/nav-energy-books

Conversation

@transfix

Copy link
Copy Markdown
Owner

The DBG white paper's navigator energy ledger (Eq. 38, the explicit-Euler alarm, and context work at tick boundaries; companion Part V §3.3) was specified but not built. #747 evaluates U_nav along every rollout, but nothing booked a step's energy or checked it. This adds the books, their two checks, and context work in the live drive.

The per-step books

For one semi-implicit step of a point mass, v' = v + dt/m (-g - γv + F), vt = clamp_vmax(v'), x' = x + dt vt, with g = ∇U(x):

  • W_γ = dt/2 γ v·(v+v'), W_port = dt/2 F·(v+v'), W_clamp = m/2(|v'|²-|vt|²), W_× = dt g·(vt-v');
  • r_E = ΔH + W_γ + W_clamp - W_port - W_×, which is exactly dt²/2 [vtᵀH̄vt - g·(g+γv-F)/m].

Two checks:

  • Cheap bound, every step: |r_E| ≤ κ_H dt²/2 [|g|(|g|+γ|v|+|F|)/m + L̂|vt|²], with L̂ the secant Lipschitz estimate and κ_H = 2. It catches work of order dt left out of the books: an unlogged force, a stale field, an unbooked clamp.
  • Sharp check r̂_E = dt²/2 [vtᵀ∇²U(x)vt - g·(g+γv-F)/m], with |r_E - r̂_E| = O(dt³). The Hessian-vector product is a central difference of the force along vt (two extra force evaluations, only when asked for).

Where it lives:

  • rollout_energy (material.h) gains optional per-step arrays: kinetic, w_damp, w_port, w_clamp, w_cross, residual, residual_sharp and bound, plus a sharp flag.
    • integrate_surrogate_material fills them. It has no speed clamp, so W_clamp and W_× are zero there.
    • The books are those of the step taken from the rollout's state, evaluated in double (detail::material_energy<double>). r_E - r̂_E is O(dt³), about 1e-8 at dt = 0.0025, which is below float round-off on U (about 1e-6). So the books leave out the rollout's own float rounding, which is O(eps) a step.
    • integrate_bicycle_material and integrate_dynamic_material throw std::invalid_argument if asked for the books, because they are not point masses.
  • book_step() books any such step from its quantities, and energy_residual_bound() is the cheap bound from norms.
  • ipc_b_pw, sdf_barrier_b, ipc_dbdd_pw, sdf_barrier_db and patch_sample_channel become *_t<T> templates. The float functions are their float instantiations, op for op. Outputs, the byte-identity tests and nav_potential_golden.json are unchanged.

Explicit Euler and the cheap bound. The cheap bound cannot see explicit Euler. Its per-step residual, dt²/2(|g|² + vᵀH̄v) on a goal spring, is second order like the bound itself, and is about half of it. The sharp check does see it: the explicit step sits dt² g·(g+γv-F)/m off r̂_E, while a semi-implicit step on a quadratic potential meets r̂_E exactly. So ExplicitEulerRaisesTheAlarm asserts the sharp check.

Context work in sim_world

While drive-telemetry capture is on (set_capture_drive_telemetry), sim_world::step fills three new drive_telemetry fields. They are zero from the drive_step* entry points and on the CUDA drive. All three use drive_potential with the tick's coefficients and carrot.

  • potential_end: U where the tick ends, after the drive, override_command and min_gap.
  • projection_work: U's change across override_command and min_gap, measured from where the drive (or the plant) left the agent.
  • context_work: this tick's potential, at substep 0 in the new context, minus U at the same pose with last tick's context (coefficients, carrot, field, material planes).
    • It is evaluated as step() begins, before the sense and the field rebuild.
    • It is zero on the first captured tick.
    • It equals potential - last potential_end unless the agent was moved, or the scene edited, between ticks.

To make both evaluations use the same vehicle, the existing vehicle-params block moves into a private sim_world::drive_vehicle() unchanged. With capture off nothing else runs, so outputs are byte-identical (checked).

Tests (nav_potential_test, 10 → 16)

  • SurrogateEnergyIdentityPerStep: the material fixture, one step from a moving start, with damping and a port, at dt 0.02 / 0.01 / 0.005 / 0.0025.
    • Measured order of r_E: 1.99 / 2.00 / 2.00.
    • Measured order of r_E - r̂_E: 2.99 / 2.94 / 2.93.
    • Per agent, r_E/dt² and (r_E - r̂_E)/dt³ stay within a factor of 2 per halving: worst 1.28 and 1.60.
    • Two agents sit so deep in a barrier that a 0.02 step changes v by more than a quarter. The per-agent check skips them, the order fits keep them, and |r_E| ≤ bound holds everywhere.
  • ClampWorkClosesTheBooks: diff::surr_step with vmax cutting v' to a third. Every step closes within the bound once W_× is booked, and all 213 fail it without W_×. |r_E - r̂_E|/bound is at most 0.037.
  • UnloggedForceRaisesTheAlarm: on the surrogate with a port, every step closes with F_ext booked. With it left out (r_E + W_port), 229 of 240 steps fail the bound.
  • ExplicitEulerRaisesTheAlarm: integrate_surrogate_v2 on circular orbits of a goal spring, for one period.
    • Explicit Euler: r_E > 0 at every step, with gain exactly ω²dt² a step, and H_T/H_0 = (1+ω²dt²)^S to 2e-6. (r_E - r̂_E)/bound = 0.499 at every step.
    • Semi-implicit: within 3.5e-5 of the sharp check, and inside the bound.
  • SimWorldBooksTheContextWorkOfAGoalChange: a goal changed between two ticks. context_work == potential - last potential_end bit for bit, projection_work is 0, and capture-on positions are byte-identical to capture-off. The retargeted agents book -1.63 and -3.27; the others book the same value as in the run without the change.
  • SimWorldBooksTheMinGapPushAsProjectionWork: the pushed pair books +0.18 and +0.59, and the others book exactly 0.
  • MaterialRolloutsAreByteIdenticalWithTheRecord also covers the books with the sharp check on, and the vehicles' refusal.

Verified locally on Windows (MSVC 14.44, Release, cvcpkg prefix), before the rebase onto 4cd69ad (master's two new commits touch volume, LBIE and app.h only):

  • nav_potential_test 16/16, nav_test 62/62 (its drive-telemetry capture test is the byte-identity check), nav_sim_world_test 3/3, nav_material_test 33/33, nav_material_rollout_grad_test 2/2, nav_material_bike_rollout_test 12/12, nav_material_ext_test 11/11, nav_geom_rollout_grad_test 15/15, nav_terrain_test 10/10, plus nav_coef_train_test, nav_drive_refinements_test and nav_stats_test.
  • clang-format 18.1.8 clean on the changed files.

Notes:

…k at the tick

The white paper's navigator energy ledger (Eq. 38) was specified but not
built: the rollouts evaluate U_nav (#747), but nothing booked a step's energy
or checked it.

- rollout_energy gains per-step books for the point-mass material surrogate:
  kinetic energy, damping work, port work, clamp loss, clamp cross term, the
  residual r_E, its cheap bound (every step) and the sharp check r_hat_E (two
  extra force evaluations a step, when asked). They are the books of the step
  taken from the rollout's state, evaluated in double, so they resolve
  r_E - r_hat_E = O(dt^3), which is below float round-off on U. The bicycle
  and dynamic rollouts throw when asked for them.
- book_step() books any semi-implicit point-mass step from its quantities;
  energy_residual_bound() is the cheap bound with the secant Lipschitz
  estimate and kappa_H = 2.
- The barrier potentials and force primitives become *_t<T> templates; the
  float functions are their float instantiations, op for op.
  detail::material_energy<T> evaluates U and the force the surrogate applies.
- sim_world fills three new drive_telemetry fields while it captures:
  potential_end, projection_work (override_command and min_gap) and
  context_work (this tick's U at its start pose minus U at the same pose with
  last tick's context, evaluated before the sense and the field rebuild). The
  vehicle-params block moves unchanged into sim_world::drive_vehicle() so both
  evaluations read the same vehicle. The drive leaves the three fields zero.

nav_potential_test, 10 -> 16 tests: as dt halves 0.02 -> 0.0025 the measured
order of r_E is 1.99/2.00/2.00 and of r_E - r_hat_E 2.99/2.94/2.93. The cheap
bound catches an unlogged force and an unbooked clamp cross term; explicit
Euler passes it (its residual is second order too) and fails the sharp check.
@transfix
transfix enabled auto-merge (squash) October 27, 2026 01:20
@transfix
transfix merged commit 09fa876 into master Oct 27, 2026
23 of 24 checks passed
transfix added a commit that referenced this pull request Oct 27, 2026
The project version and the cvcpkg recipe family move to 3.7.1. The recipes that carried
3.7.0-lineage revisions (libcvc, cvcgl, libcvc-examples-web, pycvc-gl-cp312) restart at
cvc_revision 1, since a version bump restarts it.

3.7.1 carries dyn_bike's port-Hamiltonian form and per-step energy books (#831), the
navigator's per-step energy identity and context work (#832), and the dynamic single-track
plant for plant_host (#833).
transfix added a commit to CVC-Lab/GRL-SNAM that referenced this pull request Oct 28, 2026
…he sharp check (#139)

The torch twin of libcvc's per-step energy books for the navigator's
point-mass surrogate (the white paper's Eq. 38; transfix/libcvc#832), held
to libcvc's nav_energy_books_golden.json (transfix/libcvc#849).

- nav_potential.book_step (StepBooks) and energy_residual_bound: libcvc's
  book_step, with the cheap bound and, given vHv, the sharp check r_hat_E.
- nav_potential.material_energy: U and the material surrogate's conservative
  force in any dtype, libcvc's detail::material_energy<double>.
- integrate_surrogate_material: energy={"books": True} (or "sharp": True)
  also fills kinetic, w_damp, w_port, w_clamp, w_cross, residual, bound
  (and residual_sharp), float64 and detached, booked from the rollout's
  state in float64 as libcvc does. The rollout's outputs are unchanged.

tests/test_nav_energy_books_parity.py: book_step and the bound bit for bit
on libcvc's arguments (DOUBLE tier); the clamped surr_step rollout's books
to 2e-5 of the step's bound (measured 1.1e-6), and the material surrogate's
to 1e-5 (measured 8.3e-8) (FLOAT tier); the unlogged port raises the alarm
at every step, as in libcvc; r_E is second order, and the sharp check meets
r_E on a quadratic potential.
transfix added a commit that referenced this pull request Oct 28, 2026
…av (#857)

nav_sim_world_set_capture_drive_telemetry and nav_sim_world_drive_telemetry
read sim_world::drive_telemetry_data from Python. The second returns the last
captured tick as a dict of (N,) arrays named as drive_telemetry's fields, with
the books only a world fills (#832): potential_end, projection_work and
context_work. It returns None while capture is off.

The dict is built by one helper (pycvc_telemetry::to_dict), which
nav_drive_step_material_cuda's telemetry now uses too. That dict gains the
three keys, which the bare drive step leaves zero; test_pycvc_nav_cuda checks
both.

test_pycvc_nav_telemetry.py (ctest pycvc_nav_telemetry) is
NavPotential.SimWorldBooksTheContextWorkOfAGoalChange through Python. It
checks:
- capture changes no pose;
- context_work is zero on the first captured tick, and projection_work is
  zero without an override or min_gap;
- context_work equals potential minus last tick's potential_end, bit for bit;
- the two retargeted agents book -3.353 of context work, against +0.007
  without the goal change.
Locally (MSVC, Python 3.11) it passes 23 checks, and the nav routes and
belief scripts pass unchanged.

sim_world.h: drive_telemetry_data() returns the defaults, not null, from
turning capture on until the first captured step; its comment now says so.
@transfix
transfix deleted the feat/nav-energy-books branch October 29, 2026 20:52
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