def test_change_comb_rule(self): top = Topology() assert top.combining_rule == 'lorentz' top.combining_rule = 'geometric' assert top.combining_rule == 'geometric' with pytest.raises(GMSOError): top.combining_rule = 'kong'
def test_add_site(self): top = Topology() site = Atom(name="site") assert top.n_sites == 0 top.add_site(site) assert top.n_sites == 1
def test_add_subtopology(self): top = Topology() subtop = SubTopology() assert top.n_subtops == 0 top.add_subtopology(subtop) assert top.n_subtops == 1
def test_change_comb_rule(self): top = Topology() assert top.combining_rule == "lorentz" top.combining_rule = "geometric" assert top.combining_rule == "geometric" with pytest.raises(GMSOError): top.combining_rule = "kong"
def test_add_site(self): top = Topology() site = Site(name='site') assert top.n_sites == 0 top.add_site(site) assert top.n_sites == 1
def test_to_mbuild_name_none(self): top = Top() top.add_site(Atom(position=[0.0, 0.0, 0.0])) top.name = None compound = to_mbuild(top) assert compound.name == "Compound"
def test_to_mbuild_name_none(self): top = Top() top.add_site(Site()) top.name = None compound = to_mbuild(top) assert compound.name == 'Compound'
def test_add_box(self): top = Topology() box = Box(2 * u.nm * np.ones(3)) assert top.box is None top.box = box assert top.box is not None assert allclose(top.box.lengths, u.nm * 2 * np.ones(3))
def test_parametrization_setter(self): top = Topology() assert top.typed == False assert top.is_typed() == False top.typed = True assert top.typed == True assert top.is_typed() == False
def test_parametrization(self): top = Topology() assert top.typed == False top.add_site(Site(atom_type=AtomType())) assert top.typed == True assert top.is_typed() == True assert top.typed == True
def test_add_box(self): top = Topology() box = Box(2 * u.nm * np.ones(3)) assert top.box is None top.box = box assert top.box is not None assert_allclose_units( top.box.lengths, u.nm * 2 * np.ones(3), rtol=1e-5, atol=1e-8 )
def test_positions_dtype(self): top = Topology() site1 = Site(name='site1') top.add_site(site1) assert set([type(site.position) for site in top.sites]) == {u.unyt_array} assert set([site.position.units for site in top.sites]) == {u.nm} assert top.positions.dtype == float assert top.positions.units == u.nm assert isinstance(top.positions, u.unyt_array)
def test_positions_dtype(self): top = Topology() atom1 = Atom(name='atom1', position=[0.0, 0.0, 0.0]) top.add_site(atom1) assert set([type(site.position) for site in top.sites]) == {u.unyt_array} assert set([site.position.units for site in top.sites]) == {u.nm} assert top.positions.dtype == float assert top.positions.units == u.nm assert isinstance(top.positions, u.unyt_array)
def test_3_layer_top(self): top_top = Top() mid_top = SubTop() site = Site() top_top.add_subtopology(mid_top) mid_top.add_site(site) compound = to_mbuild(top_top) assert len(compound.children) == 1 assert compound.children[0].n_particles == 1 assert compound.n_particles == 1
def _topology(sites=1): top = Topology() top.box = Box(lengths=[1, 1, 1]) H = Hydrogen site1 = Site( name='site1', element=H, atom_type=AtomType(name="at1", mass=H.mass), ) for i in range(sites): top.add_site(site1) return top
def test_atom_type_with_topology_and_site_change_properties(self): site1 = Site() site2 = Site() top = Topology() atom_type1 = AtomType() atom_type2 = AtomType() site1.atom_type = atom_type1 site2.atom_type = atom_type2 top.add_site(site1) top.add_site(site2) site1.atom_type.mass = 250 assert site2.atom_type.mass == 250 assert top.atom_types[0].mass == 250
def test_add_typed_bond_update(self): atom1 = Atom(atom_type=None) atom2 = Atom(atom_type=None) bond = Bond(connection_members=[atom1, atom2], bond_type=BondType()) top = Topology() top.add_site(atom1) top.add_site(atom2) top.add_connection(bond, update_types=False) assert len(top.connection_types) == 0 top = Topology() top.add_connection(bond, update_types=True) assert len(top.bond_types) == 1
def test_square(self): mytop = Topology() s1 = Atom(name="1") s2 = Atom(name="2") s3 = Atom(name="3") s4 = Atom(name="4") c12 = Bond(connection_members=[s1, s2]) c23 = Bond(connection_members=[s2, s3]) c34 = Bond(connection_members=[s3, s4]) c41 = Bond(connection_members=[s4, s1]) for site in [s1, s2, s3, s4]: mytop.add_site(site, update_types=False) for conn in [c12, c23, c34, c41]: mytop.add_connection(conn, update_types=False) assert mytop.n_bonds == 4 assert mytop.n_angles == 0 assert mytop.n_dihedrals == 0 assert mytop.n_impropers == 0 mytop.identify_connections() assert mytop.n_bonds == 4 assert mytop.n_angles == 4 assert mytop.n_dihedrals == 4 assert mytop.n_impropers == 0
def from_networkx(graph): """Convert a networkx.Graph to a gmso.Topology Creates a topology from the graph where each node is a site and each edge becomes a connection. Parameters ---------- graph : networkX.Graph networkx.Graph instance that need to be converted Returns ------- top : gmso.Topology Notes ----- - While a lot of information is lost from converting to a graph object (e.g. metadata, mixing rules, etc.), the graph representation is a useful way to manipulate and extract connectivity information from Topology objects. - The edge has a `connection` attribute, which stores the Bond object it was created from """ if not isinstance(graph, nx.Graph): raise TypeError( "Type mismatch, graph object is expected to be " "an instance of networkx.Graph, was provided: {}".format( type(graph))) top = Topology() node_mapping = dict() for node in graph.nodes: if not isinstance(node, Site): raise TypeError("Nodes must be instances of gmso.abc.Site") else: top.add_site(node) for edge in graph.edges: try: conn = graph.get_edge_data(*edge)["connection"] if (isinstance(conn, Connection) and set(edge).issubset(set(conn.connection_members))): top.add_connection(conn) except KeyError: conn = Bond(connection_members=edge) top.add_connection(conn) warnings.simplefilter('once', UserWarning) for node in graph.nodes: try: graph.nodes[node]['angles'] or graph.nodes[node]['dihedrals'] warnings.warn("Angle and Dihedral information is not converted.") except KeyError: pass return top
def test_atom_type_with_topology_and_site(self): site1 = Site() site2 = Site() top = Topology() atom_type1 = AtomType() atom_type2 = AtomType() site1.atom_type = atom_type1 site2.atom_type = atom_type2 top.add_site(site1) top.add_site(site2) assert id(site1.atom_type) == id(site2.atom_type) assert site1.atom_type is not None assert len(top.atom_types) == 1 assert site1.atom_type.topology == top assert site2.atom_type.topology == top
def test_add_typed_bond_update(self): site1 = Site(atom_type=None) site2 = Site(atom_type=None) bond = Bond(connection_members=[site1, site2], connection_type=BondType()) top = Topology() top.add_site(site1) top.add_site(site2) top.add_connection(bond, update_types=False) assert len(top.connection_types) == 0 top = Topology() top.add_connection(bond, update_types=True) assert len(top.bond_types) == 1
def to_mbuild(topology): """ Convert a gmso.Topology to mbuild.Compound Parameters ---------- topology : gmso.Topology topology instance that need to be converted Returns ------- compound : mbuild.Compound """ msg = ("Argument topology is not a Topology") assert isinstance(topology, Topology), msg compound = mb.Compound() if topology.name is Topology().name: compound.name = 'Compound' else: compound.name = topology.name particle_map = dict() for site in topology.sites: particle = mb.Compound(name=site.name, pos=site.position[0]) particle_map[site] = particle compound.add(particle) for connect in topology.connections: if isinstance(connect, Bond): compound.add_bond((particle_map[connect.connection_members[0]], particle_map[connect.connection_members[1]])) return compound
def test_subtop_assign_parent(self): subtop = SubTopology() top = Topology() subtop.parent = top assert subtop.parent is not None
def test_atomtype_update(self): top = Topology() assert top.n_sites == 0 assert top.n_bonds == 0 assert top.n_connections == 0 atype1 = AtomType(expression="sigma + epsilon*r") atype2 = AtomType(expression="sigma * epsilon*r") atom1 = Atom(name="a", atom_type=atype1) atom2 = Atom(name="b", atom_type=atype2) top.add_site(atom1) top.add_site(atom2) assert top.n_sites == 2 assert len(top.atom_types) == 2 assert len(top.atom_type_expressions) == 2
def test_atomtype_update(self): top = Topology() assert top.n_sites == 0 assert top.n_bonds == 0 assert top.n_connections == 0 atype1 = AtomType(expression='sigma + epsilon') atype2 = AtomType(expression='sigma * epsilon') site1 = Site('a', atom_type=atype1) site2 = Site('b', atom_type=atype2) top.add_site(site1) top.add_site(site2) assert top.n_sites == 2 assert len(top.atom_types) == 2 assert len(top.atom_type_expressions) == 2
def test_add_duplicate_connected_atom(self): top = Topology() atom1 = Atom(name="AtomA") atom2 = Atom(name="AtomB") bond = Bond(connection_members=[atom1, atom2]) bond_eq = Bond(connection_members=[atom1, atom2]) top.add_connection(bond) top.add_connection(bond_eq) top.update_topology() assert top.n_connections == 1
def test_subtop_add_site_parent(self): top = Topology() subtop = SubTopology(parent=top) site = Atom() subtop.add_site(site) assert subtop.n_sites == 1 assert subtop.parent.n_sites == 1 assert top.n_sites == 1
def test_add_untyped_site_update(self): untyped_site = Site(atom_type=None) top = Topology() assert len(top.atom_types) == 0 top.add_site(untyped_site, update_types=False) assert len(top.atom_types) == 0 top = Topology() assert len(top.atom_types) == 0 top.add_site(untyped_site, update_types=True) assert len(top.atom_types) == 0
def test_add_typed_site_update(self): typed_site = Site(atom_type=AtomType()) top = Topology() assert len(top.atom_types) == 0 top.add_site(typed_site, update_types=False) assert len(top.atom_types) == 0 top = Topology() assert len(top.atom_types) == 0 top.add_site(typed_site, update_types=True) assert len(top.atom_types) == 1
def test_subtop_setters(self): subtop = SubTopology() assert subtop.name == 'Sub-Topology' assert subtop.parent is None assert subtop.n_sites == 0 subtop.name = 'NewSubTopology' assert subtop.name == 'NewSubTopology' newparent = Topology() subtop.parent = newparent assert subtop.parent == newparent