LCOV - code coverage report
Current view: top level - src - bse_properties.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:4c33f95) Lines: 380 398 95.5 %
Date: 2025-01-30 06:53:08 Functions: 4 5 80.0 %

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

Generated by: LCOV version 1.15