# MDTrajectory¶

class MDTrajectory(path=None, group_name=<class 'NL.ComputerScienceUtilities.NLFlag._NLFlag.Automatic'>)

Constructor for the MDTrajectory that contains the configurations from a MolecularDynamics() run.

Parameters: path (str | None) – The path to the HDF5 file. If the path is None then all data will be stored in memory and no file will be written. Default: None group_name (str | Automatic) – The name of the HDF5 group to store the data in. If the value is Automatic then a new group will be automatically made. Default: Automatic
calculator()
Returns: The calculator if one is available, otherwise None. Calculator | None
cells(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The cell vectors of the images or None if the trajectory length is zero or the configuration is a MoleculeConfiguration. PhysicalQuantity of type length | None
coordinates(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The Cartesian coordinates of the images or None the trajectory length is zero. PhysicalQuantity of type length | None
elements()
Returns: The elements of the images as a list of length n or None if the trajectory length is zero. list of type PeriodicTableElement
forces(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The forces of the images. If the trajectory length is zero or the forces were not written during the MolecularDynamics run then None is returned. PhysicalQuantity of type energy/length | None
image(image_index, set_calculator=True)
Parameters: image_index (int) – The index of the configuration that should be extracted. set_calculator (bool) – If True, the calculator is set on the returned image. This can be disabled to increase the performance when iterating over images, because the overhead of copying and setting up a new calculator can be large. Default: True A copy of the configuration with the image_index. Note that no calculator is attached.
kineticEnergies(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The kinetic energies of the images or None the trajectory length is zero. PhysicalQuantity of type energy | None
lastImage()
Returns: The last image in the trajectory with the calculator attached. MoleculeConfiguration | BulkConfiguration | DeviceConfiguration | SurfaceConfiguration
lastStep()
Returns: The step number of the last step. int
lastTime()
Returns: The time associated with the last step. PhysicalQuantity of type time
length()
Returns: The number of images in the trajectory. int
logInterval()
masses()
Returns: The masses of the atoms with shape (num_atoms,) or None if the trajectory length is zero. PhysicalQuantity of type mass
metatext()
Returns: The metatext of the object or None if no metatext is set. str | unicode | None
nlprint(stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)

Write a text description of the trajectory to the stream.

Parameters: stream (file-like object) – The stream to write to. Default: sys.stdout
potentialEnergies(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The potential energies of the images or None the trajectory length is zero. PhysicalQuantity of type energy | None
pressureTensors(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The pressure tensors of the images as a vector with shape (m, 3, 3) or None if the trajectory length is zero. PhysicalQuantity of type pressure | None
pressures(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The pressure values of the images as a vector with shape (m) or None if the trajectory length is zero. PhysicalQuantity of type pressure | None
setMetatext(text)

Set a given metatext string on the object.

Parameters: metatext (str | unicode | None) – The metatext string that should be set. A value of “None” can be given to remove the current metatext.
steps(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The MD step numbers of the images or None the trajectory length is zero. array of type int | None
stresses(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The stresses of the images. If the trajectory length is zero or the stresses were not written during the MolecularDynamics run then None is returned. None is also returned if the configuration is a MoleculeConfiguration. PhysicalQuantity of type pressure | None
temperatures(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The instantaneous kinetic temperatures of the images. If the trajectory length is zero or the temperatures were not written during the MolecularDynamics run then None is returned. PhysicalQuantity of type temperature
timeInterval()
Returns: The time interval between two images. PhysicalQuantity of type time
timeStep()
Returns: The MD time step of the simulation. PhysicalQuantity of type time
times(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The simulation times of the images or None the trajectory length is zero. PhysicalQuantity of type time | None
uniqueElements()
Returns: The unique elements of the images or None if the trajectory length is zero. list of type PeriodicTableElement
velocities(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: All indices The velocities of the images. If the trajectory length is zero or the velocities were not written during the MolecularDynamics run then None is returned. PhysicalQuantity of type length/time
volumes(idx=Ellipsis)
Parameters: idx (int | slice) – The index or slice to return. Default: Ellipsis The volumes of the unit cells. If this was a MD simulation of molecules, then None will be returned. PhysicalQuantity of type length**3 | None
writeToXYZ(filename)

Make a xyz dump of the whole trajectory.

Parameters: filename (str) – The name of the file to write to.

## Usage Examples¶

Perform four Molecular Dynamics simulations with different time steps and save their trajectories in the file trajectory.nc.

# Define a benzene ring
cartesian_coordinates = [[ 0.0     ,  1.399795, 0.0],
[ 1.212253,  0.69976 , 0.0],
[ 1.212253, -0.69976 , 0.0],
[ 0.0     , -1.399795, 0.0],
[-1.212253, -0.69976 , 0.0],
[-1.212253,  0.69976 , 0.0],
[ 0.0     ,  2.500678, 0.0],
[ 2.165671,  1.250363, 0.0],
[ 2.165671, -1.250363, 0.0],
[ 0.0     , -2.500678, 0.0],
[-2.165671, -1.250363, 0.0],
[-2.165671,  1.250363, 0.0]]*Angstrom

molecule_configuration = MoleculeConfiguration(
elements=[Carbon,]*6+[Hydrogen,]*6,
cartesian_coordinates=cartesian_coordinates
)

# Attach a calculator
molecule_configuration.setCalculator(BrennerCalculator())

initial_velocity = MaxwellBoltzmannDistribution(
temperature=300.0*Kelvin
)

length_of_simulation = 20*femtoSecond

# Generate trajectories for different time steps
for time_step in [0.1, 0.5, 1, 2]*femtoSecond:

method = NVEVelocityVerlet(
time_step=time_step,
initial_velocity=initial_velocity
)

# Generate trajectory
molecular_dynamics = MolecularDynamics(
molecule_configuration,
trajectory_filename='trajectory.nc',
steps=int(length_of_simulation/time_step),
log_interval=max(1,int(2*femtoSecond/time_step)),
method=method
)


mdrun.py

Plot the total energy as function of time for the trajectories in the file trajectory.nc.

import pylab

# Read in a list with the trajectories

pylab.figure()
for trajectory in trajectories:
# Get the time axis of the trajectory
t = trajectory.times().inUnitsOf(femtoSecond)

# Calculate the time step
time_step = t[-1]/trajectory.steps()[-1]

# Get the energies
epot = trajectory.potentialEnergies().inUnitsOf(eV)
ekin = trajectory.kineticEnergies().inUnitsOf(eV)

# Plot the data
pylab.plot(t,epot+ekin, label='timestep=' + str(time_step)+' fs')
pylab.xlabel('time (fs)')
pylab.ylabel('total energy (eV)')
pylab.legend()

pylab.show()


mdplot.py

Fig. 133 The total energy as function of the simulation time in MD runs with different time steps.