def test_bonds(self): frame = Frame() frame.add_atom(Atom(""), (0, 0, 0)) frame.add_atom(Atom(""), (0, 0, 0)) frame.add_atom(Atom(""), (0, 0, 0)) frame.add_atom(Atom(""), (0, 0, 0)) frame.add_atom(Atom(""), (0, 0, 0)) frame.add_bond(0, 1) frame.add_bond(3, 4) frame.add_bond(2, 1, BondOrder.Qintuplet) self.assertEqual(frame.topology.bonds.all(), np.array([[0, 1], [1, 2], [3, 4]]).all()) self.assertEqual( frame.topology.bonds_orders, [BondOrder.Unknown, BondOrder.Qintuplet, BondOrder.Unknown]) frame.remove_bond(3, 4) # Also try to remove non-existing bonds frame.remove_bond(3, 4) frame.remove_bond(0, 4) self.assertEqual(frame.topology.bonds.all(), np.array([[0, 1], [1, 2]]).all())
def test_distance(self): frame = Frame() frame.cell = UnitCell(3.0, 4.0, 5.0) frame.add_atom(Atom(""), (0, 0, 0)) frame.add_atom(Atom(""), (1, 2, 6)) self.assertEqual(frame.distance(0, 1), math.sqrt(6.0))
def test_name(self): atom = Atom("He") self.assertEqual(atom.name, "He") self.assertEqual(atom.full_name, "Helium") atom.name = "Zn" self.assertEqual(atom.name, "Zn")
def test_angle(self): frame = Frame() frame.add_atom(Atom(""), (1, 0, 0)) frame.add_atom(Atom(""), (0, 0, 0)) frame.add_atom(Atom(""), (0, 1, 0)) self.assertEqual(frame.angle(0, 1, 2), math.pi / 2.0)
def test_property(self): atom = Atom("He") atom["foo"] = 3 self.assertEqual(atom["foo"], 3.0) atom["foo"] = False self.assertEqual(atom["foo"], False) with remove_warnings: with self.assertRaises(ChemfilesError): _ = atom["bar"] with self.assertRaises(ChemfilesError): atom[3] = "test" with self.assertRaises(ChemfilesError): _ = atom[3] # Check that enabling indexing/__getitem__ did not enable iteration with self.assertRaises(TypeError): for i in atom: pass atom["bar"] = "baz" self.assertEqual(atom.properties_count(), 2) self.assertEqual(set(atom.list_properties()), {"bar", "foo"})
def test_out_of_plane(self): frame = Frame() frame.add_atom(Atom(""), (1, 0, 0)) frame.add_atom(Atom(""), (0, 0, 0)) frame.add_atom(Atom(""), (0, 1, 0)) frame.add_atom(Atom(""), (0, 0, 3)) self.assertEqual(frame.out_of_plane(1, 3, 0, 2), 3.0)
def test_dihedral(self): frame = Frame() frame.add_atom(Atom(""), (1, 0, 0)) frame.add_atom(Atom(""), (0, 0, 0)) frame.add_atom(Atom(""), (0, 1, 0)) frame.add_atom(Atom(""), (-1, 1, 0)) self.assertEqual(frame.dihedral(0, 1, 2, 3), math.pi)
def test_copy(self): atom = Atom("He") cloned = copy.copy(atom) self.assertEqual(atom.name, "He") self.assertEqual(cloned.name, "He") atom.name = "Zn" self.assertEqual(atom.name, "Zn") self.assertEqual(cloned.name, "He")
def test_add_atom(self): frame = Frame() frame.add_atom(Atom("F"), (3, 4, 5)) self.assertEqual(len(frame.atoms), 1) self.assertEqual(list(frame.positions[0]), [3, 4, 5]) frame.add_velocities() frame.add_atom(Atom("F"), (-3, -4, 5), (1, 0, 1)) self.assertEqual(list(frame.positions[1]), [-3, -4, 5]) self.assertEqual(list(frame.velocities[1]), [1, 0, 1])
def test_topology(self): frame = Frame() frame.resize(2) topology = Topology() topology.atoms.append(Atom("Zn")) topology.atoms.append(Atom("Ar")) frame.topology = topology self.assertEqual(frame.atoms[0].name, "Zn") self.assertEqual(frame.atoms[1].name, "Ar")
def test_type(self): atom = Atom("He") self.assertEqual(atom.type, "He") self.assertEqual(atom.full_name, "Helium") atom.type = "Zn" self.assertEqual(atom.type, "Zn") self.assertEqual(atom.full_name, "Zinc") atom = Atom("He2", "H") self.assertEqual(atom.name, "He2") self.assertEqual(atom.type, "H")
def testing_frame(): frame = Frame() frame.add_atom(Atom("H"), [0, 0, 0]) frame.add_atom(Atom("O"), [0, 0, 0]) frame.add_atom(Atom("O"), [0, 0, 0]) frame.add_atom(Atom("H"), [0, 0, 0]) frame.add_bond(0, 1) frame.add_bond(1, 2) frame.add_bond(2, 3) return frame
def test_size(self): topology = Topology() self.assertEqual(len(topology.atoms), 0) topology.atoms.append(Atom("H")) topology.atoms.append(Atom("O")) topology.atoms.append(Atom("O")) topology.atoms.append(Atom("H")) self.assertEqual(len(topology.atoms), 4) topology.resize(8) self.assertEqual(len(topology.atoms), 8) topology.atoms.remove(3) self.assertEqual(len(topology.atoms), 7) del topology.atoms[4] self.assertEqual(len(topology.atoms), 6)
def test_indexes(self): # Create an input file frame = Frame() for i in range(120): frame.add_atom(Atom('X'), [i % 10, i + 1 % 10, i + 2 % 10]) with Trajectory("filename.xyz", "w") as file: file.write(frame) path = os.path.join(ROOT, "..", "examples", "indexes.py") # disable output exec(open(path).read(), globals(), {'print': lambda _: None})
def test_select(self): # Create an input file frame = Frame() NAMES = ["N", "Zn", "C", "O"] for i in range(120): frame.add_atom(Atom(NAMES[i % 4]), [i % 10, i + 1 % 10, i + 2 % 10]) with Trajectory("input.arc", "w") as file: file.write(frame) path = os.path.join(ROOT, "..", "examples", "select.py") exec(open(path).read(), globals())
def test_read(self): trajectory = Trajectory(get_data_path("water.xyz")) self.assertEqual(trajectory.nsteps, 100) self.assertEqual(trajectory.path, get_data_path("water.xyz")) frame = trajectory.read() self.assertEqual(len(frame.atoms), 297) self.assertEqual(frame.positions[0].all(), np.array([0.417219, 8.303366, 11.737172]).all()) self.assertEqual(frame.positions[124].all(), np.array([5.099554, -0.045104, 14.153846]).all()) self.assertEqual(len(frame.atoms), 297) self.assertEqual(frame.atoms[0].name, "O") self.assertEqual(frame.atoms[1].name, "H") trajectory.set_cell(UnitCell(30, 30, 30)) frame = trajectory.read_step(41) self.assertEqual(frame.cell.lengths, (30.0, 30.0, 30.0)) self.assertEqual(frame.positions[0].all(), np.array([0.761277, 8.106125, 10.622949]).all()) self.assertEqual(frame.positions[124].all(), np.array([5.13242, 0.079862, 14.194161]).all()) self.assertEqual(len(frame.atoms), 297) self.assertEqual(frame.topology.bonds_count(), 0) frame.guess_bonds() self.assertEqual(frame.topology.bonds_count(), 181) self.assertEqual(frame.topology.angles_count(), 87) topology = Topology() for i in range(297): topology.atoms.append(Atom("Cs")) trajectory.set_topology(topology) frame = trajectory.read_step(10) self.assertEqual(frame.atoms[10].name, "Cs") trajectory.set_topology(get_data_path("topology.xyz"), "XYZ") frame = trajectory.read() self.assertEqual(frame.atoms[100].name, "Rd")
def test_write(self): frame = Frame() for i in range(4): frame.add_atom(Atom("X"), [1, 2, 3]) with Trajectory("test-tmp.xyz", "w") as fd: fd.write(frame) expected_content = """4 Written by the chemfiles library X 1 2 3 X 1 2 3 X 1 2 3 X 1 2 3 """ with open("test-tmp.xyz") as fd: self.assertEqual(fd.read(), expected_content) os.unlink("test-tmp.xyz")
def test_mass(self): atom = Atom("He") self.assertAlmostEqual(atom.mass, 4.002602, 6) atom.mass = 1.0 self.assertEqual(atom.mass, 1.0)
def test_very_long_name(self): atom = Atom("He" * 128) self.assertEqual(atom.name, "He" * 128)
def test_repr(self): self.assertEqual(Atom("He").__repr__(), "Atom('He')") self.assertEqual(Atom("He-3", "He").__repr__(), "Atom('He-3', 'He')")
def test_charge(self): atom = Atom("He") self.assertEqual(atom.charge, 0.0) atom.charge = -1.5 self.assertEqual(atom.charge, -1.5)
if state < 7: x1, y1, x2, y2, x3, y3, z3 = coordinate_si( ring, column, state) if state > 6 and ring % 4 == 1 and column % 2 == 0: #up (vert) x1, y1, x2, y2, x3, y3, z3 = coordinate_up( ring, column, state) if state > 6 and ring % 4 == 1 and column % 2 == 1: #down (rouge) x1, y1, x2, y2, x3, y3, z3 = coordinate_down( ring, column, state) if state > 6 and ring % 4 == 3 and column % 2 == 1: #up (vert) x1, y1, x2, y2, x3, y3, z3 = coordinate_up( ring, column, state) if state > 6 and ring % 4 == 3 and column % 2 == 0: #down (rouge) x1, y1, x2, y2, x3, y3, z3 = coordinate_down( ring, column, state) if state < 7 and state > 0: atom1 = Atom("Si", "Si") atom2 = Atom("O", "O") atom3 = Atom("H", "H") frame.add_atom(atom1, [x1, y1, 0]) frame.add_atom(atom2, [x2, y2, height]) frame.add_atom(atom3, [x3, y3, height]) if state > 7: atom1 = Atom("Ow", "O") atom2 = Atom("Hw", "H") atom3 = Atom("Hw", "H") frame.add_atom(atom1, [x1, y1, height]) frame.add_atom(atom2, [x2, y2, height]) frame.add_atom(atom3, [x3, y3, z3]) ring += 1
# This file is an example for the chemfiles library # Any copyright is dedicated to the Public Domain. # http://creativecommons.org/publicdomain/zero/1.0/ #!/usr/bin/env python import numpy as np from chemfiles import Topology, Frame, Atom, UnitCell, Trajectory topology = Topology() topology.atoms.append(Atom("H")) topology.atoms.append(Atom("O")) topology.atoms.append(Atom("H")) topology.add_bond(0, 1) topology.add_bond(2, 1) frame = Frame() frame.resize(3) frame.topology = topology frame.positions[0, :] = np.array([1.0, 0.0, 0.0]) frame.positions[1, :] = np.array([0.0, 0.0, 0.0]) frame.positions[2, :] = np.array([0.0, 1.0, 0.0]) frame.add_atom(Atom("O"), [5.0, 0.0, 0.0]) frame.add_atom(Atom("C"), [6.0, 0.0, 0.0]) frame.add_atom(Atom("O"), [7.0, 0.0, 0.0]) frame.add_bond(3, 4) frame.add_bond(4, 5) frame.cell = UnitCell(10, 10, 10)
def test_radii(self): self.assertAlmostEqual(Atom("He").vdw_radius, 1.4, 2) self.assertAlmostEqual(Atom("He").covalent_radius, 0.32, 3) self.assertEqual(Atom("H1").vdw_radius, 0) self.assertEqual(Atom("H1").covalent_radius, 0)
def test_atomic_number(self): self.assertEqual(Atom("He").atomic_number, 2) self.assertEqual(Atom("H1").atomic_number, 0)