LCOV - code coverage report
Current view: top level - src - core_ppl.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:936074a) Lines: 95.8 % 118 113
Test Date: 2025-12-04 06:27:48 Functions: 100.0 % 2 2

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2025 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : ! **************************************************************************************************
       8              : !> \brief Calculation of the local pseudopotential contribution to the core Hamiltonian
       9              : !>         <a|V(local)|b> = <a|Sum e^a*rc**2|b>
      10              : !> \par History
      11              : !>      - core_ppnl refactored from qs_core_hamiltonian [Joost VandeVondele, 2008-11-01]
      12              : !>      - adapted for PPL [jhu, 2009-02-23]
      13              : !>      - OpenMP added [Iain Bethune, Fiona Reid, 2013-11-13]
      14              : !>      - Bug fix: correct orbital pointer range [07.2014,JGH]
      15              : !>      - k-point aware [07.2015,JGH]
      16              : !>      - Extended by the derivatives for DFPT [Sandra Luber, Edward Ditler, 2021]
      17              : ! **************************************************************************************************
      18              : MODULE core_ppl
      19              : 
      20              :    USE ai_overlap_ppl,                  ONLY: ecploc_integral,&
      21              :                                               ppl_integral,&
      22              :                                               ppl_integral_ri
      23              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      24              :                                               get_atomic_kind_set
      25              :    USE basis_set_types,                 ONLY: gto_basis_set_p_type,&
      26              :                                               gto_basis_set_type
      27              :    USE cp_dbcsr_api,                    ONLY: dbcsr_add,&
      28              :                                               dbcsr_get_block_p,&
      29              :                                               dbcsr_p_type
      30              :    USE external_potential_types,        ONLY: get_potential,&
      31              :                                               gth_potential_type,&
      32              :                                               sgp_potential_type
      33              :    USE kinds,                           ONLY: dp,&
      34              :                                               int_8
      35              :    USE libgrpp_integrals,               ONLY: libgrpp_local_forces_ref,&
      36              :                                               libgrpp_local_integrals,&
      37              :                                               libgrpp_semilocal_forces_ref,&
      38              :                                               libgrpp_semilocal_integrals
      39              :    USE lri_environment_types,           ONLY: lri_kind_type
      40              :    USE orbital_pointers,                ONLY: init_orbital_pointers,&
      41              :                                               ncoset
      42              :    USE particle_types,                  ONLY: particle_type
      43              :    USE qs_force_types,                  ONLY: qs_force_type
      44              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      45              :                                               get_qs_kind_set,&
      46              :                                               qs_kind_type
      47              :    USE qs_neighbor_list_types,          ONLY: get_iterator_info,&
      48              :                                               neighbor_list_iterator_create,&
      49              :                                               neighbor_list_iterator_p_type,&
      50              :                                               neighbor_list_iterator_release,&
      51              :                                               neighbor_list_set_p_type,&
      52              :                                               nl_set_sub_iterator,&
      53              :                                               nl_sub_iterate
      54              :    USE virial_methods,                  ONLY: virial_pair_force
      55              :    USE virial_types,                    ONLY: virial_type
      56              : 
      57              : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
      58              : !$ USE OMP_LIB, ONLY: omp_lock_kind, &
      59              : !$                    omp_init_lock, omp_set_lock, &
      60              : !$                    omp_unset_lock, omp_destroy_lock
      61              : 
      62              : #include "./base/base_uses.f90"
      63              : 
      64              :    IMPLICIT NONE
      65              : 
      66              :    PRIVATE
      67              : 
      68              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'core_ppl'
      69              : 
      70              :    PUBLIC :: build_core_ppl, build_core_ppl_ri
      71              : 
      72              : CONTAINS
      73              : 
      74              : ! **************************************************************************************************
      75              : !> \brief ...
      76              : !> \param matrix_h ...
      77              : !> \param matrix_p ...
      78              : !> \param force ...
      79              : !> \param virial ...
      80              : !> \param calculate_forces ...
      81              : !> \param use_virial ...
      82              : !> \param nder ...
      83              : !> \param qs_kind_set ...
      84              : !> \param atomic_kind_set ...
      85              : !> \param particle_set ...
      86              : !> \param sab_orb ...
      87              : !> \param sac_ppl ...
      88              : !> \param nimages ...
      89              : !> \param cell_to_index ...
      90              : !> \param basis_type ...
      91              : !> \param deltaR Weighting factors of the derivatives wrt. nuclear positions
      92              : !> \param atcore ...
      93              : ! **************************************************************************************************
      94        17897 :    SUBROUTINE build_core_ppl(matrix_h, matrix_p, force, virial, calculate_forces, use_virial, nder, &
      95              :                              qs_kind_set, atomic_kind_set, particle_set, sab_orb, sac_ppl, &
      96        17897 :                              nimages, cell_to_index, basis_type, deltaR, atcore)
      97              : 
      98              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_h, matrix_p
      99              :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     100              :       TYPE(virial_type), POINTER                         :: virial
     101              :       LOGICAL, INTENT(IN)                                :: calculate_forces
     102              :       LOGICAL                                            :: use_virial
     103              :       INTEGER                                            :: nder
     104              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     105              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     106              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     107              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     108              :          POINTER                                         :: sab_orb, sac_ppl
     109              :       INTEGER, INTENT(IN)                                :: nimages
     110              :       INTEGER, DIMENSION(:, :, :), OPTIONAL, POINTER     :: cell_to_index
     111              :       CHARACTER(LEN=*), INTENT(IN)                       :: basis_type
     112              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(IN), &
     113              :          OPTIONAL                                        :: deltaR
     114              :       REAL(KIND=dp), DIMENSION(:), INTENT(INOUT), &
     115              :          OPTIONAL                                        :: atcore
     116              : 
     117              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'build_core_ppl'
     118              :       INTEGER, PARAMETER                                 :: nexp_max = 30
     119              : 
     120              :       INTEGER :: atom_a, handle, i, iatom, icol, ikind, img, irow, iset, jatom, jkind, jset, &
     121              :          katom, kkind, ldai, ldsab, maxco, maxder, maxl, maxlgto, maxlppl, maxnset, maxsgf, mepos, &
     122              :          n_local, natom, ncoa, ncob, nexp_lpot, nexp_ppl, nkind, nloc, nseta, nsetb, nthread, &
     123              :          sgfa, sgfb, slmax, slot
     124        17897 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind, kind_of
     125              :       INTEGER, DIMENSION(0:10)                           :: npot
     126              :       INTEGER, DIMENSION(1:10)                           :: nrloc
     127              :       INTEGER, DIMENSION(1:15, 0:10)                     :: nrpot
     128              :       INTEGER, DIMENSION(3)                              :: cellind
     129        17897 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, lb_max, lb_min, &
     130        17897 :                                                             nct_lpot, npgfa, npgfb, nsgfa, nsgfb
     131        17897 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa, first_sgfb
     132              :       INTEGER, DIMENSION(nexp_max)                       :: nct_ppl
     133              :       LOGICAL                                            :: do_dR, doat, dokp, ecp_local, &
     134              :                                                             ecp_semi_local, found, libgrpp_local, &
     135              :                                                             lpotextended, only_gaussians
     136              :       REAL(KIND=dp)                                      :: alpha, atk0, atk1, dab, dac, dbc, f0, &
     137              :                                                             ppl_radius
     138        17897 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: work
     139        17897 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: hab2_w, ppl_fwork, ppl_work
     140        17897 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :, :)  :: hab, pab
     141              :       REAL(KIND=dp), ALLOCATABLE, &
     142        17897 :          DIMENSION(:, :, :, :, :)                        :: hab2
     143              :       REAL(KIND=dp), DIMENSION(1:10)                     :: aloc, bloc
     144              :       REAL(KIND=dp), DIMENSION(1:15, 0:10)               :: apot, bpot
     145              :       REAL(KIND=dp), DIMENSION(3)                        :: force_a, force_b, rab, rac, rbc
     146              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: pv_thread
     147              :       TYPE(neighbor_list_iterator_p_type), &
     148        17897 :          DIMENSION(:), POINTER                           :: ap_iterator
     149              :       TYPE(gto_basis_set_type), POINTER                  :: basis_set_a, basis_set_b
     150        17897 :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_list
     151              :       TYPE(gth_potential_type), POINTER                  :: gth_potential
     152        35794 :       REAL(KIND=dp), DIMENSION(SIZE(particle_set))       :: at_thread
     153              :       REAL(KIND=dp), DIMENSION(nexp_max)                 :: alpha_ppl
     154        17897 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: cval_lpot, h1_1block, h1_2block, &
     155        17897 :                                                             h1_3block, h_block, p_block, rpgfa, &
     156        17897 :                                                             rpgfb, sphi_a, sphi_b, zeta, zetb
     157        17897 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: a_local, alpha_lpot, c_local, cexp_ppl, &
     158        17897 :                                                             set_radius_a, set_radius_b
     159              :       REAL(KIND=dp), DIMENSION(4, nexp_max)              :: cval_ppl
     160        35794 :       REAL(KIND=dp), DIMENSION(3, SIZE(particle_set))    :: force_thread
     161              :       TYPE(sgp_potential_type), POINTER                  :: sgp_potential
     162              : 
     163              : !$    INTEGER(kind=omp_lock_kind), &
     164        17897 : !$       ALLOCATABLE, DIMENSION(:) :: locks
     165              : !$    INTEGER                                            :: lock_num, hash, hash1, hash2
     166              : !$    INTEGER(KIND=int_8)                                :: iatom8
     167              : !$    INTEGER, PARAMETER                                 :: nlock = 501
     168              : 
     169        17897 :       do_dR = PRESENT(deltaR)
     170        17897 :       doat = PRESENT(atcore)
     171        17897 :       IF ((calculate_forces .OR. doat) .AND. do_dR) THEN
     172            0 :          CPABORT("core_ppl: incompatible options")
     173              :       END IF
     174              : 
     175              :       MARK_USED(int_8)
     176              : 
     177              :       ! Use internal integral routine for local ECP terms or use libgrrp
     178        17897 :       libgrpp_local = .FALSE.
     179              : 
     180        17897 :       IF (calculate_forces) THEN
     181         7413 :          CALL timeset(routineN//"_forces", handle)
     182              :       ELSE
     183        10484 :          CALL timeset(routineN, handle)
     184              :       END IF
     185              : 
     186        17897 :       nkind = SIZE(atomic_kind_set)
     187        17897 :       natom = SIZE(particle_set)
     188              : 
     189        17897 :       dokp = (nimages > 1)
     190              : 
     191        17897 :       IF (dokp) THEN
     192          238 :          CPASSERT(PRESENT(cell_to_index) .AND. ASSOCIATED(cell_to_index))
     193              :       END IF
     194              : 
     195        17897 :       IF (calculate_forces .OR. doat) THEN
     196         7475 :          IF (SIZE(matrix_p, 1) == 2) THEN
     197         2502 :             DO img = 1, nimages
     198              :                CALL dbcsr_add(matrix_p(1, img)%matrix, matrix_p(2, img)%matrix, &
     199         1552 :                               alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
     200              :                CALL dbcsr_add(matrix_p(2, img)%matrix, matrix_p(1, img)%matrix, &
     201         2502 :                               alpha_scalar=-2.0_dp, beta_scalar=1.0_dp)
     202              :             END DO
     203              :          END IF
     204              :       END IF
     205       283825 :       force_thread = 0.0_dp
     206        84379 :       at_thread = 0.0_dp
     207              : 
     208        17897 :       maxder = ncoset(nder)
     209              : 
     210              :       CALL get_qs_kind_set(qs_kind_set, maxco=maxco, maxlgto=maxlgto, &
     211              :                            maxsgf=maxsgf, maxnset=maxnset, maxlppl=maxlppl, &
     212        17897 :                            basis_type=basis_type)
     213              : 
     214        17897 :       maxl = MAX(maxlgto, maxlppl)
     215        17897 :       CALL init_orbital_pointers(2*maxl + 2*nder + 1)
     216              : 
     217        17897 :       ldsab = MAX(maxco, ncoset(maxlppl), maxsgf, maxlppl)
     218        17897 :       ldai = ncoset(maxl + nder + 1)
     219              : 
     220        85428 :       ALLOCATE (basis_set_list(nkind))
     221        49634 :       DO ikind = 1, nkind
     222        31737 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=basis_set_a, basis_type=basis_type)
     223        49634 :          IF (ASSOCIATED(basis_set_a)) THEN
     224        31737 :             basis_set_list(ikind)%gto_basis_set => basis_set_a
     225              :          ELSE
     226            0 :             NULLIFY (basis_set_list(ikind)%gto_basis_set)
     227              :          END IF
     228              :       END DO
     229              : 
     230        17897 :       pv_thread = 0.0_dp
     231              : 
     232              :       nthread = 1
     233        17897 : !$    nthread = omp_get_max_threads()
     234              : 
     235              :       ! iterator for basis/potential list
     236        17897 :       CALL neighbor_list_iterator_create(ap_iterator, sac_ppl, search=.TRUE., nthread=nthread)
     237              : 
     238              : !$OMP PARALLEL &
     239              : !$OMP DEFAULT (NONE) &
     240              : !$OMP SHARED  (ap_iterator, basis_set_list, calculate_forces, use_virial, &
     241              : !$OMP          matrix_h, matrix_p, atomic_kind_set, qs_kind_set, particle_set, &
     242              : !$OMP          sab_orb, sac_ppl, nthread, ncoset, nkind, cell_to_index, &
     243              : !$OMP          ldsab,  maxnset, maxder, do_dR, deltaR, doat, libgrpp_local, &
     244              : !$OMP          maxlgto, nder, maxco, dokp, locks, natom) &
     245              : !$OMP PRIVATE (ikind, jkind, iatom, jatom, rab, basis_set_a, basis_set_b, &
     246              : !$OMP          first_sgfa, la_max, la_min, npgfa, nsgfa, sphi_a, &
     247              : !$OMP          zeta, first_sgfb, lb_max, lb_min, npgfb, nsetb, rpgfb, set_radius_b, sphi_b, &
     248              : !$OMP          zetb, dab, irow, icol, h_block, found, iset, ncoa, lock_num, &
     249              : !$OMP          sgfa, jset, ncob, sgfb, nsgfb, p_block, work, pab, hab, hab2, hab2_w, &
     250              : !$OMP          atk0, atk1, h1_1block, h1_2block, h1_3block, kkind, nseta, &
     251              : !$OMP          gth_potential, sgp_potential, alpha, cexp_ppl, lpotextended, &
     252              : !$OMP          ppl_radius, nexp_lpot, nexp_ppl, alpha_ppl, alpha_lpot, nct_ppl, &
     253              : !$OMP          nct_lpot, cval_ppl, cval_lpot, rac, dac, rbc, dbc, &
     254              : !$OMP          set_radius_a,  rpgfa, force_a, force_b, ppl_fwork, mepos, &
     255              : !$OMP          slot, f0, katom, ppl_work, cellind, img, ecp_local, ecp_semi_local, &
     256              : !$OMP          nloc, nrloc, aloc, bloc, n_local, a_local, c_local, &
     257              : !$OMP          slmax, npot, nrpot, apot, bpot, only_gaussians, &
     258              : !$OMP          ldai, hash, hash1, hash2, iatom8) &
     259        17897 : !$OMP REDUCTION (+ : pv_thread, force_thread, at_thread )
     260              : 
     261              : !$OMP SINGLE
     262              : !$    ALLOCATE (locks(nlock))
     263              : !$OMP END SINGLE
     264              : 
     265              : !$OMP DO
     266              : !$    DO lock_num = 1, nlock
     267              : !$       call omp_init_lock(locks(lock_num))
     268              : !$    END DO
     269              : !$OMP END DO
     270              : 
     271              :       mepos = 0
     272              : !$    mepos = omp_get_thread_num()
     273              : 
     274              :       ALLOCATE (hab(ldsab, ldsab, maxnset, maxnset), work(ldsab, ldsab*maxder))
     275              :       ldai = ncoset(2*maxlgto + 2*nder)
     276              :       ALLOCATE (ppl_work(ldai, ldai, MAX(maxder, 2*maxlgto + 2*nder + 1)))
     277              :       IF (calculate_forces .OR. doat) THEN
     278              :          ALLOCATE (pab(maxco, maxco, maxnset, maxnset))
     279              :          ldai = ncoset(maxlgto)
     280              :          ALLOCATE (ppl_fwork(ldai, ldai, maxder))
     281              :       END IF
     282              : 
     283              : !$OMP DO SCHEDULE(GUIDED)
     284              :       DO slot = 1, sab_orb(1)%nl_size
     285              :          !SL
     286              :          IF (do_dR) THEN
     287              :             ALLOCATE (hab2(ldsab, ldsab, 4, maxnset, maxnset))
     288              :             ALLOCATE (hab2_w(ldsab, ldsab, 6))
     289              :             ALLOCATE (ppl_fwork(ldai, ldai, maxder))
     290              :          END IF
     291              : 
     292              :          ikind = sab_orb(1)%nlist_task(slot)%ikind
     293              :          jkind = sab_orb(1)%nlist_task(slot)%jkind
     294              :          iatom = sab_orb(1)%nlist_task(slot)%iatom
     295              :          jatom = sab_orb(1)%nlist_task(slot)%jatom
     296              :          cellind(:) = sab_orb(1)%nlist_task(slot)%cell(:)
     297              :          rab(1:3) = sab_orb(1)%nlist_task(slot)%r(1:3)
     298              : 
     299              :          basis_set_a => basis_set_list(ikind)%gto_basis_set
     300              :          IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
     301              :          basis_set_b => basis_set_list(jkind)%gto_basis_set
     302              :          IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
     303              : 
     304              : !$       iatom8 = INT(iatom - 1, int_8)*INT(natom, int_8) + INT(jatom, int_8)
     305              : !$       hash1 = INT(MOD(iatom8, INT(nlock, int_8)) + 1)
     306              : 
     307              :          ! basis ikind
     308              :          first_sgfa => basis_set_a%first_sgf
     309              :          la_max => basis_set_a%lmax
     310              :          la_min => basis_set_a%lmin
     311              :          npgfa => basis_set_a%npgf
     312              :          nseta = basis_set_a%nset
     313              :          nsgfa => basis_set_a%nsgf_set
     314              :          rpgfa => basis_set_a%pgf_radius
     315              :          set_radius_a => basis_set_a%set_radius
     316              :          sphi_a => basis_set_a%sphi
     317              :          zeta => basis_set_a%zet
     318              :          ! basis jkind
     319              :          first_sgfb => basis_set_b%first_sgf
     320              :          lb_max => basis_set_b%lmax
     321              :          lb_min => basis_set_b%lmin
     322              :          npgfb => basis_set_b%npgf
     323              :          nsetb = basis_set_b%nset
     324              :          nsgfb => basis_set_b%nsgf_set
     325              :          rpgfb => basis_set_b%pgf_radius
     326              :          set_radius_b => basis_set_b%set_radius
     327              :          sphi_b => basis_set_b%sphi
     328              :          zetb => basis_set_b%zet
     329              : 
     330              :          dab = SQRT(SUM(rab*rab))
     331              : 
     332              :          IF (dokp) THEN
     333              :             img = cell_to_index(cellind(1), cellind(2), cellind(3))
     334              :          ELSE
     335              :             img = 1
     336              :          END IF
     337              : 
     338              :          ! *** Use the symmetry of the first derivatives ***
     339              :          IF (iatom == jatom) THEN
     340              :             f0 = 1.0_dp
     341              :          ELSE
     342              :             f0 = 2.0_dp
     343              :          END IF
     344              : 
     345              :          ! *** Create matrix blocks for a new matrix block column ***
     346              :          IF (iatom <= jatom) THEN
     347              :             irow = iatom
     348              :             icol = jatom
     349              :          ELSE
     350              :             irow = jatom
     351              :             icol = iatom
     352              :          END IF
     353              :          NULLIFY (h_block)
     354              : 
     355              :          IF (do_dR) THEN
     356              :             NULLIFY (h1_1block, h1_2block, h1_3block)
     357              : 
     358              :             CALL dbcsr_get_block_p(matrix=matrix_h(1, img)%matrix, &
     359              :                                    row=irow, col=icol, BLOCK=h1_1block, found=found)
     360              :             CALL dbcsr_get_block_p(matrix=matrix_h(2, img)%matrix, &
     361              :                                    row=irow, col=icol, BLOCK=h1_2block, found=found)
     362              :             CALL dbcsr_get_block_p(matrix=matrix_h(3, img)%matrix, &
     363              :                                    row=irow, col=icol, BLOCK=h1_3block, found=found)
     364              :          END IF
     365              : 
     366              :          CALL dbcsr_get_block_p(matrix_h(1, img)%matrix, irow, icol, h_block, found)
     367              :          CPASSERT(found)
     368              :          IF (calculate_forces .OR. doat) THEN
     369              :             NULLIFY (p_block)
     370              :             CALL dbcsr_get_block_p(matrix_p(1, img)%matrix, irow, icol, p_block, found)
     371              :             IF (ASSOCIATED(p_block)) THEN
     372              :                DO iset = 1, nseta
     373              :                   ncoa = npgfa(iset)*ncoset(la_max(iset))
     374              :                   sgfa = first_sgfa(1, iset)
     375              :                   DO jset = 1, nsetb
     376              :                      ncob = npgfb(jset)*ncoset(lb_max(jset))
     377              :                      sgfb = first_sgfb(1, jset)
     378              : 
     379              :                      ! *** Decontract density matrix block ***
     380              :                      IF (iatom <= jatom) THEN
     381              :                         work(1:ncoa, 1:nsgfb(jset)) = MATMUL(sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1), &
     382              :                                                              p_block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1))
     383              :                      ELSE
     384              :                         work(1:ncoa, 1:nsgfb(jset)) = MATMUL(sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1), &
     385              :                                                        TRANSPOSE(p_block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1)))
     386              :                      END IF
     387              : 
     388              :                      pab(1:ncoa, 1:ncob, iset, jset) = MATMUL(work(1:ncoa, 1:nsgfb(jset)), &
     389              :                                                               TRANSPOSE(sphi_b(1:ncob, sgfb:sgfb + nsgfb(jset) - 1)))
     390              :                   END DO
     391              :                END DO
     392              :             END IF
     393              :          END IF
     394              : 
     395              :          hab = 0._dp
     396              :          IF (do_dr) hab2 = 0._dp
     397              : 
     398              :          ! loop over all kinds for pseudopotential atoms
     399              :          DO kkind = 1, nkind
     400              : 
     401              :             CALL get_qs_kind(qs_kind_set(kkind), gth_potential=gth_potential, &
     402              :                              sgp_potential=sgp_potential)
     403              :             ecp_semi_local = .FALSE.
     404              :             only_gaussians = .TRUE.
     405              :             IF (ASSOCIATED(gth_potential)) THEN
     406              :                CALL get_potential(potential=gth_potential, &
     407              :                                   alpha_ppl=alpha, cexp_ppl=cexp_ppl, &
     408              :                                   lpot_present=lpotextended, ppl_radius=ppl_radius)
     409              :                nexp_ppl = 1
     410              :                alpha_ppl(1) = alpha
     411              :                nct_ppl(1) = SIZE(cexp_ppl)
     412              :                cval_ppl(1:nct_ppl(1), 1) = cexp_ppl(1:nct_ppl(1))
     413              :                IF (lpotextended) THEN
     414              :                   CALL get_potential(potential=gth_potential, &
     415              :                                      nexp_lpot=nexp_lpot, alpha_lpot=alpha_lpot, nct_lpot=nct_lpot, &
     416              :                                      cval_lpot=cval_lpot)
     417              :                   CPASSERT(nexp_lpot < nexp_max)
     418              :                   nexp_ppl = nexp_lpot + 1
     419              :                   alpha_ppl(2:nexp_lpot + 1) = alpha_lpot(1:nexp_lpot)
     420              :                   nct_ppl(2:nexp_lpot + 1) = nct_lpot(1:nexp_lpot)
     421              :                   DO i = 1, nexp_lpot
     422              :                      cval_ppl(1:nct_lpot(i), i + 1) = cval_lpot(1:nct_lpot(i), i)
     423              :                   END DO
     424              :                END IF
     425              :             ELSE IF (ASSOCIATED(sgp_potential)) THEN
     426              :                CALL get_potential(potential=sgp_potential, ecp_local=ecp_local, ecp_semi_local=ecp_semi_local, &
     427              :                                   ppl_radius=ppl_radius)
     428              :                IF (ecp_local) THEN
     429              :                   CALL get_potential(potential=sgp_potential, nloc=nloc, nrloc=nrloc, aloc=aloc, bloc=bloc)
     430              :                   nexp_ppl = nloc
     431              :                   CPASSERT(nexp_ppl <= nexp_max)
     432              :                   nct_ppl(1:nloc) = nrloc(1:nloc)
     433              :                   alpha_ppl(1:nloc) = bloc(1:nloc)
     434              :                   cval_ppl(1, 1:nloc) = aloc(1:nloc)
     435              :                   only_gaussians = .FALSE.
     436              :                ELSE
     437              :                   CALL get_potential(potential=sgp_potential, n_local=n_local, a_local=a_local, c_local=c_local)
     438              :                   nexp_ppl = n_local
     439              :                   CPASSERT(nexp_ppl <= nexp_max)
     440              :                   nct_ppl(1:n_local) = 1
     441              :                   alpha_ppl(1:n_local) = a_local(1:n_local)
     442              :                   cval_ppl(1, 1:n_local) = c_local(1:n_local)
     443              :                END IF
     444              :                IF (ecp_semi_local) THEN
     445              :                   CALL get_potential(potential=sgp_potential, sl_lmax=slmax, &
     446              :                                      npot=npot, nrpot=nrpot, apot=apot, bpot=bpot)
     447              :                ELSEIF (ecp_local) THEN
     448              :                   IF (SUM(ABS(aloc(1:nloc))) < 1.0e-12_dp) CYCLE
     449              :                END IF
     450              :             ELSE
     451              :                CYCLE
     452              :             END IF
     453              : 
     454              :             CALL nl_set_sub_iterator(ap_iterator, ikind, kkind, iatom, mepos=mepos)
     455              : 
     456              :             DO WHILE (nl_sub_iterate(ap_iterator, mepos=mepos) == 0)
     457              : 
     458              :                CALL get_iterator_info(ap_iterator, mepos=mepos, jatom=katom, r=rac)
     459              : 
     460              :                dac = SQRT(SUM(rac*rac))
     461              :                rbc(:) = rac(:) - rab(:)
     462              :                dbc = SQRT(SUM(rbc*rbc))
     463              :                IF ((MAXVAL(set_radius_a(:)) + ppl_radius < dac) .OR. &
     464              :                    (MAXVAL(set_radius_b(:)) + ppl_radius < dbc)) THEN
     465              :                   CYCLE
     466              :                END IF
     467              : 
     468              :                DO iset = 1, nseta
     469              :                   IF (set_radius_a(iset) + ppl_radius < dac) CYCLE
     470              :                   ncoa = npgfa(iset)*ncoset(la_max(iset))
     471              :                   sgfa = first_sgfa(1, iset)
     472              :                   DO jset = 1, nsetb
     473              :                      IF (set_radius_b(jset) + ppl_radius < dbc) CYCLE
     474              :                      ncob = npgfb(jset)*ncoset(lb_max(jset))
     475              :                      sgfb = first_sgfb(1, jset)
     476              :                      IF (set_radius_a(iset) + set_radius_b(jset) < dab) CYCLE
     477              :                      ! *** Calculate the GTH pseudo potential forces ***
     478              :                      IF (doat) THEN
     479              :                         atk0 = f0*SUM(hab(1:ncoa, 1:ncob, iset, jset)* &
     480              :                                       pab(1:ncoa, 1:ncob, iset, jset))
     481              :                      END IF
     482              :                      IF (calculate_forces) THEN
     483              : 
     484              :                         force_a(:) = 0.0_dp
     485              :                         force_b(:) = 0.0_dp
     486              : 
     487              :                         IF (only_gaussians) THEN
     488              :                            CALL ppl_integral( &
     489              :                               la_max(iset), la_min(iset), npgfa(iset), &
     490              :                               rpgfa(:, iset), zeta(:, iset), &
     491              :                               lb_max(jset), lb_min(jset), npgfb(jset), &
     492              :                               rpgfb(:, jset), zetb(:, jset), &
     493              :                               nexp_ppl, alpha_ppl, nct_ppl, cval_ppl, ppl_radius, &
     494              :                               rab, dab, rac, dac, rbc, dbc, &
     495              :                               hab(:, :, iset, jset), ppl_work, pab(:, :, iset, jset), &
     496              :                               force_a, force_b, ppl_fwork)
     497              :                         ELSEIF (libgrpp_local) THEN
     498              : !$OMP CRITICAL(type1)
     499              :                            CALL libgrpp_local_forces_ref(la_max(iset), la_min(iset), npgfa(iset), &
     500              :                                                          rpgfa(:, iset), zeta(:, iset), &
     501              :                                                          lb_max(jset), lb_min(jset), npgfb(jset), &
     502              :                                                          rpgfb(:, jset), zetb(:, jset), &
     503              :                                                          nexp_ppl, alpha_ppl, cval_ppl(1, :), nct_ppl, &
     504              :                                                          ppl_radius, rab, dab, rac, dac, dbc, &
     505              :                                                          hab(:, :, iset, jset), pab(:, :, iset, jset), &
     506              :                                                          force_a, force_b)
     507              : !$OMP END CRITICAL(type1)
     508              :                         ELSE
     509              :                            CALL ecploc_integral( &
     510              :                               la_max(iset), la_min(iset), npgfa(iset), &
     511              :                               rpgfa(:, iset), zeta(:, iset), &
     512              :                               lb_max(jset), lb_min(jset), npgfb(jset), &
     513              :                               rpgfb(:, jset), zetb(:, jset), &
     514              :                               nexp_ppl, alpha_ppl, nct_ppl, cval_ppl, ppl_radius, &
     515              :                               rab, dab, rac, dac, rbc, dbc, &
     516              :                               hab(:, :, iset, jset), ppl_work, pab(:, :, iset, jset), &
     517              :                               force_a, force_b, ppl_fwork)
     518              :                         END IF
     519              : 
     520              :                         IF (ecp_semi_local) THEN
     521              : 
     522              : !$OMP CRITICAL(type2)
     523              :                            CALL libgrpp_semilocal_forces_ref(la_max(iset), la_min(iset), npgfa(iset), &
     524              :                                                              rpgfa(:, iset), zeta(:, iset), &
     525              :                                                              lb_max(jset), lb_min(jset), npgfb(jset), &
     526              :                                                              rpgfb(:, jset), zetb(:, jset), &
     527              :                                                              slmax, npot, bpot, apot, nrpot, &
     528              :                                                              ppl_radius, rab, dab, rac, dac, dbc, &
     529              :                                                              hab(:, :, iset, jset), pab(:, :, iset, jset), &
     530              :                                                              force_a, force_b)
     531              : !$OMP END CRITICAL(type2)
     532              :                         END IF
     533              :                         ! *** The derivatives w.r.t. atomic center c are    ***
     534              :                         ! *** calculated using the translational invariance ***
     535              :                         ! *** of the first derivatives                      ***
     536              : 
     537              :                         force_thread(1, iatom) = force_thread(1, iatom) + f0*force_a(1)
     538              :                         force_thread(2, iatom) = force_thread(2, iatom) + f0*force_a(2)
     539              :                         force_thread(3, iatom) = force_thread(3, iatom) + f0*force_a(3)
     540              :                         force_thread(1, katom) = force_thread(1, katom) - f0*force_a(1)
     541              :                         force_thread(2, katom) = force_thread(2, katom) - f0*force_a(2)
     542              :                         force_thread(3, katom) = force_thread(3, katom) - f0*force_a(3)
     543              : 
     544              :                         force_thread(1, jatom) = force_thread(1, jatom) + f0*force_b(1)
     545              :                         force_thread(2, jatom) = force_thread(2, jatom) + f0*force_b(2)
     546              :                         force_thread(3, jatom) = force_thread(3, jatom) + f0*force_b(3)
     547              :                         force_thread(1, katom) = force_thread(1, katom) - f0*force_b(1)
     548              :                         force_thread(2, katom) = force_thread(2, katom) - f0*force_b(2)
     549              :                         force_thread(3, katom) = force_thread(3, katom) - f0*force_b(3)
     550              : 
     551              :                         IF (use_virial) THEN
     552              :                            CALL virial_pair_force(pv_thread, f0, force_a, rac)
     553              :                            CALL virial_pair_force(pv_thread, f0, force_b, rbc)
     554              :                         END IF
     555              :                      ELSEIF (do_dR) THEN
     556              :                         hab2_w = 0._dp
     557              :                         CALL ppl_integral( &
     558              :                            la_max(iset), la_min(iset), npgfa(iset), &
     559              :                            rpgfa(:, iset), zeta(:, iset), &
     560              :                            lb_max(jset), lb_min(jset), npgfb(jset), &
     561              :                            rpgfb(:, jset), zetb(:, jset), &
     562              :                            nexp_ppl, alpha_ppl, nct_ppl, cval_ppl, ppl_radius, &
     563              :                            rab, dab, rac, dac, rbc, dbc, &
     564              :                            vab=hab(:, :, iset, jset), s=ppl_work, &
     565              :                            hab2=hab2(:, :, :, iset, jset), hab2_work=hab2_w, fs=ppl_fwork, &
     566              :                            deltaR=deltaR, iatom=iatom, jatom=jatom, katom=katom)
     567              :                         IF (ecp_semi_local) THEN
     568              :                            ! semi local ECP part
     569              :                            CPABORT("Option not implemented")
     570              :                         END IF
     571              :                      ELSE
     572              :                         IF (only_gaussians) THEN
     573              :                            !If the local part of the pseudo-potential only has Gaussian functions
     574              :                            !we can use CP2K native code, that can run without libgrpp installation
     575              :                            CALL ppl_integral( &
     576              :                               la_max(iset), la_min(iset), npgfa(iset), &
     577              :                               rpgfa(:, iset), zeta(:, iset), &
     578              :                               lb_max(jset), lb_min(jset), npgfb(jset), &
     579              :                               rpgfb(:, jset), zetb(:, jset), &
     580              :                               nexp_ppl, alpha_ppl, nct_ppl, cval_ppl, ppl_radius, &
     581              :                               rab, dab, rac, dac, rbc, dbc, hab(:, :, iset, jset), ppl_work)
     582              : 
     583              :                         ELSEIF (libgrpp_local) THEN
     584              :                            !If the local part of the potential is more complex, we need libgrpp
     585              : !$OMP CRITICAL(type1)
     586              :                            CALL libgrpp_local_integrals(la_max(iset), la_min(iset), npgfa(iset), &
     587              :                                                         rpgfa(:, iset), zeta(:, iset), &
     588              :                                                         lb_max(jset), lb_min(jset), npgfb(jset), &
     589              :                                                         rpgfb(:, jset), zetb(:, jset), &
     590              :                                                         nexp_ppl, alpha_ppl, cval_ppl(1, :), nct_ppl, &
     591              :                                                         ppl_radius, rab, dab, rac, dac, dbc, &
     592              :                                                         hab(:, :, iset, jset))
     593              : !$OMP END CRITICAL(type1)
     594              :                         ELSE
     595              :                            CALL ecploc_integral( &
     596              :                               la_max(iset), la_min(iset), npgfa(iset), &
     597              :                               rpgfa(:, iset), zeta(:, iset), &
     598              :                               lb_max(jset), lb_min(jset), npgfb(jset), &
     599              :                               rpgfb(:, jset), zetb(:, jset), &
     600              :                               nexp_ppl, alpha_ppl, nct_ppl, cval_ppl, ppl_radius, &
     601              :                               rab, dab, rac, dac, rbc, dbc, hab(:, :, iset, jset), ppl_work)
     602              :                         END IF
     603              : 
     604              :                         IF (ecp_semi_local) THEN
     605              :                            ! semi local ECP part
     606              : !$OMP CRITICAL(type2)
     607              :                            CALL libgrpp_semilocal_integrals(la_max(iset), la_min(iset), npgfa(iset), &
     608              :                                                             rpgfa(:, iset), zeta(:, iset), &
     609              :                                                             lb_max(jset), lb_min(jset), npgfb(jset), &
     610              :                                                             rpgfb(:, jset), zetb(:, jset), &
     611              :                                                             slmax, npot, bpot, apot, nrpot, &
     612              :                                                             ppl_radius, rab, dab, rac, dac, dbc, &
     613              :                                                             hab(:, :, iset, jset))
     614              : !$OMP END CRITICAL(type2)
     615              :                         END IF
     616              :                      END IF
     617              :                      ! calculate atomic contributions
     618              :                      IF (doat) THEN
     619              :                         atk1 = f0*SUM(hab(1:ncoa, 1:ncob, iset, jset)* &
     620              :                                       pab(1:ncoa, 1:ncob, iset, jset))
     621              :                         at_thread(katom) = at_thread(katom) + (atk1 - atk0)
     622              :                      END IF
     623              :                   END DO
     624              :                END DO
     625              :             END DO
     626              :          END DO
     627              : 
     628              :          ! *** Contract PPL integrals
     629              :          IF (.NOT. do_dR) THEN
     630              :          DO iset = 1, nseta
     631              :             ncoa = npgfa(iset)*ncoset(la_max(iset))
     632              :             sgfa = first_sgfa(1, iset)
     633              :             DO jset = 1, nsetb
     634              :                ncob = npgfb(jset)*ncoset(lb_max(jset))
     635              :                sgfb = first_sgfb(1, jset)
     636              : 
     637              : !$             hash2 = MOD((iset - 1)*nsetb + jset, nlock) + 1
     638              : !$             hash = MOD(hash1 + hash2, nlock) + 1
     639              : 
     640              :                work(1:ncoa, 1:nsgfb(jset)) = MATMUL(hab(1:ncoa, 1:ncob, iset, jset), &
     641              :                                                     sphi_b(1:ncob, sgfb:sgfb + nsgfb(jset) - 1))
     642              : !$             CALL omp_set_lock(locks(hash))
     643              :                IF (iatom <= jatom) THEN
     644              :                   h_block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1) = &
     645              :                      h_block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1) + &
     646              :                      MATMUL(TRANSPOSE(sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1)), work(1:ncoa, 1:nsgfb(jset)))
     647              :                ELSE
     648              :                   h_block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1) = &
     649              :                      h_block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1) + &
     650              :                      MATMUL(TRANSPOSE(work(1:ncoa, 1:nsgfb(jset))), sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1))
     651              :                END IF
     652              : !$             CALL omp_unset_lock(locks(hash))
     653              : 
     654              :             END DO
     655              :          END DO
     656              :          ELSE  ! do_dr == .true.
     657              :          DO iset = 1, nseta
     658              :             ncoa = npgfa(iset)*ncoset(la_max(iset))
     659              :             sgfa = first_sgfa(1, iset)
     660              :             DO jset = 1, nsetb
     661              :                ncob = npgfb(jset)*ncoset(lb_max(jset))
     662              :                sgfb = first_sgfb(1, jset)
     663              :                work(1:ncoa, 1:nsgfb(jset)) = MATMUL(hab2(1:ncoa, 1:ncob, 1, iset, jset), &
     664              :                                                     sphi_b(1:ncob, sgfb:sgfb + nsgfb(jset) - 1))
     665              : 
     666              : !$OMP CRITICAL(h1_1block_critical)
     667              :                IF (iatom <= jatom) THEN
     668              :                   h1_1block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1) = &
     669              :                      h1_1block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1) + &
     670              :                      MATMUL(TRANSPOSE(sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1)), work(1:ncoa, 1:nsgfb(jset)))
     671              : 
     672              :                ELSE
     673              :                   h1_1block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1) = &
     674              :                      h1_1block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1) + &
     675              :                      MATMUL(TRANSPOSE(work(1:ncoa, 1:nsgfb(jset))), sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1))
     676              :                END IF
     677              : !$OMP END CRITICAL(h1_1block_critical)
     678              :                work(1:ncoa, 1:nsgfb(jset)) = MATMUL(hab2(1:ncoa, 1:ncob, 2, iset, jset), &
     679              :                                                     sphi_b(1:ncob, sgfb:sgfb + nsgfb(jset) - 1))
     680              : 
     681              : !$OMP CRITICAL(h1_2block_critical)
     682              :                IF (iatom <= jatom) THEN
     683              :                   h1_2block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1) = &
     684              :                      h1_2block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1) + &
     685              :                      MATMUL(TRANSPOSE(sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1)), work(1:ncoa, 1:nsgfb(jset)))
     686              : 
     687              :                ELSE
     688              :                   h1_2block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1) = &
     689              :                      h1_2block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1) + &
     690              :                      MATMUL(TRANSPOSE(work(1:ncoa, 1:nsgfb(jset))), sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1))
     691              :                END IF
     692              : !$OMP END CRITICAL(h1_2block_critical)
     693              :                work(1:ncoa, 1:nsgfb(jset)) = MATMUL(hab2(1:ncoa, 1:ncob, 3, iset, jset), &
     694              :                                                     sphi_b(1:ncob, sgfb:sgfb + nsgfb(jset) - 1))
     695              : !$OMP CRITICAL(h1_3block_critical)
     696              :                IF (iatom <= jatom) THEN
     697              :                   h1_3block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1) = &
     698              :                      h1_3block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1) + &
     699              :                      MATMUL(TRANSPOSE(sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1)), work(1:ncoa, 1:nsgfb(jset)))
     700              : 
     701              :                ELSE
     702              :                   h1_3block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1) = &
     703              :                      h1_3block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1) + &
     704              :                      MATMUL(TRANSPOSE(work(1:ncoa, 1:nsgfb(jset))), sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1))
     705              :                END IF
     706              : !$OMP END CRITICAL(h1_3block_critical)
     707              :             END DO
     708              :          END DO
     709              :          END IF
     710              :          IF (do_dR) DEALLOCATE (hab2, ppl_fwork, hab2_w)
     711              :       END DO ! slot
     712              : 
     713              :       DEALLOCATE (hab, work, ppl_work)
     714              :       IF (calculate_forces .OR. doat) THEN
     715              :          DEALLOCATE (pab, ppl_fwork)
     716              :       END IF
     717              : 
     718              : !$OMP DO
     719              : !$    DO lock_num = 1, nlock
     720              : !$       call omp_destroy_lock(locks(lock_num))
     721              : !$    END DO
     722              : !$OMP END DO
     723              : 
     724              : !$OMP SINGLE
     725              : !$    DEALLOCATE (locks)
     726              : !$OMP END SINGLE NOWAIT
     727              : 
     728              : !$OMP END PARALLEL
     729              : 
     730        17897 :       CALL neighbor_list_iterator_release(ap_iterator)
     731              : 
     732        17897 :       DEALLOCATE (basis_set_list)
     733              : 
     734        17897 :       IF (calculate_forces .OR. doat) THEN
     735              :          ! *** If LSD, then recover alpha density and beta density     ***
     736              :          ! *** from the total density (1) and the spin density (2)     ***
     737         7475 :          IF (SIZE(matrix_p, 1) == 2) THEN
     738         2502 :             DO img = 1, nimages
     739              :                CALL dbcsr_add(matrix_p(1, img)%matrix, matrix_p(2, img)%matrix, &
     740         1552 :                               alpha_scalar=0.5_dp, beta_scalar=0.5_dp)
     741              :                CALL dbcsr_add(matrix_p(2, img)%matrix, matrix_p(1, img)%matrix, &
     742         2502 :                               alpha_scalar=-1.0_dp, beta_scalar=1.0_dp)
     743              :             END DO
     744              :          END IF
     745              :       END IF
     746              : 
     747        17897 :       IF (calculate_forces) THEN
     748         7413 :          CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
     749              : !$OMP DO
     750              :          DO iatom = 1, natom
     751        26161 :             atom_a = atom_of_kind(iatom)
     752        26161 :             ikind = kind_of(iatom)
     753       104644 :             force(ikind)%gth_ppl(:, atom_a) = force(ikind)%gth_ppl(:, atom_a) + force_thread(:, iatom)
     754              :          END DO
     755              : !$OMP END DO
     756         7413 :          DEALLOCATE (atom_of_kind, kind_of)
     757              :       END IF
     758        17897 :       IF (doat) THEN
     759          280 :          atcore(1:natom) = atcore(1:natom) + at_thread(1:natom)
     760              :       END IF
     761              : 
     762        17897 :       IF (calculate_forces .AND. use_virial) THEN
     763        10920 :          virial%pv_ppl = virial%pv_ppl + pv_thread
     764        10920 :          virial%pv_virial = virial%pv_virial + pv_thread
     765              :       END IF
     766              : 
     767        17897 :       CALL timestop(handle)
     768              : 
     769        53691 :    END SUBROUTINE build_core_ppl
     770              : 
     771              : ! **************************************************************************************************
     772              : !> \brief ...
     773              : !> \param lri_ppl_coef ...
     774              : !> \param force ...
     775              : !> \param virial ...
     776              : !> \param calculate_forces ...
     777              : !> \param use_virial ...
     778              : !> \param qs_kind_set ...
     779              : !> \param atomic_kind_set ...
     780              : !> \param particle_set ...
     781              : !> \param sac_ppl ...
     782              : !> \param basis_type ...
     783              : ! **************************************************************************************************
     784            4 :    SUBROUTINE build_core_ppl_ri(lri_ppl_coef, force, virial, calculate_forces, use_virial, &
     785              :                                 qs_kind_set, atomic_kind_set, particle_set, sac_ppl, &
     786              :                                 basis_type)
     787              : 
     788              :       TYPE(lri_kind_type), DIMENSION(:), POINTER         :: lri_ppl_coef
     789              :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     790              :       TYPE(virial_type), POINTER                         :: virial
     791              :       LOGICAL, INTENT(IN)                                :: calculate_forces
     792              :       LOGICAL                                            :: use_virial
     793              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     794              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     795              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     796              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     797              :          POINTER                                         :: sac_ppl
     798              :       CHARACTER(LEN=*), INTENT(IN)                       :: basis_type
     799              : 
     800              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'build_core_ppl_ri'
     801              :       INTEGER, PARAMETER                                 :: nexp_max = 30
     802              : 
     803              :       INTEGER :: atom_a, handle, i, iatom, ikind, iset, katom, kkind, maxco, maxsgf, n_local, &
     804              :          natom, ncoa, nexp_lpot, nexp_ppl, nfun, nkind, nloc, nseta, sgfa, sgfb, slot
     805            4 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind, kind_of
     806              :       INTEGER, DIMENSION(1:10)                           :: nrloc
     807            4 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, nct_lpot, npgfa, nsgfa
     808            4 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa
     809              :       INTEGER, DIMENSION(nexp_max)                       :: nct_ppl
     810              :       LOGICAL                                            :: ecp_local, ecp_semi_local, lpotextended
     811              :       REAL(KIND=dp)                                      :: alpha, dac, ppl_radius
     812            4 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: va, work
     813            4 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: dva, dvas
     814              :       REAL(KIND=dp), DIMENSION(1:10)                     :: aloc, bloc
     815              :       REAL(KIND=dp), DIMENSION(3)                        :: force_a, rac
     816              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: pv_thread
     817              :       TYPE(gto_basis_set_type), POINTER                  :: basis_set
     818            4 :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_list
     819              :       TYPE(gth_potential_type), POINTER                  :: gth_potential
     820              :       REAL(KIND=dp), DIMENSION(nexp_max)                 :: alpha_ppl
     821            4 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: bcon, cval_lpot, rpgfa, sphi_a, zeta
     822            4 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: a_local, alpha_lpot, c_local, cexp_ppl, &
     823            4 :                                                             set_radius_a
     824              :       REAL(KIND=dp), DIMENSION(4, nexp_max)              :: cval_ppl
     825            8 :       REAL(KIND=dp), DIMENSION(3, SIZE(particle_set))    :: force_thread
     826              :       TYPE(sgp_potential_type), POINTER                  :: sgp_potential
     827              : 
     828              : !$    INTEGER(kind=omp_lock_kind), &
     829            4 : !$       ALLOCATABLE, DIMENSION(:) :: locks
     830              : !$    INTEGER                                            :: lock_num, hash
     831              : !$    INTEGER, PARAMETER                                 :: nlock = 501
     832              : 
     833            8 :       IF (calculate_forces) THEN
     834            2 :          CALL timeset(routineN//"_forces", handle)
     835              :       ELSE
     836            2 :          CALL timeset(routineN, handle)
     837              :       END IF
     838              : 
     839            4 :       nkind = SIZE(atomic_kind_set)
     840            4 :       natom = SIZE(particle_set)
     841              : 
     842           52 :       force_thread = 0.0_dp
     843            4 :       pv_thread = 0.0_dp
     844            4 :       CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
     845              : 
     846           20 :       ALLOCATE (basis_set_list(nkind))
     847           12 :       DO ikind = 1, nkind
     848            8 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=basis_set, basis_type=basis_type)
     849           12 :          IF (ASSOCIATED(basis_set)) THEN
     850            8 :             basis_set_list(ikind)%gto_basis_set => basis_set
     851              :          ELSE
     852            0 :             NULLIFY (basis_set_list(ikind)%gto_basis_set)
     853              :          END IF
     854              :       END DO
     855              : 
     856            4 :       CALL get_qs_kind_set(qs_kind_set, maxco=maxco, maxsgf=maxsgf, basis_type=basis_type)
     857              : 
     858              : !$OMP PARALLEL &
     859              : !$OMP DEFAULT (NONE) &
     860              : !$OMP SHARED  (maxco,maxsgf,basis_set_list,calculate_forces,lri_ppl_coef,qs_kind_set,&
     861              : !$OMP          locks,natom,use_virial,virial,ncoset,atom_of_kind,sac_ppl) &
     862              : !$OMP PRIVATE (ikind,kkind,iatom,katom,atom_a,rac,va,dva,dvas,basis_set,slot,&
     863              : !$OMP          first_sgfa,la_max,la_min,npgfa,nseta,nsgfa,rpgfa,set_radius_a,lock_num,&
     864              : !$OMP          sphi_a,zeta,gth_potential,sgp_potential,alpha,cexp_ppl,lpotextended,ppl_radius,&
     865              : !$OMP          nexp_ppl,alpha_ppl,nct_ppl,cval_ppl,nloc,n_local,nrloc,a_local,aloc,bloc,c_local,nfun,work,&
     866              : !$OMP          hash,dac,force_a,iset,sgfa,sgfb,ncoa,bcon,cval_lpot,nct_lpot,alpha_lpot,nexp_lpot,&
     867              : !$OMP          ecp_local,ecp_semi_local) &
     868            4 : !$OMP REDUCTION (+ : pv_thread, force_thread )
     869              : 
     870              : !$OMP SINGLE
     871              : !$    ALLOCATE (locks(nlock))
     872              : !$OMP END SINGLE
     873              : 
     874              : !$OMP DO
     875              : !$    DO lock_num = 1, nlock
     876              : !$       call omp_init_lock(locks(lock_num))
     877              : !$    END DO
     878              : !$OMP END DO
     879              : 
     880              :       ALLOCATE (va(maxco), work(maxsgf))
     881              :       IF (calculate_forces) THEN
     882              :          ALLOCATE (dva(maxco, 3), dvas(maxco, 3))
     883              :       END IF
     884              : 
     885              : !$OMP DO SCHEDULE(GUIDED)
     886              :       DO slot = 1, sac_ppl(1)%nl_size
     887              : 
     888              :          ikind = sac_ppl(1)%nlist_task(slot)%ikind
     889              :          kkind = sac_ppl(1)%nlist_task(slot)%jkind
     890              :          iatom = sac_ppl(1)%nlist_task(slot)%iatom
     891              :          katom = sac_ppl(1)%nlist_task(slot)%jatom
     892              :          rac(1:3) = sac_ppl(1)%nlist_task(slot)%r(1:3)
     893              :          atom_a = atom_of_kind(iatom)
     894              : 
     895              :          basis_set => basis_set_list(ikind)%gto_basis_set
     896              :          IF (.NOT. ASSOCIATED(basis_set)) CYCLE
     897              : 
     898              :          ! basis ikind
     899              :          first_sgfa => basis_set%first_sgf
     900              :          la_max => basis_set%lmax
     901              :          la_min => basis_set%lmin
     902              :          npgfa => basis_set%npgf
     903              :          nseta = basis_set%nset
     904              :          nsgfa => basis_set%nsgf_set
     905              :          nfun = basis_set%nsgf
     906              :          rpgfa => basis_set%pgf_radius
     907              :          set_radius_a => basis_set%set_radius
     908              :          sphi_a => basis_set%sphi
     909              :          zeta => basis_set%zet
     910              : 
     911              :          CALL get_qs_kind(qs_kind_set(kkind), gth_potential=gth_potential, &
     912              :                           sgp_potential=sgp_potential)
     913              :          ecp_semi_local = .FALSE.
     914              :          IF (ASSOCIATED(gth_potential)) THEN
     915              :             CALL get_potential(potential=gth_potential, &
     916              :                                alpha_ppl=alpha, cexp_ppl=cexp_ppl, &
     917              :                                lpot_present=lpotextended, ppl_radius=ppl_radius)
     918              :             nexp_ppl = 1
     919              :             alpha_ppl(1) = alpha
     920              :             nct_ppl(1) = SIZE(cexp_ppl)
     921              :             cval_ppl(1:nct_ppl(1), 1) = cexp_ppl(1:nct_ppl(1))
     922              :             IF (lpotextended) THEN
     923              :                CALL get_potential(potential=gth_potential, &
     924              :                                   nexp_lpot=nexp_lpot, alpha_lpot=alpha_lpot, nct_lpot=nct_lpot, cval_lpot=cval_lpot)
     925              :                CPASSERT(nexp_lpot < nexp_max)
     926              :                nexp_ppl = nexp_lpot + 1
     927              :                alpha_ppl(2:nexp_lpot + 1) = alpha_lpot(1:nexp_lpot)
     928              :                nct_ppl(2:nexp_lpot + 1) = nct_lpot(1:nexp_lpot)
     929              :                DO i = 1, nexp_lpot
     930              :                   cval_ppl(1:nct_lpot(i), i + 1) = cval_lpot(1:nct_lpot(i), i)
     931              :                END DO
     932              :             END IF
     933              :          ELSE IF (ASSOCIATED(sgp_potential)) THEN
     934              :             CALL get_potential(potential=sgp_potential, ecp_local=ecp_local, ecp_semi_local=ecp_semi_local, &
     935              :                                ppl_radius=ppl_radius)
     936              :             CPASSERT(.NOT. ecp_semi_local)
     937              :             IF (ecp_local) THEN
     938              :                CALL get_potential(potential=sgp_potential, nloc=nloc, nrloc=nrloc, aloc=aloc, bloc=bloc)
     939              :                IF (SUM(ABS(aloc(1:nloc))) < 1.0e-12_dp) CYCLE
     940              :                nexp_ppl = nloc
     941              :                CPASSERT(nexp_ppl <= nexp_max)
     942              :                nct_ppl(1:nloc) = nrloc(1:nloc)
     943              :                alpha_ppl(1:nloc) = bloc(1:nloc)
     944              :                cval_ppl(1, 1:nloc) = aloc(1:nloc)
     945              :             ELSE
     946              :                CALL get_potential(potential=sgp_potential, n_local=n_local, a_local=a_local, c_local=c_local)
     947              :                nexp_ppl = n_local
     948              :                CPASSERT(nexp_ppl <= nexp_max)
     949              :                nct_ppl(1:n_local) = 1
     950              :                alpha_ppl(1:n_local) = a_local(1:n_local)
     951              :                cval_ppl(1, 1:n_local) = c_local(1:n_local)
     952              :             END IF
     953              :          ELSE
     954              :             CYCLE
     955              :          END IF
     956              : 
     957              :          dac = SQRT(SUM(rac*rac))
     958              :          IF ((MAXVAL(set_radius_a(:)) + ppl_radius < dac)) CYCLE
     959              :          IF (calculate_forces) force_a = 0.0_dp
     960              :          work(1:nfun) = 0.0_dp
     961              : 
     962              :          DO iset = 1, nseta
     963              :             IF (set_radius_a(iset) + ppl_radius < dac) CYCLE
     964              :             ! integrals
     965              :             IF (calculate_forces) THEN
     966              :                va = 0.0_dp
     967              :                dva = 0.0_dp
     968              :                CALL ppl_integral_ri( &
     969              :                   la_max(iset), la_min(iset), npgfa(iset), rpgfa(:, iset), zeta(:, iset), &
     970              :                   nexp_ppl, alpha_ppl, nct_ppl, cval_ppl, ppl_radius, &
     971              :                   -rac, dac, va, dva)
     972              :             ELSE
     973              :                va = 0.0_dp
     974              :                CALL ppl_integral_ri( &
     975              :                   la_max(iset), la_min(iset), npgfa(iset), rpgfa(:, iset), zeta(:, iset), &
     976              :                   nexp_ppl, alpha_ppl, nct_ppl, cval_ppl, ppl_radius, &
     977              :                   -rac, dac, va)
     978              :             END IF
     979              :             ! contraction
     980              :             sgfa = first_sgfa(1, iset)
     981              :             sgfb = sgfa + nsgfa(iset) - 1
     982              :             ncoa = npgfa(iset)*ncoset(la_max(iset))
     983              :             bcon => sphi_a(1:ncoa, sgfa:sgfb)
     984              :             work(sgfa:sgfb) = MATMUL(TRANSPOSE(bcon), va(1:ncoa))
     985              :             IF (calculate_forces) THEN
     986              :                dvas(1:nsgfa(iset), 1:3) = MATMUL(TRANSPOSE(bcon), dva(1:ncoa, 1:3))
     987              :                force_a(1) = force_a(1) + SUM(lri_ppl_coef(ikind)%acoef(atom_a, sgfa:sgfb)*dvas(1:nsgfa(iset), 1))
     988              :                force_a(2) = force_a(2) + SUM(lri_ppl_coef(ikind)%acoef(atom_a, sgfa:sgfb)*dvas(1:nsgfa(iset), 2))
     989              :                force_a(3) = force_a(3) + SUM(lri_ppl_coef(ikind)%acoef(atom_a, sgfa:sgfb)*dvas(1:nsgfa(iset), 3))
     990              :             END IF
     991              :          END DO
     992              : !$       hash = MOD(iatom, nlock) + 1
     993              : !$       CALL omp_set_lock(locks(hash))
     994              :          lri_ppl_coef(ikind)%v_int(atom_a, 1:nfun) = lri_ppl_coef(ikind)%v_int(atom_a, 1:nfun) + work(1:nfun)
     995              : !$       CALL omp_unset_lock(locks(hash))
     996              :          IF (calculate_forces) THEN
     997              :             force_thread(1, iatom) = force_thread(1, iatom) + force_a(1)
     998              :             force_thread(2, iatom) = force_thread(2, iatom) + force_a(2)
     999              :             force_thread(3, iatom) = force_thread(3, iatom) + force_a(3)
    1000              :             force_thread(1, katom) = force_thread(1, katom) - force_a(1)
    1001              :             force_thread(2, katom) = force_thread(2, katom) - force_a(2)
    1002              :             force_thread(3, katom) = force_thread(3, katom) - force_a(3)
    1003              :             IF (use_virial) THEN
    1004              :                CALL virial_pair_force(pv_thread, 1.0_dp, force_a, rac)
    1005              :             END IF
    1006              :          END IF
    1007              :       END DO
    1008              : 
    1009              :       DEALLOCATE (va, work)
    1010              :       IF (calculate_forces) THEN
    1011              :          DEALLOCATE (dva, dvas)
    1012              :       END IF
    1013              : 
    1014              : !$OMP END PARALLEL
    1015              : 
    1016            4 :       IF (calculate_forces) THEN
    1017            8 :          DO iatom = 1, natom
    1018            6 :             atom_a = atom_of_kind(iatom)
    1019            6 :             ikind = kind_of(iatom)
    1020            6 :             force(ikind)%gth_ppl(1, atom_a) = force(ikind)%gth_ppl(1, atom_a) + force_thread(1, iatom)
    1021            6 :             force(ikind)%gth_ppl(2, atom_a) = force(ikind)%gth_ppl(2, atom_a) + force_thread(2, iatom)
    1022            8 :             force(ikind)%gth_ppl(3, atom_a) = force(ikind)%gth_ppl(3, atom_a) + force_thread(3, iatom)
    1023              :          END DO
    1024              :       END IF
    1025            4 :       DEALLOCATE (atom_of_kind, kind_of)
    1026              : 
    1027            4 :       IF (calculate_forces .AND. use_virial) THEN
    1028            0 :          virial%pv_ppl = virial%pv_ppl + pv_thread
    1029            0 :          virial%pv_virial = virial%pv_virial + pv_thread
    1030              :       END IF
    1031              : 
    1032            4 :       DEALLOCATE (basis_set_list)
    1033              : 
    1034            4 :       CALL timestop(handle)
    1035              : 
    1036           12 :    END SUBROUTINE build_core_ppl_ri
    1037              : 
    1038              : ! **************************************************************************************************
    1039              : 
    1040              : END MODULE core_ppl
        

Generated by: LCOV version 2.0-1