diff --git a/src/somd2/runner/_samplers/_terminal_flip.py b/src/somd2/runner/_samplers/_terminal_flip.py index 8eba9ac0..feb5dfea 100644 --- a/src/somd2/runner/_samplers/_terminal_flip.py +++ b/src/somd2/runner/_samplers/_terminal_flip.py @@ -172,7 +172,6 @@ def _detect_for_view(mol, all_atoms, flip_angle, max_mobile_atoms, seen): num_atoms = mol.num_atoms() seen_bonds = set() - rdmol = None # lazily initialised if geometric detection fails for i in range(num_atoms): atom_i_idx = _Mol.AtomIdx(i) @@ -210,7 +209,7 @@ def _detect_for_view(mol, all_atoms, flip_angle, max_mobile_atoms, seen): # Skip groups with too many mobile atoms. if max_mobile_atoms is not None and len(mobile) > max_mobile_atoms: - _logger.warning( + _logger.info( f"Terminal group at pivot atom {j} has {len(mobile)} mobile " f"atoms (max_mobile_atoms={max_mobile_atoms}). Skipping group." ) @@ -255,39 +254,30 @@ def _detect_for_view(mol, all_atoms, flip_angle, max_mobile_atoms, seen): group_angle = _round_to_symmetry_angle(raw) if group_angle is None: - # Geometric detection failed; fall back to hybridization. - try: - if rdmol is None: - from sire.convert import to_rdkit as _to_rdkit - from rdkit.Chem import HybridizationType as _HybType - - rdmol = _to_rdkit(mol) - hyb = rdmol.GetAtomWithIdx(j).GetHybridization() - if hyb == _HybType.SP2: - group_angle = 180.0 - elif hyb == _HybType.SP3: - group_angle = 120.0 - else: - _logger.warning( - f"Terminal group at pivot atom {j}: geometric " - f"detection gave unrecognised angle " - f"({raw:.1f}{_degree_sym}) and hybridization " - f"({hyb}) has no defined flip angle. Skipping." - ) - continue + # A distorted ring, so use the number of neighbours of the + # pivot in this end state: planar (3) flips by 180°, + # tetrahedral (4) by 120°. + num_neighbours = sum( + 1 + for n in connectivity.connections_to(pivot_idx_obj) + if mol.atom(n).property("element").num_protons() > 0 + ) + group_angle = {3: 180.0, 4: 120.0}.get(num_neighbours) + if group_angle is None: _logger.warning( f"Terminal group at pivot atom {j}: geometric " f"detection gave unrecognised angle " - f"({raw:.1f}{_degree_sym}), using hybridization-based " - f"angle {group_angle}{_degree_sym} (pivot is {hyb.name})." - ) - except Exception as e: - _logger.warning( - f"Terminal group at pivot atom {j} has no recognised " - f"rotational symmetry (raw angle = {raw:.1f}{_degree_sym}) " - f"and hybridization fallback failed: {e}. Skipping." + f"({raw:.1f}{_degree_sym}) and the pivot has " + f"{num_neighbours} neighbours, so it has no defined " + "flip angle. Skipping." ) continue + _logger.info( + f"Terminal group at pivot atom {j}: geometric " + f"detection gave unrecognised angle " + f"({raw:.1f}{_degree_sym}), using {group_angle}{_degree_sym} " + f"for a pivot with {num_neighbours} neighbours." + ) _logger.debug( f"Terminal group at pivot atom {j}: auto-detected flip " diff --git a/tests/runner/test_terminal_flip.py b/tests/runner/test_terminal_flip.py index c04f8a33..6cdde042 100644 --- a/tests/runner/test_terminal_flip.py +++ b/tests/runner/test_terminal_flip.py @@ -157,6 +157,35 @@ def test_multiple_groups_disjoint_mobile(diphenylethane_mols): assert mobile_0.isdisjoint(mobile_1) +def test_distorted_ring_flip_angle(phenethyl_mols, monkeypatch): + """ + A ring too distorted for the geometric flip angle gets its angle from the + number of neighbours of the pivot, so a planar pivot flips by 180°. + """ + import sire.convert + + from somd2.runner._samplers import _terminal_flip + + def no_rdkit(*args, **kwargs): + raise RuntimeError("RDKit isn't needed for the flip angle") + + monkeypatch.setattr(sire.convert, "to_rdkit", no_rdkit) + monkeypatch.setattr(_terminal_flip, "_auto_flip_angle", lambda *args: 165.0) + groups = detect_terminal_groups(phenethyl_mols) + assert len(groups) == 1 + angle, _ = groups[0] + assert angle == pytest.approx(180.0) + + +def test_max_mobile_atoms(phenethyl_mols): + """Groups with more mobile atoms than the limit are skipped.""" + _, atoms = detect_terminal_groups(phenethyl_mols)[0] + num_mobile = len(atoms) - 2 + + assert len(detect_terminal_groups(phenethyl_mols, max_mobile_atoms=num_mobile)) == 1 + assert detect_terminal_groups(phenethyl_mols, max_mobile_atoms=num_mobile - 1) == [] + + # --------------------------------------------------------------------------- # Config validation # --------------------------------------------------------------------------- @@ -168,6 +197,14 @@ def test_config_terminal_flip_frequency_none(): assert config.terminal_flip_frequency is None +def test_config_terminal_flip_max_mobile_atoms(): + """There is no mobile atom limit by default.""" + assert Config().terminal_flip_max_mobile_atoms is None + assert ( + Config(terminal_flip_max_mobile_atoms=15).terminal_flip_max_mobile_atoms == 15 + ) + + def test_config_terminal_flip_frequency_valid(): """A valid time string is parsed to a Sire GeneralUnit.""" config = Config(terminal_flip_frequency="1 ps")