LCOV - code coverage report
Current view: top level - src - qs_mo_occupation.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:2fce0f8) Lines: 243 301 80.7 %
Date: 2024-12-21 06:28:57 Functions: 3 3 100.0 %

          Line data    Source code
       1             : !--------------------------------------------------------------------------------------------------!
       2             : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3             : !   Copyright 2000-2024 CP2K developers group <https://cp2k.org>                                   !
       4             : !                                                                                                  !
       5             : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6             : !--------------------------------------------------------------------------------------------------!
       7             : 
       8             : ! **************************************************************************************************
       9             : !> \brief Set occupation of molecular orbitals
      10             : !> \par History
      11             : !>      - set_mo_occupation subroutines moved from qs_mo_types (11.12.2014 MI)
      12             : !> \author  MI
      13             : ! **************************************************************************************************
      14             : 
      15             : MODULE qs_mo_occupation
      16             : 
      17             :    USE cp_log_handling,                 ONLY: cp_to_string
      18             :    USE fermi_utils,                     ONLY: FermiFixed,&
      19             :                                               FermiFixedDeriv
      20             :    USE input_constants,                 ONLY: smear_energy_window,&
      21             :                                               smear_fermi_dirac,&
      22             :                                               smear_list
      23             :    USE kahan_sum,                       ONLY: accurate_sum
      24             :    USE kinds,                           ONLY: dp
      25             :    USE qs_mo_types,                     ONLY: get_mo_set,&
      26             :                                               has_uniform_occupation,&
      27             :                                               mo_set_type,&
      28             :                                               set_mo_set
      29             :    USE scf_control_types,               ONLY: smear_type
      30             :    USE util,                            ONLY: sort
      31             :    USE xas_env_types,                   ONLY: get_xas_env,&
      32             :                                               xas_environment_type
      33             : #include "./base/base_uses.f90"
      34             : 
      35             :    IMPLICIT NONE
      36             : 
      37             :    PRIVATE
      38             : 
      39             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_mo_occupation'
      40             : 
      41             :    PUBLIC :: set_mo_occupation
      42             : 
      43             :    INTERFACE set_mo_occupation
      44             :       MODULE PROCEDURE set_mo_occupation_1, set_mo_occupation_2
      45             :    END INTERFACE
      46             : 
      47             : CONTAINS
      48             : 
      49             : ! **************************************************************************************************
      50             : !> \brief  Occupation for smeared spin polarized electronic structures
      51             : !>         with relaxed multiplicity
      52             : !>
      53             : !> \param mo_array ...
      54             : !> \param smear ...
      55             : !> \date    10.03.2011 (MI)
      56             : !> \author  MI
      57             : !> \version 1.0
      58             : ! **************************************************************************************************
      59          46 :    SUBROUTINE set_mo_occupation_3(mo_array, smear)
      60             : 
      61             :       TYPE(mo_set_type), DIMENSION(2), INTENT(INOUT)     :: mo_array
      62             :       TYPE(smear_type), POINTER                          :: smear
      63             : 
      64             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'set_mo_occupation_3'
      65             : 
      66             :       INTEGER                                            :: all_nmo, handle, homo_a, homo_b, i, &
      67             :                                                             lfomo_a, lfomo_b, nmo_a, nmo_b, &
      68             :                                                             xas_estate
      69          46 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: all_index
      70             :       LOGICAL                                            :: is_large
      71             :       REAL(KIND=dp)                                      :: all_nelec, kTS, mu, nelec_a, nelec_b, &
      72             :                                                             occ_estate
      73             :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: all_eigval, all_occ
      74          46 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: eigval_a, eigval_b, occ_a, occ_b
      75             : 
      76          46 :       CALL timeset(routineN, handle)
      77             : 
      78          46 :       NULLIFY (eigval_a, eigval_b, occ_a, occ_b)
      79             :       CALL get_mo_set(mo_set=mo_array(1), nmo=nmo_a, eigenvalues=eigval_a, &
      80          46 :                       occupation_numbers=occ_a)
      81             :       CALL get_mo_set(mo_set=mo_array(2), nmo=nmo_b, eigenvalues=eigval_b, &
      82          46 :                       occupation_numbers=occ_b)
      83          46 :       all_nmo = nmo_a + nmo_b
      84         138 :       ALLOCATE (all_eigval(all_nmo))
      85          92 :       ALLOCATE (all_occ(all_nmo))
      86         138 :       ALLOCATE (all_index(all_nmo))
      87             : 
      88        1264 :       all_eigval(1:nmo_a) = eigval_a(1:nmo_a)
      89        1264 :       all_eigval(nmo_a + 1:all_nmo) = eigval_b(1:nmo_b)
      90             : 
      91          46 :       CALL sort(all_eigval, all_nmo, all_index)
      92             : 
      93          46 :       xas_estate = -1
      94          46 :       occ_estate = 0.0_dp
      95             : 
      96             :       nelec_a = 0.0_dp
      97             :       nelec_b = 0.0_dp
      98             :       all_nelec = 0.0_dp
      99          46 :       nelec_a = accurate_sum(occ_a(:))
     100          46 :       nelec_b = accurate_sum(occ_b(:))
     101          46 :       all_nelec = nelec_a + nelec_b
     102             : 
     103        2482 :       DO i = 1, all_nmo
     104        2482 :          IF (all_index(i) <= nmo_a) THEN
     105        1218 :             all_occ(i) = occ_a(all_index(i))
     106             :          ELSE
     107        1218 :             all_occ(i) = occ_b(all_index(i) - nmo_a)
     108             :          END IF
     109             :       END DO
     110             : 
     111             :       CALL FermiFixed(all_occ, mu, kTS, all_eigval, all_nelec, &
     112          46 :                       smear%electronic_temperature, 1._dp, xas_estate, occ_estate)
     113             : 
     114        2528 :       is_large = ABS(MAXVAL(all_occ) - 1.0_dp) > smear%eps_fermi_dirac
     115             :       ! this is not a real problem, but the temperature might be a bit large
     116          46 :       CPWARN_IF(is_large, "Fermi-Dirac smearing includes the first MO")
     117             : 
     118        2528 :       is_large = ABS(MINVAL(all_occ)) > smear%eps_fermi_dirac
     119          46 :       IF (is_large) &
     120             :          CALL cp_warn(__LOCATION__, &
     121             :                       "Fermi-Dirac smearing includes the last MO => "// &
     122           0 :                       "Add more MOs for proper smearing.")
     123             : 
     124             :       ! check that the total electron count is accurate
     125          46 :       is_large = (ABS(all_nelec - accurate_sum(all_occ(:))) > smear%eps_fermi_dirac*all_nelec)
     126          46 :       CPWARN_IF(is_large, "Total number of electrons is not accurate")
     127             : 
     128        2482 :       DO i = 1, all_nmo
     129        2482 :          IF (all_index(i) <= nmo_a) THEN
     130        1218 :             occ_a(all_index(i)) = all_occ(i)
     131        1218 :             eigval_a(all_index(i)) = all_eigval(i)
     132             :          ELSE
     133        1218 :             occ_b(all_index(i) - nmo_a) = all_occ(i)
     134        1218 :             eigval_b(all_index(i) - nmo_a) = all_eigval(i)
     135             :          END IF
     136             :       END DO
     137             : 
     138          46 :       nelec_a = accurate_sum(occ_a(:))
     139          46 :       nelec_b = accurate_sum(occ_b(:))
     140             : 
     141         530 :       DO i = 1, nmo_a
     142         530 :          IF (occ_a(i) < 1.0_dp) THEN
     143          46 :             lfomo_a = i
     144          46 :             EXIT
     145             :          END IF
     146             :       END DO
     147         528 :       DO i = 1, nmo_b
     148         528 :          IF (occ_b(i) < 1.0_dp) THEN
     149          46 :             lfomo_b = i
     150          46 :             EXIT
     151             :          END IF
     152             :       END DO
     153          46 :       homo_a = lfomo_a - 1
     154         488 :       DO i = nmo_a, lfomo_a, -1
     155         488 :          IF (occ_a(i) > smear%eps_fermi_dirac) THEN
     156          46 :             homo_a = i
     157          46 :             EXIT
     158             :          END IF
     159             :       END DO
     160          46 :       homo_b = lfomo_b - 1
     161         488 :       DO i = nmo_b, lfomo_b, -1
     162         488 :          IF (occ_b(i) > smear%eps_fermi_dirac) THEN
     163          46 :             homo_b = i
     164          46 :             EXIT
     165             :          END IF
     166             :       END DO
     167             : 
     168             :       CALL set_mo_set(mo_set=mo_array(1), kTS=kTS/2.0_dp, mu=mu, n_el_f=nelec_a, &
     169          46 :                       lfomo=lfomo_a, homo=homo_a, uniform_occupation=.FALSE.)
     170             :       CALL set_mo_set(mo_set=mo_array(2), kTS=kTS/2.0_dp, mu=mu, n_el_f=nelec_b, &
     171          46 :                       lfomo=lfomo_b, homo=homo_b, uniform_occupation=.FALSE.)
     172             : 
     173          46 :       CALL timestop(handle)
     174             : 
     175          92 :    END SUBROUTINE set_mo_occupation_3
     176             : 
     177             : ! **************************************************************************************************
     178             : !> \brief   Prepare an occupation of alpha and beta MOs following an Aufbau
     179             : !>          principle, i.e. allowing a change in multiplicity.
     180             : !> \param mo_array ...
     181             : !> \param smear ...
     182             : !> \param eval_deriv ...
     183             : !> \param tot_zeff_corr ...
     184             : !> \date    25.01.2010 (MK)
     185             : !> \par   History
     186             : !>        10.2019 Added functionality to adjust mo occupation if the core
     187             : !>                charges are changed via CORE_CORRECTION during surface dipole
     188             : !>                calculation. Total number of electrons matches the total core
     189             : !>                charges if tot_zeff_corr is non-zero. Not yet implemented for
     190             : !>                OT type method. [Soumya Ghosh]
     191             : !> \author  Matthias Krack (MK)
     192             : !> \version 1.0
     193             : ! **************************************************************************************************
     194       81577 :    SUBROUTINE set_mo_occupation_2(mo_array, smear, eval_deriv, tot_zeff_corr)
     195             : 
     196             :       TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT)     :: mo_array
     197             :       TYPE(smear_type), POINTER                          :: smear
     198             :       REAL(KIND=dp), DIMENSION(:), OPTIONAL, POINTER     :: eval_deriv
     199             :       REAL(KIND=dp), OPTIONAL                            :: tot_zeff_corr
     200             : 
     201             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'set_mo_occupation_2'
     202             : 
     203             :       INTEGER                                            :: handle, i, lumo_a, lumo_b, &
     204             :                                                             multiplicity_new, multiplicity_old, &
     205             :                                                             nelec
     206             :       REAL(KIND=dp)                                      :: nelec_f, threshold
     207       81577 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: eigval_a, eigval_b
     208             : 
     209       81577 :       CALL timeset(routineN, handle)
     210             : 
     211             :       ! Fall back for the case that we have only one MO set
     212       81577 :       IF (SIZE(mo_array) == 1) THEN
     213       71587 :          IF (PRESENT(eval_deriv)) THEN
     214             : ! Change of MO occupancy to account for CORE_CORRECTION is not yet implemented
     215           0 :             CALL set_mo_occupation_1(mo_array(1), smear=smear, eval_deriv=eval_deriv)
     216             :          ELSE
     217       71587 :             IF (PRESENT(tot_zeff_corr)) THEN
     218          20 :                CALL set_mo_occupation_1(mo_array(1), smear=smear, tot_zeff_corr=tot_zeff_corr)
     219             :             ELSE
     220       71567 :                CALL set_mo_occupation_1(mo_array(1), smear=smear)
     221             :             END IF
     222             :          END IF
     223       71587 :          CALL timestop(handle)
     224             :          RETURN
     225             :       END IF
     226             : 
     227        9990 :       IF (smear%do_smear) THEN
     228        1416 :          IF (smear%fixed_mag_mom < 0.0_dp) THEN
     229          46 :             IF (PRESENT(tot_zeff_corr)) THEN
     230             :                CALL cp_warn(__LOCATION__, &
     231             :                             "CORE_CORRECTION /= 0.0 might cause the cell to charge up "// &
     232             :                             "that will lead to application of different background "// &
     233             :                             "correction compared to the reference system. "// &
     234             :                             "Use FIXED_MAGNETIC_MOMENT >= 0.0 if using SMEAR keyword "// &
     235           0 :                             "to correct the electron density")
     236             :             END IF
     237          46 :             IF (smear%fixed_mag_mom /= -1.0_dp) THEN
     238          46 :                CPASSERT(.NOT. (PRESENT(eval_deriv)))
     239          46 :                CALL set_mo_occupation_3(mo_array, smear=smear)
     240          46 :                CALL timestop(handle)
     241          46 :                RETURN
     242             :             END IF
     243             :          ELSE
     244        1370 :             nelec_f = mo_array(1)%n_el_f + mo_array(2)%n_el_f
     245        1370 :             IF (ABS((mo_array(1)%n_el_f - mo_array(2)%n_el_f) - smear%fixed_mag_mom) > smear%eps_fermi_dirac*nelec_f) THEN
     246           2 :                mo_array(1)%n_el_f = nelec_f/2.0_dp + smear%fixed_mag_mom/2.0_dp
     247           2 :                mo_array(2)%n_el_f = nelec_f/2.0_dp - smear%fixed_mag_mom/2.0_dp
     248             :             END IF
     249        1370 :             CPASSERT(.NOT. (PRESENT(eval_deriv)))
     250        1370 :             IF (PRESENT(tot_zeff_corr)) THEN
     251          20 :                CALL set_mo_occupation_1(mo_array(1), smear=smear, tot_zeff_corr=tot_zeff_corr)
     252          20 :                CALL set_mo_occupation_1(mo_array(2), smear=smear, tot_zeff_corr=tot_zeff_corr)
     253             :             ELSE
     254        1350 :                CALL set_mo_occupation_1(mo_array(1), smear=smear)
     255        1350 :                CALL set_mo_occupation_1(mo_array(2), smear=smear)
     256             :             END IF
     257             :          END IF
     258             :       END IF
     259             : 
     260        9944 :       IF (.NOT. ((mo_array(1)%flexible_electron_count > 0.0_dp) .AND. &
     261             :                  (mo_array(2)%flexible_electron_count > 0.0_dp))) THEN
     262        9778 :          IF (PRESENT(eval_deriv)) THEN
     263           0 :             CALL set_mo_occupation_1(mo_array(1), smear=smear, eval_deriv=eval_deriv)
     264           0 :             CALL set_mo_occupation_1(mo_array(2), smear=smear, eval_deriv=eval_deriv)
     265             :          ELSE
     266        9778 :             IF (PRESENT(tot_zeff_corr)) THEN
     267          20 :                CALL set_mo_occupation_1(mo_array(1), smear=smear, tot_zeff_corr=tot_zeff_corr)
     268          20 :                CALL set_mo_occupation_1(mo_array(2), smear=smear, tot_zeff_corr=tot_zeff_corr)
     269             :             ELSE
     270        9758 :                CALL set_mo_occupation_1(mo_array(1), smear=smear)
     271        9758 :                CALL set_mo_occupation_1(mo_array(2), smear=smear)
     272             :             END IF
     273             :          END IF
     274        9778 :          CALL timestop(handle)
     275        9778 :          RETURN
     276             :       END IF
     277             : 
     278         166 :       nelec = mo_array(1)%nelectron + mo_array(2)%nelectron
     279             : 
     280         166 :       multiplicity_old = mo_array(1)%nelectron - mo_array(2)%nelectron + 1
     281             : 
     282         166 :       IF (mo_array(1)%nelectron >= mo_array(1)%nmo) &
     283             :          CALL cp_warn(__LOCATION__, &
     284             :                       "All alpha MOs are occupied. Add more alpha MOs to "// &
     285           0 :                       "allow for a higher multiplicity")
     286         166 :       IF ((mo_array(2)%nelectron >= mo_array(2)%nmo) .AND. (mo_array(2)%nelectron /= mo_array(1)%nelectron)) &
     287             :          CALL cp_warn(__LOCATION__, "All beta MOs are occupied. Add more beta MOs to "// &
     288           0 :                       "allow for a lower multiplicity")
     289             : 
     290         166 :       eigval_a => mo_array(1)%eigenvalues
     291         166 :       eigval_b => mo_array(2)%eigenvalues
     292             : 
     293         166 :       lumo_a = 1
     294         166 :       lumo_b = 1
     295             : 
     296             :       ! Apply Aufbau principle
     297        2046 :       DO i = 1, nelec
     298             :          ! Threshold is needed to ensure a preference for alpha occupation in the case
     299             :          ! of degeneracy
     300        1880 :          threshold = MAX(mo_array(1)%flexible_electron_count, mo_array(2)%flexible_electron_count)
     301        1880 :          IF ((eigval_a(lumo_a) - threshold) < eigval_b(lumo_b)) THEN
     302        1072 :             lumo_a = lumo_a + 1
     303             :          ELSE
     304         808 :             lumo_b = lumo_b + 1
     305             :          END IF
     306        1880 :          IF (lumo_a > mo_array(1)%nmo) THEN
     307           0 :             IF (i /= nelec) &
     308             :                CALL cp_warn(__LOCATION__, &
     309             :                             "All alpha MOs are occupied. Add more alpha MOs to "// &
     310           0 :                             "allow for a higher multiplicity")
     311           0 :             IF (i < nelec) THEN
     312           0 :                lumo_a = lumo_a - 1
     313           0 :                lumo_b = lumo_b + 1
     314             :             END IF
     315             :          END IF
     316        2046 :          IF (lumo_b > mo_array(2)%nmo) THEN
     317          34 :             IF (lumo_b < lumo_a) &
     318             :                CALL cp_warn(__LOCATION__, &
     319             :                             "All beta MOs are occupied. Add more beta MOs to "// &
     320           0 :                             "allow for a lower multiplicity")
     321          34 :             IF (i < nelec) THEN
     322           6 :                lumo_a = lumo_a + 1
     323           6 :                lumo_b = lumo_b - 1
     324             :             END IF
     325             :          END IF
     326             :       END DO
     327             : 
     328         166 :       mo_array(1)%homo = lumo_a - 1
     329         166 :       mo_array(2)%homo = lumo_b - 1
     330             : 
     331         166 :       IF (mo_array(2)%homo > mo_array(1)%homo) THEN
     332             :          CALL cp_warn(__LOCATION__, &
     333             :                       "More beta ("// &
     334             :                       TRIM(ADJUSTL(cp_to_string(mo_array(2)%homo)))// &
     335             :                       ") than alpha ("// &
     336             :                       TRIM(ADJUSTL(cp_to_string(mo_array(1)%homo)))// &
     337           0 :                       ") MOs are occupied. Resorting to low spin state")
     338           0 :          mo_array(1)%homo = nelec/2 + MODULO(nelec, 2)
     339           0 :          mo_array(2)%homo = nelec/2
     340             :       END IF
     341             : 
     342         166 :       mo_array(1)%nelectron = mo_array(1)%homo
     343         166 :       mo_array(2)%nelectron = mo_array(2)%homo
     344         166 :       multiplicity_new = mo_array(1)%nelectron - mo_array(2)%nelectron + 1
     345             : 
     346         166 :       IF (multiplicity_new /= multiplicity_old) &
     347             :          CALL cp_warn(__LOCATION__, &
     348             :                       "Multiplicity changed from "// &
     349             :                       TRIM(ADJUSTL(cp_to_string(multiplicity_old)))//" to "// &
     350           8 :                       TRIM(ADJUSTL(cp_to_string(multiplicity_new))))
     351             : 
     352         166 :       IF (PRESENT(eval_deriv)) THEN
     353           0 :          CALL set_mo_occupation_1(mo_array(1), smear=smear, eval_deriv=eval_deriv)
     354           0 :          CALL set_mo_occupation_1(mo_array(2), smear=smear, eval_deriv=eval_deriv)
     355             :       ELSE
     356         166 :          IF (PRESENT(tot_zeff_corr)) THEN
     357           0 :             CALL set_mo_occupation_1(mo_array(1), smear=smear, tot_zeff_corr=tot_zeff_corr)
     358           0 :             CALL set_mo_occupation_1(mo_array(2), smear=smear, tot_zeff_corr=tot_zeff_corr)
     359             :          ELSE
     360         166 :             CALL set_mo_occupation_1(mo_array(1), smear=smear)
     361         166 :             CALL set_mo_occupation_1(mo_array(2), smear=smear)
     362             :          END IF
     363             :       END IF
     364             : 
     365         166 :       CALL timestop(handle)
     366             : 
     367       81577 :    END SUBROUTINE set_mo_occupation_2
     368             : 
     369             : ! **************************************************************************************************
     370             : !> \brief   Smearing of the MO occupation with all kind of occupation numbers
     371             : !> \param   mo_set MO dataset structure
     372             : !> \param   smear optional smearing information
     373             : !> \param   eval_deriv on entry the derivative of the KS energy wrt to the occupation number
     374             : !>                     on exit  the derivative of the full free energy (i.e. KS and entropy) wrt to the eigenvalue
     375             : !> \param xas_env ...
     376             : !> \param tot_zeff_corr ...
     377             : !> \date    17.04.2002 (v1.0), 26.08.2008 (v1.1)
     378             : !> \par   History
     379             : !>        10.2019 Added functionality to adjust mo occupation if the core
     380             : !>                charges are changed via CORE_CORRECTION during surface dipole
     381             : !>                calculation. Total number of electrons matches the total core
     382             : !>                charges if tot_zeff_corr is non-zero. Not yet implemented for
     383             : !>                OT type method. [Soumya Ghosh]
     384             : !> \author  Matthias Krack
     385             : !> \version 1.1
     386             : ! **************************************************************************************************
     387      194384 :    SUBROUTINE set_mo_occupation_1(mo_set, smear, eval_deriv, xas_env, tot_zeff_corr)
     388             : 
     389             :       TYPE(mo_set_type), INTENT(INOUT)                   :: mo_set
     390             :       TYPE(smear_type), OPTIONAL, POINTER                :: smear
     391             :       REAL(KIND=dp), DIMENSION(:), OPTIONAL, POINTER     :: eval_deriv
     392             :       TYPE(xas_environment_type), OPTIONAL, POINTER      :: xas_env
     393             :       REAL(KIND=dp), OPTIONAL                            :: tot_zeff_corr
     394             : 
     395             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'set_mo_occupation_1'
     396             : 
     397             :       INTEGER                                            :: handle, i_first, imo, ir, irmo, nmo, &
     398             :                                                             nomo, xas_estate
     399             :       LOGICAL                                            :: equal_size, is_large
     400             :       REAL(KIND=dp)                                      :: delectron, e1, e2, edelta, edist, &
     401             :                                                             el_count, lengthscale, nelec, &
     402             :                                                             occ_estate, total_zeff_corr, &
     403             :                                                             xas_nelectron
     404      194384 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: dfde
     405             : 
     406      194384 :       CALL timeset(routineN, handle)
     407             : 
     408      194384 :       CPASSERT(ASSOCIATED(mo_set%eigenvalues))
     409      194384 :       CPASSERT(ASSOCIATED(mo_set%occupation_numbers))
     410     2005084 :       mo_set%occupation_numbers(:) = 0.0_dp
     411             : 
     412             :       ! Quick return, if no electrons are available
     413      194384 :       IF (mo_set%nelectron == 0) THEN
     414        1580 :          CALL timestop(handle)
     415        1580 :          RETURN
     416             :       END IF
     417             : 
     418      192804 :       xas_estate = -1
     419      192804 :       occ_estate = 0.0_dp
     420      192804 :       IF (PRESENT(xas_env)) THEN
     421         786 :          CALL get_xas_env(xas_env=xas_env, xas_nelectron=xas_nelectron, occ_estate=occ_estate, xas_estate=xas_estate)
     422         786 :          nomo = CEILING(xas_nelectron + 1.0 - occ_estate - EPSILON(0.0_dp))
     423             : 
     424        7802 :          mo_set%occupation_numbers(1:nomo) = mo_set%maxocc
     425         786 :          IF (xas_estate > 0) mo_set%occupation_numbers(xas_estate) = occ_estate
     426        7802 :          el_count = SUM(mo_set%occupation_numbers(1:nomo))
     427         786 :          IF (el_count > xas_nelectron) &
     428          98 :             mo_set%occupation_numbers(nomo) = mo_set%occupation_numbers(nomo) - (el_count - xas_nelectron)
     429        7802 :          el_count = SUM(mo_set%occupation_numbers(1:nomo))
     430         786 :          is_large = ABS(el_count - xas_nelectron) > xas_nelectron*EPSILON(el_count)
     431         786 :          CPASSERT(.NOT. is_large)
     432             :       ELSE
     433      192018 :          IF (MODULO(mo_set%nelectron, INT(mo_set%maxocc)) == 0) THEN
     434      190976 :             nomo = NINT(mo_set%nelectron/mo_set%maxocc)
     435             :             ! Initialize MO occupations
     436     1840864 :             mo_set%occupation_numbers(1:nomo) = mo_set%maxocc
     437             :          ELSE
     438        1042 :             nomo = INT(mo_set%nelectron/mo_set%maxocc) + 1
     439             :             ! Initialize MO occupations
     440        6648 :             mo_set%occupation_numbers(1:nomo - 1) = mo_set%maxocc
     441        1042 :             mo_set%occupation_numbers(nomo) = mo_set%nelectron - (nomo - 1)*mo_set%maxocc
     442             :          END IF
     443             : ! introduce applied potential correction here
     444             : ! electron density is adjusted according to applied core correction
     445             : ! ref: SS, MT, MWF, JN PRL, 2018, 120, 246801
     446             : ! see whether both surface dipole correction and core correction is present in
     447             : ! the inputfile
     448      192018 :          IF (PRESENT(tot_zeff_corr)) THEN
     449             : ! find the additional core charges
     450         106 :             total_zeff_corr = tot_zeff_corr
     451         106 :             IF (INT(mo_set%maxocc) == 1) total_zeff_corr = total_zeff_corr/2.0_dp
     452         106 :             delectron = 0.0_dp
     453         106 :             IF (total_zeff_corr < 0.0_dp) THEN
     454             : ! remove electron density from the mos
     455         106 :                delectron = ABS(total_zeff_corr) - REAL(mo_set%maxocc, KIND=dp)
     456         106 :                IF (delectron > 0.0_dp) THEN
     457           0 :                   mo_set%occupation_numbers(nomo) = 0.0_dp
     458           0 :                   irmo = CEILING(delectron/REAL(mo_set%maxocc, KIND=dp))
     459           0 :                   DO ir = 1, irmo
     460           0 :                      delectron = delectron - REAL(mo_set%maxocc, KIND=dp)
     461           0 :                      IF (delectron < 0.0_dp) THEN
     462           0 :                         mo_set%occupation_numbers(nomo - ir) = -delectron
     463             :                      ELSE
     464           0 :                         mo_set%occupation_numbers(nomo - ir) = 0.0_dp
     465             :                      END IF
     466             :                   END DO
     467           0 :                   nomo = nomo - irmo
     468           0 :                   IF (mo_set%occupation_numbers(nomo) == 0.0_dp) nomo = nomo - 1
     469         106 :                ELSEIF (delectron < 0.0_dp) THEN
     470         106 :                   mo_set%occupation_numbers(nomo) = -delectron
     471             :                ELSE
     472           0 :                   mo_set%occupation_numbers(nomo) = 0.0_dp
     473           0 :                   nomo = nomo - 1
     474             :                END IF
     475           0 :             ELSEIF (total_zeff_corr > 0.0_dp) THEN
     476             : ! add electron density to the mos
     477           0 :                delectron = total_zeff_corr - REAL(mo_set%maxocc, KIND=dp)
     478           0 :                IF (delectron > 0.0_dp) THEN
     479           0 :                   mo_set%occupation_numbers(nomo + 1) = REAL(mo_set%maxocc, KIND=dp)
     480           0 :                   nomo = nomo + 1
     481           0 :                   irmo = CEILING(delectron/REAL(mo_set%maxocc, KIND=dp))
     482           0 :                   DO ir = 1, irmo
     483           0 :                      delectron = delectron - REAL(mo_set%maxocc, KIND=dp)
     484           0 :                      IF (delectron < 0.0_dp) THEN
     485           0 :                         mo_set%occupation_numbers(nomo + ir) = delectron + REAL(mo_set%maxocc, KIND=dp)
     486             :                      ELSE
     487           0 :                         mo_set%occupation_numbers(nomo + ir) = REAL(mo_set%maxocc, KIND=dp)
     488             :                      END IF
     489             :                   END DO
     490           0 :                   nomo = nomo + irmo
     491             :                ELSE
     492           0 :                   mo_set%occupation_numbers(nomo + 1) = total_zeff_corr
     493           0 :                   nomo = nomo + 1
     494             :                END IF
     495             :             END IF
     496             :          END IF
     497             :       END IF
     498      192804 :       nmo = SIZE(mo_set%eigenvalues)
     499             : 
     500      192804 :       CPASSERT(nmo >= nomo)
     501      192804 :       CPASSERT((SIZE(mo_set%occupation_numbers) == nmo))
     502             : 
     503      192804 :       mo_set%homo = nomo
     504      192804 :       mo_set%lfomo = nomo + 1
     505      192804 :       mo_set%mu = mo_set%eigenvalues(nomo)
     506             : 
     507             :       ! Check consistency of the array lengths
     508      192804 :       IF (PRESENT(eval_deriv)) THEN
     509           0 :          equal_size = (SIZE(mo_set%occupation_numbers, 1) == SIZE(eval_deriv, 1))
     510           0 :          CPASSERT(equal_size)
     511             :       END IF
     512             : 
     513             :       ! Quick return, if no smearing information is supplied (TO BE FIXED, smear should become non-optional...)
     514      192804 :       IF (.NOT. PRESENT(smear)) THEN
     515             :          ! there is no dependence of the energy on the eigenvalues
     516           8 :          mo_set%uniform_occupation = .TRUE.
     517           8 :          IF (PRESENT(eval_deriv)) THEN
     518           0 :             eval_deriv = 0.0_dp
     519             :          END IF
     520           8 :          CALL timestop(handle)
     521           8 :          RETURN
     522             :       END IF
     523             : 
     524             :       ! Check if proper eigenvalues are already available
     525      192796 :       IF (smear%method /= smear_list) THEN
     526      192772 :          IF ((ABS(mo_set%eigenvalues(1)) < 1.0E-12_dp) .AND. &
     527             :              (ABS(mo_set%eigenvalues(nmo)) < 1.0E-12_dp)) THEN
     528       83343 :             CALL timestop(handle)
     529       83343 :             RETURN
     530             :          END IF
     531             :       END IF
     532             : 
     533             :       ! Perform smearing
     534      109453 :       IF (smear%do_smear) THEN
     535        8700 :          IF (PRESENT(xas_env)) THEN
     536          18 :             i_first = xas_estate + 1
     537          18 :             nelec = xas_nelectron
     538             :          ELSE
     539        8682 :             i_first = 1
     540        8682 :             IF (smear%fixed_mag_mom == -1.0_dp) THEN
     541           0 :                nelec = REAL(mo_set%nelectron, dp)
     542             :             ELSE
     543        8682 :                nelec = mo_set%n_el_f
     544             :             END IF
     545             :          END IF
     546        7714 :          SELECT CASE (smear%method)
     547             :          CASE (smear_fermi_dirac)
     548        7714 :             IF (.NOT. PRESENT(eval_deriv)) THEN
     549             :                CALL FermiFixed(mo_set%occupation_numbers, mo_set%mu, mo_set%kTS, mo_set%eigenvalues, Nelec, &
     550        7714 :                                smear%electronic_temperature, mo_set%maxocc, xas_estate, occ_estate)
     551             :             ELSE
     552             :                ! could be a relatively large matrix, but one could get rid of it by never storing it
     553             :                ! we only need dE/df * df/de, one could equally parallelize over entries, this could become expensive
     554           0 :                ALLOCATE (dfde(nmo, nmo))
     555             :                ! lengthscale could become a parameter, but this is pretty good
     556           0 :                lengthscale = 10*smear%electronic_temperature
     557             : 
     558             :                CALL FermiFixedDeriv(dfde, mo_set%occupation_numbers, mo_set%mu, mo_set%kTS, mo_set%eigenvalues, Nelec, &
     559           0 :                                     smear%electronic_temperature, mo_set%maxocc, lengthscale, xas_estate, occ_estate)
     560             : 
     561             :                ! deriv of E_{KS}-kT*S wrt to f_i
     562           0 :                eval_deriv = eval_deriv - mo_set%eigenvalues + mo_set%mu
     563             :                ! correspondingly the deriv of  E_{KS}-kT*S wrt to e_i
     564           0 :                eval_deriv = MATMUL(TRANSPOSE(dfde), eval_deriv)
     565             : 
     566           0 :                DEALLOCATE (dfde)
     567             :             END IF
     568             : 
     569             :             ! Find the lowest fractional occupied MO (LFOMO)
     570       36252 :             DO imo = i_first, nmo
     571       36252 :                IF (mo_set%occupation_numbers(imo) < mo_set%maxocc) THEN
     572        7714 :                   mo_set%lfomo = imo
     573        7714 :                   EXIT
     574             :                END IF
     575             :             END DO
     576      122258 :             is_large = ABS(MAXVAL(mo_set%occupation_numbers) - mo_set%maxocc) > smear%eps_fermi_dirac
     577             :             ! this is not a real problem, but the temperature might be a bit large
     578        7714 :             CPWARN_IF(is_large, "Fermi-Dirac smearing includes the first MO")
     579             : 
     580             :             ! Find the highest (fractional) occupied MO which will be now the HOMO
     581       49266 :             DO imo = nmo, mo_set%lfomo, -1
     582       49266 :                IF (mo_set%occupation_numbers(imo) > smear%eps_fermi_dirac) THEN
     583        6874 :                   mo_set%homo = imo
     584        6874 :                   EXIT
     585             :                END IF
     586             :             END DO
     587      122258 :             is_large = ABS(MINVAL(mo_set%occupation_numbers)) > smear%eps_fermi_dirac
     588        7714 :             IF (is_large) &
     589             :                CALL cp_warn(__LOCATION__, &
     590             :                             "Fermi-Dirac smearing includes the last MO => "// &
     591         680 :                             "Add more MOs for proper smearing.")
     592             : 
     593             :             ! check that the total electron count is accurate
     594        7714 :             is_large = (ABS(nelec - accurate_sum(mo_set%occupation_numbers(:))) > smear%eps_fermi_dirac*nelec)
     595        7714 :             CPWARN_IF(is_large, "Total number of electrons is not accurate")
     596             : 
     597             :          CASE (smear_energy_window)
     598             :             ! not implemented
     599         962 :             CPASSERT(.NOT. PRESENT(eval_deriv))
     600             : 
     601             :             ! Define the energy window for the eigenvalues
     602         962 :             e1 = mo_set%eigenvalues(mo_set%homo) - 0.5_dp*smear%window_size
     603         962 :             IF (e1 <= mo_set%eigenvalues(1)) THEN
     604           0 :                CPWARN("Energy window for smearing includes the first MO")
     605             :             END IF
     606             : 
     607         962 :             e2 = mo_set%eigenvalues(mo_set%homo) + 0.5_dp*smear%window_size
     608         962 :             IF (e2 >= mo_set%eigenvalues(nmo)) &
     609             :                CALL cp_warn(__LOCATION__, &
     610             :                             "Energy window for smearing includes the last MO => "// &
     611           0 :                             "Add more MOs for proper smearing.")
     612             : 
     613             :             ! Find the lowest fractional occupied MO (LFOMO)
     614        8264 :             DO imo = i_first, nomo
     615        8264 :                IF (mo_set%eigenvalues(imo) > e1) THEN
     616         158 :                   mo_set%lfomo = imo
     617         158 :                   EXIT
     618             :                END IF
     619             :             END DO
     620             : 
     621             :             ! Find the highest fractional occupied (non-zero) MO which will be the HOMO
     622        7776 :             DO imo = nmo, nomo, -1
     623        7776 :                IF (mo_set%eigenvalues(imo) < e2) THEN
     624         158 :                   mo_set%homo = imo
     625         158 :                   EXIT
     626             :                END IF
     627             :             END DO
     628             : 
     629             :             ! Get the number of electrons to be smeared
     630         962 :             edist = 0.0_dp
     631         962 :             nelec = 0.0_dp
     632             : 
     633        1194 :             DO imo = mo_set%lfomo, mo_set%homo
     634         232 :                nelec = nelec + mo_set%occupation_numbers(imo)
     635        1194 :                edist = edist + ABS(e2 - mo_set%eigenvalues(imo))
     636             :             END DO
     637             : 
     638             :             ! Smear electrons inside the energy window
     639        1194 :             DO imo = mo_set%lfomo, mo_set%homo
     640         232 :                edelta = ABS(e2 - mo_set%eigenvalues(imo))
     641         232 :                mo_set%occupation_numbers(imo) = MIN(mo_set%maxocc, nelec*edelta/edist)
     642         232 :                nelec = nelec - mo_set%occupation_numbers(imo)
     643        1194 :                edist = edist - edelta
     644             :             END DO
     645             : 
     646             :          CASE (smear_list)
     647          24 :             equal_size = SIZE(mo_set%occupation_numbers, 1) == SIZE(smear%list, 1)
     648          24 :             CPASSERT(equal_size)
     649         168 :             mo_set%occupation_numbers = smear%list
     650             :             ! there is no dependence of the energy on the eigenvalues
     651          24 :             IF (PRESENT(eval_deriv)) THEN
     652           0 :                eval_deriv = 0.0_dp
     653             :             END IF
     654             :             ! most general case
     655          24 :             mo_set%lfomo = 1
     656        8724 :             mo_set%homo = nmo
     657             :          END SELECT
     658             : 
     659             :          ! Check, if the smearing involves more than one MO
     660        8700 :          IF (mo_set%lfomo == mo_set%homo) THEN
     661         584 :             mo_set%homo = nomo
     662         584 :             mo_set%lfomo = nomo + 1
     663             :          ELSE
     664        8116 :             mo_set%uniform_occupation = .FALSE.
     665             :          END IF
     666             : 
     667             :       END IF ! do smear
     668             : 
     669             :       ! zeros don't count as uniform
     670      109453 :       mo_set%uniform_occupation = has_uniform_occupation(mo_set=mo_set)
     671             : 
     672      109453 :       CALL timestop(handle)
     673             : 
     674      109453 :    END SUBROUTINE set_mo_occupation_1
     675             : 
     676             : END MODULE qs_mo_occupation

Generated by: LCOV version 1.15