-
Notifications
You must be signed in to change notification settings - Fork 9
/
decompose_lo_on_to.py
executable file
·172 lines (152 loc) · 6.69 KB
/
decompose_lo_on_to.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Decompose LO modes eigenvectors on TO modes eigenvectors basis set
#
# Author: Eugene Roginskii
import numpy as np
import phonopy
import re
import yaml
from math import sqrt
import sys
from math import pi
from math import exp
from optparse import OptionParser
import xml.etree.cElementTree as etree
import argparse
parser = argparse.ArgumentParser(description='Decompose LO mode on TO basis')
parser.add_argument("-i", "--input", action="store", type=str, dest="in_fn", help="Input filename")
parser.add_argument("-o", "--output", action="store", type=str, dest="out_fn", help="Output filename")
parser.add_argument("-c", "--calc", action="store", type=str, dest="calc", help="Calculator (abinit,vasp,qe,castep,cp2k)")
parser.add_argument("-f", "--fc", action="store", type=str, dest="fc_fn", default='FORCE_CONSTANTS', help="Force constants filename")
parser.add_argument("-n", "--lonum", action="store", type=int, dest="modenum", help="Number of LO mode, starting from 1")
parser.add_argument("--factor", action="store", type=float, dest="factor",
default=716.8519, help="Frequency factor. Default for cm-1 521.47083 (vasp), 716.8519 (abinit), 3739.4256800756 (cp2k)")
parser.add_argument("--q-direction", action="store", type=str, dest="nacqdir",
help="Direction of q-vector for non-analytical term")
parser.add_argument("-d", "--dim", action="store", type=str, dest="dim", default="",
help="Supercell Transformation matrix")
parser.add_argument("--irreps", dest="irreps", action="store_true",
default=False, help="Find Irreducible representations and print in input files")
Angst2Bohr=1.889725989
args = parser.parse_args()
if (args.in_fn == None):
print('Error. No input filename was given.')
sys.exit(1)
try:
abinit_fh = open(args.in_fn, 'r')
except IOError:
print("ERROR Couldn't open input file, exiting...\n")
sys.exit(1)
if ( len(args.dim.split()) > 0 ):
if (len(args.dim.split()) > 3):
sc=np.array(float(d) for d in args.dim).reshape(3,3)
else:
sc=np.array([1,0,0,0,1,0,0,0,1]).reshape(3,3)
for i in range(3):
sc[i][i]=float(args.dim.split()[i])
else:
sc=np.array([1,0,0,0,1,0,0,0,1]).reshape(3,3)
pm=np.array([1,0,0,0,1,0,0,0,1]).reshape(3,3)
ph = phonopy.load(supercell_matrix=sc,
primitive_matrix=pm,
unitcell_filename=args.in_fn,
is_nac=True, calculator=args.calc, factor=args.factor,
force_constants_filename=args.fc_fn)
# born_filename='BORN')
basis = ph.get_unitcell().get_cell()
xred = ph.get_unitcell().get_scaled_positions()
natom = len(xred)
masses = ph.get_unitcell().get_masses()
chemel = ph.get_unitcell().get_chemical_symbols()
if (args.irreps):
# Hack to do irrep analysis. Spin order will be restored later
ph.primitive.set_magnetic_moments(None)
# Set IR for Gamma point
ph.set_irreps([0.0,0.0,0.0])
ir_labels = ['N' for i in range(natom*3)]
# print(ph.get_irreps()._get_degenerate_sets()[0])
# print(ir_labelstmp)
for deg_set, ir in zip(ph.get_irreps()._get_degenerate_sets(),
ph.get_irreps()._get_ir_labels()):
# print(deg_set, ir)
for d in deg_set:
if(ir is None):
ir_labels[d] = 'Non'
else:
ir_labels[d] = ir
else:
ir_labels=["" for x in range(natom*3)]
#print(ir_labels)
try:
out_fh = open(args.out_fn, 'w')
except IOError:
print("ERROR Couldn't open output file for writing, exiting...\n")
sys.exit(1)
ph.run_qpoints([[0, 0, 0]], with_eigenvectors=True)
eigens_to = ph.get_qpoints_dict()['eigenvectors'][0].real
freqs_to = ph.get_qpoints_dict()['frequencies'][0]
#Debug TO eigens and orthogonality of TO basis
#
#for i in range(natom*3):
# print("Freq % 9.4f" % freqs_to[i])
# for j in range(natom):
# print(''.join('% 9.6f ' % eigens_to[:,i][j*3+k] for k in range(3)))
# weights=np.dot(eigens_to[:,i], eigens_to)
# for j in range(len(weights)):
# print("% 4.2f % 9.5f %s" % (weights[j], freqs_to[j], ir_labels[j]))
ph.run_qpoints([[0, 0, 0]], nac_q_direction=[float(l) for l in args.nacqdir.split()], with_eigenvectors=True, with_dynamical_matrices=True)
eigens_lo = ph.get_qpoints_dict()['eigenvectors'][0].real
freqs_lo = ph.get_qpoints_dict()['frequencies'][0]
#for i in range(natom*3):
# print(eigens_lo[:,i])
#Debug treat all LO modes
#for i in range(natom*3):
# print("Freq % 9.4f" % freqs_lo[i])
# weights=np.dot(eigens_lo[:,i], eigens_to)
# for j in range(natom):
# print(''.join('% 8.6f ' % eigens_lo[:,i][3*j+k] for k in range(3)))
# for j in range(len(weights)):
# print("% 4.2f % 9.5f %s" % (weights[j], freqs_to[j], ir_labels[j]))
try:
out_fh = open(args.out_fn, 'w')
except IOError:
print("ERROR Couldn't open output file for writting.")
sys.exit(1)
# Single mode
if(args.modenum):
out_fh.write("Mode number %d Freq % 9.4f\n" % (args.modenum, freqs_lo[args.modenum-1]))
out_fh.write("Atomic displacements (Cartesian coordinates):\n")
weights = np.dot(eigens_lo[:,args.modenum-1], eigens_to)
for j in range(natom):
out_fh.write(''.join('% 8.6f ' % eigens_lo[:,args.modenum-1][3*j+k] for k in range(3)))
out_fh.write('\n')
out_fh.write("Decomposition:\n")
for j in range(len(weights)):
out_fh.write("% 4.2f % 10.4f %s\n" % (weights[j], freqs_to[j], ir_labels[j]))
else:
# Proceed all modes
for n in range(natom*3):
out_fh.write("Mode number %d Freq % 9.4f\n" % ((n+1), freqs_lo[n]))
out_fh.write("Atomic displacements (Cartesian coordinates):\n")
weights = np.dot(eigens_lo[:,n], eigens_to)
for j in range(natom):
out_fh.write(''.join('% 8.6f ' % eigens_lo[:,n][3*j+k] for k in range(3)))
out_fh.write('\n')
out_fh.write("Decomposition:\n")
for j in range(len(weights)):
out_fh.write("% 4.2f % 10.4f %s\n" % (weights[j], freqs_to[j], ir_labels[j]))