Skip to content

Instantly share code, notes, and snippets.

@simonbatzner
Created February 19, 2022 17:18
Show Gist options
  • Save simonbatzner/c2b05d38789b67f6fe5d3c75a4f2223d to your computer and use it in GitHub Desktop.
Save simonbatzner/c2b05d38789b67f6fe5d3c75a4f2223d to your computer and use it in GitHub Desktop.
Convert VASP OUTCAR to extxyz file for NequIP input
from ase.io import read, write
# outcar contanining one (single-point) or multiple (trajectory) DFT frames
in_filename = './OUTCAR'
out_filename = 'nequip-data.extxyz'
# read all frames into a list of ase.Atoms objects
all_atoms = read(in_filename, format='vasp-out', index=':')
for curr_atoms in enumerate(all_atoms):
write('out_filename', curr_atoms, append=True, format='extxyz')
@AhmedBourouis
Copy link

Hi, Thank you for the code
i don't think its taking into consideration all the frames (at least for me), i ran the code on a system with 390 atom and 37 frame, i confirmed that using dpdata package as follow:
image

but when i read the same file with ase.io.read i get the following:
image

normally this shouldn't be happening since index is settled to all (':') , maybe there is something that i'm not seeing it !
Can you help me with that please ?

@AhmedBourouis
Copy link

the list i'm getting has a length of 1 instead of 37 (num of frames)
image

@simonbatzner
Copy link
Author

Hm, this worked for me in the past, and looking at the code here it does indeed iterate over frames:

@reader
def read_vasp_out(filename='OUTCAR', index=-1):
    """Import OUTCAR type file.

    Reads unitcell, atom positions, energies, and forces from the OUTCAR file
    and attempts to read constraints (if any) from CONTCAR/POSCAR, if present.
    """
    # "filename" is actually a file-descriptor thanks to @reader
    g = iread_vasp_out(filename, index=index)
    # Code borrowed from formats.py:read
    if isinstance(index, (slice, str)):
        # Return list of atoms
        return list(g)
    else:
        # Return single atoms object
        return next(g)

Can I ask what is in 220.OUTCAR, does it come from an AIMD simulation?

@AhmedBourouis
Copy link

yes 200.OUTCAR come from an AIMD simulation
image

it worked fine with dpdata package and i can't see why its not working with ase

@simonbatzner
Copy link
Author

Hm, that seems more like a question for the ASE folks, I'm sorry I can't help you much here.

@apoletayev
Copy link

apoletayev commented Dec 2, 2022

Unrelated to previous comments: there is a glitch in reading OUTCAR's with ase if one (a) installs ase with anaconda and (b) uses spin-polarized calculations in (c) VASP 6. It manifests as an AssertionError. It can be fixed by taking vasp_outcar_parsers.py from the ase GitLab repo (tested 2022.12.02) and substituting that for the anaconda-installed one.

@mariummou
Copy link

hello,
I replaced the vasp-out file,but still I am getting this error: ase.io.ParseError: Did not find required property positions during parse. My outcar files include positions but the script can not find any. thanks

@kartiksau89
Copy link

kartiksau89 commented Dec 27, 2024

This one works.

from ase.io import read, write

Read all frames from OUTCAR

atoms_list = read('OUTCAR', index=':')

Write all frames to a single EXTXYZ file

write('output.extxyz', atoms_list)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment