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 Forcefield.apply assert_bonds #199

Merged
merged 3 commits into from
Feb 22, 2019
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
18 changes: 16 additions & 2 deletions foyer/forcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,9 @@ def registerAtomType(self, parameters):
self.atomTypeRefs[name] = dois

def apply(self, topology, references_file=None, use_residue_map=True,
assert_angle_params=True, assert_dihedral_params=True,
assert_improper_params=False, *args, **kwargs):
assert_bond_params=True, assert_angle_params=True,
assert_dihedral_params=True, assert_improper_params=False,
*args, **kwargs):
"""Apply the force field to a molecular structure

Parameters
Expand All @@ -360,6 +361,9 @@ def apply(self, topology, references_file=None, use_residue_map=True,
residues, i.e. a box of water. Note that for this to be applied to
independent molecules, they must each be saved as different
residues in the topology.
assert_bond_params : bool, optional, default=True
If True, Foyer will exit if parameters are not found for all system
bonds.
assert_angle_params : bool, optional, default=True
If True, Foyer will exit if parameters are not found for all system
angles.
Expand Down Expand Up @@ -390,6 +394,16 @@ def apply(self, topology, references_file=None, use_residue_map=True,
'''
data = self._SystemData

if data.bonds:
missing = [b for b in structure.bonds
if b.type is None]
if missing:
nmissing = len(structure.bonds) - len(missing)
msg = ("Parameters have not been assigned to all bonds. "
"Total system bonds: {}, Parametrized bonds: {}"
"".format(len(structure.bonds), nmissing))
_error_or_warn(assert_bond_params, msg)

if data.angles and (len(data.angles) != len(structure.angles)):
msg = ("Parameters have not been assigned to all angles. Total "
"system angles: {}, Parameterized angles: {}"
Expand Down
17 changes: 17 additions & 0 deletions foyer/tests/test_forcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,20 @@ def test_overrides_space():
ff = Forcefield(forcefield_files=get_fn('overrides-space.xml'))
typed_ethane = ff.apply(ethane)
assert typed_ethane.atoms[0].type == 'CT3'

def test_assert_bonds():
ff = Forcefield(name='trappe-ua')

derponium = mb.Compound()
at1 = mb.Particle(name='H')
at2 = mb.Particle(name='O')
at3 = mb.Particle(name='_CH4')

derponium.add([at1, at2, at3])
derponium.add_bond((at1, at2))
derponium.add_bond((at2, at3))

with pytest.raises(Exception):
ff.apply(derponium)
thing = ff.apply(derponium, assert_bond_params=False, assert_angle_params=False)
assert any(b.type is None for b in thing.bonds)