Skip to content

test(md): known-answer tests for the stress -> pressure conversion (S-02) - #79

Merged
hydrogen1222 merged 1 commit into
mainfrom
test/s02-md-pressure-known-answer
Nov 1, 2026
Merged

hydrogen1222 merged 1 commit into
mainfrom
test/s02-md-pressure-known-answer

Conversation

@hydrogen1222

Copy link
Copy Markdown
Owner

Tracking: #75 (Living code audit & exhaustive scientific test campaign)

S-02 — known-answer test for the MD stress → pressure conversion

Test-only. No production code changed.

Blind spot

The tracker's S-02 invariant asks for "Units: … pressure conversion … Independent
hand-calculated known answers". The MD observables
configurational_pressure_gpa / total_pressure_gpa were only ever asserted to
be None (non-3D PBC) or merely unequal to each other; the numeric value,
the unit conversion and the sign convention were never checked against an
independent calculation.

Added tests (tests/mliport/mliport/test_md_runner.py)

Test Physical assertion
test_md_pressure_known_answer_isotropic_compression isotropic compressive stress -0.03 eV/ų → positive 0.03 × 160.2176634 = 4.807 GPa; with zero momenta total == configurational (ideal-gas term vanishes)
test_md_pressure_known_answer_is_tensile_negative tensile +0.03 eV/ų → negative -4.807 GPa (sign not inverted)
test_md_pressure_uses_the_trace_not_all_six_components only xx+yy+zz enters; a pure shear stress gives exactly 0.0 GPa

The reference constant is derived independently from the exact SI constants
(1 eV/ų = e/1e-30 Pa = 160.2176634 GPa, e = 1.602176634e-19 C), not copied
from the product code. The product rounds it to 160.21766208 (8e-9 relative
difference), so the comparisons use rel=1e-6 — tight enough to catch a wrong
factor, unit or sign, far looser than the rounding.

The tests are not vacuous (mutation check)

Doubling the reference constant makes exactly the two magnitude tests fail:

FAILED ...::test_md_pressure_known_answer_isotropic_compression
FAILED ...::test_md_pressure_known_answer_is_tensile_negative
2 failed, 65 deselected

and the observed values match the hand calculation:

compressive [-0.03 x3] -> +4.8065298624 GPa   (hand calc 4.8065299020 GPa)
tensile     [+0.03 x3] -> -4.8065298624 GPa

Evidence

  • Branch from main @ 1ef9a1bea5033dc1a1664d8450f4b4f9edfbd91d, Python 3.12.13,
    pytest 9.1.1, numpy 2.5.4, CPU-only .venv-dev.
  • pytest tests/mliport/mliport/test_md_runner.py -q → 67 passed.
  • ruff check / ruff format --check → clean.
  • Full suite: see CI / run log.

DATA IMPACT

No scientific record invalidated (test-only change; no stored data touched).

Residual

The same unit conversion is spelled out as a literal in four production sites
(runners/base.py, runners/md.py via MDOutcarWriter.EV_A3_TO_GPA,
writers/outcar.py, writers/json_writer.py). This PR pins the MD path only;
the 160.2177 vs 160.21766208 rounding difference is ~8e-9 relative and not
physically meaningful, but consolidating the constant would be a follow-up
cleanup, not a correctness fix.

S-02 (tracker #75).  The MD observables configurational_pressure_gpa /
total_pressure_gpa were only asserted to be None (non-3D PBC) or merely
unequal to each other; the numeric value, the eV/A^3 -> GPa unit
conversion and the sign convention were never checked against an
independent calculation.

Add three known-answer tests over a synthetic calculator with a caller
chosen Voigt stress:
  * isotropic compression -0.03 eV/A^3 -> +4.807 GPa (positive), and with
    zero momenta total == configurational,
  * isotropic tension +0.03 eV/A^3 -> -4.807 GPa (sign not inverted),
  * only xx+yy+zz enters; a pure shear stress gives exactly 0.0 GPa.

The reference constant comes from the exact SI constants
(1 eV/A^3 = e/1e-30 Pa = 160.2176634 GPa), not from the product code;
the comparisons use rel=1e-6, which is far looser than the 8e-9 rounding
difference and far tighter than any wrong factor/unit/sign.  Doubling the
reference constant fails exactly the two magnitude tests, so the tests are
not vacuous.

DATA IMPACT: no scientific record invalidated (test-only change).

Tracking: #75

@hydrogen1222 hydrogen1222 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Audit review — no blocking concern in the shown test-only diff.

The three known-answer assertions independently pin the pressure sign, eV/ų→GPa conversion and trace-vs-shear contract; CI is green on Python 3.10–3.13. This can be considered for merge after normal branch/current-head checks.

Suggested follow-up (not required to merge this test-only PR): add a genuinely nonzero-momentum, mixed-mass known answer for the include_ideal_gas=True kinetic contribution, since the new test only proves total == configurational when momenta are zero. Keep S-02 marked only partly covered until kinetic and writer paths are checked.

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