Skip to content

AgentiGrid: MATPOWER reader/writer hardening (8 defects) - #150

Merged
ahajdar merged 2 commits into
amir/fix-genfuel-terminatorsfrom
amir/parser-hardening
Oct 21, 2026
Merged

ahajdar merged 2 commits into
amir/fix-genfuel-terminatorsfrom
amir/parser-hardening

Conversation

@ahajdar

@ahajdar ahajdar commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Replaces #138 (fork PR, closed so CI can run).
Depends on #149 — review only the last two commits.

Merge request type

  • New feature
  • Resolves bug
  • Documentation
  • Other

Relates to

  • OPFLOW
  • SOPFLOW
  • SCOPFLOW
  • TCOPFLOW
  • CMake build system
  • Spack configuration
  • Manual
  • Web docs
  • Other (AgentiGrid)

This MR updates

  • Header files
  • Source code
  • CMake build system
  • Spack configuration
  • Web docs
  • Manual
  • Other (AgentiGrid tests, claude_code_changes.md)

Summary

Follow-up to #137 (}; terminator for cell arrays), as agreed: same two files, found while writing an independent spec for the MATPOWER reader and writer.

Depends on #137. Until that merges, this branch also shows PR-A's two commits; please review only the last two ("reject malformed input, stable round trips" and "exact near-max floats, idempotent rounding, baseMVA literals"). Once #137 is merged I'll rebase this branch onto develop so only these two commits remain.

Defects fixed

# Before After
1 A row with a non-numeric value was skipped with only a log warning, so a bus, generator or branch could silently disappear. Too-short rows crashed with IndexError. ValueError naming the section and row for non-numeric values, wrong column counts, and non-integral values in integer columns.
2 Valid MATLAB syntax was misread: two rows on one line became one garbage row, and comma-separated rows were all dropped. ValueError with a clear message. The same applies to ... continuations, unterminated blocks and a missing mpc.bus.
3 The writer crashed on Inf/NaN, which the reader accepts. Written as Inf/-Inf/NaN.
4 Every read/write round trip added another header line. Writing is idempotent.
5 Case version '1' was accepted even though ExaGO reads version 2. ValueError. A missing version still means '2'.

Also: CRLF input reads the same as LF and output is always LF. The function mpc = … line always carries casename.

Compatibility

All 29 case files in datafiles/ (from case5 to case_ACTIVSg10k, including the CRLF ones) still parse and round-trip to equivalent networks. Sections other than bus, gen, branch and gencost (areas, dcline, …) are kept as opaque text and are not validated.

Tests

The new tests/grid_files/test_matpower_hardening.py has 29 tests; 22 of them fail without the fix. The existing suite still passes, and PR-B applies cleanly on top.

ahajdar and others added 2 commits October 20, 2026 06:08
…nd trips

Five defects found while specifying the parser and writer:

1. Rows with a non-numeric value were skipped with only a log warning, so a
   bus, generator or branch could silently disappear; too-short rows crashed
   with IndexError. Known sections (bus, gen, branch, gencost) now raise
   ValueError naming the section and row for non-numeric values, column
   counts outside the section's limits, and non-integral values in integer
   columns.
2. Valid MATLAB syntax was misread: two rows on one line became one garbage
   row and comma-separated rows were all dropped. These, '...' continuations,
   unterminated blocks and a missing mpc.bus now raise ValueError.
3. The writer crashed on Inf/NaN, which the reader accepts. They are written
   as Inf/-Inf/NaN; bus solution columns no longer lose precision to .4f.
4. Every read/write round trip added another "MATPOWER Case Format" header
   line. Writing is now idempotent.
5. Case version '1' was accepted although ExaGO reads version 2; it now
   raises. A missing version still means '2'.

CRLF input is normalised on read and output is always LF. The function line
always carries network.casename. All 29 case files in datafiles/ still parse
and round-trip to equivalent networks. New
tests/grid_files/test_matpower_hardening.py (29 tests, 22 fail without the fix).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rounding, baseMVA literals

Three defects found by property-based round-trip testing:

1. A finite value near the float maximum was written with 10 significant
   digits that round past it and read back as Inf. The writer now falls back
   to the exact representation when rounding overflows.
2. A value that rounds to an integer (e.g. 9999999999.5) was written as
   "1e+10" the first time and "10000000000" after re-reading, so writing was
   not idempotent. The integer form is now decided on the rounded value.
3. mpc.baseMVA was read with a digits-and-dots pattern: "1e2" (and "1e+15",
   which the writer itself emits for large values) was silently read as 1,
   and a negative value was reported as missing. Any numeric literal (sign,
   exponent, Inf/NaN) on an mpc.baseMVA line is now accepted, text in
   comments is ignored, and a non-numeric value raises ValueError.

18 more tests in tests/grid_files/test_matpower_hardening.py (12 fail
without the fix). All 29 case files in datafiles/ still round-trip.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ahajdar ahajdar changed the title Replaces #138 (fork PR, closed so CI can run)- Depends on #149 AgentiGrid: MATPOWER reader/writer hardening (8 defects) Oct 20, 2026
@ahajdar
ahajdar merged commit acecb0d into amir/fix-genfuel-terminators Oct 21, 2026
9 of 11 checks passed
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