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

            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 3-center integrals machinery for the XAS_TDP method
      10              : !> \author A. Bussy (03.2020)
      11              : ! **************************************************************************************************
      12              : MODULE xas_tdp_integrals
      13              :    USE r12_fundamentals, ONLY: r12_fundamentals_init
      14              :    USE coulomb_integral_interface, ONLY: coulomb_integral_context_type, coulomb_integral_init, &
      15              :                                          coulomb_integral_cleanup, compute_coulomb_2c, compute_coulomb_3c, &
      16              :                                          compute_coulomb_2c_derivs, compute_coulomb_3c_derivs
      17              :    USE integral_library_types, ONLY: library_libint
      18              :    USE OMP_LIB, ONLY: omp_get_max_threads, &
      19              :                       omp_get_num_threads, &
      20              :                       omp_get_thread_num
      21              :    USE ai_contraction_sphi, ONLY: ab_contract, &
      22              :                                   abc_contract_xsmm
      23              :    USE atomic_kind_types, ONLY: atomic_kind_type
      24              :    USE basis_set_types, ONLY: get_gto_basis_set, &
      25              :                               gto_basis_set_p_type, &
      26              :                               gto_basis_set_type
      27              :    USE cell_types, ONLY: cell_type
      28              :    USE constants_operator, ONLY: operator_coulomb
      29              :    USE cp_array_utils, ONLY: cp_1d_i_p_type, &
      30              :                              cp_2d_r_p_type
      31              :    USE cp_blacs_env, ONLY: cp_blacs_env_type
      32              :    USE cp_dbcsr_api, ONLY: dbcsr_distribution_get, &
      33              :                            dbcsr_distribution_release, &
      34              :                            dbcsr_distribution_type
      35              :    USE cp_dbcsr_operations, ONLY: cp_dbcsr_dist2d_to_dist
      36              :    USE cp_eri_mme_interface, ONLY: cp_eri_mme_param, &
      37              :                                    cp_eri_mme_set_params
      38              :    USE cp_files, ONLY: close_file, &
      39              :                        open_file
      40              :    USE dbt_api, ONLY: &
      41              :       dbt_create, dbt_distribution_destroy, dbt_distribution_new, dbt_distribution_type, &
      42              :       dbt_finalize, dbt_pgrid_create, dbt_pgrid_destroy, dbt_pgrid_type, dbt_put_block, &
      43              :       dbt_reserve_blocks, dbt_type
      44              :    USE distribution_1d_types, ONLY: distribution_1d_type
      45              :    USE distribution_2d_types, ONLY: distribution_2d_create, &
      46              :                                     distribution_2d_type
      47              :    USE eri_mme_integrate, ONLY: eri_mme_2c_integrate
      48              :    USE eri_mme_types, ONLY: eri_mme_init, &
      49              :                             eri_mme_release
      50              :    USE generic_os_integrals, ONLY: int_operators_r12_ab_os
      51              :    USE input_constants, ONLY: do_potential_coulomb, &
      52              :                               do_potential_id, &
      53              :                               do_potential_short, &
      54              :                               do_potential_truncated
      55              :    USE input_section_types, ONLY: section_vals_val_get
      56              :    USE kinds, ONLY: dp
      57              :    USE integral_library_types, ONLY: cutoff_screen_factor, &
      58              :                                      coulomb_operator_type
      59              :    USE mathlib, ONLY: invmat_symm
      60              :    USE message_passing, ONLY: mp_comm_type, &
      61              :                               mp_para_env_type
      62              :    USE molecule_types, ONLY: molecule_type
      63              :    USE orbital_pointers, ONLY: ncoset
      64              :    USE particle_methods, ONLY: get_particle_set
      65              :    USE particle_types, ONLY: particle_type
      66              :    USE qs_environment_types, ONLY: get_qs_env, &
      67              :                                    qs_environment_type
      68              :    USE qs_integral_utils, ONLY: basis_set_list_setup
      69              :    USE qs_kind_types, ONLY: get_qs_kind, &
      70              :                             qs_kind_type
      71              :    USE qs_neighbor_list_types, ONLY: &
      72              :       get_iterator_info, get_neighbor_list_set_p, neighbor_list_iterate, &
      73              :       neighbor_list_iterator_create, neighbor_list_iterator_p_type, &
      74              :       neighbor_list_iterator_release, neighbor_list_set_p_type, nl_set_sub_iterator, &
      75              :       nl_sub_iterate, release_neighbor_list_sets
      76              :    USE qs_neighbor_lists, ONLY: atom2d_build, &
      77              :                                 atom2d_cleanup, &
      78              :                                 build_neighbor_lists, &
      79              :                                 local_atoms_type, &
      80              :                                 pair_radius_setup
      81              :    USE qs_o3c_types, ONLY: get_o3c_iterator_info, &
      82              :                            init_o3c_container, &
      83              :                            o3c_container_type, &
      84              :                            o3c_iterate, &
      85              :                            o3c_iterator_create, &
      86              :                            o3c_iterator_release, &
      87              :                            o3c_iterator_type, &
      88              :                            release_o3c_container
      89              :    USE xas_tdp_types, ONLY: xas_tdp_control_type, &
      90              :                             xas_tdp_env_type
      91              : #if defined(__LIBXS)
      92              :    USE ISO_C_BINDING, ONLY: C_LOC
      93              :    USE libxs, ONLY: libxs_matcopy, &
      94              :                     libxs_otrans
      95              : #endif
      96              : #include "./base/base_uses.f90"
      97              : 
      98              :    IMPLICIT NONE
      99              :    PRIVATE
     100              : 
     101              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'xas_tdp_integrals'
     102              : 
     103              :    PUBLIC :: create_pqX_tensor, fill_pqX_tensor, compute_ri_3c_coulomb, compute_ri_3c_exchange, &
     104              :              build_xas_tdp_3c_nl, build_xas_tdp_ovlp_nl, get_opt_3c_dist2d, &
     105              :              compute_ri_coulomb2_int, compute_ri_exchange2_int
     106              : 
     107              : CONTAINS
     108              : 
     109              : ! **************************************************************************************************
     110              : !> \brief Prepares a tensor to hold 3-center integrals (pq|X), where p,q are distributed according
     111              : !>        to the given 2d dbcsr distribution of the given . The third dimension of the tensor is
     112              : !>        iteslf not distributed (i.e. the t_pgrid's third dimension has size 1). The blocks are
     113              : !>        reserved according to the neighbor lists
     114              : !> \param pq_X the tensor to store the integrals
     115              : !> \param ab_nl the 1st and 2nd center neighbor list
     116              : !> \param ac_nl the 1st and 3rd center neighbor list
     117              : !> \param matrix_dist ...
     118              : !> \param blk_size_1 the block size in the first dimension
     119              : !> \param blk_size_2 the block size in the second dimension
     120              : !> \param blk_size_3 the block size in the third dimension
     121              : !> \param only_bc_same_center only keep block if, for the corresponding integral (ab|c), b and c
     122              : !>        share the same center, i.e. r_bc = 0.0
     123              : ! **************************************************************************************************
     124         2024 :    SUBROUTINE create_pqX_tensor(pq_X, ab_nl, ac_nl, matrix_dist, blk_size_1, blk_size_2, &
     125          184 :                                 blk_size_3, only_bc_same_center)
     126              : 
     127              :       TYPE(dbt_type), INTENT(OUT)                        :: pq_X
     128              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     129              :          POINTER                                         :: ab_nl, ac_nl
     130              :       TYPE(dbcsr_distribution_type), INTENT(IN)          :: matrix_dist
     131              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: blk_size_1, blk_size_2, blk_size_3
     132              :       LOGICAL, INTENT(IN), OPTIONAL                      :: only_bc_same_center
     133              : 
     134              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'create_pqX_tensor'
     135              : 
     136              :       INTEGER                                            :: A, b, group_handle, handle, i, iatom, &
     137              :                                                             ikind, jatom, katom, kkind, nblk, &
     138              :                                                             nblk_3, nblk_per_thread, nkind
     139          184 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: idx1, idx2, idx3
     140              :       INTEGER, DIMENSION(3)                              :: pdims
     141          184 :       INTEGER, DIMENSION(:), POINTER                     :: col_dist, row_dist
     142          184 :       INTEGER, DIMENSION(:, :), POINTER                  :: mat_pgrid
     143              :       LOGICAL                                            :: my_sort_bc, symmetric
     144              :       REAL(dp), DIMENSION(3)                             :: rab, rac, rbc
     145         1656 :       TYPE(dbt_distribution_type)                        :: t_dist
     146          552 :       TYPE(dbt_pgrid_type)                               :: t_pgrid
     147              :       TYPE(mp_comm_type)                                 :: group
     148              :       TYPE(neighbor_list_iterator_p_type), &
     149          184 :          DIMENSION(:), POINTER                           :: ab_iter, ac_iter
     150              : 
     151          184 :       NULLIFY (ab_iter, ac_iter, col_dist, row_dist, mat_pgrid)
     152              : 
     153          184 :       CALL timeset(routineN, handle)
     154              : 
     155          184 :       my_sort_bc = .FALSE.
     156          184 :       IF (PRESENT(only_bc_same_center)) my_sort_bc = only_bc_same_center
     157              : 
     158          184 :       CALL get_neighbor_list_set_p(ab_nl, symmetric=symmetric)
     159          184 :       CPASSERT(symmetric)
     160              : 
     161              :       !get 2D distribution info from matrix_dist
     162              :       CALL dbcsr_distribution_get(matrix_dist, pgrid=mat_pgrid, group=group_handle, &
     163          184 :                                   row_dist=row_dist, col_dist=col_dist)
     164          184 :       CALL group%set_handle(group_handle)
     165              : 
     166              :       !create the corresponding tensor proc grid and dist
     167          184 :       pdims(1) = SIZE(mat_pgrid, 1); pdims(2) = SIZE(mat_pgrid, 2); pdims(3) = 1
     168          184 :       CALL dbt_pgrid_create(group, pdims, t_pgrid)
     169              : 
     170          184 :       nblk_3 = SIZE(blk_size_3)
     171              :       CALL dbt_distribution_new(t_dist, t_pgrid, nd_dist_1=row_dist, nd_dist_2=col_dist, &
     172         2964 :                                 nd_dist_3=[(0, i=1, nblk_3)])
     173              : 
     174              :       !create the tensor itself.
     175              :       CALL dbt_create(pq_X, name="(pq|X)", dist=t_dist, map1_2d=[1, 2], map2_2d=[3], &
     176          184 :                       blk_size_1=blk_size_1, blk_size_2=blk_size_2, blk_size_3=blk_size_3)
     177              : 
     178              :       !count the blocks to reserve !note: dbcsr takes care of only keeping unique indices
     179          184 :       CALL neighbor_list_iterator_create(ab_iter, ab_nl)
     180          184 :       CALL neighbor_list_iterator_create(ac_iter, ac_nl, search=.TRUE.)
     181          184 :       nblk = 0
     182        17538 :       DO WHILE (neighbor_list_iterate(ab_iter) == 0)
     183        17354 :          CALL get_iterator_info(ab_iter, ikind=ikind, iatom=iatom, nkind=nkind, r=rab)
     184              : 
     185        51004 :          DO kkind = 1, nkind
     186        33466 :             CALL nl_set_sub_iterator(ac_iter, ikind, kkind, iatom)
     187              : 
     188        72362 :             DO WHILE (nl_sub_iterate(ac_iter) == 0)
     189              : 
     190        21542 :                IF (my_sort_bc) THEN
     191              :                   !we check for rbc or rac because of symmetry in ab_nl
     192          724 :                   CALL get_iterator_info(ac_iter, r=rac)
     193         2896 :                   rbc(:) = rac(:) - rab(:)
     194         3196 :                   IF (.NOT. (ALL(ABS(rbc) <= 1.0E-8_dp) .OR. ALL(ABS(rac) <= 1.0E-8_dp))) CYCLE
     195              : 
     196              :                END IF
     197              : 
     198        21542 :                nblk = nblk + 1
     199              :             END DO !ac_iter
     200              :          END DO !kkind
     201              :       END DO !ab_iter
     202          184 :       CALL neighbor_list_iterator_release(ab_iter)
     203          184 :       CALL neighbor_list_iterator_release(ac_iter)
     204              : 
     205          833 :       ALLOCATE (idx1(nblk), idx2(nblk), idx3(nblk))
     206              : 
     207              :       !actually reserve the blocks
     208          184 :       CALL neighbor_list_iterator_create(ab_iter, ab_nl)
     209          184 :       CALL neighbor_list_iterator_create(ac_iter, ac_nl, search=.TRUE.)
     210          184 :       nblk = 0
     211        17538 :       DO WHILE (neighbor_list_iterate(ab_iter) == 0)
     212        17354 :          CALL get_iterator_info(ab_iter, ikind=ikind, iatom=iatom, jatom=jatom, nkind=nkind, r=rab)
     213              : 
     214        51004 :          DO kkind = 1, nkind
     215        33466 :             CALL nl_set_sub_iterator(ac_iter, ikind, kkind, iatom)
     216              : 
     217        72362 :             DO WHILE (nl_sub_iterate(ac_iter) == 0)
     218        21542 :                CALL get_iterator_info(ac_iter, jatom=katom, r=rac)
     219              : 
     220        21542 :                IF (my_sort_bc) THEN
     221              :                   !we check for rbc or rac because of symmetry in ab_nl
     222          724 :                   CALL get_iterator_info(ac_iter, r=rac)
     223         2896 :                   rbc(:) = rac(:) - rab(:)
     224         3196 :                   IF (.NOT. (ALL(ABS(rbc) <= 1.0E-8_dp) .OR. ALL(ABS(rac) <= 1.0E-8_dp))) CYCLE
     225              : 
     226              :                END IF
     227              : 
     228        21248 :                nblk = nblk + 1
     229              : 
     230        21248 :                idx1(nblk) = iatom
     231        21248 :                idx2(nblk) = jatom
     232        21542 :                idx3(nblk) = katom
     233              : 
     234              :             END DO !ac_iter
     235              :          END DO !kkind
     236              :       END DO !ab_iter
     237          184 :       CALL neighbor_list_iterator_release(ab_iter)
     238          184 :       CALL neighbor_list_iterator_release(ac_iter)
     239              : 
     240              : !TODO: Parallelize creation of block list.
     241          184 : !$OMP PARALLEL DEFAULT(NONE) SHARED(pq_X, nblk, idx1, idx2, idx3) PRIVATE(nblk_per_thread,A,b)
     242              :       nblk_per_thread = nblk/omp_get_num_threads() + 1
     243              :       a = omp_get_thread_num()*nblk_per_thread + 1
     244              :       b = MIN(a + nblk_per_thread, nblk)
     245              :       CALL dbt_reserve_blocks(pq_X, idx1(a:b), idx2(a:b), idx3(a:b))
     246              : !$OMP END PARALLEL
     247          184 :       CALL dbt_finalize(pq_X)
     248              : 
     249              :       !clean-up
     250          184 :       CALL dbt_distribution_destroy(t_dist)
     251          184 :       CALL dbt_pgrid_destroy(t_pgrid)
     252              : 
     253          184 :       CALL timestop(handle)
     254              : 
     255          920 :    END SUBROUTINE create_pqX_tensor
     256              : 
     257              : ! **************************************************************************************************
     258              : !> \brief Fills the given 3 dimensional (pq|X) tensor with integrals
     259              : !> \param pq_X the tensor to fill
     260              : !> \param ab_nl the neighbor list for the first 2 centers
     261              : !> \param ac_nl the neighbor list for the first and third centers
     262              : !> \param basis_set_list_a basis sets for first center
     263              : !> \param basis_set_list_b basis sets for second center
     264              : !> \param basis_set_list_c basis sets for third center
     265              : !> \param potential_parameter the operator for the integrals
     266              : !> \param qs_env ...
     267              : !> \param only_bc_same_center same as in create_pqX_tensor
     268              : !> \param eps_screen threshold for possible screening
     269              : !> \note The following indices are happily mixed within this routine: First center i,a,p
     270              : !>       Second center: j,b,q       Third center: k,c,X
     271              : ! **************************************************************************************************
     272          184 :    SUBROUTINE fill_pqX_tensor(pq_X, ab_nl, ac_nl, basis_set_list_a, basis_set_list_b, &
     273              :                               basis_set_list_c, potential_parameter, qs_env, &
     274              :                               only_bc_same_center, eps_screen)
     275              : 
     276              :       TYPE(dbt_type)                                     :: pq_X
     277              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     278              :          POINTER                                         :: ab_nl, ac_nl
     279              :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_list_a, basis_set_list_b, &
     280              :                                                             basis_set_list_c
     281              :       TYPE(coulomb_operator_type)                        :: potential_parameter
     282              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     283              :       LOGICAL, INTENT(IN), OPTIONAL                      :: only_bc_same_center
     284              :       REAL(dp), INTENT(IN), OPTIONAL                     :: eps_screen
     285              : 
     286              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'fill_pqX_tensor'
     287              : 
     288              :       INTEGER :: egfa, egfb, egfc, handle, i, iatom, ibasis, ikind, ilist, imax, iset, jatom, &
     289              :          jkind, jset, katom, kkind, kset, m_max, max_nset, maxli, maxlj, maxlk, mepos, nbasis, &
     290              :          ncoa, ncob, ncoc, ni, nj, nk, nseta, nsetb, nsetc, nthread, sgfa, sgfb, sgfc
     291          184 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, lb_max, lb_min, lc_max, &
     292          184 :                                                             lc_min, npgfa, npgfb, npgfc, nsgfa, &
     293          184 :                                                             nsgfb, nsgfc
     294          184 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa, first_sgfb, first_sgfc
     295              :       LOGICAL                                            :: do_screen, my_sort_bc
     296              :       REAL(dp)                                           :: dij, dik, djk, my_eps_screen, ri(3), &
     297              :                                                             rij(3), rik(3), rj(3), rjk(3), rk(3), &
     298              :                                                             sabc_ext, screen_radius
     299          184 :       REAL(dp), ALLOCATABLE, DIMENSION(:)                :: ccp_buffer, cpp_buffer
     300          184 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: max_contr, max_contra, max_contrb, &
     301          184 :                                                             max_contrc
     302          184 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :, :)          :: iabc, sabc, work
     303          184 :       REAL(dp), DIMENSION(:), POINTER                    :: set_radius_a, set_radius_b, set_radius_c
     304          184 :       REAL(dp), DIMENSION(:, :), POINTER                 :: rpgf_a, rpgf_b, rpgf_c, zeta, zetb, zetc
     305          184 :       TYPE(coulomb_integral_context_type)                :: integral_context
     306          184 :       TYPE(cp_2d_r_p_type), DIMENSION(:, :), POINTER     :: spb, spc, tspa
     307          184 :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_list
     308              :       TYPE(gto_basis_set_type), POINTER                  :: basis_set, basis_set_a, basis_set_b, &
     309              :                                                             basis_set_c
     310              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     311              :       TYPE(o3c_container_type), POINTER                  :: o3c
     312              :       TYPE(o3c_iterator_type)                            :: o3c_iterator
     313              : 
     314          184 :       NULLIFY (basis_set, basis_set_list, para_env, la_max, la_min)
     315          184 :       NULLIFY (lb_max, lb_min, lc_max, lc_min, npgfa, npgfb, npgfc, nsgfa, nsgfb, nsgfc)
     316          184 :       NULLIFY (first_sgfa, first_sgfb, first_sgfc, set_radius_a, set_radius_b, set_radius_c)
     317          184 :       NULLIFY (rpgf_a, rpgf_b, rpgf_c, zeta, zetb, zetc)
     318          184 :       NULLIFY (basis_set_a, basis_set_b, basis_set_c, tspa, spb, spc)
     319              : 
     320          184 :       CALL timeset(routineN, handle)
     321              : 
     322              :       !Need the max l for each basis for libint (and overall max #of sets for screening)
     323          184 :       nbasis = SIZE(basis_set_list_a)
     324          184 :       max_nset = 0
     325          184 :       maxli = 0
     326          492 :       DO ibasis = 1, nbasis
     327              :          CALL get_gto_basis_set(gto_basis_set=basis_set_list_a(ibasis)%gto_basis_set, &
     328          308 :                                 maxl=imax, nset=iset, nsgf_set=nsgfa)
     329          308 :          maxli = MAX(maxli, imax)
     330          492 :          max_nset = MAX(max_nset, iset)
     331              :       END DO
     332              :       maxlj = 0
     333          492 :       DO ibasis = 1, nbasis
     334              :          CALL get_gto_basis_set(gto_basis_set=basis_set_list_b(ibasis)%gto_basis_set, &
     335          308 :                                 maxl=imax, nset=iset, nsgf_set=nsgfb, npgf=npgfb)
     336          308 :          maxlj = MAX(maxlj, imax)
     337          492 :          max_nset = MAX(max_nset, iset)
     338              :       END DO
     339              :       maxlk = 0
     340          492 :       DO ibasis = 1, nbasis
     341              :          CALL get_gto_basis_set(gto_basis_set=basis_set_list_c(ibasis)%gto_basis_set, &
     342          308 :                                 maxl=imax, nset=iset, npgf=npgfc)
     343          308 :          maxlk = MAX(maxlk, imax)
     344          492 :          max_nset = MAX(max_nset, iset)
     345              :       END DO
     346          184 :       m_max = maxli + maxlj + maxlk
     347              : 
     348              :       !Screening
     349          184 :       do_screen = .FALSE.
     350          184 :       IF (PRESENT(eps_screen)) THEN
     351           54 :          do_screen = .TRUE.
     352           54 :          my_eps_screen = eps_screen
     353              :       END IF
     354          184 :       screen_radius = 0.0_dp
     355          184 :       IF (potential_parameter%potential_type == do_potential_truncated .OR. &
     356              :           potential_parameter%potential_type == do_potential_short) THEN
     357              : 
     358           12 :          screen_radius = potential_parameter%cutoff_radius*cutoff_screen_factor
     359          172 :       ELSE IF (potential_parameter%potential_type == do_potential_coulomb) THEN
     360              : 
     361          102 :          screen_radius = 1000000.0_dp
     362              :       END IF
     363              : 
     364              :       !get maximum contraction values for abc_contract screening
     365          184 :       IF (do_screen) THEN
     366              : 
     367              :          !Allocate max_contraction arrays such that we have a specific value for each set/kind
     368            0 :          ALLOCATE (max_contr(max_nset, nbasis), max_contra(max_nset, nbasis), &
     369          540 :                    max_contrb(max_nset, nbasis), max_contrc(max_nset, nbasis))
     370              : 
     371              :          !Not the most elegent, but better than copying 3 times the same
     372          216 :          DO ilist = 1, 3
     373              : 
     374          162 :             IF (ilist == 1) basis_set_list => basis_set_list_a
     375          162 :             IF (ilist == 2) basis_set_list => basis_set_list_b
     376          162 :             IF (ilist == 3) basis_set_list => basis_set_list_c
     377              : 
     378          162 :             max_contr = 0.0_dp
     379              : 
     380          438 :             DO ibasis = 1, nbasis
     381          276 :                basis_set => basis_set_list(ibasis)%gto_basis_set
     382              : 
     383         1326 :                DO iset = 1, basis_set%nset
     384              : 
     385          888 :                   ncoa = basis_set%npgf(iset)*ncoset(basis_set%lmax(iset))
     386          888 :                   sgfa = basis_set%first_sgf(1, iset)
     387          888 :                   egfa = sgfa + basis_set%nsgf_set(iset) - 1
     388              : 
     389              :                   max_contr(iset, ibasis) = &
     390       562602 :                      MAXVAL([(SUM(ABS(basis_set%sphi(1:ncoa, i))), i=sgfa, egfa)])
     391              : 
     392              :                END DO !iset
     393              :             END DO !ibasis
     394              : 
     395          700 :             IF (ilist == 1) max_contra(:, :) = max_contr(:, :)
     396          700 :             IF (ilist == 2) max_contrb(:, :) = max_contr(:, :)
     397          754 :             IF (ilist == 3) max_contrc(:, :) = max_contr(:, :)
     398              :          END DO !ilist
     399           54 :          DEALLOCATE (max_contr)
     400              :       END IF !do_screen
     401              : 
     402              :       !To minimize memory ops in contraction, we need to pre-allocate buffers, pre-tranpose sphi_a
     403              :       !and also trim sphi in general to have contiguous arrays
     404         6872 :       ALLOCATE (tspa(max_nset, nbasis), spb(max_nset, nbasis), spc(max_nset, nbasis))
     405          492 :       DO ibasis = 1, nbasis
     406         1984 :          DO iset = 1, max_nset
     407         1492 :             NULLIFY (tspa(iset, ibasis)%array)
     408         1492 :             NULLIFY (spb(iset, ibasis)%array)
     409         1800 :             NULLIFY (spc(iset, ibasis)%array)
     410              :          END DO
     411              :       END DO
     412              : 
     413          736 :       DO ilist = 1, 3
     414              : 
     415         1660 :          DO ibasis = 1, nbasis
     416          924 :             IF (ilist == 1) basis_set => basis_set_list_a(ibasis)%gto_basis_set
     417          924 :             IF (ilist == 2) basis_set => basis_set_list_b(ibasis)%gto_basis_set
     418          924 :             IF (ilist == 3) basis_set => basis_set_list_c(ibasis)%gto_basis_set
     419              : 
     420         4536 :             DO iset = 1, basis_set%nset
     421              : 
     422         3060 :                ncoa = basis_set%npgf(iset)*ncoset(basis_set%lmax(iset))
     423         3060 :                sgfa = basis_set%first_sgf(1, iset)
     424         3060 :                egfa = sgfa + basis_set%nsgf_set(iset) - 1
     425              : 
     426         3984 :                IF (ilist == 1) THEN
     427         4584 :                   ALLOCATE (tspa(iset, ibasis)%array(basis_set%nsgf_set(iset), ncoa))
     428              : #if defined(__LIBXS)
     429              :                   CALL libxs_otrans(C_LOC(tspa(iset, ibasis)%array), &
     430              :                                     C_LOC(basis_set%sphi(1, sgfa)), &
     431              :                                     8, ncoa, egfa - sgfa + 1, &
     432              :                                     SIZE(basis_set%sphi, 1), &
     433         1146 :                                     basis_set%nsgf_set(iset))
     434              : #else
     435              :                   tspa(iset, ibasis)%array(:, :) = TRANSPOSE(basis_set%sphi(1:ncoa, sgfa:egfa))
     436              : #endif
     437         1914 :                ELSE IF (ilist == 2) THEN
     438         4584 :                   ALLOCATE (spb(iset, ibasis)%array(ncoa, basis_set%nsgf_set(iset)))
     439              : #if defined(__LIBXS)
     440              :                   CALL libxs_matcopy(C_LOC(spb(iset, ibasis)%array), &
     441              :                                      C_LOC(basis_set%sphi(1, sgfa)), &
     442              :                                      8, ncoa, egfa - sgfa + 1, &
     443         1146 :                                      SIZE(basis_set%sphi, 1), ncoa)
     444              : #else
     445              :                   spb(iset, ibasis)%array(:, :) = basis_set%sphi(1:ncoa, sgfa:egfa)
     446              : #endif
     447              :                ELSE
     448         3072 :                   ALLOCATE (spc(iset, ibasis)%array(ncoa, basis_set%nsgf_set(iset)))
     449              : #if defined(__LIBXS)
     450              :                   CALL libxs_matcopy(C_LOC(spc(iset, ibasis)%array), &
     451              :                                      C_LOC(basis_set%sphi(1, sgfa)), &
     452              :                                      8, ncoa, egfa - sgfa + 1, &
     453          768 :                                      SIZE(basis_set%sphi, 1), ncoa)
     454              : #else
     455              :                   spc(iset, ibasis)%array(:, :) = basis_set%sphi(1:ncoa, sgfa:egfa)
     456              : #endif
     457              :                END IF
     458              : 
     459              :             END DO !iset
     460              :          END DO !ibasis
     461              :       END DO !ilist
     462              : 
     463          184 :       my_sort_bc = .FALSE.
     464          184 :       IF (PRESENT(only_bc_same_center)) my_sort_bc = only_bc_same_center
     465              : 
     466          184 :       CALL get_qs_env(qs_env, para_env=para_env)
     467          184 :       CALL r12_fundamentals_init(m_max, potential_parameter, para_env)
     468              : 
     469              :       !Strategy: we use the o3c iterator because it is OMP parallelized and also takes the
     470              :       !          only_bc_same_center argument. Only the dbcsr_put_block is critical
     471              : 
     472              :       nthread = 1
     473          184 : !$    nthread = omp_get_max_threads()
     474              : 
     475          184 :       ALLOCATE (o3c)
     476              :       CALL init_o3c_container(o3c, 1, basis_set_list_a, basis_set_list_b, basis_set_list_c, &
     477          184 :                               ab_nl, ac_nl, only_bc_same_center=my_sort_bc)
     478          184 :       CALL o3c_iterator_create(o3c, o3c_iterator, nthread=nthread)
     479              : 
     480              : !$OMP PARALLEL DEFAULT(NONE) &
     481              : !$OMP SHARED (pq_X,do_screen,max_nset,basis_set_list_a,max_contra,max_contrb,max_contrc,&
     482              : !$OMP         basis_set_list_b, basis_set_list_c,ncoset,screen_radius,potential_parameter,&
     483              : !$OMP         my_eps_screen,maxli,maxlj,maxlk,my_sort_bc,nthread,o3c,o3c_iterator,tspa,spb,spc) &
     484              : !$OMP PRIVATE (integral_context,i,mepos,work,iset,ncoa,sgfa,egfa,nseta,iatom,ikind,jatom,jkind,katom,kkind,&
     485              : !$OMP          rij,rik,rjk,basis_set_a,nsetb,la_max,la_min,lb_max,lb_min,lc_max,lc_min,npgfa,&
     486              : !$OMP          npgfb,npgfc,nsgfa,nsgfb,nsgfc,ri,rk,first_sgfa,first_sgfb,first_sgfc,nsetc,&
     487              : !$OMP          set_radius_a,set_radius_b,set_radius_c,rj,rpgf_a,rpgf_b,rpgf_c,zeta,zetb,&
     488              : !$OMP          zetc,basis_set_b,basis_set_c,dij,dik,djk,ni,nj,nk,iabc,sabc,jset,kset,&
     489          184 : !$OMP          ncob,ncoc,sgfb,sgfc,egfb,egfc,sabc_ext,cpp_buffer,ccp_buffer)
     490              : 
     491              :       mepos = 0
     492              : !$    mepos = omp_get_thread_num()
     493              : 
     494              :       !each thread need its own libint object (internals may change at different rates)
     495              :       integral_context = coulomb_integral_context_type()
     496              :       CALL coulomb_integral_init(integral_context, max_am_3c=MAX(maxli, maxlj, maxlk), &
     497              :                                  derivative_order=0, library=library_libint)
     498              : 
     499              :       DO WHILE (o3c_iterate(o3c_iterator, mepos=mepos) == 0)
     500              :          CALL get_o3c_iterator_info(o3c_iterator, mepos=mepos, ikind=ikind, jkind=jkind, kkind=kkind, &
     501              :                                     iatom=iatom, jatom=jatom, katom=katom, rij=rij, rik=rik)
     502              : 
     503              :          !get first center basis info
     504              :          basis_set_a => basis_set_list_a(ikind)%gto_basis_set
     505              :          first_sgfa => basis_set_a%first_sgf
     506              :          la_max => basis_set_a%lmax
     507              :          la_min => basis_set_a%lmin
     508              :          npgfa => basis_set_a%npgf
     509              :          nseta = basis_set_a%nset
     510              :          nsgfa => basis_set_a%nsgf_set
     511              :          zeta => basis_set_a%zet
     512              :          rpgf_a => basis_set_a%pgf_radius
     513              :          set_radius_a => basis_set_a%set_radius
     514              :          ni = SUM(nsgfa)
     515              :          !second center basis info
     516              :          basis_set_b => basis_set_list_b(jkind)%gto_basis_set
     517              :          first_sgfb => basis_set_b%first_sgf
     518              :          lb_max => basis_set_b%lmax
     519              :          lb_min => basis_set_b%lmin
     520              :          npgfb => basis_set_b%npgf
     521              :          nsetb = basis_set_b%nset
     522              :          nsgfb => basis_set_b%nsgf_set
     523              :          zetb => basis_set_b%zet
     524              :          rpgf_b => basis_set_b%pgf_radius
     525              :          set_radius_b => basis_set_b%set_radius
     526              :          nj = SUM(nsgfb)
     527              :          !third center basis info
     528              :          basis_set_c => basis_set_list_c(kkind)%gto_basis_set
     529              :          first_sgfc => basis_set_c%first_sgf
     530              :          lc_max => basis_set_c%lmax
     531              :          lc_min => basis_set_c%lmin
     532              :          npgfc => basis_set_c%npgf
     533              :          nsetc = basis_set_c%nset
     534              :          nsgfc => basis_set_c%nsgf_set
     535              :          zetc => basis_set_c%zet
     536              :          rpgf_c => basis_set_c%pgf_radius
     537              :          set_radius_c => basis_set_c%set_radius
     538              :          nk = SUM(nsgfc)
     539              : 
     540              :          !position and distances, only relative pos matter for libint
     541              :          rjk = rik - rij
     542              :          ri = 0.0_dp
     543              :          rj = rij ! ri + rij
     544              :          rk = rik ! ri + rik
     545              : 
     546              :          djk = NORM2(rjk)
     547              :          dij = NORM2(rij)
     548              :          dik = NORM2(rik)
     549              : 
     550              :          !sgf integrals
     551              :          ALLOCATE (iabc(ni, nj, nk))
     552              :          iabc(:, :, :) = 0.0_dp
     553              : 
     554              :          DO iset = 1, nseta
     555              :             ncoa = npgfa(iset)*ncoset(la_max(iset))
     556              :             sgfa = first_sgfa(1, iset)
     557              :             egfa = sgfa + nsgfa(iset) - 1
     558              : 
     559              :             DO jset = 1, nsetb
     560              :                ncob = npgfb(jset)*ncoset(lb_max(jset))
     561              :                sgfb = first_sgfb(1, jset)
     562              :                egfb = sgfb + nsgfb(jset) - 1
     563              : 
     564              :                !screening (overlap)
     565              :                IF (set_radius_a(iset) + set_radius_b(jset) < dij) CYCLE
     566              : 
     567              :                DO kset = 1, nsetc
     568              :                   ncoc = npgfc(kset)*ncoset(lc_max(kset))
     569              :                   sgfc = first_sgfc(1, kset)
     570              :                   egfc = sgfc + nsgfc(kset) - 1
     571              : 
     572              :                   !screening (potential)
     573              :                   IF (set_radius_a(iset) + set_radius_c(kset) + screen_radius < dik) CYCLE
     574              :                   IF (set_radius_b(jset) + set_radius_c(kset) + screen_radius < djk) CYCLE
     575              : 
     576              :                   !pgf integrals
     577              :                   ALLOCATE (sabc(ncoa, ncob, ncoc))
     578              :                   sabc(:, :, :) = 0.0_dp
     579              : 
     580              :                   IF (do_screen) THEN
     581              :                      CALL compute_coulomb_3c(integral_context, la_min(iset), la_max(iset), lb_min(jset), &
     582              :                                              lb_max(jset), lc_min(kset), lc_max(kset), npgfa(iset), npgfb(jset), npgfc(kset), &
     583              :                                              zeta(:, iset), zetb(:, jset), zetc(:, kset), rpgf_a(:, iset), rpgf_b(:, jset), &
     584              :                                              rpgf_c(:, kset), ri, rj, rk, sabc, potential_parameter, dab=dij, dac=dik, dbc=djk, &
     585              :                                              int_abc_ext=sabc_ext)
     586              :                      IF (my_eps_screen > sabc_ext*(max_contra(iset, ikind)* &
     587              :                                                    max_contrb(jset, jkind)* &
     588              :                                                    max_contrc(kset, kkind))) THEN
     589              :                         DEALLOCATE (sabc)
     590              :                         CYCLE
     591              :                      END IF
     592              :                   ELSE
     593              :                      CALL compute_coulomb_3c(integral_context, la_min(iset), la_max(iset), lb_min(jset), &
     594              :                                              lb_max(jset), lc_min(kset), lc_max(kset), npgfa(iset), npgfb(jset), npgfc(kset), &
     595              :                                              zeta(:, iset), zetb(:, jset), zetc(:, kset), rpgf_a(:, iset), rpgf_b(:, jset), &
     596              :                                              rpgf_c(:, kset), ri, rj, rk, sabc, potential_parameter, dab=dij, dac=dik, dbc=djk)
     597              :                   END IF
     598              : 
     599              :                   ALLOCATE (work(nsgfa(iset), nsgfb(jset), nsgfc(kset)))
     600              : 
     601              :                   CALL abc_contract_xsmm(work, sabc, tspa(iset, ikind)%array, spb(jset, jkind)%array, &
     602              :                                          spc(kset, kkind)%array, ncoa, ncob, ncoc, nsgfa(iset), &
     603              :                                          nsgfb(jset), nsgfc(kset), cpp_buffer, ccp_buffer)
     604              : 
     605              :                   iabc(sgfa:egfa, sgfb:egfb, sgfc:egfc) = work(:, :, :)
     606              :                   DEALLOCATE (sabc, work)
     607              : 
     608              :                END DO !kset
     609              :             END DO !jset
     610              :          END DO !iset
     611              : 
     612              :          !Add the integral to the proper tensor block
     613              : !$OMP CRITICAL(omp_fill_pqX_tensor)
     614              :          CALL dbt_put_block(pq_X, [iatom, jatom, katom], SHAPE(iabc), iabc, summation=.TRUE.)
     615              : !$OMP END CRITICAL(omp_fill_pqX_tensor)
     616              : 
     617              :          DEALLOCATE (iabc)
     618              :       END DO !o3c_iterator
     619              : 
     620              :       IF (ALLOCATED(ccp_buffer)) DEALLOCATE (ccp_buffer)
     621              :       IF (ALLOCATED(cpp_buffer)) DEALLOCATE (cpp_buffer)
     622              : 
     623              :       CALL coulomb_integral_cleanup(integral_context)
     624              : 
     625              : !$OMP END PARALLEL
     626          184 :       CALL o3c_iterator_release(o3c_iterator)
     627          184 :       CALL release_o3c_container(o3c)
     628          184 :       DEALLOCATE (o3c)
     629              : 
     630         1122 :       DO iset = 1, max_nset
     631         2614 :          DO ibasis = 1, nbasis
     632         1492 :             IF (ASSOCIATED(tspa(iset, ibasis)%array)) DEALLOCATE (tspa(iset, ibasis)%array)
     633         1492 :             IF (ASSOCIATED(spb(iset, ibasis)%array)) DEALLOCATE (spb(iset, ibasis)%array)
     634         2430 :             IF (ASSOCIATED(spc(iset, ibasis)%array)) DEALLOCATE (spc(iset, ibasis)%array)
     635              :          END DO
     636              :       END DO
     637          184 :       DEALLOCATE (tspa, spb, spc)
     638              : 
     639          184 :       CALL timestop(handle)
     640              : 
     641          368 :    END SUBROUTINE fill_pqX_tensor
     642              : 
     643              : ! **************************************************************************************************
     644              : !> \brief Builds a neighbor lists set for overlaping 2-center S_ab, where b is restricted on a
     645              : !>        a given list of atoms. Used for Coulomb RI where (aI|P) = sum_b C_bI (ab|P), where
     646              : !>        contraction coeff C_bI is assumed to be non-zero only on excited atoms
     647              : !> \param ab_list the neighbor list
     648              : !> \param basis_a basis set list for atom a
     649              : !> \param basis_b basis set list for atom b
     650              : !> \param qs_env ...
     651              : !> \param excited_atoms the indices of the excited atoms on which b is centered
     652              : !> \param ext_dist2d use an external distribution2d
     653              : ! **************************************************************************************************
     654          356 :    SUBROUTINE build_xas_tdp_ovlp_nl(ab_list, basis_a, basis_b, qs_env, excited_atoms, ext_dist2d)
     655              : 
     656              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     657              :          POINTER                                         :: ab_list
     658              :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_a, basis_b
     659              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     660              :       INTEGER, DIMENSION(:), INTENT(IN), OPTIONAL        :: excited_atoms
     661              :       TYPE(distribution_2d_type), OPTIONAL, POINTER      :: ext_dist2d
     662              : 
     663              :       INTEGER                                            :: ikind, nkind
     664              :       LOGICAL                                            :: my_restrictb
     665              :       LOGICAL, ALLOCATABLE, DIMENSION(:)                 :: a_present, b_present
     666              :       REAL(dp)                                           :: subcells
     667              :       REAL(dp), ALLOCATABLE, DIMENSION(:)                :: a_radius, b_radius
     668          356 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: pair_radius
     669          356 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     670              :       TYPE(cell_type), POINTER                           :: cell
     671              :       TYPE(distribution_1d_type), POINTER                :: distribution_1d
     672              :       TYPE(distribution_2d_type), POINTER                :: distribution_2d
     673          356 :       TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:)  :: atom2d
     674          356 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     675          356 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     676              : 
     677          356 :       NULLIFY (atomic_kind_set, distribution_1d, distribution_2d, molecule_set, particle_set, cell)
     678              : 
     679              : !  Initialization
     680          356 :       CALL get_qs_env(qs_env, nkind=nkind)
     681          356 :       CALL section_vals_val_get(qs_env%input, "DFT%SUBCELLS", r_val=subcells)
     682              : 
     683          356 :       my_restrictb = .FALSE.
     684          356 :       IF (PRESENT(excited_atoms)) THEN
     685          124 :          my_restrictb = .TRUE.
     686              :       END IF
     687              : 
     688         1424 :       ALLOCATE (a_present(nkind), b_present(nkind))
     689          356 :       a_present = .FALSE.
     690          356 :       b_present = .FALSE.
     691         1424 :       ALLOCATE (a_radius(nkind), b_radius(nkind))
     692          356 :       a_radius = 0.0_dp
     693          356 :       b_radius = 0.0_dp
     694              : 
     695              : !  Set up the radii
     696          946 :       DO ikind = 1, nkind
     697          590 :          IF (ASSOCIATED(basis_a(ikind)%gto_basis_set)) THEN
     698          590 :             a_present(ikind) = .TRUE.
     699          590 :             CALL get_gto_basis_set(basis_a(ikind)%gto_basis_set, kind_radius=a_radius(ikind))
     700              :          END IF
     701              : 
     702          946 :          IF (ASSOCIATED(basis_b(ikind)%gto_basis_set)) THEN
     703          590 :             b_present(ikind) = .TRUE.
     704          590 :             CALL get_gto_basis_set(basis_b(ikind)%gto_basis_set, kind_radius=b_radius(ikind))
     705              :          END IF
     706              :       END DO !ikind
     707              : 
     708         1424 :       ALLOCATE (pair_radius(nkind, nkind))
     709          356 :       pair_radius = 0.0_dp
     710          356 :       CALL pair_radius_setup(a_present, b_present, a_radius, b_radius, pair_radius)
     711              : 
     712              : !  Set up the nl
     713              :       CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, cell=cell, &
     714              :                       distribution_2d=distribution_2d, local_particles=distribution_1d, &
     715          356 :                       particle_set=particle_set, molecule_set=molecule_set)
     716              : 
     717              :       !use an external distribution_2d if required
     718          356 :       IF (PRESENT(ext_dist2d)) distribution_2d => ext_dist2d
     719              : 
     720         1658 :       ALLOCATE (atom2d(nkind))
     721              :       CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
     722          356 :                         molecule_set, .FALSE., particle_set)
     723              : 
     724          356 :       IF (my_restrictb) THEN
     725              : 
     726              :          CALL build_neighbor_lists(ab_list, particle_set, atom2d, cell, pair_radius, subcells, &
     727          124 :                                    atomb_to_keep=excited_atoms, nlname="XAS_TDP_ovlp_nl")
     728              : 
     729              :       ELSE
     730              : 
     731              :          CALL build_neighbor_lists(ab_list, particle_set, atom2d, cell, pair_radius, subcells, &
     732          232 :                                    nlname="XAS_TDP_ovlp_nl")
     733              : 
     734              :       END IF
     735              : !  Clean-up
     736          356 :       CALL atom2d_cleanup(atom2d)
     737              : 
     738          712 :    END SUBROUTINE build_xas_tdp_ovlp_nl
     739              : 
     740              : ! **************************************************************************************************
     741              : !> \brief Builds a neighbor lists set taylored for 3-center integral within XAS TDP, such that only
     742              : !>        excited atoms are taken into account for the list_c
     743              : !> \param ac_list the neighbor list ready for 3-center integrals
     744              : !> \param basis_a basis set list for atom a
     745              : !> \param basis_c basis set list for atom c
     746              : !> \param op_type to indicate whther the list should be built with overlap, Coulomb or else in mind
     747              : !> \param qs_env ...
     748              : !> \param excited_atoms the indices of the excited atoms to consider (if not given, all atoms are taken)
     749              : !> \param x_range in case some truncated/screened operator is used, gives its range
     750              : !> \param ext_dist2d external distribution_2d to be used
     751              : !> \note Based on setup_neighbor_list with added features
     752              : ! **************************************************************************************************
     753          300 :    SUBROUTINE build_xas_tdp_3c_nl(ac_list, basis_a, basis_c, op_type, qs_env, excited_atoms, &
     754              :                                   x_range, ext_dist2d)
     755              : 
     756              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     757              :          POINTER                                         :: ac_list
     758              :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_a, basis_c
     759              :       INTEGER, INTENT(IN)                                :: op_type
     760              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     761              :       INTEGER, DIMENSION(:), INTENT(IN), OPTIONAL        :: excited_atoms
     762              :       REAL(dp), INTENT(IN), OPTIONAL                     :: x_range
     763              :       TYPE(distribution_2d_type), OPTIONAL, POINTER      :: ext_dist2d
     764              : 
     765              :       INTEGER                                            :: ikind, nkind
     766              :       LOGICAL                                            :: sort_atoms
     767              :       LOGICAL, ALLOCATABLE, DIMENSION(:)                 :: a_present, c_present
     768              :       REAL(dp)                                           :: subcells
     769              :       REAL(dp), ALLOCATABLE, DIMENSION(:)                :: a_radius, c_radius
     770          300 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: pair_radius
     771          300 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     772              :       TYPE(cell_type), POINTER                           :: cell
     773              :       TYPE(distribution_1d_type), POINTER                :: distribution_1d
     774              :       TYPE(distribution_2d_type), POINTER                :: distribution_2d
     775          300 :       TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:)  :: atom2d
     776          300 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     777          300 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     778              : 
     779          300 :       NULLIFY (atomic_kind_set, distribution_1d, distribution_2d, molecule_set, particle_set, cell)
     780              : 
     781              : !  Initialization
     782          300 :       CALL get_qs_env(qs_env, nkind=nkind)
     783          300 :       CALL section_vals_val_get(qs_env%input, "DFT%SUBCELLS", r_val=subcells)
     784          300 :       sort_atoms = .FALSE.
     785          300 :       IF ((PRESENT(excited_atoms))) sort_atoms = .TRUE.
     786              : 
     787         1200 :       ALLOCATE (a_present(nkind), c_present(nkind))
     788          300 :       a_present = .FALSE.
     789          300 :       c_present = .FALSE.
     790         1200 :       ALLOCATE (a_radius(nkind), c_radius(nkind))
     791          300 :       a_radius = 0.0_dp
     792          300 :       c_radius = 0.0_dp
     793              : 
     794              : !  Set up the radii, depending on the operator type
     795          300 :       IF (op_type == do_potential_id) THEN
     796              : 
     797              :          !overlap => use the kind radius for both a and c
     798          514 :          DO ikind = 1, nkind
     799              :             !orbital basis set
     800          318 :             IF (ASSOCIATED(basis_a(ikind)%gto_basis_set)) THEN
     801          318 :                a_present(ikind) = .TRUE.
     802          318 :                CALL get_gto_basis_set(basis_a(ikind)%gto_basis_set, kind_radius=a_radius(ikind))
     803              :             END IF
     804              :             !RI_XAS basis set
     805          514 :             IF (ASSOCIATED(basis_c(ikind)%gto_basis_set)) THEN
     806          318 :                c_present(ikind) = .TRUE.
     807          318 :                CALL get_gto_basis_set(basis_c(ikind)%gto_basis_set, kind_radius=c_radius(ikind))
     808              :             END IF
     809              :          END DO !ikind
     810              : 
     811          104 :       ELSE IF (op_type == do_potential_coulomb) THEN
     812              : 
     813              :          !Coulomb operator, virtually infinite range => set c_radius to arbitrarily large number
     814          236 :          DO ikind = 1, nkind
     815          156 :             IF (ASSOCIATED(basis_c(ikind)%gto_basis_set)) THEN
     816          156 :                c_present(ikind) = .TRUE.
     817          156 :                c_radius(ikind) = 1000000.0_dp
     818              :             END IF
     819          236 :             IF (ASSOCIATED(basis_a(ikind)%gto_basis_set)) a_present(ikind) = .TRUE.
     820              :          END DO !ikind
     821              : 
     822           24 :       ELSE IF (op_type == do_potential_truncated .OR. op_type == do_potential_short) THEN
     823              : 
     824              :          !Truncated coulomb/short range: set c_radius to x_range + the kind_radii
     825           48 :          DO ikind = 1, nkind
     826           24 :             IF (ASSOCIATED(basis_a(ikind)%gto_basis_set)) THEN
     827           24 :                a_present(ikind) = .TRUE.
     828           24 :                CALL get_gto_basis_set(basis_a(ikind)%gto_basis_set, kind_radius=a_radius(ikind))
     829              :             END IF
     830           48 :             IF (ASSOCIATED(basis_c(ikind)%gto_basis_set)) THEN
     831           24 :                c_present(ikind) = .TRUE.
     832           24 :                CALL get_gto_basis_set(basis_c(ikind)%gto_basis_set, kind_radius=c_radius(ikind))
     833           24 :                c_radius(ikind) = c_radius(ikind) + x_range
     834              :             END IF
     835              :          END DO !ikind
     836              : 
     837              :       ELSE
     838            0 :          CPABORT("Operator not known")
     839              :       END IF
     840              : 
     841         1200 :       ALLOCATE (pair_radius(nkind, nkind))
     842          300 :       pair_radius = 0.0_dp
     843          300 :       CALL pair_radius_setup(a_present, c_present, a_radius, c_radius, pair_radius)
     844              : 
     845              : !  Actually setup the list
     846              :       CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, cell=cell, &
     847              :                       distribution_2d=distribution_2d, local_particles=distribution_1d, &
     848          300 :                       particle_set=particle_set, molecule_set=molecule_set)
     849              : 
     850              :       !use an external distribution_2d if required
     851          300 :       IF (PRESENT(ext_dist2d)) distribution_2d => ext_dist2d
     852              : 
     853         1398 :       ALLOCATE (atom2d(nkind))
     854              :       CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
     855          300 :                         molecule_set, .FALSE., particle_set)
     856              : 
     857          300 :       IF (sort_atoms) THEN
     858              :          CALL build_neighbor_lists(ac_list, particle_set, atom2d, cell, pair_radius, subcells, &
     859              :                                    operator_type="ABC", atomb_to_keep=excited_atoms, &
     860          300 :                                    nlname="XAS_TDP_3c_nl")
     861              :       ELSE
     862              :          CALL build_neighbor_lists(ac_list, particle_set, atom2d, cell, pair_radius, subcells, &
     863            0 :                                    operator_type="ABC", nlname="XAS_TDP_3c_nl")
     864              :       END IF
     865              : 
     866              : !  Clean-up
     867          300 :       CALL atom2d_cleanup(atom2d)
     868              : 
     869          600 :    END SUBROUTINE build_xas_tdp_3c_nl
     870              : 
     871              : ! **************************************************************************************************
     872              : !> \brief Returns an optimized distribution_2d for the given neighbor lists based on an evaluation
     873              : !>        of the cost of the corresponding 3-center integrals
     874              : !> \param opt_3c_dist2d the optimized distribution_2d
     875              : !> \param ab_list ...
     876              : !> \param ac_list ...
     877              : !> \param basis_set_a ...
     878              : !> \param basis_set_b ...
     879              : !> \param basis_set_c ...
     880              : !> \param qs_env ...
     881              : !> \param only_bc_same_center ...
     882              : ! **************************************************************************************************
     883          116 :    SUBROUTINE get_opt_3c_dist2d(opt_3c_dist2d, ab_list, ac_list, basis_set_a, basis_set_b, &
     884              :                                 basis_set_c, qs_env, only_bc_same_center)
     885              : 
     886              :       TYPE(distribution_2d_type), POINTER                :: opt_3c_dist2d
     887              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     888              :          POINTER                                         :: ab_list, ac_list
     889              :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_a, basis_set_b, basis_set_c
     890              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     891              :       LOGICAL, INTENT(IN), OPTIONAL                      :: only_bc_same_center
     892              : 
     893              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'get_opt_3c_dist2d'
     894              : 
     895              :       INTEGER                                            :: handle, i, iatom, ikind, ip, jatom, &
     896              :                                                             jkind, kkind, mypcol, myprow, n, &
     897              :                                                             natom, nkind, npcol, nprow, nsgfa, &
     898              :                                                             nsgfb, nsgfc
     899          116 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: nparticle_local_col, nparticle_local_row
     900          116 :       INTEGER, DIMENSION(:, :), POINTER                  :: col_dist, row_dist
     901              :       LOGICAL                                            :: my_sort_bc
     902              :       REAL(dp)                                           :: cost, rab(3), rac(3), rbc(3)
     903          116 :       REAL(dp), ALLOCATABLE, DIMENSION(:)                :: col_cost, col_proc_cost, row_cost, &
     904          116 :                                                             row_proc_cost
     905          116 :       TYPE(cp_1d_i_p_type), DIMENSION(:), POINTER        :: local_particle_col, local_particle_row
     906              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     907              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     908              :       TYPE(neighbor_list_iterator_p_type), &
     909          116 :          DIMENSION(:), POINTER                           :: ab_iter, ac_iter
     910          116 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     911          116 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     912              : 
     913          116 :       NULLIFY (para_env, col_dist, row_dist, blacs_env, qs_kind_set, particle_set)
     914          116 :       NULLIFY (local_particle_col, local_particle_row, ab_iter, ac_iter)
     915              : 
     916          116 :       CALL timeset(routineN, handle)
     917              : 
     918              :       !Idea: create a,b and a,c nl_iterators in the original dist, then loop over them and compute the
     919              :       !      cost of each ab pairs and project on the col/row. Then distribute the atom col/row to
     920              :       !      the proc col/row in order to spread out the cost as much as possible
     921              : 
     922          116 :       my_sort_bc = .FALSE.
     923          116 :       IF (PRESENT(only_bc_same_center)) my_sort_bc = only_bc_same_center
     924              : 
     925              :       CALL get_qs_env(qs_env, natom=natom, para_env=para_env, blacs_env=blacs_env, &
     926          116 :                       qs_kind_set=qs_kind_set, particle_set=particle_set, nkind=nkind)
     927              : 
     928          116 :       myprow = blacs_env%mepos(1) + 1
     929          116 :       mypcol = blacs_env%mepos(2) + 1
     930          116 :       nprow = blacs_env%num_pe(1)
     931          116 :       npcol = blacs_env%num_pe(2)
     932              : 
     933          464 :       ALLOCATE (col_cost(natom), row_cost(natom))
     934          116 :       col_cost = 0.0_dp; row_cost = 0.0_dp
     935              : 
     936          464 :       ALLOCATE (row_dist(natom, 2), col_dist(natom, 2))
     937         3212 :       row_dist = 1; col_dist = 1
     938              : 
     939          116 :       CALL neighbor_list_iterator_create(ab_iter, ab_list)
     940          116 :       CALL neighbor_list_iterator_create(ac_iter, ac_list, search=.TRUE.)
     941         1256 :       DO WHILE (neighbor_list_iterate(ab_iter) == 0)
     942         1140 :          CALL get_iterator_info(ab_iter, ikind=ikind, jkind=jkind, iatom=iatom, jatom=jatom, r=rab)
     943              : 
     944         2846 :          DO kkind = 1, nkind
     945         1590 :             CALL nl_set_sub_iterator(ac_iter, ikind, kkind, iatom)
     946              : 
     947         7135 :             DO WHILE (nl_sub_iterate(ac_iter) == 0)
     948              : 
     949         4405 :                IF (my_sort_bc) THEN
     950              :                   !only take a,b,c if b,c (or a,c because of symmetry) share the same center
     951          724 :                   CALL get_iterator_info(ac_iter, r=rac)
     952         2896 :                   rbc(:) = rac(:) - rab(:)
     953         3196 :                   IF (.NOT. (ALL(ABS(rbc) <= 1.0E-8_dp) .OR. ALL(ABS(rac) <= 1.0E-8_dp))) CYCLE
     954              : 
     955              :                END IF
     956              : 
     957              :                !Use the size of integral as measure as contraciton cost seems to dominate
     958         4111 :                nsgfa = basis_set_a(ikind)%gto_basis_set%nsgf
     959         4111 :                nsgfb = basis_set_b(jkind)%gto_basis_set%nsgf
     960         4111 :                nsgfc = basis_set_c(kkind)%gto_basis_set%nsgf
     961              : 
     962         4111 :                cost = REAL(nsgfa*nsgfb*nsgfc, dp)
     963              : 
     964         4111 :                row_cost(iatom) = row_cost(iatom) + cost
     965         4405 :                col_cost(jatom) = col_cost(jatom) + cost
     966              : 
     967              :             END DO !ac_iter
     968              :          END DO !kkind
     969              :       END DO !ab_iter
     970          116 :       CALL neighbor_list_iterator_release(ab_iter)
     971          116 :       CALL neighbor_list_iterator_release(ac_iter)
     972              : 
     973          116 :       CALL para_env%sum(row_cost)
     974          116 :       CALL para_env%sum(col_cost)
     975              : 
     976              :       !Distribute the cost as evenly as possible
     977          580 :       ALLOCATE (col_proc_cost(npcol), row_proc_cost(nprow))
     978          116 :       col_proc_cost = 0.0_dp; row_proc_cost = 0.0_dp
     979          774 :       DO i = 1, natom
     980        21708 :          iatom = MAXLOC(row_cost, 1)
     981         2632 :          ip = MINLOC(row_proc_cost, 1)
     982          658 :          row_proc_cost(ip) = row_proc_cost(ip) + row_cost(iatom)
     983          658 :          row_dist(iatom, 1) = ip
     984          658 :          row_cost(iatom) = 0.0_dp
     985              : 
     986        21708 :          iatom = MAXLOC(col_cost, 1)
     987         1974 :          ip = MINLOC(col_proc_cost, 1)
     988          658 :          col_proc_cost(ip) = col_proc_cost(ip) + col_cost(iatom)
     989          658 :          col_dist(iatom, 1) = ip
     990          774 :          col_cost(iatom) = 0.0_dp
     991              :       END DO
     992              : 
     993              :       !the usual stuff
     994          844 :       ALLOCATE (local_particle_col(nkind), local_particle_row(nkind))
     995          464 :       ALLOCATE (nparticle_local_row(nkind), nparticle_local_col(nkind))
     996          116 :       nparticle_local_row = 0; nparticle_local_col = 0
     997              : 
     998          774 :       DO iatom = 1, natom
     999          658 :          ikind = particle_set(iatom)%atomic_kind%kind_number
    1000              : 
    1001          658 :          IF (row_dist(iatom, 1) == myprow) nparticle_local_row(ikind) = nparticle_local_row(ikind) + 1
    1002          774 :          IF (col_dist(iatom, 1) == mypcol) nparticle_local_col(ikind) = nparticle_local_col(ikind) + 1
    1003              :       END DO
    1004              : 
    1005          306 :       DO ikind = 1, nkind
    1006          190 :          n = nparticle_local_row(ikind)
    1007          505 :          ALLOCATE (local_particle_row(ikind)%array(n))
    1008              : 
    1009          190 :          n = nparticle_local_col(ikind)
    1010          686 :          ALLOCATE (local_particle_col(ikind)%array(n))
    1011              :       END DO
    1012              : 
    1013          116 :       nparticle_local_row = 0; nparticle_local_col = 0
    1014          774 :       DO iatom = 1, natom
    1015          658 :          ikind = particle_set(iatom)%atomic_kind%kind_number
    1016              : 
    1017          658 :          IF (row_dist(iatom, 1) == myprow) THEN
    1018          329 :             nparticle_local_row(ikind) = nparticle_local_row(ikind) + 1
    1019          329 :             local_particle_row(ikind)%array(nparticle_local_row(ikind)) = iatom
    1020              :          END IF
    1021          774 :          IF (col_dist(iatom, 1) == mypcol) THEN
    1022          658 :             nparticle_local_col(ikind) = nparticle_local_col(ikind) + 1
    1023          658 :             local_particle_col(ikind)%array(nparticle_local_col(ikind)) = iatom
    1024              :          END IF
    1025              :       END DO
    1026              : 
    1027              :       !Finally create the dist_2d
    1028          774 :       row_dist(:, 1) = row_dist(:, 1) - 1
    1029          774 :       col_dist(:, 1) = col_dist(:, 1) - 1
    1030              :       CALL distribution_2d_create(opt_3c_dist2d, row_distribution_ptr=row_dist, &
    1031              :                                   col_distribution_ptr=col_dist, local_rows_ptr=local_particle_row, &
    1032          116 :                                   local_cols_ptr=local_particle_col, blacs_env=blacs_env)
    1033              : 
    1034          116 :       CALL timestop(handle)
    1035              : 
    1036          232 :    END SUBROUTINE get_opt_3c_dist2d
    1037              : 
    1038              : ! **************************************************************************************************
    1039              : !> \brief Computes the RI exchange 3-center integrals (ab|c), where c is from the RI_XAS basis and
    1040              : !>        centered on excited atoms and kind. The operator used is that of the RI metric
    1041              : !> \param ex_atoms excited atoms on which the third center is located
    1042              : !> \param xas_tdp_env ...
    1043              : !> \param xas_tdp_control ...
    1044              : !> \param qs_env ...
    1045              : !> \note  This routine is called once for each excited atom. Because there are many different a,b
    1046              : !>        pairs involved, load balance is ok. This allows memory saving
    1047              : ! **************************************************************************************************
    1048           54 :    SUBROUTINE compute_ri_3c_exchange(ex_atoms, xas_tdp_env, xas_tdp_control, qs_env)
    1049              : 
    1050              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: ex_atoms
    1051              :       TYPE(xas_tdp_env_type), POINTER                    :: xas_tdp_env
    1052              :       TYPE(xas_tdp_control_type), POINTER                :: xas_tdp_control
    1053              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1054              : 
    1055              :       CHARACTER(len=*), PARAMETER :: routineN = 'compute_ri_3c_exchange'
    1056              : 
    1057              :       INTEGER                                            :: handle, natom, nkind
    1058              :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: blk_size_orb, blk_size_ri
    1059              :       TYPE(dbcsr_distribution_type)                      :: opt_dbcsr_dist
    1060           54 :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_orb, basis_set_ri
    1061              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1062              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1063           54 :          POINTER                                         :: ab_list, ac_list
    1064           54 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1065           54 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1066              : 
    1067           54 :       NULLIFY (basis_set_ri, basis_set_orb, ac_list, ab_list, qs_kind_set, para_env, particle_set)
    1068              : 
    1069           54 :       CALL timeset(routineN, handle)
    1070              : 
    1071              : !  Take what we need from the qs_env
    1072              :       CALL get_qs_env(qs_env, nkind=nkind, qs_kind_set=qs_kind_set, para_env=para_env, &
    1073           54 :                       natom=natom, particle_set=particle_set)
    1074              : 
    1075              : !  Build the basis set lists
    1076          254 :       ALLOCATE (basis_set_ri(nkind))
    1077          200 :       ALLOCATE (basis_set_orb(nkind))
    1078           54 :       CALL basis_set_list_setup(basis_set_ri, "RI_XAS", qs_kind_set)
    1079           54 :       CALL basis_set_list_setup(basis_set_orb, "ORB", qs_kind_set)
    1080              : 
    1081              : !  Get the optimized distribution 2d for theses integrals (and store it in xas_tdp_env)
    1082           54 :       CALL build_xas_tdp_ovlp_nl(ab_list, basis_set_orb, basis_set_orb, qs_env)
    1083              :       CALL build_xas_tdp_3c_nl(ac_list, basis_set_orb, basis_set_ri, &
    1084              :                                xas_tdp_control%ri_m_potential%potential_type, qs_env, &
    1085           54 :                                excited_atoms=ex_atoms, x_range=xas_tdp_control%ri_m_potential%cutoff_radius)
    1086              : 
    1087              :       CALL get_opt_3c_dist2d(xas_tdp_env%opt_dist2d_ex, ab_list, ac_list, basis_set_orb, &
    1088           54 :                              basis_set_orb, basis_set_ri, qs_env)
    1089           54 :       CALL release_neighbor_list_sets(ab_list)
    1090           54 :       CALL release_neighbor_list_sets(ac_list)
    1091              : 
    1092              : !  Build the ab and ac centers neighbor lists based on the optimized distribution
    1093              :       CALL build_xas_tdp_ovlp_nl(ab_list, basis_set_orb, basis_set_orb, qs_env, &
    1094           54 :                                  ext_dist2d=xas_tdp_env%opt_dist2d_ex)
    1095              :       CALL build_xas_tdp_3c_nl(ac_list, basis_set_orb, basis_set_ri, &
    1096              :                                xas_tdp_control%ri_m_potential%potential_type, qs_env, &
    1097              :                                excited_atoms=ex_atoms, x_range=xas_tdp_control%ri_m_potential%cutoff_radius, &
    1098           54 :                                ext_dist2d=xas_tdp_env%opt_dist2d_ex)
    1099              : 
    1100              : !  Allocate, init and compute the integrals.
    1101          216 :       ALLOCATE (blk_size_orb(natom), blk_size_ri(natom))
    1102           54 :       CALL cp_dbcsr_dist2d_to_dist(xas_tdp_env%opt_dist2d_ex, opt_dbcsr_dist)
    1103           54 :       CALL get_particle_set(particle_set, qs_kind_set, nsgf=blk_size_orb, basis=basis_set_orb)
    1104           54 :       CALL get_particle_set(particle_set, qs_kind_set, nsgf=blk_size_ri, basis=basis_set_ri)
    1105              : 
    1106          540 :       ALLOCATE (xas_tdp_env%ri_3c_ex)
    1107              :       CALL create_pqX_tensor(xas_tdp_env%ri_3c_ex, ab_list, ac_list, opt_dbcsr_dist, blk_size_orb, &
    1108           54 :                              blk_size_orb, blk_size_ri)
    1109              :       CALL fill_pqX_tensor(xas_tdp_env%ri_3c_ex, ab_list, ac_list, basis_set_orb, basis_set_orb, &
    1110              :                            basis_set_ri, xas_tdp_control%ri_m_potential, qs_env, &
    1111           54 :                            eps_screen=xas_tdp_control%eps_screen)
    1112              : 
    1113              : ! Clean-up
    1114           54 :       CALL release_neighbor_list_sets(ab_list)
    1115           54 :       CALL release_neighbor_list_sets(ac_list)
    1116           54 :       CALL dbcsr_distribution_release(opt_dbcsr_dist)
    1117           54 :       DEALLOCATE (basis_set_ri, basis_set_orb)
    1118              : 
    1119              :       !not strictly necessary but avoid having any load unbalance here being reported in the
    1120              :       !timings for other routines
    1121           54 :       CALL para_env%sync()
    1122              : 
    1123           54 :       CALL timestop(handle)
    1124              : 
    1125          162 :    END SUBROUTINE compute_ri_3c_exchange
    1126              : 
    1127              : ! **************************************************************************************************
    1128              : !> \brief Computes the RI Coulomb 3-center integrals (ab|c), where c is from the RI_XAS basis and
    1129              : !>        centered on the excited atoms of xas_tdp_env
    1130              : !> \param xas_tdp_env ...
    1131              : !> \param qs_env ...
    1132              : !> \note  The ri_3c_coul tensor of xas_tdp_env is defined and allocated here. Only computed once
    1133              : !>        for the whole system (for optimized load balance). Ok because not too much memory needed
    1134              : ! **************************************************************************************************
    1135           62 :    SUBROUTINE compute_ri_3c_coulomb(xas_tdp_env, qs_env)
    1136              : 
    1137              :       TYPE(xas_tdp_env_type), POINTER                    :: xas_tdp_env
    1138              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1139              : 
    1140              :       CHARACTER(len=*), PARAMETER :: routineN = 'compute_ri_3c_coulomb'
    1141              : 
    1142              :       INTEGER                                            :: handle, natom, nkind
    1143              :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: blk_size_orb, blk_size_ri
    1144              :       TYPE(coulomb_operator_type)                        :: pot
    1145              :       TYPE(dbcsr_distribution_type)                      :: opt_dbcsr_dist
    1146           62 :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_orb, basis_set_ri
    1147              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1148              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    1149           62 :          POINTER                                         :: ab_list, ac_list
    1150           62 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1151           62 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1152              : 
    1153           62 :       NULLIFY (basis_set_ri, basis_set_orb, ac_list, ab_list, qs_kind_set, para_env, particle_set)
    1154              : 
    1155           62 :       CALL timeset(routineN, handle)
    1156              : 
    1157              : !  Take what we need from the qs_env
    1158              :       CALL get_qs_env(qs_env, nkind=nkind, qs_kind_set=qs_kind_set, para_env=para_env, &
    1159           62 :                       natom=natom, particle_set=particle_set)
    1160              : 
    1161              : !  Build the basis set lists
    1162          284 :       ALLOCATE (basis_set_ri(nkind))
    1163          222 :       ALLOCATE (basis_set_orb(nkind))
    1164           62 :       CALL basis_set_list_setup(basis_set_ri, "RI_XAS", qs_kind_set)
    1165           62 :       CALL basis_set_list_setup(basis_set_orb, "ORB", qs_kind_set)
    1166              : 
    1167              : !  Get the optimized distribution 2d for these integrals (and store it in xas_tdp_env)
    1168              :       CALL build_xas_tdp_ovlp_nl(ab_list, basis_set_orb, basis_set_orb, qs_env, &
    1169           62 :                                  excited_atoms=xas_tdp_env%ex_atom_indices)
    1170              :       CALL build_xas_tdp_3c_nl(ac_list, basis_set_orb, basis_set_ri, do_potential_id, &
    1171           62 :                                qs_env, excited_atoms=xas_tdp_env%ex_atom_indices)
    1172              :       CALL get_opt_3c_dist2d(xas_tdp_env%opt_dist2d_coul, ab_list, ac_list, basis_set_orb, &
    1173           62 :                              basis_set_orb, basis_set_ri, qs_env, only_bc_same_center=.TRUE.)
    1174           62 :       CALL release_neighbor_list_sets(ab_list)
    1175           62 :       CALL release_neighbor_list_sets(ac_list)
    1176              : 
    1177              : !  Build a neighbor list for the ab centers. Assume (aI|c) = sum_b c_bI (ab|c), with c_bI only
    1178              : !  non-zero for b centered on the same atom as c => build overlap nl, but only keeping b if centered
    1179              : !  on an excited atom
    1180              :       CALL build_xas_tdp_ovlp_nl(ab_list, basis_set_orb, basis_set_orb, qs_env, &
    1181              :                                  excited_atoms=xas_tdp_env%ex_atom_indices, &
    1182           62 :                                  ext_dist2d=xas_tdp_env%opt_dist2d_coul)
    1183              : 
    1184              : !  Build a neighbor list for the ac centers. Since we later contract as (aI|c) and we assume I is
    1185              : !  very localized on the same atom as c, we take a,c as neighbors if they overlap
    1186              :       CALL build_xas_tdp_3c_nl(ac_list, basis_set_orb, basis_set_ri, do_potential_id, &
    1187              :                                qs_env, excited_atoms=xas_tdp_env%ex_atom_indices, &
    1188           62 :                                ext_dist2d=xas_tdp_env%opt_dist2d_coul)
    1189              : 
    1190              : !  Allocate, init and compute the integrals
    1191          248 :       ALLOCATE (blk_size_orb(natom), blk_size_ri(natom))
    1192           62 :       CALL cp_dbcsr_dist2d_to_dist(xas_tdp_env%opt_dist2d_coul, opt_dbcsr_dist)
    1193           62 :       CALL get_particle_set(particle_set, qs_kind_set, nsgf=blk_size_orb, basis=basis_set_orb)
    1194           62 :       CALL get_particle_set(particle_set, qs_kind_set, nsgf=blk_size_ri, basis=basis_set_ri)
    1195              :       pot%potential_type = do_potential_coulomb
    1196              : 
    1197          620 :       ALLOCATE (xas_tdp_env%ri_3c_coul)
    1198              :       CALL create_pqX_tensor(xas_tdp_env%ri_3c_coul, ab_list, ac_list, opt_dbcsr_dist, blk_size_orb, &
    1199           62 :                              blk_size_orb, blk_size_ri, only_bc_same_center=.TRUE.)
    1200              :       CALL fill_pqX_tensor(xas_tdp_env%ri_3c_coul, ab_list, ac_list, basis_set_orb, basis_set_orb, &
    1201           62 :                            basis_set_ri, pot, qs_env, only_bc_same_center=.TRUE.)
    1202              : 
    1203              : ! Clean-up
    1204           62 :       CALL release_neighbor_list_sets(ab_list)
    1205           62 :       CALL release_neighbor_list_sets(ac_list)
    1206           62 :       CALL dbcsr_distribution_release(opt_dbcsr_dist)
    1207           62 :       DEALLOCATE (basis_set_ri, basis_set_orb)
    1208              : 
    1209              :       !not strictly necessary but avoid having any load unbalance here being reported in the
    1210              :       !timings for other routines
    1211           62 :       CALL para_env%sync()
    1212              : 
    1213           62 :       CALL timestop(handle)
    1214              : 
    1215          186 :    END SUBROUTINE compute_ri_3c_coulomb
    1216              : 
    1217              : ! **************************************************************************************************
    1218              : !> \brief Computes the two-center Exchange integral needed for the RI in kernel calculation. Stores
    1219              : !>        the integrals in the xas_tdp_env as global (small) arrays. Does that for a given excited
    1220              : !>        kind. The quantity stored is M^-1 (P|Q) M^-1, where M is the RI metric. If the metric is
    1221              : !>        the same as the exchange potential, then we end up with the V-approximation (P|Q)^-1
    1222              : !>        By default (if no metric), the ri_m_potential is a copy of the x_potential
    1223              : !> \param ex_kind ...
    1224              : !> \param xas_tdp_env ...
    1225              : !> \param xas_tdp_control ...
    1226              : !> \param qs_env ...
    1227              : !> \note Computes all these integrals in non-PBCs as we assume that the range is short enough that
    1228              : !>       atoms do not exchange with their periodic images
    1229              : ! **************************************************************************************************
    1230           54 :    SUBROUTINE compute_ri_exchange2_int(ex_kind, xas_tdp_env, xas_tdp_control, qs_env)
    1231              : 
    1232              :       INTEGER, INTENT(IN)                                :: ex_kind
    1233              :       TYPE(xas_tdp_env_type), POINTER                    :: xas_tdp_env
    1234              :       TYPE(xas_tdp_control_type), POINTER                :: xas_tdp_control
    1235              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1236              : 
    1237              :       INTEGER                                            :: egfp, egfq, maxl, ncop, ncoq, nset, &
    1238              :                                                             nsgf, pset, qset, sgfp, sgfq
    1239           54 :       INTEGER, DIMENSION(:), POINTER                     :: lmax, lmin, npgf_set, nsgf_set
    1240           54 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgf
    1241              :       REAL(dp)                                           :: r(3)
    1242           54 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: metric, pq, work
    1243           54 :       REAL(dp), DIMENSION(:, :), POINTER                 :: rpgf, sphi, zet
    1244           54 :       TYPE(coulomb_integral_context_type)                :: integral_context
    1245              :       TYPE(gto_basis_set_type), POINTER                  :: ri_basis
    1246              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1247           54 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1248              : 
    1249           54 :       NULLIFY (ri_basis, qs_kind_set, para_env, lmin, lmax, npgf_set, zet, rpgf, first_sgf)
    1250           54 :       NULLIFY (sphi, nsgf_set)
    1251              : 
    1252              : !  Initialization
    1253           54 :       CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, para_env=para_env)
    1254           54 :       IF (ASSOCIATED(xas_tdp_env%ri_inv_ex)) THEN
    1255            8 :          DEALLOCATE (xas_tdp_env%ri_inv_ex)
    1256              :       END IF
    1257              : 
    1258              : !  Get the RI basis of interest and its quantum numbers
    1259           54 :       CALL get_qs_kind(qs_kind_set(ex_kind), basis_set=ri_basis, basis_type="RI_XAS")
    1260              :       CALL get_gto_basis_set(ri_basis, nsgf=nsgf, maxl=maxl, npgf=npgf_set, lmin=lmin, &
    1261              :                              lmax=lmax, zet=zet, pgf_radius=rpgf, first_sgf=first_sgf, &
    1262           54 :                              nsgf_set=nsgf_set, sphi=sphi, nset=nset)
    1263          216 :       ALLOCATE (metric(nsgf, nsgf))
    1264           54 :       metric = 0.0_dp
    1265              : 
    1266           54 :       r = 0.0_dp
    1267              : 
    1268              :       !init the libint 2-center object
    1269              :       CALL coulomb_integral_init(integral_context, max_am_2c=maxl, &
    1270           54 :                                  derivative_order=0, library=library_libint)
    1271              : 
    1272           54 :       CALL r12_fundamentals_init(2*maxl + 1, xas_tdp_control%ri_m_potential, para_env)
    1273              : 
    1274              : !  Compute the RI metric
    1275          162 :       DO pset = 1, nset
    1276          108 :          ncop = npgf_set(pset)*ncoset(lmax(pset))
    1277          108 :          sgfp = first_sgf(1, pset)
    1278          108 :          egfp = sgfp + nsgf_set(pset) - 1
    1279              : 
    1280          378 :          DO qset = 1, nset
    1281          216 :             ncoq = npgf_set(qset)*ncoset(lmax(qset))
    1282          216 :             sgfq = first_sgf(1, qset)
    1283          216 :             egfq = sgfq + nsgf_set(qset) - 1
    1284              : 
    1285          864 :             ALLOCATE (work(ncop, ncoq))
    1286          216 :             work = 0.0_dp
    1287              : 
    1288              :             CALL compute_coulomb_2c(integral_context, lmin(pset), lmax(pset), lmin(qset), lmax(qset), &
    1289              :                                    npgf_set(pset), npgf_set(qset), zet(:, pset), zet(:, qset), rpgf(:, pset), rpgf(:, qset), r, r, &
    1290          216 :                                     work, xas_tdp_control%ri_m_potential, dab=0.0_dp)
    1291              : 
    1292              :             CALL ab_contract(metric(sgfp:egfp, sgfq:egfq), work, sphi(:, sgfp:), sphi(:, sgfq:), &
    1293          216 :                              ncop, ncoq, nsgf_set(pset), nsgf_set(qset))
    1294              : 
    1295          324 :             DEALLOCATE (work)
    1296              :          END DO !qset
    1297              :       END DO !pset
    1298              : 
    1299              : !  Inverting (to M^-1)
    1300           54 :       CALL invmat_symm(metric)
    1301              : 
    1302           54 :       IF (.NOT. xas_tdp_control%do_ri_metric) THEN
    1303              : 
    1304              :          !If no metric, then x_pot = ri_m_pot and (P|Q)^-1 = M^-1 (V-approximation)
    1305          144 :          ALLOCATE (xas_tdp_env%ri_inv_ex(nsgf, nsgf))
    1306       445396 :          xas_tdp_env%ri_inv_ex(:, :) = metric(:, :)
    1307           48 :          CALL coulomb_integral_cleanup(integral_context)
    1308           48 :          RETURN
    1309              : 
    1310              :       END IF
    1311              : 
    1312            6 :       CALL r12_fundamentals_init(2*maxl + 1, xas_tdp_control%x_potential, para_env)
    1313              : 
    1314              : !  Compute the proper exchange 2-center
    1315           18 :       ALLOCATE (pq(nsgf, nsgf))
    1316            6 :       pq = 0.0_dp
    1317              : 
    1318           18 :       DO pset = 1, nset
    1319           12 :          ncop = npgf_set(pset)*ncoset(lmax(pset))
    1320           12 :          sgfp = first_sgf(1, pset)
    1321           12 :          egfp = sgfp + nsgf_set(pset) - 1
    1322              : 
    1323           42 :          DO qset = 1, nset
    1324           24 :             ncoq = npgf_set(qset)*ncoset(lmax(qset))
    1325           24 :             sgfq = first_sgf(1, qset)
    1326           24 :             egfq = sgfq + nsgf_set(qset) - 1
    1327              : 
    1328           96 :             ALLOCATE (work(ncop, ncoq))
    1329           24 :             work = 0.0_dp
    1330              : 
    1331              :             CALL compute_coulomb_2c(integral_context, lmin(pset), lmax(pset), lmin(qset), lmax(qset), &
    1332              :                                    npgf_set(pset), npgf_set(qset), zet(:, pset), zet(:, qset), rpgf(:, pset), rpgf(:, qset), r, r, &
    1333           24 :                                     work, xas_tdp_control%x_potential, dab=0.0_dp)
    1334              : 
    1335              :             CALL ab_contract(pq(sgfp:egfp, sgfq:egfq), work, sphi(:, sgfp:), sphi(:, sgfq:), &
    1336           24 :                              ncop, ncoq, nsgf_set(pset), nsgf_set(qset))
    1337              : 
    1338           36 :             DEALLOCATE (work)
    1339              :          END DO !qset
    1340              :       END DO !pset
    1341              : 
    1342              : !  Compute and store M^-1 (P|Q) M^-1
    1343           18 :       ALLOCATE (xas_tdp_env%ri_inv_ex(nsgf, nsgf))
    1344        22698 :       xas_tdp_env%ri_inv_ex = 0.0_dp
    1345              : 
    1346              :       CALL dgemm('N', 'N', nsgf, nsgf, nsgf, 1.0_dp, metric, nsgf, pq, nsgf, &
    1347            6 :                  0.0_dp, xas_tdp_env%ri_inv_ex, nsgf)
    1348              :       CALL dgemm('N', 'N', nsgf, nsgf, nsgf, 1.0_dp, xas_tdp_env%ri_inv_ex, nsgf, metric, nsgf, &
    1349            6 :                  0.0_dp, pq, nsgf)
    1350        22698 :       xas_tdp_env%ri_inv_ex(:, :) = pq(:, :)
    1351              : 
    1352            6 :       CALL coulomb_integral_cleanup(integral_context)
    1353              : 
    1354          162 :    END SUBROUTINE compute_ri_exchange2_int
    1355              : 
    1356              : ! **************************************************************************************************
    1357              : !> \brief Computes the two-center Coulomb integral needed for the RI in kernel calculation. Stores
    1358              : !>        the integrals (P|Q)^-1 in the xas_tdp_env as global (small) arrays. Does that for a given
    1359              : !>        excited kind
    1360              : !> \param ex_kind ...
    1361              : !> \param xas_tdp_env ...
    1362              : !> \param xas_tdp_control ...
    1363              : !> \param qs_env ...
    1364              : ! **************************************************************************************************
    1365           74 :    SUBROUTINE compute_ri_coulomb2_int(ex_kind, xas_tdp_env, xas_tdp_control, qs_env)
    1366              : 
    1367              :       INTEGER, INTENT(IN)                                :: ex_kind
    1368              :       TYPE(xas_tdp_env_type), POINTER                    :: xas_tdp_env
    1369              :       TYPE(xas_tdp_control_type), POINTER                :: xas_tdp_control
    1370              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1371              : 
    1372              :       INTEGER                                            :: nsgf
    1373              :       TYPE(gto_basis_set_type), POINTER                  :: ri_basis
    1374           74 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1375              : 
    1376           74 :       NULLIFY (ri_basis, qs_kind_set)
    1377              : 
    1378              : !  Initialization
    1379           74 :       CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set)
    1380           74 :       IF (ASSOCIATED(xas_tdp_env%ri_inv_coul)) THEN
    1381            8 :          DEALLOCATE (xas_tdp_env%ri_inv_coul)
    1382              :       END IF
    1383              : 
    1384              : !  Get the RI basis of interest and its quantum numbers
    1385           74 :       CALL get_qs_kind(qs_kind_set(ex_kind), basis_set=ri_basis, basis_type="RI_XAS")
    1386           74 :       CALL get_gto_basis_set(ri_basis, nsgf=nsgf)
    1387          296 :       ALLOCATE (xas_tdp_env%ri_inv_coul(nsgf, nsgf))
    1388       660186 :       xas_tdp_env%ri_inv_coul = 0.0_dp
    1389              : 
    1390           74 :       IF (.NOT. xas_tdp_control%is_periodic) THEN
    1391              :          CALL int_operators_r12_ab_os(r12_operator=operator_coulomb, vab=xas_tdp_env%ri_inv_coul, &
    1392              :                                       rab=[0.0_dp, 0.0_dp, 0.0_dp], fba=ri_basis, fbb=ri_basis, &
    1393           58 :                                       calculate_forces=.FALSE.)
    1394           58 :          CPASSERT(ASSOCIATED(xas_tdp_control))
    1395              :       ELSE
    1396           16 :          CALL periodic_ri_coulomb2(xas_tdp_env%ri_inv_coul, ri_basis, qs_env)
    1397              :       END IF
    1398              : 
    1399              : !  Inverting
    1400           74 :       CALL invmat_symm(xas_tdp_env%ri_inv_coul)
    1401              : 
    1402          148 :    END SUBROUTINE compute_ri_coulomb2_int
    1403              : 
    1404              : ! **************************************************************************************************
    1405              : !> \brief Computes the two-center inverse coulomb integral in the case of PBCs
    1406              : !> \param ri_coul2 ...
    1407              : !> \param ri_basis ...
    1408              : !> \param qs_env ...
    1409              : ! **************************************************************************************************
    1410           16 :    SUBROUTINE periodic_ri_coulomb2(ri_coul2, ri_basis, qs_env)
    1411              : 
    1412              :       REAL(dp), DIMENSION(:, :), INTENT(INOUT)           :: ri_coul2
    1413              :       TYPE(gto_basis_set_type), POINTER                  :: ri_basis
    1414              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1415              : 
    1416              :       INTEGER                                            :: maxco, ncop, ncoq, nset, op, oq, ppgf, &
    1417              :                                                             pset, qpgf, qset, sgfp, sgfq
    1418           32 :       INTEGER, DIMENSION(:), POINTER                     :: lmax, lmin, npgf, nsgf
    1419           16 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgf
    1420              :       REAL(dp)                                           :: r(3)
    1421           16 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: hpq
    1422           16 :       REAL(dp), DIMENSION(:, :), POINTER                 :: sphi, zet
    1423              :       TYPE(cell_type), POINTER                           :: cell
    1424          416 :       TYPE(cp_eri_mme_param)                             :: mme_param
    1425              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1426           16 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1427              : 
    1428           16 :       NULLIFY (cell, qs_kind_set, lmin, lmax, nsgf, npgf, zet, sphi, first_sgf)
    1429              : 
    1430              :       ! Use eri_mme for this. Don't want to annoy the user with a full input section just for this
    1431              :       ! tiny bit => initialize our own eri_mme section with the defaults
    1432              : 
    1433           16 :       CALL get_qs_env(qs_env, cell=cell, qs_kind_set=qs_kind_set, para_env=para_env)
    1434              : 
    1435              :       CALL eri_mme_init(mme_param%par, n_minimax=20, cutoff=300.0_dp, do_calib_cutoff=.TRUE., &
    1436              :                         cutoff_min=10.0_dp, cutoff_max=10000.0_dp, cutoff_eps=0.01_dp, &
    1437              :                         cutoff_delta=0.9_dp, sum_precision=1.0E-12_dp, debug=.FALSE., &
    1438              :                         debug_delta=1.0E-6_dp, debug_nsum=1000000, unit_nr=0, print_calib=.FALSE., &
    1439           16 :                         do_error_est=.FALSE.)
    1440           16 :       mme_param%do_calib = .TRUE.
    1441              : 
    1442           16 :       CALL cp_eri_mme_set_params(mme_param, cell, qs_kind_set, basis_type_1="RI_XAS", para_env=para_env)
    1443              : 
    1444              :       CALL get_gto_basis_set(ri_basis, lmax=lmax, npgf=npgf, zet=zet, lmin=lmin, nset=nset, &
    1445           16 :                              nsgf_set=nsgf, sphi=sphi, first_sgf=first_sgf, maxco=maxco)
    1446              : 
    1447           16 :       r = 0.0_dp
    1448           64 :       ALLOCATE (hpq(nset*maxco, nset*maxco))
    1449           16 :       hpq = 0.0_dp
    1450              : 
    1451           48 :       DO pset = 1, nset
    1452           32 :          ncop = npgf(pset)*ncoset(lmax(pset))
    1453           32 :          sgfp = first_sgf(1, pset)
    1454              : 
    1455          112 :          DO qset = 1, nset
    1456           64 :             ncoq = npgf(qset)*ncoset(lmax(qset))
    1457           64 :             sgfq = first_sgf(1, qset)
    1458              : 
    1459          608 :             DO ppgf = 1, npgf(pset)
    1460          544 :                op = (pset - 1)*maxco + (ppgf - 1)*ncoset(lmax(pset))
    1461         5232 :                DO qpgf = 1, npgf(qset)
    1462         4624 :                   oq = (qset - 1)*maxco + (qpgf - 1)*ncoset(lmax(qset))
    1463              : 
    1464              :                   CALL eri_mme_2c_integrate(mme_param%par, lmin(pset), lmax(pset), lmin(qset), &
    1465              :                                             lmax(qset), zet(ppgf, pset), zet(qpgf, qset), r, hpq, &
    1466         5168 :                                             op, oq)
    1467              : 
    1468              :                END DO !qpgf
    1469              :             END DO ! ppgf
    1470              : 
    1471              :             !contraction into sgfs
    1472           64 :             op = (pset - 1)*maxco + 1
    1473           64 :             oq = (qset - 1)*maxco + 1
    1474              : 
    1475              :             CALL ab_contract(ri_coul2(sgfp:sgfp + nsgf(pset) - 1, sgfq:sgfq + nsgf(qset) - 1), &
    1476              :                              hpq(op:op + ncop - 1, oq:oq + ncoq - 1), sphi(:, sgfp:), sphi(:, sgfq:), &
    1477           96 :                              ncop, ncoq, nsgf(pset), nsgf(qset))
    1478              : 
    1479              :          END DO !qset
    1480              :       END DO !pset
    1481              : 
    1482              :       !celan-up
    1483           16 :       CALL eri_mme_release(mme_param%par)
    1484              : 
    1485           64 :    END SUBROUTINE periodic_ri_coulomb2
    1486              : 
    1487              : END MODULE xas_tdp_integrals
        

Generated by: LCOV version 2.0-1