OpenBioSim/somd2

[BUG] HMR Search Causes Crashes In Systems With No Perturbable Hydrogen Molecules

akalpokas opened this issue · 3 comments

Describe the bug

Sodium-in-a-box (a single sodium atom being perturbed in a water box) system triggers a HMR crash, because the code currently tries to find perturbable hydrogen atoms in the system, which here don't exist.

Exact error reported is:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /home/akalpokas/mambaforge/envs/somd2_fast/bin/somd2:8 in <module>                               │
│                                                                                                  │
│   5 from somd2.app import cli                                                                    │
│   6 if __name__ == '__main__':                                                                   │
│   7sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])                         │
│ ❱ 8sys.exit(cli())                                                                          │
│   9                                                                                              │
│                                                                                                  │
│ /home/akalpokas/code/git/somd2/src/somd2/app/run.py:94 in cli                                    │
│                                                                                                  │
│   91_logger.info(f"sire version: {sire_version}+{sire_revisionid}")                         │
│   92 │                                                                                           │
│   93# Instantiate a Runner object to run the simulation.                                    │
│ ❱ 94runner = Runner(system, config)                                                         │
│   95 │                                                                                           │
│   96# Run the simulation.                                                                   │97runner.run()                                                                            │
│                                                                                                  │
│ /home/akalpokas/code/git/somd2/src/somd2/runner/_runner.py:179 in __init__                       │
│                                                                                                  │
│   176 │   │   ]                                                                                  │
│   177 │   │                                                                                      │
│   178 │   │   # Work out the current hydrogen mass factor.                                       │
│ ❱ 179 │   │   h_mass_factor = self._get_h_mass_factor(self._system)                              │
│   180 │   │                                                                                      │
│   181 │   │   # HMR has already been applied.                                                    │182 │   │   from math import isclose                                                           │
│                                                                                                  │
│ /home/akalpokas/code/git/somd2/src/somd2/runner/_runner.py:609 in _get_h_mass_factor             │
│                                                                                                  │
│   606 │   │   expected_h_mass = Element("H").mass().value()                                      │
│   607 │   │                                                                                      │
│   608 │   │   # Get the hydrogen mass.                                                           │
│ ❱ 609 │   │   h_mass = system.molecules("property is_perturbable")["element H"][0].mass()        │
│   610 │   │                                                                                      │
│   611 │   │   # Work out the current hydrogen mass factor. We round to 3dp due to                │612 │   │   # the precision of atomic masses loaded from text files.                           │
│                                                                                                  │
│ /home/akalpokas/mambaforge/envs/somd2_fast/lib/python3.11/site-packages/sire/mol/__init__.py:505 │
│ in __fixed__getitem__                                                                            │
│                                                                                                  │
│    502 │   │   │   # try to search for the object - this will raise                              │503 │   │   │   # a SyntaxError if this is not a search term                                  │504 │   │   │   # (and is instead a name)                                                     │
│ ❱  505 │   │   │   return __from_select_result(obj.search(key, map=map))                         │
│    506 │   │   except SyntaxError:                                                               │
│    507 │   │   │   # ignore SyntaxErrors as this is a name                                       │508 │   │   │   pass                                                                          │
│                                                                                                  │
│ /home/akalpokas/mambaforge/envs/somd2_fast/lib/python3.11/site-packages/sire/mol/__init__.py:364 │
│ in __from_select_result                                                                          │
│                                                                                                  │
│    361 │   │   )                                                                                 │
│    362 │                                                                                         │
│    363if obj.list_count() == 0:                                                             │
│ ❱  364 │   │   raise KeyError("Nothing matched the search.")                                     │
│    365 │                                                                                         │
│    366typ = obj.get_common_type()                                                           │
│    367                                                                                           │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
KeyError: 'Nothing matched the search.'

To reproduce

Run the provided input file with current default SOMD2 parameters.

Input files

inputs.tar.gz

Environment information:

SOMD2 version: somd2 version: 0.1.dev281+g32ab743.d20240319
Sire version: sire version: 2024.1.0.dev+d971cfd - compiled with OpenMM performance enhancements enabled

Good catch, will fix now.

Should be fixed. I now check the mass of the first hydrogen in the entire system and catch the case where no hydrogens are present, i.e. don't try to perform HMR in that case.

Great, thanks for a quick fix!