Esempio n. 1
0
    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
Esempio n. 2
0
    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
Esempio n. 3
0
    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
Esempio n. 4
0
 def test_add_connection_non_member(self):
     site1, site2, site3 = (Site(), Site(), Site())
     bond1, bond2, bond3 = (Bond([site1, site2]), Bond([site2, site3]),
                            Bond([site1, site3]))
     assert site1.n_connections == site2.n_connections == site3.n_connections == 2
     with pytest.raises(GMSOError):
         site3.add_connection(bond1)
         site2.add_connection(bond3)
         site1.add_connection(bond2)
Esempio n. 5
0
    def test_add_equivalent_connections(self):
        atom1 = Atom(name="AtomA")
        atom2 = Atom(name="AtomB")

        bond = Bond(connection_members=[atom1, atom2])
        bond_eq = Bond(connection_members=[atom2, atom1])

        top = Topology()
        top.add_connection(bond)
        top.add_connection(bond_eq)
        assert top.n_bonds == 1
Esempio n. 6
0
    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
Esempio n. 7
0
    def test_bond_eq(self):
        atom1 = Atom(name='atom1', position=[0, 0, 0])
        atom2 = Atom(name='atom2', position=[1, 1, 1])

        ref_connection = Bond(connection_members=[atom1, atom2], )

        same_connection = Bond(connection_members=[atom1, atom2], )

        diff_connection = Bond(connection_members=[atom1, atom2], )

        assert ref_connection != same_connection
        assert ref_connection != diff_connection
Esempio n. 8
0
 def test_add_connection_redundant(self):
     site1, site2, site3 = (Site(), Site(), Site())
     bond1, bond2, bond3 = (Bond([site1, site2]), Bond([site2, site3]),
                            Bond([site1, site3]))
     assert site1.n_connections == site2.n_connections == site3.n_connections == 2
     site1.add_connection(bond1)
     site1.add_connection(bond3)
     site2.add_connection(bond1)
     site2.add_connection(bond2)
     site3.add_connection(bond3)
     site3.add_connection(bond2)
     assert site1.n_connections == site2.n_connections == site3.n_connections == 2
Esempio n. 9
0
    def test_bond_eq(self):
        site1 = Site(name='site1', position=[0, 0, 0])
        site2 = Site(name='site2', position=[1, 1, 1])

        ref_connection = Bond(connection_members=[site1, site2], )

        same_connection = Bond(connection_members=[site1, site2], )

        diff_connection = Bond(connection_members=[site1, site2], )

        assert ref_connection != same_connection
        assert ref_connection != diff_connection
Esempio n. 10
0
    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
Esempio n. 11
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
Esempio n. 12
0
    def test_bond_nonparametrized(self):
        atom1 = Atom(name='atom1')
        atom2 = Atom(name='atom2')

        connect = Bond(connection_members=[atom1, atom2])

        assert connect.bond_type is None
Esempio n. 13
0
    def ethane(self):
        mytop = Topology()
        c1 = Atom(name='C1')
        h11 = Atom(name='H11')
        h12 = Atom(name='H12')
        h13 = Atom(name='H13')

        c2 = Atom(name='C2')
        h21 = Atom(name='H21')
        h22 = Atom(name='H22')
        h23 = Atom(name='H23')

        c1h11 = Bond(connection_members=[c1, h11])
        c1h12 = Bond(connection_members=[c1, h12])
        c1h13 = Bond(connection_members=[c1, h13])

        c2h21 = Bond(connection_members=[c2, h21])
        c2h22 = Bond(connection_members=[c2, h22])
        c2h23 = Bond(connection_members=[c2, h23])

        c1c2 = Bond(connection_members=[c1, c2])

        mytop.add_connection(c1h11, update_types=False)
        mytop.add_connection(c1h12, update_types=False)
        mytop.add_connection(c1h13, update_types=False)

        mytop.add_connection(c2h21, update_types=False)
        mytop.add_connection(c2h22, update_types=False)
        mytop.add_connection(c2h23, update_types=False)

        mytop.add_connection(c1c2, update_types=False)
        mytop.update_topology()

        return mytop
Esempio n. 14
0
 def test_bond_to_json_loop(self, typed_ethane, are_equivalent_atoms):
     for bond in typed_ethane.bonds:
         bond_json = bond.json()
         bond_copy = Bond.parse_raw(bond_json)
         assert bond_copy.name == bond.name
         for member1, member2 in zip(bond.connection_members,
                                     bond_copy.connection_members):
             assert are_equivalent_atoms(member1, member2)
         assert bond_copy.bond_type == bond.bond_type
Esempio n. 15
0
    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
Esempio n. 16
0
    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
Esempio n. 17
0
    def test_bond_nonparametrized(self):
        site1 = Site(name='site1')
        site2 = Site(name='site2')

        assert site1.n_connections == 0
        assert site2.n_connections == 0

        connect = Bond(connection_members=[site1, site2])

        assert site1.n_connections == 1
        assert site2.n_connections == 1
        assert connect.connection_type is None
Esempio n. 18
0
 def test_bond_constituent_types(self):
     atom1 = Atom(name="atom1",
                  position=[0, 0, 0],
                  atom_type=AtomType(name="A"))
     atom2 = Atom(name="atom2",
                  position=[1, 0, 0],
                  atom_type=AtomType(name="B"))
     bondtype = BondType(
         member_types=[atom1.atom_type.name, atom2.atom_type.name])
     bond = Bond(connection_members=[atom1, atom2], bond_type=bondtype)
     assert "A" in bond.connection_type.member_types
     assert "B" in bond.connection_type.member_types
Esempio n. 19
0
 def test_bond_constituent_types(self):
     atom1 = Atom(name='atom1',
                  position=[0, 0, 0],
                  atom_type=AtomType(name='A'))
     atom2 = Atom(name='atom2',
                  position=[1, 0, 0],
                  atom_type=AtomType(name='B'))
     bondtype = BondType(
         member_types=[atom1.atom_type.name, atom2.atom_type.name])
     bond = Bond(connection_members=[atom1, atom2], bond_type=bondtype)
     assert 'A' in bond.connection_type.member_types
     assert 'B' in bond.connection_type.member_types
Esempio n. 20
0
    def test_bond_parametrized(self):
        atom1 = Atom(name='atom1')
        atom2 = Atom(name='atom2')

        bond_type = BondType()

        connect = Bond(connection_members=[atom1, atom2],
                       bond_type=bond_type,
                       name='bond_name')

        assert len(connect.connection_members) == 2
        assert connect.connection_type is not None
        assert connect.name == 'bond_name'
Esempio n. 21
0
 def test_bond_constituent_types(self):
     site1 = Site(name='site1',
                  position=[0, 0, 0],
                  atom_type=AtomType(name='A'))
     site2 = Site(name='site2',
                  position=[1, 0, 0],
                  atom_type=AtomType(name='B'))
     bondtype = BondType(
         member_types=[site1.atom_type.name, site2.atom_type.name])
     bond = Bond(connection_members=[site1, site2],
                 connection_type=bondtype)
     assert 'A' in bond.connection_type.member_types
     assert 'B' in bond.connection_type.member_types
Esempio n. 22
0
    def test_bond_every_field_set(self, full_atom_type, are_equivalent_atoms):
        atom1 = Atom(
            name="test_atom1",
            label="test_label1",
            position=[0.0, 0.0, 0.0],
            charge=1.5,
            mass=2.0,
            element=element_by_symbol("C"),
            atom_type=full_atom_type,
        )

        atom2 = Atom(
            name="test_atom1",
            label="test_label1",
            position=[0.1, 0.4, 0.5],
            charge=5,
            mass=2.6,
            element=element_by_symbol("H"),
            atom_type=full_atom_type,
        )

        bond = Bond(name="test_bond1", connection_members=(atom1, atom2))

        bond_type = BondType(
            name="test_bond_type",
            expression="a*b+c**2",
            parameters={
                "a": 10 * u.nm,
                "b": 20 * u.angstrom
            },
            independent_variables={"c"},
        )

        bond_copy = Bond.parse_raw(bond.json())
        assert bond_copy.name == bond.name
        for member1, member2 in zip(bond.connection_members,
                                    bond_copy.connection_members):
            assert are_equivalent_atoms(member1, member2)
        assert bond_copy.bond_type == bond.bond_type
Esempio n. 23
0
    def methane(self):
        mytop = Topology()
        c = Atom(name='c')
        h1 = Atom(name='h1')
        h2 = Atom(name='h2')
        h3 = Atom(name='h3')
        h4 = Atom(name='h4')
        ch1 = Bond(connection_members=[c, h1])
        ch2 = Bond(connection_members=[c, h2])
        ch3 = Bond(connection_members=[c, h3])
        ch4 = Bond(connection_members=[c, h4])
        mytop.add_site(c, update_types=False)
        mytop.add_site(h1, update_types=False)
        mytop.add_site(h2, update_types=False)
        mytop.add_site(h3, update_types=False)
        mytop.add_site(h4, update_types=False)
        mytop.add_connection(ch1, update_types=False)
        mytop.add_connection(ch2, update_types=False)
        mytop.add_connection(ch3, update_types=False)
        mytop.add_connection(ch4, update_types=False)
        mytop.update_topology()

        return mytop
Esempio n. 24
0
    def test_add_untyped_bond_update(self):
        site1 = Site(atom_type=None)
        site2 = Site(atom_type=None)
        bond = Bond(connection_members=[site1, site2], connection_type=None)

        top = Topology()
        assert len(top.bond_types) == 0
        top.add_connection(bond, update_types=False)
        assert len(top.bond_types) == 0

        top = Topology()
        assert len(top.bond_types) == 0
        top.add_connection(bond, update_types=True)
        assert len(top.bond_types) == 0
Esempio n. 25
0
    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
Esempio n. 26
0
    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
Esempio n. 27
0
    def test_equivalent_members_set(self):
        atom1 = Atom(name="AtomA")
        atom2 = Atom(name="AtomB")

        bond = Bond(connection_members=[atom1, atom2])
        bond_eq = Bond(connection_members=[atom2, atom1])

        assert (tuple(bond_eq.connection_members) in bond.equivalent_members())
        assert (tuple(bond.connection_members) in bond_eq.equivalent_members())
Esempio n. 28
0
    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
Esempio n. 29
0
    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
Esempio n. 30
0
    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