-
Notifications
You must be signed in to change notification settings - Fork 1
/
xas_tdp_atom.F
3504 lines (2969 loc) · 155 KB
/
xas_tdp_atom.F
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief This module deals with all the integrals done on local atomic grids in xas_tdp. This is
!> mostly used to compute the xc kernel matrix elements wrt two RI basis elements (centered
!> on the same excited atom) <P|fxc(r)|Q>, where the kernel fxc is purely a function of the
!> ground state density and r. This is also used to compute the SOC matrix elements in the
!> orbital basis
! **************************************************************************************************
MODULE xas_tdp_atom
USE ai_contraction_sphi, ONLY: ab_contract
USE atom_operators, ONLY: calculate_model_potential
USE basis_set_types, ONLY: get_gto_basis_set,&
gto_basis_set_p_type,&
gto_basis_set_type
USE cell_types, ONLY: cell_type,&
pbc
USE cp_array_utils, ONLY: cp_1d_i_p_type,&
cp_1d_r_p_type,&
cp_2d_r_p_type,&
cp_3d_r_p_type
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_control_types, ONLY: dft_control_type,&
qs_control_type
USE cp_dbcsr_api, ONLY: &
dbcsr_copy, dbcsr_create, dbcsr_distribution_get, dbcsr_distribution_new, &
dbcsr_distribution_release, dbcsr_distribution_type, dbcsr_filter, dbcsr_finalize, &
dbcsr_get_block_p, dbcsr_get_stored_coordinates, dbcsr_iterator_blocks_left, &
dbcsr_iterator_next_block, dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, &
dbcsr_p_type, dbcsr_put_block, dbcsr_release, dbcsr_replicate_all, dbcsr_set, dbcsr_type, &
dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, dbcsr_type_symmetric
USE cp_dbcsr_cholesky, ONLY: cp_dbcsr_cholesky_decompose,&
cp_dbcsr_cholesky_invert
USE cp_dbcsr_operations, ONLY: dbcsr_deallocate_matrix_set
USE cp_log_handling, ONLY: cp_logger_get_default_io_unit
USE dbt_api, ONLY: dbt_destroy,&
dbt_get_block,&
dbt_iterator_blocks_left,&
dbt_iterator_next_block,&
dbt_iterator_start,&
dbt_iterator_stop,&
dbt_iterator_type,&
dbt_type
USE input_constants, ONLY: do_potential_id
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_type
USE kinds, ONLY: default_string_length,&
dp
USE lebedev, ONLY: deallocate_lebedev_grids,&
get_number_of_lebedev_grid,&
init_lebedev_grids,&
lebedev_grid
USE libint_2c_3c, ONLY: libint_potential_type
USE mathlib, ONLY: get_diag,&
invmat_symm
USE memory_utilities, ONLY: reallocate
USE message_passing, ONLY: mp_comm_type,&
mp_para_env_type,&
mp_request_type,&
mp_waitall
USE orbital_pointers, ONLY: indco,&
indso,&
nco,&
ncoset,&
nso,&
nsoset
USE orbital_transformation_matrices, ONLY: orbtramat
USE particle_methods, ONLY: get_particle_set
USE particle_types, ONLY: particle_type
USE physcon, ONLY: c_light_au
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_grid_atom, ONLY: allocate_grid_atom,&
create_grid_atom,&
grid_atom_type
USE qs_harmonics_atom, ONLY: allocate_harmonics_atom,&
create_harmonics_atom,&
get_maxl_CG,&
get_none0_cg_list,&
harmonics_atom_type
USE qs_integral_utils, ONLY: basis_set_list_setup
USE qs_kind_types, ONLY: get_qs_kind,&
get_qs_kind_set,&
qs_kind_type
USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type,&
release_neighbor_list_sets
USE qs_overlap, ONLY: build_overlap_matrix_simple
USE qs_rho_types, ONLY: qs_rho_get,&
qs_rho_type
USE qs_tddfpt2_soc_types, ONLY: soc_atom_env_type
USE spherical_harmonics, ONLY: clebsch_gordon,&
clebsch_gordon_deallocate,&
clebsch_gordon_init
USE util, ONLY: get_limit,&
sort_unique
USE xas_tdp_integrals, ONLY: build_xas_tdp_3c_nl,&
build_xas_tdp_ovlp_nl,&
create_pqX_tensor,&
fill_pqx_tensor
USE xas_tdp_types, ONLY: batch_info_type,&
get_proc_batch_sizes,&
release_batch_info,&
xas_atom_env_type,&
xas_tdp_control_type,&
xas_tdp_env_type
USE xc, ONLY: divide_by_norm_drho
USE xc_atom, ONLY: xc_rho_set_atom_update
USE xc_derivative_desc, ONLY: deriv_norm_drho,&
deriv_norm_drhoa,&
deriv_norm_drhob,&
deriv_rhoa,&
deriv_rhob
USE xc_derivative_set_types, ONLY: xc_derivative_set_type,&
xc_dset_create,&
xc_dset_get_derivative,&
xc_dset_release
USE xc_derivative_types, ONLY: xc_derivative_get,&
xc_derivative_type
USE xc_derivatives, ONLY: xc_functionals_eval,&
xc_functionals_get_needs
USE xc_rho_cflags_types, ONLY: xc_rho_cflags_type
USE xc_rho_set_types, ONLY: xc_rho_set_create,&
xc_rho_set_release,&
xc_rho_set_type
!$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'xas_tdp_atom'
PUBLIC :: init_xas_atom_env, &
integrate_fxc_atoms, &
integrate_soc_atoms, &
calculate_density_coeffs, &
compute_sphi_so, &
truncate_radial_grid, &
init_xas_atom_grid_harmo
CONTAINS
! **************************************************************************************************
!> \brief Initializes a xas_atom_env type given the qs_enxas_atom_env, qs_envv
!> \param xas_atom_env the xas_atom_env to initialize
!> \param xas_tdp_env ...
!> \param xas_tdp_control ...
!> \param qs_env ...
!> \param ltddfpt ...
! **************************************************************************************************
SUBROUTINE init_xas_atom_env(xas_atom_env, xas_tdp_env, xas_tdp_control, qs_env, ltddfpt)
TYPE(xas_atom_env_type), POINTER :: xas_atom_env
TYPE(xas_tdp_env_type), POINTER :: xas_tdp_env
TYPE(xas_tdp_control_type), POINTER :: xas_tdp_control
TYPE(qs_environment_type), POINTER :: qs_env
LOGICAL, OPTIONAL :: ltddfpt
CHARACTER(len=*), PARAMETER :: routineN = 'init_xas_atom_env'
INTEGER :: handle, ikind, natom, nex_atoms, &
nex_kinds, nkind, nspins
LOGICAL :: do_xc
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
NULLIFY (qs_kind_set, particle_set)
CALL timeset(routineN, handle)
! Initializing the type
CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, natom=natom, particle_set=particle_set)
nkind = SIZE(qs_kind_set)
nex_kinds = xas_tdp_env%nex_kinds
nex_atoms = xas_tdp_env%nex_atoms
do_xc = xas_tdp_control%do_xc
IF (PRESENT(ltddfpt) .AND. ltddfpt) do_xc = .FALSE.
nspins = 1; IF (xas_tdp_control%do_uks .OR. xas_tdp_control%do_roks) nspins = 2
xas_atom_env%nspins = nspins
xas_atom_env%ri_radius = xas_tdp_control%ri_radius
ALLOCATE (xas_atom_env%grid_atom_set(nkind))
ALLOCATE (xas_atom_env%harmonics_atom_set(nkind))
ALLOCATE (xas_atom_env%ri_sphi_so(nkind))
ALLOCATE (xas_atom_env%orb_sphi_so(nkind))
IF (do_xc) THEN
ALLOCATE (xas_atom_env%gr(nkind))
ALLOCATE (xas_atom_env%ga(nkind))
ALLOCATE (xas_atom_env%dgr1(nkind))
ALLOCATE (xas_atom_env%dgr2(nkind))
ALLOCATE (xas_atom_env%dga1(nkind))
ALLOCATE (xas_atom_env%dga2(nkind))
END IF
xas_atom_env%excited_atoms => xas_tdp_env%ex_atom_indices
xas_atom_env%excited_kinds => xas_tdp_env%ex_kind_indices
! Allocate and initialize the atomic grids and harmonics
CALL init_xas_atom_grid_harmo(xas_atom_env, xas_tdp_control%grid_info, do_xc, qs_env)
! Compute the contraction coefficients for spherical orbitals
DO ikind = 1, nkind
NULLIFY (xas_atom_env%orb_sphi_so(ikind)%array, xas_atom_env%ri_sphi_so(ikind)%array)
CALL compute_sphi_so(ikind, "ORB", xas_atom_env%orb_sphi_so(ikind)%array, qs_env)
IF (ANY(xas_atom_env%excited_kinds == ikind)) THEN
CALL compute_sphi_so(ikind, "RI_XAS", xas_atom_env%ri_sphi_so(ikind)%array, qs_env)
END IF
END DO !ikind
! Compute the coefficients to expand the density in the RI_XAS basis, if requested
IF (do_xc .AND. (.NOT. xas_tdp_control%xps_only)) THEN
CALL calculate_density_coeffs(xas_atom_env=xas_atom_env, qs_env=qs_env)
END IF
CALL timestop(handle)
END SUBROUTINE init_xas_atom_env
! **************************************************************************************************
!> \brief Initializes the atomic grids and harmonics for the RI atomic calculations
!> \param xas_atom_env ...
!> \param grid_info ...
!> \param do_xc Whether the xc kernel will ne computed on the atomic grids. If not, the harmonics
!> are built for the orbital basis for all kinds.
!> \param qs_env ...
!> \note Largely inspired by init_rho_atom subroutine
! **************************************************************************************************
SUBROUTINE init_xas_atom_grid_harmo(xas_atom_env, grid_info, do_xc, qs_env)
TYPE(xas_atom_env_type), POINTER :: xas_atom_env
CHARACTER(len=default_string_length), &
DIMENSION(:, :), POINTER :: grid_info
LOGICAL, INTENT(IN) :: do_xc
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(LEN=default_string_length) :: kind_name
INTEGER :: igrid, ikind, il, iso, iso1, iso2, l1, l1l2, l2, la, lc1, lc2, lcleb, ll, llmax, &
lp, m1, m2, max_s_harm, max_s_set, maxl, maxlgto, maxs, mm, mp, na, nr, quadrature, stat
REAL(dp) :: kind_radius
REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: rga
REAL(dp), DIMENSION(:, :, :), POINTER :: my_CG
TYPE(dft_control_type), POINTER :: dft_control
TYPE(grid_atom_type), POINTER :: grid_atom
TYPE(gto_basis_set_type), POINTER :: tmp_basis
TYPE(harmonics_atom_type), POINTER :: harmonics
TYPE(qs_control_type), POINTER :: qs_control
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
NULLIFY (my_CG, qs_kind_set, tmp_basis, grid_atom, harmonics, qs_control, dft_control)
! Initialization of some integer for the CG coeff generation
CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, dft_control=dft_control)
IF (do_xc) THEN
CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto, basis_type="RI_XAS")
ELSE
CALL get_qs_kind_set(qs_kind_set, maxlgto=maxlgto, basis_type="ORB")
END IF
qs_control => dft_control%qs_control
!maximum expansion
llmax = 2*maxlgto
max_s_harm = nsoset(llmax)
max_s_set = nsoset(maxlgto)
lcleb = llmax
! Allocate and compute the CG coeffs (copied from init_rho_atom)
CALL clebsch_gordon_init(lcleb)
CALL reallocate(my_CG, 1, max_s_set, 1, max_s_set, 1, max_s_harm)
ALLOCATE (rga(lcleb, 2))
DO lc1 = 0, maxlgto
DO iso1 = nsoset(lc1 - 1) + 1, nsoset(lc1)
l1 = indso(1, iso1)
m1 = indso(2, iso1)
DO lc2 = 0, maxlgto
DO iso2 = nsoset(lc2 - 1) + 1, nsoset(lc2)
l2 = indso(1, iso2)
m2 = indso(2, iso2)
CALL clebsch_gordon(l1, m1, l2, m2, rga)
IF (l1 + l2 > llmax) THEN
l1l2 = llmax
ELSE
l1l2 = l1 + l2
END IF
mp = m1 + m2
mm = m1 - m2
IF (m1*m2 < 0 .OR. (m1*m2 == 0 .AND. (m1 < 0 .OR. m2 < 0))) THEN
mp = -ABS(mp)
mm = -ABS(mm)
ELSE
mp = ABS(mp)
mm = ABS(mm)
END IF
DO lp = MOD(l1 + l2, 2), l1l2, 2
il = lp/2 + 1
IF (ABS(mp) <= lp) THEN
IF (mp >= 0) THEN
iso = nsoset(lp - 1) + lp + 1 + mp
ELSE
iso = nsoset(lp - 1) + lp + 1 - ABS(mp)
END IF
my_CG(iso1, iso2, iso) = rga(il, 1)
END IF
IF (mp /= mm .AND. ABS(mm) <= lp) THEN
IF (mm >= 0) THEN
iso = nsoset(lp - 1) + lp + 1 + mm
ELSE
iso = nsoset(lp - 1) + lp + 1 - ABS(mm)
END IF
my_CG(iso1, iso2, iso) = rga(il, 2)
END IF
END DO
END DO ! iso2
END DO ! lc2
END DO ! iso1
END DO ! lc1
DEALLOCATE (rga)
CALL clebsch_gordon_deallocate()
! Create the Lebedev grids and compute the spherical harmonics
CALL init_lebedev_grids()
quadrature = qs_control%gapw_control%quadrature
DO ikind = 1, SIZE(xas_atom_env%grid_atom_set)
! Allocate the grid and the harmonics for this kind
NULLIFY (xas_atom_env%grid_atom_set(ikind)%grid_atom)
NULLIFY (xas_atom_env%harmonics_atom_set(ikind)%harmonics_atom)
CALL allocate_grid_atom(xas_atom_env%grid_atom_set(ikind)%grid_atom)
CALL allocate_harmonics_atom(xas_atom_env%harmonics_atom_set(ikind)%harmonics_atom)
NULLIFY (grid_atom, harmonics)
grid_atom => xas_atom_env%grid_atom_set(ikind)%grid_atom
harmonics => xas_atom_env%harmonics_atom_set(ikind)%harmonics_atom
! Initialize some integers
CALL get_qs_kind(qs_kind_set(ikind), ngrid_rad=nr, ngrid_ang=na, name=kind_name)
!take the grid dimension given as input, if none, take the GAPW ones above
IF (ANY(grid_info == kind_name)) THEN
DO igrid = 1, SIZE(grid_info, 1)
IF (grid_info(igrid, 1) == kind_name) THEN
!hack to convert string into integer
READ (grid_info(igrid, 2), *, iostat=stat) na
IF (stat .NE. 0) CPABORT("The 'na' value for the GRID keyword must be an integer")
READ (grid_info(igrid, 3), *, iostat=stat) nr
IF (stat .NE. 0) CPABORT("The 'nr' value for the GRID keyword must be an integer")
EXIT
END IF
END DO
ELSE IF (do_xc .AND. ANY(xas_atom_env%excited_kinds == ikind)) THEN
!need good integration grids for the xc kernel, but taking the default GAPW grid
CPWARN("The default (and possibly small) GAPW grid is being used for one excited KIND")
END IF
ll = get_number_of_lebedev_grid(n=na)
na = lebedev_grid(ll)%n
la = lebedev_grid(ll)%l
grid_atom%ng_sphere = na
grid_atom%nr = nr
! If this is an excited kind, create the harmonics with the RI_XAS basis, otherwise the ORB
IF (ANY(xas_atom_env%excited_kinds == ikind) .AND. do_xc) THEN
CALL get_qs_kind(qs_kind_set(ikind), basis_set=tmp_basis, basis_type="RI_XAS")
ELSE
CALL get_qs_kind(qs_kind_set(ikind), basis_set=tmp_basis, basis_type="ORB")
END IF
CALL get_gto_basis_set(gto_basis_set=tmp_basis, maxl=maxl, kind_radius=kind_radius)
CALL create_grid_atom(grid_atom, nr, na, llmax, ll, quadrature)
CALL truncate_radial_grid(grid_atom, kind_radius)
maxs = nsoset(maxl)
CALL create_harmonics_atom(harmonics, &
my_CG, na, llmax, maxs, max_s_harm, ll, grid_atom%wa, &
grid_atom%azi, grid_atom%pol)
CALL get_maxl_CG(harmonics, tmp_basis, llmax, max_s_harm)
END DO
CALL deallocate_lebedev_grids()
DEALLOCATE (my_CG)
END SUBROUTINE init_xas_atom_grid_harmo
! **************************************************************************************************
!> \brief Reduces the radial extension of an atomic grid such that it only covers a given radius
!> \param grid_atom the atomic grid from which we truncate the radial part
!> \param max_radius the maximal radial extension of the resulting grid
!> \note Since the RI density used for <P|fxc|Q> is only of quality close to the atom, and the
!> integrand only non-zero within the radius of the gaussian P,Q. One can reduce the grid
!> extansion to the largest radius of the RI basis set
! **************************************************************************************************
SUBROUTINE truncate_radial_grid(grid_atom, max_radius)
TYPE(grid_atom_type), POINTER :: grid_atom
REAL(dp), INTENT(IN) :: max_radius
INTEGER :: first_ir, ir, llmax_p1, na, new_nr, nr
nr = grid_atom%nr
na = grid_atom%ng_sphere
llmax_p1 = SIZE(grid_atom%rad2l, 2) - 1
! Find the index corresponding to the limiting radius (small ir => large radius)
DO ir = 1, nr
IF (grid_atom%rad(ir) < max_radius) THEN
first_ir = ir
EXIT
END IF
END DO
new_nr = nr - first_ir + 1
! Reallcoate everything that depends on r
grid_atom%nr = new_nr
grid_atom%rad(1:new_nr) = grid_atom%rad(first_ir:nr)
grid_atom%rad2(1:new_nr) = grid_atom%rad2(first_ir:nr)
grid_atom%wr(1:new_nr) = grid_atom%wr(first_ir:nr)
grid_atom%weight(:, 1:new_nr) = grid_atom%weight(:, first_ir:nr)
grid_atom%rad2l(1:new_nr, :) = grid_atom%rad2l(first_ir:nr, :)
grid_atom%oorad2l(1:new_nr, :) = grid_atom%oorad2l(first_ir:nr, :)
CALL reallocate(grid_atom%rad, 1, new_nr)
CALL reallocate(grid_atom%rad2, 1, new_nr)
CALL reallocate(grid_atom%wr, 1, new_nr)
CALL reallocate(grid_atom%weight, 1, na, 1, new_nr)
CALL reallocate(grid_atom%rad2l, 1, new_nr, 0, llmax_p1)
CALL reallocate(grid_atom%oorad2l, 1, new_nr, 0, llmax_p1)
END SUBROUTINE truncate_radial_grid
! **************************************************************************************************
!> \brief Computes the contraction coefficients to go from spherical orbitals to sgf for a given
!> atomic kind and a given basis type.
!> \param ikind the kind for which we compute the coefficients
!> \param basis_type the type of basis for which we compute
!> \param sphi_so where the new contraction coefficients are stored (not yet allocated)
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE compute_sphi_so(ikind, basis_type, sphi_so, qs_env)
INTEGER, INTENT(IN) :: ikind
CHARACTER(len=*), INTENT(IN) :: basis_type
REAL(dp), DIMENSION(:, :), POINTER :: sphi_so
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER :: ico, ipgf, iset, iso, l, lx, ly, lz, &
maxso, nset, sgfi, start_c, start_s
INTEGER, DIMENSION(:), POINTER :: lmax, lmin, npgf, nsgf_set
INTEGER, DIMENSION(:, :), POINTER :: first_sgf
REAL(dp) :: factor
REAL(dp), DIMENSION(:, :), POINTER :: sphi
TYPE(gto_basis_set_type), POINTER :: basis
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
NULLIFY (basis, lmax, lmin, npgf, nsgf_set, qs_kind_set, first_sgf, sphi)
CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set)
CALL get_qs_kind(qs_kind_set(ikind), basis_set=basis, basis_type=basis_type)
CALL get_gto_basis_set(basis, lmax=lmax, nset=nset, npgf=npgf, maxso=maxso, lmin=lmin, &
nsgf_set=nsgf_set, sphi=sphi, first_sgf=first_sgf)
ALLOCATE (sphi_so(maxso, SUM(nsgf_set)))
sphi_so = 0.0_dp
DO iset = 1, nset
sgfi = first_sgf(1, iset)
DO ipgf = 1, npgf(iset)
start_s = (ipgf - 1)*nsoset(lmax(iset))
start_c = (ipgf - 1)*ncoset(lmax(iset))
DO l = lmin(iset), lmax(iset)
DO iso = 1, nso(l)
DO ico = 1, nco(l)
lx = indco(1, ico + ncoset(l - 1))
ly = indco(2, ico + ncoset(l - 1))
lz = indco(3, ico + ncoset(l - 1))
!MK factor = orbtramat(l)%s2c(iso, ico) &
!MK *SQRT(4.0_dp*pi/dfac(2*l + 1)*dfac(2*lx - 1)*dfac(2*ly - 1)*dfac(2*lz - 1))
factor = orbtramat(l)%slm_inv(iso, ico)
sphi_so(start_s + nsoset(l - 1) + iso, sgfi:sgfi + nsgf_set(iset) - 1) = &
sphi_so(start_s + nsoset(l - 1) + iso, sgfi:sgfi + nsgf_set(iset) - 1) + &
factor*sphi(start_c + ncoset(l - 1) + ico, sgfi:sgfi + nsgf_set(iset) - 1)
END DO ! ico
END DO ! iso
END DO ! l
END DO ! ipgf
END DO ! iset
END SUBROUTINE compute_sphi_so
! **************************************************************************************************
!> \brief Find the neighbors of a given set of atoms based on the non-zero blocks of a provided
!> overlap matrix. Optionally returns an array containing the indices of all involved atoms
!> (the given subset plus all their neighbors, without repetition) AND/OR an array of arrays
!> providing the indices of the neighbors of each input atom.
!> \param base_atoms the set of atoms for which we search neighbors
!> \param mat_s the overlap matrix used to find neighbors
!> \param radius the cutoff radius after which atoms are not considered neighbors
!> \param qs_env ...
!> \param all_neighbors the array uniquely contatining all indices of all atoms involved
!> \param neighbor_set array of arrays containing the neighbors of all given atoms
! **************************************************************************************************
SUBROUTINE find_neighbors(base_atoms, mat_s, radius, qs_env, all_neighbors, neighbor_set)
INTEGER, DIMENSION(:), INTENT(INOUT) :: base_atoms
TYPE(dbcsr_type), INTENT(IN) :: mat_s
REAL(dp) :: radius
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER, DIMENSION(:), OPTIONAL, POINTER :: all_neighbors
TYPE(cp_1d_i_p_type), DIMENSION(:), OPTIONAL, &
POINTER :: neighbor_set
INTEGER :: blk, i, iat, ibase, iblk, jblk, mepos, &
natom, nb, nbase
INTEGER, ALLOCATABLE, DIMENSION(:) :: blk_to_base, inb, who_is_there
INTEGER, ALLOCATABLE, DIMENSION(:, :) :: n_neighbors
LOGICAL, ALLOCATABLE, DIMENSION(:) :: is_base_atom
REAL(dp) :: dist2, rad2, ri(3), rij(3), rj(3)
TYPE(cell_type), POINTER :: cell
TYPE(cp_1d_i_p_type), DIMENSION(:), POINTER :: my_neighbor_set
TYPE(dbcsr_iterator_type) :: iter
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
NULLIFY (particle_set, para_env, my_neighbor_set, cell)
! Initialization
CALL get_qs_env(qs_env, para_env=para_env, natom=natom, particle_set=particle_set, cell=cell)
mepos = para_env%mepos
nbase = SIZE(base_atoms)
!work with the neighbor_set structure, see at the end if we keep it
ALLOCATE (my_neighbor_set(nbase))
rad2 = radius**2
ALLOCATE (blk_to_base(natom), is_base_atom(natom))
blk_to_base = 0; is_base_atom = .FALSE.
DO ibase = 1, nbase
blk_to_base(base_atoms(ibase)) = ibase
is_base_atom(base_atoms(ibase)) = .TRUE.
END DO
! First loop over S => count the number of neighbors
ALLOCATE (n_neighbors(nbase, 0:para_env%num_pe - 1))
n_neighbors = 0
CALL dbcsr_iterator_start(iter, mat_s)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, row=iblk, column=jblk, blk=blk)
!avoid self-neighbors
IF (iblk == jblk) CYCLE
!test distance
ri = pbc(particle_set(iblk)%r, cell)
rj = pbc(particle_set(jblk)%r, cell)
rij = pbc(ri, rj, cell)
dist2 = SUM(rij**2)
IF (dist2 > rad2) CYCLE
IF (is_base_atom(iblk)) THEN
ibase = blk_to_base(iblk)
n_neighbors(ibase, mepos) = n_neighbors(ibase, mepos) + 1
END IF
IF (is_base_atom(jblk)) THEN
ibase = blk_to_base(jblk)
n_neighbors(ibase, mepos) = n_neighbors(ibase, mepos) + 1
END IF
END DO !iter
CALL dbcsr_iterator_stop(iter)
CALL para_env%sum(n_neighbors)
! Allocate the neighbor_set arrays at the correct length
DO ibase = 1, nbase
ALLOCATE (my_neighbor_set(ibase)%array(SUM(n_neighbors(ibase, :))))
my_neighbor_set(ibase)%array = 0
END DO
! Loop a second time over S, this time fill the neighbors details
CALL dbcsr_iterator_start(iter, mat_s)
ALLOCATE (inb(nbase))
inb = 1
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, row=iblk, column=jblk, blk=blk)
IF (iblk == jblk) CYCLE
!test distance
ri = pbc(particle_set(iblk)%r, cell)
rj = pbc(particle_set(jblk)%r, cell)
rij = pbc(ri, rj, cell)
dist2 = SUM(rij**2)
IF (dist2 > rad2) CYCLE
IF (is_base_atom(iblk)) THEN
ibase = blk_to_base(iblk)
my_neighbor_set(ibase)%array(SUM(n_neighbors(ibase, 0:mepos - 1)) + inb(ibase)) = jblk
inb(ibase) = inb(ibase) + 1
END IF
IF (is_base_atom(jblk)) THEN
ibase = blk_to_base(jblk)
my_neighbor_set(ibase)%array(SUM(n_neighbors(ibase, 0:mepos - 1)) + inb(ibase)) = iblk
inb(ibase) = inb(ibase) + 1
END IF
END DO !iter
CALL dbcsr_iterator_stop(iter)
! Make sure the info is shared among the procs
DO ibase = 1, nbase
CALL para_env%sum(my_neighbor_set(ibase)%array)
END DO
! Gather all indices if asked for it
IF (PRESENT(all_neighbors)) THEN
ALLOCATE (who_is_there(natom))
who_is_there = 0
DO ibase = 1, nbase
who_is_there(base_atoms(ibase)) = 1
DO nb = 1, SIZE(my_neighbor_set(ibase)%array)
who_is_there(my_neighbor_set(ibase)%array(nb)) = 1
END DO
END DO
ALLOCATE (all_neighbors(natom))
i = 0
DO iat = 1, natom
IF (who_is_there(iat) == 1) THEN
i = i + 1
all_neighbors(i) = iat
END IF
END DO
CALL reallocate(all_neighbors, 1, i)
END IF
! If not asked for the neighbor set, deallocate it
IF (PRESENT(neighbor_set)) THEN
neighbor_set => my_neighbor_set
ELSE
DO ibase = 1, nbase
DEALLOCATE (my_neighbor_set(ibase)%array)
END DO
DEALLOCATE (my_neighbor_set)
END IF
END SUBROUTINE find_neighbors
! **************************************************************************************************
!> \brief Returns the RI inverse overlap for a subset of the RI_XAS matrix spaning a given
!> excited atom and its neighbors.
!> \param ri_sinv the inverse overlap as a dbcsr matrix
!> \param whole_s the whole RI overlap matrix
!> \param neighbors the indeces of the excited atom and their neighbors
!> \param idx_to_nb array telling where any atom can be found in neighbors (if there at all)
!> \param basis_set_ri the RI basis set list for all kinds
!> \param qs_env ...
!> \note It is assumed that the neighbors are sorted, the output matrix is assumed to be small and
!> is replicated on all processors
! **************************************************************************************************
SUBROUTINE get_exat_ri_sinv(ri_sinv, whole_s, neighbors, idx_to_nb, basis_set_ri, qs_env)
TYPE(dbcsr_type) :: ri_sinv, whole_s
INTEGER, DIMENSION(:), INTENT(IN) :: neighbors, idx_to_nb
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_ri
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'get_exat_ri_sinv'
INTEGER :: blk, dest, group_handle, handle, iat, &
ikind, inb, ir, is, jat, jnb, natom, &
nnb, npcols, nprows, source, tag
INTEGER, DIMENSION(:), POINTER :: col_dist, nsgf, row_dist
INTEGER, DIMENSION(:, :), POINTER :: pgrid
LOGICAL :: found_risinv, found_whole
LOGICAL, ALLOCATABLE, DIMENSION(:) :: is_neighbor
REAL(dp) :: ri(3), rij(3), rj(3)
REAL(dp), ALLOCATABLE, DIMENSION(:) :: radius
REAL(dp), DIMENSION(:, :), POINTER :: block_risinv, block_whole
TYPE(cell_type), POINTER :: cell
TYPE(cp_2d_r_p_type), ALLOCATABLE, DIMENSION(:) :: recv_buff, send_buff
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(dbcsr_distribution_type) :: sinv_dist
TYPE(dbcsr_distribution_type), POINTER :: dbcsr_dist
TYPE(dbcsr_iterator_type) :: iter
TYPE(mp_comm_type) :: group
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(mp_request_type), ALLOCATABLE, DIMENSION(:) :: recv_req, send_req
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
NULLIFY (pgrid, dbcsr_dist, row_dist, col_dist, nsgf, particle_set, block_whole, block_risinv)
NULLIFY (cell, para_env, blacs_env)
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env, dbcsr_dist=dbcsr_dist, particle_set=particle_set, natom=natom, &
para_env=para_env, blacs_env=blacs_env, cell=cell)
nnb = SIZE(neighbors)
ALLOCATE (nsgf(nnb), is_neighbor(natom), radius(nnb))
is_neighbor = .FALSE.
DO inb = 1, nnb
iat = neighbors(inb)
ikind = particle_set(iat)%atomic_kind%kind_number
CALL get_gto_basis_set(basis_set_ri(ikind)%gto_basis_set, nsgf=nsgf(inb), kind_radius=radius(inb))
is_neighbor(iat) = .TRUE.
END DO
!Create the ri_sinv matrix based on some arbitrary dbcsr_dist
CALL dbcsr_distribution_get(dbcsr_dist, group=group_handle, pgrid=pgrid, nprows=nprows, npcols=npcols)
CALL group%set_handle(group_handle)
ALLOCATE (row_dist(nnb), col_dist(nnb))
DO inb = 1, nnb
row_dist(inb) = MODULO(nprows - inb, nprows)
col_dist(inb) = MODULO(npcols - inb, npcols)
END DO
CALL dbcsr_distribution_new(sinv_dist, group=group_handle, pgrid=pgrid, row_dist=row_dist, &
col_dist=col_dist)
CALL dbcsr_create(matrix=ri_sinv, name="RI_SINV", matrix_type=dbcsr_type_symmetric, &
dist=sinv_dist, row_blk_size=nsgf, col_blk_size=nsgf)
!reserving the blocks in the correct pattern
DO inb = 1, nnb
ri = pbc(particle_set(neighbors(inb))%r, cell)
DO jnb = inb, nnb
!do the atom overlap ?
rj = pbc(particle_set(neighbors(jnb))%r, cell)
rij = pbc(ri, rj, cell)
IF (SUM(rij**2) > (radius(inb) + radius(jnb))**2) CYCLE
CALL dbcsr_get_stored_coordinates(ri_sinv, inb, jnb, blk)
IF (para_env%mepos == blk) THEN
ALLOCATE (block_risinv(nsgf(inb), nsgf(jnb)))
block_risinv = 0.0_dp
CALL dbcsr_put_block(ri_sinv, inb, jnb, block_risinv)
DEALLOCATE (block_risinv)
END IF
END DO
END DO
CALL dbcsr_finalize(ri_sinv)
CALL dbcsr_distribution_release(sinv_dist)
DEALLOCATE (row_dist, col_dist)
!prepare the send and recv buffers we will need for change of dist between the two matrices
!worst case scenario: all neighbors are on same procs => need to send nnb**2 messages
ALLOCATE (send_buff(nnb**2), recv_buff(nnb**2))
ALLOCATE (send_req(nnb**2), recv_req(nnb**2))
is = 0; ir = 0
!Loop over the whole RI overlap matrix and pick the blocks we need
CALL dbcsr_iterator_start(iter, whole_s)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, row=iat, column=jat, blk=blk)
CALL dbcsr_get_block_p(whole_s, iat, jat, block_whole, found_whole)
!only interested in neighbors
IF (.NOT. found_whole) CYCLE
IF (.NOT. is_neighbor(iat)) CYCLE
IF (.NOT. is_neighbor(jat)) CYCLE
inb = idx_to_nb(iat)
jnb = idx_to_nb(jat)
!If blocks are on the same proc for both matrices, simply copy
CALL dbcsr_get_block_p(ri_sinv, inb, jnb, block_risinv, found_risinv)
IF (found_risinv) THEN
CALL dcopy(nsgf(inb)*nsgf(jnb), block_whole, 1, block_risinv, 1)
ELSE
!send the block with unique tag to the proc where inb,jnb is in ri_sinv
CALL dbcsr_get_stored_coordinates(ri_sinv, inb, jnb, dest)
is = is + 1
send_buff(is)%array => block_whole
tag = natom*inb + jnb
CALL group%isend(msgin=send_buff(is)%array, dest=dest, request=send_req(is), tag=tag)
END IF
END DO !dbcsr iter
CALL dbcsr_iterator_stop(iter)
!Loop over ri_sinv and receive all those blocks
CALL dbcsr_iterator_start(iter, ri_sinv)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, row=inb, column=jnb, blk=blk)
CALL dbcsr_get_block_p(ri_sinv, inb, jnb, block_risinv, found_risinv)
IF (.NOT. found_risinv) CYCLE
iat = neighbors(inb)
jat = neighbors(jnb)
!If blocks are on the same proc on both matrices do nothing
CALL dbcsr_get_stored_coordinates(whole_s, iat, jat, source)
IF (para_env%mepos == source) CYCLE
tag = natom*inb + jnb
ir = ir + 1
recv_buff(ir)%array => block_risinv
CALL group%irecv(msgout=recv_buff(ir)%array, source=source, request=recv_req(ir), &
tag=tag)
END DO
CALL dbcsr_iterator_stop(iter)
!make sure that all comm is over before proceeding
CALL mp_waitall(send_req(1:is))
CALL mp_waitall(recv_req(1:ir))
!Invert. 2 cases: with or without neighbors. If no neighbors, easier to invert on one proc and
!avoid the whole fm to dbcsr to fm that is quite expensive
IF (nnb == 1) THEN
CALL dbcsr_get_block_p(ri_sinv, 1, 1, block_risinv, found_risinv)
IF (found_risinv) THEN
CALL invmat_symm(block_risinv)
END IF
ELSE
CALL cp_dbcsr_cholesky_decompose(ri_sinv, para_env=para_env, blacs_env=blacs_env)
CALL cp_dbcsr_cholesky_invert(ri_sinv, para_env=para_env, blacs_env=blacs_env, upper_to_full=.TRUE.)
CALL dbcsr_filter(ri_sinv, 1.E-10_dp) !make sure ri_sinv is sparse coming out of fm routines
END IF
CALL dbcsr_replicate_all(ri_sinv)
!clean-up
DEALLOCATE (nsgf)
CALL timestop(handle)
END SUBROUTINE get_exat_ri_sinv
! **************************************************************************************************
!> \brief Compute the coefficients to project the density on a partial RI_XAS basis
!> \param xas_atom_env ...
!> \param qs_env ...
!> \note The density is n = sum_ab P_ab*phi_a*phi_b, the RI basis covers the products of orbital sgfs
!> => n = sum_ab sum_cd P_ab (phi_a phi_b xi_c) S_cd^-1 xi_d
!> = sum_d coeff_d xi_d , where xi are the RI basis func.
!> In this case, with the partial RI projection, the RI basis is restricted to an excited atom
!> and its neighbors at a time. Leads to smaller overlap matrix to invert and less 3-center
!> overlap to compute. The procedure is repeated for each excited atom
! **************************************************************************************************
SUBROUTINE calculate_density_coeffs(xas_atom_env, qs_env)
TYPE(xas_atom_env_type), POINTER :: xas_atom_env
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'calculate_density_coeffs'
INTEGER :: exat, handle, i, iat, iatom, iex, inb, &
ind(3), ispin, jatom, jnb, katom, &
natom, nex, nkind, nnb, nspins, &
output_unit, ri_at
INTEGER, ALLOCATABLE, DIMENSION(:) :: blk_size_orb, blk_size_ri, idx_to_nb, &
neighbors
INTEGER, DIMENSION(:), POINTER :: all_ri_atoms
LOGICAL :: pmat_found, pmat_foundt, sinv_found, &
sinv_foundt, tensor_found, unique
REAL(dp) :: factor, prefac
REAL(dp), ALLOCATABLE, DIMENSION(:) :: work2
REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: work1
REAL(dp), ALLOCATABLE, DIMENSION(:, :, :) :: t_block
REAL(dp), DIMENSION(:, :), POINTER :: pmat_block, pmat_blockt, sinv_block, &
sinv_blockt
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(dbcsr_distribution_type), POINTER :: dbcsr_dist
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: overlap, rho_ao
TYPE(dbcsr_type) :: ri_sinv
TYPE(dbcsr_type), POINTER :: ri_mats
TYPE(dbt_iterator_type) :: iter
TYPE(dbt_type) :: pqX
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_orb, basis_set_ri
TYPE(libint_potential_type) :: pot
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: ab_list, ac_list, sab_ri
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_rho_type), POINTER :: rho
NULLIFY (qs_kind_set, basis_set_ri, basis_set_orb, ac_list, rho, rho_ao, sab_ri, ri_mats)
NULLIFY (particle_set, para_env, all_ri_atoms, overlap, pmat_blockt)
NULLIFY (blacs_env, pmat_block, ab_list, dbcsr_dist, sinv_block, sinv_blockt)
!Idea: We don't do a full RI here as it would be too expensive in many ways (inversion of a
! large matrix, many 3-center overlaps, density coefficients for all atoms, etc...)
! Instead, we go excited atom by excited atom and only do a RI expansion on its basis
! and that of its closest neighbors (defined through RI_RADIUS), such that we only have
! very small matrices to invert and only a few (abc) overlp integrals with c on the
! excited atom its neighbors. This is physically sound since we only need the density
! well defined on the excited atom grid as we do (aI|P)*(P|Q)^-1*(Q|fxc|R)*(R|S)^-1*(S|Jb)
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env, nkind=nkind, qs_kind_set=qs_kind_set, rho=rho, &
natom=natom, particle_set=particle_set, dbcsr_dist=dbcsr_dist, &
para_env=para_env, blacs_env=blacs_env)
nspins = xas_atom_env%nspins
nex = SIZE(xas_atom_env%excited_atoms)
CALL qs_rho_get(rho, rho_ao=rho_ao)
! Create the needed neighbor list and basis set lists.
ALLOCATE (basis_set_ri(nkind))
ALLOCATE (basis_set_orb(nkind))
CALL basis_set_list_setup(basis_set_ri, "RI_XAS", qs_kind_set)
CALL basis_set_list_setup(basis_set_orb, "ORB", qs_kind_set)
! Compute the RI overlap matrix on the whole system
CALL build_xas_tdp_ovlp_nl(sab_ri, basis_set_ri, basis_set_ri, qs_env)
CALL build_overlap_matrix_simple(qs_env%ks_env, overlap, basis_set_ri, basis_set_ri, sab_ri)
ri_mats => overlap(1)%matrix
CALL release_neighbor_list_sets(sab_ri)
! Get the neighbors of the excited atoms (= all the atoms where density coeffs are needed)
CALL find_neighbors(xas_atom_env%excited_atoms, ri_mats, xas_atom_env%ri_radius, &
qs_env, all_neighbors=all_ri_atoms, neighbor_set=xas_atom_env%exat_neighbors)
!keep in mind that double occupation is included in rho_ao in case of closed-shell
factor = 0.5_dp; IF (nspins == 2) factor = 1.0_dp
! Allocate space for the projected density coefficients. On all ri_atoms.
! Note: the sub-region where we project the density changes from excited atom to excited atom
! => need different sets of RI coeffs
ALLOCATE (blk_size_ri(natom))
CALL get_particle_set(particle_set, qs_kind_set, nsgf=blk_size_ri, basis=basis_set_ri)
ALLOCATE (xas_atom_env%ri_dcoeff(natom, nspins, nex))
DO iex = 1, nex
DO ispin = 1, nspins
DO iat = 1, natom
NULLIFY (xas_atom_env%ri_dcoeff(iat, ispin, iex)%array)
IF ((.NOT. ANY(xas_atom_env%exat_neighbors(iex)%array == iat)) &
.AND. (.NOT. xas_atom_env%excited_atoms(iex) == iat)) CYCLE
ALLOCATE (xas_atom_env%ri_dcoeff(iat, ispin, iex)%array(blk_size_ri(iat)))
xas_atom_env%ri_dcoeff(iat, ispin, iex)%array = 0.0_dp
END DO
END DO
END DO
output_unit = cp_logger_get_default_io_unit()
IF (output_unit > 0) THEN
WRITE (output_unit, FMT="(/,T7,A,/,T7,A)") &
"Excited atom, natoms in RI_REGION:", &
"---------------------------------"
END IF
!We go atom by atom, first computing the integrals themselves that we put into a tensor, then we do
!the contraction with the density. We do that in the original dist, which is optimized for overlap
ALLOCATE (blk_size_orb(natom))
CALL get_particle_set(particle_set, qs_kind_set, nsgf=blk_size_orb, basis=basis_set_orb)
DO iex = 1, nex
!get neighbors of current atom
exat = xas_atom_env%excited_atoms(iex)
nnb = 1 + SIZE(xas_atom_env%exat_neighbors(iex)%array)
ALLOCATE (neighbors(nnb))
neighbors(1) = exat
neighbors(2:nnb) = xas_atom_env%exat_neighbors(iex)%array(:)
CALL sort_unique(neighbors, unique)
!link the atoms to their position in neighbors
ALLOCATE (idx_to_nb(natom))
idx_to_nb = 0
DO inb = 1, nnb
idx_to_nb(neighbors(inb)) = inb
END DO
IF (output_unit > 0) THEN
WRITE (output_unit, FMT="(T7,I12,I21)") &
exat, nnb
END IF
!Get the neighbor lists for the overlap integrals (abc), centers c on the current
!excited atom and its neighbors defined by RI_RADIUS
CALL build_xas_tdp_ovlp_nl(ab_list, basis_set_orb, basis_set_orb, qs_env)
CALL build_xas_tdp_3c_nl(ac_list, basis_set_orb, basis_set_ri, do_potential_id, &
qs_env, excited_atoms=neighbors)
!Compute the 3-center overlap integrals
pot%potential_type = do_potential_id
CALL create_pqX_tensor(pqX, ab_list, ac_list, dbcsr_dist, blk_size_orb, blk_size_orb, &
blk_size_ri)