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

            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 all routins needed for a nonperiodic  electric field
      10              : ! **************************************************************************************************
      11              : 
      12              : MODULE efield_utils
      13              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      14              :                                               get_atomic_kind,&
      15              :                                               get_atomic_kind_set
      16              :    USE cell_types,                      ONLY: cell_type,&
      17              :                                               pbc
      18              :    USE cp_control_types,                ONLY: dft_control_type,&
      19              :                                               efield_type
      20              :    USE cp_dbcsr_api,                    ONLY: dbcsr_add,&
      21              :                                               dbcsr_copy,&
      22              :                                               dbcsr_p_type,&
      23              :                                               dbcsr_set
      24              :    USE cp_dbcsr_operations,             ONLY: dbcsr_allocate_matrix_set,&
      25              :                                               dbcsr_deallocate_matrix_set
      26              :    USE input_constants,                 ONLY: constant_env,&
      27              :                                               custom_env,&
      28              :                                               gaussian_env,&
      29              :                                               ramp_env
      30              :    USE kinds,                           ONLY: dp
      31              :    USE mathconstants,                   ONLY: pi,&
      32              :                                               twopi
      33              :    USE message_passing,                 ONLY: mp_para_env_type
      34              :    USE particle_types,                  ONLY: particle_type
      35              :    USE physcon,                         ONLY: c_light_au
      36              :    USE qs_efield_local,                 ONLY: qs_efield_dipole_force
      37              :    USE qs_energy_types,                 ONLY: qs_energy_type
      38              :    USE qs_environment_types,            ONLY: get_qs_env,&
      39              :                                               qs_environment_type
      40              :    USE qs_force_types,                  ONLY: qs_force_type
      41              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      42              :                                               qs_kind_type
      43              :    USE qs_moments,                      ONLY: build_local_moment_matrix
      44              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
      45              :                                               qs_rho_type
      46              : #include "./base/base_uses.f90"
      47              : 
      48              :    IMPLICIT NONE
      49              : 
      50              :    PRIVATE
      51              : 
      52              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'efield_utils'
      53              : 
      54              : ! *** Public subroutines ***
      55              : 
      56              :    PUBLIC :: efield_potential_lengh_gauge, &
      57              :              calculate_ecore_efield, &
      58              :              make_field
      59              : 
      60              : CONTAINS
      61              : 
      62              : ! **************************************************************************************************
      63              : !> \brief Replace the original implementation of the electric-electronic
      64              : !>        interaction in the length gauge. This calculation is no longer done in
      65              : !>        the grid but using matrices to match the velocity gauge implementation.
      66              : !>        Note: The energy is stored in energy%core and computed later on.
      67              : !> \param qs_env ...
      68              : !> \param calculate_forces add the electronic force from the derivative of the dipole integrals
      69              : !> \author Guillaume Le Breton (02.23)
      70              : ! **************************************************************************************************
      71              : 
      72          394 :    SUBROUTINE efield_potential_lengh_gauge(qs_env, calculate_forces)
      73              : 
      74              :       TYPE(qs_environment_type), POINTER                 :: qs_env
      75              :       LOGICAL, INTENT(IN), OPTIONAL                      :: calculate_forces
      76              : 
      77              :       CHARACTER(len=*), PARAMETER :: routineN = 'efield_potential_lengh_gauge'
      78              : 
      79              :       INTEGER                                            :: handle, i, iatom, image, natom
      80          394 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind, kind_of
      81              :       LOGICAL                                            :: my_force
      82              :       REAL(kind=dp)                                      :: field(3)
      83          394 :       REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :)        :: fdip
      84          394 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
      85          394 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_p, matrix_s, moments
      86          394 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_h
      87              :       TYPE(dft_control_type), POINTER                    :: dft_control
      88              :       TYPE(mp_para_env_type), POINTER                    :: para_env
      89          394 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
      90              :       TYPE(qs_rho_type), POINTER                         :: rho
      91              : 
      92          394 :       NULLIFY (dft_control)
      93          394 :       CALL timeset(routineN, handle)
      94              : 
      95              :       CALL get_qs_env(qs_env, &
      96              :                       dft_control=dft_control, &
      97              :                       matrix_h_kp=matrix_h, &
      98          394 :                       matrix_s=matrix_s)
      99              : 
     100          394 :       NULLIFY (moments)
     101          394 :       CALL dbcsr_allocate_matrix_set(moments, 3)
     102         1576 :       DO i = 1, 3
     103         1182 :          ALLOCATE (moments(i)%matrix)
     104         1182 :          CALL dbcsr_copy(moments(i)%matrix, matrix_s(1)%matrix, "Moments")
     105         1576 :          CALL dbcsr_set(moments(i)%matrix, 0.0_dp)
     106              :       END DO
     107              : 
     108          394 :       CALL build_local_moment_matrix(qs_env, moments, 1)
     109              : 
     110          394 :       CALL make_field(dft_control, field, qs_env%sim_step, qs_env%sim_time)
     111              : 
     112         1576 :       DO i = 1, 3
     113         2758 :          DO image = 1, dft_control%nimages
     114         2364 :             CALL dbcsr_add(matrix_h(1, image)%matrix, moments(i)%matrix, 1.0_dp, field(i))
     115              :          END DO
     116              :       END DO
     117              : 
     118          394 :       CALL dbcsr_deallocate_matrix_set(moments)
     119              : 
     120          394 :       my_force = .FALSE.
     121          394 :       IF (PRESENT(calculate_forces)) my_force = calculate_forces
     122          282 :       IF (my_force) THEN
     123              :          ! The dipole integrals are real and symmetric: only the real part of P contributes
     124              :          CALL get_qs_env(qs_env, rho=rho, force=force, atomic_kind_set=atomic_kind_set, &
     125          282 :                          natom=natom, para_env=para_env)
     126          282 :          CALL qs_rho_get(rho, rho_ao=matrix_p)
     127          846 :          ALLOCATE (fdip(3, natom))
     128          282 :          fdip = 0.0_dp
     129          282 :          CALL qs_efield_dipole_force(qs_env, matrix_p, field, [0.0_dp, 0.0_dp, 0.0_dp], fdip)
     130          282 :          CALL para_env%sum(fdip)
     131          282 :          CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
     132          990 :          DO iatom = 1, natom
     133              :             force(kind_of(iatom))%efield(:, atom_of_kind(iatom)) = &
     134         3114 :                force(kind_of(iatom))%efield(:, atom_of_kind(iatom)) + fdip(:, iatom)
     135              :          END DO
     136          282 :          DEALLOCATE (fdip)
     137              :       END IF
     138              : 
     139          394 :       CALL timestop(handle)
     140              : 
     141          788 :    END SUBROUTINE efield_potential_lengh_gauge
     142              : 
     143              : ! **************************************************************************************************
     144              : !> \brief computes the amplitude of the efield within a given envelop
     145              : !> \param dft_control ...
     146              : !> \param field ...
     147              : !> \param sim_step ...
     148              : !> \param sim_time ...
     149              : !> \author Florian Schiffmann (02.09)
     150              : ! **************************************************************************************************
     151              : 
     152         1852 :    SUBROUTINE make_field(dft_control, field, sim_step, sim_time)
     153              :       TYPE(dft_control_type), INTENT(IN)                 :: dft_control
     154              :       REAL(dp), INTENT(OUT)                              :: field(3)
     155              :       INTEGER, INTENT(IN)                                :: sim_step
     156              :       REAL(KIND=dp), INTENT(IN)                          :: sim_time
     157              : 
     158              :       INTEGER                                            :: i, lower, nfield, upper
     159              :       REAL(dp)                                           :: E_0, env, nu, pol(3), strength
     160              :       REAL(KIND=dp)                                      :: dt
     161              :       TYPE(efield_type), POINTER                         :: efield
     162              : 
     163         1852 :       field = 0._dp
     164         1852 :       nfield = SIZE(dft_control%efield_fields)
     165         3704 :       DO i = 1, nfield
     166         1852 :          efield => dft_control%efield_fields(i)%efield
     167         1852 :          nu = 0.0_dp
     168         1852 :          IF (.NOT. efield%envelop_id == custom_env .AND. efield%wavelength > EPSILON(0.0_dp)) THEN
     169         1736 :             nu = c_light_au/(efield%wavelength) !in case of a custom efield we do not need nu
     170              :          END IF
     171         1852 :          E_0 = efield%amplitude
     172              : 
     173         7408 :          IF (DOT_PRODUCT(efield%polarisation, efield%polarisation) == 0) THEN
     174            0 :             pol(:) = 1.0_dp/3.0_dp
     175              :          ELSE
     176        12964 :             pol(:) = efield%polarisation(:)/NORM2(efield%polarisation)
     177              :          END IF
     178              : 
     179         1852 :          SELECT CASE (efield%envelop_id)
     180              :          CASE (constant_env)
     181          798 :             IF (sim_step >= efield%envelop_i_vars(1) .AND. &
     182          120 :                 (sim_step <= efield%envelop_i_vars(2) .OR. efield%envelop_i_vars(2) < 0)) THEN
     183              :                field = field + E_0*COS(sim_time*nu*twopi + &
     184         2862 :                                        efield%phase_offset*pi)*pol(:)
     185              :             END IF
     186              :          CASE (ramp_env)
     187          120 :             IF (sim_step >= efield%envelop_i_vars(1) .AND. sim_step <= efield%envelop_i_vars(2)) THEN
     188           52 :                strength = E_0*(sim_step - efield%envelop_i_vars(1))/(efield%envelop_i_vars(2) - efield%envelop_i_vars(1))
     189              :             END IF
     190          120 :             IF (sim_step >= efield%envelop_i_vars(3) .AND. sim_step <= efield%envelop_i_vars(4)) THEN
     191           60 :                strength = E_0*(efield%envelop_i_vars(4) - sim_step)/(efield%envelop_i_vars(4) - efield%envelop_i_vars(3))
     192              :             END IF
     193          120 :             IF (sim_step > efield%envelop_i_vars(4) .AND. efield%envelop_i_vars(4) > 0) strength = 0.0_dp
     194          120 :             IF (sim_step <= efield%envelop_i_vars(1)) strength = 0.0_dp
     195              :             field = field + strength*COS(sim_time*nu*twopi + &
     196          480 :                                          efield%phase_offset*pi)*pol(:)
     197              :          CASE (gaussian_env)
     198          818 :             env = EXP(-0.5_dp*((sim_time - efield%envelop_r_vars(1))/efield%envelop_r_vars(2))**2.0_dp)
     199              :             field = field + E_0*env*COS(sim_time*nu*twopi + &
     200         3272 :                                         efield%phase_offset*pi)*pol(:)
     201              :          CASE (custom_env)
     202          116 :             dt = efield%envelop_r_vars(1)
     203          116 :             IF (sim_time < (SIZE(efield%envelop_r_vars) - 2)*dt) THEN
     204              :                !make a linear interpolation between the two next points
     205           64 :                lower = FLOOR(sim_time/dt)
     206           64 :                upper = lower + 1
     207              :                strength = (efield%envelop_r_vars(lower + 2)*(upper*dt - sim_time) &
     208           64 :                            + efield%envelop_r_vars(upper + 2)*(sim_time - lower*dt))/dt
     209              :             ELSE
     210              :                strength = 0.0_dp
     211              :             END IF
     212         2316 :             field = field + strength*pol(:)
     213              :          CASE default
     214              :          END SELECT
     215              :       END DO
     216              : 
     217         1852 :    END SUBROUTINE make_field
     218              : 
     219              : ! **************************************************************************************************
     220              : !> \brief Computes the force and the energy due to a efield on the cores
     221              : !>        Note: In the velocity gauge, the energy term is not added because
     222              : !>        it would lead to an unbalanced energy (center of negative charge not
     223              : !>        involved in the electric energy in this gauge).
     224              : !> \param qs_env ...
     225              : !> \param calculate_forces ...
     226              : !> \author Florian Schiffmann (02.09)
     227              : ! **************************************************************************************************
     228              : 
     229        20991 :    SUBROUTINE calculate_ecore_efield(qs_env, calculate_forces)
     230              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     231              :       LOGICAL, OPTIONAL                                  :: calculate_forces
     232              : 
     233              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_ecore_efield'
     234              : 
     235              :       INTEGER                                            :: atom_a, handle, iatom, ikind, natom, &
     236              :                                                             nkind
     237        20991 :       INTEGER, DIMENSION(:), POINTER                     :: list
     238              :       LOGICAL                                            :: my_force
     239              :       REAL(KIND=dp)                                      :: efield_ener, zeff
     240              :       REAL(KIND=dp), DIMENSION(3)                        :: field, r
     241        20991 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     242              :       TYPE(cell_type), POINTER                           :: cell
     243              :       TYPE(dft_control_type), POINTER                    :: dft_control
     244        20991 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     245              :       TYPE(qs_energy_type), POINTER                      :: energy
     246        20991 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     247        20991 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     248              : 
     249        20991 :       NULLIFY (dft_control)
     250        20991 :       CALL timeset(routineN, handle)
     251        20991 :       CALL get_qs_env(qs_env, dft_control=dft_control)
     252        20991 :       IF (dft_control%apply_efield_field .OR. dft_control%apply_vector_potential) THEN
     253          750 :          my_force = .FALSE.
     254          750 :          IF (PRESENT(calculate_forces)) my_force = calculate_forces
     255              : 
     256              :          CALL get_qs_env(qs_env=qs_env, &
     257              :                          atomic_kind_set=atomic_kind_set, &
     258              :                          qs_kind_set=qs_kind_set, &
     259              :                          energy=energy, &
     260              :                          particle_set=particle_set, &
     261          750 :                          cell=cell)
     262          750 :          efield_ener = 0.0_dp
     263          750 :          nkind = SIZE(atomic_kind_set)
     264          750 :          CALL make_field(dft_control, field, qs_env%sim_step, qs_env%sim_time)
     265              : 
     266         2006 :          DO ikind = 1, SIZE(atomic_kind_set)
     267         1256 :             CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=list, natom=natom)
     268         1256 :             CALL get_qs_kind(qs_kind_set(ikind), zeff=zeff)
     269              : 
     270         1256 :             natom = SIZE(list)
     271         4012 :             DO iatom = 1, natom
     272         2006 :                IF (dft_control%apply_efield_field) THEN
     273         1068 :                   atom_a = list(iatom)
     274         1068 :                   r(:) = pbc(particle_set(atom_a)%r(:), cell)
     275         4272 :                   efield_ener = efield_ener - zeff*DOT_PRODUCT(r, field)
     276              :                END IF
     277         3262 :                IF (my_force) THEN
     278         1344 :                   CALL get_qs_env(qs_env=qs_env, force=force)
     279         5376 :                   force(ikind)%efield(:, iatom) = force(ikind)%efield(:, iatom) - field*zeff
     280              :                END IF
     281              :             END DO
     282              : 
     283              :          END DO
     284          750 :          IF (dft_control%apply_efield_field) energy%efield_core = efield_ener
     285              :       END IF
     286        20991 :       CALL timestop(handle)
     287        20991 :    END SUBROUTINE calculate_ecore_efield
     288              : END MODULE efield_utils
        

Generated by: LCOV version 2.0-1