-
Notifications
You must be signed in to change notification settings - Fork 1
/
dymmatrix_surf.py
154 lines (131 loc) · 4.71 KB
/
dymmatrix_surf.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
#!/usr/bin/env python
import numpy
numpy.seterr(all='raise')
import os
import glob
import ase.io
from sys import exit
def check_file(filename):
if not os.path.isfile(filename):
print('No such file: %s' % filename)
exit(1)
def dymmatrix(displacecars, outcars):
all_displacements = []
displacements = None
for displacecar in displacecars:
check_file(displacecar)
print('Reading %s' % displacecar)
d = numpy.loadtxt(displacecar)[:,:3].flatten()
all_displacements.extend(d)
if displacements == None:
displacements = d
else:
displacements += d
ndisp = numpy.count_nonzero(displacements)
#indices of the nonzero displacements
di = numpy.nonzero(all_displacements)[0] #% ndisp
unconstr_indices = [int(i) for i in di[::3]/3]
print('Number of displacements: %i' % ndisp)
print(f'Unconstrained indices: {unconstr_indices}')
numpy.savetxt('unconstrained.dat', unconstr_indices, fmt='%d')
traj = []
for outcar in outcars:
check_file(outcar)
print('Reading %s' % outcar)
images = ase.io.read(outcar, index=':')
atoms = images[0]
traj += images[1:]
unconstr_forces = atoms.get_forces()[unconstr_indices]
reference_force = atoms.get_forces().flatten()
masses = traj[0].get_masses()
displacement_masses = []
for mass in masses:
displacement_masses.extend([mass,mass,mass])
masses = numpy.array(displacement_masses)
if len(traj) != ndisp:
print('ERROR: number of displacements (%i) !=' % ndisp)
print('number of calculations (%i)' % len(traj))
exit(1)
print('Building dynamical matrix')
dymmat = numpy.zeros((ndisp,ndisp))
print(f'Dynamical matrix: {dymmat.shape}')
hessian = numpy.zeros_like(dymmat)
print(f'Hessian: {hessian.shape}')
for i in range(ndisp):
f1 = traj[i].get_forces().flatten()[di]
f0 = reference_force[di]
dymmat[i] = -(f1-f0)
dymmat[i] /= displacements[di]
hessian[i] = dymmat[i]
for j in range(ndisp):
dymmat[i,j] /= numpy.sqrt(masses[di][i]*masses[di][j])
# for i in range(ndisp):
# f1 = traj[i].get_forces().flatten()[di]
# f0 = reference_force[di]
# dymmat[i] = -(f1-f0)
# dymmat[i] /= displacements[di]
# hessian[i] = dymmat[i]
# for j in range(ndisp):
# dymmat[i,j] /= numpy.sqrt(masses[di][i]*masses[di][j])
#symmetrize
dymmat = (dymmat + dymmat.transpose()) / 2.0
hessian = (hessian + hessian.transpose()) / 2.0
numpy.savetxt('freq.mat', dymmat, fmt='%16.8f')
print('Diagonalizing matrix')
omegas, ev = numpy.linalg.eigh(dymmat)
numpy.savetxt('eigs.dat', omegas, fmt='%25.15g')
f = open('freq.dat', 'w')
for omega in omegas:
imag = 0
if omega < 0:
imag = 1
freq = numpy.sqrt(numpy.abs(omega))*521.47
s = '%12.6f cm^{-1} ... %i ' % (freq, imag)
f.write(s+'\n')
print(s)
f.close()
print(f'Modes: {ev.shape}')
numpy.savetxt('modes.dat', ev, fmt='%16.8f')
#
f = open('modes_sqrt_amu.dat', 'w')
masses_ = atoms.get_masses()
for i in range(len(unconstr_indices)*3):
evec = ev[:,i].tolist() # eigenvectors are in columns
for j in range(len(unconstr_indices)):
dx = evec[3*j]/numpy.sqrt(masses_[unconstr_indices[j]])
dy = evec[3*j+1]/numpy.sqrt(masses_[unconstr_indices[j]])
dz = evec[3*j+2]/numpy.sqrt(masses_[unconstr_indices[j]])
f.write('%10.6f %10.6f %10.6f\n' % (dx, dy, dz))
f.write('\n')
f.close()
#
force_constants, ev = numpy.linalg.eigh(hessian)
numpy.savetxt('force_constants.dat', force_constants, fmt='%16.12f')
print(f'Force constants: {force_constants.shape}')
effective_masses = force_constants/omegas
numpy.savetxt('effective_masses.dat', effective_masses, fmt='%12.6f')
print(f'Effective masses: {effective_masses.shape}')
def usage():
print('usage: dymmmatrix.py [DISPLACECAR] [OUTCAR]')
print(' or dymmmatrix.py #DISPLACECAR DISPLACECAR1')
print('DISPLACECAR2 ...')
print(' OUTCAR1 OUTCAR2 OUTCAR3 ...')
if __name__ == '__main__':
from sys import argv
if '-h' in argv:
usage()
exit(0)
if len(argv) == 1:
displacecars = ['DISPLACECAR']
outcars = ['OUTCAR']
elif len(argv) == 3:
displacecars = [argv[1]]
outcars = [argv[2]]
elif len(argv) > 3:
ndisplacecars = int(argv[1])
displacecars = argv[2:2+ndisplacecars]
outcars = argv[2+ndisplacecars:]
else:
usage()
exit(1)
dymmatrix(displacecars, outcars)