External Library Conversions
The gmso.external module provides functions to convert Topology objects
to and from in-memory representations used by other molecular simulation libraries.
All public functions are importable from gmso.external.
mBuild
mBuild is a hierarchical molecule builder. The conversions
below allow a built mBuild.Compound to be typed and written to simulation files via
GMSO, and vice-versa.
Convert an mbuild.Compound to a gmso.Topology. |
|
Convert an |
|
Convert a |
from_mbuild
- gmso.external.from_mbuild(compound, box=None, search_method=element_by_symbol, parse_label=True, custom_groups=None, infer_elements=False)[source]
Convert an mbuild.Compound to a gmso.Topology.
This conversion makes the following assumptions about the inputted Compound:
All positional and box dimension values in compound are in nanometers.
The hierarchical structure of the Compound will be flattened and translated to labels in GMSO Sites. The directly supported labels include Site.group, Site.molecule_name, and Site.residue_name.
group is determined as te second-highest level Compound and is automatically generated; * molecule is determined by traversing through hierarchy of the mb.Compound, starting from the particle level, until the lowest independent mb.Compound is reached (determined as an mb.Compound that does not have any bond outside its boundary);
residue is the mb.Compound level right above particle level.
molecule and residue take the format of (name, index), where the latter can be used to distinguish between molecule/residue of the same name. These two labels are only generated if parse_label=True.
Only Bonds are added for each bond in the Compound. If Angles and Dihedrals are desired in the resulting Topology, they must be added separately from this function.
- Parameters:
compound (
Compound) – mbuild.Compound instance that need to be convertedbox (
Box|None) – Box information to be loaded to a gmso.Topologysearch_method (
function, optional, defaultelement_by_symbol) – Searching method used to assign element from periodic table to particle site. The information specified in the search_method argument is extracted from each Particle’s name attribute. Valid functions are element_by_symbol, element_by_name, element_by_atomic_number, and element_by_mass, which can be imported from gmso.core.elementparse_label (
bool) – Option to parse hierarchy info of the compound into system of top label, including, group, molecule and residue labels.custom_groups (
listorstr, optional, defaultNone) – Allows user to identify the groups assigned to each site in the topology based on the compound.name attributes found traversing down the hierarchy. Be sure to supply names such that every particle will be pass through one matching name on the way down from compound.children. Only the first match while moving downwards will be assigned to the site. If parse_label=False, this argument does nothing.infer_elements (
bool) – Allows the reader to try to load element info from the mbuild Particle.name instead of only from the populated Particle.element
- Returns:
top
- Return type:
from_mbuild_box
to_mbuild
ParmEd
ParmEd is a library for editing and converting molecular mechanics parameter files.
Convert a |
|
Convert a |
from_parmed
- gmso.external.from_parmed(structure, refer_type=True)[source]
Convert a
parmed.Structureto aTopology.Maps the Structure hierarchy to GMSO objects:
Structure→Topology,Atom→Atom.- Parameters:
structure (
Structure) – The ParmEd structure to convert. May be parametrized or un-parametrized.refer_type (
bool) – WhenTrue, transferAtomType,BondType,AngleType,DihedralType, andImproperTypeinformation from the ParmEd structure.
- Returns:
Topology constructed from structure.
- Return type:
to_parmed
OpenMM
OpenMM is a GPU-accelerated molecular dynamics library. GMSO can
export a typed topology as an OpenMM Topology object.
Convert a |
to_openmm
- gmso.external.to_openmm(topology, openmm_object='topology')[source]
Convert a
Topologyto an untyped OpenMM topology or modeller.Useful when atom-typing is performed within OpenMM rather than GMSO. See https://openmm.org for more information.
- Parameters:
topology (gmso.Topology) – The un-typed GMSO topology to convert.
openmm_object (
str) – Target OpenMM object type. Accepted values:'topology'or'modeller'.
- Returns:
The converted OpenMM object.
- Return type:
openmm.app.Topologyoropenmm.app.Modeller
HOOMD-blue
HOOMD-blue is a particle simulation toolkit. GMSO can export directly to HOOMD-blue’s GSD snapshot and forcefield objects.
Create a hoomd.Snapshot objcet (HOOMD default data format). |
|
Convert the potential portion of a typed GMSO to hoomd forces. |
|
Create a gsd.hoomd.Frame objcet (HOOMD default data format). |
to_hoomd_snapshot
- gmso.external.to_hoomd_snapshot(top, base_units=None, shift_coords=True, parse_special_pairs=True, auto_scale=False)[source]
Create a hoomd.Snapshot objcet (HOOMD default data format).
The Hoomd snapshot defines the topology of HOOMD-blue simulations. This file can be used as a starting point for a HOOMD-blue simulation, for analysis, and for visualization in various tools.
- Parameters:
top (
gmso.Topology) – gmso.Topology objectfilename (
str) – Path of the output file.base_units (
dict,optinoal, defaultNone) – The dictionary of base units to be converted to. Entries restricted to “energy”, “length”, and “mass”. There is also option to used predefined unit systems (“MD” or “AKMA” provided as string). If None is provided, this method will perform no units conversion.shift_coords (
bool, optional, defaultTrue) – Shift coordinates from (0, L) to (-L/2, L/2) if necessary.parse_special_pairs (
bool, optional, defaultTrue) – Writes out special pair information necessary to correctly use the OPLS fudged 1,4 interactions in HOOMD.auto_scale (
boolordict, optional, defaultFalse) – Automatically scaling relevant length, energy and mass units. Referenced mass unit is obtained from sites’ masses. Referenced energy and distance are refered from sites’ atom types (when applicable). If the referenced scaling values cannot be determined (e.g., when the topology is not typed), all reference scaling values is set to 1. A dictionary specifying the referenced scaling values may also be provided for this argument.
- Returns:
hoomd_snapshot (
hoomd.Snapshot) – Converted HOOMD snapshot. Always returned.base_units (
dict) – Base units dictionary used during the conversion. Always returned.rigid_info (
hoomd.md.constrain.Rigid) – A HOOMD constraint object storing constituent particle information needed to run rigid body simulations in HOOMD-blue. Only returned when sites withsite.molecule.isrigid = Trueare found in the topology.
Notes
Force field parameters are not written to the snapshot and must be generated separately using
gmso.external.convert_hoomd.to_hoomd_forcefield().When using mBuild and GMSO to initialise rigid body simulations with HOOMD-blue, set
site.molecule.isrigid = True. If the topology contains a mix of rigid and flexible molecules, the rigid molecules must appear first in the GMSO topology hierarchy (and therefore first in the mBuild Compound hierarchy).For more information about rigid body simulations in HOOMD-blue see https://hoomd-blue.readthedocs.io/en/latest/tutorial/06-Modelling-Rigid-Bodies/00-index.html
For information on the
base_unitsdictionary see https://hoomd-blue.readthedocs.io/en/latest/units.html#base-unitsExamples
Rigid benzene + flexible ethane system (benzene must be passed first so it sits at the top of the mBuild Compound hierarchy):
ethane = mb.lib.molecules.Ethane() ethane.name = "ethane" benzene = mb.load("c1ccccc1", smiles=True) benzene.name = "benzene" box = mb.fill_box([benzene, ethane], n_compounds=[1, 1], box=[2, 2, 2]) top = from_mbuild(box) for site in top.sites: if site.molecule.name == "benzene": site.molecule.isrigid = True snapshot, refs, rigid = to_hoomd_snapshot(top)
to_hoomd_forcefield
- gmso.external.to_hoomd_forcefield(top, r_cut, nlist=None, pppm_kwargs={'resolution': (8, 8, 8), 'order': 4}, base_units=None, auto_scale=False, kT=None)[source]
Convert the potential portion of a typed GMSO to hoomd forces.
- Parameters:
top (
gmso.Topology) – The typed topology to be convertedr_cut (
float) – r_cut for the nonbonded forces.nlist (
hoomd.md.nlist.NeighborListortupleorlistorNone, optional, defaultNone) – Neighborlist object to use for nonbonded forcefield. Can also be a list or tuple of neighborlists where the first neighborlist is used for nonbonded pair forces, and the second with coulombic forces. If None, the default value used will be a hoomd.md.nlist.Cell(exclusions=exclusions, buffer=0.4).pppm_kwargs (
dict) – Keyword arguments to pass to hoomd.md.long_range.make_pppm_coulomb_forces().base_units (
dictorstr, optional, defaultNone) – The dictionary of base units to be converted to. Entries restricted to “energy”, “length”, and “mass”. There is also option to used predefined unit systems (“MD” or “AKMA” provided as string). If None is provided, this method will perform no units conversion.auto_scale (
boolordict, optional, defaultFalse) – Automatically scaling relevant length, energy and mass units. Referenced mass unit is obtained from sites’ masses. Referenced energy and distance are refered from sites’ atom types (when applicable). If the referenced scaling values cannot be determined (e.g., when the topology is not typed), all reference scaling values is set to 1. A dictionary specifying the referenced scaling values may also be provided for this argument.kT (
float, optional, defaultNone) – Set the kT parameter for running simulations with hoomd.md.pair.DPD forces. If none and these forces are present in top.pairpotential_types, will raise an error. Please convert to units of [energy] using base_units to ensure proper value is passed.
- Returns:
forces (
dict) – HOOMD forces converted from all available PotentialTypes of the provided GMSO Topology. Converted are grouped by their category (as key of the dictionary), namely, “nonbonded”, “bonds”, “angles”, “dihedrals”, and “impropers”.base_units (
dict) – Base units dictionary utilized during the conversion.
to_gsd_snapshot
- gmso.external.to_gsd_snapshot(top, base_units=None, shift_coords=True, parse_special_pairs=True, auto_scale=False)[source]
Create a gsd.hoomd.Frame objcet (HOOMD default data format).
The gsd snapshot defines the topology of HOOMD-blue simulations. This file can be used as a starting point for a HOOMD-blue simulation, for analysis, and for visualization in various tools.
- Parameters:
top (
gmso.Topology) – gmso.Topology objectfilename (
str) – Path of the output file.base_units (
dict,optinoal, defaultNone) – The dictionary of base units to be converted to. Entries restricted to “energy”, “length”, and “mass”. There is also option to used predefined unit systems (“MD” or “AKMA” provided as string). If None is provided, this method will perform no units conversion.shift_coords (
bool, optional, defaultTrue) – Shift coordinates from (0, L) to (-L/2, L/2) if necessary.parse_special_pairs (
bool, optional, defaultTrue) – Writes out special pair information necessary to correctly use the OPLS fudged 1,4 interactions in HOOMD.auto_scale (
boolordict, optional, defaultFalse) – Automatically scaling relevant length, energy and mass units. Referenced mass unit is obtained from sites’ masses. Referenced energy and distance are refered from sites’ atom types (when applicable). If the referenced scaling values cannot be determined (e.g., when the topology is not typed), all reference scaling values is set to 1. A dictionary specifying the referenced scaling values may also be provided for this argument.
- Returns:
gsd_snapshot (
gsd.hoomd.Frame) – Converted HOOMD snapshot. Always returned.base_units (
dict) – Base units dictionary used during the conversion. Always returned.rigid_info (
hoomd.md.constrain.Rigid) – A HOOMD constraint object storing constituent particle information needed to run rigid body simulations in HOOMD-blue. Only returned when sites withsite.molecule.isrigid = Trueare found in the topology.
Notes
Force field parameters are not written to the GSD file and must be generated separately using
gmso.external.convert_hoomd.to_hoomd_forcefield().When using mBuild and GMSO to initialise rigid body simulations with HOOMD-blue, set
site.molecule.isrigid = True. If the topology contains a mix of rigid and flexible molecules, the rigid molecules must appear first in the GMSO topology hierarchy (and therefore first in the mBuild Compound hierarchy).For more information about rigid body simulations in HOOMD-blue see https://hoomd-blue.readthedocs.io/en/latest/tutorial/06-Modelling-Rigid-Bodies/00-index.html
For information on the
base_unitsdictionary see https://hoomd-blue.readthedocs.io/en/latest/units.html#base-unitsExamples
Rigid benzene + flexible ethane system (benzene must be passed first so it sits at the top of the mBuild Compound hierarchy):
ethane = mb.lib.molecules.Ethane() ethane.name = "ethane" benzene = mb.load("c1ccccc1", smiles=True) benzene.name = "benzene" box = mb.fill_box([benzene, ethane], n_compounds=[1, 1], box=[2, 2, 2]) top = from_mbuild(box) for site in top.sites: if site.molecule.name == "benzene": site.molecule.isrigid = True snapshot, refs, rigid = to_gsd_snapshot(top)
NetworkX
GMSO topologies can be converted to and from NetworkX graphs for graph-based analysis.
Convert a |
|
Convert a |
from_networkx
- gmso.external.from_networkx(graph)[source]
Convert a
networkx.Graphto aTopology.Each graph node must be a
Siteand each edge becomes aBond.- Parameters:
graph (
Graph) – Graph whose nodes areSiteinstances. Edge data may optionally contain a'connection'key holding aConnectionobject.- Returns:
Topology containing the sites and bonds from graph.
- Return type:
Notes
Much information is lost when converting a topology to a graph (mixing rules, metadata, etc.). The edge
'connection'attribute stores the originalBondand is used to reconstruct it during the reverse conversion.Angle and dihedral information stored as node attributes is detected but not converted; a warning is logged if such data is found.
to_networkx
- gmso.external.to_networkx(top, parse_angles=True, parse_dihedrals=True)[source]
Convert a
Topologyto anetworkx.Graph.Each site becomes a graph node and each bond becomes an edge.
- Parameters:
top (
Topology) – The topology to convert.parse_angles (
bool) – WhenTrue, populate an'angles'attribute on every node with the list of angles that include that site.parse_dihedrals (
bool) – WhenTrue, populate a'dihedrals'attribute on every node with the list of dihedrals that include that site.
- Returns:
Graph where nodes are
Atomobjects and edges carry a'connection'attribute holding the correspondingBond.- Return type:
Notes
Converting to a graph loses metadata, mixing rules, and all potential type information. The graph is primarily useful for connectivity analysis.