-
Notifications
You must be signed in to change notification settings - Fork 1
/
xas_tdp_correction.F
1922 lines (1563 loc) · 93.9 KB
/
xas_tdp_correction.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 Second order perturbation correction to XAS_TDP spectra (i.e. shift)
!> \author A. Bussy (01.2020)
! **************************************************************************************************
MODULE xas_tdp_correction
USE admm_types, ONLY: admm_type
USE admm_utils, ONLY: admm_correct_for_eigenvalues
USE bibliography, ONLY: Bussy2021b,&
Shigeta2001,&
cite_reference
USE cp_array_utils, ONLY: cp_1d_i_p_type,&
cp_1d_r_p_type
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_cfm_types, ONLY: cp_cfm_create,&
cp_cfm_get_submatrix,&
cp_cfm_release,&
cp_cfm_type,&
cp_fm_to_cfm
USE cp_control_types, ONLY: dft_control_type
USE cp_dbcsr_api, ONLY: &
dbcsr_copy, dbcsr_create, dbcsr_distribution_get, dbcsr_distribution_new, &
dbcsr_distribution_release, dbcsr_distribution_type, dbcsr_get_info, dbcsr_p_type, &
dbcsr_release, dbcsr_type
USE cp_dbcsr_operations, ONLY: copy_fm_to_dbcsr,&
cp_dbcsr_sm_fm_multiply,&
dbcsr_deallocate_matrix_set
USE cp_fm_diag, ONLY: choose_eigv_solver
USE cp_fm_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_p_type,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_create,&
cp_fm_get_diag,&
cp_fm_get_submatrix,&
cp_fm_release,&
cp_fm_to_fm,&
cp_fm_to_fm_submat,&
cp_fm_type
USE cp_log_handling, ONLY: cp_logger_get_default_io_unit
USE dbt_api, ONLY: &
dbt_contract, dbt_copy, dbt_copy_matrix_to_tensor, dbt_create, dbt_default_distvec, &
dbt_destroy, dbt_distribution_destroy, dbt_distribution_new, dbt_distribution_type, &
dbt_finalize, dbt_get_block, dbt_get_info, dbt_iterator_blocks_left, &
dbt_iterator_next_block, dbt_iterator_start, dbt_iterator_stop, dbt_iterator_type, &
dbt_pgrid_create, dbt_pgrid_destroy, dbt_pgrid_type, dbt_put_block, dbt_type
USE hfx_admm_utils, ONLY: create_admm_xc_section
USE input_section_types, ONLY: section_vals_create,&
section_vals_get_subs_vals,&
section_vals_release,&
section_vals_retain,&
section_vals_set_subs_vals,&
section_vals_type
USE kinds, ONLY: dp
USE machine, ONLY: m_flush
USE mathlib, ONLY: complex_diag
USE message_passing, ONLY: mp_para_env_type
USE parallel_gemm_api, ONLY: parallel_gemm
USE physcon, ONLY: evolt
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_ks_methods, ONLY: qs_ks_build_kohn_sham_matrix
USE qs_mo_types, ONLY: deallocate_mo_set,&
duplicate_mo_set,&
get_mo_set,&
mo_set_type,&
reassign_allocated_mos
USE util, ONLY: get_limit
USE xas_tdp_kernel, ONLY: contract2_AO_to_doMO,&
ri_all_blocks_mm
USE xas_tdp_types, ONLY: donor_state_type,&
xas_tdp_control_type,&
xas_tdp_env_type
!$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'xas_tdp_correction'
PUBLIC :: gw2x_shift, get_soc_splitting
CONTAINS
! **************************************************************************************************
!> \brief Computes the ionization potential using the GW2X method of Shigeta et. al. The result cam
!> be used for XAS correction (shift) or XPS directly.
!> \param donor_state ...
!> \param xas_tdp_env ...
!> \param xas_tdp_control ...
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE GW2X_shift(donor_state, xas_tdp_env, xas_tdp_control, qs_env)
TYPE(donor_state_type), POINTER :: donor_state
TYPE(xas_tdp_env_type), POINTER :: xas_tdp_env
TYPE(xas_tdp_control_type), POINTER :: xas_tdp_control
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'GW2X_shift'
INTEGER :: ex_idx, exat, first_domo(2), handle, i, ido_mo, iloc, ilocat, ispin, jspin, &
locat, nao, natom, ndo_mo, nhomo(2), nlumo(2), nonloc, nspins, start_sgf
INTEGER, DIMENSION(:), POINTER :: nsgf_blk
LOGICAL :: pseudo_canonical
REAL(dp) :: og_hfx_frac
REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: contract_coeffs_backup
TYPE(admm_type), POINTER :: admm_env
TYPE(cp_1d_r_p_type), ALLOCATABLE, DIMENSION(:) :: homo_evals, lumo_evals
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(cp_fm_struct_p_type), ALLOCATABLE, &
DIMENSION(:) :: all_struct, homo_struct, lumo_struct
TYPE(cp_fm_struct_type), POINTER :: hoho_struct, lulu_struct
TYPE(cp_fm_type) :: hoho_fock, hoho_work, homo_work, &
lulu_fock, lulu_work, lumo_work
TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: all_coeffs, homo_coeffs, lumo_coeffs
TYPE(cp_fm_type), POINTER :: mo_coeff
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: dbcsr_work, fock_matrix, matrix_ks
TYPE(dbt_type), ALLOCATABLE, DIMENSION(:) :: ja_X, oI_Y
TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :) :: mo_template
TYPE(dft_control_type), POINTER :: dft_control
TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(section_vals_type), POINTER :: xc_fun_empty, xc_fun_original, xc_section
NULLIFY (xc_fun_empty, xc_fun_original, xc_section, mos, dft_control, dbcsr_work, &
fock_matrix, matrix_ks, para_env, mo_coeff, blacs_env, nsgf_blk)
CALL cite_reference(Shigeta2001)
CALL cite_reference(Bussy2021b)
CALL timeset(routineN, handle)
!The GW2X correction we want to compute goes like this, where omega is the corrected epsilon_I:
!omega = eps_I + 0.5 * sum_ajk |<Ia||jk>|^2/(omega + eps_a - eps_j - eps_k)
! + 0.5 * sum_jab |<Ij||ab>|^2/(omega + eps_j - eps_a - eps_b)
! j,k denote occupied spin-orbitals and a,b denote virtual spin orbitals
!The strategy is the following (we assume restricted closed-shell):
!1) Get the LUMOs from xas_tdp_env
!2) Get the HOMOs from qs_env
!3) Compute or fetch the generalize Fock matric
!4) Diagonalize it in the subspace of HOMOs and LUMOs (or just take diagonal matrix elements)
!5) Build the full HOMO-LUMO basis that we will use and compute eigenvalues
!6) Iterate over GW2X steps to compute the self energy
!We implement 2 approaches => diagonal elements of Fock matrix with original MOs and
!pseudo-canonical MOs
pseudo_canonical = xas_tdp_control%pseudo_canonical
!Get donor state info
ndo_mo = donor_state%ndo_mo
nspins = 1; IF (xas_tdp_control%do_uks .OR. xas_tdp_control%do_roks) nspins = 2
!1) Get the LUMO coefficients from the xas_tdp_env, that have been precomputed
CALL get_qs_env(qs_env, matrix_ks=matrix_ks, mos=mos, para_env=para_env, &
blacs_env=blacs_env, natom=natom)
ALLOCATE (lumo_struct(nspins), lumo_coeffs(nspins))
DO ispin = 1, nspins
CALL get_mo_set(mos(ispin), homo=nhomo(ispin), nao=nao)
nlumo(ispin) = nao - nhomo(ispin)
CALL cp_fm_struct_create(lumo_struct(ispin)%struct, para_env=para_env, context=blacs_env, &
ncol_global=nlumo(ispin), nrow_global=nao)
CALL cp_fm_create(lumo_coeffs(ispin), lumo_struct(ispin)%struct)
CALL cp_fm_to_fm(xas_tdp_env%lumo_evecs(ispin), lumo_coeffs(ispin))
END DO
!2) get the HOMO coeffs. Reminder: keep all non-localized MOs + those localized on core atom
! For this to work, it is assumed that the LOCALIZE keyword is used
ALLOCATE (homo_struct(nspins), homo_coeffs(nspins))
DO ispin = 1, nspins
nonloc = nhomo(ispin) - xas_tdp_control%n_search
exat = donor_state%at_index
ex_idx = MINLOC(ABS(xas_tdp_env%ex_atom_indices - exat), 1)
locat = COUNT(xas_tdp_env%mos_of_ex_atoms(:, ex_idx, ispin) == 1)
CALL cp_fm_struct_create(homo_struct(ispin)%struct, para_env=para_env, context=blacs_env, &
ncol_global=locat + nonloc, nrow_global=nao)
CALL cp_fm_create(homo_coeffs(ispin), homo_struct(ispin)%struct)
CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff)
CALL cp_fm_to_fm_submat(mo_coeff, homo_coeffs(ispin), nrow=nao, ncol=nonloc, s_firstrow=1, &
s_firstcol=xas_tdp_control%n_search + 1, t_firstrow=1, t_firstcol=locat + 1)
!this bit is taken from xas_tdp_methods
ilocat = 1
DO iloc = 1, xas_tdp_control%n_search
IF (xas_tdp_env%mos_of_ex_atoms(iloc, ex_idx, ispin) == -1) CYCLE
CALL cp_fm_to_fm_submat(mo_coeff, homo_coeffs(ispin), nrow=nao, ncol=1, s_firstrow=1, &
s_firstcol=iloc, t_firstrow=1, t_firstcol=ilocat)
!keep track of donor MO index
IF (iloc == donor_state%mo_indices(1, ispin)) first_domo(ispin) = ilocat !first donor MO
ilocat = ilocat + 1
END DO
nhomo(ispin) = locat + nonloc
END DO
!3) Computing the generalized Fock Matrix, if not there already
IF (ASSOCIATED(xas_tdp_env%fock_matrix)) THEN
fock_matrix => xas_tdp_env%fock_matrix
ELSE
BLOCK
TYPE(mo_set_type), DIMENSION(:), ALLOCATABLE :: backup_mos
ALLOCATE (xas_tdp_env%fock_matrix(nspins))
fock_matrix => xas_tdp_env%fock_matrix
! remove the xc_functionals and set HF fraction to 1
xc_section => section_vals_get_subs_vals(qs_env%input, "DFT%XC")
xc_fun_original => section_vals_get_subs_vals(xc_section, "XC_FUNCTIONAL")
CALL section_vals_retain(xc_fun_original)
CALL section_vals_create(xc_fun_empty, xc_fun_original%section)
CALL section_vals_set_subs_vals(xc_section, "XC_FUNCTIONAL", xc_fun_empty)
CALL section_vals_release(xc_fun_empty)
og_hfx_frac = qs_env%x_data(1, 1)%general_parameter%fraction
qs_env%x_data(:, :)%general_parameter%fraction = 1.0_dp
!In case of ADMM, we need to re-create the admm XC section for the new hfx_fraction
!We also need to make a backup of the MOs as theiy are modified
CALL get_qs_env(qs_env, dft_control=dft_control, admm_env=admm_env)
IF (dft_control%do_admm) THEN
IF (ASSOCIATED(admm_env%xc_section_primary)) CALL section_vals_release(admm_env%xc_section_primary)
IF (ASSOCIATED(admm_env%xc_section_aux)) CALL section_vals_release(admm_env%xc_section_aux)
CALL create_admm_xc_section(qs_env%x_data, xc_section, admm_env)
ALLOCATE (backup_mos(SIZE(mos)))
DO i = 1, SIZE(mos)
CALL duplicate_mo_set(backup_mos(i), mos(i))
END DO
END IF
ALLOCATE (dbcsr_work(nspins))
DO ispin = 1, nspins
ALLOCATE (dbcsr_work(ispin)%matrix)
CALL dbcsr_copy(dbcsr_work(ispin)%matrix, matrix_ks(ispin)%matrix)
END DO
!both spins treated internally
CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.FALSE., just_energy=.FALSE.)
DO ispin = 1, nspins
ALLOCATE (fock_matrix(ispin)%matrix)
CALL dbcsr_copy(fock_matrix(ispin)%matrix, matrix_ks(ispin)%matrix, name="FOCK MATRIX")
CALL dbcsr_release(matrix_ks(ispin)%matrix)
CALL dbcsr_copy(matrix_ks(ispin)%matrix, dbcsr_work(ispin)%matrix)
END DO
CALL dbcsr_deallocate_matrix_set(dbcsr_work)
!In case of ADMM, we want to correct for eigenvalues
IF (dft_control%do_admm) THEN
DO ispin = 1, nspins
CALL admm_correct_for_eigenvalues(ispin, admm_env, fock_matrix(ispin)%matrix)
END DO
END IF
!restore xc and HF fraction
CALL section_vals_set_subs_vals(xc_section, "XC_FUNCTIONAL", xc_fun_original)
CALL section_vals_release(xc_fun_original)
qs_env%x_data(:, :)%general_parameter%fraction = og_hfx_frac
IF (dft_control%do_admm) THEN
IF (ASSOCIATED(admm_env%xc_section_primary)) CALL section_vals_release(admm_env%xc_section_primary)
IF (ASSOCIATED(admm_env%xc_section_aux)) CALL section_vals_release(admm_env%xc_section_aux)
CALL create_admm_xc_section(qs_env%x_data, xc_section, admm_env)
DO i = 1, SIZE(mos)
CALL reassign_allocated_mos(mos(i), backup_mos(i))
CALL deallocate_mo_set(backup_mos(i))
END DO
DEALLOCATE (backup_mos)
END IF
END BLOCK
END IF
!4,5) Build pseudo-canonical MOs if needed + get related Fock matrix elements
ALLOCATE (all_struct(nspins), all_coeffs(nspins))
ALLOCATE (homo_evals(nspins), lumo_evals(nspins))
CALL dbcsr_get_info(matrix_ks(1)%matrix, row_blk_size=nsgf_blk)
ALLOCATE (contract_coeffs_backup(nsgf_blk(exat), nspins*ndo_mo))
DO ispin = 1, nspins
CALL cp_fm_struct_create(hoho_struct, para_env=para_env, context=blacs_env, &
ncol_global=nhomo(ispin), nrow_global=nhomo(ispin))
CALL cp_fm_struct_create(lulu_struct, para_env=para_env, context=blacs_env, &
ncol_global=nlumo(ispin), nrow_global=nlumo(ispin))
CALL cp_fm_create(hoho_work, hoho_struct)
CALL cp_fm_create(lulu_work, lulu_struct)
CALL cp_fm_create(homo_work, homo_struct(ispin)%struct)
CALL cp_fm_create(lumo_work, lumo_struct(ispin)%struct)
IF (pseudo_canonical) THEN
!That is where we rotate the MOs to make them pseudo canonical
!The eigenvalues we get from the diagonalization
!The Fock matrix in the HOMO subspace
CALL cp_fm_create(hoho_fock, hoho_struct)
NULLIFY (homo_evals(ispin)%array)
ALLOCATE (homo_evals(ispin)%array(nhomo(ispin)))
CALL cp_dbcsr_sm_fm_multiply(fock_matrix(ispin)%matrix, homo_coeffs(ispin), &
homo_work, ncol=nhomo(ispin))
CALL parallel_gemm('T', 'N', nhomo(ispin), nhomo(ispin), nao, 1.0_dp, homo_coeffs(ispin), &
homo_work, 0.0_dp, hoho_fock)
!diagonalize and get pseudo-canonical MOs
CALL choose_eigv_solver(hoho_fock, hoho_work, homo_evals(ispin)%array)
CALL parallel_gemm('N', 'N', nao, nhomo(ispin), nhomo(ispin), 1.0_dp, homo_coeffs(ispin), &
hoho_work, 0.0_dp, homo_work)
CALL cp_fm_to_fm(homo_work, homo_coeffs(ispin))
!overwrite the donor_state's contract coeffs with those
contract_coeffs_backup(:, (ispin - 1)*ndo_mo + 1:ispin*ndo_mo) = &
donor_state%contract_coeffs(:, (ispin - 1)*ndo_mo + 1:ispin*ndo_mo)
start_sgf = SUM(nsgf_blk(1:exat - 1)) + 1
CALL cp_fm_get_submatrix(homo_coeffs(ispin), &
donor_state%contract_coeffs(:, (ispin - 1)*ndo_mo + 1:ispin*ndo_mo), &
start_row=start_sgf, start_col=first_domo(ispin), &
n_rows=nsgf_blk(exat), n_cols=ndo_mo)
!do the same for the pseudo-LUMOs
CALL cp_fm_create(lulu_fock, lulu_struct)
NULLIFY (lumo_evals(ispin)%array)
ALLOCATE (lumo_evals(ispin)%array(nlumo(ispin)))
CALL cp_dbcsr_sm_fm_multiply(fock_matrix(ispin)%matrix, lumo_coeffs(ispin), &
lumo_work, ncol=nlumo(ispin))
CALL parallel_gemm('T', 'N', nlumo(ispin), nlumo(ispin), nao, 1.0_dp, lumo_coeffs(ispin), &
lumo_work, 0.0_dp, lulu_fock)
!diagonalize and get pseudo-canonical MOs
CALL choose_eigv_solver(lulu_fock, lulu_work, lumo_evals(ispin)%array)
CALL parallel_gemm('N', 'N', nao, nlumo(ispin), nlumo(ispin), 1.0_dp, lumo_coeffs(ispin), &
lulu_work, 0.0_dp, lumo_work)
CALL cp_fm_to_fm(lumo_work, lumo_coeffs(ispin))
CALL cp_fm_release(lulu_fock)
CALL cp_fm_release(hoho_fock)
ELSE !using the generalized Fock matrix diagonal elements
!Compute their Fock matrix diagonal
ALLOCATE (homo_evals(ispin)%array(nhomo(ispin)))
CALL cp_dbcsr_sm_fm_multiply(fock_matrix(ispin)%matrix, homo_coeffs(ispin), &
homo_work, ncol=nhomo(ispin))
CALL parallel_gemm('T', 'N', nhomo(ispin), nhomo(ispin), nao, 1.0_dp, homo_coeffs(ispin), &
homo_work, 0.0_dp, hoho_work)
CALL cp_fm_get_diag(hoho_work, homo_evals(ispin)%array)
ALLOCATE (lumo_evals(ispin)%array(nlumo(ispin)))
CALL cp_dbcsr_sm_fm_multiply(fock_matrix(ispin)%matrix, lumo_coeffs(ispin), &
lumo_work, ncol=nlumo(ispin))
CALL parallel_gemm('T', 'N', nlumo(ispin), nlumo(ispin), nao, 1.0_dp, lumo_coeffs(ispin), &
lumo_work, 0.0_dp, lulu_work)
CALL cp_fm_get_diag(lulu_work, lumo_evals(ispin)%array)
END IF
CALL cp_fm_release(homo_work)
CALL cp_fm_release(hoho_work)
CALL cp_fm_struct_release(hoho_struct)
CALL cp_fm_release(lumo_work)
CALL cp_fm_release(lulu_work)
CALL cp_fm_struct_release(lulu_struct)
!Put back homo and lumo coeffs together, to fit tensor structure
CALL cp_fm_struct_create(all_struct(ispin)%struct, para_env=para_env, context=blacs_env, &
ncol_global=nhomo(ispin) + nlumo(ispin), nrow_global=nao)
CALL cp_fm_create(all_coeffs(ispin), all_struct(ispin)%struct)
CALL cp_fm_to_fm(homo_coeffs(ispin), all_coeffs(ispin), ncol=nhomo(ispin), &
source_start=1, target_start=1)
CALL cp_fm_to_fm(lumo_coeffs(ispin), all_coeffs(ispin), ncol=nlumo(ispin), &
source_start=1, target_start=nhomo(ispin) + 1)
END DO !ispin
!get semi-contracted tensor (AOs to MOs, keep RI uncontracted)
CALL contract_AOs_to_MOs(ja_X, oI_Y, mo_template, all_coeffs, nhomo, nlumo, &
donor_state, xas_tdp_env, xas_tdp_control, qs_env)
!intermediate clean-up
DO ispin = 1, nspins
CALL cp_fm_release(all_coeffs(ispin))
CALL cp_fm_release(homo_coeffs(ispin))
CALL cp_fm_release(lumo_coeffs(ispin))
CALL cp_fm_struct_release(all_struct(ispin)%struct)
CALL cp_fm_struct_release(lumo_struct(ispin)%struct)
CALL cp_fm_struct_release(homo_struct(ispin)%struct)
END DO
!6) GW2X iterations
IF (nspins == 1) THEN
!restricted-closed shell: only alpha spin
CALL GW2X_rcs_iterations(first_domo(1), ja_X(1), oI_Y, mo_template(1, 1), homo_evals(1)%array, &
lumo_evals(1)%array, donor_state, xas_tdp_control, qs_env)
ELSE
!open-shell, need both spins
CALL GW2X_os_iterations(first_domo, ja_X, oI_Y, mo_template, homo_evals, lumo_evals, &
donor_state, xas_tdp_control, qs_env)
END IF
!restore proper contract_coeffs
IF (pseudo_canonical) THEN
donor_state%contract_coeffs(:, :) = contract_coeffs_backup(:, :)
END IF
!Final clean-up
DO ido_mo = 1, nspins*ndo_mo
CALL dbt_destroy(oI_Y(ido_mo))
END DO
DO ispin = 1, nspins
CALL dbt_destroy(ja_X(ispin))
DEALLOCATE (homo_evals(ispin)%array)
DEALLOCATE (lumo_evals(ispin)%array)
DO jspin = 1, nspins
CALL dbt_destroy(mo_template(ispin, jspin))
END DO
END DO
DEALLOCATE (oI_Y, homo_evals, lumo_evals)
CALL timestop(handle)
END SUBROUTINE GW2X_shift
! **************************************************************************************************
!> \brief Preforms the GW2X iterations in the restricted-closed shell formalism according to the
!> Newton-Raphson method
!> \param first_domo index of the first core donor MO to consider
!> \param ja_X semi-contracted tensor with j: occupied MO, a: virtual MO, X: RI basis element
!> \param oI_Y semi-contracted tensors with o: all MOs, I donor core MO, Y: RI basis element
!> \param mo_template tensor template for fully MO contracted tensor
!> \param homo_evals ...
!> \param lumo_evals ...
!> \param donor_state ...
!> \param xas_tdp_control ...
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE GW2X_rcs_iterations(first_domo, ja_X, oI_Y, mo_template, homo_evals, lumo_evals, &
donor_state, xas_tdp_control, qs_env)
INTEGER, INTENT(IN) :: first_domo
TYPE(dbt_type), INTENT(inout) :: ja_X
TYPE(dbt_type), DIMENSION(:), INTENT(inout) :: oI_Y
TYPE(dbt_type), INTENT(inout) :: mo_template
REAL(dp), DIMENSION(:), INTENT(IN) :: homo_evals, lumo_evals
TYPE(donor_state_type), POINTER :: donor_state
TYPE(xas_tdp_control_type), POINTER :: xas_tdp_control
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'GW2X_rcs_iterations'
INTEGER :: batch_size, bounds_1d(2), bounds_2d(2, 2), handle, i, ibatch, ido_mo, iloop, &
max_iter, nbatch_occ, nbatch_virt, nblk_occ, nblk_virt, nblks(3), ndo_mo, nhomo, nlumo, &
occ_bo(2), output_unit, tmp_sum, virt_bo(2)
INTEGER, ALLOCATABLE, DIMENSION(:) :: mo_blk_size
REAL(dp) :: c_os, c_ss, dg, diff, ds1, ds2, eps_I, &
eps_iter, g, omega_k, parts(4), s1, s2
TYPE(dbt_type) :: aj_Ib, aj_Ib_diff, aj_X, ja_Ik, &
ja_Ik_diff
TYPE(mp_para_env_type), POINTER :: para_env
CALL timeset(routineN, handle)
eps_iter = xas_tdp_control%gw2x_eps
max_iter = xas_tdp_control%max_gw2x_iter
c_os = xas_tdp_control%c_os
c_ss = xas_tdp_control%c_ss
batch_size = xas_tdp_control%batch_size
ndo_mo = donor_state%ndo_mo
output_unit = cp_logger_get_default_io_unit()
nhomo = SIZE(homo_evals)
nlumo = SIZE(lumo_evals)
CALL get_qs_env(qs_env, para_env=para_env)
!We use the Newton-Raphson method to find the zero of the function:
!g(omega) = eps_I - omega + mp2 terms, dg(omega) = -1 + d/d_omega (mp2 terms)
!We simply compute at each iteration: omega_k+1 = omega_k - g(omega_k)/dg(omega_k)
!need transposed tensor of (ja|X) for optimal contraction scheme (s.t. (aj|X) block is on same
!processor as (ja|X))
CALL dbt_create(ja_X, aj_X)
CALL dbt_copy(ja_X, aj_X, order=[2, 1, 3])
!split the MO blocks into batches for memory friendly batched contraction
!huge dense tensors never need to be stored
CALL dbt_get_info(ja_X, nblks_total=nblks)
ALLOCATE (mo_blk_size(nblks(1)))
CALL dbt_get_info(ja_X, blk_size_1=mo_blk_size)
tmp_sum = 0
DO i = 1, nblks(1)
tmp_sum = tmp_sum + mo_blk_size(i)
IF (tmp_sum == nhomo) THEN
nblk_occ = i
nblk_virt = nblks(1) - i
EXIT
END IF
END DO
nbatch_occ = MAX(1, nblk_occ/batch_size)
nbatch_virt = MAX(1, nblk_virt/batch_size)
!Loop over donor_states
DO ido_mo = 1, ndo_mo
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, FMT="(/,T5,A,I2,A,I4,A,/,T5,A)") &
"- GW2X correction for donor MO with spin ", 1, &
" and MO index ", donor_state%mo_indices(ido_mo, 1), ":", &
" iteration convergence (eV)"
CALL m_flush(output_unit)
END IF
!starting values
eps_I = homo_evals(first_domo + ido_mo - 1)
omega_k = eps_I
iloop = 0
diff = 2.0_dp*eps_iter
DO WHILE (ABS(diff) > eps_iter)
iloop = iloop + 1
!Compute the mp2 terms and their first derivative
parts = 0.0_dp
!We do batched contraction for (ja|Ik) and (ja|Ib) to never have to carry the full tensor
DO ibatch = 1, nbatch_occ
occ_bo = get_limit(nblk_occ, nbatch_occ, ibatch - 1)
bounds_1d = [SUM(mo_blk_size(1:occ_bo(1) - 1)) + 1, SUM(mo_blk_size(1:occ_bo(2)))]
CALL dbt_create(mo_template, ja_Ik)
CALL dbt_contract(alpha=1.0_dp, tensor_1=ja_X, tensor_2=oI_Y(ido_mo), &
beta=0.0_dp, tensor_3=ja_Ik, contract_1=[3], &
notcontract_1=[1, 2], contract_2=[2], notcontract_2=[1], &
map_1=[1, 2], map_2=[3], bounds_3=bounds_1d)
!opposite-spin contribution
CALL calc_os_oov_contrib(parts(1), parts(2), ja_Ik, homo_evals, lumo_evals, homo_evals, &
omega_k, c_os, nhomo)
bounds_2d(:, 2) = bounds_1d
bounds_2d(1, 1) = nhomo + 1
bounds_2d(2, 1) = nhomo + nlumo
!same-spin contribution. Contraction only neede if c_ss != 0
!directly compute the difference (ja|Ik) - (ka|Ij)
IF (ABS(c_ss) > EPSILON(1.0_dp)) THEN
CALL dbt_create(ja_Ik, ja_Ik_diff, map1_2d=[1], map2_2d=[2, 3])
CALL dbt_copy(ja_Ik, ja_Ik_diff, move_data=.TRUE.)
CALL dbt_contract(alpha=-1.0_dp, tensor_1=oI_Y(ido_mo), tensor_2=aj_X, &
beta=1.0_dp, tensor_3=ja_Ik_diff, contract_1=[2], &
notcontract_1=[1], contract_2=[3], notcontract_2=[1, 2], &
map_1=[1], map_2=[2, 3], bounds_2=[1, nhomo], bounds_3=bounds_2d)
CALL calc_ss_oov_contrib(parts(1), parts(2), ja_Ik_diff, homo_evals, lumo_evals, omega_k, c_ss)
CALL dbt_destroy(ja_Ik_diff)
END IF !c_ss != 0
CALL dbt_destroy(ja_Ik)
END DO
DO ibatch = 1, nbatch_virt
virt_bo = get_limit(nblk_virt, nbatch_virt, ibatch - 1)
bounds_1d = [SUM(mo_blk_size(1:nblk_occ + virt_bo(1) - 1)) + 1, &
SUM(mo_blk_size(1:nblk_occ + virt_bo(2)))]
CALL dbt_create(mo_template, aj_Ib)
CALL dbt_contract(alpha=1.0_dp, tensor_1=aj_X, tensor_2=oI_Y(ido_mo), &
beta=0.0_dp, tensor_3=aj_Ib, contract_1=[3], &
notcontract_1=[1, 2], contract_2=[2], notcontract_2=[1], &
map_1=[1, 2], map_2=[3], bounds_3=bounds_1d)
!opposite-spin contribution
CALL calc_os_ovv_contrib(parts(3), parts(4), aj_Ib, lumo_evals, homo_evals, lumo_evals, &
omega_k, c_os, nhomo, nhomo)
!same-spin contribution, only if c_ss is not 0
!directly compute the difference (aj|Ib) - (bj|Ia)
IF (ABS(c_ss) > EPSILON(1.0_dp)) THEN
bounds_2d(1, 1) = 1
bounds_2d(2, 1) = nhomo
bounds_2d(:, 2) = bounds_1d
CALL dbt_create(aj_Ib, aj_Ib_diff, map1_2d=[1], map2_2d=[2, 3])
CALL dbt_copy(aj_Ib, aj_Ib_diff, move_data=.TRUE.)
CALL dbt_contract(alpha=-1.0_dp, tensor_1=oI_Y(ido_mo), tensor_2=ja_X, &
beta=1.0_dp, tensor_3=aj_Ib_diff, contract_1=[2], &
notcontract_1=[1], contract_2=[3], notcontract_2=[1, 2], &
map_1=[1], map_2=[2, 3], &
bounds_2=[nhomo + 1, nhomo + nlumo], bounds_3=bounds_2d)
CALL calc_ss_ovv_contrib(parts(3), parts(4), aj_Ib_diff, homo_evals, lumo_evals, omega_k, c_ss)
CALL dbt_destroy(aj_Ib_diff)
END IF ! c_ss not 0
CALL dbt_destroy(aj_Ib)
END DO
CALL para_env%sum(parts)
s1 = parts(1); ds1 = parts(2)
s2 = parts(3); ds2 = parts(4)
!evaluate g and its derivative
g = eps_I - omega_k + s1 + s2
dg = -1.0_dp + ds1 + ds2
!compute the diff to the new step
diff = -g/dg
!and the new omega
omega_k = omega_k + diff
diff = diff*evolt
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, FMT="(T21,I18,F32.6)") &
iloop, diff
CALL m_flush(output_unit)
END IF
IF (iloop > max_iter) THEN
CPWARN("GW2X iteration not converged.")
EXIT
END IF
END DO !while loop on eps_iter
!compute the shift and update donor_state
donor_state%gw2x_evals(ido_mo, 1) = omega_k
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, FMT="(/T7,A,F11.6,/,T5,A,F11.6)") &
"Final GW2X shift for this donor MO (eV):", &
(donor_state%energy_evals(ido_mo, 1) - omega_k)*evolt
END IF
END DO !ido_mo
CALL dbt_destroy(aj_X)
CALL timestop(handle)
END SUBROUTINE GW2X_rcs_iterations
! **************************************************************************************************
!> \brief Preforms the GW2X iterations in the open-shell shell formalism according to the
!> Newton-Raphson method
!> \param first_domo index of the first core donor MO to consider, for each spin
!> \param ja_X semi-contracted tensors with j: occupied MO, a: virtual MO, X: RI basis element
!> \param oI_Y semi-contracted tensors with o: all MOs, I donor core MO, Y: RI basis element
!> \param mo_template tensor template for fully MO contracted tensor, for each spin combination
!> \param homo_evals ...
!> \param lumo_evals ...
!> \param donor_state ...
!> \param xas_tdp_control ...
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE GW2X_os_iterations(first_domo, ja_X, oI_Y, mo_template, homo_evals, lumo_evals, &
donor_state, xas_tdp_control, qs_env)
INTEGER, INTENT(IN) :: first_domo(2)
TYPE(dbt_type), DIMENSION(:), INTENT(inout) :: ja_X, oI_Y
TYPE(dbt_type), DIMENSION(:, :), INTENT(inout) :: mo_template
TYPE(cp_1d_r_p_type), DIMENSION(:), INTENT(in) :: homo_evals, lumo_evals
TYPE(donor_state_type), POINTER :: donor_state
TYPE(xas_tdp_control_type), POINTER :: xas_tdp_control
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'GW2X_os_iterations'
INTEGER :: batch_size, bounds_1d(2), bounds_2d(2, 2), handle, i, ibatch, ido_mo, iloop, &
ispin, max_iter, nbatch_occ, nbatch_virt, nblk_occ, nblk_virt, nblks(3), ndo_mo, &
nhomo(2), nlumo(2), nspins, occ_bo(2), other_spin, output_unit, tmp_sum, virt_bo(2)
INTEGER, ALLOCATABLE, DIMENSION(:) :: mo_blk_size
REAL(dp) :: c_os, c_ss, dg, diff, ds1, ds2, eps_I, &
eps_iter, g, omega_k, parts(4), s1, s2
TYPE(dbt_type) :: aj_Ib, aj_Ib_diff, ja_Ik, ja_Ik_diff
TYPE(dbt_type), ALLOCATABLE, DIMENSION(:) :: aj_X
TYPE(mp_para_env_type), POINTER :: para_env
CALL timeset(routineN, handle)
eps_iter = xas_tdp_control%gw2x_eps
max_iter = xas_tdp_control%max_gw2x_iter
c_os = xas_tdp_control%c_os
c_ss = xas_tdp_control%c_ss
batch_size = xas_tdp_control%batch_size
nspins = 2
ndo_mo = donor_state%ndo_mo
output_unit = cp_logger_get_default_io_unit()
DO ispin = 1, nspins
nhomo(ispin) = SIZE(homo_evals(ispin)%array)
nlumo(ispin) = SIZE(lumo_evals(ispin)%array)
END DO
CALL get_qs_env(qs_env, para_env=para_env)
!We use the Newton-Raphson method to find the zero of the function:
!g(omega) = eps_I - omega + mp2 terms, dg(omega) = -1 + d/d_omega (mp2 terms)
!We simply compute at each iteration: omega_k+1 = omega_k - g(omega_k)/dg(omega_k)
ALLOCATE (aj_X(2))
DO ispin = 1, nspins
!need transposed tensor of (ja|X) for optimal contraction scheme,
!s.t. (aj|X) block is on same processor as (ja|X)) and differences can be taken
CALL dbt_create(ja_X(ispin), aj_X(ispin))
CALL dbt_copy(ja_X(ispin), aj_X(ispin), order=[2, 1, 3])
END DO ! ispin
DO ispin = 1, nspins
other_spin = 3 - ispin
!split the MO blocks into batches for memory friendly batched contraction
!huge dense tensors never need to be stored. Split MOs for the current spin
CALL dbt_get_info(ja_X(ispin), nblks_total=nblks)
ALLOCATE (mo_blk_size(nblks(1)))
CALL dbt_get_info(ja_X(ispin), blk_size_1=mo_blk_size)
tmp_sum = 0
DO i = 1, nblks(1)
tmp_sum = tmp_sum + mo_blk_size(i)
IF (tmp_sum == nhomo(ispin)) THEN
nblk_occ = i
nblk_virt = nblks(1) - i
EXIT
END IF
END DO
nbatch_occ = MAX(1, nblk_occ/batch_size)
nbatch_virt = MAX(1, nblk_virt/batch_size)
!Loop over donor_states of the current spin
DO ido_mo = 1, ndo_mo
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, FMT="(/,T5,A,I2,A,I4,A,/,T5,A)") &
"- GW2X correction for donor MO with spin ", ispin, &
" and MO index ", donor_state%mo_indices(ido_mo, ispin), ":", &
" iteration convergence (eV)"
CALL m_flush(output_unit)
END IF
!starting values
eps_I = homo_evals(ispin)%array(first_domo(ispin) + ido_mo - 1)
omega_k = eps_I
iloop = 0
diff = 2.0_dp*eps_iter
DO WHILE (ABS(diff) > eps_iter)
iloop = iloop + 1
!Compute the mp2 terms and their first derivative
parts = 0.0_dp
!We do batched contraction for (ja|Ik) and (ja|Ib) to never have to carry the full tensor
DO ibatch = 1, nbatch_occ
!opposite-spin contribution, i.e. (j_beta a_beta| I_alpha k_alpha) and vice-versa
!do the batching along k because same spin as donor MO
occ_bo = get_limit(nblk_occ, nbatch_occ, ibatch - 1)
bounds_1d = [SUM(mo_blk_size(1:occ_bo(1) - 1)) + 1, SUM(mo_blk_size(1:occ_bo(2)))]
CALL dbt_create(mo_template(other_spin, ispin), ja_Ik)
CALL dbt_contract(alpha=1.0_dp, tensor_1=ja_X(other_spin), &
tensor_2=oI_Y((ispin - 1)*ndo_mo + ido_mo), &
beta=0.0_dp, tensor_3=ja_Ik, contract_1=[3], &
notcontract_1=[1, 2], contract_2=[2], notcontract_2=[1], &
map_1=[1, 2], map_2=[3], bounds_3=bounds_1d)
CALL calc_os_oov_contrib(parts(1), parts(2), ja_Ik, homo_evals(other_spin)%array, &
lumo_evals(other_spin)%array, homo_evals(ispin)%array, &
omega_k, c_os, nhomo(other_spin))
CALL dbt_destroy(ja_Ik)
!same-spin contribution, need to compute (ja|Ik) - (ka|Ij), all with the current spin
!skip if c_ss == 0
IF (ABS(c_ss) > EPSILON(1.0_dp)) THEN
!same batching as opposite spin
CALL dbt_create(mo_template(ispin, ispin), ja_Ik)
CALL dbt_contract(alpha=1.0_dp, tensor_1=ja_X(ispin), &
tensor_2=oI_Y((ispin - 1)*ndo_mo + ido_mo), &
beta=0.0_dp, tensor_3=ja_Ik, contract_1=[3], &
notcontract_1=[1, 2], contract_2=[2], notcontract_2=[1], &
map_1=[1, 2], map_2=[3], bounds_3=bounds_1d)
bounds_2d(:, 2) = bounds_1d
bounds_2d(1, 1) = nhomo(ispin) + 1
bounds_2d(2, 1) = nhomo(ispin) + nlumo(ispin)
!the tensor difference is directly taken here
CALL dbt_create(ja_Ik, ja_Ik_diff, map1_2d=[1], map2_2d=[2, 3])
CALL dbt_copy(ja_Ik, ja_Ik_diff, move_data=.TRUE.)
CALL dbt_contract(alpha=-1.0_dp, tensor_1=oI_Y((ispin - 1)*ndo_mo + ido_mo), &
tensor_2=aj_X(ispin), beta=1.0_dp, tensor_3=ja_Ik_diff, &
contract_1=[2], notcontract_1=[1], contract_2=[3], notcontract_2=[1, 2], &
map_1=[1], map_2=[2, 3], bounds_2=[1, nhomo(ispin)], bounds_3=bounds_2d)
CALL calc_ss_oov_contrib(parts(1), parts(2), ja_Ik_diff, homo_evals(ispin)%array, &
lumo_evals(ispin)%array, omega_k, c_ss)
CALL dbt_destroy(ja_Ik_diff)
CALL dbt_destroy(ja_Ik)
END IF !c_ss !!= 0
END DO
DO ibatch = 1, nbatch_virt
!opposite-spin contribution, i.e. (a_beta j_beta| I_alpha b_alpha) and vice-versa
!do the batching along b because same spin as donor MO
virt_bo = get_limit(nblk_virt, nbatch_virt, ibatch - 1)
bounds_1d = [SUM(mo_blk_size(1:nblk_occ + virt_bo(1) - 1)) + 1, &
SUM(mo_blk_size(1:nblk_occ + virt_bo(2)))]
CALL dbt_create(mo_template(other_spin, ispin), aj_Ib)
CALL dbt_contract(alpha=1.0_dp, tensor_1=aj_X(other_spin), &
tensor_2=oI_Y((ispin - 1)*ndo_mo + ido_mo), &
beta=0.0_dp, tensor_3=aj_Ib, contract_1=[3], &
notcontract_1=[1, 2], contract_2=[2], notcontract_2=[1], &
map_1=[1, 2], map_2=[3], bounds_3=bounds_1d)
CALL calc_os_ovv_contrib(parts(3), parts(4), aj_Ib, lumo_evals(other_spin)%array, &
homo_evals(other_spin)%array, lumo_evals(ispin)%array, &
omega_k, c_os, nhomo(other_spin), nhomo(ispin))
CALL dbt_destroy(aj_Ib)
!same-spin contribution, need to compute (aj|Ib) - (bj|Ia), all with the current spin
!skip if c_ss == 0
IF (ABS(c_ss) > EPSILON(1.0_dp)) THEN
!same batching as opposite spin
CALL dbt_create(mo_template(ispin, ispin), aj_Ib)
CALL dbt_contract(alpha=1.0_dp, tensor_1=aj_X(ispin), &
tensor_2=oI_Y((ispin - 1)*ndo_mo + ido_mo), &
beta=0.0_dp, tensor_3=aj_Ib, contract_1=[3], &
notcontract_1=[1, 2], contract_2=[2], notcontract_2=[1], &
map_1=[1, 2], map_2=[3], bounds_3=bounds_1d)
bounds_2d(1, 1) = 1
bounds_2d(2, 1) = nhomo(ispin)
bounds_2d(:, 2) = bounds_1d
CALL dbt_create(aj_Ib, aj_Ib_diff, map1_2d=[1], map2_2d=[2, 3])
CALL dbt_copy(aj_Ib, aj_Ib_diff, move_data=.TRUE.)
CALL dbt_contract(alpha=-1.0_dp, tensor_1=oI_Y((ispin - 1)*ndo_mo + ido_mo), &
tensor_2=ja_X(ispin), beta=1.0_dp, tensor_3=aj_Ib_diff, &
contract_1=[2], notcontract_1=[1], contract_2=[3], &
notcontract_2=[1, 2], map_1=[1], map_2=[2, 3], &
bounds_2=[nhomo(ispin) + 1, nhomo(ispin) + nlumo(ispin)], &
bounds_3=bounds_2d)
CALL calc_ss_ovv_contrib(parts(3), parts(4), aj_Ib_diff, homo_evals(ispin)%array, &
lumo_evals(ispin)%array, omega_k, c_ss)
CALL dbt_destroy(aj_Ib_diff)
CALL dbt_destroy(aj_Ib)
END IF ! c_ss not 0
END DO
CALL para_env%sum(parts)
s1 = parts(1); ds1 = parts(2)
s2 = parts(3); ds2 = parts(4)
!evaluate g and its derivative
g = eps_I - omega_k + s1 + s2
dg = -1.0_dp + ds1 + ds2
!compute the diff to the new step
diff = -g/dg
!and the new omega
omega_k = omega_k + diff
diff = diff*evolt
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, FMT="(T21,I18,F32.6)") &
iloop, diff
CALL m_flush(output_unit)
END IF
IF (iloop > max_iter) THEN
CPWARN("GW2X iteration not converged.")
EXIT
END IF
END DO !while loop on eps_iter
!compute the shift and update donor_state
donor_state%gw2x_evals(ido_mo, ispin) = omega_k
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, FMT="(/T7,A,F11.6,/,T5,A,F11.6)") &
"Final GW2X shift for this donor MO (eV):", &
(donor_state%energy_evals(ido_mo, ispin) - omega_k)*evolt
END IF
END DO !ido_mo
DEALLOCATE (mo_blk_size)
END DO ! ispin
DO ispin = 1, nspins
CALL dbt_destroy(aj_X(ispin))
END DO
CALL timestop(handle)
END SUBROUTINE GW2X_os_iterations
! **************************************************************************************************
!> \brief Takes the 3-center integrals from the ri_ex_3c tensor and returns a full tensor. Since
!> ri_ex_3c is only half filled because of symmetry, we have to add the transpose
!> and scale the diagonal blocks by 0.5
!> \param pq_X the full (desymmetrized) tensor containing the (pq|X) exchange integrals, in a new
!> 3d distribution and optimized block sizes
!> \param exat index of current excited atom
!> \param xas_tdp_env ...
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE get_full_pqX_from_3c_ex(pq_X, exat, xas_tdp_env, qs_env)
TYPE(dbt_type), INTENT(INOUT) :: pq_X
INTEGER, INTENT(IN) :: exat
TYPE(xas_tdp_env_type), POINTER :: xas_tdp_env
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER :: i, ind(3), natom, nblk_ri, nsgf_x
INTEGER, ALLOCATABLE, DIMENSION(:) :: orb_blk_size, proc_dist_1, proc_dist_2, &
proc_dist_3
INTEGER, DIMENSION(3) :: pdims
LOGICAL :: found
REAL(dp), ALLOCATABLE, DIMENSION(:, :, :) :: pblock
TYPE(dbt_distribution_type) :: t_dist
TYPE(dbt_iterator_type) :: iter
TYPE(dbt_pgrid_type) :: t_pgrid
TYPE(dbt_type) :: pq_X_tmp, work
TYPE(mp_para_env_type), POINTER :: para_env
NULLIFY (para_env)
!create work tensor with same 2D dist as pq_X, but only keep excited atom along RI direction
CALL get_qs_env(qs_env, para_env=para_env, natom=natom)
CALL dbt_get_info(xas_tdp_env%ri_3c_ex, pdims=pdims)
nsgf_x = SIZE(xas_tdp_env%ri_inv_ex, 1)
nblk_ri = 1
CALL dbt_pgrid_create(para_env, pdims, t_pgrid)
ALLOCATE (proc_dist_1(natom), proc_dist_2(natom), orb_blk_size(natom))
CALL dbt_get_info(xas_tdp_env%ri_3c_ex, proc_dist_1=proc_dist_1, proc_dist_2=proc_dist_2, &
blk_size_1=orb_blk_size)
CALL dbt_distribution_new(t_dist, t_pgrid, nd_dist_1=proc_dist_1, nd_dist_2=proc_dist_2, &
nd_dist_3=[(0, i=1, nblk_ri)])
CALL dbt_create(work, name="(pq|X)", dist=t_dist, map1_2d=[1], map2_2d=[2, 3], &
blk_size_1=orb_blk_size, blk_size_2=orb_blk_size, blk_size_3=[nsgf_x])
CALL dbt_distribution_destroy(t_dist)
!dist of 3c_ex and work match, can simply copy blocks over. Diagonal with factor 0.5
!$OMP PARALLEL DEFAULT(NONE) SHARED(xas_tdp_env,exat,work,orb_blk_size,nsgf_x) &
!$OMP PRIVATE(iter,ind,pblock,found)
CALL dbt_iterator_start(iter, xas_tdp_env%ri_3c_ex)
DO WHILE (dbt_iterator_blocks_left(iter))
CALL dbt_iterator_next_block(iter, ind)
CALL dbt_get_block(xas_tdp_env%ri_3c_ex, ind, pblock, found)
IF (ind(1) == ind(2)) pblock = 0.5_dp*pblock
IF (ind(3) /= exat) CYCLE
CALL dbt_put_block(work, [ind(1), ind(2), 1], &
[orb_blk_size(ind(1)), orb_blk_size(ind(2)), nsgf_x], pblock)
DEALLOCATE (pblock)