LCOV - code coverage report
Current view: top level - src - kpoint_lattice_fft_unittest.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 91.8 % 267 245
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 4 4

            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            2 : PROGRAM kpoint_lattice_fft_unittest
       8            2 :    USE fft_api,                         ONLY: FFT_LIBRARY_BACKEND_DEFAULT,&
       9              :                                               FFT_LIBRARY_FFTW_MODE_ESTIMATE
      10              :    USE fft_tools,                       ONLY: finalize_fft,&
      11              :                                               init_fft
      12              :    USE kinds,                           ONLY: dp,&
      13              :                                               int_8
      14              :    USE kpoint_lattice_fft,              ONLY: cell_to_k_grid_fft,&
      15              :                                               k_grid_to_cell_execute,&
      16              :                                               k_grid_to_cell_prepare,&
      17              :                                               k_grid_to_cell_release,&
      18              :                                               k_grid_to_cell_work_type,&
      19              :                                               lattice_fft_profile_type
      20              :    USE lattice_preconditioner_operator, ONLY: apply_lattice_inverse_dense,&
      21              :                                               apply_lattice_state_inverse_dense,&
      22              :                                               build_lattice_inverse,&
      23              :                                               build_lattice_local_correction,&
      24              :                                               lattice_fft_cost_ratio,&
      25              :                                               lattice_grid
      26              :    USE mathconstants,                   ONLY: gaussi,&
      27              :                                               twopi,&
      28              :                                               z_zero
      29              :    USE message_passing,                 ONLY: mp_comm_type,&
      30              :                                               mp_world_finalize,&
      31              :                                               mp_world_init
      32              :    USE offload_api,                     ONLY: offload_get_device_count,&
      33              :                                               offload_set_chosen_device
      34              : 
      35              :    IMPLICIT NONE
      36              : 
      37              :    INTEGER, PARAMETER                                  :: base_block = 2, base_cell = 8, base_n = 16, &
      38              :                                                           base_ncol = 3, ncell = 31, nkp = 64, &
      39              :                                                           nselected = 3
      40            2 :    COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)   :: inverse_k
      41            2 :    COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: state_inverse_k
      42              :    COMPLEX(KIND=dp), DIMENSION(2, 3, nkp)              :: direct, transformed
      43              :    COMPLEX(KIND=dp), DIMENSION(2, 3, nselected)         :: selected_transformed
      44              :    INTEGER                                             :: d, i, icell, info, j, jcell, ik, ix, iy, iz, &
      45              :                                                           inverse_case, inverse_nkp, status
      46            2 :    INTEGER, ALLOCATABLE, DIMENSION(:)                  :: correction_indices
      47              :    INTEGER, DIMENSION(3)                               :: base_dims, delta
      48              :    INTEGER, DIMENSION(3, base_cell)                    :: base_index
      49              :    INTEGER, DIMENSION(3)                               :: nkp_grid
      50              :    INTEGER, DIMENSION(3, ncell)                        :: index_to_cell
      51              :    INTEGER, DIMENSION(nselected)                       :: selected_kpoints
      52              :    LOGICAL                                             :: used_fft
      53              :    REAL(KIND=dp), DIMENSION(3, 3)                      :: hmat
      54              :    REAL(KIND=dp)                                       :: corrected_projection_error, cost_ratio, &
      55              :                                                           imaginary_residual, projection_error
      56              :    REAL(KIND=dp), DIMENSION(3, base_cell)              :: base_xkp
      57              :    REAL(KIND=dp), DIMENSION(base_n, base_n)            :: base_overlap
      58              :    REAL(KIND=dp), DIMENSION(base_n, base_n)            :: defect_operator
      59              :    REAL(KIND=dp), DIMENSION(base_n, base_ncol)          :: base_input, base_output, base_state_output
      60            2 :    REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: coarse_inverse, local_operator
      61              :    REAL(KIND=dp), DIMENSION(base_block, base_block)    :: coarse_residual
      62              :    REAL(KIND=dp), DIMENSION(3, nkp)                    :: xkp, xkp_bad
      63              :    REAL(KIND=dp), DIMENSION(2, 3, ncell)               :: values_rs
      64              :    TYPE(mp_comm_type)                                  :: mp_comm
      65            8 :    TYPE(k_grid_to_cell_work_type)                      :: inverse_work
      66              :    REAL(KIND=dp), DIMENSION(9)                         :: subset_weights
      67              :    COMPLEX(KIND=dp), DIMENSION(2, 3, ncell)            :: weighted_reference, weighted_result
      68              :    REAL(KIND=dp)                                       :: real_result(2, 3, ncell)
      69              :    REAL(KIND=dp)                                       :: inverse_shift(3), inverse_weights(nkp)
      70              :    TYPE(lattice_fft_profile_type)                      :: inverse_profile
      71              : 
      72            2 :    CALL mp_world_init(mp_comm)
      73              :    ! Select the active offload device before initializing the FFT library.
      74            2 :    IF (offload_get_device_count() > 0) THEN
      75            0 :       CALL offload_set_chosen_device(MOD(mp_comm%mepos, offload_get_device_count()))
      76              :    END IF
      77            2 :    CALL check_image_reconstruction(mp_comm)
      78              :    CALL init_fft(FFT_LIBRARY_BACKEND_DEFAULT, alltoall=.FALSE., pool_limit=4, &
      79            2 :                  wisdom_file="", plan_style=FFT_LIBRARY_FFTW_MODE_ESTIMATE, status=status)
      80            2 :    IF (status /= 0) ERROR STOP "FFT library initalization step failed"
      81              : 
      82            2 :    nkp_grid = [4, 4, 4]
      83              :    hmat = RESHAPE([4.0_dp, 0.1_dp, 0.2_dp, &
      84              :                    0.0_dp, 5.0_dp, 0.3_dp, &
      85            2 :                    0.0_dp, 0.0_dp, 6.0_dp], [3, 3])
      86              : 
      87            2 :    ik = 0
      88           10 :    DO iz = 0, nkp_grid(3) - 1
      89           42 :       DO iy = 0, nkp_grid(2) - 1
      90          168 :          DO ix = 0, nkp_grid(1) - 1
      91          128 :             ik = ik + 1
      92              :             xkp(:, ik) = [(REAL(ix, dp) + 0.5_dp)/REAL(nkp_grid(1), dp), &
      93              :                           REAL(iy, dp)/REAL(nkp_grid(2), dp), &
      94          544 :                           (REAL(iz, dp) + 0.25_dp)/REAL(nkp_grid(3), dp)]
      95              :          END DO
      96              :       END DO
      97              :    END DO
      98              : 
      99           64 :    DO icell = 1, ncell
     100              :       index_to_cell(:, icell) = [MODULO(3*icell + 1, 11) - 5, &
     101              :                                  MODULO(5*icell + 2, 9) - 4, &
     102          248 :                                  MODULO(2*icell + 3, 7) - 3]
     103          250 :       DO d = 1, 3
     104          186 :          values_rs(1, d, icell) = SIN(0.13_dp*REAL(icell + 2*d, dp))
     105          248 :          values_rs(2, d, icell) = COS(0.17_dp*REAL(2*icell - d, dp))
     106              :       END DO
     107              :    END DO
     108              : 
     109            2 :    CALL cell_to_k_grid_fft(values_rs, index_to_cell, xkp, nkp_grid, transformed, used_fft)
     110            2 :    IF (.NOT. used_fft) ERROR STOP "Complete regular grid did not use lattice FFT"
     111            2 :    CALL direct_reference(values_rs, index_to_cell, xkp, direct)
     112         1282 :    IF (MAXVAL(ABS(transformed - direct)) > 2.0E-12_dp) THEN
     113            0 :       ERROR STOP "Shifted lattice FFT differs from direct transform"
     114              :    END IF
     115              : 
     116            2 :    selected_kpoints = [64, 2, 31]
     117              :    ! A reduced request retains the full mesh spacing and shift, including a single point.
     118              :    CALL cell_to_k_grid_fft(values_rs, index_to_cell, xkp(:, selected_kpoints), nkp_grid, &
     119           26 :                            selected_transformed, used_fft)
     120            2 :    IF (.NOT. used_fft) ERROR STOP "Regular-grid subset did not use lattice FFT"
     121           62 :    IF (MAXVAL(ABS(selected_transformed - direct(:, :, selected_kpoints))) > 2.0E-12_dp) THEN
     122            0 :       ERROR STOP "Subset lattice FFT differs from direct transform"
     123              :    END IF
     124              :    CALL cell_to_k_grid_fft(values_rs, index_to_cell, xkp(:, selected_kpoints), nkp_grid, &
     125           26 :                            selected_transformed(:, :, 1:2), used_fft, selected_kpoints=[3, 1])
     126            2 :    IF (.NOT. used_fft) ERROR STOP "Selection within a subset did not use lattice FFT"
     127           46 :    IF (MAXVAL(ABS(selected_transformed(:, :, 1:2) - direct(:, :, selected_kpoints([3, 1])))) > 2.0E-12_dp) THEN
     128            0 :       ERROR STOP "Subset selection confused local and full-grid indices"
     129              :    END IF
     130            2 :    CALL cell_to_k_grid_fft(values_rs, index_to_cell, xkp(:, 31:31), nkp_grid, selected_transformed(:, :, 1:1), used_fft)
     131            2 :    IF (.NOT. used_fft) ERROR STOP "Single requested grid point did not use lattice FFT"
     132           20 :    IF (MAXVAL(ABS(selected_transformed(:, :, 1) - direct(:, :, 31))) > 2.0E-12_dp) THEN
     133            0 :       ERROR STOP "Single-point lattice FFT lost the mesh shift"
     134              :    END IF
     135              : 
     136              :    CALL cell_to_k_grid_fft(values_rs, index_to_cell, xkp, nkp_grid, selected_transformed, &
     137            2 :                            used_fft, 2, hmat, selected_kpoints=selected_kpoints)
     138            2 :    IF (.NOT. used_fft) ERROR STOP "Selected derivative did not use lattice FFT"
     139            2 :    CALL direct_reference(values_rs, index_to_cell, xkp, direct, 2, hmat)
     140           62 :    IF (MAXVAL(ABS(selected_transformed - direct(:, :, selected_kpoints))) > 5.0E-12_dp) THEN
     141            0 :       ERROR STOP "Selected lattice-FFT derivative differs from direct transform"
     142              :    END IF
     143              :    CALL cell_to_k_grid_fft(values_rs, index_to_cell, xkp(:, selected_kpoints), nkp_grid, &
     144           26 :                            selected_transformed, used_fft, 2, hmat)
     145            2 :    IF (.NOT. used_fft) ERROR STOP "Subset derivative did not use lattice FFT"
     146           62 :    IF (MAXVAL(ABS(selected_transformed - direct(:, :, selected_kpoints))) > 5.0E-12_dp) THEN
     147            0 :       ERROR STOP "Subset lattice-FFT derivative differs from direct transform"
     148              :    END IF
     149              : 
     150            2 :    xkp_bad = xkp
     151            2 :    xkp_bad(1, nkp) = xkp_bad(1, nkp) + 0.03125_dp
     152            2 :    CALL cell_to_k_grid_fft(values_rs, index_to_cell, xkp_bad, nkp_grid, transformed, used_fft)
     153            2 :    IF (used_fft) ERROR STOP "Irregular k-point list incorrectly used lattice FFT"
     154            2 :    CALL direct_reference(values_rs, index_to_cell, xkp_bad, direct)
     155         1282 :    IF (MAXVAL(ABS(transformed - direct)) > 1.0E-14_dp) THEN
     156            0 :       ERROR STOP "Direct lattice-transform fallback is inconsistent"
     157              :    END IF
     158              : 
     159              :    ! An incomplete shifted grid must use the supplied, non-normalized weights.
     160              :    ! Include negative translations and reuse preparation for separate matrix batches.
     161            2 :    subset_weights = [(REAL(ik, dp)/23.0_dp, ik=1, 9)]
     162          182 :    transformed(:, :, 1:9) = CMPLX(values_rs(:, :, 1:9), values_rs(:, :, 2:10), KIND=dp)
     163            2 :    weighted_reference = z_zero
     164           64 :    DO icell = 1, ncell
     165          622 :       DO ik = 1, 9
     166              :          weighted_reference(:, :, icell) = weighted_reference(:, :, icell) + subset_weights(ik)* &
     167              :                                            EXP(-gaussi*twopi*SUM(xkp(:, ik)*REAL(index_to_cell(:, icell), dp)))* &
     168         7316 :                                            transformed(:, :, ik)
     169              :       END DO
     170              :    END DO
     171              :    CALL k_grid_to_cell_prepare(inverse_work, xkp(:, 1:9), nkp_grid, index_to_cell, &
     172            2 :                                weights=subset_weights, allow_incomplete=.TRUE.)
     173            6 :    DO i = 1, 2
     174            4 :       CALL k_grid_to_cell_execute(inverse_work, transformed(i:i, :, 1:9), weighted_result(i:i, :, :), used_fft)
     175            6 :       IF (.NOT. used_fft) ERROR STOP "Prepared incomplete grid did not use FFT"
     176              :    END DO
     177          622 :    IF (MAXVAL(ABS(weighted_result - weighted_reference)) > 3.0E-12_dp) THEN
     178            0 :       ERROR STOP "Prepared weighted inverse differs from explicit phase sum"
     179              :    END IF
     180              :    ! A one-element FFT panel covers extraction across batch boundaries.
     181              :    CALL k_grid_to_cell_execute(inverse_work, transformed(:, :, 1:9), values_real=real_result, &
     182            2 :                                max_workspace_bytes=48_int_8*nkp)
     183          622 :    IF (MAXVAL(ABS(real_result - REAL(weighted_reference, dp))) > 3.0E-12_dp) THEN
     184            0 :       ERROR STOP "Tiled inverse extraction differs from the original real field"
     185              :    END IF
     186            2 :    real_result = values_rs
     187              :    CALL k_grid_to_cell_execute(inverse_work, transformed(:, :, 1:9), values_real=real_result, &
     188            2 :                                max_workspace_bytes=48_int_8*nkp)
     189          622 :    IF (MAXVAL(ABS(real_result - REAL(weighted_reference, dp))) > 3.0E-12_dp) THEN
     190            0 :       ERROR STOP "Repeated inverse extraction retained the previous real output"
     191              :    END IF
     192              :    CALL k_grid_to_cell_execute(inverse_work, transformed(1:0, :, 1:9), values_real=real_result(1:0, :, :), &
     193            2 :                                used_fft=used_fft)
     194            2 :    IF (.NOT. used_fft) ERROR STOP "An empty inverse panel did not complete"
     195            2 :    CALL k_grid_to_cell_release(inverse_work)
     196              : 
     197              :    ! Real-output pairing preserves Re(T[A]) even for arbitrary complex input with
     198              :    ! unequal +/-k weights. Cover Gamma, mixed half shifts, odd meshes and a quarter
     199              :    ! shift that must retain the unpaired execution path; native k order is permuted.
     200           10 :    DO inverse_case = 1, 4
     201            8 :       nkp_grid = [4, 3, 2]
     202            8 :       inverse_shift = 0.0_dp
     203            8 :       IF (inverse_case == 2) inverse_shift = [0.5_dp, 0.0_dp, 0.5_dp]
     204            8 :       IF (inverse_case == 3) THEN
     205            2 :          nkp_grid = [3, 3, 1]
     206            2 :          inverse_shift = [0.5_dp, 0.5_dp, 0.0_dp]
     207              :       END IF
     208            8 :       IF (inverse_case == 4) inverse_shift = [0.25_dp, 0.0_dp, 0.0_dp]
     209           32 :       inverse_nkp = PRODUCT(nkp_grid)
     210            8 :       ik = 0
     211           22 :       DO iz = 0, nkp_grid(3) - 1
     212           64 :          DO iy = 0, nkp_grid(2) - 1
     213          218 :             DO ix = 0, nkp_grid(1) - 1
     214          162 :                ik = ik + 1
     215          162 :                j = MODULO(5*(ik - 1) + 1, inverse_nkp) + 1
     216          690 :                xkp(:, j) = (REAL([ix, iy, iz], dp) + inverse_shift)/REAL(nkp_grid, dp)
     217              :             END DO
     218              :          END DO
     219              :       END DO
     220          170 :       DO ik = 1, inverse_nkp
     221          162 :          inverse_weights(ik) = REAL(2*ik - 5, dp)/REAL(3*inverse_nkp, dp)
     222          656 :          DO d = 1, 3
     223          486 :             transformed(1, d, ik) = CMPLX(SIN(REAL(ik + d, dp)), COS(REAL(2*ik - d, dp)), dp)
     224          648 :             transformed(2, d, ik) = CMPLX(COS(REAL(ik - d, dp)), SIN(REAL(3*ik + d, dp)), dp)
     225              :          END DO
     226              :       END DO
     227            8 :       weighted_reference = z_zero
     228          256 :       DO icell = 1, ncell
     229         5278 :          DO ik = 1, inverse_nkp
     230              :             weighted_reference(:, :, icell) = weighted_reference(:, :, icell) + inverse_weights(ik)* &
     231              :                                               EXP(-gaussi*twopi*SUM(xkp(:, ik)*REAL(index_to_cell(:, icell), dp)))* &
     232        65534 :                                               transformed(:, :, ik)
     233              :          END DO
     234              :       END DO
     235              :       CALL k_grid_to_cell_prepare(inverse_work, xkp(:, 1:inverse_nkp), nkp_grid, index_to_cell, &
     236            8 :                                   weights=inverse_weights(1:inverse_nkp))
     237          104 :       inverse_profile = lattice_fft_profile_type()
     238              :       CALL k_grid_to_cell_execute(inverse_work, transformed(:, :, 1:inverse_nkp), values_real=real_result, &
     239            8 :                                   profile=inverse_profile)
     240         2488 :       IF (MAXVAL(ABS(real_result - REAL(weighted_reference, dp))) > 2.0E-11_dp) THEN
     241            0 :          ERROR STOP "Paired real-output FFT changed the weighted real projection"
     242              :       END IF
     243            8 :       IF (inverse_profile%two_real .NEQV. (inverse_case /= 4)) ERROR STOP "Incorrect real FFT pairing eligibility"
     244            8 :       IF (inverse_case /= 4 .AND. inverse_profile%transforms /= 3_int_8) ERROR STOP "Real FFT transforms were not halved"
     245            8 :       CALL k_grid_to_cell_execute(inverse_work, transformed(:, :, 1:inverse_nkp), weighted_result)
     246         2488 :       IF (MAXVAL(ABS(weighted_result - weighted_reference)) > 2.0E-11_dp) THEN
     247            0 :          ERROR STOP "Real-output pairing changed a subsequent complex inverse transform"
     248              :       END IF
     249              :       ! Three physical elements in two-element panels exercise a lone packed tail.
     250            8 :       real_result = values_rs
     251              :       CALL k_grid_to_cell_execute(inverse_work, transformed(1:1, :, 1:inverse_nkp), values_real=real_result(1:1, :, :), &
     252            8 :                                   max_workspace_bytes=96_int_8*inverse_nkp)
     253         1000 :       IF (MAXVAL(ABS(real_result(1, :, :) - REAL(weighted_reference(1, :, :), dp))) > 3.0E-11_dp) THEN
     254            0 :          ERROR STOP "Paired inverse extraction or odd tail changed the original real field"
     255              :       END IF
     256            8 :       CALL k_grid_to_cell_release(inverse_work)
     257            8 :       CALL k_grid_to_cell_prepare(inverse_work, xkp(:, 1:inverse_nkp), nkp_grid, index_to_cell)
     258            8 :       weighted_reference = z_zero
     259          256 :       DO icell = 1, ncell
     260         5278 :          DO ik = 1, inverse_nkp
     261              :             weighted_reference(:, :, icell) = weighted_reference(:, :, icell) + &
     262              :                                               EXP(-gaussi*twopi*SUM(xkp(:, ik)*REAL(index_to_cell(:, icell), dp)))* &
     263        65534 :                                               transformed(:, :, ik)/REAL(inverse_nkp, dp)
     264              :          END DO
     265              :       END DO
     266            8 :       CALL k_grid_to_cell_execute(inverse_work, transformed(:, :, 1:inverse_nkp), values_real=real_result)
     267         2488 :       IF (MAXVAL(ABS(real_result - REAL(weighted_reference, dp))) > 2.0E-11_dp) THEN
     268            0 :          ERROR STOP "Paired real FFT lost the common uniform-weight normalization"
     269              :       END IF
     270           10 :       CALL k_grid_to_cell_release(inverse_work)
     271              :    END DO
     272              : 
     273              :    ! Verify the block-circulant inverse by applying the original dense operator.
     274            2 :    base_dims = [4, 2, 1]
     275            2 :    CALL lattice_grid(base_dims, base_index, base_xkp)
     276            2 :    base_overlap = 0.0_dp
     277           18 :    DO jcell = 1, base_cell
     278          146 :       DO icell = 1, base_cell
     279          512 :          delta = MODULO(base_index(:, icell) - base_index(:, jcell), base_dims)
     280          384 :          DO i = 1, base_block
     281          512 :             IF (ALL(delta == 0)) THEN
     282              :                base_overlap((icell - 1)*base_block + i, (jcell - 1)*base_block + i) = &
     283           32 :                   2.8_dp + 0.2_dp*REAL(i, KIND=dp)
     284          224 :             ELSE IF ((delta(1) == 1 .OR. delta(1) == base_dims(1) - 1) .AND. &
     285              :                      delta(2) == 0) THEN
     286           64 :                base_overlap((icell - 1)*base_block + i, (jcell - 1)*base_block + i) = -0.12_dp
     287          160 :             ELSE IF (delta(1) == 0 .AND. delta(2) == 1) THEN
     288           32 :                base_overlap((icell - 1)*base_block + i, (jcell - 1)*base_block + i) = -0.08_dp
     289              :             END IF
     290              :          END DO
     291          208 :          IF (ALL(delta == 0)) THEN
     292           16 :             base_overlap((icell - 1)*base_block + 1, (jcell - 1)*base_block + 2) = 0.15_dp
     293           16 :             base_overlap((icell - 1)*base_block + 2, (jcell - 1)*base_block + 1) = 0.15_dp
     294              :          END IF
     295              :       END DO
     296              :    END DO
     297            8 :    DO j = 1, base_ncol
     298          104 :       DO i = 1, base_n
     299          102 :          base_input(i, j) = SIN(0.11_dp*REAL(2*i + 3*j, KIND=dp))
     300              :       END DO
     301              :    END DO
     302            2 :    CALL build_lattice_inverse(base_overlap, base_dims, inverse_k, projection_error, used_fft, info)
     303            2 :    IF (info /= 0) ERROR STOP "Positive lattice overlap failed block Cholesky inversion"
     304            2 :    IF (.NOT. used_fft) ERROR STOP "Block-circulant overlap construction did not use lattice FFT"
     305            2 :    IF (projection_error > 2.0E-15_dp) ERROR STOP "Exact block-circulant overlap was changed by projection"
     306              :    ! Check the large-problem scale without duplicating the heuristic's formula.
     307            2 :    cost_ratio = lattice_fft_cost_ratio([100, 100, 1], 1000, 1000, 16)
     308            2 :    IF (.NOT. (cost_ratio > 1.0E-4_dp .AND. cost_ratio < 2.0E-4_dp)) THEN
     309            0 :       ERROR STOP "Lattice-FFT cost estimate overflowed for a large problem"
     310              :    END IF
     311              :    CALL apply_lattice_inverse_dense(inverse_k, base_dims, base_input, base_output, &
     312            2 :                                     used_fft, imaginary_residual)
     313            2 :    IF (.NOT. used_fft) ERROR STOP "Block-circulant inverse application did not use lattice FFT"
     314            2 :    IF (imaginary_residual > 3.0E-13_dp) ERROR STOP "Lattice inverse did not reconstruct a real matrix"
     315         1742 :    IF (MAXVAL(ABS(MATMUL(base_overlap, base_output) - base_input)) > 2.0E-11_dp) THEN
     316            0 :       ERROR STOP "FFT block-circulant inverse differs from the dense operator inverse"
     317              :    END IF
     318              : 
     319            2 :    ALLOCATE (state_inverse_k(base_block, base_block, base_cell, base_ncol))
     320            8 :    DO j = 1, base_ncol
     321          344 :       state_inverse_k(:, :, :, j) = (0.5_dp + 0.25_dp*REAL(j, KIND=dp))*inverse_k
     322              :    END DO
     323              :    CALL apply_lattice_state_inverse_dense(state_inverse_k, base_dims, base_input, &
     324            2 :                                           base_state_output, used_fft, imaginary_residual)
     325            2 :    IF (.NOT. used_fft) ERROR STOP "State-dependent inverse application did not use lattice FFT"
     326            8 :    DO j = 1, base_ncol
     327          102 :       IF (MAXVAL(ABS(base_state_output(:, j) - &
     328            2 :                      (0.5_dp + 0.25_dp*REAL(j, KIND=dp))*base_output(:, j))) > 3.0E-12_dp) THEN
     329            0 :          ERROR STOP "State-dependent lattice inverse used the wrong column operator"
     330              :       END IF
     331              :    END DO
     332            2 :    DEALLOCATE (state_inverse_k)
     333              : 
     334              :    ! Check the production builder's selected columns and coarse inverse directly.
     335            2 :    defect_operator = base_overlap
     336            2 :    defect_operator(5, 5) = defect_operator(5, 5) + 0.90_dp
     337            2 :    defect_operator(6, 6) = defect_operator(6, 6) + 0.60_dp
     338            2 :    CALL build_lattice_inverse(defect_operator, base_dims, inverse_k, projection_error, used_fft, info)
     339            2 :    IF (info /= 0 .OR. .NOT. used_fft) ERROR STOP "Defective lattice operator failed construction"
     340            2 :    IF (projection_error <= 1.0E-3_dp) ERROR STOP "Localized defect did not break lattice translations"
     341              :    CALL build_lattice_local_correction( &
     342              :       defect_operator, base_dims, 1, correction_indices, local_operator, coarse_inverse, &
     343            2 :       corrected_projection_error, info)
     344            2 :    IF (info /= 0) ERROR STOP "Local correction coarse inverse failed"
     345            2 :    IF (SIZE(correction_indices) /= base_block) ERROR STOP "Local correction has the wrong rank"
     346            6 :    IF (ANY(correction_indices /= [5, 6])) ERROR STOP "Local correction selected the wrong defect cell"
     347            2 :    IF (corrected_projection_error >= projection_error) THEN
     348            0 :       ERROR STOP "Local correction did not reduce the non-circulant residual"
     349              :    END IF
     350              : 
     351           70 :    IF (MAXVAL(ABS(local_operator - defect_operator(:, correction_indices))) > 1.0E-14_dp) THEN
     352            0 :       ERROR STOP "Local correction returned incorrect operator columns"
     353              :    END IF
     354           58 :    coarse_residual = MATMUL(defect_operator(correction_indices, correction_indices), coarse_inverse)
     355            6 :    DO i = 1, base_block
     356            6 :       coarse_residual(i, i) = coarse_residual(i, i) - 1.0_dp
     357              :    END DO
     358           14 :    IF (MAXVAL(ABS(coarse_residual)) > 3.0E-13_dp) ERROR STOP "Local correction coarse inverse is inconsistent"
     359            2 :    DEALLOCATE (coarse_inverse, correction_indices, local_operator)
     360            2 :    DEALLOCATE (inverse_k)
     361              : 
     362            2 :    CALL finalize_fft("")
     363            2 :    CALL mp_world_finalize()
     364              : 
     365              : CONTAINS
     366              : 
     367              : ! **************************************************************************************************
     368              : !> \brief Check triangular image storage and its signed mirror for both neighbor orientations.
     369              : !> \param comm MPI communicator
     370              : ! **************************************************************************************************
     371            2 :    SUBROUTINE check_image_reconstruction(comm)
     372              :       USE cp_dbcsr_api, ONLY: &
     373              :          dbcsr_create, dbcsr_distribution_new, dbcsr_distribution_release, dbcsr_distribution_type, &
     374            2 :          dbcsr_finalize, dbcsr_finalize_lib, dbcsr_init_lib, dbcsr_p_type, dbcsr_put_block, &
     375              :          dbcsr_release, dbcsr_type_antisymmetric, dbcsr_type_symmetric
     376              :       USE cp_log_handling, ONLY: cp_add_default_logger, cp_logger_create, cp_logger_release, &
     377              :                                  cp_logger_type, cp_rm_default_logger
     378              :       USE kpoint_k_r_trafo_simple, ONLY: replicate_rs_matrices
     379              :       USE kpoint_types, ONLY: kpoint_type
     380              :       USE machine, ONLY: default_output_unit
     381              :       USE message_passing, ONLY: mp_para_env_release, mp_para_env_type
     382              :       USE qs_neighbor_list_types, ONLY: add_neighbor_list, add_neighbor_node, &
     383              :                                         allocate_neighbor_list_set, deallocate_neighbor_list_set, neighbor_list_p_type
     384              :       TYPE(mp_comm_type), INTENT(IN)                     :: comm
     385              : 
     386              :       CHARACTER                                          :: matrix_type
     387              :       INTEGER                                            :: atom, image(3), img, io, mode, neighbor, &
     388              :                                                             orientation, owner
     389            2 :       INTEGER, CONTIGUOUS, POINTER                       :: col_dist(:), process_grid(:, :), &
     390            2 :                                                             row_dist(:), sizes(:)
     391              :       REAL(dp)                                           :: expanded(1, 5, 5, 3), expected(5, 5, 3), &
     392              :                                                             sign, values(2, 3)
     393              :       TYPE(cp_logger_type), POINTER                      :: logger
     394              :       TYPE(dbcsr_distribution_type)                      :: distribution
     395            2 :       TYPE(dbcsr_p_type), POINTER                        :: source(:, :)
     396              :       TYPE(kpoint_type), POINTER                         :: points
     397              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     398              :       TYPE(neighbor_list_p_type)                         :: neighbors
     399              : 
     400            0 :       ALLOCATE (para_env)
     401            2 :       CALL para_env%from_dup(comm)
     402            2 :       io = -1
     403            2 :       IF (comm%mepos == 0) io = default_output_unit
     404            2 :       NULLIFY (logger)
     405              :       CALL cp_logger_create(logger, para_env=para_env, default_global_unit_nr=io, &
     406            2 :                             close_global_unit_on_dealloc=.FALSE.)
     407            2 :       CALL cp_add_default_logger(logger)
     408            2 :       CALL dbcsr_init_lib(comm%get_handle(), io)
     409            6 :       ALLOCATE (process_grid(0:comm%num_pe - 1, 0:0), row_dist(2), col_dist(2), sizes(2))
     410           10 :       process_grid(:, 0) = [(img, img=0, comm%num_pe - 1)]
     411            2 :       col_dist = 0
     412            6 :       sizes = [2, 3]
     413           30 :       ALLOCATE (points, source(1, 3))
     414            4 :       ALLOCATE (points%cell_to_index(-1:1, 0:0, 0:0), points%sab_nl(1))
     415            8 :       DO img = 1, 3
     416            6 :          points%cell_to_index(img - 2, 0, 0) = img
     417            8 :          ALLOCATE (source(1, img)%matrix)
     418              :       END DO
     419            2 :       values = RESHAPE([0.3_dp, -0.2_dp, 0.7_dp, 0.1_dp, -0.4_dp, 0.6_dp], [2, 3])
     420            6 :       DO mode = 1, 2
     421            4 :          matrix_type = MERGE(dbcsr_type_symmetric, dbcsr_type_antisymmetric, mode == 1)
     422            4 :          sign = MERGE(1.0_dp, -1.0_dp, mode == 1)
     423           14 :          DO orientation = 1, 2
     424            8 :             owner = MODULO(orientation - 1, comm%num_pe)
     425           24 :             row_dist = [owner, MODULO(owner + 1, comm%num_pe)]
     426              :             CALL dbcsr_distribution_new(distribution, group=comm%get_handle(), pgrid=process_grid, &
     427            8 :                                         row_dist=row_dist, col_dist=col_dist)
     428            8 :             atom = orientation
     429            8 :             neighbor = 3 - orientation
     430           32 :             image = [3 - 2*orientation, 0, 0]
     431            8 :             CALL allocate_neighbor_list_set(points%sab_nl(1)%neighbor_list_set, symmetric=.TRUE.)
     432            8 :             CALL add_neighbor_list(points%sab_nl(1)%neighbor_list_set, atom, neighbors%neighbor_list)
     433           32 :             CALL add_neighbor_node(neighbors%neighbor_list, neighbor, image, REAL(image, dp))
     434           32 :             DO img = 1, 3
     435              :                CALL dbcsr_create(source(1, img)%matrix, name="Image reconstruction", dist=distribution, &
     436           24 :                                  matrix_type=matrix_type, row_blk_size=sizes, col_blk_size=sizes)
     437           24 :                IF (comm%mepos == owner .AND. img == image(1) + 2) THEN
     438            4 :                   CALL dbcsr_put_block(source(1, img)%matrix, 1, 2, values)
     439              :                END IF
     440           32 :                CALL dbcsr_finalize(source(1, img)%matrix)
     441              :             END DO
     442            8 :             expanded = 0.0_dp
     443            8 :             CALL replicate_rs_matrices(source, points, expanded, points%cell_to_index)
     444            8 :             expected = 0.0_dp
     445           80 :             expected(1:2, 3:5, 3) = values
     446           72 :             expected(3:5, 1:2, 1) = sign*TRANSPOSE(values)
     447          752 :             IF (MAXVAL(ABS(expanded(1, :, :, :) - expected)) > 1.e-12_dp) THEN
     448            0 :                ERROR STOP "Image reconstruction depends on neighbor orientation"
     449              :             END IF
     450           32 :             DO img = 1, 3
     451           32 :                CALL dbcsr_release(source(1, img)%matrix)
     452              :             END DO
     453            8 :             CALL dbcsr_distribution_release(distribution)
     454           12 :             CALL deallocate_neighbor_list_set(points%sab_nl(1)%neighbor_list_set)
     455              :          END DO
     456              :       END DO
     457            8 :       DO img = 1, 3
     458            8 :          DEALLOCATE (source(1, img)%matrix)
     459              :       END DO
     460            2 :       DEALLOCATE (points%cell_to_index, points%sab_nl)
     461            2 :       DEALLOCATE (points, source)
     462            2 :       DEALLOCATE (process_grid, row_dist, col_dist, sizes)
     463            2 :       CALL dbcsr_finalize_lib()
     464            2 :       CALL cp_rm_default_logger()
     465            2 :       CALL cp_logger_release(logger)
     466            2 :       CALL mp_para_env_release(para_env)
     467            4 :    END SUBROUTINE check_image_reconstruction
     468              : 
     469              : ! **************************************************************************************************
     470              : !> \brief ...
     471              : !> \param values_rs ...
     472              : !> \param index_to_cell ...
     473              : !> \param xkp ...
     474              : !> \param values_k ...
     475              : !> \param deriv_direction ...
     476              : !> \param hmat ...
     477              : ! **************************************************************************************************
     478            6 :    SUBROUTINE direct_reference(values_rs, index_to_cell, xkp, values_k, deriv_direction, hmat)
     479              : 
     480              :       REAL(KIND=dp), DIMENSION(:, :, :), INTENT(IN)      :: values_rs
     481              :       INTEGER, DIMENSION(:, :), INTENT(IN)               :: index_to_cell
     482              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(IN)         :: xkp
     483              :       COMPLEX(KIND=dp), DIMENSION(:, :, :), INTENT(OUT)  :: values_k
     484              :       INTEGER, INTENT(IN), OPTIONAL                      :: deriv_direction
     485              :       REAL(KIND=dp), DIMENSION(3, 3), INTENT(IN), &
     486              :          OPTIONAL                                        :: hmat
     487              : 
     488              :       COMPLEX(KIND=dp)                                   :: factor
     489              :       INTEGER                                            :: icell, ik
     490              :       REAL(KIND=dp), DIMENSION(3)                        :: cell_index_real, cell_vector
     491              : 
     492         3846 :       values_k = z_zero
     493          390 :       DO ik = 1, SIZE(xkp, 2)
     494        12294 :          DO icell = 1, SIZE(values_rs, 3)
     495        47616 :             factor = EXP(gaussi*twopi*SUM(xkp(:, ik)*REAL(index_to_cell(:, icell), KIND=dp)))
     496        11904 :             IF (PRESENT(deriv_direction)) THEN
     497        15872 :                cell_index_real = REAL(index_to_cell(:, icell), KIND=dp)
     498        51584 :                cell_vector = MATMUL(hmat, cell_index_real)
     499         3968 :                factor = factor*gaussi*cell_vector(deriv_direction)
     500              :             END IF
     501       119424 :             values_k(:, :, ik) = values_k(:, :, ik) + factor*values_rs(:, :, icell)
     502              :          END DO
     503              :       END DO
     504              : 
     505            2 :    END SUBROUTINE direct_reference
     506              : 
     507              : END PROGRAM kpoint_lattice_fft_unittest
        

Generated by: LCOV version 2.0-1