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

            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 to perform the RTP in the velocity gauge
      10              : ! **************************************************************************************************
      11              : 
      12              : MODULE rt_propagation_velocity_gauge
      13              :    USE ai_moments,                      ONLY: expikr
      14              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      15              :                                               get_atomic_kind_set
      16              :    USE basis_set_types,                 ONLY: gto_basis_set_p_type,&
      17              :                                               gto_basis_set_type
      18              :    USE bibliography,                    ONLY: Mattiat2022,&
      19              :                                               cite_reference
      20              :    USE cell_types,                      ONLY: cell_type,&
      21              :                                               pbc
      22              :    USE core_ppnl,                       ONLY: build_core_ppnl
      23              :    USE cp_control_types,                ONLY: dft_control_type
      24              :    USE cp_dbcsr_api,                    ONLY: &
      25              :         dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_desymmetrize, dbcsr_get_block_p, dbcsr_init_p, &
      26              :         dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, dbcsr_iterator_start, &
      27              :         dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_p_type, dbcsr_release, dbcsr_scale, &
      28              :         dbcsr_set, dbcsr_type, dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, &
      29              :         dbcsr_type_symmetric
      30              :    USE cp_dbcsr_cp2k_link,              ONLY: cp_dbcsr_alloc_block_from_nbl
      31              :    USE cp_dbcsr_operations,             ONLY: dbcsr_allocate_matrix_set,&
      32              :                                               dbcsr_deallocate_matrix_set
      33              :    USE efield_utils,                    ONLY: make_field
      34              :    USE external_potential_types,        ONLY: gth_potential_p_type,&
      35              :                                               gth_potential_type,&
      36              :                                               sgp_potential_p_type,&
      37              :                                               sgp_potential_type
      38              :    USE input_section_types,             ONLY: section_vals_type
      39              :    USE kinds,                           ONLY: dp,&
      40              :                                               int_8
      41              :    USE kpoint_types,                    ONLY: get_kpoint_info,&
      42              :                                               kpoint_type
      43              :    USE mathconstants,                   ONLY: one,&
      44              :                                               zero
      45              :    USE orbital_pointers,                ONLY: init_orbital_pointers,&
      46              :                                               nco,&
      47              :                                               ncoset
      48              :    USE particle_types,                  ONLY: particle_type
      49              :    USE qs_environment_types,            ONLY: get_qs_env,&
      50              :                                               qs_environment_type
      51              :    USE qs_force_types,                  ONLY: qs_force_type
      52              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      53              :                                               get_qs_kind_set,&
      54              :                                               qs_kind_type
      55              :    USE qs_ks_types,                     ONLY: get_ks_env,&
      56              :                                               qs_ks_env_type
      57              :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type
      58              :    USE qs_operators_ao,                 ONLY: build_lin_mom_matrix
      59              :    USE qs_overlap,                      ONLY: build_overlap_force
      60              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
      61              :                                               qs_rho_type
      62              :    USE rt_propagation_types,            ONLY: get_rtp,&
      63              :                                               rt_prop_type
      64              :    USE sap_kind_types,                  ONLY: alist_complex_type,&
      65              :                                               clist_complex_type,&
      66              :                                               get_alist,&
      67              :                                               release_sap_int,&
      68              :                                               sap_int_complex_type,&
      69              :                                               sap_sort
      70              :    USE virial_types,                    ONLY: virial_type
      71              : 
      72              : !$ USE OMP_LIB, ONLY: omp_lock_kind, &
      73              : !$                    omp_init_lock, omp_set_lock, &
      74              : !$                    omp_unset_lock, omp_destroy_lock
      75              : 
      76              : #include "./base/base_uses.f90"
      77              : 
      78              :    IMPLICIT NONE
      79              : 
      80              :    PRIVATE
      81              : 
      82              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rt_propagation_velocity_gauge'
      83              : 
      84              :    PUBLIC :: velocity_gauge_ks_matrix, update_vector_potential, velocity_gauge_nl_force, &
      85              :              velocity_gauge_nl_commutator, velocity_gauge_kinetic_force
      86              : 
      87              : CONTAINS
      88              : 
      89              : ! **************************************************************************************************
      90              : !> \brief ...
      91              : !> \param qs_env ...
      92              : !> \param subtract_nl_term ...
      93              : ! **************************************************************************************************
      94          314 :    SUBROUTINE velocity_gauge_ks_matrix(qs_env, subtract_nl_term)
      95              :       TYPE(qs_environment_type), POINTER                 :: qs_env
      96              :       LOGICAL, INTENT(IN), OPTIONAL                      :: subtract_nl_term
      97              : 
      98              :       CHARACTER(len=*), PARAMETER :: routineN = 'velocity_gauge_ks_matrix'
      99              : 
     100              :       INTEGER                                            :: handle, idir, image, nder, nimages
     101          314 :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index
     102              :       LOGICAL                                            :: calculate_forces, my_subtract_nl_term, &
     103              :                                                             ppnl_present, use_virial
     104              :       REAL(KIND=dp)                                      :: eps_ppnl, factor
     105              :       REAL(KIND=dp), DIMENSION(3)                        :: vec_pot
     106          314 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     107              :       TYPE(cell_type), POINTER                           :: cell
     108          314 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: momentum, nl_term
     109          314 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_h, matrix_h_im, matrix_nl, &
     110          314 :                                                             matrix_p, matrix_s
     111              :       TYPE(dft_control_type), POINTER                    :: dft_control
     112              :       TYPE(kpoint_type), POINTER                         :: kpoints
     113              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     114          314 :          POINTER                                         :: sab_orb, sap_ppnl
     115          314 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     116          314 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     117          314 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     118              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     119              :       TYPE(qs_rho_type), POINTER                         :: rho
     120              :       TYPE(section_vals_type), POINTER                   :: input
     121              :       TYPE(virial_type), POINTER                         :: virial
     122              : 
     123          314 :       CALL timeset(routineN, handle)
     124              : 
     125          314 :       CALL cite_reference(Mattiat2022)
     126              : 
     127          314 :       my_subtract_nl_term = .FALSE.
     128          314 :       IF (PRESENT(subtract_nl_term)) my_subtract_nl_term = subtract_nl_term
     129              : 
     130          314 :       NULLIFY (dft_control, matrix_s, sab_orb, matrix_h, cell, input, matrix_h_im, kpoints, cell_to_index, &
     131          314 :                sap_ppnl, particle_set, qs_kind_set, atomic_kind_set, virial, force, matrix_p, rho, matrix_nl)
     132              : 
     133              :       CALL get_qs_env(qs_env, &
     134              :                       rho=rho, &
     135              :                       dft_control=dft_control, &
     136              :                       sab_orb=sab_orb, &
     137              :                       sap_ppnl=sap_ppnl, &
     138              :                       matrix_s_kp=matrix_s, &
     139              :                       matrix_h_kp=matrix_h, &
     140              :                       cell=cell, &
     141              :                       input=input, &
     142          314 :                       matrix_h_im_kp=matrix_h_im)
     143              : 
     144          314 :       nimages = dft_control%nimages
     145          314 :       ppnl_present = ASSOCIATED(sap_ppnl)
     146              :       ! The plain non-local term is only replaced if the gauge transformed one is added below
     147          314 :       IF (.NOT. dft_control%rtp_control%nl_gauge_transform) my_subtract_nl_term = .FALSE.
     148              : 
     149          314 :       IF (nimages > 1) THEN
     150            0 :          CALL get_ks_env(ks_env=ks_env, kpoints=kpoints)
     151            0 :          CALL get_kpoint_info(kpoint=kpoints, cell_to_index=cell_to_index)
     152              :       END IF
     153              : 
     154          314 :       IF (my_subtract_nl_term) THEN
     155           16 :          IF (ppnl_present) THEN
     156              :             CALL get_qs_env(qs_env, &
     157              :                             qs_kind_set=qs_kind_set, &
     158              :                             particle_set=particle_set, &
     159              :                             atomic_kind_set=atomic_kind_set, &
     160              :                             virial=virial, &
     161              :                             rho=rho, &
     162           10 :                             force=force)
     163              : 
     164           10 :             CALL qs_rho_get(rho, rho_ao_kp=matrix_p)
     165           10 :             calculate_forces = .FALSE.
     166           10 :             use_virial = .FALSE.
     167           10 :             nder = 1
     168           10 :             eps_ppnl = dft_control%qs_control%eps_ppnl
     169              : 
     170           10 :             CALL dbcsr_allocate_matrix_set(matrix_nl, 1, nimages)
     171           20 :             DO image = 1, nimages
     172           10 :                ALLOCATE (matrix_nl(1, image)%matrix)
     173           10 :                CALL dbcsr_create(matrix_nl(1, image)%matrix, template=matrix_s(1, 1)%matrix)
     174           10 :                CALL cp_dbcsr_alloc_block_from_nbl(matrix_nl(1, image)%matrix, sab_orb)
     175           20 :                CALL dbcsr_set(matrix_nl(1, image)%matrix, zero)
     176              :             END DO
     177              : 
     178              :             CALL build_core_ppnl(matrix_nl, matrix_p, force, virial, calculate_forces, use_virial, nder, &
     179              :                                  qs_kind_set, atomic_kind_set, particle_set, sab_orb, sap_ppnl, eps_ppnl, &
     180           10 :                                  nimages, cell_to_index, "ORB")
     181              : 
     182           20 :             DO image = 1, nimages
     183           20 :                CALL dbcsr_add(matrix_h(1, image)%matrix, matrix_nl(1, image)%matrix, one, -one)
     184              :             END DO
     185              : 
     186           10 :             CALL dbcsr_deallocate_matrix_set(matrix_nl)
     187              :          END IF
     188              :       END IF
     189              : 
     190              :       !get vector potential
     191         1256 :       vec_pot = dft_control%rtp_control%vec_pot
     192              : 
     193              :       ! allocate and build matrices for linear momentum term
     194          314 :       NULLIFY (momentum)
     195          314 :       CALL dbcsr_allocate_matrix_set(momentum, 3)
     196         1256 :       DO idir = 1, 3
     197          942 :          CALL dbcsr_init_p(momentum(idir)%matrix)
     198              :          CALL dbcsr_create(momentum(idir)%matrix, template=matrix_s(1, 1)%matrix, &
     199          942 :                            matrix_type=dbcsr_type_antisymmetric)
     200          942 :          CALL cp_dbcsr_alloc_block_from_nbl(momentum(idir)%matrix, sab_orb)
     201         1256 :          CALL dbcsr_set(momentum(idir)%matrix, zero)
     202              :       END DO
     203          314 :       CALL build_lin_mom_matrix(qs_env, momentum)
     204              : 
     205              :       ! set imaginary part of KS matrix to zero
     206          628 :       DO image = 1, nimages
     207          628 :          CALL dbcsr_set(matrix_h_im(1, image)%matrix, zero)
     208              :       END DO
     209              : 
     210              :       ! add linear term in vector potential to imaginary part of KS-matrix
     211          628 :       DO image = 1, nimages
     212         1570 :          DO idir = 1, 3
     213         1256 :             CALL dbcsr_add(matrix_h_im(1, image)%matrix, momentum(idir)%matrix, one, -vec_pot(idir))
     214              :          END DO
     215              :       END DO
     216              : 
     217          314 :       CALL dbcsr_deallocate_matrix_set(momentum)
     218              : 
     219              :       ! add quadratic term to real part of KS matrix
     220          314 :       factor = 0._dp
     221         1256 :       DO idir = 1, 3
     222         1256 :          factor = factor + vec_pot(idir)**2
     223              :       END DO
     224              : 
     225          628 :       DO image = 1, nimages
     226          628 :          CALL dbcsr_add(matrix_h(1, image)%matrix, matrix_s(1, image)%matrix, one, 0.5_dp*factor)
     227              :       END DO
     228              : 
     229              :       ! add Non local term
     230          314 :       IF (ppnl_present) THEN
     231          212 :          IF (dft_control%rtp_control%nl_gauge_transform) THEN
     232          136 :             NULLIFY (nl_term)
     233          136 :             CALL dbcsr_allocate_matrix_set(nl_term, 2)
     234              : 
     235          136 :             CALL dbcsr_init_p(nl_term(1)%matrix)
     236              :             CALL dbcsr_create(nl_term(1)%matrix, template=matrix_s(1, 1)%matrix, &
     237          136 :                               matrix_type=dbcsr_type_symmetric, name="nl gauge term real part")
     238          136 :             CALL cp_dbcsr_alloc_block_from_nbl(nl_term(1)%matrix, sab_orb)
     239          136 :             CALL dbcsr_set(nl_term(1)%matrix, zero)
     240              : 
     241          136 :             CALL dbcsr_init_p(nl_term(2)%matrix)
     242              :             CALL dbcsr_create(nl_term(2)%matrix, template=matrix_s(1, 1)%matrix, &
     243          136 :                               matrix_type=dbcsr_type_antisymmetric, name="nl gauge term imaginary part")
     244          136 :             CALL cp_dbcsr_alloc_block_from_nbl(nl_term(2)%matrix, sab_orb)
     245          136 :             CALL dbcsr_set(nl_term(2)%matrix, zero)
     246              : 
     247          136 :             CALL velocity_gauge_nl_term(qs_env, nl_term, vec_pot)
     248              : 
     249          272 :             DO image = 1, nimages
     250          136 :                CALL dbcsr_add(matrix_h(1, image)%matrix, nl_term(1)%matrix, one, one)
     251          272 :                CALL dbcsr_add(matrix_h_im(1, image)%matrix, nl_term(2)%matrix, one, one)
     252              :             END DO
     253          136 :             CALL dbcsr_deallocate_matrix_set(nl_term)
     254              :          END IF
     255              :       END IF
     256              : 
     257          314 :       CALL timestop(handle)
     258              : 
     259          314 :    END SUBROUTINE velocity_gauge_ks_matrix
     260              : 
     261              : ! **************************************************************************************************
     262              : !> \brief Update the vector potential in the case where a time-dependant
     263              : !>        electric field is apply.
     264              : !> \param qs_env ...
     265              : !> \param dft_control ...
     266              : ! **************************************************************************************************
     267           72 :    SUBROUTINE update_vector_potential(qs_env, dft_control)
     268              :       TYPE(qs_environment_type), INTENT(INOUT), POINTER  :: qs_env
     269              :       TYPE(dft_control_type), INTENT(INOUT), POINTER     :: dft_control
     270              : 
     271              :       REAL(kind=dp)                                      :: field(3)
     272              : 
     273           72 :       CALL make_field(dft_control, field, qs_env%sim_step, qs_env%sim_time)
     274          288 :       dft_control%rtp_control%field = field
     275          288 :       dft_control%rtp_control%vec_pot = dft_control%rtp_control%vec_pot - field*qs_env%rtp%dt
     276              :       ! Update the vec_pot_initial value for RTP restart:
     277          288 :       dft_control%efield_fields(1)%efield%vec_pot_initial = dft_control%rtp_control%vec_pot
     278              : 
     279           72 :    END SUBROUTINE update_vector_potential
     280              : 
     281              : ! **************************************************************************************************
     282              : !> \brief ...
     283              : !> \param qs_env ...
     284              : !> \param nl_term ...
     285              : !> \param vec_pot ...
     286              : ! **************************************************************************************************
     287          136 :    SUBROUTINE velocity_gauge_nl_term(qs_env, nl_term, vec_pot)
     288              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     289              :       TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT), &
     290              :          POINTER                                         :: nl_term
     291              :       REAL(KIND=dp), DIMENSION(3), INTENT(in)            :: vec_pot
     292              : 
     293              :       CHARACTER(len=*), PARAMETER :: routiuneN = "velocity_gauge_nl_term"
     294              : 
     295          136 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: zblock(:, :)
     296          136 :       COMPLEX(KIND=dp), DIMENSION(:, :, :), POINTER      :: achint, bcint
     297              :       INTEGER                                            :: handle, iac, iatom, ibc, icol, ikind, &
     298              :                                                             irow, jatom, jkind, kac, kbc, kkind, &
     299              :                                                             maxl, maxlgto, maxlppnl, na, natom, &
     300              :                                                             nb, nkind, np, slot
     301              :       INTEGER, DIMENSION(3)                              :: cell_b
     302              :       LOGICAL                                            :: found
     303              :       REAL(dp)                                           :: eps_ppnl
     304              :       REAL(KIND=dp), DIMENSION(3)                        :: rab
     305          136 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: imag_block, real_block
     306              :       TYPE(alist_complex_type), POINTER                  :: alist_ac, alist_bc
     307          136 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     308              :       TYPE(cell_type), POINTER                           :: cell
     309              :       TYPE(dft_control_type), POINTER                    :: dft_control
     310              :       TYPE(gto_basis_set_p_type), ALLOCATABLE, &
     311          136 :          DIMENSION(:)                                    :: basis_set
     312              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
     313              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     314          136 :          POINTER                                         :: sab_orb, sap_ppnl
     315          136 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     316          136 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     317          136 :       TYPE(sap_int_complex_type), DIMENSION(:), POINTER  :: sap_int
     318              : 
     319              : !$    INTEGER(kind=omp_lock_kind), &
     320          136 : !$       ALLOCATABLE, DIMENSION(:) :: locks
     321              : !$    INTEGER(KIND=int_8)                                :: iatom8
     322              : !$    INTEGER                                            :: lock_num, hash
     323              : !$    INTEGER, PARAMETER                                 :: nlock = 501
     324              : 
     325              :       MARK_USED(int_8)
     326              : 
     327          136 :       CALL timeset(routiuneN, handle)
     328              : 
     329          136 :       NULLIFY (sap_ppnl, sab_orb)
     330              :       CALL get_qs_env(qs_env, &
     331              :                       sap_ppnl=sap_ppnl, &
     332          136 :                       sab_orb=sab_orb)
     333              : 
     334          136 :       IF (ASSOCIATED(sap_ppnl)) THEN
     335          136 :          NULLIFY (qs_kind_set, particle_set, cell, dft_control)
     336              :          CALL get_qs_env(qs_env, &
     337              :                          dft_control=dft_control, &
     338              :                          qs_kind_set=qs_kind_set, &
     339              :                          particle_set=particle_set, &
     340              :                          cell=cell, &
     341          136 :                          atomic_kind_set=atomic_kind_set)
     342              : 
     343          136 :          nkind = SIZE(atomic_kind_set)
     344          136 :          natom = SIZE(particle_set)
     345          136 :          eps_ppnl = dft_control%qs_control%eps_ppnl
     346              : 
     347              :          CALL get_qs_kind_set(qs_kind_set, &
     348              :                               maxlgto=maxlgto, &
     349          136 :                               maxlppnl=maxlppnl)
     350              : 
     351          136 :          maxl = MAX(maxlppnl, maxlgto)
     352          136 :          CALL init_orbital_pointers(maxl + 1)
     353              : 
     354              :          ! initalize sab_int types to store the integrals
     355          136 :          NULLIFY (sap_int)
     356          952 :          ALLOCATE (sap_int(nkind*nkind))
     357              : 
     358              :          ! get basis set
     359          680 :          ALLOCATE (basis_set(nkind))
     360          408 :          DO ikind = 1, nkind
     361          272 :             CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set)
     362          408 :             IF (ASSOCIATED(orb_basis_set)) THEN
     363          272 :                basis_set(ikind)%gto_basis_set => orb_basis_set
     364              :             ELSE
     365            0 :                NULLIFY (basis_set(ikind)%gto_basis_set)
     366              :             END IF
     367              :          END DO
     368              : 
     369              :          ! calculate exponential integrals
     370              :          CALL build_sap_exp_ints(sap_int, sap_ppnl, qs_kind_set, particle_set, &
     371              :                                  cell, kvec=vec_pot, basis_set=basis_set, nkind=nkind, &
     372          136 :                                  derivative=.FALSE.)
     373              : 
     374          136 :          CALL sap_sort(sap_int)
     375              : 
     376              :          ! assemble the integrals for the gauge term
     377              : !$OMP PARALLEL &
     378              : !$OMP DEFAULT (NONE) &
     379              : !$OMP SHARED (basis_set, nl_term, sab_orb, sap_int, eps_ppnl, locks, nkind, natom) &
     380              : !$OMP PRIVATE (real_block, imag_block, achint, bcint, &
     381              : !$OMP           slot, ikind, jkind, iatom, jatom, cell_b, rab, irow, icol,&
     382              : !$OMP          found, kkind, iac, ibc, alist_ac, alist_bc, kac, kbc, &
     383          136 : !$OMP          zblock, na, np, nb, iatom8, hash, lock_num)
     384              : 
     385              : !$OMP SINGLE
     386              : !$       ALLOCATE (locks(nlock))
     387              : !$OMP END SINGLE
     388              : 
     389              : !$OMP DO
     390              : !$       DO lock_num = 1, nlock
     391              : !$          call omp_init_lock(locks(lock_num))
     392              : !$       END DO
     393              : !$OMP END DO
     394              : 
     395              :          NULLIFY (real_block, imag_block)
     396              :          NULLIFY (achint, bcint)
     397              : 
     398              :          ! loop over atom pairs
     399              : !$OMP DO SCHEDULE(GUIDED)
     400              :          DO slot = 1, sab_orb(1)%nl_size
     401              :             ikind = sab_orb(1)%nlist_task(slot)%ikind
     402              :             jkind = sab_orb(1)%nlist_task(slot)%jkind
     403              :             iatom = sab_orb(1)%nlist_task(slot)%iatom
     404              :             jatom = sab_orb(1)%nlist_task(slot)%jatom
     405              :             cell_b(:) = sab_orb(1)%nlist_task(slot)%cell
     406              :             rab(1:3) = sab_orb(1)%nlist_task(slot)%r(1:3)
     407              : 
     408              :             IF (.NOT. ASSOCIATED(basis_set(ikind)%gto_basis_set)) CYCLE
     409              :             IF (.NOT. ASSOCIATED(basis_set(jkind)%gto_basis_set)) CYCLE
     410              : 
     411              :             IF (iatom <= jatom) THEN
     412              :                irow = iatom
     413              :                icol = jatom
     414              :             ELSE
     415              :                irow = jatom
     416              :                icol = iatom
     417              :             END IF
     418              : 
     419              :             CALL dbcsr_get_block_p(nl_term(1)%matrix, irow, icol, real_block, found)
     420              :             CALL dbcsr_get_block_p(nl_term(2)%matrix, irow, icol, imag_block, found)
     421              : 
     422              :             IF (ASSOCIATED(real_block) .AND. ASSOCIATED(imag_block)) THEN
     423              :                ! loop over the <gto_a|ppln_c>h_ij<ppnl_c|gto_b> pairs
     424              :                DO kkind = 1, nkind
     425              :                   iac = ikind + nkind*(kkind - 1)
     426              :                   ibc = jkind + nkind*(kkind - 1)
     427              :                   IF (.NOT. ASSOCIATED(sap_int(iac)%alist)) CYCLE
     428              :                   IF (.NOT. ASSOCIATED(sap_int(ibc)%alist)) CYCLE
     429              :                   CALL get_alist(sap_int(iac), alist_ac, iatom)
     430              :                   CALL get_alist(sap_int(ibc), alist_bc, jatom)
     431              :                   IF (.NOT. ASSOCIATED(alist_ac)) CYCLE
     432              :                   IF (.NOT. ASSOCIATED(alist_bc)) CYCLE
     433              :                   DO kac = 1, alist_ac%nclist
     434              :                      DO kbc = 1, alist_bc%nclist
     435              :                         IF (alist_ac%clist(kac)%catom /= alist_bc%clist(kbc)%catom) CYCLE
     436              :                         IF (ALL(cell_b + alist_bc%clist(kbc)%cell - alist_ac%clist(kac)%cell == 0)) THEN
     437              :                            ! screening
     438              :                            IF (alist_ac%clist(kac)%maxac*alist_bc%clist(kbc)%maxach < eps_ppnl) CYCLE
     439              : 
     440              :                            ! Orient the projector factors to the stored upper atom block.
     441              :                            IF (iatom <= jatom) THEN
     442              :                               achint => alist_ac%clist(kac)%achint
     443              :                               bcint => alist_bc%clist(kbc)%acint
     444              :                            ELSE
     445              :                               achint => alist_bc%clist(kbc)%achint
     446              :                               bcint => alist_ac%clist(kac)%acint
     447              :                            END IF
     448              : 
     449              :                            na = SIZE(achint, 1)
     450              :                            np = SIZE(achint, 2)
     451              :                            nb = SIZE(bcint, 1)
     452              :                            ALLOCATE (zblock(na, nb))
     453              : !$                         iatom8 = INT(iatom - 1, int_8)*INT(natom, int_8) + INT(jatom, int_8)
     454              : !$                         hash = INT(MOD(iatom8, INT(nlock, int_8)) + 1)
     455              : !$                         CALL omp_set_lock(locks(hash))
     456              :                            ! The gauge-dressed operator uses the conjugated bra phase.
     457              :                            zblock(:, :) = MATMUL(CONJG(achint(1:na, 1:np, 1)), TRANSPOSE(bcint(1:nb, 1:np, 1)))
     458              :                            real_block(1:na, 1:nb) = real_block(1:na, 1:nb) + REAL(zblock, KIND=dp)
     459              :                            imag_block(1:na, 1:nb) = imag_block(1:na, 1:nb) + AIMAG(zblock)
     460              : !$                         CALL omp_unset_lock(locks(hash))
     461              :                            DEALLOCATE (zblock)
     462              :                            EXIT
     463              :                         END IF
     464              :                      END DO
     465              :                   END DO
     466              :                END DO
     467              :             END IF
     468              : 
     469              :          END DO
     470              : 
     471              : !$OMP DO
     472              : !$       DO lock_num = 1, nlock
     473              : !$          call omp_destroy_lock(locks(lock_num))
     474              : !$       END DO
     475              : !$OMP END DO
     476              : 
     477              : !$OMP SINGLE
     478              : !$       DEALLOCATE (locks)
     479              : !$OMP END SINGLE NOWAIT
     480              : 
     481              : !$OMP END PARALLEL
     482          136 :          CALL release_sap_int(sap_int)
     483              : 
     484          272 :          DEALLOCATE (basis_set)
     485              :       END IF
     486              : 
     487          136 :       CALL timestop(handle)
     488              : 
     489          272 :    END SUBROUTINE velocity_gauge_nl_term
     490              : 
     491              : ! **************************************************************************************************
     492              : !> \brief Calculate the commutator [Vnl~, r_d] (d=x,y,z) of the gauge-transformed nonlocal
     493              : !>        pseudopotential with the position operator, needed for the nonlocal correction
     494              : !>        to the integrated electronic current in RTP/EMD.
     495              : !>        The moment is taken relative to each projector center, which makes the result
     496              : !>        origin-independent (the constant shifts cancel exactly in the commutator).
     497              : !>        For vec_pot = 0 the plain commutator [Vnl, r_d] is obtained (matrix_cim = 0).
     498              : !> \param qs_env ...
     499              : !> \param matrix_cre real part of the commutator, antisymmetric, dimension(3)
     500              : !> \param matrix_cim imaginary part of the commutator, symmetric, dimension(3)
     501              : !> \param vec_pot vector potential defining the gauge phase, use 0 for no gauge transform
     502              : ! **************************************************************************************************
     503          144 :    SUBROUTINE velocity_gauge_nl_commutator(qs_env, matrix_cre, matrix_cim, vec_pot)
     504              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     505              :       TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT), &
     506              :          POINTER                                         :: matrix_cre, matrix_cim
     507              :       REAL(KIND=dp), DIMENSION(3), INTENT(in)            :: vec_pot
     508              : 
     509              :       CHARACTER(len=*), PARAMETER :: routiuneN = "velocity_gauge_nl_commutator"
     510              : 
     511          144 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: zblock(:, :)
     512          144 :       COMPLEX(KIND=dp), DIMENSION(:, :, :), POINTER      :: achint, bcint
     513              :       INTEGER                                            :: handle, iac, iatom, ibc, icol, idir, &
     514              :                                                             ikind, irow, jatom, jkind, kac, kbc, &
     515              :                                                             kkind, m, maxl, maxlgto, maxlppnl, na, &
     516              :                                                             natom, nb, nkind, np, slot
     517              :       INTEGER, DIMENSION(3)                              :: cell_b
     518              :       LOGICAL                                            :: found
     519              :       REAL(dp)                                           :: eps_ppnl
     520              :       REAL(KIND=dp), DIMENSION(3)                        :: rab
     521          144 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: imag_block, real_block
     522              :       TYPE(alist_complex_type), POINTER                  :: alist_ac, alist_bc
     523          144 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     524              :       TYPE(cell_type), POINTER                           :: cell
     525              :       TYPE(dft_control_type), POINTER                    :: dft_control
     526              :       TYPE(gto_basis_set_p_type), ALLOCATABLE, &
     527          144 :          DIMENSION(:)                                    :: basis_set
     528              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
     529              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     530          144 :          POINTER                                         :: sab_orb, sap_ppnl
     531          144 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     532          144 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     533          144 :       TYPE(sap_int_complex_type), DIMENSION(:), POINTER  :: sap_int
     534              : 
     535              : !$    INTEGER(kind=omp_lock_kind), &
     536          144 : !$       ALLOCATABLE, DIMENSION(:) :: locks
     537              : !$    INTEGER(KIND=int_8)                                :: iatom8
     538              : !$    INTEGER                                            :: lock_num, hash
     539              : !$    INTEGER, PARAMETER                                 :: nlock = 501
     540              : 
     541              :       MARK_USED(int_8)
     542              : 
     543          144 :       CALL timeset(routiuneN, handle)
     544              : 
     545          144 :       NULLIFY (sap_ppnl, sab_orb)
     546              :       CALL get_qs_env(qs_env, &
     547              :                       sap_ppnl=sap_ppnl, &
     548          144 :                       sab_orb=sab_orb)
     549              : 
     550          144 :       IF (ASSOCIATED(sap_ppnl)) THEN
     551          144 :          NULLIFY (qs_kind_set, particle_set, cell, dft_control)
     552              :          CALL get_qs_env(qs_env, &
     553              :                          dft_control=dft_control, &
     554              :                          qs_kind_set=qs_kind_set, &
     555              :                          particle_set=particle_set, &
     556              :                          cell=cell, &
     557          144 :                          atomic_kind_set=atomic_kind_set)
     558              : 
     559          144 :          nkind = SIZE(atomic_kind_set)
     560          144 :          natom = SIZE(particle_set)
     561          144 :          eps_ppnl = dft_control%qs_control%eps_ppnl
     562              : 
     563              :          CALL get_qs_kind_set(qs_kind_set, &
     564              :                               maxlgto=maxlgto, &
     565          144 :                               maxlppnl=maxlppnl)
     566              : 
     567              :          ! Coordinate moments use one raised basis degree in the recurrence.
     568          144 :          maxl = MAX(maxlppnl, maxlgto)
     569          144 :          CALL init_orbital_pointers(maxl + 1)
     570              : 
     571              :          ! initalize sab_int types to store the integrals
     572          144 :          NULLIFY (sap_int)
     573         1056 :          ALLOCATE (sap_int(nkind*nkind))
     574              : 
     575              :          ! get basis set
     576          728 :          ALLOCATE (basis_set(nkind))
     577          440 :          DO ikind = 1, nkind
     578          296 :             CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set)
     579          440 :             IF (ASSOCIATED(orb_basis_set)) THEN
     580          296 :                basis_set(ikind)%gto_basis_set => orb_basis_set
     581              :             ELSE
     582            0 :                NULLIFY (basis_set(ikind)%gto_basis_set)
     583              :             END IF
     584              :          END DO
     585              : 
     586              :          ! calculate exponential integrals, plain (slot 1) and moment-weighted (slots 2:4)
     587              :          CALL build_sap_exp_ints(sap_int, sap_ppnl, qs_kind_set, particle_set, &
     588              :                                  cell, kvec=vec_pot, basis_set=basis_set, nkind=nkind, &
     589          144 :                                  derivative=.FALSE., moment=.TRUE.)
     590              : 
     591          144 :          CALL sap_sort(sap_int)
     592              : 
     593              :          ! assemble the commutator matrices
     594              : !$OMP PARALLEL &
     595              : !$OMP DEFAULT (NONE) &
     596              : !$OMP SHARED (basis_set, matrix_cre, matrix_cim, sab_orb, sap_int, eps_ppnl, locks, nkind, natom) &
     597              : !$OMP PRIVATE (real_block, imag_block, achint, bcint, &
     598              : !$OMP           slot, ikind, jkind, iatom, jatom, cell_b, rab, irow, icol,&
     599              : !$OMP          found, kkind, iac, ibc, alist_ac, alist_bc, kac, kbc, &
     600          144 : !$OMP          zblock, na, np, nb, idir, m, iatom8, hash, lock_num)
     601              : 
     602              : !$OMP SINGLE
     603              : !$       ALLOCATE (locks(nlock))
     604              : !$OMP END SINGLE
     605              : 
     606              : !$OMP DO
     607              : !$       DO lock_num = 1, nlock
     608              : !$          call omp_init_lock(locks(lock_num))
     609              : !$       END DO
     610              : !$OMP END DO
     611              : 
     612              :          NULLIFY (real_block, imag_block)
     613              :          NULLIFY (achint, bcint)
     614              : 
     615              :          ! loop over atom pairs
     616              : !$OMP DO SCHEDULE(GUIDED)
     617              :          DO slot = 1, sab_orb(1)%nl_size
     618              :             ikind = sab_orb(1)%nlist_task(slot)%ikind
     619              :             jkind = sab_orb(1)%nlist_task(slot)%jkind
     620              :             iatom = sab_orb(1)%nlist_task(slot)%iatom
     621              :             jatom = sab_orb(1)%nlist_task(slot)%jatom
     622              :             cell_b(:) = sab_orb(1)%nlist_task(slot)%cell
     623              :             rab(1:3) = sab_orb(1)%nlist_task(slot)%r(1:3)
     624              : 
     625              :             IF (.NOT. ASSOCIATED(basis_set(ikind)%gto_basis_set)) CYCLE
     626              :             IF (.NOT. ASSOCIATED(basis_set(jkind)%gto_basis_set)) CYCLE
     627              : 
     628              :             IF (iatom <= jatom) THEN
     629              :                irow = iatom
     630              :                icol = jatom
     631              :             ELSE
     632              :                irow = jatom
     633              :                icol = iatom
     634              :             END IF
     635              : 
     636              :             ! loop over the <gto_a|ppln_c>h_ij<ppnl_c|gto_b> pairs
     637              :             DO kkind = 1, nkind
     638              :                iac = ikind + nkind*(kkind - 1)
     639              :                ibc = jkind + nkind*(kkind - 1)
     640              :                IF (.NOT. ASSOCIATED(sap_int(iac)%alist)) CYCLE
     641              :                IF (.NOT. ASSOCIATED(sap_int(ibc)%alist)) CYCLE
     642              :                CALL get_alist(sap_int(iac), alist_ac, iatom)
     643              :                CALL get_alist(sap_int(ibc), alist_bc, jatom)
     644              :                IF (.NOT. ASSOCIATED(alist_ac)) CYCLE
     645              :                IF (.NOT. ASSOCIATED(alist_bc)) CYCLE
     646              :                DO kac = 1, alist_ac%nclist
     647              :                   DO kbc = 1, alist_bc%nclist
     648              :                      IF (alist_ac%clist(kac)%catom /= alist_bc%clist(kbc)%catom) CYCLE
     649              :                      IF (ALL(cell_b + alist_bc%clist(kbc)%cell - alist_ac%clist(kac)%cell == 0)) THEN
     650              :                         ! screening
     651              :                         IF (alist_ac%clist(kac)%maxac*alist_bc%clist(kbc)%maxach < eps_ppnl) CYCLE
     652              : 
     653              :                         ! Orient the projector factors to the stored upper atom block.
     654              :                         IF (iatom <= jatom) THEN
     655              :                            achint => alist_ac%clist(kac)%achint
     656              :                            bcint => alist_bc%clist(kbc)%acint
     657              :                         ELSE
     658              :                            achint => alist_bc%clist(kbc)%achint
     659              :                            bcint => alist_ac%clist(kac)%acint
     660              :                         END IF
     661              : 
     662              :                         na = SIZE(achint, 1)
     663              :                         np = SIZE(achint, 2)
     664              :                         nb = SIZE(bcint, 1)
     665              :                         ALLOCATE (zblock(na, nb))
     666              : !$                      iatom8 = INT(iatom - 1, int_8)*INT(natom, int_8) + INT(jatom, int_8)
     667              : !$                      hash = INT(MOD(iatom8, INT(nlock, int_8)) + 1)
     668              : !$                      CALL omp_set_lock(locks(hash))
     669              :                         DO idir = 1, 3
     670              :                            m = 1 + idir
     671              :                            CALL dbcsr_get_block_p(matrix_cre(idir)%matrix, irow, icol, real_block, found)
     672              :                            CALL dbcsr_get_block_p(matrix_cim(idir)%matrix, irow, icol, imag_block, found)
     673              :                            IF (.NOT. (ASSOCIATED(real_block) .AND. ASSOCIATED(imag_block))) CYCLE
     674              :                            ! <a|Vnl~ (r_d - R_c)|b> - <a|(r_d - R_c) Vnl~|b>
     675              :                            zblock(:, :) = MATMUL(CONJG(achint(1:na, 1:np, 1)), TRANSPOSE(bcint(1:nb, 1:np, m))) - &
     676              :                                           MATMUL(CONJG(achint(1:na, 1:np, m)), TRANSPOSE(bcint(1:nb, 1:np, 1)))
     677              :                            real_block(1:na, 1:nb) = real_block(1:na, 1:nb) + REAL(zblock, KIND=dp)
     678              :                            imag_block(1:na, 1:nb) = imag_block(1:na, 1:nb) + AIMAG(zblock)
     679              :                         END DO
     680              : !$                      CALL omp_unset_lock(locks(hash))
     681              :                         DEALLOCATE (zblock)
     682              :                         EXIT
     683              :                      END IF
     684              :                   END DO
     685              :                END DO
     686              :             END DO
     687              : 
     688              :          END DO
     689              : 
     690              : !$OMP DO
     691              : !$       DO lock_num = 1, nlock
     692              : !$          call omp_destroy_lock(locks(lock_num))
     693              : !$       END DO
     694              : !$OMP END DO
     695              : 
     696              : !$OMP SINGLE
     697              : !$       DEALLOCATE (locks)
     698              : !$OMP END SINGLE NOWAIT
     699              : 
     700              : !$OMP END PARALLEL
     701          144 :          CALL release_sap_int(sap_int)
     702              : 
     703          288 :          DEALLOCATE (basis_set)
     704              :       END IF
     705              : 
     706          144 :       CALL timestop(handle)
     707              : 
     708          288 :    END SUBROUTINE velocity_gauge_nl_commutator
     709              : 
     710              : ! **************************************************************************************************
     711              : !> \brief Calculate complex plane-wave projector integrals in sap_int_complex_type.
     712              : !>        adapted from build_sap_ints
     713              : !>        Do this on each MPI task as the integrals need to be available globally.
     714              : !>        Might be faster than communicating as the integrals are obtained analytically.
     715              : !>        If asked, compute basis-center derivatives of the plane-wave integrals.
     716              : !>        If moment is requested, compute the moment-weighted integrals
     717              : !>        <a|exp(i k.r) (r_d - R_c,d)|p> (d=x,y,z) in slots 2:4, with the moment taken
     718              : !>        relative to the projector center R_c (the frame origin used here)
     719              : !> \param sap_int complex plane-wave integrals and their derivatives or coordinate moments
     720              : !> \param sap_ppnl ...
     721              : !> \param qs_kind_set ...
     722              : !> \param particle_set ...
     723              : !> \param cell ...
     724              : !> \param kvec ...
     725              : !> \param basis_set ...
     726              : !> \param nkind ...
     727              : !> \param derivative ...
     728              : !> \param moment ...
     729              : ! **************************************************************************************************
     730          374 :    SUBROUTINE build_sap_exp_ints(sap_int, sap_ppnl, qs_kind_set, particle_set, cell, &
     731          374 :                                  kvec, basis_set, nkind, derivative, moment)
     732              :       TYPE(sap_int_complex_type), DIMENSION(:), &
     733              :          INTENT(INOUT), POINTER                          :: sap_int
     734              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     735              :          INTENT(IN), POINTER                             :: sap_ppnl
     736              :       TYPE(qs_kind_type), DIMENSION(:), INTENT(IN), &
     737              :          POINTER                                         :: qs_kind_set
     738              :       TYPE(particle_type), DIMENSION(:), INTENT(IN), &
     739              :          POINTER                                         :: particle_set
     740              :       TYPE(cell_type), INTENT(IN), POINTER               :: cell
     741              :       REAL(KIND=dp), DIMENSION(3), INTENT(in)            :: kvec
     742              :       TYPE(gto_basis_set_p_type), DIMENSION(:), &
     743              :          INTENT(IN)                                      :: basis_set
     744              :       INTEGER, INTENT(IN)                                :: nkind
     745              :       LOGICAL, INTENT(IN)                                :: derivative
     746              :       LOGICAL, INTENT(IN), OPTIONAL                      :: moment
     747              : 
     748              :       CHARACTER(len=*), PARAMETER :: routiuneN = "build_sap_exp_ints"
     749              : 
     750          374 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: ai_work_dz(:, :, :), ai_work_z(:, :), &
     751          374 :                                                             work_dz(:, :, :), work_z(:, :)
     752              :       INTEGER :: handle, i, iac, iatom, idir, ikind, ilist, iset, jneighbor, katom, kkind, l, &
     753              :          lc_max, lc_min, ldints, lppnl, maxco, maxlppnl, maxppnl, maxsgf, na, nb, ncoa, ncoc, &
     754              :          nlist, nneighbor, np, nppnl, nprjc, nseta, nsgfa, nsl, prjc, sgfa, slot
     755              :       INTEGER, DIMENSION(3)                              :: cell_c
     756          374 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, npgfa, nprj_ppnl, &
     757          374 :                                                             nsgf_seta
     758          374 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa
     759              :       LOGICAL                                            :: dogth, my_moment
     760              :       REAL(KIND=dp)                                      :: dac, ppnl_radius
     761              :       REAL(KIND=dp), DIMENSION(1)                        :: rprjc, zetc
     762              :       REAL(KIND=dp), DIMENSION(3)                        :: ra, rac, raf, rc, rcf
     763          374 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: alpha_ppnl, set_radius_a
     764          374 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: cprj, rpgfa, sphi_a, vprj_ppnl, zeta
     765              :       TYPE(clist_complex_type), POINTER                  :: clist
     766          374 :       TYPE(gth_potential_p_type), DIMENSION(:), POINTER  :: gpotential
     767              :       TYPE(gth_potential_type), POINTER                  :: gth_potential
     768          374 :       TYPE(sgp_potential_p_type), DIMENSION(:), POINTER  :: spotential
     769              :       TYPE(sgp_potential_type), POINTER                  :: sgp_potential
     770              : 
     771          374 :       CALL timeset(routiuneN, handle)
     772              : 
     773          374 :       my_moment = .FALSE.
     774          374 :       IF (PRESENT(moment)) my_moment = moment
     775              :       ! derivative and moment integrals use the same storage slots 2:4
     776          374 :       CPASSERT(.NOT. (derivative .AND. my_moment))
     777          374 :       nsl = 1
     778          374 :       IF (derivative .OR. my_moment) nsl = 4
     779              : 
     780              :       CALL get_qs_kind_set(qs_kind_set, &
     781              :                            maxco=maxco, &
     782              :                            maxlppnl=maxlppnl, &
     783              :                            maxppnl=maxppnl, &
     784          374 :                            maxsgf=maxsgf)
     785              : 
     786              :       ! maximum dimensions for allocations
     787          374 :       ldints = MAX(maxco, ncoset(maxlppnl), maxsgf, maxppnl)
     788              : 
     789              :       !set up direct access to basis and potential
     790          374 :       NULLIFY (gpotential, spotential)
     791         3382 :       ALLOCATE (gpotential(nkind), spotential(nkind))
     792         1130 :       DO ikind = 1, nkind
     793          756 :          CALL get_qs_kind(qs_kind_set(ikind), gth_potential=gth_potential, sgp_potential=sgp_potential)
     794          756 :          NULLIFY (gpotential(ikind)%gth_potential)
     795          756 :          NULLIFY (spotential(ikind)%sgp_potential)
     796         1130 :          IF (ASSOCIATED(gth_potential)) THEN
     797          756 :             gpotential(ikind)%gth_potential => gth_potential
     798            0 :          ELSE IF (ASSOCIATED(sgp_potential)) THEN
     799            0 :             spotential(ikind)%sgp_potential => sgp_potential
     800              :          END IF
     801              :       END DO
     802              : 
     803              :       !allocate sap int
     804          374 :       NULLIFY (clist)
     805         3140 :       DO slot = 1, sap_ppnl(1)%nl_size
     806              : 
     807         2766 :          ikind = sap_ppnl(1)%nlist_task(slot)%ikind
     808         2766 :          kkind = sap_ppnl(1)%nlist_task(slot)%jkind
     809         2766 :          iatom = sap_ppnl(1)%nlist_task(slot)%iatom
     810         2766 :          katom = sap_ppnl(1)%nlist_task(slot)%jatom
     811         2766 :          nlist = sap_ppnl(1)%nlist_task(slot)%nlist
     812         2766 :          ilist = sap_ppnl(1)%nlist_task(slot)%ilist
     813         2766 :          nneighbor = sap_ppnl(1)%nlist_task(slot)%nnode
     814              : 
     815         2766 :          iac = ikind + nkind*(kkind - 1)
     816         2766 :          IF (.NOT. ASSOCIATED(basis_set(ikind)%gto_basis_set)) CYCLE
     817         2766 :          IF (.NOT. ASSOCIATED(gpotential(kkind)%gth_potential) .AND. &
     818              :              .NOT. ASSOCIATED(spotential(kkind)%sgp_potential)) CYCLE
     819         2766 :          IF (.NOT. ASSOCIATED(sap_int(iac)%alist)) THEN
     820          796 :             sap_int(iac)%a_kind = ikind
     821          796 :             sap_int(iac)%p_kind = kkind
     822          796 :             sap_int(iac)%nalist = nlist
     823         3982 :             ALLOCATE (sap_int(iac)%alist(nlist))
     824         2390 :             DO i = 1, nlist
     825         1594 :                NULLIFY (sap_int(iac)%alist(i)%clist)
     826         1594 :                sap_int(iac)%alist(i)%aatom = 0
     827         2390 :                sap_int(iac)%alist(i)%nclist = 0
     828              :             END DO
     829              :          END IF
     830         3140 :          IF (.NOT. ASSOCIATED(sap_int(iac)%alist(ilist)%clist)) THEN
     831         1594 :             sap_int(iac)%alist(ilist)%aatom = iatom
     832         1594 :             sap_int(iac)%alist(ilist)%nclist = nneighbor
     833        18706 :             ALLOCATE (sap_int(iac)%alist(ilist)%clist(nneighbor))
     834         4360 :             DO i = 1, nneighbor
     835         2766 :                clist => sap_int(iac)%alist(ilist)%clist(i)
     836         2766 :                clist%catom = 0
     837         2766 :                NULLIFY (clist%acint)
     838         2766 :                NULLIFY (clist%achint)
     839         4360 :                NULLIFY (clist%sgf_list)
     840              :             END DO
     841              :          END IF
     842              :       END DO
     843              : 
     844              :       ! actual calculation of the complex plane-wave projector integrals
     845              :       ! allocate temporary storage using maximum dimensions
     846              : 
     847              : !$OMP PARALLEL DEFAULT(NONE) &
     848              : !$OMP SHARED (basis_set, gpotential, ncoset, sap_ppnl, sap_int, nkind, ldints, maxco, nco, cell, &
     849              : !$OMP         particle_set, kvec, derivative, my_moment, nsl) &
     850              : !$OMP PRIVATE (slot, ikind, kkind, iatom, katom, nlist, ilist, nneighbor, jneighbor, cell_c, rac, dac, &
     851              : !$OMP          iac, first_sgfa, la_max, la_min, npgfa, nseta, nsgfa, nsgf_seta, rpgfa, set_radius_a, &
     852              : !$OMP          sphi_a, zeta, alpha_ppnl, cprj, lppnl, nppnl, nprj_ppnl, ppnl_radius, vprj_ppnl, clist, &
     853              : !$OMP          ra, rc, ncoa, sgfa, prjc, work_z, nprjc, rprjc, lc_max, lc_min, zetc, ncoc, ai_work_z, &
     854          374 : !$OMP          na, nb, np, dogth, raf, rcf, work_dz, ai_work_dz, idir)
     855              : 
     856              :       ALLOCATE (work_z(ldints, ldints), ai_work_z(maxco, maxco))
     857              :       IF (derivative .OR. my_moment) THEN
     858              :          ALLOCATE (work_dz(ldints, ldints, 3), ai_work_dz(maxco, maxco, 3))
     859              :       END IF
     860              :       work_z = 0.0_dp
     861              :       ai_work_z = 0.0_dp
     862              :       dogth = .FALSE.
     863              : 
     864              :       NULLIFY (first_sgfa, la_max, la_min, npgfa, nsgf_seta, rpgfa, set_radius_a, sphi_a, zeta)
     865              :       NULLIFY (alpha_ppnl, cprj, nprj_ppnl, vprj_ppnl)
     866              :       NULLIFY (clist)
     867              : 
     868              : !$OMP DO SCHEDULE(GUIDED)
     869              :       DO slot = 1, sap_ppnl(1)%nl_size
     870              :          ikind = sap_ppnl(1)%nlist_task(slot)%ikind
     871              :          kkind = sap_ppnl(1)%nlist_task(slot)%jkind
     872              :          iatom = sap_ppnl(1)%nlist_task(slot)%iatom
     873              :          katom = sap_ppnl(1)%nlist_task(slot)%jatom
     874              :          nlist = sap_ppnl(1)%nlist_task(slot)%nlist
     875              :          ilist = sap_ppnl(1)%nlist_task(slot)%ilist
     876              :          nneighbor = sap_ppnl(1)%nlist_task(slot)%nnode
     877              :          jneighbor = sap_ppnl(1)%nlist_task(slot)%inode
     878              :          cell_c(:) = sap_ppnl(1)%nlist_task(slot)%cell(:)
     879              :          rac(1:3) = sap_ppnl(1)%nlist_task(slot)%r(1:3)
     880              :          dac = NORM2(rac)
     881              : 
     882              :          iac = ikind + nkind*(kkind - 1)
     883              :          IF (.NOT. ASSOCIATED(basis_set(ikind)%gto_basis_set)) CYCLE
     884              :          ! get definition of gto basis set
     885              :          first_sgfa => basis_set(ikind)%gto_basis_set%first_sgf
     886              :          la_max => basis_set(ikind)%gto_basis_set%lmax
     887              :          la_min => basis_set(ikind)%gto_basis_set%lmin
     888              :          npgfa => basis_set(ikind)%gto_basis_set%npgf
     889              :          nseta = basis_set(ikind)%gto_basis_set%nset
     890              :          nsgfa = basis_set(ikind)%gto_basis_set%nsgf
     891              :          nsgf_seta => basis_set(ikind)%gto_basis_set%nsgf_set
     892              :          rpgfa => basis_set(ikind)%gto_basis_set%pgf_radius
     893              :          set_radius_a => basis_set(ikind)%gto_basis_set%set_radius
     894              :          sphi_a => basis_set(ikind)%gto_basis_set%sphi
     895              :          zeta => basis_set(ikind)%gto_basis_set%zet
     896              : 
     897              :          IF (ASSOCIATED(gpotential(kkind)%gth_potential)) THEN
     898              :             ! GTH potential
     899              :             dogth = .TRUE.
     900              :             alpha_ppnl => gpotential(kkind)%gth_potential%alpha_ppnl
     901              :             cprj => gpotential(kkind)%gth_potential%cprj
     902              :             lppnl = gpotential(kkind)%gth_potential%lppnl
     903              :             nppnl = gpotential(kkind)%gth_potential%nppnl
     904              :             nprj_ppnl => gpotential(kkind)%gth_potential%nprj_ppnl
     905              :             ppnl_radius = gpotential(kkind)%gth_potential%ppnl_radius
     906              :             vprj_ppnl => gpotential(kkind)%gth_potential%vprj_ppnl
     907              :          ELSE
     908              :             CYCLE
     909              :          END IF
     910              : 
     911              :          clist => sap_int(iac)%alist(ilist)%clist(jneighbor)
     912              : 
     913              :          clist%catom = katom
     914              :          clist%cell = cell_c
     915              :          clist%rac = rac
     916              : 
     917              :          ALLOCATE (clist%acint(nsgfa, nppnl, nsl), clist%achint(nsgfa, nppnl, nsl))
     918              :          clist%acint = 0.0_dp
     919              :          clist%achint = 0.0_dp
     920              :          clist%nsgf_cnt = 0
     921              : 
     922              :          ! reference point at zero
     923              :          ra(:) = pbc(particle_set(iatom)%r(:), cell)
     924              :          rc(:) = ra + rac
     925              : 
     926              :          ! reference point at pseudized atom
     927              :          raf(:) = ra - rc
     928              :          rcf(:) = 0._dp
     929              : 
     930              :          DO iset = 1, nseta
     931              :             ncoa = npgfa(iset)*ncoset(la_max(iset))
     932              :             sgfa = first_sgfa(1, iset)
     933              :             IF (dogth) THEN
     934              :                prjc = 1
     935              :                work_z = 0.0_dp
     936              :                IF (derivative .OR. my_moment) work_dz = 0.0_dp
     937              :                DO l = 0, lppnl
     938              :                   nprjc = nprj_ppnl(l)*nco(l)
     939              :                   IF (nprjc == 0) CYCLE
     940              :                   rprjc(1) = ppnl_radius
     941              :                   IF (set_radius_a(iset) + rprjc(1) < dac) CYCLE
     942              :                   lc_max = l + 2*(nprj_ppnl(l) - 1)
     943              :                   lc_min = l
     944              :                   zetc(1) = alpha_ppnl(l)
     945              :                   ncoc = ncoset(lc_max)
     946              : 
     947              :                   IF (derivative) THEN
     948              :                      CALL expikr(la_max(iset), npgfa(iset), zeta(:, iset), rpgfa(:, iset), la_min(iset), &
     949              :                                  lc_max, 1, zetc, rprjc, lc_min, raf, rcf, kvec, ai_work_z, dexpab=ai_work_dz)
     950              :                   ELSE IF (my_moment) THEN
     951              :                      ! Coordinate-weighted integrals relative to the projector center.
     952              :                      CALL expikr(la_max(iset), npgfa(iset), zeta(:, iset), rpgfa(:, iset), la_min(iset), &
     953              :                                  lc_max, 1, zetc, rprjc, lc_min, raf, rcf, kvec, ai_work_z, momentab=ai_work_dz)
     954              :                   ELSE
     955              :                      CALL expikr(la_max(iset), npgfa(iset), zeta(:, iset), rpgfa(:, iset), la_min(iset), &
     956              :                                  lc_max, 1, zetc, rprjc, lc_min, raf, rcf, kvec, ai_work_z)
     957              :                   END IF
     958              :                   ! projector functions: Cartesian -> spherical
     959              :                   na = ncoa
     960              :                   nb = nprjc
     961              :                   np = ncoc
     962              :                   work_z(1:na, prjc:prjc + nb - 1) = &
     963              :                      MATMUL(ai_work_z(1:na, 1:np), cprj(1:np, prjc:prjc + nb - 1))
     964              : 
     965              :                   IF (derivative .OR. my_moment) THEN
     966              :                      DO idir = 1, 3
     967              :                         work_dz(1:na, prjc:prjc + nb - 1, idir) = &
     968              :                            MATMUL(ai_work_dz(1:na, 1:np, idir), cprj(1:np, prjc:prjc + nb - 1))
     969              :                      END DO
     970              :                   END IF
     971              : 
     972              :                   prjc = prjc + nprjc
     973              :                END DO
     974              : 
     975              :                ! contract gto basis set into acint
     976              :                na = nsgf_seta(iset)
     977              :                nb = nppnl
     978              :                np = ncoa
     979              :                clist%acint(sgfa:sgfa + na - 1, 1:nb, 1) = &
     980              :                   MATMUL(TRANSPOSE(sphi_a(1:np, sgfa:sgfa + na - 1)), work_z(1:np, 1:nb))
     981              :                IF (derivative .OR. my_moment) THEN
     982              :                   DO idir = 1, 3
     983              :                      clist%acint(sgfa:sgfa + na - 1, 1:nb, 1 + idir) = &
     984              :                         MATMUL(TRANSPOSE(sphi_a(1:np, sgfa:sgfa + na - 1)), work_dz(1:np, 1:nb, idir))
     985              :                   END DO
     986              :                END IF
     987              : 
     988              :                ! Apply the real projector interaction to each complex integral slot.
     989              :                DO idir = 1, nsl
     990              :                   clist%achint(sgfa:sgfa + na - 1, 1:nb, idir) = &
     991              :                      MATMUL(clist%acint(sgfa:sgfa + na - 1, 1:nb, idir), vprj_ppnl(1:nb, 1:nb))
     992              :                END DO
     993              :             END IF
     994              : 
     995              :          END DO
     996              :          ! Retain the component-wise screening bound used by the split representation.
     997              :          clist%maxac = MAXVAL(MAX(ABS(REAL(clist%acint(:, :, 1), KIND=dp)), ABS(AIMAG(clist%acint(:, :, 1)))))
     998              :          clist%maxach = MAXVAL(MAX(ABS(REAL(clist%achint(:, :, 1), KIND=dp)), ABS(AIMAG(clist%achint(:, :, 1)))))
     999              :       END DO
    1000              : 
    1001              :       DEALLOCATE (work_z, ai_work_z)
    1002              :       IF (derivative .OR. my_moment) DEALLOCATE (work_dz, ai_work_dz)
    1003              : 
    1004              : !$OMP END PARALLEL
    1005              : 
    1006          374 :       DEALLOCATE (gpotential, spotential)
    1007              : 
    1008          374 :       CALL timestop(handle)
    1009              : 
    1010         1122 :    END SUBROUTINE build_sap_exp_ints
    1011              : 
    1012              : ! **************************************************************************************************
    1013              : !> \brief Calculate the force associated to non-local pseudo potential in the velocity gauge
    1014              : !> \param qs_env ...
    1015              : !> \param particle_set ...
    1016              : !> \date    09.2023
    1017              : !> \author  Guillaume Le Breton
    1018              : ! **************************************************************************************************
    1019          160 :    SUBROUTINE velocity_gauge_nl_force(qs_env, particle_set)
    1020              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1021              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1022              : 
    1023              :       CHARACTER(len=*), PARAMETER :: routiuneN = "velocity_gauge_nl_force"
    1024              : 
    1025          160 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: zblock(:, :), zblock_b(:, :)
    1026          160 :       COMPLEX(KIND=dp), DIMENSION(:, :, :), POINTER      :: achint, acint, bchint, bcint
    1027              :       INTEGER :: handle, i, iac, iatom, ibc, icol, idir, ikind, irow, jatom, jkind, kac, katom, &
    1028              :          kbc, kkind, maxl, maxlgto, maxlppnl, na, natom, nb, nkind, np, slot
    1029          160 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind, kind_of
    1030              :       INTEGER, DIMENSION(3)                              :: cell_b
    1031              :       LOGICAL                                            :: found_imag, found_real
    1032              :       REAL(dp)                                           :: eps_ppnl, f0
    1033              :       REAL(KIND=dp), DIMENSION(3)                        :: fa, fb, rab, vec_pot
    1034              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1035          160 :          POINTER                                         :: sab_orb, sap_ppnl
    1036              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
    1037              :       TYPE(gto_basis_set_p_type), ALLOCATABLE, &
    1038          160 :          DIMENSION(:)                                    :: basis_set
    1039              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1040          160 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao, rho_ao_im
    1041              :       TYPE(cell_type), POINTER                           :: cell
    1042          160 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1043              :       TYPE(alist_complex_type), POINTER                  :: alist_ac, alist_bc
    1044          160 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: matrix_p_imag, matrix_p_real
    1045          320 :       REAL(KIND=dp), DIMENSION(3, SIZE(particle_set))    :: force_thread
    1046          160 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
    1047          160 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1048              :       TYPE(qs_rho_type), POINTER                         :: rho
    1049          160 :       TYPE(sap_int_complex_type), DIMENSION(:), POINTER  :: sap_int
    1050              : 
    1051          160 :       CALL timeset(routiuneN, handle)
    1052              : 
    1053          160 :       NULLIFY (sap_ppnl)
    1054              : 
    1055              :       CALL get_qs_env(qs_env, &
    1056          160 :                       sap_ppnl=sap_ppnl)
    1057              : 
    1058          160 :       IF (ASSOCIATED(sap_ppnl)) THEN
    1059           94 :          NULLIFY (qs_kind_set, cell, dft_control, force, sab_orb, atomic_kind_set, &
    1060           94 :                   sap_int)
    1061              :          ! Load and initialized the required quantities
    1062              : 
    1063              :          CALL get_qs_env(qs_env, &
    1064              :                          sab_orb=sab_orb, &
    1065              :                          force=force, &
    1066              :                          dft_control=dft_control, &
    1067              :                          qs_kind_set=qs_kind_set, &
    1068              :                          cell=cell, &
    1069              :                          atomic_kind_set=atomic_kind_set, &
    1070           94 :                          rho=rho)
    1071              : 
    1072           94 :          nkind = SIZE(atomic_kind_set)
    1073           94 :          natom = SIZE(particle_set)
    1074           94 :          eps_ppnl = dft_control%qs_control%eps_ppnl
    1075              : 
    1076              :          CALL get_qs_kind_set(qs_kind_set, &
    1077              :                               maxlgto=maxlgto, &
    1078           94 :                               maxlppnl=maxlppnl)
    1079              : 
    1080           94 :          maxl = MAX(maxlppnl, maxlgto)
    1081           94 :          CALL init_orbital_pointers(maxl + 1)
    1082              : 
    1083              :          ! initalize sab_int types to store the integrals
    1084          658 :          ALLOCATE (sap_int(nkind*nkind))
    1085              : 
    1086              :          ! get basis set
    1087          470 :          ALLOCATE (basis_set(nkind))
    1088          282 :          DO ikind = 1, nkind
    1089          188 :             CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set)
    1090          282 :             IF (ASSOCIATED(orb_basis_set)) THEN
    1091          188 :                basis_set(ikind)%gto_basis_set => orb_basis_set
    1092              :             ELSE
    1093            0 :                NULLIFY (basis_set(ikind)%gto_basis_set)
    1094              :             END IF
    1095              :          END DO
    1096              : 
    1097              :          !get vector potential
    1098          376 :          vec_pot = dft_control%rtp_control%vec_pot
    1099              : 
    1100         1342 :          force_thread = 0.0_dp
    1101              : 
    1102           94 :          CALL qs_rho_get(rho_struct=rho, rho_ao=rho_ao, rho_ao_im=rho_ao_im)
    1103              :          ! To avoid FOR loop over spin, sum the 2 spin into the first one directly. Undone later on
    1104           94 :          IF (SIZE(rho_ao) == 2) THEN
    1105              :             CALL dbcsr_add(rho_ao(1)%matrix, rho_ao(2)%matrix, &
    1106            0 :                            alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
    1107              :             CALL dbcsr_add(rho_ao_im(1)%matrix, rho_ao_im(2)%matrix, &
    1108            0 :                            alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
    1109              :          END IF
    1110              : 
    1111              :          ! Compute complex plane-wave projector integrals and their basis-center derivatives.
    1112              :          CALL build_sap_exp_ints(sap_int, sap_ppnl, qs_kind_set, particle_set, &
    1113           94 :                                  cell, kvec=vec_pot, basis_set=basis_set, nkind=nkind, derivative=.TRUE.)
    1114           94 :          CALL sap_sort(sap_int)
    1115              : 
    1116              :          ! Compute the force, on nuclei A it is given by: Re(P_ab) Re(dV_ab/dRA) - Im(P_ab) Im(dV_ab/dRA)
    1117              : 
    1118              : !$OMP PARALLEL &
    1119              : !$OMP DEFAULT (NONE) &
    1120              : !$OMP SHARED (basis_set, sab_orb, sap_int, eps_ppnl, nkind, natom,&
    1121              : !$OMP         rho_ao, rho_ao_im) &
    1122              : !$OMP PRIVATE (matrix_p_real, matrix_p_imag, acint, achint, bcint, bchint, &
    1123              : !$OMP           slot, ikind, jkind, iatom, jatom,&
    1124              : !$OMP          cell_b, rab, irow, icol, fa, fb, f0, found_real, found_imag, &
    1125              : !$OMP          kkind, iac, ibc, alist_ac, alist_bc, kac, kbc,&
    1126              : !$OMP          zblock, zblock_b, na, np, nb,  katom) &
    1127           94 : !$OMP REDUCTION (+ : force_thread )
    1128              : 
    1129              :          NULLIFY (acint, bcint, achint, bchint)
    1130              : 
    1131              :          ! loop over atom pairs
    1132              : !$OMP DO SCHEDULE(GUIDED)
    1133              :          DO slot = 1, sab_orb(1)%nl_size
    1134              :             ikind = sab_orb(1)%nlist_task(slot)%ikind
    1135              :             jkind = sab_orb(1)%nlist_task(slot)%jkind
    1136              :             iatom = sab_orb(1)%nlist_task(slot)%iatom
    1137              :             jatom = sab_orb(1)%nlist_task(slot)%jatom
    1138              :             cell_b(:) = sab_orb(1)%nlist_task(slot)%cell
    1139              :             rab(1:3) = sab_orb(1)%nlist_task(slot)%r(1:3)
    1140              : 
    1141              :             IF (.NOT. ASSOCIATED(basis_set(ikind)%gto_basis_set)) CYCLE
    1142              :             IF (.NOT. ASSOCIATED(basis_set(jkind)%gto_basis_set)) CYCLE
    1143              : 
    1144              :             ! Use the symmetry of the first derivatives
    1145              :             IF (iatom == jatom) THEN
    1146              :                f0 = 1.0_dp
    1147              :             ELSE
    1148              :                f0 = 2.0_dp
    1149              :             END IF
    1150              : 
    1151              :             fa = 0.0_dp
    1152              :             fb = 0.0_dp
    1153              : 
    1154              :             IF (iatom <= jatom) THEN
    1155              :                irow = iatom
    1156              :                icol = jatom
    1157              :             ELSE
    1158              :                irow = jatom
    1159              :                icol = iatom
    1160              :             END IF
    1161              :             NULLIFY (matrix_p_real, matrix_p_imag)
    1162              :             CALL dbcsr_get_block_p(rho_ao(1)%matrix, irow, icol, matrix_p_real, found_real)
    1163              :             CALL dbcsr_get_block_p(rho_ao_im(1)%matrix, irow, icol, matrix_p_imag, found_imag)
    1164              : 
    1165              :             IF (found_real .OR. found_imag) THEN
    1166              :                ! loop over the <gto_a|ppln_c>h_ij<ppnl_c|gto_b> pairs
    1167              :                DO kkind = 1, nkind
    1168              :                   iac = ikind + nkind*(kkind - 1)
    1169              :                   ibc = jkind + nkind*(kkind - 1)
    1170              :                   IF (.NOT. ASSOCIATED(sap_int(iac)%alist)) CYCLE
    1171              :                   IF (.NOT. ASSOCIATED(sap_int(ibc)%alist)) CYCLE
    1172              :                   CALL get_alist(sap_int(iac), alist_ac, iatom)
    1173              :                   CALL get_alist(sap_int(ibc), alist_bc, jatom)
    1174              :                   IF (.NOT. ASSOCIATED(alist_ac)) CYCLE
    1175              :                   IF (.NOT. ASSOCIATED(alist_bc)) CYCLE
    1176              :                   DO kac = 1, alist_ac%nclist
    1177              :                      DO kbc = 1, alist_bc%nclist
    1178              :                         IF (alist_ac%clist(kac)%catom /= alist_bc%clist(kbc)%catom) CYCLE
    1179              :                         IF (ALL(cell_b + alist_bc%clist(kbc)%cell - alist_ac%clist(kac)%cell == 0)) THEN
    1180              :                            ! screening
    1181              :                            IF (alist_ac%clist(kac)%maxac*alist_bc%clist(kbc)%maxach < eps_ppnl) CYCLE
    1182              : 
    1183              :                            acint => alist_ac%clist(kac)%acint
    1184              :                            bcint => alist_bc%clist(kbc)%acint
    1185              :                            achint => alist_ac%clist(kac)%achint
    1186              :                            bchint => alist_bc%clist(kbc)%achint
    1187              : 
    1188              :                            na = SIZE(acint, 1)
    1189              :                            np = SIZE(acint, 2)
    1190              :                            nb = SIZE(bcint, 1)
    1191              :                            ! Differentiate the complex projector product on each basis center.
    1192              :                            katom = alist_ac%clist(kac)%catom
    1193              :                            ALLOCATE (zblock(MERGE(na, nb, iatom <= jatom), MERGE(nb, na, iatom <= jatom)))
    1194              :                            ALLOCATE (zblock_b(SIZE(zblock, 1), SIZE(zblock, 2)))
    1195              :                            ! In both orientations zblock is conj(dV/dR) on the stored block, so
    1196              :                            ! Re(P) Re(dV) + Im(P) Im(dV) needs no sign change for the imaginary part
    1197              :                            DO idir = 1, 3
    1198              :                               IF (iatom <= jatom) THEN
    1199              :                                  zblock(:, :) = MATMUL(acint(1:na, 1:np, 1 + idir), &
    1200              :                                                        CONJG(TRANSPOSE(bchint(1:nb, 1:np, 1))))
    1201              :                                  zblock_b(:, :) = MATMUL(achint(1:na, 1:np, 1), &
    1202              :                                                          CONJG(TRANSPOSE(bcint(1:nb, 1:np, 1 + idir))))
    1203              :                               ELSE
    1204              :                                  zblock(:, :) = MATMUL(bchint(1:nb, 1:np, 1), &
    1205              :                                                        CONJG(TRANSPOSE(acint(1:na, 1:np, 1 + idir))))
    1206              :                                  zblock_b(:, :) = MATMUL(bcint(1:nb, 1:np, 1 + idir), &
    1207              :                                                          CONJG(TRANSPOSE(achint(1:na, 1:np, 1))))
    1208              :                               END IF
    1209              :                               fa(idir) = 0.0_dp
    1210              :                               fb(idir) = 0.0_dp
    1211              :                               IF (found_real) THEN
    1212              :                                  fa(idir) = SUM(matrix_p_real(1:SIZE(zblock, 1), 1:SIZE(zblock, 2))*REAL(zblock, KIND=dp))
    1213              :                                  fb(idir) = SUM(matrix_p_real(1:SIZE(zblock_b, 1), 1:SIZE(zblock_b, 2))*REAL(zblock_b, KIND=dp))
    1214              :                               END IF
    1215              :                               IF (found_imag) THEN
    1216              :                                  fa(idir) = fa(idir) - SUM(matrix_p_imag(1:SIZE(zblock, 1), 1:SIZE(zblock, 2))*AIMAG(zblock))
    1217              :                                  fb(idir) = fb(idir) - SUM(matrix_p_imag(1:SIZE(zblock_b, 1), 1:SIZE(zblock_b, 2))*AIMAG(zblock_b))
    1218              :                               END IF
    1219              :                               force_thread(idir, iatom) = force_thread(idir, iatom) + f0*fa(idir)
    1220              :                               force_thread(idir, katom) = force_thread(idir, katom) - f0*fa(idir)
    1221              :                               force_thread(idir, jatom) = force_thread(idir, jatom) + f0*fb(idir)
    1222              :                               force_thread(idir, katom) = force_thread(idir, katom) - f0*fb(idir)
    1223              :                            END DO
    1224              :                            DEALLOCATE (zblock, zblock_b)
    1225              :                            EXIT
    1226              :                         END IF
    1227              :                      END DO
    1228              :                   END DO
    1229              :                END DO
    1230              :             END IF
    1231              : 
    1232              :          END DO
    1233              : 
    1234              : !$OMP END PARALLEL
    1235              : 
    1236              :          ! Update the force
    1237           94 :          CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
    1238              : !$OMP DO
    1239              :          DO iatom = 1, natom
    1240          312 :             i = atom_of_kind(iatom)
    1241          312 :             ikind = kind_of(iatom)
    1242         1248 :             force(ikind)%gth_ppnl(:, i) = force(ikind)%gth_ppnl(:, i) + force_thread(:, iatom)
    1243              :          END DO
    1244              : !$OMP END DO
    1245              : 
    1246              :          ! Clean up
    1247           94 :          IF (SIZE(rho_ao) == 2) THEN
    1248              :             CALL dbcsr_add(rho_ao(1)%matrix, rho_ao(2)%matrix, &
    1249            0 :                            alpha_scalar=1.0_dp, beta_scalar=-1.0_dp)
    1250              :             CALL dbcsr_add(rho_ao_im(1)%matrix, rho_ao_im(2)%matrix, &
    1251            0 :                            alpha_scalar=1.0_dp, beta_scalar=-1.0_dp)
    1252              :          END IF
    1253           94 :          CALL release_sap_int(sap_int)
    1254              : 
    1255          188 :          DEALLOCATE (basis_set, atom_of_kind, kind_of)
    1256              : 
    1257              :       END IF
    1258              : 
    1259          160 :       CALL timestop(handle)
    1260              : 
    1261          320 :    END SUBROUTINE velocity_gauge_nl_force
    1262              : 
    1263              : ! **************************************************************************************************
    1264              : !> \brief Force from the field-dependent kinetic terms of the velocity gauge,
    1265              : !>        1/2 (p + A)^2 = 1/2 p^2 + A.p + 1/2 A^2. With H_im = - sum_i A_i M_i,
    1266              : !>        M_i = <a|d_i|b> = D_A S_ab (build_lin_mom_matrix), and H_re = H_re + 1/2 A^2 S:
    1267              : !>        dE/dR = - sum_i A_i Tr(P_im dM_i/dR) + 1/2 A^2 Tr(P_re dS/dR)
    1268              : !> \param qs_env ...
    1269              : ! **************************************************************************************************
    1270          222 :    SUBROUTINE velocity_gauge_kinetic_force(qs_env)
    1271              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1272              : 
    1273              :       CHARACTER(len=*), PARAMETER :: routineN = 'velocity_gauge_kinetic_force'
    1274              :       INTEGER, DIMENSION(3, 3), PARAMETER :: ider = RESHAPE([4, 5, 6, 5, 7, 8, 6, 8, 9], [3, 3])
    1275              : 
    1276              :       INTEGER                                            :: col_atom, handle, iatom, idir, ispin, &
    1277              :                                                             jdir, natom, row_atom
    1278          222 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind, kind_of
    1279              :       LOGICAL                                            :: found
    1280              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: fk
    1281              :       REAL(KIND=dp), DIMENSION(3)                        :: fab, vec_pot
    1282          222 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: p_block, s_block
    1283          222 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1284              :       TYPE(dbcsr_iterator_type)                          :: iter
    1285          222 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao, rho_ao_im, S_der
    1286              :       TYPE(dbcsr_type)                                   :: matrix_p
    1287              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1288              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1289          222 :          POINTER                                         :: sab_orb
    1290          222 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
    1291              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1292              :       TYPE(qs_rho_type), POINTER                         :: rho
    1293              :       TYPE(rt_prop_type), POINTER                        :: rtp
    1294              : 
    1295          222 :       CALL timeset(routineN, handle)
    1296              : 
    1297              :       CALL get_qs_env(qs_env, dft_control=dft_control, ks_env=ks_env, rho=rho, force=force, &
    1298          222 :                       atomic_kind_set=atomic_kind_set, sab_orb=sab_orb, natom=natom, rtp=rtp)
    1299          222 :       CALL qs_rho_get(rho, rho_ao=rho_ao, rho_ao_im=rho_ao_im)
    1300          222 :       CALL get_rtp(rtp=rtp, S_der=S_der)
    1301          888 :       vec_pot = dft_control%rtp_control%vec_pot
    1302              : 
    1303          666 :       ALLOCATE (fk(3, natom))
    1304          222 :       fk = 0.0_dp
    1305              : 
    1306              :       ! 1/2 A^2 Tr(P_re dS/dR), build_overlap_force adds -Tr(P dS/dR)
    1307          222 :       CALL dbcsr_create(matrix_p, template=rho_ao(1)%matrix)
    1308          222 :       CALL dbcsr_copy(matrix_p, rho_ao(1)%matrix)
    1309          222 :       DO ispin = 2, SIZE(rho_ao)
    1310          222 :          CALL dbcsr_add(matrix_p, rho_ao(ispin)%matrix, 1.0_dp, 1.0_dp)
    1311              :       END DO
    1312          888 :       CALL dbcsr_scale(matrix_p, -0.5_dp*DOT_PRODUCT(vec_pot, vec_pot))
    1313              :       CALL build_overlap_force(ks_env, fk, basis_type_a="ORB", basis_type_b="ORB", &
    1314          222 :                                sab_nl=sab_orb, matrix_p=matrix_p)
    1315          222 :       CALL dbcsr_release(matrix_p)
    1316              : 
    1317              :       ! - sum_i A_i Tr(P_im dM_i/dR), with dM_i/dR_A,j = D_A,j D_A,i S = - dM_i/dR_B,j.
    1318              :       ! The second derivatives of S are in S_der(4:9) (calc_S_derivs, stored as -D_A,i D_A,j S),
    1319              :       ! the on-site blocks are zero there. P_im is antisymmetric, so the upper triangle
    1320              :       ! counted twice gives the full trace. It is taken from a full copy, as a block of the
    1321              :       ! antisymmetric matrix need not be on the process that holds it in S_der.
    1322          222 :       CALL dbcsr_create(matrix_p, template=S_der(1)%matrix, matrix_type=dbcsr_type_no_symmetry)
    1323          444 :       DO ispin = 1, SIZE(rho_ao_im)
    1324          222 :          CALL dbcsr_desymmetrize(rho_ao_im(ispin)%matrix, matrix_p)
    1325          222 :          CALL dbcsr_iterator_start(iter, matrix_p)
    1326         1356 :          DO WHILE (dbcsr_iterator_blocks_left(iter))
    1327         1134 :             CALL dbcsr_iterator_next_block(iter, row_atom, col_atom, p_block)
    1328         1134 :             IF (row_atom >= col_atom) CYCLE
    1329          393 :             fab = 0.0_dp
    1330         1572 :             DO jdir = 1, 3
    1331         5109 :                DO idir = 1, 3
    1332         3537 :                   NULLIFY (s_block)
    1333         3537 :                   CALL dbcsr_get_block_p(S_der(ider(idir, jdir))%matrix, row_atom, col_atom, s_block, found)
    1334         3537 :                   IF (.NOT. found) CYCLE
    1335       214893 :                   fab(jdir) = fab(jdir) + vec_pot(idir)*SUM(p_block*s_block)
    1336              :                END DO
    1337              :             END DO
    1338         1572 :             fk(:, row_atom) = fk(:, row_atom) + 2.0_dp*fab(:)
    1339         2535 :             fk(:, col_atom) = fk(:, col_atom) - 2.0_dp*fab(:)
    1340              :          END DO
    1341          666 :          CALL dbcsr_iterator_stop(iter)
    1342              :       END DO
    1343          222 :       CALL dbcsr_release(matrix_p)
    1344              : 
    1345              :       ! add the local contribution, the kinetic force is summed over ranks later
    1346          222 :       CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
    1347          918 :       DO iatom = 1, natom
    1348              :          force(kind_of(iatom))%kinetic(:, atom_of_kind(iatom)) = &
    1349         3006 :             force(kind_of(iatom))%kinetic(:, atom_of_kind(iatom)) + fk(:, iatom)
    1350              :       END DO
    1351              : 
    1352          222 :       DEALLOCATE (fk)
    1353              : 
    1354          222 :       CALL timestop(handle)
    1355              : 
    1356          444 :    END SUBROUTINE velocity_gauge_kinetic_force
    1357              : 
    1358              : END MODULE rt_propagation_velocity_gauge
        

Generated by: LCOV version 2.0-1