Skip to content

Stabilize cell volumes for highly skew lattice bases - #137

Open
srijan2607 wants to merge 4 commits into
python-periodictable:masterfrom
srijan2607:fix/stable-skew-cell-volume
Open

srijan2607 wants to merge 4 commits into
python-periodictable:masterfrom
srijan2607:fix/stable-skew-cell-volume

Conversation

@srijan2607

@srijan2607 srijan2607 commented Oct 16, 2026 •

Copy link
Copy Markdown

Evaluate crystal-cell volume using the equivalent four-sine factorization of the Gram determinant. Compensated degree-space sums reduce cancellation for extreme or near-degenerate cell descriptions; this is not a typical reduced-cell density-corruption claim.

Exact orthogonal results are preserved: cell_volume(2) == 8.0 and cell_volume(2, 3, 4) == 24.0. Flat cells with angles (30, 60, 90), (90, 90, 180) and (120, 120, 120) return exactly 0.0. Real-angle conversion occurs before negation so NumPy unsigned scalars do not wrap.

Verification

The configured default command, python -m pytest, passes 279 tests, including the project's doctests, coverage and mypy checks. Mypy reports no issues in 33 source files; overall coverage is 87%, with periodictable/util.py at 100%. This was rerun on Python 3.13.13, NumPy 2.5.4 and mypy 2.4.0.

The previous head produced 2 failed, 277 passed, with three typing errors in REAL_ANGLE_CASES. The follow-up changes only test-constructor annotations; production code, test configuration and checks are unchanged. Hosted CI has passed: all four test jobs and the wheel build completed successfully; the PyPI upload job was skipped.

No new angle-range validation or general length-product overflow guarantee is added. Known behavior outside the principal angle domain remains outside this repair.

Research basis: NIST DLMF §4.21 supplies the addition/product identities; the IUCr coreCIF cell-volume definition supplies the Gram determinant. Floating-point accuracy is tested separately from the algebraic identity.

@srijan2607

Copy link
Copy Markdown
Author

The test-typing follow-up is pushed at 0dbbf7d0a4d2. The configured local python -m pytest run passes 279 tests, including the mypy checks.

@pkienzle could you approve the pending fork workflow when appropriate? The current-head run is waiting for approval with zero jobs executed; hosted CI is not yet verified.

@srijan2607
srijan2607 marked this pull request as ready for review October 18, 2026 04:26

@pkienzle pkienzle left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks okay and tests pass. I checked some of the conditions against high precision math.

I'm approving as is, but I'll wait a bit before merging in case you want to tighten the relative error in the tests.

Comment thread test/test_cell_volume.py
assert volume == pytest.approx(expected*1e-24, rel=1e-12, abs=0)
density = crystal.molecular_mass/volume
assert density == pytest.approx(crystal.molecular_mass/1e-24, rel=1e-12)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These three asserts pass on mac M2 with rel=5e-16 (≈2 ULP)

Comment thread test/test_cell_volume.py
])
def test_cartesian_volume(vectors):
expected = abs(_dot(vectors[0], _cross(vectors[1], vectors[2])))
assert cell_volume(*_parameters(vectors)) == pytest.approx(expected, rel=1e-10)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first two tests also pass with rel=5e-16, but the third requires rel=1e-12

expected = float(Decimal(12)*Decimal(2).sqrt())
assert cell_volume(2, 3, 4, *angles) == pytest.approx(expected, rel=2e-15)
assert formula("Si").volume(2, 3, 4, *angles) == pytest.approx(
expected*1e-24, rel=2e-15)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests pass with rel=5e-16

This branch has not been deployed

No deployments
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.

2 participants