LCOV - code coverage report
Current view: top level - src - bse_properties.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:92574dc) Lines: 95.5 % 403 385
Test Date: 2026-09-24 01:27:39 Functions: 80.0 % 5 4

            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 computing excitonic properties, e.g. exciton diameter, from the BSE
      10              : !> \par History
      11              : !>      10.2024 created [Maximilian Graml]
      12              : ! **************************************************************************************************
      13              : MODULE bse_properties
      14              :    USE bse_util,                        ONLY: fm_general_add_bse,&
      15              :                                               print_bse_nto_cubes,&
      16              :                                               reshuffle_eigvec,&
      17              :                                               trace_exciton_descr
      18              :    USE cp_files,                        ONLY: close_file,&
      19              :                                               open_file
      20              :    USE cp_fm_basic_linalg,              ONLY: cp_fm_scale_and_add,&
      21              :                                               cp_fm_trace
      22              :    USE cp_fm_diag,                      ONLY: cp_fm_svd
      23              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      24              :                                               cp_fm_struct_release,&
      25              :                                               cp_fm_struct_type
      26              :    USE cp_fm_types,                     ONLY: &
      27              :         cp_fm_create, cp_fm_get_info, cp_fm_get_submatrix, cp_fm_release, cp_fm_set_all, &
      28              :         cp_fm_to_fm, cp_fm_to_fm_submat, cp_fm_to_fm_submat_general, cp_fm_type
      29              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      30              :                                               cp_logger_get_default_unit_nr,&
      31              :                                               cp_logger_type
      32              :    USE input_section_types,             ONLY: section_vals_get_subs_vals,&
      33              :                                               section_vals_type,&
      34              :                                               section_vals_val_get
      35              :    USE kinds,                           ONLY: dp
      36              :    USE mathconstants,                   ONLY: pi
      37              :    USE mp2_types,                       ONLY: mp2_type
      38              :    USE parallel_gemm_api,               ONLY: parallel_gemm
      39              :    USE physcon,                         ONLY: c_light_au,&
      40              :                                               evolt
      41              :    USE qs_environment_types,            ONLY: get_qs_env,&
      42              :                                               qs_environment_type
      43              :    USE qs_mo_types,                     ONLY: allocate_mo_set,&
      44              :                                               deallocate_mo_set,&
      45              :                                               init_mo_set,&
      46              :                                               mo_set_type
      47              : #include "./base/base_uses.f90"
      48              : 
      49              :    IMPLICIT NONE
      50              : 
      51              :    PRIVATE
      52              : 
      53              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'bse_properties'
      54              : 
      55              :    PUBLIC :: exciton_descr_type
      56              : 
      57              :    PUBLIC :: get_exciton_descriptors, get_oscillator_strengths, compute_and_print_absorption_spectrum, &
      58              :              calculate_NTOs
      59              : 
      60              : ! TYPE definitions for exciton wavefunction descriptors
      61              : 
      62              :    TYPE exciton_descr_type
      63              :       REAL(KIND=dp), DIMENSION(3)                        :: r_e = 0.0_dp, &
      64              :                                                             r_h = 0.0_dp, &
      65              :                                                             r_e_sq = 0.0_dp, &
      66              :                                                             r_h_sq = 0.0_dp, &
      67              :                                                             r_e_shift = 0.0_dp, &
      68              :                                                             r_h_shift = 0.0_dp, &
      69              :                                                             d_eh_dir = 0.0_dp, &
      70              :                                                             sigma_e_dir = 0.0_dp, &
      71              :                                                             sigma_h_dir = 0.0_dp, &
      72              :                                                             d_exc_dir = 0.0_dp
      73              :       REAL(KIND=dp), DIMENSION(3, 3)                      :: r_e_h = 0.0_dp, &
      74              :                                                              cov_e_h = 0.0_dp, &
      75              :                                                              corr_e_h_matrix = 0.0_dp
      76              :       REAL(KIND=dp)                                      :: sigma_e = 0.0_dp, &
      77              :                                                             sigma_h = 0.0_dp, &
      78              :                                                             cov_e_h_sum = 0.0_dp, &
      79              :                                                             corr_e_h = 0.0_dp, &
      80              :                                                             diff_r_abs = 0.0_dp, &
      81              :                                                             diff_r_sqr = 0.0_dp, &
      82              :                                                             norm_XpY = 0.0_dp
      83              :       LOGICAL                                           :: flag_TDA = .FALSE.
      84              :    END TYPE exciton_descr_type
      85              : 
      86              : CONTAINS
      87              : 
      88              : ! **************************************************************************************************
      89              : !> \brief Compute and return BSE dipoles d_r^n = sqrt(2) sum_ia < ψ_i | r | ψ_a > ( X_ia^n + Y_ia^n )
      90              : !>    and oscillator strengths f^n = 2/3 * Ω^n sum_r∈(x,y,z) ( d_r^n )^2
      91              : !>    Prelim Ref.: Eqs. (23), (24)
      92              : !>    in J. Chem. Phys. 152, 044105 (2020); https://doi.org/10.1063/1.5123290
      93              : !> \param fm_eigvec_X ...
      94              : !> \param Exc_ens ...
      95              : !> \param fm_dipole_ai_trunc ...
      96              : !> \param trans_mom_bse BSE dipole vectors in real space per excitation level
      97              : !> \param oscill_str Oscillator strength per excitation level
      98              : !> \param polarizability_residues Residues of polarizability ("tensorial oscillator strength")
      99              : !>          per excitation level
     100              : !> \param mp2_env ...
     101              : !> \param homo_red ...
     102              : !> \param virtual_red ...
     103              : !> \param unit_nr ...
     104              : !> \param fm_eigvec_Y ...
     105              : ! **************************************************************************************************
     106           38 :    SUBROUTINE get_oscillator_strengths(fm_eigvec_X, Exc_ens, fm_dipole_ai_trunc, &
     107              :                                        trans_mom_bse, oscill_str, polarizability_residues, &
     108              :                                        mp2_env, homo_red, virtual_red, unit_nr, &
     109              :                                        fm_eigvec_Y)
     110              : 
     111              :       TYPE(cp_fm_type), INTENT(IN)                       :: fm_eigvec_X
     112              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), &
     113              :          INTENT(IN)                                      :: Exc_ens
     114              :       TYPE(cp_fm_type), DIMENSION(3)                     :: fm_dipole_ai_trunc
     115              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
     116              :          INTENT(OUT)                                     :: trans_mom_bse
     117              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), &
     118              :          INTENT(OUT)                                     :: oscill_str
     119              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
     120              :          INTENT(OUT)                                     :: polarizability_residues
     121              :       TYPE(mp2_type), INTENT(IN)                         :: mp2_env
     122              :       INTEGER, INTENT(IN)                                :: homo_red, virtual_red, unit_nr
     123              :       TYPE(cp_fm_type), INTENT(IN), OPTIONAL             :: fm_eigvec_Y
     124              : 
     125              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_oscillator_strengths'
     126              : 
     127              :       INTEGER                                            :: handle, idir, jdir, n, n_exc
     128              :       TYPE(cp_fm_struct_type), POINTER :: fm_struct_dipole_MO_trunc_reordered, &
     129              :          fm_struct_trans_mom_bse
     130              :       TYPE(cp_fm_type)                                   :: fm_eigvec_XYsum
     131          152 :       TYPE(cp_fm_type), DIMENSION(3)                     :: fm_dipole_MO_trunc_reordered, &
     132          304 :                                                             fm_dipole_per_dir, fm_trans_mom_bse
     133              : 
     134           38 :       CALL timeset(routineN, handle)
     135              : 
     136              :       ! an iterative solver provides fewer excitations than transitions
     137           38 :       CALL cp_fm_get_info(fm_eigvec_X, ncol_global=n_exc)
     138              : 
     139              :       CALL cp_fm_struct_create(fm_struct_dipole_MO_trunc_reordered, fm_eigvec_X%matrix_struct%para_env, &
     140           38 :                                fm_eigvec_X%matrix_struct%context, 1, homo_red*virtual_red)
     141              :       CALL cp_fm_struct_create(fm_struct_trans_mom_bse, fm_eigvec_X%matrix_struct%para_env, &
     142           38 :                                fm_eigvec_X%matrix_struct%context, 1, n_exc)
     143              : 
     144              :       ! Include excitonic amplitudes in dipoles, i.e. obtain "BSE dipoles":
     145              :       ! \vec{D}_n = sqrt(2) * sum_{i,a} \vec{D}_ai (X_{ai}^{(n)} + Y_{ai}^{(n)})
     146              : 
     147              :       ! Reorder dipoles in order to execute the sum over i and a by parallel gemm
     148          152 :       DO idir = 1, 3
     149              :          CALL cp_fm_create(fm_dipole_MO_trunc_reordered(idir), matrix_struct=fm_struct_dipole_MO_trunc_reordered, &
     150          114 :                            name="dipoles_mo_reordered")
     151          114 :          CALL cp_fm_set_all(fm_dipole_MO_trunc_reordered(idir), 0.0_dp)
     152              :          CALL fm_general_add_bse(fm_dipole_MO_trunc_reordered(idir), fm_dipole_ai_trunc(idir), 1.0_dp, &
     153              :                                  1, 1, &
     154              :                                  1, virtual_red, &
     155          114 :                                  unit_nr, [2, 4, 3, 1], mp2_env)
     156          152 :          CALL cp_fm_release(fm_dipole_per_dir(idir))
     157              :       END DO
     158              : 
     159          152 :       DO idir = 1, 3
     160              :          CALL cp_fm_create(fm_trans_mom_bse(idir), matrix_struct=fm_struct_trans_mom_bse, &
     161          114 :                            name="excitonic_dipoles")
     162          152 :          CALL cp_fm_set_all(fm_trans_mom_bse(idir), 0.0_dp)
     163              :       END DO
     164              : 
     165              :       ! If TDA is invoked, Y is not present as it is simply 0
     166           38 :       CALL cp_fm_create(fm_eigvec_XYsum, matrix_struct=fm_eigvec_X%matrix_struct, name="excit_amplitude_sum")
     167           38 :       CALL cp_fm_set_all(fm_eigvec_XYsum, 0.0_dp)
     168           38 :       CALL cp_fm_to_fm(fm_eigvec_X, fm_eigvec_XYsum)
     169           38 :       IF (PRESENT(fm_eigvec_Y)) THEN
     170           22 :          CALL cp_fm_scale_and_add(1.0_dp, fm_eigvec_XYsum, 1.0_dp, fm_eigvec_Y)
     171              :       END IF
     172          152 :       DO idir = 1, 3
     173              :          CALL parallel_gemm('N', 'N', 1, n_exc, homo_red*virtual_red, SQRT(2.0_dp), &
     174          152 :                             fm_dipole_MO_trunc_reordered(idir), fm_eigvec_XYsum, 0.0_dp, fm_trans_mom_bse(idir))
     175              :       END DO
     176              : 
     177              :       ! Get oscillator strengths themselves
     178          114 :       ALLOCATE (oscill_str(n_exc))
     179              :       ! trans_mom_bse needs to be a 2D array per direction idir, such that cp_fm_get_submatrix can directly
     180              :       !  write to it
     181          114 :       ALLOCATE (trans_mom_bse(3, 1, n_exc))
     182          114 :       ALLOCATE (polarizability_residues(3, 3, n_exc))
     183           38 :       trans_mom_bse(:, :, :) = 0.0_dp
     184              : 
     185              :       ! Sum over all directions
     186          152 :       DO idir = 1, 3
     187          152 :          CALL cp_fm_get_submatrix(fm_trans_mom_bse(idir), trans_mom_bse(idir, :, :))
     188              :       END DO
     189              : 
     190         1568 :       DO n = 1, n_exc
     191         6120 :          DO idir = 1, 3
     192        19890 :             DO jdir = 1, 3
     193        18360 :                polarizability_residues(idir, jdir, n) = 2.0_dp*Exc_ens(n)*trans_mom_bse(idir, 1, n)*trans_mom_bse(jdir, 1, n)
     194              :             END DO
     195              :          END DO
     196         6158 :          oscill_str(n) = 2.0_dp/3.0_dp*Exc_ens(n)*SUM(ABS(trans_mom_bse(:, 1, n))**2)
     197              :       END DO
     198              : 
     199           38 :       CALL cp_fm_struct_release(fm_struct_dipole_MO_trunc_reordered)
     200           38 :       CALL cp_fm_struct_release(fm_struct_trans_mom_bse)
     201          152 :       DO idir = 1, 3
     202          114 :          CALL cp_fm_release(fm_dipole_MO_trunc_reordered(idir))
     203          114 :          CALL cp_fm_release(fm_trans_mom_bse(idir))
     204          152 :          CALL cp_fm_release(fm_dipole_ai_trunc(idir))
     205              :       END DO
     206           38 :       CALL cp_fm_release(fm_eigvec_XYsum)
     207              : 
     208           38 :       CALL timestop(handle)
     209              : 
     210          114 :    END SUBROUTINE get_oscillator_strengths
     211              : 
     212              : ! **************************************************************************************************
     213              : !> \brief Computes and returns absorption spectrum for the frequency range and broadening
     214              : !>    provided by the user.
     215              : !>    Prelim Ref.: C. Ullrich, Time-Dependent Density-Functional Theory: Concepts and Applications
     216              : !>    (Oxford University Press, Oxford, 2012), Eq. 7.51
     217              : !> \param oscill_str ...
     218              : !> \param polarizability_residues ...
     219              : !> \param Exc_ens ...
     220              : !> \param info_approximation ...
     221              : !> \param unit_nr ...
     222              : !> \param mp2_env ...
     223              : ! **************************************************************************************************
     224            2 :    SUBROUTINE compute_and_print_absorption_spectrum(oscill_str, polarizability_residues, Exc_ens, &
     225              :                                                     info_approximation, unit_nr, mp2_env)
     226              : 
     227              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), &
     228              :          INTENT(IN)                                      :: oscill_str
     229              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
     230              :          INTENT(IN)                                      :: polarizability_residues
     231              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), &
     232              :          INTENT(IN)                                      :: Exc_ens
     233              :       CHARACTER(LEN=10)                                  :: info_approximation
     234              :       INTEGER, INTENT(IN)                                :: unit_nr
     235              :       TYPE(mp2_type), INTENT(INOUT)                      :: mp2_env
     236              : 
     237              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_and_print_absorption_spectrum'
     238              : 
     239              :       CHARACTER(LEN=10)                                  :: eta_str, width_eta_format_str
     240              :       CHARACTER(LEN=40)                                  :: file_name_crosssection, &
     241              :                                                             file_name_spectrum
     242              :       INTEGER                                            :: handle, i, idir, j, jdir, k, num_steps, &
     243              :                                                             unit_nr_file, width_eta
     244              :       REAL(KIND=dp)                                      :: eta, freq_end, freq_start, freq_step, &
     245              :                                                             omega
     246            2 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: abs_cross_section, abs_spectrum
     247            2 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: eta_list
     248              :       TYPE(cp_logger_type), POINTER                      :: logger
     249              : 
     250            2 :       CALL timeset(routineN, handle)
     251              : 
     252            2 :       freq_step = mp2_env%bse%bse_spectrum_freq_step_size
     253            2 :       freq_start = mp2_env%bse%bse_spectrum_freq_start
     254            2 :       freq_end = mp2_env%bse%bse_spectrum_freq_end
     255            2 :       eta_list => mp2_env%bse%bse_eta_spectrum_list
     256              : 
     257              :       ! Calculate number of steps to fit given frequency range
     258            2 :       num_steps = NINT((freq_end - freq_start)/freq_step) + 1
     259              : 
     260            4 :       DO k = 1, SIZE(eta_list)
     261            2 :          eta = eta_list(k)
     262              : 
     263              :          ! Some magic to get a nice formatting of the eta value in filenames
     264            2 :          width_eta = MAX(1, INT(LOG10(eta)) + 1) + 4
     265            2 :          WRITE (width_eta_format_str, "(A2,I0,A3)") '(F', width_eta, '.3)'
     266            2 :          WRITE (eta_str, width_eta_format_str) eta*evolt
     267              :          ! Filename itself
     268            2 :          file_name_spectrum = 'BSE'//TRIM(ADJUSTL(info_approximation))//'eta='//TRIM(eta_str)//'.spectrum'
     269            2 :          file_name_crosssection = 'BSE'//TRIM(ADJUSTL(info_approximation))//'eta='//TRIM(eta_str)//'.crosssection'
     270              : 
     271              :          ! First column is frequency in eV, second column is imaginary part of the trace of the polarizability
     272              :          ! The following 9 columns are the entries of the polarizability tensor
     273            6 :          ALLOCATE (abs_spectrum(num_steps, 11))
     274            2 :          abs_spectrum(:, :) = 0.0_dp
     275              :          ! Also calculate and print the photoabsorption cross section tensor
     276              :          ! σ_{µ µ'}(ω) = 4πω Im{α_{µ µ'}(ω)} / c
     277            4 :          ALLOCATE (abs_cross_section(num_steps, 11))
     278            2 :          abs_cross_section(:, :) = 0.0_dp
     279              : 
     280              :          ! Calculate the imaginary part of the mean dipole polarizability α_{avg}(ω)
     281              :          ! which is given by (cf. C. Ullrichs Book on TDDFT, Eq. 7.51)
     282              :          ! We introduce an additional - due to his convention for charge vs particle density, see also:
     283              :          ! Computer Physics Communications, 208:149–161, November 2016
     284              :          ! https://doi.org/10.1016/j.cpc.2016.06.019
     285              :          ! α_{avg}(ω) = - \sum_{n=1}^{N_exc} \frac{f_n}{(ω+iη)² - (Ω^n)²}
     286              :          ! and then the imaginary part is (in the limit η -> 0)
     287              :          ! Im[α_{avg}(ω)] = - \sum_{n=1}^{N_exc} f_n * η / ((ω - Ω^n)² + η²)
     288              :          ! where f_n are the oscillator strengths and E_exc the excitation energies
     289              :          ! For the full polarizability tensor, we have
     290              :          ! α_{µ µ'}(ω) = - \sum_n [2 Ω^n d^n_µ d^n_µ'] / [(ω+iη)^2- (Ω^n)^2]
     291              :          !             = - \sum_n "polarizability_residues" / [(ω+iη)^2- (Ω^n)^2]
     292         2004 :          DO i = 1, num_steps
     293         2002 :             omega = freq_start + (i - 1)*freq_step
     294         2002 :             abs_spectrum(i, 1) = omega
     295        98100 :             DO j = 1, SIZE(oscill_str)
     296              :                abs_spectrum(i, 2) = abs_spectrum(i, 2) - oscill_str(j)* &
     297        96096 :                                     AIMAG(1/((omega + CMPLX(0.0, eta, kind=dp))**2 - Exc_ens(j)**2))
     298       386386 :                DO idir = 1, 3
     299      1249248 :                   DO jdir = 1, 3
     300              :                      ! Factor 2 from formula for tensor is already in the polarizability_residues
     301              :                      !  to follow the same convention as the oscillator strengths
     302              :                      abs_spectrum(i, 2 + (idir - 1)*3 + jdir) = abs_spectrum(i, 2 + (idir - 1)*3 + jdir) &
     303              :                                                                 - polarizability_residues(idir, jdir, j)* &
     304      1153152 :                                                                 AIMAG(1/((omega + CMPLX(0.0, eta, kind=dp))**2 - Exc_ens(j)**2))
     305              :                   END DO
     306              :                END DO
     307              :             END DO
     308              :          END DO
     309              : 
     310              :          ! Extract cross section σ from polarizability tensor
     311         2004 :          DO i = 1, num_steps
     312         2002 :             omega = abs_spectrum(i, 1)
     313         2002 :             abs_cross_section(i, 1) = omega
     314        22024 :             abs_cross_section(i, 2:) = 4.0_dp*pi*abs_spectrum(i, 2:)*omega/c_light_au
     315              :          END DO
     316              : 
     317              :          !For debug runs: Export an entry of the two tensors to allow regtests on spectra
     318            2 :          IF (mp2_env%bse%bse_debug_print) THEN
     319            2 :             IF (unit_nr > 0) THEN
     320            1 :                WRITE (unit_nr, '(T2,A10,T13,A,T65,F16.4)') 'BSE|DEBUG|', &
     321            1 :                   'Averaged dynamical dipole polarizability at 8.2 eV:', &
     322            2 :                   abs_spectrum(83, 2)
     323            1 :                WRITE (unit_nr, '(T2,A10,T13,A,T65,F16.4)') 'BSE|DEBUG|', &
     324            1 :                   'Averaged photoabsorption cross section at 8.2 eV:', &
     325            2 :                   abs_cross_section(83, 2)
     326              :             END IF
     327              :          END IF
     328              : 
     329              :          ! Print it to file
     330            2 :          logger => cp_get_default_logger()
     331            2 :          IF (logger%para_env%is_source()) THEN
     332            2 :             unit_nr_file = cp_logger_get_default_unit_nr()
     333              :          ELSE
     334            0 :             unit_nr_file = -1
     335              :          END IF
     336              : 
     337            2 :          IF (unit_nr_file > 0) THEN
     338              :             CALL open_file(file_name_crosssection, unit_number=unit_nr_file, &
     339            2 :                            file_status="UNKNOWN", file_action="WRITE")
     340              :             WRITE (unit_nr_file, '(A,A6)') "# Photoabsorption cross section  σ_{µ µ'}(ω) =  -4πω/c * Im[ \sum_n "// &
     341            2 :                "[2 Ω^n d_µ^n d_µ'^n] / [(ω+iη)²- (Ω^n)²] ] from Bethe Salpeter equation for method ", &
     342            4 :                TRIM(ADJUSTL(info_approximation))
     343            2 :             WRITE (unit_nr_file, '(A20,1X,10(2X,A20,1X))') "#     Frequency (eV)", "σ_{avg}(ω)", "σ_xx(ω)", &
     344            2 :                "σ_xy(ω)", "σ_xz(ω)", "σ_yx(ω)", "σ_yy(ω)", "σ_yz(ω)", "σ_zx(ω)", &
     345            4 :                "σ_zy(ω)", "σ_zz(ω)"
     346         2004 :             DO i = 1, num_steps
     347         2004 :                WRITE (unit_nr_file, '(11(F20.8,1X))') abs_cross_section(i, 1)*evolt, abs_cross_section(i, 2:11)
     348              :             END DO
     349            2 :             CALL close_file(unit_nr_file)
     350              :          END IF
     351            2 :          DEALLOCATE (abs_cross_section)
     352              : 
     353            2 :          IF (unit_nr_file > 0) THEN
     354              :             CALL open_file(file_name_spectrum, unit_number=unit_nr_file, &
     355            2 :                            file_status="UNKNOWN", file_action="WRITE")
     356              :             WRITE (unit_nr_file, '(A,A6)') "# Imaginary part of polarizability α_{µ µ'}(ω) = -\sum_n "// &
     357            2 :                "[2 Ω^n d_µ^n d_µ'^n] / [(ω+iη)²- (Ω^n)²] from Bethe Salpeter equation for method ", &
     358            4 :                TRIM(ADJUSTL(info_approximation))
     359            2 :             WRITE (unit_nr_file, '(A20,1X,10(2X,A20,1X))') "#     Frequency (eV)", "Im{α_{avg}(ω)}", "Im{α_xx(ω)}", &
     360            2 :                "Im{α_xy(ω)}", "Im{α_xz(ω)}", "Im{α_yx(ω)}", "Im{α_yy(ω)}", "Im{α_yz(ω)}", "Im{α_zx(ω)}", &
     361            4 :                "Im{α_zy(ω)}", "Im{α_zz(ω)}"
     362         2004 :             DO i = 1, num_steps
     363         2004 :                WRITE (unit_nr_file, '(11(F20.8,1X))') abs_spectrum(i, 1)*evolt, abs_spectrum(i, 2:11)
     364              :             END DO
     365            2 :             CALL close_file(unit_nr_file)
     366              :          END IF
     367            4 :          DEALLOCATE (abs_spectrum)
     368              :       END DO
     369              : 
     370            2 :       IF (unit_nr > 0) THEN
     371            1 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     372              :          WRITE (unit_nr, '(T2,A4,T7,A,A)') &
     373            1 :             'BSE|', "Printed optical absorption spectrum to local files, e.g. "
     374              :          WRITE (unit_nr, '(T2,A4,T7,A)') &
     375            1 :             'BSE|', file_name_spectrum
     376              :          WRITE (unit_nr, '(T2,A4,T7,A,A)') &
     377            1 :             'BSE|', "as well as photoabsorption cross section to, e.g. "
     378              :          WRITE (unit_nr, '(T2,A4,T7,A)') &
     379            1 :             'BSE|', file_name_crosssection
     380              :          WRITE (unit_nr, '(T2,A4,T7,A52)') &
     381            1 :             'BSE|', "using the Eq. (7.51) from C. Ullrichs Book on TDDFT:"
     382            1 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     383              :          WRITE (unit_nr, '(T2,A4,T10,A75)') &
     384            1 :             'BSE|', "Im{α_{avg}(ω)} = -Im{\sum_{n=1}^{N_exc} \frac{f_n}{(ω+iη)² - (Ω^n)²}}"
     385            1 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     386              :          WRITE (unit_nr, '(T2,A4,T7,A)') &
     387            1 :             'BSE|', "or for the full polarizability tensor:"
     388              :          WRITE (unit_nr, '(T2,A4,T10,A)') &
     389            1 :             'BSE|', "α_{µ µ'}(ω) =  -\sum_n [2 Ω^n d_µ^n d_µ'^n] / [(ω+iη)²- (Ω^n)²]"
     390            1 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     391              :          WRITE (unit_nr, '(T2,A4,T7,A)') &
     392            1 :             'BSE|', "as well as Eq. (7.48):"
     393              :          WRITE (unit_nr, '(T2,A4,T10,A)') &
     394            1 :             'BSE|', "σ_{µ µ'}(ω) = 4πω Im{α_{µ µ'}(ω)} / c"
     395            1 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     396              :          WRITE (unit_nr, '(T2,A4,T7,A)') &
     397            1 :             'BSE|', "with transition moments d_µ^n, oscillator strengths f_n,"
     398              :          WRITE (unit_nr, '(T2,A4,T7,A)') &
     399            1 :             'BSE|', "excitation energies Ω^n and the speed of light c."
     400            1 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     401              :          WRITE (unit_nr, '(T2,A4,T7,A)') &
     402            1 :             'BSE|', "Please note that we adopt an additional minus sign for both quantities,"
     403              :          WRITE (unit_nr, '(T2,A4,T7,A)') &
     404            1 :             'BSE|', "due to the convention for charge vs particle density as done in MolGW:"
     405              :          WRITE (unit_nr, '(T2,A4,T7,A)') &
     406            1 :             'BSE|', "https://doi.org/10.1016/j.cpc.2016.06.019."
     407            1 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     408              :       END IF
     409              : 
     410            2 :       CALL timestop(handle)
     411              : 
     412            4 :    END SUBROUTINE compute_and_print_absorption_spectrum
     413              : 
     414              : ! **************************************************************************************************
     415              : !> \brief ...
     416              : !> \param fm_X ...
     417              : !> \param fm_Y ...
     418              : !> \param mo_coeff ...
     419              : !> \param homo ...
     420              : !> \param virtual ...
     421              : !> \param info_approximation ...
     422              : !> \param oscill_str ...
     423              : !> \param qs_env ...
     424              : !> \param unit_nr ...
     425              : !> \param mp2_env ...
     426              : ! **************************************************************************************************
     427            4 :    SUBROUTINE calculate_NTOs(fm_X, fm_Y, &
     428            4 :                              mo_coeff, homo, virtual, &
     429              :                              info_approximation, &
     430              :                              oscill_str, &
     431              :                              qs_env, unit_nr, mp2_env)
     432              : 
     433              :       TYPE(cp_fm_type), INTENT(IN)                       :: fm_X
     434              :       TYPE(cp_fm_type), INTENT(IN), OPTIONAL             :: fm_Y
     435              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(IN)         :: mo_coeff
     436              :       INTEGER, INTENT(IN)                                :: homo, virtual
     437              :       CHARACTER(LEN=10)                                  :: info_approximation
     438              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: oscill_str
     439              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     440              :       INTEGER, INTENT(IN)                                :: unit_nr
     441              :       TYPE(mp2_type), INTENT(INOUT)                      :: mp2_env
     442              : 
     443              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'calculate_NTOs'
     444              :       REAL(KIND=dp), PARAMETER                           :: coeff_err = 1.0E-5_dp
     445              : 
     446              :       CHARACTER(LEN=20), DIMENSION(2)                    :: nto_name
     447              :       INTEGER                                            :: handle, homo_irred, i, i_nto, info_svd, &
     448              :                                                             j, n_exc, n_nto, nao_full, nao_trunc
     449            4 :       INTEGER, DIMENSION(:), POINTER                     :: stride
     450              :       LOGICAL                                            :: append_cube, cube_file
     451            4 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigval_svd_squ
     452            4 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: eigval_svd
     453              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_m, fm_struct_mo_coeff, &
     454              :                                                             fm_struct_nto_holes, &
     455              :                                                             fm_struct_nto_particles, &
     456              :                                                             fm_struct_nto_set
     457              :       TYPE(cp_fm_type) :: fm_eigvl, fm_eigvr_t, fm_m, fm_mo_coeff, fm_nto_coeff_holes, &
     458              :          fm_nto_coeff_particles, fm_nto_set, fm_X_ia, fm_Y_ai
     459            4 :       TYPE(mo_set_type), ALLOCATABLE, DIMENSION(:)       :: nto_set
     460              :       TYPE(section_vals_type), POINTER                   :: bse_section, input, nto_section
     461              : 
     462            4 :       CALL timeset(routineN, handle)
     463              :       CALL get_qs_env(qs_env=qs_env, &
     464            4 :                       input=input)
     465            4 :       bse_section => section_vals_get_subs_vals(input, "DFT%XC%WF_CORRELATION%RI_RPA%GW%BSE")
     466              : 
     467            4 :       nao_full = qs_env%mos(1)%nao
     468            4 :       nao_trunc = homo + virtual
     469              :       ! This is not influenced by the BSE cutoff
     470            4 :       homo_irred = qs_env%mos(1)%homo
     471              :       ! M will have a block structure and is quadratic in homo+virtual, i.e.
     472              :       !                          occ   virt
     473              :       !                       |   0    X_i,a |  occ  = homo
     474              :       !     M        =        | Y_a,i    0   |  virt = virtual
     475              :       !
     476              :       ! X and Y are here not the eigenvectors X_ia,n - instead we fix n and reshape the combined ia index
     477              :       ! Notice the index structure of the lower block, i.e. X is transposed
     478              :       CALL cp_fm_struct_create(fm_struct_m, &
     479              :                                fm_X%matrix_struct%para_env, fm_X%matrix_struct%context, &
     480            4 :                                nao_trunc, nao_trunc)
     481              :       CALL cp_fm_struct_create(fm_struct_mo_coeff, &
     482              :                                fm_X%matrix_struct%para_env, fm_X%matrix_struct%context, &
     483            4 :                                nao_full, nao_trunc)
     484              :       CALL cp_fm_struct_create(fm_struct_nto_holes, &
     485              :                                fm_X%matrix_struct%para_env, fm_X%matrix_struct%context, &
     486            4 :                                nao_full, nao_trunc)
     487              :       CALL cp_fm_struct_create(fm_struct_nto_particles, &
     488              :                                fm_X%matrix_struct%para_env, fm_X%matrix_struct%context, &
     489            4 :                                nao_full, nao_trunc)
     490              : 
     491              :       CALL cp_fm_create(fm_mo_coeff, matrix_struct=fm_struct_mo_coeff, &
     492            4 :                         name="mo_coeff")
     493              :       ! Here, we take care of possible cutoffs
     494              :       ! Simply truncating the matrix causes problems with the print function
     495              :       ! Therefore, we keep the dimension, but set the coefficients of truncated indices to 0
     496              :       CALL cp_fm_to_fm_submat_general(mo_coeff(1), fm_mo_coeff, &
     497              :                                       nao_full, nao_trunc, &
     498              :                                       1, homo_irred - homo + 1, &
     499              :                                       1, 1, &
     500            4 :                                       mo_coeff(1)%matrix_struct%context)
     501              : 
     502              :       ! Print some information about the NTOs
     503            4 :       IF (unit_nr > 0) THEN
     504            2 :          WRITE (unit_nr, '(T2,A4,T7,A)') 'BSE|', &
     505            4 :             'The Natural Transition Orbital (NTO) pairs φ_I(r_e) and χ_I(r_h) for a fixed'
     506            2 :          WRITE (unit_nr, '(T2,A4,T7,A)') 'BSE|', &
     507            4 :             'excitation index n are obtained by singular value decomposition of T'
     508            2 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     509            2 :          WRITE (unit_nr, '(T2,A4,T15,A)') 'BSE|', &
     510            4 :             '        = (0   X)'
     511            2 :          IF (PRESENT(fm_Y)) THEN
     512            1 :             WRITE (unit_nr, '(T2,A4,T15,A)') 'BSE|', &
     513            2 :                'T       = (Y^T 0)'
     514              :          ELSE
     515            1 :             WRITE (unit_nr, '(T2,A4,T15,A)') 'BSE|', &
     516            2 :                'T       = (0   0)'
     517              :          END IF
     518            2 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     519            2 :          WRITE (unit_nr, '(T2,A4,T15,A)') 'BSE|', &
     520            4 :             'T        = U Λ V^T'
     521            2 :          WRITE (unit_nr, '(T2,A4,T15,A)') 'BSE|', &
     522            4 :             'φ_I(r_e) = \sum_p V_pI ψ_p(r_e)'
     523            2 :          WRITE (unit_nr, '(T2,A4,T15,A)') 'BSE|', &
     524            4 :             'χ_I(r_h) = \sum_p U_pI ψ_p(r_e)'
     525            2 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     526            2 :          WRITE (unit_nr, '(T2,A4,T7,A)') 'BSE|', &
     527            4 :             'where we have introduced'
     528            2 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     529              :          WRITE (unit_nr, '(T2,A4,T7,A,T20,A)') &
     530            2 :             'BSE|', "ψ_p:", "occupied and virtual molecular orbitals,"
     531              :          WRITE (unit_nr, '(T2,A4,T7,A,T20,A)') &
     532            2 :             'BSE|', "φ_I(r_e):", "NTO state for the electron,"
     533              :          WRITE (unit_nr, '(T2,A4,T7,A,T20,A)') &
     534            2 :             'BSE|', "χ_I(r_h):", "NTO state for the hole,"
     535              :          WRITE (unit_nr, '(T2,A4,T7,A,T20,A)') &
     536            2 :             'BSE|', "Λ:", "diagonal matrix of NTO weights λ_I,"
     537            2 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     538            2 :          WRITE (unit_nr, '(T2,A4,T7,A)') 'BSE|', &
     539            4 :             "The NTOs are calculated with the following settings:"
     540            2 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     541            2 :          WRITE (unit_nr, '(T2,A4,T7,A,T71,I10)') 'BSE|', 'Number of excitations, for which NTOs are computed', &
     542            4 :             mp2_env%bse%num_print_exc_ntos
     543            2 :          IF (mp2_env%bse%eps_nto_osc_str > 0.0_dp) THEN
     544            0 :             WRITE (unit_nr, '(T2,A4,T7,A,T71,F10.3)') 'BSE|', 'Threshold for oscillator strength f^n', &
     545            0 :                mp2_env%bse%eps_nto_osc_str
     546              :          ELSE
     547            2 :             WRITE (unit_nr, '(T2,A4,T7,A,T71,A10)') 'BSE|', 'Threshold for oscillator strength f^n', &
     548            4 :                ADJUSTL("---")
     549              :          END IF
     550            2 :          WRITE (unit_nr, '(T2,A4,T7,A,T72,F10.3)') 'BSE|', 'Threshold for NTO weights (λ_I)^2', &
     551            4 :             mp2_env%bse%eps_nto_eigval
     552              :       END IF
     553              : 
     554              :       ! Write the header of NTO info table
     555            4 :       IF (unit_nr > 0) THEN
     556            2 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     557            2 :          IF (.NOT. PRESENT(fm_Y)) THEN
     558            1 :             WRITE (unit_nr, '(T2,A4,T7,A)') 'BSE|', &
     559            2 :                'NTOs from solving the BSE within the TDA:'
     560              :          ELSE
     561            1 :             WRITE (unit_nr, '(T2,A4,T7,A)') 'BSE|', &
     562            2 :                'NTOs from solving the BSE without the TDA:'
     563              :          END IF
     564            2 :          WRITE (unit_nr, '(T2,A4)') 'BSE|'
     565            2 :          WRITE (unit_nr, '(T2,A4,T8,A12,T22,A8,T33,A14,T62,A)') 'BSE|', &
     566            4 :             'Excitation n', "TDA/ABBA", "Index of NTO I", 'NTO weights (λ_I)^2'
     567              :       END IF
     568              : 
     569          104 :       DO j = 1, mp2_env%bse%num_print_exc_ntos
     570          100 :          n_exc = mp2_env%bse%bse_nto_state_list_final(j)
     571              :          ! Takes care of unallocated oscill_str array in case of Triplet
     572          100 :          IF (mp2_env%bse%eps_nto_osc_str > 0.0_dp) THEN
     573              :             ! Check actual values
     574            0 :             IF (oscill_str(n_exc) < mp2_env%bse%eps_nto_osc_str) THEN
     575              :                ! Print skipped levels to table
     576            0 :                IF (unit_nr > 0) THEN
     577            0 :                   WRITE (unit_nr, '(T2,A4)') 'BSE|'
     578            0 :                   WRITE (unit_nr, '(T2,A4,T8,I12,T24,A6,T42,A39)') 'BSE|', &
     579            0 :                      n_exc, info_approximation, "Skipped (Oscillator strength too small)"
     580              :                END IF
     581              :                CYCLE
     582              :             END IF
     583              :          END IF
     584              : 
     585              :          CALL cp_fm_create(fm_m, matrix_struct=fm_struct_m, &
     586          100 :                            name="single_part_trans_dm")
     587          100 :          CALL cp_fm_set_all(fm_m, 0.0_dp)
     588              : 
     589              :          CALL cp_fm_create(fm_nto_coeff_holes, matrix_struct=fm_struct_nto_holes, &
     590          100 :                            name="nto_coeffs_holes")
     591          100 :          CALL cp_fm_set_all(fm_nto_coeff_holes, 0.0_dp)
     592              : 
     593              :          CALL cp_fm_create(fm_nto_coeff_particles, matrix_struct=fm_struct_nto_particles, &
     594          100 :                            name="nto_coeffs_particles")
     595          100 :          CALL cp_fm_set_all(fm_nto_coeff_particles, 0.0_dp)
     596              : 
     597              :          ! Reshuffle from X_ia,n_exc to X_i,a
     598              :          CALL reshuffle_eigvec(fm_X, fm_X_ia, homo, virtual, n_exc, &
     599          100 :                                .FALSE., unit_nr, mp2_env)
     600              : 
     601              :          ! Copy X to upper block in M, i.e. starting from column homo+1
     602              :          CALL cp_fm_to_fm_submat(fm_X_ia, fm_m, &
     603              :                                  homo, virtual, &
     604              :                                  1, 1, &
     605          100 :                                  1, homo + 1)
     606          100 :          CALL cp_fm_release(fm_X_ia)
     607              :          ! Copy Y if present
     608          100 :          IF (PRESENT(fm_Y)) THEN
     609              :             ! Reshuffle from Y_ia,n_exc to Y_a,i
     610              :             CALL reshuffle_eigvec(fm_Y, fm_Y_ai, homo, virtual, n_exc, &
     611           50 :                                   .TRUE., unit_nr, mp2_env)
     612              : 
     613              :             ! Copy Y^T to lower block in M, i.e. starting from row homo+1
     614              :             CALL cp_fm_to_fm_submat(fm_Y_ai, fm_m, &
     615              :                                     virtual, homo, &
     616              :                                     1, 1, &
     617           50 :                                     homo + 1, 1)
     618              : 
     619           50 :             CALL cp_fm_release(fm_Y_ai)
     620              : 
     621              :          END IF
     622              : 
     623              :          ! Now we compute the SVD of M_{occ+virt,occ+virt}, which yields
     624              :          ! M = U * Lambda * V^T
     625              :          ! Initialize matrices and arrays to store left/right eigenvectors and singular values
     626              :          CALL cp_fm_create(matrix=fm_eigvl, &
     627              :                            matrix_struct=fm_m%matrix_struct, &
     628          100 :                            name="LEFT_SINGULAR_MATRIX")
     629          100 :          CALL cp_fm_set_all(fm_eigvl, alpha=0.0_dp)
     630              :          CALL cp_fm_create(matrix=fm_eigvr_t, &
     631              :                            matrix_struct=fm_m%matrix_struct, &
     632          100 :                            name="RIGHT_SINGULAR_MATRIX")
     633          100 :          CALL cp_fm_set_all(fm_eigvr_t, alpha=0.0_dp)
     634              : 
     635          300 :          ALLOCATE (eigval_svd(nao_trunc))
     636         1700 :          eigval_svd(:) = 0.0_dp
     637              :          info_svd = 0
     638          100 :          CALL cp_fm_svd(fm_m, fm_eigvl, fm_eigvr_t, eigval_svd, info_svd)
     639          604 :          IF (info_svd /= 0) THEN
     640            0 :             IF (unit_nr > 0) THEN
     641              :                CALL cp_warn(__LOCATION__, &
     642              :                             "SVD for computation of NTOs not successful. "// &
     643            0 :                             "Skipping print of NTOs.")
     644            0 :                IF (info_svd > 0) THEN
     645              :                   CALL cp_warn(__LOCATION__, &
     646              :                                "PDGESVD detected heterogeneity. "// &
     647            0 :                                "Decreasing number of MPI ranks might solve this issue.")
     648              :                END IF
     649              :             END IF
     650              :             ! Release matrices to avoid memory leaks
     651            0 :             CALL cp_fm_release(fm_m)
     652            0 :             CALL cp_fm_release(fm_nto_coeff_holes)
     653            0 :             CALL cp_fm_release(fm_nto_coeff_particles)
     654              :          ELSE
     655              :             ! Rescale singular values as done in Martin2003 (10.1063/1.1558471)
     656          200 :             ALLOCATE (eigval_svd_squ(nao_trunc))
     657         1700 :             eigval_svd_squ(:) = eigval_svd(:)**2
     658              :             ! Sanity check for TDA: In case of TDA, the sum should be \sum_ia |X_ia|^2 = 1
     659          100 :             IF (.NOT. PRESENT(fm_Y)) THEN
     660          850 :                IF (ABS(SUM(eigval_svd_squ) - 1) >= coeff_err) THEN
     661            0 :                   CPWARN("Sum of NTO coefficients deviates from 1!")
     662              :                END IF
     663              :             END IF
     664              : 
     665              :             ! Create NTO coefficients for later print to grid via TDDFT routine
     666              :             ! Apply U = fm_eigvl to MO coeffs, which yields hole states
     667              :             CALL parallel_gemm("N", "N", nao_full, nao_trunc, nao_trunc, 1.0_dp, fm_mo_coeff, fm_eigvl, 0.0_dp, &
     668          100 :                                fm_nto_coeff_holes)
     669              : 
     670              :             ! Apply V^T = fm_eigvr_t to MO coeffs, which yields particle states
     671              :             CALL parallel_gemm("N", "T", nao_full, nao_trunc, nao_trunc, 1.0_dp, fm_mo_coeff, fm_eigvr_t, 0.0_dp, &
     672          100 :                                fm_nto_coeff_particles)
     673              : 
     674              :             !Release intermediary work matrices
     675          100 :             CALL cp_fm_release(fm_m)
     676          100 :             CALL cp_fm_release(fm_eigvl)
     677          100 :             CALL cp_fm_release(fm_eigvr_t)
     678              : 
     679              :             ! Transfer NTO coefficients to sets
     680          100 :             nto_name(1) = 'Hole_coord'
     681          100 :             nto_name(2) = 'Particle_coord'
     682          300 :             ALLOCATE (nto_set(2))
     683              :             ! Extract number of significant NTOs
     684          100 :             n_nto = 0
     685          320 :             DO i_nto = 1, nao_trunc
     686          320 :                IF (eigval_svd_squ(i_nto) > mp2_env%bse%eps_nto_eigval) THEN
     687          220 :                   n_nto = n_nto + 1
     688              :                ELSE
     689              :                   ! Since svd orders in descending order, we can exit the loop if smaller
     690              :                   EXIT
     691              :                END IF
     692              :             END DO
     693              : 
     694          100 :             IF (unit_nr > 0) THEN
     695           50 :                WRITE (unit_nr, '(T2,A4)') 'BSE|'
     696          160 :                DO i_nto = 1, n_nto
     697          110 :                   WRITE (unit_nr, '(T2,A4,T8,I12,T24,A6,T41,I6,T71,F10.5)') 'BSE|', &
     698          270 :                      n_exc, info_approximation, i_nto, eigval_svd_squ(i_nto)
     699              :                END DO
     700              :             END IF
     701              : 
     702              :             CALL cp_fm_struct_create(fm_struct_nto_set, template_fmstruct=fm_struct_nto_holes, &
     703          100 :                                      ncol_global=n_nto)
     704          100 :             CALL cp_fm_create(fm_nto_set, fm_struct_nto_set)
     705          300 :             DO i = 1, 2
     706          200 :                CALL allocate_mo_set(nto_set(i), nao_trunc, n_nto, 0, 0.0_dp, 2.0_dp, 0.0_dp)
     707          300 :                CALL init_mo_set(nto_set(i), fm_ref=fm_nto_set, name=nto_name(i))
     708              :             END DO
     709          100 :             CALL cp_fm_release(fm_nto_set)
     710          100 :             CALL cp_fm_struct_release(fm_struct_nto_set)
     711              : 
     712              :             ! Fill NTO sets
     713          100 :             CALL cp_fm_to_fm(fm_nto_coeff_holes, nto_set(1)%mo_coeff, ncol=n_nto)
     714          100 :             CALL cp_fm_to_fm(fm_nto_coeff_particles, nto_set(2)%mo_coeff, ncol=n_nto)
     715              : 
     716              :             ! Cube files
     717          100 :             nto_section => section_vals_get_subs_vals(bse_section, "NTO_ANALYSIS")
     718          100 :             CALL section_vals_val_get(nto_section, "CUBE_FILES", l_val=cube_file)
     719          100 :             CALL section_vals_val_get(nto_section, "STRIDE", i_vals=stride)
     720          100 :             CALL section_vals_val_get(nto_section, "APPEND", l_val=append_cube)
     721          100 :             IF (cube_file) THEN
     722              :                CALL print_bse_nto_cubes(qs_env, nto_set, n_exc, info_approximation, &
     723            0 :                                         stride, append_cube, nto_section)
     724              :             END IF
     725              : 
     726          100 :             CALL cp_fm_release(fm_nto_coeff_holes)
     727          100 :             CALL cp_fm_release(fm_nto_coeff_particles)
     728          100 :             DEALLOCATE (eigval_svd)
     729          100 :             DEALLOCATE (eigval_svd_squ)
     730          300 :             DO i = 1, 2
     731          300 :                CALL deallocate_mo_set(nto_set(i))
     732              :             END DO
     733          400 :             DEALLOCATE (nto_set)
     734              :          END IF
     735              :       END DO
     736              : 
     737            4 :       CALL cp_fm_release(fm_mo_coeff)
     738            4 :       CALL cp_fm_struct_release(fm_struct_m)
     739            4 :       CALL cp_fm_struct_release(fm_struct_nto_holes)
     740            4 :       CALL cp_fm_struct_release(fm_struct_nto_particles)
     741            4 :       CALL cp_fm_struct_release(fm_struct_mo_coeff)
     742              : 
     743            4 :       CALL timestop(handle)
     744              : 
     745            8 :    END SUBROUTINE calculate_NTOs
     746              : 
     747              : ! **************************************************************************************************
     748              : !> \brief ...
     749              : !> \param exc_descr Allocated and initialized on exit
     750              : !> \param fm_X_ia ...
     751              : !> \param fm_multipole_ij_trunc ...
     752              : !> \param fm_multipole_ab_trunc ...
     753              : !> \param fm_multipole_ai_trunc ...
     754              : !> \param i_exc ...
     755              : !> \param homo ...
     756              : !> \param virtual ...
     757              : !> \param fm_Y_ia ...
     758              : ! **************************************************************************************************
     759          110 :    SUBROUTINE get_exciton_descriptors(exc_descr, fm_X_ia, &
     760              :                                       fm_multipole_ij_trunc, fm_multipole_ab_trunc, &
     761              :                                       fm_multipole_ai_trunc, &
     762              :                                       i_exc, homo, virtual, &
     763              :                                       fm_Y_ia)
     764              : 
     765              :       TYPE(exciton_descr_type), ALLOCATABLE, &
     766              :          DIMENSION(:)                                    :: exc_descr
     767              :       TYPE(cp_fm_type), INTENT(IN)                       :: fm_X_ia
     768              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:), &
     769              :          INTENT(IN)                                      :: fm_multipole_ij_trunc, &
     770              :                                                             fm_multipole_ab_trunc, &
     771              :                                                             fm_multipole_ai_trunc
     772              :       INTEGER, INTENT(IN)                                :: i_exc, homo, virtual
     773              :       TYPE(cp_fm_type), INTENT(IN), OPTIONAL             :: fm_Y_ia
     774              : 
     775              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_exciton_descriptors'
     776              : 
     777              :       INTEGER                                            :: handle, i_dir, j_dir
     778              :       INTEGER, DIMENSION(3)                              :: mask_quadrupole
     779              :       LOGICAL                                            :: flag_TDA
     780              :       REAL(KIND=dp)                                      :: norm_X, norm_XpY, norm_Y
     781              :       REAL(KIND=dp), DIMENSION(3)                        :: r_e_sq_X, r_e_sq_Y, r_e_X, r_e_Y, &
     782              :                                                             r_h_sq_X, r_h_sq_Y, r_h_X, r_h_Y
     783              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: r_e_h_XX, r_e_h_XY, r_e_h_YY
     784              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_ab, fm_struct_ia
     785              :       TYPE(cp_fm_type)                                   :: fm_work_ba, fm_work_ia, fm_work_ia_2
     786              : 
     787          110 :       CALL timeset(routineN, handle)
     788          110 :       IF (PRESENT(fm_Y_ia)) THEN
     789              :          flag_TDA = .FALSE.
     790              :       ELSE
     791           60 :          flag_TDA = .TRUE.
     792              :       END IF
     793              : 
     794              :       ! translates 1,2,3 to diagonal entries of quadrupoles xx, yy, zz
     795              :       ! Ordering in quadrupole moments is x, y, z, xx, xy, xz, yy, yz, zz
     796          110 :       mask_quadrupole = [4, 7, 9]
     797              : 
     798              :       CALL cp_fm_struct_create(fm_struct_ia, &
     799          110 :                                context=fm_X_ia%matrix_struct%context, nrow_global=homo, ncol_global=virtual)
     800              :       CALL cp_fm_struct_create(fm_struct_ab, &
     801          110 :                                context=fm_X_ia%matrix_struct%context, nrow_global=virtual, ncol_global=virtual)
     802              : 
     803          110 :       r_e_X(:) = 0.0_dp
     804          110 :       r_e_Y(:) = 0.0_dp
     805          110 :       r_h_X(:) = 0.0_dp
     806          110 :       r_h_Y(:) = 0.0_dp
     807          110 :       r_e_sq_X(:) = 0.0_dp
     808          110 :       r_h_sq_X(:) = 0.0_dp
     809          110 :       r_e_sq_Y(:) = 0.0_dp
     810          110 :       r_h_sq_Y(:) = 0.0_dp
     811          110 :       r_e_h_XX(:, :) = 0.0_dp
     812          110 :       r_e_h_XY(:, :) = 0.0_dp
     813          110 :       r_e_h_YY(:, :) = 0.0_dp
     814              : 
     815              :       norm_X = 0.0_dp
     816              :       norm_Y = 0.0_dp
     817          110 :       norm_XpY = 0.0_dp
     818              : 
     819              :       ! Initialize values of exciton descriptors
     820          440 :       exc_descr(i_exc)%r_e(:) = 0.0_dp
     821          440 :       exc_descr(i_exc)%r_h(:) = 0.0_dp
     822          440 :       exc_descr(i_exc)%r_e_sq(:) = 0.0_dp
     823          440 :       exc_descr(i_exc)%r_h_sq(:) = 0.0_dp
     824         1430 :       exc_descr(i_exc)%r_e_h(:, :) = 0.0_dp
     825              : 
     826          110 :       exc_descr(i_exc)%flag_TDA = flag_TDA
     827          110 :       exc_descr(i_exc)%norm_XpY = 0.0_dp
     828              : 
     829              :       ! Norm of X
     830          110 :       CALL cp_fm_trace(fm_X_ia, fm_X_ia, norm_X)
     831          110 :       norm_XpY = norm_X
     832              :       ! Norm of Y
     833          110 :       IF (.NOT. flag_TDA) THEN
     834           50 :          CALL cp_fm_trace(fm_Y_ia, fm_Y_ia, norm_Y)
     835           50 :          norm_XpY = norm_XpY + norm_Y
     836              :       END IF
     837              : 
     838          110 :       exc_descr(i_exc)%norm_XpY = norm_XpY
     839              : 
     840              :       ! <r_h>_X = Tr[ X^T µ_ij X + Y µ_ab Y^T ] = X_ai µ_ij X_ja + Y_ia  µ_ab Y_bi
     841          440 :       DO i_dir = 1, 3
     842              :          ! <r_h>_X = X_ai µ_ij X_ja + ...
     843          330 :          CALL trace_exciton_descr(fm_X_ia, fm_multipole_ij_trunc(i_dir), fm_X_ia, r_h_X(i_dir))
     844          330 :          r_h_X(i_dir) = r_h_X(i_dir)/norm_XpY
     845          440 :          IF (.NOT. flag_TDA) THEN
     846              :             ! <r_h>_X = ... + Y_ia  µ_ab Y_bi
     847          150 :             CALL trace_exciton_descr(fm_Y_ia, fm_Y_ia, fm_multipole_ab_trunc(i_dir), r_h_Y(i_dir))
     848          150 :             r_h_Y(i_dir) = r_h_Y(i_dir)/norm_XpY
     849              :          END IF
     850              :       END DO
     851          440 :       exc_descr(i_exc)%r_h(:) = r_h_X(:) + r_h_Y(:)
     852              : 
     853              :       ! <r_e>_X = Tr[ X µ_ab X^T + Y^T µ_ij Y ] = X_ia µ_ab X_bi + Y_ai µ_ij Y_ja
     854          440 :       DO i_dir = 1, 3
     855              :          ! <r_e>_X = work_ib X_bi + ... = X_ib^T work_ib + ...
     856          330 :          CALL trace_exciton_descr(fm_X_ia, fm_X_ia, fm_multipole_ab_trunc(i_dir), r_e_X(i_dir))
     857          330 :          r_e_X(i_dir) = r_e_X(i_dir)/norm_XpY
     858          440 :          IF (.NOT. flag_TDA) THEN
     859              :             ! <r_e>_X = ... + Y_ai µ_ij Y_ja
     860          150 :             CALL trace_exciton_descr(fm_Y_ia, fm_multipole_ij_trunc(i_dir), fm_Y_ia, r_e_Y(i_dir))
     861          150 :             r_e_Y(i_dir) = r_e_Y(i_dir)/norm_XpY
     862              :          END IF
     863              :       END DO
     864          440 :       exc_descr(i_exc)%r_e(:) = r_e_X(:) + r_e_Y(:)
     865              : 
     866              :       ! <r_h^2>_X = Tr[ X^T M_ij X + Y M_ab Y^T ] = X_ai M_ij X_ja + Y_ia  M_ab Y_bi
     867          440 :       DO i_dir = 1, 3
     868              :          ! <r_h^2>_X = X_ai M_ij X_ja + ...
     869              :          CALL trace_exciton_descr(fm_X_ia, fm_multipole_ij_trunc(mask_quadrupole(i_dir)), &
     870          330 :                                   fm_X_ia, r_h_sq_X(i_dir))
     871          330 :          r_h_sq_X(i_dir) = r_h_sq_X(i_dir)/norm_XpY
     872          440 :          IF (.NOT. flag_TDA) THEN
     873              :             ! <r_h^2>_X = ... + Y_ia  M_ab Y_bi
     874              :             CALL trace_exciton_descr(fm_Y_ia, fm_Y_ia, &
     875          150 :                                      fm_multipole_ab_trunc(mask_quadrupole(i_dir)), r_h_sq_Y(i_dir))
     876          150 :             r_h_sq_Y(i_dir) = r_h_sq_Y(i_dir)/norm_XpY
     877              :          END IF
     878              :       END DO
     879          440 :       exc_descr(i_exc)%r_h_sq(:) = r_h_sq_X(:) + r_h_sq_Y(:)
     880              : 
     881              :       ! <r_e^2>_X = Tr[ X M_ab X^T + Y^T M_ij Y ] = X_ia M_ab X_bi + Y_ai M_ij Y_ja
     882          440 :       DO i_dir = 1, 3
     883              :          ! <r_e^2>_X = work_ib X_bi + ... = X_ib^T work_ib + ...
     884              :          CALL trace_exciton_descr(fm_X_ia, fm_X_ia, &
     885          330 :                                   fm_multipole_ab_trunc(mask_quadrupole(i_dir)), r_e_sq_X(i_dir))
     886          330 :          r_e_sq_X(i_dir) = r_e_sq_X(i_dir)/norm_XpY
     887          440 :          IF (.NOT. flag_TDA) THEN
     888              :             ! <r_e^2>_X = ... + Y_ai M_ij Y_ja
     889              :             CALL trace_exciton_descr(fm_Y_ia, fm_multipole_ij_trunc(mask_quadrupole(i_dir)), &
     890          150 :                                      fm_Y_ia, r_e_sq_Y(i_dir))
     891          150 :             r_e_sq_Y(i_dir) = r_e_sq_Y(i_dir)/norm_XpY
     892              :          END IF
     893              :       END DO
     894          440 :       exc_descr(i_exc)%r_e_sq(:) = r_e_sq_X(:) + r_e_sq_Y(:)
     895              : 
     896              :       ! <r_e^\mu r_h^\mu'>_X
     897              :       !   = Tr[ X^T µ'_ij X µ_ab  +  Y^T µ_ij Y µ'_ab  +  2 X µ_ai Y µ'_ai ]
     898              :       !   = X_bj µ'_ji X_ia µ_ab + Y_bj µ_ji Y_ia µ'_ab + 2 X_ia µ_aj Y_jb µ'_bi
     899              :       ! The i_dir and j_dir convert to mu and mu'. µ (electron) sits between a (from X) and j (from Y),
     900              :       ! µ' (hole) between i (from X) and b (from Y); Tr[ Y µ'_ai X µ_ai ] = Tr[ X µ_ai Y µ'_ai ] by cyclicity, hence the 2.
     901          110 :       CALL cp_fm_create(fm_work_ia, fm_struct_ia)
     902          110 :       CALL cp_fm_create(fm_work_ia_2, fm_struct_ia)
     903          110 :       CALL cp_fm_create(fm_work_ba, fm_struct_ab)
     904          440 :       DO i_dir = 1, 3
     905         1430 :          DO j_dir = 1, 3
     906              :             ! First term - X^T µ'_ij X µ_ab
     907          990 :             CALL cp_fm_set_all(fm_work_ia, 0.0_dp)
     908          990 :             CALL cp_fm_set_all(fm_work_ia_2, 0.0_dp)
     909              :             ! work_ib = X_ia µ_ab
     910              :             CALL parallel_gemm("N", "N", homo, virtual, virtual, 1.0_dp, &
     911          990 :                                fm_X_ia, fm_multipole_ab_trunc(i_dir), 0.0_dp, fm_work_ia)
     912              :             ! work_ja_2 = µ'_ji work_ia
     913              :             CALL parallel_gemm("N", "N", homo, virtual, homo, 1.0_dp, &
     914          990 :                                fm_multipole_ij_trunc(j_dir), fm_work_ia, 0.0_dp, fm_work_ia_2)
     915              :             ! <r_e^\mu r_h^\mu'>_X = work_ia_2 X_bj + ... = X^T work_ia_2 + ...
     916          990 :             CALL cp_fm_trace(fm_X_ia, fm_work_ia_2, r_e_h_XX(i_dir, j_dir))
     917          990 :             r_e_h_XX(i_dir, j_dir) = r_e_h_XX(i_dir, j_dir)/norm_XpY
     918         1320 :             IF (.NOT. flag_TDA) THEN
     919              :                ! Second term -  Y^T µ_ij Y µ'_ab
     920          450 :                CALL cp_fm_set_all(fm_work_ia, 0.0_dp)
     921          450 :                CALL cp_fm_set_all(fm_work_ia_2, 0.0_dp)
     922              :                ! work_ib = Y_ia µ'_ab
     923              :                CALL parallel_gemm("N", "N", homo, virtual, virtual, 1.0_dp, &
     924          450 :                                   fm_Y_ia, fm_multipole_ab_trunc(j_dir), 0.0_dp, fm_work_ia)
     925              :                ! work_ja_2 = µ_ji work_ia
     926              :                CALL parallel_gemm("N", "N", homo, virtual, homo, 1.0_dp, &
     927          450 :                                   fm_multipole_ij_trunc(i_dir), fm_work_ia, 0.0_dp, fm_work_ia_2)
     928              :                ! <r_h r_e>_X = work_ia_2 Y_bj + ... = Y^T work_ia_2 + ...
     929          450 :                CALL cp_fm_trace(fm_Y_ia, fm_work_ia_2, r_e_h_YY(i_dir, j_dir))
     930          450 :                r_e_h_YY(i_dir, j_dir) = r_e_h_YY(i_dir, j_dir)/norm_XpY
     931              : 
     932              :                ! Third term (counted twice) - X µ_ai Y µ'_ai = X_ia µ_aj Y_jb µ'_bi
     933              :                !     Reshuffle for usage of trace (where first argument is transposed)
     934              :                !     = µ_aj Y_jb µ'_bi X_ia =
     935              :                !        \___________/
     936              :                !          fm_work_ai
     937              :                !     fm_work_ai = µ_aj Y_jb µ'_bi
     938              :                !     fm_work_ia = µ'_ib Y_bj µ_ja
     939              :                !                         \_____/
     940              :                !                        fm_work_ba
     941          450 :                CALL cp_fm_set_all(fm_work_ba, 0.0_dp)
     942          450 :                CALL cp_fm_set_all(fm_work_ia, 0.0_dp)
     943              :                ! fm_work_ba = Y_bj µ_ja
     944              :                CALL parallel_gemm("T", "T", virtual, virtual, homo, 1.0_dp, &
     945          450 :                                   fm_Y_ia, fm_multipole_ai_trunc(i_dir), 0.0_dp, fm_work_ba)
     946              :                ! fm_work_ia = µ'_ib fm_work_ba
     947              :                CALL parallel_gemm("T", "N", homo, virtual, virtual, 1.0_dp, &
     948          450 :                                   fm_multipole_ai_trunc(j_dir), fm_work_ba, 0.0_dp, fm_work_ia)
     949              :                ! <r_e r_h>_X = ... + 2 X_ia µ_aj Y_jb µ'_bi
     950          450 :                CALL cp_fm_trace(fm_work_ia, fm_X_ia, r_e_h_XY(i_dir, j_dir))
     951          450 :                r_e_h_XY(i_dir, j_dir) = 2.0_dp*r_e_h_XY(i_dir, j_dir)/norm_XpY
     952              :             END IF
     953              :          END DO
     954              :       END DO
     955         1430 :       exc_descr(i_exc)%r_e_h(:, :) = r_e_h_XX(:, :) + r_e_h_XY(:, :) + r_e_h_YY(:, :)
     956              : 
     957          110 :       CALL cp_fm_release(fm_work_ia)
     958          110 :       CALL cp_fm_release(fm_work_ia_2)
     959          110 :       CALL cp_fm_release(fm_work_ba)
     960              : 
     961              :       ! Now we compute all the descriptors and correlation coefficients
     962              :       ! Order is: Directional ones, then covariances and correlation coefficients and
     963              : 
     964              :       ! diff_r_abs = |<r_h>_X - <r_e>_X|
     965          440 :       exc_descr(i_exc)%diff_r_abs = SQRT(SUM((exc_descr(i_exc)%r_h(:) - exc_descr(i_exc)%r_e(:))**2))
     966              : 
     967              :       ! σ_e = sqrt( <r_e^2>_X - <r_e>_X^2 )
     968          770 :       exc_descr(i_exc)%sigma_e = SQRT(SUM(exc_descr(i_exc)%r_e_sq(:)) - SUM(exc_descr(i_exc)%r_e(:)**2))
     969              : 
     970              :       ! σ_h = sqrt( <r_h^2>_X - <r_h>_X^2 )
     971          770 :       exc_descr(i_exc)%sigma_h = SQRT(SUM(exc_descr(i_exc)%r_h_sq(:)) - SUM(exc_descr(i_exc)%r_h(:)**2))
     972              : 
     973              :       ! Now directed ones
     974          440 :       DO i_dir = 1, 3
     975          330 :          exc_descr(i_exc)%d_eh_dir(i_dir) = ABS(exc_descr(i_exc)%r_h(i_dir) - exc_descr(i_exc)%r_e(i_dir))
     976          330 :          exc_descr(i_exc)%sigma_e_dir(i_dir) = SQRT(exc_descr(i_exc)%r_e_sq(i_dir) - exc_descr(i_exc)%r_e(i_dir)**2)
     977          440 :          exc_descr(i_exc)%sigma_h_dir(i_dir) = SQRT(exc_descr(i_exc)%r_h_sq(i_dir) - exc_descr(i_exc)%r_h(i_dir)**2)
     978              :       END DO
     979              : 
     980              :       ! Covariance and correlation coefficient (as well as crosscorrelation matrices)
     981              :       ! COV(r_e, r_h) = < r_e r_h >_X - < r_e >_X < r_h >_X
     982          110 :       exc_descr(i_exc)%cov_e_h_sum = 0.0_dp
     983         1430 :       exc_descr(i_exc)%cov_e_h(:, :) = 0.0_dp
     984         1430 :       exc_descr(i_exc)%corr_e_h_matrix(:, :) = 0.0_dp
     985          440 :       DO i_dir = 1, 3
     986         1320 :          DO j_dir = 1, 3
     987              :             exc_descr(i_exc)%cov_e_h(i_dir, j_dir) = exc_descr(i_exc)%r_e_h(i_dir, j_dir) &
     988          990 :                                                      - exc_descr(i_exc)%r_e(i_dir)*exc_descr(i_exc)%r_h(j_dir)
     989              :             exc_descr(i_exc)%corr_e_h_matrix(i_dir, j_dir) = &
     990              :                exc_descr(i_exc)%cov_e_h(i_dir, j_dir)/ &
     991         1320 :                (exc_descr(i_exc)%sigma_e_dir(i_dir)*exc_descr(i_exc)%sigma_h_dir(j_dir))
     992              :          END DO
     993              :          exc_descr(i_exc)%cov_e_h_sum = exc_descr(i_exc)%cov_e_h_sum + &
     994              :                                         exc_descr(i_exc)%r_e_h(i_dir, i_dir) - &
     995          440 :                                         exc_descr(i_exc)%r_e(i_dir)*exc_descr(i_exc)%r_h(i_dir)
     996              :       END DO
     997              : 
     998              :       ! e-h-correlation coefficient R_eh = COV(r_e, r_h) / ( σ_e σ_h )
     999          110 :       exc_descr(i_exc)%corr_e_h = exc_descr(i_exc)%cov_e_h_sum/(exc_descr(i_exc)%sigma_e*exc_descr(i_exc)%sigma_h)
    1000              : 
    1001              :       ! root-mean-square e-h separation
    1002              :       exc_descr(i_exc)%diff_r_sqr = SQRT(exc_descr(i_exc)%diff_r_abs**2 + &
    1003              :                                          exc_descr(i_exc)%sigma_e**2 + exc_descr(i_exc)%sigma_h**2 &
    1004          110 :                                          - 2*exc_descr(i_exc)%cov_e_h_sum)
    1005              : 
    1006          440 :       DO i_dir = 1, 3
    1007              :          exc_descr(i_exc)%d_exc_dir(i_dir) = SQRT(exc_descr(i_exc)%d_eh_dir(i_dir)**2 + &
    1008              :                                                   exc_descr(i_exc)%sigma_e_dir(i_dir)**2 + &
    1009              :                                                   exc_descr(i_exc)%sigma_h_dir(i_dir)**2 - &
    1010          440 :                                                   2*exc_descr(i_exc)%cov_e_h(i_dir, i_dir))
    1011              :       END DO
    1012              : 
    1013              :       ! Expectation values of r_e and r_h
    1014          440 :       exc_descr(i_exc)%r_e_shift(:) = exc_descr(i_exc)%r_e(:)
    1015          440 :       exc_descr(i_exc)%r_h_shift(:) = exc_descr(i_exc)%r_h(:)
    1016              : 
    1017          110 :       CALL cp_fm_struct_release(fm_struct_ia)
    1018          110 :       CALL cp_fm_struct_release(fm_struct_ab)
    1019              : 
    1020          110 :       CALL timestop(handle)
    1021              : 
    1022          330 :    END SUBROUTINE get_exciton_descriptors
    1023              : 
    1024            0 : END MODULE bse_properties
        

Generated by: LCOV version 2.0-1