-
Notifications
You must be signed in to change notification settings - Fork 152
feat: add support for non-orthogonal cells in amber/md format #870
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
Copilot
wants to merge
6
commits into
devel
Choose a base branch
from
copilot/fix-869
base: devel
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 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac13cb7
Initial plan
Copilot d7a0ea5
Implement non-orthogonal cell support for amber/md format
Copilot c485300
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8aaea48
Address PR review feedback: add type hints and improve integration test
Copilot a8b31c4
Consolidate amber nonorthogonal test files as requested in PR review
Copilot 14d6447
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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,152 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import unittest | ||
|
|
||
| import numpy as np | ||
|
|
||
| from dpdata.amber.md import cell_lengths_angles_to_cell | ||
|
|
||
|
|
||
| class TestAmberNonOrthogonalCells(unittest.TestCase): | ||
| def test_orthogonal_cell_conversion(self): | ||
| """Test that orthogonal cells (90° angles) work correctly.""" | ||
| # Test case: simple cubic cell with a=10, b=15, c=20, all angles=90° | ||
| cell_lengths = np.array([[10.0, 15.0, 20.0]]) | ||
| cell_angles = np.array([[90.0, 90.0, 90.0]]) | ||
|
|
||
| expected_cell = np.array( | ||
| [[[10.0, 0.0, 0.0], [0.0, 15.0, 0.0], [0.0, 0.0, 20.0]]] | ||
| ) | ||
|
|
||
| result_cell = cell_lengths_angles_to_cell(cell_lengths, cell_angles) | ||
|
|
||
| np.testing.assert_allclose(result_cell, expected_cell, rtol=1e-12, atol=1e-14) | ||
|
|
||
| def test_monoclinic_cell_conversion(self): | ||
| """Test monoclinic cell (beta != 90°, alpha=gamma=90°).""" | ||
| # Test case: monoclinic cell with a=10, b=15, c=20, alpha=90°, beta=120°, gamma=90° | ||
| cell_lengths = np.array([[10.0, 15.0, 20.0]]) | ||
| cell_angles = np.array([[90.0, 120.0, 90.0]]) | ||
|
|
||
| # Expected result: | ||
| # v1 = [10, 0, 0] | ||
| # v2 = [0, 15, 0] (gamma=90°) | ||
| # v3 = [20*cos(120°), 0, 20*sin(120°)] = [-10, 0, 17.32...] | ||
| cos_120 = np.cos(np.deg2rad(120.0)) # -0.5 | ||
| sin_120 = np.sin(np.deg2rad(120.0)) # sqrt(3)/2 | ||
|
|
||
| expected_cell = np.array( | ||
| [ | ||
| [ | ||
| [10.0, 0.0, 0.0], | ||
| [0.0, 15.0, 0.0], | ||
| [20.0 * cos_120, 0.0, 20.0 * sin_120], | ||
| ] | ||
| ] | ||
| ) | ||
|
|
||
| result_cell = cell_lengths_angles_to_cell(cell_lengths, cell_angles) | ||
|
|
||
| np.testing.assert_allclose(result_cell, expected_cell, rtol=1e-12, atol=1e-14) | ||
|
|
||
| def test_hexagonal_cell_conversion(self): | ||
| """Test hexagonal cell (gamma=120°, alpha=beta=90°).""" | ||
| # Test case: hexagonal cell with a=10, b=10, c=15, alpha=90°, beta=90°, gamma=120° | ||
| cell_lengths = np.array([[10.0, 10.0, 15.0]]) | ||
| cell_angles = np.array([[90.0, 90.0, 120.0]]) | ||
|
|
||
| # Expected result: | ||
| # v1 = [10, 0, 0] | ||
| # v2 = [10*cos(120°), 10*sin(120°), 0] = [-5, 8.66..., 0] | ||
| # v3 = [0, 0, 15] (alpha=beta=90°) | ||
| cos_120 = np.cos(np.deg2rad(120.0)) # -0.5 | ||
| sin_120 = np.sin(np.deg2rad(120.0)) # sqrt(3)/2 | ||
|
|
||
| expected_cell = np.array( | ||
| [ | ||
| [ | ||
| [10.0, 0.0, 0.0], | ||
| [10.0 * cos_120, 10.0 * sin_120, 0.0], | ||
| [0.0, 0.0, 15.0], | ||
| ] | ||
| ] | ||
| ) | ||
|
|
||
| result_cell = cell_lengths_angles_to_cell(cell_lengths, cell_angles) | ||
|
|
||
| np.testing.assert_allclose(result_cell, expected_cell, rtol=1e-12, atol=1e-14) | ||
|
|
||
| def test_triclinic_cell_conversion(self): | ||
| """Test triclinic cell (all angles != 90°).""" | ||
| # Test case: triclinic cell with a=8, b=10, c=12, alpha=70°, beta=80°, gamma=110° | ||
| cell_lengths = np.array([[8.0, 10.0, 12.0]]) | ||
| cell_angles = np.array([[70.0, 80.0, 110.0]]) | ||
|
|
||
| result_cell = cell_lengths_angles_to_cell(cell_lengths, cell_angles) | ||
|
|
||
| # Check that the result has the right shape | ||
| self.assertEqual(result_cell.shape, (1, 3, 3)) | ||
|
|
||
| # Check that the cell vectors have the correct lengths | ||
| computed_lengths = np.linalg.norm(result_cell[0], axis=1) | ||
| expected_lengths = np.array([8.0, 10.0, 12.0]) | ||
| np.testing.assert_allclose(computed_lengths, expected_lengths, rtol=1e-12) | ||
|
|
||
| # Check that the angles between vectors are correct | ||
| v1, v2, v3 = result_cell[0] | ||
|
|
||
| # Angle between v2 and v3 should be alpha (70°) | ||
| cos_alpha = np.dot(v2, v3) / (np.linalg.norm(v2) * np.linalg.norm(v3)) | ||
| alpha_computed = np.rad2deg(np.arccos(cos_alpha)) | ||
| np.testing.assert_allclose(alpha_computed, 70.0, rtol=1e-8) | ||
|
|
||
| # Angle between v1 and v3 should be beta (80°) | ||
| cos_beta = np.dot(v1, v3) / (np.linalg.norm(v1) * np.linalg.norm(v3)) | ||
| beta_computed = np.rad2deg(np.arccos(cos_beta)) | ||
| np.testing.assert_allclose(beta_computed, 80.0, rtol=1e-8) | ||
|
|
||
| # Angle between v1 and v2 should be gamma (110°) | ||
| cos_gamma = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)) | ||
| gamma_computed = np.rad2deg(np.arccos(cos_gamma)) | ||
| np.testing.assert_allclose(gamma_computed, 110.0, rtol=1e-8) | ||
|
|
||
| def test_multiple_frames(self): | ||
| """Test that multiple frames are handled correctly.""" | ||
| # Test case: 3 frames with different cell parameters | ||
| cell_lengths = np.array( | ||
| [ | ||
| [10.0, 10.0, 10.0], # cubic | ||
| [8.0, 12.0, 15.0], # orthorhombic | ||
| [10.0, 10.0, 12.0], | ||
| ] | ||
| ) # hexagonal-like | ||
| cell_angles = np.array( | ||
| [[90.0, 90.0, 90.0], [90.0, 90.0, 90.0], [90.0, 90.0, 120.0]] | ||
| ) | ||
|
|
||
| result_cell = cell_lengths_angles_to_cell(cell_lengths, cell_angles) | ||
|
|
||
| # Check shape | ||
| self.assertEqual(result_cell.shape, (3, 3, 3)) | ||
|
|
||
| # Check first frame (cubic) | ||
| expected_frame1 = np.array( | ||
| [[10.0, 0.0, 0.0], [0.0, 10.0, 0.0], [0.0, 0.0, 10.0]] | ||
| ) | ||
| np.testing.assert_allclose( | ||
| result_cell[0], expected_frame1, rtol=1e-12, atol=1e-14 | ||
| ) | ||
|
|
||
| # Check third frame (hexagonal-like) | ||
| cos_120 = np.cos(np.deg2rad(120.0)) # -0.5 | ||
| sin_120 = np.sin(np.deg2rad(120.0)) # sqrt(3)/2 | ||
| expected_frame3 = np.array( | ||
| [[10.0, 0.0, 0.0], [10.0 * cos_120, 10.0 * sin_120, 0.0], [0.0, 0.0, 12.0]] | ||
| ) | ||
| np.testing.assert_allclose( | ||
| result_cell[2], expected_frame3, rtol=1e-12, atol=1e-14 | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
njzjz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
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,58 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import unittest | ||
|
|
||
| import numpy as np | ||
|
|
||
| from dpdata.amber.md import cell_lengths_angles_to_cell | ||
|
|
||
|
|
||
| class TestAmberNonOrthogonalIntegration(unittest.TestCase): | ||
| def test_no_runtime_error_for_nonorthogonal_cells(self): | ||
| """Test that non-orthogonal cells no longer raise RuntimeError.""" | ||
| # Test case that would have previously raised RuntimeError | ||
njzjz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cell_lengths = np.array([[10.0, 10.0, 15.0]]) | ||
| cell_angles = np.array([[90.0, 90.0, 120.0]]) # gamma != 90 | ||
|
|
||
| # This should NOT raise a RuntimeError anymore | ||
| try: | ||
| result = cell_lengths_angles_to_cell(cell_lengths, cell_angles) | ||
| # Should return a valid 3x3 cell matrix | ||
| self.assertEqual(result.shape, (1, 3, 3)) | ||
| except RuntimeError as e: | ||
| if "Unsupported cells" in str(e): | ||
| self.fail( | ||
| "cell_lengths_angles_to_cell should support non-orthogonal cells" | ||
| ) | ||
| else: | ||
| raise # Re-raise if it's a different RuntimeError | ||
|
|
||
| def test_extreme_angles_case(self): | ||
| """Test edge case with angles very far from 90°.""" | ||
| cell_lengths = np.array([[5.0, 8.0, 12.0]]) | ||
| cell_angles = np.array([[60.0, 70.0, 130.0]]) # all far from 90° | ||
|
|
||
| # Should work without error | ||
| result = cell_lengths_angles_to_cell(cell_lengths, cell_angles) | ||
| self.assertEqual(result.shape, (1, 3, 3)) | ||
|
|
||
| # Verify the lengths are preserved | ||
| computed_lengths = np.linalg.norm(result[0], axis=1) | ||
| expected_lengths = np.array([5.0, 8.0, 12.0]) | ||
| np.testing.assert_allclose(computed_lengths, expected_lengths, rtol=1e-10) | ||
|
|
||
| def test_backwards_compatibility_orthogonal(self): | ||
| """Test that orthogonal cells still produce the same result as before.""" | ||
| cell_lengths = np.array([[10.0, 15.0, 20.0]]) | ||
| cell_angles = np.array([[90.0, 90.0, 90.0]]) | ||
|
|
||
| result = cell_lengths_angles_to_cell(cell_lengths, cell_angles) | ||
|
|
||
| # Should produce the same diagonal matrix as the old implementation | ||
| expected = np.array([[[10.0, 0.0, 0.0], [0.0, 15.0, 0.0], [0.0, 0.0, 20.0]]]) | ||
|
|
||
| np.testing.assert_allclose(result, expected, rtol=1e-12, atol=1e-14) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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.