Repository navigation
Expand file tree
/
Copy pathbse_util.F
More file actions
1395 lines (1201 loc) · 68.3 KB
/
Copy pathbse_util.F
File metadata and controls
1395 lines (1201 loc) · 68.3 KB
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-2026 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Auxiliary routines for GW + Bethe-Salpeter for computing electronic excitations
!> \par History
!> 11.2023 created [Maximilian Graml]
! **************************************************************************************************
MODULE bse_util
USE atomic_kind_types, ONLY: atomic_kind_type
USE bse_types, ONLY: bse_env_type
USE cell_types, ONLY: cell_type
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_control_types, ONLY: dft_control_type
USE cp_dbcsr_api, ONLY: dbcsr_p_type
USE cp_dbcsr_operations, ONLY: cp_dbcsr_sm_fm_multiply
USE cp_fm_basic_linalg, ONLY: cp_fm_trace
USE cp_fm_redistribution, ONLY: cp_fm_permute
USE cp_fm_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_create,&
cp_fm_get_info,&
cp_fm_release,&
cp_fm_set_all,&
cp_fm_to_fm_submat,&
cp_fm_to_fm_submat_general,&
cp_fm_type
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_type
USE cp_output_handling, ONLY: cp_print_key_finished_output,&
cp_print_key_unit_nr
USE cp_realspace_grid_cube, ONLY: cp_pw_to_cube
USE input_constants, ONLY: bse_iterdiag,&
bse_screening_alpha,&
bse_screening_rpa,&
bse_screening_tdhf
USE input_section_types, ONLY: section_vals_type,&
section_vals_val_get
USE kinds, ONLY: default_path_length,&
dp,&
int_8
USE message_passing, ONLY: mp_para_env_type
USE mo_window, ONLY: combine_mo_windows,&
determine_mo_window,&
mo_window_type
USE parallel_gemm_api, ONLY: parallel_gemm
USE particle_list_types, ONLY: particle_list_type
USE particle_types, ONLY: particle_type
USE physcon, ONLY: evolt
USE pw_env_types, ONLY: pw_env_get,&
pw_env_type
USE pw_pool_types, ONLY: pw_pool_p_type,&
pw_pool_type
USE pw_types, ONLY: pw_c1d_gs_type,&
pw_r3d_rs_type
USE qs_collocate_density, ONLY: calculate_wavefunction
USE qs_kind_types, ONLY: qs_kind_type
USE qs_mo_types, ONLY: get_mo_set,&
mo_set_type
USE qs_subsys_types, ONLY: qs_subsys_get,&
qs_subsys_type
USE util, ONLY: sort,&
sort_unique
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'bse_util'
PUBLIC :: screen_slabs, bse_window_from_dft, bse_window_from_mos, &
comp_eigvec_coeff_BSE, sort_excitations, &
estimate_BSE_resources, fulldiag_memory_estimate_GB, filter_eigvec_contrib, truncate_BSE_matrices, &
adapt_BSE_input_params, adapt_BSE_screening, get_multipoles_mo, &
reshuffle_eigvec, print_bse_nto_cubes, trace_exciton_descr, &
get_bse_spin_block_layout, assemble_joint_ov_slab, &
ia_of_occ_virt, occ_of_ia, virt_of_ia
CONTAINS
! **************************************************************************************************
!> \brief Compound transition index ia = (i-1)*virt + a of the pair (i, a): a is the fast index
!> \param i_occ occupied level
!> \param a_virt virtual level
!> \param virt number of virtual levels
!> \return ia
! **************************************************************************************************
PURE FUNCTION ia_of_occ_virt(i_occ, a_virt, virt) RESULT(ia)
INTEGER, INTENT(IN) :: i_occ, a_virt, virt
INTEGER :: ia
ia = (i_occ - 1)*virt + a_virt
END FUNCTION ia_of_occ_virt
! **************************************************************************************************
!> \brief Occupied level of the compound transition index ia = (i-1)*virt + a
!> \param ia compound transition index
!> \param virt number of virtual levels
!> \return i
! **************************************************************************************************
PURE FUNCTION occ_of_ia(ia, virt) RESULT(i_occ)
INTEGER, INTENT(IN) :: ia, virt
INTEGER :: i_occ
i_occ = (ia - 1)/virt + 1
END FUNCTION occ_of_ia
! **************************************************************************************************
!> \brief Virtual level of the compound transition index ia = (i-1)*virt + a
!> \param ia compound transition index
!> \param virt number of virtual levels
!> \return a
! **************************************************************************************************
PURE FUNCTION virt_of_ia(ia, virt) RESULT(a_virt)
INTEGER, INTENT(IN) :: ia, virt
INTEGER :: a_virt
a_virt = MOD(ia - 1, virt) + 1
END FUNCTION virt_of_ia
! **************************************************************************************************
!> \brief The screened slabs \bar{B}^P_ij = sum_Q [1+Q(0)]^-1_PQ B^Q_ij and
!> \bar{B}^P_ia = sum_Q [1+Q(0)]^-1_PQ B^Q_ia, P, Q ≤ dimen_RI_red
!> \param fm_eps_inv [1+Q(0)]^-1_PQ
!> \param fm_S_ij B^P_ij of the window, dimen_RI x homo^2
!> \param fm_S_ia B^P_ia of the window, dimen_RI x homo*virtual
!> \param dimen_RI_red reduced RI dimension, the contraction length
!> \param homo occupied levels in the window
!> \param virtual virtual levels in the window
!> \param fm_S_bar_ij \bar{B}^P_ij, created here on fm_S_ij's structure
!> \param fm_S_bar_ia \bar{B}^P_ia, created here on fm_S_ia's structure for ABBA only
!> \param do_abba whether the coupling matrix B needs the screened ia slab
! **************************************************************************************************
SUBROUTINE screen_slabs(fm_eps_inv, fm_S_ij, fm_S_ia, dimen_RI_red, homo, virtual, &
fm_S_bar_ij, fm_S_bar_ia, do_abba)
TYPE(cp_fm_type), INTENT(IN) :: fm_eps_inv, fm_S_ij, fm_S_ia
INTEGER, INTENT(IN) :: dimen_RI_red, homo, virtual
TYPE(cp_fm_type), INTENT(OUT) :: fm_S_bar_ij, fm_S_bar_ia
LOGICAL, INTENT(IN) :: do_abba
CHARACTER(LEN=*), PARAMETER :: routineN = 'screen_slabs'
INTEGER :: handle
CALL timeset(routineN, handle)
CALL cp_fm_create(fm_S_bar_ij, fm_S_ij%matrix_struct)
CALL cp_fm_set_all(fm_S_bar_ij, 0.0_dp)
! \bar{B}^P_ij = sum_Q [1+Q(0)]^-1_PQ B^Q_ij
CALL parallel_gemm(transa="N", transb="N", m=dimen_RI_red, n=homo**2, k=dimen_RI_red, alpha=1.0_dp, &
matrix_a=fm_eps_inv, matrix_b=fm_S_ij, beta=0.0_dp, matrix_c=fm_S_bar_ij)
IF (do_abba) THEN
CALL cp_fm_create(fm_S_bar_ia, fm_S_ia%matrix_struct)
CALL cp_fm_set_all(fm_S_bar_ia, 0.0_dp)
! \bar{B}^P_ia = sum_Q [1+Q(0)]^-1_PQ B^Q_ia; fm_S_ia comes from RPA and has a different
! blacs_env than fm_S_ij
CALL parallel_gemm(transa="N", transb="N", m=dimen_RI_red, n=homo*virtual, k=dimen_RI_red, alpha=1.0_dp, &
matrix_a=fm_eps_inv, matrix_b=fm_S_ia, beta=0.0_dp, matrix_c=fm_S_bar_ia)
END IF
CALL timestop(handle)
END SUBROUTINE screen_slabs
! **************************************************************************************************
!> \brief Routine for computing the coefficients of the eigenvectors of the BSE matrix from a
!> multiplication with the eigenvalues
!> \param fm_work ...
!> \param eig_vals ...
!> \param beta ...
!> \param gamma ...
!> \param do_transpose ...
! **************************************************************************************************
SUBROUTINE comp_eigvec_coeff_BSE(fm_work, eig_vals, beta, gamma, do_transpose)
TYPE(cp_fm_type), INTENT(INOUT) :: fm_work
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), &
INTENT(IN) :: eig_vals
REAL(KIND=dp), INTENT(IN) :: beta
REAL(KIND=dp), INTENT(IN), OPTIONAL :: gamma
LOGICAL, INTENT(IN), OPTIONAL :: do_transpose
CHARACTER(LEN=*), PARAMETER :: routineN = 'comp_eigvec_coeff_BSE'
INTEGER :: handle, i_row_global, ii, j_col_global, &
jj, ncol_local, nrow_local
INTEGER, DIMENSION(:), POINTER :: col_indices, row_indices
LOGICAL :: my_do_transpose
REAL(KIND=dp) :: coeff, my_gamma
CALL timeset(routineN, handle)
IF (PRESENT(gamma)) THEN
my_gamma = gamma
ELSE
my_gamma = 2.0_dp
END IF
IF (PRESENT(do_transpose)) THEN
my_do_transpose = do_transpose
ELSE
my_do_transpose = .FALSE.
END IF
CALL cp_fm_get_info(matrix=fm_work, &
nrow_local=nrow_local, &
ncol_local=ncol_local, &
row_indices=row_indices, &
col_indices=col_indices)
IF (my_do_transpose) THEN
DO jj = 1, ncol_local
j_col_global = col_indices(jj)
DO ii = 1, nrow_local
coeff = (eig_vals(j_col_global)**beta)/my_gamma
fm_work%local_data(ii, jj) = fm_work%local_data(ii, jj)*coeff
END DO
END DO
ELSE
DO jj = 1, ncol_local
DO ii = 1, nrow_local
i_row_global = row_indices(ii)
coeff = (eig_vals(i_row_global)**beta)/my_gamma
fm_work%local_data(ii, jj) = fm_work%local_data(ii, jj)*coeff
END DO
END DO
END IF
CALL timestop(handle)
END SUBROUTINE comp_eigvec_coeff_BSE
! **************************************************************************************************
!> \brief Sorts excitation entries by ascending primary index, reordering the secondary index,
!> the eigenvector coefficients and - open shell - the spin index alongside
!> \param idx_prim Primary index of each entry; sorted in place and used as the sort key
!> \param idx_sec Secondary index of each entry, reordered to follow idx_prim
!> \param eigvec_entries Eigenvector coefficients of each entry, reordered to follow idx_prim
!> \param idx_spin Optional spin index of each entry (open shell), reordered to follow idx_prim
! **************************************************************************************************
SUBROUTINE sort_excitations(idx_prim, idx_sec, eigvec_entries, idx_spin)
INTEGER, ALLOCATABLE, DIMENSION(:) :: idx_prim, idx_sec
REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigvec_entries
INTEGER, ALLOCATABLE, DIMENSION(:), OPTIONAL :: idx_spin
CHARACTER(LEN=*), PARAMETER :: routineN = 'sort_excitations'
INTEGER :: handle, ii, kk, num_entries, num_mults
INTEGER, ALLOCATABLE, DIMENSION(:) :: idx_prim_work, idx_sec_work, &
idx_spin_work, tmp_index
LOGICAL :: unique_entries
REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigvec_entries_work
CALL timeset(routineN, handle)
num_entries = SIZE(idx_prim)
ALLOCATE (tmp_index(num_entries))
CALL sort(idx_prim, num_entries, tmp_index)
ALLOCATE (idx_sec_work(num_entries))
ALLOCATE (eigvec_entries_work(num_entries))
IF (PRESENT(idx_spin)) ALLOCATE (idx_spin_work(num_entries))
DO ii = 1, num_entries
idx_sec_work(ii) = idx_sec(tmp_index(ii))
eigvec_entries_work(ii) = eigvec_entries(tmp_index(ii))
IF (PRESENT(idx_spin)) idx_spin_work(ii) = idx_spin(tmp_index(ii))
END DO
DEALLOCATE (tmp_index)
DEALLOCATE (idx_sec)
DEALLOCATE (eigvec_entries)
CALL MOVE_ALLOC(idx_sec_work, idx_sec)
CALL MOVE_ALLOC(eigvec_entries_work, eigvec_entries)
IF (PRESENT(idx_spin)) THEN
DEALLOCATE (idx_spin)
CALL MOVE_ALLOC(idx_spin_work, idx_spin)
END IF
!Now check for multiple entries in first idx to check necessity of sorting in second idx
CALL sort_unique(idx_prim, unique_entries)
IF (.NOT. unique_entries) THEN
ALLOCATE (idx_prim_work(num_entries))
idx_prim_work(:) = idx_prim(:)
! Find duplicate entries in idx_prim
DO ii = 1, num_entries
IF (idx_prim_work(ii) == 0) CYCLE
num_mults = COUNT(idx_prim_work == idx_prim_work(ii))
IF (num_mults > 1) THEN
!Set all duplicate entries to 0
idx_prim_work(ii:ii + num_mults - 1) = 0
!Start sorting in secondary index
ALLOCATE (idx_sec_work(num_mults))
ALLOCATE (eigvec_entries_work(num_mults))
idx_sec_work(:) = idx_sec(ii:ii + num_mults - 1)
eigvec_entries_work(:) = eigvec_entries(ii:ii + num_mults - 1)
IF (PRESENT(idx_spin)) THEN
ALLOCATE (idx_spin_work(num_mults))
idx_spin_work(:) = idx_spin(ii:ii + num_mults - 1)
END IF
ALLOCATE (tmp_index(num_mults))
CALL sort(idx_sec_work, num_mults, tmp_index)
!Now write newly sorted indices to original arrays
DO kk = ii, ii + num_mults - 1
idx_sec(kk) = idx_sec_work(kk - ii + 1)
eigvec_entries(kk) = eigvec_entries_work(tmp_index(kk - ii + 1))
IF (PRESENT(idx_spin)) idx_spin(kk) = idx_spin_work(tmp_index(kk - ii + 1))
END DO
!Deallocate work arrays
DEALLOCATE (tmp_index)
DEALLOCATE (idx_sec_work)
DEALLOCATE (eigvec_entries_work)
IF (PRESENT(idx_spin)) DEALLOCATE (idx_spin_work)
END IF
idx_prim_work(ii) = idx_prim(ii)
END DO
DEALLOCATE (idx_prim_work)
END IF
CALL timestop(handle)
END SUBROUTINE sort_excitations
! **************************************************************************************************
!> \brief Roughly estimates the needed runtime and memory during the BSE run
!> \param n_ov_joint ...
!> \param unit_nr ...
!> \param bse_abba ...
!> \param para_env ...
!> \param diag_runtime_est ...
! **************************************************************************************************
SUBROUTINE estimate_BSE_resources(n_ov_joint, unit_nr, bse_abba, &
para_env, diag_runtime_est)
INTEGER, INTENT(IN) :: n_ov_joint, unit_nr
LOGICAL :: bse_abba
TYPE(mp_para_env_type), POINTER :: para_env
REAL(KIND=dp) :: diag_runtime_est
CHARACTER(LEN=*), PARAMETER :: routineN = 'estimate_BSE_resources'
INTEGER :: handle
REAL(KIND=dp) :: mem_est, mem_est_per_rank
CALL timeset(routineN, handle)
mem_est = fulldiag_memory_estimate_GB(n_ov_joint, bse_abba)
mem_est_per_rank = REAL(mem_est/para_env%num_pe, KIND=dp)
IF (unit_nr > 0) THEN
! WRITE (unit_nr, '(T2,A4,T7,A40,T68,F13.3)') 'BSE|', 'Total peak memory estimate from BSE [GB]', &
! mem_est
WRITE (unit_nr, '(T2,A4,T7,A40,T68,ES13.3)') 'BSE|', 'Total peak memory estimate from BSE [GB]', &
mem_est
WRITE (unit_nr, '(T2,A4,T7,A47,T68,F13.3)') 'BSE|', 'Peak memory estimate per MPI rank from BSE [GB]', &
mem_est_per_rank
WRITE (unit_nr, '(T2,A4)') 'BSE|'
END IF
! Rough estimation of diagonalization runtimes. Baseline was a full BSE Naphthalene
! run with 11000x11000 entries in A/B/C, which took 10s on 32 ranks
diag_runtime_est = REAL(INT(n_ov_joint, KIND=int_8)/11000_int_8, KIND=dp)**3* &
10*32/REAL(para_env%num_pe, KIND=dp)
CALL timestop(handle)
END SUBROUTINE estimate_BSE_resources
! **************************************************************************************************
!> \brief Peak memory of the full diagonalization in GB, all ranks together: n_ov^2 doubles times
!> the matrices held at once, 2 for TDA (A and W_ijab), 10 for ABBA (B, C and the six that
!> build C added)
!> \param n_ov_joint transition pairs in the active window, summed over spins
!> \param do_abba full problem, not TDA
!> \return the estimate in GB
! **************************************************************************************************
PURE FUNCTION fulldiag_memory_estimate_GB(n_ov_joint, do_abba) RESULT(mem_GB)
INTEGER, INTENT(IN) :: n_ov_joint
LOGICAL, INTENT(IN) :: do_abba
REAL(KIND=dp) :: mem_GB
INTEGER :: num_BSE_matrices
INTEGER(KIND=int_8) :: full_dim
num_BSE_matrices = 2
IF (do_abba) num_BSE_matrices = 10
full_dim = INT(n_ov_joint, KIND=int_8)**2*INT(num_BSE_matrices, KIND=int_8)
! 1 GB = 1E9 B, the unit of the free-memory budget it is compared with
mem_GB = 8.0E-9_dp*REAL(full_dim, KIND=dp)
END FUNCTION fulldiag_memory_estimate_GB
! **************************************************************************************************
!> \brief Filters eigenvector entries above a given threshold to describe excitations in the
!> singleparticle basis
!> \param fm_eigvec ...
!> \param idx_homo ...
!> \param idx_virt ...
!> \param eigvec_entries ...
!> \param i_exc ...
!> \param virtual ...
!> \param num_entries ...
!> \param bse_env the BSE environment (settings)
!> \param offsets ...
!> \param virtual_per_spin ...
!> \param idx_spin ...
! **************************************************************************************************
SUBROUTINE filter_eigvec_contrib(fm_eigvec, idx_homo, idx_virt, eigvec_entries, &
i_exc, virtual, num_entries, bse_env, &
offsets, virtual_per_spin, idx_spin)
TYPE(cp_fm_type), INTENT(IN) :: fm_eigvec
INTEGER, ALLOCATABLE, DIMENSION(:) :: idx_homo, idx_virt
REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigvec_entries
INTEGER :: i_exc, virtual, num_entries
TYPE(bse_env_type), INTENT(IN) :: bse_env
INTEGER, DIMENSION(:), INTENT(IN), OPTIONAL :: offsets, virtual_per_spin
INTEGER, ALLOCATABLE, DIMENSION(:), OPTIONAL :: idx_spin
CHARACTER(LEN=*), PARAMETER :: routineN = 'filter_eigvec_contrib'
INTEGER :: eigvec_idx, handle, ii, iproc, isp, jj, &
kk, ksp, ncol_local, nrow_local, &
num_entries_local, r_local, v_local
INTEGER, ALLOCATABLE, DIMENSION(:) :: displ, indx_all, indx_local, &
num_entries_to_comm
INTEGER, DIMENSION(:), POINTER :: col_indices, row_indices
REAL(KIND=dp) :: eigvec_entry
REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: entries_local
TYPE(mp_para_env_type), POINTER :: para_env
CALL timeset(routineN, handle)
para_env => fm_eigvec%matrix_struct%para_env
CALL cp_fm_get_info(matrix=fm_eigvec, &
nrow_local=nrow_local, &
ncol_local=ncol_local, &
row_indices=row_indices, &
col_indices=col_indices)
! the local entries of column i_exc above EPS_X: count, then collect with their (i, a, sigma)
num_entries_local = 0
DO jj = 1, ncol_local
IF (col_indices(jj) /= i_exc) CYCLE
DO ii = 1, nrow_local
IF (ABS(fm_eigvec%local_data(ii, jj)) > bse_env%eps_x) num_entries_local = num_entries_local + 1
END DO
END DO
ALLOCATE (entries_local(num_entries_local), indx_local(3*num_entries_local))
kk = 0
DO jj = 1, ncol_local
IF (col_indices(jj) /= i_exc) CYCLE
DO ii = 1, nrow_local
eigvec_idx = row_indices(ii)
eigvec_entry = fm_eigvec%local_data(ii, jj)
IF (ABS(eigvec_entry) > bse_env%eps_x) THEN
! Decode spin block from the joint row index (blocks are contiguous; sigma is the
! largest offset strictly below eigvec_idx). offsets absent -> closed shell, sigma=1.
isp = 1
r_local = eigvec_idx
v_local = virtual
IF (PRESENT(offsets)) THEN
DO ksp = SIZE(offsets), 1, -1
IF (eigvec_idx > offsets(ksp)) THEN
isp = ksp
EXIT
END IF
END DO
r_local = eigvec_idx - offsets(isp)
v_local = virtual_per_spin(isp)
END IF
kk = kk + 1
indx_local(3*kk - 2) = occ_of_ia(r_local, v_local)
indx_local(3*kk - 1) = virt_of_ia(r_local, v_local)
indx_local(3*kk) = isp
entries_local(kk) = eigvec_entry
END IF
END DO
END DO
! one allgather of the counts, one allgatherv per array: every rank ends with the same
! rank-ordered list
ALLOCATE (num_entries_to_comm(0:para_env%num_pe - 1), displ(0:para_env%num_pe - 1))
CALL para_env%allgather(num_entries_local, num_entries_to_comm)
displ(0) = 0
DO iproc = 1, para_env%num_pe - 1
displ(iproc) = displ(iproc - 1) + num_entries_to_comm(iproc - 1)
END DO
num_entries = SUM(num_entries_to_comm)
ALLOCATE (eigvec_entries(num_entries), indx_all(3*num_entries))
CALL para_env%allgatherv(entries_local, eigvec_entries, num_entries_to_comm, displ)
CALL para_env%allgatherv(indx_local, indx_all, 3*num_entries_to_comm, 3*displ)
ALLOCATE (idx_homo(num_entries), idx_virt(num_entries))
IF (PRESENT(idx_spin)) ALLOCATE (idx_spin(num_entries))
DO kk = 1, num_entries
idx_homo(kk) = indx_all(3*kk - 2)
idx_virt(kk) = indx_all(3*kk - 1)
IF (PRESENT(idx_spin)) idx_spin(kk) = indx_all(3*kk)
END DO
DEALLOCATE (entries_local, indx_local, indx_all, num_entries_to_comm, displ)
NULLIFY (row_indices)
NULLIFY (col_indices)
!Now sort the results according to the involved singleparticle orbitals
! (homo first, then virtual). idx_spin is payload, permuted alongside the entries.
IF (PRESENT(idx_spin)) THEN
CALL sort_excitations(idx_homo, idx_virt, eigvec_entries, idx_spin)
ELSE
CALL sort_excitations(idx_homo, idx_virt, eigvec_entries)
END IF
CALL timestop(handle)
END SUBROUTINE filter_eigvec_contrib
! **************************************************************************************************
!> \brief Spin-block layout for the open-shell (joint) BSE matrix: per-spin OV-pair counts and the
!> block offsets into the joint matrix of dimension n_ov_joint = sum_sigma homo*virtual.
!> \param homo_red per-spin (reduced) number of occupied levels
!> \param virt_red per-spin (reduced) number of virtual levels
!> \param n_ov per-spin OV-pair count (OUT)
!> \param offsets per-spin block offset into the joint matrix (OUT)
!> \param n_ov_joint total joint dimension (OUT)
! **************************************************************************************************
SUBROUTINE get_bse_spin_block_layout(homo_red, virt_red, n_ov, offsets, n_ov_joint)
INTEGER, DIMENSION(:), INTENT(IN) :: homo_red, virt_red
INTEGER, DIMENSION(:), INTENT(OUT) :: n_ov, offsets
INTEGER, INTENT(OUT) :: n_ov_joint
INTEGER :: isp
n_ov_joint = 0
DO isp = 1, SIZE(homo_red)
offsets(isp) = n_ov_joint
n_ov(isp) = homo_red(isp)*virt_red(isp)
n_ov_joint = n_ov_joint + n_ov(isp)
END DO
END SUBROUTINE get_bse_spin_block_layout
! **************************************************************************************************
!> \brief The active MO window on the DFT axis: one absolute MO range for every spin; when the spin
!> windows differ, the smallest range covering all of them
!> \param eigenval_scf DFT eigenvalues (level, spin)
!> \param n_mo number of MOs per spin
!> \param homo occupied MOs per spin
!> \param cutoff_occ ENERGY_CUTOFF_OCC in Hartree, zero or negative for no cutoff
!> \param cutoff_empty ENERGY_CUTOFF_EMPTY in Hartree, zero or negative for no cutoff
!> \param window first and last MO to keep
!> \param warn warn when the spin windows differ; false for a second call in the same run
! **************************************************************************************************
SUBROUTINE bse_window_from_dft(eigenval_scf, n_mo, homo, cutoff_occ, cutoff_empty, window, warn)
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: eigenval_scf
INTEGER, DIMENSION(:), INTENT(IN) :: n_mo, homo
REAL(KIND=dp), INTENT(IN) :: cutoff_occ, cutoff_empty
TYPE(mo_window_type), INTENT(OUT) :: window
LOGICAL, INTENT(IN) :: warn
INTEGER :: ispin, nspins
LOGICAL :: spins_differ
TYPE(mo_window_type), ALLOCATABLE, DIMENSION(:) :: spin_windows
nspins = SIZE(homo)
ALLOCATE (spin_windows(nspins))
DO ispin = 1, nspins
CALL determine_mo_window(eigenval_scf(:, ispin), n_mo(ispin), homo(ispin), &
cutoff_occ, cutoff_empty, spin_windows(ispin))
END DO
IF (nspins > 1) THEN
CALL combine_mo_windows(spin_windows, window, spins_differ)
IF (spins_differ .AND. warn) THEN
CPWARN("BSE: spin-resolved active MO cutoff candidates differ; using combined window.")
END IF
ELSE
window = spin_windows(1)
END IF
DEALLOCATE (spin_windows)
END SUBROUTINE bse_window_from_dft
! **************************************************************************************************
!> \brief The active MO window of bse_window_from_dft, from the DFT eigenvalues of the MO sets
!> \param mos MO sets per spin
!> \param cutoff_occ ENERGY_CUTOFF_OCC in Hartree, zero or negative for no cutoff
!> \param cutoff_empty ENERGY_CUTOFF_EMPTY in Hartree, zero or negative for no cutoff
!> \param window first and last MO to keep, one absolute range for every spin
!> \param warn warn when the spin windows differ
! **************************************************************************************************
SUBROUTINE bse_window_from_mos(mos, cutoff_occ, cutoff_empty, window, warn)
TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
REAL(KIND=dp), INTENT(IN) :: cutoff_occ, cutoff_empty
TYPE(mo_window_type), INTENT(OUT) :: window
LOGICAL, INTENT(IN) :: warn
INTEGER :: ispin, nspins
INTEGER, ALLOCATABLE, DIMENSION(:) :: homo, n_mo
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: eigenval_dft
REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues
nspins = SIZE(mos)
ALLOCATE (homo(nspins), n_mo(nspins))
DO ispin = 1, nspins
CALL get_mo_set(mos(ispin), nmo=n_mo(ispin), homo=homo(ispin), eigenvalues=eigenvalues)
IF (ispin == 1) ALLOCATE (eigenval_dft(n_mo(1), nspins))
eigenval_dft(:, ispin) = eigenvalues(1:n_mo(ispin))
END DO
CALL bse_window_from_dft(eigenval_dft, n_mo, homo, cutoff_occ, cutoff_empty, window, warn)
DEALLOCATE (homo, n_mo, eigenval_dft)
END SUBROUTINE bse_window_from_mos
! **************************************************************************************************
!> \brief Determines indices within the given energy cutoffs and truncates Eigenvalues and matrices
!> \param fm_S_ia_full ...
!> \param fm_S_ij_full ...
!> \param fm_S_ab_full ...
!> \param Eigenval_scf ...
!> \param Eigenval ...
!> \param Eigenval_reduced_scf DFT energies of the window, allocated here
!> \param Eigenval_reduced_qp quasiparticle energies of the window, allocated here
!> \param homo ...
!> \param virtual ...
!> \param dimen_RI ...
!> \param unit_nr ...
!> \param ispin spin of the slabs, selects the GW-corrected virtual count
!> \param bse_env the BSE environment (settings; the GW-corrected level counts); receives the
!> window's slabs fm_S_ia, fm_S_ij, fm_S_ab and its level counts homo_red, virt_red of spin ispin
!> \param window Absolute MO window to retain.
!> \param print_window Whether to print the cutoff summary.
! **************************************************************************************************
SUBROUTINE truncate_BSE_matrices(fm_S_ia_full, fm_S_ij_full, fm_S_ab_full, &
Eigenval_scf, Eigenval, Eigenval_reduced_scf, Eigenval_reduced_qp, &
homo, virtual, dimen_RI, unit_nr, &
ispin, &
bse_env, &
window, print_window)
TYPE(cp_fm_type), INTENT(IN) :: fm_S_ia_full, fm_S_ij_full, fm_S_ab_full
REAL(KIND=dp), DIMENSION(:) :: Eigenval_scf, Eigenval
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: Eigenval_reduced_scf, Eigenval_reduced_qp
INTEGER, INTENT(IN) :: homo, virtual, dimen_RI, unit_nr, ispin
TYPE(bse_env_type), INTENT(INOUT) :: bse_env
TYPE(mo_window_type), INTENT(IN) :: window
LOGICAL, INTENT(IN) :: print_window
CHARACTER(LEN=*), PARAMETER :: routineN = 'truncate_BSE_matrices'
INTEGER :: handle, homo_incl, homo_red, virt_incl, &
virt_red
TYPE(cp_blacs_env_type), POINTER :: context
TYPE(cp_fm_struct_type), POINTER :: fm_struct_ab, fm_struct_ia, fm_struct_ij
TYPE(mp_para_env_type), POINTER :: para_env
CALL timeset(routineN, handle)
homo_incl = window%first_mo
virt_incl = window%last_mo - homo
homo_red = homo - homo_incl + 1
virt_red = virt_incl
IF (print_window .AND. unit_nr > 0) THEN
IF (bse_env%bse_cutoff_occ > 0) THEN
WRITE (unit_nr, '(T2,A4,T7,A29,T71,F10.3)') 'BSE|', 'Cutoff occupied orbitals [eV]', &
bse_env%bse_cutoff_occ*evolt
ELSE
WRITE (unit_nr, '(T2,A4,T7,A37)') 'BSE|', 'No cutoff given for occupied orbitals'
END IF
IF (bse_env%bse_cutoff_empty > 0) THEN
WRITE (unit_nr, '(T2,A4,T7,A26,T71,F10.3)') 'BSE|', 'Cutoff empty orbitals [eV]', &
bse_env%bse_cutoff_empty*evolt
ELSE
WRITE (unit_nr, '(T2,A4,T7,A34)') 'BSE|', 'No cutoff given for empty orbitals'
END IF
WRITE (unit_nr, '(T2,A4,T7,A20,T71,I10)') 'BSE|', 'First occupied index', homo_incl
WRITE (unit_nr, '(T2,A4,T7,A32,T71,I10)') 'BSE|', 'Last empty index (not MO index!)', virt_incl
WRITE (unit_nr, '(T2,A4,T7,A35,T71,F10.3)') 'BSE|', 'Energy of first occupied index [eV]', &
Eigenval(homo_incl)*evolt
WRITE (unit_nr, '(T2,A4,T7,A31,T71,F10.3)') 'BSE|', 'Energy of last empty index [eV]', &
Eigenval(homo + virt_incl)*evolt
WRITE (unit_nr, '(T2,A4,T7,A54,T71,F10.3)') 'BSE|', &
'Energy difference of first occupied index to HOMO [eV]', &
-(Eigenval(homo_incl) - Eigenval(homo))*evolt
WRITE (unit_nr, '(T2,A4,T7,A50,T71,F10.3)') 'BSE|', &
'Energy difference of last empty index to LUMO [eV]', &
(Eigenval(homo + virt_incl) - Eigenval(homo + 1))*evolt
WRITE (unit_nr, '(T2,A4,T7,A35,T71,I10)') 'BSE|', 'Number of GW-corrected occupied MOs', &
bse_env%gw_corr_lev_occ
WRITE (unit_nr, '(T2,A4,T7,A32,T71,I10)') 'BSE|', 'Number of GW-corrected empty MOs', &
bse_env%gw_corr_lev_virt(ispin)
WRITE (unit_nr, '(T2,A4)') 'BSE|'
END IF
IF (unit_nr > 0) THEN
IF (homo - homo_incl + 1 > bse_env%gw_corr_lev_occ) THEN
CPABORT("Number of GW-corrected occupied MOs too small for chosen BSE cutoff")
END IF
IF (virt_incl > bse_env%gw_corr_lev_virt(ispin)) THEN
CPABORT("Number of GW-corrected virtual MOs too small for chosen BSE cutoff")
END IF
END IF
!Truncate full fm_S matrices
!Allocate new truncated matrices of proper size
para_env => fm_S_ia_full%matrix_struct%para_env
context => fm_S_ia_full%matrix_struct%context
CALL cp_fm_struct_create(fm_struct_ia, para_env, context, dimen_RI, homo_red*virt_red)
CALL cp_fm_struct_create(fm_struct_ij, para_env, context, dimen_RI, homo_red*homo_red)
CALL cp_fm_struct_create(fm_struct_ab, para_env, context, dimen_RI, virt_red*virt_red)
CALL cp_fm_create(bse_env%fm_S_ia(ispin), fm_struct_ia, name="fm_S_trunc", set_zero=.TRUE.)
CALL cp_fm_create(bse_env%fm_S_ij(ispin), fm_struct_ij, name="fm_S_ij_trunc", set_zero=.TRUE.)
CALL cp_fm_create(bse_env%fm_S_ab(ispin), fm_struct_ab, name="fm_S_ab_trunc", set_zero=.TRUE.)
!Copy parts of original matrices to truncated ones
IF (bse_env%bse_cutoff_occ > 0 .OR. bse_env%bse_cutoff_empty > 0) THEN
!Truncate eigenvals
ALLOCATE (Eigenval_reduced_scf(homo_red + virt_red), Eigenval_reduced_qp(homo_red + virt_red))
Eigenval_reduced_scf(:) = Eigenval_scf(homo_incl:homo + virt_incl)
Eigenval_reduced_qp(:) = Eigenval(homo_incl:homo + virt_incl)
CALL cp_fm_permute(bse_env%fm_S_ia(ispin), fm_S_ia_full, 1.0_dp, 1, virtual, 1, virt_red, [1, 2, 3, 4], &
first_index=[1, 1, homo_incl, 1], &
last_index=[dimen_RI, 1, (homo_incl) + homo_red - 1, virt_red])
CALL cp_fm_permute(bse_env%fm_S_ij(ispin), fm_S_ij_full, 1.0_dp, 1, homo, 1, homo_red, [1, 2, 3, 4], &
first_index=[1, 1, homo_incl, homo_incl], &
last_index=[dimen_RI, 1, (homo_incl) + homo_red - 1, (homo_incl) + homo_red - 1])
CALL cp_fm_permute(bse_env%fm_S_ab(ispin), fm_S_ab_full, 1.0_dp, 1, bse_env%gw_corr_lev_virt(ispin), 1, &
virt_red, [1, 2, 3, 4], first_index=[1, 1, 1, 1], &
last_index=[dimen_RI, 1, virt_red, virt_red])
ELSE
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T2,A4,T7,A37)') 'BSE|', 'No truncation of BSE matrices applied'
WRITE (unit_nr, '(T2,A4)') 'BSE|'
END IF
ALLOCATE (Eigenval_reduced_scf(homo_red + virt_red), Eigenval_reduced_qp(homo_red + virt_red))
Eigenval_reduced_scf(:) = Eigenval_scf(:)
Eigenval_reduced_qp(:) = Eigenval(:)
CALL cp_fm_to_fm_submat_general(fm_S_ia_full, bse_env%fm_S_ia(ispin), dimen_RI, homo_red*virt_red, &
1, 1, 1, 1, context)
CALL cp_fm_to_fm_submat_general(fm_S_ij_full, bse_env%fm_S_ij(ispin), dimen_RI, homo_red*homo_red, &
1, 1, 1, 1, context)
CALL cp_fm_to_fm_submat_general(fm_S_ab_full, bse_env%fm_S_ab(ispin), dimen_RI, virt_red*virt_red, &
1, 1, 1, 1, context)
END IF
bse_env%homo_red(ispin) = homo_red
bse_env%virt_red(ispin) = virt_red
CALL cp_fm_struct_release(fm_struct_ia)
CALL cp_fm_struct_release(fm_struct_ij)
CALL cp_fm_struct_release(fm_struct_ab)
NULLIFY (para_env)
NULLIFY (context)
CALL timestop(handle)
END SUBROUTINE truncate_BSE_matrices
! **************************************************************************************************
!> \brief ...
!> \param fm_eigvec ...
!> \param fm_eigvec_reshuffled ...
!> \param homo ...
!> \param virtual ...
!> \param n_exc ...
!> \param do_transpose ...
! **************************************************************************************************
SUBROUTINE reshuffle_eigvec(fm_eigvec, fm_eigvec_reshuffled, homo, virtual, n_exc, do_transpose)
TYPE(cp_fm_type), INTENT(IN) :: fm_eigvec
TYPE(cp_fm_type), INTENT(INOUT) :: fm_eigvec_reshuffled
INTEGER, INTENT(IN) :: homo, virtual, n_exc
LOGICAL, INTENT(IN) :: do_transpose
CHARACTER(LEN=*), PARAMETER :: routineN = 'reshuffle_eigvec'
INTEGER :: handle, my_m_col, my_n_row
INTEGER, DIMENSION(4) :: reordering
TYPE(cp_fm_struct_type), POINTER :: fm_struct_eigvec_col, &
fm_struct_eigvec_reshuffled
TYPE(cp_fm_type) :: fm_eigvec_col
CALL timeset(routineN, handle)
! Define reordering:
! (ia,11) to (a1,i1) for transposition
! (ia,11) to (i1,a1) for default
IF (do_transpose) THEN
reordering = [2, 3, 1, 4]
my_n_row = virtual
my_m_col = homo
ELSE
reordering = [1, 3, 2, 4]
my_n_row = homo
my_m_col = virtual
END IF
CALL cp_fm_struct_create(fm_struct_eigvec_col, &
fm_eigvec%matrix_struct%para_env, fm_eigvec%matrix_struct%context, &
homo*virtual, 1)
CALL cp_fm_struct_create(fm_struct_eigvec_reshuffled, &
fm_eigvec%matrix_struct%para_env, fm_eigvec%matrix_struct%context, &
my_n_row, my_m_col)
! Resort indices
CALL cp_fm_create(fm_eigvec_col, fm_struct_eigvec_col, name="BSE_column_vector")
CALL cp_fm_set_all(fm_eigvec_col, 0.0_dp)
CALL cp_fm_create(fm_eigvec_reshuffled, fm_struct_eigvec_reshuffled, name="BSE_reshuffled_eigenvector")
CALL cp_fm_set_all(fm_eigvec_reshuffled, 0.0_dp)
! Fill matrix
CALL cp_fm_to_fm_submat(fm_eigvec, fm_eigvec_col, &
homo*virtual, 1, &
1, n_exc, &
1, 1)
! Reshuffle
CALL cp_fm_permute(fm_eigvec_reshuffled, fm_eigvec_col, 1.0_dp, virtual, 1, 1, 1, reordering)
CALL cp_fm_release(fm_eigvec_col)
CALL cp_fm_struct_release(fm_struct_eigvec_col)
CALL cp_fm_struct_release(fm_struct_eigvec_reshuffled)
CALL timestop(handle)
END SUBROUTINE reshuffle_eigvec
! **************************************************************************************************
!> \brief Borrowed from the tddfpt module with slight adaptions
!> \param bse_env the BSE environment (the QS objects the cube writer reads)
!> \param mos ...
!> \param istate ...
!> \param info_approximation ...
!> \param stride ...
!> \param append_cube ...
!> \param print_section ...
! **************************************************************************************************
SUBROUTINE print_bse_nto_cubes(bse_env, mos, istate, info_approximation, &
stride, append_cube, print_section)
TYPE(bse_env_type), INTENT(IN) :: bse_env
TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
INTEGER, INTENT(IN) :: istate
CHARACTER(LEN=10) :: info_approximation
INTEGER, DIMENSION(:), POINTER :: stride
LOGICAL, INTENT(IN) :: append_cube
TYPE(section_vals_type), POINTER :: print_section
CHARACTER(LEN=*), PARAMETER :: routineN = 'print_bse_nto_cubes'
CHARACTER(LEN=default_path_length) :: filename, info_approx_trunc, &
my_pos_cube, title
INTEGER :: handle, i, iset, nmo, unit_nr_cube
LOGICAL :: mpi_io
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell
TYPE(cp_fm_type), POINTER :: mo_coeff
TYPE(cp_logger_type), POINTER :: logger
TYPE(dft_control_type), POINTER :: dft_control
TYPE(particle_list_type), POINTER :: particles
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(pw_c1d_gs_type) :: wf_g
TYPE(pw_env_type), POINTER :: pw_env
TYPE(pw_pool_p_type), DIMENSION(:), POINTER :: pw_pools
TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
TYPE(pw_r3d_rs_type) :: wf_r
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_subsys_type), POINTER :: subsys
logger => cp_get_default_logger()
CALL timeset(routineN, handle)
dft_control => bse_env%dft_control
! PROPERTIES%BSE runs after the GW released the grids; its requirement reader refuses cube files
IF (.NOT. ASSOCIATED(bse_env%pw_env)) THEN
CPABORT("NTO cube files need the SCF grids: set NTO_ANALYSIS%CUBE_FILES to F")
END IF
pw_env => bse_env%pw_env
CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, pw_pools=pw_pools)
CALL auxbas_pw_pool%create_pw(wf_r)
CALL auxbas_pw_pool%create_pw(wf_g)
subsys => bse_env%subsys
CALL qs_subsys_get(subsys, particles=particles)
my_pos_cube = "REWIND"
IF (append_cube) THEN
my_pos_cube = "APPEND"
END IF
CALL qs_subsys_get(subsys, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set, &
cell=cell, particle_set=particle_set)
DO iset = 1, 2
CALL get_mo_set(mo_set=mos(iset), mo_coeff=mo_coeff, nmo=nmo)
DO i = 1, nmo
CALL calculate_wavefunction(mo_coeff, i, wf_r, wf_g, atomic_kind_set, qs_kind_set, &
cell, dft_control, particle_set, pw_env)
IF (iset == 1) THEN
WRITE (filename, '(A6,I3.3,A5,I2.2,a11)') "_NEXC_", istate, "_NTO_", i, "_Hole_State"
ELSE IF (iset == 2) THEN
WRITE (filename, '(A6,I3.3,A5,I2.2,a15)') "_NEXC_", istate, "_NTO_", i, "_Particle_State"
END IF
info_approx_trunc = TRIM(ADJUSTL(info_approximation))
info_approx_trunc = info_approx_trunc(2:LEN_TRIM(info_approx_trunc) - 1)
filename = TRIM(info_approx_trunc)//TRIM(filename)
mpi_io = .TRUE.
unit_nr_cube = cp_print_key_unit_nr(logger, print_section, '', extension=".cube", &
middle_name=TRIM(filename), file_position=my_pos_cube, &
log_filename=.FALSE., ignore_should_output=.TRUE., mpi_io=mpi_io)
IF (iset == 1) THEN
WRITE (title, *) "Natural Transition Orbital Hole State", i
ELSE IF (iset == 2) THEN
WRITE (title, *) "Natural Transition Orbital Particle State", i
END IF
CALL cp_pw_to_cube(wf_r, unit_nr_cube, title, particles=particles, stride=stride, mpi_io=mpi_io)
CALL cp_print_key_finished_output(unit_nr_cube, logger, print_section, '', &
ignore_should_output=.TRUE., mpi_io=mpi_io)
END DO
END DO
CALL auxbas_pw_pool%give_back_pw(wf_g)
CALL auxbas_pw_pool%give_back_pw(wf_r)
CALL timestop(handle)
END SUBROUTINE print_bse_nto_cubes
! **************************************************************************************************
!> \brief Checks BSE input section and adapts them if necessary
!> \param homo ...
!> \param virtual ...
!> \param unit_nr ...
!> \param bse_env the BSE environment; the settings are normalised in place
! **************************************************************************************************
SUBROUTINE adapt_BSE_input_params(homo, virtual, unit_nr, bse_env)
INTEGER, INTENT(IN) :: homo, virtual, unit_nr
TYPE(bse_env_type), INTENT(INOUT) :: bse_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'adapt_BSE_input_params'
INTEGER :: handle, i, j, n, n_exc_max, &
ndim_periodic_cell, &
ndim_periodic_poisson, &
num_state_list_exceptions
LOGICAL :: explicit
TYPE(cell_type), POINTER :: cell_ref
CALL timeset(routineN, handle)
! the Poisson periodicity was read from the input (finish_bse_env): the grids may be released
NULLIFY (cell_ref)
CALL qs_subsys_get(bse_env%subsys, cell_ref=cell_ref)
ndim_periodic_poisson = bse_env%n_periodic_poisson
ndim_periodic_cell = SUM(cell_ref%perd(1:3)) ! Borrowed from cell_methods.F/write_cell_low
! The iterative solver computes the NUM_EXC_EN lowest excitations only
n_exc_max = homo*virtual
IF (bse_env%bse_diag_method == bse_iterdiag) THEN
n_exc_max = MIN(n_exc_max, bse_env%num_exc_en)
END IF
! Handle negative NUM_PRINT_EXC
IF (bse_env%num_print_exc < 0 .OR. &
bse_env%num_print_exc > n_exc_max) THEN
bse_env%num_print_exc = n_exc_max
IF (unit_nr > 0) THEN
CALL cp_hint(__LOCATION__, &
"Keyword NUM_PRINT_EXC is either negative or too large. "// &
"Printing all computed excitations.")
END IF
END IF
! Default to NUM_PRINT_EXC if too large or negative,
! but only if NTOs are called - would be confusing for the user otherwise
! Prepare and adapt user inputs for NTO analysis
! Logic: Explicit state list overrides NUM_PRINT_EXC_NTOS