def test_molecule_R(): a1 = Atom(H, 0, 0, 0) a2 = Atom(H, 1, 0, 0) m = Molecule([a1, a2]) expected = [[0, 1], [1, 0]] assert np.allclose(m.R, expected, atol=1e-10)
def test_molecule_from_xyz(): expected = Molecule([Atom(Li, 3.0, 4.0, 5.0), Atom(H, 2.3, 1.3, -1.0)]) xyz = """first line not needed second is comment and ignored 3 3.0 4.0 5.0 1 2.3 1.3 -1.0 """ assert Molecule.from_xyz(xyz) == expected
def test_molecule_coords(): a1 = Atom(H, -1, 0, 0) a2 = Atom(H, 1, 0, 0) m = Molecule([a1, a2]) expected = [[-1, 0, 0], [1, 0, 0]] assert np.allclose(m.coords, expected, atol=1e-10)
def test_extended_huckel_hamiltonian_if_s_orthogonal(): S = np.eye(2) m = Molecule([Atom(C, 0, 0, 0), Atom(N, 0, 0, 0)]) h = ExtendedHuckelHamiltonian(S=S, molecule=m).matrix() assert is_diagonal(h) assert h[0, 0] == -C.voie assert h[1, 1] == -N.voie
def test_molecule_translated(): a1 = Atom(H, 20, 0, 0) a2 = Atom(H, 10, 0, 0) m = Molecule([a1, a2]) e1 = Atom(H, 20, 10, -3) e2 = Atom(H, 10, 10, -3) expected = Molecule([e1, e2]) assert m.translated(y=10, z=-3) == expected
def test_extended_huckel_hamiltonian_s_ones(): S = np.ones((2, 2)) m = Molecule([Atom(C, 0, 0, 0), Atom(N, 0, 0, 0)]) h = ExtendedHuckelHamiltonian(S=S, molecule=m).matrix() assert h[0, 0] == -C.voie assert h[1, 1] == -N.voie expected = 1.75 * (-C.voie + -N.voie) / 2.0 assert isclose(h[1, 0], expected) assert isclose(h[0, 1], expected)
def test_rotate(): atom = Atom(H, 0, 2, 0) r = atom.rotation_matrix_to(x=4, y=0, z=0) rotated = atom.rotated(r) expected = Atom(H, 2, 0, 0) assert rotated == expected m = Molecule([atom]) assert m.with_atom_aligned_to(atom, x=1.0, y=0.0, z=0.0) == Molecule([expected]) assert m.rotated_about_z(np.pi / 2) == Molecule(atoms=[Atom(H, -2.0, 0.0, 0.0)])
def test_molecule_mass(): a1 = Atom(3, 0, 0, 0) a2 = Atom(25, 1, 0, 0) m = Molecule([a1, a2]) assert m.mass == 28
def test_molecule_sorted(): m = Molecule([Atom(Li, 1, 1, 1), Atom(H, -1, 10, -30)]) expected = Molecule([Atom(H, -1, 10, -30), Atom(Li, 1, 1, 1)]) assert m.sorted(lambda a: a.z) == expected assert m.sorted(lambda a: a.mass) == expected
def test_atom_rotated(): a = Atom(H, 0.0, 2.0, 0.0) r = a.rotation_matrix_to(1, 0, 0) assert a.rotated(r) == Atom(H, 2.0, 0.0, 0.0)
def test_atom_scaled(): a = Atom(H, 1, 2, 4) assert a.scaled(2.0) == Atom(H, 2.0, 4.0, 8.0)
def test_center_of_mass(): a1 = Atom(H, -1, 0, 0) a2 = Atom(H, 1, 0, 0) m = Molecule([a1, a2]) assert allclose(m.center_of_mass, (0, 0, 0))
def test_molecule___len__(): a1 = Atom(H, -1, 0, 0) a2 = Atom(H, 1, 0, 0) m = Molecule([a1, a2]) assert len(m) == 2
def test_molecule_rotated_about_y(): a1 = Atom(H, 1, 2, 0) a2 = Atom(H, 0, 6, -1) m = Molecule([a1, a2]) expected = Molecule([Atom(H, 0, 2, 1), Atom(H, 1, 6, 0)]) assert m.rotated_about_y(np.pi / 2) == expected
def test_molecule_rotated_about_z(): a1 = Atom(H, -1, 0, 100) a2 = Atom(H, 0, -1, 200) m = Molecule([a1, a2]) expected = Molecule([Atom(H, 0, -1, 100), Atom(H, 1, 0, 200)]) assert m.rotated_about_z(np.pi / 2) == expected
def test_molecule_rotated_raises_if_wrong_shape(): with raises(ValueError): Molecule([Atom(H, 1, 1, 1)]).rotated(np.array([1]))
def test_atom_translated(): a = Atom(H, 1, 2, 3) assert a.translated(x=2.0, y=3.0, z=4.0) == Atom(H, 3.0, 5.0, 7.0)
def test_atom__init__(): h1 = Atom("H", 0, 0, 0) h2 = Atom(H, 0, 0, 0) assert h1 == h2
def test_molecule_flipped_x(): m = Molecule([Atom(Li, 1, 1, 1), Atom(H, -1, 10, -30)]) expected = Molecule([Atom(Li, -1, 1, 1), Atom(H, 1, 10, -30)]) assert m.flipped_x() == expected
def with_atom_aligned_to(self, atom: Atom, x: float, y: float, z: float) -> "Molecule": r = atom.rotation_matrix_to(x=x, y=y, z=z) return self.rotated(r)
def from_xyz(xyz: str) -> "Molecule": """Create a molecule from an xyz-file formatted string""" text = (line.split() for line in xyz.splitlines()[2:] if line.strip()) atoms = [Atom(element=e, x=x, y=y, z=z) for e, x, y, z in text] return Molecule(atoms)
def test_extended_huckel_hamiltonian_raises_if_missing_voie(): m = Molecule([Atom(Uuo, 0, 0, 0)]) S = np.eye(1) with raises(ValueError): ExtendedHuckelHamiltonian(S, m)
def test_molecule_com_as_origin(): m = Molecule([Atom(Li, 23, 1, 2), Atom(H, 24, 8, -23)]) translated = m.com_as_origin() assert allclose(translated.center_of_mass, (0, 0, 0))
def test_molecule_rotated_about_x(): a1 = Atom(H, 5, 1, 0) a2 = Atom(H, 2, 0, 1) m = Molecule([a1, a2]) expected = Molecule([Atom(H, 5, 0, -1), Atom(H, 2, 1, 0)]) assert m.rotated_about_x(-np.pi / 2) == expected
def test_molecule_scaled(): m = Molecule([Atom(Li, 1, 1, 1), Atom(H, -1, 10, -30)]) expected = Molecule([Atom(Li, 10, 10, 10), Atom(H, -10, 100, -300)]) assert m.scaled(10) == expected
def test_atom_mass(): assert Atom(H, 0, 0, 0).mass == 1 for i in range(1, 100): assert Atom(i, 0, 0, 0).mass == i