-
Notifications
You must be signed in to change notification settings - Fork 0
/
almo_scf_methods.F
3427 lines (2895 loc) · 144 KB
/
almo_scf_methods.F
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
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright (C) 2000 - 2019 CP2K developers group !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Subroutines for ALMO SCF
!> \par History
!> 2011.06 created [Rustam Z Khaliullin]
!> 2018.09 smearing support [Ruben Staub]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
MODULE almo_scf_methods
USE almo_scf_types, ONLY: almo_scf_env_type,&
almo_scf_history_type
USE bibliography, ONLY: Kolafa2004,&
cite_reference
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_dbcsr_cholesky, ONLY: cp_dbcsr_cholesky_decompose,&
cp_dbcsr_cholesky_invert
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_unit_nr,&
cp_logger_type
USE cp_para_types, ONLY: cp_para_env_type
USE dbcsr_api, ONLY: &
dbcsr_add, dbcsr_add_on_diag, dbcsr_copy, dbcsr_create, dbcsr_distribution_get, &
dbcsr_distribution_type, dbcsr_filter, dbcsr_finalize, dbcsr_frobenius_norm, &
dbcsr_get_block_p, dbcsr_get_diag, dbcsr_get_info, dbcsr_get_stored_coordinates, &
dbcsr_init_random, dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, &
dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_multiply, &
dbcsr_nblkcols_total, dbcsr_nblkrows_total, dbcsr_print, dbcsr_release, &
dbcsr_reserve_block2d, dbcsr_scale_by_vector, dbcsr_set, dbcsr_set_diag, dbcsr_transposed, &
dbcsr_type, dbcsr_type_no_symmetry, dbcsr_type_symmetric, dbcsr_work_create
USE domain_submatrix_methods, ONLY: &
add_submatrices, construct_dbcsr_from_submatrices, construct_submatrices, &
copy_submatrices, copy_submatrix_data, init_submatrices, multiply_submatrices, &
print_submatrices, release_submatrices
USE domain_submatrix_types, ONLY: domain_map_type,&
domain_submatrix_type,&
select_row,&
select_row_col
USE fermi_utils, ONLY: FermiFixed
!! for smearing
USE input_constants, ONLY: almo_domain_layout_molecular,&
almo_mat_distr_atomic,&
almo_scf_diag,&
spd_inversion_dense_cholesky,&
spd_inversion_ls_hotelling,&
spd_inversion_ls_taylor
USE iterate_matrix, ONLY: invert_Hotelling,&
invert_Taylor,&
matrix_sqrt_Newton_Schulz
USE kinds, ONLY: dp
USE mathlib, ONLY: binomial
USE util, ONLY: sort
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'almo_scf_methods'
PUBLIC almo_scf_ks_to_ks_blk, almo_scf_p_blk_to_t_blk, &
almo_scf_t_to_proj, almo_scf_ks_blk_to_tv_blk, &
almo_scf_ks_xx_to_tv_xx, almo_scf_t_rescaling, &
apply_projector, get_overlap, &
generator_to_unitary, &
orthogonalize_mos, &
pseudo_invert_diagonal_blk, construct_test, &
construct_domain_preconditioner, &
apply_domain_operators, &
construct_domain_s_inv, &
construct_domain_s_sqrt, &
distribute_domains, &
almo_scf_ks_to_ks_xx, &
construct_domain_r_down, &
xalmo_initial_guess
CONTAINS
! **************************************************************************************************
!> \brief builds projected KS matrices for the overlapping domains
!> also computes the DIIS error vector as a by-product
!> \param almo_scf_env ...
!> \par History
!> 2013.03 created [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE almo_scf_ks_to_ks_xx(almo_scf_env)
TYPE(almo_scf_env_type), INTENT(INOUT) :: almo_scf_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'almo_scf_ks_to_ks_xx', &
routineP = moduleN//':'//routineN
INTEGER :: handle, ispin, ndomains
REAL(KIND=dp) :: eps_multiply
TYPE(dbcsr_type) :: matrix_tmp1, matrix_tmp2, matrix_tmp3, matrix_tmp4, matrix_tmp5, &
matrix_tmp6, matrix_tmp7, matrix_tmp8, matrix_tmp9
TYPE(domain_submatrix_type), ALLOCATABLE, &
DIMENSION(:) :: subm_tmp1, subm_tmp2, subm_tmp3
CALL timeset(routineN, handle)
eps_multiply = almo_scf_env%eps_filter
DO ispin = 1, almo_scf_env%nspins
ndomains = dbcsr_nblkcols_total(almo_scf_env%quench_t(ispin))
! 0. Create KS_xx
CALL construct_submatrices( &
almo_scf_env%matrix_ks(ispin), &
almo_scf_env%domain_ks_xx(:, ispin), &
almo_scf_env%quench_t(ispin), &
almo_scf_env%domain_map(ispin), &
almo_scf_env%cpu_of_domain, &
select_row_col)
!!!!! RZK-warning MAKE SURE THAT YOU NEED BLOCKS OUTSIDE QUENCH_T
!!!!! FOR ALL NO-MATRICES NOT COMPUTING THEM CAN SAVE LOTS OF TIME
! 1. TMP1=KS.T
! Cost: NOn
!matrix_tmp1 = create NxO, full
CALL dbcsr_create(matrix_tmp1, &
template=almo_scf_env%matrix_t(ispin))
CALL dbcsr_multiply("N", "N", 1.0_dp, almo_scf_env%matrix_ks(ispin), &
almo_scf_env%matrix_t(ispin), &
0.0_dp, matrix_tmp1, &
filter_eps=eps_multiply)
! 2. TMP2=TMP1.SigInv=KS.T.SigInv
! Cost: NOO
!matrix_tmp2 = create NxO, full
CALL dbcsr_create(matrix_tmp2, &
template=almo_scf_env%matrix_t(ispin))
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_tmp1, &
almo_scf_env%matrix_sigma_inv(ispin), &
0.0_dp, matrix_tmp2, &
filter_eps=eps_multiply)
! 3. TMP1=S.T
! Cost: NOn
CALL dbcsr_multiply("N", "N", 1.0_dp, almo_scf_env%matrix_s(1), &
almo_scf_env%matrix_t(ispin), &
0.0_dp, matrix_tmp1, &
filter_eps=eps_multiply)
! 4. TMP4=TMP2.tr(TMP1)=KS.T.SigInv.tr(T).S
! Cost: NNO
!matrix_tmp4 = create NxN
CALL dbcsr_create(matrix_tmp4, &
template=almo_scf_env%matrix_s(1), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_multiply("N", "T", 1.0_dp, matrix_tmp2, &
matrix_tmp1, &
0.0_dp, matrix_tmp4, &
filter_eps=eps_multiply)
! 5. KS_xx=KS_xx-TMP4_xx-tr(TMP4_xx)
ALLOCATE (subm_tmp1(ndomains))
CALL init_submatrices(subm_tmp1)
CALL construct_submatrices( &
matrix_tmp4, &
subm_tmp1, &
almo_scf_env%quench_t(ispin), &
almo_scf_env%domain_map(ispin), &
almo_scf_env%cpu_of_domain, &
select_row_col)
CALL add_submatrices(1.0_dp, almo_scf_env%domain_ks_xx(:, ispin), &
-1.0_dp, subm_tmp1, 'N')
CALL add_submatrices(1.0_dp, almo_scf_env%domain_ks_xx(:, ispin), &
-1.0_dp, subm_tmp1, 'T')
! 6. TMP3=tr(TMP4).T=S.T.SigInv.tr(T).KS.T
! Cost: NOn
!matrix_tmp3 = create NxO, full
CALL dbcsr_create(matrix_tmp3, &
template=almo_scf_env%matrix_t(ispin), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_multiply("T", "N", 1.0_dp, &
matrix_tmp4, &
almo_scf_env%matrix_t(ispin), &
0.0_dp, matrix_tmp3, &
filter_eps=eps_multiply)
CALL dbcsr_release(matrix_tmp4)
! 8. TMP6=TMP3.SigInv=S.T.SigInv.tr(T).KS.T.SigInv
! Cost: NOO
!matrix_tmp6 = create NxO, full
CALL dbcsr_create(matrix_tmp6, &
template=almo_scf_env%matrix_t(ispin), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_multiply("N", "N", 1.0_dp, &
matrix_tmp3, &
almo_scf_env%matrix_sigma_inv(ispin), &
0.0_dp, matrix_tmp6, &
filter_eps=eps_multiply)
! 8A. Use intermediate matrices to evaluate the gradient/error
! Err=(TMP2-TMP6)_q=(KS.T.SigInv-S.T.SigInv.tr(T).KS.T.SigInv)_q
! error vector in AO-MO basis
CALL dbcsr_copy(almo_scf_env%matrix_err_xx(ispin), &
almo_scf_env%quench_t(ispin))
CALL dbcsr_copy(almo_scf_env%matrix_err_xx(ispin), &
matrix_tmp2, keep_sparsity=.TRUE.)
CALL dbcsr_create(matrix_tmp4, &
template=almo_scf_env%matrix_t(ispin), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_copy(matrix_tmp4, &
almo_scf_env%quench_t(ispin))
CALL dbcsr_copy(matrix_tmp4, &
matrix_tmp6, keep_sparsity=.TRUE.)
CALL dbcsr_add(almo_scf_env%matrix_err_xx(ispin), &
matrix_tmp4, 1.0_dp, -1.0_dp)
CALL dbcsr_release(matrix_tmp4)
!
! error vector in AO-AO basis
! RZK-warning tmp4 can be created using the sparsity pattern,
! then retain_sparsity can be used to perform the multiply
! this will save some time
CALL dbcsr_copy(matrix_tmp3, &
matrix_tmp2)
CALL dbcsr_add(matrix_tmp3, &
matrix_tmp6, 1.0_dp, -1.0_dp)
CALL dbcsr_create(matrix_tmp4, &
template=almo_scf_env%matrix_s(1), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_multiply("N", "T", 1.0_dp, &
matrix_tmp3, &
almo_scf_env%matrix_t(ispin), &
0.0_dp, matrix_tmp4, &
filter_eps=eps_multiply)
CALL construct_submatrices( &
matrix_tmp4, &
almo_scf_env%domain_err(:, ispin), &
almo_scf_env%quench_t(ispin), &
almo_scf_env%domain_map(ispin), &
almo_scf_env%cpu_of_domain, &
select_row_col)
CALL dbcsr_release(matrix_tmp4)
! domain_err submatrices are in down-up representation
! bring them into the orthogonalized basis
ALLOCATE (subm_tmp2(ndomains))
CALL init_submatrices(subm_tmp2)
CALL multiply_submatrices('N', 'N', 1.0_dp, &
almo_scf_env%domain_err(:, ispin), &
almo_scf_env%domain_s_sqrt(:, ispin), 0.0_dp, subm_tmp2)
CALL multiply_submatrices('N', 'N', 1.0_dp, &
almo_scf_env%domain_s_sqrt_inv(:, ispin), &
subm_tmp2, 0.0_dp, almo_scf_env%domain_err(:, ispin))
! 9. TMP5=TMP6.tr(TMP1)=S.T.SigInv.tr(T).KS.T.SigInv.tr(T).S
! Cost: NNO
! matrix_tmp5 = create NxN, full
! RZK-warning tmp5 can be created using the sparsity pattern,
! then retain_sparsity can be used to perform the multiply
! this will save some time
CALL dbcsr_create(matrix_tmp5, &
template=almo_scf_env%matrix_s(1), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_multiply("N", "T", 1.0_dp, &
matrix_tmp6, &
matrix_tmp1, &
0.0_dp, matrix_tmp5, &
filter_eps=eps_multiply)
! 10. KS_xx=KS_xx+TMP5_xx
CALL construct_submatrices( &
matrix_tmp5, &
subm_tmp1, &
almo_scf_env%quench_t(ispin), &
almo_scf_env%domain_map(ispin), &
almo_scf_env%cpu_of_domain, &
select_row_col)
CALL dbcsr_release(matrix_tmp5)
CALL add_submatrices(1.0_dp, almo_scf_env%domain_ks_xx(:, ispin), &
1.0_dp, subm_tmp1, 'N')
! 11. KS_xx=KS_xx + [S.T]_xx.[SigInv.tr(T).KS.(1-T.SigInv.tr(T).S)]_xx + transposed
ALLOCATE (subm_tmp3(ndomains))
CALL init_submatrices(subm_tmp3)
CALL construct_submatrices( &
matrix_tmp2, &
subm_tmp2, &
almo_scf_env%quench_t(ispin), &
almo_scf_env%domain_map(ispin), &
almo_scf_env%cpu_of_domain, &
select_row)
CALL construct_submatrices( &
matrix_tmp6, &
subm_tmp3, &
almo_scf_env%quench_t(ispin), &
almo_scf_env%domain_map(ispin), &
almo_scf_env%cpu_of_domain, &
select_row)
CALL dbcsr_release(matrix_tmp6)
CALL add_submatrices(1.0_dp, subm_tmp2, &
-1.0_dp, subm_tmp3, 'N')
CALL construct_submatrices( &
matrix_tmp1, &
subm_tmp3, &
almo_scf_env%quench_t(ispin), &
almo_scf_env%domain_map(ispin), &
almo_scf_env%cpu_of_domain, &
select_row)
CALL multiply_submatrices('N', 'T', 1.0_dp, subm_tmp2, &
subm_tmp3, 0.0_dp, subm_tmp1)
CALL add_submatrices(1.0_dp, almo_scf_env%domain_ks_xx(:, ispin), &
1.0_dp, subm_tmp1, 'N')
CALL add_submatrices(1.0_dp, almo_scf_env%domain_ks_xx(:, ispin), &
1.0_dp, subm_tmp1, 'T')
! 12. TMP7=tr(T).KS.T.SigInv
CALL dbcsr_create(matrix_tmp7, &
template=almo_scf_env%matrix_sigma_blk(ispin), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_multiply("T", "N", 1.0_dp, &
almo_scf_env%matrix_t(ispin), &
matrix_tmp2, &
0.0_dp, matrix_tmp7, &
filter_eps=eps_multiply)
! 13. TMP8=[SigInv.tr(T).KS.T.SigInv]_xx
CALL dbcsr_create(matrix_tmp8, &
template=almo_scf_env%matrix_sigma_blk(ispin), &
matrix_type=dbcsr_type_symmetric)
CALL dbcsr_copy(matrix_tmp8, almo_scf_env%matrix_sigma_blk(ispin))
CALL dbcsr_multiply("N", "N", 1.0_dp, &
almo_scf_env%matrix_sigma_inv(ispin), &
matrix_tmp7, &
0.0_dp, matrix_tmp8, &
retain_sparsity=.TRUE., &
filter_eps=eps_multiply)
CALL dbcsr_release(matrix_tmp7)
! 13. TMP9=[S.T]_xx
CALL dbcsr_create(matrix_tmp9, &
template=almo_scf_env%matrix_t(ispin), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_copy(matrix_tmp9, almo_scf_env%quench_t(ispin))
CALL dbcsr_copy(matrix_tmp9, matrix_tmp1, keep_sparsity=.TRUE.)
! 14. TMP3=TMP9.TMP8=[S.T]_xx.[SigInv.tr(T).KS.T.SigInv]_xx
CALL dbcsr_multiply("N", "N", 1.0_dp, &
matrix_tmp9, &
matrix_tmp8, &
0.0_dp, matrix_tmp3, &
filter_eps=eps_multiply)
CALL dbcsr_release(matrix_tmp8)
CALL dbcsr_release(matrix_tmp9)
! 15. KS_xx=KS_xx+[S.T]_xx.[SigInv.tr(T).KS.T.SigInv]_xx.[tr(T).S]_xx
CALL construct_submatrices( &
matrix_tmp3, &
subm_tmp2, &
almo_scf_env%quench_t(ispin), &
almo_scf_env%domain_map(ispin), &
almo_scf_env%cpu_of_domain, &
select_row)
CALL multiply_submatrices('N', 'T', 1.0_dp, subm_tmp2, &
subm_tmp3, 0.0_dp, subm_tmp1)
CALL add_submatrices(1.0_dp, almo_scf_env%domain_ks_xx(:, ispin), &
1.0_dp, subm_tmp1, 'N')
!!!!!!! use intermediate matrices to get the error vector !!!!!!!
!!!!!!! make sure s_blk_sqrt and its inverse exist (i.e. we use diag algorithm)
!CPPrecondition(almo_scf_env%almo_update_algorithm.eq.almo_scf_diag,cp_failure_level,routineP,failure)
!! tmp_err = (1-S.T_blk.SigInv.tr(T_blk)).F.T_blk.SigInv
!CALL dbcsr_init(matrix_tmp_err)
!CALL dbcsr_create(matrix_tmp_err,&
! template=almo_scf_env%matrix_t(ispin))
!CALL dbcsr_copy(matrix_tmp_err,&
! matrix_tmp2)
!CALL dbcsr_add(matrix_tmp_err,matrix_tmp3,&
! 1.0_dp,-1.0_dp)
!! err_blk = tmp_err.tr(T_blk)
!CALL dbcsr_copy(almo_scf_env%matrix_err_blk(ispin),&
! almo_scf_env%matrix_s_blk_sqrt(1))
!CALL dbcsr_multiply("N", "T", 1.0_dp, matrix_tmp_err,&
! almo_scf_env%matrix_t(ispin),&
! 0.0_dp, almo_scf_env%matrix_err_blk(ispin),&
! retain_sparsity=.TRUE.,&
! filter_eps=eps_multiply)
!CALL dbcsr_release(matrix_tmp_err)
!! bring to the orthogonal basis
!! err_blk = (S_blk^-1/2).err_blk.(S_blk^1/2)
!CALL dbcsr_init(matrix_tmp_err)
!CALL dbcsr_create(matrix_tmp_err,&
! template=almo_scf_env%matrix_err_blk(ispin))
!CALL dbcsr_multiply("N", "N", 1.0_dp,&
! almo_scf_env%matrix_err_blk(ispin),&
! almo_scf_env%matrix_s_blk_sqrt(1),&
! 0.0_dp, matrix_tmp_err,&
! filter_eps=eps_multiply)
!CALL dbcsr_multiply("N", "N", 1.0_dp,&
! almo_scf_env%matrix_s_blk_sqrt_inv(1),&
! matrix_tmp_err,&
! 0.0_dp, almo_scf_env%matrix_err_blk(ispin),&
! filter_eps=eps_multiply)
!! subtract transpose
!CALL dbcsr_transposed(matrix_tmp_err,&
! almo_scf_env%matrix_err_blk(ispin))
!CALL dbcsr_add(almo_scf_env%matrix_err_blk(ispin),&
! matrix_tmp_err,&
! 1.0_dp,-1.0_dp)
!CALL dbcsr_release(matrix_tmp_err)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
CALL release_submatrices(subm_tmp3)
CALL release_submatrices(subm_tmp2)
CALL release_submatrices(subm_tmp1)
DEALLOCATE (subm_tmp3)
DEALLOCATE (subm_tmp2)
DEALLOCATE (subm_tmp1)
CALL dbcsr_release(matrix_tmp3)
CALL dbcsr_release(matrix_tmp2)
CALL dbcsr_release(matrix_tmp1)
ENDDO ! spins
CALL timestop(handle)
END SUBROUTINE almo_scf_ks_to_ks_xx
! **************************************************************************************************
!> \brief computes the projected KS from the total KS matrix
!> also computes the DIIS error vector as a by-product
!> \param almo_scf_env ...
!> \par History
!> 2011.06 created [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE almo_scf_ks_to_ks_blk(almo_scf_env)
TYPE(almo_scf_env_type), INTENT(INOUT) :: almo_scf_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'almo_scf_ks_to_ks_blk', &
routineP = moduleN//':'//routineN
INTEGER :: handle, ispin
REAL(KIND=dp) :: eps_multiply
TYPE(dbcsr_type) :: matrix_tmp1, matrix_tmp2, matrix_tmp3, matrix_tmp4, matrix_tmp5, &
matrix_tmp6, matrix_tmp7, matrix_tmp8, matrix_tmp9, matrix_tmp_err
CALL timeset(routineN, handle)
eps_multiply = almo_scf_env%eps_filter
DO ispin = 1, almo_scf_env%nspins
! 1. TMP1=KS.T_blk
! Cost: NOn
!matrix_tmp1 = create NxO, full
CALL dbcsr_create(matrix_tmp1, &
template=almo_scf_env%matrix_t(ispin))
CALL dbcsr_multiply("N", "N", 1.0_dp, almo_scf_env%matrix_ks(ispin), &
almo_scf_env%matrix_t_blk(ispin), &
0.0_dp, matrix_tmp1, &
filter_eps=eps_multiply)
! 2. TMP2=TMP1.SigInv=KS.T_blk.SigInv
! Cost: NOO
!matrix_tmp2 = create NxO, full
CALL dbcsr_create(matrix_tmp2, &
template=almo_scf_env%matrix_t(ispin))
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_tmp1, &
almo_scf_env%matrix_sigma_inv(ispin), &
0.0_dp, matrix_tmp2, &
filter_eps=eps_multiply)
!!!!!! use intermediate matrices to get the error vector !!!!!!!
!CALL dbcsr_copy(almo_scf_env%matrix_err_blk(ispin),&
! almo_scf_env%matrix_t_blk(ispin))
!CALL dbcsr_copy(almo_scf_env%matrix_err_blk(ispin),&
! matrix_tmp2,&
! keep_sparsity=.TRUE.)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! 3. TMP1=S.T_blk
! Cost: NOn
CALL dbcsr_multiply("N", "N", 1.0_dp, almo_scf_env%matrix_s(1), &
almo_scf_env%matrix_t_blk(ispin), &
0.0_dp, matrix_tmp1, &
filter_eps=eps_multiply)
! 4. TMP4_blk=TMP2.tr(TMP1)=KS.T_blk.SigInv.tr(T_blk).S
! Cost: NnO
!matrix_tmp4 = create NxN, blk
CALL dbcsr_create(matrix_tmp4, &
template=almo_scf_env%matrix_s_blk(1), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_copy(matrix_tmp4, almo_scf_env%matrix_s_blk(1))
CALL dbcsr_multiply("N", "T", 1.0_dp, matrix_tmp2, &
matrix_tmp1, &
0.0_dp, matrix_tmp4, &
retain_sparsity=.TRUE., &
filter_eps=eps_multiply)
! 5. KS_blk=KS_blk-TMP4_blk
CALL dbcsr_copy(almo_scf_env%matrix_ks_blk(ispin), &
almo_scf_env%matrix_ks(ispin), keep_sparsity=.TRUE.)
CALL dbcsr_add(almo_scf_env%matrix_ks_blk(ispin), &
matrix_tmp4, &
1.0_dp, -1.0_dp)
! 6. TMP5_blk=tr(TMP4_blk)
! KS_blk=KS_blk-tr(TMP4_blk)
!matrix_tmp5 = create NxN, blk
CALL dbcsr_create(matrix_tmp5, &
template=almo_scf_env%matrix_s_blk(1), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_transposed(matrix_tmp5, matrix_tmp4)
CALL dbcsr_add(almo_scf_env%matrix_ks_blk(ispin), matrix_tmp5, &
1.0_dp, -1.0_dp)
! 7. TMP3=tr(T_blk).TMP2=tr(T_blk).KS.T_blk.SigInv
! Cost: OOn
!matrix_tmp3 = create OxO, full
CALL dbcsr_create(matrix_tmp3, &
template=almo_scf_env%matrix_sigma_inv(ispin), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_multiply("T", "N", 1.0_dp, &
almo_scf_env%matrix_t_blk(ispin), &
matrix_tmp2, &
0.0_dp, matrix_tmp3, &
filter_eps=eps_multiply)
! 8. TMP6=SigInv.TMP3=SigInv.tr(T_blk).KS.T_blk.SigInv
! Cost: OOO
!matrix_tmp6 = create OxO, full
CALL dbcsr_create(matrix_tmp6, &
template=almo_scf_env%matrix_sigma_inv(ispin), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_multiply("N", "N", 1.0_dp, &
almo_scf_env%matrix_sigma_inv(ispin), &
matrix_tmp3, &
0.0_dp, matrix_tmp6, &
filter_eps=eps_multiply)
! 9. TMP3=TMP1.TMP6=S.T_blk.SigInv.tr(T_blk).KS.T_blk.SigInv
! Cost: NOO
!matrix_tmp3 = re-create NxO, full
CALL dbcsr_release(matrix_tmp3)
CALL dbcsr_create(matrix_tmp3, &
template=almo_scf_env%matrix_t(ispin))
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_tmp1, &
matrix_tmp6, &
0.0_dp, matrix_tmp3, &
filter_eps=eps_multiply)
!!!!!! use intermediate matrices to get the error vector !!!!!!!
!CALL dbcsr_init(matrix_tmp_err)
!CALL dbcsr_create(matrix_tmp_err,&
! template=almo_scf_env%matrix_t_blk(ispin))
!CALL dbcsr_copy(matrix_tmp_err,&
! almo_scf_env%matrix_t_blk(ispin))
!CALL dbcsr_copy(matrix_tmp_err,matrix_tmp3,&
! keep_sparsity=.TRUE.)
!CALL dbcsr_add(almo_scf_env%matrix_err_blk(ispin),matrix_tmp_err,&
! 1.0_dp,-1.0_dp)
!CALL dbcsr_release(matrix_tmp_err)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!! use intermediate matrices to get the error vector !!!!!!!
!!!!!! make sure s_blk_sqrt and its inverse exist (i.e. we use diag algorithm)
CPASSERT(almo_scf_env%almo_update_algorithm .EQ. almo_scf_diag)
! tmp_err = (1-S.T_blk.SigInv.tr(T_blk)).F.T_blk.SigInv
CALL dbcsr_create(matrix_tmp_err, &
template=almo_scf_env%matrix_t_blk(ispin))
CALL dbcsr_copy(matrix_tmp_err, &
matrix_tmp2)
CALL dbcsr_add(matrix_tmp_err, matrix_tmp3, &
1.0_dp, -1.0_dp)
! err_blk = tmp_err.tr(T_blk)
CALL dbcsr_copy(almo_scf_env%matrix_err_blk(ispin), &
almo_scf_env%matrix_s_blk_sqrt(1))
CALL dbcsr_multiply("N", "T", 1.0_dp, matrix_tmp_err, &
almo_scf_env%matrix_t_blk(ispin), &
0.0_dp, almo_scf_env%matrix_err_blk(ispin), &
retain_sparsity=.TRUE., &
filter_eps=eps_multiply)
CALL dbcsr_release(matrix_tmp_err)
! bring to the orthogonal basis
! err_blk = (S_blk^-1/2).err_blk.(S_blk^1/2)
CALL dbcsr_create(matrix_tmp_err, &
template=almo_scf_env%matrix_err_blk(ispin))
CALL dbcsr_multiply("N", "N", 1.0_dp, &
almo_scf_env%matrix_err_blk(ispin), &
almo_scf_env%matrix_s_blk_sqrt(1), &
0.0_dp, matrix_tmp_err, &
filter_eps=eps_multiply)
CALL dbcsr_multiply("N", "N", 1.0_dp, &
almo_scf_env%matrix_s_blk_sqrt_inv(1), &
matrix_tmp_err, &
0.0_dp, almo_scf_env%matrix_err_blk(ispin), &
filter_eps=eps_multiply)
! subtract transpose
CALL dbcsr_transposed(matrix_tmp_err, &
almo_scf_env%matrix_err_blk(ispin))
CALL dbcsr_add(almo_scf_env%matrix_err_blk(ispin), &
matrix_tmp_err, &
1.0_dp, -1.0_dp)
CALL dbcsr_release(matrix_tmp_err)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! later we will need only the blk version of TMP6
! create it here and release TMP6
!matrix_tmp9 = create OxO, blk
!matrix_tmp9 = copy data from matrix_tmp6, retain sparsity
!matrix_tmp6 = release
CALL dbcsr_create(matrix_tmp9, &
template=almo_scf_env%matrix_sigma_blk(ispin), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_copy(matrix_tmp9, almo_scf_env%matrix_sigma_blk(ispin))
CALL dbcsr_copy(matrix_tmp9, matrix_tmp6, keep_sparsity=.TRUE.)
CALL dbcsr_release(matrix_tmp6)
!10. KS_blk=KS_blk+TMP3.tr(TMP1)=
! =KS_blk+S.T_blk.SigInv.tr.(T_blk).KS.T_blk.SigInv.tr(T_blk).S
! Cost: NnO
CALL dbcsr_multiply("N", "T", 1.0_dp, matrix_tmp3, &
matrix_tmp1, &
1.0_dp, almo_scf_env%matrix_ks_blk(ispin), &
retain_sparsity=.TRUE., &
filter_eps=eps_multiply)
! 11. TMP4_blk=TMP7_blk.tr(TMP8_blk)
! Cost: Nnn
!matrix_tmp7 = create NxO, blk
!matrix_tmp7 = copy data from matrix_tmp3, retain sparsity
!matrix_tmp3 = release
!matrix_tmp8 = create NxO, blk
!matrix_tmp8 = copy data from matrix_tmp1, retain sparsity
!matrix_tmp1 = release
CALL dbcsr_create(matrix_tmp7, &
template=almo_scf_env%matrix_t_blk(ispin))
! transfer only the ALMO blocks from tmp3 into tmp7:
! first, copy t_blk into tmp7 to transfer the blk structure,
! then copy tmp3 into tmp7 with retain_sparsity
CALL dbcsr_copy(matrix_tmp7, almo_scf_env%matrix_t_blk(ispin))
CALL dbcsr_copy(matrix_tmp7, matrix_tmp3, keep_sparsity=.TRUE.)
CALL dbcsr_release(matrix_tmp3)
! do the same for tmp1->tmp8
CALL dbcsr_create(matrix_tmp8, &
template=almo_scf_env%matrix_t_blk(ispin))
CALL dbcsr_copy(matrix_tmp8, almo_scf_env%matrix_t_blk(ispin))
CALL dbcsr_copy(matrix_tmp8, matrix_tmp1, keep_sparsity=.TRUE.)
CALL dbcsr_release(matrix_tmp1)
CALL dbcsr_multiply("N", "T", 1.0_dp, matrix_tmp7, &
matrix_tmp8, &
0.0_dp, matrix_tmp4, &
filter_eps=eps_multiply, &
retain_sparsity=.TRUE.)
! 12. KS_blk=KS_blk-TMP4_blk
CALL dbcsr_add(almo_scf_env%matrix_ks_blk(ispin), matrix_tmp4, &
1.0_dp, -1.0_dp)
! 13. TMP5_blk=tr(TMP5_blk)
! KS_blk=KS_blk-tr(TMP4_blk)
CALL dbcsr_transposed(matrix_tmp5, matrix_tmp4)
CALL dbcsr_add(almo_scf_env%matrix_ks_blk(ispin), matrix_tmp5, &
1.0_dp, -1.0_dp)
! 14. TMP4_blk=TMP7_blk.tr(TMP8_blk)
! Cost: Nnn
CALL dbcsr_copy(matrix_tmp7, matrix_tmp2, keep_sparsity=.TRUE.)
CALL dbcsr_release(matrix_tmp2)
CALL dbcsr_multiply("N", "T", 1.0_dp, matrix_tmp7, &
matrix_tmp8, &
0.0_dp, matrix_tmp4, &
retain_sparsity=.TRUE., &
filter_eps=eps_multiply)
! 15. KS_blk=KS_blk+TMP4_blk
CALL dbcsr_add(almo_scf_env%matrix_ks_blk(ispin), matrix_tmp4, &
1.0_dp, 1.0_dp)
! 16. KS_blk=KS_blk+tr(TMP4_blk)
CALL dbcsr_transposed(matrix_tmp5, matrix_tmp4)
CALL dbcsr_release(matrix_tmp4)
CALL dbcsr_add(almo_scf_env%matrix_ks_blk(ispin), matrix_tmp5, &
1.0_dp, 1.0_dp)
CALL dbcsr_release(matrix_tmp5)
! 17. TMP10_blk=TMP8_blk.TMP9_blk
! Cost: Noo
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_tmp8, &
matrix_tmp9, &
0.0_dp, matrix_tmp7, &
retain_sparsity=.TRUE., &
filter_eps=eps_multiply)
CALL dbcsr_release(matrix_tmp9)
! 18. KS_blk=TMP7_blk.tr(TMP8_blk)
! Cost: Nno
CALL dbcsr_multiply("N", "T", 1.0_dp, matrix_tmp7, &
matrix_tmp8, &
1.0_dp, almo_scf_env%matrix_ks_blk(ispin), &
retain_sparsity=.TRUE., &
filter_eps=eps_multiply)
CALL dbcsr_release(matrix_tmp7)
CALL dbcsr_release(matrix_tmp8)
ENDDO ! spins
CALL timestop(handle)
END SUBROUTINE almo_scf_ks_to_ks_blk
! **************************************************************************************************
!> \brief ALMOs by diagonalizing the KS domain submatrices
!> computes both the occupied and virtual orbitals
!> \param almo_scf_env ...
!> \par History
!> 2013.03 created [Rustam Z Khaliullin]
!> 2018.09 smearing support [Ruben Staub]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE almo_scf_ks_xx_to_tv_xx(almo_scf_env)
TYPE(almo_scf_env_type), INTENT(INOUT) :: almo_scf_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'almo_scf_ks_xx_to_tv_xx', &
routineP = moduleN//':'//routineN
INTEGER :: handle, iblock_size, idomain, info, &
ispin, lwork, ndomains
REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalues, work
REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: data_copy
TYPE(domain_submatrix_type), ALLOCATABLE, &
DIMENSION(:) :: subm_ks_xx_orthog, subm_t, subm_tmp
CALL timeset(routineN, handle)
IF (almo_scf_env%domain_layout_aos == almo_domain_layout_molecular .AND. &
almo_scf_env%mat_distr_aos == almo_mat_distr_atomic) THEN
CPABORT("a domain must be located entirely on a CPU")
ENDIF
ndomains = almo_scf_env%ndomains
ALLOCATE (subm_tmp(ndomains))
ALLOCATE (subm_ks_xx_orthog(ndomains))
ALLOCATE (subm_t(ndomains))
DO ispin = 1, almo_scf_env%nspins
CALL init_submatrices(subm_tmp)
CALL init_submatrices(subm_ks_xx_orthog)
! TRY: project out T0-occupied space for each domain
! F=(1-R_du).F.(1-tr(R_du))
!CALL copy_submatrices(almo_scf_env%domain_ks_xx(:,ispin),&
! subm_ks_xx_orthog,copy_data=.TRUE.)
!CALL multiply_submatrices('N','N',1.0_dp,&
! almo_scf_env%domain_r_down_up(:,ispin),&
! almo_scf_env%domain_ks_xx(:,ispin),0.0_dp,subm_tmp)
!CALL add_submatrices(1.0_dp,subm_ks_xx_orthog,-1.0_dp,subm_tmp,'N')
!CALL add_submatrices(1.0_dp,subm_ks_xx_orthog,-1.0_dp,subm_tmp,'T')
!!CALL multiply_submatrices('N','T',1.0_dp,subm_tmp,&
!! almo_scf_env%domain_r_down_up(:,ispin),&
!! 1.0_dp,subm_ks_xx_orthog)
! convert blocks to the orthogonal basis set
! TRY: replace one multiply
!CALL multiply_submatrices('N','N',1.0_dp,subm_ks_xx_orthog,&
! almo_scf_env%domain_s_sqrt_inv(:,ispin),0.0_dp,subm_tmp)
CALL multiply_submatrices('N', 'N', 1.0_dp, almo_scf_env%domain_ks_xx(:, ispin), &
almo_scf_env%domain_s_sqrt_inv(:, ispin), 0.0_dp, subm_tmp)
CALL multiply_submatrices('N', 'N', 1.0_dp, almo_scf_env%domain_s_sqrt_inv(:, ispin), &
subm_tmp, 0.0_dp, subm_ks_xx_orthog)
CALL release_submatrices(subm_tmp)
! create temporary matrices for occupied and virtual orbitals
! represented in the orthogonalized basis set
CALL init_submatrices(subm_t)
! loop over domains - perform diagonalization
DO idomain = 1, ndomains
! check if the submatrix exists
IF (subm_ks_xx_orthog(idomain)%domain .GT. 0) THEN
iblock_size = subm_ks_xx_orthog(idomain)%nrows
! Prepare data
ALLOCATE (eigenvalues(iblock_size))
ALLOCATE (data_copy(iblock_size, iblock_size))
data_copy(:, :) = subm_ks_xx_orthog(idomain)%mdata(:, :)
! Query the optimal workspace for dsyev
LWORK = -1
ALLOCATE (WORK(MAX(1, LWORK)))
CALL DSYEV('V', 'L', iblock_size, data_copy, iblock_size, eigenvalues, WORK, LWORK, INFO)
LWORK = INT(WORK(1))
DEALLOCATE (WORK)
! Allocate the workspace and solve the eigenproblem
ALLOCATE (WORK(MAX(1, LWORK)))
CALL DSYEV('V', 'L', iblock_size, data_copy, iblock_size, eigenvalues, WORK, LWORK, INFO)
IF (INFO .NE. 0) THEN
CPABORT("DSYEV failed")
END IF
! Copy occupied eigenvectors
IF (almo_scf_env%domain_t(idomain, ispin)%ncols .NE. &
almo_scf_env%nocc_of_domain(idomain, ispin)) THEN
CPABORT("wrong domain structure")
END IF
CALL copy_submatrices(almo_scf_env%domain_t(idomain, ispin), &
subm_t(idomain), .FALSE.)
CALL copy_submatrix_data(data_copy(:, 1:almo_scf_env%nocc_of_domain(idomain, ispin)), &
subm_t(idomain))
!! Copy occupied eigenvalues if smearing requested
IF (almo_scf_env%smear) THEN
almo_scf_env%mo_energies(1+SUM(almo_scf_env%nocc_of_domain(:idomain-1, ispin)) &
:SUM(almo_scf_env%nocc_of_domain(:idomain, ispin)), ispin) &
= eigenvalues(1:almo_scf_env%nocc_of_domain(idomain, ispin))
END IF
DEALLOCATE (WORK)
DEALLOCATE (data_copy)
DEALLOCATE (eigenvalues)
ENDIF ! submatrix for the domain exists
ENDDO ! loop over domains
CALL release_submatrices(subm_ks_xx_orthog)
! convert orbitals to the AO basis set (from orthogonalized AOs)
CALL multiply_submatrices('N', 'N', 1.0_dp, almo_scf_env%domain_s_sqrt_inv(:, ispin), &
subm_t, 0.0_dp, almo_scf_env%domain_t(:, ispin))
CALL release_submatrices(subm_t)
! convert domain orbitals to a dbcsr matrix
CALL construct_dbcsr_from_submatrices( &
almo_scf_env%matrix_t(ispin), &
almo_scf_env%domain_t(:, ispin), &
almo_scf_env%quench_t(ispin))
CALL dbcsr_filter(almo_scf_env%matrix_t(ispin), &
almo_scf_env%eps_filter)
! TRY: add T0 component
!!CALL dbcsr_add(almo_scf_env%matrix_t(ispin),&
!! almo_scf_env%matrix_t_blk(ispin),1.0_dp,1.0_dp)
ENDDO ! spins
DEALLOCATE (subm_tmp)
DEALLOCATE (subm_ks_xx_orthog)
DEALLOCATE (subm_t)
CALL timestop(handle)
END SUBROUTINE almo_scf_ks_xx_to_tv_xx
! **************************************************************************************************
!> \brief computes ALMOs by diagonalizing the projected blocked KS matrix
!> uses the diagonalization code for blocks
!> computes both the occupied and virtual orbitals
!> \param almo_scf_env ...
!> \par History
!> 2011.07 created [Rustam Z Khaliullin]
!> 2018.09 smearing support [Ruben Staub]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE almo_scf_ks_blk_to_tv_blk(almo_scf_env)
TYPE(almo_scf_env_type), INTENT(INOUT) :: almo_scf_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'almo_scf_ks_blk_to_tv_blk', &
routineP = moduleN//':'//routineN
INTEGER :: handle, iblock_col, iblock_row, &
iblock_size, info, ispin, lwork, &
nocc_of_block, nvirt_of_block, orbital
LOGICAL :: block_needed
REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalues, work
REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: data_copy
REAL(kind=dp), DIMENSION(:, :), POINTER :: data_p, p_new_block
TYPE(dbcsr_iterator_type) :: iter
TYPE(dbcsr_type) :: matrix_ks_blk_orthog, &
matrix_t_blk_orthog, matrix_tmp, &
matrix_v_blk_orthog
CALL timeset(routineN, handle)
IF (almo_scf_env%domain_layout_aos == almo_domain_layout_molecular .AND. &
almo_scf_env%mat_distr_aos == almo_mat_distr_atomic) THEN
CPABORT("a domain must be located entirely on a CPU")
ENDIF
DO ispin = 1, almo_scf_env%nspins
CALL dbcsr_create(matrix_tmp, template=almo_scf_env%matrix_ks_blk(ispin), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(matrix_ks_blk_orthog, template=almo_scf_env%matrix_ks_blk(ispin), &
matrix_type=dbcsr_type_no_symmetry)
! convert blocks to the orthogonal basis set
CALL dbcsr_multiply("N", "N", 1.0_dp, almo_scf_env%matrix_ks_blk(ispin), &
almo_scf_env%matrix_s_blk_sqrt_inv(1), 0.0_dp, matrix_tmp, &
filter_eps=almo_scf_env%eps_filter)
CALL dbcsr_multiply("N", "N", 1.0_dp, almo_scf_env%matrix_s_blk_sqrt_inv(1), &
matrix_tmp, 0.0_dp, matrix_ks_blk_orthog, &
filter_eps=almo_scf_env%eps_filter)
CALL dbcsr_release(matrix_tmp)
! create temporary matrices for occupied and virtual orbitals
! represented in the orthogonalized AOs basis set
CALL dbcsr_create(matrix_t_blk_orthog, template=almo_scf_env%matrix_t_blk(ispin))
CALL dbcsr_create(matrix_v_blk_orthog, template=almo_scf_env%matrix_v_full_blk(ispin))
CALL dbcsr_work_create(matrix_t_blk_orthog, work_mutable=.TRUE.)
CALL dbcsr_work_create(matrix_v_blk_orthog, work_mutable=.TRUE.)
CALL dbcsr_work_create(almo_scf_env%matrix_eoo(ispin), work_mutable=.TRUE.)
CALL dbcsr_work_create(almo_scf_env%matrix_evv_full(ispin), work_mutable=.TRUE.)
CALL dbcsr_iterator_start(iter, matrix_ks_blk_orthog)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, iblock_row, iblock_col, data_p, row_size=iblock_size)
IF (iblock_row .NE. iblock_col) THEN
CPABORT("off-diagonal block found")
ENDIF
block_needed = .TRUE.
IF (almo_scf_env%nocc_of_domain(iblock_col, ispin) .EQ. 0 .AND. &
almo_scf_env%nvirt_of_domain(iblock_col, ispin) .EQ. 0) THEN
block_needed = .FALSE.
ENDIF
IF (block_needed) THEN
! Prepare data
ALLOCATE (eigenvalues(iblock_size))
ALLOCATE (data_copy(iblock_size, iblock_size))
data_copy(:, :) = data_p(:, :)
! Query the optimal workspace for dsyev
LWORK = -1
ALLOCATE (WORK(MAX(1, LWORK)))
CALL DSYEV('V', 'L', iblock_size, data_copy, iblock_size, eigenvalues, WORK, LWORK, INFO)
LWORK = INT(WORK(1))
DEALLOCATE (WORK)
! Allocate the workspace and solve the eigenproblem
ALLOCATE (WORK(MAX(1, LWORK)))
CALL DSYEV('V', 'L', iblock_size, data_copy, iblock_size, eigenvalues, WORK, LWORK, INFO)
IF (INFO .NE. 0) THEN
CPABORT("DSYEV failed")
END IF
!!! RZK-warning !!!
!!! IT IS EXTREMELY IMPORTANT THAT THE DIAGONAL BLOCKS OF THE !!!
!!! FOLLOWING MATRICES ARE LOCATED ON THE SAME NODES WITH !!!
!!! THE CORRESPONDING DIAGONAL BLOCKS OF THE FOCK MATRIX: !!!
!!! T, V, E_o, E_v
! copy eigenvectors into two dbcsr matrices - occupied and virtuals
nocc_of_block = almo_scf_env%nocc_of_domain(iblock_col, ispin)
IF (nocc_of_block .GT. 0) THEN
NULLIFY (p_new_block)
CALL dbcsr_reserve_block2d(matrix_t_blk_orthog, iblock_row, iblock_col, p_new_block)
CPASSERT(ASSOCIATED(p_new_block))
p_new_block(:, :) = data_copy(:, 1:nocc_of_block)
! copy eigenvalues into diagonal dbcsr matrix - Eoo
NULLIFY (p_new_block)
CALL dbcsr_reserve_block2d(almo_scf_env%matrix_eoo(ispin), iblock_row, iblock_col, p_new_block)
CPASSERT(ASSOCIATED(p_new_block))
p_new_block(:, :) = 0.0_dp
DO orbital = 1, nocc_of_block
p_new_block(orbital, orbital) = eigenvalues(orbital)
ENDDO
!! Retrieve occupied MOs energies for smearing purpose, if requested
!! RS-WARNING: Hack to retrieve the occupied energies, since matrix_eoo seems to be ill-defined
!! for multiprocessing (any idea for fix?)
!! RS-WARNING: This section is not suitable for parallel run !!!
!! (but usually fails less than retrieving the diagonal of matrix_eoo)
!! RS-WARNING: This method will likely keep the energies of the initial guess if run in parallel
!! (which is still a reasonable smearing in most cases...)
IF (almo_scf_env%smear) THEN
DO orbital = 1, nocc_of_block
almo_scf_env%mo_energies(SUM(almo_scf_env%nocc_of_domain(:iblock_row-1, ispin))+orbital, &
ispin) = eigenvalues(orbital)
END DO
END IF
ENDIF
! now virtuals
nvirt_of_block = almo_scf_env%nvirt_of_domain(iblock_col, ispin)
IF (nvirt_of_block .GT. 0) THEN
NULLIFY (p_new_block)
CALL dbcsr_reserve_block2d(matrix_v_blk_orthog, iblock_row, iblock_col, p_new_block)
CPASSERT(ASSOCIATED(p_new_block))