openforcefield / openff-interchange

A project (and object) for storing, manipulating, and converting molecular mechanics data.
https://docs.openforcefield.org/projects/interchange
MIT License
69 stars 22 forks source link

Passing a Molecule without charges to `from_smirnoff`'s `charge_from_molecules` fails ungracefully #1057

Open IAlibay opened 3 hours ago

IAlibay commented 3 hours ago

Definitely not a big issue, mostly opening it because @mattwthompson told me to raise these issues 😅

Description

Passing an uncharged Molecule through charge_from_molecule leads to a TypeError when trying to iterate over None. The traceback is clear, but the error message could be better for users.

Reproduction

from openff.toolkit import Molecule, Topology, ForceField
from openff.interchange.components._packmol import solvate_topology
from openff.interchange import Interchange
from openmm import NonbondedForce

water = Molecule.from_smiles('O')

ligand = Molecule.from_smiles('CCCCC')
ligand.generate_conformers()

off_top = Topology.from_molecules(ligand)
solvated_off_top = solvate_topology(topology=off_top)

ff = ForceField('openff-2.2.0.offxml', 'opc.offxml')
inter = Interchange.from_smirnoff(topology=solvated_off_top, force_field=ff, charge_from_molecules=[water])

Output

TypeError: 'NoneType' object is not iterable

Software versions

Interchange v0.3.29

mattwthompson commented 3 hours ago

I ran into this myself at some point recently; it's be nice to just error out early if any molecules in that list don't have partial charges for all atoms. There are some esoteric cases in which extra molecules could be passed in without charges if they're not going to match molecules, but I'd rather just enforce a simpler rule.