forked from wannier-developers/wannier90
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisentangle.F90
4219 lines (3782 loc) · 170 KB
/
disentangle.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_disentangle: extract subspace from entangled bands !
! !
!------------------------------------------------------------!
module w90_disentangle_mod
!! This module contains the core routines to extract an optimal
!! subspace from a set of entangled bands.
implicit none
public :: dis_main
public :: setup_m_loc
contains
!================================================!
subroutine dis_main(dis_control, dis_spheres, dis_manifold, kmesh_info, kpt_latt, sitesym, &
print_output, m_matrix_orig_local, u_matrix, u_matrix_opt, eigval, &
real_lattice, omega_invariant, num_bands, num_kpts, num_wann, gamma_only, &
lsitesymmetry, stdout, timer, dist_k, error, comm)
!================================================!
!
!! Main disentanglement routine
!
!================================================!
use w90_comms, only: comms_bcast, w90_comm_type, mpirank
use w90_constants, only: dp, cmplx_0, cmplx_1
use w90_error
use w90_io, only: io_stopwatch_start, io_stopwatch_stop
use w90_sitesym, only: sitesym_replace_d_matrix_band, sitesym_symmetrize_u_matrix, &
sitesym_symmetrize_zmatrix, sitesym_dis_extract_symmetry
use w90_types, only: dis_manifold_type, kmesh_info_type, print_output_type, timer_list_type
use w90_utility, only: utility_recip_lattice_base
use w90_wannier90_types, only: dis_control_type, dis_spheres_type, sitesym_type
! arguments
integer, intent(in) :: num_bands, num_kpts, num_wann
integer, intent(in) :: stdout
integer, intent(in) :: dist_k(:)
logical, intent(in) :: lsitesymmetry
logical, intent(in) :: gamma_only
real(kind=dp), pointer, intent(in) :: eigval(:, :) ! (num_bands, num_kpts)
real(kind=dp), intent(in) :: kpt_latt(:, :)
real(kind=dp), intent(inout) :: omega_invariant
real(kind=dp), intent(in) :: real_lattice(3, 3)
complex(kind=dp), intent(inout) :: u_matrix(:, :, :) ! (num_wann, num_wann, num_kpts)
complex(kind=dp), intent(inout) :: u_matrix_opt(:, :, :) ! (num_bands, num_wann, num_kpts)
complex(kind=dp), intent(inout) :: m_matrix_orig_local(:, :, :, :) ! this is the only "m matrix" here now
type(dis_control_type), intent(inout) :: dis_control
type(dis_manifold_type), intent(inout) :: dis_manifold
type(dis_spheres_type), intent(in) :: dis_spheres
type(kmesh_info_type), intent(in) :: kmesh_info
type(print_output_type), intent(in) :: print_output
type(sitesym_type), intent(inout) :: sitesym
type(w90_comm_type), intent(in) :: comm
type(timer_list_type), intent(inout) :: timer
type(w90_error_type), allocatable, intent(out) :: error
! internal variables
real(kind=dp) :: recip_lattice(3, 3), volume
integer :: nkp, nkp2, nn, j, ierr, nkp_global
logical :: linner !! Is there a frozen window
logical :: lfrozen(num_bands, num_kpts) !! true if the i-th band inside outer window is frozen
integer :: ndimfroz(num_kpts) !! number of frozen bands at nkp-th k point
integer :: indxfroz(num_bands, num_kpts) !! number of bands inside outer window at nkp-th k point
integer :: indxnfroz(num_bands, num_kpts) !! outer-window band index for the i-th non-frozen state
complex(kind=dp), allocatable :: a_matrix(:, :, :) ! (num_bands, num_wann, num_kpts)
!! (equals 1 if it is the bottom of outer window)
real(kind=dp), allocatable :: eigval_opt(:, :) !! At input it contains a large set of eigenvalues. At
!! it is slimmed down to contain only those inside the energy window.
complex(kind=dp), allocatable :: cwb(:, :), cww(:, :)
! pllel setup
integer :: nkrank, ikg, ikl, my_node_id
integer, allocatable :: global_k(:)
logical :: on_root = .false.
my_node_id = mpirank(comm)
on_root = (my_node_id == 0)
nkrank = count(dist_k == my_node_id) ! this routine must proceed also in the case of zero k-points this rank, to ensure collective communications are matched
allocate (a_matrix(num_bands, num_wann, num_kpts), stat=ierr) ! a_matrix is local to disentangle()
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating a_matrix in dis_main', comm)
return
endif
a_matrix = u_matrix_opt ! initial projections are passed to this routine via u_matrix_opt
allocate (global_k(nkrank), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating global_k in dis_main', comm)
return
endif
global_k = huge(1); ikl = 1
do ikg = 1, num_kpts
if (dist_k(ikg) == my_node_id) then
global_k(ikl) = ikg
ikl = ikl + 1
endif
enddo
if (print_output%timing_level > 0) call io_stopwatch_start('dis: main', timer)
call utility_recip_lattice_base(real_lattice, recip_lattice, volume)
if (print_output%iprint > 0) write (stdout, '(/1x,a)') &
'*------------------------------- DISENTANGLE --------------------------------*'
! Allocate arrays
allocate (eigval_opt(num_bands, num_kpts), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating eigval_opt in dis_main', comm)
return
endif
eigval_opt(1:num_bands, 1:num_kpts) = eigval(1:num_bands, 1:num_kpts)
! Set up energy windows
if (dis_manifold%frozen_proj) then
call dis_windows_proj(dis_manifold, eigval_opt, a_matrix, m_matrix_orig_local, &
indxfroz, indxnfroz, ndimfroz, dis_manifold%nfirstwin, &
print_output%iprint, kmesh_info%nnlist, kmesh_info%nntot, num_bands, &
num_kpts, num_wann, print_output%timing_level, lfrozen, linner, &
on_root, stdout, dist_k, global_k, my_node_id, timer, error, comm)
if (allocated(error)) return
else
call dis_windows(dis_spheres, dis_manifold, eigval_opt, kpt_latt, recip_lattice, indxfroz, &
indxnfroz, ndimfroz, print_output%iprint, num_bands, num_kpts, num_wann, &
print_output%timing_level, lfrozen, linner, on_root, stdout, timer, error, comm)
if (allocated(error)) return
endif
! Construct the unitarized projection
call dis_project(a_matrix, u_matrix_opt, dis_manifold%ndimwin, dis_manifold%nfirstwin, &
num_bands, num_kpts, num_wann, print_output%timing_level, on_root, &
print_output%iprint, timer, error, stdout, comm)
if (allocated(error)) return
! If there is an inner window, need to modify projection procedure
! (Sec. III.G SMV)
if (linner) then
if (lsitesymmetry) then
call set_error_fatal(error, 'in symmetry-adapted mode, frozen window not implemented yet', &
comm)
return
endif
if (print_output%iprint > 0) write (stdout, '(3x,a)') 'Using an inner window (linner = T)'
call dis_proj_froz(u_matrix_opt, indxfroz, ndimfroz, dis_manifold%ndimwin, &
print_output%iprint, num_bands, num_kpts, num_wann, &
print_output%timing_level, lfrozen, on_root, timer, error, stdout, comm)
if (allocated(error)) return
else
if (print_output%iprint > 0) write (stdout, '(3x,a)') 'No inner window (linner = F)'
endif
! Debug
call internal_check_orthonorm(u_matrix_opt, dis_manifold%ndimwin, num_kpts, num_wann, &
print_output%timing_level, on_root, timer, error, stdout, comm)
if (allocated(error)) return
! For frozen_proj, these are done inside dis_windows_proj()
if (.not. dis_manifold%frozen_proj) then
! Slim down the original Mmn(k,b)
call internal_slim_m(m_matrix_orig_local, dis_manifold%ndimwin, dis_manifold%nfirstwin, &
kmesh_info%nnlist, kmesh_info%nntot, num_bands, print_output%timing_level, &
timer, dist_k, global_k, error, comm)
if (allocated(error)) return
dis_manifold%lwindow = .false.
do nkp = 1, num_kpts
do j = dis_manifold%nfirstwin(nkp), dis_manifold%nfirstwin(nkp) + dis_manifold%ndimwin(nkp) - 1
dis_manifold%lwindow(j, nkp) = .true.
end do
end do
endif
if (lsitesymmetry) then
call sitesym_symmetrize_u_matrix(sitesym, u_matrix_opt, num_bands, num_bands, num_kpts, &
num_wann, stdout, error, comm, dis_manifold%lwindow)
if (allocated(error)) return
endif
!RS: calculate initial U_{opt}(Rk) from U_{opt}(k)
! Extract the optimally-connected num_wann-dimensional subspaces
if (gamma_only) then
call dis_extract_gamma(dis_control, kmesh_info, print_output, dis_manifold, &
m_matrix_orig_local, u_matrix_opt, eigval_opt, omega_invariant, &
indxnfroz, ndimfroz, num_bands, num_kpts, num_wann, timer, error, &
stdout, comm)
if (allocated(error)) return
else
call dis_extract(dis_control, kmesh_info, sitesym, print_output, dis_manifold, &
m_matrix_orig_local, u_matrix_opt, eigval_opt, omega_invariant, indxnfroz, &
ndimfroz, my_node_id, num_bands, num_kpts, num_wann, lsitesymmetry, timer, &
nkrank, global_k, error, stdout, comm)
if (allocated(error)) return
end if
! Allocate workspace
allocate (cwb(num_wann, num_bands), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating cwb in dis_main', comm)
return
endif
allocate (cww(num_wann, num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating cww in dis_main', comm)
return
endif
! Find the num_wann x num_wann overlap matrices between
! the basis states of the optimal subspaces
do nkp = 1, nkrank
nkp_global = global_k(nkp)
do nn = 1, kmesh_info%nntot
nkp2 = kmesh_info%nnlist(nkp_global, nn)
call zgemm('C', 'N', num_wann, dis_manifold%ndimwin(nkp2), dis_manifold%ndimwin(nkp_global), &
cmplx_1, u_matrix_opt(:, :, nkp_global), num_bands, &
m_matrix_orig_local(:, :, nn, nkp), num_bands, cmplx_0, cwb, num_wann)
call zgemm('N', 'N', num_wann, num_wann, dis_manifold%ndimwin(nkp2), cmplx_1, cwb, &
num_wann, u_matrix_opt(:, :, nkp2), num_bands, cmplx_0, cww, num_wann)
m_matrix_orig_local(1:num_wann, 1:num_wann, nn, nkp) = cww(:, :)
enddo
enddo
! Find the initial u_matrix
if (lsitesymmetry) call sitesym_replace_d_matrix_band(sitesym, num_wann) !RS: replace d_matrix_band here
if (gamma_only) then
call internal_find_u_gamma(a_matrix, u_matrix, u_matrix_opt, dis_manifold%ndimwin, num_wann, &
print_output%timing_level, stdout, timer, error, comm)
if (allocated(error)) return
else
call internal_find_u(sitesym, a_matrix, u_matrix, u_matrix_opt, dis_manifold%ndimwin, &
num_bands, num_kpts, num_wann, print_output%timing_level, &
lsitesymmetry, on_root, stdout, timer, error, comm)
if (allocated(error)) return
end if
!zero the unused elements of u_matrix_opt (just in case...)
do nkp = 1, num_kpts
do j = 1, num_wann
if (dis_manifold%ndimwin(nkp) < num_bands) &
u_matrix_opt(dis_manifold%ndimwin(nkp) + 1:, j, nkp) = cmplx_0
enddo
enddo
! Deallocate workspace
deallocate (cww, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error in deallocating cww in dis_main', comm)
return
endif
deallocate (cwb, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error in deallocating cwb in dis_main', comm)
return
endif
deallocate (global_k, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error in deallocating global_k in dis_main', comm)
return
endif
deallocate (a_matrix, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error in deallocating a_matrix in dis_main', comm)
return
endif
if (print_output%timing_level > 0 .and. on_root) call io_stopwatch_stop('dis: main', timer)
return
!================================================!
end subroutine dis_main
subroutine setup_m_loc(kmesh_info, print_output, m_matrix_local, m_matrix_orig_local, u_matrix, &
num_bands, num_kpts, num_wann, timer, dist_k, error, comm)
!================================================!
!
! map m_matrix_orig_local to m_matrix_local
! at entry, m_matrix_local is not allocated
!
!================================================!
use w90_comms, only: w90_comm_type, mpirank
use w90_constants, only: dp, cmplx_0, cmplx_1
use w90_error
use w90_io, only: io_stopwatch_start, io_stopwatch_stop
use w90_types, only: kmesh_info_type, print_output_type, timer_list_type
! arguments
integer, intent(in) :: num_bands, num_kpts, num_wann
integer, intent(in) :: dist_k(:)
complex(kind=dp), intent(in) :: u_matrix(:, :, :) ! (num_wann, num_wann, num_kpts) -- full array duplicated on all ranks
complex(kind=dp), intent(in) :: m_matrix_orig_local(:, :, :, :) ! (num_bands, num_bands, nntot, num_kpts) -- only local kpts
complex(kind=dp), intent(inout) :: m_matrix_local(:, :, :, :) ! (num_wann, num_wann, nntot, rank_kpts) -- only local kpts
type(kmesh_info_type), intent(in) :: kmesh_info
type(print_output_type), intent(in) :: print_output
type(w90_comm_type), intent(in) :: comm
type(timer_list_type), intent(inout) :: timer
type(w90_error_type), allocatable, intent(out) :: error
! internal variables
complex(kind=dp), allocatable :: cwb(:, :), cww(:, :)
integer :: nkp, nkp2, nn, ierr, nkp_global, nkrank
integer, allocatable :: global_k(:)
integer :: ikg, ikl, my_node_id
if (print_output%timing_level > 1) call io_stopwatch_start('dis: splitm', timer)
! local-global k index mapping
my_node_id = mpirank(comm)
nkrank = count(dist_k == my_node_id)
allocate (global_k(nkrank), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating global_k in dis_main', comm)
return
endif
global_k = huge(1); ikl = 1
do ikg = 1, num_kpts
if (dist_k(ikg) == my_node_id) then
global_k(ikl) = ikg ! global [1,num_kpts] index corresponding to local [1,nk_this_node] index
ikl = ikl + 1
endif
enddo
allocate (cwb(num_wann, num_bands), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating cwb in dis_main', comm)
return
endif
allocate (cww(num_wann, num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating cww in dis_main', comm)
return
endif
do nkp = 1, nkrank
nkp_global = global_k(nkp)
do nn = 1, kmesh_info%nntot
nkp2 = kmesh_info%nnlist(nkp_global, nn)
call zgemm('C', 'N', num_wann, num_wann, num_wann, cmplx_1, u_matrix(:, :, nkp_global), &
num_wann, m_matrix_orig_local(:, :, nn, nkp), num_bands, cmplx_0, cwb, num_wann)
call zgemm('N', 'N', num_wann, num_wann, num_wann, cmplx_1, cwb, num_wann, &
u_matrix(:, :, nkp2), num_wann, cmplx_0, cww, num_wann)
m_matrix_local(1:num_wann, 1:num_wann, nn, nkp) = cww(:, :)
enddo
enddo
deallocate (cwb)
deallocate (cww)
deallocate (global_k)
if (print_output%timing_level > 1) call io_stopwatch_stop('dis: splitm', timer)
return
!================================================!
end subroutine setup_m_loc
subroutine internal_check_orthonorm(u_matrix_opt, ndimwin, num_kpts, num_wann, timing_level, &
on_root, timer, error, stdout, comm)
!================================================!
!
!! This subroutine checks that the states in the columns of the
!! final matrix U_opt are orthonormal at every k-point, i.e.,
!! that the matrix is unitary in the sense that
!! conjg(U_opt).U_opt = 1 (but not U_opt.conjg(U_opt) = 1).
!!
!! In particular, this checks whether the projected gaussians
!! are indeed orthogonal to the frozen states, at those k-points
!! where both are present in the trial subspace.
!
!================================================!
use w90_comms, only: w90_comm_type
use w90_constants, only: dp, cmplx_0, cmplx_1
use w90_constants, only: eps8
use w90_error
use w90_io, only: io_stopwatch_start, io_stopwatch_stop
use w90_types, only: timer_list_type
implicit none
! arguments
type(timer_list_type), intent(inout) :: timer
type(w90_error_type), allocatable, intent(out) :: error
type(w90_comm_type), intent(in) :: comm
integer, intent(in) :: timing_level
integer, intent(in) :: stdout
integer, intent(in) :: num_kpts, num_wann
integer, intent(in) :: ndimwin(:) ! (num_kpts)
complex(kind=dp), intent(inout) :: u_matrix_opt(:, :, :)
logical, intent(in) :: on_root
! local variables
integer :: nkp, l, m, j
complex(kind=dp) :: ctmp
if (timing_level > 1) call io_stopwatch_start('dis: main: check_orthonorm', timer)
do nkp = 1, num_kpts
do l = 1, num_wann
do m = 1, l
ctmp = cmplx_0
do j = 1, ndimwin(nkp)
ctmp = ctmp + conjg(u_matrix_opt(j, m, nkp))*u_matrix_opt(j, l, nkp)
enddo
if (l .eq. m) then
if (abs(ctmp - cmplx_1) .gt. eps8) then
if (on_root) then
write (stdout, '(3i6,2f16.12)') nkp, l, m, ctmp
write (stdout, '(1x,a)') &
'The trial orbitals for disentanglement are not orthonormal'
endif
call set_error_fatal(error, 'Error in dis_main: orthonormal error 1', comm)
return
endif
else
if (abs(ctmp) .gt. eps8) then
if (on_root) then
write (stdout, '(3i6,2f16.12)') nkp, l, m, ctmp
write (stdout, '(1x,a)') &
'The trial orbitals for disentanglement are not orthonormal'
endif
call set_error_fatal(error, 'Error in dis_main: orthonormal error 2', comm)
return
endif
endif
enddo
enddo
enddo
if (timing_level > 1) call io_stopwatch_stop('dis: main: check_orthonorm', timer)
return
!================================================!
end subroutine internal_check_orthonorm
subroutine internal_slim_m(m_matrix_orig_local, ndimwin, nfirstwin, nnlist, nntot, num_bands, &
timing_level, timer, dist_k, global_k, error, comm)
!================================================!
!
!! This subroutine slims down the original Mmn(k,b), removing
!! rows and columns corresponding to u_nks that fall outside
!! the outer energy window.
!
!================================================!
use w90_comms, only: w90_comm_type, mpirank
use w90_constants, only: dp, cmplx_0
use w90_error
use w90_io, only: io_stopwatch_start, io_stopwatch_stop
use w90_types, only: timer_list_type
implicit none
! arguments
type(w90_comm_type), intent(in) :: comm
type(timer_list_type), intent(inout) :: timer
type(w90_error_type), allocatable, intent(out) :: error
integer, intent(in) :: dist_k(:), global_k(:)
integer, intent(in) :: ndimwin(:)
integer, intent(in) :: nfirstwin(:) ! (num_kpts) index of lowest band inside outer window at nkp-th
integer, intent(in) :: nntot, nnlist(:, :) ! (num_kpts, nntot)
integer, intent(in) :: num_bands
integer, intent(in) :: timing_level
complex(kind=dp), intent(inout) :: m_matrix_orig_local(:, :, :, :)
! local variables
integer :: nkp, nkp2, nn, i, j, m, n, ierr, nkp_global, nkrank, my_node_id
complex(kind=dp), allocatable :: cmtmp(:, :)
if (timing_level > 1) call io_stopwatch_start('dis: main: slim_m', timer)
my_node_id = mpirank(comm)
nkrank = count(dist_k == my_node_id) ! number of k points this rank
allocate (cmtmp(num_bands, num_bands), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating cmtmp in dis_main', comm)
return
endif
do nkp = 1, nkrank
nkp_global = global_k(nkp)
do nn = 1, nntot
nkp2 = nnlist(nkp_global, nn)
do j = 1, ndimwin(nkp2)
n = nfirstwin(nkp2) + j - 1
do i = 1, ndimwin(nkp_global)
m = nfirstwin(nkp_global) + i - 1
cmtmp(i, j) = m_matrix_orig_local(m, n, nn, nkp)
enddo
enddo
m_matrix_orig_local(:, :, nn, nkp) = cmplx_0
do j = 1, ndimwin(nkp2)
do i = 1, ndimwin(nkp_global)
m_matrix_orig_local(i, j, nn, nkp) = cmtmp(i, j)
enddo
enddo
enddo
enddo
deallocate (cmtmp, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating cmtmp in dis_main', comm)
return
endif
if (timing_level > 1) call io_stopwatch_stop('dis: main: slim_m', timer)
return
!================================================!
end subroutine internal_slim_m
subroutine internal_find_u(sitesym, a_matrix, u_matrix, u_matrix_opt, ndimwin, num_bands, &
num_kpts, num_wann, timing_level, lsitesymmetry, on_root, stdout, &
timer, error, comm)
!================================================!
!
!! This subroutine finds the initial guess for the square unitary
!! rotation matrix u_matrix. The method is similar to Sec. III.D
!! of SMV, but with square instead of rectangular matrices:
!!
!! First find caa, the square overlap matrix <psitilde_nk|g_m>,
!! where psitilde is an eigenstate of the optimal subspace.
!!
!! Note that, contrary to what is implied in Sec. III.E of SMV,
!! this does *not* need to be computed by brute: instead we take
!! advantage of the previous computation of overlaps with the
!! same projections that are used to initiate the minimization of
!! Omega.
!!
!! Note: |psi> U_opt = |psitilde> and obviously
!! <psitilde| = (U_opt)^dagger <psi|
!
!================================================!
use w90_constants, only: dp, cmplx_0, cmplx_1
use w90_error
use w90_io, only: io_stopwatch_start, io_stopwatch_stop
use w90_sitesym, only: sitesym_symmetrize_u_matrix
use w90_types, only: timer_list_type
use w90_wannier90_types, only: sitesym_type
implicit none
! arguments
type(sitesym_type), intent(inout) :: sitesym
type(timer_list_type), intent(inout) :: timer
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
integer, intent(in) :: ndimwin(:) ! (num_kpts)
integer, intent(in) :: num_bands, num_kpts, num_wann
integer, intent(in) :: stdout
integer, intent(in) :: timing_level
complex(kind=dp), intent(in) :: a_matrix(:, :, :)
complex(kind=dp), intent(inout) :: u_matrix(:, :, :)
complex(kind=dp), intent(inout) :: u_matrix_opt(:, :, :)
logical, intent(in) :: on_root, lsitesymmetry
! local variables
integer :: nkp, info, ierr
complex(kind=dp), allocatable :: caa(:, :, :)
! For ZGESVD
real(kind=dp), allocatable :: svals(:)
real(kind=dp), allocatable :: rwork(:)
complex(kind=dp), allocatable :: cv(:, :)
complex(kind=dp), allocatable :: cz(:, :)
complex(kind=dp), allocatable :: cwork(:)
if (timing_level > 1) call io_stopwatch_start('dis: main: find_u', timer)
! Currently, this part is not parallelized; thus, we perform the task only on root and then broadcast the result.
if (on_root) then
! Allocate arrays needed for ZGESVD
allocate (svals(num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating svals in dis_main', comm)
return
endif
allocate (rwork(5*num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating rwork in dis_main', comm)
return
endif
allocate (cv(num_wann, num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating cv in dis_main', comm)
return
endif
allocate (cz(num_wann, num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating cz in dis_main', comm)
return
endif
allocate (cwork(4*num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating cwork in dis_main', comm)
return
endif
allocate (caa(num_wann, num_wann, num_kpts), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating caa in dis_main', comm)
return
endif
do nkp = 1, num_kpts
if (lsitesymmetry) then
if (sitesym%ir2ik(sitesym%ik2ir(nkp)) .ne. nkp) cycle
endif
call zgemm('C', 'N', num_wann, num_wann, ndimwin(nkp), cmplx_1, u_matrix_opt(:, :, nkp), &
num_bands, a_matrix(:, :, nkp), num_bands, cmplx_0, caa(:, :, nkp), num_wann)
! Singular-value decomposition
call zgesvd('A', 'A', num_wann, num_wann, caa(:, :, nkp), num_wann, svals, cz, num_wann, &
cv, num_wann, cwork, 4*num_wann, rwork, info)
if (info .ne. 0) then
if (on_root) write (stdout, *) ' ERROR: IN ZGESVD IN dis_main'
if (on_root) write (stdout, *) 'K-POINT NKP=', nkp, ' INFO=', info
if (info .lt. 0) then
if (on_root) write (stdout, *) 'THE ', -info, '-TH ARGUMENT HAD ILLEGAL VALUE'
endif
call set_error_fatal(error, 'dis_main: problem in ZGESVD 1', comm)
return
endif
! u_matrix is the initial guess for the unitary rotation of the
! basis states given by the subroutine extract
call zgemm('N', 'N', num_wann, num_wann, num_wann, cmplx_1, cz, num_wann, cv, num_wann, &
cmplx_0, u_matrix(:, :, nkp), num_wann)
enddo
endif
call comms_bcast(u_matrix(1, 1, 1), num_wann*num_wann*num_kpts, error, comm)
if (allocated(error)) return
! if (lsitesymmetry) call sitesym_symmetrize_u_matrix(num_wann,u_matrix) !RS:
if (on_root) then
! Deallocate arrays for ZGESVD
deallocate (caa, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating caa in dis_main', comm)
return
endif
deallocate (cwork, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating cwork in dis_main', comm)
return
endif
deallocate (cz, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating cz in dis_main', comm)
return
endif
deallocate (cv, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating cv in dis_main', comm)
return
endif
deallocate (rwork, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating rwork in dis_main', comm)
return
endif
deallocate (svals, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating svals in dis_main', comm)
return
endif
endif
if (lsitesymmetry) then
call sitesym_symmetrize_u_matrix(sitesym, u_matrix, num_bands, num_wann, num_kpts, num_wann, &
stdout, error, comm)
if (allocated(error)) return
endif
if (timing_level > 1) call io_stopwatch_stop('dis: main: find_u', timer)
return
!================================================!
end subroutine internal_find_u
subroutine internal_find_u_gamma(a_matrix, u_matrix, u_matrix_opt, ndimwin, num_wann, &
timing_level, stdout, timer, error, comm)
!================================================!
!
!! Make initial u_matrix real
!! Must be the case when gamma_only = .true.
!
!================================================!
use w90_constants, only: dp
use w90_error
use w90_io, only: io_stopwatch_start, io_stopwatch_stop
use w90_types, only: timer_list_type
implicit none
! arguments
type(timer_list_type), intent(inout) :: timer
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
integer, intent(in) :: ndimwin(:)
integer, intent(in) :: stdout
integer, intent(in) :: timing_level, num_wann
complex(kind=dp), intent(in) :: a_matrix(:, :, :)
complex(kind=dp), intent(inout) :: u_matrix(:, :, :)
complex(kind=dp), intent(inout) :: u_matrix_opt(:, :, :)
! local variables
integer :: info, ierr
real(kind=dp), allocatable :: a_matrix_r(:, :)
real(kind=dp), allocatable :: raa(:, :)
real(kind=dp), allocatable :: u_opt_r(:, :)
! for dgesvd
real(kind=dp), allocatable :: rv(:, :)
real(kind=dp), allocatable :: rz(:, :)
real(kind=dp), allocatable :: svals(:)
real(kind=dp), allocatable :: work(:)
if (timing_level > 1) call io_stopwatch_start('dis: main: find_u_gamma', timer)
! Allocate arrays needed for getting a_matrix_r
allocate (u_opt_r(ndimwin(1), num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating u_opt_r in dis_main', comm)
return
endif
allocate (a_matrix_r(ndimwin(1), num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating a_matrix_r in dis_main', comm)
return
endif
! Allocate arrays needed for dgesvd
allocate (svals(num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating svals in dis_main', comm)
return
endif
allocate (work(5*num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating rwork in dis_main', comm)
return
endif
allocate (rv(num_wann, num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating cv in dis_main', comm)
return
endif
allocate (rz(num_wann, num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating cz in dis_main', comm)
return
endif
allocate (raa(num_wann, num_wann), stat=ierr)
if (ierr /= 0) then
call set_error_alloc(error, 'Error in allocating raa in dis_main', comm)
return
endif
u_opt_r(:, :) = real(u_matrix_opt(1:ndimwin(1), 1:num_wann, 1), dp)
a_matrix_r(:, :) = real(a_matrix(1:ndimwin(1), 1:num_wann, 1), kind=dp)
call dgemm('T', 'N', num_wann, num_wann, ndimwin(1), 1.0_dp, u_opt_r, ndimwin(1), a_matrix_r, &
ndimwin(1), 0.0_dp, raa, num_wann)
! Singular-value decomposition
call dgesvd('A', 'A', num_wann, num_wann, raa, num_wann, svals, rz, num_wann, rv, num_wann, &
work, 5*num_wann, info)
if (info .ne. 0) then
write (stdout, *) ' ERROR: IN DGESVD IN dis_main'
write (stdout, *) 'K-POINT = Gamma', ' INFO=', info
if (info .lt. 0) then
write (stdout, *) 'THE ', -info, '-TH ARGUMENT HAD ILLEGAL VALUE'
endif
call set_error_fatal(error, 'dis_main: problem in DGESVD 1', comm)
return
endif
! u_matrix is the initial guess for the unitary rotation of the
! basis states given by the subroutine extract
call dgemm('N', 'N', num_wann, num_wann, num_wann, 1.0_dp, rz, num_wann, rv, num_wann, 0.0_dp, &
raa, num_wann)
u_matrix(:, :, 1) = cmplx(raa(:, :), 0.0_dp, dp)
! Deallocate arrays for DGESVD
deallocate (raa, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating raa in dis_main', comm)
return
endif
deallocate (rz, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating rz in dis_main', comm)
return
endif
deallocate (rv, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating rv in dis_main', comm)
return
endif
deallocate (work, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating work in dis_main', comm)
return
endif
deallocate (svals, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error deallocating svals in dis_main', comm)
return
endif
! Deallocate arrays for a_matrix_r
deallocate (a_matrix_r, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error in deallocating a_matrix_r in dis_main', comm)
return
endif
deallocate (u_opt_r, stat=ierr)
if (ierr /= 0) then
call set_error_dealloc(error, 'Error in deallocating u_opt_r in dis_main', comm)
return
endif
if (timing_level > 1) call io_stopwatch_stop('dis: main: find_u_gamma', timer)
return
!================================================!
end subroutine internal_find_u_gamma
subroutine dis_windows(dis_spheres, dis_manifold, eigval_opt, kpt_latt, recip_lattice, indxfroz, &
indxnfroz, ndimfroz, iprint, num_bands, num_kpts, num_wann, &
timing_level, lfrozen, linner, on_root, stdout, timer, error, comm)
!================================================!
!
!! This subroutine selects the states that are inside the outer
!! window (ie, the energy window out of which we fish out the
!! optimally-connected subspace) and those that are inside the
!! inner window (that make up the frozen manifold, and are
!! straightfowardly included as they are). This, in practice,
!! amounts to slimming down the original num_wann x num_wann overlap
!! matrices, removing rows and columns that belong to u_nks that
!! have been excluded forever, and marking (indexing) the rows and
!! columns that correspond to frozen states.
!!
!! Note - in windows eigval_opt are shifted, so the lowest ones go
!! from nfirstwin(nkp) to nfirstwin(nkp)+ndimwin(nkp)-1, and above
!! they are set to zero.
!
!================================================!
! OUTPUT:
! ndimwin(nkp) number of bands inside outer window at nkp-th k poi
! ndimfroz(nkp) number of frozen bands at nkp-th k point
! lfrozen(i,nkp) true if the i-th band inside outer window is frozen
! linner true if there is an inner window
! indxfroz(i,nkp) outer-window band index for the i-th frozen state
! (equals 1 if it is the bottom of outer window)
! indxnfroz(i,nkp) outer-window band index for the i-th non-frozen s
! (equals 1 if it is the bottom of outer window)
! nfirstwin(nkp) index of lowest band inside outer window at nkp-th
! MODIFIED:
! eigval_opt(nb,nkp) At input it contains a large set of eigenvalues. At
! it is slimmed down to contain only those inside the
! energy window, stored in nb=1,...,ndimwin(nkp)
use w90_constants, only: dp, cmplx_0, cmplx_1
use w90_error
use w90_io, only: io_stopwatch_start, io_stopwatch_stop
use w90_types, only: dis_manifold_type, kmesh_info_type, print_output_type, timer_list_type
use w90_wannier90_types, only: dis_spheres_type
implicit none
! arguments
type(dis_spheres_type), intent(in) :: dis_spheres
type(dis_manifold_type), intent(inout) :: dis_manifold ! ndimwin alone is modified
type(timer_list_type), intent(inout) :: timer
type(w90_error_type), allocatable, intent(out) :: error
type(w90_comm_type), intent(in) :: comm
integer, intent(in) :: iprint, timing_level
integer, intent(in) :: stdout
integer, intent(in) :: num_bands, num_kpts, num_wann
integer, intent(inout) :: ndimfroz(:)
integer, intent(inout) :: indxfroz(:, :)
integer, intent(inout) :: indxnfroz(:, :)
real(kind=dp), intent(in) :: kpt_latt(3, num_kpts), recip_lattice(3, 3)
real(kind=dp), intent(inout) :: eigval_opt(:, :)
logical, intent(in) :: on_root
logical, intent(inout) :: linner
logical, intent(inout) :: lfrozen(:, :)
! local variables
integer :: i, j, nkp
integer :: imin, imax, kifroz_min, kifroz_max
real(kind=dp) :: dk(3)
logical :: dis_ok
if (timing_level > 1) call io_stopwatch_start('dis: windows', timer)
linner = .false.
if (iprint > 0) write (stdout, '(1x,a)') &
'+----------------------------------------------------------------------------+'
if (iprint > 0) write (stdout, '(1x,a)') &
'| Energy Windows |'
if (iprint > 0) write (stdout, '(1x,a)') &
'| --------------- |'
if (iprint > 0) write (stdout, '(1x,a,f10.5,a,f10.5,a)') &
'| Outer: ', dis_manifold%win_min, ' to ', dis_manifold%win_max, &
' (eV) |'
if (dis_manifold%frozen_states) then
if (iprint > 0) write (stdout, '(1x,a,f10.5,a,f10.5,a)') &
'| Inner: ', dis_manifold%froz_min, ' to ', dis_manifold%froz_max, &
' (eV) |'
else
if (iprint > 0) write (stdout, '(1x,a)') &
'| No frozen states were specified |'
endif
if (iprint > 0) write (stdout, '(1x,a)') &
'+----------------------------------------------------------------------------+'
do nkp = 1, num_kpts
! Check which eigenvalues fall within the outer window
if ((eigval_opt(1, nkp) .gt. dis_manifold%win_max) .or. &
(eigval_opt(num_bands, nkp) .lt. dis_manifold%win_min)) then
if (on_root) then
write (stdout, *) ' ERROR AT K-POINT: ', nkp
write (stdout, *) ' ENERGY WINDOW (eV): [', dis_manifold%win_min, ',', &
dis_manifold%win_max, ']'
write (stdout, *) ' EIGENVALUE RANGE (eV): [', &
eigval_opt(1, nkp), ',', eigval_opt(num_bands, nkp), ']'
call set_error_fatal(error, 'dis_windows: The outer energy window contains no eigenvalues', comm)
return
endif
endif
dis_manifold%ndimwin(nkp) = num_bands
dis_manifold%nfirstwin(nkp) = 1
! Note: we assume that eigvals are ordered from the bottom up
imin = 0
imax = 0
do i = 1, num_bands
if (imin .eq. 0) then
if ((eigval_opt(i, nkp) .ge. dis_manifold%win_min) .and. &
(eigval_opt(i, nkp) .le. dis_manifold%win_max)) imin = i
imax = i
endif
if (eigval_opt(i, nkp) .le. dis_manifold%win_max) imax = i
enddo
dis_manifold%ndimwin(nkp) = imax - imin + 1
dis_manifold%nfirstwin(nkp) = imin
!~~ GS-start
! disentangle at the current k-point only if it is within one of the
! spheres centered at the k-points listed in kpt_dis
if (dis_spheres%num .gt. 0) then
dis_ok = .false.
! loop on the sphere centers
do i = 1, dis_spheres%num
dk = kpt_latt(:, nkp) - dis_spheres%spheres(1:3, i)
dk = matmul(anint(dk) - dk, recip_lattice(:, :))
! if the current k-point is included in at least one sphere,
! then perform disentanglement as usual
if (abs(dot_product(dk, dk)) .lt. dis_spheres%spheres(4, i)**2) then
dis_ok = .true.