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

Generated by: LCOV version 2.0-1