-
Notifications
You must be signed in to change notification settings - Fork 0
/
gwienfile.py
1398 lines (1278 loc) · 67.3 KB
/
gwienfile.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# @Copyright 2020 Kristjan Haule
'''
Classes to handle reading of WIEN2K files.
'''
import sys, re, os, operator
from numpy import * #zeros,arange,array,identity
#from scipy import *
from scipy import linalg
import rdVec
from functools import reduce
def vectorso_exists(case):
filename = env.SCRATCH+"/"+case+".vectorso"
return os.path.isfile(filename) and os.path.getsize(filename) > 0
def in1c_exists(case):
filename = case+".in1c"
return os.path.isfile(filename) and os.path.getsize(filename) > 0
class Struct:
def __init__(self, case_file, fh_info=sys.stdout):
self.case_file = case_file
self.parse()
self.w2kCorrect()
def parse(self):
'''The file is structured for reading by fortran code,
so data is positioned by line and by column.'''
f = open(self.case_file + '.struct', 'r')
self.title = next(f).strip()
line = next(f)
self.lattice = line[0:4].strip()
self.nat = int(line[27:30])
self.latticename = line[30:].strip()
self.mode = next(f)[13:17]
line = next(f)
self.a, self.b, self.c, self.alpha, self.beta, self.gamma = [float(line[i:i+10]) for i in range(0,60,10)]
self.iatom = []
self.mult = []
self.isplit = []
self.aname = []
self.nrpt = []
self.r0 = []
self.rmt = []
self.Znuc = []
self.pos = []
self.rotloc = []
self.iatnr = [] # iatnr[iat]>0 => cubic, iatnr[iat]<0 => non-cubic
for iat in range(self.nat):
line = next(f)
self.iatnr.append( int(line[4:8]) )
pos = [[float(line[col:col+10]) for col in 12+13*arange(3)]]
line = next(f)
mult = int(line[15:17])
self.mult.append(mult)
self.isplit.append( int(line[34:37]) )
for mu in range(mult-1):
line = next(f)
pos.append( [float(line[col:col+10]) for col in 12+13*arange(3)] )
self.pos.append(pos)
line = next(f)
self.aname.append( line[0:10].strip() )
self.nrpt.append( int(line[15:20]) )
self.r0.append( float(line[25:35]) )
self.rmt.append( float(line[40:50]) )
self.Znuc.append( int(float(line[55:65])) )
#rmt[iat] = self.r0[iat]*exp(dh[iat]*(self.nrpt[iat]-1)) # calc. the muffin tin radius
rt=[]
for i in range(3):
line = next(f)
rt.append( [float(line[col:col+10]) for col in 20+10*arange(3)] )
self.rotloc.append(array(rt).T)
self.aZnuc=[]
for iat in range(self.nat):
for mu in range(self.mult[iat]):
self.aZnuc.append(self.Znuc[iat])
line = next(f)
dt = line.split()
if len(line)<2 or dt[1][:6]!='NUMBER':
f.close()
return
self.Nsym = int(dt[0])
self.timat = zeros((self.Nsym,3,3),dtype=int) # it is transpose compared to w2k fortran convention
self.tau = zeros((self.Nsym,3))
for isym in range(self.Nsym):
for j in range(3):
line = next(f)
#print int(line[0:2]),int(line[2:4]),int(line[4:6]),float(line[6:16])
self.timat[isym,j,:] = [int(line[0:2]),int(line[2:4]),int(line[4:6])]
self.tau[isym,j] = float(line[6:16])
#print self.timat[isym,:,:]
#print self.tau[isym]
ii = int(next(f))
if (ii != isym+1):
print('WARNING : issues with reading symmetry operations in struct file at isym=', isym+1, 'ii=', ii)
f.close()
return
f.close()
if self.mode == 'RELA':
self.rel = True
elif self.mode == 'NREL':
self.rel = False
else:
print('ERROR : struct file '+strc.case_file+'.struct seems to have wrong mode '+strc.mode)
#print >> fout, 'RELA=', strc.mode
# create array of pos, which is list of lists, and not necessary equal length
#n_eqm = max([len(self.pos[iat]) for iat in range(self.nat)])
#self.vpos = zeros((self.nat,n_eqm,3))
#for iat in range(len(self.pos)):
# ne = len(self.pos[iat])
# self.vpos[iat,:ne,:] = strc.pos[iat][:]
self.mult = array(self.mult)
self.rotloc = array(self.rotloc)
ndf = sum(self.mult)
self.vpos = zeros((3,ndf),order='F')
idf = 0
for iat in range(self.nat):
for ieq in range(self.mult[iat]):
self.vpos[:,idf] = self.pos[iat][ieq][:]
idf += 1
# We here rearange symmetry operations such that idenity appears first. This is just for the convenience
if sum(abs(self.timat[0,:,:]-identity(3)))>0 or sum(abs(self.tau[0,:]))>0:
#print 'Idenity did not appear first in the structure file. We will rearange symmetry operations so that identity is first'
iisym = None
Id = identity(3)
for isym in range(len(self.timat)):
if sum(abs(self.timat[isym,:,:]-Id))==0 and sum(abs(self.tau[0,:]))==0:
iisym = isym
break
#print 'Idenity found at place', iisym
if iisym is None:
print('ERROR : we could not find Idenity in the list of symmetries')
else:
self.timat[0,:,:], self.timat[iisym,:,:] = copy(self.timat[iisym,:,:]), copy(self.timat[0,:,:])
self.tau[0,:], self.tau[iisym,:] = copy(self.tau[iisym,:]), copy(self.tau[0,:])
#print self.timat[0,:,:], self.timat[iisym,:,:]
def w2kCorrect(self):
if self.alpha == 0:
self.alpha = 90.0
if self.beta == 0:
self.beta = 90.0
if self.gamma == 0 :
self.gamma = 90.0
if self.lattice.strip() == 'H':
self.gamma = 120.
def debugprint(self, f=sys.stdout):
print('***** Structure File Contents *****', file=f)
print('title =', self.title, file=f)
print('Number of sorts, nat =', self.nat, file=f)
print('unit cell parameters (a,b,c) =', self.a, self.b, self.c, file=f)
print('angles (alpha, beta, gamma) =', self.alpha, self.beta, self.gamma, file=f)
printlist = [
(self.aname, 'Atom name, aname ='),
(self.iatnr, 'Atom index, cubic/noncubic environment ='),
(self.mult, 'Number of atoms of this type, mult ='),
(self.isplit, 'Symmetry of the atom, isplit ='),
(self.nrpt, 'Number of radial points, npt ='),
(self.r0, 'First point in radial mesh, r0 ='),
(self.rmt, 'Muffin tin radius, rmt ='),
(self.Znuc, 'Atomic number, Znuc ='),
]
for i in range(self.nat):
print('---------- atom type', i, '------------', file=f)
for var,docstr in printlist:
print(docstr, var[i], file=f)
print('Position(s) inside the unit cell:', file=f)
for m in range(self.mult[i]):
print(' pos =', self.pos[i][m], file=f)
print('Local rotation matrix:', file=f)
for row in self.rotloc[i,:,:]:
print(row, file=f)
#print >> f, '***********************************'
def flat(self, notflat):
'''Return a flat view of given data as a list.
Example: if w.mult = [2,4] and w.aname = ['V', 'O']
w.flatten(w.aname) -> ['V', 'V', 'O', 'O', 'O', 'O']'''
if notflat is self.pos:
listoflists = self.pos
else:
listoflists = [[elem]*mult for elem,mult in zip(notflat, self.mult)]
return reduce(operator.add, listoflists)
def radial_mesh(self, iat):
npt = self.nrpt[iat]
dh = log(self.rmt[iat]/self.r0[iat])/(npt - 1) # logarithmic step for the radial mesh
dd = exp(dh)
return( self.r0[iat]*dd**list(range(npt)), dh, npt )
class Latgen:
"""
Given wien2k structure file creates lattice BR2 and gbas, rbas, Volume
"""
def __init__(self, strc, fout):
self.pia = array([2.0*pi/strc.a, 2.0*pi/strc.b, 2.0*pi/strc.c])
self.alpha = [strc.alpha*pi/180., strc.beta*pi/180., strc.gamma*pi/180.]
self.ortho = False
self.br2 = zeros((3,3))
if strc.lattice[:1]=='H': # hexagonal
print('hexagonal lattice', file=fout)
self.br2[0,0] = 2.0/sqrt(3.0)
self.br2[0,1] = 1.0/sqrt(3.0)
self.br2[1,1] = 1.0
self.br2[2,2] = 1.0
self.rvfac = 2.0/sqrt(3.0)
self.ortho = False
for j in range(3):
self.br2[j,:] *= self.pia[j]
elif strc.lattice[:1] in ['S','P']: # primitive or simple
print('primitive or simple lattice', file=fout)
self.ortho = True
for i in range(3):
if( abs(self.alpha[i]-pi/2.) > 0.0001):
print('alpha['+str(i)+'] not equal 90', file=fout)
self.ortho = False
# includes triclinic, monoclinic, simple orthorhombic, tetragonal and cubic
sinbc = sin(self.alpha[0])
cosab = cos(self.alpha[2])
cosac = cos(self.alpha[1])
cosbc = cos(self.alpha[0])
wurzel=sqrt(sinbc**2-cosac**2-cosab**2+2*cosbc*cosac*cosab)
self.br2[0,0]= sinbc/wurzel*self.pia[0]
self.br2[0,1]= (-cosab+cosbc*cosac)/(sinbc*wurzel)*self.pia[1]
self.br2[0,2]= ( cosbc*cosab-cosac)/(sinbc*wurzel)*self.pia[2]
self.br2[1,1]= self.pia[1]/sinbc
self.br2[1,2]= -self.pia[2]*cosbc/sinbc
self.br2[2,2]= self.pia[2]
self.rvfac = 1.0/wurzel
elif strc.lattice[:1] == 'F': # face centered
print('face centered lattice', file=fout)
self.br2[0,0] = -1.0
self.br2[1,0] = 1.0
self.br2[2,0] = 1.0
self.br2[0,1] = 1.0
self.br2[1,1] = -1.0
self.br2[2,1] = 1.0
self.br2[0,2] = 1.0
self.br2[1,2] = 1.0
self.br2[2,2] = -1.0
self.rvfac = 4.0
self.ortho = True
for j in range(3):
self.br2[j,:] *= self.pia[j]
elif strc.lattice[:1] == 'B': # body centered
print('body centered lattice', file=fout)
self.br2[0,0] = 0.0
self.br2[1,0] = 1.0
self.br2[2,0] = 1.0
self.br2[0,1] = 1.0
self.br2[1,1] = 0.0
self.br2[2,1] = 1.0
self.br2[0,2] = 1.0
self.br2[1,2] = 1.0
self.br2[2,2] = 0.0
self.rvfac = 2.0
self.ortho = True
for j in range(3):
self.br2[j,:] *= self.pia[j]
elif strc.lattice[:1] == 'R': # rhombohedral
print('rhombohedral lattice', file=fout)
self.br2[0,0] = 1.0/sqrt(3.0)
self.br2[0,1] = 1.0/sqrt(3.0)
self.br2[0,2] = -2.0/sqrt(3.0)
self.br2[1,0] = -1.0
self.br2[1,1] = 1.0
self.br2[2,0] = 1.0
self.br2[2,1] = 1.0
self.br2[2,2] = 1.0
self.rvfac = 6.0/sqrt(3.0)
self.ortho = False
for j in range(3):
self.br2[j,:] *= self.pia[j]
elif strc.lattice[:1] == 'C': # base centered
print('base centered lattice', file=fout)
if strc.lattice[1:3] == 'XZ':
ix=0
iy=1
iz=2
elif strc.lattice[1:3] == 'YZ':
ix=1
iy=0
iz=2
elif strc.lattice[1:3] == 'XY':
ix=0
iy=2
iz=1
if( abs(self.alpha[iz]-pi/2.0) < 0.0001 ):
# orthorombic case
self.br2[ix,ix] = 1.0
self.br2[ix,iz] = 1.0
self.br2[iy,iy] = 1.0
self.br2[iz,ix] = -1.0
self.br2[iz,iz] = 1.0
self.rvfac = 2.0
self.ortho = True
for j in range(3):
self.br2[j,:] *= self.pia[j]
else:
# monoclinic case
print('alpha['+str(iz)+'] not equal 90 degrees')
sinab = sin(self.alpha[iz])
cosab = cos(self.alpha[iz])
self.br2[ix,ix] = self.pia[ix]/sinab
self.br2[ix,iy] = -self.pia[iy]*cosab/sinab
self.br2[ix,iz] = self.pia[ix]/sinab
self.br2[iy,iy] = self.pia[iy]
self.br2[iz,ix] = -self.pia[iz]
self.br2[iz,iz] = self.pia[iz]
self.rvfac = 2.0/sinab
self.ortho= False
else:
print('ERROR wrong lattice=', strc.lattice, file=fout)
sys.exit(1)
# define inverse of cellvolume
vi = self.rvfac/ (strc.a * strc.b * strc.c)
self.Vol = 1./vi
# Calculate the basis vectors of the real lattice
self.gbas = self.br2[:,:] / (2.0*pi)
self.rbas = linalg.inv(self.gbas)
for i in range(3):
for j in range(3):
if abs(self.rbas[i,j])<1e-14:
self.rbas[i,j]=0
print('Ortho=', self.ortho, file=fout)
print('BR2=', self.br2, file=fout)
print('Unit cell volume=', self.Vol, file=fout)
print('gbas=', self.gbas, file=fout)
print('rbas=', self.rbas, file=fout)
self.Rotdef( strc, fout)
self.Symoper( strc, fout)
def Rotdef(self, strc, fout):
""" define rotation matrices if required
Finds the symmetry operations, which transform equivalent
atoms into each other
the operation rotij[idf,3,3], tauij[idf,3] transforms
a position of a "not equivalent" atom to the position of
a corresponding equivalent atom (idf).
"""
Nat_all = sum([len(strc.pos[i]) for i in range(len(strc.pos))])
#print 'Nat_all=', Nat_all
self.trotij = zeros((Nat_all,3,3))
self.tauij = zeros((Nat_all,3))
self.rotij = zeros((Nat_all,3,3)) # old fashioned rotij like in dmft1
nnat = 0
for iat in range(strc.nat):
poss = strc.pos[iat]
pos0 = poss[0]
for ieq in range(len(poss)):
cpos = poss[ieq]
#print >> fout, iat, ieq, cpos
Found_sym = False
for isym in range(strc.Nsym):
sym_pos = dot(strc.timat[isym,:,:],pos0)
sym_pos += strc.tau[isym,:]
for i in range(3):
sym_pos[i] = (sym_pos[i] % 1.0)
delta_pos = abs(sym_pos[:]-cpos[:])
#print >> fout, iat, ieq, isym, 'delta_pos=', delta_pos
if sum(delta_pos) < 3e-4:
Found_sym = True
break
# Not done yet, some lattices require half translation
# Check now for centered lattices
delta_cpos = array([(delta_pos[i]+0.5) % 1.0 for i in range(3)])
if strc.lattice[:1]=='B': # bc
if sum(abs(delta_cpos)) < 3e-4:
Found_sym = True
break
elif strc.lattice[:1]=='F': # fc
delta = delta_cpos[:]
for i in range(3):
delta[i] = delta_pos[i]
if sum(abs(delta)) < 3e-4:
Found_sym = True
break
if Found_sym:
break
elif strc.lattice[:1]=='C':
if strc.lattice[1:3]=='XY':
delta = array( [delta_cpos[0], delta_cpos[1], delta_pos[2] ] )
elif strc.lattice[1:3]=='XZ':
delta = array( [delta_cpos[0], delta_pos[1], delta_cpos[2] ] )
elif strc.lattice[1:3]=='YZ':
delta = array( [delta_pos[0], delta_cpos[1], delta_cpos[2] ] )
if sum(abs(delta)) < 3e-4:
Found_sym = True
break
if Found_sym:
self.trotij[nnat,:,:] = strc.timat[isym,:,:]
self.tauij[nnat,:] = strc.tau[isym,:]
else:
print("ERROR in rotdef: no sym operation found for iat=",iat,"ieq=",ieq,"idf",nnat, file=fout)
print("ERROR in rotdef: no sym operation found for iat=",iat,"ieq=",ieq,"idf",nnat)
sys.exit(1)
# correct_rotij
# Redefines local rotation matix from struct-file with
# unitary transformation u(-1) * S * u for non-orthogonal lattice
#if not self.ortho and strc.lattice[1:3]!='CXZ':
self.rotij[nnat,:,:] = copy(self.trotij[nnat,:,:].T)
if not( self.ortho or strc.lattice[1:3]=='CXZ' ):
# Note in dmft1 and lapw2 we used : rotij_cartesian = BR1 * rotij * BR1inv^{-1}
# but here we use the convention as in lapw1 : rotij_cartesian = BR2 * rotij * ((BR2^T)^{-1})^T
# Note also that in this way we rotate real space vectors, not momentum space vectors. The latter are rotated with inverse of BR2 matrices (see Symoper)
# Here kqm.k2cartes = latgen.br2== BR2
# This is like rtij = BR2 * troij[].T * BR2^{-1}, therefore rtij*kqm.k2cartes = latgen.br2 * troij[].T
# This means that we can first transform k_semicartesian to cartesian, and only later use trotij.
rtij = transpose( dot( dot(self.gbas,self.trotij[nnat,:,:].T), self.rbas ) )
self.trotij[nnat,:,:] = rtij
else:
# This is something that is not done in lapw1, and I am not sure if it is needed, but mathematically it makes sense
# Note that here we use rotij in different way than dmft1.
# Normally, we use BR2*rotij*k_semicartes or k2cartes*rotij*k_semicartes, but here we change
# rotij so that we will use trotij.T*k2cartes*k or (k2cartes*k).trotij
# hence we can first transform to cartesian coordinates, and rotij then works in cartesian coordinates
aaa = array([strc.a, strc.b, strc.c])
k2cartes = 2*pi/aaa*identity(3)
k2cartes_1 = aaa/(2*pi)*identity(3)
rtij = dot(dot(k2cartes, self.trotij[nnat,:,:].T), k2cartes_1).T
#print('k2cartes=', k2cartes, file=fout)
#print('k2cartes_1=', k2cartes_1, file=fout)
#print('rotij=', self.trotij[nnat,:,:].T, file=fout)
#print('dmft1-rotij=', self.rotij[nnat,:,:], file=fout)
#print('rtij=', rtij , file=fout)
self.trotij[nnat,:,:] = rtij
print('For atom '+strc.aname[iat]+' ieq='+str(ieq)+' at pos=',cpos, 'found tauij=', self.tauij[nnat,:], 'rotij.T=', (self.trotij[nnat,:,:]).tolist(), file=fout)
nnat += 1
def Symoper(self, strc, fout):
""" Redefine symmetry matrices such that they will work in lattice system
rather than cartesian system.
Example: bcc structure. We will use 1BZ k-point in lattice vectors written as k_l=(i/N, j/N, k/N).
For bcc structure the same k-point in cartesian coordinates should be k_c=( (i+j)/N, (i+k)/N, (j+k)/N )
For bcc structure BR2 = [[0,1,1],[1,0,1],[1,1,0]], hence
BR2*k_l = k_c
We could apply symmetry operations on k_c as R*k_c. Alternatively, we can use
(BR2)^{-1}*R*BR2*k_l = rbas*R*gbas
"""
if self.ortho: # I suspect that here we should have "or strc.lattic[:3]=='CXZ'", hence I suspect that this does not work for CXZ!
# If the system is orthogonal, than we can work with integer k-vectors even in lattice coordinate system
# and we do not need to work in cartesian.
self.tizmat = zeros(shape(strc.timat),dtype=int) # this is like timat matrix, but tizmat is used in integer representation, while timat has to be used only in semi-cartesian coordinates.
self.iztau = zeros(shape(strc.tau),dtype=float)
for isym in range(strc.Nsym):
#ttmp = round_(transpose( dot( dot(self.rbas, strc.timat[isym,:,:].T), self.gbas ) ))
ttmp = (transpose( dot( dot(self.rbas, strc.timat[isym,:,:].T), self.gbas ) )).round()
self.tizmat[isym,:,:] = ttmp[:,:]
self.iztau[isym,:] = dot(self.rbas,strc.tau[isym,:])
# Note that this is very different than in dmft1 or dmft2
else:
# we are forced to stary in cartesian system
#print 'WARNING : Check Symoper if it really works. I think it does not'
self.tizmat = copy(strc.timat)
class In1File:
def __init__(self, case_file, strc, fout, lomax=-1):
if in1c_exists(case_file):
fin1 = open(case_file+'.in1c','r')
else:
fin1 = open(case_file+'.in1', 'r')
lines = fin1.readlines()
if (lomax<0):
# In origonal wien2k lomax is seto to 3, but the GW part changes that to lomax=10
# Here we determine from in1 file what might lomax be, but be careful because in initializing GW we usualy set lomax->10
# This has consequence for reading case.energy file, to find energy for radial functions. They are stored in backes of lomax.
# first pass through the file to determine lomax
lomax = 3
iread = 2
for iat in range(strc.nat):
line_dat = lines[iread].split()
iread += 1
Ei0, nlr, iapw = float(line_dat[0]), int(line_dat[1]), int(line_dat[2]) # default parameters ei,iapw and number of exceptions nlr
dat=[]
for j in range(nlr):
line_dat = lines[iread].split()
iread += 1
dat.append( [int(line_dat[0]), float(line_dat[1]), float(line_dat[2]), line_dat[3], int(line_dat[4])] )
for j in range(nlr):
l, iapw = dat[j][0], dat[j][4]
l_prev = dat[j-1][0] if j>0 else -1
if l==l_prev or iapw==1: # LO or lo
if l==l_prev:
print('lomax increased to', lomax, 'because l='+str(l)+' repeated', file=fout)
if iapw==1:
print('lomax increased to', lomax, 'because l='+str(l)+' has APW+lo', file=fout)
if l >= lomax:
lomax = l+1
print('lomax set to', lomax, ' make sure that this is compatible with wien2k case.energy file', file=fout)
Ef = 0.5
str_float = "[-+]?\d*\.?\d*[e|E]?\d*"
m = re.search('EF=('+str_float+')',lines[0])
if m is not None:
Ef = float(m.group(1))
line_dat=lines[1].split()
self.rkmax, self.lmax, self.lnsmax = float(line_dat[0]), int(line_dat[1]), int(line_dat[2])
self.nt = self.lmax + 1
if self.lnsmax==0: self.lnsmax = 2
#kmax = rkmax/rmtmin
print('rkmax=', self.rkmax, 'lmax=', self.lmax, 'lnsmax=', self.lnsmax, 'nat=', strc.nat, file=fout)
self.lapw = ones((self.lmax+1,strc.nat), dtype=int, order='F')
self.nlo = zeros((lomax+1,strc.nat), dtype=int, order='F')
self.loor = ones((lomax+1,strc.nat), dtype=int, order='F')
self.nlo_tot = 0
iread = 2
for iat in range(strc.nat):
line_dat = lines[iread].split()
iread += 1
Ei0, nlr, iapw = float(line_dat[0]), int(line_dat[1]), int(line_dat[2]) # default parameters ei,iapw and number of exceptions nlr
if abs(Ei0-0.3)<1e-6: Ei0=Ef-0.2
if (iapw == 1):
self.lapw[:,iat] = 0 # default value for lapw(l,iat) = False
print('atom=', strc.aname[iat], 'E=', Ei0, ' apw with n.cases=', nlr, file=fout)
else:
self.lapw[:,iat] = 1
print('atom=', strc.aname[iat], 'E=', Ei0, 'lapw with n.cases=', nlr, file=fout)
self.loor[:,iat] = 0
self.nlo[:,iat] = 0
# Read exceptional cases.
# 1) If the same l appears twice, the second one is a LO
# set loor to true, nlo must be incremented by 1
# and g_tot by (2l+1)*mult[iat]
# 2) If it is an APW+lo (iapw == 1): lapw(l,iat) is set to false, and
# g_tot has to be incremented, nlo =1 , elo = ei...
dat=[]
for j in range(nlr):
line_dat = lines[iread].split()
iread += 1
dat.append( [int(line_dat[0]), float(line_dat[1]), float(line_dat[2]), line_dat[3], int(line_dat[4])] )
for j in range(nlr):
l, Ei, dE, scanflag, iapw = dat[j][0], dat[j][1], dat[j][2], dat[j][3], dat[j][4]
l_prev = dat[j-1][0] if j>0 else -1
this_is_LO = (l==l_prev) # current must be an LO
if this_is_LO:
self.loor[l,iat] = 1
self.nlo[l,iat] += 1 # this is ilo in fortran
self.nlo_tot += (2*l+1)*strc.mult[iat]
print((' '*10+'l=%2d %15s') % (l, 'Local Orbital'), file=fout)
else:
if (iapw==1):
self.lapw[l,iat]=0
self.nlo[l,iat] =1 # this is ilo in fortran
self.nlo_tot += (2*l+1)*strc.mult[iat]
print((' '*10+'l=%2d %15s') % (l, 'APW+lo'), file=fout)
else:
print((' '*10+'l=%2d %15s') % (l, 'LAPW'), file=fout)
nLO_at = zeros((strc.nat,lomax+1), dtype=int)
self.nLO_at_ind = []
for iat in range(strc.nat):
_nlo_at_ind_=[]
for l in range(lomax+1):
if self.lapw[l,iat]: # LAPW+LO, hence all orbitals are actually local orbitals
nLO_at[iat,l] = self.nlo[l,iat]
_nlo_at_ind_.append( list(range(1,self.nlo[l,iat]+1)) )
else: # this is APW+lo, hence one is not LO, but corresponds to lo in APW+lo
nLO_at[iat,l] = self.nlo[l,iat]-1
_nlo_at_ind_.append( list(range(1,self.nlo[l,iat])) )# the first orbital is not LO
self.nLO_at_ind.append(_nlo_at_ind_)
self.nLO_at = zeros((lomax+1,strc.nat), dtype=int, order='F')
self.nLO_at[:,:] = copy(nLO_at.T)
print('nLO_at_ind=', self.nLO_at_ind, file=fout)
print('nLO_at=', self.nLO_at, '==', [[ len(self.nLO_at_ind[iat][l]) for l in range(lomax+1)] for iat in range(strc.nat)], file=fout)
self.nlomax = amax(nLO_at)
self.l_newlo=0
if self.nlomax>1:
self.l_newlo = 1
nloat = amax(self.nlo)
print("More than one LO's are detected: set l_newlo to", nloat, file=fout)
print("Max. number of LO's per l per atom ( nlomax):", self.nlomax, file=fout)
print("Information on Local Orbitals (LO or lo) ", file=fout)
print(" lomax=",lomax, file=fout)
for l in range(lomax):
print("l=%2d LO? #lo #LO " % (l,), end=' ', file=fout)
print(file=fout)
for iat in range(strc.nat):
for l in range(lomax):
print(" %4d%4d%4d " % (self.loor[l,iat],self.nlo[l,iat],self.nLO_at[l,iat]), end=' ', file=fout)
print(file=fout)
print(' Total number of local orbitals (nlo_tot)= ', self.nlo_tot, file=fout)
def Add_Linearization_Energy(self, Elapw, Elo):
self.Elapw = Elapw
self.Elo = Elo
#El = zeros((lmax+1,strc.nat))
#Elo = zeros((lomax+1,nloat,strc.nat))
#umt = zeros((6,lmax+1,strc.nat)) # collective storage of e,p,pe,dp,dpe,pei
# umt(0,:,:) --> e(:,:) -- linearization energy E_l for atom iat
# umt(1,:,:) --> p(:,:) -- u_l(r,E) at r = RMT(iat)
# umt(2,:,:) --> pe(:,:) -- ud(r,E) at r = RMT(iat)
# umt(3,:,:) --> dp(:,:) -- derivative of u_l(r,E) to r at r=RMT(iat)
# umt(4,:,:) --> dpe(:,:) --derivative of ud_l(r,E) to r at r=RMT(iat)
# umt(5,:,:) --> pei(:,:) -- norm of ud_l(r,E) integrated over MT sphere
def get_linearization_energies(case, in1, strc, nspin, fout):
def is_float(str):
try:
float(str)
return True
except ValueError:
return False
Elapw = zeros((nspin,strc.nat,in1.nt))
#lomaxp1 = shape(in1.nlo)[1]
#nloat = amax(in1.nlo)
lomaxp1, nloat = shape(in1.nlo)[0], amax(in1.nlo)
#print 'lomax=', lomaxp1-1, 'nloat=', nloat
Elo = zeros((nspin,strc.nat,lomaxp1,nloat))
if nspin==2:
spflag = ['up','dn']
else:
spflag = ['']
for isp in range(nspin):
fi = open(case+'.energy'+spflag[isp], 'r')
for iat in range(strc.nat):
line1 = next(fi)
line2 = next(fi)
possible_spacing = [9,12]
spacing_type = sign(in1.l_newlo) # spacing = if l_newlo==0 9 else 12
# checking if this is really how linearization energies are written?
correct_spacing=True
spacing = possible_spacing[spacing_type]
#print('spacing=', spacing, 'spcing_type=', spacing_type, 'l_newlo=', in1.l_newlo)
for d in [line1[spacing*i:spacing*(i+1)] for i in range( int(len(line1)/spacing) )]:
if not is_float(d):
correct_spacing=False
break
if not correct_spacing:
spacing_type = 1-spacing_type
spacing = possible_spacing[spacing_type]
_elapw_ = list(map(float,[line1[spacing*i:spacing*(i+1)] for i in range(int(len(line1)/spacing))]))
_elo_ = list(map(float,[line2[spacing*i:spacing*(i+1)] for i in range(int(len(line2)/spacing))]))
Elapw[isp,iat,:] = _elapw_[:in1.nt]
_nloat_ = int( len(_elo_)/lomaxp1 )
if _nloat_ * lomaxp1 != len(_elo_):
print('WARNING lomax+1=', lomaxp1, 'and nloat=', _nloat_, 'and number of energies in case.energy is ', len(_elo_), 'which is not compatible. Expected # of energies in case.energy=', _nloat_ * lomaxp1)
print('Energies=', _elo_)
_elo_ = _elo_[:lomaxp1*_nloat_]
_elo_ = reshape(_elo_, (_nloat_,lomaxp1) ).T
# previoulsy wrong version
#Elo[isp,iat,l,ilo] = _elo_[:,:nloat]
for l in range(lomaxp1):
if in1.lapw[l,iat]: # This is LAPW+LO, hence local orbitals start at ilo=1 and not at ilo=0
Elo[isp,iat,l,1:nloat] = _elo_[l,0:(nloat-1)]
else: # This is APW+lo, hence first local orbital is actually not LO
Elo[isp,iat,l,:] = _elo_[l,:nloat]
for l in range(in1.nt):
if not in1.lapw[l,iat]:
Elapw[isp,iat,l] -= 200
print('-'*80, file=fout)
print('Linearization energy (read from energy file) for spin='+str(isp+1)+' atom '+strc.aname[iat]+':', file=fout)
for l in range(in1.nt):
print('Eapw[l='+str(l)+']=',Elapw[isp,iat,l], file=fout)
print(file=fout)
for l in range(lomaxp1):
for ilo in range(nloat):
if Elo[isp,iat,l,ilo] < 90000.0:
print('Elo[ilo='+str(ilo)+',l='+str(l)+']=', Elo[isp,iat,l,ilo], file=fout)
print(file=fout)
return (Elapw, Elo)
def Read_Radial_Potential(case, nat, nspin, nrpt, fout):
nrad = max(nrpt)
fid = open(case+'.vsp','r')
Vr = zeros((nspin,nat,nrad))
for isp in range(nspin):
line = next(fid)
line = next(fid)
line = next(fid)
for iat in range(nat):
npt = nrpt[iat]
line = next(fid)
dat = line.split()
if int(dat[2]) != iat+1:
print('WARN: ATOMNUMBER seems wrong in reading '+case+'.vsp file iat=', iat+1, 'and dat=', dat)
line = next(fid)
dat = line.split()
num_lm = int(dat[3])
line = next(fid)
line = next(fid)
line = next(fid)
dat = line.split()
r_l,r_m = int(dat[3]), int(dat[5])
line = next(fid)
ipt = 0
while (fid):
line = next(fid)
for i in range(4):
Vr[isp,iat,ipt] = float(line[(3+i*19):(3+(i+1)*19)])
ipt += 1
if ipt >= npt: break
if ipt >= npt : break
for i in range(6):
line = next(fid)
fid.close()
print('potential read from file '+case+'.vsp', file=fout)
return Vr
#def Read_energy_file(spflag, case, strc, fout, give_kname=False,Extended=False):
# fi = open(case+'.energy'+spflag, 'r')
# filename = case+'.energy'+spflag
def Read_energy_file(filename, strc, fout, give_kname=False,Extended=False):
fi = open(filename, 'r')
lines = fi.readlines()
fi.close()
iln = 2*strc.nat # bug jul.7.2020
if give_kname: knames=[]
Ebnd=[]
klist=[]
wegh=[]
hsrws=[]
for ik in range(1000000):
if iln >= len(lines): break
line = lines[iln]; iln += 1
if Extended:
kks = [line[27*i:27*(i+1)] for i in range(3)]
kname = line[81:91]
hsr = [line[91+6*i:91+6*(i+1)] for i in range(2)]
ws = line[103:108]
else:
kks = [line[19*i:19*(i+1)] for i in range(3)]
kname = line[57:67]
hsr = [line[67+6*i:67+6*(i+1)] for i in range(2)]
ws = line[79:84]
kp = list(map(float,kks))
hsrows, Ne = int(hsr[0]), int(hsr[1])
wgh = float(ws)
klist.append(kp) # kvecs2
if give_kname: knames.append(kname)
wegh.append(wgh) # wk2
hsrws.append(hsrows) # ngkir
#print kp, kname, hsrows, Ne, wgh
Ebands=[]
for ib in range(Ne):
line = lines[iln]; iln += 1
dd = line.split()
ibp1, ee = int(dd[0]), float(dd[1])
Ebands.append(ee)
#print ib+1, ibp1, ee
Ebnd.append( Ebands ) # bande
# He multiplies Ebnd by 0.5 because of the units!
if give_kname:
return (klist, wegh, Ebnd, hsrws, knames)
else:
return (klist, wegh, Ebnd, hsrws)
def Read_xc_file_dimension(case, strc, fout, ReadAll=False):
fi = open(case+'.vxc', 'r')
lines = fi.readlines()
fi.close()
iln = 4
for iat in range(strc.nat):
npt = strc.nrpt[iat]
lcmax = int(lines[iln][15:18]); iln += 3
Vxc = zeros(npt)
for lxc in range(lcmax):
lm = int(lines[iln][15:18]), int(lines[iln][23:25])
#print lm
iln += 2
ipt = 0
for il in range(10000):
line = lines[iln]; iln+=1
if ReadAll:
for i in range(4):
w = float(line[(3+19*i):(3+19*(i+1))])
Vxc[ipt]=w; ipt += 1
if ipt >= npt: break
else:
ipt += 4
if ipt>=npt: break
iln += 2
iln += 5 # bug jul. 7, 2020
iln += 1 # bug jul. 7, 2020
nksxc = int(lines[iln][13:19])
iln += 1
kxcmax = 0
for ikxc in range(nksxc):
line = lines[iln]; iln += 1
ksxc = list(map(int, [line[3+5*i:3+5*(i+1)] for i in range(3)]))
if ReadAll:
w = float(line[18:(18+19)]) + float(line[(18+19):(18+2*19)])*1j
kxcmax = max( kxcmax, max(list(map(abs,ksxc))) )
return kxcmax
def Read_xc_file(case, strc, fout):
fi = open(case+'.vxc', 'r')
lines = fi.readlines()
fi.close()
iln = 4
#lxcm=[]
lmxc=[]
Vxclm=[]
for iat in range(strc.nat):
npt = strc.nrpt[iat]
ncm = int(lines[iln][15:18]); iln += 3
#lxcm.append( ncm )
Vxc = zeros((ncm,npt))
_lmxc_=[]
for lxc in range(ncm):
lm = [int(lines[iln][15:18]), int(lines[iln][23:25])]
_lmxc_.append(lm)
iln += 2
ipt = 0
for il in range(10000):
line = lines[iln]; iln+=1
for i in range(4):
w = float(line[(3+19*i):(3+19*(i+1))])
Vxc[lxc,ipt]=w; ipt += 1
if ipt >= npt: break
if ipt>=npt: break
iln += 2
iln += 5 # bug jul.7 2020
lmxc.append( _lmxc_ ) # lmxc[iat][lxc][0-1]
Vxclm.append( Vxc ) # Vxclm[iat][lxc,ir]
iln += 1 # bug jul.7 2020
nksxc = int(lines[iln][13:19])
iln += 1
kxcmax = 0
Vxcs = zeros((nksxc),dtype=complex)
ksxc = zeros((nksxc,3),dtype=int)
for ikxc in range(nksxc):
line = lines[iln]; iln += 1
ksxc[ikxc,:] = list(map(int, [line[3+5*i:3+5*(i+1)] for i in range(3)]))
Vxcs[ikxc] = float(line[18:(18+19)]) + float(line[(18+19):(18+2*19)])*1j
#kxcmax = max( kxcmax, max(map(abs,ksxc)) )
print("*** Number of lm component for each atom ", file=fout)
print(" iat lxcm(iat)", file=fout)
for iat in range(strc.nat):
print('%5d %10d' % (iat, len(lmxc[iat])), file=fout)
print("# Number of Institial plane waves from vxc=", nksxc, file=fout)
return (lmxc, Vxclm, ksxc, Vxcs)
def Read_vector_file(case, strc_nat, fout, lmaxp1=1,lomaxp1=1,nloat=1):
vectortype = float
vecread = rdVec.fvread3
vecwrite = rdVec.fvwrite3
so = ''
if os.path.isfile(case+".inso") and os.path.getsize(case+".inso")>0:
print('Found '+case+'.inso file, hence assuming so-coupling exists. Switching -so switch!', file=fout)
so = 'so'
vectortype=complex
if os.path.isfile(case+".in1c") and os.path.getsize(case+".in1c")>0:
print('Found '+case+'.in1c file, hence assuming complex eigenvector. Switching to complex!', file=fout)
so = ''
vectortype=complex
if vectortype==complex:
vecread = rdVec.fvread3c
vecwrite = rdVec.fvwrite3c
maxkpoints = 10000
# opens vector file
heads=[]
all_Gs=[]
all_As=[]
all_Ek=[]
fname, tape = case+'.vector'+so, 9
#print('nat=', strc_nat, 'lmaxp1=', lmaxp1, 'lomaxp1=', lomaxp1, 'nloat=', nloat)
#fVopen(Elapw, Elo, fh, filename, nat, lmaxp1, lomaxp1, nloat)
#REAL*8, intent(out) :: Elapw(nat,lmaxp1), Elo(nat,lomaxp1,nloat)
Elapw, Elo = rdVec.fvopen(tape, fname, strc_nat, lmaxp1, lomaxp1, nloat)
for ik in range(maxkpoints):
# Reads vector file
head = rdVec.fvread1(tape)
(k, kname, wgh, ios, n0, nb) = head
if ios!=0: break # vector file is finished, no more k-points
print('Read_vector_file k=', k, 'nrows=', n0, 'nbands=', nb, file=fout)
heads.append(head)
# Reciprocal vectors
Gs = rdVec.fvread2(tape, n0)
all_Gs.append(Gs.T)
# Reading KS eigensystem
As=zeros((nb,n0), dtype=vectortype)
Ek=zeros(nb, dtype=float)
for i in range(nb):
(num, ek, A) = vecread(tape, n0)
As[i,:] = A # KS eigenvector
Ek[i] = ek # KS eigenvalue
all_As.append(As)
all_Ek.append(Ek)
rdVec.fvclose(tape)
if lmaxp1==1:
return (heads, all_Gs, all_As, all_Ek)
else:
return (heads, all_Gs, all_As, all_Ek, Elapw, Elo)
def Read_vector_file2(vector_filename, strc_nat, fout, vectortype=float):
if vectortype == float:
vecread = rdVec.fvread3
vecwrite = rdVec.fvwrite3
elif vectortype == complex:
vecread = rdVec.fvread3c
vecwrite = rdVec.fvwrite3c
else:
print('ERROR: vectortype has to be float or complex')
sys.exit(1)
maxkpoints = 10000
# opens vector file
heads=[]
all_Gs=[]
all_As=[]
all_Ek=[]
tape = 9
Elinear = rdVec.fvopen(tape, vector_filename, strc_nat, 1, 1, 1)
#print('linearization energy=', Elinear, file=fout)
for ik in range(maxkpoints):
# Reads vector file
head = rdVec.fvread1(tape)
(k, kname, wgh, ios, n0, nb) = head
if ios!=0: break # vector file is finished, no more k-points
print('Read_vector_file2 k=', k, 'nrows=', n0, 'nbands=', nb, file=fout)
heads.append(head)
# Reciprocal vectors
Gs = rdVec.fvread2(tape, n0)
all_Gs.append(Gs.T)
# Reading KS eigensystem
As=zeros((nb,n0), dtype=vectortype)
Ek=zeros(nb, dtype=float)
for i in range(nb):
(num, ek, A) = vecread(tape, n0)
As[i,:] = A # KS eigenvector
Ek[i] = ek # KS eigenvalue
all_As.append(As)
all_Ek.append(Ek)
rdVec.fvclose(tape)
return (heads, all_Gs, all_As, all_Ek)
class RadialFunctions:
def __init__(self, in1, strc, Elapw, Elo, Vr, nspin, fout):
self.umt = zeros((nspin,strc.nat,in1.nt,5))
import radials as rd
Vr *= 0.5 # Converting from Rydberg to Hartree