Skip to content

cvc::ph queries: the C-PHAST query, from measured history to a certified ranking (Phase 3) - #1287

Merged
transfix merged 16 commits into
masterfrom
feat/ph-query
Nov 17, 2026
Merged

transfix merged 16 commits into
masterfrom
feat/ph-query

Conversation

@transfix

Copy link
Copy Markdown
Owner

Phase 3 of the C-PHAST port (docs/roadmap/PH-COMPOSE-ROADMAP.md): the query. It estimates a system's present state from what its sensors measured, gives each candidate change its start, rolls the candidates out, and ranks them with error bounds.

Stacked on #1265 (base feat/ph-microgrid), which has squash auto-merge on. This merges after it, on Joe's OK. The C-PHAST licence note on #1227 applies here too: the authors' LGPL-2.1 permission is still pending.

What it adds (inc/cvc/ph/query.h, src/cvc/ph/query.cpp)

  • fit_history: the present state from a measured history. This is the paper's voltage-history fit (Eq. 204).
    • It fits the physical state at the window start: prior mean + scale · ξ, through storage_from_efforts.
    • It runs the window's inputs through the frozen transition and minimizes the weighted misfit of the measured channels, plus |ξ|².
    • The solver is Levenberg–Marquardt from ξ = 0, with a central-difference Jacobian and at most 20 evaluations.
    • It reports success, optimality, the data Jacobian's rank, and admitted() (success, optimality ≤ 1e-4, full rank). It also gives a Laplace posterior, which posterior_states samples.
  • consequences: each step's readout, stored energy, loss and external powers, and the step's work, loss and residual (Eq. 19).
  • make_card: an action card per candidate. Each line is max_abs, min, max or final_value over chosen readout channels, or total_loss, total_work or max_residual over the books.
  • rank_candidates: the certified ranking (Eq. 45, Prop. F.5).
    • A candidate is admitted when every limit holds with its bound (g + dg ≤ 0), rejected when one fails without it, and unresolved otherwise.
    • a is ordered before b only when J(a) + dJ(a) < J(b) − dJ(b).
    • The outcome is certified, inconclusive or abstain, with the best's 2·max dJ suboptimality bound.
  • sampled_bounds: sampled bounds from an ensemble of starts and laws, plus the transition's own error against RK4. They are evidence, not a proof.
  • Microgrid helpers: voltage_channels, dgu_current_channels, line_current_channels, and microgrid_prior (the studies' 1 ± 0.4 V, 0.1 ± 0.3 A, 0 ± 0.2 A).
  • pycvc: ph_composite.fit_history(...), ph_composite.card(...) and pycvc.ph_rank(...).
  • Docs: the "Queries" section of docs/PH_COMPOSE.md and the roadmap's Phase 3.

Measured (ph_query_test)

The network is the four-DGU chain of reference DGUs. The history is one second of bus voltages sampled every 0.05 s, and the truth is RK4 at dt/16.

forecast from the fitted state voltage RMSE DGU current RMSE line current RMSE
0.5 s 0.29 mV 26 mA 22 mA
2 s 0.74 mV 18 mA 15 mA
C-PHAST Table 8 (numerical history estimate), 0.5 s 0.36 mV 26 mA 25 mA
the same, 2 s 1.0 mV 18 mA 18 mA

The paper's networks are five-DGU chains, stars and cycles, with learned laws, so the agreement is in magnitude.

  • Observability: a three-DGU tree has full rank from its voltages. A symmetric cycle with equal line decay rates loses one rank (a circulating current) and is not admitted.
  • The candidate query: a fifth DGU at bus 0 or at bus 2, with a limit of 0.5 A on the peak line current, then the lowest loss.
    • From voltages alone it picked the physical best in 2/4 episodes and certified 0. The candidates' losses differ by less than the hidden currents' error, so the bounds overlap.
    • Measuring the full state, it picked the best in 4/4 and certified 1.
    • No certificate was wrong.

Tests

  • ph_query_test, 7 tests (about 8.5 s):
    • the fit's forecasts;
    • a noisy fit (20 mV);
    • tree vs symmetric cycle;
    • history validation;
    • consequences against the books;
    • the ranking's admit, order, inconclusive and abstain cases;
    • the whole query in both sensing modes.
  • test_pycvc_ph.py gains a query section; it now runs 35 checks.

Checked locally on Linux Release, GCC 13:

  • ph_query 7/7, ph_microgrid 8/8, ph_compose 15/15, parity 3/3, convoy 3/3, CLI ph 6/6, pycvc_ph;
  • clang++-18 -Wall -Wextra clean on the new files;
  • git clang-format clean;
  • the writable-data scan finds nothing new.

Not here

  • A cvc ph query subcommand: it needs a query file format.
  • The paper's analytic error bounds (Props. F.7/F.8) in place of sampled ones.

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.
Capacitor curves on canonical_law (the reference DGU's q = 2.2 (v + v^3/3)),
storage_from_efforts (physical voltages and currents to charges and fluxes),
assign_state (a candidate's start), law_json / system_json, and
cvc/ph/microgrid.h: DGU and line presets, make_microgrid, trajectory errors.
cvc ph microgrid and simulate --physical; pycvc storage, system_json and
ph_microgrid_json.

The candidate study (four-DGU chain, a fifth DGU at bus 0 or bus 2, 16
episodes) against RK4 at dt/16: midpoint coupling 5.1e-7 V voltage RMSE and
5.7e-8 A peak line-current error, Euler 7.9e-5 V and 2.4e-4 A; both order all
16 candidate pairs.
…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.
…crogrid

# Conflicts:
#	src/cvc/tests/CMakeLists.txt
# Conflicts:
#	bindings/pycvc/pycvc_ph.i
#	bindings/pycvc/test_pycvc_ph.py
#	docs/PH_COMPOSE.md
#	docs/roadmap/PH-COMPOSE-ROADMAP.md
#	inc/cvc/ph/compose.h
#	src/cvc-cli/cvc_cli_test.cpp
#	src/cvc-cli/main.cpp
#	src/cvc/CMakeLists.txt
#	src/cvc/ph/compose.cpp
#	src/cvc/tests/CMakeLists.txt
…certified ranking (Phase 3)

fit_history estimates a system's present state from measured channels over a
window (the C-PHAST voltage-history fit: Levenberg-Marquardt over the
prior-scaled physical state at the window start, through the frozen
transition), with success, optimality, data-Jacobian rank and a Laplace
posterior; posterior_states samples it. consequences reads each step's
readout and energy channels; make_card summarizes a candidate; rank_candidates
admits candidates by limits with their bounds and orders them only where the
bounds separate (certified, inconclusive or abstain); sampled_bounds gives
sampled bounds. Microgrid helpers: the readout channels and the studies' prior.

On the four-DGU chain, from clean voltages: forecast RMSE at 0.5 s 0.29 mV,
26 mA (DGU), 22 mA (line), against the paper's 0.36 mV, 26 mA, 25 mA; a
symmetric cycle loses one rank and is not admitted; no certificate is wrong.
Base automatically changed from feat/ph-microgrid to master November 16, 2026 07:43
# Conflicts:
#	bindings/pycvc/pycvc_ph.i
#	bindings/pycvc/test_pycvc_ph.py
#	docs/PH_COMPOSE.md
#	docs/roadmap/PH-COMPOSE-ROADMAP.md
#	inc/cvc/ph/microgrid.h
#	src/cvc/CMakeLists.txt
#	src/cvc/ph/microgrid.cpp
#	src/cvc/tests/CMakeLists.txt
@transfix
transfix enabled auto-merge (squash) November 16, 2026 19:39
# Conflicts:
#	src/cvc/tests/CMakeLists.txt
@transfix
transfix merged commit 2d03ad1 into master Nov 17, 2026
29 checks passed
@transfix
transfix deleted the feat/ph-query branch November 17, 2026 12:23
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