-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp2k_wfn.py
172 lines (161 loc) · 6.33 KB
/
cp2k_wfn.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
##### cp2k wfn reader and writer
##### May 8th 2015
##### YY
##### [email protected]
import struct
import numpy as np
class cp2k_wavefunction:
"""
class of cp2k wavefunction. read and write cp2k wavefunction.
"""
def __init__(self):
self.fileContent = ''
self.natom_read,self.nspin_read,self.nao_read,self.nset_max,self.nshell_max \
= (0,0,0,0,0)
self.nset_info = np.array([])
self.nshell_info = np.array([])
self.nso_info = np.array([])
self.nmo_all = []
self.homo_all = []
self.lfomo_all = []
self.nelectron_all = []
self.evals_all = []
self.occups_all = []
self.vecs_all = []
def __str__(self):
return_string = ""
return_string += "natom_read, nspin_read, nao_read, nset_max, nshell_max\n"
return_string += str(self.natom_read) + " " +\
str(self.nspin_read) + " " +\
str(self.nao_read) + " " +\
str(self.nset_max) + " " +\
str(self.nshell_max) + "\n"
return_string += "nset_info: " + str(self.nset_info) + "\n"
return_string += "nshell_info: " + str(self.nshell_info) + "\n"
return_string += "nso_info: " + str(self.nso_info) + "\n"
return_string += "nmo_all: " + str(self.nmo_all) + "\n"
return_string += "homo_all: " + str(self.homo_all) + "\n"
return_string += "lfomo_all: " + str(self.lfomo_all) + "\n"
return_string += "nelectron_all: " + str(self.nelectron_all) + "\n"
return_string += "evals_all: " + str(self.evals_all) + "\n"
return_string += "occups_all: " + str(self.occups_all) + "\n"
return_string += "vecs_all: " + str(self.vecs_all) + "\n"
return return_string
def __repr__(self):
return self.__str__()
def readline(self):
if len(self.fileContent) == 0:
print "end of File"
return False
len_line, = struct.unpack("I", self.fileContent[0:4])
line = self.fileContent[4:len_line+4]
self.fileContent = self.fileContent[4+4+len_line:]
return line
def writeline(self,words):
len_line = 0
fmt = ""
for word in words:
if type(word) == int or type(word) == np.int64:
len_line += 4
if type(word) == float or type(word) == np.float64:
len_line += 8
self.fileContent += struct.pack("I",len_line)
for word in words:
if type(word) == int or type(word) == np.int64:
fmt = "I"
if type(word) == float or type(word) == np.float64: #double
fmt = "d"
self.fileContent += struct.pack(fmt,word)
self.fileContent += struct.pack("I",len_line)
def read_cp2k_wfn(self,filename):
"""
read cp2k wfn file
"""
with open(filename, mode='rb') as file:
self.fileContent = file.read()
line = self.readline()
self.natom_read, \
self.nspin_read, \
self.nao_read, \
self.nset_max, \
self.nshell_max \
= struct.unpack("IIIII",line)
line = self.readline()
self.nset_info = np.array(struct.unpack( \
"I"*self.natom_read,line))
line = self.readline()
self.nshell_info = np.array(struct.unpack( \
"I"*self.natom_read*self.nset_max,line))
line = self.readline()
self.nso_info = np.array(struct.unpack( \
"I"*self.natom_read*self.nset_max*self.nshell_max,line))
self.vecs_all = []
self.nmo_all = []
self.homo_all = []
self.lfomo_all = []
self.nelectron_all = []
self.evals_all = []
self.occups_all = []
for i in range(self.nspin_read):
vecs_spin = []
line = self.readline()
if not line:
break
nmo,homo,lfomo,nelectron = \
struct.unpack("IIII",line)
self.nmo_all.append(nmo)
self.homo_all.append(homo)
self.lfomo_all.append(lfomo)
self.nelectron_all.append(nelectron)
line = self.readline()
evals = np.array(struct.unpack("d"*nmo,line[:8*nmo]))
occups = np.array(struct.unpack("d"*nmo,line[8*nmo:]))
self.evals_all.append(evals)
self.occups_all.append(occups)
for i in range(nmo):
line = self.readline()
vec = np.array(struct.unpack("d"*self.nao_read,line))
vecs_spin.append(vec)
self.vecs_all.append(vecs_spin)
def write_cp2k_wfn(self,filename):
"""
write cp2k wfn file.
"""
words = (self.natom_read,\
self.nspin_read,\
self.nao_read,\
self.nset_max,\
self.nshell_max)
self.writeline(words)
self.writeline(self.nset_info)
self.writeline(self.nshell_info)
self.writeline(self.nso_info)
for i in range(self.nspin_read):
if self.nmo_all[i] > 0:
words = (self.nmo_all[i],\
self.homo_all[i],\
self.lfomo_all[i],\
self.nelectron_all[i])
self.writeline(words)
words = [word for word in self.evals_all[i]] +\
[word for word in self.occups_all[i]]
self.writeline(words)
for j in range(self.nmo_all[i]):
self.writeline(self.vecs_all[i][j])
f = open(filename, 'wb')
f.write(self.fileContent)
def add_H(self, hydrogen_shells = [1,1,3]):
"""
function to add a proton basis in the wfn.
"""
self.natom_read += 1
self.nao_read += sum(hydrogen_shells)
self.nset_info = np.append(self.nset_info, 1)
self.nshell_info = np.append(self.nshell_info, len(hydrogen_shells))
hydrogen_nso_info = np.zeros(self.nshell_max)
for i in range(len(hydrogen_shells)):
hydrogen_nso_info[i] = hydrogen_shells[i]
self.nso_info = np.append(self.nso_info, hydrogen_nso_info)
hydrogen_vecs = np.zeros(sum(hydrogen_shells))
for i in range(self.nmo_all[0]):
self.vecs_all[0][i] = np.append(self.vecs_all[0][i], hydrogen_vecs)