Skip to content

Commit d58e74e

Browse files
chrisiacovellaChristopher Iacovellapre-commit-ci[bot]daico007
authored
fixed freud bond issue (#1127)
* fixed freud bond issue * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update mbuild/compound.py --------- Co-authored-by: Christopher Iacovella <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Co Quach <[email protected]>
1 parent 738bf49 commit d58e74e

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

mbuild/compound.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,6 @@ def freud_generate_bonds(
13061306
name_b,
13071307
dmin,
13081308
dmax,
1309-
exclude_ii=True,
13101309
):
13111310
"""Add Bonds between all pairs of types a/b within [dmin, dmax].
13121311
@@ -1320,8 +1319,6 @@ def freud_generate_bonds(
13201319
The minimum distance (in nm) between Particles for considering a bond
13211320
dmax : float
13221321
The maximum distance (in nm) between Particles for considering a bond
1323-
exclude_ii : bool, optional, default=True
1324-
Whether or not to include neighbors with the same index.
13251322
13261323
Notes
13271324
-----
@@ -1367,7 +1364,21 @@ def freud_generate_bonds(
13671364
a_indices.append(i)
13681365
if part.name == name_b:
13691366
b_indices.append(i)
1370-
1367+
# If we are looking to create bonds between the same species
1368+
# then the indices added to a_indices and b_indices will be identical.
1369+
# In this case we need to make sure that we don't try to bond a particle
1370+
# to itself (i.e., excluded_ii = True).
1371+
# If we are looking for bonds between two different species,
1372+
# the indices we find for a and b will be distinct, with no overlap.
1373+
# However, the way the code is structured, we don't actually pass
1374+
# freud the indices, but rather a list of particle positions associated with each set of indices.
1375+
# As such, the indices that freud sees will be (0, len(a_indices)) and (0, len(b_indices)), even though
1376+
# they represent different actually particles. Thus, to get the right behavior we
1377+
# must not exclude particles with the same index, and thus exclude_ii = False.
1378+
if name_a == name_b:
1379+
exclude_ii = True
1380+
else:
1381+
exclude_ii = False
13711382
aq = freud.locality.AABBQuery(freud_box, moved_positions[b_indices])
13721383

13731384
nlist = aq.query(

mbuild/tests/test_compound.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -720,33 +720,49 @@ def test_freud_generated_bonds_periodicity(self, ch3):
720720
ch3_clone = mb.clone(ch3)
721721
ch3_clone.box = mb.Box(lengths=[max(bounding_box.lengths) + 1] * 3)
722722
ch3_clone.periodicity = (True, True, True)
723-
ch3_clone.freud_generate_bonds(
724-
"H", "H", dmin=0.01, dmax=0.2, exclude_ii=True
725-
)
723+
ch3_clone.freud_generate_bonds("H", "H", dmin=0.01, dmax=0.2)
726724
assert ch3_clone.n_bonds == 3 + 3
727725

728726
ch3_clone2 = mb.clone(ch3)
729727
ch3_clone2.box = mb.Box(lengths=[max(bounding_box.lengths) + 1] * 3)
730728
ch3_clone2.periodicity = (True, True, False)
731-
ch3_clone2.freud_generate_bonds(
732-
"H", "H", dmin=0.01, dmax=0.2, exclude_ii=True
733-
)
729+
ch3_clone2.freud_generate_bonds("H", "H", dmin=0.01, dmax=0.2)
734730
assert ch3_clone2.n_bonds == 3 + 3
735731

736732
@pytest.mark.skipif(not has_freud, reason="Freud not installed.")
737733
def test_freud_generate_bonds(self, ch3):
738734
bounding_box = ch3.get_boundingbox()
739735
ch3.box = mb.Box(lengths=[max(bounding_box.lengths) + 1] * 3)
740-
ch3.freud_generate_bonds("H", "H", dmin=0.01, dmax=0.2, exclude_ii=True)
736+
ch3.freud_generate_bonds("H", "H", dmin=0.01, dmax=0.2)
741737
assert ch3.n_bonds == 3 + 3
742738

743739
@pytest.mark.skipif(not has_freud, reason="Freud not installed.")
744740
def test_freud_generate_bonds_expected(self, ch3):
745741
bounding_box = ch3.get_boundingbox()
746742
ch3.box = mb.Box(lengths=[max(bounding_box.lengths) + 1] * 3)
747-
ch3.freud_generate_bonds("H", "H", dmin=0.01, dmax=0.1, exclude_ii=True)
743+
ch3.freud_generate_bonds("H", "H", dmin=0.01, dmax=0.1)
748744
assert ch3.n_bonds == 3
749745

746+
@pytest.mark.skipif(not has_freud, reason="Freud not installed.")
747+
def test_freud_generate_bonds_mixed(self):
748+
carbon_atom = mb.Compound(name="C", element="C")
749+
750+
grid_pattern = mb.Grid3DPattern(2, 2, 2)
751+
752+
grid_pattern.scale([0.25, 0.25, 0.25])
753+
carbon_list = grid_pattern.apply(carbon_atom)
754+
co_system = mb.Compound(carbon_list)
755+
co_system.box = mb.Box([1, 1, 1])
756+
for i, child in enumerate(co_system.children):
757+
if i % 2 == 0:
758+
child.name = "O"
759+
child.element = "O"
760+
761+
co_system.freud_generate_bonds(
762+
name_a="C", name_b="O", dmin=0.0, dmax=0.16
763+
)
764+
assert co_system.n_bonds == 4
765+
750766
def test_remove_from_box(self, ethane):
751767
n_ethanes = 5
752768
box = mb.fill_box(ethane, n_ethanes, [3, 3, 3])

0 commit comments

Comments
 (0)