Skip to content

Tolerate determinant roundoff for unimodular unit cells - #245

Open
davrens101 wants to merge 3 commits into
Z2PackDev:trunkfrom
davrens101:fix/unimodular-cell-roundoff
Open

davrens101 wants to merge 3 commits into
Z2PackDev:trunkfrom
davrens101:fix/unimodular-cell-roundoff

Conversation

@davrens101

@davrens101 davrens101 commented Nov 4, 2026 •

Copy link
Copy Markdown

Problem

Model.change_unit_cell checks the determinant of an already-validated integer lattice transformation using exact floating-point equality. scipy.linalg.det uses a floating-point factorization, so valid determinant-one integer changes of basis can be rejected.

For example, this matrix has exact integer determinant 1:

U = np.array([[1, -5, -5], [-2, 11, 4], [2, -10, -9]])

In the executed environment, scipy.linalg.det(U) returns 1.0000000000000042. Before the fix, changing a model to this basis raises:

ValueError: The determinant of the unit cell in reduced coordinates must be 1, but it is 1.0000000000000042 instead.

Change

After the existing integer-compatibility validation and rounding, compare the determinant against 1 with an explicit small absolute tolerance (rtol=0, atol=1e-8). Compute the determinant once and reuse it in the error message. Keep the determinant +1/orientation-preserving restriction; do not round arbitrary determinants to the nearest accepted value.

Add a deterministic dense/sparse regression, covering Cartesian and reduced cell specifications, that verifies both reciprocal-coordinate band invariance and an inverse-transformation roundtrip. Include an unreleased changelog entry.

This is independent of the lower-dimensional default-origin fix in #244.

Executed validation

Python 3.11.18, NumPy 1.26.4, SciPy 1.17.1 on Windows.

  • pytest tests/test_change_unit_cell.py tests/test_folding.py tests/test_equivalent_operations.py tests/test_supercell.py -q: 252 passed.
  • Direct actual-model smoke: the previously rejected U is accepted in both Cartesian and reduced modes. Across eight reproducible generic k points, transformed band energies differ by at most 4.704570066849101e-14.
  • Direct invalid-cell smoke: determinants -1, 0, and 2 remain rejected.
  • Black 22.8.0 check and configured Pylint checks pass for both changed Python files.
  • Full suite attempted: 1173 passed, 83 failed, 16 xfailed. The failing test identities match the other worktree's run: Windows tests reopening an already-open NamedTemporaryFile fail with PermissionError, and four CLI tests use Click's removed mix_stderr argument. These paths were not modified or suppressed; no all-green full-suite claim is made.

This contribution was prepared with AI assistance. The before/after reproductions, physical band-invariance smoke, and test commands above were executed.

GitHub CI

The original setup failure from retired cache/artifact actions is resolved. Both the generated workflow and its source template now use supported action versions.

Latest head efa8a2408dacd6ddff8f21512bfd4e65f511f1bf: all seven checks passed, including documentation, pre-commit, the Python test matrix, and version consistency.

@davrens101

Copy link
Copy Markdown
Author

CI currently stops during runner preparation, before checkout or tests: GitHub rejects the workflow's retired actions/cache@v2; the docs job also uses retired actions/upload-artifact@v2. Failed run. These checks did not exercise the patch. I have not rerun them or mixed an unrelated workflow migration into this numerical fix.

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