RKF trajectory files¶
-
class
RKFTrajectoryFile
(filename, mode='rb', fileobject=None, ntap=None)[source]¶ Class representing an RKF file containing a molecular trajectory
An instance of this class has the following attributes:
file_object
– A PLAMSKFFile
object, referring to the actual RKF fileposition
– The frame to which the cursor is currently pointing in the RKF filemode
– Designates whether the file is in read or write mode (‘rb’ or ‘wb’)ntap
– The number of atoms in the molecular system (needs to be constant throughout)elements
– The elements of the atoms in the system (needs to be constant throughout)conect
– The connectivity information of the current framemddata
– Read mode only: A dictionary containing data from the MDHistory section in the RKF fileread_lattice
– Read mode only: Wether the lattice vectors will be read from the fileread_bonds
– Wether the connectivity information will be read from the filesaving_freq
– How often the ‘wb’ file is written (default: only whenclose()
is called)
An
RKFTrajectoryFile
object behaves very similar to a regular file object. It has read and write methods (read_next()
andwrite_next()
) that read and write from/to the position of the cursor in thefile_object
attribute. If the file is in read mode, an additional methodread_frame()
can be used that moves the cursor to any frame in the file and reads from there. The amount of information stored in memory is kept to a minimum, as only information from the latest frame is ever stored.Reading and writing to and from the files can be done as follows:
>>> from scm.plams import RKFTrajectoryFile >>> rkf = RKFTrajectoryFile('ams.rkf') >>> mol = rkf.get_plamsmol() >>> rkfout = RKFTrajectoryFile('new.rkf',mode='wb') >>> for i in range(rkf.get_length()) : >>> crd,cell = rkf.read_frame(i,molecule=mol) >>> rkfout.write_next(molecule=mol) >>> rkfout.close()
The above script reads information from the RKF file
ams.rkf
into theMolecule
objectmol
in a step-by-step manner. TheMolecule
object is then passed to thewrite_next()
method of the newRKFTrajectoryFile
object corresponding to the new rkf filenew.rkf
.The exact same result can also be achieved by iterating over the instance as a callable
>>> rkf = RKFTrajectoryFile('ams.rkf') >>> mol = rkf.get_plamsmol()
>>> rkfout = RKFTrajectoryFile('new.rkf',mode='wb')
>>> for crd,cell in rkf(mol) : >>> rkfout.write_next(molecule=mol) >>> rkfout.close()
This procedure requires all coordinate information to be passed to and from the
Molecule
object for each frame, which can be time-consuming. Some time can be saved by bypassing theMolecule
object:>>> rkf = RKFTrajectoryFile('ams.rkf') >>> rkfout = RKFTrajectoryFile('new.rkf',mode='wb') >>> rkfout.set_elements(rkf.get_elements()) >>> for crd,cell in rkf : >>> rkfout.write_next(coords=crd,cell=cell,conect=rkf.conect) >>> rkfout.close()
The only mandatory argument to the
write_next()
method iscoords
. Further time can be saved by setting theread_lattice
andread_bonds
variables to False.By default the write mode will create a minimal version of the RKF file, containing only elements, coordinates, lattice, and connectivity information. This minimal file format can be read by AMSMovie.
It is possible to store additional information, such as energies, velocities, and charges. To enable this, the method
store_mddata()
needs to be called after creation, and a dictionary of mddata needs to be passed to thewrite_next()
method. When that is done, the AMS trajectory analysis tools can be used on the file. Restarting an MD run with such a file is however currently not possible:>>> rkf = RKFTrajectoryFile('ams.rkf') >>> rkf.store_mddata() >>> mol = rkf.get_plamsmol() >>> rkf_out = RKFTrajectoryFile('new.rkf',mode='wb') >>> rkf_out.store_mddata(rkf) >>> for i in range(len(rkf)) : >>> crd,cell = rkf.read_frame(i,molecule=mol) >>> rkf_out.write_next(molecule=mol,mddata=rkf.mddata) >>> rkf_out.close()
-
__init__
(filename, mode='rb', fileobject=None, ntap=None)[source]¶ Initiates an RKFTrajectoryFile object
filename
– The path to the RKF filemode
– The mode in which to open the RKF file (‘rb’ or ‘wb’)fileobject
– Optionally, a file object can be passed instead (filename needs to be set to None)ntap
– If the file is in write mode, the number of atoms needs to be passed here
-
store_mddata
(rkf=None)[source]¶ Read/write an MDHistory section
rkf
– If in write mode an RKFTrajectoryFile object in read mode needs to be passed to extract unit info
-
close
(override_molecule_section_with_last_frame=True)[source]¶ Execute all prior commands and cleanly close and garbage collect the RKF file
-
_update_celldata
(cell)[source]¶ Use the newly supplied cell to update the dimensionality of the system
-
read_frame
(i, molecule=None)[source]¶ Reads the relevant info from frame
i
and returns it, or stores it inmolecule
i
– The frame number to be read from the RKF filemolecule
–Molecule
object in which the new coordinates need to be stored
-
_store_historydata_for_step
(istep)[source]¶ Store the extra data from the History section
Note: Block format is not used in the History section
-
read_next
(molecule=None, read=True)[source]¶ Reads coordinates and lattice vectors from the current position of the cursor and returns it
molecule
–Molecule
object in which the new coordinates need to be storedread
– If set to False the cursor will move to the next frame without reading
-
write_next
(coords=None, molecule=None, cell=[0.0, 0.0, 0.0], conect=None, historydata=None, mddata=None)[source]¶ Write frame to next position in trajectory file
coords
– A list or numpy array of (ntap
,3) containing the system coordinates in angstrommolecule
– A molecule object to read the molecular data fromcell
– A set of lattice vectors (or cell diameters for an orthorhombic system) in angstromconect
– A dictionary containing the connectivity info (e.g. {1:[2],2:[1]})historydata
– A dictionary containing additional variables to be written to the History sectionmddata
– A dictionary containing the variables to be written to the MDHistory section
The
mddata
dictionary can contain the following keys: (‘TotalEnergy’, ‘PotentialEnergy’, ‘Step’, ‘Velocities’, ‘KineticEnergy’, ‘Charges’, ‘ConservedEnergy’, ‘Time’, ‘Temperature’)The
historydata
dictionary can contain for example: (‘Energy’,’Gradients’,’StressTensor’) All values must be in atomic units Numpy arrays or lists of lists will be flattened before they are written to the fileNote
Either
coords
ormolecule
are mandatory arguments
-
_set_energy
(mddata, historydata)[source]¶ Looks if an energy is passed as input, and it not, sets to zero
-
_write_dictionary_to_history
(data, section, counter=1)[source]¶ Add the entries of a dictionary to a History section
RKF history files¶
This subsection describes the API of the RKFHistoryFile
class,
which can read and write the results from simulations with changing numbers of atoms.
The majority of molecular simulations explore a subspace of the canonical, micro-canonical,
or isothermal-isobaric ensembles, in which the number of atoms \(N\) remains constant.
However, a Grand Canonical Monte Carlo simulation is one of the exceptions in which the number of atoms in the
system does change.
The RKFTrajectoryFile
object cannot read and write the resulting simulation history,
and the derived class RKFHistoryFile
was developed to handle these atypical trajectories.
While the methods in this class will be slower than the ones in the parent class, the API is nearly identical.
The only exception is the write_next()
method, which has an additional argument elements
.
-
class
RKFHistoryFile
(filename, mode='rb', fileobject=None, ntap=None)[source]¶ Class representing an RKF file containing a molecular simulation history with varying numbers of atoms
An instance of this class has the following attributes:
file_object
– A PLAMSKFFile
object, referring to the actual RKF fileposition
– The frame to which the cursor is currently pointing in the RKF filemode
– Designates whether the file is in read or write mode (‘rb’ or ‘wb’)elements
– The elements of the atoms in the system at the current frameconect
– The connectivity information of the current framemddata
– Read mode only: A dictionary containing data from the MDHistory section in the RKF fileread_lattice
– Read mode only: Wether the lattice vectors will be read from the fileread_bonds
– Wether the connectivity information will be read from the file
An
RKFHistoryFile
object behaves very similar to a regular file object. It has read and write methods (read_next()
andwrite_next()
) that read and write from/to the position of the cursor in thefile_object
attribute. If the file is in read mode, an additional methodread_frame()
can be used that moves the cursor to any frame in the file and reads from there. The amount of information stored in memory is kept to a minimum, as only information from the latest frame is ever stored.Reading and writing to and from the files can be done as follows:
>>> from scm.plams import RKFHistoryFile >>> rkf = RKFHistoryFile('ams.rkf') >>> mol = rkf.get_plamsmol() >>> rkfout = RKFHistoryFile('new.rkf',mode='wb') >>> for i in range(rkf.get_length()) : >>> crd,cell = rkf.read_frame(i,molecule=mol) >>> rkfout.write_next(molecule=mol) >>> rkfout.close()
The above script reads information from the RKF file
ams.rkf
into theMolecule
objectmol
in a step-by-step manner.. TheMolecule
object is then passed to thewrite_next()
method of the newRKFHistoryFile
object corresponding to the new rkf filenew.rkf
.The exact same result can also be achieved by iterating over the instance as a callable
>>> rkf = RKFHistoryFile('ams.rkf') >>> mol = rkf.get_plamsmol()
>>> rkfout = RKFHistoryFile('new.rkf',mode='wb')
>>> for crd,cell in rkf(mol) : >>> rkfout.write_next(molecule=mol) >>> rkfout.close()
This procedure requires all coordinate information to be passed to and from the
Molecule
object for each frame, which can be time-consuming. Some time can be saved by bypassing theMolecule
object:>>> rkf = RKFHistoryFile('ams.rkf') >>> rkfout = RKFHistoryFile('new.rkf',mode='wb') >>> for crd,cell in rkf : >>> rkfout.write_next(coords=crd,cell=cell,elements=rkf.elements,conect=rkf.conect) >>> rkfout.close()
The only mandatory argument to the
write_next()
method iscoords
. Further time can be saved by setting theread_lattice
andread_bonds
variables to False.By default the write mode will create a minimal version of the RKF file, containing only elements, coordinates, lattice, and connectivity information. This minimal file format can be read by AMSMovie.
If the original RKF file contains an MDHistory section (if it resulted from a MolecularGun simulation) it is possible to store the information from that section and write it to another file. To enable this, the method
store_mddata()
needs to be called after creation, and a dictionary of mddata needs to be passed to thewrite_next()
method. When that is done, the AMS trajectory analysis tools can be used on the file. Restarting an MD run with such a file is however currently not possible:>>> rkf = RKFHistoryFile('ams.rkf') >>> rkf.store_mddata() >>> mol = rkf.get_plamsmol() >>> rkf_out = RKFHistoryFile('new.rkf',mode='wb') >>> rkf_out.store_mddata(rkf) >>> for i in range(rkf.get_length()) : >>> crd,cell = rkf.read_frame(i,molecule=mol) >>> rkf_out.write_next(molecule=mol,mddata=rkf.mddata) >>> rkf_out.close()
-
__init__
(filename, mode='rb', fileobject=None, ntap=None)[source]¶ Initializes the RKFHistoryFile object
filename
– The path to the RKF filemode
– The mode in which to open the RKF file (‘rb’ or ‘wb’)fileobject
– Optionally, a file object can be passed instead (filename needs to be set to None)ntap
– If the file is in write mode, the number of atoms can be passed here
-
_rewrite_molecule
()[source]¶ Overwrite the molecule section with the latest frame (called in close())
-
_correct_chemical_system
(elements, prev_elements, added_atoms, removed_atoms)[source]¶ Check if the referenced chemical system is correct, and if not, find one matching added/removed atoms
-
write_next
(coords=None, molecule=None, elements=None, cell=[0.0, 0.0, 0.0], conect=None, historydata=None, mddata=None)[source]¶ Write frame to next position in trajectory file
coords
– A list or numpy array of (ntap
,3) containing the system coordinatesmolecule
– A molecule object to read the molecular data fromelements
– The element symbols of the atoms in the systemcell
– A set of lattice vectors (or cell diameters for an orthorhombic system)conect
– A dictionary containing the connectivity info (e.g. {1:[2],2:[1]})historydata
– A dictionary containing additional variables to be written to the History sectionmddata
– A dictionary containing the variables to be written to the MDHistory section
The
mddata
dictionary can contain the following keys: (‘TotalEnergy’, ‘PotentialEnergy’, ‘Step’, ‘Velocities’, ‘KineticEnergy’, ‘Charges’, ‘ConservedEnergy’, ‘Time’, ‘Temperature’)The
historydata
dictionary can contain for example: (‘Energy’,’Gradients’,’StressTensor’) All values must be in atomic units Numpy arrays or lists of lists will be flattened before they are written to the fileNote
Either
coords
andelements
ormolecule
are mandatory arguments