-
Notifications
You must be signed in to change notification settings - Fork 1
/
xas_tdp_integrals.F
1534 lines (1265 loc) · 72.1 KB
/
xas_tdp_integrals.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 3-center integrals machinery for the XAS_TDP method
!> \author A. Bussy (03.2020)
! **************************************************************************************************
MODULE xas_tdp_integrals
USE OMP_LIB, ONLY: omp_get_max_threads,&
omp_get_num_threads,&
omp_get_thread_num
USE ai_contraction_sphi, ONLY: ab_contract,&
abc_contract_xsmm
USE atomic_kind_types, ONLY: atomic_kind_type
USE basis_set_types, ONLY: get_gto_basis_set,&
gto_basis_set_p_type,&
gto_basis_set_type
USE cell_types, ONLY: cell_type
USE constants_operator, ONLY: operator_coulomb
USE cp_array_utils, ONLY: cp_1d_i_p_type,&
cp_2d_r_p_type
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_dbcsr_api, ONLY: dbcsr_distribution_get,&
dbcsr_distribution_release,&
dbcsr_distribution_type
USE cp_dbcsr_operations, ONLY: cp_dbcsr_dist2d_to_dist
USE cp_eri_mme_interface, ONLY: cp_eri_mme_param,&
cp_eri_mme_set_params
USE cp_files, ONLY: close_file,&
open_file
USE dbt_api, ONLY: &
dbt_create, dbt_distribution_destroy, dbt_distribution_new, dbt_distribution_type, &
dbt_finalize, dbt_pgrid_create, dbt_pgrid_destroy, dbt_pgrid_type, dbt_put_block, &
dbt_reserve_blocks, dbt_type
USE distribution_1d_types, ONLY: distribution_1d_type
USE distribution_2d_types, ONLY: distribution_2d_create,&
distribution_2d_type
USE eri_mme_integrate, ONLY: eri_mme_2c_integrate
USE eri_mme_types, ONLY: eri_mme_init,&
eri_mme_release
USE gamma, ONLY: init_md_ftable
USE generic_os_integrals, ONLY: int_operators_r12_ab_os
USE input_constants, ONLY: do_potential_coulomb,&
do_potential_id,&
do_potential_short,&
do_potential_truncated
USE input_section_types, ONLY: section_vals_val_get
USE kinds, ONLY: dp
USE libint_2c_3c, ONLY: cutoff_screen_factor,&
eri_2center,&
eri_3center,&
libint_potential_type
USE libint_wrapper, ONLY: cp_libint_cleanup_2eri,&
cp_libint_cleanup_3eri,&
cp_libint_init_2eri,&
cp_libint_init_3eri,&
cp_libint_set_contrdepth,&
cp_libint_t
USE mathlib, ONLY: invmat_symm
USE message_passing, ONLY: mp_comm_type,&
mp_para_env_type
USE molecule_types, ONLY: molecule_type
USE orbital_pointers, ONLY: ncoset
USE particle_methods, ONLY: get_particle_set
USE particle_types, ONLY: particle_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_integral_utils, ONLY: basis_set_list_setup
USE qs_kind_types, ONLY: get_qs_kind,&
qs_kind_type
USE qs_neighbor_list_types, ONLY: &
get_iterator_info, get_neighbor_list_set_p, neighbor_list_iterate, &
neighbor_list_iterator_create, neighbor_list_iterator_p_type, &
neighbor_list_iterator_release, neighbor_list_set_p_type, nl_set_sub_iterator, &
nl_sub_iterate, release_neighbor_list_sets
USE qs_neighbor_lists, ONLY: atom2d_build,&
atom2d_cleanup,&
build_neighbor_lists,&
local_atoms_type,&
pair_radius_setup
USE qs_o3c_types, ONLY: get_o3c_iterator_info,&
init_o3c_container,&
o3c_container_type,&
o3c_iterate,&
o3c_iterator_create,&
o3c_iterator_release,&
o3c_iterator_type,&
release_o3c_container
USE t_c_g0, ONLY: get_lmax_init,&
init
USE xas_tdp_types, ONLY: xas_tdp_control_type,&
xas_tdp_env_type
#if(__LIBXSMM)
USE libxsmm, ONLY: libxsmm_matcopy, &
libxsmm_otrans, &
libxsmm_ptr
#endif
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'xas_tdp_integrals'
PUBLIC :: create_pqX_tensor, fill_pqX_tensor, compute_ri_3c_coulomb, compute_ri_3c_exchange, &
build_xas_tdp_3c_nl, build_xas_tdp_ovlp_nl, get_opt_3c_dist2d, &
compute_ri_coulomb2_int, compute_ri_exchange2_int
CONTAINS
! **************************************************************************************************
!> \brief Prepares a tensor to hold 3-center integrals (pq|X), where p,q are distributed according
!> to the given 2d dbcsr distribution of the given . The third dimension of the tensor is
!> iteslf not distributed (i.e. the t_pgrid's third dimension has size 1). The blocks are
!> reserved according to the neighbor lists
!> \param pq_X the tensor to store the integrals
!> \param ab_nl the 1st and 2nd center neighbor list
!> \param ac_nl the 1st and 3rd center neighbor list
!> \param matrix_dist ...
!> \param blk_size_1 the block size in the first dimension
!> \param blk_size_2 the block size in the second dimension
!> \param blk_size_3 the block size in the third dimension
!> \param only_bc_same_center only keep block if, for the corresponding integral (ab|c), b and c
!> share the same center, i.e. r_bc = 0.0
! **************************************************************************************************
SUBROUTINE create_pqX_tensor(pq_X, ab_nl, ac_nl, matrix_dist, blk_size_1, blk_size_2, &
blk_size_3, only_bc_same_center)
TYPE(dbt_type), INTENT(OUT) :: pq_X
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: ab_nl, ac_nl
TYPE(dbcsr_distribution_type), INTENT(IN) :: matrix_dist
INTEGER, DIMENSION(:), INTENT(IN) :: blk_size_1, blk_size_2, blk_size_3
LOGICAL, INTENT(IN), OPTIONAL :: only_bc_same_center
CHARACTER(len=*), PARAMETER :: routineN = 'create_pqX_tensor'
INTEGER :: A, b, group_handle, handle, i, iatom, &
ikind, jatom, katom, kkind, nblk, &
nblk_3, nblk_per_thread, nkind
INTEGER, ALLOCATABLE, DIMENSION(:) :: idx1, idx2, idx3
INTEGER, DIMENSION(3) :: pdims
INTEGER, DIMENSION(:), POINTER :: col_dist, row_dist
INTEGER, DIMENSION(:, :), POINTER :: mat_pgrid
LOGICAL :: my_sort_bc, symmetric
REAL(dp), DIMENSION(3) :: rab, rac, rbc
TYPE(dbt_distribution_type) :: t_dist
TYPE(dbt_pgrid_type) :: t_pgrid
TYPE(mp_comm_type) :: group
TYPE(neighbor_list_iterator_p_type), &
DIMENSION(:), POINTER :: ab_iter, ac_iter
NULLIFY (ab_iter, ac_iter, col_dist, row_dist, mat_pgrid)
CALL timeset(routineN, handle)
my_sort_bc = .FALSE.
IF (PRESENT(only_bc_same_center)) my_sort_bc = only_bc_same_center
CALL get_neighbor_list_set_p(ab_nl, symmetric=symmetric)
CPASSERT(symmetric)
!get 2D distribution info from matrix_dist
CALL dbcsr_distribution_get(matrix_dist, pgrid=mat_pgrid, group=group_handle, &
row_dist=row_dist, col_dist=col_dist)
CALL group%set_handle(group_handle)
!create the corresponding tensor proc grid and dist
pdims(1) = SIZE(mat_pgrid, 1); pdims(2) = SIZE(mat_pgrid, 2); pdims(3) = 1
CALL dbt_pgrid_create(group, pdims, t_pgrid)
nblk_3 = SIZE(blk_size_3)
CALL dbt_distribution_new(t_dist, t_pgrid, nd_dist_1=row_dist, nd_dist_2=col_dist, &
nd_dist_3=[(0, i=1, nblk_3)])
!create the tensor itself.
CALL dbt_create(pq_X, name="(pq|X)", dist=t_dist, map1_2d=[1, 2], map2_2d=[3], &
blk_size_1=blk_size_1, blk_size_2=blk_size_2, blk_size_3=blk_size_3)
!count the blocks to reserve !note: dbcsr takes care of only keeping unique indices
CALL neighbor_list_iterator_create(ab_iter, ab_nl)
CALL neighbor_list_iterator_create(ac_iter, ac_nl, search=.TRUE.)
nblk = 0
DO WHILE (neighbor_list_iterate(ab_iter) == 0)
CALL get_iterator_info(ab_iter, ikind=ikind, iatom=iatom, nkind=nkind, r=rab)
DO kkind = 1, nkind
CALL nl_set_sub_iterator(ac_iter, ikind, kkind, iatom)
DO WHILE (nl_sub_iterate(ac_iter) == 0)
IF (my_sort_bc) THEN
!we check for rbc or rac because of symmetry in ab_nl
CALL get_iterator_info(ac_iter, r=rac)
rbc(:) = rac(:) - rab(:)
IF (.NOT. (ALL(ABS(rbc) .LE. 1.0E-8_dp) .OR. ALL(ABS(rac) .LE. 1.0E-8_dp))) CYCLE
END IF
nblk = nblk + 1
END DO !ac_iter
END DO !kkind
END DO !ab_iter
CALL neighbor_list_iterator_release(ab_iter)
CALL neighbor_list_iterator_release(ac_iter)
ALLOCATE (idx1(nblk), idx2(nblk), idx3(nblk))
!actually reserve the blocks
CALL neighbor_list_iterator_create(ab_iter, ab_nl)
CALL neighbor_list_iterator_create(ac_iter, ac_nl, search=.TRUE.)
nblk = 0
DO WHILE (neighbor_list_iterate(ab_iter) == 0)
CALL get_iterator_info(ab_iter, ikind=ikind, iatom=iatom, jatom=jatom, nkind=nkind, r=rab)
DO kkind = 1, nkind
CALL nl_set_sub_iterator(ac_iter, ikind, kkind, iatom)
DO WHILE (nl_sub_iterate(ac_iter) == 0)
CALL get_iterator_info(ac_iter, jatom=katom, r=rac)
IF (my_sort_bc) THEN
!we check for rbc or rac because of symmetry in ab_nl
CALL get_iterator_info(ac_iter, r=rac)
rbc(:) = rac(:) - rab(:)
IF (.NOT. (ALL(ABS(rbc) .LE. 1.0E-8_dp) .OR. ALL(ABS(rac) .LE. 1.0E-8_dp))) CYCLE
END IF
nblk = nblk + 1
idx1(nblk) = iatom
idx2(nblk) = jatom
idx3(nblk) = katom
END DO !ac_iter
END DO !kkind
END DO !ab_iter
CALL neighbor_list_iterator_release(ab_iter)
CALL neighbor_list_iterator_release(ac_iter)
!TODO: Parallelize creation of block list.
!$OMP PARALLEL DEFAULT(NONE) SHARED(pq_X, nblk, idx1, idx2, idx3) PRIVATE(nblk_per_thread,A,b)
nblk_per_thread = nblk/omp_get_num_threads() + 1
a = omp_get_thread_num()*nblk_per_thread + 1
b = MIN(a + nblk_per_thread, nblk)
CALL dbt_reserve_blocks(pq_X, idx1(a:b), idx2(a:b), idx3(a:b))
!$OMP END PARALLEL
CALL dbt_finalize(pq_X)
!clean-up
CALL dbt_distribution_destroy(t_dist)
CALL dbt_pgrid_destroy(t_pgrid)
CALL timestop(handle)
END SUBROUTINE create_pqX_tensor
! **************************************************************************************************
!> \brief Fills the given 3 dimensional (pq|X) tensor with integrals
!> \param pq_X the tensor to fill
!> \param ab_nl the neighbor list for the first 2 centers
!> \param ac_nl the neighbor list for the first and third centers
!> \param basis_set_list_a basis sets for first center
!> \param basis_set_list_b basis sets for second center
!> \param basis_set_list_c basis sets for third center
!> \param potential_parameter the operator for the integrals
!> \param qs_env ...
!> \param only_bc_same_center same as in create_pqX_tensor
!> \param eps_screen threshold for possible screening
!> \note The following indices are happily mixed within this routine: First center i,a,p
!> Second center: j,b,q Third center: k,c,X
! **************************************************************************************************
SUBROUTINE fill_pqX_tensor(pq_X, ab_nl, ac_nl, basis_set_list_a, basis_set_list_b, &
basis_set_list_c, potential_parameter, qs_env, &
only_bc_same_center, eps_screen)
TYPE(dbt_type) :: pq_X
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: ab_nl, ac_nl
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list_a, basis_set_list_b, &
basis_set_list_c
TYPE(libint_potential_type) :: potential_parameter
TYPE(qs_environment_type), POINTER :: qs_env
LOGICAL, INTENT(IN), OPTIONAL :: only_bc_same_center
REAL(dp), INTENT(IN), OPTIONAL :: eps_screen
CHARACTER(len=*), PARAMETER :: routineN = 'fill_pqX_tensor'
INTEGER :: egfa, egfb, egfc, handle, i, iatom, ibasis, ikind, ilist, imax, iset, jatom, &
jkind, jset, katom, kkind, kset, m_max, max_nset, maxli, maxlj, maxlk, mepos, nbasis, &
ncoa, ncob, ncoc, ni, nj, nk, nseta, nsetb, nsetc, nthread, sgfa, sgfb, sgfc, unit_id
INTEGER, DIMENSION(:), POINTER :: la_max, la_min, lb_max, lb_min, lc_max, &
lc_min, npgfa, npgfb, npgfc, nsgfa, &
nsgfb, nsgfc
INTEGER, DIMENSION(:, :), POINTER :: first_sgfa, first_sgfb, first_sgfc
LOGICAL :: do_screen, my_sort_bc
REAL(dp) :: dij, dik, djk, my_eps_screen, ri(3), &
rij(3), rik(3), rj(3), rjk(3), rk(3), &
sabc_ext, screen_radius
REAL(dp), ALLOCATABLE, DIMENSION(:) :: ccp_buffer, cpp_buffer
REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: max_contr, max_contra, max_contrb, &
max_contrc
REAL(dp), ALLOCATABLE, DIMENSION(:, :, :) :: iabc, sabc, work
REAL(dp), DIMENSION(:), POINTER :: set_radius_a, set_radius_b, set_radius_c
REAL(dp), DIMENSION(:, :), POINTER :: rpgf_a, rpgf_b, rpgf_c, zeta, zetb, zetc
TYPE(cp_2d_r_p_type), DIMENSION(:, :), POINTER :: spb, spc, tspa
TYPE(cp_libint_t) :: lib
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
TYPE(gto_basis_set_type), POINTER :: basis_set, basis_set_a, basis_set_b, &
basis_set_c
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(o3c_container_type), POINTER :: o3c
TYPE(o3c_iterator_type) :: o3c_iterator
NULLIFY (basis_set, basis_set_list, para_env, la_max, la_min)
NULLIFY (lb_max, lb_min, lc_max, lc_min, npgfa, npgfb, npgfc, nsgfa, nsgfb, nsgfc)
NULLIFY (first_sgfa, first_sgfb, first_sgfc, set_radius_a, set_radius_b, set_radius_c)
NULLIFY (rpgf_a, rpgf_b, rpgf_c, zeta, zetb, zetc)
NULLIFY (basis_set_a, basis_set_b, basis_set_c, tspa, spb, spc)
CALL timeset(routineN, handle)
!Need the max l for each basis for libint (and overall max #of sets for screening)
nbasis = SIZE(basis_set_list_a)
max_nset = 0
maxli = 0
DO ibasis = 1, nbasis
CALL get_gto_basis_set(gto_basis_set=basis_set_list_a(ibasis)%gto_basis_set, &
maxl=imax, nset=iset, nsgf_set=nsgfa)
maxli = MAX(maxli, imax)
max_nset = MAX(max_nset, iset)
END DO
maxlj = 0
DO ibasis = 1, nbasis
CALL get_gto_basis_set(gto_basis_set=basis_set_list_b(ibasis)%gto_basis_set, &
maxl=imax, nset=iset, nsgf_set=nsgfb, npgf=npgfb)
maxlj = MAX(maxlj, imax)
max_nset = MAX(max_nset, iset)
END DO
maxlk = 0
DO ibasis = 1, nbasis
CALL get_gto_basis_set(gto_basis_set=basis_set_list_c(ibasis)%gto_basis_set, &
maxl=imax, nset=iset, npgf=npgfc)
maxlk = MAX(maxlk, imax)
max_nset = MAX(max_nset, iset)
END DO
m_max = maxli + maxlj + maxlk
!Screening
do_screen = .FALSE.
IF (PRESENT(eps_screen)) THEN
do_screen = .TRUE.
my_eps_screen = eps_screen
END IF
screen_radius = 0.0_dp
IF (potential_parameter%potential_type == do_potential_truncated .OR. &
potential_parameter%potential_type == do_potential_short) THEN
screen_radius = potential_parameter%cutoff_radius*cutoff_screen_factor
ELSE IF (potential_parameter%potential_type == do_potential_coulomb) THEN
screen_radius = 1000000.0_dp
END IF
!get maximum contraction values for abc_contract screening
IF (do_screen) THEN
!Allocate max_contraction arrays such that we have a specific value for each set/kind
ALLOCATE (max_contr(max_nset, nbasis), max_contra(max_nset, nbasis), &
max_contrb(max_nset, nbasis), max_contrc(max_nset, nbasis))
!Not the most elegent, but better than copying 3 times the same
DO ilist = 1, 3
IF (ilist == 1) basis_set_list => basis_set_list_a
IF (ilist == 2) basis_set_list => basis_set_list_b
IF (ilist == 3) basis_set_list => basis_set_list_c
max_contr = 0.0_dp
DO ibasis = 1, nbasis
basis_set => basis_set_list(ibasis)%gto_basis_set
DO iset = 1, basis_set%nset
ncoa = basis_set%npgf(iset)*ncoset(basis_set%lmax(iset))
sgfa = basis_set%first_sgf(1, iset)
egfa = sgfa + basis_set%nsgf_set(iset) - 1
max_contr(iset, ibasis) = &
MAXVAL((/(SUM(ABS(basis_set%sphi(1:ncoa, i))), i=sgfa, egfa)/))
END DO !iset
END DO !ibasis
IF (ilist == 1) max_contra(:, :) = max_contr(:, :)
IF (ilist == 2) max_contrb(:, :) = max_contr(:, :)
IF (ilist == 3) max_contrc(:, :) = max_contr(:, :)
END DO !ilist
DEALLOCATE (max_contr)
END IF !do_screen
!To minimize memory ops in contraction, we need to pre-allocate buffers, pre-tranpose sphi_a
!and also trim sphi in general to have contiguous arrays
ALLOCATE (tspa(max_nset, nbasis), spb(max_nset, nbasis), spc(max_nset, nbasis))
DO ibasis = 1, nbasis
DO iset = 1, max_nset
NULLIFY (tspa(iset, ibasis)%array)
NULLIFY (spb(iset, ibasis)%array)
NULLIFY (spc(iset, ibasis)%array)
END DO
END DO
DO ilist = 1, 3
DO ibasis = 1, nbasis
IF (ilist == 1) basis_set => basis_set_list_a(ibasis)%gto_basis_set
IF (ilist == 2) basis_set => basis_set_list_b(ibasis)%gto_basis_set
IF (ilist == 3) basis_set => basis_set_list_c(ibasis)%gto_basis_set
DO iset = 1, basis_set%nset
ncoa = basis_set%npgf(iset)*ncoset(basis_set%lmax(iset))
sgfa = basis_set%first_sgf(1, iset)
egfa = sgfa + basis_set%nsgf_set(iset) - 1
IF (ilist == 1) THEN
ALLOCATE (tspa(iset, ibasis)%array(basis_set%nsgf_set(iset), ncoa))
#if defined(__LIBXSMM)
CALL libxsmm_otrans(libxsmm_ptr(tspa(iset, ibasis)%array), &
libxsmm_ptr(basis_set%sphi(1, sgfa)), &
8, ncoa, egfa - sgfa + 1, &
SIZE(basis_set%sphi, 1), &
basis_set%nsgf_set(iset))
#else
tspa(iset, ibasis)%array(:, :) = TRANSPOSE(basis_set%sphi(1:ncoa, sgfa:egfa))
#endif
ELSE IF (ilist == 2) THEN
ALLOCATE (spb(iset, ibasis)%array(ncoa, basis_set%nsgf_set(iset)))
#if defined(__LIBXSMM)
CALL libxsmm_matcopy(libxsmm_ptr(spb(iset, ibasis)%array), &
libxsmm_ptr(basis_set%sphi(1, sgfa)), &
8, ncoa, egfa - sgfa + 1, &
SIZE(basis_set%sphi, 1), ncoa)
#else
spb(iset, ibasis)%array(:, :) = basis_set%sphi(1:ncoa, sgfa:egfa)
#endif
ELSE
ALLOCATE (spc(iset, ibasis)%array(ncoa, basis_set%nsgf_set(iset)))
#if defined(__LIBXSMM)
CALL libxsmm_matcopy(libxsmm_ptr(spc(iset, ibasis)%array), &
libxsmm_ptr(basis_set%sphi(1, sgfa)), &
8, ncoa, egfa - sgfa + 1, &
SIZE(basis_set%sphi, 1), ncoa)
#else
spc(iset, ibasis)%array(:, :) = basis_set%sphi(1:ncoa, sgfa:egfa)
#endif
END IF
END DO !iset
END DO !ibasis
END DO !ilist
my_sort_bc = .FALSE.
IF (PRESENT(only_bc_same_center)) my_sort_bc = only_bc_same_center
!Init the truncated Coulomb operator
CALL get_qs_env(qs_env, para_env=para_env)
IF (potential_parameter%potential_type == do_potential_truncated) THEN
!open the file only if necessary
IF (m_max > get_lmax_init()) THEN
IF (para_env%mepos == 0) THEN
CALL open_file(unit_number=unit_id, file_name=potential_parameter%filename)
END IF
CALL init(m_max, unit_id, para_env%mepos, para_env)
IF (para_env%mepos == 0) THEN
CALL close_file(unit_id)
END IF
END IF
END IF
!Inint the initial gamma function before the OMP region as it is not thread safe
CALL init_md_ftable(nmax=m_max)
!Strategy: we use the o3c iterator because it is OMP parallelized and also takes the
! only_bc_same_center argument. Only the dbcsr_put_block is critical
nthread = 1
!$ nthread = omp_get_max_threads()
ALLOCATE (o3c)
CALL init_o3c_container(o3c, 1, basis_set_list_a, basis_set_list_b, basis_set_list_c, &
ab_nl, ac_nl, only_bc_same_center=my_sort_bc)
CALL o3c_iterator_create(o3c, o3c_iterator, nthread=nthread)
!$OMP PARALLEL DEFAULT(NONE) &
!$OMP SHARED (pq_X,do_screen,max_nset,basis_set_list_a,max_contra,max_contrb,max_contrc,&
!$OMP basis_set_list_b, basis_set_list_c,ncoset,screen_radius,potential_parameter,&
!$OMP my_eps_screen,maxli,maxlj,maxlk,my_sort_bc,nthread,o3c,o3c_iterator,tspa,spb,spc) &
!$OMP PRIVATE (lib,i,mepos,work,iset,ncoa,sgfa,egfa,nseta,iatom,ikind,jatom,jkind,katom,kkind,&
!$OMP rij,rik,rjk,basis_set_a,nsetb,la_max,la_min,lb_max,lb_min,lc_max,lc_min,npgfa,&
!$OMP npgfb,npgfc,nsgfa,nsgfb,nsgfc,ri,rk,first_sgfa,first_sgfb,first_sgfc,nsetc,&
!$OMP set_radius_a,set_radius_b,set_radius_c,rj,rpgf_a,rpgf_b,rpgf_c,zeta,zetb,&
!$OMP zetc,basis_set_b,basis_set_c,dij,dik,djk,ni,nj,nk,iabc,sabc,jset,kset,&
!$OMP ncob,ncoc,sgfb,sgfc,egfb,egfc,sabc_ext,cpp_buffer,ccp_buffer)
mepos = 0
!$ mepos = omp_get_thread_num()
!each thread need its own libint object (internals may change at different rates)
CALL cp_libint_init_3eri(lib, MAX(maxli, maxlj, maxlk))
CALL cp_libint_set_contrdepth(lib, 1)
DO WHILE (o3c_iterate(o3c_iterator, mepos=mepos) == 0)
CALL get_o3c_iterator_info(o3c_iterator, mepos=mepos, ikind=ikind, jkind=jkind, kkind=kkind, &
iatom=iatom, jatom=jatom, katom=katom, rij=rij, rik=rik)
!get first center basis info
basis_set_a => basis_set_list_a(ikind)%gto_basis_set
first_sgfa => basis_set_a%first_sgf
la_max => basis_set_a%lmax
la_min => basis_set_a%lmin
npgfa => basis_set_a%npgf
nseta = basis_set_a%nset
nsgfa => basis_set_a%nsgf_set
zeta => basis_set_a%zet
rpgf_a => basis_set_a%pgf_radius
set_radius_a => basis_set_a%set_radius
ni = SUM(nsgfa)
!second center basis info
basis_set_b => basis_set_list_b(jkind)%gto_basis_set
first_sgfb => basis_set_b%first_sgf
lb_max => basis_set_b%lmax
lb_min => basis_set_b%lmin
npgfb => basis_set_b%npgf
nsetb = basis_set_b%nset
nsgfb => basis_set_b%nsgf_set
zetb => basis_set_b%zet
rpgf_b => basis_set_b%pgf_radius
set_radius_b => basis_set_b%set_radius
nj = SUM(nsgfb)
!third center basis info
basis_set_c => basis_set_list_c(kkind)%gto_basis_set
first_sgfc => basis_set_c%first_sgf
lc_max => basis_set_c%lmax
lc_min => basis_set_c%lmin
npgfc => basis_set_c%npgf
nsetc = basis_set_c%nset
nsgfc => basis_set_c%nsgf_set
zetc => basis_set_c%zet
rpgf_c => basis_set_c%pgf_radius
set_radius_c => basis_set_c%set_radius
nk = SUM(nsgfc)
!position and distances, only relative pos matter for libint
rjk = rik - rij
ri = 0.0_dp
rj = rij ! ri + rij
rk = rik ! ri + rik
djk = NORM2(rjk)
dij = NORM2(rij)
dik = NORM2(rik)
!sgf integrals
ALLOCATE (iabc(ni, nj, nk))
iabc(:, :, :) = 0.0_dp
DO iset = 1, nseta
ncoa = npgfa(iset)*ncoset(la_max(iset))
sgfa = first_sgfa(1, iset)
egfa = sgfa + nsgfa(iset) - 1
DO jset = 1, nsetb
ncob = npgfb(jset)*ncoset(lb_max(jset))
sgfb = first_sgfb(1, jset)
egfb = sgfb + nsgfb(jset) - 1
!screening (overlap)
IF (set_radius_a(iset) + set_radius_b(jset) < dij) CYCLE
DO kset = 1, nsetc
ncoc = npgfc(kset)*ncoset(lc_max(kset))
sgfc = first_sgfc(1, kset)
egfc = sgfc + nsgfc(kset) - 1
!screening (potential)
IF (set_radius_a(iset) + set_radius_c(kset) + screen_radius < dik) CYCLE
IF (set_radius_b(jset) + set_radius_c(kset) + screen_radius < djk) CYCLE
!pgf integrals
ALLOCATE (sabc(ncoa, ncob, ncoc))
sabc(:, :, :) = 0.0_dp
IF (do_screen) THEN
CALL eri_3center(sabc, la_min(iset), la_max(iset), npgfa(iset), zeta(:, iset), &
rpgf_a(:, iset), ri, lb_min(jset), lb_max(jset), npgfb(jset), &
zetb(:, jset), rpgf_b(:, jset), rj, lc_min(kset), lc_max(kset), &
npgfc(kset), zetc(:, kset), rpgf_c(:, kset), rk, dij, dik, &
djk, lib, potential_parameter, int_abc_ext=sabc_ext)
IF (my_eps_screen > sabc_ext*(max_contra(iset, ikind)* &
max_contrb(jset, jkind)* &
max_contrc(kset, kkind))) THEN
DEALLOCATE (sabc)
CYCLE
END IF
ELSE
CALL eri_3center(sabc, la_min(iset), la_max(iset), npgfa(iset), zeta(:, iset), &
rpgf_a(:, iset), ri, lb_min(jset), lb_max(jset), npgfb(jset), &
zetb(:, jset), rpgf_b(:, jset), rj, lc_min(kset), lc_max(kset), &
npgfc(kset), zetc(:, kset), rpgf_c(:, kset), rk, dij, dik, &
djk, lib, potential_parameter)
END IF
ALLOCATE (work(nsgfa(iset), nsgfb(jset), nsgfc(kset)))
CALL abc_contract_xsmm(work, sabc, tspa(iset, ikind)%array, spb(jset, jkind)%array, &
spc(kset, kkind)%array, ncoa, ncob, ncoc, nsgfa(iset), &
nsgfb(jset), nsgfc(kset), cpp_buffer, ccp_buffer)
iabc(sgfa:egfa, sgfb:egfb, sgfc:egfc) = work(:, :, :)
DEALLOCATE (sabc, work)
END DO !kset
END DO !jset
END DO !iset
!Add the integral to the proper tensor block
!$OMP CRITICAL
CALL dbt_put_block(pq_X, [iatom, jatom, katom], SHAPE(iabc), iabc, summation=.TRUE.)
!$OMP END CRITICAL
DEALLOCATE (iabc)
END DO !o3c_iterator
IF (ALLOCATED(ccp_buffer)) DEALLOCATE (ccp_buffer)
IF (ALLOCATED(cpp_buffer)) DEALLOCATE (cpp_buffer)
CALL cp_libint_cleanup_3eri(lib)
!$OMP END PARALLEL
CALL o3c_iterator_release(o3c_iterator)
CALL release_o3c_container(o3c)
DEALLOCATE (o3c)
DO iset = 1, max_nset
DO ibasis = 1, nbasis
IF (ASSOCIATED(tspa(iset, ibasis)%array)) DEALLOCATE (tspa(iset, ibasis)%array)
IF (ASSOCIATED(spb(iset, ibasis)%array)) DEALLOCATE (spb(iset, ibasis)%array)
IF (ASSOCIATED(spc(iset, ibasis)%array)) DEALLOCATE (spc(iset, ibasis)%array)
END DO
END DO
DEALLOCATE (tspa, spb, spc)
CALL timestop(handle)
END SUBROUTINE fill_pqX_tensor
! **************************************************************************************************
!> \brief Builds a neighbor lists set for overlaping 2-center S_ab, where b is restricted on a
!> a given list of atoms. Used for Coulomb RI where (aI|P) = sum_b C_bI (ab|P), where
!> contraction coeff C_bI is assumed to be non-zero only on excited atoms
!> \param ab_list the neighbor list
!> \param basis_a basis set list for atom a
!> \param basis_b basis set list for atom b
!> \param qs_env ...
!> \param excited_atoms the indices of the excited atoms on which b is centered
!> \param ext_dist2d use an external distribution2d
! **************************************************************************************************
SUBROUTINE build_xas_tdp_ovlp_nl(ab_list, basis_a, basis_b, qs_env, excited_atoms, ext_dist2d)
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: ab_list
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_a, basis_b
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER, DIMENSION(:), INTENT(IN), OPTIONAL :: excited_atoms
TYPE(distribution_2d_type), OPTIONAL, POINTER :: ext_dist2d
INTEGER :: ikind, nkind
LOGICAL :: my_restrictb
LOGICAL, ALLOCATABLE, DIMENSION(:) :: a_present, b_present
REAL(dp) :: subcells
REAL(dp), ALLOCATABLE, DIMENSION(:) :: a_radius, b_radius
REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: pair_radius
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell
TYPE(distribution_1d_type), POINTER :: distribution_1d
TYPE(distribution_2d_type), POINTER :: distribution_2d
TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:) :: atom2d
TYPE(molecule_type), DIMENSION(:), POINTER :: molecule_set
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
NULLIFY (atomic_kind_set, distribution_1d, distribution_2d, molecule_set, particle_set, cell)
! Initialization
CALL get_qs_env(qs_env, nkind=nkind)
CALL section_vals_val_get(qs_env%input, "DFT%SUBCELLS", r_val=subcells)
my_restrictb = .FALSE.
IF (PRESENT(excited_atoms)) THEN
my_restrictb = .TRUE.
END IF
ALLOCATE (a_present(nkind), b_present(nkind))
a_present = .FALSE.
b_present = .FALSE.
ALLOCATE (a_radius(nkind), b_radius(nkind))
a_radius = 0.0_dp
b_radius = 0.0_dp
! Set up the radii
DO ikind = 1, nkind
IF (ASSOCIATED(basis_a(ikind)%gto_basis_set)) THEN
a_present(ikind) = .TRUE.
CALL get_gto_basis_set(basis_a(ikind)%gto_basis_set, kind_radius=a_radius(ikind))
END IF
IF (ASSOCIATED(basis_b(ikind)%gto_basis_set)) THEN
b_present(ikind) = .TRUE.
CALL get_gto_basis_set(basis_b(ikind)%gto_basis_set, kind_radius=b_radius(ikind))
END IF
END DO !ikind
ALLOCATE (pair_radius(nkind, nkind))
pair_radius = 0.0_dp
CALL pair_radius_setup(a_present, b_present, a_radius, b_radius, pair_radius)
! Set up the nl
CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, cell=cell, &
distribution_2d=distribution_2d, local_particles=distribution_1d, &
particle_set=particle_set, molecule_set=molecule_set)
!use an external distribution_2d if required
IF (PRESENT(ext_dist2d)) distribution_2d => ext_dist2d
ALLOCATE (atom2d(nkind))
CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
molecule_set, .FALSE., particle_set)
IF (my_restrictb) THEN
CALL build_neighbor_lists(ab_list, particle_set, atom2d, cell, pair_radius, subcells, &
atomb_to_keep=excited_atoms, nlname="XAS_TDP_ovlp_nl")
ELSE
CALL build_neighbor_lists(ab_list, particle_set, atom2d, cell, pair_radius, subcells, &
nlname="XAS_TDP_ovlp_nl")
END IF
! Clean-up
CALL atom2d_cleanup(atom2d)
END SUBROUTINE build_xas_tdp_ovlp_nl
! **************************************************************************************************
!> \brief Builds a neighbor lists set taylored for 3-center integral within XAS TDP, such that only
!> excited atoms are taken into account for the list_c
!> \param ac_list the neighbor list ready for 3-center integrals
!> \param basis_a basis set list for atom a
!> \param basis_c basis set list for atom c
!> \param op_type to indicate whther the list should be built with overlap, Coulomb or else in mind
!> \param qs_env ...
!> \param excited_atoms the indices of the excited atoms to consider (if not given, all atoms are taken)
!> \param x_range in case some truncated/screened operator is used, gives its range
!> \param ext_dist2d external distribution_2d to be used
!> \note Based on setup_neighbor_list with added features
! **************************************************************************************************
SUBROUTINE build_xas_tdp_3c_nl(ac_list, basis_a, basis_c, op_type, qs_env, excited_atoms, &
x_range, ext_dist2d)
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: ac_list
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_a, basis_c
INTEGER, INTENT(IN) :: op_type
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER, DIMENSION(:), INTENT(IN), OPTIONAL :: excited_atoms
REAL(dp), INTENT(IN), OPTIONAL :: x_range
TYPE(distribution_2d_type), OPTIONAL, POINTER :: ext_dist2d
INTEGER :: ikind, nkind
LOGICAL :: sort_atoms
LOGICAL, ALLOCATABLE, DIMENSION(:) :: a_present, c_present
REAL(dp) :: subcells
REAL(dp), ALLOCATABLE, DIMENSION(:) :: a_radius, c_radius
REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: pair_radius
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell
TYPE(distribution_1d_type), POINTER :: distribution_1d
TYPE(distribution_2d_type), POINTER :: distribution_2d
TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:) :: atom2d
TYPE(molecule_type), DIMENSION(:), POINTER :: molecule_set
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
NULLIFY (atomic_kind_set, distribution_1d, distribution_2d, molecule_set, particle_set, cell)
! Initialization
CALL get_qs_env(qs_env, nkind=nkind)
CALL section_vals_val_get(qs_env%input, "DFT%SUBCELLS", r_val=subcells)
sort_atoms = .FALSE.
IF ((PRESENT(excited_atoms))) sort_atoms = .TRUE.
ALLOCATE (a_present(nkind), c_present(nkind))
a_present = .FALSE.
c_present = .FALSE.
ALLOCATE (a_radius(nkind), c_radius(nkind))
a_radius = 0.0_dp
c_radius = 0.0_dp
! Set up the radii, depending on the operator type
IF (op_type == do_potential_id) THEN
!overlap => use the kind radius for both a and c
DO ikind = 1, nkind
!orbital basis set
IF (ASSOCIATED(basis_a(ikind)%gto_basis_set)) THEN
a_present(ikind) = .TRUE.
CALL get_gto_basis_set(basis_a(ikind)%gto_basis_set, kind_radius=a_radius(ikind))
END IF
!RI_XAS basis set
IF (ASSOCIATED(basis_c(ikind)%gto_basis_set)) THEN
c_present(ikind) = .TRUE.
CALL get_gto_basis_set(basis_c(ikind)%gto_basis_set, kind_radius=c_radius(ikind))
END IF
END DO !ikind
ELSE IF (op_type == do_potential_coulomb) THEN
!Coulomb operator, virtually infinite range => set c_radius to arbitrarily large number
DO ikind = 1, nkind
IF (ASSOCIATED(basis_c(ikind)%gto_basis_set)) THEN
c_present(ikind) = .TRUE.
c_radius(ikind) = 1000000.0_dp
END IF
IF (ASSOCIATED(basis_a(ikind)%gto_basis_set)) a_present(ikind) = .TRUE.
END DO !ikind
ELSE IF (op_type == do_potential_truncated .OR. op_type == do_potential_short) THEN
!Truncated coulomb/short range: set c_radius to x_range + the kind_radii
DO ikind = 1, nkind
IF (ASSOCIATED(basis_a(ikind)%gto_basis_set)) THEN
a_present(ikind) = .TRUE.
CALL get_gto_basis_set(basis_a(ikind)%gto_basis_set, kind_radius=a_radius(ikind))
END IF
IF (ASSOCIATED(basis_c(ikind)%gto_basis_set)) THEN
c_present(ikind) = .TRUE.
CALL get_gto_basis_set(basis_c(ikind)%gto_basis_set, kind_radius=c_radius(ikind))
c_radius(ikind) = c_radius(ikind) + x_range
END IF
END DO !ikind
ELSE
CPABORT("Operator not known")
END IF
ALLOCATE (pair_radius(nkind, nkind))
pair_radius = 0.0_dp
CALL pair_radius_setup(a_present, c_present, a_radius, c_radius, pair_radius)
! Actually setup the list
CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, cell=cell, &
distribution_2d=distribution_2d, local_particles=distribution_1d, &
particle_set=particle_set, molecule_set=molecule_set)
!use an external distribution_2d if required
IF (PRESENT(ext_dist2d)) distribution_2d => ext_dist2d
ALLOCATE (atom2d(nkind))
CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
molecule_set, .FALSE., particle_set)
IF (sort_atoms) THEN
CALL build_neighbor_lists(ac_list, particle_set, atom2d, cell, pair_radius, subcells, &
operator_type="ABC", atomb_to_keep=excited_atoms, &
nlname="XAS_TDP_3c_nl")
ELSE
CALL build_neighbor_lists(ac_list, particle_set, atom2d, cell, pair_radius, subcells, &
operator_type="ABC", nlname="XAS_TDP_3c_nl")
END IF
! Clean-up
CALL atom2d_cleanup(atom2d)
END SUBROUTINE build_xas_tdp_3c_nl
! **************************************************************************************************
!> \brief Returns an optimized distribution_2d for the given neighbor lists based on an evaluation
!> of the cost of the corresponding 3-center integrals
!> \param opt_3c_dist2d the optimized distribution_2d
!> \param ab_list ...
!> \param ac_list ...
!> \param basis_set_a ...
!> \param basis_set_b ...
!> \param basis_set_c ...
!> \param qs_env ...
!> \param only_bc_same_center ...
! **************************************************************************************************
SUBROUTINE get_opt_3c_dist2d(opt_3c_dist2d, ab_list, ac_list, basis_set_a, basis_set_b, &
basis_set_c, qs_env, only_bc_same_center)
TYPE(distribution_2d_type), POINTER :: opt_3c_dist2d
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: ab_list, ac_list
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_a, basis_set_b, basis_set_c
TYPE(qs_environment_type), POINTER :: qs_env
LOGICAL, INTENT(IN), OPTIONAL :: only_bc_same_center
CHARACTER(len=*), PARAMETER :: routineN = 'get_opt_3c_dist2d'
INTEGER :: handle, i, iatom, ikind, ip, jatom, &
jkind, kkind, mypcol, myprow, n, &
natom, nkind, npcol, nprow, nsgfa, &
nsgfb, nsgfc
INTEGER, ALLOCATABLE, DIMENSION(:) :: nparticle_local_col, nparticle_local_row
INTEGER, DIMENSION(:, :), POINTER :: col_dist, row_dist
LOGICAL :: my_sort_bc
REAL(dp) :: cost, rab(3), rac(3), rbc(3)
REAL(dp), ALLOCATABLE, DIMENSION(:) :: col_cost, col_proc_cost, row_cost, &
row_proc_cost
TYPE(cp_1d_i_p_type), DIMENSION(:), POINTER :: local_particle_col, local_particle_row
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(neighbor_list_iterator_p_type), &
DIMENSION(:), POINTER :: ab_iter, ac_iter
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
NULLIFY (para_env, col_dist, row_dist, blacs_env, qs_kind_set, particle_set)
NULLIFY (local_particle_col, local_particle_row, ab_iter, ac_iter)
CALL timeset(routineN, handle)
!Idea: create a,b and a,c nl_iterators in the original dist, then loop over them and compute the
! cost of each ab pairs and project on the col/row. Then distribute the atom col/row to
! the proc col/row in order to spread out the cost as much as possible
my_sort_bc = .FALSE.
IF (PRESENT(only_bc_same_center)) my_sort_bc = only_bc_same_center
CALL get_qs_env(qs_env, natom=natom, para_env=para_env, blacs_env=blacs_env, &
qs_kind_set=qs_kind_set, particle_set=particle_set, nkind=nkind)
myprow = blacs_env%mepos(1) + 1
mypcol = blacs_env%mepos(2) + 1
nprow = blacs_env%num_pe(1)
npcol = blacs_env%num_pe(2)
ALLOCATE (col_cost(natom), row_cost(natom))
col_cost = 0.0_dp; row_cost = 0.0_dp
ALLOCATE (row_dist(natom, 2), col_dist(natom, 2))
row_dist = 1; col_dist = 1
CALL neighbor_list_iterator_create(ab_iter, ab_list)
CALL neighbor_list_iterator_create(ac_iter, ac_list, search=.TRUE.)
DO WHILE (neighbor_list_iterate(ab_iter) == 0)
CALL get_iterator_info(ab_iter, ikind=ikind, jkind=jkind, iatom=iatom, jatom=jatom, r=rab)
DO kkind = 1, nkind
CALL nl_set_sub_iterator(ac_iter, ikind, kkind, iatom)
DO WHILE (nl_sub_iterate(ac_iter) == 0)
IF (my_sort_bc) THEN
!only take a,b,c if b,c (or a,c because of symmetry) share the same center
CALL get_iterator_info(ac_iter, r=rac)
rbc(:) = rac(:) - rab(:)
IF (.NOT. (ALL(ABS(rbc) .LE. 1.0E-8_dp) .OR. ALL(ABS(rac) .LE. 1.0E-8_dp))) CYCLE
END IF
!Use the size of integral as measure as contraciton cost seems to dominate
nsgfa = basis_set_a(ikind)%gto_basis_set%nsgf
nsgfb = basis_set_b(jkind)%gto_basis_set%nsgf
nsgfc = basis_set_c(kkind)%gto_basis_set%nsgf
cost = REAL(nsgfa*nsgfb*nsgfc, dp)
row_cost(iatom) = row_cost(iatom) + cost
col_cost(jatom) = col_cost(jatom) + cost
END DO !ac_iter
END DO !kkind
END DO !ab_iter
CALL neighbor_list_iterator_release(ab_iter)
CALL neighbor_list_iterator_release(ac_iter)
CALL para_env%sum(row_cost)
CALL para_env%sum(col_cost)
!Distribute the cost as evenly as possible
ALLOCATE (col_proc_cost(npcol), row_proc_cost(nprow))