-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfplib2.py
More file actions
201 lines (183 loc) · 7.44 KB
/
fplib2.py
File metadata and controls
201 lines (183 loc) · 7.44 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import numpy as np
from scipy.optimize import linear_sum_assignment
import rcovdata
# import numba
# @numba.jit()
def get_gom(lseg, rxyz, rcov, amp):
# s orbital only lseg == 1
nat = len(rxyz)
if lseg == 1:
om = np.zeros((nat, nat))
for iat in range(nat):
for jat in range(nat):
d = rxyz[iat] - rxyz[jat]
d2 = np.vdot(d, d)
r = 0.5/(rcov[iat]**2 + rcov[jat]**2)
om[iat][jat] = np.sqrt( 4.0*r*(rcov[iat]*rcov[jat]) )**3 \
* np.exp(-1.0*d2*r) * amp[iat] * amp[jat]
else:
# for both s and p orbitals
om = np.zeros((4*nat, 4*nat))
for iat in range(nat):
for jat in range(nat):
d = rxyz[iat] - rxyz[jat]
d2 = np.vdot(d, d)
r = 0.5/(rcov[iat]**2 + rcov[jat]**2)
om[4*iat][4*jat] = np.sqrt( 4.0*r*(rcov[iat]*rcov[jat]) )**3 \
* np.exp(-1*d2*r) * amp[iat] * amp[jat]
# <s_i | p_j>
sji = np.sqrt(4.0*rcov[iat]*rcov[jat])**3 * np.exp(-1*d2*r)
stv = np.sqrt(8.0) * rcov[jat] * r * sji
om[4*iat][4*jat+1] = stv * d[0] * amp[iat] * amp[jat]
om[4*iat][4*jat+2] = stv * d[1] * amp[iat] * amp[jat]
om[4*iat][4*jat+3] = stv * d[2] * amp[iat] * amp[jat]
# <p_i | s_j>
stv = np.sqrt(8.0) * rcov[iat] * r * sji * -1.0
om[4*iat+1][4*jat] = stv * d[0] * amp[iat] * amp[jat]
om[4*iat+2][4*jat] = stv * d[1] * amp[iat] * amp[jat]
om[4*iat+3][4*jat] = stv * d[2] * amp[iat] * amp[jat]
# <p_i | p_j>
stv = -8.0 * rcov[iat] * rcov[jat] * r * r * sji
om[4*iat+1][4*jat+1] = stv * (d[0] * d[0] - 0.5/r) * amp[iat] * amp[jat]
om[4*iat+1][4*jat+2] = stv * (d[1] * d[0] ) * amp[iat] * amp[jat]
om[4*iat+1][4*jat+3] = stv * (d[2] * d[0] ) * amp[iat] * amp[jat]
om[4*iat+2][4*jat+1] = stv * (d[0] * d[1] ) * amp[iat] * amp[jat]
om[4*iat+2][4*jat+2] = stv * (d[1] * d[1] - 0.5/r) * amp[iat] * amp[jat]
om[4*iat+2][4*jat+3] = stv * (d[2] * d[1] ) * amp[iat] * amp[jat]
om[4*iat+3][4*jat+1] = stv * (d[0] * d[2] ) * amp[iat] * amp[jat]
om[4*iat+3][4*jat+2] = stv * (d[1] * d[2] ) * amp[iat] * amp[jat]
om[4*iat+3][4*jat+3] = stv * (d[2] * d[2] - 0.5/r) * amp[iat] * amp[jat]
# for i in range(len(om)):
# for j in range(len(om)):
# if abs(om[i][j] - om[j][i]) > 1e-6:
# print ("ERROR", i, j, om[i][j], om[j][i])
return om
# @numba.jit()
def get_fp_nonperiodic(rxyz, znucls):
rcov = []
amp = [1.0] * len(rxyz)
for x in znucls:
rcov.append(rcovdata.rcovdata[x][2])
gom = get_gom(1, rxyz, rcov, amp)
fp = np.linalg.eigvals(gom)
fp = sorted(fp)
fp = np.array(fp, float)
return fp
# @numba.jit()
def get_fpdist_nonperiodic(fp1, fp2):
d = fp1 - fp2
return np.sqrt(np.vdot(d, d))
# @numba.jit()
def get_fp(contract, ntyp, nx, lmax, lat, rxyz, types, znucl, cutoff):
if lmax == 0:
lseg = 1
l = 1
else:
lseg = 4
l = 2
ixyz = get_ixyz(lat, cutoff)
NC = 3
wc = cutoff / np.sqrt(2.* NC)
fc = 1.0 / (2.0 * NC * wc**2)
nat = len(rxyz)
cutoff2 = cutoff**2
n_sphere_list = []
lfp = []
sfp = []
for iat in range(nat):
rxyz_sphere = []
rcov_sphere = []
ind = [0] * (lseg * nx)
amp = []
xi, yi, zi = rxyz[iat]
n_sphere = 0
for jat in range(nat):
for ix in range(-ixyz, ixyz+1):
for iy in range(-ixyz, ixyz+1):
for iz in range(-ixyz, ixyz+1):
xj = rxyz[jat][0] + ix*lat[0][0] + iy*lat[1][0] + iz*lat[2][0]
yj = rxyz[jat][1] + ix*lat[0][1] + iy*lat[1][1] + iz*lat[2][1]
zj = rxyz[jat][2] + ix*lat[0][2] + iy*lat[1][2] + iz*lat[2][2]
d2 = (xj-xi)**2 + (yj-yi)**2 + (zj-zi)**2
if d2 <= cutoff2:
n_sphere += 1
if n_sphere > nx:
print ("FP WARNING: the cutoff is too large.")
amp.append((1.0-d2*fc)**NC)
# print (1.0-d2*fc)**NC
rxyz_sphere.append([xj, yj, zj])
rcov_sphere.append(rcovdata.rcovdata[znucl[types[jat]-1]][2])
if jat == iat and ix == 0 and iy == 0 and iz == 0:
ityp_sphere = 0
else:
ityp_sphere = types[jat]
for il in range(lseg):
if il == 0:
# print len(ind)
# print ind
# print il+lseg*(n_sphere-1)
ind[il+lseg*(n_sphere-1)] = ityp_sphere * l
else:
ind[il+lseg*(n_sphere-1)] == ityp_sphere * l + 1
n_sphere_list.append(n_sphere)
rxyz_sphere = np.array(rxyz_sphere, float)
# full overlap matrix
nid = lseg * n_sphere
gom = get_gom(lseg, rxyz_sphere, rcov_sphere, amp)
val, vec = np.linalg.eig(gom)
val = np.real(val)
fp0 = np.zeros(nx*lseg)
for i in range(len(val)):
fp0[i] = val[i]
lfp.append(sorted(fp0))
pvec = np.real(np.transpose(vec)[0])
# contracted overlap matrix
if contract:
nids = l * (ntyp + 1)
omx = np.zeros((nids, nids))
for i in range(nid):
for j in range(nid):
# print ind[i], ind[j]
omx[ind[i]][ind[j]] = omx[ind[i]][ind[j]] + pvec[i] * gom[i][j] * pvec[j]
# for i in range(nids):
# for j in range(nids):
# if abs(omx[i][j] - omx[j][i]) > 1e-6:
# print ("ERROR", i, j, omx[i][j], omx[j][i])
# print omx
sfp0 = np.linalg.eigvals(omx)
sfp.append(sorted(sfp0))
print ("n_sphere_min", min(n_sphere_list))
print ("n_shpere_max", max(n_sphere_list))
if contract:
sfp = np.array(sfp, float)
return sfp
else:
lfp = np.array(lfp, float)
return lfp
# @numba.jit()
def get_ixyz(lat, cutoff):
lat2 = np.matmul(lat, np.transpose(lat))
# print lat2
vec = np.linalg.eigvals(lat2)
# print (vec)
ixyz = int(np.sqrt(1.0/max(vec))*cutoff) + 1
return ixyz
# @numba.jit()
def get_fpdist(ntyp, types, fp1, fp2):
nat, lenfp = np.shape(fp1)
fpd = 0.0
for ityp in range(ntyp):
itype = ityp + 1
MX = np.zeros((nat, nat))
for iat in range(nat):
if types[iat] == itype:
for jat in range(nat):
if types[jat] == itype:
tfpd = fp1[iat] - fp2[jat]
MX[iat][jat] = np.sqrt(np.vdot(tfpd, tfpd)/lenfp)
row_ind, col_ind = linear_sum_assignment(MX)
# print(row_ind, col_ind)
total = MX[row_ind, col_ind].sum()
fpd += total
fpd = fpd / nat
return fpd