Is it possible to export to XYZ trajectory file from TrajectoryData?

I am trying to understand whether there is a built-in aiida function that exports TrajectoryData info to XYZ format (or similar). By far, I haven’t found much. Any idea or pointer, or shall one write a custom function?
Thanks a lot!

I see two options:

  • TrajectoryData.get_positions() together with TrajectoryData.symbols gives you all information and then you format it manually
  • TrajectoryData.get_structure() to get a StructureData which comes with _prepare_xyz to return it in XYZ format.

Think latter is easier as you don’t reimplement formatting yourself, but not sure about performance of having to go to StructureData as intermediate.

1 Like

Thanks Sebastian!
Since I needed energy, forces, and stresse for training such data, I found this solution:

from qe_tools import CONSTANTS as C
from import write
from ase import units
from ase.calculators.singlepoint import SinglePointCalculator

filename = './'
stress_units = (-1 * units.Ry / units.Bohr**3)/(C.ry_si / C.bohr_si**3 / 10**9) # convention as in ASE (sign and eV/Ang^3)

index = 0
atoms = traj.get_step_structure(index).get_ase()
energy = traj.get_array('energy')[index]
s = traj.get_array('stress')[index]

calc = SinglePointCalculator(atoms)

calc.results = {
    'energy': energy,
    'free_energy': energy,
    'forces': traj.get_array('forces')[index],
    'stress': stress_units*np.array([s[0,0],s[1,1],s[2,2],s[1,2],s[0,2],s[0,1]]),

atoms.calc = calc

write(filename, atoms, format='extxyz', append=True)

I think one can also collect in a single calculator more than one “Atoms”, and then write it once instead of looping each index. Happy to receive suggestions here on how to optimize (:

I struggled with this as well, and in the end used the get_step_structure. It might be nice if TrajectoryData had the get_ase() method similarly to StructureData.

Another quality-of-life improvement would be if verdi data core.trajectory export supported the extended XYZ format.