Last active
December 13, 2019 15:45
-
-
Save Asif-Iqbal-Bhatti/1cefe8a462d7ff04931a to your computer and use it in GitHub Desktop.
exciting input file to VASP poscar file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from lxml import etree | |
import xml.etree.ElementTree as xml | |
from math import * | |
import sys, getopt, os | |
from sys import stdout | |
from array import * | |
import numpy as np | |
from collections import Counter | |
from termcolor import colored | |
#*******************************DOCUMENTATION*********************************** | |
# | |
#AUTHOR : ASIF IQBAL BHATTI | |
#CREATED ON : 18/10/2015 | |
#USAGE : To parse an EXCITING xml input file to VASP format. | |
#Output file has already been defined in the code (POSCAR.vasp). | |
#CAUTION: Use at your own risk (NOTEVEN IMPLIED GUARANTEED, WHATSOEVER), | |
#the code has been tested but the user in the end will have to verify the ouput | |
#geometry and be vary of the order of atoms in list. | |
# | |
#************************END OF DOCUMENTATION*********************************** | |
Bohr2angstrom = 0.529177249 | |
yellow = '\033[01;33m' | |
red = '\033[01;31m' | |
blue = '\x1b[01;34m' | |
cyan = '\x1b[01;36m' | |
green = yellow | |
#*********************DEFINITION FOR INPUTFILE********************************** | |
if len(sys.argv) < 2: | |
sys.exit('Usage: %s <Exciting xml inputfile>' % sys.argv[0]) | |
tree = xml.parse(sys.argv[1]) | |
root = tree.getroot() | |
struct = root.getchildren()[1] # pivot for controling element | |
lattice = struct.getchildren() | |
#*********************CONTROL SECTION FOR INPUT FILE*************************** | |
f = open('POSCAR.vasp', 'w+') | |
print colored("\nExtracting information from input file... ", 'blue') | |
print '-'*80 | |
print colored(" | Title is: ", 'blue'), root.findtext('title') | |
f.write(root.findtext('title')) | |
#*************************DETECTING # OF SPECIES****************************** | |
i = 0 | |
for child in struct: | |
i = i+1 | |
print colored(" | Species detected: ", 'blue'), i-1 | |
f.write(" |\t Number of species detected: %d\n" % (i-1)) | |
#***************************SCALING DEFINITION******************************** | |
for sca in struct.findall('crystal'): | |
for key in sca.attrib: | |
if key == 'scale': # SCALING HAS TO BE CHANGED | |
sc = sca.get('scale') | |
print colored(" | Scaling factor is: ", 'blue'), float(sc) | |
else: | |
sc = float(1.00) | |
print colored(" | Scaling factor is: ", 'blue'), float(sc) | |
print colored(" | Converting bohr to angstrom ... ", 'blue') | |
print colored(" | Writing to a File in VASP format ... ", 'green') | |
f.write("%f\n" % 1.0) | |
print '-'*80 | |
#***********************LATTICE VECTORS SECTION************************ | |
lv = [] | |
for vect in struct.findall('crystal'): | |
lat = vect.find('basevect') | |
for k in vect.findall('basevect'): | |
lv.append(k.text.split()) | |
for list in lv: | |
for lv1 in list: | |
a = float(lv1)*float(sc)*Bohr2angstrom | |
sys.stdout.write(green + "\t%16.10f" % (a)) | |
f.write("%16.10f" % a) | |
stdout.write("\n") | |
f.write("\n") | |
#******************SPECIES CONTROL SECTION********************************* | |
lab = [] | |
for coord in struct.findall('species'): | |
spe = coord.get('speciesfile') | |
jj = os.path.splitext(spe) | |
lab.append(jj[0]) | |
sp = [] | |
for x in range(i-1): | |
sys.stdout.write(green + "\t%s" % lab[x]) | |
f.write("\t%s" % lab[x]) | |
for s in struct[x+1].iter('atom'): | |
sp.append(lab[x]) | |
stdout.write("\n") | |
f.write("\n") | |
for h in range(i-1): | |
sys.stdout.write(green + "\t%d" % sp.count(lab[h])) | |
f.write("\t%d" % sp.count(lab[h])) | |
stdout.write("\n") | |
f.write("\n") | |
#*******************CONTROL SECTION FOR CARTESIAN COORDINATE*************** | |
lll = [] | |
ccl = [] | |
checker = False | |
for cart in root.findall('structure'): | |
ca = cart.get('cartesian') | |
for key in cart.attrib: | |
if key == 'cartesian': | |
checker = True | |
if ca == "true": | |
print "Cartesian" | |
f.write("Cartesian\n") | |
for i in range(i-1): | |
for s in struct[i+1].iter('atom'): | |
atom = s.get('coord') | |
ccl.append(atom.split()) | |
lll.append(lab[i]) | |
# print atom, lab[i] | |
for list in ccl: | |
for at in list: | |
a = float(at)* Bohr2angstrom | |
sys.stdout.write(green + "\t%16.10f" % (a)) | |
f.write("%16.10f" % (a)) | |
stdout.write("\n") | |
f.write("\n") | |
#**************************DIRECT COORDINATE********************* | |
else: | |
print "Direct" | |
for i in range(i-1): | |
for s in struct[i+1].iter('atom'): | |
atom = s.get('coord') | |
ccl.append(atom.split()) | |
lll.append(lab[i]) | |
for list in ccl: | |
for at in list: | |
a = float(at) | |
sys.stdout.write(green + "\t%16.10f" % (a)) | |
f.write("%16.10f" % (a)) | |
stdout.write("\n") | |
f.write("\n") | |
print "\n" | |
print colored("File generated ... ", 'red') | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment