Skip to content

Commit

Permalink
Merge pull request #119 from j-wags/handle_none_partial_charges
Browse files Browse the repository at this point in the history
added ability to handle 'None' partial charges on OFFMols
  • Loading branch information
jchodera authored Jun 16, 2020
2 parents ab5f16f + 8b6e5c9 commit 32f8913
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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

0 comments on commit 32f8913

Please sign in to comment.