LCOV - code coverage report
Current view: top level - src - qs_dftb_matrices.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:262480d) Lines: 446 466 95.7 %
Date: 2024-11-22 07:00:40 Functions: 5 5 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 Overlap and Hamiltonian matrices in DFTB
      10             : !> \author JGH
      11             : ! **************************************************************************************************
      12             : MODULE qs_dftb_matrices
      13             :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      14             :                                               get_atomic_kind,&
      15             :                                               get_atomic_kind_set
      16             :    USE atprop_types,                    ONLY: atprop_array_init,&
      17             :                                               atprop_type
      18             :    USE block_p_types,                   ONLY: block_p_type
      19             :    USE cp_control_types,                ONLY: dft_control_type,&
      20             :                                               dftb_control_type
      21             :    USE cp_dbcsr_api,                    ONLY: &
      22             :         dbcsr_add, dbcsr_convert_offsets_to_sizes, dbcsr_copy, dbcsr_create, &
      23             :         dbcsr_distribution_type, dbcsr_dot, dbcsr_finalize, dbcsr_get_block_p, dbcsr_multiply, &
      24             :         dbcsr_p_type, dbcsr_type, dbcsr_type_antisymmetric, dbcsr_type_symmetric
      25             :    USE cp_dbcsr_cp2k_link,              ONLY: cp_dbcsr_alloc_block_from_nbl
      26             :    USE cp_dbcsr_operations,             ONLY: dbcsr_allocate_matrix_set
      27             :    USE cp_dbcsr_output,                 ONLY: cp_dbcsr_write_sparse_matrix
      28             :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      29             :                                               cp_logger_type
      30             :    USE cp_output_handling,              ONLY: cp_p_file,&
      31             :                                               cp_print_key_finished_output,&
      32             :                                               cp_print_key_should_output,&
      33             :                                               cp_print_key_unit_nr
      34             :    USE efield_tb_methods,               ONLY: efield_tb_matrix
      35             :    USE input_section_types,             ONLY: section_vals_get_subs_vals,&
      36             :                                               section_vals_type,&
      37             :                                               section_vals_val_get
      38             :    USE kinds,                           ONLY: default_string_length,&
      39             :                                               dp
      40             :    USE kpoint_types,                    ONLY: get_kpoint_info,&
      41             :                                               kpoint_type
      42             :    USE message_passing,                 ONLY: mp_para_env_type
      43             :    USE mulliken,                        ONLY: mulliken_charges
      44             :    USE particle_methods,                ONLY: get_particle_set
      45             :    USE particle_types,                  ONLY: particle_type
      46             :    USE qs_dftb_coulomb,                 ONLY: build_dftb_coulomb
      47             :    USE qs_dftb_types,                   ONLY: qs_dftb_atom_type,&
      48             :                                               qs_dftb_pairpot_type
      49             :    USE qs_dftb_utils,                   ONLY: compute_block_sk,&
      50             :                                               get_dftb_atom_param,&
      51             :                                               iptr,&
      52             :                                               urep_egr
      53             :    USE qs_energy_types,                 ONLY: qs_energy_type
      54             :    USE qs_environment_types,            ONLY: get_qs_env,&
      55             :                                               qs_environment_type
      56             :    USE qs_force_types,                  ONLY: qs_force_type
      57             :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      58             :                                               get_qs_kind_set,&
      59             :                                               qs_kind_type
      60             :    USE qs_ks_types,                     ONLY: get_ks_env,&
      61             :                                               qs_ks_env_type,&
      62             :                                               set_ks_env
      63             :    USE qs_mo_types,                     ONLY: get_mo_set,&
      64             :                                               mo_set_type
      65             :    USE qs_neighbor_list_types,          ONLY: get_iterator_info,&
      66             :                                               neighbor_list_iterate,&
      67             :                                               neighbor_list_iterator_create,&
      68             :                                               neighbor_list_iterator_p_type,&
      69             :                                               neighbor_list_iterator_release,&
      70             :                                               neighbor_list_set_p_type
      71             :    USE qs_rho_types,                    ONLY: qs_rho_get,&
      72             :                                               qs_rho_type
      73             :    USE virial_methods,                  ONLY: virial_pair_force
      74             :    USE virial_types,                    ONLY: virial_type
      75             : #include "./base/base_uses.f90"
      76             : 
      77             :    IMPLICIT NONE
      78             : 
      79             :    INTEGER, DIMENSION(16), PARAMETER        :: orbptr = (/0, 1, 1, 1, &
      80             :                                                           2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3/)
      81             : 
      82             :    PRIVATE
      83             : 
      84             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_dftb_matrices'
      85             : 
      86             :    PUBLIC :: build_dftb_matrices, build_dftb_ks_matrix, build_dftb_overlap
      87             : 
      88             : CONTAINS
      89             : 
      90             : ! **************************************************************************************************
      91             : !> \brief ...
      92             : !> \param qs_env ...
      93             : !> \param para_env ...
      94             : !> \param calculate_forces ...
      95             : ! **************************************************************************************************
      96        2822 :    SUBROUTINE build_dftb_matrices(qs_env, para_env, calculate_forces)
      97             : 
      98             :       TYPE(qs_environment_type), POINTER                 :: qs_env
      99             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     100             :       LOGICAL, INTENT(IN)                                :: calculate_forces
     101             : 
     102             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'build_dftb_matrices'
     103             : 
     104             :       INTEGER :: after, atom_a, atom_b, handle, i, iatom, ic, icol, ikind, img, irow, iw, jatom, &
     105             :          jkind, l1, l2, la, lb, llm, lmaxi, lmaxj, m, n1, n2, n_urpoly, natorb_a, natorb_b, &
     106             :          nderivatives, ngrd, ngrdcut, nimg, nkind, spdim
     107        2822 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind
     108             :       INTEGER, DIMENSION(3)                              :: cell
     109        2822 :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index
     110             :       LOGICAL                                            :: defined, found, omit_headers, use_virial
     111             :       REAL(KIND=dp)                                      :: ddr, dgrd, dr, erep, erepij, f0, foab, &
     112             :                                                             fow, s_cut, urep_cut
     113             :       REAL(KIND=dp), DIMENSION(0:3)                      :: eta_a, eta_b, skself
     114             :       REAL(KIND=dp), DIMENSION(10)                       :: urep
     115             :       REAL(KIND=dp), DIMENSION(2)                        :: surr
     116             :       REAL(KIND=dp), DIMENSION(3)                        :: drij, force_ab, force_rr, force_w, rij, &
     117             :                                                             srep
     118        5644 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: dfblock, dsblock, fblock, fmatij, &
     119        2822 :                                                             fmatji, pblock, sblock, scoeff, &
     120        2822 :                                                             smatij, smatji, spxr, wblock
     121        2822 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     122             :       TYPE(atprop_type), POINTER                         :: atprop
     123       11288 :       TYPE(block_p_type), DIMENSION(2:4)                 :: dsblocks
     124             :       TYPE(cp_logger_type), POINTER                      :: logger
     125        2822 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_h, matrix_p, matrix_s, matrix_w
     126             :       TYPE(dft_control_type), POINTER                    :: dft_control
     127             :       TYPE(dftb_control_type), POINTER                   :: dftb_control
     128             :       TYPE(kpoint_type), POINTER                         :: kpoints
     129             :       TYPE(neighbor_list_iterator_p_type), &
     130        2822 :          DIMENSION(:), POINTER                           :: nl_iterator
     131             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     132        2822 :          POINTER                                         :: sab_orb
     133        2822 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     134             :       TYPE(qs_dftb_atom_type), POINTER                   :: dftb_kind_a, dftb_kind_b
     135             :       TYPE(qs_dftb_pairpot_type), DIMENSION(:, :), &
     136        2822 :          POINTER                                         :: dftb_potential
     137             :       TYPE(qs_dftb_pairpot_type), POINTER                :: dftb_param_ij, dftb_param_ji
     138             :       TYPE(qs_energy_type), POINTER                      :: energy
     139        2822 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     140        2822 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     141             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     142             :       TYPE(qs_rho_type), POINTER                         :: rho
     143             :       TYPE(virial_type), POINTER                         :: virial
     144             : 
     145        2822 :       CALL timeset(routineN, handle)
     146             : 
     147             :       ! set pointers
     148        2822 :       iptr = 0
     149       14110 :       DO la = 0, 3
     150       59262 :          DO lb = 0, 3
     151       45152 :             llm = 0
     152      197540 :             DO l1 = 0, MAX(la, lb)
     153      412012 :                DO l2 = 0, MIN(l1, la, lb)
     154      702678 :                   DO m = 0, l2
     155      335818 :                      llm = llm + 1
     156      561578 :                      iptr(l1, l2, m, la, lb) = llm
     157             :                   END DO
     158             :                END DO
     159             :             END DO
     160             :          END DO
     161             :       END DO
     162             : 
     163        2822 :       NULLIFY (logger, virial, atprop)
     164        2822 :       logger => cp_get_default_logger()
     165             : 
     166        2822 :       NULLIFY (matrix_h, matrix_s, matrix_p, matrix_w, atomic_kind_set, &
     167        2822 :                qs_kind_set, sab_orb, ks_env)
     168             : 
     169             :       CALL get_qs_env(qs_env=qs_env, &
     170             :                       energy=energy, &
     171             :                       atomic_kind_set=atomic_kind_set, &
     172             :                       qs_kind_set=qs_kind_set, &
     173             :                       matrix_h_kp=matrix_h, &
     174             :                       matrix_s_kp=matrix_s, &
     175             :                       atprop=atprop, &
     176             :                       dft_control=dft_control, &
     177        2822 :                       ks_env=ks_env)
     178             : 
     179        2822 :       dftb_control => dft_control%qs_control%dftb_control
     180        2822 :       nimg = dft_control%nimages
     181             :       ! Allocate the overlap and Hamiltonian matrix
     182        2822 :       CALL get_qs_env(qs_env=qs_env, sab_orb=sab_orb)
     183        2822 :       nderivatives = 0
     184        2822 :       IF (dftb_control%self_consistent .AND. calculate_forces) nderivatives = 1
     185        2822 :       CALL setup_matrices2(qs_env, nderivatives, nimg, matrix_s, "OVERLAP", sab_orb)
     186        2822 :       CALL setup_matrices2(qs_env, 0, nimg, matrix_h, "CORE HAMILTONIAN", sab_orb)
     187        2822 :       CALL set_ks_env(ks_env, matrix_s_kp=matrix_s)
     188        2822 :       CALL set_ks_env(ks_env, matrix_h_kp=matrix_h)
     189             : 
     190        2822 :       NULLIFY (dftb_potential)
     191        2822 :       CALL get_qs_env(qs_env=qs_env, dftb_potential=dftb_potential)
     192        2822 :       NULLIFY (particle_set)
     193        2822 :       CALL get_qs_env(qs_env=qs_env, particle_set=particle_set)
     194             : 
     195        2822 :       IF (calculate_forces) THEN
     196         724 :          NULLIFY (rho, force, matrix_w)
     197             :          CALL get_qs_env(qs_env=qs_env, &
     198             :                          rho=rho, &
     199             :                          matrix_w_kp=matrix_w, &
     200             :                          virial=virial, &
     201         724 :                          force=force)
     202         724 :          CALL qs_rho_get(rho, rho_ao_kp=matrix_p)
     203             : 
     204         724 :          IF (SIZE(matrix_p, 1) == 2) THEN
     205         328 :             DO img = 1, nimg
     206             :                CALL dbcsr_add(matrix_p(1, img)%matrix, matrix_p(2, img)%matrix, &
     207         164 :                               alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
     208             :                CALL dbcsr_add(matrix_w(1, img)%matrix, matrix_w(2, img)%matrix, &
     209         328 :                               alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
     210             :             END DO
     211             :          END IF
     212         724 :          CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, atom_of_kind=atom_of_kind)
     213         724 :          use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
     214             :       END IF
     215             :       ! atomic energy decomposition
     216        2822 :       IF (atprop%energy) THEN
     217         162 :          CALL atprop_array_init(atprop%atecc, natom=SIZE(particle_set))
     218             :       END IF
     219             : 
     220        2822 :       NULLIFY (cell_to_index)
     221        2822 :       IF (nimg > 1) THEN
     222         290 :          CALL get_ks_env(ks_env=ks_env, kpoints=kpoints)
     223         290 :          CALL get_kpoint_info(kpoint=kpoints, cell_to_index=cell_to_index)
     224             :       END IF
     225             : 
     226        2822 :       erep = 0._dp
     227             : 
     228        2822 :       nkind = SIZE(atomic_kind_set)
     229             : 
     230        2822 :       CALL neighbor_list_iterator_create(nl_iterator, sab_orb)
     231      726673 :       DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
     232             :          CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, &
     233      723851 :                                 iatom=iatom, jatom=jatom, r=rij, cell=cell)
     234      723851 :          CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind_a)
     235             :          CALL get_dftb_atom_param(dftb_kind_a, &
     236             :                                   defined=defined, lmax=lmaxi, skself=skself, &
     237      723851 :                                   eta=eta_a, natorb=natorb_a)
     238      723851 :          IF (.NOT. defined .OR. natorb_a < 1) CYCLE
     239      723851 :          CALL get_qs_kind(qs_kind_set(jkind), dftb_parameter=dftb_kind_b)
     240             :          CALL get_dftb_atom_param(dftb_kind_b, &
     241      723851 :                                   defined=defined, lmax=lmaxj, eta=eta_b, natorb=natorb_b)
     242             : 
     243      723851 :          IF (.NOT. defined .OR. natorb_b < 1) CYCLE
     244             : 
     245             :          ! retrieve information on F and S matrix
     246      723851 :          dftb_param_ij => dftb_potential(ikind, jkind)
     247      723851 :          dftb_param_ji => dftb_potential(jkind, ikind)
     248             :          ! assume table size and type is symmetric
     249      723851 :          ngrd = dftb_param_ij%ngrd
     250      723851 :          ngrdcut = dftb_param_ij%ngrdcut
     251      723851 :          dgrd = dftb_param_ij%dgrd
     252      723851 :          ddr = dgrd*0.1_dp
     253      723851 :          CPASSERT(dftb_param_ij%llm == dftb_param_ji%llm)
     254      723851 :          llm = dftb_param_ij%llm
     255      723851 :          fmatij => dftb_param_ij%fmat
     256      723851 :          smatij => dftb_param_ij%smat
     257      723851 :          fmatji => dftb_param_ji%fmat
     258      723851 :          smatji => dftb_param_ji%smat
     259             :          ! repulsive pair potential
     260      723851 :          n_urpoly = dftb_param_ij%n_urpoly
     261      723851 :          urep_cut = dftb_param_ij%urep_cut
     262     7962361 :          urep = dftb_param_ij%urep
     263      723851 :          spxr => dftb_param_ij%spxr
     264      723851 :          scoeff => dftb_param_ij%scoeff
     265      723851 :          spdim = dftb_param_ij%spdim
     266      723851 :          s_cut = dftb_param_ij%s_cut
     267     2895404 :          srep = dftb_param_ij%srep
     268     2171553 :          surr = dftb_param_ij%surr
     269             : 
     270     2895404 :          dr = SQRT(SUM(rij(:)**2))
     271      723851 :          IF (NINT(dr/dgrd) <= ngrdcut) THEN
     272             : 
     273      722189 :             IF (nimg == 1) THEN
     274             :                ic = 1
     275             :             ELSE
     276       34363 :                ic = cell_to_index(cell(1), cell(2), cell(3))
     277       34363 :                CPASSERT(ic > 0)
     278             :             END IF
     279             : 
     280      722189 :             icol = MAX(iatom, jatom)
     281      722189 :             irow = MIN(iatom, jatom)
     282      722189 :             NULLIFY (sblock, fblock)
     283             :             CALL dbcsr_get_block_p(matrix=matrix_s(1, ic)%matrix, &
     284      722189 :                                    row=irow, col=icol, BLOCK=sblock, found=found)
     285      722189 :             CPASSERT(found)
     286             :             CALL dbcsr_get_block_p(matrix=matrix_h(1, ic)%matrix, &
     287      722189 :                                    row=irow, col=icol, BLOCK=fblock, found=found)
     288      722189 :             CPASSERT(found)
     289             : 
     290      722189 :             IF (calculate_forces) THEN
     291      312241 :                NULLIFY (pblock)
     292             :                CALL dbcsr_get_block_p(matrix=matrix_p(1, ic)%matrix, &
     293      312241 :                                       row=irow, col=icol, block=pblock, found=found)
     294      312241 :                CPASSERT(ASSOCIATED(pblock))
     295      312241 :                NULLIFY (wblock)
     296             :                CALL dbcsr_get_block_p(matrix=matrix_w(1, ic)%matrix, &
     297      312241 :                                       row=irow, col=icol, block=wblock, found=found)
     298      312241 :                CPASSERT(ASSOCIATED(wblock))
     299      312241 :                IF (dftb_control%self_consistent) THEN
     300     1038628 :                   DO i = 2, 4
     301      778971 :                      NULLIFY (dsblocks(i)%block)
     302             :                      CALL dbcsr_get_block_p(matrix=matrix_s(i, ic)%matrix, &
     303      778971 :                                             row=irow, col=icol, BLOCK=dsblocks(i)%block, found=found)
     304     1038628 :                      CPASSERT(found)
     305             :                   END DO
     306             :                END IF
     307             :             END IF
     308             : 
     309      722189 :             IF (iatom == jatom .AND. dr < 0.001_dp) THEN
     310             :                ! diagonal block
     311       71100 :                DO i = 1, natorb_a
     312       48649 :                   sblock(i, i) = sblock(i, i) + 1._dp
     313       71100 :                   fblock(i, i) = fblock(i, i) + skself(orbptr(i))
     314             :                END DO
     315             :             ELSE
     316             :                ! off-diagonal block
     317             :                CALL compute_block_sk(sblock, smatij, smatji, rij, ngrd, ngrdcut, dgrd, &
     318      699738 :                                      llm, lmaxi, lmaxj, irow, iatom)
     319             :                CALL compute_block_sk(fblock, fmatij, fmatji, rij, ngrd, ngrdcut, dgrd, &
     320      699738 :                                      llm, lmaxi, lmaxj, irow, iatom)
     321      699738 :                IF (calculate_forces) THEN
     322      303765 :                   force_ab = 0._dp
     323      303765 :                   force_w = 0._dp
     324      303765 :                   n1 = SIZE(fblock, 1)
     325      303765 :                   n2 = SIZE(fblock, 2)
     326             :                   ! make sure that displacement is in the correct direction depending on the position
     327             :                   ! of the block (upper or lower triangle)
     328      303765 :                   f0 = 1.0_dp
     329      303765 :                   IF (irow == iatom) f0 = -1.0_dp
     330             : 
     331     1822590 :                   ALLOCATE (dfblock(n1, n2), dsblock(n1, n2))
     332             : 
     333     1215060 :                   DO i = 1, 3
     334      911295 :                      drij = rij
     335    12094617 :                      dfblock = 0._dp; dsblock = 0._dp
     336             : 
     337      911295 :                      drij(i) = rij(i) - ddr*f0
     338             :                      CALL compute_block_sk(dsblock, smatij, smatji, drij, ngrd, ngrdcut, dgrd, &
     339      911295 :                                            llm, lmaxi, lmaxj, irow, iatom)
     340             :                      CALL compute_block_sk(dfblock, fmatij, fmatji, drij, ngrd, ngrdcut, dgrd, &
     341      911295 :                                            llm, lmaxi, lmaxj, irow, iatom)
     342             : 
     343     6502956 :                      dsblock = -dsblock
     344     6502956 :                      dfblock = -dfblock
     345             : 
     346      911295 :                      drij(i) = rij(i) + ddr*f0
     347             :                      CALL compute_block_sk(dsblock, smatij, smatji, drij, ngrd, ngrdcut, dgrd, &
     348      911295 :                                            llm, lmaxi, lmaxj, irow, iatom)
     349             :                      CALL compute_block_sk(dfblock, fmatij, fmatji, drij, ngrd, ngrdcut, dgrd, &
     350      911295 :                                            llm, lmaxi, lmaxj, irow, iatom)
     351             : 
     352     6502956 :                      dfblock = dfblock/(2.0_dp*ddr)
     353     6502956 :                      dsblock = dsblock/(2.0_dp*ddr)
     354             : 
     355     6502956 :                      foab = 2.0_dp*SUM(dfblock*pblock)
     356     6502956 :                      fow = -2.0_dp*SUM(dsblock*wblock)
     357             : 
     358      911295 :                      force_ab(i) = force_ab(i) + foab
     359      911295 :                      force_w(i) = force_w(i) + fow
     360     1215060 :                      IF (dftb_control%self_consistent) THEN
     361      757650 :                         CPASSERT(ASSOCIATED(dsblocks(i + 1)%block))
     362     5395806 :                         dsblocks(i + 1)%block = dsblocks(i + 1)%block + dsblock
     363             :                      END IF
     364             :                   END DO
     365      303765 :                   IF (use_virial) THEN
     366      188575 :                      IF (iatom == jatom) f0 = 0.5_dp*f0
     367      188575 :                      CALL virial_pair_force(virial%pv_virial, -f0, force_ab, rij)
     368      188575 :                      CALL virial_pair_force(virial%pv_virial, -f0, force_w, rij)
     369             :                   END IF
     370      303765 :                   DEALLOCATE (dfblock, dsblock)
     371             :                END IF
     372             :             END IF
     373             : 
     374      722189 :             IF (calculate_forces .AND. (iatom /= jatom .OR. dr > 0.001_dp)) THEN
     375      303765 :                atom_a = atom_of_kind(iatom)
     376      303765 :                atom_b = atom_of_kind(jatom)
     377      756729 :                IF (irow == iatom) force_ab = -force_ab
     378      756729 :                IF (irow == iatom) force_w = -force_w
     379     1215060 :                force(ikind)%all_potential(:, atom_a) = force(ikind)%all_potential(:, atom_a) - force_ab(:)
     380     1215060 :                force(jkind)%all_potential(:, atom_b) = force(jkind)%all_potential(:, atom_b) + force_ab(:)
     381     1215060 :                force(ikind)%overlap(:, atom_a) = force(ikind)%overlap(:, atom_a) - force_w(:)
     382     1223536 :                force(jkind)%overlap(:, atom_b) = force(jkind)%overlap(:, atom_b) + force_w(:)
     383             :             END IF
     384             : 
     385             :          END IF
     386             : 
     387             :          ! repulsive potential
     388      726673 :          IF ((dr <= urep_cut .OR. spdim > 0) .AND. dr > 0.001_dp) THEN
     389      542295 :             erepij = 0._dp
     390             :             CALL urep_egr(rij, dr, erepij, force_rr, &
     391      542295 :                           n_urpoly, urep, spdim, s_cut, srep, spxr, scoeff, surr, calculate_forces)
     392      542295 :             erep = erep + erepij
     393      542295 :             IF (atprop%energy) THEN
     394      228665 :                atprop%atecc(iatom) = atprop%atecc(iatom) + 0.5_dp*erepij
     395      228665 :                atprop%atecc(jatom) = atprop%atecc(jatom) + 0.5_dp*erepij
     396             :             END IF
     397      542295 :             IF (calculate_forces .AND. (iatom /= jatom .OR. dr > 0.001_dp)) THEN
     398      254994 :                atom_a = atom_of_kind(iatom)
     399      254994 :                atom_b = atom_of_kind(jatom)
     400             :                force(ikind)%repulsive(:, atom_a) = &
     401     1019976 :                   force(ikind)%repulsive(:, atom_a) - force_rr(:)
     402             :                force(jkind)%repulsive(:, atom_b) = &
     403     1019976 :                   force(jkind)%repulsive(:, atom_b) + force_rr(:)
     404      254994 :                IF (use_virial) THEN
     405      169917 :                   f0 = -1.0_dp
     406      169917 :                   IF (iatom == jatom) f0 = -0.5_dp
     407      169917 :                   CALL virial_pair_force(virial%pv_virial, f0, force_rr, rij)
     408             :                END IF
     409             :             END IF
     410             :          END IF
     411             : 
     412             :       END DO
     413        2822 :       CALL neighbor_list_iterator_release(nl_iterator)
     414             : 
     415        7492 :       DO i = 1, SIZE(matrix_s, 1)
     416       20984 :          DO img = 1, nimg
     417       18162 :             CALL dbcsr_finalize(matrix_s(i, img)%matrix)
     418             :          END DO
     419             :       END DO
     420        5644 :       DO i = 1, SIZE(matrix_h, 1)
     421       15026 :          DO img = 1, nimg
     422       12204 :             CALL dbcsr_finalize(matrix_h(i, img)%matrix)
     423             :          END DO
     424             :       END DO
     425             : 
     426             :       ! set repulsive energy
     427        2822 :       CALL para_env%sum(erep)
     428        2822 :       energy%repulsive = erep
     429             : 
     430        2822 :       CALL section_vals_val_get(qs_env%input, "DFT%PRINT%AO_MATRICES%OMIT_HEADERS", l_val=omit_headers)
     431        2822 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, &
     432             :                                            qs_env%input, "DFT%PRINT%AO_MATRICES/CORE_HAMILTONIAN"), cp_p_file)) THEN
     433             :          iw = cp_print_key_unit_nr(logger, qs_env%input, "DFT%PRINT%AO_MATRICES/CORE_HAMILTONIAN", &
     434           0 :                                    extension=".Log")
     435           0 :          CALL section_vals_val_get(qs_env%input, "DFT%PRINT%AO_MATRICES%NDIGITS", i_val=after)
     436           0 :          after = MIN(MAX(after, 1), 16)
     437           0 :          DO img = 1, nimg
     438             :             CALL cp_dbcsr_write_sparse_matrix(matrix_h(1, img)%matrix, 4, after, qs_env, para_env, &
     439           0 :                                               output_unit=iw, omit_headers=omit_headers)
     440             :          END DO
     441             : 
     442             :          CALL cp_print_key_finished_output(iw, logger, qs_env%input, &
     443           0 :                                            "DFT%PRINT%AO_MATRICES/CORE_HAMILTONIAN")
     444             :       END IF
     445             : 
     446        2822 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, &
     447             :                                            qs_env%input, "DFT%PRINT%AO_MATRICES/OVERLAP"), cp_p_file)) THEN
     448             :          iw = cp_print_key_unit_nr(logger, qs_env%input, "DFT%PRINT%AO_MATRICES/OVERLAP", &
     449           0 :                                    extension=".Log")
     450           0 :          CALL section_vals_val_get(qs_env%input, "DFT%PRINT%AO_MATRICES%NDIGITS", i_val=after)
     451           0 :          after = MIN(MAX(after, 1), 16)
     452           0 :          DO img = 1, nimg
     453             :             CALL cp_dbcsr_write_sparse_matrix(matrix_s(1, img)%matrix, 4, after, qs_env, para_env, &
     454           0 :                                               output_unit=iw, omit_headers=omit_headers)
     455             : 
     456           0 :             IF (BTEST(cp_print_key_should_output(logger%iter_info, &
     457           0 :                                                  qs_env%input, "DFT%PRINT%AO_MATRICES/DERIVATIVES"), cp_p_file)) THEN
     458           0 :                DO i = 2, SIZE(matrix_s, 1)
     459             :                   CALL cp_dbcsr_write_sparse_matrix(matrix_s(i, img)%matrix, 4, after, qs_env, para_env, &
     460           0 :                                                     output_unit=iw, omit_headers=omit_headers)
     461             :                END DO
     462             :             END IF
     463             :          END DO
     464             : 
     465             :          CALL cp_print_key_finished_output(iw, logger, qs_env%input, &
     466           0 :                                            "DFT%PRINT%AO_MATRICES/OVERLAP")
     467             :       END IF
     468             : 
     469        2822 :       IF (calculate_forces) THEN
     470         724 :          IF (SIZE(matrix_p, 1) == 2) THEN
     471         328 :             DO img = 1, nimg
     472             :                CALL dbcsr_add(matrix_p(1, img)%matrix, matrix_p(2, img)%matrix, alpha_scalar=1.0_dp, &
     473         328 :                               beta_scalar=-1.0_dp)
     474             :             END DO
     475             :          END IF
     476             :       END IF
     477             : 
     478        2822 :       CALL timestop(handle)
     479             : 
     480        8466 :    END SUBROUTINE build_dftb_matrices
     481             : 
     482             : ! **************************************************************************************************
     483             : !> \brief ...
     484             : !> \param qs_env ...
     485             : !> \param calculate_forces ...
     486             : !> \param just_energy ...
     487             : ! **************************************************************************************************
     488       13050 :    SUBROUTINE build_dftb_ks_matrix(qs_env, calculate_forces, just_energy)
     489             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     490             :       LOGICAL, INTENT(in)                                :: calculate_forces, just_energy
     491             : 
     492             :       CHARACTER(len=*), PARAMETER :: routineN = 'build_dftb_ks_matrix'
     493             : 
     494             :       INTEGER                                            :: atom_a, handle, iatom, ikind, img, &
     495             :                                                             ispin, natom, nkind, nspins, &
     496             :                                                             output_unit
     497             :       REAL(KIND=dp)                                      :: pc_ener, qmmm_el, zeff
     498       13050 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: mcharge, occupation_numbers
     499       13050 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: charges
     500       13050 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     501             :       TYPE(cp_logger_type), POINTER                      :: logger
     502       13050 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_p1, mo_derivs
     503       13050 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: ks_matrix, matrix_h, matrix_p, matrix_s
     504             :       TYPE(dbcsr_type), POINTER                          :: mo_coeff
     505             :       TYPE(dft_control_type), POINTER                    :: dft_control
     506             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     507       13050 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     508             :       TYPE(qs_dftb_atom_type), POINTER                   :: dftb_kind
     509             :       TYPE(qs_energy_type), POINTER                      :: energy
     510       13050 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     511             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     512             :       TYPE(qs_rho_type), POINTER                         :: rho
     513             :       TYPE(section_vals_type), POINTER                   :: scf_section
     514             : 
     515       13050 :       CALL timeset(routineN, handle)
     516       13050 :       NULLIFY (dft_control, logger, scf_section, matrix_p, particle_set, ks_env, &
     517       13050 :                ks_matrix, rho, energy)
     518       13050 :       logger => cp_get_default_logger()
     519       13050 :       CPASSERT(ASSOCIATED(qs_env))
     520             : 
     521             :       CALL get_qs_env(qs_env, &
     522             :                       dft_control=dft_control, &
     523             :                       atomic_kind_set=atomic_kind_set, &
     524             :                       qs_kind_set=qs_kind_set, &
     525             :                       matrix_h_kp=matrix_h, &
     526             :                       para_env=para_env, &
     527             :                       ks_env=ks_env, &
     528             :                       matrix_ks_kp=ks_matrix, &
     529             :                       rho=rho, &
     530       13050 :                       energy=energy)
     531             : 
     532       13050 :       energy%hartree = 0.0_dp
     533       13050 :       energy%qmmm_el = 0.0_dp
     534             : 
     535       13050 :       scf_section => section_vals_get_subs_vals(qs_env%input, "DFT%SCF")
     536       13050 :       nspins = dft_control%nspins
     537       13050 :       CPASSERT(ASSOCIATED(matrix_h))
     538       13050 :       CPASSERT(ASSOCIATED(rho))
     539       39150 :       CPASSERT(SIZE(ks_matrix) > 0)
     540             : 
     541       26380 :       DO ispin = 1, nspins
     542       85856 :          DO img = 1, SIZE(ks_matrix, 2)
     543             :             ! copy the core matrix into the fock matrix
     544       72806 :             CALL dbcsr_copy(ks_matrix(ispin, img)%matrix, matrix_h(1, img)%matrix)
     545             :          END DO
     546             :       END DO
     547             : 
     548       13050 :       IF (dft_control%qs_control%dftb_control%self_consistent) THEN
     549             :          ! Mulliken charges
     550             :          CALL get_qs_env(qs_env=qs_env, particle_set=particle_set, &
     551       11918 :                          matrix_s_kp=matrix_s)
     552       11918 :          CALL qs_rho_get(rho, rho_ao_kp=matrix_p)
     553       11918 :          natom = SIZE(particle_set)
     554       47672 :          ALLOCATE (charges(natom, nspins))
     555             :          !
     556       11918 :          CALL mulliken_charges(matrix_p, matrix_s, para_env, charges)
     557             :          !
     558       35754 :          ALLOCATE (mcharge(natom))
     559       11918 :          nkind = SIZE(atomic_kind_set)
     560       37402 :          DO ikind = 1, nkind
     561       25484 :             CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom)
     562       25484 :             CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind)
     563       25484 :             CALL get_dftb_atom_param(dftb_kind, zeff=zeff)
     564      218168 :             DO iatom = 1, natom
     565      155282 :                atom_a = atomic_kind_set(ikind)%atom_list(iatom)
     566      337168 :                mcharge(atom_a) = zeff - SUM(charges(atom_a, 1:nspins))
     567             :             END DO
     568             :          END DO
     569       11918 :          DEALLOCATE (charges)
     570             : 
     571             :          CALL build_dftb_coulomb(qs_env, ks_matrix, rho, mcharge, energy, &
     572       11918 :                                  calculate_forces, just_energy)
     573             : 
     574             :          CALL efield_tb_matrix(qs_env, ks_matrix, rho, mcharge, energy, &
     575       11918 :                                calculate_forces, just_energy)
     576             : 
     577       11918 :          DEALLOCATE (mcharge)
     578             : 
     579             :       END IF
     580             : 
     581       13050 :       IF (qs_env%qmmm) THEN
     582        4808 :          CPASSERT(SIZE(ks_matrix, 2) == 1)
     583        9616 :          DO ispin = 1, nspins
     584             :             ! If QM/MM sumup the 1el Hamiltonian
     585             :             CALL dbcsr_add(ks_matrix(ispin, 1)%matrix, qs_env%ks_qmmm_env%matrix_h(1)%matrix, &
     586        4808 :                            1.0_dp, 1.0_dp)
     587        4808 :             CALL qs_rho_get(rho, rho_ao=matrix_p1)
     588             :             ! Compute QM/MM Energy
     589             :             CALL dbcsr_dot(qs_env%ks_qmmm_env%matrix_h(1)%matrix, &
     590        4808 :                            matrix_p1(ispin)%matrix, qmmm_el)
     591        9616 :             energy%qmmm_el = energy%qmmm_el + qmmm_el
     592             :          END DO
     593        4808 :          pc_ener = qs_env%ks_qmmm_env%pc_ener
     594        4808 :          energy%qmmm_el = energy%qmmm_el + pc_ener
     595             :       END IF
     596             : 
     597             :       energy%total = energy%core + energy%hartree + energy%qmmm_el + energy%efield + &
     598       13050 :                      energy%repulsive + energy%dispersion + energy%dftb3
     599             : 
     600       13050 :       IF (dft_control%qs_control%dftb_control%self_consistent) THEN
     601             :          output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%DETAILED_ENERGY", &
     602       11918 :                                             extension=".scfLog")
     603       11918 :          IF (output_unit > 0) THEN
     604             :             WRITE (UNIT=output_unit, FMT="(/,(T9,A,T60,F20.10))") &
     605          15 :                "Repulsive pair potential energy:               ", energy%repulsive, &
     606          15 :                "Zeroth order Hamiltonian energy:               ", energy%core, &
     607          15 :                "Charge fluctuation energy:                     ", energy%hartree, &
     608          30 :                "London dispersion energy:                      ", energy%dispersion
     609          15 :             IF (ABS(energy%efield) > 1.e-12_dp) THEN
     610             :                WRITE (UNIT=output_unit, FMT="(T9,A,T60,F20.10)") &
     611           0 :                   "Electric field interaction energy:             ", energy%efield
     612             :             END IF
     613          15 :             IF (dft_control%qs_control%dftb_control%dftb3_diagonal) THEN
     614             :                WRITE (UNIT=output_unit, FMT="(T9,A,T60,F20.10)") &
     615           0 :                   "DFTB3 3rd Order Energy Correction              ", energy%dftb3
     616             :             END IF
     617          15 :             IF (qs_env%qmmm) THEN
     618             :                WRITE (UNIT=output_unit, FMT="(T9,A,T60,F20.10)") &
     619           0 :                   "QM/MM Electrostatic energy:                    ", energy%qmmm_el
     620             :             END IF
     621             :          END IF
     622             :          CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
     623       11918 :                                            "PRINT%DETAILED_ENERGY")
     624             :       END IF
     625             :       ! here we compute dE/dC if needed. Assumes dE/dC is H_{ks}C (plus occupation numbers)
     626       13050 :       IF (qs_env%requires_mo_derivs .AND. .NOT. just_energy) THEN
     627         336 :          CPASSERT(SIZE(ks_matrix, 2) == 1)
     628             :          BLOCK
     629         336 :             TYPE(mo_set_type), DIMENSION(:), POINTER :: mo_array
     630         336 :             CALL get_qs_env(qs_env, mo_derivs=mo_derivs, mos=mo_array)
     631         700 :             DO ispin = 1, SIZE(mo_derivs)
     632             :                CALL get_mo_set(mo_set=mo_array(ispin), &
     633         364 :                                mo_coeff_b=mo_coeff, occupation_numbers=occupation_numbers)
     634         364 :                IF (.NOT. mo_array(ispin)%use_mo_coeff_b) THEN
     635           0 :                   CPABORT("")
     636             :                END IF
     637             :                CALL dbcsr_multiply('n', 'n', 1.0_dp, ks_matrix(ispin, 1)%matrix, mo_coeff, &
     638         700 :                                    0.0_dp, mo_derivs(ispin)%matrix)
     639             :             END DO
     640             :          END BLOCK
     641             :       END IF
     642             : 
     643       13050 :       CALL timestop(handle)
     644             : 
     645       13050 :    END SUBROUTINE build_dftb_ks_matrix
     646             : 
     647             : ! **************************************************************************************************
     648             : !> \brief ...
     649             : !> \param qs_env ...
     650             : !> \param nderivative ...
     651             : !> \param matrix_s ...
     652             : ! **************************************************************************************************
     653        1792 :    SUBROUTINE build_dftb_overlap(qs_env, nderivative, matrix_s)
     654             : 
     655             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     656             :       INTEGER, INTENT(IN)                                :: nderivative
     657             :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
     658             : 
     659             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'build_dftb_overlap'
     660             : 
     661             :       INTEGER :: handle, i, iatom, icol, ikind, indder, irow, j, jatom, jkind, l1, l2, la, lb, &
     662             :          llm, lmaxi, lmaxj, m, n1, n2, natom, natorb_a, natorb_b, ngrd, ngrdcut, nkind
     663             :       LOGICAL                                            :: defined, found
     664             :       REAL(KIND=dp)                                      :: ddr, dgrd, dr, f0
     665             :       REAL(KIND=dp), DIMENSION(0:3)                      :: skself
     666             :       REAL(KIND=dp), DIMENSION(3)                        :: drij, rij
     667        1792 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: dsblock, dsblockm, sblock, smatij, smatji
     668         896 :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER         :: dsblock1
     669         896 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     670        8960 :       TYPE(block_p_type), DIMENSION(2:10)                :: dsblocks
     671             :       TYPE(cp_logger_type), POINTER                      :: logger
     672             :       TYPE(dft_control_type), POINTER                    :: dft_control
     673             :       TYPE(dftb_control_type), POINTER                   :: dftb_control
     674             :       TYPE(neighbor_list_iterator_p_type), &
     675         896 :          DIMENSION(:), POINTER                           :: nl_iterator
     676             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     677         896 :          POINTER                                         :: sab_orb
     678             :       TYPE(qs_dftb_atom_type), POINTER                   :: dftb_kind_a, dftb_kind_b
     679             :       TYPE(qs_dftb_pairpot_type), DIMENSION(:, :), &
     680         896 :          POINTER                                         :: dftb_potential
     681             :       TYPE(qs_dftb_pairpot_type), POINTER                :: dftb_param_ij, dftb_param_ji
     682         896 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     683             : 
     684         896 :       CALL timeset(routineN, handle)
     685             : 
     686             :       ! set pointers
     687         896 :       iptr = 0
     688        4480 :       DO la = 0, 3
     689       18816 :          DO lb = 0, 3
     690       14336 :             llm = 0
     691       62720 :             DO l1 = 0, MAX(la, lb)
     692      130816 :                DO l2 = 0, MIN(l1, la, lb)
     693      223104 :                   DO m = 0, l2
     694      106624 :                      llm = llm + 1
     695      178304 :                      iptr(l1, l2, m, la, lb) = llm
     696             :                   END DO
     697             :                END DO
     698             :             END DO
     699             :          END DO
     700             :       END DO
     701             : 
     702         896 :       NULLIFY (logger)
     703         896 :       logger => cp_get_default_logger()
     704             : 
     705         896 :       NULLIFY (atomic_kind_set, qs_kind_set, sab_orb)
     706             : 
     707             :       CALL get_qs_env(qs_env=qs_env, &
     708             :                       atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set, &
     709         896 :                       dft_control=dft_control)
     710             : 
     711         896 :       dftb_control => dft_control%qs_control%dftb_control
     712             : 
     713         896 :       NULLIFY (dftb_potential)
     714             :       CALL get_qs_env(qs_env=qs_env, &
     715         896 :                       dftb_potential=dftb_potential)
     716             : 
     717         896 :       nkind = SIZE(atomic_kind_set)
     718             : 
     719             :       ! Allocate the overlap matrix
     720         896 :       CALL get_qs_env(qs_env=qs_env, sab_orb=sab_orb)
     721         896 :       CALL setup_matrices1(qs_env, nderivative, matrix_s, 'OVERLAP', sab_orb)
     722             : 
     723         896 :       CALL neighbor_list_iterator_create(nl_iterator, sab_orb)
     724        3668 :       DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
     725             :          CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, &
     726        2772 :                                 iatom=iatom, jatom=jatom, r=rij)
     727             : 
     728        2772 :          CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom)
     729        2772 :          CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind_a)
     730             :          CALL get_dftb_atom_param(dftb_kind_a, &
     731             :                                   defined=defined, lmax=lmaxi, skself=skself, &
     732        2772 :                                   natorb=natorb_a)
     733             : 
     734        2772 :          IF (.NOT. defined .OR. natorb_a < 1) CYCLE
     735             : 
     736        2772 :          CALL get_qs_kind(qs_kind_set(jkind), dftb_parameter=dftb_kind_b)
     737             :          CALL get_dftb_atom_param(dftb_kind_b, &
     738        2772 :                                   defined=defined, lmax=lmaxj, natorb=natorb_b)
     739             : 
     740        2772 :          IF (.NOT. defined .OR. natorb_b < 1) CYCLE
     741             : 
     742             :          ! retrieve information on F and S matrix
     743        2772 :          dftb_param_ij => dftb_potential(ikind, jkind)
     744        2772 :          dftb_param_ji => dftb_potential(jkind, ikind)
     745             :          ! assume table size and type is symmetric
     746        2772 :          ngrd = dftb_param_ij%ngrd
     747        2772 :          ngrdcut = dftb_param_ij%ngrdcut
     748        2772 :          dgrd = dftb_param_ij%dgrd
     749        2772 :          ddr = dgrd*0.1_dp
     750        2772 :          CPASSERT(dftb_param_ij%llm == dftb_param_ji%llm)
     751        2772 :          llm = dftb_param_ij%llm
     752        2772 :          smatij => dftb_param_ij%smat
     753        2772 :          smatji => dftb_param_ji%smat
     754             : 
     755       11088 :          dr = SQRT(SUM(rij(:)**2))
     756        3668 :          IF (NINT(dr/dgrd) <= ngrdcut) THEN
     757             : 
     758        2772 :             icol = MAX(iatom, jatom); irow = MIN(iatom, jatom)
     759             : 
     760        2772 :             NULLIFY (sblock)
     761             :             CALL dbcsr_get_block_p(matrix=matrix_s(1)%matrix, &
     762        2772 :                                    row=irow, col=icol, BLOCK=sblock, found=found)
     763        2772 :             CPASSERT(found)
     764             : 
     765        2772 :             IF (nderivative .GT. 0) THEN
     766        3672 :                DO i = 2, SIZE(matrix_s, 1)
     767        3258 :                   NULLIFY (dsblocks(i)%block)
     768             :                   CALL dbcsr_get_block_p(matrix=matrix_s(i)%matrix, &
     769        3672 :                                          row=irow, col=icol, BLOCK=dsblocks(i)%block, found=found)
     770             :                END DO
     771             :             END IF
     772             : 
     773        2772 :             IF (iatom == jatom .AND. dr < 0.001_dp) THEN
     774             :                ! diagonal block
     775        4137 :                DO i = 1, natorb_a
     776        4137 :                   sblock(i, i) = sblock(i, i) + 1._dp
     777             :                END DO
     778             :             ELSE
     779             :                ! off-diagonal block
     780             :                CALL compute_block_sk(sblock, smatij, smatji, rij, ngrd, ngrdcut, dgrd, &
     781        1407 :                                      llm, lmaxi, lmaxj, irow, iatom)
     782             : 
     783        1407 :                IF (nderivative .GE. 1) THEN
     784         228 :                   n1 = SIZE(sblock, 1); n2 = SIZE(sblock, 2)
     785         228 :                   indder = 1 ! used to put the 2nd derivatives in the correct matric (5=xx,8=yy,10=zz)
     786             : 
     787        2280 :                   ALLOCATE (dsblock1(n1, n2, 3), dsblock(n1, n2), dsblockm(n1, n2))
     788        5394 :                   dsblock1 = 0.0_dp
     789         912 :                   DO i = 1, 3
     790        9648 :                      dsblock = 0._dp; dsblockm = 0.0_dp
     791         684 :                      drij = rij
     792         684 :                      f0 = 1.0_dp; IF (irow == iatom) f0 = -1.0_dp
     793             : 
     794         684 :                      drij(i) = rij(i) - ddr*f0
     795             :                      CALL compute_block_sk(dsblockm, smatij, smatji, drij, ngrd, ngrdcut, dgrd, &
     796         684 :                                            llm, lmaxi, lmaxj, irow, iatom)
     797             : 
     798         684 :                      drij(i) = rij(i) + ddr*f0
     799             :                      CALL compute_block_sk(dsblock, smatij, smatji, drij, ngrd, ngrdcut, dgrd, &
     800         684 :                                            llm, lmaxi, lmaxj, irow, iatom)
     801             : 
     802        9648 :                      dsblock1(:, :, i) = (dsblock + dsblockm)
     803        9648 :                      dsblock = dsblock - dsblockm
     804        5166 :                      dsblock = dsblock/(2.0_dp*ddr)
     805             : 
     806         684 :                      CPASSERT(ASSOCIATED(dsblocks(i + 1)%block))
     807        5166 :                      dsblocks(i + 1)%block = dsblocks(i + 1)%block + dsblock
     808         912 :                      IF (nderivative .GT. 1) THEN
     809         567 :                         indder = indder + 5 - i
     810        4347 :                         dsblocks(indder)%block = 0.0_dp
     811             :                         dsblocks(indder)%block = dsblocks(indder)%block + &
     812        4347 :                                                  (dsblock1(:, :, i) - 2.0_dp*sblock)/ddr**2
     813             :                      END IF
     814             :                   END DO
     815             : 
     816         228 :                   IF (nderivative .GT. 1) THEN
     817         567 :                      DO i = 1, 2
     818        1134 :                         DO j = i + 1, 3
     819        8127 :                            dsblock = 0._dp; dsblockm = 0.0_dp
     820         567 :                            drij = rij
     821         567 :                            f0 = 1.0_dp; IF (irow == iatom) f0 = -1.0_dp
     822             : 
     823         567 :                            drij(i) = rij(i) - ddr*f0; drij(j) = rij(j) - ddr*f0
     824             :                            CALL compute_block_sk(dsblockm, smatij, smatji, drij, ngrd, ngrdcut, dgrd, &
     825         567 :                                                  llm, lmaxi, lmaxj, irow, iatom)
     826             : 
     827         567 :                            drij(i) = rij(i) + ddr*f0; drij(j) = rij(j) + ddr*f0
     828             :                            CALL compute_block_sk(dsblock, smatij, smatji, drij, ngrd, ngrdcut, dgrd, &
     829         567 :                                                  llm, lmaxi, lmaxj, irow, iatom)
     830             : 
     831         567 :                            indder = 2 + 2*i + j
     832        4347 :                            dsblocks(indder)%block = 0.0_dp
     833             :                            dsblocks(indder)%block = &
     834             :                               dsblocks(indder)%block + ( &
     835        4725 :                               dsblock + dsblockm - dsblock1(:, :, i) - dsblock1(:, :, j) + 2.0_dp*sblock)/(2.0_dp*ddr**2)
     836             :                         END DO
     837             :                      END DO
     838             :                   END IF
     839             : 
     840         228 :                   DEALLOCATE (dsblock1, dsblock, dsblockm)
     841             :                END IF
     842             :             END IF
     843             :          END IF
     844             :       END DO
     845         896 :       CALL neighbor_list_iterator_release(nl_iterator)
     846             : 
     847        2626 :       DO i = 1, SIZE(matrix_s, 1)
     848        2626 :          CALL dbcsr_finalize(matrix_s(i)%matrix)
     849             :       END DO
     850             : 
     851         896 :       CALL timestop(handle)
     852             : 
     853        1792 :    END SUBROUTINE build_dftb_overlap
     854             : 
     855             : ! **************************************************************************************************
     856             : !> \brief ...
     857             : !> \param qs_env ...
     858             : !> \param nderivative ...
     859             : !> \param matrices ...
     860             : !> \param mnames ...
     861             : !> \param sab_nl ...
     862             : ! **************************************************************************************************
     863         896 :    SUBROUTINE setup_matrices1(qs_env, nderivative, matrices, mnames, sab_nl)
     864             : 
     865             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     866             :       INTEGER, INTENT(IN)                                :: nderivative
     867             :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrices
     868             :       CHARACTER(LEN=*)                                   :: mnames
     869             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     870             :          POINTER                                         :: sab_nl
     871             : 
     872             :       CHARACTER(1)                                       :: symmetry_type
     873             :       CHARACTER(LEN=default_string_length)               :: matnames
     874             :       INTEGER                                            :: i, natom, neighbor_list_id, nkind, nmat, &
     875             :                                                             nsgf
     876             :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: first_sgf, last_sgf
     877         896 :       INTEGER, DIMENSION(:), POINTER                     :: row_blk_sizes
     878         896 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     879             :       TYPE(dbcsr_distribution_type), POINTER             :: dbcsr_dist
     880         896 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     881         896 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     882             : 
     883         896 :       NULLIFY (particle_set, atomic_kind_set)
     884             : 
     885             :       CALL get_qs_env(qs_env=qs_env, &
     886             :                       atomic_kind_set=atomic_kind_set, &
     887             :                       qs_kind_set=qs_kind_set, &
     888             :                       particle_set=particle_set, &
     889             :                       dbcsr_dist=dbcsr_dist, &
     890         896 :                       neighbor_list_id=neighbor_list_id)
     891             : 
     892         896 :       nkind = SIZE(atomic_kind_set)
     893         896 :       natom = SIZE(particle_set)
     894             : 
     895         896 :       CALL get_qs_kind_set(qs_kind_set, nsgf=nsgf)
     896             : 
     897        2688 :       ALLOCATE (first_sgf(natom))
     898        1792 :       ALLOCATE (last_sgf(natom))
     899             : 
     900             :       CALL get_particle_set(particle_set, qs_kind_set, &
     901             :                             first_sgf=first_sgf, &
     902         896 :                             last_sgf=last_sgf)
     903             : 
     904         896 :       nmat = 0
     905         896 :       IF (nderivative == 0) nmat = 1
     906         896 :       IF (nderivative == 1) nmat = 4
     907         896 :       IF (nderivative == 2) nmat = 10
     908         896 :       CPASSERT(nmat > 0)
     909             : 
     910        1792 :       ALLOCATE (row_blk_sizes(natom))
     911         896 :       CALL dbcsr_convert_offsets_to_sizes(first_sgf, row_blk_sizes, last_sgf)
     912             : 
     913         896 :       CALL dbcsr_allocate_matrix_set(matrices, nmat)
     914             : 
     915             :       ! Up to 2nd derivative take care to get the symmetries correct
     916        2626 :       DO i = 1, nmat
     917        1730 :          IF (i .GT. 1) THEN
     918         834 :             matnames = TRIM(mnames)//" DERIVATIVE MATRIX DFTB"
     919         834 :             symmetry_type = dbcsr_type_antisymmetric
     920         834 :             IF (i .GT. 4) symmetry_type = dbcsr_type_symmetric
     921             :          ELSE
     922         896 :             symmetry_type = dbcsr_type_symmetric
     923         896 :             matnames = TRIM(mnames)//" MATRIX DFTB"
     924             :          END IF
     925        1730 :          ALLOCATE (matrices(i)%matrix)
     926             :          CALL dbcsr_create(matrix=matrices(i)%matrix, &
     927             :                            name=TRIM(matnames), &
     928             :                            dist=dbcsr_dist, matrix_type=symmetry_type, &
     929             :                            row_blk_size=row_blk_sizes, col_blk_size=row_blk_sizes, &
     930        1730 :                            nze=0, mutable_work=.TRUE.)
     931        2626 :          CALL cp_dbcsr_alloc_block_from_nbl(matrices(i)%matrix, sab_nl)
     932             :       END DO
     933             : 
     934         896 :       DEALLOCATE (first_sgf)
     935         896 :       DEALLOCATE (last_sgf)
     936             : 
     937         896 :       DEALLOCATE (row_blk_sizes)
     938             : 
     939         896 :    END SUBROUTINE setup_matrices1
     940             : 
     941             : ! **************************************************************************************************
     942             : !> \brief ...
     943             : !> \param qs_env ...
     944             : !> \param nderivative ...
     945             : !> \param nimg ...
     946             : !> \param matrices ...
     947             : !> \param mnames ...
     948             : !> \param sab_nl ...
     949             : ! **************************************************************************************************
     950        5644 :    SUBROUTINE setup_matrices2(qs_env, nderivative, nimg, matrices, mnames, sab_nl)
     951             : 
     952             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     953             :       INTEGER, INTENT(IN)                                :: nderivative, nimg
     954             :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrices
     955             :       CHARACTER(LEN=*)                                   :: mnames
     956             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     957             :          POINTER                                         :: sab_nl
     958             : 
     959             :       CHARACTER(1)                                       :: symmetry_type
     960             :       CHARACTER(LEN=default_string_length)               :: matnames
     961             :       INTEGER                                            :: i, img, natom, neighbor_list_id, nkind, &
     962             :                                                             nmat, nsgf
     963             :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: first_sgf, last_sgf
     964        5644 :       INTEGER, DIMENSION(:), POINTER                     :: row_blk_sizes
     965        5644 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     966             :       TYPE(dbcsr_distribution_type), POINTER             :: dbcsr_dist
     967        5644 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     968        5644 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     969             : 
     970        5644 :       NULLIFY (particle_set, atomic_kind_set)
     971             : 
     972             :       CALL get_qs_env(qs_env=qs_env, &
     973             :                       atomic_kind_set=atomic_kind_set, &
     974             :                       qs_kind_set=qs_kind_set, &
     975             :                       particle_set=particle_set, &
     976             :                       dbcsr_dist=dbcsr_dist, &
     977        5644 :                       neighbor_list_id=neighbor_list_id)
     978             : 
     979        5644 :       nkind = SIZE(atomic_kind_set)
     980        5644 :       natom = SIZE(particle_set)
     981             : 
     982        5644 :       CALL get_qs_kind_set(qs_kind_set, nsgf=nsgf)
     983             : 
     984       16932 :       ALLOCATE (first_sgf(natom))
     985       11288 :       ALLOCATE (last_sgf(natom))
     986             : 
     987             :       CALL get_particle_set(particle_set, qs_kind_set, &
     988             :                             first_sgf=first_sgf, &
     989        5644 :                             last_sgf=last_sgf)
     990             : 
     991        5644 :       nmat = 0
     992        5644 :       IF (nderivative == 0) nmat = 1
     993        5644 :       IF (nderivative == 1) nmat = 4
     994        5644 :       IF (nderivative == 2) nmat = 10
     995        5644 :       CPASSERT(nmat > 0)
     996             : 
     997       11288 :       ALLOCATE (row_blk_sizes(natom))
     998        5644 :       CALL dbcsr_convert_offsets_to_sizes(first_sgf, row_blk_sizes, last_sgf)
     999             : 
    1000        5644 :       CALL dbcsr_allocate_matrix_set(matrices, nmat, nimg)
    1001             : 
    1002             :       ! Up to 2nd derivative take care to get the symmetries correct
    1003       24408 :       DO img = 1, nimg
    1004       47282 :          DO i = 1, nmat
    1005       22874 :             IF (i .GT. 1) THEN
    1006        4110 :                matnames = TRIM(mnames)//" DERIVATIVE MATRIX DFTB"
    1007        4110 :                symmetry_type = dbcsr_type_antisymmetric
    1008        4110 :                IF (i .GT. 4) symmetry_type = dbcsr_type_symmetric
    1009             :             ELSE
    1010       18764 :                symmetry_type = dbcsr_type_symmetric
    1011       18764 :                matnames = TRIM(mnames)//" MATRIX DFTB"
    1012             :             END IF
    1013       22874 :             ALLOCATE (matrices(i, img)%matrix)
    1014             :             CALL dbcsr_create(matrix=matrices(i, img)%matrix, &
    1015             :                               name=TRIM(matnames), &
    1016             :                               dist=dbcsr_dist, matrix_type=symmetry_type, &
    1017             :                               row_blk_size=row_blk_sizes, col_blk_size=row_blk_sizes, &
    1018       22874 :                               nze=0, mutable_work=.TRUE.)
    1019       41638 :             CALL cp_dbcsr_alloc_block_from_nbl(matrices(i, img)%matrix, sab_nl)
    1020             :          END DO
    1021             :       END DO
    1022             : 
    1023        5644 :       DEALLOCATE (first_sgf)
    1024        5644 :       DEALLOCATE (last_sgf)
    1025             : 
    1026        5644 :       DEALLOCATE (row_blk_sizes)
    1027             : 
    1028        5644 :    END SUBROUTINE setup_matrices2
    1029             : 
    1030             : END MODULE qs_dftb_matrices
    1031             : 

Generated by: LCOV version 1.15