Skip to content

Torch twins of the dynamic plant's wheel spin, compliant contact and full (q, p) pH form - #146

Merged
transfix merged 4 commits into
mainfrom
feat/dyn-wheel-spin-twin
Nov 3, 2026
Merged

transfix merged 4 commits into
mainfrom
feat/dyn-wheel-spin-twin

Conversation

@transfix

@transfix transfix commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

What

Torch twins of three opt-in additions to libcvc's dynamic vehicle plants. Each is held to a libcvc golden by a parity test, under the DOUBLE contract at 1e-9 relative, with gradients by autograd against libcvc's forward-mode duals:

  1. Wheel spin (dyn_bike, dyn_4w: opt-in wheel spin in pH form; plant logs carry the wheels; cvc::ph potential and input options transfix/libcvc#982): single track and four wheels.
  2. Compliant contact (dyn_bike_plant: compliant contact in pH form against sim_world's field and between vehicles; plant logs keep contact windows transfix/libcvc#983): the plant's external port.
  3. The full (q, p) port-Hamiltonian form, with the single track's slope across its heading and gravity as a potential in H (the libcvc PR stacked on #983).

Nothing changes without the new options or functions: the existing parity files pass unchanged.

The goldens come from those libcvc branches. Their libcvc PRs are open, so this one should merge after them, in case review changes a golden.

1. Wheel spin (grl_snam.dyn_bike, grl_snam.dyn4)

  • Single track:
    • dyn_spin_plant: the state plus om_f, om_r;
    • dyn_spin_ph_parts_at, dyn_spin_ph_rates_at, dyn_spin_ph_hamiltonian, dyn_spin_plant_books;
    • DynBikeParams gains r_w, J_f, J_r, B_x, C_x.
  • Four wheels:
    • dyn4_spin_plant and its form and books, on any ground;
    • Dyn4Params gains the wheels and libcvc's drive_gain, brake_gain, resist_comp;
    • grade_force is recorded but not twinned: a plant that sets it raises.
  • Parity: tests/test_dyn_spin_ph_parity.py and tests/test_dyn4_spin_ph_parity.py.
worst |torch − libcvc| / max(1, |·|) single track four wheels
parts / wheels 4.5e-15 1.2e-16
rates 4.9e-16 1.4e-16
next 0 2.1e-16
free twist 0 5.6e-17
books 0 7.3e-16
gradients 4.4e-14 7.2e-13

2. Compliant contact (grl_snam.dyn_contact, new)

  • Single vehicle:
    • dyn_footprint_points samples the outline about the centre of gravity;
    • dyn_contact_field gives each point within a field's skin:
      • a barrier energy ½k(s − d)²;
      • a Hunt–Crossley damper on its normal velocity;
      • smoothed Coulomb friction.
        These come out as the barrier's and the damper's body wrenches, the energy, the dissipation and the points touching.
  • Between vehicles: dyn_rounded_box_sdf and dyn_contact_pair do the same, both ways.
  • The plant's port: dyn_plant / dyn_spin_plant take an optional wrench, the external port held over the step. Their books gain a port line, dyn_ph_port_work.
  • Parity (tests/test_dyn_contact_parity.py), worst difference 0 in every group:
    • the points;
    • five field cases, now all touching: 4, 1, 1, 1, 2 points;
    • three pair cases;
    • the step's next state, books (port included) and free twist.
  • Structure:
    • the barrier is minus autograd's gradient of its energy to 2.8e-16, over 157 poses in the skin;
    • between vehicles, to 2.0e-15 over 254 touching pairs;
    • the world forces and moments cancel;
    • the damper's power is minus its dissipation.

3. The full (q, p) form and the side slope (grl_snam.dyn_bike)

H(q, p) = 1/2 p^T M^-1 p + m g h(centre of gravity)
q' = T(q) v,   p' = -T(q)^T dV/dq + (J(p) - R) v + G u + f_eps + f_g
  • The form:
    • dyn_ph_T;
    • dyn_ph_rhs, the momentum-only field;
    • dyn_ph_rhs_full: the slopes as data, f_g, or ground= with gravity in H.
  • The ground is a callable ground(x, y) -> height:
    • dyn_ph_slope, dyn_ph_potential, dyn_ph_grad_q;
    • dyn_ph_hamiltonian_ground, dyn_ph_rates_ground.
  • The plant:
    • dyn_plant(..., side=) takes the slope across the heading;
    • dyn_plant_ground and dyn_plant_books_ground apply gravity along and across the heading. The books' grade line is −ΔV.
  • The slopes are the ground's gradient at the centre of gravity (the tangents). That makes m g h the potential of the planar model. libcvc's PR explains why the sine would leave m g (tan − sin) unbooked.
  • Parity (tests/test_dyn_ph_full_parity.py, against dyn_ph_full_golden.json: the Humvee on a hillside at six states):
group worst relative difference
slopes, V, H 6.9e-18
dV/dq 0
T(q) 2.1e-16
q', p' (on the ground) 1.6e-16, 2.4e-16
p' (slopes as data) 8.3e-16
rates 7.9e-16
next state 1.4e-17
books 2.1e-14
gradients (through the ground) 3.2e-11
  • Torch-only gates:
    • the structure [[0, T], [-T^T, J(p)]] is skew, moves no energy, and satisfies Jacobi by autograd (defect below 1e-12);
    • the pose update is q1 = q0 + hdt T(q1) v1 to 1e-13 m, on the ground and flat;
    • closed loop across the hillside with the body slipping: with gravity in H, the books close (|Σ|/flow < 1e-3, about 4× smaller per halving of the step), while the same steps' momentum-only books leave over 30× that unbooked.

Tests

  • pytest tests/test_dyn_*parity.py tests/test_dyn4_*parity.py: 113 passed.
  • ruff and black are clean.
  • The whole suite: see the comment below.

The goldens are byte-identical to libcvc's: dyn_spin_ph, dyn4_spin_ph and dyn_contact from 7b862ca0; dyn_ph_full from 9dda6dd3.

… and four wheels

dyn_bike: dyn_spin_plant (the state plus om_f, om_r), dyn_spin_ph_parts_at, dyn_spin_ph_rates_at,
dyn_spin_ph_hamiltonian and dyn_spin_plant_books, twins of libcvc's opt-in wheel spin
(dyn_spin_*), with DynBikeParams' r_w, J_f, J_r, B_x, C_x. dyn_plant and dyn_ph_parts_at share
the step and the lateral parts with them, split as libcvc's dyn_plant_core and
dyn_ph_parts_lateral are; their arithmetic is unchanged.

dyn4: dyn4_spin_plant (the state plus the four wheels' speeds), dyn4_spin_ph_parts_at,
dyn4_spin_ph_rates_at, dyn4_spin_ph_hamiltonian (and dyn4_ph_hamiltonian) and
dyn4_spin_plant_books on any ground. Dyn4Params gains the wheels' r_w, J_f, J_r, B_x, C_x and
libcvc's drive_gain, brake_gain, resist_comp (the drive's target, a_cmd itself at the defaults)
and grade_force, which is not twinned: a plant that sets it raises.

Parity against libcvc's dyn_spin_ph_golden.json and dyn4_spin_ph_golden.json (copied from libcvc
6215e6ae), DOUBLE contract, 1e-9 relative. Measured worst |torch - libcvc| / max(1, |torch|,
|libcvc|):

                 single track   four wheels
  parts/wheels   4.5e-15        1.2e-16
  rates          4.9e-16        1.4e-16
  next           0              2.1e-16
  free           0              5.6e-17
  books          0              7.3e-16
  gradients      4.4e-14        7.2e-13

The books' step is the plant's to the last bit in every case.
…external port

dyn_contact (new): the twin of libcvc's dyn_contact.h. dyn_footprint_points samples the outline
about the centre of gravity; dyn_contact_field gives each point within the skin of a field a
barrier energy 1/2 k (s - d)^2, a Hunt-Crossley damper on its normal velocity and smoothed
Coulomb friction along the obstacle, as the barrier's and the damper's body wrenches (Wc, Wd),
the energy, the dissipation and the points touching; dyn_rounded_box_sdf and dyn_contact_pair do
the same between two vehicles, both ways. A body is a (..., 6) tensor and a field a callable
(phi, gx, gy), with the golden's analytic wall and pillar; autograd differentiates it all.

dyn_bike: dyn_plant and dyn_spin_plant take an optional keyword wrench, the external port held
over the step (joining the resistances along the body and the lateral solve's force and moment,
as libcvc's dyn_plant_core does); dyn_plant_books and dyn_spin_plant_books book its energy as a
port line (dyn_ph_port_work) and leave the residual net of it. Without a wrench nothing changes.

Parity against libcvc's dyn_contact_golden.json (copied from libcvc b64921cb), DOUBLE contract,
1e-9 relative: the footprint points, the five field cases, the three pair cases, and the plant
step's next state, books (port included) and free twist all agree exactly (worst difference 0).
The step's plant is DynBikePH.Golden's Humvee, whose parameters the golden does not repeat; they
come from dyn_bike_ph_golden.json. Structural gates in torch: the barrier's wrench is minus
autograd's gradient of its energy in the pose to 2.8e-16 over 153 poses in the skin of a wall and
a pillar; between vehicles the world forces and moments cancel, and the barrier matches the
pair's energy's gradient to 2.0e-15 over 254 touching pairs; the damper's power is minus its
dissipation.
… corner radius held

The contact golden is libcvc 7b862ca0's: every field case now has points in the skin (touching 4,
1, 1, 1, 2 against the wall and the pillar, where the wall's were all out of reach), and the plant
step records its vehicle's parameters, so the test reads them there rather than from
dyn_bike_ph_golden.json. dyn_rounded_box_sdf holds the corner radius to [0, min(half width, half
length)], as libcvc's now does; no golden value depends on it, and a test pins it.

Parity, DOUBLE contract, 1e-9 relative: the footprint points, the field and pair cases, and the
step's next state, books (port included) and free twist agree exactly (worst difference 0). The
barrier is minus autograd's gradient of its energy to 2.8e-16 over 157 poses in the skin, and a
pair's barrier to 2.0e-15 over 254 touching pairs.
libcvc's dyn_ph_T, dyn_ph_rhs_full, the ground overloads (the slopes, the
potential m g h at the centre of gravity, dV/dq, the rates) and
dyn_plant_ground / dyn_plant_books_ground: with the pose in the state,
q' = T(q) v and p' = -T(q)^T dV/dq + (J(p) - R) v + G u + f_eps, gravity in
H on a ground and the books' grade line -dV. dyn_plant takes the optional
slope across the heading (`side`), and dyn_ph_rhs the momentum-only field.

tests/test_dyn_ph_full_parity.py holds them to libcvc's
dyn_ph_full_golden.json (libcvc 9dda6dd3, DynPHFull.Golden: the Humvee on a
hillside at six states) to 1e-9, the books' gradient by autograd through the
ground; and checks, in torch alone, that the structure is skew and Poisson
(Jacobi by autograd), that the pose update is q1 = q0 + hdt T(q1) v1, and that
side-slope books close to second order with gravity in H while the
momentum-only books leave the lateral component's work unbooked.
@transfix

transfix commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

I ran the whole suite on Windows (Python 3.12, torch CPU), on the branch rebased onto main 2cf3230: 639 passed, 61 skipped, 7 failed. None of the failures touches this PR's files. Each comes from this machine's environment:

  • test_material_raster.py (5): ModuleNotFoundError: No module named 'imageio'.
  • test_plant_capture.py::test_plant_names: this pycvc has no physics bindings (nav_plant, dyn_bike_plant).
  • test_material_gate_sim.py::test_direction_table_is_exact_and_matches_math: an exact comparison 0.7071067811865475 == math.sin(pi/4), which Windows' libm returns as ...476.

The parity files (tests/test_dyn_*parity.py, tests/test_dyn4_*parity.py) pass: 113 tests. ruff and black are clean.

@transfix
transfix merged commit b6ddb80 into main Nov 3, 2026
4 checks passed
transfix added a commit that referenced this pull request Nov 3, 2026
…e slope, and compliant contact (#151)

* docs(vehicle-ph-plant): wheel spin, the full (q, p) form with the side slope, and compliant contact

A new section covers libcvc #982 (wheel spin: the wheels' momenta in the state, a
(wheel torque, wheel speed) port, the longitudinal slip's secants in R), #987 (the
full form with gravity in H, tangents not sines, the four-wheel full form) and
#983 (a barrier energy in H, the damper and friction in R, through the external
port; against the rigid-body world; contact windows in the logs), with three
validation tables measured on libcvc master 3d5c15c6. The learned plant gains the
potential's form (#982). The review gaps now describe actuation and collisions as
built, and Proposition 1's pose update has its own test. GRL-SNAM #146's twins
get their own table, and the twins' parity tests move to a table of their own.
At the default substep a 7.8 m/s impact overshoots the contact's damper; the
paper says so. Three passes of pdflatex: no warnings, 48 pages.

* docs(vehicle-ph-plant): cite libcvc #1002 for the pycvc binding of the new options
transfix added a commit that referenced this pull request Nov 7, 2026
* release: 0.3.0

95 commits since v0.2.1 (PRs #66 to #158), with new features, so the
pre-1.0 minor version goes up. The main changes:

- Ariadne hosts: GRL-SNAM's demos run as Python hosts of Ariadne documents
  (#147, #149, #153). `grl-snam ariadne cars` runs libcvc's multi-car
  template (#155), and fleet and live take its settings, worlds, plants,
  panels and log (#157). These need pycvc-gl 3.7.12.
- Vehicle plants: capture, the demos, the fog-of-war stories and training
  run on kinematic, dynamic and Jolt plants (#119, #130, #143, #145, #154).
- Torch twins of libcvc, held to its goldens: the material rollout (#118),
  terrain (#120), dyn_bike and dyn4 (#121, #123, #134-#136), the external
  force (#122, #124), the material trainer (#125), belief material
  (#128, #129, #131), U_nav (#133), and the port-Hamiltonian form, its
  energy books, wheel spin and compliant contact (#138, #139, #146).
- The vehicle-plant port-Hamiltonian whitepaper (#137 to #158).
- Training and evaluation: the base scorecard and selection (#82-#98),
  `grl-snam coef-train` (#102), the learned lam head and .cvcnav v2
  (#94, #105), and the 2026-10 audit fixes (#113).
- Data: grl-snam-weights, plus the Austin scene and Humvee as runtime
  dependencies, so `grl-snam demo` runs from a bare install (#72-#77).
- The research core moves under grl_snam/ (#103). The world clock is
  shared with libcvc's (#116, #117).

The version lives in pyproject.toml. The three cvcpkg columns mirror it in
upstream_version and in the package.files dist-info path. cvc_revision goes
back to 1 for the new upstream version. The publish job asks the server for
the next revision in any case.

* material_train_native: pass semi_implicit=False explicitly (libcvc #981)

libcvc #981 (in 3.7.12) changed nav_material_trainer_create's
ms_semi_implicit default from 0 to 1. MaterialTrainer left the L_multi
sampling args out for the legacy single start, so on pycvc-gl 3.7.12
semi_implicit=False quietly ran semi-implicit Euler. #159's CI was the
first run on 3.7.12, and four tests failed on cp311, cp312 and cp313:

- test_sampled_multi_start_args: "semi_implicit did not reach the
  rollout", because the default and semi_implicit=True were now the same
  rollout;
- test_live_native_trainer_agrees[legacy] and [legacy+ext], and
  test_live_native_trainer_agrees_on_the_belief_net[surrogate]: the
  explicit torch trainer and the native one differed by 1.3e-3 to 1.5e-3
  (tolerance 1e-5).

Now a binding that takes the args always gets them, so the integrator is
the one asked for on any pycvc. An older binding still gets the legacy
start through the old signature. GRL-SNAM's default stays explicit Euler.

tests/test_material_train_native_args.py checks both bindings. Its new
case fails without this change.
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