narupa.ase.trajectory_logger module

Module containing a trajectory logging class that can be used to output portable trajectory files from an ASE molecular dynamics simulation.

class narupa.ase.trajectory_logger.TrajectoryLogger(atoms: ase.atoms.Atoms, filename: str, format: Optional[str] = None, timestamp=True, parallel=True, **kwargs)

Bases: object

Trajectory logging class for use with ASE simulations.

Can be attached to an ASE simulation, resulting in frames being written automatically:

>>> from ase.calculators.emt import EMT
>>> from ase.lattice.cubic import FaceCenteredCubic
>>> atoms = FaceCenteredCubic(directions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]], symbol="Cu", size=(2, 2, 2), pbc=True)
>>> atoms.calc = EMT()
>>> dynamics = Langevin(atoms, timestep=0.5, temperature_K=300, friction=1.0)
>>> with TrajectoryLogger(atoms, 'example.xyz') as logger:
...     dynamics.attach(TrajectoryLogger(atoms, 'example.xyz'), interval=10) # attach an XYZ logger.
Parameters:
  • atoms – ASE Atoms from which to write data.
  • filename – Path to filename to write to.
  • format – Format to use, as supported by ASE. If not specified, derived from filename.
  • timestamp – Whether to append a timestamp to the file name. Use to avoid overwriting the same file if

dynamics is reset. :param parallel: Default is to write on master process only. Set to False to write from all processes. :param kwargs: Keyword arguments to be passed to the underlying :fun:`ase.io.write` method.

Note that even if an ASE format supports appending, if that file requires additional data between frames (e.g. headers), then the resulting output may not be valid.

If ASE supports writing directly a file descriptor for the given format, then that will be used for performance, otherwise, the file will be reopened and appended to each frame, which will negatively impact performance.

close()

Closes the current file being written to, if it exists.

reset()

Resets the logger, restarting logging with a new file.

If the logger is set to use timestamps, a new file will be generated with the current time. Otherwise, the file will be overwritten.

..note

This method is used in Narupa to produce new trajectory files whenever the simulation is reset by the user.

timestamping

Indicates whether this logger is appending timestamps to the names of any files it produces.

Returns:True, if appending timestamps, False otherwise.
write()

Writes the current state of the atoms to file.

exception narupa.ase.trajectory_logger.UnsupportedFormatError

Bases: Exception

narupa.ase.trajectory_logger.validate_ase_can_append_filename(filename: str)
Raises:UnsupportedFormatError – if ase is unable to append the format implied by the file extension
narupa.ase.trajectory_logger.validate_ase_can_append_format(format: str)
Raises:UnsupportedFormatError – if ase is unable to append the desired format.