LCOV - code coverage report
Current view: top level - src - qs_neighbor_lists.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:21ef868) Lines: 99.2 % 856 849
Test Date: 2026-08-14 07:04:57 Functions: 81.8 % 11 9

            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 Generate the atomic neighbor lists.
      10              : !> \par History
      11              : !>      - List rebuild for sab_orb neighbor list (10.09.2002,MK)
      12              : !>      - List rebuild for all lists (25.09.2002,MK)
      13              : !>      - Row-wise parallelized version (16.06.2003,MK)
      14              : !>      - Row- and column-wise parallelized version (19.07.2003,MK)
      15              : !>      - bug fix for non-periodic case (23.02.06,MK)
      16              : !>      - major refactoring (25.07.10,jhu)
      17              : !> \author Matthias Krack (08.10.1999,26.03.2002,16.06.2003)
      18              : ! **************************************************************************************************
      19              : MODULE qs_neighbor_lists
      20              :    USE almo_scf_types,                  ONLY: almo_max_cutoff_multiplier
      21              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      22              :                                               get_atomic_kind,&
      23              :                                               get_atomic_kind_set
      24              :    USE basis_set_types,                 ONLY: get_gto_basis_set,&
      25              :                                               gto_basis_set_p_type,&
      26              :                                               gto_basis_set_type
      27              :    USE cell_types,                      ONLY: cell_type,&
      28              :                                               get_cell,&
      29              :                                               pbc,&
      30              :                                               pbc_stable,&
      31              :                                               plane_distance,&
      32              :                                               real_to_scaled,&
      33              :                                               scaled_to_real
      34              :    USE cp_control_types,                ONLY: dft_control_type
      35              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      36              :                                               cp_logger_type
      37              :    USE cp_output_handling,              ONLY: cp_p_file,&
      38              :                                               cp_print_key_finished_output,&
      39              :                                               cp_print_key_should_output,&
      40              :                                               cp_print_key_unit_nr
      41              :    USE cp_units,                        ONLY: cp_unit_from_cp2k
      42              :    USE distribution_1d_types,           ONLY: distribution_1d_type
      43              :    USE distribution_2d_types,           ONLY: distribution_2d_type
      44              :    USE ewald_environment_types,         ONLY: ewald_env_get,&
      45              :                                               ewald_environment_type
      46              :    USE external_potential_types,        ONLY: all_potential_type,&
      47              :                                               get_potential,&
      48              :                                               gth_potential_type,&
      49              :                                               sgp_potential_type
      50              :    USE input_constants,                 ONLY: &
      51              :         dispersion_uff, do_method_lrigpw, do_method_rigpw, do_potential_id, &
      52              :         do_potential_mix_cl_trunc, do_potential_short, do_potential_truncated, do_se_IS_slater, &
      53              :         vdw_pairpot_dftd4, xc_vdw_fun_pairpot
      54              :    USE input_section_types,             ONLY: section_vals_get,&
      55              :                                               section_vals_get_subs_vals,&
      56              :                                               section_vals_type,&
      57              :                                               section_vals_val_get
      58              :    USE kinds,                           ONLY: default_string_length,&
      59              :                                               dp,&
      60              :                                               int_8
      61              :    USE kpoint_types,                    ONLY: kpoint_type
      62              :    USE libint_2c_3c,                    ONLY: cutoff_screen_factor
      63              :    USE mathlib,                         ONLY: erfc_cutoff
      64              :    USE message_passing,                 ONLY: mp_para_env_type
      65              :    USE molecule_types,                  ONLY: molecule_type
      66              :    USE particle_types,                  ONLY: particle_type
      67              :    USE paw_proj_set_types,              ONLY: get_paw_proj_set,&
      68              :                                               paw_proj_set_type
      69              :    USE periodic_table,                  ONLY: ptable
      70              :    USE physcon,                         ONLY: bohr
      71              :    USE qs_cneo_types,                   ONLY: cneo_potential_type
      72              :    USE qs_dftb_types,                   ONLY: qs_dftb_atom_type
      73              :    USE qs_dftb_utils,                   ONLY: get_dftb_atom_param
      74              :    USE qs_dispersion_types,             ONLY: qs_dispersion_type
      75              :    USE qs_environment_types,            ONLY: get_qs_env,&
      76              :                                               qs_environment_type
      77              :    USE qs_gcp_types,                    ONLY: qs_gcp_type
      78              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      79              :                                               get_qs_kind_set,&
      80              :                                               qs_kind_type
      81              :    USE qs_ks_types,                     ONLY: get_ks_env,&
      82              :                                               qs_ks_env_type,&
      83              :                                               set_ks_env
      84              :    USE qs_neighbor_list_types,          ONLY: &
      85              :         add_neighbor_list, add_neighbor_node, allocate_neighbor_list_set, get_iterator_info, &
      86              :         get_iterator_task, neighbor_list_iterate, neighbor_list_iterator_create, &
      87              :         neighbor_list_iterator_p_type, neighbor_list_iterator_release, neighbor_list_p_type, &
      88              :         neighbor_list_set_p_type, neighbor_list_set_type, release_neighbor_list_sets
      89              :    USE string_utilities,                ONLY: compress,&
      90              :                                               uppercase
      91              :    USE subcell_types,                   ONLY: allocate_subcell,&
      92              :                                               deallocate_subcell,&
      93              :                                               give_ijk_subcell,&
      94              :                                               subcell_type
      95              :    USE util,                            ONLY: locate,&
      96              :                                               sort
      97              :    USE xtb_types,                       ONLY: get_xtb_atom_param,&
      98              :                                               xtb_atom_type
      99              : #include "./base/base_uses.f90"
     100              : 
     101              :    IMPLICIT NONE
     102              : 
     103              :    PRIVATE
     104              : 
     105              : ! **************************************************************************************************
     106              :    TYPE local_atoms_type
     107              :       INTEGER, DIMENSION(:), POINTER                   :: list => NULL(), &
     108              :                                                           list_local_a_index => NULL(), &
     109              :                                                           list_local_b_index => NULL(), &
     110              :                                                           list_1d => NULL(), &
     111              :                                                           list_a_mol => NULL(), &
     112              :                                                           list_b_mol => NULL()
     113              :    END TYPE local_atoms_type
     114              : ! **************************************************************************************************
     115              : 
     116              :    TYPE local_lists
     117              :       INTEGER, DIMENSION(:), POINTER           :: list => NULL()
     118              :    END TYPE local_lists
     119              : 
     120              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_neighbor_lists'
     121              : 
     122              :    ! private counter, used to version qs neighbor lists
     123              :    INTEGER, SAVE, PRIVATE :: last_qs_neighbor_list_id_nr = 0
     124              : 
     125              :    ! Public subroutines
     126              :    PUBLIC :: build_qs_neighbor_lists, local_atoms_type, atom2d_cleanup, &
     127              :              atom2d_build, build_neighbor_lists, pair_radius_setup, &
     128              :              setup_neighbor_list, write_neighbor_lists
     129              : CONTAINS
     130              : 
     131              : ! **************************************************************************************************
     132              : !> \brief   free the internals of atom2d
     133              : !> \param atom2d ...
     134              : !> \param
     135              : ! **************************************************************************************************
     136        50502 :    SUBROUTINE atom2d_cleanup(atom2d)
     137              :       TYPE(local_atoms_type), DIMENSION(:)               :: atom2d
     138              : 
     139              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'atom2d_cleanup'
     140              : 
     141              :       INTEGER                                            :: handle, ikind
     142              : 
     143        50502 :       CALL timeset(routineN, handle)
     144       143149 :       DO ikind = 1, SIZE(atom2d)
     145        92647 :          NULLIFY (atom2d(ikind)%list)
     146        92647 :          IF (ASSOCIATED(atom2d(ikind)%list_local_a_index)) THEN
     147        66996 :             DEALLOCATE (atom2d(ikind)%list_local_a_index)
     148              :          END IF
     149        92647 :          IF (ASSOCIATED(atom2d(ikind)%list_local_b_index)) THEN
     150        92585 :             DEALLOCATE (atom2d(ikind)%list_local_b_index)
     151              :          END IF
     152        92647 :          IF (ASSOCIATED(atom2d(ikind)%list_a_mol)) THEN
     153        66996 :             DEALLOCATE (atom2d(ikind)%list_a_mol)
     154              :          END IF
     155        92647 :          IF (ASSOCIATED(atom2d(ikind)%list_b_mol)) THEN
     156        92585 :             DEALLOCATE (atom2d(ikind)%list_b_mol)
     157              :          END IF
     158       143149 :          IF (ASSOCIATED(atom2d(ikind)%list_1d)) THEN
     159        92647 :             DEALLOCATE (atom2d(ikind)%list_1d)
     160              :          END IF
     161              :       END DO
     162        50502 :       CALL timestop(handle)
     163              : 
     164        50502 :    END SUBROUTINE atom2d_cleanup
     165              : 
     166              : ! **************************************************************************************************
     167              : !> \brief   Build some distribution structure of atoms, refactored from build_qs_neighbor_lists
     168              : !> \param atom2d output
     169              : !> \param distribution_1d ...
     170              : !> \param distribution_2d ...
     171              : !> \param atomic_kind_set ...
     172              : !> \param molecule_set ...
     173              : !> \param molecule_only ...
     174              : !> \param particle_set ...
     175              : !> \author  JH
     176              : ! **************************************************************************************************
     177        50502 :    SUBROUTINE atom2d_build(atom2d, distribution_1d, distribution_2d, &
     178              :                            atomic_kind_set, molecule_set, molecule_only, particle_set)
     179              :       TYPE(local_atoms_type), DIMENSION(:)               :: atom2d
     180              :       TYPE(distribution_1d_type), POINTER                :: distribution_1d
     181              :       TYPE(distribution_2d_type), POINTER                :: distribution_2d
     182              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     183              :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     184              :       LOGICAL                                            :: molecule_only
     185              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     186              : 
     187              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'atom2d_build'
     188              : 
     189              :       INTEGER                                            :: atom_a, handle, ia, iat, iatom, &
     190              :                                                             iatom_local, ikind, imol, natom, &
     191              :                                                             natom_a, natom_local_a, natom_local_b, &
     192              :                                                             nel, nkind
     193        50502 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom2mol, atom_of_kind, listindex, &
     194        50502 :                                                             listsort
     195        50502 :       INTEGER, DIMENSION(:), POINTER                     :: local_cols_array, local_rows_array
     196              : 
     197        50502 :       CALL timeset(routineN, handle)
     198              : 
     199        50502 :       nkind = SIZE(atomic_kind_set)
     200        50502 :       natom = SIZE(particle_set)
     201        50502 :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, atom_of_kind=atom_of_kind)
     202              : 
     203        50502 :       IF (molecule_only) THEN
     204         1116 :          ALLOCATE (atom2mol(natom))
     205         1270 :          DO imol = 1, SIZE(molecule_set)
     206         3256 :             DO iat = molecule_set(imol)%first_atom, molecule_set(imol)%last_atom
     207         2884 :                atom2mol(iat) = imol
     208              :             END DO
     209              :          END DO
     210              :       END IF
     211              : 
     212       143149 :       DO ikind = 1, nkind
     213        92647 :          NULLIFY (atom2d(ikind)%list)
     214        92647 :          NULLIFY (atom2d(ikind)%list_local_a_index)
     215        92647 :          NULLIFY (atom2d(ikind)%list_local_b_index)
     216        92647 :          NULLIFY (atom2d(ikind)%list_1d)
     217        92647 :          NULLIFY (atom2d(ikind)%list_a_mol)
     218        92647 :          NULLIFY (atom2d(ikind)%list_b_mol)
     219              : 
     220        92647 :          CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom2d(ikind)%list)
     221              : 
     222        92647 :          natom_a = SIZE(atom2d(ikind)%list)
     223              : 
     224        92647 :          natom_local_a = distribution_2d%n_local_rows(ikind)
     225        92647 :          natom_local_b = distribution_2d%n_local_cols(ikind)
     226        92647 :          local_rows_array => distribution_2d%local_rows(ikind)%array
     227        92647 :          local_cols_array => distribution_2d%local_cols(ikind)%array
     228              : 
     229        92647 :          nel = distribution_1d%n_el(ikind)
     230       250773 :          ALLOCATE (atom2d(ikind)%list_1d(nel))
     231       207222 :          DO iat = 1, nel
     232       114575 :             ia = distribution_1d%list(ikind)%array(iat)
     233       207222 :             atom2d(ikind)%list_1d(iat) = atom_of_kind(ia)
     234              :          END DO
     235              : 
     236       370588 :          ALLOCATE (listsort(natom_a), listindex(natom_a))
     237       317763 :          listsort(1:natom_a) = atom2d(ikind)%list(1:natom_a)
     238        92647 :          CALL sort(listsort, natom_a, listindex)
     239              :          ! Block rows
     240        92647 :          IF (natom_local_a > 0) THEN
     241       200988 :             ALLOCATE (atom2d(ikind)%list_local_a_index(natom_local_a))
     242       133992 :             ALLOCATE (atom2d(ikind)%list_a_mol(natom_local_a))
     243       192059 :             atom2d(ikind)%list_a_mol(:) = 0
     244              : 
     245              :             ! Build index vector for mapping
     246       192059 :             DO iatom_local = 1, natom_local_a
     247       125063 :                atom_a = local_rows_array(iatom_local)
     248       125063 :                iatom = locate(listsort, atom_a)
     249       125063 :                atom2d(ikind)%list_local_a_index(iatom_local) = listindex(iatom)
     250       192059 :                IF (molecule_only) atom2d(ikind)%list_a_mol(iatom_local) = atom2mol(atom_a)
     251              :             END DO
     252              : 
     253              :          END IF
     254              : 
     255              :          ! Block columns
     256        92647 :          IF (natom_local_b > 0) THEN
     257              : 
     258       277755 :             ALLOCATE (atom2d(ikind)%list_local_b_index(natom_local_b))
     259       185170 :             ALLOCATE (atom2d(ikind)%list_b_mol(natom_local_b))
     260       317549 :             atom2d(ikind)%list_b_mol(:) = 0
     261              : 
     262              :             ! Build index vector for mapping
     263       317549 :             DO iatom_local = 1, natom_local_b
     264       224964 :                atom_a = local_cols_array(iatom_local)
     265       224964 :                iatom = locate(listsort, atom_a)
     266       224964 :                atom2d(ikind)%list_local_b_index(iatom_local) = listindex(iatom)
     267       317549 :                IF (molecule_only) atom2d(ikind)%list_b_mol(iatom_local) = atom2mol(atom_a)
     268              :             END DO
     269              : 
     270              :          END IF
     271              : 
     272       143149 :          DEALLOCATE (listsort, listindex)
     273              : 
     274              :       END DO
     275              : 
     276        50502 :       CALL timestop(handle)
     277              : 
     278       101004 :    END SUBROUTINE atom2d_build
     279              : 
     280              : ! **************************************************************************************************
     281              : !> \brief   Build all the required neighbor lists for Quickstep.
     282              : !> \param qs_env ...
     283              : !> \param para_env ...
     284              : !> \param molecular ...
     285              : !> \param force_env_section ...
     286              : !> \date    28.08.2000
     287              : !> \par History
     288              : !>          - Major refactoring (25.07.2010,jhu)
     289              : !> \author  MK
     290              : !> \version 1.0
     291              : ! **************************************************************************************************
     292        31911 :    SUBROUTINE build_qs_neighbor_lists(qs_env, para_env, molecular, force_env_section)
     293              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     294              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     295              :       LOGICAL, OPTIONAL                                  :: molecular
     296              :       TYPE(section_vals_type), POINTER                   :: force_env_section
     297              : 
     298              :       CHARACTER(len=*), PARAMETER :: routineN = 'build_qs_neighbor_lists'
     299              : 
     300              :       CHARACTER(LEN=2)                                   :: element_symbol, element_symbol2
     301              :       CHARACTER(LEN=default_string_length)               :: print_key_path
     302              :       INTEGER                                            :: handle, hfx_pot, ikind, ingp, iw, jkind, &
     303              :                                                             maxatom, ngp, nkind, zat
     304              :       LOGICAL :: all_potential_present, almo, cneo_potential_present, dftb, do_hfx, dokp, &
     305              :          gth_potential_present, lri_optbas, lrigpw, mic, molecule_only, nddo, paw_atom, &
     306              :          paw_atom_present, rigpw, sgp_potential_present, stable_images, xtb
     307        31911 :       LOGICAL, ALLOCATABLE, DIMENSION(:) :: all_present, aux_fit_present, aux_present, &
     308        31911 :          cneo_present, core_present, default_present, nonbond1_atom, nonbond2_atom, oce_present, &
     309        31911 :          orb_present, ppl_present, ppnl_present, ri_present, xb1_atom, xb2_atom
     310              :       REAL(dp)                                           :: almo_rcov, almo_rvdw, eps_schwarz, &
     311              :                                                             omega, pdist, rcut, roperator, subcells
     312        31911 :       REAL(dp), ALLOCATABLE, DIMENSION(:) :: all_pot_rad, aux_fit_radius, c_radius, calpha, &
     313        31911 :          core_radius, nuc_orb_radius, oce_radius, orb_radius, ppl_radius, ppnl_radius, ri_radius, &
     314        31911 :          zeff
     315        31911 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: pair_radius, pair_radius_lb
     316              :       TYPE(all_potential_type), POINTER                  :: all_potential
     317        31911 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     318              :       TYPE(cell_type), POINTER                           :: cell
     319              :       TYPE(cneo_potential_type), POINTER                 :: cneo_potential
     320              :       TYPE(cp_logger_type), POINTER                      :: logger
     321              :       TYPE(dft_control_type), POINTER                    :: dft_control
     322              :       TYPE(distribution_1d_type), POINTER                :: distribution_1d
     323              :       TYPE(distribution_2d_type), POINTER                :: distribution_2d
     324              :       TYPE(ewald_environment_type), POINTER              :: ewald_env
     325              :       TYPE(gth_potential_type), POINTER                  :: gth_potential
     326              :       TYPE(gto_basis_set_type), POINTER                  :: aux_basis_set, aux_fit_basis_set, &
     327              :                                                             nuc_basis_set, orb_basis_set, &
     328              :                                                             ri_basis_set
     329              :       TYPE(kpoint_type), POINTER                         :: kpoints
     330        31911 :       TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:)  :: atom2d
     331        31911 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     332        31911 :       TYPE(neighbor_list_set_p_type), DIMENSION(:), POINTER :: saa_list, sab_all, sab_almo, &
     333        31911 :          sab_cn, sab_cneo, sab_core, sab_gcp, sab_kp, sab_kp_nosym, sab_lrc, sab_orb, sab_scp, &
     334        31911 :          sab_se, sab_tbe, sab_vdw, sab_xb, sab_xtb_nonbond, sab_xtb_pp, sab_xtbe, sac_ae, sac_lri, &
     335        31911 :          sac_ppl, sap_oce, sap_ppnl, soa_list, soo_list
     336        31911 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     337              :       TYPE(paw_proj_set_type), POINTER                   :: paw_proj
     338              :       TYPE(qs_dftb_atom_type), POINTER                   :: dftb_atom
     339              :       TYPE(qs_dispersion_type), POINTER                  :: dispersion_env
     340              :       TYPE(qs_gcp_type), POINTER                         :: gcp_env
     341        31911 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     342              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     343              :       TYPE(section_vals_type), POINTER                   :: hfx_sections, neighbor_list_section
     344              :       TYPE(sgp_potential_type), POINTER                  :: sgp_potential
     345              :       TYPE(xtb_atom_type), POINTER                       :: xtb_atom
     346              : 
     347        31911 :       CALL timeset(routineN, handle)
     348        31911 :       NULLIFY (logger)
     349        31911 :       logger => cp_get_default_logger()
     350              : 
     351        31911 :       NULLIFY (atomic_kind_set, qs_kind_set, cell, neighbor_list_section, &
     352        31911 :                distribution_1d, distribution_2d, gth_potential, sgp_potential, orb_basis_set, &
     353        31911 :                particle_set, molecule_set, dft_control, ks_env)
     354              : 
     355        31911 :       NULLIFY (sab_orb)
     356        31911 :       NULLIFY (sac_ae)
     357        31911 :       NULLIFY (sac_ppl)
     358        31911 :       NULLIFY (sac_lri)
     359        31911 :       NULLIFY (sap_ppnl)
     360        31911 :       NULLIFY (sap_oce)
     361        31911 :       NULLIFY (sab_se)
     362        31911 :       NULLIFY (sab_lrc)
     363        31911 :       NULLIFY (sab_tbe)
     364        31911 :       NULLIFY (sab_xtbe)
     365        31911 :       NULLIFY (sab_core)
     366        31911 :       NULLIFY (sab_xb)
     367        31911 :       NULLIFY (sab_xtb_pp)
     368        31911 :       NULLIFY (sab_xtb_nonbond)
     369        31911 :       NULLIFY (sab_all)
     370        31911 :       NULLIFY (sab_vdw)
     371        31911 :       NULLIFY (sab_cn)
     372        31911 :       NULLIFY (soo_list)
     373        31911 :       NULLIFY (sab_scp)
     374        31911 :       NULLIFY (sab_almo)
     375        31911 :       NULLIFY (sab_kp)
     376        31911 :       NULLIFY (sab_kp_nosym)
     377        31911 :       NULLIFY (sab_cneo)
     378              : 
     379              :       CALL get_qs_env(qs_env, &
     380              :                       ks_env=ks_env, &
     381              :                       atomic_kind_set=atomic_kind_set, &
     382              :                       qs_kind_set=qs_kind_set, &
     383              :                       cell=cell, &
     384              :                       kpoints=kpoints, &
     385              :                       distribution_2d=distribution_2d, &
     386              :                       local_particles=distribution_1d, &
     387              :                       particle_set=particle_set, &
     388              :                       molecule_set=molecule_set, &
     389        31911 :                       dft_control=dft_control)
     390              : 
     391        31911 :       neighbor_list_section => section_vals_get_subs_vals(force_env_section, "DFT%PRINT%NEIGHBOR_LISTS")
     392              : 
     393              :       ! This sets the id number of the qs neighbor lists, new lists, means new version
     394              :       ! new version implies new sparsity of the matrices
     395        31911 :       last_qs_neighbor_list_id_nr = last_qs_neighbor_list_id_nr + 1
     396        31911 :       CALL set_ks_env(ks_env=ks_env, neighbor_list_id=last_qs_neighbor_list_id_nr)
     397              : 
     398              :       CALL get_ks_env(ks_env=ks_env, &
     399              :                       sab_orb=sab_orb, &
     400              :                       sac_ae=sac_ae, &
     401              :                       sac_ppl=sac_ppl, &
     402              :                       sac_lri=sac_lri, &
     403              :                       sab_vdw=sab_vdw, &
     404              :                       sap_ppnl=sap_ppnl, &
     405              :                       sap_oce=sap_oce, &
     406              :                       sab_se=sab_se, &
     407              :                       sab_lrc=sab_lrc, &
     408              :                       sab_tbe=sab_tbe, &
     409              :                       sab_xtbe=sab_xtbe, &
     410              :                       sab_core=sab_core, &
     411              :                       sab_xb=sab_xb, &
     412              :                       sab_xtb_pp=sab_xtb_pp, &
     413              :                       sab_xtb_nonbond=sab_xtb_nonbond, &
     414              :                       sab_scp=sab_scp, &
     415              :                       sab_all=sab_all, &
     416              :                       sab_almo=sab_almo, &
     417              :                       sab_kp=sab_kp, &
     418              :                       sab_kp_nosym=sab_kp_nosym, &
     419        31911 :                       sab_cneo=sab_cneo)
     420              : 
     421        31911 :       dokp = (kpoints%nkp > 0)
     422        31911 :       stable_images = dokp .AND. kpoints%symmetry
     423        31911 :       nddo = dft_control%qs_control%semi_empirical
     424        31911 :       dftb = dft_control%qs_control%dftb
     425        31911 :       xtb = dft_control%qs_control%xtb
     426        31911 :       almo = dft_control%qs_control%do_almo_scf
     427        31911 :       lrigpw = (dft_control%qs_control%method_id == do_method_lrigpw)
     428        31911 :       rigpw = (dft_control%qs_control%method_id == do_method_rigpw)
     429        31911 :       lri_optbas = dft_control%qs_control%lri_optbas
     430              : 
     431              :       ! molecular lists
     432        31911 :       molecule_only = .FALSE.
     433        31911 :       IF (PRESENT(molecular)) molecule_only = molecular
     434              :       ! minimum image convention (MIC)
     435        31911 :       mic = molecule_only
     436        31911 :       IF (dokp) THEN
     437              :          ! no MIC for kpoints
     438         3562 :          mic = .FALSE.
     439        28349 :       ELSE IF (nddo) THEN
     440              :          ! enforce MIC for interaction lists in SE
     441         5804 :          mic = .TRUE.
     442              :       END IF
     443        31911 :       pdist = dft_control%qs_control%pairlist_radius
     444              : 
     445        31911 :       hfx_sections => section_vals_get_subs_vals(qs_env%input, "DFT%XC%HF")
     446        31911 :       CALL section_vals_get(hfx_sections, explicit=do_hfx)
     447              : 
     448        31911 :       CALL get_atomic_kind_set(atomic_kind_set, maxatom=maxatom)
     449              :       CALL get_qs_kind_set(qs_kind_set, paw_atom_present=paw_atom_present, &
     450              :                            gth_potential_present=gth_potential_present, &
     451              :                            sgp_potential_present=sgp_potential_present, &
     452              :                            all_potential_present=all_potential_present, &
     453        31911 :                            cneo_potential_present=cneo_potential_present)
     454              : 
     455        31911 :       CALL section_vals_val_get(qs_env%input, "DFT%SUBCELLS", r_val=subcells)
     456              : 
     457              :       ! Allocate work storage
     458        31911 :       nkind = SIZE(atomic_kind_set)
     459              :       ALLOCATE (orb_present(nkind), aux_fit_present(nkind), aux_present(nkind), &
     460       223377 :                 default_present(nkind), core_present(nkind))
     461              :       ALLOCATE (orb_radius(nkind), aux_fit_radius(nkind), c_radius(nkind), &
     462       255288 :                 core_radius(nkind), calpha(nkind), zeff(nkind))
     463        31911 :       orb_radius(:) = 0.0_dp
     464        31911 :       aux_fit_radius(:) = 0.0_dp
     465        31911 :       c_radius(:) = 0.0_dp
     466        31911 :       core_radius(:) = 0.0_dp
     467        31911 :       calpha(:) = 0.0_dp
     468        31911 :       zeff(:) = 0.0_dp
     469              : 
     470       127644 :       ALLOCATE (pair_radius(nkind, nkind))
     471        31911 :       IF (gth_potential_present .OR. sgp_potential_present) THEN
     472        34839 :          ALLOCATE (ppl_present(nkind), ppl_radius(nkind))
     473        11613 :          ppl_radius = 0.0_dp
     474        34839 :          ALLOCATE (ppnl_present(nkind), ppnl_radius(nkind))
     475        11613 :          ppnl_radius = 0.0_dp
     476              :       END IF
     477        31911 :       IF (paw_atom_present) THEN
     478         7434 :          ALLOCATE (oce_present(nkind), oce_radius(nkind))
     479         2478 :          oce_radius = 0.0_dp
     480              :       END IF
     481        31911 :       IF (all_potential_present .OR. sgp_potential_present) THEN
     482        61236 :          ALLOCATE (all_present(nkind), all_pot_rad(nkind))
     483        20412 :          all_pot_rad = 0.0_dp
     484              :       END IF
     485        31911 :       IF (cneo_potential_present) THEN
     486           24 :          ALLOCATE (cneo_present(nkind), nuc_orb_radius(nkind))
     487            8 :          nuc_orb_radius = 0.0_dp
     488              :       END IF
     489              : 
     490              :       ! Initialize the local data structures
     491       159441 :       ALLOCATE (atom2d(nkind))
     492              :       CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
     493        31911 :                         molecule_set, molecule_only, particle_set=particle_set)
     494              : 
     495        95619 :       DO ikind = 1, nkind
     496              : 
     497        63708 :          CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom2d(ikind)%list)
     498              : 
     499        63708 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, basis_type="ORB")
     500        63708 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=aux_basis_set, basis_type="AUX")
     501        63708 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=aux_fit_basis_set, basis_type="AUX_FIT")
     502        63708 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=nuc_basis_set, basis_type="NUC")
     503              : 
     504              :          CALL get_qs_kind(qs_kind_set(ikind), &
     505              :                           paw_proj_set=paw_proj, &
     506              :                           paw_atom=paw_atom, &
     507              :                           all_potential=all_potential, &
     508              :                           gth_potential=gth_potential, &
     509              :                           sgp_potential=sgp_potential, &
     510        63708 :                           cneo_potential=cneo_potential)
     511              : 
     512        63708 :          IF (dftb) THEN
     513              :             ! Set the interaction radius for the neighbor lists (DFTB case)
     514              :             ! This includes all interactions (orbitals and short range pair potential) except vdW
     515         8946 :             CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_atom)
     516              :             CALL get_dftb_atom_param(dftb_parameter=dftb_atom, &
     517              :                                      cutoff=orb_radius(ikind), &
     518         8946 :                                      defined=orb_present(ikind))
     519              :          ELSE
     520        54762 :             IF (ASSOCIATED(orb_basis_set)) THEN
     521        54760 :                orb_present(ikind) = .TRUE.
     522        54760 :                CALL get_gto_basis_set(gto_basis_set=orb_basis_set, kind_radius=orb_radius(ikind))
     523              :             ELSE
     524            2 :                orb_present(ikind) = .FALSE.
     525              :             END IF
     526              :          END IF
     527              : 
     528        63708 :          IF (ASSOCIATED(aux_basis_set)) THEN
     529            0 :             aux_present(ikind) = .TRUE.
     530              :          ELSE
     531        63708 :             aux_present(ikind) = .FALSE.
     532              :          END IF
     533              : 
     534        63708 :          IF (ASSOCIATED(aux_fit_basis_set)) THEN
     535         1856 :             aux_fit_present(ikind) = .TRUE.
     536         1856 :             CALL get_gto_basis_set(gto_basis_set=aux_fit_basis_set, kind_radius=aux_fit_radius(ikind))
     537              :          ELSE
     538        61852 :             aux_fit_present(ikind) = .FALSE.
     539              :          END IF
     540              : 
     541        63708 :          core_present(ikind) = .FALSE.
     542        63708 :          IF (ASSOCIATED(cneo_potential) .AND. ASSOCIATED(nuc_basis_set)) THEN
     543            8 :             cneo_present(ikind) = .TRUE.
     544            8 :             CALL get_gto_basis_set(gto_basis_set=nuc_basis_set, kind_radius=nuc_orb_radius(ikind))
     545              :          ELSE
     546        63700 :             IF (cneo_potential_present) cneo_present(ikind) = .FALSE.
     547              :             ! core overlap
     548              :             CALL get_qs_kind(qs_kind_set(ikind), &
     549              :                              alpha_core_charge=calpha(ikind), &
     550              :                              core_charge_radius=core_radius(ikind), &
     551        63700 :                              zeff=zeff(ikind))
     552        63700 :             IF (zeff(ikind) /= 0._dp .AND. calpha(ikind) /= 0._dp) THEN
     553        63522 :                core_present(ikind) = .TRUE.
     554              :             ELSE
     555          178 :                core_present(ikind) = .FALSE.
     556              :             END IF
     557              :          END IF
     558              : 
     559              :          ! Pseudopotentials
     560        63708 :          IF (gth_potential_present .OR. sgp_potential_present) THEN
     561        20490 :             IF (ASSOCIATED(gth_potential)) THEN
     562              :                CALL get_potential(potential=gth_potential, &
     563              :                                   ppl_present=ppl_present(ikind), &
     564              :                                   ppl_radius=ppl_radius(ikind), &
     565              :                                   ppnl_present=ppnl_present(ikind), &
     566        20130 :                                   ppnl_radius=ppnl_radius(ikind))
     567          360 :             ELSE IF (ASSOCIATED(sgp_potential)) THEN
     568              :                CALL get_potential(potential=sgp_potential, &
     569              :                                   ppl_present=ppl_present(ikind), &
     570              :                                   ppl_radius=ppl_radius(ikind), &
     571              :                                   ppnl_present=ppnl_present(ikind), &
     572          148 :                                   ppnl_radius=ppnl_radius(ikind))
     573              :             ELSE
     574          212 :                ppl_present(ikind) = .FALSE.
     575          212 :                ppnl_present(ikind) = .FALSE.
     576              :             END IF
     577              :          END IF
     578              : 
     579              :          ! GAPW
     580        63708 :          IF (paw_atom_present) THEN
     581         4700 :             IF (paw_atom) THEN
     582         4502 :                oce_present(ikind) = .TRUE.
     583         4502 :                CALL get_paw_proj_set(paw_proj_set=paw_proj, rcprj=oce_radius(ikind))
     584              :             ELSE
     585          198 :                oce_present(ikind) = .FALSE.
     586              :             END IF
     587              :          END IF
     588              : 
     589              :          ! Check the presence of an all electron potential or ERFC potential
     590       159327 :          IF (all_potential_present .OR. sgp_potential_present) THEN
     591        43430 :             all_present(ikind) = .FALSE.
     592        43430 :             all_pot_rad(ikind) = 0.0_dp
     593        43430 :             IF (ASSOCIATED(all_potential)) THEN
     594        43244 :                all_present(ikind) = .TRUE.
     595        43244 :                CALL get_potential(potential=all_potential, core_charge_radius=all_pot_rad(ikind))
     596          186 :             ELSE IF (ASSOCIATED(sgp_potential)) THEN
     597          148 :                IF (sgp_potential%ecp_local) THEN
     598          136 :                   all_present(ikind) = .TRUE.
     599          136 :                   CALL get_potential(potential=sgp_potential, core_charge_radius=all_pot_rad(ikind))
     600              :                END IF
     601              :             END IF
     602              :          END IF
     603              : 
     604              :       END DO
     605              : 
     606              :       ! Build the orbital-orbital overlap neighbor lists
     607        31911 :       IF (pdist < 0.0_dp) THEN
     608              :          pdist = MAX(plane_distance(1, 0, 0, cell), &
     609              :                      plane_distance(0, 1, 0, cell), &
     610            4 :                      plane_distance(0, 0, 1, cell))
     611              :       END IF
     612        31911 :       CALL pair_radius_setup(orb_present, orb_present, orb_radius, orb_radius, pair_radius, pdist)
     613              :       CALL build_neighbor_lists(sab_orb, particle_set, atom2d, cell, pair_radius, &
     614              :                                 mic=mic, subcells=subcells, molecular=molecule_only, nlname="sab_orb", &
     615        31911 :                                 stable_images=stable_images)
     616        31911 :       CALL set_ks_env(ks_env=ks_env, sab_orb=sab_orb)
     617              :       CALL write_neighbor_lists(sab_orb, particle_set, cell, para_env, neighbor_list_section, &
     618        31911 :                                 "/SAB_ORB", "sab_orb", "ORBITAL ORBITAL")
     619              : 
     620              :       ! Build orbital-orbital list containing all the pairs, to be used with
     621              :       ! non-symmetric operators. Beware: the cutoff of the orbital-orbital overlap
     622              :       ! might not be optimal. It should be verified for each operator.
     623        31911 :       IF (.NOT. (nddo .OR. dftb .OR. xtb)) THEN
     624              :          CALL build_neighbor_lists(sab_all, particle_set, atom2d, cell, pair_radius, &
     625              :                                    mic=mic, symmetric=.FALSE., subcells=subcells, molecular=molecule_only, &
     626        12601 :                                    nlname="sab_all", stable_images=stable_images)
     627        12601 :          CALL set_ks_env(ks_env=ks_env, sab_all=sab_all)
     628              :       END IF
     629              : 
     630              :       ! Build the core-core overlap neighbor lists
     631        31911 :       IF (.NOT. (nddo .OR. dftb .OR. xtb)) THEN
     632        12601 :          CALL pair_radius_setup(core_present, core_present, core_radius, core_radius, pair_radius)
     633              :          CALL build_neighbor_lists(sab_core, particle_set, atom2d, cell, pair_radius, subcells=subcells, &
     634        12601 :                                    operator_type="PP", nlname="sab_core", stable_images=stable_images)
     635        12601 :          CALL set_ks_env(ks_env=ks_env, sab_core=sab_core)
     636              :          CALL write_neighbor_lists(sab_core, particle_set, cell, para_env, neighbor_list_section, &
     637        12601 :                                    "/SAB_CORE", "sab_core", "CORE CORE")
     638              :       END IF
     639              : 
     640        31911 :       IF (dokp) THEN
     641              :          ! We try to guess an integration radius for K-points
     642              :          ! For non-HFX calculations we use the overlap list
     643              :          ! For HFX we use the interaction radius of kinds (ORB or ADMM basis)
     644              :          ! plus a range for the operator
     645         3562 :          IF (do_hfx) THEN
     646              : 
     647              :             !case study on the HFX potential: TC, SR or Overlap?
     648           88 :             CALL section_vals_val_get(hfx_sections, "INTERACTION_POTENTIAL%POTENTIAL_TYPE", i_val=hfx_pot)
     649              : 
     650           34 :             SELECT CASE (hfx_pot)
     651              :             CASE (do_potential_id)
     652           34 :                roperator = 0.0_dp
     653              :             CASE (do_potential_truncated)
     654           54 :                CALL section_vals_val_get(hfx_sections, "INTERACTION_POTENTIAL%CUTOFF_RADIUS", r_val=roperator)
     655              :             CASE (do_potential_mix_cl_trunc)
     656            8 :                CALL section_vals_val_get(hfx_sections, "INTERACTION_POTENTIAL%CUTOFF_RADIUS", r_val=roperator)
     657              :             CASE (do_potential_short)
     658            0 :                CALL section_vals_val_get(hfx_sections, "INTERACTION_POTENTIAL%OMEGA", r_val=omega)
     659            0 :                CALL section_vals_val_get(hfx_sections, "SCREENING%EPS_SCHWARZ", r_val=eps_schwarz)
     660            0 :                CALL erfc_cutoff(eps_schwarz, omega, roperator)
     661              :             CASE DEFAULT
     662           88 :                CPABORT("HFX potential not available for K-points (NYI)")
     663              :             END SELECT
     664              : 
     665           88 :             IF (dft_control%do_admm) THEN
     666              :                CALL pair_radius_setup(aux_fit_present, aux_fit_present, aux_fit_radius, aux_fit_radius, &
     667           48 :                                       pair_radius)
     668              : 
     669              :                !We cannot accept a pair radius smaller than the ORB overlap, for sanity reasons
     670          144 :                ALLOCATE (pair_radius_lb(nkind, nkind))
     671           48 :                CALL pair_radius_setup(orb_present, orb_present, orb_radius, orb_radius, pair_radius_lb)
     672          122 :                DO jkind = 1, nkind
     673          248 :                   DO ikind = 1, nkind
     674          200 :                      IF (pair_radius(ikind, jkind) + cutoff_screen_factor*roperator <= pair_radius_lb(ikind, jkind)) THEN
     675           84 :                         pair_radius(ikind, jkind) = pair_radius_lb(ikind, jkind) - roperator
     676              :                      END IF
     677              :                   END DO
     678              :                END DO
     679              :             ELSE
     680           40 :                CALL pair_radius_setup(orb_present, orb_present, orb_radius, orb_radius, pair_radius)
     681              :             END IF
     682          448 :             pair_radius = pair_radius + cutoff_screen_factor*roperator
     683              :          ELSE
     684         3474 :             CALL pair_radius_setup(orb_present, orb_present, orb_radius, orb_radius, pair_radius)
     685              :          END IF
     686              :          CALL build_neighbor_lists(sab_kp, particle_set, atom2d, cell, pair_radius, &
     687         3562 :                                    subcells=subcells, nlname="sab_kp", stable_images=stable_images)
     688         3562 :          CALL set_ks_env(ks_env=ks_env, sab_kp=sab_kp)
     689              : 
     690         3562 :          IF (do_hfx) THEN
     691              :             CALL build_neighbor_lists(sab_kp_nosym, particle_set, atom2d, cell, pair_radius, &
     692              :                                       subcells=subcells, nlname="sab_kp_nosym", symmetric=.FALSE., &
     693           88 :                                       stable_images=stable_images)
     694           88 :             CALL set_ks_env(ks_env=ks_env, sab_kp_nosym=sab_kp_nosym)
     695              :          END IF
     696              :       END IF
     697              : 
     698              :       ! Build orbital GTH-PPL operator overlap list
     699        31911 :       IF (gth_potential_present .OR. sgp_potential_present) THEN
     700        11709 :          IF (ANY(ppl_present)) THEN
     701        11611 :             CALL pair_radius_setup(orb_present, ppl_present, orb_radius, ppl_radius, pair_radius)
     702              :             CALL build_neighbor_lists(sac_ppl, particle_set, atom2d, cell, pair_radius, &
     703              :                                       subcells=subcells, operator_type="ABC", nlname="sac_ppl", &
     704        11611 :                                       stable_images=stable_images)
     705        11611 :             CALL set_ks_env(ks_env=ks_env, sac_ppl=sac_ppl)
     706              :             CALL write_neighbor_lists(sac_ppl, particle_set, cell, para_env, neighbor_list_section, &
     707        11611 :                                       "/SAC_PPL", "sac_ppl", "ORBITAL GTH-PPL")
     708        11611 :             IF (lrigpw) THEN
     709           60 :                IF (qs_env%lri_env%ppl_ri) THEN
     710              :                   CALL build_neighbor_lists(sac_lri, particle_set, atom2d, cell, pair_radius, &
     711              :                                             subcells=subcells, symmetric=.FALSE., operator_type="PP", &
     712            2 :                                             nlname="sac_lri", stable_images=stable_images)
     713            2 :                   CALL set_ks_env(ks_env=ks_env, sac_lri=sac_lri)
     714              :                END IF
     715              :             END IF
     716              :          END IF
     717              : 
     718        15335 :          IF (ANY(ppnl_present)) THEN
     719         9255 :             CALL pair_radius_setup(orb_present, ppnl_present, orb_radius, ppnl_radius, pair_radius)
     720              :             CALL build_neighbor_lists(sap_ppnl, particle_set, atom2d, cell, pair_radius, &
     721              :                                       subcells=subcells, operator_type="ABBA", nlname="sap_ppnl", &
     722         9255 :                                       stable_images=stable_images)
     723         9255 :             CALL set_ks_env(ks_env=ks_env, sap_ppnl=sap_ppnl)
     724              :             CALL write_neighbor_lists(sap_ppnl, particle_set, cell, para_env, neighbor_list_section, &
     725         9255 :                                       "/SAP_PPNL", "sap_ppnl", "ORBITAL GTH-PPNL")
     726              :          END IF
     727              :       END IF
     728              : 
     729        31911 :       IF (paw_atom_present) THEN
     730              :          ! Build orbital-GAPW projector overlap list
     731         2548 :          IF (ANY(oce_present)) THEN
     732         2478 :             CALL pair_radius_setup(orb_present, oce_present, orb_radius, oce_radius, pair_radius)
     733              :             CALL build_neighbor_lists(sap_oce, particle_set, atom2d, cell, pair_radius, &
     734              :                                       subcells=subcells, operator_type="ABBA", nlname="sap_oce", &
     735         2478 :                                       stable_images=stable_images)
     736         2478 :             CALL set_ks_env(ks_env=ks_env, sap_oce=sap_oce)
     737              :             CALL write_neighbor_lists(sap_oce, particle_set, cell, para_env, neighbor_list_section, &
     738         2478 :                                       "/SAP_OCE", "sap_oce", "ORBITAL(A) PAW-PRJ")
     739              :          END IF
     740              :       END IF
     741              : 
     742              :       ! Build orbital-ERFC potential list
     743        31911 :       IF (.NOT. (nddo .OR. dftb .OR. xtb)) THEN
     744        12601 :          IF (all_potential_present .OR. sgp_potential_present) THEN
     745         1102 :             CALL pair_radius_setup(orb_present, all_present, orb_radius, all_pot_rad, pair_radius)
     746              :             CALL build_neighbor_lists(sac_ae, particle_set, atom2d, cell, pair_radius, &
     747              :                                       subcells=subcells, operator_type="ABC", nlname="sac_ae", &
     748         1102 :                                       stable_images=stable_images)
     749         1102 :             CALL set_ks_env(ks_env=ks_env, sac_ae=sac_ae)
     750              :             CALL write_neighbor_lists(sac_ae, particle_set, cell, para_env, neighbor_list_section, &
     751         1102 :                                       "/SAC_AE", "sac_ae", "ORBITAL ERFC POTENTIAL")
     752              :          END IF
     753              :       END IF
     754              : 
     755              :       ! Build quantum nuclear orbital-classical nuclear ERFC potential list for CNEO
     756        31911 :       IF (cneo_potential_present) THEN
     757            8 :          CALL pair_radius_setup(cneo_present, core_present, nuc_orb_radius, core_radius, pair_radius)
     758              :          CALL build_neighbor_lists(sab_cneo, particle_set, atom2d, cell, pair_radius, &
     759            8 :                                    subcells=subcells, symmetric=.FALSE., operator_type="PP", nlname="sab_cneo")
     760            8 :          CALL set_ks_env(ks_env=ks_env, sab_cneo=sab_cneo)
     761              :          CALL write_neighbor_lists(sab_cneo, particle_set, cell, para_env, neighbor_list_section, &
     762            8 :                                    "/SAB_CNEO", "sab_cneo", "NUCLEAR ORBITAL ERFC POTENTIAL")
     763              :       END IF
     764              : 
     765        31911 :       IF (nddo) THEN
     766              :          ! Semi-empirical neighbor lists
     767        18514 :          default_present = .TRUE.
     768        18514 :          c_radius = dft_control%qs_control%se_control%cutoff_cou
     769              :          ! Build the neighbor lists for the Hartree terms
     770         5804 :          CALL pair_radius_setup(default_present, default_present, c_radius, c_radius, pair_radius)
     771         5804 :          IF (dft_control%qs_control%se_control%do_ewald_gks) THEN
     772              :             ! Use MIC for the periodic code of GKS
     773              :             CALL build_neighbor_lists(sab_se, particle_set, atom2d, cell, pair_radius, mic=mic, &
     774            2 :                                       subcells=subcells, nlname="sab_se")
     775              :          ELSE
     776              :             CALL build_neighbor_lists(sab_se, particle_set, atom2d, cell, pair_radius, &
     777         5802 :                                       subcells=subcells, nlname="sab_se")
     778              :          END IF
     779         5804 :          CALL set_ks_env(ks_env=ks_env, sab_se=sab_se)
     780              :          CALL write_neighbor_lists(sab_se, particle_set, cell, para_env, neighbor_list_section, &
     781         5804 :                                    "/SAB_SE", "sab_se", "HARTREE INTERACTIONS")
     782              : 
     783              :          ! If requested build the SE long-range correction neighbor list
     784         5804 :          IF ((dft_control%qs_control%se_control%do_ewald) .AND. &
     785              :              (dft_control%qs_control%se_control%integral_screening /= do_se_IS_slater)) THEN
     786          328 :             c_radius = dft_control%qs_control%se_control%cutoff_lrc
     787          140 :             CALL pair_radius_setup(default_present, default_present, c_radius, c_radius, pair_radius)
     788              :             CALL build_neighbor_lists(sab_lrc, particle_set, atom2d, cell, pair_radius, &
     789          140 :                                       subcells=subcells, nlname="sab_lrc")
     790          140 :             CALL set_ks_env(ks_env=ks_env, sab_lrc=sab_lrc)
     791              :             CALL write_neighbor_lists(sab_lrc, particle_set, cell, para_env, neighbor_list_section, &
     792          140 :                                       "/SAB_LRC", "sab_lrc", "SE LONG-RANGE CORRECTION")
     793              :          END IF
     794              :       END IF
     795              : 
     796        31911 :       IF (dftb) THEN
     797              :          ! Build the neighbor lists for the DFTB Ewald methods
     798         4312 :          IF (dft_control%qs_control%dftb_control%do_ewald) THEN
     799         1486 :             CALL get_qs_env(qs_env=qs_env, ewald_env=ewald_env)
     800         1486 :             CALL ewald_env_get(ewald_env, rcut=rcut)
     801         4344 :             c_radius = rcut
     802         1486 :             CALL pair_radius_setup(orb_present, orb_present, c_radius, c_radius, pair_radius)
     803              :             CALL build_neighbor_lists(sab_tbe, particle_set, atom2d, cell, pair_radius, mic=mic, &
     804         1486 :                                       subcells=subcells, nlname="sab_tbe")
     805         1486 :             CALL set_ks_env(ks_env=ks_env, sab_tbe=sab_tbe)
     806              :          END IF
     807              : 
     808              :          ! Build the neighbor lists for the DFTB vdW pair potential
     809         4312 :          IF (dft_control%qs_control%dftb_control%dispersion) THEN
     810         1226 :             IF (dft_control%qs_control%dftb_control%dispersion_type == dispersion_uff) THEN
     811         3378 :                DO ikind = 1, nkind
     812         2242 :                   CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_atom)
     813         3378 :                   CALL get_dftb_atom_param(dftb_parameter=dftb_atom, rcdisp=c_radius(ikind))
     814              :                END DO
     815         3378 :                default_present = .TRUE.
     816         1136 :                CALL pair_radius_setup(default_present, default_present, c_radius, c_radius, pair_radius)
     817              :                CALL build_neighbor_lists(sab_vdw, particle_set, atom2d, cell, pair_radius, &
     818         1136 :                                          subcells=subcells, nlname="sab_vdw")
     819         1136 :                CALL set_ks_env(ks_env=ks_env, sab_vdw=sab_vdw)
     820              :             END IF
     821              :          END IF
     822              :       END IF
     823              : 
     824        31911 :       IF (xtb .AND. (.NOT. dft_control%qs_control%xtb_control%do_tblite)) THEN
     825              :          ! Build the neighbor lists for the xTB Ewald method
     826         6408 :          IF (dft_control%qs_control%xtb_control%do_ewald) THEN
     827         2568 :             CALL get_qs_env(qs_env=qs_env, ewald_env=ewald_env)
     828         2568 :             CALL ewald_env_get(ewald_env, rcut=rcut)
     829         8624 :             c_radius = rcut
     830         2568 :             CALL pair_radius_setup(orb_present, orb_present, c_radius, c_radius, pair_radius)
     831              :             CALL build_neighbor_lists(sab_tbe, particle_set, atom2d, cell, pair_radius, mic=mic, &
     832         2568 :                                       subcells=subcells, nlname="sab_tbe")
     833         2568 :             CALL set_ks_env(ks_env=ks_env, sab_tbe=sab_tbe)
     834              :          END IF
     835              :          ! Repulsive Potential
     836        60828 :          pair_radius(1:nkind, 1:nkind) = dft_control%qs_control%xtb_control%rcpair(1:nkind, 1:nkind)
     837        21588 :          default_present = .TRUE.
     838              :          CALL build_neighbor_lists(sab_xtb_pp, particle_set, atom2d, cell, pair_radius, &
     839         6408 :                                    subcells=subcells, nlname="sab_xtb_pp")
     840         6408 :          CALL set_ks_env(ks_env=ks_env, sab_xtb_pp=sab_xtb_pp)
     841              :          ! SR part of Coulomb interaction
     842        21588 :          DO ikind = 1, nkind
     843        15180 :             CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_atom)
     844        21588 :             CALL get_xtb_atom_param(xtb_parameter=xtb_atom, rcut=c_radius(ikind))
     845              :          END DO
     846        21588 :          default_present = .TRUE.
     847         6408 :          CALL pair_radius_setup(default_present, default_present, c_radius, c_radius, pair_radius)
     848              :          CALL build_neighbor_lists(sab_xtbe, particle_set, atom2d, cell, pair_radius, &
     849         6408 :                                    subcells=subcells, nlname="sab_xtbe")
     850         6408 :          CALL set_ks_env(ks_env=ks_env, sab_xtbe=sab_xtbe)
     851              :          ! XB list
     852        19224 :          ALLOCATE (xb1_atom(nkind), xb2_atom(nkind))
     853        21588 :          c_radius = 0.5_dp*dft_control%qs_control%xtb_control%xb_radius
     854        21588 :          DO ikind = 1, nkind
     855        15180 :             CALL get_atomic_kind(atomic_kind_set(ikind), z=zat)
     856        15180 :             IF (zat == 17 .OR. zat == 35 .OR. zat == 53 .OR. zat == 85) THEN
     857          130 :                xb1_atom(ikind) = .TRUE.
     858              :             ELSE
     859        15050 :                xb1_atom(ikind) = .FALSE.
     860              :             END IF
     861        36768 :             IF (zat == 7 .OR. zat == 8 .OR. zat == 15 .OR. zat == 16) THEN
     862         5700 :                xb2_atom(ikind) = .TRUE.
     863              :             ELSE
     864         9480 :                xb2_atom(ikind) = .FALSE.
     865              :             END IF
     866              :          END DO
     867         6408 :          CALL pair_radius_setup(xb1_atom, xb2_atom, c_radius, c_radius, pair_radius)
     868              :          CALL build_neighbor_lists(sab_xb, particle_set, atom2d, cell, pair_radius, &
     869         6408 :                                    symmetric=.FALSE., subcells=subcells, operator_type="PP", nlname="sab_xb")
     870         6408 :          CALL set_ks_env(ks_env=ks_env, sab_xb=sab_xb)
     871              :          CALL write_neighbor_lists(sab_xb, particle_set, cell, para_env, neighbor_list_section, &
     872         6408 :                                    "/SAB_XB", "sab_xb", "XB bonding")
     873              : 
     874              :          ! nonbonded interactions list
     875              :          IF (dft_control%qs_control%xtb_control%do_nonbonded &
     876         6408 :              .AND. (.NOT. dft_control%qs_control%xtb_control%do_tblite)) THEN
     877           24 :             ngp = SIZE(dft_control%qs_control%xtb_control%nonbonded%pot)
     878           72 :             ALLOCATE (nonbond1_atom(nkind), nonbond2_atom(nkind))
     879           24 :             nonbond1_atom = .FALSE.
     880           24 :             nonbond2_atom = .FALSE.
     881           48 :             DO ingp = 1, ngp
     882          120 :                DO ikind = 1, nkind
     883           96 :                   rcut = SQRT(dft_control%qs_control%xtb_control%nonbonded%pot(ingp)%pot%rcutsq)
     884          480 :                   c_radius = rcut
     885           96 :                   CALL get_atomic_kind(atomic_kind_set(ikind), element_symbol=element_symbol)
     886           96 :                   CALL uppercase(element_symbol)
     887          120 :                   IF (TRIM(dft_control%qs_control%xtb_control%nonbonded%pot(ingp)%pot%at1) == TRIM(element_symbol)) THEN
     888           24 :                      nonbond1_atom(ikind) = .TRUE.
     889          120 :                      DO jkind = 1, nkind
     890           96 :                         CALL get_atomic_kind(atomic_kind_set(jkind), element_symbol=element_symbol2)
     891           96 :                         CALL uppercase(element_symbol2)
     892          120 :                         IF (TRIM(dft_control%qs_control%xtb_control%nonbonded%pot(ingp)%pot%at2) == TRIM(element_symbol2)) THEN
     893           24 :                            nonbond2_atom(jkind) = .TRUE.
     894              :                         END IF
     895              :                      END DO
     896              :                   END IF
     897              :                END DO
     898           24 :                CALL pair_radius_setup(nonbond1_atom, nonbond2_atom, c_radius, c_radius, pair_radius)
     899              :                CALL build_neighbor_lists(sab_xtb_nonbond, particle_set, atom2d, cell, pair_radius, &
     900           24 :                                          symmetric=.FALSE., subcells=subcells, operator_type="PP", nlname="sab_xtb_nonbond")
     901           24 :                CALL set_ks_env(ks_env=ks_env, sab_xtb_nonbond=sab_xtb_nonbond)
     902              :                CALL write_neighbor_lists(sab_xtb_nonbond, particle_set, cell, para_env, neighbor_list_section, &
     903           48 :                                          "/SAB_XTB_NONBOND", "sab_xtb_nonbond", "XTB NONBONDED INTERACTIONS")
     904              :             END DO
     905              :          END IF
     906              :       END IF
     907              : 
     908              :       ! Build the neighbor lists for the vdW pair potential
     909        31911 :       IF (.NOT. dft_control%qs_control%xtb_control%do_tblite) THEN
     910        29125 :          CALL get_qs_env(qs_env=qs_env, dispersion_env=dispersion_env)
     911        29125 :          sab_vdw => dispersion_env%sab_vdw
     912        29125 :          sab_cn => dispersion_env%sab_cn
     913        29125 :          IF (dispersion_env%type == xc_vdw_fun_pairpot .OR. xtb) THEN
     914         6890 :             IF (dispersion_env%pp_type == vdw_pairpot_dftd4) THEN
     915         2976 :                c_radius(:) = dispersion_env%rc_d4
     916              :             ELSE
     917        20004 :                c_radius(:) = dispersion_env%rc_disp
     918              :             END IF
     919        22980 :             default_present = .TRUE. !include all atoms in vdW (even without basis)
     920         6890 :             CALL pair_radius_setup(default_present, default_present, c_radius, c_radius, pair_radius)
     921              :             CALL build_neighbor_lists(sab_vdw, particle_set, atom2d, cell, pair_radius, &
     922         6890 :                                       subcells=subcells, operator_type="PP", nlname="sab_vdw")
     923         6890 :             dispersion_env%sab_vdw => sab_vdw
     924              : 
     925              :             ! Build the neighbor lists for coordination numbers as needed by the DFT-D3/D4 method
     926              :             ! This is also needed for the xTB Hamiltonian
     927        22980 :             DO ikind = 1, nkind
     928        16090 :                CALL get_atomic_kind(atomic_kind_set(ikind), z=zat)
     929        22980 :                c_radius(ikind) = 4._dp*ptable(zat)%covalent_radius*bohr
     930              :             END DO
     931         6890 :             CALL pair_radius_setup(default_present, default_present, c_radius, c_radius, pair_radius)
     932              :             CALL build_neighbor_lists(sab_cn, particle_set, atom2d, cell, pair_radius, &
     933         6890 :                                       subcells=subcells, operator_type="PP", nlname="sab_cn")
     934         6890 :             dispersion_env%sab_cn => sab_cn
     935              :          END IF
     936              :       END IF
     937              : 
     938              :       ! Build the neighbor lists for the gCP pair potential
     939        31911 :       NULLIFY (gcp_env)
     940        31911 :       CALL get_qs_env(qs_env=qs_env, gcp_env=gcp_env)
     941        31911 :       IF (ASSOCIATED(gcp_env)) THEN
     942        12601 :          IF (gcp_env%do_gcp) THEN
     943            6 :             sab_gcp => gcp_env%sab_gcp
     944           14 :             DO ikind = 1, nkind
     945           14 :                c_radius(ikind) = gcp_env%gcp_kind(ikind)%rcsto
     946              :             END DO
     947            6 :             CALL pair_radius_setup(orb_present, orb_present, c_radius, c_radius, pair_radius)
     948              :             CALL build_neighbor_lists(sab_gcp, particle_set, atom2d, cell, pair_radius, &
     949            6 :                                       subcells=subcells, operator_type="PP", nlname="sab_gcp")
     950            6 :             gcp_env%sab_gcp => sab_gcp
     951              :          ELSE
     952        12595 :             NULLIFY (gcp_env%sab_gcp)
     953              :          END IF
     954              :       END IF
     955              : 
     956        31911 :       IF (lrigpw .OR. lri_optbas) THEN
     957              :          ! set neighborlists in lri_env environment
     958           66 :          CALL pair_radius_setup(orb_present, orb_present, orb_radius, orb_radius, pair_radius)
     959           66 :          soo_list => qs_env%lri_env%soo_list
     960              :          CALL build_neighbor_lists(soo_list, particle_set, atom2d, cell, pair_radius, &
     961           66 :                                    mic=mic, molecular=molecule_only, subcells=subcells, nlname="soo_list")
     962           66 :          qs_env%lri_env%soo_list => soo_list
     963              :          CALL write_neighbor_lists(soo_list, particle_set, cell, para_env, neighbor_list_section, &
     964           66 :                                    "/SOO_LIST", "soo_list", "ORBITAL ORBITAL (RI)")
     965        31845 :       ELSE IF (rigpw) THEN
     966            6 :          ALLOCATE (ri_present(nkind), ri_radius(nkind))
     967            2 :          ri_present = .FALSE.
     968            2 :          ri_radius = 0.0_dp
     969            4 :          DO ikind = 1, nkind
     970            2 :             CALL get_qs_kind(qs_kind_set(ikind), basis_set=ri_basis_set, basis_type="RI_HXC")
     971            4 :             IF (ASSOCIATED(ri_basis_set)) THEN
     972            2 :                ri_present(ikind) = .TRUE.
     973            2 :                CALL get_gto_basis_set(gto_basis_set=ri_basis_set, kind_radius=ri_radius(ikind))
     974              :             ELSE
     975            0 :                ri_present(ikind) = .FALSE.
     976              :             END IF
     977              :          END DO
     978              :          ! set neighborlists in lri_env environment
     979            2 :          CALL pair_radius_setup(orb_present, orb_present, orb_radius, orb_radius, pair_radius)
     980            2 :          soo_list => qs_env%lri_env%soo_list
     981              :          CALL build_neighbor_lists(soo_list, particle_set, atom2d, cell, pair_radius, &
     982            2 :                                    mic=mic, molecular=molecule_only, subcells=subcells, nlname="soo_list")
     983            2 :          qs_env%lri_env%soo_list => soo_list
     984              :          !
     985            2 :          CALL pair_radius_setup(ri_present, ri_present, ri_radius, ri_radius, pair_radius)
     986            2 :          saa_list => qs_env%lri_env%saa_list
     987              :          CALL build_neighbor_lists(saa_list, particle_set, atom2d, cell, pair_radius, &
     988            2 :                                    mic=mic, molecular=molecule_only, subcells=subcells, nlname="saa_list")
     989            2 :          qs_env%lri_env%saa_list => saa_list
     990              :          !
     991            2 :          CALL pair_radius_setup(ri_present, orb_present, ri_radius, orb_radius, pair_radius)
     992            2 :          soa_list => qs_env%lri_env%soa_list
     993              :          CALL build_neighbor_lists(soa_list, particle_set, atom2d, cell, pair_radius, &
     994              :                                    mic=mic, symmetric=.FALSE., molecular=molecule_only, &
     995            2 :                                    subcells=subcells, operator_type="ABC", nlname="saa_list")
     996            2 :          qs_env%lri_env%soa_list => soa_list
     997              :       END IF
     998              : 
     999              :       ! Build the neighbor lists for the ALMO delocalization
    1000        31911 :       IF (almo) THEN
    1001          378 :          DO ikind = 1, nkind
    1002          256 :             CALL get_atomic_kind(atomic_kind_set(ikind), rcov=almo_rcov, rvdw=almo_rvdw)
    1003              :             ! multiply the radius by some hard-coded number
    1004              :             c_radius(ikind) = MAX(almo_rcov, almo_rvdw)*bohr* &
    1005          378 :                               almo_max_cutoff_multiplier
    1006              :          END DO
    1007          378 :          default_present = .TRUE. !include all atoms (even without basis)
    1008          122 :          CALL pair_radius_setup(default_present, default_present, c_radius, c_radius, pair_radius)
    1009              :          CALL build_neighbor_lists(sab_almo, particle_set, atom2d, cell, pair_radius, &
    1010          122 :                                    subcells=subcells, operator_type="PP", nlname="sab_almo")
    1011          122 :          CALL set_ks_env(ks_env=ks_env, sab_almo=sab_almo)
    1012              :       END IF
    1013              : 
    1014              :       ! Print particle distribution
    1015        31911 :       print_key_path = "PRINT%DISTRIBUTION"
    1016        31911 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, force_env_section, &
    1017              :                                            print_key_path), &
    1018              :                 cp_p_file)) THEN
    1019              :          iw = cp_print_key_unit_nr(logger=logger, &
    1020              :                                    basis_section=force_env_section, &
    1021              :                                    print_key_path=print_key_path, &
    1022          166 :                                    extension=".out")
    1023          166 :          CALL write_neighbor_distribution(sab_orb, qs_kind_set, iw, para_env)
    1024              :          CALL cp_print_key_finished_output(unit_nr=iw, &
    1025              :                                            logger=logger, &
    1026              :                                            basis_section=force_env_section, &
    1027          166 :                                            print_key_path=print_key_path)
    1028              :       END IF
    1029              : 
    1030              :       ! Release work storage
    1031        31911 :       CALL atom2d_cleanup(atom2d)
    1032              : 
    1033        31911 :       DEALLOCATE (atom2d)
    1034        31911 :       DEALLOCATE (orb_present, default_present, core_present)
    1035        31911 :       DEALLOCATE (orb_radius, aux_fit_radius, c_radius, core_radius)
    1036        31911 :       DEALLOCATE (calpha, zeff)
    1037        31911 :       DEALLOCATE (pair_radius)
    1038        31911 :       IF (gth_potential_present .OR. sgp_potential_present) THEN
    1039        11613 :          DEALLOCATE (ppl_present, ppl_radius)
    1040        11613 :          DEALLOCATE (ppnl_present, ppnl_radius)
    1041              :       END IF
    1042        31911 :       IF (paw_atom_present) THEN
    1043         2478 :          DEALLOCATE (oce_present, oce_radius)
    1044              :       END IF
    1045        31911 :       IF (all_potential_present .OR. sgp_potential_present) THEN
    1046        20412 :          DEALLOCATE (all_present, all_pot_rad)
    1047              :       END IF
    1048        31911 :       IF (cneo_potential_present) THEN
    1049            8 :          DEALLOCATE (cneo_present, nuc_orb_radius)
    1050              :       END IF
    1051              : 
    1052        31911 :       CALL timestop(handle)
    1053              : 
    1054        95733 :    END SUBROUTINE build_qs_neighbor_lists
    1055              : 
    1056              : ! **************************************************************************************************
    1057              : !> \brief   Build simple pair neighbor lists.
    1058              : !> \param ab_list ...
    1059              : !> \param particle_set ...
    1060              : !> \param atom ...
    1061              : !> \param cell ...
    1062              : !> \param pair_radius ...
    1063              : !> \param subcells ...
    1064              : !> \param mic ...
    1065              : !> \param symmetric ...
    1066              : !> \param molecular ...
    1067              : !> \param subset_of_mol ...
    1068              : !> \param current_subset ...
    1069              : !> \param operator_type ...
    1070              : !> \param nlname ...
    1071              : !> \param atomb_to_keep the list of atom indices to keep for pairs from the atom2d%b_list
    1072              : !> \param stable_images use a deterministic half-cell convention for atom images
    1073              : !> \date    20.03.2002
    1074              : !> \par History
    1075              : !>          - Major refactoring (25.07.2010,jhu)
    1076              : !>          - Added option to filter out atoms from list_b (08.2018, A.  Bussy)
    1077              : !> \author  MK
    1078              : !> \version 2.0
    1079              : ! **************************************************************************************************
    1080       151892 :    SUBROUTINE build_neighbor_lists(ab_list, particle_set, atom, cell, pair_radius, subcells, &
    1081              :                                    mic, symmetric, molecular, subset_of_mol, current_subset, &
    1082       151892 :                                    operator_type, nlname, atomb_to_keep, stable_images)
    1083              : 
    1084              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1085              :          POINTER                                         :: ab_list
    1086              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1087              :       TYPE(local_atoms_type), DIMENSION(:), INTENT(IN)   :: atom
    1088              :       TYPE(cell_type), POINTER                           :: cell
    1089              :       REAL(dp), DIMENSION(:, :), INTENT(IN)              :: pair_radius
    1090              :       REAL(dp), INTENT(IN)                               :: subcells
    1091              :       LOGICAL, INTENT(IN), OPTIONAL                      :: mic, symmetric, molecular
    1092              :       INTEGER, DIMENSION(:), OPTIONAL, POINTER           :: subset_of_mol
    1093              :       INTEGER, OPTIONAL                                  :: current_subset
    1094              :       CHARACTER(LEN=*), INTENT(IN), OPTIONAL             :: operator_type
    1095              :       CHARACTER(LEN=*), INTENT(IN)                       :: nlname
    1096              :       INTEGER, DIMENSION(:), INTENT(IN), OPTIONAL        :: atomb_to_keep
    1097              :       LOGICAL, INTENT(IN), OPTIONAL                      :: stable_images
    1098              : 
    1099              :       CHARACTER(len=*), PARAMETER :: routineN = 'build_neighbor_lists'
    1100              : 
    1101              :       INTEGER :: atom_a, atom_b, handle, i, iab, iatom, iatom_local, iatom_subcell, icell, ikind, &
    1102              :          inode, j, jatom, jatom_local, jcell, jkind, k, kcell, maxat, mol_a, mol_b, natom, nentry, &
    1103              :          nkind, nnode, otype
    1104       151892 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: nlista, nlistb
    1105              :       INTEGER, DIMENSION(3)                              :: cell_b, ncell, nsubcell, periodic
    1106       151892 :       INTEGER, DIMENSION(:), POINTER                     :: index_list
    1107              :       LOGICAL                                            :: include_ab, my_mic, my_molecular, &
    1108              :                                                             my_sort_atomb, my_stable_images, &
    1109              :                                                             my_symmetric
    1110       151892 :       LOGICAL, ALLOCATABLE, DIMENSION(:)                 :: pres_a, pres_b
    1111              :       REAL(dp)                                           :: deth, rab2, rab2_max, rab_max, rabm, &
    1112              :                                                             subcell_scale
    1113              :       REAL(dp), DIMENSION(3)                             :: pd, r, ra, rab, rab_pbc, rb, sab_max, &
    1114              :                                                             sab_max_guard, sb, sb_max, sb_min, &
    1115              :                                                             sb_pbc
    1116       151892 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: r_pbc
    1117       151892 :       TYPE(local_lists), DIMENSION(:), POINTER           :: lista, listb
    1118              :       TYPE(neighbor_list_iterator_p_type), &
    1119       151892 :          DIMENSION(:), POINTER                           :: nl_iterator
    1120              :       TYPE(neighbor_list_p_type), ALLOCATABLE, &
    1121       151892 :          DIMENSION(:)                                    :: kind_a
    1122              :       TYPE(neighbor_list_set_type), POINTER              :: neighbor_list_set
    1123       151892 :       TYPE(subcell_type), DIMENSION(:, :, :), POINTER    :: subcell
    1124              : 
    1125       151892 :       CALL timeset(routineN//"_"//TRIM(nlname), handle)
    1126              : 
    1127              :       ! input options
    1128       151892 :       my_mic = .FALSE.
    1129       151892 :       IF (PRESENT(mic)) my_mic = mic
    1130       151892 :       my_symmetric = .TRUE.
    1131       151892 :       IF (PRESENT(symmetric)) my_symmetric = symmetric
    1132       151892 :       my_molecular = .FALSE.
    1133              :       ! if we have a molecular NL, MIC has to be used
    1134       151892 :       IF (PRESENT(molecular)) my_molecular = molecular
    1135       151892 :       my_stable_images = .FALSE.
    1136       151892 :       IF (PRESENT(stable_images)) my_stable_images = stable_images
    1137              :       ! check for operator types
    1138       151892 :       IF (PRESENT(operator_type)) THEN
    1139              :          SELECT CASE (operator_type)
    1140              :          CASE ("AB")
    1141        13743 :             otype = 1 ! simple overlap
    1142              :          CASE ("ABC")
    1143        13743 :             otype = 2 ! for three center operators
    1144        13743 :             CPASSERT(.NOT. my_molecular)
    1145        13743 :             my_symmetric = .FALSE.
    1146              :          CASE ("ABBA")
    1147        12933 :             otype = 3 ! for separable nonlocal operators
    1148        12933 :             my_symmetric = .FALSE.
    1149              :          CASE ("PP")
    1150        33051 :             otype = 4 ! simple atomic pair potential list
    1151              :          CASE default
    1152              :             CALL cp_abort(__LOCATION__, &
    1153              :                           "<AB>, <ABC>, <ABBA>, <PP> are supported as the <operator_type> "// &
    1154              :                           "for build_neighbor_lists, found unknown option "// &
    1155        59727 :                           "<"//TRIM(operator_type)//">")
    1156              :          END SELECT
    1157              :       ELSE
    1158              :          ! default is a simple AB neighbor list
    1159              :          otype = 1
    1160              :       END IF
    1161       151892 :       my_sort_atomb = .FALSE.
    1162       151892 :       IF (PRESENT(atomb_to_keep)) THEN
    1163          424 :          my_sort_atomb = .TRUE.
    1164              :       END IF
    1165              : 
    1166       151892 :       nkind = SIZE(atom)
    1167              :       ! Deallocate the old neighbor list structure
    1168       151892 :       CALL release_neighbor_list_sets(ab_list)
    1169              :       ! Allocate and initialize the new neighbor list structure
    1170      1113842 :       ALLOCATE (ab_list(nkind*nkind))
    1171       810058 :       DO iab = 1, SIZE(ab_list)
    1172       658166 :          NULLIFY (ab_list(iab)%neighbor_list_set)
    1173       658166 :          ab_list(iab)%nl_size = -1
    1174       658166 :          ab_list(iab)%nl_start = -1
    1175       658166 :          ab_list(iab)%nl_end = -1
    1176       810058 :          NULLIFY (ab_list(iab)%nlist_task)
    1177              :       END DO
    1178              : 
    1179              :       ! Allocate and initialize the kind availability
    1180       607568 :       ALLOCATE (pres_a(nkind), pres_b(nkind))
    1181       447822 :       DO ikind = 1, nkind
    1182       339034 :          pres_a(ikind) = ANY(pair_radius(ikind, :) > 0._dp)
    1183       507058 :          pres_b(ikind) = ANY(pair_radius(:, ikind) > 0._dp)
    1184              :       END DO
    1185              : 
    1186              :       ! create a copy of the pbc'ed coordinates
    1187       151892 :       natom = SIZE(particle_set)
    1188       455676 :       ALLOCATE (r_pbc(3, natom))
    1189       908635 :       DO i = 1, natom
    1190       908635 :          IF (my_stable_images) THEN
    1191        32122 :             r_pbc(1:3, i) = pbc_stable(particle_set(i)%r(1:3), cell)
    1192              :          ELSE
    1193       724621 :             r_pbc(1:3, i) = pbc(particle_set(i)%r(1:3), cell)
    1194              :          END IF
    1195              :       END DO
    1196              : 
    1197              :       ! setup the local lists of atoms
    1198       151892 :       maxat = 0
    1199       447822 :       DO ikind = 1, nkind
    1200       447822 :          maxat = MAX(maxat, SIZE(atom(ikind)%list))
    1201              :       END DO
    1202       455676 :       ALLOCATE (index_list(maxat))
    1203       656949 :       DO i = 1, maxat
    1204       656949 :          index_list(i) = i
    1205              :       END DO
    1206      1503212 :       ALLOCATE (lista(nkind), listb(nkind), nlista(nkind), nlistb(nkind))
    1207       151892 :       nlista = 0
    1208       151892 :       nlistb = 0
    1209       447822 :       DO ikind = 1, nkind
    1210       295930 :          NULLIFY (lista(ikind)%list, listb(ikind)%list)
    1211       151892 :          SELECT CASE (otype)
    1212              :          CASE (1)
    1213       177630 :             IF (ASSOCIATED(atom(ikind)%list_local_a_index)) THEN
    1214       124603 :                lista(ikind)%list => atom(ikind)%list_local_a_index
    1215       124603 :                nlista(ikind) = SIZE(lista(ikind)%list)
    1216              :             END IF
    1217       177630 :             IF (ASSOCIATED(atom(ikind)%list_local_b_index)) THEN
    1218       177568 :                listb(ikind)%list => atom(ikind)%list_local_b_index
    1219       177568 :                nlistb(ikind) = SIZE(listb(ikind)%list)
    1220              :             END IF
    1221              :          CASE (2)
    1222        23886 :             IF (ASSOCIATED(atom(ikind)%list_local_a_index)) THEN
    1223        15901 :                lista(ikind)%list => atom(ikind)%list_local_a_index
    1224        15901 :                nlista(ikind) = SIZE(lista(ikind)%list)
    1225              :             END IF
    1226        23886 :             nlistb(ikind) = SIZE(atom(ikind)%list)
    1227        23886 :             listb(ikind)%list => index_list
    1228              :          CASE (3)
    1229        24224 :             CALL combine_lists(lista(ikind)%list, nlista(ikind), ikind, atom)
    1230        24224 :             nlistb(ikind) = SIZE(atom(ikind)%list)
    1231        24224 :             listb(ikind)%list => index_list
    1232              :          CASE (4)
    1233        70190 :             nlista(ikind) = SIZE(atom(ikind)%list_1d)
    1234        70190 :             lista(ikind)%list => atom(ikind)%list_1d
    1235        70190 :             nlistb(ikind) = SIZE(atom(ikind)%list)
    1236        70190 :             listb(ikind)%list => index_list
    1237              :          CASE default
    1238       295930 :             CPABORT("Only 1, 2, 3, 4 are supported as otype for the operator")
    1239              :          END SELECT
    1240              :       END DO
    1241              : 
    1242              :       ! Determine max. number of local atoms
    1243       151892 :       maxat = 0
    1244       447822 :       DO ikind = 1, nkind
    1245       447822 :          maxat = MAX(maxat, nlista(ikind), nlistb(ikind))
    1246              :       END DO
    1247      1465790 :       ALLOCATE (kind_a(2*maxat))
    1248              : 
    1249              :       ! Load informations about the simulation cell
    1250       151892 :       CALL get_cell(cell=cell, periodic=periodic, deth=deth)
    1251              : 
    1252              :       ! Loop over all atomic kind pairs
    1253       447822 :       DO ikind = 1, nkind
    1254       295930 :          IF (.NOT. pres_a(ikind)) CYCLE
    1255              : 
    1256      1050406 :          DO jkind = 1, nkind
    1257       618036 :             IF (.NOT. pres_b(jkind)) CYCLE
    1258              : 
    1259       598658 :             iab = ikind + nkind*(jkind - 1)
    1260              : 
    1261              :             ! Calculate the square of the maximum interaction distance
    1262       598658 :             IF (pair_radius(ikind, jkind) <= 0._dp) CYCLE
    1263       598614 :             rab_max = pair_radius(ikind, jkind)
    1264       598614 :             IF (otype == 3) THEN
    1265              :                ! Calculate the square of the maximum interaction distance
    1266              :                ! for sac_max / ncell this must be the maximum over all kinds
    1267              :                ! to be correct for three center terms involving different kinds
    1268       101930 :                rabm = MAXVAL(pair_radius(:, jkind))
    1269              :             ELSE
    1270              :                rabm = rab_max
    1271              :             END IF
    1272       598614 :             rab2_max = rabm*rabm
    1273              : 
    1274       598614 :             pd(1) = plane_distance(1, 0, 0, cell)
    1275       598614 :             pd(2) = plane_distance(0, 1, 0, cell)
    1276       598614 :             pd(3) = plane_distance(0, 0, 1, cell)
    1277              : 
    1278      2394456 :             sab_max = rabm/pd
    1279      2394456 :             sab_max_guard = 15.0_dp/pd
    1280              : 
    1281              :             ! It makes sense to have fewer subcells for larger systems
    1282       598614 :             subcell_scale = ((125.0_dp**3)/deth)**(1.0_dp/6.0_dp)
    1283              : 
    1284              :             ! guess the number of subcells for optimal performance,
    1285              :             ! guard against crazy stuff triggered by very small rabm
    1286              :             nsubcell(:) = INT(MAX(1.0_dp, MIN(0.5_dp*subcells*subcell_scale/sab_max(:), &
    1287      2394456 :                                               0.5_dp*subcells*subcell_scale/sab_max_guard(:))))
    1288              : 
    1289              :             ! number of image cells to be considered
    1290      2394456 :             ncell(:) = (INT(sab_max(:)) + 1)*periodic(:)
    1291              : 
    1292              :             CALL allocate_neighbor_list_set(neighbor_list_set=ab_list(iab)%neighbor_list_set, &
    1293       598614 :                                             symmetric=my_symmetric)
    1294       598614 :             neighbor_list_set => ab_list(iab)%neighbor_list_set
    1295              : 
    1296      1436004 :             DO iatom_local = 1, nlista(ikind)
    1297       837390 :                iatom = lista(ikind)%list(iatom_local)
    1298       837390 :                atom_a = atom(ikind)%list(iatom)
    1299              :                CALL add_neighbor_list(neighbor_list_set=neighbor_list_set, &
    1300              :                                       atom=atom_a, &
    1301      1436004 :                                       neighbor_list=kind_a(iatom_local)%neighbor_list)
    1302              :             END DO
    1303              : 
    1304       598614 :             CALL allocate_subcell(subcell, nsubcell)
    1305      1436004 :             DO iatom_local = 1, nlista(ikind)
    1306       837390 :                iatom = lista(ikind)%list(iatom_local)
    1307       837390 :                atom_a = atom(ikind)%list(iatom)
    1308      3349560 :                r = r_pbc(:, atom_a)
    1309       837390 :                CALL give_ijk_subcell(r, i, j, k, cell, nsubcell)
    1310      1436004 :                subcell(i, j, k)%natom = subcell(i, j, k)%natom + 1
    1311              :             END DO
    1312      1786141 :             DO k = 1, nsubcell(3)
    1313      4491938 :                DO j = 1, nsubcell(2)
    1314     11299173 :                   DO i = 1, nsubcell(1)
    1315      7405849 :                      maxat = subcell(i, j, k)%natom + subcell(i, j, k)%natom/10
    1316     15383315 :                      ALLOCATE (subcell(i, j, k)%atom_list(maxat))
    1317     10111646 :                      subcell(i, j, k)%natom = 0
    1318              :                   END DO
    1319              :                END DO
    1320              :             END DO
    1321      1436004 :             DO iatom_local = 1, nlista(ikind)
    1322       837390 :                iatom = lista(ikind)%list(iatom_local)
    1323       837390 :                atom_a = atom(ikind)%list(iatom)
    1324      3349560 :                r = r_pbc(:, atom_a)
    1325       837390 :                CALL give_ijk_subcell(r, i, j, k, cell, nsubcell)
    1326       837390 :                subcell(i, j, k)%natom = subcell(i, j, k)%natom + 1
    1327      1436004 :                subcell(i, j, k)%atom_list(subcell(i, j, k)%natom) = iatom_local
    1328              :             END DO
    1329              : 
    1330      2155444 :             DO jatom_local = 1, nlistb(jkind)
    1331      1556830 :                jatom = listb(jkind)%list(jatom_local)
    1332      1556830 :                atom_b = atom(jkind)%list(jatom)
    1333      1556830 :                IF (my_sort_atomb .AND. .NOT. my_symmetric) THEN
    1334         7046 :                   IF (.NOT. ANY(atomb_to_keep == atom_b)) CYCLE
    1335              :                END IF
    1336      1554124 :                IF (my_molecular) THEN
    1337         4180 :                   mol_b = atom(jkind)%list_b_mol(jatom_local)
    1338         4180 :                   IF (PRESENT(subset_of_mol)) THEN
    1339         1716 :                      IF (subset_of_mol(mol_b) /= current_subset) CYCLE
    1340              :                   END IF
    1341              :                END IF
    1342      6212816 :                r = r_pbc(:, atom_b)
    1343      1553204 :                CALL real_to_scaled(sb_pbc(:), r(:), cell)
    1344              : 
    1345      6145830 :                loop2_kcell: DO kcell = -ncell(3), ncell(3)
    1346      4314448 :                   sb(3) = sb_pbc(3) + REAL(kcell, dp)
    1347      4314448 :                   sb_min(3) = sb(3) - sab_max(3)
    1348      4314448 :                   sb_max(3) = sb(3) + sab_max(3)
    1349      4314448 :                   IF (periodic(3) /= 0) THEN
    1350      3493048 :                      IF (sb_min(3) >= 0.5_dp) EXIT loop2_kcell
    1351      3172612 :                      IF (sb_max(3) < -0.5_dp) CYCLE loop2_kcell
    1352              :                   END IF
    1353      3680026 :                   cell_b(3) = kcell
    1354              : 
    1355     22015231 :                   loop2_jcell: DO jcell = -ncell(2), ncell(2)
    1356     17870398 :                      sb(2) = sb_pbc(2) + REAL(jcell, dp)
    1357     17870398 :                      sb_min(2) = sb(2) - sab_max(2)
    1358     17870398 :                      sb_max(2) = sb(2) + sab_max(2)
    1359     17870398 :                      IF (periodic(2) /= 0) THEN
    1360     17045632 :                         IF (sb_min(2) >= 0.5_dp) EXIT loop2_jcell
    1361     15953609 :                         IF (sb_max(2) < -0.5_dp) CYCLE loop2_jcell
    1362              :                      END IF
    1363     15615012 :                      cell_b(2) = jcell
    1364              : 
    1365    123640117 :                      loop2_icell: DO icell = -ncell(1), ncell(1)
    1366    110335098 :                         sb(1) = sb_pbc(1) + REAL(icell, dp)
    1367    110335098 :                         sb_min(1) = sb(1) - sab_max(1)
    1368    110335098 :                         sb_max(1) = sb(1) + sab_max(1)
    1369    110335098 :                         IF (periodic(1) /= 0) THEN
    1370    109205416 :                            IF (sb_min(1) >= 0.5_dp) EXIT loop2_icell
    1371    102901411 :                            IF (sb_max(1) < -0.5_dp) CYCLE loop2_icell
    1372              :                         END IF
    1373     98805219 :                         cell_b(1) = icell
    1374              : 
    1375     98805219 :                         CALL scaled_to_real(rb, sb, cell)
    1376              : 
    1377    232464383 :                         loop_k: DO k = 1, nsubcell(3)
    1378    374564098 :                            loop_j: DO j = 1, nsubcell(2)
    1379    562941661 :                               loop_i: DO i = 1, nsubcell(1)
    1380              : 
    1381              :                                  ! FIXME for non-periodic systems, the whole subcell trick is skipped
    1382              :                                  ! yielding a Natom**2 pair list build.
    1383    310889423 :                                  IF (periodic(3) /= 0) THEN
    1384    297676807 :                                     IF (sb_max(3) < subcell(i, j, k)%s_min(3)) EXIT loop_k
    1385    294926405 :                                     IF (sb_min(3) >= subcell(i, j, k)%s_max(3)) CYCLE loop_k
    1386              :                                  END IF
    1387              : 
    1388    304681388 :                                  IF (periodic(2) /= 0) THEN
    1389    291681854 :                                     IF (sb_max(2) < subcell(i, j, k)%s_min(2)) EXIT loop_j
    1390    287575847 :                                     IF (sb_min(2) >= subcell(i, j, k)%s_max(2)) CYCLE loop_j
    1391              :                                  END IF
    1392              : 
    1393    294271104 :                                  IF (periodic(1) /= 0) THEN
    1394    280183978 :                                     IF (sb_max(1) < subcell(i, j, k)%s_min(1)) EXIT loop_i
    1395    270757890 :                                     IF (sb_min(1) >= subcell(i, j, k)%s_max(1)) CYCLE loop_i
    1396              :                                  END IF
    1397              : 
    1398    259441481 :                                  IF (subcell(i, j, k)%natom == 0) CYCLE loop_i
    1399              : 
    1400    596086406 :                                  DO iatom_subcell = 1, subcell(i, j, k)%natom
    1401    351361454 :                                     iatom_local = subcell(i, j, k)%atom_list(iatom_subcell)
    1402    351361454 :                                     iatom = lista(ikind)%list(iatom_local)
    1403    351361454 :                                     atom_a = atom(ikind)%list(iatom)
    1404    351361454 :                                     IF (my_molecular) THEN
    1405      1049740 :                                        mol_a = atom(ikind)%list_a_mol(iatom_local)
    1406      1049740 :                                        IF (mol_a /= mol_b) CYCLE
    1407              :                                     END IF
    1408    350839577 :                                     IF (my_symmetric) THEN
    1409    335184347 :                                        IF (atom_a > atom_b) THEN
    1410    155545909 :                                           include_ab = (MODULO(atom_a + atom_b, 2) /= 0)
    1411              :                                        ELSE
    1412    179638438 :                                           include_ab = (MODULO(atom_a + atom_b, 2) == 0)
    1413              :                                        END IF
    1414    335184347 :                                        IF (my_sort_atomb) THEN
    1415       666204 :                                           IF ((.NOT. ANY(atomb_to_keep == atom_b)) .AND. &
    1416              :                                               (.NOT. ANY(atomb_to_keep == atom_a))) THEN
    1417              :                                              include_ab = .FALSE.
    1418              :                                           END IF
    1419              :                                        END IF
    1420              :                                     ELSE
    1421              :                                        include_ab = .TRUE.
    1422              :                                     END IF
    1423    619920165 :                                     IF (include_ab) THEN
    1424    779740660 :                                        ra(:) = r_pbc(:, atom_a)
    1425    779740660 :                                        rab(:) = rb(:) - ra(:)
    1426    194935165 :                                        rab2 = rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3)
    1427    194935165 :                                        IF (rab2 < rab2_max) THEN
    1428     59541149 :                                           include_ab = .TRUE.
    1429     59541149 :                                           IF (my_mic) THEN
    1430              :                                              ! only if rab is minimum image the pair will be included
    1431              :                                              ! ideally the range of the pair list is < L/2 so
    1432              :                                              ! that this never triggers
    1433      1429021 :                                              rab_pbc(:) = pbc(rab(:), cell)
    1434      5716084 :                                              IF (SUM((rab_pbc - rab)**2) > EPSILON(1.0_dp)) THEN
    1435              :                                                 include_ab = .FALSE.
    1436              :                                              END IF
    1437              :                                           END IF
    1438              :                                           IF (include_ab) THEN
    1439              :                                              CALL add_neighbor_node( &
    1440              :                                                 neighbor_list=kind_a(iatom_local)%neighbor_list, &
    1441              :                                                 neighbor=atom_b, &
    1442              :                                                 cell=cell_b, &
    1443              :                                                 r=rab, &
    1444     58425684 :                                                 nkind=nkind)
    1445              :                                           END IF
    1446              :                                        END IF
    1447              :                                     END IF
    1448              :                                  END DO
    1449              : 
    1450              :                               END DO loop_i
    1451              :                            END DO loop_j
    1452              :                         END DO loop_k
    1453              : 
    1454              :                      END DO loop2_icell
    1455              :                   END DO loop2_jcell
    1456              :                END DO loop2_kcell
    1457              : 
    1458              :             END DO
    1459              : 
    1460       913966 :             CALL deallocate_subcell(subcell)
    1461              : 
    1462              :          END DO
    1463              :       END DO
    1464              : 
    1465        12933 :       SELECT CASE (otype)
    1466              :       CASE (1:2, 4)
    1467              :       CASE (3)
    1468        37157 :          DO ikind = 1, nkind
    1469        37157 :             DEALLOCATE (lista(ikind)%list)
    1470              :          END DO
    1471              :       CASE default
    1472       151892 :          CPABORT("Only 1, 2, 3, 4 are supported as otype for the operator")
    1473              :       END SELECT
    1474       151892 :       DEALLOCATE (kind_a, pres_a, pres_b, lista, listb, nlista, nlistb)
    1475       151892 :       DEALLOCATE (index_list)
    1476       151892 :       DEALLOCATE (r_pbc)
    1477              : 
    1478       151892 :       nentry = 0
    1479       151892 :       CALL neighbor_list_iterator_create(nl_iterator, ab_list)
    1480     58577576 :       DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
    1481     58425684 :          CALL get_iterator_info(nl_iterator, inode=inode, nnode=nnode)
    1482     58577576 :          IF (inode == 1) nentry = nentry + nnode
    1483              :       END DO
    1484       151892 :       CALL neighbor_list_iterator_release(nl_iterator)
    1485              :       !
    1486     59784662 :       ALLOCATE (ab_list(1)%nlist_task(nentry))
    1487       151892 :       ab_list(1)%nl_size = nentry
    1488       658166 :       DO iab = 2, SIZE(ab_list)
    1489       506274 :          ab_list(iab)%nl_size = nentry
    1490       658166 :          ab_list(iab)%nlist_task => ab_list(1)%nlist_task
    1491              :       END DO
    1492              :       !
    1493       151892 :       nentry = 0
    1494       151892 :       CALL neighbor_list_iterator_create(nl_iterator, ab_list)
    1495     58577576 :       DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
    1496     58425684 :          nentry = nentry + 1
    1497     58425684 :          CALL get_iterator_task(nl_iterator, ab_list(1)%nlist_task(nentry))
    1498     58425684 :          CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, nkind=nkind)
    1499     58425684 :          iab = (ikind - 1)*nkind + jkind
    1500     58425684 :          IF (ab_list(iab)%nl_start < 0) ab_list(iab)%nl_start = nentry
    1501     58577576 :          IF (ab_list(iab)%nl_end < 0) THEN
    1502       392619 :             ab_list(iab)%nl_end = nentry
    1503              :          ELSE
    1504     58033065 :             CPASSERT(ab_list(iab)%nl_end + 1 == nentry)
    1505     58033065 :             ab_list(iab)%nl_end = nentry
    1506              :          END IF
    1507              :       END DO
    1508       151892 :       CALL neighbor_list_iterator_release(nl_iterator)
    1509              : 
    1510       151892 :       CALL timestop(handle)
    1511              : 
    1512       303784 :    END SUBROUTINE build_neighbor_lists
    1513              : 
    1514              : ! **************************************************************************************************
    1515              : !> \brief Build a neighborlist
    1516              : !> \param ab_list ...
    1517              : !> \param basis_set_a ...
    1518              : !> \param basis_set_b ...
    1519              : !> \param qs_env ...
    1520              : !> \param mic ...
    1521              : !> \param symmetric ...
    1522              : !> \param molecular ...
    1523              : !> \param operator_type ...
    1524              : !> \date    14.03.2016
    1525              : !> \author  JGH
    1526              : ! **************************************************************************************************
    1527          116 :    SUBROUTINE setup_neighbor_list(ab_list, basis_set_a, basis_set_b, qs_env, &
    1528              :                                   mic, symmetric, molecular, operator_type)
    1529              : 
    1530              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1531              :          POINTER                                         :: ab_list
    1532              :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_a
    1533              :       TYPE(gto_basis_set_p_type), DIMENSION(:), &
    1534              :          OPTIONAL, POINTER                               :: basis_set_b
    1535              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1536              :       LOGICAL, INTENT(IN), OPTIONAL                      :: mic, symmetric, molecular
    1537              :       CHARACTER(LEN=*), INTENT(IN), OPTIONAL             :: operator_type
    1538              : 
    1539              :       CHARACTER(LEN=4)                                   :: otype
    1540              :       INTEGER                                            :: ikind, nkind
    1541              :       LOGICAL                                            :: my_mic, my_molecular, my_symmetric
    1542              :       LOGICAL, ALLOCATABLE, DIMENSION(:)                 :: a_present, b_present
    1543              :       REAL(dp), ALLOCATABLE, DIMENSION(:)                :: a_radius, b_radius
    1544          116 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: pair_radius
    1545          116 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1546              :       TYPE(cell_type), POINTER                           :: cell
    1547              :       TYPE(distribution_1d_type), POINTER                :: distribution_1d
    1548              :       TYPE(distribution_2d_type), POINTER                :: distribution_2d
    1549          116 :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_a, basis_b
    1550              :       TYPE(gto_basis_set_type), POINTER                  :: abas, bbas
    1551          116 :       TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:)  :: atom2d
    1552          116 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
    1553          116 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1554              : 
    1555          116 :       basis_a => basis_set_a
    1556          116 :       IF (PRESENT(basis_set_b)) THEN
    1557           56 :          basis_b => basis_set_b
    1558           56 :          my_symmetric = .FALSE.
    1559              :       ELSE
    1560           60 :          basis_b => basis_set_a
    1561           60 :          my_symmetric = .TRUE.
    1562              :       END IF
    1563          116 :       IF (PRESENT(symmetric)) my_symmetric = symmetric
    1564              : 
    1565          116 :       IF (PRESENT(mic)) THEN
    1566            6 :          my_mic = mic
    1567              :       ELSE
    1568          110 :          my_mic = .FALSE.
    1569              :       END IF
    1570              : 
    1571          116 :       IF (PRESENT(molecular)) THEN
    1572            8 :          my_molecular = molecular
    1573              :       ELSE
    1574          108 :          my_molecular = .FALSE.
    1575              :       END IF
    1576              : 
    1577              :       IF (PRESENT(operator_type)) THEN
    1578              :          otype = operator_type
    1579              :       ELSE
    1580              :          ! default is a simple AB neighbor list
    1581              :          otype = "AB"
    1582              :       END IF
    1583              : 
    1584          116 :       nkind = SIZE(basis_a)
    1585          464 :       ALLOCATE (a_present(nkind), b_present(nkind))
    1586          116 :       a_present = .FALSE.
    1587          116 :       b_present = .FALSE.
    1588          464 :       ALLOCATE (a_radius(nkind), b_radius(nkind))
    1589          116 :       a_radius = 0.0_dp
    1590          116 :       b_radius = 0.0_dp
    1591          358 :       DO ikind = 1, nkind
    1592          242 :          IF (ASSOCIATED(basis_a(ikind)%gto_basis_set)) THEN
    1593          242 :             a_present(ikind) = .TRUE.
    1594          242 :             abas => basis_a(ikind)%gto_basis_set
    1595          242 :             CALL get_gto_basis_set(gto_basis_set=abas, kind_radius=a_radius(ikind))
    1596              :          END IF
    1597          358 :          IF (ASSOCIATED(basis_b(ikind)%gto_basis_set)) THEN
    1598          242 :             b_present(ikind) = .TRUE.
    1599          242 :             bbas => basis_b(ikind)%gto_basis_set
    1600          242 :             CALL get_gto_basis_set(gto_basis_set=bbas, kind_radius=b_radius(ikind))
    1601              :          END IF
    1602              :       END DO
    1603              : 
    1604          464 :       ALLOCATE (pair_radius(nkind, nkind))
    1605          116 :       pair_radius = 0.0_dp
    1606          116 :       CALL pair_radius_setup(a_present, b_present, a_radius, b_radius, pair_radius)
    1607              : 
    1608              :       CALL get_qs_env(qs_env, &
    1609              :                       atomic_kind_set=atomic_kind_set, &
    1610              :                       cell=cell, &
    1611              :                       distribution_2d=distribution_2d, &
    1612              :                       local_particles=distribution_1d, &
    1613              :                       particle_set=particle_set, &
    1614          116 :                       molecule_set=molecule_set)
    1615              : 
    1616          590 :       ALLOCATE (atom2d(nkind))
    1617              :       CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
    1618          116 :                         molecule_set, my_molecular, particle_set=particle_set)
    1619              :       CALL build_neighbor_lists(ab_list, particle_set, atom2d, cell, pair_radius, &
    1620              :                                 mic=my_mic, symmetric=my_symmetric, molecular=my_molecular, &
    1621          116 :                                 subcells=2.0_dp, nlname="AUX_NL")
    1622              : 
    1623          116 :       CALL atom2d_cleanup(atom2d)
    1624              : 
    1625          116 :       DEALLOCATE (a_present, b_present, a_radius, b_radius, pair_radius, atom2d)
    1626              : 
    1627          116 :    END SUBROUTINE setup_neighbor_list
    1628              : 
    1629              : ! **************************************************************************************************
    1630              : !> \brief ...
    1631              : !> \param list ...
    1632              : !> \param n ...
    1633              : !> \param ikind ...
    1634              : !> \param atom ...
    1635              : ! **************************************************************************************************
    1636        24224 :    SUBROUTINE combine_lists(list, n, ikind, atom)
    1637              :       INTEGER, DIMENSION(:), POINTER                     :: list
    1638              :       INTEGER, INTENT(OUT)                               :: n
    1639              :       INTEGER, INTENT(IN)                                :: ikind
    1640              :       TYPE(local_atoms_type), DIMENSION(:), INTENT(IN)   :: atom
    1641              : 
    1642              :       INTEGER                                            :: i, ib, na, nb
    1643        24224 :       INTEGER, DIMENSION(:), POINTER                     :: lista, listb
    1644              : 
    1645            0 :       CPASSERT(.NOT. ASSOCIATED(list))
    1646              : 
    1647        24224 :       lista => atom(ikind)%list_local_a_index
    1648        24224 :       listb => atom(ikind)%list_local_b_index
    1649              : 
    1650        24224 :       IF (ASSOCIATED(lista)) THEN
    1651        15366 :          na = SIZE(lista)
    1652              :       ELSE
    1653              :          na = 0
    1654              :       END IF
    1655              : 
    1656        24224 :       IF (ASSOCIATED(listb)) THEN
    1657        24224 :          nb = SIZE(listb)
    1658              :       ELSE
    1659              :          nb = 0
    1660              :       END IF
    1661              : 
    1662        72672 :       ALLOCATE (list(na + nb))
    1663              : 
    1664        24224 :       n = na
    1665        87916 :       IF (na > 0) list(1:na) = lista(1:na)
    1666        24224 :       IF (nb > 0) THEN
    1667        71941 :          loopb: DO ib = 1, nb
    1668        98160 :             DO i = 1, na
    1669        98160 :                IF (listb(ib) == list(i)) CYCLE loopb
    1670              :             END DO
    1671        23554 :             n = n + 1
    1672        71941 :             list(n) = listb(ib)
    1673              :          END DO loopb
    1674              :       END IF
    1675        24224 :    END SUBROUTINE combine_lists
    1676              : 
    1677              : ! **************************************************************************************************
    1678              : 
    1679              : ! **************************************************************************************************
    1680              : !> \brief ...
    1681              : !> \param present_a ...
    1682              : !> \param present_b ...
    1683              : !> \param radius_a ...
    1684              : !> \param radius_b ...
    1685              : !> \param pair_radius ...
    1686              : !> \param prmin ...
    1687              : ! **************************************************************************************************
    1688       131837 :    SUBROUTINE pair_radius_setup(present_a, present_b, radius_a, radius_b, pair_radius, prmin)
    1689              :       LOGICAL, DIMENSION(:), INTENT(IN)                  :: present_a, present_b
    1690              :       REAL(dp), DIMENSION(:), INTENT(IN)                 :: radius_a, radius_b
    1691              :       REAL(dp), DIMENSION(:, :), INTENT(OUT)             :: pair_radius
    1692              :       REAL(dp), INTENT(IN), OPTIONAL                     :: prmin
    1693              : 
    1694              :       INTEGER                                            :: i, j, nkind
    1695              :       REAL(dp)                                           :: rrmin
    1696              : 
    1697       131837 :       nkind = SIZE(present_a)
    1698              : 
    1699       958691 :       pair_radius = 0._dp
    1700              : 
    1701       131837 :       rrmin = 0.0_dp
    1702       131837 :       IF (PRESENT(prmin)) rrmin = prmin
    1703              : 
    1704       388425 :       DO i = 1, nkind
    1705       256588 :          IF (.NOT. present_a(i)) CYCLE
    1706       903557 :          DO j = 1, nkind
    1707       530448 :             IF (.NOT. present_b(j)) CYCLE
    1708       510764 :             pair_radius(i, j) = radius_a(i) + radius_b(j)
    1709       787036 :             pair_radius(i, j) = MAX(pair_radius(i, j), rrmin)
    1710              :          END DO
    1711              :       END DO
    1712              : 
    1713       131837 :    END SUBROUTINE pair_radius_setup
    1714              : 
    1715              : ! **************************************************************************************************
    1716              : !> \brief   Print the distribution of the simple pair neighbor list.
    1717              : !> \param ab ...
    1718              : !> \param qs_kind_set ...
    1719              : !> \param output_unit ...
    1720              : !> \param para_env ...
    1721              : !> \date    19.06.2003
    1722              : !> \author  MK
    1723              : !> \version 1.0
    1724              : ! **************************************************************************************************
    1725          166 :    SUBROUTINE write_neighbor_distribution(ab, qs_kind_set, output_unit, para_env)
    1726              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1727              :          POINTER                                         :: ab
    1728              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1729              :       INTEGER, INTENT(in)                                :: output_unit
    1730              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1731              : 
    1732              :       CHARACTER(len=*), PARAMETER :: routineN = 'write_neighbor_distribution'
    1733              :       LOGICAL, PARAMETER                                 :: full_output = .FALSE.
    1734              : 
    1735              :       INTEGER                                            :: handle, ikind, inode, ipe, jkind, n, &
    1736              :                                                             nkind, nnode
    1737              :       INTEGER(int_8)                                     :: nblock_max, nblock_sum, nelement_max, &
    1738              :                                                             nelement_sum, tmp(2)
    1739          166 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: nblock, nelement, nnsgf
    1740              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
    1741              :       TYPE(neighbor_list_iterator_p_type), &
    1742          166 :          DIMENSION(:), POINTER                           :: nl_iterator
    1743              : 
    1744          166 :       CALL timeset(routineN, handle)
    1745              :       ASSOCIATE (mype => para_env%mepos + 1, npe => para_env%num_pe)
    1746              : 
    1747              :          ! Allocate work storage
    1748          664 :          ALLOCATE (nblock(npe), nelement(npe))
    1749          166 :          nblock(:) = 0
    1750          166 :          nelement(:) = 0
    1751          166 :          nkind = SIZE(qs_kind_set)
    1752          498 :          ALLOCATE (nnsgf(nkind))
    1753          466 :          nnsgf = 1
    1754          466 :          DO ikind = 1, nkind
    1755          300 :             CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set)
    1756          466 :             IF (ASSOCIATED(orb_basis_set)) THEN
    1757          242 :                CALL get_gto_basis_set(gto_basis_set=orb_basis_set, nsgf=nnsgf(ikind))
    1758              :             END IF
    1759              :          END DO
    1760              : 
    1761          166 :          CALL neighbor_list_iterator_create(nl_iterator, ab)
    1762        45554 :          DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
    1763        45388 :             CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, inode=inode, nnode=nnode)
    1764        45554 :             IF (inode == 1) THEN
    1765         1181 :                n = nnsgf(ikind)*nnsgf(jkind)
    1766         1181 :                nblock(mype) = nblock(mype) + nnode
    1767         1181 :                nelement(mype) = nelement(mype) + n*nnode
    1768              :             END IF
    1769              :          END DO
    1770          166 :          CALL neighbor_list_iterator_release(nl_iterator)
    1771              : 
    1772              :          IF (full_output) THEN
    1773              :             ! XXXXXXXX should gather/scatter this on ionode
    1774              :             CALL para_env%sum(nblock)
    1775              :             CALL para_env%sum(nelement)
    1776              : 
    1777              :             nblock_sum = SUM(INT(nblock, KIND=int_8))
    1778              :             nelement_sum = SUM(INT(nelement, KIND=int_8))
    1779              :          ELSE
    1780          166 :             nblock_sum = nblock(mype)
    1781              :             nblock_max = nblock(mype)
    1782          166 :             nelement_sum = nelement(mype)
    1783              :             nelement_max = nelement(mype)
    1784          498 :             tmp = [nblock_sum, nelement_sum]
    1785          166 :             CALL para_env%sum(tmp)
    1786          166 :             nblock_sum = tmp(1); nelement_sum = tmp(2)
    1787          498 :             tmp = [nblock_max, nelement_max]
    1788          166 :             CALL para_env%max(tmp)
    1789          166 :             nblock_max = tmp(1); nelement_max = tmp(2)
    1790              :          END IF
    1791              : 
    1792          332 :          IF (output_unit > 0) THEN
    1793              :             IF (full_output) THEN
    1794              :                WRITE (UNIT=output_unit, &
    1795              :                       FMT="(/,/,T2,A,/,/,T3,A,/,/,(T4,I6,T27,I10,T55,I10))") &
    1796              :                   "DISTRIBUTION OF THE NEIGHBOR LISTS", &
    1797              :                   "Process   Number of particle pairs   Number of matrix elements", &
    1798              :                   (ipe - 1, nblock(ipe), nelement(ipe), ipe=1, npe)
    1799              :                WRITE (UNIT=output_unit, FMT="(/,T7,A3,T27,I10,T55,I10)") &
    1800              :                   "Sum", SUM(nblock), SUM(nelement)
    1801              :             ELSE
    1802           83 :                WRITE (UNIT=output_unit, FMT="(/,T2,A)") "DISTRIBUTION OF THE NEIGHBOR LISTS"
    1803           83 :                WRITE (UNIT=output_unit, FMT="(T15,A,T68,I13)") "Total number of particle pairs:", nblock_sum
    1804           83 :                WRITE (UNIT=output_unit, FMT="(T15,A,T68,I13)") "Total number of matrix elements:", nelement_sum
    1805           83 :                WRITE (UNIT=output_unit, FMT="(T15,A,T68,I13)") "Average number of particle pairs:", (nblock_sum + npe - 1)/npe
    1806           83 :                WRITE (UNIT=output_unit, FMT="(T15,A,T68,I13)") "Maximum number of particle pairs:", nblock_max
    1807           83 :                WRITE (UNIT=output_unit, FMT="(T15,A,T68,I13)") "Average number of matrix element:", (nelement_sum + npe - 1)/npe
    1808           83 :                WRITE (UNIT=output_unit, FMT="(T15,A,T68,I13)") "Maximum number of matrix elements:", nelement_max
    1809              :             END IF
    1810              :          END IF
    1811              :       END ASSOCIATE
    1812              : 
    1813              :       ! Release work storage
    1814              : 
    1815          166 :       DEALLOCATE (nblock, nelement, nnsgf)
    1816              : 
    1817          166 :       CALL timestop(handle)
    1818              : 
    1819          166 :    END SUBROUTINE write_neighbor_distribution
    1820              : 
    1821              : ! **************************************************************************************************
    1822              : !> \brief   Write a set of neighbor lists to the output unit.
    1823              : !> \param ab ...
    1824              : !> \param particle_set ...
    1825              : !> \param cell ...
    1826              : !> \param para_env ...
    1827              : !> \param neighbor_list_section ...
    1828              : !> \param nl_type ...
    1829              : !> \param middle_name ...
    1830              : !> \param nlname ...
    1831              : !> \date    04.03.2002
    1832              : !> \par History
    1833              : !>       - Adapted to the new parallelized neighbor list version
    1834              : !>         (26.06.2003,MK)
    1835              : !> \author  MK
    1836              : !> \version 1.0
    1837              : ! **************************************************************************************************
    1838        83784 :    SUBROUTINE write_neighbor_lists(ab, particle_set, cell, para_env, neighbor_list_section, &
    1839              :                                    nl_type, middle_name, nlname)
    1840              : 
    1841              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1842              :          POINTER                                         :: ab
    1843              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1844              :       TYPE(cell_type), POINTER                           :: cell
    1845              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1846              :       TYPE(section_vals_type), POINTER                   :: neighbor_list_section
    1847              :       CHARACTER(LEN=*), INTENT(IN)                       :: nl_type, middle_name, nlname
    1848              : 
    1849              :       CHARACTER(LEN=default_string_length)               :: string, unit_str
    1850              :       INTEGER                                            :: iatom, inode, iw, jatom, nneighbor, nnode
    1851              :       INTEGER, DIMENSION(3)                              :: cell_b
    1852              :       REAL(dp)                                           :: dab, unit_conv
    1853              :       REAL(dp), DIMENSION(3)                             :: ra, rab, rb
    1854              :       TYPE(cp_logger_type), POINTER                      :: logger
    1855              :       TYPE(neighbor_list_iterator_p_type), &
    1856        83784 :          DIMENSION(:), POINTER                           :: nl_iterator
    1857              : 
    1858        83784 :       NULLIFY (logger)
    1859        83784 :       logger => cp_get_default_logger()
    1860        83784 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, neighbor_list_section, &
    1861              :                                            TRIM(nl_type)), &
    1862              :                 cp_p_file)) THEN
    1863              :          iw = cp_print_key_unit_nr(logger=logger, &
    1864              :                                    basis_section=neighbor_list_section, &
    1865              :                                    print_key_path=TRIM(nl_type), &
    1866              :                                    extension=".out", &
    1867              :                                    middle_name=TRIM(middle_name), &
    1868              :                                    local=.TRUE., &
    1869              :                                    log_filename=.FALSE., &
    1870            4 :                                    file_position="REWIND")
    1871              :          ASSOCIATE (mype => para_env%mepos)
    1872            4 :             CALL section_vals_val_get(neighbor_list_section, "UNIT", c_val=unit_str)
    1873            4 :             unit_conv = cp_unit_from_cp2k(1.0_dp, TRIM(unit_str))
    1874              : 
    1875              :             ! Print headline
    1876            4 :             string = ""
    1877              :             WRITE (UNIT=string, FMT="(A,I5,A)") &
    1878            4 :                TRIM(nlname)//" IN "//TRIM(unit_str)//" (PROCESS", mype, ")"
    1879            4 :             CALL compress(string)
    1880            4 :             IF (iw > 0) WRITE (UNIT=iw, FMT="(/,/,T2,A)") TRIM(string)
    1881              : 
    1882            4 :             nneighbor = 0
    1883              : 
    1884            4 :             CALL neighbor_list_iterator_create(nl_iterator, ab)
    1885           16 :             DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
    1886              :                CALL get_iterator_info(nl_iterator, inode=inode, nnode=nnode, &
    1887           12 :                                       iatom=iatom, jatom=jatom, cell=cell_b, r=rab)
    1888           12 :                nneighbor = nneighbor + 1
    1889           12 :                ra(:) = pbc(particle_set(iatom)%r, cell)
    1890           48 :                rb(:) = ra(:) + rab(:)
    1891           12 :                dab = SQRT(rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3))
    1892           16 :                IF (iw > 0) THEN
    1893           12 :                   IF (inode == 1) THEN
    1894              :                      WRITE (UNIT=iw, FMT="(/,T2,I5,3X,I6,3X,3F12.6)") &
    1895           40 :                         iatom, nnode, ra(1:3)*unit_conv
    1896              :                   END IF
    1897              :                   WRITE (UNIT=iw, FMT="(T10,I6,3X,3I4,3F12.6,2X,F12.6)") &
    1898           60 :                      jatom, cell_b(1:3), rb(1:3)*unit_conv, dab*unit_conv
    1899              :                END IF
    1900              :             END DO
    1901            4 :             CALL neighbor_list_iterator_release(nl_iterator)
    1902              : 
    1903            4 :             string = ""
    1904              :             WRITE (UNIT=string, FMT="(A,I12,A,I12)") &
    1905            4 :                "Total number of neighbor interactions for process", mype, ":", &
    1906            8 :                nneighbor
    1907            4 :             CALL compress(string)
    1908            4 :             IF (iw > 0) WRITE (UNIT=iw, FMT="(/,T2,A)") TRIM(string)
    1909              :             CALL cp_print_key_finished_output(unit_nr=iw, &
    1910              :                                               logger=logger, &
    1911              :                                               basis_section=neighbor_list_section, &
    1912              :                                               print_key_path=TRIM(nl_type), &
    1913            8 :                                               local=.TRUE.)
    1914              :          END ASSOCIATE
    1915              :       END IF
    1916              : 
    1917        83784 :    END SUBROUTINE write_neighbor_lists
    1918              : 
    1919            0 : END MODULE qs_neighbor_lists
        

Generated by: LCOV version 2.0-1