LCOV - code coverage report
Current view: top level - src - gw_large_cell_gamma.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:936074a) Lines: 92.7 % 738 684
Test Date: 2025-12-04 06:27:48 Functions: 97.5 % 40 39

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

Generated by: LCOV version 2.0-1