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

            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 Calculation of the energies concerning the core charge distribution
      10              : !> \par History
      11              : !>      - Full refactoring of calculate_ecore and calculate_ecore_overlap (jhu)
      12              : !> \author Matthias Krack (27.04.2001)
      13              : ! **************************************************************************************************
      14              : MODULE qs_core_energies
      15              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      16              :                                               get_atomic_kind,&
      17              :                                               get_atomic_kind_set
      18              :    USE atprop_types,                    ONLY: atprop_array_init,&
      19              :                                               atprop_type
      20              :    USE cell_types,                      ONLY: cell_type,&
      21              :                                               pbc
      22              :    USE cp_dbcsr_api,                    ONLY: dbcsr_get_info,&
      23              :                                               dbcsr_p_type,&
      24              :                                               dbcsr_type
      25              :    USE cp_dbcsr_contrib,                ONLY: dbcsr_dot,&
      26              :                                               dbcsr_dot_batch
      27              :    USE distribution_1d_types,           ONLY: distribution_1d_type
      28              :    USE kinds,                           ONLY: dp
      29              :    USE mathconstants,                   ONLY: oorootpi,&
      30              :                                               twopi
      31              :    USE message_passing,                 ONLY: mp_comm_type,&
      32              :                                               mp_para_env_type
      33              :    USE particle_types,                  ONLY: particle_type
      34              :    USE qs_cneo_types,                   ONLY: cneo_potential_type
      35              :    USE qs_energy_types,                 ONLY: qs_energy_type
      36              :    USE qs_environment_types,            ONLY: get_qs_env,&
      37              :                                               qs_environment_type
      38              :    USE qs_force_types,                  ONLY: qs_force_type
      39              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      40              :                                               qs_kind_type
      41              :    USE qs_neighbor_list_types,          ONLY: get_iterator_info,&
      42              :                                               neighbor_list_iterate,&
      43              :                                               neighbor_list_iterator_create,&
      44              :                                               neighbor_list_iterator_p_type,&
      45              :                                               neighbor_list_iterator_release,&
      46              :                                               neighbor_list_set_p_type
      47              :    USE virial_methods,                  ONLY: virial_pair_force
      48              :    USE virial_types,                    ONLY: virial_type
      49              : #include "./base/base_uses.f90"
      50              : 
      51              :    IMPLICIT NONE
      52              : 
      53              :    PRIVATE
      54              : 
      55              : ! *** Global parameters ***
      56              : 
      57              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_core_energies'
      58              : 
      59              :    PUBLIC :: calculate_ptrace, &
      60              :              calculate_ecore_overlap, &
      61              :              calculate_ecore_self, calculate_ecore_alpha
      62              : 
      63              :    INTERFACE calculate_ptrace
      64              :       MODULE PROCEDURE calculate_ptrace_1, calculate_ptrace_gamma, calculate_ptrace_kp
      65              :    END INTERFACE
      66              : 
      67              : ! **************************************************************************************************
      68              : 
      69              : CONTAINS
      70              : 
      71              : ! **************************************************************************************************
      72              : !> \brief  Calculate the trace of a operator matrix with the density matrix.
      73              : !>         Sum over all spin components (in P, no spin in H)
      74              : !> \param hmat ...
      75              : !> \param pmat ...
      76              : !> \param ecore ...
      77              : !> \param nspin ...
      78              : !> \param spinmat   Flag for spin dependency of operator matrix
      79              : !> \date    29.07.2014
      80              : !> \par History
      81              : !>         - none
      82              : !> \author  JGH
      83              : !> \version 1.0
      84              : ! **************************************************************************************************
      85          144 :    SUBROUTINE calculate_ptrace_gamma(hmat, pmat, ecore, nspin, spinmat)
      86              : 
      87              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: hmat, pmat
      88              :       REAL(KIND=dp), INTENT(OUT)                         :: ecore
      89              :       INTEGER, INTENT(IN)                                :: nspin
      90              :       LOGICAL, INTENT(IN), OPTIONAL                      :: spinmat
      91              : 
      92              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_ptrace_gamma'
      93              : 
      94              :       INTEGER                                            :: handle, ispin
      95              :       LOGICAL                                            :: lspin
      96              :       REAL(KIND=dp)                                      :: etr
      97              : 
      98          144 :       CALL timeset(routineN, handle)
      99              : 
     100          144 :       lspin = .FALSE.
     101          144 :       IF (PRESENT(spinmat)) lspin = spinmat
     102              : 
     103          144 :       ecore = 0.0_dp
     104          288 :       DO ispin = 1, nspin
     105              :          etr = 0.0_dp
     106          144 :          IF (lspin) THEN
     107           72 :             CALL dbcsr_dot(hmat(ispin)%matrix, pmat(ispin)%matrix, etr)
     108              :          ELSE
     109           72 :             CALL dbcsr_dot(hmat(1)%matrix, pmat(ispin)%matrix, etr)
     110              :          END IF
     111          288 :          ecore = ecore + etr
     112              :       END DO
     113              : 
     114          144 :       CALL timestop(handle)
     115              : 
     116          144 :    END SUBROUTINE calculate_ptrace_gamma
     117              : 
     118              : ! **************************************************************************************************
     119              : !> \brief  Calculate the trace of a operator matrix with the density matrix.
     120              : !>         Sum over all spin components (in P, no spin in H) and the real space
     121              : !>         coordinates
     122              : !> \param hmat    H matrix
     123              : !> \param pmat    P matrices
     124              : !> \param ecore   Tr(HP) output
     125              : !> \param nspin   Number of P matrices
     126              : !> \param spinmat   Flag for spin dependency of operator matrix
     127              : !> \date    29.07.2014
     128              : !> \author  JGH
     129              : !> \version 1.0
     130              : ! **************************************************************************************************
     131       452206 :    SUBROUTINE calculate_ptrace_kp(hmat, pmat, ecore, nspin, spinmat)
     132              : 
     133              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: hmat, pmat
     134              :       REAL(KIND=dp), INTENT(OUT)                         :: ecore
     135              :       INTEGER, INTENT(IN)                                :: nspin
     136              :       LOGICAL, INTENT(IN), OPTIONAL                      :: spinmat
     137              : 
     138              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_ptrace_kp'
     139              : 
     140              :       INTEGER                                            :: handle, ic, ispin, j, nc
     141              :       LOGICAL                                            :: lspin
     142       452206 :       REAL(KIND=dp), ALLOCATABLE                         :: traces(:)
     143       452206 :       TYPE(dbcsr_p_type), ALLOCATABLE                    :: h(:), p(:)
     144              :       TYPE(mp_comm_type)                                 :: group
     145              : 
     146       452206 :       CALL timeset(routineN, handle)
     147              : 
     148       452206 :       lspin = .FALSE.
     149       452206 :       IF (PRESENT(spinmat)) lspin = spinmat
     150              : 
     151       452206 :       nc = SIZE(pmat, 2)
     152     14297278 :       ALLOCATE (h(nspin*nc), p(nspin*nc), traces(nspin*nc))
     153       981512 :       j = 0
     154       981512 :       DO ispin = 1, nspin
     155      6999636 :          DO ic = 1, nc
     156      6018124 :             j = j + 1
     157      6018124 :             IF (lspin) THEN
     158        43998 :                h(j)%matrix => hmat(ispin, ic)%matrix
     159              :             ELSE
     160      5974126 :                h(j)%matrix => hmat(1, ic)%matrix
     161              :             END IF
     162      6547430 :             p(j)%matrix => pmat(ispin, ic)%matrix
     163              :          END DO
     164              :       END DO
     165       452206 :       IF (SIZE(h) > 0) CALL dbcsr_get_info(h(1)%matrix, group=group)
     166       452206 :       CALL dbcsr_dot_batch(h, p, traces, comm=group)
     167              :       ! Retain the spin/image accumulation order after the common MPI reduction.
     168       452206 :       ecore = 0.0_dp
     169      6470330 :       DO j = 1, SIZE(traces)
     170      6470330 :          ecore = ecore + traces(j)
     171              :       END DO
     172              : 
     173       452206 :       CALL timestop(handle)
     174              : 
     175       904412 :    END SUBROUTINE calculate_ptrace_kp
     176              : 
     177              : ! **************************************************************************************************
     178              : !> \brief  Calculate the core Hamiltonian energy which includes the kinetic
     179              : !>          and the potential energy of the electrons. It is assumed, that
     180              : !>          the core Hamiltonian matrix h and the density matrix p have the
     181              : !>          same sparse matrix structure (same atomic blocks and block
     182              : !>          ordering)
     183              : !> \param h ...
     184              : !> \param p ...
     185              : !> \param ecore ...
     186              : !> \date    03.05.2001
     187              : !> \par History
     188              : !>         - simplified taking advantage of new non-redundant matrix
     189              : !>           structure (27.06.2003,MK)
     190              : !>         - simplified using DBCSR trace function (21.07.2010, jhu)
     191              : !> \author  MK
     192              : !> \version 1.0
     193              : ! **************************************************************************************************
     194           56 :    SUBROUTINE calculate_ptrace_1(h, p, ecore)
     195              : 
     196              :       TYPE(dbcsr_type), POINTER                          :: h, p
     197              :       REAL(KIND=dp), INTENT(OUT)                         :: ecore
     198              : 
     199              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_ptrace_1'
     200              : 
     201              :       INTEGER                                            :: handle
     202              : 
     203           28 :       CALL timeset(routineN, handle)
     204              : 
     205              :       ecore = 0.0_dp
     206           28 :       CALL dbcsr_dot(h, p, ecore)
     207              : 
     208           28 :       CALL timestop(handle)
     209              : 
     210           28 :    END SUBROUTINE calculate_ptrace_1
     211              : 
     212              : ! **************************************************************************************************
     213              : !> \brief   Calculate the overlap energy of the core charge distribution.
     214              : !> \param qs_env ...
     215              : !> \param para_env ...
     216              : !> \param calculate_forces ...
     217              : !> \param molecular ...
     218              : !> \param E_overlap_core ...
     219              : !> \param atecc ...
     220              : !> \date    30.04.2001
     221              : !> \par History
     222              : !>       - Force calculation added (03.06.2002,MK)
     223              : !>       - Parallelized using a list of local atoms for rows and
     224              : !>         columns (19.07.2003,MK)
     225              : !>       - Use precomputed neighborlists (sab_core) and nl iterator (28.07.2010,jhu)
     226              : !> \author  MK
     227              : !> \version 1.0
     228              : ! **************************************************************************************************
     229        23121 :    SUBROUTINE calculate_ecore_overlap(qs_env, para_env, calculate_forces, molecular, &
     230        23121 :                                       E_overlap_core, atecc)
     231              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     232              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     233              :       LOGICAL, INTENT(IN)                                :: calculate_forces
     234              :       LOGICAL, INTENT(IN), OPTIONAL                      :: molecular
     235              :       REAL(KIND=dp), INTENT(OUT), OPTIONAL               :: E_overlap_core
     236              :       REAL(KIND=dp), DIMENSION(:), OPTIONAL              :: atecc
     237              : 
     238              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_ecore_overlap'
     239              : 
     240              :       INTEGER                                            :: atom_a, atom_b, handle, iatom, ikind, &
     241              :                                                             jatom, jkind, natom, nkind
     242        23121 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind
     243              :       LOGICAL                                            :: atenergy, only_molecule, use_virial
     244              :       REAL(KIND=dp)                                      :: aab, dab, eab, ecore_overlap, f, fab, &
     245              :                                                             rab2, rootaab, zab
     246        23121 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: alpha, radius, zeff
     247              :       REAL(KIND=dp), DIMENSION(3)                        :: deab, rab
     248              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: pv_loc
     249        23121 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     250              :       TYPE(atprop_type), POINTER                         :: atprop
     251              :       TYPE(cneo_potential_type), POINTER                 :: cneo_potential
     252              :       TYPE(mp_comm_type)                                 :: group
     253              :       TYPE(neighbor_list_iterator_p_type), &
     254        23121 :          DIMENSION(:), POINTER                           :: nl_iterator
     255              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     256        23121 :          POINTER                                         :: sab_core
     257        23121 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     258              :       TYPE(qs_energy_type), POINTER                      :: energy
     259        23121 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     260        23121 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     261              :       TYPE(virial_type), POINTER                         :: virial
     262              : 
     263        23121 :       CALL timeset(routineN, handle)
     264              : 
     265        23121 :       NULLIFY (atomic_kind_set)
     266        23121 :       NULLIFY (qs_kind_set)
     267        23121 :       NULLIFY (energy)
     268        23121 :       NULLIFY (atprop)
     269        23121 :       NULLIFY (force)
     270        23121 :       NULLIFY (particle_set)
     271              : 
     272        23121 :       group = para_env
     273              : 
     274        23121 :       only_molecule = .FALSE.
     275        23121 :       IF (PRESENT(molecular)) only_molecule = molecular
     276              : 
     277              :       CALL get_qs_env(qs_env=qs_env, &
     278              :                       atomic_kind_set=atomic_kind_set, &
     279              :                       qs_kind_set=qs_kind_set, &
     280              :                       particle_set=particle_set, &
     281              :                       energy=energy, &
     282              :                       force=force, &
     283              :                       sab_core=sab_core, &
     284              :                       atprop=atprop, &
     285        23121 :                       virial=virial)
     286              : 
     287              :       ! Allocate work storage
     288        23121 :       nkind = SIZE(atomic_kind_set)
     289        23121 :       natom = SIZE(particle_set)
     290              : 
     291        23121 :       use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
     292              : 
     293       115605 :       ALLOCATE (alpha(nkind), radius(nkind), zeff(nkind))
     294        23121 :       alpha(:) = 0.0_dp
     295        23121 :       radius(:) = 0.0_dp
     296        23121 :       zeff(:) = 0.0_dp
     297              : 
     298        23121 :       IF (calculate_forces) THEN
     299         7237 :          CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind)
     300              :       END IF
     301              : 
     302        23121 :       atenergy = .FALSE.
     303        23121 :       IF (ASSOCIATED(atprop)) THEN
     304        23121 :          IF (atprop%energy) THEN
     305           58 :             atenergy = .TRUE.
     306           58 :             CALL atprop_array_init(atprop%atecc, natom)
     307              :          END IF
     308              :       END IF
     309              : 
     310        63390 :       DO ikind = 1, nkind
     311              :          ! cneo quantum nuclei have their core energies calculated elsewhere
     312        40269 :          NULLIFY (cneo_potential)
     313        40269 :          CALL get_qs_kind(qs_kind_set(ikind), cneo_potential=cneo_potential)
     314        63390 :          IF (ASSOCIATED(cneo_potential)) THEN
     315           14 :             alpha(ikind) = 1.0_dp
     316           14 :             radius(ikind) = 1.0_dp
     317           14 :             zeff(ikind) = 0.0_dp
     318              :          ELSE
     319              :             CALL get_qs_kind(qs_kind_set(ikind), &
     320              :                              alpha_core_charge=alpha(ikind), &
     321              :                              core_charge_radius=radius(ikind), &
     322        40255 :                              zeff=zeff(ikind))
     323              :          END IF
     324              :       END DO
     325              : 
     326        23121 :       ecore_overlap = 0.0_dp
     327        23121 :       pv_loc = 0.0_dp
     328              : 
     329        23121 :       CALL neighbor_list_iterator_create(nl_iterator, sab_core)
     330       119186 :       DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
     331        96065 :          CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, iatom=iatom, jatom=jatom, r=rab)
     332        96065 :          zab = zeff(ikind)*zeff(jkind)
     333        96065 :          aab = alpha(ikind)*alpha(jkind)/(alpha(ikind) + alpha(jkind))
     334        96065 :          rootaab = SQRT(aab)
     335        96065 :          fab = 2.0_dp*oorootpi*zab*rootaab
     336        96065 :          rab2 = rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3)
     337       119186 :          IF (rab2 > 1.e-8_dp) THEN
     338        53685 :             IF (ikind == jkind .AND. iatom == jatom) THEN
     339              :                f = 0.5_dp
     340              :             ELSE
     341        51981 :                f = 1.0_dp
     342              :             END IF
     343        53685 :             dab = SQRT(rab2)
     344        53685 :             eab = zab*erfc(rootaab*dab)/dab
     345        53685 :             ecore_overlap = ecore_overlap + f*eab
     346        53685 :             IF (atenergy) THEN
     347           98 :                atprop%atecc(iatom) = atprop%atecc(iatom) + 0.5_dp*f*eab
     348           98 :                atprop%atecc(jatom) = atprop%atecc(jatom) + 0.5_dp*f*eab
     349              :             END IF
     350        53685 :             IF (PRESENT(atecc)) THEN
     351           55 :                atecc(iatom) = atecc(iatom) + 0.5_dp*f*eab
     352           55 :                atecc(jatom) = atecc(jatom) + 0.5_dp*f*eab
     353              :             END IF
     354        53685 :             IF (calculate_forces) THEN
     355        70932 :                deab(:) = rab(:)*f*(eab + fab*EXP(-aab*rab2))/rab2
     356        17733 :                atom_a = atom_of_kind(iatom)
     357        17733 :                atom_b = atom_of_kind(jatom)
     358        70932 :                force(ikind)%core_overlap(:, atom_a) = force(ikind)%core_overlap(:, atom_a) + deab(:)
     359        70932 :                force(jkind)%core_overlap(:, atom_b) = force(jkind)%core_overlap(:, atom_b) - deab(:)
     360        17733 :                IF (use_virial) THEN
     361         1711 :                   CALL virial_pair_force(pv_loc, 1._dp, deab, rab)
     362              :                END IF
     363              :             END IF
     364              :          END IF
     365              :       END DO
     366        23121 :       CALL neighbor_list_iterator_release(nl_iterator)
     367              : 
     368        23121 :       DEALLOCATE (alpha, radius, zeff)
     369        23121 :       IF (calculate_forces) THEN
     370         7237 :          DEALLOCATE (atom_of_kind)
     371              :       END IF
     372        23121 :       IF (calculate_forces .AND. use_virial) THEN
     373        10894 :          virial%pv_ecore_overlap = virial%pv_ecore_overlap + pv_loc
     374        10894 :          virial%pv_virial = virial%pv_virial + pv_loc
     375              :       END IF
     376              : 
     377        23121 :       CALL group%sum(ecore_overlap)
     378              : 
     379        23121 :       energy%core_overlap = ecore_overlap
     380              : 
     381        23121 :       IF (PRESENT(E_overlap_core)) THEN
     382         2500 :          E_overlap_core = energy%core_overlap
     383              :       END IF
     384              : 
     385        23121 :       CALL timestop(handle)
     386              : 
     387        46242 :    END SUBROUTINE calculate_ecore_overlap
     388              : 
     389              : ! **************************************************************************************************
     390              : !> \brief   Calculate the self energy of the core charge distribution.
     391              : !> \param qs_env ...
     392              : !> \param E_self_core ...
     393              : !> \param atecc ...
     394              : !> \date    27.04.2001
     395              : !> \author  MK
     396              : !> \version 1.0
     397              : ! **************************************************************************************************
     398        22639 :    SUBROUTINE calculate_ecore_self(qs_env, E_self_core, atecc)
     399              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     400              :       REAL(KIND=dp), INTENT(OUT), OPTIONAL               :: E_self_core
     401              :       REAL(KIND=dp), DIMENSION(:), OPTIONAL              :: atecc
     402              : 
     403              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_ecore_self'
     404              : 
     405              :       INTEGER                                            :: handle, iatom, ikind, iparticle_local, &
     406              :                                                             natom, nparticle_local
     407              :       REAL(KIND=dp)                                      :: alpha_core_charge, ecore_self, es, zeff
     408        22639 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     409              :       TYPE(atprop_type), POINTER                         :: atprop
     410              :       TYPE(cneo_potential_type), POINTER                 :: cneo_potential
     411              :       TYPE(distribution_1d_type), POINTER                :: local_particles
     412        22639 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     413              :       TYPE(qs_energy_type), POINTER                      :: energy
     414        22639 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     415              : 
     416              : ! -------------------------------------------------------------------------
     417              : 
     418        22639 :       NULLIFY (atprop)
     419        22639 :       CALL timeset(routineN, handle)
     420              : 
     421              :       CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, &
     422        22639 :                       qs_kind_set=qs_kind_set, energy=energy, atprop=atprop)
     423              : 
     424        22639 :       ecore_self = 0.0_dp
     425              : 
     426        62362 :       DO ikind = 1, SIZE(atomic_kind_set)
     427              :          ! nuclear density self-interaction is already removed in CNEO
     428        39723 :          NULLIFY (cneo_potential)
     429        39723 :          CALL get_qs_kind(qs_kind_set(ikind), cneo_potential=cneo_potential)
     430        39723 :          IF (ASSOCIATED(cneo_potential)) CYCLE
     431        39709 :          CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom)
     432        39709 :          CALL get_qs_kind(qs_kind_set(ikind), zeff=zeff, alpha_core_charge=alpha_core_charge)
     433        62362 :          ecore_self = ecore_self - REAL(natom, dp)*zeff**2*SQRT(alpha_core_charge)
     434              :       END DO
     435              : 
     436        22639 :       energy%core_self = ecore_self/SQRT(twopi)
     437        22639 :       IF (PRESENT(E_self_core)) THEN
     438         2018 :          E_self_core = energy%core_self
     439              :       END IF
     440              : 
     441        22639 :       IF (ASSOCIATED(atprop)) THEN
     442        22639 :          IF (atprop%energy) THEN
     443              :             ! atomic energy
     444              :             CALL get_qs_env(qs_env=qs_env, particle_set=particle_set, &
     445           58 :                             local_particles=local_particles)
     446           58 :             natom = SIZE(particle_set)
     447           58 :             CALL atprop_array_init(atprop%ateself, natom)
     448              : 
     449          172 :             DO ikind = 1, SIZE(atomic_kind_set)
     450              :                ! nuclear density self-interaction is already removed in CNEO
     451          114 :                NULLIFY (cneo_potential)
     452          114 :                CALL get_qs_kind(qs_kind_set(ikind), cneo_potential=cneo_potential)
     453          114 :                IF (ASSOCIATED(cneo_potential)) CYCLE
     454          114 :                nparticle_local = local_particles%n_el(ikind)
     455          114 :                CALL get_qs_kind(qs_kind_set(ikind), zeff=zeff, alpha_core_charge=alpha_core_charge)
     456          114 :                es = zeff**2*SQRT(alpha_core_charge)/SQRT(twopi)
     457          270 :                DO iparticle_local = 1, nparticle_local
     458           98 :                   iatom = local_particles%list(ikind)%array(iparticle_local)
     459          212 :                   atprop%ateself(iatom) = atprop%ateself(iatom) - es
     460              :                END DO
     461              :             END DO
     462              :          END IF
     463              :       END IF
     464        22639 :       IF (PRESENT(atecc)) THEN
     465              :          ! atomic energy
     466              :          CALL get_qs_env(qs_env=qs_env, particle_set=particle_set, &
     467           30 :                          local_particles=local_particles)
     468              :          natom = SIZE(particle_set)
     469           88 :          DO ikind = 1, SIZE(atomic_kind_set)
     470           58 :             nparticle_local = local_particles%n_el(ikind)
     471           58 :             CALL get_qs_kind(qs_kind_set(ikind), zeff=zeff, alpha_core_charge=alpha_core_charge)
     472           58 :             es = zeff**2*SQRT(alpha_core_charge)/SQRT(twopi)
     473          141 :             DO iparticle_local = 1, nparticle_local
     474           53 :                iatom = local_particles%list(ikind)%array(iparticle_local)
     475          111 :                atecc(iatom) = atecc(iatom) - es
     476              :             END DO
     477              :          END DO
     478              :       END IF
     479              : 
     480        22639 :       CALL timestop(handle)
     481              : 
     482        22639 :    END SUBROUTINE calculate_ecore_self
     483              : 
     484              : ! **************************************************************************************************
     485              : !> \brief Calculate the overlap and self energy of the core charge distribution for a given alpha
     486              : !>        Use a minimum image convention and double loop over all atoms
     487              : !> \param qs_env ...
     488              : !> \param alpha ...
     489              : !> \param atecc ...
     490              : !> \author  JGH
     491              : !> \version 1.0
     492              : ! **************************************************************************************************
     493            2 :    SUBROUTINE calculate_ecore_alpha(qs_env, alpha, atecc)
     494              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     495              :       REAL(KIND=dp), INTENT(IN)                          :: alpha
     496              :       REAL(KIND=dp), DIMENSION(:)                        :: atecc
     497              : 
     498              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_ecore_alpha'
     499              : 
     500              :       INTEGER                                            :: handle, iatom, ikind, jatom, jkind, &
     501              :                                                             natom, nkind
     502            2 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: kind_of
     503              :       REAL(KIND=dp)                                      :: dab, eab, fab, rootaab, zab
     504            2 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: zeff
     505              :       REAL(KIND=dp), DIMENSION(3)                        :: rab
     506            2 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     507              :       TYPE(cell_type), POINTER                           :: cell
     508            2 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     509            2 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     510              : 
     511            2 :       CALL timeset(routineN, handle)
     512              : 
     513              :       CALL get_qs_env(qs_env=qs_env, &
     514              :                       cell=cell, &
     515              :                       atomic_kind_set=atomic_kind_set, &
     516              :                       qs_kind_set=qs_kind_set, &
     517            2 :                       particle_set=particle_set)
     518            2 :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
     519              :       !
     520            2 :       nkind = SIZE(atomic_kind_set)
     521            2 :       natom = SIZE(particle_set)
     522            6 :       ALLOCATE (zeff(nkind))
     523            2 :       zeff(:) = 0.0_dp
     524            6 :       DO ikind = 1, nkind
     525            6 :          CALL get_qs_kind(qs_kind_set(ikind), zeff=zeff(ikind))
     526              :       END DO
     527              : 
     528            2 :       rootaab = SQRT(0.5_dp*alpha)
     529            8 :       DO iatom = 1, natom
     530            6 :          ikind = kind_of(iatom)
     531            6 :          atecc(iatom) = atecc(iatom) - zeff(ikind)**2*SQRT(alpha/twopi)
     532           14 :          DO jatom = iatom + 1, natom
     533            6 :             jkind = kind_of(jatom)
     534            6 :             zab = zeff(ikind)*zeff(jkind)
     535            6 :             fab = 2.0_dp*oorootpi*zab*rootaab
     536           24 :             rab = particle_set(iatom)%r - particle_set(jatom)%r
     537           24 :             rab = pbc(rab, cell)
     538           24 :             dab = SQRT(SUM(rab(:)**2))
     539            6 :             eab = zab*erfc(rootaab*dab)/dab
     540            6 :             atecc(iatom) = atecc(iatom) + 0.5_dp*eab
     541           12 :             atecc(jatom) = atecc(jatom) + 0.5_dp*eab
     542              :          END DO
     543              :       END DO
     544              : 
     545            2 :       DEALLOCATE (zeff)
     546              : 
     547            2 :       CALL timestop(handle)
     548              : 
     549            4 :    END SUBROUTINE calculate_ecore_alpha
     550              : 
     551              : END MODULE qs_core_energies
        

Generated by: LCOV version 2.0-1