Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ability to handle 'None' partial charges on OFFMols #119

Merged
merged 1 commit into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion openmmforcefields/generators/template_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ def _molecule_has_user_charges(self, molecule):
import numpy as np
from simtk import unit
zeros = np.zeros([molecule.n_particles])
charges_are_zero = np.allclose(molecule.partial_charges / unit.elementary_charge, zeros)
if (molecule.partial_charges is None) or (np.allclose(molecule.partial_charges / unit.elementary_charge, zeros)):
charges_are_zero = True
else:
charges_are_zero = False
return not charges_are_zero

def _generate_unique_atom_names(self, molecule):
Expand Down
24 changes: 24 additions & 0 deletions openmmforcefields/tests/test_template_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,30 @@ def test_energies(self):
# Compare energies again
self.compare_energies(molecule, openmm_system, smirnoff_system)


def test_partial_charges_are_none(self):
"""Test parameterizing a small molecule with `partial_charges=None` instead
of zeros (happens frequently in OFFTK>=0.7.0)"""
from openforcefield.topology import Molecule
molecule = Molecule.from_smiles('C=O')
molecule.generate_conformers(n_conformers=1)
molecule._partial_charges = None
# Test all supported SMIRNOFF force fields
for small_molecule_forcefield in SMIRNOFFTemplateGenerator.INSTALLED_FORCEFIELDS:
print(f'Testing energies for {small_molecule_forcefield}...')
# Create a generator that knows about a few molecules
# TODO: Should the generator also load the appropriate force field files into the ForceField object?
generator = SMIRNOFFTemplateGenerator(molecules=[molecule], forcefield=small_molecule_forcefield)
# Create a ForceField
import simtk
openmm_forcefield = simtk.openmm.app.ForceField()
# Register the template generator
openmm_forcefield.registerTemplateGenerator(generator.generator)
# Create OpenMM System using OpenMM app
from simtk.openmm.app import NoCutoff
openmm_system = openmm_forcefield.createSystem(molecule.to_topology().to_openmm(), removeCMMotion=False, onbondedMethod=NoCutoff)
smirnoff_system = generator.get_openmm_system(molecule)

def test_version(self):
"""Test version"""
for forcefield in SMIRNOFFTemplateGenerator.INSTALLED_FORCEFIELDS:
Expand Down