LCOV - code coverage report
Current view: top level - src - qs_electrons_utils.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 95.8 % 24 23
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              : ! **************************************************************************************************
       9              : !> \brief Propagation of the electronic degree of freedom in constant-potential MD:
      10              : !>        generalized force, momentum kick, position drift and CSVR potentiostat step.
      11              : !> \author Nanchen DONGFANG
      12              : ! **************************************************************************************************
      13              : MODULE qs_electrons_utils
      14              :    USE csvr_system_utils,               ONLY: rescaling_factor
      15              :    USE kinds,                           ONLY: dp
      16              :    USE physcon,                         ONLY: femtoseconds,&
      17              :                                               kelvin
      18              :    USE qs_electrons_types,              ONLY: qs_electrons_type
      19              : #include "./base/base_uses.f90"
      20              : 
      21              :    IMPLICIT NONE
      22              : 
      23              :    PRIVATE
      24              :    PUBLIC :: qs_electrons_force, &
      25              :              qs_electrons_kick, &
      26              :              qs_electrons_drift, &
      27              :              qs_electrons_thermostat_step
      28              : 
      29              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_electrons_utils'
      30              :    REAL(KIND=dp), PARAMETER, PRIVATE :: HARTREE_TO_EV = 27.211386245988_dp
      31              : 
      32              : CONTAINS
      33              : 
      34              : ! **************************************************************************************************
      35              : !> \brief Generalized electronic force: deviation of the current work function
      36              : !>        from the target, driving q toward the requested constant-potential state.
      37              : !> \param electrons ...
      38              : !> \return ...
      39              : ! **************************************************************************************************
      40            4 :    FUNCTION qs_electrons_force(electrons) RESULT(force)
      41              :       TYPE(qs_electrons_type), POINTER                   :: electrons
      42              :       REAL(KIND=dp)                                      :: force
      43              : 
      44            4 :       CPASSERT(ASSOCIATED(electrons))
      45            4 :       IF (electrons%control_fermi) THEN
      46              :          ! Fermi-level control. The sign is OPPOSITE to work-function control,
      47              :          ! and deliberately so: dPhi/dq < 0 while d(e_F)/dq > 0, so the restoring
      48              :          ! force has to change sign with the controlled variable:
      49              :          !     current_workfunction - target_workfunction   (workfunction mode)
      50              :          !     target_ef - current_ef                       (fermi mode)
      51            0 :          force = (electrons%target_fermi_level - electrons%current_fermi_level_eV)/HARTREE_TO_EV
      52              :       ELSE
      53            4 :          force = (electrons%current_workfunction - electrons%target_workfunction)/HARTREE_TO_EV
      54              :       END IF
      55              : 
      56            4 :    END FUNCTION qs_electrons_force
      57              : 
      58              : ! **************************************************************************************************
      59              : !> \brief Half-kick of the electronic momentum from the current generalized force.
      60              : !> \param electrons ...
      61              : !> \param dt ...
      62              : ! **************************************************************************************************
      63            4 :    SUBROUTINE qs_electrons_kick(electrons, dt)
      64              :       TYPE(qs_electrons_type), POINTER                   :: electrons
      65              :       REAL(KIND=dp), INTENT(IN)                          :: dt
      66              : 
      67            4 :       CPASSERT(ASSOCIATED(electrons))
      68              :       ! A fixed mixing weight means q carries no dynamics: freeze it rather than
      69              :       ! letting it drift under a force that no longer influences the mixing.
      70            4 :       IF (electrons%lambda_is_fixed) RETURN
      71            4 :       electrons%p = electrons%p + qs_electrons_force(electrons)*dt
      72              : 
      73              :    END SUBROUTINE qs_electrons_kick
      74              : 
      75              : ! **************************************************************************************************
      76              : !> \brief Drift of the electronic coordinate (total electron number).
      77              : !> \param electrons ...
      78              : !> \param dt ...
      79              : ! **************************************************************************************************
      80            4 :    SUBROUTINE qs_electrons_drift(electrons, dt)
      81              :       TYPE(qs_electrons_type), POINTER                   :: electrons
      82              :       REAL(KIND=dp), INTENT(IN)                          :: dt
      83              : 
      84            4 :       CPASSERT(ASSOCIATED(electrons))
      85            4 :       IF (electrons%lambda_is_fixed) RETURN
      86            4 :       electrons%q = electrons%q + electrons%p*dt/electrons%mass
      87              : 
      88              :    END SUBROUTINE qs_electrons_drift
      89              : 
      90              : ! **************************************************************************************************
      91              : !> \brief Stochastic velocity rescaling ("O" step) of the electronic momentum,
      92              : !>        reusing CP2K's own CSVR rescaling_factor (Bussi-Donadio-Parrinello).
      93              : !> \param electrons ...
      94              : !> \param dt ...
      95              : ! **************************************************************************************************
      96            2 :    SUBROUTINE qs_electrons_thermostat_step(electrons, dt)
      97              :       TYPE(qs_electrons_type), POINTER                   :: electrons
      98              :       REAL(KIND=dp), INTENT(IN)                          :: dt
      99              : 
     100              :       REAL(KIND=dp)                                      :: alpha, kk, old_kinetic, sigma, taut
     101              : 
     102            2 :       CPASSERT(ASSOCIATED(electrons))
     103              : 
     104            2 :       old_kinetic = 0.5_dp*electrons%p**2/electrons%mass
     105            2 :       kk = old_kinetic
     106              :       ! kelvin is the [a.u.] -> [K] conversion factor from physcon, so dividing
     107              :       ! a temperature in K by it directly yields k_B*T in Hartree atomic units
     108              :       ! -- this is the same idiom CP2K's own thermostat code uses, avoiding any
     109              :       ! SI/atomic-unit mismatch with the (atomic-units) kinetic energy above.
     110              :       ! sigma is the target kinetic energy ndeg*k_B*T/2 of the single (ndeg = 1)
     111              :       ! electronic degree of freedom.
     112            2 :       sigma = 0.5_dp*electrons%temp_potentiostat/kelvin
     113              : 
     114              :       ! rescaling_factor expects taut expressed in units of "how often this
     115              :       ! routine is called" (see csvr_system_utils.F).  tau_potentiostat is a
     116              :       ! plain number in femtoseconds, so the fs -> a.u. conversion is required.
     117              :       !
     118              :       ! NOTE ON THE ARGUMENT: dt here is the INTERVAL BETWEEN SUCCESSIVE CALLS,
     119              :       ! not the MD timestep.  It used to be the MD timestep with a hard-wired
     120              :       ! 0.5 factor baked in, which silently assumed the OBABO placement (two
     121              :       ! calls per step).  Under BAOAB the potentiostat fires ONCE per step, so
     122              :       ! the caller must be free to say so; nvt() now passes dt_md for BAOAB and
     123              :       ! langevin() passes 0.5*dt_md for its OBABO placement.  Getting this wrong
     124              :       ! changes the damping by a factor of two without any other symptom.
     125            2 :       taut = (electrons%tau_potentiostat/femtoseconds)/dt
     126              : 
     127              :       ! ndeg = 1: q is a single genuine degree of freedom.  With ndeg /= 1 the
     128              :       ! sumnoises(ndeg-1, ...) term samples toward 2-DOF kinetic-energy
     129              :       ! statistics and rescaling_factor also disables the momentum-reversal
     130              :       ! branch that gives the correct Langevin limit at ndeg = 1.
     131            2 :       alpha = rescaling_factor(kk, sigma, 1, taut, electrons%gaussian_rng_stream)
     132            2 :       electrons%p = electrons%p*alpha
     133              :       electrons%thermostat_energy = electrons%thermostat_energy + &
     134            2 :                                     old_kinetic - 0.5_dp*electrons%p**2/electrons%mass
     135              : 
     136            2 :    END SUBROUTINE qs_electrons_thermostat_step
     137              : 
     138              : END MODULE qs_electrons_utils
        

Generated by: LCOV version 2.0-1