LCOV - code coverage report
Current view: top level - src - almo_scf_qs.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 82.0 % 540 443
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 11 11

            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 Interface between ALMO SCF and QS
      10              : !> \par History
      11              : !>       2011.05 created [Rustam Z Khaliullin]
      12              : !> \author Rustam Z Khaliullin
      13              : ! **************************************************************************************************
      14              : MODULE almo_scf_qs
      15              :    USE almo_scf_types,                  ONLY: almo_mat_dim_aobasis,&
      16              :                                               almo_mat_dim_occ,&
      17              :                                               almo_mat_dim_virt,&
      18              :                                               almo_mat_dim_virt_disc,&
      19              :                                               almo_mat_dim_virt_full,&
      20              :                                               almo_scf_env_type
      21              :    USE atomic_kind_types,               ONLY: get_atomic_kind
      22              :    USE cell_types,                      ONLY: cell_type,&
      23              :                                               pbc
      24              :    USE cp_control_types,                ONLY: dft_control_type
      25              :    USE cp_dbcsr_api,                    ONLY: &
      26              :         dbcsr_complete_redistribute, dbcsr_copy, dbcsr_create, dbcsr_desymmetrize, &
      27              :         dbcsr_distribution_get, dbcsr_distribution_new, dbcsr_distribution_release, &
      28              :         dbcsr_distribution_type, dbcsr_filter, dbcsr_finalize, dbcsr_get_block_p, dbcsr_get_info, &
      29              :         dbcsr_get_num_blocks, dbcsr_get_stored_coordinates, dbcsr_multiply, dbcsr_p_type, &
      30              :         dbcsr_put_block, dbcsr_release, dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry, &
      31              :         dbcsr_work_create
      32              :    USE cp_dbcsr_cp2k_link,              ONLY: cp_dbcsr_alloc_block_from_nbl
      33              :    USE cp_dbcsr_operations,             ONLY: dbcsr_allocate_matrix_set
      34              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      35              :                                               cp_fm_struct_release,&
      36              :                                               cp_fm_struct_type
      37              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      38              :                                               cp_fm_release,&
      39              :                                               cp_fm_type
      40              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      41              :                                               cp_logger_get_default_unit_nr,&
      42              :                                               cp_logger_type
      43              :    USE cp_units,                        ONLY: cp_unit_to_cp2k
      44              :    USE input_constants,                 ONLY: almo_constraint_ao_overlap,&
      45              :                                               almo_constraint_block_diagonal,&
      46              :                                               almo_constraint_distance,&
      47              :                                               almo_domain_layout_molecular,&
      48              :                                               almo_mat_distr_atomic,&
      49              :                                               almo_mat_distr_molecular,&
      50              :                                               do_bondparm_covalent,&
      51              :                                               do_bondparm_vdw
      52              :    USE kinds,                           ONLY: dp
      53              :    USE message_passing,                 ONLY: mp_comm_type
      54              :    USE molecule_types,                  ONLY: get_molecule_set_info,&
      55              :                                               molecule_type
      56              :    USE particle_types,                  ONLY: particle_type
      57              :    USE qs_energy_types,                 ONLY: qs_energy_type
      58              :    USE qs_environment_types,            ONLY: get_qs_env,&
      59              :                                               qs_environment_type,&
      60              :                                               set_qs_env
      61              :    USE qs_ks_methods,                   ONLY: qs_ks_update_qs_env
      62              :    USE qs_ks_types,                     ONLY: qs_ks_did_change,&
      63              :                                               qs_ks_env_type,&
      64              :                                               set_ks_env
      65              :    USE qs_mo_types,                     ONLY: allocate_mo_set,&
      66              :                                               deallocate_mo_set,&
      67              :                                               init_mo_set,&
      68              :                                               mo_set_type
      69              :    USE qs_neighbor_list_types,          ONLY: get_iterator_info,&
      70              :                                               neighbor_list_iterate,&
      71              :                                               neighbor_list_iterator_create,&
      72              :                                               neighbor_list_iterator_p_type,&
      73              :                                               neighbor_list_iterator_release,&
      74              :                                               neighbor_list_set_p_type
      75              :    USE qs_rho_methods,                  ONLY: qs_rho_update_rho
      76              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
      77              :                                               qs_rho_type
      78              :    USE qs_scf_types,                    ONLY: qs_scf_env_type,&
      79              :                                               scf_env_create
      80              : #include "./base/base_uses.f90"
      81              : 
      82              :    IMPLICIT NONE
      83              : 
      84              :    PRIVATE
      85              : 
      86              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'almo_scf_qs'
      87              : 
      88              :    PUBLIC :: matrix_almo_create, &
      89              :              almo_scf_construct_quencher, &
      90              :              calculate_w_matrix_almo, &
      91              :              init_almo_ks_matrix_via_qs, &
      92              :              almo_scf_update_ks_energy, &
      93              :              construct_qs_mos, &
      94              :              matrix_qs_to_almo, &
      95              :              almo_dm_to_almo_ks, &
      96              :              almo_dm_to_qs_env
      97              : 
      98              : CONTAINS
      99              : 
     100              : ! **************************************************************************************************
     101              : !> \brief create the ALMO matrix templates
     102              : !> \param matrix_new ...
     103              : !> \param matrix_qs ...
     104              : !> \param almo_scf_env ...
     105              : !> \param name_new ...
     106              : !> \param size_keys ...
     107              : !> \param symmetry_new ...
     108              : !> \param spin_key ...
     109              : !> \param init_domains ...
     110              : !> \par History
     111              : !>       2011.05 created [Rustam Z Khaliullin]
     112              : !> \author Rustam Z Khaliullin
     113              : ! **************************************************************************************************
     114         3596 :    SUBROUTINE matrix_almo_create(matrix_new, matrix_qs, almo_scf_env, &
     115              :                                  name_new, size_keys, symmetry_new, &
     116              :                                  spin_key, init_domains)
     117              : 
     118              :       TYPE(dbcsr_type)                                   :: matrix_new, matrix_qs
     119              :       TYPE(almo_scf_env_type), INTENT(IN)                :: almo_scf_env
     120              :       CHARACTER(len=*), INTENT(IN)                       :: name_new
     121              :       INTEGER, DIMENSION(2), INTENT(IN)                  :: size_keys
     122              :       CHARACTER, INTENT(IN)                              :: symmetry_new
     123              :       INTEGER, INTENT(IN)                                :: spin_key
     124              :       LOGICAL, INTENT(IN)                                :: init_domains
     125              : 
     126              :       CHARACTER(len=*), PARAMETER :: routineN = 'matrix_almo_create'
     127              : 
     128              :       INTEGER                                            :: dimen, handle, hold, iatom, iblock_col, &
     129              :                                                             iblock_row, imol, mynode, natoms, &
     130              :                                                             nblkrows_tot, nlength, nmols, row
     131         3596 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: blk_distr, blk_sizes, block_sizes_new, &
     132         3596 :          col_blk_size, col_distr_new, col_sizes_new, distr_new_array, row_blk_size, row_distr_new, &
     133         3596 :          row_sizes_new
     134              :       LOGICAL                                            :: active, one_dim_is_mo, tr
     135         3596 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: new_block
     136              :       TYPE(dbcsr_distribution_type)                      :: dist_new, dist_qs
     137              : 
     138              : ! dimension size: AO, MO, etc
     139              : !                 almo_mat_dim_aobasis - no. of AOs,
     140              : !                 almo_mat_dim_occ     - no. of occupied MOs
     141              : !                 almo_mat_dim_domains - no. of domains
     142              : ! symmetry type: dbcsr_type_no_symmetry, dbcsr_type_symmetric,
     143              : !  dbcsr_type_antisymmetric, dbcsr_type_hermitian, dbcsr_type_antihermitian
     144              : !  (see dbcsr_lib/dbcsr_types.F for other values)
     145              : ! spin_key: either 1 or 2 (0 is allowed for matrics in the AO basis)
     146              : !    TYPE(dbcsr_iterator_type)                  :: iter
     147              : !    REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: allones
     148              : !-----------------------------------------------------------------------
     149              : 
     150         3596 :       CALL timeset(routineN, handle)
     151              : 
     152              :       ! RZK-warning The structure of the matrices can be optimized:
     153              :       ! 1. Diagonal matrices must be distributed evenly over the processes.
     154              :       !    This can be achieved by distributing cpus: 012012-rows and 001122-cols
     155              :       !    block_diagonal_flag is introduced but not used
     156              :       ! 2. Multiplication of diagonally dominant matrices will be faster
     157              :       !    if the diagonal blocks are local to the same processes.
     158              :       ! 3. Systems of molecules of drastically different sizes might need
     159              :       !    better distribution.
     160              : 
     161              :       ! obtain distribution from the qs matrix - it might be useful
     162              :       ! to get the structure of the AO dimensions
     163         3596 :       CALL dbcsr_get_info(matrix_qs, distribution=dist_qs)
     164              : 
     165         3596 :       natoms = almo_scf_env%natoms
     166         3596 :       nmols = almo_scf_env%nmolecules
     167              : 
     168        10788 :       DO dimen = 1, 2 ! 1 - row, 2 - column dimension
     169              : 
     170              :          ! distribution pattern is the same for all matrix types (ao, occ, virt)
     171         7192 :          IF (dimen == 1) THEN !rows
     172         3596 :             CALL dbcsr_distribution_get(dist_qs, row_dist=blk_distr)
     173              :          ELSE !columns
     174         3596 :             CALL dbcsr_distribution_get(dist_qs, col_dist=blk_distr)
     175              :          END IF
     176              : 
     177         7192 :          IF (size_keys(dimen) == almo_mat_dim_aobasis) THEN ! this dimension is AO
     178              : 
     179              :             ! structure of an AO dimension can be copied from matrix_qs
     180         2712 :             CALL dbcsr_get_info(matrix_qs, row_blk_size=blk_sizes)
     181              : 
     182              :             ! atomic clustering of AOs
     183         2712 :             IF (almo_scf_env%mat_distr_aos == almo_mat_distr_atomic) THEN
     184            0 :                ALLOCATE (block_sizes_new(natoms), distr_new_array(natoms))
     185            0 :                block_sizes_new(:) = blk_sizes(:)
     186            0 :                distr_new_array(:) = blk_distr(:)
     187              :                ! molecular clustering of AOs
     188         2712 :             ELSE IF (almo_scf_env%mat_distr_aos == almo_mat_distr_molecular) THEN
     189        10848 :                ALLOCATE (block_sizes_new(nmols), distr_new_array(nmols))
     190         2712 :                block_sizes_new(:) = 0
     191        47092 :                DO iatom = 1, natoms
     192              :                   block_sizes_new(almo_scf_env%domain_index_of_atom(iatom)) = &
     193              :                      block_sizes_new(almo_scf_env%domain_index_of_atom(iatom)) + &
     194        47092 :                      blk_sizes(iatom)
     195              :                END DO
     196        20742 :                DO imol = 1, nmols
     197              :                   distr_new_array(imol) = &
     198        20742 :                      blk_distr(almo_scf_env%first_atom_of_domain(imol))
     199              :                END DO
     200              :             ELSE
     201            0 :                CPABORT("Illegal distribution")
     202              :             END IF
     203              : 
     204              :          ELSE ! this dimension is not AO
     205              : 
     206              :             IF (size_keys(dimen) == almo_mat_dim_occ .OR. &
     207              :                 size_keys(dimen) == almo_mat_dim_virt .OR. &
     208         4480 :                 size_keys(dimen) == almo_mat_dim_virt_disc .OR. &
     209              :                 size_keys(dimen) == almo_mat_dim_virt_full) THEN ! this dim is MO
     210              : 
     211              :                ! atomic clustering of MOs
     212         4480 :                IF (almo_scf_env%mat_distr_mos == almo_mat_distr_atomic) THEN
     213            0 :                   nlength = natoms
     214            0 :                   ALLOCATE (block_sizes_new(nlength))
     215            0 :                   block_sizes_new(:) = 0
     216              :                   IF (size_keys(dimen) == almo_mat_dim_occ) THEN
     217              :                      ! currently distributing atomic distr of mos is not allowed
     218              :                      ! RZK-warning define nocc_of_atom and nvirt_atom to implement it
     219              :                      !block_sizes_new(:)=almo_scf_env%nocc_of_atom(:,spin_key)
     220              :                   ELSE IF (size_keys(dimen) == almo_mat_dim_virt) THEN
     221              :                      !block_sizes_new(:)=almo_scf_env%nvirt_of_atom(:,spin_key)
     222              :                   END IF
     223              :                   ! molecular clustering of MOs
     224         4480 :                ELSE IF (almo_scf_env%mat_distr_mos == almo_mat_distr_molecular) THEN
     225         4480 :                   nlength = nmols
     226        13440 :                   ALLOCATE (block_sizes_new(nlength))
     227         4480 :                   IF (size_keys(dimen) == almo_mat_dim_occ) THEN
     228        19240 :                      block_sizes_new(:) = almo_scf_env%nocc_of_domain(:, spin_key)
     229              :                      ! Handle zero-electron fragments by adding one-orbital that
     230              :                      ! must remain zero at all times
     231        19240 :                      WHERE (block_sizes_new == 0) block_sizes_new = 1
     232         1920 :                   ELSE IF (size_keys(dimen) == almo_mat_dim_virt_disc) THEN
     233            0 :                      block_sizes_new(:) = almo_scf_env%nvirt_disc_of_domain(:, spin_key)
     234         1920 :                   ELSE IF (size_keys(dimen) == almo_mat_dim_virt_full) THEN
     235         5772 :                      block_sizes_new(:) = almo_scf_env%nvirt_full_of_domain(:, spin_key)
     236         1152 :                   ELSE IF (size_keys(dimen) == almo_mat_dim_virt) THEN
     237         8658 :                      block_sizes_new(:) = almo_scf_env%nvirt_of_domain(:, spin_key)
     238              :                   END IF
     239              :                ELSE
     240            0 :                   CPABORT("Illegal distribution")
     241              :                END IF
     242              : 
     243              :             ELSE
     244              : 
     245            0 :                CPABORT("Illegal dimension")
     246              : 
     247              :             END IF ! end choosing dim size (occ, virt)
     248              : 
     249              :             ! distribution for MOs is copied from AOs
     250        13440 :             ALLOCATE (distr_new_array(nlength))
     251              :             ! atomic clustering
     252         4480 :             IF (almo_scf_env%mat_distr_mos == almo_mat_distr_atomic) THEN
     253            0 :                distr_new_array(:) = blk_distr(:)
     254              :                ! molecular clustering
     255         4480 :             ELSE IF (almo_scf_env%mat_distr_mos == almo_mat_distr_molecular) THEN
     256        33670 :                DO imol = 1, nmols
     257              :                   distr_new_array(imol) = &
     258        33670 :                      blk_distr(almo_scf_env%first_atom_of_domain(imol))
     259              :                END DO
     260              :             END IF
     261              :          END IF ! end choosing dimension size (AOs vs .NOT.AOs)
     262              : 
     263              :          ! create final arrays
     264        10788 :          IF (dimen == 1) THEN !rows
     265         3596 :             row_sizes_new => block_sizes_new
     266         3596 :             row_distr_new => distr_new_array
     267              :          ELSE !columns
     268         3596 :             col_sizes_new => block_sizes_new
     269         3596 :             col_distr_new => distr_new_array
     270              :          END IF
     271              :       END DO ! both rows and columns are done
     272              : 
     273              :       ! Create the distribution
     274              :       CALL dbcsr_distribution_new(dist_new, template=dist_qs, &
     275              :                                   row_dist=row_distr_new, col_dist=col_distr_new, &
     276         3596 :                                   reuse_arrays=.TRUE.)
     277              : 
     278              :       ! Create the matrix
     279              :       CALL dbcsr_create(matrix_new, name_new, &
     280              :                         dist_new, symmetry_new, &
     281         3596 :                         row_sizes_new, col_sizes_new, reuse_arrays=.TRUE.)
     282         3596 :       CALL dbcsr_distribution_release(dist_new)
     283              : 
     284              :       ! fill out reqired blocks with 1.0_dp to tell the dbcsr library
     285              :       ! which blocks to keep
     286         3596 :       IF (init_domains) THEN
     287              : 
     288         1426 :          CALL dbcsr_distribution_get(dist_new, mynode=mynode)
     289         1426 :          CALL dbcsr_work_create(matrix_new, work_mutable=.TRUE.)
     290              :          CALL dbcsr_get_info(matrix_new, nblkrows_total=nblkrows_tot, &
     291         1426 :                              row_blk_size=row_blk_size, col_blk_size=col_blk_size)
     292              :          ! start linear-scaling replacement:
     293              :          ! works only for molecular blocks AND molecular distributions
     294        10870 :          DO row = 1, nblkrows_tot
     295         9444 :             tr = .FALSE.
     296         9444 :             iblock_row = row
     297         9444 :             iblock_col = row
     298         9444 :             CALL dbcsr_get_stored_coordinates(matrix_new, iblock_row, iblock_col, hold)
     299              : 
     300        10870 :             IF (hold == mynode) THEN
     301              : 
     302        14166 :                active = .TRUE.
     303              : 
     304              :                one_dim_is_mo = .FALSE.
     305        14166 :                DO dimen = 1, 2 ! 1 - row, 2 - column dimension
     306        14166 :                   IF (size_keys(dimen) == almo_mat_dim_occ) one_dim_is_mo = .TRUE.
     307              :                END DO
     308         4722 :                IF (one_dim_is_mo) THEN
     309         1668 :                   IF (almo_scf_env%nocc_of_domain(row, spin_key) == 0) active = .FALSE.
     310              :                END IF
     311              : 
     312         4722 :                one_dim_is_mo = .FALSE.
     313        14166 :                DO dimen = 1, 2
     314        14166 :                   IF (size_keys(dimen) == almo_mat_dim_virt) one_dim_is_mo = .TRUE.
     315              :                END DO
     316         4722 :                IF (one_dim_is_mo) THEN
     317          417 :                   IF (almo_scf_env%nvirt_of_domain(row, spin_key) == 0) active = .FALSE.
     318              :                END IF
     319              : 
     320         4722 :                one_dim_is_mo = .FALSE.
     321        14166 :                DO dimen = 1, 2
     322        14166 :                   IF (size_keys(dimen) == almo_mat_dim_virt_disc) one_dim_is_mo = .TRUE.
     323              :                END DO
     324         4722 :                IF (one_dim_is_mo) THEN
     325            0 :                   IF (almo_scf_env%nvirt_disc_of_domain(row, spin_key) == 0) active = .FALSE.
     326              :                END IF
     327              : 
     328         4722 :                one_dim_is_mo = .FALSE.
     329        14166 :                DO dimen = 1, 2
     330        14166 :                   IF (size_keys(dimen) == almo_mat_dim_virt_full) one_dim_is_mo = .TRUE.
     331              :                END DO
     332         4722 :                IF (one_dim_is_mo) THEN
     333          417 :                   IF (almo_scf_env%nvirt_full_of_domain(row, spin_key) == 0) active = .FALSE.
     334              :                END IF
     335              : 
     336         4688 :                IF (active) THEN
     337        17592 :                   ALLOCATE (new_block(row_blk_size(iblock_row), col_blk_size(iblock_col)))
     338       840230 :                   new_block(:, :) = 1.0_dp
     339         4398 :                   CALL dbcsr_put_block(matrix_new, iblock_row, iblock_col, new_block)
     340         4398 :                   DEALLOCATE (new_block)
     341              :                END IF
     342              : 
     343              :             END IF ! mynode
     344              :          END DO
     345              :          ! end lnear-scaling replacement
     346              : 
     347              :       END IF ! init_domains
     348              : 
     349         3596 :       CALL dbcsr_finalize(matrix_new)
     350              : 
     351         3596 :       CALL timestop(handle)
     352              : 
     353         7192 :    END SUBROUTINE matrix_almo_create
     354              : 
     355              : ! **************************************************************************************************
     356              : !> \brief convert between two types of matrices: QS style to ALMO style
     357              : !> \param matrix_qs ...
     358              : !> \param matrix_almo ...
     359              : !> \param mat_distr_aos ...
     360              : !> \par History
     361              : !>       2011.06 created [Rustam Z Khaliullin]
     362              : !> \author Rustam Z Khaliullin
     363              : ! **************************************************************************************************
     364         2080 :    SUBROUTINE matrix_qs_to_almo(matrix_qs, matrix_almo, mat_distr_aos)
     365              : 
     366              :       TYPE(dbcsr_type)                                   :: matrix_qs, matrix_almo
     367              :       INTEGER                                            :: mat_distr_aos
     368              : 
     369              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'matrix_qs_to_almo'
     370              : 
     371              :       INTEGER                                            :: handle
     372              :       TYPE(dbcsr_type)                                   :: matrix_qs_nosym
     373              : 
     374         2080 :       CALL timeset(routineN, handle)
     375              :       !RZK-warning if it's not a N(AO)xN(AO) matrix then stop
     376              : 
     377         2080 :       SELECT CASE (mat_distr_aos)
     378              :       CASE (almo_mat_distr_atomic)
     379              :          ! automatic data_type conversion
     380            0 :          CALL dbcsr_copy(matrix_almo, matrix_qs)
     381              :       CASE (almo_mat_distr_molecular)
     382              :          ! desymmetrize the qs matrix
     383         2080 :          CALL dbcsr_create(matrix_qs_nosym, template=matrix_qs, matrix_type=dbcsr_type_no_symmetry)
     384         2080 :          CALL dbcsr_desymmetrize(matrix_qs, matrix_qs_nosym)
     385              : 
     386              :          ! perform the magic complete_redistribute
     387              :          ! before calling complete_redistribute set all blocks to zero
     388              :          ! otherwise the non-zero elements of the redistributed matrix,
     389              :          ! which are in zero-blocks of the original matrix, will remain
     390              :          ! in the final redistributed matrix. this is a bug in
     391              :          ! complete_redistribute. RZK-warning it should be later corrected by calling
     392              :          ! dbcsr_set to 0.0 from within complete_redistribute
     393         2080 :          CALL dbcsr_set(matrix_almo, 0.0_dp)
     394         2080 :          CALL dbcsr_complete_redistribute(matrix_qs_nosym, matrix_almo)
     395         2080 :          CALL dbcsr_release(matrix_qs_nosym)
     396              : 
     397              :       CASE DEFAULT
     398         2080 :          CPABORT("Unknown mat_distr_aos for matrix_qs_to_almo")
     399              :       END SELECT
     400              : 
     401         2080 :       CALL timestop(handle)
     402              : 
     403         2080 :    END SUBROUTINE matrix_qs_to_almo
     404              : 
     405              : ! **************************************************************************************************
     406              : !> \brief convert between two types of matrices: ALMO style to QS style
     407              : !> \param matrix_almo ...
     408              : !> \param matrix_qs ...
     409              : !> \param mat_distr_aos ...
     410              : !> \par History
     411              : !>       2011.06 created [Rustam Z Khaliullin]
     412              : !> \author Rustam Z Khaliullin
     413              : ! **************************************************************************************************
     414         1862 :    SUBROUTINE matrix_almo_to_qs(matrix_almo, matrix_qs, mat_distr_aos)
     415              :       TYPE(dbcsr_type)                                   :: matrix_almo, matrix_qs
     416              :       INTEGER, INTENT(IN)                                :: mat_distr_aos
     417              : 
     418              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'matrix_almo_to_qs'
     419              : 
     420              :       INTEGER                                            :: handle
     421              :       TYPE(dbcsr_type)                                   :: matrix_almo_redist
     422              : 
     423         1862 :       CALL timeset(routineN, handle)
     424              :       ! RZK-warning if it's not a N(AO)xN(AO) matrix then stop
     425              : 
     426         1862 :       SELECT CASE (mat_distr_aos)
     427              :       CASE (almo_mat_distr_atomic)
     428            0 :          CALL dbcsr_copy(matrix_qs, matrix_almo, keep_sparsity=.TRUE.)
     429              :       CASE (almo_mat_distr_molecular)
     430         1862 :          CALL dbcsr_create(matrix_almo_redist, template=matrix_qs)
     431         1862 :          CALL dbcsr_complete_redistribute(matrix_almo, matrix_almo_redist)
     432         1862 :          CALL dbcsr_set(matrix_qs, 0.0_dp)
     433         1862 :          CALL dbcsr_copy(matrix_qs, matrix_almo_redist, keep_sparsity=.TRUE.)
     434         1862 :          CALL dbcsr_release(matrix_almo_redist)
     435              :       CASE DEFAULT
     436         1862 :          CPABORT("Unknown mat_distr_aos for matrix_almo_to_qs")
     437              :       END SELECT
     438              : 
     439         1862 :       CALL timestop(handle)
     440              : 
     441         1862 :    END SUBROUTINE matrix_almo_to_qs
     442              : 
     443              : ! **************************************************************************************************
     444              : !> \brief Initialization of the QS and ALMO KS matrix
     445              : !> \param qs_env ...
     446              : !> \param matrix_ks ...
     447              : !> \param mat_distr_aos ...
     448              : !> \param eps_filter ...
     449              : !> \par History
     450              : !>       2011.05 created [Rustam Z Khaliullin]
     451              : !> \author Rustam Z Khaliullin
     452              : ! **************************************************************************************************
     453          122 :    SUBROUTINE init_almo_ks_matrix_via_qs(qs_env, matrix_ks, mat_distr_aos, eps_filter)
     454              : 
     455              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     456              :       TYPE(dbcsr_type), DIMENSION(:)                     :: matrix_ks
     457              :       INTEGER                                            :: mat_distr_aos
     458              :       REAL(KIND=dp)                                      :: eps_filter
     459              : 
     460              :       CHARACTER(len=*), PARAMETER :: routineN = 'init_almo_ks_matrix_via_qs'
     461              : 
     462              :       INTEGER                                            :: handle, ispin, nspin
     463          122 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_qs_ks, matrix_qs_s
     464              :       TYPE(dft_control_type), POINTER                    :: dft_control
     465              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     466          122 :          POINTER                                         :: sab_orb
     467              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     468              : 
     469          122 :       CALL timeset(routineN, handle)
     470              : 
     471          122 :       NULLIFY (sab_orb)
     472              : 
     473              :       ! get basic quantities from the qs_env
     474              :       CALL get_qs_env(qs_env, &
     475              :                       dft_control=dft_control, &
     476              :                       matrix_s=matrix_qs_s, &
     477              :                       matrix_ks=matrix_qs_ks, &
     478              :                       ks_env=ks_env, &
     479          122 :                       sab_orb=sab_orb)
     480              : 
     481          122 :       nspin = dft_control%nspins
     482              : 
     483              :       ! create matrix_ks in the QS env if necessary
     484          122 :       IF (.NOT. ASSOCIATED(matrix_qs_ks)) THEN
     485            0 :          CALL dbcsr_allocate_matrix_set(matrix_qs_ks, nspin)
     486            0 :          DO ispin = 1, nspin
     487            0 :             ALLOCATE (matrix_qs_ks(ispin)%matrix)
     488              :             CALL dbcsr_create(matrix_qs_ks(ispin)%matrix, &
     489            0 :                               template=matrix_qs_s(1)%matrix)
     490            0 :             CALL cp_dbcsr_alloc_block_from_nbl(matrix_qs_ks(ispin)%matrix, sab_orb)
     491            0 :             CALL dbcsr_set(matrix_qs_ks(ispin)%matrix, 0.0_dp)
     492              :          END DO
     493            0 :          CALL set_ks_env(ks_env, matrix_ks=matrix_qs_ks)
     494              :       END IF
     495              : 
     496              :       ! copy to ALMO
     497          250 :       DO ispin = 1, nspin
     498          128 :          CALL matrix_qs_to_almo(matrix_qs_ks(ispin)%matrix, matrix_ks(ispin), mat_distr_aos)
     499          250 :          CALL dbcsr_filter(matrix_ks(ispin), eps_filter)
     500              :       END DO
     501              : 
     502          122 :       CALL timestop(handle)
     503              : 
     504          122 :    END SUBROUTINE init_almo_ks_matrix_via_qs
     505              : 
     506              : ! **************************************************************************************************
     507              : !> \brief Create MOs in the QS env to be able to return ALMOs to QS
     508              : !> \param qs_env ...
     509              : !> \param almo_scf_env ...
     510              : !> \par History
     511              : !>       2016.12 created [Yifei Shi]
     512              : !> \author Yifei Shi
     513              : ! **************************************************************************************************
     514          244 :    SUBROUTINE construct_qs_mos(qs_env, almo_scf_env)
     515              : 
     516              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     517              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
     518              : 
     519              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'construct_qs_mos'
     520              : 
     521              :       INTEGER                                            :: handle, ispin, ncol_fm, nrow_fm
     522              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_tmp
     523              :       TYPE(cp_fm_type)                                   :: mo_fm_copy
     524              :       TYPE(dft_control_type), POINTER                    :: dft_control
     525          122 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     526              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     527              : 
     528          122 :       CALL timeset(routineN, handle)
     529              : 
     530              :       ! create and init scf_env (this is necessary to return MOs to qs)
     531          122 :       NULLIFY (mos, fm_struct_tmp, scf_env)
     532          122 :       ALLOCATE (scf_env)
     533          122 :       CALL scf_env_create(scf_env)
     534              : 
     535              :       !CALL qs_scf_env_initialize(qs_env, scf_env)
     536          122 :       CALL set_qs_env(qs_env, scf_env=scf_env)
     537          122 :       CALL get_qs_env(qs_env, dft_control=dft_control, mos=mos)
     538              : 
     539          122 :       CALL dbcsr_get_info(almo_scf_env%matrix_t(1), nfullrows_total=nrow_fm, nfullcols_total=ncol_fm)
     540              : 
     541              :       ! allocate and init mo_set
     542          250 :       DO ispin = 1, almo_scf_env%nspins
     543          128 :          CALL dbcsr_get_info(almo_scf_env%matrix_t(ispin), nfullrows_total=nrow_fm, nfullcols_total=ncol_fm)
     544              : 
     545              :          ! Currently only fm version of mo_set is usable.
     546              :          ! First transform the matrix_t to fm version
     547              :          ! Empty the containers to prevent memory leaks
     548          128 :          CALL deallocate_mo_set(mos(ispin))
     549              : 
     550          128 :          IF (almo_scf_env%nspins == 1) THEN
     551              :             CALL allocate_mo_set(mo_set=mos(ispin), &
     552              :                                  nao=nrow_fm, &
     553              :                                  nmo=ncol_fm, &
     554              :                                  nelectron=almo_scf_env%nelectrons_total, &
     555              :                                  n_el_f=REAL(almo_scf_env%nelectrons_total, dp), &
     556              :                                  maxocc=2.0_dp, &
     557          116 :                                  flexible_electron_count=dft_control%relax_multiplicity)
     558           12 :          ELSE IF (almo_scf_env%nspins == 2) THEN
     559              :             CALL allocate_mo_set(mo_set=mos(ispin), &
     560              :                                  nao=nrow_fm, &
     561              :                                  nmo=ncol_fm, &
     562              :                                  nelectron=SUM(almo_scf_env%nocc_of_domain(:, ispin)), &
     563              :                                  n_el_f=REAL(SUM(almo_scf_env%nocc_of_domain(:, ispin)), dp), &
     564              :                                  maxocc=1.0_dp, &
     565           60 :                                  flexible_electron_count=dft_control%relax_multiplicity)
     566              :          END IF
     567              : 
     568              :          CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=nrow_fm, ncol_global=ncol_fm, &
     569              :                                   context=almo_scf_env%blacs_env, &
     570          128 :                                   para_env=almo_scf_env%para_env)
     571              : 
     572          128 :          CALL cp_fm_create(mo_fm_copy, fm_struct_tmp, name="t_orthogonal_converted_to_fm")
     573          128 :          CALL cp_fm_struct_release(fm_struct_tmp)
     574              :          !CALL copy_dbcsr_to_fm(almo_scf_env%matrix_t(ispin), mo_fm_copy)
     575              : 
     576          128 :          CALL init_mo_set(mos(ispin), fm_ref=mo_fm_copy, name='fm_mo')
     577              : 
     578          506 :          CALL cp_fm_release(mo_fm_copy)
     579              : 
     580              :       END DO
     581              : 
     582          122 :       CALL timestop(handle)
     583              : 
     584          122 :    END SUBROUTINE construct_qs_mos
     585              : 
     586              : ! **************************************************************************************************
     587              : !> \brief return density matrix to the qs_env
     588              : !> \param qs_env ...
     589              : !> \param matrix_p ...
     590              : !> \param mat_distr_aos ...
     591              : !> \par History
     592              : !>       2011.05 created [Rustam Z Khaliullin]
     593              : !> \author Rustam Z Khaliullin
     594              : ! **************************************************************************************************
     595         1778 :    SUBROUTINE almo_dm_to_qs_env(qs_env, matrix_p, mat_distr_aos)
     596              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     597              :       TYPE(dbcsr_type), DIMENSION(:)                     :: matrix_p
     598              :       INTEGER, INTENT(IN)                                :: mat_distr_aos
     599              : 
     600              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'almo_dm_to_qs_env'
     601              : 
     602              :       INTEGER                                            :: handle, ispin, nspins
     603         1778 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao
     604              :       TYPE(qs_rho_type), POINTER                         :: rho
     605              : 
     606         1778 :       CALL timeset(routineN, handle)
     607              : 
     608         1778 :       NULLIFY (rho, rho_ao)
     609         1778 :       nspins = SIZE(matrix_p)
     610         1778 :       CALL get_qs_env(qs_env, rho=rho)
     611         1778 :       CALL qs_rho_get(rho, rho_ao=rho_ao)
     612              : 
     613              :       ! set the new density matrix
     614         3574 :       DO ispin = 1, nspins
     615              :          CALL matrix_almo_to_qs(matrix_p(ispin), &
     616              :                                 rho_ao(ispin)%matrix, &
     617         3574 :                                 mat_distr_aos)
     618              :       END DO
     619         1778 :       CALL qs_rho_update_rho(rho, qs_env=qs_env)
     620         1778 :       CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
     621              : 
     622         1778 :       CALL timestop(handle)
     623              : 
     624         1778 :    END SUBROUTINE almo_dm_to_qs_env
     625              : 
     626              : ! **************************************************************************************************
     627              : !> \brief uses the ALMO density matrix
     628              : !>        to compute KS matrix (inside QS environment) and the new energy
     629              : !> \param qs_env ...
     630              : !> \param matrix_p ...
     631              : !> \param energy_total ...
     632              : !> \param mat_distr_aos ...
     633              : !> \param smear ...
     634              : !> \param kTS_sum ...
     635              : !> \par History
     636              : !>       2011.05 created [Rustam Z Khaliullin]
     637              : !>       2018.09 smearing support [Ruben Staub]
     638              : !> \author Rustam Z Khaliullin
     639              : ! **************************************************************************************************
     640         1752 :    SUBROUTINE almo_dm_to_qs_ks(qs_env, matrix_p, energy_total, mat_distr_aos, smear, kTS_sum)
     641              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     642              :       TYPE(dbcsr_type), DIMENSION(:)                     :: matrix_p
     643              :       REAL(KIND=dp)                                      :: energy_total
     644              :       INTEGER, INTENT(IN)                                :: mat_distr_aos
     645              :       LOGICAL, INTENT(IN), OPTIONAL                      :: smear
     646              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: kTS_sum
     647              : 
     648              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'almo_dm_to_qs_ks'
     649              : 
     650              :       INTEGER                                            :: handle
     651              :       LOGICAL                                            :: smearing
     652              :       REAL(KIND=dp)                                      :: entropic_term
     653              :       TYPE(qs_energy_type), POINTER                      :: energy
     654              : 
     655         1752 :       CALL timeset(routineN, handle)
     656              : 
     657         1752 :       IF (PRESENT(smear)) THEN
     658         1752 :          smearing = smear
     659              :       ELSE
     660              :          smearing = .FALSE.
     661              :       END IF
     662              : 
     663         1752 :       IF (PRESENT(kTS_sum)) THEN
     664         1752 :          entropic_term = kTS_sum
     665              :       ELSE
     666              :          entropic_term = 0.0_dp
     667              :       END IF
     668              : 
     669         1752 :       NULLIFY (energy)
     670         1752 :       CALL get_qs_env(qs_env, energy=energy)
     671         1752 :       CALL almo_dm_to_qs_env(qs_env, matrix_p, mat_distr_aos)
     672              :       CALL qs_ks_update_qs_env(qs_env, calculate_forces=.FALSE., just_energy=.FALSE., &
     673         1752 :                                print_active=.TRUE.)
     674              : 
     675              :       !! Add electronic entropy contribution if smearing is requested
     676              :       !! Previous QS entropy is replaced by the sum of the entropy for each spin
     677         1752 :       IF (smearing) THEN
     678           20 :          energy%total = energy%total - energy%kTS + entropic_term
     679              :       END IF
     680              : 
     681         1752 :       energy_total = energy%total
     682              : 
     683         1752 :       CALL timestop(handle)
     684              : 
     685         1752 :    END SUBROUTINE almo_dm_to_qs_ks
     686              : 
     687              : ! **************************************************************************************************
     688              : !> \brief uses the ALMO density matrix
     689              : !>        to compute ALMO KS matrix and the new energy
     690              : !> \param qs_env ...
     691              : !> \param matrix_p ...
     692              : !> \param matrix_ks ...
     693              : !> \param energy_total ...
     694              : !> \param eps_filter ...
     695              : !> \param mat_distr_aos ...
     696              : !> \param smear ...
     697              : !> \param kTS_sum ...
     698              : !> \par History
     699              : !>       2011.05 created [Rustam Z Khaliullin]
     700              : !>       2018.09 smearing support [Ruben Staub]
     701              : !> \author Rustam Z Khaliullin
     702              : ! **************************************************************************************************
     703         1752 :    SUBROUTINE almo_dm_to_almo_ks(qs_env, matrix_p, matrix_ks, energy_total, eps_filter, &
     704              :                                  mat_distr_aos, smear, kTS_sum)
     705              : 
     706              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     707              :       TYPE(dbcsr_type), DIMENSION(:)                     :: matrix_p, matrix_ks
     708              :       REAL(KIND=dp)                                      :: energy_total, eps_filter
     709              :       INTEGER, INTENT(IN)                                :: mat_distr_aos
     710              :       LOGICAL, INTENT(IN), OPTIONAL                      :: smear
     711              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: kTS_sum
     712              : 
     713              :       CHARACTER(len=*), PARAMETER :: routineN = 'almo_dm_to_almo_ks'
     714              : 
     715              :       INTEGER                                            :: handle, ispin, nspins
     716              :       LOGICAL                                            :: smearing
     717              :       REAL(KIND=dp)                                      :: entropic_term
     718         1752 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_qs_ks
     719              : 
     720         1752 :       CALL timeset(routineN, handle)
     721              : 
     722         1752 :       IF (PRESENT(smear)) THEN
     723          470 :          smearing = smear
     724              :       ELSE
     725         1282 :          smearing = .FALSE.
     726              :       END IF
     727              : 
     728         1752 :       IF (PRESENT(kTS_sum)) THEN
     729          470 :          entropic_term = kTS_sum
     730              :       ELSE
     731         1282 :          entropic_term = 0.0_dp
     732              :       END IF
     733              : 
     734              :       ! update KS matrix in the QS env
     735              :       CALL almo_dm_to_qs_ks(qs_env, matrix_p, energy_total, mat_distr_aos, &
     736              :                             smear=smearing, &
     737         1752 :                             kTS_sum=entropic_term)
     738              : 
     739         1752 :       nspins = SIZE(matrix_ks)
     740              : 
     741              :       ! get KS matrix from the QS env and convert to the ALMO format
     742         1752 :       CALL get_qs_env(qs_env, matrix_ks=matrix_qs_ks)
     743         3522 :       DO ispin = 1, nspins
     744         1770 :          CALL matrix_qs_to_almo(matrix_qs_ks(ispin)%matrix, matrix_ks(ispin), mat_distr_aos)
     745         3522 :          CALL dbcsr_filter(matrix_ks(ispin), eps_filter)
     746              :       END DO
     747              : 
     748         1752 :       CALL timestop(handle)
     749              : 
     750         1752 :    END SUBROUTINE almo_dm_to_almo_ks
     751              : 
     752              : ! **************************************************************************************************
     753              : !> \brief update qs_env total energy
     754              : !> \param qs_env ...
     755              : !> \param energy ...
     756              : !> \param energy_singles_corr ...
     757              : !> \par History
     758              : !>       2013.03 created [Rustam Z Khaliullin]
     759              : !> \author Rustam Z Khaliullin
     760              : ! **************************************************************************************************
     761          112 :    SUBROUTINE almo_scf_update_ks_energy(qs_env, energy, energy_singles_corr)
     762              : 
     763              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     764              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: energy, energy_singles_corr
     765              : 
     766              :       TYPE(qs_energy_type), POINTER                      :: qs_energy
     767              : 
     768          112 :       CALL get_qs_env(qs_env, energy=qs_energy)
     769              : 
     770          112 :       IF (PRESENT(energy_singles_corr)) THEN
     771           26 :          qs_energy%singles_corr = energy_singles_corr
     772              :       ELSE
     773           86 :          qs_energy%singles_corr = 0.0_dp
     774              :       END IF
     775              : 
     776          112 :       IF (PRESENT(energy)) THEN
     777          112 :          qs_energy%total = energy
     778              :       END IF
     779              : 
     780          112 :       qs_energy%total = qs_energy%total + qs_energy%singles_corr
     781              : 
     782          112 :    END SUBROUTINE almo_scf_update_ks_energy
     783              : 
     784              : ! **************************************************************************************************
     785              : !> \brief Creates the matrix that imposes absolute locality on MOs
     786              : !> \param qs_env ...
     787              : !> \param almo_scf_env ...
     788              : !> \par History
     789              : !>       2011.11 created [Rustam Z. Khaliullin]
     790              : !> \author Rustam Z. Khaliullin
     791              : ! **************************************************************************************************
     792          122 :    SUBROUTINE almo_scf_construct_quencher(qs_env, almo_scf_env)
     793              : 
     794              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     795              :       TYPE(almo_scf_env_type), INTENT(INOUT)             :: almo_scf_env
     796              : 
     797              :       CHARACTER(len=*), PARAMETER :: routineN = 'almo_scf_construct_quencher'
     798              : 
     799              :       CHARACTER                                          :: sym
     800              :       INTEGER :: col, contact_atom_1, contact_atom_2, domain_col, domain_map_local_entries, &
     801              :          domain_row, global_entries, global_list_length, grid1, GroupID, handle, hold, iatom, &
     802              :          iatom2, iblock_col, iblock_row, idomain, idomain2, ientry, igrid, ineig, ineighbor, &
     803              :          iNode, inode2, ipair, ispin, jatom, jatom2, jdomain2, local_list_length, &
     804              :          max_domain_neighbors, max_neig, mynode, nblkcols_tot, nblkrows_tot, nblks, ndomains, &
     805              :          neig_temp, nnode2, nNodes, row, unit_nr
     806          122 :       INTEGER, ALLOCATABLE, DIMENSION(:) :: current_number_neighbors, domain_entries_cpu, &
     807          122 :          domain_map_global, domain_map_local, first_atom_of_molecule, global_list, &
     808          122 :          last_atom_of_molecule, list_length_cpu, list_offset_cpu, local_list, offset_for_cpu
     809          122 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: domain_grid, domain_neighbor_list, &
     810          122 :                                                             domain_neighbor_list_excessive
     811          122 :       INTEGER, DIMENSION(:), POINTER                     :: col_blk_size, row_blk_size
     812              :       LOGICAL                                            :: already_listed, block_active, &
     813              :                                                             delayed_increment, found, &
     814              :                                                             max_neig_fails, tr
     815              :       REAL(KIND=dp)                                      :: contact1_radius, contact2_radius, &
     816              :                                                             distance, distance_squared, overlap, &
     817              :                                                             r0, r1, s0, s1, trial_distance_squared
     818          122 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: new_block
     819              :       REAL(KIND=dp), DIMENSION(3)                        :: rab
     820          122 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: p_old_block
     821              :       TYPE(cell_type), POINTER                           :: cell
     822              :       TYPE(cp_logger_type), POINTER                      :: logger
     823              :       TYPE(dbcsr_distribution_type)                      :: dist
     824          122 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
     825              :       TYPE(dbcsr_type)                                   :: matrix_s_sym
     826          122 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     827              :       TYPE(mp_comm_type)                                 :: group
     828              :       TYPE(neighbor_list_iterator_p_type), &
     829          122 :          DIMENSION(:), POINTER                           :: nl_iterator, nl_iterator2
     830              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     831          122 :          POINTER                                         :: sab_almo
     832          122 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     833              : 
     834          122 :       CALL timeset(routineN, handle)
     835              : 
     836              :       ! get a useful output_unit
     837          122 :       logger => cp_get_default_logger()
     838          122 :       IF (logger%para_env%is_source()) THEN
     839           61 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     840              :       ELSE
     841              :          unit_nr = -1
     842              :       END IF
     843              : 
     844          122 :       ndomains = almo_scf_env%ndomains
     845              : 
     846              :       CALL get_qs_env(qs_env=qs_env, &
     847              :                       particle_set=particle_set, &
     848              :                       molecule_set=molecule_set, &
     849              :                       cell=cell, &
     850              :                       matrix_s=matrix_s, &
     851          122 :                       sab_almo=sab_almo)
     852              : 
     853              :       ! if we are dealing with molecules get info about them
     854          122 :       IF (almo_scf_env%domain_layout_mos == almo_domain_layout_molecular .OR. &
     855              :           almo_scf_env%domain_layout_aos == almo_domain_layout_molecular) THEN
     856          366 :          ALLOCATE (first_atom_of_molecule(almo_scf_env%nmolecules))
     857          244 :          ALLOCATE (last_atom_of_molecule(almo_scf_env%nmolecules))
     858              :          CALL get_molecule_set_info(molecule_set, &
     859              :                                     mol_to_first_atom=first_atom_of_molecule, &
     860          122 :                                     mol_to_last_atom=last_atom_of_molecule)
     861              :       END IF
     862              : 
     863              :       ! create a symmetrized copy of the ao overlap
     864              :       CALL dbcsr_create(matrix_s_sym, &
     865              :                         template=almo_scf_env%matrix_s(1), &
     866          122 :                         matrix_type=dbcsr_type_no_symmetry)
     867              :       CALL dbcsr_get_info(almo_scf_env%matrix_s(1), &
     868          122 :                           matrix_type=sym)
     869          122 :       IF (sym == dbcsr_type_no_symmetry) THEN
     870            0 :          CALL dbcsr_copy(matrix_s_sym, almo_scf_env%matrix_s(1))
     871              :       ELSE
     872              :          CALL dbcsr_desymmetrize(almo_scf_env%matrix_s(1), &
     873          122 :                                  matrix_s_sym)
     874              :       END IF
     875              : 
     876          494 :       ALLOCATE (almo_scf_env%quench_t(almo_scf_env%nspins))
     877          494 :       ALLOCATE (almo_scf_env%domain_map(almo_scf_env%nspins))
     878              : 
     879          250 :       DO ispin = 1, almo_scf_env%nspins
     880              : 
     881              :          ! create the sparsity template for the occupied orbitals
     882              :          CALL matrix_almo_create(matrix_new=almo_scf_env%quench_t(ispin), &
     883              :                                  matrix_qs=matrix_s(1)%matrix, &
     884              :                                  almo_scf_env=almo_scf_env, &
     885              :                                  name_new="T_QUENCHER", &
     886              :                                  size_keys=[almo_mat_dim_aobasis, almo_mat_dim_occ], &
     887              :                                  symmetry_new=dbcsr_type_no_symmetry, &
     888              :                                  spin_key=ispin, &
     889          128 :                                  init_domains=.FALSE.)
     890              : 
     891              :          ! initialize distance quencher
     892          128 :          CALL dbcsr_work_create(almo_scf_env%quench_t(ispin), work_mutable=.TRUE.)
     893              :          CALL dbcsr_get_info(almo_scf_env%quench_t(ispin), distribution=dist, &
     894          128 :                              nblkrows_total=nblkrows_tot, nblkcols_total=nblkcols_tot)
     895          128 :          CALL dbcsr_distribution_get(dist, numnodes=nNodes, group=GroupID, mynode=mynode)
     896          128 :          CALL group%set_handle(groupid)
     897              : 
     898              :          ! create global atom neighbor list from the local lists
     899              :          ! first, calculate number of local pairs
     900          128 :          local_list_length = 0
     901          128 :          CALL neighbor_list_iterator_create(nl_iterator, sab_almo)
     902        39611 :          DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
     903              :             ! nnode - total number of neighbors for iatom
     904              :             ! inode - current neighbor count
     905              :             CALL get_iterator_info(nl_iterator, &
     906        39483 :                                    iatom=iatom2, jatom=jatom2, inode=inode2, nnode=nnode2)
     907        39611 :             IF (inode2 == 1) THEN
     908         2049 :                local_list_length = local_list_length + nnode2
     909              :             END IF
     910              :          END DO
     911          128 :          CALL neighbor_list_iterator_release(nl_iterator)
     912              : 
     913              :          ! second, extract the local list to an array
     914          383 :          ALLOCATE (local_list(2*local_list_length))
     915          128 :          local_list(:) = 0
     916          128 :          local_list_length = 0
     917          128 :          CALL neighbor_list_iterator_create(nl_iterator2, sab_almo)
     918        39611 :          DO WHILE (neighbor_list_iterate(nl_iterator2) == 0)
     919              :             CALL get_iterator_info(nl_iterator2, &
     920        39483 :                                    iatom=iatom2, jatom=jatom2)
     921        39483 :             local_list(2*local_list_length + 1) = iatom2
     922        39483 :             local_list(2*local_list_length + 2) = jatom2
     923        39483 :             local_list_length = local_list_length + 1
     924              :          END DO ! end loop over pairs of atoms
     925          128 :          CALL neighbor_list_iterator_release(nl_iterator2)
     926              : 
     927              :          ! third, communicate local length to the other nodes
     928          512 :          ALLOCATE (list_length_cpu(nNodes), list_offset_cpu(nNodes))
     929          128 :          CALL group%allgather(2*local_list_length, list_length_cpu)
     930              : 
     931              :          ! fourth, create a global list
     932          128 :          list_offset_cpu(1) = 0
     933          256 :          DO iNode = 2, nNodes
     934              :             list_offset_cpu(iNode) = list_offset_cpu(iNode - 1) + &
     935          256 :                                      list_length_cpu(iNode - 1)
     936              :          END DO
     937          128 :          global_list_length = list_offset_cpu(nNodes) + list_length_cpu(nNodes)
     938              : 
     939              :          ! fifth, communicate all list data
     940          384 :          ALLOCATE (global_list(global_list_length))
     941              :          CALL group%allgatherv(local_list, global_list, &
     942          128 :                                list_length_cpu, list_offset_cpu)
     943          128 :          DEALLOCATE (list_length_cpu, list_offset_cpu)
     944          128 :          DEALLOCATE (local_list)
     945              : 
     946              :          ! calculate maximum number of atoms surrounding the domain
     947          384 :          ALLOCATE (current_number_neighbors(almo_scf_env%ndomains))
     948          128 :          current_number_neighbors(:) = 0
     949          128 :          global_list_length = global_list_length/2
     950        79094 :          DO ipair = 1, global_list_length
     951        78966 :             iatom2 = global_list(2*(ipair - 1) + 1)
     952        78966 :             jatom2 = global_list(2*(ipair - 1) + 2)
     953        78966 :             idomain2 = almo_scf_env%domain_index_of_atom(iatom2)
     954        78966 :             jdomain2 = almo_scf_env%domain_index_of_atom(jatom2)
     955              :             ! add to the list
     956        78966 :             current_number_neighbors(idomain2) = current_number_neighbors(idomain2) + 1
     957              :             ! add j,i with i,j
     958        79094 :             IF (idomain2 /= jdomain2) THEN
     959        63144 :                current_number_neighbors(jdomain2) = current_number_neighbors(jdomain2) + 1
     960              :             END IF
     961              :          END DO
     962          962 :          max_domain_neighbors = MAXVAL(current_number_neighbors)
     963              : 
     964              :          ! use the global atom neighbor list to create a global domain neighbor list
     965          512 :          ALLOCATE (domain_neighbor_list_excessive(ndomains, max_domain_neighbors))
     966          962 :          current_number_neighbors(:) = 1
     967          962 :          DO ipair = 1, ndomains
     968          962 :             domain_neighbor_list_excessive(ipair, 1) = ipair
     969              :          END DO
     970        79094 :          DO ipair = 1, global_list_length
     971        78966 :             iatom2 = global_list(2*(ipair - 1) + 1)
     972        78966 :             jatom2 = global_list(2*(ipair - 1) + 2)
     973        78966 :             idomain2 = almo_scf_env%domain_index_of_atom(iatom2)
     974        78966 :             jdomain2 = almo_scf_env%domain_index_of_atom(jatom2)
     975        78966 :             already_listed = .FALSE.
     976       325938 :             DO ineighbor = 1, current_number_neighbors(idomain2)
     977       325938 :                IF (domain_neighbor_list_excessive(idomain2, ineighbor) == jdomain2) THEN
     978              :                   already_listed = .TRUE.
     979              :                   EXIT
     980              :                END IF
     981              :             END DO
     982        79094 :             IF (.NOT. already_listed) THEN
     983              :                ! add to the list
     984         2722 :                current_number_neighbors(idomain2) = current_number_neighbors(idomain2) + 1
     985         2722 :                domain_neighbor_list_excessive(idomain2, current_number_neighbors(idomain2)) = jdomain2
     986              :                ! add j,i with i,j
     987         2722 :                IF (idomain2 /= jdomain2) THEN
     988         2722 :                   current_number_neighbors(jdomain2) = current_number_neighbors(jdomain2) + 1
     989         2722 :                   domain_neighbor_list_excessive(jdomain2, current_number_neighbors(jdomain2)) = idomain2
     990              :                END IF
     991              :             END IF
     992              :          END DO ! end loop over pairs of atoms
     993          128 :          DEALLOCATE (global_list)
     994              : 
     995          962 :          max_domain_neighbors = MAXVAL(current_number_neighbors)
     996          512 :          ALLOCATE (domain_neighbor_list(ndomains, max_domain_neighbors))
     997          128 :          domain_neighbor_list(:, :) = 0
     998         7248 :          domain_neighbor_list(:, :) = domain_neighbor_list_excessive(:, 1:max_domain_neighbors)
     999          128 :          DEALLOCATE (domain_neighbor_list_excessive)
    1000              : 
    1001          384 :          ALLOCATE (almo_scf_env%domain_map(ispin)%index1(ndomains))
    1002          384 :          ALLOCATE (almo_scf_env%domain_map(ispin)%pairs(max_domain_neighbors*ndomains, 2))
    1003        12956 :          almo_scf_env%domain_map(ispin)%pairs(:, :) = 0
    1004          962 :          almo_scf_env%domain_map(ispin)%index1(:) = 0
    1005          128 :          domain_map_local_entries = 0
    1006              : 
    1007              :          ! RZK-warning intermediate [0,1] quencher values are ill-defined
    1008              :          ! for molecules (not continuous and conceptually inadequate)
    1009              : 
    1010              :          CALL dbcsr_get_info(almo_scf_env%quench_t(ispin), &
    1011          128 :                              row_blk_size=row_blk_size, col_blk_size=col_blk_size)
    1012              :          ! O(N) loop over domain pairs
    1013          962 :          DO row = 1, nblkrows_tot
    1014         7240 :             DO col = 1, current_number_neighbors(row)
    1015         6278 :                tr = .FALSE.
    1016         6278 :                iblock_row = row
    1017         6278 :                iblock_col = domain_neighbor_list(row, col)
    1018              :                CALL dbcsr_get_stored_coordinates(almo_scf_env%quench_t(ispin), &
    1019         6278 :                                                  iblock_row, iblock_col, hold)
    1020              : 
    1021         7112 :                IF (hold == mynode) THEN
    1022              : 
    1023              :                   ! Translate indices of distribution blocks to indices of domain blocks
    1024              :                   ! Rows are AOs
    1025         3139 :                   domain_row = almo_scf_env%domain_index_of_ao_block(iblock_row)
    1026              :                   ! Columns are electrons (i.e. MOs)
    1027         3139 :                   domain_col = almo_scf_env%domain_index_of_mo_block(iblock_col)
    1028              : 
    1029         3139 :                   SELECT CASE (almo_scf_env%constraint_type)
    1030              :                   CASE (almo_constraint_block_diagonal)
    1031              : 
    1032            0 :                      block_active = .FALSE.
    1033              :                      ! type of electron groups
    1034            0 :                      IF (almo_scf_env%domain_layout_mos == almo_domain_layout_molecular) THEN
    1035              : 
    1036              :                         ! type of ao domains
    1037            0 :                         IF (almo_scf_env%domain_layout_aos == almo_domain_layout_molecular) THEN
    1038              : 
    1039              :                            ! ao domains are molecular / electron groups are molecular
    1040            0 :                            IF (domain_row == domain_col) THEN
    1041              :                               block_active = .TRUE.
    1042              :                            END IF
    1043              : 
    1044              :                         ELSE ! ao domains are atomic
    1045              : 
    1046              :                            ! ao domains are atomic / electron groups are molecular
    1047            0 :                            CPABORT("Illegal: atomic domains and molecular groups")
    1048              : 
    1049              :                         END IF
    1050              : 
    1051              :                      ELSE ! electron groups are atomic
    1052              : 
    1053              :                         ! type of ao domains
    1054            0 :                         IF (almo_scf_env%domain_layout_aos == almo_domain_layout_molecular) THEN
    1055              : 
    1056              :                            ! ao domains are molecular / electron groups are atomic
    1057            0 :                            CPABORT("Illegal: molecular domains and atomic groups")
    1058              : 
    1059              :                         ELSE
    1060              : 
    1061              :                            ! ao domains are atomic / electron groups are atomic
    1062            0 :                            IF (domain_row == domain_col) THEN
    1063              :                               block_active = .TRUE.
    1064              :                            END IF
    1065              : 
    1066              :                         END IF
    1067              : 
    1068              :                      END IF ! end type of electron groups
    1069              : 
    1070            0 :                      IF (block_active) THEN
    1071              : 
    1072            0 :                         ALLOCATE (new_block(row_blk_size(iblock_row), col_blk_size(iblock_col)))
    1073            0 :                         new_block(:, :) = 1.0_dp
    1074            0 :                         CALL dbcsr_put_block(almo_scf_env%quench_t(ispin), iblock_row, iblock_col, new_block)
    1075            0 :                         DEALLOCATE (new_block)
    1076              : 
    1077            0 :                         IF (domain_map_local_entries >= max_domain_neighbors*almo_scf_env%ndomains) THEN
    1078            0 :                            CPABORT("weird... max_domain_neighbors is exceeded")
    1079              :                         END IF
    1080            0 :                         almo_scf_env%domain_map(ispin)%pairs(domain_map_local_entries + 1, 1) = iblock_row
    1081            0 :                         almo_scf_env%domain_map(ispin)%pairs(domain_map_local_entries + 1, 2) = iblock_col
    1082            0 :                         domain_map_local_entries = domain_map_local_entries + 1
    1083              : 
    1084              :                      END IF
    1085              : 
    1086              :                   CASE (almo_constraint_ao_overlap)
    1087              : 
    1088              :                      ! type of electron groups
    1089            0 :                      IF (almo_scf_env%domain_layout_mos == almo_domain_layout_molecular) THEN
    1090              : 
    1091              :                         ! type of ao domains
    1092            0 :                         IF (almo_scf_env%domain_layout_aos == almo_domain_layout_molecular) THEN
    1093              : 
    1094              :                            ! ao domains are molecular / electron groups are molecular
    1095              : 
    1096              :                            ! compute the maximum overlap between the atoms of the two molecules
    1097            0 :                            CALL dbcsr_get_block_p(matrix_s_sym, iblock_row, iblock_col, p_old_block, found)
    1098            0 :                            IF (found) THEN
    1099            0 :                               overlap = MAXVAL(ABS(p_old_block))
    1100              :                            ELSE
    1101              :                               overlap = 0.0_dp
    1102              :                            END IF
    1103              : 
    1104              :                         ELSE ! ao domains are atomic
    1105              : 
    1106              :                            ! ao domains are atomic / electron groups are molecular
    1107              :                            ! overlap_between_atom_and_molecule(atom=domain_row,molecule=domain_col)
    1108            0 :                            CPABORT("atomic domains and molecular groups - NYI")
    1109              : 
    1110              :                         END IF
    1111              : 
    1112              :                      ELSE ! electron groups are atomic
    1113              : 
    1114              :                         ! type of ao domains
    1115            0 :                         IF (almo_scf_env%domain_layout_aos == almo_domain_layout_molecular) THEN
    1116              : 
    1117              :                            ! ao domains are molecular / electron groups are atomic
    1118              :                            ! overlap_between_atom_and_molecule(atom=domain_col,molecule=domain_row)
    1119            0 :                            CPABORT("molecular domains and atomic groups - NYI")
    1120              : 
    1121              :                         ELSE
    1122              : 
    1123              :                            ! ao domains are atomic / electron groups are atomic
    1124              :                            ! compute max overlap between atoms: domain_row and domain_col
    1125            0 :                            CALL dbcsr_get_block_p(matrix_s_sym, iblock_row, iblock_col, p_old_block, found)
    1126            0 :                            IF (found) THEN
    1127            0 :                               overlap = MAXVAL(ABS(p_old_block))
    1128              :                            ELSE
    1129              :                               overlap = 0.0_dp
    1130              :                            END IF
    1131              : 
    1132              :                         END IF
    1133              : 
    1134              :                      END IF ! end type of electron groups
    1135              : 
    1136            0 :                      s0 = -LOG10(ABS(almo_scf_env%quencher_s0))
    1137            0 :                      s1 = -LOG10(ABS(almo_scf_env%quencher_s1))
    1138            0 :                      IF (overlap == 0.0_dp) THEN
    1139            0 :                         overlap = -LOG10(ABS(almo_scf_env%eps_filter)) + 100.0_dp
    1140              :                      ELSE
    1141            0 :                         overlap = -LOG10(overlap)
    1142              :                      END IF
    1143            0 :                      IF (s0 < 0.0_dp) THEN
    1144            0 :                         CPABORT("S0 is less than zero")
    1145              :                      END IF
    1146            0 :                      IF (s1 <= 0.0_dp) THEN
    1147            0 :                         CPABORT("S1 is less than or equal to zero")
    1148              :                      END IF
    1149            0 :                      IF (s0 >= s1) THEN
    1150            0 :                         CPABORT("S0 is greater than or equal to S1")
    1151              :                      END IF
    1152              : 
    1153              :                      ! Fill in non-zero blocks if AOs are close to the electron center
    1154            0 :                      IF (overlap < s1) THEN
    1155            0 :                         ALLOCATE (new_block(row_blk_size(iblock_row), col_blk_size(iblock_col)))
    1156            0 :                         IF (overlap <= s0) THEN
    1157            0 :                            new_block(:, :) = 1.0_dp
    1158              :                         ELSE
    1159            0 :                            new_block(:, :) = 1.0_dp/(1.0_dp + EXP(-(s0 - s1)/(s0 - overlap) - (s0 - s1)/(overlap - s1)))
    1160              :                         END IF
    1161              : 
    1162            0 :                         IF (ABS(new_block(1, 1)) > ABS(almo_scf_env%eps_filter)) THEN
    1163            0 :                            IF (domain_map_local_entries >= max_domain_neighbors*almo_scf_env%ndomains) THEN
    1164            0 :                               CPABORT("weird... max_domain_neighbors is exceeded")
    1165              :                            END IF
    1166            0 :                            almo_scf_env%domain_map(ispin)%pairs(domain_map_local_entries + 1, 1) = iblock_row
    1167            0 :                            almo_scf_env%domain_map(ispin)%pairs(domain_map_local_entries + 1, 2) = iblock_col
    1168            0 :                            domain_map_local_entries = domain_map_local_entries + 1
    1169              :                         END IF
    1170              : 
    1171            0 :                         CALL dbcsr_put_block(almo_scf_env%quench_t(ispin), iblock_row, iblock_col, new_block)
    1172            0 :                         DEALLOCATE (new_block)
    1173              : 
    1174              :                      END IF
    1175              : 
    1176              :                   CASE (almo_constraint_distance)
    1177              : 
    1178              :                      ! type of electron groups
    1179         3139 :                      IF (almo_scf_env%domain_layout_mos == almo_domain_layout_molecular) THEN
    1180              : 
    1181              :                         ! type of ao domains
    1182         3139 :                         IF (almo_scf_env%domain_layout_aos == almo_domain_layout_molecular) THEN
    1183              : 
    1184              :                            ! ao domains are molecular / electron groups are molecular
    1185              : 
    1186              :                            ! compute distance between molecules: domain_row and domain_col
    1187              :                            ! distance between molecules is defined as the smallest
    1188              :                            ! distance among all atom pairs
    1189         3139 :                            IF (domain_row == domain_col) THEN
    1190          417 :                               distance = 0.0_dp
    1191          417 :                               contact_atom_1 = first_atom_of_molecule(domain_row)
    1192          417 :                               contact_atom_2 = first_atom_of_molecule(domain_col)
    1193              :                            ELSE
    1194         2722 :                               distance_squared = 1.0E+100_dp
    1195         2722 :                               contact_atom_1 = -1
    1196         2722 :                               contact_atom_2 = -1
    1197         9074 :                               DO iatom = first_atom_of_molecule(domain_row), last_atom_of_molecule(domain_row)
    1198        26414 :                                  DO jatom = first_atom_of_molecule(domain_col), last_atom_of_molecule(domain_col)
    1199        17340 :                                     rab(:) = pbc(particle_set(iatom)%r(:), particle_set(jatom)%r(:), cell)
    1200        17340 :                                     trial_distance_squared = rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3)
    1201        23692 :                                     IF (trial_distance_squared < distance_squared) THEN
    1202         6391 :                                        distance_squared = trial_distance_squared
    1203         6391 :                                        contact_atom_1 = iatom
    1204         6391 :                                        contact_atom_2 = jatom
    1205              :                                     END IF
    1206              :                                  END DO ! jatom
    1207              :                               END DO ! iatom
    1208         2722 :                               CPASSERT(contact_atom_1 > 0)
    1209         2722 :                               distance = SQRT(distance_squared)
    1210              :                            END IF
    1211              : 
    1212              :                         ELSE ! ao domains are atomic
    1213              : 
    1214              :                            ! ao domains are atomic / electron groups are molecular
    1215              :                            !distance_between_atom_and_molecule(atom=domain_row,molecule=domain_col)
    1216            0 :                            CPABORT("atomic domains and molecular groups - NYI")
    1217              : 
    1218              :                         END IF
    1219              : 
    1220              :                      ELSE ! electron groups are atomic
    1221              : 
    1222              :                         ! type of ao domains
    1223            0 :                         IF (almo_scf_env%domain_layout_aos == almo_domain_layout_molecular) THEN
    1224              : 
    1225              :                            ! ao domains are molecular / electron groups are atomic
    1226              :                            !distance_between_atom_and_molecule(atom=domain_col,molecule=domain_row)
    1227            0 :                            CPABORT("molecular domains and atomic groups - NYI")
    1228              : 
    1229              :                         ELSE
    1230              : 
    1231              :                            ! ao domains are atomic / electron groups are atomic
    1232              :                            ! compute distance between atoms: domain_row and domain_col
    1233            0 :                            rab(:) = pbc(particle_set(domain_row)%r(:), particle_set(domain_col)%r(:), cell)
    1234            0 :                            distance = SQRT(rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3))
    1235            0 :                            contact_atom_1 = domain_row
    1236            0 :                            contact_atom_2 = domain_col
    1237              : 
    1238              :                         END IF
    1239              : 
    1240              :                      END IF ! end type of electron groups
    1241              : 
    1242              :                      ! get atomic radii to compute distance cutoff threshold
    1243         3139 :                      IF (almo_scf_env%quencher_radius_type == do_bondparm_covalent) THEN
    1244              :                         CALL get_atomic_kind(atomic_kind=particle_set(contact_atom_1)%atomic_kind, &
    1245            0 :                                              rcov=contact1_radius)
    1246              :                         CALL get_atomic_kind(atomic_kind=particle_set(contact_atom_2)%atomic_kind, &
    1247            0 :                                              rcov=contact2_radius)
    1248         3139 :                      ELSE IF (almo_scf_env%quencher_radius_type == do_bondparm_vdw) THEN
    1249              :                         CALL get_atomic_kind(atomic_kind=particle_set(contact_atom_1)%atomic_kind, &
    1250         3139 :                                              rvdw=contact1_radius)
    1251              :                         CALL get_atomic_kind(atomic_kind=particle_set(contact_atom_2)%atomic_kind, &
    1252         3139 :                                              rvdw=contact2_radius)
    1253              :                      ELSE
    1254            0 :                         CPABORT("Illegal quencher_radius_type")
    1255              :                      END IF
    1256         3139 :                      contact1_radius = cp_unit_to_cp2k(contact1_radius, "angstrom")
    1257         3139 :                      contact2_radius = cp_unit_to_cp2k(contact2_radius, "angstrom")
    1258              : 
    1259              :                      !RZK-warning the procedure is faulty for molecules:
    1260              :                      ! the closest contacts should be found using
    1261              :                      ! the element specific radii
    1262              : 
    1263              :                      ! compute inner and outer cutoff radii
    1264         3139 :                      r0 = almo_scf_env%quencher_r0_factor*(contact1_radius + contact2_radius)
    1265              :                      !+almo_scf_env%quencher_r0_shift
    1266         3139 :                      r1 = almo_scf_env%quencher_r1_factor*(contact1_radius + contact2_radius)
    1267              :                      !+almo_scf_env%quencher_r1_shift
    1268              : 
    1269         3139 :                      IF (r0 < 0.0_dp) THEN
    1270            0 :                         CPABORT("R0 is less than zero")
    1271              :                      END IF
    1272         3139 :                      IF (r1 <= 0.0_dp) THEN
    1273            0 :                         CPABORT("R1 is less than or equal to zero")
    1274              :                      END IF
    1275         3139 :                      IF (r0 > r1) THEN
    1276            0 :                         CPABORT("R0 is greater than or equal to R1")
    1277              :                      END IF
    1278              : 
    1279              :                      ! Fill in non-zero blocks if AOs are close to the electron center
    1280         3139 :                      IF (distance < r1) THEN
    1281         8740 :                         ALLOCATE (new_block(row_blk_size(iblock_row), col_blk_size(iblock_col)))
    1282         2185 :                         IF (distance <= r0) THEN
    1283       101919 :                            new_block(:, :) = 1.0_dp
    1284              :                         ELSE
    1285              :                            ! remove the intermediate values from the quencher temporarily
    1286            0 :                            CPABORT("distance > r0 not yet validated") ! Unexplained in https://github.com/cp2k/cp2k/pull/5345
    1287            0 :                            new_block(:, :) = 1.0_dp/(1.0_dp + EXP((r1 - r0)/(r0 - distance) + (r1 - r0)/(r1 - distance)))
    1288              :                         END IF
    1289              : 
    1290         2185 :                         IF (ABS(new_block(1, 1)) > ABS(almo_scf_env%eps_filter)) THEN
    1291         2185 :                            IF (domain_map_local_entries >= max_domain_neighbors*almo_scf_env%ndomains) THEN
    1292            0 :                               CPABORT("weird... max_domain_neighbors is exceeded")
    1293              :                            END IF
    1294         2185 :                            almo_scf_env%domain_map(ispin)%pairs(domain_map_local_entries + 1, 1) = iblock_row
    1295         2185 :                            almo_scf_env%domain_map(ispin)%pairs(domain_map_local_entries + 1, 2) = iblock_col
    1296         2185 :                            domain_map_local_entries = domain_map_local_entries + 1
    1297              :                         END IF
    1298              : 
    1299         2185 :                         CALL dbcsr_put_block(almo_scf_env%quench_t(ispin), iblock_row, iblock_col, new_block)
    1300         2185 :                         DEALLOCATE (new_block)
    1301              :                      END IF
    1302              : 
    1303              :                   CASE DEFAULT
    1304         3139 :                      CPABORT("Illegal constraint type")
    1305              :                   END SELECT
    1306              : 
    1307              :                END IF ! mynode
    1308              : 
    1309              :             END DO
    1310              :          END DO ! end O(N) loop over pairs
    1311              : 
    1312          128 :          DEALLOCATE (domain_neighbor_list)
    1313          128 :          DEALLOCATE (current_number_neighbors)
    1314              : 
    1315          128 :          CALL dbcsr_finalize(almo_scf_env%quench_t(ispin))
    1316              : 
    1317              :          CALL dbcsr_filter(almo_scf_env%quench_t(ispin), &
    1318          128 :                            almo_scf_env%eps_filter)
    1319              : 
    1320              :          ! check that both domain_map and quench_t have the same number of entries
    1321          128 :          nblks = dbcsr_get_num_blocks(almo_scf_env%quench_t(ispin))
    1322          128 :          IF (nblks /= domain_map_local_entries) THEN
    1323            0 :             CPABORT("number of blocks is wrong")
    1324              :          END IF
    1325              : 
    1326              :          ! first, communicate map sizes on the other nodes
    1327          384 :          ALLOCATE (domain_entries_cpu(nNodes), offset_for_cpu(nNodes))
    1328          128 :          CALL group%allgather(2*domain_map_local_entries, domain_entries_cpu)
    1329              : 
    1330              :          ! second, create
    1331          128 :          offset_for_cpu(1) = 0
    1332          256 :          DO iNode = 2, nNodes
    1333              :             offset_for_cpu(iNode) = offset_for_cpu(iNode - 1) + &
    1334          256 :                                     domain_entries_cpu(iNode - 1)
    1335              :          END DO
    1336          128 :          global_entries = offset_for_cpu(nNodes) + domain_entries_cpu(nNodes)
    1337              : 
    1338              :          ! communicate all entries
    1339          384 :          ALLOCATE (domain_map_global(global_entries))
    1340          508 :          ALLOCATE (domain_map_local(2*domain_map_local_entries))
    1341         2313 :          DO ientry = 1, domain_map_local_entries
    1342         2185 :             domain_map_local(2*(ientry - 1) + 1) = almo_scf_env%domain_map(ispin)%pairs(ientry, 1)
    1343         2313 :             domain_map_local(2*ientry) = almo_scf_env%domain_map(ispin)%pairs(ientry, 2)
    1344              :          END DO
    1345              :          CALL group%allgatherv(domain_map_local, domain_map_global, &
    1346          128 :                                domain_entries_cpu, offset_for_cpu)
    1347          128 :          DEALLOCATE (domain_entries_cpu, offset_for_cpu)
    1348          128 :          DEALLOCATE (domain_map_local)
    1349              : 
    1350          128 :          DEALLOCATE (almo_scf_env%domain_map(ispin)%index1)
    1351          128 :          DEALLOCATE (almo_scf_env%domain_map(ispin)%pairs)
    1352          256 :          ALLOCATE (almo_scf_env%domain_map(ispin)%index1(ndomains))
    1353          512 :          ALLOCATE (almo_scf_env%domain_map(ispin)%pairs(global_entries/2, 2))
    1354         9124 :          almo_scf_env%domain_map(ispin)%pairs(:, :) = 0
    1355          962 :          almo_scf_env%domain_map(ispin)%index1(:) = 0
    1356              : 
    1357              :          ! unpack the received data into a local variable
    1358              :          ! since we do not know the maximum global number of neighbors
    1359              :          ! try one. if fails increase the maximum number and try again
    1360              :          ! until it succeeds
    1361              :          max_neig = max_domain_neighbors
    1362              :          max_neig_fails = .TRUE.
    1363          256 :          max_neig_loop: DO WHILE (max_neig_fails)
    1364          512 :             ALLOCATE (domain_grid(almo_scf_env%ndomains, 0:max_neig))
    1365          128 :             domain_grid(:, :) = 0
    1366              :             ! init the number of collected neighbors
    1367          962 :             domain_grid(:, 0) = 1
    1368              :             ! loop over the records
    1369          128 :             global_entries = global_entries/2
    1370         4498 :             DO ientry = 1, global_entries
    1371              :                ! get the center
    1372         4370 :                grid1 = domain_map_global(2*ientry)
    1373              :                ! get the neighbor
    1374         4370 :                ineig = domain_map_global(2*(ientry - 1) + 1)
    1375              :                ! check boundaries
    1376         4370 :                IF (domain_grid(grid1, 0) > max_neig) THEN
    1377              :                   ! this neighbor will overstep the boundaries
    1378              :                   ! stop the trial and increase the max number of neighbors
    1379            0 :                   DEALLOCATE (domain_grid)
    1380            0 :                   max_neig = max_neig*2
    1381            0 :                   CYCLE max_neig_loop
    1382              :                END IF
    1383              :                ! for the current center loop over the collected neighbors
    1384              :                ! to insert the current record in a numerical order
    1385              :                delayed_increment = .FALSE.
    1386        19016 :                DO igrid = 1, domain_grid(grid1, 0)
    1387              :                   ! compare the current neighbor with that already in the 'book'
    1388        19016 :                   IF (ineig < domain_grid(grid1, igrid)) THEN
    1389              :                      ! if this one is smaller then insert it here and pick up the one
    1390              :                      ! from the book to continue inserting
    1391         3180 :                      neig_temp = ineig
    1392         3180 :                      ineig = domain_grid(grid1, igrid)
    1393         3180 :                      domain_grid(grid1, igrid) = neig_temp
    1394              :                   ELSE
    1395        11466 :                      IF (domain_grid(grid1, igrid) == 0) THEN
    1396              :                         ! got the empty slot now - insert the record
    1397         4370 :                         domain_grid(grid1, igrid) = ineig
    1398              :                         ! increase the record counter but do it outside the loop
    1399         4370 :                         delayed_increment = .TRUE.
    1400              :                      END IF
    1401              :                   END IF
    1402              :                END DO
    1403         4498 :                IF (delayed_increment) THEN
    1404         4370 :                   domain_grid(grid1, 0) = domain_grid(grid1, 0) + 1
    1405              :                ELSE
    1406              :                   ! should not be here - all records must be inserted
    1407            0 :                   CPABORT("all records must be inserted")
    1408              :                END IF
    1409              :             END DO
    1410          128 :             max_neig_fails = .FALSE.
    1411              :          END DO max_neig_loop
    1412          128 :          DEALLOCATE (domain_map_global)
    1413              : 
    1414          128 :          ientry = 1
    1415          962 :          DO idomain = 1, almo_scf_env%ndomains
    1416         5204 :             DO ineig = 1, domain_grid(idomain, 0) - 1
    1417         4370 :                almo_scf_env%domain_map(ispin)%pairs(ientry, 1) = domain_grid(idomain, ineig)
    1418         4370 :                almo_scf_env%domain_map(ispin)%pairs(ientry, 2) = idomain
    1419         5204 :                ientry = ientry + 1
    1420              :             END DO
    1421          962 :             almo_scf_env%domain_map(ispin)%index1(idomain) = ientry
    1422              :          END DO
    1423          378 :          DEALLOCATE (domain_grid)
    1424              : 
    1425              :       END DO ! ispin
    1426          122 :       IF (almo_scf_env%nspins == 2) THEN
    1427              :          CALL dbcsr_copy(almo_scf_env%quench_t(2), &
    1428            6 :                          almo_scf_env%quench_t(1))
    1429              :          almo_scf_env%domain_map(2)%pairs(:, :) = &
    1430           50 :             almo_scf_env%domain_map(1)%pairs(:, :)
    1431              :          almo_scf_env%domain_map(2)%index1(:) = &
    1432           18 :             almo_scf_env%domain_map(1)%index1(:)
    1433              :       END IF
    1434              : 
    1435          122 :       CALL dbcsr_release(matrix_s_sym)
    1436              : 
    1437          122 :       IF (almo_scf_env%domain_layout_mos == almo_domain_layout_molecular .OR. &
    1438              :           almo_scf_env%domain_layout_aos == almo_domain_layout_molecular) THEN
    1439          122 :          DEALLOCATE (first_atom_of_molecule)
    1440          122 :          DEALLOCATE (last_atom_of_molecule)
    1441              :       END IF
    1442              : 
    1443          122 :       CALL timestop(handle)
    1444              : 
    1445          244 :    END SUBROUTINE almo_scf_construct_quencher
    1446              : 
    1447              : ! *****************************************************************************
    1448              : !> \brief Compute matrix W (energy-weighted density matrix) that is needed
    1449              : !>        for the evaluation of forces
    1450              : !> \param matrix_w ...
    1451              : !> \param almo_scf_env ...
    1452              : !> \par History
    1453              : !>       2015.03 created [Rustam Z. Khaliullin]
    1454              : !> \author Rustam Z. Khaliullin
    1455              : ! **************************************************************************************************
    1456           66 :    SUBROUTINE calculate_w_matrix_almo(matrix_w, almo_scf_env)
    1457              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_w
    1458              :       TYPE(almo_scf_env_type)                            :: almo_scf_env
    1459              : 
    1460              :       CHARACTER(len=*), PARAMETER :: routineN = 'calculate_w_matrix_almo'
    1461              : 
    1462              :       INTEGER                                            :: handle, ispin
    1463              :       REAL(KIND=dp)                                      :: scaling
    1464              :       TYPE(dbcsr_type)                                   :: tmp_nn1, tmp_no1, tmp_oo1, tmp_oo2
    1465              : 
    1466           66 :       CALL timeset(routineN, handle)
    1467              : 
    1468           66 :       IF (almo_scf_env%nspins == 1) THEN
    1469           66 :          scaling = 2.0_dp
    1470              :       ELSE
    1471            0 :          scaling = 1.0_dp
    1472              :       END IF
    1473              : 
    1474          132 :       DO ispin = 1, almo_scf_env%nspins
    1475              : 
    1476              :          CALL dbcsr_create(tmp_nn1, template=almo_scf_env%matrix_s(1), &
    1477           66 :                            matrix_type=dbcsr_type_no_symmetry)
    1478              :          CALL dbcsr_create(tmp_no1, template=almo_scf_env%matrix_t(ispin), &
    1479           66 :                            matrix_type=dbcsr_type_no_symmetry)
    1480              :          CALL dbcsr_create(tmp_oo1, template=almo_scf_env%matrix_sigma_inv(ispin), &
    1481           66 :                            matrix_type=dbcsr_type_no_symmetry)
    1482              :          CALL dbcsr_create(tmp_oo2, template=almo_scf_env%matrix_sigma_inv(ispin), &
    1483           66 :                            matrix_type=dbcsr_type_no_symmetry)
    1484              : 
    1485           66 :          CALL dbcsr_copy(tmp_nn1, almo_scf_env%matrix_ks(ispin))
    1486              :          ! 1. TMP_NO1=F.T
    1487              :          CALL dbcsr_multiply("N", "N", scaling, tmp_nn1, almo_scf_env%matrix_t(ispin), &
    1488           66 :                              0.0_dp, tmp_no1, filter_eps=almo_scf_env%eps_filter)
    1489              :          ! 2. TMP_OO1=T^(tr).TMP_NO1=T^(tr).(FT)
    1490              :          CALL dbcsr_multiply("T", "N", 1.0_dp, almo_scf_env%matrix_t(ispin), tmp_no1, &
    1491           66 :                              0.0_dp, tmp_oo1, filter_eps=almo_scf_env%eps_filter)
    1492              :          ! 3. TMP_OO2=TMP_OO1.siginv=(T^(tr)FT).siginv
    1493              :          CALL dbcsr_multiply("N", "N", 1.0_dp, tmp_oo1, almo_scf_env%matrix_sigma_inv(ispin), &
    1494           66 :                              0.0_dp, tmp_oo2, filter_eps=almo_scf_env%eps_filter)
    1495              :          ! 4. TMP_OO1=siginv.TMP_OO2=siginv.(T^(tr)FTsiginv)
    1496              :          CALL dbcsr_multiply("N", "N", 1.0_dp, almo_scf_env%matrix_sigma_inv(ispin), tmp_oo2, &
    1497           66 :                              0.0_dp, tmp_oo1, filter_eps=almo_scf_env%eps_filter)
    1498              :          ! 5. TMP_NO1=T.TMP_OO1.=T.(siginvT^(tr)FTsiginv)
    1499              :          CALL dbcsr_multiply("N", "N", 1.0_dp, almo_scf_env%matrix_t(ispin), tmp_oo1, &
    1500           66 :                              0.0_dp, tmp_no1, filter_eps=almo_scf_env%eps_filter)
    1501              :          ! 6. TMP_NN1=TMP_NO1.T^(tr)=(TsiginvT^(tr)FTsiginv).T^(tr)=RFR
    1502              :          CALL dbcsr_multiply("N", "T", 1.0_dp, tmp_no1, almo_scf_env%matrix_t(ispin), &
    1503           66 :                              0.0_dp, tmp_nn1, filter_eps=almo_scf_env%eps_filter)
    1504           66 :          CALL matrix_almo_to_qs(tmp_nn1, matrix_w(ispin)%matrix, almo_scf_env%mat_distr_aos)
    1505              : 
    1506           66 :          CALL dbcsr_release(tmp_nn1)
    1507           66 :          CALL dbcsr_release(tmp_no1)
    1508           66 :          CALL dbcsr_release(tmp_oo1)
    1509          132 :          CALL dbcsr_release(tmp_oo2)
    1510              : 
    1511              :       END DO
    1512              : 
    1513           66 :       CALL timestop(handle)
    1514              : 
    1515           66 :    END SUBROUTINE calculate_w_matrix_almo
    1516              : 
    1517              : END MODULE almo_scf_qs
    1518              : 
        

Generated by: LCOV version 2.0-1