LCOV - code coverage report
Current view: top level - src - xtb_coulomb.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:936074a) Lines: 98.6 % 487 480
Test Date: 2025-12-04 06:27:48 Functions: 100.0 % 4 4

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2025 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Calculation of Coulomb contributions in xTB
      10              : !> \author JGH
      11              : ! **************************************************************************************************
      12              : MODULE xtb_coulomb
      13              :    USE ai_contraction,                  ONLY: block_add,&
      14              :                                               contraction
      15              :    USE ai_overlap,                      ONLY: overlap_ab
      16              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      17              :                                               get_atomic_kind_set
      18              :    USE atprop_types,                    ONLY: atprop_array_init,&
      19              :                                               atprop_type
      20              :    USE basis_set_types,                 ONLY: gto_basis_set_p_type,&
      21              :                                               gto_basis_set_type
      22              :    USE cell_types,                      ONLY: cell_type,&
      23              :                                               get_cell,&
      24              :                                               pbc
      25              :    USE cp_control_types,                ONLY: dft_control_type,&
      26              :                                               xtb_control_type
      27              :    USE cp_dbcsr_api,                    ONLY: dbcsr_add,&
      28              :                                               dbcsr_get_block_p,&
      29              :                                               dbcsr_iterator_blocks_left,&
      30              :                                               dbcsr_iterator_next_block,&
      31              :                                               dbcsr_iterator_start,&
      32              :                                               dbcsr_iterator_stop,&
      33              :                                               dbcsr_iterator_type,&
      34              :                                               dbcsr_p_type
      35              :    USE distribution_1d_types,           ONLY: distribution_1d_type
      36              :    USE ewald_environment_types,         ONLY: ewald_env_get,&
      37              :                                               ewald_environment_type
      38              :    USE ewald_methods_tb,                ONLY: tb_ewald_overlap,&
      39              :                                               tb_spme_evaluate
      40              :    USE ewald_pw_types,                  ONLY: ewald_pw_type
      41              :    USE kinds,                           ONLY: dp
      42              :    USE kpoint_types,                    ONLY: get_kpoint_info,&
      43              :                                               kpoint_type
      44              :    USE mathconstants,                   ONLY: oorootpi,&
      45              :                                               pi
      46              :    USE message_passing,                 ONLY: mp_para_env_type
      47              :    USE orbital_pointers,                ONLY: ncoset
      48              :    USE particle_types,                  ONLY: particle_type
      49              :    USE pw_poisson_types,                ONLY: do_ewald_ewald,&
      50              :                                               do_ewald_none,&
      51              :                                               do_ewald_pme,&
      52              :                                               do_ewald_spme
      53              :    USE qmmm_tb_coulomb,                 ONLY: build_tb_coulomb_qmqm
      54              :    USE qs_dftb3_methods,                ONLY: build_dftb3_diagonal
      55              :    USE qs_energy_types,                 ONLY: qs_energy_type
      56              :    USE qs_environment_types,            ONLY: get_qs_env,&
      57              :                                               qs_environment_type
      58              :    USE qs_force_types,                  ONLY: qs_force_type
      59              :    USE qs_integral_utils,               ONLY: basis_set_list_setup,&
      60              :                                               get_memory_usage
      61              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      62              :                                               qs_kind_type
      63              :    USE qs_neighbor_list_types,          ONLY: get_iterator_info,&
      64              :                                               neighbor_list_iterate,&
      65              :                                               neighbor_list_iterator_create,&
      66              :                                               neighbor_list_iterator_p_type,&
      67              :                                               neighbor_list_iterator_release,&
      68              :                                               neighbor_list_set_p_type
      69              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
      70              :                                               qs_rho_type
      71              :    USE sap_kind_types,                  ONLY: clist_type,&
      72              :                                               release_sap_int,&
      73              :                                               sap_int_type
      74              :    USE virial_methods,                  ONLY: virial_pair_force
      75              :    USE virial_types,                    ONLY: virial_type
      76              :    USE xtb_types,                       ONLY: get_xtb_atom_param,&
      77              :                                               xtb_atom_type
      78              : #include "./base/base_uses.f90"
      79              : 
      80              :    IMPLICIT NONE
      81              : 
      82              :    PRIVATE
      83              : 
      84              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'xtb_coulomb'
      85              : 
      86              :    PUBLIC :: build_xtb_coulomb, gamma_rab_sr, dgamma_rab_sr, xtb_dsint_list
      87              : 
      88              : CONTAINS
      89              : 
      90              : ! **************************************************************************************************
      91              : !> \brief ...
      92              : !> \param qs_env ...
      93              : !> \param ks_matrix ...
      94              : !> \param rho ...
      95              : !> \param charges ...
      96              : !> \param mcharge ...
      97              : !> \param energy ...
      98              : !> \param calculate_forces ...
      99              : !> \param just_energy ...
     100              : ! **************************************************************************************************
     101        23898 :    SUBROUTINE build_xtb_coulomb(qs_env, ks_matrix, rho, charges, mcharge, energy, &
     102              :                                 calculate_forces, just_energy)
     103              : 
     104              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     105              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: ks_matrix
     106              :       TYPE(qs_rho_type), POINTER                         :: rho
     107              :       REAL(dp), DIMENSION(:, :), INTENT(in)              :: charges
     108              :       REAL(dp), DIMENSION(:), INTENT(in)                 :: mcharge
     109              :       TYPE(qs_energy_type), POINTER                      :: energy
     110              :       LOGICAL, INTENT(in)                                :: calculate_forces, just_energy
     111              : 
     112              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'build_xtb_coulomb'
     113              : 
     114              :       INTEGER :: atom_i, atom_j, ewald_type, handle, i, ia, iac, iatom, ic, icol, ikind, img, &
     115              :          irow, is, j, jatom, jkind, la, lb, lmaxa, lmaxb, natom, natorb_a, natorb_b, ni, nimg, nj, &
     116              :          nkind, nmat, za, zb
     117        23898 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind, kind_of
     118              :       INTEGER, DIMENSION(25)                             :: laoa, laob
     119              :       INTEGER, DIMENSION(3)                              :: cellind, periodic
     120              :       INTEGER, DIMENSION(5)                              :: occ
     121        23898 :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index
     122              :       LOGICAL                                            :: defined, do_ewald, do_gamma_stress, &
     123              :                                                             found, use_virial
     124              :       REAL(KIND=dp)                                      :: alpha, deth, dr, ecsr, etaa, etab, f1, &
     125              :                                                             f2, fi, gmij, kg, rcut, rcuta, rcutb, &
     126              :                                                             zeff
     127        23898 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: xgamma, zeffk
     128        23898 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: gammab, gcij, gmcharge
     129        23898 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: gchrg
     130              :       REAL(KIND=dp), DIMENSION(25)                       :: gcint
     131              :       REAL(KIND=dp), DIMENSION(3)                        :: fij, rij
     132              :       REAL(KIND=dp), DIMENSION(5)                        :: kappaa, kappab
     133        23898 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: dsblock, ksblock, pblock, sblock
     134        23898 :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER         :: dsint
     135        23898 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     136              :       TYPE(atprop_type), POINTER                         :: atprop
     137              :       TYPE(cell_type), POINTER                           :: cell
     138              :       TYPE(dbcsr_iterator_type)                          :: iter
     139        23898 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_p, matrix_s
     140              :       TYPE(dft_control_type), POINTER                    :: dft_control
     141              :       TYPE(distribution_1d_type), POINTER                :: local_particles
     142              :       TYPE(ewald_environment_type), POINTER              :: ewald_env
     143              :       TYPE(ewald_pw_type), POINTER                       :: ewald_pw
     144              :       TYPE(kpoint_type), POINTER                         :: kpoints
     145              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     146              :       TYPE(neighbor_list_iterator_p_type), &
     147        23898 :          DIMENSION(:), POINTER                           :: nl_iterator
     148              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     149        23898 :          POINTER                                         :: n_list
     150        23898 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     151        23898 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     152        23898 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     153        23898 :       TYPE(sap_int_type), DIMENSION(:), POINTER          :: sap_int
     154              :       TYPE(virial_type), POINTER                         :: virial
     155              :       TYPE(xtb_atom_type), POINTER                       :: xtb_atom_a, xtb_atom_b, xtb_kind
     156              :       TYPE(xtb_control_type), POINTER                    :: xtb_control
     157              : 
     158        23898 :       CALL timeset(routineN, handle)
     159              : 
     160        23898 :       NULLIFY (matrix_p, matrix_s, virial, atprop, dft_control)
     161              : 
     162              :       CALL get_qs_env(qs_env, &
     163              :                       qs_kind_set=qs_kind_set, &
     164              :                       particle_set=particle_set, &
     165              :                       cell=cell, &
     166              :                       virial=virial, &
     167              :                       atprop=atprop, &
     168        23898 :                       dft_control=dft_control)
     169              : 
     170        23898 :       xtb_control => dft_control%qs_control%xtb_control
     171              : 
     172        23898 :       use_virial = .FALSE.
     173        23898 :       IF (calculate_forces) THEN
     174          868 :          use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
     175              :       END IF
     176              : 
     177        23898 :       do_gamma_stress = .FALSE.
     178        23898 :       IF (.NOT. just_energy .AND. use_virial) THEN
     179          108 :          IF (dft_control%nimages == 1) do_gamma_stress = .TRUE.
     180              :       END IF
     181              : 
     182        23898 :       IF (atprop%energy) THEN
     183          172 :          CALL get_qs_env(qs_env=qs_env, particle_set=particle_set)
     184          172 :          natom = SIZE(particle_set)
     185          172 :          CALL atprop_array_init(atprop%atecoul, natom)
     186              :       END IF
     187              : 
     188        23898 :       IF (calculate_forces) THEN
     189              :          nmat = 4
     190              :       ELSE
     191        23410 :          nmat = 1
     192              :       END IF
     193              : 
     194        23898 :       CALL get_qs_env(qs_env, nkind=nkind, natom=natom)
     195        95592 :       ALLOCATE (gchrg(natom, 5, nmat))
     196      1338930 :       gchrg = 0._dp
     197        95592 :       ALLOCATE (gmcharge(natom, nmat))
     198       281832 :       gmcharge = 0._dp
     199              : 
     200              :       ! short range contribution (gamma)
     201              :       ! loop over all atom pairs (sab_xtbe)
     202        23898 :       kg = xtb_control%kg
     203        23898 :       NULLIFY (n_list)
     204        23898 :       CALL get_qs_env(qs_env=qs_env, sab_xtbe=n_list)
     205        23898 :       CALL neighbor_list_iterator_create(nl_iterator, n_list)
     206      8617978 :       DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
     207              :          CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, &
     208      8594080 :                                 iatom=iatom, jatom=jatom, r=rij, cell=cellind)
     209      8594080 :          CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_atom_a)
     210      8594080 :          CALL get_xtb_atom_param(xtb_atom_a, defined=defined, natorb=natorb_a)
     211      8594080 :          IF (.NOT. defined .OR. natorb_a < 1) CYCLE
     212      8594073 :          CALL get_qs_kind(qs_kind_set(jkind), xtb_parameter=xtb_atom_b)
     213      8594073 :          CALL get_xtb_atom_param(xtb_atom_b, defined=defined, natorb=natorb_b)
     214      8594073 :          IF (.NOT. defined .OR. natorb_b < 1) CYCLE
     215              :          ! atomic parameters
     216      8594066 :          CALL get_xtb_atom_param(xtb_atom_a, eta=etaa, lmax=lmaxa, kappa=kappaa, rcut=rcuta)
     217      8594066 :          CALL get_xtb_atom_param(xtb_atom_b, eta=etab, lmax=lmaxb, kappa=kappab, rcut=rcutb)
     218              :          ! gamma matrix
     219      8594066 :          ni = lmaxa + 1
     220      8594066 :          nj = lmaxb + 1
     221     34376264 :          ALLOCATE (gammab(ni, nj))
     222      8594066 :          rcut = rcuta + rcutb
     223     34376264 :          dr = SQRT(SUM(rij(:)**2))
     224      8594066 :          CALL gamma_rab_sr(gammab, dr, ni, kappaa, etaa, nj, kappab, etab, kg, rcut)
     225    116389040 :          gchrg(iatom, 1:ni, 1) = gchrg(iatom, 1:ni, 1) + MATMUL(gammab, charges(jatom, 1:nj))
     226      8594066 :          IF (iatom /= jatom) THEN
     227    115599942 :             gchrg(jatom, 1:nj, 1) = gchrg(jatom, 1:nj, 1) + MATMUL(charges(iatom, 1:ni), gammab)
     228              :          END IF
     229      8594066 :          IF (calculate_forces) THEN
     230       341660 :             IF (dr > 1.e-6_dp) THEN
     231       339478 :                CALL dgamma_rab_sr(gammab, dr, ni, kappaa, etaa, nj, kappab, etab, kg, rcut)
     232      1357912 :                DO i = 1, 3
     233              :                   gchrg(iatom, 1:ni, i + 1) = gchrg(iatom, 1:ni, i + 1) &
     234     14703972 :                                               + MATMUL(gammab, charges(jatom, 1:nj))*rij(i)/dr
     235      1357912 :                   IF (iatom /= jatom) THEN
     236              :                      gchrg(jatom, 1:nj, i + 1) = gchrg(jatom, 1:nj, i + 1) &
     237     14633208 :                                                  - MATMUL(charges(iatom, 1:ni), gammab)*rij(i)/dr
     238              :                   END IF
     239              :                END DO
     240       339478 :                IF (use_virial) THEN
     241      2379863 :                   gcint(1:ni) = MATMUL(gammab, charges(jatom, 1:nj))
     242       756600 :                   DO i = 1, 3
     243      1807584 :                      fij(i) = -SUM(charges(iatom, 1:ni)*gcint(1:ni))*rij(i)/dr
     244              :                   END DO
     245       189150 :                   fi = 1.0_dp
     246       189150 :                   IF (iatom == jatom) fi = 0.5_dp
     247       189150 :                   CALL virial_pair_force(virial%pv_virial, fi, fij, rij)
     248              :                END IF
     249              :             END IF
     250              :          END IF
     251     34376285 :          DEALLOCATE (gammab)
     252              :       END DO
     253        23898 :       CALL neighbor_list_iterator_release(nl_iterator)
     254              : 
     255              :       ! 1/R contribution
     256              : 
     257        23898 :       IF (xtb_control%coulomb_lr) THEN
     258        23898 :          do_ewald = xtb_control%do_ewald
     259        23898 :          IF (do_ewald) THEN
     260              :             ! Ewald sum
     261        11118 :             NULLIFY (ewald_env, ewald_pw)
     262              :             CALL get_qs_env(qs_env=qs_env, &
     263        11118 :                             ewald_env=ewald_env, ewald_pw=ewald_pw)
     264        11118 :             CALL get_cell(cell=cell, periodic=periodic, deth=deth)
     265        11118 :             CALL ewald_env_get(ewald_env, alpha=alpha, ewald_type=ewald_type)
     266        11118 :             CALL get_qs_env(qs_env=qs_env, sab_tbe=n_list)
     267        11118 :             CALL tb_ewald_overlap(gmcharge, mcharge, alpha, n_list, virial, use_virial)
     268            0 :             SELECT CASE (ewald_type)
     269              :             CASE DEFAULT
     270            0 :                CPABORT("Invalid Ewald type")
     271              :             CASE (do_ewald_none)
     272            0 :                CPABORT("Not allowed with xTB/DFTB")
     273              :             CASE (do_ewald_ewald)
     274            0 :                CPABORT("Standard Ewald not implemented in xTB/DFTB")
     275              :             CASE (do_ewald_pme)
     276            0 :                CPABORT("PME not implemented in xTB/DFTB")
     277              :             CASE (do_ewald_spme)
     278              :                CALL tb_spme_evaluate(ewald_env, ewald_pw, particle_set, cell, &
     279        11118 :                                      gmcharge, mcharge, calculate_forces, virial, use_virial)
     280              :             END SELECT
     281              :          ELSE
     282              :             ! direct sum
     283              :             CALL get_qs_env(qs_env=qs_env, &
     284        12780 :                             local_particles=local_particles)
     285        49190 :             DO ikind = 1, SIZE(local_particles%n_el)
     286       116615 :                DO ia = 1, local_particles%n_el(ikind)
     287        67425 :                   iatom = local_particles%list(ikind)%array(ia)
     288       834417 :                   DO jatom = 1, iatom - 1
     289      2922328 :                      rij = particle_set(iatom)%r - particle_set(jatom)%r
     290      2922328 :                      rij = pbc(rij, cell)
     291      2922328 :                      dr = SQRT(SUM(rij(:)**2))
     292       798007 :                      IF (dr > 1.e-6_dp) THEN
     293       730582 :                         gmcharge(iatom, 1) = gmcharge(iatom, 1) + mcharge(jatom)/dr
     294       730582 :                         gmcharge(jatom, 1) = gmcharge(jatom, 1) + mcharge(iatom)/dr
     295       735778 :                         DO i = 2, nmat
     296         5196 :                            gmcharge(iatom, i) = gmcharge(iatom, i) + rij(i - 1)*mcharge(jatom)/dr**3
     297       735778 :                            gmcharge(jatom, i) = gmcharge(jatom, i) - rij(i - 1)*mcharge(iatom)/dr**3
     298              :                         END DO
     299              :                      END IF
     300              :                   END DO
     301              :                END DO
     302              :             END DO
     303        12780 :             CPASSERT(.NOT. use_virial)
     304              :          END IF
     305              :       END IF
     306              : 
     307              :       ! global sum of gamma*p arrays
     308              :       CALL get_qs_env(qs_env=qs_env, &
     309              :                       atomic_kind_set=atomic_kind_set, &
     310        23898 :                       force=force, para_env=para_env)
     311        23898 :       CALL para_env%sum(gmcharge(:, 1))
     312        23898 :       CALL para_env%sum(gchrg(:, :, 1))
     313              : 
     314        23898 :       IF (xtb_control%coulomb_lr) THEN
     315        23898 :          IF (do_ewald) THEN
     316              :             ! add self charge interaction and background charge contribution
     317        99624 :             gmcharge(:, 1) = gmcharge(:, 1) - 2._dp*alpha*oorootpi*mcharge(:)
     318        12054 :             IF (ANY(periodic(:) == 1)) THEN
     319        98064 :                gmcharge(:, 1) = gmcharge(:, 1) - pi/alpha**2/deth
     320              :             END IF
     321              :          END IF
     322              :       END IF
     323              : 
     324              :       ! energy
     325              :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, &
     326              :                                kind_of=kind_of, &
     327        23898 :                                atom_of_kind=atom_of_kind)
     328        23898 :       ecsr = 0.0_dp
     329       244296 :       DO iatom = 1, natom
     330       220398 :          ikind = kind_of(iatom)
     331       220398 :          CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
     332       220398 :          CALL get_xtb_atom_param(xtb_kind, lmax=ni)
     333       220398 :          ni = ni + 1
     334       593034 :          ecsr = ecsr + SUM(charges(iatom, 1:ni)*gchrg(iatom, 1:ni, 1))
     335              :       END DO
     336              : 
     337        23898 :       energy%hartree = energy%hartree + 0.5_dp*ecsr
     338       244296 :       energy%hartree = energy%hartree + 0.5_dp*SUM(mcharge(:)*gmcharge(:, 1))
     339              : 
     340        23898 :       IF (atprop%energy) THEN
     341          172 :          CALL get_qs_env(qs_env=qs_env, local_particles=local_particles)
     342          748 :          DO ikind = 1, SIZE(local_particles%n_el)
     343          576 :             CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
     344          576 :             CALL get_xtb_atom_param(xtb_kind, lmax=ni, occupation=occ)
     345          576 :             ni = ni + 1
     346         3456 :             zeff = SUM(REAL(occ, KIND=dp))
     347         4360 :             DO ia = 1, local_particles%n_el(ikind)
     348         3036 :                iatom = local_particles%list(ikind)%array(ia)
     349              :                atprop%atecoul(iatom) = atprop%atecoul(iatom) + &
     350         7258 :                                        0.5_dp*SUM(REAL(occ(1:ni), KIND=dp)*gchrg(iatom, 1:ni, 1))
     351              :                atprop%atecoul(iatom) = atprop%atecoul(iatom) + &
     352         3612 :                                        0.5_dp*zeff*gmcharge(iatom, 1)
     353              :             END DO
     354              :          END DO
     355              :       END IF
     356              : 
     357        23898 :       IF (calculate_forces) THEN
     358         4546 :          DO iatom = 1, natom
     359         4058 :             ikind = kind_of(iatom)
     360         4058 :             atom_i = atom_of_kind(iatom)
     361         4058 :             CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
     362         4058 :             CALL get_xtb_atom_param(xtb_kind, lmax=ni)
     363              :             ! short range
     364         4058 :             ni = ni + 1
     365        16232 :             DO i = 1, 3
     366        35216 :                fij(i) = SUM(charges(iatom, 1:ni)*gchrg(iatom, 1:ni, i + 1))
     367              :             END DO
     368         4058 :             force(ikind)%rho_elec(1, atom_i) = force(ikind)%rho_elec(1, atom_i) - fij(1)
     369         4058 :             force(ikind)%rho_elec(2, atom_i) = force(ikind)%rho_elec(2, atom_i) - fij(2)
     370         4058 :             force(ikind)%rho_elec(3, atom_i) = force(ikind)%rho_elec(3, atom_i) - fij(3)
     371              :             ! long range
     372        16232 :             DO i = 1, 3
     373        16232 :                fij(i) = gmcharge(iatom, i + 1)*mcharge(iatom)
     374              :             END DO
     375         4058 :             force(ikind)%rho_elec(1, atom_i) = force(ikind)%rho_elec(1, atom_i) - fij(1)
     376         4058 :             force(ikind)%rho_elec(2, atom_i) = force(ikind)%rho_elec(2, atom_i) - fij(2)
     377         8604 :             force(ikind)%rho_elec(3, atom_i) = force(ikind)%rho_elec(3, atom_i) - fij(3)
     378              :          END DO
     379              :       END IF
     380              : 
     381        23898 :       IF (.NOT. just_energy) THEN
     382        23392 :          CALL get_qs_env(qs_env=qs_env, matrix_s_kp=matrix_s)
     383        23392 :          CALL qs_rho_get(rho, rho_ao_kp=matrix_p)
     384              : 
     385        23392 :          nimg = dft_control%nimages
     386        23392 :          NULLIFY (cell_to_index)
     387        23392 :          IF (nimg > 1) THEN
     388         2656 :             NULLIFY (kpoints)
     389         2656 :             CALL get_qs_env(qs_env=qs_env, kpoints=kpoints)
     390         2656 :             CALL get_kpoint_info(kpoint=kpoints, cell_to_index=cell_to_index)
     391              :          END IF
     392              : 
     393        23392 :          IF (calculate_forces .AND. SIZE(matrix_p, 1) == 2) THEN
     394          432 :             DO img = 1, nimg
     395              :                CALL dbcsr_add(matrix_p(1, img)%matrix, matrix_p(2, img)%matrix, &
     396          432 :                               alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
     397              :             END DO
     398              :          END IF
     399              : 
     400        23392 :          NULLIFY (sap_int)
     401        23392 :          IF (do_gamma_stress) THEN
     402              :             ! derivative overlap integral (non collapsed)
     403           94 :             CALL xtb_dsint_list(qs_env, sap_int)
     404              :          END IF
     405              : 
     406        23392 :          IF (nimg == 1) THEN
     407              :             ! no k-points; all matrices have been transformed to periodic bsf
     408        20736 :             CALL dbcsr_iterator_start(iter, matrix_s(1, 1)%matrix)
     409      1419470 :             DO WHILE (dbcsr_iterator_blocks_left(iter))
     410      1398734 :                CALL dbcsr_iterator_next_block(iter, irow, icol, sblock)
     411      1398734 :                ikind = kind_of(irow)
     412      1398734 :                jkind = kind_of(icol)
     413              : 
     414              :                ! atomic parameters
     415      1398734 :                CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_atom_a)
     416      1398734 :                CALL get_qs_kind(qs_kind_set(jkind), xtb_parameter=xtb_atom_b)
     417      1398734 :                CALL get_xtb_atom_param(xtb_atom_a, z=za, lao=laoa)
     418      1398734 :                CALL get_xtb_atom_param(xtb_atom_b, z=zb, lao=laob)
     419              : 
     420      1398734 :                ni = SIZE(sblock, 1)
     421      1398734 :                nj = SIZE(sblock, 2)
     422      5594936 :                ALLOCATE (gcij(ni, nj))
     423      5717481 :                DO i = 1, ni
     424     19230711 :                   DO j = 1, nj
     425     13513230 :                      la = laoa(i) + 1
     426     13513230 :                      lb = laob(j) + 1
     427     17831977 :                      gcij(i, j) = 0.5_dp*(gchrg(irow, la, 1) + gchrg(icol, lb, 1))
     428              :                   END DO
     429              :                END DO
     430      1398734 :                gmij = 0.5_dp*(gmcharge(irow, 1) + gmcharge(icol, 1))
     431      2805864 :                DO is = 1, SIZE(ks_matrix, 1)
     432      1407130 :                   NULLIFY (ksblock)
     433              :                   CALL dbcsr_get_block_p(matrix=ks_matrix(is, 1)%matrix, &
     434      1407130 :                                          row=irow, col=icol, block=ksblock, found=found)
     435      1407130 :                   CPASSERT(found)
     436     37210294 :                   ksblock = ksblock - gcij*sblock
     437     40016158 :                   ksblock = ksblock - gmij*sblock
     438              :                END DO
     439      1398734 :                IF (calculate_forces) THEN
     440        46367 :                   atom_i = atom_of_kind(irow)
     441        46367 :                   atom_j = atom_of_kind(icol)
     442        46367 :                   NULLIFY (pblock)
     443              :                   CALL dbcsr_get_block_p(matrix=matrix_p(1, 1)%matrix, &
     444        46367 :                                          row=irow, col=icol, block=pblock, found=found)
     445        46367 :                   CPASSERT(found)
     446       185468 :                   DO i = 1, 3
     447       139101 :                      NULLIFY (dsblock)
     448              :                      CALL dbcsr_get_block_p(matrix=matrix_s(1 + i, 1)%matrix, &
     449       139101 :                                             row=irow, col=icol, block=dsblock, found=found)
     450       139101 :                      CPASSERT(found)
     451       139101 :                      fij(i) = 0.0_dp
     452              :                      ! short range
     453      1665702 :                      fi = -2.0_dp*SUM(pblock*dsblock*gcij)
     454       139101 :                      force(ikind)%rho_elec(i, atom_i) = force(ikind)%rho_elec(i, atom_i) + fi
     455       139101 :                      force(jkind)%rho_elec(i, atom_j) = force(jkind)%rho_elec(i, atom_j) - fi
     456       139101 :                      fij(i) = fij(i) + fi
     457              :                      ! long range
     458      1665702 :                      fi = -2.0_dp*gmij*SUM(pblock*dsblock)
     459       139101 :                      force(ikind)%rho_elec(i, atom_i) = force(ikind)%rho_elec(i, atom_i) + fi
     460       139101 :                      force(jkind)%rho_elec(i, atom_j) = force(jkind)%rho_elec(i, atom_j) - fi
     461       324569 :                      fij(i) = fij(i) + fi
     462              :                   END DO
     463              :                END IF
     464      4216938 :                DEALLOCATE (gcij)
     465              :             END DO
     466        20736 :             CALL dbcsr_iterator_stop(iter)
     467              :             ! stress tensor (needs recalculation of overlap integrals)
     468        20736 :             IF (do_gamma_stress) THEN
     469          286 :                DO ikind = 1, nkind
     470          738 :                   DO jkind = 1, nkind
     471          452 :                      iac = ikind + nkind*(jkind - 1)
     472          452 :                      IF (.NOT. ASSOCIATED(sap_int(iac)%alist)) CYCLE
     473              :                      ! atomic parameters
     474          332 :                      CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_atom_a)
     475          332 :                      CALL get_qs_kind(qs_kind_set(jkind), xtb_parameter=xtb_atom_b)
     476          332 :                      CALL get_xtb_atom_param(xtb_atom_a, lao=laoa, natorb=ni)
     477          332 :                      CALL get_xtb_atom_param(xtb_atom_b, lao=laob, natorb=nj)
     478         1908 :                      DO ia = 1, sap_int(iac)%nalist
     479         1384 :                         IF (.NOT. ASSOCIATED(sap_int(iac)%alist(ia)%clist)) CYCLE
     480         1366 :                         iatom = sap_int(iac)%alist(ia)%aatom
     481       119982 :                         DO ic = 1, sap_int(iac)%alist(ia)%nclist
     482       118164 :                            jatom = sap_int(iac)%alist(ia)%clist(ic)%catom
     483       472656 :                            rij = sap_int(iac)%alist(ia)%clist(ic)%rac
     484       472656 :                            dr = SQRT(SUM(rij(:)**2))
     485       119548 :                            IF (dr > 1.e-6_dp) THEN
     486       117472 :                               dsint => sap_int(iac)%alist(ia)%clist(ic)%acint
     487       469888 :                               ALLOCATE (gcij(ni, nj))
     488       705336 :                               DO i = 1, ni
     489      4489496 :                                  DO j = 1, nj
     490      3784160 :                                     la = laoa(i) + 1
     491      3784160 :                                     lb = laob(j) + 1
     492      4372024 :                                     gcij(i, j) = 0.5_dp*(gchrg(iatom, la, 1) + gchrg(jatom, lb, 1))
     493              :                                  END DO
     494              :                               END DO
     495       117472 :                               gmij = 0.5_dp*(gmcharge(iatom, 1) + gmcharge(jatom, 1))
     496       117472 :                               icol = MAX(iatom, jatom)
     497       117472 :                               irow = MIN(iatom, jatom)
     498       117472 :                               NULLIFY (pblock)
     499              :                               CALL dbcsr_get_block_p(matrix=matrix_p(1, 1)%matrix, &
     500       117472 :                                                      row=irow, col=icol, block=pblock, found=found)
     501       117472 :                               CPASSERT(found)
     502       117472 :                               fij = 0.0_dp
     503       469888 :                               DO i = 1, 3
     504              :                                  ! short/long range
     505       352416 :                                  IF (irow == iatom) THEN
     506      7931793 :                                     f1 = -2.0_dp*SUM(pblock*dsint(:, :, i)*gcij)
     507      7931793 :                                     f2 = -2.0_dp*gmij*SUM(pblock*dsint(:, :, i))
     508              :                                  ELSE
     509      5536401 :                                     f1 = -2.0_dp*SUM(TRANSPOSE(pblock)*dsint(:, :, i)*gcij)
     510      5536401 :                                     f2 = -2.0_dp*gmij*SUM(TRANSPOSE(pblock)*dsint(:, :, i))
     511              :                                  END IF
     512       469888 :                                  fij(i) = f1 + f2
     513              :                               END DO
     514       117472 :                               DEALLOCATE (gcij)
     515       117472 :                               fi = 1.0_dp
     516       117472 :                               IF (iatom == jatom) fi = 0.5_dp
     517       234944 :                               CALL virial_pair_force(virial%pv_virial, fi, fij, rij)
     518              :                            END IF
     519              :                         END DO
     520              :                      END DO
     521              :                   END DO
     522              :                END DO
     523              :             END IF
     524              :          ELSE
     525         2656 :             NULLIFY (n_list)
     526         2656 :             CALL get_qs_env(qs_env=qs_env, sab_orb=n_list)
     527         2656 :             CALL neighbor_list_iterator_create(nl_iterator, n_list)
     528      1963764 :             DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
     529              :                CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, &
     530      1961108 :                                       iatom=iatom, jatom=jatom, r=rij, cell=cellind)
     531              : 
     532      1961108 :                icol = MAX(iatom, jatom)
     533      1961108 :                irow = MIN(iatom, jatom)
     534              : 
     535      1961108 :                ic = cell_to_index(cellind(1), cellind(2), cellind(3))
     536      1961108 :                CPASSERT(ic > 0)
     537              : 
     538      1961108 :                NULLIFY (sblock)
     539              :                CALL dbcsr_get_block_p(matrix=matrix_s(1, ic)%matrix, &
     540      1961108 :                                       row=irow, col=icol, block=sblock, found=found)
     541      1961108 :                CPASSERT(found)
     542              : 
     543              :                ! atomic parameters
     544      1961108 :                CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_atom_a)
     545      1961108 :                CALL get_qs_kind(qs_kind_set(jkind), xtb_parameter=xtb_atom_b)
     546      1961108 :                CALL get_xtb_atom_param(xtb_atom_a, z=za, lao=laoa)
     547      1961108 :                CALL get_xtb_atom_param(xtb_atom_b, z=zb, lao=laob)
     548              : 
     549      1961108 :                ni = SIZE(sblock, 1)
     550      1961108 :                nj = SIZE(sblock, 2)
     551      7844432 :                ALLOCATE (gcij(ni, nj))
     552     11442495 :                DO i = 1, ni
     553     72925194 :                   DO j = 1, nj
     554     70964086 :                      IF (irow == iatom) THEN
     555     34957817 :                         la = laoa(i) + 1
     556     34957817 :                         lb = laob(j) + 1
     557     34957817 :                         gcij(i, j) = 0.5_dp*(gchrg(iatom, la, 1) + gchrg(jatom, lb, 1))
     558              :                      ELSE
     559     26524882 :                         la = laoa(j) + 1
     560     26524882 :                         lb = laob(i) + 1
     561     26524882 :                         gcij(i, j) = 0.5_dp*(gchrg(iatom, la, 1) + gchrg(jatom, lb, 1))
     562              :                      END IF
     563              :                   END DO
     564              :                END DO
     565      1961108 :                gmij = 0.5_dp*(gmcharge(iatom, 1) + gmcharge(jatom, 1))
     566      4109816 :                DO is = 1, SIZE(ks_matrix, 1)
     567      2148708 :                   NULLIFY (ksblock)
     568              :                   CALL dbcsr_get_block_p(matrix=ks_matrix(is, ic)%matrix, &
     569      2148708 :                                          row=irow, col=icol, block=ksblock, found=found)
     570      2148708 :                   CPASSERT(found)
     571    147117272 :                   ksblock = ksblock - gcij*sblock
     572    151227088 :                   ksblock = ksblock - gmij*sblock
     573              :                END DO
     574              : 
     575      1961108 :                IF (calculate_forces) THEN
     576        29400 :                   atom_i = atom_of_kind(iatom)
     577        29400 :                   atom_j = atom_of_kind(jatom)
     578        29400 :                   IF (irow /= iatom) THEN
     579        11957 :                      gmij = -gmij
     580       759507 :                      gcij = -gcij
     581              :                   END IF
     582        29400 :                   NULLIFY (pblock)
     583              :                   CALL dbcsr_get_block_p(matrix=matrix_p(1, ic)%matrix, &
     584        29400 :                                          row=irow, col=icol, block=pblock, found=found)
     585        29400 :                   CPASSERT(found)
     586       117600 :                   DO i = 1, 3
     587        88200 :                      NULLIFY (dsblock)
     588              :                      CALL dbcsr_get_block_p(matrix=matrix_s(1 + i, ic)%matrix, &
     589        88200 :                                             row=irow, col=icol, block=dsblock, found=found)
     590        88200 :                      CPASSERT(found)
     591        88200 :                      fij(i) = 0.0_dp
     592              :                      ! short range
     593      5846208 :                      fi = -2.0_dp*SUM(pblock*dsblock*gcij)
     594        88200 :                      force(ikind)%rho_elec(i, atom_i) = force(ikind)%rho_elec(i, atom_i) + fi
     595        88200 :                      force(jkind)%rho_elec(i, atom_j) = force(jkind)%rho_elec(i, atom_j) - fi
     596        88200 :                      fij(i) = fij(i) + fi
     597              :                      ! long range
     598      5846208 :                      fi = -2.0_dp*gmij*SUM(pblock*dsblock)
     599        88200 :                      force(ikind)%rho_elec(i, atom_i) = force(ikind)%rho_elec(i, atom_i) + fi
     600        88200 :                      force(jkind)%rho_elec(i, atom_j) = force(jkind)%rho_elec(i, atom_j) - fi
     601       205800 :                      fij(i) = fij(i) + fi
     602              :                   END DO
     603        29400 :                   IF (use_virial) THEN
     604        73828 :                      dr = SQRT(SUM(rij(:)**2))
     605        18457 :                      IF (dr > 1.e-6_dp) THEN
     606        18393 :                         fi = 1.0_dp
     607        18393 :                         IF (iatom == jatom) fi = 0.5_dp
     608        18393 :                         CALL virial_pair_force(virial%pv_virial, fi, fij, rij)
     609              :                      END IF
     610              :                   END IF
     611              :                END IF
     612      5885980 :                DEALLOCATE (gcij)
     613              : 
     614              :             END DO
     615         2656 :             CALL neighbor_list_iterator_release(nl_iterator)
     616              :          END IF
     617              : 
     618        23392 :          IF (calculate_forces .AND. SIZE(matrix_p, 1) == 2) THEN
     619          432 :             DO img = 1, nimg
     620              :                CALL dbcsr_add(matrix_p(1, img)%matrix, matrix_p(2, img)%matrix, &
     621          432 :                               alpha_scalar=1.0_dp, beta_scalar=-1.0_dp)
     622              :             END DO
     623              :          END IF
     624              :       END IF
     625              : 
     626        23898 :       IF (xtb_control%tb3_interaction) THEN
     627        23898 :          CALL get_qs_env(qs_env, nkind=nkind)
     628        95592 :          ALLOCATE (zeffk(nkind), xgamma(nkind))
     629        86816 :          DO ikind = 1, nkind
     630        62918 :             CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
     631        86816 :             CALL get_xtb_atom_param(xtb_kind, xgamma=xgamma(ikind), zeff=zeffk(ikind))
     632              :          END DO
     633              :          ! Diagonal 3rd order correction (DFTB3)
     634              :          CALL build_dftb3_diagonal(qs_env, ks_matrix, rho, mcharge, energy, xgamma, zeffk, &
     635        23898 :                                    sap_int, calculate_forces, just_energy)
     636        23898 :          DEALLOCATE (zeffk, xgamma)
     637              :       END IF
     638              : 
     639              :       ! QMMM
     640        23898 :       IF (qs_env%qmmm .AND. qs_env%qmmm_periodic) THEN
     641              :          CALL build_tb_coulomb_qmqm(qs_env, ks_matrix, rho, mcharge, energy, &
     642          862 :                                     calculate_forces, just_energy)
     643              :       END IF
     644              : 
     645        23898 :       IF (do_gamma_stress) THEN
     646           94 :          CALL release_sap_int(sap_int)
     647              :       END IF
     648              : 
     649        23898 :       CALL timestop(handle)
     650              : 
     651        47796 :    END SUBROUTINE build_xtb_coulomb
     652              : 
     653              : ! **************************************************************************************************
     654              : !> \brief  Computes the short-range gamma parameter from
     655              : !>         Nataga-Mishimoto-Ohno-Klopman formula for xTB
     656              : !>         WARNING: The xTB function (gamma - 1/r) has still an l-dependent longrange (1/r^3)
     657              : !>                  behaviour. We use a cutoff function to smoothly remove this part.
     658              : !>                  However, this will change energies and effect final results.
     659              : !>
     660              : !> \param gmat ...
     661              : !> \param rab ...
     662              : !> \param nla ...
     663              : !> \param kappaa ...
     664              : !> \param etaa ...
     665              : !> \param nlb ...
     666              : !> \param kappab ...
     667              : !> \param etab ...
     668              : !> \param kg ...
     669              : !> \param rcut ...
     670              : !> \par History
     671              : !>      10.2018 JGH
     672              : !> \version 1.1
     673              : ! **************************************************************************************************
     674      8816593 :    SUBROUTINE gamma_rab_sr(gmat, rab, nla, kappaa, etaa, nlb, kappab, etab, kg, rcut)
     675              :       REAL(dp), DIMENSION(:, :), INTENT(INOUT)           :: gmat
     676              :       REAL(dp), INTENT(IN)                               :: rab
     677              :       INTEGER, INTENT(IN)                                :: nla
     678              :       REAL(dp), DIMENSION(:), INTENT(IN)                 :: kappaa
     679              :       REAL(dp), INTENT(IN)                               :: etaa
     680              :       INTEGER, INTENT(IN)                                :: nlb
     681              :       REAL(dp), DIMENSION(:), INTENT(IN)                 :: kappab
     682              :       REAL(dp), INTENT(IN)                               :: etab, kg, rcut
     683              : 
     684              :       REAL(KIND=dp), PARAMETER                           :: rsmooth = 1.0_dp
     685              : 
     686              :       INTEGER                                            :: i, j
     687              :       REAL(KIND=dp)                                      :: fcut, r, rk, x
     688      8816593 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: eta
     689              : 
     690     35266372 :       ALLOCATE (eta(nla, nlb))
     691     47871578 :       eta = 0.0_dp
     692              : 
     693     22887653 :       DO j = 1, nlb
     694     47871578 :          DO i = 1, nla
     695     24983925 :             eta(i, j) = 1._dp/(etaa*(1._dp + kappaa(i))) + 1._dp/(etab*(1._dp + kappab(j)))
     696     39054985 :             eta(i, j) = 2._dp/eta(i, j)
     697              :          END DO
     698              :       END DO
     699              : 
     700     47871578 :       gmat = 0.0_dp
     701      8816593 :       IF (rab < 1.e-6_dp) THEN
     702              :          ! on site terms
     703       608195 :          gmat(:, :) = eta(:, :)
     704      8703828 :       ELSEIF (rab > rcut) THEN
     705              :          ! do nothing
     706              :       ELSE
     707      8703828 :          rk = rab**kg
     708     47263383 :          eta = eta**(-kg)
     709      8703828 :          IF (rab < rcut - rsmooth) THEN
     710              :             fcut = 1.0_dp
     711              :          ELSE
     712      1002675 :             r = rab - (rcut - rsmooth)
     713      1002675 :             x = r/rsmooth
     714      1002675 :             fcut = -6._dp*x**5 + 15._dp*x**4 - 10._dp*x**3 + 1._dp
     715              :          END IF
     716     47263383 :          gmat(:, :) = fcut*(1._dp/(rk + eta(:, :)))**(1._dp/kg) - fcut/rab
     717              :       END IF
     718              : 
     719      8816593 :       DEALLOCATE (eta)
     720              : 
     721      8816593 :    END SUBROUTINE gamma_rab_sr
     722              : 
     723              : ! **************************************************************************************************
     724              : !> \brief  Computes the derivative of the short-range gamma parameter from
     725              : !>         Nataga-Mishimoto-Ohno-Klopman formula for xTB
     726              : !>         WARNING: The xTB function (gamma - 1/r) has still an l-dependent longrange (1/r^3)
     727              : !>                  behaviour. We use a cutoff function to smoothly remove this part.
     728              : !>                  However, this will change energies and effect final results.
     729              : !>
     730              : !> \param dgmat ...
     731              : !> \param rab ...
     732              : !> \param nla ...
     733              : !> \param kappaa ...
     734              : !> \param etaa ...
     735              : !> \param nlb ...
     736              : !> \param kappab ...
     737              : !> \param etab ...
     738              : !> \param kg ...
     739              : !> \param rcut ...
     740              : !> \par History
     741              : !>      10.2018 JGH
     742              : !> \version 1.1
     743              : ! **************************************************************************************************
     744       364055 :    SUBROUTINE dgamma_rab_sr(dgmat, rab, nla, kappaa, etaa, nlb, kappab, etab, kg, rcut)
     745              :       REAL(dp), DIMENSION(:, :), INTENT(INOUT)           :: dgmat
     746              :       REAL(dp), INTENT(IN)                               :: rab
     747              :       INTEGER, INTENT(IN)                                :: nla
     748              :       REAL(dp), DIMENSION(:), INTENT(IN)                 :: kappaa
     749              :       REAL(dp), INTENT(IN)                               :: etaa
     750              :       INTEGER, INTENT(IN)                                :: nlb
     751              :       REAL(dp), DIMENSION(:), INTENT(IN)                 :: kappab
     752              :       REAL(dp), INTENT(IN)                               :: etab, kg, rcut
     753              : 
     754              :       REAL(KIND=dp), PARAMETER                           :: rsmooth = 1.0_dp
     755              : 
     756              :       INTEGER                                            :: i, j
     757              :       REAL(KIND=dp)                                      :: dfcut, fcut, r, rk, x
     758       364055 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: eta
     759              : 
     760      1456220 :       ALLOCATE (eta(nla, nlb))
     761              : 
     762       962268 :       DO j = 1, nlb
     763      2094090 :          DO i = 1, nla
     764      1131822 :             eta(i, j) = 1._dp/(etaa*(1._dp + kappaa(i))) + 1._dp/(etab*(1._dp + kappab(j)))
     765      1730035 :             eta(i, j) = 2._dp/eta(i, j)
     766              :          END DO
     767              :       END DO
     768              : 
     769       364055 :       IF (rab < 1.e-6) THEN
     770              :          ! on site terms
     771            0 :          dgmat(:, :) = 0.0_dp
     772       364055 :       ELSEIF (rab > rcut) THEN
     773            0 :          dgmat(:, :) = 0.0_dp
     774              :       ELSE
     775      2094090 :          eta = eta**(-kg)
     776       364055 :          rk = rab**kg
     777       364055 :          IF (rab < rcut - rsmooth) THEN
     778              :             fcut = 1.0_dp
     779              :             dfcut = 0.0_dp
     780              :          ELSE
     781        47855 :             r = rab - (rcut - rsmooth)
     782        47855 :             x = r/rsmooth
     783        47855 :             fcut = -6._dp*x**5 + 15._dp*x**4 - 10._dp*x**3 + 1._dp
     784        47855 :             dfcut = -30._dp*x**4 + 60._dp*x**3 - 30._dp*x**2
     785        47855 :             dfcut = dfcut/rsmooth
     786              :          END IF
     787      2094090 :          dgmat(:, :) = dfcut*(1._dp/(rk + eta(:, :)))**(1._dp/kg)
     788      2094090 :          dgmat(:, :) = dgmat(:, :) - dfcut/rab + fcut/rab**2
     789      2094090 :          dgmat(:, :) = dgmat(:, :) - fcut/(rk + eta(:, :))*(1._dp/(rk + eta(:, :)))**(1._dp/kg)*rk/rab
     790              :       END IF
     791              : 
     792       364055 :       DEALLOCATE (eta)
     793              : 
     794       364055 :    END SUBROUTINE dgamma_rab_sr
     795              : 
     796              : ! **************************************************************************************************
     797              : !> \brief ...
     798              : !> \param qs_env ...
     799              : !> \param sap_int ...
     800              : ! **************************************************************************************************
     801          100 :    SUBROUTINE xtb_dsint_list(qs_env, sap_int)
     802              : 
     803              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     804              :       TYPE(sap_int_type), DIMENSION(:), POINTER          :: sap_int
     805              : 
     806              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'xtb_dsint_list'
     807              : 
     808              :       INTEGER :: handle, i, iac, iatom, ikind, ilist, iset, jatom, jkind, jneighbor, jset, ldsab, &
     809              :          n1, n2, natorb_a, natorb_b, ncoa, ncob, nkind, nlist, nneighbor, nseta, nsetb, sgfa, sgfb
     810              :       INTEGER, DIMENSION(3)                              :: cell
     811          100 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, lb_max, lb_min, npgfa, &
     812          100 :                                                             npgfb, nsgfa, nsgfb
     813          100 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa, first_sgfb
     814              :       LOGICAL                                            :: defined
     815              :       REAL(KIND=dp)                                      :: dr
     816          100 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: owork
     817          100 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: oint, sint
     818              :       REAL(KIND=dp), DIMENSION(3)                        :: rij
     819          100 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: set_radius_a, set_radius_b
     820          100 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: rpgfa, rpgfb, scon_a, scon_b, zeta, zetb
     821              :       TYPE(clist_type), POINTER                          :: clist
     822              :       TYPE(dft_control_type), POINTER                    :: dft_control
     823          100 :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_list
     824              :       TYPE(gto_basis_set_type), POINTER                  :: basis_set_a, basis_set_b
     825              :       TYPE(neighbor_list_iterator_p_type), &
     826          100 :          DIMENSION(:), POINTER                           :: nl_iterator
     827              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     828          100 :          POINTER                                         :: sab_orb
     829          100 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     830              :       TYPE(xtb_atom_type), POINTER                       :: xtb_atom_a, xtb_atom_b
     831              : 
     832          100 :       CALL timeset(routineN, handle)
     833              : 
     834          100 :       CALL get_qs_env(qs_env=qs_env, nkind=nkind)
     835          100 :       CPASSERT(.NOT. ASSOCIATED(sap_int))
     836          806 :       ALLOCATE (sap_int(nkind*nkind))
     837          606 :       DO i = 1, nkind*nkind
     838          506 :          NULLIFY (sap_int(i)%alist, sap_int(i)%asort, sap_int(i)%aindex)
     839          606 :          sap_int(i)%nalist = 0
     840              :       END DO
     841              : 
     842              :       CALL get_qs_env(qs_env=qs_env, &
     843              :                       qs_kind_set=qs_kind_set, &
     844              :                       dft_control=dft_control, &
     845          100 :                       sab_orb=sab_orb)
     846              : 
     847              :       ! set up basis set lists
     848          510 :       ALLOCATE (basis_set_list(nkind))
     849          100 :       CALL basis_set_list_setup(basis_set_list, "ORB", qs_kind_set)
     850              : 
     851              :       ! loop over all atom pairs with a non-zero overlap (sab_orb)
     852          100 :       CALL neighbor_list_iterator_create(nl_iterator, sab_orb)
     853       119646 :       DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
     854              :          CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, iatom=iatom, &
     855              :                                 jatom=jatom, nlist=nlist, ilist=ilist, nnode=nneighbor, &
     856       119546 :                                 inode=jneighbor, cell=cell, r=rij)
     857       119546 :          iac = ikind + nkind*(jkind - 1)
     858              :          !
     859       119546 :          CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_atom_a)
     860       119546 :          CALL get_xtb_atom_param(xtb_atom_a, defined=defined, natorb=natorb_a)
     861       119546 :          IF (.NOT. defined .OR. natorb_a < 1) CYCLE
     862       119546 :          CALL get_qs_kind(qs_kind_set(jkind), xtb_parameter=xtb_atom_b)
     863       119546 :          CALL get_xtb_atom_param(xtb_atom_b, defined=defined, natorb=natorb_b)
     864       119546 :          IF (.NOT. defined .OR. natorb_b < 1) CYCLE
     865              : 
     866       478184 :          dr = SQRT(SUM(rij(:)**2))
     867              : 
     868              :          ! integral list
     869       119546 :          IF (.NOT. ASSOCIATED(sap_int(iac)%alist)) THEN
     870          359 :             sap_int(iac)%a_kind = ikind
     871          359 :             sap_int(iac)%p_kind = jkind
     872          359 :             sap_int(iac)%nalist = nlist
     873         2488 :             ALLOCATE (sap_int(iac)%alist(nlist))
     874         1770 :             DO i = 1, nlist
     875         1411 :                NULLIFY (sap_int(iac)%alist(i)%clist)
     876         1411 :                sap_int(iac)%alist(i)%aatom = 0
     877         1770 :                sap_int(iac)%alist(i)%nclist = 0
     878              :             END DO
     879              :          END IF
     880       119546 :          IF (.NOT. ASSOCIATED(sap_int(iac)%alist(ilist)%clist)) THEN
     881         1393 :             sap_int(iac)%alist(ilist)%aatom = iatom
     882         1393 :             sap_int(iac)%alist(ilist)%nclist = nneighbor
     883       132083 :             ALLOCATE (sap_int(iac)%alist(ilist)%clist(nneighbor))
     884       120939 :             DO i = 1, nneighbor
     885       120939 :                sap_int(iac)%alist(ilist)%clist(i)%catom = 0
     886              :             END DO
     887              :          END IF
     888       119546 :          clist => sap_int(iac)%alist(ilist)%clist(jneighbor)
     889       119546 :          clist%catom = jatom
     890       478184 :          clist%cell = cell
     891       478184 :          clist%rac = rij
     892       597730 :          ALLOCATE (clist%acint(natorb_a, natorb_b, 3))
     893       119546 :          NULLIFY (clist%achint)
     894     13692392 :          clist%acint = 0._dp
     895       119546 :          clist%nsgf_cnt = 0
     896       119546 :          NULLIFY (clist%sgf_list)
     897              : 
     898              :          ! overlap
     899       119546 :          basis_set_a => basis_set_list(ikind)%gto_basis_set
     900       119546 :          IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
     901       119546 :          basis_set_b => basis_set_list(jkind)%gto_basis_set
     902       119546 :          IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
     903              :          ! basis ikind
     904       119546 :          first_sgfa => basis_set_a%first_sgf
     905       119546 :          la_max => basis_set_a%lmax
     906       119546 :          la_min => basis_set_a%lmin
     907       119546 :          npgfa => basis_set_a%npgf
     908       119546 :          nseta = basis_set_a%nset
     909       119546 :          nsgfa => basis_set_a%nsgf_set
     910       119546 :          rpgfa => basis_set_a%pgf_radius
     911       119546 :          set_radius_a => basis_set_a%set_radius
     912       119546 :          scon_a => basis_set_a%scon
     913       119546 :          zeta => basis_set_a%zet
     914              :          ! basis jkind
     915       119546 :          first_sgfb => basis_set_b%first_sgf
     916       119546 :          lb_max => basis_set_b%lmax
     917       119546 :          lb_min => basis_set_b%lmin
     918       119546 :          npgfb => basis_set_b%npgf
     919       119546 :          nsetb = basis_set_b%nset
     920       119546 :          nsgfb => basis_set_b%nsgf_set
     921       119546 :          rpgfb => basis_set_b%pgf_radius
     922       119546 :          set_radius_b => basis_set_b%set_radius
     923       119546 :          scon_b => basis_set_b%scon
     924       119546 :          zetb => basis_set_b%zet
     925              : 
     926       119546 :          ldsab = get_memory_usage(qs_kind_set, "ORB", "ORB")
     927       956368 :          ALLOCATE (oint(ldsab, ldsab, 4), owork(ldsab, ldsab))
     928       597730 :          ALLOCATE (sint(natorb_a, natorb_b, 4))
     929     18216674 :          sint = 0.0_dp
     930              : 
     931       400816 :          DO iset = 1, nseta
     932       281270 :             ncoa = npgfa(iset)*ncoset(la_max(iset))
     933       281270 :             n1 = npgfa(iset)*(ncoset(la_max(iset)) - ncoset(la_min(iset) - 1))
     934       281270 :             sgfa = first_sgfa(1, iset)
     935      1080726 :             DO jset = 1, nsetb
     936       679910 :                IF (set_radius_a(iset) + set_radius_b(jset) < dr) CYCLE
     937       473228 :                ncob = npgfb(jset)*ncoset(lb_max(jset))
     938       473228 :                n2 = npgfb(jset)*(ncoset(lb_max(jset)) - ncoset(lb_min(jset) - 1))
     939       473228 :                sgfb = first_sgfb(1, jset)
     940              :                CALL overlap_ab(la_max(iset), la_min(iset), npgfa(iset), rpgfa(:, iset), zeta(:, iset), &
     941              :                                lb_max(jset), lb_min(jset), npgfb(jset), rpgfb(:, jset), zetb(:, jset), &
     942       473228 :                                rij, sab=oint(:, :, 1), dab=oint(:, :, 2:4))
     943              :                ! Contraction
     944      2647410 :                DO i = 1, 4
     945              :                   CALL contraction(oint(:, :, i), owork, ca=scon_a(:, sgfa:), na=n1, ma=nsgfa(iset), &
     946      1892912 :                                    cb=scon_b(:, sgfb:), nb=n2, mb=nsgfb(jset), fscale=1.0_dp, trans=.FALSE.)
     947              :                   CALL block_add("IN", owork, nsgfa(iset), nsgfb(jset), sint(:, :, i), &
     948      2572822 :                                  sgfa, sgfb, trans=.FALSE.)
     949              :                END DO
     950              :             END DO
     951              :          END DO
     952              :          ! update dS/dR matrix
     953     13692392 :          clist%acint(1:natorb_a, 1:natorb_b, 1:3) = sint(1:natorb_a, 1:natorb_b, 2:4)
     954              : 
     955       358738 :          DEALLOCATE (oint, owork, sint)
     956              : 
     957              :       END DO
     958          100 :       CALL neighbor_list_iterator_release(nl_iterator)
     959              : 
     960          100 :       DEALLOCATE (basis_set_list)
     961              : 
     962          100 :       CALL timestop(handle)
     963              : 
     964          200 :    END SUBROUTINE xtb_dsint_list
     965              : 
     966     17810969 : END MODULE xtb_coulomb
     967              : 
        

Generated by: LCOV version 2.0-1