Page 1 of 1

trajectory with velocities?

Posted: Mon Mar 23, 2020 7:32 am
by mkrompiec
Hello, I would like to save a trajectory with both positions and velocities of all particles. Ideally, in XYZ or GRO format (or something that can be converted to any of these). Is it possible or do I have to write my own reporter? So far, I tried the MdcrdReporter from Parmed, but its format is not what I need (unless there is a way to convert it to xyz or gro?).
Thanks in advance,
Michal

Re: trajectory with velocities?

Posted: Mon Mar 23, 2020 9:11 am
by peastman
The NetCDF reporter in ParmEd can also save velocities. And MDTraj has an HDF5 reporter that can save velocities: http://mdtraj.org/development/api/gener ... F5Reporter.

Re: trajectory with velocities?

Posted: Mon Mar 23, 2020 3:17 pm
by mkrompiec
Thank you!
It turned out to be relatively simple. Just in case anyone else needs a xyz trajectory with units of their choice, this is how I eventually did it:

Code: Select all

class XyzReporter(object):
    def __init__(self, file, reportInterval):
        self._reportInterval = reportInterval
        self._out = None
        self.fname = file

    def describeNextReport(self, simulation):
        stepsleft = simulation.currentStep % self._reportInterval
        steps = self._reportInterval - stepsleft
        return (steps, True, True, False, False)

    def report(self, simulation, state):
        crds = state.getPositions().value_in_unit(unit.angstrom)
        vels = state.getVelocities().value_in_unit(unit.angstrom / unit.femtosecond)
        if self._out is None:
            self.atom = len(crds)
            self._out = open(self.fname, 'w')

        self._out.write(f"{self.atom}\nOpenMM Trajectory\n")
        # Add the coordinates, velocities, and/or forces as needed
        atoms=simulation.topology.atoms()
        for atom, crd, vel in zip(atoms, crds, vels):
            self._out.write(f"{atom.name} {crd[0]} {crd[1]} {crd[2]} {vel[0]} {vel[1]} {vel[2]}\n")

Best,
Michal