Ejemplo n.º 1
0
    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())
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
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")
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
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"})
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
    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")
Ejemplo n.º 9
0
    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])
Ejemplo n.º 10
0
    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")
Ejemplo n.º 11
0
    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")
Ejemplo n.º 12
0
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
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
    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})
Ejemplo n.º 15
0
    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())
Ejemplo n.º 16
0
    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")
Ejemplo n.º 17
0
    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")
Ejemplo n.º 18
0
 def test_mass(self):
     atom = Atom("He")
     self.assertAlmostEqual(atom.mass, 4.002602, 6)
     atom.mass = 1.0
     self.assertEqual(atom.mass, 1.0)
Ejemplo n.º 19
0
 def test_very_long_name(self):
     atom = Atom("He" * 128)
     self.assertEqual(atom.name, "He" * 128)
Ejemplo n.º 20
0
 def test_repr(self):
     self.assertEqual(Atom("He").__repr__(), "Atom('He')")
     self.assertEqual(Atom("He-3", "He").__repr__(), "Atom('He-3', 'He')")
Ejemplo n.º 21
0
 def test_charge(self):
     atom = Atom("He")
     self.assertEqual(atom.charge, 0.0)
     atom.charge = -1.5
     self.assertEqual(atom.charge, -1.5)
Ejemplo n.º 22
0
                    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
Ejemplo n.º 23
0
# 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)
Ejemplo n.º 24
0
    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)
Ejemplo n.º 25
0
 def test_atomic_number(self):
     self.assertEqual(Atom("He").atomic_number, 2)
     self.assertEqual(Atom("H1").atomic_number, 0)