LCOV - code coverage report
Current view: top level - src - ri_rs_grid_initialization.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 91.0 % 266 242
Test Date: 2026-11-24 01:22:08 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 Initialize atom-owned molecular or periodic RI-RS grids by Cholesky selection.
      10              : ! **************************************************************************************************
      11              : MODULE ri_rs_grid_initialization
      12              :    USE basis_set_types,                 ONLY: gto_basis_set_type
      13              :    USE input_constants,                 ONLY: do_gapw_log,&
      14              :                                               ri_rs_small_cell_full_kp
      15              :    USE kinds,                           ONLY: default_string_length,&
      16              :                                               dp,&
      17              :                                               int_8
      18              :    USE lebedev,                         ONLY: get_number_of_lebedev_grid,&
      19              :                                               lebedev_grid
      20              :    USE message_passing,                 ONLY: mp_comm_type
      21              :    USE post_scf_bandstructure_types,    ONLY: post_scf_bandstructure_type
      22              :    USE qs_grid_atom,                    ONLY: allocate_grid_atom,&
      23              :                                               create_grid_atom,&
      24              :                                               deallocate_grid_atom,&
      25              :                                               grid_atom_type
      26              :    USE ri_rs_types,                     ONLY: rirs_grid_type
      27              :    USE ri_rs_utils,                     ONLY: evaluate_ao_basis_on_points,&
      28              :                                               filter_grid_to_voronoi,&
      29              :                                               get_rirs_cluster_atoms
      30              :    USE string_utilities,                ONLY: uppercase
      31              :    USE util,                            ONLY: locate
      32              : #include "./base/base_uses.f90"
      33              : 
      34              :    IMPLICIT NONE
      35              :    PRIVATE
      36              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ri_rs_grid_initialization'
      37              : 
      38              :    PUBLIC :: initialize_ri_rs_grid
      39              : 
      40              : CONTAINS
      41              : 
      42              : ! **************************************************************************************************
      43              : !> \brief Construct the initial RI-RS grid of every atom.
      44              : !>
      45              : !> For every atom B, the Lebedev grid points are
      46              : !>
      47              : !> (1)  r_l = R_B + ρ_s Ω_a,
      48              : !>
      49              : !> where ρ_s is a radial quadrature point and Ω_a is a Lebedev direction. Lebedev grids are
      50              : !> tabulated according to the angular degree L up to which they integrate exactly. To integrate a
      51              : !> three-centre integral (μν|P), the smallest available Lebedev grid satisfying
      52              : !>
      53              : !> (2)  L >= 2 l_AO,max + l_RI,max + ΔL
      54              : !>
      55              : !> is used. Here, l_AO,max is the maximum angular momentum of the atomic AO basis functions ϕ_μ,
      56              : !> l_RI,max is the maximum angular momentum of the auxiliary basis functions φ_P, and the internal
      57              : !> angular buffer is ΔL. Enough radial points ρ_s are used that the Lebedev grid of every atom B
      58              : !> contains at least
      59              : !>
      60              : !> (3)  N_initial^B >= α_initial N_AO^B,
      61              : !>
      62              : !> points, where N_AO^B is the number of atomic orbitals on atom B and
      63              : !> α_initial = max(30, 2 RS_AO_RATIO). The factor two provides candidates that can be discarded by
      64              : !> the atomic Voronoi filter. The requested initial RI-RS grid of atom A contains
      65              : !>
      66              : !> (4)  N_R^A = ceil(α * N_AO^A)
      67              : !>
      68              : !> points, with α = RS_AO_RATIO of the atomic kind (rs_ao_ratio_of_kind). Around every atom A, a
      69              : !> cluster is defined as
      70              : !>
      71              : !> (5)  C_A contains all atoms and, for periodic systems, their images within the cluster radius.
      72              : !>
      73              : !> The Cholesky selection uses all Lebedev grid points of every atom image B in C_A. The
      74              : !> Voronoi cell of atom A is
      75              : !>
      76              : !> (6)  V_A = {r_l : |r_l - R_A| <= |r_l - R_B| for every atom image B}.
      77              : !>
      78              : !> For cluster C_A, Cholesky selection is performed on
      79              : !>
      80              : !> (7)  D_ll' = [Σ_(μ in C_A) ϕ_μ(r_l) ϕ_μ(r_l')]^2.
      81              : !>
      82              : !> The first grid point is the point with the largest diagonal element,
      83              : !>
      84              : !> (8)  d_l^(0) = D_ll,                 q_1 = arg max_l d_l^(0).
      85              : !>
      86              : !> For every selected point q_k, the Cholesky column and diagonal are updated according to
      87              : !>
      88              : !> (9)  L_lk = [D_lq_k - Σ_(j<k) L_lj L_q_kj]/sqrt(d_q_k^(k-1)),
      89              : !>
      90              : !> (10) d_l^(k) = max(0, d_l^(k-1) - L_lk^2),
      91              : !>
      92              : !> and the next point q_(k+1) is the point with the largest d_l^(k). Points outside V_A take part
      93              : !> in Eqs. (7)-(10), but only selected points inside V_A are placed into the RI-RS grid of atom A.
      94              : !> If D_ll' reaches its numerical rank before N_R^A points have been retained, let S_A contain the
      95              : !> retained points. Every unused point r_l in V_A is assigned the distance
      96              : !>
      97              : !> (11) ρ_l = min_(q in S_A) |r_l - q|,
      98              : !>
      99              : !> and the point
     100              : !>
     101              : !> (12) q_new = arg max_(r_l in V_A and r_l not in S_A) ρ_l
     102              : !>
     103              : !> is appended to S_A. Equations (11)-(12) are repeated until Eq. (4) is satisfied. This maximin
     104              : !> distance is only a geometric selection criterion, not the three-centre-integral fitting error.
     105              : !> Clusters C_A are assigned to MPI groups; Cholesky rows are distributed within each group.
     106              : !> The completed atom grids are broadcast to all ranks.
     107              : !> \param bs_env Band-structure environment containing GW parameters.
     108              : ! **************************************************************************************************
     109           24 :    SUBROUTINE initialize_ri_rs_grid(bs_env)
     110              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     111              : 
     112              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'initialize_ri_rs_grid'
     113              :       INTEGER, PARAMETER :: l_additional = 2, radial_quadrature = do_gapw_log
     114              :       REAL(KIND=dp), PARAMETER :: minimum_candidate_ratio = 30.0_dp
     115              : 
     116              :       INTEGER                                            :: handle, ikind
     117              :       REAL(KIND=dp)                                      :: candidate_ratio
     118              :       TYPE(rirs_grid_type), ALLOCATABLE                  :: radial_lebedev_grids(:)
     119              : 
     120           24 :       CALL timeset(routineN, handle)
     121              : 
     122              :       ! Build the Lebedev grids of Eqs. (1)-(3) for all elements in the calculation
     123          106 :       ALLOCATE (radial_lebedev_grids(SIZE(bs_env%basis_set_AO)))
     124           58 :       DO ikind = 1, SIZE(bs_env%basis_set_AO)
     125           34 :          candidate_ratio = MAX(minimum_candidate_ratio, 2.0_dp*rs_ao_ratio_of_kind(bs_env, ikind))
     126           34 :          IF (bs_env%unit_nr > 0) THEN
     127              :             WRITE (bs_env%unit_nr, '(T2,A,T69,F12.1)') 'RS_AO_RATIO of '// &
     128              :                TRIM(bs_env%ri_rs%atomic_kind_set(ikind)%element_symbol)//' ('// &
     129           17 :                TRIM(bs_env%basis_set_AO(ikind)%gto_basis_set%name)//')', rs_ao_ratio_of_kind(bs_env, ikind)
     130              :          END IF
     131              :          CALL build_lebedev_grid(bs_env%basis_set_AO(ikind)%gto_basis_set, &
     132              :                                  bs_env%basis_set_RI(ikind)%gto_basis_set, &
     133              :                                  candidate_ratio, l_additional, radial_quadrature, &
     134           58 :                                  radial_lebedev_grids(ikind))
     135              :       END DO
     136              : 
     137              :       ! Apply Eqs. (5)-(12) for every atom and keep points inside Voronoi volume, Eq. (6)
     138           24 :       CALL cholesky_selection_and_voronoi_filtering(bs_env, radial_lebedev_grids)
     139              : 
     140           24 :       CALL broadcast_ri_rs_grids(bs_env)
     141              : 
     142           24 :       CALL timestop(handle)
     143              : 
     144           82 :    END SUBROUTINE initialize_ri_rs_grid
     145              : 
     146              : ! **************************************************************************************************
     147              : !> \brief Build one fixed-orientation Lebedev grid according to Eqs. (1)-(3).
     148              : !> \param ao ...
     149              : !> \param ri ...
     150              : !> \param ratio α_initial in Eq. (3).
     151              : !> \param l_additional ΔL in Eq. (2).
     152              : !> \param radial_quadrature ...
     153              : !> \param grid ...
     154              : ! **************************************************************************************************
     155           34 :    SUBROUTINE build_lebedev_grid(ao, ri, ratio, l_additional, radial_quadrature, grid)
     156              :       TYPE(gto_basis_set_type), POINTER                  :: ao, ri
     157              :       REAL(KIND=dp), INTENT(IN)                          :: ratio
     158              :       INTEGER, INTENT(IN)                                :: l_additional, radial_quadrature
     159              :       TYPE(rirs_grid_type), INTENT(OUT)                  :: grid
     160              : 
     161              :       INTEGER                                            :: degree, ir, nang, nrad, offset, rule
     162              :       TYPE(grid_atom_type), POINTER                      :: radial_grid
     163              : 
     164           34 :       CPASSERT(ASSOCIATED(ao) .AND. ASSOCIATED(ri))
     165          318 :       degree = 2*MAXVAL(ao%lmax) + MAXVAL(ri%lmax) + l_additional
     166           34 :       rule = get_number_of_lebedev_grid(l=degree)
     167           34 :       nang = lebedev_grid(rule)%n
     168           34 :       nrad = MAX(2, CEILING(ratio*REAL(ao%nsgf, dp)/REAL(nang, dp)))
     169              : 
     170           34 :       NULLIFY (radial_grid)
     171           34 :       CALL allocate_grid_atom(radial_grid)
     172           34 :       CALL create_grid_atom(radial_grid, nrad, nang, 0, rule, radial_quadrature)
     173           34 :       grid%npts = nrad*nang
     174          102 :       ALLOCATE (grid%raw_points(3, grid%npts))
     175          300 :       DO ir = 1, nrad
     176          266 :          offset = (ir - 1)*nang
     177              :          grid%raw_points(:, offset + 1:offset + nang) = &
     178        14428 :             radial_grid%rad(ir)*lebedev_grid(rule)%r
     179              :       END DO
     180           34 :       CALL deallocate_grid_atom(radial_grid)
     181           34 :    END SUBROUTINE build_lebedev_grid
     182              : 
     183              : ! **************************************************************************************************
     184              : !> \brief Apply Eqs. (5)-(12) independently for every atom and keep points inside Eq. (6).
     185              : !> \param bs_env ...
     186              : !> \param radial_lebedev_grids ...
     187              : ! **************************************************************************************************
     188           24 :    SUBROUTINE cholesky_selection_and_voronoi_filtering(bs_env, radial_lebedev_grids)
     189              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     190              :       TYPE(rirs_grid_type), INTENT(IN)                   :: radial_lebedev_grids(:)
     191              : 
     192              :       INTEGER                                            :: iatom, ikind, my_group, ngroups, npoints
     193              :       TYPE(mp_comm_type)                                 :: atom_env
     194              : 
     195            0 :       CPASSERT(.NOT. ALLOCATED(bs_env%ri_rs%atomic_grids))
     196          130 :       ALLOCATE (bs_env%ri_rs%atomic_grids(bs_env%n_atom))
     197           24 :       ngroups = MIN(bs_env%n_atom, bs_env%para_env%num_pe)
     198           24 :       my_group = MOD(bs_env%para_env%mepos, ngroups)
     199           24 :       CALL atom_env%from_split(bs_env%para_env, my_group)
     200           82 :       DO iatom = 1, bs_env%n_atom
     201           58 :          ikind = bs_env%ri_rs%particle_set(iatom)%atomic_kind%kind_number
     202              :          npoints = CEILING(rs_ao_ratio_of_kind(bs_env, ikind)* &
     203           58 :                            bs_env%basis_set_AO(ikind)%gto_basis_set%nsgf)
     204           58 :          bs_env%ri_rs%atomic_grids(iatom)%npts = npoints
     205          174 :          ALLOCATE (bs_env%ri_rs%atomic_grids(iatom)%raw_points(3, npoints))
     206              : 
     207           82 :          IF (MOD(iatom - 1, ngroups) == my_group) THEN
     208           29 :             CALL select_cholesky_grid_iatom(bs_env, radial_lebedev_grids, iatom, atom_env)
     209              :          END IF
     210              :       END DO
     211           24 :       CALL atom_env%free()
     212           24 :    END SUBROUTINE cholesky_selection_and_voronoi_filtering
     213              : 
     214              : ! **************************************************************************************************
     215              : !> \brief Select one atom grid with Eqs. (5)-(12) and the atomic Voronoi cell in Eq. (6).
     216              : !> \param bs_env ...
     217              : !> \param radial_lebedev_grids ...
     218              : !> \param iatom ...
     219              : !> \param atom_env ...
     220              : ! **************************************************************************************************
     221           29 :    SUBROUTINE select_cholesky_grid_iatom(bs_env, radial_lebedev_grids, iatom, atom_env)
     222              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     223              :       TYPE(rirs_grid_type), INTENT(IN)                   :: radial_lebedev_grids(:)
     224              :       INTEGER, INTENT(IN)                                :: iatom
     225              :       TYPE(mp_comm_type), INTENT(IN)                     :: atom_env
     226              : 
     227              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'select_cholesky_grid_iatom'
     228              : 
     229              :       CHARACTER(LEN=3*default_string_length)             :: failure
     230              :       INTEGER                                            :: handle, igrid_point, npoints, nretained
     231           29 :       INTEGER, ALLOCATABLE :: ao_point_indices(:), atom_n_ao(:), atom_point_offsets(:), &
     232           29 :          atom_value_offsets(:), cluster_atoms(:), cluster_cells(:, :), selected_indices(:)
     233              :       LOGICAL, ALLOCATABLE                               :: inside_voronoi(:)
     234              :       REAL(KIND=dp)                                      :: center(3)
     235           29 :       REAL(KIND=dp), ALLOCATABLE                         :: ao_values(:), cluster_centers(:, :), &
     236           29 :                                                             cluster_points(:, :)
     237              : 
     238           29 :       CALL timeset(routineN, handle)
     239              : 
     240              :       CALL get_rirs_cluster_atoms(bs_env%ri_rs%particle_set, bs_env%ri_rs%cell, iatom, &
     241              :                                   bs_env%ri_rs%grid_opt%cutoff_atomic_cluster, cluster_atoms, &
     242           29 :                                   cluster_cells, cluster_centers)
     243          116 :       center = cluster_centers(:, 1)
     244              :       CALL collect_cluster_lebedev_points(bs_env, radial_lebedev_grids, cluster_atoms, &
     245           29 :                                           cluster_centers, center, cluster_points)
     246           29 :       npoints = SIZE(cluster_points, 2)
     247           87 :       ALLOCATE (inside_voronoi(npoints))
     248              :       CALL filter_grid_to_voronoi(cluster_points, iatom, bs_env%ri_rs%particle_set, &
     249           29 :                                   bs_env%ri_rs%cell, mask=inside_voronoi)
     250         7069 :       IF (COUNT(inside_voronoi) < bs_env%ri_rs%atomic_grids(iatom)%npts) THEN
     251            0 :          WRITE (failure, '(A,I0,A,I0,A,I0,A)') 'Atom ', iatom, ': only ', COUNT(inside_voronoi), &
     252            0 :             ' Voronoi grid points for ', bs_env%ri_rs%atomic_grids(iatom)%npts, &
     253            0 :             ' points; reduce RS_AO_RATIO.'
     254            0 :          CPABORT(TRIM(failure))
     255              :       END IF
     256              : 
     257              :       CALL evaluate_cluster_ao_values(bs_env, cluster_atoms, cluster_centers, center, cluster_points, &
     258              :                                       ao_point_indices, atom_point_offsets, atom_value_offsets, &
     259           29 :                                       atom_n_ao, ao_values)
     260           87 :       ALLOCATE (selected_indices(bs_env%ri_rs%atomic_grids(iatom)%npts))
     261              :       CALL select_cholesky_grid_points(ao_point_indices, atom_point_offsets, atom_value_offsets, &
     262              :                                        atom_n_ao, ao_values, npoints, &
     263           29 :                                        inside_voronoi, selected_indices, atom_env)
     264          572 :       nretained = COUNT(selected_indices > 0)
     265           29 :       IF (nretained == 0) THEN
     266            0 :          WRITE (failure, '(A,I0,A)') 'Atom ', iatom, &
     267            0 :             ': Cholesky selection found no numerically independent point.'
     268            0 :          CPABORT(TRIM(failure))
     269           29 :       ELSE IF (nretained < SIZE(selected_indices)) THEN
     270           20 :          CALL complete_grid_by_maximin_distance(cluster_points, inside_voronoi, selected_indices)
     271              :       END IF
     272          572 :       DO igrid_point = 1, SIZE(selected_indices)
     273              :          bs_env%ri_rs%atomic_grids(iatom)%raw_points(:, igrid_point) = &
     274         2201 :             cluster_points(:, selected_indices(igrid_point))
     275              :       END DO
     276              : 
     277           29 :       CALL timestop(handle)
     278              : 
     279           58 :    END SUBROUTINE select_cholesky_grid_iatom
     280              : 
     281              : ! **************************************************************************************************
     282              : !> \brief Complete a rank-saturated atom grid with the geometric maximin rule in Eqs. (11)-(12).
     283              : !> \param cluster_points Coordinates r_l of all existing cluster-grid points.
     284              : !> \param inside_voronoi True for points inside the atomic Voronoi cell V_A.
     285              : !> \param selected_indices Cholesky indices on entry and the completed indices on exit.
     286              : ! **************************************************************************************************
     287           20 :    SUBROUTINE complete_grid_by_maximin_distance(cluster_points, inside_voronoi, selected_indices)
     288              :       REAL(KIND=dp), INTENT(IN)                          :: cluster_points(:, :)
     289              :       LOGICAL, INTENT(IN)                                :: inside_voronoi(:)
     290              :       INTEGER, INTENT(INOUT)                             :: selected_indices(:)
     291              : 
     292              :       REAL(KIND=dp), PARAMETER                           :: tie_tolerance = 1.0E-8_dp
     293              : 
     294              :       INTEGER                                            :: igrid_point, inew_point, iselected, &
     295              :                                                             nretained
     296           20 :       LOGICAL, ALLOCATABLE                               :: unselected_inside(:)
     297              :       REAL(KIND=dp)                                      :: distance_sq, largest_distance_sq
     298           20 :       REAL(KIND=dp), ALLOCATABLE                         :: nearest_distance_sq(:)
     299              : 
     300           20 :       CPASSERT(SIZE(cluster_points, 2) == SIZE(inside_voronoi))
     301         5100 :       CPASSERT(COUNT(inside_voronoi) >= SIZE(selected_indices))
     302              : 
     303          504 :       nretained = COUNT(selected_indices > 0)
     304           20 :       CPASSERT(nretained > 0)
     305              :       ALLOCATE (unselected_inside(SIZE(inside_voronoi)), &
     306          100 :                 nearest_distance_sq(SIZE(inside_voronoi)))
     307         5100 :       unselected_inside(:) = inside_voronoi
     308         5100 :       nearest_distance_sq(:) = HUGE(1.0_dp)
     309              : 
     310              :       ! Initialize ρ_l^2 in Eq. (11) from all retained Cholesky points.
     311          180 :       DO iselected = 1, nretained
     312          160 :          unselected_inside(selected_indices(iselected)) = .FALSE.
     313        50116 :          DO igrid_point = 1, SIZE(inside_voronoi)
     314        49936 :             IF (.NOT. unselected_inside(igrid_point)) CYCLE
     315              :             distance_sq = SUM((cluster_points(:, igrid_point) - &
     316        61704 :                                cluster_points(:, selected_indices(iselected)))**2)
     317              :             nearest_distance_sq(igrid_point) = &
     318        50096 :                MIN(nearest_distance_sq(igrid_point), distance_sq)
     319              :          END DO
     320              :       END DO
     321              : 
     322          344 :       DO WHILE (nretained < SIZE(selected_indices))
     323        30265 :          inew_point = 0
     324       164428 :          largest_distance_sq = MAXVAL(nearest_distance_sq, MASK=unselected_inside)
     325        48601 :          DO igrid_point = 1, SIZE(inside_voronoi)
     326        48601 :             IF (.NOT. unselected_inside(igrid_point)) CYCLE
     327              :             ! Ties up to rounding go to the first point, independent of the compiler.
     328         8451 :             IF (nearest_distance_sq(igrid_point) >= (1.0_dp - tie_tolerance)*largest_distance_sq) THEN
     329              :                inew_point = igrid_point
     330              :                EXIT
     331              :             END IF
     332              :          END DO
     333          324 :          CPASSERT(inew_point > 0)
     334              : 
     335          324 :          nretained = nretained + 1
     336          324 :          selected_indices(nretained) = inew_point
     337          324 :          unselected_inside(inew_point) = .FALSE.
     338       164428 :          DO igrid_point = 1, SIZE(inside_voronoi)
     339       164104 :             IF (.NOT. unselected_inside(igrid_point)) CYCLE
     340              :             distance_sq = SUM((cluster_points(:, igrid_point) - &
     341       189628 :                                cluster_points(:, inew_point))**2)
     342              :             nearest_distance_sq(igrid_point) = &
     343       164428 :                MIN(nearest_distance_sq(igrid_point), distance_sq)
     344              :          END DO
     345              :       END DO
     346           20 :    END SUBROUTINE complete_grid_by_maximin_distance
     347              : 
     348              : ! **************************************************************************************************
     349              : !> \brief Collect all cluster Lebedev points used in Eqs. (5) and (7).
     350              : !> \param bs_env ...
     351              : !> \param radial_lebedev_grids ...
     352              : !> \param cluster_atoms ...
     353              : !> \param cluster_centers ...
     354              : !> \param center Position of the central atom, defining atom-relative grid coordinates.
     355              : !> \param cluster_points ...
     356              : ! **************************************************************************************************
     357           29 :    SUBROUTINE collect_cluster_lebedev_points(bs_env, radial_lebedev_grids, cluster_atoms, &
     358           29 :                                              cluster_centers, center, cluster_points)
     359              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     360              :       TYPE(rirs_grid_type), INTENT(IN)                   :: radial_lebedev_grids(:)
     361              :       INTEGER, INTENT(IN)                                :: cluster_atoms(:)
     362              :       REAL(KIND=dp), INTENT(IN)                          :: cluster_centers(:, :), center(3)
     363              :       REAL(KIND=dp), ALLOCATABLE, INTENT(OUT)            :: cluster_points(:, :)
     364              : 
     365              :       INTEGER                                            :: cluster_iatom, i_cluster_atom, ikind, n, &
     366              :                                                             npoints, offset
     367              : 
     368           29 :       npoints = 0
     369          106 :       DO i_cluster_atom = 1, SIZE(cluster_atoms)
     370           77 :          ikind = bs_env%ri_rs%particle_set(cluster_atoms(i_cluster_atom))%atomic_kind%kind_number
     371          106 :          npoints = npoints + radial_lebedev_grids(ikind)%npts
     372              :       END DO
     373           87 :       ALLOCATE (cluster_points(3, npoints))
     374              : 
     375           29 :       offset = 0
     376          106 :       DO i_cluster_atom = 1, SIZE(cluster_atoms)
     377           77 :          cluster_iatom = cluster_atoms(i_cluster_atom)
     378           77 :          ikind = bs_env%ri_rs%particle_set(cluster_iatom)%atomic_kind%kind_number
     379           77 :          n = radial_lebedev_grids(ikind)%npts
     380              :          cluster_points(:, offset + 1:offset + n) = radial_lebedev_grids(ikind)%raw_points + &
     381              :                                                     SPREAD(cluster_centers(:, i_cluster_atom) - &
     382        28468 :                                                            center, 2, n)
     383          106 :          offset = offset + n
     384              :       END DO
     385           29 :    END SUBROUTINE collect_cluster_lebedev_points
     386              : 
     387              : ! **************************************************************************************************
     388              : !> \brief Evaluate ϕ_μ(r_l) needed for D_ll' in Eq. (7), using AO locality. Atom b uses the
     389              : !>        point rows atom_point_offsets(b):atom_point_offsets(b+1)-1. Its column-major block
     390              : !>        ϕ_μ(r_l) has shape (number of point rows, atom_n_ao(b)) and starts at
     391              : !>        atom_value_offsets(b) in ao_values.
     392              : !> \param bs_env ...
     393              : !> \param cluster_atoms ...
     394              : !> \param cluster_centers ...
     395              : !> \param center Position of the central atom, defining atom-relative grid coordinates.
     396              : !> \param cluster_points ...
     397              : !> \param ao_point_indices Cluster-point index for every stored point row.
     398              : !> \param atom_point_offsets First stored point row for each atom, followed by the final bound.
     399              : !> \param atom_value_offsets First AO value for each atom, followed by the final bound.
     400              : !> \param atom_n_ao Number of AO functions for each atom.
     401              : !> \param ao_values Compact atom-local values ϕ_μ(r_l).
     402              : ! **************************************************************************************************
     403           29 :    SUBROUTINE evaluate_cluster_ao_values(bs_env, cluster_atoms, cluster_centers, center, cluster_points, &
     404              :                                          ao_point_indices, atom_point_offsets, atom_value_offsets, &
     405              :                                          atom_n_ao, ao_values)
     406              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     407              :       INTEGER, INTENT(IN)                                :: cluster_atoms(:)
     408              :       REAL(KIND=dp), INTENT(IN)                          :: cluster_centers(:, :), center(3), &
     409              :                                                             cluster_points(:, :)
     410              :       INTEGER, ALLOCATABLE, INTENT(OUT)                  :: ao_point_indices(:), &
     411              :                                                             atom_point_offsets(:), &
     412              :                                                             atom_value_offsets(:), atom_n_ao(:)
     413              :       REAL(KIND=dp), ALLOCATABLE, INTENT(OUT)            :: ao_values(:)
     414              : 
     415              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'evaluate_cluster_ao_values'
     416              : 
     417              :       INTEGER :: cluster_iatom, first_point, first_value, handle, i_cluster_atom, igrid_point, &
     418              :          ikind, last_point, last_value, nactive, npoints
     419              :       INTEGER, ALLOCATABLE                               :: point_indices(:)
     420              :       LOGICAL, ALLOCATABLE                               :: active(:)
     421              :       REAL(KIND=dp)                                      :: source_position(3)
     422           29 :       REAL(KIND=dp), ALLOCATABLE                         :: atom_points(:, :), atom_values(:, :), &
     423              :                                                             relative_points(:, :)
     424              :       TYPE(gto_basis_set_type), POINTER                  :: ao
     425              : 
     426           29 :       CALL timeset(routineN, handle)
     427              : 
     428           29 :       npoints = SIZE(cluster_points, 2)
     429           87 :       ALLOCATE (relative_points(3, npoints))
     430           87 :       ALLOCATE (point_indices(npoints))
     431        14138 :       point_indices(:) = [(igrid_point, igrid_point=1, npoints)]
     432            0 :       ALLOCATE (active(npoints), atom_n_ao(SIZE(cluster_atoms)), &
     433            0 :                 atom_point_offsets(SIZE(cluster_atoms) + 1), &
     434          203 :                 atom_value_offsets(SIZE(cluster_atoms) + 1))
     435              : 
     436           29 :       atom_point_offsets(1) = 1
     437           29 :       atom_value_offsets(1) = 1
     438          106 :       DO i_cluster_atom = 1, SIZE(cluster_atoms)
     439           77 :          cluster_iatom = cluster_atoms(i_cluster_atom)
     440           77 :          ikind = bs_env%ri_rs%particle_set(cluster_iatom)%atomic_kind%kind_number
     441           77 :          ao => bs_env%basis_set_AO(ikind)%gto_basis_set
     442           77 :          CPASSERT(ao%kind_radius > 0.0_dp)
     443              :          relative_points(:, :) = cluster_points + &
     444        79585 :                                  SPREAD(center - cluster_centers(:, i_cluster_atom), 2, npoints)
     445              : 
     446              :          active(:) = SUM(relative_points**2, DIM=1) &
     447        79277 :                      <= ao%kind_radius**2
     448        19877 :          nactive = COUNT(active)
     449           77 :          atom_n_ao(i_cluster_atom) = ao%nsgf
     450           77 :          atom_point_offsets(i_cluster_atom + 1) = atom_point_offsets(i_cluster_atom) + nactive
     451              :          atom_value_offsets(i_cluster_atom + 1) = atom_value_offsets(i_cluster_atom) + &
     452          106 :                                                   nactive*atom_n_ao(i_cluster_atom)
     453              :       END DO
     454              : 
     455            0 :       ALLOCATE (ao_point_indices(atom_point_offsets(SIZE(cluster_atoms) + 1) - 1), &
     456          145 :                 ao_values(atom_value_offsets(SIZE(cluster_atoms) + 1) - 1))
     457          106 :       DO i_cluster_atom = 1, SIZE(cluster_atoms)
     458           77 :          cluster_iatom = cluster_atoms(i_cluster_atom)
     459           77 :          ikind = bs_env%ri_rs%particle_set(cluster_iatom)%atomic_kind%kind_number
     460           77 :          ao => bs_env%basis_set_AO(ikind)%gto_basis_set
     461              :          relative_points(:, :) = cluster_points + &
     462        79585 :                                  SPREAD(center - cluster_centers(:, i_cluster_atom), 2, npoints)
     463              : 
     464              :          active(:) = SUM(relative_points**2, DIM=1) &
     465        79277 :                      <= ao%kind_radius**2
     466           77 :          first_point = atom_point_offsets(i_cluster_atom)
     467           77 :          last_point = atom_point_offsets(i_cluster_atom + 1) - 1
     468           77 :          first_value = atom_value_offsets(i_cluster_atom)
     469           77 :          last_value = atom_value_offsets(i_cluster_atom + 1) - 1
     470           77 :          nactive = last_point - first_point + 1
     471           77 :          ao_point_indices(first_point:last_point) = PACK(point_indices, active)
     472          462 :          ALLOCATE (atom_points(3, nactive), atom_values(nactive, ao%nsgf))
     473          231 :          atom_points(:, :) = RESHAPE(PACK(relative_points, SPREAD(active, 1, 3)), [3, nactive])
     474           77 :          atom_values(:, :) = 0.0_dp
     475           77 :          source_position = 0.0_dp
     476              :          CALL evaluate_ao_basis_on_points(atom_values, atom_points, ao, source_position, &
     477           77 :                                           bs_env%ri_rs%cell, minimum_image=.FALSE.)
     478          154 :          ao_values(first_value:last_value) = RESHAPE(atom_values, [nactive*ao%nsgf])
     479          106 :          DEALLOCATE (atom_points, atom_values)
     480              :       END DO
     481              : 
     482           29 :       CALL timestop(handle)
     483              : 
     484           58 :    END SUBROUTINE evaluate_cluster_ao_values
     485              : 
     486              : ! **************************************************************************************************
     487              : !> \brief Apply the Cholesky selection of Eqs. (7)-(10) and retain selected points in V_A.
     488              : !> \param ao_point_indices Cluster-point index for every stored point row.
     489              : !> \param atom_point_offsets First stored point row for each atom, followed by the final bound.
     490              : !> \param atom_value_offsets First AO value for each atom, followed by the final bound.
     491              : !> \param atom_n_ao Number of AO functions for each atom.
     492              : !> \param ao_values Compact atom-local values ϕ_μ(r_l).
     493              : !> \param npoints Number of points in the cluster Lebedev grids.
     494              : !> \param inside_voronoi True for points inside the atomic Voronoi cell V_A.
     495              : !> \param selected_indices Selected indices in V_A; zero denotes rank exhaustion.
     496              : !> \param atom_env ...
     497              : ! **************************************************************************************************
     498           29 :    SUBROUTINE select_cholesky_grid_points(ao_point_indices, atom_point_offsets, atom_value_offsets, &
     499           58 :                                           atom_n_ao, ao_values, npoints, inside_voronoi, &
     500           29 :                                           selected_indices, atom_env)
     501              :       INTEGER, INTENT(IN)                                :: ao_point_indices(:), &
     502              :                                                             atom_point_offsets(:), &
     503              :                                                             atom_value_offsets(:), atom_n_ao(:)
     504              :       REAL(KIND=dp), CONTIGUOUS, INTENT(IN), TARGET      :: ao_values(:)
     505              :       INTEGER, INTENT(IN)                                :: npoints
     506              :       LOGICAL, INTENT(IN)                                :: inside_voronoi(npoints)
     507              :       INTEGER, INTENT(OUT)                               :: selected_indices(:)
     508              :       TYPE(mp_comm_type), INTENT(IN)                     :: atom_env
     509              : 
     510              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'select_cholesky_grid_points'
     511              :       REAL(KIND=dp), PARAMETER                           :: tie_tolerance = 1.0E-8_dp
     512              : 
     513              :       INTEGER :: capacity, first_local, first_point, first_row, first_value, handle, iblock, &
     514              :          igrid_point, irow, last_local, last_point, last_row, last_value, n_ao, new_capacity, &
     515              :          nretained, nrows, nselected, selected_point
     516           29 :       INTEGER, ALLOCATABLE                               :: first_row_local(:), last_row_local(:)
     517              :       REAL(KIND=dp)                                      :: rank_tolerance, selected_diagonal
     518           29 :       REAL(KIND=dp), ALLOCATABLE :: diagonal(:), factor(:, :), grown(:, :), selected_ao_values(:), &
     519           29 :          selected_factor_values(:), selection_matrix_column(:)
     520           29 :       REAL(KIND=dp), POINTER                             :: atom_ao_values(:, :)
     521              : 
     522           29 :       CALL timeset(routineN, handle)
     523              : 
     524          116 :       ALLOCATE (diagonal(npoints), selection_matrix_column(npoints))
     525              : 
     526              :       ! d_l^(0) = D_ll = [Σ_μ ϕ_μ(r_l)^2]^2.
     527           29 :       diagonal(:) = 0.0_dp
     528          106 :       DO iblock = 1, SIZE(atom_n_ao)
     529           77 :          first_point = atom_point_offsets(iblock)
     530           77 :          last_point = atom_point_offsets(iblock + 1) - 1
     531           77 :          first_value = atom_value_offsets(iblock)
     532           77 :          last_value = atom_value_offsets(iblock + 1) - 1
     533           77 :          nrows = last_point - first_point + 1
     534           77 :          atom_ao_values(1:nrows, 1:atom_n_ao(iblock)) => ao_values(first_value:last_value)
     535        16636 :          DO irow = 1, nrows
     536        16530 :             igrid_point = ao_point_indices(first_point + irow - 1)
     537        56809 :             diagonal(igrid_point) = diagonal(igrid_point) + SUM(atom_ao_values(irow, :)**2)
     538              :          END DO
     539              :       END DO
     540         7069 :       diagonal(:) = diagonal**2
     541              :       ! Residuals below N*eps*max(D_ll) are rounding noise (rank tolerance of LAPACK DPSTRF);
     542              :       ! accepting them would let the compiler's rounding decide the grid.
     543         7069 :       rank_tolerance = REAL(npoints, dp)*EPSILON(1.0_dp)*MAXVAL(diagonal)
     544              : 
     545           29 :       capacity = MIN(32, npoints)
     546              :       ALLOCATE (factor(npoints, capacity), selected_ao_values(MAXVAL(atom_n_ao)), &
     547          280 :                 selected_factor_values(npoints))
     548          572 :       selected_indices(:) = 0
     549           29 :       first_local = 1 + INT((INT(npoints, int_8)*atom_env%mepos)/atom_env%num_pe)
     550           29 :       last_local = INT((INT(npoints, int_8)*(atom_env%mepos + 1))/atom_env%num_pe)
     551          116 :       ALLOCATE (first_row_local(SIZE(atom_n_ao)), last_row_local(SIZE(atom_n_ao)))
     552          106 :       DO iblock = 1, SIZE(atom_n_ao)
     553           77 :          first_point = atom_point_offsets(iblock)
     554           77 :          last_point = atom_point_offsets(iblock + 1) - 1
     555        16607 :          first_row_local(iblock) = 1 + COUNT(ao_point_indices(first_point:last_point) < first_local)
     556        16636 :          last_row_local(iblock) = COUNT(ao_point_indices(first_point:last_point) <= last_local)
     557              :       END DO
     558          615 :       nselected = 0
     559          615 :       nretained = 0
     560          615 :       DO WHILE (nretained < SIZE(selected_indices) .AND. nselected < npoints)
     561              :          ! Symmetry-equivalent points tie up to rounding; take the first of them so that the
     562              :          ! grid does not depend on the compiler's floating-point evaluation (e.g. FMA).
     563       175158 :          selected_diagonal = MAXVAL(diagonal)
     564          606 :          IF (selected_diagonal <= 0.0_dp) EXIT
     565        90903 :          selected_point = FINDLOC(diagonal >= (1.0_dp - tie_tolerance)*selected_diagonal, .TRUE., DIM=1)
     566          586 :          selected_diagonal = diagonal(selected_point)
     567              : 
     568          586 :          IF (nselected == capacity) THEN
     569            0 :             new_capacity = MIN(npoints, capacity + MAX(32, capacity/2))
     570            0 :             ALLOCATE (grown(npoints, new_capacity))
     571            0 :             grown(:, :capacity) = factor(:, :capacity)
     572            0 :             CALL MOVE_ALLOC(grown, factor)
     573            0 :             capacity = new_capacity
     574              :          END IF
     575              : 
     576              :          ! Form D_lq from atom-local AO products without storing the dense D_ll' matrix.
     577          586 :          selection_matrix_column(:) = 0.0_dp
     578         2250 :          DO iblock = 1, SIZE(atom_n_ao)
     579         1664 :             first_point = atom_point_offsets(iblock)
     580         1664 :             last_point = atom_point_offsets(iblock + 1) - 1
     581         1664 :             nrows = last_point - first_point + 1
     582         1664 :             IF (nrows == 0) CYCLE
     583         1664 :             irow = locate(ao_point_indices(first_point:last_point), selected_point)
     584         1664 :             IF (irow == 0) CYCLE
     585         1576 :             first_value = atom_value_offsets(iblock)
     586         1576 :             last_value = atom_value_offsets(iblock + 1) - 1
     587         1576 :             n_ao = atom_n_ao(iblock)
     588         1576 :             atom_ao_values(1:nrows, 1:n_ao) => ao_values(first_value:last_value)
     589         5744 :             selected_ao_values(:n_ao) = atom_ao_values(irow, :)
     590         1576 :             first_row = first_row_local(iblock)
     591         1576 :             last_row = last_row_local(iblock)
     592              :             ! Sum each row in the same order independently of the MPI row partition.
     593       384022 :             DO irow = first_row, last_row
     594       381860 :                igrid_point = ao_point_indices(first_point + irow - 1)
     595              :                selection_matrix_column(igrid_point) = &
     596              :                   selection_matrix_column(igrid_point) + &
     597      1347648 :                   DOT_PRODUCT(atom_ao_values(irow, :), selected_ao_values(:n_ao))
     598              :             END DO
     599              :          END DO
     600          586 :          CALL atom_env%sum(selection_matrix_column)
     601       170058 :          selection_matrix_column(:) = selection_matrix_column**2
     602              : 
     603              :          ! L_lk = [D_lq_k - sum_(j<k) L_lj L_q_kj]/sqrt(d_q_k^(k-1)).
     604          586 :          IF (nselected > 0) THEN
     605         7745 :             selected_factor_values(:nselected) = factor(selected_point, :nselected)
     606          557 :             IF (atom_env%num_pe > 1) selection_matrix_column(:first_local - 1) = 0.0_dp
     607          557 :             IF (atom_env%num_pe > 1) selection_matrix_column(last_local + 1:) = 0.0_dp
     608       162989 :             DO igrid_point = first_local, last_local
     609              :                selection_matrix_column(igrid_point) = &
     610              :                   selection_matrix_column(igrid_point) - &
     611      2345069 :                   DOT_PRODUCT(factor(igrid_point, :nselected), selected_factor_values(:nselected))
     612              :             END DO
     613          557 :             CALL atom_env%sum(selection_matrix_column)
     614              :          END IF
     615          586 :          nselected = nselected + 1
     616       170058 :          factor(:, nselected) = selection_matrix_column/SQRT(selected_diagonal)
     617       170058 :          diagonal(:) = MAX(0.0_dp, diagonal - factor(:, nselected)**2)
     618       170058 :          WHERE (diagonal <= rank_tolerance) diagonal = 0.0_dp
     619          586 :          diagonal(selected_point) = 0.0_dp
     620              : 
     621          606 :          IF (inside_voronoi(selected_point)) THEN
     622          219 :             nretained = nretained + 1
     623          219 :             selected_indices(nretained) = selected_point
     624              :          END IF
     625              :       END DO
     626              : 
     627           29 :       CALL timestop(handle)
     628              : 
     629           58 :    END SUBROUTINE select_cholesky_grid_points
     630              : 
     631              : ! **************************************************************************************************
     632              : !> \brief Communicate the independently constructed atom grids after Eqs. (5)-(12).
     633              : !> \param bs_env ...
     634              : ! **************************************************************************************************
     635           24 :    SUBROUTINE broadcast_ri_rs_grids(bs_env)
     636              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     637              : 
     638              :       INTEGER                                            :: atom_mpi_rank, iatom, ngroups
     639              : 
     640           24 :       ngroups = MIN(bs_env%n_atom, bs_env%para_env%num_pe)
     641           82 :       DO iatom = 1, bs_env%n_atom
     642           58 :          atom_mpi_rank = MOD(iatom - 1, ngroups)
     643           82 :          CALL bs_env%para_env%bcast(bs_env%ri_rs%atomic_grids(iatom)%raw_points, atom_mpi_rank)
     644              :       END DO
     645           24 :    END SUBROUTINE broadcast_ri_rs_grids
     646              : 
     647              : ! **************************************************************************************************
     648              : !> \brief RS_AO_RATIO α of one atomic kind in Eq. (4).
     649              : !>
     650              : !> A positive RS_AO_RATIO applies to every kind. The default (a negative value) is 7 points per AO
     651              : !> function, except for full-k periodic GW with the small augmented MOLOPT basis sets: 14 for
     652              : !> aug-SZV-MOLOPT-GTH-tier-2, 9 for aug-DZVP-MOLOPT-GTH-tier-1 and 8 for aug-DZVP-MOLOPT-GTH-tier-2.
     653              : !> \param bs_env ...
     654              : !> \param ikind ...
     655              : !> \return α of the kind
     656              : ! **************************************************************************************************
     657          109 :    FUNCTION rs_ao_ratio_of_kind(bs_env, ikind) RESULT(ratio)
     658              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     659              :       INTEGER, INTENT(IN)                                :: ikind
     660              :       REAL(KIND=dp)                                      :: ratio
     661              : 
     662              :       CHARACTER(LEN=default_string_length)               :: basis_name
     663              : 
     664          109 :       ratio = bs_env%ri_rs%grid_opt%rs_ao_ratio
     665          109 :       IF (ratio > 0.0_dp) RETURN
     666            0 :       ratio = 7.0_dp
     667            0 :       IF (bs_env%gw_implementation /= ri_rs_small_cell_full_kp) RETURN
     668            0 :       basis_name = bs_env%basis_set_AO(ikind)%gto_basis_set%name
     669            0 :       CALL uppercase(basis_name)
     670            0 :       SELECT CASE (TRIM(basis_name))
     671              :       CASE ("AUG-SZV-MOLOPT-GTH-TIER-2")
     672            0 :          ratio = 14.0_dp
     673              :       CASE ("AUG-DZVP-MOLOPT-GTH-TIER-1")
     674            0 :          ratio = 9.0_dp
     675              :       CASE ("AUG-DZVP-MOLOPT-GTH-TIER-2")
     676            0 :          ratio = 8.0_dp
     677              :       END SELECT
     678              :    END FUNCTION rs_ao_ratio_of_kind
     679              : 
     680              : END MODULE ri_rs_grid_initialization
        

Generated by: LCOV version 2.0-1