def apply(self, structure, references_file=None, use_residue_map=True, assert_bond_params=True, assert_angle_params=True, assert_dihedral_params=True, assert_improper_params=False, combining_rule='geometric', verbose=False, *args, **kwargs): """Apply the force field to a molecular structure Parameters ---------- structure : parmed.Structure or mbuild.Compound Molecular structure to apply the force field to references_file : str, optional, default=None Name of file where force field references will be written (in Bibtex format) use_residue_map : boolean, optional, default=True Store atomtyped topologies of residues to a dictionary that maps them to residue names. Each topology, including atomtypes, will be copied to other residues with the same name. This avoids repeatedly calling the subgraph isomorphism on idential residues and should result in better performance for systems with many identical 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. assert_dihedral_params : bool, optional, default=True If True, Foyer will exit if parameters are not found for all system proper dihedrals. assert_improper_params : bool, optional, default=False If True, Foyer will exit if parameters are not found for all system improper dihedrals. combining_rule : str, optional, default='geometric' The combining rule of the system, stored as an attribute of the ParmEd structure. Accepted arguments are `geometric` and `lorentz`. verbose : bool, optional, default=False If True, Foyer will print debug-level information about notable or potentially problematic details it encounters. """ if self.atomTypeDefinitions == {}: raise FoyerError('Attempting to atom-type using a force field ' 'with no atom type defitions.') if not isinstance(structure, pmd.Structure): mb = import_('mbuild') if isinstance(structure, mb.Compound): structure = structure.to_parmed(**kwargs) typemap = self.run_atomtyping(structure, use_residue_map=use_residue_map, **kwargs) self._apply_typemap(structure, typemap) return self.parametrize_system(structure=structure, references_file=references_file, assert_bond_params=assert_bond_params, assert_angle_params=assert_angle_params, assert_dihedral_params=assert_dihedral_params, assert_improper_params=assert_improper_params, combining_rule=combining_rule, verbose=verbose, *args, **kwargs)
def from_gmso_topology(cls, gmso_topology): """Return a TopologyGraph with relevant attributes from an GMSO topology. Parameters ---------- gmso_topology: gmso.Topology The GMSO Topology Returns ------- TopologyGraph The equivalent TopologyGraph of the openFF Topology `openff_topology` """ from foyer.utils.io import import_ gmso = import_("gmso") # This might only be required for type checking if not isinstance(gmso_topology, gmso.Topology): raise TypeError( f"Expected `openff_topology` to be of type {gmso.Topology}. " f"Got {type(gmso_topology).__name__} instead") top_graph = cls() for atom in gmso_topology.sites: if isinstance(atom, gmso.Atom): if atom.name.startswith("_"): top_graph.add_atom( name=atom.name, index=gmso_topology.get_index(atom), atomic_number=None, element=atom.name, ) else: top_graph.add_atom( name=atom.name, index=gmso_topology.get_index(atom), atomic_number=atom.element.atomic_number, element=atom.element.symbol, ) for top_bond in gmso_topology.bonds: atoms_indices = [ gmso_topology.get_index(atom) for atom in top_bond.connection_members ] top_graph.add_bond(atoms_indices[0], atoms_indices[1]) return top_graph
def _check_structure(molecule): """ Confirm that input is parmed.Structure. Convert from mbuild.Compound to parmed.Structure if possible. """ if not isinstance(molecule, pmd.Structure) and has_mbuild: mb = import_('mbuild') if isinstance(molecule, mb.Compound): molecule = molecule.to_parmed() if not isinstance(molecule, pmd.Structure): raise FoyerError('Unknown molecule format: {}\n' 'Supported formats are: ' '"parmed.Structure" and ' '"mbuild.Compound"'.format(molecule)) return molecule
def generate_topology(non_omm_topology, non_element_types=None, residues=None): """Create an OpenMM Topology from another supported topology structure.""" if non_element_types is None: non_element_types = set() if isinstance(non_omm_topology, pmd.Structure): return _topology_from_parmed(non_omm_topology, non_element_types) elif has_mbuild: mb = import_('mbuild') if (non_omm_topology, mb.Compound): pmd_comp_struct = non_omm_topology.to_parmed(residues=residues) return _topology_from_parmed(pmd_comp_struct, non_element_types) else: raise FoyerError('Unknown topology format: {}\n' 'Supported formats are: ' '"parmed.Structure", ' '"mbuild.Compound", ' '"openmm.app.Topology"'.format(non_omm_topology))
def generate_topology(non_omm_topology, non_element_types=None, residues=None): """Create an OpenMM Topology from another supported topology structure.""" if non_element_types is None: non_element_types = set() if isinstance(non_omm_topology, pmd.Structure): return _topology_from_parmed(non_omm_topology, non_element_types) elif has_mbuild: mb = import_('mbuild') if (non_omm_topology, mb.Compound): pmdCompoundStructure = non_omm_topology.to_parmed(residues=residues) return _topology_from_parmed(pmdCompoundStructure, non_element_types) else: raise FoyerError('Unknown topology format: {}\n' 'Supported formats are: ' '"parmed.Structure", ' '"mbuild.Compound", ' '"openmm.app.Topology"'.format(non_omm_topology))
def from_openff_topology(cls, openff_topology): """Return a TopologyGraph with relevant attributes from an openForceField topology. Parameters ---------- openff_topology: openff.toolkit.Topology The openFF Topology Returns ------- TopologyGraph The equivalent TopologyGraph of the openFF Topology `openff_topology` """ from foyer.utils.io import import_ openff_toolkit = import_( "openff.toolkit") # This might only be required for type checking if not isinstance(openff_topology, openff_toolkit.topology.Topology): raise TypeError( f"Expected `openff_topology` to be of type {openff_toolkit.topology.Topology}. " f"Got {type(openff_topology).__name__} instead") top_graph = cls() for top_atom in openff_topology.topology_atoms: atom = top_atom.atom element = pt.Element[atom.atomic_number] top_graph.add_atom( name=atom.name, index=top_atom.topology_atom_index, atomic_number=atom.atomic_number, element=element, ) for top_bond in openff_topology.topology_bonds: atoms_indices = [ atom.topology_atom_index for atom in top_bond.atoms ] top_graph.add_bond(atoms_indices[0], atoms_indices[1]) return top_graph
def apply( self, top, references_file=None, use_residue_map=True, assert_bond_params=True, assert_angle_params=True, assert_dihedral_params=True, assert_improper_params=True, combining_rule="geometric", debug=False, remove_untyped_connections=True, *args, **kwargs, ): """Apply the force field to a molecular topology. Parameters ---------- top : gmso.Topology, and structures/files that can be loaded by mbuild Molecular Topology to apply the force field to. references_file : str, optional, defaut=None Name of file where force field references will be written (in Bibtex format). use_residue_map : bool, optional, default=True Options to speed up if there are a lot of repeated subtopology within the topology (assuming they all have the same name). 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. assert_dihedral_params : bool, optional, default=True If True, Foyer will exit if parameters are not found for all system dihedrals. assert_improper_params : bool, optional, default=True If True, Foyer will exit if parameters are not found for all system impropers. combining_rule : str, optional, default='geometric' The combining rule of the system, stored as an attribute of the GMSO topology. Available options: 'geometric', 'lorentz'. debug : bool, optional, default=False If True, Foyer will print debug-level information about notable or potential problematic details it encounters. remove_untyped_connections : bool, optional, default=True If True, Foyer will remove an untyped connection (i.e., bond, angle, etc.) from the top if the associated assert statement (e.g., assert_bond_params, assert_angle_params, etc.) is False. """ if self.atomTypeDefinitions == {}: raise FoyerError("Attempting to atom-type using a forcefield " "with no atom type definitions.") if self.backend == "gmso": if not isinstance(top, gmso.Topology): mb = import_("mbuild") tmp_top = mb.load(top) top = gmso.external.from_mbuild(tmp_top) assert isinstance(top, gmso.Topology) typemap = self._run_atomtyping(top, use_residue_map=use_residue_map, **kwargs) return self._parametrize( top=top, typemap=typemap, references_file=references_file, assert_bond_params=assert_bond_params, assert_angle_params=assert_angle_params, assert_dihedral_params=assert_dihedral_params, assert_improper_params=assert_improper_params, debug=debug, combining_rule=combining_rule, remove_untyped_connections=remove_untyped_connections, *args, **kwargs, )