Skip to content

Commit 04e3fb3

Browse files
committed
Also filter pairs in ASE neighbors list
1 parent 9f529bd commit 04e3fb3

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

python/rascaline/rascaline/systems/ase.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,23 @@ def compute_neighbors(self, cutoff):
9191

9292
nl_result = neighborlist.neighbor_list("ijdDS", self._atoms, cutoff)
9393
for i, j, d, D, S in zip(*nl_result):
94+
# we want a half neighbor list, so drop all duplicated neighbors
9495
if j < i:
95-
# we want a half neighbor list, so drop all duplicated
96-
# neighbors
9796
continue
97+
elif i == j:
98+
if S[0] == 0 and S[1] == 0 and S[2] == 0:
99+
# only create pairs with the same atom twice if the pair spans more
100+
# than one unit cell
101+
continue
102+
elif S[0] + S[1] + S[2] < 0 or (
103+
(S[0] + S[1] + S[2] == 0) and (S[2] < 0 or (S[2] == 0 and S[1] < 0))
104+
):
105+
# When creating pairs between an atom and one of its periodic
106+
# images, the code generate multiple redundant pairs (e.g. with
107+
# shifts 0 1 1 and 0 -1 -1); and we want to only keep one of these.
108+
# We keep the pair in the positive half plane of shifts.
109+
continue
110+
98111
self._pairs.append((i, j, d, D, S))
99112

100113
self._pairs_by_center = []

python/rascaline/tests/systems/ase.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
from rascaline import SphericalExpansion
45
from rascaline.systems import AseSystem
56

67

@@ -145,3 +146,56 @@ def test_no_pbc_cell():
145146
)
146147
with pytest.warns(Warning, match=message):
147148
AseSystem(atoms)
149+
150+
151+
def test_same_spherical_expansion():
152+
system = ase.Atoms(
153+
"CaC6",
154+
positions=[
155+
(0.0, 0.0, 0.0),
156+
(1.88597, 1.92706, 0.0113749),
157+
(2.66157, 3.55479, 7.7372),
158+
(2.35488, 3.36661, 3.88),
159+
(2.19266, 2.11524, 3.86858),
160+
(2.52936, 4.62777, 3.87771),
161+
(2.01817, 0.85408, 3.87087),
162+
],
163+
cell=[[3.57941, 0, 0], [0.682558, 5.02733, 0], [0.285565, 0.454525, 7.74858]],
164+
pbc=True,
165+
)
166+
167+
calculator = SphericalExpansion(
168+
# make sure to choose a cutoff larger then the cell to test for pairs crossing
169+
# multiple periodic boundaries
170+
cutoff=9,
171+
max_radial=5,
172+
max_angular=5,
173+
atomic_gaussian_width=0.3,
174+
radial_basis={"Gto": {}},
175+
center_atom_weight=1.0,
176+
cutoff_function={"Step": {}},
177+
)
178+
179+
rascaline_nl = calculator.compute(
180+
system, gradients=["positions", "cell"], use_native_system=True
181+
)
182+
183+
ase_nl = calculator.compute(
184+
system, gradients=["positions", "cell"], use_native_system=False
185+
)
186+
187+
for key, block in rascaline_nl.items():
188+
ase_block = ase_nl.block(key)
189+
190+
assert ase_block.samples == block.samples
191+
# Since the pairs are in a different order, the values are slightly different
192+
assert np.allclose(ase_block.values, block.values, atol=1e-16, rtol=1e-9)
193+
194+
for parameter in ["cell", "positions"]:
195+
gradient = block.gradient(parameter)
196+
ase_gradient = ase_block.gradient(parameter)
197+
198+
assert gradient.samples == ase_gradient.samples
199+
assert np.allclose(
200+
ase_gradient.values, gradient.values, atol=1e-16, rtol=1e-6
201+
)

0 commit comments

Comments
 (0)