LCOV - code coverage report
Current view: top level - src - gw_ri_rs_non_periodic.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 95.6 % 182 174
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 9 9

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief GW using RI-RS Approximation for molecules
      10              : !> \par History
      11              : !>      04.2026 created [Ritaj Tyagi]
      12              : ! **************************************************************************************************
      13              : 
      14              : MODULE gw_ri_rs_non_periodic
      15              :    USE cp_dbcsr_api,                    ONLY: dbcsr_get_info
      16              :    USE cp_fm_diag,                      ONLY: cp_fm_geeig
      17              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      18              :                                               cp_fm_get_diag,&
      19              :                                               cp_fm_release,&
      20              :                                               cp_fm_to_fm,&
      21              :                                               cp_fm_type
      22              :    USE gw_compute_V_and_W,              ONLY: compute_W
      23              :    USE gw_ri_rs_chi_sigma_kernels,      ONLY: compute_Sigma_c,&
      24              :                                               compute_Sigma_x,&
      25              :                                               get_mat_chi_Gamma_tau
      26              :    USE gw_tensor_large_cell_Gamma,      ONLY: delete_unnecessary_files
      27              :    USE gw_utils,                        ONLY: analyt_conti_and_print,&
      28              :                                               de_init_bs_env,&
      29              :                                               gw_qp_summary_type
      30              :    USE gw_utils_fm,                     ONLY: fm_contract_ABA
      31              :    USE input_constants,                 ONLY: G0W0,&
      32              :                                               evGW0
      33              :    USE kinds,                           ONLY: dp,&
      34              :                                               int_8,&
      35              :                                               max_line_length
      36              :    USE message_passing,                 ONLY: mp_mem_avail_per_rank_GB,&
      37              :                                               mp_print_mem_per_rank
      38              :    USE physcon,                         ONLY: evolt
      39              :    USE post_scf_bandstructure_types,    ONLY: post_scf_bandstructure_type
      40              :    USE post_scf_bandstructure_utils,    ONLY: get_all_VBM_CBM_bandgaps
      41              :    USE qs_environment_types,            ONLY: qs_environment_type
      42              :    USE ri_rs_matrices,                  ONLY: mask_grid_blocks_near_panel,&
      43              :                                               panel_mem_estimate_GB,&
      44              :                                               panel_template_elems,&
      45              :                                               resolve_grid_panels
      46              :    USE ri_rs_setup,                     ONLY: ensure_ri_rs_representation
      47              :    USE time_frequency_grids,            ONLY: negative_time,&
      48              :                                               positive_time,&
      49              :                                               transform_time_to_frequency
      50              : #include "./base/base_uses.f90"
      51              : 
      52              :    IMPLICIT NONE
      53              : 
      54              :    PRIVATE
      55              : 
      56              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_ri_rs_non_periodic'
      57              : 
      58              :    PUBLIC :: gw_calc_ri_rs_non_periodic
      59              : 
      60              : CONTAINS
      61              : 
      62              : ! **************************************************************************************************
      63              : !> \brief GW calculation using RI-RS formalism for molecules
      64              : !> \param qs_env ...
      65              : !> \param bs_env Band-structure environment containing GW parameters.
      66              : ! **************************************************************************************************
      67           42 :    SUBROUTINE gw_calc_ri_rs_non_periodic(qs_env, bs_env)
      68              : 
      69              :       TYPE(qs_environment_type), POINTER                 :: qs_env
      70              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
      71              : 
      72              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'gw_calc_ri_rs_non_periodic'
      73              : 
      74              :       INTEGER                                            :: handle
      75           42 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_Sigma_x_Gamma, fm_W_time
      76              : 
      77           42 :       CALL timeset(routineN, handle)
      78              : 
      79              :       ! Build the common grid, phi and Z representation with the system's boundary conditions.
      80           42 :       CALL ensure_ri_rs_representation(qs_env, bs_env)
      81           42 :       IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, '(A)') ' '
      82           42 :       CALL print_ri_rs_memory_estimate(bs_env)
      83              : 
      84              :       CALL mp_print_mem_per_rank(bs_env%para_env, bs_env%unit_nr, &
      85           42 :                                  label='Memory per MPI process after computing Z_lP:')
      86              : 
      87              :       ! ==============================================================================
      88              :       ! 4. Polarizability matrix χ on the imaginary-time grid
      89              :       !      G^occ_µλ(i|τ|)  = Σ_n^occ C_µn e^(-|(ϵ_n-ϵ_F)τ|) C_λn
      90              :       !      G^vir_µλ(i|τ|)  = Σ_n^vir C_µn e^(-|(ϵ_n-ϵ_F)τ|) C_λn
      91              :       !      G^occ_ll'(i|τ|) = Σ_µν ϕ_µ(r_l) G^occ_µν ϕ_ν(r_l')  (G^vir analogous)
      92              :       !      χ_ll'(iτ)       = G^occ_ll'(i|τ|) ∘ G^vir_ll'(i|τ|) (element-wise)
      93              :       !      χ_PQ(iτ)        = Σ_ll' Z_lP χ_ll'(iτ) Z_l'Q
      94              :       ! ==============================================================================
      95           42 :       CALL get_mat_chi_Gamma_tau(bs_env, bs_env%mat_chi_Gamma_tau, bs_env%ri_rs%cutoff_radius_v_w)
      96              : 
      97              :       ! ==============================================================================
      98              :       ! 5. Screened Coulomb interaction W (RI basis)
      99              :       !      χ_PQ(iτ) -> χ_PQ(iω) -> ε_PQ(iω) -> W_PQ(iω) -> W_PQ(iτ)
     100              :       ! ==============================================================================
     101           42 :       CALL compute_W(bs_env, qs_env, bs_env%mat_chi_Gamma_tau, fm_W_time)
     102              : 
     103              :       ! ==============================================================================
     104              :       ! 6. Exact-exchange self-energy Σ^x
     105              :       !      D_µν          = Σ_n^occ C_µn C_νn                (density matrix)
     106              :       !      D_ll'         = Σ_µν ϕ_µ(r_l) D_µν ϕ_ν(r_l')
     107              :       !      V^tr_ll'      = Σ_PQ Z_lP V^tr_PQ Z_l'Q          (truncated Coulomb)
     108              :       !      Σ^x_ll'       = D_ll' ∘ V^tr_ll'
     109              :       !      Σ^x_λσ(k=0)   = -Σ_ll' ϕ_λ(r_l) Σ^x_ll' ϕ_σ(r_l')
     110              :       ! ==============================================================================
     111           42 :       CALL compute_Sigma_x(bs_env, qs_env, fm_Sigma_x_Gamma, bs_env%ri_rs%cutoff_radius_v_w)
     112              : 
     113              :       ! ==============================================================================
     114              :       ! 7. Correlation self-energy Σ^c and quasiparticle energies, iterated
     115              :       !    until eigenvalue self-consistency if the &EVGW0 section is given and
     116              :       !    done in a single pass for G0W0.
     117              :       !
     118              :       !    (a) W_ll'(iτ)   =  Σ_PQ Z_lP W^MIC_PQ(iτ) Z_l'Q
     119              :       !        Σ^c_ll'(iτ) = -G^occ_ll'(i|τ|) ∘ W_ll'(iτ),  τ < 0
     120              :       !        Σ^c_ll'(iτ) =  G^vir_ll'(i|τ|) ∘ W_ll'(iτ),  τ > 0
     121              :       !        Σ^c_λσ(iτ)  =  Σ_ll' ϕ_λ(r_l) Σ^c_ll'(iτ) ϕ_σ(r_l')
     122              :       !    (b) Σ^c_λσ(iτ) -> Σ^c_nn(ϵ)
     123              :       !        ϵ_n^GW,(i) = ϵ_n^DFT + Σ^c_nn[G^(i-1),W](ϵ_n^GW,(i)) + Σ^x_nn - v^xc_nn
     124              :       ! ==============================================================================
     125           42 :       CALL compute_Sigma_c_and_QP_energies(bs_env, fm_W_time, fm_Sigma_x_Gamma)
     126              : 
     127           42 :       CALL de_init_bs_env(qs_env, bs_env)
     128              : 
     129           42 :       CALL timestop(handle)
     130              : 
     131           42 :    END SUBROUTINE gw_calc_ri_rs_non_periodic
     132              : 
     133              : ! **************************************************************************************************
     134              : !> \brief Correlation self-energy and quasiparticle energies, iterated to eigenvalue
     135              : !>        self-consistency in G (evGW0).
     136              : !>
     137              : !>        For G0W0 this runs once with G^(0) built from the DFT eigenvalues. For evGW0 the
     138              : !>        Green's function is rebuilt from the quasiparticle energies of all states until the
     139              : !>        quasiparticle HOMO, LUMO and HOMO-LUMO gap change by less than EPS_ITER between two
     140              : !>        cycles, or MAX_ITER cycles are spent. W, Σ^x and everything computed before this
     141              : !>        routine stay frozen: they either do not depend on the eigenvalues at all (Σ^x is G
     142              : !>        at τ = 0, a pure density matrix) or are held fixed by construction in GW0. The
     143              : !>        imaginary-time grid is fixed as well, since W(iτ) lives on it.
     144              : !>
     145              : !> \param bs_env ...
     146              : !> \param fm_W_time ...
     147              : !> \param fm_Sigma_x_Gamma ...
     148              : ! **************************************************************************************************
     149           42 :    SUBROUTINE compute_Sigma_c_and_QP_energies(bs_env, fm_W_time, fm_Sigma_x_Gamma)
     150              : 
     151              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     152              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_W_time, fm_Sigma_x_Gamma
     153              : 
     154              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_Sigma_c_and_QP_energies'
     155              : 
     156              :       INTEGER                                            :: handle, i_iter, n_iter
     157              :       LOGICAL                                            :: converged
     158           42 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: eigenval_scf_Gamma_dft
     159              :       REAL(KIND=dp), DIMENSION(2)                        :: e_fermi_dft
     160              :       REAL(KIND=dp), DIMENSION(3, 2)                     :: band_prev
     161           42 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :, :)  :: fm_Sigma_c_Gamma_time
     162              : 
     163           42 :       CALL timeset(routineN, handle)
     164              : 
     165              :       converged = .FALSE.
     166              : 
     167           42 :       CALL init_evGW0(bs_env, n_iter, band_prev, eigenval_scf_Gamma_dft, e_fermi_dft)
     168              : 
     169              :       ! evGW0 self-consistency loop; for G0W0, loop is terminated after one iteration
     170           68 :       DO i_iter = 1, n_iter
     171              : 
     172              :          ! W_ll'(iτ)   =  Σ_PQ Z_lP W^MIC_PQ(iτ) Z_l'Q
     173              :          ! Σ^c_ll'(iτ) = -G^occ_ll'(i|τ|) ∘ W_ll'(iτ),  τ < 0
     174              :          ! Σ^c_ll'(iτ) =  G^vir_ll'(i|τ|) ∘ W_ll'(iτ),  τ > 0
     175              :          ! Σ^c_λσ(iτ)  =  Σ_ll' ϕ_λ(r_l) Σ^c_ll'(iτ) ϕ_σ(r_l')
     176              :          CALL compute_Sigma_c(bs_env, fm_W_time, fm_Sigma_c_Gamma_time, &
     177           68 :                               bs_env%ri_rs%cutoff_radius_v_w, keep_w_and_files=.TRUE.)
     178              : 
     179              :          ! Σ^c_λσ(iτ) -> Σ^c_nn(ϵ)
     180              :          ! ϵ_n^GW,(i) = ϵ_n^DFT + Σ^c_nn[G^(i-1),W](ϵ_n^GW,(i)) + Σ^x_nn - v^xc_nn
     181           68 :          CALL compute_QP_energies(bs_env, fm_Sigma_x_Gamma, fm_Sigma_c_Gamma_time, i_iter)
     182              : 
     183           68 :          IF (bs_env%gw_flavour == G0W0) EXIT
     184              : 
     185           30 :          CALL print_evGW0_band_edges(bs_env, band_prev, i_iter, n_iter, converged)
     186              : 
     187           30 :          IF (i_iter == n_iter .OR. converged) EXIT
     188              : 
     189              :          ! eigenvalues ϵ_n to be updated in G:
     190              :          ! G^occ_µλ(i|τ|) = Σ_n^occ C_µn e^(-|(ϵ_n-ϵ_F)τ|) C_λn
     191              :          ! G^vir_µλ(i|τ|) = Σ_n^vir C_µn e^(-|(ϵ_n-ϵ_F)τ|) C_λn
     192           98 :          CALL update_eigenvalues_G(bs_env)
     193              : 
     194              :       END DO
     195              : 
     196           42 :       CALL cp_fm_release(fm_W_time)
     197              : 
     198           42 :       CALL reset_and_clean_bs_env(bs_env, eigenval_scf_Gamma_dft, e_fermi_dft, fm_Sigma_x_Gamma)
     199              : 
     200           42 :       CALL delete_unnecessary_files(bs_env)
     201              : 
     202           42 :       CALL timestop(handle)
     203              : 
     204           42 :    END SUBROUTINE compute_Sigma_c_and_QP_energies
     205              : 
     206              : ! **************************************************************************************************
     207              : !> \brief Sets up the evGW0 eigenvalue self-consistency loop: the cycle count, the eigenvalues
     208              : !>        the first Green's function is built from, and the DFT reference that the loop
     209              : !>        overwrites. A G0W0 run reduces to a single cycle and needs none of it.
     210              : !> \param bs_env ...
     211              : !> \param n_iter ...
     212              : !> \param band_prev ...
     213              : !> \param eigenval_scf_Gamma_dft ...
     214              : !> \param e_fermi_dft ...
     215              : ! **************************************************************************************************
     216           42 :    SUBROUTINE init_evGW0(bs_env, n_iter, band_prev, eigenval_scf_Gamma_dft, e_fermi_dft)
     217              : 
     218              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     219              :       INTEGER, INTENT(OUT)                               :: n_iter
     220              :       REAL(KIND=dp), DIMENSION(3, 2), INTENT(OUT)        :: band_prev
     221              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
     222              :          INTENT(OUT)                                     :: eigenval_scf_Gamma_dft
     223              :       REAL(KIND=dp), DIMENSION(2), INTENT(OUT)           :: e_fermi_dft
     224              : 
     225              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'init_evGW0'
     226              : 
     227              :       INTEGER                                            :: handle
     228              : 
     229           42 :       CALL timeset(routineN, handle)
     230              : 
     231           42 :       n_iter = bs_env%evgw0_n_iter
     232           42 :       band_prev(:, :) = 0.0_dp
     233           42 :       e_fermi_dft(:) = 0.0_dp
     234              : 
     235           42 :       IF (bs_env%gw_flavour == evGW0) THEN
     236              : 
     237              :          ! eigenvalues currently in G; the first cycle starts is a plain G0W0 step
     238           70 :          bs_env%eigenval_evGW0(:, :, :) = bs_env%eigenval_scf(:, :, :)
     239              :          ! the loop overwrites these; post-GW printing expects the DFT values back
     240           76 :          ALLOCATE (eigenval_scf_Gamma_dft, SOURCE=bs_env%eigenval_scf_Gamma)
     241           12 :          e_fermi_dft(:) = bs_env%e_fermi(:)
     242              :       END IF
     243              : 
     244           42 :       CALL timestop(handle)
     245              : 
     246           42 :    END SUBROUTINE init_evGW0
     247              : 
     248              : ! **************************************************************************************************
     249              : !> \brief Feeds the quasiparticle energies of the current evGW0 cycle back into the Green's
     250              : !>        function used by the next one, and re-centres the Fermi level between the new
     251              : !>        quasiparticle HOMO and LUMO.
     252              : !> \param bs_env ...
     253              : ! **************************************************************************************************
     254           26 :    SUBROUTINE update_eigenvalues_G(bs_env)
     255              : 
     256              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     257              : 
     258              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'update_eigenvalues_G'
     259              : 
     260              :       INTEGER                                            :: handle, i_mo, ispin
     261              : 
     262           26 :       CALL timeset(routineN, handle)
     263              : 
     264              :       ! record this cycle's evGW0 result; it is also what the next Green's function is built from
     265          530 :       bs_env%eigenval_evGW0(:, :, :) = bs_env%eigenval_GW(:, :, :)
     266              : 
     267           70 :       DO ispin = 1, bs_env%n_spin
     268              :          ! Update all physical states; exclude linear-dependency placeholders.
     269          460 :          DO i_mo = 1, bs_env%n_mo_retained
     270          460 :             bs_env%eigenval_scf_Gamma(i_mo, ispin) = bs_env%eigenval_GW(i_mo, 1, ispin)
     271              :          END DO
     272              :          bs_env%e_fermi(ispin) = &
     273              :             0.5_dp*(bs_env%eigenval_GW(bs_env%n_occ(ispin), 1, ispin) + &
     274           70 :                     bs_env%eigenval_GW(bs_env%n_occ(ispin) + 1, 1, ispin))
     275              :       END DO
     276              : 
     277           26 :       CALL timestop(handle)
     278              : 
     279           26 :    END SUBROUTINE update_eigenvalues_G
     280              : 
     281              : ! **************************************************************************************************
     282              : !> \brief Restores the DFT reference that the evGW0 loop overwrote and cleanup
     283              : !> \param bs_env ...
     284              : !> \param eigenval_scf_Gamma_dft ...
     285              : !> \param e_fermi_dft ...
     286              : !> \param fm_Sigma_x_Gamma ..
     287              : ! **************************************************************************************************
     288           42 :    SUBROUTINE reset_and_clean_bs_env(bs_env, eigenval_scf_Gamma_dft, e_fermi_dft, fm_Sigma_x_Gamma)
     289              : 
     290              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     291              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
     292              :          INTENT(INOUT)                                   :: eigenval_scf_Gamma_dft
     293              :       REAL(KIND=dp), DIMENSION(2), INTENT(IN)            :: e_fermi_dft
     294              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_Sigma_x_Gamma
     295              : 
     296              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'reset_and_clean_bs_env'
     297              : 
     298              :       INTEGER                                            :: handle
     299              : 
     300           42 :       CALL timeset(routineN, handle)
     301              : 
     302           42 :       IF (bs_env%gw_flavour == evGW0) THEN
     303              :          ! Update array with final evGW0 result
     304           70 :          bs_env%eigenval_evGW0(:, :, :) = bs_env%eigenval_GW(:, :, :)
     305              :          ! put the DFT reference back for the post-GW DOS/band-edge printing
     306           64 :          bs_env%eigenval_scf_Gamma(:, :) = eigenval_scf_Gamma_dft(:, :)
     307           12 :          bs_env%e_fermi(:) = e_fermi_dft(:)
     308            4 :          DEALLOCATE (eigenval_scf_Gamma_dft)
     309            4 :          CALL cp_fm_release(fm_Sigma_x_Gamma)
     310              :       END IF
     311              : 
     312           42 :       CALL timestop(handle)
     313              : 
     314           42 :    END SUBROUTINE reset_and_clean_bs_env
     315              : 
     316              : ! **************************************************************************************************
     317              : !> \brief Print the quasiparticle HOMO, LUMO and HOMO-LUMO gap of the current evGW0 cycle
     318              : !> \param bs_env ...
     319              : !> \param band_prev ...
     320              : !> \param i_iter ...
     321              : !> \param n_iter ...
     322              : !> \param converged ...
     323              : ! **************************************************************************************************
     324           30 :    SUBROUTINE print_evGW0_band_edges(bs_env, band_prev, i_iter, n_iter, converged)
     325              : 
     326              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     327              :       REAL(KIND=dp), DIMENSION(3, 2), INTENT(INOUT)      :: band_prev
     328              :       INTEGER, INTENT(IN)                                :: i_iter, n_iter
     329              :       LOGICAL, INTENT(OUT)                               :: converged
     330              : 
     331              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'print_evGW0_band_edges'
     332              : 
     333              :       INTEGER                                            :: handle, homo, ispin, u
     334              :       REAL(KIND=dp)                                      :: max_delta
     335              :       REAL(KIND=dp), DIMENSION(3)                        :: band
     336              : 
     337           30 :       CALL timeset(routineN, handle)
     338              : 
     339           30 :       u = bs_env%unit_nr
     340              : 
     341           30 :       converged = (i_iter > 1)
     342           30 :       max_delta = 0.0_dp
     343              : 
     344           30 :       IF (u > 0) THEN
     345           15 :          WRITE (u, '(A)') ' '
     346           15 :          WRITE (u, '(T2,A)') REPEAT('-', 79)
     347           15 :          WRITE (u, '(T2,A,I4,A,I4)') 'evGW0 cycle', i_iter, ' /', n_iter
     348           15 :          WRITE (u, '(T2,A)') REPEAT('-', 79)
     349              :       END IF
     350              : 
     351           80 :       DO ispin = 1, bs_env%n_spin
     352              : 
     353           50 :          homo = bs_env%n_occ(ispin)
     354           50 :          band(1) = bs_env%eigenval_GW(homo, 1, ispin)
     355           50 :          band(2) = bs_env%eigenval_GW(homo + 1, 1, ispin)
     356           50 :          band(3) = band(2) - band(1)
     357              : 
     358           50 :          IF (i_iter > 1) THEN
     359          176 :             max_delta = MAX(max_delta, MAXVAL(ABS(band(:) - band_prev(:, ispin))))
     360              :          END IF
     361              : 
     362           50 :          IF (u > 0) THEN
     363           25 :             IF (bs_env%n_spin == 2) WRITE (u, '(T2,A,I0)') 'Spin ', ispin
     364           25 :             WRITE (u, '(T2,A,T61,F20.3)') 'evGW0 HOMO (eV)', band(1)*evolt
     365           25 :             WRITE (u, '(T2,A,T61,F20.3)') 'evGW0 LUMO (eV)', band(2)*evolt
     366           25 :             WRITE (u, '(T2,A,T61,F20.3)') 'evGW0 HOMO-LUMO gap (eV)', band(3)*evolt
     367              :          END IF
     368              : 
     369          230 :          band_prev(:, ispin) = band(:)
     370              : 
     371              :       END DO
     372              : 
     373           30 :       IF (i_iter > 1) THEN
     374           26 :          converged = (max_delta < bs_env%evgw0_eps_iter)
     375           39 :          IF (u > 0) WRITE (u, '(T2,A,T61,F20.6)') 'Max. change to previous cycle (eV)', &
     376           26 :             max_delta*evolt
     377              :       END IF
     378              : 
     379           17 :       IF (u > 0) WRITE (u, '(T2,A)') REPEAT('-', 79)
     380              : 
     381           30 :       IF (converged) THEN
     382            2 :          IF (u > 0) THEN
     383            1 :             WRITE (u, '(A)') ' '
     384              :             WRITE (u, '(T2,A,I4,A)') &
     385            1 :                'evGW0 eigenvalue self-consistency reached in', i_iter, ' cycles.'
     386            1 :             WRITE (u, '(A)') ' '
     387              :          END IF
     388           28 :       ELSE IF (i_iter == n_iter) THEN
     389              :          CALL cp_warn(__LOCATION__, &
     390              :                       "The evGW0 eigenvalue self-consistency cycle did not converge "// &
     391              :                       "within MAX_ITER cycles. The reported quasiparticle energies are "// &
     392            2 :                       "those of the last cycle.")
     393              :       END IF
     394              : 
     395           30 :       CALL timestop(handle)
     396              : 
     397           30 :    END SUBROUTINE print_evGW0_band_edges
     398              : 
     399              : ! **************************************************************************************************
     400              : !> \brief Estimates and prints per-process memory requirements for the RI-RS GW calculation.
     401              : !> \param bs_env ...
     402              : ! **************************************************************************************************
     403           42 :    SUBROUTINE print_ri_rs_memory_estimate(bs_env)
     404              : 
     405              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     406              : 
     407              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'print_ri_rs_memory_estimate'
     408              : 
     409              :       CHARACTER(LEN=max_line_length)                     :: msg
     410              :       INTEGER                                            :: handle, ipan, n_grid_total, n_procs, &
     411              :                                                             n_RI, pan_rows, pan_width
     412              :       INTEGER(KIND=int_8)                                :: nze_tmpl
     413           42 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: pan_first, pan_last
     414           42 :       INTEGER, DIMENSION(:), POINTER                     :: r_blk_sizes
     415              :       LOGICAL                                            :: use_cutoff
     416           42 :       LOGICAL, ALLOCATABLE, DIMENSION(:)                 :: grid_used
     417              :       REAL(KIND=dp)                                      :: mem_avail_GB, mem_pan_GB, mem_panels_GB, &
     418              :                                                             mem_Z_lP_GB
     419              : 
     420           42 :       CALL timeset(routineN, handle)
     421              : 
     422           42 :       n_grid_total = bs_env%ri_rs%n_grid_points
     423           42 :       CALL dbcsr_get_info(bs_env%ri_rs%mat_phi_mu_l, row_blk_size=r_blk_sizes)
     424          614 :       CPASSERT(SUM(r_blk_sizes) == n_grid_total)
     425           42 :       n_RI = bs_env%n_RI
     426           42 :       n_procs = bs_env%para_env%num_pe
     427              : 
     428              :       ! Z_lP upper bound: dense n_grid × n_RI, distributed evenly across all ranks.
     429              :       ! The actual sparse Z_lP is smaller due to the per-atom locality cutoff.
     430              :       mem_Z_lP_GB = REAL(n_grid_total, dp)*REAL(n_RI, dp)*8.0_dp/ &
     431           42 :                     REAL(n_procs, dp)*1.0E-9_dp
     432              : 
     433              :       ! Peak panel memory during Σ^c: two G panels (A_occ, A_vir) + one W panel plus the
     434              :       ! grid × RI / grid × AO intermediates. With the CUTOFF_RADIUS_RL_W restriction the panel
     435              :       ! matrices only allocate the geo-template blocks, so use the same nze-aware model as the
     436              :       ! panel planner (panel_mem_estimate_GB); without the cutoff, dense panel_rows × n_grid.
     437              :       ! Plus the n_RI × n_RI W_aux matrix. Distributed over n_procs ranks.
     438              :       use_cutoff = bs_env%ri_rs%cutoff_radius_v_w > 0.0_dp .AND. &
     439           42 :                    ALLOCATED(bs_env%ri_rs%chunk_centroids)
     440              :       CALL resolve_grid_panels(bs_env%ri_rs, bs_env%para_env, bs_env%dbcsr_msg_elem_limit, &
     441           42 :                                pan_first, pan_last, bs_env%ri_rs%cutoff_radius_v_w)
     442           42 :       mem_panels_GB = 0.0_dp
     443           84 :       DO ipan = 1, SIZE(pan_first)
     444          614 :          pan_rows = SUM(r_blk_sizes(pan_first(ipan):pan_last(ipan)))
     445           42 :          IF (use_cutoff) THEN
     446              :             CALL mask_grid_blocks_near_panel(bs_env%ri_rs%chunk_centroids, pan_first(ipan), &
     447              :                                              pan_last(ipan), bs_env%ri_rs%cutoff_radius_v_w, &
     448            2 :                                              grid_used)
     449           44 :             pan_width = SUM(r_blk_sizes, MASK=grid_used)
     450              :             CALL panel_template_elems(r_blk_sizes, bs_env%ri_rs%chunk_centroids, &
     451              :                                       grid_used, pan_first(ipan), pan_last(ipan), &
     452            2 :                                       bs_env%ri_rs%cutoff_radius_v_w, nze_tmpl)
     453              :          ELSE
     454           40 :             pan_width = n_grid_total
     455           40 :             nze_tmpl = INT(pan_rows, int_8)*INT(pan_width, int_8)
     456              :          END IF
     457              :          CALL panel_mem_estimate_GB(nze_tmpl, pan_rows, pan_width, n_grid_total, &
     458           42 :                                     n_RI, bs_env%n_ao, n_procs, mem_pan_GB)
     459           84 :          mem_panels_GB = MAX(mem_panels_GB, mem_pan_GB)
     460              :       END DO
     461              :       mem_panels_GB = mem_panels_GB + &
     462           42 :                       REAL(n_RI, dp)*REAL(n_RI, dp)*8.0_dp/REAL(n_procs, dp)*1.0E-9_dp
     463              : 
     464              :       ! Available memory per process = node MemLikelyFree / ranks-per-node, min across ranks
     465              :       ! (0 on non-Linux => warnings suppressed below).
     466           42 :       CALL mp_mem_avail_per_rank_GB(bs_env%para_env, mem_avail_GB)
     467              : 
     468           42 :       IF (bs_env%unit_nr > 0) THEN
     469           21 :          WRITE (bs_env%unit_nr, '(A)') ' '
     470           21 :          WRITE (bs_env%unit_nr, '(T2,A)') 'RI-RS memory estimate per MPI process:'
     471              :          WRITE (bs_env%unit_nr, '(T4,A,F37.2,A)') &
     472           21 :             'Available memory per process (system)', mem_avail_GB, ' GB'
     473              :          WRITE (bs_env%unit_nr, '(T4,A,F18.2,A)') &
     474           21 :             'Required for Z_lP (dense upper bound; actual is sparser)', mem_Z_lP_GB, ' GB'
     475              :          WRITE (bs_env%unit_nr, '(T4,A,F25.2,A)') &
     476           21 :             'Required for χ, W, Σ panels (peak per panel step)', mem_panels_GB, ' GB'
     477           21 :          IF (bs_env%ri_rs%Z_lP_exists .AND. .NOT. bs_env%auto_ri%enabled) THEN
     478            1 :             WRITE (bs_env%unit_nr, '(T4,A)') 'Z_lP fitting skipped (matrix read from restart)'
     479              :          END IF
     480              : 
     481              :       END IF
     482              : 
     483           42 :       IF (mem_avail_GB > 0.0_dp .AND. mem_Z_lP_GB > mem_avail_GB) THEN
     484              :          WRITE (msg, '(A,F0.2,A,F0.2,A)') &
     485            0 :             "The estimated memory for Z_lP, ", mem_Z_lP_GB, " GB per process, exceeds the "// &
     486            0 :             "available ", mem_avail_GB, " GB. Z_lP (n_grid x n_RI) is distributed across all "// &
     487            0 :             "MPI ranks, so add nodes or use fewer MPI ranks per node."
     488            0 :          CPWARN(TRIM(msg))
     489              :       END IF
     490              : 
     491           42 :       IF (mem_avail_GB > 0.0_dp .AND. mem_panels_GB > mem_avail_GB) THEN
     492              :          WRITE (msg, '(A,F0.2,A,F0.2,A)') &
     493            0 :             "The estimated peak memory of the chi/W/Sigma panels, ", mem_panels_GB, &
     494            0 :             " GB per process, exceeds the available ", mem_avail_GB, " GB. Panel memory "// &
     495              :             "scales roughly as 3*panel_size*n_grid/n_procs, so add nodes, use fewer MPI ranks "// &
     496            0 :             "per node."
     497            0 :          CPWARN(TRIM(msg))
     498              :       END IF
     499              : 
     500           42 :       CALL timestop(handle)
     501              : 
     502           84 :    END SUBROUTINE print_ri_rs_memory_estimate
     503              : 
     504              : ! **************************************************************************************************
     505              : !> \brief Σ^c_λσ(iτ) -> Σ^c_nn(ϵ) and the quasi-particle levels of the non-periodic RI-RS path,
     506              : !>        ϵ_n^GW = ϵ_n^DFT + Σ^c_nn(ϵ_n^GW) + Σ^x_nn - v^xc_nn.
     507              : !> \param bs_env ...
     508              : !> \param fm_Sigma_x_Gamma ...
     509              : !> \param fm_Sigma_c_Gamma_time ...
     510              : !> \param i_iter ...
     511              : ! **************************************************************************************************
     512           68 :    SUBROUTINE compute_QP_energies(bs_env, fm_Sigma_x_Gamma, fm_Sigma_c_Gamma_time, i_iter)
     513              : 
     514              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     515              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_Sigma_x_Gamma
     516              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :, :)  :: fm_Sigma_c_Gamma_time
     517              :       INTEGER, INTENT(IN)                                :: i_iter
     518              : 
     519              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_QP_energies'
     520              : 
     521              :       COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :)     :: Sigma_c_n_freq
     522              :       INTEGER                                            :: handle, ispin, j_t
     523              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: Sigma_x_n, V_xc_n
     524              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: Sigma_c_n_time
     525              :       TYPE(cp_fm_type)                                   :: fm_ks, fm_mos, fm_s, fm_work
     526              :       TYPE(gw_qp_summary_type)                           :: qp_summary
     527              : 
     528           68 :       CALL timeset(routineN, handle)
     529              : 
     530           68 :       CALL cp_fm_create(fm_ks, bs_env%fm_s_Gamma%matrix_struct)
     531           68 :       CALL cp_fm_create(fm_s, bs_env%fm_s_Gamma%matrix_struct)
     532           68 :       CALL cp_fm_create(fm_mos, bs_env%fm_s_Gamma%matrix_struct)
     533           68 :       CALL cp_fm_create(fm_work, bs_env%fm_s_Gamma%matrix_struct)
     534              : 
     535          272 :       ALLOCATE (V_xc_n(bs_env%n_ao), Sigma_x_n(bs_env%n_ao))
     536          340 :       ALLOCATE (Sigma_c_n_time(bs_env%n_ao, bs_env%num_time_freq_points, 2))
     537          272 :       ALLOCATE (Sigma_c_n_freq(bs_env%n_ao, bs_env%num_freq_points_fit))
     538              : 
     539          160 :       DO ispin = 1, bs_env%n_spin
     540              : 
     541              :          ! 1. Roothaan-Hall H^KS_µν C_νn = S_µν C_νn ϵ_n
     542           92 :          CALL cp_fm_to_fm(bs_env%fm_ks_Gamma(ispin), fm_ks)
     543           92 :          CALL cp_fm_to_fm(bs_env%fm_s_Gamma, fm_s)
     544           92 :          CALL cp_fm_geeig(fm_ks, fm_s, fm_mos, bs_env%eigenval_scf(:, 1, ispin), fm_work)
     545              : 
     546              :          ! 2. v^xc_µν -> v^xc_nn  and  Σ^x_µν -> Σ^x_nn
     547           92 :          CALL to_Gamma_and_mo_real(V_xc_n, bs_env%fm_V_xc_Gamma(ispin), fm_mos)
     548           92 :          CALL to_Gamma_and_mo_real(Sigma_x_n, fm_Sigma_x_Gamma(ispin), fm_mos)
     549              : 
     550              :          ! 3. Σ^c_µν(+/-i|τ_j|) -> Σ^c_nn(+/-i|τ_j|)
     551         1192 :          DO j_t = 1, bs_env%num_time_freq_points
     552              :             CALL to_Gamma_and_mo_real(Sigma_c_n_time(:, j_t, positive_time), &
     553         1100 :                                       fm_Sigma_c_Gamma_time(j_t, positive_time, ispin), fm_mos)
     554              :             CALL to_Gamma_and_mo_real(Sigma_c_n_time(:, j_t, negative_time), &
     555         1192 :                                       fm_Sigma_c_Gamma_time(j_t, negative_time, ispin), fm_mos)
     556              :          END DO
     557              : 
     558              :          ! 4. Σ^c_nn(iτ) -> Σ^c_nn(iω)
     559           92 :          CALL transform_time_to_frequency(bs_env%time_frequency_grid_sigma, Sigma_c_n_time, Sigma_c_n_freq)
     560         6542 :          Sigma_c_n_freq(1:bs_env%n_occ(ispin), :) = CONJG(Sigma_c_n_freq(1:bs_env%n_occ(ispin), :))
     561              : 
     562              :          ! 5. Analytic continuation Σ^c_nn(iω) -> Σ^c_nn(ϵ) and the QP levels
     563              :          CALL analyt_conti_and_print(bs_env, Sigma_c_n_freq, Sigma_x_n, V_xc_n, &
     564          160 :                                      bs_env%eigenval_scf(:, 1, ispin), 1, ispin, qp_summary, i_iter)
     565              : 
     566              :       END DO ! ispin
     567              : 
     568           68 :       CALL get_all_VBM_CBM_bandgaps(bs_env)
     569              : 
     570           68 :       IF (bs_env%gw_flavour == G0W0) CALL cp_fm_release(fm_Sigma_x_Gamma)
     571           68 :       CALL cp_fm_release(fm_Sigma_c_Gamma_time)
     572              : 
     573           68 :       CALL cp_fm_release(fm_ks)
     574           68 :       CALL cp_fm_release(fm_s)
     575           68 :       CALL cp_fm_release(fm_mos)
     576           68 :       CALL cp_fm_release(fm_work)
     577              : 
     578           68 :       CALL timestop(handle)
     579              : 
     580          136 :    END SUBROUTINE compute_QP_energies
     581              : 
     582              : ! **************************************************************************************************
     583              : !> \brief AO -> MO transform of a Γ-point matrix
     584              : !> \param array_n ...
     585              : !> \param fm_Gamma ...
     586              : !> \param fm_mos ...
     587              : ! **************************************************************************************************
     588         2384 :    SUBROUTINE to_Gamma_and_mo_real(array_n, fm_Gamma, fm_mos)
     589              : 
     590              :       REAL(KIND=dp), DIMENSION(:)                        :: array_n
     591              :       TYPE(cp_fm_type)                                   :: fm_Gamma, fm_mos
     592              : 
     593              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'to_Gamma_and_mo_real'
     594              : 
     595              :       INTEGER                                            :: handle
     596              :       TYPE(cp_fm_type)                                   :: fm_mo
     597              : 
     598         2384 :       CALL timeset(routineN, handle)
     599              : 
     600         2384 :       CALL cp_fm_create(fm_mo, fm_Gamma%matrix_struct)
     601              : 
     602              :       ! A_nn' = Σ_μν C_μn A_μν C_νn'
     603         2384 :       CALL fm_contract_ABA(fm_mos, fm_Gamma, fm_mo)
     604              : 
     605         2384 :       CALL cp_fm_get_diag(fm_mo, array_n)
     606              : 
     607         2384 :       CALL cp_fm_release(fm_mo)
     608              : 
     609         2384 :       CALL timestop(handle)
     610              : 
     611         2384 :    END SUBROUTINE to_Gamma_and_mo_real
     612              : 
     613              : END MODULE gw_ri_rs_non_periodic
        

Generated by: LCOV version 2.0-1