forked from wannier-developers/wannier90
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.F90
1252 lines (1032 loc) · 37.3 KB
/
utility.F90
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
!-*- mode: F90 -*-!
!------------------------------------------------------------!
! This file is distributed as part of the Wannier90 code and !
! under the terms of the GNU General Public License. See the !
! file `LICENSE' in the root directory of the Wannier90 !
! distribution, or http://www.gnu.org/copyleft/gpl.txt !
! !
! The webpage of the Wannier90 code is www.wannier.org !
! !
! The Wannier90 code is hosted on GitHub: !
! !
! https://github.com/wannier-developers/wannier90 !
!------------------------------------------------------------!
! !
! w90_utility: blas wrappers and other basic routines !
! !
!------------------------------------------------------------!
module w90_utility
!! Module contains lots of useful general routines
use w90_constants, only: dp
use w90_comms, only: w90_comm_type
implicit none
private
public :: utility_cart_to_frac
public :: utility_commutator_diag
public :: utility_compar
public :: utility_det3
public :: utility_diagonalize
public :: utility_frac_to_cart
public :: utility_im_tr
public :: utility_im_tr_prod
public :: utility_inv2
public :: utility_inv3
public :: utility_inverse_mat
public :: utility_lowercase
public :: utility_matmul_diag
public :: utility_metric
public :: utility_recip_lattice
public :: utility_recip_lattice_base
public :: utility_re_tr
public :: utility_re_tr_prod
public :: utility_rotate
public :: utility_rotate_diag
public :: utility_rotate_new
public :: utility_string_to_coord
public :: utility_strip
public :: utility_translate_home
public :: utility_w0gauss
public :: utility_w0gauss_vec
public :: utility_wgauss
public :: utility_zdotu
public :: utility_zgemm
public :: utility_zgemmm
public :: utility_zgemm_new
contains
!================================================!
subroutine utility_zgemm(c, a, transa, b, transb, n)
!================================================!
!
!! Return matrix product of complex n x n matrices a and b:
!!
!! C = Op(A) Op(B)
!!
!! transa = 'N' ==> Op(A) = A
!! transa = 'T' ==> Op(A) = transpose(A)
!! transa = 'C' ==> Op(A) = congj(transpose(A))
!!
!! similarly for B
!
!================================================!
use w90_constants, only: cmplx_0, cmplx_1
implicit none
integer, intent(in) :: n
character(len=1), intent(in) :: transa
character(len=1), intent(in) :: transb
complex(kind=dp), intent(in) :: a(n, n)
complex(kind=dp), intent(in) :: b(n, n)
complex(kind=dp), intent(out) :: c(n, n)
call zgemm(transa, transb, n, n, n, cmplx_1, a, n, b, n, cmplx_0, c, n)
return
end subroutine utility_zgemm
!================================================
function utility_det3(A)
!================================================!
!
! Return determinant of a 3x3 matrix A
!
!================================================
real(kind=dp), intent(in) :: a(3, 3)
real(kind=dp) :: utility_det3
utility_det3 = A(1, 1)*(A(2, 2)*A(3, 3) - A(2, 3)*A(3, 2)) + &
A(1, 2)*(A(2, 3)*A(3, 1) - A(2, 1)*A(3, 3)) + &
A(1, 3)*(A(2, 1)*A(3, 2) - A(2, 2)*A(3, 1))
return
end function utility_det3
!================================================!
subroutine utility_zgemm_new(a, b, c, transa_opt, transb_opt)
!=============================================================!
! !
! Return matrix product of complex matrices a and b: !
! !
! C = Op(A) Op(B) !
! !
! transa = 'N' ==> Op(A) = A !
! transa = 'T' ==> Op(A) = transpose(A) !
! transa = 'C' ==> Op(A) = congj(transpose(A)) !
! !
! similarly for B !
! !
! Due to the use of assumed shape arrays, this routine is a !
! safer and more general replacement for the above routine !
! utility_zgemm. Consider removing utility_zgemm and using !
! utility_zgemm_new throughout. !
! !
!=============================================================!
use w90_constants, only: cmplx_0, cmplx_1
implicit none
complex(kind=dp), intent(in) :: a(:, :)
complex(kind=dp), intent(in) :: b(:, :)
complex(kind=dp), intent(out) :: c(:, :)
character(len=1), intent(in), optional :: transa_opt
character(len=1), intent(in), optional :: transb_opt
integer :: m, n, k
character(len=1) :: transa, transb
transa = 'N'
transb = 'N'
if (present(transa_opt)) transa = transa_opt
if (present(transb_opt)) transb = transb_opt
! m ... number of rows in Op(A) and C
! n ... number of columns in Op(B) and C
! k ... number of columns in Op(A) resp. rows in Op(B)
m = size(c, 1)
n = size(c, 2)
if (transa /= 'N') then
k = size(a, 1)
else
k = size(a, 2)
end if
call zgemm(transa, transb, m, n, k, cmplx_1, a, size(a, 1), b, size(b, 1), cmplx_0, c, m)
end subroutine utility_zgemm_new
!================================================!
function utility_zdotu(a, b)
complex(kind=dp), intent(in) :: a(:), b(:)
complex(kind=dp) :: utility_zdotu
utility_zdotu = sum(a*b)
return
end function utility_zdotu
!================================================!
subroutine utility_zgemmm(a, transa, b, transb, c, transc, prod1, eigval, prod2)
!================================================!
! Returns the complex matrix-matrix-matrix product
! --> prod1 = op(a).op(b).op(c),
! where op(a/b/c) are defined according to transa/transb/transc
! (see also documentation of utility_zgemm above)
!
! If eigval and prod2 are present, also
! --> prod2 = op(a).diag(eigval).op(b).op(c)
! is returned.
!================================================!
complex(kind=dp), intent(in) :: a(:, :), b(:, :), c(:, :)
character(len=1), intent(in) :: transa, transb, transc
real(kind=dp), intent(in), optional :: eigval(:)
complex(kind=dp), intent(out), optional :: prod1(:, :), prod2(:, :)
complex(kind=dp), allocatable :: tmp(:, :)
integer :: nb, mc, i, j
! query matrix sizes
! naming convention:
! matrix op(a) [resp. op(b) and op(c)] is of size na x ma [resp. nb x mb and nc x mc]
! only nb (=ma) and mc are explicitly needed
if (transb /= 'N') then
nb = size(b, 2)
else
nb = size(b, 1)
end if
if (transc /= 'N') then
mc = size(c, 1)
else
mc = size(c, 2)
end if
! tmp = op(b).op(c)
allocate (tmp(nb, mc))
call utility_zgemm_new(b, c, tmp, transb, transc)
! prod1 = op(a).tmp
if (present(prod1)) then
call utility_zgemm_new(a, tmp, prod1, transa, 'N')
end if
if (present(prod2) .and. present(eigval)) then
! tmp = diag(eigval).tmp
do j = 1, mc
do i = 1, nb
tmp(i, j) = eigval(i)*tmp(i, j)
enddo
enddo
! prod2 = op(a).tmp
call utility_zgemm_new(a, tmp, prod2, transa, 'N')
end if
end subroutine
!================================================
subroutine utility_inv3(a, b, det)
!================================================!
!
!! Return in b the adjoint of the 3x3 matrix a, and its
!! determinant.
!! The inverse is defined as the adjoint divided by the
!! determinant, so that inverse(a) = b/det
!
!================================================
implicit none
real(kind=dp), intent(in) :: a(3, 3)
real(kind=dp), intent(out) :: b(3, 3)
real(kind=dp), intent(out) :: det
b(1, 1) = a(2, 2)*a(3, 3) - a(3, 2)*a(2, 3)
b(1, 2) = a(2, 3)*a(3, 1) - a(3, 3)*a(2, 1)
b(1, 3) = a(2, 1)*a(3, 2) - a(3, 1)*a(2, 2)
b(2, 1) = a(3, 2)*a(1, 3) - a(1, 2)*a(3, 3)
b(2, 2) = a(3, 3)*a(1, 1) - a(1, 3)*a(3, 1)
b(2, 3) = a(3, 1)*a(1, 2) - a(1, 1)*a(3, 2)
b(3, 1) = a(1, 2)*a(2, 3) - a(2, 2)*a(1, 3)
b(3, 2) = a(1, 3)*a(2, 1) - a(2, 3)*a(1, 1)
b(3, 3) = a(1, 1)*a(2, 2) - a(2, 1)*a(1, 2)
det = a(1, 1)*b(1, 1) + a(1, 2)*b(1, 2) + a(1, 3)*b(1, 3)
return
end subroutine utility_inv3
!================================================
subroutine utility_inv2(a, b, det)
!================================================!
!
!! Return in b the adjoint of the 2x2 matrix
!! a, together with the determinant of a.
!! The inverse is defined as the adjoind divided by the
!! determinant, so that inverse(a) = b/det
!
!================================================
implicit none
real(kind=dp), intent(in) :: a(2, 2)
real(kind=dp), intent(out) :: b(2, 2)
real(kind=dp), intent(out) :: det
det = a(1, 1)*a(2, 2) - a(1, 2)*a(2, 1)
b(1, 1) = a(2, 2)
b(1, 2) = -a(1, 2)
b(2, 1) = -a(2, 1)
b(2, 2) = a(1, 1)
return
end subroutine utility_inv2
!================================================
subroutine utility_inverse_mat(a, b)
!================================================!
!
!! Return in b int inverse of a. Uses utility_inv3
!
!================================================
implicit none
real(kind=dp), intent(in) :: a(3, 3)
real(kind=dp), intent(out) :: b(3, 3)
real(kind=dp) :: det
call utility_inv3(a, b, det)
b = b/det
return
end subroutine utility_inverse_mat
!================================================
subroutine utility_recip_lattice_base(real_lat, recip_lat, volume)
!================================================!
!
!! Calculates the reciprical lattice vectors and the cell volume
!
!================================================
use w90_constants, only: dp, twopi, eps5
implicit none
real(kind=dp), intent(in) :: real_lat(3, 3)
real(kind=dp), intent(out) :: recip_lat(3, 3)
real(kind=dp), intent(out) :: volume
call utility_inv3(real_lat, recip_lat, volume)
if (abs(volume) > eps5) then
recip_lat = twopi*recip_lat/volume
volume = abs(volume)
endif
return
end subroutine utility_recip_lattice_base
subroutine utility_recip_lattice(real_lat, recip_lat, volume, error, comm)
!================================================!
!
!! Calculates the reciprical lattice vectors and the cell volume
!! Includes a check that the volume isn't almost 0
!! Use the first time the lattice is read to check its sensible
!
!================================================
use w90_constants, only: dp, twopi, eps5
use w90_error, only: w90_error_type, set_error_fatal
implicit none
type(w90_error_type), allocatable :: error
real(kind=dp), intent(in) :: real_lat(3, 3)
real(kind=dp), intent(out) :: recip_lat(3, 3)
real(kind=dp), intent(out) :: volume
type(w90_comm_type), intent(in) :: comm
call utility_recip_lattice_base(real_lat, recip_lat, volume)
if (abs(volume) < eps5) then
call set_error_fatal(error, ' Found almost zero Volume in utility_recip_lattice', comm)
return
end if
return
end subroutine utility_recip_lattice
!================================================
subroutine utility_compar(a, b, ifpos, ifneg)
!================================================!
!
!! Compares two vectors
!
!================================================
use w90_constants, only: eps8
implicit none
real(kind=dp), intent(in) :: a(3)
real(kind=dp), intent(in) :: b(3)
integer, intent(out) :: ifpos, ifneg
real(kind=dp) :: rrp, rrm
rrp = (a(1) - b(1))**2 + (a(2) - b(2))**2 + (a(3) - b(3))**2
rrm = (a(1) + b(1))**2 + (a(2) + b(2))**2 + (a(3) + b(3))**2
ifpos = 0
if (abs(rrp) .lt. eps8) ifpos = 1
ifneg = 0
if (abs(rrm) .lt. eps8) ifneg = 1
return
end subroutine utility_compar
!================================================
subroutine utility_metric(lattice, metric)
!================================================!
!
!! Calculate the metric for a lattice
!
!================================================
implicit none
real(kind=dp), intent(in) :: lattice(3, 3)
real(kind=dp), intent(out) :: metric(3, 3)
integer :: i, j, l
metric = 0.0_dp
do j = 1, 3
do i = 1, j
do l = 1, 3
metric(i, j) = metric(i, j) + lattice(i, l)*lattice(j, l)
enddo
if (i .lt. j) then
metric(j, i) = metric(i, j)
endif
enddo
enddo
end subroutine utility_metric
!================================================
subroutine utility_frac_to_cart(frac, cart, real_lat)
!================================================!
!
!! Convert from fractional to Cartesian coordinates
!
!================================================
implicit none
real(kind=dp), intent(in) :: real_lat(3, 3)
real(kind=dp), intent(in) :: frac(3)
real(kind=dp), intent(out) :: cart(3)
integer :: i
do i = 1, 3
cart(i) = real_lat(1, i)*frac(1) + real_lat(2, i)*frac(2) + real_lat(3, i)*frac(3)
end do
return
end subroutine utility_frac_to_cart
!================================================
subroutine utility_cart_to_frac(cart, frac, inv_lat)
!================================================!
!
!! Convert from Cartesian to fractional coordinates
!
!================================================
use w90_constants, only: twopi
implicit none
real(kind=dp), intent(in) :: inv_lat(3, 3)
real(kind=dp), intent(out) :: frac(3)
real(kind=dp), intent(in) :: cart(3)
integer :: i
do i = 1, 3
frac(i) = inv_lat(i, 1)*cart(1) + inv_lat(i, 2)*cart(2) + inv_lat(i, 3)*cart(3)
end do
return
end subroutine utility_cart_to_frac
!================================================!
function utility_strip(string)!
!================================================!
!
!! Strips string of all blank spaces
!
!================================================!
use w90_constants, only: maxlen
implicit none
character(len=*), intent(in) :: string
character(len=maxlen) :: utility_strip
integer :: ispc, ipos, ilett, icount
! Initialise
utility_strip = repeat(' ', maxlen)
ispc = ichar(' ')
icount = 0
do ipos = 1, len(string)
ilett = ichar(string(ipos:ipos))
if (ilett .ne. ispc) then
icount = icount + 1
utility_strip(icount:icount) = string(ipos:ipos)
endif
enddo
utility_strip = trim(utility_strip)
return
end function utility_strip
!================================================!
function utility_lowercase(string)!
!================================================!
!
!! Takes a string and converts to
!! lowercase characters
!
!================================================!
use w90_constants, only: maxlen
implicit none
character(len=*), intent(in) :: string
character(len=maxlen) :: utility_lowercase
integer :: iA, iZ, idiff, ipos, ilett
iA = ichar('A')
iZ = ichar('Z')
idiff = iZ - ichar('z')
utility_lowercase = string
do ipos = 1, len(string)
ilett = ichar(string(ipos:ipos))
if ((ilett .ge. iA) .and. (ilett .le. iZ)) &
utility_lowercase(ipos:ipos) = char(ilett - idiff)
enddo
utility_lowercase = trim(adjustl(utility_lowercase))
return
end function utility_lowercase
!================================================!
subroutine utility_string_to_coord(string_tmp, outvec, error, comm)
!================================================!
!
!! Takes a string in the form 0.0,1.0,0.5
!! and returns an array of the real num
!
!================================================!
use w90_constants, only: maxlen
use w90_error, only: w90_error_type, set_error_input
implicit none
character(len=maxlen), intent(in) :: string_tmp
real(kind=dp), intent(out) :: outvec(3)
type(w90_error_type), allocatable, intent(out) :: error
type(w90_comm_type), intent(in) :: comm
integer :: pos
character(len=maxlen) :: ctemp
character(len=maxlen) :: ctemp2
ctemp = string_tmp
pos = index(ctemp, ',')
if (pos <= 0) then
call set_error_input(error, 'utility_string_to_coord: Problem reading string into real number '//trim(string_tmp), comm)
return
endif
ctemp2 = ctemp(1:pos - 1)
read (ctemp2, *, err=100, end=100) outvec(1)
ctemp = ctemp(pos + 1:)
pos = index(ctemp, ',')
ctemp2 = ctemp(1:pos - 1)
read (ctemp2, *, err=100, end=100) outvec(2)
ctemp = ctemp(pos + 1:)
read (ctemp, *, err=100, end=100) outvec(3)
return
100 call set_error_input(error, 'utility_string_to_coord: Problem reading string into real number '//trim(string_tmp), comm)
return ! this is kinda ugly, JJ
end subroutine utility_string_to_coord
!================================================!
subroutine utility_translate_home(vec, real_lat)
!================================================!
!
!! Translate a vector to the home unit cell
!
!================================================!
implicit none
real(kind=dp), intent(inout) :: vec(3)
real(kind=dp), intent(in) :: real_lat(3, 3)
! <<<local variables>>>
real(kind=dp) :: recip_lat(3, 3), volume
integer :: ind
real(kind=dp) :: r_home(3), r_frac(3)
real(kind=dp) :: shift
r_home = 0.0_dp; r_frac = 0.0_dp
! Cartesian --> fractional
call utility_recip_lattice_base(real_lat, recip_lat, volume)
call utility_cart_to_frac(vec, r_frac, recip_lat)
! Rationalise to interval [0,1]
do ind = 1, 3
if (r_frac(ind) .lt. 0.0_dp) then
shift = real(ceiling(abs(r_frac(ind))), kind=dp)
r_frac(ind) = r_frac(ind) + shift
endif
if (r_frac(ind) .gt. 1.0_dp) then
shift = -real(int(r_frac(ind)), kind=dp)
r_frac(ind) = r_frac(ind) + shift
endif
enddo
! Fractional --> Cartesian
call utility_frac_to_cart(r_frac, r_home, real_lat)
vec = r_home
return
end subroutine utility_translate_home
!================================================!
subroutine utility_diagonalize(mat, dim, eig, rot, error, comm)
!================================================!
!
!! Diagonalize the dim x dim hermitian matrix 'mat' and
!! return the eigenvalues 'eig' and the unitary rotation 'rot'
!
!================================================!
use w90_constants, only: dp, cmplx_0
use w90_error, only: w90_error_type, set_error_fatal
integer, intent(in) :: dim
complex(kind=dp), intent(in) :: mat(dim, dim)
real(kind=dp), intent(out) :: eig(dim)
complex(kind=dp), intent(out) :: rot(dim, dim)
type(w90_error_type), allocatable, intent(out) :: error
type(w90_comm_type), intent(in) :: comm
complex(kind=dp) :: mat_pack((dim*(dim + 1))/2), cwork(2*dim)
real(kind=dp) :: rwork(7*dim)
integer :: i, j, info, nfound, iwork(5*dim), ifail(dim)
character(len=120) :: errormsg
do j = 1, dim
do i = 1, j
mat_pack(i + ((j - 1)*j)/2) = mat(i, j)
enddo
enddo
rot = cmplx_0; eig = 0.0_dp; cwork = cmplx_0; rwork = 0.0_dp; iwork = 0
call ZHPEVX('V', 'A', 'U', dim, mat_pack, 0.0_dp, 0.0_dp, 0, 0, -1.0_dp, &
nfound, eig(1), rot, dim, cwork, rwork, iwork, ifail, info)
if (info < 0) then
write (errormsg, '(a,i3,a)') 'Error in utility_diagonalize: THE ', -info, &
' ARGUMENT OF ZHPEVX HAD AN ILLEGAL VALUE'
call set_error_fatal(error, errormsg, comm)
return
endif
if (info > 0) then
write (errormsg, '(a,i3,a)') 'Error in utility_diagonalize: ', info, &
' EIGENVECTORS FAILED TO CONVERGE'
call set_error_fatal(error, errormsg, comm)
return
endif
end subroutine utility_diagonalize
!================================================!
function utility_rotate(mat, rot, dim)
!================================================!
!
!! Rotates the dim x dim matrix 'mat' according to
!! (rot)^dagger.mat.rot, where 'rot' is a unitary matrix
!
!================================================!
use w90_constants, only: dp
integer :: dim
complex(kind=dp) :: utility_rotate(dim, dim)
complex(kind=dp) :: mat(dim, dim)
complex(kind=dp) :: rot(dim, dim)
utility_rotate = matmul(matmul(transpose(conjg(rot)), mat), rot)
end function utility_rotate
!================================================!
subroutine utility_rotate_new(mat, rot, N, reverse)
!================================================!
!
! Rotates the N x N matrix 'mat' according to
! * (rot)^dagger.mat.rot (reverse = .false. or not present) OR
! * rot.mat.(rot)^dagger (reverse = .true.),
! where 'rot' is a unitary matrix.
! The matrix 'mat' is overwritten.
!
!================================================!
use w90_constants, only: dp
integer, intent(in) :: N
logical, optional, intent(in) :: reverse
complex(kind=dp), intent(inout) :: mat(N, N)
complex(kind=dp), intent(in) :: rot(N, N)
complex(kind=dp) :: tmp(N, N)
logical :: rev
if (.not. present(reverse)) then
rev = .false.
else
rev = reverse
end if
if (rev) then
call utility_zgemm_new(rot, mat, tmp, 'N', 'C')
call utility_zgemm_new(rot, tmp, mat, 'N', 'C')
else
call utility_zgemm_new(mat, rot, tmp, 'C', 'N')
call utility_zgemm_new(tmp, rot, mat, 'C', 'N')
end if
end subroutine utility_rotate_new
!================================================!
function utility_matmul_diag(mat1, mat2, dim)
!================================================!
!
!! Computes the diagonal elements of the matrix mat1.mat2
!
!================================================!
use w90_constants, only: dp, cmplx_0
integer :: dim
complex(kind=dp) :: utility_matmul_diag(dim)
complex(kind=dp) :: mat1(dim, dim)
complex(kind=dp) :: mat2(dim, dim)
integer i, j
utility_matmul_diag = cmplx_0
do i = 1, dim
do j = 1, dim
utility_matmul_diag(i) = utility_matmul_diag(i) + mat1(i, j)*mat2(j, i)
end do
end do
end function utility_matmul_diag
!================================================!
function utility_rotate_diag(mat, rot, dim)
!================================================!
!
!! Rotates the dim x dim matrix 'mat' according to
!! (rot)^dagger.mat.rot, where 'rot' is a unitary matrix.
!! Computes only the diagonal elements of rotated matrix.
!
!================================================!
use w90_constants, only: dp
integer :: dim
complex(kind=dp) :: utility_rotate_diag(dim)
complex(kind=dp) :: mat(dim, dim)
complex(kind=dp) :: rot(dim, dim)
complex(kind=dp) :: tmp(dim, dim)
call utility_zgemm_new(rot, mat, tmp, 'C', 'N')
utility_rotate_diag = utility_matmul_diag(tmp, rot, dim)
end function utility_rotate_diag
!================================================!
function utility_commutator_diag(mat1, mat2, dim)
!================================================!
!
!! Computes diagonal elements of
!! [mat1,mat2]=mat1.mat2-mat2.mat1
!
!================================================!
use w90_constants, only: dp
integer :: dim
complex(kind=dp) :: utility_commutator_diag(dim)
complex(kind=dp) :: mat1(dim, dim)
complex(kind=dp) :: mat2(dim, dim)
utility_commutator_diag = utility_matmul_diag(mat1, mat2, dim) - utility_matmul_diag(mat2, mat1, dim)
end function utility_commutator_diag
!================================================!
function utility_re_tr_prod(a, b)
!================================================!
!
! Return Re(tr(a.b)), i.e. the real part of the
! trace of the matrix product of a and b.
!
!================================================!
use w90_constants, only: dp, cmplx_0, cmplx_i
complex(kind=dp), intent(in) :: a(:, :), b(:, :)
real(kind=dp) :: utility_re_tr_prod
real(kind=dp) :: s
integer :: i, j, n, m
n = min(size(a, 1), size(b, 2))
m = min(size(a, 2), size(b, 1))
s = 0
do i = 1, n
do j = 1, m
s = s + dble(a(i, j)*b(j, i))
end do
end do
utility_re_tr_prod = s
end function
!================================================!
function utility_im_tr_prod(a, b)
!================================================!
!
! Return Im(tr(a.b)), i.e. the imaginary part of the
! trace of the matrix product of a and b.
!
!================================================!
use w90_constants, only: dp, cmplx_0, cmplx_i
complex(kind=dp), intent(in) :: a(:, :), b(:, :)
real(kind=dp) :: utility_im_tr_prod
real(kind=dp) :: s
integer :: i, j, n, m
n = min(size(a, 1), size(b, 2))
m = min(size(a, 2), size(b, 1))
s = 0
do i = 1, n
do j = 1, m
s = s + aimag(a(i, j)*b(j, i))
end do
end do
utility_im_tr_prod = s
end function
!================================================!
function utility_re_tr(mat)
!================================================!
!
!! Real part of the trace
!
!================================================!
use w90_constants, only: dp, cmplx_0, cmplx_i
real(kind=dp) :: utility_re_tr
complex(kind=dp) :: mat(:, :)
integer :: i, mydim
complex(kind=dp) :: cdum
mydim = size(mat, 1)
cdum = cmplx_0
do i = 1, mydim
cdum = cdum + mat(i, i)
enddo
utility_re_tr = aimag(cmplx_i*cdum)
end function utility_re_tr
function utility_im_tr(mat)
!================================================!
!
!! Imaginary part of the trace
!
!================================================!
use w90_constants, only: dp, cmplx_0
real(kind=dp) :: utility_im_tr
complex(kind=dp) :: mat(:, :)
integer :: i, mydim
complex(kind=dp) :: cdum
mydim = size(mat, 1)
cdum = cmplx_0
do i = 1, mydim
cdum = cdum + mat(i, i)
enddo
utility_im_tr = aimag(cdum)
end function utility_im_tr
function utility_wgauss(x, n)
!-----------------------------------------------------------------------
!
!! this function computes the approximate theta function for the
!! given order n, at the point x.
!!
!! (n>=0) : Methfessel-Paxton case. See PRB 40, 3616 (1989).
!!
!! (n=-1 ): Cold smearing (Marzari-Vanderbilt). See PRL 82, 3296 (1999)
!! 1/2*erf(x-1/sqrt(2)) + 1/sqrt(2*pi)*exp(-(x-1/sqrt(2))**2) + 1/2
!!
!! (n=-99): Fermi-Dirac case: 1.0/(1.0+exp(-x)).
!
use w90_constants, only: dp, pi
implicit none
! arguments
real(kind=dp) :: utility_wgauss, x
!! output: the value of the function
!! input: the argument of the function
integer :: n
!! input: the order of the function
! local variables
real(kind=dp) :: a, hp, arg, hd, xp
! the coefficient a_n
! the hermitean function
! the argument of the exponential
! the hermitean function
! auxiliary variable (cold smearing)
integer :: i, ni
! counter on the n indices
! counter on 2n
!real(kind=dp), external :: gauss_freq, qe_erf
real(kind=dp), parameter :: maxarg = 200.0_dp
! maximum value for the argument of the exponential
! Fermi-Dirac smearing
if (n .eq. -99) then
if (x .lt. -maxarg) then
utility_wgauss = 0.0_dp
elseif (x .gt. maxarg) then
utility_wgauss = 1.0_dp
else
utility_wgauss = 1.00_dp/(1.00_dp + exp(-x))
endif
return
endif
! Cold smearing
if (n .eq. -1) then
xp = x - 1.00_dp/sqrt(2.00_dp)
arg = min(maxarg, xp**2)
utility_wgauss = 0.50_dp*qe_erf(xp) + 1.00_dp/sqrt(2.00_dp*pi)*exp(- &
arg) + 0.50_dp
return
endif
! Methfessel-Paxton
utility_wgauss = gauss_freq(x*sqrt(2.00_dp))
if (n .eq. 0) return
hd = 0.0_dp
arg = min(maxarg, x**2)
hp = exp(-arg)
ni = 0
a = 1.0_dp/sqrt(pi)
do i = 1, n
hd = 2.00_dp*x*hp - 2.00_dp*DBLE(ni)*hd
ni = ni + 1
a = -a/(DBLE(i)*4.00_dp)
utility_wgauss = utility_wgauss - a*hd
hp = 2.00_dp*x*hd - 2.00_dp*DBLE(ni)*hp
ni = ni + 1
enddo
return
end function utility_wgauss
function utility_w0gauss(x, n, error, comm)
!-----------------------------------------------------------------------
!
!! the derivative of utility_wgauss: an approximation to the delta function
!!
!! (n>=0) : derivative of the corresponding Methfessel-Paxton utility_wgauss
!!
!! (n=-1 ): derivative of cold smearing:
!! 1/sqrt(pi)*exp(-(x-1/sqrt(2))**2)*(2-sqrt(2)*x)
!!