Repository navigation
Split weighted pair energies equally between atoms #635
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
Brean0
wants to merge
2
commits into
TorchSim:main
Choose a base branch
from
Brean0:brian/fix-pair-atom-energies
base: main
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 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| """Tests for the general pair potential model and pair forces model.""" | ||
|
|
||
| import functools | ||
| from collections.abc import Callable | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
@@ -19,7 +20,7 @@ | |
| ) | ||
| from torch_sim.models.particle_life import particle_life_pair_force | ||
| from torch_sim.models.soft_sphere import soft_sphere_pair | ||
| from torch_sim.neighbors import torch_nl_n2 | ||
| from torch_sim.neighbors import torch_nl_n2, torchsim_nl | ||
|
|
||
|
|
||
| # BMHTF (Born-Meyer-Huggins-Tosi-Fumi) potential for NaCl | ||
|
|
@@ -353,3 +354,76 @@ def test_no_retain_graph_frees_graph(nacl_sim_state: ts.SimState) -> None: | |
| out = model(nacl_sim_state) | ||
| with pytest.raises(RuntimeError, match="does not require grad"): | ||
| torch.autograd.grad(out["energy"].sum(), A) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("neighbor_list_fn", [torch_nl_n2, torchsim_nl]) | ||
| @pytest.mark.parametrize("reduce_to_half_list", [False, True]) | ||
| def test_isolated_pair_atom_energies( | ||
| neighbor_list_fn: Callable, *, reduce_to_half_list: bool | ||
| ) -> None: | ||
| """The unique pair energy is divided equally between its two atoms.""" | ||
| state = ts.SimState( | ||
| positions=torch.tensor([[0.0, 0.0, 0.0], [0.5, 0.0, 0.0]], dtype=DTYPE), | ||
| cell=10 * torch.eye(3, dtype=DTYPE), | ||
| pbc=False, | ||
| masses=torch.ones(2, dtype=DTYPE), | ||
| atomic_numbers=torch.tensor([1, 2]), | ||
| ) | ||
| model = PairPotentialModel( | ||
| pair_fn=soft_sphere_pair, | ||
| cutoff=1.0, | ||
| dtype=DTYPE, | ||
| per_atom_energies=True, | ||
| neighbor_list_fn=neighbor_list_fn, | ||
| reduce_to_half_list=reduce_to_half_list, | ||
| ) | ||
| out = model(state) | ||
| # For epsilon=sigma=1 and alpha=2, V(0.5) = (1-0.5)**2 / 2 = 0.125. | ||
| torch.testing.assert_close(out["energy"], torch.tensor([0.125], dtype=DTYPE)) | ||
| torch.testing.assert_close( | ||
| out["energies"], torch.tensor([0.0625, 0.0625], dtype=DTYPE) | ||
| ) | ||
| torch.testing.assert_close(out["energies"].sum(), out["energy"].sum()) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("neighbor_list_fn", [torch_nl_n2, torchsim_nl]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The neighbor-list backend parametrization is unnecessary, since the neighbor-list backends are tested separately. |
||
| @pytest.mark.parametrize("reduce_to_half_list", [False, True]) | ||
| def test_batched_atom_energies_unequal_system_sizes( | ||
| neighbor_list_fn: Callable, *, reduce_to_half_list: bool | ||
| ) -> None: | ||
| """Atom energies sum to each system's total for unequal-sized systems.""" | ||
| state = ts.SimState( | ||
| positions=torch.tensor( | ||
| [ | ||
| [0.0, 0.0, 0.0], | ||
| [0.5, 0.0, 0.0], | ||
| [1.0, 0.0, 0.0], | ||
| [0.0, 0.0, 0.0], | ||
| [0.5, 0.0, 0.0], | ||
| ], | ||
| dtype=DTYPE, | ||
| ), | ||
| cell=(10 * torch.eye(3, dtype=DTYPE)).repeat(2, 1, 1), | ||
| pbc=False, | ||
| masses=torch.ones(5, dtype=DTYPE), | ||
| atomic_numbers=torch.ones(5, dtype=torch.long), | ||
| system_idx=torch.tensor([0, 0, 0, 1, 1]), | ||
| ) | ||
| model = PairPotentialModel( | ||
| pair_fn=soft_sphere_pair, | ||
| cutoff=1.0, | ||
| dtype=DTYPE, | ||
| per_atom_energies=True, | ||
| neighbor_list_fn=neighbor_list_fn, | ||
| reduce_to_half_list=reduce_to_half_list, | ||
| ) | ||
| out = model(state) | ||
| # System 0 has two adjacent r=0.5 pairs; system 1 has one such pair. | ||
| torch.testing.assert_close(out["energy"], torch.tensor([0.25, 0.125], dtype=DTYPE)) | ||
| torch.testing.assert_close( | ||
| out["energies"], | ||
| torch.tensor([0.0625, 0.125, 0.0625, 0.0625, 0.0625], dtype=DTYPE), | ||
| ) | ||
| for system in range(state.n_systems): | ||
| atom_energy_sum = out["energies"][state.system_idx == system].sum() | ||
| torch.testing.assert_close(atom_energy_sum, out["energy"][system]) | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This path is covered by
test_batched_atom_energies_unequal_system_sizes, whose second system is the same isolated pair, so I think you can delete this test.