LCOV - code coverage report
Current view: top level - src - qs_electrons_ne_electrode.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 0.0 % 99 0
Test Date: 2026-11-24 01:22:08 Functions: 0.0 % 1 0

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Ne-electrode core-charge correction for constant-potential MD: adjusts the Ne
      10              : !>        pseudopotential core charge to follow the fluctuating total electron number.
      11              : !> \author Nanchen DONGFANG
      12              : ! **************************************************************************************************
      13              : MODULE qs_electrons_ne_electrode
      14              :    USE IEEE_ARITHMETIC,                 ONLY: IEEE_IS_FINITE
      15              :    USE atomic_kind_types,               ONLY: atomic_kind_type
      16              :    USE cp_log_handling,                 ONLY: cp_logger_get_default_io_unit
      17              :    USE cp_subsys_types,                 ONLY: cp_subsys_type
      18              :    USE external_potential_types,        ONLY: all_potential_type,&
      19              :                                               gth_potential_type,&
      20              :                                               sgp_potential_type
      21              :    USE force_env_types,                 ONLY: force_env_get,&
      22              :                                               force_env_type
      23              :    USE kinds,                           ONLY: dp
      24              :    USE qs_environment_types,            ONLY: get_qs_env,&
      25              :                                               qs_environment_type
      26              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      27              :                                               get_qs_kind_set,&
      28              :                                               qs_kind_type
      29              :    USE qs_ks_types,                     ONLY: qs_ks_env_type,&
      30              :                                               set_ks_env
      31              : #include "./base/base_uses.f90"
      32              : 
      33              :    IMPLICIT NONE
      34              : 
      35              :    PRIVATE
      36              :    PUBLIC :: apply_ne_core_correction
      37              : 
      38              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_electrons_ne_electrode'
      39              : 
      40              : CONTAINS
      41              : 
      42              : ! **************************************************************************************************
      43              : !> \brief   Apply Ne core correction update from the current total electron number.
      44              : !>
      45              : !> It is called directly on force_env once per
      46              : !> MD step, right after qs_electrons_drift updates electrons%q, and before the SCF
      47              : !> call, since it changes the Ne pseudopotential and must be in effect for the SCF
      48              : !> that immediately follows.
      49              : !>
      50              : !> Receives the target total electron number and converts it to a per-Ne core
      51              : !> correction using core_corr_ne = (target_nelect - neutral_electrons_ref)/N_ne.
      52              : !> It applies the resulting value to all Ne atomic kinds in the QS subsystem by
      53              : !> updating the underlying all-electron or GTH/SGP potential zeff_correction
      54              : !> field. This effectively corresponds to adjusting the CORE_CORRECTION keyword
      55              : !> for Ne.
      56              : !>
      57              : !> The implementation traverses the QS environment, obtains the atomic kind
      58              : !> and QS kind arrays, and for each kind whose element symbol is 'Ne' it
      59              : !> updates zeff_correction := core_corr_ne and keeps the associated effective
      60              : !> charge parameters consistent with a statically parsed CORE_CORRECTION input.
      61              : !> The resulting change is reflected in the subsequent charge and electron
      62              : !> counts through the existing zeff_correction usage in QS routines.
      63              : !> \param force_env ...
      64              : !> \param target_nelect ...
      65              : !> \param neutral_electrons_ref ...
      66              : ! **************************************************************************************************
      67            0 :    SUBROUTINE apply_ne_core_correction(force_env, target_nelect, neutral_electrons_ref)
      68              : 
      69              :       TYPE(force_env_type), POINTER                      :: force_env
      70              :       REAL(KIND=dp), INTENT(IN)                          :: target_nelect, neutral_electrons_ref
      71              : 
      72              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'apply_ne_core_correction'
      73              :       REAL(KIND=dp), PARAMETER :: core_corr_refresh_tol = 1.0e-12_dp
      74              : 
      75              :       CHARACTER(LEN=2)                                   :: elem
      76              :       INTEGER                                            :: handle, ikind, iparticle, n_ne_atoms, &
      77              :                                                             nkind, output_unit
      78              :       LOGICAL                                            :: core_corr_changed, found_ne
      79              :       REAL(KIND=dp)                                      :: base_zeff, core_corr_ne, delta_q, &
      80              :                                                             new_zeff, old_zeff, zeff_scale
      81              :       TYPE(all_potential_type), POINTER                  :: all_potential
      82            0 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
      83              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
      84              :       TYPE(cp_subsys_type), POINTER                      :: cp_subsys
      85              :       TYPE(gth_potential_type), POINTER                  :: gth_potential
      86              :       TYPE(qs_environment_type), POINTER                 :: qs_env
      87            0 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
      88              :       TYPE(qs_kind_type), POINTER                        :: qs_kind
      89              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
      90              :       TYPE(sgp_potential_type), POINTER                  :: sgp_potential
      91              : 
      92            0 :       CALL timeset(routineN, handle)
      93              : 
      94            0 :       IF (.NOT. IEEE_IS_FINITE(target_nelect) .OR. target_nelect <= 0.0_dp) THEN
      95            0 :          CPABORT("target electron count must be positive.")
      96              :       END IF
      97            0 :       IF (.NOT. IEEE_IS_FINITE(neutral_electrons_ref) .OR. neutral_electrons_ref <= 0.0_dp) THEN
      98            0 :          CPABORT("neutral_electrons_ref must be positive.")
      99              :       END IF
     100              : 
     101              :       ! Obtain QS environment and atomic kind / kind sets. If this is not a
     102              :       ! QS calculation there is nothing to do.
     103            0 :       NULLIFY (qs_env)
     104            0 :       CALL force_env_get(force_env, qs_env=qs_env)
     105            0 :       IF (.NOT. ASSOCIATED(qs_env)) THEN
     106            0 :          CPABORT("Ne correction requires a QS force environment.")
     107              :       END IF
     108              : 
     109            0 :       NULLIFY (cp_subsys)
     110            0 :       CALL force_env_get(force_env, subsys=cp_subsys)
     111            0 :       IF (.NOT. ASSOCIATED(cp_subsys)) THEN
     112            0 :          CPABORT("Ne correction could not access CP2K subsys.")
     113              :       END IF
     114              : 
     115            0 :       NULLIFY (atomic_kind_set)
     116            0 :       NULLIFY (qs_kind_set)
     117            0 :       NULLIFY (ks_env)
     118              :       CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, &
     119            0 :                       qs_kind_set=qs_kind_set, ks_env=ks_env)
     120              : 
     121            0 :       IF (.NOT. (ASSOCIATED(atomic_kind_set) .AND. ASSOCIATED(qs_kind_set))) THEN
     122            0 :          CPABORT("Ne correction could not access kind sets.")
     123              :       END IF
     124              : 
     125            0 :       nkind = SIZE(atomic_kind_set, 1)
     126            0 :       found_ne = .FALSE.
     127            0 :       core_corr_changed = .FALSE.
     128            0 :       n_ne_atoms = 0
     129              : 
     130            0 :       DO iparticle = 1, cp_subsys%particles%n_els
     131            0 :          atomic_kind => cp_subsys%particles%els(iparticle)%atomic_kind
     132            0 :          IF (ASSOCIATED(atomic_kind)) THEN
     133            0 :             elem = atomic_kind%element_symbol
     134            0 :             elem = ADJUSTL(elem)
     135            0 :             IF (TRIM(elem) == 'Ne' .OR. TRIM(elem) == 'NE') THEN
     136            0 :                n_ne_atoms = n_ne_atoms + 1
     137              :             END IF
     138              :          END IF
     139              :       END DO
     140              : 
     141            0 :       IF (n_ne_atoms <= 0) THEN
     142            0 :          CPABORT("Ne mode requested but no Ne atoms found.")
     143              :       END IF
     144              : 
     145            0 :       delta_q = target_nelect - neutral_electrons_ref
     146            0 :       core_corr_ne = delta_q/REAL(n_ne_atoms, KIND=dp)
     147              : 
     148            0 :       DO ikind = 1, nkind
     149            0 :          atomic_kind => atomic_kind_set(ikind)
     150            0 :          qs_kind => qs_kind_set(ikind)
     151              : 
     152              :          ! Identify Ne atomic kinds by their element symbol. Compare against
     153              :          ! the canonical two-character symbol "Ne" (and its fully uppercase
     154              :          ! variant) after left adjustment.
     155            0 :          elem = atomic_kind%element_symbol
     156            0 :          elem = ADJUSTL(elem)
     157              : 
     158            0 :          IF (TRIM(elem) == 'Ne' .OR. TRIM(elem) == 'NE') THEN
     159            0 :             found_ne = .TRUE.
     160            0 :             NULLIFY (all_potential)
     161            0 :             NULLIFY (gth_potential)
     162            0 :             NULLIFY (sgp_potential)
     163              : 
     164              :             CALL get_qs_kind(qs_kind, all_potential=all_potential, gth_potential=gth_potential, &
     165            0 :                              sgp_potential=sgp_potential)
     166              : 
     167            0 :             IF (ASSOCIATED(all_potential)) THEN
     168            0 :                IF (ABS(all_potential%zeff_correction - core_corr_ne) > core_corr_refresh_tol) THEN
     169            0 :                   old_zeff = all_potential%zeff
     170            0 :                   base_zeff = old_zeff - all_potential%zeff_correction
     171            0 :                   new_zeff = base_zeff + core_corr_ne
     172            0 :                   IF (ABS(old_zeff) > EPSILON(1.0_dp)) THEN
     173            0 :                      zeff_scale = new_zeff/old_zeff
     174            0 :                      all_potential%ccore_charge = all_potential%ccore_charge*zeff_scale
     175              :                   END IF
     176            0 :                   all_potential%zeff = new_zeff
     177            0 :                   all_potential%zeff_correction = core_corr_ne
     178            0 :                   core_corr_changed = .TRUE.
     179              :                END IF
     180              :             END IF
     181            0 :             IF (ASSOCIATED(gth_potential)) THEN
     182            0 :                IF (ABS(gth_potential%zeff_correction - core_corr_ne) > core_corr_refresh_tol) THEN
     183            0 :                   old_zeff = gth_potential%zeff
     184            0 :                   base_zeff = old_zeff - gth_potential%zeff_correction
     185            0 :                   new_zeff = base_zeff + core_corr_ne
     186            0 :                   IF (ABS(old_zeff) > EPSILON(1.0_dp)) THEN
     187            0 :                      zeff_scale = new_zeff/old_zeff
     188            0 :                      gth_potential%ccore_charge = gth_potential%ccore_charge*zeff_scale
     189            0 :                      gth_potential%cerf_ppl = gth_potential%cerf_ppl*zeff_scale
     190              :                   END IF
     191            0 :                   gth_potential%zeff = new_zeff
     192            0 :                   gth_potential%zeff_correction = core_corr_ne
     193            0 :                   core_corr_changed = .TRUE.
     194              :                END IF
     195              :             END IF
     196            0 :             IF (ASSOCIATED(sgp_potential)) THEN
     197            0 :                IF (ABS(sgp_potential%zeff_correction - core_corr_ne) > core_corr_refresh_tol) THEN
     198            0 :                   old_zeff = sgp_potential%zeff
     199            0 :                   base_zeff = old_zeff - sgp_potential%zeff_correction
     200            0 :                   new_zeff = base_zeff + core_corr_ne
     201            0 :                   IF (ABS(old_zeff) > EPSILON(1.0_dp)) THEN
     202            0 :                      zeff_scale = new_zeff/old_zeff
     203            0 :                      sgp_potential%ccore_charge = sgp_potential%ccore_charge*zeff_scale
     204              :                   END IF
     205            0 :                   sgp_potential%zeff = new_zeff
     206            0 :                   sgp_potential%zeff_correction = core_corr_ne
     207            0 :                   core_corr_changed = .TRUE.
     208              :                END IF
     209              :             END IF
     210              :          END IF
     211              :       END DO
     212              : 
     213            0 :       IF (.NOT. found_ne) THEN
     214            0 :          CPABORT("no Ne atomic kind was found.")
     215              :       END IF
     216              : 
     217            0 :       output_unit = cp_logger_get_default_io_unit()
     218            0 :       IF (output_unit > 0) THEN
     219            0 :          WRITE (output_unit, *) " @ DRIVER MODE: Ne-electrode target_nelect = ", target_nelect, &
     220            0 :             " neutral_electrons = ", neutral_electrons_ref
     221            0 :          WRITE (output_unit, *) " @ DRIVER MODE: Ne atoms = ", n_ne_atoms, &
     222            0 :             " CORE_CORRECTION per Ne = ", core_corr_ne
     223            0 :          IF (.NOT. core_corr_changed) THEN
     224            0 :             WRITE (output_unit, *) " @ DRIVER MODE: Ne CORE_CORRECTION refresh skipped; change <= ", &
     225            0 :                core_corr_refresh_tol
     226              :          END IF
     227              :       END IF
     228              : 
     229            0 :       IF (core_corr_changed) THEN
     230            0 :          CALL get_qs_kind_set(qs_kind_set, total_zeff_corr=qs_env%total_zeff_corr)
     231              :       END IF
     232              : 
     233            0 :       IF (core_corr_changed .AND. ASSOCIATED(ks_env)) THEN
     234              :          CALL set_ks_env(ks_env, potential_changed=.TRUE., &
     235            0 :                          forces_up_to_date=.FALSE.)
     236              :       END IF
     237              : 
     238            0 :       CALL timestop(handle)
     239              : 
     240            0 :    END SUBROUTINE apply_ne_core_correction
     241              : 
     242              : END MODULE qs_electrons_ne_electrode
        

Generated by: LCOV version 2.0-1