LCOV - code coverage report
Current view: top level - src - ri_rs_utils.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 92.7 % 177 164
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 6 6

            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 Geometry and AO evaluation for real-space resolution of the identity.
      10              : ! **************************************************************************************************
      11              : MODULE ri_rs_utils
      12              :    USE atomic_kind_types,               ONLY: get_atomic_kind_set
      13              :    USE basis_set_types,                 ONLY: gto_basis_set_p_type,&
      14              :                                               gto_basis_set_type
      15              :    USE cell_types,                      ONLY: cell_type,&
      16              :                                               get_cell,&
      17              :                                               pbc,&
      18              :                                               pbc_stable
      19              :    USE kinds,                           ONLY: dp
      20              :    USE orbital_pointers,                ONLY: indco,&
      21              :                                               ncoset
      22              :    USE particle_types,                  ONLY: particle_type
      23              :    USE physcon,                         ONLY: angstrom
      24              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      25              :                                               qs_kind_type
      26              :    USE ri_rs_types,                     ONLY: ri_rs_env
      27              : #include "./base/base_uses.f90"
      28              : 
      29              :    IMPLICIT NONE
      30              :    PRIVATE
      31              : 
      32              :    PUBLIC :: evaluate_ao_basis_on_points, evaluate_ao_on_points, &
      33              :              get_rirs_cluster_atoms, precompute_ri_rs_radii, &
      34              :              filter_grid_to_voronoi
      35              : 
      36              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ri_rs_utils'
      37              : 
      38              :    ! Automatic cluster radius in units of the nearest interatomic distance.
      39              :    REAL(KIND=dp), PARAMETER :: cluster_radius_per_nearest = 1.4_dp
      40              : 
      41              : CONTAINS
      42              : 
      43              : ! **************************************************************************************************
      44              : !> \brief Compute per-atom AO and RI basis radii from the most diffuse Gaussian
      45              : !>        primitive in the AO ("ORB") and RI auxiliary ("RI_AUX") basis sets.
      46              : !>        Stores per-atom screening radii in ri_rs and prints a per-kind table.
      47              : !>        Radius: r_kind = sqrt(-log(eps)/alpha_min_kind), with eps = eps_filter.
      48              : !> \param ri_rs RI-RS geometry references and screening radii
      49              : !> \param basis_set_AO AO basis sets, one per atomic kind
      50              : !> \param basis_set_RI auxiliary basis sets, one per atomic kind
      51              : !> \param eps screening threshold
      52              : !> \param unit_nr output unit
      53              : ! **************************************************************************************************
      54           64 :    SUBROUTINE precompute_ri_rs_radii(ri_rs, basis_set_AO, basis_set_RI, eps, unit_nr)
      55              :       TYPE(ri_rs_env), INTENT(INOUT)                     :: ri_rs
      56              :       TYPE(gto_basis_set_p_type), INTENT(IN)             :: basis_set_AO(:), basis_set_RI(:)
      57              :       REAL(KIND=dp), INTENT(IN)                          :: eps
      58              :       INTEGER, INTENT(IN)                                :: unit_nr
      59              : 
      60              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'precompute_ri_rs_radii'
      61              :       REAL(KIND=dp), PARAMETER :: min_exponent_for_radius = 1.0E-3_dp
      62              : 
      63              :       INTEGER                                            :: handle, i, iatom, ikind, j, natom, nkind
      64           64 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: kind_of
      65           64 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: alpha_min_ao_kind, alpha_min_ri_kind
      66           64 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: zet_ao, zet_ri
      67              : 
      68           64 :       CALL timeset(routineN, handle)
      69              : 
      70           64 :       CPASSERT(ASSOCIATED(ri_rs%atomic_kind_set))
      71           64 :       CPASSERT(ASSOCIATED(ri_rs%particle_set))
      72           64 :       CPASSERT(0.0_dp < eps .AND. eps < 1.0_dp)
      73           64 :       nkind = SIZE(ri_rs%atomic_kind_set)
      74           64 :       natom = SIZE(ri_rs%particle_set)
      75              : 
      76          256 :       ALLOCATE (alpha_min_ao_kind(nkind), alpha_min_ri_kind(nkind))
      77          152 :       alpha_min_ao_kind = HUGE(1.0_dp)
      78          152 :       alpha_min_ri_kind = HUGE(1.0_dp)
      79              : 
      80          152 :       DO ikind = 1, nkind
      81           88 :          zet_ao => basis_set_AO(ikind)%gto_basis_set%zet
      82           88 :          zet_ri => basis_set_RI(ikind)%gto_basis_set%zet
      83          314 :          DO i = 1, SIZE(zet_ao, 1)
      84          690 :             DO j = 1, SIZE(zet_ao, 2)
      85          602 :                IF (zet_ao(i, j) > min_exponent_for_radius) THEN
      86          376 :                   alpha_min_ao_kind(ikind) = MIN(alpha_min_ao_kind(ikind), zet_ao(i, j))
      87              :                END IF
      88              :             END DO
      89              :          END DO
      90          316 :          DO i = 1, SIZE(zet_ri, 1)
      91          934 :             DO j = 1, SIZE(zet_ri, 2)
      92          846 :                IF (zet_ri(i, j) > min_exponent_for_radius) THEN
      93          646 :                   alpha_min_ri_kind(ikind) = MIN(alpha_min_ri_kind(ikind), zet_ri(i, j))
      94              :                END IF
      95              :             END DO
      96              :          END DO
      97              :       END DO
      98              : 
      99           64 :       CALL get_atomic_kind_set(atomic_kind_set=ri_rs%atomic_kind_set, kind_of=kind_of)
     100              : 
     101          192 :       ALLOCATE (ri_rs%radius_ao_per_atom(natom))
     102          128 :       ALLOCATE (ri_rs%radius_ri_per_atom(natom))
     103          216 :       DO iatom = 1, natom
     104          152 :          ikind = kind_of(iatom)
     105          152 :          ri_rs%radius_ao_per_atom(iatom) = SQRT(-LOG(eps)/alpha_min_ao_kind(ikind))
     106          216 :          ri_rs%radius_ri_per_atom(iatom) = SQRT(-LOG(eps)/alpha_min_ri_kind(ikind))
     107              :       END DO
     108              : 
     109           64 :       IF (unit_nr > 0) THEN
     110           32 :          WRITE (unit_nr, '(T2,A)') 'RI-RS basis radii (Å):'
     111           32 :          WRITE (unit_nr, '(T4,A6,2X,A4,2A14)') 'Kind', 'Elem', 'r_AO (Å)', 'r_RI (Å)'
     112           76 :          DO ikind = 1, nkind
     113              :             WRITE (unit_nr, '(T4,I6,2X,A4,2F14.4)') &
     114           44 :                ikind, &
     115           44 :                ri_rs%atomic_kind_set(ikind)%element_symbol, &
     116           44 :                SQRT(-LOG(eps)/alpha_min_ao_kind(ikind))*angstrom, &
     117          120 :                SQRT(-LOG(eps)/alpha_min_ri_kind(ikind))*angstrom
     118              :          END DO
     119           32 :          WRITE (unit_nr, '(A)') ' '
     120              :       END IF
     121              : 
     122           64 :       DEALLOCATE (alpha_min_ao_kind, alpha_min_ri_kind, kind_of)
     123              : 
     124           64 :       CALL timestop(handle)
     125              : 
     126           64 :    END SUBROUTINE precompute_ri_rs_radii
     127              : 
     128              : ! **************************************************************************************************
     129              : !> \brief Keep atom-relative points inside the Voronoi volume of one atom.
     130              : !> \param points Atom-relative source-grid coordinates; unchanged when mask is present.
     131              : !> \param icenter_atom Atom in the reference cell whose Voronoi volume is retained.
     132              : !> \param particle_set Atoms in the reference cell.
     133              : !> \param cell Simulation cell and periodicity.
     134              : !> \param mask Optional membership mask.
     135              : ! **************************************************************************************************
     136           29 :    SUBROUTINE filter_grid_to_voronoi(points, icenter_atom, particle_set, cell, mask)
     137              :       REAL(KIND=dp), ALLOCATABLE, INTENT(INOUT)          :: points(:, :)
     138              :       INTEGER, INTENT(IN)                                :: icenter_atom
     139              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     140              :       TYPE(cell_type), POINTER                           :: cell
     141              :       LOGICAL, INTENT(OUT), OPTIONAL                     :: mask(:)
     142              : 
     143              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'filter_grid_to_voronoi'
     144              : 
     145              :       INTEGER                                            :: handle, iimage, ipoint, n_keep
     146           29 :       INTEGER, ALLOCATABLE                               :: atom_indices(:), cell_indices(:, :)
     147              :       LOGICAL, ALLOCATABLE                               :: keep(:)
     148              :       REAL(KIND=dp)                                      :: displacement(3), distance2, max_radius, &
     149              :                                                             tolerance
     150           29 :       REAL(KIND=dp), ALLOCATABLE                         :: centers(:, :), filtered_points(:, :)
     151              : 
     152           29 :       CALL timeset(routineN, handle)
     153              : 
     154           29 :       CPASSERT(SIZE(points, 1) == 3)
     155         7127 :       ALLOCATE (keep(SIZE(points, 2)), SOURCE=.TRUE.)
     156           29 :       IF (SIZE(points, 2) > 0) THEN
     157         7069 :          max_radius = MAXVAL(NORM2(points, DIM=1))
     158              :       ELSE
     159              :          max_radius = 0.0_dp
     160              :       END IF
     161              :       CALL get_rirs_cluster_atoms(particle_set, cell, icenter_atom, 2.0_dp*max_radius, &
     162           29 :                                   atom_indices, cell_indices, centers)
     163        19512 :       DO iimage = 1, SIZE(atom_indices)
     164        22118 :          IF (atom_indices(iimage) == icenter_atom .AND. &
     165              :              ALL(cell_indices(:, iimage) == 0)) CYCLE
     166        77816 :          displacement(:) = centers(:, iimage) - pbc(particle_set(icenter_atom)%r, cell)
     167        77816 :          distance2 = SUM(displacement**2)
     168        19454 :          tolerance = 32.0_dp*EPSILON(1.0_dp)*MAX(1.0_dp, distance2)
     169      5232539 :          DO ipoint = 1, SIZE(points, 2)
     170      5213056 :             IF (.NOT. keep(ipoint)) CYCLE
     171              :             ! |r_l-R_B|² - |r_l-R_A|² = |R_B-R_A|² - 2(r_l-R_A)·(R_B-R_A).
     172      9724803 :             IF (2.0_dp*DOT_PRODUCT(points(:, ipoint), displacement) > distance2 + tolerance) THEN
     173         4803 :                keep(ipoint) = .FALSE.
     174      2421527 :             ELSE IF (atom_indices(iimage) < icenter_atom) THEN
     175      3002804 :                IF (ABS(2.0_dp*DOT_PRODUCT(points(:, ipoint), displacement) - distance2) <= &
     176           49 :                    tolerance) keep(ipoint) = .FALSE.
     177              :             END IF
     178              :          END DO
     179              :       END DO
     180           29 :       IF (PRESENT(mask)) THEN
     181           29 :          CPASSERT(SIZE(mask) == SIZE(keep))
     182         7069 :          mask(:) = keep
     183           29 :          CALL timestop(handle)
     184           29 :          RETURN
     185              :       END IF
     186            0 :       n_keep = COUNT(keep)
     187            0 :       ALLOCATE (filtered_points(3, n_keep))
     188            0 :       IF (n_keep > 0) filtered_points(:, :) = RESHAPE(PACK(points, SPREAD(keep, 1, 3)), [3, n_keep])
     189            0 :       CALL MOVE_ALLOC(filtered_points, points)
     190              : 
     191            0 :       CALL timestop(handle)
     192              : 
     193           58 :    END SUBROUTINE filter_grid_to_voronoi
     194              : 
     195              : ! **************************************************************************************************
     196              : !> \brief Form a cluster from all atom images inside a sphere about a reference atom.
     197              : !>
     198              : !> Nonperiodic directions have only the zero translation.
     199              : !> Every image is identified by its reference-cell atom and integer cell translation. Thus, the
     200              : !> same atom can occur more than once in a small periodic cell. The central atom is returned first;
     201              : !> the remaining images follow in atom-major, relative-image-minor order.
     202              : !> \param particle_set Atoms in the reference cell.
     203              : !> \param cell Simulation cell and periodicity.
     204              : !> \param icenter_atom Central atom in cell (0,0,0).
     205              : !> \param radius Cluster radius; a negative value selects 1.4 times the nearest-neighbor distance.
     206              : !> \param atom_indices Reference-cell atom index for every image.
     207              : !> \param cell_indices Integer cell translation for every image.
     208              : !> \param centers Cartesian image centres.
     209              : !> \param effective_radius Resolved nonnegative radius, in internal length units.
     210              : ! **************************************************************************************************
     211           87 :    SUBROUTINE get_rirs_cluster_atoms(particle_set, cell, icenter_atom, radius, atom_indices, &
     212              :                                      cell_indices, centers, effective_radius)
     213              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     214              :       TYPE(cell_type), POINTER                           :: cell
     215              :       INTEGER, INTENT(IN)                                :: icenter_atom
     216              :       REAL(KIND=dp), INTENT(IN)                          :: radius
     217              :       INTEGER, ALLOCATABLE, INTENT(OUT)                  :: atom_indices(:)
     218              :       INTEGER, ALLOCATABLE, INTENT(OUT), OPTIONAL        :: cell_indices(:, :)
     219              :       REAL(KIND=dp), ALLOCATABLE, INTENT(OUT), OPTIONAL  :: centers(:, :)
     220              :       REAL(KIND=dp), INTENT(OUT), OPTIONAL               :: effective_radius
     221              : 
     222              :       INTEGER                                            :: iatom, idim, image(3), image_max(3), &
     223              :                                                             image_min(3), image_shift(3), ix, iy, &
     224              :                                                             iz, max_images, nimages
     225           87 :       INTEGER, ALLOCATABLE                               :: atom_work(:), cell_work(:, :)
     226              :       REAL(KIND=dp)                                      :: center(3), center_atom(3), &
     227              :                                                             cluster_radius, delta(3), radius2, &
     228              :                                                             tolerance
     229           87 :       REAL(KIND=dp), ALLOCATABLE                         :: center_work(:, :)
     230              : 
     231           87 :       cluster_radius = radius
     232           87 :       IF (radius < 0.0_dp) cluster_radius = automatic_cluster_radius(particle_set, cell, icenter_atom)
     233           87 :       IF (PRESENT(effective_radius)) effective_radius = cluster_radius
     234           87 :       center_atom(:) = pbc(particle_set(icenter_atom)%r, cell)
     235          348 :       DO idim = 1, 3
     236          348 :          IF (cell%perd(idim) == 1) THEN
     237          432 :             image_max(idim) = CEILING(cluster_radius*NORM2(cell%h_inv(idim, :))) + 1
     238              :          ELSE
     239          153 :             image_max(idim) = 0
     240              :          END IF
     241              :       END DO
     242          348 :       image_min(:) = -image_max(:)
     243          348 :       max_images = SIZE(particle_set)*PRODUCT(image_max - image_min + 1)
     244          609 :       ALLOCATE (atom_work(max_images), cell_work(3, max_images), center_work(3, max_images))
     245           87 :       radius2 = cluster_radius**2
     246           87 :       tolerance = 64.0_dp*EPSILON(1.0_dp)*MAX(1.0_dp, radius2)
     247           87 :       nimages = 1
     248           87 :       atom_work(1) = icenter_atom
     249          348 :       cell_work(:, 1) = 0
     250          348 :       center_work(:, 1) = center_atom
     251          306 :       DO iatom = 1, SIZE(particle_set)
     252          876 :          delta(:) = pbc_stable(particle_set(iatom)%r - particle_set(icenter_atom)%r, cell)
     253              :          ! δ_i = r_i^w - r_c^w + H s_i; cell labels remain those of the reference-cell atoms.
     254         4161 :          image_shift(:) = NINT(MATMUL(cell%h_inv, delta - (pbc(particle_set(iatom)%r, cell) - center_atom)))
     255          909 :          DO iz = image_min(3), image_max(3)
     256         6609 :             DO iy = image_min(2), image_max(2)
     257        90705 :                DO ix = image_min(1), image_max(1)
     258       337260 :                   image(:) = [ix, iy, iz]
     259        90924 :                   IF (iatom == icenter_atom .AND. ALL(image == 0)) CYCLE
     260      1600332 :                   center(:) = delta + MATMUL(cell%hmat, REAL(image, dp))
     261       336912 :                   IF (SUM(center**2) > radius2 + tolerance) CYCLE
     262        19550 :                   nimages = nimages + 1
     263        19550 :                   atom_work(nimages) = iatom
     264        78200 :                   cell_work(:, nimages) = image + image_shift
     265        83987 :                   center_work(:, nimages) = center_atom + center
     266              :                END DO
     267              :             END DO
     268              :          END DO
     269              :       END DO
     270        19898 :       ALLOCATE (atom_indices(nimages), SOURCE=atom_work(:nimages))
     271        78809 :       IF (PRESENT(cell_indices)) ALLOCATE (cell_indices(3, nimages), SOURCE=cell_work(:, :nimages))
     272        78809 :       IF (PRESENT(centers)) ALLOCATE (centers(3, nimages), SOURCE=center_work(:, :nimages))
     273           87 :    END SUBROUTINE get_rirs_cluster_atoms
     274              : 
     275              : ! **************************************************************************************************
     276              : !> \brief Evaluate contracted spherical AOs, and optionally their grid-coordinate derivatives.
     277              : !>
     278              : !> For atom A and displacement d(alpha)=r_l(alpha)-R_A(alpha), the value is
     279              : !>
     280              : !>   ϕ_μ(r_l) = Σ_(p,c) S_(pc,μ) d_x^lx d_y^ly d_z^lz exp(-ζ_p |d|^2).
     281              : !>
     282              : !> The optional derivative is evaluated analytically as
     283              : !>
     284              : !>   d ϕ_μ(r_l)/d r_(l,α)
     285              : !>     = sum_(p,c) S_(pc,mu) exp(-zeta_p |d|^2)
     286              : !>       [d polynomial_c/d d_alpha - 2 zeta_p d_alpha polynomial_c].
     287              : !>
     288              : !> \param phi AO values, accumulated into phi(l,mu).
     289              : !> \param grid_points Cartesian grid points, indexed (alpha,l).
     290              : !> \param iatom Source atom whose contracted AOs are evaluated.
     291              : !> \param particle_set Molecular particles.
     292              : !> \param qs_kind_set Quickstep atomic kinds containing the ORB bases.
     293              : !> \param cell Simulation cell used for the minimum-image displacement.
     294              : !> \param dphi Optional AO derivatives, accumulated into dphi(alpha,l,mu).
     295              : !> \param cutoff_squared Optional squared AO cutoff radius.
     296              : ! **************************************************************************************************
     297          151 :    SUBROUTINE evaluate_ao_on_points(phi, grid_points, iatom, particle_set, qs_kind_set, cell, &
     298          151 :                                     dphi, cutoff_squared)
     299              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT)      :: phi
     300              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(IN)         :: grid_points
     301              :       INTEGER, INTENT(IN)                                :: iatom
     302              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     303              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     304              :       TYPE(cell_type), POINTER                           :: cell
     305              :       REAL(KIND=dp), DIMENSION(:, :, :), INTENT(INOUT), &
     306              :          OPTIONAL                                        :: dphi
     307              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: cutoff_squared
     308              : 
     309              :       CHARACTER(len=*), PARAMETER :: routineN = 'evaluate_ao_on_points'
     310              : 
     311              :       INTEGER                                            :: handle, ikind
     312              :       TYPE(gto_basis_set_type), POINTER                  :: basis
     313              : 
     314          151 :       CALL timeset(routineN, handle)
     315          151 :       CPASSERT(SIZE(grid_points, 1) == 3)
     316          151 :       CPASSERT(SIZE(phi, 1) == SIZE(grid_points, 2))
     317          151 :       IF (PRESENT(dphi)) THEN
     318            0 :          CPASSERT(SIZE(dphi, 1) == 3)
     319            0 :          CPASSERT(SIZE(dphi, 2) == SIZE(phi, 1))
     320            0 :          CPASSERT(SIZE(dphi, 3) == SIZE(phi, 2))
     321              :       END IF
     322              : 
     323          151 :       ikind = particle_set(iatom)%atomic_kind%kind_number
     324          151 :       CALL get_qs_kind(qs_kind_set(ikind), basis_set=basis, basis_type='ORB')
     325          151 :       IF (.NOT. ASSOCIATED(basis)) THEN
     326            0 :          CALL timestop(handle)
     327            0 :          RETURN
     328              :       END IF
     329              :       CALL evaluate_ao_basis_on_points(phi, grid_points, basis, &
     330          302 :                                        particle_set(iatom)%r, cell, dphi, cutoff_squared)
     331          151 :       CALL timestop(handle)
     332              : 
     333              :    END SUBROUTINE evaluate_ao_on_points
     334              : 
     335              : ! **************************************************************************************************
     336              : !> \brief Evaluate one explicitly supplied contracted Gaussian basis on Cartesian points.
     337              : !> \param phi AO values, accumulated into phi(l,mu).
     338              : !> \param grid_points Cartesian grid points, indexed (alpha,l).
     339              : !> \param basis Contracted Gaussian basis to evaluate.
     340              : !> \param source_position Centre of the basis.
     341              : !> \param cell Simulation cell defining minimum-image or image-summed displacements.
     342              : !> \param dphi Optional AO derivatives, accumulated into dphi(alpha,l,mu).
     343              : !> \param cutoff_squared Optional squared AO cutoff radius.
     344              : !> \param periodic_images Sum images -1:1 along periodic axes and only image 0 along other axes.
     345              : !> \param minimum_image Wrap displacements into the cell (default true); disable for explicit images.
     346              : ! **************************************************************************************************
     347        35935 :    SUBROUTINE evaluate_ao_basis_on_points(phi, grid_points, basis, source_position, cell, &
     348        35935 :                                           dphi, cutoff_squared, periodic_images, minimum_image)
     349              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT)      :: phi
     350              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(IN)         :: grid_points
     351              :       TYPE(gto_basis_set_type), POINTER                  :: basis
     352              :       REAL(KIND=dp), DIMENSION(3), INTENT(IN)            :: source_position
     353              :       TYPE(cell_type), POINTER                           :: cell
     354              :       REAL(KIND=dp), DIMENSION(:, :, :), INTENT(INOUT), &
     355              :          OPTIONAL                                        :: dphi
     356              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: cutoff_squared
     357              :       LOGICAL, INTENT(IN), OPTIONAL                      :: periodic_images, minimum_image
     358              : 
     359              :       INTEGER :: first_sgf, ialpha, ico, iend_co, image_extent(3), ipgf, ipoint, irow, iset, isgf, &
     360              :          ishell, istart_co, ix, iy, iz, l, last_sgf, lx, ly, lz, n_cart_total
     361              :       LOGICAL                                            :: sum_images, use_minimum_image
     362              :       REAL(KIND=dp) :: exponent, exponential, hmat(3, 3), polynomial, polynomial_derivative(3), &
     363              :          radius2, relative(3), relative_raw(3), weight
     364              : 
     365        35935 :       CPASSERT(ASSOCIATED(basis))
     366        35935 :       CPASSERT(SIZE(grid_points, 1) == 3)
     367        35935 :       CPASSERT(SIZE(phi, 1) == SIZE(grid_points, 2))
     368        35935 :       IF (PRESENT(dphi)) THEN
     369          553 :          CPASSERT(SIZE(dphi, 1) == 3)
     370          553 :          CPASSERT(SIZE(dphi, 2) == SIZE(phi, 1))
     371          553 :          CPASSERT(SIZE(dphi, 3) == SIZE(phi, 2))
     372              :       END IF
     373              : 
     374        35935 :       use_minimum_image = .TRUE.
     375        35935 :       IF (PRESENT(minimum_image)) use_minimum_image = minimum_image
     376        35935 :       sum_images = .FALSE.
     377        35935 :       IF (PRESENT(periodic_images)) sum_images = periodic_images
     378        35935 :       image_extent = 0
     379        35935 :       hmat = 0.0_dp
     380        35935 :       IF (sum_images) THEN
     381            0 :          image_extent = cell%perd
     382            0 :          CALL get_cell(cell, h=hmat)
     383              :       END IF
     384              : 
     385              :       !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
     386              :       !$OMP SHARED(phi, dphi, grid_points, cell, basis, source_position, cutoff_squared, ncoset, indco) &
     387              :       !$OMP SHARED(sum_images, image_extent, hmat, use_minimum_image) &
     388              :       !$OMP PRIVATE(ipoint, relative, radius2, iset, n_cart_total, ishell, l, istart_co, &
     389              :       !$OMP         iend_co, first_sgf, last_sgf, ipgf, exponent, exponential, isgf, ico, &
     390              :       !$OMP         irow, weight, lx, ly, lz, polynomial, polynomial_derivative, ialpha, &
     391        35935 :       !$OMP         relative_raw, ix, iy, iz)
     392              :       DO ipoint = 1, SIZE(grid_points, 2)
     393              :          relative_raw = grid_points(:, ipoint) - source_position
     394              :          IF (.NOT. sum_images .AND. use_minimum_image) relative_raw = pbc(relative_raw, cell)
     395              :          DO ix = -image_extent(1), image_extent(1)
     396              :          DO iy = -image_extent(2), image_extent(2)
     397              :          DO iz = -image_extent(3), image_extent(3)
     398              :             relative = relative_raw
     399              :             IF (sum_images) relative = relative_raw - MATMUL(hmat, REAL([ix, iy, iz], dp))
     400              :             radius2 = DOT_PRODUCT(relative, relative)
     401              :             IF (PRESENT(cutoff_squared)) THEN
     402              :                IF (radius2 > cutoff_squared) CYCLE
     403              :             END IF
     404              : 
     405              :             DO iset = 1, basis%nset
     406              :                n_cart_total = ncoset(basis%lmax(iset))
     407              :                DO ishell = 1, basis%nshell(iset)
     408              :                   l = basis%l(ishell, iset)
     409              :                   istart_co = ncoset(l - 1) + 1
     410              :                   iend_co = ncoset(l)
     411              :                   first_sgf = basis%first_sgf(ishell, iset)
     412              :                   last_sgf = basis%last_sgf(ishell, iset)
     413              :                   DO ipgf = 1, basis%npgf(iset)
     414              :                      exponent = basis%zet(ipgf, iset)
     415              :                      exponential = EXP(-exponent*radius2)
     416              :                      DO isgf = first_sgf, last_sgf
     417              :                         DO ico = istart_co, iend_co
     418              :                            irow = (ipgf - 1)*n_cart_total + ico
     419              :                            weight = basis%sphi(irow, isgf)
     420              :                            lx = indco(1, ico)
     421              :                            ly = indco(2, ico)
     422              :                            lz = indco(3, ico)
     423              :                            polynomial = relative(1)**lx*relative(2)**ly*relative(3)**lz
     424              :                            phi(ipoint, isgf) = phi(ipoint, isgf) + weight*polynomial*exponential
     425              : 
     426              :                            IF (PRESENT(dphi)) THEN
     427              :                               polynomial_derivative = 0.0_dp
     428              :                               IF (lx > 0) polynomial_derivative(1) = REAL(lx, dp)*relative(1)**(lx - 1)* &
     429              :                                                                      relative(2)**ly*relative(3)**lz
     430              :                               IF (ly > 0) polynomial_derivative(2) = REAL(ly, dp)*relative(1)**lx* &
     431              :                                                                      relative(2)**(ly - 1)*relative(3)**lz
     432              :                               IF (lz > 0) polynomial_derivative(3) = REAL(lz, dp)*relative(1)**lx* &
     433              :                                                                      relative(2)**ly*relative(3)**(lz - 1)
     434              :                               DO ialpha = 1, 3
     435              :                                  dphi(ialpha, ipoint, isgf) = dphi(ialpha, ipoint, isgf) + weight*exponential* &
     436              :                                                               (polynomial_derivative(ialpha) - &
     437              :                                                                2.0_dp*exponent*relative(ialpha)*polynomial)
     438              :                               END DO
     439              :                            END IF
     440              :                         END DO
     441              :                      END DO
     442              :                   END DO
     443              :                END DO
     444              :             END DO
     445              :          END DO
     446              :          END DO
     447              :          END DO
     448              :       END DO
     449              :       !$OMP END PARALLEL DO
     450        35935 :    END SUBROUTINE evaluate_ao_basis_on_points
     451              : 
     452              : ! **************************************************************************************************
     453              : !> \brief Resolve radius_A = 1.4 min_(B,T)!=(A,0) |R_B + T - R_A|, or zero for an isolated atom.
     454              : !>
     455              : !> A wrapped interatomic displacement or a periodic cell vector supplies an upper bound on the
     456              : !> nearest distance. Reciprocal-cell bounds enumerate every image inside that sphere, including
     457              : !> for skewed cells. Only the central image is excluded; coincident distinct atoms are invalid.
     458              : !> \param particle_set ...
     459              : !> \param cell ...
     460              : !> \param icenter_atom ...
     461              : !> \return ...
     462              : ! **************************************************************************************************
     463           12 :    FUNCTION automatic_cluster_radius(particle_set, cell, icenter_atom) RESULT(radius)
     464              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     465              :       TYPE(cell_type), POINTER                           :: cell
     466              :       INTEGER, INTENT(IN)                                :: icenter_atom
     467              :       REAL(KIND=dp)                                      :: radius
     468              : 
     469              :       INTEGER                                            :: iatom, idim, image_max(3), ix, iy, iz
     470              :       REAL(KIND=dp)                                      :: delta(3), distance2, nearest2, upper2
     471              : 
     472           12 :       CPASSERT(ASSOCIATED(particle_set))
     473           12 :       CPASSERT(ASSOCIATED(cell))
     474           12 :       CPASSERT(icenter_atom >= 1 .AND. icenter_atom <= SIZE(particle_set))
     475           48 :       IF (SIZE(particle_set) == 1 .AND. ALL(cell%perd == 0)) THEN
     476           12 :          radius = 0.0_dp
     477              :          RETURN
     478              :       END IF
     479              :       upper2 = HUGE(1.0_dp)
     480           48 :       DO idim = 1, 3
     481           48 :          IF (cell%perd(idim) == 1) upper2 = MIN(upper2, SUM(cell%hmat(:, idim)**2))
     482              :       END DO
     483           48 :       DO iatom = 1, SIZE(particle_set)
     484           36 :          IF (iatom == icenter_atom) CYCLE
     485           96 :          delta = pbc_stable(particle_set(iatom)%r - particle_set(icenter_atom)%r, cell)
     486          120 :          upper2 = MIN(upper2, SUM(delta**2))
     487              :       END DO
     488           48 :       DO idim = 1, 3
     489           36 :          image_max(idim) = 0
     490           48 :          IF (cell%perd(idim) == 1) THEN
     491            0 :             image_max(idim) = CEILING(SQRT(upper2)*NORM2(cell%h_inv(idim, :))) + 1
     492              :          END IF
     493              :       END DO
     494           12 :       nearest2 = upper2
     495           48 :       DO iatom = 1, SIZE(particle_set)
     496          144 :          delta = pbc_stable(particle_set(iatom)%r - particle_set(icenter_atom)%r, cell)
     497           84 :          DO iz = -image_max(3), image_max(3)
     498          108 :             DO iy = -image_max(2), image_max(2)
     499          108 :                DO ix = -image_max(1), image_max(1)
     500           36 :                   IF (iatom == icenter_atom .AND. ix == 0 .AND. iy == 0 .AND. iz == 0) CYCLE
     501          456 :                   distance2 = SUM((delta + MATMUL(cell%hmat, REAL([ix, iy, iz], dp)))**2)
     502           60 :                   nearest2 = MIN(nearest2, distance2)
     503              :                END DO
     504              :             END DO
     505              :          END DO
     506              :       END DO
     507           12 :       IF (nearest2 <= 0.0_dp) CPABORT("Automatic RI-RS cluster radius requires distinct atomic positions")
     508           12 :       radius = cluster_radius_per_nearest*SQRT(nearest2)
     509           12 :    END FUNCTION automatic_cluster_radius
     510              : 
     511              : END MODULE ri_rs_utils
        

Generated by: LCOV version 2.0-1