Skip to content

dyn_bike: an opt-in discrete-gradient step whose energy books close exactly - #978

Merged
transfix merged 5 commits into
masterfrom
feat/dyn-bike-discrete-gradient
Nov 7, 2026
Merged

transfix merged 5 commits into
masterfrom
feat/dyn-bike-discrete-gradient

Conversation

@transfix

@transfix transfix commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

What

An opt-in step for the dynamic single-track vehicle whose energy books close exactly: diff::dyn_plant_dg and diff::dyn_plant_dg_books, in a new header inc/cvc/nav/detail/dyn_bike_dg.h. dyn_bike.h, dyn_plant and everything that calls them are untouched, so the defaults are byte-identical. DynBike.Golden and DynBikePH.Golden pass unchanged.

Why

dyn_plant is first order: implicit Euler on (vy, r), and a predicted then corrected vx. So dyn_plant_books leaves its error, which is second order per step, in the books' residual. The GRL-SNAM whitepaper's future work names a discrete-gradient step as the way to close the books exactly.

How

H = 1/2 v^T M v is quadratic in the twist v = (vx, vy, r), so its discrete gradient is the midpoint twist. The step is the implicit midpoint rule on the twist:

M (v1 - v) / hdt = J(M vbar) vbar + Q(vbar),   vbar = (v + v1) / 2
  • Q is the tires, the axle forces, drag, rolling resistance and grade, taken from one dyn_ph_parts_at at the midpoint state. That state uses the actuators' outputs at the step's midpoint, which is what makes the step second order while the actuators move.
  • J is skew, so dH = hdt vbar^T Q(vbar) holds line for line, and dyn_plant_dg_books closes to the solve's tolerance.
  • The solve is Newton from v1 = v. Its Jacobian comes from forward-mode duals, with a backtracking line search.
  • The actuators (step 1), speed clamp and low-speed blend are dyn_plant's. The books charge the clamp and the blend to the limiter line, as dyn_plant_books does.

Gates (nav_dyn_bike_dg_test, the Humvee in metres)

Gate dyn_plant_dg dyn_plant
Momenta's rate vs dyn_ph_rhs, 800 states at every slip 1.1e-15 m g
Actuators vs dyn_plant's, double and float bit for bit
Books' residual, swerving past the peak at mu 0.6, hdt 0.06 to 0.0025 s at most 1.7e-10 J a step, 3.5e-14 to 1.3e-12 of the step's flow 25 J to 0.056 J a step
Books' residual, braking to a stop through the clamp and the blend (0.06, 0.01, 0.005 s) at most 6e-11 J a step
Error at 2 s of a turn-in past the peak, per halving of hdt (0.04 to 0.005 s) 3.92x to 4.03x smaller (second order); 5.3e-3 at 0.04 s 1.98x to 2.13x (first order); 0.45 at 0.04 s
Coasting at full lock from 0.5, 3 and 12 m/s; tires 1x, 4x, 16x; 1 x 0.06 s and 6 x 0.01 s no step gains energy (dH - limiter <= 0 exactly)
Steady 5 m/s turn with 16x tires, both step configurations yaw rate within 0.02% of the kinematic bicycle within 0.03%
Newton steps 3 to 4 a step on the Humvee's tires; up to 13 with 16x tires at a crawl at 0.06 s
Cost per step, double 3.3 to 3.6 us (float 1.9 to 2.1 us); 3.6 to 4.1 us with its books 0.34 to 0.42 us; 1.0 to 1.7 us with its books

Stiff tires: what differs from dyn_plant

  • The midpoint rule is A-stable but not L-stable. A tire much stiffer than the step resolves (S hdt / m >> 1) swings its slip past zero and back, decaying. dyn_plant's implicit Euler lands on the root in one step instead.
  • At the live plant's 6 x 0.01 s substeps with the Humvee's own tires it does not swing: none at a crawl, one in a slide, the same as dyn_plant.
  • At a single 0.06 s step at 0.5 m/s with the blend off, it swings 15 times in 6 s on the Humvee's tires, and at every step with 16x tires. The low-speed blend, which the plant runs with, takes the crawl over: at most 2 swings.
  • With 16x tires at 0.06 s it still swings at 3 and 12 m/s (19 to 43 times in 6 s).
  • It never gains energy while swinging. Where a coasting run ends is closer to the 0.5 ms solution than dyn_plant's at 0.06 s: sliding from 12 m/s, it ends at 9.7 kJ against the reference 10.1 kJ, where dyn_plant ends at 23.4 kJ.
  • Getting the books' exact closure with L-stability would mean booking the integrator's own dissipation on a separate line, e.g. implicit Euler with a 1/2 |v1 - v|_M^2 line. That is not this step and is not done here.

Wheel spin

The plant-physics session owns dyn_bike.h, dyn_4w.h and dyn_bike_plant, and is adding wheel-spin states. This header reads only what dyn_bike.h already exports: dyn_ph_parts_at, dyn_ph_rates_at, dyn_ph_hamiltonian, dyn_clamp, dyn_state and dyn_step_free.

Local

MSVC 14.44 Release, deps from the local cvcpkg prefix, on the branch with master (8f468c18, 2026-11-06) merged in. That merge also brings #1003, the fix for ari_chat_component's Debug-runner timeout, which failed this PR's package-linux / libcvc-debug:

Suite Result
nav_dyn_bike_dg_test 7/7
nav_dyn_bike_test 23/23
nav_dyn_spin_test 13/13
physics_dyn_bike_plant_test 10/10
physics_dyn_contact_test 7/7
nav_dyn_ph_full_test 8/8

clang-format 18 is clean on the new files.

…xactly

dyn_plant is first order (implicit Euler on (vy, r), a predicted then
corrected vx), so dyn_plant_books leaves its error, second order per step,
in the residual. diff::dyn_plant_dg (inc/cvc/nav/detail/dyn_bike_dg.h) is
the discrete gradient of the plant's port-Hamiltonian form: H is quadratic
in the twist, so the step is the implicit midpoint rule on it,
M (v1 - v) / hdt = J(M vbar) vbar + Q(vbar), with Q at the midpoint state
(the actuators' outputs at the step's midpoint). Then dH = hdt vbar^T Q
line for line, and dyn_plant_dg_books closes to the Newton solve's
tolerance. Its actuators, clamp and blend are dyn_plant's; the books charge
the limiters as dyn_plant_books does. dyn_plant and its callers are
untouched, so the defaults are byte-identical.

nav_dyn_bike_dg_test (the Humvee):
- the rate is dyn_ph_rhs's to 1.1e-15 m g; the actuators are dyn_plant's,
  bit for bit, in double and float;
- books, swerving past the peak at hdt 0.06 to 0.0025 s and braking to a
  stop: residual at most 1.7e-10 J a step (3.5e-14 to 1.3e-12 of the
  step's flow), against dyn_plant_books' 25 J to 0.056 J;
- second order: 3.92x to 4.03x smaller error per halving of hdt, where
  dyn_plant is first order (1.98x to 2.13x);
- stiff tires (1x, 4x, 16x) at 1 x 0.06 s and 6 x 0.01 s: no coasting
  step gains energy; a steady turn settles within 0.02% of the kinematic
  bicycle;
- cost: 3.3 to 3.6 us a step against dyn_plant's 0.34 to 0.42 us.

The midpoint rule is A-stable but not L-stable: a tire much stiffer than
the step swings its slip past zero and back, decaying. At the live plant's
6 x 0.01 s with the Humvee's tires it does not; the test prints the swings.
@transfix
transfix merged commit 789d2f6 into master Nov 7, 2026
27 of 28 checks passed
@transfix
transfix deleted the feat/dyn-bike-discrete-gradient branch November 7, 2026 01:03
transfix added a commit that referenced this pull request Nov 7, 2026
Bumps the project version and every recipe's upstream_version from 3.7.11 to 3.7.12. The v3.7.12 tag on this commit is the first release with the multi-car controller template (pycvc_gl.cars, #1031 #1035 #1040), the hidden-subtree fix in cvcGL (#1037), the discrete-gradient dyn_bike step (#978), semi-implicit Euler by default in the nav rollout (#981), the implicit contact damper (#1004, #1038), and generated_world's roads and water (#1032, #1034).
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