LCOV - code coverage report
Current view: top level - src - qs_collocate_density.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:c24029e) Lines: 77.1 % 1073 827
Test Date: 2026-07-04 06:36:57 Functions: 94.1 % 17 16

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Calculate the plane wave density by collocating the primitive Gaussian
      10              : !>      functions (pgf).
      11              : !> \par History
      12              : !>      - rewrote collocate for increased accuracy and speed
      13              : !>      - introduced the PGI hack for increased speed with that compiler
      14              : !>        (22.02.02)
      15              : !>      - Added Multiple Grid feature
      16              : !>      - new way to get over the grid (01.03.02)
      17              : !>      - removed timing calls since they were getting expensive
      18              : !>      - Updated with the new QS data structures (09.04.02,MK)
      19              : !>      - introduction of the real space grid type ( prelim. version JVdV 05.02)
      20              : !>      - parallel FFT (JGH 22.05.02)
      21              : !>      - multigrid arrays independent from density (JGH 30.08.02)
      22              : !>      - old density stored in g space (JGH 30.08.02)
      23              : !>      - distributed real space code (JGH 17.07.03)
      24              : !>      - refactoring and new loop ordering (JGH 23.11.03)
      25              : !>      - OpenMP parallelization (JGH 03.12.03)
      26              : !>      - Modified to compute tau (Joost 12.03)
      27              : !>      - removed the incremental density rebuild (Joost 01.04)
      28              : !>      - introduced realspace multigridding (Joost 02.04)
      29              : !>      - introduced map_consistent (Joost 02.04)
      30              : !>      - Addition of the subroutine calculate_atomic_charge_density (TdK, 08.05)
      31              : !>      - rewrite of the collocate/integrate kernels (Joost VandeVondele, 03.07)
      32              : !>      - Extended by the derivatives for DFPT [Sandra Luber, Edward Ditler, 2021]
      33              : !> \author Matthias Krack (03.04.2001)
      34              : !>      1) Joost VandeVondele (01.2002)
      35              : !>      Thomas D. Kuehne (04.08.2005)
      36              : !>      Ole Schuett (2020)
      37              : ! **************************************************************************************************
      38              : MODULE qs_collocate_density
      39              :    USE admm_types, ONLY: get_admm_env
      40              :    USE ao_util, ONLY: exp_radius_very_extended
      41              :    USE atomic_kind_types, ONLY: atomic_kind_type, &
      42              :                                 get_atomic_kind, &
      43              :                                 get_atomic_kind_set
      44              :    USE basis_set_types, ONLY: get_gto_basis_set, &
      45              :                               gto_basis_set_type
      46              :    USE cell_types, ONLY: cell_type, &
      47              :                          pbc
      48              :    USE cp_control_types, ONLY: dft_control_type
      49              :    USE cp_dbcsr_operations, ONLY: dbcsr_deallocate_matrix_set
      50              :    USE cp_fm_types, ONLY: cp_fm_get_element, &
      51              :                           cp_fm_get_info, &
      52              :                           cp_fm_type
      53              :    USE cp_dbcsr_api, ONLY: dbcsr_copy, &
      54              :                            dbcsr_get_block_p, &
      55              :                            dbcsr_p_type, &
      56              :                            dbcsr_type
      57              :    USE external_potential_types, ONLY: get_potential, &
      58              :                                        gth_potential_type
      59              :    USE gaussian_gridlevels, ONLY: gaussian_gridlevel, &
      60              :                                   gridlevel_info_type
      61              :    USE grid_api, ONLY: &
      62              :       GRID_FUNC_AB, GRID_FUNC_CORE_X, GRID_FUNC_CORE_Y, GRID_FUNC_CORE_Z, GRID_FUNC_DAB_X, &
      63              :       GRID_FUNC_DAB_Y, GRID_FUNC_DAB_Z, GRID_FUNC_DABpADB_X, GRID_FUNC_DABpADB_Y, &
      64              :       GRID_FUNC_DABpADB_Z, GRID_FUNC_DADB, GRID_FUNC_DX, GRID_FUNC_DXDX, GRID_FUNC_DXDY, &
      65              :       GRID_FUNC_DY, GRID_FUNC_DYDY, GRID_FUNC_DYDZ, GRID_FUNC_DZ, GRID_FUNC_DZDX, &
      66              :       GRID_FUNC_DZDZ, collocate_pgf_product, grid_collocate_task_list
      67              :    USE input_constants, ONLY: &
      68              :       orb_dx2, orb_dxy, orb_dy2, orb_dyz, orb_dz2, orb_dzx, orb_px, orb_py, orb_pz, orb_s
      69              :    USE kinds, ONLY: default_string_length, &
      70              :                     dp
      71              :    USE lri_environment_types, ONLY: lri_kind_type
      72              :    USE memory_utilities, ONLY: reallocate
      73              :    USE message_passing, ONLY: mp_comm_type
      74              :    USE orbital_pointers, ONLY: coset, &
      75              :                                ncoset
      76              :    USE particle_types, ONLY: particle_type
      77              :    USE pw_env_types, ONLY: pw_env_get, &
      78              :                            pw_env_type
      79              :    USE pw_methods, ONLY: pw_axpy, &
      80              :                          pw_integrate_function, &
      81              :                          pw_transfer, &
      82              :                          pw_zero
      83              :    USE pw_pool_types, ONLY: pw_pool_p_type, &
      84              :                             pw_pool_type, &
      85              :                             pw_pools_create_pws, &
      86              :                             pw_pools_give_back_pws
      87              :    USE pw_types, ONLY: pw_r3d_rs_type, &
      88              :                        pw_c1d_gs_type, &
      89              :                        pw_r3d_rs_type
      90              :    USE qs_environment_types, ONLY: get_qs_env, &
      91              :                                    qs_environment_type
      92              :    USE qs_kind_types, ONLY: get_qs_kind, &
      93              :                             get_qs_kind_set, &
      94              :                             qs_kind_type
      95              :    USE qs_ks_types, ONLY: get_ks_env, &
      96              :                           qs_ks_env_type
      97              :    USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
      98              :    USE realspace_grid_types, ONLY: map_gaussian_here, &
      99              :                                    realspace_grid_desc_p_type, &
     100              :                                    realspace_grid_type, &
     101              :                                    rs_grid_zero, &
     102              :                                    transfer_rs2pw
     103              :    USE rs_pw_interface, ONLY: density_rs2pw
     104              :    USE task_list_methods, ONLY: rs_copy_to_buffer, &
     105              :                                 rs_distribute_matrix, &
     106              :                                 rs_scatter_matrices
     107              :    USE task_list_types, ONLY: atom_pair_type, &
     108              :                               task_list_type, &
     109              :                               task_type
     110              : 
     111              : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
     112              : 
     113              : #include "./base/base_uses.f90"
     114              : 
     115              :    IMPLICIT NONE
     116              : 
     117              :    PRIVATE
     118              : 
     119              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_collocate_density'
     120              : ! *** Public subroutines ***
     121              : 
     122              :    PUBLIC :: calculate_ppl_grid, &
     123              :              calculate_rho_core, &
     124              :              calculate_lri_rho_elec, &
     125              :              calculate_rho_single_gaussian, &
     126              :              calculate_rho_metal, &
     127              :              calculate_rho_resp_single, &
     128              :              calculate_rho_resp_all, &
     129              :              calculate_rho_elec, &
     130              :              calculate_drho_elec, &
     131              :              calculate_wavefunction, &
     132              :              collocate_function, &
     133              :              calculate_rho_nlcc, &
     134              :              calculate_drho_elec_dR, &
     135              :              calculate_drho_core, &
     136              :              collocate_single_gaussian
     137              : 
     138              :    INTERFACE calculate_rho_core
     139              :       MODULE PROCEDURE calculate_rho_core_r3d_rs
     140              :       MODULE PROCEDURE calculate_rho_core_c1d_gs
     141              :    END INTERFACE
     142              : 
     143              :    INTERFACE calculate_rho_resp_all
     144              :       MODULE PROCEDURE calculate_rho_resp_all_r3d_rs, calculate_rho_resp_all_c1d_gs
     145              :    END INTERFACE
     146              : 
     147              : CONTAINS
     148              : 
     149              : ! **************************************************************************************************
     150              : !> \brief computes the density of the non-linear core correction on the grid
     151              : !> \param rho_nlcc ...
     152              : !> \param qs_env ...
     153              : ! **************************************************************************************************
     154           52 :    SUBROUTINE calculate_rho_nlcc(rho_nlcc, qs_env)
     155              : 
     156              :       TYPE(pw_r3d_rs_type), INTENT(INOUT)                       :: rho_nlcc
     157              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     158              : 
     159              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_rho_nlcc'
     160              : 
     161              :       INTEGER                                            :: atom_a, handle, iatom, iexp_nlcc, ikind, &
     162              :                                                             ithread, j, n, natom, nc, nexp_nlcc, &
     163              :                                                             ni, npme, nthread, subpatch_pattern
     164           52 :       INTEGER, DIMENSION(:), POINTER                     :: atom_list, cores, nct_nlcc
     165              :       LOGICAL                                            :: nlcc
     166              :       REAL(KIND=dp)                                      :: alpha, eps_rho_rspace, radius
     167              :       REAL(KIND=dp), DIMENSION(3)                        :: ra
     168           52 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: alpha_nlcc
     169           52 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: cval_nlcc, pab
     170           52 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     171              :       TYPE(cell_type), POINTER                           :: cell
     172              :       TYPE(dft_control_type), POINTER                    :: dft_control
     173              :       TYPE(gth_potential_type), POINTER                  :: gth_potential
     174           52 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     175              :       TYPE(pw_env_type), POINTER                         :: pw_env
     176              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     177           52 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     178              :       TYPE(realspace_grid_type), POINTER                 :: rs_rho
     179              : 
     180           52 :       CALL timeset(routineN, handle)
     181              : 
     182           52 :       NULLIFY (cell, dft_control, pab, particle_set, atomic_kind_set, &
     183           52 :                qs_kind_set, atom_list, pw_env, rs_rho, auxbas_pw_pool, cores)
     184              : 
     185              :       CALL get_qs_env(qs_env=qs_env, &
     186              :                       atomic_kind_set=atomic_kind_set, &
     187              :                       qs_kind_set=qs_kind_set, &
     188              :                       cell=cell, &
     189              :                       dft_control=dft_control, &
     190              :                       particle_set=particle_set, &
     191           52 :                       pw_env=pw_env)
     192              :       CALL pw_env_get(pw_env, auxbas_rs_grid=rs_rho, &
     193           52 :                       auxbas_pw_pool=auxbas_pw_pool)
     194              :       ! be careful in parallel nsmax is chosen with multigrid in mind!
     195           52 :       CALL rs_grid_zero(rs_rho)
     196              : 
     197           52 :       eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
     198              : 
     199          140 :       DO ikind = 1, SIZE(atomic_kind_set)
     200           88 :          CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom, atom_list=atom_list)
     201           88 :          CALL get_qs_kind(qs_kind_set(ikind), gth_potential=gth_potential)
     202              : 
     203           88 :          IF (.NOT. ASSOCIATED(gth_potential)) CYCLE
     204              :          CALL get_potential(potential=gth_potential, nlcc_present=nlcc, nexp_nlcc=nexp_nlcc, &
     205           88 :                             alpha_nlcc=alpha_nlcc, nct_nlcc=nct_nlcc, cval_nlcc=cval_nlcc)
     206              : 
     207           88 :          IF (.NOT. nlcc) CYCLE
     208              : 
     209          368 :          DO iexp_nlcc = 1, nexp_nlcc
     210              : 
     211           70 :             alpha = alpha_nlcc(iexp_nlcc)
     212           70 :             nc = nct_nlcc(iexp_nlcc)
     213              : 
     214           70 :             ni = ncoset(2*nc - 2)
     215          210 :             ALLOCATE (pab(ni, 1))
     216          354 :             pab = 0._dp
     217              : 
     218           70 :             nthread = 1
     219           70 :             ithread = 0
     220              : 
     221           70 :             CALL reallocate(cores, 1, natom)
     222           70 :             npme = 0
     223          264 :             cores = 0
     224              : 
     225              :             ! prepare core function
     226          156 :             DO j = 1, nc
     227           70 :                SELECT CASE (j)
     228              :                CASE (1)
     229           70 :                   pab(1, 1) = cval_nlcc(1, iexp_nlcc)
     230              :                CASE (2)
     231           16 :                   n = coset(2, 0, 0)
     232           16 :                   pab(n, 1) = cval_nlcc(2, iexp_nlcc)/alpha**2
     233           16 :                   n = coset(0, 2, 0)
     234           16 :                   pab(n, 1) = cval_nlcc(2, iexp_nlcc)/alpha**2
     235           16 :                   n = coset(0, 0, 2)
     236           16 :                   pab(n, 1) = cval_nlcc(2, iexp_nlcc)/alpha**2
     237              :                CASE (3)
     238            0 :                   n = coset(4, 0, 0)
     239            0 :                   pab(n, 1) = cval_nlcc(3, iexp_nlcc)/alpha**4
     240            0 :                   n = coset(0, 4, 0)
     241            0 :                   pab(n, 1) = cval_nlcc(3, iexp_nlcc)/alpha**4
     242            0 :                   n = coset(0, 0, 4)
     243            0 :                   pab(n, 1) = cval_nlcc(3, iexp_nlcc)/alpha**4
     244            0 :                   n = coset(2, 2, 0)
     245            0 :                   pab(n, 1) = 2._dp*cval_nlcc(3, iexp_nlcc)/alpha**4
     246            0 :                   n = coset(2, 0, 2)
     247            0 :                   pab(n, 1) = 2._dp*cval_nlcc(3, iexp_nlcc)/alpha**4
     248            0 :                   n = coset(0, 2, 2)
     249            0 :                   pab(n, 1) = 2._dp*cval_nlcc(3, iexp_nlcc)/alpha**4
     250              :                CASE (4)
     251            0 :                   n = coset(6, 0, 0)
     252            0 :                   pab(n, 1) = cval_nlcc(4, iexp_nlcc)/alpha**6
     253            0 :                   n = coset(0, 6, 0)
     254            0 :                   pab(n, 1) = cval_nlcc(4, iexp_nlcc)/alpha**6
     255            0 :                   n = coset(0, 0, 6)
     256            0 :                   pab(n, 1) = cval_nlcc(4, iexp_nlcc)/alpha**6
     257            0 :                   n = coset(4, 2, 0)
     258            0 :                   pab(n, 1) = 3._dp*cval_nlcc(4, iexp_nlcc)/alpha**6
     259            0 :                   n = coset(4, 0, 2)
     260            0 :                   pab(n, 1) = 3._dp*cval_nlcc(4, iexp_nlcc)/alpha**6
     261            0 :                   n = coset(2, 4, 0)
     262            0 :                   pab(n, 1) = 3._dp*cval_nlcc(4, iexp_nlcc)/alpha**6
     263            0 :                   n = coset(2, 0, 4)
     264            0 :                   pab(n, 1) = 3._dp*cval_nlcc(4, iexp_nlcc)/alpha**6
     265            0 :                   n = coset(0, 4, 2)
     266            0 :                   pab(n, 1) = 3._dp*cval_nlcc(4, iexp_nlcc)/alpha**6
     267            0 :                   n = coset(0, 2, 4)
     268            0 :                   pab(n, 1) = 3._dp*cval_nlcc(4, iexp_nlcc)/alpha**6
     269            0 :                   n = coset(2, 2, 2)
     270            0 :                   pab(n, 1) = 6._dp*cval_nlcc(4, iexp_nlcc)/alpha**6
     271              :                CASE DEFAULT
     272              :                   CALL cp_abort(__LOCATION__, &
     273              :                                 "Only 1, 2, 3, 4 are supported as the "// &
     274           86 :                                 "value of j in calculate_rho_nlcc")
     275              :                END SELECT
     276              :             END DO
     277           70 :             IF (dft_control%nspins == 2) pab = pab*0.5_dp
     278              : 
     279          264 :             DO iatom = 1, natom
     280          194 :                atom_a = atom_list(iatom)
     281          194 :                ra(:) = pbc(particle_set(atom_a)%r, cell)
     282          264 :                IF (rs_rho%desc%parallel .AND. .NOT. rs_rho%desc%distributed) THEN
     283              :                   ! replicated realspace grid, split the atoms up between procs
     284          194 :                   IF (MODULO(iatom, rs_rho%desc%group_size) == rs_rho%desc%my_pos) THEN
     285           97 :                      npme = npme + 1
     286           97 :                      cores(npme) = iatom
     287              :                   END IF
     288              :                ELSE
     289            0 :                   npme = npme + 1
     290            0 :                   cores(npme) = iatom
     291              :                END IF
     292              :             END DO
     293              : 
     294          167 :             DO j = 1, npme
     295              : 
     296           97 :                iatom = cores(j)
     297           97 :                atom_a = atom_list(iatom)
     298           97 :                ra(:) = pbc(particle_set(atom_a)%r, cell)
     299           97 :                subpatch_pattern = 0
     300           97 :                ni = 2*nc - 2
     301              :                radius = exp_radius_very_extended(la_min=0, la_max=ni, lb_min=0, lb_max=0, &
     302              :                                                  ra=ra, rb=ra, rp=ra, &
     303              :                                                  zetp=1/(2*alpha**2), eps=eps_rho_rspace, &
     304              :                                                  pab=pab, o1=0, o2=0, &  ! without map_consistent
     305           97 :                                                  prefactor=1.0_dp, cutoff=0.0_dp)
     306              : 
     307              :                CALL collocate_pgf_product(ni, 1/(2*alpha**2), 0, 0, 0.0_dp, 0, ra, &
     308              :                                           [0.0_dp, 0.0_dp, 0.0_dp], 1.0_dp, pab, 0, 0, rs_rho, &
     309              :                                           ga_gb_function=GRID_FUNC_AB, radius=radius, &
     310          167 :                                           use_subpatch=.TRUE., subpatch_pattern=subpatch_pattern)
     311              : 
     312              :             END DO
     313              : 
     314          158 :             DEALLOCATE (pab)
     315              : 
     316              :          END DO
     317              : 
     318              :       END DO
     319              : 
     320           52 :       IF (ASSOCIATED(cores)) THEN
     321           52 :          DEALLOCATE (cores)
     322              :       END IF
     323              : 
     324           52 :       CALL transfer_rs2pw(rs_rho, rho_nlcc)
     325              : 
     326           52 :       CALL timestop(handle)
     327              : 
     328           52 :    END SUBROUTINE calculate_rho_nlcc
     329              : 
     330              : ! **************************************************************************************************
     331              : !> \brief computes the local pseudopotential (without erf term) on the grid
     332              : !> \param vppl ...
     333              : !> \param qs_env ...
     334              : ! **************************************************************************************************
     335           12 :    SUBROUTINE calculate_ppl_grid(vppl, qs_env)
     336              : 
     337              :       TYPE(pw_r3d_rs_type), INTENT(INOUT)                       :: vppl
     338              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     339              : 
     340              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_ppl_grid'
     341              : 
     342              :       INTEGER                                            :: atom_a, handle, iatom, ikind, ithread, &
     343              :                                                             j, lppl, n, natom, ni, npme, nthread, &
     344              :                                                             subpatch_pattern
     345           12 :       INTEGER, DIMENSION(:), POINTER                     :: atom_list, cores
     346              :       REAL(KIND=dp)                                      :: alpha, eps_rho_rspace, radius
     347              :       REAL(KIND=dp), DIMENSION(3)                        :: ra
     348           12 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: cexp_ppl
     349           12 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: pab
     350           12 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     351              :       TYPE(cell_type), POINTER                           :: cell
     352              :       TYPE(dft_control_type), POINTER                    :: dft_control
     353              :       TYPE(gth_potential_type), POINTER                  :: gth_potential
     354           12 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     355              :       TYPE(pw_env_type), POINTER                         :: pw_env
     356              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     357           12 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     358              :       TYPE(realspace_grid_type), POINTER                 :: rs_rho
     359              : 
     360           12 :       CALL timeset(routineN, handle)
     361              : 
     362           12 :       NULLIFY (cell, dft_control, pab, atomic_kind_set, qs_kind_set, particle_set, &
     363           12 :                atom_list, pw_env, rs_rho, auxbas_pw_pool, cores)
     364              : 
     365              :       CALL get_qs_env(qs_env=qs_env, &
     366              :                       atomic_kind_set=atomic_kind_set, &
     367              :                       qs_kind_set=qs_kind_set, &
     368              :                       cell=cell, &
     369              :                       dft_control=dft_control, &
     370              :                       particle_set=particle_set, &
     371           12 :                       pw_env=pw_env)
     372              :       CALL pw_env_get(pw_env, auxbas_rs_grid=rs_rho, &
     373           12 :                       auxbas_pw_pool=auxbas_pw_pool)
     374              :       ! be careful in parallel nsmax is chosen with multigrid in mind!
     375           12 :       CALL rs_grid_zero(rs_rho)
     376              : 
     377           12 :       eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
     378              : 
     379           28 :       DO ikind = 1, SIZE(atomic_kind_set)
     380           16 :          CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom, atom_list=atom_list)
     381           16 :          CALL get_qs_kind(qs_kind_set(ikind), gth_potential=gth_potential)
     382              : 
     383           16 :          IF (.NOT. ASSOCIATED(gth_potential)) CYCLE
     384           16 :          CALL get_potential(potential=gth_potential, alpha_ppl=alpha, nexp_ppl=lppl, cexp_ppl=cexp_ppl)
     385              : 
     386           16 :          IF (lppl <= 0) CYCLE
     387              : 
     388           16 :          ni = ncoset(2*lppl - 2)
     389           48 :          ALLOCATE (pab(ni, 1))
     390          192 :          pab = 0._dp
     391              : 
     392           16 :          nthread = 1
     393           16 :          ithread = 0
     394              : 
     395           16 :          CALL reallocate(cores, 1, natom)
     396           16 :          npme = 0
     397           60 :          cores = 0
     398              : 
     399              :          ! prepare core function
     400           48 :          DO j = 1, lppl
     401           16 :             SELECT CASE (j)
     402              :             CASE (1)
     403           16 :                pab(1, 1) = cexp_ppl(1)
     404              :             CASE (2)
     405           16 :                n = coset(2, 0, 0)
     406           16 :                pab(n, 1) = cexp_ppl(2)
     407           16 :                n = coset(0, 2, 0)
     408           16 :                pab(n, 1) = cexp_ppl(2)
     409           16 :                n = coset(0, 0, 2)
     410           16 :                pab(n, 1) = cexp_ppl(2)
     411              :             CASE (3)
     412            0 :                n = coset(4, 0, 0)
     413            0 :                pab(n, 1) = cexp_ppl(3)
     414            0 :                n = coset(0, 4, 0)
     415            0 :                pab(n, 1) = cexp_ppl(3)
     416            0 :                n = coset(0, 0, 4)
     417            0 :                pab(n, 1) = cexp_ppl(3)
     418            0 :                n = coset(2, 2, 0)
     419            0 :                pab(n, 1) = 2._dp*cexp_ppl(3)
     420            0 :                n = coset(2, 0, 2)
     421            0 :                pab(n, 1) = 2._dp*cexp_ppl(3)
     422            0 :                n = coset(0, 2, 2)
     423            0 :                pab(n, 1) = 2._dp*cexp_ppl(3)
     424              :             CASE (4)
     425            0 :                n = coset(6, 0, 0)
     426            0 :                pab(n, 1) = cexp_ppl(4)
     427            0 :                n = coset(0, 6, 0)
     428            0 :                pab(n, 1) = cexp_ppl(4)
     429            0 :                n = coset(0, 0, 6)
     430            0 :                pab(n, 1) = cexp_ppl(4)
     431            0 :                n = coset(4, 2, 0)
     432            0 :                pab(n, 1) = 3._dp*cexp_ppl(4)
     433            0 :                n = coset(4, 0, 2)
     434            0 :                pab(n, 1) = 3._dp*cexp_ppl(4)
     435            0 :                n = coset(2, 4, 0)
     436            0 :                pab(n, 1) = 3._dp*cexp_ppl(4)
     437            0 :                n = coset(2, 0, 4)
     438            0 :                pab(n, 1) = 3._dp*cexp_ppl(4)
     439            0 :                n = coset(0, 4, 2)
     440            0 :                pab(n, 1) = 3._dp*cexp_ppl(4)
     441            0 :                n = coset(0, 2, 4)
     442            0 :                pab(n, 1) = 3._dp*cexp_ppl(4)
     443            0 :                n = coset(2, 2, 2)
     444            0 :                pab(n, 1) = 6._dp*cexp_ppl(4)
     445              :             CASE DEFAULT
     446              :                CALL cp_abort(__LOCATION__, &
     447              :                              "Only 1, 2, 3, 4 are supported as the "// &
     448           32 :                              "value of j in calculate_ppl_grid")
     449              :             END SELECT
     450              :          END DO
     451              : 
     452           60 :          DO iatom = 1, natom
     453           44 :             atom_a = atom_list(iatom)
     454           44 :             ra(:) = pbc(particle_set(atom_a)%r, cell)
     455           60 :             IF (rs_rho%desc%parallel .AND. .NOT. rs_rho%desc%distributed) THEN
     456              :                ! replicated realspace grid, split the atoms up between procs
     457           44 :                IF (MODULO(iatom, rs_rho%desc%group_size) == rs_rho%desc%my_pos) THEN
     458           22 :                   npme = npme + 1
     459           22 :                   cores(npme) = iatom
     460              :                END IF
     461              :             ELSE
     462            0 :                npme = npme + 1
     463            0 :                cores(npme) = iatom
     464              :             END IF
     465              :          END DO
     466              : 
     467           16 :          IF (npme > 0) THEN
     468           36 :             DO j = 1, npme
     469              : 
     470           22 :                iatom = cores(j)
     471           22 :                atom_a = atom_list(iatom)
     472           22 :                ra(:) = pbc(particle_set(atom_a)%r, cell)
     473           22 :                subpatch_pattern = 0
     474           22 :                ni = 2*lppl - 2
     475              : 
     476              :                radius = exp_radius_very_extended(la_min=0, la_max=ni, &
     477              :                                                  lb_min=0, lb_max=0, &
     478              :                                                  ra=ra, rb=ra, rp=ra, &
     479              :                                                  zetp=alpha, eps=eps_rho_rspace, &
     480              :                                                  pab=pab, o1=0, o2=0, &  ! without map_consistent
     481           22 :                                                  prefactor=1.0_dp, cutoff=0.0_dp)
     482              : 
     483              :                CALL collocate_pgf_product(ni, alpha, 0, 0, 0.0_dp, 0, ra, &
     484              :                                           [0.0_dp, 0.0_dp, 0.0_dp], 1.0_dp, pab, 0, 0, rs_rho, &
     485              :                                           radius=radius, ga_gb_function=GRID_FUNC_AB, &
     486           36 :                                           use_subpatch=.TRUE., subpatch_pattern=subpatch_pattern)
     487              : 
     488              :             END DO
     489              :          END IF
     490              : 
     491           60 :          DEALLOCATE (pab)
     492              : 
     493              :       END DO
     494              : 
     495           12 :       IF (ASSOCIATED(cores)) THEN
     496           12 :          DEALLOCATE (cores)
     497              :       END IF
     498              : 
     499           12 :       CALL transfer_rs2pw(rs_rho, vppl)
     500              : 
     501           12 :       CALL timestop(handle)
     502              : 
     503           12 :    END SUBROUTINE calculate_ppl_grid
     504              : 
     505              : ! **************************************************************************************************
     506              : !> \brief Collocates the fitted lri density on a grid.
     507              : !> \param lri_rho_g ...
     508              : !> \param lri_rho_r ...
     509              : !> \param qs_env ...
     510              : !> \param lri_coef ...
     511              : !> \param total_rho ...
     512              : !> \param basis_type ...
     513              : !> \param exact_1c_terms ...
     514              : !> \param pmat replicated block diagonal density matrix (optional)
     515              : !> \param atomlist list of atoms to be included (optional)
     516              : !> \par History
     517              : !>      04.2013
     518              : !> \author Dorothea Golze
     519              : ! **************************************************************************************************
     520         1170 :    SUBROUTINE calculate_lri_rho_elec(lri_rho_g, lri_rho_r, qs_env, &
     521         1170 :                                      lri_coef, total_rho, basis_type, exact_1c_terms, pmat, atomlist)
     522              : 
     523              :       TYPE(pw_c1d_gs_type), INTENT(INOUT) :: lri_rho_g
     524              :       TYPE(pw_r3d_rs_type), INTENT(INOUT)                       ::  lri_rho_r
     525              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     526              :       TYPE(lri_kind_type), DIMENSION(:), POINTER         :: lri_coef
     527              :       REAL(KIND=dp), INTENT(OUT)                         :: total_rho
     528              :       CHARACTER(len=*), INTENT(IN)                       :: basis_type
     529              :       LOGICAL, INTENT(IN)                                :: exact_1c_terms
     530              :       TYPE(dbcsr_type), OPTIONAL                         :: pmat
     531              :       INTEGER, DIMENSION(:), OPTIONAL                    :: atomlist
     532              : 
     533              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_lri_rho_elec'
     534              : 
     535              :       INTEGER :: atom_a, group_size, handle, iatom, igrid_level, ikind, ipgf, iset, jpgf, jset, &
     536              :                  m1, maxco, maxsgf_set, my_pos, na1, natom, nb1, ncoa, ncob, nseta, offset, sgfa, sgfb
     537         1170 :       INTEGER, DIMENSION(:), POINTER                     :: atom_list, la_max, la_min, npgfa, nsgfa
     538         1170 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa
     539              :       LOGICAL                                            :: found
     540         1170 :       LOGICAL, ALLOCATABLE, DIMENSION(:)                 :: map_it
     541         1170 :       LOGICAL, ALLOCATABLE, DIMENSION(:, :)              :: map_it2
     542              :       REAL(KIND=dp)                                      :: eps_rho_rspace, radius, zetp
     543              :       REAL(KIND=dp), DIMENSION(3)                        :: ra
     544         1170 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: aci
     545         1170 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: p_block, pab, sphi_a, work, zeta
     546         1170 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     547              :       TYPE(cell_type), POINTER                           :: cell
     548              :       TYPE(dft_control_type), POINTER                    :: dft_control
     549              :       TYPE(gridlevel_info_type), POINTER                 :: gridlevel_info
     550              :       TYPE(gto_basis_set_type), POINTER                  :: lri_basis_set, orb_basis_set
     551         1170 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     552              :       TYPE(pw_env_type), POINTER                         :: pw_env
     553         1170 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
     554         1170 :       TYPE(pw_c1d_gs_type), ALLOCATABLE, DIMENSION(:) :: mgrid_gspace
     555         1170 :       TYPE(pw_r3d_rs_type), ALLOCATABLE, DIMENSION(:)           ::  mgrid_rspace
     556         1170 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     557         1170 :       TYPE(realspace_grid_type), DIMENSION(:), POINTER   :: rs_rho
     558              :       TYPE(realspace_grid_type), POINTER                 :: rs_grid
     559              : 
     560         1170 :       NULLIFY (aci, atomic_kind_set, qs_kind_set, atom_list, cell, &
     561         1170 :                dft_control, first_sgfa, gridlevel_info, la_max, &
     562         1170 :                la_min, lri_basis_set, npgfa, nsgfa, &
     563         1170 :                pab, particle_set, pw_env, pw_pools, rs_grid, rs_rho, sphi_a, &
     564         1170 :                work, zeta)
     565              : 
     566         1170 :       CALL timeset(routineN, handle)
     567              : 
     568         1170 :       IF (exact_1c_terms) THEN
     569           48 :          CPASSERT(PRESENT(pmat))
     570              :       END IF
     571              : 
     572              :       CALL get_qs_env(qs_env=qs_env, qs_kind_set=qs_kind_set, &
     573              :                       atomic_kind_set=atomic_kind_set, &
     574              :                       cell=cell, particle_set=particle_set, &
     575              :                       pw_env=pw_env, &
     576         1170 :                       dft_control=dft_control)
     577              : 
     578         1170 :       eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
     579         1170 :       gridlevel_info => pw_env%gridlevel_info
     580              : 
     581              :       ! *** set up the pw multi-grids *** !
     582         1170 :       CPASSERT(ASSOCIATED(pw_env))
     583         1170 :       CALL pw_env_get(pw_env=pw_env, rs_grids=rs_rho, pw_pools=pw_pools)
     584              : 
     585         1170 :       CALL pw_pools_create_pws(pw_pools, mgrid_rspace)
     586              : 
     587         1170 :       CALL pw_pools_create_pws(pw_pools, mgrid_gspace)
     588              : 
     589              :       ! *** set up the rs multi-grids *** !
     590         5790 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
     591         5790 :          CALL rs_grid_zero(rs_rho(igrid_level))
     592              :       END DO
     593              : 
     594              :       !take maxco from the LRI basis set!
     595              :       CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
     596         1170 :                            maxco=maxco, basis_type=basis_type)
     597              : 
     598         3510 :       ALLOCATE (pab(maxco, 1))
     599         1170 :       offset = 0
     600         1170 :       my_pos = mgrid_rspace(1)%pw_grid%para%group%mepos
     601         1170 :       group_size = mgrid_rspace(1)%pw_grid%para%group%num_pe
     602              : 
     603         3472 :       DO ikind = 1, SIZE(atomic_kind_set)
     604              : 
     605         2302 :          CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom, atom_list=atom_list)
     606         2302 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=lri_basis_set, basis_type=basis_type)
     607              : 
     608              :          !Take the lri basis set here!
     609              :          CALL get_gto_basis_set(gto_basis_set=lri_basis_set, lmax=la_max, &
     610              :                                 lmin=la_min, zet=zeta, nset=nseta, npgf=npgfa, &
     611         2302 :                                 sphi=sphi_a, first_sgf=first_sgfa, nsgf_set=nsgfa)
     612              : 
     613        10442 :          DO iatom = 1, natom
     614         4668 :             atom_a = atom_list(iatom)
     615         4668 :             IF (PRESENT(ATOMLIST)) THEN
     616         1260 :                IF (atomlist(atom_a) == 0) CYCLE
     617              :             END IF
     618         4088 :             ra(:) = pbc(particle_set(atom_a)%r, cell)
     619         4088 :             aci => lri_coef(ikind)%acoef(iatom, :)
     620              : 
     621        65256 :             m1 = MAXVAL(npgfa(1:nseta))
     622        12264 :             ALLOCATE (map_it(m1))
     623        65256 :             DO iset = 1, nseta
     624              :                ! collocate this set locally?
     625        61168 :                map_it = .FALSE.
     626       127708 :                DO ipgf = 1, npgfa(iset)
     627        66540 :                   igrid_level = gaussian_gridlevel(gridlevel_info, zeta(ipgf, iset))
     628        66540 :                   rs_grid => rs_rho(igrid_level)
     629       127708 :                   map_it(ipgf) = map_gaussian_here(rs_grid, cell%h_inv, ra, offset, group_size, my_pos)
     630              :                END DO
     631        61168 :                offset = offset + 1
     632              : 
     633        98526 :                IF (ANY(map_it(1:npgfa(iset)))) THEN
     634        30584 :                   sgfa = first_sgfa(1, iset)
     635        30584 :                   ncoa = npgfa(iset)*ncoset(la_max(iset))
     636        30584 :                   m1 = sgfa + nsgfa(iset) - 1
     637        91752 :                   ALLOCATE (work(nsgfa(iset), 1))
     638       464884 :                   work(1:nsgfa(iset), 1) = aci(sgfa:m1)
     639       845985 :                   pab = 0._dp
     640              : 
     641              :                   CALL dgemm("N", "N", ncoa, 1, nsgfa(iset), 1.0_dp, lri_basis_set%sphi(1, sgfa), &
     642              :                              SIZE(lri_basis_set%sphi, 1), work(1, 1), SIZE(work, 1), 0.0_dp, pab(1, 1), &
     643        30584 :                              SIZE(pab, 1))
     644              : 
     645        63854 :                   DO ipgf = 1, npgfa(iset)
     646        33270 :                      na1 = (ipgf - 1)*ncoset(la_max(iset))
     647        33270 :                      igrid_level = gaussian_gridlevel(gridlevel_info, zeta(ipgf, iset))
     648        33270 :                      rs_grid => rs_rho(igrid_level)
     649        63854 :                      IF (map_it(ipgf)) THEN
     650              :                         radius = exp_radius_very_extended(la_min=la_min(iset), la_max=la_max(iset), &
     651              :                                                           lb_min=0, lb_max=0, &
     652              :                                                           ra=ra, rb=ra, rp=ra, &
     653              :                                                           zetp=zeta(ipgf, iset), eps=eps_rho_rspace, &
     654        33270 :                                                           prefactor=1.0_dp, cutoff=1.0_dp)
     655              : 
     656              :                         CALL collocate_pgf_product(la_max=la_max(iset), &
     657              :                                                    zeta=zeta(ipgf, iset), &
     658              :                                                    la_min=la_min(iset), &
     659              :                                                    lb_max=0, zetb=0.0_dp, lb_min=0, &
     660              :                                                    ra=ra, rab=[0.0_dp, 0.0_dp, 0.0_dp], &
     661              :                                                    scale=1._dp, &
     662              :                                                    pab=pab, o1=na1, o2=0, &
     663              :                                                    rsgrid=rs_grid, &
     664              :                                                    radius=radius, &
     665        33270 :                                                    ga_gb_function=GRID_FUNC_AB)
     666              :                      END IF
     667              :                   END DO
     668        30584 :                   DEALLOCATE (work)
     669              :                END IF
     670              :             END DO
     671         6970 :             DEALLOCATE (map_it)
     672              :          END DO
     673              :       END DO
     674              : 
     675         1170 :       DEALLOCATE (pab)
     676              : 
     677              :       ! process the one-center terms
     678         1170 :       IF (exact_1c_terms) THEN
     679              :          ! find maximum numbers
     680           48 :          offset = 0
     681              :          CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
     682              :                               maxco=maxco, &
     683              :                               maxsgf_set=maxsgf_set, &
     684           48 :                               basis_type="ORB")
     685          336 :          ALLOCATE (pab(maxco, maxco), work(maxco, maxsgf_set))
     686              : 
     687          144 :          DO ikind = 1, SIZE(atomic_kind_set)
     688           96 :             CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom, atom_list=atom_list)
     689           96 :             CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, basis_type="ORB")
     690              :             CALL get_gto_basis_set(gto_basis_set=orb_basis_set, lmax=la_max, &
     691              :                                    lmin=la_min, zet=zeta, nset=nseta, npgf=npgfa, &
     692           96 :                                    sphi=sphi_a, first_sgf=first_sgfa, nsgf_set=nsgfa)
     693          528 :             DO iatom = 1, natom
     694          288 :                atom_a = atom_list(iatom)
     695          288 :                ra(:) = pbc(particle_set(atom_a)%r, cell)
     696          288 :                CALL dbcsr_get_block_p(matrix=pmat, row=atom_a, col=atom_a, BLOCK=p_block, found=found)
     697          576 :                m1 = MAXVAL(npgfa(1:nseta))
     698         1152 :                ALLOCATE (map_it2(m1, m1))
     699          576 :                DO iset = 1, nseta
     700          864 :                   DO jset = 1, nseta
     701              :                      ! processor mappint
     702          288 :                      map_it2 = .FALSE.
     703         2304 :                      DO ipgf = 1, npgfa(iset)
     704        16416 :                         DO jpgf = 1, npgfa(jset)
     705        14112 :                            zetp = zeta(ipgf, iset) + zeta(jpgf, jset)
     706        14112 :                            igrid_level = gaussian_gridlevel(gridlevel_info, zetp)
     707        14112 :                            rs_grid => rs_rho(igrid_level)
     708        16128 :                            map_it2(ipgf, jpgf) = map_gaussian_here(rs_grid, cell%h_inv, ra, offset, group_size, my_pos)
     709              :                         END DO
     710              :                      END DO
     711          288 :                      offset = offset + 1
     712              :                      !
     713         8640 :                      IF (ANY(map_it2(1:npgfa(iset), 1:npgfa(jset)))) THEN
     714          144 :                         ncoa = npgfa(iset)*ncoset(la_max(iset))
     715          144 :                         sgfa = first_sgfa(1, iset)
     716          144 :                         ncob = npgfa(jset)*ncoset(la_max(jset))
     717          144 :                         sgfb = first_sgfa(1, jset)
     718              :                         ! decontract density block
     719              :                         CALL dgemm("N", "N", ncoa, nsgfa(jset), nsgfa(iset), &
     720              :                                    1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
     721              :                                    p_block(sgfa, sgfb), SIZE(p_block, 1), &
     722          144 :                                    0.0_dp, work(1, 1), maxco)
     723              :                         CALL dgemm("N", "T", ncoa, ncob, nsgfa(jset), &
     724              :                                    1.0_dp, work(1, 1), maxco, &
     725              :                                    sphi_a(1, sgfb), SIZE(sphi_a, 1), &
     726          144 :                                    0.0_dp, pab(1, 1), maxco)
     727         1152 :                         DO ipgf = 1, npgfa(iset)
     728         8208 :                            DO jpgf = 1, npgfa(jset)
     729         7056 :                               zetp = zeta(ipgf, iset) + zeta(jpgf, jset)
     730         7056 :                               igrid_level = gaussian_gridlevel(gridlevel_info, zetp)
     731         7056 :                               rs_grid => rs_rho(igrid_level)
     732              : 
     733         7056 :                               na1 = (ipgf - 1)*ncoset(la_max(iset))
     734         7056 :                               nb1 = (jpgf - 1)*ncoset(la_max(jset))
     735              : 
     736         8064 :                               IF (map_it2(ipgf, jpgf)) THEN
     737              :                                  radius = exp_radius_very_extended(la_min=la_min(iset), &
     738              :                                                                    la_max=la_max(iset), &
     739              :                                                                    lb_min=la_min(jset), &
     740              :                                                                    lb_max=la_max(jset), &
     741              :                                                                    ra=ra, rb=ra, rp=ra, &
     742              :                                                                    zetp=zetp, eps=eps_rho_rspace, &
     743         7056 :                                                                    prefactor=1.0_dp, cutoff=1.0_dp)
     744              : 
     745              :                                  CALL collocate_pgf_product( &
     746              :                                     la_max(iset), zeta(ipgf, iset), la_min(iset), &
     747              :                                     la_max(jset), zeta(jpgf, jset), la_min(jset), &
     748              :                                     ra, [0.0_dp, 0.0_dp, 0.0_dp], 1.0_dp, pab, na1, nb1, &
     749              :                                     rs_grid, &
     750         7056 :                                     radius=radius, ga_gb_function=GRID_FUNC_AB)
     751              :                               END IF
     752              :                            END DO
     753              :                         END DO
     754              :                      END IF
     755              :                   END DO
     756              :                END DO
     757          672 :                DEALLOCATE (map_it2)
     758              :                !
     759              :             END DO
     760              :          END DO
     761           96 :          DEALLOCATE (pab, work)
     762              :       END IF
     763              : 
     764         1170 :       CALL pw_zero(lri_rho_g)
     765         1170 :       CALL pw_zero(lri_rho_r)
     766              : 
     767         5790 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
     768         4620 :          CALL pw_zero(mgrid_rspace(igrid_level))
     769              :          CALL transfer_rs2pw(rs=rs_rho(igrid_level), &
     770         5790 :                              pw=mgrid_rspace(igrid_level))
     771              :       END DO
     772              : 
     773         5790 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
     774         4620 :          CALL pw_zero(mgrid_gspace(igrid_level))
     775              :          CALL pw_transfer(mgrid_rspace(igrid_level), &
     776         4620 :                           mgrid_gspace(igrid_level))
     777         5790 :          CALL pw_axpy(mgrid_gspace(igrid_level), lri_rho_g)
     778              :       END DO
     779         1170 :       CALL pw_transfer(lri_rho_g, lri_rho_r)
     780         1170 :       total_rho = pw_integrate_function(lri_rho_r, isign=-1)
     781              : 
     782              :       ! *** give back the multi-grids *** !
     783         1170 :       CALL pw_pools_give_back_pws(pw_pools, mgrid_gspace)
     784         1170 :       CALL pw_pools_give_back_pws(pw_pools, mgrid_rspace)
     785              : 
     786         1170 :       CALL timestop(handle)
     787              : 
     788         3510 :    END SUBROUTINE calculate_lri_rho_elec
     789              : 
     790              :    #:for kind in ["r3d_rs", "c1d_gs"]
     791              : ! **************************************************************************************************
     792              : !> \brief computes the density of the core charges on the grid
     793              : !> \param rho_core ...
     794              : !> \param total_rho ...
     795              : !> \param qs_env ...
     796              : !> \param calpha ...
     797              : !> \param ccore ...
     798              : !> \param only_nopaw ...
     799              : ! **************************************************************************************************
     800        11084 :       SUBROUTINE calculate_rho_core_${kind}$ (rho_core, total_rho, qs_env, calpha, ccore, only_nopaw)
     801              : 
     802              :          TYPE(pw_${kind}$_type), INTENT(INOUT)                       :: rho_core
     803              :          REAL(KIND=dp), INTENT(OUT)                         :: total_rho
     804              :          TYPE(qs_environment_type), POINTER                 :: qs_env
     805              :          REAL(KIND=dp), DIMENSION(:), OPTIONAL              :: calpha, ccore
     806              :          LOGICAL, INTENT(IN), OPTIONAL                      :: only_nopaw
     807              : 
     808              :          CHARACTER(len=*), PARAMETER :: routineN = 'calculate_rho_core'
     809              : 
     810              :          INTEGER                                            :: atom_a, handle, iatom, ikind, ithread, &
     811              :                                                                j, natom, npme, nthread, &
     812              :                                                                subpatch_pattern
     813        11084 :          INTEGER, DIMENSION(:), POINTER                     :: atom_list, cores
     814              :          LOGICAL                                            :: my_only_nopaw, paw_atom
     815              :          REAL(KIND=dp)                                      :: alpha, eps_rho_rspace, radius
     816              :          REAL(KIND=dp), DIMENSION(3)                        :: ra
     817        11084 :          REAL(KIND=dp), DIMENSION(:, :), POINTER            :: pab
     818        11084 :          TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     819              :          TYPE(cell_type), POINTER                           :: cell
     820              :          TYPE(dft_control_type), POINTER                    :: dft_control
     821        11084 :          TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     822              :          TYPE(pw_env_type), POINTER                         :: pw_env
     823              :          TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     824              :          TYPE(pw_r3d_rs_type)                                      :: rhoc_r
     825        11084 :          TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     826              :          TYPE(realspace_grid_type), POINTER                 :: rs_rho
     827              : 
     828        11084 :          CALL timeset(routineN, handle)
     829        11084 :          NULLIFY (cell, dft_control, pab, atomic_kind_set, qs_kind_set, particle_set, &
     830        11084 :                   atom_list, pw_env, rs_rho, auxbas_pw_pool, cores)
     831        11084 :          ALLOCATE (pab(1, 1))
     832              : 
     833        11084 :          my_only_nopaw = .FALSE.
     834        11084 :          IF (PRESENT(only_nopaw)) my_only_nopaw = only_nopaw
     835        11084 :          IF (PRESENT(calpha)) THEN
     836          594 :             CPASSERT(PRESENT(ccore))
     837              :          END IF
     838              : 
     839              :          CALL get_qs_env(qs_env=qs_env, &
     840              :                          atomic_kind_set=atomic_kind_set, &
     841              :                          qs_kind_set=qs_kind_set, &
     842              :                          cell=cell, &
     843              :                          dft_control=dft_control, &
     844              :                          particle_set=particle_set, &
     845        11084 :                          pw_env=pw_env)
     846              :          CALL pw_env_get(pw_env, auxbas_rs_grid=rs_rho, &
     847        11084 :                          auxbas_pw_pool=auxbas_pw_pool)
     848              :          ! be careful in parallel nsmax is chosen with multigrid in mind!
     849        11084 :          CALL rs_grid_zero(rs_rho)
     850              : 
     851        11084 :          eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
     852              : 
     853        30809 :          DO ikind = 1, SIZE(atomic_kind_set)
     854        19725 :             CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom, atom_list=atom_list)
     855        19725 :             IF (PRESENT(calpha)) THEN
     856         1166 :                alpha = calpha(ikind)
     857         1166 :                pab(1, 1) = ccore(ikind)
     858              :             ELSE
     859        18559 :                CALL get_qs_kind(qs_kind_set(ikind), paw_atom=paw_atom)
     860        18559 :                IF (my_only_nopaw .AND. paw_atom) CYCLE
     861              :                CALL get_qs_kind(qs_kind_set(ikind), alpha_core_charge=alpha, &
     862        18361 :                                 ccore_charge=pab(1, 1))
     863              :             END IF
     864              : 
     865        19527 :             IF (my_only_nopaw .AND. paw_atom) CYCLE
     866        19527 :             IF (alpha == 0.0_dp .OR. pab(1, 1) == 0.0_dp) CYCLE
     867              : 
     868        19335 :             nthread = 1
     869        19335 :             ithread = 0
     870              : 
     871        19335 :             CALL reallocate(cores, 1, natom)
     872        19335 :             npme = 0
     873        62854 :             cores = 0
     874              : 
     875        62854 :             DO iatom = 1, natom
     876        62854 :                IF (rs_rho%desc%parallel .AND. .NOT. rs_rho%desc%distributed) THEN
     877              :                   ! replicated realspace grid, split the atoms up between procs
     878        42688 :                   IF (MODULO(iatom, rs_rho%desc%group_size) == rs_rho%desc%my_pos) THEN
     879        21344 :                      npme = npme + 1
     880        21344 :                      cores(npme) = iatom
     881              :                   END IF
     882              :                ELSE
     883          831 :                   npme = npme + 1
     884          831 :                   cores(npme) = iatom
     885              :                END IF
     886              :             END DO
     887              : 
     888        50144 :             IF (npme > 0) THEN
     889        37517 :                DO j = 1, npme
     890              : 
     891        22175 :                   iatom = cores(j)
     892        22175 :                   atom_a = atom_list(iatom)
     893        22175 :                   ra(:) = pbc(particle_set(atom_a)%r, cell)
     894        22175 :                   subpatch_pattern = 0
     895              :                   radius = exp_radius_very_extended(la_min=0, la_max=0, &
     896              :                                                     lb_min=0, lb_max=0, &
     897              :                                                     ra=ra, rb=ra, rp=ra, &
     898              :                                                     zetp=alpha, eps=eps_rho_rspace, &
     899              :                                                     pab=pab, o1=0, o2=0, &  ! without map_consistent
     900        22175 :                                                     prefactor=-1.0_dp, cutoff=0.0_dp)
     901              : 
     902              :                   CALL collocate_pgf_product(0, alpha, 0, 0, 0.0_dp, 0, ra, &
     903              :                                              [0.0_dp, 0.0_dp, 0.0_dp], -1.0_dp, pab, 0, 0, rs_rho, &
     904              :                                              radius=radius, ga_gb_function=GRID_FUNC_AB, &
     905        37517 :                                              use_subpatch=.TRUE., subpatch_pattern=subpatch_pattern)
     906              : 
     907              :                END DO
     908              :             END IF
     909              : 
     910              :          END DO
     911              : 
     912        11084 :          IF (ASSOCIATED(cores)) THEN
     913        11072 :             DEALLOCATE (cores)
     914              :          END IF
     915        11084 :          DEALLOCATE (pab)
     916              : 
     917        11084 :          CALL auxbas_pw_pool%create_pw(rhoc_r)
     918              : 
     919        11084 :          CALL transfer_rs2pw(rs_rho, rhoc_r)
     920              : 
     921        11084 :          total_rho = pw_integrate_function(rhoc_r, isign=-1)
     922              : 
     923        11084 :          CALL pw_transfer(rhoc_r, rho_core)
     924              : 
     925        11084 :          CALL auxbas_pw_pool%give_back_pw(rhoc_r)
     926              : 
     927        11084 :          CALL timestop(handle)
     928              : 
     929        11084 :       END SUBROUTINE calculate_rho_core_${kind}$
     930              :    #:endfor
     931              : 
     932              : ! *****************************************************************************
     933              : !> \brief Computes the derivative of the density of the core charges with
     934              : !>        respect to the nuclear coordinates on the grid.
     935              : !> \param drho_core The resulting density derivative
     936              : !> \param qs_env ...
     937              : !> \param beta Derivative direction
     938              : !> \param lambda Atom index
     939              : !> \note SL November 2014, ED 2021
     940              : ! **************************************************************************************************
     941          216 :    SUBROUTINE calculate_drho_core(drho_core, qs_env, beta, lambda)
     942              : 
     943              :       TYPE(pw_c1d_gs_type), INTENT(INOUT)                       :: drho_core
     944              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     945              :       INTEGER, INTENT(IN)                                :: beta, lambda
     946              : 
     947              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_drho_core'
     948              : 
     949              :       INTEGER                                            :: atom_a, dabqadb_func, handle, iatom, &
     950              :                                                             ikind, ithread, j, natom, npme, &
     951              :                                                             nthread, subpatch_pattern
     952          216 :       INTEGER, DIMENSION(:), POINTER                     :: atom_list, cores
     953              :       REAL(KIND=dp)                                      :: alpha, eps_rho_rspace, radius
     954              :       REAL(KIND=dp), DIMENSION(3)                        :: ra
     955          216 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: pab
     956          216 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     957              :       TYPE(cell_type), POINTER                           :: cell
     958              :       TYPE(dft_control_type), POINTER                    :: dft_control
     959          216 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     960              :       TYPE(pw_env_type), POINTER                         :: pw_env
     961              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     962              :       TYPE(pw_r3d_rs_type)                                      :: rhoc_r
     963          216 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     964              :       TYPE(realspace_grid_type), POINTER                 :: rs_rho
     965              : 
     966          216 :       CALL timeset(routineN, handle)
     967          216 :       NULLIFY (cell, dft_control, pab, atomic_kind_set, qs_kind_set, particle_set, &
     968          216 :                atom_list, pw_env, rs_rho, auxbas_pw_pool, cores)
     969          216 :       ALLOCATE (pab(1, 1))
     970              : 
     971              :       CALL get_qs_env(qs_env=qs_env, &
     972              :                       atomic_kind_set=atomic_kind_set, &
     973              :                       qs_kind_set=qs_kind_set, &
     974              :                       cell=cell, &
     975              :                       dft_control=dft_control, &
     976              :                       particle_set=particle_set, &
     977          216 :                       pw_env=pw_env)
     978              :       CALL pw_env_get(pw_env, auxbas_rs_grid=rs_rho, &
     979          216 :                       auxbas_pw_pool=auxbas_pw_pool)
     980              :       ! be careful in parallel nsmax is chosen with multigrid in mind!
     981          216 :       CALL rs_grid_zero(rs_rho)
     982              : 
     983          216 :       eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
     984              : 
     985          288 :       SELECT CASE (beta)
     986              :       CASE (1)
     987           72 :          dabqadb_func = GRID_FUNC_CORE_X
     988              :       CASE (2)
     989           72 :          dabqadb_func = GRID_FUNC_CORE_Y
     990              :       CASE (3)
     991           72 :          dabqadb_func = GRID_FUNC_CORE_Z
     992              :       CASE DEFAULT
     993          216 :          CPABORT("invalid beta")
     994              :       END SELECT
     995          648 :       DO ikind = 1, SIZE(atomic_kind_set)
     996          432 :          CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom, atom_list=atom_list)
     997              :          CALL get_qs_kind(qs_kind_set(ikind), &
     998          432 :                           alpha_core_charge=alpha, ccore_charge=pab(1, 1))
     999              : 
    1000          432 :          IF (alpha == 0.0_dp .OR. pab(1, 1) == 0.0_dp) CYCLE
    1001              : 
    1002          432 :          nthread = 1
    1003          432 :          ithread = 0
    1004              : 
    1005          432 :          CALL reallocate(cores, 1, natom)
    1006          432 :          npme = 0
    1007         1080 :          cores = 0
    1008              : 
    1009         1080 :          DO iatom = 1, natom
    1010         1080 :             IF (rs_rho%desc%parallel .AND. .NOT. rs_rho%desc%distributed) THEN
    1011              :                ! replicated realspace grid, split the atoms up between procs
    1012          648 :                IF (MODULO(iatom, rs_rho%desc%group_size) == rs_rho%desc%my_pos) THEN
    1013          324 :                   npme = npme + 1
    1014          324 :                   cores(npme) = iatom
    1015              :                END IF
    1016              :             ELSE
    1017            0 :                npme = npme + 1
    1018            0 :                cores(npme) = iatom
    1019              :             END IF
    1020              :          END DO
    1021              : 
    1022         1080 :          IF (npme > 0) THEN
    1023          648 :             DO j = 1, npme
    1024              : 
    1025          324 :                iatom = cores(j)
    1026          324 :                atom_a = atom_list(iatom)
    1027          324 :                IF (atom_a /= lambda) CYCLE
    1028          108 :                ra(:) = pbc(particle_set(atom_a)%r, cell)
    1029          108 :                subpatch_pattern = 0
    1030              :                radius = exp_radius_very_extended(la_min=0, la_max=0, &
    1031              :                                                  lb_min=0, lb_max=0, &
    1032              :                                                  ra=ra, rb=ra, rp=ra, &
    1033              :                                                  zetp=alpha, eps=eps_rho_rspace, &
    1034              :                                                  pab=pab, o1=0, o2=0, &  ! without map_consistent
    1035          108 :                                                  prefactor=-1.0_dp, cutoff=0.0_dp)
    1036              : 
    1037              :                CALL collocate_pgf_product(0, alpha, 0, 0, 0.0_dp, 0, ra, &
    1038              :                                           [0.0_dp, 0.0_dp, 0.0_dp], -1.0_dp, pab, 0, 0, rs_rho, &
    1039              :                                           radius=radius, ga_gb_function=dabqadb_func, &
    1040          648 :                                           use_subpatch=.TRUE., subpatch_pattern=subpatch_pattern)
    1041              : 
    1042              :             END DO
    1043              :          END IF
    1044              : 
    1045              :       END DO
    1046              : 
    1047          216 :       IF (ASSOCIATED(cores)) THEN
    1048          216 :          DEALLOCATE (cores)
    1049              :       END IF
    1050          216 :       DEALLOCATE (pab)
    1051              : 
    1052          216 :       CALL auxbas_pw_pool%create_pw(rhoc_r)
    1053              : 
    1054          216 :       CALL transfer_rs2pw(rs_rho, rhoc_r)
    1055              : 
    1056          216 :       CALL pw_transfer(rhoc_r, drho_core)
    1057              : 
    1058          216 :       CALL auxbas_pw_pool%give_back_pw(rhoc_r)
    1059              : 
    1060          216 :       CALL timestop(handle)
    1061              : 
    1062          216 :    END SUBROUTINE calculate_drho_core
    1063              : 
    1064              : ! **************************************************************************************************
    1065              : !> \brief collocate a single Gaussian on the grid
    1066              : !> \param rho_gb charge density generated by a single gaussian
    1067              : !> \param qs_env qs environment
    1068              : !> \param iatom_in atom index
    1069              : !> \par History
    1070              : !>        12.2011 created
    1071              : !> \author Dorothea Golze
    1072              : ! **************************************************************************************************
    1073            4 :    SUBROUTINE calculate_rho_single_gaussian(rho_gb, qs_env, iatom_in)
    1074              : 
    1075              :       TYPE(pw_c1d_gs_type), INTENT(INOUT)                       :: rho_gb
    1076              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1077              :       INTEGER, INTENT(IN)                                :: iatom_in
    1078              : 
    1079              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_rho_single_gaussian'
    1080              : 
    1081              :       INTEGER                                            :: atom_a, handle, iatom, npme, &
    1082              :                                                             subpatch_pattern
    1083              :       REAL(KIND=dp)                                      :: eps_rho_rspace, radius
    1084              :       REAL(KIND=dp), DIMENSION(3)                        :: ra
    1085            4 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: pab
    1086              :       TYPE(cell_type), POINTER                           :: cell
    1087              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1088              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1089              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1090              :       TYPE(pw_r3d_rs_type)                                      :: rhoc_r
    1091              :       TYPE(realspace_grid_type), POINTER                 :: rs_rho
    1092              : 
    1093            4 :       CALL timeset(routineN, handle)
    1094            4 :       NULLIFY (cell, dft_control, pab, pw_env, rs_rho, auxbas_pw_pool)
    1095              : 
    1096            4 :       ALLOCATE (pab(1, 1))
    1097              : 
    1098              :       CALL get_qs_env(qs_env=qs_env, &
    1099              :                       cell=cell, &
    1100              :                       dft_control=dft_control, &
    1101            4 :                       pw_env=pw_env)
    1102              :       CALL pw_env_get(pw_env, auxbas_rs_grid=rs_rho, &
    1103            4 :                       auxbas_pw_pool=auxbas_pw_pool)
    1104            4 :       CALL rs_grid_zero(rs_rho)
    1105              : 
    1106            4 :       eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
    1107            4 :       pab(1, 1) = 1.0_dp
    1108            4 :       iatom = iatom_in
    1109              : 
    1110            4 :       npme = 0
    1111              : 
    1112            4 :       IF (rs_rho%desc%parallel .AND. .NOT. rs_rho%desc%distributed) THEN
    1113            4 :          IF (MODULO(iatom, rs_rho%desc%group_size) == rs_rho%desc%my_pos) THEN
    1114              :             npme = npme + 1
    1115              :          END IF
    1116              :       ELSE
    1117              :          npme = npme + 1
    1118              :       END IF
    1119              : 
    1120              :       IF (npme > 0) THEN
    1121            2 :          atom_a = qs_env%qmmm_env_qm%image_charge_pot%image_mm_list(iatom)
    1122            2 :          ra(:) = pbc(qs_env%qmmm_env_qm%image_charge_pot%particles_all(atom_a)%r, cell)
    1123            2 :          subpatch_pattern = 0
    1124              :          radius = exp_radius_very_extended(la_min=0, la_max=0, &
    1125              :                                            lb_min=0, lb_max=0, &
    1126              :                                            ra=ra, rb=ra, rp=ra, &
    1127              :                                            zetp=qs_env%qmmm_env_qm%image_charge_pot%eta, &
    1128              :                                            eps=eps_rho_rspace, &
    1129              :                                            pab=pab, o1=0, o2=0, &  ! without map_consistent
    1130            2 :                                            prefactor=1.0_dp, cutoff=0.0_dp)
    1131              : 
    1132              :          CALL collocate_pgf_product(0, qs_env%qmmm_env_qm%image_charge_pot%eta, &
    1133              :                                     0, 0, 0.0_dp, 0, ra, [0.0_dp, 0.0_dp, 0.0_dp], 1.0_dp, pab, 0, 0, rs_rho, &
    1134              :                                     radius=radius, ga_gb_function=GRID_FUNC_AB, &
    1135            2 :                                     use_subpatch=.TRUE., subpatch_pattern=subpatch_pattern)
    1136              :       END IF
    1137              : 
    1138            4 :       DEALLOCATE (pab)
    1139              : 
    1140            4 :       CALL auxbas_pw_pool%create_pw(rhoc_r)
    1141              : 
    1142            4 :       CALL transfer_rs2pw(rs_rho, rhoc_r)
    1143              : 
    1144            4 :       CALL pw_transfer(rhoc_r, rho_gb)
    1145              : 
    1146            4 :       CALL auxbas_pw_pool%give_back_pw(rhoc_r)
    1147              : 
    1148            4 :       CALL timestop(handle)
    1149              : 
    1150            4 :    END SUBROUTINE calculate_rho_single_gaussian
    1151              : 
    1152              : ! **************************************************************************************************
    1153              : !> \brief computes the image charge density on the grid (including coeffcients)
    1154              : !> \param rho_metal image charge density
    1155              : !> \param coeff expansion coefficients of the image charge density, i.e.
    1156              : !>        rho_metal=sum_a c_a*g_a
    1157              : !> \param total_rho_metal total induced image charge density
    1158              : !> \param qs_env qs environment
    1159              : !> \par History
    1160              : !>        01.2012 created
    1161              : !> \author Dorothea Golze
    1162              : ! **************************************************************************************************
    1163           90 :    SUBROUTINE calculate_rho_metal(rho_metal, coeff, total_rho_metal, qs_env)
    1164              : 
    1165              :       TYPE(pw_c1d_gs_type), INTENT(INOUT)                       :: rho_metal
    1166              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: coeff
    1167              :       REAL(KIND=dp), INTENT(OUT), OPTIONAL               :: total_rho_metal
    1168              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1169              : 
    1170              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_rho_metal'
    1171              : 
    1172              :       INTEGER                                            :: atom_a, handle, iatom, j, natom, npme, &
    1173              :                                                             subpatch_pattern
    1174           90 :       INTEGER, DIMENSION(:), POINTER                     :: cores
    1175              :       REAL(KIND=dp)                                      :: eps_rho_rspace, radius
    1176              :       REAL(KIND=dp), DIMENSION(3)                        :: ra
    1177           90 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: pab
    1178              :       TYPE(cell_type), POINTER                           :: cell
    1179              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1180              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1181              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1182              :       TYPE(pw_r3d_rs_type)                                      :: rhoc_r
    1183              :       TYPE(realspace_grid_type), POINTER                 :: rs_rho
    1184              : 
    1185           90 :       CALL timeset(routineN, handle)
    1186              : 
    1187           90 :       NULLIFY (cell, dft_control, pab, pw_env, rs_rho, auxbas_pw_pool, cores)
    1188              : 
    1189           90 :       ALLOCATE (pab(1, 1))
    1190              : 
    1191              :       CALL get_qs_env(qs_env=qs_env, &
    1192              :                       cell=cell, &
    1193              :                       dft_control=dft_control, &
    1194           90 :                       pw_env=pw_env)
    1195              :       CALL pw_env_get(pw_env, auxbas_rs_grid=rs_rho, &
    1196           90 :                       auxbas_pw_pool=auxbas_pw_pool)
    1197           90 :       CALL rs_grid_zero(rs_rho)
    1198              : 
    1199           90 :       eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
    1200           90 :       pab(1, 1) = 1.0_dp
    1201              : 
    1202           90 :       natom = SIZE(qs_env%qmmm_env_qm%image_charge_pot%image_mm_list)
    1203              : 
    1204           90 :       CALL reallocate(cores, 1, natom)
    1205           90 :       npme = 0
    1206          270 :       cores = 0
    1207              : 
    1208          270 :       DO iatom = 1, natom
    1209          270 :          IF (rs_rho%desc%parallel .AND. .NOT. rs_rho%desc%distributed) THEN
    1210          180 :             IF (MODULO(iatom, rs_rho%desc%group_size) == rs_rho%desc%my_pos) THEN
    1211           90 :                npme = npme + 1
    1212           90 :                cores(npme) = iatom
    1213              :             END IF
    1214              :          ELSE
    1215            0 :             npme = npme + 1
    1216            0 :             cores(npme) = iatom
    1217              :          END IF
    1218              :       END DO
    1219              : 
    1220           90 :       IF (npme > 0) THEN
    1221          180 :          DO j = 1, npme
    1222           90 :             iatom = cores(j)
    1223           90 :             atom_a = qs_env%qmmm_env_qm%image_charge_pot%image_mm_list(iatom)
    1224           90 :             ra(:) = pbc(qs_env%qmmm_env_qm%image_charge_pot%particles_all(atom_a)%r, cell)
    1225           90 :             subpatch_pattern = 0
    1226              :             radius = exp_radius_very_extended(la_min=0, la_max=0, &
    1227              :                                               lb_min=0, lb_max=0, &
    1228              :                                               ra=ra, rb=ra, rp=ra, &
    1229              :                                               zetp=qs_env%qmmm_env_qm%image_charge_pot%eta, &
    1230              :                                               eps=eps_rho_rspace, &
    1231              :                                               pab=pab, o1=0, o2=0, &  ! without map_consistent
    1232           90 :                                               prefactor=coeff(iatom), cutoff=0.0_dp)
    1233              : 
    1234              :             CALL collocate_pgf_product( &
    1235              :                0, qs_env%qmmm_env_qm%image_charge_pot%eta, &
    1236              :                0, 0, 0.0_dp, 0, ra, [0.0_dp, 0.0_dp, 0.0_dp], coeff(iatom), pab, 0, 0, rs_rho, &
    1237              :                radius=radius, ga_gb_function=GRID_FUNC_AB, &
    1238          180 :                use_subpatch=.TRUE., subpatch_pattern=subpatch_pattern)
    1239              :          END DO
    1240              :       END IF
    1241              : 
    1242           90 :       DEALLOCATE (pab, cores)
    1243              : 
    1244           90 :       CALL auxbas_pw_pool%create_pw(rhoc_r)
    1245              : 
    1246           90 :       CALL transfer_rs2pw(rs_rho, rhoc_r)
    1247              : 
    1248           90 :       IF (PRESENT(total_rho_metal)) &
    1249              :          !minus sign: account for the fact that rho_metal has opposite sign
    1250           90 :          total_rho_metal = pw_integrate_function(rhoc_r, isign=-1)
    1251              : 
    1252           90 :       CALL pw_transfer(rhoc_r, rho_metal)
    1253           90 :       CALL auxbas_pw_pool%give_back_pw(rhoc_r)
    1254              : 
    1255           90 :       CALL timestop(handle)
    1256              : 
    1257           90 :    END SUBROUTINE calculate_rho_metal
    1258              : 
    1259              : ! **************************************************************************************************
    1260              : !> \brief collocate a single Gaussian on the grid for periodic RESP fitting
    1261              : !> \param rho_gb charge density generated by a single gaussian
    1262              : !> \param qs_env qs environment
    1263              : !> \param eta width of single Gaussian
    1264              : !> \param iatom_in atom index
    1265              : !> \par History
    1266              : !>        06.2012 created
    1267              : !> \author Dorothea Golze
    1268              : ! **************************************************************************************************
    1269           66 :    SUBROUTINE calculate_rho_resp_single(rho_gb, qs_env, eta, iatom_in)
    1270              : 
    1271              :       TYPE(pw_c1d_gs_type), INTENT(INOUT)                       :: rho_gb
    1272              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1273              :       REAL(KIND=dp), INTENT(IN)                          :: eta
    1274              :       INTEGER, INTENT(IN)                                :: iatom_in
    1275              : 
    1276              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_rho_resp_single'
    1277              : 
    1278              :       INTEGER                                            :: handle, iatom, npme, subpatch_pattern
    1279              :       REAL(KIND=dp)                                      :: eps_rho_rspace, radius
    1280              :       REAL(KIND=dp), DIMENSION(3)                        :: ra
    1281           66 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: pab
    1282              :       TYPE(cell_type), POINTER                           :: cell
    1283              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1284           66 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1285              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1286              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1287              :       TYPE(pw_r3d_rs_type)                                      :: rhoc_r
    1288              :       TYPE(realspace_grid_type), POINTER                 :: rs_rho
    1289              : 
    1290           66 :       CALL timeset(routineN, handle)
    1291           66 :       NULLIFY (cell, dft_control, pab, pw_env, rs_rho, auxbas_pw_pool, &
    1292           66 :                particle_set)
    1293              : 
    1294           66 :       ALLOCATE (pab(1, 1))
    1295              : 
    1296              :       CALL get_qs_env(qs_env=qs_env, &
    1297              :                       cell=cell, &
    1298              :                       dft_control=dft_control, &
    1299              :                       particle_set=particle_set, &
    1300           66 :                       pw_env=pw_env)
    1301              :       CALL pw_env_get(pw_env, auxbas_rs_grid=rs_rho, &
    1302           66 :                       auxbas_pw_pool=auxbas_pw_pool)
    1303           66 :       CALL rs_grid_zero(rs_rho)
    1304              : 
    1305           66 :       eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
    1306           66 :       pab(1, 1) = 1.0_dp
    1307           66 :       iatom = iatom_in
    1308              : 
    1309           66 :       npme = 0
    1310              : 
    1311           66 :       IF (rs_rho%desc%parallel .AND. .NOT. rs_rho%desc%distributed) THEN
    1312           66 :          IF (MODULO(iatom, rs_rho%desc%group_size) == rs_rho%desc%my_pos) THEN
    1313              :             npme = npme + 1
    1314              :          END IF
    1315              :       ELSE
    1316              :          npme = npme + 1
    1317              :       END IF
    1318              : 
    1319              :       IF (npme > 0) THEN
    1320           33 :          ra(:) = pbc(particle_set(iatom)%r, cell)
    1321           33 :          subpatch_pattern = 0
    1322              :          radius = exp_radius_very_extended(la_min=0, la_max=0, &
    1323              :                                            lb_min=0, lb_max=0, &
    1324              :                                            ra=ra, rb=ra, rp=ra, &
    1325              :                                            zetp=eta, eps=eps_rho_rspace, &
    1326              :                                            pab=pab, o1=0, o2=0, &  ! without map_consistent
    1327           33 :                                            prefactor=1.0_dp, cutoff=0.0_dp)
    1328              : 
    1329              :          CALL collocate_pgf_product(0, eta, 0, 0, 0.0_dp, 0, ra, &
    1330              :                                     [0.0_dp, 0.0_dp, 0.0_dp], 1.0_dp, pab, 0, 0, rs_rho, &
    1331              :                                     radius=radius, ga_gb_function=GRID_FUNC_AB, &
    1332           33 :                                     use_subpatch=.TRUE., subpatch_pattern=subpatch_pattern)
    1333              :       END IF
    1334              : 
    1335           66 :       DEALLOCATE (pab)
    1336              : 
    1337           66 :       CALL auxbas_pw_pool%create_pw(rhoc_r)
    1338              : 
    1339           66 :       CALL transfer_rs2pw(rs_rho, rhoc_r)
    1340              : 
    1341           66 :       CALL pw_transfer(rhoc_r, rho_gb)
    1342              : 
    1343           66 :       CALL auxbas_pw_pool%give_back_pw(rhoc_r)
    1344              : 
    1345           66 :       CALL timestop(handle)
    1346              : 
    1347           66 :    END SUBROUTINE calculate_rho_resp_single
    1348              : 
    1349              :    #:for kind in ["r3d_rs", "c1d_gs"]
    1350              : ! **************************************************************************************************
    1351              : !> \brief computes the RESP charge density on a grid based on the RESP charges
    1352              : !> \param rho_resp RESP charge density
    1353              : !> \param coeff RESP charges, take care of normalization factor
    1354              : !>        (eta/pi)**1.5 later
    1355              : !> \param natom number of atoms
    1356              : !> \param eta width of single Gaussian
    1357              : !> \param qs_env qs environment
    1358              : !> \par History
    1359              : !>        01.2012 created
    1360              : !> \author Dorothea Golze
    1361              : ! **************************************************************************************************
    1362           24 :       SUBROUTINE calculate_rho_resp_all_${kind}$ (rho_resp, coeff, natom, eta, qs_env)
    1363              : 
    1364              :          TYPE(pw_${kind}$_type), INTENT(INOUT)                       :: rho_resp
    1365              :          REAL(KIND=dp), DIMENSION(:), POINTER               :: coeff
    1366              :          INTEGER, INTENT(IN)                                :: natom
    1367              :          REAL(KIND=dp), INTENT(IN)                          :: eta
    1368              :          TYPE(qs_environment_type), POINTER                 :: qs_env
    1369              : 
    1370              :          CHARACTER(len=*), PARAMETER :: routineN = 'calculate_rho_resp_all'
    1371              : 
    1372              :          INTEGER                                            :: handle, iatom, j, npme, subpatch_pattern
    1373           24 :          INTEGER, DIMENSION(:), POINTER                     :: cores
    1374              :          REAL(KIND=dp)                                      :: eps_rho_rspace, radius
    1375              :          REAL(KIND=dp), DIMENSION(3)                        :: ra
    1376           24 :          REAL(KIND=dp), DIMENSION(:, :), POINTER            :: pab
    1377              :          TYPE(cell_type), POINTER                           :: cell
    1378              :          TYPE(dft_control_type), POINTER                    :: dft_control
    1379           24 :          TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1380              :          TYPE(pw_env_type), POINTER                         :: pw_env
    1381              :          TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1382              :          TYPE(pw_r3d_rs_type)                                      :: rhoc_r
    1383              :          TYPE(realspace_grid_type), POINTER                 :: rs_rho
    1384              : 
    1385           24 :          CALL timeset(routineN, handle)
    1386              : 
    1387           24 :          NULLIFY (cell, cores, dft_control, pab, pw_env, rs_rho, auxbas_pw_pool, &
    1388           24 :                   particle_set)
    1389              : 
    1390           24 :          ALLOCATE (pab(1, 1))
    1391              : 
    1392              :          CALL get_qs_env(qs_env=qs_env, &
    1393              :                          cell=cell, &
    1394              :                          dft_control=dft_control, &
    1395              :                          particle_set=particle_set, &
    1396           24 :                          pw_env=pw_env)
    1397              :          CALL pw_env_get(pw_env, auxbas_rs_grid=rs_rho, &
    1398           24 :                          auxbas_pw_pool=auxbas_pw_pool)
    1399           24 :          CALL rs_grid_zero(rs_rho)
    1400              : 
    1401           24 :          eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
    1402           24 :          pab(1, 1) = 1.0_dp
    1403              : 
    1404           24 :          CALL reallocate(cores, 1, natom)
    1405           24 :          npme = 0
    1406          142 :          cores = 0
    1407              : 
    1408          142 :          DO iatom = 1, natom
    1409          142 :             IF (rs_rho%desc%parallel .AND. .NOT. rs_rho%desc%distributed) THEN
    1410          118 :                IF (MODULO(iatom, rs_rho%desc%group_size) == rs_rho%desc%my_pos) THEN
    1411           59 :                   npme = npme + 1
    1412           59 :                   cores(npme) = iatom
    1413              :                END IF
    1414              :             ELSE
    1415            0 :                npme = npme + 1
    1416            0 :                cores(npme) = iatom
    1417              :             END IF
    1418              :          END DO
    1419              : 
    1420           24 :          IF (npme > 0) THEN
    1421           83 :             DO j = 1, npme
    1422           59 :                iatom = cores(j)
    1423           59 :                ra(:) = pbc(particle_set(iatom)%r, cell)
    1424           59 :                subpatch_pattern = 0
    1425              :                radius = exp_radius_very_extended(la_min=0, la_max=0, &
    1426              :                                                  lb_min=0, lb_max=0, &
    1427              :                                                  ra=ra, rb=ra, rp=ra, &
    1428              :                                                  zetp=eta, eps=eps_rho_rspace, &
    1429              :                                                  pab=pab, o1=0, o2=0, &  ! without map_consistent
    1430           59 :                                                  prefactor=coeff(iatom), cutoff=0.0_dp)
    1431              : 
    1432              :                CALL collocate_pgf_product( &
    1433              :                   0, eta, &
    1434              :                   0, 0, 0.0_dp, 0, ra, [0.0_dp, 0.0_dp, 0.0_dp], coeff(iatom), pab, 0, 0, rs_rho, &
    1435              :                   radius=radius, ga_gb_function=GRID_FUNC_AB, &
    1436           83 :                   use_subpatch=.TRUE., subpatch_pattern=subpatch_pattern)
    1437              :             END DO
    1438              :          END IF
    1439              : 
    1440           24 :          DEALLOCATE (pab, cores)
    1441              : 
    1442           24 :          CALL auxbas_pw_pool%create_pw(rhoc_r)
    1443              : 
    1444           24 :          CALL transfer_rs2pw(rs_rho, rhoc_r)
    1445              : 
    1446           24 :          CALL pw_transfer(rhoc_r, rho_resp)
    1447           24 :          CALL auxbas_pw_pool%give_back_pw(rhoc_r)
    1448              : 
    1449           24 :          CALL timestop(handle)
    1450              : 
    1451           24 :       END SUBROUTINE calculate_rho_resp_all_${kind}$
    1452              :    #:endfor
    1453              : 
    1454              : ! **************************************************************************************************
    1455              : !> \brief computes the density corresponding to a given density matrix on the grid
    1456              : !> \param matrix_p ...
    1457              : !> \param matrix_p_kp ...
    1458              : !> \param rho ...
    1459              : !> \param rho_gspace ...
    1460              : !> \param total_rho ...
    1461              : !> \param ks_env ...
    1462              : !> \param soft_valid ...
    1463              : !> \param compute_tau ...
    1464              : !> \param compute_grad ...
    1465              : !> \param basis_type ...
    1466              : !> \param der_type ...
    1467              : !> \param idir ...
    1468              : !> \param task_list_external ...
    1469              : !> \param pw_env_external ...
    1470              : !> \par History
    1471              : !>      IAB (15-Feb-2010): Added OpenMP parallelisation to task loop
    1472              : !>                         (c) The Numerical Algorithms Group (NAG) Ltd, 2010 on behalf of the HECToR project
    1473              : !>      Anything that is not the default ORB basis_type requires an external_task_list 12.2019, (A.Bussy)
    1474              : !>      Ole Schuett (2020): Migrated to C, see grid_api.F
    1475              : !> \note
    1476              : !>      both rho and rho_gspace contain the new rho
    1477              : !>      (in real and g-space respectively)
    1478              : ! **************************************************************************************************
    1479       235414 :    SUBROUTINE calculate_rho_elec(matrix_p, matrix_p_kp, rho, rho_gspace, total_rho, &
    1480              :                                  ks_env, soft_valid, compute_tau, compute_grad, &
    1481              :                                  basis_type, der_type, idir, task_list_external, pw_env_external)
    1482              : 
    1483              :       TYPE(dbcsr_type), OPTIONAL, TARGET                 :: matrix_p
    1484              :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
    1485              :          POINTER                                         :: matrix_p_kp
    1486              :       TYPE(pw_r3d_rs_type), INTENT(INOUT)                       :: rho
    1487              :       TYPE(pw_c1d_gs_type), INTENT(INOUT) :: rho_gspace
    1488              :       REAL(KIND=dp), INTENT(OUT), OPTIONAL               :: total_rho
    1489              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1490              :       LOGICAL, INTENT(IN), OPTIONAL                      :: soft_valid, compute_tau, compute_grad
    1491              :       CHARACTER(LEN=*), INTENT(IN), OPTIONAL             :: basis_type
    1492              :       INTEGER, INTENT(IN), OPTIONAL                      :: der_type, idir
    1493              :       TYPE(task_list_type), OPTIONAL, POINTER            :: task_list_external
    1494              :       TYPE(pw_env_type), OPTIONAL, POINTER               :: pw_env_external
    1495              : 
    1496              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_rho_elec'
    1497              : 
    1498              :       CHARACTER(LEN=default_string_length)               :: my_basis_type
    1499              :       INTEGER                                            :: ga_gb_function, handle, ilevel, img, &
    1500              :                                                             nimages, nlevels
    1501              :       LOGICAL                                            :: any_distributed, my_compute_grad, &
    1502              :                                                             my_compute_tau, my_soft_valid
    1503       235414 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_images
    1504              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1505              :       TYPE(mp_comm_type)                                 :: group
    1506              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1507       235414 :       TYPE(realspace_grid_type), DIMENSION(:), POINTER   :: rs_rho
    1508              :       TYPE(task_list_type), POINTER                      :: task_list
    1509              : 
    1510       235414 :       CALL timeset(routineN, handle)
    1511              : 
    1512       235414 :       NULLIFY (matrix_images, dft_control, pw_env, rs_rho, task_list)
    1513              : 
    1514              :       ! Figure out which function to collocate.
    1515       235414 :       my_compute_tau = .FALSE.
    1516       235414 :       IF (PRESENT(compute_tau)) my_compute_tau = compute_tau
    1517       235414 :       my_compute_grad = .FALSE.
    1518       235414 :       IF (PRESENT(compute_grad)) my_compute_grad = compute_grad
    1519       235414 :       IF (PRESENT(der_type)) THEN
    1520           84 :          SELECT CASE (der_type)
    1521              :          CASE (orb_s)
    1522           36 :             ga_gb_function = GRID_FUNC_AB
    1523              :          CASE (orb_px)
    1524            0 :             ga_gb_function = GRID_FUNC_DX
    1525              :          CASE (orb_py)
    1526            0 :             ga_gb_function = GRID_FUNC_DY
    1527              :          CASE (orb_pz)
    1528           12 :             ga_gb_function = GRID_FUNC_DZ
    1529              :          CASE (orb_dxy)
    1530            0 :             ga_gb_function = GRID_FUNC_DXDY
    1531              :          CASE (orb_dyz)
    1532            0 :             ga_gb_function = GRID_FUNC_DYDZ
    1533              :          CASE (orb_dzx)
    1534            0 :             ga_gb_function = GRID_FUNC_DZDX
    1535              :          CASE (orb_dx2)
    1536            0 :             ga_gb_function = GRID_FUNC_DXDX
    1537              :          CASE (orb_dy2)
    1538            0 :             ga_gb_function = GRID_FUNC_DYDY
    1539              :          CASE (orb_dz2)
    1540            0 :             ga_gb_function = GRID_FUNC_DZDZ
    1541              :          CASE DEFAULT
    1542           48 :             CPABORT("Unknown der_type")
    1543              :          END SELECT
    1544       235366 :       ELSE IF (my_compute_tau) THEN
    1545         6628 :          ga_gb_function = GRID_FUNC_DADB
    1546       228738 :       ELSE IF (my_compute_grad) THEN
    1547          324 :          CPASSERT(PRESENT(idir))
    1548          432 :          SELECT CASE (idir)
    1549              :          CASE (1)
    1550          108 :             ga_gb_function = GRID_FUNC_DABpADB_X
    1551              :          CASE (2)
    1552          108 :             ga_gb_function = GRID_FUNC_DABpADB_Y
    1553              :          CASE (3)
    1554          108 :             ga_gb_function = GRID_FUNC_DABpADB_Z
    1555              :          CASE DEFAULT
    1556          324 :             CPABORT("invalid idir")
    1557              :          END SELECT
    1558              :       ELSE
    1559       228414 :          ga_gb_function = GRID_FUNC_AB
    1560              :       END IF
    1561              : 
    1562              :       ! Figure out which basis_type to use.
    1563       235414 :       my_basis_type = "ORB"  ! by default, the full density is calculated
    1564       235414 :       IF (PRESENT(basis_type)) my_basis_type = basis_type
    1565       235414 :       CPASSERT(my_basis_type == "ORB" .OR. PRESENT(task_list_external))
    1566              : 
    1567              :       ! Figure out which task_list to use.
    1568       235414 :       my_soft_valid = .FALSE.
    1569       235414 :       IF (PRESENT(soft_valid)) my_soft_valid = soft_valid
    1570       235414 :       IF (PRESENT(task_list_external)) THEN
    1571        45288 :          task_list => task_list_external
    1572       190126 :       ELSEIF (my_soft_valid) THEN
    1573        37096 :          CALL get_ks_env(ks_env, task_list_soft=task_list)
    1574              :       ELSE
    1575       153030 :          CALL get_ks_env(ks_env, task_list=task_list)
    1576              :       END IF
    1577       235414 :       CPASSERT(ASSOCIATED(task_list))
    1578              : 
    1579              :       ! Figure out which pw_env to use.
    1580       235414 :       IF (PRESENT(pw_env_external)) THEN
    1581        24992 :          pw_env => pw_env_external
    1582              :       ELSE
    1583       210422 :          CALL get_ks_env(ks_env, pw_env=pw_env)
    1584              :       END IF
    1585       235414 :       CPASSERT(ASSOCIATED(pw_env))
    1586              : 
    1587              :       ! Get grids.
    1588       235414 :       CALL pw_env_get(pw_env, rs_grids=rs_rho)
    1589       235414 :       nlevels = SIZE(rs_rho)
    1590       235414 :       group = rs_rho(1)%desc%group
    1591              : 
    1592              :       ! Check if any of the grids is distributed.
    1593       235414 :       any_distributed = .FALSE.
    1594      1165838 :       DO ilevel = 1, nlevels
    1595      2095354 :          any_distributed = any_distributed .OR. rs_rho(ilevel)%desc%distributed
    1596              :       END DO
    1597              : 
    1598              :       ! Gather all matrix images in a single array.
    1599       235414 :       CALL get_ks_env(ks_env, dft_control=dft_control)
    1600       235414 :       nimages = dft_control%nimages
    1601      1379514 :       ALLOCATE (matrix_images(nimages))
    1602       235414 :       IF (PRESENT(matrix_p_kp)) THEN
    1603       201126 :          CPASSERT(.NOT. PRESENT(matrix_p))
    1604       840110 :          DO img = 1, nimages
    1605       840110 :             matrix_images(img)%matrix => matrix_p_kp(img)%matrix
    1606              :          END DO
    1607              :       ELSE
    1608        34288 :          CPASSERT(PRESENT(matrix_p) .AND. nimages == 1)
    1609        34288 :          matrix_images(1)%matrix => matrix_p
    1610              :       END IF
    1611              : 
    1612              :       ! Distribute matrix blocks.
    1613       235414 :       IF (any_distributed) THEN
    1614          230 :          CALL rs_scatter_matrices(matrix_images, task_list%pab_buffer, task_list, group)
    1615              :       ELSE
    1616       235184 :          CALL rs_copy_to_buffer(matrix_images, task_list%pab_buffer, task_list)
    1617              :       END IF
    1618       235414 :       DEALLOCATE (matrix_images)
    1619              : 
    1620              :       ! Map all tasks onto the grids
    1621              :       CALL grid_collocate_task_list(task_list=task_list%grid_task_list, &
    1622              :                                     ga_gb_function=ga_gb_function, &
    1623              :                                     pab_blocks=task_list%pab_buffer, &
    1624       235414 :                                     rs_grids=rs_rho)
    1625              : 
    1626              :       ! Merge realspace multi-grids into single planewave grid.
    1627       235414 :       CALL density_rs2pw(pw_env, rs_rho, rho, rho_gspace)
    1628       235414 :       IF (PRESENT(total_rho)) total_rho = pw_integrate_function(rho, isign=-1)
    1629              : 
    1630       235414 :       CALL timestop(handle)
    1631              : 
    1632       235414 :    END SUBROUTINE calculate_rho_elec
    1633              : 
    1634              : ! **************************************************************************************************
    1635              : !> \brief computes the gradient of the density corresponding to a given
    1636              : !>        density matrix on the grid
    1637              : !> \param matrix_p ...
    1638              : !> \param matrix_p_kp ...
    1639              : !> \param drho ...
    1640              : !> \param drho_gspace ...
    1641              : !> \param qs_env ...
    1642              : !> \param soft_valid ...
    1643              : !> \param basis_type ...
    1644              : !> \note  this is an alternative to calculate the gradient through FFTs
    1645              : ! **************************************************************************************************
    1646            0 :    SUBROUTINE calculate_drho_elec(matrix_p, matrix_p_kp, drho, drho_gspace, qs_env, &
    1647              :                                   soft_valid, basis_type)
    1648              : 
    1649              :       TYPE(dbcsr_type), OPTIONAL, TARGET                 :: matrix_p
    1650              :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
    1651              :          POINTER                                         :: matrix_p_kp
    1652              :       TYPE(pw_r3d_rs_type), DIMENSION(3), INTENT(INOUT)         :: drho
    1653              :       TYPE(pw_c1d_gs_type), DIMENSION(3), INTENT(INOUT) :: drho_gspace
    1654              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1655              :       LOGICAL, INTENT(IN), OPTIONAL                      :: soft_valid
    1656              :       CHARACTER(LEN=*), INTENT(IN), OPTIONAL             :: basis_type
    1657              : 
    1658              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_drho_elec'
    1659              : 
    1660              :       CHARACTER(LEN=default_string_length)               :: my_basis_type
    1661              :       INTEGER :: bcol, brow, dabqadb_func, handle, iatom, iatom_old, idir, igrid_level, ikind, &
    1662              :                  ikind_old, img, img_old, ipgf, iset, iset_old, itask, ithread, jatom, jatom_old, jkind, &
    1663              :                  jkind_old, jpgf, jset, jset_old, maxco, maxsgf_set, na1, na2, natoms, nb1, nb2, ncoa, &
    1664              :                  ncob, nimages, nseta, nsetb, ntasks, nthread, sgfa, sgfb
    1665            0 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, lb_max, lb_min, npgfa, &
    1666            0 :                                                             npgfb, nsgfa, nsgfb
    1667            0 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa, first_sgfb
    1668              :       LOGICAL                                            :: atom_pair_changed, distributed_rs_grids, &
    1669              :                                                             do_kp, found, my_soft, use_subpatch
    1670              :       REAL(KIND=dp)                                      :: eps_rho_rspace, f, prefactor, radius, &
    1671              :                                                             scale, zetp
    1672              :       REAL(KIND=dp), DIMENSION(3)                        :: ra, rab, rab_inv, rb, rp
    1673            0 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: p_block, pab, sphi_a, sphi_b, work, &
    1674            0 :                                                             zeta, zetb
    1675            0 :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER         :: pabt, workt
    1676            0 :       TYPE(atom_pair_type), DIMENSION(:), POINTER        :: atom_pair_recv, atom_pair_send
    1677              :       TYPE(cell_type), POINTER                           :: cell
    1678            0 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: deltap
    1679              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1680              :       TYPE(gridlevel_info_type), POINTER                 :: gridlevel_info
    1681              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
    1682              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1683            0 :          POINTER                                         :: sab_orb
    1684            0 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1685              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1686            0 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1687              :       TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
    1688            0 :          POINTER                                         :: rs_descs
    1689            0 :       TYPE(realspace_grid_type), DIMENSION(:), POINTER   :: rs_rho
    1690              :       TYPE(task_list_type), POINTER                      :: task_list, task_list_soft
    1691            0 :       TYPE(task_type), DIMENSION(:), POINTER             :: tasks
    1692              : 
    1693            0 :       CALL timeset(routineN, handle)
    1694              : 
    1695            0 :       CPASSERT(PRESENT(matrix_p) .OR. PRESENT(matrix_p_kp))
    1696            0 :       do_kp = PRESENT(matrix_p_kp)
    1697              : 
    1698            0 :       NULLIFY (cell, dft_control, orb_basis_set, deltap, qs_kind_set, &
    1699            0 :                sab_orb, particle_set, rs_rho, pw_env, rs_descs, la_max, la_min, &
    1700            0 :                lb_max, lb_min, npgfa, npgfb, nsgfa, nsgfb, p_block, sphi_a, &
    1701            0 :                sphi_b, zeta, zetb, first_sgfa, first_sgfb, tasks, pabt, workt)
    1702              : 
    1703              :       ! by default, the full density is calculated
    1704            0 :       my_soft = .FALSE.
    1705            0 :       IF (PRESENT(soft_valid)) my_soft = soft_valid
    1706              : 
    1707            0 :       IF (PRESENT(basis_type)) THEN
    1708            0 :          my_basis_type = basis_type
    1709              :       ELSE
    1710            0 :          my_basis_type = "ORB"
    1711              :       END IF
    1712              : 
    1713              :       CALL get_qs_env(qs_env=qs_env, &
    1714              :                       qs_kind_set=qs_kind_set, &
    1715              :                       cell=cell, &
    1716              :                       dft_control=dft_control, &
    1717              :                       particle_set=particle_set, &
    1718              :                       sab_orb=sab_orb, &
    1719            0 :                       pw_env=pw_env)
    1720              : 
    1721            0 :       SELECT CASE (my_basis_type)
    1722              :       CASE ("ORB")
    1723              :          CALL get_qs_env(qs_env=qs_env, &
    1724              :                          task_list=task_list, &
    1725            0 :                          task_list_soft=task_list_soft)
    1726              :       CASE ("AUX_FIT")
    1727              :          CALL get_qs_env(qs_env=qs_env, &
    1728            0 :                          task_list_soft=task_list_soft)
    1729            0 :          CALL get_admm_env(qs_env%admm_env, task_list_aux_fit=task_list)
    1730              :       END SELECT
    1731              : 
    1732              :       ! *** assign from pw_env
    1733            0 :       gridlevel_info => pw_env%gridlevel_info
    1734              : 
    1735              :       !   *** Allocate work storage ***
    1736            0 :       nthread = 1
    1737              :       CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
    1738              :                            maxco=maxco, &
    1739              :                            maxsgf_set=maxsgf_set, &
    1740            0 :                            basis_type=my_basis_type)
    1741            0 :       CALL reallocate(pabt, 1, maxco, 1, maxco, 0, nthread - 1)
    1742            0 :       CALL reallocate(workt, 1, maxco, 1, maxsgf_set, 0, nthread - 1)
    1743              : 
    1744              :       ! find maximum numbers
    1745            0 :       nimages = dft_control%nimages
    1746            0 :       CPASSERT(nimages == 1 .OR. do_kp)
    1747              : 
    1748            0 :       natoms = SIZE(particle_set)
    1749              : 
    1750              :       ! get the task lists
    1751            0 :       IF (my_soft) task_list => task_list_soft
    1752            0 :       CPASSERT(ASSOCIATED(task_list))
    1753            0 :       tasks => task_list%tasks
    1754            0 :       atom_pair_send => task_list%atom_pair_send
    1755            0 :       atom_pair_recv => task_list%atom_pair_recv
    1756            0 :       ntasks = task_list%ntasks
    1757              : 
    1758              :       ! *** set up the rs multi-grids
    1759            0 :       CPASSERT(ASSOCIATED(pw_env))
    1760            0 :       CALL pw_env_get(pw_env, rs_descs=rs_descs, rs_grids=rs_rho)
    1761            0 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
    1762            0 :          distributed_rs_grids = rs_rho(igrid_level)%desc%distributed
    1763              :       END DO
    1764              : 
    1765            0 :       eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
    1766              : 
    1767              :       !   *** Initialize working density matrix ***
    1768              :       ! distributed rs grids require a matrix that will be changed
    1769              :       ! whereas this is not the case for replicated grids
    1770            0 :       ALLOCATE (deltap(nimages))
    1771            0 :       IF (distributed_rs_grids) THEN
    1772            0 :          DO img = 1, nimages
    1773              :          END DO
    1774              :          ! this matrix has no strict sparsity pattern in parallel
    1775              :          ! deltap%sparsity_id=-1
    1776            0 :          IF (do_kp) THEN
    1777            0 :             DO img = 1, nimages
    1778              :                CALL dbcsr_copy(deltap(img)%matrix, matrix_p_kp(img)%matrix, &
    1779            0 :                                name="DeltaP")
    1780              :             END DO
    1781              :          ELSE
    1782            0 :             CALL dbcsr_copy(deltap(1)%matrix, matrix_p, name="DeltaP")
    1783              :          END IF
    1784              :       ELSE
    1785            0 :          IF (do_kp) THEN
    1786            0 :             DO img = 1, nimages
    1787            0 :                deltap(img)%matrix => matrix_p_kp(img)%matrix
    1788              :             END DO
    1789              :          ELSE
    1790            0 :             deltap(1)%matrix => matrix_p
    1791              :          END IF
    1792              :       END IF
    1793              : 
    1794              :       ! distribute the matrix
    1795            0 :       IF (distributed_rs_grids) THEN
    1796              :          CALL rs_distribute_matrix(rs_descs=rs_descs, pmats=deltap, &
    1797              :                                    atom_pair_send=atom_pair_send, atom_pair_recv=atom_pair_recv, &
    1798            0 :                                    nimages=nimages, scatter=.TRUE.)
    1799              :       END IF
    1800              : 
    1801              :       ! map all tasks on the grids
    1802              : 
    1803            0 :       ithread = 0
    1804            0 :       pab => pabt(:, :, ithread)
    1805            0 :       work => workt(:, :, ithread)
    1806              : 
    1807            0 :       loop_xyz: DO idir = 1, 3
    1808              : 
    1809            0 :          DO igrid_level = 1, gridlevel_info%ngrid_levels
    1810            0 :             CALL rs_grid_zero(rs_rho(igrid_level))
    1811              :          END DO
    1812              : 
    1813              :          iatom_old = -1; jatom_old = -1; iset_old = -1; jset_old = -1
    1814              :          ikind_old = -1; jkind_old = -1; img_old = -1
    1815            0 :          loop_tasks: DO itask = 1, ntasks
    1816              : 
    1817              :             !decode the atom pair and basis info
    1818            0 :             igrid_level = tasks(itask)%grid_level
    1819            0 :             img = tasks(itask)%image
    1820            0 :             iatom = tasks(itask)%iatom
    1821            0 :             jatom = tasks(itask)%jatom
    1822            0 :             iset = tasks(itask)%iset
    1823            0 :             jset = tasks(itask)%jset
    1824            0 :             ipgf = tasks(itask)%ipgf
    1825            0 :             jpgf = tasks(itask)%jpgf
    1826              : 
    1827            0 :             ikind = particle_set(iatom)%atomic_kind%kind_number
    1828            0 :             jkind = particle_set(jatom)%atomic_kind%kind_number
    1829              : 
    1830            0 :             IF (iatom /= iatom_old .OR. jatom /= jatom_old .OR. img /= img_old) THEN
    1831              : 
    1832            0 :                IF (iatom /= iatom_old) ra(:) = pbc(particle_set(iatom)%r, cell)
    1833              : 
    1834            0 :                IF (iatom <= jatom) THEN
    1835            0 :                   brow = iatom
    1836            0 :                   bcol = jatom
    1837              :                ELSE
    1838            0 :                   brow = jatom
    1839            0 :                   bcol = iatom
    1840              :                END IF
    1841              : 
    1842            0 :                IF (ikind /= ikind_old) THEN
    1843            0 :                   IF (my_soft) THEN
    1844              :                      CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, &
    1845            0 :                                       basis_type="ORB_SOFT")
    1846              :                   ELSE
    1847              :                      CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, &
    1848            0 :                                       basis_type=my_basis_type)
    1849              :                   END IF
    1850              :                   CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
    1851              :                                          first_sgf=first_sgfa, &
    1852              :                                          lmax=la_max, &
    1853              :                                          lmin=la_min, &
    1854              :                                          npgf=npgfa, &
    1855              :                                          nset=nseta, &
    1856              :                                          nsgf_set=nsgfa, &
    1857              :                                          sphi=sphi_a, &
    1858            0 :                                          zet=zeta)
    1859              :                END IF
    1860              : 
    1861            0 :                IF (jkind /= jkind_old) THEN
    1862            0 :                   IF (my_soft) THEN
    1863              :                      CALL get_qs_kind(qs_kind_set(jkind), basis_set=orb_basis_set, &
    1864            0 :                                       basis_type="ORB_SOFT")
    1865              :                   ELSE
    1866              :                      CALL get_qs_kind(qs_kind_set(jkind), basis_set=orb_basis_set, &
    1867            0 :                                       basis_type=my_basis_type)
    1868              :                   END IF
    1869              :                   CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
    1870              :                                          first_sgf=first_sgfb, &
    1871              :                                          lmax=lb_max, &
    1872              :                                          lmin=lb_min, &
    1873              :                                          npgf=npgfb, &
    1874              :                                          nset=nsetb, &
    1875              :                                          nsgf_set=nsgfb, &
    1876              :                                          sphi=sphi_b, &
    1877            0 :                                          zet=zetb)
    1878              :                END IF
    1879              : 
    1880              :                CALL dbcsr_get_block_p(matrix=deltap(img)%matrix, &
    1881            0 :                                       row=brow, col=bcol, BLOCK=p_block, found=found)
    1882            0 :                CPASSERT(found)
    1883              : 
    1884              :                iatom_old = iatom
    1885              :                jatom_old = jatom
    1886              :                ikind_old = ikind
    1887              :                jkind_old = jkind
    1888              :                img_old = img
    1889              :                atom_pair_changed = .TRUE.
    1890              : 
    1891              :             ELSE
    1892              : 
    1893              :                atom_pair_changed = .FALSE.
    1894              : 
    1895              :             END IF
    1896              : 
    1897            0 :             IF (atom_pair_changed .OR. iset_old /= iset .OR. jset_old /= jset) THEN
    1898              : 
    1899            0 :                ncoa = npgfa(iset)*ncoset(la_max(iset))
    1900            0 :                sgfa = first_sgfa(1, iset)
    1901            0 :                ncob = npgfb(jset)*ncoset(lb_max(jset))
    1902            0 :                sgfb = first_sgfb(1, jset)
    1903              : 
    1904            0 :                IF (iatom <= jatom) THEN
    1905              :                   CALL dgemm("N", "N", ncoa, nsgfb(jset), nsgfa(iset), &
    1906              :                              1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
    1907              :                              p_block(sgfa, sgfb), SIZE(p_block, 1), &
    1908            0 :                              0.0_dp, work(1, 1), maxco)
    1909              :                   CALL dgemm("N", "T", ncoa, ncob, nsgfb(jset), &
    1910              :                              1.0_dp, work(1, 1), maxco, &
    1911              :                              sphi_b(1, sgfb), SIZE(sphi_b, 1), &
    1912            0 :                              0.0_dp, pab(1, 1), maxco)
    1913              :                ELSE
    1914              :                   CALL dgemm("N", "N", ncob, nsgfa(iset), nsgfb(jset), &
    1915              :                              1.0_dp, sphi_b(1, sgfb), SIZE(sphi_b, 1), &
    1916              :                              p_block(sgfb, sgfa), SIZE(p_block, 1), &
    1917            0 :                              0.0_dp, work(1, 1), maxco)
    1918              :                   CALL dgemm("N", "T", ncob, ncoa, nsgfa(iset), &
    1919              :                              1.0_dp, work(1, 1), maxco, &
    1920              :                              sphi_a(1, sgfa), SIZE(sphi_a, 1), &
    1921            0 :                              0.0_dp, pab(1, 1), maxco)
    1922              :                END IF
    1923              : 
    1924              :                iset_old = iset
    1925              :                jset_old = jset
    1926              : 
    1927              :             END IF
    1928              : 
    1929            0 :             rab(:) = tasks(itask)%rab
    1930            0 :             rb(:) = ra(:) + rab(:)
    1931            0 :             zetp = zeta(ipgf, iset) + zetb(jpgf, jset)
    1932              : 
    1933            0 :             f = zetb(jpgf, jset)/zetp
    1934            0 :             rp(:) = ra(:) + f*rab(:)
    1935            0 :             prefactor = EXP(-zeta(ipgf, iset)*f*DOT_PRODUCT(rab, rab))
    1936              :             radius = exp_radius_very_extended(la_min=la_min(iset), la_max=la_max(iset), &
    1937              :                                               lb_min=lb_min(jset), lb_max=lb_max(jset), &
    1938              :                                               ra=ra, rb=rb, rp=rp, &
    1939              :                                               zetp=zeta(ipgf, iset), eps=eps_rho_rspace, &
    1940            0 :                                               prefactor=prefactor, cutoff=1.0_dp)
    1941              : 
    1942            0 :             na1 = (ipgf - 1)*ncoset(la_max(iset)) + 1
    1943            0 :             na2 = ipgf*ncoset(la_max(iset))
    1944            0 :             nb1 = (jpgf - 1)*ncoset(lb_max(jset)) + 1
    1945            0 :             nb2 = jpgf*ncoset(lb_max(jset))
    1946              : 
    1947              :             ! takes the density matrix symmetry in account, i.e. off-diagonal blocks need to be mapped 'twice'
    1948            0 :             IF (iatom == jatom .AND. img == 1) THEN
    1949            0 :                scale = 1.0_dp
    1950              :             ELSE
    1951            0 :                scale = 2.0_dp
    1952              :             END IF
    1953              : 
    1954              :             ! check whether we need to use fawzi's generalised collocation scheme
    1955            0 :             IF (rs_rho(igrid_level)%desc%distributed) THEN
    1956              :                !tasks(4,:) is 0 for replicated, 1 for distributed 2 for exceptional distributed tasks
    1957            0 :                IF (tasks(itask)%dist_type == 2) THEN
    1958            0 :                   use_subpatch = .TRUE.
    1959              :                ELSE
    1960            0 :                   use_subpatch = .FALSE.
    1961              :                END IF
    1962              :             ELSE
    1963            0 :                use_subpatch = .FALSE.
    1964              :             END IF
    1965              : 
    1966            0 :             SELECT CASE (idir)
    1967              :             CASE (1)
    1968            0 :                dabqadb_func = GRID_FUNC_DABpADB_X
    1969              :             CASE (2)
    1970            0 :                dabqadb_func = GRID_FUNC_DABpADB_Y
    1971              :             CASE (3)
    1972            0 :                dabqadb_func = GRID_FUNC_DABpADB_Z
    1973              :             CASE DEFAULT
    1974            0 :                CPABORT("invalid idir")
    1975              :             END SELECT
    1976              : 
    1977            0 :             IF (iatom <= jatom) THEN
    1978              :                CALL collocate_pgf_product( &
    1979              :                   la_max(iset), zeta(ipgf, iset), la_min(iset), &
    1980              :                   lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
    1981              :                   ra, rab, scale, pab, na1 - 1, nb1 - 1, &
    1982              :                   rs_rho(igrid_level), &
    1983              :                   radius=radius, ga_gb_function=dabqadb_func, &
    1984            0 :                   use_subpatch=use_subpatch, subpatch_pattern=tasks(itask)%subpatch_pattern)
    1985              :             ELSE
    1986            0 :                rab_inv = -rab
    1987              :                CALL collocate_pgf_product( &
    1988              :                   lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
    1989              :                   la_max(iset), zeta(ipgf, iset), la_min(iset), &
    1990              :                   rb, rab_inv, scale, pab, nb1 - 1, na1 - 1, &
    1991              :                   rs_rho(igrid_level), &
    1992              :                   radius=radius, ga_gb_function=dabqadb_func, &
    1993            0 :                   use_subpatch=use_subpatch, subpatch_pattern=tasks(itask)%subpatch_pattern)
    1994              :             END IF
    1995              : 
    1996              :          END DO loop_tasks
    1997              : 
    1998            0 :          CALL density_rs2pw(pw_env, rs_rho, drho(idir), drho_gspace(idir))
    1999              : 
    2000              :       END DO loop_xyz
    2001              : 
    2002              :       !   *** Release work storage ***
    2003            0 :       IF (distributed_rs_grids) THEN
    2004            0 :          CALL dbcsr_deallocate_matrix_set(deltap)
    2005              :       ELSE
    2006            0 :          DO img = 1, nimages
    2007            0 :             NULLIFY (deltap(img)%matrix)
    2008              :          END DO
    2009            0 :          DEALLOCATE (deltap)
    2010              :       END IF
    2011              : 
    2012            0 :       DEALLOCATE (pabt, workt)
    2013              : 
    2014            0 :       CALL timestop(handle)
    2015              : 
    2016            0 :    END SUBROUTINE calculate_drho_elec
    2017              : 
    2018              : ! **************************************************************************************************
    2019              : !> \brief Computes the gradient wrt. nuclear coordinates of a density on the grid
    2020              : !>        The density is given in terms of the density matrix_p
    2021              : !> \param matrix_p Density matrix
    2022              : !> \param matrix_p_kp ...
    2023              : !> \param drho Density gradient on the grid
    2024              : !> \param drho_gspace Density gradient on the reciprocal grid
    2025              : !> \param qs_env ...
    2026              : !> \param soft_valid ...
    2027              : !> \param basis_type ...
    2028              : !> \param beta Derivative direction
    2029              : !> \param lambda Atom index
    2030              : !> \note SL, ED 2021
    2031              : !>       Adapted from calculate_drho_elec
    2032              : ! **************************************************************************************************
    2033          252 :    SUBROUTINE calculate_drho_elec_dR(matrix_p, matrix_p_kp, drho, drho_gspace, qs_env, &
    2034              :                                      soft_valid, basis_type, beta, lambda)
    2035              : 
    2036              :       TYPE(dbcsr_type), OPTIONAL, TARGET                 :: matrix_p
    2037              :       TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
    2038              :          POINTER                                         :: matrix_p_kp
    2039              :       TYPE(pw_r3d_rs_type), INTENT(INOUT)                       :: drho
    2040              :       TYPE(pw_c1d_gs_type), INTENT(INOUT) :: drho_gspace
    2041              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2042              :       LOGICAL, INTENT(IN), OPTIONAL                      :: soft_valid
    2043              :       CHARACTER(LEN=*), INTENT(IN), OPTIONAL             :: basis_type
    2044              :       INTEGER, INTENT(IN)                                :: beta, lambda
    2045              : 
    2046              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_drho_elec_dR'
    2047              : 
    2048              :       CHARACTER(LEN=default_string_length)               :: my_basis_type
    2049              :       INTEGER :: bcol, brow, dabqadb_func, handle, iatom, iatom_old, igrid_level, ikind, &
    2050              :                  ikind_old, img, img_old, ipgf, iset, iset_old, itask, ithread, jatom, jatom_old, jkind, &
    2051              :                  jkind_old, jpgf, jset, jset_old, maxco, maxsgf_set, na1, na2, natoms, nb1, nb2, ncoa, &
    2052              :                  ncob, nimages, nseta, nsetb, ntasks, nthread, sgfa, sgfb
    2053          252 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, lb_max, lb_min, npgfa, &
    2054          252 :                                                             npgfb, nsgfa, nsgfb
    2055          252 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa, first_sgfb
    2056              :       LOGICAL                                            :: atom_pair_changed, distributed_rs_grids, &
    2057              :                                                             do_kp, found, my_soft, use_subpatch
    2058              :       REAL(KIND=dp)                                      :: eps_rho_rspace, f, prefactor, radius, &
    2059              :                                                             scale, zetp
    2060              :       REAL(KIND=dp), DIMENSION(3)                        :: ra, rab, rab_inv, rb, rp
    2061          252 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: p_block, pab, sphi_a, sphi_b, work, &
    2062          252 :                                                             zeta, zetb
    2063          252 :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER         :: pabt, workt
    2064          252 :       TYPE(atom_pair_type), DIMENSION(:), POINTER        :: atom_pair_recv, atom_pair_send
    2065              :       TYPE(cell_type), POINTER                           :: cell
    2066          252 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: deltap
    2067              :       TYPE(dft_control_type), POINTER                    :: dft_control
    2068              :       TYPE(gridlevel_info_type), POINTER                 :: gridlevel_info
    2069              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
    2070          252 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2071              :       TYPE(pw_env_type), POINTER                         :: pw_env
    2072          252 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    2073              :       TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
    2074          252 :          POINTER                                         :: rs_descs
    2075          252 :       TYPE(realspace_grid_type), DIMENSION(:), POINTER   :: rs_rho
    2076              :       TYPE(task_list_type), POINTER                      :: task_list, task_list_soft
    2077          252 :       TYPE(task_type), DIMENSION(:), POINTER             :: tasks
    2078              : 
    2079          252 :       CALL timeset(routineN, handle)
    2080              : 
    2081          252 :       CPASSERT(PRESENT(matrix_p) .OR. PRESENT(matrix_p_kp))
    2082          252 :       do_kp = PRESENT(matrix_p_kp)
    2083              : 
    2084          252 :       NULLIFY (cell, dft_control, orb_basis_set, deltap, qs_kind_set, &
    2085          252 :                particle_set, rs_rho, pw_env, rs_descs, la_max, la_min, lb_max, &
    2086          252 :                lb_min, npgfa, npgfb, nsgfa, nsgfb, p_block, sphi_a, sphi_b, &
    2087          252 :                zeta, zetb, first_sgfa, first_sgfb, tasks, pabt, workt)
    2088              : 
    2089              :       ! by default, the full density is calculated
    2090          252 :       my_soft = .FALSE.
    2091          252 :       IF (PRESENT(soft_valid)) my_soft = soft_valid
    2092              : 
    2093          252 :       IF (PRESENT(basis_type)) THEN
    2094            0 :          my_basis_type = basis_type
    2095              :       ELSE
    2096          252 :          my_basis_type = "ORB"
    2097              :       END IF
    2098              : 
    2099              :       CALL get_qs_env(qs_env=qs_env, &
    2100              :                       qs_kind_set=qs_kind_set, &
    2101              :                       cell=cell, &
    2102              :                       dft_control=dft_control, &
    2103              :                       particle_set=particle_set, &
    2104          252 :                       pw_env=pw_env)
    2105              : 
    2106          252 :       SELECT CASE (my_basis_type)
    2107              :       CASE ("ORB")
    2108              :          CALL get_qs_env(qs_env=qs_env, &
    2109              :                          task_list=task_list, &
    2110          252 :                          task_list_soft=task_list_soft)
    2111              :       CASE ("AUX_FIT")
    2112              :          CALL get_qs_env(qs_env=qs_env, &
    2113            0 :                          task_list_soft=task_list_soft)
    2114          252 :          CALL get_admm_env(qs_env%admm_env, task_list_aux_fit=task_list)
    2115              :       END SELECT
    2116              : 
    2117              :       ! *** assign from pw_env
    2118          252 :       gridlevel_info => pw_env%gridlevel_info
    2119              : 
    2120              :       !   *** Allocate work storage ***
    2121          252 :       nthread = 1
    2122              :       CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
    2123              :                            maxco=maxco, &
    2124              :                            maxsgf_set=maxsgf_set, &
    2125          252 :                            basis_type=my_basis_type)
    2126          252 :       CALL reallocate(pabt, 1, maxco, 1, maxco, 0, nthread - 1)
    2127          252 :       CALL reallocate(workt, 1, maxco, 1, maxsgf_set, 0, nthread - 1)
    2128              : 
    2129              :       ! find maximum numbers
    2130          252 :       nimages = dft_control%nimages
    2131          252 :       CPASSERT(nimages == 1 .OR. do_kp)
    2132              : 
    2133          252 :       natoms = SIZE(particle_set)
    2134              : 
    2135              :       ! get the task lists
    2136          252 :       IF (my_soft) task_list => task_list_soft
    2137          252 :       CPASSERT(ASSOCIATED(task_list))
    2138          252 :       tasks => task_list%tasks
    2139          252 :       atom_pair_send => task_list%atom_pair_send
    2140          252 :       atom_pair_recv => task_list%atom_pair_recv
    2141          252 :       ntasks = task_list%ntasks
    2142              : 
    2143              :       ! *** set up the rs multi-grids
    2144          252 :       CPASSERT(ASSOCIATED(pw_env))
    2145          252 :       CALL pw_env_get(pw_env, rs_descs=rs_descs, rs_grids=rs_rho)
    2146          774 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
    2147          774 :          distributed_rs_grids = rs_rho(igrid_level)%desc%distributed
    2148              :       END DO
    2149              : 
    2150          252 :       eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
    2151              : 
    2152              :       !   *** Initialize working density matrix ***
    2153              :       ! distributed rs grids require a matrix that will be changed
    2154              :       ! whereas this is not the case for replicated grids
    2155         1008 :       ALLOCATE (deltap(nimages))
    2156          252 :       IF (distributed_rs_grids) THEN
    2157            0 :          DO img = 1, nimages
    2158              :          END DO
    2159              :          ! this matrix has no strict sparsity pattern in parallel
    2160              :          ! deltap%sparsity_id=-1
    2161            0 :          IF (do_kp) THEN
    2162            0 :             DO img = 1, nimages
    2163              :                CALL dbcsr_copy(deltap(img)%matrix, matrix_p_kp(img)%matrix, &
    2164            0 :                                name="DeltaP")
    2165              :             END DO
    2166              :          ELSE
    2167            0 :             CALL dbcsr_copy(deltap(1)%matrix, matrix_p, name="DeltaP")
    2168              :          END IF
    2169              :       ELSE
    2170          252 :          IF (do_kp) THEN
    2171            0 :             DO img = 1, nimages
    2172            0 :                deltap(img)%matrix => matrix_p_kp(img)%matrix
    2173              :             END DO
    2174              :          ELSE
    2175          252 :             deltap(1)%matrix => matrix_p
    2176              :          END IF
    2177              :       END IF
    2178              : 
    2179              :       ! distribute the matrix
    2180          252 :       IF (distributed_rs_grids) THEN
    2181              :          CALL rs_distribute_matrix(rs_descs=rs_descs, pmats=deltap, &
    2182              :                                    atom_pair_send=atom_pair_send, atom_pair_recv=atom_pair_recv, &
    2183            0 :                                    nimages=nimages, scatter=.TRUE.)
    2184              :       END IF
    2185              : 
    2186              :       ! map all tasks on the grids
    2187              : 
    2188          252 :       ithread = 0
    2189          252 :       pab => pabt(:, :, ithread)
    2190          252 :       work => workt(:, :, ithread)
    2191              : 
    2192          774 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
    2193          774 :          CALL rs_grid_zero(rs_rho(igrid_level))
    2194              :       END DO
    2195              : 
    2196              :       iatom_old = -1; jatom_old = -1; iset_old = -1; jset_old = -1
    2197              :       ikind_old = -1; jkind_old = -1; img_old = -1
    2198        16506 :       loop_tasks: DO itask = 1, ntasks
    2199              : 
    2200              :          !decode the atom pair and basis info
    2201        16254 :          igrid_level = tasks(itask)%grid_level
    2202        16254 :          img = tasks(itask)%image
    2203        16254 :          iatom = tasks(itask)%iatom
    2204        16254 :          jatom = tasks(itask)%jatom
    2205        16254 :          iset = tasks(itask)%iset
    2206        16254 :          jset = tasks(itask)%jset
    2207        16254 :          ipgf = tasks(itask)%ipgf
    2208        16254 :          jpgf = tasks(itask)%jpgf
    2209              : 
    2210        16254 :          ikind = particle_set(iatom)%atomic_kind%kind_number
    2211        16254 :          jkind = particle_set(jatom)%atomic_kind%kind_number
    2212              : 
    2213        16254 :          IF (iatom /= iatom_old .OR. jatom /= jatom_old .OR. img /= img_old) THEN
    2214              : 
    2215         1296 :             IF (iatom /= iatom_old) ra(:) = pbc(particle_set(iatom)%r, cell)
    2216              : 
    2217         1296 :             IF (iatom <= jatom) THEN
    2218          864 :                brow = iatom
    2219          864 :                bcol = jatom
    2220              :             ELSE
    2221          432 :                brow = jatom
    2222          432 :                bcol = iatom
    2223              :             END IF
    2224              : 
    2225         1296 :             IF (ikind /= ikind_old) THEN
    2226          252 :                IF (my_soft) THEN
    2227              :                   CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, &
    2228            0 :                                    basis_type="ORB_SOFT")
    2229              :                ELSE
    2230              :                   CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, &
    2231          252 :                                    basis_type=my_basis_type)
    2232              :                END IF
    2233              :                CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
    2234              :                                       first_sgf=first_sgfa, &
    2235              :                                       lmax=la_max, &
    2236              :                                       lmin=la_min, &
    2237              :                                       npgf=npgfa, &
    2238              :                                       nset=nseta, &
    2239              :                                       nsgf_set=nsgfa, &
    2240              :                                       sphi=sphi_a, &
    2241          252 :                                       zet=zeta)
    2242              :             END IF
    2243              : 
    2244         1296 :             IF (jkind /= jkind_old) THEN
    2245          864 :                IF (my_soft) THEN
    2246              :                   CALL get_qs_kind(qs_kind_set(jkind), basis_set=orb_basis_set, &
    2247            0 :                                    basis_type="ORB_SOFT")
    2248              :                ELSE
    2249              :                   CALL get_qs_kind(qs_kind_set(jkind), basis_set=orb_basis_set, &
    2250          864 :                                    basis_type=my_basis_type)
    2251              :                END IF
    2252              :                CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
    2253              :                                       first_sgf=first_sgfb, &
    2254              :                                       lmax=lb_max, &
    2255              :                                       lmin=lb_min, &
    2256              :                                       npgf=npgfb, &
    2257              :                                       nset=nsetb, &
    2258              :                                       nsgf_set=nsgfb, &
    2259              :                                       sphi=sphi_b, &
    2260          864 :                                       zet=zetb)
    2261              :             END IF
    2262              : 
    2263              :             CALL dbcsr_get_block_p(matrix=deltap(img)%matrix, &
    2264         1296 :                                    row=brow, col=bcol, BLOCK=p_block, found=found)
    2265         1296 :             CPASSERT(found)
    2266              : 
    2267              :             iatom_old = iatom
    2268              :             jatom_old = jatom
    2269              :             ikind_old = ikind
    2270              :             jkind_old = jkind
    2271              :             img_old = img
    2272              :             atom_pair_changed = .TRUE.
    2273              : 
    2274              :          ELSE
    2275              : 
    2276              :             atom_pair_changed = .FALSE.
    2277              : 
    2278              :          END IF
    2279              : 
    2280        16254 :          IF (atom_pair_changed .OR. iset_old /= iset .OR. jset_old /= jset) THEN
    2281              : 
    2282         1296 :             ncoa = npgfa(iset)*ncoset(la_max(iset))
    2283         1296 :             sgfa = first_sgfa(1, iset)
    2284         1296 :             ncob = npgfb(jset)*ncoset(lb_max(jset))
    2285         1296 :             sgfb = first_sgfb(1, jset)
    2286              : 
    2287         1296 :             IF (iatom <= jatom) THEN
    2288              :                CALL dgemm("N", "N", ncoa, nsgfb(jset), nsgfa(iset), &
    2289              :                           1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
    2290              :                           p_block(sgfa, sgfb), SIZE(p_block, 1), &
    2291          864 :                           0.0_dp, work(1, 1), maxco)
    2292              :                CALL dgemm("N", "T", ncoa, ncob, nsgfb(jset), &
    2293              :                           1.0_dp, work(1, 1), maxco, &
    2294              :                           sphi_b(1, sgfb), SIZE(sphi_b, 1), &
    2295          864 :                           0.0_dp, pab(1, 1), maxco)
    2296              :             ELSE
    2297              :                CALL dgemm("N", "N", ncob, nsgfa(iset), nsgfb(jset), &
    2298              :                           1.0_dp, sphi_b(1, sgfb), SIZE(sphi_b, 1), &
    2299              :                           p_block(sgfb, sgfa), SIZE(p_block, 1), &
    2300          432 :                           0.0_dp, work(1, 1), maxco)
    2301              :                CALL dgemm("N", "T", ncob, ncoa, nsgfa(iset), &
    2302              :                           1.0_dp, work(1, 1), maxco, &
    2303              :                           sphi_a(1, sgfa), SIZE(sphi_a, 1), &
    2304          432 :                           0.0_dp, pab(1, 1), maxco)
    2305              :             END IF
    2306              : 
    2307              :             iset_old = iset
    2308              :             jset_old = jset
    2309              : 
    2310              :          END IF
    2311              : 
    2312        65016 :          rab(:) = tasks(itask)%rab
    2313        65016 :          rb(:) = ra(:) + rab(:)
    2314        16254 :          zetp = zeta(ipgf, iset) + zetb(jpgf, jset)
    2315              : 
    2316        16254 :          f = zetb(jpgf, jset)/zetp
    2317        65016 :          rp(:) = ra(:) + f*rab(:)
    2318        65016 :          prefactor = EXP(-zeta(ipgf, iset)*f*DOT_PRODUCT(rab, rab))
    2319              :          radius = exp_radius_very_extended(la_min=la_min(iset), la_max=la_max(iset), &
    2320              :                                            lb_min=lb_min(jset), lb_max=lb_max(jset), &
    2321              :                                            ra=ra, rb=rb, rp=rp, &
    2322              :                                            zetp=zetp, eps=eps_rho_rspace, &
    2323        16254 :                                            prefactor=prefactor, cutoff=1.0_dp)
    2324              : 
    2325        16254 :          na1 = (ipgf - 1)*ncoset(la_max(iset)) + 1
    2326        16254 :          na2 = ipgf*ncoset(la_max(iset))
    2327        16254 :          nb1 = (jpgf - 1)*ncoset(lb_max(jset)) + 1
    2328        16254 :          nb2 = jpgf*ncoset(lb_max(jset))
    2329              : 
    2330              :          ! takes the density matrix symmetry in account, i.e. off-diagonal blocks need to be mapped 'twice'
    2331        16254 :          IF (iatom == jatom .AND. img == 1) THEN
    2332         8100 :             scale = 1.0_dp
    2333              :          ELSE
    2334         8154 :             scale = 2.0_dp
    2335              :          END IF
    2336              : 
    2337              :          ! check whether we need to use fawzi's generalised collocation scheme
    2338        16254 :          IF (rs_rho(igrid_level)%desc%distributed) THEN
    2339              :             !tasks(4,:) is 0 for replicated, 1 for distributed 2 for exceptional distributed tasks
    2340            0 :             IF (tasks(itask)%dist_type == 2) THEN
    2341            0 :                use_subpatch = .TRUE.
    2342              :             ELSE
    2343            0 :                use_subpatch = .FALSE.
    2344              :             END IF
    2345              :          ELSE
    2346        16254 :             use_subpatch = .FALSE.
    2347              :          END IF
    2348              : 
    2349        21672 :          SELECT CASE (beta)
    2350              :          CASE (1)
    2351         5418 :             dabqadb_func = GRID_FUNC_DAB_X
    2352              :          CASE (2)
    2353         5418 :             dabqadb_func = GRID_FUNC_DAB_Y
    2354              :          CASE (3)
    2355         5418 :             dabqadb_func = GRID_FUNC_DAB_Z
    2356              :          CASE DEFAULT
    2357        16254 :             CPABORT("invalid beta")
    2358              :          END SELECT
    2359              : 
    2360        16506 :          IF (iatom <= jatom) THEN
    2361        10854 :             IF (iatom == lambda) &
    2362              :                CALL collocate_pgf_product( &
    2363              :                la_max(iset), zeta(ipgf, iset), la_min(iset), &
    2364              :                lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
    2365              :                ra, rab, scale, pab, na1 - 1, nb1 - 1, &
    2366              :                rsgrid=rs_rho(igrid_level), &
    2367              :                ga_gb_function=dabqadb_func, radius=radius, &
    2368              :                use_subpatch=use_subpatch, &
    2369         3618 :                subpatch_pattern=tasks(itask)%subpatch_pattern)
    2370        10854 :             IF (jatom == lambda) &
    2371              :                CALL collocate_pgf_product( &
    2372              :                la_max(iset), zeta(ipgf, iset), la_min(iset), &
    2373              :                lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
    2374              :                ra, rab, scale, pab, na1 - 1, nb1 - 1, &
    2375              :                rsgrid=rs_rho(igrid_level), &
    2376              :                ga_gb_function=dabqadb_func + 3, radius=radius, &
    2377              :                use_subpatch=use_subpatch, &
    2378         3618 :                subpatch_pattern=tasks(itask)%subpatch_pattern)
    2379              :          ELSE
    2380        21600 :             rab_inv = -rab
    2381         5400 :             IF (jatom == lambda) &
    2382              :                CALL collocate_pgf_product( &
    2383              :                lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
    2384              :                la_max(iset), zeta(ipgf, iset), la_min(iset), &
    2385              :                rb, rab_inv, scale, pab, nb1 - 1, na1 - 1, &
    2386              :                rs_rho(igrid_level), &
    2387              :                ga_gb_function=dabqadb_func, radius=radius, &
    2388              :                use_subpatch=use_subpatch, &
    2389         1800 :                subpatch_pattern=tasks(itask)%subpatch_pattern)
    2390         5400 :             IF (iatom == lambda) &
    2391              :                CALL collocate_pgf_product( &
    2392              :                lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
    2393              :                la_max(iset), zeta(ipgf, iset), la_min(iset), &
    2394              :                rb, rab_inv, scale, pab, nb1 - 1, na1 - 1, &
    2395              :                rs_rho(igrid_level), &
    2396              :                ga_gb_function=dabqadb_func + 3, radius=radius, &
    2397              :                use_subpatch=use_subpatch, &
    2398         1800 :                subpatch_pattern=tasks(itask)%subpatch_pattern)
    2399              :          END IF
    2400              : 
    2401              :       END DO loop_tasks
    2402              : 
    2403          252 :       CALL density_rs2pw(pw_env, rs_rho, drho, drho_gspace)
    2404              : 
    2405              :       !   *** Release work storage ***
    2406          252 :       IF (distributed_rs_grids) THEN
    2407            0 :          CALL dbcsr_deallocate_matrix_set(deltap)
    2408              :       ELSE
    2409          504 :          DO img = 1, nimages
    2410          504 :             NULLIFY (deltap(img)%matrix)
    2411              :          END DO
    2412          252 :          DEALLOCATE (deltap)
    2413              :       END IF
    2414              : 
    2415          252 :       DEALLOCATE (pabt, workt)
    2416              : 
    2417          252 :       CALL timestop(handle)
    2418              : 
    2419          504 :    END SUBROUTINE calculate_drho_elec_dR
    2420              : 
    2421              : ! **************************************************************************************************
    2422              : !> \brief maps a single gaussian on the grid
    2423              : !> \param rho ...
    2424              : !> \param rho_gspace ...
    2425              : !> \param atomic_kind_set ...
    2426              : !> \param qs_kind_set ...
    2427              : !> \param cell ...
    2428              : !> \param dft_control ...
    2429              : !> \param particle_set ...
    2430              : !> \param pw_env ...
    2431              : !> \param required_function ...
    2432              : !> \param basis_type ...
    2433              : !> \par History
    2434              : !>      08.2022 created from calculate_wavefunction
    2435              : !> \note
    2436              : !>      modified calculate_wave function assuming that the collocation of only a single Gaussian is required.
    2437              : !>      chooses a basis function (in contrast to calculate_rho_core or calculate_rho_single_gaussian)
    2438              : ! **************************************************************************************************
    2439        28573 :    SUBROUTINE collocate_single_gaussian(rho, rho_gspace, &
    2440              :                                         atomic_kind_set, qs_kind_set, cell, dft_control, particle_set, &
    2441              :                                         pw_env, required_function, basis_type)
    2442              : 
    2443              :       TYPE(pw_r3d_rs_type), INTENT(INOUT)                       :: rho
    2444              :       TYPE(pw_c1d_gs_type), INTENT(INOUT) :: rho_gspace
    2445              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    2446              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    2447              :       TYPE(cell_type), POINTER                           :: cell
    2448              :       TYPE(dft_control_type), POINTER                    :: dft_control
    2449              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2450              :       TYPE(pw_env_type), POINTER                         :: pw_env
    2451              :       INTEGER, INTENT(IN)                                :: required_function
    2452              :       CHARACTER(LEN=*), INTENT(IN), OPTIONAL             :: basis_type
    2453              : 
    2454              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'collocate_single_gaussian'
    2455              : 
    2456              :       CHARACTER(LEN=default_string_length)               :: my_basis_type
    2457              :       INTEGER :: group_size, handle, i, iatom, igrid_level, ikind, ipgf, iset, maxco, maxsgf_set, &
    2458              :                  my_index, my_pos, na1, na2, natom, ncoa, nseta, offset, sgfa
    2459        28573 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: where_is_the_point
    2460        28573 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, npgfa, nsgfa
    2461        28573 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa
    2462              :       LOGICAL                                            :: found
    2463              :       REAL(KIND=dp)                                      :: dab, eps_rho_rspace, radius, scale
    2464              :       REAL(KIND=dp), DIMENSION(3)                        :: ra
    2465        28573 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: pab, sphi_a, zeta
    2466              :       TYPE(gridlevel_info_type), POINTER                 :: gridlevel_info
    2467              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
    2468              :       TYPE(mp_comm_type)                                 :: group
    2469        28573 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
    2470        28573 :       TYPE(pw_c1d_gs_type), ALLOCATABLE, DIMENSION(:) :: mgrid_gspace
    2471        28573 :       TYPE(pw_r3d_rs_type), ALLOCATABLE, DIMENSION(:)           ::  mgrid_rspace
    2472        28573 :       TYPE(realspace_grid_type), DIMENSION(:), POINTER   :: rs_rho
    2473              : 
    2474        28573 :       IF (PRESENT(basis_type)) THEN
    2475        28573 :          my_basis_type = basis_type
    2476              :       ELSE
    2477            0 :          my_basis_type = "ORB"
    2478              :       END IF
    2479              : 
    2480        28573 :       CALL timeset(routineN, handle)
    2481              : 
    2482        28573 :       NULLIFY (orb_basis_set, pab, la_max, la_min, npgfa, nsgfa, sphi_a, &
    2483        28573 :                zeta, first_sgfa, rs_rho, pw_pools)
    2484              : 
    2485              :       ! *** set up the pw multi-grids
    2486        28573 :       CPASSERT(ASSOCIATED(pw_env))
    2487              :       CALL pw_env_get(pw_env, rs_grids=rs_rho, pw_pools=pw_pools, &
    2488        28573 :                       gridlevel_info=gridlevel_info)
    2489              : 
    2490        28573 :       CALL pw_pools_create_pws(pw_pools, mgrid_gspace)
    2491        28573 :       CALL pw_pools_create_pws(pw_pools, mgrid_rspace)
    2492              : 
    2493              :       ! *** set up rs multi-grids
    2494       142865 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
    2495       142865 :          CALL rs_grid_zero(rs_rho(igrid_level))
    2496              :       END DO
    2497              : 
    2498        28573 :       eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
    2499              : !   *** Allocate work storage ***
    2500        28573 :       CALL get_atomic_kind_set(atomic_kind_set, natom=natom)
    2501              :       CALL get_qs_kind_set(qs_kind_set, &
    2502              :                            maxco=maxco, &
    2503              :                            maxsgf_set=maxsgf_set, &
    2504        28573 :                            basis_type=my_basis_type)
    2505              : 
    2506        85719 :       ALLOCATE (pab(maxco, 1))
    2507              : 
    2508        28573 :       offset = 0
    2509        28573 :       group = mgrid_rspace(1)%pw_grid%para%group
    2510        28573 :       my_pos = mgrid_rspace(1)%pw_grid%para%group%mepos
    2511        28573 :       group_size = mgrid_rspace(1)%pw_grid%para%group%num_pe
    2512        85719 :       ALLOCATE (where_is_the_point(0:group_size - 1))
    2513              : 
    2514       117551 :       DO iatom = 1, natom
    2515        88978 :          ikind = particle_set(iatom)%atomic_kind%kind_number
    2516        88978 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, basis_type=my_basis_type)
    2517              :          CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
    2518              :                                 first_sgf=first_sgfa, &
    2519              :                                 lmax=la_max, &
    2520              :                                 lmin=la_min, &
    2521              :                                 npgf=npgfa, &
    2522              :                                 nset=nseta, &
    2523              :                                 nsgf_set=nsgfa, &
    2524              :                                 sphi=sphi_a, &
    2525        88978 :                                 zet=zeta)
    2526        88978 :          ra(:) = pbc(particle_set(iatom)%r, cell)
    2527        88978 :          dab = 0.0_dp
    2528              : 
    2529      1047571 :          DO iset = 1, nseta
    2530              : 
    2531       841042 :             ncoa = npgfa(iset)*ncoset(la_max(iset))
    2532       841042 :             sgfa = first_sgfa(1, iset)
    2533              : 
    2534       841042 :             found = .FALSE.
    2535       841042 :             my_index = 0
    2536      3176013 :             DO i = 1, nsgfa(iset)
    2537      3176013 :                IF (offset + i == required_function) THEN
    2538              :                   my_index = i
    2539              :                   found = .TRUE.
    2540              :                   EXIT
    2541              :                END IF
    2542              :             END DO
    2543              : 
    2544       841042 :             IF (found) THEN
    2545              : 
    2546       523769 :                pab(1:ncoa, 1) = sphi_a(1:ncoa, sgfa + my_index - 1)
    2547              : 
    2548        58202 :                DO ipgf = 1, npgfa(iset)
    2549              : 
    2550        29629 :                   na1 = (ipgf - 1)*ncoset(la_max(iset)) + 1
    2551        29629 :                   na2 = ipgf*ncoset(la_max(iset))
    2552              : 
    2553        29629 :                   scale = 1.0_dp
    2554        29629 :                   igrid_level = gaussian_gridlevel(gridlevel_info, zeta(ipgf, iset))
    2555              : 
    2556        58202 :                   IF (map_gaussian_here(rs_rho(igrid_level), cell%h_inv, ra, offset, group_size, my_pos)) THEN
    2557              :                      radius = exp_radius_very_extended(la_min=la_min(iset), la_max=la_max(iset), &
    2558              :                                                        lb_min=0, lb_max=0, ra=ra, rb=ra, rp=ra, &
    2559              :                                                        zetp=zeta(ipgf, iset), eps=eps_rho_rspace, &
    2560        27872 :                                                        prefactor=1.0_dp, cutoff=1.0_dp)
    2561              : 
    2562              :                      CALL collocate_pgf_product(la_max(iset), zeta(ipgf, iset), la_min(iset), &
    2563              :                                                 0, 0.0_dp, 0, &
    2564              :                                                 ra, [0.0_dp, 0.0_dp, 0.0_dp], &
    2565              :                                                 scale, pab, na1 - 1, 0, rs_rho(igrid_level), &
    2566        27872 :                                                 radius=radius, ga_gb_function=GRID_FUNC_AB)
    2567              :                   END IF
    2568              : 
    2569              :                END DO
    2570              : 
    2571              :             END IF
    2572              : 
    2573       930020 :             offset = offset + nsgfa(iset)
    2574              : 
    2575              :          END DO
    2576              : 
    2577              :       END DO
    2578              : 
    2579       142865 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
    2580              :          CALL transfer_rs2pw(rs_rho(igrid_level), &
    2581       142865 :                              mgrid_rspace(igrid_level))
    2582              :       END DO
    2583              : 
    2584        28573 :       CALL pw_zero(rho_gspace)
    2585       142865 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
    2586              :          CALL pw_transfer(mgrid_rspace(igrid_level), &
    2587       114292 :                           mgrid_gspace(igrid_level))
    2588       142865 :          CALL pw_axpy(mgrid_gspace(igrid_level), rho_gspace)
    2589              :       END DO
    2590              : 
    2591        28573 :       CALL pw_transfer(rho_gspace, rho)
    2592              : 
    2593              :       ! Release work storage
    2594        28573 :       DEALLOCATE (pab)
    2595              : 
    2596              :       ! give back the pw multi-grids
    2597        28573 :       CALL pw_pools_give_back_pws(pw_pools, mgrid_gspace)
    2598        28573 :       CALL pw_pools_give_back_pws(pw_pools, mgrid_rspace)
    2599              : 
    2600        28573 :       CALL timestop(handle)
    2601              : 
    2602       114292 :    END SUBROUTINE collocate_single_gaussian
    2603              : 
    2604              : ! **************************************************************************************************
    2605              : !> \brief maps a given wavefunction on the grid
    2606              : !> \param mo_vectors ...
    2607              : !> \param ivector ...
    2608              : !> \param rho ...
    2609              : !> \param rho_gspace ...
    2610              : !> \param atomic_kind_set ...
    2611              : !> \param qs_kind_set ...
    2612              : !> \param cell ...
    2613              : !> \param dft_control ...
    2614              : !> \param particle_set ...
    2615              : !> \param pw_env ...
    2616              : !> \param basis_type ...
    2617              : !> \par History
    2618              : !>      08.2002 created [Joost VandeVondele]
    2619              : !>      03.2006 made independent of qs_env [Joost VandeVondele]
    2620              : !>      08.2024 call collocate_function [JGH]
    2621              : ! **************************************************************************************************
    2622         1399 :    SUBROUTINE calculate_wavefunction(mo_vectors, ivector, rho, rho_gspace, &
    2623              :                                      atomic_kind_set, qs_kind_set, cell, dft_control, particle_set, &
    2624              :                                      pw_env, basis_type)
    2625              :       TYPE(cp_fm_type), INTENT(IN)                       :: mo_vectors
    2626              :       INTEGER, INTENT(IN)                                :: ivector
    2627              :       TYPE(pw_r3d_rs_type), INTENT(INOUT)                :: rho
    2628              :       TYPE(pw_c1d_gs_type), INTENT(INOUT)                :: rho_gspace
    2629              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    2630              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    2631              :       TYPE(cell_type), POINTER                           :: cell
    2632              :       TYPE(dft_control_type), POINTER                    :: dft_control
    2633              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2634              :       TYPE(pw_env_type), POINTER                         :: pw_env
    2635              :       CHARACTER(LEN=*), INTENT(IN), OPTIONAL             :: basis_type
    2636              : 
    2637              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_wavefunction'
    2638              : 
    2639              :       INTEGER                                            :: handle, i, nao
    2640              :       LOGICAL                                            :: local
    2641              :       REAL(KIND=dp)                                      :: eps_rho_rspace
    2642              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: eigenvector
    2643              : 
    2644         1399 :       CALL timeset(routineN, handle)
    2645              : 
    2646         1399 :       CALL cp_fm_get_info(matrix=mo_vectors, nrow_global=nao)
    2647         4197 :       ALLOCATE (eigenvector(nao))
    2648        26203 :       DO i = 1, nao
    2649        26203 :          CALL cp_fm_get_element(mo_vectors, i, ivector, eigenvector(i), local)
    2650              :       END DO
    2651              : 
    2652         1399 :       eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
    2653              : 
    2654              :       CALL collocate_function(eigenvector, rho, rho_gspace, &
    2655              :                               atomic_kind_set, qs_kind_set, cell, particle_set, pw_env, &
    2656         2798 :                               eps_rho_rspace, basis_type)
    2657              : 
    2658         1399 :       DEALLOCATE (eigenvector)
    2659              : 
    2660         1399 :       CALL timestop(handle)
    2661              : 
    2662         1399 :    END SUBROUTINE calculate_wavefunction
    2663              : 
    2664              : ! **************************************************************************************************
    2665              : !> \brief maps a given function on the grid
    2666              : !> \param vector ...
    2667              : !> \param rho ...
    2668              : !> \param rho_gspace ...
    2669              : !> \param atomic_kind_set ...
    2670              : !> \param qs_kind_set ...
    2671              : !> \param cell ...
    2672              : !> \param particle_set ...
    2673              : !> \param pw_env ...
    2674              : !> \param eps_rho_rspace ...
    2675              : !> \param basis_type ...
    2676              : !> \par History
    2677              : !>      08.2002 created [Joost VandeVondele]
    2678              : !>      03.2006 made independent of qs_env [Joost VandeVondele]
    2679              : !>      08.2024 specialized version from calculate_wavefunction [JGH]
    2680              : !> \notes
    2681              : !>      modified calculate_rho_elec, should write the wavefunction represented by vector
    2682              : !>      it's presumably dominated by the FFT and the rs->pw and back routines
    2683              : ! **************************************************************************************************
    2684        39818 :    SUBROUTINE collocate_function(vector, rho, rho_gspace, &
    2685              :                                  atomic_kind_set, qs_kind_set, cell, particle_set, pw_env, &
    2686              :                                  eps_rho_rspace, basis_type)
    2687              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: vector
    2688              :       TYPE(pw_r3d_rs_type), INTENT(INOUT)                :: rho
    2689              :       TYPE(pw_c1d_gs_type), INTENT(INOUT)                :: rho_gspace
    2690              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    2691              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    2692              :       TYPE(cell_type), POINTER                           :: cell
    2693              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2694              :       TYPE(pw_env_type), POINTER                         :: pw_env
    2695              :       REAL(KIND=dp), INTENT(IN)                          :: eps_rho_rspace
    2696              :       CHARACTER(LEN=*), INTENT(IN), OPTIONAL             :: basis_type
    2697              : 
    2698              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'collocate_function'
    2699              : 
    2700              :       CHARACTER(LEN=default_string_length)               :: my_basis_type
    2701              :       INTEGER :: group_size, handle, i, iatom, igrid_level, ikind, ipgf, iset, maxco, maxsgf_set, &
    2702              :                  my_pos, na1, na2, natom, ncoa, nseta, offset, sgfa
    2703        19909 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: where_is_the_point
    2704        19909 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, npgfa, nsgfa
    2705        19909 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa
    2706              :       REAL(KIND=dp)                                      :: dab, radius, scale
    2707              :       REAL(KIND=dp), DIMENSION(3)                        :: ra
    2708        19909 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: pab, sphi_a, work, zeta
    2709              :       TYPE(gridlevel_info_type), POINTER                 :: gridlevel_info
    2710              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
    2711              :       TYPE(mp_comm_type)                                 :: group
    2712        19909 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
    2713        19909 :       TYPE(pw_c1d_gs_type), ALLOCATABLE, DIMENSION(:)    :: mgrid_gspace
    2714        19909 :       TYPE(pw_r3d_rs_type), ALLOCATABLE, DIMENSION(:)    :: mgrid_rspace
    2715        19909 :       TYPE(realspace_grid_type), DIMENSION(:), POINTER   :: rs_rho
    2716              : 
    2717        19909 :       CALL timeset(routineN, handle)
    2718              : 
    2719        19909 :       IF (PRESENT(basis_type)) THEN
    2720        18206 :          my_basis_type = basis_type
    2721              :       ELSE
    2722         1703 :          my_basis_type = "ORB"
    2723              :       END IF
    2724              : 
    2725        19909 :       NULLIFY (orb_basis_set, pab, work, la_max, la_min, &
    2726        19909 :                npgfa, nsgfa, sphi_a, zeta, first_sgfa, rs_rho, pw_pools)
    2727              : 
    2728              :       ! *** set up the pw multi-grids
    2729        19909 :       CPASSERT(ASSOCIATED(pw_env))
    2730              :       CALL pw_env_get(pw_env, rs_grids=rs_rho, pw_pools=pw_pools, &
    2731        19909 :                       gridlevel_info=gridlevel_info)
    2732              : 
    2733        19909 :       CALL pw_pools_create_pws(pw_pools, mgrid_gspace)
    2734        19909 :       CALL pw_pools_create_pws(pw_pools, mgrid_rspace)
    2735              : 
    2736              :       ! *** set up rs multi-grids
    2737        99263 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
    2738        99263 :          CALL rs_grid_zero(rs_rho(igrid_level))
    2739              :       END DO
    2740              : 
    2741              :       !   *** Allocate work storage ***
    2742        19909 :       CALL get_atomic_kind_set(atomic_kind_set, natom=natom)
    2743              :       CALL get_qs_kind_set(qs_kind_set, &
    2744              :                            maxco=maxco, &
    2745              :                            maxsgf_set=maxsgf_set, &
    2746        19909 :                            basis_type=my_basis_type)
    2747              : 
    2748        59727 :       ALLOCATE (pab(maxco, 1))
    2749        39818 :       ALLOCATE (work(maxco, 1))
    2750              : 
    2751        19909 :       offset = 0
    2752        19909 :       group = mgrid_rspace(1)%pw_grid%para%group
    2753        19909 :       my_pos = mgrid_rspace(1)%pw_grid%para%group%mepos
    2754        19909 :       group_size = mgrid_rspace(1)%pw_grid%para%group%num_pe
    2755        59727 :       ALLOCATE (where_is_the_point(0:group_size - 1))
    2756              : 
    2757        82559 :       DO iatom = 1, natom
    2758        62650 :          ikind = particle_set(iatom)%atomic_kind%kind_number
    2759        62650 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, basis_type=my_basis_type)
    2760              :          CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
    2761              :                                 first_sgf=first_sgfa, &
    2762              :                                 lmax=la_max, &
    2763              :                                 lmin=la_min, &
    2764              :                                 npgf=npgfa, &
    2765              :                                 nset=nseta, &
    2766              :                                 nsgf_set=nsgfa, &
    2767              :                                 sphi=sphi_a, &
    2768        62650 :                                 zet=zeta)
    2769        62650 :          ra(:) = pbc(particle_set(iatom)%r, cell)
    2770        62650 :          dab = 0.0_dp
    2771              : 
    2772       691140 :          DO iset = 1, nseta
    2773              : 
    2774       545931 :             ncoa = npgfa(iset)*ncoset(la_max(iset))
    2775       545931 :             sgfa = first_sgfa(1, iset)
    2776              : 
    2777      2111310 :             DO i = 1, nsgfa(iset)
    2778      2111310 :                work(i, 1) = vector(offset + i)
    2779              :             END DO
    2780              : 
    2781              :             CALL dgemm("N", "N", ncoa, 1, nsgfa(iset), &
    2782              :                        1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
    2783              :                        work(1, 1), SIZE(work, 1), &
    2784       545931 :                        0.0_dp, pab(1, 1), SIZE(pab, 1))
    2785              : 
    2786      1119647 :             DO ipgf = 1, npgfa(iset)
    2787              : 
    2788       573716 :                na1 = (ipgf - 1)*ncoset(la_max(iset)) + 1
    2789       573716 :                na2 = ipgf*ncoset(la_max(iset))
    2790              : 
    2791       573716 :                scale = 1.0_dp
    2792       573716 :                igrid_level = gaussian_gridlevel(gridlevel_info, zeta(ipgf, iset))
    2793              : 
    2794      1119647 :                IF (map_gaussian_here(rs_rho(igrid_level), cell%h_inv, ra, offset, group_size, my_pos)) THEN
    2795              :                   radius = exp_radius_very_extended(la_min=la_min(iset), la_max=la_max(iset), &
    2796              :                                                     lb_min=0, lb_max=0, ra=ra, rb=ra, rp=ra, &
    2797              :                                                     zetp=zeta(ipgf, iset), eps=eps_rho_rspace, &
    2798       523349 :                                                     prefactor=1.0_dp, cutoff=1.0_dp)
    2799              : 
    2800              :                   CALL collocate_pgf_product(la_max(iset), zeta(ipgf, iset), la_min(iset), &
    2801              :                                              0, 0.0_dp, 0, &
    2802              :                                              ra, [0.0_dp, 0.0_dp, 0.0_dp], &
    2803              :                                              scale, pab, na1 - 1, 0, rs_rho(igrid_level), &
    2804       523349 :                                              radius=radius, ga_gb_function=GRID_FUNC_AB)
    2805              :                END IF
    2806              : 
    2807              :             END DO
    2808              : 
    2809       608581 :             offset = offset + nsgfa(iset)
    2810              : 
    2811              :          END DO
    2812              : 
    2813              :       END DO
    2814              : 
    2815        99263 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
    2816              :          CALL transfer_rs2pw(rs_rho(igrid_level), &
    2817        99263 :                              mgrid_rspace(igrid_level))
    2818              :       END DO
    2819              : 
    2820        19909 :       CALL pw_zero(rho_gspace)
    2821        99263 :       DO igrid_level = 1, gridlevel_info%ngrid_levels
    2822              :          CALL pw_transfer(mgrid_rspace(igrid_level), &
    2823        79354 :                           mgrid_gspace(igrid_level))
    2824        99263 :          CALL pw_axpy(mgrid_gspace(igrid_level), rho_gspace)
    2825              :       END DO
    2826              : 
    2827        19909 :       CALL pw_transfer(rho_gspace, rho)
    2828              : 
    2829              :       ! Release work storage
    2830        19909 :       DEALLOCATE (pab)
    2831        19909 :       DEALLOCATE (work)
    2832              : 
    2833              :       ! give back the pw multi-grids
    2834        19909 :       CALL pw_pools_give_back_pws(pw_pools, mgrid_gspace)
    2835        19909 :       CALL pw_pools_give_back_pws(pw_pools, mgrid_rspace)
    2836              : 
    2837        19909 :       CALL timestop(handle)
    2838              : 
    2839        59727 :    END SUBROUTINE collocate_function
    2840              : 
    2841              : END MODULE qs_collocate_density
        

Generated by: LCOV version 2.0-1