forked from vanroeke/qscaild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thirdorder_core.pyx
1028 lines (953 loc) · 42 KB
/
thirdorder_core.pyx
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
# thirdorder, help compute anharmonic IFCs from minimal sets of displacements
# Copyright (C) 2012-2014 Wu Li <[email protected]>
# Copyright (C) 2012-2014 Jesús Carrete Montaña <[email protected]>
# Copyright (C) 2012-2014 Natalio Mingo Bisquert <[email protected]>
# Copyright (C) 2014 Antti J. Karttunen <[email protected]>
#
# 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/>.
import sys
# This file contains Cython wrappers allowing the relevant functions
# in spglib need to be used from Python.
# The algorithms for finding minimal sets of interatomic force constants
# and for reconstructing the full set from such a minimal subset are
# also implemented in this file in the interest of efficiency.
from libc.stdlib cimport malloc,free
from libc.math cimport floor,fabs
import sys
import copy
import numpy as np
import scipy as sp
import scipy.linalg
import scipy.sparse
import scipy.sparse.linalg
cimport cython
cimport numpy as np
np.import_array()
cimport cthirdorder_core
# NOTE: all indices used in this module are zero-based.
# Maximum matrix size (rows*cols) for the dense method.
#DEF MAXDENSE=33554432
DEF MAXDENSE=0
# Permutations of 3 elements listed in the same order as in the old
# Fortran code.
cdef int[:,:] permutations=np.array([
[0,1,2],
[1,0,2],
[2,1,0],
[0,2,1],
[1,2,0],
[2,0,1]],dtype=np.intc)
@cython.boundscheck(False)
@cython.wraparound(False)
cdef inline int _ind2id(int[:] icell,int ispecies,int[:] ngrid,int nspecies):
"""
Merge a set of cell+atom indices into a single index into a supercell.
"""
return (icell[0]+(icell[1]+icell[2]*ngrid[1])*ngrid[0])*nspecies+ispecies
@cython.boundscheck(False)
@cython.wraparound(False)
cdef inline bint _triplet_in_list(int[:] triplet,int[:,:] llist,int nlist):
"""
Return True if triplet is found in llist[:,:nlist]. The first dimension
of list must have a length of 3.
"""
# This works fine for the nlist ranges we have to deal with, but
# using std::vector and std::push_heap would be a better general
# solution.
cdef int i
for i in xrange(nlist):
if (triplet[0]==llist[0,i] and
triplet[1]==llist[1,i] and triplet[2]==llist[2,i]):
return True
return False
@cython.boundscheck(False)
@cython.wraparound(False)
cdef inline bint _triplets_are_equal(int[:] triplet1,int[:] triplet2):
"""
Return True if two triplets are equal and False otherwise.
"""
cdef int i
for i in xrange(3):
if triplet1[i]!=triplet2[i]:
return False
return True
@cython.boundscheck(False)
@cython.wraparound(False)
cdef tuple _id2ind(int[:] ngrid,int nspecies):
"""
Create a map from supercell indices to cell+atom indices.
"""
cdef int ii,ntot,tmp
cdef int[:,:] icell
cdef int[:] ispecies
cdef np.ndarray np_icell,np_ispecies
ntot=ngrid[0]*ngrid[1]*ngrid[2]*nspecies
np_icell=np.empty((3,ntot),dtype=np.intc)
np_ispecies=np.empty(ntot,dtype=np.intc)
icell=np_icell
ispecies=np_ispecies
for ii in xrange(ntot):
tmp,ispecies[ii]=divmod(ii,nspecies)
tmp,icell[0,ii]=divmod(tmp,ngrid[0])
icell[2,ii],icell[1,ii]=divmod(tmp,ngrid[1])
return (np_icell,np_ispecies)
# Thin, specialized wrapper around spglib.
cdef class SymmetryOperations:
"""
Object that contains all the interesting information about the
crystal symmetry group of a set of atoms.
"""
cdef double[:,:] __lattvec
cdef int[:] __types
cdef double[:,:] __positions
cdef readonly str symbol
cdef double[:] __shift
cdef double[:,:] __transform
cdef double[:,:,:] __rotations
cdef double[:,:,:] __crotations
cdef double[:,:] __translations
cdef double[:,:] __ctranslations
cdef double c_lattvec[3][3]
cdef int *c_types
cdef double (*c_positions)[3]
cdef readonly int natoms,nsyms
cdef readonly double symprec
property lattice_vectors:
def __get__(self):
return np.asarray(self.__lattvec)
property types:
def __get__(self):
return np.asarray(self.__lattvec)
property positions:
def __get__(self):
return np.asarray(self.__positions)
property origin_shift:
def __get__(self):
return np.asarray(self.__shift)
property transformation_matrix:
def __get__(self):
return np.asarray(self.__transform)
property rotations:
def __get__(self):
return np.asarray(self.__rotations)
property translations:
def __get__(self):
return np.asarray(self.__translations)
property crotations:
def __get__(self):
return np.asarray(self.__crotations)
property ctranslations:
def __get__(self):
return np.asarray(self.__ctranslations)
cdef void __build_c_arrays(self):
"""
Build the internal low-level representations of the input
parameters, ready to be passed to C functions.
"""
self.c_types=<int*>malloc(self.natoms*sizeof(int))
self.c_positions=<double(*)[3]>malloc(self.natoms*sizeof(double[3]))
if self.c_types is NULL or self.c_positions is NULL:
raise MemoryError()
cdef void __refresh_c_arrays(self):
"""
Copy the values of __types, __positions and __lattvec to
their C counterparts.
"""
cdef int i,j
for i in xrange(3):
for j in xrange(3):
self.c_lattvec[i][j]=self.__lattvec[i,j]
for i in xrange(self.natoms):
self.c_types[i]=self.__types[i]
for j in xrange(3):
self.c_positions[i][j]=self.__positions[i,j]
cdef void __spg_get_dataset(self) except *:
"""
Thin, slightly selective wrapper around spg_get_dataset(). The
interesting information is copied out to Python objects and the
rest discarded.
"""
cdef int i,j,k
cdef double[:] tmp1d
cdef double[:,:] tmp2d
cdef cthirdorder_core.SpglibDataset *data
data=cthirdorder_core.spg_get_dataset(self.c_lattvec,
self.c_positions,
self.c_types,
self.natoms,
self.symprec)
# The C arrays can get corrupted by this function call.
self.__refresh_c_arrays()
if data is NULL:
raise MemoryError()
self.symbol=data.international_symbol.decode("ASCII").strip()
self.__shift=np.empty((3,),dtype=np.double)
self.__transform=np.empty((3,3),dtype=np.double)
self.nsyms=data.n_operations
self.__rotations=np.empty((self.nsyms,3,3),
dtype=np.double)
self.__translations=np.empty((self.nsyms,3),
dtype=np.double)
for i in xrange(3):
self.__shift[i]=data.origin_shift[i]
for j in xrange(3):
self.__transform[i,j]=data.transformation_matrix[i][j]
for i in xrange(self.nsyms):
for j in xrange(3):
self.__translations[i,j]=data.translations[i][j]
for k in xrange(3):
self.__rotations[i,j,k]=data.rotations[i][j][k]
self.__crotations=np.empty_like(self.__rotations)
self.__ctranslations=np.empty_like(self.__translations)
for i in xrange(self.nsyms):
tmp2d=np.dot(self.__lattvec,
np.dot(self.__rotations[i,:,:],
sp.linalg.inv(self.__lattvec)))
self.__crotations[i,:,:]=tmp2d
tmp1d=np.dot(self.__lattvec,self.__translations[i,:])
self.__ctranslations[i,:]=tmp1d
cthirdorder_core.spg_free_dataset(data)
def __cinit__(self,lattvec,types,positions,symprec=1e-5):
self.__lattvec=np.array(lattvec,dtype=np.double)
self.__types=np.array(types,dtype=np.intc)
self.__positions=np.array(positions,dtype=np.double)
self.natoms=self.positions.shape[0]
self.symprec=symprec
if self.__positions.shape[0]!=self.natoms or self.__positions.shape[1]!=3:
raise ValueError("positions must be a natoms x 3 array")
if not (self.__lattvec.shape[0]==self.__lattvec.shape[1]==3):
raise ValueError("lattice vectors must form a 3 x 3 matrix")
self.__build_c_arrays()
self.__refresh_c_arrays()
self.__spg_get_dataset()
def __dealloc__(self):
if self.c_types is not NULL:
free(self.c_types)
if self.c_positions is not NULL:
free(self.c_positions)
cdef __apply_all(self,double[:] r_in):
"""
Apply all symmetry operations to a vector and return the results.
"""
cdef int ii,jj,kk
cdef np.ndarray r_out
cdef double[:,:] vr_out
r_out=np.zeros((3,self.nsyms),dtype=np.double)
vr_out=r_out
for ii in xrange(self.nsyms):
for jj in xrange(3):
for kk in xrange(3):
vr_out[jj,ii]+=self.__crotations[ii,jj,kk]*r_in[kk]
vr_out[jj,ii]+=self.__ctranslations[ii,jj]
return r_out
@cython.boundscheck(False)
@cython.wraparound(False)
cdef map_supercell(self,dict sposcar):
"""
Each symmetry operation defines an atomic permutation in a supercell. This method
returns an array with those permutations. The supercell must be compatible with
the unit cell used to create the object.
"""
cdef int ntot
cdef int i,ii,ll,isym
cdef int[:] ngrid,vec
cdef int[:,:] v_nruter
cdef double diff
cdef double[:] car,tmp
cdef double[:,:] car_sym,positions,lattvec,motif
cdef np.ndarray nruter
cdef tuple factorization
positions=sposcar["positions"]
lattvec=sposcar["lattvec"]
ngrid=np.array([sposcar["na"],sposcar["nb"],sposcar["nc"]],
dtype=np.intc)
ntot=positions.shape[1]
natoms=ntot//(ngrid[0]*ngrid[1]*ngrid[2])
motif=np.empty((3,natoms),dtype=np.double)
for i in xrange(natoms):
for ii in xrange(3):
motif[ii,i]=(self.__positions[i,0]*self.__lattvec[ii,0]+
self.__positions[i,1]*self.__lattvec[ii,1]+
self.__positions[i,2]*self.__lattvec[ii,2])
nruter=np.empty((self.nsyms,ntot),dtype=np.intc)
car=np.empty(3,dtype=np.double)
tmp=np.empty(3,dtype=np.double)
v_nruter=nruter
vec=np.empty(3,dtype=np.intc)
factorization=sp.linalg.lu_factor(self.__lattvec)
for i in xrange(ntot):
for ii in xrange(3):
car[ii]=(positions[0,i]*lattvec[ii,0]+
positions[1,i]*lattvec[ii,1]+
positions[2,i]*lattvec[ii,2])
car_sym=self.__apply_all(car)
for isym in xrange(self.nsyms):
for ii in xrange(natoms):
for ll in xrange(3):
tmp[ll]=car_sym[ll,isym]-motif[ll,ii]
tmp=sp.linalg.lu_solve(factorization,tmp)
for ll in xrange(3):
vec[ll]=int(round(tmp[ll]))
diff=(fabs(vec[0]-tmp[0])+
fabs(vec[1]-tmp[1])+
fabs(vec[2]-tmp[2]))
for ll in xrange(3):
vec[ll]=vec[ll]%ngrid[ll]
if diff<1e-4:
v_nruter[isym,i]=_ind2id(vec,ii,ngrid,natoms)
break
else:
sys.exit("Error: equivalent atom not found for isym={0}, atom={1}"
.format(isym,i))
return nruter
@cython.boundscheck(False)
def reconstruct_ifcs(phipart,wedge,list4,poscar,sposcar):
"""
Recover the full anharmonic IFC set from the irreducible set of
force constants and the information contained in a wedge object.
"""
cdef int ii,jj,ll,mm,nn,kk,ss,tt,ix,e0,e1,e2,e3
cdef int nlist,nlist4,natoms,ntot
cdef int ntotalindependent,tribasisindex,colindex,nrows,ncols
cdef int[:] naccumindependent
cdef int[:,:,:] vind1
cdef int[:,:,:] vind2
cdef int[:,:,:] vequilist
cdef double[:] aphilist
cdef double[:,:] vaa
cdef double[:,:,:] vphipart
cdef double[:,:,:,:,:,:] vnruter
nlist=wedge.nlist
natoms=len(poscar["types"])
ntot=len(sposcar["types"])
vnruter=np.zeros((3,3,3,natoms,ntot,ntot),dtype=np.double)
naccumindependent=np.insert(np.cumsum(
wedge.nindependentbasis[:nlist],dtype=np.intc),0,
np.zeros(1,dtype=np.intc))
ntotalindependent=naccumindependent[-1]
vphipart=phipart
nlist4=len(list4)
for ii in xrange(nlist4):
e0,e1,e2,e3=list4[ii]
vnruter[e2,e3,:,e0,e1,:]=vphipart[:,ii,:]
philist=[]
for ii in xrange(nlist):
for jj in xrange(wedge.nindependentbasis[ii]):
ll=wedge.independentbasis[jj,ii]//9
mm=(wedge.independentbasis[jj,ii]%9)//3
nn=wedge.independentbasis[jj,ii]%3
philist.append(vnruter[ll,mm,nn,
wedge.llist[0,ii],
wedge.llist[1,ii],
wedge.llist[2,ii]])
aphilist=np.array(philist,dtype=np.double)
vind1=-np.ones((natoms,ntot,ntot),dtype=np.intc)
vind2=-np.ones((natoms,ntot,ntot),dtype=np.intc)
vequilist=wedge.allequilist
for ii in xrange(nlist):
for jj in xrange(wedge.nequi[ii]):
vind1[vequilist[0,jj,ii],
vequilist[1,jj,ii],
vequilist[2,jj,ii]]=ii
vind2[vequilist[0,jj,ii],
vequilist[1,jj,ii],
vequilist[2,jj,ii]]=jj
vtrans=wedge.transformationarray
nrows=ntotalindependent
ncols=natoms*ntot*27
if nrows*ncols<=MAXDENSE:
print "- Storing the coefficients in a dense matrix"
aa=np.zeros((nrows,ncols),dtype=np.double)
vaa=aa
colindex=0
for ii in xrange(natoms):
for jj in xrange(ntot):
tribasisindex=0
for ll in xrange(3):
for mm in xrange(3):
for nn in xrange(3):
for kk in xrange(ntot):
for ix in xrange(nlist):
if vind1[ii,jj,kk]==ix:
for ss in xrange(naccumindependent[ix],
naccumindependent[ix+1]):
tt=ss-naccumindependent[ix]
vaa[ss,colindex]+=vtrans[tribasisindex,tt,
vind2[ii,jj,kk],ix]
tribasisindex+=1
colindex+=1
else:
print "- Storing the coefficients in a sparse matrix"
i=[]
j=[]
v=[]
colindex=0
for ii in xrange(natoms):
for jj in xrange(ntot):
tribasisindex=0
for ll in xrange(3):
for mm in xrange(3):
for nn in xrange(3):
for kk in xrange(ntot):
for ix in xrange(nlist):
if vind1[ii,jj,kk]==ix:
for ss in xrange(naccumindependent[ix],
naccumindependent[ix+1]):
tt=ss-naccumindependent[ix]
i.append(ss)
j.append(colindex)
v.append(vtrans[tribasisindex,tt,
vind2[ii,jj,kk],ix])
tribasisindex+=1
colindex+=1
print "- \t Density: {0:.2g}%".format(100.*len(i)/float(nrows*ncols))
aa=sp.sparse.coo_matrix((v,(i,j)),(nrows,ncols)).tocsr()
D=sp.sparse.spdiags(aphilist,[0,],aphilist.size,aphilist.size,
format="csr")
bbs=D.dot(aa)
ones=np.ones_like(aphilist)
multiplier=-sp.sparse.linalg.lsqr(bbs,ones)[0]
compensation=D.dot(bbs.dot(multiplier))
aphilist+=compensation
# Build the final, full set of anharmonic IFCs.
vnruter[:,:,:,:,:,:]=0.
for ii in xrange(nlist):
for jj in xrange(wedge.nequi[ii]):
for ll in xrange(3):
for mm in xrange(3):
for nn in xrange(3):
tribasisindex=(ll*3+mm)*3+nn
for ix in xrange(wedge.nindependentbasis[ii]):
vnruter[ll,mm,nn,vequilist[0,jj,ii],
vequilist[1,jj,ii],
vequilist[2,jj,ii]
]+=wedge.transformationarray[
tribasisindex,ix,jj,ii]*aphilist[
naccumindependent[ii]+ix]
return vnruter
@cython.boundscheck(False)
def reconstruct_ifcs_philist(philist,wedge,list4,poscar,sposcar):
"""
Recover the full anharmonic IFC set from the irreducible set of
force constants and the information contained in a wedge object.
"""
cdef int ii,jj,ll,mm,nn,kk,ss,tt,ix,e0,e1,e2,e3
cdef int nlist,nlist4,natoms,ntot
cdef int ntotalindependent,tribasisindex,colindex,nrows,ncols
cdef int[:] naccumindependent
cdef int[:,:,:] vind1
cdef int[:,:,:] vind2
cdef int[:,:,:] vequilist
cdef double[:] aphilist
cdef double[:,:] vaa
cdef double[:,:,:,:,:,:] vnruter
nlist=wedge.nlist
natoms=len(poscar["types"])
ntot=len(sposcar["types"])
vnruter=np.zeros((3,3,3,natoms,ntot,ntot),dtype=np.double)
naccumindependent=np.insert(np.cumsum(
wedge.nindependentbasis[:nlist],dtype=np.intc),0,
np.zeros(1,dtype=np.intc))
ntotalindependent=naccumindependent[-1]
nlist4=len(list4)
aphilist=np.array(philist,dtype=np.double)
vind1=-np.ones((natoms,ntot,ntot),dtype=np.intc)
vind2=-np.ones((natoms,ntot,ntot),dtype=np.intc)
vequilist=wedge.allequilist
for ii in xrange(nlist):
for jj in xrange(wedge.nequi[ii]):
vind1[vequilist[0,jj,ii],
vequilist[1,jj,ii],
vequilist[2,jj,ii]]=ii
vind2[vequilist[0,jj,ii],
vequilist[1,jj,ii],
vequilist[2,jj,ii]]=jj
vtrans=wedge.transformationarray
nrows=ntotalindependent
ncols=natoms*ntot*27
if nrows*ncols<=MAXDENSE:
print "- Storing the coefficients in a dense matrix"
aa=np.zeros((nrows,ncols),dtype=np.double)
vaa=aa
colindex=0
for ii in xrange(natoms):
for jj in xrange(ntot):
tribasisindex=0
for ll in xrange(3):
for mm in xrange(3):
for nn in xrange(3):
for kk in xrange(ntot):
for ix in xrange(nlist):
if vind1[ii,jj,kk]==ix:
for ss in xrange(naccumindependent[ix],
naccumindependent[ix+1]):
tt=ss-naccumindependent[ix]
vaa[ss,colindex]+=vtrans[tribasisindex,tt,
vind2[ii,jj,kk],ix]
tribasisindex+=1
colindex+=1
else:
print "- Storing the coefficients in a sparse matrix"
i=[]
j=[]
v=[]
colindex=0
for ii in xrange(natoms):
for jj in xrange(ntot):
tribasisindex=0
for ll in xrange(3):
for mm in xrange(3):
for nn in xrange(3):
for kk in xrange(ntot):
for ix in xrange(nlist):
if vind1[ii,jj,kk]==ix:
for ss in xrange(naccumindependent[ix],
naccumindependent[ix+1]):
tt=ss-naccumindependent[ix]
i.append(ss)
j.append(colindex)
v.append(vtrans[tribasisindex,tt,
vind2[ii,jj,kk],ix])
tribasisindex+=1
colindex+=1
print "- \t Density: {0:.2g}%".format(100.*len(i)/float(nrows*ncols))
aa=sp.sparse.coo_matrix((v,(i,j)),(nrows,ncols)).tocsr()
D=sp.sparse.spdiags(aphilist,[0,],aphilist.size,aphilist.size,
format="csr")
# bbs=D.dot(aa)
# ones=np.ones_like(aphilist)
# multiplier=-sp.sparse.linalg.lsqr(bbs,ones)[0]
# compensation=D.dot(bbs.dot(multiplier))
# aphilist+=compensation
# Build the final, full set of anharmonic IFCs.
vnruter[:,:,:,:,:,:]=0.
for ii in xrange(nlist):
for jj in xrange(wedge.nequi[ii]):
for ll in xrange(3):
for mm in xrange(3):
for nn in xrange(3):
tribasisindex=(ll*3+mm)*3+nn
for ix in xrange(wedge.nindependentbasis[ii]):
vnruter[ll,mm,nn,vequilist[0,jj,ii],
vequilist[1,jj,ii],
vequilist[2,jj,ii]
]+=wedge.transformationarray[
tribasisindex,ix,jj,ii]*aphilist[
naccumindependent[ii]+ix]
return vnruter
cdef class Wedge:
"""
Objects of this class allow the user to extract irreducible sets
of force constants and to reconstruct the full third-order IFC
matrix from them.
"""
cdef readonly SymmetryOperations symops
cdef readonly dict poscar,sposcar
cdef int allocsize,allallocsize,nalllist
cdef readonly int nlist
cdef readonly np.ndarray nequi,llist,allequilist
cdef readonly np.ndarray nindependentbasis,independentbasis
cdef readonly np.ndarray transformationarray
cdef np.ndarray alllist,transformation,transformationaux
cdef int[:,:] nequis
cdef int[:,:,:] shifts
cdef double[:,:] dmin
cdef readonly double frange
def __cinit__(self,poscar,sposcar,symops,dmin,nequis,shifts,frange):
"""
Build the object by computing all the relevant information about
irreducible IFCs.
"""
self.poscar=poscar
self.sposcar=sposcar
self.symops=symops
self.dmin=dmin
self.nequis=nequis
self.shifts=shifts
self.frange=frange
self.allocsize=0
self.allallocsize=0
self._expandlist()
self._expandalllist()
self._reduce()
cdef _expandlist(self):
"""
Expand nequi, allequilist, transformationarray, transformation,
transformationaux, nindependentbasis, independentbasis,
and llist to accommodate more elements.
"""
if self.allocsize==0:
self.allocsize=16
self.nequi=np.empty(self.allocsize,dtype=np.intc)
self.allequilist=np.empty((3,6*self.symops.nsyms,
self.allocsize),dtype=np.intc)
self.transformationarray=np.empty((27,27,6*self.symops.nsyms,
self.allocsize),dtype=np.double)
self.transformation=np.empty((27,27,6*self.symops.nsyms,
self.allocsize),dtype=np.double)
self.transformationaux=np.empty((27,27,self.allocsize),
dtype=np.double)
self.nindependentbasis=np.empty(self.allocsize,dtype=np.intc)
self.independentbasis=np.empty((27,self.allocsize),dtype=np.intc)
self.llist=np.empty((3,self.allocsize),dtype=np.intc)
else:
self.allocsize<<=1
self.nequi=np.concatenate((self.nequi,self.nequi),axis=-1)
self.allequilist=np.concatenate((self.allequilist,self.allequilist),axis=-1)
self.transformation=np.concatenate((self.transformation,self.transformation),
axis=-1)
self.transformationarray=np.concatenate((self.transformationarray,
self.transformationarray),axis=-1)
self.transformationaux=np.concatenate((self.transformationaux,
self.transformationaux),axis=-1)
self.nindependentbasis=np.concatenate((self.nindependentbasis,self.nindependentbasis),
axis=-1)
self.independentbasis=np.concatenate((self.independentbasis,self.independentbasis),
axis=-1)
self.llist=np.concatenate((self.llist,self.llist),axis=-1)
cdef _expandalllist(self):
"""
Expand alllist to accommodate more elements.
"""
if self.allallocsize==0:
self.allallocsize=512
self.alllist=np.empty((3,self.allallocsize),dtype=np.intc)
else:
self.allallocsize<<=1
self.alllist=np.concatenate((self.alllist,self.alllist),axis=-1)
@cython.boundscheck(False)
@cython.wraparound(False)
cdef _reduce(self):
"""
C-level method that performs most of the actual work.
"""
cdef int ngrid1,ngrid2,ngrid3,nsym,natoms,ntot,summ,nnonzero
cdef int ii,jj,kk,ll,iaux,jaux
cdef int ibasis,jbasis,kbasis,ibasisprime,jbasisprime,kbasisprime
cdef int iperm,isym,indexijk,indexijkprime
cdef int[:] ngrid,ind_species,vec1,vec2,vec3,independent
cdef int[:] v_nequi,v_nindependentbasis
cdef int[:] basis,triplet,triplet_perm,triplet_sym
cdef int[:,:] v_llist,v_alllist,v_independentbasis
cdef int[:,:] shifts27,shift2all,shift3all
cdef int[:,:] equilist,id_equi,ind_cell
cdef int[:,:,:] nonzero
cdef int[:,:,:] v_allequilist
cdef double dist,frange2
cdef double[:] car2,car3
cdef double[:,:] lattvec,coordall,b,coeffi,coeffi_reduced
cdef double[:,:,:] orth
cdef double[:,:,:] v_transformationaux
cdef double[:,:,:,:] rot,rot2
cdef double[:,:,:,:] v_transformationarray,v_transformation
# Preliminary work: memory allocation and initialization.
frange2=self.frange*self.frange
ngrid1=self.sposcar["na"]
ngrid2=self.sposcar["nb"]
ngrid3=self.sposcar["nc"]
ngrid=np.array([ngrid1,ngrid2,ngrid3],dtype=np.intc)
nsym=self.symops.nsyms
natoms=len(self.poscar["types"])
ntot=len(self.sposcar["types"])
vec1=np.empty(3,dtype=np.intc)
vec2=np.empty(3,dtype=np.intc)
vec3=np.empty(3,dtype=np.intc)
lattvec=self.sposcar["lattvec"]
coordall=np.dot(lattvec,self.sposcar["positions"])
orth=np.transpose(self.symops.crotations,(1,2,0))
car2=np.empty(3,dtype=np.double)
car3=np.empty(3,dtype=np.double)
summ=0
self.nlist=0
self.nalllist=0
v_nequi=self.nequi
v_allequilist=self.allequilist
v_transformation=self.transformation
v_transformationarray=self.transformationarray
v_transformationaux=self.transformationaux
v_nindependentbasis=self.nindependentbasis
v_independentbasis=self.independentbasis
v_llist=self.llist
v_alllist=self.alllist
iaux=0
shifts27=np.empty((27,3),dtype=np.intc)
for ii in xrange(-1,2):
for jj in xrange(-1,2):
for kk in xrange(-1,2):
shifts27[iaux,0]=ii
shifts27[iaux,1]=jj
shifts27[iaux,2]=kk
iaux+=1
basis=np.empty(3,dtype=np.intc)
triplet=np.empty(3,dtype=np.intc)
triplet_perm=np.empty(3,dtype=np.intc)
triplet_sym=np.empty(3,dtype=np.intc)
shift2all=np.empty((3,27),dtype=np.intc)
shift3all=np.empty((3,27),dtype=np.intc)
equilist=np.empty((3,nsym*6),dtype=np.intc)
coeffi=np.empty((6*nsym*27,27),dtype=np.double)
id_equi=self.symops.map_supercell(self.sposcar)
ind_cell,ind_species=_id2ind(ngrid,natoms)
# Rotation matrices for third derivatives and related quantities.
rot=np.empty((6,nsym,27,27),dtype=np.double)
for iperm in xrange(6):
for isym in xrange(nsym):
for ibasisprime in xrange(3):
for jbasisprime in xrange(3):
for kbasisprime in xrange(3):
indexijkprime=(ibasisprime*3+jbasisprime)*3+kbasisprime
for ibasis in xrange(3):
basis[0]=ibasis
for jbasis in xrange(3):
basis[1]=jbasis
for kbasis in xrange(3):
basis[2]=kbasis
indexijk=ibasis*9+jbasis*3+kbasis
ibasispermut=basis[permutations[iperm,0]]
jbasispermut=basis[permutations[iperm,1]]
kbasispermut=basis[permutations[iperm,2]]
rot[iperm,isym,indexijkprime,indexijk]=(
orth[ibasisprime,ibasispermut,isym]*
orth[jbasisprime,jbasispermut,isym]*
orth[kbasisprime,kbasispermut,isym])
rot2=rot.copy()
nonzero=np.zeros((6,nsym,27),dtype=np.intc)
for iperm in xrange(6):
for isym in xrange(nsym):
for indexijkprime in xrange(27):
rot2[iperm,isym,indexijkprime,indexijkprime]-=1.
for indexijk in xrange(27):
if fabs(rot2[iperm,isym,indexijkprime,indexijk])>1e-12:
nonzero[iperm,isym,indexijkprime]=1
else:
rot2[iperm,isym,indexijkprime,indexijk]=0.
# Scan all atom triplets (ii,jj,kk) in the supercell.
for ii in xrange(natoms):
for jj in xrange(ntot):
dist=self.dmin[ii,jj]
if dist>=self.frange:
continue
n2equi=self.nequis[ii,jj]
for kk in xrange(n2equi):
shift2all[:,kk]=shifts27[self.shifts[ii,jj,kk],:]
for kk in xrange(ntot):
dist=self.dmin[ii,kk]
if dist>=self.frange:
continue
n3equi=self.nequis[ii,kk]
for ll in xrange(n3equi):
shift3all[:,ll]=shifts27[self.shifts[ii,kk,ll],:]
d2_min=np.inf
for iaux in xrange(n2equi):
for ll in xrange(3):
car2[ll]=(shift2all[0,iaux]*lattvec[ll,0]+
shift2all[1,iaux]*lattvec[ll,1]+
shift2all[2,iaux]*lattvec[ll,2]+
coordall[ll,jj])
for jaux in xrange(n3equi):
for ll in xrange(3):
car3[ll]=(shift3all[0,jaux]*lattvec[ll,0]+
shift3all[1,jaux]*lattvec[ll,1]+
shift3all[2,jaux]*lattvec[ll,2]+
coordall[ll,kk])
d2_min=min(d2_min,
(car3[0]-car2[0])**2+
(car3[1]-car2[1])**2+
(car3[2]-car2[2])**2)
if d2_min>=frange2:
continue
# This point is only reached if there is a choice of periodic images of
# ii, jj and kk such that all pairs ii-jj, ii-kk and jj-kk are within
# the specified interaction range.
summ+=1
triplet[0]=ii
triplet[1]=jj
triplet[2]=kk
if _triplet_in_list(triplet,v_alllist,self.nalllist):
continue
# This point is only reached if the triplet is not
# equivalent to any of the triplets already considered,
# including permutations and symmetries.
self.nlist+=1
if self.nlist==self.allocsize:
self._expandlist()
v_nequi=self.nequi
v_allequilist=self.allequilist
v_transformation=self.transformation
v_transformationarray=self.transformationarray
v_transformationaux=self.transformationaux
v_nindependentbasis=self.nindependentbasis
v_independentbasis=self.independentbasis
v_llist=self.llist
v_llist[0,self.nlist-1]=ii
v_llist[1,self.nlist-1]=jj
v_llist[2,self.nlist-1]=kk
v_nequi[self.nlist-1]=0
coeffi[:,:]=0.
nnonzero=0
# Scan the six possible permutations of triplet (ii,jj,kk).
for iperm in xrange(6):
triplet_perm[0]=triplet[permutations[iperm,0]]
triplet_perm[1]=triplet[permutations[iperm,1]]
triplet_perm[2]=triplet[permutations[iperm,2]]
# Explore the effect of all symmetry operations on each of
# the permuted triplets.
for isym in xrange(nsym):
triplet_sym[0]=id_equi[isym,triplet_perm[0]]
triplet_sym[1]=id_equi[isym,triplet_perm[1]]
triplet_sym[2]=id_equi[isym,triplet_perm[2]]
for ll in xrange(3):
vec1[ll]=ind_cell[ll,id_equi[isym,triplet_perm[0]]]
vec2[ll]=ind_cell[ll,id_equi[isym,triplet_perm[1]]]
vec3[ll]=ind_cell[ll,id_equi[isym,triplet_perm[2]]]
# Choose a displaced version of triplet_sym chosen so that
# atom 0 is always in the first unit cell.
if not vec1[0]==vec1[1]==vec1[2]==0:
for ll in xrange(3):
vec3[ll]=(vec3[ll]-vec1[ll])%ngrid[ll]
vec2[ll]=(vec2[ll]-vec1[ll])%ngrid[ll]
vec1[ll]=0
ispecies1=ind_species[id_equi[isym,triplet_perm[0]]]
ispecies2=ind_species[id_equi[isym,triplet_perm[1]]]
ispecies3=ind_species[id_equi[isym,triplet_perm[2]]]
triplet_sym[0]=_ind2id(vec1,ispecies1,ngrid,natoms)
triplet_sym[1]=_ind2id(vec2,ispecies2,ngrid,natoms)
triplet_sym[2]=_ind2id(vec3,ispecies3,ngrid,natoms)
# If the permutation+symmetry operation changes the triplet into
# an as-yet-unseen image, add it to the list of equivalent triplets
# and fill the transformation array accordingly.
if (iperm==0 and isym==0) or not (
_triplets_are_equal(triplet_sym,triplet) or
_triplet_in_list(triplet_sym,equilist,v_nequi[self.nlist-1])):
v_nequi[self.nlist-1]+=1
for ll in xrange(3):
equilist[ll,v_nequi[self.nlist-1]-1]=triplet_sym[ll]
v_allequilist[ll,v_nequi[self.nlist-1]-1,
self.nlist-1]=triplet_sym[ll]
self.nalllist+=1
if self.nalllist==self.allallocsize:
self._expandalllist()
v_alllist=self.alllist
for ll in xrange(3):
v_alllist[ll,self.nalllist-1]=triplet_sym[ll]
for iaux in xrange(27):
for jaux in xrange(27):
v_transformation[iaux,jaux,v_nequi[self.nlist-1]-1,
self.nlist-1]=rot[iperm,isym,iaux,jaux]
# If the permutation+symmetry operation amounts to the identity,
# add a row to the coefficient matrix.
if _triplets_are_equal(triplet_sym,triplet):
for indexijkprime in xrange(27):
if nonzero[iperm,isym,indexijkprime]:
for ll in xrange(27):
coeffi[nnonzero,ll]=rot2[iperm,isym,indexijkprime,ll]
nnonzero+=1
coeffi_reduced=np.zeros((max(nnonzero,27),27),dtype=np.double)
for iaux in xrange(nnonzero):
for jaux in xrange(27):
coeffi_reduced[iaux,jaux]=coeffi[iaux,jaux]
# Obtain a set of independent IFCs for this triplet equivalence class.
b,independent=gaussian(coeffi_reduced)
for iaux in xrange(27):
for jaux in xrange(27):
v_transformationaux[iaux,jaux,self.nlist-1]=b[iaux,jaux]
v_nindependentbasis[self.nlist-1]=independent.shape[0]
for ll in xrange(independent.shape[0]):
v_independentbasis[ll,self.nlist-1]=independent[ll]
v_transformationarray[:,:,:,:]=0.
for ii in xrange(self.nlist):
for jj in xrange(v_nequi[ii]):
for kk in xrange(27):
for ll in xrange(v_nindependentbasis[ii]):
for iaux in xrange(27):
v_transformationarray[kk,ll,jj,ii]+=(
v_transformation[kk,iaux,jj,ii]*
v_transformationaux[iaux,ll,ii])
for kk in xrange(27):
for ll in xrange(27):
if fabs(v_transformationarray[kk,ll,jj,ii])<1e-12:
v_transformationarray[kk,ll,jj,ii]=0.
def build_list4(self):
"""
Build a list of 4-uples from the results of the reduction.
"""
cdef int ii,jj,ll,mm,nn
cdef list list6,nruter
list6=[]
for ii in xrange(self.nlist):
for jj in xrange(self.nindependentbasis[ii]):
ll=self.independentbasis[jj,ii]//9
mm=(self.independentbasis[jj,ii]%9)//3
nn=self.independentbasis[jj,ii]%3
list6.append((ll,self.llist[0,ii],
mm,self.llist[1,ii],
nn,self.llist[2,ii]))
nruter=[]
for i in list6:
fournumbers=(i[1],i[3],i[0],i[2])
if fournumbers not in nruter:
nruter.append(fournumbers)
return nruter
DEF EPS=1e-10
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef tuple gaussian(double[:,:] a):
"""
Specialized version of Gaussian elimination.
"""
cdef int i,j,k,irow
cdef int row,col,ndependent,nindependent
cdef double tmp
cdef int[:] dependent,independent
row=a.shape[0]
col=a.shape[1]
dependent=np.empty(col,dtype=np.intc)
independent=np.empty(col,dtype=np.intc)
b=np.zeros((col,col),dtype=np.double)
irow=0
ndependent=0
nindependent=0
for k in xrange(min(row,col)):
for i in xrange(row):
if fabs(a[i,k])<EPS:
a[i,k]=0.