Skip to content

cvc::ph microgrids: the C-PHAST DC microgrid study (Phase 2) - #1265

Merged
transfix merged 11 commits into
masterfrom
feat/ph-microgrid
Nov 16, 2026
Merged

transfix merged 11 commits into
masterfrom
feat/ph-microgrid

Conversation

@transfix

Copy link
Copy Markdown
Owner

Phase 2 of the C-PHAST port (docs/roadmap/PH-COMPOSE-ROADMAP.md): the DC microgrid study, end to end, on cvc::ph composition.

Stacked on #1227 (base feat/ph-compose). It merges only after #1227, and only on Joe's OK. #1227's licence note applies here too: the authors' LGPL-2.1 permission is pending, and nothing merges until Joe confirms it.

What it adds

  • A capacitor curve on canonical_law. The charge is an odd increasing polynomial of the voltage, q = sum_j c_j v^j, with energy C(q) and dC/dq = v; the voltage is found from the charge by Newton. The paper's reference DGU capacitor is q = 2.2 (v + v^3/3). JSON: "potential": {"capacitor": [...]}.
  • storage_from_efforts. Physical voltages and currents to charges and fluxes, block by block: Newton with a central-difference Jacobian, each update capped at 2, at most 30 iterations, to 1e-9. It reports convergence, iterations and residual.
  • assign_state. A candidate's start: blocks the actual system has keep their state, and added blocks take supplied states, by name.
  • law_json / system_json. The formats written out, every number to 17 digits, each shared law once, and the pairing as pair couplings. A file read back gives the same composite.
  • cvc/ph/microgrid.h. The presets linear_dgu, nonlinear_dgu, reference_dgu (q = 2.2 (v + v^3/3) C, 1.8 H, 1.2 ohm) and rl_line. Also make_microgrid (blocks dgu* and line*, wired by incidence), physical_state, peak_line_current, and compare_trajectories (voltage, DGU-current and line-current RMSE, and the peak line-current error).
  • CLI and Python.
    • cvc ph microgrid <out> writes a network file; cvc ph simulate --physical ... starts from voltages and currents.
    • pycvc gains ph_composite.storage(), ph_composite.system_json() and pycvc.ph_microgrid_json(...).
    • docs/PH_COMPOSE.md documents all of it.

The candidate study (ph_microgrid_test)

The paper's network with its reference DGU laws, over 16 episodes:

  1. A four-DGU chain (lines 0.3/0.4/0.5 ohm, 0.8 H) starts from a sampled physical state and runs for one second.
  2. The same DGU law and a 0.4 ohm / 0.8 H line are added at bus 0 or at bus 2.
  3. Each candidate runs 200 steps of 0.01 s.

phast has no counterpart for this DGU, so the reference is RK4 at dt/16, which gives the same scores to six digits as dt/64. Since the laws are known, these errors are the split step's own. Averaged over 32 trajectories:

coupling voltage RMSE DGU current RMSE line current RMSE peak line current error orderings by peak
Euler 7.9e-5 V 2.0e-5 A 2.0e-4 A 2.4e-4 A 16 / 16
midpoint 5.1e-7 V 7.1e-7 A 2.9e-7 A 5.7e-8 A 16 / 16

The worst single-step energy residual is 1.2e-5 J with Euler coupling and 1.7e-7 J with midpoint coupling.

Not here: the paper's learned DGU family (2,407 parameters). It is not in phast, and would come with Shubham's checkpoints (Phase 4).

Tests

ph_microgrid_test has 8 tests (about 2.5 s):

  • the capacitor curve inverts, and a linear curve equals the quadratic capacitor;
  • the nonlinear network balances its power (check_balance_random);
  • storage inverts sampled physical states, and says when it stops short;
  • assign_state keeps existing blocks, adds supplied ones and refuses missing or wrong-sized states;
  • a system with every field of both law kinds round-trips through system_json, with equal energy and field;
  • the candidate study.

Also covered: CvcCliTest.PhMicrogridWritesANetworkAndStartsFromVoltages, and 6 more checks in test_pycvc_ph.py.

Checked locally on Linux Release, GCC 13:

  • ph_microgrid_test 8/8, ph_compose_test 15/15, ph_compose_parity_test 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.

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
@transfix
transfix changed the base branch from feat/ph-compose to master November 15, 2026 23:50
# 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
@transfix
transfix enabled auto-merge (squash) November 16, 2026 00:02
@transfix
transfix merged commit 5c731c5 into master Nov 16, 2026
29 checks passed
@transfix
transfix deleted the feat/ph-microgrid branch November 16, 2026 07:43
transfix added a commit that referenced this pull request Nov 16, 2026
#1265 added cvc_discover_tests(ph_microgrid_test) beside ph_convoy_test's
line; both stay.
transfix added a commit that referenced this pull request Nov 16, 2026
…#1307)

One conflict, in ariadne_runtime_test.cpp's ActionCapsAreTreeSettingsAndChargeAsyncCalls: this
branch cut its loop to 20 rounds so it fit the 0.1 s time cap on Linux debug CI, and master
(#1282) rewrote the test, raising the time cap to 30 s wherever the loop must finish.
Master's fix covers the same flake, so the test is master's as it stands, 200 rounds again.

tools/singleton_scan is master's (#1276); the auto-merged net, CI, test and doc files keep both
sides.
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