LCOV - code coverage report
Current view: top level - src - gw_large_cell_gamma.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:c24029e) Lines: 92.5 % 776 718
Test Date: 2026-07-04 06:36:57 Functions: 97.6 % 41 40

            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_large_cell_gamma
      16              :    USE atomic_kind_types,               ONLY: atomic_kind_type
      17              :    USE bibliography,                    ONLY: Graml2024,&
      18              :                                               cite_reference
      19              :    USE cell_types,                      ONLY: cell_type,&
      20              :                                               get_cell,&
      21              :                                               pbc
      22              :    USE constants_operator,              ONLY: operator_coulomb
      23              :    USE cp_cfm_basic_linalg,             ONLY: cp_cfm_uplo_to_full
      24              :    USE cp_cfm_cholesky,                 ONLY: cp_cfm_cholesky_decompose,&
      25              :                                               cp_cfm_cholesky_invert
      26              :    USE cp_cfm_diag,                     ONLY: cp_cfm_geeig
      27              :    USE cp_cfm_types,                    ONLY: cp_cfm_create,&
      28              :                                               cp_cfm_get_info,&
      29              :                                               cp_cfm_release,&
      30              :                                               cp_cfm_to_cfm,&
      31              :                                               cp_cfm_to_fm,&
      32              :                                               cp_cfm_type,&
      33              :                                               cp_fm_to_cfm
      34              :    USE cp_dbcsr_api,                    ONLY: &
      35              :         dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_deallocate_matrix, dbcsr_get_block_p, &
      36              :         dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, dbcsr_iterator_start, &
      37              :         dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_p_type, dbcsr_release, dbcsr_set, &
      38              :         dbcsr_type
      39              :    USE cp_dbcsr_contrib,                ONLY: dbcsr_reserve_all_blocks
      40              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      41              :                                               copy_fm_to_dbcsr,&
      42              :                                               dbcsr_deallocate_matrix_set
      43              :    USE cp_files,                        ONLY: close_file,&
      44              :                                               open_file
      45              :    USE cp_fm_basic_linalg,              ONLY: cp_fm_scale_and_add
      46              :    USE cp_fm_diag,                      ONLY: cp_fm_power
      47              :    USE cp_fm_types,                     ONLY: &
      48              :         cp_fm_create, cp_fm_get_diag, cp_fm_get_info, cp_fm_read_unformatted, cp_fm_release, &
      49              :         cp_fm_set_all, cp_fm_to_fm, cp_fm_type, cp_fm_write_unformatted
      50              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      51              :                                               cp_logger_type
      52              :    USE cp_output_handling,              ONLY: cp_p_file,&
      53              :                                               cp_print_key_should_output,&
      54              :                                               cp_print_key_unit_nr
      55              :    USE dbt_api,                         ONLY: dbt_clear,&
      56              :                                               dbt_contract,&
      57              :                                               dbt_copy,&
      58              :                                               dbt_create,&
      59              :                                               dbt_destroy,&
      60              :                                               dbt_filter,&
      61              :                                               dbt_type
      62              :    USE gw_communication,                ONLY: fm_to_local_tensor,&
      63              :                                               local_dbt_to_global_mat
      64              :    USE gw_utils,                        ONLY: analyt_conti_and_print,&
      65              :                                               de_init_bs_env,&
      66              :                                               time_to_freq
      67              :    USE input_constants,                 ONLY: rtp_method_bse
      68              :    USE input_section_types,             ONLY: section_vals_type
      69              :    USE kinds,                           ONLY: default_path_length,&
      70              :                                               dp,&
      71              :                                               int_8
      72              :    USE kpoint_coulomb_2c,               ONLY: build_2c_coulomb_matrix_kp
      73              :    USE kpoint_types,                    ONLY: kpoint_type
      74              :    USE machine,                         ONLY: m_walltime
      75              :    USE mathconstants,                   ONLY: twopi,&
      76              :                                               z_one,&
      77              :                                               z_zero
      78              :    USE message_passing,                 ONLY: mp_file_delete
      79              :    USE mp2_ri_2c,                       ONLY: RI_2c_integral_mat
      80              :    USE parallel_gemm_api,               ONLY: parallel_gemm
      81              :    USE particle_types,                  ONLY: particle_type
      82              :    USE post_scf_bandstructure_types,    ONLY: post_scf_bandstructure_type
      83              :    USE post_scf_bandstructure_utils,    ONLY: MIC_contribution_from_ikp,&
      84              :                                               cfm_ikp_from_fm_Gamma,&
      85              :                                               get_all_VBM_CBM_bandgaps
      86              :    USE qs_environment_types,            ONLY: get_qs_env,&
      87              :                                               qs_environment_type
      88              :    USE qs_kind_types,                   ONLY: qs_kind_type
      89              :    USE qs_tensors,                      ONLY: build_3c_integrals
      90              :    USE rpa_gw_kpoints_util,             ONLY: cp_cfm_power
      91              : #include "./base/base_uses.f90"
      92              : 
      93              :    IMPLICIT NONE
      94              : 
      95              :    PRIVATE
      96              : 
      97              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_large_cell_gamma'
      98              : 
      99              :    PUBLIC :: gw_calc_large_cell_Gamma, &
     100              :              compute_3c_integrals, G_occ_vir, fm_read, write_matrix, &
     101              :              fill_fm_Sigma_c_Gamma_time, delete_unnecessary_files, &
     102              :              multiply_fm_W_MIC_time_with_Minv_Gamma, get_W_MIC, &
     103              :              create_fm_W_MIC_time, Fourier_transform_w_to_t, &
     104              :              compute_fm_chi_Gamma_freq, compute_QP_energies, fm_write
     105              : 
     106              : CONTAINS
     107              : 
     108              : ! **************************************************************************************************
     109              : !> \brief Perform GW band structure calculation
     110              : !> \param qs_env ...
     111              : !> \param bs_env ...
     112              : !> \par History
     113              : !>    * 07.2023 created [Jan Wilhelm]
     114              : ! **************************************************************************************************
     115           24 :    SUBROUTINE gw_calc_large_cell_Gamma(qs_env, bs_env)
     116              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     117              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     118              : 
     119              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'gw_calc_large_cell_Gamma'
     120              : 
     121              :       INTEGER                                            :: handle
     122           24 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_Sigma_x_Gamma, fm_W_MIC_time
     123           24 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :, :)  :: fm_Sigma_c_Gamma_time
     124              : 
     125           24 :       CALL timeset(routineN, handle)
     126              : 
     127           24 :       CALL cite_reference(Graml2024)
     128              : 
     129              :       ! G^occ_µλ(i|τ|,k=0) = sum_n^occ C_µn(k=0) e^(-|(ϵ_nk=0-ϵ_F)τ|) C_λn(k=0)
     130              :       ! G^vir_µλ(i|τ|,k=0) = sum_n^vir C_µn(k=0) e^(-|(ϵ_nk=0-ϵ_F)τ|) C_λn(k=0)
     131              :       ! χ_PQ(iτ,k=0) = sum_λν [sum_µ (µν|P) G^occ_µλ(i|τ|)] [sum_σ (σλ|Q) G^vir_σν(i|τ|)]
     132           24 :       CALL get_mat_chi_Gamma_tau(bs_env, qs_env, bs_env%mat_chi_Gamma_tau)
     133              : 
     134              :       ! χ_PQ(iτ,k=0) -> χ_PQ(iω,k) -> ε_PQ(iω,k) -> W_PQ(iω,k) -> W^MIC_PQ(iτ) -> M^-1*W^MIC*M^-1
     135           24 :       CALL get_W_MIC(bs_env, qs_env, bs_env%mat_chi_Gamma_tau, fm_W_MIC_time)
     136              : 
     137              :       ! 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>
     138              :       ! Σ^x_λσ(k=0) = sum_νQ [sum_P (νσ|P) V^trunc_PQ] [sum_µ (λµ|Q) D_µν)]
     139           24 :       CALL get_Sigma_x(bs_env, qs_env, fm_Sigma_x_Gamma)
     140              : 
     141              :       ! Σ^c_λσ(iτ,k=0) = sum_νQ [sum_P (νσ|P) W^MIC_PQ(iτ)] [sum_µ (λµ|Q) G^occ_µν(i|τ|)], τ < 0
     142              :       ! Σ^c_λσ(iτ,k=0) = sum_νQ [sum_P (νσ|P) W^MIC_PQ(iτ)] [sum_µ (λµ|Q) G^vir_µν(i|τ|)], τ > 0
     143           24 :       CALL get_Sigma_c(bs_env, qs_env, fm_W_MIC_time, fm_Sigma_c_Gamma_time)
     144              : 
     145              :       ! Σ^c_λσ(iτ,k=0) -> Σ^c_nn(ϵ,k); ϵ_nk^GW = ϵ_nk^DFT + Σ^c_nn(ϵ,k) + Σ^x_nn(k) - v^xc_nn(k)
     146           24 :       CALL compute_QP_energies(bs_env, qs_env, fm_Sigma_x_Gamma, fm_Sigma_c_Gamma_time)
     147              : 
     148           24 :       CALL de_init_bs_env(bs_env)
     149              : 
     150           24 :       CALL timestop(handle)
     151              : 
     152           24 :    END SUBROUTINE gw_calc_large_cell_Gamma
     153              : 
     154              : ! **************************************************************************************************
     155              : !> \brief ...
     156              : !> \param bs_env ...
     157              : !> \param qs_env ...
     158              : !> \param mat_chi_Gamma_tau ...
     159              : ! **************************************************************************************************
     160           24 :    SUBROUTINE get_mat_chi_Gamma_tau(bs_env, qs_env, mat_chi_Gamma_tau)
     161              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     162              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     163              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: mat_chi_Gamma_tau
     164              : 
     165              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_mat_chi_Gamma_tau'
     166              : 
     167              :       INTEGER :: handle, i_intval_idx, i_t, inner_loop_atoms_interval_index, ispin, j_intval_idx
     168              :       INTEGER(KIND=int_8)                                :: flop
     169              :       INTEGER, DIMENSION(2)                              :: bounds_P, bounds_Q, i_atoms, IL_atoms, &
     170              :                                                             j_atoms
     171              :       INTEGER, DIMENSION(2, 2)                           :: bounds_comb
     172              :       LOGICAL                                            :: dist_too_long_i, dist_too_long_j
     173              :       REAL(KIND=dp)                                      :: t1, tau
     174          600 :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, &
     175          408 :                                                             t_3c_for_Gvir, t_3c_x_Gocc, &
     176          408 :                                                             t_3c_x_Gocc_2, t_3c_x_Gvir, &
     177          216 :                                                             t_3c_x_Gvir_2
     178              : 
     179           24 :       CALL timeset(routineN, handle)
     180              : 
     181          388 :       DO i_t = 1, bs_env%num_time_freq_points
     182              : 
     183          364 :          t1 = m_walltime()
     184              : 
     185          364 :          IF (bs_env%read_chi(i_t)) THEN
     186              : 
     187            0 :             CALL fm_read(bs_env%fm_RI_RI, bs_env, bs_env%chi_name, i_t)
     188              : 
     189              :             CALL copy_fm_to_dbcsr(bs_env%fm_RI_RI, mat_chi_Gamma_tau(i_t)%matrix, &
     190            0 :                                   keep_sparsity=.FALSE.)
     191              : 
     192            0 :             IF (bs_env%unit_nr > 0) THEN
     193              :                WRITE (bs_env%unit_nr, '(T2,A,I5,A,I3,A,F10.1,A)') &
     194            0 :                   'Read χ(iτ,k=0) from file for time point  ', i_t, ' /', &
     195            0 :                   bs_env%num_time_freq_points, &
     196            0 :                   ', Execution time', m_walltime() - t1, ' s'
     197              :             END IF
     198              : 
     199              :             CYCLE
     200              : 
     201              :          END IF
     202              : 
     203          364 :          IF (.NOT. bs_env%calc_chi(i_t)) CYCLE
     204              : 
     205              :          CALL create_tensors_chi(t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, t_3c_for_Gvir, &
     206          264 :                                  t_3c_x_Gocc, t_3c_x_Gvir, t_3c_x_Gocc_2, t_3c_x_Gvir_2, bs_env)
     207              : 
     208              :          ! 1. compute G^occ and G^vir
     209              :          !    Background: G^σ(iτ) = G^occ,σ(iτ) * Θ(-τ) + G^vir,σ(iτ) * Θ(τ), σ ∈ {↑,↓}
     210              :          !    G^occ,σ_µλ(i|τ|,k=0) = sum_n^occ C^σ_µn(k=0) e^(-|(ϵ^σ_nk=0-ϵ_F)τ|) C^σ_λn(k=0)
     211              :          !    G^vir,σ_µλ(i|τ|,k=0) = sum_n^vir C^σ_µn(k=0) e^(-|(ϵ^σ_nk=0-ϵ_F)τ|) C^σ_λn(k=0)
     212          264 :          tau = bs_env%imag_time_points(i_t)
     213              : 
     214          548 :          DO ispin = 1, bs_env%n_spin
     215          284 :             CALL G_occ_vir(bs_env, tau, bs_env%fm_Gocc, ispin, occ=.TRUE., vir=.FALSE.)
     216          284 :             CALL G_occ_vir(bs_env, tau, bs_env%fm_Gvir, ispin, occ=.FALSE., vir=.TRUE.)
     217              : 
     218              :             CALL fm_to_local_tensor(bs_env%fm_Gocc, bs_env%mat_ao_ao%matrix, &
     219              :                                     bs_env%mat_ao_ao_tensor%matrix, t_2c_Gocc, bs_env, &
     220          284 :                                     bs_env%atoms_j_t_group)
     221              :             CALL fm_to_local_tensor(bs_env%fm_Gvir, bs_env%mat_ao_ao%matrix, &
     222              :                                     bs_env%mat_ao_ao_tensor%matrix, t_2c_Gvir, bs_env, &
     223          284 :                                     bs_env%atoms_i_t_group)
     224              : 
     225              :             ! every group has its own range of i_atoms and j_atoms; only deal with a
     226              :             ! limited number of i_atom-j_atom pairs simultaneously in a group to save memory
     227          832 :             DO i_intval_idx = 1, bs_env%n_intervals_i
     228          852 :                DO j_intval_idx = 1, bs_env%n_intervals_j
     229          852 :                   i_atoms = bs_env%i_atom_intervals(1:2, i_intval_idx)
     230          852 :                   j_atoms = bs_env%j_atom_intervals(1:2, j_intval_idx)
     231              : 
     232          284 :                   IF (bs_env%skip_chi(i_intval_idx, j_intval_idx)) THEN
     233              :                      ! Do that only after first timestep to avoid skips due to vanishing G
     234              :                      ! caused by gaps
     235           14 :                      IF (i_t == 2) THEN
     236            0 :                         bs_env%n_skip_chi = bs_env%n_skip_chi + 1
     237              :                      END IF
     238              :                      CYCLE
     239              :                   END IF
     240              : 
     241          540 :                   DO inner_loop_atoms_interval_index = 1, bs_env%n_intervals_inner_loop_atoms
     242              : 
     243          810 :                      IL_atoms = bs_env%inner_loop_atom_intervals(1:2, inner_loop_atoms_interval_index)
     244              :                      ! Idea: Use sparsity in 3c integrals behind χ_PQ(iτ,k=0)
     245              :                      !   ->  λ   bounds from j_atoms -> sparse in IL_atoms through σ in
     246              :                      !                                   N_Qλν(iτ) = sum_σ (Qλ|σ) G^vir_νσ(i|τ|,k=0)
     247              :                      !   ->  ν   bounds from i_atoms -> sparse in IL_atoms through µ in
     248              :                      !                                   M_Pνλ(iτ) = sum_µ (Pν|µ) G^occ_λµ(i|τ|,k=0)
     249          270 :                      CALL check_dist(i_atoms, IL_atoms, qs_env, bs_env, dist_too_long_i)
     250          270 :                      CALL check_dist(j_atoms, IL_atoms, qs_env, bs_env, dist_too_long_j)
     251          270 :                      IF (.NOT. dist_too_long_i) THEN
     252              :                         ! 2. compute 3-center integrals (Pν|µ) ("|": truncated Coulomb operator)
     253              :                         CALL compute_3c_integrals(qs_env, bs_env, t_3c_for_Gocc, &
     254          270 :                                                   atoms_AO_1=i_atoms, atoms_AO_2=IL_atoms)
     255              :                         ! 3. tensor operation M_Pνλ(iτ) = sum_µ (Pν|µ) G^occ_λµ(i|τ|,k=0)
     256              :                         CALL G_times_3c(t_3c_for_Gocc, t_2c_Gocc, t_3c_x_Gocc, bs_env, &
     257          270 :                                         j_atoms, i_atoms, IL_atoms)
     258              :                      END IF
     259          540 :                      IF (.NOT. dist_too_long_j) THEN
     260              :                         ! 4. compute 3-center integrals (Qλ|σ) ("|": truncated Coulomb operator)
     261              :                         CALL compute_3c_integrals(qs_env, bs_env, t_3c_for_Gvir, &
     262          270 :                                                   atoms_AO_1=j_atoms, atoms_AO_2=IL_atoms)
     263              :                         ! 5. tensor operation N_Qλν(iτ) = sum_σ (Qλ|σ) G^vir_νσ(i|τ|,k=0)
     264              :                         CALL G_times_3c(t_3c_for_Gvir, t_2c_Gvir, t_3c_x_Gvir, bs_env, &
     265          270 :                                         i_atoms, j_atoms, IL_atoms)
     266              :                      END IF
     267              :                   END DO ! IL_atoms
     268              : 
     269              :                   ! 6. reorder tensors: M_Pνλ -> M_Pλν
     270          270 :                   CALL dbt_copy(t_3c_x_Gocc, t_3c_x_Gocc_2, move_data=.TRUE., order=[1, 3, 2])
     271          270 :                   CALL dbt_copy(t_3c_x_Gvir, t_3c_x_Gvir_2, move_data=.TRUE.)
     272              : 
     273              :                   ! 7. tensor operation χ_PQ(iτ,k=0) = sum_λν M_Pλν(iτ) N_Qλν(iτ),
     274              :                   ! Bounds:
     275              :                   ! "comb" (combined index)
     276              :                   !   ->  λ   bounds from j_atoms
     277              :                   !   ->  ν   bounds from i_atoms
     278              :                   ! P   -> sparse in ν (see 3.)
     279              :                   ! Q   -> sparse in λ (see 5.)
     280              :                   bounds_comb(1:2, 1) = [bs_env%i_ao_start_from_atom(j_atoms(1)), &
     281          810 :                                          bs_env%i_ao_end_from_atom(j_atoms(2))]
     282              :                   bounds_comb(1:2, 2) = [bs_env%i_ao_start_from_atom(i_atoms(1)), &
     283          810 :                                          bs_env%i_ao_end_from_atom(i_atoms(2))]
     284              : 
     285              :                   CALL get_bounds_from_atoms(bounds_P, i_atoms, [1, bs_env%n_atom], &
     286              :                                              bs_env%min_RI_idx_from_AO_AO_atom, &
     287          810 :                                              bs_env%max_RI_idx_from_AO_AO_atom)
     288              :                   CALL get_bounds_from_atoms(bounds_Q, [1, bs_env%n_atom], j_atoms, &
     289              :                                              bs_env%min_RI_idx_from_AO_AO_atom, &
     290          810 :                                              bs_env%max_RI_idx_from_AO_AO_atom)
     291              : 
     292          270 :                   IF (bounds_Q(1) > bounds_Q(2) .OR. bounds_P(1) > bounds_P(2)) THEN
     293            0 :                      flop = 0_int_8
     294              :                   ELSE
     295              :                      CALL dbt_contract(alpha=bs_env%spin_degeneracy, &
     296              :                                        tensor_1=t_3c_x_Gocc_2, tensor_2=t_3c_x_Gvir_2, &
     297              :                                        beta=1.0_dp, tensor_3=bs_env%t_chi, &
     298              :                                        contract_1=[2, 3], notcontract_1=[1], map_1=[1], &
     299              :                                        contract_2=[2, 3], notcontract_2=[1], map_2=[2], &
     300              :                                        bounds_1=bounds_comb, &
     301              :                                        bounds_2=bounds_P, &
     302              :                                        bounds_3=bounds_Q, &
     303              :                                        filter_eps=bs_env%eps_filter, move_data=.FALSE., flop=flop, &
     304              :                                        unit_nr=bs_env%unit_nr_contract, &
     305          270 :                                        log_verbose=bs_env%print_contract_verbose)
     306              :                   END IF
     307          554 :                   IF (flop == 0_int_8) bs_env%skip_chi(i_intval_idx, j_intval_idx) = .TRUE.
     308              : 
     309              :                END DO ! j_atoms
     310              :             END DO ! i_atoms
     311              :          END DO ! ispin
     312              : 
     313              :          ! 8. communicate data of χ_PQ(iτ,k=0) in tensor bs_env%t_chi (which local in the
     314              :          !    subgroup) to the global dbcsr matrix mat_chi_Gamma_tau (which stores
     315              :          !    χ_PQ(iτ,k=0) for all time points)
     316              :          CALL local_dbt_to_global_mat(bs_env%t_chi, bs_env%mat_RI_RI_tensor%matrix, &
     317          264 :                                       mat_chi_Gamma_tau(i_t)%matrix, bs_env%para_env)
     318              : 
     319              :          CALL write_matrix(mat_chi_Gamma_tau(i_t)%matrix, i_t, bs_env%chi_name, &
     320          264 :                            bs_env%fm_RI_RI, qs_env)
     321              : 
     322              :          CALL destroy_tensors_chi(t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, t_3c_for_Gvir, &
     323          264 :                                   t_3c_x_Gocc, t_3c_x_Gvir, t_3c_x_Gocc_2, t_3c_x_Gvir_2)
     324              : 
     325          288 :          IF (bs_env%unit_nr > 0) THEN
     326              :             WRITE (bs_env%unit_nr, '(T2,A,I13,A,I3,A,F10.1,A)') &
     327          132 :                'Computed χ(iτ,k=0) for time point', i_t, ' /', bs_env%num_time_freq_points, &
     328          264 :                ', Execution time', m_walltime() - t1, ' s'
     329              :          END IF
     330              : 
     331              :       END DO ! i_t
     332              : 
     333           24 :       IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, '(A)') ' '
     334              : 
     335           24 :       CALL timestop(handle)
     336              : 
     337           24 :    END SUBROUTINE get_mat_chi_Gamma_tau
     338              : 
     339              : ! **************************************************************************************************
     340              : !> \brief ...
     341              : !> \param fm ...
     342              : !> \param bs_env ...
     343              : !> \param mat_name ...
     344              : !> \param idx ...
     345              : ! **************************************************************************************************
     346          352 :    SUBROUTINE fm_read(fm, bs_env, mat_name, idx)
     347              :       TYPE(cp_fm_type)                                   :: fm
     348              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     349              :       CHARACTER(LEN=*)                                   :: mat_name
     350              :       INTEGER                                            :: idx
     351              : 
     352              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'fm_read'
     353              : 
     354              :       CHARACTER(LEN=default_path_length)                 :: f_chi
     355              :       INTEGER                                            :: handle, unit_nr
     356              : 
     357          352 :       CALL timeset(routineN, handle)
     358              : 
     359          352 :       unit_nr = -1
     360          352 :       IF (bs_env%para_env%is_source()) THEN
     361              : 
     362          176 :          IF (idx < 10) THEN
     363           87 :             WRITE (f_chi, '(3A,I1,A)') TRIM(bs_env%prefix), TRIM(mat_name), "_0", idx, ".matrix"
     364           89 :          ELSE IF (idx < 100) THEN
     365           89 :             WRITE (f_chi, '(3A,I2,A)') TRIM(bs_env%prefix), TRIM(mat_name), "_", idx, ".matrix"
     366              :          ELSE
     367            0 :             CPABORT('Please implement more than 99 time/frequency points.')
     368              :          END IF
     369              : 
     370              :          CALL open_file(file_name=TRIM(f_chi), file_action="READ", file_form="UNFORMATTED", &
     371          176 :                         file_position="REWIND", file_status="OLD", unit_number=unit_nr)
     372              : 
     373              :       END IF
     374              : 
     375          352 :       CALL cp_fm_read_unformatted(fm, unit_nr)
     376              : 
     377          352 :       IF (bs_env%para_env%is_source()) CALL close_file(unit_number=unit_nr)
     378              : 
     379          352 :       CALL timestop(handle)
     380              : 
     381          352 :    END SUBROUTINE fm_read
     382              : 
     383              : ! **************************************************************************************************
     384              : !> \brief ...
     385              : !> \param t_2c_Gocc ...
     386              : !> \param t_2c_Gvir ...
     387              : !> \param t_3c_for_Gocc ...
     388              : !> \param t_3c_for_Gvir ...
     389              : !> \param t_3c_x_Gocc ...
     390              : !> \param t_3c_x_Gvir ...
     391              : !> \param t_3c_x_Gocc_2 ...
     392              : !> \param t_3c_x_Gvir_2 ...
     393              : !> \param bs_env ...
     394              : ! **************************************************************************************************
     395          264 :    SUBROUTINE create_tensors_chi(t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, t_3c_for_Gvir, &
     396              :                                  t_3c_x_Gocc, t_3c_x_Gvir, t_3c_x_Gocc_2, t_3c_x_Gvir_2, bs_env)
     397              : 
     398              :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, &
     399              :                                                             t_3c_for_Gvir, t_3c_x_Gocc, &
     400              :                                                             t_3c_x_Gvir, t_3c_x_Gocc_2, &
     401              :                                                             t_3c_x_Gvir_2
     402              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     403              : 
     404              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'create_tensors_chi'
     405              : 
     406              :       INTEGER                                            :: handle
     407              : 
     408          264 :       CALL timeset(routineN, handle)
     409              : 
     410          264 :       CALL dbt_create(bs_env%t_G, t_2c_Gocc, name="Gocc 2c (AO|AO)")
     411          264 :       CALL dbt_create(bs_env%t_G, t_2c_Gvir, name="Gvir 2c (AO|AO)")
     412          264 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_for_Gocc, name="Gocc 3c (RI AO|AO)")
     413          264 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_for_Gvir, name="Gvir 3c (RI AO|AO)")
     414          264 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_Gocc, name="xGocc 3c (RI AO|AO)")
     415          264 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_Gvir, name="xGvir 3c (RI AO|AO)")
     416          264 :       CALL dbt_create(bs_env%t_RI__AO_AO, t_3c_x_Gocc_2, name="x2Gocc 3c (RI AO|AO)")
     417          264 :       CALL dbt_create(bs_env%t_RI__AO_AO, t_3c_x_Gvir_2, name="x2Gvir 3c (RI AO|AO)")
     418              : 
     419          264 :       CALL timestop(handle)
     420              : 
     421          264 :    END SUBROUTINE create_tensors_chi
     422              : 
     423              : ! **************************************************************************************************
     424              : !> \brief ...
     425              : !> \param t_2c_Gocc ...
     426              : !> \param t_2c_Gvir ...
     427              : !> \param t_3c_for_Gocc ...
     428              : !> \param t_3c_for_Gvir ...
     429              : !> \param t_3c_x_Gocc ...
     430              : !> \param t_3c_x_Gvir ...
     431              : !> \param t_3c_x_Gocc_2 ...
     432              : !> \param t_3c_x_Gvir_2 ...
     433              : ! **************************************************************************************************
     434          264 :    SUBROUTINE destroy_tensors_chi(t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, t_3c_for_Gvir, &
     435              :                                   t_3c_x_Gocc, t_3c_x_Gvir, t_3c_x_Gocc_2, t_3c_x_Gvir_2)
     436              :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, t_3c_for_Gocc, &
     437              :                                                             t_3c_for_Gvir, t_3c_x_Gocc, &
     438              :                                                             t_3c_x_Gvir, t_3c_x_Gocc_2, &
     439              :                                                             t_3c_x_Gvir_2
     440              : 
     441              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'destroy_tensors_chi'
     442              : 
     443              :       INTEGER                                            :: handle
     444              : 
     445          264 :       CALL timeset(routineN, handle)
     446              : 
     447          264 :       CALL dbt_destroy(t_2c_Gocc)
     448          264 :       CALL dbt_destroy(t_2c_Gvir)
     449          264 :       CALL dbt_destroy(t_3c_for_Gocc)
     450          264 :       CALL dbt_destroy(t_3c_for_Gvir)
     451          264 :       CALL dbt_destroy(t_3c_x_Gocc)
     452          264 :       CALL dbt_destroy(t_3c_x_Gvir)
     453          264 :       CALL dbt_destroy(t_3c_x_Gocc_2)
     454          264 :       CALL dbt_destroy(t_3c_x_Gvir_2)
     455              : 
     456          264 :       CALL timestop(handle)
     457              : 
     458          264 :    END SUBROUTINE destroy_tensors_chi
     459              : 
     460              : ! **************************************************************************************************
     461              : !> \brief ...
     462              : !> \param matrix ...
     463              : !> \param matrix_index ...
     464              : !> \param matrix_name ...
     465              : !> \param fm ...
     466              : !> \param qs_env ...
     467              : ! **************************************************************************************************
     468          852 :    SUBROUTINE write_matrix(matrix, matrix_index, matrix_name, fm, qs_env)
     469              :       TYPE(dbcsr_type)                                   :: matrix
     470              :       INTEGER                                            :: matrix_index
     471              :       CHARACTER(LEN=*)                                   :: matrix_name
     472              :       TYPE(cp_fm_type), INTENT(IN), POINTER              :: fm
     473              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     474              : 
     475              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'write_matrix'
     476              : 
     477              :       INTEGER                                            :: handle
     478              : 
     479          852 :       CALL timeset(routineN, handle)
     480              : 
     481          852 :       CALL cp_fm_set_all(fm, 0.0_dp)
     482              : 
     483          852 :       CALL copy_dbcsr_to_fm(matrix, fm)
     484              : 
     485          852 :       CALL fm_write(fm, matrix_index, matrix_name, qs_env)
     486              : 
     487          852 :       CALL timestop(handle)
     488              : 
     489          852 :    END SUBROUTINE write_matrix
     490              : 
     491              : ! **************************************************************************************************
     492              : !> \brief ...
     493              : !> \param fm ...
     494              : !> \param matrix_index ...
     495              : !> \param matrix_name ...
     496              : !> \param qs_env ...
     497              : ! **************************************************************************************************
     498         1126 :    SUBROUTINE fm_write(fm, matrix_index, matrix_name, qs_env)
     499              :       TYPE(cp_fm_type)                                   :: fm
     500              :       INTEGER                                            :: matrix_index
     501              :       CHARACTER(LEN=*)                                   :: matrix_name
     502              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     503              : 
     504              :       CHARACTER(LEN=*), PARAMETER :: key = 'PROPERTIES%BANDSTRUCTURE%GW%PRINT%RESTART', &
     505              :          routineN = 'fm_write'
     506              : 
     507              :       CHARACTER(LEN=default_path_length)                 :: filename
     508              :       INTEGER                                            :: handle, unit_nr
     509              :       TYPE(cp_logger_type), POINTER                      :: logger
     510              :       TYPE(section_vals_type), POINTER                   :: input
     511              : 
     512         1126 :       CALL timeset(routineN, handle)
     513              : 
     514         1126 :       CALL get_qs_env(qs_env, input=input)
     515              : 
     516         1126 :       logger => cp_get_default_logger()
     517              : 
     518         1126 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, input, key), cp_p_file)) THEN
     519              : 
     520          944 :          IF (matrix_index < 10) THEN
     521          456 :             WRITE (filename, '(3A,I1)') "RESTART_", matrix_name, "_0", matrix_index
     522          488 :          ELSE IF (matrix_index < 100) THEN
     523          488 :             WRITE (filename, '(3A,I2)') "RESTART_", matrix_name, "_", matrix_index
     524              :          ELSE
     525            0 :             CPABORT('Please implement more than 99 time/frequency points.')
     526              :          END IF
     527              : 
     528              :          unit_nr = cp_print_key_unit_nr(logger, input, key, extension=".matrix", &
     529              :                                         file_form="UNFORMATTED", middle_name=TRIM(filename), &
     530          944 :                                         file_position="REWIND", file_action="WRITE")
     531              : 
     532          944 :          CALL cp_fm_write_unformatted(fm, unit_nr)
     533          944 :          IF (unit_nr > 0) THEN
     534          472 :             CALL close_file(unit_nr)
     535              :          END IF
     536              :       END IF
     537              : 
     538         1126 :       CALL timestop(handle)
     539              : 
     540         1126 :    END SUBROUTINE fm_write
     541              : 
     542              : ! **************************************************************************************************
     543              : !> \brief ...
     544              : !> \param bs_env ...
     545              : !> \param tau ...
     546              : !> \param fm_G_Gamma ...
     547              : !> \param ispin ...
     548              : !> \param occ ...
     549              : !> \param vir ...
     550              : ! **************************************************************************************************
     551         3376 :    SUBROUTINE G_occ_vir(bs_env, tau, fm_G_Gamma, ispin, occ, vir)
     552              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     553              :       REAL(KIND=dp)                                      :: tau
     554              :       TYPE(cp_fm_type)                                   :: fm_G_Gamma
     555              :       INTEGER                                            :: ispin
     556              :       LOGICAL                                            :: occ, vir
     557              : 
     558              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'G_occ_vir'
     559              : 
     560              :       INTEGER                                            :: handle, homo, i_row_local, j_col, &
     561              :                                                             j_col_local, n_mo, ncol_local, &
     562              :                                                             nrow_local
     563         1688 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices
     564              :       REAL(KIND=dp)                                      :: tau_E
     565              : 
     566         1688 :       CALL timeset(routineN, handle)
     567              : 
     568         1688 :       CPASSERT(occ .NEQV. vir)
     569              : 
     570              :       CALL cp_fm_get_info(matrix=bs_env%fm_work_mo(1), &
     571              :                           nrow_local=nrow_local, &
     572              :                           ncol_local=ncol_local, &
     573         1688 :                           col_indices=col_indices)
     574              : 
     575         1688 :       n_mo = bs_env%n_ao
     576         1688 :       homo = bs_env%n_occ(ispin)
     577              : 
     578         1688 :       CALL cp_fm_to_fm(bs_env%fm_mo_coeff_Gamma(ispin), bs_env%fm_work_mo(1))
     579              : 
     580         6863 :       DO i_row_local = 1, nrow_local
     581        62112 :          DO j_col_local = 1, ncol_local
     582              : 
     583        55249 :             j_col = col_indices(j_col_local)
     584              : 
     585        55249 :             tau_E = ABS(tau*0.5_dp*(bs_env%eigenval_scf_Gamma(j_col, ispin) - bs_env%e_fermi(ispin)))
     586              : 
     587        55249 :             IF (tau_E < bs_env%stabilize_exp) THEN
     588              :                bs_env%fm_work_mo(1)%local_data(i_row_local, j_col_local) = &
     589        54157 :                   bs_env%fm_work_mo(1)%local_data(i_row_local, j_col_local)*EXP(-tau_E)
     590              :             ELSE
     591         1092 :                bs_env%fm_work_mo(1)%local_data(i_row_local, j_col_local) = 0.0_dp
     592              :             END IF
     593              : 
     594        60424 :             IF ((occ .AND. j_col > homo) .OR. (vir .AND. j_col <= homo)) THEN
     595        27897 :                bs_env%fm_work_mo(1)%local_data(i_row_local, j_col_local) = 0.0_dp
     596              :             END IF
     597              : 
     598              :          END DO
     599              :       END DO
     600              : 
     601              :       CALL parallel_gemm(transa="N", transb="T", m=n_mo, n=n_mo, k=n_mo, alpha=1.0_dp, &
     602              :                          matrix_a=bs_env%fm_work_mo(1), matrix_b=bs_env%fm_work_mo(1), &
     603         1688 :                          beta=0.0_dp, matrix_c=fm_G_Gamma)
     604              : 
     605         1688 :       CALL timestop(handle)
     606              : 
     607         1688 :    END SUBROUTINE G_occ_vir
     608              : 
     609              : ! **************************************************************************************************
     610              : !> \brief ...
     611              : !> \param qs_env ...
     612              : !> \param bs_env ...
     613              : !> \param t_3c ...
     614              : !> \param atoms_AO_1 ...
     615              : !> \param atoms_AO_2 ...
     616              : !> \param atoms_RI ...
     617              : ! **************************************************************************************************
     618         1392 :    SUBROUTINE compute_3c_integrals(qs_env, bs_env, t_3c, atoms_AO_1, atoms_AO_2, atoms_RI)
     619              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     620              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     621              :       TYPE(dbt_type)                                     :: t_3c
     622              :       INTEGER, DIMENSION(2), OPTIONAL                    :: atoms_AO_1, atoms_AO_2, atoms_RI
     623              : 
     624              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_3c_integrals'
     625              : 
     626              :       INTEGER                                            :: handle
     627         1392 :       TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :)       :: t_3c_array
     628              : 
     629         1392 :       CALL timeset(routineN, handle)
     630              : 
     631              :       ! free memory (not clear whether memory has been freed previously)
     632         1392 :       CALL dbt_clear(t_3c)
     633              : 
     634        15312 :       ALLOCATE (t_3c_array(1, 1))
     635         1392 :       CALL dbt_create(t_3c, t_3c_array(1, 1))
     636              : 
     637              :       CALL build_3c_integrals(t_3c_array, &
     638              :                               bs_env%eps_filter, &
     639              :                               qs_env, &
     640              :                               bs_env%nl_3c, &
     641              :                               int_eps=bs_env%eps_filter, &
     642              :                               basis_i=bs_env%basis_set_RI, &
     643              :                               basis_j=bs_env%basis_set_AO, &
     644              :                               basis_k=bs_env%basis_set_AO, &
     645              :                               potential_parameter=bs_env%ri_metric, &
     646              :                               bounds_i=atoms_RI, &
     647              :                               bounds_j=atoms_AO_1, &
     648              :                               bounds_k=atoms_AO_2, &
     649         1392 :                               desymmetrize=.FALSE.)
     650              : 
     651         1392 :       CALL dbt_filter(t_3c_array(1, 1), bs_env%eps_filter)
     652              : 
     653         1392 :       CALL dbt_copy(t_3c_array(1, 1), t_3c, move_data=.TRUE.)
     654              : 
     655         1392 :       CALL dbt_destroy(t_3c_array(1, 1))
     656         2784 :       DEALLOCATE (t_3c_array)
     657              : 
     658         1392 :       CALL timestop(handle)
     659              : 
     660         2784 :    END SUBROUTINE compute_3c_integrals
     661              : 
     662              : ! **************************************************************************************************
     663              : !> \brief ...
     664              : !> \param t_3c_for_G ...
     665              : !> \param t_G ...
     666              : !> \param t_M ...
     667              : !> \param bs_env ...
     668              : !> \param atoms_AO_1 ...
     669              : !> \param atoms_AO_2 ...
     670              : !> \param atoms_IL ...
     671              : ! **************************************************************************************************
     672          540 :    SUBROUTINE G_times_3c(t_3c_for_G, t_G, t_M, bs_env, atoms_AO_1, atoms_AO_2, atoms_IL)
     673              :       TYPE(dbt_type)                                     :: t_3c_for_G, t_G, t_M
     674              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     675              :       INTEGER, DIMENSION(2)                              :: atoms_AO_1, atoms_AO_2, atoms_IL
     676              : 
     677              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'G_times_3c'
     678              : 
     679              :       INTEGER                                            :: handle
     680              :       INTEGER(KIND=int_8)                                :: flop
     681              :       INTEGER, DIMENSION(2)                              :: bounds_ao_1, bounds_IL
     682              :       INTEGER, DIMENSION(2, 2)                           :: bounds_comb
     683              : 
     684          540 :       CALL timeset(routineN, handle)
     685              : 
     686              :       ! Bounds reduce needed memory and therefore scaling behavior
     687              :       ! Operations are of the form, e.g, M_Pνλ = sum_µ (Pν|µ) G_λµ
     688              :       ! "comb" (combined index)
     689              :       !   ->  P   sparse in ν and µ
     690              :       !   ->  λ   bounds from j_atoms (via atoms_AO_1)
     691              :       ! µ   bounds from inner loop "IL" indices and sparse in P and ν
     692              :       ! ν   bounds from i_atoms (via atoms_AO_2) and sparse in P and µ
     693              : 
     694              :       ! µ index
     695              :       CALL get_bounds_from_atoms(bounds_IL, [1, bs_env%n_atom], atoms_AO_2, &
     696              :                                  bs_env%min_AO_idx_from_RI_AO_atom, &
     697              :                                  bs_env%max_AO_idx_from_RI_AO_atom, &
     698              :                                  atoms_3=atoms_IL, &
     699              :                                  indices_3_start=bs_env%i_ao_start_from_atom, &
     700         1620 :                                  indices_3_end=bs_env%i_ao_end_from_atom)
     701              : 
     702              :       ! P index
     703              :       CALL get_bounds_from_atoms(bounds_comb(:, 1), atoms_IL, atoms_AO_2, &
     704              :                                  bs_env%min_RI_idx_from_AO_AO_atom, &
     705          540 :                                  bs_env%max_RI_idx_from_AO_AO_atom)
     706              : 
     707              :       ! ν index
     708              :       CALL get_bounds_from_atoms(bounds_comb(:, 2), [1, bs_env%n_atom], atoms_IL, &
     709              :                                  bs_env%min_AO_idx_from_RI_AO_atom, &
     710              :                                  bs_env%max_AO_idx_from_RI_AO_atom, &
     711              :                                  atoms_3=atoms_AO_2, &
     712              :                                  indices_3_start=bs_env%i_ao_start_from_atom, &
     713         1620 :                                  indices_3_end=bs_env%i_ao_end_from_atom)
     714              : 
     715              :       ! λ index
     716              :       bounds_ao_1(1:2) = [bs_env%i_ao_start_from_atom(atoms_AO_1(1)), &
     717         1620 :                           bs_env%i_ao_end_from_atom(atoms_AO_1(2))]
     718              : 
     719          540 :       IF (bounds_IL(1) > bounds_IL(2) .OR. bounds_comb(1, 2) > bounds_comb(2, 2)) THEN
     720              :          flop = 0_int_8
     721              :       ELSE
     722              :          CALL dbt_contract(alpha=1.0_dp, &
     723              :                            tensor_1=t_3c_for_G, &
     724              :                            tensor_2=t_G, &
     725              :                            beta=1.0_dp, &
     726              :                            tensor_3=t_M, &
     727              :                            contract_1=[3], notcontract_1=[1, 2], map_1=[1, 2], &
     728              :                            contract_2=[2], notcontract_2=[1], map_2=[3], &
     729              :                            bounds_1=bounds_IL, &
     730              :                            bounds_2=bounds_comb, &
     731              :                            bounds_3=bounds_ao_1, &
     732              :                            flop=flop, &
     733              :                            filter_eps=bs_env%eps_filter, &
     734              :                            unit_nr=bs_env%unit_nr_contract, &
     735          540 :                            log_verbose=bs_env%print_contract_verbose)
     736              :       END IF
     737              : 
     738          540 :       CALL dbt_clear(t_3c_for_G)
     739              : 
     740          540 :       CALL timestop(handle)
     741              : 
     742          540 :    END SUBROUTINE G_times_3c
     743              : 
     744              : ! **************************************************************************************************
     745              : !> \brief ...
     746              : !> \param atoms_1 ...
     747              : !> \param atoms_2 ...
     748              : !> \param qs_env ...
     749              : !> \param bs_env ...
     750              : !> \param dist_too_long ...
     751              : ! **************************************************************************************************
     752          540 :    SUBROUTINE check_dist(atoms_1, atoms_2, qs_env, bs_env, dist_too_long)
     753              :       INTEGER, DIMENSION(2)                              :: atoms_1, atoms_2
     754              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     755              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     756              :       LOGICAL                                            :: dist_too_long
     757              : 
     758              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'check_dist'
     759              : 
     760              :       INTEGER                                            :: atom_1, atom_2, handle
     761              :       REAL(dp)                                           :: abs_rab, min_dist_AO_atoms
     762              :       REAL(KIND=dp), DIMENSION(3)                        :: rab
     763              :       TYPE(cell_type), POINTER                           :: cell
     764          540 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     765              : 
     766          540 :       CALL timeset(routineN, handle)
     767              : 
     768          540 :       CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set)
     769              : 
     770          540 :       min_dist_AO_atoms = HUGE(1.0_dp)
     771         1668 :       DO atom_1 = atoms_1(1), atoms_1(2)
     772         4068 :          DO atom_2 = atoms_2(1), atoms_2(2)
     773         2400 :             rab = pbc(particle_set(atom_1)%r(1:3), particle_set(atom_2)%r(1:3), cell)
     774              : 
     775         2400 :             abs_rab = SQRT(rab(1)**2 + rab(2)**2 + rab(3)**2)
     776              : 
     777         3528 :             min_dist_AO_atoms = MIN(min_dist_AO_atoms, abs_rab)
     778              :          END DO
     779              :       END DO
     780              : 
     781          540 :       dist_too_long = (min_dist_AO_atoms > bs_env%max_dist_AO_atoms)
     782              : 
     783          540 :       CALL timestop(handle)
     784              : 
     785          540 :    END SUBROUTINE check_dist
     786              : 
     787              : ! **************************************************************************************************
     788              : !> \brief ...
     789              : !> \param bs_env ...
     790              : !> \param qs_env ...
     791              : !> \param mat_chi_Gamma_tau ...
     792              : !> \param fm_W_MIC_time ...
     793              : ! **************************************************************************************************
     794           24 :    SUBROUTINE get_W_MIC(bs_env, qs_env, mat_chi_Gamma_tau, fm_W_MIC_time)
     795              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     796              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     797              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: mat_chi_Gamma_tau
     798              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_W_MIC_time
     799              : 
     800              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_W_MIC'
     801              : 
     802              :       INTEGER                                            :: handle
     803              : 
     804           24 :       CALL timeset(routineN, handle)
     805              : 
     806           24 :       IF (bs_env%all_W_exist) THEN
     807            6 :          CALL read_W_MIC_time(bs_env, mat_chi_Gamma_tau, fm_W_MIC_time)
     808              :       ELSE
     809           18 :          CALL compute_W_MIC(bs_env, qs_env, mat_chi_Gamma_tau, fm_W_MIC_time)
     810              :       END IF
     811              : 
     812           24 :       CALL timestop(handle)
     813              : 
     814           24 :    END SUBROUTINE get_W_MIC
     815              : 
     816              : ! **************************************************************************************************
     817              : !> \brief ...
     818              : !> \param bs_env ...
     819              : !> \param qs_env ...
     820              : !> \param fm_V_kp ...
     821              : !> \param ikp_batch ...
     822              : ! **************************************************************************************************
     823           66 :    SUBROUTINE compute_V_k_by_lattice_sum(bs_env, qs_env, fm_V_kp, ikp_batch)
     824              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     825              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     826              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_V_kp
     827              :       INTEGER                                            :: ikp_batch
     828              : 
     829              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_V_k_by_lattice_sum'
     830              : 
     831              :       INTEGER                                            :: handle, ikp, ikp_end, ikp_start, &
     832              :                                                             nkp_chi_eps_W_batch, re_im
     833           66 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     834              :       TYPE(cell_type), POINTER                           :: cell
     835           66 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: mat_V_kp
     836           66 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     837           66 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     838              : 
     839           66 :       CALL timeset(routineN, handle)
     840              : 
     841           66 :       nkp_chi_eps_W_batch = bs_env%nkp_chi_eps_W_batch
     842              : 
     843           66 :       ikp_start = (ikp_batch - 1)*bs_env%nkp_chi_eps_W_batch + 1
     844           66 :       ikp_end = MIN(ikp_batch*bs_env%nkp_chi_eps_W_batch, bs_env%kpoints_chi_eps_W%nkp)
     845              : 
     846           66 :       NULLIFY (mat_V_kp)
     847          832 :       ALLOCATE (mat_V_kp(ikp_start:ikp_end, 2))
     848              : 
     849          198 :       DO re_im = 1, 2
     850          634 :          DO ikp = ikp_start, ikp_end
     851          436 :             NULLIFY (mat_V_kp(ikp, re_im)%matrix)
     852          436 :             ALLOCATE (mat_V_kp(ikp, re_im)%matrix)
     853          436 :             CALL dbcsr_create(mat_V_kp(ikp, re_im)%matrix, template=bs_env%mat_RI_RI%matrix)
     854          436 :             CALL dbcsr_reserve_all_blocks(mat_V_kp(ikp, re_im)%matrix)
     855          568 :             CALL dbcsr_set(mat_V_kp(ikp, re_im)%matrix, 0.0_dp)
     856              :          END DO ! ikp
     857              :       END DO ! re_im
     858              : 
     859              :       CALL get_qs_env(qs_env=qs_env, &
     860              :                       particle_set=particle_set, &
     861              :                       cell=cell, &
     862              :                       qs_kind_set=qs_kind_set, &
     863           66 :                       atomic_kind_set=atomic_kind_set)
     864              : 
     865           66 :       IF (ikp_end <= bs_env%nkp_chi_eps_W_orig) THEN
     866              : 
     867              :          ! 1. 2c Coulomb integrals for the first "original" k-point grid
     868          104 :          bs_env%kpoints_chi_eps_W%nkp_grid = bs_env%nkp_grid_chi_eps_W_orig
     869              : 
     870           40 :       ELSE IF (ikp_start > bs_env%nkp_chi_eps_W_orig .AND. &
     871              :                ikp_end <= bs_env%nkp_chi_eps_W_orig_plus_extra) THEN
     872              : 
     873              :          ! 2. 2c Coulomb integrals for the second "extrapolation" k-point grid
     874          160 :          bs_env%kpoints_chi_eps_W%nkp_grid = bs_env%nkp_grid_chi_eps_W_extra
     875              : 
     876              :       ELSE
     877              : 
     878            0 :          CPABORT("Error with k-point parallelization.")
     879              : 
     880              :       END IF
     881              : 
     882              :       CALL build_2c_coulomb_matrix_kp(mat_V_kp, &
     883              :                                       bs_env%kpoints_chi_eps_W, &
     884              :                                       basis_type="RI_AUX", &
     885              :                                       cell=cell, &
     886              :                                       particle_set=particle_set, &
     887              :                                       qs_kind_set=qs_kind_set, &
     888              :                                       atomic_kind_set=atomic_kind_set, &
     889              :                                       size_lattice_sum=bs_env%size_lattice_sum_V, &
     890              :                                       operator_type=operator_coulomb, &
     891              :                                       ikp_start=ikp_start, &
     892           66 :                                       ikp_end=ikp_end)
     893              : 
     894          264 :       bs_env%kpoints_chi_eps_W%nkp_grid = bs_env%nkp_grid_chi_eps_W_orig
     895              : 
     896          832 :       ALLOCATE (fm_V_kp(ikp_start:ikp_end, 2))
     897          198 :       DO re_im = 1, 2
     898          634 :          DO ikp = ikp_start, ikp_end
     899          436 :             CALL cp_fm_create(fm_V_kp(ikp, re_im), bs_env%fm_RI_RI%matrix_struct)
     900          436 :             CALL copy_dbcsr_to_fm(mat_V_kp(ikp, re_im)%matrix, fm_V_kp(ikp, re_im))
     901          568 :             CALL dbcsr_deallocate_matrix(mat_V_kp(ikp, re_im)%matrix)
     902              :          END DO
     903              :       END DO
     904           66 :       DEALLOCATE (mat_V_kp)
     905              : 
     906           66 :       CALL timestop(handle)
     907              : 
     908           66 :    END SUBROUTINE compute_V_k_by_lattice_sum
     909              : 
     910              : ! **************************************************************************************************
     911              : !> \brief ...
     912              : !> \param bs_env ...
     913              : !> \param qs_env ...
     914              : !> \param fm_V_kp ...
     915              : !> \param cfm_V_sqrt_ikp ...
     916              : !> \param cfm_M_inv_V_sqrt_ikp ...
     917              : !> \param ikp ...
     918              : ! **************************************************************************************************
     919          218 :    SUBROUTINE compute_MinvVsqrt_Vsqrt(bs_env, qs_env, fm_V_kp, cfm_V_sqrt_ikp, &
     920              :                                       cfm_M_inv_V_sqrt_ikp, ikp)
     921              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     922              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     923              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_V_kp
     924              :       TYPE(cp_cfm_type)                                  :: cfm_V_sqrt_ikp, cfm_M_inv_V_sqrt_ikp
     925              :       INTEGER                                            :: ikp
     926              : 
     927              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_MinvVsqrt_Vsqrt'
     928              : 
     929              :       INTEGER                                            :: handle, info, n_RI
     930              :       TYPE(cp_cfm_type)                                  :: cfm_M_inv_ikp, cfm_work
     931          218 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_M_ikp
     932              : 
     933          218 :       CALL timeset(routineN, handle)
     934              : 
     935          218 :       n_RI = bs_env%n_RI
     936              : 
     937              :       ! get here M(k) and write it to fm_M_ikp
     938              :       CALL RI_2c_integral_mat(qs_env, fm_M_ikp, fm_V_kp(ikp, 1), &
     939              :                               n_RI, bs_env%ri_metric, do_kpoints=.TRUE., &
     940              :                               kpoints=bs_env%kpoints_chi_eps_W, &
     941              :                               regularization_RI=bs_env%regularization_RI, ikp_ext=ikp, &
     942          218 :                               do_build_cell_index=(ikp == 1))
     943              : 
     944          218 :       IF (ikp == 1) THEN
     945           18 :          CALL cp_cfm_create(cfm_V_sqrt_ikp, fm_V_kp(ikp, 1)%matrix_struct)
     946           18 :          CALL cp_cfm_create(cfm_M_inv_V_sqrt_ikp, fm_V_kp(ikp, 1)%matrix_struct)
     947              :       END IF
     948          218 :       CALL cp_cfm_create(cfm_M_inv_ikp, fm_V_kp(ikp, 1)%matrix_struct)
     949              : 
     950          218 :       CALL cp_fm_to_cfm(fm_M_ikp(1, 1), fm_M_ikp(1, 2), cfm_M_inv_ikp)
     951          218 :       CALL cp_fm_to_cfm(fm_V_kp(ikp, 1), fm_V_kp(ikp, 2), cfm_V_sqrt_ikp)
     952              : 
     953          218 :       CALL cp_fm_release(fm_M_ikp)
     954              : 
     955          218 :       CALL cp_cfm_create(cfm_work, fm_V_kp(ikp, 1)%matrix_struct)
     956              : 
     957              :       ! M(k) -> M^-1(k)
     958          218 :       CALL cp_cfm_to_cfm(cfm_M_inv_ikp, cfm_work)
     959          218 :       CALL cp_cfm_cholesky_decompose(matrix=cfm_M_inv_ikp, n=n_RI, info_out=info)
     960          218 :       IF (info == 0) THEN
     961              :          ! successful Cholesky decomposition
     962          218 :          CALL cp_cfm_cholesky_invert(cfm_M_inv_ikp)
     963              :          ! symmetrize the result
     964          218 :          CALL cp_cfm_uplo_to_full(cfm_M_inv_ikp)
     965              :       ELSE
     966              :          ! Cholesky decomposition not successful: use expensive diagonalization
     967            0 :          CALL cp_cfm_power(cfm_work, threshold=bs_env%eps_eigval_mat_RI, exponent=-1.0_dp)
     968            0 :          CALL cp_cfm_to_cfm(cfm_work, cfm_M_inv_ikp)
     969              :       END IF
     970              : 
     971              :       ! V(k) -> L(k) with L^H(k)*L(k) = V(k) [L(k) can be just considered to be V^0.5(k)]
     972          218 :       CALL cp_cfm_to_cfm(cfm_V_sqrt_ikp, cfm_work)
     973          218 :       CALL cp_cfm_cholesky_decompose(matrix=cfm_V_sqrt_ikp, n=n_RI, info_out=info)
     974          218 :       IF (info == 0) THEN
     975              :          ! successful Cholesky decomposition
     976          218 :          CALL clean_lower_part(cfm_V_sqrt_ikp)
     977              :       ELSE
     978              :          ! Cholesky decomposition not successful: use expensive diagonalization
     979            0 :          CALL cp_cfm_power(cfm_work, threshold=0.0_dp, exponent=0.5_dp)
     980            0 :          CALL cp_cfm_to_cfm(cfm_work, cfm_V_sqrt_ikp)
     981              :       END IF
     982          218 :       CALL cp_cfm_release(cfm_work)
     983              : 
     984              :       ! get M^-1(k)*V^0.5(k)
     985              :       CALL parallel_gemm("N", "C", n_RI, n_RI, n_RI, z_one, cfm_M_inv_ikp, cfm_V_sqrt_ikp, &
     986          218 :                          z_zero, cfm_M_inv_V_sqrt_ikp)
     987              : 
     988          218 :       CALL cp_cfm_release(cfm_M_inv_ikp)
     989              : 
     990          218 :       CALL timestop(handle)
     991              : 
     992          436 :    END SUBROUTINE compute_MinvVsqrt_Vsqrt
     993              : 
     994              : ! **************************************************************************************************
     995              : !> \brief ...
     996              : !> \param bs_env ...
     997              : !> \param mat_chi_Gamma_tau ...
     998              : !> \param fm_W_MIC_time ...
     999              : ! **************************************************************************************************
    1000            6 :    SUBROUTINE read_W_MIC_time(bs_env, mat_chi_Gamma_tau, fm_W_MIC_time)
    1001              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1002              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: mat_chi_Gamma_tau
    1003              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_W_MIC_time
    1004              : 
    1005              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'read_W_MIC_time'
    1006              : 
    1007              :       INTEGER                                            :: handle, i_t
    1008              :       REAL(KIND=dp)                                      :: t1
    1009              : 
    1010            6 :       CALL timeset(routineN, handle)
    1011              : 
    1012            6 :       CALL dbcsr_deallocate_matrix_set(mat_chi_Gamma_tau)
    1013            6 :       CALL create_fm_W_MIC_time(bs_env, fm_W_MIC_time)
    1014              : 
    1015          106 :       DO i_t = 1, bs_env%num_time_freq_points
    1016              : 
    1017          100 :          t1 = m_walltime()
    1018              : 
    1019          100 :          CALL fm_read(fm_W_MIC_time(i_t), bs_env, bs_env%W_time_name, i_t)
    1020              : 
    1021          106 :          IF (bs_env%unit_nr > 0) THEN
    1022              :             WRITE (bs_env%unit_nr, '(T2,A,I5,A,I3,A,F10.1,A)') &
    1023           50 :                'Read W^MIC(iτ) from file for time point  ', i_t, ' /', bs_env%num_time_freq_points, &
    1024          100 :                ', Execution time', m_walltime() - t1, ' s'
    1025              :          END IF
    1026              : 
    1027              :       END DO
    1028              : 
    1029            6 :       IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, '(A)') ' '
    1030              : 
    1031              :       ! Marek : Reading of the W(w=0) potential for RTP
    1032              :       ! TODO : is the condition bs_env%all_W_exist sufficient for reading?
    1033            6 :       IF (bs_env%rtp_method == rtp_method_bse) THEN
    1034            4 :          CALL cp_fm_create(bs_env%fm_W_MIC_freq_zero, bs_env%fm_W_MIC_freq%matrix_struct)
    1035            4 :          t1 = m_walltime()
    1036            4 :          CALL fm_read(bs_env%fm_W_MIC_freq_zero, bs_env, "W_freq_rtp", 0)
    1037            4 :          IF (bs_env%unit_nr > 0) THEN
    1038              :             WRITE (bs_env%unit_nr, '(T2,A,I3,A,I3,A,F10.1,A)') &
    1039            2 :                'Read W^MIC(f=0) from file for freq. point  ', 1, ' /', 1, &
    1040            4 :                ', Execution time', m_walltime() - t1, ' s'
    1041              :          END IF
    1042              :       END IF
    1043              : 
    1044            6 :       CALL timestop(handle)
    1045              : 
    1046            6 :    END SUBROUTINE read_W_MIC_time
    1047              : 
    1048              : ! **************************************************************************************************
    1049              : !> \brief ...
    1050              : !> \param bs_env ...
    1051              : !> \param qs_env ...
    1052              : !> \param mat_chi_Gamma_tau ...
    1053              : !> \param fm_W_MIC_time ...
    1054              : ! **************************************************************************************************
    1055           18 :    SUBROUTINE compute_W_MIC(bs_env, qs_env, mat_chi_Gamma_tau, fm_W_MIC_time)
    1056              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1057              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1058              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: mat_chi_Gamma_tau
    1059              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_W_MIC_time
    1060              : 
    1061              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'compute_W_MIC'
    1062              : 
    1063              :       INTEGER                                            :: handle, i_t, ikp, ikp_batch, &
    1064              :                                                             ikp_in_batch, j_w
    1065              :       REAL(KIND=dp)                                      :: t1
    1066              :       TYPE(cp_cfm_type)                                  :: cfm_M_inv_V_sqrt_ikp, cfm_V_sqrt_ikp
    1067           18 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_V_kp
    1068              : 
    1069           18 :       CALL timeset(routineN, handle)
    1070              : 
    1071           18 :       CALL create_fm_W_MIC_time(bs_env, fm_W_MIC_time)
    1072              : 
    1073           84 :       DO ikp_batch = 1, bs_env%num_chi_eps_W_batches
    1074              : 
    1075           66 :          t1 = m_walltime()
    1076              : 
    1077              :          ! Compute V_PQ(k) = sum_R e^(ikR) <phi_P, cell 0 | 1/r | phi_Q, cell R>
    1078           66 :          CALL compute_V_k_by_lattice_sum(bs_env, qs_env, fm_V_kp, ikp_batch)
    1079              : 
    1080          330 :          DO ikp_in_batch = 1, bs_env%nkp_chi_eps_W_batch
    1081              : 
    1082          264 :             ikp = (ikp_batch - 1)*bs_env%nkp_chi_eps_W_batch + ikp_in_batch
    1083              : 
    1084          264 :             IF (ikp > bs_env%nkp_chi_eps_W_orig_plus_extra) CYCLE
    1085              : 
    1086              :             CALL compute_MinvVsqrt_Vsqrt(bs_env, qs_env, fm_V_kp, &
    1087          218 :                                          cfm_V_sqrt_ikp, cfm_M_inv_V_sqrt_ikp, ikp)
    1088              : 
    1089          218 :             CALL bs_env%para_env%sync()
    1090          218 :             CALL cp_fm_release(fm_V_kp(ikp, 1))
    1091          218 :             CALL cp_fm_release(fm_V_kp(ikp, 2))
    1092              : 
    1093         2148 :             DO j_w = 1, bs_env%num_time_freq_points
    1094              : 
    1095              :                ! check if we need this (ikp, ω_j) combination for approximate k-point extrapolation
    1096         1864 :                IF (bs_env%approx_kp_extrapol .AND. j_w > 1 .AND. &
    1097              :                    ikp > bs_env%nkp_chi_eps_W_orig) CYCLE
    1098              : 
    1099              :                CALL compute_fm_W_MIC_freq_j(bs_env, qs_env, bs_env%fm_W_MIC_freq, j_w, ikp, &
    1100              :                                             mat_chi_Gamma_tau, cfm_M_inv_V_sqrt_ikp, &
    1101         1540 :                                             cfm_V_sqrt_ikp)
    1102              : 
    1103              :                ! Fourier trafo from W_PQ^MIC(iω_j) to W_PQ^MIC(iτ)
    1104         2128 :                CALL Fourier_transform_w_to_t(bs_env, fm_W_MIC_time, bs_env%fm_W_MIC_freq, j_w)
    1105              : 
    1106              :             END DO ! ω_j
    1107              : 
    1108              :          END DO ! ikp_in_batch
    1109              : 
    1110           66 :          DEALLOCATE (fm_V_kp)
    1111              : 
    1112           84 :          IF (bs_env%unit_nr > 0) THEN
    1113              :             WRITE (bs_env%unit_nr, '(T2,A,I12,A,I3,A,F10.1,A)') &
    1114           33 :                'Computed W(iτ,k) for k-point batch', &
    1115           33 :                ikp_batch, ' /', bs_env%num_chi_eps_W_batches, &
    1116           66 :                ', Execution time', m_walltime() - t1, ' s'
    1117              :          END IF
    1118              : 
    1119              :       END DO ! ikp_batch
    1120              : 
    1121           18 :       IF (bs_env%approx_kp_extrapol) THEN
    1122            2 :          CALL apply_extrapol_factor(bs_env, fm_W_MIC_time)
    1123              :       END IF
    1124              : 
    1125              :       ! M^-1(k=0)*W^MIC(iτ)*M^-1(k=0)
    1126           18 :       CALL multiply_fm_W_MIC_time_with_Minv_Gamma(bs_env, qs_env, fm_W_MIC_time)
    1127              : 
    1128          282 :       DO i_t = 1, bs_env%num_time_freq_points
    1129          282 :          CALL fm_write(fm_W_MIC_time(i_t), i_t, bs_env%W_time_name, qs_env)
    1130              :       END DO
    1131              : 
    1132           18 :       CALL cp_cfm_release(cfm_M_inv_V_sqrt_ikp)
    1133           18 :       CALL cp_cfm_release(cfm_V_sqrt_ikp)
    1134           18 :       CALL dbcsr_deallocate_matrix_set(mat_chi_Gamma_tau)
    1135              : 
    1136              :       ! Marek : Fourier transform W^MIC(itau) back to get it at a specific im.frequency point - iomega = 0
    1137           18 :       IF (bs_env%rtp_method == rtp_method_bse) THEN
    1138           10 :          t1 = m_walltime()
    1139           10 :          CALL cp_fm_create(bs_env%fm_W_MIC_freq_zero, bs_env%fm_W_MIC_freq%matrix_struct)
    1140              :          ! Set to zero
    1141           10 :          CALL cp_fm_set_all(bs_env%fm_W_MIC_freq_zero, 0.0_dp)
    1142              :          ! Sum over all times
    1143          210 :          DO i_t = 1, bs_env%num_time_freq_points
    1144              :             ! Add the relevant structure with correct weight
    1145              :             CALL cp_fm_scale_and_add(1.0_dp, bs_env%fm_W_MIC_freq_zero, &
    1146          210 :                                      bs_env%imag_time_weights_freq_zero(i_t), fm_W_MIC_time(i_t))
    1147              :          END DO
    1148              :          ! Done, save to file
    1149           10 :          CALL fm_write(bs_env%fm_W_MIC_freq_zero, 0, "W_freq_rtp", qs_env)
    1150              :          ! Report calculation
    1151           10 :          IF (bs_env%unit_nr > 0) THEN
    1152              :             WRITE (bs_env%unit_nr, '(T2,A,I11,A,I3,A,F10.1,A)') &
    1153            5 :                'Computed W(f=0,k) for k-point batch', &
    1154            5 :                1, ' /', 1, &
    1155           10 :                ', Execution time', m_walltime() - t1, ' s'
    1156              :          END IF
    1157              :       END IF
    1158              : 
    1159           18 :       IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, '(A)') ' '
    1160              : 
    1161           18 :       CALL timestop(handle)
    1162              : 
    1163           36 :    END SUBROUTINE compute_W_MIC
    1164              : 
    1165              : ! **************************************************************************************************
    1166              : !> \brief ...
    1167              : !> \param bs_env ...
    1168              : !> \param qs_env ...
    1169              : !> \param fm_W_MIC_freq_j ...
    1170              : !> \param j_w ...
    1171              : !> \param ikp ...
    1172              : !> \param mat_chi_Gamma_tau ...
    1173              : !> \param cfm_M_inv_V_sqrt_ikp ...
    1174              : !> \param cfm_V_sqrt_ikp ...
    1175              : ! **************************************************************************************************
    1176         1540 :    SUBROUTINE compute_fm_W_MIC_freq_j(bs_env, qs_env, fm_W_MIC_freq_j, j_w, ikp, mat_chi_Gamma_tau, &
    1177              :                                       cfm_M_inv_V_sqrt_ikp, cfm_V_sqrt_ikp)
    1178              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1179              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1180              :       TYPE(cp_fm_type)                                   :: fm_W_MIC_freq_j
    1181              :       INTEGER                                            :: j_w, ikp
    1182              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: mat_chi_Gamma_tau
    1183              :       TYPE(cp_cfm_type)                                  :: cfm_M_inv_V_sqrt_ikp, cfm_V_sqrt_ikp
    1184              : 
    1185              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_fm_W_MIC_freq_j'
    1186              : 
    1187              :       INTEGER                                            :: handle
    1188              :       TYPE(cp_cfm_type)                                  :: cfm_chi_ikp_freq_j, cfm_W_ikp_freq_j
    1189              : 
    1190         1540 :       CALL timeset(routineN, handle)
    1191              : 
    1192              :       ! 1. Fourier transformation of χ_PQ(iτ,k=0) to χ_PQ(iω_j,k=0)
    1193         1540 :       CALL compute_fm_chi_Gamma_freq(bs_env, bs_env%fm_chi_Gamma_freq, j_w, mat_chi_Gamma_tau)
    1194              : 
    1195         1540 :       CALL cp_fm_set_all(fm_W_MIC_freq_j, 0.0_dp)
    1196              : 
    1197              :       ! 2. Get χ_PQ(iω_j,k_i) from χ_PQ(iω_j,k=0) using the minimum image convention
    1198              :       CALL cfm_ikp_from_fm_Gamma(cfm_chi_ikp_freq_j, bs_env%fm_chi_Gamma_freq, &
    1199         1540 :                                  ikp, qs_env, bs_env%kpoints_chi_eps_W, "RI_AUX")
    1200              : 
    1201              :       ! 3. Remove all negative eigenvalues from χ_PQ(iω_j,k_i)
    1202         1540 :       CALL cp_cfm_power(cfm_chi_ikp_freq_j, threshold=0.0_dp, exponent=1.0_dp)
    1203              : 
    1204              :       ! 4. ε(iω_j,k_i) = Id - V^0.5(k_i)*M^-1(k_i)*χ(iω_j,k_i)*M^-1(k_i)*V^0.5(k_i)
    1205              :       !    W(iω_j,k_i) = V^0.5(k_i)*(ε^-1(iω_j,k_i)-Id)*V^0.5(k_i)
    1206              :       CALL compute_cfm_W_ikp_freq_j(bs_env, cfm_chi_ikp_freq_j, cfm_V_sqrt_ikp, &
    1207         1540 :                                     cfm_M_inv_V_sqrt_ikp, cfm_W_ikp_freq_j)
    1208              : 
    1209              :       ! 5. k-point integration W_PQ(iω_j, k_i) to W_PQ^MIC(iω_j)
    1210         1540 :       SELECT CASE (bs_env%approx_kp_extrapol)
    1211              :       CASE (.FALSE.)
    1212              :          ! default: standard k-point extrapolation
    1213              :          CALL MIC_contribution_from_ikp(bs_env, qs_env, fm_W_MIC_freq_j, cfm_W_ikp_freq_j, ikp, &
    1214         1540 :                                         bs_env%kpoints_chi_eps_W, "RI_AUX")
    1215              :       CASE (.TRUE.)
    1216              :          ! for approximate kpoint extrapolation: get W_PQ^MIC(iω_1) with and without k-point
    1217              :          ! extrapolation to compute the extrapolation factor f_PQ for every PQ-matrix element,
    1218              :          ! f_PQ = (W_PQ^MIC(iω_1) with extrapolation) / (W_PQ^MIC(iω_1) without extrapolation)
    1219              : 
    1220              :          ! for ω_1, we compute the k-point extrapolated result using all k-points
    1221          196 :          IF (j_w == 1) THEN
    1222              : 
    1223              :             ! k-point extrapolated
    1224              :             CALL MIC_contribution_from_ikp(bs_env, qs_env, bs_env%fm_W_MIC_freq_1_extra, &
    1225              :                                            cfm_W_ikp_freq_j, ikp, bs_env%kpoints_chi_eps_W, &
    1226           52 :                                            "RI_AUX")
    1227              :             ! non-kpoint extrapolated
    1228           52 :             IF (ikp <= bs_env%nkp_chi_eps_W_orig) THEN
    1229              :                CALL MIC_contribution_from_ikp(bs_env, qs_env, bs_env%fm_W_MIC_freq_1_no_extra, &
    1230              :                                               cfm_W_ikp_freq_j, ikp, bs_env%kpoints_chi_eps_W, &
    1231           16 :                                               "RI_AUX", wkp_ext=bs_env%wkp_orig)
    1232              :             END IF
    1233              : 
    1234              :          END IF
    1235              : 
    1236              :          ! for all ω_j, we need to compute W^MIC without k-point extrpolation
    1237          196 :          IF (ikp <= bs_env%nkp_chi_eps_W_orig) THEN
    1238              :             CALL MIC_contribution_from_ikp(bs_env, qs_env, fm_W_MIC_freq_j, cfm_W_ikp_freq_j, &
    1239              :                                            ikp, bs_env%kpoints_chi_eps_W, "RI_AUX", &
    1240          160 :                                            wkp_ext=bs_env%wkp_orig)
    1241              :          END IF
    1242              :       END SELECT
    1243              : 
    1244         1540 :       CALL cp_cfm_release(cfm_W_ikp_freq_j)
    1245              : 
    1246         1540 :       CALL timestop(handle)
    1247              : 
    1248         1540 :    END SUBROUTINE compute_fm_W_MIC_freq_j
    1249              : 
    1250              : ! **************************************************************************************************
    1251              : !> \brief ...
    1252              : !> \param cfm_mat ...
    1253              : ! **************************************************************************************************
    1254          436 :    SUBROUTINE clean_lower_part(cfm_mat)
    1255              :       TYPE(cp_cfm_type)                                  :: cfm_mat
    1256              : 
    1257              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'clean_lower_part'
    1258              : 
    1259              :       INTEGER                                            :: handle, i_row, j_col, j_global, &
    1260              :                                                             ncol_local, nrow_local
    1261          218 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    1262              : 
    1263          218 :       CALL timeset(routineN, handle)
    1264              : 
    1265              :       CALL cp_cfm_get_info(matrix=cfm_mat, &
    1266              :                            nrow_local=nrow_local, ncol_local=ncol_local, &
    1267          218 :                            row_indices=row_indices, col_indices=col_indices)
    1268              : 
    1269         1790 :       DO j_col = 1, ncol_local
    1270         1572 :          j_global = col_indices(j_col)
    1271         8838 :          DO i_row = 1, nrow_local
    1272         8620 :             IF (j_global < row_indices(i_row)) cfm_mat%local_data(i_row, j_col) = z_zero
    1273              :          END DO
    1274              :       END DO
    1275              : 
    1276          218 :       CALL timestop(handle)
    1277              : 
    1278          218 :    END SUBROUTINE clean_lower_part
    1279              : 
    1280              : ! **************************************************************************************************
    1281              : !> \brief ...
    1282              : !> \param bs_env ...
    1283              : !> \param fm_W_MIC_time ...
    1284              : ! **************************************************************************************************
    1285            4 :    SUBROUTINE apply_extrapol_factor(bs_env, fm_W_MIC_time)
    1286              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1287              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_W_MIC_time
    1288              : 
    1289              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'apply_extrapol_factor'
    1290              : 
    1291              :       INTEGER                                            :: handle, i, i_t, j, ncol_local, nrow_local
    1292              :       REAL(KIND=dp)                                      :: extrapol_factor, W_extra_1, W_no_extra_1
    1293              : 
    1294            2 :       CALL timeset(routineN, handle)
    1295              : 
    1296            2 :       CALL cp_fm_get_info(matrix=fm_W_MIC_time(1), nrow_local=nrow_local, ncol_local=ncol_local)
    1297              : 
    1298           22 :       DO i_t = 1, bs_env%num_time_freq_points
    1299          122 :          DO j = 1, ncol_local
    1300          370 :             DO i = 1, nrow_local
    1301              : 
    1302          250 :                W_extra_1 = bs_env%fm_W_MIC_freq_1_extra%local_data(i, j)
    1303          250 :                W_no_extra_1 = bs_env%fm_W_MIC_freq_1_no_extra%local_data(i, j)
    1304              : 
    1305          250 :                IF (ABS(W_no_extra_1) > 1.0E-13) THEN
    1306          190 :                   extrapol_factor = ABS(W_extra_1/W_no_extra_1)
    1307              :                ELSE
    1308              :                   extrapol_factor = 1.0_dp
    1309              :                END IF
    1310              : 
    1311              :                ! reset extrapolation factor if it is very large
    1312          190 :                IF (extrapol_factor > 10.0_dp) extrapol_factor = 1.0_dp
    1313              : 
    1314              :                fm_W_MIC_time(i_t)%local_data(i, j) = fm_W_MIC_time(i_t)%local_data(i, j) &
    1315          350 :                                                      *extrapol_factor
    1316              :             END DO
    1317              :          END DO
    1318              :       END DO
    1319              : 
    1320            2 :       CALL timestop(handle)
    1321              : 
    1322            2 :    END SUBROUTINE apply_extrapol_factor
    1323              : 
    1324              : ! **************************************************************************************************
    1325              : !> \brief ...
    1326              : !> \param bs_env ...
    1327              : !> \param fm_chi_Gamma_freq ...
    1328              : !> \param j_w ...
    1329              : !> \param mat_chi_Gamma_tau ...
    1330              : ! **************************************************************************************************
    1331         1650 :    SUBROUTINE compute_fm_chi_Gamma_freq(bs_env, fm_chi_Gamma_freq, j_w, mat_chi_Gamma_tau)
    1332              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1333              :       TYPE(cp_fm_type)                                   :: fm_chi_Gamma_freq
    1334              :       INTEGER                                            :: j_w
    1335              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: mat_chi_Gamma_tau
    1336              : 
    1337              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_fm_chi_Gamma_freq'
    1338              : 
    1339              :       INTEGER                                            :: handle, i_t
    1340              :       REAL(KIND=dp)                                      :: freq_j, time_i, weight_ij
    1341              : 
    1342         1650 :       CALL timeset(routineN, handle)
    1343              : 
    1344         1650 :       CALL dbcsr_set(bs_env%mat_RI_RI%matrix, 0.0_dp)
    1345              : 
    1346         1650 :       freq_j = bs_env%imag_freq_points(j_w)
    1347              : 
    1348        17804 :       DO i_t = 1, bs_env%num_time_freq_points
    1349              : 
    1350        16154 :          time_i = bs_env%imag_time_points(i_t)
    1351        16154 :          weight_ij = bs_env%weights_cos_t_to_w(j_w, i_t)
    1352              : 
    1353              :          ! actual Fourier transform
    1354              :          CALL dbcsr_add(bs_env%mat_RI_RI%matrix, mat_chi_Gamma_tau(i_t)%matrix, &
    1355        17804 :                         1.0_dp, COS(time_i*freq_j)*weight_ij)
    1356              : 
    1357              :       END DO
    1358              : 
    1359         1650 :       CALL copy_dbcsr_to_fm(bs_env%mat_RI_RI%matrix, fm_chi_Gamma_freq)
    1360              : 
    1361         1650 :       CALL timestop(handle)
    1362              : 
    1363         1650 :    END SUBROUTINE compute_fm_chi_Gamma_freq
    1364              : 
    1365              : ! **************************************************************************************************
    1366              : !> \brief ...
    1367              : !> \param mat_ikp_re ...
    1368              : !> \param mat_ikp_im ...
    1369              : !> \param mat_Gamma ...
    1370              : !> \param kpoints ...
    1371              : !> \param ikp ...
    1372              : !> \param qs_env ...
    1373              : ! **************************************************************************************************
    1374            0 :    SUBROUTINE mat_ikp_from_mat_Gamma(mat_ikp_re, mat_ikp_im, mat_Gamma, kpoints, ikp, qs_env)
    1375              :       TYPE(dbcsr_type)                                   :: mat_ikp_re, mat_ikp_im, mat_Gamma
    1376              :       TYPE(kpoint_type), POINTER                         :: kpoints
    1377              :       INTEGER                                            :: ikp
    1378              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1379              : 
    1380              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'mat_ikp_from_mat_Gamma'
    1381              : 
    1382              :       INTEGER                                            :: col, handle, i_cell, j_cell, num_cells, &
    1383              :                                                             row
    1384            0 :       INTEGER, DIMENSION(:, :), POINTER                  :: index_to_cell
    1385              :       LOGICAL :: f, i_cell_is_the_minimum_image_cell
    1386              :       REAL(KIND=dp)                                      :: abs_rab_cell_i, abs_rab_cell_j, arg
    1387              :       REAL(KIND=dp), DIMENSION(3)                        :: cell_vector, cell_vector_j, rab_cell_i, &
    1388              :                                                             rab_cell_j
    1389              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: hmat
    1390            0 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: block_im, block_re, data_block
    1391              :       TYPE(cell_type), POINTER                           :: cell
    1392              :       TYPE(dbcsr_iterator_type)                          :: iter
    1393            0 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1394              : 
    1395            0 :       CALL timeset(routineN, handle)
    1396              : 
    1397              :       ! get the same blocks in mat_ikp_re and mat_ikp_im as in mat_Gamma
    1398            0 :       CALL dbcsr_copy(mat_ikp_re, mat_Gamma)
    1399            0 :       CALL dbcsr_copy(mat_ikp_im, mat_Gamma)
    1400            0 :       CALL dbcsr_set(mat_ikp_re, 0.0_dp)
    1401            0 :       CALL dbcsr_set(mat_ikp_im, 0.0_dp)
    1402              : 
    1403            0 :       NULLIFY (cell, particle_set)
    1404            0 :       CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set)
    1405            0 :       CALL get_cell(cell=cell, h=hmat)
    1406              : 
    1407            0 :       index_to_cell => kpoints%index_to_cell
    1408              : 
    1409            0 :       num_cells = SIZE(index_to_cell, 2)
    1410              : 
    1411            0 :       DO i_cell = 1, num_cells
    1412              : 
    1413            0 :          CALL dbcsr_iterator_start(iter, mat_Gamma)
    1414            0 :          DO WHILE (dbcsr_iterator_blocks_left(iter))
    1415            0 :             CALL dbcsr_iterator_next_block(iter, row, col, data_block)
    1416              : 
    1417            0 :             cell_vector(1:3) = MATMUL(hmat, REAL(index_to_cell(1:3, i_cell), dp))
    1418              : 
    1419              :             rab_cell_i(1:3) = pbc(particle_set(row)%r(1:3), cell) - &
    1420            0 :                               (pbc(particle_set(col)%r(1:3), cell) + cell_vector(1:3))
    1421            0 :             abs_rab_cell_i = SQRT(rab_cell_i(1)**2 + rab_cell_i(2)**2 + rab_cell_i(3)**2)
    1422              : 
    1423              :             ! minimum image convention
    1424            0 :             i_cell_is_the_minimum_image_cell = .TRUE.
    1425            0 :             DO j_cell = 1, num_cells
    1426            0 :                cell_vector_j(1:3) = MATMUL(hmat, REAL(index_to_cell(1:3, j_cell), dp))
    1427              :                rab_cell_j(1:3) = pbc(particle_set(row)%r(1:3), cell) - &
    1428            0 :                                  (pbc(particle_set(col)%r(1:3), cell) + cell_vector_j(1:3))
    1429            0 :                abs_rab_cell_j = SQRT(rab_cell_j(1)**2 + rab_cell_j(2)**2 + rab_cell_j(3)**2)
    1430              : 
    1431            0 :                IF (abs_rab_cell_i > abs_rab_cell_j + 1.0E-6_dp) THEN
    1432            0 :                   i_cell_is_the_minimum_image_cell = .FALSE.
    1433              :                END IF
    1434              :             END DO
    1435              : 
    1436            0 :             IF (i_cell_is_the_minimum_image_cell) THEN
    1437            0 :                NULLIFY (block_re, block_im)
    1438            0 :                CALL dbcsr_get_block_p(matrix=mat_ikp_re, row=row, col=col, block=block_re, found=f)
    1439            0 :                CALL dbcsr_get_block_p(matrix=mat_ikp_im, row=row, col=col, block=block_im, found=f)
    1440            0 :                CPASSERT(ALL(ABS(block_re) < 1.0E-10_dp))
    1441            0 :                CPASSERT(ALL(ABS(block_im) < 1.0E-10_dp))
    1442              : 
    1443              :                arg = REAL(index_to_cell(1, i_cell), dp)*kpoints%xkp(1, ikp) + &
    1444              :                      REAL(index_to_cell(2, i_cell), dp)*kpoints%xkp(2, ikp) + &
    1445            0 :                      REAL(index_to_cell(3, i_cell), dp)*kpoints%xkp(3, ikp)
    1446              : 
    1447            0 :                block_re(:, :) = COS(twopi*arg)*data_block(:, :)
    1448            0 :                block_im(:, :) = SIN(twopi*arg)*data_block(:, :)
    1449              :             END IF
    1450              : 
    1451              :          END DO
    1452            0 :          CALL dbcsr_iterator_stop(iter)
    1453              : 
    1454              :       END DO
    1455              : 
    1456            0 :       CALL timestop(handle)
    1457              : 
    1458            0 :    END SUBROUTINE mat_ikp_from_mat_Gamma
    1459              : 
    1460              : ! **************************************************************************************************
    1461              : !> \brief ...
    1462              : !> \param bs_env ...
    1463              : !> \param cfm_chi_ikp_freq_j ...
    1464              : !> \param cfm_V_sqrt_ikp ...
    1465              : !> \param cfm_M_inv_V_sqrt_ikp ...
    1466              : !> \param cfm_W_ikp_freq_j ...
    1467              : ! **************************************************************************************************
    1468         7700 :    SUBROUTINE compute_cfm_W_ikp_freq_j(bs_env, cfm_chi_ikp_freq_j, cfm_V_sqrt_ikp, &
    1469              :                                        cfm_M_inv_V_sqrt_ikp, cfm_W_ikp_freq_j)
    1470              : 
    1471              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1472              :       TYPE(cp_cfm_type)                                  :: cfm_chi_ikp_freq_j, cfm_V_sqrt_ikp, &
    1473              :                                                             cfm_M_inv_V_sqrt_ikp, cfm_W_ikp_freq_j
    1474              : 
    1475              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_cfm_W_ikp_freq_j'
    1476              : 
    1477              :       INTEGER                                            :: handle, info, n_RI
    1478              :       TYPE(cp_cfm_type)                                  :: cfm_eps_ikp_freq_j, cfm_work
    1479              : 
    1480         1540 :       CALL timeset(routineN, handle)
    1481              : 
    1482         1540 :       CALL cp_cfm_create(cfm_work, cfm_chi_ikp_freq_j%matrix_struct)
    1483         1540 :       n_RI = bs_env%n_RI
    1484              : 
    1485              :       ! 1. ε(iω_j,k) = Id - V^0.5(k)*M^-1(k)*χ(iω_j,k)*M^-1(k)*V^0.5(k)
    1486              : 
    1487              :       ! 1. a) work = χ(iω_j,k)*M^-1(k)*V^0.5(k)
    1488              :       CALL parallel_gemm('N', 'N', n_RI, n_RI, n_RI, z_one, &
    1489         1540 :                          cfm_chi_ikp_freq_j, cfm_M_inv_V_sqrt_ikp, z_zero, cfm_work)
    1490         1540 :       CALL cp_cfm_release(cfm_chi_ikp_freq_j)
    1491              : 
    1492              :       ! 1. b) eps_work = V^0.5(k)*M^-1(k)*work
    1493         1540 :       CALL cp_cfm_create(cfm_eps_ikp_freq_j, cfm_work%matrix_struct)
    1494              :       CALL parallel_gemm('C', 'N', n_RI, n_RI, n_RI, z_one, &
    1495         1540 :                          cfm_M_inv_V_sqrt_ikp, cfm_work, z_zero, cfm_eps_ikp_freq_j)
    1496              : 
    1497              :       ! 1. c) ε(iω_j,k) = eps_work - Id
    1498         1540 :       CALL cfm_add_on_diag(cfm_eps_ikp_freq_j, z_one)
    1499              : 
    1500              :       ! 2. W(iω_j,k) = V^0.5(k)*(ε^-1(iω_j,k)-Id)*V^0.5(k)
    1501              : 
    1502              :       ! 2. a) Cholesky decomposition of ε(iω_j,k) as preparation for inversion
    1503         1540 :       CALL cp_cfm_cholesky_decompose(matrix=cfm_eps_ikp_freq_j, n=n_RI, info_out=info)
    1504         1540 :       CPASSERT(info == 0)
    1505              : 
    1506              :       ! 2. b) Inversion of ε(iω_j,k) using its Cholesky decomposition
    1507         1540 :       CALL cp_cfm_cholesky_invert(cfm_eps_ikp_freq_j)
    1508         1540 :       CALL cp_cfm_uplo_to_full(cfm_eps_ikp_freq_j)
    1509              : 
    1510              :       ! 2. c) ε^-1(iω_j,k)-Id
    1511         1540 :       CALL cfm_add_on_diag(cfm_eps_ikp_freq_j, -z_one)
    1512              : 
    1513              :       ! 2. d) work = (ε^-1(iω_j,k)-Id)*V^0.5(k)
    1514              :       CALL parallel_gemm('N', 'N', n_RI, n_RI, n_RI, z_one, cfm_eps_ikp_freq_j, cfm_V_sqrt_ikp, &
    1515         1540 :                          z_zero, cfm_work)
    1516              : 
    1517              :       ! 2. e) W(iw,k) = V^0.5(k)*work
    1518         1540 :       CALL cp_cfm_create(cfm_W_ikp_freq_j, cfm_work%matrix_struct)
    1519              :       CALL parallel_gemm('C', 'N', n_RI, n_RI, n_RI, z_one, cfm_V_sqrt_ikp, cfm_work, &
    1520         1540 :                          z_zero, cfm_W_ikp_freq_j)
    1521              : 
    1522         1540 :       CALL cp_cfm_release(cfm_work)
    1523         1540 :       CALL cp_cfm_release(cfm_eps_ikp_freq_j)
    1524              : 
    1525         1540 :       CALL timestop(handle)
    1526              : 
    1527         1540 :    END SUBROUTINE compute_cfm_W_ikp_freq_j
    1528              : 
    1529              : ! **************************************************************************************************
    1530              : !> \brief ...
    1531              : !> \param cfm ...
    1532              : !> \param alpha ...
    1533              : ! **************************************************************************************************
    1534         6160 :    SUBROUTINE cfm_add_on_diag(cfm, alpha)
    1535              : 
    1536              :       TYPE(cp_cfm_type)                                  :: cfm
    1537              :       COMPLEX(KIND=dp)                                   :: alpha
    1538              : 
    1539              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'cfm_add_on_diag'
    1540              : 
    1541              :       INTEGER                                            :: handle, i_row, j_col, j_global, &
    1542              :                                                             ncol_local, nrow_local
    1543         3080 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    1544              : 
    1545         3080 :       CALL timeset(routineN, handle)
    1546              : 
    1547              :       CALL cp_cfm_get_info(matrix=cfm, &
    1548              :                            nrow_local=nrow_local, &
    1549              :                            ncol_local=ncol_local, &
    1550              :                            row_indices=row_indices, &
    1551         3080 :                            col_indices=col_indices)
    1552              : 
    1553              :       ! add 1 on the diagonal
    1554        29024 :       DO j_col = 1, ncol_local
    1555        25944 :          j_global = col_indices(j_col)
    1556       183660 :          DO i_row = 1, nrow_local
    1557       180580 :             IF (j_global == row_indices(i_row)) THEN
    1558        12972 :                cfm%local_data(i_row, j_col) = cfm%local_data(i_row, j_col) + alpha
    1559              :             END IF
    1560              :          END DO
    1561              :       END DO
    1562              : 
    1563         3080 :       CALL timestop(handle)
    1564              : 
    1565         3080 :    END SUBROUTINE cfm_add_on_diag
    1566              : 
    1567              : ! **************************************************************************************************
    1568              : !> \brief ...
    1569              : !> \param bs_env ...
    1570              : !> \param fm_W_MIC_time ...
    1571              : ! **************************************************************************************************
    1572           34 :    SUBROUTINE create_fm_W_MIC_time(bs_env, fm_W_MIC_time)
    1573              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1574              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_W_MIC_time
    1575              : 
    1576              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'create_fm_W_MIC_time'
    1577              : 
    1578              :       INTEGER                                            :: handle, i_t
    1579              : 
    1580           34 :       CALL timeset(routineN, handle)
    1581              : 
    1582          576 :       ALLOCATE (fm_W_MIC_time(bs_env%num_time_freq_points))
    1583          508 :       DO i_t = 1, bs_env%num_time_freq_points
    1584          508 :          CALL cp_fm_create(fm_W_MIC_time(i_t), bs_env%fm_RI_RI%matrix_struct, set_zero=.TRUE.)
    1585              :       END DO
    1586              : 
    1587           34 :       CALL timestop(handle)
    1588              : 
    1589           34 :    END SUBROUTINE create_fm_W_MIC_time
    1590              : 
    1591              : ! **************************************************************************************************
    1592              : !> \brief ...
    1593              : !> \param bs_env ...
    1594              : !> \param fm_W_MIC_time ...
    1595              : !> \param fm_W_MIC_freq_j ...
    1596              : !> \param j_w ...
    1597              : ! **************************************************************************************************
    1598         1650 :    SUBROUTINE Fourier_transform_w_to_t(bs_env, fm_W_MIC_time, fm_W_MIC_freq_j, j_w)
    1599              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1600              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_W_MIC_time
    1601              :       TYPE(cp_fm_type)                                   :: fm_W_MIC_freq_j
    1602              :       INTEGER                                            :: j_w
    1603              : 
    1604              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'Fourier_transform_w_to_t'
    1605              : 
    1606              :       INTEGER                                            :: handle, i_t
    1607              :       REAL(KIND=dp)                                      :: freq_j, time_i, weight_ij
    1608              : 
    1609         1650 :       CALL timeset(routineN, handle)
    1610              : 
    1611         1650 :       freq_j = bs_env%imag_freq_points(j_w)
    1612              : 
    1613        17804 :       DO i_t = 1, bs_env%num_time_freq_points
    1614              : 
    1615        16154 :          time_i = bs_env%imag_time_points(i_t)
    1616        16154 :          weight_ij = bs_env%weights_cos_w_to_t(i_t, j_w)
    1617              : 
    1618              :          ! actual Fourier transform
    1619              :          CALL cp_fm_scale_and_add(alpha=1.0_dp, matrix_a=fm_W_MIC_time(i_t), &
    1620        17804 :                                   beta=weight_ij*COS(time_i*freq_j), matrix_b=fm_W_MIC_freq_j)
    1621              : 
    1622              :       END DO
    1623              : 
    1624         1650 :       CALL timestop(handle)
    1625              : 
    1626         1650 :    END SUBROUTINE Fourier_transform_w_to_t
    1627              : 
    1628              : ! **************************************************************************************************
    1629              : !> \brief ...
    1630              : !> \param bs_env ...
    1631              : !> \param qs_env ...
    1632              : !> \param fm_W_MIC_time ...
    1633              : ! **************************************************************************************************
    1634           56 :    SUBROUTINE multiply_fm_W_MIC_time_with_Minv_Gamma(bs_env, qs_env, fm_W_MIC_time)
    1635              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1636              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1637              :       TYPE(cp_fm_type), DIMENSION(:)                     :: fm_W_MIC_time
    1638              : 
    1639              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'multiply_fm_W_MIC_time_with_Minv_Gamma'
    1640              : 
    1641              :       INTEGER                                            :: handle, i_t, n_RI, ndep
    1642              :       TYPE(cp_fm_type)                                   :: fm_work
    1643           56 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_Minv_Gamma
    1644              : 
    1645           56 :       CALL timeset(routineN, handle)
    1646              : 
    1647           56 :       n_RI = bs_env%n_RI
    1648              : 
    1649           56 :       CALL cp_fm_create(fm_work, fm_W_MIC_time(1)%matrix_struct)
    1650              : 
    1651              :       ! compute Gamma-only RI-metric matrix M(k=0); no regularization
    1652              :       CALL RI_2c_integral_mat(qs_env, fm_Minv_Gamma, fm_W_MIC_time(1), n_RI, &
    1653           56 :                               bs_env%ri_metric, do_kpoints=.FALSE.)
    1654              : 
    1655           56 :       CALL cp_fm_power(fm_Minv_Gamma(1, 1), fm_work, -1.0_dp, 0.0_dp, ndep)
    1656              : 
    1657              :       ! M^-1(k=0)*W^MIC(iτ)*M^-1(k=0)
    1658          458 :       DO i_t = 1, SIZE(fm_W_MIC_time)
    1659              : 
    1660              :          CALL parallel_gemm('N', 'N', n_RI, n_RI, n_RI, 1.0_dp, fm_Minv_Gamma(1, 1), &
    1661          402 :                             fm_W_MIC_time(i_t), 0.0_dp, fm_work)
    1662              : 
    1663              :          CALL parallel_gemm('N', 'N', n_RI, n_RI, n_RI, 1.0_dp, fm_work, &
    1664          458 :                             fm_Minv_Gamma(1, 1), 0.0_dp, fm_W_MIC_time(i_t))
    1665              : 
    1666              :       END DO
    1667              : 
    1668           56 :       CALL cp_fm_release(fm_work)
    1669           56 :       CALL cp_fm_release(fm_Minv_Gamma)
    1670              : 
    1671           56 :       CALL timestop(handle)
    1672              : 
    1673          112 :    END SUBROUTINE multiply_fm_W_MIC_time_with_Minv_Gamma
    1674              : 
    1675              : ! **************************************************************************************************
    1676              : !> \brief ...
    1677              : !> \param bs_env ...
    1678              : !> \param qs_env ...
    1679              : !> \param fm_Sigma_x_Gamma ...
    1680              : ! **************************************************************************************************
    1681           24 :    SUBROUTINE get_Sigma_x(bs_env, qs_env, fm_Sigma_x_Gamma)
    1682              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1683              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1684              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_Sigma_x_Gamma
    1685              : 
    1686              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_Sigma_x'
    1687              : 
    1688              :       INTEGER                                            :: handle, ispin
    1689              : 
    1690           24 :       CALL timeset(routineN, handle)
    1691              : 
    1692          100 :       ALLOCATE (fm_Sigma_x_Gamma(bs_env%n_spin))
    1693           52 :       DO ispin = 1, bs_env%n_spin
    1694           52 :          CALL cp_fm_create(fm_Sigma_x_Gamma(ispin), bs_env%fm_s_Gamma%matrix_struct)
    1695              :       END DO
    1696              : 
    1697           24 :       IF (bs_env%Sigma_x_exists) THEN
    1698           14 :          DO ispin = 1, bs_env%n_spin
    1699           14 :             CALL fm_read(fm_Sigma_x_Gamma(ispin), bs_env, bs_env%Sigma_x_name, ispin)
    1700              :          END DO
    1701              :       ELSE
    1702           18 :          CALL compute_Sigma_x(bs_env, qs_env, fm_Sigma_x_Gamma)
    1703              :       END IF
    1704              : 
    1705           24 :       CALL timestop(handle)
    1706              : 
    1707           24 :    END SUBROUTINE get_Sigma_x
    1708              : 
    1709              : ! **************************************************************************************************
    1710              : !> \brief ...
    1711              : !> \param bs_env ...
    1712              : !> \param qs_env ...
    1713              : !> \param fm_Sigma_x_Gamma ...
    1714              : ! **************************************************************************************************
    1715           18 :    SUBROUTINE compute_Sigma_x(bs_env, qs_env, fm_Sigma_x_Gamma)
    1716              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1717              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1718              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_Sigma_x_Gamma
    1719              : 
    1720              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'compute_Sigma_x'
    1721              : 
    1722              :       INTEGER                                            :: handle, i_intval_idx, ispin, j_intval_idx
    1723              :       INTEGER, DIMENSION(2)                              :: i_atoms, j_atoms
    1724              :       REAL(KIND=dp)                                      :: t1
    1725           18 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_Vtr_Gamma
    1726              :       TYPE(dbcsr_type)                                   :: mat_Sigma_x_Gamma
    1727          594 :       TYPE(dbt_type)                                     :: t_2c_D, t_2c_Sigma_x, t_2c_V, t_3c_x_V
    1728              : 
    1729           18 :       CALL timeset(routineN, handle)
    1730              : 
    1731           18 :       t1 = m_walltime()
    1732              : 
    1733           18 :       CALL dbt_create(bs_env%t_G, t_2c_D)
    1734           18 :       CALL dbt_create(bs_env%t_W, t_2c_V)
    1735           18 :       CALL dbt_create(bs_env%t_G, t_2c_Sigma_x)
    1736           18 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_V)
    1737           18 :       CALL dbcsr_create(mat_Sigma_x_Gamma, template=bs_env%mat_ao_ao%matrix)
    1738              : 
    1739              :       ! 1. Compute truncated Coulomb operator matrix V^tr(k=0) (cutoff rad: cellsize/2)
    1740              :       CALL RI_2c_integral_mat(qs_env, fm_Vtr_Gamma, bs_env%fm_RI_RI, bs_env%n_RI, &
    1741           18 :                               bs_env%trunc_coulomb, do_kpoints=.FALSE.)
    1742              : 
    1743              :       ! 2. Compute M^-1(k=0) and get M^-1(k=0)*V^tr(k=0)*M^-1(k=0)
    1744           18 :       CALL multiply_fm_W_MIC_time_with_Minv_Gamma(bs_env, qs_env, fm_Vtr_Gamma(:, 1))
    1745              : 
    1746           38 :       DO ispin = 1, bs_env%n_spin
    1747              : 
    1748              :          ! 3. Compute density matrix D_µν
    1749           20 :          CALL G_occ_vir(bs_env, 0.0_dp, bs_env%fm_work_mo(2), ispin, occ=.TRUE., vir=.FALSE.)
    1750              : 
    1751              :          CALL fm_to_local_tensor(bs_env%fm_work_mo(2), bs_env%mat_ao_ao%matrix, &
    1752              :                                  bs_env%mat_ao_ao_tensor%matrix, t_2c_D, bs_env, &
    1753           20 :                                  bs_env%atoms_i_t_group)
    1754              : 
    1755              :          CALL fm_to_local_tensor(fm_Vtr_Gamma(1, 1), bs_env%mat_RI_RI%matrix, &
    1756              :                                  bs_env%mat_RI_RI_tensor%matrix, t_2c_V, bs_env, &
    1757           20 :                                  bs_env%atoms_j_t_group)
    1758              : 
    1759              :          ! every group has its own range of i_atoms and j_atoms; only deal with a
    1760              :          ! limited number of i_atom-j_atom pairs simultaneously in a group to save memory
    1761           40 :          DO i_intval_idx = 1, bs_env%n_intervals_i
    1762           60 :             DO j_intval_idx = 1, bs_env%n_intervals_j
    1763           60 :                i_atoms = bs_env%i_atom_intervals(1:2, i_intval_idx)
    1764           60 :                j_atoms = bs_env%j_atom_intervals(1:2, j_intval_idx)
    1765              : 
    1766              :                ! 4. compute 3-center integrals (µν|P) ("|": truncated Coulomb operator)
    1767              :                ! 5. M_Qνσ(iτ) = sum_P (νσ|P) (M^-1(k=0)*V^tr(k=0)*M^-1(k=0))_QP(iτ)
    1768           20 :                CALL compute_3c_and_contract_W(qs_env, bs_env, i_atoms, j_atoms, t_3c_x_V, t_2c_V)
    1769              : 
    1770              :                ! 6. tensor operations with D and computation of Σ^x
    1771              :                !    Σ^x_λσ(k=0) = sum_νQ M_Qνσ(iτ) sum_µ (Qλ|µ) D_νµ
    1772              :                CALL contract_to_Sigma(t_2c_D, t_3c_x_V, t_2c_Sigma_x, i_atoms, j_atoms, &
    1773           40 :                                       qs_env, bs_env, occ=.TRUE., vir=.FALSE.)
    1774              : 
    1775              :             END DO ! j_atoms
    1776              :          END DO ! i_atoms
    1777              : 
    1778              :          CALL local_dbt_to_global_mat(t_2c_Sigma_x, bs_env%mat_ao_ao_tensor%matrix, &
    1779           20 :                                       mat_Sigma_x_Gamma, bs_env%para_env)
    1780              : 
    1781              :          CALL write_matrix(mat_Sigma_x_Gamma, ispin, bs_env%Sigma_x_name, &
    1782           20 :                            bs_env%fm_work_mo(1), qs_env)
    1783              : 
    1784           38 :          CALL copy_dbcsr_to_fm(mat_Sigma_x_Gamma, fm_Sigma_x_Gamma(ispin))
    1785              : 
    1786              :       END DO ! ispin
    1787              : 
    1788           18 :       IF (bs_env%unit_nr > 0) THEN
    1789              :          WRITE (bs_env%unit_nr, '(T2,A,T55,A,F10.1,A)') &
    1790            9 :             'Computed Σ^x(k=0),', ' Execution time', m_walltime() - t1, ' s'
    1791            9 :          WRITE (bs_env%unit_nr, '(A)') ' '
    1792              :       END IF
    1793              : 
    1794           18 :       CALL dbcsr_release(mat_Sigma_x_Gamma)
    1795           18 :       CALL dbt_destroy(t_2c_D)
    1796           18 :       CALL dbt_destroy(t_2c_V)
    1797           18 :       CALL dbt_destroy(t_2c_Sigma_x)
    1798           18 :       CALL dbt_destroy(t_3c_x_V)
    1799           18 :       CALL cp_fm_release(fm_Vtr_Gamma)
    1800              : 
    1801           18 :       CALL timestop(handle)
    1802              : 
    1803           36 :    END SUBROUTINE compute_Sigma_x
    1804              : 
    1805              : ! **************************************************************************************************
    1806              : !> \brief ...
    1807              : !> \param bs_env ...
    1808              : !> \param qs_env ...
    1809              : !> \param fm_W_MIC_time ...
    1810              : !> \param fm_Sigma_c_Gamma_time ...
    1811              : ! **************************************************************************************************
    1812           24 :    SUBROUTINE get_Sigma_c(bs_env, qs_env, fm_W_MIC_time, fm_Sigma_c_Gamma_time)
    1813              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1814              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1815              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_W_MIC_time
    1816              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :, :)  :: fm_Sigma_c_Gamma_time
    1817              : 
    1818              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_Sigma_c'
    1819              : 
    1820              :       INTEGER                                            :: handle, i_intval_idx, i_t, ispin, &
    1821              :                                                             j_intval_idx, read_write_index
    1822              :       INTEGER, DIMENSION(2)                              :: i_atoms, j_atoms
    1823              :       REAL(KIND=dp)                                      :: t1, tau
    1824           24 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: mat_Sigma_neg_tau, mat_Sigma_pos_tau
    1825          408 :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, &
    1826          216 :                                                             t_2c_Sigma_neg_tau, &
    1827          600 :                                                             t_2c_Sigma_pos_tau, t_2c_W, t_3c_x_W
    1828              : 
    1829           24 :       CALL timeset(routineN, handle)
    1830              : 
    1831              :       CALL create_mat_for_Sigma_c(bs_env, t_2c_Gocc, t_2c_Gvir, t_2c_W, t_2c_Sigma_neg_tau, &
    1832              :                                   t_2c_Sigma_pos_tau, t_3c_x_W, &
    1833           24 :                                   mat_Sigma_neg_tau, mat_Sigma_pos_tau)
    1834              : 
    1835          388 :       DO i_t = 1, bs_env%num_time_freq_points
    1836              : 
    1837          792 :          DO ispin = 1, bs_env%n_spin
    1838              : 
    1839          404 :             t1 = m_walltime()
    1840              : 
    1841          404 :             read_write_index = i_t + (ispin - 1)*bs_env%num_time_freq_points
    1842              : 
    1843              :             ! read self-energy from restart
    1844          404 :             IF (bs_env%Sigma_c_exists(i_t, ispin)) THEN
    1845          120 :                CALL fm_read(bs_env%fm_work_mo(1), bs_env, bs_env%Sigma_p_name, read_write_index)
    1846              :                CALL copy_fm_to_dbcsr(bs_env%fm_work_mo(1), mat_Sigma_pos_tau(i_t, ispin)%matrix, &
    1847          120 :                                      keep_sparsity=.FALSE.)
    1848          120 :                CALL fm_read(bs_env%fm_work_mo(1), bs_env, bs_env%Sigma_n_name, read_write_index)
    1849              :                CALL copy_fm_to_dbcsr(bs_env%fm_work_mo(1), mat_Sigma_neg_tau(i_t, ispin)%matrix, &
    1850          120 :                                      keep_sparsity=.FALSE.)
    1851          120 :                IF (bs_env%unit_nr > 0) THEN
    1852           60 :                   WRITE (bs_env%unit_nr, '(T2,2A,I3,A,I3,A,F10.1,A)') 'Read Σ^c(iτ,k=0) ', &
    1853           60 :                      'from file for time point  ', i_t, ' /', bs_env%num_time_freq_points, &
    1854          120 :                      ', Execution time', m_walltime() - t1, ' s'
    1855              :                END IF
    1856              : 
    1857              :                CYCLE
    1858              : 
    1859              :             END IF
    1860              : 
    1861          284 :             tau = bs_env%imag_time_points(i_t)
    1862              : 
    1863          284 :             CALL G_occ_vir(bs_env, tau, bs_env%fm_Gocc, ispin, occ=.TRUE., vir=.FALSE.)
    1864          284 :             CALL G_occ_vir(bs_env, tau, bs_env%fm_Gvir, ispin, occ=.FALSE., vir=.TRUE.)
    1865              : 
    1866              :             ! fm G^occ, G^vir and W to local tensor
    1867              :             CALL fm_to_local_tensor(bs_env%fm_Gocc, bs_env%mat_ao_ao%matrix, &
    1868              :                                     bs_env%mat_ao_ao_tensor%matrix, t_2c_Gocc, bs_env, &
    1869          284 :                                     bs_env%atoms_i_t_group)
    1870              :             CALL fm_to_local_tensor(bs_env%fm_Gvir, bs_env%mat_ao_ao%matrix, &
    1871              :                                     bs_env%mat_ao_ao_tensor%matrix, t_2c_Gvir, bs_env, &
    1872          284 :                                     bs_env%atoms_i_t_group)
    1873              :             CALL fm_to_local_tensor(fm_W_MIC_time(i_t), bs_env%mat_RI_RI%matrix, &
    1874              :                                     bs_env%mat_RI_RI_tensor%matrix, t_2c_W, bs_env, &
    1875          284 :                                     bs_env%atoms_j_t_group)
    1876              : 
    1877              :             ! every group has its own range of i_atoms and j_atoms; only deal with a
    1878              :             ! limited number of i_atom-j_atom pairs simultaneously in a group to save memory
    1879          568 :             DO i_intval_idx = 1, bs_env%n_intervals_i
    1880          852 :                DO j_intval_idx = 1, bs_env%n_intervals_j
    1881          852 :                   i_atoms = bs_env%i_atom_intervals(1:2, i_intval_idx)
    1882          852 :                   j_atoms = bs_env%j_atom_intervals(1:2, j_intval_idx)
    1883              : 
    1884          284 :                   IF (bs_env%skip_Sigma_occ(i_intval_idx, j_intval_idx) .AND. &
    1885              :                       bs_env%skip_Sigma_vir(i_intval_idx, j_intval_idx)) THEN
    1886              :                      ! Do that only after first timestep to avoid skips due to vanishing G
    1887              :                      ! caused by gaps
    1888           18 :                      IF (i_t == 2) THEN
    1889            0 :                         bs_env%n_skip_sigma = bs_env%n_skip_sigma + 1
    1890              :                      END IF
    1891              :                      CYCLE
    1892              :                   END IF
    1893              : 
    1894              :                   ! 1. compute 3-center integrals (µν|P) ("|": truncated Coulomb operator)
    1895              :                   ! 2. tensor operation M_Qνσ(iτ) = sum_P (νσ|P) W^MIC_QP(iτ)
    1896          266 :                   CALL compute_3c_and_contract_W(qs_env, bs_env, i_atoms, j_atoms, t_3c_x_W, t_2c_W)
    1897              : 
    1898              :                   ! 3. Σ_λσ(iτ,k=0) = sum_νQ M_Qνσ(iτ) sum_µ (Qλ|µ) G^occ_νµ(i|τ|) for τ < 0
    1899              :                   !    (recall M_Qνσ(iτ) = M_Qνσ(-iτ) because W^MIC_PQ(iτ) = W^MIC_PQ(-iτ) )
    1900              :                   CALL contract_to_Sigma(t_2c_Gocc, t_3c_x_W, t_2c_Sigma_neg_tau, i_atoms, j_atoms, &
    1901              :                                          qs_env, bs_env, occ=.TRUE., vir=.FALSE., &
    1902          266 :                                          can_skip=bs_env%skip_Sigma_occ(i_intval_idx, j_intval_idx))
    1903              : 
    1904              :                   !    Σ_λσ(iτ,k=0) = sum_νQ M_Qνσ(iτ) sum_µ (Qλ|µ) G^vir_νµ(i|τ|) for τ > 0
    1905              :                   CALL contract_to_Sigma(t_2c_Gvir, t_3c_x_W, t_2c_Sigma_pos_tau, i_atoms, j_atoms, &
    1906              :                                          qs_env, bs_env, occ=.FALSE., vir=.TRUE., &
    1907          550 :                                          can_skip=bs_env%skip_Sigma_vir(i_intval_idx, j_intval_idx))
    1908              : 
    1909              :                END DO ! j_atoms
    1910              :             END DO ! i_atoms
    1911              : 
    1912              :             ! 4. communicate data tensor t_2c_Sigma (which is local in the subgroup)
    1913              :             !    to the global dbcsr matrix mat_Sigma_pos/neg_tau (which stores Σ for all iτ)
    1914              :             CALL local_dbt_to_global_mat(t_2c_Sigma_neg_tau, bs_env%mat_ao_ao_tensor%matrix, &
    1915          284 :                                          mat_Sigma_neg_tau(i_t, ispin)%matrix, bs_env%para_env)
    1916              :             CALL local_dbt_to_global_mat(t_2c_Sigma_pos_tau, bs_env%mat_ao_ao_tensor%matrix, &
    1917          284 :                                          mat_Sigma_pos_tau(i_t, ispin)%matrix, bs_env%para_env)
    1918              : 
    1919              :             CALL write_matrix(mat_Sigma_pos_tau(i_t, ispin)%matrix, read_write_index, &
    1920          284 :                               bs_env%Sigma_p_name, bs_env%fm_work_mo(1), qs_env)
    1921              :             CALL write_matrix(mat_Sigma_neg_tau(i_t, ispin)%matrix, read_write_index, &
    1922          284 :                               bs_env%Sigma_n_name, bs_env%fm_work_mo(1), qs_env)
    1923              : 
    1924          648 :             IF (bs_env%unit_nr > 0) THEN
    1925              :                WRITE (bs_env%unit_nr, '(T2,A,I10,A,I3,A,F10.1,A)') &
    1926          142 :                   'Computed Σ^c(iτ,k=0) for time point ', i_t, ' /', bs_env%num_time_freq_points, &
    1927          284 :                   ', Execution time', m_walltime() - t1, ' s'
    1928              :             END IF
    1929              : 
    1930              :          END DO ! ispin
    1931              : 
    1932              :       END DO ! i_t
    1933              : 
    1934           24 :       IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, '(A)') ' '
    1935              : 
    1936              :       CALL fill_fm_Sigma_c_Gamma_time(fm_Sigma_c_Gamma_time, bs_env, &
    1937           24 :                                       mat_Sigma_pos_tau, mat_Sigma_neg_tau)
    1938              : 
    1939           24 :       CALL print_skipping(bs_env)
    1940              : 
    1941              :       CALL destroy_mat_Sigma_c(t_2c_Gocc, t_2c_Gvir, t_2c_W, t_2c_Sigma_neg_tau, &
    1942              :                                t_2c_Sigma_pos_tau, t_3c_x_W, fm_W_MIC_time, &
    1943           24 :                                mat_Sigma_neg_tau, mat_Sigma_pos_tau)
    1944              : 
    1945           24 :       CALL delete_unnecessary_files(bs_env)
    1946              : 
    1947           24 :       CALL timestop(handle)
    1948              : 
    1949           48 :    END SUBROUTINE get_Sigma_c
    1950              : 
    1951              : ! **************************************************************************************************
    1952              : !> \brief ...
    1953              : !> \param bs_env ...
    1954              : !> \param t_2c_Gocc ...
    1955              : !> \param t_2c_Gvir ...
    1956              : !> \param t_2c_W ...
    1957              : !> \param t_2c_Sigma_neg_tau ...
    1958              : !> \param t_2c_Sigma_pos_tau ...
    1959              : !> \param t_3c_x_W ...
    1960              : !> \param mat_Sigma_neg_tau ...
    1961              : !> \param mat_Sigma_pos_tau ...
    1962              : ! **************************************************************************************************
    1963           24 :    SUBROUTINE create_mat_for_Sigma_c(bs_env, t_2c_Gocc, t_2c_Gvir, t_2c_W, t_2c_Sigma_neg_tau, &
    1964              :                                      t_2c_Sigma_pos_tau, t_3c_x_W, &
    1965              :                                      mat_Sigma_neg_tau, mat_Sigma_pos_tau)
    1966              : 
    1967              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1968              :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, t_2c_W, &
    1969              :                                                             t_2c_Sigma_neg_tau, &
    1970              :                                                             t_2c_Sigma_pos_tau, t_3c_x_W
    1971              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: mat_Sigma_neg_tau, mat_Sigma_pos_tau
    1972              : 
    1973              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'create_mat_for_Sigma_c'
    1974              : 
    1975              :       INTEGER                                            :: handle, i_t, ispin
    1976              : 
    1977           24 :       CALL timeset(routineN, handle)
    1978              : 
    1979           24 :       CALL dbt_create(bs_env%t_G, t_2c_Gocc)
    1980           24 :       CALL dbt_create(bs_env%t_G, t_2c_Gvir)
    1981           24 :       CALL dbt_create(bs_env%t_W, t_2c_W)
    1982           24 :       CALL dbt_create(bs_env%t_G, t_2c_Sigma_neg_tau)
    1983           24 :       CALL dbt_create(bs_env%t_G, t_2c_Sigma_pos_tau)
    1984           24 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_W)
    1985              : 
    1986           24 :       NULLIFY (mat_Sigma_neg_tau, mat_Sigma_pos_tau)
    1987          528 :       ALLOCATE (mat_Sigma_neg_tau(bs_env%num_time_freq_points, bs_env%n_spin))
    1988          528 :       ALLOCATE (mat_Sigma_pos_tau(bs_env%num_time_freq_points, bs_env%n_spin))
    1989              : 
    1990           52 :       DO ispin = 1, bs_env%n_spin
    1991          456 :          DO i_t = 1, bs_env%num_time_freq_points
    1992          404 :             ALLOCATE (mat_Sigma_neg_tau(i_t, ispin)%matrix)
    1993          404 :             ALLOCATE (mat_Sigma_pos_tau(i_t, ispin)%matrix)
    1994          404 :             CALL dbcsr_create(mat_Sigma_neg_tau(i_t, ispin)%matrix, template=bs_env%mat_ao_ao%matrix)
    1995          432 :             CALL dbcsr_create(mat_Sigma_pos_tau(i_t, ispin)%matrix, template=bs_env%mat_ao_ao%matrix)
    1996              :          END DO
    1997              :       END DO
    1998              : 
    1999           24 :       CALL timestop(handle)
    2000              : 
    2001           24 :    END SUBROUTINE create_mat_for_Sigma_c
    2002              : 
    2003              : ! **************************************************************************************************
    2004              : !> \brief ...
    2005              : !> \param qs_env ...
    2006              : !> \param bs_env ...
    2007              : !> \param i_atoms ...
    2008              : !> \param j_atoms ...
    2009              : !> \param t_3c_x_W ...
    2010              : !> \param t_2c_W ...
    2011              : ! **************************************************************************************************
    2012          286 :    SUBROUTINE compute_3c_and_contract_W(qs_env, bs_env, i_atoms, j_atoms, t_3c_x_W, t_2c_W)
    2013              : 
    2014              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2015              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2016              :       INTEGER, DIMENSION(2)                              :: i_atoms, j_atoms
    2017              :       TYPE(dbt_type)                                     :: t_3c_x_W, t_2c_W
    2018              : 
    2019              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_3c_and_contract_W'
    2020              : 
    2021              :       INTEGER                                            :: handle, RI_intval_idx
    2022              :       INTEGER(KIND=int_8)                                :: flop
    2023              :       INTEGER, DIMENSION(2)                              :: bounds_P, bounds_Q, RI_atoms
    2024              :       INTEGER, DIMENSION(2, 2)                           :: bounds_ao
    2025         4862 :       TYPE(dbt_type)                                     :: t_3c_for_W, t_3c_x_W_tmp
    2026              : 
    2027          286 :       CALL timeset(routineN, handle)
    2028              : 
    2029          286 :       CALL dbt_create(bs_env%t_RI__AO_AO, t_3c_x_W_tmp)
    2030          286 :       CALL dbt_create(bs_env%t_RI__AO_AO, t_3c_for_W)
    2031              : 
    2032              :       ! final layout will be: M_Qνσ(iτ) = sum_P (P|νσ) W^MIC_QP(iτ)
    2033              :       ! Bounds:
    2034              :       ! "AO"
    2035              :       !  ->  ν (AO_1 in compute_3c_integrals)  bounds from i_atoms and sparse in σ and P
    2036              :       !  ->  σ (AO_2 in compute_3c_integrals)  sparse in ν and P
    2037              :       ! Q   bounds from j_atoms
    2038              :       ! P   bounds from inner loop indices and sparse in ν and σ
    2039              : 
    2040              :       bounds_Q(1:2) = [bs_env%i_RI_start_from_atom(j_atoms(1)), &
    2041          858 :                        bs_env%i_RI_end_from_atom(j_atoms(2))]
    2042              : 
    2043          572 :       DO RI_intval_idx = 1, bs_env%n_intervals_inner_loop_atoms
    2044          858 :          RI_atoms = bs_env%inner_loop_atom_intervals(1:2, RI_intval_idx)
    2045              : 
    2046              :          CALL get_bounds_from_atoms(bounds_P, i_atoms, [1, bs_env%n_atom], &
    2047              :                                     bs_env%min_RI_idx_from_AO_AO_atom, &
    2048              :                                     bs_env%max_RI_idx_from_AO_AO_atom, &
    2049              :                                     atoms_3=RI_atoms, &
    2050              :                                     indices_3_start=bs_env%i_RI_start_from_atom, &
    2051          858 :                                     indices_3_end=bs_env%i_RI_end_from_atom)
    2052              : 
    2053              :          ! σ
    2054              :          CALL get_bounds_from_atoms(bounds_ao(:, 2), RI_atoms, i_atoms, &
    2055              :                                     bs_env%min_AO_idx_from_RI_AO_atom, &
    2056          286 :                                     bs_env%max_AO_idx_from_RI_AO_atom)
    2057              :          ! ν
    2058              :          CALL get_bounds_from_atoms(bounds_ao(:, 1), RI_atoms, [1, bs_env%n_atom], &
    2059              :                                     bs_env%min_AO_idx_from_RI_AO_atom, &
    2060              :                                     bs_env%max_AO_idx_from_RI_AO_atom, &
    2061              :                                     atoms_3=i_atoms, &
    2062              :                                     indices_3_start=bs_env%i_ao_start_from_atom, &
    2063          858 :                                     indices_3_end=bs_env%i_ao_end_from_atom)
    2064              : 
    2065          286 :          IF (bounds_P(1) > bounds_P(2) .OR. bounds_ao(1, 2) > bounds_ao(2, 2)) THEN
    2066              :             CYCLE
    2067              :          END IF
    2068              : 
    2069              :          ! 1. compute 3-center integrals (P|µν) ("|": truncated Coulomb operator)
    2070              :          CALL compute_3c_integrals(qs_env, bs_env, t_3c_for_W, &
    2071          286 :                                    atoms_AO_1=i_atoms, atoms_RI=RI_atoms)
    2072              : 
    2073              :          ! 2. tensor operation M_Qνσ(iτ) = sum_P  W^MIC_QP(iτ) (P|νσ)
    2074              :          CALL dbt_contract(alpha=1.0_dp, &
    2075              :                            tensor_1=t_2c_W, &
    2076              :                            tensor_2=t_3c_for_W, &
    2077              :                            beta=1.0_dp, &
    2078              :                            tensor_3=t_3c_x_W_tmp, &
    2079              :                            contract_1=[2], notcontract_1=[1], map_1=[1], &
    2080              :                            contract_2=[1], notcontract_2=[2, 3], map_2=[2, 3], &
    2081              :                            bounds_1=bounds_P, &
    2082              :                            bounds_2=bounds_Q, &
    2083              :                            bounds_3=bounds_ao, &
    2084              :                            flop=flop, &
    2085              :                            move_data=.FALSE., &
    2086              :                            filter_eps=bs_env%eps_filter, &
    2087              :                            unit_nr=bs_env%unit_nr_contract, &
    2088          572 :                            log_verbose=bs_env%print_contract_verbose)
    2089              : 
    2090              :       END DO ! RI_atoms
    2091              : 
    2092              :       ! 3. reorder tensor
    2093          286 :       CALL dbt_copy(t_3c_x_W_tmp, t_3c_x_W, order=[1, 2, 3], move_data=.TRUE.)
    2094              : 
    2095          286 :       CALL dbt_destroy(t_3c_x_W_tmp)
    2096          286 :       CALL dbt_destroy(t_3c_for_W)
    2097              : 
    2098          286 :       CALL timestop(handle)
    2099              : 
    2100          286 :    END SUBROUTINE compute_3c_and_contract_W
    2101              : 
    2102              : ! **************************************************************************************************
    2103              : !> \brief ...
    2104              : !> \param t_2c_G ...
    2105              : !> \param t_3c_x_W ...
    2106              : !> \param t_2c_Sigma ...
    2107              : !> \param i_atoms ...
    2108              : !> \param j_atoms ...
    2109              : !> \param qs_env ...
    2110              : !> \param bs_env ...
    2111              : !> \param occ ...
    2112              : !> \param vir ...
    2113              : !> \param can_skip ...
    2114              : ! **************************************************************************************************
    2115          552 :    SUBROUTINE contract_to_Sigma(t_2c_G, t_3c_x_W, t_2c_Sigma, i_atoms, j_atoms, qs_env, bs_env, &
    2116              :                                 occ, vir, can_skip)
    2117              :       TYPE(dbt_type)                                     :: t_2c_G, t_3c_x_W, t_2c_Sigma
    2118              :       INTEGER, DIMENSION(2)                              :: i_atoms, j_atoms
    2119              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2120              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2121              :       LOGICAL                                            :: occ, vir
    2122              :       LOGICAL, OPTIONAL                                  :: can_skip
    2123              : 
    2124              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'contract_to_Sigma'
    2125              : 
    2126              :       INTEGER :: handle, inner_loop_atoms_interval_index
    2127              :       INTEGER(KIND=int_8)                                :: flop
    2128              :       INTEGER, DIMENSION(2)                              :: bounds_lambda, bounds_mu, bounds_nu, &
    2129              :                                                             bounds_sigma, IL_atoms
    2130              :       INTEGER, DIMENSION(2, 2)                           :: bounds_comb
    2131              :       REAL(KIND=dp)                                      :: sign_Sigma
    2132        13800 :       TYPE(dbt_type)                                     :: t_3c_for_G, t_3c_x_G, t_3c_x_G_2
    2133              : 
    2134          552 :       CALL timeset(routineN, handle)
    2135              : 
    2136          552 :       CPASSERT(occ .EQV. (.NOT. vir))
    2137          552 :       IF (occ) sign_Sigma = -1.0_dp
    2138          552 :       IF (vir) sign_Sigma = 1.0_dp
    2139              : 
    2140          552 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_for_G)
    2141          552 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_G)
    2142          552 :       CALL dbt_create(bs_env%t_RI_AO__AO, t_3c_x_G_2)
    2143              : 
    2144              :       ! Here, in the first step e.g., is computed: N_Qλν = sum_µ (Qλ|µ) G_νµ
    2145              :       ! Afterwards e.g., is computed: Σ_λσ = sum_νQ M_Qνσ N_Qνλ (after reordering)
    2146              :       ! Bounds:
    2147              :       ! "comb" (combined index)
    2148              :       !   ->  Q   bounds from j_atoms and sparse in λ
    2149              :       !   ->  λ (AO_1 in compute_3c_integrals)  sparse in Q and µ
    2150              :       ! µ (AO_2 in compute_3c_integrals)  bounds from inner loop "IL" indices and sparse in Q and λ
    2151              :       ! ν bounds from i_atoms
    2152              :       ! σ sparse in ν
    2153              : 
    2154              :       ! ν
    2155              :       bounds_nu(1:2) = [bs_env%i_ao_start_from_atom(i_atoms(1)), &
    2156         1656 :                         bs_env%i_ao_end_from_atom(i_atoms(2))]
    2157              : 
    2158         1104 :       DO inner_loop_atoms_interval_index = 1, bs_env%n_intervals_inner_loop_atoms
    2159         1656 :          IL_atoms = bs_env%inner_loop_atom_intervals(1:2, inner_loop_atoms_interval_index)
    2160              : 
    2161              :          ! µ
    2162              :          CALL get_bounds_from_atoms(bounds_mu, j_atoms, [1, bs_env%n_atom], &
    2163              :                                     bs_env%min_AO_idx_from_RI_AO_atom, &
    2164              :                                     bs_env%max_AO_idx_from_RI_AO_atom, &
    2165              :                                     atoms_3=IL_atoms, &
    2166              :                                     indices_3_start=bs_env%i_ao_start_from_atom, &
    2167         1656 :                                     indices_3_end=bs_env%i_ao_end_from_atom)
    2168              : 
    2169              :          ! Q
    2170              :          CALL get_bounds_from_atoms(bounds_comb(:, 1), IL_atoms, [1, bs_env%n_atom], &
    2171              :                                     bs_env%min_RI_idx_from_AO_AO_atom, &
    2172              :                                     bs_env%max_RI_idx_from_AO_AO_atom, &
    2173              :                                     atoms_3=j_atoms, &
    2174              :                                     indices_3_start=bs_env%i_RI_start_from_atom, &
    2175         1656 :                                     indices_3_end=bs_env%i_RI_end_from_atom)
    2176              : 
    2177              :          ! λ
    2178              :          CALL get_bounds_from_atoms(bounds_comb(:, 2), j_atoms, IL_atoms, &
    2179              :                                     bs_env%min_AO_idx_from_RI_AO_atom, &
    2180          552 :                                     bs_env%max_AO_idx_from_RI_AO_atom)
    2181              : 
    2182          552 :          IF (bounds_mu(1) > bounds_mu(2) .OR. bounds_comb(1, 1) > bounds_comb(2, 1) .OR. &
    2183              :              bounds_comb(1, 2) > bounds_comb(2, 2)) THEN
    2184              :             CYCLE
    2185              :          END IF
    2186              : 
    2187              :          CALL compute_3c_integrals(qs_env, bs_env, t_3c_for_G, &
    2188          552 :                                    atoms_RI=j_atoms, atoms_AO_2=IL_atoms)
    2189              : 
    2190              :          CALL dbt_contract(alpha=1.0_dp, &
    2191              :                            tensor_1=t_2c_G, &
    2192              :                            tensor_2=t_3c_for_G, &
    2193              :                            beta=1.0_dp, &
    2194              :                            tensor_3=t_3c_x_G, &
    2195              :                            contract_1=[2], notcontract_1=[1], map_1=[3], &
    2196              :                            contract_2=[3], notcontract_2=[1, 2], map_2=[1, 2], &
    2197              :                            bounds_1=bounds_mu, &
    2198              :                            bounds_2=bounds_nu, &
    2199              :                            bounds_3=bounds_comb, &
    2200              :                            flop=flop, &
    2201              :                            move_data=.FALSE., &
    2202              :                            filter_eps=bs_env%eps_filter, &
    2203              :                            unit_nr=bs_env%unit_nr_contract, &
    2204         1104 :                            log_verbose=bs_env%print_contract_verbose)
    2205              :       END DO ! IL_atoms
    2206              : 
    2207              :       ! Reordering: N_Qλν -> N_Qνλ
    2208          552 :       CALL dbt_copy(t_3c_x_G, t_3c_x_G_2, order=[1, 3, 2], move_data=.TRUE.)
    2209              : 
    2210              :       ! Here, the last contraction is done, e.g., Σ_λσ = sum_νQ M_Qνσ N_Qνλ
    2211              :       ! Bounds as above, new "comb" with upper ingredients
    2212              :       bounds_comb(1:2, 1) = [bs_env%i_RI_start_from_atom(j_atoms(1)), &
    2213         1656 :                              bs_env%i_RI_end_from_atom(j_atoms(2))]
    2214         1656 :       bounds_comb(1:2, 2) = bounds_nu(1:2)
    2215              : 
    2216              :       CALL get_bounds_from_atoms(bounds_lambda, j_atoms, [1, bs_env%n_atom], &
    2217              :                                  bs_env%min_AO_idx_from_RI_AO_atom, &
    2218         1656 :                                  bs_env%max_AO_idx_from_RI_AO_atom)
    2219              :       CALL get_bounds_from_atoms(bounds_sigma, [1, bs_env%n_atom], i_atoms, &
    2220              :                                  bs_env%min_AO_idx_from_RI_AO_atom, &
    2221         1656 :                                  bs_env%max_AO_idx_from_RI_AO_atom)
    2222              : 
    2223          552 :       IF (bounds_sigma(1) > bounds_sigma(2) .OR. bounds_lambda(1) > bounds_lambda(2)) THEN
    2224            0 :          flop = 0_int_8
    2225              :       ELSE
    2226              :          CALL dbt_contract(alpha=sign_Sigma, &
    2227              :                            tensor_1=t_3c_x_W, &
    2228              :                            tensor_2=t_3c_x_G_2, &
    2229              :                            beta=1.0_dp, &
    2230              :                            tensor_3=t_2c_Sigma, &
    2231              :                            contract_1=[1, 2], notcontract_1=[3], map_1=[1], &
    2232              :                            contract_2=[1, 2], notcontract_2=[3], map_2=[2], &
    2233              :                            bounds_1=bounds_comb, &
    2234              :                            bounds_2=bounds_sigma, &
    2235              :                            bounds_3=bounds_lambda, &
    2236              :                            filter_eps=bs_env%eps_filter, move_data=.FALSE., flop=flop, &
    2237              :                            unit_nr=bs_env%unit_nr_contract, &
    2238          552 :                            log_verbose=bs_env%print_contract_verbose)
    2239              :       END IF
    2240              : 
    2241          552 :       IF (PRESENT(can_skip)) THEN
    2242          532 :          IF (flop == 0_int_8) can_skip = .TRUE.
    2243              :       END IF
    2244              : 
    2245          552 :       CALL dbt_destroy(t_3c_for_G)
    2246          552 :       CALL dbt_destroy(t_3c_x_G)
    2247          552 :       CALL dbt_destroy(t_3c_x_G_2)
    2248              : 
    2249          552 :       CALL timestop(handle)
    2250              : 
    2251          552 :    END SUBROUTINE contract_to_Sigma
    2252              : 
    2253              : ! **************************************************************************************************
    2254              : !> \brief ...
    2255              : !> \param fm_Sigma_c_Gamma_time ...
    2256              : !> \param bs_env ...
    2257              : !> \param mat_Sigma_pos_tau ...
    2258              : !> \param mat_Sigma_neg_tau ...
    2259              : ! **************************************************************************************************
    2260           34 :    SUBROUTINE fill_fm_Sigma_c_Gamma_time(fm_Sigma_c_Gamma_time, bs_env, &
    2261              :                                          mat_Sigma_pos_tau, mat_Sigma_neg_tau)
    2262              : 
    2263              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :, :)  :: fm_Sigma_c_Gamma_time
    2264              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2265              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: mat_Sigma_pos_tau, mat_Sigma_neg_tau
    2266              : 
    2267              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'fill_fm_Sigma_c_Gamma_time'
    2268              : 
    2269              :       INTEGER                                            :: handle, i_t, ispin, pos_neg
    2270              : 
    2271           34 :       CALL timeset(routineN, handle)
    2272              : 
    2273         1324 :       ALLOCATE (fm_Sigma_c_Gamma_time(bs_env%num_time_freq_points, 2, bs_env%n_spin))
    2274           74 :       DO ispin = 1, bs_env%n_spin
    2275          608 :          DO i_t = 1, bs_env%num_time_freq_points
    2276         1602 :             DO pos_neg = 1, 2
    2277              :                CALL cp_fm_create(fm_Sigma_c_Gamma_time(i_t, pos_neg, ispin), &
    2278         1602 :                                  bs_env%fm_s_Gamma%matrix_struct)
    2279              :             END DO
    2280              :             CALL copy_dbcsr_to_fm(mat_Sigma_pos_tau(i_t, ispin)%matrix, &
    2281          534 :                                   fm_Sigma_c_Gamma_time(i_t, 1, ispin))
    2282              :             CALL copy_dbcsr_to_fm(mat_Sigma_neg_tau(i_t, ispin)%matrix, &
    2283          574 :                                   fm_Sigma_c_Gamma_time(i_t, 2, ispin))
    2284              :          END DO
    2285              :       END DO
    2286              : 
    2287           34 :       CALL timestop(handle)
    2288              : 
    2289           34 :    END SUBROUTINE fill_fm_Sigma_c_Gamma_time
    2290              : 
    2291              : ! **************************************************************************************************
    2292              : !> \brief ...
    2293              : !> \param bs_env ...
    2294              : ! **************************************************************************************************
    2295           24 :    SUBROUTINE print_skipping(bs_env)
    2296              : 
    2297              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2298              : 
    2299              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'print_skipping'
    2300              : 
    2301              :       INTEGER                                            :: handle, n_pairs
    2302              : 
    2303           24 :       CALL timeset(routineN, handle)
    2304              : 
    2305           24 :       n_pairs = bs_env%n_intervals_i*bs_env%n_intervals_j*bs_env%n_spin
    2306              : 
    2307           24 :       CALL bs_env%para_env_tensor%sum(bs_env%n_skip_sigma)
    2308           24 :       CALL bs_env%para_env_tensor%sum(bs_env%n_skip_chi)
    2309           24 :       CALL bs_env%para_env_tensor%sum(n_pairs)
    2310              : 
    2311           24 :       IF (bs_env%unit_nr > 0) THEN
    2312              :          WRITE (bs_env%unit_nr, '(T2,A,T74,F7.1,A)') &
    2313           12 :             'Sparsity of Σ^c(iτ,k=0): Percentage of skipped atom pairs:', &
    2314           24 :             REAL(100*bs_env%n_skip_sigma, KIND=dp)/REAL(n_pairs, KIND=dp), ' %'
    2315              :          WRITE (bs_env%unit_nr, '(T2,A,T74,F7.1,A)') &
    2316           12 :             'Sparsity of χ(iτ,k=0): Percentage of skipped atom pairs:', &
    2317           24 :             REAL(100*bs_env%n_skip_chi, KIND=dp)/REAL(n_pairs, KIND=dp), ' %'
    2318              :       END IF
    2319              : 
    2320           24 :       CALL timestop(handle)
    2321              : 
    2322           24 :    END SUBROUTINE print_skipping
    2323              : 
    2324              : ! **************************************************************************************************
    2325              : !> \brief ...
    2326              : !> \param t_2c_Gocc ...
    2327              : !> \param t_2c_Gvir ...
    2328              : !> \param t_2c_W ...
    2329              : !> \param t_2c_Sigma_neg_tau ...
    2330              : !> \param t_2c_Sigma_pos_tau ...
    2331              : !> \param t_3c_x_W ...
    2332              : !> \param fm_W_MIC_time ...
    2333              : !> \param mat_Sigma_neg_tau ...
    2334              : !> \param mat_Sigma_pos_tau ...
    2335              : ! **************************************************************************************************
    2336           24 :    SUBROUTINE destroy_mat_Sigma_c(t_2c_Gocc, t_2c_Gvir, t_2c_W, t_2c_Sigma_neg_tau, &
    2337              :                                   t_2c_Sigma_pos_tau, t_3c_x_W, fm_W_MIC_time, &
    2338              :                                   mat_Sigma_neg_tau, mat_Sigma_pos_tau)
    2339              : 
    2340              :       TYPE(dbt_type)                                     :: t_2c_Gocc, t_2c_Gvir, t_2c_W, &
    2341              :                                                             t_2c_Sigma_neg_tau, &
    2342              :                                                             t_2c_Sigma_pos_tau, t_3c_x_W
    2343              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_W_MIC_time
    2344              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: mat_Sigma_neg_tau, mat_Sigma_pos_tau
    2345              : 
    2346              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'destroy_mat_Sigma_c'
    2347              : 
    2348              :       INTEGER                                            :: handle
    2349              : 
    2350           24 :       CALL timeset(routineN, handle)
    2351              : 
    2352           24 :       CALL dbt_destroy(t_2c_Gocc)
    2353           24 :       CALL dbt_destroy(t_2c_Gvir)
    2354           24 :       CALL dbt_destroy(t_2c_W)
    2355           24 :       CALL dbt_destroy(t_2c_Sigma_neg_tau)
    2356           24 :       CALL dbt_destroy(t_2c_Sigma_pos_tau)
    2357           24 :       CALL dbt_destroy(t_3c_x_W)
    2358           24 :       CALL cp_fm_release(fm_W_MIC_time)
    2359           24 :       CALL dbcsr_deallocate_matrix_set(mat_Sigma_neg_tau)
    2360           24 :       CALL dbcsr_deallocate_matrix_set(mat_Sigma_pos_tau)
    2361              : 
    2362           24 :       CALL timestop(handle)
    2363              : 
    2364           24 :    END SUBROUTINE destroy_mat_Sigma_c
    2365              : 
    2366              : ! **************************************************************************************************
    2367              : !> \brief ...
    2368              : !> \param bs_env ...
    2369              : ! **************************************************************************************************
    2370           34 :    SUBROUTINE delete_unnecessary_files(bs_env)
    2371              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2372              : 
    2373              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'delete_unnecessary_files'
    2374              : 
    2375              :       CHARACTER(LEN=default_path_length)                 :: f_chi, f_W_t, prefix
    2376              :       INTEGER                                            :: handle, i_t
    2377              : 
    2378           34 :       CALL timeset(routineN, handle)
    2379              : 
    2380           34 :       prefix = bs_env%prefix
    2381              : 
    2382          508 :       DO i_t = 1, bs_env%num_time_freq_points
    2383              : 
    2384          474 :          IF (i_t < 10) THEN
    2385          294 :             WRITE (f_chi, '(3A,I1,A)') TRIM(prefix), bs_env%chi_name, "_00", i_t, ".matrix"
    2386          294 :             WRITE (f_W_t, '(3A,I1,A)') TRIM(prefix), bs_env%W_time_name, "_00", i_t, ".matrix"
    2387          180 :          ELSE IF (i_t < 100) THEN
    2388          180 :             WRITE (f_chi, '(3A,I2,A)') TRIM(prefix), bs_env%chi_name, "_0", i_t, ".matrix"
    2389          180 :             WRITE (f_W_t, '(3A,I2,A)') TRIM(prefix), bs_env%W_time_name, "_0", i_t, ".matrix"
    2390              :          ELSE
    2391            0 :             CPABORT('Please implement more than 99 time/frequency points.')
    2392              :          END IF
    2393              : 
    2394          474 :          CALL safe_delete(f_chi, bs_env)
    2395          508 :          CALL safe_delete(f_W_t, bs_env)
    2396              : 
    2397              :       END DO
    2398              : 
    2399           34 :       CALL timestop(handle)
    2400              : 
    2401           34 :    END SUBROUTINE delete_unnecessary_files
    2402              : 
    2403              : ! **************************************************************************************************
    2404              : !> \brief ...
    2405              : !> \param filename ...
    2406              : !> \param bs_env ...
    2407              : ! **************************************************************************************************
    2408          948 :    SUBROUTINE safe_delete(filename, bs_env)
    2409              :       CHARACTER(LEN=*)                                   :: filename
    2410              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2411              : 
    2412              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'safe_delete'
    2413              : 
    2414              :       INTEGER                                            :: handle
    2415              :       LOGICAL                                            :: file_exists
    2416              : 
    2417          948 :       CALL timeset(routineN, handle)
    2418              : 
    2419          948 :       IF (bs_env%para_env%mepos == 0) THEN
    2420              : 
    2421          474 :          INQUIRE (file=TRIM(filename), exist=file_exists)
    2422          474 :          IF (file_exists) CALL mp_file_delete(TRIM(filename))
    2423              : 
    2424              :       END IF
    2425              : 
    2426          948 :       CALL timestop(handle)
    2427              : 
    2428          948 :    END SUBROUTINE safe_delete
    2429              : 
    2430              : ! **************************************************************************************************
    2431              : !> \brief ...
    2432              : !> \param bs_env ...
    2433              : !> \param qs_env ...
    2434              : !> \param fm_Sigma_x_Gamma ...
    2435              : !> \param fm_Sigma_c_Gamma_time ...
    2436              : ! **************************************************************************************************
    2437           34 :    SUBROUTINE compute_QP_energies(bs_env, qs_env, fm_Sigma_x_Gamma, fm_Sigma_c_Gamma_time)
    2438              : 
    2439              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2440              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2441              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_Sigma_x_Gamma
    2442              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :, :)  :: fm_Sigma_c_Gamma_time
    2443              : 
    2444              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_QP_energies'
    2445              : 
    2446              :       INTEGER                                            :: handle, ikp, ispin, j_t
    2447              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: Sigma_x_ikp_n, V_xc_ikp_n
    2448              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: Sigma_c_ikp_n_freq, Sigma_c_ikp_n_time
    2449              :       TYPE(cp_cfm_type)                                  :: cfm_ks_ikp, cfm_mos_ikp, cfm_s_ikp, &
    2450              :                                                             cfm_Sigma_x_ikp, cfm_work_ikp
    2451              : 
    2452           34 :       CALL timeset(routineN, handle)
    2453              : 
    2454           34 :       CALL cp_cfm_create(cfm_mos_ikp, bs_env%fm_s_Gamma%matrix_struct)
    2455           34 :       CALL cp_cfm_create(cfm_work_ikp, bs_env%fm_s_Gamma%matrix_struct)
    2456              :       ! JW TODO: fully distribute these arrays at given time; also eigenvalues in bs_env
    2457          136 :       ALLOCATE (V_xc_ikp_n(bs_env%n_ao), Sigma_x_ikp_n(bs_env%n_ao))
    2458          170 :       ALLOCATE (Sigma_c_ikp_n_time(bs_env%n_ao, bs_env%num_time_freq_points, 2))
    2459          102 :       ALLOCATE (Sigma_c_ikp_n_freq(bs_env%n_ao, bs_env%num_time_freq_points, 2))
    2460              : 
    2461           74 :       DO ispin = 1, bs_env%n_spin
    2462              : 
    2463          126 :          DO ikp = 1, bs_env%nkp_bs_and_DOS
    2464              : 
    2465              :             ! 1. get H^KS_µν(k_i) from H^KS_µν(k=0)
    2466              :             CALL cfm_ikp_from_fm_Gamma(cfm_ks_ikp, bs_env%fm_ks_Gamma(ispin), &
    2467           52 :                                        ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    2468              : 
    2469              :             ! 2. get S_µν(k_i) from S_µν(k=0)
    2470              :             CALL cfm_ikp_from_fm_Gamma(cfm_s_ikp, bs_env%fm_s_Gamma, &
    2471           52 :                                        ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    2472              : 
    2473              :             ! 3. Diagonalize (Roothaan-Hall): H_KS(k_i)*C(k_i) = S(k_i)*C(k_i)*ϵ(k_i)
    2474              :             CALL cp_cfm_geeig(cfm_ks_ikp, cfm_s_ikp, cfm_mos_ikp, &
    2475           52 :                               bs_env%eigenval_scf(:, ikp, ispin), cfm_work_ikp)
    2476              : 
    2477              :             ! 4. V^xc_µν(k=0) -> V^xc_µν(k_i) -> V^xc_nn(k_i)
    2478              :             CALL to_ikp_and_mo(V_xc_ikp_n, bs_env%fm_V_xc_Gamma(ispin), &
    2479           52 :                                ikp, qs_env, bs_env, cfm_mos_ikp)
    2480              : 
    2481              :             ! 5. Σ^x_µν(k=0) -> Σ^x_µν(k_i) -> Σ^x_nn(k_i)
    2482              :             CALL to_ikp_and_mo(Sigma_x_ikp_n, fm_Sigma_x_Gamma(ispin), &
    2483           52 :                                ikp, qs_env, bs_env, cfm_mos_ikp)
    2484              : 
    2485              :             ! 6. Σ^c_µν(k=0,+/-i|τ_j|) -> Σ^c_µν(k_i,+/-i|τ_j|) -> Σ^c_nn(k_i,+/-i|τ_j|)
    2486          690 :             DO j_t = 1, bs_env%num_time_freq_points
    2487              :                CALL to_ikp_and_mo(Sigma_c_ikp_n_time(:, j_t, 1), &
    2488              :                                   fm_Sigma_c_Gamma_time(j_t, 1, ispin), &
    2489          638 :                                   ikp, qs_env, bs_env, cfm_mos_ikp)
    2490              :                CALL to_ikp_and_mo(Sigma_c_ikp_n_time(:, j_t, 2), &
    2491              :                                   fm_Sigma_c_Gamma_time(j_t, 2, ispin), &
    2492          690 :                                   ikp, qs_env, bs_env, cfm_mos_ikp)
    2493              :             END DO
    2494              : 
    2495              :             ! 7. Σ^c_nn(k_i,iτ) -> Σ^c_nn(k_i,iω)
    2496           52 :             CALL time_to_freq(bs_env, Sigma_c_ikp_n_time, Sigma_c_ikp_n_freq, ispin)
    2497              : 
    2498              :             ! 8. Analytic continuation Σ^c_nn(k_i,iω) -> Σ^c_nn(k_i,ϵ) and
    2499              :             !    ϵ_nk_i^GW = ϵ_nk_i^DFT + Σ^c_nn(k_i,ϵ) + Σ^x_nn(k_i) - v^xc_nn(k_i)
    2500              :             CALL analyt_conti_and_print(bs_env, Sigma_c_ikp_n_freq, Sigma_x_ikp_n, V_xc_ikp_n, &
    2501           92 :                                         bs_env%eigenval_scf(:, ikp, ispin), ikp, ispin)
    2502              : 
    2503              :          END DO ! ikp_DOS
    2504              : 
    2505              :       END DO ! ispin
    2506              : 
    2507           34 :       CALL get_all_VBM_CBM_bandgaps(bs_env)
    2508              : 
    2509           34 :       CALL cp_fm_release(fm_Sigma_x_Gamma)
    2510           34 :       CALL cp_fm_release(fm_Sigma_c_Gamma_time)
    2511           34 :       CALL cp_cfm_release(cfm_ks_ikp)
    2512           34 :       CALL cp_cfm_release(cfm_s_ikp)
    2513           34 :       CALL cp_cfm_release(cfm_mos_ikp)
    2514           34 :       CALL cp_cfm_release(cfm_work_ikp)
    2515           34 :       CALL cp_cfm_release(cfm_Sigma_x_ikp)
    2516              : 
    2517           34 :       CALL timestop(handle)
    2518              : 
    2519           68 :    END SUBROUTINE compute_QP_energies
    2520              : 
    2521              : ! **************************************************************************************************
    2522              : !> \brief ...
    2523              : !> \param array_ikp_n ...
    2524              : !> \param fm_Gamma ...
    2525              : !> \param ikp ...
    2526              : !> \param qs_env ...
    2527              : !> \param bs_env ...
    2528              : !> \param cfm_mos_ikp ...
    2529              : ! **************************************************************************************************
    2530         1380 :    SUBROUTINE to_ikp_and_mo(array_ikp_n, fm_Gamma, ikp, qs_env, bs_env, cfm_mos_ikp)
    2531              : 
    2532              :       REAL(KIND=dp), DIMENSION(:)                        :: array_ikp_n
    2533              :       TYPE(cp_fm_type)                                   :: fm_Gamma
    2534              :       INTEGER                                            :: ikp
    2535              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2536              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2537              :       TYPE(cp_cfm_type)                                  :: cfm_mos_ikp
    2538              : 
    2539              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'to_ikp_and_mo'
    2540              : 
    2541              :       INTEGER                                            :: handle
    2542              :       TYPE(cp_fm_type)                                   :: fm_ikp_mo_re
    2543              : 
    2544         1380 :       CALL timeset(routineN, handle)
    2545              : 
    2546         1380 :       CALL cp_fm_create(fm_ikp_mo_re, fm_Gamma%matrix_struct)
    2547              : 
    2548         1380 :       CALL fm_Gamma_ao_to_cfm_ikp_mo(fm_Gamma, fm_ikp_mo_re, ikp, qs_env, bs_env, cfm_mos_ikp)
    2549              : 
    2550         1380 :       CALL cp_fm_get_diag(fm_ikp_mo_re, array_ikp_n)
    2551              : 
    2552         1380 :       CALL cp_fm_release(fm_ikp_mo_re)
    2553              : 
    2554         1380 :       CALL timestop(handle)
    2555              : 
    2556         1380 :    END SUBROUTINE to_ikp_and_mo
    2557              : 
    2558              : ! **************************************************************************************************
    2559              : !> \brief ...
    2560              : !> \param fm_Gamma ...
    2561              : !> \param fm_ikp_mo_re ...
    2562              : !> \param ikp ...
    2563              : !> \param qs_env ...
    2564              : !> \param bs_env ...
    2565              : !> \param cfm_mos_ikp ...
    2566              : ! **************************************************************************************************
    2567         5520 :    SUBROUTINE fm_Gamma_ao_to_cfm_ikp_mo(fm_Gamma, fm_ikp_mo_re, ikp, qs_env, bs_env, cfm_mos_ikp)
    2568              :       TYPE(cp_fm_type)                                   :: fm_Gamma, fm_ikp_mo_re
    2569              :       INTEGER                                            :: ikp
    2570              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2571              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2572              :       TYPE(cp_cfm_type)                                  :: cfm_mos_ikp
    2573              : 
    2574              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'fm_Gamma_ao_to_cfm_ikp_mo'
    2575              : 
    2576              :       INTEGER                                            :: handle, nmo
    2577              :       TYPE(cp_cfm_type)                                  :: cfm_ikp_ao, cfm_ikp_mo, cfm_tmp
    2578              : 
    2579         1380 :       CALL timeset(routineN, handle)
    2580              : 
    2581         1380 :       CALL cp_cfm_create(cfm_ikp_ao, fm_Gamma%matrix_struct)
    2582         1380 :       CALL cp_cfm_create(cfm_ikp_mo, fm_Gamma%matrix_struct)
    2583         1380 :       CALL cp_cfm_create(cfm_tmp, fm_Gamma%matrix_struct)
    2584              : 
    2585              :       ! get cfm_µν(k_i) from fm_µν(k=0)
    2586         1380 :       CALL cfm_ikp_from_fm_Gamma(cfm_ikp_ao, fm_Gamma, ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    2587              : 
    2588         1380 :       nmo = bs_env%n_ao
    2589         1380 :       CALL parallel_gemm('N', 'N', nmo, nmo, nmo, z_one, cfm_ikp_ao, cfm_mos_ikp, z_zero, cfm_tmp)
    2590         1380 :       CALL parallel_gemm('C', 'N', nmo, nmo, nmo, z_one, cfm_mos_ikp, cfm_tmp, z_zero, cfm_ikp_mo)
    2591              : 
    2592         1380 :       CALL cp_cfm_to_fm(cfm_ikp_mo, fm_ikp_mo_re)
    2593              : 
    2594         1380 :       CALL cp_cfm_release(cfm_ikp_mo)
    2595         1380 :       CALL cp_cfm_release(cfm_ikp_ao)
    2596         1380 :       CALL cp_cfm_release(cfm_tmp)
    2597              : 
    2598         1380 :       CALL timestop(handle)
    2599              : 
    2600         1380 :    END SUBROUTINE fm_Gamma_ao_to_cfm_ikp_mo
    2601              : 
    2602              : ! **************************************************************************************************
    2603              : !> \brief Computes bounds (AO or RI) for given atom intervals atoms_1 and atoms_2 from indices_min
    2604              : !>        and indices_max and returns them in bounds_out.
    2605              : !>        In case, atoms_3 and indices_3 are given, the bounds are computed as the intersection
    2606              : !> \param bounds_out Bounds to be computed
    2607              : !> \param atoms_1 First atom interval
    2608              : !> \param atoms_2 Second atom interval
    2609              : !> \param indices_min Minimum indices for each atom pair (typically from bs_env,
    2610              : !>        computed in get_i_j_atom_ranges in gw_utils.F, e.g. bs_env%min_RI_idx_from_AO_AO_atom)
    2611              : !> \param indices_max Maximum indices for each atom pair (typically from bs_env,
    2612              : !>        computed in get_i_j_atom_ranges in gw_utils.F)
    2613              : !> \param atoms_3 (Optional) Third atom interval for intersection
    2614              : !> \param indices_3_start (Optional) Indices for third atom interval for intersection
    2615              : !> \param indices_3_end (Optional) Indices for third atom interval for intersection
    2616              : ! **************************************************************************************************
    2617         5778 :    SUBROUTINE get_bounds_from_atoms(bounds_out, atoms_1, atoms_2, indices_min, indices_max, &
    2618         5778 :                                     atoms_3, indices_3_start, indices_3_end)
    2619              : 
    2620              :       INTEGER, DIMENSION(2), INTENT(OUT)                 :: bounds_out
    2621              :       INTEGER, DIMENSION(2), INTENT(IN)                  :: atoms_1, atoms_2
    2622              :       INTEGER, DIMENSION(:, :)                           :: indices_min, indices_max
    2623              :       INTEGER, DIMENSION(2), INTENT(IN), OPTIONAL        :: atoms_3
    2624              :       INTEGER, DIMENSION(:), OPTIONAL                    :: indices_3_start, indices_3_end
    2625              : 
    2626              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_bounds_from_atoms'
    2627              : 
    2628              :       INTEGER                                            :: handle, i_at, j_at
    2629              : 
    2630         5778 :       CALL timeset(routineN, handle)
    2631         5778 :       bounds_out(1) = HUGE(0)
    2632         5778 :       bounds_out(2) = -1
    2633              :       !Loop over all atoms in the two intervals and find min/max indices
    2634        17870 :       DO i_at = atoms_1(1), atoms_1(2)
    2635        43662 :          DO j_at = atoms_2(1), atoms_2(2)
    2636        25792 :             bounds_out(1) = MIN(bounds_out(1), indices_min(i_at, j_at))
    2637        37884 :             bounds_out(2) = MAX(bounds_out(2), indices_max(i_at, j_at))
    2638              :          END DO
    2639              :       END DO
    2640              : 
    2641         5778 :       IF (PRESENT(atoms_3) .AND. PRESENT(indices_3_start) .AND. PRESENT(indices_3_end)) THEN
    2642         2756 :          bounds_out(1) = MAX(bounds_out(1), indices_3_start(atoms_3(1)))
    2643         2756 :          bounds_out(2) = MIN(bounds_out(2), indices_3_end(atoms_3(2)))
    2644              :       END IF
    2645              : 
    2646         5778 :       CALL timestop(handle)
    2647              : 
    2648         5778 :    END SUBROUTINE get_bounds_from_atoms
    2649              : 
    2650              : END MODULE gw_large_cell_gamma
        

Generated by: LCOV version 2.0-1