LCOV - code coverage report
Current view: top level - src - bse_full_diag.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 98.4 % 425 418
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 9 9

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Routines for the full diagonalization of GW + Bethe-Salpeter for computing
      10              : !> electronic excitations
      11              : !> \par History
      12              : !>      10.2023 created [Maximilian Graml]
      13              : ! **************************************************************************************************
      14              : MODULE bse_full_diag
      15              : 
      16              :    USE bse_print,                       ONLY: print_excitation_energies,&
      17              :                                               print_exciton_descriptors,&
      18              :                                               print_optical_properties,&
      19              :                                               print_output_header,&
      20              :                                               print_transition_amplitudes
      21              :    USE bse_properties,                  ONLY: calculate_NTOs,&
      22              :                                               exciton_descr_type,&
      23              :                                               get_exciton_descriptors,&
      24              :                                               get_oscillator_strengths
      25              :    USE bse_types,                       ONLY: bse_env_type
      26              :    USE bse_util,                        ONLY: assemble_joint_ov_slab,&
      27              :                                               comp_eigvec_coeff_BSE,&
      28              :                                               get_bse_spin_block_layout,&
      29              :                                               get_multipoles_mo,&
      30              :                                               occ_of_ia,&
      31              :                                               reshuffle_eigvec,&
      32              :                                               virt_of_ia
      33              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_create,&
      34              :                                               cp_blacs_env_release,&
      35              :                                               cp_blacs_env_type
      36              :    USE cp_control_types,                ONLY: dft_control_type,&
      37              :                                               tddfpt2_control_type
      38              :    USE cp_fm_basic_linalg,              ONLY: cp_fm_column_scale,&
      39              :                                               cp_fm_row_scale,&
      40              :                                               cp_fm_scale,&
      41              :                                               cp_fm_scale_and_add
      42              :    USE cp_fm_cholesky,                  ONLY: cp_fm_cholesky_decompose
      43              :    USE cp_fm_diag,                      ONLY: choose_eigv_solver
      44              :    USE cp_fm_redistribution,            ONLY: cp_fm_permute
      45              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      46              :                                               cp_fm_struct_release,&
      47              :                                               cp_fm_struct_type
      48              :    USE cp_fm_types,                     ONLY: &
      49              :         cp_fm_create, cp_fm_get_diag, cp_fm_get_info, cp_fm_get_submatrix, cp_fm_release, &
      50              :         cp_fm_set_all, cp_fm_to_fm, cp_fm_to_fm_submat, cp_fm_to_fm_triangular, cp_fm_type
      51              :    USE exstates_types,                  ONLY: excited_energy_type
      52              :    USE input_constants,                 ONLY: bse_iterdiag,&
      53              :                                               bse_screening_alpha,&
      54              :                                               bse_screening_rpa,&
      55              :                                               bse_screening_w0,&
      56              :                                               bse_singlet,&
      57              :                                               bse_triplet
      58              :    USE kinds,                           ONLY: dp
      59              :    USE parallel_gemm_api,               ONLY: parallel_gemm
      60              :    USE particle_types,                  ONLY: particle_type
      61              :    USE physcon,                         ONLY: evolt
      62              :    USE qs_subsys_types,                 ONLY: qs_subsys_get
      63              : #include "./base/base_uses.f90"
      64              : 
      65              :    IMPLICIT NONE
      66              : 
      67              :    PRIVATE
      68              : 
      69              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'bse_full_diag'
      70              : 
      71              :    PUBLIC :: create_A, diagonalize_A, create_B, create_A_and_B, create_hermitian_form_of_ABBA, &
      72              :              diagonalize_C, postprocess_bse, create_hermitian_form_rpa
      73              : 
      74              : CONTAINS
      75              : 
      76              : ! **************************************************************************************************
      77              : !> \brief Matrix A constructed from GW energies and 3c-B-matrices (cf. subroutine screen_slabs)
      78              : !>   A_ia,jb = (ε_a-ε_i) δ_ij δ_ab + α * v_ia,jb - W_ij,ab
      79              : !>   ε_a, ε_i are GW singleparticle energies from Eigenval_reduced
      80              : !>   α is a spin-dependent factor
      81              : !>   v_ia,jb = \sum_P B^P_ia B^P_jb (unscreened Coulomb interaction)
      82              : !>   W_ij,ab = \sum_P \bar{B}^P_ij B^P_ab (screened Coulomb interaction)
      83              : !> \param fm_S_ia ...
      84              : !> \param fm_S_bar_ij ...
      85              : !> \param fm_S_ab ...
      86              : !> \param fm_A ...
      87              : !> \param Eigenval ...
      88              : !> \param unit_nr ...
      89              : !> \param homo ...
      90              : !> \param virtual ...
      91              : !> \param dimen_RI ...
      92              : !> \param bse_env the BSE environment (settings; para_env, dft_control, exstate_env)
      93              : ! **************************************************************************************************
      94           56 :    SUBROUTINE create_A(fm_S_ia, fm_S_bar_ij, fm_S_ab, &
      95           56 :                        fm_A, Eigenval, unit_nr, &
      96           56 :                        homo, virtual, dimen_RI, bse_env)
      97              : 
      98              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(IN)         :: fm_S_ia, fm_S_bar_ij, fm_S_ab
      99              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: fm_A
     100              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: Eigenval
     101              :       INTEGER, INTENT(IN)                                :: unit_nr
     102              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: homo, virtual
     103              :       INTEGER, INTENT(IN)                                :: dimen_RI
     104              :       TYPE(bse_env_type), INTENT(IN)                     :: bse_env
     105              : 
     106              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'create_A'
     107              : 
     108              :       INTEGER :: a_virt_row, handle, i_occ_row, i_row_global, ii, isp, j_col_global, jj, k_isp, &
     109              :          k_ov, n_ov_joint, ncol_local_A, nrow_local_A, nspins, sizeeigen
     110           56 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: eig_offsets, n_ov, offsets
     111              :       INTEGER, DIMENSION(4)                              :: reordering
     112           56 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices_A, row_indices_A
     113              :       REAL(KIND=dp)                                      :: alpha, alpha_screening, eigen_diff
     114              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     115              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_A, fm_struct_S_joint, &
     116              :                                                             fm_struct_W
     117              :       TYPE(cp_fm_type)                                   :: fm_A_copy, fm_S_joint, fm_W
     118              :       TYPE(dft_control_type), POINTER                    :: dft_control
     119              :       TYPE(excited_energy_type), POINTER                 :: ex_env
     120              :       TYPE(tddfpt2_control_type), POINTER                :: tddfpt_control
     121              : 
     122           56 :       CALL timeset(routineN, handle)
     123              : 
     124           56 :       nspins = SIZE(homo)
     125          280 :       ALLOCATE (n_ov(nspins), offsets(nspins), eig_offsets(nspins))
     126           56 :       CALL get_bse_spin_block_layout(homo, virtual, n_ov, offsets, n_ov_joint)
     127              :       ! Flat Eigenval layout: sigma-block isp at eig_offsets(isp)+1 .. eig_offsets(isp)+homo(isp)+virtual(isp)
     128           56 :       eig_offsets(1) = 0
     129           70 :       DO isp = 2, nspins
     130           70 :          eig_offsets(isp) = eig_offsets(isp - 1) + homo(isp - 1) + virtual(isp - 1)
     131              :       END DO
     132              : 
     133           56 :       NULLIFY (dft_control, tddfpt_control)
     134           56 :       dft_control => bse_env%dft_control
     135           56 :       tddfpt_control => dft_control%tddfpt2_control
     136              : 
     137           56 :       IF (unit_nr > 0 .AND. bse_env%bse_debug_print) THEN
     138            4 :          WRITE (unit_nr, '(T2,A10,T13,A10)') 'BSE|DEBUG|', 'Creating A'
     139              :       END IF
     140              : 
     141              :       !Determines factor of exchange term, depending on requested spin configuration (cf. input_constants.F)
     142          112 :       SELECT CASE (bse_env%bse_spin_config)
     143              :       CASE (bse_singlet)
     144           56 :          alpha = 2.0_dp
     145              :       CASE (bse_triplet)
     146           56 :          alpha = 0.0_dp
     147              :       END SELECT
     148              :       ! For open-shell (nspins>1): each spin block contributes once; SPIN_CONFIG is ignored.
     149           56 :       IF (nspins > 1) THEN
     150              :          CALL cp_warn(__LOCATION__, &
     151           14 :                       "BSE: SPIN_CONFIG ignored for open-shell reference; using alpha=1.")
     152           14 :          alpha = 1.0_dp
     153              :       END IF
     154              : 
     155           56 :       IF (bse_env%screening_method == bse_screening_alpha) THEN
     156            2 :          alpha_screening = bse_env%screening_factor
     157              :       ELSE
     158           54 :          alpha_screening = 1.0_dp
     159              :       END IF
     160              : 
     161              :       ! create the blacs env for ij matrices (NOT fm_S_ia%matrix_struct related parallel_gemms!)
     162           56 :       NULLIFY (blacs_env)
     163           56 :       CALL cp_blacs_env_create(blacs_env=blacs_env, para_env=bse_env%para_env)
     164              : 
     165              :       ! We have to use the same blacs_env for A as for the matrices fm_S_ia from RPA
     166              :       ! Logic: A_ia,jb = (ε_a-ε_i) δ_ij δ_ab + α * v_ia,jb - W_ij,ab
     167              :       ! We create v_ia,jb and W_ij,ab, then we communicate entries from local W_ij,ab
     168              :       ! to the full matrix v_ia,jb. By adding these and the energy diffenences: v_ia,jb -> A_ia,jb
     169              :       ! We use the A matrix already from the start instead of v
     170              :       CALL cp_fm_struct_create(fm_struct_A, context=fm_S_ia(1)%matrix_struct%context, &
     171              :                                nrow_global=n_ov_joint, ncol_global=n_ov_joint, &
     172           56 :                                para_env=fm_S_ia(1)%matrix_struct%para_env)
     173           56 :       CALL cp_fm_create(fm_A, fm_struct_A, name="fm_A_iajb")
     174           56 :       CALL cp_fm_set_all(fm_A, 0.0_dp)
     175              :       ! fm_A_copy only used in the TDDFPT do_bse_w_only path (closed-shell only)
     176           56 :       IF (tddfpt_control%do_bse_w_only .AND. nspins == 1) THEN
     177            2 :          CALL cp_fm_create(fm_A_copy, fm_struct_A, name="fm_A_iajb")
     178            2 :          CALL cp_fm_set_all(fm_A_copy, 0.0_dp)
     179              :       END IF
     180              : 
     181              :       ! Create A matrix from GW Energies, v_ia,jb and W_ij,ab
     182              :       ! v_ia,jb = \sum_P B^P_ia B^P_jb  (Coulomb)
     183           56 :       IF ((.NOT. tddfpt_control%do_bse) .AND. (.NOT. tddfpt_control%do_bse_w_only)) THEN
     184           52 :          IF (nspins > 1) THEN
     185              :             ! Assemble joint ia-slab for a single Coulomb gemm across all spin blocks
     186              :             CALL cp_fm_struct_create(fm_struct_S_joint, &
     187              :                                      context=fm_S_ia(1)%matrix_struct%context, &
     188              :                                      nrow_global=dimen_RI, ncol_global=n_ov_joint, &
     189           14 :                                      para_env=fm_S_ia(1)%matrix_struct%para_env)
     190           14 :             CALL cp_fm_create(fm_S_joint, fm_struct_S_joint, name="fm_S_ia_joint")
     191           14 :             CALL cp_fm_set_all(fm_S_joint, 0.0_dp)
     192           14 :             CALL assemble_joint_ov_slab(fm_S_ia, offsets, n_ov, dimen_RI, fm_S_joint)
     193              :             CALL parallel_gemm(transa="T", transb="N", m=n_ov_joint, n=n_ov_joint, k=dimen_RI, &
     194              :                                alpha=alpha, matrix_a=fm_S_joint, matrix_b=fm_S_joint, beta=0.0_dp, &
     195           14 :                                matrix_c=fm_A)
     196           14 :             CALL cp_fm_release(fm_S_joint)
     197           14 :             CALL cp_fm_struct_release(fm_struct_S_joint)
     198              :          ELSE
     199              :             CALL parallel_gemm(transa="T", transb="N", m=homo(1)*virtual(1), n=homo(1)*virtual(1), &
     200              :                                k=dimen_RI, alpha=alpha, &
     201              :                                matrix_a=fm_S_ia(1), matrix_b=fm_S_ia(1), &
     202           38 :                                beta=0.0_dp, matrix_c=fm_A)
     203              :          END IF
     204              :       END IF
     205              : 
     206           56 :       IF (unit_nr > 0 .AND. bse_env%bse_debug_print) THEN
     207            4 :          WRITE (unit_nr, '(T2,A10,T13,A16)') 'BSE|DEBUG|', 'Allocated A_iajb'
     208              :       END IF
     209              : 
     210              :       ! W term on sigma-diagonal blocks only: W^sigma_ij,ab = sum_P barB^P_ij B^P_ab
     211              :       ! offsets(isp) places each block at the correct position in joint A.
     212              :       ! For nspins=1: offsets(1)=0, equivalent to the original code.
     213          126 :       DO isp = 1, nspins
     214          126 :          IF (bse_env%screening_method /= bse_screening_rpa) THEN
     215              :             CALL cp_fm_struct_create(fm_struct_W, context=fm_S_ab(isp)%matrix_struct%context, &
     216              :                                      nrow_global=homo(isp)**2, ncol_global=virtual(isp)**2, &
     217           68 :                                      para_env=fm_S_ab(isp)%matrix_struct%para_env)
     218           68 :             CALL cp_fm_create(fm_W, fm_struct_W, name="fm_W_ijab")
     219           68 :             CALL cp_fm_set_all(fm_W, 0.0_dp)
     220              :             !W_ij,ab = \sum_P \bar{B}^P_ij B^P_ab
     221              :             CALL parallel_gemm(transa="T", transb="N", m=homo(isp)**2, n=virtual(isp)**2, &
     222              :                                k=dimen_RI, alpha=alpha_screening, &
     223              :                                matrix_a=fm_S_bar_ij(isp), matrix_b=fm_S_ab(isp), &
     224           68 :                                beta=0.0_dp, matrix_c=fm_W)
     225           68 :             reordering = [1, 3, 2, 4]
     226              :             CALL cp_fm_permute(fm_A, fm_W, -1.0_dp, homo(isp), virtual(isp), virtual(isp), virtual(isp), reordering, &
     227           68 :                                row_offset=offsets(isp), col_offset=offsets(isp))
     228           68 :             IF (nspins == 1 .AND. tddfpt_control%do_bse_w_only) THEN
     229            2 :                CALL cp_fm_permute(fm_A_copy, fm_W, -1.0_dp, homo(1), virtual(1), virtual(1), virtual(1), reordering)
     230              :             END IF
     231              :             ! W and A stash for TDDFPT path (closed-shell only; open-shell deferred)
     232              :             IF (nspins == 1) THEN
     233           40 :                IF (tddfpt_control%do_bse .OR. tddfpt_control%do_bse_w_only .OR. &
     234              :                    tddfpt_control%do_bse_gw_only) THEN
     235            4 :                   NULLIFY (ex_env)
     236            4 :                   ex_env => bse_env%exstate_env
     237            4 :                   IF (.NOT. tddfpt_control%do_bse_gw_only) THEN
     238           12 :                      ALLOCATE (ex_env%bse_w_matrix_MO(1, 1))
     239           12 :                      ALLOCATE (ex_env%bse_a_matrix_MO(1, 1))
     240            4 :                      CALL cp_fm_create(ex_env%bse_w_matrix_MO(1, 1), fm_struct_W)
     241            4 :                      CALL cp_fm_create(ex_env%bse_a_matrix_MO(1, 1), fm_struct_A)
     242            4 :                      CALL cp_fm_to_fm(fm_W, ex_env%bse_w_matrix_MO(1, 1))
     243            4 :                      IF (tddfpt_control%do_bse_w_only) THEN
     244            2 :                         CALL cp_fm_to_fm(fm_A_copy, ex_env%bse_a_matrix_MO(1, 1))
     245              :                      ELSE
     246            2 :                         CALL cp_fm_to_fm(fm_A, ex_env%bse_a_matrix_MO(1, 1))
     247              :                      END IF
     248              :                   END IF
     249              :                END IF
     250              :             END IF
     251           68 :             CALL cp_fm_release(fm_W)
     252          136 :             CALL cp_fm_struct_release(fm_struct_W)
     253              :          END IF
     254              :       END DO
     255              : 
     256           56 :       IF (unit_nr > 0 .AND. bse_env%bse_debug_print) THEN
     257            4 :          WRITE (unit_nr, '(T2,A10,T13,A16)') 'BSE|DEBUG|', 'Allocated W_ijab'
     258              :       END IF
     259           56 :       IF (nspins == 1 .AND. tddfpt_control%do_bse_w_only) CALL cp_fm_release(fm_A_copy)
     260              : 
     261              :       ! Get local row/col indices for direct diagonal access
     262              :       CALL cp_fm_get_info(matrix=fm_A, nrow_local=nrow_local_A, ncol_local=ncol_local_A, &
     263           56 :                           row_indices=row_indices_A, col_indices=col_indices_A)
     264              : 
     265              :       !Add (ε_a-ε_i) on the diagonal of each sigma-block; cross-spin blocks have no ε contribution.
     266           56 :       IF (.NOT. tddfpt_control%do_bse) THEN
     267         6794 :          DO ii = 1, nrow_local_A
     268         6740 :             i_row_global = row_indices_A(ii)
     269      2136042 :             DO jj = 1, ncol_local_A
     270      2129248 :                j_col_global = col_indices_A(jj)
     271      2135988 :                IF (i_row_global == j_col_global) THEN
     272              :                   ! Decode spin: isp such that i_row_global in [offsets(isp)+1, offsets(isp)+n_ov(isp)]
     273         5218 :                   isp = nspins
     274         5218 :                   DO k_isp = 1, nspins - 1
     275         5218 :                      IF (i_row_global <= offsets(k_isp) + n_ov(k_isp)) THEN
     276              :                         isp = k_isp
     277              :                         EXIT
     278              :                      END IF
     279              :                   END DO
     280         3940 :                   k_ov = i_row_global - offsets(isp)
     281         3940 :                   i_occ_row = occ_of_ia(k_ov, virtual(isp))
     282         3940 :                   a_virt_row = virt_of_ia(k_ov, virtual(isp))
     283              :                   eigen_diff = Eigenval(eig_offsets(isp) + a_virt_row + homo(isp)) - &
     284         3940 :                                Eigenval(eig_offsets(isp) + i_occ_row)
     285         3940 :                   fm_A%local_data(ii, jj) = fm_A%local_data(ii, jj) + eigen_diff
     286              :                END IF
     287              :             END DO
     288              :          END DO
     289              :       END IF
     290              : 
     291              :       ! GW eigenvalue stash for TDDFPT path (closed-shell only)
     292           56 :       IF (nspins == 1) THEN
     293           42 :          IF (tddfpt_control%do_bse .OR. tddfpt_control%do_bse_w_only .OR. &
     294              :              tddfpt_control%do_bse_gw_only) THEN
     295            4 :             sizeeigen = SIZE(Eigenval)
     296           12 :             ALLOCATE (ex_env%gw_eigen(sizeeigen))
     297          134 :             ex_env%gw_eigen(:) = Eigenval(:)
     298              :          END IF
     299              :       END IF
     300              : 
     301           56 :       CALL cp_fm_struct_release(fm_struct_A)
     302           56 :       DEALLOCATE (n_ov, offsets, eig_offsets)
     303              : 
     304           56 :       CALL cp_blacs_env_release(blacs_env)
     305              : 
     306           56 :       CALL timestop(handle)
     307              : 
     308          168 :    END SUBROUTINE create_A
     309              : 
     310              : ! **************************************************************************************************
     311              : !> \brief Matrix B constructed from 3c-B-matrices (cf. subroutine screen_slabs)
     312              : !>   B_ia,jb = α * v_ia,jb - W_ib,aj
     313              : !>   α is a spin-dependent factor
     314              : !>   v_ia,jb = \sum_P B^P_ia B^P_jb (unscreened Coulomb interaction)
     315              : !>   W_ib,aj = \sum_P \bar{B}^P_ib B^P_aj (screened Coulomb interaction)
     316              : !> \param fm_S_ia ...
     317              : !> \param fm_S_bar_ia ...
     318              : !> \param fm_B ...
     319              : !> \param homo ...
     320              : !> \param virtual ...
     321              : !> \param dimen_RI ...
     322              : !> \param unit_nr ...
     323              : !> \param bse_env the BSE environment (settings)
     324              : ! **************************************************************************************************
     325           34 :    SUBROUTINE create_B(fm_S_ia, fm_S_bar_ia, fm_B, &
     326           34 :                        homo, virtual, dimen_RI, unit_nr, bse_env)
     327              : 
     328              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(IN)         :: fm_S_ia, fm_S_bar_ia
     329              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: fm_B
     330              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: homo, virtual
     331              :       INTEGER, INTENT(IN)                                :: dimen_RI, unit_nr
     332              :       TYPE(bse_env_type), INTENT(IN)                     :: bse_env
     333              : 
     334              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'create_B'
     335              : 
     336              :       INTEGER                                            :: handle, isp, n_ov_joint, nspins
     337              :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: n_ov, offsets
     338              :       INTEGER, DIMENSION(4)                              :: reordering
     339              :       REAL(KIND=dp)                                      :: alpha, alpha_screening
     340              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_B, fm_struct_S_joint, &
     341              :                                                             fm_struct_W
     342              :       TYPE(cp_fm_type)                                   :: fm_S_joint, fm_W
     343              : 
     344           34 :       CALL timeset(routineN, handle)
     345              : 
     346           34 :       nspins = SIZE(homo)
     347          136 :       ALLOCATE (n_ov(nspins), offsets(nspins))
     348           34 :       CALL get_bse_spin_block_layout(homo, virtual, n_ov, offsets, n_ov_joint)
     349              : 
     350           34 :       IF (unit_nr > 0 .AND. bse_env%bse_debug_print) THEN
     351            3 :          WRITE (unit_nr, '(T2,A10,T13,A10)') 'BSE|DEBUG|', 'Creating B'
     352              :       END IF
     353              : 
     354              :       ! Coulomb prefactor: SPIN_CONFIG sector for closed shell; for open shell each spin block
     355              :       ! contributes once (alpha=1). create_A already emits the SPIN_CONFIG-ignored warning.
     356           68 :       SELECT CASE (bse_env%bse_spin_config)
     357              :       CASE (bse_singlet)
     358           34 :          alpha = 2.0_dp
     359              :       CASE (bse_triplet)
     360           34 :          alpha = 0.0_dp
     361              :       END SELECT
     362           34 :       IF (nspins > 1) alpha = 1.0_dp
     363              : 
     364           34 :       IF (bse_env%screening_method == bse_screening_alpha) THEN
     365            2 :          alpha_screening = bse_env%screening_factor
     366              :       ELSE
     367           32 :          alpha_screening = 1.0_dp
     368              :       END IF
     369              : 
     370              :       ! Joint B over all spin blocks: B_ia,jb = alpha*(ia|bj) - W^sigma_ib,aj (W spin-diagonal)
     371           34 :       NULLIFY (fm_struct_B)
     372              :       CALL cp_fm_struct_create(fm_struct_B, context=fm_S_ia(1)%matrix_struct%context, &
     373              :                                nrow_global=n_ov_joint, ncol_global=n_ov_joint, &
     374           34 :                                para_env=fm_S_ia(1)%matrix_struct%para_env)
     375           34 :       CALL cp_fm_create(fm_B, fm_struct_B, name="fm_B_iajb")
     376           34 :       CALL cp_fm_set_all(fm_B, 0.0_dp)
     377              : 
     378              :       ! Coulomb v_ia,jb = sum_P B^P_ia B^P_jb (= (ia|bj)); cross-spin blocks filled automatically.
     379           34 :       IF (nspins > 1) THEN
     380            4 :          NULLIFY (fm_struct_S_joint)
     381              :          CALL cp_fm_struct_create(fm_struct_S_joint, &
     382              :                                   context=fm_S_ia(1)%matrix_struct%context, &
     383              :                                   nrow_global=dimen_RI, ncol_global=n_ov_joint, &
     384            4 :                                   para_env=fm_S_ia(1)%matrix_struct%para_env)
     385            4 :          CALL cp_fm_create(fm_S_joint, fm_struct_S_joint, name="fm_S_ia_joint")
     386            4 :          CALL cp_fm_set_all(fm_S_joint, 0.0_dp)
     387            4 :          CALL assemble_joint_ov_slab(fm_S_ia, offsets, n_ov, dimen_RI, fm_S_joint)
     388              :          CALL parallel_gemm(transa="T", transb="N", m=n_ov_joint, n=n_ov_joint, k=dimen_RI, &
     389              :                             alpha=alpha, matrix_a=fm_S_joint, matrix_b=fm_S_joint, beta=0.0_dp, &
     390            4 :                             matrix_c=fm_B)
     391            4 :          CALL cp_fm_release(fm_S_joint)
     392            4 :          CALL cp_fm_struct_release(fm_struct_S_joint)
     393              :       ELSE
     394              :          CALL parallel_gemm(transa="T", transb="N", m=homo(1)*virtual(1), n=homo(1)*virtual(1), &
     395              :                             k=dimen_RI, alpha=alpha, &
     396              :                             matrix_a=fm_S_ia(1), matrix_b=fm_S_ia(1), &
     397           30 :                             beta=0.0_dp, matrix_c=fm_B)
     398              :       END IF
     399              : 
     400           34 :       IF (unit_nr > 0 .AND. bse_env%bse_debug_print) THEN
     401            3 :          WRITE (unit_nr, '(T2,A10,T13,A16)') 'BSE|DEBUG|', 'Allocated B_iajb'
     402              :       END IF
     403              : 
     404              :       ! W^sigma_ib,aj = sum_P barB^P_ib B^P_aj on sigma-diagonal blocks only (offsets place them).
     405              :       ! reordering [1,4,3,2] maps W_ib,ja -> B_ia,jb. For nspins=1: offsets(1)=0 (original code).
     406           34 :       IF (bse_env%screening_method /= bse_screening_rpa) THEN
     407           56 :          DO isp = 1, nspins
     408           30 :             NULLIFY (fm_struct_W)
     409              :             CALL cp_fm_struct_create(fm_struct_W, &
     410              :                                      context=fm_S_ia(isp)%matrix_struct%context, &
     411              :                                      nrow_global=homo(isp)*virtual(isp), &
     412              :                                      ncol_global=homo(isp)*virtual(isp), &
     413           30 :                                      para_env=fm_S_ia(isp)%matrix_struct%para_env)
     414           30 :             CALL cp_fm_create(fm_W, fm_struct_W, name="fm_W_ibaj")
     415           30 :             CALL cp_fm_set_all(fm_W, 0.0_dp)
     416              :             CALL parallel_gemm(transa="T", transb="N", m=homo(isp)*virtual(isp), &
     417              :                                n=homo(isp)*virtual(isp), k=dimen_RI, alpha=alpha_screening, &
     418              :                                matrix_a=fm_S_bar_ia(isp), matrix_b=fm_S_ia(isp), &
     419           30 :                                beta=0.0_dp, matrix_c=fm_W)
     420           30 :             reordering = [1, 4, 3, 2]
     421              :             CALL cp_fm_permute(fm_B, fm_W, -1.0_dp, virtual(isp), virtual(isp), virtual(isp), virtual(isp), &
     422           30 :                                reordering, row_offset=offsets(isp), col_offset=offsets(isp))
     423           30 :             CALL cp_fm_release(fm_W)
     424           86 :             CALL cp_fm_struct_release(fm_struct_W)
     425              :          END DO
     426              :       END IF
     427              : 
     428           34 :       CALL cp_fm_struct_release(fm_struct_B)
     429           34 :       DEALLOCATE (n_ov, offsets)
     430              : 
     431           34 :       CALL timestop(handle)
     432              : 
     433           68 :    END SUBROUTINE create_B
     434              : 
     435              : ! **************************************************************************************************
     436              : !> \brief Explicit A, and B for ABBA, from the slabs the screening method contracts: the bare ones for
     437              : !>        TDHF and ALPHA screening, the W-multiplied ones otherwise
     438              : !> \param fm_S_ia ...
     439              : !> \param fm_S_ij ...
     440              : !> \param fm_S_ab ...
     441              : !> \param fm_S_bar_ia ...
     442              : !> \param fm_S_bar_ij ...
     443              : !> \param fm_A ...
     444              : !> \param fm_B created only for ABBA
     445              : !> \param do_abba ...
     446              : !> \param Eigenval ...
     447              : !> \param unit_nr ...
     448              : !> \param homo ...
     449              : !> \param virtual ...
     450              : !> \param dimen_RI ...
     451              : !> \param bse_env the BSE environment (settings)
     452              : ! **************************************************************************************************
     453           56 :    SUBROUTINE create_A_and_B(fm_S_ia, fm_S_ij, fm_S_ab, fm_S_bar_ia, &
     454          112 :                              fm_S_bar_ij, fm_A, fm_B, do_abba, Eigenval, unit_nr, &
     455           56 :                              homo, virtual, dimen_RI, bse_env)
     456              : 
     457              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(IN)         :: fm_S_ia, fm_S_ij, fm_S_ab, fm_S_bar_ia, &
     458              :                                                             fm_S_bar_ij
     459              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: fm_A, fm_B
     460              :       LOGICAL, INTENT(IN)                                :: do_abba
     461              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: Eigenval
     462              :       INTEGER, INTENT(IN)                                :: unit_nr
     463              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: homo, virtual
     464              :       INTEGER, INTENT(IN)                                :: dimen_RI
     465              :       TYPE(bse_env_type), INTENT(IN)                     :: bse_env
     466              : 
     467              :       ! RPA skips the W term; TDHF and ALPHA use the bare slabs.
     468           56 :       IF (bse_env%screening_method /= bse_screening_w0) THEN
     469              :          CALL create_A(fm_S_ia, fm_S_ij, fm_S_ab, fm_A, Eigenval, unit_nr, &
     470            6 :                        homo, virtual, dimen_RI, bse_env)
     471            6 :          IF (do_abba) THEN
     472              :             CALL create_B(fm_S_ia, fm_S_ia, fm_B, homo, virtual, dimen_RI, unit_nr, &
     473            6 :                           bse_env)
     474              :          END IF
     475              :       ELSE
     476              :          CALL create_A(fm_S_ia, fm_S_bar_ij, fm_S_ab, fm_A, Eigenval, unit_nr, &
     477           50 :                        homo, virtual, dimen_RI, bse_env)
     478           50 :          IF (do_abba) THEN
     479              :             CALL create_B(fm_S_ia, fm_S_bar_ia, fm_B, homo, virtual, dimen_RI, unit_nr, &
     480           22 :                           bse_env)
     481              :          END IF
     482              :       END IF
     483              : 
     484           56 :    END SUBROUTINE create_A_and_B
     485              : 
     486              : ! **************************************************************************************************
     487              : !> \brief Form C = sqrt(Δ) (Δ + 2 B) sqrt(Δ) in place for RPA, where A-B = diag(Δ).
     488              : !> \param fm_C RPA coupling matrix B on entry; squared excitation matrix C on exit.
     489              : !> \param gap Positive occupied-virtual energy differences Δ.
     490              : ! **************************************************************************************************
     491            8 :    SUBROUTINE create_hermitian_form_rpa(fm_C, gap)
     492              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: fm_C
     493              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: gap
     494              : 
     495              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'create_hermitian_form_rpa'
     496              : 
     497              :       INTEGER                                            :: handle, icol, irow, ncol_local, &
     498              :                                                             nrow_local
     499            8 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
     500              : 
     501            8 :       CALL timeset(routineN, handle)
     502              : 
     503          158 :       IF (MINVAL(gap) <= 0.0_dp) CPABORT("RPA requires positive occupied-virtual energy differences")
     504            8 :       CALL cp_fm_scale(2.0_dp, fm_C)
     505              :       CALL cp_fm_get_info(fm_C, nrow_local=nrow_local, ncol_local=ncol_local, &
     506            8 :                           row_indices=row_indices, col_indices=col_indices)
     507          158 :       DO icol = 1, ncol_local
     508         2705 :          DO irow = 1, nrow_local
     509         2697 :             IF (row_indices(irow) == col_indices(icol)) THEN
     510           75 :                fm_C%local_data(irow, icol) = fm_C%local_data(irow, icol) + gap(row_indices(irow))
     511              :             END IF
     512              :          END DO
     513              :       END DO
     514          158 :       CALL cp_fm_column_scale(fm_C, SQRT(gap))
     515          158 :       CALL cp_fm_row_scale(fm_C, SQRT(gap))
     516              : 
     517            8 :       CALL timestop(handle)
     518              : 
     519            8 :    END SUBROUTINE create_hermitian_form_rpa
     520              : 
     521              :    ! **************************************************************************************************
     522              : !> \brief Construct C = U (A+B) U^T with A-B = U^T U (U upper Cholesky factor) to solve the full
     523              : !>   BSE as a hermitian problem; C is similar to (A-B)(A+B) and has eigenvalues Ω_n^2.
     524              : !>   Any factor M with A-B = M M^T works in Eq. (A7) of F. Furche, J. Chem. Phys. 114, 5982 (2001);
     525              : !>   here M = U^T; U and A+B are kept for the back-transform of Eq. (A10).
     526              : !> \param fm_A matrix A
     527              : !> \param fm_B matrix B
     528              : !> \param fm_C on exit C = U (A+B) U^T
     529              : !> \param fm_chol_A_minus_B on exit the upper Cholesky factor U of A-B, zero below the diagonal
     530              : !> \param fm_A_plus_B on exit A+B
     531              : !> \param unit_nr output unit
     532              : !> \param bse_env the BSE environment (settings)
     533              : ! **************************************************************************************************
     534           28 :    SUBROUTINE create_hermitian_form_of_ABBA(fm_A, fm_B, fm_C, fm_chol_A_minus_B, fm_A_plus_B, unit_nr, bse_env)
     535              : 
     536              :       TYPE(cp_fm_type), INTENT(IN)                       :: fm_A, fm_B
     537              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: fm_C, fm_chol_A_minus_B, fm_A_plus_B
     538              :       INTEGER, INTENT(IN)                                :: unit_nr
     539              :       TYPE(bse_env_type), INTENT(IN)                     :: bse_env
     540              : 
     541              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'create_hermitian_form_of_ABBA'
     542              : 
     543              :       INTEGER                                            :: chol_info, dim_mat, handle
     544           28 :       REAL(KIND=dp), ALLOCATABLE                         :: chol_pivot(:), diag_B(:), gap(:)
     545              :       TYPE(cp_fm_type)                                   :: fm_work
     546              : 
     547           28 :       CALL timeset(routineN, handle)
     548              : 
     549           28 :       CALL cp_fm_create(fm_A_plus_B, fm_A%matrix_struct)
     550           28 :       CALL cp_fm_to_fm(fm_A, fm_A_plus_B)
     551           28 :       CALL cp_fm_scale_and_add(1.0_dp, fm_A_plus_B, 1.0_dp, fm_B)
     552              : 
     553           28 :       IF (bse_env%screening_method == bse_screening_rpa) THEN
     554              :          ! In RPA, A-B is diagonal, so its Cholesky factor is the diagonal sqrt(Δ)
     555            2 :          CALL cp_fm_get_info(fm_A, nrow_global=dim_mat)
     556            8 :          ALLOCATE (gap(dim_mat), diag_B(dim_mat))
     557            2 :          CALL cp_fm_get_diag(fm_A, gap)
     558            2 :          CALL cp_fm_get_diag(fm_B, diag_B)
     559           98 :          gap(:) = gap(:) - diag_B(:)
     560            2 :          CALL cp_fm_create(fm_C, fm_B%matrix_struct)
     561            2 :          CALL cp_fm_to_fm(fm_B, fm_C)
     562            2 :          CALL create_hermitian_form_rpa(fm_C, gap)
     563              :          ! diagonal A-B: U = diag(sqrt(Δ)), so C = sqrt(Δ) (A+B) sqrt(Δ)
     564            2 :          CALL cp_fm_create(fm_chol_A_minus_B, fm_A%matrix_struct)
     565            2 :          CALL cp_fm_set_all(fm_chol_A_minus_B, 0.0_dp, 1.0_dp)
     566           98 :          CALL cp_fm_column_scale(fm_chol_A_minus_B, SQRT(gap))
     567            2 :          IF (unit_nr > 0) THEN
     568            1 :             WRITE (unit_nr, '(T2,A4,T7,A,T65,F16.6)') 'BSE|', &
     569           50 :                'Smallest eigenvalue of A-B (eV)', MINVAL(gap)*evolt
     570              :          END IF
     571            2 :          DEALLOCATE (gap, diag_B)
     572            2 :          CALL timestop(handle)
     573            6 :          RETURN
     574              :       END IF
     575              : 
     576              :       ! Final result: C = U                    (A+B)                U^T                      EQ.I
     577              :       !                   \_/                  \___/                \_/
     578              :       !            fm_chol_A_minus_B        fm_A_plus_B      fm_chol_A_minus_B
     579              :       !                 (EQ.Ia)               (EQ.Ib)              (EQ.Ia)
     580              :       ! with the upper Cholesky factor U of A-B = U^T U (EQ.Ia) and A+B built above (EQ.Ib).
     581              :       ! Intermediate work matrix:
     582              :       ! fm_work: A-B, overwritten by its factorization, then the product U (A+B)              EQ.II
     583              :       ! A-B = U^T U; the Cholesky factorization fails when A-B is not positive definite
     584           26 :       CALL cp_fm_create(fm_work, fm_A%matrix_struct)
     585           26 :       CALL cp_fm_to_fm(fm_A, fm_work)
     586           26 :       CALL cp_fm_scale_and_add(1.0_dp, fm_work, -1.0_dp, fm_B)
     587           26 :       CALL cp_fm_cholesky_decompose(fm_work, info_out=chol_info)
     588           26 :       IF (chol_info /= 0) THEN
     589              :          CALL cp_abort(__LOCATION__, &
     590              :                        "Matrix (A-B) is not positive definite. "// &
     591            0 :                        "Hermitian diagonalization of full ABBA matrix is ill-defined.")
     592              :       END IF
     593              :       ! the factorization leaves A-B in the strict lower part; a zero there lets U enter plain GEMMs
     594           26 :       CALL cp_fm_create(fm_chol_A_minus_B, fm_A%matrix_struct)
     595           26 :       CALL cp_fm_set_all(fm_chol_A_minus_B, 0.0_dp)
     596           26 :       CALL cp_fm_to_fm_triangular(fm_work, fm_chol_A_minus_B, "U")
     597              : 
     598              :       ! each U_ii^2 is a Schur complement of A-B, so min_i U_ii^2 >= smallest eigenvalue of A-B
     599           26 :       CALL cp_fm_get_info(fm_A, nrow_global=dim_mat)
     600           78 :       ALLOCATE (chol_pivot(dim_mat))
     601           26 :       CALL cp_fm_get_diag(fm_chol_A_minus_B, chol_pivot)
     602           26 :       IF (unit_nr > 0) THEN
     603           13 :          WRITE (unit_nr, '(T2,A4,T7,A,T65,F16.6)') 'BSE|', &
     604         1464 :             'Upper bound to smallest eigenvalue of A-B (eV)', MINVAL(chol_pivot**2)*evolt
     605              :       END IF
     606           26 :       DEALLOCATE (chol_pivot)
     607              : 
     608              :       ! C = U (A+B) U^T by two GEMMs
     609           26 :       CALL cp_fm_create(fm_C, fm_A%matrix_struct)
     610           26 :       CALL parallel_gemm("N", "N", dim_mat, dim_mat, dim_mat, 1.0_dp, fm_chol_A_minus_B, fm_A_plus_B, 0.0_dp, fm_work)
     611           26 :       CALL parallel_gemm("N", "T", dim_mat, dim_mat, dim_mat, 1.0_dp, fm_work, fm_chol_A_minus_B, 0.0_dp, fm_C)
     612           26 :       CALL cp_fm_release(fm_work)
     613              : 
     614           26 :       IF (unit_nr > 0 .AND. bse_env%bse_debug_print) THEN
     615            3 :          WRITE (unit_nr, '(T2,A10,T13,A)') 'BSE|DEBUG|', 'Filled C=U (A+B) U^T with A-B=U^T U'
     616              :       END IF
     617              : 
     618           26 :       CALL timestop(handle)
     619          186 :    END SUBROUTINE create_hermitian_form_of_ABBA
     620              : 
     621              : ! **************************************************************************************************
     622              : !> \brief Solving eigenvalue equation C Z^n = (Ω^n)^2 Z^n .
     623              : !>   Here, the eigenvectors Z^n relate to X^n via
     624              : !>   Eq. (A10) in F. Furche J. Chem. Phys., Vol. 114, No. 14, (2001).
     625              : !> \param fm_C ...
     626              : !> \param homo ...
     627              : !> \param virtual ...
     628              : !> \param homo_irred ...
     629              : !> \param fm_chol_A_minus_B upper Cholesky factor U of A-B = U^T U; deallocated on exit
     630              : !> \param fm_A_plus_B A+B; deallocated on exit
     631              : !> \param unit_nr ...
     632              : !> \param diag_est ...
     633              : !> \param bse_env the BSE environment (settings, and the state the post-processing reads)
     634              : !> \param mo_coeff ...
     635              : ! **************************************************************************************************
     636           26 :    SUBROUTINE diagonalize_C(fm_C, homo, virtual, homo_irred, fm_chol_A_minus_B, fm_A_plus_B, &
     637           26 :                             unit_nr, diag_est, bse_env, mo_coeff)
     638              : 
     639              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: fm_C
     640              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: homo, virtual, homo_irred
     641              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: fm_chol_A_minus_B, fm_A_plus_B
     642              :       INTEGER, INTENT(IN)                                :: unit_nr
     643              :       REAL(KIND=dp), INTENT(IN)                          :: diag_est
     644              :       TYPE(bse_env_type), INTENT(IN)                     :: bse_env
     645              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(IN)         :: mo_coeff
     646              : 
     647              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'diagonalize_C'
     648              : 
     649              :       INTEGER                                            :: diag_info, handle, n_ov_joint, nspins
     650           26 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: Exc_ens
     651              :       TYPE(cp_fm_type)                                   :: fm_eigvec_X, fm_eigvec_Y, fm_eigvec_Z, &
     652              :                                                             fm_mat_eigvec_transform_diff, &
     653              :                                                             fm_mat_eigvec_transform_sum
     654              : 
     655           26 :       CALL timeset(routineN, handle)
     656              : 
     657           26 :       nspins = SIZE(homo)
     658           56 :       n_ov_joint = SUM(homo*virtual)
     659              : 
     660           26 :       IF (unit_nr > 0) THEN
     661           13 :          WRITE (unit_nr, '(T2,A4,T7,A17,A22,ES6.0,A3)') 'BSE|', 'Diagonalizing C. ', &
     662           26 :             'This will take around ', diag_est, ' s.'
     663              :       END IF
     664              : 
     665              :       !We have now the full matrix C=U (A+B) U^T
     666              :       !Now: Diagonalize it
     667           26 :       CALL cp_fm_create(fm_eigvec_Z, fm_C%matrix_struct)
     668              : 
     669           78 :       ALLOCATE (Exc_ens(n_ov_joint))
     670              : 
     671           26 :       CALL choose_eigv_solver(fm_C, fm_eigvec_Z, Exc_ens, diag_info)
     672              : 
     673           26 :       IF (diag_info /= 0) THEN
     674              :          CALL cp_abort(__LOCATION__, &
     675            0 :                        "Diagonalization of C=U (A+B) U^T failed in BSE")
     676              :       END IF
     677              : 
     678              :       ! C could have negative eigenvalues, since we do not explicitly check A+B
     679              :       ! for positive definiteness (would make another O(N^6) Diagon. necessary)
     680              :       ! Instead, we include a check here
     681           26 :       IF (Exc_ens(1) < 0) THEN
     682            0 :          IF (unit_nr > 0) THEN
     683              :             CALL cp_abort(__LOCATION__, &
     684              :                           "Matrix C=U (A+B) U^T has negative eigenvalues, i.e. "// &
     685            0 :                           "(A+B) is not positive definite.")
     686              :          END IF
     687              :       END IF
     688         2902 :       Exc_ens = SQRT(Exc_ens)
     689              : 
     690              :       ! Prepare eigenvector for interpretation of singleparticle transitions
     691              :       ! Compare: F. Furche J. Chem. Phys., Vol. 114, No. 14, (2001)
     692              :       ! We aim for the upper part of the vector (X,Y) for a direct comparison with the TDA result
     693              : 
     694              :       ! Following Furche, we basically use Eqs. (A10): First, we multiply
     695              :       ! the factor of A-B with eigenvectors and then the eigenvalues
     696              :       ! One has to be careful about the index structure, since the eigenvector matrix is not symmetric anymore!
     697              : 
     698              :       ! First, Eq. I from (A10) from Furche with (A-B)^0.5 -> U^T: (X+Y)_n = (Ω_n)^-0.5 U^T T_n
     699           26 :       CALL cp_fm_create(fm_mat_eigvec_transform_sum, fm_C%matrix_struct)
     700           26 :       CALL cp_fm_set_all(fm_mat_eigvec_transform_sum, 0.0_dp)
     701              :       CALL parallel_gemm(transa="T", transb="N", m=n_ov_joint, n=n_ov_joint, k=n_ov_joint, alpha=1.0_dp, &
     702              :                          matrix_a=fm_chol_A_minus_B, matrix_b=fm_eigvec_Z, beta=0.0_dp, &
     703           26 :                          matrix_c=fm_mat_eigvec_transform_sum)
     704           26 :       CALL cp_fm_release(fm_chol_A_minus_B)
     705           26 :       CALL cp_fm_release(fm_eigvec_Z)
     706              :       ! This normalizes the eigenvectors
     707           26 :       CALL comp_eigvec_coeff_BSE(fm_mat_eigvec_transform_sum, Exc_ens, -0.5_dp, gamma=2.0_dp, do_transpose=.TRUE.)
     708              : 
     709              :       ! Second, Eq. II from (A10) from Furche, (X-Y)_n = (Ω_n)^0.5 U^-1 T_n, taken from
     710              :       ! (A+B)(X+Y)_n = Ω_n (X-Y)_n, so no inverse of U is needed
     711           26 :       CALL cp_fm_create(fm_mat_eigvec_transform_diff, fm_C%matrix_struct)
     712           26 :       CALL cp_fm_set_all(fm_mat_eigvec_transform_diff, 0.0_dp)
     713              :       CALL parallel_gemm(transa="N", transb="N", m=n_ov_joint, n=n_ov_joint, k=n_ov_joint, alpha=1.0_dp, &
     714              :                          matrix_a=fm_A_plus_B, matrix_b=fm_mat_eigvec_transform_sum, beta=0.0_dp, &
     715           26 :                          matrix_c=fm_mat_eigvec_transform_diff)
     716           26 :       CALL cp_fm_release(fm_A_plus_B)
     717              : 
     718              :       ! This normalizes the eigenvectors; the factor Ω_n^-1 carries the normalization of (X+Y)_n
     719           26 :       CALL comp_eigvec_coeff_BSE(fm_mat_eigvec_transform_diff, Exc_ens, -1.0_dp, gamma=1.0_dp, do_transpose=.TRUE.)
     720              : 
     721              :       ! Now, we add the two equations to obtain X_n
     722              :       ! Add overwrites the first argument, therefore we copy it beforehand
     723           26 :       CALL cp_fm_create(fm_eigvec_X, fm_C%matrix_struct)
     724           26 :       CALL cp_fm_to_fm(fm_mat_eigvec_transform_sum, fm_eigvec_X)
     725           26 :       CALL cp_fm_scale_and_add(1.0_dp, fm_eigvec_X, 1.0_dp, fm_mat_eigvec_transform_diff)
     726              : 
     727              :       ! Now, we subtract the two equations to obtain Y_n
     728              :       ! Add overwrites the first argument, therefore we copy it beforehand
     729           26 :       CALL cp_fm_create(fm_eigvec_Y, fm_C%matrix_struct)
     730           26 :       CALL cp_fm_to_fm(fm_mat_eigvec_transform_sum, fm_eigvec_Y)
     731           26 :       CALL cp_fm_scale_and_add(1.0_dp, fm_eigvec_Y, -1.0_dp, fm_mat_eigvec_transform_diff)
     732              : 
     733              :       !Cleanup
     734           26 :       CALL cp_fm_release(fm_mat_eigvec_transform_diff)
     735           26 :       CALL cp_fm_release(fm_mat_eigvec_transform_sum)
     736              : 
     737           26 :       IF (nspins == 1) THEN
     738              :          CALL postprocess_bse(Exc_ens, fm_eigvec_X, bse_env, mo_coeff, &
     739              :                               homo(1), virtual(1), homo_irred(1), unit_nr, &
     740           22 :                               .FALSE., fm_eigvec_Y)
     741              :       ELSE
     742              :          ! Open-shell ABBA: helper forms X+Y internally and prints amplitudes (X and Y).
     743              :          CALL bse_open_shell_optical(Exc_ens, fm_eigvec_X, homo, virtual, homo_irred, &
     744            4 :                                      .FALSE., mo_coeff, bse_env, unit_nr, fm_eigvec_Y)
     745              :       END IF
     746              : 
     747           26 :       DEALLOCATE (Exc_ens)
     748           26 :       CALL cp_fm_release(fm_eigvec_X)
     749           26 :       CALL cp_fm_release(fm_eigvec_Y)
     750              : 
     751           26 :       CALL timestop(handle)
     752              : 
     753          182 :    END SUBROUTINE diagonalize_C
     754              : 
     755              : ! **************************************************************************************************
     756              : !> \brief Solving hermitian eigenvalue equation A X^n = Ω^n X^n
     757              : !> \param fm_A ...
     758              : !> \param homo ...
     759              : !> \param virtual ...
     760              : !> \param homo_irred ...
     761              : !> \param unit_nr ...
     762              : !> \param diag_est ...
     763              : !> \param bse_env the BSE environment (settings, and the state the post-processing reads)
     764              : !> \param mo_coeff ...
     765              : ! **************************************************************************************************
     766           26 :    SUBROUTINE diagonalize_A(fm_A, homo, virtual, homo_irred, &
     767           26 :                             unit_nr, diag_est, bse_env, mo_coeff)
     768              : 
     769              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: fm_A
     770              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: homo, virtual, homo_irred
     771              :       INTEGER, INTENT(IN)                                :: unit_nr
     772              :       REAL(KIND=dp), INTENT(IN)                          :: diag_est
     773              :       TYPE(bse_env_type), INTENT(IN)                     :: bse_env
     774              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(IN)         :: mo_coeff
     775              : 
     776              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'diagonalize_A'
     777              : 
     778              :       INTEGER                                            :: diag_info, handle, n_ov_joint, nspins
     779           26 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: Exc_ens
     780              :       TYPE(cp_fm_type)                                   :: fm_eigvec
     781              : 
     782           26 :       CALL timeset(routineN, handle)
     783              : 
     784           26 :       nspins = SIZE(homo)
     785           62 :       n_ov_joint = SUM(homo*virtual)
     786              : 
     787           26 :       IF (unit_nr > 0) THEN
     788           13 :          WRITE (unit_nr, '(T2,A4,T7,A17,A22,ES6.0,A3)') 'BSE|', 'Diagonalizing A. ', &
     789           26 :             'This will take around ', diag_est, ' s.'
     790              :       END IF
     791              : 
     792           26 :       CALL cp_fm_create(fm_eigvec, fm_A%matrix_struct)
     793              : 
     794           78 :       ALLOCATE (Exc_ens(n_ov_joint))
     795              : 
     796           26 :       CALL choose_eigv_solver(fm_A, fm_eigvec, Exc_ens, diag_info)
     797              : 
     798           26 :       IF (diag_info /= 0) THEN
     799              :          CALL cp_abort(__LOCATION__, &
     800            0 :                        "Diagonalization of A failed in TDA-BSE")
     801              :       END IF
     802              : 
     803           26 :       IF (nspins == 1) THEN
     804              :          CALL postprocess_bse(Exc_ens, fm_eigvec, bse_env, mo_coeff, &
     805           16 :                               homo(1), virtual(1), homo_irred(1), unit_nr, .TRUE.)
     806              :       ELSE
     807              :          CALL bse_open_shell_optical(Exc_ens, fm_eigvec, homo, virtual, homo_irred, &
     808           10 :                                      .TRUE., mo_coeff, bse_env, unit_nr)
     809              :       END IF
     810              : 
     811           26 :       CALL cp_fm_release(fm_eigvec)
     812           26 :       DEALLOCATE (Exc_ens)
     813              : 
     814           26 :       CALL timestop(handle)
     815              : 
     816           78 :    END SUBROUTINE diagonalize_A
     817              : 
     818              : ! **************************************************************************************************
     819              : !> \brief Open-shell (UKS) spin-summed post-processing for the joint spin-block space: joint
     820              : !>   excitation energies, per-spin transition amplitudes, and oscillator strengths. Mirrors
     821              : !>   postprocess_bse but spin-summed; exciton descriptors and NTOs are not yet implemented (CPWARN).
     822              : !> \param Exc_ens joint excitation energies
     823              : !> \param fm_eigvec_X joint X eigenvectors (excitations)
     824              : !> \param homo per-spin reduced/active occupied counts
     825              : !> \param virtual per-spin reduced/active virtual counts
     826              : !> \param homo_irred per-spin full occupied counts (absolute-MO labels; N_e = sum)
     827              : !> \param flag_tda .TRUE. -> TDA (coeff=X), .FALSE. -> ABBA (coeff=X+Y)
     828              : !> \param mo_coeff per-spin MO coefficients
     829              : !> \param bse_env the BSE environment (settings; the AO dipoles, mos)
     830              : !> \param unit_nr ...
     831              : !> \param fm_eigvec_Y joint Y eigenvectors (deexcitations; ABBA only)
     832              : ! **************************************************************************************************
     833           14 :    SUBROUTINE bse_open_shell_optical(Exc_ens, fm_eigvec_X, homo, virtual, homo_irred, &
     834           14 :                                      flag_tda, mo_coeff, bse_env, unit_nr, fm_eigvec_Y)
     835              : 
     836              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: Exc_ens
     837              :       TYPE(cp_fm_type), INTENT(IN)                       :: fm_eigvec_X
     838              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: homo, virtual, homo_irred
     839              :       LOGICAL, INTENT(IN)                                :: flag_tda
     840              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(IN)         :: mo_coeff
     841              :       TYPE(bse_env_type), INTENT(IN)                     :: bse_env
     842              :       INTEGER, INTENT(IN)                                :: unit_nr
     843              :       TYPE(cp_fm_type), INTENT(IN), OPTIONAL             :: fm_eigvec_Y
     844              : 
     845              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'bse_open_shell_optical'
     846              : 
     847              :       CHARACTER(LEN=10)                                  :: info_approximation, multiplet
     848              :       INTEGER                                            :: handle, idir, isp, jdir, n, n_ov_joint, &
     849              :                                                             nspins
     850              :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: n_ov_sp, offsets_sp
     851              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: oscill_str_joint
     852              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: pol_res_joint, trans_mom_joint
     853              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_dip_reord, fm_struct_sp, &
     854              :                                                             fm_struct_tmom
     855              :       TYPE(cp_fm_type)                                   :: fm_dip_reord_sp, fm_eigvec_sp, &
     856              :                                                             fm_trans_coeff
     857           14 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_dip_ab_sp, fm_dip_ai_sp, fm_dip_ij_sp
     858           56 :       TYPE(cp_fm_type), DIMENSION(3)                     :: fm_trans_mom_joint
     859              : 
     860           14 :       CALL timeset(routineN, handle)
     861              : 
     862           14 :       nspins = SIZE(homo)
     863           56 :       ALLOCATE (n_ov_sp(nspins), offsets_sp(nspins))
     864           14 :       CALL get_bse_spin_block_layout(homo, virtual, n_ov_sp, offsets_sp, n_ov_joint)
     865              : 
     866              :       ! LEN=10 locals auto-pad short literals with spaces (avoids the L-15 short-literal trap);
     867              :       ! print_excitation_energies prints A6 of these, print_optical_properties prints them as-is.
     868           14 :       multiplet = "UKS"
     869           14 :       IF (flag_tda) THEN
     870           10 :          info_approximation = " -TDA- "
     871              :       ELSE
     872            4 :          info_approximation = "-ABBA-"
     873              :       END IF
     874              : 
     875           14 :       IF (unit_nr > 0) THEN
     876            7 :          WRITE (unit_nr, '(T2,A4,T7,A43)') 'BSE|', 'Joint open-shell BSE excitation energies:'
     877              :       END IF
     878              :       CALL print_excitation_energies(Exc_ens, n_ov_joint, 1, flag_tda, multiplet, &
     879           14 :                                      info_approximation, bse_env, unit_nr)
     880              : 
     881              :       ! Per-spin single-particle transition amplitudes (X via =>, Y via <=).
     882              :       CALL print_transition_amplitudes(fm_eigvec_X, homo, virtual, homo_irred, &
     883           14 :                                        info_approximation, bse_env, unit_nr, fm_eigvec_Y)
     884              : 
     885              :       ! Transition coefficient for the spin-summed moment: X (TDA) or X+Y (ABBA).
     886           14 :       CALL cp_fm_create(fm_trans_coeff, fm_eigvec_X%matrix_struct)
     887           14 :       CALL cp_fm_to_fm(fm_eigvec_X, fm_trans_coeff)
     888           14 :       IF (PRESENT(fm_eigvec_Y)) CALL cp_fm_scale_and_add(1.0_dp, fm_trans_coeff, 1.0_dp, fm_eigvec_Y)
     889              : 
     890              :       ! Spin-summed transition moments: D^n_dir = sum_σ sum_{ia,σ} D^{dir,σ}_{ai} C_{ia,σ,n}
     891              :       ! with C = X (TDA) or X+Y (ABBA); explicit spin sum, factor 1.0.
     892          140 :       ALLOCATE (fm_dip_ai_sp(3), fm_dip_ij_sp(3), fm_dip_ab_sp(3))
     893           70 :       ALLOCATE (oscill_str_joint(n_ov_joint), trans_mom_joint(3, 1, n_ov_joint))
     894           42 :       ALLOCATE (pol_res_joint(3, 3, n_ov_joint))
     895           14 :       trans_mom_joint(:, :, :) = 0.0_dp
     896           14 :       NULLIFY (fm_struct_dip_reord, fm_struct_sp, fm_struct_tmom)
     897              :       CALL cp_fm_struct_create(fm_struct_tmom, fm_trans_coeff%matrix_struct%para_env, &
     898           14 :                                fm_trans_coeff%matrix_struct%context, 1, n_ov_joint)
     899           56 :       DO idir = 1, 3
     900           42 :          CALL cp_fm_create(fm_trans_mom_joint(idir), fm_struct_tmom)
     901           56 :          CALL cp_fm_set_all(fm_trans_mom_joint(idir), 0.0_dp)
     902              :       END DO
     903           42 :       DO isp = 1, nspins
     904              :          CALL get_multipoles_mo(fm_dip_ai_sp, fm_dip_ij_sp, fm_dip_ab_sp, &
     905              :                                 bse_env%matrix_dipole_ao, bse_env%mos, mo_coeff(isp:isp), &
     906              :                                 homo(isp), virtual(isp), fm_trans_coeff%matrix_struct%context, &
     907           28 :                                 ispin=isp)
     908           28 :          NULLIFY (fm_struct_sp, fm_struct_dip_reord)
     909              :          CALL cp_fm_struct_create(fm_struct_sp, fm_trans_coeff%matrix_struct%para_env, &
     910           28 :                                   fm_trans_coeff%matrix_struct%context, n_ov_sp(isp), n_ov_joint)
     911           28 :          CALL cp_fm_create(fm_eigvec_sp, fm_struct_sp)
     912           28 :          CALL cp_fm_set_all(fm_eigvec_sp, 0.0_dp)
     913              :          CALL cp_fm_to_fm_submat(fm_trans_coeff, fm_eigvec_sp, n_ov_sp(isp), n_ov_joint, &
     914           28 :                                  offsets_sp(isp) + 1, 1, 1, 1)
     915              :          CALL cp_fm_struct_create(fm_struct_dip_reord, fm_trans_coeff%matrix_struct%para_env, &
     916           28 :                                   fm_trans_coeff%matrix_struct%context, 1, n_ov_sp(isp))
     917          112 :          DO idir = 1, 3
     918           84 :             CALL cp_fm_create(fm_dip_reord_sp, fm_struct_dip_reord, name="bse_dip_reord")
     919           84 :             CALL cp_fm_set_all(fm_dip_reord_sp, 0.0_dp)
     920           84 :             CALL cp_fm_permute(fm_dip_reord_sp, fm_dip_ai_sp(idir), 1.0_dp, 1, 1, 1, virtual(isp), [2, 4, 3, 1])
     921              :             CALL parallel_gemm('N', 'N', 1, n_ov_joint, n_ov_sp(isp), 1.0_dp, &
     922           84 :                                fm_dip_reord_sp, fm_eigvec_sp, 1.0_dp, fm_trans_mom_joint(idir))
     923           84 :             CALL cp_fm_release(fm_dip_reord_sp)
     924           84 :             CALL cp_fm_release(fm_dip_ai_sp(idir))
     925           84 :             CALL cp_fm_release(fm_dip_ij_sp(idir))
     926          196 :             CALL cp_fm_release(fm_dip_ab_sp(idir))
     927              :          END DO
     928           28 :          CALL cp_fm_release(fm_eigvec_sp)
     929           28 :          CALL cp_fm_struct_release(fm_struct_sp)
     930           28 :          NULLIFY (fm_struct_sp)
     931           28 :          CALL cp_fm_struct_release(fm_struct_dip_reord)
     932           70 :          NULLIFY (fm_struct_dip_reord)
     933              :       END DO
     934           56 :       DO idir = 1, 3
     935           42 :          CALL cp_fm_get_submatrix(fm_trans_mom_joint(idir), trans_mom_joint(idir, :, :))
     936           56 :          CALL cp_fm_release(fm_trans_mom_joint(idir))
     937              :       END DO
     938           14 :       CALL cp_fm_struct_release(fm_struct_tmom)
     939         5886 :       DO n = 1, n_ov_joint
     940        23488 :          DO idir = 1, 3
     941        76336 :             DO jdir = 1, 3
     942              :                pol_res_joint(idir, jdir, n) = 2.0_dp*Exc_ens(n)*trans_mom_joint(idir, 1, n) &
     943        70464 :                                               *trans_mom_joint(jdir, 1, n)
     944              :             END DO
     945              :          END DO
     946        23502 :          oscill_str_joint(n) = 2.0_dp/3.0_dp*Exc_ens(n)*SUM(ABS(trans_mom_joint(:, 1, n))**2)
     947              :       END DO
     948              :       CALL print_optical_properties(Exc_ens, oscill_str_joint, trans_mom_joint, pol_res_joint, &
     949              :                                     n_ov_joint, 1, SUM(homo_irred), flag_tda, info_approximation, &
     950           42 :                                     bse_env, unit_nr, open_shell=.TRUE.)
     951              :       ! Open-shell post-processing is partial: energies, amplitudes, spin-summed oscillator strengths.
     952              :       CALL cp_warn(__LOCATION__, &
     953              :                    "Open-shell (UKS) BSE: exciton descriptors and NTO analysis are not yet "// &
     954           14 :                    "implemented and have been skipped.")
     955           14 :       CALL cp_fm_release(fm_trans_coeff)
     956           14 :       DEALLOCATE (fm_dip_ai_sp, fm_dip_ij_sp, fm_dip_ab_sp)
     957           14 :       DEALLOCATE (n_ov_sp, offsets_sp, oscill_str_joint, trans_mom_joint, pol_res_joint)
     958              : 
     959           14 :       CALL timestop(handle)
     960              : 
     961           56 :    END SUBROUTINE bse_open_shell_optical
     962              : 
     963              : ! **************************************************************************************************
     964              : !> \brief Prints the success message (incl. energies) for full diag of BSE (TDA/full ABBA via flag)
     965              : !> \param Exc_ens ...
     966              : !> \param fm_eigvec_X ...
     967              : !> \param bse_env the BSE environment (settings; the AO multipoles and their reference point, mos, subsys)
     968              : !> \param mo_coeff ...
     969              : !> \param homo ...
     970              : !> \param virtual ...
     971              : !> \param homo_irred ...
     972              : !> \param unit_nr ...
     973              : !> \param flag_TDA ...
     974              : !> \param fm_eigvec_Y ...
     975              : ! **************************************************************************************************
     976           48 :    SUBROUTINE postprocess_bse(Exc_ens, fm_eigvec_X, bse_env, mo_coeff, &
     977              :                               homo, virtual, homo_irred, unit_nr, &
     978              :                               flag_TDA, fm_eigvec_Y)
     979              : 
     980              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: Exc_ens
     981              :       TYPE(cp_fm_type), INTENT(IN)                       :: fm_eigvec_X
     982              :       TYPE(bse_env_type), INTENT(IN)                     :: bse_env
     983              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(IN)         :: mo_coeff
     984              :       INTEGER                                            :: homo, virtual, homo_irred, unit_nr
     985              :       LOGICAL, OPTIONAL                                  :: flag_TDA
     986              :       TYPE(cp_fm_type), INTENT(IN), OPTIONAL             :: fm_eigvec_Y
     987              : 
     988              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'postprocess_bse'
     989              : 
     990              :       CHARACTER(LEN=10)                                  :: info_approximation, multiplet
     991              :       INTEGER                                            :: handle, i_exc, idir, n_moments_di, &
     992              :                                                             n_moments_quad
     993           48 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: oscill_str, ref_point_multipole
     994           48 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: polarizability_residues, trans_mom_bse
     995              :       TYPE(cp_fm_type)                                   :: fm_X_ia, fm_Y_ia
     996           48 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: fm_dipole_ab_trunc, fm_dipole_ai_trunc, &
     997           48 :          fm_dipole_ij_trunc, fm_quadpole_ab_trunc, fm_quadpole_ai_trunc, fm_quadpole_ij_trunc
     998              :       TYPE(exciton_descr_type), ALLOCATABLE, &
     999           48 :          DIMENSION(:)                                    :: exc_descr
    1000           48 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1001              : 
    1002           48 :       CALL timeset(routineN, handle)
    1003              : 
    1004              :       !Prepare variables for printing
    1005           48 :       IF (bse_env%bse_spin_config == 0) THEN
    1006           48 :          multiplet = "Singlet"
    1007              :       ELSE
    1008            0 :          multiplet = "Triplet"
    1009              :       END IF
    1010           48 :       IF (.NOT. PRESENT(flag_TDA)) THEN
    1011            0 :          flag_TDA = .FALSE.
    1012              :       END IF
    1013           48 :       IF (flag_TDA) THEN
    1014           20 :          info_approximation = " -TDA- "
    1015              :       ELSE
    1016           28 :          info_approximation = "-ABBA-"
    1017              :       END IF
    1018              : 
    1019           48 :       n_moments_di = 3
    1020           48 :       n_moments_quad = 9
    1021              :       ! Compute BSE dipoles and oscillator strengths - Keep in memory for later usage
    1022              :       ! Need dipoles also for spatial expectation values, which are well-defined also for triplets
    1023          192 :       ALLOCATE (fm_dipole_ij_trunc(n_moments_di))
    1024          192 :       ALLOCATE (fm_dipole_ab_trunc(n_moments_di))
    1025          192 :       ALLOCATE (fm_dipole_ai_trunc(n_moments_di))
    1026           48 :       ALLOCATE (ref_point_multipole(3))
    1027          192 :       ref_point_multipole(:) = bse_env%multipole_ref_point
    1028              :       ! Obtain dipoles in MO basis
    1029              :       CALL get_multipoles_mo(fm_dipole_ai_trunc, fm_dipole_ij_trunc, fm_dipole_ab_trunc, &
    1030              :                              bse_env%matrix_dipole_ao, bse_env%mos, mo_coeff, &
    1031           48 :                              homo, virtual, fm_eigvec_X%matrix_struct%context)
    1032              :       ! Compute exciton descriptors from these multipoles
    1033           48 :       IF (bse_env%num_print_exc_descr > 0) THEN
    1034              :          ! Obtain quadrupoles in MO basis
    1035           40 :          ALLOCATE (fm_quadpole_ij_trunc(n_moments_quad))
    1036           40 :          ALLOCATE (fm_quadpole_ab_trunc(n_moments_quad))
    1037           40 :          ALLOCATE (fm_quadpole_ai_trunc(n_moments_quad))
    1038              :          ! the preparer builds the AO quadrupoles whenever NUM_PRINT_EXC_DESCR is nonzero
    1039            4 :          CPASSERT(ASSOCIATED(bse_env%matrix_quadpole_ao))
    1040              :          CALL get_multipoles_mo(fm_quadpole_ai_trunc, fm_quadpole_ij_trunc, fm_quadpole_ab_trunc, &
    1041              :                                 bse_env%matrix_quadpole_ao, bse_env%mos, mo_coeff, &
    1042            4 :                                 homo, virtual, fm_eigvec_X%matrix_struct%context)
    1043              :          ! Iterate over excitation index outside of routine to make it compatible with tddft module
    1044          424 :          ALLOCATE (exc_descr(bse_env%num_print_exc_descr))
    1045          104 :          DO i_exc = 1, bse_env%num_print_exc_descr
    1046          100 :             CALL reshuffle_eigvec(fm_eigvec_X, fm_X_ia, homo, virtual, i_exc, .FALSE.)
    1047          100 :             IF (.NOT. flag_TDA) THEN
    1048           50 :                CALL reshuffle_eigvec(fm_eigvec_Y, fm_Y_ia, homo, virtual, i_exc, .FALSE.)
    1049              : 
    1050              :                CALL get_exciton_descriptors(exc_descr, fm_X_ia, &
    1051              :                                             fm_quadpole_ij_trunc, fm_quadpole_ab_trunc, &
    1052              :                                             fm_quadpole_ai_trunc, &
    1053              :                                             i_exc, homo, virtual, &
    1054           50 :                                             fm_Y_ia)
    1055              :             ELSE
    1056              :                CALL get_exciton_descriptors(exc_descr, fm_X_ia, &
    1057              :                                             fm_quadpole_ij_trunc, fm_quadpole_ab_trunc, &
    1058              :                                             fm_quadpole_ai_trunc, &
    1059           50 :                                             i_exc, homo, virtual)
    1060              :             END IF
    1061          100 :             CALL cp_fm_release(fm_X_ia)
    1062          104 :             IF (.NOT. flag_TDA) THEN
    1063           50 :                CALL cp_fm_release(fm_Y_ia)
    1064              :             END IF
    1065              :          END DO
    1066              :       END IF
    1067              : 
    1068           48 :       IF (bse_env%bse_spin_config == 0) THEN
    1069              :          CALL get_oscillator_strengths(fm_eigvec_X, Exc_ens, fm_dipole_ai_trunc, &
    1070              :                                        trans_mom_bse, oscill_str, polarizability_residues, &
    1071           48 :                                        homo, virtual, fm_eigvec_Y)
    1072              :       END IF
    1073              : 
    1074              :       ! the iterative driver prints the header before its solver runs
    1075           48 :       IF (bse_env%bse_diag_method /= bse_iterdiag) THEN
    1076           38 :          CALL print_output_header(homo, virtual, homo_irred, flag_TDA, bse_env, unit_nr)
    1077              :       END IF
    1078              : 
    1079              :       ! Prints excitation energies up to user-specified number
    1080              :       CALL print_excitation_energies(Exc_ens, homo, virtual, flag_TDA, multiplet, &
    1081           48 :                                      info_approximation, bse_env, unit_nr)
    1082              : 
    1083              :       ! Print single particle transition amplitudes, i.e. components of eigenvectors X and Y
    1084              :       CALL print_transition_amplitudes(fm_eigvec_X, [homo], [virtual], [homo_irred], &
    1085          192 :                                        info_approximation, bse_env, unit_nr, fm_eigvec_Y)
    1086              : 
    1087              :       ! Prints optical properties, if state is a singlet
    1088              :       CALL print_optical_properties(Exc_ens, oscill_str, trans_mom_bse, polarizability_residues, &
    1089              :                                     homo, virtual, homo_irred, flag_TDA, &
    1090           48 :                                     info_approximation, bse_env, unit_nr)
    1091              :       ! Print exciton descriptors if keyword is invoked
    1092           48 :       IF (bse_env%num_print_exc_descr > 0) THEN
    1093            4 :          CALL qs_subsys_get(bse_env%subsys, particle_set=particle_set)
    1094              :          CALL print_exciton_descriptors(exc_descr, ref_point_multipole, unit_nr, &
    1095              :                                         bse_env%num_print_exc_descr, bse_env%bse_debug_print, &
    1096              :                                         bse_env%print_directional_exc_descr, &
    1097              :                                         bse_env%print_directional_crosscorrelation, &
    1098            4 :                                         'BSE|', particle_set)
    1099              :       END IF
    1100              : 
    1101              :       ! Compute and print excitation wavefunctions
    1102           48 :       IF (bse_env%do_nto_analysis) THEN
    1103            4 :          IF (unit_nr > 0) THEN
    1104            2 :             WRITE (unit_nr, '(T2,A4)') 'BSE|'
    1105              :             WRITE (unit_nr, '(T2,A4,T7,A47)') &
    1106            2 :                'BSE|', "Calculating Natural Transition Orbitals (NTOs)."
    1107            2 :             WRITE (unit_nr, '(T2,A4)') 'BSE|'
    1108              :          END IF
    1109              :          CALL calculate_NTOs(fm_eigvec_X, fm_eigvec_Y, &
    1110              :                              mo_coeff, homo, virtual, &
    1111              :                              info_approximation, &
    1112              :                              oscill_str, &
    1113            4 :                              bse_env, unit_nr)
    1114              :       END IF
    1115              : 
    1116          192 :       DO idir = 1, n_moments_di
    1117          144 :          CALL cp_fm_release(fm_dipole_ai_trunc(idir))
    1118          144 :          CALL cp_fm_release(fm_dipole_ij_trunc(idir))
    1119          192 :          CALL cp_fm_release(fm_dipole_ab_trunc(idir))
    1120              :       END DO
    1121           48 :       IF (bse_env%num_print_exc_descr > 0) THEN
    1122           40 :          DO idir = 1, n_moments_quad
    1123           36 :             CALL cp_fm_release(fm_quadpole_ai_trunc(idir))
    1124           36 :             CALL cp_fm_release(fm_quadpole_ij_trunc(idir))
    1125           40 :             CALL cp_fm_release(fm_quadpole_ab_trunc(idir))
    1126              :          END DO
    1127            4 :          DEALLOCATE (fm_quadpole_ai_trunc, fm_quadpole_ij_trunc, fm_quadpole_ab_trunc)
    1128            4 :          DEALLOCATE (exc_descr)
    1129              :       END IF
    1130           48 :       DEALLOCATE (fm_dipole_ai_trunc, fm_dipole_ij_trunc, fm_dipole_ab_trunc)
    1131           48 :       DEALLOCATE (ref_point_multipole)
    1132           48 :       IF (bse_env%bse_spin_config == 0) THEN
    1133           48 :          DEALLOCATE (oscill_str, trans_mom_bse, polarizability_residues)
    1134              :       END IF
    1135           48 :       IF (unit_nr > 0) THEN
    1136           24 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
    1137           24 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
    1138              :       END IF
    1139              : 
    1140           48 :       CALL timestop(handle)
    1141              : 
    1142           96 :    END SUBROUTINE postprocess_bse
    1143              : 
    1144              : END MODULE bse_full_diag
        

Generated by: LCOV version 2.0-1