-
Notifications
You must be signed in to change notification settings - Fork 158
fix(lammps): tolerate incomplete and commented dump frames #1045
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
2
commits into
deepmodeling:master
Choose a base branch
from
njzjz:fix/issue-616
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.
+177
−14
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,103 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import shutil | ||
| import tempfile | ||
| import unittest | ||
| import warnings | ||
|
|
||
| import numpy as np | ||
| from context import dpdata | ||
|
|
||
| SOURCE = os.path.join("poscars", "conf.5.dump") | ||
|
|
||
|
|
||
| class TestLmpDumpIncomplete(unittest.TestCase): | ||
| """A dump file may be truncated mid-frame or carry stray comment lines.""" | ||
|
|
||
| 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 _write(self, name, lines): | ||
| path = os.path.join(self.tmp_dir, name) | ||
| with open(path, "w") as fp: | ||
| fp.write("\n".join(lines)) | ||
| return path | ||
|
|
||
| def _load(self, path, **kwargs): | ||
| return dpdata.System(path, fmt="lammps/dump", type_map=["O", "H"], **kwargs) | ||
|
|
||
| def test_complete_file_is_unchanged(self): | ||
| self.assertEqual(self._load(SOURCE).get_nframes(), 5) | ||
|
|
||
| def test_truncated_last_frame_is_skipped(self): | ||
| # A run killed while writing leaves the final frame without its atom | ||
| # lines; the earlier frames are still usable. | ||
| path = self._write("truncated.dump", self.lines[:-3]) | ||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter("always") | ||
| system = self._load(path) | ||
| self.assertEqual(system.get_nframes(), 4) | ||
| self.assertTrue( | ||
| any("incomplete frame 4" in str(ii.message) for ii in caught), | ||
| "dropping a frame must be reported", | ||
| ) | ||
|
|
||
| def test_truncated_frame_does_not_shift_later_frames(self): | ||
| # Frames are sliced at their own markers, so a short frame in the | ||
| # middle must not push the remaining frames out of alignment. | ||
| starts = [i for i, ll in enumerate(self.lines) if "ITEM: TIMESTEP" in ll] | ||
| damaged = self.lines[: starts[1] + 4] + self.lines[starts[2] :] | ||
| path = self._write("short_middle.dump", damaged) | ||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter("always") | ||
| system = self._load(path) | ||
| reference = self._load(SOURCE) | ||
| self.assertEqual(system.get_nframes(), 4) | ||
| self.assertTrue(any("incomplete frame 1" in str(ii.message) for ii in caught)) | ||
| # Frame 1 is the damaged one; frames 2..4 must survive intact. | ||
| np.testing.assert_allclose( | ||
| system["coords"][1:], reference["coords"][2:], atol=1e-10 | ||
| ) | ||
|
|
||
| def test_comment_lines_are_ignored(self): | ||
| atoms = [i for i, ll in enumerate(self.lines) if ll.startswith("ITEM: ATOMS")] | ||
| annotated = list(self.lines) | ||
| for offset, idx in enumerate(atoms): | ||
| annotated.insert(idx + 1 + offset, "##### restart marker") | ||
| path = self._write("commented.dump", annotated) | ||
| system = self._load(path) | ||
| reference = self._load(SOURCE) | ||
| self.assertEqual(system.get_nframes(), reference.get_nframes()) | ||
| np.testing.assert_allclose(system["coords"], reference["coords"], atol=1e-10) | ||
|
|
||
| def test_blank_lines_are_ignored(self): | ||
| atoms = [i for i, ll in enumerate(self.lines) if ll.startswith("ITEM: ATOMS")] | ||
| separated = list(self.lines) | ||
| for offset, idx in enumerate(atoms): | ||
| separated.insert(idx + 1 + offset, "") | ||
| path = self._write("blank_lines.dump", separated) | ||
| system = self._load(path) | ||
| reference = self._load(SOURCE) | ||
| self.assertEqual(system.get_nframes(), reference.get_nframes()) | ||
| np.testing.assert_allclose(system["coords"], reference["coords"], atol=1e-10) | ||
|
|
||
| def test_no_usable_frame_raises(self): | ||
| starts = [i for i, ll in enumerate(self.lines) if "ITEM: TIMESTEP" in ll] | ||
| path = self._write("headers_only.dump", self.lines[: starts[0] + 4]) | ||
| with self.assertRaisesRegex(RuntimeError, "no complete frame"): | ||
| self._load(path) | ||
|
|
||
| def test_not_a_dump_file_raises(self): | ||
| path = self._write("garbage.dump", ["not a dump file", "1 2 3"]) | ||
| with self.assertRaisesRegex(RuntimeError, "not a LAMMPS dump file"): | ||
| self._load(path) | ||
|
|
||
|
|
||
| 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.