-
Notifications
You must be signed in to change notification settings - Fork 9
/
scan_alnog_mode_abinit.py
executable file
·563 lines (494 loc) · 18.1 KB
/
scan_alnog_mode_abinit.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#!/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/>.
#
# The program to distort geometry along selected normal mode eigenvector. Abinit input format
#
#
# Author: Eugene Roginskii
#
# Some code was borrowed from very powerfool PHONOPY project
# http://phonopy.sourceforge.net/
# Copyright 2009, Atsushi Togo.
def chunks(l, n):
if n < 1:
n = 1
return [l[i:i + n] for i in range(0, len(l), n)]
def strType(var):
try:
if int(var) == float(var):
return 'int'
except:
try:
float(var)
return 'float'
except:
return 'str'
def isNumeric(var):
isnum=strType(var)
if (isnum == 'int' or isnum == 'float'):
return (True)
return(False)
def direct2cart(directpos,basis):
cart=[]
for atdirect in directpos:
cart.append(np.dot(np.transpose(basis),atdirect))
return np.array(cart)
def genabinit(abinitfn,modenum,basis,natom,typat,znucl,cartshiftd,comment):
abinit_fh=open(abinitfn,'w')
abinit_fh.write('#Atom positions shifted along Mode %d\n' % modenum)
abinit_fh.write('# %s\n' % comment)
abinit_fh.write('acell 1.0 1.0 1.0\nrprim\n')
for b in basis:
abinit_fh.write('% 20.16f % 20.16f % 20.16f\n' % (b[0], b[1], b[2]))
abinit_fh.write('typat ' + ' '.join('%d' % typat[i] for i in range(natom))+'\n')
abinit_fh.write('znucl ' + ' '.join('%d' % znucl[i] for i in range(len(znucl)))+'\n')
abinit_fh.write('natom %d\n' % natom)
abinit_fh.write('ntypat %d\n' % len(znucl))
abinit_fh.write('xcart\n')
for cartat in cartshiftd:
abinit_fh.write('%12.9f %12.9f %12.9f\n' % (cartat.tolist()[0], cartat.tolist()[1], cartat.tolist()[2]))
def get_epsilon(abinitfn):
e=[]
try:
abinit_fh = open(abinitfn, 'r')
except IOError:
print ("ERROR Couldn't open abinit output file %s, exiting...\n" % abinitfn)
sys.exit(1)
while True:
line=abinit_fh.readline()
if not line: break
if 'Dielectric tensor, in cartesian coordinates' in line:
while True:
sline=abinit_fh.readline()
if not sline: break
if 'Effective charges' in sline: break
if (re.match('\s*\d+\s*\d+',sline)):
e.append(float(sline.split()[4]))
break
eps=np.array(e).reshape(3,3)
return eps
import numpy as np
import re
from math import sqrt
import sys
from math import pi
from math import exp
from shutil import move
import os
import datetime
import time
import argparse
import xml.etree.cElementTree as etree
mau=1822.888485
AMU = 1.6605402e-27 # [kg]
# ABINIT freq to cm-1 factor
factorcm=716.851928469
# ABINIT freq to HZ factor
factorHz=21.49068e12
hbar=6.6260695729e-34 #J*s
#hbar=4.13566751691e−15 #eV*s
EV = 1.60217733e-19 # [J]
#
Angst2Bohr=1.889725989
#print sqrt(hbar/AMU/10e12)*10e10 #Angstrom
basedirname='epsilon'
Temp=300
ramanmult=0.0028
atom_data = [
[ 0, "X", "X", 0], # 0
[ 1, "H", "Hydrogen", 1.00794], # 1
[ 2, "He", "Helium", 4.002602], # 2
[ 3, "Li", "Lithium", 6.941], # 3
[ 4, "Be", "Beryllium", 9.012182], # 4
[ 5, "B", "Boron", 10.811], # 5
[ 6, "C", "Carbon", 12.0107], # 6
[ 7, "N", "Nitrogen", 14.0067], # 7
[ 8, "O", "Oxygen", 15.9994], # 8
[ 9, "F", "Fluorine", 18.9984032], # 9
[ 10, "Ne", "Neon", 20.1797], # 10
[ 11, "Na", "Sodium", 22.98976928], # 11
[ 12, "Mg", "Magnesium", 24.3050], # 12
[ 13, "Al", "Aluminium", 26.9815386], # 13
[ 14, "Si", "Silicon", 28.0855], # 14
[ 15, "P", "Phosphorus", 30.973762], # 15
[ 16, "S", "Sulfur", 32.065], # 16
[ 17, "Cl", "Chlorine", 35.453], # 17
[ 18, "Ar", "Argon", 39.948], # 18
[ 19, "K", "Potassium", 39.0983], # 19
[ 20, "Ca", "Calcium", 40.078], # 20
[ 21, "Sc", "Scandium", 44.955912], # 21
[ 22, "Ti", "Titanium", 47.867], # 22
[ 23, "V", "Vanadium", 50.9415], # 23
[ 24, "Cr", "Chromium", 51.9961], # 24
[ 25, "Mn", "Manganese", 54.938045], # 25
[ 26, "Fe", "Iron", 55.845], # 26
[ 27, "Co", "Cobalt", 58.933195], # 27
[ 28, "Ni", "Nickel", 58.6934], # 28
[ 29, "Cu", "Copper", 63.546], # 29
[ 30, "Zn", "Zinc", 65.38], # 30
[ 31, "Ga", "Gallium", 69.723], # 31
[ 32, "Ge", "Germanium", 72.64], # 32
[ 33, "As", "Arsenic", 74.92160], # 33
[ 34, "Se", "Selenium", 78.96], # 34
[ 35, "Br", "Bromine", 79.904], # 35
[ 36, "Kr", "Krypton", 83.798], # 36
[ 37, "Rb", "Rubidium", 85.4678], # 37
[ 38, "Sr", "Strontium", 87.62], # 38
[ 39, "Y", "Yttrium", 88.90585], # 39
[ 40, "Zr", "Zirconium", 91.224], # 40
[ 41, "Nb", "Niobium", 92.90638], # 41
[ 42, "Mo", "Molybdenum", 95.96], # 42
[ 43, "Tc", "Technetium", 0], # 43
[ 44, "Ru", "Ruthenium", 101.07], # 44
[ 45, "Rh", "Rhodium", 102.90550], # 45
[ 46, "Pd", "Palladium", 106.42], # 46
[ 47, "Ag", "Silver", 107.8682], # 47
[ 48, "Cd", "Cadmium", 112.411], # 48
[ 49, "In", "Indium", 114.818], # 49
[ 50, "Sn", "Tin", 118.710], # 50
[ 51, "Sb", "Antimony", 121.760], # 51
[ 52, "Te", "Tellurium", 127.60], # 52
[ 53, "I", "Iodine", 126.90447], # 53
[ 54, "Xe", "Xenon", 131.293], # 54
[ 55, "Cs", "Caesium", 132.9054519], # 55
[ 56, "Ba", "Barium", 137.327], # 56
[ 57, "La", "Lanthanum", 138.90547], # 57
[ 58, "Ce", "Cerium", 140.116], # 58
[ 59, "Pr", "Praseodymium", 140.90765], # 59
[ 60, "Nd", "Neodymium", 144.242], # 60
[ 61, "Pm", "Promethium", 0], # 61
[ 62, "Sm", "Samarium", 150.36], # 62
[ 63, "Eu", "Europium", 151.964], # 63
[ 64, "Gd", "Gadolinium", 157.25], # 64
[ 65, "Tb", "Terbium", 158.92535], # 65
[ 66, "Dy", "Dysprosium", 162.500], # 66
[ 67, "Ho", "Holmium", 164.93032], # 67
[ 68, "Er", "Erbium", 167.259], # 68
[ 69, "Tm", "Thulium", 168.93421], # 69
[ 70, "Yb", "Ytterbium", 173.054], # 70
[ 71, "Lu", "Lutetium", 174.9668], # 71
[ 72, "Hf", "Hafnium", 178.49], # 72
[ 73, "Ta", "Tantalum", 180.94788], # 73
[ 74, "W", "Tungsten", 183.84], # 74
[ 75, "Re", "Rhenium", 186.207], # 75
[ 76, "Os", "Osmium", 190.23], # 76
[ 77, "Ir", "Iridium", 192.217], # 77
[ 78, "Pt", "Platinum", 195.084], # 78
[ 79, "Au", "Gold", 196.966569], # 79
[ 80, "Hg", "Mercury", 200.59], # 80
[ 81, "Tl", "Thallium", 204.3833], # 81
[ 82, "Pb", "Lead", 207.2], # 82
[ 83, "Bi", "Bismuth", 208.98040], # 83
[ 84, "Po", "Polonium", 0], # 84
[ 85, "At", "Astatine", 0], # 85
[ 86, "Rn", "Radon", 0], # 86
[ 87, "Fr", "Francium", 0], # 87
[ 88, "Ra", "Radium", 226.0254], # 88
[ 89, "Ac", "Actinium", 0], # 89
[ 90, "Th", "Thorium", 232.03806], # 90
[ 91, "Pa", "Protactinium", 231.03588], # 91
[ 92, "U", "Uranium", 238.02891], # 92
[ 93, "Np", "Neptunium", 0], # 93
[ 94, "Pu", "Plutonium", 0], # 94
[ 95, "Am", "Americium", 0], # 95
[ 96, "Cm", "Curium", 0], # 96
[ 97, "Bk", "Berkelium", 0], # 97
[ 98, "Cf", "Californium", 0], # 98
[ 99, "Es", "Einsteinium", 0], # 99
[100, "Fm", "Fermium", 0], # 100
[101, "Md", "Mendelevium", 0], # 101
[102, "No", "Nobelium", 0], # 102
[103, "Lr", "Lawrencium", 0], # 103
[104, "Rf", "Rutherfordium", 0], # 104
[105, "Db", "Dubnium", 0], # 105
[106, "Sg", "Seaborgium", 0], # 106
[107, "Bh", "Bohrium", 0], # 107
[108, "Hs", "Hassium", 0], # 108
[109, "Mt", "Meitnerium", 0], # 109
[110, "Ds", "Darmstadtium", 0], # 110
[111, "Rg", "Roentgenium", 0], # 111
[112, "Cn", "Copernicium", 0], # 112
[113, "Uut", "Ununtrium", 0], # 113
[114, "Uuq", "Ununquadium", 0], # 114
[115, "Uup", "Ununpentium", 0], # 115
[116, "Uuh", "Ununhexium", 0], # 116
[117, "Uus", "Ununseptium", 0], # 117
[118, "Uuo", "Ununoctium", 0], # 118
]
parser = argparse.ArgumentParser(description='The program to distort geometry along selected normal mode eigenvector. Abinit input format')
parser.add_argument("-i", "--input", action="store", type=str, dest="abinit_fn", help="Abinit Input filename")
parser.add_argument("-d", "--dynmat", action="store", type=str, dest="dynmat_fn", default='qpoints.yaml', help="Dynmat in yaml format filename")
parser.add_argument("-m", "--mode", action="store", type=int, dest="modenum", default=1, help="Number of mode")
parser.add_argument("-a", "--amplitudes", action="store", type=str, dest="amplstr", default='0.1 0.25 0.5 1',
help="Mode shift amplitude, space separated")
args = parser.parse_args()
if (args.abinit_fn == None):
print('Error. No input filename was given.')
sys.exit(1)
try:
abinit_fh = open(args.abinit_fn, 'r')
except IOError:
print("ERROR Couldn't open abinit input file, exiting...")
sys.exit(1)
try:
qpoints_fh = open(args.dynmat_fn, 'r')
except IOError:
print("ERROR Couldn't open qpoints file, exiting...")
sys.exit(1)
ampl=[]
for i in range(len(args.amplstr.split())):
if isNumeric(args.amplstr.split()[i]):
ampl.append(float(args.amplstr.split()[i]))
else:
print('Error parsing amplitudes array')
sys.exit(1)
modenum=args.modenum
natom=0
for line in qpoints_fh:
if 'natom' in line:
natom=int(line.split(':')[1])
print (natom*3)
break
for line in qpoints_fh:
if 'dynamical_matrix' in line:
break
i=0
dynmatarray=[]
for line in qpoints_fh:
if (i>=natom*3):
break
line=re.sub('^\s+-?\s*\[\s*', '', line)
line=re.sub('\s*\]\s*', '', line)
line=line.split(',')
k=0
dynmatline=chunks(line,2)
for dynmatelem, idynmatelem in dynmatline:
dynmatarray.append(float(dynmatelem))
k+=1
i+=1
dm=np.array(dynmatarray).reshape(natom*3,k)
dm=dm.transpose()
eigvals, eigvecs = np.linalg.eigh(dm)
eigvals = eigvals.real
frequencies=[]
frequencies=np.sqrt(np.abs(eigvals)) * np.sign(eigvals)
# abinit input file process
acell=[]
rprim=[1,0,0, 0,1,0, 0,0,1]
typat=[]
znucl=[]
scalecart=[1.0,1.0,1.0]
while True:
line=abinit_fh.readline()
if not line: break
if (re.match('^\s*#',line)):
continue
if '#' in line:
line=re.sub('\s*#.*','',line)
if 'natom' in line:
if(natom != int(line.split()[1])):
print('Error. Number of atoms in qpoints file and in abinit input file differ')
sys.exit(1)
elif 'ntypat' in line:
ntypat=int(line.split()[1])
elif 'typat' in line:
if(re.match('^\s*typat\s+\d',line) is None):
line=abinit_fh.readline()
if(re.match('^\s*\d',line) is None):
print('Error reading typat variable in abinit input file')
sys.exit(1)
else:
typat=[int(line.split()[i]) for i in range(natom)]
else:
typat=[int(line.split()[i+1]) for i in range(natom)]
elif 'znucl' in line:
znuclstr=line.split()[1:]
znucl=[int(znuclstr[i]) for i in range(len(znuclstr)) ]
print('znucl= %s' % znucl)
elif 'acell' in line:
if re.match('.*acell.*\d+\s*A.*',line):
acell=[float(line.split()[i+1])*Angst2Bohr for i in range(3)]
else:
acell=[float(line.split()[i+1]) for i in range(3)]
print('acell = %s' % acell)
elif 'rprim' in line:
rprim=[]
try:
for coor in line.split()[1:]:
rprim.append(float(coor))
while True:
subline=abinit_fh.readline()
if not subline: break
if (re.match('^\s*#',subline)):
continue
if (re.match('^\s*$',subline)):
continue
if((re.match('^\s*-?\d*\.',subline) is None) and (re.match('^\s*-?\d',subline) is None)):
break
else:
for coor in subline.split():
rprim.append(float(coor))
if (len(rprim) >= 9):
break
except:
while True:
subline=abinit_fh.readline()
if not subline: break
if (re.match('^\s*#',subline)):
continue
if (re.match('^\s*$',subline)):
continue
if((re.match('^\s*-?\d*\.',subline) is None) and (re.match('^\s*-?\d',subline) is None)):
break
else:
for coor in subline.split():
rprim.append(float(coor))
if (len(rprim) >= 9):
break
elif 'scalecart' in line:
scalecart=[float(line.split()[i+1]) for i in range(3)]
print('rprim = %s' % rprim)
print('natom = %d' % natom)
print('typat = %s' % typat)
# Start from begining to read xred xcart xangs
abinit_fh.seek(0)
xred=[]
xcart=[]
for line in abinit_fh:
if (re.match('^\s*#',line)):
continue
if '#' in line:
line=re.sub('\s*#.*','',line)
if 'xred' in line:
try:
for coor in line.split()[1:]:
xred.append(float(coor))
for subline in abinit_fh:
if (re.match('^\s*#',subline)):
continue
if (re.match('^\s*$',subline)):
continue
if((re.match('^\s*-?\d*\.',subline) is None) and (re.match('^\s*-?\d',subline) is None)):
break
else:
for coor in subline.split():
xred.append(float(coor))
except:
for subline in abinit_fh:
if (re.match('^\s*#',subline)):
continue
if (re.match('^\s*$',subline)):
continue
if((re.match('^\s*-?\d*\.',subline) is None) and (re.match('^\s*-?\d',subline) is None)):
break
else:
for coor in subline.split():
xred.append(float(coor))
elif 'xcart' in line:
try:
for coor in line.split()[1:]:
xcart.append(float(coor))
for subline in abinit_fh:
if (re.match('^\s*#',subline)):
continue
if (re.match('^\s*$',subline)):
continue
if((re.match('^\s*-?\d*\.',subline) is None) and (re.match('^\s*-?\d',subline) is None)):
break
else:
for coor in subline.split():
xcart.append(float(coor))
except:
for subline in abinit_fh:
if (re.match('^\s*#',subline)):
continue
if (re.match('^\s*$',subline)):
continue
if((re.match('^\s*-?\d*\.',subline) is None) and (re.match('^\s*-?\d',subline) is None)):
break
else:
for coor in subline.split():
xcart.append(float(coor))
elif 'xangs' in line:
try:
for coor in line.split()[1:]:
xcart.append(float(coor)*Angst2Bohr)
for subline in abinit_fh:
if (re.match('^\s*#',subline)):
continue
if (re.match('^\s*$',subline)):
continue
if((re.match('^\s*-?\d*\.',subline) is None) and (re.match('^\s*-?\d',subline) is None)):
break
else:
for coor in subline.split():
xcart.append(float(coor)*Angst2Bohr)
except:
for subline in abinit_fh:
if (re.match('^\s*#',subline)):
continue
if (re.match('^\s*$',subline)):
continue
if((re.match('^\s*-?\d*\.',subline) is None) and (re.match('^\s*-?\d',subline) is None)):
break
else:
for coor in subline.split():
xcart.append(float(coor)*Angst2Bohr)
if len(rprim) != 9:
print('Error reading rprim array')
sys.exit(1)
rprimd=[]
for i in range(3):
# print([scalecart[1],acell[i],rprim[i*3+1]])
rprimd.append([scalecart[k]*rprim[i*3+k]*acell[i] for k in range(3)])
basis=np.array(rprimd).reshape(3,3)
# Masses aray generation on znucl and typat data
masses=[]
for i in range (natom):
masses.append(float(atom_data[znucl[typat[i]-1]][3]))
cvol=np.dot(basis[0],np.cross(basis[1],basis[2]))
if len(xred) != natom*3:
if len(xcart) != natom*3:
print('Error reading atom coordinate array')
print('xred/xcart')
print(xred)
print(xcart)
sys.exit(1)
else:
cartpos=np.array(xcart).reshape(natom,3)
else:
directpos=np.array(xred).reshape(natom,3)
cartpos=direct2cart(directpos,basis)
# GENERATION OF DISPLACEMENTS ABINIT INPUT FILES
#j - mode number; i -atom number
j=args.modenum-1
for n in range(len(ampl)):
cartshiftdm=[]
cartshiftdp=[]
print ('mode: %d freq: %8.5f' % ((j+1),frequencies[j] * factorcm))
print('====eigenvector:====')
for i in range(natom):
print(" ".join('%9.7f' % eigvecs[i*3+l,j] for l in range(3)))
print('delta = %f' % ampl[n])
print('shiftvector:')
for i in range(natom):
shiftvec=[0.0e0,0.0e0,0.0e0]
for l in range(3):
shiftvec[l]=eigvecs[i*3+l,j]*ampl[n]*sqrt(hbar/(AMU*masses[i]*abs(frequencies[j])*factorHz))*1e9*Angst2Bohr
cartshiftdm.append(cartpos[i]-np.array(shiftvec))
cartshiftdp.append(cartpos[i]+np.array(shiftvec))
abinitfn="shiftcell-%.3f.in" % ampl[n]
genabinit(abinitfn,j+1,basis,natom,typat,znucl,cartshiftdm,'freq = %9.4f cm-1; Delta=%6.4f' % ((frequencies[j] * factorcm),ampl[n]))