Skip to content

Commit 153bb5a

Browse files
committed
Fix intermittent ring-constrained ghost detection by using integer atom indices
1 parent b925aa4 commit 153bb5a

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

‎src/ghostly/_ghostly.py‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,26 +2445,32 @@ def _remove_ghost_centre_angles(
24452445

24462446

24472447
def _ghost_adjacency(ghosts, connectivity):
2448-
"""Build an adjacency dict for the ghost-atom subgraph."""
2449-
ghost_set = set(ghosts)
2450-
adj = {g: [] for g in ghost_set}
2451-
for g in ghost_set:
2448+
"""Build an adjacency dict for the ghost-atom subgraph, keyed by integer atom index.
2449+
2450+
Using integer indices avoids any AtomIdx Python-wrapper hashing
2451+
inconsistencies when looking up atoms returned by connections_to().
2452+
"""
2453+
ghost_indices = {g.value() for g in ghosts}
2454+
adj = {i: [] for i in ghost_indices}
2455+
for g in ghosts:
2456+
i = g.value()
24522457
for c in connectivity.connections_to(g):
2453-
if c in ghost_set:
2454-
adj[g].append(c)
2458+
j = c.value()
2459+
if j in ghost_indices:
2460+
adj[i].append(j)
24552461
return adj
24562462

24572463

2458-
def _ghost_in_cycle(atom, adj):
2464+
def _ghost_in_cycle(atom_idx, adj):
24592465
"""
2460-
Return True if ``atom`` lies on a cycle in the ghost subgraph described
2461-
by ``adj``.
2466+
Return True if ``atom_idx`` lies on a cycle in the ghost subgraph described
2467+
by ``adj`` (an integer-keyed adjacency dict).
24622468
2463-
Removes ``atom`` from the graph and does a BFS from the first neighbour.
2464-
If any other neighbour of ``atom`` is reachable without passing through
2465-
``atom``, the three form a cycle.
2469+
Removes the atom from the graph and does a BFS from the first neighbour.
2470+
If any other neighbour is reachable without passing through the atom,
2471+
they form a cycle.
24662472
"""
2467-
neighbors = adj.get(atom, [])
2473+
neighbors = adj.get(atom_idx, [])
24682474
if len(neighbors) < 2:
24692475
return False
24702476

@@ -2473,7 +2479,7 @@ def _ghost_in_cycle(atom, adj):
24732479
while queue:
24742480
node = queue.pop(0)
24752481
for n in adj.get(node, []):
2476-
if n != atom and n not in visited:
2482+
if n != atom_idx and n not in visited:
24772483
visited.add(n)
24782484
queue.append(n)
24792485

@@ -2503,13 +2509,13 @@ def _ring_constrained_ghosts(bridges, ghosts, connectivity):
25032509
ring_ghosts = set()
25042510
for ghost_list in bridges.values():
25052511
for g in ghost_list:
2506-
if _ghost_in_cycle(g, adj):
2507-
ring_ghosts.add(g)
2512+
if _ghost_in_cycle(g.value(), adj):
2513+
ring_ghosts.add(g.value())
25082514

25092515
if ring_ghosts:
25102516
_logger.debug(
25112517
f"Ring-constrained immediate ghosts (anchors will be zeroed): "
2512-
f"[{', '.join(str(g.value()) for g in ring_ghosts)}]"
2518+
f"[{', '.join(str(i) for i in sorted(ring_ghosts))}]"
25132519
)
25142520

25152521
return ring_ghosts
@@ -2554,11 +2560,11 @@ def _soften_mixed_dihedrals(
25542560
soften_anchors : float, optional
25552561
Scale factor for mixed dihedral force constants (0.0 to 1.0).
25562562
2557-
ring_ghosts : set, optional
2558-
Ghost atoms that are both directly bonded to a bridge and part of a
2559-
ring in the ghost subgraph. Any surviving mixed dihedral whose
2560-
bridge-adjacent ghost atom is in this set is removed regardless of
2561-
``soften_anchors``.
2563+
ring_ghosts : set of int, optional
2564+
Integer atom indices of ghost atoms that are both directly bonded to a
2565+
bridge and part of a ring in the ghost subgraph. Any surviving mixed
2566+
dihedral whose bridge-adjacent ghost atom is in this set is removed
2567+
regardless of ``soften_anchors``.
25622568
25632569
is_lambda1 : bool, optional
25642570
Whether to modify dihedrals at lambda = 1.
@@ -2620,7 +2626,7 @@ def _soften_mixed_dihedrals(
26202626
effective_scale = soften_anchors
26212627
if ring_ghosts:
26222628
for i, a in enumerate(atoms):
2623-
if a in ring_ghosts:
2629+
if a.value() in ring_ghosts:
26242630
neighbors_in_dih = []
26252631
if i > 0:
26262632
neighbors_in_dih.append(atoms[i - 1])

0 commit comments

Comments
 (0)