Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion tests/models/test_pair_potential.py
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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Comment on lines +378 to +380

Copy link
Copy Markdown
Collaborator

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.

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])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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.
Keeping this test parametrized only over reduce_to_half_list should be enough.

@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])
9 changes: 7 additions & 2 deletions torch_sim/models/pair_potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,13 @@ def forward(self, state: SimState, **_kwargs: object) -> dict[str, torch.Tensor]
atom_energies = torch.zeros(
positions.shape[0], dtype=dtype, device=self._device
)
atom_energies = atom_energies.index_add(0, mapping[0], ew * pair_energies)
atom_energies = atom_energies.index_add(0, mapping[1], ew * pair_energies)
# Divide each weighted pair energy equally between its endpoints.
atom_energies = atom_energies.index_add(
0, mapping[0], 0.5 * ew * pair_energies
)
atom_energies = atom_energies.index_add(
0, mapping[1], 0.5 * ew * pair_energies
)
results["energies"] = atom_energies

if need_grad:
Expand Down
Loading