Skip to content

cvc::ph composition: compositional port-Hamiltonian systems (C-PHAST, Phase 1) - #1227

Merged
transfix merged 6 commits into
masterfrom
feat/ph-compose
Nov 15, 2026
Merged

transfix merged 6 commits into
masterfrom
feat/ph-compose

Conversation

@transfix

@transfix transfix commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Phase 1 of porting C-PHAST into libcvc. It is two commits: the core (library, CLI, pycvc, docs/PH_COMPOSE.md, self-contained tests), which can stand alone, then the parity layer (the plan, the phast fixture script, its golden file, ph_compose_parity_test). The plan is docs/roadmap/PH-COMPOSE-ROADMAP.md. This PR adds a cvc::ph composition layer: port-Hamiltonian components joined by power-preserving ports, the composed field, the paper's split transition with per-step energy books, parity with CVC-Lab/phast, pycvc bindings and cvc ph subcommands.

Licence: merged on Joe's OK with the authors' permission still pending

  • The method is C-PHAST, by Shubham Bhardwaj and Chandrajit Bajaj (UT Austin, CVC): C-PHAST: Compositional Port-Hamiltonian World Models for Structured Dynamics Transfer (2026). It extends PHAST (arXiv:2602.17998).
  • Their reference code, CVC-Lab/phast, is Apache-2.0. libcvc is LGPL-2.1-only.
  • The C++ here is written from the paper's equations. phast is only run, by the fixture script, and none of its source is copied. Even so, this is a port of their method.
  • Permission to license this port under LGPL-2.1, from Shubham Bhardwaj and from Dr. Bajaj / UT CVC-Lab: PENDING. Joe chose to merge before it was recorded (2026-11-15: "Merge anyway"). Record it here (who, when, where) when it arrives.
  • Attribution: docs/roadmap/PH-COMPOSE-ROADMAP.md, docs/PH_COMPOSE.md, the header of inc/cvc/ph/compose.h and the pycvc binding all credit Shubham Bhardwaj and the C-PHAST paper.

What it adds

inc/cvc/ph/compose.h, src/cvc/ph/compose.cpp

Piece What it is
block_law A component: energy, effort (∇H), structure (J, R), port_map (B), input_map (G), plus the local split maps kick, damp, conserve
linear_law H = ½ xᵀQx with constant J, R, B, G (lines, linear DGUs, the linear-quadratic theorems)
canonical_law PHAST's form H = V(q) + ½ pᵀM(q)⁻¹p, R = diag(0, D(q)). V is a quadratic plus an optional MLP; M and D are diagonal constants or softplus(MLP) + min. Leapfrog or Picard implicit midpoint.
composite Stacks blocks. connect pairs two ports, connect_incidence wires an electrical network (Eq. 23), connect_skew applies a dense skew rule. It gives the skew pairing C, the composed field (Eq. 11), the coupling field, the power terms and the readout.
step The split transition: substeps × (coupling half-step, each block's LocalStep, coupling half-step), with Euler (phast's) or explicit-midpoint (the paper's Eq. 71) coupling. Also whole-field RK4 and implicit-midpoint references.
step_books The Eq. 54 accounting, as libcvc's cvc::nav::diff::dyn_ph_books (dH, supplied, dissipated, residual)
rollout, check_balance, describe, write_csv Rollouts, the balance check, and the reports the CLI prints
load_system, load_law cvc-ph-system/1 and cvc-ph-law/1 JSON files

Around it

  • CLI: cvc ph info|simulate|check <system.json>. check exits 1 when the pairing does work or dH/dt departs from -loss + supplied.
  • pycvc: pycvc.ph_composite(path_or_json), with simulate, which returns a (steps+1, n) numpy array and the books, plus check, energy, effort, field, powers, pairing and block.
  • Docs: docs/PH_COMPOSE.md (model, schemes, books, file formats, CLI, Python) and docs/roadmap/PH-COMPOSE-ROADMAP.md (the plan: phases 1-5 and the mapping to the paper).

There is no process-static state. Laws are immutable and shared through shared_ptr, and a composite belongs to its caller.

Tests

ph_compose_test has 15 self-contained tests (no golden file), and ph_compose_parity_test has 3 tests against phast. CvcCliTest.PhInfoSimulateAndCheckAComposedGrid and test_pycvc_ph.py (21 checks) cover the CLI and Python.

Parity with phast (src/cvc/tests/data/ph_compose_golden.json, from src/cvc/tools/cphast_fixtures.py at phast 8b7edc86, float64, 200 steps of phast's Euler-coupled step):

Case Max state error
4 linear DGUs + RL lines on a chain (the paper's H.1 network, linear laws) 3.6e-15
The same network one second in, plus a 5th DGU and line at bus 0 / at bus 2 3.1e-15 / 3.6e-15
3 copies of a learned DGU law (MLP potential, MLP mass, MLP damping, Picard implicit midpoint) 6.7e-15

The learned law's efforts match phast's autograd to 1.7e-16.

Structure, whatever the laws (random heterogeneous systems with rectangular pairings and a dense skew rule):

  • Theorem 3.1: the pairing's net power is below 1e-15 (relative), and dH/dt = -loss + supplied to 1e-13.
  • The efforts are the energy's gradient, and the MLP Jacobians match finite differences.
  • The split step's energy residual per step falls as dt² with Euler coupling and as dt³ with midpoint coupling.
  • Against RK4, Euler coupling is globally first order and midpoint coupling second order.
  • On a linear system, the whole-field implicit midpoint closes its midpoint-rule books to 1e-12.
  • Rewiring the same law objects changes the trajectories.
  • Incidence signs follow the paper's Eqs. 23-25.
  • A DC microgrid written as a cvc-ph-system/1 file follows RK4 at dt/64 to 8.0e-4 (Euler coupling) and 3.4e-6 (midpoint), and so do the fifth-DGU candidates.
  • Malformed specs and files are refused with messages.

On the bus-2 candidate, 200 steps, the books' total residual is -2.5e-5 with midpoint coupling and +1.1e-4 with Euler.

Checked locally on Linux Release (GCC 13): ph_compose_test 16/16, the CLI ph tests and test_pycvc_ph.py pass. clang++-18 -Wall -Wextra is clean on the new files, and git clang-format reports nothing on the changed lines.

Findings about phast (for Shubham)

  1. phast's newest commit (2026-09-21) predates the paper's October electrical runs. It has no nonlinear DGU family, no explicit-midpoint coupling, no voltage-history fit and no certified ranking. Parity is therefore against what it does have; the rest is checked analytically, and Phase 2 adds a fine-step reference.
  2. The microgrid factory gives each line QuadraticPotential(k=0.0). phast's _softplus_inverse(0.0) returns 0.0, so k = softplus(0) = ln 2: a spring on the line's dummy charge, which leapfrog feeds into the line's flux. On the 4-DGU chain this moves the state by up to 6.4e-2 in 200 steps. The fixtures use an exact zero potential instead.
  3. IncidenceCouplingPort couples through q / C_node whatever the law, so learned DGUs are coupled through the reference capacitance rather than their own efforts (the paper's Eq. 203). The fixtures use CompositionalPHAST's existing needs_grad_q/p hook for the effort form.

CI note

The first CI run (on e7c84e4) failed only where master was broken: #1203 had left cmd_trace and the rich-text tests on the old signatures. A Windows runner also failed installing cvcpkg's tools. #1225 fixed master, and master is merged in as d5e6776; the PR's diff against master is still only its own 16 files. On the merged tree, locally (Linux Release, GCC 13): ph_compose_test 15/15, ph_compose_parity_test 3/3, CLI ph + trace 6/6, pycvc_ph.

Limits (Phase 1)

  • Mass and damping are diagonal (no Householder rank-1 terms).
  • No periodic coordinates.
  • Port and input maps are constant.
  • Picard stops per block.
  • The DC-microgrid study, the query (history fit, candidates, action cards, certified ranking) and training are Phases 2-4.

Port-Hamiltonian components joined by power-preserving ports: block laws with
typed ports (linear_law; canonical_law with optional MLP potential, mass and
damping), the skew pairing from explicit pairs, an incidence matrix or a dense
rule, the composed field and power terms, a split transition (Euler or
midpoint coupling) with whole-field RK4 and implicit-midpoint references,
per-step energy books in dyn_ph_books, cvc-ph-law/1 and cvc-ph-system/1
files, cvc ph info|simulate|check, and pycvc ph_composite.

The method is C-PHAST, by Shubham Bhardwaj and Chandrajit Bajaj.
docs/roadmap/PH-COMPOSE-ROADMAP.md maps the composition to the C-PHAST paper
(Shubham Bhardwaj and Chandrajit Bajaj) and plans phases 2-5.
src/cvc/tools/cphast_fixtures.py runs phast's own torch modules in float64
(it imports phast and copies none of it) and writes
tests/data/ph_compose_golden.json; ph_compose_parity_test replays its four
microgrids to rounding (3.1e-15 to 6.7e-15 over 200 steps).

This port of C-PHAST needs Shubham Bhardwaj's and UT CVC-Lab's permission to
license it LGPL-2.1 before it merges.
…asured (#1242)

ph_convoy_test builds a convoy's formation linkage as a cvc::ph composite: followers as
point-mass canonical blocks, links as linear storage blocks wired by connect_incidence. It
compares that formation with slot chasing and with the same links pulling one way, on a speed
step and an emergency stop, for a column and a wedge, and checks the composite against an RK4 of
the same equations.

docs/roadmap/PH-CONVOY-FORMATION.md has the numbers. The pairing keeps errors bounded at any
column length and any gains. It loses on peak error, settling, overshoot and closest approach,
both to slot chasing and to the one-way links with the same information. So convoys are no
reason for Phase 5's state-dependent port maps.

It also records a limit of the split step: a flow source fed to a line through its input lets
the line's state creep from the geometry at a steady state. The composite is therefore written
in the lead's frame.
@transfix

Copy link
Copy Markdown
Owner Author

Merging on Joe's OK (2026-11-15: "Merge anyway"). The authors' LGPL-2.1 permission (Shubham Bhardwaj; Dr. Bajaj / UT CVC-Lab) is still pending: record it on this PR when it arrives.

@transfix
transfix merged commit 8f4bc5e into master Nov 15, 2026
30 checks passed
@transfix
transfix deleted the feat/ph-compose branch November 15, 2026 23:50
transfix added a commit that referenced this pull request Nov 16, 2026
… ci]

Master after #1255's squash (0879fc4) and #1227. Delta unchanged; checked locally.
transfix added a commit that referenced this pull request Nov 16, 2026
…#1253, #1255, #1256, #1264, #1268, #1271, #1273, #1277)

- ci.yml: the sanitizer list keeps this branch's tests (stream_test, net_ws_client_test,
  ariadne_host_test) and master's segmentation_intrinsics_test.
- STATE_EXEC_THREADED.md: the room-call verbs (room-run is #1273's) and #1252's segment.seeded
  provider in the provider table, both registered without a kind. room-call's async functions
  stay on the app's host: a reply settles on the relay client's reader thread through
  host.finish, so moving them to *ictx.host needs a finisher first.
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