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(Site()) top.name = None compound = to_mbuild(top) assert compound.name == 'Compound'
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_topology_get_index(self): top = Topology() conn_members = [Site(), Site(), Site(), Site()] for i in range(5): top.add_site(Site()) top.add_connection(Bond( connection_members=[conn_members[0], conn_members[1]])) top.add_connection(Angle( connection_members=[conn_members[0], conn_members[1], conn_members[2]])) top.add_connection(Dihedral( connection_members=[conn_members[0], conn_members[1], conn_members[2], conn_members[3]])) top.add_connection(Improper( connection_members=[conn_members[0], conn_members[1], conn_members[2], conn_members[3]])) a_bond = Bond(connection_members=[conn_members[0], conn_members[1]]) an_angle = Angle(connection_members=[conn_members[0], conn_members[1], conn_members[2]]) a_site = Site() a_dihedral = Dihedral(connection_members=[conn_members[0], conn_members[1], conn_members[2], conn_members[3]]) an_improper = Improper(connection_members=[conn_members[0], conn_members[1], conn_members[2], conn_members[3]]) top.add_site(a_site) top.add_connection(a_bond) top.add_connection(an_angle) top.add_connection(a_dihedral) top.add_connection(an_improper) assert top.get_index(a_site) == 9 assert top.get_index(a_bond) == 5 assert top.get_index(an_angle) == 5 assert top.get_index(a_dihedral) == 5 assert top.get_index(an_improper) == 5
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_square_with_bridge(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]) c24 = Bond(connection_members=[s2, s4]) mytop.add_site(s1, update_types=False) mytop.add_site(s2, update_types=False) mytop.add_site(s3, update_types=False) mytop.add_site(s4, update_types=False) mytop.add_connection(c12, update_types=False) mytop.add_connection(c23, update_types=False) mytop.add_connection(c34, update_types=False) mytop.add_connection(c41, update_types=False) mytop.add_connection(c24, update_types=False) assert mytop.n_bonds == 5 assert mytop.n_angles == 0 assert mytop.n_dihedrals == 0 assert mytop.n_impropers == 0 mytop.identify_connections() assert mytop.n_bonds == 5 assert mytop.n_angles == 8 assert mytop.n_dihedrals == 6 assert mytop.n_impropers == 2
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 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_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_with_1000_atom_types(self): top = Topology() for i in range(1000): site = Site() atom_type = AtomType() site.atom_type = atom_type top.add_site(site, update_types=False) top.update_topology() assert len(top.atom_types) == 1 assert top.n_sites == 1000
def test_add_connection(self): top = Topology() site1 = Site(name='site1') site2 = Site(name='site2') connect = Bond(connection_members=[site1, site2]) top.add_connection(connect) top.add_site(site1) top.add_site(site2) assert len(top.connections) == 1
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_add_connection(self): top = Topology() atom1 = Atom(name='atom1') atom2 = Atom(name='atom2') connect = Bond(connection_members=[atom1, atom2]) top.add_connection(connect) top.add_site(atom1) top.add_site(atom2) assert len(top.connections) == 1
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_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_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_topology_atom_type_changes(self): top = Topology() for i in range(100): site = Site(name='site{}'.format(i)) atom_type = AtomType(name='atom_type{}'.format(i % 10)) site.atom_type = atom_type top.add_site(site, update_types=False) top.update_topology() assert len(top.atom_types) == 10 top.sites[0].atom_type.name = 'atom_type_changed' assert id(top.sites[0].atom_type) == id(top.sites[10].atom_type) assert top.sites[10].atom_type.name == 'atom_type_changed' assert top.is_typed()
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 _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_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_top_update(self): top = Topology() top.update_topology() assert top.n_sites == 0 assert len(top.atom_types) == 0 assert len(top.atom_type_expressions) == 0 assert top.n_connections == 0 assert len(top.connection_types) == 0 assert len(top.connection_type_expressions) == 0 atomtype = AtomType() site1 = Site(name='site1', atom_type=atomtype) top.add_site(site1) site2 = Site(name='site2', atom_type=atomtype) top.add_site(site2) assert top.n_sites == 2 assert len(top.atom_types) == 1 assert len(top.atom_type_expressions) == 1 assert top.n_connections == 0 assert len(top.connection_types) == 0 assert len(top.connection_type_expressions) == 0 ctype = BondType() connection_12 = Bond(connection_members=[site1, site2], connection_type=ctype) top.add_connection(connection_12) assert top.n_sites == 2 assert len(top.atom_types) == 1 assert len(top.atom_type_expressions) == 1 assert top.n_connections == 1 assert len(top.connection_types) == 1 assert len(top.connection_type_expressions) == 1 site1.atom_type = AtomType(expression='sigma*epsilon') assert top.n_sites == 2 assert len(top.atom_types) == 1 assert len(top.atom_type_expressions) == 1 assert top.n_connections == 1 assert len(top.connection_types) == 1 assert len(top.connection_type_expressions) == 1 top.update_atom_types() assert top.n_sites == 2 assert len(top.atom_types) == 2 assert len(top.atom_type_expressions) == 2 assert top.n_connections == 1 assert len(top.connection_types) == 1 assert len(top.connection_type_expressions) == 1
def test_top_update(self): top = Topology() top.update_topology() assert top.n_sites == 0 assert len(top.atom_types) == 0 assert len(top.atom_type_expressions) == 0 assert top.n_connections == 0 assert len(top.connection_types) == 0 assert len(top.connection_type_expressions) == 0 atomtype = AtomType() atom1 = Atom(name='atom1', atom_type=atomtype) top.add_site(atom1) atom2 = Atom(name='atom2', atom_type=atomtype) top.add_site(atom2) assert top.n_sites == 2 assert len(top.atom_types) == 1 assert len(top.atom_type_expressions) == 1 assert top.n_connections == 0 assert len(top.connection_types) == 0 assert len(top.connection_type_expressions) == 0 ctype = BondType() connection_12 = Bond(connection_members=[atom1, atom2], bond_type=ctype) top.add_connection(connection_12) assert top.n_sites == 2 assert len(top.atom_types) == 1 assert len(top.atom_type_expressions) == 1 assert top.n_connections == 1 assert len(top.connection_types) == 1 assert len(top.connection_type_expressions) == 1 atom1.atom_type = AtomType(expression='sigma*epsilon*r') assert top.n_sites == 2 assert len(top.atom_types) == 1 assert len(top.atom_type_expressions) == 1 assert top.n_connections == 1 assert len(top.connection_types) == 1 assert len(top.connection_type_expressions) == 1 top.update_atom_types() assert top.n_sites == 2 assert len(top.atom_types) == 2 assert len(top.atom_type_expressions) == 2 assert top.n_connections == 1 assert len(top.connection_types) == 1 assert len(top.connection_type_expressions) == 1
def test_add_site_parent(self): top = Topology() subtop = SubTopology() site1 = Atom(position=u.nm * np.zeros(3)) site2 = Atom(position=u.nm * np.ones(3)) top.add_subtopology(subtop) assert top.n_sites == 0 assert subtop.n_sites == 0 subtop.add_site(site1) assert top.n_sites == 1 assert subtop.n_sites == 1 top.add_site(site2) assert top.n_sites == 2 assert subtop.n_sites == 1
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 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_topology_get_index(self): top = Topology() conn_members = [Atom() for _ in range(10)] for atom in conn_members: top.add_site(atom) for i in range(5): top.add_connection( Bond(connection_members=[conn_members[i], conn_members[i + 1]])) top.add_connection( Angle(connection_members=[ conn_members[i], conn_members[i + 1], conn_members[i + 2] ])) top.add_connection( Dihedral(connection_members=[ conn_members[i], conn_members[i + 1], conn_members[i + 2], conn_members[i + 3] ])) top.add_connection( Improper(connection_members=[ conn_members[i], conn_members[i + 1], conn_members[i + 2], conn_members[i + 3] ])) a_atom = Atom() a_bond = Bond(connection_members=[conn_members[6], conn_members[7]]) an_angle = Angle(connection_members=[ conn_members[6], conn_members[7], conn_members[8] ]) a_dihedral = Dihedral(connection_members=[ conn_members[6], conn_members[7], conn_members[8], conn_members[9] ]) an_improper = Improper(connection_members=[ conn_members[6], conn_members[7], conn_members[8], conn_members[9] ]) top.add_site(a_atom) top.add_connection(a_bond) top.add_connection(an_angle) top.add_connection(a_dihedral) top.add_connection(an_improper) assert top.get_index(a_atom) == 10 assert top.get_index(a_bond) == 5 assert top.get_index(an_angle) == 5 assert top.get_index(a_dihedral) == 5 assert top.get_index(an_improper) == 5
def test_bond_bondtype_update(self): top = Topology() 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) btype = BondType() bond = Bond(connection_members=[atom1, atom2], bond_type=btype) top.add_site(atom1) top.add_site(atom2) top.add_connection(bond) assert top.n_bonds == 1 assert len(top.bond_types) == 1 assert len(top.bond_type_expressions) == 1
def test_bond_bondtype_update(self): top = Topology() atype1 = AtomType(expression='sigma + epsilon') atype2 = AtomType(expression='sigma * epsilon') site1 = Site('a', atom_type=atype1) site2 = Site('b', atom_type=atype2) btype = BondType() bond = Bond(connection_members=[site1, site2], connection_type=btype) top.add_site(site1) top.add_site(site2) top.add_connection(bond) assert top.n_bonds == 1 assert len(top.bond_types) == 1 assert len(top.bond_type_expressions) == 1
def read_xyz(filename): """Reader for xyz file format. Read in an xyz file at the given path and return a Topology object. Parameters ---------- filename : str Path to .xyz file that need to be read. Return ------ top : topology.Topology Topology object """ top = Topology() with open(filename, "r") as xyz_file: n_atoms = int(xyz_file.readline()) xyz_file.readline() coords = np.zeros(shape=(n_atoms, 3)) * u.nanometer for row, _ in enumerate(coords): line = xyz_file.readline().split() if not line: msg = ( "Incorrect number of lines in input file. Based on the " "number in the first line of the file, {} rows of atoms " "were expected, but at least one fewer was found." ) raise ValueError(msg.format(n_atoms)) tmp = np.array(line[1:4], dtype=np.float) * u.angstrom coords[row] = tmp.in_units(u.nanometer) site = Atom(name=line[0], position=coords[row]) top.add_site(site) top.update_topology() # Verify we have read the last line by ensuring the next line in blank line = xyz_file.readline().split() if line: msg = ( "Incorrect number of lines in input file. Based on the " "number in the first line of the file, {} rows of atoms " "were expected, but at least one more was found." ) raise ValueError(msg.format(n_atoms)) return top
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