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_angle_eq(self): atom1 = Atom(name='atom1', position=[0, 0, 0]) atom2 = Atom(name='atom2', position=[1, 1, 1]) atom3 = Atom(name='atom3', position=[1, 1, 1]) ref_angle = Angle(connection_members=[atom1, atom2, atom3], ) same_angle = Angle(connection_members=[atom1, atom2, atom3], ) diff_angle = Angle(connection_members=[atom3, atom2, atom1], ) assert ref_angle != same_angle assert ref_angle != diff_angle
def test_angle_eq(self): site1 = Site(name='site1', position=[0, 0, 0]) site2 = Site(name='site2', position=[1, 1, 1]) site3 = Site(name='site3', position=[1, 1, 1]) ref_angle = Angle(connection_members=[site1, site2, site3], ) same_angle = Angle(connection_members=[site1, site2, site3], ) diff_angle = Angle(connection_members=[site3, site2, site1], ) assert ref_angle != same_angle assert ref_angle != diff_angle
def test_equivalent_members_set(self): atom1 = Atom(name="AtomA") atom2 = Atom(name="AtomB") atom3 = Atom(name="AtomC") angle = Angle(connection_members=[atom1, atom2, atom3]) angle_eq = Angle(connection_members=[atom3, atom2, atom1]) angle_not_eq = Angle(connection_members=[atom1, atom3, atom2]) assert tuple(angle_eq.connection_members) in angle.equivalent_members() assert tuple(angle.connection_members) in angle_eq.equivalent_members() assert not (tuple(angle.connection_members) in angle_not_eq.equivalent_members())
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_angle_nonparametrized(self): atom1 = Atom(name='atom1') atom2 = Atom(name='atom2') atom3 = Atom(name='atom3') connect = Angle(connection_members=[atom1, atom2, atom3]) assert connect.angle_type is None
def test_angle_fake_angletype(self): atom1 = Atom(name='atom1') atom2 = Atom(name='atom2') atom3 = Atom(name='atom3') with pytest.raises(ValidationError): Angle(connection_members=[atom1, atom2, atom3], angle_type='Fake angletype')
def test_angle_fake_angletype(self): site1 = Site(name='site1') site2 = Site(name='site2') site3 = Site(name='site3') with pytest.raises(GMSOError): Angle(connection_members=[site1, site2, site3], connection_type='Fake angletype')
def test_add_equivalent_connections(self): atom1 = Atom(name="AtomA") atom2 = Atom(name="AtomB") atom3 = Atom(name="AtomC") angle = Angle(connection_members=[atom1, atom2, atom3]) angle_eq = Angle(connection_members=[atom3, atom2, atom1]) angle_not_eq = Angle(connection_members=[atom1, atom3, atom2]) top = Topology() top.add_connection(angle) top.add_connection(angle_eq) assert top.n_angles == 1 top.add_connection(angle_not_eq) assert top.n_angles == 2
def test_angle_fake_angletype(self): atom1 = Atom(name="atom1") atom2 = Atom(name="atom2") atom3 = Atom(name="atom3") with pytest.raises(ValidationError): Angle( connection_members=[atom1, atom2, atom3], angle_type="Fake angletype", )
def test_angle_parametrized(self): atom1 = Atom(name='atom1') atom2 = Atom(name='atom2') atom3 = Atom(name='atom3') angle_type = AngleType() connect = Angle(connection_members=[atom1, atom2, atom3], angle_type=angle_type, name='angle_name') assert len(connect.connection_members) == 3 assert connect.angle_type is not None assert connect.name == 'angle_name'
def test_angle_nonparametrized(self): site1 = Site(name='site1') site2 = Site(name='site2') site3 = Site(name='site3') assert site1.n_connections == 0 assert site2.n_connections == 0 assert site3.n_connections == 0 connect = Angle(connection_members=[site1, site2, site3]) assert site1.n_connections == 1 assert site2.n_connections == 1 assert site3.n_connections == 1 assert connect.connection_type is None
def test_angle_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')) site3 = Site(name='site3', position=[1, 1, 0], atom_type=AtomType(name='C')) angtype = AngleType(member_types=[ site1.atom_type.name, site2.atom_type.name, site3.atom_type.name ]) ang = Angle(connection_members=[site1, site2, site3], connection_type=angtype) assert 'A' in ang.connection_type.member_types assert 'B' in ang.connection_type.member_types assert 'C' in ang.connection_type.member_types
def test_angle_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')) atom3 = Atom(name='atom3', position=[1, 1, 0], atom_type=AtomType(name='C')) angtype = AngleType(member_types=[ atom1.atom_type.name, atom2.atom_type.name, atom3.atom_type.name ]) ang = Angle(connection_members=[atom1, atom2, atom3], angle_type=angtype) assert 'A' in ang.angle_type.member_types assert 'B' in ang.angle_type.member_types assert 'C' in ang.angle_type.member_types
def test_angle_angletype_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) site3 = Site('c', atom_type=atype2) atype = AngleType() angle = Angle(connection_members=[site1, site2, site3], connection_type=atype, name='angle_name') top.add_site(site1) top.add_site(site2) top.add_site(site3) top.add_connection(angle) assert top.n_angles == 1 assert len(top.angle_types) == 1 assert len(top.angle_type_expressions) == 1 assert len(top.atom_type_expressions) == 2
def test_angle_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")) atom3 = Atom(name="atom3", position=[1, 1, 0], atom_type=AtomType(name="C")) angtype = AngleType(member_types=[ atom1.atom_type.name, atom2.atom_type.name, atom3.atom_type.name, ]) ang = Angle(connection_members=[atom1, atom2, atom3], angle_type=angtype) assert "A" in ang.angle_type.member_types assert "B" in ang.angle_type.member_types assert "C" in ang.angle_type.member_types
def test_angle_parametrized(self): site1 = Site(name='site1') site2 = Site(name='site2') site3 = Site(name='site3') assert site1.n_connections == 0 assert site2.n_connections == 0 assert site3.n_connections == 0 angle_type = AngleType() connect = Angle(connection_members=[site1, site2, site3], connection_type=angle_type, name='angle_name') assert site1.n_connections == 1 assert site2.n_connections == 1 assert site3.n_connections == 1 assert len(connect.connection_members) == 3 assert connect.connection_type is not None assert connect.name == 'angle_name'
def test_angle_angletype_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) atom3 = Atom(name='c', atom_type=atype2) atype = AngleType() angle = Angle(connection_members=[atom1, atom2, atom3], angle_type=atype, name='angle_name') top.add_site(atom1) top.add_site(atom2) top.add_site(atom3) top.add_connection(angle) assert top.n_angles == 1 assert len(top.angle_types) == 1 assert len(top.angle_type_expressions) == 1 assert len(top.atom_type_expressions) == 2
def typed_water_system(self, water_system): top = water_system ff = ForceField(get_path('tip3p.xml')) element_map = {"O": "opls_111", "H": "opls_112"} for atom in top.sites: atom.atom_type = ff.atom_types[atom.name] for bond in top.bonds: bond.connection_type = ff.bond_types["opls_111~opls_112"] for subtop in top.subtops: angle = Angle( connection_members=[site for site in subtop.sites], name="opls_112~opls_111~opls_112", connection_type=ff.angle_types["opls_112~opls_111~opls_112"]) top.add_connection(angle) top.update_topology() return top
def _get_connection(filename, topology, unit_style, connection_type): """General function to parse connection types """ with open(filename, 'r') as lammps_file: types = False for i, line in enumerate(lammps_file): if connection_type in line.split(): n_connection_types = int(line.split()[0]) types = True if connection_type.capitalize() in line.split(): break if types == False: return topology connection_type_lines = open(filename, 'r').readlines()[i+2:i+n_connection_types+2] connection_type_list = list() for line in connection_type_lines: if connection_type == 'bond': c_type = BondType(name=line.split()[0] ) # Multiply 'k' by 2 since LAMMPS includes 1/2 in the term c_type.parameters['k']=float(line.split()[1])*u.Unit( get_units(unit_style)['energy']/ get_units(unit_style)['distance']**2 )*2 c_type.parameters['r_eq']=float(line.split()[2])*(get_units(unit_style)['distance']**2) elif connection_type == 'angle': c_type = AngleType(name=line.split()[0] ) # Multiply 'k' by 2 since LAMMPS includes 1/2 in the term c_type.parameters['k']=float(line.split()[1])*u.Unit( get_units(unit_style)['energy']/ get_units(unit_style)['angle_k']**2 )*2 c_type.parameters['theta_eq']=float(line.split()[2])*u.Unit( get_units(unit_style)['angle']) connection_type_list.append(c_type) with open(filename, 'r') as lammps_file: for i, line in enumerate(lammps_file): if connection_type + 's' in line.split(): n_connections = int(line.split()[0]) if connection_type.capitalize() + 's' in line.split(): break connection_lines = open(filename, 'r').readlines()[i+2:i+n_connections+2] # Determine number of sites to generate if connection_type == 'bond': n_sites = 2 elif connection_type == 'angle': n_sites = 3 else: n_sites = 4 for i, line in enumerate(connection_lines): site_list = list() for j in range(n_sites): site = topology.sites[int(line.split()[j+2])-1] site_list.append(site) if connection_type == 'bond': connection = Bond( connection_members=site_list, connection_type=connection_type_list[int(line.split()[1])-1], ) elif connection_type == 'angle': connection = Angle( connection_members=site_list, connection_type=connection_type_list[int(line.split()[1])-1], ) topology.add_connection(connection) return topology
def test_angle_fake(self): site1 = Site(name='site1') site2 = Site(name='site2') site3 = Site(name='site3') with pytest.raises(GMSOError): Angle(connection_members=['fakesite1', 'fakesite2', 4.2])
def _get_connection(filename, topology, unit_style, connection_type): """Parse connection types.""" with open(filename, "r") as lammps_file: types = False for i, line in enumerate(lammps_file): if connection_type in line.split(): n_connection_types = int(line.split()[0]) types = True if connection_type.capitalize() in line.split(): break if types == False: return topology connection_type_lines = open( filename, "r").readlines()[i + 2:i + n_connection_types + 2] connection_type_list = list() for line in connection_type_lines: if connection_type == "bond": c_type = BondType(name=line.split()[0]) # Multiply 'k' by 2 since LAMMPS includes 1/2 in the term c_type.parameters["k"] = (float(line.split()[1]) * u.Unit( get_units(unit_style)["energy"] / get_units(unit_style)["distance"]**2) * 2) c_type.parameters["r_eq"] = float( line.split()[2]) * (get_units(unit_style)["distance"]**2) elif connection_type == "angle": c_type = AngleType(name=line.split()[0]) # Multiply 'k' by 2 since LAMMPS includes 1/2 in the term c_type.parameters["k"] = (float(line.split()[1]) * u.Unit( get_units(unit_style)["energy"] / get_units(unit_style)["angle_k"]**2) * 2) c_type.parameters["theta_eq"] = float(line.split()[2]) * u.Unit( get_units(unit_style)["angle"]) connection_type_list.append(c_type) with open(filename, "r") as lammps_file: for i, line in enumerate(lammps_file): if connection_type + "s" in line.split(): n_connections = int(line.split()[0]) if connection_type.capitalize() + "s" in line.split(): break connection_lines = open(filename, "r").readlines()[i + 2:i + n_connections + 2] # Determine number of sites to generate if connection_type == "bond": n_sites = 2 elif connection_type == "angle": n_sites = 3 else: n_sites = 4 for i, line in enumerate(connection_lines): site_list = list() for j in range(n_sites): site = topology.sites[int(line.split()[j + 2]) - 1] site_list.append(site) if connection_type == "bond": connection = Bond( connection_members=site_list, bond_type=connection_type_list[int(line.split()[1]) - 1], ) elif connection_type == "angle": connection = Angle( connection_members=site_list, angle_type=connection_type_list[int(line.split()[1]) - 1], ) topology.add_connection(connection) return topology
def test_angle_fake(self): atom1 = Atom(name='atom1') atom2 = Atom(name='atom2') atom3 = Atom(name='atom3') with pytest.raises(ValidationError): Angle(connection_members=['fakesite1', 'fakesite2', 4.2])
def test_angle_fake(self): atom1 = Atom(name="atom1") atom2 = Atom(name="atom2") atom3 = Atom(name="atom3") with pytest.raises(ValidationError): Angle(connection_members=["fakesite1", "fakesite2", 4.2])