Skip to content

Commit 4176343

Browse files
committed
Take into account rmax in bin_width
1 parent 5065246 commit 4176343

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/tinyff/neighborlist.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def _rebuild(self, atpos: ArrayLike, cell_lengths: ArrayLike):
226226
cell_lengths = parse_cell_lengths(cell_lengths, self.rmax)
227227

228228
# Group the atoms into bins
229-
nbins = _determine_nbins(cell_lengths, self.nbin_approx)
229+
nbins = _determine_nbins(cell_lengths, self.rmax, self.nbin_approx)
230230
bins = _assign_atoms_to_bins(atpos, cell_lengths, nbins)
231231

232232
# Loop over pairs of nearby bins and collect parts for neighborlist.
@@ -261,13 +261,17 @@ def _rebuild(self, atpos: ArrayLike, cell_lengths: ArrayLike):
261261
return cell_lengths
262262

263263

264-
def _determine_nbins(cell_lengths: NDArray[float], nbin_approx: float) -> NDArray[int]:
264+
def _determine_nbins(cell_lengths: NDArray[float], rmax: float, nbin_approx: float) -> NDArray[int]:
265265
"""Determine the number of bins, aiming for a given number of atoms per bin.
266266
267267
Parameters
268268
----------
269269
cell_lengths
270270
The lengths of a periodic orthorombic box.
271+
rmax
272+
The maximum distance between atoms in the neighborlist.
273+
It is guaranteed that the opposite faces of a bin are separated
274+
by a distance not less than rmax.
271275
nbin_approx
272276
The target number of bins, may be a floating point number.
273277
For example, the number of atoms divided by 100.
@@ -279,7 +283,7 @@ def _determine_nbins(cell_lengths: NDArray[float], nbin_approx: float) -> NDArra
279283
The number of bins is at least two.
280284
"""
281285
nbin_volume = np.prod(cell_lengths) / nbin_approx
282-
bin_width = nbin_volume ** (1 / 3)
286+
bin_width = max(nbin_volume ** (1 / 3), rmax)
283287
return np.floor(np.clip(cell_lengths / bin_width, 2, np.inf)).astype(int)
284288

285289

tests/test_neighborlist.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,25 @@ def test_mic_random():
5959

6060
def test_determine_nbins_ortho1():
6161
cell_lengths = np.array([5.0, 10.0, 20.0])
62-
assert (_determine_nbins(cell_lengths, 8) == [2, 2, 4]).all()
63-
assert (_determine_nbins(cell_lengths, 15) == [2, 2, 4]).all()
62+
assert (_determine_nbins(cell_lengths, 2.0, 8) == [2, 2, 4]).all()
63+
assert (_determine_nbins(cell_lengths, 2.0, 15) == [2, 2, 4]).all()
6464

6565

6666
def test_determine_nbins_ortho2():
6767
cell_lengths = np.array([10.0, 10.0, 20.0])
68-
assert (_determine_nbins(cell_lengths, 8) == [2, 2, 3]).all()
69-
assert (_determine_nbins(cell_lengths, 16) == [2, 2, 4]).all()
68+
assert (_determine_nbins(cell_lengths, 2.0, 8) == [2, 2, 3]).all()
69+
assert (_determine_nbins(cell_lengths, 2.0, 16) == [2, 2, 4]).all()
7070

7171

7272
def test_determine_nbins_cubic():
7373
cell_lengths = np.array([10.0, 10.0, 10.0])
74-
assert (_determine_nbins(cell_lengths, 8) == [2, 2, 2]).all()
75-
assert (_determine_nbins(cell_lengths, 27) == [3, 3, 3]).all()
74+
assert (_determine_nbins(cell_lengths, 2.0, 8) == [2, 2, 2]).all()
75+
assert (_determine_nbins(cell_lengths, 2.0, 27) == [3, 3, 3]).all()
76+
77+
78+
def test_determine_nbins_cubic_rmax():
79+
cell_lengths = np.array([100.0, 100.0, 100.0])
80+
assert (_determine_nbins(cell_lengths, 20.0, 1000) == [5, 5, 5]).all()
7681

7782

7883
def test_assign_atoms_to_bins_simple():

0 commit comments

Comments
 (0)