Usage examples

Read a single frame (indexes.py)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from chemharp import Trajectory


def main():
    traj = Trajectory("filename.xyz")
    frame = traj.read_next_step()
    positions = frame.positions()

    indexes = []
    for i in range(len(frame)):
        # positions is a numpy ndarray
        if positions[i, 0] < 5:
            indexes.append(i)

    print("Atoms with x < 5: ")
    for i in indexes:
        print("  - {}".format(i))


if __name__ == "__main__":
    main()

Read Multiple frames (rmsd.py)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from chemharp import Trajectory
import math


def main():
    traj = Trajectory("filename.nc")

    distances = []
    # Accumulate the distances to the origin of the 10th atom throughtout the
    # trajectory
    while (not traj.done()):
        frame = traj.read()
        # Position of the 10th atom
        position = frame.positions()[9, :]
        distance = math.sqrt(position.dot(position))
        distances.append(distance)

    mean = sum(distances)/len(distances)
    rmsd = 0.0
    for dist in distances:
        rmsd += (mean - dist)*(mean - dist)

    rmsd /= len(distances)
    rmsd = math.sqrt(rmsd)

    print("Root-mean square displacement is: {}".format(rmsd))


if __name__ == "__main__":
    main()

Write frames (convert.py)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from chemharp import Trajectory, UnitCell, Atom, Topology


def main():
    input_file = Trajectory("water.xyz")
    water_topology = Topology()
    # Orthorombic UnitCell with lengths of 20, 15 and 35 A
    cell = UnitCell(20, 15, 35)

    # Create Atoms
    O = Atom("O")
    H = Atom("H")

    # Fill the topology with one water molecule
    water_topology.append(O)
    water_topology.append(H)
    water_topology.append(H)
    water_topology.add_bond(0, 1)
    water_topology.add_bond(0, 2)

    output = Trajectory("water.pdb", "w")

    while not input_file.done():
        frame = input_file.read()
        # Set the frame cell and topology
        frame.cell = cell
        frame.topology = water_topology
        # Write the frame to the output file, using PDB format
        output.write(frame)

if __name__ == "__main__":
    main()