-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit_parsing.py
149 lines (138 loc) · 6.91 KB
/
init_parsing.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#Main output file must be called input_name.out
#Mulliken population file must be called input_name.mulliken
#Gradient file must be generated with FORMAT XYZ and be called input_name.xyz
import os
#To be set
spin_center = ""
input_name = ""
es_program = ""
calc_type = ""
def parse_energies():
#Returns list of energies of each spin state in subfolders of wfs_opt/iter_step
energies = []
for i in sorted(os.listdir(".")):
if os.path.isdir(os.path.join(".", i)):
file_obj = open(os.path.join(".", i, input_name + ".out"), "r")
file_list = file_obj.readlines()
file_obj.close()
for j in reversed(file_list):
if es_program == "cp2k":
if "Total energy:" in j:
energies.append(j.split()[-1])
break
elif es_program == "orca":
if "FINAL SINGLE POINT ENERGY " in j:
energies.append(j.split()[-1])
break
return energies
def print_energies():
file_obj = open("Energies.dat", "w")
energies = parse_energies()
for i in energies:
file_obj.write(i + "\n")
file_obj.close()
def parse_spin_moments():
#Returns list of spin moments of spin centers in each spin state in subfolders of wfs_opt/iter_step
spin_moments = []
for i in sorted(os.listdir(".")):
if os.path.isdir(os.path.join(".", i)):
if es_program == "cp2k":
file_obj = open(os.path.join(".", i, input_name + ".mulliken"), "r")
file_list = file_obj.readlines()
file_obj.close()
spin_moments.append([])
for j in file_list[5:-3]:
if spin_center in j:
spin_moments[-1].append(j.split()[-1])
if "Mulliken Population Analysis" in j:
break
elif es_program == "orca":
file_obj = open(os.path.join(".", i, input_name + ".out"), "r")
file_list = file_obj.readlines()
file_obj.close()
spin_moments.append([])
if calc_type == "MP2":
vpattern = "MULLIKEN ATOMIC CHARGES AND SPIN DENSITIES"
elif calc_type == "DFT":
vpattern = "MULLIKEN ATOMIC CHARGES AND SPIN POPULATIONS"
for j in reversed(range(len(file_list))):
if "Sum of atomic charges" in file_list[j]:
bottom_index = j
if vpattern in file_list[j]:
top_index = j
break
for j in file_list[top_index:bottom_index]:
if spin_center in j:
spin_moments[-1].append(j.split()[-1])
return spin_moments
def print_spin_moments():
file_obj = open("M_values.dat", "w")
spin_moments = parse_spin_moments()
file_obj.write(str(len(spin_moments)) + " " + str(len(spin_moments[0])) + "\n")
for i in spin_moments:
file_obj.write("\t".join(i) + "\n")
file_obj.close()
def parse_S2():
#Returns list of S**2 for each spin state in subfolders of wfs_opt/iter_step
S2 = []
for i in sorted(os.listdir(".")):
if os.path.isdir(os.path.join(".", i)):
file_obj = open(os.path.join(".", i, input_name + ".out"), "r")
file_list = file_obj.readlines()
file_obj.close()
for j in reversed(file_list):
if es_program == "cp2k":
if "Ideal and single determinant S**2 :" in j:
S2.append(j.split()[-1])
break
elif es_program == "orca":
if "Expectation value of <S**2> " in j:
S2.append(j.split()[-1])
break
return S2
def print_S2():
file_obj = open("S2_tot.dat", "w")
S2 = parse_S2()
for i in S2:
file_obj.write(i + "\n")
file_obj.close()
def parse_gradients():
#Returns list of gradients of each spin state in subfolders of wfs_opt/iter_step
gradients = []
for i in sorted(os.listdir(".")):
if os.path.isdir(os.path.join(".", i)):
if es_program == "cp2k":
file_obj = open(os.path.join(".", i, input_name + ".xyz"), "r")
file_list = file_obj.readlines()
file_obj.close()
gradients.append([])
for j in file_list[4:-1]:
for k in j.split()[-3:]:
gradients[-1].append(str(-1 * float(k)))
elif es_program == "orca":
file_obj = open(os.path.join(".", i, input_name + ".engrad"), "r")
file_list = file_obj.readlines()
file_obj.close()
gradients.append([])
for j in range(len(file_list)):
if "The current gradient in Eh/bohr" in file_list[j]:
top_index = j
elif "The atomic numbers and current coordinates in Bohr" in file_list[j]:
bottom_index = j
for k in file_list[top_index + 2:bottom_index - 1]:
gradients[-1].append(k.strip())
return gradients
def print_gradients():
file_obj = open("engrad.dat", "w")
gradients = parse_gradients()
file_obj.write(str(len(gradients)) + " " + str(len(gradients[0])) + "\n")
for i in gradients:
file_obj.write("\t".join(i) + "\n")
file_obj.close()
def main():
print_energies()
print_spin_moments()
print_S2()
print_gradients()
if __name__ == "__main__":
main()