LCOV - code coverage report
Current view: top level - src - gapw_unittest.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 97.9 % 145 142
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 5 5

            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            2 : PROGRAM gapw_unittest
       9              :    USE basis_set_types,                 ONLY: allocate_gto_basis_set,&
      10            2 :                                               create_primitive_basis_set,&
      11              :                                               deallocate_gto_basis_set,&
      12              :                                               gto_basis_set_type
      13              :    USE cell_types,                      ONLY: cell_type
      14              :    USE cp_log_handling,                 ONLY: cp_logger_get_default_io_unit
      15              :    USE gapw_1c_basis_set,               ONLY: create_1c_basis
      16              :    USE hartree_local_methods,           ONLY: gapw_background_potential
      17              :    USE kinds,                           ONLY: dp
      18              :    USE lebedev,                         ONLY: init_lebedev_grids,&
      19              :                                               lebedev_grid
      20              :    USE libcp2k,                         ONLY: cp2k_finalize,&
      21              :                                               cp2k_init
      22              :    USE mathconstants,                   ONLY: fourpi,&
      23              :                                               pi
      24              :    USE mathlib,                         ONLY: inv_3x3
      25              :    USE orbital_pointers,                ONLY: indso,&
      26              :                                               init_orbital_pointers
      27              :    USE orbital_transformation_matrices, ONLY: init_spherical_harmonics
      28              :    USE qs_grid_atom,                    ONLY: grid_atom_type
      29              :    USE qs_harmonics_atom,               ONLY: harmonics_atom_type
      30              :    USE qs_vxc_atom,                     ONLY: add_gapw_atom_grid_interpolation_adjoint,&
      31              :                                               atom_grid_image_bounds,&
      32              :                                               interpolate_gapw_atom_grid_fields
      33              :    USE spherical_harmonics,             ONLY: y_lm
      34              : 
      35              :    IMPLICIT NONE
      36              : 
      37              :    INTEGER, PARAMETER :: nr = 1601, ng = 32
      38              :    REAL(dp), PARAMETER :: alpha = 1.4_dp, beta = 0.55_dp, box = 12.0_dp, dr = 0.00625_dp
      39              :    REAL(dp), PARAMETER :: charges(2) = [-1.0_dp, 0.3_dp]
      40              :    REAL(dp) :: rad2(nr), weights(nr), delta_rho(nr), potential(nr), shifted(nr)
      41              :    REAL(dp) :: q, g2, periodic_difference, local_difference, correction, error, h, eplus, eminus
      42              :    INTEGER :: i, ix, iy, iz, io_unit
      43              : 
      44            2 :    CALL cp2k_init()
      45            2 :    io_unit = cp_logger_get_default_io_unit()
      46            2 :    CALL check_1c_basis()
      47            2 :    CALL check_atom_grid_adjoint()
      48            2 :    CALL check_atom_grid_images()
      49         3204 :    DO i = 1, nr
      50         3204 :       rad2(i) = ((i - 1)*dr)**2
      51              :    END DO
      52         3204 :    weights = 2.0_dp*dr/3.0_dp
      53         1602 :    weights(2:nr - 1:2) = 4.0_dp*dr/3.0_dp
      54            2 :    weights(1) = dr/3.0_dp
      55            2 :    weights(nr) = dr/3.0_dp
      56         3204 :    weights = weights*rad2
      57              : 
      58            6 :    DO i = 1, SIZE(charges)
      59            4 :       q = charges(i)
      60              :       ! Electron-minus-nuclear l=0 density. Both normalized Gaussians carry q,
      61              :       ! so their difference has zero monopole and a nonzero second moment.
      62              :       delta_rho = q*SQRT(fourpi)*((alpha/pi)**1.5_dp*EXP(-alpha*rad2) - &
      63         6408 :                                   (beta/pi)**1.5_dp*EXP(-beta*rad2))
      64            4 :       potential = gapw_background_potential(-q, box**3, rad2)
      65         6408 :       correction = SUM(potential*delta_rho*weights)
      66              : 
      67              :       ! Independent reference: exact reciprocal-space Coulomb energies of the
      68              :       ! two periodic Gaussians with G=0 omitted, minus their free-space energies.
      69              :       periodic_difference = 0.0_dp
      70          264 :       DO iz = -ng, ng
      71        17164 :          DO iy = -ng, ng
      72      1115660 :             DO ix = -ng, ng
      73      1098500 :                IF (ix == 0 .AND. iy == 0 .AND. iz == 0) CYCLE
      74      1098496 :                g2 = (2.0_dp*pi/box)**2*REAL(ix*ix + iy*iy + iz*iz, dp)
      75              :                periodic_difference = periodic_difference + &
      76      1115400 :                                      2.0_dp*pi*q*q/box**3/g2*(EXP(-g2/(2.0_dp*alpha)) - EXP(-g2/(2.0_dp*beta)))
      77              :             END DO
      78              :          END DO
      79              :       END DO
      80            4 :       local_difference = q*q*(SQRT(alpha) - SQRT(beta))/SQRT(2.0_dp*pi)
      81            4 :       error = ABS(periodic_difference - local_difference - correction)
      82            4 :       IF (io_unit > 0) WRITE (io_unit, '(A,F8.3,ES16.7)') "GAPW periodic Gaussian background:", q, error
      83            4 :       IF (error > 1.e-12_dp) ERROR STOP "GAPW background disagrees with periodic Gaussian electrostatics"
      84              : 
      85              :       ! The explicit volume derivative contributes E_background to each
      86              :       ! diagonal component of -dE/dstrain (local radial functions held fixed).
      87            4 :       h = 1.e-5_dp
      88            4 :       shifted = gapw_background_potential(-q, box**3*(1.0_dp + h), rad2)
      89         6408 :       eplus = SUM(shifted*delta_rho*weights)
      90            4 :       shifted = gapw_background_potential(-q, box**3*(1.0_dp - h), rad2)
      91         6408 :       eminus = SUM(shifted*delta_rho*weights)
      92            6 :       IF (ABS(-(eplus - eminus)/(2.0_dp*h) - correction) > 1.e-11_dp) THEN
      93            0 :          ERROR STOP "GAPW background volume derivative failed"
      94              :       END IF
      95              :    END DO
      96            2 :    potential = gapw_background_potential(0.0_dp, box**3, rad2)
      97         3204 :    IF (ANY(potential /= 0.0_dp)) ERROR STOP "Neutral cell has a background potential"
      98            2 :    CALL cp2k_finalize()
      99              : CONTAINS
     100              : 
     101              : ! **************************************************************************************************
     102              : !> \brief Check that one-centre basis extension retains diffuse exponents without duplicates.
     103              : ! **************************************************************************************************
     104            2 :    SUBROUTINE check_1c_basis()
     105              :       INTEGER, PARAMETER                                 :: expected(4) = [3, 4, 5, 7]
     106              :       REAL(KIND=dp), PARAMETER :: spacing(4) = [2.5_dp, 2.0_dp, 1.75_dp, 1.5_dp]
     107              : 
     108              :       INTEGER                                            :: level
     109              :       REAL(KIND=dp)                                      :: diffuse, ratio
     110              :       TYPE(gto_basis_set_type), POINTER                  :: extended, orbitals, seed
     111              : 
     112            2 :       NULLIFY (extended, orbitals, seed)
     113            2 :       CALL init_orbital_pointers(5)
     114            2 :       CALL init_spherical_harmonics(5, 0)
     115           10 :       DO level = 1, 4
     116            8 :          ratio = SPACING(level)
     117            8 :          CALL allocate_gto_basis_set(seed)
     118            8 :          seed%name = 'two-s-primitives'
     119            8 :          seed%norm_type = 2
     120            8 :          seed%nset = 1
     121            8 :          ALLOCATE (seed%lmin(1), seed%lmax(1), seed%npgf(1), seed%zet(2, 1))
     122           16 :          seed%lmin = 0
     123           16 :          seed%lmax = 0
     124           16 :          seed%npgf = 2
     125              :          ! The second extension candidate duplicates the second soft exponent.
     126              :          ! More diffuse, well-separated candidates must still be admitted.
     127           24 :          seed%zet(:, 1) = [1.0_dp, 1.0_dp/SQRT(ratio)]
     128            8 :          CALL create_primitive_basis_set(seed, orbitals)
     129           16 :          orbitals%set_radius = 0.0_dp
     130            8 :          CALL create_1c_basis(orbitals, orbitals, extended, level)
     131            8 :          diffuse = 1.0_dp/(ratio*SQRT(ratio))
     132            8 :          IF (io_unit > 0) WRITE (io_unit, '(A,I2,A,I3)') &
     133            4 :             'GAPW extension level ', level, ': added s primitives ', extended%npgf(2)
     134            8 :          IF (extended%npgf(2) /= expected(level)) ERROR STOP 'GAPW extension lost admissible diffuse exponents'
     135           46 :          IF (MINVAL(ABS(extended%zet(:extended%npgf(2), 2) - diffuse)) > 1.e-12_dp) THEN
     136            0 :             ERROR STOP 'GAPW extension is missing the first admissible diffuse exponent'
     137              :          END IF
     138           46 :          IF (MINVAL(ABS(extended%zet(:extended%npgf(2), 2) - seed%zet(2, 1))) < 1.e-12_dp) THEN
     139            0 :             ERROR STOP 'GAPW extension duplicated an existing soft exponent'
     140              :          END IF
     141            8 :          CALL deallocate_gto_basis_set(extended)
     142            8 :          CALL deallocate_gto_basis_set(orbitals)
     143           10 :          CALL deallocate_gto_basis_set(seed)
     144              :       END DO
     145            2 :    END SUBROUTINE check_1c_basis
     146              : 
     147              : ! **************************************************************************************************
     148              : !> \brief Check the hard-minus-soft density, gradient and tau interpolation transpose.
     149              : ! **************************************************************************************************
     150            2 :    SUBROUTINE check_atom_grid_adjoint()
     151              :       INTEGER, PARAMETER                                 :: nrad = 7
     152              :       REAL(dp), PARAMETER :: radii(5) = [0.0_dp, 0.06_dp, 1.5_dp, 4.99_dp, 5.01_dp]
     153              : 
     154              :       INTEGER                                            :: a, ir, iso, na, ns, order, sample, spin
     155              :       REAL(dp)                                           :: d(2), da(2), ds(3, 2), g(3, 2), &
     156              :                                                             ga(3, 2), gs(3, 3, 2), lhs(3), &
     157              :                                                             max_error, rhs(3), t(2), ta(2), &
     158              :                                                             ts(3, 2), x(3)
     159            2 :       REAL(dp), ALLOCATABLE :: grad_h(:, :, :, :), grad_s(:, :, :, :), rho_h(:, :, :), &
     160            2 :          rho_s(:, :, :), tau_h(:, :, :), tau_s(:, :, :), v_h(:, :, :), v_s(:, :, :), &
     161            2 :          vg_h(:, :, :, :), vg_s(:, :, :, :), vt_h(:, :, :), vt_s(:, :, :)
     162              :       TYPE(grid_atom_type), POINTER                      :: grid
     163              :       TYPE(harmonics_atom_type), POINTER                 :: harmonics
     164              : 
     165            2 :       CALL init_lebedev_grids()
     166            2 :       ALLOCATE (grid, harmonics)
     167            2 :       na = lebedev_grid(5)%n
     168            2 :       grid%nr = nrad
     169            2 :       grid%ng_sphere = na
     170            2 :       harmonics%max_s_harm = 25
     171           10 :       ALLOCATE (grid%rad(nrad), grid%wa(na), harmonics%slm(na, 25))
     172           16 :       grid%rad = [(0.05_dp + 4.95_dp*(REAL(ir - 1, dp)/REAL(nrad - 1, dp))**2, ir=1, nrad)]
     173          102 :       grid%wa = fourpi*lebedev_grid(5)%w
     174           52 :       DO iso = 1, 25
     175         2552 :          DO a = 1, na
     176         2550 :             CALL y_lm(lebedev_grid(5)%r(:, a), harmonics%slm(a, iso), indso(1, iso), indso(2, iso))
     177              :          END DO
     178              :       END DO
     179              :       ALLOCATE (rho_h(na, nrad, 2), rho_s(na, nrad, 2), tau_h(na, nrad, 2), tau_s(na, nrad, 2), &
     180              :                 v_h(na, nrad, 2), v_s(na, nrad, 2), vt_h(na, nrad, 2), vt_s(na, nrad, 2), &
     181           36 :                 grad_h(3, na, nrad, 2), grad_s(3, na, nrad, 2), vg_h(3, na, nrad, 2), vg_s(3, na, nrad, 2))
     182            6 :       DO spin = 1, 2
     183           34 :          DO ir = 1, nrad
     184         1432 :             DO a = 1, na
     185         1400 :                rho_h(a, ir, spin) = SIN(REAL(a + ir + spin, dp))
     186         1400 :                rho_s(a, ir, spin) = COS(REAL(a - ir + spin, dp))
     187         5600 :                grad_h(:, a, ir, spin) = rho_h(a, ir, spin)*[0.3_dp, -0.2_dp, 0.7_dp]
     188         5628 :                grad_s(:, a, ir, spin) = rho_s(a, ir, spin)*[-0.7_dp, 0.4_dp, 0.1_dp]
     189              :             END DO
     190              :          END DO
     191              :       END DO
     192         1434 :       tau_h(:, :, :) = 0.4_dp*rho_h
     193         1434 :       tau_s(:, :, :) = 0.7_dp*rho_s
     194            2 :       da = [0.3_dp, -0.2_dp]
     195            2 :       ga = RESHAPE([0.1_dp, 0.7_dp, -0.3_dp, 0.8_dp, -0.2_dp, 0.3_dp], [3, 2])
     196            2 :       ta = [0.9_dp, -0.6_dp]
     197            2 :       max_error = 0.0_dp
     198            6 :       DO order = 1, 2
     199           12 :          DO ns = 1, 2
     200           52 :             DO sample = 1, SIZE(radii)
     201          160 :                x = radii(sample)*[2.0_dp, 3.0_dp, 6.0_dp]/7.0_dp
     202              :                CALL interpolate_gapw_atom_grid_fields(grid, harmonics, x, 5.0_dp, ns, &
     203              :                                                       rho_h, rho_s, grad_h, grad_s, tau_h, tau_s, &
     204           40 :                                                       d, g, t, ds, gs, ts, calculate_spatial=.FALSE.)
     205           40 :                v_h = 0.0_dp
     206           40 :                v_s = 0.0_dp
     207           40 :                vg_h = 0.0_dp
     208           40 :                vg_s = 0.0_dp
     209           40 :                vt_h = 0.0_dp
     210           40 :                vt_s = 0.0_dp
     211              :                CALL add_gapw_atom_grid_interpolation_adjoint(grid, harmonics, x, 5.0_dp, ns, &
     212           40 :                                                              da, ga, ta, v_h, v_s, vg_h, vg_s, vt_h, vt_s)
     213          640 :                lhs = [SUM(d*da), SUM(g*ga), SUM(t*ta)]
     214              :                rhs = [SUM(v_h*rho_h) - SUM(v_s*rho_s), SUM(vg_h*grad_h) - SUM(vg_s*grad_s), &
     215       340000 :                       SUM(vt_h*tau_h) - SUM(vt_s*tau_s)]
     216          160 :                IF (sample == 3 .AND. MINVAL(ABS(lhs)) < 1.e-8_dp) ERROR STOP 'Atom-grid fixture is inactive'
     217           40 :                IF (sample == 1 .OR. sample == 5) THEN
     218          128 :                   IF (ANY(lhs /= 0.0_dp) .OR. ANY(rhs /= 0.0_dp)) ERROR STOP 'Inactive atom-grid point contributed'
     219              :                END IF
     220          168 :                max_error = MAX(max_error, MAXVAL(ABS(lhs - rhs)/MAX(1.0_dp, ABS(lhs), ABS(rhs))))
     221              :             END DO
     222              :          END DO
     223           62 :          grid%rad = grid%rad(nrad:1:-1)
     224              :       END DO
     225            2 :       IF (io_unit > 0) WRITE (io_unit, '(A,ES16.7)') 'GAPW atom-grid adjoint error: ', max_error
     226            2 :       IF (max_error > 2.e-13_dp) ERROR STOP 'GAPW interpolation transpose identity failed'
     227            2 :       DEALLOCATE (grid%rad, grid%wa, harmonics%slm)
     228            2 :       DEALLOCATE (grid, harmonics)
     229            2 :    END SUBROUTINE check_atom_grid_adjoint
     230              : 
     231              : ! **************************************************************************************************
     232              : !> \brief Ensure periodic-image screening retains every image within the Cartesian cutoff.
     233              : ! **************************************************************************************************
     234            2 :    SUBROUTINE check_atom_grid_images()
     235              :       INTEGER, PARAMETER                                 :: masks(3) = [0, 3, 7]
     236              : 
     237              :       INTEGER                                            :: center(3), hi(3), i1, i2, i3, lo(3), &
     238              :                                                             mask, p, sample, shell(3), shift(3)
     239              :       REAL(dp)                                           :: cutoff, fractional(3), point(3)
     240              :       TYPE(cell_type), POINTER                           :: cell
     241              : 
     242          128 :       ALLOCATE (cell)
     243           26 :       cell%hmat = RESHAPE([8.0_dp, 0.0_dp, 0.0_dp, 3.2_dp, 8.0_dp, 0.0_dp, 0.0_dp, 1.6_dp, 8.0_dp], [3, 3])
     244           26 :       cell%h_inv = inv_3x3(cell%hmat)
     245            8 :       DO mask = 1, SIZE(masks)
     246           42 :          cell%perd = [(MERGE(1, 0, BTEST(masks(mask), p)), p=0, 2)]
     247           26 :          DO sample = 1, 3
     248              :             fractional = [0.5_dp, -0.5_dp, 0.2_dp] + &
     249           72 :                          REAL(sample - 1, dp)*cell%perd*[100.0_dp, -51.0_dp, 77.0_dp]
     250          234 :             point = MATMUL(cell%hmat, fractional)
     251           72 :             center = cell%perd*NINT(fractional)
     252          342 :             cutoff = NORM2(point - MATMUL(cell%hmat, REAL(center, dp)))
     253          234 :             shell = cell%perd*(CEILING(cutoff*SQRT(SUM(cell%h_inv**2, DIM=2))) + 1)
     254           18 :             CALL atom_grid_image_bounds(cell, fractional, cutoff, shell, lo, hi)
     255          144 :             IF (ANY(lo < center - shell) .OR. ANY(hi > center + shell)) ERROR STOP 'Image bounds expanded'
     256           66 :             DO i3 = center(3) - shell(3), center(3) + shell(3)
     257          246 :                DO i2 = center(2) - shell(2), center(2) + shell(2)
     258         1134 :                   DO i1 = center(1) - shell(1), center(1) + shell(1)
     259         3624 :                      shift = [i1, i2, i3]
     260        17214 :                      IF (NORM2(point - MATMUL(cell%hmat, REAL(shift, dp))) > cutoff) CYCLE
     261          426 :                      IF (ANY(shift < lo) .OR. ANY(shift > hi)) ERROR STOP 'Image within support omitted'
     262              :                   END DO
     263              :                END DO
     264              :             END DO
     265              :          END DO
     266              :       END DO
     267            2 :       DEALLOCATE (cell)
     268            2 :    END SUBROUTINE check_atom_grid_images
     269              : END PROGRAM gapw_unittest
        

Generated by: LCOV version 2.0-1