LCOV - code coverage report
Current view: top level - src/emd - rt_bse_ri_rs.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:dbd702d) Lines: 97.1 % 242 235
Test Date: 2026-11-22 01:17:58 Functions: 100.0 % 10 10

            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 RT-BSE RI-RS kernels: SEX and Hartree evaluated by collocation on grid points r_l.
      10              : !>        Once-built grid objects:
      11              : !>          φ_µ(r_l)  [grid×AO],   Z_lP  [grid×RI],
      12              : !>          V^aux_PQ = [M^-1 V^tr M^-1]_PQ  [RI×RI],  W^0_ll' = sum_PQ Z_lP (V + W^c(ω=0))_PQ Z_l'Q.
      13              : !>        Per-call kernels, collocation X = φ_µ(r_l) (AO domain, written φ_lµ below):
      14              : !>          SEX:      ρ^grid_ll' = sum_µν φ_lµ Δρ_µν φ_l'ν ;
      15              : !>                    Σ_µν = pref * sum_ll' φ_lµ [ρ^grid ∘ W^0]_ll' φ_l'ν
      16              : !>          Hartree:  n_l = sum_µν φ_lµ Δρ_µν φ_lν ;
      17              : !>                    v_l = sum_PQl' Z_lP V^aux_PQ Z_l'Q n_l'   (applied factorized, stage 2) ;
      18              : !>                    V^H_µν = sum_l φ_lµ v_l φ_lν      (diagonal-only, no grid×grid)
      19              : !>        Independent of which GW variant produced bs_env%fm_W_MIC_freq_zero.
      20              : !> \author Maximilian Graml (05.26)
      21              : ! **************************************************************************************************
      22              : MODULE rt_bse_ri_rs
      23              :    USE cp_cfm_basic_linalg,             ONLY: cp_cfm_scale_and_add
      24              :    USE cp_cfm_types,                    ONLY: cp_cfm_create,&
      25              :                                               cp_cfm_release,&
      26              :                                               cp_cfm_to_fm,&
      27              :                                               cp_cfm_type,&
      28              :                                               cp_fm_to_cfm
      29              :    USE cp_dbcsr_api,                    ONLY: &
      30              :         dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_distribution_type, dbcsr_filter, &
      31              :         dbcsr_get_info, dbcsr_get_occupation, dbcsr_iterator_blocks_left, &
      32              :         dbcsr_iterator_next_block, dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, &
      33              :         dbcsr_multiply, dbcsr_release, dbcsr_scale, dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry
      34              :    USE cp_dbcsr_contrib,                ONLY: dbcsr_frobenius_norm,&
      35              :                                               dbcsr_get_diag
      36              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      37              :                                               copy_fm_to_dbcsr
      38              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      39              :                                               cp_fm_release,&
      40              :                                               cp_fm_type
      41              :    USE gw_utils_dbcsr,                  ONLY: dbcsr_contract_ABA,&
      42              :                                               hadamard_product_inplace
      43              :    USE gw_utils_fm,                     ONLY: fm_contract_ABA
      44              :    USE kinds,                           ONLY: dp
      45              :    USE machine,                         ONLY: m_walltime
      46              :    USE message_passing,                 ONLY: mp_para_env_type
      47              :    USE mp2_ri_2c,                       ONLY: RI_2c_integral_mat
      48              :    USE physcon,                         ONLY: angstrom
      49              :    USE post_scf_bandstructure_types,    ONLY: post_scf_bandstructure_type
      50              :    USE qs_environment_types,            ONLY: qs_environment_type
      51              :    USE ri_rs_matrices,                  ONLY: release_dbcsr_topology_and_matrices,&
      52              :                                               reserve_blocks_within_radius,&
      53              :                                               setup_square_topology
      54              :    USE ri_rs_setup,                     ONLY: ensure_ri_rs_representation
      55              : #include "../base/base_uses.f90"
      56              : 
      57              :    IMPLICIT NONE
      58              : 
      59              :    PRIVATE
      60              : 
      61              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rt_bse_ri_rs'
      62              : 
      63              :    PUBLIC :: rt_bse_ri_rs_ensure_V_grid, &
      64              :              rt_bse_ri_rs_ensure_W0_grid, &
      65              :              compute_sigma_ri_rs, &
      66              :              compute_sigma_ri_rs_complex, &
      67              :              compute_hartree_ri_rs, &
      68              :              compute_hartree_ri_rs_complex, &
      69              :              compute_hartree_ri_rs_from_diag, &
      70              :              hartree_potential_from_diag_ri_rs
      71              : 
      72              : CONTAINS
      73              : 
      74              : ! **************************************************************************************************
      75              : !> \brief Build V^aux_PQ = [M^-1 V^tr M^-1]_PQ (truncated Coulomb in the RI basis, M^-1-sandwiched
      76              : !>        to match the W^MIC convention). The grid Coulomb V_ll' = sum_PQ Z_lP V^aux_PQ Z_l'Q is
      77              : !>        never materialized -- the Hartree stage applies it factorized:
      78              : !>          v_l = sum_PQl' Z_lP V^aux_PQ Z_l'Q n_l'
      79              : !>        Needed by RT-BSE RI-RS Hartree.
      80              : !> \param bs_env ...
      81              : !> \param qs_env ...
      82              : ! **************************************************************************************************
      83           22 :    SUBROUTINE rt_bse_ri_rs_ensure_V_grid(bs_env, qs_env)
      84              : 
      85              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
      86              :       TYPE(qs_environment_type), POINTER                 :: qs_env
      87              : 
      88              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'rt_bse_ri_rs_ensure_V_grid'
      89              : 
      90              :       INTEGER                                            :: handle
      91           22 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: blk_aux, dist_row_aux
      92              :       REAL(KIND=dp)                                      :: t1
      93           22 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_Vtr_Gamma
      94              :       TYPE(dbcsr_distribution_type)                      :: dist_aux_aux
      95              : 
      96           22 :       CALL timeset(routineN, handle)
      97              : 
      98           22 :       IF (bs_env%ri_rs%V_grid_built) THEN
      99            0 :          CALL timestop(handle)
     100            0 :          RETURN
     101              :       END IF
     102              : 
     103           22 :       CALL ensure_ri_rs_representation(qs_env, bs_env)
     104           22 :       t1 = m_walltime()
     105              : 
     106           22 :       CALL setup_square_topology(bs_env%ri_rs%mat_Z_lP, 'COL', dist_aux_aux, blk_aux, dist_row_aux)
     107              : 
     108              :       CALL RI_2c_integral_mat(qs_env, fm_Vtr_Gamma, bs_env%fm_RI_RI%matrix_struct, bs_env%n_RI, &
     109           22 :                               bs_env%trunc_coulomb)
     110              :       ! Apply M^-1 sandwich to match the W^MIC convention; same scale as W^c when both used.
     111              :       ! M^-1(k=0) V^tr M^-1(k=0) -> fm_Vtr_Gamma
     112           22 :       CALL fm_contract_ABA(bs_env%fm_Minv_Gamma, fm_Vtr_Gamma(:, 1))
     113              : 
     114              :       ! Store the M^-1-sandwiched RI-basis Coulomb; the grid kernel Z V Z^T is applied factorized.
     115              :       CALL dbcsr_create(bs_env%ri_rs%mat_V_aux_rtbse, "V_aux_rtbse", dist_aux_aux, &
     116           22 :                         dbcsr_type_no_symmetry, blk_aux, blk_aux)
     117              :       CALL copy_fm_to_dbcsr(fm_Vtr_Gamma(1, 1), bs_env%ri_rs%mat_V_aux_rtbse, &
     118           22 :                             keep_sparsity=.FALSE.)
     119              : 
     120           22 :       bs_env%ri_rs%V_grid_built = .TRUE.
     121              : 
     122           22 :       IF (bs_env%unit_nr > 0) THEN
     123              :          WRITE (bs_env%unit_nr, '(T2,A,T57,A,F7.1,A)') &
     124           11 :             'Precomputed RT-BSE RI-RS V_aux kernel,', ' Execution time', &
     125           22 :             m_walltime() - t1, ' s'
     126           11 :          WRITE (bs_env%unit_nr, '(A)') ' '
     127              :       END IF
     128              : 
     129           22 :       CALL release_dbcsr_topology_and_matrices(dist=dist_aux_aux, mapped_dist=dist_row_aux)
     130           22 :       CALL cp_fm_release(fm_Vtr_Gamma)
     131              : 
     132           22 :       CALL timestop(handle)
     133              : 
     134           44 :    END SUBROUTINE rt_bse_ri_rs_ensure_V_grid
     135              : 
     136              : ! **************************************************************************************************
     137              : !> \brief Build W^0_ll' = sum_PQ Z_lP (V + W^c(ω=0))_PQ Z_l'Q (statically screened W on the grid).
     138              : !>        Needed by RT-BSE RI-RS SEX/COH. Reuses bs_env%fm_W_MIC_freq_zero which must already
     139              : !>        contain M^-1 W^c(ω=0) M^-1 (built by either GW path under the BSE rtp_method gate).
     140              : !>        W^0 enters only through the Hadamard ρ^grid ∘ W^0 -- its elements are needed, so it is
     141              : !>        the one persistent grid×grid object of the kernel layer.
     142              : !>        RTBSE%CUTOFF_RADIUS_W0 > 0 keeps only block pairs whose grid centroids lie within that
     143              : !>        radius; the per-step ρ^grid inherits the pattern, so both grid×grid objects shrink.
     144              : !> \param bs_env ...
     145              : !> \param qs_env ...
     146              : ! **************************************************************************************************
     147           22 :    SUBROUTINE rt_bse_ri_rs_ensure_W0_grid(bs_env, qs_env)
     148              : 
     149              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     150              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     151              : 
     152              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'rt_bse_ri_rs_ensure_W0_grid'
     153              : 
     154              :       INTEGER                                            :: handle
     155           22 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: blk_aux, blk_grid, dist_col_grid, &
     156           22 :                                                             dist_row_aux
     157              :       LOGICAL                                            :: use_cutoff_w0
     158              :       REAL(KIND=dp)                                      :: fnorm_w0, occ_w0, t1
     159           22 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_Vtr_Gamma
     160              :       TYPE(dbcsr_distribution_type)                      :: dist_aux_aux, dist_grid_grid
     161              :       TYPE(dbcsr_type)                                   :: matrix_V_aux, matrix_W_aux
     162              : 
     163           22 :       CALL timeset(routineN, handle)
     164              : 
     165           22 :       IF (bs_env%ri_rs%W0_grid_built) THEN
     166            0 :          CALL timestop(handle)
     167            0 :          RETURN
     168              :       END IF
     169              : 
     170              :       ! W(w=0) is built by the GW step only under its RTBSE rtp_method gate; reaching this consumer
     171              :       ! without it means gate and consumer disagree. Abort rather than read a never-created cp_fm.
     172           22 :       IF (.NOT. ASSOCIATED(bs_env%fm_W_MIC_freq_zero%matrix_struct)) THEN
     173              :          CALL cp_abort(__LOCATION__, &
     174              :                        "RT-BSE RI-RS kernel needs the screened interaction W(w=0), which the GW "// &
     175              :                        "step did not build. Select the RT-BSE propagator with '&RTBSE' or "// &
     176            0 :                        "'&RTBSE RTBSE', not '&RTBSE TDDFT'.")
     177              :       END IF
     178              : 
     179           22 :       CALL ensure_ri_rs_representation(qs_env, bs_env)
     180           22 :       t1 = m_walltime()
     181              : 
     182              :       CALL setup_square_topology(bs_env%ri_rs%mat_phi_mu_l, 'ROW', dist_grid_grid, blk_grid, &
     183           22 :                                  dist_col_grid)
     184           22 :       CALL setup_square_topology(bs_env%ri_rs%mat_Z_lP, 'COL', dist_aux_aux, blk_aux, dist_row_aux)
     185              : 
     186              :       CALL RI_2c_integral_mat(qs_env, fm_Vtr_Gamma, bs_env%fm_RI_RI%matrix_struct, bs_env%n_RI, &
     187           22 :                               bs_env%trunc_coulomb)
     188              :       ! M^-1(k=0) V^tr M^-1(k=0) -> fm_Vtr_Gamma
     189           22 :       CALL fm_contract_ABA(bs_env%fm_Minv_Gamma, fm_Vtr_Gamma(:, 1))
     190              : 
     191              :       CALL dbcsr_create(matrix_V_aux, "V_aux_rtbse_W0", dist_aux_aux, dbcsr_type_no_symmetry, &
     192           22 :                         blk_aux, blk_aux)
     193           22 :       CALL copy_fm_to_dbcsr(fm_Vtr_Gamma(1, 1), matrix_V_aux, keep_sparsity=.FALSE.)
     194              : 
     195              :       CALL dbcsr_create(matrix_W_aux, "W_aux_rtbse", dist_aux_aux, dbcsr_type_no_symmetry, &
     196           22 :                         blk_aux, blk_aux)
     197           22 :       CALL copy_fm_to_dbcsr(bs_env%fm_W_MIC_freq_zero, matrix_W_aux, keep_sparsity=.FALSE.)
     198           22 :       CALL dbcsr_add(matrix_W_aux, matrix_V_aux, 1.0_dp, 1.0_dp)
     199              : 
     200              :       CALL dbcsr_create(bs_env%ri_rs%mat_W0_grid_rtbse, "W0_grid_rtbse", dist_grid_grid, &
     201           22 :                         dbcsr_type_no_symmetry, blk_grid, blk_grid)
     202              : 
     203              :       ! CUTOFF_RADIUS_W0: pre-seed only the in-radius block pairs, then let retain_sparsity confine
     204              :       ! the contraction to them. Gate on the value, never on ALLOCATED alone -- .AND. does not
     205              :       ! short-circuit in Fortran.
     206           22 :       use_cutoff_w0 = bs_env%ri_rs%cutoff_radius_w0 > 0.0_dp
     207              : 
     208              :       ! chunk_centroids is built only by the non-periodic RI-RS GW driver, so on a periodic cell
     209              :       ! the cut would silently fall through to the exact path while the log reports it as active.
     210           22 :       IF (use_cutoff_w0 .AND. .NOT. ALLOCATED(bs_env%ri_rs%chunk_centroids)) THEN
     211              :          CALL cp_abort(__LOCATION__, &
     212              :                        "RTBSE%CUTOFF_RADIUS_W0 needs the per-grid-block centroids, which only the "// &
     213              :                        "non-periodic RI-RS GW driver builds; this cell is periodic. Use '&CELL "// &
     214            0 :                        "PERIODIC NONE', or drop CUTOFF_RADIUS_W0 to propagate with the exact W0.")
     215              :       END IF
     216              : 
     217              :       IF (use_cutoff_w0) THEN
     218              :          CALL reserve_blocks_within_radius(bs_env%ri_rs%mat_W0_grid_rtbse, &
     219              :                                            bs_env%ri_rs%chunk_centroids, &
     220            6 :                                            bs_env%ri_rs%cutoff_radius_w0)
     221              :          ! TODO: eps_filter is INERT here -- DBCSR skips multrec_filtering whenever
     222              :          ! keep_sparsity is set (dbcsr_mm_multrec.F: "IF (use_eps .AND. .NOT. keep_sparsity)").
     223              :          ! So a radius wider than the cell equals the uncut build only while eps_filter prunes
     224              :          ! nothing on W0, which holds for every system measured so far but is not guaranteed.
     225              :          CALL dbcsr_contract_ABA("N", "T", bs_env%ri_rs%mat_Z_lP, matrix_W_aux, &
     226              :                                  bs_env%ri_rs%mat_W0_grid_rtbse, bs_env%eps_filter, &
     227            6 :                                  retain_sparsity=.TRUE.)
     228              :       ELSE
     229              :          CALL dbcsr_contract_ABA("N", "T", bs_env%ri_rs%mat_Z_lP, matrix_W_aux, &
     230           16 :                                  bs_env%ri_rs%mat_W0_grid_rtbse, bs_env%eps_filter)
     231              :       END IF
     232              : 
     233           22 :       bs_env%ri_rs%W0_grid_built = .TRUE.
     234              : 
     235              :       ! Occupancy is a storage statistic with no magnitude, so pair it with the retained Frobenius
     236              :       ! weight -- the two together are the x-axis of a CUTOFF_RADIUS_W0 convergence scan. Both
     237              :       ! reduce over all ranks; occupancy saturates at 1.000 and says nothing about magnitude.
     238           22 :       occ_w0 = dbcsr_get_occupation(bs_env%ri_rs%mat_W0_grid_rtbse)
     239           22 :       fnorm_w0 = dbcsr_frobenius_norm(bs_env%ri_rs%mat_W0_grid_rtbse)
     240              : 
     241           22 :       IF (bs_env%unit_nr > 0) THEN
     242              :          WRITE (bs_env%unit_nr, '(T2,A,T67,F14.4)') &
     243           11 :             'W0CUT| Cutoff radius, negative = off [angstrom]', bs_env%ri_rs%cutoff_radius_w0*angstrom
     244           11 :          WRITE (bs_env%unit_nr, '(T2,A,T67,F14.6)') 'W0CUT| Retained block fraction', occ_w0
     245           11 :          WRITE (bs_env%unit_nr, '(T2,A,T59,ES22.8)') 'W0CUT| Retained Frobenius norm', fnorm_w0
     246              :          WRITE (bs_env%unit_nr, '(T2,A,T57,A,F7.1,A)') &
     247           11 :             'Precomputed RT-BSE RI-RS W0_grid kernel,', ' Execution time', &
     248           22 :             m_walltime() - t1, ' s'
     249           11 :          WRITE (bs_env%unit_nr, '(A)') ' '
     250              :       END IF
     251              : 
     252           22 :       CALL release_dbcsr_topology_and_matrices(dist=dist_grid_grid, mapped_dist=dist_col_grid)
     253              :       CALL release_dbcsr_topology_and_matrices(dist=dist_aux_aux, mapped_dist=dist_row_aux, &
     254           22 :                                                m1=matrix_V_aux, m2=matrix_W_aux)
     255           22 :       CALL cp_fm_release(fm_Vtr_Gamma)
     256              : 
     257           22 :       CALL timestop(handle)
     258              : 
     259           44 :    END SUBROUTINE rt_bse_ri_rs_ensure_W0_grid
     260              : 
     261              : ! **************************************************************************************************
     262              : !> \brief AO-domain SEX:  Σ_µν = pref * sum_ll' φ_lµ [ρ^grid ∘ W^0]_ll' φ_l'ν,
     263              : !>        ρ^grid_ll' = sum_µν φ_lµ Δρ_µν φ_l'ν.
     264              : !>        The grid×grid ρ^grid is intrinsic to SEX -- the Hadamard needs W^0's elements, so no
     265              : !>        factorized application exists (unlike the Hartree V_ll'). Real input, real output;
     266              : !>        used for COH (input S^-1) and the init reference (ρ^0); dynamic Δρ goes through the
     267              : !>        complex variant. Mirrors the AO-RI get_sigma(rtbse_env, sigma_fm, prefactor, rho_fm) API.
     268              : !> \param bs_env ...
     269              : !> \param sigma_AO_fm result, AO x AO
     270              : !> \param prefactor scaling applied to the final result
     271              : !> \param rho_AO_fm input density-like matrix, AO x AO
     272              : !> \param grid_diag_accum ...
     273              : ! **************************************************************************************************
     274         4600 :    SUBROUTINE compute_sigma_ri_rs(bs_env, sigma_AO_fm, prefactor, rho_AO_fm, &
     275         4600 :                                   grid_diag_accum)
     276              : 
     277              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     278              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: sigma_AO_fm
     279              :       REAL(KIND=dp), INTENT(IN)                          :: prefactor
     280              :       TYPE(cp_fm_type), INTENT(IN)                       :: rho_AO_fm
     281              :       REAL(KIND=dp), INTENT(INOUT), OPTIONAL             :: grid_diag_accum(:)
     282              : 
     283              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_sigma_ri_rs'
     284              : 
     285              :       INTEGER                                            :: handle, n_grid
     286         4600 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: blk_ao, dist_row_ao
     287         4600 :       REAL(KIND=dp), ALLOCATABLE                         :: diag_local(:)
     288              :       TYPE(dbcsr_distribution_type)                      :: dist_ao_ao
     289              :       TYPE(dbcsr_type)                                   :: matrix_rho_AO, matrix_rho_grid, &
     290              :                                                             matrix_Sigma_AO
     291              : 
     292         4600 :       CALL timeset(routineN, handle)
     293              : 
     294         4600 :       CPASSERT(bs_env%ri_rs%W0_grid_built)
     295              : 
     296              :       ! Only the AO×AO topology is built here; ρ^grid takes its grid×grid one from W^0's template.
     297         4600 :       CALL setup_square_topology(bs_env%ri_rs%mat_phi_mu_l, 'COL', dist_ao_ao, blk_ao, dist_row_ao)
     298              : 
     299              :       CALL dbcsr_create(matrix_rho_AO, "rho_AO_ri_rs", dist_ao_ao, dbcsr_type_no_symmetry, &
     300         4600 :                         blk_ao, blk_ao)
     301         4600 :       CALL copy_fm_to_dbcsr(rho_AO_fm, matrix_rho_AO, keep_sparsity=.FALSE.)
     302              : 
     303              :       ! The projection below is shared with the Hartree, so this cuts SEX and Hartree alike.
     304         4600 :       IF (bs_env%ri_rs%eps_filter_rho >= 0.0_dp) THEN
     305            0 :          CALL dbcsr_filter(matrix_rho_AO, bs_env%ri_rs%eps_filter_rho)
     306              :       END IF
     307              : 
     308              :       ! ρ^grid_ll' = sum_µν φ_lµ Δρ_µν φ_l'ν   (the SEX-intrinsic grid×grid transient),
     309              :       ! built directly into W^0's block pattern. Exact: the Hadamard below zeroes every block
     310              :       ! without a W^0 partner anyway, so restricting the multiply drops only computed zeros.
     311              :       ! No eps_filter on the SEX projections either -- a Δρ-dependent block drop breaks kernel
     312              :       ! self-adjointness (L non-Hermitian); W^0's pattern is symmetric and Δρ-independent.
     313              :       ! TODO: seeds the pattern by copying W0's values and discarding them every RK4 substep --
     314              :       ! unquantified overhead, paid even when no cut is set. Cache the zeroed pattern instead.
     315         4600 :       CALL dbcsr_create(matrix_rho_grid, template=bs_env%ri_rs%mat_W0_grid_rtbse)
     316         4600 :       CALL dbcsr_copy(matrix_rho_grid, bs_env%ri_rs%mat_W0_grid_rtbse)
     317         4600 :       CALL dbcsr_set(matrix_rho_grid, 0.0_dp)
     318              :       CALL dbcsr_contract_ABA("N", "T", bs_env%ri_rs%mat_phi_mu_l, matrix_rho_AO, &
     319         4600 :                               matrix_rho_grid, 0.0_dp, retain_sparsity=.TRUE.)
     320              : 
     321              :       ! Harvest n_l = diag(ρ^grid) BEFORE the in-place Hadamard destroys it; the Hartree reuses it
     322              :       ! (compute_hartree_ri_rs_from_diag) instead of rebuilding φρφ^T. Bare accumulate (caller
     323              :       ! pre-zeroes): the cross-spin Hartree density is the spin SUM of these, and spin_degeneracy is
     324              :       ! applied to the V_H OUTPUT (post-filter, bit-identical) -- never to the diagonal, which would
     325              :       ! shift the stage-3 eps_filter cut (coarse-filter sensitivity).
     326         4600 :       IF (PRESENT(grid_diag_accum)) THEN
     327         3040 :          n_grid = SIZE(grid_diag_accum)
     328         9120 :          ALLOCATE (diag_local(n_grid))
     329         3040 :          diag_local = 0.0_dp
     330         3040 :          CALL dbcsr_get_diag(matrix_rho_grid, diag_local)
     331         3040 :          CALL bs_env%para_env%sum(diag_local)
     332       889440 :          grid_diag_accum(:) = grid_diag_accum(:) + diag_local(:)
     333         3040 :          DEALLOCATE (diag_local)
     334              :       END IF
     335              : 
     336              :       ! ρ^grid_ll' <- ρ^grid_ll' * W^0_ll'   (in place; blocks without a W^0 partner zeroed)
     337              :       CALL hadamard_product_inplace(matrix_rho_grid, bs_env%ri_rs%mat_W0_grid_rtbse, &
     338         4600 :                                     1.0_dp)
     339              : 
     340              :       ! Σ_µν = sum_ll' φ_lµ [ρ^grid ∘ W^0]_ll' φ_l'ν
     341         4600 :       CALL dbcsr_create(matrix_Sigma_AO, template=matrix_rho_AO)
     342              :       ! Unfiltered for self-adjointness (see the forward projection above; PERF note there).
     343              :       CALL dbcsr_contract_ABA("T", "N", bs_env%ri_rs%mat_phi_mu_l, matrix_rho_grid, &
     344         4600 :                               matrix_Sigma_AO, 0.0_dp)
     345              : 
     346         4600 :       CALL dbcsr_scale(matrix_Sigma_AO, prefactor)
     347         4600 :       CALL copy_dbcsr_to_fm(matrix_Sigma_AO, sigma_AO_fm)
     348              : 
     349              :       CALL release_dbcsr_topology_and_matrices(dist=dist_ao_ao, mapped_dist=dist_row_ao, &
     350         4600 :                                                m1=matrix_rho_AO, m2=matrix_Sigma_AO)
     351         4600 :       CALL release_dbcsr_topology_and_matrices(m1=matrix_rho_grid)
     352              : 
     353         4600 :       CALL timestop(handle)
     354              : 
     355         9200 :    END SUBROUTINE compute_sigma_ri_rs
     356              : 
     357              : ! **************************************************************************************************
     358              : !> \brief Complex-input AO SEX via Re/Im split: the kernel is real, so complex linearity holds as
     359              : !>        Σ[Δρ] = Σ[Re Δρ] + i Σ[Im Δρ].  Required for non-Hermitian Δρ inputs
     360              : !>        (TDA OV-only / ABBA OV+VO).
     361              : !> \param bs_env ...
     362              : !> \param sigma_AO_cfm result, AO x AO (complex)
     363              : !> \param prefactor scaling applied to the final result
     364              : !> \param rho_AO_cfm input AO x AO complex matrix
     365              : !> \param grid_diag_re_accum optional: accumulate diag(φ.Re(ρ).φ^T) (bare; for the Hartree reuse)
     366              : !> \param grid_diag_im_accum optional: accumulate diag(φ.Im(ρ).φ^T) (bare; for the Hartree reuse)
     367              : ! **************************************************************************************************
     368        11500 :    SUBROUTINE compute_sigma_ri_rs_complex(bs_env, sigma_AO_cfm, prefactor, rho_AO_cfm, &
     369         2300 :                                           grid_diag_re_accum, grid_diag_im_accum)
     370              : 
     371              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     372              :       TYPE(cp_cfm_type), INTENT(INOUT)                   :: sigma_AO_cfm
     373              :       REAL(KIND=dp), INTENT(IN)                          :: prefactor
     374              :       TYPE(cp_cfm_type), INTENT(IN)                      :: rho_AO_cfm
     375              :       REAL(KIND=dp), INTENT(INOUT), OPTIONAL             :: grid_diag_re_accum(:), &
     376              :                                                             grid_diag_im_accum(:)
     377              : 
     378              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_sigma_ri_rs_complex'
     379              : 
     380              :       INTEGER                                            :: handle
     381              :       TYPE(cp_cfm_type)                                  :: cfm_real_part
     382              :       TYPE(cp_fm_type)                                   :: fm_rho, fm_sigma
     383              : 
     384         2300 :       CALL timeset(routineN, handle)
     385              : 
     386         2300 :       CALL cp_fm_create(fm_rho, bs_env%fm_s_Gamma%matrix_struct)
     387         2300 :       CALL cp_fm_create(fm_sigma, bs_env%fm_s_Gamma%matrix_struct)
     388         2300 :       CALL cp_cfm_create(cfm_real_part, bs_env%fm_s_Gamma%matrix_struct)
     389              : 
     390              :       ! Re/Im each harvest into their own accumulator; absent optionals propagate as absent.
     391         2300 :       CALL cp_cfm_to_fm(msource=rho_AO_cfm, mtargetr=fm_rho)
     392              :       CALL compute_sigma_ri_rs(bs_env, fm_sigma, prefactor, fm_rho, &
     393         2680 :                                grid_diag_accum=grid_diag_re_accum)
     394         2300 :       CALL cp_fm_to_cfm(msourcer=fm_sigma, mtarget=cfm_real_part)
     395              : 
     396         2300 :       CALL cp_cfm_to_fm(msource=rho_AO_cfm, mtargeti=fm_rho)
     397              :       CALL compute_sigma_ri_rs(bs_env, fm_sigma, prefactor, fm_rho, &
     398         3480 :                                grid_diag_accum=grid_diag_im_accum)
     399         2300 :       CALL cp_fm_to_cfm(msourcei=fm_sigma, mtarget=sigma_AO_cfm)
     400              : 
     401              :       CALL cp_cfm_scale_and_add(CMPLX(1.0_dp, 0.0_dp, kind=dp), sigma_AO_cfm, &
     402         2300 :                                 CMPLX(1.0_dp, 0.0_dp, kind=dp), cfm_real_part)
     403              : 
     404         2300 :       CALL cp_fm_release(fm_rho)
     405         2300 :       CALL cp_fm_release(fm_sigma)
     406         2300 :       CALL cp_cfm_release(cfm_real_part)
     407              : 
     408         2300 :       CALL timestop(handle)
     409              : 
     410         2300 :    END SUBROUTINE compute_sigma_ri_rs_complex
     411              : 
     412              : ! **************************************************************************************************
     413              : !> \brief AO-domain Hartree via RI-RS:
     414              : !>          n_l = sum_µν φ_lµ Δρ_µν φ_lν = (φ ρ φ^T)_ll  (diagonal of materialized grid×grid) ;
     415              : !>          v_l = sum_PQl' Z_lP V^aux_PQ Z_l'Q n_l'   (factorized, stage 2) ;
     416              : !>          V^H_µν = sum_l φ_lµ v_l φ_lν   (diagonal-only row-scale, stage 3)
     417              : !>        Real input, real output; complex inputs go through compute_hartree_ri_rs_complex.
     418              : !> \param bs_env ...
     419              : !> \param rho_AO_fm input AO x AO density matrix
     420              : !> \param V_H_AO_fm output AO x AO Hartree potential
     421              : ! **************************************************************************************************
     422          578 :    SUBROUTINE compute_hartree_ri_rs(bs_env, rho_AO_fm, V_H_AO_fm)
     423              : 
     424              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     425              :       TYPE(cp_fm_type), INTENT(IN)                       :: rho_AO_fm
     426              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: V_H_AO_fm
     427              : 
     428              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_hartree_ri_rs'
     429              : 
     430              :       INTEGER                                            :: handle, n_grid
     431          578 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: blk_ao, blk_grid, dist_col_grid, &
     432          578 :                                                             dist_row_ao
     433              :       REAL(KIND=dp), ALLOCATABLE                         :: n_vec(:)
     434              :       TYPE(dbcsr_distribution_type)                      :: dist_ao_ao, dist_grid_grid
     435              :       TYPE(dbcsr_type)                                   :: matrix_rho_AO, matrix_rho_grid
     436              : 
     437          578 :       CALL timeset(routineN, handle)
     438              : 
     439          578 :       CPASSERT(bs_env%ri_rs%V_grid_built)
     440              : 
     441          578 :       CALL setup_square_topology(bs_env%ri_rs%mat_phi_mu_l, 'COL', dist_ao_ao, blk_ao, dist_row_ao)
     442              :       CALL setup_square_topology(bs_env%ri_rs%mat_phi_mu_l, 'ROW', dist_grid_grid, blk_grid, &
     443          578 :                                  dist_col_grid)
     444              : 
     445              :       CALL dbcsr_create(matrix_rho_AO, "rho_AO_hartree", dist_ao_ao, dbcsr_type_no_symmetry, &
     446          578 :                         blk_ao, blk_ao)
     447          578 :       CALL copy_fm_to_dbcsr(rho_AO_fm, matrix_rho_AO, keep_sparsity=.FALSE.)
     448              : 
     449         6710 :       n_grid = SUM(blk_grid)
     450         1734 :       ALLOCATE (n_vec(n_grid))
     451              : 
     452              :       ! stage 1: n_l = (φ ρ φ^T)_ll. A grid×AO row-dot (iterate φρ, look up φ) silently drops
     453              :       ! off-rank pairs at ≥2 ranks — φ is sparse and the product is not co-located with φ — so
     454              :       ! materialize φρφ^T and take its diagonal, co-location-safe like the SEX grid kernel.
     455              :       CALL dbcsr_create(matrix_rho_grid, "rho_grid_hartree", dist_grid_grid, &
     456          578 :                         dbcsr_type_no_symmetry, blk_grid, blk_grid)
     457              :       ! No eps_filter: a Δρ-dependent diagonal-block drop zeroes n_l inconsistently between OV
     458              :       ! pairs and breaks Hartree self-adjointness (L non-Hermitian).
     459              :       ! PERF(rirs-selfadjoint): this full grid×grid is built only for its diagonal n_l; a
     460              :       ! diagonal-only build would avoid it. NOT YET APPLIED, needs a perf test.
     461              :       CALL dbcsr_contract_ABA("N", "T", bs_env%ri_rs%mat_phi_mu_l, matrix_rho_AO, &
     462          578 :                               matrix_rho_grid, 0.0_dp)
     463          578 :       n_vec = 0.0_dp
     464          578 :       CALL dbcsr_get_diag(matrix_rho_grid, n_vec)
     465          578 :       CALL bs_env%para_env%sum(n_vec)
     466              :       CALL release_dbcsr_topology_and_matrices(dist=dist_grid_grid, mapped_dist=dist_col_grid, &
     467          578 :                                                m1=matrix_rho_grid)
     468              :       CALL release_dbcsr_topology_and_matrices(dist=dist_ao_ao, mapped_dist=dist_row_ao, &
     469          578 :                                                m1=matrix_rho_AO)
     470              : 
     471              :       ! stages 2-3: factorized Coulomb v = Z V^aux Z^T n, then V^H = φ^T diag(v) φ.
     472          578 :       CALL hartree_potential_from_diag_ri_rs(bs_env, n_vec, V_H_AO_fm)
     473              : 
     474          578 :       DEALLOCATE (n_vec)
     475              : 
     476          578 :       CALL timestop(handle)
     477              : 
     478         1156 :    END SUBROUTINE compute_hartree_ri_rs
     479              : 
     480              : ! **************************************************************************************************
     481              : !> \brief Hartree stages 2-3 from a precomputed grid density n_l (skips the stage-1 φρφ^T build):
     482              : !>          v_l = sum_PQl' Z_lP V^aux_PQ Z_l'Q n_l'   (factorized Coulomb) ;
     483              : !>          V^H_µν = sum_l φ_lµ v_l φ_lν   (Φ_lν = v_l φ_lν rowscale, then V^H = φ^T Φ).
     484              : !>        n_l is harvested as diag(φρφ^T) inside compute_sigma_ri_rs (the SEX grid kernel), so the
     485              : !>        Hartree never rebuilds the grid×grid product. Real in/out.
     486              : !> \param bs_env ...
     487              : !> \param n_vec grid density n_l (length n_grid, replicated)
     488              : !> \param V_H_AO_fm output AO x AO Hartree potential
     489              : ! **************************************************************************************************
     490         3138 :    SUBROUTINE hartree_potential_from_diag_ri_rs(bs_env, n_vec, V_H_AO_fm)
     491              : 
     492              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     493              :       REAL(KIND=dp), INTENT(IN)                          :: n_vec(:)
     494              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: V_H_AO_fm
     495              : 
     496              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'hartree_potential_from_diag_ri_rs'
     497              : 
     498              :       INTEGER                                            :: handle
     499         3138 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: blk_ao, dist_row_ao
     500              :       REAL(KIND=dp), ALLOCATABLE                         :: u_ri(:), v_vec(:), w_ri(:)
     501              :       TYPE(dbcsr_distribution_type)                      :: dist_ao_ao
     502              :       TYPE(dbcsr_type)                                   :: matrix_Phi, matrix_V_H_AO
     503              : 
     504         3138 :       CALL timeset(routineN, handle)
     505              : 
     506         3138 :       CPASSERT(bs_env%ri_rs%V_grid_built)
     507              : 
     508         3138 :       CALL setup_square_topology(bs_env%ri_rs%mat_phi_mu_l, 'COL', dist_ao_ao, blk_ao, dist_row_ao)
     509         9414 :       ALLOCATE (v_vec(SIZE(n_vec)))
     510              : 
     511              :       ! stage 2: v_l = sum_PQl' Z_lP V^aux_PQ Z_l'Q n_l'  (factorized; helper zeroes each output)
     512        12552 :       ALLOCATE (w_ri(bs_env%n_RI), u_ri(bs_env%n_RI))
     513              :       ! w_Q = sum_l Z_lQ n_l
     514              :       CALL dbcsr_matvec_replicated(bs_env%ri_rs%mat_Z_lP, n_vec, w_ri, bs_env%para_env, &
     515         3138 :                                    transposed=.TRUE.)
     516              :       ! u_P = sum_Q V^aux_PQ w_Q
     517         3138 :       CALL dbcsr_matvec_replicated(bs_env%ri_rs%mat_V_aux_rtbse, w_ri, u_ri, bs_env%para_env)
     518              :       ! v_l = sum_P Z_lP u_P
     519         3138 :       CALL dbcsr_matvec_replicated(bs_env%ri_rs%mat_Z_lP, u_ri, v_vec, bs_env%para_env)
     520         3138 :       DEALLOCATE (w_ri, u_ri)
     521              : 
     522              :       ! stage 3: V^H_µν = sum_l φ_lµ v_l φ_lν  (Φ_lν = v_l φ_lν rowscale, then V^H = φ^T Φ)
     523         3138 :       CALL dbcsr_create(matrix_Phi, template=bs_env%ri_rs%mat_phi_mu_l)
     524         3138 :       CALL dbcsr_copy(matrix_Phi, bs_env%ri_rs%mat_phi_mu_l)
     525         3138 :       CALL dbcsr_scale_rows_replicated(matrix_Phi, v_vec)
     526              : 
     527              :       CALL dbcsr_create(matrix_V_H_AO, "V_H_AO_hartree", dist_ao_ao, dbcsr_type_no_symmetry, &
     528         3138 :                         blk_ao, blk_ao)
     529              :       ! No eps_filter: a Δρ-dependent block drop breaks Hartree kernel self-adjointness.
     530              :       CALL dbcsr_multiply("T", "N", 1.0_dp, bs_env%ri_rs%mat_phi_mu_l, matrix_Phi, &
     531         3138 :                           0.0_dp, matrix_V_H_AO, filter_eps=0.0_dp)
     532         3138 :       CALL dbcsr_release(matrix_Phi)
     533              : 
     534         3138 :       CALL copy_dbcsr_to_fm(matrix_V_H_AO, V_H_AO_fm)
     535              : 
     536         3138 :       DEALLOCATE (v_vec)
     537              : 
     538              :       CALL release_dbcsr_topology_and_matrices(dist=dist_ao_ao, mapped_dist=dist_row_ao, &
     539         3138 :                                                m1=matrix_V_H_AO)
     540              : 
     541         3138 :       CALL timestop(handle)
     542              : 
     543         6276 :    END SUBROUTINE hartree_potential_from_diag_ri_rs
     544              : 
     545              : ! **************************************************************************************************
     546              : !> \brief Complex Hartree from precomputed grid diagonals: V^H = V^H[n_re] + i V^H[n_im], each via
     547              : !>        hartree_potential_from_diag_ri_rs (stages 2-3 only). n_re/n_im are the spin-summed grid
     548              : !>        densities harvested in the SEX kernel; this is the cross-spin / TDA complex consumer that
     549              : !>        replaces compute_hartree_ri_rs_complex when SEX already built the grid. n_im optional: when
     550              : !>        absent the result is purely real (matches the Re-only real-input Hartree).
     551              : !> \param bs_env ...
     552              : !> \param n_re grid density Re part (length n_grid, replicated)
     553              : !> \param V_H_AO_cfm output AO x AO complex Hartree potential
     554              : !> \param n_im optional grid density Im part (length n_grid, replicated)
     555              : ! **************************************************************************************************
     556         1600 :    SUBROUTINE compute_hartree_ri_rs_from_diag(bs_env, n_re, V_H_AO_cfm, n_im)
     557              : 
     558              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     559              :       REAL(KIND=dp), INTENT(IN)                          :: n_re(:)
     560              :       TYPE(cp_cfm_type), INTENT(INOUT)                   :: V_H_AO_cfm
     561              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: n_im(:)
     562              : 
     563              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_hartree_ri_rs_from_diag'
     564              : 
     565              :       INTEGER                                            :: handle
     566              :       TYPE(cp_cfm_type)                                  :: cfm_real_part
     567              :       TYPE(cp_fm_type)                                   :: fm_v
     568              : 
     569         1600 :       CALL timeset(routineN, handle)
     570              : 
     571         1600 :       CALL cp_fm_create(fm_v, bs_env%fm_s_Gamma%matrix_struct)
     572              : 
     573         1600 :       CALL hartree_potential_from_diag_ri_rs(bs_env, n_re, fm_v)
     574         1600 :       IF (PRESENT(n_im)) THEN
     575          960 :          CALL cp_cfm_create(cfm_real_part, bs_env%fm_s_Gamma%matrix_struct)
     576          960 :          CALL cp_fm_to_cfm(msourcer=fm_v, mtarget=cfm_real_part)
     577          960 :          CALL hartree_potential_from_diag_ri_rs(bs_env, n_im, fm_v)
     578          960 :          CALL cp_fm_to_cfm(msourcei=fm_v, mtarget=V_H_AO_cfm)
     579              :          CALL cp_cfm_scale_and_add(CMPLX(1.0_dp, 0.0_dp, kind=dp), V_H_AO_cfm, &
     580          960 :                                    CMPLX(1.0_dp, 0.0_dp, kind=dp), cfm_real_part)
     581          960 :          CALL cp_cfm_release(cfm_real_part)
     582              :       ELSE
     583          640 :          CALL cp_fm_to_cfm(msourcer=fm_v, mtarget=V_H_AO_cfm)
     584              :       END IF
     585              : 
     586         1600 :       CALL cp_fm_release(fm_v)
     587              : 
     588         1600 :       CALL timestop(handle)
     589              : 
     590         1600 :    END SUBROUTINE compute_hartree_ri_rs_from_diag
     591              : 
     592              : ! **************************************************************************************************
     593              : !> \brief Complex-input Hartree potential via RI-RS. Re/Im split: feed each part to the real
     594              : !>        compute_hartree_ri_rs and reassemble. Real-input Hartree on a non-Hermitian input
     595              : !>        would silently drop Im and break Hermitian conjugacy of OV+VO contributions in TDA.
     596              : !> \param bs_env ...
     597              : !> \param rho_AO_cfm input AO x AO complex density-like matrix
     598              : !> \param V_H_AO_cfm output AO x AO complex Hartree potential
     599              : ! **************************************************************************************************
     600         1144 :    SUBROUTINE compute_hartree_ri_rs_complex(bs_env, rho_AO_cfm, V_H_AO_cfm)
     601              : 
     602              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     603              :       TYPE(cp_cfm_type), INTENT(IN)                      :: rho_AO_cfm
     604              :       TYPE(cp_cfm_type), INTENT(INOUT)                   :: V_H_AO_cfm
     605              : 
     606              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_hartree_ri_rs_complex'
     607              : 
     608              :       INTEGER                                            :: handle
     609              :       TYPE(cp_cfm_type)                                  :: cfm_real_part
     610              :       TYPE(cp_fm_type)                                   :: fm_rho, fm_v
     611              : 
     612          286 :       CALL timeset(routineN, handle)
     613              : 
     614          286 :       CALL cp_fm_create(fm_rho, bs_env%fm_s_Gamma%matrix_struct)
     615          286 :       CALL cp_fm_create(fm_v, bs_env%fm_s_Gamma%matrix_struct)
     616          286 :       CALL cp_cfm_create(cfm_real_part, bs_env%fm_s_Gamma%matrix_struct)
     617              : 
     618          286 :       CALL cp_cfm_to_fm(msource=rho_AO_cfm, mtargetr=fm_rho)
     619          286 :       CALL compute_hartree_ri_rs(bs_env, fm_rho, fm_v)
     620          286 :       CALL cp_fm_to_cfm(msourcer=fm_v, mtarget=cfm_real_part)
     621              : 
     622          286 :       CALL cp_cfm_to_fm(msource=rho_AO_cfm, mtargeti=fm_rho)
     623          286 :       CALL compute_hartree_ri_rs(bs_env, fm_rho, fm_v)
     624          286 :       CALL cp_fm_to_cfm(msourcei=fm_v, mtarget=V_H_AO_cfm)
     625              : 
     626              :       CALL cp_cfm_scale_and_add(CMPLX(1.0_dp, 0.0_dp, kind=dp), V_H_AO_cfm, &
     627          286 :                                 CMPLX(1.0_dp, 0.0_dp, kind=dp), cfm_real_part)
     628              : 
     629          286 :       CALL cp_fm_release(fm_rho)
     630          286 :       CALL cp_fm_release(fm_v)
     631          286 :       CALL cp_cfm_release(cfm_real_part)
     632              : 
     633          286 :       CALL timestop(handle)
     634              : 
     635          286 :    END SUBROUTINE compute_hartree_ri_rs_complex
     636              : 
     637              : ! **************************************************************************************************
     638              : !> \brief Scale each row of a dbcsr matrix by a replicated full-length vector:
     639              : !>        block(ir,ic) <- vec(global_row(ir)) * block(ir,ic). Value mutation only.
     640              : !> \param matrix ...
     641              : !> \param vec full-length replicated row-scaling vector
     642              : ! **************************************************************************************************
     643         3138 :    SUBROUTINE dbcsr_scale_rows_replicated(matrix, vec)
     644              : 
     645              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: matrix
     646              :       REAL(KIND=dp), INTENT(IN)                          :: vec(:)
     647              : 
     648              :       INTEGER                                            :: col_blk, ic, ir, nblkrows_total, &
     649              :                                                             row_blk, row_off
     650         3138 :       INTEGER, ALLOCATABLE                               :: row_offset(:)
     651         3138 :       INTEGER, DIMENSION(:), POINTER                     :: row_blk_size
     652         3138 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: blk
     653              :       TYPE(dbcsr_iterator_type)                          :: iter
     654              : 
     655         3138 :       CALL dbcsr_get_info(matrix, nblkrows_total=nblkrows_total, row_blk_size=row_blk_size)
     656         9414 :       ALLOCATE (row_offset(nblkrows_total + 1))
     657         3138 :       row_offset(1) = 0
     658        35830 :       DO ir = 1, nblkrows_total
     659        35830 :          row_offset(ir + 1) = row_offset(ir) + row_blk_size(ir)
     660              :       END DO
     661              : 
     662         3138 :       CALL dbcsr_iterator_start(iter, matrix)
     663        35830 :       DO WHILE (dbcsr_iterator_blocks_left(iter))
     664        32692 :          CALL dbcsr_iterator_next_block(iter, row_blk, col_blk, blk)
     665        32692 :          row_off = row_offset(row_blk)
     666       231982 :          DO ic = 1, SIZE(blk, 2)
     667      5594092 :             DO ir = 1, SIZE(blk, 1)
     668      5561400 :                blk(ir, ic) = vec(row_off + ir)*blk(ir, ic)
     669              :             END DO
     670              :          END DO
     671              :       END DO
     672         3138 :       CALL dbcsr_iterator_stop(iter)
     673              : 
     674         3138 :       DEALLOCATE (row_offset)
     675              : 
     676         6276 :    END SUBROUTINE dbcsr_scale_rows_replicated
     677              : 
     678              : ! **************************************************************************************************
     679              : !> \brief Replicated matvec: vec_out = matrix * vec_in (or matrix^T * vec_in if transposed) for a
     680              : !>        possibly rectangular distributed dbcsr matrix and replicated full-length vectors.
     681              : !>        Iterates over local blocks and reduces.
     682              : !> \param matrix distributed dbcsr matrix (may be rectangular)
     683              : !> \param vec_in full input vector, replicated on all ranks (column length, or row length if transposed)
     684              : !> \param vec_out full output vector, replicated on all ranks (zeroed on entry; sum-reduced on exit)
     685              : !> \param para_env ...
     686              : !> \param transposed if .TRUE. compute vec_out = matrix^T * vec_in
     687              : ! **************************************************************************************************
     688         9414 :    SUBROUTINE dbcsr_matvec_replicated(matrix, vec_in, vec_out, para_env, transposed)
     689              : 
     690              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: matrix
     691              :       REAL(KIND=dp), INTENT(IN)                          :: vec_in(:)
     692              :       REAL(KIND=dp), INTENT(INOUT)                       :: vec_out(:)
     693              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     694              :       LOGICAL, INTENT(IN), OPTIONAL                      :: transposed
     695              : 
     696              :       INTEGER                                            :: col_blk, col_off, ic, ir, &
     697              :                                                             nblkcols_total, nblkrows_total, &
     698              :                                                             row_blk, row_off
     699         9414 :       INTEGER, ALLOCATABLE                               :: col_offset(:), row_offset(:)
     700         9414 :       INTEGER, DIMENSION(:), POINTER                     :: col_blk_size, row_blk_size
     701              :       LOGICAL                                            :: my_trans
     702         9414 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: block
     703              :       TYPE(dbcsr_iterator_type)                          :: iter
     704              : 
     705         9414 :       my_trans = .FALSE.
     706         3138 :       IF (PRESENT(transposed)) my_trans = transposed
     707              : 
     708              :       CALL dbcsr_get_info(matrix, nblkrows_total=nblkrows_total, &
     709              :                           nblkcols_total=nblkcols_total, &
     710         9414 :                           row_blk_size=row_blk_size, col_blk_size=col_blk_size)
     711              : 
     712        47070 :       ALLOCATE (row_offset(nblkrows_total + 1), col_offset(nblkcols_total + 1))
     713         9414 :       row_offset(1) = 0
     714        81074 :       DO ir = 1, nblkrows_total
     715        81074 :          row_offset(ir + 1) = row_offset(ir) + row_blk_size(ir)
     716              :       END DO
     717         9414 :       col_offset(1) = 0
     718        28242 :       DO ic = 1, nblkcols_total
     719        28242 :          col_offset(ic + 1) = col_offset(ic) + col_blk_size(ic)
     720              :       END DO
     721              : 
     722      1192318 :       vec_out(:) = 0.0_dp
     723              : 
     724         9414 :       CALL dbcsr_iterator_start(iter, matrix)
     725        81074 :       DO WHILE (dbcsr_iterator_blocks_left(iter))
     726        71660 :          CALL dbcsr_iterator_next_block(iter, row_blk, col_blk, block)
     727        71660 :          row_off = row_offset(row_blk)
     728        71660 :          col_off = col_offset(col_blk)
     729        81074 :          IF (my_trans) THEN
     730       784608 :             DO ic = 1, SIZE(block, 2)
     731     21351392 :                DO ir = 1, SIZE(block, 1)
     732              :                   vec_out(col_off + ic) = vec_out(col_off + ic) + &
     733     21318700 :                                           block(ir, ic)*vec_in(row_off + ir)
     734              :                END DO
     735              :             END DO
     736              :          ELSE
     737       935232 :             DO ic = 1, SIZE(block, 2)
     738     24822020 :                DO ir = 1, SIZE(block, 1)
     739              :                   vec_out(row_off + ir) = vec_out(row_off + ir) + &
     740     24783052 :                                           block(ir, ic)*vec_in(col_off + ic)
     741              :                END DO
     742              :             END DO
     743              :          END IF
     744              :       END DO
     745         9414 :       CALL dbcsr_iterator_stop(iter)
     746              : 
     747         9414 :       DEALLOCATE (row_offset, col_offset)
     748              : 
     749      2375222 :       CALL para_env%sum(vec_out)
     750              : 
     751        21966 :    END SUBROUTINE dbcsr_matvec_replicated
     752              : 
     753              : END MODULE rt_bse_ri_rs
        

Generated by: LCOV version 2.0-1