LCOV - code coverage report
Current view: top level - src - xtb_ehess.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:2fce0f8) Lines: 144 151 95.4 %
Date: 2024-12-21 06:28:57 Functions: 2 2 100.0 %

          Line data    Source code
       1             : !--------------------------------------------------------------------------------------------------!
       2             : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3             : !   Copyright 2000-2024 CP2K developers group <https://cp2k.org>                                   !
       4             : !                                                                                                  !
       5             : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6             : !--------------------------------------------------------------------------------------------------!
       7             : 
       8             : ! **************************************************************************************************
       9             : !> \brief Calculation of Coulomb Hessian contributions in xTB
      10             : !> \author JGH
      11             : ! **************************************************************************************************
      12             : MODULE xtb_ehess
      13             :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      14             :                                               get_atomic_kind_set
      15             :    USE cell_types,                      ONLY: cell_type,&
      16             :                                               get_cell,&
      17             :                                               pbc
      18             :    USE cp_control_types,                ONLY: dft_control_type,&
      19             :                                               xtb_control_type
      20             :    USE cp_dbcsr_api,                    ONLY: dbcsr_get_block_p,&
      21             :                                               dbcsr_iterator_blocks_left,&
      22             :                                               dbcsr_iterator_next_block,&
      23             :                                               dbcsr_iterator_start,&
      24             :                                               dbcsr_iterator_stop,&
      25             :                                               dbcsr_iterator_type,&
      26             :                                               dbcsr_p_type
      27             :    USE distribution_1d_types,           ONLY: distribution_1d_type
      28             :    USE ewald_environment_types,         ONLY: ewald_env_get,&
      29             :                                               ewald_environment_type
      30             :    USE ewald_methods_tb,                ONLY: tb_ewald_overlap,&
      31             :                                               tb_spme_evaluate
      32             :    USE ewald_pw_types,                  ONLY: ewald_pw_type
      33             :    USE kinds,                           ONLY: dp
      34             :    USE mathconstants,                   ONLY: oorootpi,&
      35             :                                               pi
      36             :    USE message_passing,                 ONLY: mp_para_env_type
      37             :    USE particle_types,                  ONLY: particle_type
      38             :    USE pw_poisson_types,                ONLY: do_ewald_ewald,&
      39             :                                               do_ewald_none,&
      40             :                                               do_ewald_pme,&
      41             :                                               do_ewald_spme
      42             :    USE qs_environment_types,            ONLY: get_qs_env,&
      43             :                                               qs_environment_type
      44             :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      45             :                                               qs_kind_type
      46             :    USE qs_neighbor_list_types,          ONLY: get_iterator_info,&
      47             :                                               neighbor_list_iterate,&
      48             :                                               neighbor_list_iterator_create,&
      49             :                                               neighbor_list_iterator_p_type,&
      50             :                                               neighbor_list_iterator_release,&
      51             :                                               neighbor_list_set_p_type
      52             :    USE virial_types,                    ONLY: virial_type
      53             :    USE xtb_coulomb,                     ONLY: gamma_rab_sr
      54             :    USE xtb_types,                       ONLY: get_xtb_atom_param,&
      55             :                                               xtb_atom_type
      56             : #include "./base/base_uses.f90"
      57             : 
      58             :    IMPLICIT NONE
      59             : 
      60             :    PRIVATE
      61             : 
      62             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'xtb_ehess'
      63             : 
      64             :    PUBLIC :: xtb_coulomb_hessian
      65             : 
      66             : CONTAINS
      67             : 
      68             : ! **************************************************************************************************
      69             : !> \brief ...
      70             : !> \param qs_env ...
      71             : !> \param ks_matrix ...
      72             : !> \param charges1 ...
      73             : !> \param mcharge1 ...
      74             : !> \param mcharge ...
      75             : ! **************************************************************************************************
      76         134 :    SUBROUTINE xtb_coulomb_hessian(qs_env, ks_matrix, charges1, mcharge1, mcharge)
      77             : 
      78             :       TYPE(qs_environment_type), POINTER                 :: qs_env
      79             :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ks_matrix
      80             :       REAL(dp), DIMENSION(:, :)                          :: charges1
      81             :       REAL(dp), DIMENSION(:)                             :: mcharge1, mcharge
      82             : 
      83             :       CHARACTER(len=*), PARAMETER :: routineN = 'xtb_coulomb_hessian'
      84             : 
      85             :       INTEGER :: blk, ewald_type, handle, i, ia, iatom, icol, ikind, irow, is, j, jatom, jkind, &
      86             :          la, lb, lmaxa, lmaxb, natom, natorb_a, natorb_b, ni, nj, nkind, nmat, za, zb
      87         134 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: kind_of
      88             :       INTEGER, DIMENSION(25)                             :: laoa, laob
      89             :       INTEGER, DIMENSION(3)                              :: cellind, periodic
      90             :       LOGICAL                                            :: defined, do_ewald, found
      91             :       REAL(KIND=dp)                                      :: alpha, deth, dr, etaa, etab, gmij, kg, &
      92             :                                                             rcut, rcuta, rcutb
      93         134 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: xgamma
      94         134 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: gammab, gcij, gmcharge
      95         134 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: gchrg
      96             :       REAL(KIND=dp), DIMENSION(3)                        :: rij
      97             :       REAL(KIND=dp), DIMENSION(5)                        :: kappaa, kappab
      98         134 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: ksblock, sblock
      99         134 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     100             :       TYPE(cell_type), POINTER                           :: cell
     101             :       TYPE(dbcsr_iterator_type)                          :: iter
     102         134 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s
     103             :       TYPE(dft_control_type), POINTER                    :: dft_control
     104             :       TYPE(distribution_1d_type), POINTER                :: local_particles
     105             :       TYPE(ewald_environment_type), POINTER              :: ewald_env
     106             :       TYPE(ewald_pw_type), POINTER                       :: ewald_pw
     107             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     108             :       TYPE(neighbor_list_iterator_p_type), &
     109         134 :          DIMENSION(:), POINTER                           :: nl_iterator
     110             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     111         134 :          POINTER                                         :: n_list
     112         134 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     113         134 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     114             :       TYPE(virial_type), POINTER                         :: virial
     115             :       TYPE(xtb_atom_type), POINTER                       :: xtb_atom_a, xtb_atom_b, xtb_kind
     116             :       TYPE(xtb_control_type), POINTER                    :: xtb_control
     117             : 
     118         134 :       CALL timeset(routineN, handle)
     119             : 
     120             :       CALL get_qs_env(qs_env, &
     121             :                       matrix_s_kp=matrix_s, &
     122             :                       qs_kind_set=qs_kind_set, &
     123             :                       particle_set=particle_set, &
     124             :                       cell=cell, &
     125         134 :                       dft_control=dft_control)
     126             : 
     127         134 :       xtb_control => dft_control%qs_control%xtb_control
     128             : 
     129         134 :       IF (dft_control%nimages /= 1) THEN
     130           0 :          CPABORT("No kpoints allowed in xTB response calculation")
     131             :       END IF
     132             : 
     133         134 :       CALL get_qs_env(qs_env, nkind=nkind, natom=natom)
     134         134 :       nmat = 1
     135         402 :       ALLOCATE (gchrg(natom, 5, nmat))
     136       10708 :       gchrg = 0._dp
     137         402 :       ALLOCATE (gmcharge(natom, nmat))
     138        2222 :       gmcharge = 0._dp
     139             : 
     140             :       ! short range contribution (gamma)
     141             :       ! loop over all atom pairs (sab_xtbe)
     142         134 :       kg = xtb_control%kg
     143         134 :       NULLIFY (n_list)
     144         134 :       CALL get_qs_env(qs_env=qs_env, sab_xtbe=n_list)
     145         134 :       CALL neighbor_list_iterator_create(nl_iterator, n_list)
     146      197967 :       DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
     147             :          CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, &
     148      197833 :                                 iatom=iatom, jatom=jatom, r=rij, cell=cellind)
     149      197833 :          CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_atom_a)
     150      197833 :          CALL get_xtb_atom_param(xtb_atom_a, defined=defined, natorb=natorb_a)
     151      197833 :          IF (.NOT. defined .OR. natorb_a < 1) CYCLE
     152      197833 :          CALL get_qs_kind(qs_kind_set(jkind), xtb_parameter=xtb_atom_b)
     153      197833 :          CALL get_xtb_atom_param(xtb_atom_b, defined=defined, natorb=natorb_b)
     154      197833 :          IF (.NOT. defined .OR. natorb_b < 1) CYCLE
     155             :          ! atomic parameters
     156      197833 :          CALL get_xtb_atom_param(xtb_atom_a, eta=etaa, lmax=lmaxa, kappa=kappaa, rcut=rcuta)
     157      197833 :          CALL get_xtb_atom_param(xtb_atom_b, eta=etab, lmax=lmaxb, kappa=kappab, rcut=rcutb)
     158             :          ! gamma matrix
     159      197833 :          ni = lmaxa + 1
     160      197833 :          nj = lmaxb + 1
     161      791332 :          ALLOCATE (gammab(ni, nj))
     162      197833 :          rcut = rcuta + rcutb
     163      791332 :          dr = SQRT(SUM(rij(:)**2))
     164      197833 :          CALL gamma_rab_sr(gammab, dr, ni, kappaa, etaa, nj, kappab, etab, kg, rcut)
     165     2010292 :          gchrg(iatom, 1:ni, 1) = gchrg(iatom, 1:ni, 1) + MATMUL(gammab, charges1(jatom, 1:nj))
     166      197833 :          IF (iatom /= jatom) THEN
     167     2288907 :             gchrg(jatom, 1:nj, 1) = gchrg(jatom, 1:nj, 1) + MATMUL(charges1(iatom, 1:ni), gammab)
     168             :          END IF
     169      791332 :          DEALLOCATE (gammab)
     170             :       END DO
     171         134 :       CALL neighbor_list_iterator_release(nl_iterator)
     172             : 
     173             :       ! 1/R contribution
     174             : 
     175         134 :       IF (xtb_control%coulomb_lr) THEN
     176         134 :          do_ewald = xtb_control%do_ewald
     177         134 :          IF (do_ewald) THEN
     178             :             ! Ewald sum
     179          54 :             NULLIFY (ewald_env, ewald_pw)
     180          54 :             NULLIFY (virial)
     181             :             CALL get_qs_env(qs_env=qs_env, &
     182          54 :                             ewald_env=ewald_env, ewald_pw=ewald_pw)
     183          54 :             CALL get_cell(cell=cell, periodic=periodic, deth=deth)
     184          54 :             CALL ewald_env_get(ewald_env, alpha=alpha, ewald_type=ewald_type)
     185          54 :             CALL get_qs_env(qs_env=qs_env, sab_tbe=n_list)
     186          54 :             CALL tb_ewald_overlap(gmcharge, mcharge1, alpha, n_list, virial, .FALSE.)
     187           0 :             SELECT CASE (ewald_type)
     188             :             CASE DEFAULT
     189           0 :                CPABORT("Invalid Ewald type")
     190             :             CASE (do_ewald_none)
     191           0 :                CPABORT("Not allowed with DFTB")
     192             :             CASE (do_ewald_ewald)
     193           0 :                CPABORT("Standard Ewald not implemented in DFTB")
     194             :             CASE (do_ewald_pme)
     195           0 :                CPABORT("PME not implemented in DFTB")
     196             :             CASE (do_ewald_spme)
     197             :                CALL tb_spme_evaluate(ewald_env, ewald_pw, particle_set, cell, &
     198          54 :                                      gmcharge, mcharge1, .FALSE., virial, .FALSE.)
     199             :             END SELECT
     200             :          ELSE
     201             :             ! direct sum
     202             :             CALL get_qs_env(qs_env=qs_env, &
     203          80 :                             local_particles=local_particles)
     204         280 :             DO ikind = 1, SIZE(local_particles%n_el)
     205         420 :                DO ia = 1, local_particles%n_el(ikind)
     206         140 :                   iatom = local_particles%list(ikind)%array(ia)
     207         520 :                   DO jatom = 1, iatom - 1
     208         720 :                      rij = particle_set(iatom)%r - particle_set(jatom)%r
     209         720 :                      rij = pbc(rij, cell)
     210         720 :                      dr = SQRT(SUM(rij(:)**2))
     211         320 :                      IF (dr > 1.e-6_dp) THEN
     212         180 :                         gmcharge(iatom, 1) = gmcharge(iatom, 1) + mcharge1(jatom)/dr
     213         180 :                         gmcharge(jatom, 1) = gmcharge(jatom, 1) + mcharge1(iatom)/dr
     214             :                      END IF
     215             :                   END DO
     216             :                END DO
     217             :             END DO
     218             :          END IF
     219             :       END IF
     220             : 
     221             :       ! global sum of gamma*p arrays
     222         134 :       CALL get_qs_env(qs_env=qs_env, para_env=para_env)
     223         134 :       CALL para_env%sum(gmcharge(:, 1))
     224         134 :       CALL para_env%sum(gchrg(:, :, 1))
     225             : 
     226         134 :       IF (xtb_control%coulomb_lr) THEN
     227         134 :          IF (do_ewald) THEN
     228             :             ! add self charge interaction and background charge contribution
     229        1728 :             gmcharge(:, 1) = gmcharge(:, 1) - 2._dp*alpha*oorootpi*mcharge1(:)
     230         102 :             IF (ANY(periodic(:) == 1)) THEN
     231        1648 :                gmcharge(:, 1) = gmcharge(:, 1) - pi/alpha**2/deth
     232             :             END IF
     233             :          END IF
     234             :       END IF
     235             : 
     236         134 :       CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set)
     237         134 :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
     238             : 
     239             :       ! no k-points; all matrices have been transformed to periodic bsf
     240         134 :       CALL dbcsr_iterator_start(iter, matrix_s(1, 1)%matrix)
     241       37680 :       DO WHILE (dbcsr_iterator_blocks_left(iter))
     242       37546 :          CALL dbcsr_iterator_next_block(iter, irow, icol, sblock, blk)
     243       37546 :          ikind = kind_of(irow)
     244       37546 :          jkind = kind_of(icol)
     245             : 
     246             :          ! atomic parameters
     247       37546 :          CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_atom_a)
     248       37546 :          CALL get_qs_kind(qs_kind_set(jkind), xtb_parameter=xtb_atom_b)
     249       37546 :          CALL get_xtb_atom_param(xtb_atom_a, z=za, lao=laoa)
     250       37546 :          CALL get_xtb_atom_param(xtb_atom_b, z=zb, lao=laob)
     251             : 
     252       37546 :          ni = SIZE(sblock, 1)
     253       37546 :          nj = SIZE(sblock, 2)
     254      150184 :          ALLOCATE (gcij(ni, nj))
     255      154096 :          DO i = 1, ni
     256      420500 :             DO j = 1, nj
     257      266404 :                la = laoa(i) + 1
     258      266404 :                lb = laob(j) + 1
     259      382954 :                gcij(i, j) = gchrg(irow, la, 1) + gchrg(icol, lb, 1)
     260             :             END DO
     261             :          END DO
     262       37546 :          gmij = gmcharge(irow, 1) + gmcharge(icol, 1)
     263       75092 :          DO is = 1, SIZE(ks_matrix)
     264       37546 :             NULLIFY (ksblock)
     265             :             CALL dbcsr_get_block_p(matrix=ks_matrix(is)%matrix, &
     266       37546 :                                    row=irow, col=icol, block=ksblock, found=found)
     267       37546 :             CPASSERT(found)
     268      737190 :             ksblock = ksblock - gcij*sblock
     269      812282 :             ksblock = ksblock - gmij*sblock
     270             :          END DO
     271      112772 :          DEALLOCATE (gcij)
     272             :       END DO
     273         134 :       CALL dbcsr_iterator_stop(iter)
     274             : 
     275         134 :       IF (xtb_control%tb3_interaction) THEN
     276         134 :          CALL get_qs_env(qs_env, nkind=nkind)
     277         402 :          ALLOCATE (xgamma(nkind))
     278         466 :          DO ikind = 1, nkind
     279         332 :             CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
     280         466 :             CALL get_xtb_atom_param(xtb_kind, xgamma=xgamma(ikind))
     281             :          END DO
     282             :          ! Diagonal 3rd order correction (DFTB3)
     283         134 :          CALL dftb3_diagonal_hessian(qs_env, ks_matrix, mcharge, mcharge1, xgamma)
     284         134 :          DEALLOCATE (xgamma)
     285             :       END IF
     286             : 
     287         134 :       IF (qs_env%qmmm .AND. qs_env%qmmm_periodic) THEN
     288           0 :          CPABORT("QMMM not available in xTB response calculations")
     289             :       END IF
     290             : 
     291         134 :       DEALLOCATE (gmcharge, gchrg)
     292             : 
     293         134 :       CALL timestop(handle)
     294             : 
     295         402 :    END SUBROUTINE xtb_coulomb_hessian
     296             : 
     297             : ! **************************************************************************************************
     298             : !> \brief ...
     299             : !> \param qs_env ...
     300             : !> \param ks_matrix ...
     301             : !> \param mcharge ...
     302             : !> \param mcharge1 ...
     303             : !> \param xgamma ...
     304             : ! **************************************************************************************************
     305         134 :    SUBROUTINE dftb3_diagonal_hessian(qs_env, ks_matrix, mcharge, mcharge1, xgamma)
     306             : 
     307             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     308             :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ks_matrix
     309             :       REAL(dp), DIMENSION(:)                             :: mcharge, mcharge1, xgamma
     310             : 
     311             :       CHARACTER(len=*), PARAMETER :: routineN = 'dftb3_diagonal_hessian'
     312             : 
     313             :       INTEGER                                            :: blk, handle, icol, ikind, irow, is, jkind
     314         134 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: kind_of
     315             :       LOGICAL                                            :: found
     316             :       REAL(KIND=dp)                                      :: gmij, ui, uj
     317         134 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: ksblock, sblock
     318         134 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     319             :       TYPE(dbcsr_iterator_type)                          :: iter
     320         134 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s
     321         134 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     322             : 
     323         134 :       CALL timeset(routineN, handle)
     324             : 
     325         134 :       CALL get_qs_env(qs_env=qs_env, matrix_s_kp=matrix_s)
     326         134 :       CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set)
     327         134 :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
     328             :       ! no k-points; all matrices have been transformed to periodic bsf
     329         134 :       CALL dbcsr_iterator_start(iter, matrix_s(1, 1)%matrix)
     330       37680 :       DO WHILE (dbcsr_iterator_blocks_left(iter))
     331       37546 :          CALL dbcsr_iterator_next_block(iter, irow, icol, sblock, blk)
     332       37546 :          ikind = kind_of(irow)
     333       37546 :          ui = xgamma(ikind)
     334       37546 :          jkind = kind_of(icol)
     335       37546 :          uj = xgamma(jkind)
     336       37546 :          gmij = ui*mcharge(irow)*mcharge1(irow) + uj*mcharge(icol)*mcharge1(icol)
     337       75226 :          DO is = 1, SIZE(ks_matrix)
     338       37546 :             NULLIFY (ksblock)
     339             :             CALL dbcsr_get_block_p(matrix=ks_matrix(is)%matrix, &
     340       37546 :                                    row=irow, col=icol, block=ksblock, found=found)
     341       37546 :             CPASSERT(found)
     342      812282 :             ksblock = ksblock + gmij*sblock
     343             :          END DO
     344             :       END DO
     345         134 :       CALL dbcsr_iterator_stop(iter)
     346             : 
     347         134 :       CALL timestop(handle)
     348             : 
     349         268 :    END SUBROUTINE dftb3_diagonal_hessian
     350             : 
     351      391031 : END MODULE xtb_ehess
     352             : 

Generated by: LCOV version 1.15