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

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief routines that build the Kohn-Sham matrix  contributions
      10              : !>      coming from local atomic densities
      11              : ! **************************************************************************************************
      12              : MODULE qs_ks_atom
      13              : 
      14              :    USE ao_util,                         ONLY: trace_r_AxB
      15              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      16              :                                               get_atomic_kind,&
      17              :                                               get_atomic_kind_set
      18              :    USE basis_set_types,                 ONLY: gto_basis_set_p_type,&
      19              :                                               gto_basis_set_type
      20              :    USE cp_array_utils,                  ONLY: cp_2d_r_p_type
      21              :    USE cp_control_types,                ONLY: dft_control_type
      22              :    USE cp_dbcsr_api,                    ONLY: dbcsr_get_block_p,&
      23              :                                               dbcsr_p_type
      24              :    USE kinds,                           ONLY: dp
      25              :    USE kpoint_types,                    ONLY: get_kpoint_info,&
      26              :                                               kpoint_type
      27              :    USE message_passing,                 ONLY: mp_para_env_type
      28              :    USE qs_environment_types,            ONLY: get_qs_env,&
      29              :                                               qs_environment_type
      30              :    USE qs_force_types,                  ONLY: qs_force_type
      31              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      32              :                                               get_qs_kind_set,&
      33              :                                               qs_kind_type
      34              :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type,&
      35              :                                               neighbor_list_task_type
      36              :    USE qs_oce_methods,                  ONLY: prj_gather
      37              :    USE qs_oce_types,                    ONLY: oce_matrix_type
      38              :    USE qs_rho_atom_types,               ONLY: get_rho_atom,&
      39              :                                               rho_atom_coeff,&
      40              :                                               rho_atom_type
      41              :    USE sap_kind_types,                  ONLY: alist_post_align_blk,&
      42              :                                               alist_pre_align_blk,&
      43              :                                               alist_type,&
      44              :                                               clist_type,&
      45              :                                               get_alist
      46              :    USE util,                            ONLY: get_limit
      47              :    USE virial_methods,                  ONLY: virial_pair_force
      48              :    USE virial_types,                    ONLY: virial_type
      49              : 
      50              : !$ USE OMP_LIB, ONLY: omp_lock_kind, &
      51              : !$                    omp_init_lock, omp_set_lock, &
      52              : !$                    omp_unset_lock, omp_destroy_lock
      53              : 
      54              : #include "./base/base_uses.f90"
      55              : 
      56              :    IMPLICIT NONE
      57              : 
      58              :    PRIVATE
      59              : 
      60              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_ks_atom'
      61              :    INTEGER, PARAMETER, PRIVATE :: l2_chunk_dp = 16000
      62              : 
      63              :    PUBLIC :: update_ks_atom
      64              : 
      65              : CONTAINS
      66              : 
      67              : ! **************************************************************************************************
      68              : !> \brief Sum zero-padded rank-owned PAW potential blocks across MPI ranks in L2-sized chunks.
      69              : !> \param my_rho_atom atomic PAW data holding local potential integral blocks
      70              : !> \param natom total number of atoms
      71              : !> \param nspins number of spin components
      72              : !> \param max_gau maximum size of the one-center basis
      73              : !> \param para_env MPI parallel environment
      74              : ! **************************************************************************************************
      75        41690 :    SUBROUTINE replicate_int_scr(my_rho_atom, natom, nspins, max_gau, para_env)
      76              :       TYPE(rho_atom_type), DIMENSION(:), POINTER         :: my_rho_atom
      77              :       INTEGER, INTENT(IN)                                :: natom, nspins, max_gau
      78              :       TYPE(mp_para_env_type), POINTER                    :: para_env
      79              : 
      80              :       INTEGER                                            :: b_end, b_lo, iat, ispin, jj, m1, off, p
      81        41690 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: buf
      82        41690 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: scr_h, scr_s
      83              : 
      84       125070 :       ALLOCATE (buf(MAX(l2_chunk_dp, 2*nspins*max_gau*max_gau)))
      85        83676 :       b_lo = 1
      86        83676 :       DO WHILE (b_lo <= natom)
      87              :          b_end = natom
      88       125958 :          DO p = 1, 2
      89              :             off = 0
      90       340996 :             DO iat = b_lo, b_end
      91       257320 :                IF (.NOT. ASSOCIATED(my_rho_atom(iat)%int_scr_h(1)%r_coef)) CYCLE
      92       221736 :                m1 = SIZE(my_rho_atom(iat)%int_scr_h(1)%r_coef, 1)
      93       221736 :                IF (p == 1 .AND. off > 0 .AND. off + 2*nspins*m1*m1 > SIZE(buf)) EXIT
      94       556604 :                DO ispin = 1, nspins
      95       251192 :                   scr_h => my_rho_atom(iat)%int_scr_h(ispin)%r_coef
      96       251192 :                   scr_s => my_rho_atom(iat)%int_scr_s(ispin)%r_coef
      97      4694916 :                   DO jj = 1, m1
      98     63478794 :                      IF (p == 1) buf(off + 1:off + m1) = scr_h(1:m1, jj)
      99     61385444 :                      IF (p == 1) buf(off + m1 + 1:off + 2*m1) = scr_s(1:m1, jj)
     100     63478794 :                      IF (p == 2) scr_h(1:m1, jj) = buf(off + 1:off + m1)
     101     61385444 :                      IF (p == 2) scr_s(1:m1, jj) = buf(off + m1 + 1:off + 2*m1)
     102      4437892 :                      off = off + 2*m1
     103              :                   END DO
     104              :                END DO
     105              :             END DO
     106        83972 :             b_end = iat - 1
     107       125958 :             IF (p == 1 .AND. off > 0) CALL para_env%sum(buf(1:off))
     108              :          END DO
     109        41986 :          b_lo = b_end + 1
     110              :       END DO
     111        41690 :       DEALLOCATE (buf)
     112        41690 :    END SUBROUTINE replicate_int_scr
     113              : 
     114              : ! **************************************************************************************************
     115              : !> \brief The correction to the KS matrix due to the GAPW local terms to the hartree and
     116              : !>      XC contributions is here added. The correspondig forces contribution are also calculated
     117              : !>      if required. Each sparse-matrix block A-B is corrected by all the atomic contributions
     118              : !>      centered on atoms C for which the triplet A-C-B exists (they are close enough)
     119              : !>      To this end special lists are used
     120              : !> \param qs_env qs environment, for the lists, the contraction coefficients and the
     121              : !>               pre calculated integrals of the potential with the atomic orbitals
     122              : !> \param ksmat KS matrix, sparse matrix
     123              : !> \param pmat density matrix, sparse matrix, needed only for the forces
     124              : !> \param forces switch for the calculation of the forces on atoms
     125              : !> \param tddft switch for TDDFT linear response
     126              : !> \param rho_atom_external ...
     127              : !> \param kind_set_external ...
     128              : !> \param oce_external ...
     129              : !> \param sab_external ...
     130              : !> \param kscale ...
     131              : !> \param kintegral ...
     132              : !> \param kforce ...
     133              : !> \param fscale ...
     134              : !> \par History
     135              : !>      created [MI]
     136              : !>      the loop over the spins is done internally [03-05,MI]
     137              : !>      Rewrite using new OCE matrices [08.02.09, jhu]
     138              : !>      Add OpenMP [Apr 2016, EPCC]
     139              : !>      Allow for external kind_set, rho_atom_set, oce, sab 12.2019 (A. Bussy)
     140              : ! **************************************************************************************************
     141        41690 :    SUBROUTINE update_ks_atom(qs_env, ksmat, pmat, forces, tddft, rho_atom_external, &
     142              :                              kind_set_external, oce_external, sab_external, kscale, &
     143              :                              kintegral, kforce, fscale)
     144              : 
     145              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     146              :       TYPE(dbcsr_p_type), DIMENSION(*), INTENT(INOUT)    :: ksmat, pmat
     147              :       LOGICAL, INTENT(IN)                                :: forces
     148              :       LOGICAL, INTENT(IN), OPTIONAL                      :: tddft
     149              :       TYPE(rho_atom_type), DIMENSION(:), OPTIONAL, &
     150              :          POINTER                                         :: rho_atom_external
     151              :       TYPE(qs_kind_type), DIMENSION(:), OPTIONAL, &
     152              :          POINTER                                         :: kind_set_external
     153              :       TYPE(oce_matrix_type), OPTIONAL, POINTER           :: oce_external
     154              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     155              :          OPTIONAL, POINTER                               :: sab_external
     156              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: kscale, kintegral, kforce
     157              :       REAL(KIND=dp), DIMENSION(2), INTENT(IN), OPTIONAL  :: fscale
     158              : 
     159              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'update_ks_atom'
     160              : 
     161              :       INTEGER :: bo(2), dir, handle, i, ia_kind, iac, iat, iatom, ibc, ikind, img, inb, ip, ispin, &
     162              :          ja_kind, jatom, jkind, k_total, ka_kind, kac, kac_hi, kac_lo, katom, kbc, kkind, &
     163              :          ku_alloc, max_gau, max_nb, max_nclist, max_nsgf, n_cont_a, n_cont_b, n_nb, nat, natom, &
     164              :          nimages, nkind, nl_size, nsoctot, nspins, slot
     165        41690 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind, ia_ptr, ia_work, k_offset, &
     166        41690 :                                                             slot_order
     167              :       INTEGER, DIMENSION(3)                              :: cell_b
     168        41690 :       INTEGER, DIMENSION(:), POINTER                     :: atom_list, list_a, list_b
     169        41690 :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index
     170              :       LOGICAL                                            :: dista, do_forces, found, my_tddft, &
     171              :                                                             paw_atom, use_virial
     172        41690 :       LOGICAL, ALLOCATABLE, DIMENSION(:)                 :: has_intac, paw_kind
     173        41690 :       LOGICAL, ALLOCATABLE, DIMENSION(:, :)              :: u_touched
     174              :       REAL(KIND=dp)                                      :: beta, factor1, factor2, ieqj, trace
     175        41690 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: C_int_h, C_int_s
     176        41690 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: a_matrix, coc, f_katom, k_buf, p_matrix, &
     177        41690 :                                                             u_buf
     178              :       REAL(KIND=dp), DIMENSION(2)                        :: force_fac
     179              :       REAL(KIND=dp), DIMENSION(3)                        :: f_jatom, fb_triple, rac, rbc
     180              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: force_tmp, pv_virial_thread
     181        41690 :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER         :: C_coeff_hh_a, C_coeff_hh_b, &
     182        41690 :                                                             C_coeff_ss_a, C_coeff_ss_b
     183              :       TYPE(alist_type), POINTER                          :: alist_ac, alist_bc
     184        41690 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     185              :       TYPE(clist_type), POINTER                          :: clist_a, clist_b
     186        41690 :       TYPE(cp_2d_r_p_type), ALLOCATABLE, DIMENSION(:, :) :: nb_hblk, nb_pblk
     187              :       TYPE(dft_control_type), POINTER                    :: dft_control
     188        41690 :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_list
     189              :       TYPE(gto_basis_set_type), POINTER                  :: basis_set_a
     190              :       TYPE(kpoint_type), POINTER                         :: kpoints
     191              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     192              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     193        41690 :          POINTER                                         :: my_sab
     194              :       TYPE(neighbor_list_task_type), POINTER             :: task
     195              :       TYPE(oce_matrix_type), POINTER                     :: my_oce
     196        41690 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     197        41690 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: my_kind_set
     198        41690 :       TYPE(rho_atom_coeff), DIMENSION(:), POINTER        :: int_local_h, int_local_s
     199        41690 :       TYPE(rho_atom_type), DIMENSION(:), POINTER         :: my_rho_atom
     200              :       TYPE(virial_type), POINTER                         :: virial
     201              : 
     202        41690 : !$    INTEGER(kind=omp_lock_kind), ALLOCATABLE, DIMENSION(:) :: locks
     203              : !$    INTEGER                                            :: lock_num
     204              : 
     205        41690 :       CALL timeset(routineN, handle)
     206              : 
     207        41690 :       NULLIFY (my_kind_set, atomic_kind_set, force, my_oce, para_env, my_rho_atom, my_sab, dft_control)
     208              : 
     209              :       CALL get_qs_env(qs_env=qs_env, &
     210              :                       qs_kind_set=my_kind_set, &
     211              :                       atomic_kind_set=atomic_kind_set, &
     212              :                       force=force, &
     213              :                       oce=my_oce, &
     214              :                       para_env=para_env, &
     215              :                       rho_atom_set=my_rho_atom, &
     216              :                       virial=virial, &
     217              :                       sab_orb=my_sab, &
     218        41690 :                       dft_control=dft_control)
     219              : 
     220        41690 :       nspins = dft_control%nspins
     221        41690 :       nimages = dft_control%nimages
     222              : 
     223        41690 :       factor1 = 1.0_dp
     224        41690 :       factor2 = 1.0_dp
     225              : 
     226              :       !deal with externals
     227        41690 :       my_tddft = .FALSE.
     228        41690 :       IF (PRESENT(tddft)) my_tddft = tddft
     229        11764 :       IF (my_tddft) THEN
     230         6026 :          IF (nspins == 1) factor1 = 2.0_dp
     231         6026 :          CPASSERT(nimages == 1)
     232              :       END IF
     233        41690 :       IF (PRESENT(kscale)) THEN
     234          420 :          factor1 = factor1*kscale
     235          420 :          factor2 = factor2*kscale
     236              :       END IF
     237        41690 :       IF (PRESENT(kintegral)) factor1 = kintegral
     238        41690 :       IF (PRESENT(kforce)) factor2 = kforce
     239       125070 :       force_fac = 1.0_dp
     240        41690 :       IF (PRESENT(fscale)) force_fac(:) = fscale(:)
     241              : 
     242        41690 :       IF (PRESENT(rho_atom_external)) my_rho_atom => rho_atom_external
     243        41690 :       IF (PRESENT(kind_set_external)) my_kind_set => kind_set_external
     244        41690 :       IF (PRESENT(oce_external)) my_oce => oce_external
     245        41690 :       IF (PRESENT(sab_external)) my_sab => sab_external
     246              : 
     247              :       ! kpoint images
     248        41690 :       NULLIFY (cell_to_index)
     249        41690 :       IF (nimages > 1) THEN
     250          706 :          CALL get_qs_env(qs_env=qs_env, kpoints=kpoints)
     251          706 :          CALL get_kpoint_info(kpoint=kpoints, cell_to_index=cell_to_index)
     252              :       END IF
     253              : 
     254        41690 :       CALL get_atomic_kind_set(atomic_kind_set, natom=natom)
     255        41690 :       CALL get_qs_kind_set(my_kind_set, maxsgf=max_nsgf, maxgtops=max_gau, basis_type="GAPW_1C")
     256              : 
     257        41690 :       IF (forces) THEN
     258         2120 :          CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind)
     259         2120 :          use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
     260              :       ELSE
     261              :          use_virial = .FALSE.
     262              :       END IF
     263              : 
     264        41690 :       pv_virial_thread(:, :) = 0.0_dp ! always initialize to avoid floating point exceptions in OMP REDUCTION
     265              : 
     266        41690 :       nkind = SIZE(my_kind_set, 1)
     267              :       ! Collect the local potential contributions from all the processors
     268              :       ASSOCIATE (mepos => para_env%mepos, num_pe => para_env%num_pe)
     269       122424 :       DO ikind = 1, nkind
     270        80734 :          NULLIFY (atom_list)
     271        80734 :          CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=nat)
     272        80734 :          CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom)
     273       122424 :          IF (paw_atom) THEN
     274              :             ! gather the atomic block integrals in a more compressed format
     275        72430 :             bo = get_limit(nat, num_pe, mepos)
     276       127790 :             DO iat = bo(1), bo(2)
     277        55360 :                iatom = atom_list(iat)
     278       190588 :                DO ispin = 1, nspins
     279              :                   CALL prj_gather(my_rho_atom(iatom)%ga_Vlocal_gb_h(ispin)%r_coef, &
     280        62798 :                                   my_rho_atom(iatom)%int_scr_h(ispin)%r_coef, my_kind_set(ikind))
     281              :                   CALL prj_gather(my_rho_atom(iatom)%ga_Vlocal_gb_s(ispin)%r_coef, &
     282       118158 :                                   my_rho_atom(iatom)%int_scr_s(ispin)%r_coef, my_kind_set(ikind))
     283              :                END DO
     284              :             END DO
     285              :             ! Zero non-local PAW blocks and sum across ranks to replicate int_scr_h/s
     286       183150 :             DO ip = 1, MERGE(nat, 0, num_pe > 1)
     287       110720 :                IF (ip >= bo(1) .AND. ip <= bo(2)) CYCLE
     288       190588 :                DO ispin = 1, nspins
     289     30755520 :                   my_rho_atom(atom_list(ip))%int_scr_h(ispin)%r_coef = 0.0_dp
     290     30866240 :                   my_rho_atom(atom_list(ip))%int_scr_s(ispin)%r_coef = 0.0_dp
     291              :                END DO
     292              :             END DO
     293              :          END IF
     294              :       END DO
     295        83380 :       IF (num_pe > 1) CALL replicate_int_scr(my_rho_atom, natom, nspins, max_gau, para_env)
     296              :       END ASSOCIATE
     297              : 
     298       205804 :       ALLOCATE (basis_set_list(nkind))
     299       208450 :       ALLOCATE (paw_kind(nkind), has_intac(nkind*nkind))
     300        41690 :       paw_kind(:) = .FALSE.
     301        41690 :       has_intac(:) = .FALSE.
     302       122424 :       DO ikind = 1, nkind
     303        80734 :          CALL get_qs_kind(my_kind_set(ikind), basis_set=basis_set_a, paw_atom=paw_kind(ikind))
     304       122424 :          IF (ASSOCIATED(basis_set_a)) THEN
     305        80734 :             basis_set_list(ikind)%gto_basis_set => basis_set_a
     306              :          ELSE
     307            0 :             NULLIFY (basis_set_list(ikind)%gto_basis_set)
     308              :          END IF
     309              :       END DO
     310       213288 :       DO ikind = 1, nkind*nkind
     311       213288 :          has_intac(ikind) = ASSOCIATED(my_oce%intac(ikind)%alist)
     312              :       END DO
     313              : 
     314        41690 :       ku_alloc = MAX(1, l2_chunk_dp/nspins, max_nsgf*max_gau) ! 16k-double L2 chunk across spins
     315        41690 :       max_nclist = 1
     316       213288 :       DO iac = 1, nkind*nkind
     317       171598 :          IF (.NOT. has_intac(iac)) CYCLE
     318       428906 :          DO i = 1, SIZE(my_oce%intac(iac)%alist)
     319       405178 :             max_nclist = MAX(max_nclist, my_oce%intac(iac)%alist(i)%nclist)
     320              :          END DO
     321              :       END DO
     322              : 
     323        41690 :       CPASSERT(ASSOCIATED(my_sab(1)%nlist_task))
     324        41690 :       nl_size = my_sab(1)%nl_size
     325       291830 :       ALLOCATE (ia_ptr(natom + 1), ia_work(natom), slot_order(MAX(1, nl_size)))
     326        41690 :       ia_work(:) = 0
     327        83380 :       DO i = 1, 2
     328      2312860 :          DO slot = 1, nl_size
     329      2229480 :             task => my_sab(1)%nlist_task(slot)
     330      2229480 :             IF (.NOT. ASSOCIATED(basis_set_list(task%ikind)%gto_basis_set)) CYCLE
     331      2229480 :             IF (.NOT. ASSOCIATED(basis_set_list(task%jkind)%gto_basis_set)) CYCLE
     332      2229480 :             iatom = MIN(task%iatom, task%jatom)
     333      2229480 :             IF (i == 2) slot_order(ia_work(iatom)) = slot
     334      2312860 :             ia_work(iatom) = ia_work(iatom) + 1
     335              :          END DO
     336        83380 :          IF (i == 2) EXIT
     337        41690 :          ia_ptr(1) = 1
     338       170202 :          max_nb = MAX(1, MAXVAL(ia_work))
     339       211892 :          DO iatom = 1, natom
     340       128512 :             ia_ptr(iatom + 1) = ia_ptr(iatom) + ia_work(iatom)
     341       170202 :             ia_work(iatom) = ia_ptr(iatom)
     342              :          END DO
     343              :       END DO
     344        41690 :       DEALLOCATE (ia_work)
     345              : 
     346              : !$OMP PARALLEL DEFAULT(NONE) &
     347              : !$OMP          SHARED(natom, nkind, nspins, nimages, cell_to_index, ksmat, pmat, forces, max_gau, &
     348              : !$OMP                 max_nsgf, max_nb, max_nclist, ku_alloc, ia_ptr, slot_order, my_sab, paw_kind, &
     349              : !$OMP                 has_intac, my_oce, my_rho_atom, factor1, factor2, force_fac, use_virial, &
     350              : !$OMP                 atom_of_kind, force, locks) &
     351              : !$OMP          PRIVATE(nb_hblk, nb_pblk, C_int_h, C_int_s, coc, a_matrix, p_matrix, k_buf, k_offset, &
     352              : !$OMP                  u_buf, u_touched, f_katom, f_jatom, fb_triple, beta, dir, ieqj, trace, iatom, &
     353              : !$OMP                  n_nb, ikind, TASK, inb, jatom, cell_b, img, ispin, found, kkind, iac, &
     354              : !$OMP                  alist_ac, kac_lo, kac_hi, k_total, kac, clist_a, n_cont_a, nsoctot, katom, &
     355              : !$OMP                  int_local_h, int_local_s, C_coeff_hh_a, C_coeff_ss_a, jkind, ibc, alist_bc, &
     356              : !$OMP                  kbc, clist_b, n_cont_b, list_a, list_b, dista, C_coeff_hh_b, C_coeff_ss_b, &
     357              : !$OMP                  rac, rbc, do_forces, force_tmp, ia_kind, ja_kind, ka_kind, lock_num) &
     358        41690 : !$OMP          REDUCTION(+:pv_virial_thread)
     359              : 
     360              :       ALLOCATE (nb_hblk(nspins, max_nb), nb_pblk(nspins, max_nb), k_buf(ku_alloc, nspins), &
     361              :                 k_offset(max_nclist), C_int_h(MAX(1, max_gau*max_nsgf)), &
     362              :                 C_int_s(MAX(1, max_gau*max_nsgf)), coc(MAX(1, max_gau), MAX(1, max_gau)), &
     363              :                 a_matrix(MAX(1, max_nsgf), MAX(1, max_nsgf)), p_matrix(MAX(1, max_nsgf), MAX(1, max_nsgf)))
     364              :       IF (forces) ALLOCATE (u_buf(ku_alloc, nspins), u_touched(max_nclist, nspins), f_katom(3, max_nclist))
     365              : 
     366              :       IF (forces) THEN
     367              : !$OMP SINGLE
     368              : !$       ALLOCATE (locks(natom*nkind))
     369              : !$OMP END SINGLE
     370              : 
     371              : !$OMP DO
     372              : !$       do lock_num = 1, natom*nkind
     373              : !$          call omp_init_lock(locks(lock_num))
     374              : !$       end do
     375              : !$OMP END DO
     376              :       END IF
     377              : 
     378              : !$OMP DO SCHEDULE(DYNAMIC, 1)
     379              :       DO iatom = 1, natom
     380              :          n_nb = ia_ptr(iatom + 1) - ia_ptr(iatom)
     381              :          IF (n_nb == 0) CYCLE
     382              :          task => my_sab(1)%nlist_task(slot_order(ia_ptr(iatom)))
     383              :          ikind = MERGE(task%jkind, task%ikind, task%iatom > task%jatom)
     384              : 
     385              :          DO inb = 1, n_nb
     386              :             task => my_sab(1)%nlist_task(slot_order(ia_ptr(iatom) + inb - 1))
     387              :             jatom = MAX(task%iatom, task%jatom)
     388              :             img = 1
     389              :             IF (nimages > 1) img = cell_to_index(task%cell(1), task%cell(2), task%cell(3))
     390              :             CPASSERT(img > 0)
     391              :             DO ispin = 1, nspins
     392              :                CALL dbcsr_get_block_p(ksmat(nspins*(img - 1) + ispin)%matrix, iatom, jatom, nb_hblk(ispin, inb)%array, found)
     393              :                CPASSERT(found)
     394              :                IF (forces) THEN
     395              :                   CALL dbcsr_get_block_p(pmat(nspins*(img - 1) + ispin)%matrix, iatom, jatom, nb_pblk(ispin, inb)%array, found)
     396              :                   CPASSERT(found)
     397              :                END IF
     398              :             END DO
     399              :          END DO
     400              : 
     401              :          DO kkind = 1, nkind
     402              :             IF (.NOT. paw_kind(kkind)) CYCLE
     403              :             iac = ikind + nkind*(kkind - 1)
     404              :             IF (.NOT. has_intac(iac)) CYCLE
     405              :             CALL get_alist(my_oce%intac(iac), alist_ac, iatom)
     406              :             IF (.NOT. ASSOCIATED(alist_ac)) CYCLE
     407              : 
     408              :             ! Precompute K_a = C_a^h V_h - C_a^s V_s and accumulate U_a = sum_(B/=K) f P_AB C_b^s.
     409              :             kac_lo = 1
     410              :             DO WHILE (kac_lo <= alist_ac%nclist)
     411              :                k_total = 0
     412              :                DO kac = kac_lo, alist_ac%nclist
     413              :                   clist_a => alist_ac%clist(kac)
     414              :                   n_cont_a = clist_a%nsgf_cnt
     415              :                   IF (n_cont_a == 0) CYCLE
     416              :                   nsoctot = SIZE(clist_a%acint, 2)
     417              :                   IF (k_total > 0 .AND. k_total + n_cont_a*nsoctot > ku_alloc) EXIT
     418              :                   katom = clist_a%catom
     419              :                   k_offset(kac) = k_total + 1
     420              :                   k_total = k_total + n_cont_a*nsoctot
     421              :                   CALL get_rho_atom(rho_atom=my_rho_atom(katom), int_scr_h=int_local_h, int_scr_s=int_local_s)
     422              :                   C_coeff_ss_a => clist_a%acint
     423              :                   IF (iatom == katom .AND. ALL(clist_a%cell == 0)) THEN
     424              :                      C_coeff_hh_a => clist_a%achint
     425              :                      DO ispin = 1, nspins
     426              :                         CALL DGEMM('N', 'N', n_cont_a, nsoctot, nsoctot, 1.0_dp, C_coeff_hh_a(:, :, 1), n_cont_a, &
     427              :                                    int_local_h(ispin)%r_coef, nsoctot, 0.0_dp, k_buf(k_offset(kac):, ispin), n_cont_a)
     428              :                         CALL DGEMM('N', 'N', n_cont_a, nsoctot, nsoctot, -1.0_dp, C_coeff_ss_a(:, :, 1), n_cont_a, &
     429              :                                    int_local_s(ispin)%r_coef, nsoctot, 1.0_dp, k_buf(k_offset(kac):, ispin), n_cont_a)
     430              :                      END DO
     431              :                   ELSE
     432              :                      DO ispin = 1, nspins
     433              :                         coc(1:nsoctot, 1:nsoctot) = int_local_h(ispin)%r_coef - int_local_s(ispin)%r_coef
     434              :                         CALL DGEMM('N', 'N', n_cont_a, nsoctot, nsoctot, 1.0_dp, C_coeff_ss_a(:, :, 1), n_cont_a, &
     435              :                                    coc, SIZE(coc, 1), 0.0_dp, k_buf(k_offset(kac):, ispin), n_cont_a)
     436              :                      END DO
     437              :                   END IF
     438              :                END DO
     439              :                kac_hi = kac - 1
     440              : 
     441              :                IF (k_total > 0) THEN
     442              :                   IF (forces) THEN
     443              :                      u_touched(kac_lo:kac_hi, 1:nspins) = .FALSE.
     444              :                      f_katom(1:3, kac_lo:kac_hi) = 0.0_dp
     445              :                   END IF
     446              :                   DO inb = 1, n_nb
     447              :                      task => my_sab(1)%nlist_task(slot_order(ia_ptr(iatom) + inb - 1))
     448              :                      jkind = MERGE(task%ikind, task%jkind, task%iatom > task%jatom)
     449              :                      jatom = MAX(task%iatom, task%jatom)
     450              :                      cell_b = MERGE(-task%cell, task%cell, task%iatom > task%jatom)
     451              :                      ibc = jkind + nkind*(kkind - 1)
     452              :                      IF (.NOT. has_intac(ibc)) CYCLE
     453              :                      CALL get_alist(my_oce%intac(ibc), alist_bc, jatom)
     454              :                      IF (.NOT. ASSOCIATED(alist_bc)) CYCLE
     455              : 
     456              :                      f_jatom = 0.0_dp
     457              :                      ieqj = MERGE(1.0_dp, 2.0_dp, iatom == jatom)
     458              : 
     459              :                      DO kbc = 1, alist_bc%nclist
     460              :                         clist_b => alist_bc%clist(kbc)
     461              :                         n_cont_b = clist_b%nsgf_cnt
     462              :                         IF (n_cont_b == 0) CYCLE
     463              :                         katom = clist_b%catom
     464              :                         DO kac = kac_lo, kac_hi
     465              :                            IF (alist_ac%clist(kac)%catom /= katom) CYCLE
     466              :                            IF (ALL(cell_b + clist_b%cell == alist_ac%clist(kac)%cell)) EXIT
     467              :                         END DO
     468              :                         IF (kac > kac_hi) CYCLE
     469              :                         clist_a => alist_ac%clist(kac)
     470              :                         n_cont_a = clist_a%nsgf_cnt
     471              :                         IF (n_cont_a == 0) CYCLE
     472              :                         list_a => clist_a%sgf_list
     473              :                         list_b => clist_b%sgf_list
     474              : 
     475              :                         IF (jatom /= katom .OR. ANY(clist_b%cell /= 0)) THEN
     476              :                            nsoctot = SIZE(clist_a%acint, 2)
     477              :                            C_coeff_ss_b => clist_b%acint
     478              :                            DO ispin = 1, nspins
     479              :                               CALL DGEMM('N', 'T', n_cont_a, n_cont_b, nsoctot, factor1, k_buf(k_offset(kac):, ispin), &
     480              :                                          n_cont_a, C_coeff_ss_b(:, :, 1), n_cont_b, 0.0_dp, a_matrix, SIZE(a_matrix, 1))
     481              :                               CALL alist_post_align_blk(a_matrix, SIZE(a_matrix, 1), nb_hblk(ispin, inb)%array, &
     482              :                                                         SIZE(nb_hblk(ispin, inb)%array, 1), list_a, n_cont_a, list_b, n_cont_b)
     483              :                            END DO
     484              :                            IF (forces) THEN
     485              :                               dista = (iatom /= katom .OR. ANY(clist_a%cell /= 0))
     486              :                               fb_triple = 0.0_dp
     487              :                               DO ispin = 1, nspins
     488              :                                  CALL alist_pre_align_blk(nb_pblk(ispin, inb)%array, SIZE(nb_pblk(ispin, inb)%array, 1), &
     489              :                                                           p_matrix, SIZE(p_matrix, 1), list_a, n_cont_a, list_b, n_cont_b)
     490              :                                  CALL DGEMM('T', 'N', n_cont_b, nsoctot, n_cont_a, 1.0_dp, p_matrix, SIZE(p_matrix, 1), &
     491              :                                             k_buf(k_offset(kac):, ispin), n_cont_a, 0.0_dp, C_int_s, n_cont_b)
     492              :                                  IF (dista) THEN
     493              :                                     beta = MERGE(1.0_dp, 0.0_dp, u_touched(kac, ispin))
     494              :                                     u_touched(kac, ispin) = .TRUE.
     495              :                                     CALL DGEMM('N', 'N', n_cont_a, nsoctot, n_cont_b, ieqj*force_fac(ispin), p_matrix, &
     496              :                                                SIZE(p_matrix, 1), C_coeff_ss_b(:, :, 1), n_cont_b, beta, &
     497              :                                                u_buf(k_offset(kac):, ispin), n_cont_a)
     498              :                                  END IF
     499              :                                  DO dir = 2, 4
     500              :                                     trace = factor2*ieqj*force_fac(ispin)* &
     501              :                                             trace_r_AxB(C_int_s, n_cont_b, C_coeff_ss_b(:, :, dir), n_cont_b, n_cont_b, nsoctot)
     502              :                                     f_katom(dir - 1, kac) = f_katom(dir - 1, kac) + trace
     503              :                                     fb_triple(dir - 1) = fb_triple(dir - 1) - trace
     504              :                                  END DO
     505              :                               END DO
     506              :                               f_jatom = f_jatom + fb_triple
     507              :                               IF (use_virial) CALL virial_pair_force(pv_virial_thread, 1.0_dp, fb_triple, clist_b%rac)
     508              :                            END IF
     509              :                         ELSE
     510              :                            C_coeff_ss_a => clist_a%acint
     511              :                            IF (iatom == katom .AND. ALL(clist_a%cell == 0)) THEN
     512              :                               C_coeff_hh_a => clist_a%achint
     513              :                               dista = .FALSE.
     514              :                            ELSE
     515              :                               C_coeff_hh_a => clist_a%acint
     516              :                               dista = .TRUE.
     517              :                            END IF
     518              :                            C_coeff_hh_b => clist_b%achint
     519              :                            C_coeff_ss_b => clist_b%acint
     520              :                            nsoctot = SIZE(C_coeff_ss_a, 2)
     521              :                            CALL get_rho_atom(rho_atom=my_rho_atom(katom), int_scr_h=int_local_h, int_scr_s=int_local_s)
     522              :                            do_forces = forces .AND. dista
     523              :                            ! Use the stored block ordering for both KS and force contractions.
     524              :                            CALL add_vhxca(nb_hblk(:, inb), nb_pblk(:, inb), C_coeff_hh_a, C_coeff_hh_b, &
     525              :                                           C_coeff_ss_a, C_coeff_ss_b, int_local_h, int_local_s, nspins, &
     526              :                                           iatom, jatom, nsoctot, factor1, list_a, n_cont_a, list_b, n_cont_b, &
     527              :                                           dista, .FALSE., C_int_h, C_int_s, MAX(1, max_nsgf), coc, a_matrix, &
     528              :                                           p_matrix, do_forces, force_tmp, force_fac)
     529              : 
     530              :                            IF (do_forces) THEN
     531              :                               IF (use_virial) THEN
     532              :                                  rac = alist_ac%clist(kac)%rac
     533              :                                  rbc = alist_bc%clist(kbc)%rac
     534              :                               END IF
     535              :                               ia_kind = atom_of_kind(iatom)
     536              :                               ja_kind = atom_of_kind(jatom)
     537              :                               ka_kind = atom_of_kind(katom)
     538              :                               force_tmp = factor2*force_tmp
     539              : !$                            CALL omp_set_lock(locks((ka_kind - 1)*nkind + kkind))
     540              :                               force(kkind)%vhxc_atom(1:3, ka_kind) = force(kkind)%vhxc_atom(1:3, ka_kind) + force_tmp(1:3, 3)
     541              : !$                            CALL omp_unset_lock(locks((ka_kind - 1)*nkind + kkind))
     542              : !$                            CALL omp_set_lock(locks((ia_kind - 1)*nkind + ikind))
     543              :                               force(ikind)%vhxc_atom(1:3, ia_kind) = force(ikind)%vhxc_atom(1:3, ia_kind) + force_tmp(1:3, 1)
     544              : !$                            CALL omp_unset_lock(locks((ia_kind - 1)*nkind + ikind))
     545              : !$                            CALL omp_set_lock(locks((ja_kind - 1)*nkind + jkind))
     546              :                               force(jkind)%vhxc_atom(1:3, ja_kind) = force(jkind)%vhxc_atom(1:3, ja_kind) + force_tmp(1:3, 2)
     547              : !$                            CALL omp_unset_lock(locks((ja_kind - 1)*nkind + jkind))
     548              :                               IF (use_virial) THEN
     549              :                                  CALL virial_pair_force(pv_virial_thread, 1._dp, force_tmp(1:3, 1), rac)
     550              :                                  CALL virial_pair_force(pv_virial_thread, 1._dp, force_tmp(1:3, 2), rbc)
     551              :                               END IF
     552              :                            END IF
     553              :                         END IF
     554              :                      END DO ! kbc
     555              : 
     556              :                      IF (forces .AND. ANY(f_jatom /= 0.0_dp)) THEN
     557              :                         ja_kind = atom_of_kind(jatom)
     558              : !$                      CALL omp_set_lock(locks((ja_kind - 1)*nkind + jkind))
     559              :                         force(jkind)%vhxc_atom(:, ja_kind) = force(jkind)%vhxc_atom(:, ja_kind) + f_jatom
     560              : !$                      CALL omp_unset_lock(locks((ja_kind - 1)*nkind + jkind))
     561              :                      END IF
     562              :                   END DO
     563              : 
     564              :                   IF (forces) THEN
     565              :                      ia_kind = atom_of_kind(iatom)
     566              :                      DO kac = kac_lo, kac_hi
     567              :                         clist_a => alist_ac%clist(kac)
     568              :                         n_cont_a = clist_a%nsgf_cnt
     569              :                         katom = clist_a%catom
     570              :                         IF (ANY(u_touched(kac, 1:nspins))) THEN
     571              :                            CALL get_rho_atom(rho_atom=my_rho_atom(katom), int_scr_h=int_local_h, int_scr_s=int_local_s)
     572              :                            C_coeff_ss_a => clist_a%acint
     573              :                            nsoctot = SIZE(C_coeff_ss_a, 2)
     574              :                            fb_triple = 0.0_dp
     575              :                            DO ispin = 1, nspins
     576              :                               IF (.NOT. u_touched(kac, ispin)) CYCLE
     577              :                               coc(1:nsoctot, 1:nsoctot) = int_local_h(ispin)%r_coef - int_local_s(ispin)%r_coef
     578              :                               CALL DGEMM('N', 'N', n_cont_a, nsoctot, nsoctot, 1.0_dp, u_buf(k_offset(kac):, ispin), &
     579              :                                          n_cont_a, coc, SIZE(coc, 1), 0.0_dp, C_int_s, n_cont_a)
     580              :                               DO dir = 2, 4
     581              :                                  trace = factor2*trace_r_AxB(C_int_s, n_cont_a, C_coeff_ss_a(:, :, dir), &
     582              :                                                              n_cont_a, n_cont_a, nsoctot)
     583              :                                  f_katom(dir - 1, kac) = f_katom(dir - 1, kac) + trace
     584              :                                  fb_triple(dir - 1) = fb_triple(dir - 1) - trace
     585              :                               END DO
     586              :                            END DO
     587              : !$                         CALL omp_set_lock(locks((ia_kind - 1)*nkind + ikind))
     588              :                            force(ikind)%vhxc_atom(:, ia_kind) = force(ikind)%vhxc_atom(:, ia_kind) + fb_triple
     589              : !$                         CALL omp_unset_lock(locks((ia_kind - 1)*nkind + ikind))
     590              :                            IF (use_virial) CALL virial_pair_force(pv_virial_thread, 1.0_dp, fb_triple, clist_a%rac)
     591              :                         END IF
     592              :                         IF (ANY(f_katom(:, kac) /= 0.0_dp)) THEN
     593              :                            ka_kind = atom_of_kind(katom)
     594              : !$                         CALL omp_set_lock(locks((ka_kind - 1)*nkind + kkind))
     595              :                            force(kkind)%vhxc_atom(:, ka_kind) = force(kkind)%vhxc_atom(:, ka_kind) + f_katom(:, kac)
     596              : !$                         CALL omp_unset_lock(locks((ka_kind - 1)*nkind + kkind))
     597              :                         END IF
     598              :                      END DO
     599              :                   END IF
     600              :                END IF
     601              :                kac_lo = kac_hi + 1
     602              :             END DO
     603              :          END DO
     604              :       END DO
     605              : !$OMP END DO
     606              : 
     607              :       DEALLOCATE (nb_hblk, nb_pblk, k_buf, k_offset, C_int_h, C_int_s, a_matrix, p_matrix, coc)
     608              :       IF (forces) DEALLOCATE (u_buf, u_touched, f_katom)
     609              : 
     610              :       IF (forces) THEN
     611              :          ! Implicit barrier at end of OMP DO, so locks can be freed
     612              : !$OMP DO
     613              : !$       DO lock_num = 1, natom*nkind
     614              : !$          call omp_destroy_lock(locks(lock_num))
     615              : !$       END DO
     616              : !$OMP END DO
     617              : 
     618              : !$OMP SINGLE
     619              : !$       DEALLOCATE (locks)
     620              : !$OMP END SINGLE NOWAIT
     621              :       END IF
     622              : 
     623              : !$OMP END PARALLEL
     624              : 
     625        41690 :       IF (use_virial) THEN
     626         1742 :          virial%pv_gapw(1:3, 1:3) = virial%pv_gapw(1:3, 1:3) + pv_virial_thread(1:3, 1:3)
     627         1742 :          virial%pv_virial(1:3, 1:3) = virial%pv_virial(1:3, 1:3) + pv_virial_thread(1:3, 1:3)
     628              :       END IF
     629              : 
     630        41690 :       DEALLOCATE (ia_ptr, slot_order)
     631              : 
     632        41690 :       DEALLOCATE (basis_set_list, paw_kind, has_intac)
     633              : 
     634        41690 :       CALL timestop(handle)
     635              : 
     636       125070 :    END SUBROUTINE update_ks_atom
     637              : 
     638              : ! **************************************************************************************************
     639              : !> \brief Add one-center hard-minus-soft KS and coefficient-derivative force contributions.
     640              : !>        Real symmetric one-center potentials allow the same unscaled C*V intermediates
     641              : !>        to be used for KS assembly and forces within each spin.
     642              : !> \param mat_h KS blocks in stored (A <= B) order
     643              : !> \param mat_p density blocks in the same order, used only for forces
     644              : !> \param C_hh_a hard coefficients and derivatives on A
     645              : !> \param C_hh_b hard coefficients and derivatives on B
     646              : !> \param C_ss_a soft coefficients and derivatives on A
     647              : !> \param C_ss_b soft coefficients and derivatives on B
     648              : !> \param int_local_h hard one-center potentials by spin
     649              : !> \param int_local_s soft one-center potentials by spin
     650              : !> \param nspins number of spins
     651              : !> \param ia atom A
     652              : !> \param ja atom B
     653              : !> \param nsp size of the one-center basis
     654              : !> \param factor KS scaling, kept separate from the force scaling
     655              : !> \param lista contracted basis indices on A
     656              : !> \param nconta number of contracted functions on A
     657              : !> \param listb contracted basis indices on B
     658              : !> \param ncontb number of contracted functions on B
     659              : !> \param dista true if A is offsite relative to the expansion center
     660              : !> \param distb true if B is offsite relative to the expansion center
     661              : !> \param work_h workspace for unscaled potential-weighted coefficients
     662              : !> \param work_s workspace for soft or density-weighted coefficients
     663              : !> \param ldwork leading dimension of the coefficient workspaces
     664              : !> \param int_diff workspace for the hard-minus-soft potential
     665              : !> \param a_matrix workspace for the contracted KS block
     666              : !> \param p_matrix workspace for the aligned density block
     667              : !> \param do_forces whether nonzero coefficient-derivative contributions are needed
     668              : !> \param force forces on A, B, and the expansion center
     669              : !> \param force_scaling force scaling by spin
     670              : ! **************************************************************************************************
     671       446712 :    SUBROUTINE add_vhxca(mat_h, mat_p, C_hh_a, C_hh_b, C_ss_a, C_ss_b, &
     672       223356 :                         int_local_h, int_local_s, nspins, ia, ja, nsp, factor, &
     673       223356 :                         lista, nconta, listb, ncontb, dista, distb, &
     674       670068 :                         work_h, work_s, ldwork, int_diff, a_matrix, p_matrix, &
     675              :                         do_forces, force, force_scaling)
     676              :       TYPE(cp_2d_r_p_type), DIMENSION(:), INTENT(IN)     :: mat_h, mat_p
     677              :       REAL(KIND=dp), DIMENSION(:, :, :), INTENT(IN)      :: C_hh_a, C_hh_b, C_ss_a, C_ss_b
     678              :       TYPE(rho_atom_coeff), DIMENSION(:), INTENT(IN)     :: int_local_h, int_local_s
     679              :       INTEGER, INTENT(IN)                                :: nspins, ia, ja, nsp
     680              :       REAL(KIND=dp), INTENT(IN)                          :: factor
     681              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: lista
     682              :       INTEGER, INTENT(IN)                                :: nconta
     683              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: listb
     684              :       INTEGER, INTENT(IN)                                :: ncontb
     685              :       LOGICAL, INTENT(IN)                                :: dista, distb
     686              :       INTEGER, INTENT(IN)                                :: ldwork
     687              :       REAL(KIND=dp), INTENT(OUT)                         :: work_s(ldwork, nsp), &
     688              :                                                             work_h(ldwork, nsp), int_diff(nsp, nsp)
     689              :       REAL(dp), DIMENSION(:, :), INTENT(OUT)             :: a_matrix, p_matrix
     690              :       LOGICAL, INTENT(IN)                                :: do_forces
     691              :       REAL(dp), DIMENSION(3, 3), INTENT(OUT)             :: force
     692              :       REAL(KIND=dp), DIMENSION(2), INTENT(IN)            :: force_scaling
     693              : 
     694              :       INTEGER                                            :: dir, ispin
     695       223356 :       REAL(dp), DIMENSION(:, :), POINTER                 :: h_block, int_hard, int_soft, p_block
     696              :       REAL(KIND=dp)                                      :: ieqj, trace
     697              : 
     698       223356 :       force(:, :) = 0.0_dp
     699              :       ! Factor 2 because only half of the pairs with ia /= ja are considered.
     700       223356 :       ieqj = 2.0_dp
     701        96260 :       IF (ia == ja) ieqj = 1.0_dp
     702              : 
     703       472184 :       DO ispin = 1, nspins
     704       248828 :          h_block => mat_h(ispin)%array
     705       248828 :          int_hard => int_local_h(ispin)%r_coef
     706       248828 :          int_soft => int_local_s(ispin)%r_coef
     707              : 
     708       248828 :          IF (dista .AND. distb) int_diff(:, :) = int_hard(1:nsp, 1:nsp) - int_soft(1:nsp, 1:nsp)
     709              : 
     710       248828 :          IF (distb .AND. .NOT. dista) THEN
     711              :             ! B is offsite: H_ab = (C_a^h V_h - C_a^s V_s) C_b^T.
     712              :             CALL DGEMM('N', 'N', nconta, nsp, nsp, 1.0_dp, C_hh_a(:, :, 1), SIZE(C_hh_a, 1), &
     713            0 :                        int_hard, SIZE(int_hard, 1), 0.0_dp, work_h, ldwork)
     714              :             CALL DGEMM('N', 'N', nconta, nsp, nsp, -1.0_dp, C_ss_a(:, :, 1), SIZE(C_ss_a, 1), &
     715            0 :                        int_soft, SIZE(int_soft, 1), 1.0_dp, work_h, ldwork)
     716              :             CALL DGEMM('N', 'T', nconta, ncontb, nsp, factor, work_h, ldwork, &
     717            0 :                        C_hh_b(:, :, 1), SIZE(C_hh_b, 1), 0.0_dp, a_matrix, SIZE(a_matrix, 1))
     718              :          ELSE
     719       248828 :             IF (distb) THEN
     720              :                ! Both ends are offsite: C_h = C_s, including their derivatives.
     721              :                CALL DGEMM('N', 'N', ncontb, nsp, nsp, 1.0_dp, C_hh_b(:, :, 1), SIZE(C_hh_b, 1), &
     722            0 :                           int_diff, nsp, 0.0_dp, work_h, ldwork)
     723              :             ELSE
     724              :                CALL DGEMM('N', 'N', ncontb, nsp, nsp, 1.0_dp, C_hh_b(:, :, 1), SIZE(C_hh_b, 1), &
     725       248828 :                           int_hard, SIZE(int_hard, 1), 0.0_dp, work_h, ldwork)
     726       248828 :                IF (dista) THEN
     727              :                   CALL DGEMM('N', 'N', ncontb, nsp, nsp, -1.0_dp, C_ss_b(:, :, 1), SIZE(C_ss_b, 1), &
     728       186030 :                              int_soft, SIZE(int_soft, 1), 1.0_dp, work_h, ldwork)
     729              :                ELSE
     730              :                   CALL DGEMM('N', 'N', ncontb, nsp, nsp, 1.0_dp, C_ss_b(:, :, 1), SIZE(C_ss_b, 1), &
     731        62798 :                              int_soft, SIZE(int_soft, 1), 0.0_dp, work_s, ldwork)
     732              :                END IF
     733              :             END IF
     734              :             CALL DGEMM('N', 'T', nconta, ncontb, nsp, factor, C_hh_a(:, :, 1), SIZE(C_hh_a, 1), &
     735       248828 :                        work_h, ldwork, 0.0_dp, a_matrix, SIZE(a_matrix, 1))
     736       248828 :             IF (.NOT. dista) THEN
     737              :                CALL DGEMM('N', 'T', nconta, ncontb, nsp, -factor, C_ss_a(:, :, 1), SIZE(C_ss_a, 1), &
     738        62798 :                           work_s, ldwork, 1.0_dp, a_matrix, SIZE(a_matrix, 1))
     739              :             END IF
     740              :          END IF
     741              : 
     742              :          CALL alist_post_align_blk(a_matrix, SIZE(a_matrix, 1), h_block, SIZE(h_block, 1), &
     743       248828 :                                    lista, nconta, listb, ncontb)
     744              : 
     745       248828 :          IF (.NOT. do_forces) CYCLE
     746        12574 :          p_block => mat_p(ispin)%array
     747              :          CALL alist_pre_align_blk(p_block, SIZE(p_block, 1), p_matrix, SIZE(p_matrix, 1), &
     748        12574 :                                   lista, nconta, listb, ncontb)
     749              : 
     750              :          ! Onsite coefficient derivatives are zero (see build_oce_matrices).
     751              :          ! The offsite derivative is common to hard and soft coefficients.
     752        12574 :          IF (dista) THEN
     753              :             ! Reuse K_b = C_b^h V_h - C_b^s V_s from KS assembly.
     754              :             CALL DGEMM('N', 'N', nconta, nsp, ncontb, 1.0_dp, p_matrix, SIZE(p_matrix, 1), &
     755        12574 :                        work_h, ldwork, 0.0_dp, work_s, ldwork)
     756              :             ! <(P C_b)^T dC_a, V>_F = <dC_a, P (C_b V)>_F.
     757        50296 :             DO dir = 2, 4
     758        37722 :                trace = trace_r_AxB(work_s, ldwork, C_hh_a(:, :, dir), SIZE(C_hh_a, 1), nconta, nsp)
     759        37722 :                trace = ieqj*trace*force_scaling(ispin)
     760        37722 :                force(dir - 1, 3) = force(dir - 1, 3) + trace
     761        50296 :                force(dir - 1, 1) = force(dir - 1, 1) - trace
     762              :             END DO
     763              :          END IF
     764              : 
     765       235930 :          IF (distb) THEN
     766            0 :             IF (dista) THEN
     767              :                ! A force used K_b; now form K_a = C_a (V_h - V_s).
     768              :                CALL DGEMM('N', 'N', nconta, nsp, nsp, 1.0_dp, C_hh_a(:, :, 1), SIZE(C_hh_a, 1), &
     769            0 :                           int_diff, nsp, 0.0_dp, work_h, ldwork)
     770              :             END IF
     771              :             ! For an onsite A, K_a is already available from KS assembly.
     772              :             CALL DGEMM('T', 'N', ncontb, nsp, nconta, 1.0_dp, p_matrix, SIZE(p_matrix, 1), &
     773            0 :                        work_h, ldwork, 0.0_dp, work_s, ldwork)
     774            0 :             DO dir = 2, 4
     775            0 :                trace = trace_r_AxB(work_s, ldwork, C_hh_b(:, :, dir), SIZE(C_hh_b, 1), ncontb, nsp)
     776            0 :                trace = ieqj*trace*force_scaling(ispin)
     777            0 :                force(dir - 1, 3) = force(dir - 1, 3) + trace
     778            0 :                force(dir - 1, 2) = force(dir - 1, 2) - trace
     779              :             END DO
     780              :          END IF
     781              :       END DO
     782       223356 :    END SUBROUTINE add_vhxca
     783              : 
     784              : END MODULE qs_ks_atom
        

Generated by: LCOV version 2.0-1