Skip to content

Normalize periodic box vectors passed as a nested array of Quantities - #5487

Merged
peastman merged 2 commits into
openmm:masterfrom
barlowa124:fix/topology-box-vectors-array-of-quantities
Oct 12, 2026
Merged

peastman merged 2 commits into
openmm:masterfrom
barlowa124:fix/topology-box-vectors-array-of-quantities

Conversation

@barlowa124

@barlowa124 barlowa124 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

The check only looked at the first element, nested Quantity arrays skipped normalization.


The section below was generated with AI assistance (Devin) and checked locally before submission.

Fixes #3265.

Topology.setPeriodicBoxVectors checked only whether the first element was a Quantity, so a NumPy object array whose elements are all Quantity objects was stored unnormalized. PDBFile.writeFile then formatted bare Quantity objects as CRYST1 edge lengths and raised TypeError from Quantity.__float__.

The setter now recognizes a nested sequence of Quantities and collapses it into a single Quantity wrapping a numeric matrix before the existing validation and storage. Plain numeric sequences and proper Quantity inputs are handled exactly as before.

Added test_BoxVectorsArrayOfQuantities to TestPdbFile.py, which builds the issue's box-vector shape and asserts the CRYST1 edge lengths after a PDBFile.writeFile round trip. The new test raises the reported TypeError on the unfixed code and passes with the change.

The issue's reproduction script now writes a correct CRYST1 record, and neighboring input forms behave as before (proper Quantity input, plain numeric list, invalid reduced-form input still raising ValueError). The full Python test suite was not run since a local Python-wrapper build was not available.

Topology.setPeriodicBoxVectors checked only the first element's type, so a
NumPy object array whose elements are Quantities was stored unnormalized.
PDBFile's CRYST1 record then formatted bare Quantities as edge lengths and
raised TypeError.  Collapse such inputs into a single Quantity over a plain
numeric matrix before the existing validation.

Fixes openmm#3265.
@barlowa124
barlowa124 marked this pull request as ready for review October 5, 2026 16:17
@peastman

peastman commented Oct 6, 2026

Copy link
Copy Markdown
Member

The new test case is failing on CI:

    def assertVecAlmostEqual(self, p1, p2, tol=1e-7):
>       unit = p1.unit
               ^^^^^^^
E       AttributeError: 'list' object has no attribute 'unit'

@barlowa124

Copy link
Copy Markdown
Contributor Author

Fixed in 83385bb. The new test was passing plain lists to assertVecAlmostEqual, which expects Quantities. It now asserts the CRYST1 edge lengths directly. Ran it locally against a patched install (pytest TestPdbFile.py::TestPdbFile::test_BoxVectorsArrayOfQuantities), green.

@peastman

peastman commented Oct 8, 2026

Copy link
Copy Markdown
Member

Thanks, builds are running now.

@peastman
peastman merged commit 30dfdc2 into openmm:master Oct 12, 2026
16 of 17 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.

PDBFile.writeFile() fails when box vectors are an array of quantities

2 participants