LCOV - code coverage report
Current view: top level - src - kpoint_smearing_unittest.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 92.8 % 83 77
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              : 
       8            2 : PROGRAM kpoint_smearing_unittest
       9            2 :    USE bibliography,                    ONLY: add_all_references
      10              :    USE input_constants,                 ONLY: smear_fermi_dirac,&
      11              :                                               smear_gaussian,&
      12              :                                               smear_mp,&
      13              :                                               smear_mv
      14              :    USE kinds,                           ONLY: dp
      15              :    USE kpoint_methods,                  ONLY: kpoint_smearing_edge_status
      16              :    USE reference_manager,               ONLY: remove_all_references
      17              :    USE scf_control_types,               ONLY: smear_type
      18              :    USE smearing_utils,                  ONLY: Smearkp,&
      19              :                                               smearing_response_weight,&
      20              :                                               smearing_separate_spin,&
      21              :                                               smearing_spin_targets
      22              : 
      23              :    IMPLICIT NONE
      24              : 
      25              :    LOGICAL                                            :: first_fractional, has_weight, last_occupied
      26              :    LOGICAL, DIMENSION(2)                              :: last_occupied_spin
      27              :    REAL(KIND=dp), DIMENSION(2)                        :: weight
      28              :    REAL(KIND=dp), DIMENSION(3, 2, 2)                  :: occupation
      29              :    TYPE(smear_type)                                   :: smear
      30              : 
      31            2 :    CALL add_all_references()
      32            2 :    smear%do_smear = .TRUE.
      33            2 :    smear%eps_fermi_dirac = 1.0E-5_dp
      34            2 :    occupation = 0.0_dp
      35           14 :    occupation(1, :, :) = 1.0_dp
      36            2 :    weight = [1.0_dp, 0.0_dp]
      37              : 
      38            2 :    occupation(1, 2, 1) = 0.5_dp
      39            2 :    occupation(3, 2, 2) = 0.5_dp
      40              :    CALL kpoint_smearing_edge_status(occupation, weight, smear, 2, has_weight, &
      41            2 :                                     first_fractional, last_occupied, last_occupied_spin)
      42            2 :    IF (.NOT. has_weight) ERROR STOP "Positive K-point weight was not detected"
      43            2 :    IF (first_fractional) ERROR STOP "Zero-weight first-band occupation was included"
      44            2 :    IF (last_occupied) ERROR STOP "Zero-weight last-band occupation was included"
      45            6 :    IF (ANY(last_occupied_spin)) ERROR STOP "Zero-weight spin edge was included"
      46              : 
      47            2 :    weight(2) = 0.25_dp
      48              :    CALL kpoint_smearing_edge_status(occupation, weight, smear, 2, has_weight, &
      49            2 :                                     first_fractional, last_occupied, last_occupied_spin)
      50            2 :    IF (.NOT. first_fractional) ERROR STOP "Fractional first band was not detected"
      51            2 :    IF (.NOT. last_occupied) ERROR STOP "Occupied last band was not detected"
      52            2 :    IF (last_occupied_spin(1)) ERROR STOP "Wrong occupied spin edge was reported"
      53            2 :    IF (.NOT. last_occupied_spin(2)) ERROR STOP "Occupied spin edge was not reported"
      54              : 
      55            2 :    occupation = 0.0_dp
      56            6 :    occupation(1, :, 1) = 2.0_dp
      57              :    CALL kpoint_smearing_edge_status(occupation(:, :, 1:1), weight, smear, 1, has_weight, &
      58            2 :                                     first_fractional, last_occupied)
      59            2 :    IF (first_fractional) ERROR STOP "Restricted full occupation was classified as fractional"
      60              : 
      61            2 :    weight = 0.0_dp
      62              :    CALL kpoint_smearing_edge_status(occupation, weight, smear, 2, has_weight, &
      63            2 :                                     first_fractional, last_occupied, last_occupied_spin)
      64            2 :    IF (has_weight .OR. first_fractional .OR. last_occupied) THEN
      65            0 :       ERROR STOP "Zero-weight K-point set produced an edge status"
      66              :    END IF
      67              : 
      68            2 :    smear%do_smear = .FALSE.
      69            6 :    weight = 1.0_dp
      70              :    CALL kpoint_smearing_edge_status(occupation, weight, smear, 2, has_weight, &
      71            2 :                                     first_fractional, last_occupied, last_occupied_spin)
      72            2 :    IF (has_weight .OR. first_fractional .OR. last_occupied) THEN
      73            0 :       ERROR STOP "Disabled smearing produced an edge status"
      74              :    END IF
      75              : 
      76            2 :    CALL test_fixed_n_smearing_response()
      77            2 :    CALL test_fixed_spin_populations()
      78            2 :    CALL remove_all_references()
      79              : 
      80              : CONTAINS
      81              : 
      82              : ! **************************************************************************************************
      83              : !> \brief Check fractional, zero, integer-preserving, and unconstrained spin targets.
      84              : ! **************************************************************************************************
      85            2 :    SUBROUTINE test_fixed_spin_populations()
      86              :       REAL(KIND=dp), DIMENSION(2)                        :: targets
      87              : 
      88            2 :       IF (smearing_separate_spin(-100.0_dp)) ERROR STOP "Unconstrained spin was fixed"
      89            2 :       IF (.NOT. smearing_separate_spin(-1.0_dp)) ERROR STOP "Integer spin populations were released"
      90            2 :       IF (.NOT. smearing_separate_spin(0.0_dp)) ERROR STOP "Zero magnetization was not constrained"
      91            2 :       IF (.NOT. smearing_separate_spin(0.5_dp)) ERROR STOP "Fractional magnetization was not constrained"
      92            2 :       targets(:) = smearing_spin_targets([3, 1], -1.0_dp)
      93            6 :       IF (MAXVAL(ABS(targets - [3.0_dp, 1.0_dp])) > 1.0E-14_dp) THEN
      94            0 :          ERROR STOP "Integer spin targets changed"
      95              :       END IF
      96            2 :       targets(:) = smearing_spin_targets([3, 1], 0.0_dp)
      97            6 :       IF (MAXVAL(ABS(targets - 2.0_dp)) > 1.0E-14_dp) ERROR STOP "Zero-moment targets are wrong"
      98            2 :       targets(:) = smearing_spin_targets([3, 1], 0.5_dp)
      99            6 :       IF (MAXVAL(ABS(targets - [2.25_dp, 1.75_dp])) > 1.0E-14_dp) THEN
     100            0 :          ERROR STOP "Fractional spin targets were rounded"
     101              :       END IF
     102            2 :    END SUBROUTINE test_fixed_spin_populations
     103              : 
     104              : ! **************************************************************************************************
     105              : !> \brief Check the fixed-N occupation response for every supported K-point smearing method.
     106              : ! **************************************************************************************************
     107            2 :    SUBROUTINE test_fixed_n_smearing_response()
     108              :       INTEGER, PARAMETER                                 :: nkpoint = 2, nstate = 5
     109              :       REAL(KIND=dp), PARAMETER                           :: fd_step = 1.0E-6_dp, maxocc = 2.0_dp, &
     110              :                                                             sigma = 0.15_dp, TARGET = 4.3_dp
     111              : 
     112              :       INTEGER                                            :: ikpoint, imethod
     113              :       INTEGER, DIMENSION(4)                              :: methods
     114              :       REAL(KIND=dp)                                      :: error, kTS, mu, response_sum
     115              :       REAL(KIND=dp), DIMENSION(nkpoint)                  :: kpoint_weight
     116              :       REAL(KIND=dp), DIMENSION(nstate*nkpoint)           :: fd_response, flat_direction, &
     117              :                                                             flat_response, predicted_response
     118              :       REAL(KIND=dp), DIMENSION(nstate, nkpoint) :: direction, eigenvalue, eigenvalue_minus, &
     119              :          eigenvalue_plus, occupation, occupation_minus, occupation_plus, response
     120              : 
     121            2 :       methods(:) = [smear_fermi_dirac, smear_gaussian, smear_mp, smear_mv]
     122            2 :       kpoint_weight(:) = [0.4_dp, 0.6_dp]
     123           12 :       eigenvalue(:, 1) = [-0.38_dp, -0.14_dp, 0.02_dp, 0.23_dp, 0.52_dp]
     124           12 :       eigenvalue(:, 2) = [-0.31_dp, -0.08_dp, 0.07_dp, 0.28_dp, 0.61_dp]
     125           12 :       direction(:, 1) = [0.13_dp, -0.07_dp, 0.11_dp, -0.04_dp, 0.09_dp]
     126           12 :       direction(:, 2) = [-0.08_dp, 0.05_dp, -0.12_dp, 0.06_dp, -0.03_dp]
     127            2 :       flat_direction(:) = RESHAPE(direction, [nstate*nkpoint])
     128              : 
     129           10 :       DO imethod = 1, SIZE(methods)
     130              :          CALL Smearkp(occupation, mu, kTS, eigenvalue, TARGET, kpoint_weight, sigma, maxocc, &
     131            8 :                       methods(imethod))
     132           24 :          DO ikpoint = 1, nkpoint
     133              :             CALL smearing_response_weight( &
     134              :                response(:, ikpoint), occupation(:, ikpoint), eigenvalue(:, ikpoint), mu, sigma, &
     135           16 :                maxocc, nstate, methods(imethod))
     136          104 :             response(:, ikpoint) = kpoint_weight(ikpoint)*response(:, ikpoint)
     137              :          END DO
     138          104 :          response_sum = SUM(response)
     139            8 :          IF (ABS(response_sum) <= EPSILON(response_sum)) THEN
     140            0 :             ERROR STOP "Singular fixed-N smearing response in unit test"
     141              :          END IF
     142           56 :          IF ((methods(imethod) == smear_mp .OR. methods(imethod) == smear_mv) .AND. &
     143              :              .NOT. ANY(response < 0.0_dp)) THEN
     144            0 :             ERROR STOP "Signed smearing-response branch was not exercised"
     145              :          END IF
     146            8 :          flat_response = RESHAPE(response, [nstate*nkpoint])
     147              : 
     148          104 :          eigenvalue_plus(:, :) = eigenvalue + fd_step*direction
     149          104 :          eigenvalue_minus(:, :) = eigenvalue - fd_step*direction
     150              :          CALL Smearkp(occupation_plus, mu, kTS, eigenvalue_plus, TARGET, kpoint_weight, sigma, &
     151            8 :                       maxocc, methods(imethod))
     152              :          CALL Smearkp(occupation_minus, mu, kTS, eigenvalue_minus, TARGET, kpoint_weight, sigma, &
     153            8 :                       maxocc, methods(imethod))
     154           24 :          DO ikpoint = 1, nkpoint
     155              :             fd_response((ikpoint - 1)*nstate + 1:ikpoint*nstate) = &
     156              :                kpoint_weight(ikpoint)*(occupation_plus(:, ikpoint) - &
     157          104 :                                        occupation_minus(:, ikpoint))/(2.0_dp*fd_step)
     158              :          END DO
     159              :          predicted_response = -flat_response* &
     160          168 :                               (flat_direction - DOT_PRODUCT(flat_response, flat_direction)/response_sum)
     161           88 :          error = MAXVAL(ABS(fd_response - predicted_response))
     162           88 :          error = MAX(error, ABS(SUM(fd_response)))
     163              :          ! A common energy shift leaves fixed-N occupations unchanged.
     164              :          CALL Smearkp(occupation_plus, mu, kTS, eigenvalue + 0.37_dp, TARGET, kpoint_weight, &
     165          104 :                       sigma, maxocc, methods(imethod))
     166          112 :          error = MAX(error, MAXVAL(ABS(occupation_plus - occupation)))
     167           10 :          IF (error > 2.0E-6_dp) ERROR STOP "Fixed-N smearing response finite difference failed"
     168              :       END DO
     169              : 
     170            2 :    END SUBROUTINE test_fixed_n_smearing_response
     171              : 
     172              : END PROGRAM kpoint_smearing_unittest
        

Generated by: LCOV version 2.0-1