LCOV - code coverage report
Current view: top level - src - ri_rs_kp_fitting.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 92.7 % 356 330
Test Date: 2026-11-24 01:22:08 Functions: 70.0 % 10 7

            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 AO sampling, D_llp(R), and the periodic RI-RS fit Z_lP(q).
      10              : !> \author Jan Wilhelm (with help of ChatGPT, GPT-6 Sol and GPT-6 Astra)
      11              : ! **************************************************************************************************
      12              : MODULE ri_rs_kp_fitting
      13              :    USE basis_set_types,                 ONLY: gto_basis_set_type
      14              :    USE cell_types,                      ONLY: cell_type,&
      15              :                                               pbc
      16              :    USE input_constants,                 ONLY: do_potential_short,&
      17              :                                               rskp_backend_gemm
      18              :    USE kinds,                           ONLY: dp,&
      19              :                                               int_8
      20              :    USE kpoint_lattice_fft,              ONLY: fft_profile_enabled
      21              :    USE kpoint_transform,                ONLY: cell_to_k_transform
      22              :    USE machine,                         ONLY: m_walltime
      23              :    USE mathconstants,                   ONLY: pi,&
      24              :                                               z_one,&
      25              :                                               z_zero
      26              :    USE message_passing,                 ONLY: mp_print_mem_per_rank,&
      27              :                                               mp_request_type,&
      28              :                                               mp_waitall
      29              :    USE particle_types,                  ONLY: particle_type
      30              :    USE physcon,                         ONLY: angstrom
      31              :    USE post_scf_bandstructure_types,    ONLY: post_scf_bandstructure_type
      32              :    USE ri_rs_kp_integrals,              ONLY: compute_d_lP_R,&
      33              :                                               compute_d_lP_cutoffs,&
      34              :                                               get_local_l_range,&
      35              :                                               write_ri_rs_progress
      36              :    USE ri_rs_utils,                     ONLY: evaluate_ao_basis_on_points
      37              : #include "./base/base_uses.f90"
      38              : 
      39              :    IMPLICIT NONE
      40              :    PRIVATE
      41              : 
      42              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ri_rs_kp_fitting'
      43              : 
      44              :    ! Complex numbers in the 64 MiB batches of q points.
      45              :    INTEGER(KIND=int_8), PARAMETER :: q_batch_elements = 64_int_8*1024_int_8**2/16_int_8
      46              : 
      47              :    TYPE :: ao_cell_type
      48              :       INTEGER :: index = 0
      49              :       REAL(dp), ALLOCATABLE :: phi_local(:, :)
      50              :    END TYPE ao_cell_type
      51              : 
      52              :    PUBLIC :: compute_Z_lP_q, &
      53              :              evaluate_mu_rl_R_values
      54              : 
      55              : CONTAINS
      56              : 
      57              : ! **************************************************************************************************
      58              : !> \brief Build explicit-cell AO values and periodic RI-RS coefficients Z_lP(q).
      59              : !> \param bs_env Band-structure environment.
      60              : !> \param grid_points Reference-cell RI-RS points r_l.
      61              : !> \param q_slot Compact owner-local index of each retained q, zero for omitted points.
      62              : !> \param Z_lP_q Gaussian RI-RS coefficients at the retained q points.
      63              : !> \param Z_lP_zero ...
      64              : ! **************************************************************************************************
      65           14 :    SUBROUTINE compute_Z_lP_q(bs_env, grid_points, q_slot, &
      66              :                              Z_lP_q, Z_lP_zero)
      67              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
      68              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(IN)         :: grid_points
      69              :       INTEGER, INTENT(IN)                                :: q_slot(:)
      70              :       COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT)         :: Z_lP_q(:, :, :), Z_lP_zero(:, :, :)
      71              : 
      72              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'compute_Z_lP_q'
      73              : 
      74           14 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: charge_lq(:, :)
      75              :       INTEGER                                            :: first_l, handle, last_l, n_d_lP_cells, &
      76              :                                                             n_grid
      77           14 :       INTEGER, ALLOCATABLE                               :: cells_R_D_llp(:, :), cells_R_d_lP(:, :)
      78              :       REAL(KIND=dp)                                      :: elapsed, start_time
      79           14 :       REAL(KIND=dp), ALLOCATABLE                         :: D_llp_R(:, :, :), d_lP_R(:, :, :), &
      80           14 :                                                             qpoints_head(:, :), qpoints_scf(:, :)
      81              : 
      82           14 :       CALL timeset(routineN, handle)
      83              : 
      84           14 :       start_time = m_walltime()
      85           14 :       IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, '(T2,A)') "RI-RS: Compute Z_lP(k); required: D_ll'^R and d_lP^R"
      86              : 
      87           14 :       n_grid = bs_env%ri_rs%n_grid_points
      88           14 :       CPASSERT(n_grid > 0)
      89           14 :       CPASSERT(SIZE(grid_points, 2) == n_grid)
      90           14 :       CPASSERT(bs_env%nkp_scf_desymm > 0)
      91              : 
      92           14 :       CALL build_cells_R_for_d_lP_R(bs_env, grid_points, cells_R_d_lP)
      93           14 :       n_d_lP_cells = SIZE(cells_R_d_lP, 2)
      94           14 :       CPASSERT(n_d_lP_cells > 0)
      95           14 :       IF (bs_env%unit_nr > 0) THEN
      96            7 :          WRITE (bs_env%unit_nr, '(T2,A,T72,I9)') 'RI-RS d_lP^R cells R:', n_d_lP_cells
      97              :       END IF
      98              : 
      99              :       ! Each rank stores a rectangular (l,l') block for every translation A.
     100           14 :       CALL get_local_l_range(n_grid, bs_env%para_env%num_pe, bs_env%para_env%mepos, first_l, last_l)
     101           14 :       CALL build_D_llp_R(bs_env, grid_points, first_l, last_l, cells_R_D_llp, D_llp_R)
     102           14 :       CALL mp_print_mem_per_rank(bs_env%para_env, bs_env%unit_nr, "After distribution of D_ll'^R:")
     103           14 :       IF (bs_env%unit_nr > 0) THEN
     104            7 :          WRITE (bs_env%unit_nr, '(T2,A)') 'RI-RS three-center projection d_lP^R: starting'
     105            7 :          FLUSH (bs_env%unit_nr)
     106              :       END IF
     107           42 :       ALLOCATE (qpoints_scf(3, bs_env%nkp_scf_desymm))
     108         1006 :       qpoints_scf(:, :) = bs_env%kpoints_chi_eps_W%xkp(:, 1:bs_env%nkp_scf_desymm)
     109           14 :       ALLOCATE (qpoints_head(3, 1), SOURCE=0.0_dp)
     110           14 :       CALL compute_d_lP_R(bs_env, grid_points, cells_R_d_lP, d_lP_R, qpoints_head, charge_lq)
     111           14 :       CALL mp_print_mem_per_rank(bs_env%para_env, bs_env%unit_nr, 'After RI-RS three-center projection d_lP^R')
     112              : 
     113              :       CALL solve_for_Z_lP_q(bs_env, cells_R_D_llp, cells_R_d_lP, D_llp_R, d_lP_R, first_l, last_l, qpoints_scf, &
     114           14 :                             q_slot, Z_lP_q, distributed=.TRUE.)
     115              :       CALL solve_for_Z_lP_q(bs_env, cells_R_D_llp, cells_R_d_lP, D_llp_R, d_lP_R, first_l, last_l, &
     116           14 :                             qpoints_head, [1], Z_lP_zero, distributed=.FALSE., charge_lq=charge_lq)
     117              : 
     118           14 :       DEALLOCATE (cells_R_d_lP, cells_R_D_llp, D_llp_R, qpoints_scf, d_lP_R)
     119           14 :       DEALLOCATE (qpoints_head)
     120              : 
     121           14 :       elapsed = m_walltime() - start_time
     122           14 :       CALL bs_env%para_env%max(elapsed)
     123           14 :       IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, '(T2,A,T69,F10.1,A)') &
     124            7 :          'Completed computing Z_lP(q), Exec. time:', elapsed, ' s'
     125           14 :       IF (bs_env%unit_nr > 0) FLUSH (bs_env%unit_nr)
     126              : 
     127           14 :       CALL timestop(handle)
     128              : 
     129           42 :    END SUBROUTINE compute_Z_lP_q
     130              : 
     131              : ! **************************************************************************************************
     132              : !> \brief Evaluate ϕ_μ(r_l-R) locally for one cell R and the supplied points r_l.
     133              : !> \param bs_env ...
     134              : !> \param grid_points ...
     135              : !> \param i_cell Index of R in index_to_cell_3c.
     136              : !> \param ao_values AO values indexed (l,μ), without MPI communication.
     137              : ! **************************************************************************************************
     138        13196 :    SUBROUTINE evaluate_mu_rl_R_values(bs_env, grid_points, i_cell, ao_values)
     139              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     140              :       REAL(dp), INTENT(IN)                               :: grid_points(:, :)
     141              :       INTEGER, INTENT(IN)                                :: i_cell
     142              :       REAL(dp), INTENT(OUT)                              :: ao_values(:, :)
     143              : 
     144              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'evaluate_mu_rl_R_values'
     145              : 
     146              :       INTEGER                                            :: first_ao, handle, i_atom, i_kind, last_ao
     147              :       REAL(dp)                                           :: cutoff_squared, source_position(3)
     148              :       TYPE(cell_type), POINTER                           :: cell
     149              :       TYPE(gto_basis_set_type), POINTER                  :: basis
     150        13196 :       TYPE(particle_type), POINTER                       :: particle_set(:)
     151              : 
     152        13196 :       CALL timeset(routineN, handle)
     153              : 
     154        13196 :       cell => bs_env%ri_rs%cell
     155        13196 :       particle_set => bs_env%ri_rs%particle_set
     156        13196 :       CPASSERT(SIZE(ao_values, 1) == SIZE(grid_points, 2))
     157        13196 :       CPASSERT(SIZE(ao_values, 2) == bs_env%n_ao)
     158      2480948 :       ao_values = 0.0_dp
     159        39588 :       DO i_atom = 1, bs_env%n_atom
     160        26392 :          i_kind = particle_set(i_atom)%atomic_kind%kind_number
     161        26392 :          basis => bs_env%basis_set_AO(i_kind)%gto_basis_set
     162        26392 :          first_ao = bs_env%i_ao_start_from_atom(i_atom)
     163        26392 :          last_ao = bs_env%i_ao_end_from_atom(i_atom)
     164              :          source_position = pbc(particle_set(i_atom)%r, cell) + &
     165       527840 :                            MATMUL(bs_env%hmat, REAL(bs_env%index_to_cell_3c(1:3, i_cell), dp))
     166        26392 :          cutoff_squared = bs_env%ri_rs%radius_ao_per_atom(i_atom)**2
     167        26392 :          IF (bs_env%ri_rs%cutoff_radius_ri_ao > 0.0_dp) THEN
     168            0 :             cutoff_squared = bs_env%ri_rs%cutoff_radius_ri_ao**2
     169              :          END IF
     170              :          CALL evaluate_ao_basis_on_points(ao_values(:, first_ao:last_ao), &
     171              :                                           grid_points, basis, source_position, cell, &
     172        39588 :                                           cutoff_squared=cutoff_squared, minimum_image=.FALSE.)
     173              :       END DO
     174              : 
     175        13196 :       CALL timestop(handle)
     176              : 
     177        13196 :    END SUBROUTINE evaluate_mu_rl_R_values
     178              : 
     179              : ! **************************************************************************************************
     180              : !> \brief Solve the periodic RI-RS equations at the supplied momentum-transfer points.
     181              : !> At zero Tikhonov parameter the Hermitian D matrix is solved in the
     182              : !> Moore-Penrose sense, yielding the minimum-norm unregularized fit.
     183              : !> \param bs_env ...
     184              : !> \param cells_R_D_llp ...
     185              : !> \param cells_R_d_lP ...
     186              : !> \param D_llp_R ...
     187              : !> \param d_lP_R ...
     188              : !> \param first_l ...
     189              : !> \param last_l ...
     190              : !> \param qpoints ...
     191              : !> \param q_slot compact local indices; zero omits a q point
     192              : !> \param Z_lP_q ...
     193              : !> \param distributed ...
     194              : !> \param charge_lq ...
     195              : ! **************************************************************************************************
     196           56 :    SUBROUTINE solve_for_Z_lP_q(bs_env, cells_R_D_llp, cells_R_d_lP, D_llp_R, d_lP_R, first_l, last_l, qpoints, &
     197           56 :                                q_slot, Z_lP_q, distributed, charge_lq)
     198              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     199              :       INTEGER, DIMENSION(:, :), INTENT(IN)               :: cells_R_D_llp, cells_R_d_lP
     200              :       REAL(KIND=dp), CONTIGUOUS, INTENT(IN)              :: D_llp_R(:, :, :), d_lP_R(:, :, :)
     201              :       INTEGER, INTENT(IN)                                :: first_l, last_l
     202              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(IN)         :: qpoints
     203              :       INTEGER, INTENT(IN)                                :: q_slot(:)
     204              :       COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT)         :: Z_lP_q(:, :, :)
     205              :       LOGICAL, INTENT(IN)                                :: distributed
     206              :       COMPLEX(KIND=dp), INTENT(IN), OPTIONAL             :: charge_lq(:, :)
     207              : 
     208              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'solve_for_Z_lP_q'
     209              : 
     210              :       COMPLEX(KIND=dp)                                   :: empty_matrix(0, 0), phase
     211           28 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: D_llp_q(:, :), D_llp_q_batch(:, :, :), &
     212              :                                                             D_llp_q_local(:, :), d_lP_q(:, :)
     213              :       INTEGER :: backend, first_l_rank, first_q, handle, i_batch, i_rank, info, iq, iq_batch, &
     214              :          iq_store, l, last_l_rank, last_q, lp, n_active, n_grid, n_q, n_q_local, n_rank, n_RI, &
     215              :          n_RI_solve, n_sub, next_progress
     216              :       INTEGER(KIND=int_8)                                :: n_done, n_local
     217           28 :       INTEGER, ALLOCATABLE                               :: displs(:), q_batch(:), recvcounts(:)
     218           28 :       LOGICAL, ALLOCATABLE                               :: active_grid_point(:)
     219              :       REAL(KIND=dp)                                      :: max_scale, metric_head, progress_start, &
     220              :                                                             volume
     221           28 :       REAL(KIND=dp), ALLOCATABLE                         :: scale(:)
     222              : 
     223           28 :       CALL timeset(routineN, handle)
     224              : 
     225           28 :       n_grid = bs_env%ri_rs%n_grid_points
     226           28 :       n_RI = bs_env%n_RI
     227           28 :       n_RI_solve = n_RI
     228           28 :       IF (PRESENT(charge_lq)) THEN
     229           14 :          IF (bs_env%ri_metric%potential_type /= do_potential_short) THEN
     230            0 :             CPABORT('Unsupported RI metric')
     231              :          END IF
     232           14 :          n_RI_solve = n_RI + 1
     233              :       END IF
     234           28 :       n_q = SIZE(qpoints, 2)
     235           28 :       n_rank = bs_env%para_env%num_pe
     236           28 :       CPASSERT(SIZE(q_slot) == n_q)
     237           28 :       backend = MERGE(bs_env%kpoints_scf_desymm%rskp_transform, rskp_backend_gemm, distributed)
     238              :       ! q points are cyclic: rank MOD(q,P) for the SCF mesh, MOD(q-1,P) otherwise.
     239           28 :       iq = MODULO(bs_env%para_env%mepos - MERGE(1, 0, distributed), n_rank) + 1
     240          159 :       n_q_local = COUNT(q_slot(iq:n_q:n_rank) > 0)
     241          140 :       ALLOCATE (Z_lP_q(n_grid, n_RI_solve, MERGE(n_q_local, n_q, distributed)))
     242              :       ! Only ranks solving a q point need a complete D(q).
     243           28 :       IF (n_q_local > 0) THEN
     244           84 :          ALLOCATE (D_llp_q(n_grid, n_grid))
     245              :       ELSE
     246            7 :          ALLOCATE (D_llp_q(0, 0))
     247              :       END IF
     248              :       ! D(q) of up to n_sub q points at a time, bounded to 64 MiB unless one q is larger.
     249              :       n_sub = INT(MAX(1_int_8, MIN(INT(bs_env%para_env%num_pe, int_8), q_batch_elements/ &
     250           28 :                                    MAX(1_int_8, SIZE(D_llp_R, 1, KIND=int_8)*n_grid))))
     251              :       ALLOCATE (D_llp_q_local(n_grid, last_l - first_l + 1), &
     252              :                 D_llp_q_batch(last_l - first_l + 1, n_grid, n_sub), &
     253              :                 recvcounts(bs_env%para_env%num_pe), displs(bs_env%para_env%num_pe), &
     254          448 :                 d_lP_q(n_grid, n_RI_solve), q_batch(n_rank))
     255           84 :       DO i_rank = 0, bs_env%para_env%num_pe - 1
     256           56 :          CALL get_local_l_range(n_grid, bs_env%para_env%num_pe, i_rank, first_l_rank, last_l_rank)
     257           56 :          recvcounts(i_rank + 1) = (last_l_rank - first_l_rank + 1)*n_grid
     258          112 :          displs(i_rank + 1) = SUM(recvcounts(1:i_rank))
     259              :       END DO
     260          140 :       ALLOCATE (SCALE(n_grid), active_grid_point(n_grid))
     261           28 :       Z_lP_q = z_zero
     262              : 
     263           28 :       progress_start = m_walltime()
     264           28 :       next_progress = 1
     265           28 :       n_done = 0_int_8
     266           28 :       n_local = INT(n_q_local, int_8)
     267          168 :       DO i_batch = 0, (n_q - 1)/n_rank
     268          140 :          n_active = 0
     269          402 :          DO iq_batch = i_batch*n_rank + 1, MIN(n_q, (i_batch + 1)*n_rank)
     270          262 :             IF (q_slot(iq_batch) == 0) CYCLE
     271          140 :             n_active = n_active + 1
     272          402 :             q_batch(n_active) = iq_batch
     273              :          END DO
     274          140 :          IF (n_active == 0) CYCLE
     275              :          ! D_ll'(q) = sum_A exp(i q.A) D_ll'^A; gather only retained q points.
     276          156 :          DO first_q = 1, n_active, n_sub
     277           78 :             last_q = MIN(first_q + n_sub - 1, n_active)
     278              :             CALL cell_to_k_transform(D_llp_R, cells_R_D_llp, -qpoints(:, q_batch(first_q:last_q)), &
     279              :                                      bs_env%kpoints_scf_desymm%nkp_grid, &
     280              :                                      D_llp_q_batch(:, :, 1:last_q - first_q + 1), backend, &
     281          638 :                                      profile_fft=fft_profile_enabled, profile_label='RI-RS D_R -> D_q')
     282          296 :             DO iq_batch = first_q, last_q
     283        77900 :                D_llp_q_local(:, :) = TRANSPOSE(D_llp_q_batch(:, :, iq_batch - first_q + 1))
     284          140 :                i_rank = MODULO(q_batch(iq_batch) - 1 + MERGE(1, 0, distributed), n_rank)
     285          218 :                IF (bs_env%para_env%mepos == i_rank) THEN
     286           70 :                   CALL bs_env%para_env%gatherv(D_llp_q_local, D_llp_q, recvcounts, displs, i_rank)
     287              :                ELSE
     288           70 :                   CALL bs_env%para_env%gatherv(D_llp_q_local, empty_matrix, recvcounts, displs, i_rank)
     289              :                END IF
     290              :             END DO
     291              :          END DO
     292           78 :          IF (PRESENT(charge_lq)) THEN
     293              :             CALL fourier_transform_d_lP_R_to_q(bs_env, cells_R_d_lP, d_lP_R, qpoints, i_batch, &
     294           14 :                                                distributed, q_slot, d_lP_q(:, 2:), iq)
     295              :          ELSE
     296              :             CALL fourier_transform_d_lP_R_to_q(bs_env, cells_R_d_lP, d_lP_R, qpoints, i_batch, &
     297           64 :                                                distributed, q_slot, d_lP_q, iq)
     298              :          END IF
     299           78 :          IF (iq > n_q) CYCLE
     300           71 :          iq_store = q_slot(iq)
     301           71 :          IF (iq_store == 0) CYCLE
     302           70 :          IF (PRESENT(charge_lq)) THEN
     303              :             ! The Gamma-correction scalar head is separate. Keep the full Gaussian RI.
     304            7 :             volume = bs_env%ri_rs%cell%deth
     305           24 :             IF (ALL(bs_env%periodic == [1, 1, 0])) THEN
     306              :                ! The separated 2D charge channel is normalized by layer area,
     307              :                ! independent of the vacuum-cell height.
     308              :                volume = NORM2([bs_env%hmat(2, 1)*bs_env%hmat(3, 2) - &
     309              :                                bs_env%hmat(3, 1)*bs_env%hmat(2, 2), &
     310              :                                bs_env%hmat(3, 1)*bs_env%hmat(1, 2) - &
     311              :                                bs_env%hmat(1, 1)*bs_env%hmat(3, 2), &
     312              :                                bs_env%hmat(1, 1)*bs_env%hmat(2, 2) - &
     313           12 :                                bs_env%hmat(2, 1)*bs_env%hmat(1, 2)])
     314              :             END IF
     315            7 :             CPASSERT(volume > 0.0_dp)
     316            7 :             metric_head = pi/bs_env%ri_metric%omega**2
     317          169 :             d_lP_q(:, 1) = metric_head/SQRT(volume)*charge_lq(:, iq)
     318              :          END IF
     319              : 
     320         2230 :          DO lp = 1, n_grid
     321        41110 :             DO l = 1, lp
     322        38880 :                phase = 0.5_dp*(D_llp_q(l, lp) + CONJG(D_llp_q(lp, l)))
     323        38880 :                D_llp_q(l, lp) = phase
     324        41040 :                D_llp_q(lp, l) = CONJG(phase)
     325              :             END DO
     326              :          END DO
     327              : 
     328         2230 :          DO l = 1, n_grid
     329         2230 :             SCALE(l) = SQRT(MAX(REAL(D_llp_q(l, l), KIND=dp), TINY(1.0_dp)))
     330              :          END DO
     331         2230 :          max_scale = MAXVAL(scale)
     332              :          active_grid_point(:) = scale > &
     333         2230 :                                 SQRT(MAX(bs_env%eps_filter, EPSILON(1.0_dp)))*max_scale
     334           70 :          IF (.NOT. ANY(active_grid_point)) THEN
     335            0 :             CPABORT("Periodic RI-RS D(q) has no active grid points")
     336              :          END IF
     337              : 
     338              :          ! Form D'_ll'(q) = D_ll'(q)/(s_l s_l') + eta delta_ll'.
     339         2230 :          DO lp = 1, n_grid
     340        77830 :             DO l = 1, n_grid
     341        77760 :                IF (active_grid_point(l) .AND. active_grid_point(lp)) THEN
     342        75216 :                   D_llp_q(l, lp) = D_llp_q(l, lp)/(SCALE(l)*SCALE(lp))
     343              :                ELSE
     344          384 :                   D_llp_q(l, lp) = z_zero
     345              :                END IF
     346              :             END DO
     347              :          END DO
     348         2230 :          DO l = 1, n_grid
     349         2160 :             D_llp_q(l, l) = D_llp_q(l, l) + bs_env%ri_rs%tikhonov*z_one
     350         2230 :             IF (active_grid_point(l)) THEN
     351        27006 :                d_lP_q(l, :) = d_lP_q(l, :)/SCALE(l)
     352              :             ELSE
     353          156 :                d_lP_q(l, :) = z_zero
     354           48 :                SCALE(l) = 1.0_dp
     355              :             END IF
     356              :          END DO
     357              : 
     358           70 :          IF (bs_env%ri_rs%tikhonov <= 0.0_dp) THEN
     359            0 :             CALL solve_unregularized_D_llp(D_llp_q, d_lP_q)
     360              :          ELSE
     361           70 :             CALL zpotrf('L', n_grid, D_llp_q, n_grid, info)
     362           70 :             IF (info == 0) CALL zpotrs('L', n_grid, n_RI_solve, D_llp_q, n_grid, d_lP_q, n_grid, info)
     363           70 :             IF (info /= 0) CPABORT("Periodic RI-RS regularized solve failed")
     364              :          END IF
     365              : 
     366         2230 :          DO l = 1, n_grid
     367        27232 :             Z_lP_q(l, :, iq_store) = d_lP_q(l, :)/SCALE(l)
     368              :          END DO
     369           98 :          IF (bs_env%para_env%mepos == 0 .AND. n_local > 1) THEN
     370           29 :             n_done = n_done + 1_int_8
     371              :             CALL write_ri_rs_progress(bs_env%unit_nr, 'Z_lP(q) q-point', &
     372           29 :                                       n_done, n_local, next_progress, progress_start)
     373              :          END IF
     374              :       END DO
     375           28 :       IF (.NOT. distributed) CALL bs_env%para_env%sum(Z_lP_q)
     376           28 :       DEALLOCATE (D_llp_q, D_llp_q_batch, D_llp_q_local, recvcounts, displs, d_lP_q, scale, active_grid_point)
     377              : 
     378           28 :       CALL timestop(handle)
     379              : 
     380           56 :    END SUBROUTINE solve_for_Z_lP_q
     381              : 
     382              : ! **************************************************************************************************
     383              : !> \brief Solve a possibly rank-deficient Hermitian D matrix without Tikhonov regularization.
     384              : !> Only eigenvalues indistinguishable from zero at matrix-roundoff precision
     385              : !> are excluded from the Moore-Penrose inverse.
     386              : !> \param D_llp_q ...
     387              : !> \param rhs ...
     388              : ! **************************************************************************************************
     389            0 :    SUBROUTINE solve_unregularized_D_llp(D_llp_q, rhs)
     390              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(INOUT)   :: D_llp_q, rhs
     391              : 
     392            0 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: projected_rhs(:, :), work(:)
     393              :       INTEGER                                            :: ieigenvalue, info, ngrid
     394              :       REAL(KIND=dp)                                      :: rank_tolerance
     395            0 :       REAL(KIND=dp), ALLOCATABLE                         :: eigenvalues(:), rwork(:)
     396              : 
     397            0 :       ngrid = SIZE(D_llp_q, 1)
     398            0 :       CPASSERT(ngrid > 0)
     399            0 :       CPASSERT(SIZE(D_llp_q, 2) == ngrid)
     400            0 :       CPASSERT(SIZE(rhs, 1) == ngrid)
     401              :       ALLOCATE (eigenvalues(ngrid), rwork(MAX(1, 3*ngrid - 2)), &
     402            0 :                 work(MAX(1, 2*ngrid - 1)), projected_rhs(ngrid, SIZE(rhs, 2)))
     403            0 :       CALL zheev('V', 'L', ngrid, D_llp_q, ngrid, eigenvalues, work, SIZE(work), rwork, info)
     404            0 :       IF (info /= 0) CPABORT('Unregularized periodic RI-RS eigensolver failed')
     405            0 :       rank_tolerance = EPSILON(1.0_dp)*REAL(ngrid, dp)*MAXVAL(ABS(eigenvalues))
     406            0 :       IF (.NOT. ANY(eigenvalues > rank_tolerance)) THEN
     407            0 :          CPABORT('Unregularized RI-RS D matrix has zero numerical rank')
     408              :       END IF
     409              : 
     410              :       CALL zgemm('C', 'N', ngrid, SIZE(rhs, 2), ngrid, z_one, D_llp_q, ngrid, &
     411            0 :                  rhs, ngrid, z_zero, projected_rhs, ngrid)
     412            0 :       DO ieigenvalue = 1, ngrid
     413            0 :          IF (eigenvalues(ieigenvalue) > rank_tolerance) THEN
     414              :             projected_rhs(ieigenvalue, :) = &
     415            0 :                projected_rhs(ieigenvalue, :)/eigenvalues(ieigenvalue)
     416              :          ELSE
     417            0 :             projected_rhs(ieigenvalue, :) = z_zero
     418              :          END IF
     419              :       END DO
     420              :       CALL zgemm('N', 'N', ngrid, SIZE(rhs, 2), ngrid, z_one, D_llp_q, ngrid, &
     421            0 :                  projected_rhs, ngrid, z_zero, rhs, ngrid)
     422              : 
     423            0 :    END SUBROUTINE solve_unregularized_D_llp
     424              : 
     425              : ! **************************************************************************************************
     426              : !> \brief Enumerate translations T that survive the R_cut^(lP) criterion for at least one (l,P).
     427              : !> \param bs_env ...
     428              : !> \param grid_points ...
     429              : !> \param cells_R_d_lP ...
     430              : ! **************************************************************************************************
     431           14 :    SUBROUTINE build_cells_R_for_d_lP_R(bs_env, grid_points, cells_R_d_lP)
     432              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     433              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(IN)         :: grid_points
     434              :       INTEGER, ALLOCATABLE, INTENT(OUT)                  :: cells_R_d_lP(:, :)
     435              : 
     436              :       INTEGER                                            :: cell_index(3), i_atom, i_l, ix, iy, iz, &
     437              :                                                             n_max(3), n_RI, n_work
     438           14 :       INTEGER, ALLOCATABLE                               :: cells_work(:, :)
     439              :       LOGICAL                                            :: keep
     440              :       REAL(KIND=dp)                                      :: center(3), cutoff, max_cutoff
     441           14 :       REAL(KIND=dp), ALLOCATABLE                         :: automatic_cutoffs(:)
     442           14 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     443              : 
     444           14 :       particle_set => bs_env%ri_rs%particle_set
     445           14 :       CALL compute_d_lP_cutoffs(bs_env, automatic_cutoffs)
     446           42 :       max_cutoff = MAXVAL(automatic_cutoffs)
     447           56 :       DO ix = 1, 3
     448           56 :          IF (bs_env%periodic(ix) == 1) THEN
     449              :             ! If |h*T + delta| <= R, then every fractional component obeys
     450              :             ! |T_i| <= R*|(h^-1)_i| + |(h^-1 delta)_i|.  Both the atom and the
     451              :             ! grid point are mapped into the reference cell, so the last term
     452              :             ! is smaller than one.  This reciprocal-vector bound remains safe
     453              :             ! for skewed cells, unlike a bound based on direct-vector lengths.
     454          144 :             n_max(ix) = CEILING(max_cutoff*NORM2(bs_env%ri_rs%cell%h_inv(ix, :))) + 1
     455              :          ELSE
     456            6 :             n_max(ix) = 0
     457              :          END IF
     458              :       END DO
     459              : 
     460           56 :       n_work = PRODUCT(2*n_max + 1)
     461           42 :       ALLOCATE (cells_work(3, n_work))
     462           14 :       n_RI = 0
     463          192 :       DO ix = -n_max(1), n_max(1)
     464         2510 :          DO iy = -n_max(2), n_max(2)
     465        14494 :             DO iz = -n_max(3), n_max(3)
     466        47992 :                cell_index = [ix, iy, iz]
     467        11998 :                keep = .FALSE.
     468        29162 :                DO i_atom = 1, bs_env%n_atom
     469        20618 :                   cutoff = automatic_cutoffs(i_atom)
     470              :                   center = pbc(particle_set(i_atom)%r, bs_env%ri_rs%cell) + &
     471       412360 :                            MATMUL(bs_env%hmat, REAL(cell_index, dp))
     472       597190 :                   DO i_l = 1, bs_env%ri_rs%n_grid_points
     473      2337268 :                      IF (NORM2(center - grid_points(:, i_l)) <= cutoff) THEN
     474              :                         keep = .TRUE.
     475              :                         EXIT
     476              :                      END IF
     477              :                   END DO
     478        29162 :                   IF (keep) EXIT
     479              :                END DO
     480        11998 :                IF (.NOT. keep) CYCLE
     481         3454 :                n_RI = n_RI + 1
     482        16134 :                cells_work(:, n_RI) = cell_index
     483              :             END DO
     484              :          END DO
     485              :       END DO
     486              : 
     487           42 :       ALLOCATE (cells_R_d_lP(3, n_RI))
     488        13830 :       cells_R_d_lP(:, :) = cells_work(:, 1:n_RI)
     489           14 :       IF (bs_env%unit_nr > 0) THEN
     490              :          WRITE (bs_env%unit_nr, '(T2,A,T69,F10.1,A)') &
     491            7 :             'Periodic RI-RS automatic R_cut^(lP):', &
     492           14 :             max_cutoff*angstrom, ' Å'
     493              :       END IF
     494           14 :       DEALLOCATE (automatic_cutoffs, cells_work)
     495              : 
     496           14 :    END SUBROUTINE build_cells_R_for_d_lP_R
     497              : 
     498              : ! **************************************************************************************************
     499              : !> \brief Build the AO-product D matrix D_ll'^A on its active translations A.
     500              : !>
     501              : !> Unlike the right-hand side d_lP^T, D_ll'^A is independent of R_cut^(lP). A translation can
     502              : !> contribute only if it is the difference of two image cells with nonzero AO values on the
     503              : !> reference-cell grid. Keeping a separate cell set prevents Eq. (10) screening from truncating
     504              : !> the least-squares metric and avoids enumerating empty three-center image cells.
     505              : !> \param bs_env ...
     506              : !> \param grid_points Reference-cell RI-RS points.
     507              : !> \param first_l First grid point owned by this rank.
     508              : !> \param last_l Last grid point owned by this rank.
     509              : !> \param cells_R_D_llp ...
     510              : !> \param D_llp_R Local grid rows of D for each translation.
     511              : ! **************************************************************************************************
     512           14 :    SUBROUTINE build_D_llp_R(bs_env, grid_points, first_l, last_l, cells_R_D_llp, D_llp_R)
     513              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     514              :       REAL(dp), INTENT(IN)                               :: grid_points(:, :)
     515              :       INTEGER, INTENT(IN)                                :: first_l, last_l
     516              :       INTEGER, ALLOCATABLE, INTENT(OUT)                  :: cells_R_D_llp(:, :)
     517              :       REAL(dp), ALLOCATABLE, INTENT(OUT)                 :: D_llp_R(:, :, :)
     518              : 
     519              :       INTEGER                                            :: cell_index(3), extent(3), i_R, &
     520              :                                                             i_R_prime, n_active, n_D_cells
     521           14 :       INTEGER, ALLOCATABLE                               :: cell_to_index(:, :, :), cells_work(:, :)
     522           14 :       REAL(dp), ALLOCATABLE                              :: phi(:, :)
     523           14 :       TYPE(ao_cell_type), ALLOCATABLE                    :: active_cells(:)
     524              : 
     525         4392 :       ALLOCATE (active_cells(SIZE(bs_env%index_to_cell_3c, 2)))
     526           56 :       ALLOCATE (phi(bs_env%ri_rs%n_grid_points, bs_env%n_ao))
     527           14 :       n_active = 0
     528              :       ! Keep the owned rows from the full-grid activity test for the contractions below.
     529         4364 :       DO i_R = 1, SIZE(bs_env%index_to_cell_3c, 2)
     530         4350 :          CALL evaluate_mu_rl_R_values(bs_env, grid_points, i_R, phi)
     531       798086 :          IF (.NOT. ANY(ABS(phi) > 0.0_dp)) CYCLE
     532          146 :          n_active = n_active + 1
     533          146 :          active_cells(n_active)%index = i_R
     534        12790 :          ALLOCATE (active_cells(n_active)%phi_local, SOURCE=phi(first_l:last_l, :))
     535              :       END DO
     536           14 :       DEALLOCATE (phi)
     537           14 :       CPASSERT(n_active > 0)
     538              : 
     539              :       extent = MAXVAL(bs_env%index_to_cell_3c(:, active_cells(:n_active)%index), DIM=2) - &
     540         1558 :                MINVAL(bs_env%index_to_cell_3c(:, active_cells(:n_active)%index), DIM=2)
     541           70 :       ALLOCATE (cell_to_index(-extent(1):extent(1), -extent(2):extent(2), -extent(3):extent(3)), SOURCE=0)
     542           84 :       ALLOCATE (cells_work(3, MIN(SIZE(cell_to_index, KIND=int_8), INT(n_active, int_8)**2)))
     543           14 :       n_D_cells = 0
     544          160 :       DO i_R = 1, n_active
     545         1806 :          DO i_R_prime = 1, n_active
     546              :             cell_index = bs_env%index_to_cell_3c(:, active_cells(i_R)%index) - &
     547         6584 :                          bs_env%index_to_cell_3c(:, active_cells(i_R_prime)%index)
     548         1646 :             IF (cell_to_index(cell_index(1), cell_index(2), cell_index(3)) /= 0) CYCLE
     549          586 :             n_D_cells = n_D_cells + 1
     550          586 :             cell_to_index(cell_index(1), cell_index(2), cell_index(3)) = n_D_cells
     551         2490 :             cells_work(:, n_D_cells) = cell_index
     552              :          END DO
     553              :       END DO
     554              : 
     555           42 :       ALLOCATE (cells_R_D_llp(3, n_D_cells))
     556         2358 :       cells_R_D_llp(:, :) = cells_work(:, 1:n_D_cells)
     557           14 :       DEALLOCATE (cells_work)
     558           14 :       IF (bs_env%unit_nr > 0) THEN
     559            7 :          WRITE (bs_env%unit_nr, '(T2,A,T72,I9)') "RI-RS D_ll'^R cells R:", n_D_cells
     560            7 :          FLUSH (bs_env%unit_nr)
     561              :       END IF
     562           70 :       ALLOCATE (D_llp_R(last_l - first_l + 1, SIZE(grid_points, 2), n_D_cells))
     563           14 :       CALL compute_D_llp(bs_env, grid_points, cell_to_index, active_cells(:n_active), D_llp_R)
     564              : 
     565         4364 :    END SUBROUTINE build_D_llp_R
     566              : 
     567              : ! **************************************************************************************************
     568              : !> \brief Construct D_ll'^R = [Σ_{S-S'=R,μ} ϕ_μ^S(r_L) ϕ_μ^S'(r_L')]².
     569              : !> The progress count enumerates pairs of active cell images (S,S'), not Gaussian pairs.
     570              : !> \param bs_env ...
     571              : !> \param grid_points Reference-cell RI-RS points.
     572              : !> \param cell_to_index Index of each active translation in D_llp_R.
     573              : !> \param active_cells Active image cells and their local AO rows.
     574              : !> \param D_llp_R ...
     575              : ! **************************************************************************************************
     576           14 :    SUBROUTINE compute_D_llp(bs_env, grid_points, cell_to_index, active_cells, D_llp_R)
     577              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     578              :       REAL(dp), INTENT(IN)                               :: grid_points(:, :)
     579              :       INTEGER, ALLOCATABLE, INTENT(IN)                   :: cell_to_index(:, :, :)
     580              :       TYPE(ao_cell_type), INTENT(IN)                     :: active_cells(:)
     581              :       REAL(KIND=dp), CONTIGUOUS, INTENT(OUT)             :: D_llp_R(:, :, :)
     582              : 
     583              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'compute_D_llp'
     584              : 
     585              :       INTEGER                                            :: cell_A(3), handle, i_A, i_active, &
     586              :                                                             i_active_prime, i_R, i_R_prime, &
     587              :                                                             next_progress, nrows
     588              :       INTEGER(KIND=int_8)                                :: n_done, n_total
     589              :       REAL(KIND=dp)                                      :: progress_start
     590           14 :       REAL(KIND=dp), ALLOCATABLE                         :: phi_full(:, :)
     591              : 
     592           14 :       CALL timeset(routineN, handle)
     593              : 
     594           14 :       progress_start = m_walltime()
     595           14 :       next_progress = 1
     596           14 :       n_done = 0_int_8
     597           14 :       n_total = INT(SIZE(active_cells), int_8)**2
     598              :       ! Sum the AO images for each owned (l,l') rectangle before squaring.
     599       326184 :       D_llp_R = 0.0_dp
     600           14 :       nrows = SIZE(D_llp_R, 1)
     601           14 :       IF (nrows > 0) THEN
     602           56 :          ALLOCATE (phi_full(bs_env%ri_rs%n_grid_points, bs_env%n_ao))
     603          160 :          DO i_active_prime = 1, SIZE(active_cells)
     604          146 :             i_R_prime = active_cells(i_active_prime)%index
     605          146 :             IF (nrows == SIZE(grid_points, 2)) THEN
     606            0 :                phi_full(:, :) = active_cells(i_active_prime)%phi_local
     607              :             ELSE
     608          146 :                CALL evaluate_mu_rl_R_values(bs_env, grid_points, i_R_prime, phi_full)
     609              :             END IF
     610         1806 :             DO i_active = 1, SIZE(active_cells)
     611         1646 :                i_R = active_cells(i_active)%index
     612         1646 :                IF (n_done > 0) CALL write_ri_rs_progress(bs_env%unit_nr, "D_ll'^R Cell-image pairs", &
     613         1632 :                                                          n_done, n_total, next_progress, progress_start)
     614         1646 :                n_done = n_done + 1
     615              :                cell_A = bs_env%index_to_cell_3c(:, i_R) - &
     616         6584 :                         bs_env%index_to_cell_3c(:, i_R_prime)
     617         1646 :                i_A = cell_to_index(cell_A(1), cell_A(2), cell_A(3))
     618              :                CALL dgemm('N', 'T', nrows, bs_env%ri_rs%n_grid_points, bs_env%n_ao, &
     619              :                           1.0_dp, active_cells(i_active)%phi_local, MAX(1, nrows), phi_full, &
     620              :                           MAX(1, bs_env%ri_rs%n_grid_points), 1.0_dp, D_llp_R(:, :, i_A), &
     621         1792 :                           MAX(1, nrows))
     622              :             END DO
     623              :          END DO
     624              :          CALL write_ri_rs_progress(bs_env%unit_nr, "D_ll'^R Cell-image pairs", &
     625           14 :                                    n_done, n_total, next_progress, progress_start)
     626           14 :          DEALLOCATE (phi_full)
     627              :       END IF
     628          600 :       DO i_A = 1, SIZE(D_llp_R, 3)
     629       326184 :          D_llp_R(:, :, i_A) = D_llp_R(:, :, i_A)*D_llp_R(:, :, i_A)
     630              :       END DO
     631              : 
     632           14 :       CALL timestop(handle)
     633              : 
     634           14 :    END SUBROUTINE compute_D_llp
     635              : 
     636              : ! **************************************************************************************************
     637              : !> \brief Fourier-transform d_lP(R) to d_lP(q) and gather l rows on each q owner.
     638              : !> d_lP(q)=Σ_T exp(iqT)d_lP(T); two send buffers overlap transfer with the next q-point transform.
     639              : !> Each rank holds all P and T for its l interval; each destination receives one complete q.
     640              : !> \param bs_env ...
     641              : !> \param cells_R_d_lP ...
     642              : !> \param d_lP_R ...
     643              : !> \param qpoints ...
     644              : !> \param i_batch ...
     645              : !> \param distributed ...
     646              : !> \param q_slot compact local indices; zero omits a q point
     647              : !> \param d_lP_q ...
     648              : !> \param iq ...
     649              : ! **************************************************************************************************
     650          312 :    SUBROUTINE fourier_transform_d_lP_R_to_q(bs_env, cells_R_d_lP, d_lP_R, qpoints, i_batch, distributed, &
     651           78 :                                             q_slot, d_lP_q, iq)
     652              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     653              :       INTEGER, INTENT(IN)                                :: cells_R_d_lP(:, :)
     654              :       REAL(dp), CONTIGUOUS, INTENT(IN)                   :: d_lP_R(:, :, :)
     655              :       REAL(dp), INTENT(IN)                               :: qpoints(:, :)
     656              :       INTEGER, INTENT(IN)                                :: i_batch
     657              :       LOGICAL, INTENT(IN)                                :: distributed
     658              :       INTEGER, INTENT(IN)                                :: q_slot(:)
     659              :       COMPLEX(dp), ASYNCHRONOUS, CONTIGUOUS, INTENT(OUT) :: d_lP_q(:, :)
     660              :       INTEGER, INTENT(OUT)                               :: iq
     661              : 
     662              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'fourier_transform_d_lP_R_to_q'
     663              : 
     664           78 :       COMPLEX(dp), ALLOCATABLE                           :: result_q(:, :, :)
     665           78 :       COMPLEX(dp), ALLOCATABLE, ASYNCHRONOUS             :: receive_q(:), send_q(:, :, :)
     666              :       INTEGER :: backend, first_l, first_q, handle, i_dest, i_l, i_P, i_q, i_rank, i_slot, last_l, &
     667              :          last_q, local_first_l, local_last_l, n_active_q, n_l, n_P, n_ranks, n_recv, n_sub, &
     668              :          position, q_column, q_offset
     669           78 :       INTEGER, ALLOCATABLE                               :: q_dest(:), q_indices(:)
     670              :       LOGICAL                                            :: receiving, sending(2)
     671          234 :       TYPE(mp_request_type)                              :: send_requests(2)
     672           78 :       TYPE(mp_request_type), ALLOCATABLE                 :: recv_requests(:)
     673              : 
     674           78 :       CALL timeset(routineN, handle)
     675              : 
     676           78 :       n_ranks = bs_env%para_env%num_pe
     677           78 :       backend = MERGE(bs_env%kpoints_scf_desymm%rskp_transform, rskp_backend_gemm, distributed)
     678           78 :       i_rank = bs_env%para_env%mepos
     679           78 :       q_offset = 0
     680           78 :       IF (distributed) q_offset = 1
     681           78 :       iq = i_batch*n_ranks + MODULO(i_rank - q_offset, n_ranks) + 1
     682           78 :       CALL get_local_l_range(bs_env%ri_rs%n_grid_points, n_ranks, i_rank, local_first_l, local_last_l)
     683           78 :       CPASSERT(SIZE(d_lP_R, 1) == local_last_l - local_first_l + 1)
     684           78 :       CPASSERT(SIZE(d_lP_R, 2) == SIZE(d_lP_q, 2))
     685          234 :       CPASSERT(SIZE(d_lP_q, KIND=int_8) <= INT(HUGE(0), int_8))
     686           78 :       n_P = bs_env%n_RI
     687          702 :       ALLOCATE (recv_requests(n_ranks), send_q(SIZE(d_lP_R, 1), n_P, 2))
     688        27386 :       d_lP_q = z_zero
     689           78 :       n_recv = 0
     690           78 :       receiving = iq <= SIZE(qpoints, 2)
     691           78 :       IF (receiving) receiving = q_slot(iq) > 0
     692           78 :       IF (receiving) THEN
     693              :          ! Packed rank blocks: offset=(first_l-1)*n_P, followed by local (l,P) order.
     694              :          ! Receive into contiguous storage before assigning the noncontiguous output rows.
     695          350 :          ALLOCATE (receive_q(SIZE(d_lP_q)))
     696          210 :          DO i_dest = 0, n_ranks - 1
     697          140 :             IF (i_dest == i_rank) CYCLE
     698           70 :             CALL get_local_l_range(bs_env%ri_rs%n_grid_points, n_ranks, i_dest, first_l, last_l)
     699           70 :             IF (last_l < first_l .OR. n_P == 0) CYCLE
     700           70 :             n_recv = n_recv + 1
     701              :             CALL bs_env%para_env%irecv(receive_q((first_l - 1)*n_P + 1:last_l*n_P), &
     702          280 :                                        i_dest, recv_requests(n_recv), tag=29571)
     703              :          END DO
     704              :       END IF
     705           78 :       n_active_q = 0
     706          312 :       ALLOCATE (q_indices(n_ranks), q_dest(n_ranks))
     707          234 :       DO i_dest = 0, n_ranks - 1
     708          156 :          i_q = i_batch*n_ranks + MODULO(i_dest - q_offset, n_ranks) + 1
     709          156 :          IF (i_q > SIZE(qpoints, 2)) CYCLE
     710          142 :          IF (q_slot(i_q) == 0) CYCLE
     711          140 :          n_active_q = n_active_q + 1
     712          140 :          q_indices(n_active_q) = i_q
     713          220 :          q_dest(n_active_q) = i_dest
     714              :       END DO
     715           78 :       sending = .FALSE.
     716           78 :       IF (SIZE(d_lP_R, 1) > 0 .AND. n_P > 0) THEN
     717              :          ! Sums of up to n_sub q points at a time, bounded to 64 MiB unless one q is larger.
     718              :          n_sub = INT(MAX(1_int_8, MIN(INT(n_active_q, int_8), q_batch_elements/ &
     719           78 :                                       (SIZE(d_lP_R, 1, KIND=int_8)*n_P))))
     720          390 :          ALLOCATE (result_q(SIZE(d_lP_R, 1), n_P, n_sub))
     721          156 :          DO first_q = 1, n_active_q, n_sub
     722           78 :             last_q = MIN(first_q + n_sub - 1, n_active_q)
     723              :             CALL cell_to_k_transform(d_lP_R, cells_R_d_lP, qpoints(:, q_indices(first_q:last_q)), &
     724              :                                      bs_env%kpoints_scf_desymm%nkp_grid, &
     725              :                                      result_q(:, :, 1:last_q - first_q + 1), backend, &
     726          638 :                                      profile_fft=fft_profile_enabled, profile_label='RI-RS d_lP_R -> d_lP_q')
     727          296 :             DO q_column = first_q, last_q
     728          140 :                i_dest = q_dest(q_column)
     729          140 :                i_slot = MODULO(i_dest, 2) + 1
     730          140 :                IF (sending(i_slot)) CALL mp_waitall(send_requests(i_slot:i_slot))
     731          140 :                sending(i_slot) = .FALSE.
     732        26400 :                send_q(:, :, i_slot) = result_q(:, :, q_column - first_q + 1)
     733          218 :                IF (i_dest == i_rank) THEN
     734        13200 :                   d_lP_q(local_first_l:local_last_l, :) = send_q(:, :, i_slot)
     735              :                ELSE
     736           70 :                   CALL bs_env%para_env%isend(send_q(:, :, i_slot), i_dest, send_requests(i_slot), tag=29571)
     737           70 :                   sending(i_slot) = .TRUE.
     738              :                END IF
     739              :             END DO
     740              :          END DO
     741           78 :          DEALLOCATE (result_q)
     742              :       END IF
     743          234 :       DO i_slot = 1, 2
     744          234 :          IF (sending(i_slot)) CALL mp_waitall(send_requests(i_slot:i_slot))
     745              :       END DO
     746           78 :       IF (n_recv > 0) CALL mp_waitall(recv_requests(:n_recv))
     747           78 :       IF (receiving) THEN
     748          210 :          DO i_dest = 0, n_ranks - 1
     749          140 :             IF (i_dest == i_rank) CYCLE
     750           70 :             CALL get_local_l_range(bs_env%ri_rs%n_grid_points, n_ranks, i_dest, first_l, last_l)
     751           70 :             n_l = last_l - first_l + 1
     752          850 :             DO i_P = 1, n_P
     753          710 :                position = (first_l - 1)*n_P + (i_P - 1)*n_l
     754        13270 :                DO i_l = 1, n_l
     755        13130 :                   d_lP_q(first_l + i_l - 1, i_P) = receive_q(position + i_l)
     756              :                END DO
     757              :             END DO
     758              :          END DO
     759           70 :          DEALLOCATE (receive_q)
     760              :       END IF
     761           78 :       DEALLOCATE (send_q, recv_requests)
     762              : 
     763           78 :       CALL timestop(handle)
     764              : 
     765          234 :    END SUBROUTINE fourier_transform_d_lP_R_to_q
     766              : 
     767            0 : END MODULE ri_rs_kp_fitting
        

Generated by: LCOV version 2.0-1