-
Notifications
You must be signed in to change notification settings - Fork 1
/
bse_iterative.F
1589 lines (1282 loc) · 69.3 KB
/
bse_iterative.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 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Iterative routines for GW + Bethe-Salpeter for computing electronic excitations
!> \par History
!> 04.2017 created [Jan Wilhelm]
!> 11.2023 Davidson solver implemented [Maximilian Graml]
! **************************************************************************************************
MODULE bse_iterative
USE cp_fm_types, ONLY: cp_fm_get_info,&
cp_fm_type
USE group_dist_types, ONLY: get_group_dist,&
group_dist_d1_type
USE input_constants, ONLY: bse_singlet,&
bse_triplet
USE kinds, ONLY: dp
USE message_passing, ONLY: mp_para_env_type,&
mp_request_type
USE mp2_types, ONLY: integ_mat_buffer_type,&
mp2_type
USE physcon, ONLY: evolt
USE rpa_communication, ONLY: communicate_buffer
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'bse_iterative'
PUBLIC :: fill_local_3c_arrays, do_subspace_iterations
CONTAINS
! **************************************************************************************************
!> \brief ...
!> \param B_bar_ijQ_bse_local ...
!> \param B_abQ_bse_local ...
!> \param B_bar_iaQ_bse_local ...
!> \param B_iaQ_bse_local ...
!> \param homo ...
!> \param virtual ...
!> \param bse_spin_config ...
!> \param unit_nr ...
!> \param Eigenval ...
!> \param para_env ...
!> \param mp2_env ...
! **************************************************************************************************
SUBROUTINE do_subspace_iterations(B_bar_ijQ_bse_local, B_abQ_bse_local, B_bar_iaQ_bse_local, &
B_iaQ_bse_local, homo, virtual, bse_spin_config, unit_nr, &
Eigenval, para_env, mp2_env)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: B_bar_ijQ_bse_local, B_abQ_bse_local, &
B_bar_iaQ_bse_local, B_iaQ_bse_local
INTEGER :: homo, virtual, bse_spin_config, unit_nr
REAL(KIND=dp), DIMENSION(:) :: Eigenval
TYPE(mp_para_env_type), INTENT(IN) :: para_env
TYPE(mp2_type) :: mp2_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'do_subspace_iterations'
CHARACTER(LEN=10) :: bse_davidson_abort_cond_string, &
success_abort_string
INTEGER :: bse_davidson_abort_cond, davidson_converged, fac_max_z_space, handle, i_iter, &
j_print, local_RI_size, num_add_start_z_space, num_davidson_iter, num_en_unconverged, &
num_exact_en_unconverged, num_exc_en, num_max_z_space, num_new_t, num_res_unconverged, &
num_Z_vectors, num_Z_vectors_init
LOGICAL :: bse_full_diag_debug
REAL(kind=dp) :: eps_exc_en, eps_res, max_en_diff, &
max_res_norm, z_space_energy_cutoff
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: En_diffs, En_diffs_exact, Full_exc_spectrum, &
Res_norms, Subspace_full_eigenval, Subspace_new_eigenval, Subspace_prev_eigenval
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: AZ_reshaped, M_ia_tmp, M_ji_tmp, RI_vector, &
Subspace_new_eigenvec, Subspace_residuals_reshaped, Z_vectors_reshaped
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: AZ, BZ, Subspace_add_dir, &
Subspace_ritzvec, W_vectors, Z_vectors
CALL timeset(routineN, handle)
!MG to del
!Debug flag for exact diagonalization (only using lapack!!!)
bse_full_diag_debug = .TRUE.
num_en_unconverged = -1
num_res_unconverged = -1
num_exact_en_unconverged = -1
bse_davidson_abort_cond = mp2_env%bse%davidson_abort_cond
num_exc_en = mp2_env%bse%num_exc_en
num_add_start_z_space = mp2_env%bse%num_add_start_z_space
fac_max_z_space = mp2_env%bse%fac_max_z_space
num_new_t = mp2_env%bse%num_new_t
num_davidson_iter = mp2_env%bse%num_davidson_iter
eps_res = mp2_env%bse%eps_res
eps_exc_en = mp2_env%bse%eps_exc_en
z_space_energy_cutoff = mp2_env%bse%z_space_energy_cutoff
num_Z_vectors_init = num_exc_en + num_add_start_z_space
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) "bse_spin_config", bse_spin_config
WRITE (unit_nr, *) "num_exc_en", num_exc_en
WRITE (unit_nr, *) "num_add_start_z_space", num_add_start_z_space
WRITE (unit_nr, *) "num_Z_vectors_init", num_Z_vectors_init
WRITE (unit_nr, *) "fac_max_z_space", fac_max_z_space
WRITE (unit_nr, *) "num_new_t", num_new_t
WRITE (unit_nr, *) "eps_res", eps_res
WRITE (unit_nr, *) "num_davidson_iter", num_davidson_iter
WRITE (unit_nr, *) "eps_exc_en", eps_exc_en
WRITE (unit_nr, *) "bse_davidson_abort_cond", bse_davidson_abort_cond
WRITE (unit_nr, *) "z_space_energy_cutoff", z_space_energy_cutoff
WRITE (unit_nr, *) "Printing B_bar_iaQ_bse_local of shape", SHAPE(B_bar_iaQ_bse_local)
END IF
local_RI_size = SIZE(B_iaQ_bse_local, 3)
num_Z_vectors = num_Z_vectors_init
num_max_z_space = num_Z_vectors_init*fac_max_z_space
!Check input parameters and correct them if necessary
IF (num_new_t > num_Z_vectors_init) THEN
num_new_t = num_Z_vectors_init
IF (unit_nr > 0) THEN
CALL cp_warn(__LOCATION__, "Number of added directions has to be smaller/equals than "// &
"initial dimension. Corrected num_new_t accordingly.")
END IF
END IF
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) "Between BSE correction Warnings"
END IF
!If initial number is too big, already the first iteration causes trouble in LAPACK diagonal. (DORGQR)
IF (2*num_Z_vectors_init > homo*virtual) THEN
CALL cp_abort(__LOCATION__, "Initial dimension was too large and could not be corrected. "// &
"Choose another num_exc_en and num_add_start_z_space or adapt your basis set.")
END IF
IF (num_max_z_space .GE. homo*virtual) THEN
fac_max_z_space = homo*virtual/num_Z_vectors_init
num_max_z_space = num_Z_vectors_init*fac_max_z_space
IF (fac_max_z_space == 0) THEN
CALL cp_abort(__LOCATION__, "Maximal dimension was too large and could not be corrected. "// &
"Choose another fac_max_z_space and num_Z_vectors_init or adapt your basis set.")
ELSE
IF (unit_nr > 0) THEN
CALL cp_warn(__LOCATION__, "Maximal dimension of Z space has to be smaller than homo*virtual. "// &
"Corrected fac_max_z_space accordingly.")
END IF
END IF
END IF
DO i_iter = 1, num_davidson_iter
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) "Allocating Z_vec,AZ,BZ with dimensions (homo,virt,num_Z)", homo, virtual, num_Z_vectors
WRITE (unit_nr, *) 'ProcNr', para_env%mepos, 'you really enter here for i_iter', i_iter
END IF
ALLOCATE (Z_vectors(homo, virtual, num_Z_vectors))
Z_vectors = 0.0_dp
!Dellocation procedures are a bit intricate, W_/Z_vectors and eigenvalues are needed for the next iteration,
! therefore we have to deallocate them separately from the other quantities
IF (i_iter == 1) THEN
CALL initial_guess_Z_vectors(Z_vectors, Eigenval, num_Z_vectors, homo, virtual)
ALLOCATE (Subspace_prev_eigenval(num_exc_en))
Subspace_prev_eigenval = 0.0_dp
ELSE
Z_vectors(:, :, :) = W_vectors(:, :, :)
DEALLOCATE (W_vectors)
END IF
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) 'ProcNr', para_env%mepos, "Allocated/rewritten Z arrays"
END IF
CALL create_bse_work_arrays(AZ, Z_vectors_reshaped, AZ_reshaped, BZ, M_ia_tmp, M_ji_tmp, &
RI_vector, Subspace_new_eigenval, Subspace_full_eigenval, Subspace_new_eigenvec, &
Subspace_residuals_reshaped, Subspace_ritzvec, Subspace_add_dir, W_vectors, &
homo, virtual, num_Z_vectors, local_RI_size, num_new_t)
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) 'ProcNr', para_env%mepos, "Allocated Work arrays"
END IF
CALL compute_AZ(AZ, Z_vectors, B_iaQ_bse_local, B_bar_ijQ_bse_local, B_abQ_bse_local, &
M_ia_tmp, RI_vector, Eigenval, homo, virtual, num_Z_vectors, local_RI_size, &
para_env, bse_spin_config, z_space_energy_cutoff, i_iter, bse_full_diag_debug, &
Full_exc_spectrum, unit_nr)
!MG: functionality of BZ not checked (issue with fm_mat_Q_static_bse_gemm in rpa_util needs to be checked!)
!CALL compute_BZ(BZ, Z_vectors, B_iaQ_bse_local, B_bar_iaQ_bse_local, &
! M_ji_tmp, homo, virtual, num_Z_vectors, local_RI_size, &
! para_env)
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) 'ProcNr', para_env%mepos, "Computed AZ"
END IF
!MG to check: Reshaping correct?
AZ_reshaped(:, :) = RESHAPE(AZ, (/homo*virtual, num_Z_vectors/))
Z_vectors_reshaped(:, :) = RESHAPE(Z_vectors, (/homo*virtual, num_Z_vectors/))
! Diagonalize M and extract smallest eigenvalues/corresponding eigenvector
CALL compute_diagonalize_ZAZ(AZ_reshaped, Z_vectors_reshaped, num_Z_vectors, Subspace_new_eigenval, &
Subspace_new_eigenvec, num_new_t, Subspace_full_eigenval, para_env, unit_nr)
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) "Eigenval (eV) in iter=", i_iter, " is:", Subspace_new_eigenval(:6)*evolt
END IF
! Threshold in energies
CALL check_en_convergence(Subspace_full_eigenval, Subspace_prev_eigenval, eps_exc_en, num_en_unconverged, &
num_exc_en, max_en_diff, En_diffs)
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) "Largest change of desired exc ens =", max_en_diff
END IF
! Compute residuals
CALL compute_residuals(AZ_reshaped, Z_vectors_reshaped, Subspace_new_eigenval, Subspace_new_eigenvec, &
Subspace_residuals_reshaped, homo, virtual, num_new_t, num_Z_vectors, Subspace_ritzvec)
!Abort, if residuals are small enough w.r.t threshold
CALL check_res_convergence(Subspace_residuals_reshaped, num_new_t, eps_res, num_res_unconverged, &
i_iter, max_res_norm, unit_nr, Res_norms)
davidson_converged = -1
IF (num_res_unconverged == 0 .AND. bse_davidson_abort_cond /= 0) THEN
davidson_converged = 1
success_abort_string = "RESIDUALS"
ELSE IF (num_en_unconverged == 0 .AND. (bse_davidson_abort_cond /= 1)) THEN
davidson_converged = 1
success_abort_string = "ENERGIES"
ELSE IF (i_iter == num_davidson_iter) THEN
davidson_converged = -100
success_abort_string = "-----"
ELSE
davidson_converged = -1
END IF
IF (bse_davidson_abort_cond == 0) THEN
bse_davidson_abort_cond_string = "ENERGY"
ELSE IF (bse_davidson_abort_cond == 1) THEN
bse_davidson_abort_cond_string = "RESIDUAL"
ELSE
bse_davidson_abort_cond_string = "EITHER"
END IF
IF (davidson_converged == 1) THEN
CALL postprocess_bse(Subspace_full_eigenval, num_new_t, eps_res, num_res_unconverged, &
bse_spin_config, unit_nr, num_exc_en, num_Z_vectors_init, &
num_davidson_iter, i_iter, num_Z_vectors, num_max_z_space, max_res_norm, &
max_en_diff, num_en_unconverged, bse_davidson_abort_cond_string, &
eps_exc_en, success_abort_string, z_space_energy_cutoff)
!Deallocate matrices, which are otherwise not cleared due to exiting the loop
DEALLOCATE (AZ, BZ, &
Z_vectors, M_ia_tmp, M_ji_tmp, RI_vector, Subspace_prev_eigenval, &
Subspace_new_eigenval, Subspace_new_eigenvec, Subspace_residuals_reshaped, &
Subspace_add_dir, AZ_reshaped, Z_vectors_reshaped, Subspace_ritzvec, Subspace_full_eigenval)
EXIT
ELSE IF (davidson_converged < -1) THEN
CALL print_davidson_parameter(i_iter, num_davidson_iter, num_Z_vectors, num_res_unconverged, max_res_norm, &
eps_res, num_en_unconverged, max_en_diff, eps_exc_en, num_exc_en, &
num_Z_vectors_init, num_max_z_space, num_new_t, unit_nr, &
success_abort_string, bse_davidson_abort_cond_string, z_space_energy_cutoff)
CALL cp_abort(__LOCATION__, "BSE/TDA-Davidson did not converge using "// &
bse_davidson_abort_cond_string//" threshold condition!")
END IF
! Calculate and add next orthonormal vector and update num_Z_vectors
CALL compute_new_directions(homo, virtual, Subspace_residuals_reshaped, Subspace_new_eigenval, Eigenval, &
num_new_t, Subspace_add_dir)
!If exact-diag: compute difference to exact eigenvalues
IF (bse_full_diag_debug) THEN
ALLOCATE (En_diffs_exact(num_exc_en))
num_exact_en_unconverged = 0
DO j_print = 1, num_exc_en
En_diffs_exact(j_print) = ABS(Subspace_full_eigenval(j_print) - Full_exc_spectrum(j_print))
IF (En_diffs_exact(j_print) > eps_exc_en) num_exact_en_unconverged = num_exact_en_unconverged + 1
END DO
END IF
!Check dimensions and orthonormalize vector system, depending on dimensionality
CALL check_Z_space_dimension(W_vectors, Z_vectors, Subspace_add_dir, Subspace_ritzvec, &
num_Z_vectors, num_new_t, num_max_z_space, homo, virtual, i_iter, unit_nr)
!Copy eigenvalues for threshold
Subspace_prev_eigenval(:) = Subspace_full_eigenval(:num_exc_en)
DEALLOCATE (AZ, & !BZ,
Z_vectors, M_ia_tmp, M_ji_tmp, RI_vector, &
Subspace_new_eigenval, Subspace_new_eigenvec, Subspace_residuals_reshaped, &
Subspace_add_dir, AZ_reshaped, Z_vectors_reshaped, Subspace_ritzvec, Subspace_full_eigenval, &
Res_norms, En_diffs)
IF (bse_full_diag_debug) THEN
DEALLOCATE (En_diffs_exact)
END IF
!Orthonorm:
CALL orthonormalize_W(W_vectors, num_Z_vectors, homo, virtual)
END DO
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param W_vectors ...
!> \param Z_vectors ...
!> \param Subspace_add_dir ...
!> \param Subspace_ritzvec ...
!> \param num_Z_vectors ...
!> \param num_new_t ...
!> \param num_max_z_space ...
!> \param homo ...
!> \param virtual ...
!> \param i_iter ...
!> \param unit_nr ...
! **************************************************************************************************
SUBROUTINE check_Z_space_dimension(W_vectors, Z_vectors, Subspace_add_dir, Subspace_ritzvec, &
num_Z_vectors, num_new_t, num_max_z_space, homo, virtual, i_iter, unit_nr)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: W_vectors, Z_vectors, Subspace_add_dir, &
Subspace_ritzvec
INTEGER :: num_Z_vectors, num_new_t, &
num_max_z_space, homo, virtual, &
i_iter, unit_nr
CHARACTER(LEN=*), PARAMETER :: routineN = 'check_Z_space_dimension'
INTEGER :: handle
CALL timeset(routineN, handle)
IF (num_Z_vectors + num_new_t .LE. num_max_z_space) THEN
W_vectors(:, :, :num_Z_vectors) = Z_vectors(:, :, :)
W_vectors(:, :, num_Z_vectors + 1:) = Subspace_add_dir
num_Z_vectors = num_Z_vectors + num_new_t
ELSE
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) "Resetting dimension in i_iter=", i_iter
END IF
DEALLOCATE (W_vectors)
ALLOCATE (W_vectors(homo, virtual, 2*num_new_t))
W_vectors(:, :, :num_new_t) = Subspace_ritzvec(:, :, :)
W_vectors(:, :, num_new_t + 1:) = Subspace_add_dir
num_Z_vectors = 2*num_new_t
END IF
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param AZ ...
!> \param Z_vectors_reshaped ...
!> \param AZ_reshaped ...
!> \param BZ ...
!> \param M_ia_tmp ...
!> \param M_ji_tmp ...
!> \param RI_vector ...
!> \param Subspace_new_eigenval ...
!> \param Subspace_full_eigenval ...
!> \param Subspace_new_eigenvec ...
!> \param Subspace_residuals_reshaped ...
!> \param Subspace_ritzvec ...
!> \param Subspace_add_dir ...
!> \param W_vectors ...
!> \param homo ...
!> \param virtual ...
!> \param num_Z_vectors ...
!> \param local_RI_size ...
!> \param num_new_t ...
! **************************************************************************************************
SUBROUTINE create_bse_work_arrays(AZ, Z_vectors_reshaped, AZ_reshaped, BZ, M_ia_tmp, M_ji_tmp, &
RI_vector, Subspace_new_eigenval, Subspace_full_eigenval, Subspace_new_eigenvec, &
Subspace_residuals_reshaped, Subspace_ritzvec, Subspace_add_dir, W_vectors, &
homo, virtual, num_Z_vectors, local_RI_size, num_new_t)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: AZ
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: Z_vectors_reshaped, AZ_reshaped
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: BZ
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: M_ia_tmp, M_ji_tmp, RI_vector
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: Subspace_new_eigenval, &
Subspace_full_eigenval
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: Subspace_new_eigenvec, &
Subspace_residuals_reshaped
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: Subspace_ritzvec, Subspace_add_dir, &
W_vectors
INTEGER :: homo, virtual, num_Z_vectors, &
local_RI_size, num_new_t
CHARACTER(LEN=*), PARAMETER :: routineN = 'create_bse_work_arrays'
INTEGER :: handle
CALL timeset(routineN, handle)
ALLOCATE (AZ(homo, virtual, num_Z_vectors))
AZ = 0.0_dp
ALLOCATE (Z_vectors_reshaped(homo*virtual, num_Z_vectors))
Z_vectors_reshaped = 0.0_dp
ALLOCATE (AZ_reshaped(homo*virtual, num_Z_vectors))
AZ_reshaped = 0.0_dp
ALLOCATE (BZ(homo, virtual, num_Z_vectors))
BZ = 0.0_dp
ALLOCATE (M_ia_tmp(homo, virtual))
M_ia_tmp = 0.0_dp
ALLOCATE (M_ji_tmp(homo, homo))
M_ji_tmp = 0.0_dp
ALLOCATE (RI_vector(local_RI_size, num_Z_vectors))
RI_vector = 0.0_dp
ALLOCATE (Subspace_new_eigenval(num_new_t))
Subspace_new_eigenval = 0.0_dp
ALLOCATE (Subspace_full_eigenval(num_Z_vectors))
Subspace_full_eigenval = 0.0_dp
ALLOCATE (Subspace_new_eigenvec(num_Z_vectors, num_new_t))
Subspace_new_eigenvec = 0.0_dp
ALLOCATE (subspace_residuals_reshaped(homo*virtual, num_new_t))
subspace_residuals_reshaped = 0.0_dp
ALLOCATE (Subspace_ritzvec(homo, virtual, num_new_t))
Subspace_ritzvec = 0.0_dp
ALLOCATE (Subspace_add_dir(homo, virtual, num_new_t))
Subspace_add_dir = 0.0_dp
ALLOCATE (W_vectors(homo, virtual, num_Z_vectors + num_new_t))
W_vectors = 0.0_dp
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param Subspace_full_eigenval ...
!> \param num_new_t ...
!> \param eps_res ...
!> \param num_res_unconverged ...
!> \param bse_spin_config ...
!> \param unit_nr ...
!> \param num_exc_en ...
!> \param num_Z_vectors_init ...
!> \param num_davidson_iter ...
!> \param i_iter ...
!> \param num_Z_vectors ...
!> \param num_max_z_space ...
!> \param max_res_norm ...
!> \param max_en_diff ...
!> \param num_en_unconverged ...
!> \param bse_davidson_abort_cond_string ...
!> \param eps_exc_en ...
!> \param success_abort_string ...
!> \param z_space_energy_cutoff ...
! **************************************************************************************************
SUBROUTINE postprocess_bse(Subspace_full_eigenval, num_new_t, eps_res, num_res_unconverged, &
bse_spin_config, unit_nr, num_exc_en, num_Z_vectors_init, &
num_davidson_iter, i_iter, num_Z_vectors, num_max_z_space, max_res_norm, &
max_en_diff, num_en_unconverged, bse_davidson_abort_cond_string, &
eps_exc_en, success_abort_string, z_space_energy_cutoff)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: Subspace_full_eigenval
INTEGER :: num_new_t
REAL(KIND=dp) :: eps_res
INTEGER :: num_res_unconverged, bse_spin_config, unit_nr, num_exc_en, num_Z_vectors_init, &
num_davidson_iter, i_iter, num_Z_vectors, num_max_z_space
REAL(KIND=dp) :: max_res_norm, max_en_diff
INTEGER :: num_en_unconverged
CHARACTER(LEN=10) :: bse_davidson_abort_cond_string
REAL(KIND=dp) :: eps_exc_en
CHARACTER(LEN=10) :: success_abort_string
REAL(KIND=dp) :: z_space_energy_cutoff
CHARACTER(LEN=*), PARAMETER :: routineN = 'postprocess_bse'
CHARACTER(LEN=10) :: multiplet
INTEGER :: handle, i
REAL(KIND=dp) :: alpha
CALL timeset(routineN, handle)
!Prepare variables for printing
SELECT CASE (bse_spin_config)
CASE (bse_singlet)
alpha = 2.0_dp
multiplet = "Singlet"
CASE (bse_triplet)
alpha = 0.0_dp
multiplet = "Triplet"
END SELECT
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) ' '
WRITE (unit_nr, '(T3,A)') '******************************************************************************'
WRITE (unit_nr, '(T3,A)') '** **'
WRITE (unit_nr, '(T3,A)') '** BSE-TDA EXCITONIC ENERGIES **'
WRITE (unit_nr, '(T3,A)') '** **'
WRITE (unit_nr, '(T3,A)') '******************************************************************************'
WRITE (unit_nr, '(T3,A)') ' '
WRITE (unit_nr, '(T3,A)') ' '
WRITE (unit_nr, '(T3,A)') ' The excitation energies are calculated by iteratively diagonalizing: '
WRITE (unit_nr, '(T3,A)') ' '
WRITE (unit_nr, '(T3,A)') ' A_iajb = (E_a-E_i) delta_ij delta_ab + alpha * v_iajb - W_ijab '
WRITE (unit_nr, '(T3,A)') ' '
WRITE (unit_nr, '(T3,A48,A7,A12,F3.1)') &
' The spin-dependent factor for the requested ', multiplet, " is alpha = ", alpha
WRITE (unit_nr, '(T3,A)') ' '
WRITE (unit_nr, '(T3,A16,T50,A22)') &
' Excitonic level', 'Excitation energy (eV)'
!prints actual energies values
DO i = 1, num_exc_en
WRITE (unit_nr, '(T3,I16,T50,F22.3)') i, Subspace_full_eigenval(i)*evolt
END DO
WRITE (unit_nr, '(T3,A)') ' '
!prints parameters of Davidson algorithm
CALL print_davidson_parameter(i_iter, num_davidson_iter, num_Z_vectors, num_res_unconverged, max_res_norm, &
eps_res, num_en_unconverged, max_en_diff, eps_exc_en, num_exc_en, &
num_Z_vectors_init, num_max_z_space, num_new_t, unit_nr, &
success_abort_string, bse_davidson_abort_cond_string, z_space_energy_cutoff)
!Insert warning if energies are not converged (could probably be the case if one uses residual threshold)
IF (num_en_unconverged > 0) THEN
WRITE (unit_nr, '(T3,A)') '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
WRITE (unit_nr, '(T3,A2,T79,A2)') '!!', "!!"
WRITE (unit_nr, '(T3,A2,T8,A65,T79,A2)') '!!', "THERE ARE UNCONVERGED ENERGIES PRINTED OUT, SOMETHING WENT WRONG!", "!!"
WRITE (unit_nr, '(T3,A2,T79,A2)') '!!', "!!"
WRITE (unit_nr, '(T3,A)') '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
END IF
END IF
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param i_iter ...
!> \param num_davidson_iter ...
!> \param num_Z_vectors ...
!> \param num_res_unconverged ...
!> \param max_res_norm ...
!> \param eps_res ...
!> \param num_en_unconverged ...
!> \param max_en_diff ...
!> \param eps_exc_en ...
!> \param num_exc_en ...
!> \param num_Z_vectors_init ...
!> \param num_max_z_space ...
!> \param num_new_t ...
!> \param unit_nr ...
!> \param success_abort_string ...
!> \param bse_davidson_abort_cond_string ...
!> \param z_space_energy_cutoff ...
! **************************************************************************************************
SUBROUTINE print_davidson_parameter(i_iter, num_davidson_iter, num_Z_vectors, num_res_unconverged, max_res_norm, &
eps_res, num_en_unconverged, max_en_diff, eps_exc_en, num_exc_en, &
num_Z_vectors_init, num_max_z_space, num_new_t, unit_nr, &
success_abort_string, bse_davidson_abort_cond_string, z_space_energy_cutoff)
INTEGER :: i_iter, num_davidson_iter, &
num_Z_vectors, num_res_unconverged
REAL(KIND=dp) :: max_res_norm, eps_res
INTEGER :: num_en_unconverged
REAL(KIND=dp) :: max_en_diff, eps_exc_en
INTEGER :: num_exc_en, num_Z_vectors_init, &
num_max_z_space, num_new_t, unit_nr
CHARACTER(LEN=10) :: success_abort_string, &
bse_davidson_abort_cond_string
REAL(KIND=dp) :: z_space_energy_cutoff
CHARACTER(LEN=*), PARAMETER :: routineN = 'print_davidson_parameter'
INTEGER :: handle
CALL timeset(routineN, handle)
WRITE (unit_nr, '(T3,A)') '******************************************************************************'
WRITE (unit_nr, '(T3,A2,T15,A49,T79,A2)') &
'**', "Parameters of the BSE-Davidson solver:", "**"
WRITE (unit_nr, '(T3,A2,T79,A2)') &
'**', "**"
WRITE (unit_nr, '(T3,A2,T79,A2)') &
'**', "**"
WRITE (unit_nr, '(T3,A2,T10,A16,I5,A12,I5,A8,T79,A2)') &
'**', "Converged after ", i_iter, " of maximal ", num_davidson_iter, " cycles,", "**"
WRITE (unit_nr, '(T3,A2,T20,A11,A9,A7,A8,A20,T79,A2)') &
'**', "because of ", success_abort_string, " using ", &
bse_davidson_abort_cond_string, " threshold condition", "**"
WRITE (unit_nr, '(T3,A2,T79,A2)') &
'**', "**"
WRITE (unit_nr, '(T3,A2,T10,A32,T65,I11,T79,A2)') &
'**', "The Z space has at the end dim. ", num_Z_vectors, "**"
WRITE (unit_nr, '(T3,A2,T10,A45,T65,I11,T79,A2)') &
'**', "Number of unconverged residuals in subspace: ", num_res_unconverged, "**"
WRITE (unit_nr, '(T3,A2,T10,A35,T65,E11.4,T79,A2)') &
'**', "largest unconverged residual (eV): ", max_res_norm*evolt, "**"
WRITE (unit_nr, '(T3,A2,T10,A45,T65,E11.4,T79,A2)') &
'**', "threshold for convergence of residuals (eV): ", eps_res*evolt, "**"
WRITE (unit_nr, '(T3,A2,T10,A45,T65,I11,T79,A2)') &
'**', "Number of desired, but unconverged energies: ", num_en_unconverged, "**"
WRITE (unit_nr, '(T3,A2,T10,A44,T65,E11.4,T79,A2)') &
'**', "largest unconverged energy difference (eV): ", max_en_diff*evolt, "**"
WRITE (unit_nr, '(T3,A2,T10,A44,T65,E11.4,T79,A2)') &
'**', "threshold for convergence of energies (eV): ", eps_exc_en*evolt, "**"
WRITE (unit_nr, '(T3,A2,T10,A40,T65,I11,T79,A2)') &
'**', "number of computed excitation energies: ", num_exc_en, "**"
IF (z_space_energy_cutoff > 0) THEN
WRITE (unit_nr, '(T3,A2,T10,A37,T65,E11.4,T79,A2)') &
'**', "cutoff for excitation energies (eV): ", z_space_energy_cutoff*evolt, "**"
END IF
WRITE (unit_nr, '(T3,A2,T10,A36,T65,I11,T79,A2)') &
'**', "number of Z space at the beginning: ", num_Z_vectors_init, "**"
WRITE (unit_nr, '(T3,A2,T10,A30,T65,I11,T79,A2)') &
'**', "maximal dimension of Z space: ", num_max_z_space, "**"
WRITE (unit_nr, '(T3,A2,T10,A31,T65,I11,T79,A2)') &
'**', "added directions per iteration: ", num_new_t, "**"
WRITE (unit_nr, '(T3,A2,T79,A2)') &
'**', "**"
WRITE (unit_nr, '(T3,A2,T79,A2)') &
'**', "**"
WRITE (unit_nr, '(T3,A)') '******************************************************************************'
WRITE (unit_nr, '(T3,A)') ' '
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param Subspace_full_eigenval ...
!> \param Subspace_prev_eigenval ...
!> \param eps_exc_en ...
!> \param num_en_unconverged ...
!> \param num_exc_en ...
!> \param max_en_diff ...
!> \param En_diffs ...
! **************************************************************************************************
SUBROUTINE check_en_convergence(Subspace_full_eigenval, Subspace_prev_eigenval, eps_exc_en, num_en_unconverged, &
num_exc_en, max_en_diff, En_diffs)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: Subspace_full_eigenval, &
Subspace_prev_eigenval
REAL(KIND=dp) :: eps_exc_en
INTEGER :: num_en_unconverged, num_exc_en
REAL(KIND=dp) :: max_en_diff
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: En_diffs
CHARACTER(LEN=*), PARAMETER :: routineN = 'check_en_convergence'
INTEGER :: handle, mu_l
CALL timeset(routineN, handle)
num_en_unconverged = 0
ALLOCATE (En_diffs(num_exc_en))
DO mu_l = 1, num_exc_en
En_diffs(mu_l) = ABS(Subspace_full_eigenval(mu_l) - Subspace_prev_eigenval(mu_l))
IF (En_diffs(mu_l) > eps_exc_en) num_en_unconverged = num_en_unconverged + 1
END DO
max_en_diff = MAXVAL(En_diffs)
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param Subspace_residuals_reshaped ...
!> \param num_new_t ...
!> \param eps_res ...
!> \param num_res_unconverged ...
!> \param i_iter ...
!> \param max_res_norm ...
!> \param unit_nr ...
!> \param Res_norms ...
! **************************************************************************************************
SUBROUTINE check_res_convergence(Subspace_residuals_reshaped, num_new_t, eps_res, num_res_unconverged, &
i_iter, max_res_norm, unit_nr, Res_norms)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: Subspace_residuals_reshaped
INTEGER :: num_new_t
REAL(KIND=dp) :: eps_res
INTEGER :: num_res_unconverged, i_iter
REAL(KIND=dp) :: max_res_norm
INTEGER :: unit_nr
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: Res_norms
CHARACTER(LEN=*), PARAMETER :: routineN = 'check_res_convergence'
INTEGER :: handle, mu_l
CALL timeset(routineN, handle)
num_res_unconverged = 0
ALLOCATE (Res_norms(num_new_t))
DO mu_l = 1, num_new_t
Res_norms(mu_l) = NORM2(Subspace_residuals_reshaped(:, mu_l))
IF (Res_norms(mu_l) > eps_res) THEN
num_res_unconverged = num_res_unconverged + 1
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) "Unconverged res in i_iter=", i_iter, "is:", Res_norms(mu_l)
END IF
END IF
END DO
max_res_norm = MAXVAL(Res_norms)
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) "Maximal unconverged res (of ", num_res_unconverged, &
" unconverged res in this step) in i_iter=", i_iter, "is:", max_res_norm
END IF
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param W_vectors ...
!> \param num_Z_vectors ...
!> \param homo ...
!> \param virtual ...
! **************************************************************************************************
SUBROUTINE orthonormalize_W(W_vectors, num_Z_vectors, homo, virtual)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: W_vectors
INTEGER :: num_Z_vectors, homo, virtual
CHARACTER(LEN=*), PARAMETER :: routineN = 'orthonormalize_W'
INTEGER :: handle, info_dor, info_orth, LWORK_dor, &
LWORK_W
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: Tau_W, WORK_W, WORK_W_dor
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: W_vectors_reshaped
CALL timeset(routineN, handle)
ALLOCATE (W_vectors_reshaped(homo*virtual, num_Z_vectors))
W_vectors_reshaped(:, :) = RESHAPE(W_vectors, (/homo*virtual, num_Z_vectors/))
ALLOCATE (Tau_W(MIN(homo*virtual, num_Z_vectors)))
Tau_W = 0.0_dp
ALLOCATE (WORK_W(1))
WORK_W = 0.0_dp
ALLOCATE (WORK_W_dor(1))
WORK_W_dor = 0.0_dp
CALL DGEQRF(homo*virtual, num_Z_vectors, W_vectors_reshaped, homo*virtual, Tau_W, WORK_W, -1, info_orth)
LWORK_W = INT(WORK_W(1))
DEALLOCATE (WORK_W)
ALLOCATE (WORK_W(LWORK_W))
WORK_W = 0.0_dp
CALL DGEQRF(homo*virtual, num_Z_vectors, W_vectors_reshaped, homo*virtual, Tau_W, WORK_W, LWORK_W, info_orth)
IF (info_orth /= 0) THEN
CPABORT("QR Decomp Step 1 doesnt work")
END IF
CALL DORGQR(homo*virtual, num_Z_vectors, MIN(homo*virtual, num_Z_vectors), W_vectors_reshaped, homo*virtual, &
Tau_W, WORK_W_dor, -1, info_dor)
LWORK_dor = INT(WORK_W_dor(1))
DEALLOCATE (WORK_W_dor)
ALLOCATE (WORK_W_dor(LWORK_dor))
WORK_W_dor = 0.0_dp
CALL DORGQR(homo*virtual, num_Z_vectors, MIN(homo*virtual, num_Z_vectors), W_vectors_reshaped, homo*virtual, &
Tau_W, WORK_W_dor, LWORK_dor, info_dor)
IF (info_orth /= 0) THEN
CPABORT("QR Decomp Step 2 doesnt work")
END IF
W_vectors(:, :, :) = RESHAPE(W_vectors_reshaped, (/homo, virtual, num_Z_vectors/))
DEALLOCATE (WORK_W, WORK_W_dor, Tau_W, W_vectors_reshaped)
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param homo ...
!> \param virtual ...
!> \param Subspace_residuals_reshaped ...
!> \param Subspace_new_eigenval ...
!> \param Eigenval ...
!> \param num_new_t ...
!> \param Subspace_add_dir ...
! **************************************************************************************************
SUBROUTINE compute_new_directions(homo, virtual, Subspace_residuals_reshaped, Subspace_new_eigenval, Eigenval, &
num_new_t, Subspace_add_dir)
INTEGER :: homo, virtual
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: Subspace_residuals_reshaped
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: Subspace_new_eigenval
REAL(KIND=dp), DIMENSION(:) :: Eigenval
INTEGER :: num_new_t
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: Subspace_add_dir
CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_new_directions'
INTEGER :: a_virt, handle, i_occ, mu_subspace, &
prec_neg
REAL(KIND=dp) :: prec_scalar
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: Subspace_add_dir_reshaped
CALL timeset(routineN, handle)
ALLOCATE (Subspace_add_dir_reshaped(homo*virtual, num_new_t))
prec_neg = 0
DO mu_subspace = 1, num_new_t
DO i_occ = 1, homo
DO a_virt = 1, virtual
!MG to check: Indexorder and range of indices
prec_scalar = -1/(Subspace_new_eigenval(mu_subspace) - (Eigenval(a_virt + homo) - Eigenval(i_occ)))
IF (prec_scalar < 0) THEN
prec_neg = prec_neg + 1
!prec_scalar = - prec_scalar
END IF
Subspace_add_dir_reshaped((i_occ - 1)*virtual + a_virt, mu_subspace) = prec_scalar* &
Subspace_residuals_reshaped((i_occ - 1)*virtual + a_virt, mu_subspace)
END DO
END DO
END DO
Subspace_add_dir(:, :, :) = RESHAPE(Subspace_add_dir_reshaped, (/homo, virtual, num_new_t/))
DEALLOCATE (Subspace_add_dir_reshaped)
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param AZ_reshaped ...
!> \param Z_vectors_reshaped ...
!> \param Subspace_new_eigenval ...
!> \param Subspace_new_eigenvec ...
!> \param Subspace_residuals_reshaped ...
!> \param homo ...
!> \param virtual ...
!> \param num_new_t ...
!> \param num_Z_vectors ...
!> \param Subspace_ritzvec ...
! **************************************************************************************************
SUBROUTINE compute_residuals(AZ_reshaped, Z_vectors_reshaped, Subspace_new_eigenval, Subspace_new_eigenvec, &
Subspace_residuals_reshaped, homo, virtual, num_new_t, num_Z_vectors, Subspace_ritzvec)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: AZ_reshaped, Z_vectors_reshaped
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: Subspace_new_eigenval
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: Subspace_new_eigenvec, &
Subspace_residuals_reshaped
INTEGER :: homo, virtual, num_new_t, num_Z_vectors
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: Subspace_ritzvec
CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_residuals'
INTEGER :: handle, mu_subspace
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: Subspace_res_A, Subspace_res_ev
CALL timeset(routineN, handle)
ALLOCATE (Subspace_res_ev(homo*virtual, num_new_t))
Subspace_res_ev = 0.0_dp
ALLOCATE (Subspace_res_A(homo*virtual, num_new_t))
Subspace_res_A = 0.0_dp
!Compute all residuals in one loop, iterating over number of new/added t per iteration
DO mu_subspace = 1, num_new_t
CALL DGEMM("N", "N", homo*virtual, 1, num_Z_vectors, 1.0_dp, Z_vectors_reshaped, homo*virtual, &
Subspace_new_eigenvec(:, mu_subspace), num_Z_vectors, 0.0_dp, Subspace_res_ev(:, mu_subspace), homo*virtual)
CALL DGEMM("N", "N", homo*virtual, 1, num_Z_vectors, 1.0_dp, AZ_reshaped, homo*virtual, &
Subspace_new_eigenvec(:, mu_subspace), num_Z_vectors, 0.0_dp, Subspace_res_A(:, mu_subspace), homo*virtual)
Subspace_residuals_reshaped(:, mu_subspace) = Subspace_new_eigenval(mu_subspace)*Subspace_res_ev(:, mu_subspace) &
- Subspace_res_A(:, mu_subspace)
END DO
Subspace_ritzvec(:, :, :) = RESHAPE(Subspace_res_ev, (/homo, virtual, num_new_t/))
DEALLOCATE (Subspace_res_ev, Subspace_res_A)
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param AZ_reshaped ...
!> \param Z_vectors_reshaped ...
!> \param num_Z_vectors ...
!> \param Subspace_new_eigenval ...
!> \param Subspace_new_eigenvec ...
!> \param num_new_t ...
!> \param Subspace_full_eigenval ...
!> \param para_env ...
!> \param unit_nr ...
! **************************************************************************************************
SUBROUTINE compute_diagonalize_ZAZ(AZ_reshaped, Z_vectors_reshaped, num_Z_vectors, Subspace_new_eigenval, &
Subspace_new_eigenvec, num_new_t, Subspace_full_eigenval, para_env, unit_nr)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: AZ_reshaped, Z_vectors_reshaped
INTEGER, INTENT(in) :: num_Z_vectors
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: Subspace_new_eigenval
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: Subspace_new_eigenvec
INTEGER, INTENT(in) :: num_new_t
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: Subspace_full_eigenval
TYPE(mp_para_env_type), INTENT(IN) :: para_env
INTEGER, INTENT(in) :: unit_nr
CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_diagonalize_ZAZ'
INTEGER :: handle, i_Z_vector, j_Z_vector, LWORK, &
ZAZ_diag_info
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: WORK
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: ZAZ
CALL timeset(routineN, handle)
ALLOCATE (ZAZ(num_Z_vectors, num_Z_vectors))
ZAZ(:, :) = 0.0_dp
!Flatten AZ and Z matrices of a certain j_Z_vector w.r.t. occ and virt indices
!Multiply for each j_Z_vec and write into matrix of dim (num_Z_vec, num_Z_vec)
DO i_Z_vector = 1, num_Z_vectors
DO j_Z_vector = 1, num_Z_vectors
ZAZ(j_Z_vector, i_Z_vector) = DOT_PRODUCT(Z_vectors_reshaped(:, j_Z_vector), AZ_reshaped(:, i_Z_vector))
END DO
END DO
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) 'ProcNr', para_env%mepos, "Before Diag"
END IF
!MG to do: Check for symmetry of ZAZ!
ALLOCATE (WORK(1))
WORK = 0.0_dp
CALL DSYEV("V", "U", num_Z_vectors, ZAZ, num_Z_vectors, Subspace_full_eigenval, WORK, -1, ZAZ_diag_info)
LWORK = INT(WORK(1))
DEALLOCATE (WORK)
ALLOCATE (WORK(LWORK))
WORK = 0.0_dp
!MG to check: Usage of symmetric routine okay? (Correct LWORK?)
CALL DSYEV("V", "U", num_Z_vectors, ZAZ, num_Z_vectors, Subspace_full_eigenval, WORK, LWORK, ZAZ_diag_info)
IF (ZAZ_diag_info /= 0) THEN
CPABORT("ZAZ could not be diagonalized successfully.")
END IF
IF (unit_nr > 0) THEN
WRITE (unit_nr, *) 'ProcNr', para_env%mepos, "After Diag"
END IF
Subspace_new_eigenval(1:num_new_t) = Subspace_full_eigenval(1:num_new_t)
Subspace_new_eigenvec(:, 1:num_new_t) = ZAZ(:, 1:num_new_t)
DEALLOCATE (WORK)
DEALLOCATE (ZAZ)
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param BZ ...
!> \param Z_vectors ...
!> \param B_iaQ_bse_local ...
!> \param B_bar_iaQ_bse_local ...
!> \param M_ji_tmp ...
!> \param homo ...
!> \param virtual ...
!> \param num_Z_vectors ...
!> \param local_RI_size ...
!> \param para_env ...
! **************************************************************************************************
SUBROUTINE compute_BZ(BZ, Z_vectors, B_iaQ_bse_local, B_bar_iaQ_bse_local, &
M_ji_tmp, homo, virtual, num_Z_vectors, local_RI_size, para_env)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: BZ, Z_vectors, B_iaQ_bse_local, &
B_bar_iaQ_bse_local
REAL(KIND=dp), DIMENSION(:, :) :: M_ji_tmp