LCOV - code coverage report
Current view: top level - src - gw_tensor_large_cell_gamma.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 96.1 % 483 464
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 25 25

            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 from paper [Graml2024]
      10              : !> \par History
      11              : !>      01.2026 Maximilian Graml: add more bounds to exploit sparsity in 3c integrals, fixes
      12              : !> \author Jan Wilhelm
      13              : !> \date 07.2023
      14              : ! **************************************************************************************************
      15              : MODULE gw_tensor_large_cell_Gamma
      16              :    USE bibliography,                    ONLY: Graml2024,&
      17              :                                               cite_reference
      18              :    USE cell_types,                      ONLY: cell_type,&
      19              :                                               pbc
      20              :    USE cp_cfm_basic_linalg,             ONLY: cp_cfm_get_diag
      21              :    USE cp_cfm_diag,                     ONLY: cp_cfm_geeig
      22              :    USE cp_cfm_types,                    ONLY: cp_cfm_create,&
      23              :                                               cp_cfm_release,&
      24              :                                               cp_cfm_type
      25              :    USE cp_dbcsr_api,                    ONLY: dbcsr_create,&
      26              :                                               dbcsr_p_type,&
      27              :                                               dbcsr_release,&
      28              :                                               dbcsr_type
      29              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      30              :                                               copy_fm_to_dbcsr,&
      31              :                                               dbcsr_deallocate_matrix_set
      32              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      33              :                                               cp_fm_release,&
      34              :                                               cp_fm_type
      35              :    USE dbt_api,                         ONLY: dbt_clear,&
      36              :                                               dbt_contract,&
      37              :                                               dbt_copy,&
      38              :                                               dbt_create,&
      39              :                                               dbt_destroy,&
      40              :                                               dbt_filter,&
      41              :                                               dbt_get_nze,&
      42              :                                               dbt_type
      43              :    USE dbt_matrix_redistribution,       ONLY: cp_dbt_to_dbcsr,&
      44              :                                               cp_fm_to_dbt
      45              :    USE gw_compute_V_and_W,              ONLY: fm_read,&
      46              :                                               fm_write,&
      47              :                                               get_W_MIC
      48              :    USE gw_utils,                        ONLY: G_occ_vir,&
      49              :                                               analyt_conti_and_print,&
      50              :                                               de_init_bs_env,&
      51              :                                               gw_qp_summary_type
      52              :    USE gw_utils_fm,                     ONLY: cfm_contract_ABA,&
      53              :                                               fm_contract_ABA
      54              :    USE hfx_types,                       ONLY: alloc_containers
      55              :    USE input_constants,                 ONLY: G0W0
      56              :    USE kinds,                           ONLY: default_path_length,&
      57              :                                               dp,&
      58              :                                               int_8
      59              :    USE machine,                         ONLY: m_walltime
      60              :    USE message_passing,                 ONLY: mp_file_delete,&
      61              :                                               mp_mem_avail_per_rank_GB
      62              :    USE mp2_ri_2c,                       ONLY: RI_2c_integral_mat
      63              :    USE particle_types,                  ONLY: particle_type
      64              :    USE post_scf_bandstructure_types,    ONLY: post_scf_bandstructure_type,&
      65              :                                               store_3c_release,&
      66              :                                               store_3c_type
      67              :    USE post_scf_bandstructure_utils,    ONLY: cfm_ikp_from_fm_Gamma,&
      68              :                                               get_all_VBM_CBM_bandgaps
      69              :    USE qs_environment_types,            ONLY: get_qs_env,&
      70              :                                               qs_environment_type
      71              :    USE qs_tensors,                      ONLY: build_3c_integrals,&
      72              :                                               compress_tensor,&
      73              :                                               decompress_tensor
      74              :    USE time_frequency_grids,            ONLY: negative_time,&
      75              :                                               positive_time,&
      76              :                                               transform_time_to_frequency
      77              : #include "./base/base_uses.f90"
      78              : 
      79              :    IMPLICIT NONE
      80              : 
      81              :    PRIVATE
      82              : 
      83              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_tensor_large_cell_Gamma'
      84              : 
      85              :    ! shares of memory that the kept 3-center integrals may take (estimated uncompressed): compressed
      86              :    ! below a share of MEMORY_PER_PROC, kept as tensors (used in place) only below a small share of
      87              :    ! MEMORY_PER_PROC and of the memory free at that time, since they add to the working set of the
      88              :    ! tensor contractions that the estimate does not cover
      89              :    REAL(KIND=dp), PARAMETER, PRIVATE :: store_3c_share_uncompressed = 0.1_dp, &
      90              :                                         store_3c_share_compressed = 0.5_dp
      91              :    ! compression stores multiples of EPS_FILTER times this (and drops smaller entries), as the
      92              :    ! default EPS_STORAGE_SCALING of low-scaling RPA/GW
      93              :    REAL(KIND=dp), PARAMETER, PRIVATE :: store_3c_eps_scaling = 1.0E-3_dp
      94              : 
      95              :    PUBLIC :: gw_calc_tensor_large_cell_Gamma, &
      96              :              compute_3c_integrals, &
      97              :              G_occ_vir, &
      98              :              delete_unnecessary_files, &
      99              :              compute_QP_energies, &
     100              :              get_mat_chi_Gamma_tau, &
     101              :              get_Sigma_x, &
     102              :              create_mat_for_Sigma_c, &
     103              :              destroy_mat_Sigma_c, &
     104              :              compute_Sigma_c_occ_vir, &
     105              :              print_skipping
     106              : 
     107              : CONTAINS
     108              : 
     109              : ! **************************************************************************************************
     110              : !> \brief Perform GW band structure calculation
     111              : !> \param qs_env ...
     112              : !> \param bs_env Band-structure environment containing GW parameters.
     113              : !> \par History
     114              : !>    * 07.2023 created [Jan Wilhelm]
     115              : ! **************************************************************************************************
     116           76 :    SUBROUTINE gw_calc_tensor_large_cell_Gamma(qs_env, bs_env)
     117              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     118              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     119              : 
     120              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'gw_calc_tensor_large_cell_Gamma'
     121              : 
     122              :       INTEGER                                            :: handle
     123           76 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_Sigma_x_Gamma, fm_W_MIC_time
     124           76 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :, :)  :: fm_Sigma_c_Gamma_time
     125              : 
     126           76 :       CALL timeset(routineN, handle)
     127              : 
     128           76 :       CALL cite_reference(Graml2024)
     129              : 
     130              :       ! G^occ_µλ(i|τ|,k=0) = sum_n^occ C_µn(k=0) e^(-|(ϵ_nk=0-ϵ_F)τ|) C_λn(k=0)
     131              :       ! G^vir_µλ(i|τ|,k=0) = sum_n^vir C_µn(k=0) e^(-|(ϵ_nk=0-ϵ_F)τ|) C_λn(k=0)
     132              :       ! χ_PQ(iτ,k=0) = sum_λν [sum_µ (µν|P) G^occ_µλ(i|τ|)] [sum_σ (σλ|Q) G^vir_σν(i|τ|)]
     133              :       ! (µν|P) does not depend on τ: kept (compressed) for the time points of χ if it fits
     134           76 :       CALL store_3c_init(bs_env, bs_env%store_3c_chi, bs_env%n_intervals_i + bs_env%n_intervals_j)
     135           76 :       CALL get_mat_chi_Gamma_tau(bs_env, qs_env, bs_env%mat_chi_Gamma_tau)
     136           76 :       CALL store_3c_finish(bs_env, bs_env%store_3c_chi)
     137              : 
     138              :       ! χ_PQ(iτ,k=0) -> χ_PQ(iω,k) -> ε_PQ(iω,k) -> W_PQ(iω,k) -> W^MIC_PQ(iτ) -> M^-1*W^MIC*M^-1
     139           76 :       CALL get_W_MIC(bs_env, qs_env, bs_env%mat_chi_Gamma_tau, fm_W_MIC_time)
     140              : 
     141              :       ! (νσ|P) and (λµ|Q) kept (compressed) for Σ^x and the time points of Σ^c if they fit
     142           76 :       CALL store_3c_init(bs_env, bs_env%store_3c_W, bs_env%n_intervals_i)
     143           76 :       CALL store_3c_init(bs_env, bs_env%store_3c_Sigma, bs_env%n_intervals_j)
     144              : 
     145              :       ! D_µν = sum_n^occ C_µn(k=0) C_νn(k=0), V^trunc_PQ = sum_cell_R <phi_P,0|V^trunc|phi_Q,R>
     146              :       ! Σ^x_λσ(k=0) = sum_νQ [sum_P (νσ|P) V^trunc_PQ] [sum_µ (λµ|Q) D_µν)]
     147           76 :       CALL get_Sigma_x(bs_env, qs_env, fm_Sigma_x_Gamma)
     148              : 
     149              :       ! Σ^c_λσ(iτ,k=0) = sum_νQ [sum_P (νσ|P) W^MIC_PQ(iτ)] [sum_µ (λµ|Q) G^occ_µν(i|τ|)], τ < 0
     150              :       ! Σ^c_λσ(iτ,k=0) = sum_νQ [sum_P (νσ|P) W^MIC_PQ(iτ)] [sum_µ (λµ|Q) G^vir_µν(i|τ|)], τ > 0
     151           76 :       CALL get_Sigma_c(bs_env, qs_env, fm_W_MIC_time, fm_Sigma_c_Gamma_time)
     152           76 :       CALL store_3c_finish(bs_env, bs_env%store_3c_W, bs_env%store_3c_Sigma)
     153              : 
     154              :       ! Σ^c_λσ(iτ,k=0) -> Σ^c_nn(ϵ,k); ϵ_nk^GW = ϵ_nk^DFT + Σ^c_nn(ϵ,k) + Σ^x_nn(k) - v^xc_nn(k)
     155           76 :       CALL compute_QP_energies(bs_env, qs_env, fm_Sigma_x_Gamma, fm_Sigma_c_Gamma_time)
     156              : 
     157           76 :       CALL de_init_bs_env(qs_env, bs_env)
     158              : 
     159           76 :       CALL timestop(handle)
     160              : 
     161           76 :    END SUBROUTINE gw_calc_tensor_large_cell_Gamma
     162              : 
     163              : ! **************************************************************************************************
     164              : !> \brief ...
     165              : !> \param bs_env ...
     166              : !> \param qs_env ...
     167              : !> \param mat_chi_Gamma_tau ...
     168              : ! **************************************************************************************************
     169           84 :    SUBROUTINE get_mat_chi_Gamma_tau(bs_env, qs_env, mat_chi_Gamma_tau)
     170              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     171              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     172              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: mat_chi_Gamma_tau
     173              : 
     174              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_mat_chi_Gamma_tau'
     175              : 
     176              :       INTEGER :: handle, i_intval_idx, i_t, inner_loop_atoms_interval_index, ispin, j_intval_idx
     177              :       INTEGER(KIND=int_8)                                :: flop
     178              :       INTEGER, DIMENSION(2)                              :: bounds_P, bounds_Q, i_atoms, IL_atoms, &
     179              :                                                             j_atoms
     180              :       INTEGER, DIMENSION(2, 2)                           :: bounds_comb
     181              :       LOGICAL                                            :: dist_too_long_i, dist_too_long_j
     182              :       REAL(KIND=dp)                                      :: t1, tau
     183         2184 :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, t_3c_x_Gocc, &
     184         1428 :                                                             t_3c_x_Gocc_2, t_3c_x_Gvir, &
     185          756 :                                                             t_3c_x_Gvir_2
     186              :       TYPE(dbt_type), POINTER                            :: t_3c_use
     187         1428 :       TYPE(dbt_type), TARGET                             :: t_3c_for_Gocc, t_3c_for_Gvir
     188              : 
     189           84 :       CALL timeset(routineN, handle)
     190              : 
     191         1520 :       DO i_t = 1, bs_env%num_time_freq_points
     192              : 
     193         1436 :          t1 = m_walltime()
     194              : 
     195         1436 :          IF (bs_env%read_chi(i_t)) THEN
     196              : 
     197            0 :             CALL fm_read(bs_env%fm_RI_RI, bs_env, bs_env%chi_name, i_t)
     198              : 
     199              :             CALL copy_fm_to_dbcsr(bs_env%fm_RI_RI, mat_chi_Gamma_tau(i_t)%matrix, &
     200            0 :                                   keep_sparsity=.FALSE.)
     201              : 
     202            0 :             IF (bs_env%unit_nr > 0) THEN
     203              :                WRITE (bs_env%unit_nr, '(T2,A,I5,A,I3,A,F10.1,A)') &
     204            0 :                   'Read χ(iτ,k=0) from file for time point  ', i_t, ' /', &
     205            0 :                   bs_env%num_time_freq_points, &
     206            0 :                   ', Execution time', m_walltime() - t1, ' s'
     207              :             END IF
     208              : 
     209              :             CYCLE
     210              : 
     211              :          END IF
     212              : 
     213         1436 :          IF (.NOT. bs_env%calc_chi(i_t)) CYCLE
     214              : 
     215              :          CALL create_tensors_chi(t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, t_3c_for_Gvir, &
     216         1436 :                                  t_3c_x_Gocc, t_3c_x_Gvir, t_3c_x_Gocc_2, t_3c_x_Gvir_2, bs_env)
     217              : 
     218              :          ! 1. compute G^occ and G^vir
     219              :          !    Background: G^σ(iτ) = G^occ,σ(iτ) * Θ(-τ) + G^vir,σ(iτ) * Θ(τ), σ ∈ {↑,↓}
     220              :          !    G^occ,σ_µλ(i|τ|,k=0) = sum_n^occ C^σ_µn(k=0) e^(-|(ϵ^σ_nk=0-ϵ_F)τ|) C^σ_λn(k=0)
     221              :          !    G^vir,σ_µλ(i|τ|,k=0) = sum_n^vir C^σ_µn(k=0) e^(-|(ϵ^σ_nk=0-ϵ_F)τ|) C^σ_λn(k=0)
     222         1436 :          tau = bs_env%time_frequency_grid%imaginary_time(i_t)
     223              : 
     224         3072 :          DO ispin = 1, bs_env%n_spin
     225         1636 :             CALL G_occ_vir(bs_env, tau, bs_env%fm_Gocc, ispin, occ=.TRUE., vir=.FALSE.)
     226         1636 :             CALL G_occ_vir(bs_env, tau, bs_env%fm_Gvir, ispin, occ=.FALSE., vir=.TRUE.)
     227              : 
     228              :             CALL cp_fm_to_dbt(bs_env%fm_Gocc, bs_env%mat_ao_ao%matrix, bs_env%mat_ao_ao_tensor%matrix, t_2c_Gocc, &
     229              :                               bs_env%para_env, bs_env%para_env_tensor%num_pe, bs_env%eps_filter, &
     230         1636 :                               bs_env%atoms_j_t_group)
     231              :             CALL cp_fm_to_dbt(bs_env%fm_Gvir, bs_env%mat_ao_ao%matrix, bs_env%mat_ao_ao_tensor%matrix, t_2c_Gvir, &
     232              :                               bs_env%para_env, bs_env%para_env_tensor%num_pe, bs_env%eps_filter, &
     233         1636 :                               bs_env%atoms_i_t_group)
     234              : 
     235              :             ! every group has its own range of i_atoms and j_atoms; only deal with a
     236              :             ! limited number of i_atom-j_atom pairs simultaneously in a group to save memory
     237         4708 :             DO i_intval_idx = 1, bs_env%n_intervals_i
     238         4908 :                DO j_intval_idx = 1, bs_env%n_intervals_j
     239         4908 :                   i_atoms = bs_env%i_atom_intervals(1:2, i_intval_idx)
     240         4908 :                   j_atoms = bs_env%j_atom_intervals(1:2, j_intval_idx)
     241              : 
     242         1636 :                   IF (bs_env%skip_chi(i_intval_idx, j_intval_idx)) THEN
     243              :                      ! Do that only after first timestep to avoid skips due to vanishing G
     244              :                      ! caused by gaps
     245           36 :                      IF (i_t == 2) THEN
     246            0 :                         bs_env%n_skip_chi = bs_env%n_skip_chi + 1
     247              :                      END IF
     248              :                      CYCLE
     249              :                   END IF
     250              : 
     251         3200 :                   DO inner_loop_atoms_interval_index = 1, bs_env%n_intervals_inner_loop_atoms
     252              : 
     253         4800 :                      IL_atoms = bs_env%inner_loop_atom_intervals(1:2, inner_loop_atoms_interval_index)
     254              :                      ! Idea: Use sparsity in 3c integrals behind χ_PQ(iτ,k=0)
     255              :                      !   ->  λ   bounds from j_atoms -> sparse in IL_atoms through σ in
     256              :                      !                                   N_Qλν(iτ) = sum_σ (Qλ|σ) G^vir_νσ(i|τ|,k=0)
     257              :                      !   ->  ν   bounds from i_atoms -> sparse in IL_atoms through µ in
     258              :                      !                                   M_Pνλ(iτ) = sum_µ (Pν|µ) G^occ_λµ(i|τ|,k=0)
     259         1600 :                      CALL check_dist(i_atoms, IL_atoms, qs_env, bs_env, dist_too_long_i)
     260         1600 :                      CALL check_dist(j_atoms, IL_atoms, qs_env, bs_env, dist_too_long_j)
     261         1600 :                      IF (.NOT. dist_too_long_i) THEN
     262              :                         ! 2. compute 3-center integrals (Pν|µ) ("|": truncated Coulomb operator)
     263              :                         CALL get_3c_integrals(qs_env, bs_env, bs_env%store_3c_chi, t_3c_for_Gocc, &
     264         1600 :                                               t_3c_use, atoms_AO_1=i_atoms, atoms_AO_2=IL_atoms)
     265              :                         ! 3. tensor operation M_Pνλ(iτ) = sum_µ (Pν|µ) G^occ_λµ(i|τ|,k=0)
     266              :                         CALL G_times_3c(t_3c_use, t_2c_Gocc, t_3c_x_Gocc, bs_env, &
     267         1600 :                                         j_atoms, i_atoms, IL_atoms)
     268         1600 :                         CALL dbt_clear(t_3c_for_Gocc)
     269              :                      END IF
     270         3200 :                      IF (.NOT. dist_too_long_j) THEN
     271              :                         ! 4. compute 3-center integrals (Qλ|σ) ("|": truncated Coulomb operator)
     272              :                         CALL get_3c_integrals(qs_env, bs_env, bs_env%store_3c_chi, t_3c_for_Gvir, &
     273         1600 :                                               t_3c_use, atoms_AO_1=j_atoms, atoms_AO_2=IL_atoms)
     274              :                         ! 5. tensor operation N_Qλν(iτ) = sum_σ (Qλ|σ) G^vir_νσ(i|τ|,k=0)
     275              :                         CALL G_times_3c(t_3c_use, t_2c_Gvir, t_3c_x_Gvir, bs_env, &
     276         1600 :                                         i_atoms, j_atoms, IL_atoms)
     277         1600 :                         CALL dbt_clear(t_3c_for_Gvir)
     278              :                      END IF
     279              :                   END DO ! IL_atoms
     280              : 
     281              :                   ! 6. reorder tensors: M_Pνλ -> M_Pλν
     282         1600 :                   CALL dbt_copy(t_3c_x_Gocc, t_3c_x_Gocc_2, move_data=.TRUE., order=[1, 3, 2])
     283         1600 :                   CALL dbt_copy(t_3c_x_Gvir, t_3c_x_Gvir_2, move_data=.TRUE.)
     284              : 
     285              :                   ! 7. tensor operation χ_PQ(iτ,k=0) = sum_λν M_Pλν(iτ) N_Qλν(iτ),
     286              :                   ! Bounds:
     287              :                   ! "comb" (combined index)
     288              :                   !   ->  λ   bounds from j_atoms
     289              :                   !   ->  ν   bounds from i_atoms
     290              :                   ! P   -> sparse in ν (see 3.)
     291              :                   ! Q   -> sparse in λ (see 5.)
     292              :                   bounds_comb(1:2, 1) = [bs_env%i_ao_start_from_atom(j_atoms(1)), &
     293         4800 :                                          bs_env%i_ao_end_from_atom(j_atoms(2))]
     294              :                   bounds_comb(1:2, 2) = [bs_env%i_ao_start_from_atom(i_atoms(1)), &
     295         4800 :                                          bs_env%i_ao_end_from_atom(i_atoms(2))]
     296              : 
     297              :                   CALL get_bounds_from_atoms(bounds_P, i_atoms, [1, bs_env%n_atom], &
     298              :                                              bs_env%min_RI_idx_from_AO_AO_atom, &
     299         4800 :                                              bs_env%max_RI_idx_from_AO_AO_atom)
     300              :                   CALL get_bounds_from_atoms(bounds_Q, [1, bs_env%n_atom], j_atoms, &
     301              :                                              bs_env%min_RI_idx_from_AO_AO_atom, &
     302         4800 :                                              bs_env%max_RI_idx_from_AO_AO_atom)
     303              : 
     304         1600 :                   IF (bounds_Q(1) > bounds_Q(2) .OR. bounds_P(1) > bounds_P(2)) THEN
     305            0 :                      flop = 0_int_8
     306              :                   ELSE
     307              :                      CALL dbt_contract(alpha=bs_env%spin_degeneracy, &
     308              :                                        tensor_1=t_3c_x_Gocc_2, tensor_2=t_3c_x_Gvir_2, &
     309              :                                        beta=1.0_dp, tensor_3=bs_env%t_chi, &
     310              :                                        contract_1=[2, 3], notcontract_1=[1], map_1=[1], &
     311              :                                        contract_2=[2, 3], notcontract_2=[1], map_2=[2], &
     312              :                                        bounds_1=bounds_comb, &
     313              :                                        bounds_2=bounds_P, &
     314              :                                        bounds_3=bounds_Q, &
     315         1600 :                                        filter_eps=bs_env%eps_filter, move_data=.FALSE., flop=flop)
     316              :                   END IF
     317         3236 :                   IF (flop == 0_int_8) bs_env%skip_chi(i_intval_idx, j_intval_idx) = .TRUE.
     318              : 
     319              :                END DO ! j_atoms
     320              :             END DO ! i_atoms
     321              :          END DO ! ispin
     322              : 
     323              :          ! 8. communicate data of χ_PQ(iτ,k=0) in tensor bs_env%t_chi (which local in the
     324              :          !    subgroup) to the global dbcsr matrix mat_chi_Gamma_tau (which stores
     325              :          !    χ_PQ(iτ,k=0) for all time points)
     326              :          CALL cp_dbt_to_dbcsr(bs_env%t_chi, bs_env%mat_RI_RI_tensor%matrix, &
     327         1436 :                               mat_chi_Gamma_tau(i_t)%matrix, bs_env%para_env)
     328              : 
     329              :          CALL fm_write(bs_env%fm_RI_RI, i_t, bs_env%chi_name, qs_env, &
     330         1436 :                        matrix=mat_chi_Gamma_tau(i_t)%matrix)
     331              : 
     332              :          CALL destroy_tensors_chi(t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, t_3c_for_Gvir, &
     333         1436 :                                   t_3c_x_Gocc, t_3c_x_Gvir, t_3c_x_Gocc_2, t_3c_x_Gvir_2)
     334              : 
     335         1520 :          IF (bs_env%unit_nr > 0) THEN
     336              :             WRITE (bs_env%unit_nr, '(T2,A,I13,A,I3,A,F10.1,A)') &
     337          718 :                'Computed χ(iτ,k=0) for time point', i_t, ' /', bs_env%num_time_freq_points, &
     338         1436 :                ', Execution time', m_walltime() - t1, ' s'
     339              :          END IF
     340              : 
     341              :       END DO ! i_t
     342              : 
     343           84 :       IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, '(A)') ' '
     344              : 
     345           84 :       CALL timestop(handle)
     346              : 
     347           84 :    END SUBROUTINE get_mat_chi_Gamma_tau
     348              : 
     349              : ! **************************************************************************************************
     350              : !> \brief ...
     351              : !> \param t_2c_Gocc ...
     352              : !> \param t_2c_Gvir ...
     353              : !> \param t_3c_for_Gocc ...
     354              : !> \param t_3c_for_Gvir ...
     355              : !> \param t_3c_x_Gocc ...
     356              : !> \param t_3c_x_Gvir ...
     357              : !> \param t_3c_x_Gocc_2 ...
     358              : !> \param t_3c_x_Gvir_2 ...
     359              : !> \param bs_env ...
     360              : ! **************************************************************************************************
     361         1436 :    SUBROUTINE create_tensors_chi(t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, t_3c_for_Gvir, &
     362              :                                  t_3c_x_Gocc, t_3c_x_Gvir, t_3c_x_Gocc_2, t_3c_x_Gvir_2, bs_env)
     363              : 
     364              :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, &
     365              :                                                             t_3c_for_Gvir, t_3c_x_Gocc, &
     366              :                                                             t_3c_x_Gvir, t_3c_x_Gocc_2, &
     367              :                                                             t_3c_x_Gvir_2
     368              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     369              : 
     370              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'create_tensors_chi'
     371              : 
     372              :       INTEGER                                            :: handle
     373              : 
     374         1436 :       CALL timeset(routineN, handle)
     375              : 
     376         1436 :       CALL dbt_create(bs_env%t_G, t_2c_Gocc, name="Gocc 2c (AO|AO)")
     377         1436 :       CALL dbt_create(bs_env%t_G, t_2c_Gvir, name="Gvir 2c (AO|AO)")
     378         1436 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_for_Gocc, name="Gocc 3c (RI AO|AO)")
     379         1436 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_for_Gvir, name="Gvir 3c (RI AO|AO)")
     380         1436 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_Gocc, name="xGocc 3c (RI AO|AO)")
     381         1436 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_Gvir, name="xGvir 3c (RI AO|AO)")
     382         1436 :       CALL dbt_create(bs_env%t_RI__AO_AO, t_3c_x_Gocc_2, name="x2Gocc 3c (RI AO|AO)")
     383         1436 :       CALL dbt_create(bs_env%t_RI__AO_AO, t_3c_x_Gvir_2, name="x2Gvir 3c (RI AO|AO)")
     384              : 
     385         1436 :       CALL timestop(handle)
     386              : 
     387         1436 :    END SUBROUTINE create_tensors_chi
     388              : 
     389              : ! **************************************************************************************************
     390              : !> \brief ...
     391              : !> \param t_2c_Gocc ...
     392              : !> \param t_2c_Gvir ...
     393              : !> \param t_3c_for_Gocc ...
     394              : !> \param t_3c_for_Gvir ...
     395              : !> \param t_3c_x_Gocc ...
     396              : !> \param t_3c_x_Gvir ...
     397              : !> \param t_3c_x_Gocc_2 ...
     398              : !> \param t_3c_x_Gvir_2 ...
     399              : ! **************************************************************************************************
     400         1436 :    SUBROUTINE destroy_tensors_chi(t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, t_3c_for_Gvir, &
     401              :                                   t_3c_x_Gocc, t_3c_x_Gvir, t_3c_x_Gocc_2, t_3c_x_Gvir_2)
     402              :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, &
     403              :                                                             t_3c_for_Gvir, t_3c_x_Gocc, &
     404              :                                                             t_3c_x_Gvir, t_3c_x_Gocc_2, &
     405              :                                                             t_3c_x_Gvir_2
     406              : 
     407              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'destroy_tensors_chi'
     408              : 
     409              :       INTEGER                                            :: handle
     410              : 
     411         1436 :       CALL timeset(routineN, handle)
     412              : 
     413         1436 :       CALL dbt_destroy(t_2c_Gocc)
     414         1436 :       CALL dbt_destroy(t_2c_Gvir)
     415         1436 :       CALL dbt_destroy(t_3c_for_Gocc)
     416         1436 :       CALL dbt_destroy(t_3c_for_Gvir)
     417         1436 :       CALL dbt_destroy(t_3c_x_Gocc)
     418         1436 :       CALL dbt_destroy(t_3c_x_Gvir)
     419         1436 :       CALL dbt_destroy(t_3c_x_Gocc_2)
     420         1436 :       CALL dbt_destroy(t_3c_x_Gvir_2)
     421              : 
     422         1436 :       CALL timestop(handle)
     423              : 
     424         1436 :    END SUBROUTINE destroy_tensors_chi
     425              : 
     426              : ! **************************************************************************************************
     427              : !> \brief ...
     428              : !> \param qs_env ...
     429              : !> \param bs_env ...
     430              : !> \param t_3c ...
     431              : !> \param atoms_AO_1 ...
     432              : !> \param atoms_AO_2 ...
     433              : !> \param atoms_RI ...
     434              : ! **************************************************************************************************
     435          418 :    SUBROUTINE compute_3c_integrals(qs_env, bs_env, t_3c, atoms_AO_1, atoms_AO_2, atoms_RI)
     436              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     437              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     438              :       TYPE(dbt_type)                                     :: t_3c
     439              :       INTEGER, DIMENSION(2), OPTIONAL                    :: atoms_AO_1, atoms_AO_2, atoms_RI
     440              : 
     441              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_3c_integrals'
     442              : 
     443              :       INTEGER                                            :: handle
     444          418 :       TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :)       :: t_3c_array
     445              : 
     446          418 :       CALL timeset(routineN, handle)
     447              : 
     448              :       ! free memory (not clear whether memory has been freed previously)
     449          418 :       CALL dbt_clear(t_3c)
     450              : 
     451         4598 :       ALLOCATE (t_3c_array(1, 1))
     452          418 :       CALL dbt_create(t_3c, t_3c_array(1, 1))
     453              : 
     454              :       CALL build_3c_integrals(t_3c_array, &
     455              :                               bs_env%eps_filter, &
     456              :                               qs_env, &
     457              :                               bs_env%nl_3c, &
     458              :                               int_eps=bs_env%eps_filter, &
     459              :                               basis_i=bs_env%basis_set_RI, &
     460              :                               basis_j=bs_env%basis_set_AO, &
     461              :                               basis_k=bs_env%basis_set_AO, &
     462              :                               potential_parameter=bs_env%ri_metric, &
     463              :                               bounds_i=atoms_RI, &
     464              :                               bounds_j=atoms_AO_1, &
     465              :                               bounds_k=atoms_AO_2, &
     466          418 :                               desymmetrize=.FALSE.)
     467              : 
     468          418 :       CALL dbt_filter(t_3c_array(1, 1), bs_env%eps_filter)
     469              : 
     470          418 :       CALL dbt_copy(t_3c_array(1, 1), t_3c, move_data=.TRUE.)
     471              : 
     472          418 :       CALL dbt_destroy(t_3c_array(1, 1))
     473          836 :       DEALLOCATE (t_3c_array)
     474              : 
     475          418 :       CALL timestop(handle)
     476              : 
     477          836 :    END SUBROUTINE compute_3c_integrals
     478              : 
     479              : ! **************************************************************************************************
     480              : !> \brief Provides the 3-center integrals of the given atom ranges. If the store is in use, a set
     481              : !>        is computed at its first use and kept: as a tensor that every use takes in place, or
     482              : !>        compressed and decompressed into t_3c at every use (also the first), so that all uses
     483              : !>        see the same integrals. Otherwise, the integrals are computed into t_3c.
     484              : !> \param qs_env ...
     485              : !> \param bs_env ...
     486              : !> \param store integrals kept for the phase (store_3c_init)
     487              : !> \param t_3c work tensor
     488              : !> \param t_3c_use the integrals: t_3c or a kept tensor (to be read only)
     489              : !> \param atoms_AO_1 ...
     490              : !> \param atoms_AO_2 ...
     491              : !> \param atoms_RI ...
     492              : ! **************************************************************************************************
     493         7738 :    SUBROUTINE get_3c_integrals(qs_env, bs_env, store, t_3c, t_3c_use, atoms_AO_1, atoms_AO_2, &
     494              :                                atoms_RI)
     495              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     496              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     497              :       TYPE(store_3c_type), INTENT(INOUT), TARGET         :: store
     498              :       TYPE(dbt_type), TARGET                             :: t_3c
     499              :       TYPE(dbt_type), POINTER                            :: t_3c_use
     500              :       INTEGER, DIMENSION(2), OPTIONAL                    :: atoms_AO_1, atoms_AO_2, atoms_RI
     501              : 
     502              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_3c_integrals'
     503              : 
     504              :       INTEGER                                            :: handle, i, iset
     505              :       INTEGER, DIMENSION(2, 3)                           :: atoms
     506              : 
     507         7738 :       CALL timeset(routineN, handle)
     508              : 
     509         7738 :       atoms = 0
     510        16814 :       IF (PRESENT(atoms_RI)) atoms(:, 1) = atoms_RI
     511        17222 :       IF (PRESENT(atoms_AO_1)) atoms(:, 2) = atoms_AO_1
     512        20130 :       IF (PRESENT(atoms_AO_2)) atoms(:, 3) = atoms_AO_2
     513              : 
     514         7738 :       t_3c_use => t_3c
     515         7738 :       iset = 0
     516         7738 :       IF (ALLOCATED(store%sets)) THEN
     517        14972 :          DO i = 1, store%nsets
     518        81374 :             IF (ALL(store%sets(i)%atoms == atoms)) iset = i
     519              :          END DO
     520         7594 :          IF (iset == 0 .AND. store%nsets < SIZE(store%sets)) THEN
     521          216 :             iset = store%nsets + 1
     522          216 :             store%nsets = iset
     523         2160 :             store%sets(iset)%atoms = atoms
     524          216 :             IF (store%uncompressed) THEN
     525          216 :                CALL dbt_create(t_3c, store%sets(iset)%t_3c)
     526              :                CALL compute_3c_integrals(qs_env, bs_env, store%sets(iset)%t_3c, atoms_AO_1, &
     527          216 :                                          atoms_AO_2, atoms_RI)
     528              :                store%memory = store%memory + &
     529          216 :                               REAL(dbt_get_nze(store%sets(iset)%t_3c), KIND=dp)*8.0_dp/1024.0_dp**2
     530              :             ELSE
     531            0 :                CALL compute_3c_integrals(qs_env, bs_env, t_3c, atoms_AO_1, atoms_AO_2, atoms_RI)
     532            0 :                CALL alloc_containers(store%sets(iset)%compressed, 1)
     533              :                CALL compress_tensor(t_3c, store%sets(iset)%blk_indices, store%sets(iset)%compressed, &
     534            0 :                                     store_3c_eps_scaling*bs_env%eps_filter, store%memory)
     535              :             END IF
     536              :          END IF
     537              :       END IF
     538              : 
     539         7594 :       IF (iset > 0) THEN
     540         7594 :          IF (store%uncompressed) THEN
     541         7594 :             t_3c_use => store%sets(iset)%t_3c
     542              :          ELSE
     543            0 :             CALL dbt_clear(t_3c)
     544              :             CALL decompress_tensor(t_3c, store%sets(iset)%blk_indices, store%sets(iset)%compressed, &
     545            0 :                                    store_3c_eps_scaling*bs_env%eps_filter)
     546              :          END IF
     547              :       ELSE
     548          144 :          CALL compute_3c_integrals(qs_env, bs_env, t_3c, atoms_AO_1, atoms_AO_2, atoms_RI)
     549              :       END IF
     550              : 
     551         7738 :       CALL timestop(handle)
     552              : 
     553         7738 :    END SUBROUTINE get_3c_integrals
     554              : 
     555              : ! **************************************************************************************************
     556              : !> \brief Puts a store of 3-center integrals in use if the integrals of the group's i and j atoms
     557              : !>        (estimated uncompressed) fit into a share of memory: compressed into
     558              : !>        store_3c_share_compressed of MEMORY_PER_PROC, uncompressed into
     559              : !>        store_3c_share_uncompressed of MEMORY_PER_PROC and of the free memory.
     560              : !> \param bs_env ...
     561              : !> \param store ...
     562              : !> \param n_intervals number of atom intervals the integral sets are keyed by (besides the
     563              : !>        inner loop intervals)
     564              : ! **************************************************************************************************
     565          228 :    SUBROUTINE store_3c_init(bs_env, store, n_intervals)
     566              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     567              :       TYPE(store_3c_type), INTENT(INOUT)                 :: store
     568              :       INTEGER, INTENT(IN)                                :: n_intervals
     569              : 
     570              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'store_3c_init'
     571              : 
     572              :       INTEGER                                            :: handle
     573              :       REAL(KIND=dp)                                      :: free_GB, memory_GB
     574              : 
     575          228 :       CALL timeset(routineN, handle)
     576              : 
     577          228 :       CALL store_3c_release(store)
     578              : 
     579              :       ! free memory per process (zero if not detected), the same on all processes
     580          228 :       CALL mp_mem_avail_per_rank_GB(bs_env%para_env, free_GB)
     581              : 
     582              :       ! non-zero (µν|P) of the group's i and j atoms per process
     583              :       memory_GB = 8.0E-9_dp*bs_env%occupation_3c_int*REAL(bs_env%n_RI, KIND=dp)* &
     584              :                   REAL(bs_env%n_ao, KIND=dp)**2*REAL(bs_env%n_atom_i + bs_env%n_atom_j, KIND=dp)/ &
     585          228 :                   REAL(bs_env%n_atom, KIND=dp)/REAL(bs_env%para_env_tensor%num_pe, KIND=dp)
     586              : 
     587              :       ! a negative estimate is an unknown occupation (not kept)
     588          228 :       IF (0.0_dp <= memory_GB .AND. memory_GB <= store_3c_share_compressed*bs_env%input_memory_per_proc_GB) THEN
     589       487084 :          ALLOCATE (store%sets(n_intervals*bs_env%n_intervals_inner_loop_atoms))
     590              :          store%uncompressed = (memory_GB <= store_3c_share_uncompressed* &
     591          228 :                                MIN(bs_env%input_memory_per_proc_GB, free_GB))
     592              :       END IF
     593              : 
     594          228 :       CALL timestop(handle)
     595              : 
     596          228 :    END SUBROUTINE store_3c_init
     597              : 
     598              : ! **************************************************************************************************
     599              : !> \brief Prints the memory the kept 3-center integrals took and releases them.
     600              : !> \param bs_env ...
     601              : !> \param store ...
     602              : !> \param store_2 a second store of the same phase
     603              : ! **************************************************************************************************
     604          152 :    SUBROUTINE store_3c_finish(bs_env, store, store_2)
     605              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     606              :       TYPE(store_3c_type), INTENT(INOUT)                 :: store
     607              :       TYPE(store_3c_type), INTENT(INOUT), OPTIONAL       :: store_2
     608              : 
     609              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'store_3c_finish'
     610              : 
     611              :       INTEGER                                            :: handle
     612              :       LOGICAL                                            :: kept, uncompressed
     613              :       REAL(KIND=dp)                                      :: memory
     614              : 
     615          152 :       CALL timeset(routineN, handle)
     616              : 
     617          152 :       kept = ALLOCATED(store%sets)
     618          152 :       uncompressed = store%uncompressed
     619          152 :       memory = store%memory
     620          152 :       IF (PRESENT(store_2)) memory = memory + store_2%memory
     621          152 :       CALL bs_env%para_env%max(memory)
     622              : 
     623          152 :       IF (bs_env%unit_nr > 0) THEN
     624           76 :          IF (kept .AND. uncompressed) THEN
     625              :             WRITE (bs_env%unit_nr, '(T2,A,F19.1,A)') &
     626           76 :                'Kept 3-center integrals (µν|P), uncompressed, per process', memory/1024.0_dp, ' GB'
     627            0 :          ELSE IF (kept) THEN
     628              :             WRITE (bs_env%unit_nr, '(T2,A,F21.1,A)') &
     629            0 :                'Kept 3-center integrals (µν|P), compressed, per process', memory/1024.0_dp, ' GB'
     630              :          ELSE
     631              :             WRITE (bs_env%unit_nr, '(T2,A,A24)') &
     632            0 :                'Kept 3-center integrals (µν|P), compressed, per process', 'no'
     633              :          END IF
     634              :       END IF
     635              : 
     636          152 :       CALL store_3c_release(store)
     637          152 :       IF (PRESENT(store_2)) CALL store_3c_release(store_2)
     638              : 
     639          152 :       CALL timestop(handle)
     640              : 
     641          152 :    END SUBROUTINE store_3c_finish
     642              : 
     643              : ! **************************************************************************************************
     644              : !> \brief ...
     645              : !> \param t_3c_for_G ...
     646              : !> \param t_G ...
     647              : !> \param t_M ...
     648              : !> \param bs_env ...
     649              : !> \param atoms_AO_1 ...
     650              : !> \param atoms_AO_2 ...
     651              : !> \param atoms_IL ...
     652              : ! **************************************************************************************************
     653         3200 :    SUBROUTINE G_times_3c(t_3c_for_G, t_G, t_M, bs_env, atoms_AO_1, atoms_AO_2, atoms_IL)
     654              :       TYPE(dbt_type)                                     :: t_3c_for_G, t_G, t_M
     655              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     656              :       INTEGER, DIMENSION(2)                              :: atoms_AO_1, atoms_AO_2, atoms_IL
     657              : 
     658              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'G_times_3c'
     659              : 
     660              :       INTEGER                                            :: handle
     661              :       INTEGER(KIND=int_8)                                :: flop
     662              :       INTEGER, DIMENSION(2)                              :: bounds_ao_1, bounds_IL
     663              :       INTEGER, DIMENSION(2, 2)                           :: bounds_comb
     664              : 
     665         3200 :       CALL timeset(routineN, handle)
     666              : 
     667              :       ! Bounds reduce needed memory and therefore scaling behavior
     668              :       ! Operations are of the form, e.g, M_Pνλ = sum_µ (Pν|µ) G_λµ
     669              :       ! "comb" (combined index)
     670              :       !   ->  P   sparse in ν and µ
     671              :       !   ->  λ   bounds from j_atoms (via atoms_AO_1)
     672              :       ! µ   bounds from inner loop "IL" indices and sparse in P and ν
     673              :       ! ν   bounds from i_atoms (via atoms_AO_2) and sparse in P and µ
     674              : 
     675              :       ! µ index
     676              :       CALL get_bounds_from_atoms(bounds_IL, [1, bs_env%n_atom], atoms_AO_2, &
     677              :                                  bs_env%min_AO_idx_from_RI_AO_atom, &
     678              :                                  bs_env%max_AO_idx_from_RI_AO_atom, &
     679              :                                  atoms_3=atoms_IL, &
     680              :                                  indices_3_start=bs_env%i_ao_start_from_atom, &
     681         9600 :                                  indices_3_end=bs_env%i_ao_end_from_atom)
     682              : 
     683              :       ! P index
     684              :       CALL get_bounds_from_atoms(bounds_comb(:, 1), atoms_IL, atoms_AO_2, &
     685              :                                  bs_env%min_RI_idx_from_AO_AO_atom, &
     686         3200 :                                  bs_env%max_RI_idx_from_AO_AO_atom)
     687              : 
     688              :       ! ν index
     689              :       CALL get_bounds_from_atoms(bounds_comb(:, 2), [1, bs_env%n_atom], atoms_IL, &
     690              :                                  bs_env%min_AO_idx_from_RI_AO_atom, &
     691              :                                  bs_env%max_AO_idx_from_RI_AO_atom, &
     692              :                                  atoms_3=atoms_AO_2, &
     693              :                                  indices_3_start=bs_env%i_ao_start_from_atom, &
     694         9600 :                                  indices_3_end=bs_env%i_ao_end_from_atom)
     695              : 
     696              :       ! λ index
     697              :       bounds_ao_1(1:2) = [bs_env%i_ao_start_from_atom(atoms_AO_1(1)), &
     698         9600 :                           bs_env%i_ao_end_from_atom(atoms_AO_1(2))]
     699              : 
     700         3200 :       IF (bounds_IL(1) > bounds_IL(2) .OR. bounds_comb(1, 2) > bounds_comb(2, 2)) THEN
     701              :          flop = 0_int_8
     702              :       ELSE
     703              :          CALL dbt_contract(alpha=1.0_dp, &
     704              :                            tensor_1=t_3c_for_G, &
     705              :                            tensor_2=t_G, &
     706              :                            beta=1.0_dp, &
     707              :                            tensor_3=t_M, &
     708              :                            contract_1=[3], notcontract_1=[1, 2], map_1=[1, 2], &
     709              :                            contract_2=[2], notcontract_2=[1], map_2=[3], &
     710              :                            bounds_1=bounds_IL, &
     711              :                            bounds_2=bounds_comb, &
     712              :                            bounds_3=bounds_ao_1, &
     713              :                            flop=flop, &
     714         3200 :                            filter_eps=bs_env%eps_filter)
     715              :       END IF
     716              : 
     717         3200 :       CALL timestop(handle)
     718              : 
     719         3200 :    END SUBROUTINE G_times_3c
     720              : 
     721              : ! **************************************************************************************************
     722              : !> \brief ...
     723              : !> \param atoms_1 ...
     724              : !> \param atoms_2 ...
     725              : !> \param qs_env ...
     726              : !> \param bs_env ...
     727              : !> \param dist_too_long ...
     728              : ! **************************************************************************************************
     729         3200 :    SUBROUTINE check_dist(atoms_1, atoms_2, qs_env, bs_env, dist_too_long)
     730              :       INTEGER, DIMENSION(2)                              :: atoms_1, atoms_2
     731              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     732              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     733              :       LOGICAL                                            :: dist_too_long
     734              : 
     735              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'check_dist'
     736              : 
     737              :       INTEGER                                            :: atom_1, atom_2, handle
     738              :       REAL(dp)                                           :: abs_rab, min_dist_AO_atoms
     739              :       REAL(KIND=dp), DIMENSION(3)                        :: rab
     740              :       TYPE(cell_type), POINTER                           :: cell
     741         3200 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     742              : 
     743         3200 :       CALL timeset(routineN, handle)
     744              : 
     745         3200 :       CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set)
     746              : 
     747         3200 :       min_dist_AO_atoms = HUGE(1.0_dp)
     748        10096 :       DO atom_1 = atoms_1(1), atoms_1(2)
     749        25376 :          DO atom_2 = atoms_2(1), atoms_2(2)
     750        15280 :             rab = pbc(particle_set(atom_1)%r(1:3), particle_set(atom_2)%r(1:3), cell)
     751              : 
     752        15280 :             abs_rab = SQRT(rab(1)**2 + rab(2)**2 + rab(3)**2)
     753              : 
     754        22176 :             min_dist_AO_atoms = MIN(min_dist_AO_atoms, abs_rab)
     755              :          END DO
     756              :       END DO
     757              : 
     758         3200 :       dist_too_long = (min_dist_AO_atoms > bs_env%max_dist_AO_atoms)
     759              : 
     760         3200 :       CALL timestop(handle)
     761              : 
     762         3200 :    END SUBROUTINE check_dist
     763              : 
     764              : ! **************************************************************************************************
     765              : !> \brief ...
     766              : !> \param bs_env ...
     767              : !> \param qs_env ...
     768              : !> \param fm_Sigma_x_Gamma ...
     769              : ! **************************************************************************************************
     770           84 :    SUBROUTINE get_Sigma_x(bs_env, qs_env, fm_Sigma_x_Gamma)
     771              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     772              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     773              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_Sigma_x_Gamma
     774              : 
     775              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_Sigma_x'
     776              : 
     777              :       INTEGER                                            :: handle, ispin
     778              : 
     779           84 :       CALL timeset(routineN, handle)
     780              : 
     781          348 :       ALLOCATE (fm_Sigma_x_Gamma(bs_env%n_spin))
     782          180 :       DO ispin = 1, bs_env%n_spin
     783          180 :          CALL cp_fm_create(fm_Sigma_x_Gamma(ispin), bs_env%fm_s_Gamma%matrix_struct)
     784              :       END DO
     785              : 
     786           84 :       IF (bs_env%Sigma_x_exists) THEN
     787           14 :          DO ispin = 1, bs_env%n_spin
     788           14 :             CALL fm_read(fm_Sigma_x_Gamma(ispin), bs_env, bs_env%Sigma_x_name, ispin)
     789              :          END DO
     790              :       ELSE
     791           78 :          CALL compute_Sigma_x(bs_env, qs_env, fm_Sigma_x_Gamma)
     792              :       END IF
     793              : 
     794           84 :       CALL timestop(handle)
     795              : 
     796           84 :    END SUBROUTINE get_Sigma_x
     797              : 
     798              : ! **************************************************************************************************
     799              : !> \brief ...
     800              : !> \param bs_env ...
     801              : !> \param qs_env ...
     802              : !> \param fm_Sigma_x_Gamma ...
     803              : ! **************************************************************************************************
     804           78 :    SUBROUTINE compute_Sigma_x(bs_env, qs_env, fm_Sigma_x_Gamma)
     805              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     806              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     807              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_Sigma_x_Gamma
     808              : 
     809              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'compute_Sigma_x'
     810              : 
     811              :       INTEGER                                            :: handle, i_intval_idx, ispin, j_intval_idx
     812              :       INTEGER, DIMENSION(2)                              :: i_atoms, j_atoms
     813              :       REAL(KIND=dp)                                      :: t1
     814           78 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_Vtr_Gamma
     815              :       TYPE(dbcsr_type)                                   :: mat_Sigma_x_Gamma
     816         2574 :       TYPE(dbt_type)                                     :: t_2c_D, t_2c_Sigma_x, t_2c_V, t_3c_x_V
     817              : 
     818           78 :       CALL timeset(routineN, handle)
     819              : 
     820           78 :       t1 = m_walltime()
     821              : 
     822           78 :       CALL dbt_create(bs_env%t_G, t_2c_D)
     823           78 :       CALL dbt_create(bs_env%t_W, t_2c_V)
     824           78 :       CALL dbt_create(bs_env%t_G, t_2c_Sigma_x)
     825           78 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_V)
     826           78 :       CALL dbcsr_create(mat_Sigma_x_Gamma, template=bs_env%mat_ao_ao%matrix)
     827              : 
     828              :       ! 1. Compute truncated Coulomb operator matrix V^tr(k=0) (cutoff rad: cellsize/2)
     829              :       CALL RI_2c_integral_mat(qs_env, fm_Vtr_Gamma, bs_env%fm_RI_RI%matrix_struct, bs_env%n_RI, &
     830           78 :                               bs_env%trunc_coulomb)
     831              : 
     832              :       ! M^-1(k=0) V^tr M^-1(k=0) -> fm_Vtr_Gamma
     833           78 :       CALL fm_contract_ABA(bs_env%fm_Minv_Gamma, fm_Vtr_Gamma(:, 1))
     834              : 
     835          166 :       DO ispin = 1, bs_env%n_spin
     836              : 
     837              :          ! 3. Compute density matrix D_µν
     838           88 :          CALL G_occ_vir(bs_env, 0.0_dp, bs_env%fm_work_mo(2), ispin, occ=.TRUE., vir=.FALSE.)
     839              : 
     840              :          CALL cp_fm_to_dbt(bs_env%fm_work_mo(2), bs_env%mat_ao_ao%matrix, bs_env%mat_ao_ao_tensor%matrix, t_2c_D, &
     841              :                            bs_env%para_env, bs_env%para_env_tensor%num_pe, bs_env%eps_filter, &
     842           88 :                            bs_env%atoms_i_t_group)
     843              : 
     844              :          CALL cp_fm_to_dbt(fm_Vtr_Gamma(1, 1), bs_env%mat_RI_RI%matrix, bs_env%mat_RI_RI_tensor%matrix, t_2c_V, &
     845              :                            bs_env%para_env, bs_env%para_env_tensor%num_pe, bs_env%eps_filter, &
     846           88 :                            bs_env%atoms_j_t_group)
     847              : 
     848              :          ! every group has its own range of i_atoms and j_atoms; only deal with a
     849              :          ! limited number of i_atom-j_atom pairs simultaneously in a group to save memory
     850          176 :          DO i_intval_idx = 1, bs_env%n_intervals_i
     851          264 :             DO j_intval_idx = 1, bs_env%n_intervals_j
     852          264 :                i_atoms = bs_env%i_atom_intervals(1:2, i_intval_idx)
     853          264 :                j_atoms = bs_env%j_atom_intervals(1:2, j_intval_idx)
     854              : 
     855              :                ! 4. compute 3-center integrals (µν|P) ("|": truncated Coulomb operator)
     856              :                ! 5. M_Qνσ(iτ) = sum_P (νσ|P) (M^-1(k=0)*V^tr(k=0)*M^-1(k=0))_QP(iτ)
     857           88 :                CALL compute_3c_and_contract_W(qs_env, bs_env, i_atoms, j_atoms, t_3c_x_V, t_2c_V)
     858              : 
     859              :                ! 6. tensor operations with D and computation of Σ^x
     860              :                !    Σ^x_λσ(k=0) = sum_νQ M_Qνσ(iτ) sum_µ (Qλ|µ) D_νµ
     861              :                CALL contract_to_Sigma(t_2c_D, t_3c_x_V, t_2c_Sigma_x, i_atoms, j_atoms, &
     862          176 :                                       qs_env, bs_env, occ=.TRUE., vir=.FALSE.)
     863              : 
     864              :             END DO ! j_atoms
     865              :          END DO ! i_atoms
     866              : 
     867              :          CALL cp_dbt_to_dbcsr(t_2c_Sigma_x, bs_env%mat_ao_ao_tensor%matrix, &
     868           88 :                               mat_Sigma_x_Gamma, bs_env%para_env)
     869              : 
     870              :          CALL fm_write(bs_env%fm_work_mo(1), ispin, bs_env%Sigma_x_name, qs_env, &
     871           88 :                        matrix=mat_Sigma_x_Gamma)
     872              : 
     873          166 :          CALL copy_dbcsr_to_fm(mat_Sigma_x_Gamma, fm_Sigma_x_Gamma(ispin))
     874              : 
     875              :       END DO ! ispin
     876              : 
     877           78 :       IF (bs_env%unit_nr > 0) THEN
     878              :          WRITE (bs_env%unit_nr, '(T2,A,T55,A,F10.1,A)') &
     879           39 :             'Computed Σ^x(k=0),', ' Execution time', m_walltime() - t1, ' s'
     880           39 :          WRITE (bs_env%unit_nr, '(A)') ' '
     881              :       END IF
     882              : 
     883           78 :       CALL dbcsr_release(mat_Sigma_x_Gamma)
     884           78 :       CALL dbt_destroy(t_2c_D)
     885           78 :       CALL dbt_destroy(t_2c_V)
     886           78 :       CALL dbt_destroy(t_2c_Sigma_x)
     887           78 :       CALL dbt_destroy(t_3c_x_V)
     888           78 :       CALL cp_fm_release(fm_Vtr_Gamma)
     889              : 
     890           78 :       CALL timestop(handle)
     891              : 
     892          156 :    END SUBROUTINE compute_Sigma_x
     893              : 
     894              : ! **************************************************************************************************
     895              : !> \brief ...
     896              : !> \param bs_env ...
     897              : !> \param qs_env ...
     898              : !> \param fm_W_MIC_time ...
     899              : !> \param fm_Sigma_c_Gamma_time ...
     900              : ! **************************************************************************************************
     901           76 :    SUBROUTINE get_Sigma_c(bs_env, qs_env, fm_W_MIC_time, fm_Sigma_c_Gamma_time)
     902              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     903              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     904              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_W_MIC_time
     905              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :, :)  :: fm_Sigma_c_Gamma_time
     906              : 
     907              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_Sigma_c'
     908              : 
     909              :       INTEGER                                            :: handle, i_t, ispin, read_write_index
     910              :       REAL(KIND=dp)                                      :: t1, tau
     911           76 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: mat_Sigma_neg_tau, mat_Sigma_pos_tau
     912         1292 :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, &
     913          684 :                                                             t_2c_Sigma_neg_tau, &
     914         1900 :                                                             t_2c_Sigma_pos_tau, t_2c_W, t_3c_x_W
     915              : 
     916           76 :       CALL timeset(routineN, handle)
     917              : 
     918              :       CALL create_mat_for_Sigma_c(bs_env, t_2c_Gocc, t_2c_Gvir, t_2c_W, t_2c_Sigma_neg_tau, &
     919              :                                   t_2c_Sigma_pos_tau, t_3c_x_W, &
     920           76 :                                   mat_Sigma_neg_tau, mat_Sigma_pos_tau)
     921              : 
     922         1460 :       DO i_t = 1, bs_env%num_time_freq_points
     923              : 
     924         3044 :          DO ispin = 1, bs_env%n_spin
     925              : 
     926         1584 :             t1 = m_walltime()
     927              : 
     928         1584 :             read_write_index = i_t + (ispin - 1)*bs_env%num_time_freq_points
     929              : 
     930              :             ! read self-energy from restart
     931         1584 :             IF (bs_env%Sigma_c_exists(i_t, ispin)) THEN
     932          120 :                CALL fm_read(bs_env%fm_work_mo(1), bs_env, bs_env%Sigma_p_name, read_write_index)
     933              :                CALL copy_fm_to_dbcsr(bs_env%fm_work_mo(1), mat_Sigma_pos_tau(i_t, ispin)%matrix, &
     934          120 :                                      keep_sparsity=.FALSE.)
     935          120 :                CALL fm_read(bs_env%fm_work_mo(1), bs_env, bs_env%Sigma_n_name, read_write_index)
     936              :                CALL copy_fm_to_dbcsr(bs_env%fm_work_mo(1), mat_Sigma_neg_tau(i_t, ispin)%matrix, &
     937          120 :                                      keep_sparsity=.FALSE.)
     938          120 :                IF (bs_env%unit_nr > 0) THEN
     939           60 :                   WRITE (bs_env%unit_nr, '(T2,2A,I3,A,I3,A,F10.1,A)') 'Read Σ^c(iτ,k=0) ', &
     940           60 :                      'from file for time point  ', i_t, ' /', bs_env%num_time_freq_points, &
     941          120 :                      ', Execution time', m_walltime() - t1, ' s'
     942              :                END IF
     943              : 
     944              :                CYCLE
     945              : 
     946              :             END IF
     947              : 
     948         1464 :             tau = bs_env%time_frequency_grid%imaginary_time(i_t)
     949              : 
     950              :             CALL compute_Sigma_c_occ_vir(bs_env, qs_env, tau, ispin, fm_W_MIC_time(i_t), &
     951              :                                          t_2c_Gocc, t_2c_Gvir, t_2c_W, t_2c_Sigma_neg_tau, &
     952              :                                          t_2c_Sigma_pos_tau, t_3c_x_W, &
     953              :                                          mat_Sigma_neg_tau(i_t, ispin)%matrix, &
     954              :                                          mat_Sigma_pos_tau(i_t, ispin)%matrix, &
     955         1464 :                                          count_skipped=(i_t == 2))
     956              : 
     957              :             CALL fm_write(bs_env%fm_work_mo(1), read_write_index, bs_env%Sigma_p_name, qs_env, &
     958         1464 :                           matrix=mat_Sigma_pos_tau(i_t, ispin)%matrix)
     959              :             CALL fm_write(bs_env%fm_work_mo(1), read_write_index, bs_env%Sigma_n_name, qs_env, &
     960         1464 :                           matrix=mat_Sigma_neg_tau(i_t, ispin)%matrix)
     961              : 
     962         2848 :             IF (bs_env%unit_nr > 0) THEN
     963              :                WRITE (bs_env%unit_nr, '(T2,A,I10,A,I3,A,F10.1,A)') &
     964          732 :                   'Computed Σ^c(iτ,k=0) for time point ', i_t, ' /', bs_env%num_time_freq_points, &
     965         1464 :                   ', Execution time', m_walltime() - t1, ' s'
     966              :             END IF
     967              : 
     968              :          END DO ! ispin
     969              : 
     970              :       END DO ! i_t
     971              : 
     972           76 :       IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, '(A)') ' '
     973              : 
     974              :       CALL fill_fm_Sigma_c_Gamma_time(fm_Sigma_c_Gamma_time, bs_env, &
     975           76 :                                       mat_Sigma_pos_tau, mat_Sigma_neg_tau)
     976              : 
     977           76 :       CALL print_skipping(bs_env)
     978              : 
     979              :       CALL destroy_mat_Sigma_c(t_2c_Gocc, t_2c_Gvir, t_2c_W, t_2c_Sigma_neg_tau, &
     980              :                                t_2c_Sigma_pos_tau, t_3c_x_W, fm_W_MIC_time, &
     981           76 :                                mat_Sigma_neg_tau, mat_Sigma_pos_tau)
     982              : 
     983           76 :       CALL delete_unnecessary_files(bs_env)
     984              : 
     985           76 :       CALL timestop(handle)
     986              : 
     987          152 :    END SUBROUTINE get_Sigma_c
     988              : 
     989              : ! **************************************************************************************************
     990              : !> \brief ...
     991              : !> \brief Occupied and virtual parts of the correlation self-energy at one imaginary time τ
     992              : !>          Σ^neg_λσ(iτ) = -sum_νQ [sum_P (νσ|P) W_PQ] [sum_µ (λµ|Q) G^occ_µν(i|τ|)]
     993              : !>          Σ^pos_λσ(iτ) =  sum_νQ [sum_P (νσ|P) W_PQ] [sum_µ (λµ|Q) G^vir_µν(i|τ|)]
     994              : !>        (G0W0: W = W^MIC(iτ); static COHSEX: W = W^MIC(iω=0))
     995              : !> \param bs_env ...
     996              : !> \param qs_env ...
     997              : !> \param tau imaginary time τ
     998              : !> \param ispin ...
     999              : !> \param fm_W W^MIC_PQ
    1000              : !> \param t_2c_Gocc ...
    1001              : !> \param t_2c_Gvir ...
    1002              : !> \param t_2c_W ...
    1003              : !> \param t_2c_Sigma_neg work tensor for Σ^neg
    1004              : !> \param t_2c_Sigma_pos work tensor for Σ^pos
    1005              : !> \param t_3c_x_W ...
    1006              : !> \param mat_Sigma_neg Σ^neg_λσ(iτ) on exit
    1007              : !> \param mat_Sigma_pos Σ^pos_λσ(iτ) on exit
    1008              : !> \param count_skipped count the skipped atom-interval pairs (for print_skipping)
    1009              : ! **************************************************************************************************
    1010         1472 :    SUBROUTINE compute_Sigma_c_occ_vir(bs_env, qs_env, tau, ispin, fm_W, t_2c_Gocc, t_2c_Gvir, &
    1011              :                                       t_2c_W, t_2c_Sigma_neg, t_2c_Sigma_pos, t_3c_x_W, &
    1012              :                                       mat_Sigma_neg, mat_Sigma_pos, count_skipped)
    1013              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1014              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1015              :       REAL(KIND=dp)                                      :: tau
    1016              :       INTEGER                                            :: ispin
    1017              :       TYPE(cp_fm_type)                                   :: fm_W
    1018              :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, t_2c_W, &
    1019              :                                                             t_2c_Sigma_neg, t_2c_Sigma_pos, &
    1020              :                                                             t_3c_x_W
    1021              :       TYPE(dbcsr_type)                                   :: mat_Sigma_neg, mat_Sigma_pos
    1022              :       LOGICAL                                            :: count_skipped
    1023              : 
    1024              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_Sigma_c_occ_vir'
    1025              : 
    1026              :       INTEGER                                            :: handle, i_intval_idx, j_intval_idx
    1027              :       INTEGER, DIMENSION(2)                              :: i_atoms, j_atoms
    1028              : 
    1029         1472 :       CALL timeset(routineN, handle)
    1030              : 
    1031         1472 :       CALL G_occ_vir(bs_env, tau, bs_env%fm_Gocc, ispin, occ=.TRUE., vir=.FALSE.)
    1032         1472 :       CALL G_occ_vir(bs_env, tau, bs_env%fm_Gvir, ispin, occ=.FALSE., vir=.TRUE.)
    1033              : 
    1034              :       ! fm G^occ, G^vir and W to local tensor
    1035              :       CALL cp_fm_to_dbt(bs_env%fm_Gocc, bs_env%mat_ao_ao%matrix, bs_env%mat_ao_ao_tensor%matrix, t_2c_Gocc, &
    1036              :                         bs_env%para_env, bs_env%para_env_tensor%num_pe, bs_env%eps_filter, &
    1037         1472 :                         bs_env%atoms_i_t_group)
    1038              :       CALL cp_fm_to_dbt(bs_env%fm_Gvir, bs_env%mat_ao_ao%matrix, bs_env%mat_ao_ao_tensor%matrix, t_2c_Gvir, &
    1039              :                         bs_env%para_env, bs_env%para_env_tensor%num_pe, bs_env%eps_filter, &
    1040         1472 :                         bs_env%atoms_i_t_group)
    1041              :       CALL cp_fm_to_dbt(fm_W, bs_env%mat_RI_RI%matrix, bs_env%mat_RI_RI_tensor%matrix, t_2c_W, &
    1042              :                         bs_env%para_env, bs_env%para_env_tensor%num_pe, bs_env%eps_filter, &
    1043         1472 :                         bs_env%atoms_j_t_group)
    1044              : 
    1045              :       ! every group has its own range of i_atoms and j_atoms; only deal with a
    1046              :       ! limited number of i_atom-j_atom pairs simultaneously in a group to save memory
    1047         2944 :       DO i_intval_idx = 1, bs_env%n_intervals_i
    1048         4416 :          DO j_intval_idx = 1, bs_env%n_intervals_j
    1049         4416 :             i_atoms = bs_env%i_atom_intervals(1:2, i_intval_idx)
    1050         4416 :             j_atoms = bs_env%j_atom_intervals(1:2, j_intval_idx)
    1051              : 
    1052         1472 :             IF (bs_env%skip_Sigma_occ(i_intval_idx, j_intval_idx) .AND. &
    1053              :                 bs_env%skip_Sigma_vir(i_intval_idx, j_intval_idx)) THEN
    1054              :                ! Do that only after first timestep to avoid skips due to vanishing G
    1055              :                ! caused by gaps
    1056           18 :                IF (count_skipped) THEN
    1057            0 :                   bs_env%n_skip_sigma = bs_env%n_skip_sigma + 1
    1058              :                END IF
    1059              :                CYCLE
    1060              :             END IF
    1061              : 
    1062              :             ! 1. compute 3-center integrals (µν|P) ("|": truncated Coulomb operator)
    1063              :             ! 2. tensor operation M_Qνσ(iτ) = sum_P (νσ|P) W^MIC_QP(iτ)
    1064         1454 :             CALL compute_3c_and_contract_W(qs_env, bs_env, i_atoms, j_atoms, t_3c_x_W, t_2c_W)
    1065              : 
    1066              :             ! 3. Σ_λσ(iτ,k=0) = sum_νQ M_Qνσ(iτ) sum_µ (Qλ|µ) G^occ_νµ(i|τ|) for τ < 0
    1067              :             !    (recall M_Qνσ(iτ) = M_Qνσ(-iτ) because W^MIC_PQ(iτ) = W^MIC_PQ(-iτ) )
    1068              :             CALL contract_to_Sigma(t_2c_Gocc, t_3c_x_W, t_2c_Sigma_neg, i_atoms, j_atoms, &
    1069              :                                    qs_env, bs_env, occ=.TRUE., vir=.FALSE., &
    1070         1454 :                                    can_skip=bs_env%skip_Sigma_occ(i_intval_idx, j_intval_idx))
    1071              : 
    1072              :             !    Σ_λσ(iτ,k=0) = sum_νQ M_Qνσ(iτ) sum_µ (Qλ|µ) G^vir_νµ(i|τ|) for τ > 0
    1073              :             CALL contract_to_Sigma(t_2c_Gvir, t_3c_x_W, t_2c_Sigma_pos, i_atoms, j_atoms, &
    1074              :                                    qs_env, bs_env, occ=.FALSE., vir=.TRUE., &
    1075         2926 :                                    can_skip=bs_env%skip_Sigma_vir(i_intval_idx, j_intval_idx))
    1076              : 
    1077              :          END DO ! j_atoms
    1078              :       END DO ! i_atoms
    1079              : 
    1080              :       ! 4. communicate data tensor t_2c_Sigma (which is local in the subgroup)
    1081              :       !    to the global dbcsr matrix mat_Sigma_pos/neg_tau (which stores Σ for all iτ)
    1082              :       CALL cp_dbt_to_dbcsr(t_2c_Sigma_neg, bs_env%mat_ao_ao_tensor%matrix, &
    1083         1472 :                            mat_Sigma_neg, bs_env%para_env)
    1084              :       CALL cp_dbt_to_dbcsr(t_2c_Sigma_pos, bs_env%mat_ao_ao_tensor%matrix, &
    1085         1472 :                            mat_Sigma_pos, bs_env%para_env)
    1086              : 
    1087         1472 :       CALL timestop(handle)
    1088              : 
    1089         1472 :    END SUBROUTINE compute_Sigma_c_occ_vir
    1090              : 
    1091              : ! **************************************************************************************************
    1092              : !> \brief ...
    1093              : !> \param bs_env ...
    1094              : !> \param t_2c_Gocc ...
    1095              : !> \param t_2c_Gvir ...
    1096              : !> \param t_2c_W ...
    1097              : !> \param t_2c_Sigma_neg_tau ...
    1098              : !> \param t_2c_Sigma_pos_tau ...
    1099              : !> \param t_3c_x_W ...
    1100              : !> \param mat_Sigma_neg_tau ...
    1101              : !> \param mat_Sigma_pos_tau ...
    1102              : ! **************************************************************************************************
    1103           84 :    SUBROUTINE create_mat_for_Sigma_c(bs_env, t_2c_Gocc, t_2c_Gvir, t_2c_W, t_2c_Sigma_neg_tau, &
    1104              :                                      t_2c_Sigma_pos_tau, t_3c_x_W, &
    1105              :                                      mat_Sigma_neg_tau, mat_Sigma_pos_tau)
    1106              : 
    1107              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1108              :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, t_2c_W, &
    1109              :                                                             t_2c_Sigma_neg_tau, &
    1110              :                                                             t_2c_Sigma_pos_tau, t_3c_x_W
    1111              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: mat_Sigma_neg_tau, mat_Sigma_pos_tau
    1112              : 
    1113              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'create_mat_for_Sigma_c'
    1114              : 
    1115              :       INTEGER                                            :: handle, i_t, ispin
    1116              : 
    1117           84 :       CALL timeset(routineN, handle)
    1118              : 
    1119           84 :       CALL dbt_create(bs_env%t_G, t_2c_Gocc)
    1120           84 :       CALL dbt_create(bs_env%t_G, t_2c_Gvir)
    1121           84 :       CALL dbt_create(bs_env%t_W, t_2c_W)
    1122           84 :       CALL dbt_create(bs_env%t_G, t_2c_Sigma_neg_tau)
    1123           84 :       CALL dbt_create(bs_env%t_G, t_2c_Sigma_pos_tau)
    1124           84 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_W)
    1125              : 
    1126           84 :       NULLIFY (mat_Sigma_neg_tau, mat_Sigma_pos_tau)
    1127         2068 :       ALLOCATE (mat_Sigma_neg_tau(bs_env%num_time_freq_points, bs_env%n_spin))
    1128         2068 :       ALLOCATE (mat_Sigma_pos_tau(bs_env%num_time_freq_points, bs_env%n_spin))
    1129              : 
    1130          180 :       DO ispin = 1, bs_env%n_spin
    1131         1816 :          DO i_t = 1, bs_env%num_time_freq_points
    1132         1636 :             ALLOCATE (mat_Sigma_neg_tau(i_t, ispin)%matrix)
    1133         1636 :             ALLOCATE (mat_Sigma_pos_tau(i_t, ispin)%matrix)
    1134         1636 :             CALL dbcsr_create(mat_Sigma_neg_tau(i_t, ispin)%matrix, template=bs_env%mat_ao_ao%matrix)
    1135         1732 :             CALL dbcsr_create(mat_Sigma_pos_tau(i_t, ispin)%matrix, template=bs_env%mat_ao_ao%matrix)
    1136              :          END DO
    1137              :       END DO
    1138              : 
    1139           84 :       CALL timestop(handle)
    1140              : 
    1141           84 :    END SUBROUTINE create_mat_for_Sigma_c
    1142              : 
    1143              : ! **************************************************************************************************
    1144              : !> \brief ...
    1145              : !> \param qs_env ...
    1146              : !> \param bs_env ...
    1147              : !> \param i_atoms ...
    1148              : !> \param j_atoms ...
    1149              : !> \param t_3c_x_W ...
    1150              : !> \param t_2c_W ...
    1151              : ! **************************************************************************************************
    1152         1542 :    SUBROUTINE compute_3c_and_contract_W(qs_env, bs_env, i_atoms, j_atoms, t_3c_x_W, t_2c_W)
    1153              : 
    1154              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1155              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1156              :       INTEGER, DIMENSION(2)                              :: i_atoms, j_atoms
    1157              :       TYPE(dbt_type)                                     :: t_3c_x_W, t_2c_W
    1158              : 
    1159              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_3c_and_contract_W'
    1160              : 
    1161              :       INTEGER                                            :: handle, RI_intval_idx
    1162              :       INTEGER(KIND=int_8)                                :: flop
    1163              :       INTEGER, DIMENSION(2)                              :: bounds_P, bounds_Q, RI_atoms
    1164              :       INTEGER, DIMENSION(2, 2)                           :: bounds_ao
    1165        13878 :       TYPE(dbt_type)                                     :: t_3c_x_W_tmp
    1166              :       TYPE(dbt_type), POINTER                            :: t_3c_use
    1167        13878 :       TYPE(dbt_type), TARGET                             :: t_3c_for_W
    1168              : 
    1169         1542 :       CALL timeset(routineN, handle)
    1170              : 
    1171         1542 :       CALL dbt_create(bs_env%t_RI__AO_AO, t_3c_x_W_tmp)
    1172         1542 :       CALL dbt_create(bs_env%t_RI__AO_AO, t_3c_for_W)
    1173              : 
    1174              :       ! final layout will be: M_Qνσ(iτ) = sum_P (P|νσ) W^MIC_QP(iτ)
    1175              :       ! Bounds:
    1176              :       ! "AO"
    1177              :       !  ->  ν (AO_1 in compute_3c_integrals)  bounds from i_atoms and sparse in σ and P
    1178              :       !  ->  σ (AO_2 in compute_3c_integrals)  sparse in ν and P
    1179              :       ! Q   bounds from j_atoms
    1180              :       ! P   bounds from inner loop indices and sparse in ν and σ
    1181              : 
    1182              :       bounds_Q(1:2) = [bs_env%i_RI_start_from_atom(j_atoms(1)), &
    1183         4626 :                        bs_env%i_RI_end_from_atom(j_atoms(2))]
    1184              : 
    1185         3084 :       DO RI_intval_idx = 1, bs_env%n_intervals_inner_loop_atoms
    1186         4626 :          RI_atoms = bs_env%inner_loop_atom_intervals(1:2, RI_intval_idx)
    1187              : 
    1188              :          CALL get_bounds_from_atoms(bounds_P, i_atoms, [1, bs_env%n_atom], &
    1189              :                                     bs_env%min_RI_idx_from_AO_AO_atom, &
    1190              :                                     bs_env%max_RI_idx_from_AO_AO_atom, &
    1191              :                                     atoms_3=RI_atoms, &
    1192              :                                     indices_3_start=bs_env%i_RI_start_from_atom, &
    1193         4626 :                                     indices_3_end=bs_env%i_RI_end_from_atom)
    1194              : 
    1195              :          ! σ
    1196              :          CALL get_bounds_from_atoms(bounds_ao(:, 2), RI_atoms, i_atoms, &
    1197              :                                     bs_env%min_AO_idx_from_RI_AO_atom, &
    1198         1542 :                                     bs_env%max_AO_idx_from_RI_AO_atom)
    1199              :          ! ν
    1200              :          CALL get_bounds_from_atoms(bounds_ao(:, 1), RI_atoms, [1, bs_env%n_atom], &
    1201              :                                     bs_env%min_AO_idx_from_RI_AO_atom, &
    1202              :                                     bs_env%max_AO_idx_from_RI_AO_atom, &
    1203              :                                     atoms_3=i_atoms, &
    1204              :                                     indices_3_start=bs_env%i_ao_start_from_atom, &
    1205         4626 :                                     indices_3_end=bs_env%i_ao_end_from_atom)
    1206              : 
    1207         1542 :          IF (bounds_P(1) > bounds_P(2) .OR. bounds_ao(1, 2) > bounds_ao(2, 2)) THEN
    1208              :             CYCLE
    1209              :          END IF
    1210              : 
    1211              :          ! 1. compute 3-center integrals (P|µν) ("|": truncated Coulomb operator)
    1212              :          CALL get_3c_integrals(qs_env, bs_env, bs_env%store_3c_W, t_3c_for_W, t_3c_use, &
    1213         1542 :                                atoms_AO_1=i_atoms, atoms_RI=RI_atoms)
    1214              : 
    1215              :          ! 2. tensor operation M_Qνσ(iτ) = sum_P  W^MIC_QP(iτ) (P|νσ)
    1216              :          CALL dbt_contract(alpha=1.0_dp, &
    1217              :                            tensor_1=t_2c_W, &
    1218              :                            tensor_2=t_3c_use, &
    1219              :                            beta=1.0_dp, &
    1220              :                            tensor_3=t_3c_x_W_tmp, &
    1221              :                            contract_1=[2], notcontract_1=[1], map_1=[1], &
    1222              :                            contract_2=[1], notcontract_2=[2, 3], map_2=[2, 3], &
    1223              :                            bounds_1=bounds_P, &
    1224              :                            bounds_2=bounds_Q, &
    1225              :                            bounds_3=bounds_ao, &
    1226              :                            flop=flop, &
    1227              :                            move_data=.FALSE., &
    1228         3084 :                            filter_eps=bs_env%eps_filter)
    1229              : 
    1230              :       END DO ! RI_atoms
    1231              : 
    1232              :       ! 3. reorder tensor
    1233         1542 :       CALL dbt_copy(t_3c_x_W_tmp, t_3c_x_W, order=[1, 2, 3], move_data=.TRUE.)
    1234              : 
    1235         1542 :       CALL dbt_destroy(t_3c_x_W_tmp)
    1236         1542 :       CALL dbt_destroy(t_3c_for_W)
    1237              : 
    1238         1542 :       CALL timestop(handle)
    1239              : 
    1240         1542 :    END SUBROUTINE compute_3c_and_contract_W
    1241              : 
    1242              : ! **************************************************************************************************
    1243              : !> \brief ...
    1244              : !> \param t_2c_G ...
    1245              : !> \param t_3c_x_W ...
    1246              : !> \param t_2c_Sigma ...
    1247              : !> \param i_atoms ...
    1248              : !> \param j_atoms ...
    1249              : !> \param qs_env ...
    1250              : !> \param bs_env ...
    1251              : !> \param occ ...
    1252              : !> \param vir ...
    1253              : !> \param can_skip ...
    1254              : ! **************************************************************************************************
    1255         2996 :    SUBROUTINE contract_to_Sigma(t_2c_G, t_3c_x_W, t_2c_Sigma, i_atoms, j_atoms, qs_env, bs_env, &
    1256              :                                 occ, vir, can_skip)
    1257              :       TYPE(dbt_type)                                     :: t_2c_G, t_3c_x_W, t_2c_Sigma
    1258              :       INTEGER, DIMENSION(2)                              :: i_atoms, j_atoms
    1259              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1260              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1261              :       LOGICAL                                            :: occ, vir
    1262              :       LOGICAL, OPTIONAL                                  :: can_skip
    1263              : 
    1264              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'contract_to_Sigma'
    1265              : 
    1266              :       INTEGER :: handle, inner_loop_atoms_interval_index
    1267              :       INTEGER(KIND=int_8)                                :: flop
    1268              :       INTEGER, DIMENSION(2)                              :: bounds_lambda, bounds_mu, bounds_nu, &
    1269              :                                                             bounds_sigma, IL_atoms
    1270              :       INTEGER, DIMENSION(2, 2)                           :: bounds_comb
    1271              :       REAL(KIND=dp)                                      :: sign_Sigma
    1272        50932 :       TYPE(dbt_type)                                     :: t_3c_x_G, t_3c_x_G_2
    1273              :       TYPE(dbt_type), POINTER                            :: t_3c_use
    1274        26964 :       TYPE(dbt_type), TARGET                             :: t_3c_for_G
    1275              : 
    1276         2996 :       CALL timeset(routineN, handle)
    1277              : 
    1278         2996 :       CPASSERT(occ .EQV. (.NOT. vir))
    1279         2996 :       IF (occ) sign_Sigma = -1.0_dp
    1280         2996 :       IF (vir) sign_Sigma = 1.0_dp
    1281              : 
    1282         2996 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_for_G)
    1283         2996 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_G)
    1284         2996 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_G_2)
    1285              : 
    1286              :       ! Here, in the first step e.g., is computed: N_Qλν = sum_µ (Qλ|µ) G_νµ
    1287              :       ! Afterwards e.g., is computed: Σ_λσ = sum_νQ M_Qνσ N_Qνλ (after reordering)
    1288              :       ! Bounds:
    1289              :       ! "comb" (combined index)
    1290              :       !   ->  Q   bounds from j_atoms and sparse in λ
    1291              :       !   ->  λ (AO_1 in compute_3c_integrals)  sparse in Q and µ
    1292              :       ! µ (AO_2 in compute_3c_integrals)  bounds from inner loop "IL" indices and sparse in Q and λ
    1293              :       ! ν bounds from i_atoms
    1294              :       ! σ sparse in ν
    1295              : 
    1296              :       ! ν
    1297              :       bounds_nu(1:2) = [bs_env%i_ao_start_from_atom(i_atoms(1)), &
    1298         8988 :                         bs_env%i_ao_end_from_atom(i_atoms(2))]
    1299              : 
    1300         5992 :       DO inner_loop_atoms_interval_index = 1, bs_env%n_intervals_inner_loop_atoms
    1301         8988 :          IL_atoms = bs_env%inner_loop_atom_intervals(1:2, inner_loop_atoms_interval_index)
    1302              : 
    1303              :          ! µ
    1304              :          CALL get_bounds_from_atoms(bounds_mu, j_atoms, [1, bs_env%n_atom], &
    1305              :                                     bs_env%min_AO_idx_from_RI_AO_atom, &
    1306              :                                     bs_env%max_AO_idx_from_RI_AO_atom, &
    1307              :                                     atoms_3=IL_atoms, &
    1308              :                                     indices_3_start=bs_env%i_ao_start_from_atom, &
    1309         8988 :                                     indices_3_end=bs_env%i_ao_end_from_atom)
    1310              : 
    1311              :          ! Q
    1312              :          CALL get_bounds_from_atoms(bounds_comb(:, 1), IL_atoms, [1, bs_env%n_atom], &
    1313              :                                     bs_env%min_RI_idx_from_AO_AO_atom, &
    1314              :                                     bs_env%max_RI_idx_from_AO_AO_atom, &
    1315              :                                     atoms_3=j_atoms, &
    1316              :                                     indices_3_start=bs_env%i_RI_start_from_atom, &
    1317         8988 :                                     indices_3_end=bs_env%i_RI_end_from_atom)
    1318              : 
    1319              :          ! λ
    1320              :          CALL get_bounds_from_atoms(bounds_comb(:, 2), j_atoms, IL_atoms, &
    1321              :                                     bs_env%min_AO_idx_from_RI_AO_atom, &
    1322         2996 :                                     bs_env%max_AO_idx_from_RI_AO_atom)
    1323              : 
    1324         2996 :          IF (bounds_mu(1) > bounds_mu(2) .OR. bounds_comb(1, 1) > bounds_comb(2, 1) .OR. &
    1325              :              bounds_comb(1, 2) > bounds_comb(2, 2)) THEN
    1326              :             CYCLE
    1327              :          END IF
    1328              : 
    1329              :          CALL get_3c_integrals(qs_env, bs_env, bs_env%store_3c_Sigma, t_3c_for_G, t_3c_use, &
    1330         2996 :                                atoms_RI=j_atoms, atoms_AO_2=IL_atoms)
    1331              : 
    1332              :          CALL dbt_contract(alpha=1.0_dp, &
    1333              :                            tensor_1=t_2c_G, &
    1334              :                            tensor_2=t_3c_use, &
    1335              :                            beta=1.0_dp, &
    1336              :                            tensor_3=t_3c_x_G, &
    1337              :                            contract_1=[2], notcontract_1=[1], map_1=[3], &
    1338              :                            contract_2=[3], notcontract_2=[1, 2], map_2=[1, 2], &
    1339              :                            bounds_1=bounds_mu, &
    1340              :                            bounds_2=bounds_nu, &
    1341              :                            bounds_3=bounds_comb, &
    1342              :                            flop=flop, &
    1343              :                            move_data=.FALSE., &
    1344         5992 :                            filter_eps=bs_env%eps_filter)
    1345              :       END DO ! IL_atoms
    1346              : 
    1347              :       ! Reordering: N_Qλν -> N_Qνλ
    1348         2996 :       CALL dbt_copy(t_3c_x_G, t_3c_x_G_2, order=[1, 3, 2], move_data=.TRUE.)
    1349              : 
    1350              :       ! Here, the last contraction is done, e.g., Σ_λσ = sum_νQ M_Qνσ N_Qνλ
    1351              :       ! Bounds as above, new "comb" with upper ingredients
    1352              :       bounds_comb(1:2, 1) = [bs_env%i_RI_start_from_atom(j_atoms(1)), &
    1353         8988 :                              bs_env%i_RI_end_from_atom(j_atoms(2))]
    1354         8988 :       bounds_comb(1:2, 2) = bounds_nu(1:2)
    1355              : 
    1356              :       CALL get_bounds_from_atoms(bounds_lambda, j_atoms, [1, bs_env%n_atom], &
    1357              :                                  bs_env%min_AO_idx_from_RI_AO_atom, &
    1358         8988 :                                  bs_env%max_AO_idx_from_RI_AO_atom)
    1359              :       CALL get_bounds_from_atoms(bounds_sigma, [1, bs_env%n_atom], i_atoms, &
    1360              :                                  bs_env%min_AO_idx_from_RI_AO_atom, &
    1361         8988 :                                  bs_env%max_AO_idx_from_RI_AO_atom)
    1362              : 
    1363         2996 :       IF (bounds_sigma(1) > bounds_sigma(2) .OR. bounds_lambda(1) > bounds_lambda(2)) THEN
    1364            0 :          flop = 0_int_8
    1365              :       ELSE
    1366              :          CALL dbt_contract(alpha=sign_Sigma, &
    1367              :                            tensor_1=t_3c_x_W, &
    1368              :                            tensor_2=t_3c_x_G_2, &
    1369              :                            beta=1.0_dp, &
    1370              :                            tensor_3=t_2c_Sigma, &
    1371              :                            contract_1=[1, 2], notcontract_1=[3], map_1=[1], &
    1372              :                            contract_2=[1, 2], notcontract_2=[3], map_2=[2], &
    1373              :                            bounds_1=bounds_comb, &
    1374              :                            bounds_2=bounds_sigma, &
    1375              :                            bounds_3=bounds_lambda, &
    1376         2996 :                            filter_eps=bs_env%eps_filter, move_data=.FALSE., flop=flop)
    1377              :       END IF
    1378              : 
    1379         2996 :       IF (PRESENT(can_skip)) THEN
    1380         2908 :          IF (flop == 0_int_8) can_skip = .TRUE.
    1381              :       END IF
    1382              : 
    1383         2996 :       CALL dbt_destroy(t_3c_for_G)
    1384         2996 :       CALL dbt_destroy(t_3c_x_G)
    1385         2996 :       CALL dbt_destroy(t_3c_x_G_2)
    1386              : 
    1387         2996 :       CALL timestop(handle)
    1388              : 
    1389         2996 :    END SUBROUTINE contract_to_Sigma
    1390              : 
    1391              : ! **************************************************************************************************
    1392              : !> \brief Copy the positive- and negative-time self-energy matrices to the FM time array.
    1393              : !> \param fm_Sigma_c_Gamma_time allocated output indexed by time, time sector and spin
    1394              : !> \param bs_env time grid, spin count and AO layout
    1395              : !> \param mat_Sigma_pos_tau positive-time self-energy matrices
    1396              : !> \param mat_Sigma_neg_tau negative-time self-energy matrices
    1397              : ! **************************************************************************************************
    1398           76 :    SUBROUTINE fill_fm_Sigma_c_Gamma_time(fm_Sigma_c_Gamma_time, bs_env, &
    1399              :                                          mat_Sigma_pos_tau, mat_Sigma_neg_tau)
    1400              : 
    1401              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :, :)  :: fm_Sigma_c_Gamma_time
    1402              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1403              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: mat_Sigma_pos_tau, mat_Sigma_neg_tau
    1404              : 
    1405              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'fill_fm_Sigma_c_Gamma_time'
    1406              : 
    1407              :       INTEGER                                            :: handle, i_t, ispin, pos_neg
    1408              : 
    1409           76 :       CALL timeset(routineN, handle)
    1410              : 
    1411         3736 :       ALLOCATE (fm_Sigma_c_Gamma_time(bs_env%num_time_freq_points, 2, bs_env%n_spin))
    1412          164 :       DO ispin = 1, bs_env%n_spin
    1413         1748 :          DO i_t = 1, bs_env%num_time_freq_points
    1414         4752 :             DO pos_neg = 1, 2
    1415              :                CALL cp_fm_create(fm_Sigma_c_Gamma_time(i_t, pos_neg, ispin), &
    1416         4752 :                                  bs_env%fm_s_Gamma%matrix_struct)
    1417              :             END DO
    1418              :             CALL copy_dbcsr_to_fm(mat_Sigma_pos_tau(i_t, ispin)%matrix, &
    1419         1584 :                                   fm_Sigma_c_Gamma_time(i_t, 1, ispin))
    1420              :             CALL copy_dbcsr_to_fm(mat_Sigma_neg_tau(i_t, ispin)%matrix, &
    1421         1672 :                                   fm_Sigma_c_Gamma_time(i_t, 2, ispin))
    1422              :          END DO
    1423              :       END DO
    1424              : 
    1425           76 :       CALL timestop(handle)
    1426              : 
    1427           76 :    END SUBROUTINE fill_fm_Sigma_c_Gamma_time
    1428              : 
    1429              : ! **************************************************************************************************
    1430              : !> \brief ...
    1431              : !> \param bs_env ...
    1432              : ! **************************************************************************************************
    1433           84 :    SUBROUTINE print_skipping(bs_env)
    1434              : 
    1435              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1436              : 
    1437              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'print_skipping'
    1438              : 
    1439              :       INTEGER                                            :: handle, n_pairs
    1440              : 
    1441           84 :       CALL timeset(routineN, handle)
    1442              : 
    1443           84 :       n_pairs = bs_env%n_intervals_i*bs_env%n_intervals_j*bs_env%n_spin
    1444              : 
    1445           84 :       CALL bs_env%para_env_tensor%sum(bs_env%n_skip_sigma)
    1446           84 :       CALL bs_env%para_env_tensor%sum(bs_env%n_skip_chi)
    1447           84 :       CALL bs_env%para_env_tensor%sum(n_pairs)
    1448              : 
    1449           84 :       IF (bs_env%unit_nr > 0) THEN
    1450              :          WRITE (bs_env%unit_nr, '(T2,A,T74,F7.1,A)') &
    1451           42 :             'Sparsity of Σ^c(iτ,k=0): Percentage of skipped atom pairs:', &
    1452           84 :             REAL(100*bs_env%n_skip_sigma, KIND=dp)/REAL(n_pairs, KIND=dp), ' %'
    1453              :          WRITE (bs_env%unit_nr, '(T2,A,T74,F7.1,A)') &
    1454           42 :             'Sparsity of χ(iτ,k=0): Percentage of skipped atom pairs:', &
    1455           84 :             REAL(100*bs_env%n_skip_chi, KIND=dp)/REAL(n_pairs, KIND=dp), ' %'
    1456              :       END IF
    1457              : 
    1458           84 :       CALL timestop(handle)
    1459              : 
    1460           84 :    END SUBROUTINE print_skipping
    1461              : 
    1462              : ! **************************************************************************************************
    1463              : !> \brief ...
    1464              : !> \param t_2c_Gocc ...
    1465              : !> \param t_2c_Gvir ...
    1466              : !> \param t_2c_W ...
    1467              : !> \param t_2c_Sigma_neg_tau ...
    1468              : !> \param t_2c_Sigma_pos_tau ...
    1469              : !> \param t_3c_x_W ...
    1470              : !> \param fm_W_MIC_time ...
    1471              : !> \param mat_Sigma_neg_tau ...
    1472              : !> \param mat_Sigma_pos_tau ...
    1473              : ! **************************************************************************************************
    1474           84 :    SUBROUTINE destroy_mat_Sigma_c(t_2c_Gocc, t_2c_Gvir, t_2c_W, t_2c_Sigma_neg_tau, &
    1475              :                                   t_2c_Sigma_pos_tau, t_3c_x_W, fm_W_MIC_time, &
    1476              :                                   mat_Sigma_neg_tau, mat_Sigma_pos_tau)
    1477              : 
    1478              :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, t_2c_W, &
    1479              :                                                             t_2c_Sigma_neg_tau, &
    1480              :                                                             t_2c_Sigma_pos_tau, t_3c_x_W
    1481              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_W_MIC_time
    1482              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: mat_Sigma_neg_tau, mat_Sigma_pos_tau
    1483              : 
    1484              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'destroy_mat_Sigma_c'
    1485              : 
    1486              :       INTEGER                                            :: handle
    1487              : 
    1488           84 :       CALL timeset(routineN, handle)
    1489              : 
    1490           84 :       CALL dbt_destroy(t_2c_Gocc)
    1491           84 :       CALL dbt_destroy(t_2c_Gvir)
    1492           84 :       CALL dbt_destroy(t_2c_W)
    1493           84 :       CALL dbt_destroy(t_2c_Sigma_neg_tau)
    1494           84 :       CALL dbt_destroy(t_2c_Sigma_pos_tau)
    1495           84 :       CALL dbt_destroy(t_3c_x_W)
    1496           84 :       CALL cp_fm_release(fm_W_MIC_time)
    1497           84 :       CALL dbcsr_deallocate_matrix_set(mat_Sigma_neg_tau)
    1498           84 :       CALL dbcsr_deallocate_matrix_set(mat_Sigma_pos_tau)
    1499              : 
    1500           84 :       CALL timestop(handle)
    1501              : 
    1502           84 :    END SUBROUTINE destroy_mat_Sigma_c
    1503              : 
    1504              : ! **************************************************************************************************
    1505              : !> \brief ...
    1506              : !> \param bs_env ...
    1507              : ! **************************************************************************************************
    1508          126 :    SUBROUTINE delete_unnecessary_files(bs_env)
    1509              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1510              : 
    1511              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'delete_unnecessary_files'
    1512              : 
    1513              :       CHARACTER(LEN=default_path_length)                 :: f_chi, f_W_t, prefix
    1514              :       INTEGER                                            :: handle, i_t
    1515              : 
    1516          126 :       CALL timeset(routineN, handle)
    1517              : 
    1518          126 :       prefix = bs_env%prefix
    1519              : 
    1520         2142 :       DO i_t = 1, bs_env%num_time_freq_points
    1521              : 
    1522         2016 :          IF (i_t < 10) THEN
    1523         1100 :             WRITE (f_chi, '(3A,I1,A)') TRIM(prefix), bs_env%chi_name, "_0", i_t, ".matrix"
    1524         1100 :             WRITE (f_W_t, '(3A,I1,A)') TRIM(prefix), bs_env%W_time_name, "_0", i_t, ".matrix"
    1525          916 :          ELSE IF (i_t < 100) THEN
    1526          916 :             WRITE (f_chi, '(3A,I2,A)') TRIM(prefix), bs_env%chi_name, "_", i_t, ".matrix"
    1527          916 :             WRITE (f_W_t, '(3A,I2,A)') TRIM(prefix), bs_env%W_time_name, "_", i_t, ".matrix"
    1528              :          ELSE
    1529            0 :             CPABORT('Please implement more than 99 time/frequency points.')
    1530              :          END IF
    1531              : 
    1532         2016 :          CALL safe_delete(f_chi, bs_env)
    1533         2142 :          CALL safe_delete(f_W_t, bs_env)
    1534              : 
    1535              :       END DO
    1536              : 
    1537          126 :       CALL timestop(handle)
    1538              : 
    1539          126 :    END SUBROUTINE delete_unnecessary_files
    1540              : 
    1541              : ! **************************************************************************************************
    1542              : !> \brief ...
    1543              : !> \param filename ...
    1544              : !> \param bs_env ...
    1545              : ! **************************************************************************************************
    1546         4032 :    SUBROUTINE safe_delete(filename, bs_env)
    1547              :       CHARACTER(LEN=*)                                   :: filename
    1548              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1549              : 
    1550              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'safe_delete'
    1551              : 
    1552              :       INTEGER                                            :: handle
    1553              :       LOGICAL                                            :: file_exists
    1554              : 
    1555         4032 :       CALL timeset(routineN, handle)
    1556              : 
    1557         4032 :       IF (bs_env%para_env%mepos == 0) THEN
    1558              : 
    1559         2016 :          INQUIRE (file=TRIM(filename), exist=file_exists)
    1560         2016 :          IF (file_exists) CALL mp_file_delete(TRIM(filename))
    1561              : 
    1562              :       END IF
    1563              : 
    1564         4032 :       CALL timestop(handle)
    1565              : 
    1566         4032 :    END SUBROUTINE safe_delete
    1567              : 
    1568              : ! **************************************************************************************************
    1569              : !> \brief ...
    1570              : !> \param bs_env ...
    1571              : !> \param qs_env ...
    1572              : !> \param fm_Sigma_x_Gamma ...
    1573              : !> \param fm_Sigma_c_Gamma_time ...
    1574              : ! **************************************************************************************************
    1575           76 :    SUBROUTINE compute_QP_energies(bs_env, qs_env, fm_Sigma_x_Gamma, fm_Sigma_c_Gamma_time)
    1576              : 
    1577              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1578              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1579              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_Sigma_x_Gamma
    1580              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :, :)  :: fm_Sigma_c_Gamma_time
    1581              : 
    1582              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_QP_energies'
    1583              : 
    1584              :       COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :)     :: Sigma_c_ikp_n_freq
    1585              :       INTEGER                                            :: handle, ikp, ispin, j_t
    1586              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: Sigma_x_ikp_n, V_xc_ikp_n
    1587              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: Sigma_c_ikp_n_time
    1588              :       TYPE(cp_cfm_type)                                  :: cfm_ks_ikp, cfm_mos_ikp, cfm_s_ikp, &
    1589              :                                                             cfm_work_ikp
    1590              :       TYPE(gw_qp_summary_type)                           :: qp_summary
    1591              : 
    1592           76 :       CALL timeset(routineN, handle)
    1593              : 
    1594           76 :       CALL cp_cfm_create(cfm_mos_ikp, bs_env%fm_s_Gamma%matrix_struct)
    1595           76 :       CALL cp_cfm_create(cfm_work_ikp, bs_env%fm_s_Gamma%matrix_struct)
    1596              :       ! JW TODO: fully distribute these arrays at given time; also eigenvalues in bs_env
    1597          304 :       ALLOCATE (V_xc_ikp_n(bs_env%n_ao), Sigma_x_ikp_n(bs_env%n_ao))
    1598          380 :       ALLOCATE (Sigma_c_ikp_n_time(bs_env%n_ao, bs_env%num_time_freq_points, 2))
    1599          304 :       ALLOCATE (Sigma_c_ikp_n_freq(bs_env%n_ao, bs_env%num_freq_points_fit))
    1600              : 
    1601           76 :       qp_summary%n_kpoints = bs_env%nkp_bs_and_DOS
    1602          164 :       DO ispin = 1, bs_env%n_spin
    1603              : 
    1604          266 :          DO ikp = 1, bs_env%nkp_bs_and_DOS
    1605              : 
    1606              :             ! 1. get H^KS_µν(k_i) from H^KS_µν(k=0)
    1607              :             CALL cfm_ikp_from_fm_Gamma(cfm_ks_ikp, bs_env%fm_ks_Gamma(ispin), &
    1608          102 :                                        ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    1609              : 
    1610              :             ! 2. get S_µν(k_i) from S_µν(k=0)
    1611              :             CALL cfm_ikp_from_fm_Gamma(cfm_s_ikp, bs_env%fm_s_Gamma, &
    1612          102 :                                        ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    1613              : 
    1614              :             ! 3. Diagonalize (Roothaan-Hall): H_KS(k_i)*C(k_i) = S(k_i)*C(k_i)*ϵ(k_i)
    1615              :             CALL cp_cfm_geeig(cfm_ks_ikp, cfm_s_ikp, cfm_mos_ikp, &
    1616          102 :                               bs_env%eigenval_scf(:, ikp, ispin), cfm_work_ikp)
    1617              : 
    1618              :             ! 4. V^xc_µν(k=0) -> V^xc_µν(k_i) -> V^xc_nn(k_i)
    1619              :             CALL to_ikp_and_mo(V_xc_ikp_n, bs_env%fm_V_xc_Gamma(ispin), &
    1620          102 :                                ikp, qs_env, bs_env, cfm_mos_ikp, cfm_ks_ikp, cfm_work_ikp)
    1621              : 
    1622              :             ! 5. Σ^x_µν(k=0) -> Σ^x_µν(k_i) -> Σ^x_nn(k_i)
    1623              :             CALL to_ikp_and_mo(Sigma_x_ikp_n, fm_Sigma_x_Gamma(ispin), &
    1624          102 :                                ikp, qs_env, bs_env, cfm_mos_ikp, cfm_ks_ikp, cfm_work_ikp)
    1625              : 
    1626              :             ! 6. Σ^c_µν(k=0,+/-i|τ_j|) -> Σ^c_µν(k_i,+/-i|τ_j|) -> Σ^c_nn(k_i,+/-i|τ_j|)
    1627         1810 :             DO j_t = 1, bs_env%num_time_freq_points
    1628              :                CALL to_ikp_and_mo(Sigma_c_ikp_n_time(:, j_t, positive_time), &
    1629              :                                   fm_Sigma_c_Gamma_time(j_t, positive_time, ispin), &
    1630         1708 :                                   ikp, qs_env, bs_env, cfm_mos_ikp, cfm_ks_ikp, cfm_work_ikp)
    1631              :                CALL to_ikp_and_mo(Sigma_c_ikp_n_time(:, j_t, negative_time), &
    1632              :                                   fm_Sigma_c_Gamma_time(j_t, negative_time, ispin), &
    1633         1810 :                                   ikp, qs_env, bs_env, cfm_mos_ikp, cfm_ks_ikp, cfm_work_ikp)
    1634              :             END DO
    1635              : 
    1636              :             ! 7. Σ^c_nn(k_i,iτ) -> Σ^c_nn(k_i,iω)
    1637          102 :             CALL transform_time_to_frequency(bs_env%time_frequency_grid_sigma, Sigma_c_ikp_n_time, Sigma_c_ikp_n_freq)
    1638         4722 :             Sigma_c_ikp_n_freq(1:bs_env%n_occ(ispin), :) = CONJG(Sigma_c_ikp_n_freq(1:bs_env%n_occ(ispin), :))
    1639              : 
    1640              :             ! 8. Analytic continuation Σ^c_nn(k_i,iω) -> Σ^c_nn(k_i,ϵ) and
    1641              :             !    ϵ_nk_i^GW = ϵ_nk_i^DFT + Σ^c_nn(k_i,ϵ) + Σ^x_nn(k_i) - v^xc_nn(k_i)
    1642              :             CALL analyt_conti_and_print(bs_env, Sigma_c_ikp_n_freq, Sigma_x_ikp_n, V_xc_ikp_n, &
    1643          190 :                                         bs_env%eigenval_scf(:, ikp, ispin), ikp, ispin, qp_summary)
    1644              : 
    1645              :          END DO ! ikp_DOS
    1646              : 
    1647              :       END DO ! ispin
    1648              : 
    1649           76 :       CALL get_all_VBM_CBM_bandgaps(bs_env)
    1650              : 
    1651              :       ! Σ^x is releases here in case of G0W0
    1652           76 :       IF (bs_env%gw_flavour == G0W0) CALL cp_fm_release(fm_Sigma_x_Gamma)
    1653           76 :       CALL cp_fm_release(fm_Sigma_c_Gamma_time)
    1654           76 :       CALL cp_cfm_release(cfm_ks_ikp)
    1655           76 :       CALL cp_cfm_release(cfm_s_ikp)
    1656           76 :       CALL cp_cfm_release(cfm_mos_ikp)
    1657           76 :       CALL cp_cfm_release(cfm_work_ikp)
    1658              : 
    1659           76 :       CALL timestop(handle)
    1660              : 
    1661          152 :    END SUBROUTINE compute_QP_energies
    1662              : 
    1663              : ! **************************************************************************************************
    1664              : !> \brief Reconstruct a k-point Hermitian operator and return its real MO diagonal.
    1665              : !> \param array_ikp_n Real diagonal elements in the MO basis.
    1666              : !> \param fm_Gamma Real AO operator at the Gamma point.
    1667              : !> \param ikp Index of the target k-point.
    1668              : !> \param qs_env Quantum simulation environment.
    1669              : !> \param bs_env Band-structure environment containing the target k-points.
    1670              : !> \param cfm_mos_ikp Complex MO coefficients at the target k-point.
    1671              : !> \param cfm_ikp Reusable AO operator buffer; overwritten by its MO projection.
    1672              : !> \param workspace Reusable projection scratch matrix with the MO coefficient layout.
    1673              : ! **************************************************************************************************
    1674         3620 :    SUBROUTINE to_ikp_and_mo(array_ikp_n, fm_Gamma, ikp, qs_env, bs_env, cfm_mos_ikp, cfm_ikp, workspace)
    1675              : 
    1676              :       REAL(KIND=dp), DIMENSION(:), INTENT(OUT)           :: array_ikp_n
    1677              :       TYPE(cp_fm_type), INTENT(IN)                       :: fm_Gamma
    1678              :       INTEGER, INTENT(IN)                                :: ikp
    1679              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1680              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1681              :       TYPE(cp_cfm_type), INTENT(IN)                      :: cfm_mos_ikp
    1682              :       TYPE(cp_cfm_type), INTENT(INOUT)                   :: cfm_ikp, workspace
    1683              : 
    1684              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'to_ikp_and_mo'
    1685              : 
    1686              :       COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:)        :: cdiag
    1687              :       INTEGER                                            :: handle
    1688              : 
    1689         3620 :       CALL timeset(routineN, handle)
    1690              : 
    1691              :       ! get cfm_µν(k_i) from fm_µν(k=0)
    1692         3620 :       CALL cfm_ikp_from_fm_Gamma(cfm_ikp, fm_Gamma, ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    1693              : 
    1694         3620 :       CALL cfm_contract_ABA(cfm_mos_ikp, cfm_ikp, workspace=workspace)
    1695              : 
    1696              :       ! Taking the real MO diagonal selects the Hermitian part of the operator.
    1697        10860 :       ALLOCATE (cdiag(SIZE(array_ikp_n)))
    1698         3620 :       CALL cp_cfm_get_diag(cfm_ikp, cdiag)
    1699        45888 :       array_ikp_n = REAL(cdiag, KIND=dp)
    1700              : 
    1701         3620 :       CALL timestop(handle)
    1702              : 
    1703         7240 :    END SUBROUTINE to_ikp_and_mo
    1704              : 
    1705              : ! **************************************************************************************************
    1706              : !> \brief Computes bounds (AO or RI) for given atom intervals atoms_1 and atoms_2 from indices_min
    1707              : !>        and indices_max and returns them in bounds_out.
    1708              : !>        In case, atoms_3 and indices_3 are given, the bounds are computed as the intersection
    1709              : !> \param bounds_out Bounds to be computed
    1710              : !> \param atoms_1 First atom interval
    1711              : !> \param atoms_2 Second atom interval
    1712              : !> \param indices_min Minimum indices for each atom pair (typically from bs_env,
    1713              : !>        computed in get_i_j_atom_ranges in gw_utils.F, e.g. bs_env%min_RI_idx_from_AO_AO_atom)
    1714              : !> \param indices_max Maximum indices for each atom pair (typically from bs_env,
    1715              : !>        computed in get_i_j_atom_ranges in gw_utils.F)
    1716              : !> \param atoms_3 (Optional) Third atom interval for intersection
    1717              : !> \param indices_3_start (Optional) Indices for third atom interval for intersection
    1718              : !> \param indices_3_end (Optional) Indices for third atom interval for intersection
    1719              : ! **************************************************************************************************
    1720        32406 :    SUBROUTINE get_bounds_from_atoms(bounds_out, atoms_1, atoms_2, indices_min, indices_max, &
    1721        32406 :                                     atoms_3, indices_3_start, indices_3_end)
    1722              : 
    1723              :       INTEGER, DIMENSION(2), INTENT(OUT)                 :: bounds_out
    1724              :       INTEGER, DIMENSION(2), INTENT(IN)                  :: atoms_1, atoms_2
    1725              :       INTEGER, DIMENSION(:, :)                           :: indices_min, indices_max
    1726              :       INTEGER, DIMENSION(2), INTENT(IN), OPTIONAL        :: atoms_3
    1727              :       INTEGER, DIMENSION(:), OPTIONAL                    :: indices_3_start, indices_3_end
    1728              : 
    1729              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_bounds_from_atoms'
    1730              : 
    1731              :       INTEGER                                            :: handle, i_at, j_at
    1732              : 
    1733        32406 :       CALL timeset(routineN, handle)
    1734        32406 :       bounds_out(1) = HUGE(0)
    1735        32406 :       bounds_out(2) = -1
    1736              :       !Loop over all atoms in the two intervals and find min/max indices
    1737       102092 :       DO i_at = atoms_1(1), atoms_1(2)
    1738       256086 :          DO j_at = atoms_2(1), atoms_2(2)
    1739       153994 :             bounds_out(1) = MIN(bounds_out(1), indices_min(i_at, j_at))
    1740       223680 :             bounds_out(2) = MAX(bounds_out(2), indices_max(i_at, j_at))
    1741              :          END DO
    1742              :       END DO
    1743              : 
    1744        32406 :       IF (PRESENT(atoms_3) .AND. PRESENT(indices_3_start) .AND. PRESENT(indices_3_end)) THEN
    1745        15476 :          bounds_out(1) = MAX(bounds_out(1), indices_3_start(atoms_3(1)))
    1746        15476 :          bounds_out(2) = MIN(bounds_out(2), indices_3_end(atoms_3(2)))
    1747              :       END IF
    1748              : 
    1749        32406 :       CALL timestop(handle)
    1750              : 
    1751        32406 :    END SUBROUTINE get_bounds_from_atoms
    1752              : 
    1753              : END MODULE gw_tensor_large_cell_Gamma
        

Generated by: LCOV version 2.0-1