-
Notifications
You must be signed in to change notification settings - Fork 0
/
bader_new.py
103 lines (93 loc) · 2.37 KB
/
bader_new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import numpy as np
from xml.dom import minidom
from sys import argv
from ase import io
# read ZVAL from POTCAR
def read_ZVAL(filename = "POTCAR"):
with open(filename) as f:
c = f.readlines()
zval = []
for i in c:
if "ZVAL" in i:
i = i.split()[5]
zval.append(float(i))
return zval
# read ACF.dat
def read_ACF(filename="ACF.dat"):
with open(filename,"r") as f:
content = f.readlines()
c = content[2:-4]
new = []
for i in c:
i = i.split()
i = [float(x) for x in i]
new.append(i)
charges = np.array(new)[:,4]
count = 0
total_charge = []
for i in valence_electron:
total_charge+=[i]*atom_map[count]
count += 1
return np.subtract(np.array(total_charge),charges)
# read atom num
def atom_read(filename="CONTCAR"):
atom = io.read(filename)
atom_list = list(atom.symbols)
atom_set = set(atom_list)
dic={}
for i in atom_list:
for a in atom_set:
if i == a:
if a not in dic:
dic[a] = 1
else:
dic[a] += 1
return atom_set,dic
# IO
valence_electron = read_ZVAL()
atom_map = list(atom_read()[1].values())
infile = "CONTCAR"
outfile = "CONTCAR.xsd"
print(f"ZVAL = {valence_electron}")
print(f"ATOM = {atom_map}")
print("Please Check!")
# main
# 读取文件
atom = io.read(infile,format="vasp")
io.write(outfile,images=atom,format="xsd")
# 解析XSD文件
doc =minidom.parse(outfile)
root = doc.documentElement
elements=root.getElementsByTagName("Atom3d")
c=0
charge=read_ACF()
for element in elements:
element.setAttribute("Charge",f"{charge[c]}")
c += 1
# 写出XSD文件
with open(outfile, "w", encoding="utf-8") as f:
doc.writexml(f, indent='', addindent='\t', newl='\n', encoding="utf-8")
# post-processing
average=[]
c = 0
t = 0
i=0
all_atom = list(atom.symbols)
for a_c in charge:
t += a_c
c += 1
if c == atom_map[i]:
average.append(t/atom_map[i])
if i+1 == len(atom_map):
break
i += 1
c = 0
t = 0
with open("bader_data.csv","w") as f:
f.write(f"average charge: ")
for i in range(len(atom_map)):
f.write(f"{average[i]};")
f.write("\n")
for i in range(sum(atom_map)):
f.write(f"{all_atom[i]},{charge[i]}\n")
print(f"The average charge = {average}")