-
Notifications
You must be signed in to change notification settings - Fork 158
fix(vasp): warn when POTCAR and POSCAR species order disagree #1046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
njzjz
wants to merge
3
commits into
deepmodeling:master
Choose a base branch
from
njzjz:fix/issue-778
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import shutil | ||
| import tempfile | ||
| import unittest | ||
| import warnings | ||
|
|
||
| from context import dpdata | ||
|
|
||
| from dpdata.formats.vasp.outcar import ( | ||
| check_potcar_poscar_order, | ||
| composition_from_poscar_title, | ||
| ) | ||
|
|
||
| SOURCE = os.path.join("poscars", "OUTCAR.ch4.1step") | ||
|
|
||
|
|
||
| class TestPoscarTitleFormula(unittest.TestCase): | ||
| """The POSCAR title is free-form text and only sometimes a formula.""" | ||
|
|
||
| def test_formula_with_counts(self): | ||
| self.assertEqual( | ||
| composition_from_poscar_title(" Li3 F39 K3 Mg3 Ca3 Na3 Al10 O6"), | ||
| {"Li": 3, "F": 39, "K": 3, "Mg": 3, "Ca": 3, "Na": 3, "Al": 10, "O": 6}, | ||
| ) | ||
|
|
||
| def test_elements_without_counts_are_ambiguous(self): | ||
| self.assertIsNone(composition_from_poscar_title(" H C ")) | ||
|
|
||
| def test_prose_is_not_a_formula(self): | ||
| for title in ( | ||
| " POSCAR file written by OVITO", | ||
| " File generated by python recompute scrip", | ||
| " generated by SeeK-path", | ||
| ): | ||
| with self.subTest(title=title): | ||
| self.assertIsNone(composition_from_poscar_title(title)) | ||
|
|
||
| def test_unknown_symbol_is_rejected(self): | ||
| self.assertIsNone(composition_from_poscar_title(" Xx2 Yy3")) | ||
|
|
||
| def test_single_token_is_rejected(self): | ||
| # One species cannot disagree on ordering, and a one-word title is far | ||
| # more likely to be prose. | ||
| self.assertIsNone(composition_from_poscar_title(" Si")) | ||
|
|
||
| def test_truncated_title_drops_the_cut_token(self): | ||
| # VASP pads the title to 40 characters, cutting the last species. | ||
| title = " Cr3 Mn5 Fe3 Co1 Ni4 Cu4 Zn2 Y5 Zr4 Nb5 M" | ||
| self.assertEqual( | ||
| composition_from_poscar_title(title), | ||
| { | ||
| "Cr": 3, | ||
| "Mn": 5, | ||
| "Fe": 3, | ||
| "Co": 1, | ||
| "Ni": 4, | ||
| "Cu": 4, | ||
| "Zn": 2, | ||
| "Y": 5, | ||
| "Zr": 4, | ||
| "Nb": 5, | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| class TestPotcarPoscarOrderCheck(unittest.TestCase): | ||
| def _warnings(self, atom_names, atom_numbs, title): | ||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter("always") | ||
| check_potcar_poscar_order(atom_names, atom_numbs, title) | ||
| return [str(ii.message) for ii in caught] | ||
|
|
||
| def test_matching_order_is_silent(self): | ||
| self.assertEqual(self._warnings(["H", "C"], [2, 1], " H2 C1"), []) | ||
|
|
||
| def test_missing_title_is_silent(self): | ||
| self.assertEqual(self._warnings(["H", "C"], [2, 1], None), []) | ||
|
|
||
| def test_prose_title_is_silent(self): | ||
| self.assertEqual( | ||
| self._warnings(["O", "H"], [1, 2], " POSCAR file written by OVITO"), | ||
| [], | ||
| ) | ||
|
|
||
| def test_comment_order_does_not_imply_species_order(self): | ||
| # Pymatgen commonly writes a reduced formula as the free-form comment | ||
| # even when the actual POSCAR species line uses another order. | ||
| self.assertEqual(self._warnings(["O", "H"], [1, 2], " H2 O1"), []) | ||
|
|
||
| def test_conflicting_composition_warns(self): | ||
| messages = self._warnings( | ||
| ["Li", "O", "F"], | ||
| [3, 39, 3], | ||
| " Li3 F39 K3", | ||
| ) | ||
| self.assertEqual(len(messages), 1) | ||
| self.assertIn("does not match the explicit composition", messages[0]) | ||
| self.assertIn("Li3 O39 F3", messages[0]) | ||
| self.assertIn("Li3 F39 K3", messages[0]) | ||
|
|
||
| def test_truncated_title_compares_only_shared_species(self): | ||
| # Only the species the title still carries may be compared. | ||
| title = " Cr3 Mn5 Fe3 Co1 Ni4 Cu4 Zn2 Y5 Zr4 Nb5 M" | ||
| self.assertEqual( | ||
| self._warnings( | ||
| ["Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Y", "Zr", "Nb", "Mo"], | ||
| [3, 5, 3, 1, 4, 4, 2, 5, 4, 5, 7], | ||
| title, | ||
| ), | ||
| [], | ||
| ) | ||
|
|
||
|
|
||
| class TestOutcarOrderMismatch(unittest.TestCase): | ||
| """End-to-end: an OUTCAR whose POTCAR order contradicts its POSCAR title.""" | ||
|
|
||
| def setUp(self): | ||
| with open(SOURCE) as fp: | ||
| self.lines = fp.read().split("\n") | ||
| self.tmp_dir = tempfile.mkdtemp() | ||
|
|
||
| def tearDown(self): | ||
| shutil.rmtree(self.tmp_dir, ignore_errors=True) | ||
|
|
||
| def _load(self, lines): | ||
| path = os.path.join(self.tmp_dir, "OUTCAR") | ||
| with open(path, "w") as fp: | ||
| fp.write("\n".join(lines)) | ||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter("always") | ||
| system = dpdata.LabeledSystem(path, fmt="vasp/outcar") | ||
| messages = [ | ||
| str(ii.message) | ||
| for ii in caught | ||
| if "composition produced by pairing" in str(ii.message) | ||
| ] | ||
| return system, messages | ||
|
|
||
| def test_consistent_outcar_is_silent(self): | ||
| system, messages = self._load(self.lines) | ||
| self.assertEqual(system.data["atom_names"], ["H", "C"]) | ||
| self.assertEqual(messages, []) | ||
|
|
||
| def test_swapped_potcar_order_warns(self): | ||
| # Swap the two TITEL records so the POTCAR order becomes C, H while | ||
| # the POSCAR title still says H C. | ||
| titel = [i for i, ll in enumerate(self.lines) if "TITEL" in ll] | ||
| self.assertEqual(len(titel), 2) | ||
| swapped = list(self.lines) | ||
| swapped[titel[0]], swapped[titel[1]] = ( | ||
| self.lines[titel[1]], | ||
| self.lines[titel[0]], | ||
| ) | ||
| title = next(i for i, line in enumerate(swapped) if "POSCAR =" in line) | ||
| swapped[title] = " POSCAR = H4 C1" | ||
| system, messages = self._load(swapped) | ||
| # dpdata keeps reporting what VASP computed, but says the inputs | ||
| # disagree. | ||
| self.assertEqual(system.data["atom_names"], ["C", "H"]) | ||
| self.assertEqual(len(messages), 1) | ||
| self.assertIn("does not match the explicit composition", messages[0]) | ||
|
|
||
| def test_poscar_separator_padding_is_not_part_of_title(self): | ||
| # VASP commonly emits multiple spaces after ``POSCAR =``. They are | ||
| # separator padding, not part of the fixed-width title field used to | ||
| # decide whether the last formula token was truncated. | ||
| padded = list(self.lines) | ||
| title = next(i for i, line in enumerate(padded) if "POSCAR =" in line) | ||
| padded[title] = " POSCAR = H11111111111111111111111111111111111 O1" | ||
| _, messages = self._load(padded) | ||
| self.assertEqual(len(messages), 1) | ||
| self.assertIn("does not match the explicit composition", messages[0]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.