Exemple #1
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))
Exemple #2
0
    def test_iter(self):
        frame = Frame()
        frame.resize(3)

        for i, atom in enumerate(frame.atoms):
            self.assertEqual(atom.name, "")
        self.assertEqual(i, 2)
Exemple #3
0
    def test_property(self):
        frame = Frame()

        frame["foo"] = 3
        self.assertEqual(frame["foo"], 3.0)

        frame["foo"] = False
        self.assertEqual(frame["foo"], False)

        with remove_warnings:
            with self.assertRaises(ChemfilesError):
                _ = frame["bar"]

            with self.assertRaises(ChemfilesError):
                frame[3] = "test"

            with self.assertRaises(ChemfilesError):
                _ = frame[3]

        # Check that enabling indexing/__getitem__ did not enable iteration
        with self.assertRaises(TypeError):
            for i in frame:
                pass

        frame["bar"] = "baz"

        self.assertEqual(frame.properties_count(), 2)
        self.assertEqual(set(frame.list_properties()), {"bar", "foo"})
Exemple #4
0
    def test_repr(self):
        frame = Frame()
        self.assertEqual(frame.__repr__(), "Frame with 0 atoms")
        frame.resize(4)
        self.assertEqual(frame.__repr__(), "Frame with 4 atoms")

        self.assertEqual(frame.atoms.__repr__(), "[Atom(''), Atom(''), Atom(''), Atom('')]")
Exemple #5
0
    def test_property(self):
        frame = Frame()

        frame["foo"] = 3
        self.assertEqual(frame["foo"], 3.0)

        frame["foo"] = False
        self.assertEqual(frame["foo"], False)

        with remove_warnings:
            with self.assertRaises(ChemfilesError):
                _ = frame["bar"]

            with self.assertRaises(ChemfilesError):
                frame[3] = "test"

            with self.assertRaises(ChemfilesError):
                _ = frame[3]

        # Check that enabling indexing/__getitem__ did not enable iteration
        with self.assertRaises(TypeError):
            for i in frame:
                pass

        frame["bar"] = "baz"

        self.assertEqual(frame.properties_count(), 2)
        self.assertEqual(set(frame.list_properties()), {"bar", "foo"})
Exemple #6
0
    def test_residues(self):
        frame = Frame()
        frame.add_residue(Residue("Foo"))
        frame.add_residue(Residue("Foo"))
        frame.add_residue(Residue("Foo"))

        self.assertEqual(len(frame.topology.residues), 3)
        self.assertEqual(frame.topology.residues[0].name, "Foo")
Exemple #7
0
    def test_cell(self):
        frame = Frame()
        frame.cell = UnitCell(1, 2, 4)
        self.assertEqual(frame.cell.lengths, (1, 2, 4))
        self.assertEqual(frame.cell.angles, (90, 90, 90))
        self.assertEqual(frame.cell.shape, CellShape.Orthorhombic)

        frame.cell.lengths = (3, 4, 5)
        self.assertEqual(frame.cell.lengths, (3, 4, 5))
Exemple #8
0
    def test_atoms_count(self):
        frame = Frame()
        self.assertEqual(len(frame.atoms), 0)

        frame.resize(4)
        self.assertEqual(len(frame.atoms), 4)

        frame.remove(2)
        self.assertEqual(len(frame.atoms), 3)
Exemple #9
0
    def test_cell(self):
        frame = Frame()
        frame.cell = UnitCell(1, 2, 4)
        self.assertEqual(frame.cell.lengths, (1, 2, 4))
        self.assertEqual(frame.cell.angles, (90, 90, 90))
        self.assertEqual(frame.cell.shape, CellShape.Orthorhombic)

        frame.cell.lengths = (3, 4, 5)
        self.assertEqual(frame.cell.lengths, (3, 4, 5))
Exemple #10
0
    def test_copy(self):
        frame = Frame()
        cloned = copy.copy(frame)

        self.assertEqual(len(frame.atoms), 0)
        self.assertEqual(len(cloned.atoms), 0)

        frame.resize(6)
        self.assertEqual(len(frame.atoms), 6)
        self.assertEqual(len(cloned.atoms), 0)
Exemple #11
0
    def test_copy(self):
        frame = Frame()
        cloned = copy.copy(frame)

        self.assertEqual(len(frame.atoms), 0)
        self.assertEqual(len(cloned.atoms), 0)

        frame.resize(6)
        self.assertEqual(len(frame.atoms), 6)
        self.assertEqual(len(cloned.atoms), 0)
Exemple #12
0
    def test_iter(self):
        frame = Frame()
        frame.resize(3)

        for i, atom in enumerate(frame.atoms):
            self.assertEqual(atom.name, "")
        self.assertEqual(i, 2)

        for i, atom in enumerate(frame.topology.atoms):
            self.assertEqual(atom.name, "")
        self.assertEqual(i, 2)
Exemple #13
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")
Exemple #14
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)
Exemple #15
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})
Exemple #16
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())
Exemple #17
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
Exemple #18
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.topology.atoms[0].name, "Zn")
        self.assertEqual(frame.topology.atoms[1].name, "Ar")

        self.assertEqual(frame.atoms[0].name, "Zn")
        self.assertEqual(frame.atoms[1].name, "Ar")
Exemple #19
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)
Exemple #20
0
    def test_positions(self):
        frame = Frame()
        frame.resize(4)

        expected = np.array(
            [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]],
            np.float64,
        )
        np.copyto(frame.positions, expected)
        self.assertEqual(frame.positions.all(), expected.all())

        frame.positions[3, 2] = 42
        self.assertEqual(frame.positions[3, 2], 42)

        # Checking empty frame positions access
        _ = Frame().positions
Exemple #21
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])
Exemple #22
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)
Exemple #23
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)
Exemple #24
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")
Exemple #25
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")
Exemple #26
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])
Exemple #27
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))
Exemple #28
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)
Exemple #29
0
    def test_repr(self):
        frame = Frame()
        self.assertEqual(frame.__repr__(), "Frame with 0 atoms")
        frame.resize(4)
        self.assertEqual(frame.__repr__(), "Frame with 4 atoms")

        self.assertEqual(frame.atoms.__repr__(),
                         "[Atom(''), Atom(''), Atom(''), Atom('')]")
Exemple #30
0
    def test_residues(self):
        frame = Frame()
        frame.add_residue(Residue("Foo"))
        frame.add_residue(Residue("Foo"))
        frame.add_residue(Residue("Foo"))

        self.assertEqual(len(frame.topology.residues), 3)
        self.assertEqual(frame.topology.residues[0].name, "Foo")
Exemple #31
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)
Exemple #32
0
    def test_atoms_count(self):
        frame = Frame()
        self.assertEqual(len(frame.atoms), 0)

        frame.resize(4)
        self.assertEqual(len(frame.atoms), 4)

        frame.remove(2)
        self.assertEqual(len(frame.atoms), 3)
Exemple #33
0
    def test_positions(self):
        frame = Frame()
        frame.resize(4)

        expected = np.array(
            [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0],
             [10.0, 11.0, 12.0]],
            np.float64,
        )
        np.copyto(frame.positions, expected)
        self.assertEqual(frame.positions.all(), expected.all())

        frame.positions[3, 2] = 42
        self.assertEqual(frame.positions[3, 2], 42)

        # Checking empty frame positions access
        _ = Frame().positions
Exemple #34
0
 def test_out_of_bounds(self):
     frame = Frame()
     frame.resize(3)
     _ = frame.atoms[2]
     with self.assertRaises(IndexError):
         _ = frame.atoms[6]
Exemple #35
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())
Exemple #36
0
 def test_step(self):
     frame = Frame()
     self.assertEqual(frame.step, 0)
     frame.step = 42
     self.assertEqual(frame.step, 42)
Exemple #37
0
    def test_velocities(self):
        frame = Frame()
        frame.resize(4)

        self.assertFalse(frame.has_velocities())
        frame.add_velocities()
        self.assertTrue(frame.has_velocities())

        expected = np.array(
            [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0],
             [10.0, 11.0, 12.0]],
            np.float64,
        )
        np.copyto(frame.velocities, expected)
        self.assertEqual(frame.velocities.all(), expected.all())

        frame.velocities[3, 2] = 42
        self.assertEqual(frame.velocities[3, 2], 42)

        # Checking empty frame velocities access
        frame = Frame()
        frame.add_velocities()
        _ = frame.velocities
Exemple #38
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)
Exemple #39
0
 def test_out_of_bounds(self):
     frame = Frame()
     frame.resize(3)
     _ = frame.atoms[2]
     with self.assertRaises(IndexError):
         _ = frame.atoms[6]
Exemple #40
0
    def test_velocities(self):
        frame = Frame()
        frame.resize(4)

        self.assertFalse(frame.has_velocities())
        frame.add_velocities()
        self.assertTrue(frame.has_velocities())

        expected = np.array(
            [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]],
            np.float64,
        )
        np.copyto(frame.velocities, expected)
        self.assertEqual(frame.velocities.all(), expected.all())

        frame.velocities[3, 2] = 42
        self.assertEqual(frame.velocities[3, 2], 42)

        # Checking empty frame velocities access
        frame = Frame()
        frame.add_velocities()
        _ = frame.velocities
Exemple #41
0
 def test_step(self):
     frame = Frame()
     self.assertEqual(frame.step, 0)
     frame.step = 42
     self.assertEqual(frame.step, 42)
Exemple #42
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()
        )
Exemple #43
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.add_atom(Atom("H"))
topology.add_atom(Atom("O"))
topology.add_atom(Atom("H"))

topology.add_bond(0, 1)
topology.add_bond(2, 1)

frame = Frame()
frame.resize(3)
frame.set_topology(topology)

positions = frame.positions()
positions[0, :] = np.array([1.0, 0.0, 0.0])
positions[1, :] = np.array([0.0, 0.0, 0.0])
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.set_cell(UnitCell(10, 10, 10))
Exemple #44
0
        return x, y, x - 0.866, y + 0.5, x, y, height + 1
    if state == 11:
        return x, y, x, y - 1, x + 0.866, y + 0.5, height
    if state == 12:
        return x, y, x - 0.866, y + 0.5, x + 0.866, y + 0.5, height
    if state == 13:
        return x, y, x - 0.866, y + 0.5, x, y - 1, height


with Trajectory('conf.xyz', 'w') as output:
    with open('conf.dat', 'r') as conf:
        ring = 0
        skip = 0
        for line in conf:
            if line.startswith("Step:") and skip % 100 == 0:
                frame = Frame()
                ring = 0
            elif line.startswith("####"):
                if skip % 100 == 0:
                    output.write(frame)
                skip += 1
            elif skip % 100 == 0:
                row = map(int, line.split())
                for column, state in enumerate(row):
                    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)