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.
Return type:Calculator | None
cells(idx=Ellipsis)
Parameters:idx (int | slice) – The index or slice to return.
Default: All indices
Returns:The cell vectors of the images or None if the trajectory length is zero or the configuration is a MoleculeConfiguration.
Return type:PhysicalQuantity of type length | None
coordinates(idx=Ellipsis)
Parameters:idx (int | slice) – The index or slice to return.
Default: All indices
Returns:The Cartesian coordinates of the images or None the trajectory length is zero.
Return type: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.
Return type:list of type PeriodicTableElement
forces(idx=Ellipsis)
Parameters:idx (int | slice) – The index or slice to return.
Default: All indices
Returns: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.
Return type: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
Returns:

A copy of the configuration with the image_index. Note that no calculator is attached.

Return type:

MoleculeConfiguration | BulkConfiguration | DeviceConfiguration | SurfaceConfiguration

kineticEnergies(idx=Ellipsis)
Parameters:idx (int | slice) – The index or slice to return.
Default: All indices
Returns:The kinetic energies of the images or None the trajectory length is zero.
Return type:PhysicalQuantity of type energy | None
lastImage()
Returns:The last image in the trajectory with the calculator attached.
Return type:MoleculeConfiguration | BulkConfiguration | DeviceConfiguration | SurfaceConfiguration
lastStep()
Returns:The step number of the last step.
Return type:int
lastTime()
Returns:The time associated with the last step.
Return type:PhysicalQuantity of type time
length()
Returns:The number of images in the trajectory.
Return type:int
logInterval()
Returns:The log interval.
Return type:int
masses()
Returns:The masses of the atoms with shape (num_atoms,) or None if the trajectory length is zero.
Return type:PhysicalQuantity of type mass
metatext()
Returns:The metatext of the object or None if no metatext is set.
Return type: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
Returns:The potential energies of the images or None the trajectory length is zero.
Return type:PhysicalQuantity of type energy | None
pressureTensors(idx=Ellipsis)
Parameters:idx (int | slice) – The index or slice to return.
Default: All indices
Returns:The pressure tensors of the images as a vector with shape (m, 3, 3) or None if the trajectory length is zero.
Return type:PhysicalQuantity of type pressure | None
pressures(idx=Ellipsis)
Parameters:idx (int | slice) – The index or slice to return.
Default: All indices
Returns:The pressure values of the images as a vector with shape (m) or None if the trajectory length is zero.
Return type: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
Returns:The MD step numbers of the images or None the trajectory length is zero.
Return type:array of type int | None
stresses(idx=Ellipsis)
Parameters:idx (int | slice) – The index or slice to return.
Default: All indices
Returns: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.
Return type:PhysicalQuantity of type pressure | None
temperatures(idx=Ellipsis)
Parameters:idx (int | slice) – The index or slice to return.
Default: All indices
Returns: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.
Return type:PhysicalQuantity of type temperature
timeInterval()
Returns:The time interval between two images.
Return type:PhysicalQuantity of type time
timeStep()
Returns:The MD time step of the simulation.
Return type:PhysicalQuantity of type time
times(idx=Ellipsis)
Parameters:idx (int | slice) – The index or slice to return.
Default: All indices
Returns:The simulation times of the images or None the trajectory length is zero.
Return type:PhysicalQuantity of type time | None
uniqueElements()
Returns:The unique elements of the images or None if the trajectory length is zero.
Return type:list of type PeriodicTableElement
velocities(idx=Ellipsis)
Parameters:idx (int | slice) – The index or slice to return.
Default: All indices
Returns: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.
Return type:PhysicalQuantity of type length/time
volumes(idx=Ellipsis)
Parameters:idx (int | slice) – The index or slice to return.
Default: Ellipsis
Returns:The volumes of the unit cells. If this was a MD simulation of molecules, then None will be returned.
Return type: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
trajectories = nlread('trajectory.nc', MDTrajectory)

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

../../../_images/mdenergy.png

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