def element_by_smarts_string(smarts_string): """Search for an element by a given SMARTS string. Look up an element from a list of known elements by SMARTS string. Return None if no match found. Parameters ---------- smarts_string : str SMARTS string representation of an atom type or its local chemical context. The Foyer SMARTS parser will be used to find the central atom and look up an Element. Note that this means some SMARTS grammar may not be parsed properly. For details, see https://github.com/mosdef-hub/foyer/issues/63 Returns ------- matched_element : element.Element Return an element from the periodic table if we find a match Raises ------ GMSOError If no matching element is found for the provided smarts string """ from gmso.utils.io import import_ foyer = import_("foyer") SMARTS = foyer.smarts.SMARTS PARSER = SMARTS() symbols = PARSER.parse(smarts_string).iter_subtrees_topdown() first_symbol = None for symbol in symbols: if symbol.data == "atom_symbol": first_symbol = symbol.children[0] break matched_element = None if first_symbol is not None: matched_element = element_by_symbol(first_symbol) if matched_element is None: raise GMSOError( f"Failed to find an element from SMARTS string {smarts_string}. The " f"parser detected a central node with name {first_symbol}") return matched_element
import numpy as np import unyt as u import pytest from gmso.formats.gro import read_gro, write_gro from gmso.external.convert_parmed import from_parmed from gmso.tests.base_test import BaseTest from gmso.utils.io import get_fn, import_, has_parmed from unyt.testing import assert_allclose_units if has_parmed: pmd = import_('parmed') @pytest.mark.skipif(not has_parmed, reason="ParmEd is not installed") class TestGro(BaseTest): def test_read_gro(self): top = read_gro(get_fn('acn.gro')) assert top.name == 'ACN' assert top.n_sites == 6 assert_allclose_units(top.box.lengths, 4*np.ones(3)*u.nm, rtol=1e-5, atol=1e-8) top = read_gro(get_fn('350-waters.gro')) assert top.name == 'Generic title' assert top.n_sites == 1050 assert_allclose_units(top.box.lengths, 2.20866*np.ones(3)*u.nm, rtol=1e-5, atol=1e-8) def test_wrong_n_atoms(self): with pytest.raises(ValueError):
import unyt as u from gmso.utils.io import import_, has_openmm, has_simtk_unit if has_openmm & has_simtk_unit: simtk_unit = import_('simtk.unit') from simtk.openmm.app import * from simtk.openmm import * def to_openmm(topology, openmm_object='topology'): """ Convert an untyped topology object to an untyped OpenMM modeller or topology. This is useful if it's preferred to atom-type a system within OpenMM. See http://openmm.org for more information. Parameters ---------- topology : `Topology` object An untyped topology object open_mm_object : 'topology' or 'modeller' OpenMM object, default='topology' Untyped OpenMM object to convert to Returns ------- open_mm_object : Untyped `topology` or `modeller` object """ openmm_top = app.Topology() # Get topology.positions into OpenMM form
plot_networkx_nodes, plot_networkx_params, report_bond_parameters, report_parameter_expression, return_labels_for_nodes, select_angles_from_sites, select_dihedrals_from_sites, select_edges, select_edges_on_networkx, select_params_on_networkx, show_bond_info, show_parameter_values, ) if has_matplotlib: plt = import_("matplotlib.pyplot") if has_ipywidgets: widgets = import_("ipywidgets") @pytest.mark.skipif(not has_ipywidgets, reason="Ipywidgets is not installed") @pytest.mark.skipif(not has_matplotlib, reason="Matplotlib is not installed") class TestNetworkx(BaseTest): def test_highlight_networkx_edges(self, typed_ethane): list(typed_ethane.angles)[0].angle_type = None list(typed_ethane.dihedrals)[0].dihedral_type = None graph = to_networkx(typed_ethane) list_edges = list(graph.edges)[0:3] test_edge_weights, test_edge_colors = highlight_networkx_edges( graph, list_edges[0:2]) assert test_edge_weights[list_edges[0]] == 5
import numpy as np import sympy as sym import unyt as u from sympy.parsing.sympy_parser import parse_expr import gmso from gmso.core.element import ( element_by_atom_type, element_by_name, element_by_symbol, ) from gmso.utils.io import has_parmed, import_ if has_parmed: pmd = import_("parmed") def from_parmed(structure, refer_type=True): """Convert a parmed.Structure to a gmso.Topology. Convert a parametrized or un-parametrized parmed.Structure object to a topology.Topology. Specifically, this method maps Structure to Topology and Atom to Site. At this point, this method can only convert AtomType, BondType and AngleType. Conversion of DihedralType will be implement in the near future. Parameters ---------- structure : parmed.Structure parmed.Structure instance that need to be converted. refer_type : bool, optional, default=True
import networkx as nx import unyt from gmso.utils.io import import_, has_ipywidgets widgets = import_('ipywidgets') plt = import_('matplotlib.pyplot') if has_ipywidgets: from ipywidgets import interact, fixed from gmso.external.convert_networkx import to_networkx def plot_networkx_params(networkx_graph, list_of_edges): """Get a networkx plot showing the specified edges in a networkx graph object. Parameters ---------- networkx_graph : A networkx.Graph object This is a networkx graph object that can be created from a topology using the to_networkx function found in gmso.external.convert_networkx list_of_edges : a list of edges that should be shown on the plot Will be of the shape [(node1,node2), (node2,node3), etc...] Returns ------- matplotlib.pyplot.figure The drawn networkx plot of the topology on a matplotlib editable figures matplotlib.pyplot.axis The axis information that corresponds to a networkx drawn figure. This output can be
"""Convert to and from an OpenMM Topology or System object.""" import unyt as u from gmso.utils.io import has_openmm, has_simtk_unit, import_ if has_openmm & has_simtk_unit: simtk_unit = import_("simtk.unit") from simtk.openmm import * from simtk.openmm.app import * def to_openmm(topology, openmm_object="topology"): """Convert an untyped topology object to an untyped OpenMM modeller or topology. This is useful if it's preferred to atom-type a system within OpenMM. See http://openmm.org for more information. Parameters ---------- topology : `Topology` object An untyped topology object open_mm_object : 'topology' or 'modeller' OpenMM object, default='topology' Untyped OpenMM object to convert to Returns ------- open_mm_object : Untyped `topology` or `modeller` object """ openmm_top = app.Topology()